PR target/77416
[official-gcc.git] / gcc / dwarf2out.c
blob169da86dace18f29cf16dd896ab25bc6b70e2abf
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, 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, get_DW_FORM_name (str_form));
11555 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11556 if (str_form == DW_FORM_string)
11558 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11559 for (i = 1 - idx_offset; i < ndirs; i++)
11560 dw2_asm_output_nstring (dirs[i].path,
11561 dirs[i].length
11562 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11563 "Directory Entry: %#x", i + idx_offset);
11565 else
11567 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11568 for (i = 1 - idx_offset; i < ndirs; i++)
11570 const char *str
11571 = ggc_alloc_string (dirs[i].path,
11572 dirs[i].length
11573 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11574 output_line_string (str_form, str, "Directory Entry",
11575 (unsigned) i + idx_offset);
11579 else
11581 for (i = 1 - idx_offset; i < ndirs; i++)
11582 dw2_asm_output_nstring (dirs[i].path,
11583 dirs[i].length
11584 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11585 "Directory Entry: %#x", i + idx_offset);
11587 dw2_asm_output_data (1, 0, "End directory table");
11590 /* We have to emit them in the order of emitted_number since that's
11591 used in the debug info generation. To do this efficiently we
11592 generate a back-mapping of the indices first. */
11593 backmap = XALLOCAVEC (int, numfiles);
11594 for (i = 0; i < numfiles; i++)
11595 backmap[files[i].file_idx->emitted_number - 1] = i;
11597 if (dwarf_version >= 5)
11599 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11600 if (filename0 == NULL)
11601 filename0 = "";
11602 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11603 DW_FORM_data2. Choose one based on the number of directories
11604 and how much space would they occupy in each encoding.
11605 If we have at most 256 directories, all indexes fit into
11606 a single byte, so DW_FORM_data1 is most compact (if there
11607 are at most 128 directories, DW_FORM_udata would be as
11608 compact as that, but not shorter and slower to decode). */
11609 if (ndirs + idx_offset <= 256)
11610 idx_form = DW_FORM_data1;
11611 /* If there are more than 65536 directories, we have to use
11612 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11613 Otherwise, compute what space would occupy if all the indexes
11614 used DW_FORM_udata - sum - and compare that to how large would
11615 be DW_FORM_data2 encoding, and pick the more efficient one. */
11616 else if (ndirs + idx_offset <= 65536)
11618 unsigned HOST_WIDE_INT sum = 1;
11619 for (i = 0; i < numfiles; i++)
11621 int file_idx = backmap[i];
11622 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11623 sum += size_of_uleb128 (dir_idx);
11625 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11626 idx_form = DW_FORM_data2;
11628 #ifdef VMS_DEBUGGING_INFO
11629 dw2_asm_output_data (1, 4, "File name entry format count");
11630 #else
11631 dw2_asm_output_data (1, 2, "File name entry format count");
11632 #endif
11633 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11634 dw2_asm_output_data_uleb128 (str_form, get_DW_FORM_name (str_form));
11635 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11636 "DW_LNCT_directory_index");
11637 dw2_asm_output_data_uleb128 (idx_form, get_DW_FORM_name (idx_form));
11638 #ifdef VMS_DEBUGGING_INFO
11639 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11640 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11641 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11642 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11643 #endif
11644 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11646 output_line_string (str_form, filename0, "File Entry", 0);
11648 /* Include directory index. */
11649 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11650 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11651 0, NULL);
11652 else
11653 dw2_asm_output_data_uleb128 (0, NULL);
11655 #ifdef VMS_DEBUGGING_INFO
11656 dw2_asm_output_data_uleb128 (0, NULL);
11657 dw2_asm_output_data_uleb128 (0, NULL);
11658 #endif
11661 /* Now write all the file names. */
11662 for (i = 0; i < numfiles; i++)
11664 int file_idx = backmap[i];
11665 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11667 #ifdef VMS_DEBUGGING_INFO
11668 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11670 /* Setting these fields can lead to debugger miscomparisons,
11671 but VMS Debug requires them to be set correctly. */
11673 int ver;
11674 long long cdt;
11675 long siz;
11676 int maxfilelen = (strlen (files[file_idx].path)
11677 + dirs[dir_idx].length
11678 + MAX_VMS_VERSION_LEN + 1);
11679 char *filebuf = XALLOCAVEC (char, maxfilelen);
11681 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11682 snprintf (filebuf, maxfilelen, "%s;%d",
11683 files[file_idx].path + dirs[dir_idx].length, ver);
11685 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11687 /* Include directory index. */
11688 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11689 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11690 dir_idx + idx_offset, NULL);
11691 else
11692 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11694 /* Modification time. */
11695 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11696 &cdt, 0, 0, 0) == 0)
11697 ? cdt : 0, NULL);
11699 /* File length in bytes. */
11700 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11701 0, &siz, 0, 0) == 0)
11702 ? siz : 0, NULL);
11703 #else
11704 output_line_string (str_form,
11705 files[file_idx].path + dirs[dir_idx].length,
11706 "File Entry", (unsigned) i + 1);
11708 /* Include directory index. */
11709 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11710 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11711 dir_idx + idx_offset, NULL);
11712 else
11713 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11715 if (dwarf_version >= 5)
11716 continue;
11718 /* Modification time. */
11719 dw2_asm_output_data_uleb128 (0, NULL);
11721 /* File length in bytes. */
11722 dw2_asm_output_data_uleb128 (0, NULL);
11723 #endif /* VMS_DEBUGGING_INFO */
11726 if (dwarf_version < 5)
11727 dw2_asm_output_data (1, 0, "End file name table");
11731 /* Output one line number table into the .debug_line section. */
11733 static void
11734 output_one_line_info_table (dw_line_info_table *table)
11736 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11737 unsigned int current_line = 1;
11738 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11739 dw_line_info_entry *ent;
11740 size_t i;
11742 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11744 switch (ent->opcode)
11746 case LI_set_address:
11747 /* ??? Unfortunately, we have little choice here currently, and
11748 must always use the most general form. GCC does not know the
11749 address delta itself, so we can't use DW_LNS_advance_pc. Many
11750 ports do have length attributes which will give an upper bound
11751 on the address range. We could perhaps use length attributes
11752 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11753 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11755 /* This can handle any delta. This takes
11756 4+DWARF2_ADDR_SIZE bytes. */
11757 dw2_asm_output_data (1, 0, "set address %s", line_label);
11758 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11759 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11760 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11761 break;
11763 case LI_set_line:
11764 if (ent->val == current_line)
11766 /* We still need to start a new row, so output a copy insn. */
11767 dw2_asm_output_data (1, DW_LNS_copy,
11768 "copy line %u", current_line);
11770 else
11772 int line_offset = ent->val - current_line;
11773 int line_delta = line_offset - DWARF_LINE_BASE;
11775 current_line = ent->val;
11776 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11778 /* This can handle deltas from -10 to 234, using the current
11779 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11780 This takes 1 byte. */
11781 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11782 "line %u", current_line);
11784 else
11786 /* This can handle any delta. This takes at least 4 bytes,
11787 depending on the value being encoded. */
11788 dw2_asm_output_data (1, DW_LNS_advance_line,
11789 "advance to line %u", current_line);
11790 dw2_asm_output_data_sleb128 (line_offset, NULL);
11791 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11794 break;
11796 case LI_set_file:
11797 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11798 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11799 break;
11801 case LI_set_column:
11802 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11803 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11804 break;
11806 case LI_negate_stmt:
11807 current_is_stmt = !current_is_stmt;
11808 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11809 "is_stmt %d", current_is_stmt);
11810 break;
11812 case LI_set_prologue_end:
11813 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11814 "set prologue end");
11815 break;
11817 case LI_set_epilogue_begin:
11818 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11819 "set epilogue begin");
11820 break;
11822 case LI_set_discriminator:
11823 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11824 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11825 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11826 dw2_asm_output_data_uleb128 (ent->val, NULL);
11827 break;
11831 /* Emit debug info for the address of the end of the table. */
11832 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11833 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11834 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11835 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11837 dw2_asm_output_data (1, 0, "end sequence");
11838 dw2_asm_output_data_uleb128 (1, NULL);
11839 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11842 /* Output the source line number correspondence information. This
11843 information goes into the .debug_line section. */
11845 static void
11846 output_line_info (bool prologue_only)
11848 static unsigned int generation;
11849 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11850 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11851 bool saw_one = false;
11852 int opc;
11854 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11855 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11856 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11857 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11859 if (!XCOFF_DEBUGGING_INFO)
11861 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11862 dw2_asm_output_data (4, 0xffffffff,
11863 "Initial length escape value indicating 64-bit DWARF extension");
11864 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11865 "Length of Source Line Info");
11868 ASM_OUTPUT_LABEL (asm_out_file, l1);
11870 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11871 if (dwarf_version >= 5)
11873 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11874 dw2_asm_output_data (1, 0, "Segment Size");
11876 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11877 ASM_OUTPUT_LABEL (asm_out_file, p1);
11879 /* Define the architecture-dependent minimum instruction length (in bytes).
11880 In this implementation of DWARF, this field is used for information
11881 purposes only. Since GCC generates assembly language, we have no
11882 a priori knowledge of how many instruction bytes are generated for each
11883 source line, and therefore can use only the DW_LNE_set_address and
11884 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11885 this as '1', which is "correct enough" for all architectures,
11886 and don't let the target override. */
11887 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11889 if (dwarf_version >= 4)
11890 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11891 "Maximum Operations Per Instruction");
11892 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11893 "Default is_stmt_start flag");
11894 dw2_asm_output_data (1, DWARF_LINE_BASE,
11895 "Line Base Value (Special Opcodes)");
11896 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11897 "Line Range Value (Special Opcodes)");
11898 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11899 "Special Opcode Base");
11901 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11903 int n_op_args;
11904 switch (opc)
11906 case DW_LNS_advance_pc:
11907 case DW_LNS_advance_line:
11908 case DW_LNS_set_file:
11909 case DW_LNS_set_column:
11910 case DW_LNS_fixed_advance_pc:
11911 case DW_LNS_set_isa:
11912 n_op_args = 1;
11913 break;
11914 default:
11915 n_op_args = 0;
11916 break;
11919 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11920 opc, n_op_args);
11923 /* Write out the information about the files we use. */
11924 output_file_names ();
11925 ASM_OUTPUT_LABEL (asm_out_file, p2);
11926 if (prologue_only)
11928 /* Output the marker for the end of the line number info. */
11929 ASM_OUTPUT_LABEL (asm_out_file, l2);
11930 return;
11933 if (separate_line_info)
11935 dw_line_info_table *table;
11936 size_t i;
11938 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11939 if (table->in_use)
11941 output_one_line_info_table (table);
11942 saw_one = true;
11945 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
11947 output_one_line_info_table (cold_text_section_line_info);
11948 saw_one = true;
11951 /* ??? Some Darwin linkers crash on a .debug_line section with no
11952 sequences. Further, merely a DW_LNE_end_sequence entry is not
11953 sufficient -- the address column must also be initialized.
11954 Make sure to output at least one set_address/end_sequence pair,
11955 choosing .text since that section is always present. */
11956 if (text_section_line_info->in_use || !saw_one)
11957 output_one_line_info_table (text_section_line_info);
11959 /* Output the marker for the end of the line number info. */
11960 ASM_OUTPUT_LABEL (asm_out_file, l2);
11963 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
11965 static inline bool
11966 need_endianity_attribute_p (bool reverse)
11968 return reverse && (dwarf_version >= 3 || !dwarf_strict);
11971 /* Given a pointer to a tree node for some base type, return a pointer to
11972 a DIE that describes the given type. REVERSE is true if the type is
11973 to be interpreted in the reverse storage order wrt the target order.
11975 This routine must only be called for GCC type nodes that correspond to
11976 Dwarf base (fundamental) types. */
11978 static dw_die_ref
11979 base_type_die (tree type, bool reverse)
11981 dw_die_ref base_type_result;
11982 enum dwarf_type encoding;
11983 bool fpt_used = false;
11984 struct fixed_point_type_info fpt_info;
11985 tree type_bias = NULL_TREE;
11987 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11988 return 0;
11990 /* If this is a subtype that should not be emitted as a subrange type,
11991 use the base type. See subrange_type_for_debug_p. */
11992 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11993 type = TREE_TYPE (type);
11995 switch (TREE_CODE (type))
11997 case INTEGER_TYPE:
11998 if ((dwarf_version >= 4 || !dwarf_strict)
11999 && TYPE_NAME (type)
12000 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12001 && DECL_IS_BUILTIN (TYPE_NAME (type))
12002 && DECL_NAME (TYPE_NAME (type)))
12004 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12005 if (strcmp (name, "char16_t") == 0
12006 || strcmp (name, "char32_t") == 0)
12008 encoding = DW_ATE_UTF;
12009 break;
12012 if ((dwarf_version >= 3 || !dwarf_strict)
12013 && lang_hooks.types.get_fixed_point_type_info)
12015 memset (&fpt_info, 0, sizeof (fpt_info));
12016 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12018 fpt_used = true;
12019 encoding = ((TYPE_UNSIGNED (type))
12020 ? DW_ATE_unsigned_fixed
12021 : DW_ATE_signed_fixed);
12022 break;
12025 if (TYPE_STRING_FLAG (type))
12027 if (TYPE_UNSIGNED (type))
12028 encoding = DW_ATE_unsigned_char;
12029 else
12030 encoding = DW_ATE_signed_char;
12032 else if (TYPE_UNSIGNED (type))
12033 encoding = DW_ATE_unsigned;
12034 else
12035 encoding = DW_ATE_signed;
12037 if (!dwarf_strict
12038 && lang_hooks.types.get_type_bias)
12039 type_bias = lang_hooks.types.get_type_bias (type);
12040 break;
12042 case REAL_TYPE:
12043 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12045 if (dwarf_version >= 3 || !dwarf_strict)
12046 encoding = DW_ATE_decimal_float;
12047 else
12048 encoding = DW_ATE_lo_user;
12050 else
12051 encoding = DW_ATE_float;
12052 break;
12054 case FIXED_POINT_TYPE:
12055 if (!(dwarf_version >= 3 || !dwarf_strict))
12056 encoding = DW_ATE_lo_user;
12057 else if (TYPE_UNSIGNED (type))
12058 encoding = DW_ATE_unsigned_fixed;
12059 else
12060 encoding = DW_ATE_signed_fixed;
12061 break;
12063 /* Dwarf2 doesn't know anything about complex ints, so use
12064 a user defined type for it. */
12065 case COMPLEX_TYPE:
12066 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12067 encoding = DW_ATE_complex_float;
12068 else
12069 encoding = DW_ATE_lo_user;
12070 break;
12072 case BOOLEAN_TYPE:
12073 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12074 encoding = DW_ATE_boolean;
12075 break;
12077 default:
12078 /* No other TREE_CODEs are Dwarf fundamental types. */
12079 gcc_unreachable ();
12082 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12084 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12085 int_size_in_bytes (type));
12086 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12088 if (need_endianity_attribute_p (reverse))
12089 add_AT_unsigned (base_type_result, DW_AT_endianity,
12090 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12092 if (fpt_used)
12094 switch (fpt_info.scale_factor_kind)
12096 case fixed_point_scale_factor_binary:
12097 add_AT_int (base_type_result, DW_AT_binary_scale,
12098 fpt_info.scale_factor.binary);
12099 break;
12101 case fixed_point_scale_factor_decimal:
12102 add_AT_int (base_type_result, DW_AT_decimal_scale,
12103 fpt_info.scale_factor.decimal);
12104 break;
12106 case fixed_point_scale_factor_arbitrary:
12107 /* Arbitrary scale factors cannot be described in standard DWARF,
12108 yet. */
12109 if (!dwarf_strict)
12111 /* Describe the scale factor as a rational constant. */
12112 const dw_die_ref scale_factor
12113 = new_die (DW_TAG_constant, comp_unit_die (), type);
12115 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12116 fpt_info.scale_factor.arbitrary.numerator);
12117 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12118 fpt_info.scale_factor.arbitrary.denominator);
12120 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12122 break;
12124 default:
12125 gcc_unreachable ();
12129 if (type_bias)
12130 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12131 dw_scalar_form_constant
12132 | dw_scalar_form_exprloc
12133 | dw_scalar_form_reference,
12134 NULL);
12136 add_pubtype (type, base_type_result);
12138 return base_type_result;
12141 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12142 named 'auto' in its type: return true for it, false otherwise. */
12144 static inline bool
12145 is_cxx_auto (tree type)
12147 if (is_cxx ())
12149 tree name = TYPE_IDENTIFIER (type);
12150 if (name == get_identifier ("auto")
12151 || name == get_identifier ("decltype(auto)"))
12152 return true;
12154 return false;
12157 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12158 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12160 static inline int
12161 is_base_type (tree type)
12163 switch (TREE_CODE (type))
12165 case ERROR_MARK:
12166 case VOID_TYPE:
12167 case INTEGER_TYPE:
12168 case REAL_TYPE:
12169 case FIXED_POINT_TYPE:
12170 case COMPLEX_TYPE:
12171 case BOOLEAN_TYPE:
12172 case POINTER_BOUNDS_TYPE:
12173 return 1;
12175 case ARRAY_TYPE:
12176 case RECORD_TYPE:
12177 case UNION_TYPE:
12178 case QUAL_UNION_TYPE:
12179 case ENUMERAL_TYPE:
12180 case FUNCTION_TYPE:
12181 case METHOD_TYPE:
12182 case POINTER_TYPE:
12183 case REFERENCE_TYPE:
12184 case NULLPTR_TYPE:
12185 case OFFSET_TYPE:
12186 case LANG_TYPE:
12187 case VECTOR_TYPE:
12188 return 0;
12190 default:
12191 if (is_cxx_auto (type))
12192 return 0;
12193 gcc_unreachable ();
12196 return 0;
12199 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12200 node, return the size in bits for the type if it is a constant, or else
12201 return the alignment for the type if the type's size is not constant, or
12202 else return BITS_PER_WORD if the type actually turns out to be an
12203 ERROR_MARK node. */
12205 static inline unsigned HOST_WIDE_INT
12206 simple_type_size_in_bits (const_tree type)
12208 if (TREE_CODE (type) == ERROR_MARK)
12209 return BITS_PER_WORD;
12210 else if (TYPE_SIZE (type) == NULL_TREE)
12211 return 0;
12212 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12213 return tree_to_uhwi (TYPE_SIZE (type));
12214 else
12215 return TYPE_ALIGN (type);
12218 /* Similarly, but return an offset_int instead of UHWI. */
12220 static inline offset_int
12221 offset_int_type_size_in_bits (const_tree type)
12223 if (TREE_CODE (type) == ERROR_MARK)
12224 return BITS_PER_WORD;
12225 else if (TYPE_SIZE (type) == NULL_TREE)
12226 return 0;
12227 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12228 return wi::to_offset (TYPE_SIZE (type));
12229 else
12230 return TYPE_ALIGN (type);
12233 /* Given a pointer to a tree node for a subrange type, return a pointer
12234 to a DIE that describes the given type. */
12236 static dw_die_ref
12237 subrange_type_die (tree type, tree low, tree high, tree bias,
12238 dw_die_ref context_die)
12240 dw_die_ref subrange_die;
12241 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12243 if (context_die == NULL)
12244 context_die = comp_unit_die ();
12246 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12248 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12250 /* The size of the subrange type and its base type do not match,
12251 so we need to generate a size attribute for the subrange type. */
12252 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12255 if (low)
12256 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12257 if (high)
12258 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12259 if (bias && !dwarf_strict)
12260 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12261 dw_scalar_form_constant
12262 | dw_scalar_form_exprloc
12263 | dw_scalar_form_reference,
12264 NULL);
12266 return subrange_die;
12269 /* Returns the (const and/or volatile) cv_qualifiers associated with
12270 the decl node. This will normally be augmented with the
12271 cv_qualifiers of the underlying type in add_type_attribute. */
12273 static int
12274 decl_quals (const_tree decl)
12276 return ((TREE_READONLY (decl)
12277 /* The C++ front-end correctly marks reference-typed
12278 variables as readonly, but from a language (and debug
12279 info) standpoint they are not const-qualified. */
12280 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12281 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12282 | (TREE_THIS_VOLATILE (decl)
12283 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12286 /* Determine the TYPE whose qualifiers match the largest strict subset
12287 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12288 qualifiers outside QUAL_MASK. */
12290 static int
12291 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12293 tree t;
12294 int best_rank = 0, best_qual = 0, max_rank;
12296 type_quals &= qual_mask;
12297 max_rank = popcount_hwi (type_quals) - 1;
12299 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12300 t = TYPE_NEXT_VARIANT (t))
12302 int q = TYPE_QUALS (t) & qual_mask;
12304 if ((q & type_quals) == q && q != type_quals
12305 && check_base_type (t, type))
12307 int rank = popcount_hwi (q);
12309 if (rank > best_rank)
12311 best_rank = rank;
12312 best_qual = q;
12317 return best_qual;
12320 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12321 static const dwarf_qual_info_t dwarf_qual_info[] =
12323 { TYPE_QUAL_CONST, DW_TAG_const_type },
12324 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12325 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12326 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12328 static const unsigned int dwarf_qual_info_size
12329 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12331 /* If DIE is a qualified DIE of some base DIE with the same parent,
12332 return the base DIE, otherwise return NULL. Set MASK to the
12333 qualifiers added compared to the returned DIE. */
12335 static dw_die_ref
12336 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12338 unsigned int i;
12339 for (i = 0; i < dwarf_qual_info_size; i++)
12340 if (die->die_tag == dwarf_qual_info[i].t)
12341 break;
12342 if (i == dwarf_qual_info_size)
12343 return NULL;
12344 if (vec_safe_length (die->die_attr) != 1)
12345 return NULL;
12346 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12347 if (type == NULL || type->die_parent != die->die_parent)
12348 return NULL;
12349 *mask |= dwarf_qual_info[i].q;
12350 if (depth)
12352 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12353 if (ret)
12354 return ret;
12356 return type;
12359 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12360 entry that chains the modifiers specified by CV_QUALS in front of the
12361 given type. REVERSE is true if the type is to be interpreted in the
12362 reverse storage order wrt the target order. */
12364 static dw_die_ref
12365 modified_type_die (tree type, int cv_quals, bool reverse,
12366 dw_die_ref context_die)
12368 enum tree_code code = TREE_CODE (type);
12369 dw_die_ref mod_type_die;
12370 dw_die_ref sub_die = NULL;
12371 tree item_type = NULL;
12372 tree qualified_type;
12373 tree name, low, high;
12374 dw_die_ref mod_scope;
12375 /* Only these cv-qualifiers are currently handled. */
12376 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12377 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12379 if (code == ERROR_MARK)
12380 return NULL;
12382 if (lang_hooks.types.get_debug_type)
12384 tree debug_type = lang_hooks.types.get_debug_type (type);
12386 if (debug_type != NULL_TREE && debug_type != type)
12387 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12390 cv_quals &= cv_qual_mask;
12392 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12393 tag modifier (and not an attribute) old consumers won't be able
12394 to handle it. */
12395 if (dwarf_version < 3)
12396 cv_quals &= ~TYPE_QUAL_RESTRICT;
12398 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12399 if (dwarf_version < 5)
12400 cv_quals &= ~TYPE_QUAL_ATOMIC;
12402 /* See if we already have the appropriately qualified variant of
12403 this type. */
12404 qualified_type = get_qualified_type (type, cv_quals);
12406 if (qualified_type == sizetype
12407 && TYPE_NAME (qualified_type)
12408 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12410 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12412 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12413 && TYPE_PRECISION (t)
12414 == TYPE_PRECISION (qualified_type)
12415 && TYPE_UNSIGNED (t)
12416 == TYPE_UNSIGNED (qualified_type));
12417 qualified_type = t;
12420 /* If we do, then we can just use its DIE, if it exists. */
12421 if (qualified_type)
12423 mod_type_die = lookup_type_die (qualified_type);
12425 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12426 if (mod_type_die
12427 && (!need_endianity_attribute_p (reverse)
12428 || !is_base_type (type)
12429 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12430 return mod_type_die;
12433 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12435 /* Handle C typedef types. */
12436 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12437 && !DECL_ARTIFICIAL (name))
12439 tree dtype = TREE_TYPE (name);
12441 if (qualified_type == dtype)
12443 /* For a named type, use the typedef. */
12444 gen_type_die (qualified_type, context_die);
12445 return lookup_type_die (qualified_type);
12447 else
12449 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12450 dquals &= cv_qual_mask;
12451 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12452 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12453 /* cv-unqualified version of named type. Just use
12454 the unnamed type to which it refers. */
12455 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12456 reverse, context_die);
12457 /* Else cv-qualified version of named type; fall through. */
12461 mod_scope = scope_die_for (type, context_die);
12463 if (cv_quals)
12465 int sub_quals = 0, first_quals = 0;
12466 unsigned i;
12467 dw_die_ref first = NULL, last = NULL;
12469 /* Determine a lesser qualified type that most closely matches
12470 this one. Then generate DW_TAG_* entries for the remaining
12471 qualifiers. */
12472 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12473 cv_qual_mask);
12474 if (sub_quals && use_debug_types)
12476 bool needed = false;
12477 /* If emitting type units, make sure the order of qualifiers
12478 is canonical. Thus, start from unqualified type if
12479 an earlier qualifier is missing in sub_quals, but some later
12480 one is present there. */
12481 for (i = 0; i < dwarf_qual_info_size; i++)
12482 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12483 needed = true;
12484 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12486 sub_quals = 0;
12487 break;
12490 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12491 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12493 /* As not all intermediate qualified DIEs have corresponding
12494 tree types, ensure that qualified DIEs in the same scope
12495 as their DW_AT_type are emitted after their DW_AT_type,
12496 only with other qualified DIEs for the same type possibly
12497 in between them. Determine the range of such qualified
12498 DIEs now (first being the base type, last being corresponding
12499 last qualified DIE for it). */
12500 unsigned int count = 0;
12501 first = qualified_die_p (mod_type_die, &first_quals,
12502 dwarf_qual_info_size);
12503 if (first == NULL)
12504 first = mod_type_die;
12505 gcc_assert ((first_quals & ~sub_quals) == 0);
12506 for (count = 0, last = first;
12507 count < (1U << dwarf_qual_info_size);
12508 count++, last = last->die_sib)
12510 int quals = 0;
12511 if (last == mod_scope->die_child)
12512 break;
12513 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12514 != first)
12515 break;
12519 for (i = 0; i < dwarf_qual_info_size; i++)
12520 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12522 dw_die_ref d;
12523 if (first && first != last)
12525 for (d = first->die_sib; ; d = d->die_sib)
12527 int quals = 0;
12528 qualified_die_p (d, &quals, dwarf_qual_info_size);
12529 if (quals == (first_quals | dwarf_qual_info[i].q))
12530 break;
12531 if (d == last)
12533 d = NULL;
12534 break;
12537 if (d)
12539 mod_type_die = d;
12540 continue;
12543 if (first)
12545 d = ggc_cleared_alloc<die_node> ();
12546 d->die_tag = dwarf_qual_info[i].t;
12547 add_child_die_after (mod_scope, d, last);
12548 last = d;
12550 else
12551 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12552 if (mod_type_die)
12553 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12554 mod_type_die = d;
12555 first_quals |= dwarf_qual_info[i].q;
12558 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12560 dwarf_tag tag = DW_TAG_pointer_type;
12561 if (code == REFERENCE_TYPE)
12563 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12564 tag = DW_TAG_rvalue_reference_type;
12565 else
12566 tag = DW_TAG_reference_type;
12568 mod_type_die = new_die (tag, mod_scope, type);
12570 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12571 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12572 item_type = TREE_TYPE (type);
12574 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12575 if (!ADDR_SPACE_GENERIC_P (as))
12577 int action = targetm.addr_space.debug (as);
12578 if (action >= 0)
12580 /* Positive values indicate an address_class. */
12581 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12583 else
12585 /* Negative values indicate an (inverted) segment base reg. */
12586 dw_loc_descr_ref d
12587 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12588 add_AT_loc (mod_type_die, DW_AT_segment, d);
12592 else if (code == INTEGER_TYPE
12593 && TREE_TYPE (type) != NULL_TREE
12594 && subrange_type_for_debug_p (type, &low, &high))
12596 tree bias = NULL_TREE;
12597 if (lang_hooks.types.get_type_bias)
12598 bias = lang_hooks.types.get_type_bias (type);
12599 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12600 item_type = TREE_TYPE (type);
12602 else if (is_base_type (type))
12603 mod_type_die = base_type_die (type, reverse);
12604 else
12606 gen_type_die (type, context_die);
12608 /* We have to get the type_main_variant here (and pass that to the
12609 `lookup_type_die' routine) because the ..._TYPE node we have
12610 might simply be a *copy* of some original type node (where the
12611 copy was created to help us keep track of typedef names) and
12612 that copy might have a different TYPE_UID from the original
12613 ..._TYPE node. */
12614 if (TREE_CODE (type) == FUNCTION_TYPE
12615 || TREE_CODE (type) == METHOD_TYPE)
12617 /* For function/method types, can't just use type_main_variant here,
12618 because that can have different ref-qualifiers for C++,
12619 but try to canonicalize. */
12620 tree main = TYPE_MAIN_VARIANT (type);
12621 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12622 if (check_base_type (t, main) && check_lang_type (t, type))
12623 return lookup_type_die (t);
12624 return lookup_type_die (type);
12626 else if (TREE_CODE (type) != VECTOR_TYPE
12627 && TREE_CODE (type) != ARRAY_TYPE)
12628 return lookup_type_die (type_main_variant (type));
12629 else
12630 /* Vectors have the debugging information in the type,
12631 not the main variant. */
12632 return lookup_type_die (type);
12635 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12636 don't output a DW_TAG_typedef, since there isn't one in the
12637 user's program; just attach a DW_AT_name to the type.
12638 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12639 if the base type already has the same name. */
12640 if (name
12641 && ((TREE_CODE (name) != TYPE_DECL
12642 && (qualified_type == TYPE_MAIN_VARIANT (type)
12643 || (cv_quals == TYPE_UNQUALIFIED)))
12644 || (TREE_CODE (name) == TYPE_DECL
12645 && TREE_TYPE (name) == qualified_type
12646 && DECL_NAME (name))))
12648 if (TREE_CODE (name) == TYPE_DECL)
12649 /* Could just call add_name_and_src_coords_attributes here,
12650 but since this is a builtin type it doesn't have any
12651 useful source coordinates anyway. */
12652 name = DECL_NAME (name);
12653 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12655 /* This probably indicates a bug. */
12656 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12658 name = TYPE_IDENTIFIER (type);
12659 add_name_attribute (mod_type_die,
12660 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12663 if (qualified_type)
12664 equate_type_number_to_die (qualified_type, mod_type_die);
12666 if (item_type)
12667 /* We must do this after the equate_type_number_to_die call, in case
12668 this is a recursive type. This ensures that the modified_type_die
12669 recursion will terminate even if the type is recursive. Recursive
12670 types are possible in Ada. */
12671 sub_die = modified_type_die (item_type,
12672 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12673 reverse,
12674 context_die);
12676 if (sub_die != NULL)
12677 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12679 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12680 if (TYPE_ARTIFICIAL (type))
12681 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12683 return mod_type_die;
12686 /* Generate DIEs for the generic parameters of T.
12687 T must be either a generic type or a generic function.
12688 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12690 static void
12691 gen_generic_params_dies (tree t)
12693 tree parms, args;
12694 int parms_num, i;
12695 dw_die_ref die = NULL;
12696 int non_default;
12698 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12699 return;
12701 if (TYPE_P (t))
12702 die = lookup_type_die (t);
12703 else if (DECL_P (t))
12704 die = lookup_decl_die (t);
12706 gcc_assert (die);
12708 parms = lang_hooks.get_innermost_generic_parms (t);
12709 if (!parms)
12710 /* T has no generic parameter. It means T is neither a generic type
12711 or function. End of story. */
12712 return;
12714 parms_num = TREE_VEC_LENGTH (parms);
12715 args = lang_hooks.get_innermost_generic_args (t);
12716 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12717 non_default = int_cst_value (TREE_CHAIN (args));
12718 else
12719 non_default = TREE_VEC_LENGTH (args);
12720 for (i = 0; i < parms_num; i++)
12722 tree parm, arg, arg_pack_elems;
12723 dw_die_ref parm_die;
12725 parm = TREE_VEC_ELT (parms, i);
12726 arg = TREE_VEC_ELT (args, i);
12727 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12728 gcc_assert (parm && TREE_VALUE (parm) && arg);
12730 if (parm && TREE_VALUE (parm) && arg)
12732 /* If PARM represents a template parameter pack,
12733 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12734 by DW_TAG_template_*_parameter DIEs for the argument
12735 pack elements of ARG. Note that ARG would then be
12736 an argument pack. */
12737 if (arg_pack_elems)
12738 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12739 arg_pack_elems,
12740 die);
12741 else
12742 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12743 true /* emit name */, die);
12744 if (i >= non_default)
12745 add_AT_flag (parm_die, DW_AT_default_value, 1);
12750 /* Create and return a DIE for PARM which should be
12751 the representation of a generic type parameter.
12752 For instance, in the C++ front end, PARM would be a template parameter.
12753 ARG is the argument to PARM.
12754 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12755 name of the PARM.
12756 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12757 as a child node. */
12759 static dw_die_ref
12760 generic_parameter_die (tree parm, tree arg,
12761 bool emit_name_p,
12762 dw_die_ref parent_die)
12764 dw_die_ref tmpl_die = NULL;
12765 const char *name = NULL;
12767 if (!parm || !DECL_NAME (parm) || !arg)
12768 return NULL;
12770 /* We support non-type generic parameters and arguments,
12771 type generic parameters and arguments, as well as
12772 generic generic parameters (a.k.a. template template parameters in C++)
12773 and arguments. */
12774 if (TREE_CODE (parm) == PARM_DECL)
12775 /* PARM is a nontype generic parameter */
12776 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12777 else if (TREE_CODE (parm) == TYPE_DECL)
12778 /* PARM is a type generic parameter. */
12779 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12780 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12781 /* PARM is a generic generic parameter.
12782 Its DIE is a GNU extension. It shall have a
12783 DW_AT_name attribute to represent the name of the template template
12784 parameter, and a DW_AT_GNU_template_name attribute to represent the
12785 name of the template template argument. */
12786 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12787 parent_die, parm);
12788 else
12789 gcc_unreachable ();
12791 if (tmpl_die)
12793 tree tmpl_type;
12795 /* If PARM is a generic parameter pack, it means we are
12796 emitting debug info for a template argument pack element.
12797 In other terms, ARG is a template argument pack element.
12798 In that case, we don't emit any DW_AT_name attribute for
12799 the die. */
12800 if (emit_name_p)
12802 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12803 gcc_assert (name);
12804 add_AT_string (tmpl_die, DW_AT_name, name);
12807 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12809 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12810 TMPL_DIE should have a child DW_AT_type attribute that is set
12811 to the type of the argument to PARM, which is ARG.
12812 If PARM is a type generic parameter, TMPL_DIE should have a
12813 child DW_AT_type that is set to ARG. */
12814 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12815 add_type_attribute (tmpl_die, tmpl_type,
12816 (TREE_THIS_VOLATILE (tmpl_type)
12817 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12818 false, parent_die);
12820 else
12822 /* So TMPL_DIE is a DIE representing a
12823 a generic generic template parameter, a.k.a template template
12824 parameter in C++ and arg is a template. */
12826 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12827 to the name of the argument. */
12828 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12829 if (name)
12830 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12833 if (TREE_CODE (parm) == PARM_DECL)
12834 /* So PARM is a non-type generic parameter.
12835 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12836 attribute of TMPL_DIE which value represents the value
12837 of ARG.
12838 We must be careful here:
12839 The value of ARG might reference some function decls.
12840 We might currently be emitting debug info for a generic
12841 type and types are emitted before function decls, we don't
12842 know if the function decls referenced by ARG will actually be
12843 emitted after cgraph computations.
12844 So must defer the generation of the DW_AT_const_value to
12845 after cgraph is ready. */
12846 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12849 return tmpl_die;
12852 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12853 PARM_PACK must be a template parameter pack. The returned DIE
12854 will be child DIE of PARENT_DIE. */
12856 static dw_die_ref
12857 template_parameter_pack_die (tree parm_pack,
12858 tree parm_pack_args,
12859 dw_die_ref parent_die)
12861 dw_die_ref die;
12862 int j;
12864 gcc_assert (parent_die && parm_pack);
12866 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12867 add_name_and_src_coords_attributes (die, parm_pack);
12868 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12869 generic_parameter_die (parm_pack,
12870 TREE_VEC_ELT (parm_pack_args, j),
12871 false /* Don't emit DW_AT_name */,
12872 die);
12873 return die;
12876 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12877 an enumerated type. */
12879 static inline int
12880 type_is_enum (const_tree type)
12882 return TREE_CODE (type) == ENUMERAL_TYPE;
12885 /* Return the DBX register number described by a given RTL node. */
12887 static unsigned int
12888 dbx_reg_number (const_rtx rtl)
12890 unsigned regno = REGNO (rtl);
12892 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12894 #ifdef LEAF_REG_REMAP
12895 if (crtl->uses_only_leaf_regs)
12897 int leaf_reg = LEAF_REG_REMAP (regno);
12898 if (leaf_reg != -1)
12899 regno = (unsigned) leaf_reg;
12901 #endif
12903 regno = DBX_REGISTER_NUMBER (regno);
12904 gcc_assert (regno != INVALID_REGNUM);
12905 return regno;
12908 /* Optionally add a DW_OP_piece term to a location description expression.
12909 DW_OP_piece is only added if the location description expression already
12910 doesn't end with DW_OP_piece. */
12912 static void
12913 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12915 dw_loc_descr_ref loc;
12917 if (*list_head != NULL)
12919 /* Find the end of the chain. */
12920 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12923 if (loc->dw_loc_opc != DW_OP_piece)
12924 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12928 /* Return a location descriptor that designates a machine register or
12929 zero if there is none. */
12931 static dw_loc_descr_ref
12932 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12934 rtx regs;
12936 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12937 return 0;
12939 /* We only use "frame base" when we're sure we're talking about the
12940 post-prologue local stack frame. We do this by *not* running
12941 register elimination until this point, and recognizing the special
12942 argument pointer and soft frame pointer rtx's.
12943 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
12944 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
12945 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
12947 dw_loc_descr_ref result = NULL;
12949 if (dwarf_version >= 4 || !dwarf_strict)
12951 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
12952 initialized);
12953 if (result)
12954 add_loc_descr (&result,
12955 new_loc_descr (DW_OP_stack_value, 0, 0));
12957 return result;
12960 regs = targetm.dwarf_register_span (rtl);
12962 if (REG_NREGS (rtl) > 1 || regs)
12963 return multiple_reg_loc_descriptor (rtl, regs, initialized);
12964 else
12966 unsigned int dbx_regnum = dbx_reg_number (rtl);
12967 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12968 return 0;
12969 return one_reg_loc_descriptor (dbx_regnum, initialized);
12973 /* Return a location descriptor that designates a machine register for
12974 a given hard register number. */
12976 static dw_loc_descr_ref
12977 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12979 dw_loc_descr_ref reg_loc_descr;
12981 if (regno <= 31)
12982 reg_loc_descr
12983 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12984 else
12985 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12987 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12988 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12990 return reg_loc_descr;
12993 /* Given an RTL of a register, return a location descriptor that
12994 designates a value that spans more than one register. */
12996 static dw_loc_descr_ref
12997 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
12998 enum var_init_status initialized)
13000 int size, i;
13001 dw_loc_descr_ref loc_result = NULL;
13003 /* Simple, contiguous registers. */
13004 if (regs == NULL_RTX)
13006 unsigned reg = REGNO (rtl);
13007 int nregs;
13009 #ifdef LEAF_REG_REMAP
13010 if (crtl->uses_only_leaf_regs)
13012 int leaf_reg = LEAF_REG_REMAP (reg);
13013 if (leaf_reg != -1)
13014 reg = (unsigned) leaf_reg;
13016 #endif
13018 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13019 nregs = REG_NREGS (rtl);
13021 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13023 loc_result = NULL;
13024 while (nregs--)
13026 dw_loc_descr_ref t;
13028 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13029 VAR_INIT_STATUS_INITIALIZED);
13030 add_loc_descr (&loc_result, t);
13031 add_loc_descr_op_piece (&loc_result, size);
13032 ++reg;
13034 return loc_result;
13037 /* Now onto stupid register sets in non contiguous locations. */
13039 gcc_assert (GET_CODE (regs) == PARALLEL);
13041 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13042 loc_result = NULL;
13044 for (i = 0; i < XVECLEN (regs, 0); ++i)
13046 dw_loc_descr_ref t;
13048 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13049 VAR_INIT_STATUS_INITIALIZED);
13050 add_loc_descr (&loc_result, t);
13051 add_loc_descr_op_piece (&loc_result, size);
13054 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13055 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13056 return loc_result;
13059 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13061 /* Return a location descriptor that designates a constant i,
13062 as a compound operation from constant (i >> shift), constant shift
13063 and DW_OP_shl. */
13065 static dw_loc_descr_ref
13066 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13068 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13069 add_loc_descr (&ret, int_loc_descriptor (shift));
13070 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13071 return ret;
13074 /* Return a location descriptor that designates a constant. */
13076 static dw_loc_descr_ref
13077 int_loc_descriptor (HOST_WIDE_INT i)
13079 enum dwarf_location_atom op;
13081 /* Pick the smallest representation of a constant, rather than just
13082 defaulting to the LEB encoding. */
13083 if (i >= 0)
13085 int clz = clz_hwi (i);
13086 int ctz = ctz_hwi (i);
13087 if (i <= 31)
13088 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13089 else if (i <= 0xff)
13090 op = DW_OP_const1u;
13091 else if (i <= 0xffff)
13092 op = DW_OP_const2u;
13093 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13094 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13095 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13096 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13097 while DW_OP_const4u is 5 bytes. */
13098 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13099 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13100 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13101 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13102 while DW_OP_const4u is 5 bytes. */
13103 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13105 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13106 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13107 <= 4)
13109 /* As i >= 2**31, the double cast above will yield a negative number.
13110 Since wrapping is defined in DWARF expressions we can output big
13111 positive integers as small negative ones, regardless of the size
13112 of host wide ints.
13114 Here, since the evaluator will handle 32-bit values and since i >=
13115 2**31, we know it's going to be interpreted as a negative literal:
13116 store it this way if we can do better than 5 bytes this way. */
13117 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13119 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13120 op = DW_OP_const4u;
13122 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13123 least 6 bytes: see if we can do better before falling back to it. */
13124 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13125 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13126 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13127 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13128 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13129 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13130 >= HOST_BITS_PER_WIDE_INT)
13131 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13132 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13133 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13134 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13135 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13136 && size_of_uleb128 (i) > 6)
13137 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13138 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13139 else
13140 op = DW_OP_constu;
13142 else
13144 if (i >= -0x80)
13145 op = DW_OP_const1s;
13146 else if (i >= -0x8000)
13147 op = DW_OP_const2s;
13148 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13150 if (size_of_int_loc_descriptor (i) < 5)
13152 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13153 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13154 return ret;
13156 op = DW_OP_const4s;
13158 else
13160 if (size_of_int_loc_descriptor (i)
13161 < (unsigned long) 1 + size_of_sleb128 (i))
13163 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13164 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13165 return ret;
13167 op = DW_OP_consts;
13171 return new_loc_descr (op, i, 0);
13174 /* Likewise, for unsigned constants. */
13176 static dw_loc_descr_ref
13177 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13179 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13180 const unsigned HOST_WIDE_INT max_uint
13181 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13183 /* If possible, use the clever signed constants handling. */
13184 if (i <= max_int)
13185 return int_loc_descriptor ((HOST_WIDE_INT) i);
13187 /* Here, we are left with positive numbers that cannot be represented as
13188 HOST_WIDE_INT, i.e.:
13189 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13191 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13192 whereas may be better to output a negative integer: thanks to integer
13193 wrapping, we know that:
13194 x = x - 2 ** DWARF2_ADDR_SIZE
13195 = x - 2 * (max (HOST_WIDE_INT) + 1)
13196 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13197 small negative integers. Let's try that in cases it will clearly improve
13198 the encoding: there is no gain turning DW_OP_const4u into
13199 DW_OP_const4s. */
13200 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13201 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13202 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13204 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13206 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13207 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13208 const HOST_WIDE_INT second_shift
13209 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13211 /* So we finally have:
13212 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13213 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13214 return int_loc_descriptor (second_shift);
13217 /* Last chance: fallback to a simple constant operation. */
13218 return new_loc_descr
13219 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13220 ? DW_OP_const4u
13221 : DW_OP_const8u,
13222 i, 0);
13225 /* Generate and return a location description that computes the unsigned
13226 comparison of the two stack top entries (a OP b where b is the top-most
13227 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13228 LE_EXPR, GT_EXPR or GE_EXPR. */
13230 static dw_loc_descr_ref
13231 uint_comparison_loc_list (enum tree_code kind)
13233 enum dwarf_location_atom op, flip_op;
13234 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13236 switch (kind)
13238 case LT_EXPR:
13239 op = DW_OP_lt;
13240 break;
13241 case LE_EXPR:
13242 op = DW_OP_le;
13243 break;
13244 case GT_EXPR:
13245 op = DW_OP_gt;
13246 break;
13247 case GE_EXPR:
13248 op = DW_OP_ge;
13249 break;
13250 default:
13251 gcc_unreachable ();
13254 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13255 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13257 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13258 possible to perform unsigned comparisons: we just have to distinguish
13259 three cases:
13261 1. when a and b have the same sign (as signed integers); then we should
13262 return: a OP(signed) b;
13264 2. when a is a negative signed integer while b is a positive one, then a
13265 is a greater unsigned integer than b; likewise when a and b's roles
13266 are flipped.
13268 So first, compare the sign of the two operands. */
13269 ret = new_loc_descr (DW_OP_over, 0, 0);
13270 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13271 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13272 /* If they have different signs (i.e. they have different sign bits), then
13273 the stack top value has now the sign bit set and thus it's smaller than
13274 zero. */
13275 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13276 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13277 add_loc_descr (&ret, bra_node);
13279 /* We are in case 1. At this point, we know both operands have the same
13280 sign, to it's safe to use the built-in signed comparison. */
13281 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13282 add_loc_descr (&ret, jmp_node);
13284 /* We are in case 2. Here, we know both operands do not have the same sign,
13285 so we have to flip the signed comparison. */
13286 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13287 tmp = new_loc_descr (flip_op, 0, 0);
13288 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13289 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13290 add_loc_descr (&ret, tmp);
13292 /* This dummy operation is necessary to make the two branches join. */
13293 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13294 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13295 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13296 add_loc_descr (&ret, tmp);
13298 return ret;
13301 /* Likewise, but takes the location description lists (might be destructive on
13302 them). Return NULL if either is NULL or if concatenation fails. */
13304 static dw_loc_list_ref
13305 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13306 enum tree_code kind)
13308 if (left == NULL || right == NULL)
13309 return NULL;
13311 add_loc_list (&left, right);
13312 if (left == NULL)
13313 return NULL;
13315 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13316 return left;
13319 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13320 without actually allocating it. */
13322 static unsigned long
13323 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13325 return size_of_int_loc_descriptor (i >> shift)
13326 + size_of_int_loc_descriptor (shift)
13327 + 1;
13330 /* Return size_of_locs (int_loc_descriptor (i)) without
13331 actually allocating it. */
13333 static unsigned long
13334 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13336 unsigned long s;
13338 if (i >= 0)
13340 int clz, ctz;
13341 if (i <= 31)
13342 return 1;
13343 else if (i <= 0xff)
13344 return 2;
13345 else if (i <= 0xffff)
13346 return 3;
13347 clz = clz_hwi (i);
13348 ctz = ctz_hwi (i);
13349 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13350 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13351 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13352 - clz - 5);
13353 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13354 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13355 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13356 - clz - 8);
13357 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13358 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13359 <= 4)
13360 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13361 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13362 return 5;
13363 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13364 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13365 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13366 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13367 - clz - 8);
13368 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13369 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13370 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13371 - clz - 16);
13372 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13373 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13374 && s > 6)
13375 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13376 - clz - 32);
13377 else
13378 return 1 + s;
13380 else
13382 if (i >= -0x80)
13383 return 2;
13384 else if (i >= -0x8000)
13385 return 3;
13386 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13388 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13390 s = size_of_int_loc_descriptor (-i) + 1;
13391 if (s < 5)
13392 return s;
13394 return 5;
13396 else
13398 unsigned long r = 1 + size_of_sleb128 (i);
13399 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13401 s = size_of_int_loc_descriptor (-i) + 1;
13402 if (s < r)
13403 return s;
13405 return r;
13410 /* Return loc description representing "address" of integer value.
13411 This can appear only as toplevel expression. */
13413 static dw_loc_descr_ref
13414 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13416 int litsize;
13417 dw_loc_descr_ref loc_result = NULL;
13419 if (!(dwarf_version >= 4 || !dwarf_strict))
13420 return NULL;
13422 litsize = size_of_int_loc_descriptor (i);
13423 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13424 is more compact. For DW_OP_stack_value we need:
13425 litsize + 1 (DW_OP_stack_value)
13426 and for DW_OP_implicit_value:
13427 1 (DW_OP_implicit_value) + 1 (length) + size. */
13428 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13430 loc_result = int_loc_descriptor (i);
13431 add_loc_descr (&loc_result,
13432 new_loc_descr (DW_OP_stack_value, 0, 0));
13433 return loc_result;
13436 loc_result = new_loc_descr (DW_OP_implicit_value,
13437 size, 0);
13438 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13439 loc_result->dw_loc_oprnd2.v.val_int = i;
13440 return loc_result;
13443 /* Return a location descriptor that designates a base+offset location. */
13445 static dw_loc_descr_ref
13446 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13447 enum var_init_status initialized)
13449 unsigned int regno;
13450 dw_loc_descr_ref result;
13451 dw_fde_ref fde = cfun->fde;
13453 /* We only use "frame base" when we're sure we're talking about the
13454 post-prologue local stack frame. We do this by *not* running
13455 register elimination until this point, and recognizing the special
13456 argument pointer and soft frame pointer rtx's. */
13457 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13459 rtx elim = (ira_use_lra_p
13460 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13461 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13463 if (elim != reg)
13465 if (GET_CODE (elim) == PLUS)
13467 offset += INTVAL (XEXP (elim, 1));
13468 elim = XEXP (elim, 0);
13470 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13471 && (elim == hard_frame_pointer_rtx
13472 || elim == stack_pointer_rtx))
13473 || elim == (frame_pointer_needed
13474 ? hard_frame_pointer_rtx
13475 : stack_pointer_rtx));
13477 /* If drap register is used to align stack, use frame
13478 pointer + offset to access stack variables. If stack
13479 is aligned without drap, use stack pointer + offset to
13480 access stack variables. */
13481 if (crtl->stack_realign_tried
13482 && reg == frame_pointer_rtx)
13484 int base_reg
13485 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13486 ? HARD_FRAME_POINTER_REGNUM
13487 : REGNO (elim));
13488 return new_reg_loc_descr (base_reg, offset);
13491 gcc_assert (frame_pointer_fb_offset_valid);
13492 offset += frame_pointer_fb_offset;
13493 return new_loc_descr (DW_OP_fbreg, offset, 0);
13497 regno = REGNO (reg);
13498 #ifdef LEAF_REG_REMAP
13499 if (crtl->uses_only_leaf_regs)
13501 int leaf_reg = LEAF_REG_REMAP (regno);
13502 if (leaf_reg != -1)
13503 regno = (unsigned) leaf_reg;
13505 #endif
13506 regno = DWARF_FRAME_REGNUM (regno);
13508 if (!optimize && fde
13509 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13511 /* Use cfa+offset to represent the location of arguments passed
13512 on the stack when drap is used to align stack.
13513 Only do this when not optimizing, for optimized code var-tracking
13514 is supposed to track where the arguments live and the register
13515 used as vdrap or drap in some spot might be used for something
13516 else in other part of the routine. */
13517 return new_loc_descr (DW_OP_fbreg, offset, 0);
13520 if (regno <= 31)
13521 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13522 offset, 0);
13523 else
13524 result = new_loc_descr (DW_OP_bregx, regno, offset);
13526 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13527 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13529 return result;
13532 /* Return true if this RTL expression describes a base+offset calculation. */
13534 static inline int
13535 is_based_loc (const_rtx rtl)
13537 return (GET_CODE (rtl) == PLUS
13538 && ((REG_P (XEXP (rtl, 0))
13539 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13540 && CONST_INT_P (XEXP (rtl, 1)))));
13543 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13544 failed. */
13546 static dw_loc_descr_ref
13547 tls_mem_loc_descriptor (rtx mem)
13549 tree base;
13550 dw_loc_descr_ref loc_result;
13552 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13553 return NULL;
13555 base = get_base_address (MEM_EXPR (mem));
13556 if (base == NULL
13557 || !VAR_P (base)
13558 || !DECL_THREAD_LOCAL_P (base))
13559 return NULL;
13561 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13562 if (loc_result == NULL)
13563 return NULL;
13565 if (MEM_OFFSET (mem))
13566 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13568 return loc_result;
13571 /* Output debug info about reason why we failed to expand expression as dwarf
13572 expression. */
13574 static void
13575 expansion_failed (tree expr, rtx rtl, char const *reason)
13577 if (dump_file && (dump_flags & TDF_DETAILS))
13579 fprintf (dump_file, "Failed to expand as dwarf: ");
13580 if (expr)
13581 print_generic_expr (dump_file, expr, dump_flags);
13582 if (rtl)
13584 fprintf (dump_file, "\n");
13585 print_rtl (dump_file, rtl);
13587 fprintf (dump_file, "\nReason: %s\n", reason);
13591 /* Helper function for const_ok_for_output. */
13593 static bool
13594 const_ok_for_output_1 (rtx rtl)
13596 if (GET_CODE (rtl) == UNSPEC)
13598 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13599 we can't express it in the debug info. */
13600 /* Don't complain about TLS UNSPECs, those are just too hard to
13601 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13602 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13603 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13604 if (flag_checking
13605 && (XVECLEN (rtl, 0) == 0
13606 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13607 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13608 inform (current_function_decl
13609 ? DECL_SOURCE_LOCATION (current_function_decl)
13610 : UNKNOWN_LOCATION,
13611 #if NUM_UNSPEC_VALUES > 0
13612 "non-delegitimized UNSPEC %s (%d) found in variable location",
13613 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13614 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13615 XINT (rtl, 1));
13616 #else
13617 "non-delegitimized UNSPEC %d found in variable location",
13618 XINT (rtl, 1));
13619 #endif
13620 expansion_failed (NULL_TREE, rtl,
13621 "UNSPEC hasn't been delegitimized.\n");
13622 return false;
13625 if (targetm.const_not_ok_for_debug_p (rtl))
13627 expansion_failed (NULL_TREE, rtl,
13628 "Expression rejected for debug by the backend.\n");
13629 return false;
13632 /* FIXME: Refer to PR60655. It is possible for simplification
13633 of rtl expressions in var tracking to produce such expressions.
13634 We should really identify / validate expressions
13635 enclosed in CONST that can be handled by assemblers on various
13636 targets and only handle legitimate cases here. */
13637 if (GET_CODE (rtl) != SYMBOL_REF)
13639 if (GET_CODE (rtl) == NOT)
13640 return false;
13641 return true;
13644 if (CONSTANT_POOL_ADDRESS_P (rtl))
13646 bool marked;
13647 get_pool_constant_mark (rtl, &marked);
13648 /* If all references to this pool constant were optimized away,
13649 it was not output and thus we can't represent it. */
13650 if (!marked)
13652 expansion_failed (NULL_TREE, rtl,
13653 "Constant was removed from constant pool.\n");
13654 return false;
13658 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13659 return false;
13661 /* Avoid references to external symbols in debug info, on several targets
13662 the linker might even refuse to link when linking a shared library,
13663 and in many other cases the relocations for .debug_info/.debug_loc are
13664 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13665 to be defined within the same shared library or executable are fine. */
13666 if (SYMBOL_REF_EXTERNAL_P (rtl))
13668 tree decl = SYMBOL_REF_DECL (rtl);
13670 if (decl == NULL || !targetm.binds_local_p (decl))
13672 expansion_failed (NULL_TREE, rtl,
13673 "Symbol not defined in current TU.\n");
13674 return false;
13678 return true;
13681 /* Return true if constant RTL can be emitted in DW_OP_addr or
13682 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13683 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13685 static bool
13686 const_ok_for_output (rtx rtl)
13688 if (GET_CODE (rtl) == SYMBOL_REF)
13689 return const_ok_for_output_1 (rtl);
13691 if (GET_CODE (rtl) == CONST)
13693 subrtx_var_iterator::array_type array;
13694 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13695 if (!const_ok_for_output_1 (*iter))
13696 return false;
13697 return true;
13700 return true;
13703 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13704 if possible, NULL otherwise. */
13706 static dw_die_ref
13707 base_type_for_mode (machine_mode mode, bool unsignedp)
13709 dw_die_ref type_die;
13710 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13712 if (type == NULL)
13713 return NULL;
13714 switch (TREE_CODE (type))
13716 case INTEGER_TYPE:
13717 case REAL_TYPE:
13718 break;
13719 default:
13720 return NULL;
13722 type_die = lookup_type_die (type);
13723 if (!type_die)
13724 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13725 comp_unit_die ());
13726 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13727 return NULL;
13728 return type_die;
13731 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13732 type matching MODE, or, if MODE is narrower than or as wide as
13733 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13734 possible. */
13736 static dw_loc_descr_ref
13737 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13739 machine_mode outer_mode = mode;
13740 dw_die_ref type_die;
13741 dw_loc_descr_ref cvt;
13743 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13745 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13746 return op;
13748 type_die = base_type_for_mode (outer_mode, 1);
13749 if (type_die == NULL)
13750 return NULL;
13751 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13752 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13753 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13754 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13755 add_loc_descr (&op, cvt);
13756 return op;
13759 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13761 static dw_loc_descr_ref
13762 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13763 dw_loc_descr_ref op1)
13765 dw_loc_descr_ref ret = op0;
13766 add_loc_descr (&ret, op1);
13767 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13768 if (STORE_FLAG_VALUE != 1)
13770 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13771 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13773 return ret;
13776 /* Return location descriptor for signed comparison OP RTL. */
13778 static dw_loc_descr_ref
13779 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13780 machine_mode mem_mode)
13782 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13783 dw_loc_descr_ref op0, op1;
13784 int shift;
13786 if (op_mode == VOIDmode)
13787 op_mode = GET_MODE (XEXP (rtl, 1));
13788 if (op_mode == VOIDmode)
13789 return NULL;
13791 if (dwarf_strict
13792 && dwarf_version < 5
13793 && (!SCALAR_INT_MODE_P (op_mode)
13794 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13795 return NULL;
13797 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13798 VAR_INIT_STATUS_INITIALIZED);
13799 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13800 VAR_INIT_STATUS_INITIALIZED);
13802 if (op0 == NULL || op1 == NULL)
13803 return NULL;
13805 if (!SCALAR_INT_MODE_P (op_mode)
13806 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13807 return compare_loc_descriptor (op, op0, op1);
13809 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13811 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13812 dw_loc_descr_ref cvt;
13814 if (type_die == NULL)
13815 return NULL;
13816 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13817 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13818 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13819 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13820 add_loc_descr (&op0, cvt);
13821 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13822 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13823 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13824 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13825 add_loc_descr (&op1, cvt);
13826 return compare_loc_descriptor (op, op0, op1);
13829 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13830 /* For eq/ne, if the operands are known to be zero-extended,
13831 there is no need to do the fancy shifting up. */
13832 if (op == DW_OP_eq || op == DW_OP_ne)
13834 dw_loc_descr_ref last0, last1;
13835 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13837 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13839 /* deref_size zero extends, and for constants we can check
13840 whether they are zero extended or not. */
13841 if (((last0->dw_loc_opc == DW_OP_deref_size
13842 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13843 || (CONST_INT_P (XEXP (rtl, 0))
13844 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13845 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13846 && ((last1->dw_loc_opc == DW_OP_deref_size
13847 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13848 || (CONST_INT_P (XEXP (rtl, 1))
13849 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13850 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13851 return compare_loc_descriptor (op, op0, op1);
13853 /* EQ/NE comparison against constant in narrower type than
13854 DWARF2_ADDR_SIZE can be performed either as
13855 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13856 DW_OP_{eq,ne}
13858 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13859 DW_OP_{eq,ne}. Pick whatever is shorter. */
13860 if (CONST_INT_P (XEXP (rtl, 1))
13861 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13862 && (size_of_int_loc_descriptor (shift) + 1
13863 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13864 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13865 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13866 & GET_MODE_MASK (op_mode))))
13868 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13869 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13870 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13871 & GET_MODE_MASK (op_mode));
13872 return compare_loc_descriptor (op, op0, op1);
13875 add_loc_descr (&op0, int_loc_descriptor (shift));
13876 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13877 if (CONST_INT_P (XEXP (rtl, 1)))
13878 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13879 else
13881 add_loc_descr (&op1, int_loc_descriptor (shift));
13882 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13884 return compare_loc_descriptor (op, op0, op1);
13887 /* Return location descriptor for unsigned comparison OP RTL. */
13889 static dw_loc_descr_ref
13890 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13891 machine_mode mem_mode)
13893 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13894 dw_loc_descr_ref op0, op1;
13896 if (op_mode == VOIDmode)
13897 op_mode = GET_MODE (XEXP (rtl, 1));
13898 if (op_mode == VOIDmode)
13899 return NULL;
13900 if (!SCALAR_INT_MODE_P (op_mode))
13901 return NULL;
13903 if (dwarf_strict
13904 && dwarf_version < 5
13905 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13906 return NULL;
13908 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13909 VAR_INIT_STATUS_INITIALIZED);
13910 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13911 VAR_INIT_STATUS_INITIALIZED);
13913 if (op0 == NULL || op1 == NULL)
13914 return NULL;
13916 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13918 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13919 dw_loc_descr_ref last0, last1;
13920 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13922 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13924 if (CONST_INT_P (XEXP (rtl, 0)))
13925 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13926 /* deref_size zero extends, so no need to mask it again. */
13927 else if (last0->dw_loc_opc != DW_OP_deref_size
13928 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13930 add_loc_descr (&op0, int_loc_descriptor (mask));
13931 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13933 if (CONST_INT_P (XEXP (rtl, 1)))
13934 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13935 /* deref_size zero extends, so no need to mask it again. */
13936 else if (last1->dw_loc_opc != DW_OP_deref_size
13937 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13939 add_loc_descr (&op1, int_loc_descriptor (mask));
13940 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13943 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13945 HOST_WIDE_INT bias = 1;
13946 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13947 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13948 if (CONST_INT_P (XEXP (rtl, 1)))
13949 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13950 + INTVAL (XEXP (rtl, 1)));
13951 else
13952 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13953 bias, 0));
13955 return compare_loc_descriptor (op, op0, op1);
13958 /* Return location descriptor for {U,S}{MIN,MAX}. */
13960 static dw_loc_descr_ref
13961 minmax_loc_descriptor (rtx rtl, machine_mode mode,
13962 machine_mode mem_mode)
13964 enum dwarf_location_atom op;
13965 dw_loc_descr_ref op0, op1, ret;
13966 dw_loc_descr_ref bra_node, drop_node;
13968 if (dwarf_strict
13969 && dwarf_version < 5
13970 && (!SCALAR_INT_MODE_P (mode)
13971 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
13972 return NULL;
13974 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13975 VAR_INIT_STATUS_INITIALIZED);
13976 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13977 VAR_INIT_STATUS_INITIALIZED);
13979 if (op0 == NULL || op1 == NULL)
13980 return NULL;
13982 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13983 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13984 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13985 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13987 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13989 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
13990 add_loc_descr (&op0, int_loc_descriptor (mask));
13991 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13992 add_loc_descr (&op1, int_loc_descriptor (mask));
13993 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13995 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13997 HOST_WIDE_INT bias = 1;
13998 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13999 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14000 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14003 else if (!SCALAR_INT_MODE_P (mode)
14004 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14006 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14007 add_loc_descr (&op0, int_loc_descriptor (shift));
14008 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14009 add_loc_descr (&op1, int_loc_descriptor (shift));
14010 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14012 else if (SCALAR_INT_MODE_P (mode)
14013 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14015 dw_die_ref type_die = base_type_for_mode (mode, 0);
14016 dw_loc_descr_ref cvt;
14017 if (type_die == NULL)
14018 return NULL;
14019 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14020 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14021 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14022 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14023 add_loc_descr (&op0, cvt);
14024 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14025 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14026 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14027 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14028 add_loc_descr (&op1, cvt);
14031 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14032 op = DW_OP_lt;
14033 else
14034 op = DW_OP_gt;
14035 ret = op0;
14036 add_loc_descr (&ret, op1);
14037 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14038 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14039 add_loc_descr (&ret, bra_node);
14040 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14041 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14042 add_loc_descr (&ret, drop_node);
14043 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14044 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14045 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14046 && SCALAR_INT_MODE_P (mode)
14047 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14048 ret = convert_descriptor_to_mode (mode, ret);
14049 return ret;
14052 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14053 but after converting arguments to type_die, afterwards
14054 convert back to unsigned. */
14056 static dw_loc_descr_ref
14057 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14058 machine_mode mode, machine_mode mem_mode)
14060 dw_loc_descr_ref cvt, op0, op1;
14062 if (type_die == NULL)
14063 return NULL;
14064 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14065 VAR_INIT_STATUS_INITIALIZED);
14066 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14067 VAR_INIT_STATUS_INITIALIZED);
14068 if (op0 == NULL || op1 == NULL)
14069 return NULL;
14070 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14071 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14072 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14073 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14074 add_loc_descr (&op0, cvt);
14075 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14076 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14077 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14078 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14079 add_loc_descr (&op1, cvt);
14080 add_loc_descr (&op0, op1);
14081 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14082 return convert_descriptor_to_mode (mode, op0);
14085 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14086 const0 is DW_OP_lit0 or corresponding typed constant,
14087 const1 is DW_OP_lit1 or corresponding typed constant
14088 and constMSB is constant with just the MSB bit set
14089 for the mode):
14090 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14091 L1: const0 DW_OP_swap
14092 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14093 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14094 L3: DW_OP_drop
14095 L4: DW_OP_nop
14097 CTZ is similar:
14098 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14099 L1: const0 DW_OP_swap
14100 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14101 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14102 L3: DW_OP_drop
14103 L4: DW_OP_nop
14105 FFS is similar:
14106 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14107 L1: const1 DW_OP_swap
14108 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14109 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14110 L3: DW_OP_drop
14111 L4: DW_OP_nop */
14113 static dw_loc_descr_ref
14114 clz_loc_descriptor (rtx rtl, machine_mode mode,
14115 machine_mode mem_mode)
14117 dw_loc_descr_ref op0, ret, tmp;
14118 HOST_WIDE_INT valv;
14119 dw_loc_descr_ref l1jump, l1label;
14120 dw_loc_descr_ref l2jump, l2label;
14121 dw_loc_descr_ref l3jump, l3label;
14122 dw_loc_descr_ref l4jump, l4label;
14123 rtx msb;
14125 if (!SCALAR_INT_MODE_P (mode)
14126 || GET_MODE (XEXP (rtl, 0)) != mode)
14127 return NULL;
14129 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14130 VAR_INIT_STATUS_INITIALIZED);
14131 if (op0 == NULL)
14132 return NULL;
14133 ret = op0;
14134 if (GET_CODE (rtl) == CLZ)
14136 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14137 valv = GET_MODE_BITSIZE (mode);
14139 else if (GET_CODE (rtl) == FFS)
14140 valv = 0;
14141 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14142 valv = GET_MODE_BITSIZE (mode);
14143 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14144 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14145 add_loc_descr (&ret, l1jump);
14146 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14147 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14148 VAR_INIT_STATUS_INITIALIZED);
14149 if (tmp == NULL)
14150 return NULL;
14151 add_loc_descr (&ret, tmp);
14152 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14153 add_loc_descr (&ret, l4jump);
14154 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14155 ? const1_rtx : const0_rtx,
14156 mode, mem_mode,
14157 VAR_INIT_STATUS_INITIALIZED);
14158 if (l1label == NULL)
14159 return NULL;
14160 add_loc_descr (&ret, l1label);
14161 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14162 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14163 add_loc_descr (&ret, l2label);
14164 if (GET_CODE (rtl) != CLZ)
14165 msb = const1_rtx;
14166 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14167 msb = GEN_INT (HOST_WIDE_INT_1U
14168 << (GET_MODE_BITSIZE (mode) - 1));
14169 else
14170 msb = immed_wide_int_const
14171 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14172 GET_MODE_PRECISION (mode)), mode);
14173 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14174 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14175 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14176 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14177 else
14178 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14179 VAR_INIT_STATUS_INITIALIZED);
14180 if (tmp == NULL)
14181 return NULL;
14182 add_loc_descr (&ret, tmp);
14183 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14184 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14185 add_loc_descr (&ret, l3jump);
14186 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14187 VAR_INIT_STATUS_INITIALIZED);
14188 if (tmp == NULL)
14189 return NULL;
14190 add_loc_descr (&ret, tmp);
14191 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14192 ? DW_OP_shl : DW_OP_shr, 0, 0));
14193 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14194 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14195 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14196 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14197 add_loc_descr (&ret, l2jump);
14198 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14199 add_loc_descr (&ret, l3label);
14200 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14201 add_loc_descr (&ret, l4label);
14202 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14203 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14204 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14205 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14206 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14207 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14208 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14209 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14210 return ret;
14213 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14214 const1 is DW_OP_lit1 or corresponding typed constant):
14215 const0 DW_OP_swap
14216 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14217 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14218 L2: DW_OP_drop
14220 PARITY is similar:
14221 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14222 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14223 L2: DW_OP_drop */
14225 static dw_loc_descr_ref
14226 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14227 machine_mode mem_mode)
14229 dw_loc_descr_ref op0, ret, tmp;
14230 dw_loc_descr_ref l1jump, l1label;
14231 dw_loc_descr_ref l2jump, l2label;
14233 if (!SCALAR_INT_MODE_P (mode)
14234 || GET_MODE (XEXP (rtl, 0)) != mode)
14235 return NULL;
14237 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14238 VAR_INIT_STATUS_INITIALIZED);
14239 if (op0 == NULL)
14240 return NULL;
14241 ret = op0;
14242 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14243 VAR_INIT_STATUS_INITIALIZED);
14244 if (tmp == NULL)
14245 return NULL;
14246 add_loc_descr (&ret, tmp);
14247 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14248 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14249 add_loc_descr (&ret, l1label);
14250 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14251 add_loc_descr (&ret, l2jump);
14252 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14253 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14254 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14255 VAR_INIT_STATUS_INITIALIZED);
14256 if (tmp == NULL)
14257 return NULL;
14258 add_loc_descr (&ret, tmp);
14259 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14260 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14261 ? DW_OP_plus : DW_OP_xor, 0, 0));
14262 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14263 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14264 VAR_INIT_STATUS_INITIALIZED);
14265 add_loc_descr (&ret, tmp);
14266 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14267 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14268 add_loc_descr (&ret, l1jump);
14269 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14270 add_loc_descr (&ret, l2label);
14271 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14272 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14273 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14274 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14275 return ret;
14278 /* BSWAP (constS is initial shift count, either 56 or 24):
14279 constS const0
14280 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14281 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14282 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14283 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14284 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14286 static dw_loc_descr_ref
14287 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14288 machine_mode mem_mode)
14290 dw_loc_descr_ref op0, ret, tmp;
14291 dw_loc_descr_ref l1jump, l1label;
14292 dw_loc_descr_ref l2jump, l2label;
14294 if (!SCALAR_INT_MODE_P (mode)
14295 || BITS_PER_UNIT != 8
14296 || (GET_MODE_BITSIZE (mode) != 32
14297 && GET_MODE_BITSIZE (mode) != 64))
14298 return NULL;
14300 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14301 VAR_INIT_STATUS_INITIALIZED);
14302 if (op0 == NULL)
14303 return NULL;
14305 ret = op0;
14306 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14307 mode, mem_mode,
14308 VAR_INIT_STATUS_INITIALIZED);
14309 if (tmp == NULL)
14310 return NULL;
14311 add_loc_descr (&ret, tmp);
14312 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14313 VAR_INIT_STATUS_INITIALIZED);
14314 if (tmp == NULL)
14315 return NULL;
14316 add_loc_descr (&ret, tmp);
14317 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14318 add_loc_descr (&ret, l1label);
14319 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14320 mode, mem_mode,
14321 VAR_INIT_STATUS_INITIALIZED);
14322 add_loc_descr (&ret, tmp);
14323 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14325 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14326 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14327 VAR_INIT_STATUS_INITIALIZED);
14328 if (tmp == NULL)
14329 return NULL;
14330 add_loc_descr (&ret, tmp);
14331 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14332 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14333 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14334 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14335 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14336 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14337 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14338 VAR_INIT_STATUS_INITIALIZED);
14339 add_loc_descr (&ret, tmp);
14340 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14341 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14342 add_loc_descr (&ret, l2jump);
14343 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14344 VAR_INIT_STATUS_INITIALIZED);
14345 add_loc_descr (&ret, tmp);
14346 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14347 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14348 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14349 add_loc_descr (&ret, l1jump);
14350 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14351 add_loc_descr (&ret, l2label);
14352 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14353 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14354 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14355 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14356 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14357 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14358 return ret;
14361 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14362 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14363 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14364 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14366 ROTATERT is similar:
14367 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14368 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14369 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14371 static dw_loc_descr_ref
14372 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14373 machine_mode mem_mode)
14375 rtx rtlop1 = XEXP (rtl, 1);
14376 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14377 int i;
14379 if (!SCALAR_INT_MODE_P (mode))
14380 return NULL;
14382 if (GET_MODE (rtlop1) != VOIDmode
14383 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14384 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14385 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14386 VAR_INIT_STATUS_INITIALIZED);
14387 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14388 VAR_INIT_STATUS_INITIALIZED);
14389 if (op0 == NULL || op1 == NULL)
14390 return NULL;
14391 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14392 for (i = 0; i < 2; i++)
14394 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14395 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14396 mode, mem_mode,
14397 VAR_INIT_STATUS_INITIALIZED);
14398 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14399 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14400 ? DW_OP_const4u
14401 : HOST_BITS_PER_WIDE_INT == 64
14402 ? DW_OP_const8u : DW_OP_constu,
14403 GET_MODE_MASK (mode), 0);
14404 else
14405 mask[i] = NULL;
14406 if (mask[i] == NULL)
14407 return NULL;
14408 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14410 ret = op0;
14411 add_loc_descr (&ret, op1);
14412 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14413 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14414 if (GET_CODE (rtl) == ROTATERT)
14416 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14417 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14418 GET_MODE_BITSIZE (mode), 0));
14420 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14421 if (mask[0] != NULL)
14422 add_loc_descr (&ret, mask[0]);
14423 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14424 if (mask[1] != NULL)
14426 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14427 add_loc_descr (&ret, mask[1]);
14428 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14430 if (GET_CODE (rtl) == ROTATE)
14432 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14433 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14434 GET_MODE_BITSIZE (mode), 0));
14436 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14437 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14438 return ret;
14441 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14442 for DEBUG_PARAMETER_REF RTL. */
14444 static dw_loc_descr_ref
14445 parameter_ref_descriptor (rtx rtl)
14447 dw_loc_descr_ref ret;
14448 dw_die_ref ref;
14450 if (dwarf_strict)
14451 return NULL;
14452 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14453 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14454 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14455 if (ref)
14457 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14458 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14459 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14461 else
14463 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14464 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14466 return ret;
14469 /* The following routine converts the RTL for a variable or parameter
14470 (resident in memory) into an equivalent Dwarf representation of a
14471 mechanism for getting the address of that same variable onto the top of a
14472 hypothetical "address evaluation" stack.
14474 When creating memory location descriptors, we are effectively transforming
14475 the RTL for a memory-resident object into its Dwarf postfix expression
14476 equivalent. This routine recursively descends an RTL tree, turning
14477 it into Dwarf postfix code as it goes.
14479 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14481 MEM_MODE is the mode of the memory reference, needed to handle some
14482 autoincrement addressing modes.
14484 Return 0 if we can't represent the location. */
14486 dw_loc_descr_ref
14487 mem_loc_descriptor (rtx rtl, machine_mode mode,
14488 machine_mode mem_mode,
14489 enum var_init_status initialized)
14491 dw_loc_descr_ref mem_loc_result = NULL;
14492 enum dwarf_location_atom op;
14493 dw_loc_descr_ref op0, op1;
14494 rtx inner = NULL_RTX;
14496 if (mode == VOIDmode)
14497 mode = GET_MODE (rtl);
14499 /* Note that for a dynamically sized array, the location we will generate a
14500 description of here will be the lowest numbered location which is
14501 actually within the array. That's *not* necessarily the same as the
14502 zeroth element of the array. */
14504 rtl = targetm.delegitimize_address (rtl);
14506 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14507 return NULL;
14509 switch (GET_CODE (rtl))
14511 case POST_INC:
14512 case POST_DEC:
14513 case POST_MODIFY:
14514 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14516 case SUBREG:
14517 /* The case of a subreg may arise when we have a local (register)
14518 variable or a formal (register) parameter which doesn't quite fill
14519 up an entire register. For now, just assume that it is
14520 legitimate to make the Dwarf info refer to the whole register which
14521 contains the given subreg. */
14522 if (!subreg_lowpart_p (rtl))
14523 break;
14524 inner = SUBREG_REG (rtl);
14525 /* FALLTHRU */
14526 case TRUNCATE:
14527 if (inner == NULL_RTX)
14528 inner = XEXP (rtl, 0);
14529 if (SCALAR_INT_MODE_P (mode)
14530 && SCALAR_INT_MODE_P (GET_MODE (inner))
14531 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14532 #ifdef POINTERS_EXTEND_UNSIGNED
14533 || (mode == Pmode && mem_mode != VOIDmode)
14534 #endif
14536 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14538 mem_loc_result = mem_loc_descriptor (inner,
14539 GET_MODE (inner),
14540 mem_mode, initialized);
14541 break;
14543 if (dwarf_strict && dwarf_version < 5)
14544 break;
14545 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14546 break;
14547 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14548 && (!SCALAR_INT_MODE_P (mode)
14549 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14550 break;
14551 else
14553 dw_die_ref type_die;
14554 dw_loc_descr_ref cvt;
14556 mem_loc_result = mem_loc_descriptor (inner,
14557 GET_MODE (inner),
14558 mem_mode, initialized);
14559 if (mem_loc_result == NULL)
14560 break;
14561 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14562 if (type_die == NULL)
14564 mem_loc_result = NULL;
14565 break;
14567 if (GET_MODE_SIZE (mode)
14568 != GET_MODE_SIZE (GET_MODE (inner)))
14569 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14570 else
14571 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14572 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14573 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14574 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14575 add_loc_descr (&mem_loc_result, cvt);
14576 if (SCALAR_INT_MODE_P (mode)
14577 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14579 /* Convert it to untyped afterwards. */
14580 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14581 add_loc_descr (&mem_loc_result, cvt);
14584 break;
14586 case REG:
14587 if (! SCALAR_INT_MODE_P (mode)
14588 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14589 && rtl != arg_pointer_rtx
14590 && rtl != frame_pointer_rtx
14591 #ifdef POINTERS_EXTEND_UNSIGNED
14592 && (mode != Pmode || mem_mode == VOIDmode)
14593 #endif
14596 dw_die_ref type_die;
14597 unsigned int dbx_regnum;
14599 if (dwarf_strict && dwarf_version < 5)
14600 break;
14601 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14602 break;
14603 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14604 if (type_die == NULL)
14605 break;
14607 dbx_regnum = dbx_reg_number (rtl);
14608 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14609 break;
14610 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14611 dbx_regnum, 0);
14612 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14613 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14614 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14615 break;
14617 /* Whenever a register number forms a part of the description of the
14618 method for calculating the (dynamic) address of a memory resident
14619 object, DWARF rules require the register number be referred to as
14620 a "base register". This distinction is not based in any way upon
14621 what category of register the hardware believes the given register
14622 belongs to. This is strictly DWARF terminology we're dealing with
14623 here. Note that in cases where the location of a memory-resident
14624 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14625 OP_CONST (0)) the actual DWARF location descriptor that we generate
14626 may just be OP_BASEREG (basereg). This may look deceptively like
14627 the object in question was allocated to a register (rather than in
14628 memory) so DWARF consumers need to be aware of the subtle
14629 distinction between OP_REG and OP_BASEREG. */
14630 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14631 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14632 else if (stack_realign_drap
14633 && crtl->drap_reg
14634 && crtl->args.internal_arg_pointer == rtl
14635 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14637 /* If RTL is internal_arg_pointer, which has been optimized
14638 out, use DRAP instead. */
14639 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14640 VAR_INIT_STATUS_INITIALIZED);
14642 break;
14644 case SIGN_EXTEND:
14645 case ZERO_EXTEND:
14646 if (!SCALAR_INT_MODE_P (mode))
14647 break;
14648 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14649 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14650 if (op0 == 0)
14651 break;
14652 else if (GET_CODE (rtl) == ZERO_EXTEND
14653 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14654 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14655 < HOST_BITS_PER_WIDE_INT
14656 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14657 to expand zero extend as two shifts instead of
14658 masking. */
14659 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14661 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14662 mem_loc_result = op0;
14663 add_loc_descr (&mem_loc_result,
14664 int_loc_descriptor (GET_MODE_MASK (imode)));
14665 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14667 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14669 int shift = DWARF2_ADDR_SIZE
14670 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14671 shift *= BITS_PER_UNIT;
14672 if (GET_CODE (rtl) == SIGN_EXTEND)
14673 op = DW_OP_shra;
14674 else
14675 op = DW_OP_shr;
14676 mem_loc_result = op0;
14677 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14678 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14679 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14680 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14682 else if (!dwarf_strict || dwarf_version >= 5)
14684 dw_die_ref type_die1, type_die2;
14685 dw_loc_descr_ref cvt;
14687 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14688 GET_CODE (rtl) == ZERO_EXTEND);
14689 if (type_die1 == NULL)
14690 break;
14691 type_die2 = base_type_for_mode (mode, 1);
14692 if (type_die2 == NULL)
14693 break;
14694 mem_loc_result = op0;
14695 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14696 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14697 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14698 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14699 add_loc_descr (&mem_loc_result, cvt);
14700 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14701 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14702 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14703 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14704 add_loc_descr (&mem_loc_result, cvt);
14706 break;
14708 case MEM:
14710 rtx new_rtl = avoid_constant_pool_reference (rtl);
14711 if (new_rtl != rtl)
14713 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14714 initialized);
14715 if (mem_loc_result != NULL)
14716 return mem_loc_result;
14719 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14720 get_address_mode (rtl), mode,
14721 VAR_INIT_STATUS_INITIALIZED);
14722 if (mem_loc_result == NULL)
14723 mem_loc_result = tls_mem_loc_descriptor (rtl);
14724 if (mem_loc_result != NULL)
14726 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14727 || !SCALAR_INT_MODE_P(mode))
14729 dw_die_ref type_die;
14730 dw_loc_descr_ref deref;
14732 if (dwarf_strict && dwarf_version < 5)
14733 return NULL;
14734 type_die
14735 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14736 if (type_die == NULL)
14737 return NULL;
14738 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14739 GET_MODE_SIZE (mode), 0);
14740 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14741 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14742 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14743 add_loc_descr (&mem_loc_result, deref);
14745 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14746 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14747 else
14748 add_loc_descr (&mem_loc_result,
14749 new_loc_descr (DW_OP_deref_size,
14750 GET_MODE_SIZE (mode), 0));
14752 break;
14754 case LO_SUM:
14755 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14757 case LABEL_REF:
14758 /* Some ports can transform a symbol ref into a label ref, because
14759 the symbol ref is too far away and has to be dumped into a constant
14760 pool. */
14761 case CONST:
14762 case SYMBOL_REF:
14763 if (!SCALAR_INT_MODE_P (mode)
14764 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14765 #ifdef POINTERS_EXTEND_UNSIGNED
14766 && (mode != Pmode || mem_mode == VOIDmode)
14767 #endif
14769 break;
14770 if (GET_CODE (rtl) == SYMBOL_REF
14771 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14773 dw_loc_descr_ref temp;
14775 /* If this is not defined, we have no way to emit the data. */
14776 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14777 break;
14779 temp = new_addr_loc_descr (rtl, dtprel_true);
14781 /* We check for DWARF 5 here because gdb did not implement
14782 DW_OP_form_tls_address until after 7.12. */
14783 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14784 ? DW_OP_form_tls_address
14785 : DW_OP_GNU_push_tls_address),
14786 0, 0);
14787 add_loc_descr (&mem_loc_result, temp);
14789 break;
14792 if (!const_ok_for_output (rtl))
14794 if (GET_CODE (rtl) == CONST)
14795 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14796 initialized);
14797 break;
14800 symref:
14801 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14802 vec_safe_push (used_rtx_array, rtl);
14803 break;
14805 case CONCAT:
14806 case CONCATN:
14807 case VAR_LOCATION:
14808 case DEBUG_IMPLICIT_PTR:
14809 expansion_failed (NULL_TREE, rtl,
14810 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14811 return 0;
14813 case ENTRY_VALUE:
14814 if (dwarf_strict && dwarf_version < 5)
14815 return NULL;
14816 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14818 if (!SCALAR_INT_MODE_P (mode)
14819 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14820 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14821 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14822 else
14824 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14825 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14826 return NULL;
14827 op0 = one_reg_loc_descriptor (dbx_regnum,
14828 VAR_INIT_STATUS_INITIALIZED);
14831 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14832 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14834 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14835 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14836 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14837 return NULL;
14839 else
14840 gcc_unreachable ();
14841 if (op0 == NULL)
14842 return NULL;
14843 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14844 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14845 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14846 break;
14848 case DEBUG_PARAMETER_REF:
14849 mem_loc_result = parameter_ref_descriptor (rtl);
14850 break;
14852 case PRE_MODIFY:
14853 /* Extract the PLUS expression nested inside and fall into
14854 PLUS code below. */
14855 rtl = XEXP (rtl, 1);
14856 goto plus;
14858 case PRE_INC:
14859 case PRE_DEC:
14860 /* Turn these into a PLUS expression and fall into the PLUS code
14861 below. */
14862 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14863 gen_int_mode (GET_CODE (rtl) == PRE_INC
14864 ? GET_MODE_UNIT_SIZE (mem_mode)
14865 : -GET_MODE_UNIT_SIZE (mem_mode),
14866 mode));
14868 /* fall through */
14870 case PLUS:
14871 plus:
14872 if (is_based_loc (rtl)
14873 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14874 || XEXP (rtl, 0) == arg_pointer_rtx
14875 || XEXP (rtl, 0) == frame_pointer_rtx)
14876 && SCALAR_INT_MODE_P (mode))
14877 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14878 INTVAL (XEXP (rtl, 1)),
14879 VAR_INIT_STATUS_INITIALIZED);
14880 else
14882 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14883 VAR_INIT_STATUS_INITIALIZED);
14884 if (mem_loc_result == 0)
14885 break;
14887 if (CONST_INT_P (XEXP (rtl, 1))
14888 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14889 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14890 else
14892 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14893 VAR_INIT_STATUS_INITIALIZED);
14894 if (op1 == 0)
14895 return NULL;
14896 add_loc_descr (&mem_loc_result, op1);
14897 add_loc_descr (&mem_loc_result,
14898 new_loc_descr (DW_OP_plus, 0, 0));
14901 break;
14903 /* If a pseudo-reg is optimized away, it is possible for it to
14904 be replaced with a MEM containing a multiply or shift. */
14905 case MINUS:
14906 op = DW_OP_minus;
14907 goto do_binop;
14909 case MULT:
14910 op = DW_OP_mul;
14911 goto do_binop;
14913 case DIV:
14914 if ((!dwarf_strict || dwarf_version >= 5)
14915 && SCALAR_INT_MODE_P (mode)
14916 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14918 mem_loc_result = typed_binop (DW_OP_div, rtl,
14919 base_type_for_mode (mode, 0),
14920 mode, mem_mode);
14921 break;
14923 op = DW_OP_div;
14924 goto do_binop;
14926 case UMOD:
14927 op = DW_OP_mod;
14928 goto do_binop;
14930 case ASHIFT:
14931 op = DW_OP_shl;
14932 goto do_shift;
14934 case ASHIFTRT:
14935 op = DW_OP_shra;
14936 goto do_shift;
14938 case LSHIFTRT:
14939 op = DW_OP_shr;
14940 goto do_shift;
14942 do_shift:
14943 if (!SCALAR_INT_MODE_P (mode))
14944 break;
14945 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14946 VAR_INIT_STATUS_INITIALIZED);
14948 rtx rtlop1 = XEXP (rtl, 1);
14949 if (GET_MODE (rtlop1) != VOIDmode
14950 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
14951 < GET_MODE_BITSIZE (mode))
14952 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14953 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14954 VAR_INIT_STATUS_INITIALIZED);
14957 if (op0 == 0 || op1 == 0)
14958 break;
14960 mem_loc_result = op0;
14961 add_loc_descr (&mem_loc_result, op1);
14962 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14963 break;
14965 case AND:
14966 op = DW_OP_and;
14967 goto do_binop;
14969 case IOR:
14970 op = DW_OP_or;
14971 goto do_binop;
14973 case XOR:
14974 op = DW_OP_xor;
14975 goto do_binop;
14977 do_binop:
14978 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14979 VAR_INIT_STATUS_INITIALIZED);
14980 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14981 VAR_INIT_STATUS_INITIALIZED);
14983 if (op0 == 0 || op1 == 0)
14984 break;
14986 mem_loc_result = op0;
14987 add_loc_descr (&mem_loc_result, op1);
14988 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14989 break;
14991 case MOD:
14992 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14993 && (!dwarf_strict || dwarf_version >= 5))
14995 mem_loc_result = typed_binop (DW_OP_mod, rtl,
14996 base_type_for_mode (mode, 0),
14997 mode, mem_mode);
14998 break;
15001 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15002 VAR_INIT_STATUS_INITIALIZED);
15003 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15004 VAR_INIT_STATUS_INITIALIZED);
15006 if (op0 == 0 || op1 == 0)
15007 break;
15009 mem_loc_result = op0;
15010 add_loc_descr (&mem_loc_result, op1);
15011 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15012 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15013 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15014 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15015 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15016 break;
15018 case UDIV:
15019 if ((!dwarf_strict || dwarf_version >= 5)
15020 && SCALAR_INT_MODE_P (mode))
15022 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15024 op = DW_OP_div;
15025 goto do_binop;
15027 mem_loc_result = typed_binop (DW_OP_div, rtl,
15028 base_type_for_mode (mode, 1),
15029 mode, mem_mode);
15031 break;
15033 case NOT:
15034 op = DW_OP_not;
15035 goto do_unop;
15037 case ABS:
15038 op = DW_OP_abs;
15039 goto do_unop;
15041 case NEG:
15042 op = DW_OP_neg;
15043 goto do_unop;
15045 do_unop:
15046 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15047 VAR_INIT_STATUS_INITIALIZED);
15049 if (op0 == 0)
15050 break;
15052 mem_loc_result = op0;
15053 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15054 break;
15056 case CONST_INT:
15057 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15058 #ifdef POINTERS_EXTEND_UNSIGNED
15059 || (mode == Pmode
15060 && mem_mode != VOIDmode
15061 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15062 #endif
15065 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15066 break;
15068 if ((!dwarf_strict || dwarf_version >= 5)
15069 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15070 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15072 dw_die_ref type_die = base_type_for_mode (mode, 1);
15073 machine_mode amode;
15074 if (type_die == NULL)
15075 return NULL;
15076 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15077 MODE_INT, 0);
15078 if (INTVAL (rtl) >= 0
15079 && amode != BLKmode
15080 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15081 /* const DW_OP_convert <XXX> vs.
15082 DW_OP_const_type <XXX, 1, const>. */
15083 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15084 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15086 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15087 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15088 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15089 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15090 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15091 add_loc_descr (&mem_loc_result, op0);
15092 return mem_loc_result;
15094 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15095 INTVAL (rtl));
15096 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15097 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15098 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15099 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15100 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15101 else
15103 mem_loc_result->dw_loc_oprnd2.val_class
15104 = dw_val_class_const_double;
15105 mem_loc_result->dw_loc_oprnd2.v.val_double
15106 = double_int::from_shwi (INTVAL (rtl));
15109 break;
15111 case CONST_DOUBLE:
15112 if (!dwarf_strict || dwarf_version >= 5)
15114 dw_die_ref type_die;
15116 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15117 CONST_DOUBLE rtx could represent either a large integer
15118 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15119 the value is always a floating point constant.
15121 When it is an integer, a CONST_DOUBLE is used whenever
15122 the constant requires 2 HWIs to be adequately represented.
15123 We output CONST_DOUBLEs as blocks. */
15124 if (mode == VOIDmode
15125 || (GET_MODE (rtl) == VOIDmode
15126 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15127 break;
15128 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15129 if (type_die == NULL)
15130 return NULL;
15131 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15132 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15133 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15134 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15135 #if TARGET_SUPPORTS_WIDE_INT == 0
15136 if (!SCALAR_FLOAT_MODE_P (mode))
15138 mem_loc_result->dw_loc_oprnd2.val_class
15139 = dw_val_class_const_double;
15140 mem_loc_result->dw_loc_oprnd2.v.val_double
15141 = rtx_to_double_int (rtl);
15143 else
15144 #endif
15146 unsigned int length = GET_MODE_SIZE (mode);
15147 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15149 insert_float (rtl, array);
15150 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15151 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15152 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15153 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15156 break;
15158 case CONST_WIDE_INT:
15159 if (!dwarf_strict || dwarf_version >= 5)
15161 dw_die_ref type_die;
15163 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15164 if (type_die == NULL)
15165 return NULL;
15166 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15167 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15168 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15169 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15170 mem_loc_result->dw_loc_oprnd2.val_class
15171 = dw_val_class_wide_int;
15172 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15173 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15175 break;
15177 case EQ:
15178 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15179 break;
15181 case GE:
15182 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15183 break;
15185 case GT:
15186 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15187 break;
15189 case LE:
15190 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15191 break;
15193 case LT:
15194 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15195 break;
15197 case NE:
15198 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15199 break;
15201 case GEU:
15202 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15203 break;
15205 case GTU:
15206 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15207 break;
15209 case LEU:
15210 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15211 break;
15213 case LTU:
15214 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15215 break;
15217 case UMIN:
15218 case UMAX:
15219 if (!SCALAR_INT_MODE_P (mode))
15220 break;
15221 /* FALLTHRU */
15222 case SMIN:
15223 case SMAX:
15224 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15225 break;
15227 case ZERO_EXTRACT:
15228 case SIGN_EXTRACT:
15229 if (CONST_INT_P (XEXP (rtl, 1))
15230 && CONST_INT_P (XEXP (rtl, 2))
15231 && ((unsigned) INTVAL (XEXP (rtl, 1))
15232 + (unsigned) INTVAL (XEXP (rtl, 2))
15233 <= GET_MODE_BITSIZE (mode))
15234 && SCALAR_INT_MODE_P (mode)
15235 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15236 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15238 int shift, size;
15239 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15240 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15241 if (op0 == 0)
15242 break;
15243 if (GET_CODE (rtl) == SIGN_EXTRACT)
15244 op = DW_OP_shra;
15245 else
15246 op = DW_OP_shr;
15247 mem_loc_result = op0;
15248 size = INTVAL (XEXP (rtl, 1));
15249 shift = INTVAL (XEXP (rtl, 2));
15250 if (BITS_BIG_ENDIAN)
15251 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15252 - shift - size;
15253 if (shift + size != (int) DWARF2_ADDR_SIZE)
15255 add_loc_descr (&mem_loc_result,
15256 int_loc_descriptor (DWARF2_ADDR_SIZE
15257 - shift - size));
15258 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15260 if (size != (int) DWARF2_ADDR_SIZE)
15262 add_loc_descr (&mem_loc_result,
15263 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15264 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15267 break;
15269 case IF_THEN_ELSE:
15271 dw_loc_descr_ref op2, bra_node, drop_node;
15272 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15273 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15274 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15275 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15276 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15277 VAR_INIT_STATUS_INITIALIZED);
15278 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15279 VAR_INIT_STATUS_INITIALIZED);
15280 if (op0 == NULL || op1 == NULL || op2 == NULL)
15281 break;
15283 mem_loc_result = op1;
15284 add_loc_descr (&mem_loc_result, op2);
15285 add_loc_descr (&mem_loc_result, op0);
15286 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15287 add_loc_descr (&mem_loc_result, bra_node);
15288 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15289 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15290 add_loc_descr (&mem_loc_result, drop_node);
15291 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15292 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15294 break;
15296 case FLOAT_EXTEND:
15297 case FLOAT_TRUNCATE:
15298 case FLOAT:
15299 case UNSIGNED_FLOAT:
15300 case FIX:
15301 case UNSIGNED_FIX:
15302 if (!dwarf_strict || dwarf_version >= 5)
15304 dw_die_ref type_die;
15305 dw_loc_descr_ref cvt;
15307 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15308 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15309 if (op0 == NULL)
15310 break;
15311 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15312 && (GET_CODE (rtl) == FLOAT
15313 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15314 <= DWARF2_ADDR_SIZE))
15316 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15317 GET_CODE (rtl) == UNSIGNED_FLOAT);
15318 if (type_die == NULL)
15319 break;
15320 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15321 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15322 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15323 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15324 add_loc_descr (&op0, cvt);
15326 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15327 if (type_die == NULL)
15328 break;
15329 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15330 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15331 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15332 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15333 add_loc_descr (&op0, cvt);
15334 if (SCALAR_INT_MODE_P (mode)
15335 && (GET_CODE (rtl) == FIX
15336 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15338 op0 = convert_descriptor_to_mode (mode, op0);
15339 if (op0 == NULL)
15340 break;
15342 mem_loc_result = op0;
15344 break;
15346 case CLZ:
15347 case CTZ:
15348 case FFS:
15349 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15350 break;
15352 case POPCOUNT:
15353 case PARITY:
15354 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15355 break;
15357 case BSWAP:
15358 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15359 break;
15361 case ROTATE:
15362 case ROTATERT:
15363 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15364 break;
15366 case COMPARE:
15367 /* In theory, we could implement the above. */
15368 /* DWARF cannot represent the unsigned compare operations
15369 natively. */
15370 case SS_MULT:
15371 case US_MULT:
15372 case SS_DIV:
15373 case US_DIV:
15374 case SS_PLUS:
15375 case US_PLUS:
15376 case SS_MINUS:
15377 case US_MINUS:
15378 case SS_NEG:
15379 case US_NEG:
15380 case SS_ABS:
15381 case SS_ASHIFT:
15382 case US_ASHIFT:
15383 case SS_TRUNCATE:
15384 case US_TRUNCATE:
15385 case UNORDERED:
15386 case ORDERED:
15387 case UNEQ:
15388 case UNGE:
15389 case UNGT:
15390 case UNLE:
15391 case UNLT:
15392 case LTGT:
15393 case FRACT_CONVERT:
15394 case UNSIGNED_FRACT_CONVERT:
15395 case SAT_FRACT:
15396 case UNSIGNED_SAT_FRACT:
15397 case SQRT:
15398 case ASM_OPERANDS:
15399 case VEC_MERGE:
15400 case VEC_SELECT:
15401 case VEC_CONCAT:
15402 case VEC_DUPLICATE:
15403 case UNSPEC:
15404 case HIGH:
15405 case FMA:
15406 case STRICT_LOW_PART:
15407 case CONST_VECTOR:
15408 case CONST_FIXED:
15409 case CLRSB:
15410 case CLOBBER:
15411 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15412 can't express it in the debug info. This can happen e.g. with some
15413 TLS UNSPECs. */
15414 break;
15416 case CONST_STRING:
15417 resolve_one_addr (&rtl);
15418 goto symref;
15420 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15421 the expression. An UNSPEC rtx represents a raw DWARF operation,
15422 new_loc_descr is called for it to build the operation directly.
15423 Otherwise mem_loc_descriptor is called recursively. */
15424 case PARALLEL:
15426 int index = 0;
15427 dw_loc_descr_ref exp_result = NULL;
15429 for (; index < XVECLEN (rtl, 0); index++)
15431 rtx elem = XVECEXP (rtl, 0, index);
15432 if (GET_CODE (elem) == UNSPEC)
15434 /* Each DWARF operation UNSPEC contain two operands, if
15435 one operand is not used for the operation, const0_rtx is
15436 passed. */
15437 gcc_assert (XVECLEN (elem, 0) == 2);
15439 HOST_WIDE_INT dw_op = XINT (elem, 1);
15440 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15441 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15442 exp_result
15443 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15444 oprnd2);
15446 else
15447 exp_result
15448 = mem_loc_descriptor (elem, mode, mem_mode,
15449 VAR_INIT_STATUS_INITIALIZED);
15451 if (!mem_loc_result)
15452 mem_loc_result = exp_result;
15453 else
15454 add_loc_descr (&mem_loc_result, exp_result);
15457 break;
15460 default:
15461 if (flag_checking)
15463 print_rtl (stderr, rtl);
15464 gcc_unreachable ();
15466 break;
15469 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15470 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15472 return mem_loc_result;
15475 /* Return a descriptor that describes the concatenation of two locations.
15476 This is typically a complex variable. */
15478 static dw_loc_descr_ref
15479 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15481 dw_loc_descr_ref cc_loc_result = NULL;
15482 dw_loc_descr_ref x0_ref
15483 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15484 dw_loc_descr_ref x1_ref
15485 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15487 if (x0_ref == 0 || x1_ref == 0)
15488 return 0;
15490 cc_loc_result = x0_ref;
15491 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15493 add_loc_descr (&cc_loc_result, x1_ref);
15494 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15496 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15497 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15499 return cc_loc_result;
15502 /* Return a descriptor that describes the concatenation of N
15503 locations. */
15505 static dw_loc_descr_ref
15506 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15508 unsigned int i;
15509 dw_loc_descr_ref cc_loc_result = NULL;
15510 unsigned int n = XVECLEN (concatn, 0);
15512 for (i = 0; i < n; ++i)
15514 dw_loc_descr_ref ref;
15515 rtx x = XVECEXP (concatn, 0, i);
15517 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15518 if (ref == NULL)
15519 return NULL;
15521 add_loc_descr (&cc_loc_result, ref);
15522 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15525 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15526 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15528 return cc_loc_result;
15531 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15532 for DEBUG_IMPLICIT_PTR RTL. */
15534 static dw_loc_descr_ref
15535 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15537 dw_loc_descr_ref ret;
15538 dw_die_ref ref;
15540 if (dwarf_strict && dwarf_version < 5)
15541 return NULL;
15542 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15543 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15544 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15545 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15546 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15547 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15548 if (ref)
15550 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15551 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15552 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15554 else
15556 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15557 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15559 return ret;
15562 /* Output a proper Dwarf location descriptor for a variable or parameter
15563 which is either allocated in a register or in a memory location. For a
15564 register, we just generate an OP_REG and the register number. For a
15565 memory location we provide a Dwarf postfix expression describing how to
15566 generate the (dynamic) address of the object onto the address stack.
15568 MODE is mode of the decl if this loc_descriptor is going to be used in
15569 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15570 allowed, VOIDmode otherwise.
15572 If we don't know how to describe it, return 0. */
15574 static dw_loc_descr_ref
15575 loc_descriptor (rtx rtl, machine_mode mode,
15576 enum var_init_status initialized)
15578 dw_loc_descr_ref loc_result = NULL;
15580 switch (GET_CODE (rtl))
15582 case SUBREG:
15583 /* The case of a subreg may arise when we have a local (register)
15584 variable or a formal (register) parameter which doesn't quite fill
15585 up an entire register. For now, just assume that it is
15586 legitimate to make the Dwarf info refer to the whole register which
15587 contains the given subreg. */
15588 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15589 loc_result = loc_descriptor (SUBREG_REG (rtl),
15590 GET_MODE (SUBREG_REG (rtl)), initialized);
15591 else
15592 goto do_default;
15593 break;
15595 case REG:
15596 loc_result = reg_loc_descriptor (rtl, initialized);
15597 break;
15599 case MEM:
15600 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15601 GET_MODE (rtl), initialized);
15602 if (loc_result == NULL)
15603 loc_result = tls_mem_loc_descriptor (rtl);
15604 if (loc_result == NULL)
15606 rtx new_rtl = avoid_constant_pool_reference (rtl);
15607 if (new_rtl != rtl)
15608 loc_result = loc_descriptor (new_rtl, mode, initialized);
15610 break;
15612 case CONCAT:
15613 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15614 initialized);
15615 break;
15617 case CONCATN:
15618 loc_result = concatn_loc_descriptor (rtl, initialized);
15619 break;
15621 case VAR_LOCATION:
15622 /* Single part. */
15623 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15625 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15626 if (GET_CODE (loc) == EXPR_LIST)
15627 loc = XEXP (loc, 0);
15628 loc_result = loc_descriptor (loc, mode, initialized);
15629 break;
15632 rtl = XEXP (rtl, 1);
15633 /* FALLTHRU */
15635 case PARALLEL:
15637 rtvec par_elems = XVEC (rtl, 0);
15638 int num_elem = GET_NUM_ELEM (par_elems);
15639 machine_mode mode;
15640 int i;
15642 /* Create the first one, so we have something to add to. */
15643 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15644 VOIDmode, initialized);
15645 if (loc_result == NULL)
15646 return NULL;
15647 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15648 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15649 for (i = 1; i < num_elem; i++)
15651 dw_loc_descr_ref temp;
15653 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15654 VOIDmode, initialized);
15655 if (temp == NULL)
15656 return NULL;
15657 add_loc_descr (&loc_result, temp);
15658 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15659 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15662 break;
15664 case CONST_INT:
15665 if (mode != VOIDmode && mode != BLKmode)
15666 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15667 INTVAL (rtl));
15668 break;
15670 case CONST_DOUBLE:
15671 if (mode == VOIDmode)
15672 mode = GET_MODE (rtl);
15674 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15676 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15678 /* Note that a CONST_DOUBLE rtx could represent either an integer
15679 or a floating-point constant. A CONST_DOUBLE is used whenever
15680 the constant requires more than one word in order to be
15681 adequately represented. We output CONST_DOUBLEs as blocks. */
15682 loc_result = new_loc_descr (DW_OP_implicit_value,
15683 GET_MODE_SIZE (mode), 0);
15684 #if TARGET_SUPPORTS_WIDE_INT == 0
15685 if (!SCALAR_FLOAT_MODE_P (mode))
15687 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15688 loc_result->dw_loc_oprnd2.v.val_double
15689 = rtx_to_double_int (rtl);
15691 else
15692 #endif
15694 unsigned int length = GET_MODE_SIZE (mode);
15695 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15697 insert_float (rtl, array);
15698 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15699 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15700 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15701 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15704 break;
15706 case CONST_WIDE_INT:
15707 if (mode == VOIDmode)
15708 mode = GET_MODE (rtl);
15710 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15712 loc_result = new_loc_descr (DW_OP_implicit_value,
15713 GET_MODE_SIZE (mode), 0);
15714 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15715 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15716 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15718 break;
15720 case CONST_VECTOR:
15721 if (mode == VOIDmode)
15722 mode = GET_MODE (rtl);
15724 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15726 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15727 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15728 unsigned char *array
15729 = ggc_vec_alloc<unsigned char> (length * elt_size);
15730 unsigned int i;
15731 unsigned char *p;
15732 machine_mode imode = GET_MODE_INNER (mode);
15734 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15735 switch (GET_MODE_CLASS (mode))
15737 case MODE_VECTOR_INT:
15738 for (i = 0, p = array; i < length; i++, p += elt_size)
15740 rtx elt = CONST_VECTOR_ELT (rtl, i);
15741 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15743 break;
15745 case MODE_VECTOR_FLOAT:
15746 for (i = 0, p = array; i < length; i++, p += elt_size)
15748 rtx elt = CONST_VECTOR_ELT (rtl, i);
15749 insert_float (elt, p);
15751 break;
15753 default:
15754 gcc_unreachable ();
15757 loc_result = new_loc_descr (DW_OP_implicit_value,
15758 length * elt_size, 0);
15759 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15760 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15761 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15762 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15764 break;
15766 case CONST:
15767 if (mode == VOIDmode
15768 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15769 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15770 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15772 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15773 break;
15775 /* FALLTHROUGH */
15776 case SYMBOL_REF:
15777 if (!const_ok_for_output (rtl))
15778 break;
15779 /* FALLTHROUGH */
15780 case LABEL_REF:
15781 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15782 && (dwarf_version >= 4 || !dwarf_strict))
15784 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15785 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15786 vec_safe_push (used_rtx_array, rtl);
15788 break;
15790 case DEBUG_IMPLICIT_PTR:
15791 loc_result = implicit_ptr_descriptor (rtl, 0);
15792 break;
15794 case PLUS:
15795 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15796 && CONST_INT_P (XEXP (rtl, 1)))
15798 loc_result
15799 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15800 break;
15802 /* FALLTHRU */
15803 do_default:
15804 default:
15805 if ((SCALAR_INT_MODE_P (mode)
15806 && GET_MODE (rtl) == mode
15807 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15808 && dwarf_version >= 4)
15809 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15811 /* Value expression. */
15812 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15813 if (loc_result)
15814 add_loc_descr (&loc_result,
15815 new_loc_descr (DW_OP_stack_value, 0, 0));
15817 break;
15820 return loc_result;
15823 /* We need to figure out what section we should use as the base for the
15824 address ranges where a given location is valid.
15825 1. If this particular DECL has a section associated with it, use that.
15826 2. If this function has a section associated with it, use that.
15827 3. Otherwise, use the text section.
15828 XXX: If you split a variable across multiple sections, we won't notice. */
15830 static const char *
15831 secname_for_decl (const_tree decl)
15833 const char *secname;
15835 if (VAR_OR_FUNCTION_DECL_P (decl)
15836 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15837 && DECL_SECTION_NAME (decl))
15838 secname = DECL_SECTION_NAME (decl);
15839 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15840 secname = DECL_SECTION_NAME (current_function_decl);
15841 else if (cfun && in_cold_section_p)
15842 secname = crtl->subsections.cold_section_label;
15843 else
15844 secname = text_section_label;
15846 return secname;
15849 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15851 static bool
15852 decl_by_reference_p (tree decl)
15854 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15855 || VAR_P (decl))
15856 && DECL_BY_REFERENCE (decl));
15859 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15860 for VARLOC. */
15862 static dw_loc_descr_ref
15863 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15864 enum var_init_status initialized)
15866 int have_address = 0;
15867 dw_loc_descr_ref descr;
15868 machine_mode mode;
15870 if (want_address != 2)
15872 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15873 /* Single part. */
15874 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15876 varloc = PAT_VAR_LOCATION_LOC (varloc);
15877 if (GET_CODE (varloc) == EXPR_LIST)
15878 varloc = XEXP (varloc, 0);
15879 mode = GET_MODE (varloc);
15880 if (MEM_P (varloc))
15882 rtx addr = XEXP (varloc, 0);
15883 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15884 mode, initialized);
15885 if (descr)
15886 have_address = 1;
15887 else
15889 rtx x = avoid_constant_pool_reference (varloc);
15890 if (x != varloc)
15891 descr = mem_loc_descriptor (x, mode, VOIDmode,
15892 initialized);
15895 else
15896 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15898 else
15899 return 0;
15901 else
15903 if (GET_CODE (varloc) == VAR_LOCATION)
15904 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15905 else
15906 mode = DECL_MODE (loc);
15907 descr = loc_descriptor (varloc, mode, initialized);
15908 have_address = 1;
15911 if (!descr)
15912 return 0;
15914 if (want_address == 2 && !have_address
15915 && (dwarf_version >= 4 || !dwarf_strict))
15917 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15919 expansion_failed (loc, NULL_RTX,
15920 "DWARF address size mismatch");
15921 return 0;
15923 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15924 have_address = 1;
15926 /* Show if we can't fill the request for an address. */
15927 if (want_address && !have_address)
15929 expansion_failed (loc, NULL_RTX,
15930 "Want address and only have value");
15931 return 0;
15934 /* If we've got an address and don't want one, dereference. */
15935 if (!want_address && have_address)
15937 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15938 enum dwarf_location_atom op;
15940 if (size > DWARF2_ADDR_SIZE || size == -1)
15942 expansion_failed (loc, NULL_RTX,
15943 "DWARF address size mismatch");
15944 return 0;
15946 else if (size == DWARF2_ADDR_SIZE)
15947 op = DW_OP_deref;
15948 else
15949 op = DW_OP_deref_size;
15951 add_loc_descr (&descr, new_loc_descr (op, size, 0));
15954 return descr;
15957 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15958 if it is not possible. */
15960 static dw_loc_descr_ref
15961 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15963 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15964 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15965 else if (dwarf_version >= 3 || !dwarf_strict)
15966 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15967 else
15968 return NULL;
15971 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15972 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
15974 static dw_loc_descr_ref
15975 dw_sra_loc_expr (tree decl, rtx loc)
15977 rtx p;
15978 unsigned HOST_WIDE_INT padsize = 0;
15979 dw_loc_descr_ref descr, *descr_tail;
15980 unsigned HOST_WIDE_INT decl_size;
15981 rtx varloc;
15982 enum var_init_status initialized;
15984 if (DECL_SIZE (decl) == NULL
15985 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
15986 return NULL;
15988 decl_size = tree_to_uhwi (DECL_SIZE (decl));
15989 descr = NULL;
15990 descr_tail = &descr;
15992 for (p = loc; p; p = XEXP (p, 1))
15994 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
15995 rtx loc_note = *decl_piece_varloc_ptr (p);
15996 dw_loc_descr_ref cur_descr;
15997 dw_loc_descr_ref *tail, last = NULL;
15998 unsigned HOST_WIDE_INT opsize = 0;
16000 if (loc_note == NULL_RTX
16001 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16003 padsize += bitsize;
16004 continue;
16006 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16007 varloc = NOTE_VAR_LOCATION (loc_note);
16008 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16009 if (cur_descr == NULL)
16011 padsize += bitsize;
16012 continue;
16015 /* Check that cur_descr either doesn't use
16016 DW_OP_*piece operations, or their sum is equal
16017 to bitsize. Otherwise we can't embed it. */
16018 for (tail = &cur_descr; *tail != NULL;
16019 tail = &(*tail)->dw_loc_next)
16020 if ((*tail)->dw_loc_opc == DW_OP_piece)
16022 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16023 * BITS_PER_UNIT;
16024 last = *tail;
16026 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16028 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16029 last = *tail;
16032 if (last != NULL && opsize != bitsize)
16034 padsize += bitsize;
16035 /* Discard the current piece of the descriptor and release any
16036 addr_table entries it uses. */
16037 remove_loc_list_addr_table_entries (cur_descr);
16038 continue;
16041 /* If there is a hole, add DW_OP_*piece after empty DWARF
16042 expression, which means that those bits are optimized out. */
16043 if (padsize)
16045 if (padsize > decl_size)
16047 remove_loc_list_addr_table_entries (cur_descr);
16048 goto discard_descr;
16050 decl_size -= padsize;
16051 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16052 if (*descr_tail == NULL)
16054 remove_loc_list_addr_table_entries (cur_descr);
16055 goto discard_descr;
16057 descr_tail = &(*descr_tail)->dw_loc_next;
16058 padsize = 0;
16060 *descr_tail = cur_descr;
16061 descr_tail = tail;
16062 if (bitsize > decl_size)
16063 goto discard_descr;
16064 decl_size -= bitsize;
16065 if (last == NULL)
16067 HOST_WIDE_INT offset = 0;
16068 if (GET_CODE (varloc) == VAR_LOCATION
16069 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16071 varloc = PAT_VAR_LOCATION_LOC (varloc);
16072 if (GET_CODE (varloc) == EXPR_LIST)
16073 varloc = XEXP (varloc, 0);
16077 if (GET_CODE (varloc) == CONST
16078 || GET_CODE (varloc) == SIGN_EXTEND
16079 || GET_CODE (varloc) == ZERO_EXTEND)
16080 varloc = XEXP (varloc, 0);
16081 else if (GET_CODE (varloc) == SUBREG)
16082 varloc = SUBREG_REG (varloc);
16083 else
16084 break;
16086 while (1);
16087 /* DW_OP_bit_size offset should be zero for register
16088 or implicit location descriptions and empty location
16089 descriptions, but for memory addresses needs big endian
16090 adjustment. */
16091 if (MEM_P (varloc))
16093 unsigned HOST_WIDE_INT memsize
16094 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16095 if (memsize != bitsize)
16097 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16098 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16099 goto discard_descr;
16100 if (memsize < bitsize)
16101 goto discard_descr;
16102 if (BITS_BIG_ENDIAN)
16103 offset = memsize - bitsize;
16107 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16108 if (*descr_tail == NULL)
16109 goto discard_descr;
16110 descr_tail = &(*descr_tail)->dw_loc_next;
16114 /* If there were any non-empty expressions, add padding till the end of
16115 the decl. */
16116 if (descr != NULL && decl_size != 0)
16118 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16119 if (*descr_tail == NULL)
16120 goto discard_descr;
16122 return descr;
16124 discard_descr:
16125 /* Discard the descriptor and release any addr_table entries it uses. */
16126 remove_loc_list_addr_table_entries (descr);
16127 return NULL;
16130 /* Return the dwarf representation of the location list LOC_LIST of
16131 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16132 function. */
16134 static dw_loc_list_ref
16135 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16137 const char *endname, *secname;
16138 rtx varloc;
16139 enum var_init_status initialized;
16140 struct var_loc_node *node;
16141 dw_loc_descr_ref descr;
16142 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16143 dw_loc_list_ref list = NULL;
16144 dw_loc_list_ref *listp = &list;
16146 /* Now that we know what section we are using for a base,
16147 actually construct the list of locations.
16148 The first location information is what is passed to the
16149 function that creates the location list, and the remaining
16150 locations just get added on to that list.
16151 Note that we only know the start address for a location
16152 (IE location changes), so to build the range, we use
16153 the range [current location start, next location start].
16154 This means we have to special case the last node, and generate
16155 a range of [last location start, end of function label]. */
16157 secname = secname_for_decl (decl);
16159 for (node = loc_list->first; node; node = node->next)
16160 if (GET_CODE (node->loc) == EXPR_LIST
16161 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16163 if (GET_CODE (node->loc) == EXPR_LIST)
16165 /* This requires DW_OP_{,bit_}piece, which is not usable
16166 inside DWARF expressions. */
16167 if (want_address != 2)
16168 continue;
16169 descr = dw_sra_loc_expr (decl, node->loc);
16170 if (descr == NULL)
16171 continue;
16173 else
16175 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16176 varloc = NOTE_VAR_LOCATION (node->loc);
16177 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16179 if (descr)
16181 bool range_across_switch = false;
16182 /* If section switch happens in between node->label
16183 and node->next->label (or end of function) and
16184 we can't emit it as a single entry list,
16185 emit two ranges, first one ending at the end
16186 of first partition and second one starting at the
16187 beginning of second partition. */
16188 if (node == loc_list->last_before_switch
16189 && (node != loc_list->first || loc_list->first->next)
16190 && current_function_decl)
16192 endname = cfun->fde->dw_fde_end;
16193 range_across_switch = true;
16195 /* The variable has a location between NODE->LABEL and
16196 NODE->NEXT->LABEL. */
16197 else if (node->next)
16198 endname = node->next->label;
16199 /* If the variable has a location at the last label
16200 it keeps its location until the end of function. */
16201 else if (!current_function_decl)
16202 endname = text_end_label;
16203 else
16205 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16206 current_function_funcdef_no);
16207 endname = ggc_strdup (label_id);
16210 *listp = new_loc_list (descr, node->label, endname, secname);
16211 if (TREE_CODE (decl) == PARM_DECL
16212 && node == loc_list->first
16213 && NOTE_P (node->loc)
16214 && strcmp (node->label, endname) == 0)
16215 (*listp)->force = true;
16216 listp = &(*listp)->dw_loc_next;
16218 if (range_across_switch)
16220 if (GET_CODE (node->loc) == EXPR_LIST)
16221 descr = dw_sra_loc_expr (decl, node->loc);
16222 else
16224 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16225 varloc = NOTE_VAR_LOCATION (node->loc);
16226 descr = dw_loc_list_1 (decl, varloc, want_address,
16227 initialized);
16229 gcc_assert (descr);
16230 /* The variable has a location between NODE->LABEL and
16231 NODE->NEXT->LABEL. */
16232 if (node->next)
16233 endname = node->next->label;
16234 else
16235 endname = cfun->fde->dw_fde_second_end;
16236 *listp = new_loc_list (descr,
16237 cfun->fde->dw_fde_second_begin,
16238 endname, secname);
16239 listp = &(*listp)->dw_loc_next;
16244 /* Try to avoid the overhead of a location list emitting a location
16245 expression instead, but only if we didn't have more than one
16246 location entry in the first place. If some entries were not
16247 representable, we don't want to pretend a single entry that was
16248 applies to the entire scope in which the variable is
16249 available. */
16250 if (list && loc_list->first->next)
16251 gen_llsym (list);
16253 return list;
16256 /* Return if the loc_list has only single element and thus can be represented
16257 as location description. */
16259 static bool
16260 single_element_loc_list_p (dw_loc_list_ref list)
16262 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16263 return !list->ll_symbol;
16266 /* To each location in list LIST add loc descr REF. */
16268 static void
16269 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16271 dw_loc_descr_ref copy;
16272 add_loc_descr (&list->expr, ref);
16273 list = list->dw_loc_next;
16274 while (list)
16276 copy = ggc_alloc<dw_loc_descr_node> ();
16277 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16278 add_loc_descr (&list->expr, copy);
16279 while (copy->dw_loc_next)
16281 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
16282 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16283 copy->dw_loc_next = new_copy;
16284 copy = new_copy;
16286 list = list->dw_loc_next;
16290 /* Given two lists RET and LIST
16291 produce location list that is result of adding expression in LIST
16292 to expression in RET on each position in program.
16293 Might be destructive on both RET and LIST.
16295 TODO: We handle only simple cases of RET or LIST having at most one
16296 element. General case would inolve sorting the lists in program order
16297 and merging them that will need some additional work.
16298 Adding that will improve quality of debug info especially for SRA-ed
16299 structures. */
16301 static void
16302 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16304 if (!list)
16305 return;
16306 if (!*ret)
16308 *ret = list;
16309 return;
16311 if (!list->dw_loc_next)
16313 add_loc_descr_to_each (*ret, list->expr);
16314 return;
16316 if (!(*ret)->dw_loc_next)
16318 add_loc_descr_to_each (list, (*ret)->expr);
16319 *ret = list;
16320 return;
16322 expansion_failed (NULL_TREE, NULL_RTX,
16323 "Don't know how to merge two non-trivial"
16324 " location lists.\n");
16325 *ret = NULL;
16326 return;
16329 /* LOC is constant expression. Try a luck, look it up in constant
16330 pool and return its loc_descr of its address. */
16332 static dw_loc_descr_ref
16333 cst_pool_loc_descr (tree loc)
16335 /* Get an RTL for this, if something has been emitted. */
16336 rtx rtl = lookup_constant_def (loc);
16338 if (!rtl || !MEM_P (rtl))
16340 gcc_assert (!rtl);
16341 return 0;
16343 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16345 /* TODO: We might get more coverage if we was actually delaying expansion
16346 of all expressions till end of compilation when constant pools are fully
16347 populated. */
16348 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16350 expansion_failed (loc, NULL_RTX,
16351 "CST value in contant pool but not marked.");
16352 return 0;
16354 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16355 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16358 /* Return dw_loc_list representing address of addr_expr LOC
16359 by looking for inner INDIRECT_REF expression and turning
16360 it into simple arithmetics.
16362 See loc_list_from_tree for the meaning of CONTEXT. */
16364 static dw_loc_list_ref
16365 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16366 loc_descr_context *context)
16368 tree obj, offset;
16369 HOST_WIDE_INT bitsize, bitpos, bytepos;
16370 machine_mode mode;
16371 int unsignedp, reversep, volatilep = 0;
16372 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16374 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16375 &bitsize, &bitpos, &offset, &mode,
16376 &unsignedp, &reversep, &volatilep);
16377 STRIP_NOPS (obj);
16378 if (bitpos % BITS_PER_UNIT)
16380 expansion_failed (loc, NULL_RTX, "bitfield access");
16381 return 0;
16383 if (!INDIRECT_REF_P (obj))
16385 expansion_failed (obj,
16386 NULL_RTX, "no indirect ref in inner refrence");
16387 return 0;
16389 if (!offset && !bitpos)
16390 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16391 context);
16392 else if (toplev
16393 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16394 && (dwarf_version >= 4 || !dwarf_strict))
16396 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16397 if (!list_ret)
16398 return 0;
16399 if (offset)
16401 /* Variable offset. */
16402 list_ret1 = loc_list_from_tree (offset, 0, context);
16403 if (list_ret1 == 0)
16404 return 0;
16405 add_loc_list (&list_ret, list_ret1);
16406 if (!list_ret)
16407 return 0;
16408 add_loc_descr_to_each (list_ret,
16409 new_loc_descr (DW_OP_plus, 0, 0));
16411 bytepos = bitpos / BITS_PER_UNIT;
16412 if (bytepos > 0)
16413 add_loc_descr_to_each (list_ret,
16414 new_loc_descr (DW_OP_plus_uconst,
16415 bytepos, 0));
16416 else if (bytepos < 0)
16417 loc_list_plus_const (list_ret, bytepos);
16418 add_loc_descr_to_each (list_ret,
16419 new_loc_descr (DW_OP_stack_value, 0, 0));
16421 return list_ret;
16424 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16425 all operations from LOC are nops, move to the last one. Insert in NOPS all
16426 operations that are skipped. */
16428 static void
16429 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16430 hash_set<dw_loc_descr_ref> &nops)
16432 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16434 nops.add (loc);
16435 loc = loc->dw_loc_next;
16439 /* Helper for loc_descr_without_nops: free the location description operation
16440 P. */
16442 bool
16443 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16445 ggc_free (loc);
16446 return true;
16449 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16450 finishes LOC. */
16452 static void
16453 loc_descr_without_nops (dw_loc_descr_ref &loc)
16455 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16456 return;
16458 /* Set of all DW_OP_nop operations we remove. */
16459 hash_set<dw_loc_descr_ref> nops;
16461 /* First, strip all prefix NOP operations in order to keep the head of the
16462 operations list. */
16463 loc_descr_to_next_no_nop (loc, nops);
16465 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16467 /* For control flow operations: strip "prefix" nops in destination
16468 labels. */
16469 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16470 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16471 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16472 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16474 /* Do the same for the operations that follow, then move to the next
16475 iteration. */
16476 if (cur->dw_loc_next != NULL)
16477 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16478 cur = cur->dw_loc_next;
16481 nops.traverse<void *, free_loc_descr> (NULL);
16485 struct dwarf_procedure_info;
16487 /* Helper structure for location descriptions generation. */
16488 struct loc_descr_context
16490 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16491 NULL_TREE if DW_OP_push_object_address in invalid for this location
16492 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16493 tree context_type;
16494 /* The ..._DECL node that should be translated as a
16495 DW_OP_push_object_address operation. */
16496 tree base_decl;
16497 /* Information about the DWARF procedure we are currently generating. NULL if
16498 we are not generating a DWARF procedure. */
16499 struct dwarf_procedure_info *dpi;
16500 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16501 by consumer. Used for DW_TAG_generic_subrange attributes. */
16502 bool placeholder_arg;
16503 /* True if PLACEHOLDER_EXPR has been seen. */
16504 bool placeholder_seen;
16507 /* DWARF procedures generation
16509 DWARF expressions (aka. location descriptions) are used to encode variable
16510 things such as sizes or offsets. Such computations can have redundant parts
16511 that can be factorized in order to reduce the size of the output debug
16512 information. This is the whole point of DWARF procedures.
16514 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16515 already factorized into functions ("size functions") in order to handle very
16516 big and complex types. Such functions are quite simple: they have integral
16517 arguments, they return an integral result and their body contains only a
16518 return statement with arithmetic expressions. This is the only kind of
16519 function we are interested in translating into DWARF procedures, here.
16521 DWARF expressions and DWARF procedure are executed using a stack, so we have
16522 to define some calling convention for them to interact. Let's say that:
16524 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16525 all arguments in reverse order (right-to-left) so that when the DWARF
16526 procedure execution starts, the first argument is the top of the stack.
16528 - Then, when returning, the DWARF procedure must have consumed all arguments
16529 on the stack, must have pushed the result and touched nothing else.
16531 - Each integral argument and the result are integral types can be hold in a
16532 single stack slot.
16534 - We call "frame offset" the number of stack slots that are "under DWARF
16535 procedure control": it includes the arguments slots, the temporaries and
16536 the result slot. Thus, it is equal to the number of arguments when the
16537 procedure execution starts and must be equal to one (the result) when it
16538 returns. */
16540 /* Helper structure used when generating operations for a DWARF procedure. */
16541 struct dwarf_procedure_info
16543 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16544 currently translated. */
16545 tree fndecl;
16546 /* The number of arguments FNDECL takes. */
16547 unsigned args_count;
16550 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16551 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16552 equate it to this DIE. */
16554 static dw_die_ref
16555 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16556 dw_die_ref parent_die)
16558 dw_die_ref dwarf_proc_die;
16560 if ((dwarf_version < 3 && dwarf_strict)
16561 || location == NULL)
16562 return NULL;
16564 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16565 if (fndecl)
16566 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16567 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16568 return dwarf_proc_die;
16571 /* Return whether TYPE is a supported type as a DWARF procedure argument
16572 type or return type (we handle only scalar types and pointer types that
16573 aren't wider than the DWARF expression evaluation stack. */
16575 static bool
16576 is_handled_procedure_type (tree type)
16578 return ((INTEGRAL_TYPE_P (type)
16579 || TREE_CODE (type) == OFFSET_TYPE
16580 || TREE_CODE (type) == POINTER_TYPE)
16581 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16584 /* Helper for resolve_args_picking: do the same but stop when coming across
16585 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16586 offset *before* evaluating the corresponding operation. */
16588 static bool
16589 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16590 struct dwarf_procedure_info *dpi,
16591 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16593 /* The "frame_offset" identifier is already used to name a macro... */
16594 unsigned frame_offset_ = initial_frame_offset;
16595 dw_loc_descr_ref l;
16597 for (l = loc; l != NULL;)
16599 bool existed;
16600 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16602 /* If we already met this node, there is nothing to compute anymore. */
16603 if (existed)
16605 /* Make sure that the stack size is consistent wherever the execution
16606 flow comes from. */
16607 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16608 break;
16610 l_frame_offset = frame_offset_;
16612 /* If needed, relocate the picking offset with respect to the frame
16613 offset. */
16614 if (l->frame_offset_rel)
16616 unsigned HOST_WIDE_INT off;
16617 switch (l->dw_loc_opc)
16619 case DW_OP_pick:
16620 off = l->dw_loc_oprnd1.v.val_unsigned;
16621 break;
16622 case DW_OP_dup:
16623 off = 0;
16624 break;
16625 case DW_OP_over:
16626 off = 1;
16627 break;
16628 default:
16629 gcc_unreachable ();
16631 /* frame_offset_ is the size of the current stack frame, including
16632 incoming arguments. Besides, the arguments are pushed
16633 right-to-left. Thus, in order to access the Nth argument from
16634 this operation node, the picking has to skip temporaries *plus*
16635 one stack slot per argument (0 for the first one, 1 for the second
16636 one, etc.).
16638 The targetted argument number (N) is already set as the operand,
16639 and the number of temporaries can be computed with:
16640 frame_offsets_ - dpi->args_count */
16641 off += frame_offset_ - dpi->args_count;
16643 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16644 if (off > 255)
16645 return false;
16647 if (off == 0)
16649 l->dw_loc_opc = DW_OP_dup;
16650 l->dw_loc_oprnd1.v.val_unsigned = 0;
16652 else if (off == 1)
16654 l->dw_loc_opc = DW_OP_over;
16655 l->dw_loc_oprnd1.v.val_unsigned = 0;
16657 else
16659 l->dw_loc_opc = DW_OP_pick;
16660 l->dw_loc_oprnd1.v.val_unsigned = off;
16664 /* Update frame_offset according to the effect the current operation has
16665 on the stack. */
16666 switch (l->dw_loc_opc)
16668 case DW_OP_deref:
16669 case DW_OP_swap:
16670 case DW_OP_rot:
16671 case DW_OP_abs:
16672 case DW_OP_neg:
16673 case DW_OP_not:
16674 case DW_OP_plus_uconst:
16675 case DW_OP_skip:
16676 case DW_OP_reg0:
16677 case DW_OP_reg1:
16678 case DW_OP_reg2:
16679 case DW_OP_reg3:
16680 case DW_OP_reg4:
16681 case DW_OP_reg5:
16682 case DW_OP_reg6:
16683 case DW_OP_reg7:
16684 case DW_OP_reg8:
16685 case DW_OP_reg9:
16686 case DW_OP_reg10:
16687 case DW_OP_reg11:
16688 case DW_OP_reg12:
16689 case DW_OP_reg13:
16690 case DW_OP_reg14:
16691 case DW_OP_reg15:
16692 case DW_OP_reg16:
16693 case DW_OP_reg17:
16694 case DW_OP_reg18:
16695 case DW_OP_reg19:
16696 case DW_OP_reg20:
16697 case DW_OP_reg21:
16698 case DW_OP_reg22:
16699 case DW_OP_reg23:
16700 case DW_OP_reg24:
16701 case DW_OP_reg25:
16702 case DW_OP_reg26:
16703 case DW_OP_reg27:
16704 case DW_OP_reg28:
16705 case DW_OP_reg29:
16706 case DW_OP_reg30:
16707 case DW_OP_reg31:
16708 case DW_OP_bregx:
16709 case DW_OP_piece:
16710 case DW_OP_deref_size:
16711 case DW_OP_nop:
16712 case DW_OP_bit_piece:
16713 case DW_OP_implicit_value:
16714 case DW_OP_stack_value:
16715 break;
16717 case DW_OP_addr:
16718 case DW_OP_const1u:
16719 case DW_OP_const1s:
16720 case DW_OP_const2u:
16721 case DW_OP_const2s:
16722 case DW_OP_const4u:
16723 case DW_OP_const4s:
16724 case DW_OP_const8u:
16725 case DW_OP_const8s:
16726 case DW_OP_constu:
16727 case DW_OP_consts:
16728 case DW_OP_dup:
16729 case DW_OP_over:
16730 case DW_OP_pick:
16731 case DW_OP_lit0:
16732 case DW_OP_lit1:
16733 case DW_OP_lit2:
16734 case DW_OP_lit3:
16735 case DW_OP_lit4:
16736 case DW_OP_lit5:
16737 case DW_OP_lit6:
16738 case DW_OP_lit7:
16739 case DW_OP_lit8:
16740 case DW_OP_lit9:
16741 case DW_OP_lit10:
16742 case DW_OP_lit11:
16743 case DW_OP_lit12:
16744 case DW_OP_lit13:
16745 case DW_OP_lit14:
16746 case DW_OP_lit15:
16747 case DW_OP_lit16:
16748 case DW_OP_lit17:
16749 case DW_OP_lit18:
16750 case DW_OP_lit19:
16751 case DW_OP_lit20:
16752 case DW_OP_lit21:
16753 case DW_OP_lit22:
16754 case DW_OP_lit23:
16755 case DW_OP_lit24:
16756 case DW_OP_lit25:
16757 case DW_OP_lit26:
16758 case DW_OP_lit27:
16759 case DW_OP_lit28:
16760 case DW_OP_lit29:
16761 case DW_OP_lit30:
16762 case DW_OP_lit31:
16763 case DW_OP_breg0:
16764 case DW_OP_breg1:
16765 case DW_OP_breg2:
16766 case DW_OP_breg3:
16767 case DW_OP_breg4:
16768 case DW_OP_breg5:
16769 case DW_OP_breg6:
16770 case DW_OP_breg7:
16771 case DW_OP_breg8:
16772 case DW_OP_breg9:
16773 case DW_OP_breg10:
16774 case DW_OP_breg11:
16775 case DW_OP_breg12:
16776 case DW_OP_breg13:
16777 case DW_OP_breg14:
16778 case DW_OP_breg15:
16779 case DW_OP_breg16:
16780 case DW_OP_breg17:
16781 case DW_OP_breg18:
16782 case DW_OP_breg19:
16783 case DW_OP_breg20:
16784 case DW_OP_breg21:
16785 case DW_OP_breg22:
16786 case DW_OP_breg23:
16787 case DW_OP_breg24:
16788 case DW_OP_breg25:
16789 case DW_OP_breg26:
16790 case DW_OP_breg27:
16791 case DW_OP_breg28:
16792 case DW_OP_breg29:
16793 case DW_OP_breg30:
16794 case DW_OP_breg31:
16795 case DW_OP_fbreg:
16796 case DW_OP_push_object_address:
16797 case DW_OP_call_frame_cfa:
16798 ++frame_offset_;
16799 break;
16801 case DW_OP_drop:
16802 case DW_OP_xderef:
16803 case DW_OP_and:
16804 case DW_OP_div:
16805 case DW_OP_minus:
16806 case DW_OP_mod:
16807 case DW_OP_mul:
16808 case DW_OP_or:
16809 case DW_OP_plus:
16810 case DW_OP_shl:
16811 case DW_OP_shr:
16812 case DW_OP_shra:
16813 case DW_OP_xor:
16814 case DW_OP_bra:
16815 case DW_OP_eq:
16816 case DW_OP_ge:
16817 case DW_OP_gt:
16818 case DW_OP_le:
16819 case DW_OP_lt:
16820 case DW_OP_ne:
16821 case DW_OP_regx:
16822 case DW_OP_xderef_size:
16823 --frame_offset_;
16824 break;
16826 case DW_OP_call2:
16827 case DW_OP_call4:
16828 case DW_OP_call_ref:
16830 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16831 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16833 if (stack_usage == NULL)
16834 return false;
16835 frame_offset_ += *stack_usage;
16836 break;
16839 case DW_OP_implicit_pointer:
16840 case DW_OP_entry_value:
16841 case DW_OP_const_type:
16842 case DW_OP_regval_type:
16843 case DW_OP_deref_type:
16844 case DW_OP_convert:
16845 case DW_OP_reinterpret:
16846 case DW_OP_form_tls_address:
16847 case DW_OP_GNU_push_tls_address:
16848 case DW_OP_GNU_uninit:
16849 case DW_OP_GNU_encoded_addr:
16850 case DW_OP_GNU_implicit_pointer:
16851 case DW_OP_GNU_entry_value:
16852 case DW_OP_GNU_const_type:
16853 case DW_OP_GNU_regval_type:
16854 case DW_OP_GNU_deref_type:
16855 case DW_OP_GNU_convert:
16856 case DW_OP_GNU_reinterpret:
16857 case DW_OP_GNU_parameter_ref:
16858 /* loc_list_from_tree will probably not output these operations for
16859 size functions, so assume they will not appear here. */
16860 /* Fall through... */
16862 default:
16863 gcc_unreachable ();
16866 /* Now, follow the control flow (except subroutine calls). */
16867 switch (l->dw_loc_opc)
16869 case DW_OP_bra:
16870 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16871 frame_offsets))
16872 return false;
16873 /* Fall through. */
16875 case DW_OP_skip:
16876 l = l->dw_loc_oprnd1.v.val_loc;
16877 break;
16879 case DW_OP_stack_value:
16880 return true;
16882 default:
16883 l = l->dw_loc_next;
16884 break;
16888 return true;
16891 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16892 operations) in order to resolve the operand of DW_OP_pick operations that
16893 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16894 offset *before* LOC is executed. Return if all relocations were
16895 successful. */
16897 static bool
16898 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16899 struct dwarf_procedure_info *dpi)
16901 /* Associate to all visited operations the frame offset *before* evaluating
16902 this operation. */
16903 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16905 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16906 frame_offsets);
16909 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
16910 Return NULL if it is not possible. */
16912 static dw_die_ref
16913 function_to_dwarf_procedure (tree fndecl)
16915 struct loc_descr_context ctx;
16916 struct dwarf_procedure_info dpi;
16917 dw_die_ref dwarf_proc_die;
16918 tree tree_body = DECL_SAVED_TREE (fndecl);
16919 dw_loc_descr_ref loc_body, epilogue;
16921 tree cursor;
16922 unsigned i;
16924 /* Do not generate multiple DWARF procedures for the same function
16925 declaration. */
16926 dwarf_proc_die = lookup_decl_die (fndecl);
16927 if (dwarf_proc_die != NULL)
16928 return dwarf_proc_die;
16930 /* DWARF procedures are available starting with the DWARFv3 standard. */
16931 if (dwarf_version < 3 && dwarf_strict)
16932 return NULL;
16934 /* We handle only functions for which we still have a body, that return a
16935 supported type and that takes arguments with supported types. Note that
16936 there is no point translating functions that return nothing. */
16937 if (tree_body == NULL_TREE
16938 || DECL_RESULT (fndecl) == NULL_TREE
16939 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
16940 return NULL;
16942 for (cursor = DECL_ARGUMENTS (fndecl);
16943 cursor != NULL_TREE;
16944 cursor = TREE_CHAIN (cursor))
16945 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
16946 return NULL;
16948 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
16949 if (TREE_CODE (tree_body) != RETURN_EXPR)
16950 return NULL;
16951 tree_body = TREE_OPERAND (tree_body, 0);
16952 if (TREE_CODE (tree_body) != MODIFY_EXPR
16953 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
16954 return NULL;
16955 tree_body = TREE_OPERAND (tree_body, 1);
16957 /* Try to translate the body expression itself. Note that this will probably
16958 cause an infinite recursion if its call graph has a cycle. This is very
16959 unlikely for size functions, however, so don't bother with such things at
16960 the moment. */
16961 ctx.context_type = NULL_TREE;
16962 ctx.base_decl = NULL_TREE;
16963 ctx.dpi = &dpi;
16964 ctx.placeholder_arg = false;
16965 ctx.placeholder_seen = false;
16966 dpi.fndecl = fndecl;
16967 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
16968 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
16969 if (!loc_body)
16970 return NULL;
16972 /* After evaluating all operands in "loc_body", we should still have on the
16973 stack all arguments plus the desired function result (top of the stack).
16974 Generate code in order to keep only the result in our stack frame. */
16975 epilogue = NULL;
16976 for (i = 0; i < dpi.args_count; ++i)
16978 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
16979 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
16980 op_couple->dw_loc_next->dw_loc_next = epilogue;
16981 epilogue = op_couple;
16983 add_loc_descr (&loc_body, epilogue);
16984 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
16985 return NULL;
16987 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
16988 because they are considered useful. Now there is an epilogue, they are
16989 not anymore, so give it another try. */
16990 loc_descr_without_nops (loc_body);
16992 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
16993 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
16994 though, given that size functions do not come from source, so they should
16995 not have a dedicated DW_TAG_subprogram DIE. */
16996 dwarf_proc_die
16997 = new_dwarf_proc_die (loc_body, fndecl,
16998 get_context_die (DECL_CONTEXT (fndecl)));
17000 /* The called DWARF procedure consumes one stack slot per argument and
17001 returns one stack slot. */
17002 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17004 return dwarf_proc_die;
17008 /* Generate Dwarf location list representing LOC.
17009 If WANT_ADDRESS is false, expression computing LOC will be computed
17010 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17011 if WANT_ADDRESS is 2, expression computing address useable in location
17012 will be returned (i.e. DW_OP_reg can be used
17013 to refer to register values).
17015 CONTEXT provides information to customize the location descriptions
17016 generation. Its context_type field specifies what type is implicitly
17017 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17018 will not be generated.
17020 Its DPI field determines whether we are generating a DWARF expression for a
17021 DWARF procedure, so PARM_DECL references are processed specifically.
17023 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17024 and dpi fields were null. */
17026 static dw_loc_list_ref
17027 loc_list_from_tree_1 (tree loc, int want_address,
17028 struct loc_descr_context *context)
17030 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17031 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17032 int have_address = 0;
17033 enum dwarf_location_atom op;
17035 /* ??? Most of the time we do not take proper care for sign/zero
17036 extending the values properly. Hopefully this won't be a real
17037 problem... */
17039 if (context != NULL
17040 && context->base_decl == loc
17041 && want_address == 0)
17043 if (dwarf_version >= 3 || !dwarf_strict)
17044 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17045 NULL, NULL, NULL);
17046 else
17047 return NULL;
17050 switch (TREE_CODE (loc))
17052 case ERROR_MARK:
17053 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17054 return 0;
17056 case PLACEHOLDER_EXPR:
17057 /* This case involves extracting fields from an object to determine the
17058 position of other fields. It is supposed to appear only as the first
17059 operand of COMPONENT_REF nodes and to reference precisely the type
17060 that the context allows. */
17061 if (context != NULL
17062 && TREE_TYPE (loc) == context->context_type
17063 && want_address >= 1)
17065 if (dwarf_version >= 3 || !dwarf_strict)
17067 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17068 have_address = 1;
17069 break;
17071 else
17072 return NULL;
17074 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17075 the single argument passed by consumer. */
17076 else if (context != NULL
17077 && context->placeholder_arg
17078 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17079 && want_address == 0)
17081 ret = new_loc_descr (DW_OP_pick, 0, 0);
17082 ret->frame_offset_rel = 1;
17083 context->placeholder_seen = true;
17084 break;
17086 else
17087 expansion_failed (loc, NULL_RTX,
17088 "PLACEHOLDER_EXPR for an unexpected type");
17089 break;
17091 case CALL_EXPR:
17093 const int nargs = call_expr_nargs (loc);
17094 tree callee = get_callee_fndecl (loc);
17095 int i;
17096 dw_die_ref dwarf_proc;
17098 if (callee == NULL_TREE)
17099 goto call_expansion_failed;
17101 /* We handle only functions that return an integer. */
17102 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17103 goto call_expansion_failed;
17105 dwarf_proc = function_to_dwarf_procedure (callee);
17106 if (dwarf_proc == NULL)
17107 goto call_expansion_failed;
17109 /* Evaluate arguments right-to-left so that the first argument will
17110 be the top-most one on the stack. */
17111 for (i = nargs - 1; i >= 0; --i)
17113 dw_loc_descr_ref loc_descr
17114 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17115 context);
17117 if (loc_descr == NULL)
17118 goto call_expansion_failed;
17120 add_loc_descr (&ret, loc_descr);
17123 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17124 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17125 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17126 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17127 add_loc_descr (&ret, ret1);
17128 break;
17130 call_expansion_failed:
17131 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17132 /* There are no opcodes for these operations. */
17133 return 0;
17136 case PREINCREMENT_EXPR:
17137 case PREDECREMENT_EXPR:
17138 case POSTINCREMENT_EXPR:
17139 case POSTDECREMENT_EXPR:
17140 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17141 /* There are no opcodes for these operations. */
17142 return 0;
17144 case ADDR_EXPR:
17145 /* If we already want an address, see if there is INDIRECT_REF inside
17146 e.g. for &this->field. */
17147 if (want_address)
17149 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17150 (loc, want_address == 2, context);
17151 if (list_ret)
17152 have_address = 1;
17153 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17154 && (ret = cst_pool_loc_descr (loc)))
17155 have_address = 1;
17157 /* Otherwise, process the argument and look for the address. */
17158 if (!list_ret && !ret)
17159 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17160 else
17162 if (want_address)
17163 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17164 return NULL;
17166 break;
17168 case VAR_DECL:
17169 if (DECL_THREAD_LOCAL_P (loc))
17171 rtx rtl;
17172 enum dwarf_location_atom tls_op;
17173 enum dtprel_bool dtprel = dtprel_false;
17175 if (targetm.have_tls)
17177 /* If this is not defined, we have no way to emit the
17178 data. */
17179 if (!targetm.asm_out.output_dwarf_dtprel)
17180 return 0;
17182 /* The way DW_OP_GNU_push_tls_address is specified, we
17183 can only look up addresses of objects in the current
17184 module. We used DW_OP_addr as first op, but that's
17185 wrong, because DW_OP_addr is relocated by the debug
17186 info consumer, while DW_OP_GNU_push_tls_address
17187 operand shouldn't be. */
17188 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17189 return 0;
17190 dtprel = dtprel_true;
17191 /* We check for DWARF 5 here because gdb did not implement
17192 DW_OP_form_tls_address until after 7.12. */
17193 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17194 : DW_OP_GNU_push_tls_address);
17196 else
17198 if (!targetm.emutls.debug_form_tls_address
17199 || !(dwarf_version >= 3 || !dwarf_strict))
17200 return 0;
17201 /* We stuffed the control variable into the DECL_VALUE_EXPR
17202 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17203 no longer appear in gimple code. We used the control
17204 variable in specific so that we could pick it up here. */
17205 loc = DECL_VALUE_EXPR (loc);
17206 tls_op = DW_OP_form_tls_address;
17209 rtl = rtl_for_decl_location (loc);
17210 if (rtl == NULL_RTX)
17211 return 0;
17213 if (!MEM_P (rtl))
17214 return 0;
17215 rtl = XEXP (rtl, 0);
17216 if (! CONSTANT_P (rtl))
17217 return 0;
17219 ret = new_addr_loc_descr (rtl, dtprel);
17220 ret1 = new_loc_descr (tls_op, 0, 0);
17221 add_loc_descr (&ret, ret1);
17223 have_address = 1;
17224 break;
17226 /* FALLTHRU */
17228 case PARM_DECL:
17229 if (context != NULL && context->dpi != NULL
17230 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17232 /* We are generating code for a DWARF procedure and we want to access
17233 one of its arguments: find the appropriate argument offset and let
17234 the resolve_args_picking pass compute the offset that complies
17235 with the stack frame size. */
17236 unsigned i = 0;
17237 tree cursor;
17239 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17240 cursor != NULL_TREE && cursor != loc;
17241 cursor = TREE_CHAIN (cursor), ++i)
17243 /* If we are translating a DWARF procedure, all referenced parameters
17244 must belong to the current function. */
17245 gcc_assert (cursor != NULL_TREE);
17247 ret = new_loc_descr (DW_OP_pick, i, 0);
17248 ret->frame_offset_rel = 1;
17249 break;
17251 /* FALLTHRU */
17253 case RESULT_DECL:
17254 if (DECL_HAS_VALUE_EXPR_P (loc))
17255 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17256 want_address, context);
17257 /* FALLTHRU */
17259 case FUNCTION_DECL:
17261 rtx rtl;
17262 var_loc_list *loc_list = lookup_decl_loc (loc);
17264 if (loc_list && loc_list->first)
17266 list_ret = dw_loc_list (loc_list, loc, want_address);
17267 have_address = want_address != 0;
17268 break;
17270 rtl = rtl_for_decl_location (loc);
17271 if (rtl == NULL_RTX)
17273 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17274 return 0;
17276 else if (CONST_INT_P (rtl))
17278 HOST_WIDE_INT val = INTVAL (rtl);
17279 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17280 val &= GET_MODE_MASK (DECL_MODE (loc));
17281 ret = int_loc_descriptor (val);
17283 else if (GET_CODE (rtl) == CONST_STRING)
17285 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17286 return 0;
17288 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17289 ret = new_addr_loc_descr (rtl, dtprel_false);
17290 else
17292 machine_mode mode, mem_mode;
17294 /* Certain constructs can only be represented at top-level. */
17295 if (want_address == 2)
17297 ret = loc_descriptor (rtl, VOIDmode,
17298 VAR_INIT_STATUS_INITIALIZED);
17299 have_address = 1;
17301 else
17303 mode = GET_MODE (rtl);
17304 mem_mode = VOIDmode;
17305 if (MEM_P (rtl))
17307 mem_mode = mode;
17308 mode = get_address_mode (rtl);
17309 rtl = XEXP (rtl, 0);
17310 have_address = 1;
17312 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17313 VAR_INIT_STATUS_INITIALIZED);
17315 if (!ret)
17316 expansion_failed (loc, rtl,
17317 "failed to produce loc descriptor for rtl");
17320 break;
17322 case MEM_REF:
17323 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17325 have_address = 1;
17326 goto do_plus;
17328 /* Fallthru. */
17329 case INDIRECT_REF:
17330 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17331 have_address = 1;
17332 break;
17334 case TARGET_MEM_REF:
17335 case SSA_NAME:
17336 case DEBUG_EXPR_DECL:
17337 return NULL;
17339 case COMPOUND_EXPR:
17340 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17341 context);
17343 CASE_CONVERT:
17344 case VIEW_CONVERT_EXPR:
17345 case SAVE_EXPR:
17346 case MODIFY_EXPR:
17347 case NON_LVALUE_EXPR:
17348 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17349 context);
17351 case COMPONENT_REF:
17352 case BIT_FIELD_REF:
17353 case ARRAY_REF:
17354 case ARRAY_RANGE_REF:
17355 case REALPART_EXPR:
17356 case IMAGPART_EXPR:
17358 tree obj, offset;
17359 HOST_WIDE_INT bitsize, bitpos, bytepos;
17360 machine_mode mode;
17361 int unsignedp, reversep, volatilep = 0;
17363 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17364 &unsignedp, &reversep, &volatilep);
17366 gcc_assert (obj != loc);
17368 list_ret = loc_list_from_tree_1 (obj,
17369 want_address == 2
17370 && !bitpos && !offset ? 2 : 1,
17371 context);
17372 /* TODO: We can extract value of the small expression via shifting even
17373 for nonzero bitpos. */
17374 if (list_ret == 0)
17375 return 0;
17376 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17378 expansion_failed (loc, NULL_RTX,
17379 "bitfield access");
17380 return 0;
17383 if (offset != NULL_TREE)
17385 /* Variable offset. */
17386 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17387 if (list_ret1 == 0)
17388 return 0;
17389 add_loc_list (&list_ret, list_ret1);
17390 if (!list_ret)
17391 return 0;
17392 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17395 bytepos = bitpos / BITS_PER_UNIT;
17396 if (bytepos > 0)
17397 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17398 else if (bytepos < 0)
17399 loc_list_plus_const (list_ret, bytepos);
17401 have_address = 1;
17402 break;
17405 case INTEGER_CST:
17406 if ((want_address || !tree_fits_shwi_p (loc))
17407 && (ret = cst_pool_loc_descr (loc)))
17408 have_address = 1;
17409 else if (want_address == 2
17410 && tree_fits_shwi_p (loc)
17411 && (ret = address_of_int_loc_descriptor
17412 (int_size_in_bytes (TREE_TYPE (loc)),
17413 tree_to_shwi (loc))))
17414 have_address = 1;
17415 else if (tree_fits_shwi_p (loc))
17416 ret = int_loc_descriptor (tree_to_shwi (loc));
17417 else if (tree_fits_uhwi_p (loc))
17418 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17419 else
17421 expansion_failed (loc, NULL_RTX,
17422 "Integer operand is not host integer");
17423 return 0;
17425 break;
17427 case CONSTRUCTOR:
17428 case REAL_CST:
17429 case STRING_CST:
17430 case COMPLEX_CST:
17431 if ((ret = cst_pool_loc_descr (loc)))
17432 have_address = 1;
17433 else if (TREE_CODE (loc) == CONSTRUCTOR)
17435 tree type = TREE_TYPE (loc);
17436 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17437 unsigned HOST_WIDE_INT offset = 0;
17438 unsigned HOST_WIDE_INT cnt;
17439 constructor_elt *ce;
17441 if (TREE_CODE (type) == RECORD_TYPE)
17443 /* This is very limited, but it's enough to output
17444 pointers to member functions, as long as the
17445 referenced function is defined in the current
17446 translation unit. */
17447 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17449 tree val = ce->value;
17451 tree field = ce->index;
17453 if (val)
17454 STRIP_NOPS (val);
17456 if (!field || DECL_BIT_FIELD (field))
17458 expansion_failed (loc, NULL_RTX,
17459 "bitfield in record type constructor");
17460 size = offset = (unsigned HOST_WIDE_INT)-1;
17461 ret = NULL;
17462 break;
17465 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17466 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17467 gcc_assert (pos + fieldsize <= size);
17468 if (pos < offset)
17470 expansion_failed (loc, NULL_RTX,
17471 "out-of-order fields in record constructor");
17472 size = offset = (unsigned HOST_WIDE_INT)-1;
17473 ret = NULL;
17474 break;
17476 if (pos > offset)
17478 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17479 add_loc_descr (&ret, ret1);
17480 offset = pos;
17482 if (val && fieldsize != 0)
17484 ret1 = loc_descriptor_from_tree (val, want_address, context);
17485 if (!ret1)
17487 expansion_failed (loc, NULL_RTX,
17488 "unsupported expression in field");
17489 size = offset = (unsigned HOST_WIDE_INT)-1;
17490 ret = NULL;
17491 break;
17493 add_loc_descr (&ret, ret1);
17495 if (fieldsize)
17497 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17498 add_loc_descr (&ret, ret1);
17499 offset = pos + fieldsize;
17503 if (offset != size)
17505 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17506 add_loc_descr (&ret, ret1);
17507 offset = size;
17510 have_address = !!want_address;
17512 else
17513 expansion_failed (loc, NULL_RTX,
17514 "constructor of non-record type");
17516 else
17517 /* We can construct small constants here using int_loc_descriptor. */
17518 expansion_failed (loc, NULL_RTX,
17519 "constructor or constant not in constant pool");
17520 break;
17522 case TRUTH_AND_EXPR:
17523 case TRUTH_ANDIF_EXPR:
17524 case BIT_AND_EXPR:
17525 op = DW_OP_and;
17526 goto do_binop;
17528 case TRUTH_XOR_EXPR:
17529 case BIT_XOR_EXPR:
17530 op = DW_OP_xor;
17531 goto do_binop;
17533 case TRUTH_OR_EXPR:
17534 case TRUTH_ORIF_EXPR:
17535 case BIT_IOR_EXPR:
17536 op = DW_OP_or;
17537 goto do_binop;
17539 case FLOOR_DIV_EXPR:
17540 case CEIL_DIV_EXPR:
17541 case ROUND_DIV_EXPR:
17542 case TRUNC_DIV_EXPR:
17543 case EXACT_DIV_EXPR:
17544 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17545 return 0;
17546 op = DW_OP_div;
17547 goto do_binop;
17549 case MINUS_EXPR:
17550 op = DW_OP_minus;
17551 goto do_binop;
17553 case FLOOR_MOD_EXPR:
17554 case CEIL_MOD_EXPR:
17555 case ROUND_MOD_EXPR:
17556 case TRUNC_MOD_EXPR:
17557 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17559 op = DW_OP_mod;
17560 goto do_binop;
17562 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17563 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17564 if (list_ret == 0 || list_ret1 == 0)
17565 return 0;
17567 add_loc_list (&list_ret, list_ret1);
17568 if (list_ret == 0)
17569 return 0;
17570 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17571 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17572 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17573 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17574 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17575 break;
17577 case MULT_EXPR:
17578 op = DW_OP_mul;
17579 goto do_binop;
17581 case LSHIFT_EXPR:
17582 op = DW_OP_shl;
17583 goto do_binop;
17585 case RSHIFT_EXPR:
17586 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17587 goto do_binop;
17589 case POINTER_PLUS_EXPR:
17590 case PLUS_EXPR:
17591 do_plus:
17592 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17594 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17595 smarter to encode their opposite. The DW_OP_plus_uconst operation
17596 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17597 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17598 bytes, Y being the size of the operation that pushes the opposite
17599 of the addend. So let's choose the smallest representation. */
17600 const tree tree_addend = TREE_OPERAND (loc, 1);
17601 offset_int wi_addend;
17602 HOST_WIDE_INT shwi_addend;
17603 dw_loc_descr_ref loc_naddend;
17605 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17606 if (list_ret == 0)
17607 return 0;
17609 /* Try to get the literal to push. It is the opposite of the addend,
17610 so as we rely on wrapping during DWARF evaluation, first decode
17611 the literal as a "DWARF-sized" signed number. */
17612 wi_addend = wi::to_offset (tree_addend);
17613 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17614 shwi_addend = wi_addend.to_shwi ();
17615 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17616 ? int_loc_descriptor (-shwi_addend)
17617 : NULL;
17619 if (loc_naddend != NULL
17620 && ((unsigned) size_of_uleb128 (shwi_addend)
17621 > size_of_loc_descr (loc_naddend)))
17623 add_loc_descr_to_each (list_ret, loc_naddend);
17624 add_loc_descr_to_each (list_ret,
17625 new_loc_descr (DW_OP_minus, 0, 0));
17627 else
17629 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17631 loc_naddend = loc_cur;
17632 loc_cur = loc_cur->dw_loc_next;
17633 ggc_free (loc_naddend);
17635 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17637 break;
17640 op = DW_OP_plus;
17641 goto do_binop;
17643 case LE_EXPR:
17644 op = DW_OP_le;
17645 goto do_comp_binop;
17647 case GE_EXPR:
17648 op = DW_OP_ge;
17649 goto do_comp_binop;
17651 case LT_EXPR:
17652 op = DW_OP_lt;
17653 goto do_comp_binop;
17655 case GT_EXPR:
17656 op = DW_OP_gt;
17657 goto do_comp_binop;
17659 do_comp_binop:
17660 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17662 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17663 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17664 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17665 TREE_CODE (loc));
17666 break;
17668 else
17669 goto do_binop;
17671 case EQ_EXPR:
17672 op = DW_OP_eq;
17673 goto do_binop;
17675 case NE_EXPR:
17676 op = DW_OP_ne;
17677 goto do_binop;
17679 do_binop:
17680 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17681 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17682 if (list_ret == 0 || list_ret1 == 0)
17683 return 0;
17685 add_loc_list (&list_ret, list_ret1);
17686 if (list_ret == 0)
17687 return 0;
17688 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17689 break;
17691 case TRUTH_NOT_EXPR:
17692 case BIT_NOT_EXPR:
17693 op = DW_OP_not;
17694 goto do_unop;
17696 case ABS_EXPR:
17697 op = DW_OP_abs;
17698 goto do_unop;
17700 case NEGATE_EXPR:
17701 op = DW_OP_neg;
17702 goto do_unop;
17704 do_unop:
17705 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17706 if (list_ret == 0)
17707 return 0;
17709 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17710 break;
17712 case MIN_EXPR:
17713 case MAX_EXPR:
17715 const enum tree_code code =
17716 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17718 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17719 build2 (code, integer_type_node,
17720 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17721 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17724 /* fall through */
17726 case COND_EXPR:
17728 dw_loc_descr_ref lhs
17729 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17730 dw_loc_list_ref rhs
17731 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17732 dw_loc_descr_ref bra_node, jump_node, tmp;
17734 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17735 if (list_ret == 0 || lhs == 0 || rhs == 0)
17736 return 0;
17738 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17739 add_loc_descr_to_each (list_ret, bra_node);
17741 add_loc_list (&list_ret, rhs);
17742 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17743 add_loc_descr_to_each (list_ret, jump_node);
17745 add_loc_descr_to_each (list_ret, lhs);
17746 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17747 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17749 /* ??? Need a node to point the skip at. Use a nop. */
17750 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17751 add_loc_descr_to_each (list_ret, tmp);
17752 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17753 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17755 break;
17757 case FIX_TRUNC_EXPR:
17758 return 0;
17760 default:
17761 /* Leave front-end specific codes as simply unknown. This comes
17762 up, for instance, with the C STMT_EXPR. */
17763 if ((unsigned int) TREE_CODE (loc)
17764 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17766 expansion_failed (loc, NULL_RTX,
17767 "language specific tree node");
17768 return 0;
17771 /* Otherwise this is a generic code; we should just lists all of
17772 these explicitly. We forgot one. */
17773 if (flag_checking)
17774 gcc_unreachable ();
17776 /* In a release build, we want to degrade gracefully: better to
17777 generate incomplete debugging information than to crash. */
17778 return NULL;
17781 if (!ret && !list_ret)
17782 return 0;
17784 if (want_address == 2 && !have_address
17785 && (dwarf_version >= 4 || !dwarf_strict))
17787 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17789 expansion_failed (loc, NULL_RTX,
17790 "DWARF address size mismatch");
17791 return 0;
17793 if (ret)
17794 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17795 else
17796 add_loc_descr_to_each (list_ret,
17797 new_loc_descr (DW_OP_stack_value, 0, 0));
17798 have_address = 1;
17800 /* Show if we can't fill the request for an address. */
17801 if (want_address && !have_address)
17803 expansion_failed (loc, NULL_RTX,
17804 "Want address and only have value");
17805 return 0;
17808 gcc_assert (!ret || !list_ret);
17810 /* If we've got an address and don't want one, dereference. */
17811 if (!want_address && have_address)
17813 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17815 if (size > DWARF2_ADDR_SIZE || size == -1)
17817 expansion_failed (loc, NULL_RTX,
17818 "DWARF address size mismatch");
17819 return 0;
17821 else if (size == DWARF2_ADDR_SIZE)
17822 op = DW_OP_deref;
17823 else
17824 op = DW_OP_deref_size;
17826 if (ret)
17827 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17828 else
17829 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17831 if (ret)
17832 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17834 return list_ret;
17837 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17838 expressions. */
17840 static dw_loc_list_ref
17841 loc_list_from_tree (tree loc, int want_address,
17842 struct loc_descr_context *context)
17844 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17846 for (dw_loc_list_ref loc_cur = result;
17847 loc_cur != NULL; loc_cur =
17848 loc_cur->dw_loc_next)
17849 loc_descr_without_nops (loc_cur->expr);
17850 return result;
17853 /* Same as above but return only single location expression. */
17854 static dw_loc_descr_ref
17855 loc_descriptor_from_tree (tree loc, int want_address,
17856 struct loc_descr_context *context)
17858 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17859 if (!ret)
17860 return NULL;
17861 if (ret->dw_loc_next)
17863 expansion_failed (loc, NULL_RTX,
17864 "Location list where only loc descriptor needed");
17865 return NULL;
17867 return ret->expr;
17870 /* Given a value, round it up to the lowest multiple of `boundary'
17871 which is not less than the value itself. */
17873 static inline HOST_WIDE_INT
17874 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17876 return (((value + boundary - 1) / boundary) * boundary);
17879 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17880 pointer to the declared type for the relevant field variable, or return
17881 `integer_type_node' if the given node turns out to be an
17882 ERROR_MARK node. */
17884 static inline tree
17885 field_type (const_tree decl)
17887 tree type;
17889 if (TREE_CODE (decl) == ERROR_MARK)
17890 return integer_type_node;
17892 type = DECL_BIT_FIELD_TYPE (decl);
17893 if (type == NULL_TREE)
17894 type = TREE_TYPE (decl);
17896 return type;
17899 /* Given a pointer to a tree node, return the alignment in bits for
17900 it, or else return BITS_PER_WORD if the node actually turns out to
17901 be an ERROR_MARK node. */
17903 static inline unsigned
17904 simple_type_align_in_bits (const_tree type)
17906 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
17909 static inline unsigned
17910 simple_decl_align_in_bits (const_tree decl)
17912 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
17915 /* Return the result of rounding T up to ALIGN. */
17917 static inline offset_int
17918 round_up_to_align (const offset_int &t, unsigned int align)
17920 return wi::udiv_trunc (t + align - 1, align) * align;
17923 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
17924 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
17925 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
17926 if we fail to return the size in one of these two forms. */
17928 static dw_loc_descr_ref
17929 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
17931 tree tree_size;
17932 struct loc_descr_context ctx;
17934 /* Return a constant integer in priority, if possible. */
17935 *cst_size = int_size_in_bytes (type);
17936 if (*cst_size != -1)
17937 return NULL;
17939 ctx.context_type = const_cast<tree> (type);
17940 ctx.base_decl = NULL_TREE;
17941 ctx.dpi = NULL;
17942 ctx.placeholder_arg = false;
17943 ctx.placeholder_seen = false;
17945 type = TYPE_MAIN_VARIANT (type);
17946 tree_size = TYPE_SIZE_UNIT (type);
17947 return ((tree_size != NULL_TREE)
17948 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
17949 : NULL);
17952 /* Helper structure for RECORD_TYPE processing. */
17953 struct vlr_context
17955 /* Root RECORD_TYPE. It is needed to generate data member location
17956 descriptions in variable-length records (VLR), but also to cope with
17957 variants, which are composed of nested structures multiplexed with
17958 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
17959 function processing a FIELD_DECL, it is required to be non null. */
17960 tree struct_type;
17961 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
17962 QUAL_UNION_TYPE), this holds an expression that computes the offset for
17963 this variant part as part of the root record (in storage units). For
17964 regular records, it must be NULL_TREE. */
17965 tree variant_part_offset;
17968 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
17969 addressed byte of the "containing object" for the given FIELD_DECL. If
17970 possible, return a native constant through CST_OFFSET (in which case NULL is
17971 returned); otherwise return a DWARF expression that computes the offset.
17973 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
17974 that offset is, either because the argument turns out to be a pointer to an
17975 ERROR_MARK node, or because the offset expression is too complex for us.
17977 CTX is required: see the comment for VLR_CONTEXT. */
17979 static dw_loc_descr_ref
17980 field_byte_offset (const_tree decl, struct vlr_context *ctx,
17981 HOST_WIDE_INT *cst_offset)
17983 tree tree_result;
17984 dw_loc_list_ref loc_result;
17986 *cst_offset = 0;
17988 if (TREE_CODE (decl) == ERROR_MARK)
17989 return NULL;
17990 else
17991 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
17993 /* We cannot handle variable bit offsets at the moment, so abort if it's the
17994 case. */
17995 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
17996 return NULL;
17998 #ifdef PCC_BITFIELD_TYPE_MATTERS
17999 /* We used to handle only constant offsets in all cases. Now, we handle
18000 properly dynamic byte offsets only when PCC bitfield type doesn't
18001 matter. */
18002 if (PCC_BITFIELD_TYPE_MATTERS
18003 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18005 offset_int object_offset_in_bits;
18006 offset_int object_offset_in_bytes;
18007 offset_int bitpos_int;
18008 tree type;
18009 tree field_size_tree;
18010 offset_int deepest_bitpos;
18011 offset_int field_size_in_bits;
18012 unsigned int type_align_in_bits;
18013 unsigned int decl_align_in_bits;
18014 offset_int type_size_in_bits;
18016 bitpos_int = wi::to_offset (bit_position (decl));
18017 type = field_type (decl);
18018 type_size_in_bits = offset_int_type_size_in_bits (type);
18019 type_align_in_bits = simple_type_align_in_bits (type);
18021 field_size_tree = DECL_SIZE (decl);
18023 /* The size could be unspecified if there was an error, or for
18024 a flexible array member. */
18025 if (!field_size_tree)
18026 field_size_tree = bitsize_zero_node;
18028 /* If the size of the field is not constant, use the type size. */
18029 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18030 field_size_in_bits = wi::to_offset (field_size_tree);
18031 else
18032 field_size_in_bits = type_size_in_bits;
18034 decl_align_in_bits = simple_decl_align_in_bits (decl);
18036 /* The GCC front-end doesn't make any attempt to keep track of the
18037 starting bit offset (relative to the start of the containing
18038 structure type) of the hypothetical "containing object" for a
18039 bit-field. Thus, when computing the byte offset value for the
18040 start of the "containing object" of a bit-field, we must deduce
18041 this information on our own. This can be rather tricky to do in
18042 some cases. For example, handling the following structure type
18043 definition when compiling for an i386/i486 target (which only
18044 aligns long long's to 32-bit boundaries) can be very tricky:
18046 struct S { int field1; long long field2:31; };
18048 Fortunately, there is a simple rule-of-thumb which can be used
18049 in such cases. When compiling for an i386/i486, GCC will
18050 allocate 8 bytes for the structure shown above. It decides to
18051 do this based upon one simple rule for bit-field allocation.
18052 GCC allocates each "containing object" for each bit-field at
18053 the first (i.e. lowest addressed) legitimate alignment boundary
18054 (based upon the required minimum alignment for the declared
18055 type of the field) which it can possibly use, subject to the
18056 condition that there is still enough available space remaining
18057 in the containing object (when allocated at the selected point)
18058 to fully accommodate all of the bits of the bit-field itself.
18060 This simple rule makes it obvious why GCC allocates 8 bytes for
18061 each object of the structure type shown above. When looking
18062 for a place to allocate the "containing object" for `field2',
18063 the compiler simply tries to allocate a 64-bit "containing
18064 object" at each successive 32-bit boundary (starting at zero)
18065 until it finds a place to allocate that 64- bit field such that
18066 at least 31 contiguous (and previously unallocated) bits remain
18067 within that selected 64 bit field. (As it turns out, for the
18068 example above, the compiler finds it is OK to allocate the
18069 "containing object" 64-bit field at bit-offset zero within the
18070 structure type.)
18072 Here we attempt to work backwards from the limited set of facts
18073 we're given, and we try to deduce from those facts, where GCC
18074 must have believed that the containing object started (within
18075 the structure type). The value we deduce is then used (by the
18076 callers of this routine) to generate DW_AT_location and
18077 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18078 the case of DW_AT_location, regular fields as well). */
18080 /* Figure out the bit-distance from the start of the structure to
18081 the "deepest" bit of the bit-field. */
18082 deepest_bitpos = bitpos_int + field_size_in_bits;
18084 /* This is the tricky part. Use some fancy footwork to deduce
18085 where the lowest addressed bit of the containing object must
18086 be. */
18087 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18089 /* Round up to type_align by default. This works best for
18090 bitfields. */
18091 object_offset_in_bits
18092 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18094 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18096 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18098 /* Round up to decl_align instead. */
18099 object_offset_in_bits
18100 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18103 object_offset_in_bytes
18104 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18105 if (ctx->variant_part_offset == NULL_TREE)
18107 *cst_offset = object_offset_in_bytes.to_shwi ();
18108 return NULL;
18110 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18112 else
18113 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18114 tree_result = byte_position (decl);
18116 if (ctx->variant_part_offset != NULL_TREE)
18117 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18118 ctx->variant_part_offset, tree_result);
18120 /* If the byte offset is a constant, it's simplier to handle a native
18121 constant rather than a DWARF expression. */
18122 if (TREE_CODE (tree_result) == INTEGER_CST)
18124 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18125 return NULL;
18127 struct loc_descr_context loc_ctx = {
18128 ctx->struct_type, /* context_type */
18129 NULL_TREE, /* base_decl */
18130 NULL, /* dpi */
18131 false, /* placeholder_arg */
18132 false /* placeholder_seen */
18134 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18136 /* We want a DWARF expression: abort if we only have a location list with
18137 multiple elements. */
18138 if (!loc_result || !single_element_loc_list_p (loc_result))
18139 return NULL;
18140 else
18141 return loc_result->expr;
18144 /* The following routines define various Dwarf attributes and any data
18145 associated with them. */
18147 /* Add a location description attribute value to a DIE.
18149 This emits location attributes suitable for whole variables and
18150 whole parameters. Note that the location attributes for struct fields are
18151 generated by the routine `data_member_location_attribute' below. */
18153 static inline void
18154 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18155 dw_loc_list_ref descr)
18157 if (descr == 0)
18158 return;
18159 if (single_element_loc_list_p (descr))
18160 add_AT_loc (die, attr_kind, descr->expr);
18161 else
18162 add_AT_loc_list (die, attr_kind, descr);
18165 /* Add DW_AT_accessibility attribute to DIE if needed. */
18167 static void
18168 add_accessibility_attribute (dw_die_ref die, tree decl)
18170 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18171 children, otherwise the default is DW_ACCESS_public. In DWARF2
18172 the default has always been DW_ACCESS_public. */
18173 if (TREE_PROTECTED (decl))
18174 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18175 else if (TREE_PRIVATE (decl))
18177 if (dwarf_version == 2
18178 || die->die_parent == NULL
18179 || die->die_parent->die_tag != DW_TAG_class_type)
18180 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18182 else if (dwarf_version > 2
18183 && die->die_parent
18184 && die->die_parent->die_tag == DW_TAG_class_type)
18185 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18188 /* Attach the specialized form of location attribute used for data members of
18189 struct and union types. In the special case of a FIELD_DECL node which
18190 represents a bit-field, the "offset" part of this special location
18191 descriptor must indicate the distance in bytes from the lowest-addressed
18192 byte of the containing struct or union type to the lowest-addressed byte of
18193 the "containing object" for the bit-field. (See the `field_byte_offset'
18194 function above).
18196 For any given bit-field, the "containing object" is a hypothetical object
18197 (of some integral or enum type) within which the given bit-field lives. The
18198 type of this hypothetical "containing object" is always the same as the
18199 declared type of the individual bit-field itself (for GCC anyway... the
18200 DWARF spec doesn't actually mandate this). Note that it is the size (in
18201 bytes) of the hypothetical "containing object" which will be given in the
18202 DW_AT_byte_size attribute for this bit-field. (See the
18203 `byte_size_attribute' function below.) It is also used when calculating the
18204 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18205 function below.)
18207 CTX is required: see the comment for VLR_CONTEXT. */
18209 static void
18210 add_data_member_location_attribute (dw_die_ref die,
18211 tree decl,
18212 struct vlr_context *ctx)
18214 HOST_WIDE_INT offset;
18215 dw_loc_descr_ref loc_descr = 0;
18217 if (TREE_CODE (decl) == TREE_BINFO)
18219 /* We're working on the TAG_inheritance for a base class. */
18220 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18222 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18223 aren't at a fixed offset from all (sub)objects of the same
18224 type. We need to extract the appropriate offset from our
18225 vtable. The following dwarf expression means
18227 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18229 This is specific to the V3 ABI, of course. */
18231 dw_loc_descr_ref tmp;
18233 /* Make a copy of the object address. */
18234 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18235 add_loc_descr (&loc_descr, tmp);
18237 /* Extract the vtable address. */
18238 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18239 add_loc_descr (&loc_descr, tmp);
18241 /* Calculate the address of the offset. */
18242 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18243 gcc_assert (offset < 0);
18245 tmp = int_loc_descriptor (-offset);
18246 add_loc_descr (&loc_descr, tmp);
18247 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18248 add_loc_descr (&loc_descr, tmp);
18250 /* Extract the offset. */
18251 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18252 add_loc_descr (&loc_descr, tmp);
18254 /* Add it to the object address. */
18255 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18256 add_loc_descr (&loc_descr, tmp);
18258 else
18259 offset = tree_to_shwi (BINFO_OFFSET (decl));
18261 else
18263 loc_descr = field_byte_offset (decl, ctx, &offset);
18265 /* If loc_descr is available then we know the field offset is dynamic.
18266 However, GDB does not handle dynamic field offsets very well at the
18267 moment. */
18268 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18270 loc_descr = NULL;
18271 offset = 0;
18274 /* Data member location evalutation starts with the base address on the
18275 stack. Compute the field offset and add it to this base address. */
18276 else if (loc_descr != NULL)
18277 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18280 if (! loc_descr)
18282 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18283 e.g. GDB only added support to it in November 2016. For DWARF5
18284 we need newer debug info consumers anyway. We might change this
18285 to dwarf_version >= 4 once most consumers catched up. */
18286 if (dwarf_version >= 5
18287 && TREE_CODE (decl) == FIELD_DECL
18288 && DECL_BIT_FIELD_TYPE (decl))
18290 tree off = bit_position (decl);
18291 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18293 remove_AT (die, DW_AT_byte_size);
18294 remove_AT (die, DW_AT_bit_offset);
18295 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18296 return;
18299 if (dwarf_version > 2)
18301 /* Don't need to output a location expression, just the constant. */
18302 if (offset < 0)
18303 add_AT_int (die, DW_AT_data_member_location, offset);
18304 else
18305 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18306 return;
18308 else
18310 enum dwarf_location_atom op;
18312 /* The DWARF2 standard says that we should assume that the structure
18313 address is already on the stack, so we can specify a structure
18314 field address by using DW_OP_plus_uconst. */
18315 op = DW_OP_plus_uconst;
18316 loc_descr = new_loc_descr (op, offset, 0);
18320 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18323 /* Writes integer values to dw_vec_const array. */
18325 static void
18326 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18328 while (size != 0)
18330 *dest++ = val & 0xff;
18331 val >>= 8;
18332 --size;
18336 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18338 static HOST_WIDE_INT
18339 extract_int (const unsigned char *src, unsigned int size)
18341 HOST_WIDE_INT val = 0;
18343 src += size;
18344 while (size != 0)
18346 val <<= 8;
18347 val |= *--src & 0xff;
18348 --size;
18350 return val;
18353 /* Writes wide_int values to dw_vec_const array. */
18355 static void
18356 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18358 int i;
18360 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18362 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18363 return;
18366 /* We'd have to extend this code to support odd sizes. */
18367 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18369 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18371 if (WORDS_BIG_ENDIAN)
18372 for (i = n - 1; i >= 0; i--)
18374 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18375 dest += sizeof (HOST_WIDE_INT);
18377 else
18378 for (i = 0; i < n; i++)
18380 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18381 dest += sizeof (HOST_WIDE_INT);
18385 /* Writes floating point values to dw_vec_const array. */
18387 static void
18388 insert_float (const_rtx rtl, unsigned char *array)
18390 long val[4];
18391 int i;
18393 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18395 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18396 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18398 insert_int (val[i], 4, array);
18399 array += 4;
18403 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18404 does not have a "location" either in memory or in a register. These
18405 things can arise in GNU C when a constant is passed as an actual parameter
18406 to an inlined function. They can also arise in C++ where declared
18407 constants do not necessarily get memory "homes". */
18409 static bool
18410 add_const_value_attribute (dw_die_ref die, rtx rtl)
18412 switch (GET_CODE (rtl))
18414 case CONST_INT:
18416 HOST_WIDE_INT val = INTVAL (rtl);
18418 if (val < 0)
18419 add_AT_int (die, DW_AT_const_value, val);
18420 else
18421 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18423 return true;
18425 case CONST_WIDE_INT:
18427 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18428 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18429 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18430 wide_int w = wi::zext (w1, prec);
18431 add_AT_wide (die, DW_AT_const_value, w);
18433 return true;
18435 case CONST_DOUBLE:
18436 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18437 floating-point constant. A CONST_DOUBLE is used whenever the
18438 constant requires more than one word in order to be adequately
18439 represented. */
18441 machine_mode mode = GET_MODE (rtl);
18443 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18444 add_AT_double (die, DW_AT_const_value,
18445 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18446 else
18448 unsigned int length = GET_MODE_SIZE (mode);
18449 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18451 insert_float (rtl, array);
18452 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18455 return true;
18457 case CONST_VECTOR:
18459 machine_mode mode = GET_MODE (rtl);
18460 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18461 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18462 unsigned char *array
18463 = ggc_vec_alloc<unsigned char> (length * elt_size);
18464 unsigned int i;
18465 unsigned char *p;
18466 machine_mode imode = GET_MODE_INNER (mode);
18468 switch (GET_MODE_CLASS (mode))
18470 case MODE_VECTOR_INT:
18471 for (i = 0, p = array; i < length; i++, p += elt_size)
18473 rtx elt = CONST_VECTOR_ELT (rtl, i);
18474 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18476 break;
18478 case MODE_VECTOR_FLOAT:
18479 for (i = 0, p = array; i < length; i++, p += elt_size)
18481 rtx elt = CONST_VECTOR_ELT (rtl, i);
18482 insert_float (elt, p);
18484 break;
18486 default:
18487 gcc_unreachable ();
18490 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18492 return true;
18494 case CONST_STRING:
18495 if (dwarf_version >= 4 || !dwarf_strict)
18497 dw_loc_descr_ref loc_result;
18498 resolve_one_addr (&rtl);
18499 rtl_addr:
18500 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18501 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18502 add_AT_loc (die, DW_AT_location, loc_result);
18503 vec_safe_push (used_rtx_array, rtl);
18504 return true;
18506 return false;
18508 case CONST:
18509 if (CONSTANT_P (XEXP (rtl, 0)))
18510 return add_const_value_attribute (die, XEXP (rtl, 0));
18511 /* FALLTHROUGH */
18512 case SYMBOL_REF:
18513 if (!const_ok_for_output (rtl))
18514 return false;
18515 /* FALLTHROUGH */
18516 case LABEL_REF:
18517 if (dwarf_version >= 4 || !dwarf_strict)
18518 goto rtl_addr;
18519 return false;
18521 case PLUS:
18522 /* In cases where an inlined instance of an inline function is passed
18523 the address of an `auto' variable (which is local to the caller) we
18524 can get a situation where the DECL_RTL of the artificial local
18525 variable (for the inlining) which acts as a stand-in for the
18526 corresponding formal parameter (of the inline function) will look
18527 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18528 exactly a compile-time constant expression, but it isn't the address
18529 of the (artificial) local variable either. Rather, it represents the
18530 *value* which the artificial local variable always has during its
18531 lifetime. We currently have no way to represent such quasi-constant
18532 values in Dwarf, so for now we just punt and generate nothing. */
18533 return false;
18535 case HIGH:
18536 case CONST_FIXED:
18537 return false;
18539 case MEM:
18540 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18541 && MEM_READONLY_P (rtl)
18542 && GET_MODE (rtl) == BLKmode)
18544 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18545 return true;
18547 return false;
18549 default:
18550 /* No other kinds of rtx should be possible here. */
18551 gcc_unreachable ();
18553 return false;
18556 /* Determine whether the evaluation of EXPR references any variables
18557 or functions which aren't otherwise used (and therefore may not be
18558 output). */
18559 static tree
18560 reference_to_unused (tree * tp, int * walk_subtrees,
18561 void * data ATTRIBUTE_UNUSED)
18563 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18564 *walk_subtrees = 0;
18566 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18567 && ! TREE_ASM_WRITTEN (*tp))
18568 return *tp;
18569 /* ??? The C++ FE emits debug information for using decls, so
18570 putting gcc_unreachable here falls over. See PR31899. For now
18571 be conservative. */
18572 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18573 return *tp;
18574 else if (VAR_P (*tp))
18576 varpool_node *node = varpool_node::get (*tp);
18577 if (!node || !node->definition)
18578 return *tp;
18580 else if (TREE_CODE (*tp) == FUNCTION_DECL
18581 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18583 /* The call graph machinery must have finished analyzing,
18584 optimizing and gimplifying the CU by now.
18585 So if *TP has no call graph node associated
18586 to it, it means *TP will not be emitted. */
18587 if (!cgraph_node::get (*tp))
18588 return *tp;
18590 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18591 return *tp;
18593 return NULL_TREE;
18596 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18597 for use in a later add_const_value_attribute call. */
18599 static rtx
18600 rtl_for_decl_init (tree init, tree type)
18602 rtx rtl = NULL_RTX;
18604 STRIP_NOPS (init);
18606 /* If a variable is initialized with a string constant without embedded
18607 zeros, build CONST_STRING. */
18608 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18610 tree enttype = TREE_TYPE (type);
18611 tree domain = TYPE_DOMAIN (type);
18612 machine_mode mode = TYPE_MODE (enttype);
18614 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18615 && domain
18616 && integer_zerop (TYPE_MIN_VALUE (domain))
18617 && compare_tree_int (TYPE_MAX_VALUE (domain),
18618 TREE_STRING_LENGTH (init) - 1) == 0
18619 && ((size_t) TREE_STRING_LENGTH (init)
18620 == strlen (TREE_STRING_POINTER (init)) + 1))
18622 rtl = gen_rtx_CONST_STRING (VOIDmode,
18623 ggc_strdup (TREE_STRING_POINTER (init)));
18624 rtl = gen_rtx_MEM (BLKmode, rtl);
18625 MEM_READONLY_P (rtl) = 1;
18628 /* Other aggregates, and complex values, could be represented using
18629 CONCAT: FIXME! */
18630 else if (AGGREGATE_TYPE_P (type)
18631 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18632 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18633 || TREE_CODE (type) == COMPLEX_TYPE)
18635 /* Vectors only work if their mode is supported by the target.
18636 FIXME: generic vectors ought to work too. */
18637 else if (TREE_CODE (type) == VECTOR_TYPE
18638 && !VECTOR_MODE_P (TYPE_MODE (type)))
18640 /* If the initializer is something that we know will expand into an
18641 immediate RTL constant, expand it now. We must be careful not to
18642 reference variables which won't be output. */
18643 else if (initializer_constant_valid_p (init, type)
18644 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18646 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18647 possible. */
18648 if (TREE_CODE (type) == VECTOR_TYPE)
18649 switch (TREE_CODE (init))
18651 case VECTOR_CST:
18652 break;
18653 case CONSTRUCTOR:
18654 if (TREE_CONSTANT (init))
18656 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18657 bool constant_p = true;
18658 tree value;
18659 unsigned HOST_WIDE_INT ix;
18661 /* Even when ctor is constant, it might contain non-*_CST
18662 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18663 belong into VECTOR_CST nodes. */
18664 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18665 if (!CONSTANT_CLASS_P (value))
18667 constant_p = false;
18668 break;
18671 if (constant_p)
18673 init = build_vector_from_ctor (type, elts);
18674 break;
18677 /* FALLTHRU */
18679 default:
18680 return NULL;
18683 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18685 /* If expand_expr returns a MEM, it wasn't immediate. */
18686 gcc_assert (!rtl || !MEM_P (rtl));
18689 return rtl;
18692 /* Generate RTL for the variable DECL to represent its location. */
18694 static rtx
18695 rtl_for_decl_location (tree decl)
18697 rtx rtl;
18699 /* Here we have to decide where we are going to say the parameter "lives"
18700 (as far as the debugger is concerned). We only have a couple of
18701 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18703 DECL_RTL normally indicates where the parameter lives during most of the
18704 activation of the function. If optimization is enabled however, this
18705 could be either NULL or else a pseudo-reg. Both of those cases indicate
18706 that the parameter doesn't really live anywhere (as far as the code
18707 generation parts of GCC are concerned) during most of the function's
18708 activation. That will happen (for example) if the parameter is never
18709 referenced within the function.
18711 We could just generate a location descriptor here for all non-NULL
18712 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18713 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18714 where DECL_RTL is NULL or is a pseudo-reg.
18716 Note however that we can only get away with using DECL_INCOMING_RTL as
18717 a backup substitute for DECL_RTL in certain limited cases. In cases
18718 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18719 we can be sure that the parameter was passed using the same type as it is
18720 declared to have within the function, and that its DECL_INCOMING_RTL
18721 points us to a place where a value of that type is passed.
18723 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18724 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18725 because in these cases DECL_INCOMING_RTL points us to a value of some
18726 type which is *different* from the type of the parameter itself. Thus,
18727 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18728 such cases, the debugger would end up (for example) trying to fetch a
18729 `float' from a place which actually contains the first part of a
18730 `double'. That would lead to really incorrect and confusing
18731 output at debug-time.
18733 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18734 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18735 are a couple of exceptions however. On little-endian machines we can
18736 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18737 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18738 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18739 when (on a little-endian machine) a non-prototyped function has a
18740 parameter declared to be of type `short' or `char'. In such cases,
18741 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18742 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18743 passed `int' value. If the debugger then uses that address to fetch
18744 a `short' or a `char' (on a little-endian machine) the result will be
18745 the correct data, so we allow for such exceptional cases below.
18747 Note that our goal here is to describe the place where the given formal
18748 parameter lives during most of the function's activation (i.e. between the
18749 end of the prologue and the start of the epilogue). We'll do that as best
18750 as we can. Note however that if the given formal parameter is modified
18751 sometime during the execution of the function, then a stack backtrace (at
18752 debug-time) will show the function as having been called with the *new*
18753 value rather than the value which was originally passed in. This happens
18754 rarely enough that it is not a major problem, but it *is* a problem, and
18755 I'd like to fix it.
18757 A future version of dwarf2out.c may generate two additional attributes for
18758 any given DW_TAG_formal_parameter DIE which will describe the "passed
18759 type" and the "passed location" for the given formal parameter in addition
18760 to the attributes we now generate to indicate the "declared type" and the
18761 "active location" for each parameter. This additional set of attributes
18762 could be used by debuggers for stack backtraces. Separately, note that
18763 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18764 This happens (for example) for inlined-instances of inline function formal
18765 parameters which are never referenced. This really shouldn't be
18766 happening. All PARM_DECL nodes should get valid non-NULL
18767 DECL_INCOMING_RTL values. FIXME. */
18769 /* Use DECL_RTL as the "location" unless we find something better. */
18770 rtl = DECL_RTL_IF_SET (decl);
18772 /* When generating abstract instances, ignore everything except
18773 constants, symbols living in memory, and symbols living in
18774 fixed registers. */
18775 if (! reload_completed)
18777 if (rtl
18778 && (CONSTANT_P (rtl)
18779 || (MEM_P (rtl)
18780 && CONSTANT_P (XEXP (rtl, 0)))
18781 || (REG_P (rtl)
18782 && VAR_P (decl)
18783 && TREE_STATIC (decl))))
18785 rtl = targetm.delegitimize_address (rtl);
18786 return rtl;
18788 rtl = NULL_RTX;
18790 else if (TREE_CODE (decl) == PARM_DECL)
18792 if (rtl == NULL_RTX
18793 || is_pseudo_reg (rtl)
18794 || (MEM_P (rtl)
18795 && is_pseudo_reg (XEXP (rtl, 0))
18796 && DECL_INCOMING_RTL (decl)
18797 && MEM_P (DECL_INCOMING_RTL (decl))
18798 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18800 tree declared_type = TREE_TYPE (decl);
18801 tree passed_type = DECL_ARG_TYPE (decl);
18802 machine_mode dmode = TYPE_MODE (declared_type);
18803 machine_mode pmode = TYPE_MODE (passed_type);
18805 /* This decl represents a formal parameter which was optimized out.
18806 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18807 all cases where (rtl == NULL_RTX) just below. */
18808 if (dmode == pmode)
18809 rtl = DECL_INCOMING_RTL (decl);
18810 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18811 && SCALAR_INT_MODE_P (dmode)
18812 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18813 && DECL_INCOMING_RTL (decl))
18815 rtx inc = DECL_INCOMING_RTL (decl);
18816 if (REG_P (inc))
18817 rtl = inc;
18818 else if (MEM_P (inc))
18820 if (BYTES_BIG_ENDIAN)
18821 rtl = adjust_address_nv (inc, dmode,
18822 GET_MODE_SIZE (pmode)
18823 - GET_MODE_SIZE (dmode));
18824 else
18825 rtl = inc;
18830 /* If the parm was passed in registers, but lives on the stack, then
18831 make a big endian correction if the mode of the type of the
18832 parameter is not the same as the mode of the rtl. */
18833 /* ??? This is the same series of checks that are made in dbxout.c before
18834 we reach the big endian correction code there. It isn't clear if all
18835 of these checks are necessary here, but keeping them all is the safe
18836 thing to do. */
18837 else if (MEM_P (rtl)
18838 && XEXP (rtl, 0) != const0_rtx
18839 && ! CONSTANT_P (XEXP (rtl, 0))
18840 /* Not passed in memory. */
18841 && !MEM_P (DECL_INCOMING_RTL (decl))
18842 /* Not passed by invisible reference. */
18843 && (!REG_P (XEXP (rtl, 0))
18844 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18845 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18846 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18847 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18848 #endif
18850 /* Big endian correction check. */
18851 && BYTES_BIG_ENDIAN
18852 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18853 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18854 < UNITS_PER_WORD))
18856 machine_mode addr_mode = get_address_mode (rtl);
18857 int offset = (UNITS_PER_WORD
18858 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18860 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18861 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18864 else if (VAR_P (decl)
18865 && rtl
18866 && MEM_P (rtl)
18867 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18868 && BYTES_BIG_ENDIAN)
18870 machine_mode addr_mode = get_address_mode (rtl);
18871 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18872 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18874 /* If a variable is declared "register" yet is smaller than
18875 a register, then if we store the variable to memory, it
18876 looks like we're storing a register-sized value, when in
18877 fact we are not. We need to adjust the offset of the
18878 storage location to reflect the actual value's bytes,
18879 else gdb will not be able to display it. */
18880 if (rsize > dsize)
18881 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18882 plus_constant (addr_mode, XEXP (rtl, 0),
18883 rsize - dsize));
18886 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
18887 and will have been substituted directly into all expressions that use it.
18888 C does not have such a concept, but C++ and other languages do. */
18889 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
18890 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
18892 if (rtl)
18893 rtl = targetm.delegitimize_address (rtl);
18895 /* If we don't look past the constant pool, we risk emitting a
18896 reference to a constant pool entry that isn't referenced from
18897 code, and thus is not emitted. */
18898 if (rtl)
18899 rtl = avoid_constant_pool_reference (rtl);
18901 /* Try harder to get a rtl. If this symbol ends up not being emitted
18902 in the current CU, resolve_addr will remove the expression referencing
18903 it. */
18904 if (rtl == NULL_RTX
18905 && VAR_P (decl)
18906 && !DECL_EXTERNAL (decl)
18907 && TREE_STATIC (decl)
18908 && DECL_NAME (decl)
18909 && !DECL_HARD_REGISTER (decl)
18910 && DECL_MODE (decl) != VOIDmode)
18912 rtl = make_decl_rtl_for_debug (decl);
18913 if (!MEM_P (rtl)
18914 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
18915 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
18916 rtl = NULL_RTX;
18919 return rtl;
18922 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
18923 returned. If so, the decl for the COMMON block is returned, and the
18924 value is the offset into the common block for the symbol. */
18926 static tree
18927 fortran_common (tree decl, HOST_WIDE_INT *value)
18929 tree val_expr, cvar;
18930 machine_mode mode;
18931 HOST_WIDE_INT bitsize, bitpos;
18932 tree offset;
18933 int unsignedp, reversep, volatilep = 0;
18935 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
18936 it does not have a value (the offset into the common area), or if it
18937 is thread local (as opposed to global) then it isn't common, and shouldn't
18938 be handled as such. */
18939 if (!VAR_P (decl)
18940 || !TREE_STATIC (decl)
18941 || !DECL_HAS_VALUE_EXPR_P (decl)
18942 || !is_fortran ())
18943 return NULL_TREE;
18945 val_expr = DECL_VALUE_EXPR (decl);
18946 if (TREE_CODE (val_expr) != COMPONENT_REF)
18947 return NULL_TREE;
18949 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
18950 &unsignedp, &reversep, &volatilep);
18952 if (cvar == NULL_TREE
18953 || !VAR_P (cvar)
18954 || DECL_ARTIFICIAL (cvar)
18955 || !TREE_PUBLIC (cvar))
18956 return NULL_TREE;
18958 *value = 0;
18959 if (offset != NULL)
18961 if (!tree_fits_shwi_p (offset))
18962 return NULL_TREE;
18963 *value = tree_to_shwi (offset);
18965 if (bitpos != 0)
18966 *value += bitpos / BITS_PER_UNIT;
18968 return cvar;
18971 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
18972 data attribute for a variable or a parameter. We generate the
18973 DW_AT_const_value attribute only in those cases where the given variable
18974 or parameter does not have a true "location" either in memory or in a
18975 register. This can happen (for example) when a constant is passed as an
18976 actual argument in a call to an inline function. (It's possible that
18977 these things can crop up in other ways also.) Note that one type of
18978 constant value which can be passed into an inlined function is a constant
18979 pointer. This can happen for example if an actual argument in an inlined
18980 function call evaluates to a compile-time constant address.
18982 CACHE_P is true if it is worth caching the location list for DECL,
18983 so that future calls can reuse it rather than regenerate it from scratch.
18984 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
18985 since we will need to refer to them each time the function is inlined. */
18987 static bool
18988 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
18990 rtx rtl;
18991 dw_loc_list_ref list;
18992 var_loc_list *loc_list;
18993 cached_dw_loc_list *cache;
18995 if (early_dwarf)
18996 return false;
18998 if (TREE_CODE (decl) == ERROR_MARK)
18999 return false;
19001 if (get_AT (die, DW_AT_location)
19002 || get_AT (die, DW_AT_const_value))
19003 return true;
19005 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19006 || TREE_CODE (decl) == RESULT_DECL);
19008 /* Try to get some constant RTL for this decl, and use that as the value of
19009 the location. */
19011 rtl = rtl_for_decl_location (decl);
19012 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19013 && add_const_value_attribute (die, rtl))
19014 return true;
19016 /* See if we have single element location list that is equivalent to
19017 a constant value. That way we are better to use add_const_value_attribute
19018 rather than expanding constant value equivalent. */
19019 loc_list = lookup_decl_loc (decl);
19020 if (loc_list
19021 && loc_list->first
19022 && loc_list->first->next == NULL
19023 && NOTE_P (loc_list->first->loc)
19024 && NOTE_VAR_LOCATION (loc_list->first->loc)
19025 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19027 struct var_loc_node *node;
19029 node = loc_list->first;
19030 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19031 if (GET_CODE (rtl) == EXPR_LIST)
19032 rtl = XEXP (rtl, 0);
19033 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19034 && add_const_value_attribute (die, rtl))
19035 return true;
19037 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19038 list several times. See if we've already cached the contents. */
19039 list = NULL;
19040 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19041 cache_p = false;
19042 if (cache_p)
19044 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19045 if (cache)
19046 list = cache->loc_list;
19048 if (list == NULL)
19050 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19051 NULL);
19052 /* It is usually worth caching this result if the decl is from
19053 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19054 if (cache_p && list && list->dw_loc_next)
19056 cached_dw_loc_list **slot
19057 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19058 DECL_UID (decl),
19059 INSERT);
19060 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19061 cache->decl_id = DECL_UID (decl);
19062 cache->loc_list = list;
19063 *slot = cache;
19066 if (list)
19068 add_AT_location_description (die, DW_AT_location, list);
19069 return true;
19071 /* None of that worked, so it must not really have a location;
19072 try adding a constant value attribute from the DECL_INITIAL. */
19073 return tree_add_const_value_attribute_for_decl (die, decl);
19076 /* Helper function for tree_add_const_value_attribute. Natively encode
19077 initializer INIT into an array. Return true if successful. */
19079 static bool
19080 native_encode_initializer (tree init, unsigned char *array, int size)
19082 tree type;
19084 if (init == NULL_TREE)
19085 return false;
19087 STRIP_NOPS (init);
19088 switch (TREE_CODE (init))
19090 case STRING_CST:
19091 type = TREE_TYPE (init);
19092 if (TREE_CODE (type) == ARRAY_TYPE)
19094 tree enttype = TREE_TYPE (type);
19095 machine_mode mode = TYPE_MODE (enttype);
19097 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19098 return false;
19099 if (int_size_in_bytes (type) != size)
19100 return false;
19101 if (size > TREE_STRING_LENGTH (init))
19103 memcpy (array, TREE_STRING_POINTER (init),
19104 TREE_STRING_LENGTH (init));
19105 memset (array + TREE_STRING_LENGTH (init),
19106 '\0', size - TREE_STRING_LENGTH (init));
19108 else
19109 memcpy (array, TREE_STRING_POINTER (init), size);
19110 return true;
19112 return false;
19113 case CONSTRUCTOR:
19114 type = TREE_TYPE (init);
19115 if (int_size_in_bytes (type) != size)
19116 return false;
19117 if (TREE_CODE (type) == ARRAY_TYPE)
19119 HOST_WIDE_INT min_index;
19120 unsigned HOST_WIDE_INT cnt;
19121 int curpos = 0, fieldsize;
19122 constructor_elt *ce;
19124 if (TYPE_DOMAIN (type) == NULL_TREE
19125 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19126 return false;
19128 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19129 if (fieldsize <= 0)
19130 return false;
19132 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19133 memset (array, '\0', size);
19134 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19136 tree val = ce->value;
19137 tree index = ce->index;
19138 int pos = curpos;
19139 if (index && TREE_CODE (index) == RANGE_EXPR)
19140 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19141 * fieldsize;
19142 else if (index)
19143 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19145 if (val)
19147 STRIP_NOPS (val);
19148 if (!native_encode_initializer (val, array + pos, fieldsize))
19149 return false;
19151 curpos = pos + fieldsize;
19152 if (index && TREE_CODE (index) == RANGE_EXPR)
19154 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19155 - tree_to_shwi (TREE_OPERAND (index, 0));
19156 while (count-- > 0)
19158 if (val)
19159 memcpy (array + curpos, array + pos, fieldsize);
19160 curpos += fieldsize;
19163 gcc_assert (curpos <= size);
19165 return true;
19167 else if (TREE_CODE (type) == RECORD_TYPE
19168 || TREE_CODE (type) == UNION_TYPE)
19170 tree field = NULL_TREE;
19171 unsigned HOST_WIDE_INT cnt;
19172 constructor_elt *ce;
19174 if (int_size_in_bytes (type) != size)
19175 return false;
19177 if (TREE_CODE (type) == RECORD_TYPE)
19178 field = TYPE_FIELDS (type);
19180 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19182 tree val = ce->value;
19183 int pos, fieldsize;
19185 if (ce->index != 0)
19186 field = ce->index;
19188 if (val)
19189 STRIP_NOPS (val);
19191 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19192 return false;
19194 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19195 && TYPE_DOMAIN (TREE_TYPE (field))
19196 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19197 return false;
19198 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19199 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19200 return false;
19201 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19202 pos = int_byte_position (field);
19203 gcc_assert (pos + fieldsize <= size);
19204 if (val && fieldsize != 0
19205 && !native_encode_initializer (val, array + pos, fieldsize))
19206 return false;
19208 return true;
19210 return false;
19211 case VIEW_CONVERT_EXPR:
19212 case NON_LVALUE_EXPR:
19213 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19214 default:
19215 return native_encode_expr (init, array, size) == size;
19219 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19220 attribute is the const value T. */
19222 static bool
19223 tree_add_const_value_attribute (dw_die_ref die, tree t)
19225 tree init;
19226 tree type = TREE_TYPE (t);
19227 rtx rtl;
19229 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19230 return false;
19232 init = t;
19233 gcc_assert (!DECL_P (init));
19235 if (! early_dwarf)
19237 rtl = rtl_for_decl_init (init, type);
19238 if (rtl)
19239 return add_const_value_attribute (die, rtl);
19241 /* If the host and target are sane, try harder. */
19242 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19243 && initializer_constant_valid_p (init, type))
19245 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19246 if (size > 0 && (int) size == size)
19248 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19250 if (native_encode_initializer (init, array, size))
19252 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19253 return true;
19255 ggc_free (array);
19258 return false;
19261 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19262 attribute is the const value of T, where T is an integral constant
19263 variable with static storage duration
19264 (so it can't be a PARM_DECL or a RESULT_DECL). */
19266 static bool
19267 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19270 if (!decl
19271 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19272 || (VAR_P (decl) && !TREE_STATIC (decl)))
19273 return false;
19275 if (TREE_READONLY (decl)
19276 && ! TREE_THIS_VOLATILE (decl)
19277 && DECL_INITIAL (decl))
19278 /* OK */;
19279 else
19280 return false;
19282 /* Don't add DW_AT_const_value if abstract origin already has one. */
19283 if (get_AT (var_die, DW_AT_const_value))
19284 return false;
19286 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19289 /* Convert the CFI instructions for the current function into a
19290 location list. This is used for DW_AT_frame_base when we targeting
19291 a dwarf2 consumer that does not support the dwarf3
19292 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19293 expressions. */
19295 static dw_loc_list_ref
19296 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19298 int ix;
19299 dw_fde_ref fde;
19300 dw_loc_list_ref list, *list_tail;
19301 dw_cfi_ref cfi;
19302 dw_cfa_location last_cfa, next_cfa;
19303 const char *start_label, *last_label, *section;
19304 dw_cfa_location remember;
19306 fde = cfun->fde;
19307 gcc_assert (fde != NULL);
19309 section = secname_for_decl (current_function_decl);
19310 list_tail = &list;
19311 list = NULL;
19313 memset (&next_cfa, 0, sizeof (next_cfa));
19314 next_cfa.reg = INVALID_REGNUM;
19315 remember = next_cfa;
19317 start_label = fde->dw_fde_begin;
19319 /* ??? Bald assumption that the CIE opcode list does not contain
19320 advance opcodes. */
19321 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19322 lookup_cfa_1 (cfi, &next_cfa, &remember);
19324 last_cfa = next_cfa;
19325 last_label = start_label;
19327 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19329 /* If the first partition contained no CFI adjustments, the
19330 CIE opcodes apply to the whole first partition. */
19331 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19332 fde->dw_fde_begin, fde->dw_fde_end, section);
19333 list_tail =&(*list_tail)->dw_loc_next;
19334 start_label = last_label = fde->dw_fde_second_begin;
19337 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19339 switch (cfi->dw_cfi_opc)
19341 case DW_CFA_set_loc:
19342 case DW_CFA_advance_loc1:
19343 case DW_CFA_advance_loc2:
19344 case DW_CFA_advance_loc4:
19345 if (!cfa_equal_p (&last_cfa, &next_cfa))
19347 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19348 start_label, last_label, section);
19350 list_tail = &(*list_tail)->dw_loc_next;
19351 last_cfa = next_cfa;
19352 start_label = last_label;
19354 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19355 break;
19357 case DW_CFA_advance_loc:
19358 /* The encoding is complex enough that we should never emit this. */
19359 gcc_unreachable ();
19361 default:
19362 lookup_cfa_1 (cfi, &next_cfa, &remember);
19363 break;
19365 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19367 if (!cfa_equal_p (&last_cfa, &next_cfa))
19369 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19370 start_label, last_label, section);
19372 list_tail = &(*list_tail)->dw_loc_next;
19373 last_cfa = next_cfa;
19374 start_label = last_label;
19376 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19377 start_label, fde->dw_fde_end, section);
19378 list_tail = &(*list_tail)->dw_loc_next;
19379 start_label = last_label = fde->dw_fde_second_begin;
19383 if (!cfa_equal_p (&last_cfa, &next_cfa))
19385 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19386 start_label, last_label, section);
19387 list_tail = &(*list_tail)->dw_loc_next;
19388 start_label = last_label;
19391 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19392 start_label,
19393 fde->dw_fde_second_begin
19394 ? fde->dw_fde_second_end : fde->dw_fde_end,
19395 section);
19397 if (list && list->dw_loc_next)
19398 gen_llsym (list);
19400 return list;
19403 /* Compute a displacement from the "steady-state frame pointer" to the
19404 frame base (often the same as the CFA), and store it in
19405 frame_pointer_fb_offset. OFFSET is added to the displacement
19406 before the latter is negated. */
19408 static void
19409 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19411 rtx reg, elim;
19413 #ifdef FRAME_POINTER_CFA_OFFSET
19414 reg = frame_pointer_rtx;
19415 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19416 #else
19417 reg = arg_pointer_rtx;
19418 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19419 #endif
19421 elim = (ira_use_lra_p
19422 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19423 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19424 if (GET_CODE (elim) == PLUS)
19426 offset += INTVAL (XEXP (elim, 1));
19427 elim = XEXP (elim, 0);
19430 frame_pointer_fb_offset = -offset;
19432 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19433 in which to eliminate. This is because it's stack pointer isn't
19434 directly accessible as a register within the ISA. To work around
19435 this, assume that while we cannot provide a proper value for
19436 frame_pointer_fb_offset, we won't need one either. */
19437 frame_pointer_fb_offset_valid
19438 = ((SUPPORTS_STACK_ALIGNMENT
19439 && (elim == hard_frame_pointer_rtx
19440 || elim == stack_pointer_rtx))
19441 || elim == (frame_pointer_needed
19442 ? hard_frame_pointer_rtx
19443 : stack_pointer_rtx));
19446 /* Generate a DW_AT_name attribute given some string value to be included as
19447 the value of the attribute. */
19449 static void
19450 add_name_attribute (dw_die_ref die, const char *name_string)
19452 if (name_string != NULL && *name_string != 0)
19454 if (demangle_name_func)
19455 name_string = (*demangle_name_func) (name_string);
19457 add_AT_string (die, DW_AT_name, name_string);
19461 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19462 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19463 of TYPE accordingly.
19465 ??? This is a temporary measure until after we're able to generate
19466 regular DWARF for the complex Ada type system. */
19468 static void
19469 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19470 dw_die_ref context_die)
19472 tree dtype;
19473 dw_die_ref dtype_die;
19475 if (!lang_hooks.types.descriptive_type)
19476 return;
19478 dtype = lang_hooks.types.descriptive_type (type);
19479 if (!dtype)
19480 return;
19482 dtype_die = lookup_type_die (dtype);
19483 if (!dtype_die)
19485 gen_type_die (dtype, context_die);
19486 dtype_die = lookup_type_die (dtype);
19487 gcc_assert (dtype_die);
19490 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19493 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19495 static const char *
19496 comp_dir_string (void)
19498 const char *wd;
19499 char *wd1;
19500 static const char *cached_wd = NULL;
19502 if (cached_wd != NULL)
19503 return cached_wd;
19505 wd = get_src_pwd ();
19506 if (wd == NULL)
19507 return NULL;
19509 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19511 int wdlen;
19513 wdlen = strlen (wd);
19514 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19515 strcpy (wd1, wd);
19516 wd1 [wdlen] = DIR_SEPARATOR;
19517 wd1 [wdlen + 1] = 0;
19518 wd = wd1;
19521 cached_wd = remap_debug_filename (wd);
19522 return cached_wd;
19525 /* Generate a DW_AT_comp_dir attribute for DIE. */
19527 static void
19528 add_comp_dir_attribute (dw_die_ref die)
19530 const char * wd = comp_dir_string ();
19531 if (wd != NULL)
19532 add_AT_string (die, DW_AT_comp_dir, wd);
19535 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19536 pointer computation, ...), output a representation for that bound according
19537 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19538 loc_list_from_tree for the meaning of CONTEXT. */
19540 static void
19541 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19542 int forms, struct loc_descr_context *context)
19544 dw_die_ref context_die, decl_die;
19545 dw_loc_list_ref list;
19546 bool strip_conversions = true;
19547 bool placeholder_seen = false;
19549 while (strip_conversions)
19550 switch (TREE_CODE (value))
19552 case ERROR_MARK:
19553 case SAVE_EXPR:
19554 return;
19556 CASE_CONVERT:
19557 case VIEW_CONVERT_EXPR:
19558 value = TREE_OPERAND (value, 0);
19559 break;
19561 default:
19562 strip_conversions = false;
19563 break;
19566 /* If possible and permitted, output the attribute as a constant. */
19567 if ((forms & dw_scalar_form_constant) != 0
19568 && TREE_CODE (value) == INTEGER_CST)
19570 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19572 /* If HOST_WIDE_INT is big enough then represent the bound as
19573 a constant value. We need to choose a form based on
19574 whether the type is signed or unsigned. We cannot just
19575 call add_AT_unsigned if the value itself is positive
19576 (add_AT_unsigned might add the unsigned value encoded as
19577 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19578 bounds type and then sign extend any unsigned values found
19579 for signed types. This is needed only for
19580 DW_AT_{lower,upper}_bound, since for most other attributes,
19581 consumers will treat DW_FORM_data[1248] as unsigned values,
19582 regardless of the underlying type. */
19583 if (prec <= HOST_BITS_PER_WIDE_INT
19584 || tree_fits_uhwi_p (value))
19586 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19587 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19588 else
19589 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19591 else
19592 /* Otherwise represent the bound as an unsigned value with
19593 the precision of its type. The precision and signedness
19594 of the type will be necessary to re-interpret it
19595 unambiguously. */
19596 add_AT_wide (die, attr, value);
19597 return;
19600 /* Otherwise, if it's possible and permitted too, output a reference to
19601 another DIE. */
19602 if ((forms & dw_scalar_form_reference) != 0)
19604 tree decl = NULL_TREE;
19606 /* Some type attributes reference an outer type. For instance, the upper
19607 bound of an array may reference an embedding record (this happens in
19608 Ada). */
19609 if (TREE_CODE (value) == COMPONENT_REF
19610 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19611 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19612 decl = TREE_OPERAND (value, 1);
19614 else if (VAR_P (value)
19615 || TREE_CODE (value) == PARM_DECL
19616 || TREE_CODE (value) == RESULT_DECL)
19617 decl = value;
19619 if (decl != NULL_TREE)
19621 dw_die_ref decl_die = lookup_decl_die (decl);
19623 /* ??? Can this happen, or should the variable have been bound
19624 first? Probably it can, since I imagine that we try to create
19625 the types of parameters in the order in which they exist in
19626 the list, and won't have created a forward reference to a
19627 later parameter. */
19628 if (decl_die != NULL)
19630 add_AT_die_ref (die, attr, decl_die);
19631 return;
19636 /* Last chance: try to create a stack operation procedure to evaluate the
19637 value. Do nothing if even that is not possible or permitted. */
19638 if ((forms & dw_scalar_form_exprloc) == 0)
19639 return;
19641 list = loc_list_from_tree (value, 2, context);
19642 if (context && context->placeholder_arg)
19644 placeholder_seen = context->placeholder_seen;
19645 context->placeholder_seen = false;
19647 if (list == NULL || single_element_loc_list_p (list))
19649 /* If this attribute is not a reference nor constant, it is
19650 a DWARF expression rather than location description. For that
19651 loc_list_from_tree (value, 0, &context) is needed. */
19652 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19653 if (list2 && single_element_loc_list_p (list2))
19655 if (placeholder_seen)
19657 struct dwarf_procedure_info dpi;
19658 dpi.fndecl = NULL_TREE;
19659 dpi.args_count = 1;
19660 if (!resolve_args_picking (list2->expr, 1, &dpi))
19661 return;
19663 add_AT_loc (die, attr, list2->expr);
19664 return;
19668 /* If that failed to give a single element location list, fall back to
19669 outputting this as a reference... still if permitted. */
19670 if (list == NULL
19671 || (forms & dw_scalar_form_reference) == 0
19672 || placeholder_seen)
19673 return;
19675 if (current_function_decl == 0)
19676 context_die = comp_unit_die ();
19677 else
19678 context_die = lookup_decl_die (current_function_decl);
19680 decl_die = new_die (DW_TAG_variable, context_die, value);
19681 add_AT_flag (decl_die, DW_AT_artificial, 1);
19682 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19683 context_die);
19684 add_AT_location_description (decl_die, DW_AT_location, list);
19685 add_AT_die_ref (die, attr, decl_die);
19688 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19689 default. */
19691 static int
19692 lower_bound_default (void)
19694 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19696 case DW_LANG_C:
19697 case DW_LANG_C89:
19698 case DW_LANG_C99:
19699 case DW_LANG_C11:
19700 case DW_LANG_C_plus_plus:
19701 case DW_LANG_C_plus_plus_11:
19702 case DW_LANG_C_plus_plus_14:
19703 case DW_LANG_ObjC:
19704 case DW_LANG_ObjC_plus_plus:
19705 case DW_LANG_Java:
19706 return 0;
19707 case DW_LANG_Fortran77:
19708 case DW_LANG_Fortran90:
19709 case DW_LANG_Fortran95:
19710 case DW_LANG_Fortran03:
19711 case DW_LANG_Fortran08:
19712 return 1;
19713 case DW_LANG_UPC:
19714 case DW_LANG_D:
19715 case DW_LANG_Python:
19716 return dwarf_version >= 4 ? 0 : -1;
19717 case DW_LANG_Ada95:
19718 case DW_LANG_Ada83:
19719 case DW_LANG_Cobol74:
19720 case DW_LANG_Cobol85:
19721 case DW_LANG_Pascal83:
19722 case DW_LANG_Modula2:
19723 case DW_LANG_PLI:
19724 return dwarf_version >= 4 ? 1 : -1;
19725 default:
19726 return -1;
19730 /* Given a tree node describing an array bound (either lower or upper) output
19731 a representation for that bound. */
19733 static void
19734 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19735 tree bound, struct loc_descr_context *context)
19737 int dflt;
19739 while (1)
19740 switch (TREE_CODE (bound))
19742 /* Strip all conversions. */
19743 CASE_CONVERT:
19744 case VIEW_CONVERT_EXPR:
19745 bound = TREE_OPERAND (bound, 0);
19746 break;
19748 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19749 are even omitted when they are the default. */
19750 case INTEGER_CST:
19751 /* If the value for this bound is the default one, we can even omit the
19752 attribute. */
19753 if (bound_attr == DW_AT_lower_bound
19754 && tree_fits_shwi_p (bound)
19755 && (dflt = lower_bound_default ()) != -1
19756 && tree_to_shwi (bound) == dflt)
19757 return;
19759 /* FALLTHRU */
19761 default:
19762 /* Because of the complex interaction there can be with other GNAT
19763 encodings, GDB isn't ready yet to handle proper DWARF description
19764 for self-referencial subrange bounds: let GNAT encodings do the
19765 magic in such a case. */
19766 if (is_ada ()
19767 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19768 && contains_placeholder_p (bound))
19769 return;
19771 add_scalar_info (subrange_die, bound_attr, bound,
19772 dw_scalar_form_constant
19773 | dw_scalar_form_exprloc
19774 | dw_scalar_form_reference,
19775 context);
19776 return;
19780 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19781 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19782 Note that the block of subscript information for an array type also
19783 includes information about the element type of the given array type.
19785 This function reuses previously set type and bound information if
19786 available. */
19788 static void
19789 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19791 unsigned dimension_number;
19792 tree lower, upper;
19793 dw_die_ref child = type_die->die_child;
19795 for (dimension_number = 0;
19796 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19797 type = TREE_TYPE (type), dimension_number++)
19799 tree domain = TYPE_DOMAIN (type);
19801 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19802 break;
19804 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19805 and (in GNU C only) variable bounds. Handle all three forms
19806 here. */
19808 /* Find and reuse a previously generated DW_TAG_subrange_type if
19809 available.
19811 For multi-dimensional arrays, as we iterate through the
19812 various dimensions in the enclosing for loop above, we also
19813 iterate through the DIE children and pick at each
19814 DW_TAG_subrange_type previously generated (if available).
19815 Each child DW_TAG_subrange_type DIE describes the range of
19816 the current dimension. At this point we should have as many
19817 DW_TAG_subrange_type's as we have dimensions in the
19818 array. */
19819 dw_die_ref subrange_die = NULL;
19820 if (child)
19821 while (1)
19823 child = child->die_sib;
19824 if (child->die_tag == DW_TAG_subrange_type)
19825 subrange_die = child;
19826 if (child == type_die->die_child)
19828 /* If we wrapped around, stop looking next time. */
19829 child = NULL;
19830 break;
19832 if (child->die_tag == DW_TAG_subrange_type)
19833 break;
19835 if (!subrange_die)
19836 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19838 if (domain)
19840 /* We have an array type with specified bounds. */
19841 lower = TYPE_MIN_VALUE (domain);
19842 upper = TYPE_MAX_VALUE (domain);
19844 /* Define the index type. */
19845 if (TREE_TYPE (domain)
19846 && !get_AT (subrange_die, DW_AT_type))
19848 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19849 TREE_TYPE field. We can't emit debug info for this
19850 because it is an unnamed integral type. */
19851 if (TREE_CODE (domain) == INTEGER_TYPE
19852 && TYPE_NAME (domain) == NULL_TREE
19853 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19854 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19856 else
19857 add_type_attribute (subrange_die, TREE_TYPE (domain),
19858 TYPE_UNQUALIFIED, false, type_die);
19861 /* ??? If upper is NULL, the array has unspecified length,
19862 but it does have a lower bound. This happens with Fortran
19863 dimension arr(N:*)
19864 Since the debugger is definitely going to need to know N
19865 to produce useful results, go ahead and output the lower
19866 bound solo, and hope the debugger can cope. */
19868 if (!get_AT (subrange_die, DW_AT_lower_bound))
19869 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19870 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19871 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19874 /* Otherwise we have an array type with an unspecified length. The
19875 DWARF-2 spec does not say how to handle this; let's just leave out the
19876 bounds. */
19880 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19882 static void
19883 add_byte_size_attribute (dw_die_ref die, tree tree_node)
19885 dw_die_ref decl_die;
19886 HOST_WIDE_INT size;
19887 dw_loc_descr_ref size_expr = NULL;
19889 switch (TREE_CODE (tree_node))
19891 case ERROR_MARK:
19892 size = 0;
19893 break;
19894 case ENUMERAL_TYPE:
19895 case RECORD_TYPE:
19896 case UNION_TYPE:
19897 case QUAL_UNION_TYPE:
19898 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
19899 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
19901 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
19902 return;
19904 size_expr = type_byte_size (tree_node, &size);
19905 break;
19906 case FIELD_DECL:
19907 /* For a data member of a struct or union, the DW_AT_byte_size is
19908 generally given as the number of bytes normally allocated for an
19909 object of the *declared* type of the member itself. This is true
19910 even for bit-fields. */
19911 size = int_size_in_bytes (field_type (tree_node));
19912 break;
19913 default:
19914 gcc_unreachable ();
19917 /* Support for dynamically-sized objects was introduced by DWARFv3.
19918 At the moment, GDB does not handle variable byte sizes very well,
19919 though. */
19920 if ((dwarf_version >= 3 || !dwarf_strict)
19921 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
19922 && size_expr != NULL)
19923 add_AT_loc (die, DW_AT_byte_size, size_expr);
19925 /* Note that `size' might be -1 when we get to this point. If it is, that
19926 indicates that the byte size of the entity in question is variable and
19927 that we could not generate a DWARF expression that computes it. */
19928 if (size >= 0)
19929 add_AT_unsigned (die, DW_AT_byte_size, size);
19932 /* For a FIELD_DECL node which represents a bit-field, output an attribute
19933 which specifies the distance in bits from the highest order bit of the
19934 "containing object" for the bit-field to the highest order bit of the
19935 bit-field itself.
19937 For any given bit-field, the "containing object" is a hypothetical object
19938 (of some integral or enum type) within which the given bit-field lives. The
19939 type of this hypothetical "containing object" is always the same as the
19940 declared type of the individual bit-field itself. The determination of the
19941 exact location of the "containing object" for a bit-field is rather
19942 complicated. It's handled by the `field_byte_offset' function (above).
19944 CTX is required: see the comment for VLR_CONTEXT.
19946 Note that it is the size (in bytes) of the hypothetical "containing object"
19947 which will be given in the DW_AT_byte_size attribute for this bit-field.
19948 (See `byte_size_attribute' above). */
19950 static inline void
19951 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
19953 HOST_WIDE_INT object_offset_in_bytes;
19954 tree original_type = DECL_BIT_FIELD_TYPE (decl);
19955 HOST_WIDE_INT bitpos_int;
19956 HOST_WIDE_INT highest_order_object_bit_offset;
19957 HOST_WIDE_INT highest_order_field_bit_offset;
19958 HOST_WIDE_INT bit_offset;
19960 field_byte_offset (decl, ctx, &object_offset_in_bytes);
19962 /* Must be a field and a bit field. */
19963 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
19965 /* We can't yet handle bit-fields whose offsets are variable, so if we
19966 encounter such things, just return without generating any attribute
19967 whatsoever. Likewise for variable or too large size. */
19968 if (! tree_fits_shwi_p (bit_position (decl))
19969 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
19970 return;
19972 bitpos_int = int_bit_position (decl);
19974 /* Note that the bit offset is always the distance (in bits) from the
19975 highest-order bit of the "containing object" to the highest-order bit of
19976 the bit-field itself. Since the "high-order end" of any object or field
19977 is different on big-endian and little-endian machines, the computation
19978 below must take account of these differences. */
19979 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
19980 highest_order_field_bit_offset = bitpos_int;
19982 if (! BYTES_BIG_ENDIAN)
19984 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
19985 highest_order_object_bit_offset +=
19986 simple_type_size_in_bits (original_type);
19989 bit_offset
19990 = (! BYTES_BIG_ENDIAN
19991 ? highest_order_object_bit_offset - highest_order_field_bit_offset
19992 : highest_order_field_bit_offset - highest_order_object_bit_offset);
19994 if (bit_offset < 0)
19995 add_AT_int (die, DW_AT_bit_offset, bit_offset);
19996 else
19997 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20000 /* For a FIELD_DECL node which represents a bit field, output an attribute
20001 which specifies the length in bits of the given field. */
20003 static inline void
20004 add_bit_size_attribute (dw_die_ref die, tree decl)
20006 /* Must be a field and a bit field. */
20007 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20008 && DECL_BIT_FIELD_TYPE (decl));
20010 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20011 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20014 /* If the compiled language is ANSI C, then add a 'prototyped'
20015 attribute, if arg types are given for the parameters of a function. */
20017 static inline void
20018 add_prototyped_attribute (dw_die_ref die, tree func_type)
20020 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20022 case DW_LANG_C:
20023 case DW_LANG_C89:
20024 case DW_LANG_C99:
20025 case DW_LANG_C11:
20026 case DW_LANG_ObjC:
20027 if (prototype_p (func_type))
20028 add_AT_flag (die, DW_AT_prototyped, 1);
20029 break;
20030 default:
20031 break;
20035 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20036 by looking in the type declaration, the object declaration equate table or
20037 the block mapping. */
20039 static inline dw_die_ref
20040 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20042 dw_die_ref origin_die = NULL;
20044 if (TREE_CODE (origin) != FUNCTION_DECL
20045 && TREE_CODE (origin) != BLOCK)
20047 /* We may have gotten separated from the block for the inlined
20048 function, if we're in an exception handler or some such; make
20049 sure that the abstract function has been written out.
20051 Doing this for nested functions is wrong, however; functions are
20052 distinct units, and our context might not even be inline. */
20053 tree fn = origin;
20055 if (TYPE_P (fn))
20056 fn = TYPE_STUB_DECL (fn);
20058 fn = decl_function_context (fn);
20059 if (fn)
20060 dwarf2out_abstract_function (fn);
20063 if (DECL_P (origin))
20064 origin_die = lookup_decl_die (origin);
20065 else if (TYPE_P (origin))
20066 origin_die = lookup_type_die (origin);
20067 else if (TREE_CODE (origin) == BLOCK)
20068 origin_die = BLOCK_DIE (origin);
20070 /* XXX: Functions that are never lowered don't always have correct block
20071 trees (in the case of java, they simply have no block tree, in some other
20072 languages). For these functions, there is nothing we can really do to
20073 output correct debug info for inlined functions in all cases. Rather
20074 than die, we'll just produce deficient debug info now, in that we will
20075 have variables without a proper abstract origin. In the future, when all
20076 functions are lowered, we should re-add a gcc_assert (origin_die)
20077 here. */
20079 if (origin_die)
20080 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20081 return origin_die;
20084 /* We do not currently support the pure_virtual attribute. */
20086 static inline void
20087 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20089 if (DECL_VINDEX (func_decl))
20091 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20093 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20094 add_AT_loc (die, DW_AT_vtable_elem_location,
20095 new_loc_descr (DW_OP_constu,
20096 tree_to_shwi (DECL_VINDEX (func_decl)),
20097 0));
20099 /* GNU extension: Record what type this method came from originally. */
20100 if (debug_info_level > DINFO_LEVEL_TERSE
20101 && DECL_CONTEXT (func_decl))
20102 add_AT_die_ref (die, DW_AT_containing_type,
20103 lookup_type_die (DECL_CONTEXT (func_decl)));
20107 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20108 given decl. This used to be a vendor extension until after DWARF 4
20109 standardized it. */
20111 static void
20112 add_linkage_attr (dw_die_ref die, tree decl)
20114 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20116 /* Mimic what assemble_name_raw does with a leading '*'. */
20117 if (name[0] == '*')
20118 name = &name[1];
20120 if (dwarf_version >= 4)
20121 add_AT_string (die, DW_AT_linkage_name, name);
20122 else
20123 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20126 /* Add source coordinate attributes for the given decl. */
20128 static void
20129 add_src_coords_attributes (dw_die_ref die, tree decl)
20131 expanded_location s;
20133 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20134 return;
20135 s = expand_location (DECL_SOURCE_LOCATION (decl));
20136 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20137 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20140 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20142 static void
20143 add_linkage_name_raw (dw_die_ref die, tree decl)
20145 /* Defer until we have an assembler name set. */
20146 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20148 limbo_die_node *asm_name;
20150 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20151 asm_name->die = die;
20152 asm_name->created_for = decl;
20153 asm_name->next = deferred_asm_name;
20154 deferred_asm_name = asm_name;
20156 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20157 add_linkage_attr (die, decl);
20160 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20162 static void
20163 add_linkage_name (dw_die_ref die, tree decl)
20165 if (debug_info_level > DINFO_LEVEL_NONE
20166 && VAR_OR_FUNCTION_DECL_P (decl)
20167 && TREE_PUBLIC (decl)
20168 && !(VAR_P (decl) && DECL_REGISTER (decl))
20169 && die->die_tag != DW_TAG_member)
20170 add_linkage_name_raw (die, decl);
20173 /* Add a DW_AT_name attribute and source coordinate attribute for the
20174 given decl, but only if it actually has a name. */
20176 static void
20177 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20178 bool no_linkage_name)
20180 tree decl_name;
20182 decl_name = DECL_NAME (decl);
20183 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20185 const char *name = dwarf2_name (decl, 0);
20186 if (name)
20187 add_name_attribute (die, name);
20188 if (! DECL_ARTIFICIAL (decl))
20189 add_src_coords_attributes (die, decl);
20191 if (!no_linkage_name)
20192 add_linkage_name (die, decl);
20195 #ifdef VMS_DEBUGGING_INFO
20196 /* Get the function's name, as described by its RTL. This may be different
20197 from the DECL_NAME name used in the source file. */
20198 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20200 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20201 XEXP (DECL_RTL (decl), 0), false);
20202 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20204 #endif /* VMS_DEBUGGING_INFO */
20207 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20209 static void
20210 add_discr_value (dw_die_ref die, dw_discr_value *value)
20212 dw_attr_node attr;
20214 attr.dw_attr = DW_AT_discr_value;
20215 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20216 attr.dw_attr_val.val_entry = NULL;
20217 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20218 if (value->pos)
20219 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20220 else
20221 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20222 add_dwarf_attr (die, &attr);
20225 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20227 static void
20228 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20230 dw_attr_node attr;
20232 attr.dw_attr = DW_AT_discr_list;
20233 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20234 attr.dw_attr_val.val_entry = NULL;
20235 attr.dw_attr_val.v.val_discr_list = discr_list;
20236 add_dwarf_attr (die, &attr);
20239 static inline dw_discr_list_ref
20240 AT_discr_list (dw_attr_node *attr)
20242 return attr->dw_attr_val.v.val_discr_list;
20245 #ifdef VMS_DEBUGGING_INFO
20246 /* Output the debug main pointer die for VMS */
20248 void
20249 dwarf2out_vms_debug_main_pointer (void)
20251 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20252 dw_die_ref die;
20254 /* Allocate the VMS debug main subprogram die. */
20255 die = ggc_cleared_alloc<die_node> ();
20256 die->die_tag = DW_TAG_subprogram;
20257 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20258 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20259 current_function_funcdef_no);
20260 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20262 /* Make it the first child of comp_unit_die (). */
20263 die->die_parent = comp_unit_die ();
20264 if (comp_unit_die ()->die_child)
20266 die->die_sib = comp_unit_die ()->die_child->die_sib;
20267 comp_unit_die ()->die_child->die_sib = die;
20269 else
20271 die->die_sib = die;
20272 comp_unit_die ()->die_child = die;
20275 #endif /* VMS_DEBUGGING_INFO */
20277 /* Push a new declaration scope. */
20279 static void
20280 push_decl_scope (tree scope)
20282 vec_safe_push (decl_scope_table, scope);
20285 /* Pop a declaration scope. */
20287 static inline void
20288 pop_decl_scope (void)
20290 decl_scope_table->pop ();
20293 /* walk_tree helper function for uses_local_type, below. */
20295 static tree
20296 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20298 if (!TYPE_P (*tp))
20299 *walk_subtrees = 0;
20300 else
20302 tree name = TYPE_NAME (*tp);
20303 if (name && DECL_P (name) && decl_function_context (name))
20304 return *tp;
20306 return NULL_TREE;
20309 /* If TYPE involves a function-local type (including a local typedef to a
20310 non-local type), returns that type; otherwise returns NULL_TREE. */
20312 static tree
20313 uses_local_type (tree type)
20315 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20316 return used;
20319 /* Return the DIE for the scope that immediately contains this type.
20320 Non-named types that do not involve a function-local type get global
20321 scope. Named types nested in namespaces or other types get their
20322 containing scope. All other types (i.e. function-local named types) get
20323 the current active scope. */
20325 static dw_die_ref
20326 scope_die_for (tree t, dw_die_ref context_die)
20328 dw_die_ref scope_die = NULL;
20329 tree containing_scope;
20331 /* Non-types always go in the current scope. */
20332 gcc_assert (TYPE_P (t));
20334 /* Use the scope of the typedef, rather than the scope of the type
20335 it refers to. */
20336 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20337 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20338 else
20339 containing_scope = TYPE_CONTEXT (t);
20341 /* Use the containing namespace if there is one. */
20342 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20344 if (context_die == lookup_decl_die (containing_scope))
20345 /* OK */;
20346 else if (debug_info_level > DINFO_LEVEL_TERSE)
20347 context_die = get_context_die (containing_scope);
20348 else
20349 containing_scope = NULL_TREE;
20352 /* Ignore function type "scopes" from the C frontend. They mean that
20353 a tagged type is local to a parmlist of a function declarator, but
20354 that isn't useful to DWARF. */
20355 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20356 containing_scope = NULL_TREE;
20358 if (SCOPE_FILE_SCOPE_P (containing_scope))
20360 /* If T uses a local type keep it local as well, to avoid references
20361 to function-local DIEs from outside the function. */
20362 if (current_function_decl && uses_local_type (t))
20363 scope_die = context_die;
20364 else
20365 scope_die = comp_unit_die ();
20367 else if (TYPE_P (containing_scope))
20369 /* For types, we can just look up the appropriate DIE. */
20370 if (debug_info_level > DINFO_LEVEL_TERSE)
20371 scope_die = get_context_die (containing_scope);
20372 else
20374 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20375 if (scope_die == NULL)
20376 scope_die = comp_unit_die ();
20379 else
20380 scope_die = context_die;
20382 return scope_die;
20385 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20387 static inline int
20388 local_scope_p (dw_die_ref context_die)
20390 for (; context_die; context_die = context_die->die_parent)
20391 if (context_die->die_tag == DW_TAG_inlined_subroutine
20392 || context_die->die_tag == DW_TAG_subprogram)
20393 return 1;
20395 return 0;
20398 /* Returns nonzero if CONTEXT_DIE is a class. */
20400 static inline int
20401 class_scope_p (dw_die_ref context_die)
20403 return (context_die
20404 && (context_die->die_tag == DW_TAG_structure_type
20405 || context_die->die_tag == DW_TAG_class_type
20406 || context_die->die_tag == DW_TAG_interface_type
20407 || context_die->die_tag == DW_TAG_union_type));
20410 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20411 whether or not to treat a DIE in this context as a declaration. */
20413 static inline int
20414 class_or_namespace_scope_p (dw_die_ref context_die)
20416 return (class_scope_p (context_die)
20417 || (context_die && context_die->die_tag == DW_TAG_namespace));
20420 /* Many forms of DIEs require a "type description" attribute. This
20421 routine locates the proper "type descriptor" die for the type given
20422 by 'type' plus any additional qualifiers given by 'cv_quals', and
20423 adds a DW_AT_type attribute below the given die. */
20425 static void
20426 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20427 bool reverse, dw_die_ref context_die)
20429 enum tree_code code = TREE_CODE (type);
20430 dw_die_ref type_die = NULL;
20432 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20433 or fixed-point type, use the inner type. This is because we have no
20434 support for unnamed types in base_type_die. This can happen if this is
20435 an Ada subrange type. Correct solution is emit a subrange type die. */
20436 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20437 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20438 type = TREE_TYPE (type), code = TREE_CODE (type);
20440 if (code == ERROR_MARK
20441 /* Handle a special case. For functions whose return type is void, we
20442 generate *no* type attribute. (Note that no object may have type
20443 `void', so this only applies to function return types). */
20444 || code == VOID_TYPE)
20445 return;
20447 type_die = modified_type_die (type,
20448 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20449 reverse,
20450 context_die);
20452 if (type_die != NULL)
20453 add_AT_die_ref (object_die, DW_AT_type, type_die);
20456 /* Given an object die, add the calling convention attribute for the
20457 function call type. */
20458 static void
20459 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20461 enum dwarf_calling_convention value = DW_CC_normal;
20463 value = ((enum dwarf_calling_convention)
20464 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20466 if (is_fortran ()
20467 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20469 /* DWARF 2 doesn't provide a way to identify a program's source-level
20470 entry point. DW_AT_calling_convention attributes are only meant
20471 to describe functions' calling conventions. However, lacking a
20472 better way to signal the Fortran main program, we used this for
20473 a long time, following existing custom. Now, DWARF 4 has
20474 DW_AT_main_subprogram, which we add below, but some tools still
20475 rely on the old way, which we thus keep. */
20476 value = DW_CC_program;
20478 if (dwarf_version >= 4 || !dwarf_strict)
20479 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20482 /* Only add the attribute if the backend requests it, and
20483 is not DW_CC_normal. */
20484 if (value && (value != DW_CC_normal))
20485 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20488 /* Given a tree pointer to a struct, class, union, or enum type node, return
20489 a pointer to the (string) tag name for the given type, or zero if the type
20490 was declared without a tag. */
20492 static const char *
20493 type_tag (const_tree type)
20495 const char *name = 0;
20497 if (TYPE_NAME (type) != 0)
20499 tree t = 0;
20501 /* Find the IDENTIFIER_NODE for the type name. */
20502 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20503 && !TYPE_NAMELESS (type))
20504 t = TYPE_NAME (type);
20506 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20507 a TYPE_DECL node, regardless of whether or not a `typedef' was
20508 involved. */
20509 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20510 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20512 /* We want to be extra verbose. Don't call dwarf_name if
20513 DECL_NAME isn't set. The default hook for decl_printable_name
20514 doesn't like that, and in this context it's correct to return
20515 0, instead of "<anonymous>" or the like. */
20516 if (DECL_NAME (TYPE_NAME (type))
20517 && !DECL_NAMELESS (TYPE_NAME (type)))
20518 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20521 /* Now get the name as a string, or invent one. */
20522 if (!name && t != 0)
20523 name = IDENTIFIER_POINTER (t);
20526 return (name == 0 || *name == '\0') ? 0 : name;
20529 /* Return the type associated with a data member, make a special check
20530 for bit field types. */
20532 static inline tree
20533 member_declared_type (const_tree member)
20535 return (DECL_BIT_FIELD_TYPE (member)
20536 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20539 /* Get the decl's label, as described by its RTL. This may be different
20540 from the DECL_NAME name used in the source file. */
20542 #if 0
20543 static const char *
20544 decl_start_label (tree decl)
20546 rtx x;
20547 const char *fnname;
20549 x = DECL_RTL (decl);
20550 gcc_assert (MEM_P (x));
20552 x = XEXP (x, 0);
20553 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20555 fnname = XSTR (x, 0);
20556 return fnname;
20558 #endif
20560 /* For variable-length arrays that have been previously generated, but
20561 may be incomplete due to missing subscript info, fill the subscript
20562 info. Return TRUE if this is one of those cases. */
20563 static bool
20564 fill_variable_array_bounds (tree type)
20566 if (TREE_ASM_WRITTEN (type)
20567 && TREE_CODE (type) == ARRAY_TYPE
20568 && variably_modified_type_p (type, NULL))
20570 dw_die_ref array_die = lookup_type_die (type);
20571 if (!array_die)
20572 return false;
20573 add_subscript_info (array_die, type, !is_ada ());
20574 return true;
20576 return false;
20579 /* These routines generate the internal representation of the DIE's for
20580 the compilation unit. Debugging information is collected by walking
20581 the declaration trees passed in from dwarf2out_decl(). */
20583 static void
20584 gen_array_type_die (tree type, dw_die_ref context_die)
20586 dw_die_ref array_die;
20588 /* GNU compilers represent multidimensional array types as sequences of one
20589 dimensional array types whose element types are themselves array types.
20590 We sometimes squish that down to a single array_type DIE with multiple
20591 subscripts in the Dwarf debugging info. The draft Dwarf specification
20592 say that we are allowed to do this kind of compression in C, because
20593 there is no difference between an array of arrays and a multidimensional
20594 array. We don't do this for Ada to remain as close as possible to the
20595 actual representation, which is especially important against the language
20596 flexibilty wrt arrays of variable size. */
20598 bool collapse_nested_arrays = !is_ada ();
20600 if (fill_variable_array_bounds (type))
20601 return;
20603 dw_die_ref scope_die = scope_die_for (type, context_die);
20604 tree element_type;
20606 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20607 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20608 if (TYPE_STRING_FLAG (type)
20609 && TREE_CODE (type) == ARRAY_TYPE
20610 && is_fortran ()
20611 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20613 HOST_WIDE_INT size;
20615 array_die = new_die (DW_TAG_string_type, scope_die, type);
20616 add_name_attribute (array_die, type_tag (type));
20617 equate_type_number_to_die (type, array_die);
20618 size = int_size_in_bytes (type);
20619 if (size >= 0)
20620 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20621 else if (TYPE_DOMAIN (type) != NULL_TREE
20622 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20624 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20625 tree rszdecl = szdecl;
20626 HOST_WIDE_INT rsize = 0;
20628 size = int_size_in_bytes (TREE_TYPE (szdecl));
20629 if (!DECL_P (szdecl))
20631 if (TREE_CODE (szdecl) == INDIRECT_REF
20632 && DECL_P (TREE_OPERAND (szdecl, 0)))
20634 rszdecl = TREE_OPERAND (szdecl, 0);
20635 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
20636 if (rsize <= 0)
20637 size = 0;
20639 else
20640 size = 0;
20642 if (size > 0)
20644 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
20645 if (loc == NULL
20646 && early_dwarf
20647 && current_function_decl
20648 && DECL_CONTEXT (rszdecl) == current_function_decl)
20650 dw_die_ref ref = lookup_decl_die (rszdecl);
20651 dw_loc_descr_ref l = NULL;
20652 if (ref)
20654 l = new_loc_descr (DW_OP_call4, 0, 0);
20655 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20656 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
20657 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
20659 else if (TREE_CODE (rszdecl) == PARM_DECL
20660 && string_types)
20662 l = new_loc_descr (DW_OP_call4, 0, 0);
20663 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
20664 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
20665 string_types->safe_push (array_die);
20667 if (l && rszdecl != szdecl)
20669 if (rsize == DWARF2_ADDR_SIZE)
20670 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
20671 0, 0));
20672 else
20673 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
20674 rsize, 0));
20676 if (l)
20677 loc = new_loc_list (l, NULL, NULL, NULL);
20679 if (loc)
20681 add_AT_location_description (array_die, DW_AT_string_length,
20682 loc);
20683 if (size != DWARF2_ADDR_SIZE)
20684 add_AT_unsigned (array_die, dwarf_version >= 5
20685 ? DW_AT_string_length_byte_size
20686 : DW_AT_byte_size, size);
20690 return;
20693 array_die = new_die (DW_TAG_array_type, scope_die, type);
20694 add_name_attribute (array_die, type_tag (type));
20695 equate_type_number_to_die (type, array_die);
20697 if (TREE_CODE (type) == VECTOR_TYPE)
20698 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20700 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20701 if (is_fortran ()
20702 && TREE_CODE (type) == ARRAY_TYPE
20703 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20704 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20705 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20707 #if 0
20708 /* We default the array ordering. SDB will probably do
20709 the right things even if DW_AT_ordering is not present. It's not even
20710 an issue until we start to get into multidimensional arrays anyway. If
20711 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20712 then we'll have to put the DW_AT_ordering attribute back in. (But if
20713 and when we find out that we need to put these in, we will only do so
20714 for multidimensional arrays. */
20715 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20716 #endif
20718 if (TREE_CODE (type) == VECTOR_TYPE)
20720 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20721 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20722 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20723 add_bound_info (subrange_die, DW_AT_upper_bound,
20724 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20726 else
20727 add_subscript_info (array_die, type, collapse_nested_arrays);
20729 /* Add representation of the type of the elements of this array type and
20730 emit the corresponding DIE if we haven't done it already. */
20731 element_type = TREE_TYPE (type);
20732 if (collapse_nested_arrays)
20733 while (TREE_CODE (element_type) == ARRAY_TYPE)
20735 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20736 break;
20737 element_type = TREE_TYPE (element_type);
20740 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20741 TREE_CODE (type) == ARRAY_TYPE
20742 && TYPE_REVERSE_STORAGE_ORDER (type),
20743 context_die);
20745 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20746 if (TYPE_ARTIFICIAL (type))
20747 add_AT_flag (array_die, DW_AT_artificial, 1);
20749 if (get_AT (array_die, DW_AT_name))
20750 add_pubtype (type, array_die);
20753 /* After all arguments are created, adjust any DW_TAG_string_type
20754 DIEs DW_AT_string_length attributes. */
20756 static void
20757 adjust_string_types (void)
20759 dw_die_ref array_die;
20760 unsigned int i;
20761 FOR_EACH_VEC_ELT (*string_types, i, array_die)
20763 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
20764 if (a == NULL)
20765 continue;
20766 dw_loc_descr_ref loc = AT_loc (a);
20767 gcc_assert (loc->dw_loc_opc == DW_OP_call4
20768 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
20769 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
20770 if (ref)
20772 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20773 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
20774 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
20776 else
20778 remove_AT (array_die, DW_AT_string_length);
20779 remove_AT (array_die, dwarf_version >= 5
20780 ? DW_AT_string_length_byte_size
20781 : DW_AT_byte_size);
20786 /* This routine generates DIE for array with hidden descriptor, details
20787 are filled into *info by a langhook. */
20789 static void
20790 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20791 dw_die_ref context_die)
20793 const dw_die_ref scope_die = scope_die_for (type, context_die);
20794 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20795 struct loc_descr_context context = { type, info->base_decl, NULL,
20796 false, false };
20797 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20798 int dim;
20800 add_name_attribute (array_die, type_tag (type));
20801 equate_type_number_to_die (type, array_die);
20803 if (info->ndimensions > 1)
20804 switch (info->ordering)
20806 case array_descr_ordering_row_major:
20807 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20808 break;
20809 case array_descr_ordering_column_major:
20810 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20811 break;
20812 default:
20813 break;
20816 if (dwarf_version >= 3 || !dwarf_strict)
20818 if (info->data_location)
20819 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20820 dw_scalar_form_exprloc, &context);
20821 if (info->associated)
20822 add_scalar_info (array_die, DW_AT_associated, info->associated,
20823 dw_scalar_form_constant
20824 | dw_scalar_form_exprloc
20825 | dw_scalar_form_reference, &context);
20826 if (info->allocated)
20827 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20828 dw_scalar_form_constant
20829 | dw_scalar_form_exprloc
20830 | dw_scalar_form_reference, &context);
20831 if (info->stride)
20833 const enum dwarf_attribute attr
20834 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20835 const int forms
20836 = (info->stride_in_bits)
20837 ? dw_scalar_form_constant
20838 : (dw_scalar_form_constant
20839 | dw_scalar_form_exprloc
20840 | dw_scalar_form_reference);
20842 add_scalar_info (array_die, attr, info->stride, forms, &context);
20845 if (dwarf_version >= 5)
20847 if (info->rank)
20849 add_scalar_info (array_die, DW_AT_rank, info->rank,
20850 dw_scalar_form_constant
20851 | dw_scalar_form_exprloc, &context);
20852 subrange_tag = DW_TAG_generic_subrange;
20853 context.placeholder_arg = true;
20857 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20859 for (dim = 0; dim < info->ndimensions; dim++)
20861 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20863 if (info->dimen[dim].bounds_type)
20864 add_type_attribute (subrange_die,
20865 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20866 false, context_die);
20867 if (info->dimen[dim].lower_bound)
20868 add_bound_info (subrange_die, DW_AT_lower_bound,
20869 info->dimen[dim].lower_bound, &context);
20870 if (info->dimen[dim].upper_bound)
20871 add_bound_info (subrange_die, DW_AT_upper_bound,
20872 info->dimen[dim].upper_bound, &context);
20873 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20874 add_scalar_info (subrange_die, DW_AT_byte_stride,
20875 info->dimen[dim].stride,
20876 dw_scalar_form_constant
20877 | dw_scalar_form_exprloc
20878 | dw_scalar_form_reference,
20879 &context);
20882 gen_type_die (info->element_type, context_die);
20883 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20884 TREE_CODE (type) == ARRAY_TYPE
20885 && TYPE_REVERSE_STORAGE_ORDER (type),
20886 context_die);
20888 if (get_AT (array_die, DW_AT_name))
20889 add_pubtype (type, array_die);
20892 #if 0
20893 static void
20894 gen_entry_point_die (tree decl, dw_die_ref context_die)
20896 tree origin = decl_ultimate_origin (decl);
20897 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20899 if (origin != NULL)
20900 add_abstract_origin_attribute (decl_die, origin);
20901 else
20903 add_name_and_src_coords_attributes (decl_die, decl);
20904 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20905 TYPE_UNQUALIFIED, false, context_die);
20908 if (DECL_ABSTRACT_P (decl))
20909 equate_decl_number_to_die (decl, decl_die);
20910 else
20911 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
20913 #endif
20915 /* Walk through the list of incomplete types again, trying once more to
20916 emit full debugging info for them. */
20918 static void
20919 retry_incomplete_types (void)
20921 set_early_dwarf s;
20922 int i;
20924 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
20925 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
20926 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
20927 vec_safe_truncate (incomplete_types, 0);
20930 /* Determine what tag to use for a record type. */
20932 static enum dwarf_tag
20933 record_type_tag (tree type)
20935 if (! lang_hooks.types.classify_record)
20936 return DW_TAG_structure_type;
20938 switch (lang_hooks.types.classify_record (type))
20940 case RECORD_IS_STRUCT:
20941 return DW_TAG_structure_type;
20943 case RECORD_IS_CLASS:
20944 return DW_TAG_class_type;
20946 case RECORD_IS_INTERFACE:
20947 if (dwarf_version >= 3 || !dwarf_strict)
20948 return DW_TAG_interface_type;
20949 return DW_TAG_structure_type;
20951 default:
20952 gcc_unreachable ();
20956 /* Generate a DIE to represent an enumeration type. Note that these DIEs
20957 include all of the information about the enumeration values also. Each
20958 enumerated type name/value is listed as a child of the enumerated type
20959 DIE. */
20961 static dw_die_ref
20962 gen_enumeration_type_die (tree type, dw_die_ref context_die)
20964 dw_die_ref type_die = lookup_type_die (type);
20966 if (type_die == NULL)
20968 type_die = new_die (DW_TAG_enumeration_type,
20969 scope_die_for (type, context_die), type);
20970 equate_type_number_to_die (type, type_die);
20971 add_name_attribute (type_die, type_tag (type));
20972 if (dwarf_version >= 4 || !dwarf_strict)
20974 if (ENUM_IS_SCOPED (type))
20975 add_AT_flag (type_die, DW_AT_enum_class, 1);
20976 if (ENUM_IS_OPAQUE (type))
20977 add_AT_flag (type_die, DW_AT_declaration, 1);
20979 if (!dwarf_strict)
20980 add_AT_unsigned (type_die, DW_AT_encoding,
20981 TYPE_UNSIGNED (type)
20982 ? DW_ATE_unsigned
20983 : DW_ATE_signed);
20985 else if (! TYPE_SIZE (type))
20986 return type_die;
20987 else
20988 remove_AT (type_die, DW_AT_declaration);
20990 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
20991 given enum type is incomplete, do not generate the DW_AT_byte_size
20992 attribute or the DW_AT_element_list attribute. */
20993 if (TYPE_SIZE (type))
20995 tree link;
20997 TREE_ASM_WRITTEN (type) = 1;
20998 add_byte_size_attribute (type_die, type);
20999 if (dwarf_version >= 3 || !dwarf_strict)
21001 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21002 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21003 context_die);
21005 if (TYPE_STUB_DECL (type) != NULL_TREE)
21007 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21008 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21011 /* If the first reference to this type was as the return type of an
21012 inline function, then it may not have a parent. Fix this now. */
21013 if (type_die->die_parent == NULL)
21014 add_child_die (scope_die_for (type, context_die), type_die);
21016 for (link = TYPE_VALUES (type);
21017 link != NULL; link = TREE_CHAIN (link))
21019 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21020 tree value = TREE_VALUE (link);
21022 add_name_attribute (enum_die,
21023 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21025 if (TREE_CODE (value) == CONST_DECL)
21026 value = DECL_INITIAL (value);
21028 if (simple_type_size_in_bits (TREE_TYPE (value))
21029 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21031 /* For constant forms created by add_AT_unsigned DWARF
21032 consumers (GDB, elfutils, etc.) always zero extend
21033 the value. Only when the actual value is negative
21034 do we need to use add_AT_int to generate a constant
21035 form that can represent negative values. */
21036 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21037 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21038 add_AT_unsigned (enum_die, DW_AT_const_value,
21039 (unsigned HOST_WIDE_INT) val);
21040 else
21041 add_AT_int (enum_die, DW_AT_const_value, val);
21043 else
21044 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21045 that here. TODO: This should be re-worked to use correct
21046 signed/unsigned double tags for all cases. */
21047 add_AT_wide (enum_die, DW_AT_const_value, value);
21050 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21051 if (TYPE_ARTIFICIAL (type))
21052 add_AT_flag (type_die, DW_AT_artificial, 1);
21054 else
21055 add_AT_flag (type_die, DW_AT_declaration, 1);
21057 add_pubtype (type, type_die);
21059 return type_die;
21062 /* Generate a DIE to represent either a real live formal parameter decl or to
21063 represent just the type of some formal parameter position in some function
21064 type.
21066 Note that this routine is a bit unusual because its argument may be a
21067 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21068 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21069 node. If it's the former then this function is being called to output a
21070 DIE to represent a formal parameter object (or some inlining thereof). If
21071 it's the latter, then this function is only being called to output a
21072 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21073 argument type of some subprogram type.
21074 If EMIT_NAME_P is true, name and source coordinate attributes
21075 are emitted. */
21077 static dw_die_ref
21078 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21079 dw_die_ref context_die)
21081 tree node_or_origin = node ? node : origin;
21082 tree ultimate_origin;
21083 dw_die_ref parm_die = NULL;
21085 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21087 parm_die = lookup_decl_die (node);
21089 /* If the contexts differ, we may not be talking about the same
21090 thing. */
21091 if (parm_die && parm_die->die_parent != context_die)
21093 if (!DECL_ABSTRACT_P (node))
21095 /* This can happen when creating an inlined instance, in
21096 which case we need to create a new DIE that will get
21097 annotated with DW_AT_abstract_origin. */
21098 parm_die = NULL;
21100 else
21102 /* FIXME: Reuse DIE even with a differing context.
21104 This can happen when calling
21105 dwarf2out_abstract_function to build debug info for
21106 the abstract instance of a function for which we have
21107 already generated a DIE in
21108 dwarf2out_early_global_decl.
21110 Once we remove dwarf2out_abstract_function, we should
21111 have a call to gcc_unreachable here. */
21115 if (parm_die && parm_die->die_parent == NULL)
21117 /* Check that parm_die already has the right attributes that
21118 we would have added below. If any attributes are
21119 missing, fall through to add them. */
21120 if (! DECL_ABSTRACT_P (node_or_origin)
21121 && !get_AT (parm_die, DW_AT_location)
21122 && !get_AT (parm_die, DW_AT_const_value))
21123 /* We are missing location info, and are about to add it. */
21125 else
21127 add_child_die (context_die, parm_die);
21128 return parm_die;
21133 /* If we have a previously generated DIE, use it, unless this is an
21134 concrete instance (origin != NULL), in which case we need a new
21135 DIE with a corresponding DW_AT_abstract_origin. */
21136 bool reusing_die;
21137 if (parm_die && origin == NULL)
21138 reusing_die = true;
21139 else
21141 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21142 reusing_die = false;
21145 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21147 case tcc_declaration:
21148 ultimate_origin = decl_ultimate_origin (node_or_origin);
21149 if (node || ultimate_origin)
21150 origin = ultimate_origin;
21152 if (reusing_die)
21153 goto add_location;
21155 if (origin != NULL)
21156 add_abstract_origin_attribute (parm_die, origin);
21157 else if (emit_name_p)
21158 add_name_and_src_coords_attributes (parm_die, node);
21159 if (origin == NULL
21160 || (! DECL_ABSTRACT_P (node_or_origin)
21161 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21162 decl_function_context
21163 (node_or_origin))))
21165 tree type = TREE_TYPE (node_or_origin);
21166 if (decl_by_reference_p (node_or_origin))
21167 add_type_attribute (parm_die, TREE_TYPE (type),
21168 TYPE_UNQUALIFIED,
21169 false, context_die);
21170 else
21171 add_type_attribute (parm_die, type,
21172 decl_quals (node_or_origin),
21173 false, context_die);
21175 if (origin == NULL && DECL_ARTIFICIAL (node))
21176 add_AT_flag (parm_die, DW_AT_artificial, 1);
21177 add_location:
21178 if (node && node != origin)
21179 equate_decl_number_to_die (node, parm_die);
21180 if (! DECL_ABSTRACT_P (node_or_origin))
21181 add_location_or_const_value_attribute (parm_die, node_or_origin,
21182 node == NULL);
21184 break;
21186 case tcc_type:
21187 /* We were called with some kind of a ..._TYPE node. */
21188 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21189 context_die);
21190 break;
21192 default:
21193 gcc_unreachable ();
21196 return parm_die;
21199 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21200 children DW_TAG_formal_parameter DIEs representing the arguments of the
21201 parameter pack.
21203 PARM_PACK must be a function parameter pack.
21204 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21205 must point to the subsequent arguments of the function PACK_ARG belongs to.
21206 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21207 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21208 following the last one for which a DIE was generated. */
21210 static dw_die_ref
21211 gen_formal_parameter_pack_die (tree parm_pack,
21212 tree pack_arg,
21213 dw_die_ref subr_die,
21214 tree *next_arg)
21216 tree arg;
21217 dw_die_ref parm_pack_die;
21219 gcc_assert (parm_pack
21220 && lang_hooks.function_parameter_pack_p (parm_pack)
21221 && subr_die);
21223 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21224 add_src_coords_attributes (parm_pack_die, parm_pack);
21226 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21228 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21229 parm_pack))
21230 break;
21231 gen_formal_parameter_die (arg, NULL,
21232 false /* Don't emit name attribute. */,
21233 parm_pack_die);
21235 if (next_arg)
21236 *next_arg = arg;
21237 return parm_pack_die;
21240 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21241 at the end of an (ANSI prototyped) formal parameters list. */
21243 static void
21244 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21246 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21249 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21250 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21251 parameters as specified in some function type specification (except for
21252 those which appear as part of a function *definition*). */
21254 static void
21255 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21257 tree link;
21258 tree formal_type = NULL;
21259 tree first_parm_type;
21260 tree arg;
21262 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21264 arg = DECL_ARGUMENTS (function_or_method_type);
21265 function_or_method_type = TREE_TYPE (function_or_method_type);
21267 else
21268 arg = NULL_TREE;
21270 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21272 /* Make our first pass over the list of formal parameter types and output a
21273 DW_TAG_formal_parameter DIE for each one. */
21274 for (link = first_parm_type; link; )
21276 dw_die_ref parm_die;
21278 formal_type = TREE_VALUE (link);
21279 if (formal_type == void_type_node)
21280 break;
21282 /* Output a (nameless) DIE to represent the formal parameter itself. */
21283 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21285 parm_die = gen_formal_parameter_die (formal_type, NULL,
21286 true /* Emit name attribute. */,
21287 context_die);
21288 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21289 && link == first_parm_type)
21291 add_AT_flag (parm_die, DW_AT_artificial, 1);
21292 if (dwarf_version >= 3 || !dwarf_strict)
21293 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21295 else if (arg && DECL_ARTIFICIAL (arg))
21296 add_AT_flag (parm_die, DW_AT_artificial, 1);
21299 link = TREE_CHAIN (link);
21300 if (arg)
21301 arg = DECL_CHAIN (arg);
21304 /* If this function type has an ellipsis, add a
21305 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21306 if (formal_type != void_type_node)
21307 gen_unspecified_parameters_die (function_or_method_type, context_die);
21309 /* Make our second (and final) pass over the list of formal parameter types
21310 and output DIEs to represent those types (as necessary). */
21311 for (link = TYPE_ARG_TYPES (function_or_method_type);
21312 link && TREE_VALUE (link);
21313 link = TREE_CHAIN (link))
21314 gen_type_die (TREE_VALUE (link), context_die);
21317 /* We want to generate the DIE for TYPE so that we can generate the
21318 die for MEMBER, which has been defined; we will need to refer back
21319 to the member declaration nested within TYPE. If we're trying to
21320 generate minimal debug info for TYPE, processing TYPE won't do the
21321 trick; we need to attach the member declaration by hand. */
21323 static void
21324 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21326 gen_type_die (type, context_die);
21328 /* If we're trying to avoid duplicate debug info, we may not have
21329 emitted the member decl for this function. Emit it now. */
21330 if (TYPE_STUB_DECL (type)
21331 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21332 && ! lookup_decl_die (member))
21334 dw_die_ref type_die;
21335 gcc_assert (!decl_ultimate_origin (member));
21337 push_decl_scope (type);
21338 type_die = lookup_type_die_strip_naming_typedef (type);
21339 if (TREE_CODE (member) == FUNCTION_DECL)
21340 gen_subprogram_die (member, type_die);
21341 else if (TREE_CODE (member) == FIELD_DECL)
21343 /* Ignore the nameless fields that are used to skip bits but handle
21344 C++ anonymous unions and structs. */
21345 if (DECL_NAME (member) != NULL_TREE
21346 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21347 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21349 struct vlr_context vlr_ctx = {
21350 DECL_CONTEXT (member), /* struct_type */
21351 NULL_TREE /* variant_part_offset */
21353 gen_type_die (member_declared_type (member), type_die);
21354 gen_field_die (member, &vlr_ctx, type_die);
21357 else
21358 gen_variable_die (member, NULL_TREE, type_die);
21360 pop_decl_scope ();
21364 /* Forward declare these functions, because they are mutually recursive
21365 with their set_block_* pairing functions. */
21366 static void set_decl_origin_self (tree);
21367 static void set_decl_abstract_flags (tree, vec<tree> &);
21369 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21370 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21371 that it points to the node itself, thus indicating that the node is its
21372 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21373 the given node is NULL, recursively descend the decl/block tree which
21374 it is the root of, and for each other ..._DECL or BLOCK node contained
21375 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21376 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21377 values to point to themselves. */
21379 static void
21380 set_block_origin_self (tree stmt)
21382 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21384 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21387 tree local_decl;
21389 for (local_decl = BLOCK_VARS (stmt);
21390 local_decl != NULL_TREE;
21391 local_decl = DECL_CHAIN (local_decl))
21392 /* Do not recurse on nested functions since the inlining status
21393 of parent and child can be different as per the DWARF spec. */
21394 if (TREE_CODE (local_decl) != FUNCTION_DECL
21395 && !DECL_EXTERNAL (local_decl))
21396 set_decl_origin_self (local_decl);
21400 tree subblock;
21402 for (subblock = BLOCK_SUBBLOCKS (stmt);
21403 subblock != NULL_TREE;
21404 subblock = BLOCK_CHAIN (subblock))
21405 set_block_origin_self (subblock); /* Recurse. */
21410 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21411 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21412 node to so that it points to the node itself, thus indicating that the
21413 node represents its own (abstract) origin. Additionally, if the
21414 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21415 the decl/block tree of which the given node is the root of, and for
21416 each other ..._DECL or BLOCK node contained therein whose
21417 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21418 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21419 point to themselves. */
21421 static void
21422 set_decl_origin_self (tree decl)
21424 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21426 DECL_ABSTRACT_ORIGIN (decl) = decl;
21427 if (TREE_CODE (decl) == FUNCTION_DECL)
21429 tree arg;
21431 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21432 DECL_ABSTRACT_ORIGIN (arg) = arg;
21433 if (DECL_INITIAL (decl) != NULL_TREE
21434 && DECL_INITIAL (decl) != error_mark_node)
21435 set_block_origin_self (DECL_INITIAL (decl));
21440 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21441 and if it wasn't 1 before, push it to abstract_vec vector.
21442 For all local decls and all local sub-blocks (recursively) do it
21443 too. */
21445 static void
21446 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21448 tree local_decl;
21449 tree subblock;
21450 unsigned int i;
21452 if (!BLOCK_ABSTRACT (stmt))
21454 abstract_vec.safe_push (stmt);
21455 BLOCK_ABSTRACT (stmt) = 1;
21458 for (local_decl = BLOCK_VARS (stmt);
21459 local_decl != NULL_TREE;
21460 local_decl = DECL_CHAIN (local_decl))
21461 if (! DECL_EXTERNAL (local_decl))
21462 set_decl_abstract_flags (local_decl, abstract_vec);
21464 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21466 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21467 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21468 || TREE_CODE (local_decl) == PARM_DECL)
21469 set_decl_abstract_flags (local_decl, abstract_vec);
21472 for (subblock = BLOCK_SUBBLOCKS (stmt);
21473 subblock != NULL_TREE;
21474 subblock = BLOCK_CHAIN (subblock))
21475 set_block_abstract_flags (subblock, abstract_vec);
21478 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21479 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21480 In the case where the decl is a FUNCTION_DECL also set the abstract
21481 flags for all of the parameters, local vars, local
21482 blocks and sub-blocks (recursively). */
21484 static void
21485 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21487 if (!DECL_ABSTRACT_P (decl))
21489 abstract_vec.safe_push (decl);
21490 DECL_ABSTRACT_P (decl) = 1;
21493 if (TREE_CODE (decl) == FUNCTION_DECL)
21495 tree arg;
21497 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21498 if (!DECL_ABSTRACT_P (arg))
21500 abstract_vec.safe_push (arg);
21501 DECL_ABSTRACT_P (arg) = 1;
21503 if (DECL_INITIAL (decl) != NULL_TREE
21504 && DECL_INITIAL (decl) != error_mark_node)
21505 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21509 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21510 may later generate inlined and/or out-of-line instances of.
21512 FIXME: In the early-dwarf world, this function, and most of the
21513 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21514 the abstract instance. All we would need to do is annotate
21515 the early DIE with the appropriate DW_AT_inline in late
21516 dwarf (perhaps in gen_inlined_subroutine_die).
21518 However, we can't do this yet, because LTO streaming of DIEs
21519 has not been implemented yet. */
21521 static void
21522 dwarf2out_abstract_function (tree decl)
21524 dw_die_ref old_die;
21525 tree save_fn;
21526 tree context;
21527 hash_table<decl_loc_hasher> *old_decl_loc_table;
21528 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21529 int old_call_site_count, old_tail_call_site_count;
21530 struct call_arg_loc_node *old_call_arg_locations;
21532 /* Make sure we have the actual abstract inline, not a clone. */
21533 decl = DECL_ORIGIN (decl);
21535 old_die = lookup_decl_die (decl);
21536 if (old_die && get_AT (old_die, DW_AT_inline))
21537 /* We've already generated the abstract instance. */
21538 return;
21540 /* We can be called while recursively when seeing block defining inlined subroutine
21541 DIE. Be sure to not clobber the outer location table nor use it or we would
21542 get locations in abstract instantces. */
21543 old_decl_loc_table = decl_loc_table;
21544 decl_loc_table = NULL;
21545 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21546 cached_dw_loc_list_table = NULL;
21547 old_call_arg_locations = call_arg_locations;
21548 call_arg_locations = NULL;
21549 old_call_site_count = call_site_count;
21550 call_site_count = -1;
21551 old_tail_call_site_count = tail_call_site_count;
21552 tail_call_site_count = -1;
21554 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21555 we don't get confused by DECL_ABSTRACT_P. */
21556 if (debug_info_level > DINFO_LEVEL_TERSE)
21558 context = decl_class_context (decl);
21559 if (context)
21560 gen_type_die_for_member
21561 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21564 /* Pretend we've just finished compiling this function. */
21565 save_fn = current_function_decl;
21566 current_function_decl = decl;
21568 auto_vec<tree, 64> abstract_vec;
21569 set_decl_abstract_flags (decl, abstract_vec);
21570 dwarf2out_decl (decl);
21571 unsigned int i;
21572 tree t;
21573 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21574 if (TREE_CODE (t) == BLOCK)
21575 BLOCK_ABSTRACT (t) = 0;
21576 else
21577 DECL_ABSTRACT_P (t) = 0;
21579 current_function_decl = save_fn;
21580 decl_loc_table = old_decl_loc_table;
21581 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21582 call_arg_locations = old_call_arg_locations;
21583 call_site_count = old_call_site_count;
21584 tail_call_site_count = old_tail_call_site_count;
21587 /* Helper function of premark_used_types() which gets called through
21588 htab_traverse.
21590 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21591 marked as unused by prune_unused_types. */
21593 bool
21594 premark_used_types_helper (tree const &type, void *)
21596 dw_die_ref die;
21598 die = lookup_type_die (type);
21599 if (die != NULL)
21600 die->die_perennial_p = 1;
21601 return true;
21604 /* Helper function of premark_types_used_by_global_vars which gets called
21605 through htab_traverse.
21607 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21608 marked as unused by prune_unused_types. The DIE of the type is marked
21609 only if the global variable using the type will actually be emitted. */
21612 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21613 void *)
21615 struct types_used_by_vars_entry *entry;
21616 dw_die_ref die;
21618 entry = (struct types_used_by_vars_entry *) *slot;
21619 gcc_assert (entry->type != NULL
21620 && entry->var_decl != NULL);
21621 die = lookup_type_die (entry->type);
21622 if (die)
21624 /* Ask cgraph if the global variable really is to be emitted.
21625 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21626 varpool_node *node = varpool_node::get (entry->var_decl);
21627 if (node && node->definition)
21629 die->die_perennial_p = 1;
21630 /* Keep the parent DIEs as well. */
21631 while ((die = die->die_parent) && die->die_perennial_p == 0)
21632 die->die_perennial_p = 1;
21635 return 1;
21638 /* Mark all members of used_types_hash as perennial. */
21640 static void
21641 premark_used_types (struct function *fun)
21643 if (fun && fun->used_types_hash)
21644 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21647 /* Mark all members of types_used_by_vars_entry as perennial. */
21649 static void
21650 premark_types_used_by_global_vars (void)
21652 if (types_used_by_vars_hash)
21653 types_used_by_vars_hash
21654 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21657 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21658 for CA_LOC call arg loc node. */
21660 static dw_die_ref
21661 gen_call_site_die (tree decl, dw_die_ref subr_die,
21662 struct call_arg_loc_node *ca_loc)
21664 dw_die_ref stmt_die = NULL, die;
21665 tree block = ca_loc->block;
21667 while (block
21668 && block != DECL_INITIAL (decl)
21669 && TREE_CODE (block) == BLOCK)
21671 stmt_die = BLOCK_DIE (block);
21672 if (stmt_die)
21673 break;
21674 block = BLOCK_SUPERCONTEXT (block);
21676 if (stmt_die == NULL)
21677 stmt_die = subr_die;
21678 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21679 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21680 if (ca_loc->tail_call_p)
21681 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21682 if (ca_loc->symbol_ref)
21684 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21685 if (tdie)
21686 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21687 else
21688 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21689 false);
21691 return die;
21694 /* Generate a DIE to represent a declared function (either file-scope or
21695 block-local). */
21697 static void
21698 gen_subprogram_die (tree decl, dw_die_ref context_die)
21700 tree origin = decl_ultimate_origin (decl);
21701 dw_die_ref subr_die;
21702 dw_die_ref old_die = lookup_decl_die (decl);
21704 /* This function gets called multiple times for different stages of
21705 the debug process. For example, for func() in this code:
21707 namespace S
21709 void func() { ... }
21712 ...we get called 4 times. Twice in early debug and twice in
21713 late debug:
21715 Early debug
21716 -----------
21718 1. Once while generating func() within the namespace. This is
21719 the declaration. The declaration bit below is set, as the
21720 context is the namespace.
21722 A new DIE will be generated with DW_AT_declaration set.
21724 2. Once for func() itself. This is the specification. The
21725 declaration bit below is clear as the context is the CU.
21727 We will use the cached DIE from (1) to create a new DIE with
21728 DW_AT_specification pointing to the declaration in (1).
21730 Late debug via rest_of_handle_final()
21731 -------------------------------------
21733 3. Once generating func() within the namespace. This is also the
21734 declaration, as in (1), but this time we will early exit below
21735 as we have a cached DIE and a declaration needs no additional
21736 annotations (no locations), as the source declaration line
21737 info is enough.
21739 4. Once for func() itself. As in (2), this is the specification,
21740 but this time we will re-use the cached DIE, and just annotate
21741 it with the location information that should now be available.
21743 For something without namespaces, but with abstract instances, we
21744 are also called a multiple times:
21746 class Base
21748 public:
21749 Base (); // constructor declaration (1)
21752 Base::Base () { } // constructor specification (2)
21754 Early debug
21755 -----------
21757 1. Once for the Base() constructor by virtue of it being a
21758 member of the Base class. This is done via
21759 rest_of_type_compilation.
21761 This is a declaration, so a new DIE will be created with
21762 DW_AT_declaration.
21764 2. Once for the Base() constructor definition, but this time
21765 while generating the abstract instance of the base
21766 constructor (__base_ctor) which is being generated via early
21767 debug of reachable functions.
21769 Even though we have a cached version of the declaration (1),
21770 we will create a DW_AT_specification of the declaration DIE
21771 in (1).
21773 3. Once for the __base_ctor itself, but this time, we generate
21774 an DW_AT_abstract_origin version of the DW_AT_specification in
21775 (2).
21777 Late debug via rest_of_handle_final
21778 -----------------------------------
21780 4. One final time for the __base_ctor (which will have a cached
21781 DIE with DW_AT_abstract_origin created in (3). This time,
21782 we will just annotate the location information now
21783 available.
21785 int declaration = (current_function_decl != decl
21786 || class_or_namespace_scope_p (context_die));
21788 /* Now that the C++ front end lazily declares artificial member fns, we
21789 might need to retrofit the declaration into its class. */
21790 if (!declaration && !origin && !old_die
21791 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21792 && !class_or_namespace_scope_p (context_die)
21793 && debug_info_level > DINFO_LEVEL_TERSE)
21794 old_die = force_decl_die (decl);
21796 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21797 if (origin != NULL)
21799 gcc_assert (!declaration || local_scope_p (context_die));
21801 /* Fixup die_parent for the abstract instance of a nested
21802 inline function. */
21803 if (old_die && old_die->die_parent == NULL)
21804 add_child_die (context_die, old_die);
21806 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21808 /* If we have a DW_AT_abstract_origin we have a working
21809 cached version. */
21810 subr_die = old_die;
21812 else
21814 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21815 add_abstract_origin_attribute (subr_die, origin);
21816 /* This is where the actual code for a cloned function is.
21817 Let's emit linkage name attribute for it. This helps
21818 debuggers to e.g, set breakpoints into
21819 constructors/destructors when the user asks "break
21820 K::K". */
21821 add_linkage_name (subr_die, decl);
21824 /* A cached copy, possibly from early dwarf generation. Reuse as
21825 much as possible. */
21826 else if (old_die)
21828 /* A declaration that has been previously dumped needs no
21829 additional information. */
21830 if (declaration)
21831 return;
21833 if (!get_AT_flag (old_die, DW_AT_declaration)
21834 /* We can have a normal definition following an inline one in the
21835 case of redefinition of GNU C extern inlines.
21836 It seems reasonable to use AT_specification in this case. */
21837 && !get_AT (old_die, DW_AT_inline))
21839 /* Detect and ignore this case, where we are trying to output
21840 something we have already output. */
21841 if (get_AT (old_die, DW_AT_low_pc)
21842 || get_AT (old_die, DW_AT_ranges))
21843 return;
21845 /* If we have no location information, this must be a
21846 partially generated DIE from early dwarf generation.
21847 Fall through and generate it. */
21850 /* If the definition comes from the same place as the declaration,
21851 maybe use the old DIE. We always want the DIE for this function
21852 that has the *_pc attributes to be under comp_unit_die so the
21853 debugger can find it. We also need to do this for abstract
21854 instances of inlines, since the spec requires the out-of-line copy
21855 to have the same parent. For local class methods, this doesn't
21856 apply; we just use the old DIE. */
21857 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21858 struct dwarf_file_data * file_index = lookup_filename (s.file);
21859 if ((is_cu_die (old_die->die_parent)
21860 /* This condition fixes the inconsistency/ICE with the
21861 following Fortran test (or some derivative thereof) while
21862 building libgfortran:
21864 module some_m
21865 contains
21866 logical function funky (FLAG)
21867 funky = .true.
21868 end function
21869 end module
21871 || (old_die->die_parent
21872 && old_die->die_parent->die_tag == DW_TAG_module)
21873 || context_die == NULL)
21874 && (DECL_ARTIFICIAL (decl)
21875 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21876 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21877 == (unsigned) s.line))))
21879 subr_die = old_die;
21881 /* Clear out the declaration attribute, but leave the
21882 parameters so they can be augmented with location
21883 information later. Unless this was a declaration, in
21884 which case, wipe out the nameless parameters and recreate
21885 them further down. */
21886 if (remove_AT (subr_die, DW_AT_declaration))
21889 remove_AT (subr_die, DW_AT_object_pointer);
21890 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21893 /* Make a specification pointing to the previously built
21894 declaration. */
21895 else
21897 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21898 add_AT_specification (subr_die, old_die);
21899 add_pubname (decl, subr_die);
21900 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21901 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21902 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21903 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21905 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21906 emit the real type on the definition die. */
21907 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
21909 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
21910 if (die == auto_die || die == decltype_auto_die)
21911 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21912 TYPE_UNQUALIFIED, false, context_die);
21915 /* When we process the method declaration, we haven't seen
21916 the out-of-class defaulted definition yet, so we have to
21917 recheck now. */
21918 if ((dwarf_version >= 5 || ! dwarf_strict)
21919 && !get_AT (subr_die, DW_AT_defaulted))
21921 int defaulted
21922 = lang_hooks.decls.decl_dwarf_attribute (decl,
21923 DW_AT_defaulted);
21924 if (defaulted != -1)
21926 /* Other values must have been handled before. */
21927 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
21928 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21933 /* Create a fresh DIE for anything else. */
21934 else
21936 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21938 if (TREE_PUBLIC (decl))
21939 add_AT_flag (subr_die, DW_AT_external, 1);
21941 add_name_and_src_coords_attributes (subr_die, decl);
21942 add_pubname (decl, subr_die);
21943 if (debug_info_level > DINFO_LEVEL_TERSE)
21945 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
21946 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21947 TYPE_UNQUALIFIED, false, context_die);
21950 add_pure_or_virtual_attribute (subr_die, decl);
21951 if (DECL_ARTIFICIAL (decl))
21952 add_AT_flag (subr_die, DW_AT_artificial, 1);
21954 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
21955 add_AT_flag (subr_die, DW_AT_noreturn, 1);
21957 add_accessibility_attribute (subr_die, decl);
21960 /* Unless we have an existing non-declaration DIE, equate the new
21961 DIE. */
21962 if (!old_die || is_declaration_die (old_die))
21963 equate_decl_number_to_die (decl, subr_die);
21965 if (declaration)
21967 if (!old_die || !get_AT (old_die, DW_AT_inline))
21969 add_AT_flag (subr_die, DW_AT_declaration, 1);
21971 /* If this is an explicit function declaration then generate
21972 a DW_AT_explicit attribute. */
21973 if ((dwarf_version >= 3 || !dwarf_strict)
21974 && lang_hooks.decls.decl_dwarf_attribute (decl,
21975 DW_AT_explicit) == 1)
21976 add_AT_flag (subr_die, DW_AT_explicit, 1);
21978 /* If this is a C++11 deleted special function member then generate
21979 a DW_AT_deleted attribute. */
21980 if ((dwarf_version >= 5 || !dwarf_strict)
21981 && lang_hooks.decls.decl_dwarf_attribute (decl,
21982 DW_AT_deleted) == 1)
21983 add_AT_flag (subr_die, DW_AT_deleted, 1);
21985 /* If this is a C++11 defaulted special function member then
21986 generate a DW_AT_defaulted attribute. */
21987 if (dwarf_version >= 5 || !dwarf_strict)
21989 int defaulted
21990 = lang_hooks.decls.decl_dwarf_attribute (decl,
21991 DW_AT_defaulted);
21992 if (defaulted != -1)
21993 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21996 /* If this is a C++11 non-static member function with & ref-qualifier
21997 then generate a DW_AT_reference attribute. */
21998 if ((dwarf_version >= 5 || !dwarf_strict)
21999 && lang_hooks.decls.decl_dwarf_attribute (decl,
22000 DW_AT_reference) == 1)
22001 add_AT_flag (subr_die, DW_AT_reference, 1);
22003 /* If this is a C++11 non-static member function with &&
22004 ref-qualifier then generate a DW_AT_reference attribute. */
22005 if ((dwarf_version >= 5 || !dwarf_strict)
22006 && lang_hooks.decls.decl_dwarf_attribute (decl,
22007 DW_AT_rvalue_reference)
22008 == 1)
22009 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22012 /* Tag abstract instances with DW_AT_inline. */
22013 else if (DECL_ABSTRACT_P (decl))
22015 if (DECL_DECLARED_INLINE_P (decl))
22017 if (cgraph_function_possibly_inlined_p (decl))
22018 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22019 else
22020 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22022 else
22024 if (cgraph_function_possibly_inlined_p (decl))
22025 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22026 else
22027 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22030 if (DECL_DECLARED_INLINE_P (decl)
22031 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22032 add_AT_flag (subr_die, DW_AT_artificial, 1);
22034 /* For non DECL_EXTERNALs, if range information is available, fill
22035 the DIE with it. */
22036 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22038 HOST_WIDE_INT cfa_fb_offset;
22040 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22042 if (!flag_reorder_blocks_and_partition)
22044 dw_fde_ref fde = fun->fde;
22045 if (fde->dw_fde_begin)
22047 /* We have already generated the labels. */
22048 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22049 fde->dw_fde_end, false);
22051 else
22053 /* Create start/end labels and add the range. */
22054 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22055 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22056 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22057 current_function_funcdef_no);
22058 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22059 current_function_funcdef_no);
22060 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22061 false);
22064 #if VMS_DEBUGGING_INFO
22065 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22066 Section 2.3 Prologue and Epilogue Attributes:
22067 When a breakpoint is set on entry to a function, it is generally
22068 desirable for execution to be suspended, not on the very first
22069 instruction of the function, but rather at a point after the
22070 function's frame has been set up, after any language defined local
22071 declaration processing has been completed, and before execution of
22072 the first statement of the function begins. Debuggers generally
22073 cannot properly determine where this point is. Similarly for a
22074 breakpoint set on exit from a function. The prologue and epilogue
22075 attributes allow a compiler to communicate the location(s) to use. */
22078 if (fde->dw_fde_vms_end_prologue)
22079 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22080 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22082 if (fde->dw_fde_vms_begin_epilogue)
22083 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22084 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22086 #endif
22089 else
22091 /* Generate pubnames entries for the split function code ranges. */
22092 dw_fde_ref fde = fun->fde;
22094 if (fde->dw_fde_second_begin)
22096 if (dwarf_version >= 3 || !dwarf_strict)
22098 /* We should use ranges for non-contiguous code section
22099 addresses. Use the actual code range for the initial
22100 section, since the HOT/COLD labels might precede an
22101 alignment offset. */
22102 bool range_list_added = false;
22103 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22104 fde->dw_fde_end, &range_list_added,
22105 false);
22106 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22107 fde->dw_fde_second_end,
22108 &range_list_added, false);
22109 if (range_list_added)
22110 add_ranges (NULL);
22112 else
22114 /* There is no real support in DW2 for this .. so we make
22115 a work-around. First, emit the pub name for the segment
22116 containing the function label. Then make and emit a
22117 simplified subprogram DIE for the second segment with the
22118 name pre-fixed by __hot/cold_sect_of_. We use the same
22119 linkage name for the second die so that gdb will find both
22120 sections when given "b foo". */
22121 const char *name = NULL;
22122 tree decl_name = DECL_NAME (decl);
22123 dw_die_ref seg_die;
22125 /* Do the 'primary' section. */
22126 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22127 fde->dw_fde_end, false);
22129 /* Build a minimal DIE for the secondary section. */
22130 seg_die = new_die (DW_TAG_subprogram,
22131 subr_die->die_parent, decl);
22133 if (TREE_PUBLIC (decl))
22134 add_AT_flag (seg_die, DW_AT_external, 1);
22136 if (decl_name != NULL
22137 && IDENTIFIER_POINTER (decl_name) != NULL)
22139 name = dwarf2_name (decl, 1);
22140 if (! DECL_ARTIFICIAL (decl))
22141 add_src_coords_attributes (seg_die, decl);
22143 add_linkage_name (seg_die, decl);
22145 gcc_assert (name != NULL);
22146 add_pure_or_virtual_attribute (seg_die, decl);
22147 if (DECL_ARTIFICIAL (decl))
22148 add_AT_flag (seg_die, DW_AT_artificial, 1);
22150 name = concat ("__second_sect_of_", name, NULL);
22151 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22152 fde->dw_fde_second_end, false);
22153 add_name_attribute (seg_die, name);
22154 if (want_pubnames ())
22155 add_pubname_string (name, seg_die);
22158 else
22159 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22160 false);
22163 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22165 /* We define the "frame base" as the function's CFA. This is more
22166 convenient for several reasons: (1) It's stable across the prologue
22167 and epilogue, which makes it better than just a frame pointer,
22168 (2) With dwarf3, there exists a one-byte encoding that allows us
22169 to reference the .debug_frame data by proxy, but failing that,
22170 (3) We can at least reuse the code inspection and interpretation
22171 code that determines the CFA position at various points in the
22172 function. */
22173 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22175 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22176 add_AT_loc (subr_die, DW_AT_frame_base, op);
22178 else
22180 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22181 if (list->dw_loc_next)
22182 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22183 else
22184 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22187 /* Compute a displacement from the "steady-state frame pointer" to
22188 the CFA. The former is what all stack slots and argument slots
22189 will reference in the rtl; the latter is what we've told the
22190 debugger about. We'll need to adjust all frame_base references
22191 by this displacement. */
22192 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22194 if (fun->static_chain_decl)
22196 /* DWARF requires here a location expression that computes the
22197 address of the enclosing subprogram's frame base. The machinery
22198 in tree-nested.c is supposed to store this specific address in the
22199 last field of the FRAME record. */
22200 const tree frame_type
22201 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22202 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22204 tree fb_expr
22205 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22206 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22207 fb_expr, fb_decl, NULL_TREE);
22209 add_AT_location_description (subr_die, DW_AT_static_link,
22210 loc_list_from_tree (fb_expr, 0, NULL));
22214 /* Generate child dies for template paramaters. */
22215 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22216 gen_generic_params_dies (decl);
22218 /* Now output descriptions of the arguments for this function. This gets
22219 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22220 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22221 `...' at the end of the formal parameter list. In order to find out if
22222 there was a trailing ellipsis or not, we must instead look at the type
22223 associated with the FUNCTION_DECL. This will be a node of type
22224 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22225 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22226 an ellipsis at the end. */
22228 /* In the case where we are describing a mere function declaration, all we
22229 need to do here (and all we *can* do here) is to describe the *types* of
22230 its formal parameters. */
22231 if (debug_info_level <= DINFO_LEVEL_TERSE)
22233 else if (declaration)
22234 gen_formal_types_die (decl, subr_die);
22235 else
22237 /* Generate DIEs to represent all known formal parameters. */
22238 tree parm = DECL_ARGUMENTS (decl);
22239 tree generic_decl = early_dwarf
22240 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22241 tree generic_decl_parm = generic_decl
22242 ? DECL_ARGUMENTS (generic_decl)
22243 : NULL;
22244 auto_vec<dw_die_ref> string_types_vec;
22245 if (string_types == NULL)
22246 string_types = &string_types_vec;
22248 /* Now we want to walk the list of parameters of the function and
22249 emit their relevant DIEs.
22251 We consider the case of DECL being an instance of a generic function
22252 as well as it being a normal function.
22254 If DECL is an instance of a generic function we walk the
22255 parameters of the generic function declaration _and_ the parameters of
22256 DECL itself. This is useful because we want to emit specific DIEs for
22257 function parameter packs and those are declared as part of the
22258 generic function declaration. In that particular case,
22259 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22260 That DIE has children DIEs representing the set of arguments
22261 of the pack. Note that the set of pack arguments can be empty.
22262 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22263 children DIE.
22265 Otherwise, we just consider the parameters of DECL. */
22266 while (generic_decl_parm || parm)
22268 if (generic_decl_parm
22269 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22270 gen_formal_parameter_pack_die (generic_decl_parm,
22271 parm, subr_die,
22272 &parm);
22273 else if (parm && !POINTER_BOUNDS_P (parm))
22275 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22277 if (parm == DECL_ARGUMENTS (decl)
22278 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22279 && parm_die
22280 && (dwarf_version >= 3 || !dwarf_strict))
22281 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22283 parm = DECL_CHAIN (parm);
22285 else if (parm)
22286 parm = DECL_CHAIN (parm);
22288 if (generic_decl_parm)
22289 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22292 /* Decide whether we need an unspecified_parameters DIE at the end.
22293 There are 2 more cases to do this for: 1) the ansi ... declaration -
22294 this is detectable when the end of the arg list is not a
22295 void_type_node 2) an unprototyped function declaration (not a
22296 definition). This just means that we have no info about the
22297 parameters at all. */
22298 if (early_dwarf)
22300 if (prototype_p (TREE_TYPE (decl)))
22302 /* This is the prototyped case, check for.... */
22303 if (stdarg_p (TREE_TYPE (decl)))
22304 gen_unspecified_parameters_die (decl, subr_die);
22306 else if (DECL_INITIAL (decl) == NULL_TREE)
22307 gen_unspecified_parameters_die (decl, subr_die);
22310 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
22311 have DIEs. */
22312 if (string_types == &string_types_vec)
22314 adjust_string_types ();
22315 string_types = NULL;
22319 if (subr_die != old_die)
22320 /* Add the calling convention attribute if requested. */
22321 add_calling_convention_attribute (subr_die, decl);
22323 /* Output Dwarf info for all of the stuff within the body of the function
22324 (if it has one - it may be just a declaration).
22326 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22327 a function. This BLOCK actually represents the outermost binding contour
22328 for the function, i.e. the contour in which the function's formal
22329 parameters and labels get declared. Curiously, it appears that the front
22330 end doesn't actually put the PARM_DECL nodes for the current function onto
22331 the BLOCK_VARS list for this outer scope, but are strung off of the
22332 DECL_ARGUMENTS list for the function instead.
22334 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22335 the LABEL_DECL nodes for the function however, and we output DWARF info
22336 for those in decls_for_scope. Just within the `outer_scope' there will be
22337 a BLOCK node representing the function's outermost pair of curly braces,
22338 and any blocks used for the base and member initializers of a C++
22339 constructor function. */
22340 tree outer_scope = DECL_INITIAL (decl);
22341 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22343 int call_site_note_count = 0;
22344 int tail_call_site_note_count = 0;
22346 /* Emit a DW_TAG_variable DIE for a named return value. */
22347 if (DECL_NAME (DECL_RESULT (decl)))
22348 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22350 /* The first time through decls_for_scope we will generate the
22351 DIEs for the locals. The second time, we fill in the
22352 location info. */
22353 decls_for_scope (outer_scope, subr_die);
22355 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22357 struct call_arg_loc_node *ca_loc;
22358 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22360 dw_die_ref die = NULL;
22361 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22362 rtx arg, next_arg;
22364 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22365 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22366 : NULL_RTX);
22367 arg; arg = next_arg)
22369 dw_loc_descr_ref reg, val;
22370 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22371 dw_die_ref cdie, tdie = NULL;
22373 next_arg = XEXP (arg, 1);
22374 if (REG_P (XEXP (XEXP (arg, 0), 0))
22375 && next_arg
22376 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22377 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22378 && REGNO (XEXP (XEXP (arg, 0), 0))
22379 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22380 next_arg = XEXP (next_arg, 1);
22381 if (mode == VOIDmode)
22383 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22384 if (mode == VOIDmode)
22385 mode = GET_MODE (XEXP (arg, 0));
22387 if (mode == VOIDmode || mode == BLKmode)
22388 continue;
22389 /* Get dynamic information about call target only if we
22390 have no static information: we cannot generate both
22391 DW_AT_call_origin and DW_AT_call_target
22392 attributes. */
22393 if (ca_loc->symbol_ref == NULL_RTX)
22395 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22397 tloc = XEXP (XEXP (arg, 0), 1);
22398 continue;
22400 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22401 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22403 tlocc = XEXP (XEXP (arg, 0), 1);
22404 continue;
22407 reg = NULL;
22408 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22409 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22410 VAR_INIT_STATUS_INITIALIZED);
22411 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22413 rtx mem = XEXP (XEXP (arg, 0), 0);
22414 reg = mem_loc_descriptor (XEXP (mem, 0),
22415 get_address_mode (mem),
22416 GET_MODE (mem),
22417 VAR_INIT_STATUS_INITIALIZED);
22419 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22420 == DEBUG_PARAMETER_REF)
22422 tree tdecl
22423 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22424 tdie = lookup_decl_die (tdecl);
22425 if (tdie == NULL)
22426 continue;
22428 else
22429 continue;
22430 if (reg == NULL
22431 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22432 != DEBUG_PARAMETER_REF)
22433 continue;
22434 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22435 VOIDmode,
22436 VAR_INIT_STATUS_INITIALIZED);
22437 if (val == NULL)
22438 continue;
22439 if (die == NULL)
22440 die = gen_call_site_die (decl, subr_die, ca_loc);
22441 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22442 NULL_TREE);
22443 if (reg != NULL)
22444 add_AT_loc (cdie, DW_AT_location, reg);
22445 else if (tdie != NULL)
22446 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22447 tdie);
22448 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22449 if (next_arg != XEXP (arg, 1))
22451 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22452 if (mode == VOIDmode)
22453 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22454 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22455 0), 1),
22456 mode, VOIDmode,
22457 VAR_INIT_STATUS_INITIALIZED);
22458 if (val != NULL)
22459 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22460 val);
22463 if (die == NULL
22464 && (ca_loc->symbol_ref || tloc))
22465 die = gen_call_site_die (decl, subr_die, ca_loc);
22466 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22468 dw_loc_descr_ref tval = NULL;
22470 if (tloc != NULL_RTX)
22471 tval = mem_loc_descriptor (tloc,
22472 GET_MODE (tloc) == VOIDmode
22473 ? Pmode : GET_MODE (tloc),
22474 VOIDmode,
22475 VAR_INIT_STATUS_INITIALIZED);
22476 if (tval)
22477 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22478 else if (tlocc != NULL_RTX)
22480 tval = mem_loc_descriptor (tlocc,
22481 GET_MODE (tlocc) == VOIDmode
22482 ? Pmode : GET_MODE (tlocc),
22483 VOIDmode,
22484 VAR_INIT_STATUS_INITIALIZED);
22485 if (tval)
22486 add_AT_loc (die,
22487 dwarf_AT (DW_AT_call_target_clobbered),
22488 tval);
22491 if (die != NULL)
22493 call_site_note_count++;
22494 if (ca_loc->tail_call_p)
22495 tail_call_site_note_count++;
22499 call_arg_locations = NULL;
22500 call_arg_loc_last = NULL;
22501 if (tail_call_site_count >= 0
22502 && tail_call_site_count == tail_call_site_note_count
22503 && (!dwarf_strict || dwarf_version >= 5))
22505 if (call_site_count >= 0
22506 && call_site_count == call_site_note_count)
22507 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22508 else
22509 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22511 call_site_count = -1;
22512 tail_call_site_count = -1;
22515 /* Mark used types after we have created DIEs for the functions scopes. */
22516 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22519 /* Returns a hash value for X (which really is a die_struct). */
22521 hashval_t
22522 block_die_hasher::hash (die_struct *d)
22524 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22527 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22528 as decl_id and die_parent of die_struct Y. */
22530 bool
22531 block_die_hasher::equal (die_struct *x, die_struct *y)
22533 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22536 /* Return TRUE if DECL, which may have been previously generated as
22537 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22538 true if decl (or its origin) is either an extern declaration or a
22539 class/namespace scoped declaration.
22541 The declare_in_namespace support causes us to get two DIEs for one
22542 variable, both of which are declarations. We want to avoid
22543 considering one to be a specification, so we must test for
22544 DECLARATION and DW_AT_declaration. */
22545 static inline bool
22546 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22548 return (old_die && TREE_STATIC (decl) && !declaration
22549 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22552 /* Return true if DECL is a local static. */
22554 static inline bool
22555 local_function_static (tree decl)
22557 gcc_assert (VAR_P (decl));
22558 return TREE_STATIC (decl)
22559 && DECL_CONTEXT (decl)
22560 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22563 /* Generate a DIE to represent a declared data object.
22564 Either DECL or ORIGIN must be non-null. */
22566 static void
22567 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22569 HOST_WIDE_INT off = 0;
22570 tree com_decl;
22571 tree decl_or_origin = decl ? decl : origin;
22572 tree ultimate_origin;
22573 dw_die_ref var_die;
22574 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22575 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22576 || class_or_namespace_scope_p (context_die));
22577 bool specialization_p = false;
22578 bool no_linkage_name = false;
22580 /* While C++ inline static data members have definitions inside of the
22581 class, force the first DIE to be a declaration, then let gen_member_die
22582 reparent it to the class context and call gen_variable_die again
22583 to create the outside of the class DIE for the definition. */
22584 if (!declaration
22585 && old_die == NULL
22586 && decl
22587 && DECL_CONTEXT (decl)
22588 && TYPE_P (DECL_CONTEXT (decl))
22589 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22591 declaration = true;
22592 no_linkage_name = true;
22595 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22596 if (decl || ultimate_origin)
22597 origin = ultimate_origin;
22598 com_decl = fortran_common (decl_or_origin, &off);
22600 /* Symbol in common gets emitted as a child of the common block, in the form
22601 of a data member. */
22602 if (com_decl)
22604 dw_die_ref com_die;
22605 dw_loc_list_ref loc = NULL;
22606 die_node com_die_arg;
22608 var_die = lookup_decl_die (decl_or_origin);
22609 if (var_die)
22611 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22613 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22614 if (loc)
22616 if (off)
22618 /* Optimize the common case. */
22619 if (single_element_loc_list_p (loc)
22620 && loc->expr->dw_loc_opc == DW_OP_addr
22621 && loc->expr->dw_loc_next == NULL
22622 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22623 == SYMBOL_REF)
22625 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22626 loc->expr->dw_loc_oprnd1.v.val_addr
22627 = plus_constant (GET_MODE (x), x , off);
22629 else
22630 loc_list_plus_const (loc, off);
22632 add_AT_location_description (var_die, DW_AT_location, loc);
22633 remove_AT (var_die, DW_AT_declaration);
22636 return;
22639 if (common_block_die_table == NULL)
22640 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22642 com_die_arg.decl_id = DECL_UID (com_decl);
22643 com_die_arg.die_parent = context_die;
22644 com_die = common_block_die_table->find (&com_die_arg);
22645 if (! early_dwarf)
22646 loc = loc_list_from_tree (com_decl, 2, NULL);
22647 if (com_die == NULL)
22649 const char *cnam
22650 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22651 die_node **slot;
22653 com_die = new_die (DW_TAG_common_block, context_die, decl);
22654 add_name_and_src_coords_attributes (com_die, com_decl);
22655 if (loc)
22657 add_AT_location_description (com_die, DW_AT_location, loc);
22658 /* Avoid sharing the same loc descriptor between
22659 DW_TAG_common_block and DW_TAG_variable. */
22660 loc = loc_list_from_tree (com_decl, 2, NULL);
22662 else if (DECL_EXTERNAL (decl_or_origin))
22663 add_AT_flag (com_die, DW_AT_declaration, 1);
22664 if (want_pubnames ())
22665 add_pubname_string (cnam, com_die); /* ??? needed? */
22666 com_die->decl_id = DECL_UID (com_decl);
22667 slot = common_block_die_table->find_slot (com_die, INSERT);
22668 *slot = com_die;
22670 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22672 add_AT_location_description (com_die, DW_AT_location, loc);
22673 loc = loc_list_from_tree (com_decl, 2, NULL);
22674 remove_AT (com_die, DW_AT_declaration);
22676 var_die = new_die (DW_TAG_variable, com_die, decl);
22677 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22678 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22679 decl_quals (decl_or_origin), false,
22680 context_die);
22681 add_AT_flag (var_die, DW_AT_external, 1);
22682 if (loc)
22684 if (off)
22686 /* Optimize the common case. */
22687 if (single_element_loc_list_p (loc)
22688 && loc->expr->dw_loc_opc == DW_OP_addr
22689 && loc->expr->dw_loc_next == NULL
22690 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22692 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22693 loc->expr->dw_loc_oprnd1.v.val_addr
22694 = plus_constant (GET_MODE (x), x, off);
22696 else
22697 loc_list_plus_const (loc, off);
22699 add_AT_location_description (var_die, DW_AT_location, loc);
22701 else if (DECL_EXTERNAL (decl_or_origin))
22702 add_AT_flag (var_die, DW_AT_declaration, 1);
22703 if (decl)
22704 equate_decl_number_to_die (decl, var_die);
22705 return;
22708 if (old_die)
22710 if (declaration)
22712 /* A declaration that has been previously dumped, needs no
22713 further annotations, since it doesn't need location on
22714 the second pass. */
22715 return;
22717 else if (decl_will_get_specification_p (old_die, decl, declaration)
22718 && !get_AT (old_die, DW_AT_specification))
22720 /* Fall-thru so we can make a new variable die along with a
22721 DW_AT_specification. */
22723 else if (origin && old_die->die_parent != context_die)
22725 /* If we will be creating an inlined instance, we need a
22726 new DIE that will get annotated with
22727 DW_AT_abstract_origin. Clear things so we can get a
22728 new DIE. */
22729 gcc_assert (!DECL_ABSTRACT_P (decl));
22730 old_die = NULL;
22732 else
22734 /* If a DIE was dumped early, it still needs location info.
22735 Skip to where we fill the location bits. */
22736 var_die = old_die;
22737 goto gen_variable_die_location;
22741 /* For static data members, the declaration in the class is supposed
22742 to have DW_TAG_member tag; the specification should still be
22743 DW_TAG_variable referencing the DW_TAG_member DIE. */
22744 if (declaration && class_scope_p (context_die))
22745 var_die = new_die (DW_TAG_member, context_die, decl);
22746 else
22747 var_die = new_die (DW_TAG_variable, context_die, decl);
22749 if (origin != NULL)
22750 add_abstract_origin_attribute (var_die, origin);
22752 /* Loop unrolling can create multiple blocks that refer to the same
22753 static variable, so we must test for the DW_AT_declaration flag.
22755 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22756 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22757 sharing them.
22759 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22760 else if (decl_will_get_specification_p (old_die, decl, declaration))
22762 /* This is a definition of a C++ class level static. */
22763 add_AT_specification (var_die, old_die);
22764 specialization_p = true;
22765 if (DECL_NAME (decl))
22767 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22768 struct dwarf_file_data * file_index = lookup_filename (s.file);
22770 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22771 add_AT_file (var_die, DW_AT_decl_file, file_index);
22773 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22774 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22776 if (old_die->die_tag == DW_TAG_member)
22777 add_linkage_name (var_die, decl);
22780 else
22781 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22783 if ((origin == NULL && !specialization_p)
22784 || (origin != NULL
22785 && !DECL_ABSTRACT_P (decl_or_origin)
22786 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22787 decl_function_context
22788 (decl_or_origin))))
22790 tree type = TREE_TYPE (decl_or_origin);
22792 if (decl_by_reference_p (decl_or_origin))
22793 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22794 context_die);
22795 else
22796 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22797 context_die);
22800 if (origin == NULL && !specialization_p)
22802 if (TREE_PUBLIC (decl))
22803 add_AT_flag (var_die, DW_AT_external, 1);
22805 if (DECL_ARTIFICIAL (decl))
22806 add_AT_flag (var_die, DW_AT_artificial, 1);
22808 add_accessibility_attribute (var_die, decl);
22811 if (declaration)
22812 add_AT_flag (var_die, DW_AT_declaration, 1);
22814 if (decl && (DECL_ABSTRACT_P (decl)
22815 || !old_die || is_declaration_die (old_die)))
22816 equate_decl_number_to_die (decl, var_die);
22818 gen_variable_die_location:
22819 if (! declaration
22820 && (! DECL_ABSTRACT_P (decl_or_origin)
22821 /* Local static vars are shared between all clones/inlines,
22822 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22823 already set. */
22824 || (VAR_P (decl_or_origin)
22825 && TREE_STATIC (decl_or_origin)
22826 && DECL_RTL_SET_P (decl_or_origin))))
22828 if (early_dwarf)
22829 add_pubname (decl_or_origin, var_die);
22830 else
22831 add_location_or_const_value_attribute (var_die, decl_or_origin,
22832 decl == NULL);
22834 else
22835 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22837 if ((dwarf_version >= 4 || !dwarf_strict)
22838 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22839 DW_AT_const_expr) == 1
22840 && !get_AT (var_die, DW_AT_const_expr)
22841 && !specialization_p)
22842 add_AT_flag (var_die, DW_AT_const_expr, 1);
22844 if (!dwarf_strict)
22846 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22847 DW_AT_inline);
22848 if (inl != -1
22849 && !get_AT (var_die, DW_AT_inline)
22850 && !specialization_p)
22851 add_AT_unsigned (var_die, DW_AT_inline, inl);
22855 /* Generate a DIE to represent a named constant. */
22857 static void
22858 gen_const_die (tree decl, dw_die_ref context_die)
22860 dw_die_ref const_die;
22861 tree type = TREE_TYPE (decl);
22863 const_die = lookup_decl_die (decl);
22864 if (const_die)
22865 return;
22867 const_die = new_die (DW_TAG_constant, context_die, decl);
22868 equate_decl_number_to_die (decl, const_die);
22869 add_name_and_src_coords_attributes (const_die, decl);
22870 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22871 if (TREE_PUBLIC (decl))
22872 add_AT_flag (const_die, DW_AT_external, 1);
22873 if (DECL_ARTIFICIAL (decl))
22874 add_AT_flag (const_die, DW_AT_artificial, 1);
22875 tree_add_const_value_attribute_for_decl (const_die, decl);
22878 /* Generate a DIE to represent a label identifier. */
22880 static void
22881 gen_label_die (tree decl, dw_die_ref context_die)
22883 tree origin = decl_ultimate_origin (decl);
22884 dw_die_ref lbl_die = lookup_decl_die (decl);
22885 rtx insn;
22886 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22888 if (!lbl_die)
22890 lbl_die = new_die (DW_TAG_label, context_die, decl);
22891 equate_decl_number_to_die (decl, lbl_die);
22893 if (origin != NULL)
22894 add_abstract_origin_attribute (lbl_die, origin);
22895 else
22896 add_name_and_src_coords_attributes (lbl_die, decl);
22899 if (DECL_ABSTRACT_P (decl))
22900 equate_decl_number_to_die (decl, lbl_die);
22901 else if (! early_dwarf)
22903 insn = DECL_RTL_IF_SET (decl);
22905 /* Deleted labels are programmer specified labels which have been
22906 eliminated because of various optimizations. We still emit them
22907 here so that it is possible to put breakpoints on them. */
22908 if (insn
22909 && (LABEL_P (insn)
22910 || ((NOTE_P (insn)
22911 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
22913 /* When optimization is enabled (via -O) some parts of the compiler
22914 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
22915 represent source-level labels which were explicitly declared by
22916 the user. This really shouldn't be happening though, so catch
22917 it if it ever does happen. */
22918 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
22920 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
22921 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22923 else if (insn
22924 && NOTE_P (insn)
22925 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
22926 && CODE_LABEL_NUMBER (insn) != -1)
22928 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
22929 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22934 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
22935 attributes to the DIE for a block STMT, to describe where the inlined
22936 function was called from. This is similar to add_src_coords_attributes. */
22938 static inline void
22939 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
22941 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
22943 if (dwarf_version >= 3 || !dwarf_strict)
22945 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
22946 add_AT_unsigned (die, DW_AT_call_line, s.line);
22951 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
22952 Add low_pc and high_pc attributes to the DIE for a block STMT. */
22954 static inline void
22955 add_high_low_attributes (tree stmt, dw_die_ref die)
22957 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22959 if (BLOCK_FRAGMENT_CHAIN (stmt)
22960 && (dwarf_version >= 3 || !dwarf_strict))
22962 tree chain, superblock = NULL_TREE;
22963 dw_die_ref pdie;
22964 dw_attr_node *attr = NULL;
22966 if (inlined_function_outer_scope_p (stmt))
22968 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22969 BLOCK_NUMBER (stmt));
22970 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22973 /* Optimize duplicate .debug_ranges lists or even tails of
22974 lists. If this BLOCK has same ranges as its supercontext,
22975 lookup DW_AT_ranges attribute in the supercontext (and
22976 recursively so), verify that the ranges_table contains the
22977 right values and use it instead of adding a new .debug_range. */
22978 for (chain = stmt, pdie = die;
22979 BLOCK_SAME_RANGE (chain);
22980 chain = BLOCK_SUPERCONTEXT (chain))
22982 dw_attr_node *new_attr;
22984 pdie = pdie->die_parent;
22985 if (pdie == NULL)
22986 break;
22987 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
22988 break;
22989 new_attr = get_AT (pdie, DW_AT_ranges);
22990 if (new_attr == NULL
22991 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
22992 break;
22993 attr = new_attr;
22994 superblock = BLOCK_SUPERCONTEXT (chain);
22996 if (attr != NULL
22997 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
22998 == BLOCK_NUMBER (superblock))
22999 && BLOCK_FRAGMENT_CHAIN (superblock))
23001 unsigned long off = attr->dw_attr_val.v.val_offset;
23002 unsigned long supercnt = 0, thiscnt = 0;
23003 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23004 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23006 ++supercnt;
23007 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23008 == BLOCK_NUMBER (chain));
23010 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23011 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23012 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23013 ++thiscnt;
23014 gcc_assert (supercnt >= thiscnt);
23015 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23016 false);
23017 note_rnglist_head (off + supercnt - thiscnt);
23018 return;
23021 unsigned int offset = add_ranges (stmt, true);
23022 add_AT_range_list (die, DW_AT_ranges, offset, false);
23023 note_rnglist_head (offset);
23025 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23026 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23029 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23030 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23031 chain = BLOCK_FRAGMENT_CHAIN (chain);
23033 while (chain);
23034 add_ranges (NULL);
23036 else
23038 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23039 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23040 BLOCK_NUMBER (stmt));
23041 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23042 BLOCK_NUMBER (stmt));
23043 add_AT_low_high_pc (die, label, label_high, false);
23047 /* Generate a DIE for a lexical block. */
23049 static void
23050 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23052 dw_die_ref old_die = BLOCK_DIE (stmt);
23053 dw_die_ref stmt_die = NULL;
23054 if (!old_die)
23056 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23057 BLOCK_DIE (stmt) = stmt_die;
23060 if (BLOCK_ABSTRACT (stmt))
23062 if (old_die)
23064 /* This must have been generated early and it won't even
23065 need location information since it's a DW_AT_inline
23066 function. */
23067 if (flag_checking)
23068 for (dw_die_ref c = context_die; c; c = c->die_parent)
23069 if (c->die_tag == DW_TAG_inlined_subroutine
23070 || c->die_tag == DW_TAG_subprogram)
23072 gcc_assert (get_AT (c, DW_AT_inline));
23073 break;
23075 return;
23078 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23080 /* If this is an inlined instance, create a new lexical die for
23081 anything below to attach DW_AT_abstract_origin to. */
23082 if (old_die)
23084 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23085 BLOCK_DIE (stmt) = stmt_die;
23086 old_die = NULL;
23089 tree origin = block_ultimate_origin (stmt);
23090 if (origin != NULL_TREE && origin != stmt)
23091 add_abstract_origin_attribute (stmt_die, origin);
23094 if (old_die)
23095 stmt_die = old_die;
23097 /* A non abstract block whose blocks have already been reordered
23098 should have the instruction range for this block. If so, set the
23099 high/low attributes. */
23100 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23102 gcc_assert (stmt_die);
23103 add_high_low_attributes (stmt, stmt_die);
23106 decls_for_scope (stmt, stmt_die);
23109 /* Generate a DIE for an inlined subprogram. */
23111 static void
23112 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23114 tree decl;
23116 /* The instance of function that is effectively being inlined shall not
23117 be abstract. */
23118 gcc_assert (! BLOCK_ABSTRACT (stmt));
23120 decl = block_ultimate_origin (stmt);
23122 /* Make sure any inlined functions are known to be inlineable. */
23123 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23124 || cgraph_function_possibly_inlined_p (decl));
23126 /* Emit info for the abstract instance first, if we haven't yet. We
23127 must emit this even if the block is abstract, otherwise when we
23128 emit the block below (or elsewhere), we may end up trying to emit
23129 a die whose origin die hasn't been emitted, and crashing. */
23130 dwarf2out_abstract_function (decl);
23132 if (! BLOCK_ABSTRACT (stmt))
23134 dw_die_ref subr_die
23135 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23137 if (call_arg_locations)
23138 BLOCK_DIE (stmt) = subr_die;
23139 add_abstract_origin_attribute (subr_die, decl);
23140 if (TREE_ASM_WRITTEN (stmt))
23141 add_high_low_attributes (stmt, subr_die);
23142 add_call_src_coords_attributes (stmt, subr_die);
23144 decls_for_scope (stmt, subr_die);
23148 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23149 the comment for VLR_CONTEXT. */
23151 static void
23152 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23154 dw_die_ref decl_die;
23156 if (TREE_TYPE (decl) == error_mark_node)
23157 return;
23159 decl_die = new_die (DW_TAG_member, context_die, decl);
23160 add_name_and_src_coords_attributes (decl_die, decl);
23161 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23162 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23163 context_die);
23165 if (DECL_BIT_FIELD_TYPE (decl))
23167 add_byte_size_attribute (decl_die, decl);
23168 add_bit_size_attribute (decl_die, decl);
23169 add_bit_offset_attribute (decl_die, decl, ctx);
23172 /* If we have a variant part offset, then we are supposed to process a member
23173 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23174 trees. */
23175 gcc_assert (ctx->variant_part_offset == NULL_TREE
23176 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23177 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23178 add_data_member_location_attribute (decl_die, decl, ctx);
23180 if (DECL_ARTIFICIAL (decl))
23181 add_AT_flag (decl_die, DW_AT_artificial, 1);
23183 add_accessibility_attribute (decl_die, decl);
23185 /* Equate decl number to die, so that we can look up this decl later on. */
23186 equate_decl_number_to_die (decl, decl_die);
23189 #if 0
23190 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23191 Use modified_type_die instead.
23192 We keep this code here just in case these types of DIEs may be needed to
23193 represent certain things in other languages (e.g. Pascal) someday. */
23195 static void
23196 gen_pointer_type_die (tree type, dw_die_ref context_die)
23198 dw_die_ref ptr_die
23199 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23201 equate_type_number_to_die (type, ptr_die);
23202 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23203 context_die);
23204 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23207 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23208 Use modified_type_die instead.
23209 We keep this code here just in case these types of DIEs may be needed to
23210 represent certain things in other languages (e.g. Pascal) someday. */
23212 static void
23213 gen_reference_type_die (tree type, dw_die_ref context_die)
23215 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23217 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23218 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23219 else
23220 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23222 equate_type_number_to_die (type, ref_die);
23223 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23224 context_die);
23225 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23227 #endif
23229 /* Generate a DIE for a pointer to a member type. TYPE can be an
23230 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23231 pointer to member function. */
23233 static void
23234 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23236 if (lookup_type_die (type))
23237 return;
23239 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23240 scope_die_for (type, context_die), type);
23242 equate_type_number_to_die (type, ptr_die);
23243 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23244 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23245 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23246 context_die);
23248 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23249 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23251 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23252 add_AT_loc (ptr_die, DW_AT_use_location, op);
23256 static char *producer_string;
23258 /* Return a heap allocated producer string including command line options
23259 if -grecord-gcc-switches. */
23261 static char *
23262 gen_producer_string (void)
23264 size_t j;
23265 auto_vec<const char *> switches;
23266 const char *language_string = lang_hooks.name;
23267 char *producer, *tail;
23268 const char *p;
23269 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23270 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23272 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23273 switch (save_decoded_options[j].opt_index)
23275 case OPT_o:
23276 case OPT_d:
23277 case OPT_dumpbase:
23278 case OPT_dumpdir:
23279 case OPT_auxbase:
23280 case OPT_auxbase_strip:
23281 case OPT_quiet:
23282 case OPT_version:
23283 case OPT_v:
23284 case OPT_w:
23285 case OPT_L:
23286 case OPT_D:
23287 case OPT_I:
23288 case OPT_U:
23289 case OPT_SPECIAL_unknown:
23290 case OPT_SPECIAL_ignore:
23291 case OPT_SPECIAL_program_name:
23292 case OPT_SPECIAL_input_file:
23293 case OPT_grecord_gcc_switches:
23294 case OPT_gno_record_gcc_switches:
23295 case OPT__output_pch_:
23296 case OPT_fdiagnostics_show_location_:
23297 case OPT_fdiagnostics_show_option:
23298 case OPT_fdiagnostics_show_caret:
23299 case OPT_fdiagnostics_color_:
23300 case OPT_fverbose_asm:
23301 case OPT____:
23302 case OPT__sysroot_:
23303 case OPT_nostdinc:
23304 case OPT_nostdinc__:
23305 case OPT_fpreprocessed:
23306 case OPT_fltrans_output_list_:
23307 case OPT_fresolution_:
23308 case OPT_fdebug_prefix_map_:
23309 /* Ignore these. */
23310 continue;
23311 default:
23312 if (cl_options[save_decoded_options[j].opt_index].flags
23313 & CL_NO_DWARF_RECORD)
23314 continue;
23315 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23316 == '-');
23317 switch (save_decoded_options[j].canonical_option[0][1])
23319 case 'M':
23320 case 'i':
23321 case 'W':
23322 continue;
23323 case 'f':
23324 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23325 "dump", 4) == 0)
23326 continue;
23327 break;
23328 default:
23329 break;
23331 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23332 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23333 break;
23336 producer = XNEWVEC (char, plen + 1 + len + 1);
23337 tail = producer;
23338 sprintf (tail, "%s %s", language_string, version_string);
23339 tail += plen;
23341 FOR_EACH_VEC_ELT (switches, j, p)
23343 len = strlen (p);
23344 *tail = ' ';
23345 memcpy (tail + 1, p, len);
23346 tail += len + 1;
23349 *tail = '\0';
23350 return producer;
23353 /* Given a C and/or C++ language/version string return the "highest".
23354 C++ is assumed to be "higher" than C in this case. Used for merging
23355 LTO translation unit languages. */
23356 static const char *
23357 highest_c_language (const char *lang1, const char *lang2)
23359 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23360 return "GNU C++14";
23361 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23362 return "GNU C++11";
23363 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23364 return "GNU C++98";
23366 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23367 return "GNU C11";
23368 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23369 return "GNU C99";
23370 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23371 return "GNU C89";
23373 gcc_unreachable ();
23377 /* Generate the DIE for the compilation unit. */
23379 static dw_die_ref
23380 gen_compile_unit_die (const char *filename)
23382 dw_die_ref die;
23383 const char *language_string = lang_hooks.name;
23384 int language;
23386 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23388 if (filename)
23390 add_name_attribute (die, filename);
23391 /* Don't add cwd for <built-in>. */
23392 if (filename[0] != '<')
23393 add_comp_dir_attribute (die);
23396 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23398 /* If our producer is LTO try to figure out a common language to use
23399 from the global list of translation units. */
23400 if (strcmp (language_string, "GNU GIMPLE") == 0)
23402 unsigned i;
23403 tree t;
23404 const char *common_lang = NULL;
23406 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23408 if (!TRANSLATION_UNIT_LANGUAGE (t))
23409 continue;
23410 if (!common_lang)
23411 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23412 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23414 else if (strncmp (common_lang, "GNU C", 5) == 0
23415 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23416 /* Mixing C and C++ is ok, use C++ in that case. */
23417 common_lang = highest_c_language (common_lang,
23418 TRANSLATION_UNIT_LANGUAGE (t));
23419 else
23421 /* Fall back to C. */
23422 common_lang = NULL;
23423 break;
23427 if (common_lang)
23428 language_string = common_lang;
23431 language = DW_LANG_C;
23432 if (strncmp (language_string, "GNU C", 5) == 0
23433 && ISDIGIT (language_string[5]))
23435 language = DW_LANG_C89;
23436 if (dwarf_version >= 3 || !dwarf_strict)
23438 if (strcmp (language_string, "GNU C89") != 0)
23439 language = DW_LANG_C99;
23441 if (dwarf_version >= 5 /* || !dwarf_strict */)
23442 if (strcmp (language_string, "GNU C11") == 0)
23443 language = DW_LANG_C11;
23446 else if (strncmp (language_string, "GNU C++", 7) == 0)
23448 language = DW_LANG_C_plus_plus;
23449 if (dwarf_version >= 5 /* || !dwarf_strict */)
23451 if (strcmp (language_string, "GNU C++11") == 0)
23452 language = DW_LANG_C_plus_plus_11;
23453 else if (strcmp (language_string, "GNU C++14") == 0)
23454 language = DW_LANG_C_plus_plus_14;
23457 else if (strcmp (language_string, "GNU F77") == 0)
23458 language = DW_LANG_Fortran77;
23459 else if (strcmp (language_string, "GNU Pascal") == 0)
23460 language = DW_LANG_Pascal83;
23461 else if (dwarf_version >= 3 || !dwarf_strict)
23463 if (strcmp (language_string, "GNU Ada") == 0)
23464 language = DW_LANG_Ada95;
23465 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23467 language = DW_LANG_Fortran95;
23468 if (dwarf_version >= 5 /* || !dwarf_strict */)
23470 if (strcmp (language_string, "GNU Fortran2003") == 0)
23471 language = DW_LANG_Fortran03;
23472 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23473 language = DW_LANG_Fortran08;
23476 else if (strcmp (language_string, "GNU Java") == 0)
23477 language = DW_LANG_Java;
23478 else if (strcmp (language_string, "GNU Objective-C") == 0)
23479 language = DW_LANG_ObjC;
23480 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23481 language = DW_LANG_ObjC_plus_plus;
23482 else if (dwarf_version >= 5 || !dwarf_strict)
23484 if (strcmp (language_string, "GNU Go") == 0)
23485 language = DW_LANG_Go;
23488 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23489 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23490 language = DW_LANG_Fortran90;
23492 add_AT_unsigned (die, DW_AT_language, language);
23494 switch (language)
23496 case DW_LANG_Fortran77:
23497 case DW_LANG_Fortran90:
23498 case DW_LANG_Fortran95:
23499 case DW_LANG_Fortran03:
23500 case DW_LANG_Fortran08:
23501 /* Fortran has case insensitive identifiers and the front-end
23502 lowercases everything. */
23503 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23504 break;
23505 default:
23506 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23507 break;
23509 return die;
23512 /* Generate the DIE for a base class. */
23514 static void
23515 gen_inheritance_die (tree binfo, tree access, tree type,
23516 dw_die_ref context_die)
23518 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23519 struct vlr_context ctx = { type, NULL };
23521 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23522 context_die);
23523 add_data_member_location_attribute (die, binfo, &ctx);
23525 if (BINFO_VIRTUAL_P (binfo))
23526 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23528 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23529 children, otherwise the default is DW_ACCESS_public. In DWARF2
23530 the default has always been DW_ACCESS_private. */
23531 if (access == access_public_node)
23533 if (dwarf_version == 2
23534 || context_die->die_tag == DW_TAG_class_type)
23535 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23537 else if (access == access_protected_node)
23538 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23539 else if (dwarf_version > 2
23540 && context_die->die_tag != DW_TAG_class_type)
23541 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23544 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23545 structure. */
23546 static bool
23547 is_variant_part (tree decl)
23549 return (TREE_CODE (decl) == FIELD_DECL
23550 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23553 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23554 return the FIELD_DECL. Return NULL_TREE otherwise. */
23556 static tree
23557 analyze_discr_in_predicate (tree operand, tree struct_type)
23559 bool continue_stripping = true;
23560 while (continue_stripping)
23561 switch (TREE_CODE (operand))
23563 CASE_CONVERT:
23564 operand = TREE_OPERAND (operand, 0);
23565 break;
23566 default:
23567 continue_stripping = false;
23568 break;
23571 /* Match field access to members of struct_type only. */
23572 if (TREE_CODE (operand) == COMPONENT_REF
23573 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23574 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23575 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23576 return TREE_OPERAND (operand, 1);
23577 else
23578 return NULL_TREE;
23581 /* Check that SRC is a constant integer that can be represented as a native
23582 integer constant (either signed or unsigned). If so, store it into DEST and
23583 return true. Return false otherwise. */
23585 static bool
23586 get_discr_value (tree src, dw_discr_value *dest)
23588 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23590 if (TREE_CODE (src) != INTEGER_CST
23591 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23592 return false;
23594 dest->pos = is_unsigned;
23595 if (is_unsigned)
23596 dest->v.uval = tree_to_uhwi (src);
23597 else
23598 dest->v.sval = tree_to_shwi (src);
23600 return true;
23603 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23604 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23605 store NULL_TREE in DISCR_DECL. Otherwise:
23607 - store the discriminant field in STRUCT_TYPE that controls the variant
23608 part to *DISCR_DECL
23610 - put in *DISCR_LISTS_P an array where for each variant, the item
23611 represents the corresponding matching list of discriminant values.
23613 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23614 the above array.
23616 Note that when the array is allocated (i.e. when the analysis is
23617 successful), it is up to the caller to free the array. */
23619 static void
23620 analyze_variants_discr (tree variant_part_decl,
23621 tree struct_type,
23622 tree *discr_decl,
23623 dw_discr_list_ref **discr_lists_p,
23624 unsigned *discr_lists_length)
23626 tree variant_part_type = TREE_TYPE (variant_part_decl);
23627 tree variant;
23628 dw_discr_list_ref *discr_lists;
23629 unsigned i;
23631 /* Compute how many variants there are in this variant part. */
23632 *discr_lists_length = 0;
23633 for (variant = TYPE_FIELDS (variant_part_type);
23634 variant != NULL_TREE;
23635 variant = DECL_CHAIN (variant))
23636 ++*discr_lists_length;
23638 *discr_decl = NULL_TREE;
23639 *discr_lists_p
23640 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23641 sizeof (**discr_lists_p));
23642 discr_lists = *discr_lists_p;
23644 /* And then analyze all variants to extract discriminant information for all
23645 of them. This analysis is conservative: as soon as we detect something we
23646 do not support, abort everything and pretend we found nothing. */
23647 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23648 variant != NULL_TREE;
23649 variant = DECL_CHAIN (variant), ++i)
23651 tree match_expr = DECL_QUALIFIER (variant);
23653 /* Now, try to analyze the predicate and deduce a discriminant for
23654 it. */
23655 if (match_expr == boolean_true_node)
23656 /* Typically happens for the default variant: it matches all cases that
23657 previous variants rejected. Don't output any matching value for
23658 this one. */
23659 continue;
23661 /* The following loop tries to iterate over each discriminant
23662 possibility: single values or ranges. */
23663 while (match_expr != NULL_TREE)
23665 tree next_round_match_expr;
23666 tree candidate_discr = NULL_TREE;
23667 dw_discr_list_ref new_node = NULL;
23669 /* Possibilities are matched one after the other by nested
23670 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23671 continue with the rest at next iteration. */
23672 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23674 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23675 match_expr = TREE_OPERAND (match_expr, 1);
23677 else
23678 next_round_match_expr = NULL_TREE;
23680 if (match_expr == boolean_false_node)
23681 /* This sub-expression matches nothing: just wait for the next
23682 one. */
23685 else if (TREE_CODE (match_expr) == EQ_EXPR)
23687 /* We are matching: <discr_field> == <integer_cst>
23688 This sub-expression matches a single value. */
23689 tree integer_cst = TREE_OPERAND (match_expr, 1);
23691 candidate_discr
23692 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23693 struct_type);
23695 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23696 if (!get_discr_value (integer_cst,
23697 &new_node->dw_discr_lower_bound))
23698 goto abort;
23699 new_node->dw_discr_range = false;
23702 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23704 /* We are matching:
23705 <discr_field> > <integer_cst>
23706 && <discr_field> < <integer_cst>.
23707 This sub-expression matches the range of values between the
23708 two matched integer constants. Note that comparisons can be
23709 inclusive or exclusive. */
23710 tree candidate_discr_1, candidate_discr_2;
23711 tree lower_cst, upper_cst;
23712 bool lower_cst_included, upper_cst_included;
23713 tree lower_op = TREE_OPERAND (match_expr, 0);
23714 tree upper_op = TREE_OPERAND (match_expr, 1);
23716 /* When the comparison is exclusive, the integer constant is not
23717 the discriminant range bound we are looking for: we will have
23718 to increment or decrement it. */
23719 if (TREE_CODE (lower_op) == GE_EXPR)
23720 lower_cst_included = true;
23721 else if (TREE_CODE (lower_op) == GT_EXPR)
23722 lower_cst_included = false;
23723 else
23724 goto abort;
23726 if (TREE_CODE (upper_op) == LE_EXPR)
23727 upper_cst_included = true;
23728 else if (TREE_CODE (upper_op) == LT_EXPR)
23729 upper_cst_included = false;
23730 else
23731 goto abort;
23733 /* Extract the discriminant from the first operand and check it
23734 is consistant with the same analysis in the second
23735 operand. */
23736 candidate_discr_1
23737 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23738 struct_type);
23739 candidate_discr_2
23740 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23741 struct_type);
23742 if (candidate_discr_1 == candidate_discr_2)
23743 candidate_discr = candidate_discr_1;
23744 else
23745 goto abort;
23747 /* Extract bounds from both. */
23748 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23749 lower_cst = TREE_OPERAND (lower_op, 1);
23750 upper_cst = TREE_OPERAND (upper_op, 1);
23752 if (!lower_cst_included)
23753 lower_cst
23754 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23755 build_int_cst (TREE_TYPE (lower_cst), 1));
23756 if (!upper_cst_included)
23757 upper_cst
23758 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23759 build_int_cst (TREE_TYPE (upper_cst), 1));
23761 if (!get_discr_value (lower_cst,
23762 &new_node->dw_discr_lower_bound)
23763 || !get_discr_value (upper_cst,
23764 &new_node->dw_discr_upper_bound))
23765 goto abort;
23767 new_node->dw_discr_range = true;
23770 else
23771 /* Unsupported sub-expression: we cannot determine the set of
23772 matching discriminant values. Abort everything. */
23773 goto abort;
23775 /* If the discriminant info is not consistant with what we saw so
23776 far, consider the analysis failed and abort everything. */
23777 if (candidate_discr == NULL_TREE
23778 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23779 goto abort;
23780 else
23781 *discr_decl = candidate_discr;
23783 if (new_node != NULL)
23785 new_node->dw_discr_next = discr_lists[i];
23786 discr_lists[i] = new_node;
23788 match_expr = next_round_match_expr;
23792 /* If we reach this point, we could match everything we were interested
23793 in. */
23794 return;
23796 abort:
23797 /* Clean all data structure and return no result. */
23798 free (*discr_lists_p);
23799 *discr_lists_p = NULL;
23800 *discr_decl = NULL_TREE;
23803 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23804 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23805 under CONTEXT_DIE.
23807 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23808 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23809 this type, which are record types, represent the available variants and each
23810 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23811 values are inferred from these attributes.
23813 In trees, the offsets for the fields inside these sub-records are relative
23814 to the variant part itself, whereas the corresponding DIEs should have
23815 offset attributes that are relative to the embedding record base address.
23816 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23817 must be an expression that computes the offset of the variant part to
23818 describe in DWARF. */
23820 static void
23821 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23822 dw_die_ref context_die)
23824 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23825 tree variant_part_offset = vlr_ctx->variant_part_offset;
23826 struct loc_descr_context ctx = {
23827 vlr_ctx->struct_type, /* context_type */
23828 NULL_TREE, /* base_decl */
23829 NULL, /* dpi */
23830 false, /* placeholder_arg */
23831 false /* placeholder_seen */
23834 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23835 NULL_TREE if there is no such field. */
23836 tree discr_decl = NULL_TREE;
23837 dw_discr_list_ref *discr_lists;
23838 unsigned discr_lists_length = 0;
23839 unsigned i;
23841 dw_die_ref dwarf_proc_die = NULL;
23842 dw_die_ref variant_part_die
23843 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23845 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23847 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23848 &discr_decl, &discr_lists, &discr_lists_length);
23850 if (discr_decl != NULL_TREE)
23852 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23854 if (discr_die)
23855 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23856 else
23857 /* We have no DIE for the discriminant, so just discard all
23858 discrimimant information in the output. */
23859 discr_decl = NULL_TREE;
23862 /* If the offset for this variant part is more complex than a constant,
23863 create a DWARF procedure for it so that we will not have to generate DWARF
23864 expressions for it for each member. */
23865 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23866 && (dwarf_version >= 3 || !dwarf_strict))
23868 const tree dwarf_proc_fndecl
23869 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23870 build_function_type (TREE_TYPE (variant_part_offset),
23871 NULL_TREE));
23872 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23873 const dw_loc_descr_ref dwarf_proc_body
23874 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23876 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23877 dwarf_proc_fndecl, context_die);
23878 if (dwarf_proc_die != NULL)
23879 variant_part_offset = dwarf_proc_call;
23882 /* Output DIEs for all variants. */
23883 i = 0;
23884 for (tree variant = TYPE_FIELDS (variant_part_type);
23885 variant != NULL_TREE;
23886 variant = DECL_CHAIN (variant), ++i)
23888 tree variant_type = TREE_TYPE (variant);
23889 dw_die_ref variant_die;
23891 /* All variants (i.e. members of a variant part) are supposed to be
23892 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23893 under these records. */
23894 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23896 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23897 equate_decl_number_to_die (variant, variant_die);
23899 /* Output discriminant values this variant matches, if any. */
23900 if (discr_decl == NULL || discr_lists[i] == NULL)
23901 /* In the case we have discriminant information at all, this is
23902 probably the default variant: as the standard says, don't
23903 output any discriminant value/list attribute. */
23905 else if (discr_lists[i]->dw_discr_next == NULL
23906 && !discr_lists[i]->dw_discr_range)
23907 /* If there is only one accepted value, don't bother outputting a
23908 list. */
23909 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23910 else
23911 add_discr_list (variant_die, discr_lists[i]);
23913 for (tree member = TYPE_FIELDS (variant_type);
23914 member != NULL_TREE;
23915 member = DECL_CHAIN (member))
23917 struct vlr_context vlr_sub_ctx = {
23918 vlr_ctx->struct_type, /* struct_type */
23919 NULL /* variant_part_offset */
23921 if (is_variant_part (member))
23923 /* All offsets for fields inside variant parts are relative to
23924 the top-level embedding RECORD_TYPE's base address. On the
23925 other hand, offsets in GCC's types are relative to the
23926 nested-most variant part. So we have to sum offsets each time
23927 we recurse. */
23929 vlr_sub_ctx.variant_part_offset
23930 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
23931 variant_part_offset, byte_position (member));
23932 gen_variant_part (member, &vlr_sub_ctx, variant_die);
23934 else
23936 vlr_sub_ctx.variant_part_offset = variant_part_offset;
23937 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
23942 free (discr_lists);
23945 /* Generate a DIE for a class member. */
23947 static void
23948 gen_member_die (tree type, dw_die_ref context_die)
23950 tree member;
23951 tree binfo = TYPE_BINFO (type);
23952 dw_die_ref child;
23954 /* If this is not an incomplete type, output descriptions of each of its
23955 members. Note that as we output the DIEs necessary to represent the
23956 members of this record or union type, we will also be trying to output
23957 DIEs to represent the *types* of those members. However the `type'
23958 function (above) will specifically avoid generating type DIEs for member
23959 types *within* the list of member DIEs for this (containing) type except
23960 for those types (of members) which are explicitly marked as also being
23961 members of this (containing) type themselves. The g++ front- end can
23962 force any given type to be treated as a member of some other (containing)
23963 type by setting the TYPE_CONTEXT of the given (member) type to point to
23964 the TREE node representing the appropriate (containing) type. */
23966 /* First output info about the base classes. */
23967 if (binfo)
23969 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
23970 int i;
23971 tree base;
23973 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
23974 gen_inheritance_die (base,
23975 (accesses ? (*accesses)[i] : access_public_node),
23976 type,
23977 context_die);
23980 /* Now output info about the data members and type members. */
23981 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
23983 struct vlr_context vlr_ctx = { type, NULL_TREE };
23985 /* If we thought we were generating minimal debug info for TYPE
23986 and then changed our minds, some of the member declarations
23987 may have already been defined. Don't define them again, but
23988 do put them in the right order. */
23990 child = lookup_decl_die (member);
23991 if (child)
23993 /* Handle inline static data members, which only have in-class
23994 declarations. */
23995 if (child->die_tag == DW_TAG_variable
23996 && child->die_parent == comp_unit_die ()
23997 && get_AT (child, DW_AT_specification) == NULL)
23999 reparent_child (child, context_die);
24000 child->die_tag = DW_TAG_member;
24002 else
24003 splice_child_die (context_die, child);
24006 /* Do not generate standard DWARF for variant parts if we are generating
24007 the corresponding GNAT encodings: DIEs generated for both would
24008 conflict in our mappings. */
24009 else if (is_variant_part (member)
24010 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24012 vlr_ctx.variant_part_offset = byte_position (member);
24013 gen_variant_part (member, &vlr_ctx, context_die);
24015 else
24017 vlr_ctx.variant_part_offset = NULL_TREE;
24018 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24021 /* For C++ inline static data members emit immediately a DW_TAG_variable
24022 DIE that will refer to that DW_TAG_member through
24023 DW_AT_specification. */
24024 if (TREE_STATIC (member)
24025 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24026 != -1))
24028 int old_extern = DECL_EXTERNAL (member);
24029 DECL_EXTERNAL (member) = 0;
24030 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24031 DECL_EXTERNAL (member) = old_extern;
24035 /* We do not keep type methods in type variants. */
24036 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24037 /* Now output info about the function members (if any). */
24038 if (TYPE_METHODS (type) != error_mark_node)
24039 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24041 /* Don't include clones in the member list. */
24042 if (DECL_ABSTRACT_ORIGIN (member))
24043 continue;
24044 /* Nor constructors for anonymous classes. */
24045 if (DECL_ARTIFICIAL (member)
24046 && dwarf2_name (member, 0) == NULL)
24047 continue;
24049 child = lookup_decl_die (member);
24050 if (child)
24051 splice_child_die (context_die, child);
24052 else
24053 gen_decl_die (member, NULL, NULL, context_die);
24057 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24058 is set, we pretend that the type was never defined, so we only get the
24059 member DIEs needed by later specification DIEs. */
24061 static void
24062 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24063 enum debug_info_usage usage)
24065 if (TREE_ASM_WRITTEN (type))
24067 /* Fill in the bound of variable-length fields in late dwarf if
24068 still incomplete. */
24069 if (!early_dwarf && variably_modified_type_p (type, NULL))
24070 for (tree member = TYPE_FIELDS (type);
24071 member;
24072 member = DECL_CHAIN (member))
24073 fill_variable_array_bounds (TREE_TYPE (member));
24074 return;
24077 dw_die_ref type_die = lookup_type_die (type);
24078 dw_die_ref scope_die = 0;
24079 int nested = 0;
24080 int complete = (TYPE_SIZE (type)
24081 && (! TYPE_STUB_DECL (type)
24082 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24083 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24084 complete = complete && should_emit_struct_debug (type, usage);
24086 if (type_die && ! complete)
24087 return;
24089 if (TYPE_CONTEXT (type) != NULL_TREE
24090 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24091 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24092 nested = 1;
24094 scope_die = scope_die_for (type, context_die);
24096 /* Generate child dies for template paramaters. */
24097 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24098 schedule_generic_params_dies_gen (type);
24100 if (! type_die || (nested && is_cu_die (scope_die)))
24101 /* First occurrence of type or toplevel definition of nested class. */
24103 dw_die_ref old_die = type_die;
24105 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24106 ? record_type_tag (type) : DW_TAG_union_type,
24107 scope_die, type);
24108 equate_type_number_to_die (type, type_die);
24109 if (old_die)
24110 add_AT_specification (type_die, old_die);
24111 else
24112 add_name_attribute (type_die, type_tag (type));
24114 else
24115 remove_AT (type_die, DW_AT_declaration);
24117 /* If this type has been completed, then give it a byte_size attribute and
24118 then give a list of members. */
24119 if (complete && !ns_decl)
24121 /* Prevent infinite recursion in cases where the type of some member of
24122 this type is expressed in terms of this type itself. */
24123 TREE_ASM_WRITTEN (type) = 1;
24124 add_byte_size_attribute (type_die, type);
24125 if (TYPE_STUB_DECL (type) != NULL_TREE)
24127 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24128 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24131 /* If the first reference to this type was as the return type of an
24132 inline function, then it may not have a parent. Fix this now. */
24133 if (type_die->die_parent == NULL)
24134 add_child_die (scope_die, type_die);
24136 push_decl_scope (type);
24137 gen_member_die (type, type_die);
24138 pop_decl_scope ();
24140 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24141 if (TYPE_ARTIFICIAL (type))
24142 add_AT_flag (type_die, DW_AT_artificial, 1);
24144 /* GNU extension: Record what type our vtable lives in. */
24145 if (TYPE_VFIELD (type))
24147 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24149 gen_type_die (vtype, context_die);
24150 add_AT_die_ref (type_die, DW_AT_containing_type,
24151 lookup_type_die (vtype));
24154 else
24156 add_AT_flag (type_die, DW_AT_declaration, 1);
24158 /* We don't need to do this for function-local types. */
24159 if (TYPE_STUB_DECL (type)
24160 && ! decl_function_context (TYPE_STUB_DECL (type)))
24161 vec_safe_push (incomplete_types, type);
24164 if (get_AT (type_die, DW_AT_name))
24165 add_pubtype (type, type_die);
24168 /* Generate a DIE for a subroutine _type_. */
24170 static void
24171 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24173 tree return_type = TREE_TYPE (type);
24174 dw_die_ref subr_die
24175 = new_die (DW_TAG_subroutine_type,
24176 scope_die_for (type, context_die), type);
24178 equate_type_number_to_die (type, subr_die);
24179 add_prototyped_attribute (subr_die, type);
24180 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24181 context_die);
24182 gen_formal_types_die (type, subr_die);
24184 if (get_AT (subr_die, DW_AT_name))
24185 add_pubtype (type, subr_die);
24186 if ((dwarf_version >= 5 || !dwarf_strict)
24187 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24188 add_AT_flag (subr_die, DW_AT_reference, 1);
24189 if ((dwarf_version >= 5 || !dwarf_strict)
24190 && lang_hooks.types.type_dwarf_attribute (type,
24191 DW_AT_rvalue_reference) != -1)
24192 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24195 /* Generate a DIE for a type definition. */
24197 static void
24198 gen_typedef_die (tree decl, dw_die_ref context_die)
24200 dw_die_ref type_die;
24201 tree origin;
24203 if (TREE_ASM_WRITTEN (decl))
24205 if (DECL_ORIGINAL_TYPE (decl))
24206 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24207 return;
24210 TREE_ASM_WRITTEN (decl) = 1;
24211 type_die = new_die (DW_TAG_typedef, context_die, decl);
24212 origin = decl_ultimate_origin (decl);
24213 if (origin != NULL)
24214 add_abstract_origin_attribute (type_die, origin);
24215 else
24217 tree type;
24219 add_name_and_src_coords_attributes (type_die, decl);
24220 if (DECL_ORIGINAL_TYPE (decl))
24222 type = DECL_ORIGINAL_TYPE (decl);
24224 if (type == error_mark_node)
24225 return;
24227 gcc_assert (type != TREE_TYPE (decl));
24228 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24230 else
24232 type = TREE_TYPE (decl);
24234 if (type == error_mark_node)
24235 return;
24237 if (is_naming_typedef_decl (TYPE_NAME (type)))
24239 /* Here, we are in the case of decl being a typedef naming
24240 an anonymous type, e.g:
24241 typedef struct {...} foo;
24242 In that case TREE_TYPE (decl) is not a typedef variant
24243 type and TYPE_NAME of the anonymous type is set to the
24244 TYPE_DECL of the typedef. This construct is emitted by
24245 the C++ FE.
24247 TYPE is the anonymous struct named by the typedef
24248 DECL. As we need the DW_AT_type attribute of the
24249 DW_TAG_typedef to point to the DIE of TYPE, let's
24250 generate that DIE right away. add_type_attribute
24251 called below will then pick (via lookup_type_die) that
24252 anonymous struct DIE. */
24253 if (!TREE_ASM_WRITTEN (type))
24254 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24256 /* This is a GNU Extension. We are adding a
24257 DW_AT_linkage_name attribute to the DIE of the
24258 anonymous struct TYPE. The value of that attribute
24259 is the name of the typedef decl naming the anonymous
24260 struct. This greatly eases the work of consumers of
24261 this debug info. */
24262 add_linkage_name_raw (lookup_type_die (type), decl);
24266 add_type_attribute (type_die, type, decl_quals (decl), false,
24267 context_die);
24269 if (is_naming_typedef_decl (decl))
24270 /* We want that all subsequent calls to lookup_type_die with
24271 TYPE in argument yield the DW_TAG_typedef we have just
24272 created. */
24273 equate_type_number_to_die (type, type_die);
24275 add_accessibility_attribute (type_die, decl);
24278 if (DECL_ABSTRACT_P (decl))
24279 equate_decl_number_to_die (decl, type_die);
24281 if (get_AT (type_die, DW_AT_name))
24282 add_pubtype (decl, type_die);
24285 /* Generate a DIE for a struct, class, enum or union type. */
24287 static void
24288 gen_tagged_type_die (tree type,
24289 dw_die_ref context_die,
24290 enum debug_info_usage usage)
24292 int need_pop;
24294 if (type == NULL_TREE
24295 || !is_tagged_type (type))
24296 return;
24298 if (TREE_ASM_WRITTEN (type))
24299 need_pop = 0;
24300 /* If this is a nested type whose containing class hasn't been written
24301 out yet, writing it out will cover this one, too. This does not apply
24302 to instantiations of member class templates; they need to be added to
24303 the containing class as they are generated. FIXME: This hurts the
24304 idea of combining type decls from multiple TUs, since we can't predict
24305 what set of template instantiations we'll get. */
24306 else if (TYPE_CONTEXT (type)
24307 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24308 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24310 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24312 if (TREE_ASM_WRITTEN (type))
24313 return;
24315 /* If that failed, attach ourselves to the stub. */
24316 push_decl_scope (TYPE_CONTEXT (type));
24317 context_die = lookup_type_die (TYPE_CONTEXT (type));
24318 need_pop = 1;
24320 else if (TYPE_CONTEXT (type) != NULL_TREE
24321 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24323 /* If this type is local to a function that hasn't been written
24324 out yet, use a NULL context for now; it will be fixed up in
24325 decls_for_scope. */
24326 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24327 /* A declaration DIE doesn't count; nested types need to go in the
24328 specification. */
24329 if (context_die && is_declaration_die (context_die))
24330 context_die = NULL;
24331 need_pop = 0;
24333 else
24335 context_die = declare_in_namespace (type, context_die);
24336 need_pop = 0;
24339 if (TREE_CODE (type) == ENUMERAL_TYPE)
24341 /* This might have been written out by the call to
24342 declare_in_namespace. */
24343 if (!TREE_ASM_WRITTEN (type))
24344 gen_enumeration_type_die (type, context_die);
24346 else
24347 gen_struct_or_union_type_die (type, context_die, usage);
24349 if (need_pop)
24350 pop_decl_scope ();
24352 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24353 it up if it is ever completed. gen_*_type_die will set it for us
24354 when appropriate. */
24357 /* Generate a type description DIE. */
24359 static void
24360 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24361 enum debug_info_usage usage)
24363 struct array_descr_info info;
24365 if (type == NULL_TREE || type == error_mark_node)
24366 return;
24368 if (flag_checking && type)
24369 verify_type (type);
24371 if (TYPE_NAME (type) != NULL_TREE
24372 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24373 && is_redundant_typedef (TYPE_NAME (type))
24374 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24375 /* The DECL of this type is a typedef we don't want to emit debug
24376 info for but we want debug info for its underlying typedef.
24377 This can happen for e.g, the injected-class-name of a C++
24378 type. */
24379 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24381 /* If TYPE is a typedef type variant, let's generate debug info
24382 for the parent typedef which TYPE is a type of. */
24383 if (typedef_variant_p (type))
24385 if (TREE_ASM_WRITTEN (type))
24386 return;
24388 /* Prevent broken recursion; we can't hand off to the same type. */
24389 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24391 /* Give typedefs the right scope. */
24392 context_die = scope_die_for (type, context_die);
24394 TREE_ASM_WRITTEN (type) = 1;
24396 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24397 return;
24400 /* If type is an anonymous tagged type named by a typedef, let's
24401 generate debug info for the typedef. */
24402 if (is_naming_typedef_decl (TYPE_NAME (type)))
24404 /* Use the DIE of the containing namespace as the parent DIE of
24405 the type description DIE we want to generate. */
24406 if (DECL_CONTEXT (TYPE_NAME (type))
24407 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24408 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24410 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24411 return;
24414 if (lang_hooks.types.get_debug_type)
24416 tree debug_type = lang_hooks.types.get_debug_type (type);
24418 if (debug_type != NULL_TREE && debug_type != type)
24420 gen_type_die_with_usage (debug_type, context_die, usage);
24421 return;
24425 /* We are going to output a DIE to represent the unqualified version
24426 of this type (i.e. without any const or volatile qualifiers) so
24427 get the main variant (i.e. the unqualified version) of this type
24428 now. (Vectors and arrays are special because the debugging info is in the
24429 cloned type itself. Similarly function/method types can contain extra
24430 ref-qualification). */
24431 if (TREE_CODE (type) == FUNCTION_TYPE
24432 || TREE_CODE (type) == METHOD_TYPE)
24434 /* For function/method types, can't use type_main_variant here,
24435 because that can have different ref-qualifiers for C++,
24436 but try to canonicalize. */
24437 tree main = TYPE_MAIN_VARIANT (type);
24438 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24439 if (check_base_type (t, main) && check_lang_type (t, type))
24440 type = t;
24442 else if (TREE_CODE (type) != VECTOR_TYPE
24443 && TREE_CODE (type) != ARRAY_TYPE)
24444 type = type_main_variant (type);
24446 /* If this is an array type with hidden descriptor, handle it first. */
24447 if (!TREE_ASM_WRITTEN (type)
24448 && lang_hooks.types.get_array_descr_info)
24450 memset (&info, 0, sizeof (info));
24451 if (lang_hooks.types.get_array_descr_info (type, &info))
24453 /* Fortran sometimes emits array types with no dimension. */
24454 gcc_assert (info.ndimensions >= 0
24455 && (info.ndimensions
24456 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24457 gen_descr_array_type_die (type, &info, context_die);
24458 TREE_ASM_WRITTEN (type) = 1;
24459 return;
24463 if (TREE_ASM_WRITTEN (type))
24465 /* Variable-length types may be incomplete even if
24466 TREE_ASM_WRITTEN. For such types, fall through to
24467 gen_array_type_die() and possibly fill in
24468 DW_AT_{upper,lower}_bound attributes. */
24469 if ((TREE_CODE (type) != ARRAY_TYPE
24470 && TREE_CODE (type) != RECORD_TYPE
24471 && TREE_CODE (type) != UNION_TYPE
24472 && TREE_CODE (type) != QUAL_UNION_TYPE)
24473 || !variably_modified_type_p (type, NULL))
24474 return;
24477 switch (TREE_CODE (type))
24479 case ERROR_MARK:
24480 break;
24482 case POINTER_TYPE:
24483 case REFERENCE_TYPE:
24484 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24485 ensures that the gen_type_die recursion will terminate even if the
24486 type is recursive. Recursive types are possible in Ada. */
24487 /* ??? We could perhaps do this for all types before the switch
24488 statement. */
24489 TREE_ASM_WRITTEN (type) = 1;
24491 /* For these types, all that is required is that we output a DIE (or a
24492 set of DIEs) to represent the "basis" type. */
24493 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24494 DINFO_USAGE_IND_USE);
24495 break;
24497 case OFFSET_TYPE:
24498 /* This code is used for C++ pointer-to-data-member types.
24499 Output a description of the relevant class type. */
24500 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24501 DINFO_USAGE_IND_USE);
24503 /* Output a description of the type of the object pointed to. */
24504 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24505 DINFO_USAGE_IND_USE);
24507 /* Now output a DIE to represent this pointer-to-data-member type
24508 itself. */
24509 gen_ptr_to_mbr_type_die (type, context_die);
24510 break;
24512 case FUNCTION_TYPE:
24513 /* Force out return type (in case it wasn't forced out already). */
24514 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24515 DINFO_USAGE_DIR_USE);
24516 gen_subroutine_type_die (type, context_die);
24517 break;
24519 case METHOD_TYPE:
24520 /* Force out return type (in case it wasn't forced out already). */
24521 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24522 DINFO_USAGE_DIR_USE);
24523 gen_subroutine_type_die (type, context_die);
24524 break;
24526 case ARRAY_TYPE:
24527 case VECTOR_TYPE:
24528 gen_array_type_die (type, context_die);
24529 break;
24531 case ENUMERAL_TYPE:
24532 case RECORD_TYPE:
24533 case UNION_TYPE:
24534 case QUAL_UNION_TYPE:
24535 gen_tagged_type_die (type, context_die, usage);
24536 return;
24538 case VOID_TYPE:
24539 case INTEGER_TYPE:
24540 case REAL_TYPE:
24541 case FIXED_POINT_TYPE:
24542 case COMPLEX_TYPE:
24543 case BOOLEAN_TYPE:
24544 case POINTER_BOUNDS_TYPE:
24545 /* No DIEs needed for fundamental types. */
24546 break;
24548 case NULLPTR_TYPE:
24549 case LANG_TYPE:
24550 /* Just use DW_TAG_unspecified_type. */
24552 dw_die_ref type_die = lookup_type_die (type);
24553 if (type_die == NULL)
24555 tree name = TYPE_IDENTIFIER (type);
24556 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24557 type);
24558 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24559 equate_type_number_to_die (type, type_die);
24562 break;
24564 default:
24565 if (is_cxx_auto (type))
24567 tree name = TYPE_IDENTIFIER (type);
24568 dw_die_ref *die = (name == get_identifier ("auto")
24569 ? &auto_die : &decltype_auto_die);
24570 if (!*die)
24572 *die = new_die (DW_TAG_unspecified_type,
24573 comp_unit_die (), NULL_TREE);
24574 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24576 equate_type_number_to_die (type, *die);
24577 break;
24579 gcc_unreachable ();
24582 TREE_ASM_WRITTEN (type) = 1;
24585 static void
24586 gen_type_die (tree type, dw_die_ref context_die)
24588 if (type != error_mark_node)
24590 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24591 if (flag_checking)
24593 dw_die_ref die = lookup_type_die (type);
24594 if (die)
24595 check_die (die);
24600 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24601 things which are local to the given block. */
24603 static void
24604 gen_block_die (tree stmt, dw_die_ref context_die)
24606 int must_output_die = 0;
24607 bool inlined_func;
24609 /* Ignore blocks that are NULL. */
24610 if (stmt == NULL_TREE)
24611 return;
24613 inlined_func = inlined_function_outer_scope_p (stmt);
24615 /* If the block is one fragment of a non-contiguous block, do not
24616 process the variables, since they will have been done by the
24617 origin block. Do process subblocks. */
24618 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24620 tree sub;
24622 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24623 gen_block_die (sub, context_die);
24625 return;
24628 /* Determine if we need to output any Dwarf DIEs at all to represent this
24629 block. */
24630 if (inlined_func)
24631 /* The outer scopes for inlinings *must* always be represented. We
24632 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24633 must_output_die = 1;
24634 else
24636 /* Determine if this block directly contains any "significant"
24637 local declarations which we will need to output DIEs for. */
24638 if (debug_info_level > DINFO_LEVEL_TERSE)
24639 /* We are not in terse mode so *any* local declaration counts
24640 as being a "significant" one. */
24641 must_output_die = ((BLOCK_VARS (stmt) != NULL
24642 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24643 && (TREE_USED (stmt)
24644 || TREE_ASM_WRITTEN (stmt)
24645 || BLOCK_ABSTRACT (stmt)));
24646 else if ((TREE_USED (stmt)
24647 || TREE_ASM_WRITTEN (stmt)
24648 || BLOCK_ABSTRACT (stmt))
24649 && !dwarf2out_ignore_block (stmt))
24650 must_output_die = 1;
24653 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24654 DIE for any block which contains no significant local declarations at
24655 all. Rather, in such cases we just call `decls_for_scope' so that any
24656 needed Dwarf info for any sub-blocks will get properly generated. Note
24657 that in terse mode, our definition of what constitutes a "significant"
24658 local declaration gets restricted to include only inlined function
24659 instances and local (nested) function definitions. */
24660 if (must_output_die)
24662 if (inlined_func)
24664 /* If STMT block is abstract, that means we have been called
24665 indirectly from dwarf2out_abstract_function.
24666 That function rightfully marks the descendent blocks (of
24667 the abstract function it is dealing with) as being abstract,
24668 precisely to prevent us from emitting any
24669 DW_TAG_inlined_subroutine DIE as a descendent
24670 of an abstract function instance. So in that case, we should
24671 not call gen_inlined_subroutine_die.
24673 Later though, when cgraph asks dwarf2out to emit info
24674 for the concrete instance of the function decl into which
24675 the concrete instance of STMT got inlined, the later will lead
24676 to the generation of a DW_TAG_inlined_subroutine DIE. */
24677 if (! BLOCK_ABSTRACT (stmt))
24678 gen_inlined_subroutine_die (stmt, context_die);
24680 else
24681 gen_lexical_block_die (stmt, context_die);
24683 else
24684 decls_for_scope (stmt, context_die);
24687 /* Process variable DECL (or variable with origin ORIGIN) within
24688 block STMT and add it to CONTEXT_DIE. */
24689 static void
24690 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24692 dw_die_ref die;
24693 tree decl_or_origin = decl ? decl : origin;
24695 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24696 die = lookup_decl_die (decl_or_origin);
24697 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24699 if (TYPE_DECL_IS_STUB (decl_or_origin))
24700 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24701 else
24702 die = lookup_decl_die (decl_or_origin);
24703 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24704 if (! die && ! early_dwarf)
24705 return;
24707 else
24708 die = NULL;
24710 if (die != NULL && die->die_parent == NULL)
24711 add_child_die (context_die, die);
24712 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24714 if (early_dwarf)
24715 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24716 stmt, context_die);
24718 else
24719 gen_decl_die (decl, origin, NULL, context_die);
24722 /* Generate all of the decls declared within a given scope and (recursively)
24723 all of its sub-blocks. */
24725 static void
24726 decls_for_scope (tree stmt, dw_die_ref context_die)
24728 tree decl;
24729 unsigned int i;
24730 tree subblocks;
24732 /* Ignore NULL blocks. */
24733 if (stmt == NULL_TREE)
24734 return;
24736 /* Output the DIEs to represent all of the data objects and typedefs
24737 declared directly within this block but not within any nested
24738 sub-blocks. Also, nested function and tag DIEs have been
24739 generated with a parent of NULL; fix that up now. We don't
24740 have to do this if we're at -g1. */
24741 if (debug_info_level > DINFO_LEVEL_TERSE)
24743 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24744 process_scope_var (stmt, decl, NULL_TREE, context_die);
24745 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24746 origin - avoid doing this twice as we have no good way to see
24747 if we've done it once already. */
24748 if (! early_dwarf)
24749 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24750 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
24751 context_die);
24754 /* Even if we're at -g1, we need to process the subblocks in order to get
24755 inlined call information. */
24757 /* Output the DIEs to represent all sub-blocks (and the items declared
24758 therein) of this block. */
24759 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24760 subblocks != NULL;
24761 subblocks = BLOCK_CHAIN (subblocks))
24762 gen_block_die (subblocks, context_die);
24765 /* Is this a typedef we can avoid emitting? */
24767 bool
24768 is_redundant_typedef (const_tree decl)
24770 if (TYPE_DECL_IS_STUB (decl))
24771 return true;
24773 if (DECL_ARTIFICIAL (decl)
24774 && DECL_CONTEXT (decl)
24775 && is_tagged_type (DECL_CONTEXT (decl))
24776 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24777 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24778 /* Also ignore the artificial member typedef for the class name. */
24779 return true;
24781 return false;
24784 /* Return TRUE if TYPE is a typedef that names a type for linkage
24785 purposes. This kind of typedefs is produced by the C++ FE for
24786 constructs like:
24788 typedef struct {...} foo;
24790 In that case, there is no typedef variant type produced for foo.
24791 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24792 struct type. */
24794 static bool
24795 is_naming_typedef_decl (const_tree decl)
24797 if (decl == NULL_TREE
24798 || TREE_CODE (decl) != TYPE_DECL
24799 || DECL_NAMELESS (decl)
24800 || !is_tagged_type (TREE_TYPE (decl))
24801 || DECL_IS_BUILTIN (decl)
24802 || is_redundant_typedef (decl)
24803 /* It looks like Ada produces TYPE_DECLs that are very similar
24804 to C++ naming typedefs but that have different
24805 semantics. Let's be specific to c++ for now. */
24806 || !is_cxx (decl))
24807 return FALSE;
24809 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24810 && TYPE_NAME (TREE_TYPE (decl)) == decl
24811 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24812 != TYPE_NAME (TREE_TYPE (decl))));
24815 /* Looks up the DIE for a context. */
24817 static inline dw_die_ref
24818 lookup_context_die (tree context)
24820 if (context)
24822 /* Find die that represents this context. */
24823 if (TYPE_P (context))
24825 context = TYPE_MAIN_VARIANT (context);
24826 dw_die_ref ctx = lookup_type_die (context);
24827 if (!ctx)
24828 return NULL;
24829 return strip_naming_typedef (context, ctx);
24831 else
24832 return lookup_decl_die (context);
24834 return comp_unit_die ();
24837 /* Returns the DIE for a context. */
24839 static inline dw_die_ref
24840 get_context_die (tree context)
24842 if (context)
24844 /* Find die that represents this context. */
24845 if (TYPE_P (context))
24847 context = TYPE_MAIN_VARIANT (context);
24848 return strip_naming_typedef (context, force_type_die (context));
24850 else
24851 return force_decl_die (context);
24853 return comp_unit_die ();
24856 /* Returns the DIE for decl. A DIE will always be returned. */
24858 static dw_die_ref
24859 force_decl_die (tree decl)
24861 dw_die_ref decl_die;
24862 unsigned saved_external_flag;
24863 tree save_fn = NULL_TREE;
24864 decl_die = lookup_decl_die (decl);
24865 if (!decl_die)
24867 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24869 decl_die = lookup_decl_die (decl);
24870 if (decl_die)
24871 return decl_die;
24873 switch (TREE_CODE (decl))
24875 case FUNCTION_DECL:
24876 /* Clear current_function_decl, so that gen_subprogram_die thinks
24877 that this is a declaration. At this point, we just want to force
24878 declaration die. */
24879 save_fn = current_function_decl;
24880 current_function_decl = NULL_TREE;
24881 gen_subprogram_die (decl, context_die);
24882 current_function_decl = save_fn;
24883 break;
24885 case VAR_DECL:
24886 /* Set external flag to force declaration die. Restore it after
24887 gen_decl_die() call. */
24888 saved_external_flag = DECL_EXTERNAL (decl);
24889 DECL_EXTERNAL (decl) = 1;
24890 gen_decl_die (decl, NULL, NULL, context_die);
24891 DECL_EXTERNAL (decl) = saved_external_flag;
24892 break;
24894 case NAMESPACE_DECL:
24895 if (dwarf_version >= 3 || !dwarf_strict)
24896 dwarf2out_decl (decl);
24897 else
24898 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
24899 decl_die = comp_unit_die ();
24900 break;
24902 case TRANSLATION_UNIT_DECL:
24903 decl_die = comp_unit_die ();
24904 break;
24906 default:
24907 gcc_unreachable ();
24910 /* We should be able to find the DIE now. */
24911 if (!decl_die)
24912 decl_die = lookup_decl_die (decl);
24913 gcc_assert (decl_die);
24916 return decl_die;
24919 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
24920 always returned. */
24922 static dw_die_ref
24923 force_type_die (tree type)
24925 dw_die_ref type_die;
24927 type_die = lookup_type_die (type);
24928 if (!type_die)
24930 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
24932 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
24933 false, context_die);
24934 gcc_assert (type_die);
24936 return type_die;
24939 /* Force out any required namespaces to be able to output DECL,
24940 and return the new context_die for it, if it's changed. */
24942 static dw_die_ref
24943 setup_namespace_context (tree thing, dw_die_ref context_die)
24945 tree context = (DECL_P (thing)
24946 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
24947 if (context && TREE_CODE (context) == NAMESPACE_DECL)
24948 /* Force out the namespace. */
24949 context_die = force_decl_die (context);
24951 return context_die;
24954 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
24955 type) within its namespace, if appropriate.
24957 For compatibility with older debuggers, namespace DIEs only contain
24958 declarations; all definitions are emitted at CU scope, with
24959 DW_AT_specification pointing to the declaration (like with class
24960 members). */
24962 static dw_die_ref
24963 declare_in_namespace (tree thing, dw_die_ref context_die)
24965 dw_die_ref ns_context;
24967 if (debug_info_level <= DINFO_LEVEL_TERSE)
24968 return context_die;
24970 /* External declarations in the local scope only need to be emitted
24971 once, not once in the namespace and once in the scope.
24973 This avoids declaring the `extern' below in the
24974 namespace DIE as well as in the innermost scope:
24976 namespace S
24978 int i=5;
24979 int foo()
24981 int i=8;
24982 extern int i;
24983 return i;
24987 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
24988 return context_die;
24990 /* If this decl is from an inlined function, then don't try to emit it in its
24991 namespace, as we will get confused. It would have already been emitted
24992 when the abstract instance of the inline function was emitted anyways. */
24993 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
24994 return context_die;
24996 ns_context = setup_namespace_context (thing, context_die);
24998 if (ns_context != context_die)
25000 if (is_fortran ())
25001 return ns_context;
25002 if (DECL_P (thing))
25003 gen_decl_die (thing, NULL, NULL, ns_context);
25004 else
25005 gen_type_die (thing, ns_context);
25007 return context_die;
25010 /* Generate a DIE for a namespace or namespace alias. */
25012 static void
25013 gen_namespace_die (tree decl, dw_die_ref context_die)
25015 dw_die_ref namespace_die;
25017 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25018 they are an alias of. */
25019 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25021 /* Output a real namespace or module. */
25022 context_die = setup_namespace_context (decl, comp_unit_die ());
25023 namespace_die = new_die (is_fortran ()
25024 ? DW_TAG_module : DW_TAG_namespace,
25025 context_die, decl);
25026 /* For Fortran modules defined in different CU don't add src coords. */
25027 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25029 const char *name = dwarf2_name (decl, 0);
25030 if (name)
25031 add_name_attribute (namespace_die, name);
25033 else
25034 add_name_and_src_coords_attributes (namespace_die, decl);
25035 if (DECL_EXTERNAL (decl))
25036 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25037 equate_decl_number_to_die (decl, namespace_die);
25039 else
25041 /* Output a namespace alias. */
25043 /* Force out the namespace we are an alias of, if necessary. */
25044 dw_die_ref origin_die
25045 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25047 if (DECL_FILE_SCOPE_P (decl)
25048 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25049 context_die = setup_namespace_context (decl, comp_unit_die ());
25050 /* Now create the namespace alias DIE. */
25051 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25052 add_name_and_src_coords_attributes (namespace_die, decl);
25053 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25054 equate_decl_number_to_die (decl, namespace_die);
25056 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25057 if (want_pubnames ())
25058 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25061 /* Generate Dwarf debug information for a decl described by DECL.
25062 The return value is currently only meaningful for PARM_DECLs,
25063 for all other decls it returns NULL.
25065 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25066 It can be NULL otherwise. */
25068 static dw_die_ref
25069 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25070 dw_die_ref context_die)
25072 tree decl_or_origin = decl ? decl : origin;
25073 tree class_origin = NULL, ultimate_origin;
25075 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25076 return NULL;
25078 /* Ignore pointer bounds decls. */
25079 if (DECL_P (decl_or_origin)
25080 && TREE_TYPE (decl_or_origin)
25081 && POINTER_BOUNDS_P (decl_or_origin))
25082 return NULL;
25084 switch (TREE_CODE (decl_or_origin))
25086 case ERROR_MARK:
25087 break;
25089 case CONST_DECL:
25090 if (!is_fortran () && !is_ada ())
25092 /* The individual enumerators of an enum type get output when we output
25093 the Dwarf representation of the relevant enum type itself. */
25094 break;
25097 /* Emit its type. */
25098 gen_type_die (TREE_TYPE (decl), context_die);
25100 /* And its containing namespace. */
25101 context_die = declare_in_namespace (decl, context_die);
25103 gen_const_die (decl, context_die);
25104 break;
25106 case FUNCTION_DECL:
25107 /* Don't output any DIEs to represent mere function declarations,
25108 unless they are class members or explicit block externs. */
25109 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25110 && DECL_FILE_SCOPE_P (decl_or_origin)
25111 && (current_function_decl == NULL_TREE
25112 || DECL_ARTIFICIAL (decl_or_origin)))
25113 break;
25115 #if 0
25116 /* FIXME */
25117 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25118 on local redeclarations of global functions. That seems broken. */
25119 if (current_function_decl != decl)
25120 /* This is only a declaration. */;
25121 #endif
25123 /* If we're emitting a clone, emit info for the abstract instance. */
25124 if (origin || DECL_ORIGIN (decl) != decl)
25125 dwarf2out_abstract_function (origin
25126 ? DECL_ORIGIN (origin)
25127 : DECL_ABSTRACT_ORIGIN (decl));
25129 /* If we're emitting an out-of-line copy of an inline function,
25130 emit info for the abstract instance and set up to refer to it. */
25131 else if (cgraph_function_possibly_inlined_p (decl)
25132 && ! DECL_ABSTRACT_P (decl)
25133 && ! class_or_namespace_scope_p (context_die)
25134 /* dwarf2out_abstract_function won't emit a die if this is just
25135 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25136 that case, because that works only if we have a die. */
25137 && DECL_INITIAL (decl) != NULL_TREE)
25139 dwarf2out_abstract_function (decl);
25140 set_decl_origin_self (decl);
25143 /* Otherwise we're emitting the primary DIE for this decl. */
25144 else if (debug_info_level > DINFO_LEVEL_TERSE)
25146 /* Before we describe the FUNCTION_DECL itself, make sure that we
25147 have its containing type. */
25148 if (!origin)
25149 origin = decl_class_context (decl);
25150 if (origin != NULL_TREE)
25151 gen_type_die (origin, context_die);
25153 /* And its return type. */
25154 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25156 /* And its virtual context. */
25157 if (DECL_VINDEX (decl) != NULL_TREE)
25158 gen_type_die (DECL_CONTEXT (decl), context_die);
25160 /* Make sure we have a member DIE for decl. */
25161 if (origin != NULL_TREE)
25162 gen_type_die_for_member (origin, decl, context_die);
25164 /* And its containing namespace. */
25165 context_die = declare_in_namespace (decl, context_die);
25168 /* Now output a DIE to represent the function itself. */
25169 if (decl)
25170 gen_subprogram_die (decl, context_die);
25171 break;
25173 case TYPE_DECL:
25174 /* If we are in terse mode, don't generate any DIEs to represent any
25175 actual typedefs. */
25176 if (debug_info_level <= DINFO_LEVEL_TERSE)
25177 break;
25179 /* In the special case of a TYPE_DECL node representing the declaration
25180 of some type tag, if the given TYPE_DECL is marked as having been
25181 instantiated from some other (original) TYPE_DECL node (e.g. one which
25182 was generated within the original definition of an inline function) we
25183 used to generate a special (abbreviated) DW_TAG_structure_type,
25184 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25185 should be actually referencing those DIEs, as variable DIEs with that
25186 type would be emitted already in the abstract origin, so it was always
25187 removed during unused type prunning. Don't add anything in this
25188 case. */
25189 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25190 break;
25192 if (is_redundant_typedef (decl))
25193 gen_type_die (TREE_TYPE (decl), context_die);
25194 else
25195 /* Output a DIE to represent the typedef itself. */
25196 gen_typedef_die (decl, context_die);
25197 break;
25199 case LABEL_DECL:
25200 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25201 gen_label_die (decl, context_die);
25202 break;
25204 case VAR_DECL:
25205 case RESULT_DECL:
25206 /* If we are in terse mode, don't generate any DIEs to represent any
25207 variable declarations or definitions. */
25208 if (debug_info_level <= DINFO_LEVEL_TERSE)
25209 break;
25211 /* Output any DIEs that are needed to specify the type of this data
25212 object. */
25213 if (decl_by_reference_p (decl_or_origin))
25214 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25215 else
25216 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25218 /* And its containing type. */
25219 class_origin = decl_class_context (decl_or_origin);
25220 if (class_origin != NULL_TREE)
25221 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25223 /* And its containing namespace. */
25224 context_die = declare_in_namespace (decl_or_origin, context_die);
25226 /* Now output the DIE to represent the data object itself. This gets
25227 complicated because of the possibility that the VAR_DECL really
25228 represents an inlined instance of a formal parameter for an inline
25229 function. */
25230 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25231 if (ultimate_origin != NULL_TREE
25232 && TREE_CODE (ultimate_origin) == PARM_DECL)
25233 gen_formal_parameter_die (decl, origin,
25234 true /* Emit name attribute. */,
25235 context_die);
25236 else
25237 gen_variable_die (decl, origin, context_die);
25238 break;
25240 case FIELD_DECL:
25241 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25242 /* Ignore the nameless fields that are used to skip bits but handle C++
25243 anonymous unions and structs. */
25244 if (DECL_NAME (decl) != NULL_TREE
25245 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25246 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25248 gen_type_die (member_declared_type (decl), context_die);
25249 gen_field_die (decl, ctx, context_die);
25251 break;
25253 case PARM_DECL:
25254 if (DECL_BY_REFERENCE (decl_or_origin))
25255 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25256 else
25257 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25258 return gen_formal_parameter_die (decl, origin,
25259 true /* Emit name attribute. */,
25260 context_die);
25262 case NAMESPACE_DECL:
25263 if (dwarf_version >= 3 || !dwarf_strict)
25264 gen_namespace_die (decl, context_die);
25265 break;
25267 case IMPORTED_DECL:
25268 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25269 DECL_CONTEXT (decl), context_die);
25270 break;
25272 case NAMELIST_DECL:
25273 gen_namelist_decl (DECL_NAME (decl), context_die,
25274 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25275 break;
25277 default:
25278 /* Probably some frontend-internal decl. Assume we don't care. */
25279 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25280 break;
25283 return NULL;
25286 /* Output initial debug information for global DECL. Called at the
25287 end of the parsing process.
25289 This is the initial debug generation process. As such, the DIEs
25290 generated may be incomplete. A later debug generation pass
25291 (dwarf2out_late_global_decl) will augment the information generated
25292 in this pass (e.g., with complete location info). */
25294 static void
25295 dwarf2out_early_global_decl (tree decl)
25297 set_early_dwarf s;
25299 /* gen_decl_die() will set DECL_ABSTRACT because
25300 cgraph_function_possibly_inlined_p() returns true. This is in
25301 turn will cause DW_AT_inline attributes to be set.
25303 This happens because at early dwarf generation, there is no
25304 cgraph information, causing cgraph_function_possibly_inlined_p()
25305 to return true. Trick cgraph_function_possibly_inlined_p()
25306 while we generate dwarf early. */
25307 bool save = symtab->global_info_ready;
25308 symtab->global_info_ready = true;
25310 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25311 other DECLs and they can point to template types or other things
25312 that dwarf2out can't handle when done via dwarf2out_decl. */
25313 if (TREE_CODE (decl) != TYPE_DECL
25314 && TREE_CODE (decl) != PARM_DECL)
25316 tree save_fndecl = current_function_decl;
25317 if (TREE_CODE (decl) == FUNCTION_DECL)
25319 /* No cfun means the symbol has no body, so there's nothing
25320 to emit. */
25321 if (!DECL_STRUCT_FUNCTION (decl))
25322 goto early_decl_exit;
25324 /* For nested functions, make sure we have DIEs for the parents first
25325 so that all nested DIEs are generated at the proper scope in the
25326 first shot. */
25327 tree context = decl_function_context (decl);
25328 if (context != NULL && lookup_decl_die (context) == NULL)
25330 current_function_decl = context;
25331 dwarf2out_decl (context);
25334 current_function_decl = decl;
25336 dwarf2out_decl (decl);
25337 if (TREE_CODE (decl) == FUNCTION_DECL)
25338 current_function_decl = save_fndecl;
25340 early_decl_exit:
25341 symtab->global_info_ready = save;
25344 /* Output debug information for global decl DECL. Called from
25345 toplev.c after compilation proper has finished. */
25347 static void
25348 dwarf2out_late_global_decl (tree decl)
25350 /* Fill-in any location information we were unable to determine
25351 on the first pass. */
25352 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25354 dw_die_ref die = lookup_decl_die (decl);
25356 /* We have to generate early debug late for LTO. */
25357 if (! die && in_lto_p)
25359 dwarf2out_decl (decl);
25360 die = lookup_decl_die (decl);
25363 if (die)
25365 /* We get called via the symtab code invoking late_global_decl
25366 for symbols that are optimized out. Do not add locations
25367 for those. */
25368 varpool_node *node = varpool_node::get (decl);
25369 if (! node || ! node->definition)
25370 tree_add_const_value_attribute_for_decl (die, decl);
25371 else
25372 add_location_or_const_value_attribute (die, decl, false);
25377 /* Output debug information for type decl DECL. Called from toplev.c
25378 and from language front ends (to record built-in types). */
25379 static void
25380 dwarf2out_type_decl (tree decl, int local)
25382 if (!local)
25384 set_early_dwarf s;
25385 dwarf2out_decl (decl);
25389 /* Output debug information for imported module or decl DECL.
25390 NAME is non-NULL name in the lexical block if the decl has been renamed.
25391 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25392 that DECL belongs to.
25393 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25394 static void
25395 dwarf2out_imported_module_or_decl_1 (tree decl,
25396 tree name,
25397 tree lexical_block,
25398 dw_die_ref lexical_block_die)
25400 expanded_location xloc;
25401 dw_die_ref imported_die = NULL;
25402 dw_die_ref at_import_die;
25404 if (TREE_CODE (decl) == IMPORTED_DECL)
25406 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25407 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25408 gcc_assert (decl);
25410 else
25411 xloc = expand_location (input_location);
25413 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25415 at_import_die = force_type_die (TREE_TYPE (decl));
25416 /* For namespace N { typedef void T; } using N::T; base_type_die
25417 returns NULL, but DW_TAG_imported_declaration requires
25418 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25419 if (!at_import_die)
25421 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25422 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25423 at_import_die = lookup_type_die (TREE_TYPE (decl));
25424 gcc_assert (at_import_die);
25427 else
25429 at_import_die = lookup_decl_die (decl);
25430 if (!at_import_die)
25432 /* If we're trying to avoid duplicate debug info, we may not have
25433 emitted the member decl for this field. Emit it now. */
25434 if (TREE_CODE (decl) == FIELD_DECL)
25436 tree type = DECL_CONTEXT (decl);
25438 if (TYPE_CONTEXT (type)
25439 && TYPE_P (TYPE_CONTEXT (type))
25440 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25441 DINFO_USAGE_DIR_USE))
25442 return;
25443 gen_type_die_for_member (type, decl,
25444 get_context_die (TYPE_CONTEXT (type)));
25446 if (TREE_CODE (decl) == NAMELIST_DECL)
25447 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25448 get_context_die (DECL_CONTEXT (decl)),
25449 NULL_TREE);
25450 else
25451 at_import_die = force_decl_die (decl);
25455 if (TREE_CODE (decl) == NAMESPACE_DECL)
25457 if (dwarf_version >= 3 || !dwarf_strict)
25458 imported_die = new_die (DW_TAG_imported_module,
25459 lexical_block_die,
25460 lexical_block);
25461 else
25462 return;
25464 else
25465 imported_die = new_die (DW_TAG_imported_declaration,
25466 lexical_block_die,
25467 lexical_block);
25469 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25470 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25471 if (name)
25472 add_AT_string (imported_die, DW_AT_name,
25473 IDENTIFIER_POINTER (name));
25474 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25477 /* Output debug information for imported module or decl DECL.
25478 NAME is non-NULL name in context if the decl has been renamed.
25479 CHILD is true if decl is one of the renamed decls as part of
25480 importing whole module. */
25482 static void
25483 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25484 bool child)
25486 /* dw_die_ref at_import_die; */
25487 dw_die_ref scope_die;
25489 if (debug_info_level <= DINFO_LEVEL_TERSE)
25490 return;
25492 gcc_assert (decl);
25494 set_early_dwarf s;
25496 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25497 We need decl DIE for reference and scope die. First, get DIE for the decl
25498 itself. */
25500 /* Get the scope die for decl context. Use comp_unit_die for global module
25501 or decl. If die is not found for non globals, force new die. */
25502 if (context
25503 && TYPE_P (context)
25504 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25505 return;
25507 scope_die = get_context_die (context);
25509 if (child)
25511 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25512 there is nothing we can do, here. */
25513 if (dwarf_version < 3 && dwarf_strict)
25514 return;
25516 gcc_assert (scope_die->die_child);
25517 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25518 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25519 scope_die = scope_die->die_child;
25522 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25523 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25526 /* Output debug information for namelists. */
25528 static dw_die_ref
25529 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25531 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25532 tree value;
25533 unsigned i;
25535 if (debug_info_level <= DINFO_LEVEL_TERSE)
25536 return NULL;
25538 gcc_assert (scope_die != NULL);
25539 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25540 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25542 /* If there are no item_decls, we have a nondefining namelist, e.g.
25543 with USE association; hence, set DW_AT_declaration. */
25544 if (item_decls == NULL_TREE)
25546 add_AT_flag (nml_die, DW_AT_declaration, 1);
25547 return nml_die;
25550 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25552 nml_item_ref_die = lookup_decl_die (value);
25553 if (!nml_item_ref_die)
25554 nml_item_ref_die = force_decl_die (value);
25556 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25557 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25559 return nml_die;
25563 /* Write the debugging output for DECL and return the DIE. */
25565 static void
25566 dwarf2out_decl (tree decl)
25568 dw_die_ref context_die = comp_unit_die ();
25570 switch (TREE_CODE (decl))
25572 case ERROR_MARK:
25573 return;
25575 case FUNCTION_DECL:
25576 /* What we would really like to do here is to filter out all mere
25577 file-scope declarations of file-scope functions which are never
25578 referenced later within this translation unit (and keep all of ones
25579 that *are* referenced later on) but we aren't clairvoyant, so we have
25580 no idea which functions will be referenced in the future (i.e. later
25581 on within the current translation unit). So here we just ignore all
25582 file-scope function declarations which are not also definitions. If
25583 and when the debugger needs to know something about these functions,
25584 it will have to hunt around and find the DWARF information associated
25585 with the definition of the function.
25587 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25588 nodes represent definitions and which ones represent mere
25589 declarations. We have to check DECL_INITIAL instead. That's because
25590 the C front-end supports some weird semantics for "extern inline"
25591 function definitions. These can get inlined within the current
25592 translation unit (and thus, we need to generate Dwarf info for their
25593 abstract instances so that the Dwarf info for the concrete inlined
25594 instances can have something to refer to) but the compiler never
25595 generates any out-of-lines instances of such things (despite the fact
25596 that they *are* definitions).
25598 The important point is that the C front-end marks these "extern
25599 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25600 them anyway. Note that the C++ front-end also plays some similar games
25601 for inline function definitions appearing within include files which
25602 also contain `#pragma interface' pragmas.
25604 If we are called from dwarf2out_abstract_function output a DIE
25605 anyway. We can end up here this way with early inlining and LTO
25606 where the inlined function is output in a different LTRANS unit
25607 or not at all. */
25608 if (DECL_INITIAL (decl) == NULL_TREE
25609 && ! DECL_ABSTRACT_P (decl))
25610 return;
25612 /* If we're a nested function, initially use a parent of NULL; if we're
25613 a plain function, this will be fixed up in decls_for_scope. If
25614 we're a method, it will be ignored, since we already have a DIE. */
25615 if (decl_function_context (decl)
25616 /* But if we're in terse mode, we don't care about scope. */
25617 && debug_info_level > DINFO_LEVEL_TERSE)
25618 context_die = NULL;
25619 break;
25621 case VAR_DECL:
25622 /* For local statics lookup proper context die. */
25623 if (local_function_static (decl))
25624 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25626 /* If we are in terse mode, don't generate any DIEs to represent any
25627 variable declarations or definitions. */
25628 if (debug_info_level <= DINFO_LEVEL_TERSE)
25629 return;
25630 break;
25632 case CONST_DECL:
25633 if (debug_info_level <= DINFO_LEVEL_TERSE)
25634 return;
25635 if (!is_fortran () && !is_ada ())
25636 return;
25637 if (TREE_STATIC (decl) && decl_function_context (decl))
25638 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25639 break;
25641 case NAMESPACE_DECL:
25642 case IMPORTED_DECL:
25643 if (debug_info_level <= DINFO_LEVEL_TERSE)
25644 return;
25645 if (lookup_decl_die (decl) != NULL)
25646 return;
25647 break;
25649 case TYPE_DECL:
25650 /* Don't emit stubs for types unless they are needed by other DIEs. */
25651 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25652 return;
25654 /* Don't bother trying to generate any DIEs to represent any of the
25655 normal built-in types for the language we are compiling. */
25656 if (DECL_IS_BUILTIN (decl))
25657 return;
25659 /* If we are in terse mode, don't generate any DIEs for types. */
25660 if (debug_info_level <= DINFO_LEVEL_TERSE)
25661 return;
25663 /* If we're a function-scope tag, initially use a parent of NULL;
25664 this will be fixed up in decls_for_scope. */
25665 if (decl_function_context (decl))
25666 context_die = NULL;
25668 break;
25670 case NAMELIST_DECL:
25671 break;
25673 default:
25674 return;
25677 gen_decl_die (decl, NULL, NULL, context_die);
25679 if (flag_checking)
25681 dw_die_ref die = lookup_decl_die (decl);
25682 if (die)
25683 check_die (die);
25687 /* Write the debugging output for DECL. */
25689 static void
25690 dwarf2out_function_decl (tree decl)
25692 dwarf2out_decl (decl);
25693 call_arg_locations = NULL;
25694 call_arg_loc_last = NULL;
25695 call_site_count = -1;
25696 tail_call_site_count = -1;
25697 decl_loc_table->empty ();
25698 cached_dw_loc_list_table->empty ();
25701 /* Output a marker (i.e. a label) for the beginning of the generated code for
25702 a lexical block. */
25704 static void
25705 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25706 unsigned int blocknum)
25708 switch_to_section (current_function_section ());
25709 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25712 /* Output a marker (i.e. a label) for the end of the generated code for a
25713 lexical block. */
25715 static void
25716 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25718 switch_to_section (current_function_section ());
25719 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25722 /* Returns nonzero if it is appropriate not to emit any debugging
25723 information for BLOCK, because it doesn't contain any instructions.
25725 Don't allow this for blocks with nested functions or local classes
25726 as we would end up with orphans, and in the presence of scheduling
25727 we may end up calling them anyway. */
25729 static bool
25730 dwarf2out_ignore_block (const_tree block)
25732 tree decl;
25733 unsigned int i;
25735 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25736 if (TREE_CODE (decl) == FUNCTION_DECL
25737 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25738 return 0;
25739 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25741 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25742 if (TREE_CODE (decl) == FUNCTION_DECL
25743 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25744 return 0;
25747 return 1;
25750 /* Hash table routines for file_hash. */
25752 bool
25753 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25755 return filename_cmp (p1->filename, p2) == 0;
25758 hashval_t
25759 dwarf_file_hasher::hash (dwarf_file_data *p)
25761 return htab_hash_string (p->filename);
25764 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25765 dwarf2out.c) and return its "index". The index of each (known) filename is
25766 just a unique number which is associated with only that one filename. We
25767 need such numbers for the sake of generating labels (in the .debug_sfnames
25768 section) and references to those files numbers (in the .debug_srcinfo
25769 and .debug_macinfo sections). If the filename given as an argument is not
25770 found in our current list, add it to the list and assign it the next
25771 available unique index number. */
25773 static struct dwarf_file_data *
25774 lookup_filename (const char *file_name)
25776 struct dwarf_file_data * created;
25778 if (!file_name)
25779 return NULL;
25781 dwarf_file_data **slot
25782 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25783 INSERT);
25784 if (*slot)
25785 return *slot;
25787 created = ggc_alloc<dwarf_file_data> ();
25788 created->filename = file_name;
25789 created->emitted_number = 0;
25790 *slot = created;
25791 return created;
25794 /* If the assembler will construct the file table, then translate the compiler
25795 internal file table number into the assembler file table number, and emit
25796 a .file directive if we haven't already emitted one yet. The file table
25797 numbers are different because we prune debug info for unused variables and
25798 types, which may include filenames. */
25800 static int
25801 maybe_emit_file (struct dwarf_file_data * fd)
25803 if (! fd->emitted_number)
25805 if (last_emitted_file)
25806 fd->emitted_number = last_emitted_file->emitted_number + 1;
25807 else
25808 fd->emitted_number = 1;
25809 last_emitted_file = fd;
25811 if (DWARF2_ASM_LINE_DEBUG_INFO)
25813 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25814 output_quoted_string (asm_out_file,
25815 remap_debug_filename (fd->filename));
25816 fputc ('\n', asm_out_file);
25820 return fd->emitted_number;
25823 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25824 That generation should happen after function debug info has been
25825 generated. The value of the attribute is the constant value of ARG. */
25827 static void
25828 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25830 die_arg_entry entry;
25832 if (!die || !arg)
25833 return;
25835 gcc_assert (early_dwarf);
25837 if (!tmpl_value_parm_die_table)
25838 vec_alloc (tmpl_value_parm_die_table, 32);
25840 entry.die = die;
25841 entry.arg = arg;
25842 vec_safe_push (tmpl_value_parm_die_table, entry);
25845 /* Return TRUE if T is an instance of generic type, FALSE
25846 otherwise. */
25848 static bool
25849 generic_type_p (tree t)
25851 if (t == NULL_TREE || !TYPE_P (t))
25852 return false;
25853 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25856 /* Schedule the generation of the generic parameter dies for the
25857 instance of generic type T. The proper generation itself is later
25858 done by gen_scheduled_generic_parms_dies. */
25860 static void
25861 schedule_generic_params_dies_gen (tree t)
25863 if (!generic_type_p (t))
25864 return;
25866 gcc_assert (early_dwarf);
25868 if (!generic_type_instances)
25869 vec_alloc (generic_type_instances, 256);
25871 vec_safe_push (generic_type_instances, t);
25874 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25875 by append_entry_to_tmpl_value_parm_die_table. This function must
25876 be called after function DIEs have been generated. */
25878 static void
25879 gen_remaining_tmpl_value_param_die_attribute (void)
25881 if (tmpl_value_parm_die_table)
25883 unsigned i, j;
25884 die_arg_entry *e;
25886 /* We do this in two phases - first get the cases we can
25887 handle during early-finish, preserving those we cannot
25888 (containing symbolic constants where we don't yet know
25889 whether we are going to output the referenced symbols).
25890 For those we try again at late-finish. */
25891 j = 0;
25892 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
25894 if (!tree_add_const_value_attribute (e->die, e->arg))
25896 dw_loc_descr_ref loc = NULL;
25897 if (! early_dwarf
25898 && (dwarf_version >= 5 || !dwarf_strict))
25899 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
25900 if (loc)
25901 add_AT_loc (e->die, DW_AT_location, loc);
25902 else
25903 (*tmpl_value_parm_die_table)[j++] = *e;
25906 tmpl_value_parm_die_table->truncate (j);
25910 /* Generate generic parameters DIEs for instances of generic types
25911 that have been previously scheduled by
25912 schedule_generic_params_dies_gen. This function must be called
25913 after all the types of the CU have been laid out. */
25915 static void
25916 gen_scheduled_generic_parms_dies (void)
25918 unsigned i;
25919 tree t;
25921 if (!generic_type_instances)
25922 return;
25924 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
25925 if (COMPLETE_TYPE_P (t))
25926 gen_generic_params_dies (t);
25928 generic_type_instances = NULL;
25932 /* Replace DW_AT_name for the decl with name. */
25934 static void
25935 dwarf2out_set_name (tree decl, tree name)
25937 dw_die_ref die;
25938 dw_attr_node *attr;
25939 const char *dname;
25941 die = TYPE_SYMTAB_DIE (decl);
25942 if (!die)
25943 return;
25945 dname = dwarf2_name (name, 0);
25946 if (!dname)
25947 return;
25949 attr = get_AT (die, DW_AT_name);
25950 if (attr)
25952 struct indirect_string_node *node;
25954 node = find_AT_string (dname);
25955 /* replace the string. */
25956 attr->dw_attr_val.v.val_str = node;
25959 else
25960 add_name_attribute (die, dname);
25963 /* True if before or during processing of the first function being emitted. */
25964 static bool in_first_function_p = true;
25965 /* True if loc_note during dwarf2out_var_location call might still be
25966 before first real instruction at address equal to .Ltext0. */
25967 static bool maybe_at_text_label_p = true;
25968 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
25969 static unsigned int first_loclabel_num_not_at_text_label;
25971 /* Called by the final INSN scan whenever we see a var location. We
25972 use it to drop labels in the right places, and throw the location in
25973 our lookup table. */
25975 static void
25976 dwarf2out_var_location (rtx_insn *loc_note)
25978 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
25979 struct var_loc_node *newloc;
25980 rtx_insn *next_real, *next_note;
25981 rtx_insn *call_insn = NULL;
25982 static const char *last_label;
25983 static const char *last_postcall_label;
25984 static bool last_in_cold_section_p;
25985 static rtx_insn *expected_next_loc_note;
25986 tree decl;
25987 bool var_loc_p;
25989 if (!NOTE_P (loc_note))
25991 if (CALL_P (loc_note))
25993 call_site_count++;
25994 if (SIBLING_CALL_P (loc_note))
25995 tail_call_site_count++;
25996 if (optimize == 0 && !flag_var_tracking)
25998 /* When the var-tracking pass is not running, there is no note
25999 for indirect calls whose target is compile-time known. In this
26000 case, process such calls specifically so that we generate call
26001 sites for them anyway. */
26002 rtx x = PATTERN (loc_note);
26003 if (GET_CODE (x) == PARALLEL)
26004 x = XVECEXP (x, 0, 0);
26005 if (GET_CODE (x) == SET)
26006 x = SET_SRC (x);
26007 if (GET_CODE (x) == CALL)
26008 x = XEXP (x, 0);
26009 if (!MEM_P (x)
26010 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26011 || !SYMBOL_REF_DECL (XEXP (x, 0))
26012 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26013 != FUNCTION_DECL))
26015 call_insn = loc_note;
26016 loc_note = NULL;
26017 var_loc_p = false;
26019 next_real = next_real_insn (call_insn);
26020 next_note = NULL;
26021 cached_next_real_insn = NULL;
26022 goto create_label;
26026 return;
26029 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26030 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26031 return;
26033 /* Optimize processing a large consecutive sequence of location
26034 notes so we don't spend too much time in next_real_insn. If the
26035 next insn is another location note, remember the next_real_insn
26036 calculation for next time. */
26037 next_real = cached_next_real_insn;
26038 if (next_real)
26040 if (expected_next_loc_note != loc_note)
26041 next_real = NULL;
26044 next_note = NEXT_INSN (loc_note);
26045 if (! next_note
26046 || next_note->deleted ()
26047 || ! NOTE_P (next_note)
26048 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26049 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26050 next_note = NULL;
26052 if (! next_real)
26053 next_real = next_real_insn (loc_note);
26055 if (next_note)
26057 expected_next_loc_note = next_note;
26058 cached_next_real_insn = next_real;
26060 else
26061 cached_next_real_insn = NULL;
26063 /* If there are no instructions which would be affected by this note,
26064 don't do anything. */
26065 if (var_loc_p
26066 && next_real == NULL_RTX
26067 && !NOTE_DURING_CALL_P (loc_note))
26068 return;
26070 create_label:
26072 if (next_real == NULL_RTX)
26073 next_real = get_last_insn ();
26075 /* If there were any real insns between note we processed last time
26076 and this note (or if it is the first note), clear
26077 last_{,postcall_}label so that they are not reused this time. */
26078 if (last_var_location_insn == NULL_RTX
26079 || last_var_location_insn != next_real
26080 || last_in_cold_section_p != in_cold_section_p)
26082 last_label = NULL;
26083 last_postcall_label = NULL;
26086 if (var_loc_p)
26088 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26089 newloc = add_var_loc_to_decl (decl, loc_note,
26090 NOTE_DURING_CALL_P (loc_note)
26091 ? last_postcall_label : last_label);
26092 if (newloc == NULL)
26093 return;
26095 else
26097 decl = NULL_TREE;
26098 newloc = NULL;
26101 /* If there were no real insns between note we processed last time
26102 and this note, use the label we emitted last time. Otherwise
26103 create a new label and emit it. */
26104 if (last_label == NULL)
26106 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26107 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26108 loclabel_num++;
26109 last_label = ggc_strdup (loclabel);
26110 /* See if loclabel might be equal to .Ltext0. If yes,
26111 bump first_loclabel_num_not_at_text_label. */
26112 if (!have_multiple_function_sections
26113 && in_first_function_p
26114 && maybe_at_text_label_p)
26116 static rtx_insn *last_start;
26117 rtx_insn *insn;
26118 for (insn = loc_note; insn; insn = previous_insn (insn))
26119 if (insn == last_start)
26120 break;
26121 else if (!NONDEBUG_INSN_P (insn))
26122 continue;
26123 else
26125 rtx body = PATTERN (insn);
26126 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26127 continue;
26128 /* Inline asm could occupy zero bytes. */
26129 else if (GET_CODE (body) == ASM_INPUT
26130 || asm_noperands (body) >= 0)
26131 continue;
26132 #ifdef HAVE_attr_length
26133 else if (get_attr_min_length (insn) == 0)
26134 continue;
26135 #endif
26136 else
26138 /* Assume insn has non-zero length. */
26139 maybe_at_text_label_p = false;
26140 break;
26143 if (maybe_at_text_label_p)
26145 last_start = loc_note;
26146 first_loclabel_num_not_at_text_label = loclabel_num;
26151 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26152 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26154 if (!var_loc_p)
26156 struct call_arg_loc_node *ca_loc
26157 = ggc_cleared_alloc<call_arg_loc_node> ();
26158 rtx_insn *prev
26159 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26161 ca_loc->call_arg_loc_note = loc_note;
26162 ca_loc->next = NULL;
26163 ca_loc->label = last_label;
26164 gcc_assert (prev
26165 && (CALL_P (prev)
26166 || (NONJUMP_INSN_P (prev)
26167 && GET_CODE (PATTERN (prev)) == SEQUENCE
26168 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26169 if (!CALL_P (prev))
26170 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26171 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26173 /* Look for a SYMBOL_REF in the "prev" instruction. */
26174 rtx x = get_call_rtx_from (PATTERN (prev));
26175 if (x)
26177 /* Try to get the call symbol, if any. */
26178 if (MEM_P (XEXP (x, 0)))
26179 x = XEXP (x, 0);
26180 /* First, look for a memory access to a symbol_ref. */
26181 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26182 && SYMBOL_REF_DECL (XEXP (x, 0))
26183 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26184 ca_loc->symbol_ref = XEXP (x, 0);
26185 /* Otherwise, look at a compile-time known user-level function
26186 declaration. */
26187 else if (MEM_P (x)
26188 && MEM_EXPR (x)
26189 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26190 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26193 ca_loc->block = insn_scope (prev);
26194 if (call_arg_locations)
26195 call_arg_loc_last->next = ca_loc;
26196 else
26197 call_arg_locations = ca_loc;
26198 call_arg_loc_last = ca_loc;
26200 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26201 newloc->label = last_label;
26202 else
26204 if (!last_postcall_label)
26206 sprintf (loclabel, "%s-1", last_label);
26207 last_postcall_label = ggc_strdup (loclabel);
26209 newloc->label = last_postcall_label;
26212 last_var_location_insn = next_real;
26213 last_in_cold_section_p = in_cold_section_p;
26216 /* Called from finalize_size_functions for size functions so that their body
26217 can be encoded in the debug info to describe the layout of variable-length
26218 structures. */
26220 static void
26221 dwarf2out_size_function (tree decl)
26223 function_to_dwarf_procedure (decl);
26226 /* Note in one location list that text section has changed. */
26229 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26231 var_loc_list *list = *slot;
26232 if (list->first)
26233 list->last_before_switch
26234 = list->last->next ? list->last->next : list->last;
26235 return 1;
26238 /* Note in all location lists that text section has changed. */
26240 static void
26241 var_location_switch_text_section (void)
26243 if (decl_loc_table == NULL)
26244 return;
26246 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26249 /* Create a new line number table. */
26251 static dw_line_info_table *
26252 new_line_info_table (void)
26254 dw_line_info_table *table;
26256 table = ggc_cleared_alloc<dw_line_info_table> ();
26257 table->file_num = 1;
26258 table->line_num = 1;
26259 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26261 return table;
26264 /* Lookup the "current" table into which we emit line info, so
26265 that we don't have to do it for every source line. */
26267 static void
26268 set_cur_line_info_table (section *sec)
26270 dw_line_info_table *table;
26272 if (sec == text_section)
26273 table = text_section_line_info;
26274 else if (sec == cold_text_section)
26276 table = cold_text_section_line_info;
26277 if (!table)
26279 cold_text_section_line_info = table = new_line_info_table ();
26280 table->end_label = cold_end_label;
26283 else
26285 const char *end_label;
26287 if (flag_reorder_blocks_and_partition)
26289 if (in_cold_section_p)
26290 end_label = crtl->subsections.cold_section_end_label;
26291 else
26292 end_label = crtl->subsections.hot_section_end_label;
26294 else
26296 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26297 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26298 current_function_funcdef_no);
26299 end_label = ggc_strdup (label);
26302 table = new_line_info_table ();
26303 table->end_label = end_label;
26305 vec_safe_push (separate_line_info, table);
26308 if (DWARF2_ASM_LINE_DEBUG_INFO)
26309 table->is_stmt = (cur_line_info_table
26310 ? cur_line_info_table->is_stmt
26311 : DWARF_LINE_DEFAULT_IS_STMT_START);
26312 cur_line_info_table = table;
26316 /* We need to reset the locations at the beginning of each
26317 function. We can't do this in the end_function hook, because the
26318 declarations that use the locations won't have been output when
26319 that hook is called. Also compute have_multiple_function_sections here. */
26321 static void
26322 dwarf2out_begin_function (tree fun)
26324 section *sec = function_section (fun);
26326 if (sec != text_section)
26327 have_multiple_function_sections = true;
26329 if (flag_reorder_blocks_and_partition && !cold_text_section)
26331 gcc_assert (current_function_decl == fun);
26332 cold_text_section = unlikely_text_section ();
26333 switch_to_section (cold_text_section);
26334 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26335 switch_to_section (sec);
26338 dwarf2out_note_section_used ();
26339 call_site_count = 0;
26340 tail_call_site_count = 0;
26342 set_cur_line_info_table (sec);
26345 /* Helper function of dwarf2out_end_function, called only after emitting
26346 the very first function into assembly. Check if some .debug_loc range
26347 might end with a .LVL* label that could be equal to .Ltext0.
26348 In that case we must force using absolute addresses in .debug_loc ranges,
26349 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26350 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26351 list terminator.
26352 Set have_multiple_function_sections to true in that case and
26353 terminate htab traversal. */
26356 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26358 var_loc_list *entry = *slot;
26359 struct var_loc_node *node;
26361 node = entry->first;
26362 if (node && node->next && node->next->label)
26364 unsigned int i;
26365 const char *label = node->next->label;
26366 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26368 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26370 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26371 if (strcmp (label, loclabel) == 0)
26373 have_multiple_function_sections = true;
26374 return 0;
26378 return 1;
26381 /* Hook called after emitting a function into assembly.
26382 This does something only for the very first function emitted. */
26384 static void
26385 dwarf2out_end_function (unsigned int)
26387 if (in_first_function_p
26388 && !have_multiple_function_sections
26389 && first_loclabel_num_not_at_text_label
26390 && decl_loc_table)
26391 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26392 in_first_function_p = false;
26393 maybe_at_text_label_p = false;
26396 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26397 front-ends register a translation unit even before dwarf2out_init is
26398 called. */
26399 static tree main_translation_unit = NULL_TREE;
26401 /* Hook called by front-ends after they built their main translation unit.
26402 Associate comp_unit_die to UNIT. */
26404 static void
26405 dwarf2out_register_main_translation_unit (tree unit)
26407 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26408 && main_translation_unit == NULL_TREE);
26409 main_translation_unit = unit;
26410 /* If dwarf2out_init has not been called yet, it will perform the association
26411 itself looking at main_translation_unit. */
26412 if (decl_die_table != NULL)
26413 equate_decl_number_to_die (unit, comp_unit_die ());
26416 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26418 static void
26419 push_dw_line_info_entry (dw_line_info_table *table,
26420 enum dw_line_info_opcode opcode, unsigned int val)
26422 dw_line_info_entry e;
26423 e.opcode = opcode;
26424 e.val = val;
26425 vec_safe_push (table->entries, e);
26428 /* Output a label to mark the beginning of a source code line entry
26429 and record information relating to this source line, in
26430 'line_info_table' for later output of the .debug_line section. */
26431 /* ??? The discriminator parameter ought to be unsigned. */
26433 static void
26434 dwarf2out_source_line (unsigned int line, const char *filename,
26435 int discriminator, bool is_stmt)
26437 unsigned int file_num;
26438 dw_line_info_table *table;
26440 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26441 return;
26443 /* The discriminator column was added in dwarf4. Simplify the below
26444 by simply removing it if we're not supposed to output it. */
26445 if (dwarf_version < 4 && dwarf_strict)
26446 discriminator = 0;
26448 table = cur_line_info_table;
26449 file_num = maybe_emit_file (lookup_filename (filename));
26451 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26452 the debugger has used the second (possibly duplicate) line number
26453 at the beginning of the function to mark the end of the prologue.
26454 We could eliminate any other duplicates within the function. For
26455 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26456 that second line number entry. */
26457 /* Recall that this end-of-prologue indication is *not* the same thing
26458 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26459 to which the hook corresponds, follows the last insn that was
26460 emitted by gen_prologue. What we need is to precede the first insn
26461 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26462 insn that corresponds to something the user wrote. These may be
26463 very different locations once scheduling is enabled. */
26465 if (0 && file_num == table->file_num
26466 && line == table->line_num
26467 && discriminator == table->discrim_num
26468 && is_stmt == table->is_stmt)
26469 return;
26471 switch_to_section (current_function_section ());
26473 /* If requested, emit something human-readable. */
26474 if (flag_debug_asm)
26475 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
26477 if (DWARF2_ASM_LINE_DEBUG_INFO)
26479 /* Emit the .loc directive understood by GNU as. */
26480 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26481 file_num, line, is_stmt, discriminator */
26482 fputs ("\t.loc ", asm_out_file);
26483 fprint_ul (asm_out_file, file_num);
26484 putc (' ', asm_out_file);
26485 fprint_ul (asm_out_file, line);
26486 putc (' ', asm_out_file);
26487 putc ('0', asm_out_file);
26489 if (is_stmt != table->is_stmt)
26491 fputs (" is_stmt ", asm_out_file);
26492 putc (is_stmt ? '1' : '0', asm_out_file);
26494 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26496 gcc_assert (discriminator > 0);
26497 fputs (" discriminator ", asm_out_file);
26498 fprint_ul (asm_out_file, (unsigned long) discriminator);
26500 putc ('\n', asm_out_file);
26502 else
26504 unsigned int label_num = ++line_info_label_num;
26506 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26508 push_dw_line_info_entry (table, LI_set_address, label_num);
26509 if (file_num != table->file_num)
26510 push_dw_line_info_entry (table, LI_set_file, file_num);
26511 if (discriminator != table->discrim_num)
26512 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26513 if (is_stmt != table->is_stmt)
26514 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26515 push_dw_line_info_entry (table, LI_set_line, line);
26518 table->file_num = file_num;
26519 table->line_num = line;
26520 table->discrim_num = discriminator;
26521 table->is_stmt = is_stmt;
26522 table->in_use = true;
26525 /* Record the beginning of a new source file. */
26527 static void
26528 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26530 if (flag_eliminate_dwarf2_dups)
26532 /* Record the beginning of the file for break_out_includes. */
26533 dw_die_ref bincl_die;
26535 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26536 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26539 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26541 macinfo_entry e;
26542 e.code = DW_MACINFO_start_file;
26543 e.lineno = lineno;
26544 e.info = ggc_strdup (filename);
26545 vec_safe_push (macinfo_table, e);
26549 /* Record the end of a source file. */
26551 static void
26552 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26554 if (flag_eliminate_dwarf2_dups)
26555 /* Record the end of the file for break_out_includes. */
26556 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26558 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26560 macinfo_entry e;
26561 e.code = DW_MACINFO_end_file;
26562 e.lineno = lineno;
26563 e.info = NULL;
26564 vec_safe_push (macinfo_table, e);
26568 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26569 the tail part of the directive line, i.e. the part which is past the
26570 initial whitespace, #, whitespace, directive-name, whitespace part. */
26572 static void
26573 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26574 const char *buffer ATTRIBUTE_UNUSED)
26576 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26578 macinfo_entry e;
26579 /* Insert a dummy first entry to be able to optimize the whole
26580 predefined macro block using DW_MACRO_import. */
26581 if (macinfo_table->is_empty () && lineno <= 1)
26583 e.code = 0;
26584 e.lineno = 0;
26585 e.info = NULL;
26586 vec_safe_push (macinfo_table, e);
26588 e.code = DW_MACINFO_define;
26589 e.lineno = lineno;
26590 e.info = ggc_strdup (buffer);
26591 vec_safe_push (macinfo_table, e);
26595 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26596 the tail part of the directive line, i.e. the part which is past the
26597 initial whitespace, #, whitespace, directive-name, whitespace part. */
26599 static void
26600 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26601 const char *buffer ATTRIBUTE_UNUSED)
26603 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26605 macinfo_entry e;
26606 /* Insert a dummy first entry to be able to optimize the whole
26607 predefined macro block using DW_MACRO_import. */
26608 if (macinfo_table->is_empty () && lineno <= 1)
26610 e.code = 0;
26611 e.lineno = 0;
26612 e.info = NULL;
26613 vec_safe_push (macinfo_table, e);
26615 e.code = DW_MACINFO_undef;
26616 e.lineno = lineno;
26617 e.info = ggc_strdup (buffer);
26618 vec_safe_push (macinfo_table, e);
26622 /* Helpers to manipulate hash table of CUs. */
26624 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26626 static inline hashval_t hash (const macinfo_entry *);
26627 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26630 inline hashval_t
26631 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26633 return htab_hash_string (entry->info);
26636 inline bool
26637 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26638 const macinfo_entry *entry2)
26640 return !strcmp (entry1->info, entry2->info);
26643 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26645 /* Output a single .debug_macinfo entry. */
26647 static void
26648 output_macinfo_op (macinfo_entry *ref)
26650 int file_num;
26651 size_t len;
26652 struct indirect_string_node *node;
26653 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26654 struct dwarf_file_data *fd;
26656 switch (ref->code)
26658 case DW_MACINFO_start_file:
26659 fd = lookup_filename (ref->info);
26660 file_num = maybe_emit_file (fd);
26661 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26662 dw2_asm_output_data_uleb128 (ref->lineno,
26663 "Included from line number %lu",
26664 (unsigned long) ref->lineno);
26665 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26666 break;
26667 case DW_MACINFO_end_file:
26668 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26669 break;
26670 case DW_MACINFO_define:
26671 case DW_MACINFO_undef:
26672 len = strlen (ref->info) + 1;
26673 if (!dwarf_strict
26674 && len > DWARF_OFFSET_SIZE
26675 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26676 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26678 ref->code = ref->code == DW_MACINFO_define
26679 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26680 output_macinfo_op (ref);
26681 return;
26683 dw2_asm_output_data (1, ref->code,
26684 ref->code == DW_MACINFO_define
26685 ? "Define macro" : "Undefine macro");
26686 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26687 (unsigned long) ref->lineno);
26688 dw2_asm_output_nstring (ref->info, -1, "The macro");
26689 break;
26690 case DW_MACRO_define_strp:
26691 case DW_MACRO_undef_strp:
26692 node = find_AT_string (ref->info);
26693 gcc_assert (node
26694 && (node->form == DW_FORM_strp
26695 || node->form == DW_FORM_GNU_str_index));
26696 dw2_asm_output_data (1, ref->code,
26697 ref->code == DW_MACRO_define_strp
26698 ? "Define macro strp"
26699 : "Undefine macro strp");
26700 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26701 (unsigned long) ref->lineno);
26702 if (node->form == DW_FORM_strp)
26703 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26704 debug_str_section, "The macro: \"%s\"",
26705 ref->info);
26706 else
26707 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26708 ref->info);
26709 break;
26710 case DW_MACRO_import:
26711 dw2_asm_output_data (1, ref->code, "Import");
26712 ASM_GENERATE_INTERNAL_LABEL (label,
26713 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26714 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26715 break;
26716 default:
26717 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26718 ASM_COMMENT_START, (unsigned long) ref->code);
26719 break;
26723 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26724 other compilation unit .debug_macinfo sections. IDX is the first
26725 index of a define/undef, return the number of ops that should be
26726 emitted in a comdat .debug_macinfo section and emit
26727 a DW_MACRO_import entry referencing it.
26728 If the define/undef entry should be emitted normally, return 0. */
26730 static unsigned
26731 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26732 macinfo_hash_type **macinfo_htab)
26734 macinfo_entry *first, *second, *cur, *inc;
26735 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26736 unsigned char checksum[16];
26737 struct md5_ctx ctx;
26738 char *grp_name, *tail;
26739 const char *base;
26740 unsigned int i, count, encoded_filename_len, linebuf_len;
26741 macinfo_entry **slot;
26743 first = &(*macinfo_table)[idx];
26744 second = &(*macinfo_table)[idx + 1];
26746 /* Optimize only if there are at least two consecutive define/undef ops,
26747 and either all of them are before first DW_MACINFO_start_file
26748 with lineno {0,1} (i.e. predefined macro block), or all of them are
26749 in some included header file. */
26750 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26751 return 0;
26752 if (vec_safe_is_empty (files))
26754 if (first->lineno > 1 || second->lineno > 1)
26755 return 0;
26757 else if (first->lineno == 0)
26758 return 0;
26760 /* Find the last define/undef entry that can be grouped together
26761 with first and at the same time compute md5 checksum of their
26762 codes, linenumbers and strings. */
26763 md5_init_ctx (&ctx);
26764 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26765 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26766 break;
26767 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26768 break;
26769 else
26771 unsigned char code = cur->code;
26772 md5_process_bytes (&code, 1, &ctx);
26773 checksum_uleb128 (cur->lineno, &ctx);
26774 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26776 md5_finish_ctx (&ctx, checksum);
26777 count = i - idx;
26779 /* From the containing include filename (if any) pick up just
26780 usable characters from its basename. */
26781 if (vec_safe_is_empty (files))
26782 base = "";
26783 else
26784 base = lbasename (files->last ().info);
26785 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26786 if (ISIDNUM (base[i]) || base[i] == '.')
26787 encoded_filename_len++;
26788 /* Count . at the end. */
26789 if (encoded_filename_len)
26790 encoded_filename_len++;
26792 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26793 linebuf_len = strlen (linebuf);
26795 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26796 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26797 + 16 * 2 + 1);
26798 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26799 tail = grp_name + 4;
26800 if (encoded_filename_len)
26802 for (i = 0; base[i]; i++)
26803 if (ISIDNUM (base[i]) || base[i] == '.')
26804 *tail++ = base[i];
26805 *tail++ = '.';
26807 memcpy (tail, linebuf, linebuf_len);
26808 tail += linebuf_len;
26809 *tail++ = '.';
26810 for (i = 0; i < 16; i++)
26811 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26813 /* Construct a macinfo_entry for DW_MACRO_import
26814 in the empty vector entry before the first define/undef. */
26815 inc = &(*macinfo_table)[idx - 1];
26816 inc->code = DW_MACRO_import;
26817 inc->lineno = 0;
26818 inc->info = ggc_strdup (grp_name);
26819 if (!*macinfo_htab)
26820 *macinfo_htab = new macinfo_hash_type (10);
26821 /* Avoid emitting duplicates. */
26822 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26823 if (*slot != NULL)
26825 inc->code = 0;
26826 inc->info = NULL;
26827 /* If such an entry has been used before, just emit
26828 a DW_MACRO_import op. */
26829 inc = *slot;
26830 output_macinfo_op (inc);
26831 /* And clear all macinfo_entry in the range to avoid emitting them
26832 in the second pass. */
26833 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26835 cur->code = 0;
26836 cur->info = NULL;
26839 else
26841 *slot = inc;
26842 inc->lineno = (*macinfo_htab)->elements ();
26843 output_macinfo_op (inc);
26845 return count;
26848 /* Save any strings needed by the macinfo table in the debug str
26849 table. All strings must be collected into the table by the time
26850 index_string is called. */
26852 static void
26853 save_macinfo_strings (void)
26855 unsigned len;
26856 unsigned i;
26857 macinfo_entry *ref;
26859 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26861 switch (ref->code)
26863 /* Match the logic in output_macinfo_op to decide on
26864 indirect strings. */
26865 case DW_MACINFO_define:
26866 case DW_MACINFO_undef:
26867 len = strlen (ref->info) + 1;
26868 if (!dwarf_strict
26869 && len > DWARF_OFFSET_SIZE
26870 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26871 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26872 set_indirect_string (find_AT_string (ref->info));
26873 break;
26874 case DW_MACRO_define_strp:
26875 case DW_MACRO_undef_strp:
26876 set_indirect_string (find_AT_string (ref->info));
26877 break;
26878 default:
26879 break;
26884 /* Output macinfo section(s). */
26886 static void
26887 output_macinfo (void)
26889 unsigned i;
26890 unsigned long length = vec_safe_length (macinfo_table);
26891 macinfo_entry *ref;
26892 vec<macinfo_entry, va_gc> *files = NULL;
26893 macinfo_hash_type *macinfo_htab = NULL;
26895 if (! length)
26896 return;
26898 /* output_macinfo* uses these interchangeably. */
26899 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
26900 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
26901 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
26902 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
26904 /* For .debug_macro emit the section header. */
26905 if (!dwarf_strict || dwarf_version >= 5)
26907 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26908 "DWARF macro version number");
26909 if (DWARF_OFFSET_SIZE == 8)
26910 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
26911 else
26912 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
26913 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
26914 (!dwarf_split_debug_info ? debug_line_section_label
26915 : debug_skeleton_line_section_label),
26916 debug_line_section, NULL);
26919 /* In the first loop, it emits the primary .debug_macinfo section
26920 and after each emitted op the macinfo_entry is cleared.
26921 If a longer range of define/undef ops can be optimized using
26922 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
26923 the vector before the first define/undef in the range and the
26924 whole range of define/undef ops is not emitted and kept. */
26925 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26927 switch (ref->code)
26929 case DW_MACINFO_start_file:
26930 vec_safe_push (files, *ref);
26931 break;
26932 case DW_MACINFO_end_file:
26933 if (!vec_safe_is_empty (files))
26934 files->pop ();
26935 break;
26936 case DW_MACINFO_define:
26937 case DW_MACINFO_undef:
26938 if ((!dwarf_strict || dwarf_version >= 5)
26939 && HAVE_COMDAT_GROUP
26940 && vec_safe_length (files) != 1
26941 && i > 0
26942 && i + 1 < length
26943 && (*macinfo_table)[i - 1].code == 0)
26945 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
26946 if (count)
26948 i += count - 1;
26949 continue;
26952 break;
26953 case 0:
26954 /* A dummy entry may be inserted at the beginning to be able
26955 to optimize the whole block of predefined macros. */
26956 if (i == 0)
26957 continue;
26958 default:
26959 break;
26961 output_macinfo_op (ref);
26962 ref->info = NULL;
26963 ref->code = 0;
26966 if (!macinfo_htab)
26967 return;
26969 delete macinfo_htab;
26970 macinfo_htab = NULL;
26972 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
26973 terminate the current chain and switch to a new comdat .debug_macinfo
26974 section and emit the define/undef entries within it. */
26975 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26976 switch (ref->code)
26978 case 0:
26979 continue;
26980 case DW_MACRO_import:
26982 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26983 tree comdat_key = get_identifier (ref->info);
26984 /* Terminate the previous .debug_macinfo section. */
26985 dw2_asm_output_data (1, 0, "End compilation unit");
26986 targetm.asm_out.named_section (debug_macinfo_section_name,
26987 SECTION_DEBUG
26988 | SECTION_LINKONCE,
26989 comdat_key);
26990 ASM_GENERATE_INTERNAL_LABEL (label,
26991 DEBUG_MACRO_SECTION_LABEL,
26992 ref->lineno);
26993 ASM_OUTPUT_LABEL (asm_out_file, label);
26994 ref->code = 0;
26995 ref->info = NULL;
26996 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26997 "DWARF macro version number");
26998 if (DWARF_OFFSET_SIZE == 8)
26999 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27000 else
27001 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27003 break;
27004 case DW_MACINFO_define:
27005 case DW_MACINFO_undef:
27006 output_macinfo_op (ref);
27007 ref->code = 0;
27008 ref->info = NULL;
27009 break;
27010 default:
27011 gcc_unreachable ();
27015 /* Initialize the various sections and labels for dwarf output. */
27017 static void
27018 init_sections_and_labels (void)
27020 if (!dwarf_split_debug_info)
27022 debug_info_section = get_section (DEBUG_INFO_SECTION,
27023 SECTION_DEBUG, NULL);
27024 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27025 SECTION_DEBUG, NULL);
27026 debug_loc_section = get_section (dwarf_version >= 5
27027 ? DEBUG_LOCLISTS_SECTION
27028 : DEBUG_LOC_SECTION,
27029 SECTION_DEBUG, NULL);
27030 debug_macinfo_section_name
27031 = (dwarf_strict && dwarf_version < 5)
27032 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27033 debug_macinfo_section = get_section (debug_macinfo_section_name,
27034 SECTION_DEBUG, NULL);
27036 else
27038 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27039 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27040 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27041 SECTION_DEBUG | SECTION_EXCLUDE,
27042 NULL);
27043 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27044 SECTION_DEBUG, NULL);
27045 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27046 SECTION_DEBUG, NULL);
27047 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27048 SECTION_DEBUG, NULL);
27049 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27050 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27052 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27053 the main .o, but the skeleton_line goes into the split off dwo. */
27054 debug_skeleton_line_section
27055 = get_section (DEBUG_DWO_LINE_SECTION,
27056 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27057 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27058 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27059 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27060 SECTION_DEBUG | SECTION_EXCLUDE,
27061 NULL);
27062 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27063 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27064 debug_loc_section = get_section (dwarf_version >= 5
27065 ? DEBUG_DWO_LOCLISTS_SECTION
27066 : DEBUG_DWO_LOC_SECTION,
27067 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27068 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27069 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27070 debug_macinfo_section_name
27071 = (dwarf_strict && dwarf_version < 5)
27072 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27073 debug_macinfo_section = get_section (debug_macinfo_section_name,
27074 SECTION_DEBUG | SECTION_EXCLUDE,
27075 NULL);
27077 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27078 SECTION_DEBUG, NULL);
27079 debug_line_section = get_section (DEBUG_LINE_SECTION,
27080 SECTION_DEBUG, NULL);
27081 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27082 SECTION_DEBUG, NULL);
27083 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27084 SECTION_DEBUG, NULL);
27085 debug_str_section = get_section (DEBUG_STR_SECTION,
27086 DEBUG_STR_SECTION_FLAGS, NULL);
27087 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27088 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27089 DEBUG_STR_SECTION_FLAGS, NULL);
27091 debug_ranges_section = get_section (dwarf_version >= 5
27092 ? DEBUG_RNGLISTS_SECTION
27093 : DEBUG_RANGES_SECTION,
27094 SECTION_DEBUG, NULL);
27095 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27096 SECTION_DEBUG, NULL);
27098 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27099 DEBUG_ABBREV_SECTION_LABEL, 0);
27100 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27101 DEBUG_INFO_SECTION_LABEL, 0);
27102 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27103 DEBUG_LINE_SECTION_LABEL, 0);
27104 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27105 DEBUG_RANGES_SECTION_LABEL, 0);
27106 if (dwarf_version >= 5 && dwarf_split_debug_info)
27107 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27108 DEBUG_RANGES_SECTION_LABEL, 1);
27109 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27110 DEBUG_ADDR_SECTION_LABEL, 0);
27111 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27112 (dwarf_strict && dwarf_version < 5)
27113 ? DEBUG_MACINFO_SECTION_LABEL
27114 : DEBUG_MACRO_SECTION_LABEL, 0);
27115 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27118 /* Set up for Dwarf output at the start of compilation. */
27120 static void
27121 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27123 /* This option is currently broken, see (PR53118 and PR46102). */
27124 if (flag_eliminate_dwarf2_dups
27125 && strstr (lang_hooks.name, "C++"))
27127 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27128 flag_eliminate_dwarf2_dups = 0;
27131 /* Allocate the file_table. */
27132 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27134 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27135 /* Allocate the decl_die_table. */
27136 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27138 /* Allocate the decl_loc_table. */
27139 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27141 /* Allocate the cached_dw_loc_list_table. */
27142 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27144 /* Allocate the initial hunk of the decl_scope_table. */
27145 vec_alloc (decl_scope_table, 256);
27147 /* Allocate the initial hunk of the abbrev_die_table. */
27148 vec_alloc (abbrev_die_table, 256);
27149 /* Zero-th entry is allocated, but unused. */
27150 abbrev_die_table->quick_push (NULL);
27152 /* Allocate the dwarf_proc_stack_usage_map. */
27153 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27155 /* Allocate the pubtypes and pubnames vectors. */
27156 vec_alloc (pubname_table, 32);
27157 vec_alloc (pubtype_table, 32);
27159 vec_alloc (incomplete_types, 64);
27161 vec_alloc (used_rtx_array, 32);
27163 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27164 vec_alloc (macinfo_table, 64);
27165 #endif
27167 /* If front-ends already registered a main translation unit but we were not
27168 ready to perform the association, do this now. */
27169 if (main_translation_unit != NULL_TREE)
27170 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27173 /* Called before compile () starts outputtting functions, variables
27174 and toplevel asms into assembly. */
27176 static void
27177 dwarf2out_assembly_start (void)
27179 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27180 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27181 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27182 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27183 COLD_TEXT_SECTION_LABEL, 0);
27184 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27186 switch_to_section (text_section);
27187 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27188 #endif
27190 /* Make sure the line number table for .text always exists. */
27191 text_section_line_info = new_line_info_table ();
27192 text_section_line_info->end_label = text_end_label;
27194 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27195 cur_line_info_table = text_section_line_info;
27196 #endif
27198 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27199 && dwarf2out_do_cfi_asm ()
27200 && (!(flag_unwind_tables || flag_exceptions)
27201 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27202 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27205 /* A helper function for dwarf2out_finish called through
27206 htab_traverse. Assign a string its index. All strings must be
27207 collected into the table by the time index_string is called,
27208 because the indexing code relies on htab_traverse to traverse nodes
27209 in the same order for each run. */
27212 index_string (indirect_string_node **h, unsigned int *index)
27214 indirect_string_node *node = *h;
27216 find_string_form (node);
27217 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27219 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27220 node->index = *index;
27221 *index += 1;
27223 return 1;
27226 /* A helper function for output_indirect_strings called through
27227 htab_traverse. Output the offset to a string and update the
27228 current offset. */
27231 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27233 indirect_string_node *node = *h;
27235 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27237 /* Assert that this node has been assigned an index. */
27238 gcc_assert (node->index != NO_INDEX_ASSIGNED
27239 && node->index != NOT_INDEXED);
27240 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27241 "indexed string 0x%x: %s", node->index, node->str);
27242 *offset += strlen (node->str) + 1;
27244 return 1;
27247 /* A helper function for dwarf2out_finish called through
27248 htab_traverse. Output the indexed string. */
27251 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27253 struct indirect_string_node *node = *h;
27255 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27257 /* Assert that the strings are output in the same order as their
27258 indexes were assigned. */
27259 gcc_assert (*cur_idx == node->index);
27260 assemble_string (node->str, strlen (node->str) + 1);
27261 *cur_idx += 1;
27263 return 1;
27266 /* A helper function for dwarf2out_finish called through
27267 htab_traverse. Emit one queued .debug_str string. */
27270 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27272 struct indirect_string_node *node = *h;
27274 node->form = find_string_form (node);
27275 if (node->form == form && node->refcount > 0)
27277 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27278 assemble_string (node->str, strlen (node->str) + 1);
27281 return 1;
27284 /* Output the indexed string table. */
27286 static void
27287 output_indirect_strings (void)
27289 switch_to_section (debug_str_section);
27290 if (!dwarf_split_debug_info)
27291 debug_str_hash->traverse<enum dwarf_form,
27292 output_indirect_string> (DW_FORM_strp);
27293 else
27295 unsigned int offset = 0;
27296 unsigned int cur_idx = 0;
27298 skeleton_debug_str_hash->traverse<enum dwarf_form,
27299 output_indirect_string> (DW_FORM_strp);
27301 switch_to_section (debug_str_offsets_section);
27302 debug_str_hash->traverse_noresize
27303 <unsigned int *, output_index_string_offset> (&offset);
27304 switch_to_section (debug_str_dwo_section);
27305 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27306 (&cur_idx);
27310 /* Callback for htab_traverse to assign an index to an entry in the
27311 table, and to write that entry to the .debug_addr section. */
27314 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27316 addr_table_entry *entry = *slot;
27318 if (entry->refcount == 0)
27320 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27321 || entry->index == NOT_INDEXED);
27322 return 1;
27325 gcc_assert (entry->index == *cur_index);
27326 (*cur_index)++;
27328 switch (entry->kind)
27330 case ate_kind_rtx:
27331 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27332 "0x%x", entry->index);
27333 break;
27334 case ate_kind_rtx_dtprel:
27335 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27336 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27337 DWARF2_ADDR_SIZE,
27338 entry->addr.rtl);
27339 fputc ('\n', asm_out_file);
27340 break;
27341 case ate_kind_label:
27342 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27343 "0x%x", entry->index);
27344 break;
27345 default:
27346 gcc_unreachable ();
27348 return 1;
27351 /* Produce the .debug_addr section. */
27353 static void
27354 output_addr_table (void)
27356 unsigned int index = 0;
27357 if (addr_index_table == NULL || addr_index_table->size () == 0)
27358 return;
27360 switch_to_section (debug_addr_section);
27361 addr_index_table
27362 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27365 #if ENABLE_ASSERT_CHECKING
27366 /* Verify that all marks are clear. */
27368 static void
27369 verify_marks_clear (dw_die_ref die)
27371 dw_die_ref c;
27373 gcc_assert (! die->die_mark);
27374 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27376 #endif /* ENABLE_ASSERT_CHECKING */
27378 /* Clear the marks for a die and its children.
27379 Be cool if the mark isn't set. */
27381 static void
27382 prune_unmark_dies (dw_die_ref die)
27384 dw_die_ref c;
27386 if (die->die_mark)
27387 die->die_mark = 0;
27388 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27391 /* Given LOC that is referenced by a DIE we're marking as used, find all
27392 referenced DWARF procedures it references and mark them as used. */
27394 static void
27395 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27397 for (; loc != NULL; loc = loc->dw_loc_next)
27398 switch (loc->dw_loc_opc)
27400 case DW_OP_implicit_pointer:
27401 case DW_OP_convert:
27402 case DW_OP_reinterpret:
27403 case DW_OP_GNU_implicit_pointer:
27404 case DW_OP_GNU_convert:
27405 case DW_OP_GNU_reinterpret:
27406 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27407 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27408 break;
27409 case DW_OP_call2:
27410 case DW_OP_call4:
27411 case DW_OP_call_ref:
27412 case DW_OP_const_type:
27413 case DW_OP_GNU_const_type:
27414 case DW_OP_GNU_parameter_ref:
27415 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27416 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27417 break;
27418 case DW_OP_regval_type:
27419 case DW_OP_deref_type:
27420 case DW_OP_GNU_regval_type:
27421 case DW_OP_GNU_deref_type:
27422 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27423 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27424 break;
27425 case DW_OP_entry_value:
27426 case DW_OP_GNU_entry_value:
27427 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27428 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27429 break;
27430 default:
27431 break;
27435 /* Given DIE that we're marking as used, find any other dies
27436 it references as attributes and mark them as used. */
27438 static void
27439 prune_unused_types_walk_attribs (dw_die_ref die)
27441 dw_attr_node *a;
27442 unsigned ix;
27444 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27446 switch (AT_class (a))
27448 /* Make sure DWARF procedures referenced by location descriptions will
27449 get emitted. */
27450 case dw_val_class_loc:
27451 prune_unused_types_walk_loc_descr (AT_loc (a));
27452 break;
27453 case dw_val_class_loc_list:
27454 for (dw_loc_list_ref list = AT_loc_list (a);
27455 list != NULL;
27456 list = list->dw_loc_next)
27457 prune_unused_types_walk_loc_descr (list->expr);
27458 break;
27460 case dw_val_class_die_ref:
27461 /* A reference to another DIE.
27462 Make sure that it will get emitted.
27463 If it was broken out into a comdat group, don't follow it. */
27464 if (! AT_ref (a)->comdat_type_p
27465 || a->dw_attr == DW_AT_specification)
27466 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27467 break;
27469 case dw_val_class_str:
27470 /* Set the string's refcount to 0 so that prune_unused_types_mark
27471 accounts properly for it. */
27472 a->dw_attr_val.v.val_str->refcount = 0;
27473 break;
27475 default:
27476 break;
27481 /* Mark the generic parameters and arguments children DIEs of DIE. */
27483 static void
27484 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27486 dw_die_ref c;
27488 if (die == NULL || die->die_child == NULL)
27489 return;
27490 c = die->die_child;
27493 if (is_template_parameter (c))
27494 prune_unused_types_mark (c, 1);
27495 c = c->die_sib;
27496 } while (c && c != die->die_child);
27499 /* Mark DIE as being used. If DOKIDS is true, then walk down
27500 to DIE's children. */
27502 static void
27503 prune_unused_types_mark (dw_die_ref die, int dokids)
27505 dw_die_ref c;
27507 if (die->die_mark == 0)
27509 /* We haven't done this node yet. Mark it as used. */
27510 die->die_mark = 1;
27511 /* If this is the DIE of a generic type instantiation,
27512 mark the children DIEs that describe its generic parms and
27513 args. */
27514 prune_unused_types_mark_generic_parms_dies (die);
27516 /* We also have to mark its parents as used.
27517 (But we don't want to mark our parent's kids due to this,
27518 unless it is a class.) */
27519 if (die->die_parent)
27520 prune_unused_types_mark (die->die_parent,
27521 class_scope_p (die->die_parent));
27523 /* Mark any referenced nodes. */
27524 prune_unused_types_walk_attribs (die);
27526 /* If this node is a specification,
27527 also mark the definition, if it exists. */
27528 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27529 prune_unused_types_mark (die->die_definition, 1);
27532 if (dokids && die->die_mark != 2)
27534 /* We need to walk the children, but haven't done so yet.
27535 Remember that we've walked the kids. */
27536 die->die_mark = 2;
27538 /* If this is an array type, we need to make sure our
27539 kids get marked, even if they're types. If we're
27540 breaking out types into comdat sections, do this
27541 for all type definitions. */
27542 if (die->die_tag == DW_TAG_array_type
27543 || (use_debug_types
27544 && is_type_die (die) && ! is_declaration_die (die)))
27545 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27546 else
27547 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27551 /* For local classes, look if any static member functions were emitted
27552 and if so, mark them. */
27554 static void
27555 prune_unused_types_walk_local_classes (dw_die_ref die)
27557 dw_die_ref c;
27559 if (die->die_mark == 2)
27560 return;
27562 switch (die->die_tag)
27564 case DW_TAG_structure_type:
27565 case DW_TAG_union_type:
27566 case DW_TAG_class_type:
27567 break;
27569 case DW_TAG_subprogram:
27570 if (!get_AT_flag (die, DW_AT_declaration)
27571 || die->die_definition != NULL)
27572 prune_unused_types_mark (die, 1);
27573 return;
27575 default:
27576 return;
27579 /* Mark children. */
27580 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27583 /* Walk the tree DIE and mark types that we actually use. */
27585 static void
27586 prune_unused_types_walk (dw_die_ref die)
27588 dw_die_ref c;
27590 /* Don't do anything if this node is already marked and
27591 children have been marked as well. */
27592 if (die->die_mark == 2)
27593 return;
27595 switch (die->die_tag)
27597 case DW_TAG_structure_type:
27598 case DW_TAG_union_type:
27599 case DW_TAG_class_type:
27600 if (die->die_perennial_p)
27601 break;
27603 for (c = die->die_parent; c; c = c->die_parent)
27604 if (c->die_tag == DW_TAG_subprogram)
27605 break;
27607 /* Finding used static member functions inside of classes
27608 is needed just for local classes, because for other classes
27609 static member function DIEs with DW_AT_specification
27610 are emitted outside of the DW_TAG_*_type. If we ever change
27611 it, we'd need to call this even for non-local classes. */
27612 if (c)
27613 prune_unused_types_walk_local_classes (die);
27615 /* It's a type node --- don't mark it. */
27616 return;
27618 case DW_TAG_const_type:
27619 case DW_TAG_packed_type:
27620 case DW_TAG_pointer_type:
27621 case DW_TAG_reference_type:
27622 case DW_TAG_rvalue_reference_type:
27623 case DW_TAG_volatile_type:
27624 case DW_TAG_typedef:
27625 case DW_TAG_array_type:
27626 case DW_TAG_interface_type:
27627 case DW_TAG_friend:
27628 case DW_TAG_enumeration_type:
27629 case DW_TAG_subroutine_type:
27630 case DW_TAG_string_type:
27631 case DW_TAG_set_type:
27632 case DW_TAG_subrange_type:
27633 case DW_TAG_ptr_to_member_type:
27634 case DW_TAG_file_type:
27635 /* Type nodes are useful only when other DIEs reference them --- don't
27636 mark them. */
27637 /* FALLTHROUGH */
27639 case DW_TAG_dwarf_procedure:
27640 /* Likewise for DWARF procedures. */
27642 if (die->die_perennial_p)
27643 break;
27645 return;
27647 default:
27648 /* Mark everything else. */
27649 break;
27652 if (die->die_mark == 0)
27654 die->die_mark = 1;
27656 /* Now, mark any dies referenced from here. */
27657 prune_unused_types_walk_attribs (die);
27660 die->die_mark = 2;
27662 /* Mark children. */
27663 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27666 /* Increment the string counts on strings referred to from DIE's
27667 attributes. */
27669 static void
27670 prune_unused_types_update_strings (dw_die_ref die)
27672 dw_attr_node *a;
27673 unsigned ix;
27675 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27676 if (AT_class (a) == dw_val_class_str)
27678 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27679 s->refcount++;
27680 /* Avoid unnecessarily putting strings that are used less than
27681 twice in the hash table. */
27682 if (s->refcount
27683 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27685 indirect_string_node **slot
27686 = debug_str_hash->find_slot_with_hash (s->str,
27687 htab_hash_string (s->str),
27688 INSERT);
27689 gcc_assert (*slot == NULL);
27690 *slot = s;
27695 /* Mark DIE and its children as removed. */
27697 static void
27698 mark_removed (dw_die_ref die)
27700 dw_die_ref c;
27701 die->removed = true;
27702 FOR_EACH_CHILD (die, c, mark_removed (c));
27705 /* Remove from the tree DIE any dies that aren't marked. */
27707 static void
27708 prune_unused_types_prune (dw_die_ref die)
27710 dw_die_ref c;
27712 gcc_assert (die->die_mark);
27713 prune_unused_types_update_strings (die);
27715 if (! die->die_child)
27716 return;
27718 c = die->die_child;
27719 do {
27720 dw_die_ref prev = c, next;
27721 for (c = c->die_sib; ! c->die_mark; c = next)
27722 if (c == die->die_child)
27724 /* No marked children between 'prev' and the end of the list. */
27725 if (prev == c)
27726 /* No marked children at all. */
27727 die->die_child = NULL;
27728 else
27730 prev->die_sib = c->die_sib;
27731 die->die_child = prev;
27733 c->die_sib = NULL;
27734 mark_removed (c);
27735 return;
27737 else
27739 next = c->die_sib;
27740 c->die_sib = NULL;
27741 mark_removed (c);
27744 if (c != prev->die_sib)
27745 prev->die_sib = c;
27746 prune_unused_types_prune (c);
27747 } while (c != die->die_child);
27750 /* Remove dies representing declarations that we never use. */
27752 static void
27753 prune_unused_types (void)
27755 unsigned int i;
27756 limbo_die_node *node;
27757 comdat_type_node *ctnode;
27758 pubname_entry *pub;
27759 dw_die_ref base_type;
27761 #if ENABLE_ASSERT_CHECKING
27762 /* All the marks should already be clear. */
27763 verify_marks_clear (comp_unit_die ());
27764 for (node = limbo_die_list; node; node = node->next)
27765 verify_marks_clear (node->die);
27766 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27767 verify_marks_clear (ctnode->root_die);
27768 #endif /* ENABLE_ASSERT_CHECKING */
27770 /* Mark types that are used in global variables. */
27771 premark_types_used_by_global_vars ();
27773 /* Set the mark on nodes that are actually used. */
27774 prune_unused_types_walk (comp_unit_die ());
27775 for (node = limbo_die_list; node; node = node->next)
27776 prune_unused_types_walk (node->die);
27777 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27779 prune_unused_types_walk (ctnode->root_die);
27780 prune_unused_types_mark (ctnode->type_die, 1);
27783 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27784 are unusual in that they are pubnames that are the children of pubtypes.
27785 They should only be marked via their parent DW_TAG_enumeration_type die,
27786 not as roots in themselves. */
27787 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27788 if (pub->die->die_tag != DW_TAG_enumerator)
27789 prune_unused_types_mark (pub->die, 1);
27790 for (i = 0; base_types.iterate (i, &base_type); i++)
27791 prune_unused_types_mark (base_type, 1);
27793 if (debug_str_hash)
27794 debug_str_hash->empty ();
27795 if (skeleton_debug_str_hash)
27796 skeleton_debug_str_hash->empty ();
27797 prune_unused_types_prune (comp_unit_die ());
27798 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27800 node = *pnode;
27801 if (!node->die->die_mark)
27802 *pnode = node->next;
27803 else
27805 prune_unused_types_prune (node->die);
27806 pnode = &node->next;
27809 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27810 prune_unused_types_prune (ctnode->root_die);
27812 /* Leave the marks clear. */
27813 prune_unmark_dies (comp_unit_die ());
27814 for (node = limbo_die_list; node; node = node->next)
27815 prune_unmark_dies (node->die);
27816 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27817 prune_unmark_dies (ctnode->root_die);
27820 /* Helpers to manipulate hash table of comdat type units. */
27822 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27824 static inline hashval_t hash (const comdat_type_node *);
27825 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27828 inline hashval_t
27829 comdat_type_hasher::hash (const comdat_type_node *type_node)
27831 hashval_t h;
27832 memcpy (&h, type_node->signature, sizeof (h));
27833 return h;
27836 inline bool
27837 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27838 const comdat_type_node *type_node_2)
27840 return (! memcmp (type_node_1->signature, type_node_2->signature,
27841 DWARF_TYPE_SIGNATURE_SIZE));
27844 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
27845 to the location it would have been added, should we know its
27846 DECL_ASSEMBLER_NAME when we added other attributes. This will
27847 probably improve compactness of debug info, removing equivalent
27848 abbrevs, and hide any differences caused by deferring the
27849 computation of the assembler name, triggered by e.g. PCH. */
27851 static inline void
27852 move_linkage_attr (dw_die_ref die)
27854 unsigned ix = vec_safe_length (die->die_attr);
27855 dw_attr_node linkage = (*die->die_attr)[ix - 1];
27857 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
27858 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
27860 while (--ix > 0)
27862 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
27864 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
27865 break;
27868 if (ix != vec_safe_length (die->die_attr) - 1)
27870 die->die_attr->pop ();
27871 die->die_attr->quick_insert (ix, linkage);
27875 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
27876 referenced from typed stack ops and count how often they are used. */
27878 static void
27879 mark_base_types (dw_loc_descr_ref loc)
27881 dw_die_ref base_type = NULL;
27883 for (; loc; loc = loc->dw_loc_next)
27885 switch (loc->dw_loc_opc)
27887 case DW_OP_regval_type:
27888 case DW_OP_deref_type:
27889 case DW_OP_GNU_regval_type:
27890 case DW_OP_GNU_deref_type:
27891 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
27892 break;
27893 case DW_OP_convert:
27894 case DW_OP_reinterpret:
27895 case DW_OP_GNU_convert:
27896 case DW_OP_GNU_reinterpret:
27897 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
27898 continue;
27899 /* FALLTHRU */
27900 case DW_OP_const_type:
27901 case DW_OP_GNU_const_type:
27902 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
27903 break;
27904 case DW_OP_entry_value:
27905 case DW_OP_GNU_entry_value:
27906 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
27907 continue;
27908 default:
27909 continue;
27911 gcc_assert (base_type->die_parent == comp_unit_die ());
27912 if (base_type->die_mark)
27913 base_type->die_mark++;
27914 else
27916 base_types.safe_push (base_type);
27917 base_type->die_mark = 1;
27922 /* Comparison function for sorting marked base types. */
27924 static int
27925 base_type_cmp (const void *x, const void *y)
27927 dw_die_ref dx = *(const dw_die_ref *) x;
27928 dw_die_ref dy = *(const dw_die_ref *) y;
27929 unsigned int byte_size1, byte_size2;
27930 unsigned int encoding1, encoding2;
27931 if (dx->die_mark > dy->die_mark)
27932 return -1;
27933 if (dx->die_mark < dy->die_mark)
27934 return 1;
27935 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
27936 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
27937 if (byte_size1 < byte_size2)
27938 return 1;
27939 if (byte_size1 > byte_size2)
27940 return -1;
27941 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
27942 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
27943 if (encoding1 < encoding2)
27944 return 1;
27945 if (encoding1 > encoding2)
27946 return -1;
27947 return 0;
27950 /* Move base types marked by mark_base_types as early as possible
27951 in the CU, sorted by decreasing usage count both to make the
27952 uleb128 references as small as possible and to make sure they
27953 will have die_offset already computed by calc_die_sizes when
27954 sizes of typed stack loc ops is computed. */
27956 static void
27957 move_marked_base_types (void)
27959 unsigned int i;
27960 dw_die_ref base_type, die, c;
27962 if (base_types.is_empty ())
27963 return;
27965 /* Sort by decreasing usage count, they will be added again in that
27966 order later on. */
27967 base_types.qsort (base_type_cmp);
27968 die = comp_unit_die ();
27969 c = die->die_child;
27972 dw_die_ref prev = c;
27973 c = c->die_sib;
27974 while (c->die_mark)
27976 remove_child_with_prev (c, prev);
27977 /* As base types got marked, there must be at least
27978 one node other than DW_TAG_base_type. */
27979 gcc_assert (die->die_child != NULL);
27980 c = prev->die_sib;
27983 while (c != die->die_child);
27984 gcc_assert (die->die_child);
27985 c = die->die_child;
27986 for (i = 0; base_types.iterate (i, &base_type); i++)
27988 base_type->die_mark = 0;
27989 base_type->die_sib = c->die_sib;
27990 c->die_sib = base_type;
27991 c = base_type;
27995 /* Helper function for resolve_addr, attempt to resolve
27996 one CONST_STRING, return true if successful. Similarly verify that
27997 SYMBOL_REFs refer to variables emitted in the current CU. */
27999 static bool
28000 resolve_one_addr (rtx *addr)
28002 rtx rtl = *addr;
28004 if (GET_CODE (rtl) == CONST_STRING)
28006 size_t len = strlen (XSTR (rtl, 0)) + 1;
28007 tree t = build_string (len, XSTR (rtl, 0));
28008 tree tlen = size_int (len - 1);
28009 TREE_TYPE (t)
28010 = build_array_type (char_type_node, build_index_type (tlen));
28011 rtl = lookup_constant_def (t);
28012 if (!rtl || !MEM_P (rtl))
28013 return false;
28014 rtl = XEXP (rtl, 0);
28015 if (GET_CODE (rtl) == SYMBOL_REF
28016 && SYMBOL_REF_DECL (rtl)
28017 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28018 return false;
28019 vec_safe_push (used_rtx_array, rtl);
28020 *addr = rtl;
28021 return true;
28024 if (GET_CODE (rtl) == SYMBOL_REF
28025 && SYMBOL_REF_DECL (rtl))
28027 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28029 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28030 return false;
28032 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28033 return false;
28036 if (GET_CODE (rtl) == CONST)
28038 subrtx_ptr_iterator::array_type array;
28039 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28040 if (!resolve_one_addr (*iter))
28041 return false;
28044 return true;
28047 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28048 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28049 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28051 static rtx
28052 string_cst_pool_decl (tree t)
28054 rtx rtl = output_constant_def (t, 1);
28055 unsigned char *array;
28056 dw_loc_descr_ref l;
28057 tree decl;
28058 size_t len;
28059 dw_die_ref ref;
28061 if (!rtl || !MEM_P (rtl))
28062 return NULL_RTX;
28063 rtl = XEXP (rtl, 0);
28064 if (GET_CODE (rtl) != SYMBOL_REF
28065 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28066 return NULL_RTX;
28068 decl = SYMBOL_REF_DECL (rtl);
28069 if (!lookup_decl_die (decl))
28071 len = TREE_STRING_LENGTH (t);
28072 vec_safe_push (used_rtx_array, rtl);
28073 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28074 array = ggc_vec_alloc<unsigned char> (len);
28075 memcpy (array, TREE_STRING_POINTER (t), len);
28076 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28077 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28078 l->dw_loc_oprnd2.v.val_vec.length = len;
28079 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28080 l->dw_loc_oprnd2.v.val_vec.array = array;
28081 add_AT_loc (ref, DW_AT_location, l);
28082 equate_decl_number_to_die (decl, ref);
28084 return rtl;
28087 /* Helper function of resolve_addr_in_expr. LOC is
28088 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28089 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28090 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28091 with DW_OP_implicit_pointer if possible
28092 and return true, if unsuccessful, return false. */
28094 static bool
28095 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28097 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28098 HOST_WIDE_INT offset = 0;
28099 dw_die_ref ref = NULL;
28100 tree decl;
28102 if (GET_CODE (rtl) == CONST
28103 && GET_CODE (XEXP (rtl, 0)) == PLUS
28104 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28106 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28107 rtl = XEXP (XEXP (rtl, 0), 0);
28109 if (GET_CODE (rtl) == CONST_STRING)
28111 size_t len = strlen (XSTR (rtl, 0)) + 1;
28112 tree t = build_string (len, XSTR (rtl, 0));
28113 tree tlen = size_int (len - 1);
28115 TREE_TYPE (t)
28116 = build_array_type (char_type_node, build_index_type (tlen));
28117 rtl = string_cst_pool_decl (t);
28118 if (!rtl)
28119 return false;
28121 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28123 decl = SYMBOL_REF_DECL (rtl);
28124 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28126 ref = lookup_decl_die (decl);
28127 if (ref && (get_AT (ref, DW_AT_location)
28128 || get_AT (ref, DW_AT_const_value)))
28130 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28131 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28132 loc->dw_loc_oprnd1.val_entry = NULL;
28133 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28134 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28135 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28136 loc->dw_loc_oprnd2.v.val_int = offset;
28137 return true;
28141 return false;
28144 /* Helper function for resolve_addr, handle one location
28145 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28146 the location list couldn't be resolved. */
28148 static bool
28149 resolve_addr_in_expr (dw_loc_descr_ref loc)
28151 dw_loc_descr_ref keep = NULL;
28152 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28153 switch (loc->dw_loc_opc)
28155 case DW_OP_addr:
28156 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28158 if ((prev == NULL
28159 || prev->dw_loc_opc == DW_OP_piece
28160 || prev->dw_loc_opc == DW_OP_bit_piece)
28161 && loc->dw_loc_next
28162 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28163 && (!dwarf_strict || dwarf_version >= 5)
28164 && optimize_one_addr_into_implicit_ptr (loc))
28165 break;
28166 return false;
28168 break;
28169 case DW_OP_GNU_addr_index:
28170 case DW_OP_GNU_const_index:
28171 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28172 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28174 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28175 if (!resolve_one_addr (&rtl))
28176 return false;
28177 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28178 loc->dw_loc_oprnd1.val_entry
28179 = add_addr_table_entry (rtl, ate_kind_rtx);
28181 break;
28182 case DW_OP_const4u:
28183 case DW_OP_const8u:
28184 if (loc->dtprel
28185 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28186 return false;
28187 break;
28188 case DW_OP_plus_uconst:
28189 if (size_of_loc_descr (loc)
28190 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28192 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28194 dw_loc_descr_ref repl
28195 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28196 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28197 add_loc_descr (&repl, loc->dw_loc_next);
28198 *loc = *repl;
28200 break;
28201 case DW_OP_implicit_value:
28202 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28203 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28204 return false;
28205 break;
28206 case DW_OP_implicit_pointer:
28207 case DW_OP_GNU_implicit_pointer:
28208 case DW_OP_GNU_parameter_ref:
28209 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28211 dw_die_ref ref
28212 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28213 if (ref == NULL)
28214 return false;
28215 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28216 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28217 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28219 break;
28220 case DW_OP_const_type:
28221 case DW_OP_regval_type:
28222 case DW_OP_deref_type:
28223 case DW_OP_convert:
28224 case DW_OP_reinterpret:
28225 case DW_OP_GNU_const_type:
28226 case DW_OP_GNU_regval_type:
28227 case DW_OP_GNU_deref_type:
28228 case DW_OP_GNU_convert:
28229 case DW_OP_GNU_reinterpret:
28230 while (loc->dw_loc_next
28231 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28232 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28234 dw_die_ref base1, base2;
28235 unsigned enc1, enc2, size1, size2;
28236 if (loc->dw_loc_opc == DW_OP_regval_type
28237 || loc->dw_loc_opc == DW_OP_deref_type
28238 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28239 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28240 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28241 else if (loc->dw_loc_oprnd1.val_class
28242 == dw_val_class_unsigned_const)
28243 break;
28244 else
28245 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28246 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28247 == dw_val_class_unsigned_const)
28248 break;
28249 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28250 gcc_assert (base1->die_tag == DW_TAG_base_type
28251 && base2->die_tag == DW_TAG_base_type);
28252 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28253 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28254 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28255 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28256 if (size1 == size2
28257 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28258 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28259 && loc != keep)
28260 || enc1 == enc2))
28262 /* Optimize away next DW_OP_convert after
28263 adjusting LOC's base type die reference. */
28264 if (loc->dw_loc_opc == DW_OP_regval_type
28265 || loc->dw_loc_opc == DW_OP_deref_type
28266 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28267 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28268 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28269 else
28270 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28271 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28272 continue;
28274 /* Don't change integer DW_OP_convert after e.g. floating
28275 point typed stack entry. */
28276 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28277 keep = loc->dw_loc_next;
28278 break;
28280 break;
28281 default:
28282 break;
28284 return true;
28287 /* Helper function of resolve_addr. DIE had DW_AT_location of
28288 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28289 and DW_OP_addr couldn't be resolved. resolve_addr has already
28290 removed the DW_AT_location attribute. This function attempts to
28291 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28292 to it or DW_AT_const_value attribute, if possible. */
28294 static void
28295 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28297 if (!VAR_P (decl)
28298 || lookup_decl_die (decl) != die
28299 || DECL_EXTERNAL (decl)
28300 || !TREE_STATIC (decl)
28301 || DECL_INITIAL (decl) == NULL_TREE
28302 || DECL_P (DECL_INITIAL (decl))
28303 || get_AT (die, DW_AT_const_value))
28304 return;
28306 tree init = DECL_INITIAL (decl);
28307 HOST_WIDE_INT offset = 0;
28308 /* For variables that have been optimized away and thus
28309 don't have a memory location, see if we can emit
28310 DW_AT_const_value instead. */
28311 if (tree_add_const_value_attribute (die, init))
28312 return;
28313 if (dwarf_strict && dwarf_version < 5)
28314 return;
28315 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28316 and ADDR_EXPR refers to a decl that has DW_AT_location or
28317 DW_AT_const_value (but isn't addressable, otherwise
28318 resolving the original DW_OP_addr wouldn't fail), see if
28319 we can add DW_OP_implicit_pointer. */
28320 STRIP_NOPS (init);
28321 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28322 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28324 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28325 init = TREE_OPERAND (init, 0);
28326 STRIP_NOPS (init);
28328 if (TREE_CODE (init) != ADDR_EXPR)
28329 return;
28330 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28331 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28332 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28333 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28334 && TREE_OPERAND (init, 0) != decl))
28336 dw_die_ref ref;
28337 dw_loc_descr_ref l;
28339 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28341 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28342 if (!rtl)
28343 return;
28344 decl = SYMBOL_REF_DECL (rtl);
28346 else
28347 decl = TREE_OPERAND (init, 0);
28348 ref = lookup_decl_die (decl);
28349 if (ref == NULL
28350 || (!get_AT (ref, DW_AT_location)
28351 && !get_AT (ref, DW_AT_const_value)))
28352 return;
28353 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28354 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28355 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28356 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28357 add_AT_loc (die, DW_AT_location, l);
28361 /* Return NULL if l is a DWARF expression, or first op that is not
28362 valid DWARF expression. */
28364 static dw_loc_descr_ref
28365 non_dwarf_expression (dw_loc_descr_ref l)
28367 while (l)
28369 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28370 return l;
28371 switch (l->dw_loc_opc)
28373 case DW_OP_regx:
28374 case DW_OP_implicit_value:
28375 case DW_OP_stack_value:
28376 case DW_OP_implicit_pointer:
28377 case DW_OP_GNU_implicit_pointer:
28378 case DW_OP_GNU_parameter_ref:
28379 case DW_OP_piece:
28380 case DW_OP_bit_piece:
28381 return l;
28382 default:
28383 break;
28385 l = l->dw_loc_next;
28387 return NULL;
28390 /* Return adjusted copy of EXPR:
28391 If it is empty DWARF expression, return it.
28392 If it is valid non-empty DWARF expression,
28393 return copy of EXPR with copy of DEREF appended to it.
28394 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28395 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
28396 and no DEREF.
28397 If it is DWARF expression followed by DW_OP_stack_value, return
28398 copy of the DWARF expression without anything appended.
28399 Otherwise, return NULL. */
28401 static dw_loc_descr_ref
28402 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
28405 if (expr == NULL)
28406 return NULL;
28408 dw_loc_descr_ref l = non_dwarf_expression (expr);
28409 if (l && l->dw_loc_next)
28410 return NULL;
28412 if (l)
28414 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28415 deref = new_loc_descr ((enum dwarf_location_atom)
28416 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28417 0, 0);
28418 else
28419 switch (l->dw_loc_opc)
28421 case DW_OP_regx:
28422 deref = new_loc_descr (DW_OP_bregx,
28423 l->dw_loc_oprnd1.v.val_unsigned, 0);
28424 break;
28425 case DW_OP_stack_value:
28426 deref = NULL;
28427 break;
28428 default:
28429 return NULL;
28432 else
28433 deref = new_loc_descr (deref->dw_loc_opc,
28434 deref->dw_loc_oprnd1.v.val_int, 0);
28436 dw_loc_descr_ref ret = NULL, *p = &ret;
28437 while (expr != l)
28439 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28440 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28441 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28442 p = &(*p)->dw_loc_next;
28443 expr = expr->dw_loc_next;
28445 *p = deref;
28446 return ret;
28449 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
28450 or argument, adjust it if needed and return:
28451 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28452 attribute if present should be removed
28453 0 keep the attribute as is if the referenced var or argument has
28454 only DWARF expression that covers all ranges
28455 1 if the attribute has been successfully adjusted. */
28457 static int
28458 optimize_string_length (dw_attr_node *a)
28460 dw_loc_descr_ref l = AT_loc (a), lv;
28461 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
28462 dw_attr_node *av = get_AT (die, DW_AT_location);
28463 dw_loc_list_ref d;
28464 bool non_dwarf_expr = false;
28466 if (av == NULL)
28467 return -1;
28468 switch (AT_class (av))
28470 case dw_val_class_loc_list:
28471 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28472 if (d->expr && non_dwarf_expression (d->expr))
28473 non_dwarf_expr = true;
28474 break;
28475 case dw_val_class_loc:
28476 lv = AT_loc (av);
28477 if (lv == NULL)
28478 return -1;
28479 if (non_dwarf_expression (lv))
28480 non_dwarf_expr = true;
28481 break;
28482 default:
28483 return -1;
28486 /* If it is safe to keep DW_OP_call4 in, keep it. */
28487 if (!non_dwarf_expr
28488 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
28489 return 0;
28491 /* If not dereferencing the DW_OP_call4 afterwards, we can just
28492 copy over the DW_AT_location attribute from die to a. */
28493 if (l->dw_loc_next == NULL)
28495 a->dw_attr_val = av->dw_attr_val;
28496 return 1;
28499 dw_loc_list_ref list, *p;
28500 switch (AT_class (av))
28502 case dw_val_class_loc_list:
28503 p = &list;
28504 list = NULL;
28505 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28507 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
28508 if (lv)
28510 *p = new_loc_list (lv, d->begin, d->end, d->section);
28511 p = &(*p)->dw_loc_next;
28514 if (list == NULL)
28515 return -1;
28516 a->dw_attr_val.val_class = dw_val_class_loc_list;
28517 gen_llsym (list);
28518 *AT_loc_list_ptr (a) = list;
28519 return 1;
28520 case dw_val_class_loc:
28521 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
28522 if (lv == NULL)
28523 return -1;
28524 a->dw_attr_val.v.val_loc = lv;
28525 return 1;
28526 default:
28527 gcc_unreachable ();
28531 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28532 an address in .rodata section if the string literal is emitted there,
28533 or remove the containing location list or replace DW_AT_const_value
28534 with DW_AT_location and empty location expression, if it isn't found
28535 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28536 to something that has been emitted in the current CU. */
28538 static void
28539 resolve_addr (dw_die_ref die)
28541 dw_die_ref c;
28542 dw_attr_node *a;
28543 dw_loc_list_ref *curr, *start, loc;
28544 unsigned ix;
28545 bool remove_AT_byte_size = false;
28547 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28548 switch (AT_class (a))
28550 case dw_val_class_loc_list:
28551 start = curr = AT_loc_list_ptr (a);
28552 loc = *curr;
28553 gcc_assert (loc);
28554 /* The same list can be referenced more than once. See if we have
28555 already recorded the result from a previous pass. */
28556 if (loc->replaced)
28557 *curr = loc->dw_loc_next;
28558 else if (!loc->resolved_addr)
28560 /* As things stand, we do not expect or allow one die to
28561 reference a suffix of another die's location list chain.
28562 References must be identical or completely separate.
28563 There is therefore no need to cache the result of this
28564 pass on any list other than the first; doing so
28565 would lead to unnecessary writes. */
28566 while (*curr)
28568 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28569 if (!resolve_addr_in_expr ((*curr)->expr))
28571 dw_loc_list_ref next = (*curr)->dw_loc_next;
28572 dw_loc_descr_ref l = (*curr)->expr;
28574 if (next && (*curr)->ll_symbol)
28576 gcc_assert (!next->ll_symbol);
28577 next->ll_symbol = (*curr)->ll_symbol;
28579 if (dwarf_split_debug_info)
28580 remove_loc_list_addr_table_entries (l);
28581 *curr = next;
28583 else
28585 mark_base_types ((*curr)->expr);
28586 curr = &(*curr)->dw_loc_next;
28589 if (loc == *start)
28590 loc->resolved_addr = 1;
28591 else
28593 loc->replaced = 1;
28594 loc->dw_loc_next = *start;
28597 if (!*start)
28599 remove_AT (die, a->dw_attr);
28600 ix--;
28602 break;
28603 case dw_val_class_loc:
28605 dw_loc_descr_ref l = AT_loc (a);
28606 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
28607 DW_AT_string_length is only a rough approximation; unfortunately
28608 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
28609 needs a DWARF expression, while DW_AT_location of the referenced
28610 variable or argument might be any location description. */
28611 if (a->dw_attr == DW_AT_string_length
28612 && l
28613 && l->dw_loc_opc == DW_OP_call4
28614 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
28615 && (l->dw_loc_next == NULL
28616 || (l->dw_loc_next->dw_loc_next == NULL
28617 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
28618 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
28620 switch (optimize_string_length (a))
28622 case -1:
28623 remove_AT (die, a->dw_attr);
28624 ix--;
28625 /* If we drop DW_AT_string_length, we need to drop also
28626 DW_AT_{string_length_,}byte_size. */
28627 remove_AT_byte_size = true;
28628 continue;
28629 default:
28630 break;
28631 case 1:
28632 /* Even if we keep the optimized DW_AT_string_length,
28633 it might have changed AT_class, so process it again. */
28634 ix--;
28635 continue;
28638 /* For -gdwarf-2 don't attempt to optimize
28639 DW_AT_data_member_location containing
28640 DW_OP_plus_uconst - older consumers might
28641 rely on it being that op instead of a more complex,
28642 but shorter, location description. */
28643 if ((dwarf_version > 2
28644 || a->dw_attr != DW_AT_data_member_location
28645 || l == NULL
28646 || l->dw_loc_opc != DW_OP_plus_uconst
28647 || l->dw_loc_next != NULL)
28648 && !resolve_addr_in_expr (l))
28650 if (dwarf_split_debug_info)
28651 remove_loc_list_addr_table_entries (l);
28652 if (l != NULL
28653 && l->dw_loc_next == NULL
28654 && l->dw_loc_opc == DW_OP_addr
28655 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28656 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28657 && a->dw_attr == DW_AT_location)
28659 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28660 remove_AT (die, a->dw_attr);
28661 ix--;
28662 optimize_location_into_implicit_ptr (die, decl);
28663 break;
28665 remove_AT (die, a->dw_attr);
28666 ix--;
28668 else
28669 mark_base_types (l);
28671 break;
28672 case dw_val_class_addr:
28673 if (a->dw_attr == DW_AT_const_value
28674 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28676 if (AT_index (a) != NOT_INDEXED)
28677 remove_addr_table_entry (a->dw_attr_val.val_entry);
28678 remove_AT (die, a->dw_attr);
28679 ix--;
28681 if ((die->die_tag == DW_TAG_call_site
28682 && a->dw_attr == DW_AT_call_origin)
28683 || (die->die_tag == DW_TAG_GNU_call_site
28684 && a->dw_attr == DW_AT_abstract_origin))
28686 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28687 dw_die_ref tdie = lookup_decl_die (tdecl);
28688 dw_die_ref cdie;
28689 if (tdie == NULL
28690 && DECL_EXTERNAL (tdecl)
28691 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28692 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28694 /* Creating a full DIE for tdecl is overly expensive and
28695 at this point even wrong when in the LTO phase
28696 as it can end up generating new type DIEs we didn't
28697 output and thus optimize_external_refs will crash. */
28698 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28699 add_AT_flag (tdie, DW_AT_external, 1);
28700 add_AT_flag (tdie, DW_AT_declaration, 1);
28701 add_linkage_attr (tdie, tdecl);
28702 add_name_and_src_coords_attributes (tdie, tdecl);
28703 equate_decl_number_to_die (tdecl, tdie);
28705 if (tdie)
28707 a->dw_attr_val.val_class = dw_val_class_die_ref;
28708 a->dw_attr_val.v.val_die_ref.die = tdie;
28709 a->dw_attr_val.v.val_die_ref.external = 0;
28711 else
28713 if (AT_index (a) != NOT_INDEXED)
28714 remove_addr_table_entry (a->dw_attr_val.val_entry);
28715 remove_AT (die, a->dw_attr);
28716 ix--;
28719 break;
28720 default:
28721 break;
28724 if (remove_AT_byte_size)
28725 remove_AT (die, dwarf_version >= 5
28726 ? DW_AT_string_length_byte_size
28727 : DW_AT_byte_size);
28729 FOR_EACH_CHILD (die, c, resolve_addr (c));
28732 /* Helper routines for optimize_location_lists.
28733 This pass tries to share identical local lists in .debug_loc
28734 section. */
28736 /* Iteratively hash operands of LOC opcode into HSTATE. */
28738 static void
28739 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28741 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28742 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28744 switch (loc->dw_loc_opc)
28746 case DW_OP_const4u:
28747 case DW_OP_const8u:
28748 if (loc->dtprel)
28749 goto hash_addr;
28750 /* FALLTHRU */
28751 case DW_OP_const1u:
28752 case DW_OP_const1s:
28753 case DW_OP_const2u:
28754 case DW_OP_const2s:
28755 case DW_OP_const4s:
28756 case DW_OP_const8s:
28757 case DW_OP_constu:
28758 case DW_OP_consts:
28759 case DW_OP_pick:
28760 case DW_OP_plus_uconst:
28761 case DW_OP_breg0:
28762 case DW_OP_breg1:
28763 case DW_OP_breg2:
28764 case DW_OP_breg3:
28765 case DW_OP_breg4:
28766 case DW_OP_breg5:
28767 case DW_OP_breg6:
28768 case DW_OP_breg7:
28769 case DW_OP_breg8:
28770 case DW_OP_breg9:
28771 case DW_OP_breg10:
28772 case DW_OP_breg11:
28773 case DW_OP_breg12:
28774 case DW_OP_breg13:
28775 case DW_OP_breg14:
28776 case DW_OP_breg15:
28777 case DW_OP_breg16:
28778 case DW_OP_breg17:
28779 case DW_OP_breg18:
28780 case DW_OP_breg19:
28781 case DW_OP_breg20:
28782 case DW_OP_breg21:
28783 case DW_OP_breg22:
28784 case DW_OP_breg23:
28785 case DW_OP_breg24:
28786 case DW_OP_breg25:
28787 case DW_OP_breg26:
28788 case DW_OP_breg27:
28789 case DW_OP_breg28:
28790 case DW_OP_breg29:
28791 case DW_OP_breg30:
28792 case DW_OP_breg31:
28793 case DW_OP_regx:
28794 case DW_OP_fbreg:
28795 case DW_OP_piece:
28796 case DW_OP_deref_size:
28797 case DW_OP_xderef_size:
28798 hstate.add_object (val1->v.val_int);
28799 break;
28800 case DW_OP_skip:
28801 case DW_OP_bra:
28803 int offset;
28805 gcc_assert (val1->val_class == dw_val_class_loc);
28806 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
28807 hstate.add_object (offset);
28809 break;
28810 case DW_OP_implicit_value:
28811 hstate.add_object (val1->v.val_unsigned);
28812 switch (val2->val_class)
28814 case dw_val_class_const:
28815 hstate.add_object (val2->v.val_int);
28816 break;
28817 case dw_val_class_vec:
28819 unsigned int elt_size = val2->v.val_vec.elt_size;
28820 unsigned int len = val2->v.val_vec.length;
28822 hstate.add_int (elt_size);
28823 hstate.add_int (len);
28824 hstate.add (val2->v.val_vec.array, len * elt_size);
28826 break;
28827 case dw_val_class_const_double:
28828 hstate.add_object (val2->v.val_double.low);
28829 hstate.add_object (val2->v.val_double.high);
28830 break;
28831 case dw_val_class_wide_int:
28832 hstate.add (val2->v.val_wide->get_val (),
28833 get_full_len (*val2->v.val_wide)
28834 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28835 break;
28836 case dw_val_class_addr:
28837 inchash::add_rtx (val2->v.val_addr, hstate);
28838 break;
28839 default:
28840 gcc_unreachable ();
28842 break;
28843 case DW_OP_bregx:
28844 case DW_OP_bit_piece:
28845 hstate.add_object (val1->v.val_int);
28846 hstate.add_object (val2->v.val_int);
28847 break;
28848 case DW_OP_addr:
28849 hash_addr:
28850 if (loc->dtprel)
28852 unsigned char dtprel = 0xd1;
28853 hstate.add_object (dtprel);
28855 inchash::add_rtx (val1->v.val_addr, hstate);
28856 break;
28857 case DW_OP_GNU_addr_index:
28858 case DW_OP_GNU_const_index:
28860 if (loc->dtprel)
28862 unsigned char dtprel = 0xd1;
28863 hstate.add_object (dtprel);
28865 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
28867 break;
28868 case DW_OP_implicit_pointer:
28869 case DW_OP_GNU_implicit_pointer:
28870 hstate.add_int (val2->v.val_int);
28871 break;
28872 case DW_OP_entry_value:
28873 case DW_OP_GNU_entry_value:
28874 hstate.add_object (val1->v.val_loc);
28875 break;
28876 case DW_OP_regval_type:
28877 case DW_OP_deref_type:
28878 case DW_OP_GNU_regval_type:
28879 case DW_OP_GNU_deref_type:
28881 unsigned int byte_size
28882 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
28883 unsigned int encoding
28884 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
28885 hstate.add_object (val1->v.val_int);
28886 hstate.add_object (byte_size);
28887 hstate.add_object (encoding);
28889 break;
28890 case DW_OP_convert:
28891 case DW_OP_reinterpret:
28892 case DW_OP_GNU_convert:
28893 case DW_OP_GNU_reinterpret:
28894 if (val1->val_class == dw_val_class_unsigned_const)
28896 hstate.add_object (val1->v.val_unsigned);
28897 break;
28899 /* FALLTHRU */
28900 case DW_OP_const_type:
28901 case DW_OP_GNU_const_type:
28903 unsigned int byte_size
28904 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
28905 unsigned int encoding
28906 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
28907 hstate.add_object (byte_size);
28908 hstate.add_object (encoding);
28909 if (loc->dw_loc_opc != DW_OP_const_type
28910 && loc->dw_loc_opc != DW_OP_GNU_const_type)
28911 break;
28912 hstate.add_object (val2->val_class);
28913 switch (val2->val_class)
28915 case dw_val_class_const:
28916 hstate.add_object (val2->v.val_int);
28917 break;
28918 case dw_val_class_vec:
28920 unsigned int elt_size = val2->v.val_vec.elt_size;
28921 unsigned int len = val2->v.val_vec.length;
28923 hstate.add_object (elt_size);
28924 hstate.add_object (len);
28925 hstate.add (val2->v.val_vec.array, len * elt_size);
28927 break;
28928 case dw_val_class_const_double:
28929 hstate.add_object (val2->v.val_double.low);
28930 hstate.add_object (val2->v.val_double.high);
28931 break;
28932 case dw_val_class_wide_int:
28933 hstate.add (val2->v.val_wide->get_val (),
28934 get_full_len (*val2->v.val_wide)
28935 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28936 break;
28937 default:
28938 gcc_unreachable ();
28941 break;
28943 default:
28944 /* Other codes have no operands. */
28945 break;
28949 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
28951 static inline void
28952 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
28954 dw_loc_descr_ref l;
28955 bool sizes_computed = false;
28956 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
28957 size_of_locs (loc);
28959 for (l = loc; l != NULL; l = l->dw_loc_next)
28961 enum dwarf_location_atom opc = l->dw_loc_opc;
28962 hstate.add_object (opc);
28963 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
28965 size_of_locs (loc);
28966 sizes_computed = true;
28968 hash_loc_operands (l, hstate);
28972 /* Compute hash of the whole location list LIST_HEAD. */
28974 static inline void
28975 hash_loc_list (dw_loc_list_ref list_head)
28977 dw_loc_list_ref curr = list_head;
28978 inchash::hash hstate;
28980 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
28982 hstate.add (curr->begin, strlen (curr->begin) + 1);
28983 hstate.add (curr->end, strlen (curr->end) + 1);
28984 if (curr->section)
28985 hstate.add (curr->section, strlen (curr->section) + 1);
28986 hash_locs (curr->expr, hstate);
28988 list_head->hash = hstate.end ();
28991 /* Return true if X and Y opcodes have the same operands. */
28993 static inline bool
28994 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
28996 dw_val_ref valx1 = &x->dw_loc_oprnd1;
28997 dw_val_ref valx2 = &x->dw_loc_oprnd2;
28998 dw_val_ref valy1 = &y->dw_loc_oprnd1;
28999 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29001 switch (x->dw_loc_opc)
29003 case DW_OP_const4u:
29004 case DW_OP_const8u:
29005 if (x->dtprel)
29006 goto hash_addr;
29007 /* FALLTHRU */
29008 case DW_OP_const1u:
29009 case DW_OP_const1s:
29010 case DW_OP_const2u:
29011 case DW_OP_const2s:
29012 case DW_OP_const4s:
29013 case DW_OP_const8s:
29014 case DW_OP_constu:
29015 case DW_OP_consts:
29016 case DW_OP_pick:
29017 case DW_OP_plus_uconst:
29018 case DW_OP_breg0:
29019 case DW_OP_breg1:
29020 case DW_OP_breg2:
29021 case DW_OP_breg3:
29022 case DW_OP_breg4:
29023 case DW_OP_breg5:
29024 case DW_OP_breg6:
29025 case DW_OP_breg7:
29026 case DW_OP_breg8:
29027 case DW_OP_breg9:
29028 case DW_OP_breg10:
29029 case DW_OP_breg11:
29030 case DW_OP_breg12:
29031 case DW_OP_breg13:
29032 case DW_OP_breg14:
29033 case DW_OP_breg15:
29034 case DW_OP_breg16:
29035 case DW_OP_breg17:
29036 case DW_OP_breg18:
29037 case DW_OP_breg19:
29038 case DW_OP_breg20:
29039 case DW_OP_breg21:
29040 case DW_OP_breg22:
29041 case DW_OP_breg23:
29042 case DW_OP_breg24:
29043 case DW_OP_breg25:
29044 case DW_OP_breg26:
29045 case DW_OP_breg27:
29046 case DW_OP_breg28:
29047 case DW_OP_breg29:
29048 case DW_OP_breg30:
29049 case DW_OP_breg31:
29050 case DW_OP_regx:
29051 case DW_OP_fbreg:
29052 case DW_OP_piece:
29053 case DW_OP_deref_size:
29054 case DW_OP_xderef_size:
29055 return valx1->v.val_int == valy1->v.val_int;
29056 case DW_OP_skip:
29057 case DW_OP_bra:
29058 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29059 can cause irrelevant differences in dw_loc_addr. */
29060 gcc_assert (valx1->val_class == dw_val_class_loc
29061 && valy1->val_class == dw_val_class_loc
29062 && (dwarf_split_debug_info
29063 || x->dw_loc_addr == y->dw_loc_addr));
29064 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29065 case DW_OP_implicit_value:
29066 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29067 || valx2->val_class != valy2->val_class)
29068 return false;
29069 switch (valx2->val_class)
29071 case dw_val_class_const:
29072 return valx2->v.val_int == valy2->v.val_int;
29073 case dw_val_class_vec:
29074 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29075 && valx2->v.val_vec.length == valy2->v.val_vec.length
29076 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29077 valx2->v.val_vec.elt_size
29078 * valx2->v.val_vec.length) == 0;
29079 case dw_val_class_const_double:
29080 return valx2->v.val_double.low == valy2->v.val_double.low
29081 && valx2->v.val_double.high == valy2->v.val_double.high;
29082 case dw_val_class_wide_int:
29083 return *valx2->v.val_wide == *valy2->v.val_wide;
29084 case dw_val_class_addr:
29085 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29086 default:
29087 gcc_unreachable ();
29089 case DW_OP_bregx:
29090 case DW_OP_bit_piece:
29091 return valx1->v.val_int == valy1->v.val_int
29092 && valx2->v.val_int == valy2->v.val_int;
29093 case DW_OP_addr:
29094 hash_addr:
29095 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29096 case DW_OP_GNU_addr_index:
29097 case DW_OP_GNU_const_index:
29099 rtx ax1 = valx1->val_entry->addr.rtl;
29100 rtx ay1 = valy1->val_entry->addr.rtl;
29101 return rtx_equal_p (ax1, ay1);
29103 case DW_OP_implicit_pointer:
29104 case DW_OP_GNU_implicit_pointer:
29105 return valx1->val_class == dw_val_class_die_ref
29106 && valx1->val_class == valy1->val_class
29107 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29108 && valx2->v.val_int == valy2->v.val_int;
29109 case DW_OP_entry_value:
29110 case DW_OP_GNU_entry_value:
29111 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29112 case DW_OP_const_type:
29113 case DW_OP_GNU_const_type:
29114 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29115 || valx2->val_class != valy2->val_class)
29116 return false;
29117 switch (valx2->val_class)
29119 case dw_val_class_const:
29120 return valx2->v.val_int == valy2->v.val_int;
29121 case dw_val_class_vec:
29122 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29123 && valx2->v.val_vec.length == valy2->v.val_vec.length
29124 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29125 valx2->v.val_vec.elt_size
29126 * valx2->v.val_vec.length) == 0;
29127 case dw_val_class_const_double:
29128 return valx2->v.val_double.low == valy2->v.val_double.low
29129 && valx2->v.val_double.high == valy2->v.val_double.high;
29130 case dw_val_class_wide_int:
29131 return *valx2->v.val_wide == *valy2->v.val_wide;
29132 default:
29133 gcc_unreachable ();
29135 case DW_OP_regval_type:
29136 case DW_OP_deref_type:
29137 case DW_OP_GNU_regval_type:
29138 case DW_OP_GNU_deref_type:
29139 return valx1->v.val_int == valy1->v.val_int
29140 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29141 case DW_OP_convert:
29142 case DW_OP_reinterpret:
29143 case DW_OP_GNU_convert:
29144 case DW_OP_GNU_reinterpret:
29145 if (valx1->val_class != valy1->val_class)
29146 return false;
29147 if (valx1->val_class == dw_val_class_unsigned_const)
29148 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29149 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29150 case DW_OP_GNU_parameter_ref:
29151 return valx1->val_class == dw_val_class_die_ref
29152 && valx1->val_class == valy1->val_class
29153 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29154 default:
29155 /* Other codes have no operands. */
29156 return true;
29160 /* Return true if DWARF location expressions X and Y are the same. */
29162 static inline bool
29163 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29165 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29166 if (x->dw_loc_opc != y->dw_loc_opc
29167 || x->dtprel != y->dtprel
29168 || !compare_loc_operands (x, y))
29169 break;
29170 return x == NULL && y == NULL;
29173 /* Hashtable helpers. */
29175 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29177 static inline hashval_t hash (const dw_loc_list_struct *);
29178 static inline bool equal (const dw_loc_list_struct *,
29179 const dw_loc_list_struct *);
29182 /* Return precomputed hash of location list X. */
29184 inline hashval_t
29185 loc_list_hasher::hash (const dw_loc_list_struct *x)
29187 return x->hash;
29190 /* Return true if location lists A and B are the same. */
29192 inline bool
29193 loc_list_hasher::equal (const dw_loc_list_struct *a,
29194 const dw_loc_list_struct *b)
29196 if (a == b)
29197 return 1;
29198 if (a->hash != b->hash)
29199 return 0;
29200 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29201 if (strcmp (a->begin, b->begin) != 0
29202 || strcmp (a->end, b->end) != 0
29203 || (a->section == NULL) != (b->section == NULL)
29204 || (a->section && strcmp (a->section, b->section) != 0)
29205 || !compare_locs (a->expr, b->expr))
29206 break;
29207 return a == NULL && b == NULL;
29210 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29213 /* Recursively optimize location lists referenced from DIE
29214 children and share them whenever possible. */
29216 static void
29217 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29219 dw_die_ref c;
29220 dw_attr_node *a;
29221 unsigned ix;
29222 dw_loc_list_struct **slot;
29224 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29225 if (AT_class (a) == dw_val_class_loc_list)
29227 dw_loc_list_ref list = AT_loc_list (a);
29228 /* TODO: perform some optimizations here, before hashing
29229 it and storing into the hash table. */
29230 hash_loc_list (list);
29231 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29232 if (*slot == NULL)
29233 *slot = list;
29234 else
29235 a->dw_attr_val.v.val_loc_list = *slot;
29238 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29242 /* Recursively assign each location list a unique index into the debug_addr
29243 section. */
29245 static void
29246 index_location_lists (dw_die_ref die)
29248 dw_die_ref c;
29249 dw_attr_node *a;
29250 unsigned ix;
29252 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29253 if (AT_class (a) == dw_val_class_loc_list)
29255 dw_loc_list_ref list = AT_loc_list (a);
29256 dw_loc_list_ref curr;
29257 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29259 /* Don't index an entry that has already been indexed
29260 or won't be output. */
29261 if (curr->begin_entry != NULL
29262 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29263 continue;
29265 curr->begin_entry
29266 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29270 FOR_EACH_CHILD (die, c, index_location_lists (c));
29273 /* Optimize location lists referenced from DIE
29274 children and share them whenever possible. */
29276 static void
29277 optimize_location_lists (dw_die_ref die)
29279 loc_list_hash_type htab (500);
29280 optimize_location_lists_1 (die, &htab);
29283 /* Traverse the limbo die list, and add parent/child links. The only
29284 dies without parents that should be here are concrete instances of
29285 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29286 For concrete instances, we can get the parent die from the abstract
29287 instance. */
29289 static void
29290 flush_limbo_die_list (void)
29292 limbo_die_node *node;
29294 /* get_context_die calls force_decl_die, which can put new DIEs on the
29295 limbo list in LTO mode when nested functions are put in a different
29296 partition than that of their parent function. */
29297 while ((node = limbo_die_list))
29299 dw_die_ref die = node->die;
29300 limbo_die_list = node->next;
29302 if (die->die_parent == NULL)
29304 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29306 if (origin && origin->die_parent)
29307 add_child_die (origin->die_parent, die);
29308 else if (is_cu_die (die))
29310 else if (seen_error ())
29311 /* It's OK to be confused by errors in the input. */
29312 add_child_die (comp_unit_die (), die);
29313 else
29315 /* In certain situations, the lexical block containing a
29316 nested function can be optimized away, which results
29317 in the nested function die being orphaned. Likewise
29318 with the return type of that nested function. Force
29319 this to be a child of the containing function.
29321 It may happen that even the containing function got fully
29322 inlined and optimized out. In that case we are lost and
29323 assign the empty child. This should not be big issue as
29324 the function is likely unreachable too. */
29325 gcc_assert (node->created_for);
29327 if (DECL_P (node->created_for))
29328 origin = get_context_die (DECL_CONTEXT (node->created_for));
29329 else if (TYPE_P (node->created_for))
29330 origin = scope_die_for (node->created_for, comp_unit_die ());
29331 else
29332 origin = comp_unit_die ();
29334 add_child_die (origin, die);
29340 /* Output stuff that dwarf requires at the end of every file,
29341 and generate the DWARF-2 debugging info. */
29343 static void
29344 dwarf2out_finish (const char *)
29346 comdat_type_node *ctnode;
29347 dw_die_ref main_comp_unit_die;
29348 unsigned char checksum[16];
29350 /* Flush out any latecomers to the limbo party. */
29351 flush_limbo_die_list ();
29353 if (flag_checking)
29355 verify_die (comp_unit_die ());
29356 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29357 verify_die (node->die);
29360 /* We shouldn't have any symbols with delayed asm names for
29361 DIEs generated after early finish. */
29362 gcc_assert (deferred_asm_name == NULL);
29364 gen_remaining_tmpl_value_param_die_attribute ();
29366 #if ENABLE_ASSERT_CHECKING
29368 dw_die_ref die = comp_unit_die (), c;
29369 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29371 #endif
29372 resolve_addr (comp_unit_die ());
29373 move_marked_base_types ();
29375 /* Initialize sections and labels used for actual assembler output. */
29376 init_sections_and_labels ();
29378 /* Traverse the DIE's and add sibling attributes to those DIE's that
29379 have children. */
29380 add_sibling_attributes (comp_unit_die ());
29381 limbo_die_node *node;
29382 for (node = cu_die_list; node; node = node->next)
29383 add_sibling_attributes (node->die);
29384 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29385 add_sibling_attributes (ctnode->root_die);
29387 /* When splitting DWARF info, we put some attributes in the
29388 skeleton compile_unit DIE that remains in the .o, while
29389 most attributes go in the DWO compile_unit_die. */
29390 if (dwarf_split_debug_info)
29392 limbo_die_node *cu;
29393 main_comp_unit_die = gen_compile_unit_die (NULL);
29394 if (dwarf_version >= 5)
29395 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29396 cu = limbo_die_list;
29397 gcc_assert (cu->die == main_comp_unit_die);
29398 limbo_die_list = limbo_die_list->next;
29399 cu->next = cu_die_list;
29400 cu_die_list = cu;
29402 else
29403 main_comp_unit_die = comp_unit_die ();
29405 /* Output a terminator label for the .text section. */
29406 switch_to_section (text_section);
29407 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29408 if (cold_text_section)
29410 switch_to_section (cold_text_section);
29411 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29414 /* We can only use the low/high_pc attributes if all of the code was
29415 in .text. */
29416 if (!have_multiple_function_sections
29417 || (dwarf_version < 3 && dwarf_strict))
29419 /* Don't add if the CU has no associated code. */
29420 if (text_section_used)
29421 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29422 text_end_label, true);
29424 else
29426 unsigned fde_idx;
29427 dw_fde_ref fde;
29428 bool range_list_added = false;
29430 if (text_section_used)
29431 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29432 text_end_label, &range_list_added, true);
29433 if (cold_text_section_used)
29434 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29435 cold_end_label, &range_list_added, true);
29437 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29439 if (DECL_IGNORED_P (fde->decl))
29440 continue;
29441 if (!fde->in_std_section)
29442 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29443 fde->dw_fde_end, &range_list_added,
29444 true);
29445 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29446 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29447 fde->dw_fde_second_end, &range_list_added,
29448 true);
29451 if (range_list_added)
29453 /* We need to give .debug_loc and .debug_ranges an appropriate
29454 "base address". Use zero so that these addresses become
29455 absolute. Historically, we've emitted the unexpected
29456 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29457 Emit both to give time for other tools to adapt. */
29458 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29459 if (! dwarf_strict && dwarf_version < 4)
29460 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29462 add_ranges (NULL);
29466 if (debug_info_level >= DINFO_LEVEL_TERSE)
29467 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29468 debug_line_section_label);
29470 if (have_macinfo)
29471 add_AT_macptr (comp_unit_die (),
29472 dwarf_version >= 5 ? DW_AT_macros
29473 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29474 macinfo_section_label);
29476 if (dwarf_split_debug_info)
29478 if (have_location_lists)
29480 if (dwarf_version >= 5)
29481 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29482 loc_section_label);
29483 /* optimize_location_lists calculates the size of the lists,
29484 so index them first, and assign indices to the entries.
29485 Although optimize_location_lists will remove entries from
29486 the table, it only does so for duplicates, and therefore
29487 only reduces ref_counts to 1. */
29488 index_location_lists (comp_unit_die ());
29491 if (addr_index_table != NULL)
29493 unsigned int index = 0;
29494 addr_index_table
29495 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29496 (&index);
29500 loc_list_idx = 0;
29501 if (have_location_lists)
29503 optimize_location_lists (comp_unit_die ());
29504 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29505 if (dwarf_version >= 5 && dwarf_split_debug_info)
29506 assign_location_list_indexes (comp_unit_die ());
29509 save_macinfo_strings ();
29511 if (dwarf_split_debug_info)
29513 unsigned int index = 0;
29515 /* Add attributes common to skeleton compile_units and
29516 type_units. Because these attributes include strings, it
29517 must be done before freezing the string table. Top-level
29518 skeleton die attrs are added when the skeleton type unit is
29519 created, so ensure it is created by this point. */
29520 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29521 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29524 /* Output all of the compilation units. We put the main one last so that
29525 the offsets are available to output_pubnames. */
29526 for (node = cu_die_list; node; node = node->next)
29527 output_comp_unit (node->die, 0, NULL);
29529 hash_table<comdat_type_hasher> comdat_type_table (100);
29530 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29532 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29534 /* Don't output duplicate types. */
29535 if (*slot != HTAB_EMPTY_ENTRY)
29536 continue;
29538 /* Add a pointer to the line table for the main compilation unit
29539 so that the debugger can make sense of DW_AT_decl_file
29540 attributes. */
29541 if (debug_info_level >= DINFO_LEVEL_TERSE)
29542 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29543 (!dwarf_split_debug_info
29544 ? debug_line_section_label
29545 : debug_skeleton_line_section_label));
29547 output_comdat_type_unit (ctnode);
29548 *slot = ctnode;
29551 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29552 both the main_cu and all skeleton TUs. Making this call unconditional
29553 would end up either adding a second copy of the AT_pubnames attribute, or
29554 requiring a special case in add_top_level_skeleton_die_attrs. */
29555 if (!dwarf_split_debug_info)
29556 add_AT_pubnames (comp_unit_die ());
29558 if (dwarf_split_debug_info)
29560 int mark;
29561 struct md5_ctx ctx;
29563 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29564 index_rnglists ();
29566 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29567 md5_init_ctx (&ctx);
29568 mark = 0;
29569 die_checksum (comp_unit_die (), &ctx, &mark);
29570 unmark_all_dies (comp_unit_die ());
29571 md5_finish_ctx (&ctx, checksum);
29573 if (dwarf_version < 5)
29575 /* Use the first 8 bytes of the checksum as the dwo_id,
29576 and add it to both comp-unit DIEs. */
29577 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29578 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29581 /* Add the base offset of the ranges table to the skeleton
29582 comp-unit DIE. */
29583 if (!vec_safe_is_empty (ranges_table))
29585 if (dwarf_version >= 5)
29586 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29587 ranges_base_label);
29588 else
29589 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29590 ranges_section_label);
29593 switch_to_section (debug_addr_section);
29594 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29595 output_addr_table ();
29598 /* Output the main compilation unit if non-empty or if .debug_macinfo
29599 or .debug_macro will be emitted. */
29600 output_comp_unit (comp_unit_die (), have_macinfo,
29601 dwarf_split_debug_info ? checksum : NULL);
29603 if (dwarf_split_debug_info && info_section_emitted)
29604 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29606 /* Output the abbreviation table. */
29607 if (vec_safe_length (abbrev_die_table) != 1)
29609 switch_to_section (debug_abbrev_section);
29610 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29611 output_abbrev_section ();
29614 /* Output location list section if necessary. */
29615 if (have_location_lists)
29617 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29618 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29619 /* Output the location lists info. */
29620 switch_to_section (debug_loc_section);
29621 if (dwarf_version >= 5)
29623 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29624 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29625 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29626 dw2_asm_output_data (4, 0xffffffff,
29627 "Initial length escape value indicating "
29628 "64-bit DWARF extension");
29629 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29630 "Length of Location Lists");
29631 ASM_OUTPUT_LABEL (asm_out_file, l1);
29632 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29633 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29634 dw2_asm_output_data (1, 0, "Segment Size");
29635 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29636 "Offset Entry Count");
29638 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29639 if (dwarf_version >= 5 && dwarf_split_debug_info)
29641 unsigned int save_loc_list_idx = loc_list_idx;
29642 loc_list_idx = 0;
29643 output_loclists_offsets (comp_unit_die ());
29644 gcc_assert (save_loc_list_idx == loc_list_idx);
29646 output_location_lists (comp_unit_die ());
29647 if (dwarf_version >= 5)
29648 ASM_OUTPUT_LABEL (asm_out_file, l2);
29651 output_pubtables ();
29653 /* Output the address range information if a CU (.debug_info section)
29654 was emitted. We output an empty table even if we had no functions
29655 to put in it. This because the consumer has no way to tell the
29656 difference between an empty table that we omitted and failure to
29657 generate a table that would have contained data. */
29658 if (info_section_emitted)
29660 switch_to_section (debug_aranges_section);
29661 output_aranges ();
29664 /* Output ranges section if necessary. */
29665 if (!vec_safe_is_empty (ranges_table))
29667 if (dwarf_version >= 5)
29668 output_rnglists ();
29669 else
29670 output_ranges ();
29673 /* Have to end the macro section. */
29674 if (have_macinfo)
29676 switch_to_section (debug_macinfo_section);
29677 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29678 output_macinfo ();
29679 dw2_asm_output_data (1, 0, "End compilation unit");
29682 /* Output the source line correspondence table. We must do this
29683 even if there is no line information. Otherwise, on an empty
29684 translation unit, we will generate a present, but empty,
29685 .debug_info section. IRIX 6.5 `nm' will then complain when
29686 examining the file. This is done late so that any filenames
29687 used by the debug_info section are marked as 'used'. */
29688 switch_to_section (debug_line_section);
29689 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29690 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29691 output_line_info (false);
29693 if (dwarf_split_debug_info && info_section_emitted)
29695 switch_to_section (debug_skeleton_line_section);
29696 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29697 output_line_info (true);
29700 /* If we emitted any indirect strings, output the string table too. */
29701 if (debug_str_hash || skeleton_debug_str_hash)
29702 output_indirect_strings ();
29703 if (debug_line_str_hash)
29705 switch_to_section (debug_line_str_section);
29706 const enum dwarf_form form = DW_FORM_line_strp;
29707 debug_line_str_hash->traverse<enum dwarf_form,
29708 output_indirect_string> (form);
29712 /* Perform any cleanups needed after the early debug generation pass
29713 has run. */
29715 static void
29716 dwarf2out_early_finish (const char *filename)
29718 set_early_dwarf s;
29720 /* PCH might result in DW_AT_producer string being restored from the
29721 header compilation, so always fill it with empty string initially
29722 and overwrite only here. */
29723 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
29724 producer_string = gen_producer_string ();
29725 producer->dw_attr_val.v.val_str->refcount--;
29726 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
29728 /* Add the name for the main input file now. We delayed this from
29729 dwarf2out_init to avoid complications with PCH. */
29730 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
29731 add_comp_dir_attribute (comp_unit_die ());
29733 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
29734 DW_AT_comp_dir into .debug_line_str section. */
29735 if (!DWARF2_ASM_LINE_DEBUG_INFO
29736 && dwarf_version >= 5
29737 && DWARF5_USE_DEBUG_LINE_STR)
29739 for (int i = 0; i < 2; i++)
29741 dw_attr_node *a = get_AT (comp_unit_die (),
29742 i ? DW_AT_comp_dir : DW_AT_name);
29743 if (a == NULL
29744 || AT_class (a) != dw_val_class_str
29745 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
29746 continue;
29748 if (! debug_line_str_hash)
29749 debug_line_str_hash
29750 = hash_table<indirect_string_hasher>::create_ggc (10);
29752 struct indirect_string_node *node
29753 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
29754 set_indirect_string (node);
29755 node->form = DW_FORM_line_strp;
29756 a->dw_attr_val.v.val_str->refcount--;
29757 a->dw_attr_val.v.val_str = node;
29761 /* With LTO early dwarf was really finished at compile-time, so make
29762 sure to adjust the phase after annotating the LTRANS CU DIE. */
29763 if (in_lto_p)
29765 early_dwarf_finished = true;
29766 return;
29769 /* Walk through the list of incomplete types again, trying once more to
29770 emit full debugging info for them. */
29771 retry_incomplete_types ();
29773 /* The point here is to flush out the limbo list so that it is empty
29774 and we don't need to stream it for LTO. */
29775 flush_limbo_die_list ();
29777 gen_scheduled_generic_parms_dies ();
29778 gen_remaining_tmpl_value_param_die_attribute ();
29780 /* Add DW_AT_linkage_name for all deferred DIEs. */
29781 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
29783 tree decl = node->created_for;
29784 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
29785 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
29786 ended up in deferred_asm_name before we knew it was
29787 constant and never written to disk. */
29788 && DECL_ASSEMBLER_NAME (decl))
29790 add_linkage_attr (node->die, decl);
29791 move_linkage_attr (node->die);
29794 deferred_asm_name = NULL;
29796 if (flag_eliminate_unused_debug_types)
29797 prune_unused_types ();
29799 /* Generate separate COMDAT sections for type DIEs. */
29800 if (use_debug_types)
29802 break_out_comdat_types (comp_unit_die ());
29804 /* Each new type_unit DIE was added to the limbo die list when created.
29805 Since these have all been added to comdat_type_list, clear the
29806 limbo die list. */
29807 limbo_die_list = NULL;
29809 /* For each new comdat type unit, copy declarations for incomplete
29810 types to make the new unit self-contained (i.e., no direct
29811 references to the main compile unit). */
29812 for (comdat_type_node *ctnode = comdat_type_list;
29813 ctnode != NULL; ctnode = ctnode->next)
29814 copy_decls_for_unworthy_types (ctnode->root_die);
29815 copy_decls_for_unworthy_types (comp_unit_die ());
29817 /* In the process of copying declarations from one unit to another,
29818 we may have left some declarations behind that are no longer
29819 referenced. Prune them. */
29820 prune_unused_types ();
29823 /* Generate separate CUs for each of the include files we've seen.
29824 They will go into limbo_die_list and from there to cu_die_list. */
29825 if (flag_eliminate_dwarf2_dups)
29827 gcc_assert (limbo_die_list == NULL);
29828 break_out_includes (comp_unit_die ());
29829 limbo_die_node *cu;
29830 while ((cu = limbo_die_list))
29832 limbo_die_list = cu->next;
29833 cu->next = cu_die_list;
29834 cu_die_list = cu;
29838 /* The early debug phase is now finished. */
29839 early_dwarf_finished = true;
29842 /* Reset all state within dwarf2out.c so that we can rerun the compiler
29843 within the same process. For use by toplev::finalize. */
29845 void
29846 dwarf2out_c_finalize (void)
29848 last_var_location_insn = NULL;
29849 cached_next_real_insn = NULL;
29850 used_rtx_array = NULL;
29851 incomplete_types = NULL;
29852 decl_scope_table = NULL;
29853 debug_info_section = NULL;
29854 debug_skeleton_info_section = NULL;
29855 debug_abbrev_section = NULL;
29856 debug_skeleton_abbrev_section = NULL;
29857 debug_aranges_section = NULL;
29858 debug_addr_section = NULL;
29859 debug_macinfo_section = NULL;
29860 debug_line_section = NULL;
29861 debug_skeleton_line_section = NULL;
29862 debug_loc_section = NULL;
29863 debug_pubnames_section = NULL;
29864 debug_pubtypes_section = NULL;
29865 debug_str_section = NULL;
29866 debug_line_str_section = NULL;
29867 debug_str_dwo_section = NULL;
29868 debug_str_offsets_section = NULL;
29869 debug_ranges_section = NULL;
29870 debug_frame_section = NULL;
29871 fde_vec = NULL;
29872 debug_str_hash = NULL;
29873 debug_line_str_hash = NULL;
29874 skeleton_debug_str_hash = NULL;
29875 dw2_string_counter = 0;
29876 have_multiple_function_sections = false;
29877 text_section_used = false;
29878 cold_text_section_used = false;
29879 cold_text_section = NULL;
29880 current_unit_personality = NULL;
29882 early_dwarf = false;
29883 early_dwarf_finished = false;
29885 next_die_offset = 0;
29886 single_comp_unit_die = NULL;
29887 comdat_type_list = NULL;
29888 limbo_die_list = NULL;
29889 file_table = NULL;
29890 decl_die_table = NULL;
29891 common_block_die_table = NULL;
29892 decl_loc_table = NULL;
29893 call_arg_locations = NULL;
29894 call_arg_loc_last = NULL;
29895 call_site_count = -1;
29896 tail_call_site_count = -1;
29897 cached_dw_loc_list_table = NULL;
29898 abbrev_die_table = NULL;
29899 delete dwarf_proc_stack_usage_map;
29900 dwarf_proc_stack_usage_map = NULL;
29901 line_info_label_num = 0;
29902 cur_line_info_table = NULL;
29903 text_section_line_info = NULL;
29904 cold_text_section_line_info = NULL;
29905 separate_line_info = NULL;
29906 info_section_emitted = false;
29907 pubname_table = NULL;
29908 pubtype_table = NULL;
29909 macinfo_table = NULL;
29910 ranges_table = NULL;
29911 ranges_by_label = NULL;
29912 rnglist_idx = 0;
29913 have_location_lists = false;
29914 loclabel_num = 0;
29915 poc_label_num = 0;
29916 last_emitted_file = NULL;
29917 label_num = 0;
29918 tmpl_value_parm_die_table = NULL;
29919 generic_type_instances = NULL;
29920 frame_pointer_fb_offset = 0;
29921 frame_pointer_fb_offset_valid = false;
29922 base_types.release ();
29923 XDELETEVEC (producer_string);
29924 producer_string = NULL;
29927 #include "gt-dwarf2out.h"