PR rtl-optimization/78355
[official-gcc.git] / gcc / dwarf2out.c
bloba7344ca01363e594879297fb13f83fc328d93faf
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2016 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 = int_loc_descriptor (-offset);
1518 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1522 /* Add a constant OFFSET to a location list. */
1524 static void
1525 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1527 dw_loc_list_ref d;
1528 for (d = list_head; d != NULL; d = d->dw_loc_next)
1529 loc_descr_plus_const (&d->expr, offset);
1532 #define DWARF_REF_SIZE \
1533 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1535 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1536 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1537 DW_FORM_data16 with 128 bits. */
1538 #define DWARF_LARGEST_DATA_FORM_BITS \
1539 (dwarf_version >= 5 ? 128 : 64)
1541 /* Utility inline function for construction of ops that were GNU extension
1542 before DWARF 5. */
1543 static inline enum dwarf_location_atom
1544 dwarf_OP (enum dwarf_location_atom op)
1546 switch (op)
1548 case DW_OP_implicit_pointer:
1549 if (dwarf_version < 5)
1550 return DW_OP_GNU_implicit_pointer;
1551 break;
1553 case DW_OP_entry_value:
1554 if (dwarf_version < 5)
1555 return DW_OP_GNU_entry_value;
1556 break;
1558 case DW_OP_const_type:
1559 if (dwarf_version < 5)
1560 return DW_OP_GNU_const_type;
1561 break;
1563 case DW_OP_regval_type:
1564 if (dwarf_version < 5)
1565 return DW_OP_GNU_regval_type;
1566 break;
1568 case DW_OP_deref_type:
1569 if (dwarf_version < 5)
1570 return DW_OP_GNU_deref_type;
1571 break;
1573 case DW_OP_convert:
1574 if (dwarf_version < 5)
1575 return DW_OP_GNU_convert;
1576 break;
1578 case DW_OP_reinterpret:
1579 if (dwarf_version < 5)
1580 return DW_OP_GNU_reinterpret;
1581 break;
1583 default:
1584 break;
1586 return op;
1589 /* Similarly for attributes. */
1590 static inline enum dwarf_attribute
1591 dwarf_AT (enum dwarf_attribute at)
1593 switch (at)
1595 case DW_AT_call_return_pc:
1596 if (dwarf_version < 5)
1597 return DW_AT_low_pc;
1598 break;
1600 case DW_AT_call_tail_call:
1601 if (dwarf_version < 5)
1602 return DW_AT_GNU_tail_call;
1603 break;
1605 case DW_AT_call_origin:
1606 if (dwarf_version < 5)
1607 return DW_AT_abstract_origin;
1608 break;
1610 case DW_AT_call_target:
1611 if (dwarf_version < 5)
1612 return DW_AT_GNU_call_site_target;
1613 break;
1615 case DW_AT_call_target_clobbered:
1616 if (dwarf_version < 5)
1617 return DW_AT_GNU_call_site_target_clobbered;
1618 break;
1620 case DW_AT_call_parameter:
1621 if (dwarf_version < 5)
1622 return DW_AT_abstract_origin;
1623 break;
1625 case DW_AT_call_value:
1626 if (dwarf_version < 5)
1627 return DW_AT_GNU_call_site_value;
1628 break;
1630 case DW_AT_call_data_value:
1631 if (dwarf_version < 5)
1632 return DW_AT_GNU_call_site_data_value;
1633 break;
1635 case DW_AT_call_all_calls:
1636 if (dwarf_version < 5)
1637 return DW_AT_GNU_all_call_sites;
1638 break;
1640 case DW_AT_call_all_tail_calls:
1641 if (dwarf_version < 5)
1642 return DW_AT_GNU_all_tail_call_sites;
1643 break;
1645 case DW_AT_dwo_name:
1646 if (dwarf_version < 5)
1647 return DW_AT_GNU_dwo_name;
1648 break;
1650 default:
1651 break;
1653 return at;
1656 /* And similarly for tags. */
1657 static inline enum dwarf_tag
1658 dwarf_TAG (enum dwarf_tag tag)
1660 switch (tag)
1662 case DW_TAG_call_site:
1663 if (dwarf_version < 5)
1664 return DW_TAG_GNU_call_site;
1665 break;
1667 case DW_TAG_call_site_parameter:
1668 if (dwarf_version < 5)
1669 return DW_TAG_GNU_call_site_parameter;
1670 break;
1672 default:
1673 break;
1675 return tag;
1678 static unsigned long int get_base_type_offset (dw_die_ref);
1680 /* Return the size of a location descriptor. */
1682 static unsigned long
1683 size_of_loc_descr (dw_loc_descr_ref loc)
1685 unsigned long size = 1;
1687 switch (loc->dw_loc_opc)
1689 case DW_OP_addr:
1690 size += DWARF2_ADDR_SIZE;
1691 break;
1692 case DW_OP_GNU_addr_index:
1693 case DW_OP_GNU_const_index:
1694 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1695 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1696 break;
1697 case DW_OP_const1u:
1698 case DW_OP_const1s:
1699 size += 1;
1700 break;
1701 case DW_OP_const2u:
1702 case DW_OP_const2s:
1703 size += 2;
1704 break;
1705 case DW_OP_const4u:
1706 case DW_OP_const4s:
1707 size += 4;
1708 break;
1709 case DW_OP_const8u:
1710 case DW_OP_const8s:
1711 size += 8;
1712 break;
1713 case DW_OP_constu:
1714 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1715 break;
1716 case DW_OP_consts:
1717 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1718 break;
1719 case DW_OP_pick:
1720 size += 1;
1721 break;
1722 case DW_OP_plus_uconst:
1723 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1724 break;
1725 case DW_OP_skip:
1726 case DW_OP_bra:
1727 size += 2;
1728 break;
1729 case DW_OP_breg0:
1730 case DW_OP_breg1:
1731 case DW_OP_breg2:
1732 case DW_OP_breg3:
1733 case DW_OP_breg4:
1734 case DW_OP_breg5:
1735 case DW_OP_breg6:
1736 case DW_OP_breg7:
1737 case DW_OP_breg8:
1738 case DW_OP_breg9:
1739 case DW_OP_breg10:
1740 case DW_OP_breg11:
1741 case DW_OP_breg12:
1742 case DW_OP_breg13:
1743 case DW_OP_breg14:
1744 case DW_OP_breg15:
1745 case DW_OP_breg16:
1746 case DW_OP_breg17:
1747 case DW_OP_breg18:
1748 case DW_OP_breg19:
1749 case DW_OP_breg20:
1750 case DW_OP_breg21:
1751 case DW_OP_breg22:
1752 case DW_OP_breg23:
1753 case DW_OP_breg24:
1754 case DW_OP_breg25:
1755 case DW_OP_breg26:
1756 case DW_OP_breg27:
1757 case DW_OP_breg28:
1758 case DW_OP_breg29:
1759 case DW_OP_breg30:
1760 case DW_OP_breg31:
1761 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1762 break;
1763 case DW_OP_regx:
1764 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1765 break;
1766 case DW_OP_fbreg:
1767 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1768 break;
1769 case DW_OP_bregx:
1770 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1771 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1772 break;
1773 case DW_OP_piece:
1774 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1775 break;
1776 case DW_OP_bit_piece:
1777 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1778 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1779 break;
1780 case DW_OP_deref_size:
1781 case DW_OP_xderef_size:
1782 size += 1;
1783 break;
1784 case DW_OP_call2:
1785 size += 2;
1786 break;
1787 case DW_OP_call4:
1788 size += 4;
1789 break;
1790 case DW_OP_call_ref:
1791 size += DWARF_REF_SIZE;
1792 break;
1793 case DW_OP_implicit_value:
1794 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1795 + loc->dw_loc_oprnd1.v.val_unsigned;
1796 break;
1797 case DW_OP_implicit_pointer:
1798 case DW_OP_GNU_implicit_pointer:
1799 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1800 break;
1801 case DW_OP_entry_value:
1802 case DW_OP_GNU_entry_value:
1804 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1805 size += size_of_uleb128 (op_size) + op_size;
1806 break;
1808 case DW_OP_const_type:
1809 case DW_OP_GNU_const_type:
1811 unsigned long o
1812 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1813 size += size_of_uleb128 (o) + 1;
1814 switch (loc->dw_loc_oprnd2.val_class)
1816 case dw_val_class_vec:
1817 size += loc->dw_loc_oprnd2.v.val_vec.length
1818 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1819 break;
1820 case dw_val_class_const:
1821 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1822 break;
1823 case dw_val_class_const_double:
1824 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1825 break;
1826 case dw_val_class_wide_int:
1827 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1828 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1829 break;
1830 default:
1831 gcc_unreachable ();
1833 break;
1835 case DW_OP_regval_type:
1836 case DW_OP_GNU_regval_type:
1838 unsigned long o
1839 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1840 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1841 + size_of_uleb128 (o);
1843 break;
1844 case DW_OP_deref_type:
1845 case DW_OP_GNU_deref_type:
1847 unsigned long o
1848 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1849 size += 1 + size_of_uleb128 (o);
1851 break;
1852 case DW_OP_convert:
1853 case DW_OP_reinterpret:
1854 case DW_OP_GNU_convert:
1855 case DW_OP_GNU_reinterpret:
1856 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1857 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1858 else
1860 unsigned long o
1861 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1862 size += size_of_uleb128 (o);
1864 break;
1865 case DW_OP_GNU_parameter_ref:
1866 size += 4;
1867 break;
1868 default:
1869 break;
1872 return size;
1875 /* Return the size of a series of location descriptors. */
1877 unsigned long
1878 size_of_locs (dw_loc_descr_ref loc)
1880 dw_loc_descr_ref l;
1881 unsigned long size;
1883 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1884 field, to avoid writing to a PCH file. */
1885 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1887 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1888 break;
1889 size += size_of_loc_descr (l);
1891 if (! l)
1892 return size;
1894 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1896 l->dw_loc_addr = size;
1897 size += size_of_loc_descr (l);
1900 return size;
1903 /* Return the size of the value in a DW_AT_discr_value attribute. */
1905 static int
1906 size_of_discr_value (dw_discr_value *discr_value)
1908 if (discr_value->pos)
1909 return size_of_uleb128 (discr_value->v.uval);
1910 else
1911 return size_of_sleb128 (discr_value->v.sval);
1914 /* Return the size of the value in a DW_discr_list attribute. */
1916 static int
1917 size_of_discr_list (dw_discr_list_ref discr_list)
1919 int size = 0;
1921 for (dw_discr_list_ref list = discr_list;
1922 list != NULL;
1923 list = list->dw_discr_next)
1925 /* One byte for the discriminant value descriptor, and then one or two
1926 LEB128 numbers, depending on whether it's a single case label or a
1927 range label. */
1928 size += 1;
1929 size += size_of_discr_value (&list->dw_discr_lower_bound);
1930 if (list->dw_discr_range != 0)
1931 size += size_of_discr_value (&list->dw_discr_upper_bound);
1933 return size;
1936 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1937 static void get_ref_die_offset_label (char *, dw_die_ref);
1938 static unsigned long int get_ref_die_offset (dw_die_ref);
1940 /* Output location description stack opcode's operands (if any).
1941 The for_eh_or_skip parameter controls whether register numbers are
1942 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1943 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1944 info). This should be suppressed for the cases that have not been converted
1945 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1947 static void
1948 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1950 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1951 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1953 switch (loc->dw_loc_opc)
1955 #ifdef DWARF2_DEBUGGING_INFO
1956 case DW_OP_const2u:
1957 case DW_OP_const2s:
1958 dw2_asm_output_data (2, val1->v.val_int, NULL);
1959 break;
1960 case DW_OP_const4u:
1961 if (loc->dtprel)
1963 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1964 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1965 val1->v.val_addr);
1966 fputc ('\n', asm_out_file);
1967 break;
1969 /* FALLTHRU */
1970 case DW_OP_const4s:
1971 dw2_asm_output_data (4, val1->v.val_int, NULL);
1972 break;
1973 case DW_OP_const8u:
1974 if (loc->dtprel)
1976 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1977 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1978 val1->v.val_addr);
1979 fputc ('\n', asm_out_file);
1980 break;
1982 /* FALLTHRU */
1983 case DW_OP_const8s:
1984 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1985 dw2_asm_output_data (8, val1->v.val_int, NULL);
1986 break;
1987 case DW_OP_skip:
1988 case DW_OP_bra:
1990 int offset;
1992 gcc_assert (val1->val_class == dw_val_class_loc);
1993 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1995 dw2_asm_output_data (2, offset, NULL);
1997 break;
1998 case DW_OP_implicit_value:
1999 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2000 switch (val2->val_class)
2002 case dw_val_class_const:
2003 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2004 break;
2005 case dw_val_class_vec:
2007 unsigned int elt_size = val2->v.val_vec.elt_size;
2008 unsigned int len = val2->v.val_vec.length;
2009 unsigned int i;
2010 unsigned char *p;
2012 if (elt_size > sizeof (HOST_WIDE_INT))
2014 elt_size /= 2;
2015 len *= 2;
2017 for (i = 0, p = val2->v.val_vec.array;
2018 i < len;
2019 i++, p += elt_size)
2020 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2021 "fp or vector constant word %u", i);
2023 break;
2024 case dw_val_class_const_double:
2026 unsigned HOST_WIDE_INT first, second;
2028 if (WORDS_BIG_ENDIAN)
2030 first = val2->v.val_double.high;
2031 second = val2->v.val_double.low;
2033 else
2035 first = val2->v.val_double.low;
2036 second = val2->v.val_double.high;
2038 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2039 first, NULL);
2040 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2041 second, NULL);
2043 break;
2044 case dw_val_class_wide_int:
2046 int i;
2047 int len = get_full_len (*val2->v.val_wide);
2048 if (WORDS_BIG_ENDIAN)
2049 for (i = len - 1; i >= 0; --i)
2050 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2051 val2->v.val_wide->elt (i), NULL);
2052 else
2053 for (i = 0; i < len; ++i)
2054 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2055 val2->v.val_wide->elt (i), NULL);
2057 break;
2058 case dw_val_class_addr:
2059 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2060 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2061 break;
2062 default:
2063 gcc_unreachable ();
2065 break;
2066 #else
2067 case DW_OP_const2u:
2068 case DW_OP_const2s:
2069 case DW_OP_const4u:
2070 case DW_OP_const4s:
2071 case DW_OP_const8u:
2072 case DW_OP_const8s:
2073 case DW_OP_skip:
2074 case DW_OP_bra:
2075 case DW_OP_implicit_value:
2076 /* We currently don't make any attempt to make sure these are
2077 aligned properly like we do for the main unwind info, so
2078 don't support emitting things larger than a byte if we're
2079 only doing unwinding. */
2080 gcc_unreachable ();
2081 #endif
2082 case DW_OP_const1u:
2083 case DW_OP_const1s:
2084 dw2_asm_output_data (1, val1->v.val_int, NULL);
2085 break;
2086 case DW_OP_constu:
2087 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2088 break;
2089 case DW_OP_consts:
2090 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2091 break;
2092 case DW_OP_pick:
2093 dw2_asm_output_data (1, val1->v.val_int, NULL);
2094 break;
2095 case DW_OP_plus_uconst:
2096 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2097 break;
2098 case DW_OP_breg0:
2099 case DW_OP_breg1:
2100 case DW_OP_breg2:
2101 case DW_OP_breg3:
2102 case DW_OP_breg4:
2103 case DW_OP_breg5:
2104 case DW_OP_breg6:
2105 case DW_OP_breg7:
2106 case DW_OP_breg8:
2107 case DW_OP_breg9:
2108 case DW_OP_breg10:
2109 case DW_OP_breg11:
2110 case DW_OP_breg12:
2111 case DW_OP_breg13:
2112 case DW_OP_breg14:
2113 case DW_OP_breg15:
2114 case DW_OP_breg16:
2115 case DW_OP_breg17:
2116 case DW_OP_breg18:
2117 case DW_OP_breg19:
2118 case DW_OP_breg20:
2119 case DW_OP_breg21:
2120 case DW_OP_breg22:
2121 case DW_OP_breg23:
2122 case DW_OP_breg24:
2123 case DW_OP_breg25:
2124 case DW_OP_breg26:
2125 case DW_OP_breg27:
2126 case DW_OP_breg28:
2127 case DW_OP_breg29:
2128 case DW_OP_breg30:
2129 case DW_OP_breg31:
2130 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2131 break;
2132 case DW_OP_regx:
2134 unsigned r = val1->v.val_unsigned;
2135 if (for_eh_or_skip >= 0)
2136 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2137 gcc_assert (size_of_uleb128 (r)
2138 == size_of_uleb128 (val1->v.val_unsigned));
2139 dw2_asm_output_data_uleb128 (r, NULL);
2141 break;
2142 case DW_OP_fbreg:
2143 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2144 break;
2145 case DW_OP_bregx:
2147 unsigned r = val1->v.val_unsigned;
2148 if (for_eh_or_skip >= 0)
2149 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2150 gcc_assert (size_of_uleb128 (r)
2151 == size_of_uleb128 (val1->v.val_unsigned));
2152 dw2_asm_output_data_uleb128 (r, NULL);
2153 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2155 break;
2156 case DW_OP_piece:
2157 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2158 break;
2159 case DW_OP_bit_piece:
2160 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2161 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2162 break;
2163 case DW_OP_deref_size:
2164 case DW_OP_xderef_size:
2165 dw2_asm_output_data (1, val1->v.val_int, NULL);
2166 break;
2168 case DW_OP_addr:
2169 if (loc->dtprel)
2171 if (targetm.asm_out.output_dwarf_dtprel)
2173 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2174 DWARF2_ADDR_SIZE,
2175 val1->v.val_addr);
2176 fputc ('\n', asm_out_file);
2178 else
2179 gcc_unreachable ();
2181 else
2183 #ifdef DWARF2_DEBUGGING_INFO
2184 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2185 #else
2186 gcc_unreachable ();
2187 #endif
2189 break;
2191 case DW_OP_GNU_addr_index:
2192 case DW_OP_GNU_const_index:
2193 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2194 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2195 "(index into .debug_addr)");
2196 break;
2198 case DW_OP_call2:
2199 case DW_OP_call4:
2201 unsigned long die_offset
2202 = get_ref_die_offset (val1->v.val_die_ref.die);
2203 /* Make sure the offset has been computed and that we can encode it as
2204 an operand. */
2205 gcc_assert (die_offset > 0
2206 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2207 ? 0xffff
2208 : 0xffffffff));
2209 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2210 die_offset, NULL);
2212 break;
2214 case DW_OP_implicit_pointer:
2215 case DW_OP_GNU_implicit_pointer:
2217 char label[MAX_ARTIFICIAL_LABEL_BYTES
2218 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2219 gcc_assert (val1->val_class == dw_val_class_die_ref);
2220 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2221 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2222 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2224 break;
2226 case DW_OP_entry_value:
2227 case DW_OP_GNU_entry_value:
2228 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2229 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2230 break;
2232 case DW_OP_const_type:
2233 case DW_OP_GNU_const_type:
2235 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2236 gcc_assert (o);
2237 dw2_asm_output_data_uleb128 (o, NULL);
2238 switch (val2->val_class)
2240 case dw_val_class_const:
2241 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2242 dw2_asm_output_data (1, l, NULL);
2243 dw2_asm_output_data (l, val2->v.val_int, NULL);
2244 break;
2245 case dw_val_class_vec:
2247 unsigned int elt_size = val2->v.val_vec.elt_size;
2248 unsigned int len = val2->v.val_vec.length;
2249 unsigned int i;
2250 unsigned char *p;
2252 l = len * elt_size;
2253 dw2_asm_output_data (1, l, NULL);
2254 if (elt_size > sizeof (HOST_WIDE_INT))
2256 elt_size /= 2;
2257 len *= 2;
2259 for (i = 0, p = val2->v.val_vec.array;
2260 i < len;
2261 i++, p += elt_size)
2262 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2263 "fp or vector constant word %u", i);
2265 break;
2266 case dw_val_class_const_double:
2268 unsigned HOST_WIDE_INT first, second;
2269 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2271 dw2_asm_output_data (1, 2 * l, NULL);
2272 if (WORDS_BIG_ENDIAN)
2274 first = val2->v.val_double.high;
2275 second = val2->v.val_double.low;
2277 else
2279 first = val2->v.val_double.low;
2280 second = val2->v.val_double.high;
2282 dw2_asm_output_data (l, first, NULL);
2283 dw2_asm_output_data (l, second, NULL);
2285 break;
2286 case dw_val_class_wide_int:
2288 int i;
2289 int len = get_full_len (*val2->v.val_wide);
2290 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2292 dw2_asm_output_data (1, len * l, NULL);
2293 if (WORDS_BIG_ENDIAN)
2294 for (i = len - 1; i >= 0; --i)
2295 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2296 else
2297 for (i = 0; i < len; ++i)
2298 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2300 break;
2301 default:
2302 gcc_unreachable ();
2305 break;
2306 case DW_OP_regval_type:
2307 case DW_OP_GNU_regval_type:
2309 unsigned r = val1->v.val_unsigned;
2310 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2311 gcc_assert (o);
2312 if (for_eh_or_skip >= 0)
2314 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2315 gcc_assert (size_of_uleb128 (r)
2316 == size_of_uleb128 (val1->v.val_unsigned));
2318 dw2_asm_output_data_uleb128 (r, NULL);
2319 dw2_asm_output_data_uleb128 (o, NULL);
2321 break;
2322 case DW_OP_deref_type:
2323 case DW_OP_GNU_deref_type:
2325 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2326 gcc_assert (o);
2327 dw2_asm_output_data (1, val1->v.val_int, NULL);
2328 dw2_asm_output_data_uleb128 (o, NULL);
2330 break;
2331 case DW_OP_convert:
2332 case DW_OP_reinterpret:
2333 case DW_OP_GNU_convert:
2334 case DW_OP_GNU_reinterpret:
2335 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2336 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2337 else
2339 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2340 gcc_assert (o);
2341 dw2_asm_output_data_uleb128 (o, NULL);
2343 break;
2345 case DW_OP_GNU_parameter_ref:
2347 unsigned long o;
2348 gcc_assert (val1->val_class == dw_val_class_die_ref);
2349 o = get_ref_die_offset (val1->v.val_die_ref.die);
2350 dw2_asm_output_data (4, o, NULL);
2352 break;
2354 default:
2355 /* Other codes have no operands. */
2356 break;
2360 /* Output a sequence of location operations.
2361 The for_eh_or_skip parameter controls whether register numbers are
2362 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2363 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2364 info). This should be suppressed for the cases that have not been converted
2365 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2367 void
2368 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2370 for (; loc != NULL; loc = loc->dw_loc_next)
2372 enum dwarf_location_atom opc = loc->dw_loc_opc;
2373 /* Output the opcode. */
2374 if (for_eh_or_skip >= 0
2375 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2377 unsigned r = (opc - DW_OP_breg0);
2378 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2379 gcc_assert (r <= 31);
2380 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2382 else if (for_eh_or_skip >= 0
2383 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2385 unsigned r = (opc - DW_OP_reg0);
2386 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2387 gcc_assert (r <= 31);
2388 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2391 dw2_asm_output_data (1, opc,
2392 "%s", dwarf_stack_op_name (opc));
2394 /* Output the operand(s) (if any). */
2395 output_loc_operands (loc, for_eh_or_skip);
2399 /* Output location description stack opcode's operands (if any).
2400 The output is single bytes on a line, suitable for .cfi_escape. */
2402 static void
2403 output_loc_operands_raw (dw_loc_descr_ref loc)
2405 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2406 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2408 switch (loc->dw_loc_opc)
2410 case DW_OP_addr:
2411 case DW_OP_GNU_addr_index:
2412 case DW_OP_GNU_const_index:
2413 case DW_OP_implicit_value:
2414 /* We cannot output addresses in .cfi_escape, only bytes. */
2415 gcc_unreachable ();
2417 case DW_OP_const1u:
2418 case DW_OP_const1s:
2419 case DW_OP_pick:
2420 case DW_OP_deref_size:
2421 case DW_OP_xderef_size:
2422 fputc (',', asm_out_file);
2423 dw2_asm_output_data_raw (1, val1->v.val_int);
2424 break;
2426 case DW_OP_const2u:
2427 case DW_OP_const2s:
2428 fputc (',', asm_out_file);
2429 dw2_asm_output_data_raw (2, val1->v.val_int);
2430 break;
2432 case DW_OP_const4u:
2433 case DW_OP_const4s:
2434 fputc (',', asm_out_file);
2435 dw2_asm_output_data_raw (4, val1->v.val_int);
2436 break;
2438 case DW_OP_const8u:
2439 case DW_OP_const8s:
2440 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2441 fputc (',', asm_out_file);
2442 dw2_asm_output_data_raw (8, val1->v.val_int);
2443 break;
2445 case DW_OP_skip:
2446 case DW_OP_bra:
2448 int offset;
2450 gcc_assert (val1->val_class == dw_val_class_loc);
2451 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2453 fputc (',', asm_out_file);
2454 dw2_asm_output_data_raw (2, offset);
2456 break;
2458 case DW_OP_regx:
2460 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2461 gcc_assert (size_of_uleb128 (r)
2462 == size_of_uleb128 (val1->v.val_unsigned));
2463 fputc (',', asm_out_file);
2464 dw2_asm_output_data_uleb128_raw (r);
2466 break;
2468 case DW_OP_constu:
2469 case DW_OP_plus_uconst:
2470 case DW_OP_piece:
2471 fputc (',', asm_out_file);
2472 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2473 break;
2475 case DW_OP_bit_piece:
2476 fputc (',', asm_out_file);
2477 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2478 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2479 break;
2481 case DW_OP_consts:
2482 case DW_OP_breg0:
2483 case DW_OP_breg1:
2484 case DW_OP_breg2:
2485 case DW_OP_breg3:
2486 case DW_OP_breg4:
2487 case DW_OP_breg5:
2488 case DW_OP_breg6:
2489 case DW_OP_breg7:
2490 case DW_OP_breg8:
2491 case DW_OP_breg9:
2492 case DW_OP_breg10:
2493 case DW_OP_breg11:
2494 case DW_OP_breg12:
2495 case DW_OP_breg13:
2496 case DW_OP_breg14:
2497 case DW_OP_breg15:
2498 case DW_OP_breg16:
2499 case DW_OP_breg17:
2500 case DW_OP_breg18:
2501 case DW_OP_breg19:
2502 case DW_OP_breg20:
2503 case DW_OP_breg21:
2504 case DW_OP_breg22:
2505 case DW_OP_breg23:
2506 case DW_OP_breg24:
2507 case DW_OP_breg25:
2508 case DW_OP_breg26:
2509 case DW_OP_breg27:
2510 case DW_OP_breg28:
2511 case DW_OP_breg29:
2512 case DW_OP_breg30:
2513 case DW_OP_breg31:
2514 case DW_OP_fbreg:
2515 fputc (',', asm_out_file);
2516 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2517 break;
2519 case DW_OP_bregx:
2521 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2522 gcc_assert (size_of_uleb128 (r)
2523 == size_of_uleb128 (val1->v.val_unsigned));
2524 fputc (',', asm_out_file);
2525 dw2_asm_output_data_uleb128_raw (r);
2526 fputc (',', asm_out_file);
2527 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2529 break;
2531 case DW_OP_implicit_pointer:
2532 case DW_OP_entry_value:
2533 case DW_OP_const_type:
2534 case DW_OP_regval_type:
2535 case DW_OP_deref_type:
2536 case DW_OP_convert:
2537 case DW_OP_reinterpret:
2538 case DW_OP_GNU_implicit_pointer:
2539 case DW_OP_GNU_entry_value:
2540 case DW_OP_GNU_const_type:
2541 case DW_OP_GNU_regval_type:
2542 case DW_OP_GNU_deref_type:
2543 case DW_OP_GNU_convert:
2544 case DW_OP_GNU_reinterpret:
2545 case DW_OP_GNU_parameter_ref:
2546 gcc_unreachable ();
2547 break;
2549 default:
2550 /* Other codes have no operands. */
2551 break;
2555 void
2556 output_loc_sequence_raw (dw_loc_descr_ref loc)
2558 while (1)
2560 enum dwarf_location_atom opc = loc->dw_loc_opc;
2561 /* Output the opcode. */
2562 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2564 unsigned r = (opc - DW_OP_breg0);
2565 r = DWARF2_FRAME_REG_OUT (r, 1);
2566 gcc_assert (r <= 31);
2567 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2569 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2571 unsigned r = (opc - DW_OP_reg0);
2572 r = DWARF2_FRAME_REG_OUT (r, 1);
2573 gcc_assert (r <= 31);
2574 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2576 /* Output the opcode. */
2577 fprintf (asm_out_file, "%#x", opc);
2578 output_loc_operands_raw (loc);
2580 if (!loc->dw_loc_next)
2581 break;
2582 loc = loc->dw_loc_next;
2584 fputc (',', asm_out_file);
2588 /* This function builds a dwarf location descriptor sequence from a
2589 dw_cfa_location, adding the given OFFSET to the result of the
2590 expression. */
2592 struct dw_loc_descr_node *
2593 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2595 struct dw_loc_descr_node *head, *tmp;
2597 offset += cfa->offset;
2599 if (cfa->indirect)
2601 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2602 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2603 head->dw_loc_oprnd1.val_entry = NULL;
2604 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2605 add_loc_descr (&head, tmp);
2606 if (offset != 0)
2608 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2609 add_loc_descr (&head, tmp);
2612 else
2613 head = new_reg_loc_descr (cfa->reg, offset);
2615 return head;
2618 /* This function builds a dwarf location descriptor sequence for
2619 the address at OFFSET from the CFA when stack is aligned to
2620 ALIGNMENT byte. */
2622 struct dw_loc_descr_node *
2623 build_cfa_aligned_loc (dw_cfa_location *cfa,
2624 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2626 struct dw_loc_descr_node *head;
2627 unsigned int dwarf_fp
2628 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2630 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2631 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2633 head = new_reg_loc_descr (dwarf_fp, 0);
2634 add_loc_descr (&head, int_loc_descriptor (alignment));
2635 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2636 loc_descr_plus_const (&head, offset);
2638 else
2639 head = new_reg_loc_descr (dwarf_fp, offset);
2640 return head;
2643 /* And now, the support for symbolic debugging information. */
2645 /* .debug_str support. */
2647 static void dwarf2out_init (const char *);
2648 static void dwarf2out_finish (const char *);
2649 static void dwarf2out_early_finish (const char *);
2650 static void dwarf2out_assembly_start (void);
2651 static void dwarf2out_define (unsigned int, const char *);
2652 static void dwarf2out_undef (unsigned int, const char *);
2653 static void dwarf2out_start_source_file (unsigned, const char *);
2654 static void dwarf2out_end_source_file (unsigned);
2655 static void dwarf2out_function_decl (tree);
2656 static void dwarf2out_begin_block (unsigned, unsigned);
2657 static void dwarf2out_end_block (unsigned, unsigned);
2658 static bool dwarf2out_ignore_block (const_tree);
2659 static void dwarf2out_early_global_decl (tree);
2660 static void dwarf2out_late_global_decl (tree);
2661 static void dwarf2out_type_decl (tree, int);
2662 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2663 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2664 dw_die_ref);
2665 static void dwarf2out_abstract_function (tree);
2666 static void dwarf2out_var_location (rtx_insn *);
2667 static void dwarf2out_size_function (tree);
2668 static void dwarf2out_begin_function (tree);
2669 static void dwarf2out_end_function (unsigned int);
2670 static void dwarf2out_register_main_translation_unit (tree unit);
2671 static void dwarf2out_set_name (tree, tree);
2673 /* The debug hooks structure. */
2675 const struct gcc_debug_hooks dwarf2_debug_hooks =
2677 dwarf2out_init,
2678 dwarf2out_finish,
2679 dwarf2out_early_finish,
2680 dwarf2out_assembly_start,
2681 dwarf2out_define,
2682 dwarf2out_undef,
2683 dwarf2out_start_source_file,
2684 dwarf2out_end_source_file,
2685 dwarf2out_begin_block,
2686 dwarf2out_end_block,
2687 dwarf2out_ignore_block,
2688 dwarf2out_source_line,
2689 dwarf2out_begin_prologue,
2690 #if VMS_DEBUGGING_INFO
2691 dwarf2out_vms_end_prologue,
2692 dwarf2out_vms_begin_epilogue,
2693 #else
2694 debug_nothing_int_charstar,
2695 debug_nothing_int_charstar,
2696 #endif
2697 dwarf2out_end_epilogue,
2698 dwarf2out_begin_function,
2699 dwarf2out_end_function, /* end_function */
2700 dwarf2out_register_main_translation_unit,
2701 dwarf2out_function_decl, /* function_decl */
2702 dwarf2out_early_global_decl,
2703 dwarf2out_late_global_decl,
2704 dwarf2out_type_decl, /* type_decl */
2705 dwarf2out_imported_module_or_decl,
2706 debug_nothing_tree, /* deferred_inline_function */
2707 /* The DWARF 2 backend tries to reduce debugging bloat by not
2708 emitting the abstract description of inline functions until
2709 something tries to reference them. */
2710 dwarf2out_abstract_function, /* outlining_inline_function */
2711 debug_nothing_rtx_code_label, /* label */
2712 debug_nothing_int, /* handle_pch */
2713 dwarf2out_var_location,
2714 dwarf2out_size_function, /* size_function */
2715 dwarf2out_switch_text_section,
2716 dwarf2out_set_name,
2717 1, /* start_end_main_source_file */
2718 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2721 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2723 dwarf2out_init,
2724 debug_nothing_charstar,
2725 debug_nothing_charstar,
2726 dwarf2out_assembly_start,
2727 debug_nothing_int_charstar,
2728 debug_nothing_int_charstar,
2729 debug_nothing_int_charstar,
2730 debug_nothing_int,
2731 debug_nothing_int_int, /* begin_block */
2732 debug_nothing_int_int, /* end_block */
2733 debug_true_const_tree, /* ignore_block */
2734 dwarf2out_source_line, /* source_line */
2735 debug_nothing_int_charstar, /* begin_prologue */
2736 debug_nothing_int_charstar, /* end_prologue */
2737 debug_nothing_int_charstar, /* begin_epilogue */
2738 debug_nothing_int_charstar, /* end_epilogue */
2739 debug_nothing_tree, /* begin_function */
2740 debug_nothing_int, /* end_function */
2741 debug_nothing_tree, /* register_main_translation_unit */
2742 debug_nothing_tree, /* function_decl */
2743 debug_nothing_tree, /* early_global_decl */
2744 debug_nothing_tree, /* late_global_decl */
2745 debug_nothing_tree_int, /* type_decl */
2746 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2747 debug_nothing_tree, /* deferred_inline_function */
2748 debug_nothing_tree, /* outlining_inline_function */
2749 debug_nothing_rtx_code_label, /* label */
2750 debug_nothing_int, /* handle_pch */
2751 debug_nothing_rtx_insn, /* var_location */
2752 debug_nothing_tree, /* size_function */
2753 debug_nothing_void, /* switch_text_section */
2754 debug_nothing_tree_tree, /* set_name */
2755 0, /* start_end_main_source_file */
2756 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2759 /* NOTE: In the comments in this file, many references are made to
2760 "Debugging Information Entries". This term is abbreviated as `DIE'
2761 throughout the remainder of this file. */
2763 /* An internal representation of the DWARF output is built, and then
2764 walked to generate the DWARF debugging info. The walk of the internal
2765 representation is done after the entire program has been compiled.
2766 The types below are used to describe the internal representation. */
2768 /* Whether to put type DIEs into their own section .debug_types instead
2769 of making them part of the .debug_info section. Only supported for
2770 Dwarf V4 or higher and the user didn't disable them through
2771 -fno-debug-types-section. It is more efficient to put them in a
2772 separate comdat sections since the linker will then be able to
2773 remove duplicates. But not all tools support .debug_types sections
2774 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2775 it is DW_UT_type unit type in .debug_info section. */
2777 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2779 /* Various DIE's use offsets relative to the beginning of the
2780 .debug_info section to refer to each other. */
2782 typedef long int dw_offset;
2784 struct comdat_type_node;
2786 /* The entries in the line_info table more-or-less mirror the opcodes
2787 that are used in the real dwarf line table. Arrays of these entries
2788 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2789 supported. */
2791 enum dw_line_info_opcode {
2792 /* Emit DW_LNE_set_address; the operand is the label index. */
2793 LI_set_address,
2795 /* Emit a row to the matrix with the given line. This may be done
2796 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2797 special opcodes. */
2798 LI_set_line,
2800 /* Emit a DW_LNS_set_file. */
2801 LI_set_file,
2803 /* Emit a DW_LNS_set_column. */
2804 LI_set_column,
2806 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2807 LI_negate_stmt,
2809 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2810 LI_set_prologue_end,
2811 LI_set_epilogue_begin,
2813 /* Emit a DW_LNE_set_discriminator. */
2814 LI_set_discriminator
2817 typedef struct GTY(()) dw_line_info_struct {
2818 enum dw_line_info_opcode opcode;
2819 unsigned int val;
2820 } dw_line_info_entry;
2823 struct GTY(()) dw_line_info_table {
2824 /* The label that marks the end of this section. */
2825 const char *end_label;
2827 /* The values for the last row of the matrix, as collected in the table.
2828 These are used to minimize the changes to the next row. */
2829 unsigned int file_num;
2830 unsigned int line_num;
2831 unsigned int column_num;
2832 int discrim_num;
2833 bool is_stmt;
2834 bool in_use;
2836 vec<dw_line_info_entry, va_gc> *entries;
2840 /* Each DIE attribute has a field specifying the attribute kind,
2841 a link to the next attribute in the chain, and an attribute value.
2842 Attributes are typically linked below the DIE they modify. */
2844 typedef struct GTY(()) dw_attr_struct {
2845 enum dwarf_attribute dw_attr;
2846 dw_val_node dw_attr_val;
2848 dw_attr_node;
2851 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2852 The children of each node form a circular list linked by
2853 die_sib. die_child points to the node *before* the "first" child node. */
2855 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2856 union die_symbol_or_type_node
2858 const char * GTY ((tag ("0"))) die_symbol;
2859 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2861 GTY ((desc ("%0.comdat_type_p"))) die_id;
2862 vec<dw_attr_node, va_gc> *die_attr;
2863 dw_die_ref die_parent;
2864 dw_die_ref die_child;
2865 dw_die_ref die_sib;
2866 dw_die_ref die_definition; /* ref from a specification to its definition */
2867 dw_offset die_offset;
2868 unsigned long die_abbrev;
2869 int die_mark;
2870 unsigned int decl_id;
2871 enum dwarf_tag die_tag;
2872 /* Die is used and must not be pruned as unused. */
2873 BOOL_BITFIELD die_perennial_p : 1;
2874 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2875 /* Whether this DIE was removed from the DIE tree, for example via
2876 prune_unused_types. We don't consider those present from the
2877 DIE lookup routines. */
2878 BOOL_BITFIELD removed : 1;
2879 /* Lots of spare bits. */
2881 die_node;
2883 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2884 static bool early_dwarf;
2885 static bool early_dwarf_finished;
2886 struct set_early_dwarf {
2887 bool saved;
2888 set_early_dwarf () : saved(early_dwarf)
2890 gcc_assert (! early_dwarf_finished);
2891 early_dwarf = true;
2893 ~set_early_dwarf () { early_dwarf = saved; }
2896 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2897 #define FOR_EACH_CHILD(die, c, expr) do { \
2898 c = die->die_child; \
2899 if (c) do { \
2900 c = c->die_sib; \
2901 expr; \
2902 } while (c != die->die_child); \
2903 } while (0)
2905 /* The pubname structure */
2907 typedef struct GTY(()) pubname_struct {
2908 dw_die_ref die;
2909 const char *name;
2911 pubname_entry;
2914 struct GTY(()) dw_ranges {
2915 const char *label;
2916 /* If this is positive, it's a block number, otherwise it's a
2917 bitwise-negated index into dw_ranges_by_label. */
2918 int num;
2919 /* Index for the range list for DW_FORM_rnglistx. */
2920 unsigned int idx : 31;
2921 /* True if this range might be possibly in a different section
2922 from previous entry. */
2923 unsigned int maybe_new_sec : 1;
2926 /* A structure to hold a macinfo entry. */
2928 typedef struct GTY(()) macinfo_struct {
2929 unsigned char code;
2930 unsigned HOST_WIDE_INT lineno;
2931 const char *info;
2933 macinfo_entry;
2936 struct GTY(()) dw_ranges_by_label {
2937 const char *begin;
2938 const char *end;
2941 /* The comdat type node structure. */
2942 struct GTY(()) comdat_type_node
2944 dw_die_ref root_die;
2945 dw_die_ref type_die;
2946 dw_die_ref skeleton_die;
2947 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2948 comdat_type_node *next;
2951 /* A list of DIEs for which we can't determine ancestry (parent_die
2952 field) just yet. Later in dwarf2out_finish we will fill in the
2953 missing bits. */
2954 typedef struct GTY(()) limbo_die_struct {
2955 dw_die_ref die;
2956 /* The tree for which this DIE was created. We use this to
2957 determine ancestry later. */
2958 tree created_for;
2959 struct limbo_die_struct *next;
2961 limbo_die_node;
2963 typedef struct skeleton_chain_struct
2965 dw_die_ref old_die;
2966 dw_die_ref new_die;
2967 struct skeleton_chain_struct *parent;
2969 skeleton_chain_node;
2971 /* Define a macro which returns nonzero for a TYPE_DECL which was
2972 implicitly generated for a type.
2974 Note that, unlike the C front-end (which generates a NULL named
2975 TYPE_DECL node for each complete tagged type, each array type,
2976 and each function type node created) the C++ front-end generates
2977 a _named_ TYPE_DECL node for each tagged type node created.
2978 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2979 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2980 front-end, but for each type, tagged or not. */
2982 #define TYPE_DECL_IS_STUB(decl) \
2983 (DECL_NAME (decl) == NULL_TREE \
2984 || (DECL_ARTIFICIAL (decl) \
2985 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2986 /* This is necessary for stub decls that \
2987 appear in nested inline functions. */ \
2988 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2989 && (decl_ultimate_origin (decl) \
2990 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2992 /* Information concerning the compilation unit's programming
2993 language, and compiler version. */
2995 /* Fixed size portion of the DWARF compilation unit header. */
2996 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2997 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
2998 + (dwarf_version >= 5 \
2999 ? 4 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE : 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_version >= 5 \
3005 ? 0 : DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE))
3007 /* Fixed size portion of public names info. */
3008 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3010 /* Fixed size portion of the address range info. */
3011 #define DWARF_ARANGES_HEADER_SIZE \
3012 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3013 DWARF2_ADDR_SIZE * 2) \
3014 - DWARF_INITIAL_LENGTH_SIZE)
3016 /* Size of padding portion in the address range info. It must be
3017 aligned to twice the pointer size. */
3018 #define DWARF_ARANGES_PAD_SIZE \
3019 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3020 DWARF2_ADDR_SIZE * 2) \
3021 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3023 /* Use assembler line directives if available. */
3024 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3025 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3026 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3027 #else
3028 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3029 #endif
3030 #endif
3032 /* Minimum line offset in a special line info. opcode.
3033 This value was chosen to give a reasonable range of values. */
3034 #define DWARF_LINE_BASE -10
3036 /* First special line opcode - leave room for the standard opcodes. */
3037 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3039 /* Range of line offsets in a special line info. opcode. */
3040 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3042 /* Flag that indicates the initial value of the is_stmt_start flag.
3043 In the present implementation, we do not mark any lines as
3044 the beginning of a source statement, because that information
3045 is not made available by the GCC front-end. */
3046 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3048 /* Maximum number of operations per instruction bundle. */
3049 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3050 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3051 #endif
3053 /* This location is used by calc_die_sizes() to keep track
3054 the offset of each DIE within the .debug_info section. */
3055 static unsigned long next_die_offset;
3057 /* Record the root of the DIE's built for the current compilation unit. */
3058 static GTY(()) dw_die_ref single_comp_unit_die;
3060 /* A list of type DIEs that have been separated into comdat sections. */
3061 static GTY(()) comdat_type_node *comdat_type_list;
3063 /* A list of CU DIEs that have been separated. */
3064 static GTY(()) limbo_die_node *cu_die_list;
3066 /* A list of DIEs with a NULL parent waiting to be relocated. */
3067 static GTY(()) limbo_die_node *limbo_die_list;
3069 /* A list of DIEs for which we may have to generate
3070 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3071 static GTY(()) limbo_die_node *deferred_asm_name;
3073 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3075 typedef const char *compare_type;
3077 static hashval_t hash (dwarf_file_data *);
3078 static bool equal (dwarf_file_data *, const char *);
3081 /* Filenames referenced by this compilation unit. */
3082 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3084 struct decl_die_hasher : ggc_ptr_hash<die_node>
3086 typedef tree compare_type;
3088 static hashval_t hash (die_node *);
3089 static bool equal (die_node *, tree);
3091 /* A hash table of references to DIE's that describe declarations.
3092 The key is a DECL_UID() which is a unique number identifying each decl. */
3093 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3095 struct block_die_hasher : ggc_ptr_hash<die_struct>
3097 static hashval_t hash (die_struct *);
3098 static bool equal (die_struct *, die_struct *);
3101 /* A hash table of references to DIE's that describe COMMON blocks.
3102 The key is DECL_UID() ^ die_parent. */
3103 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3105 typedef struct GTY(()) die_arg_entry_struct {
3106 dw_die_ref die;
3107 tree arg;
3108 } die_arg_entry;
3111 /* Node of the variable location list. */
3112 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3113 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3114 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3115 in mode of the EXPR_LIST node and first EXPR_LIST operand
3116 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3117 location or NULL for padding. For larger bitsizes,
3118 mode is 0 and first operand is a CONCAT with bitsize
3119 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3120 NULL as second operand. */
3121 rtx GTY (()) loc;
3122 const char * GTY (()) label;
3123 struct var_loc_node * GTY (()) next;
3126 /* Variable location list. */
3127 struct GTY ((for_user)) var_loc_list_def {
3128 struct var_loc_node * GTY (()) first;
3130 /* Pointer to the last but one or last element of the
3131 chained list. If the list is empty, both first and
3132 last are NULL, if the list contains just one node
3133 or the last node certainly is not redundant, it points
3134 to the last node, otherwise points to the last but one.
3135 Do not mark it for GC because it is marked through the chain. */
3136 struct var_loc_node * GTY ((skip ("%h"))) last;
3138 /* Pointer to the last element before section switch,
3139 if NULL, either sections weren't switched or first
3140 is after section switch. */
3141 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3143 /* DECL_UID of the variable decl. */
3144 unsigned int decl_id;
3146 typedef struct var_loc_list_def var_loc_list;
3148 /* Call argument location list. */
3149 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3150 rtx GTY (()) call_arg_loc_note;
3151 const char * GTY (()) label;
3152 tree GTY (()) block;
3153 bool tail_call_p;
3154 rtx GTY (()) symbol_ref;
3155 struct call_arg_loc_node * GTY (()) next;
3159 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3161 typedef const_tree compare_type;
3163 static hashval_t hash (var_loc_list *);
3164 static bool equal (var_loc_list *, const_tree);
3167 /* Table of decl location linked lists. */
3168 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3170 /* Head and tail of call_arg_loc chain. */
3171 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3172 static struct call_arg_loc_node *call_arg_loc_last;
3174 /* Number of call sites in the current function. */
3175 static int call_site_count = -1;
3176 /* Number of tail call sites in the current function. */
3177 static int tail_call_site_count = -1;
3179 /* A cached location list. */
3180 struct GTY ((for_user)) cached_dw_loc_list_def {
3181 /* The DECL_UID of the decl that this entry describes. */
3182 unsigned int decl_id;
3184 /* The cached location list. */
3185 dw_loc_list_ref loc_list;
3187 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3189 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3192 typedef const_tree compare_type;
3194 static hashval_t hash (cached_dw_loc_list *);
3195 static bool equal (cached_dw_loc_list *, const_tree);
3198 /* Table of cached location lists. */
3199 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3201 /* A vector of references to DIE's that are uniquely identified by their tag,
3202 presence/absence of children DIE's, and list of attribute/value pairs. */
3203 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3205 /* A hash map to remember the stack usage for DWARF procedures. The value
3206 stored is the stack size difference between before the DWARF procedure
3207 invokation and after it returned. In other words, for a DWARF procedure
3208 that consumes N stack slots and that pushes M ones, this stores M - N. */
3209 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3211 /* A global counter for generating labels for line number data. */
3212 static unsigned int line_info_label_num;
3214 /* The current table to which we should emit line number information
3215 for the current function. This will be set up at the beginning of
3216 assembly for the function. */
3217 static GTY(()) dw_line_info_table *cur_line_info_table;
3219 /* The two default tables of line number info. */
3220 static GTY(()) dw_line_info_table *text_section_line_info;
3221 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3223 /* The set of all non-default tables of line number info. */
3224 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3226 /* A flag to tell pubnames/types export if there is an info section to
3227 refer to. */
3228 static bool info_section_emitted;
3230 /* A pointer to the base of a table that contains a list of publicly
3231 accessible names. */
3232 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3234 /* A pointer to the base of a table that contains a list of publicly
3235 accessible types. */
3236 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3238 /* A pointer to the base of a table that contains a list of macro
3239 defines/undefines (and file start/end markers). */
3240 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3242 /* True if .debug_macinfo or .debug_macros section is going to be
3243 emitted. */
3244 #define have_macinfo \
3245 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3246 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3247 && !macinfo_table->is_empty ())
3249 /* Vector of dies for which we should generate .debug_ranges info. */
3250 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3252 /* Vector of pairs of labels referenced in ranges_table. */
3253 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3255 /* Whether we have location lists that need outputting */
3256 static GTY(()) bool have_location_lists;
3258 /* Unique label counter. */
3259 static GTY(()) unsigned int loclabel_num;
3261 /* Unique label counter for point-of-call tables. */
3262 static GTY(()) unsigned int poc_label_num;
3264 /* The last file entry emitted by maybe_emit_file(). */
3265 static GTY(()) struct dwarf_file_data * last_emitted_file;
3267 /* Number of internal labels generated by gen_internal_sym(). */
3268 static GTY(()) int label_num;
3270 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3272 /* Instances of generic types for which we need to generate debug
3273 info that describe their generic parameters and arguments. That
3274 generation needs to happen once all types are properly laid out so
3275 we do it at the end of compilation. */
3276 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3278 /* Offset from the "steady-state frame pointer" to the frame base,
3279 within the current function. */
3280 static HOST_WIDE_INT frame_pointer_fb_offset;
3281 static bool frame_pointer_fb_offset_valid;
3283 static vec<dw_die_ref> base_types;
3285 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3286 once all arguments are parsed. */
3287 static vec<dw_die_ref> *string_types;
3289 /* Flags to represent a set of attribute classes for attributes that represent
3290 a scalar value (bounds, pointers, ...). */
3291 enum dw_scalar_form
3293 dw_scalar_form_constant = 0x01,
3294 dw_scalar_form_exprloc = 0x02,
3295 dw_scalar_form_reference = 0x04
3298 /* Forward declarations for functions defined in this file. */
3300 static int is_pseudo_reg (const_rtx);
3301 static tree type_main_variant (tree);
3302 static int is_tagged_type (const_tree);
3303 static const char *dwarf_tag_name (unsigned);
3304 static const char *dwarf_attr_name (unsigned);
3305 static const char *dwarf_form_name (unsigned);
3306 static tree decl_ultimate_origin (const_tree);
3307 static tree decl_class_context (tree);
3308 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3309 static inline enum dw_val_class AT_class (dw_attr_node *);
3310 static inline unsigned int AT_index (dw_attr_node *);
3311 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3312 static inline unsigned AT_flag (dw_attr_node *);
3313 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3314 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3315 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3316 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3317 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3318 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3319 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3320 unsigned int, unsigned char *);
3321 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3322 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3323 static inline const char *AT_string (dw_attr_node *);
3324 static enum dwarf_form AT_string_form (dw_attr_node *);
3325 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3326 static void add_AT_specification (dw_die_ref, dw_die_ref);
3327 static inline dw_die_ref AT_ref (dw_attr_node *);
3328 static inline int AT_ref_external (dw_attr_node *);
3329 static inline void set_AT_ref_external (dw_attr_node *, int);
3330 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3331 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3332 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3333 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3334 dw_loc_list_ref);
3335 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3336 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3337 static void remove_addr_table_entry (addr_table_entry *);
3338 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3339 static inline rtx AT_addr (dw_attr_node *);
3340 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3341 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3342 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3343 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3344 const char *);
3345 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3346 unsigned HOST_WIDE_INT);
3347 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3348 unsigned long, bool);
3349 static inline const char *AT_lbl (dw_attr_node *);
3350 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3351 static const char *get_AT_low_pc (dw_die_ref);
3352 static const char *get_AT_hi_pc (dw_die_ref);
3353 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3354 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3355 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3356 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3357 static bool is_cxx (void);
3358 static bool is_fortran (void);
3359 static bool is_ada (void);
3360 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3361 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3362 static void add_child_die (dw_die_ref, dw_die_ref);
3363 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3364 static dw_die_ref lookup_type_die (tree);
3365 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3366 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3367 static void equate_type_number_to_die (tree, dw_die_ref);
3368 static dw_die_ref lookup_decl_die (tree);
3369 static var_loc_list *lookup_decl_loc (const_tree);
3370 static void equate_decl_number_to_die (tree, dw_die_ref);
3371 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3372 static void print_spaces (FILE *);
3373 static void print_die (dw_die_ref, FILE *);
3374 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3375 static dw_die_ref pop_compile_unit (dw_die_ref);
3376 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3377 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3378 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3379 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3380 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3381 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3382 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3383 struct md5_ctx *, int *);
3384 struct checksum_attributes;
3385 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3386 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3387 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3388 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3389 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3390 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3391 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3392 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3393 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3394 static void compute_section_prefix (dw_die_ref);
3395 static int is_type_die (dw_die_ref);
3396 static int is_comdat_die (dw_die_ref);
3397 static int is_symbol_die (dw_die_ref);
3398 static inline bool is_template_instantiation (dw_die_ref);
3399 static void assign_symbol_names (dw_die_ref);
3400 static void break_out_includes (dw_die_ref);
3401 static int is_declaration_die (dw_die_ref);
3402 static int should_move_die_to_comdat (dw_die_ref);
3403 static dw_die_ref clone_as_declaration (dw_die_ref);
3404 static dw_die_ref clone_die (dw_die_ref);
3405 static dw_die_ref clone_tree (dw_die_ref);
3406 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3407 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3408 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3409 static dw_die_ref generate_skeleton (dw_die_ref);
3410 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3411 dw_die_ref,
3412 dw_die_ref);
3413 static void break_out_comdat_types (dw_die_ref);
3414 static void copy_decls_for_unworthy_types (dw_die_ref);
3416 static void add_sibling_attributes (dw_die_ref);
3417 static void output_location_lists (dw_die_ref);
3418 static int constant_size (unsigned HOST_WIDE_INT);
3419 static unsigned long size_of_die (dw_die_ref);
3420 static void calc_die_sizes (dw_die_ref);
3421 static void calc_base_type_die_sizes (void);
3422 static void mark_dies (dw_die_ref);
3423 static void unmark_dies (dw_die_ref);
3424 static void unmark_all_dies (dw_die_ref);
3425 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3426 static unsigned long size_of_aranges (void);
3427 static enum dwarf_form value_format (dw_attr_node *);
3428 static void output_value_format (dw_attr_node *);
3429 static void output_abbrev_section (void);
3430 static void output_die_abbrevs (unsigned long, dw_die_ref);
3431 static void output_die_symbol (dw_die_ref);
3432 static void output_die (dw_die_ref);
3433 static void output_compilation_unit_header (enum dwarf_unit_type);
3434 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3435 static void output_comdat_type_unit (comdat_type_node *);
3436 static const char *dwarf2_name (tree, int);
3437 static void add_pubname (tree, dw_die_ref);
3438 static void add_enumerator_pubname (const char *, dw_die_ref);
3439 static void add_pubname_string (const char *, dw_die_ref);
3440 static void add_pubtype (tree, dw_die_ref);
3441 static void output_pubnames (vec<pubname_entry, va_gc> *);
3442 static void output_aranges (void);
3443 static unsigned int add_ranges (const_tree, bool = false);
3444 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3445 bool *, bool);
3446 static void output_ranges (void);
3447 static dw_line_info_table *new_line_info_table (void);
3448 static void output_line_info (bool);
3449 static void output_file_names (void);
3450 static dw_die_ref base_type_die (tree, bool);
3451 static int is_base_type (tree);
3452 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3453 static int decl_quals (const_tree);
3454 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3455 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3456 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3457 static int type_is_enum (const_tree);
3458 static unsigned int dbx_reg_number (const_rtx);
3459 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3460 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3461 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3462 enum var_init_status);
3463 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3464 enum var_init_status);
3465 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3466 enum var_init_status);
3467 static int is_based_loc (const_rtx);
3468 static bool resolve_one_addr (rtx *);
3469 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3470 enum var_init_status);
3471 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3472 enum var_init_status);
3473 struct loc_descr_context;
3474 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3475 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3476 static dw_loc_list_ref loc_list_from_tree (tree, int,
3477 struct loc_descr_context *);
3478 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3479 struct loc_descr_context *);
3480 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3481 static tree field_type (const_tree);
3482 static unsigned int simple_type_align_in_bits (const_tree);
3483 static unsigned int simple_decl_align_in_bits (const_tree);
3484 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3485 struct vlr_context;
3486 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3487 HOST_WIDE_INT *);
3488 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3489 dw_loc_list_ref);
3490 static void add_data_member_location_attribute (dw_die_ref, tree,
3491 struct vlr_context *);
3492 static bool add_const_value_attribute (dw_die_ref, rtx);
3493 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3494 static void insert_wide_int (const wide_int &, unsigned char *, int);
3495 static void insert_float (const_rtx, unsigned char *);
3496 static rtx rtl_for_decl_location (tree);
3497 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3498 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3499 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3500 static void add_name_attribute (dw_die_ref, const char *);
3501 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3502 static void add_comp_dir_attribute (dw_die_ref);
3503 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3504 struct loc_descr_context *);
3505 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3506 struct loc_descr_context *);
3507 static void add_subscript_info (dw_die_ref, tree, bool);
3508 static void add_byte_size_attribute (dw_die_ref, tree);
3509 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3510 struct vlr_context *);
3511 static void add_bit_size_attribute (dw_die_ref, tree);
3512 static void add_prototyped_attribute (dw_die_ref, tree);
3513 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3514 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3515 static void add_src_coords_attributes (dw_die_ref, tree);
3516 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3517 static void add_discr_value (dw_die_ref, dw_discr_value *);
3518 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3519 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3520 static void push_decl_scope (tree);
3521 static void pop_decl_scope (void);
3522 static dw_die_ref scope_die_for (tree, dw_die_ref);
3523 static inline int local_scope_p (dw_die_ref);
3524 static inline int class_scope_p (dw_die_ref);
3525 static inline int class_or_namespace_scope_p (dw_die_ref);
3526 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3527 static void add_calling_convention_attribute (dw_die_ref, tree);
3528 static const char *type_tag (const_tree);
3529 static tree member_declared_type (const_tree);
3530 #if 0
3531 static const char *decl_start_label (tree);
3532 #endif
3533 static void gen_array_type_die (tree, dw_die_ref);
3534 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3535 #if 0
3536 static void gen_entry_point_die (tree, dw_die_ref);
3537 #endif
3538 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3539 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3540 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3541 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3542 static void gen_formal_types_die (tree, dw_die_ref);
3543 static void gen_subprogram_die (tree, dw_die_ref);
3544 static void gen_variable_die (tree, tree, dw_die_ref);
3545 static void gen_const_die (tree, dw_die_ref);
3546 static void gen_label_die (tree, dw_die_ref);
3547 static void gen_lexical_block_die (tree, dw_die_ref);
3548 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3549 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3550 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3551 static dw_die_ref gen_compile_unit_die (const char *);
3552 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3553 static void gen_member_die (tree, dw_die_ref);
3554 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3555 enum debug_info_usage);
3556 static void gen_subroutine_type_die (tree, dw_die_ref);
3557 static void gen_typedef_die (tree, dw_die_ref);
3558 static void gen_type_die (tree, dw_die_ref);
3559 static void gen_block_die (tree, dw_die_ref);
3560 static void decls_for_scope (tree, dw_die_ref);
3561 static bool is_naming_typedef_decl (const_tree);
3562 static inline dw_die_ref get_context_die (tree);
3563 static void gen_namespace_die (tree, dw_die_ref);
3564 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3565 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3566 static dw_die_ref force_decl_die (tree);
3567 static dw_die_ref force_type_die (tree);
3568 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3569 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3570 static struct dwarf_file_data * lookup_filename (const char *);
3571 static void retry_incomplete_types (void);
3572 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3573 static void gen_generic_params_dies (tree);
3574 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3575 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3576 static void splice_child_die (dw_die_ref, dw_die_ref);
3577 static int file_info_cmp (const void *, const void *);
3578 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3579 const char *, const char *);
3580 static void output_loc_list (dw_loc_list_ref);
3581 static char *gen_internal_sym (const char *);
3582 static bool want_pubnames (void);
3584 static void prune_unmark_dies (dw_die_ref);
3585 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3586 static void prune_unused_types_mark (dw_die_ref, int);
3587 static void prune_unused_types_walk (dw_die_ref);
3588 static void prune_unused_types_walk_attribs (dw_die_ref);
3589 static void prune_unused_types_prune (dw_die_ref);
3590 static void prune_unused_types (void);
3591 static int maybe_emit_file (struct dwarf_file_data *fd);
3592 static inline const char *AT_vms_delta1 (dw_attr_node *);
3593 static inline const char *AT_vms_delta2 (dw_attr_node *);
3594 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3595 const char *, const char *);
3596 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3597 static void gen_remaining_tmpl_value_param_die_attribute (void);
3598 static bool generic_type_p (tree);
3599 static void schedule_generic_params_dies_gen (tree t);
3600 static void gen_scheduled_generic_parms_dies (void);
3602 static const char *comp_dir_string (void);
3604 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3606 /* enum for tracking thread-local variables whose address is really an offset
3607 relative to the TLS pointer, which will need link-time relocation, but will
3608 not need relocation by the DWARF consumer. */
3610 enum dtprel_bool
3612 dtprel_false = 0,
3613 dtprel_true = 1
3616 /* Return the operator to use for an address of a variable. For dtprel_true, we
3617 use DW_OP_const*. For regular variables, which need both link-time
3618 relocation and consumer-level relocation (e.g., to account for shared objects
3619 loaded at a random address), we use DW_OP_addr*. */
3621 static inline enum dwarf_location_atom
3622 dw_addr_op (enum dtprel_bool dtprel)
3624 if (dtprel == dtprel_true)
3625 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3626 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3627 else
3628 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3631 /* Return a pointer to a newly allocated address location description. If
3632 dwarf_split_debug_info is true, then record the address with the appropriate
3633 relocation. */
3634 static inline dw_loc_descr_ref
3635 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3637 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3639 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3640 ref->dw_loc_oprnd1.v.val_addr = addr;
3641 ref->dtprel = dtprel;
3642 if (dwarf_split_debug_info)
3643 ref->dw_loc_oprnd1.val_entry
3644 = add_addr_table_entry (addr,
3645 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3646 else
3647 ref->dw_loc_oprnd1.val_entry = NULL;
3649 return ref;
3652 /* Section names used to hold DWARF debugging information. */
3654 #ifndef DEBUG_INFO_SECTION
3655 #define DEBUG_INFO_SECTION ".debug_info"
3656 #endif
3657 #ifndef DEBUG_DWO_INFO_SECTION
3658 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3659 #endif
3660 #ifndef DEBUG_ABBREV_SECTION
3661 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3662 #endif
3663 #ifndef DEBUG_DWO_ABBREV_SECTION
3664 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3665 #endif
3666 #ifndef DEBUG_ARANGES_SECTION
3667 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3668 #endif
3669 #ifndef DEBUG_ADDR_SECTION
3670 #define DEBUG_ADDR_SECTION ".debug_addr"
3671 #endif
3672 #ifndef DEBUG_MACINFO_SECTION
3673 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3674 #endif
3675 #ifndef DEBUG_DWO_MACINFO_SECTION
3676 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3677 #endif
3678 #ifndef DEBUG_DWO_MACRO_SECTION
3679 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3680 #endif
3681 #ifndef DEBUG_MACRO_SECTION
3682 #define DEBUG_MACRO_SECTION ".debug_macro"
3683 #endif
3684 #ifndef DEBUG_LINE_SECTION
3685 #define DEBUG_LINE_SECTION ".debug_line"
3686 #endif
3687 #ifndef DEBUG_DWO_LINE_SECTION
3688 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3689 #endif
3690 #ifndef DEBUG_LOC_SECTION
3691 #define DEBUG_LOC_SECTION ".debug_loc"
3692 #endif
3693 #ifndef DEBUG_DWO_LOC_SECTION
3694 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3695 #endif
3696 #ifndef DEBUG_LOCLISTS_SECTION
3697 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3698 #endif
3699 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3700 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3701 #endif
3702 #ifndef DEBUG_PUBNAMES_SECTION
3703 #define DEBUG_PUBNAMES_SECTION \
3704 ((debug_generate_pub_sections == 2) \
3705 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3706 #endif
3707 #ifndef DEBUG_PUBTYPES_SECTION
3708 #define DEBUG_PUBTYPES_SECTION \
3709 ((debug_generate_pub_sections == 2) \
3710 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3711 #endif
3712 #ifndef DEBUG_STR_OFFSETS_SECTION
3713 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3714 #endif
3715 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3716 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3717 #endif
3718 #ifndef DEBUG_STR_DWO_SECTION
3719 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3720 #endif
3721 #ifndef DEBUG_STR_SECTION
3722 #define DEBUG_STR_SECTION ".debug_str"
3723 #endif
3724 #ifndef DEBUG_RANGES_SECTION
3725 #define DEBUG_RANGES_SECTION ".debug_ranges"
3726 #endif
3727 #ifndef DEBUG_RNGLISTS_SECTION
3728 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3729 #endif
3730 #ifndef DEBUG_LINE_STR_SECTION
3731 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3732 #endif
3734 /* Standard ELF section names for compiled code and data. */
3735 #ifndef TEXT_SECTION_NAME
3736 #define TEXT_SECTION_NAME ".text"
3737 #endif
3739 /* Section flags for .debug_str section. */
3740 #define DEBUG_STR_SECTION_FLAGS \
3741 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3742 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3743 : SECTION_DEBUG)
3745 /* Section flags for .debug_str.dwo section. */
3746 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3748 /* Labels we insert at beginning sections we can reference instead of
3749 the section names themselves. */
3751 #ifndef TEXT_SECTION_LABEL
3752 #define TEXT_SECTION_LABEL "Ltext"
3753 #endif
3754 #ifndef COLD_TEXT_SECTION_LABEL
3755 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3756 #endif
3757 #ifndef DEBUG_LINE_SECTION_LABEL
3758 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3759 #endif
3760 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3761 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3762 #endif
3763 #ifndef DEBUG_INFO_SECTION_LABEL
3764 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3765 #endif
3766 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3767 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3768 #endif
3769 #ifndef DEBUG_ABBREV_SECTION_LABEL
3770 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3771 #endif
3772 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3773 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3774 #endif
3775 #ifndef DEBUG_ADDR_SECTION_LABEL
3776 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3777 #endif
3778 #ifndef DEBUG_LOC_SECTION_LABEL
3779 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3780 #endif
3781 #ifndef DEBUG_RANGES_SECTION_LABEL
3782 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3783 #endif
3784 #ifndef DEBUG_MACINFO_SECTION_LABEL
3785 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3786 #endif
3787 #ifndef DEBUG_MACRO_SECTION_LABEL
3788 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3789 #endif
3790 #define SKELETON_COMP_DIE_ABBREV 1
3791 #define SKELETON_TYPE_DIE_ABBREV 2
3793 /* Definitions of defaults for formats and names of various special
3794 (artificial) labels which may be generated within this file (when the -g
3795 options is used and DWARF2_DEBUGGING_INFO is in effect.
3796 If necessary, these may be overridden from within the tm.h file, but
3797 typically, overriding these defaults is unnecessary. */
3799 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3800 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3801 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3802 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3803 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3804 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3805 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3806 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3807 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3808 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3809 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3810 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3811 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3812 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3813 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3815 #ifndef TEXT_END_LABEL
3816 #define TEXT_END_LABEL "Letext"
3817 #endif
3818 #ifndef COLD_END_LABEL
3819 #define COLD_END_LABEL "Letext_cold"
3820 #endif
3821 #ifndef BLOCK_BEGIN_LABEL
3822 #define BLOCK_BEGIN_LABEL "LBB"
3823 #endif
3824 #ifndef BLOCK_END_LABEL
3825 #define BLOCK_END_LABEL "LBE"
3826 #endif
3827 #ifndef LINE_CODE_LABEL
3828 #define LINE_CODE_LABEL "LM"
3829 #endif
3832 /* Return the root of the DIE's built for the current compilation unit. */
3833 static dw_die_ref
3834 comp_unit_die (void)
3836 if (!single_comp_unit_die)
3837 single_comp_unit_die = gen_compile_unit_die (NULL);
3838 return single_comp_unit_die;
3841 /* We allow a language front-end to designate a function that is to be
3842 called to "demangle" any name before it is put into a DIE. */
3844 static const char *(*demangle_name_func) (const char *);
3846 void
3847 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3849 demangle_name_func = func;
3852 /* Test if rtl node points to a pseudo register. */
3854 static inline int
3855 is_pseudo_reg (const_rtx rtl)
3857 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3858 || (GET_CODE (rtl) == SUBREG
3859 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3862 /* Return a reference to a type, with its const and volatile qualifiers
3863 removed. */
3865 static inline tree
3866 type_main_variant (tree type)
3868 type = TYPE_MAIN_VARIANT (type);
3870 /* ??? There really should be only one main variant among any group of
3871 variants of a given type (and all of the MAIN_VARIANT values for all
3872 members of the group should point to that one type) but sometimes the C
3873 front-end messes this up for array types, so we work around that bug
3874 here. */
3875 if (TREE_CODE (type) == ARRAY_TYPE)
3876 while (type != TYPE_MAIN_VARIANT (type))
3877 type = TYPE_MAIN_VARIANT (type);
3879 return type;
3882 /* Return nonzero if the given type node represents a tagged type. */
3884 static inline int
3885 is_tagged_type (const_tree type)
3887 enum tree_code code = TREE_CODE (type);
3889 return (code == RECORD_TYPE || code == UNION_TYPE
3890 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3893 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3895 static void
3896 get_ref_die_offset_label (char *label, dw_die_ref ref)
3898 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3901 /* Return die_offset of a DIE reference to a base type. */
3903 static unsigned long int
3904 get_base_type_offset (dw_die_ref ref)
3906 if (ref->die_offset)
3907 return ref->die_offset;
3908 if (comp_unit_die ()->die_abbrev)
3910 calc_base_type_die_sizes ();
3911 gcc_assert (ref->die_offset);
3913 return ref->die_offset;
3916 /* Return die_offset of a DIE reference other than base type. */
3918 static unsigned long int
3919 get_ref_die_offset (dw_die_ref ref)
3921 gcc_assert (ref->die_offset);
3922 return ref->die_offset;
3925 /* Convert a DIE tag into its string name. */
3927 static const char *
3928 dwarf_tag_name (unsigned int tag)
3930 const char *name = get_DW_TAG_name (tag);
3932 if (name != NULL)
3933 return name;
3935 return "DW_TAG_<unknown>";
3938 /* Convert a DWARF attribute code into its string name. */
3940 static const char *
3941 dwarf_attr_name (unsigned int attr)
3943 const char *name;
3945 switch (attr)
3947 #if VMS_DEBUGGING_INFO
3948 case DW_AT_HP_prologue:
3949 return "DW_AT_HP_prologue";
3950 #else
3951 case DW_AT_MIPS_loop_unroll_factor:
3952 return "DW_AT_MIPS_loop_unroll_factor";
3953 #endif
3955 #if VMS_DEBUGGING_INFO
3956 case DW_AT_HP_epilogue:
3957 return "DW_AT_HP_epilogue";
3958 #else
3959 case DW_AT_MIPS_stride:
3960 return "DW_AT_MIPS_stride";
3961 #endif
3964 name = get_DW_AT_name (attr);
3966 if (name != NULL)
3967 return name;
3969 return "DW_AT_<unknown>";
3972 /* Convert a DWARF value form code into its string name. */
3974 static const char *
3975 dwarf_form_name (unsigned int form)
3977 const char *name = get_DW_FORM_name (form);
3979 if (name != NULL)
3980 return name;
3982 return "DW_FORM_<unknown>";
3985 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3986 instance of an inlined instance of a decl which is local to an inline
3987 function, so we have to trace all of the way back through the origin chain
3988 to find out what sort of node actually served as the original seed for the
3989 given block. */
3991 static tree
3992 decl_ultimate_origin (const_tree decl)
3994 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3995 return NULL_TREE;
3997 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3998 we're trying to output the abstract instance of this function. */
3999 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4000 return NULL_TREE;
4002 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4003 most distant ancestor, this should never happen. */
4004 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4006 return DECL_ABSTRACT_ORIGIN (decl);
4009 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4010 of a virtual function may refer to a base class, so we check the 'this'
4011 parameter. */
4013 static tree
4014 decl_class_context (tree decl)
4016 tree context = NULL_TREE;
4018 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4019 context = DECL_CONTEXT (decl);
4020 else
4021 context = TYPE_MAIN_VARIANT
4022 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4024 if (context && !TYPE_P (context))
4025 context = NULL_TREE;
4027 return context;
4030 /* Add an attribute/value pair to a DIE. */
4032 static inline void
4033 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4035 /* Maybe this should be an assert? */
4036 if (die == NULL)
4037 return;
4039 vec_safe_reserve (die->die_attr, 1);
4040 vec_safe_push (die->die_attr, *attr);
4043 static inline enum dw_val_class
4044 AT_class (dw_attr_node *a)
4046 return a->dw_attr_val.val_class;
4049 /* Return the index for any attribute that will be referenced with a
4050 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4051 are stored in dw_attr_val.v.val_str for reference counting
4052 pruning. */
4054 static inline unsigned int
4055 AT_index (dw_attr_node *a)
4057 if (AT_class (a) == dw_val_class_str)
4058 return a->dw_attr_val.v.val_str->index;
4059 else if (a->dw_attr_val.val_entry != NULL)
4060 return a->dw_attr_val.val_entry->index;
4061 return NOT_INDEXED;
4064 /* Add a flag value attribute to a DIE. */
4066 static inline void
4067 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4069 dw_attr_node attr;
4071 attr.dw_attr = attr_kind;
4072 attr.dw_attr_val.val_class = dw_val_class_flag;
4073 attr.dw_attr_val.val_entry = NULL;
4074 attr.dw_attr_val.v.val_flag = flag;
4075 add_dwarf_attr (die, &attr);
4078 static inline unsigned
4079 AT_flag (dw_attr_node *a)
4081 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4082 return a->dw_attr_val.v.val_flag;
4085 /* Add a signed integer attribute value to a DIE. */
4087 static inline void
4088 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4090 dw_attr_node attr;
4092 attr.dw_attr = attr_kind;
4093 attr.dw_attr_val.val_class = dw_val_class_const;
4094 attr.dw_attr_val.val_entry = NULL;
4095 attr.dw_attr_val.v.val_int = int_val;
4096 add_dwarf_attr (die, &attr);
4099 static inline HOST_WIDE_INT
4100 AT_int (dw_attr_node *a)
4102 gcc_assert (a && (AT_class (a) == dw_val_class_const
4103 || AT_class (a) == dw_val_class_const_implicit));
4104 return a->dw_attr_val.v.val_int;
4107 /* Add an unsigned integer attribute value to a DIE. */
4109 static inline void
4110 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4111 unsigned HOST_WIDE_INT unsigned_val)
4113 dw_attr_node attr;
4115 attr.dw_attr = attr_kind;
4116 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4117 attr.dw_attr_val.val_entry = NULL;
4118 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4119 add_dwarf_attr (die, &attr);
4122 static inline unsigned HOST_WIDE_INT
4123 AT_unsigned (dw_attr_node *a)
4125 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4126 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4127 return a->dw_attr_val.v.val_unsigned;
4130 /* Add an unsigned wide integer attribute value to a DIE. */
4132 static inline void
4133 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4134 const wide_int& w)
4136 dw_attr_node attr;
4138 attr.dw_attr = attr_kind;
4139 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4140 attr.dw_attr_val.val_entry = NULL;
4141 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4142 *attr.dw_attr_val.v.val_wide = w;
4143 add_dwarf_attr (die, &attr);
4146 /* Add an unsigned double integer attribute value to a DIE. */
4148 static inline void
4149 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4150 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4152 dw_attr_node attr;
4154 attr.dw_attr = attr_kind;
4155 attr.dw_attr_val.val_class = dw_val_class_const_double;
4156 attr.dw_attr_val.val_entry = NULL;
4157 attr.dw_attr_val.v.val_double.high = high;
4158 attr.dw_attr_val.v.val_double.low = low;
4159 add_dwarf_attr (die, &attr);
4162 /* Add a floating point attribute value to a DIE and return it. */
4164 static inline void
4165 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4166 unsigned int length, unsigned int elt_size, unsigned char *array)
4168 dw_attr_node attr;
4170 attr.dw_attr = attr_kind;
4171 attr.dw_attr_val.val_class = dw_val_class_vec;
4172 attr.dw_attr_val.val_entry = NULL;
4173 attr.dw_attr_val.v.val_vec.length = length;
4174 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4175 attr.dw_attr_val.v.val_vec.array = array;
4176 add_dwarf_attr (die, &attr);
4179 /* Add an 8-byte data attribute value to a DIE. */
4181 static inline void
4182 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4183 unsigned char data8[8])
4185 dw_attr_node attr;
4187 attr.dw_attr = attr_kind;
4188 attr.dw_attr_val.val_class = dw_val_class_data8;
4189 attr.dw_attr_val.val_entry = NULL;
4190 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4191 add_dwarf_attr (die, &attr);
4194 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4195 dwarf_split_debug_info, address attributes in dies destined for the
4196 final executable have force_direct set to avoid using indexed
4197 references. */
4199 static inline void
4200 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4201 bool force_direct)
4203 dw_attr_node attr;
4204 char * lbl_id;
4206 lbl_id = xstrdup (lbl_low);
4207 attr.dw_attr = DW_AT_low_pc;
4208 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4209 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4210 if (dwarf_split_debug_info && !force_direct)
4211 attr.dw_attr_val.val_entry
4212 = add_addr_table_entry (lbl_id, ate_kind_label);
4213 else
4214 attr.dw_attr_val.val_entry = NULL;
4215 add_dwarf_attr (die, &attr);
4217 attr.dw_attr = DW_AT_high_pc;
4218 if (dwarf_version < 4)
4219 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4220 else
4221 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4222 lbl_id = xstrdup (lbl_high);
4223 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4224 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4225 && dwarf_split_debug_info && !force_direct)
4226 attr.dw_attr_val.val_entry
4227 = add_addr_table_entry (lbl_id, ate_kind_label);
4228 else
4229 attr.dw_attr_val.val_entry = NULL;
4230 add_dwarf_attr (die, &attr);
4233 /* Hash and equality functions for debug_str_hash. */
4235 hashval_t
4236 indirect_string_hasher::hash (indirect_string_node *x)
4238 return htab_hash_string (x->str);
4241 bool
4242 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4244 return strcmp (x1->str, x2) == 0;
4247 /* Add STR to the given string hash table. */
4249 static struct indirect_string_node *
4250 find_AT_string_in_table (const char *str,
4251 hash_table<indirect_string_hasher> *table)
4253 struct indirect_string_node *node;
4255 indirect_string_node **slot
4256 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4257 if (*slot == NULL)
4259 node = ggc_cleared_alloc<indirect_string_node> ();
4260 node->str = ggc_strdup (str);
4261 *slot = node;
4263 else
4264 node = *slot;
4266 node->refcount++;
4267 return node;
4270 /* Add STR to the indirect string hash table. */
4272 static struct indirect_string_node *
4273 find_AT_string (const char *str)
4275 if (! debug_str_hash)
4276 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4278 return find_AT_string_in_table (str, debug_str_hash);
4281 /* Add a string attribute value to a DIE. */
4283 static inline void
4284 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4286 dw_attr_node attr;
4287 struct indirect_string_node *node;
4289 node = find_AT_string (str);
4291 attr.dw_attr = attr_kind;
4292 attr.dw_attr_val.val_class = dw_val_class_str;
4293 attr.dw_attr_val.val_entry = NULL;
4294 attr.dw_attr_val.v.val_str = node;
4295 add_dwarf_attr (die, &attr);
4298 static inline const char *
4299 AT_string (dw_attr_node *a)
4301 gcc_assert (a && AT_class (a) == dw_val_class_str);
4302 return a->dw_attr_val.v.val_str->str;
4305 /* Call this function directly to bypass AT_string_form's logic to put
4306 the string inline in the die. */
4308 static void
4309 set_indirect_string (struct indirect_string_node *node)
4311 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4312 /* Already indirect is a no op. */
4313 if (node->form == DW_FORM_strp
4314 || node->form == DW_FORM_line_strp
4315 || node->form == DW_FORM_GNU_str_index)
4317 gcc_assert (node->label);
4318 return;
4320 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4321 ++dw2_string_counter;
4322 node->label = xstrdup (label);
4324 if (!dwarf_split_debug_info)
4326 node->form = DW_FORM_strp;
4327 node->index = NOT_INDEXED;
4329 else
4331 node->form = DW_FORM_GNU_str_index;
4332 node->index = NO_INDEX_ASSIGNED;
4336 /* Find out whether a string should be output inline in DIE
4337 or out-of-line in .debug_str section. */
4339 static enum dwarf_form
4340 find_string_form (struct indirect_string_node *node)
4342 unsigned int len;
4344 if (node->form)
4345 return node->form;
4347 len = strlen (node->str) + 1;
4349 /* If the string is shorter or equal to the size of the reference, it is
4350 always better to put it inline. */
4351 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4352 return node->form = DW_FORM_string;
4354 /* If we cannot expect the linker to merge strings in .debug_str
4355 section, only put it into .debug_str if it is worth even in this
4356 single module. */
4357 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4358 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4359 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4360 return node->form = DW_FORM_string;
4362 set_indirect_string (node);
4364 return node->form;
4367 /* Find out whether the string referenced from the attribute should be
4368 output inline in DIE or out-of-line in .debug_str section. */
4370 static enum dwarf_form
4371 AT_string_form (dw_attr_node *a)
4373 gcc_assert (a && AT_class (a) == dw_val_class_str);
4374 return find_string_form (a->dw_attr_val.v.val_str);
4377 /* Add a DIE reference attribute value to a DIE. */
4379 static inline void
4380 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4382 dw_attr_node attr;
4383 gcc_checking_assert (targ_die != NULL);
4385 /* With LTO we can end up trying to reference something we didn't create
4386 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4387 if (targ_die == NULL)
4388 return;
4390 attr.dw_attr = attr_kind;
4391 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4392 attr.dw_attr_val.val_entry = NULL;
4393 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4394 attr.dw_attr_val.v.val_die_ref.external = 0;
4395 add_dwarf_attr (die, &attr);
4398 /* Change DIE reference REF to point to NEW_DIE instead. */
4400 static inline void
4401 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4403 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4404 ref->dw_attr_val.v.val_die_ref.die = new_die;
4405 ref->dw_attr_val.v.val_die_ref.external = 0;
4408 /* Add an AT_specification attribute to a DIE, and also make the back
4409 pointer from the specification to the definition. */
4411 static inline void
4412 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4414 add_AT_die_ref (die, DW_AT_specification, targ_die);
4415 gcc_assert (!targ_die->die_definition);
4416 targ_die->die_definition = die;
4419 static inline dw_die_ref
4420 AT_ref (dw_attr_node *a)
4422 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4423 return a->dw_attr_val.v.val_die_ref.die;
4426 static inline int
4427 AT_ref_external (dw_attr_node *a)
4429 if (a && AT_class (a) == dw_val_class_die_ref)
4430 return a->dw_attr_val.v.val_die_ref.external;
4432 return 0;
4435 static inline void
4436 set_AT_ref_external (dw_attr_node *a, int i)
4438 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4439 a->dw_attr_val.v.val_die_ref.external = i;
4442 /* Add an FDE reference attribute value to a DIE. */
4444 static inline void
4445 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4447 dw_attr_node attr;
4449 attr.dw_attr = attr_kind;
4450 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4451 attr.dw_attr_val.val_entry = NULL;
4452 attr.dw_attr_val.v.val_fde_index = targ_fde;
4453 add_dwarf_attr (die, &attr);
4456 /* Add a location description attribute value to a DIE. */
4458 static inline void
4459 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4461 dw_attr_node attr;
4463 attr.dw_attr = attr_kind;
4464 attr.dw_attr_val.val_class = dw_val_class_loc;
4465 attr.dw_attr_val.val_entry = NULL;
4466 attr.dw_attr_val.v.val_loc = loc;
4467 add_dwarf_attr (die, &attr);
4470 static inline dw_loc_descr_ref
4471 AT_loc (dw_attr_node *a)
4473 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4474 return a->dw_attr_val.v.val_loc;
4477 static inline void
4478 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4480 dw_attr_node attr;
4482 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4483 return;
4485 attr.dw_attr = attr_kind;
4486 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4487 attr.dw_attr_val.val_entry = NULL;
4488 attr.dw_attr_val.v.val_loc_list = loc_list;
4489 add_dwarf_attr (die, &attr);
4490 have_location_lists = true;
4493 static inline dw_loc_list_ref
4494 AT_loc_list (dw_attr_node *a)
4496 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4497 return a->dw_attr_val.v.val_loc_list;
4500 static inline dw_loc_list_ref *
4501 AT_loc_list_ptr (dw_attr_node *a)
4503 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4504 return &a->dw_attr_val.v.val_loc_list;
4507 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4509 static hashval_t hash (addr_table_entry *);
4510 static bool equal (addr_table_entry *, addr_table_entry *);
4513 /* Table of entries into the .debug_addr section. */
4515 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4517 /* Hash an address_table_entry. */
4519 hashval_t
4520 addr_hasher::hash (addr_table_entry *a)
4522 inchash::hash hstate;
4523 switch (a->kind)
4525 case ate_kind_rtx:
4526 hstate.add_int (0);
4527 break;
4528 case ate_kind_rtx_dtprel:
4529 hstate.add_int (1);
4530 break;
4531 case ate_kind_label:
4532 return htab_hash_string (a->addr.label);
4533 default:
4534 gcc_unreachable ();
4536 inchash::add_rtx (a->addr.rtl, hstate);
4537 return hstate.end ();
4540 /* Determine equality for two address_table_entries. */
4542 bool
4543 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4545 if (a1->kind != a2->kind)
4546 return 0;
4547 switch (a1->kind)
4549 case ate_kind_rtx:
4550 case ate_kind_rtx_dtprel:
4551 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4552 case ate_kind_label:
4553 return strcmp (a1->addr.label, a2->addr.label) == 0;
4554 default:
4555 gcc_unreachable ();
4559 /* Initialize an addr_table_entry. */
4561 void
4562 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4564 e->kind = kind;
4565 switch (kind)
4567 case ate_kind_rtx:
4568 case ate_kind_rtx_dtprel:
4569 e->addr.rtl = (rtx) addr;
4570 break;
4571 case ate_kind_label:
4572 e->addr.label = (char *) addr;
4573 break;
4575 e->refcount = 0;
4576 e->index = NO_INDEX_ASSIGNED;
4579 /* Add attr to the address table entry to the table. Defer setting an
4580 index until output time. */
4582 static addr_table_entry *
4583 add_addr_table_entry (void *addr, enum ate_kind kind)
4585 addr_table_entry *node;
4586 addr_table_entry finder;
4588 gcc_assert (dwarf_split_debug_info);
4589 if (! addr_index_table)
4590 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4591 init_addr_table_entry (&finder, kind, addr);
4592 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4594 if (*slot == HTAB_EMPTY_ENTRY)
4596 node = ggc_cleared_alloc<addr_table_entry> ();
4597 init_addr_table_entry (node, kind, addr);
4598 *slot = node;
4600 else
4601 node = *slot;
4603 node->refcount++;
4604 return node;
4607 /* Remove an entry from the addr table by decrementing its refcount.
4608 Strictly, decrementing the refcount would be enough, but the
4609 assertion that the entry is actually in the table has found
4610 bugs. */
4612 static void
4613 remove_addr_table_entry (addr_table_entry *entry)
4615 gcc_assert (dwarf_split_debug_info && addr_index_table);
4616 /* After an index is assigned, the table is frozen. */
4617 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4618 entry->refcount--;
4621 /* Given a location list, remove all addresses it refers to from the
4622 address_table. */
4624 static void
4625 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4627 for (; descr; descr = descr->dw_loc_next)
4628 if (descr->dw_loc_oprnd1.val_entry != NULL)
4630 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4631 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4635 /* A helper function for dwarf2out_finish called through
4636 htab_traverse. Assign an addr_table_entry its index. All entries
4637 must be collected into the table when this function is called,
4638 because the indexing code relies on htab_traverse to traverse nodes
4639 in the same order for each run. */
4642 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4644 addr_table_entry *node = *h;
4646 /* Don't index unreferenced nodes. */
4647 if (node->refcount == 0)
4648 return 1;
4650 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4651 node->index = *index;
4652 *index += 1;
4654 return 1;
4657 /* Add an address constant attribute value to a DIE. When using
4658 dwarf_split_debug_info, address attributes in dies destined for the
4659 final executable should be direct references--setting the parameter
4660 force_direct ensures this behavior. */
4662 static inline void
4663 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4664 bool force_direct)
4666 dw_attr_node attr;
4668 attr.dw_attr = attr_kind;
4669 attr.dw_attr_val.val_class = dw_val_class_addr;
4670 attr.dw_attr_val.v.val_addr = addr;
4671 if (dwarf_split_debug_info && !force_direct)
4672 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4673 else
4674 attr.dw_attr_val.val_entry = NULL;
4675 add_dwarf_attr (die, &attr);
4678 /* Get the RTX from to an address DIE attribute. */
4680 static inline rtx
4681 AT_addr (dw_attr_node *a)
4683 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4684 return a->dw_attr_val.v.val_addr;
4687 /* Add a file attribute value to a DIE. */
4689 static inline void
4690 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4691 struct dwarf_file_data *fd)
4693 dw_attr_node attr;
4695 attr.dw_attr = attr_kind;
4696 attr.dw_attr_val.val_class = dw_val_class_file;
4697 attr.dw_attr_val.val_entry = NULL;
4698 attr.dw_attr_val.v.val_file = fd;
4699 add_dwarf_attr (die, &attr);
4702 /* Get the dwarf_file_data from a file DIE attribute. */
4704 static inline struct dwarf_file_data *
4705 AT_file (dw_attr_node *a)
4707 gcc_assert (a && (AT_class (a) == dw_val_class_file
4708 || AT_class (a) == dw_val_class_file_implicit));
4709 return a->dw_attr_val.v.val_file;
4712 /* Add a vms delta attribute value to a DIE. */
4714 static inline void
4715 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4716 const char *lbl1, const char *lbl2)
4718 dw_attr_node attr;
4720 attr.dw_attr = attr_kind;
4721 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4722 attr.dw_attr_val.val_entry = NULL;
4723 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4724 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4725 add_dwarf_attr (die, &attr);
4728 /* Add a label identifier attribute value to a DIE. */
4730 static inline void
4731 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4732 const char *lbl_id)
4734 dw_attr_node attr;
4736 attr.dw_attr = attr_kind;
4737 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4738 attr.dw_attr_val.val_entry = NULL;
4739 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4740 if (dwarf_split_debug_info)
4741 attr.dw_attr_val.val_entry
4742 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4743 ate_kind_label);
4744 add_dwarf_attr (die, &attr);
4747 /* Add a section offset attribute value to a DIE, an offset into the
4748 debug_line section. */
4750 static inline void
4751 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4752 const char *label)
4754 dw_attr_node attr;
4756 attr.dw_attr = attr_kind;
4757 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4758 attr.dw_attr_val.val_entry = NULL;
4759 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4760 add_dwarf_attr (die, &attr);
4763 /* Add a section offset attribute value to a DIE, an offset into the
4764 debug_loclists section. */
4766 static inline void
4767 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4768 const char *label)
4770 dw_attr_node attr;
4772 attr.dw_attr = attr_kind;
4773 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4774 attr.dw_attr_val.val_entry = NULL;
4775 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4776 add_dwarf_attr (die, &attr);
4779 /* Add a section offset attribute value to a DIE, an offset into the
4780 debug_macinfo section. */
4782 static inline void
4783 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4784 const char *label)
4786 dw_attr_node attr;
4788 attr.dw_attr = attr_kind;
4789 attr.dw_attr_val.val_class = dw_val_class_macptr;
4790 attr.dw_attr_val.val_entry = NULL;
4791 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4792 add_dwarf_attr (die, &attr);
4795 /* Add an offset attribute value to a DIE. */
4797 static inline void
4798 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4799 unsigned HOST_WIDE_INT offset)
4801 dw_attr_node attr;
4803 attr.dw_attr = attr_kind;
4804 attr.dw_attr_val.val_class = dw_val_class_offset;
4805 attr.dw_attr_val.val_entry = NULL;
4806 attr.dw_attr_val.v.val_offset = offset;
4807 add_dwarf_attr (die, &attr);
4810 /* Add a range_list attribute value to a DIE. When using
4811 dwarf_split_debug_info, address attributes in dies destined for the
4812 final executable should be direct references--setting the parameter
4813 force_direct ensures this behavior. */
4815 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4816 #define RELOCATED_OFFSET (NULL)
4818 static void
4819 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4820 long unsigned int offset, bool force_direct)
4822 dw_attr_node attr;
4824 attr.dw_attr = attr_kind;
4825 attr.dw_attr_val.val_class = dw_val_class_range_list;
4826 /* For the range_list attribute, use val_entry to store whether the
4827 offset should follow split-debug-info or normal semantics. This
4828 value is read in output_range_list_offset. */
4829 if (dwarf_split_debug_info && !force_direct)
4830 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4831 else
4832 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4833 attr.dw_attr_val.v.val_offset = offset;
4834 add_dwarf_attr (die, &attr);
4837 /* Return the start label of a delta attribute. */
4839 static inline const char *
4840 AT_vms_delta1 (dw_attr_node *a)
4842 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4843 return a->dw_attr_val.v.val_vms_delta.lbl1;
4846 /* Return the end label of a delta attribute. */
4848 static inline const char *
4849 AT_vms_delta2 (dw_attr_node *a)
4851 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4852 return a->dw_attr_val.v.val_vms_delta.lbl2;
4855 static inline const char *
4856 AT_lbl (dw_attr_node *a)
4858 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4859 || AT_class (a) == dw_val_class_lineptr
4860 || AT_class (a) == dw_val_class_macptr
4861 || AT_class (a) == dw_val_class_loclistsptr
4862 || AT_class (a) == dw_val_class_high_pc));
4863 return a->dw_attr_val.v.val_lbl_id;
4866 /* Get the attribute of type attr_kind. */
4868 static dw_attr_node *
4869 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4871 dw_attr_node *a;
4872 unsigned ix;
4873 dw_die_ref spec = NULL;
4875 if (! die)
4876 return NULL;
4878 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4879 if (a->dw_attr == attr_kind)
4880 return a;
4881 else if (a->dw_attr == DW_AT_specification
4882 || a->dw_attr == DW_AT_abstract_origin)
4883 spec = AT_ref (a);
4885 if (spec)
4886 return get_AT (spec, attr_kind);
4888 return NULL;
4891 /* Returns the parent of the declaration of DIE. */
4893 static dw_die_ref
4894 get_die_parent (dw_die_ref die)
4896 dw_die_ref t;
4898 if (!die)
4899 return NULL;
4901 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4902 || (t = get_AT_ref (die, DW_AT_specification)))
4903 die = t;
4905 return die->die_parent;
4908 /* Return the "low pc" attribute value, typically associated with a subprogram
4909 DIE. Return null if the "low pc" attribute is either not present, or if it
4910 cannot be represented as an assembler label identifier. */
4912 static inline const char *
4913 get_AT_low_pc (dw_die_ref die)
4915 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4917 return a ? AT_lbl (a) : NULL;
4920 /* Return the "high pc" attribute value, typically associated with a subprogram
4921 DIE. Return null if the "high pc" attribute is either not present, or if it
4922 cannot be represented as an assembler label identifier. */
4924 static inline const char *
4925 get_AT_hi_pc (dw_die_ref die)
4927 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4929 return a ? AT_lbl (a) : NULL;
4932 /* Return the value of the string attribute designated by ATTR_KIND, or
4933 NULL if it is not present. */
4935 static inline const char *
4936 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4938 dw_attr_node *a = get_AT (die, attr_kind);
4940 return a ? AT_string (a) : NULL;
4943 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4944 if it is not present. */
4946 static inline int
4947 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4949 dw_attr_node *a = get_AT (die, attr_kind);
4951 return a ? AT_flag (a) : 0;
4954 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4955 if it is not present. */
4957 static inline unsigned
4958 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4960 dw_attr_node *a = get_AT (die, attr_kind);
4962 return a ? AT_unsigned (a) : 0;
4965 static inline dw_die_ref
4966 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4968 dw_attr_node *a = get_AT (die, attr_kind);
4970 return a ? AT_ref (a) : NULL;
4973 static inline struct dwarf_file_data *
4974 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4976 dw_attr_node *a = get_AT (die, attr_kind);
4978 return a ? AT_file (a) : NULL;
4981 /* Return TRUE if the language is C++. */
4983 static inline bool
4984 is_cxx (void)
4986 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4988 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4989 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4992 /* Return TRUE if the language is Java. */
4994 static inline bool
4995 is_java (void)
4997 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4999 return lang == DW_LANG_Java;
5002 /* Return TRUE if the language is Fortran. */
5004 static inline bool
5005 is_fortran (void)
5007 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5009 return (lang == DW_LANG_Fortran77
5010 || lang == DW_LANG_Fortran90
5011 || lang == DW_LANG_Fortran95
5012 || lang == DW_LANG_Fortran03
5013 || lang == DW_LANG_Fortran08);
5016 /* Return TRUE if the language is Ada. */
5018 static inline bool
5019 is_ada (void)
5021 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5023 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5026 /* Remove the specified attribute if present. Return TRUE if removal
5027 was successful. */
5029 static bool
5030 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5032 dw_attr_node *a;
5033 unsigned ix;
5035 if (! die)
5036 return false;
5038 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5039 if (a->dw_attr == attr_kind)
5041 if (AT_class (a) == dw_val_class_str)
5042 if (a->dw_attr_val.v.val_str->refcount)
5043 a->dw_attr_val.v.val_str->refcount--;
5045 /* vec::ordered_remove should help reduce the number of abbrevs
5046 that are needed. */
5047 die->die_attr->ordered_remove (ix);
5048 return true;
5050 return false;
5053 /* Remove CHILD from its parent. PREV must have the property that
5054 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5056 static void
5057 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5059 gcc_assert (child->die_parent == prev->die_parent);
5060 gcc_assert (prev->die_sib == child);
5061 if (prev == child)
5063 gcc_assert (child->die_parent->die_child == child);
5064 prev = NULL;
5066 else
5067 prev->die_sib = child->die_sib;
5068 if (child->die_parent->die_child == child)
5069 child->die_parent->die_child = prev;
5070 child->die_sib = NULL;
5073 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5074 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5076 static void
5077 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5079 dw_die_ref parent = old_child->die_parent;
5081 gcc_assert (parent == prev->die_parent);
5082 gcc_assert (prev->die_sib == old_child);
5084 new_child->die_parent = parent;
5085 if (prev == old_child)
5087 gcc_assert (parent->die_child == old_child);
5088 new_child->die_sib = new_child;
5090 else
5092 prev->die_sib = new_child;
5093 new_child->die_sib = old_child->die_sib;
5095 if (old_child->die_parent->die_child == old_child)
5096 old_child->die_parent->die_child = new_child;
5097 old_child->die_sib = NULL;
5100 /* Move all children from OLD_PARENT to NEW_PARENT. */
5102 static void
5103 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5105 dw_die_ref c;
5106 new_parent->die_child = old_parent->die_child;
5107 old_parent->die_child = NULL;
5108 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5111 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5112 matches TAG. */
5114 static void
5115 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5117 dw_die_ref c;
5119 c = die->die_child;
5120 if (c) do {
5121 dw_die_ref prev = c;
5122 c = c->die_sib;
5123 while (c->die_tag == tag)
5125 remove_child_with_prev (c, prev);
5126 c->die_parent = NULL;
5127 /* Might have removed every child. */
5128 if (die->die_child == NULL)
5129 return;
5130 c = prev->die_sib;
5132 } while (c != die->die_child);
5135 /* Add a CHILD_DIE as the last child of DIE. */
5137 static void
5138 add_child_die (dw_die_ref die, dw_die_ref child_die)
5140 /* FIXME this should probably be an assert. */
5141 if (! die || ! child_die)
5142 return;
5143 gcc_assert (die != child_die);
5145 child_die->die_parent = die;
5146 if (die->die_child)
5148 child_die->die_sib = die->die_child->die_sib;
5149 die->die_child->die_sib = child_die;
5151 else
5152 child_die->die_sib = child_die;
5153 die->die_child = child_die;
5156 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5158 static void
5159 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5160 dw_die_ref after_die)
5162 gcc_assert (die
5163 && child_die
5164 && after_die
5165 && die->die_child
5166 && die != child_die);
5168 child_die->die_parent = die;
5169 child_die->die_sib = after_die->die_sib;
5170 after_die->die_sib = child_die;
5171 if (die->die_child == after_die)
5172 die->die_child = child_die;
5175 /* Unassociate CHILD from its parent, and make its parent be
5176 NEW_PARENT. */
5178 static void
5179 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5181 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5182 if (p->die_sib == child)
5184 remove_child_with_prev (child, p);
5185 break;
5187 add_child_die (new_parent, child);
5190 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5191 is the specification, to the end of PARENT's list of children.
5192 This is done by removing and re-adding it. */
5194 static void
5195 splice_child_die (dw_die_ref parent, dw_die_ref child)
5197 /* We want the declaration DIE from inside the class, not the
5198 specification DIE at toplevel. */
5199 if (child->die_parent != parent)
5201 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5203 if (tmp)
5204 child = tmp;
5207 gcc_assert (child->die_parent == parent
5208 || (child->die_parent
5209 == get_AT_ref (parent, DW_AT_specification)));
5211 reparent_child (child, parent);
5214 /* Create and return a new die with a parent of PARENT_DIE. If
5215 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5216 associated tree T must be supplied to determine parenthood
5217 later. */
5219 static inline dw_die_ref
5220 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5222 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5224 die->die_tag = tag_value;
5226 if (parent_die != NULL)
5227 add_child_die (parent_die, die);
5228 else
5230 limbo_die_node *limbo_node;
5232 /* No DIEs created after early dwarf should end up in limbo,
5233 because the limbo list should not persist past LTO
5234 streaming. */
5235 if (tag_value != DW_TAG_compile_unit
5236 /* These are allowed because they're generated while
5237 breaking out COMDAT units late. */
5238 && tag_value != DW_TAG_type_unit
5239 && tag_value != DW_TAG_skeleton_unit
5240 && !early_dwarf
5241 /* Allow nested functions to live in limbo because they will
5242 only temporarily live there, as decls_for_scope will fix
5243 them up. */
5244 && (TREE_CODE (t) != FUNCTION_DECL
5245 || !decl_function_context (t))
5246 /* Same as nested functions above but for types. Types that
5247 are local to a function will be fixed in
5248 decls_for_scope. */
5249 && (!RECORD_OR_UNION_TYPE_P (t)
5250 || !TYPE_CONTEXT (t)
5251 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5252 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5253 especially in the ltrans stage, but once we implement LTO
5254 dwarf streaming, we should remove this exception. */
5255 && !in_lto_p)
5257 fprintf (stderr, "symbol ended up in limbo too late:");
5258 debug_generic_stmt (t);
5259 gcc_unreachable ();
5262 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5263 limbo_node->die = die;
5264 limbo_node->created_for = t;
5265 limbo_node->next = limbo_die_list;
5266 limbo_die_list = limbo_node;
5269 return die;
5272 /* Return the DIE associated with the given type specifier. */
5274 static inline dw_die_ref
5275 lookup_type_die (tree type)
5277 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5278 if (die && die->removed)
5280 TYPE_SYMTAB_DIE (type) = NULL;
5281 return NULL;
5283 return die;
5286 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5287 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5288 anonymous type instead the one of the naming typedef. */
5290 static inline dw_die_ref
5291 strip_naming_typedef (tree type, dw_die_ref type_die)
5293 if (type
5294 && TREE_CODE (type) == RECORD_TYPE
5295 && type_die
5296 && type_die->die_tag == DW_TAG_typedef
5297 && is_naming_typedef_decl (TYPE_NAME (type)))
5298 type_die = get_AT_ref (type_die, DW_AT_type);
5299 return type_die;
5302 /* Like lookup_type_die, but if type is an anonymous type named by a
5303 typedef[1], return the DIE of the anonymous type instead the one of
5304 the naming typedef. This is because in gen_typedef_die, we did
5305 equate the anonymous struct named by the typedef with the DIE of
5306 the naming typedef. So by default, lookup_type_die on an anonymous
5307 struct yields the DIE of the naming typedef.
5309 [1]: Read the comment of is_naming_typedef_decl to learn about what
5310 a naming typedef is. */
5312 static inline dw_die_ref
5313 lookup_type_die_strip_naming_typedef (tree type)
5315 dw_die_ref die = lookup_type_die (type);
5316 return strip_naming_typedef (type, die);
5319 /* Equate a DIE to a given type specifier. */
5321 static inline void
5322 equate_type_number_to_die (tree type, dw_die_ref type_die)
5324 TYPE_SYMTAB_DIE (type) = type_die;
5327 /* Returns a hash value for X (which really is a die_struct). */
5329 inline hashval_t
5330 decl_die_hasher::hash (die_node *x)
5332 return (hashval_t) x->decl_id;
5335 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5337 inline bool
5338 decl_die_hasher::equal (die_node *x, tree y)
5340 return (x->decl_id == DECL_UID (y));
5343 /* Return the DIE associated with a given declaration. */
5345 static inline dw_die_ref
5346 lookup_decl_die (tree decl)
5348 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5349 NO_INSERT);
5350 if (!die)
5351 return NULL;
5352 if ((*die)->removed)
5354 decl_die_table->clear_slot (die);
5355 return NULL;
5357 return *die;
5360 /* Returns a hash value for X (which really is a var_loc_list). */
5362 inline hashval_t
5363 decl_loc_hasher::hash (var_loc_list *x)
5365 return (hashval_t) x->decl_id;
5368 /* Return nonzero if decl_id of var_loc_list X is the same as
5369 UID of decl *Y. */
5371 inline bool
5372 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5374 return (x->decl_id == DECL_UID (y));
5377 /* Return the var_loc list associated with a given declaration. */
5379 static inline var_loc_list *
5380 lookup_decl_loc (const_tree decl)
5382 if (!decl_loc_table)
5383 return NULL;
5384 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5387 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5389 inline hashval_t
5390 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5392 return (hashval_t) x->decl_id;
5395 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5396 UID of decl *Y. */
5398 inline bool
5399 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5401 return (x->decl_id == DECL_UID (y));
5404 /* Equate a DIE to a particular declaration. */
5406 static void
5407 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5409 unsigned int decl_id = DECL_UID (decl);
5411 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5412 decl_die->decl_id = decl_id;
5415 /* Return how many bits covers PIECE EXPR_LIST. */
5417 static HOST_WIDE_INT
5418 decl_piece_bitsize (rtx piece)
5420 int ret = (int) GET_MODE (piece);
5421 if (ret)
5422 return ret;
5423 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5424 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5425 return INTVAL (XEXP (XEXP (piece, 0), 0));
5428 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5430 static rtx *
5431 decl_piece_varloc_ptr (rtx piece)
5433 if ((int) GET_MODE (piece))
5434 return &XEXP (piece, 0);
5435 else
5436 return &XEXP (XEXP (piece, 0), 1);
5439 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5440 Next is the chain of following piece nodes. */
5442 static rtx_expr_list *
5443 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5445 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5446 return alloc_EXPR_LIST (bitsize, loc_note, next);
5447 else
5448 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5449 GEN_INT (bitsize),
5450 loc_note), next);
5453 /* Return rtx that should be stored into loc field for
5454 LOC_NOTE and BITPOS/BITSIZE. */
5456 static rtx
5457 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5458 HOST_WIDE_INT bitsize)
5460 if (bitsize != -1)
5462 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5463 if (bitpos != 0)
5464 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5466 return loc_note;
5469 /* This function either modifies location piece list *DEST in
5470 place (if SRC and INNER is NULL), or copies location piece list
5471 *SRC to *DEST while modifying it. Location BITPOS is modified
5472 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5473 not copied and if needed some padding around it is added.
5474 When modifying in place, DEST should point to EXPR_LIST where
5475 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5476 to the start of the whole list and INNER points to the EXPR_LIST
5477 where earlier pieces cover PIECE_BITPOS bits. */
5479 static void
5480 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5481 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5482 HOST_WIDE_INT bitsize, rtx loc_note)
5484 HOST_WIDE_INT diff;
5485 bool copy = inner != NULL;
5487 if (copy)
5489 /* First copy all nodes preceding the current bitpos. */
5490 while (src != inner)
5492 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5493 decl_piece_bitsize (*src), NULL_RTX);
5494 dest = &XEXP (*dest, 1);
5495 src = &XEXP (*src, 1);
5498 /* Add padding if needed. */
5499 if (bitpos != piece_bitpos)
5501 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5502 copy ? NULL_RTX : *dest);
5503 dest = &XEXP (*dest, 1);
5505 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5507 gcc_assert (!copy);
5508 /* A piece with correct bitpos and bitsize already exist,
5509 just update the location for it and return. */
5510 *decl_piece_varloc_ptr (*dest) = loc_note;
5511 return;
5513 /* Add the piece that changed. */
5514 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5515 dest = &XEXP (*dest, 1);
5516 /* Skip over pieces that overlap it. */
5517 diff = bitpos - piece_bitpos + bitsize;
5518 if (!copy)
5519 src = dest;
5520 while (diff > 0 && *src)
5522 rtx piece = *src;
5523 diff -= decl_piece_bitsize (piece);
5524 if (copy)
5525 src = &XEXP (piece, 1);
5526 else
5528 *src = XEXP (piece, 1);
5529 free_EXPR_LIST_node (piece);
5532 /* Add padding if needed. */
5533 if (diff < 0 && *src)
5535 if (!copy)
5536 dest = src;
5537 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5538 dest = &XEXP (*dest, 1);
5540 if (!copy)
5541 return;
5542 /* Finally copy all nodes following it. */
5543 while (*src)
5545 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5546 decl_piece_bitsize (*src), NULL_RTX);
5547 dest = &XEXP (*dest, 1);
5548 src = &XEXP (*src, 1);
5552 /* Add a variable location node to the linked list for DECL. */
5554 static struct var_loc_node *
5555 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5557 unsigned int decl_id;
5558 var_loc_list *temp;
5559 struct var_loc_node *loc = NULL;
5560 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5562 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5564 tree realdecl = DECL_DEBUG_EXPR (decl);
5565 if (handled_component_p (realdecl)
5566 || (TREE_CODE (realdecl) == MEM_REF
5567 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5569 HOST_WIDE_INT maxsize;
5570 bool reverse;
5571 tree innerdecl
5572 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5573 &reverse);
5574 if (!DECL_P (innerdecl)
5575 || DECL_IGNORED_P (innerdecl)
5576 || TREE_STATIC (innerdecl)
5577 || bitsize <= 0
5578 || bitpos + bitsize > 256
5579 || bitsize != maxsize)
5580 return NULL;
5581 decl = innerdecl;
5585 decl_id = DECL_UID (decl);
5586 var_loc_list **slot
5587 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5588 if (*slot == NULL)
5590 temp = ggc_cleared_alloc<var_loc_list> ();
5591 temp->decl_id = decl_id;
5592 *slot = temp;
5594 else
5595 temp = *slot;
5597 /* For PARM_DECLs try to keep around the original incoming value,
5598 even if that means we'll emit a zero-range .debug_loc entry. */
5599 if (temp->last
5600 && temp->first == temp->last
5601 && TREE_CODE (decl) == PARM_DECL
5602 && NOTE_P (temp->first->loc)
5603 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5604 && DECL_INCOMING_RTL (decl)
5605 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5606 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5607 == GET_CODE (DECL_INCOMING_RTL (decl))
5608 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5609 && (bitsize != -1
5610 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5611 NOTE_VAR_LOCATION_LOC (loc_note))
5612 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5613 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5615 loc = ggc_cleared_alloc<var_loc_node> ();
5616 temp->first->next = loc;
5617 temp->last = loc;
5618 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5620 else if (temp->last)
5622 struct var_loc_node *last = temp->last, *unused = NULL;
5623 rtx *piece_loc = NULL, last_loc_note;
5624 HOST_WIDE_INT piece_bitpos = 0;
5625 if (last->next)
5627 last = last->next;
5628 gcc_assert (last->next == NULL);
5630 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5632 piece_loc = &last->loc;
5635 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5636 if (piece_bitpos + cur_bitsize > bitpos)
5637 break;
5638 piece_bitpos += cur_bitsize;
5639 piece_loc = &XEXP (*piece_loc, 1);
5641 while (*piece_loc);
5643 /* TEMP->LAST here is either pointer to the last but one or
5644 last element in the chained list, LAST is pointer to the
5645 last element. */
5646 if (label && strcmp (last->label, label) == 0)
5648 /* For SRA optimized variables if there weren't any real
5649 insns since last note, just modify the last node. */
5650 if (piece_loc != NULL)
5652 adjust_piece_list (piece_loc, NULL, NULL,
5653 bitpos, piece_bitpos, bitsize, loc_note);
5654 return NULL;
5656 /* If the last note doesn't cover any instructions, remove it. */
5657 if (temp->last != last)
5659 temp->last->next = NULL;
5660 unused = last;
5661 last = temp->last;
5662 gcc_assert (strcmp (last->label, label) != 0);
5664 else
5666 gcc_assert (temp->first == temp->last
5667 || (temp->first->next == temp->last
5668 && TREE_CODE (decl) == PARM_DECL));
5669 memset (temp->last, '\0', sizeof (*temp->last));
5670 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5671 return temp->last;
5674 if (bitsize == -1 && NOTE_P (last->loc))
5675 last_loc_note = last->loc;
5676 else if (piece_loc != NULL
5677 && *piece_loc != NULL_RTX
5678 && piece_bitpos == bitpos
5679 && decl_piece_bitsize (*piece_loc) == bitsize)
5680 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5681 else
5682 last_loc_note = NULL_RTX;
5683 /* If the current location is the same as the end of the list,
5684 and either both or neither of the locations is uninitialized,
5685 we have nothing to do. */
5686 if (last_loc_note == NULL_RTX
5687 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5688 NOTE_VAR_LOCATION_LOC (loc_note)))
5689 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5690 != NOTE_VAR_LOCATION_STATUS (loc_note))
5691 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5692 == VAR_INIT_STATUS_UNINITIALIZED)
5693 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5694 == VAR_INIT_STATUS_UNINITIALIZED))))
5696 /* Add LOC to the end of list and update LAST. If the last
5697 element of the list has been removed above, reuse its
5698 memory for the new node, otherwise allocate a new one. */
5699 if (unused)
5701 loc = unused;
5702 memset (loc, '\0', sizeof (*loc));
5704 else
5705 loc = ggc_cleared_alloc<var_loc_node> ();
5706 if (bitsize == -1 || piece_loc == NULL)
5707 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5708 else
5709 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5710 bitpos, piece_bitpos, bitsize, loc_note);
5711 last->next = loc;
5712 /* Ensure TEMP->LAST will point either to the new last but one
5713 element of the chain, or to the last element in it. */
5714 if (last != temp->last)
5715 temp->last = last;
5717 else if (unused)
5718 ggc_free (unused);
5720 else
5722 loc = ggc_cleared_alloc<var_loc_node> ();
5723 temp->first = loc;
5724 temp->last = loc;
5725 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5727 return loc;
5730 /* Keep track of the number of spaces used to indent the
5731 output of the debugging routines that print the structure of
5732 the DIE internal representation. */
5733 static int print_indent;
5735 /* Indent the line the number of spaces given by print_indent. */
5737 static inline void
5738 print_spaces (FILE *outfile)
5740 fprintf (outfile, "%*s", print_indent, "");
5743 /* Print a type signature in hex. */
5745 static inline void
5746 print_signature (FILE *outfile, char *sig)
5748 int i;
5750 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5751 fprintf (outfile, "%02x", sig[i] & 0xff);
5754 static inline void
5755 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5757 if (discr_value->pos)
5758 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5759 else
5760 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5763 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5765 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5766 RECURSE, output location descriptor operations. */
5768 static void
5769 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5771 switch (val->val_class)
5773 case dw_val_class_addr:
5774 fprintf (outfile, "address");
5775 break;
5776 case dw_val_class_offset:
5777 fprintf (outfile, "offset");
5778 break;
5779 case dw_val_class_loc:
5780 fprintf (outfile, "location descriptor");
5781 if (val->v.val_loc == NULL)
5782 fprintf (outfile, " -> <null>\n");
5783 else if (recurse)
5785 fprintf (outfile, ":\n");
5786 print_indent += 4;
5787 print_loc_descr (val->v.val_loc, outfile);
5788 print_indent -= 4;
5790 else
5791 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5792 break;
5793 case dw_val_class_loc_list:
5794 fprintf (outfile, "location list -> label:%s",
5795 val->v.val_loc_list->ll_symbol);
5796 break;
5797 case dw_val_class_range_list:
5798 fprintf (outfile, "range list");
5799 break;
5800 case dw_val_class_const:
5801 case dw_val_class_const_implicit:
5802 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5803 break;
5804 case dw_val_class_unsigned_const:
5805 case dw_val_class_unsigned_const_implicit:
5806 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5807 break;
5808 case dw_val_class_const_double:
5809 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5810 HOST_WIDE_INT_PRINT_UNSIGNED")",
5811 val->v.val_double.high,
5812 val->v.val_double.low);
5813 break;
5814 case dw_val_class_wide_int:
5816 int i = val->v.val_wide->get_len ();
5817 fprintf (outfile, "constant (");
5818 gcc_assert (i > 0);
5819 if (val->v.val_wide->elt (i - 1) == 0)
5820 fprintf (outfile, "0x");
5821 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5822 val->v.val_wide->elt (--i));
5823 while (--i >= 0)
5824 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5825 val->v.val_wide->elt (i));
5826 fprintf (outfile, ")");
5827 break;
5829 case dw_val_class_vec:
5830 fprintf (outfile, "floating-point or vector constant");
5831 break;
5832 case dw_val_class_flag:
5833 fprintf (outfile, "%u", val->v.val_flag);
5834 break;
5835 case dw_val_class_die_ref:
5836 if (val->v.val_die_ref.die != NULL)
5838 dw_die_ref die = val->v.val_die_ref.die;
5840 if (die->comdat_type_p)
5842 fprintf (outfile, "die -> signature: ");
5843 print_signature (outfile,
5844 die->die_id.die_type_node->signature);
5846 else if (die->die_id.die_symbol)
5847 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5848 else
5849 fprintf (outfile, "die -> %ld", die->die_offset);
5850 fprintf (outfile, " (%p)", (void *) die);
5852 else
5853 fprintf (outfile, "die -> <null>");
5854 break;
5855 case dw_val_class_vms_delta:
5856 fprintf (outfile, "delta: @slotcount(%s-%s)",
5857 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5858 break;
5859 case dw_val_class_lbl_id:
5860 case dw_val_class_lineptr:
5861 case dw_val_class_macptr:
5862 case dw_val_class_loclistsptr:
5863 case dw_val_class_high_pc:
5864 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5865 break;
5866 case dw_val_class_str:
5867 if (val->v.val_str->str != NULL)
5868 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5869 else
5870 fprintf (outfile, "<null>");
5871 break;
5872 case dw_val_class_file:
5873 case dw_val_class_file_implicit:
5874 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5875 val->v.val_file->emitted_number);
5876 break;
5877 case dw_val_class_data8:
5879 int i;
5881 for (i = 0; i < 8; i++)
5882 fprintf (outfile, "%02x", val->v.val_data8[i]);
5883 break;
5885 case dw_val_class_discr_value:
5886 print_discr_value (outfile, &val->v.val_discr_value);
5887 break;
5888 case dw_val_class_discr_list:
5889 for (dw_discr_list_ref node = val->v.val_discr_list;
5890 node != NULL;
5891 node = node->dw_discr_next)
5893 if (node->dw_discr_range)
5895 fprintf (outfile, " .. ");
5896 print_discr_value (outfile, &node->dw_discr_lower_bound);
5897 print_discr_value (outfile, &node->dw_discr_upper_bound);
5899 else
5900 print_discr_value (outfile, &node->dw_discr_lower_bound);
5902 if (node->dw_discr_next != NULL)
5903 fprintf (outfile, " | ");
5905 default:
5906 break;
5910 /* Likewise, for a DIE attribute. */
5912 static void
5913 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5915 print_dw_val (&a->dw_attr_val, recurse, outfile);
5919 /* Print the list of operands in the LOC location description to OUTFILE. This
5920 routine is a debugging aid only. */
5922 static void
5923 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5925 dw_loc_descr_ref l = loc;
5927 if (loc == NULL)
5929 print_spaces (outfile);
5930 fprintf (outfile, "<null>\n");
5931 return;
5934 for (l = loc; l != NULL; l = l->dw_loc_next)
5936 print_spaces (outfile);
5937 fprintf (outfile, "(%p) %s",
5938 (void *) l,
5939 dwarf_stack_op_name (l->dw_loc_opc));
5940 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5942 fprintf (outfile, " ");
5943 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5945 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5947 fprintf (outfile, ", ");
5948 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5950 fprintf (outfile, "\n");
5954 /* Print the information associated with a given DIE, and its children.
5955 This routine is a debugging aid only. */
5957 static void
5958 print_die (dw_die_ref die, FILE *outfile)
5960 dw_attr_node *a;
5961 dw_die_ref c;
5962 unsigned ix;
5964 print_spaces (outfile);
5965 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5966 die->die_offset, dwarf_tag_name (die->die_tag),
5967 (void*) die);
5968 print_spaces (outfile);
5969 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5970 fprintf (outfile, " offset: %ld", die->die_offset);
5971 fprintf (outfile, " mark: %d\n", die->die_mark);
5973 if (die->comdat_type_p)
5975 print_spaces (outfile);
5976 fprintf (outfile, " signature: ");
5977 print_signature (outfile, die->die_id.die_type_node->signature);
5978 fprintf (outfile, "\n");
5981 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5983 print_spaces (outfile);
5984 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5986 print_attribute (a, true, outfile);
5987 fprintf (outfile, "\n");
5990 if (die->die_child != NULL)
5992 print_indent += 4;
5993 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5994 print_indent -= 4;
5996 if (print_indent == 0)
5997 fprintf (outfile, "\n");
6000 /* Print the list of operations in the LOC location description. */
6002 DEBUG_FUNCTION void
6003 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6005 print_loc_descr (loc, stderr);
6008 /* Print the information collected for a given DIE. */
6010 DEBUG_FUNCTION void
6011 debug_dwarf_die (dw_die_ref die)
6013 print_die (die, stderr);
6016 DEBUG_FUNCTION void
6017 debug (die_struct &ref)
6019 print_die (&ref, stderr);
6022 DEBUG_FUNCTION void
6023 debug (die_struct *ptr)
6025 if (ptr)
6026 debug (*ptr);
6027 else
6028 fprintf (stderr, "<nil>\n");
6032 /* Print all DWARF information collected for the compilation unit.
6033 This routine is a debugging aid only. */
6035 DEBUG_FUNCTION void
6036 debug_dwarf (void)
6038 print_indent = 0;
6039 print_die (comp_unit_die (), stderr);
6042 /* Verify the DIE tree structure. */
6044 DEBUG_FUNCTION void
6045 verify_die (dw_die_ref die)
6047 gcc_assert (!die->die_mark);
6048 if (die->die_parent == NULL
6049 && die->die_sib == NULL)
6050 return;
6051 /* Verify the die_sib list is cyclic. */
6052 dw_die_ref x = die;
6055 x->die_mark = 1;
6056 x = x->die_sib;
6058 while (x && !x->die_mark);
6059 gcc_assert (x == die);
6060 x = die;
6063 /* Verify all dies have the same parent. */
6064 gcc_assert (x->die_parent == die->die_parent);
6065 if (x->die_child)
6067 /* Verify the child has the proper parent and recurse. */
6068 gcc_assert (x->die_child->die_parent == x);
6069 verify_die (x->die_child);
6071 x->die_mark = 0;
6072 x = x->die_sib;
6074 while (x && x->die_mark);
6077 /* Sanity checks on DIEs. */
6079 static void
6080 check_die (dw_die_ref die)
6082 unsigned ix;
6083 dw_attr_node *a;
6084 bool inline_found = false;
6085 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6086 int n_decl_line = 0, n_decl_file = 0;
6087 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6089 switch (a->dw_attr)
6091 case DW_AT_inline:
6092 if (a->dw_attr_val.v.val_unsigned)
6093 inline_found = true;
6094 break;
6095 case DW_AT_location:
6096 ++n_location;
6097 break;
6098 case DW_AT_low_pc:
6099 ++n_low_pc;
6100 break;
6101 case DW_AT_high_pc:
6102 ++n_high_pc;
6103 break;
6104 case DW_AT_artificial:
6105 ++n_artificial;
6106 break;
6107 case DW_AT_decl_line:
6108 ++n_decl_line;
6109 break;
6110 case DW_AT_decl_file:
6111 ++n_decl_file;
6112 break;
6113 default:
6114 break;
6117 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6118 || n_decl_line > 1 || n_decl_file > 1)
6120 fprintf (stderr, "Duplicate attributes in DIE:\n");
6121 debug_dwarf_die (die);
6122 gcc_unreachable ();
6124 if (inline_found)
6126 /* A debugging information entry that is a member of an abstract
6127 instance tree [that has DW_AT_inline] should not contain any
6128 attributes which describe aspects of the subroutine which vary
6129 between distinct inlined expansions or distinct out-of-line
6130 expansions. */
6131 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6132 gcc_assert (a->dw_attr != DW_AT_low_pc
6133 && a->dw_attr != DW_AT_high_pc
6134 && a->dw_attr != DW_AT_location
6135 && a->dw_attr != DW_AT_frame_base
6136 && a->dw_attr != DW_AT_call_all_calls
6137 && a->dw_attr != DW_AT_GNU_all_call_sites);
6141 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6142 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6143 DIE that marks the start of the DIEs for this include file. */
6145 static dw_die_ref
6146 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6148 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6149 dw_die_ref new_unit = gen_compile_unit_die (filename);
6151 new_unit->die_sib = old_unit;
6152 return new_unit;
6155 /* Close an include-file CU and reopen the enclosing one. */
6157 static dw_die_ref
6158 pop_compile_unit (dw_die_ref old_unit)
6160 dw_die_ref new_unit = old_unit->die_sib;
6162 old_unit->die_sib = NULL;
6163 return new_unit;
6166 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6167 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6168 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6170 /* Calculate the checksum of a location expression. */
6172 static inline void
6173 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6175 int tem;
6176 inchash::hash hstate;
6177 hashval_t hash;
6179 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6180 CHECKSUM (tem);
6181 hash_loc_operands (loc, hstate);
6182 hash = hstate.end();
6183 CHECKSUM (hash);
6186 /* Calculate the checksum of an attribute. */
6188 static void
6189 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6191 dw_loc_descr_ref loc;
6192 rtx r;
6194 CHECKSUM (at->dw_attr);
6196 /* We don't care that this was compiled with a different compiler
6197 snapshot; if the output is the same, that's what matters. */
6198 if (at->dw_attr == DW_AT_producer)
6199 return;
6201 switch (AT_class (at))
6203 case dw_val_class_const:
6204 case dw_val_class_const_implicit:
6205 CHECKSUM (at->dw_attr_val.v.val_int);
6206 break;
6207 case dw_val_class_unsigned_const:
6208 case dw_val_class_unsigned_const_implicit:
6209 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6210 break;
6211 case dw_val_class_const_double:
6212 CHECKSUM (at->dw_attr_val.v.val_double);
6213 break;
6214 case dw_val_class_wide_int:
6215 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6216 get_full_len (*at->dw_attr_val.v.val_wide)
6217 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6218 break;
6219 case dw_val_class_vec:
6220 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6221 (at->dw_attr_val.v.val_vec.length
6222 * at->dw_attr_val.v.val_vec.elt_size));
6223 break;
6224 case dw_val_class_flag:
6225 CHECKSUM (at->dw_attr_val.v.val_flag);
6226 break;
6227 case dw_val_class_str:
6228 CHECKSUM_STRING (AT_string (at));
6229 break;
6231 case dw_val_class_addr:
6232 r = AT_addr (at);
6233 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6234 CHECKSUM_STRING (XSTR (r, 0));
6235 break;
6237 case dw_val_class_offset:
6238 CHECKSUM (at->dw_attr_val.v.val_offset);
6239 break;
6241 case dw_val_class_loc:
6242 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6243 loc_checksum (loc, ctx);
6244 break;
6246 case dw_val_class_die_ref:
6247 die_checksum (AT_ref (at), ctx, mark);
6248 break;
6250 case dw_val_class_fde_ref:
6251 case dw_val_class_vms_delta:
6252 case dw_val_class_lbl_id:
6253 case dw_val_class_lineptr:
6254 case dw_val_class_macptr:
6255 case dw_val_class_loclistsptr:
6256 case dw_val_class_high_pc:
6257 break;
6259 case dw_val_class_file:
6260 case dw_val_class_file_implicit:
6261 CHECKSUM_STRING (AT_file (at)->filename);
6262 break;
6264 case dw_val_class_data8:
6265 CHECKSUM (at->dw_attr_val.v.val_data8);
6266 break;
6268 default:
6269 break;
6273 /* Calculate the checksum of a DIE. */
6275 static void
6276 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6278 dw_die_ref c;
6279 dw_attr_node *a;
6280 unsigned ix;
6282 /* To avoid infinite recursion. */
6283 if (die->die_mark)
6285 CHECKSUM (die->die_mark);
6286 return;
6288 die->die_mark = ++(*mark);
6290 CHECKSUM (die->die_tag);
6292 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6293 attr_checksum (a, ctx, mark);
6295 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6298 #undef CHECKSUM
6299 #undef CHECKSUM_BLOCK
6300 #undef CHECKSUM_STRING
6302 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6303 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6304 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6305 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6306 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6307 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6308 #define CHECKSUM_ATTR(FOO) \
6309 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6311 /* Calculate the checksum of a number in signed LEB128 format. */
6313 static void
6314 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6316 unsigned char byte;
6317 bool more;
6319 while (1)
6321 byte = (value & 0x7f);
6322 value >>= 7;
6323 more = !((value == 0 && (byte & 0x40) == 0)
6324 || (value == -1 && (byte & 0x40) != 0));
6325 if (more)
6326 byte |= 0x80;
6327 CHECKSUM (byte);
6328 if (!more)
6329 break;
6333 /* Calculate the checksum of a number in unsigned LEB128 format. */
6335 static void
6336 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6338 while (1)
6340 unsigned char byte = (value & 0x7f);
6341 value >>= 7;
6342 if (value != 0)
6343 /* More bytes to follow. */
6344 byte |= 0x80;
6345 CHECKSUM (byte);
6346 if (value == 0)
6347 break;
6351 /* Checksum the context of the DIE. This adds the names of any
6352 surrounding namespaces or structures to the checksum. */
6354 static void
6355 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6357 const char *name;
6358 dw_die_ref spec;
6359 int tag = die->die_tag;
6361 if (tag != DW_TAG_namespace
6362 && tag != DW_TAG_structure_type
6363 && tag != DW_TAG_class_type)
6364 return;
6366 name = get_AT_string (die, DW_AT_name);
6368 spec = get_AT_ref (die, DW_AT_specification);
6369 if (spec != NULL)
6370 die = spec;
6372 if (die->die_parent != NULL)
6373 checksum_die_context (die->die_parent, ctx);
6375 CHECKSUM_ULEB128 ('C');
6376 CHECKSUM_ULEB128 (tag);
6377 if (name != NULL)
6378 CHECKSUM_STRING (name);
6381 /* Calculate the checksum of a location expression. */
6383 static inline void
6384 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6386 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6387 were emitted as a DW_FORM_sdata instead of a location expression. */
6388 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6390 CHECKSUM_ULEB128 (DW_FORM_sdata);
6391 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6392 return;
6395 /* Otherwise, just checksum the raw location expression. */
6396 while (loc != NULL)
6398 inchash::hash hstate;
6399 hashval_t hash;
6401 CHECKSUM_ULEB128 (loc->dtprel);
6402 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6403 hash_loc_operands (loc, hstate);
6404 hash = hstate.end ();
6405 CHECKSUM (hash);
6406 loc = loc->dw_loc_next;
6410 /* Calculate the checksum of an attribute. */
6412 static void
6413 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6414 struct md5_ctx *ctx, int *mark)
6416 dw_loc_descr_ref loc;
6417 rtx r;
6419 if (AT_class (at) == dw_val_class_die_ref)
6421 dw_die_ref target_die = AT_ref (at);
6423 /* For pointer and reference types, we checksum only the (qualified)
6424 name of the target type (if there is a name). For friend entries,
6425 we checksum only the (qualified) name of the target type or function.
6426 This allows the checksum to remain the same whether the target type
6427 is complete or not. */
6428 if ((at->dw_attr == DW_AT_type
6429 && (tag == DW_TAG_pointer_type
6430 || tag == DW_TAG_reference_type
6431 || tag == DW_TAG_rvalue_reference_type
6432 || tag == DW_TAG_ptr_to_member_type))
6433 || (at->dw_attr == DW_AT_friend
6434 && tag == DW_TAG_friend))
6436 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6438 if (name_attr != NULL)
6440 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6442 if (decl == NULL)
6443 decl = target_die;
6444 CHECKSUM_ULEB128 ('N');
6445 CHECKSUM_ULEB128 (at->dw_attr);
6446 if (decl->die_parent != NULL)
6447 checksum_die_context (decl->die_parent, ctx);
6448 CHECKSUM_ULEB128 ('E');
6449 CHECKSUM_STRING (AT_string (name_attr));
6450 return;
6454 /* For all other references to another DIE, we check to see if the
6455 target DIE has already been visited. If it has, we emit a
6456 backward reference; if not, we descend recursively. */
6457 if (target_die->die_mark > 0)
6459 CHECKSUM_ULEB128 ('R');
6460 CHECKSUM_ULEB128 (at->dw_attr);
6461 CHECKSUM_ULEB128 (target_die->die_mark);
6463 else
6465 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6467 if (decl == NULL)
6468 decl = target_die;
6469 target_die->die_mark = ++(*mark);
6470 CHECKSUM_ULEB128 ('T');
6471 CHECKSUM_ULEB128 (at->dw_attr);
6472 if (decl->die_parent != NULL)
6473 checksum_die_context (decl->die_parent, ctx);
6474 die_checksum_ordered (target_die, ctx, mark);
6476 return;
6479 CHECKSUM_ULEB128 ('A');
6480 CHECKSUM_ULEB128 (at->dw_attr);
6482 switch (AT_class (at))
6484 case dw_val_class_const:
6485 case dw_val_class_const_implicit:
6486 CHECKSUM_ULEB128 (DW_FORM_sdata);
6487 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6488 break;
6490 case dw_val_class_unsigned_const:
6491 case dw_val_class_unsigned_const_implicit:
6492 CHECKSUM_ULEB128 (DW_FORM_sdata);
6493 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6494 break;
6496 case dw_val_class_const_double:
6497 CHECKSUM_ULEB128 (DW_FORM_block);
6498 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6499 CHECKSUM (at->dw_attr_val.v.val_double);
6500 break;
6502 case dw_val_class_wide_int:
6503 CHECKSUM_ULEB128 (DW_FORM_block);
6504 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6505 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6506 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6507 get_full_len (*at->dw_attr_val.v.val_wide)
6508 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6509 break;
6511 case dw_val_class_vec:
6512 CHECKSUM_ULEB128 (DW_FORM_block);
6513 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6514 * at->dw_attr_val.v.val_vec.elt_size);
6515 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6516 (at->dw_attr_val.v.val_vec.length
6517 * at->dw_attr_val.v.val_vec.elt_size));
6518 break;
6520 case dw_val_class_flag:
6521 CHECKSUM_ULEB128 (DW_FORM_flag);
6522 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6523 break;
6525 case dw_val_class_str:
6526 CHECKSUM_ULEB128 (DW_FORM_string);
6527 CHECKSUM_STRING (AT_string (at));
6528 break;
6530 case dw_val_class_addr:
6531 r = AT_addr (at);
6532 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6533 CHECKSUM_ULEB128 (DW_FORM_string);
6534 CHECKSUM_STRING (XSTR (r, 0));
6535 break;
6537 case dw_val_class_offset:
6538 CHECKSUM_ULEB128 (DW_FORM_sdata);
6539 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6540 break;
6542 case dw_val_class_loc:
6543 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6544 loc_checksum_ordered (loc, ctx);
6545 break;
6547 case dw_val_class_fde_ref:
6548 case dw_val_class_lbl_id:
6549 case dw_val_class_lineptr:
6550 case dw_val_class_macptr:
6551 case dw_val_class_loclistsptr:
6552 case dw_val_class_high_pc:
6553 break;
6555 case dw_val_class_file:
6556 case dw_val_class_file_implicit:
6557 CHECKSUM_ULEB128 (DW_FORM_string);
6558 CHECKSUM_STRING (AT_file (at)->filename);
6559 break;
6561 case dw_val_class_data8:
6562 CHECKSUM (at->dw_attr_val.v.val_data8);
6563 break;
6565 default:
6566 break;
6570 struct checksum_attributes
6572 dw_attr_node *at_name;
6573 dw_attr_node *at_type;
6574 dw_attr_node *at_friend;
6575 dw_attr_node *at_accessibility;
6576 dw_attr_node *at_address_class;
6577 dw_attr_node *at_allocated;
6578 dw_attr_node *at_artificial;
6579 dw_attr_node *at_associated;
6580 dw_attr_node *at_binary_scale;
6581 dw_attr_node *at_bit_offset;
6582 dw_attr_node *at_bit_size;
6583 dw_attr_node *at_bit_stride;
6584 dw_attr_node *at_byte_size;
6585 dw_attr_node *at_byte_stride;
6586 dw_attr_node *at_const_value;
6587 dw_attr_node *at_containing_type;
6588 dw_attr_node *at_count;
6589 dw_attr_node *at_data_location;
6590 dw_attr_node *at_data_member_location;
6591 dw_attr_node *at_decimal_scale;
6592 dw_attr_node *at_decimal_sign;
6593 dw_attr_node *at_default_value;
6594 dw_attr_node *at_digit_count;
6595 dw_attr_node *at_discr;
6596 dw_attr_node *at_discr_list;
6597 dw_attr_node *at_discr_value;
6598 dw_attr_node *at_encoding;
6599 dw_attr_node *at_endianity;
6600 dw_attr_node *at_explicit;
6601 dw_attr_node *at_is_optional;
6602 dw_attr_node *at_location;
6603 dw_attr_node *at_lower_bound;
6604 dw_attr_node *at_mutable;
6605 dw_attr_node *at_ordering;
6606 dw_attr_node *at_picture_string;
6607 dw_attr_node *at_prototyped;
6608 dw_attr_node *at_small;
6609 dw_attr_node *at_segment;
6610 dw_attr_node *at_string_length;
6611 dw_attr_node *at_string_length_bit_size;
6612 dw_attr_node *at_string_length_byte_size;
6613 dw_attr_node *at_threads_scaled;
6614 dw_attr_node *at_upper_bound;
6615 dw_attr_node *at_use_location;
6616 dw_attr_node *at_use_UTF8;
6617 dw_attr_node *at_variable_parameter;
6618 dw_attr_node *at_virtuality;
6619 dw_attr_node *at_visibility;
6620 dw_attr_node *at_vtable_elem_location;
6623 /* Collect the attributes that we will want to use for the checksum. */
6625 static void
6626 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6628 dw_attr_node *a;
6629 unsigned ix;
6631 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6633 switch (a->dw_attr)
6635 case DW_AT_name:
6636 attrs->at_name = a;
6637 break;
6638 case DW_AT_type:
6639 attrs->at_type = a;
6640 break;
6641 case DW_AT_friend:
6642 attrs->at_friend = a;
6643 break;
6644 case DW_AT_accessibility:
6645 attrs->at_accessibility = a;
6646 break;
6647 case DW_AT_address_class:
6648 attrs->at_address_class = a;
6649 break;
6650 case DW_AT_allocated:
6651 attrs->at_allocated = a;
6652 break;
6653 case DW_AT_artificial:
6654 attrs->at_artificial = a;
6655 break;
6656 case DW_AT_associated:
6657 attrs->at_associated = a;
6658 break;
6659 case DW_AT_binary_scale:
6660 attrs->at_binary_scale = a;
6661 break;
6662 case DW_AT_bit_offset:
6663 attrs->at_bit_offset = a;
6664 break;
6665 case DW_AT_bit_size:
6666 attrs->at_bit_size = a;
6667 break;
6668 case DW_AT_bit_stride:
6669 attrs->at_bit_stride = a;
6670 break;
6671 case DW_AT_byte_size:
6672 attrs->at_byte_size = a;
6673 break;
6674 case DW_AT_byte_stride:
6675 attrs->at_byte_stride = a;
6676 break;
6677 case DW_AT_const_value:
6678 attrs->at_const_value = a;
6679 break;
6680 case DW_AT_containing_type:
6681 attrs->at_containing_type = a;
6682 break;
6683 case DW_AT_count:
6684 attrs->at_count = a;
6685 break;
6686 case DW_AT_data_location:
6687 attrs->at_data_location = a;
6688 break;
6689 case DW_AT_data_member_location:
6690 attrs->at_data_member_location = a;
6691 break;
6692 case DW_AT_decimal_scale:
6693 attrs->at_decimal_scale = a;
6694 break;
6695 case DW_AT_decimal_sign:
6696 attrs->at_decimal_sign = a;
6697 break;
6698 case DW_AT_default_value:
6699 attrs->at_default_value = a;
6700 break;
6701 case DW_AT_digit_count:
6702 attrs->at_digit_count = a;
6703 break;
6704 case DW_AT_discr:
6705 attrs->at_discr = a;
6706 break;
6707 case DW_AT_discr_list:
6708 attrs->at_discr_list = a;
6709 break;
6710 case DW_AT_discr_value:
6711 attrs->at_discr_value = a;
6712 break;
6713 case DW_AT_encoding:
6714 attrs->at_encoding = a;
6715 break;
6716 case DW_AT_endianity:
6717 attrs->at_endianity = a;
6718 break;
6719 case DW_AT_explicit:
6720 attrs->at_explicit = a;
6721 break;
6722 case DW_AT_is_optional:
6723 attrs->at_is_optional = a;
6724 break;
6725 case DW_AT_location:
6726 attrs->at_location = a;
6727 break;
6728 case DW_AT_lower_bound:
6729 attrs->at_lower_bound = a;
6730 break;
6731 case DW_AT_mutable:
6732 attrs->at_mutable = a;
6733 break;
6734 case DW_AT_ordering:
6735 attrs->at_ordering = a;
6736 break;
6737 case DW_AT_picture_string:
6738 attrs->at_picture_string = a;
6739 break;
6740 case DW_AT_prototyped:
6741 attrs->at_prototyped = a;
6742 break;
6743 case DW_AT_small:
6744 attrs->at_small = a;
6745 break;
6746 case DW_AT_segment:
6747 attrs->at_segment = a;
6748 break;
6749 case DW_AT_string_length:
6750 attrs->at_string_length = a;
6751 break;
6752 case DW_AT_string_length_bit_size:
6753 attrs->at_string_length_bit_size = a;
6754 break;
6755 case DW_AT_string_length_byte_size:
6756 attrs->at_string_length_byte_size = a;
6757 break;
6758 case DW_AT_threads_scaled:
6759 attrs->at_threads_scaled = a;
6760 break;
6761 case DW_AT_upper_bound:
6762 attrs->at_upper_bound = a;
6763 break;
6764 case DW_AT_use_location:
6765 attrs->at_use_location = a;
6766 break;
6767 case DW_AT_use_UTF8:
6768 attrs->at_use_UTF8 = a;
6769 break;
6770 case DW_AT_variable_parameter:
6771 attrs->at_variable_parameter = a;
6772 break;
6773 case DW_AT_virtuality:
6774 attrs->at_virtuality = a;
6775 break;
6776 case DW_AT_visibility:
6777 attrs->at_visibility = a;
6778 break;
6779 case DW_AT_vtable_elem_location:
6780 attrs->at_vtable_elem_location = a;
6781 break;
6782 default:
6783 break;
6788 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6790 static void
6791 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6793 dw_die_ref c;
6794 dw_die_ref decl;
6795 struct checksum_attributes attrs;
6797 CHECKSUM_ULEB128 ('D');
6798 CHECKSUM_ULEB128 (die->die_tag);
6800 memset (&attrs, 0, sizeof (attrs));
6802 decl = get_AT_ref (die, DW_AT_specification);
6803 if (decl != NULL)
6804 collect_checksum_attributes (&attrs, decl);
6805 collect_checksum_attributes (&attrs, die);
6807 CHECKSUM_ATTR (attrs.at_name);
6808 CHECKSUM_ATTR (attrs.at_accessibility);
6809 CHECKSUM_ATTR (attrs.at_address_class);
6810 CHECKSUM_ATTR (attrs.at_allocated);
6811 CHECKSUM_ATTR (attrs.at_artificial);
6812 CHECKSUM_ATTR (attrs.at_associated);
6813 CHECKSUM_ATTR (attrs.at_binary_scale);
6814 CHECKSUM_ATTR (attrs.at_bit_offset);
6815 CHECKSUM_ATTR (attrs.at_bit_size);
6816 CHECKSUM_ATTR (attrs.at_bit_stride);
6817 CHECKSUM_ATTR (attrs.at_byte_size);
6818 CHECKSUM_ATTR (attrs.at_byte_stride);
6819 CHECKSUM_ATTR (attrs.at_const_value);
6820 CHECKSUM_ATTR (attrs.at_containing_type);
6821 CHECKSUM_ATTR (attrs.at_count);
6822 CHECKSUM_ATTR (attrs.at_data_location);
6823 CHECKSUM_ATTR (attrs.at_data_member_location);
6824 CHECKSUM_ATTR (attrs.at_decimal_scale);
6825 CHECKSUM_ATTR (attrs.at_decimal_sign);
6826 CHECKSUM_ATTR (attrs.at_default_value);
6827 CHECKSUM_ATTR (attrs.at_digit_count);
6828 CHECKSUM_ATTR (attrs.at_discr);
6829 CHECKSUM_ATTR (attrs.at_discr_list);
6830 CHECKSUM_ATTR (attrs.at_discr_value);
6831 CHECKSUM_ATTR (attrs.at_encoding);
6832 CHECKSUM_ATTR (attrs.at_endianity);
6833 CHECKSUM_ATTR (attrs.at_explicit);
6834 CHECKSUM_ATTR (attrs.at_is_optional);
6835 CHECKSUM_ATTR (attrs.at_location);
6836 CHECKSUM_ATTR (attrs.at_lower_bound);
6837 CHECKSUM_ATTR (attrs.at_mutable);
6838 CHECKSUM_ATTR (attrs.at_ordering);
6839 CHECKSUM_ATTR (attrs.at_picture_string);
6840 CHECKSUM_ATTR (attrs.at_prototyped);
6841 CHECKSUM_ATTR (attrs.at_small);
6842 CHECKSUM_ATTR (attrs.at_segment);
6843 CHECKSUM_ATTR (attrs.at_string_length);
6844 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6845 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6846 CHECKSUM_ATTR (attrs.at_threads_scaled);
6847 CHECKSUM_ATTR (attrs.at_upper_bound);
6848 CHECKSUM_ATTR (attrs.at_use_location);
6849 CHECKSUM_ATTR (attrs.at_use_UTF8);
6850 CHECKSUM_ATTR (attrs.at_variable_parameter);
6851 CHECKSUM_ATTR (attrs.at_virtuality);
6852 CHECKSUM_ATTR (attrs.at_visibility);
6853 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6854 CHECKSUM_ATTR (attrs.at_type);
6855 CHECKSUM_ATTR (attrs.at_friend);
6857 /* Checksum the child DIEs. */
6858 c = die->die_child;
6859 if (c) do {
6860 dw_attr_node *name_attr;
6862 c = c->die_sib;
6863 name_attr = get_AT (c, DW_AT_name);
6864 if (is_template_instantiation (c))
6866 /* Ignore instantiations of member type and function templates. */
6868 else if (name_attr != NULL
6869 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6871 /* Use a shallow checksum for named nested types and member
6872 functions. */
6873 CHECKSUM_ULEB128 ('S');
6874 CHECKSUM_ULEB128 (c->die_tag);
6875 CHECKSUM_STRING (AT_string (name_attr));
6877 else
6879 /* Use a deep checksum for other children. */
6880 /* Mark this DIE so it gets processed when unmarking. */
6881 if (c->die_mark == 0)
6882 c->die_mark = -1;
6883 die_checksum_ordered (c, ctx, mark);
6885 } while (c != die->die_child);
6887 CHECKSUM_ULEB128 (0);
6890 /* Add a type name and tag to a hash. */
6891 static void
6892 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6894 CHECKSUM_ULEB128 (tag);
6895 CHECKSUM_STRING (name);
6898 #undef CHECKSUM
6899 #undef CHECKSUM_STRING
6900 #undef CHECKSUM_ATTR
6901 #undef CHECKSUM_LEB128
6902 #undef CHECKSUM_ULEB128
6904 /* Generate the type signature for DIE. This is computed by generating an
6905 MD5 checksum over the DIE's tag, its relevant attributes, and its
6906 children. Attributes that are references to other DIEs are processed
6907 by recursion, using the MARK field to prevent infinite recursion.
6908 If the DIE is nested inside a namespace or another type, we also
6909 need to include that context in the signature. The lower 64 bits
6910 of the resulting MD5 checksum comprise the signature. */
6912 static void
6913 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6915 int mark;
6916 const char *name;
6917 unsigned char checksum[16];
6918 struct md5_ctx ctx;
6919 dw_die_ref decl;
6920 dw_die_ref parent;
6922 name = get_AT_string (die, DW_AT_name);
6923 decl = get_AT_ref (die, DW_AT_specification);
6924 parent = get_die_parent (die);
6926 /* First, compute a signature for just the type name (and its surrounding
6927 context, if any. This is stored in the type unit DIE for link-time
6928 ODR (one-definition rule) checking. */
6930 if (is_cxx () && name != NULL)
6932 md5_init_ctx (&ctx);
6934 /* Checksum the names of surrounding namespaces and structures. */
6935 if (parent != NULL)
6936 checksum_die_context (parent, &ctx);
6938 /* Checksum the current DIE. */
6939 die_odr_checksum (die->die_tag, name, &ctx);
6940 md5_finish_ctx (&ctx, checksum);
6942 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6945 /* Next, compute the complete type signature. */
6947 md5_init_ctx (&ctx);
6948 mark = 1;
6949 die->die_mark = mark;
6951 /* Checksum the names of surrounding namespaces and structures. */
6952 if (parent != NULL)
6953 checksum_die_context (parent, &ctx);
6955 /* Checksum the DIE and its children. */
6956 die_checksum_ordered (die, &ctx, &mark);
6957 unmark_all_dies (die);
6958 md5_finish_ctx (&ctx, checksum);
6960 /* Store the signature in the type node and link the type DIE and the
6961 type node together. */
6962 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6963 DWARF_TYPE_SIGNATURE_SIZE);
6964 die->comdat_type_p = true;
6965 die->die_id.die_type_node = type_node;
6966 type_node->type_die = die;
6968 /* If the DIE is a specification, link its declaration to the type node
6969 as well. */
6970 if (decl != NULL)
6972 decl->comdat_type_p = true;
6973 decl->die_id.die_type_node = type_node;
6977 /* Do the location expressions look same? */
6978 static inline int
6979 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6981 return loc1->dw_loc_opc == loc2->dw_loc_opc
6982 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6983 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6986 /* Do the values look the same? */
6987 static int
6988 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6990 dw_loc_descr_ref loc1, loc2;
6991 rtx r1, r2;
6993 if (v1->val_class != v2->val_class)
6994 return 0;
6996 switch (v1->val_class)
6998 case dw_val_class_const:
6999 case dw_val_class_const_implicit:
7000 return v1->v.val_int == v2->v.val_int;
7001 case dw_val_class_unsigned_const:
7002 case dw_val_class_unsigned_const_implicit:
7003 return v1->v.val_unsigned == v2->v.val_unsigned;
7004 case dw_val_class_const_double:
7005 return v1->v.val_double.high == v2->v.val_double.high
7006 && v1->v.val_double.low == v2->v.val_double.low;
7007 case dw_val_class_wide_int:
7008 return *v1->v.val_wide == *v2->v.val_wide;
7009 case dw_val_class_vec:
7010 if (v1->v.val_vec.length != v2->v.val_vec.length
7011 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7012 return 0;
7013 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7014 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7015 return 0;
7016 return 1;
7017 case dw_val_class_flag:
7018 return v1->v.val_flag == v2->v.val_flag;
7019 case dw_val_class_str:
7020 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7022 case dw_val_class_addr:
7023 r1 = v1->v.val_addr;
7024 r2 = v2->v.val_addr;
7025 if (GET_CODE (r1) != GET_CODE (r2))
7026 return 0;
7027 return !rtx_equal_p (r1, r2);
7029 case dw_val_class_offset:
7030 return v1->v.val_offset == v2->v.val_offset;
7032 case dw_val_class_loc:
7033 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7034 loc1 && loc2;
7035 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7036 if (!same_loc_p (loc1, loc2, mark))
7037 return 0;
7038 return !loc1 && !loc2;
7040 case dw_val_class_die_ref:
7041 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7043 case dw_val_class_fde_ref:
7044 case dw_val_class_vms_delta:
7045 case dw_val_class_lbl_id:
7046 case dw_val_class_lineptr:
7047 case dw_val_class_macptr:
7048 case dw_val_class_loclistsptr:
7049 case dw_val_class_high_pc:
7050 return 1;
7052 case dw_val_class_file:
7053 case dw_val_class_file_implicit:
7054 return v1->v.val_file == v2->v.val_file;
7056 case dw_val_class_data8:
7057 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7059 default:
7060 return 1;
7064 /* Do the attributes look the same? */
7066 static int
7067 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7069 if (at1->dw_attr != at2->dw_attr)
7070 return 0;
7072 /* We don't care that this was compiled with a different compiler
7073 snapshot; if the output is the same, that's what matters. */
7074 if (at1->dw_attr == DW_AT_producer)
7075 return 1;
7077 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7080 /* Do the dies look the same? */
7082 static int
7083 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7085 dw_die_ref c1, c2;
7086 dw_attr_node *a1;
7087 unsigned ix;
7089 /* To avoid infinite recursion. */
7090 if (die1->die_mark)
7091 return die1->die_mark == die2->die_mark;
7092 die1->die_mark = die2->die_mark = ++(*mark);
7094 if (die1->die_tag != die2->die_tag)
7095 return 0;
7097 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7098 return 0;
7100 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7101 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7102 return 0;
7104 c1 = die1->die_child;
7105 c2 = die2->die_child;
7106 if (! c1)
7108 if (c2)
7109 return 0;
7111 else
7112 for (;;)
7114 if (!same_die_p (c1, c2, mark))
7115 return 0;
7116 c1 = c1->die_sib;
7117 c2 = c2->die_sib;
7118 if (c1 == die1->die_child)
7120 if (c2 == die2->die_child)
7121 break;
7122 else
7123 return 0;
7127 return 1;
7130 /* Do the dies look the same? Wrapper around same_die_p. */
7132 static int
7133 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7135 int mark = 0;
7136 int ret = same_die_p (die1, die2, &mark);
7138 unmark_all_dies (die1);
7139 unmark_all_dies (die2);
7141 return ret;
7144 /* The prefix to attach to symbols on DIEs in the current comdat debug
7145 info section. */
7146 static const char *comdat_symbol_id;
7148 /* The index of the current symbol within the current comdat CU. */
7149 static unsigned int comdat_symbol_number;
7151 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7152 children, and set comdat_symbol_id accordingly. */
7154 static void
7155 compute_section_prefix (dw_die_ref unit_die)
7157 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7158 const char *base = die_name ? lbasename (die_name) : "anonymous";
7159 char *name = XALLOCAVEC (char, strlen (base) + 64);
7160 char *p;
7161 int i, mark;
7162 unsigned char checksum[16];
7163 struct md5_ctx ctx;
7165 /* Compute the checksum of the DIE, then append part of it as hex digits to
7166 the name filename of the unit. */
7168 md5_init_ctx (&ctx);
7169 mark = 0;
7170 die_checksum (unit_die, &ctx, &mark);
7171 unmark_all_dies (unit_die);
7172 md5_finish_ctx (&ctx, checksum);
7174 sprintf (name, "%s.", base);
7175 clean_symbol_name (name);
7177 p = name + strlen (name);
7178 for (i = 0; i < 4; i++)
7180 sprintf (p, "%.2x", checksum[i]);
7181 p += 2;
7184 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7185 comdat_symbol_number = 0;
7188 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7190 static int
7191 is_type_die (dw_die_ref die)
7193 switch (die->die_tag)
7195 case DW_TAG_array_type:
7196 case DW_TAG_class_type:
7197 case DW_TAG_interface_type:
7198 case DW_TAG_enumeration_type:
7199 case DW_TAG_pointer_type:
7200 case DW_TAG_reference_type:
7201 case DW_TAG_rvalue_reference_type:
7202 case DW_TAG_string_type:
7203 case DW_TAG_structure_type:
7204 case DW_TAG_subroutine_type:
7205 case DW_TAG_union_type:
7206 case DW_TAG_ptr_to_member_type:
7207 case DW_TAG_set_type:
7208 case DW_TAG_subrange_type:
7209 case DW_TAG_base_type:
7210 case DW_TAG_const_type:
7211 case DW_TAG_file_type:
7212 case DW_TAG_packed_type:
7213 case DW_TAG_volatile_type:
7214 case DW_TAG_typedef:
7215 return 1;
7216 default:
7217 return 0;
7221 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7222 Basically, we want to choose the bits that are likely to be shared between
7223 compilations (types) and leave out the bits that are specific to individual
7224 compilations (functions). */
7226 static int
7227 is_comdat_die (dw_die_ref c)
7229 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7230 we do for stabs. The advantage is a greater likelihood of sharing between
7231 objects that don't include headers in the same order (and therefore would
7232 put the base types in a different comdat). jason 8/28/00 */
7234 if (c->die_tag == DW_TAG_base_type)
7235 return 0;
7237 if (c->die_tag == DW_TAG_pointer_type
7238 || c->die_tag == DW_TAG_reference_type
7239 || c->die_tag == DW_TAG_rvalue_reference_type
7240 || c->die_tag == DW_TAG_const_type
7241 || c->die_tag == DW_TAG_volatile_type)
7243 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7245 return t ? is_comdat_die (t) : 0;
7248 return is_type_die (c);
7251 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7252 compilation unit. */
7254 static int
7255 is_symbol_die (dw_die_ref c)
7257 return (is_type_die (c)
7258 || is_declaration_die (c)
7259 || c->die_tag == DW_TAG_namespace
7260 || c->die_tag == DW_TAG_module);
7263 /* Returns true iff C is a compile-unit DIE. */
7265 static inline bool
7266 is_cu_die (dw_die_ref c)
7268 return c && (c->die_tag == DW_TAG_compile_unit
7269 || c->die_tag == DW_TAG_skeleton_unit);
7272 /* Returns true iff C is a unit DIE of some sort. */
7274 static inline bool
7275 is_unit_die (dw_die_ref c)
7277 return c && (c->die_tag == DW_TAG_compile_unit
7278 || c->die_tag == DW_TAG_partial_unit
7279 || c->die_tag == DW_TAG_type_unit
7280 || c->die_tag == DW_TAG_skeleton_unit);
7283 /* Returns true iff C is a namespace DIE. */
7285 static inline bool
7286 is_namespace_die (dw_die_ref c)
7288 return c && c->die_tag == DW_TAG_namespace;
7291 /* Returns true iff C is a class or structure DIE. */
7293 static inline bool
7294 is_class_die (dw_die_ref c)
7296 return c && (c->die_tag == DW_TAG_class_type
7297 || c->die_tag == DW_TAG_structure_type);
7300 /* Return non-zero if this DIE is a template parameter. */
7302 static inline bool
7303 is_template_parameter (dw_die_ref die)
7305 switch (die->die_tag)
7307 case DW_TAG_template_type_param:
7308 case DW_TAG_template_value_param:
7309 case DW_TAG_GNU_template_template_param:
7310 case DW_TAG_GNU_template_parameter_pack:
7311 return true;
7312 default:
7313 return false;
7317 /* Return non-zero if this DIE represents a template instantiation. */
7319 static inline bool
7320 is_template_instantiation (dw_die_ref die)
7322 dw_die_ref c;
7324 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7325 return false;
7326 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7327 return false;
7330 static char *
7331 gen_internal_sym (const char *prefix)
7333 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7335 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7336 return xstrdup (buf);
7339 /* Assign symbols to all worthy DIEs under DIE. */
7341 static void
7342 assign_symbol_names (dw_die_ref die)
7344 dw_die_ref c;
7346 if (is_symbol_die (die) && !die->comdat_type_p)
7348 if (comdat_symbol_id)
7350 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7352 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7353 comdat_symbol_id, comdat_symbol_number++);
7354 die->die_id.die_symbol = xstrdup (p);
7356 else
7357 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7360 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7363 struct cu_hash_table_entry
7365 dw_die_ref cu;
7366 unsigned min_comdat_num, max_comdat_num;
7367 struct cu_hash_table_entry *next;
7370 /* Helpers to manipulate hash table of CUs. */
7372 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7374 typedef die_struct *compare_type;
7375 static inline hashval_t hash (const cu_hash_table_entry *);
7376 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7377 static inline void remove (cu_hash_table_entry *);
7380 inline hashval_t
7381 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7383 return htab_hash_string (entry->cu->die_id.die_symbol);
7386 inline bool
7387 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7388 const die_struct *entry2)
7390 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7393 inline void
7394 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7396 struct cu_hash_table_entry *next;
7398 while (entry)
7400 next = entry->next;
7401 free (entry);
7402 entry = next;
7406 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7408 /* Check whether we have already seen this CU and set up SYM_NUM
7409 accordingly. */
7410 static int
7411 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7413 struct cu_hash_table_entry dummy;
7414 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7416 dummy.max_comdat_num = 0;
7418 slot = htable->find_slot_with_hash (cu,
7419 htab_hash_string (cu->die_id.die_symbol),
7420 INSERT);
7421 entry = *slot;
7423 for (; entry; last = entry, entry = entry->next)
7425 if (same_die_p_wrap (cu, entry->cu))
7426 break;
7429 if (entry)
7431 *sym_num = entry->min_comdat_num;
7432 return 1;
7435 entry = XCNEW (struct cu_hash_table_entry);
7436 entry->cu = cu;
7437 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7438 entry->next = *slot;
7439 *slot = entry;
7441 return 0;
7444 /* Record SYM_NUM to record of CU in HTABLE. */
7445 static void
7446 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7447 unsigned int sym_num)
7449 struct cu_hash_table_entry **slot, *entry;
7451 slot = htable->find_slot_with_hash (cu,
7452 htab_hash_string (cu->die_id.die_symbol),
7453 NO_INSERT);
7454 entry = *slot;
7456 entry->max_comdat_num = sym_num;
7459 /* Traverse the DIE (which is always comp_unit_die), and set up
7460 additional compilation units for each of the include files we see
7461 bracketed by BINCL/EINCL. */
7463 static void
7464 break_out_includes (dw_die_ref die)
7466 dw_die_ref c;
7467 dw_die_ref unit = NULL;
7468 limbo_die_node *node, **pnode;
7470 c = die->die_child;
7471 if (c) do {
7472 dw_die_ref prev = c;
7473 c = c->die_sib;
7474 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7475 || (unit && is_comdat_die (c)))
7477 dw_die_ref next = c->die_sib;
7479 /* This DIE is for a secondary CU; remove it from the main one. */
7480 remove_child_with_prev (c, prev);
7482 if (c->die_tag == DW_TAG_GNU_BINCL)
7483 unit = push_new_compile_unit (unit, c);
7484 else if (c->die_tag == DW_TAG_GNU_EINCL)
7485 unit = pop_compile_unit (unit);
7486 else
7487 add_child_die (unit, c);
7488 c = next;
7489 if (c == die->die_child)
7490 break;
7492 } while (c != die->die_child);
7494 #if 0
7495 /* We can only use this in debugging, since the frontend doesn't check
7496 to make sure that we leave every include file we enter. */
7497 gcc_assert (!unit);
7498 #endif
7500 assign_symbol_names (die);
7501 cu_hash_type cu_hash_table (10);
7502 for (node = limbo_die_list, pnode = &limbo_die_list;
7503 node;
7504 node = node->next)
7506 int is_dupl;
7508 compute_section_prefix (node->die);
7509 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7510 &comdat_symbol_number);
7511 assign_symbol_names (node->die);
7512 if (is_dupl)
7513 *pnode = node->next;
7514 else
7516 pnode = &node->next;
7517 record_comdat_symbol_number (node->die, &cu_hash_table,
7518 comdat_symbol_number);
7523 /* Return non-zero if this DIE is a declaration. */
7525 static int
7526 is_declaration_die (dw_die_ref die)
7528 dw_attr_node *a;
7529 unsigned ix;
7531 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7532 if (a->dw_attr == DW_AT_declaration)
7533 return 1;
7535 return 0;
7538 /* Return non-zero if this DIE is nested inside a subprogram. */
7540 static int
7541 is_nested_in_subprogram (dw_die_ref die)
7543 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7545 if (decl == NULL)
7546 decl = die;
7547 return local_scope_p (decl);
7550 /* Return non-zero if this DIE contains a defining declaration of a
7551 subprogram. */
7553 static int
7554 contains_subprogram_definition (dw_die_ref die)
7556 dw_die_ref c;
7558 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7559 return 1;
7560 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7561 return 0;
7564 /* Return non-zero if this is a type DIE that should be moved to a
7565 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7566 unit type. */
7568 static int
7569 should_move_die_to_comdat (dw_die_ref die)
7571 switch (die->die_tag)
7573 case DW_TAG_class_type:
7574 case DW_TAG_structure_type:
7575 case DW_TAG_enumeration_type:
7576 case DW_TAG_union_type:
7577 /* Don't move declarations, inlined instances, types nested in a
7578 subprogram, or types that contain subprogram definitions. */
7579 if (is_declaration_die (die)
7580 || get_AT (die, DW_AT_abstract_origin)
7581 || is_nested_in_subprogram (die)
7582 || contains_subprogram_definition (die))
7583 return 0;
7584 return 1;
7585 case DW_TAG_array_type:
7586 case DW_TAG_interface_type:
7587 case DW_TAG_pointer_type:
7588 case DW_TAG_reference_type:
7589 case DW_TAG_rvalue_reference_type:
7590 case DW_TAG_string_type:
7591 case DW_TAG_subroutine_type:
7592 case DW_TAG_ptr_to_member_type:
7593 case DW_TAG_set_type:
7594 case DW_TAG_subrange_type:
7595 case DW_TAG_base_type:
7596 case DW_TAG_const_type:
7597 case DW_TAG_file_type:
7598 case DW_TAG_packed_type:
7599 case DW_TAG_volatile_type:
7600 case DW_TAG_typedef:
7601 default:
7602 return 0;
7606 /* Make a clone of DIE. */
7608 static dw_die_ref
7609 clone_die (dw_die_ref die)
7611 dw_die_ref clone;
7612 dw_attr_node *a;
7613 unsigned ix;
7615 clone = ggc_cleared_alloc<die_node> ();
7616 clone->die_tag = die->die_tag;
7618 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7619 add_dwarf_attr (clone, a);
7621 return clone;
7624 /* Make a clone of the tree rooted at DIE. */
7626 static dw_die_ref
7627 clone_tree (dw_die_ref die)
7629 dw_die_ref c;
7630 dw_die_ref clone = clone_die (die);
7632 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7634 return clone;
7637 /* Make a clone of DIE as a declaration. */
7639 static dw_die_ref
7640 clone_as_declaration (dw_die_ref die)
7642 dw_die_ref clone;
7643 dw_die_ref decl;
7644 dw_attr_node *a;
7645 unsigned ix;
7647 /* If the DIE is already a declaration, just clone it. */
7648 if (is_declaration_die (die))
7649 return clone_die (die);
7651 /* If the DIE is a specification, just clone its declaration DIE. */
7652 decl = get_AT_ref (die, DW_AT_specification);
7653 if (decl != NULL)
7655 clone = clone_die (decl);
7656 if (die->comdat_type_p)
7657 add_AT_die_ref (clone, DW_AT_signature, die);
7658 return clone;
7661 clone = ggc_cleared_alloc<die_node> ();
7662 clone->die_tag = die->die_tag;
7664 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7666 /* We don't want to copy over all attributes.
7667 For example we don't want DW_AT_byte_size because otherwise we will no
7668 longer have a declaration and GDB will treat it as a definition. */
7670 switch (a->dw_attr)
7672 case DW_AT_abstract_origin:
7673 case DW_AT_artificial:
7674 case DW_AT_containing_type:
7675 case DW_AT_external:
7676 case DW_AT_name:
7677 case DW_AT_type:
7678 case DW_AT_virtuality:
7679 case DW_AT_linkage_name:
7680 case DW_AT_MIPS_linkage_name:
7681 add_dwarf_attr (clone, a);
7682 break;
7683 case DW_AT_byte_size:
7684 default:
7685 break;
7689 if (die->comdat_type_p)
7690 add_AT_die_ref (clone, DW_AT_signature, die);
7692 add_AT_flag (clone, DW_AT_declaration, 1);
7693 return clone;
7697 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7699 struct decl_table_entry
7701 dw_die_ref orig;
7702 dw_die_ref copy;
7705 /* Helpers to manipulate hash table of copied declarations. */
7707 /* Hashtable helpers. */
7709 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7711 typedef die_struct *compare_type;
7712 static inline hashval_t hash (const decl_table_entry *);
7713 static inline bool equal (const decl_table_entry *, const die_struct *);
7716 inline hashval_t
7717 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7719 return htab_hash_pointer (entry->orig);
7722 inline bool
7723 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7724 const die_struct *entry2)
7726 return entry1->orig == entry2;
7729 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7731 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7732 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7733 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7734 to check if the ancestor has already been copied into UNIT. */
7736 static dw_die_ref
7737 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7738 decl_hash_type *decl_table)
7740 dw_die_ref parent = die->die_parent;
7741 dw_die_ref new_parent = unit;
7742 dw_die_ref copy;
7743 decl_table_entry **slot = NULL;
7744 struct decl_table_entry *entry = NULL;
7746 if (decl_table)
7748 /* Check if the entry has already been copied to UNIT. */
7749 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7750 INSERT);
7751 if (*slot != HTAB_EMPTY_ENTRY)
7753 entry = *slot;
7754 return entry->copy;
7757 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7758 entry = XCNEW (struct decl_table_entry);
7759 entry->orig = die;
7760 entry->copy = NULL;
7761 *slot = entry;
7764 if (parent != NULL)
7766 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7767 if (spec != NULL)
7768 parent = spec;
7769 if (!is_unit_die (parent))
7770 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7773 copy = clone_as_declaration (die);
7774 add_child_die (new_parent, copy);
7776 if (decl_table)
7778 /* Record the pointer to the copy. */
7779 entry->copy = copy;
7782 return copy;
7784 /* Copy the declaration context to the new type unit DIE. This includes
7785 any surrounding namespace or type declarations. If the DIE has an
7786 AT_specification attribute, it also includes attributes and children
7787 attached to the specification, and returns a pointer to the original
7788 parent of the declaration DIE. Returns NULL otherwise. */
7790 static dw_die_ref
7791 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7793 dw_die_ref decl;
7794 dw_die_ref new_decl;
7795 dw_die_ref orig_parent = NULL;
7797 decl = get_AT_ref (die, DW_AT_specification);
7798 if (decl == NULL)
7799 decl = die;
7800 else
7802 unsigned ix;
7803 dw_die_ref c;
7804 dw_attr_node *a;
7806 /* The original DIE will be changed to a declaration, and must
7807 be moved to be a child of the original declaration DIE. */
7808 orig_parent = decl->die_parent;
7810 /* Copy the type node pointer from the new DIE to the original
7811 declaration DIE so we can forward references later. */
7812 decl->comdat_type_p = true;
7813 decl->die_id.die_type_node = die->die_id.die_type_node;
7815 remove_AT (die, DW_AT_specification);
7817 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7819 if (a->dw_attr != DW_AT_name
7820 && a->dw_attr != DW_AT_declaration
7821 && a->dw_attr != DW_AT_external)
7822 add_dwarf_attr (die, a);
7825 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7828 if (decl->die_parent != NULL
7829 && !is_unit_die (decl->die_parent))
7831 new_decl = copy_ancestor_tree (unit, decl, NULL);
7832 if (new_decl != NULL)
7834 remove_AT (new_decl, DW_AT_signature);
7835 add_AT_specification (die, new_decl);
7839 return orig_parent;
7842 /* Generate the skeleton ancestor tree for the given NODE, then clone
7843 the DIE and add the clone into the tree. */
7845 static void
7846 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7848 if (node->new_die != NULL)
7849 return;
7851 node->new_die = clone_as_declaration (node->old_die);
7853 if (node->parent != NULL)
7855 generate_skeleton_ancestor_tree (node->parent);
7856 add_child_die (node->parent->new_die, node->new_die);
7860 /* Generate a skeleton tree of DIEs containing any declarations that are
7861 found in the original tree. We traverse the tree looking for declaration
7862 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7864 static void
7865 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7867 skeleton_chain_node node;
7868 dw_die_ref c;
7869 dw_die_ref first;
7870 dw_die_ref prev = NULL;
7871 dw_die_ref next = NULL;
7873 node.parent = parent;
7875 first = c = parent->old_die->die_child;
7876 if (c)
7877 next = c->die_sib;
7878 if (c) do {
7879 if (prev == NULL || prev->die_sib == c)
7880 prev = c;
7881 c = next;
7882 next = (c == first ? NULL : c->die_sib);
7883 node.old_die = c;
7884 node.new_die = NULL;
7885 if (is_declaration_die (c))
7887 if (is_template_instantiation (c))
7889 /* Instantiated templates do not need to be cloned into the
7890 type unit. Just move the DIE and its children back to
7891 the skeleton tree (in the main CU). */
7892 remove_child_with_prev (c, prev);
7893 add_child_die (parent->new_die, c);
7894 c = prev;
7896 else
7898 /* Clone the existing DIE, move the original to the skeleton
7899 tree (which is in the main CU), and put the clone, with
7900 all the original's children, where the original came from
7901 (which is about to be moved to the type unit). */
7902 dw_die_ref clone = clone_die (c);
7903 move_all_children (c, clone);
7905 /* If the original has a DW_AT_object_pointer attribute,
7906 it would now point to a child DIE just moved to the
7907 cloned tree, so we need to remove that attribute from
7908 the original. */
7909 remove_AT (c, DW_AT_object_pointer);
7911 replace_child (c, clone, prev);
7912 generate_skeleton_ancestor_tree (parent);
7913 add_child_die (parent->new_die, c);
7914 node.new_die = c;
7915 c = clone;
7918 generate_skeleton_bottom_up (&node);
7919 } while (next != NULL);
7922 /* Wrapper function for generate_skeleton_bottom_up. */
7924 static dw_die_ref
7925 generate_skeleton (dw_die_ref die)
7927 skeleton_chain_node node;
7929 node.old_die = die;
7930 node.new_die = NULL;
7931 node.parent = NULL;
7933 /* If this type definition is nested inside another type,
7934 and is not an instantiation of a template, always leave
7935 at least a declaration in its place. */
7936 if (die->die_parent != NULL
7937 && is_type_die (die->die_parent)
7938 && !is_template_instantiation (die))
7939 node.new_die = clone_as_declaration (die);
7941 generate_skeleton_bottom_up (&node);
7942 return node.new_die;
7945 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7946 declaration. The original DIE is moved to a new compile unit so that
7947 existing references to it follow it to the new location. If any of the
7948 original DIE's descendants is a declaration, we need to replace the
7949 original DIE with a skeleton tree and move the declarations back into the
7950 skeleton tree. */
7952 static dw_die_ref
7953 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7954 dw_die_ref prev)
7956 dw_die_ref skeleton, orig_parent;
7958 /* Copy the declaration context to the type unit DIE. If the returned
7959 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7960 that DIE. */
7961 orig_parent = copy_declaration_context (unit, child);
7963 skeleton = generate_skeleton (child);
7964 if (skeleton == NULL)
7965 remove_child_with_prev (child, prev);
7966 else
7968 skeleton->comdat_type_p = true;
7969 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7971 /* If the original DIE was a specification, we need to put
7972 the skeleton under the parent DIE of the declaration.
7973 This leaves the original declaration in the tree, but
7974 it will be pruned later since there are no longer any
7975 references to it. */
7976 if (orig_parent != NULL)
7978 remove_child_with_prev (child, prev);
7979 add_child_die (orig_parent, skeleton);
7981 else
7982 replace_child (child, skeleton, prev);
7985 return skeleton;
7988 static void
7989 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7990 comdat_type_node *type_node,
7991 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7993 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7994 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7995 DWARF procedure references in the DW_AT_location attribute. */
7997 static dw_die_ref
7998 copy_dwarf_procedure (dw_die_ref die,
7999 comdat_type_node *type_node,
8000 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8002 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8004 /* DWARF procedures are not supposed to have children... */
8005 gcc_assert (die->die_child == NULL);
8007 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8008 gcc_assert (vec_safe_length (die->die_attr) == 1
8009 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8011 /* Do not copy more than once DWARF procedures. */
8012 bool existed;
8013 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8014 if (existed)
8015 return die_copy;
8017 die_copy = clone_die (die);
8018 add_child_die (type_node->root_die, die_copy);
8019 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8020 return die_copy;
8023 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8024 procedures in DIE's attributes. */
8026 static void
8027 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8028 comdat_type_node *type_node,
8029 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8031 dw_attr_node *a;
8032 unsigned i;
8034 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8036 dw_loc_descr_ref loc;
8038 if (a->dw_attr_val.val_class != dw_val_class_loc)
8039 continue;
8041 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8043 switch (loc->dw_loc_opc)
8045 case DW_OP_call2:
8046 case DW_OP_call4:
8047 case DW_OP_call_ref:
8048 gcc_assert (loc->dw_loc_oprnd1.val_class
8049 == dw_val_class_die_ref);
8050 loc->dw_loc_oprnd1.v.val_die_ref.die
8051 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8052 type_node,
8053 copied_dwarf_procs);
8055 default:
8056 break;
8062 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8063 rewrite references to point to the copies.
8065 References are looked for in DIE's attributes and recursively in all its
8066 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8067 mapping from old DWARF procedures to their copy. It is used not to copy
8068 twice the same DWARF procedure under TYPE_NODE. */
8070 static void
8071 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8072 comdat_type_node *type_node,
8073 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8075 dw_die_ref c;
8077 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8078 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8079 type_node,
8080 copied_dwarf_procs));
8083 /* Traverse the DIE and set up additional .debug_types or .debug_info
8084 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8085 section. */
8087 static void
8088 break_out_comdat_types (dw_die_ref die)
8090 dw_die_ref c;
8091 dw_die_ref first;
8092 dw_die_ref prev = NULL;
8093 dw_die_ref next = NULL;
8094 dw_die_ref unit = NULL;
8096 first = c = die->die_child;
8097 if (c)
8098 next = c->die_sib;
8099 if (c) do {
8100 if (prev == NULL || prev->die_sib == c)
8101 prev = c;
8102 c = next;
8103 next = (c == first ? NULL : c->die_sib);
8104 if (should_move_die_to_comdat (c))
8106 dw_die_ref replacement;
8107 comdat_type_node *type_node;
8109 /* Break out nested types into their own type units. */
8110 break_out_comdat_types (c);
8112 /* Create a new type unit DIE as the root for the new tree, and
8113 add it to the list of comdat types. */
8114 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8115 add_AT_unsigned (unit, DW_AT_language,
8116 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8117 type_node = ggc_cleared_alloc<comdat_type_node> ();
8118 type_node->root_die = unit;
8119 type_node->next = comdat_type_list;
8120 comdat_type_list = type_node;
8122 /* Generate the type signature. */
8123 generate_type_signature (c, type_node);
8125 /* Copy the declaration context, attributes, and children of the
8126 declaration into the new type unit DIE, then remove this DIE
8127 from the main CU (or replace it with a skeleton if necessary). */
8128 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8129 type_node->skeleton_die = replacement;
8131 /* Add the DIE to the new compunit. */
8132 add_child_die (unit, c);
8134 /* Types can reference DWARF procedures for type size or data location
8135 expressions. Calls in DWARF expressions cannot target procedures
8136 that are not in the same section. So we must copy DWARF procedures
8137 along with this type and then rewrite references to them. */
8138 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8139 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8141 if (replacement != NULL)
8142 c = replacement;
8144 else if (c->die_tag == DW_TAG_namespace
8145 || c->die_tag == DW_TAG_class_type
8146 || c->die_tag == DW_TAG_structure_type
8147 || c->die_tag == DW_TAG_union_type)
8149 /* Look for nested types that can be broken out. */
8150 break_out_comdat_types (c);
8152 } while (next != NULL);
8155 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8156 Enter all the cloned children into the hash table decl_table. */
8158 static dw_die_ref
8159 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8161 dw_die_ref c;
8162 dw_die_ref clone;
8163 struct decl_table_entry *entry;
8164 decl_table_entry **slot;
8166 if (die->die_tag == DW_TAG_subprogram)
8167 clone = clone_as_declaration (die);
8168 else
8169 clone = clone_die (die);
8171 slot = decl_table->find_slot_with_hash (die,
8172 htab_hash_pointer (die), INSERT);
8174 /* Assert that DIE isn't in the hash table yet. If it would be there
8175 before, the ancestors would be necessarily there as well, therefore
8176 clone_tree_partial wouldn't be called. */
8177 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8179 entry = XCNEW (struct decl_table_entry);
8180 entry->orig = die;
8181 entry->copy = clone;
8182 *slot = entry;
8184 if (die->die_tag != DW_TAG_subprogram)
8185 FOR_EACH_CHILD (die, c,
8186 add_child_die (clone, clone_tree_partial (c, decl_table)));
8188 return clone;
8191 /* Walk the DIE and its children, looking for references to incomplete
8192 or trivial types that are unmarked (i.e., that are not in the current
8193 type_unit). */
8195 static void
8196 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8198 dw_die_ref c;
8199 dw_attr_node *a;
8200 unsigned ix;
8202 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8204 if (AT_class (a) == dw_val_class_die_ref)
8206 dw_die_ref targ = AT_ref (a);
8207 decl_table_entry **slot;
8208 struct decl_table_entry *entry;
8210 if (targ->die_mark != 0 || targ->comdat_type_p)
8211 continue;
8213 slot = decl_table->find_slot_with_hash (targ,
8214 htab_hash_pointer (targ),
8215 INSERT);
8217 if (*slot != HTAB_EMPTY_ENTRY)
8219 /* TARG has already been copied, so we just need to
8220 modify the reference to point to the copy. */
8221 entry = *slot;
8222 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8224 else
8226 dw_die_ref parent = unit;
8227 dw_die_ref copy = clone_die (targ);
8229 /* Record in DECL_TABLE that TARG has been copied.
8230 Need to do this now, before the recursive call,
8231 because DECL_TABLE may be expanded and SLOT
8232 would no longer be a valid pointer. */
8233 entry = XCNEW (struct decl_table_entry);
8234 entry->orig = targ;
8235 entry->copy = copy;
8236 *slot = entry;
8238 /* If TARG is not a declaration DIE, we need to copy its
8239 children. */
8240 if (!is_declaration_die (targ))
8242 FOR_EACH_CHILD (
8243 targ, c,
8244 add_child_die (copy,
8245 clone_tree_partial (c, decl_table)));
8248 /* Make sure the cloned tree is marked as part of the
8249 type unit. */
8250 mark_dies (copy);
8252 /* If TARG has surrounding context, copy its ancestor tree
8253 into the new type unit. */
8254 if (targ->die_parent != NULL
8255 && !is_unit_die (targ->die_parent))
8256 parent = copy_ancestor_tree (unit, targ->die_parent,
8257 decl_table);
8259 add_child_die (parent, copy);
8260 a->dw_attr_val.v.val_die_ref.die = copy;
8262 /* Make sure the newly-copied DIE is walked. If it was
8263 installed in a previously-added context, it won't
8264 get visited otherwise. */
8265 if (parent != unit)
8267 /* Find the highest point of the newly-added tree,
8268 mark each node along the way, and walk from there. */
8269 parent->die_mark = 1;
8270 while (parent->die_parent
8271 && parent->die_parent->die_mark == 0)
8273 parent = parent->die_parent;
8274 parent->die_mark = 1;
8276 copy_decls_walk (unit, parent, decl_table);
8282 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8285 /* Copy declarations for "unworthy" types into the new comdat section.
8286 Incomplete types, modified types, and certain other types aren't broken
8287 out into comdat sections of their own, so they don't have a signature,
8288 and we need to copy the declaration into the same section so that we
8289 don't have an external reference. */
8291 static void
8292 copy_decls_for_unworthy_types (dw_die_ref unit)
8294 mark_dies (unit);
8295 decl_hash_type decl_table (10);
8296 copy_decls_walk (unit, unit, &decl_table);
8297 unmark_dies (unit);
8300 /* Traverse the DIE and add a sibling attribute if it may have the
8301 effect of speeding up access to siblings. To save some space,
8302 avoid generating sibling attributes for DIE's without children. */
8304 static void
8305 add_sibling_attributes (dw_die_ref die)
8307 dw_die_ref c;
8309 if (! die->die_child)
8310 return;
8312 if (die->die_parent && die != die->die_parent->die_child)
8313 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8315 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8318 /* Output all location lists for the DIE and its children. */
8320 static void
8321 output_location_lists (dw_die_ref die)
8323 dw_die_ref c;
8324 dw_attr_node *a;
8325 unsigned ix;
8327 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8328 if (AT_class (a) == dw_val_class_loc_list)
8329 output_loc_list (AT_loc_list (a));
8331 FOR_EACH_CHILD (die, c, output_location_lists (c));
8334 /* During assign_location_list_indexes and output_loclists_offset the
8335 current index, after it the number of assigned indexes (i.e. how
8336 large the .debug_loclists* offset table should be). */
8337 static unsigned int loc_list_idx;
8339 /* Output all location list offsets for the DIE and its children. */
8341 static void
8342 output_loclists_offsets (dw_die_ref die)
8344 dw_die_ref c;
8345 dw_attr_node *a;
8346 unsigned ix;
8348 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8349 if (AT_class (a) == dw_val_class_loc_list)
8351 dw_loc_list_ref l = AT_loc_list (a);
8352 if (l->offset_emitted)
8353 continue;
8354 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8355 loc_section_label, NULL);
8356 gcc_assert (l->hash == loc_list_idx);
8357 loc_list_idx++;
8358 l->offset_emitted = true;
8361 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8364 /* Recursively set indexes of location lists. */
8366 static void
8367 assign_location_list_indexes (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 list = AT_loc_list (a);
8377 if (!list->num_assigned)
8379 list->num_assigned = true;
8380 list->hash = loc_list_idx++;
8384 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8387 /* We want to limit the number of external references, because they are
8388 larger than local references: a relocation takes multiple words, and
8389 even a sig8 reference is always eight bytes, whereas a local reference
8390 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8391 So if we encounter multiple external references to the same type DIE, we
8392 make a local typedef stub for it and redirect all references there.
8394 This is the element of the hash table for keeping track of these
8395 references. */
8397 struct external_ref
8399 dw_die_ref type;
8400 dw_die_ref stub;
8401 unsigned n_refs;
8404 /* Hashtable helpers. */
8406 struct external_ref_hasher : free_ptr_hash <external_ref>
8408 static inline hashval_t hash (const external_ref *);
8409 static inline bool equal (const external_ref *, const external_ref *);
8412 inline hashval_t
8413 external_ref_hasher::hash (const external_ref *r)
8415 dw_die_ref die = r->type;
8416 hashval_t h = 0;
8418 /* We can't use the address of the DIE for hashing, because
8419 that will make the order of the stub DIEs non-deterministic. */
8420 if (! die->comdat_type_p)
8421 /* We have a symbol; use it to compute a hash. */
8422 h = htab_hash_string (die->die_id.die_symbol);
8423 else
8425 /* We have a type signature; use a subset of the bits as the hash.
8426 The 8-byte signature is at least as large as hashval_t. */
8427 comdat_type_node *type_node = die->die_id.die_type_node;
8428 memcpy (&h, type_node->signature, sizeof (h));
8430 return h;
8433 inline bool
8434 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8436 return r1->type == r2->type;
8439 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8441 /* Return a pointer to the external_ref for references to DIE. */
8443 static struct external_ref *
8444 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8446 struct external_ref ref, *ref_p;
8447 external_ref **slot;
8449 ref.type = die;
8450 slot = map->find_slot (&ref, INSERT);
8451 if (*slot != HTAB_EMPTY_ENTRY)
8452 return *slot;
8454 ref_p = XCNEW (struct external_ref);
8455 ref_p->type = die;
8456 *slot = ref_p;
8457 return ref_p;
8460 /* Subroutine of optimize_external_refs, below.
8462 If we see a type skeleton, record it as our stub. If we see external
8463 references, remember how many we've seen. */
8465 static void
8466 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8468 dw_die_ref c;
8469 dw_attr_node *a;
8470 unsigned ix;
8471 struct external_ref *ref_p;
8473 if (is_type_die (die)
8474 && (c = get_AT_ref (die, DW_AT_signature)))
8476 /* This is a local skeleton; use it for local references. */
8477 ref_p = lookup_external_ref (map, c);
8478 ref_p->stub = die;
8481 /* Scan the DIE references, and remember any that refer to DIEs from
8482 other CUs (i.e. those which are not marked). */
8483 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8484 if (AT_class (a) == dw_val_class_die_ref
8485 && (c = AT_ref (a))->die_mark == 0
8486 && is_type_die (c))
8488 ref_p = lookup_external_ref (map, c);
8489 ref_p->n_refs++;
8492 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8495 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8496 points to an external_ref, DATA is the CU we're processing. If we don't
8497 already have a local stub, and we have multiple refs, build a stub. */
8500 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8502 struct external_ref *ref_p = *slot;
8504 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8506 /* We have multiple references to this type, so build a small stub.
8507 Both of these forms are a bit dodgy from the perspective of the
8508 DWARF standard, since technically they should have names. */
8509 dw_die_ref cu = data;
8510 dw_die_ref type = ref_p->type;
8511 dw_die_ref stub = NULL;
8513 if (type->comdat_type_p)
8515 /* If we refer to this type via sig8, use AT_signature. */
8516 stub = new_die (type->die_tag, cu, NULL_TREE);
8517 add_AT_die_ref (stub, DW_AT_signature, type);
8519 else
8521 /* Otherwise, use a typedef with no name. */
8522 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8523 add_AT_die_ref (stub, DW_AT_type, type);
8526 stub->die_mark++;
8527 ref_p->stub = stub;
8529 return 1;
8532 /* DIE is a unit; look through all the DIE references to see if there are
8533 any external references to types, and if so, create local stubs for
8534 them which will be applied in build_abbrev_table. This is useful because
8535 references to local DIEs are smaller. */
8537 static external_ref_hash_type *
8538 optimize_external_refs (dw_die_ref die)
8540 external_ref_hash_type *map = new external_ref_hash_type (10);
8541 optimize_external_refs_1 (die, map);
8542 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8543 return map;
8546 /* The following 3 variables are temporaries that are computed only during the
8547 build_abbrev_table call and used and released during the following
8548 optimize_abbrev_table call. */
8550 /* First abbrev_id that can be optimized based on usage. */
8551 static unsigned int abbrev_opt_start;
8553 /* Vector of usage counts during build_abbrev_table. Indexed by
8554 abbrev_id - abbrev_opt_start. */
8555 static vec<unsigned int> abbrev_usage_count;
8557 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8558 static vec<dw_die_ref> sorted_abbrev_dies;
8560 /* The format of each DIE (and its attribute value pairs) is encoded in an
8561 abbreviation table. This routine builds the abbreviation table and assigns
8562 a unique abbreviation id for each abbreviation entry. The children of each
8563 die are visited recursively. */
8565 static void
8566 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8568 unsigned int abbrev_id = 0;
8569 dw_die_ref c;
8570 dw_attr_node *a;
8571 unsigned ix;
8572 dw_die_ref abbrev;
8574 /* Scan the DIE references, and replace any that refer to
8575 DIEs from other CUs (i.e. those which are not marked) with
8576 the local stubs we built in optimize_external_refs. */
8577 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8578 if (AT_class (a) == dw_val_class_die_ref
8579 && (c = AT_ref (a))->die_mark == 0)
8581 struct external_ref *ref_p;
8582 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8584 ref_p = lookup_external_ref (extern_map, c);
8585 if (ref_p->stub && ref_p->stub != die)
8586 change_AT_die_ref (a, ref_p->stub);
8587 else
8588 /* We aren't changing this reference, so mark it external. */
8589 set_AT_ref_external (a, 1);
8592 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8594 dw_attr_node *die_a, *abbrev_a;
8595 unsigned ix;
8596 bool ok = true;
8598 if (abbrev_id == 0)
8599 continue;
8600 if (abbrev->die_tag != die->die_tag)
8601 continue;
8602 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8603 continue;
8605 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8606 continue;
8608 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8610 abbrev_a = &(*abbrev->die_attr)[ix];
8611 if ((abbrev_a->dw_attr != die_a->dw_attr)
8612 || (value_format (abbrev_a) != value_format (die_a)))
8614 ok = false;
8615 break;
8618 if (ok)
8619 break;
8622 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8624 vec_safe_push (abbrev_die_table, die);
8625 if (abbrev_opt_start)
8626 abbrev_usage_count.safe_push (0);
8628 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8630 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8631 sorted_abbrev_dies.safe_push (die);
8634 die->die_abbrev = abbrev_id;
8635 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8638 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8639 by die_abbrev's usage count, from the most commonly used
8640 abbreviation to the least. */
8642 static int
8643 die_abbrev_cmp (const void *p1, const void *p2)
8645 dw_die_ref die1 = *(const dw_die_ref *) p1;
8646 dw_die_ref die2 = *(const dw_die_ref *) p2;
8648 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8649 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8651 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8652 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8653 return -1;
8654 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8655 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8656 return 1;
8658 /* Stabilize the sort. */
8659 if (die1->die_abbrev < die2->die_abbrev)
8660 return -1;
8661 if (die1->die_abbrev > die2->die_abbrev)
8662 return 1;
8664 return 0;
8667 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8668 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8669 into dw_val_class_const_implicit or
8670 dw_val_class_unsigned_const_implicit. */
8672 static void
8673 optimize_implicit_const (unsigned int first_id, unsigned int end,
8674 vec<bool> &implicit_consts)
8676 /* It never makes sense if there is just one DIE using the abbreviation. */
8677 if (end < first_id + 2)
8678 return;
8680 dw_attr_node *a;
8681 unsigned ix, i;
8682 dw_die_ref die = sorted_abbrev_dies[first_id];
8683 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8684 if (implicit_consts[ix])
8686 enum dw_val_class new_class = dw_val_class_none;
8687 switch (AT_class (a))
8689 case dw_val_class_unsigned_const:
8690 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8691 continue;
8693 /* The .debug_abbrev section will grow by
8694 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8695 in all the DIEs using that abbreviation. */
8696 if (constant_size (AT_unsigned (a)) * (end - first_id)
8697 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8698 continue;
8700 new_class = dw_val_class_unsigned_const_implicit;
8701 break;
8703 case dw_val_class_const:
8704 new_class = dw_val_class_const_implicit;
8705 break;
8707 case dw_val_class_file:
8708 new_class = dw_val_class_file_implicit;
8709 break;
8711 default:
8712 continue;
8714 for (i = first_id; i < end; i++)
8715 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8716 = new_class;
8720 /* Attempt to optimize abbreviation table from abbrev_opt_start
8721 abbreviation above. */
8723 static void
8724 optimize_abbrev_table (void)
8726 if (abbrev_opt_start
8727 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8728 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8730 auto_vec<bool, 32> implicit_consts;
8731 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8733 unsigned int abbrev_id = abbrev_opt_start - 1;
8734 unsigned int first_id = 0;
8735 unsigned int last_abbrev_id = 0;
8736 unsigned int i;
8737 dw_die_ref die;
8738 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8739 most commonly used abbreviations come first. */
8740 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8742 dw_attr_node *a;
8743 unsigned ix;
8745 if (die->die_abbrev != last_abbrev_id)
8747 last_abbrev_id = die->die_abbrev;
8748 if (dwarf_version >= 5 && i)
8749 optimize_implicit_const (first_id, i, implicit_consts);
8750 abbrev_id++;
8751 (*abbrev_die_table)[abbrev_id] = die;
8752 if (dwarf_version >= 5)
8754 first_id = i;
8755 implicit_consts.truncate (0);
8757 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8758 switch (AT_class (a))
8760 case dw_val_class_const:
8761 case dw_val_class_unsigned_const:
8762 case dw_val_class_file:
8763 implicit_consts.safe_push (true);
8764 break;
8765 default:
8766 implicit_consts.safe_push (false);
8767 break;
8771 else if (dwarf_version >= 5)
8773 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8774 if (!implicit_consts[ix])
8775 continue;
8776 else
8778 dw_attr_node *other_a
8779 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8780 if (!dw_val_equal_p (&a->dw_attr_val,
8781 &other_a->dw_attr_val))
8782 implicit_consts[ix] = false;
8785 die->die_abbrev = abbrev_id;
8787 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8788 if (dwarf_version >= 5)
8789 optimize_implicit_const (first_id, i, implicit_consts);
8792 abbrev_opt_start = 0;
8793 abbrev_usage_count.release ();
8794 sorted_abbrev_dies.release ();
8797 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8799 static int
8800 constant_size (unsigned HOST_WIDE_INT value)
8802 int log;
8804 if (value == 0)
8805 log = 0;
8806 else
8807 log = floor_log2 (value);
8809 log = log / 8;
8810 log = 1 << (floor_log2 (log) + 1);
8812 return log;
8815 /* Return the size of a DIE as it is represented in the
8816 .debug_info section. */
8818 static unsigned long
8819 size_of_die (dw_die_ref die)
8821 unsigned long size = 0;
8822 dw_attr_node *a;
8823 unsigned ix;
8824 enum dwarf_form form;
8826 size += size_of_uleb128 (die->die_abbrev);
8827 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8829 switch (AT_class (a))
8831 case dw_val_class_addr:
8832 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8834 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8835 size += size_of_uleb128 (AT_index (a));
8837 else
8838 size += DWARF2_ADDR_SIZE;
8839 break;
8840 case dw_val_class_offset:
8841 size += DWARF_OFFSET_SIZE;
8842 break;
8843 case dw_val_class_loc:
8845 unsigned long lsize = size_of_locs (AT_loc (a));
8847 /* Block length. */
8848 if (dwarf_version >= 4)
8849 size += size_of_uleb128 (lsize);
8850 else
8851 size += constant_size (lsize);
8852 size += lsize;
8854 break;
8855 case dw_val_class_loc_list:
8856 if (dwarf_split_debug_info && dwarf_version >= 5)
8858 gcc_assert (AT_loc_list (a)->num_assigned);
8859 size += size_of_uleb128 (AT_loc_list (a)->hash);
8861 else
8862 size += DWARF_OFFSET_SIZE;
8863 break;
8864 case dw_val_class_range_list:
8865 if (value_format (a) == DW_FORM_rnglistx)
8867 gcc_assert (rnglist_idx);
8868 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8869 size += size_of_uleb128 (r->idx);
8871 else
8872 size += DWARF_OFFSET_SIZE;
8873 break;
8874 case dw_val_class_const:
8875 size += size_of_sleb128 (AT_int (a));
8876 break;
8877 case dw_val_class_unsigned_const:
8879 int csize = constant_size (AT_unsigned (a));
8880 if (dwarf_version == 3
8881 && a->dw_attr == DW_AT_data_member_location
8882 && csize >= 4)
8883 size += size_of_uleb128 (AT_unsigned (a));
8884 else
8885 size += csize;
8887 break;
8888 case dw_val_class_const_implicit:
8889 case dw_val_class_unsigned_const_implicit:
8890 case dw_val_class_file_implicit:
8891 /* These occupy no size in the DIE, just an extra sleb128 in
8892 .debug_abbrev. */
8893 break;
8894 case dw_val_class_const_double:
8895 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8896 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8897 size++; /* block */
8898 break;
8899 case dw_val_class_wide_int:
8900 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8901 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8902 if (get_full_len (*a->dw_attr_val.v.val_wide)
8903 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
8904 size++; /* block */
8905 break;
8906 case dw_val_class_vec:
8907 size += constant_size (a->dw_attr_val.v.val_vec.length
8908 * a->dw_attr_val.v.val_vec.elt_size)
8909 + a->dw_attr_val.v.val_vec.length
8910 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8911 break;
8912 case dw_val_class_flag:
8913 if (dwarf_version >= 4)
8914 /* Currently all add_AT_flag calls pass in 1 as last argument,
8915 so DW_FORM_flag_present can be used. If that ever changes,
8916 we'll need to use DW_FORM_flag and have some optimization
8917 in build_abbrev_table that will change those to
8918 DW_FORM_flag_present if it is set to 1 in all DIEs using
8919 the same abbrev entry. */
8920 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8921 else
8922 size += 1;
8923 break;
8924 case dw_val_class_die_ref:
8925 if (AT_ref_external (a))
8927 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8928 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8929 is sized by target address length, whereas in DWARF3
8930 it's always sized as an offset. */
8931 if (use_debug_types)
8932 size += DWARF_TYPE_SIGNATURE_SIZE;
8933 else if (dwarf_version == 2)
8934 size += DWARF2_ADDR_SIZE;
8935 else
8936 size += DWARF_OFFSET_SIZE;
8938 else
8939 size += DWARF_OFFSET_SIZE;
8940 break;
8941 case dw_val_class_fde_ref:
8942 size += DWARF_OFFSET_SIZE;
8943 break;
8944 case dw_val_class_lbl_id:
8945 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8947 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8948 size += size_of_uleb128 (AT_index (a));
8950 else
8951 size += DWARF2_ADDR_SIZE;
8952 break;
8953 case dw_val_class_lineptr:
8954 case dw_val_class_macptr:
8955 case dw_val_class_loclistsptr:
8956 size += DWARF_OFFSET_SIZE;
8957 break;
8958 case dw_val_class_str:
8959 form = AT_string_form (a);
8960 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
8961 size += DWARF_OFFSET_SIZE;
8962 else if (form == DW_FORM_GNU_str_index)
8963 size += size_of_uleb128 (AT_index (a));
8964 else
8965 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8966 break;
8967 case dw_val_class_file:
8968 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8969 break;
8970 case dw_val_class_data8:
8971 size += 8;
8972 break;
8973 case dw_val_class_vms_delta:
8974 size += DWARF_OFFSET_SIZE;
8975 break;
8976 case dw_val_class_high_pc:
8977 size += DWARF2_ADDR_SIZE;
8978 break;
8979 case dw_val_class_discr_value:
8980 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8981 break;
8982 case dw_val_class_discr_list:
8984 unsigned block_size = size_of_discr_list (AT_discr_list (a));
8986 /* This is a block, so we have the block length and then its
8987 data. */
8988 size += constant_size (block_size) + block_size;
8990 break;
8991 default:
8992 gcc_unreachable ();
8996 return size;
8999 /* Size the debugging information associated with a given DIE. Visits the
9000 DIE's children recursively. Updates the global variable next_die_offset, on
9001 each time through. Uses the current value of next_die_offset to update the
9002 die_offset field in each DIE. */
9004 static void
9005 calc_die_sizes (dw_die_ref die)
9007 dw_die_ref c;
9009 gcc_assert (die->die_offset == 0
9010 || (unsigned long int) die->die_offset == next_die_offset);
9011 die->die_offset = next_die_offset;
9012 next_die_offset += size_of_die (die);
9014 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9016 if (die->die_child != NULL)
9017 /* Count the null byte used to terminate sibling lists. */
9018 next_die_offset += 1;
9021 /* Size just the base type children at the start of the CU.
9022 This is needed because build_abbrev needs to size locs
9023 and sizing of type based stack ops needs to know die_offset
9024 values for the base types. */
9026 static void
9027 calc_base_type_die_sizes (void)
9029 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9030 unsigned int i;
9031 dw_die_ref base_type;
9032 #if ENABLE_ASSERT_CHECKING
9033 dw_die_ref prev = comp_unit_die ()->die_child;
9034 #endif
9036 die_offset += size_of_die (comp_unit_die ());
9037 for (i = 0; base_types.iterate (i, &base_type); i++)
9039 #if ENABLE_ASSERT_CHECKING
9040 gcc_assert (base_type->die_offset == 0
9041 && prev->die_sib == base_type
9042 && base_type->die_child == NULL
9043 && base_type->die_abbrev);
9044 prev = base_type;
9045 #endif
9046 base_type->die_offset = die_offset;
9047 die_offset += size_of_die (base_type);
9051 /* Set the marks for a die and its children. We do this so
9052 that we know whether or not a reference needs to use FORM_ref_addr; only
9053 DIEs in the same CU will be marked. We used to clear out the offset
9054 and use that as the flag, but ran into ordering problems. */
9056 static void
9057 mark_dies (dw_die_ref die)
9059 dw_die_ref c;
9061 gcc_assert (!die->die_mark);
9063 die->die_mark = 1;
9064 FOR_EACH_CHILD (die, c, mark_dies (c));
9067 /* Clear the marks for a die and its children. */
9069 static void
9070 unmark_dies (dw_die_ref die)
9072 dw_die_ref c;
9074 if (! use_debug_types)
9075 gcc_assert (die->die_mark);
9077 die->die_mark = 0;
9078 FOR_EACH_CHILD (die, c, unmark_dies (c));
9081 /* Clear the marks for a die, its children and referred dies. */
9083 static void
9084 unmark_all_dies (dw_die_ref die)
9086 dw_die_ref c;
9087 dw_attr_node *a;
9088 unsigned ix;
9090 if (!die->die_mark)
9091 return;
9092 die->die_mark = 0;
9094 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9096 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9097 if (AT_class (a) == dw_val_class_die_ref)
9098 unmark_all_dies (AT_ref (a));
9101 /* Calculate if the entry should appear in the final output file. It may be
9102 from a pruned a type. */
9104 static bool
9105 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9107 /* By limiting gnu pubnames to definitions only, gold can generate a
9108 gdb index without entries for declarations, which don't include
9109 enough information to be useful. */
9110 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9111 return false;
9113 if (table == pubname_table)
9115 /* Enumerator names are part of the pubname table, but the
9116 parent DW_TAG_enumeration_type die may have been pruned.
9117 Don't output them if that is the case. */
9118 if (p->die->die_tag == DW_TAG_enumerator &&
9119 (p->die->die_parent == NULL
9120 || !p->die->die_parent->die_perennial_p))
9121 return false;
9123 /* Everything else in the pubname table is included. */
9124 return true;
9127 /* The pubtypes table shouldn't include types that have been
9128 pruned. */
9129 return (p->die->die_offset != 0
9130 || !flag_eliminate_unused_debug_types);
9133 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9134 generated for the compilation unit. */
9136 static unsigned long
9137 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9139 unsigned long size;
9140 unsigned i;
9141 pubname_entry *p;
9142 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9144 size = DWARF_PUBNAMES_HEADER_SIZE;
9145 FOR_EACH_VEC_ELT (*names, i, p)
9146 if (include_pubname_in_output (names, p))
9147 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9149 size += DWARF_OFFSET_SIZE;
9150 return size;
9153 /* Return the size of the information in the .debug_aranges section. */
9155 static unsigned long
9156 size_of_aranges (void)
9158 unsigned long size;
9160 size = DWARF_ARANGES_HEADER_SIZE;
9162 /* Count the address/length pair for this compilation unit. */
9163 if (text_section_used)
9164 size += 2 * DWARF2_ADDR_SIZE;
9165 if (cold_text_section_used)
9166 size += 2 * DWARF2_ADDR_SIZE;
9167 if (have_multiple_function_sections)
9169 unsigned fde_idx;
9170 dw_fde_ref fde;
9172 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9174 if (DECL_IGNORED_P (fde->decl))
9175 continue;
9176 if (!fde->in_std_section)
9177 size += 2 * DWARF2_ADDR_SIZE;
9178 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9179 size += 2 * DWARF2_ADDR_SIZE;
9183 /* Count the two zero words used to terminated the address range table. */
9184 size += 2 * DWARF2_ADDR_SIZE;
9185 return size;
9188 /* Select the encoding of an attribute value. */
9190 static enum dwarf_form
9191 value_format (dw_attr_node *a)
9193 switch (AT_class (a))
9195 case dw_val_class_addr:
9196 /* Only very few attributes allow DW_FORM_addr. */
9197 switch (a->dw_attr)
9199 case DW_AT_low_pc:
9200 case DW_AT_high_pc:
9201 case DW_AT_entry_pc:
9202 case DW_AT_trampoline:
9203 return (AT_index (a) == NOT_INDEXED
9204 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9205 default:
9206 break;
9208 switch (DWARF2_ADDR_SIZE)
9210 case 1:
9211 return DW_FORM_data1;
9212 case 2:
9213 return DW_FORM_data2;
9214 case 4:
9215 return DW_FORM_data4;
9216 case 8:
9217 return DW_FORM_data8;
9218 default:
9219 gcc_unreachable ();
9221 case dw_val_class_loc_list:
9222 if (dwarf_split_debug_info
9223 && dwarf_version >= 5
9224 && AT_loc_list (a)->num_assigned)
9225 return DW_FORM_loclistx;
9226 /* FALLTHRU */
9227 case dw_val_class_range_list:
9228 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9229 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9230 care about sizes of .debug* sections in shared libraries and
9231 executables and don't take into account relocations that affect just
9232 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9233 table in the .debug_rnglists section. */
9234 if (dwarf_split_debug_info
9235 && dwarf_version >= 5
9236 && AT_class (a) == dw_val_class_range_list
9237 && rnglist_idx
9238 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9239 return DW_FORM_rnglistx;
9240 if (dwarf_version >= 4)
9241 return DW_FORM_sec_offset;
9242 /* FALLTHRU */
9243 case dw_val_class_vms_delta:
9244 case dw_val_class_offset:
9245 switch (DWARF_OFFSET_SIZE)
9247 case 4:
9248 return DW_FORM_data4;
9249 case 8:
9250 return DW_FORM_data8;
9251 default:
9252 gcc_unreachable ();
9254 case dw_val_class_loc:
9255 if (dwarf_version >= 4)
9256 return DW_FORM_exprloc;
9257 switch (constant_size (size_of_locs (AT_loc (a))))
9259 case 1:
9260 return DW_FORM_block1;
9261 case 2:
9262 return DW_FORM_block2;
9263 case 4:
9264 return DW_FORM_block4;
9265 default:
9266 gcc_unreachable ();
9268 case dw_val_class_const:
9269 return DW_FORM_sdata;
9270 case dw_val_class_unsigned_const:
9271 switch (constant_size (AT_unsigned (a)))
9273 case 1:
9274 return DW_FORM_data1;
9275 case 2:
9276 return DW_FORM_data2;
9277 case 4:
9278 /* In DWARF3 DW_AT_data_member_location with
9279 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9280 constant, so we need to use DW_FORM_udata if we need
9281 a large constant. */
9282 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9283 return DW_FORM_udata;
9284 return DW_FORM_data4;
9285 case 8:
9286 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9287 return DW_FORM_udata;
9288 return DW_FORM_data8;
9289 default:
9290 gcc_unreachable ();
9292 case dw_val_class_const_implicit:
9293 case dw_val_class_unsigned_const_implicit:
9294 case dw_val_class_file_implicit:
9295 return DW_FORM_implicit_const;
9296 case dw_val_class_const_double:
9297 switch (HOST_BITS_PER_WIDE_INT)
9299 case 8:
9300 return DW_FORM_data2;
9301 case 16:
9302 return DW_FORM_data4;
9303 case 32:
9304 return DW_FORM_data8;
9305 case 64:
9306 if (dwarf_version >= 5)
9307 return DW_FORM_data16;
9308 /* FALLTHRU */
9309 default:
9310 return DW_FORM_block1;
9312 case dw_val_class_wide_int:
9313 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9315 case 8:
9316 return DW_FORM_data1;
9317 case 16:
9318 return DW_FORM_data2;
9319 case 32:
9320 return DW_FORM_data4;
9321 case 64:
9322 return DW_FORM_data8;
9323 case 128:
9324 if (dwarf_version >= 5)
9325 return DW_FORM_data16;
9326 /* FALLTHRU */
9327 default:
9328 return DW_FORM_block1;
9330 case dw_val_class_vec:
9331 switch (constant_size (a->dw_attr_val.v.val_vec.length
9332 * a->dw_attr_val.v.val_vec.elt_size))
9334 case 1:
9335 return DW_FORM_block1;
9336 case 2:
9337 return DW_FORM_block2;
9338 case 4:
9339 return DW_FORM_block4;
9340 default:
9341 gcc_unreachable ();
9343 case dw_val_class_flag:
9344 if (dwarf_version >= 4)
9346 /* Currently all add_AT_flag calls pass in 1 as last argument,
9347 so DW_FORM_flag_present can be used. If that ever changes,
9348 we'll need to use DW_FORM_flag and have some optimization
9349 in build_abbrev_table that will change those to
9350 DW_FORM_flag_present if it is set to 1 in all DIEs using
9351 the same abbrev entry. */
9352 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9353 return DW_FORM_flag_present;
9355 return DW_FORM_flag;
9356 case dw_val_class_die_ref:
9357 if (AT_ref_external (a))
9358 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9359 else
9360 return DW_FORM_ref;
9361 case dw_val_class_fde_ref:
9362 return DW_FORM_data;
9363 case dw_val_class_lbl_id:
9364 return (AT_index (a) == NOT_INDEXED
9365 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9366 case dw_val_class_lineptr:
9367 case dw_val_class_macptr:
9368 case dw_val_class_loclistsptr:
9369 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9370 case dw_val_class_str:
9371 return AT_string_form (a);
9372 case dw_val_class_file:
9373 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9375 case 1:
9376 return DW_FORM_data1;
9377 case 2:
9378 return DW_FORM_data2;
9379 case 4:
9380 return DW_FORM_data4;
9381 default:
9382 gcc_unreachable ();
9385 case dw_val_class_data8:
9386 return DW_FORM_data8;
9388 case dw_val_class_high_pc:
9389 switch (DWARF2_ADDR_SIZE)
9391 case 1:
9392 return DW_FORM_data1;
9393 case 2:
9394 return DW_FORM_data2;
9395 case 4:
9396 return DW_FORM_data4;
9397 case 8:
9398 return DW_FORM_data8;
9399 default:
9400 gcc_unreachable ();
9403 case dw_val_class_discr_value:
9404 return (a->dw_attr_val.v.val_discr_value.pos
9405 ? DW_FORM_udata
9406 : DW_FORM_sdata);
9407 case dw_val_class_discr_list:
9408 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9410 case 1:
9411 return DW_FORM_block1;
9412 case 2:
9413 return DW_FORM_block2;
9414 case 4:
9415 return DW_FORM_block4;
9416 default:
9417 gcc_unreachable ();
9420 default:
9421 gcc_unreachable ();
9425 /* Output the encoding of an attribute value. */
9427 static void
9428 output_value_format (dw_attr_node *a)
9430 enum dwarf_form form = value_format (a);
9432 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9435 /* Given a die and id, produce the appropriate abbreviations. */
9437 static void
9438 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9440 unsigned ix;
9441 dw_attr_node *a_attr;
9443 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9444 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9445 dwarf_tag_name (abbrev->die_tag));
9447 if (abbrev->die_child != NULL)
9448 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9449 else
9450 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9452 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9454 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9455 dwarf_attr_name (a_attr->dw_attr));
9456 output_value_format (a_attr);
9457 if (value_format (a_attr) == DW_FORM_implicit_const)
9459 if (AT_class (a_attr) == dw_val_class_file_implicit)
9461 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9462 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9463 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9465 else
9466 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9470 dw2_asm_output_data (1, 0, NULL);
9471 dw2_asm_output_data (1, 0, NULL);
9475 /* Output the .debug_abbrev section which defines the DIE abbreviation
9476 table. */
9478 static void
9479 output_abbrev_section (void)
9481 unsigned int abbrev_id;
9482 dw_die_ref abbrev;
9484 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9485 if (abbrev_id != 0)
9486 output_die_abbrevs (abbrev_id, abbrev);
9488 /* Terminate the table. */
9489 dw2_asm_output_data (1, 0, NULL);
9492 /* Output a symbol we can use to refer to this DIE from another CU. */
9494 static inline void
9495 output_die_symbol (dw_die_ref die)
9497 const char *sym = die->die_id.die_symbol;
9499 gcc_assert (!die->comdat_type_p);
9501 if (sym == 0)
9502 return;
9504 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9505 /* We make these global, not weak; if the target doesn't support
9506 .linkonce, it doesn't support combining the sections, so debugging
9507 will break. */
9508 targetm.asm_out.globalize_label (asm_out_file, sym);
9510 ASM_OUTPUT_LABEL (asm_out_file, sym);
9513 /* Return a new location list, given the begin and end range, and the
9514 expression. */
9516 static inline dw_loc_list_ref
9517 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9518 const char *section)
9520 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9522 retlist->begin = begin;
9523 retlist->begin_entry = NULL;
9524 retlist->end = end;
9525 retlist->expr = expr;
9526 retlist->section = section;
9528 return retlist;
9531 /* Generate a new internal symbol for this location list node, if it
9532 hasn't got one yet. */
9534 static inline void
9535 gen_llsym (dw_loc_list_ref list)
9537 gcc_assert (!list->ll_symbol);
9538 list->ll_symbol = gen_internal_sym ("LLST");
9541 /* Output the location list given to us. */
9543 static void
9544 output_loc_list (dw_loc_list_ref list_head)
9546 if (list_head->emitted)
9547 return;
9548 list_head->emitted = true;
9550 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9552 dw_loc_list_ref curr = list_head;
9553 const char *last_section = NULL;
9554 const char *base_label = NULL;
9556 /* Walk the location list, and output each range + expression. */
9557 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9559 unsigned long size;
9560 /* Don't output an entry that starts and ends at the same address. */
9561 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9562 continue;
9563 size = size_of_locs (curr->expr);
9564 /* If the expression is too large, drop it on the floor. We could
9565 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9566 in the expression, but >= 64KB expressions for a single value
9567 in a single range are unlikely very useful. */
9568 if (size > 0xffff)
9569 continue;
9570 if (dwarf_version >= 5)
9572 if (dwarf_split_debug_info)
9574 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9575 uleb128 index into .debug_addr and uleb128 length. */
9576 dw2_asm_output_data (1, DW_LLE_startx_length,
9577 "DW_LLE_startx_length (%s)",
9578 list_head->ll_symbol);
9579 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9580 "Location list range start index "
9581 "(%s)", curr->begin);
9582 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9583 For that case we probably need to emit DW_LLE_startx_endx,
9584 but we'd need 2 .debug_addr entries rather than just one. */
9585 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9586 "Location list length (%s)",
9587 list_head->ll_symbol);
9589 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9591 /* If all code is in .text section, the base address is
9592 already provided by the CU attributes. Use
9593 DW_LLE_offset_pair where both addresses are uleb128 encoded
9594 offsets against that base. */
9595 dw2_asm_output_data (1, DW_LLE_offset_pair,
9596 "DW_LLE_offset_pair (%s)",
9597 list_head->ll_symbol);
9598 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9599 "Location list begin address (%s)",
9600 list_head->ll_symbol);
9601 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9602 "Location list end address (%s)",
9603 list_head->ll_symbol);
9605 else if (HAVE_AS_LEB128)
9607 /* Otherwise, find out how many consecutive entries could share
9608 the same base entry. If just one, emit DW_LLE_start_length,
9609 otherwise emit DW_LLE_base_address for the base address
9610 followed by a series of DW_LLE_offset_pair. */
9611 if (last_section == NULL || curr->section != last_section)
9613 dw_loc_list_ref curr2;
9614 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9615 curr2 = curr2->dw_loc_next)
9617 if (strcmp (curr2->begin, curr2->end) == 0
9618 && !curr2->force)
9619 continue;
9620 if ((unsigned long) size_of_locs (curr2->expr) > 0xffff)
9621 continue;
9622 break;
9624 if (curr2 == NULL || curr->section != curr2->section)
9625 last_section = NULL;
9626 else
9628 last_section = curr->section;
9629 base_label = curr->begin;
9630 dw2_asm_output_data (1, DW_LLE_base_address,
9631 "DW_LLE_base_address (%s)",
9632 list_head->ll_symbol);
9633 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9634 "Base address (%s)",
9635 list_head->ll_symbol);
9638 /* Only one entry with the same base address. Use
9639 DW_LLE_start_length with absolute address and uleb128
9640 length. */
9641 if (last_section == NULL)
9643 dw2_asm_output_data (1, DW_LLE_start_length,
9644 "DW_LLE_start_length (%s)",
9645 list_head->ll_symbol);
9646 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9647 "Location list begin address (%s)",
9648 list_head->ll_symbol);
9649 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9650 "Location list length "
9651 "(%s)", list_head->ll_symbol);
9653 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9654 DW_LLE_base_address. */
9655 else
9657 dw2_asm_output_data (1, DW_LLE_offset_pair,
9658 "DW_LLE_offset_pair (%s)",
9659 list_head->ll_symbol);
9660 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9661 "Location list begin address "
9662 "(%s)", list_head->ll_symbol);
9663 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9664 "Location list end address "
9665 "(%s)", list_head->ll_symbol);
9668 /* The assembler does not support .uleb128 directive. Emit
9669 DW_LLE_start_end with a pair of absolute addresses. */
9670 else
9672 dw2_asm_output_data (1, DW_LLE_start_end,
9673 "DW_LLE_start_end (%s)",
9674 list_head->ll_symbol);
9675 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9676 "Location list begin address (%s)",
9677 list_head->ll_symbol);
9678 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9679 "Location list end address (%s)",
9680 list_head->ll_symbol);
9683 else if (dwarf_split_debug_info)
9685 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9686 and 4 byte length. */
9687 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9688 "Location list start/length entry (%s)",
9689 list_head->ll_symbol);
9690 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9691 "Location list range start index (%s)",
9692 curr->begin);
9693 /* The length field is 4 bytes. If we ever need to support
9694 an 8-byte length, we can add a new DW_LLE code or fall back
9695 to DW_LLE_GNU_start_end_entry. */
9696 dw2_asm_output_delta (4, curr->end, curr->begin,
9697 "Location list range length (%s)",
9698 list_head->ll_symbol);
9700 else if (!have_multiple_function_sections)
9702 /* Pair of relative addresses against start of text section. */
9703 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9704 "Location list begin address (%s)",
9705 list_head->ll_symbol);
9706 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9707 "Location list end address (%s)",
9708 list_head->ll_symbol);
9710 else
9712 /* Pair of absolute addresses. */
9713 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9714 "Location list begin address (%s)",
9715 list_head->ll_symbol);
9716 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9717 "Location list end address (%s)",
9718 list_head->ll_symbol);
9721 /* Output the block length for this list of location operations. */
9722 gcc_assert (size <= 0xffff);
9723 dw2_asm_output_data (2, size, "%s", "Location expression size");
9725 output_loc_sequence (curr->expr, -1);
9728 /* And finally list termination. */
9729 if (dwarf_version >= 5)
9730 dw2_asm_output_data (1, DW_LLE_end_of_list,
9731 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9732 else if (dwarf_split_debug_info)
9733 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9734 "Location list terminator (%s)",
9735 list_head->ll_symbol);
9736 else
9738 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9739 "Location list terminator begin (%s)",
9740 list_head->ll_symbol);
9741 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9742 "Location list terminator end (%s)",
9743 list_head->ll_symbol);
9747 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9748 section. Emit a relocated reference if val_entry is NULL, otherwise,
9749 emit an indirect reference. */
9751 static void
9752 output_range_list_offset (dw_attr_node *a)
9754 const char *name = dwarf_attr_name (a->dw_attr);
9756 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9758 if (dwarf_version >= 5)
9760 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9761 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9762 debug_ranges_section, "%s", name);
9764 else
9766 char *p = strchr (ranges_section_label, '\0');
9767 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9768 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9769 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9770 debug_ranges_section, "%s", name);
9771 *p = '\0';
9774 else if (dwarf_version >= 5)
9776 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9777 gcc_assert (rnglist_idx);
9778 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9780 else
9781 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9782 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9783 "%s (offset from %s)", name, ranges_section_label);
9786 /* Output the offset into the debug_loc section. */
9788 static void
9789 output_loc_list_offset (dw_attr_node *a)
9791 char *sym = AT_loc_list (a)->ll_symbol;
9793 gcc_assert (sym);
9794 if (!dwarf_split_debug_info)
9795 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9796 "%s", dwarf_attr_name (a->dw_attr));
9797 else if (dwarf_version >= 5)
9799 gcc_assert (AT_loc_list (a)->num_assigned);
9800 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9801 dwarf_attr_name (a->dw_attr),
9802 sym);
9804 else
9805 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9806 "%s", dwarf_attr_name (a->dw_attr));
9809 /* Output an attribute's index or value appropriately. */
9811 static void
9812 output_attr_index_or_value (dw_attr_node *a)
9814 const char *name = dwarf_attr_name (a->dw_attr);
9816 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9818 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9819 return;
9821 switch (AT_class (a))
9823 case dw_val_class_addr:
9824 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9825 break;
9826 case dw_val_class_high_pc:
9827 case dw_val_class_lbl_id:
9828 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9829 break;
9830 default:
9831 gcc_unreachable ();
9835 /* Output a type signature. */
9837 static inline void
9838 output_signature (const char *sig, const char *name)
9840 int i;
9842 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9843 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9846 /* Output a discriminant value. */
9848 static inline void
9849 output_discr_value (dw_discr_value *discr_value, const char *name)
9851 if (discr_value->pos)
9852 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9853 else
9854 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9857 /* Output the DIE and its attributes. Called recursively to generate
9858 the definitions of each child DIE. */
9860 static void
9861 output_die (dw_die_ref die)
9863 dw_attr_node *a;
9864 dw_die_ref c;
9865 unsigned long size;
9866 unsigned ix;
9868 /* If someone in another CU might refer to us, set up a symbol for
9869 them to point to. */
9870 if (! die->comdat_type_p && die->die_id.die_symbol)
9871 output_die_symbol (die);
9873 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9874 (unsigned long)die->die_offset,
9875 dwarf_tag_name (die->die_tag));
9877 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9879 const char *name = dwarf_attr_name (a->dw_attr);
9881 switch (AT_class (a))
9883 case dw_val_class_addr:
9884 output_attr_index_or_value (a);
9885 break;
9887 case dw_val_class_offset:
9888 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9889 "%s", name);
9890 break;
9892 case dw_val_class_range_list:
9893 output_range_list_offset (a);
9894 break;
9896 case dw_val_class_loc:
9897 size = size_of_locs (AT_loc (a));
9899 /* Output the block length for this list of location operations. */
9900 if (dwarf_version >= 4)
9901 dw2_asm_output_data_uleb128 (size, "%s", name);
9902 else
9903 dw2_asm_output_data (constant_size (size), size, "%s", name);
9905 output_loc_sequence (AT_loc (a), -1);
9906 break;
9908 case dw_val_class_const:
9909 /* ??? It would be slightly more efficient to use a scheme like is
9910 used for unsigned constants below, but gdb 4.x does not sign
9911 extend. Gdb 5.x does sign extend. */
9912 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9913 break;
9915 case dw_val_class_unsigned_const:
9917 int csize = constant_size (AT_unsigned (a));
9918 if (dwarf_version == 3
9919 && a->dw_attr == DW_AT_data_member_location
9920 && csize >= 4)
9921 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9922 else
9923 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9925 break;
9927 case dw_val_class_const_implicit:
9928 if (flag_debug_asm)
9929 fprintf (asm_out_file, "\t\t\t%s %s ("
9930 HOST_WIDE_INT_PRINT_DEC ")\n",
9931 ASM_COMMENT_START, name, AT_int (a));
9932 break;
9934 case dw_val_class_unsigned_const_implicit:
9935 if (flag_debug_asm)
9936 fprintf (asm_out_file, "\t\t\t%s %s ("
9937 HOST_WIDE_INT_PRINT_HEX ")\n",
9938 ASM_COMMENT_START, name, AT_unsigned (a));
9939 break;
9941 case dw_val_class_const_double:
9943 unsigned HOST_WIDE_INT first, second;
9945 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9946 dw2_asm_output_data (1,
9947 HOST_BITS_PER_DOUBLE_INT
9948 / HOST_BITS_PER_CHAR,
9949 NULL);
9951 if (WORDS_BIG_ENDIAN)
9953 first = a->dw_attr_val.v.val_double.high;
9954 second = a->dw_attr_val.v.val_double.low;
9956 else
9958 first = a->dw_attr_val.v.val_double.low;
9959 second = a->dw_attr_val.v.val_double.high;
9962 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9963 first, "%s", name);
9964 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9965 second, NULL);
9967 break;
9969 case dw_val_class_wide_int:
9971 int i;
9972 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9973 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9974 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9975 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
9976 * l, NULL);
9978 if (WORDS_BIG_ENDIAN)
9979 for (i = len - 1; i >= 0; --i)
9981 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9982 "%s", name);
9983 name = "";
9985 else
9986 for (i = 0; i < len; ++i)
9988 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9989 "%s", name);
9990 name = "";
9993 break;
9995 case dw_val_class_vec:
9997 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9998 unsigned int len = a->dw_attr_val.v.val_vec.length;
9999 unsigned int i;
10000 unsigned char *p;
10002 dw2_asm_output_data (constant_size (len * elt_size),
10003 len * elt_size, "%s", name);
10004 if (elt_size > sizeof (HOST_WIDE_INT))
10006 elt_size /= 2;
10007 len *= 2;
10009 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10010 i < len;
10011 i++, p += elt_size)
10012 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10013 "fp or vector constant word %u", i);
10014 break;
10017 case dw_val_class_flag:
10018 if (dwarf_version >= 4)
10020 /* Currently all add_AT_flag calls pass in 1 as last argument,
10021 so DW_FORM_flag_present can be used. If that ever changes,
10022 we'll need to use DW_FORM_flag and have some optimization
10023 in build_abbrev_table that will change those to
10024 DW_FORM_flag_present if it is set to 1 in all DIEs using
10025 the same abbrev entry. */
10026 gcc_assert (AT_flag (a) == 1);
10027 if (flag_debug_asm)
10028 fprintf (asm_out_file, "\t\t\t%s %s\n",
10029 ASM_COMMENT_START, name);
10030 break;
10032 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10033 break;
10035 case dw_val_class_loc_list:
10036 output_loc_list_offset (a);
10037 break;
10039 case dw_val_class_die_ref:
10040 if (AT_ref_external (a))
10042 if (AT_ref (a)->comdat_type_p)
10044 comdat_type_node *type_node
10045 = AT_ref (a)->die_id.die_type_node;
10047 gcc_assert (type_node);
10048 output_signature (type_node->signature, name);
10050 else
10052 const char *sym = AT_ref (a)->die_id.die_symbol;
10053 int size;
10055 gcc_assert (sym);
10056 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10057 length, whereas in DWARF3 it's always sized as an
10058 offset. */
10059 if (dwarf_version == 2)
10060 size = DWARF2_ADDR_SIZE;
10061 else
10062 size = DWARF_OFFSET_SIZE;
10063 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10064 name);
10067 else
10069 gcc_assert (AT_ref (a)->die_offset);
10070 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10071 "%s", name);
10073 break;
10075 case dw_val_class_fde_ref:
10077 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10079 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10080 a->dw_attr_val.v.val_fde_index * 2);
10081 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10082 "%s", name);
10084 break;
10086 case dw_val_class_vms_delta:
10087 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10088 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10089 AT_vms_delta2 (a), AT_vms_delta1 (a),
10090 "%s", name);
10091 #else
10092 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10093 AT_vms_delta2 (a), AT_vms_delta1 (a),
10094 "%s", name);
10095 #endif
10096 break;
10098 case dw_val_class_lbl_id:
10099 output_attr_index_or_value (a);
10100 break;
10102 case dw_val_class_lineptr:
10103 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10104 debug_line_section, "%s", name);
10105 break;
10107 case dw_val_class_macptr:
10108 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10109 debug_macinfo_section, "%s", name);
10110 break;
10112 case dw_val_class_loclistsptr:
10113 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10114 debug_loc_section, "%s", name);
10115 break;
10117 case dw_val_class_str:
10118 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10119 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10120 a->dw_attr_val.v.val_str->label,
10121 debug_str_section,
10122 "%s: \"%s\"", name, AT_string (a));
10123 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10124 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10125 a->dw_attr_val.v.val_str->label,
10126 debug_line_str_section,
10127 "%s: \"%s\"", name, AT_string (a));
10128 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10129 dw2_asm_output_data_uleb128 (AT_index (a),
10130 "%s: \"%s\"", name, AT_string (a));
10131 else
10132 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10133 break;
10135 case dw_val_class_file:
10137 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10139 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10140 a->dw_attr_val.v.val_file->filename);
10141 break;
10144 case dw_val_class_file_implicit:
10145 if (flag_debug_asm)
10146 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10147 ASM_COMMENT_START, name,
10148 maybe_emit_file (a->dw_attr_val.v.val_file),
10149 a->dw_attr_val.v.val_file->filename);
10150 break;
10152 case dw_val_class_data8:
10154 int i;
10156 for (i = 0; i < 8; i++)
10157 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10158 i == 0 ? "%s" : NULL, name);
10159 break;
10162 case dw_val_class_high_pc:
10163 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10164 get_AT_low_pc (die), "DW_AT_high_pc");
10165 break;
10167 case dw_val_class_discr_value:
10168 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10169 break;
10171 case dw_val_class_discr_list:
10173 dw_discr_list_ref list = AT_discr_list (a);
10174 const int size = size_of_discr_list (list);
10176 /* This is a block, so output its length first. */
10177 dw2_asm_output_data (constant_size (size), size,
10178 "%s: block size", name);
10180 for (; list != NULL; list = list->dw_discr_next)
10182 /* One byte for the discriminant value descriptor, and then as
10183 many LEB128 numbers as required. */
10184 if (list->dw_discr_range)
10185 dw2_asm_output_data (1, DW_DSC_range,
10186 "%s: DW_DSC_range", name);
10187 else
10188 dw2_asm_output_data (1, DW_DSC_label,
10189 "%s: DW_DSC_label", name);
10191 output_discr_value (&list->dw_discr_lower_bound, name);
10192 if (list->dw_discr_range)
10193 output_discr_value (&list->dw_discr_upper_bound, name);
10195 break;
10198 default:
10199 gcc_unreachable ();
10203 FOR_EACH_CHILD (die, c, output_die (c));
10205 /* Add null byte to terminate sibling list. */
10206 if (die->die_child != NULL)
10207 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10208 (unsigned long) die->die_offset);
10211 /* Output the compilation unit that appears at the beginning of the
10212 .debug_info section, and precedes the DIE descriptions. */
10214 static void
10215 output_compilation_unit_header (enum dwarf_unit_type ut)
10217 if (!XCOFF_DEBUGGING_INFO)
10219 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10220 dw2_asm_output_data (4, 0xffffffff,
10221 "Initial length escape value indicating 64-bit DWARF extension");
10222 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10223 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10224 "Length of Compilation Unit Info");
10227 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10228 if (dwarf_version >= 5)
10230 const char *name;
10231 switch (ut)
10233 case DW_UT_compile: name = "DW_UT_compile"; break;
10234 case DW_UT_type: name = "DW_UT_type"; break;
10235 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10236 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10237 default: gcc_unreachable ();
10239 dw2_asm_output_data (1, ut, name);
10240 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10242 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10243 debug_abbrev_section,
10244 "Offset Into Abbrev. Section");
10245 if (dwarf_version < 5)
10246 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10249 /* Output the compilation unit DIE and its children. */
10251 static void
10252 output_comp_unit (dw_die_ref die, int output_if_empty,
10253 const unsigned char *dwo_id)
10255 const char *secname, *oldsym;
10256 char *tmp;
10258 /* Unless we are outputting main CU, we may throw away empty ones. */
10259 if (!output_if_empty && die->die_child == NULL)
10260 return;
10262 /* Even if there are no children of this DIE, we must output the information
10263 about the compilation unit. Otherwise, on an empty translation unit, we
10264 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10265 will then complain when examining the file. First mark all the DIEs in
10266 this CU so we know which get local refs. */
10267 mark_dies (die);
10269 external_ref_hash_type *extern_map = optimize_external_refs (die);
10271 /* For now, optimize only the main CU, in order to optimize the rest
10272 we'd need to see all of them earlier. Leave the rest for post-linking
10273 tools like DWZ. */
10274 if (die == comp_unit_die ())
10275 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10277 build_abbrev_table (die, extern_map);
10279 optimize_abbrev_table ();
10281 delete extern_map;
10283 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10284 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
10285 calc_die_sizes (die);
10287 oldsym = die->die_id.die_symbol;
10288 if (oldsym)
10290 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10292 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10293 secname = tmp;
10294 die->die_id.die_symbol = NULL;
10295 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10297 else
10299 switch_to_section (debug_info_section);
10300 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10301 info_section_emitted = true;
10304 /* Output debugging information. */
10305 output_compilation_unit_header (dwo_id
10306 ? DW_UT_split_compile : DW_UT_compile);
10307 if (dwarf_version >= 5)
10309 if (dwo_id != NULL)
10310 for (int i = 0; i < 8; i++)
10311 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10312 else
10313 /* Hope all the padding will be removed for DWARF 5 final for
10314 DW_AT_compile and DW_AT_partial. */
10315 dw2_asm_output_data (8, 0, "Padding 1");
10317 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Padding 2");
10319 output_die (die);
10321 /* Leave the marks on the main CU, so we can check them in
10322 output_pubnames. */
10323 if (oldsym)
10325 unmark_dies (die);
10326 die->die_id.die_symbol = oldsym;
10330 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10331 and .debug_pubtypes. This is configured per-target, but can be
10332 overridden by the -gpubnames or -gno-pubnames options. */
10334 static inline bool
10335 want_pubnames (void)
10337 if (debug_info_level <= DINFO_LEVEL_TERSE)
10338 return false;
10339 if (debug_generate_pub_sections != -1)
10340 return debug_generate_pub_sections;
10341 return targetm.want_debug_pub_sections;
10344 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10346 static void
10347 add_AT_pubnames (dw_die_ref die)
10349 if (want_pubnames ())
10350 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10353 /* Add a string attribute value to a skeleton DIE. */
10355 static inline void
10356 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10357 const char *str)
10359 dw_attr_node attr;
10360 struct indirect_string_node *node;
10362 if (! skeleton_debug_str_hash)
10363 skeleton_debug_str_hash
10364 = hash_table<indirect_string_hasher>::create_ggc (10);
10366 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10367 find_string_form (node);
10368 if (node->form == DW_FORM_GNU_str_index)
10369 node->form = DW_FORM_strp;
10371 attr.dw_attr = attr_kind;
10372 attr.dw_attr_val.val_class = dw_val_class_str;
10373 attr.dw_attr_val.val_entry = NULL;
10374 attr.dw_attr_val.v.val_str = node;
10375 add_dwarf_attr (die, &attr);
10378 /* Helper function to generate top-level dies for skeleton debug_info and
10379 debug_types. */
10381 static void
10382 add_top_level_skeleton_die_attrs (dw_die_ref die)
10384 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10385 const char *comp_dir = comp_dir_string ();
10387 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10388 if (comp_dir != NULL)
10389 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10390 add_AT_pubnames (die);
10391 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10394 /* Output skeleton debug sections that point to the dwo file. */
10396 static void
10397 output_skeleton_debug_sections (dw_die_ref comp_unit,
10398 const unsigned char *dwo_id)
10400 /* These attributes will be found in the full debug_info section. */
10401 remove_AT (comp_unit, DW_AT_producer);
10402 remove_AT (comp_unit, DW_AT_language);
10404 switch_to_section (debug_skeleton_info_section);
10405 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10407 /* Produce the skeleton compilation-unit header. This one differs enough from
10408 a normal CU header that it's better not to call output_compilation_unit
10409 header. */
10410 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10411 dw2_asm_output_data (4, 0xffffffff,
10412 "Initial length escape value indicating 64-bit DWARF extension");
10414 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10415 DWARF_COMPILE_UNIT_HEADER_SIZE
10416 - DWARF_INITIAL_LENGTH_SIZE
10417 + size_of_die (comp_unit),
10418 "Length of Compilation Unit Info");
10419 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10420 if (dwarf_version >= 5)
10422 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10423 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10425 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10426 debug_skeleton_abbrev_section,
10427 "Offset Into Abbrev. Section");
10428 if (dwarf_version < 5)
10429 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10430 else
10432 for (int i = 0; i < 8; i++)
10433 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10435 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Padding 2");
10438 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10439 output_die (comp_unit);
10441 /* Build the skeleton debug_abbrev section. */
10442 switch_to_section (debug_skeleton_abbrev_section);
10443 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10445 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10447 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10450 /* Output a comdat type unit DIE and its children. */
10452 static void
10453 output_comdat_type_unit (comdat_type_node *node)
10455 const char *secname;
10456 char *tmp;
10457 int i;
10458 #if defined (OBJECT_FORMAT_ELF)
10459 tree comdat_key;
10460 #endif
10462 /* First mark all the DIEs in this CU so we know which get local refs. */
10463 mark_dies (node->root_die);
10465 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10467 build_abbrev_table (node->root_die, extern_map);
10469 delete extern_map;
10470 extern_map = NULL;
10472 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10473 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10474 calc_die_sizes (node->root_die);
10476 #if defined (OBJECT_FORMAT_ELF)
10477 if (dwarf_version >= 5)
10479 if (!dwarf_split_debug_info)
10480 secname = ".debug_info";
10481 else
10482 secname = ".debug_info.dwo";
10484 else if (!dwarf_split_debug_info)
10485 secname = ".debug_types";
10486 else
10487 secname = ".debug_types.dwo";
10489 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10490 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10491 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10492 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10493 comdat_key = get_identifier (tmp);
10494 targetm.asm_out.named_section (secname,
10495 SECTION_DEBUG | SECTION_LINKONCE,
10496 comdat_key);
10497 #else
10498 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10499 sprintf (tmp, (dwarf_version >= 5
10500 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10501 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10502 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10503 secname = tmp;
10504 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10505 #endif
10507 /* Output debugging information. */
10508 output_compilation_unit_header (dwarf_split_debug_info
10509 ? DW_UT_split_type : DW_UT_type);
10510 output_signature (node->signature, "Type Signature");
10511 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10512 "Offset to Type DIE");
10513 output_die (node->root_die);
10515 unmark_dies (node->root_die);
10518 /* Return the DWARF2/3 pubname associated with a decl. */
10520 static const char *
10521 dwarf2_name (tree decl, int scope)
10523 if (DECL_NAMELESS (decl))
10524 return NULL;
10525 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10528 /* Add a new entry to .debug_pubnames if appropriate. */
10530 static void
10531 add_pubname_string (const char *str, dw_die_ref die)
10533 pubname_entry e;
10535 e.die = die;
10536 e.name = xstrdup (str);
10537 vec_safe_push (pubname_table, e);
10540 static void
10541 add_pubname (tree decl, dw_die_ref die)
10543 if (!want_pubnames ())
10544 return;
10546 /* Don't add items to the table when we expect that the consumer will have
10547 just read the enclosing die. For example, if the consumer is looking at a
10548 class_member, it will either be inside the class already, or will have just
10549 looked up the class to find the member. Either way, searching the class is
10550 faster than searching the index. */
10551 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10552 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10554 const char *name = dwarf2_name (decl, 1);
10556 if (name)
10557 add_pubname_string (name, die);
10561 /* Add an enumerator to the pubnames section. */
10563 static void
10564 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10566 pubname_entry e;
10568 gcc_assert (scope_name);
10569 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10570 e.die = die;
10571 vec_safe_push (pubname_table, e);
10574 /* Add a new entry to .debug_pubtypes if appropriate. */
10576 static void
10577 add_pubtype (tree decl, dw_die_ref die)
10579 pubname_entry e;
10581 if (!want_pubnames ())
10582 return;
10584 if ((TREE_PUBLIC (decl)
10585 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10586 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10588 tree scope = NULL;
10589 const char *scope_name = "";
10590 const char *sep = is_cxx () ? "::" : ".";
10591 const char *name;
10593 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10594 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10596 scope_name = lang_hooks.dwarf_name (scope, 1);
10597 if (scope_name != NULL && scope_name[0] != '\0')
10598 scope_name = concat (scope_name, sep, NULL);
10599 else
10600 scope_name = "";
10603 if (TYPE_P (decl))
10604 name = type_tag (decl);
10605 else
10606 name = lang_hooks.dwarf_name (decl, 1);
10608 /* If we don't have a name for the type, there's no point in adding
10609 it to the table. */
10610 if (name != NULL && name[0] != '\0')
10612 e.die = die;
10613 e.name = concat (scope_name, name, NULL);
10614 vec_safe_push (pubtype_table, e);
10617 /* Although it might be more consistent to add the pubinfo for the
10618 enumerators as their dies are created, they should only be added if the
10619 enum type meets the criteria above. So rather than re-check the parent
10620 enum type whenever an enumerator die is created, just output them all
10621 here. This isn't protected by the name conditional because anonymous
10622 enums don't have names. */
10623 if (die->die_tag == DW_TAG_enumeration_type)
10625 dw_die_ref c;
10627 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10632 /* Output a single entry in the pubnames table. */
10634 static void
10635 output_pubname (dw_offset die_offset, pubname_entry *entry)
10637 dw_die_ref die = entry->die;
10638 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10640 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10642 if (debug_generate_pub_sections == 2)
10644 /* This logic follows gdb's method for determining the value of the flag
10645 byte. */
10646 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10647 switch (die->die_tag)
10649 case DW_TAG_typedef:
10650 case DW_TAG_base_type:
10651 case DW_TAG_subrange_type:
10652 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10653 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10654 break;
10655 case DW_TAG_enumerator:
10656 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10657 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10658 if (!is_cxx () && !is_java ())
10659 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10660 break;
10661 case DW_TAG_subprogram:
10662 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10663 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10664 if (!is_ada ())
10665 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10666 break;
10667 case DW_TAG_constant:
10668 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10669 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10670 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10671 break;
10672 case DW_TAG_variable:
10673 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10674 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10675 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10676 break;
10677 case DW_TAG_namespace:
10678 case DW_TAG_imported_declaration:
10679 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10680 break;
10681 case DW_TAG_class_type:
10682 case DW_TAG_interface_type:
10683 case DW_TAG_structure_type:
10684 case DW_TAG_union_type:
10685 case DW_TAG_enumeration_type:
10686 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10687 if (!is_cxx () && !is_java ())
10688 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10689 break;
10690 default:
10691 /* An unusual tag. Leave the flag-byte empty. */
10692 break;
10694 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10695 "GDB-index flags");
10698 dw2_asm_output_nstring (entry->name, -1, "external name");
10702 /* Output the public names table used to speed up access to externally
10703 visible names; or the public types table used to find type definitions. */
10705 static void
10706 output_pubnames (vec<pubname_entry, va_gc> *names)
10708 unsigned i;
10709 unsigned long pubnames_length = size_of_pubnames (names);
10710 pubname_entry *pub;
10712 if (!XCOFF_DEBUGGING_INFO)
10714 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10715 dw2_asm_output_data (4, 0xffffffff,
10716 "Initial length escape value indicating 64-bit DWARF extension");
10717 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10718 "Pub Info Length");
10721 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10722 dw2_asm_output_data (2, 2, "DWARF Version");
10724 if (dwarf_split_debug_info)
10725 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10726 debug_skeleton_info_section,
10727 "Offset of Compilation Unit Info");
10728 else
10729 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10730 debug_info_section,
10731 "Offset of Compilation Unit Info");
10732 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10733 "Compilation Unit Length");
10735 FOR_EACH_VEC_ELT (*names, i, pub)
10737 if (include_pubname_in_output (names, pub))
10739 dw_offset die_offset = pub->die->die_offset;
10741 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10742 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10743 gcc_assert (pub->die->die_mark);
10745 /* If we're putting types in their own .debug_types sections,
10746 the .debug_pubtypes table will still point to the compile
10747 unit (not the type unit), so we want to use the offset of
10748 the skeleton DIE (if there is one). */
10749 if (pub->die->comdat_type_p && names == pubtype_table)
10751 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10753 if (type_node != NULL)
10754 die_offset = (type_node->skeleton_die != NULL
10755 ? type_node->skeleton_die->die_offset
10756 : comp_unit_die ()->die_offset);
10759 output_pubname (die_offset, pub);
10763 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10766 /* Output public names and types tables if necessary. */
10768 static void
10769 output_pubtables (void)
10771 if (!want_pubnames () || !info_section_emitted)
10772 return;
10774 switch_to_section (debug_pubnames_section);
10775 output_pubnames (pubname_table);
10776 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10777 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10778 simply won't look for the section. */
10779 switch_to_section (debug_pubtypes_section);
10780 output_pubnames (pubtype_table);
10784 /* Output the information that goes into the .debug_aranges table.
10785 Namely, define the beginning and ending address range of the
10786 text section generated for this compilation unit. */
10788 static void
10789 output_aranges (void)
10791 unsigned i;
10792 unsigned long aranges_length = size_of_aranges ();
10794 if (!XCOFF_DEBUGGING_INFO)
10796 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10797 dw2_asm_output_data (4, 0xffffffff,
10798 "Initial length escape value indicating 64-bit DWARF extension");
10799 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10800 "Length of Address Ranges Info");
10803 /* Version number for aranges is still 2, even up to DWARF5. */
10804 dw2_asm_output_data (2, 2, "DWARF Version");
10805 if (dwarf_split_debug_info)
10806 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10807 debug_skeleton_info_section,
10808 "Offset of Compilation Unit Info");
10809 else
10810 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10811 debug_info_section,
10812 "Offset of Compilation Unit Info");
10813 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10814 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10816 /* We need to align to twice the pointer size here. */
10817 if (DWARF_ARANGES_PAD_SIZE)
10819 /* Pad using a 2 byte words so that padding is correct for any
10820 pointer size. */
10821 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10822 2 * DWARF2_ADDR_SIZE);
10823 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10824 dw2_asm_output_data (2, 0, NULL);
10827 /* It is necessary not to output these entries if the sections were
10828 not used; if the sections were not used, the length will be 0 and
10829 the address may end up as 0 if the section is discarded by ld
10830 --gc-sections, leaving an invalid (0, 0) entry that can be
10831 confused with the terminator. */
10832 if (text_section_used)
10834 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10835 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10836 text_section_label, "Length");
10838 if (cold_text_section_used)
10840 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10841 "Address");
10842 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10843 cold_text_section_label, "Length");
10846 if (have_multiple_function_sections)
10848 unsigned fde_idx;
10849 dw_fde_ref fde;
10851 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10853 if (DECL_IGNORED_P (fde->decl))
10854 continue;
10855 if (!fde->in_std_section)
10857 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10858 "Address");
10859 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10860 fde->dw_fde_begin, "Length");
10862 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10864 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10865 "Address");
10866 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10867 fde->dw_fde_second_begin, "Length");
10872 /* Output the terminator words. */
10873 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10874 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10877 /* Add a new entry to .debug_ranges. Return its index into
10878 ranges_table vector. */
10880 static unsigned int
10881 add_ranges_num (int num, bool maybe_new_sec)
10883 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10884 vec_safe_push (ranges_table, r);
10885 return vec_safe_length (ranges_table) - 1;
10888 /* Add a new entry to .debug_ranges corresponding to a block, or a
10889 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10890 this entry might be in a different section from previous range. */
10892 static unsigned int
10893 add_ranges (const_tree block, bool maybe_new_sec)
10895 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10898 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10899 chain, or middle entry of a chain that will be directly referred to. */
10901 static void
10902 note_rnglist_head (unsigned int offset)
10904 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10905 return;
10906 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
10909 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10910 When using dwarf_split_debug_info, address attributes in dies destined
10911 for the final executable should be direct references--setting the
10912 parameter force_direct ensures this behavior. */
10914 static void
10915 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10916 bool *added, bool force_direct)
10918 unsigned int in_use = vec_safe_length (ranges_by_label);
10919 unsigned int offset;
10920 dw_ranges_by_label rbl = { begin, end };
10921 vec_safe_push (ranges_by_label, rbl);
10922 offset = add_ranges_num (-(int)in_use - 1, true);
10923 if (!*added)
10925 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10926 *added = true;
10927 note_rnglist_head (offset);
10931 /* Emit .debug_ranges section. */
10933 static void
10934 output_ranges (void)
10936 unsigned i;
10937 static const char *const start_fmt = "Offset %#x";
10938 const char *fmt = start_fmt;
10939 dw_ranges *r;
10941 switch_to_section (debug_ranges_section);
10942 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
10943 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
10945 int block_num = r->num;
10947 if (block_num > 0)
10949 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10950 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10952 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10953 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10955 /* If all code is in the text section, then the compilation
10956 unit base address defaults to DW_AT_low_pc, which is the
10957 base of the text section. */
10958 if (!have_multiple_function_sections)
10960 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10961 text_section_label,
10962 fmt, i * 2 * DWARF2_ADDR_SIZE);
10963 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10964 text_section_label, NULL);
10967 /* Otherwise, the compilation unit base address is zero,
10968 which allows us to use absolute addresses, and not worry
10969 about whether the target supports cross-section
10970 arithmetic. */
10971 else
10973 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10974 fmt, i * 2 * DWARF2_ADDR_SIZE);
10975 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10978 fmt = NULL;
10981 /* Negative block_num stands for an index into ranges_by_label. */
10982 else if (block_num < 0)
10984 int lab_idx = - block_num - 1;
10986 if (!have_multiple_function_sections)
10988 gcc_unreachable ();
10989 #if 0
10990 /* If we ever use add_ranges_by_labels () for a single
10991 function section, all we have to do is to take out
10992 the #if 0 above. */
10993 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10994 (*ranges_by_label)[lab_idx].begin,
10995 text_section_label,
10996 fmt, i * 2 * DWARF2_ADDR_SIZE);
10997 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10998 (*ranges_by_label)[lab_idx].end,
10999 text_section_label, NULL);
11000 #endif
11002 else
11004 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11005 (*ranges_by_label)[lab_idx].begin,
11006 fmt, i * 2 * DWARF2_ADDR_SIZE);
11007 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11008 (*ranges_by_label)[lab_idx].end,
11009 NULL);
11012 else
11014 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11015 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11016 fmt = start_fmt;
11021 /* Non-zero if .debug_line_str should be used for .debug_line section
11022 strings or strings that are likely shareable with those. */
11023 #define DWARF5_USE_DEBUG_LINE_STR \
11024 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11025 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11026 /* FIXME: there is no .debug_line_str.dwo section, \
11027 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11028 && !dwarf_split_debug_info)
11030 /* Assign .debug_rnglists indexes. */
11032 static void
11033 index_rnglists (void)
11035 unsigned i;
11036 dw_ranges *r;
11038 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11039 if (r->label)
11040 r->idx = rnglist_idx++;
11043 /* Emit .debug_rnglists section. */
11045 static void
11046 output_rnglists (void)
11048 unsigned i;
11049 dw_ranges *r;
11050 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11051 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11052 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11054 switch_to_section (debug_ranges_section);
11055 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11056 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11057 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11058 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11059 dw2_asm_output_data (4, 0xffffffff,
11060 "Initial length escape value indicating "
11061 "64-bit DWARF extension");
11062 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11063 "Length of Range Lists");
11064 ASM_OUTPUT_LABEL (asm_out_file, l1);
11065 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11066 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11067 dw2_asm_output_data (1, 0, "Segment Size");
11068 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11069 about relocation sizes and primarily care about the size of .debug*
11070 sections in linked shared libraries and executables, then
11071 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11072 into it are usually larger than just DW_FORM_sec_offset offsets
11073 into the .debug_rnglists section. */
11074 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11075 "Offset Entry Count");
11076 if (dwarf_split_debug_info)
11078 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11079 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11080 if (r->label)
11081 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11082 ranges_base_label, NULL);
11085 const char *lab = "";
11086 unsigned int len = vec_safe_length (ranges_table);
11087 const char *base = NULL;
11088 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11090 int block_num = r->num;
11092 if (r->label)
11094 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11095 lab = r->label;
11097 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11098 base = NULL;
11099 if (block_num > 0)
11101 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11102 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11104 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11105 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11107 if (HAVE_AS_LEB128)
11109 /* If all code is in the text section, then the compilation
11110 unit base address defaults to DW_AT_low_pc, which is the
11111 base of the text section. */
11112 if (!have_multiple_function_sections)
11114 dw2_asm_output_data (1, DW_RLE_offset_pair,
11115 "DW_RLE_offset_pair (%s)", lab);
11116 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11117 "Range begin address (%s)", lab);
11118 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11119 "Range end address (%s)", lab);
11120 continue;
11122 if (base == NULL)
11124 dw_ranges *r2 = NULL;
11125 if (i < len - 1)
11126 r2 = &(*ranges_table)[i + 1];
11127 if (r2
11128 && r2->num != 0
11129 && r2->label == NULL
11130 && !r2->maybe_new_sec)
11132 dw2_asm_output_data (1, DW_RLE_base_address,
11133 "DW_RLE_base_address (%s)", lab);
11134 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11135 "Base address (%s)", lab);
11136 strcpy (basebuf, blabel);
11137 base = basebuf;
11140 if (base)
11142 dw2_asm_output_data (1, DW_RLE_offset_pair,
11143 "DW_RLE_offset_pair (%s)", lab);
11144 dw2_asm_output_delta_uleb128 (blabel, base,
11145 "Range begin address (%s)", lab);
11146 dw2_asm_output_delta_uleb128 (elabel, base,
11147 "Range end address (%s)", lab);
11148 continue;
11150 dw2_asm_output_data (1, DW_RLE_start_length,
11151 "DW_RLE_start_length (%s)", lab);
11152 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11153 "Range begin address (%s)", lab);
11154 dw2_asm_output_delta_uleb128 (elabel, blabel,
11155 "Range length (%s)", lab);
11157 else
11159 dw2_asm_output_data (1, DW_RLE_start_end,
11160 "DW_RLE_start_end (%s)", lab);
11161 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11162 "Range begin address (%s)", lab);
11163 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11164 "Range end address (%s)", lab);
11168 /* Negative block_num stands for an index into ranges_by_label. */
11169 else if (block_num < 0)
11171 int lab_idx = - block_num - 1;
11172 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11173 const char *elabel = (*ranges_by_label)[lab_idx].end;
11175 if (!have_multiple_function_sections)
11176 gcc_unreachable ();
11177 if (HAVE_AS_LEB128)
11179 dw2_asm_output_data (1, DW_RLE_start_length,
11180 "DW_RLE_start_length (%s)", lab);
11181 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11182 "Range begin address (%s)", lab);
11183 dw2_asm_output_delta_uleb128 (elabel, blabel,
11184 "Range length (%s)", lab);
11186 else
11188 dw2_asm_output_data (1, DW_RLE_start_end,
11189 "DW_RLE_start_end (%s)", lab);
11190 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11191 "Range begin address (%s)", lab);
11192 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11193 "Range end address (%s)", lab);
11196 else
11197 dw2_asm_output_data (1, DW_RLE_end_of_list,
11198 "DW_RLE_end_of_list (%s)", lab);
11200 ASM_OUTPUT_LABEL (asm_out_file, l2);
11203 /* Data structure containing information about input files. */
11204 struct file_info
11206 const char *path; /* Complete file name. */
11207 const char *fname; /* File name part. */
11208 int length; /* Length of entire string. */
11209 struct dwarf_file_data * file_idx; /* Index in input file table. */
11210 int dir_idx; /* Index in directory table. */
11213 /* Data structure containing information about directories with source
11214 files. */
11215 struct dir_info
11217 const char *path; /* Path including directory name. */
11218 int length; /* Path length. */
11219 int prefix; /* Index of directory entry which is a prefix. */
11220 int count; /* Number of files in this directory. */
11221 int dir_idx; /* Index of directory used as base. */
11224 /* Callback function for file_info comparison. We sort by looking at
11225 the directories in the path. */
11227 static int
11228 file_info_cmp (const void *p1, const void *p2)
11230 const struct file_info *const s1 = (const struct file_info *) p1;
11231 const struct file_info *const s2 = (const struct file_info *) p2;
11232 const unsigned char *cp1;
11233 const unsigned char *cp2;
11235 /* Take care of file names without directories. We need to make sure that
11236 we return consistent values to qsort since some will get confused if
11237 we return the same value when identical operands are passed in opposite
11238 orders. So if neither has a directory, return 0 and otherwise return
11239 1 or -1 depending on which one has the directory. */
11240 if ((s1->path == s1->fname || s2->path == s2->fname))
11241 return (s2->path == s2->fname) - (s1->path == s1->fname);
11243 cp1 = (const unsigned char *) s1->path;
11244 cp2 = (const unsigned char *) s2->path;
11246 while (1)
11248 ++cp1;
11249 ++cp2;
11250 /* Reached the end of the first path? If so, handle like above. */
11251 if ((cp1 == (const unsigned char *) s1->fname)
11252 || (cp2 == (const unsigned char *) s2->fname))
11253 return ((cp2 == (const unsigned char *) s2->fname)
11254 - (cp1 == (const unsigned char *) s1->fname));
11256 /* Character of current path component the same? */
11257 else if (*cp1 != *cp2)
11258 return *cp1 - *cp2;
11262 struct file_name_acquire_data
11264 struct file_info *files;
11265 int used_files;
11266 int max_files;
11269 /* Traversal function for the hash table. */
11272 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11274 struct dwarf_file_data *d = *slot;
11275 struct file_info *fi;
11276 const char *f;
11278 gcc_assert (fnad->max_files >= d->emitted_number);
11280 if (! d->emitted_number)
11281 return 1;
11283 gcc_assert (fnad->max_files != fnad->used_files);
11285 fi = fnad->files + fnad->used_files++;
11287 /* Skip all leading "./". */
11288 f = d->filename;
11289 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11290 f += 2;
11292 /* Create a new array entry. */
11293 fi->path = f;
11294 fi->length = strlen (f);
11295 fi->file_idx = d;
11297 /* Search for the file name part. */
11298 f = strrchr (f, DIR_SEPARATOR);
11299 #if defined (DIR_SEPARATOR_2)
11301 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11303 if (g != NULL)
11305 if (f == NULL || f < g)
11306 f = g;
11309 #endif
11311 fi->fname = f == NULL ? fi->path : f + 1;
11312 return 1;
11315 /* Helper function for output_file_names. Emit a FORM encoded
11316 string STR, with assembly comment start ENTRY_KIND and
11317 index IDX */
11319 static void
11320 output_line_string (enum dwarf_form form, const char *str,
11321 const char *entry_kind, unsigned int idx)
11323 switch (form)
11325 case DW_FORM_string:
11326 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11327 break;
11328 case DW_FORM_line_strp:
11329 if (!debug_line_str_hash)
11330 debug_line_str_hash
11331 = hash_table<indirect_string_hasher>::create_ggc (10);
11333 struct indirect_string_node *node;
11334 node = find_AT_string_in_table (str, debug_line_str_hash);
11335 set_indirect_string (node);
11336 node->form = form;
11337 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11338 debug_line_str_section, "%s: %#x: \"%s\"",
11339 entry_kind, 0, node->str);
11340 break;
11341 default:
11342 gcc_unreachable ();
11346 /* Output the directory table and the file name table. We try to minimize
11347 the total amount of memory needed. A heuristic is used to avoid large
11348 slowdowns with many input files. */
11350 static void
11351 output_file_names (void)
11353 struct file_name_acquire_data fnad;
11354 int numfiles;
11355 struct file_info *files;
11356 struct dir_info *dirs;
11357 int *saved;
11358 int *savehere;
11359 int *backmap;
11360 int ndirs;
11361 int idx_offset;
11362 int i;
11364 if (!last_emitted_file)
11366 if (dwarf_version >= 5)
11368 dw2_asm_output_data (1, 0, "Directory entry format count");
11369 dw2_asm_output_data_uleb128 (0, "Directories count");
11370 dw2_asm_output_data (1, 0, "File name entry format count");
11371 dw2_asm_output_data_uleb128 (0, "File names count");
11373 else
11375 dw2_asm_output_data (1, 0, "End directory table");
11376 dw2_asm_output_data (1, 0, "End file name table");
11378 return;
11381 numfiles = last_emitted_file->emitted_number;
11383 /* Allocate the various arrays we need. */
11384 files = XALLOCAVEC (struct file_info, numfiles);
11385 dirs = XALLOCAVEC (struct dir_info, numfiles);
11387 fnad.files = files;
11388 fnad.used_files = 0;
11389 fnad.max_files = numfiles;
11390 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11391 gcc_assert (fnad.used_files == fnad.max_files);
11393 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11395 /* Find all the different directories used. */
11396 dirs[0].path = files[0].path;
11397 dirs[0].length = files[0].fname - files[0].path;
11398 dirs[0].prefix = -1;
11399 dirs[0].count = 1;
11400 dirs[0].dir_idx = 0;
11401 files[0].dir_idx = 0;
11402 ndirs = 1;
11404 for (i = 1; i < numfiles; i++)
11405 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11406 && memcmp (dirs[ndirs - 1].path, files[i].path,
11407 dirs[ndirs - 1].length) == 0)
11409 /* Same directory as last entry. */
11410 files[i].dir_idx = ndirs - 1;
11411 ++dirs[ndirs - 1].count;
11413 else
11415 int j;
11417 /* This is a new directory. */
11418 dirs[ndirs].path = files[i].path;
11419 dirs[ndirs].length = files[i].fname - files[i].path;
11420 dirs[ndirs].count = 1;
11421 dirs[ndirs].dir_idx = ndirs;
11422 files[i].dir_idx = ndirs;
11424 /* Search for a prefix. */
11425 dirs[ndirs].prefix = -1;
11426 for (j = 0; j < ndirs; j++)
11427 if (dirs[j].length < dirs[ndirs].length
11428 && dirs[j].length > 1
11429 && (dirs[ndirs].prefix == -1
11430 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11431 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11432 dirs[ndirs].prefix = j;
11434 ++ndirs;
11437 /* Now to the actual work. We have to find a subset of the directories which
11438 allow expressing the file name using references to the directory table
11439 with the least amount of characters. We do not do an exhaustive search
11440 where we would have to check out every combination of every single
11441 possible prefix. Instead we use a heuristic which provides nearly optimal
11442 results in most cases and never is much off. */
11443 saved = XALLOCAVEC (int, ndirs);
11444 savehere = XALLOCAVEC (int, ndirs);
11446 memset (saved, '\0', ndirs * sizeof (saved[0]));
11447 for (i = 0; i < ndirs; i++)
11449 int j;
11450 int total;
11452 /* We can always save some space for the current directory. But this
11453 does not mean it will be enough to justify adding the directory. */
11454 savehere[i] = dirs[i].length;
11455 total = (savehere[i] - saved[i]) * dirs[i].count;
11457 for (j = i + 1; j < ndirs; j++)
11459 savehere[j] = 0;
11460 if (saved[j] < dirs[i].length)
11462 /* Determine whether the dirs[i] path is a prefix of the
11463 dirs[j] path. */
11464 int k;
11466 k = dirs[j].prefix;
11467 while (k != -1 && k != (int) i)
11468 k = dirs[k].prefix;
11470 if (k == (int) i)
11472 /* Yes it is. We can possibly save some memory by
11473 writing the filenames in dirs[j] relative to
11474 dirs[i]. */
11475 savehere[j] = dirs[i].length;
11476 total += (savehere[j] - saved[j]) * dirs[j].count;
11481 /* Check whether we can save enough to justify adding the dirs[i]
11482 directory. */
11483 if (total > dirs[i].length + 1)
11485 /* It's worthwhile adding. */
11486 for (j = i; j < ndirs; j++)
11487 if (savehere[j] > 0)
11489 /* Remember how much we saved for this directory so far. */
11490 saved[j] = savehere[j];
11492 /* Remember the prefix directory. */
11493 dirs[j].dir_idx = i;
11498 /* Emit the directory name table. */
11499 idx_offset = dirs[0].length > 0 ? 1 : 0;
11500 enum dwarf_form str_form = DW_FORM_string;
11501 enum dwarf_form idx_form = DW_FORM_udata;
11502 if (dwarf_version >= 5)
11504 const char *comp_dir = comp_dir_string ();
11505 if (comp_dir == NULL)
11506 comp_dir = "";
11507 dw2_asm_output_data (1, 1, "Directory entry format count");
11508 if (DWARF5_USE_DEBUG_LINE_STR)
11509 str_form = DW_FORM_line_strp;
11510 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11511 dw2_asm_output_data_uleb128 (str_form, get_DW_FORM_name (str_form));
11512 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11513 if (str_form == DW_FORM_string)
11515 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11516 for (i = 1 - idx_offset; i < ndirs; i++)
11517 dw2_asm_output_nstring (dirs[i].path,
11518 dirs[i].length
11519 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11520 "Directory Entry: %#x", i + idx_offset);
11522 else
11524 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11525 for (i = 1 - idx_offset; i < ndirs; i++)
11527 const char *str
11528 = ggc_alloc_string (dirs[i].path,
11529 dirs[i].length
11530 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11531 output_line_string (str_form, str, "Directory Entry",
11532 (unsigned) i + idx_offset);
11536 else
11538 for (i = 1 - idx_offset; i < ndirs; i++)
11539 dw2_asm_output_nstring (dirs[i].path,
11540 dirs[i].length
11541 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11542 "Directory Entry: %#x", i + idx_offset);
11544 dw2_asm_output_data (1, 0, "End directory table");
11547 /* We have to emit them in the order of emitted_number since that's
11548 used in the debug info generation. To do this efficiently we
11549 generate a back-mapping of the indices first. */
11550 backmap = XALLOCAVEC (int, numfiles);
11551 for (i = 0; i < numfiles; i++)
11552 backmap[files[i].file_idx->emitted_number - 1] = i;
11554 if (dwarf_version >= 5)
11556 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11557 if (filename0 == NULL)
11558 filename0 = "";
11559 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11560 DW_FORM_data2. Choose one based on the number of directories
11561 and how much space would they occupy in each encoding.
11562 If we have at most 256 directories, all indexes fit into
11563 a single byte, so DW_FORM_data1 is most compact (if there
11564 are at most 128 directories, DW_FORM_udata would be as
11565 compact as that, but not shorter and slower to decode). */
11566 if (ndirs + idx_offset <= 256)
11567 idx_form = DW_FORM_data1;
11568 /* If there are more than 65536 directories, we have to use
11569 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11570 Otherwise, compute what space would occupy if all the indexes
11571 used DW_FORM_udata - sum - and compare that to how large would
11572 be DW_FORM_data2 encoding, and pick the more efficient one. */
11573 else if (ndirs + idx_offset <= 65536)
11575 unsigned HOST_WIDE_INT sum = 1;
11576 for (i = 0; i < numfiles; i++)
11578 int file_idx = backmap[i];
11579 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11580 sum += size_of_uleb128 (dir_idx);
11582 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11583 idx_form = DW_FORM_data2;
11585 #ifdef VMS_DEBUGGING_INFO
11586 dw2_asm_output_data (1, 4, "File name entry format count");
11587 #else
11588 dw2_asm_output_data (1, 2, "File name entry format count");
11589 #endif
11590 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11591 dw2_asm_output_data_uleb128 (str_form, get_DW_FORM_name (str_form));
11592 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11593 "DW_LNCT_directory_index");
11594 dw2_asm_output_data_uleb128 (idx_form, get_DW_FORM_name (idx_form));
11595 #ifdef VMS_DEBUGGING_INFO
11596 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11597 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11598 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11599 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11600 #endif
11601 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11603 output_line_string (str_form, filename0, "File Entry", 0);
11605 /* Include directory index. */
11606 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11607 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11608 0, NULL);
11609 else
11610 dw2_asm_output_data_uleb128 (0, NULL);
11612 #ifdef VMS_DEBUGGING_INFO
11613 dw2_asm_output_data_uleb128 (0, NULL);
11614 dw2_asm_output_data_uleb128 (0, NULL);
11615 #endif
11618 /* Now write all the file names. */
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;
11624 #ifdef VMS_DEBUGGING_INFO
11625 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11627 /* Setting these fields can lead to debugger miscomparisons,
11628 but VMS Debug requires them to be set correctly. */
11630 int ver;
11631 long long cdt;
11632 long siz;
11633 int maxfilelen = (strlen (files[file_idx].path)
11634 + dirs[dir_idx].length
11635 + MAX_VMS_VERSION_LEN + 1);
11636 char *filebuf = XALLOCAVEC (char, maxfilelen);
11638 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11639 snprintf (filebuf, maxfilelen, "%s;%d",
11640 files[file_idx].path + dirs[dir_idx].length, ver);
11642 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11644 /* Include directory index. */
11645 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11646 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11647 dir_idx + idx_offset, NULL);
11648 else
11649 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11651 /* Modification time. */
11652 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11653 &cdt, 0, 0, 0) == 0)
11654 ? cdt : 0, NULL);
11656 /* File length in bytes. */
11657 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11658 0, &siz, 0, 0) == 0)
11659 ? siz : 0, NULL);
11660 #else
11661 output_line_string (str_form,
11662 files[file_idx].path + dirs[dir_idx].length,
11663 "File Entry", (unsigned) i + 1);
11665 /* Include directory index. */
11666 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11667 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11668 dir_idx + idx_offset, NULL);
11669 else
11670 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11672 if (dwarf_version >= 5)
11673 continue;
11675 /* Modification time. */
11676 dw2_asm_output_data_uleb128 (0, NULL);
11678 /* File length in bytes. */
11679 dw2_asm_output_data_uleb128 (0, NULL);
11680 #endif /* VMS_DEBUGGING_INFO */
11683 if (dwarf_version < 5)
11684 dw2_asm_output_data (1, 0, "End file name table");
11688 /* Output one line number table into the .debug_line section. */
11690 static void
11691 output_one_line_info_table (dw_line_info_table *table)
11693 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11694 unsigned int current_line = 1;
11695 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11696 dw_line_info_entry *ent;
11697 size_t i;
11699 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11701 switch (ent->opcode)
11703 case LI_set_address:
11704 /* ??? Unfortunately, we have little choice here currently, and
11705 must always use the most general form. GCC does not know the
11706 address delta itself, so we can't use DW_LNS_advance_pc. Many
11707 ports do have length attributes which will give an upper bound
11708 on the address range. We could perhaps use length attributes
11709 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11710 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11712 /* This can handle any delta. This takes
11713 4+DWARF2_ADDR_SIZE bytes. */
11714 dw2_asm_output_data (1, 0, "set address %s", line_label);
11715 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11716 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11717 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11718 break;
11720 case LI_set_line:
11721 if (ent->val == current_line)
11723 /* We still need to start a new row, so output a copy insn. */
11724 dw2_asm_output_data (1, DW_LNS_copy,
11725 "copy line %u", current_line);
11727 else
11729 int line_offset = ent->val - current_line;
11730 int line_delta = line_offset - DWARF_LINE_BASE;
11732 current_line = ent->val;
11733 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11735 /* This can handle deltas from -10 to 234, using the current
11736 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11737 This takes 1 byte. */
11738 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11739 "line %u", current_line);
11741 else
11743 /* This can handle any delta. This takes at least 4 bytes,
11744 depending on the value being encoded. */
11745 dw2_asm_output_data (1, DW_LNS_advance_line,
11746 "advance to line %u", current_line);
11747 dw2_asm_output_data_sleb128 (line_offset, NULL);
11748 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11751 break;
11753 case LI_set_file:
11754 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11755 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11756 break;
11758 case LI_set_column:
11759 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11760 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11761 break;
11763 case LI_negate_stmt:
11764 current_is_stmt = !current_is_stmt;
11765 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11766 "is_stmt %d", current_is_stmt);
11767 break;
11769 case LI_set_prologue_end:
11770 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11771 "set prologue end");
11772 break;
11774 case LI_set_epilogue_begin:
11775 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11776 "set epilogue begin");
11777 break;
11779 case LI_set_discriminator:
11780 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11781 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11782 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11783 dw2_asm_output_data_uleb128 (ent->val, NULL);
11784 break;
11788 /* Emit debug info for the address of the end of the table. */
11789 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11790 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11791 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11792 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11794 dw2_asm_output_data (1, 0, "end sequence");
11795 dw2_asm_output_data_uleb128 (1, NULL);
11796 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11799 /* Output the source line number correspondence information. This
11800 information goes into the .debug_line section. */
11802 static void
11803 output_line_info (bool prologue_only)
11805 static unsigned int generation;
11806 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11807 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11808 bool saw_one = false;
11809 int opc;
11811 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11812 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11813 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11814 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11816 if (!XCOFF_DEBUGGING_INFO)
11818 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11819 dw2_asm_output_data (4, 0xffffffff,
11820 "Initial length escape value indicating 64-bit DWARF extension");
11821 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11822 "Length of Source Line Info");
11825 ASM_OUTPUT_LABEL (asm_out_file, l1);
11827 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11828 if (dwarf_version >= 5)
11830 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11831 dw2_asm_output_data (1, 0, "Segment Size");
11833 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11834 ASM_OUTPUT_LABEL (asm_out_file, p1);
11836 /* Define the architecture-dependent minimum instruction length (in bytes).
11837 In this implementation of DWARF, this field is used for information
11838 purposes only. Since GCC generates assembly language, we have no
11839 a priori knowledge of how many instruction bytes are generated for each
11840 source line, and therefore can use only the DW_LNE_set_address and
11841 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11842 this as '1', which is "correct enough" for all architectures,
11843 and don't let the target override. */
11844 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11846 if (dwarf_version >= 4)
11847 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11848 "Maximum Operations Per Instruction");
11849 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11850 "Default is_stmt_start flag");
11851 dw2_asm_output_data (1, DWARF_LINE_BASE,
11852 "Line Base Value (Special Opcodes)");
11853 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11854 "Line Range Value (Special Opcodes)");
11855 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11856 "Special Opcode Base");
11858 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11860 int n_op_args;
11861 switch (opc)
11863 case DW_LNS_advance_pc:
11864 case DW_LNS_advance_line:
11865 case DW_LNS_set_file:
11866 case DW_LNS_set_column:
11867 case DW_LNS_fixed_advance_pc:
11868 case DW_LNS_set_isa:
11869 n_op_args = 1;
11870 break;
11871 default:
11872 n_op_args = 0;
11873 break;
11876 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11877 opc, n_op_args);
11880 /* Write out the information about the files we use. */
11881 output_file_names ();
11882 ASM_OUTPUT_LABEL (asm_out_file, p2);
11883 if (prologue_only)
11885 /* Output the marker for the end of the line number info. */
11886 ASM_OUTPUT_LABEL (asm_out_file, l2);
11887 return;
11890 if (separate_line_info)
11892 dw_line_info_table *table;
11893 size_t i;
11895 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11896 if (table->in_use)
11898 output_one_line_info_table (table);
11899 saw_one = true;
11902 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
11904 output_one_line_info_table (cold_text_section_line_info);
11905 saw_one = true;
11908 /* ??? Some Darwin linkers crash on a .debug_line section with no
11909 sequences. Further, merely a DW_LNE_end_sequence entry is not
11910 sufficient -- the address column must also be initialized.
11911 Make sure to output at least one set_address/end_sequence pair,
11912 choosing .text since that section is always present. */
11913 if (text_section_line_info->in_use || !saw_one)
11914 output_one_line_info_table (text_section_line_info);
11916 /* Output the marker for the end of the line number info. */
11917 ASM_OUTPUT_LABEL (asm_out_file, l2);
11920 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
11922 static inline bool
11923 need_endianity_attribute_p (bool reverse)
11925 return reverse && (dwarf_version >= 3 || !dwarf_strict);
11928 /* Given a pointer to a tree node for some base type, return a pointer to
11929 a DIE that describes the given type. REVERSE is true if the type is
11930 to be interpreted in the reverse storage order wrt the target order.
11932 This routine must only be called for GCC type nodes that correspond to
11933 Dwarf base (fundamental) types. */
11935 static dw_die_ref
11936 base_type_die (tree type, bool reverse)
11938 dw_die_ref base_type_result;
11939 enum dwarf_type encoding;
11940 bool fpt_used = false;
11941 struct fixed_point_type_info fpt_info;
11942 tree type_bias = NULL_TREE;
11944 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11945 return 0;
11947 /* If this is a subtype that should not be emitted as a subrange type,
11948 use the base type. See subrange_type_for_debug_p. */
11949 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11950 type = TREE_TYPE (type);
11952 switch (TREE_CODE (type))
11954 case INTEGER_TYPE:
11955 if ((dwarf_version >= 4 || !dwarf_strict)
11956 && TYPE_NAME (type)
11957 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11958 && DECL_IS_BUILTIN (TYPE_NAME (type))
11959 && DECL_NAME (TYPE_NAME (type)))
11961 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
11962 if (strcmp (name, "char16_t") == 0
11963 || strcmp (name, "char32_t") == 0)
11965 encoding = DW_ATE_UTF;
11966 break;
11969 if ((dwarf_version >= 3 || !dwarf_strict)
11970 && lang_hooks.types.get_fixed_point_type_info)
11972 memset (&fpt_info, 0, sizeof (fpt_info));
11973 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
11975 fpt_used = true;
11976 encoding = ((TYPE_UNSIGNED (type))
11977 ? DW_ATE_unsigned_fixed
11978 : DW_ATE_signed_fixed);
11979 break;
11982 if (TYPE_STRING_FLAG (type))
11984 if (TYPE_UNSIGNED (type))
11985 encoding = DW_ATE_unsigned_char;
11986 else
11987 encoding = DW_ATE_signed_char;
11989 else if (TYPE_UNSIGNED (type))
11990 encoding = DW_ATE_unsigned;
11991 else
11992 encoding = DW_ATE_signed;
11994 if (!dwarf_strict
11995 && lang_hooks.types.get_type_bias)
11996 type_bias = lang_hooks.types.get_type_bias (type);
11997 break;
11999 case REAL_TYPE:
12000 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12002 if (dwarf_version >= 3 || !dwarf_strict)
12003 encoding = DW_ATE_decimal_float;
12004 else
12005 encoding = DW_ATE_lo_user;
12007 else
12008 encoding = DW_ATE_float;
12009 break;
12011 case FIXED_POINT_TYPE:
12012 if (!(dwarf_version >= 3 || !dwarf_strict))
12013 encoding = DW_ATE_lo_user;
12014 else if (TYPE_UNSIGNED (type))
12015 encoding = DW_ATE_unsigned_fixed;
12016 else
12017 encoding = DW_ATE_signed_fixed;
12018 break;
12020 /* Dwarf2 doesn't know anything about complex ints, so use
12021 a user defined type for it. */
12022 case COMPLEX_TYPE:
12023 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12024 encoding = DW_ATE_complex_float;
12025 else
12026 encoding = DW_ATE_lo_user;
12027 break;
12029 case BOOLEAN_TYPE:
12030 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12031 encoding = DW_ATE_boolean;
12032 break;
12034 default:
12035 /* No other TREE_CODEs are Dwarf fundamental types. */
12036 gcc_unreachable ();
12039 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12041 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12042 int_size_in_bytes (type));
12043 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12045 if (need_endianity_attribute_p (reverse))
12046 add_AT_unsigned (base_type_result, DW_AT_endianity,
12047 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12049 if (fpt_used)
12051 switch (fpt_info.scale_factor_kind)
12053 case fixed_point_scale_factor_binary:
12054 add_AT_int (base_type_result, DW_AT_binary_scale,
12055 fpt_info.scale_factor.binary);
12056 break;
12058 case fixed_point_scale_factor_decimal:
12059 add_AT_int (base_type_result, DW_AT_decimal_scale,
12060 fpt_info.scale_factor.decimal);
12061 break;
12063 case fixed_point_scale_factor_arbitrary:
12064 /* Arbitrary scale factors cannot be described in standard DWARF,
12065 yet. */
12066 if (!dwarf_strict)
12068 /* Describe the scale factor as a rational constant. */
12069 const dw_die_ref scale_factor
12070 = new_die (DW_TAG_constant, comp_unit_die (), type);
12072 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12073 fpt_info.scale_factor.arbitrary.numerator);
12074 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12075 fpt_info.scale_factor.arbitrary.denominator);
12077 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12079 break;
12081 default:
12082 gcc_unreachable ();
12086 if (type_bias)
12087 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12088 dw_scalar_form_constant
12089 | dw_scalar_form_exprloc
12090 | dw_scalar_form_reference,
12091 NULL);
12093 add_pubtype (type, base_type_result);
12095 return base_type_result;
12098 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12099 named 'auto' in its type: return true for it, false otherwise. */
12101 static inline bool
12102 is_cxx_auto (tree type)
12104 if (is_cxx ())
12106 tree name = TYPE_IDENTIFIER (type);
12107 if (name == get_identifier ("auto")
12108 || name == get_identifier ("decltype(auto)"))
12109 return true;
12111 return false;
12114 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12115 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12117 static inline int
12118 is_base_type (tree type)
12120 switch (TREE_CODE (type))
12122 case ERROR_MARK:
12123 case VOID_TYPE:
12124 case INTEGER_TYPE:
12125 case REAL_TYPE:
12126 case FIXED_POINT_TYPE:
12127 case COMPLEX_TYPE:
12128 case BOOLEAN_TYPE:
12129 case POINTER_BOUNDS_TYPE:
12130 return 1;
12132 case ARRAY_TYPE:
12133 case RECORD_TYPE:
12134 case UNION_TYPE:
12135 case QUAL_UNION_TYPE:
12136 case ENUMERAL_TYPE:
12137 case FUNCTION_TYPE:
12138 case METHOD_TYPE:
12139 case POINTER_TYPE:
12140 case REFERENCE_TYPE:
12141 case NULLPTR_TYPE:
12142 case OFFSET_TYPE:
12143 case LANG_TYPE:
12144 case VECTOR_TYPE:
12145 return 0;
12147 default:
12148 if (is_cxx_auto (type))
12149 return 0;
12150 gcc_unreachable ();
12153 return 0;
12156 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12157 node, return the size in bits for the type if it is a constant, or else
12158 return the alignment for the type if the type's size is not constant, or
12159 else return BITS_PER_WORD if the type actually turns out to be an
12160 ERROR_MARK node. */
12162 static inline unsigned HOST_WIDE_INT
12163 simple_type_size_in_bits (const_tree type)
12165 if (TREE_CODE (type) == ERROR_MARK)
12166 return BITS_PER_WORD;
12167 else if (TYPE_SIZE (type) == NULL_TREE)
12168 return 0;
12169 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12170 return tree_to_uhwi (TYPE_SIZE (type));
12171 else
12172 return TYPE_ALIGN (type);
12175 /* Similarly, but return an offset_int instead of UHWI. */
12177 static inline offset_int
12178 offset_int_type_size_in_bits (const_tree type)
12180 if (TREE_CODE (type) == ERROR_MARK)
12181 return BITS_PER_WORD;
12182 else if (TYPE_SIZE (type) == NULL_TREE)
12183 return 0;
12184 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12185 return wi::to_offset (TYPE_SIZE (type));
12186 else
12187 return TYPE_ALIGN (type);
12190 /* Given a pointer to a tree node for a subrange type, return a pointer
12191 to a DIE that describes the given type. */
12193 static dw_die_ref
12194 subrange_type_die (tree type, tree low, tree high, tree bias,
12195 dw_die_ref context_die)
12197 dw_die_ref subrange_die;
12198 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12200 if (context_die == NULL)
12201 context_die = comp_unit_die ();
12203 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12205 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12207 /* The size of the subrange type and its base type do not match,
12208 so we need to generate a size attribute for the subrange type. */
12209 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12212 if (low)
12213 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12214 if (high)
12215 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12216 if (bias && !dwarf_strict)
12217 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12218 dw_scalar_form_constant
12219 | dw_scalar_form_exprloc
12220 | dw_scalar_form_reference,
12221 NULL);
12223 return subrange_die;
12226 /* Returns the (const and/or volatile) cv_qualifiers associated with
12227 the decl node. This will normally be augmented with the
12228 cv_qualifiers of the underlying type in add_type_attribute. */
12230 static int
12231 decl_quals (const_tree decl)
12233 return ((TREE_READONLY (decl)
12234 /* The C++ front-end correctly marks reference-typed
12235 variables as readonly, but from a language (and debug
12236 info) standpoint they are not const-qualified. */
12237 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12238 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12239 | (TREE_THIS_VOLATILE (decl)
12240 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12243 /* Determine the TYPE whose qualifiers match the largest strict subset
12244 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12245 qualifiers outside QUAL_MASK. */
12247 static int
12248 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12250 tree t;
12251 int best_rank = 0, best_qual = 0, max_rank;
12253 type_quals &= qual_mask;
12254 max_rank = popcount_hwi (type_quals) - 1;
12256 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12257 t = TYPE_NEXT_VARIANT (t))
12259 int q = TYPE_QUALS (t) & qual_mask;
12261 if ((q & type_quals) == q && q != type_quals
12262 && check_base_type (t, type))
12264 int rank = popcount_hwi (q);
12266 if (rank > best_rank)
12268 best_rank = rank;
12269 best_qual = q;
12274 return best_qual;
12277 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12278 static const dwarf_qual_info_t dwarf_qual_info[] =
12280 { TYPE_QUAL_CONST, DW_TAG_const_type },
12281 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12282 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12283 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12285 static const unsigned int dwarf_qual_info_size
12286 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12288 /* If DIE is a qualified DIE of some base DIE with the same parent,
12289 return the base DIE, otherwise return NULL. Set MASK to the
12290 qualifiers added compared to the returned DIE. */
12292 static dw_die_ref
12293 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12295 unsigned int i;
12296 for (i = 0; i < dwarf_qual_info_size; i++)
12297 if (die->die_tag == dwarf_qual_info[i].t)
12298 break;
12299 if (i == dwarf_qual_info_size)
12300 return NULL;
12301 if (vec_safe_length (die->die_attr) != 1)
12302 return NULL;
12303 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12304 if (type == NULL || type->die_parent != die->die_parent)
12305 return NULL;
12306 *mask |= dwarf_qual_info[i].q;
12307 if (depth)
12309 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12310 if (ret)
12311 return ret;
12313 return type;
12316 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12317 entry that chains the modifiers specified by CV_QUALS in front of the
12318 given type. REVERSE is true if the type is to be interpreted in the
12319 reverse storage order wrt the target order. */
12321 static dw_die_ref
12322 modified_type_die (tree type, int cv_quals, bool reverse,
12323 dw_die_ref context_die)
12325 enum tree_code code = TREE_CODE (type);
12326 dw_die_ref mod_type_die;
12327 dw_die_ref sub_die = NULL;
12328 tree item_type = NULL;
12329 tree qualified_type;
12330 tree name, low, high;
12331 dw_die_ref mod_scope;
12332 /* Only these cv-qualifiers are currently handled. */
12333 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12334 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12336 if (code == ERROR_MARK)
12337 return NULL;
12339 if (lang_hooks.types.get_debug_type)
12341 tree debug_type = lang_hooks.types.get_debug_type (type);
12343 if (debug_type != NULL_TREE && debug_type != type)
12344 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12347 cv_quals &= cv_qual_mask;
12349 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12350 tag modifier (and not an attribute) old consumers won't be able
12351 to handle it. */
12352 if (dwarf_version < 3)
12353 cv_quals &= ~TYPE_QUAL_RESTRICT;
12355 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12356 if (dwarf_version < 5)
12357 cv_quals &= ~TYPE_QUAL_ATOMIC;
12359 /* See if we already have the appropriately qualified variant of
12360 this type. */
12361 qualified_type = get_qualified_type (type, cv_quals);
12363 if (qualified_type == sizetype
12364 && TYPE_NAME (qualified_type)
12365 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12367 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12369 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12370 && TYPE_PRECISION (t)
12371 == TYPE_PRECISION (qualified_type)
12372 && TYPE_UNSIGNED (t)
12373 == TYPE_UNSIGNED (qualified_type));
12374 qualified_type = t;
12377 /* If we do, then we can just use its DIE, if it exists. */
12378 if (qualified_type)
12380 mod_type_die = lookup_type_die (qualified_type);
12382 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12383 if (mod_type_die
12384 && (!need_endianity_attribute_p (reverse)
12385 || !is_base_type (type)
12386 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12387 return mod_type_die;
12390 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12392 /* Handle C typedef types. */
12393 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12394 && !DECL_ARTIFICIAL (name))
12396 tree dtype = TREE_TYPE (name);
12398 if (qualified_type == dtype)
12400 /* For a named type, use the typedef. */
12401 gen_type_die (qualified_type, context_die);
12402 return lookup_type_die (qualified_type);
12404 else
12406 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12407 dquals &= cv_qual_mask;
12408 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12409 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12410 /* cv-unqualified version of named type. Just use
12411 the unnamed type to which it refers. */
12412 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12413 reverse, context_die);
12414 /* Else cv-qualified version of named type; fall through. */
12418 mod_scope = scope_die_for (type, context_die);
12420 if (cv_quals)
12422 int sub_quals = 0, first_quals = 0;
12423 unsigned i;
12424 dw_die_ref first = NULL, last = NULL;
12426 /* Determine a lesser qualified type that most closely matches
12427 this one. Then generate DW_TAG_* entries for the remaining
12428 qualifiers. */
12429 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12430 cv_qual_mask);
12431 if (sub_quals && use_debug_types)
12433 bool needed = false;
12434 /* If emitting type units, make sure the order of qualifiers
12435 is canonical. Thus, start from unqualified type if
12436 an earlier qualifier is missing in sub_quals, but some later
12437 one is present there. */
12438 for (i = 0; i < dwarf_qual_info_size; i++)
12439 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12440 needed = true;
12441 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12443 sub_quals = 0;
12444 break;
12447 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12448 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12450 /* As not all intermediate qualified DIEs have corresponding
12451 tree types, ensure that qualified DIEs in the same scope
12452 as their DW_AT_type are emitted after their DW_AT_type,
12453 only with other qualified DIEs for the same type possibly
12454 in between them. Determine the range of such qualified
12455 DIEs now (first being the base type, last being corresponding
12456 last qualified DIE for it). */
12457 unsigned int count = 0;
12458 first = qualified_die_p (mod_type_die, &first_quals,
12459 dwarf_qual_info_size);
12460 if (first == NULL)
12461 first = mod_type_die;
12462 gcc_assert ((first_quals & ~sub_quals) == 0);
12463 for (count = 0, last = first;
12464 count < (1U << dwarf_qual_info_size);
12465 count++, last = last->die_sib)
12467 int quals = 0;
12468 if (last == mod_scope->die_child)
12469 break;
12470 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12471 != first)
12472 break;
12476 for (i = 0; i < dwarf_qual_info_size; i++)
12477 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12479 dw_die_ref d;
12480 if (first && first != last)
12482 for (d = first->die_sib; ; d = d->die_sib)
12484 int quals = 0;
12485 qualified_die_p (d, &quals, dwarf_qual_info_size);
12486 if (quals == (first_quals | dwarf_qual_info[i].q))
12487 break;
12488 if (d == last)
12490 d = NULL;
12491 break;
12494 if (d)
12496 mod_type_die = d;
12497 continue;
12500 if (first)
12502 d = ggc_cleared_alloc<die_node> ();
12503 d->die_tag = dwarf_qual_info[i].t;
12504 add_child_die_after (mod_scope, d, last);
12505 last = d;
12507 else
12508 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12509 if (mod_type_die)
12510 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12511 mod_type_die = d;
12512 first_quals |= dwarf_qual_info[i].q;
12515 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12517 dwarf_tag tag = DW_TAG_pointer_type;
12518 if (code == REFERENCE_TYPE)
12520 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12521 tag = DW_TAG_rvalue_reference_type;
12522 else
12523 tag = DW_TAG_reference_type;
12525 mod_type_die = new_die (tag, mod_scope, type);
12527 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12528 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12529 item_type = TREE_TYPE (type);
12531 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12532 if (!ADDR_SPACE_GENERIC_P (as))
12534 int action = targetm.addr_space.debug (as);
12535 if (action >= 0)
12537 /* Positive values indicate an address_class. */
12538 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12540 else
12542 /* Negative values indicate an (inverted) segment base reg. */
12543 dw_loc_descr_ref d
12544 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12545 add_AT_loc (mod_type_die, DW_AT_segment, d);
12549 else if (code == INTEGER_TYPE
12550 && TREE_TYPE (type) != NULL_TREE
12551 && subrange_type_for_debug_p (type, &low, &high))
12553 tree bias = NULL_TREE;
12554 if (lang_hooks.types.get_type_bias)
12555 bias = lang_hooks.types.get_type_bias (type);
12556 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12557 item_type = TREE_TYPE (type);
12559 else if (is_base_type (type))
12560 mod_type_die = base_type_die (type, reverse);
12561 else
12563 gen_type_die (type, context_die);
12565 /* We have to get the type_main_variant here (and pass that to the
12566 `lookup_type_die' routine) because the ..._TYPE node we have
12567 might simply be a *copy* of some original type node (where the
12568 copy was created to help us keep track of typedef names) and
12569 that copy might have a different TYPE_UID from the original
12570 ..._TYPE node. */
12571 if (TREE_CODE (type) == FUNCTION_TYPE
12572 || TREE_CODE (type) == METHOD_TYPE)
12574 /* For function/method types, can't just use type_main_variant here,
12575 because that can have different ref-qualifiers for C++,
12576 but try to canonicalize. */
12577 tree main = TYPE_MAIN_VARIANT (type);
12578 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12579 if (check_base_type (t, main) && check_lang_type (t, type))
12580 return lookup_type_die (t);
12581 return lookup_type_die (type);
12583 else if (TREE_CODE (type) != VECTOR_TYPE
12584 && TREE_CODE (type) != ARRAY_TYPE)
12585 return lookup_type_die (type_main_variant (type));
12586 else
12587 /* Vectors have the debugging information in the type,
12588 not the main variant. */
12589 return lookup_type_die (type);
12592 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12593 don't output a DW_TAG_typedef, since there isn't one in the
12594 user's program; just attach a DW_AT_name to the type.
12595 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12596 if the base type already has the same name. */
12597 if (name
12598 && ((TREE_CODE (name) != TYPE_DECL
12599 && (qualified_type == TYPE_MAIN_VARIANT (type)
12600 || (cv_quals == TYPE_UNQUALIFIED)))
12601 || (TREE_CODE (name) == TYPE_DECL
12602 && TREE_TYPE (name) == qualified_type
12603 && DECL_NAME (name))))
12605 if (TREE_CODE (name) == TYPE_DECL)
12606 /* Could just call add_name_and_src_coords_attributes here,
12607 but since this is a builtin type it doesn't have any
12608 useful source coordinates anyway. */
12609 name = DECL_NAME (name);
12610 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12612 /* This probably indicates a bug. */
12613 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12615 name = TYPE_IDENTIFIER (type);
12616 add_name_attribute (mod_type_die,
12617 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12620 if (qualified_type)
12621 equate_type_number_to_die (qualified_type, mod_type_die);
12623 if (item_type)
12624 /* We must do this after the equate_type_number_to_die call, in case
12625 this is a recursive type. This ensures that the modified_type_die
12626 recursion will terminate even if the type is recursive. Recursive
12627 types are possible in Ada. */
12628 sub_die = modified_type_die (item_type,
12629 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12630 reverse,
12631 context_die);
12633 if (sub_die != NULL)
12634 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12636 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12637 if (TYPE_ARTIFICIAL (type))
12638 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12640 return mod_type_die;
12643 /* Generate DIEs for the generic parameters of T.
12644 T must be either a generic type or a generic function.
12645 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12647 static void
12648 gen_generic_params_dies (tree t)
12650 tree parms, args;
12651 int parms_num, i;
12652 dw_die_ref die = NULL;
12653 int non_default;
12655 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12656 return;
12658 if (TYPE_P (t))
12659 die = lookup_type_die (t);
12660 else if (DECL_P (t))
12661 die = lookup_decl_die (t);
12663 gcc_assert (die);
12665 parms = lang_hooks.get_innermost_generic_parms (t);
12666 if (!parms)
12667 /* T has no generic parameter. It means T is neither a generic type
12668 or function. End of story. */
12669 return;
12671 parms_num = TREE_VEC_LENGTH (parms);
12672 args = lang_hooks.get_innermost_generic_args (t);
12673 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12674 non_default = int_cst_value (TREE_CHAIN (args));
12675 else
12676 non_default = TREE_VEC_LENGTH (args);
12677 for (i = 0; i < parms_num; i++)
12679 tree parm, arg, arg_pack_elems;
12680 dw_die_ref parm_die;
12682 parm = TREE_VEC_ELT (parms, i);
12683 arg = TREE_VEC_ELT (args, i);
12684 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12685 gcc_assert (parm && TREE_VALUE (parm) && arg);
12687 if (parm && TREE_VALUE (parm) && arg)
12689 /* If PARM represents a template parameter pack,
12690 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12691 by DW_TAG_template_*_parameter DIEs for the argument
12692 pack elements of ARG. Note that ARG would then be
12693 an argument pack. */
12694 if (arg_pack_elems)
12695 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12696 arg_pack_elems,
12697 die);
12698 else
12699 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12700 true /* emit name */, die);
12701 if (i >= non_default)
12702 add_AT_flag (parm_die, DW_AT_default_value, 1);
12707 /* Create and return a DIE for PARM which should be
12708 the representation of a generic type parameter.
12709 For instance, in the C++ front end, PARM would be a template parameter.
12710 ARG is the argument to PARM.
12711 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12712 name of the PARM.
12713 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12714 as a child node. */
12716 static dw_die_ref
12717 generic_parameter_die (tree parm, tree arg,
12718 bool emit_name_p,
12719 dw_die_ref parent_die)
12721 dw_die_ref tmpl_die = NULL;
12722 const char *name = NULL;
12724 if (!parm || !DECL_NAME (parm) || !arg)
12725 return NULL;
12727 /* We support non-type generic parameters and arguments,
12728 type generic parameters and arguments, as well as
12729 generic generic parameters (a.k.a. template template parameters in C++)
12730 and arguments. */
12731 if (TREE_CODE (parm) == PARM_DECL)
12732 /* PARM is a nontype generic parameter */
12733 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12734 else if (TREE_CODE (parm) == TYPE_DECL)
12735 /* PARM is a type generic parameter. */
12736 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12737 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12738 /* PARM is a generic generic parameter.
12739 Its DIE is a GNU extension. It shall have a
12740 DW_AT_name attribute to represent the name of the template template
12741 parameter, and a DW_AT_GNU_template_name attribute to represent the
12742 name of the template template argument. */
12743 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12744 parent_die, parm);
12745 else
12746 gcc_unreachable ();
12748 if (tmpl_die)
12750 tree tmpl_type;
12752 /* If PARM is a generic parameter pack, it means we are
12753 emitting debug info for a template argument pack element.
12754 In other terms, ARG is a template argument pack element.
12755 In that case, we don't emit any DW_AT_name attribute for
12756 the die. */
12757 if (emit_name_p)
12759 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12760 gcc_assert (name);
12761 add_AT_string (tmpl_die, DW_AT_name, name);
12764 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12766 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12767 TMPL_DIE should have a child DW_AT_type attribute that is set
12768 to the type of the argument to PARM, which is ARG.
12769 If PARM is a type generic parameter, TMPL_DIE should have a
12770 child DW_AT_type that is set to ARG. */
12771 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12772 add_type_attribute (tmpl_die, tmpl_type,
12773 (TREE_THIS_VOLATILE (tmpl_type)
12774 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12775 false, parent_die);
12777 else
12779 /* So TMPL_DIE is a DIE representing a
12780 a generic generic template parameter, a.k.a template template
12781 parameter in C++ and arg is a template. */
12783 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12784 to the name of the argument. */
12785 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12786 if (name)
12787 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12790 if (TREE_CODE (parm) == PARM_DECL)
12791 /* So PARM is a non-type generic parameter.
12792 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12793 attribute of TMPL_DIE which value represents the value
12794 of ARG.
12795 We must be careful here:
12796 The value of ARG might reference some function decls.
12797 We might currently be emitting debug info for a generic
12798 type and types are emitted before function decls, we don't
12799 know if the function decls referenced by ARG will actually be
12800 emitted after cgraph computations.
12801 So must defer the generation of the DW_AT_const_value to
12802 after cgraph is ready. */
12803 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12806 return tmpl_die;
12809 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12810 PARM_PACK must be a template parameter pack. The returned DIE
12811 will be child DIE of PARENT_DIE. */
12813 static dw_die_ref
12814 template_parameter_pack_die (tree parm_pack,
12815 tree parm_pack_args,
12816 dw_die_ref parent_die)
12818 dw_die_ref die;
12819 int j;
12821 gcc_assert (parent_die && parm_pack);
12823 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12824 add_name_and_src_coords_attributes (die, parm_pack);
12825 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12826 generic_parameter_die (parm_pack,
12827 TREE_VEC_ELT (parm_pack_args, j),
12828 false /* Don't emit DW_AT_name */,
12829 die);
12830 return die;
12833 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12834 an enumerated type. */
12836 static inline int
12837 type_is_enum (const_tree type)
12839 return TREE_CODE (type) == ENUMERAL_TYPE;
12842 /* Return the DBX register number described by a given RTL node. */
12844 static unsigned int
12845 dbx_reg_number (const_rtx rtl)
12847 unsigned regno = REGNO (rtl);
12849 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12851 #ifdef LEAF_REG_REMAP
12852 if (crtl->uses_only_leaf_regs)
12854 int leaf_reg = LEAF_REG_REMAP (regno);
12855 if (leaf_reg != -1)
12856 regno = (unsigned) leaf_reg;
12858 #endif
12860 regno = DBX_REGISTER_NUMBER (regno);
12861 gcc_assert (regno != INVALID_REGNUM);
12862 return regno;
12865 /* Optionally add a DW_OP_piece term to a location description expression.
12866 DW_OP_piece is only added if the location description expression already
12867 doesn't end with DW_OP_piece. */
12869 static void
12870 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12872 dw_loc_descr_ref loc;
12874 if (*list_head != NULL)
12876 /* Find the end of the chain. */
12877 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12880 if (loc->dw_loc_opc != DW_OP_piece)
12881 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12885 /* Return a location descriptor that designates a machine register or
12886 zero if there is none. */
12888 static dw_loc_descr_ref
12889 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12891 rtx regs;
12893 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12894 return 0;
12896 /* We only use "frame base" when we're sure we're talking about the
12897 post-prologue local stack frame. We do this by *not* running
12898 register elimination until this point, and recognizing the special
12899 argument pointer and soft frame pointer rtx's.
12900 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
12901 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
12902 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
12904 dw_loc_descr_ref result = NULL;
12906 if (dwarf_version >= 4 || !dwarf_strict)
12908 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
12909 initialized);
12910 if (result)
12911 add_loc_descr (&result,
12912 new_loc_descr (DW_OP_stack_value, 0, 0));
12914 return result;
12917 regs = targetm.dwarf_register_span (rtl);
12919 if (REG_NREGS (rtl) > 1 || regs)
12920 return multiple_reg_loc_descriptor (rtl, regs, initialized);
12921 else
12923 unsigned int dbx_regnum = dbx_reg_number (rtl);
12924 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12925 return 0;
12926 return one_reg_loc_descriptor (dbx_regnum, initialized);
12930 /* Return a location descriptor that designates a machine register for
12931 a given hard register number. */
12933 static dw_loc_descr_ref
12934 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12936 dw_loc_descr_ref reg_loc_descr;
12938 if (regno <= 31)
12939 reg_loc_descr
12940 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12941 else
12942 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12944 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12945 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12947 return reg_loc_descr;
12950 /* Given an RTL of a register, return a location descriptor that
12951 designates a value that spans more than one register. */
12953 static dw_loc_descr_ref
12954 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
12955 enum var_init_status initialized)
12957 int size, i;
12958 dw_loc_descr_ref loc_result = NULL;
12960 /* Simple, contiguous registers. */
12961 if (regs == NULL_RTX)
12963 unsigned reg = REGNO (rtl);
12964 int nregs;
12966 #ifdef LEAF_REG_REMAP
12967 if (crtl->uses_only_leaf_regs)
12969 int leaf_reg = LEAF_REG_REMAP (reg);
12970 if (leaf_reg != -1)
12971 reg = (unsigned) leaf_reg;
12973 #endif
12975 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
12976 nregs = REG_NREGS (rtl);
12978 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
12980 loc_result = NULL;
12981 while (nregs--)
12983 dw_loc_descr_ref t;
12985 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
12986 VAR_INIT_STATUS_INITIALIZED);
12987 add_loc_descr (&loc_result, t);
12988 add_loc_descr_op_piece (&loc_result, size);
12989 ++reg;
12991 return loc_result;
12994 /* Now onto stupid register sets in non contiguous locations. */
12996 gcc_assert (GET_CODE (regs) == PARALLEL);
12998 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
12999 loc_result = NULL;
13001 for (i = 0; i < XVECLEN (regs, 0); ++i)
13003 dw_loc_descr_ref t;
13005 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13006 VAR_INIT_STATUS_INITIALIZED);
13007 add_loc_descr (&loc_result, t);
13008 add_loc_descr_op_piece (&loc_result, size);
13011 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13012 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13013 return loc_result;
13016 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13018 /* Return a location descriptor that designates a constant i,
13019 as a compound operation from constant (i >> shift), constant shift
13020 and DW_OP_shl. */
13022 static dw_loc_descr_ref
13023 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13025 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13026 add_loc_descr (&ret, int_loc_descriptor (shift));
13027 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13028 return ret;
13031 /* Return a location descriptor that designates a constant. */
13033 static dw_loc_descr_ref
13034 int_loc_descriptor (HOST_WIDE_INT i)
13036 enum dwarf_location_atom op;
13038 /* Pick the smallest representation of a constant, rather than just
13039 defaulting to the LEB encoding. */
13040 if (i >= 0)
13042 int clz = clz_hwi (i);
13043 int ctz = ctz_hwi (i);
13044 if (i <= 31)
13045 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13046 else if (i <= 0xff)
13047 op = DW_OP_const1u;
13048 else if (i <= 0xffff)
13049 op = DW_OP_const2u;
13050 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13051 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13052 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13053 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13054 while DW_OP_const4u is 5 bytes. */
13055 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13056 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13057 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13058 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13059 while DW_OP_const4u is 5 bytes. */
13060 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13062 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13063 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13064 <= 4)
13066 /* As i >= 2**31, the double cast above will yield a negative number.
13067 Since wrapping is defined in DWARF expressions we can output big
13068 positive integers as small negative ones, regardless of the size
13069 of host wide ints.
13071 Here, since the evaluator will handle 32-bit values and since i >=
13072 2**31, we know it's going to be interpreted as a negative literal:
13073 store it this way if we can do better than 5 bytes this way. */
13074 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13076 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13077 op = DW_OP_const4u;
13079 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13080 least 6 bytes: see if we can do better before falling back to it. */
13081 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13082 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13083 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13084 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13085 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13086 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13087 >= HOST_BITS_PER_WIDE_INT)
13088 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13089 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13090 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13091 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13092 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13093 && size_of_uleb128 (i) > 6)
13094 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13095 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13096 else
13097 op = DW_OP_constu;
13099 else
13101 if (i >= -0x80)
13102 op = DW_OP_const1s;
13103 else if (i >= -0x8000)
13104 op = DW_OP_const2s;
13105 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13107 if (size_of_int_loc_descriptor (i) < 5)
13109 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13110 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13111 return ret;
13113 op = DW_OP_const4s;
13115 else
13117 if (size_of_int_loc_descriptor (i)
13118 < (unsigned long) 1 + size_of_sleb128 (i))
13120 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13121 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13122 return ret;
13124 op = DW_OP_consts;
13128 return new_loc_descr (op, i, 0);
13131 /* Likewise, for unsigned constants. */
13133 static dw_loc_descr_ref
13134 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13136 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13137 const unsigned HOST_WIDE_INT max_uint
13138 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13140 /* If possible, use the clever signed constants handling. */
13141 if (i <= max_int)
13142 return int_loc_descriptor ((HOST_WIDE_INT) i);
13144 /* Here, we are left with positive numbers that cannot be represented as
13145 HOST_WIDE_INT, i.e.:
13146 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13148 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13149 whereas may be better to output a negative integer: thanks to integer
13150 wrapping, we know that:
13151 x = x - 2 ** DWARF2_ADDR_SIZE
13152 = x - 2 * (max (HOST_WIDE_INT) + 1)
13153 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13154 small negative integers. Let's try that in cases it will clearly improve
13155 the encoding: there is no gain turning DW_OP_const4u into
13156 DW_OP_const4s. */
13157 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13158 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13159 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13161 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13163 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13164 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13165 const HOST_WIDE_INT second_shift
13166 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13168 /* So we finally have:
13169 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13170 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13171 return int_loc_descriptor (second_shift);
13174 /* Last chance: fallback to a simple constant operation. */
13175 return new_loc_descr
13176 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13177 ? DW_OP_const4u
13178 : DW_OP_const8u,
13179 i, 0);
13182 /* Generate and return a location description that computes the unsigned
13183 comparison of the two stack top entries (a OP b where b is the top-most
13184 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13185 LE_EXPR, GT_EXPR or GE_EXPR. */
13187 static dw_loc_descr_ref
13188 uint_comparison_loc_list (enum tree_code kind)
13190 enum dwarf_location_atom op, flip_op;
13191 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13193 switch (kind)
13195 case LT_EXPR:
13196 op = DW_OP_lt;
13197 break;
13198 case LE_EXPR:
13199 op = DW_OP_le;
13200 break;
13201 case GT_EXPR:
13202 op = DW_OP_gt;
13203 break;
13204 case GE_EXPR:
13205 op = DW_OP_ge;
13206 break;
13207 default:
13208 gcc_unreachable ();
13211 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13212 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13214 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13215 possible to perform unsigned comparisons: we just have to distinguish
13216 three cases:
13218 1. when a and b have the same sign (as signed integers); then we should
13219 return: a OP(signed) b;
13221 2. when a is a negative signed integer while b is a positive one, then a
13222 is a greater unsigned integer than b; likewise when a and b's roles
13223 are flipped.
13225 So first, compare the sign of the two operands. */
13226 ret = new_loc_descr (DW_OP_over, 0, 0);
13227 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13228 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13229 /* If they have different signs (i.e. they have different sign bits), then
13230 the stack top value has now the sign bit set and thus it's smaller than
13231 zero. */
13232 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13233 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13234 add_loc_descr (&ret, bra_node);
13236 /* We are in case 1. At this point, we know both operands have the same
13237 sign, to it's safe to use the built-in signed comparison. */
13238 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13239 add_loc_descr (&ret, jmp_node);
13241 /* We are in case 2. Here, we know both operands do not have the same sign,
13242 so we have to flip the signed comparison. */
13243 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13244 tmp = new_loc_descr (flip_op, 0, 0);
13245 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13246 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13247 add_loc_descr (&ret, tmp);
13249 /* This dummy operation is necessary to make the two branches join. */
13250 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13251 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13252 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13253 add_loc_descr (&ret, tmp);
13255 return ret;
13258 /* Likewise, but takes the location description lists (might be destructive on
13259 them). Return NULL if either is NULL or if concatenation fails. */
13261 static dw_loc_list_ref
13262 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13263 enum tree_code kind)
13265 if (left == NULL || right == NULL)
13266 return NULL;
13268 add_loc_list (&left, right);
13269 if (left == NULL)
13270 return NULL;
13272 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13273 return left;
13276 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13277 without actually allocating it. */
13279 static unsigned long
13280 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13282 return size_of_int_loc_descriptor (i >> shift)
13283 + size_of_int_loc_descriptor (shift)
13284 + 1;
13287 /* Return size_of_locs (int_loc_descriptor (i)) without
13288 actually allocating it. */
13290 static unsigned long
13291 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13293 unsigned long s;
13295 if (i >= 0)
13297 int clz, ctz;
13298 if (i <= 31)
13299 return 1;
13300 else if (i <= 0xff)
13301 return 2;
13302 else if (i <= 0xffff)
13303 return 3;
13304 clz = clz_hwi (i);
13305 ctz = ctz_hwi (i);
13306 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13307 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13308 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13309 - clz - 5);
13310 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13311 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13312 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13313 - clz - 8);
13314 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13315 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13316 <= 4)
13317 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13318 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13319 return 5;
13320 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13321 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13322 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13323 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13324 - clz - 8);
13325 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13326 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13327 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13328 - clz - 16);
13329 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13330 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13331 && s > 6)
13332 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13333 - clz - 32);
13334 else
13335 return 1 + s;
13337 else
13339 if (i >= -0x80)
13340 return 2;
13341 else if (i >= -0x8000)
13342 return 3;
13343 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13345 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13347 s = size_of_int_loc_descriptor (-i) + 1;
13348 if (s < 5)
13349 return s;
13351 return 5;
13353 else
13355 unsigned long r = 1 + size_of_sleb128 (i);
13356 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13358 s = size_of_int_loc_descriptor (-i) + 1;
13359 if (s < r)
13360 return s;
13362 return r;
13367 /* Return loc description representing "address" of integer value.
13368 This can appear only as toplevel expression. */
13370 static dw_loc_descr_ref
13371 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13373 int litsize;
13374 dw_loc_descr_ref loc_result = NULL;
13376 if (!(dwarf_version >= 4 || !dwarf_strict))
13377 return NULL;
13379 litsize = size_of_int_loc_descriptor (i);
13380 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13381 is more compact. For DW_OP_stack_value we need:
13382 litsize + 1 (DW_OP_stack_value)
13383 and for DW_OP_implicit_value:
13384 1 (DW_OP_implicit_value) + 1 (length) + size. */
13385 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13387 loc_result = int_loc_descriptor (i);
13388 add_loc_descr (&loc_result,
13389 new_loc_descr (DW_OP_stack_value, 0, 0));
13390 return loc_result;
13393 loc_result = new_loc_descr (DW_OP_implicit_value,
13394 size, 0);
13395 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13396 loc_result->dw_loc_oprnd2.v.val_int = i;
13397 return loc_result;
13400 /* Return a location descriptor that designates a base+offset location. */
13402 static dw_loc_descr_ref
13403 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13404 enum var_init_status initialized)
13406 unsigned int regno;
13407 dw_loc_descr_ref result;
13408 dw_fde_ref fde = cfun->fde;
13410 /* We only use "frame base" when we're sure we're talking about the
13411 post-prologue local stack frame. We do this by *not* running
13412 register elimination until this point, and recognizing the special
13413 argument pointer and soft frame pointer rtx's. */
13414 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13416 rtx elim = (ira_use_lra_p
13417 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13418 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13420 if (elim != reg)
13422 if (GET_CODE (elim) == PLUS)
13424 offset += INTVAL (XEXP (elim, 1));
13425 elim = XEXP (elim, 0);
13427 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13428 && (elim == hard_frame_pointer_rtx
13429 || elim == stack_pointer_rtx))
13430 || elim == (frame_pointer_needed
13431 ? hard_frame_pointer_rtx
13432 : stack_pointer_rtx));
13434 /* If drap register is used to align stack, use frame
13435 pointer + offset to access stack variables. If stack
13436 is aligned without drap, use stack pointer + offset to
13437 access stack variables. */
13438 if (crtl->stack_realign_tried
13439 && reg == frame_pointer_rtx)
13441 int base_reg
13442 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13443 ? HARD_FRAME_POINTER_REGNUM
13444 : REGNO (elim));
13445 return new_reg_loc_descr (base_reg, offset);
13448 gcc_assert (frame_pointer_fb_offset_valid);
13449 offset += frame_pointer_fb_offset;
13450 return new_loc_descr (DW_OP_fbreg, offset, 0);
13454 regno = REGNO (reg);
13455 #ifdef LEAF_REG_REMAP
13456 if (crtl->uses_only_leaf_regs)
13458 int leaf_reg = LEAF_REG_REMAP (regno);
13459 if (leaf_reg != -1)
13460 regno = (unsigned) leaf_reg;
13462 #endif
13463 regno = DWARF_FRAME_REGNUM (regno);
13465 if (!optimize && fde
13466 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13468 /* Use cfa+offset to represent the location of arguments passed
13469 on the stack when drap is used to align stack.
13470 Only do this when not optimizing, for optimized code var-tracking
13471 is supposed to track where the arguments live and the register
13472 used as vdrap or drap in some spot might be used for something
13473 else in other part of the routine. */
13474 return new_loc_descr (DW_OP_fbreg, offset, 0);
13477 if (regno <= 31)
13478 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13479 offset, 0);
13480 else
13481 result = new_loc_descr (DW_OP_bregx, regno, offset);
13483 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13484 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13486 return result;
13489 /* Return true if this RTL expression describes a base+offset calculation. */
13491 static inline int
13492 is_based_loc (const_rtx rtl)
13494 return (GET_CODE (rtl) == PLUS
13495 && ((REG_P (XEXP (rtl, 0))
13496 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13497 && CONST_INT_P (XEXP (rtl, 1)))));
13500 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13501 failed. */
13503 static dw_loc_descr_ref
13504 tls_mem_loc_descriptor (rtx mem)
13506 tree base;
13507 dw_loc_descr_ref loc_result;
13509 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13510 return NULL;
13512 base = get_base_address (MEM_EXPR (mem));
13513 if (base == NULL
13514 || !VAR_P (base)
13515 || !DECL_THREAD_LOCAL_P (base))
13516 return NULL;
13518 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13519 if (loc_result == NULL)
13520 return NULL;
13522 if (MEM_OFFSET (mem))
13523 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13525 return loc_result;
13528 /* Output debug info about reason why we failed to expand expression as dwarf
13529 expression. */
13531 static void
13532 expansion_failed (tree expr, rtx rtl, char const *reason)
13534 if (dump_file && (dump_flags & TDF_DETAILS))
13536 fprintf (dump_file, "Failed to expand as dwarf: ");
13537 if (expr)
13538 print_generic_expr (dump_file, expr, dump_flags);
13539 if (rtl)
13541 fprintf (dump_file, "\n");
13542 print_rtl (dump_file, rtl);
13544 fprintf (dump_file, "\nReason: %s\n", reason);
13548 /* Helper function for const_ok_for_output. */
13550 static bool
13551 const_ok_for_output_1 (rtx rtl)
13553 if (GET_CODE (rtl) == UNSPEC)
13555 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13556 we can't express it in the debug info. */
13557 /* Don't complain about TLS UNSPECs, those are just too hard to
13558 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13559 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13560 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13561 if (flag_checking
13562 && (XVECLEN (rtl, 0) == 0
13563 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13564 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13565 inform (current_function_decl
13566 ? DECL_SOURCE_LOCATION (current_function_decl)
13567 : UNKNOWN_LOCATION,
13568 #if NUM_UNSPEC_VALUES > 0
13569 "non-delegitimized UNSPEC %s (%d) found in variable location",
13570 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13571 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13572 XINT (rtl, 1));
13573 #else
13574 "non-delegitimized UNSPEC %d found in variable location",
13575 XINT (rtl, 1));
13576 #endif
13577 expansion_failed (NULL_TREE, rtl,
13578 "UNSPEC hasn't been delegitimized.\n");
13579 return false;
13582 if (targetm.const_not_ok_for_debug_p (rtl))
13584 expansion_failed (NULL_TREE, rtl,
13585 "Expression rejected for debug by the backend.\n");
13586 return false;
13589 /* FIXME: Refer to PR60655. It is possible for simplification
13590 of rtl expressions in var tracking to produce such expressions.
13591 We should really identify / validate expressions
13592 enclosed in CONST that can be handled by assemblers on various
13593 targets and only handle legitimate cases here. */
13594 if (GET_CODE (rtl) != SYMBOL_REF)
13596 if (GET_CODE (rtl) == NOT)
13597 return false;
13598 return true;
13601 if (CONSTANT_POOL_ADDRESS_P (rtl))
13603 bool marked;
13604 get_pool_constant_mark (rtl, &marked);
13605 /* If all references to this pool constant were optimized away,
13606 it was not output and thus we can't represent it. */
13607 if (!marked)
13609 expansion_failed (NULL_TREE, rtl,
13610 "Constant was removed from constant pool.\n");
13611 return false;
13615 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13616 return false;
13618 /* Avoid references to external symbols in debug info, on several targets
13619 the linker might even refuse to link when linking a shared library,
13620 and in many other cases the relocations for .debug_info/.debug_loc are
13621 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13622 to be defined within the same shared library or executable are fine. */
13623 if (SYMBOL_REF_EXTERNAL_P (rtl))
13625 tree decl = SYMBOL_REF_DECL (rtl);
13627 if (decl == NULL || !targetm.binds_local_p (decl))
13629 expansion_failed (NULL_TREE, rtl,
13630 "Symbol not defined in current TU.\n");
13631 return false;
13635 return true;
13638 /* Return true if constant RTL can be emitted in DW_OP_addr or
13639 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13640 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13642 static bool
13643 const_ok_for_output (rtx rtl)
13645 if (GET_CODE (rtl) == SYMBOL_REF)
13646 return const_ok_for_output_1 (rtl);
13648 if (GET_CODE (rtl) == CONST)
13650 subrtx_var_iterator::array_type array;
13651 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13652 if (!const_ok_for_output_1 (*iter))
13653 return false;
13654 return true;
13657 return true;
13660 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13661 if possible, NULL otherwise. */
13663 static dw_die_ref
13664 base_type_for_mode (machine_mode mode, bool unsignedp)
13666 dw_die_ref type_die;
13667 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13669 if (type == NULL)
13670 return NULL;
13671 switch (TREE_CODE (type))
13673 case INTEGER_TYPE:
13674 case REAL_TYPE:
13675 break;
13676 default:
13677 return NULL;
13679 type_die = lookup_type_die (type);
13680 if (!type_die)
13681 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13682 comp_unit_die ());
13683 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13684 return NULL;
13685 return type_die;
13688 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13689 type matching MODE, or, if MODE is narrower than or as wide as
13690 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13691 possible. */
13693 static dw_loc_descr_ref
13694 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13696 machine_mode outer_mode = mode;
13697 dw_die_ref type_die;
13698 dw_loc_descr_ref cvt;
13700 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13702 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13703 return op;
13705 type_die = base_type_for_mode (outer_mode, 1);
13706 if (type_die == NULL)
13707 return NULL;
13708 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13709 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13710 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13711 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13712 add_loc_descr (&op, cvt);
13713 return op;
13716 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13718 static dw_loc_descr_ref
13719 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13720 dw_loc_descr_ref op1)
13722 dw_loc_descr_ref ret = op0;
13723 add_loc_descr (&ret, op1);
13724 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13725 if (STORE_FLAG_VALUE != 1)
13727 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13728 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13730 return ret;
13733 /* Return location descriptor for signed comparison OP RTL. */
13735 static dw_loc_descr_ref
13736 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13737 machine_mode mem_mode)
13739 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13740 dw_loc_descr_ref op0, op1;
13741 int shift;
13743 if (op_mode == VOIDmode)
13744 op_mode = GET_MODE (XEXP (rtl, 1));
13745 if (op_mode == VOIDmode)
13746 return NULL;
13748 if (dwarf_strict
13749 && dwarf_version < 5
13750 && (!SCALAR_INT_MODE_P (op_mode)
13751 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13752 return NULL;
13754 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13755 VAR_INIT_STATUS_INITIALIZED);
13756 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13757 VAR_INIT_STATUS_INITIALIZED);
13759 if (op0 == NULL || op1 == NULL)
13760 return NULL;
13762 if (!SCALAR_INT_MODE_P (op_mode)
13763 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13764 return compare_loc_descriptor (op, op0, op1);
13766 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13768 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13769 dw_loc_descr_ref cvt;
13771 if (type_die == NULL)
13772 return NULL;
13773 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13774 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13775 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13776 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13777 add_loc_descr (&op0, cvt);
13778 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13779 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13780 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13781 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13782 add_loc_descr (&op1, cvt);
13783 return compare_loc_descriptor (op, op0, op1);
13786 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13787 /* For eq/ne, if the operands are known to be zero-extended,
13788 there is no need to do the fancy shifting up. */
13789 if (op == DW_OP_eq || op == DW_OP_ne)
13791 dw_loc_descr_ref last0, last1;
13792 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13794 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13796 /* deref_size zero extends, and for constants we can check
13797 whether they are zero extended or not. */
13798 if (((last0->dw_loc_opc == DW_OP_deref_size
13799 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13800 || (CONST_INT_P (XEXP (rtl, 0))
13801 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13802 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13803 && ((last1->dw_loc_opc == DW_OP_deref_size
13804 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13805 || (CONST_INT_P (XEXP (rtl, 1))
13806 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13807 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13808 return compare_loc_descriptor (op, op0, op1);
13810 /* EQ/NE comparison against constant in narrower type than
13811 DWARF2_ADDR_SIZE can be performed either as
13812 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13813 DW_OP_{eq,ne}
13815 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13816 DW_OP_{eq,ne}. Pick whatever is shorter. */
13817 if (CONST_INT_P (XEXP (rtl, 1))
13818 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13819 && (size_of_int_loc_descriptor (shift) + 1
13820 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
13821 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13822 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13823 & GET_MODE_MASK (op_mode))))
13825 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13826 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13827 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13828 & GET_MODE_MASK (op_mode));
13829 return compare_loc_descriptor (op, op0, op1);
13832 add_loc_descr (&op0, int_loc_descriptor (shift));
13833 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13834 if (CONST_INT_P (XEXP (rtl, 1)))
13835 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13836 else
13838 add_loc_descr (&op1, int_loc_descriptor (shift));
13839 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13841 return compare_loc_descriptor (op, op0, op1);
13844 /* Return location descriptor for unsigned comparison OP RTL. */
13846 static dw_loc_descr_ref
13847 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13848 machine_mode mem_mode)
13850 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13851 dw_loc_descr_ref op0, op1;
13853 if (op_mode == VOIDmode)
13854 op_mode = GET_MODE (XEXP (rtl, 1));
13855 if (op_mode == VOIDmode)
13856 return NULL;
13857 if (!SCALAR_INT_MODE_P (op_mode))
13858 return NULL;
13860 if (dwarf_strict
13861 && dwarf_version < 5
13862 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13863 return NULL;
13865 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13866 VAR_INIT_STATUS_INITIALIZED);
13867 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13868 VAR_INIT_STATUS_INITIALIZED);
13870 if (op0 == NULL || op1 == NULL)
13871 return NULL;
13873 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13875 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13876 dw_loc_descr_ref last0, last1;
13877 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13879 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13881 if (CONST_INT_P (XEXP (rtl, 0)))
13882 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13883 /* deref_size zero extends, so no need to mask it again. */
13884 else if (last0->dw_loc_opc != DW_OP_deref_size
13885 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13887 add_loc_descr (&op0, int_loc_descriptor (mask));
13888 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13890 if (CONST_INT_P (XEXP (rtl, 1)))
13891 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13892 /* deref_size zero extends, so no need to mask it again. */
13893 else if (last1->dw_loc_opc != DW_OP_deref_size
13894 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13896 add_loc_descr (&op1, int_loc_descriptor (mask));
13897 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13900 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13902 HOST_WIDE_INT bias = 1;
13903 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13904 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13905 if (CONST_INT_P (XEXP (rtl, 1)))
13906 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13907 + INTVAL (XEXP (rtl, 1)));
13908 else
13909 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13910 bias, 0));
13912 return compare_loc_descriptor (op, op0, op1);
13915 /* Return location descriptor for {U,S}{MIN,MAX}. */
13917 static dw_loc_descr_ref
13918 minmax_loc_descriptor (rtx rtl, machine_mode mode,
13919 machine_mode mem_mode)
13921 enum dwarf_location_atom op;
13922 dw_loc_descr_ref op0, op1, ret;
13923 dw_loc_descr_ref bra_node, drop_node;
13925 if (dwarf_strict
13926 && dwarf_version < 5
13927 && (!SCALAR_INT_MODE_P (mode)
13928 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
13929 return NULL;
13931 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13932 VAR_INIT_STATUS_INITIALIZED);
13933 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13934 VAR_INIT_STATUS_INITIALIZED);
13936 if (op0 == NULL || op1 == NULL)
13937 return NULL;
13939 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13940 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13941 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13942 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13944 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13946 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
13947 add_loc_descr (&op0, int_loc_descriptor (mask));
13948 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13949 add_loc_descr (&op1, int_loc_descriptor (mask));
13950 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13952 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13954 HOST_WIDE_INT bias = 1;
13955 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13956 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13957 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13960 else if (!SCALAR_INT_MODE_P (mode)
13961 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13963 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
13964 add_loc_descr (&op0, int_loc_descriptor (shift));
13965 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13966 add_loc_descr (&op1, int_loc_descriptor (shift));
13967 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13969 else if (SCALAR_INT_MODE_P (mode)
13970 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13972 dw_die_ref type_die = base_type_for_mode (mode, 0);
13973 dw_loc_descr_ref cvt;
13974 if (type_die == NULL)
13975 return NULL;
13976 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13977 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13978 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13979 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13980 add_loc_descr (&op0, cvt);
13981 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13982 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13983 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13984 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13985 add_loc_descr (&op1, cvt);
13988 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
13989 op = DW_OP_lt;
13990 else
13991 op = DW_OP_gt;
13992 ret = op0;
13993 add_loc_descr (&ret, op1);
13994 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13995 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13996 add_loc_descr (&ret, bra_node);
13997 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13998 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13999 add_loc_descr (&ret, drop_node);
14000 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14001 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14002 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14003 && SCALAR_INT_MODE_P (mode)
14004 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14005 ret = convert_descriptor_to_mode (mode, ret);
14006 return ret;
14009 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14010 but after converting arguments to type_die, afterwards
14011 convert back to unsigned. */
14013 static dw_loc_descr_ref
14014 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14015 machine_mode mode, machine_mode mem_mode)
14017 dw_loc_descr_ref cvt, op0, op1;
14019 if (type_die == NULL)
14020 return NULL;
14021 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14022 VAR_INIT_STATUS_INITIALIZED);
14023 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14024 VAR_INIT_STATUS_INITIALIZED);
14025 if (op0 == NULL || op1 == NULL)
14026 return NULL;
14027 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14028 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14029 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14030 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14031 add_loc_descr (&op0, cvt);
14032 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14033 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14034 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14035 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14036 add_loc_descr (&op1, cvt);
14037 add_loc_descr (&op0, op1);
14038 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14039 return convert_descriptor_to_mode (mode, op0);
14042 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14043 const0 is DW_OP_lit0 or corresponding typed constant,
14044 const1 is DW_OP_lit1 or corresponding typed constant
14045 and constMSB is constant with just the MSB bit set
14046 for the mode):
14047 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14048 L1: const0 DW_OP_swap
14049 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14050 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14051 L3: DW_OP_drop
14052 L4: DW_OP_nop
14054 CTZ is similar:
14055 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14056 L1: const0 DW_OP_swap
14057 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14058 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14059 L3: DW_OP_drop
14060 L4: DW_OP_nop
14062 FFS is similar:
14063 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14064 L1: const1 DW_OP_swap
14065 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14066 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14067 L3: DW_OP_drop
14068 L4: DW_OP_nop */
14070 static dw_loc_descr_ref
14071 clz_loc_descriptor (rtx rtl, machine_mode mode,
14072 machine_mode mem_mode)
14074 dw_loc_descr_ref op0, ret, tmp;
14075 HOST_WIDE_INT valv;
14076 dw_loc_descr_ref l1jump, l1label;
14077 dw_loc_descr_ref l2jump, l2label;
14078 dw_loc_descr_ref l3jump, l3label;
14079 dw_loc_descr_ref l4jump, l4label;
14080 rtx msb;
14082 if (!SCALAR_INT_MODE_P (mode)
14083 || GET_MODE (XEXP (rtl, 0)) != mode)
14084 return NULL;
14086 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14087 VAR_INIT_STATUS_INITIALIZED);
14088 if (op0 == NULL)
14089 return NULL;
14090 ret = op0;
14091 if (GET_CODE (rtl) == CLZ)
14093 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14094 valv = GET_MODE_BITSIZE (mode);
14096 else if (GET_CODE (rtl) == FFS)
14097 valv = 0;
14098 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14099 valv = GET_MODE_BITSIZE (mode);
14100 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14101 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14102 add_loc_descr (&ret, l1jump);
14103 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14104 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14105 VAR_INIT_STATUS_INITIALIZED);
14106 if (tmp == NULL)
14107 return NULL;
14108 add_loc_descr (&ret, tmp);
14109 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14110 add_loc_descr (&ret, l4jump);
14111 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14112 ? const1_rtx : const0_rtx,
14113 mode, mem_mode,
14114 VAR_INIT_STATUS_INITIALIZED);
14115 if (l1label == NULL)
14116 return NULL;
14117 add_loc_descr (&ret, l1label);
14118 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14119 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14120 add_loc_descr (&ret, l2label);
14121 if (GET_CODE (rtl) != CLZ)
14122 msb = const1_rtx;
14123 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14124 msb = GEN_INT (HOST_WIDE_INT_1U
14125 << (GET_MODE_BITSIZE (mode) - 1));
14126 else
14127 msb = immed_wide_int_const
14128 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14129 GET_MODE_PRECISION (mode)), mode);
14130 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14131 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14132 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14133 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14134 else
14135 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14136 VAR_INIT_STATUS_INITIALIZED);
14137 if (tmp == NULL)
14138 return NULL;
14139 add_loc_descr (&ret, tmp);
14140 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14141 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14142 add_loc_descr (&ret, l3jump);
14143 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14144 VAR_INIT_STATUS_INITIALIZED);
14145 if (tmp == NULL)
14146 return NULL;
14147 add_loc_descr (&ret, tmp);
14148 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14149 ? DW_OP_shl : DW_OP_shr, 0, 0));
14150 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14151 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14152 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14153 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14154 add_loc_descr (&ret, l2jump);
14155 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14156 add_loc_descr (&ret, l3label);
14157 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14158 add_loc_descr (&ret, l4label);
14159 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14160 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14161 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14162 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14163 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14164 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14165 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14166 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14167 return ret;
14170 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14171 const1 is DW_OP_lit1 or corresponding typed constant):
14172 const0 DW_OP_swap
14173 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14174 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14175 L2: DW_OP_drop
14177 PARITY is similar:
14178 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14179 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14180 L2: DW_OP_drop */
14182 static dw_loc_descr_ref
14183 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14184 machine_mode mem_mode)
14186 dw_loc_descr_ref op0, ret, tmp;
14187 dw_loc_descr_ref l1jump, l1label;
14188 dw_loc_descr_ref l2jump, l2label;
14190 if (!SCALAR_INT_MODE_P (mode)
14191 || GET_MODE (XEXP (rtl, 0)) != mode)
14192 return NULL;
14194 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14195 VAR_INIT_STATUS_INITIALIZED);
14196 if (op0 == NULL)
14197 return NULL;
14198 ret = op0;
14199 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14200 VAR_INIT_STATUS_INITIALIZED);
14201 if (tmp == NULL)
14202 return NULL;
14203 add_loc_descr (&ret, tmp);
14204 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14205 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14206 add_loc_descr (&ret, l1label);
14207 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14208 add_loc_descr (&ret, l2jump);
14209 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14210 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14211 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14212 VAR_INIT_STATUS_INITIALIZED);
14213 if (tmp == NULL)
14214 return NULL;
14215 add_loc_descr (&ret, tmp);
14216 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14217 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14218 ? DW_OP_plus : DW_OP_xor, 0, 0));
14219 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14220 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14221 VAR_INIT_STATUS_INITIALIZED);
14222 add_loc_descr (&ret, tmp);
14223 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14224 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14225 add_loc_descr (&ret, l1jump);
14226 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14227 add_loc_descr (&ret, l2label);
14228 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14229 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14230 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14231 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14232 return ret;
14235 /* BSWAP (constS is initial shift count, either 56 or 24):
14236 constS const0
14237 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14238 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14239 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14240 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14241 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14243 static dw_loc_descr_ref
14244 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14245 machine_mode mem_mode)
14247 dw_loc_descr_ref op0, ret, tmp;
14248 dw_loc_descr_ref l1jump, l1label;
14249 dw_loc_descr_ref l2jump, l2label;
14251 if (!SCALAR_INT_MODE_P (mode)
14252 || BITS_PER_UNIT != 8
14253 || (GET_MODE_BITSIZE (mode) != 32
14254 && GET_MODE_BITSIZE (mode) != 64))
14255 return NULL;
14257 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14258 VAR_INIT_STATUS_INITIALIZED);
14259 if (op0 == NULL)
14260 return NULL;
14262 ret = op0;
14263 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14264 mode, mem_mode,
14265 VAR_INIT_STATUS_INITIALIZED);
14266 if (tmp == NULL)
14267 return NULL;
14268 add_loc_descr (&ret, tmp);
14269 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14270 VAR_INIT_STATUS_INITIALIZED);
14271 if (tmp == NULL)
14272 return NULL;
14273 add_loc_descr (&ret, tmp);
14274 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14275 add_loc_descr (&ret, l1label);
14276 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14277 mode, mem_mode,
14278 VAR_INIT_STATUS_INITIALIZED);
14279 add_loc_descr (&ret, tmp);
14280 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14281 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14282 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14283 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14284 VAR_INIT_STATUS_INITIALIZED);
14285 if (tmp == NULL)
14286 return NULL;
14287 add_loc_descr (&ret, tmp);
14288 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14289 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14290 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14291 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14292 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14293 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14294 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14295 VAR_INIT_STATUS_INITIALIZED);
14296 add_loc_descr (&ret, tmp);
14297 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14298 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14299 add_loc_descr (&ret, l2jump);
14300 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14301 VAR_INIT_STATUS_INITIALIZED);
14302 add_loc_descr (&ret, tmp);
14303 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14304 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14305 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14306 add_loc_descr (&ret, l1jump);
14307 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14308 add_loc_descr (&ret, l2label);
14309 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14310 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14311 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14312 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14313 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14314 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14315 return ret;
14318 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14319 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14320 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14321 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14323 ROTATERT is similar:
14324 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14325 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14326 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14328 static dw_loc_descr_ref
14329 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14330 machine_mode mem_mode)
14332 rtx rtlop1 = XEXP (rtl, 1);
14333 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14334 int i;
14336 if (!SCALAR_INT_MODE_P (mode))
14337 return NULL;
14339 if (GET_MODE (rtlop1) != VOIDmode
14340 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14341 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14342 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14343 VAR_INIT_STATUS_INITIALIZED);
14344 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14345 VAR_INIT_STATUS_INITIALIZED);
14346 if (op0 == NULL || op1 == NULL)
14347 return NULL;
14348 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14349 for (i = 0; i < 2; i++)
14351 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14352 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14353 mode, mem_mode,
14354 VAR_INIT_STATUS_INITIALIZED);
14355 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14356 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14357 ? DW_OP_const4u
14358 : HOST_BITS_PER_WIDE_INT == 64
14359 ? DW_OP_const8u : DW_OP_constu,
14360 GET_MODE_MASK (mode), 0);
14361 else
14362 mask[i] = NULL;
14363 if (mask[i] == NULL)
14364 return NULL;
14365 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14367 ret = op0;
14368 add_loc_descr (&ret, op1);
14369 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14370 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14371 if (GET_CODE (rtl) == ROTATERT)
14373 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14374 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14375 GET_MODE_BITSIZE (mode), 0));
14377 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14378 if (mask[0] != NULL)
14379 add_loc_descr (&ret, mask[0]);
14380 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14381 if (mask[1] != NULL)
14383 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14384 add_loc_descr (&ret, mask[1]);
14385 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14387 if (GET_CODE (rtl) == ROTATE)
14389 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14390 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14391 GET_MODE_BITSIZE (mode), 0));
14393 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14394 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14395 return ret;
14398 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14399 for DEBUG_PARAMETER_REF RTL. */
14401 static dw_loc_descr_ref
14402 parameter_ref_descriptor (rtx rtl)
14404 dw_loc_descr_ref ret;
14405 dw_die_ref ref;
14407 if (dwarf_strict)
14408 return NULL;
14409 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14410 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14411 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14412 if (ref)
14414 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14415 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14416 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14418 else
14420 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14421 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14423 return ret;
14426 /* The following routine converts the RTL for a variable or parameter
14427 (resident in memory) into an equivalent Dwarf representation of a
14428 mechanism for getting the address of that same variable onto the top of a
14429 hypothetical "address evaluation" stack.
14431 When creating memory location descriptors, we are effectively transforming
14432 the RTL for a memory-resident object into its Dwarf postfix expression
14433 equivalent. This routine recursively descends an RTL tree, turning
14434 it into Dwarf postfix code as it goes.
14436 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14438 MEM_MODE is the mode of the memory reference, needed to handle some
14439 autoincrement addressing modes.
14441 Return 0 if we can't represent the location. */
14443 dw_loc_descr_ref
14444 mem_loc_descriptor (rtx rtl, machine_mode mode,
14445 machine_mode mem_mode,
14446 enum var_init_status initialized)
14448 dw_loc_descr_ref mem_loc_result = NULL;
14449 enum dwarf_location_atom op;
14450 dw_loc_descr_ref op0, op1;
14451 rtx inner = NULL_RTX;
14453 if (mode == VOIDmode)
14454 mode = GET_MODE (rtl);
14456 /* Note that for a dynamically sized array, the location we will generate a
14457 description of here will be the lowest numbered location which is
14458 actually within the array. That's *not* necessarily the same as the
14459 zeroth element of the array. */
14461 rtl = targetm.delegitimize_address (rtl);
14463 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14464 return NULL;
14466 switch (GET_CODE (rtl))
14468 case POST_INC:
14469 case POST_DEC:
14470 case POST_MODIFY:
14471 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14473 case SUBREG:
14474 /* The case of a subreg may arise when we have a local (register)
14475 variable or a formal (register) parameter which doesn't quite fill
14476 up an entire register. For now, just assume that it is
14477 legitimate to make the Dwarf info refer to the whole register which
14478 contains the given subreg. */
14479 if (!subreg_lowpart_p (rtl))
14480 break;
14481 inner = SUBREG_REG (rtl);
14482 /* FALLTHRU */
14483 case TRUNCATE:
14484 if (inner == NULL_RTX)
14485 inner = XEXP (rtl, 0);
14486 if (SCALAR_INT_MODE_P (mode)
14487 && SCALAR_INT_MODE_P (GET_MODE (inner))
14488 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14489 #ifdef POINTERS_EXTEND_UNSIGNED
14490 || (mode == Pmode && mem_mode != VOIDmode)
14491 #endif
14493 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14495 mem_loc_result = mem_loc_descriptor (inner,
14496 GET_MODE (inner),
14497 mem_mode, initialized);
14498 break;
14500 if (dwarf_strict && dwarf_version < 5)
14501 break;
14502 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14503 break;
14504 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14505 && (!SCALAR_INT_MODE_P (mode)
14506 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14507 break;
14508 else
14510 dw_die_ref type_die;
14511 dw_loc_descr_ref cvt;
14513 mem_loc_result = mem_loc_descriptor (inner,
14514 GET_MODE (inner),
14515 mem_mode, initialized);
14516 if (mem_loc_result == NULL)
14517 break;
14518 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14519 if (type_die == NULL)
14521 mem_loc_result = NULL;
14522 break;
14524 if (GET_MODE_SIZE (mode)
14525 != GET_MODE_SIZE (GET_MODE (inner)))
14526 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14527 else
14528 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14529 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14530 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14531 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14532 add_loc_descr (&mem_loc_result, cvt);
14533 if (SCALAR_INT_MODE_P (mode)
14534 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14536 /* Convert it to untyped afterwards. */
14537 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14538 add_loc_descr (&mem_loc_result, cvt);
14541 break;
14543 case REG:
14544 if (! SCALAR_INT_MODE_P (mode)
14545 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14546 && rtl != arg_pointer_rtx
14547 && rtl != frame_pointer_rtx
14548 #ifdef POINTERS_EXTEND_UNSIGNED
14549 && (mode != Pmode || mem_mode == VOIDmode)
14550 #endif
14553 dw_die_ref type_die;
14554 unsigned int dbx_regnum;
14556 if (dwarf_strict && dwarf_version < 5)
14557 break;
14558 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14559 break;
14560 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14561 if (type_die == NULL)
14562 break;
14564 dbx_regnum = dbx_reg_number (rtl);
14565 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14566 break;
14567 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14568 dbx_regnum, 0);
14569 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14570 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14571 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14572 break;
14574 /* Whenever a register number forms a part of the description of the
14575 method for calculating the (dynamic) address of a memory resident
14576 object, DWARF rules require the register number be referred to as
14577 a "base register". This distinction is not based in any way upon
14578 what category of register the hardware believes the given register
14579 belongs to. This is strictly DWARF terminology we're dealing with
14580 here. Note that in cases where the location of a memory-resident
14581 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14582 OP_CONST (0)) the actual DWARF location descriptor that we generate
14583 may just be OP_BASEREG (basereg). This may look deceptively like
14584 the object in question was allocated to a register (rather than in
14585 memory) so DWARF consumers need to be aware of the subtle
14586 distinction between OP_REG and OP_BASEREG. */
14587 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14588 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14589 else if (stack_realign_drap
14590 && crtl->drap_reg
14591 && crtl->args.internal_arg_pointer == rtl
14592 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14594 /* If RTL is internal_arg_pointer, which has been optimized
14595 out, use DRAP instead. */
14596 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14597 VAR_INIT_STATUS_INITIALIZED);
14599 break;
14601 case SIGN_EXTEND:
14602 case ZERO_EXTEND:
14603 if (!SCALAR_INT_MODE_P (mode))
14604 break;
14605 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14606 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14607 if (op0 == 0)
14608 break;
14609 else if (GET_CODE (rtl) == ZERO_EXTEND
14610 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14611 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14612 < HOST_BITS_PER_WIDE_INT
14613 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14614 to expand zero extend as two shifts instead of
14615 masking. */
14616 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14618 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14619 mem_loc_result = op0;
14620 add_loc_descr (&mem_loc_result,
14621 int_loc_descriptor (GET_MODE_MASK (imode)));
14622 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14624 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14626 int shift = DWARF2_ADDR_SIZE
14627 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14628 shift *= BITS_PER_UNIT;
14629 if (GET_CODE (rtl) == SIGN_EXTEND)
14630 op = DW_OP_shra;
14631 else
14632 op = DW_OP_shr;
14633 mem_loc_result = op0;
14634 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14635 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14636 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14637 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14639 else if (!dwarf_strict || dwarf_version >= 5)
14641 dw_die_ref type_die1, type_die2;
14642 dw_loc_descr_ref cvt;
14644 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14645 GET_CODE (rtl) == ZERO_EXTEND);
14646 if (type_die1 == NULL)
14647 break;
14648 type_die2 = base_type_for_mode (mode, 1);
14649 if (type_die2 == NULL)
14650 break;
14651 mem_loc_result = op0;
14652 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14653 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14654 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14655 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14656 add_loc_descr (&mem_loc_result, cvt);
14657 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14658 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14659 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14660 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14661 add_loc_descr (&mem_loc_result, cvt);
14663 break;
14665 case MEM:
14667 rtx new_rtl = avoid_constant_pool_reference (rtl);
14668 if (new_rtl != rtl)
14670 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14671 initialized);
14672 if (mem_loc_result != NULL)
14673 return mem_loc_result;
14676 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14677 get_address_mode (rtl), mode,
14678 VAR_INIT_STATUS_INITIALIZED);
14679 if (mem_loc_result == NULL)
14680 mem_loc_result = tls_mem_loc_descriptor (rtl);
14681 if (mem_loc_result != NULL)
14683 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14684 || !SCALAR_INT_MODE_P(mode))
14686 dw_die_ref type_die;
14687 dw_loc_descr_ref deref;
14689 if (dwarf_strict && dwarf_version < 5)
14690 return NULL;
14691 type_die
14692 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14693 if (type_die == NULL)
14694 return NULL;
14695 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14696 GET_MODE_SIZE (mode), 0);
14697 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14698 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14699 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14700 add_loc_descr (&mem_loc_result, deref);
14702 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14703 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14704 else
14705 add_loc_descr (&mem_loc_result,
14706 new_loc_descr (DW_OP_deref_size,
14707 GET_MODE_SIZE (mode), 0));
14709 break;
14711 case LO_SUM:
14712 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14714 case LABEL_REF:
14715 /* Some ports can transform a symbol ref into a label ref, because
14716 the symbol ref is too far away and has to be dumped into a constant
14717 pool. */
14718 case CONST:
14719 case SYMBOL_REF:
14720 if (!SCALAR_INT_MODE_P (mode)
14721 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14722 #ifdef POINTERS_EXTEND_UNSIGNED
14723 && (mode != Pmode || mem_mode == VOIDmode)
14724 #endif
14726 break;
14727 if (GET_CODE (rtl) == SYMBOL_REF
14728 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14730 dw_loc_descr_ref temp;
14732 /* If this is not defined, we have no way to emit the data. */
14733 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14734 break;
14736 temp = new_addr_loc_descr (rtl, dtprel_true);
14738 /* We check for DWARF 5 here because gdb did not implement
14739 DW_OP_form_tls_address until after 7.12. */
14740 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14741 ? DW_OP_form_tls_address
14742 : DW_OP_GNU_push_tls_address),
14743 0, 0);
14744 add_loc_descr (&mem_loc_result, temp);
14746 break;
14749 if (!const_ok_for_output (rtl))
14751 if (GET_CODE (rtl) == CONST)
14752 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14753 initialized);
14754 break;
14757 symref:
14758 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14759 vec_safe_push (used_rtx_array, rtl);
14760 break;
14762 case CONCAT:
14763 case CONCATN:
14764 case VAR_LOCATION:
14765 case DEBUG_IMPLICIT_PTR:
14766 expansion_failed (NULL_TREE, rtl,
14767 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14768 return 0;
14770 case ENTRY_VALUE:
14771 if (dwarf_strict && dwarf_version < 5)
14772 return NULL;
14773 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14775 if (!SCALAR_INT_MODE_P (mode)
14776 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14777 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14778 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14779 else
14781 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14782 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14783 return NULL;
14784 op0 = one_reg_loc_descriptor (dbx_regnum,
14785 VAR_INIT_STATUS_INITIALIZED);
14788 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14789 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14791 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14792 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14793 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14794 return NULL;
14796 else
14797 gcc_unreachable ();
14798 if (op0 == NULL)
14799 return NULL;
14800 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14801 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14802 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14803 break;
14805 case DEBUG_PARAMETER_REF:
14806 mem_loc_result = parameter_ref_descriptor (rtl);
14807 break;
14809 case PRE_MODIFY:
14810 /* Extract the PLUS expression nested inside and fall into
14811 PLUS code below. */
14812 rtl = XEXP (rtl, 1);
14813 goto plus;
14815 case PRE_INC:
14816 case PRE_DEC:
14817 /* Turn these into a PLUS expression and fall into the PLUS code
14818 below. */
14819 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14820 gen_int_mode (GET_CODE (rtl) == PRE_INC
14821 ? GET_MODE_UNIT_SIZE (mem_mode)
14822 : -GET_MODE_UNIT_SIZE (mem_mode),
14823 mode));
14825 /* fall through */
14827 case PLUS:
14828 plus:
14829 if (is_based_loc (rtl)
14830 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14831 || XEXP (rtl, 0) == arg_pointer_rtx
14832 || XEXP (rtl, 0) == frame_pointer_rtx)
14833 && SCALAR_INT_MODE_P (mode))
14834 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14835 INTVAL (XEXP (rtl, 1)),
14836 VAR_INIT_STATUS_INITIALIZED);
14837 else
14839 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14840 VAR_INIT_STATUS_INITIALIZED);
14841 if (mem_loc_result == 0)
14842 break;
14844 if (CONST_INT_P (XEXP (rtl, 1))
14845 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14846 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14847 else
14849 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14850 VAR_INIT_STATUS_INITIALIZED);
14851 if (op1 == 0)
14852 return NULL;
14853 add_loc_descr (&mem_loc_result, op1);
14854 add_loc_descr (&mem_loc_result,
14855 new_loc_descr (DW_OP_plus, 0, 0));
14858 break;
14860 /* If a pseudo-reg is optimized away, it is possible for it to
14861 be replaced with a MEM containing a multiply or shift. */
14862 case MINUS:
14863 op = DW_OP_minus;
14864 goto do_binop;
14866 case MULT:
14867 op = DW_OP_mul;
14868 goto do_binop;
14870 case DIV:
14871 if ((!dwarf_strict || dwarf_version >= 5)
14872 && SCALAR_INT_MODE_P (mode)
14873 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14875 mem_loc_result = typed_binop (DW_OP_div, rtl,
14876 base_type_for_mode (mode, 0),
14877 mode, mem_mode);
14878 break;
14880 op = DW_OP_div;
14881 goto do_binop;
14883 case UMOD:
14884 op = DW_OP_mod;
14885 goto do_binop;
14887 case ASHIFT:
14888 op = DW_OP_shl;
14889 goto do_shift;
14891 case ASHIFTRT:
14892 op = DW_OP_shra;
14893 goto do_shift;
14895 case LSHIFTRT:
14896 op = DW_OP_shr;
14897 goto do_shift;
14899 do_shift:
14900 if (!SCALAR_INT_MODE_P (mode))
14901 break;
14902 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14903 VAR_INIT_STATUS_INITIALIZED);
14905 rtx rtlop1 = XEXP (rtl, 1);
14906 if (GET_MODE (rtlop1) != VOIDmode
14907 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
14908 < GET_MODE_BITSIZE (mode))
14909 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14910 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14911 VAR_INIT_STATUS_INITIALIZED);
14914 if (op0 == 0 || op1 == 0)
14915 break;
14917 mem_loc_result = op0;
14918 add_loc_descr (&mem_loc_result, op1);
14919 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14920 break;
14922 case AND:
14923 op = DW_OP_and;
14924 goto do_binop;
14926 case IOR:
14927 op = DW_OP_or;
14928 goto do_binop;
14930 case XOR:
14931 op = DW_OP_xor;
14932 goto do_binop;
14934 do_binop:
14935 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14936 VAR_INIT_STATUS_INITIALIZED);
14937 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14938 VAR_INIT_STATUS_INITIALIZED);
14940 if (op0 == 0 || op1 == 0)
14941 break;
14943 mem_loc_result = op0;
14944 add_loc_descr (&mem_loc_result, op1);
14945 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14946 break;
14948 case MOD:
14949 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14950 && (!dwarf_strict || dwarf_version >= 5))
14952 mem_loc_result = typed_binop (DW_OP_mod, rtl,
14953 base_type_for_mode (mode, 0),
14954 mode, mem_mode);
14955 break;
14958 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14959 VAR_INIT_STATUS_INITIALIZED);
14960 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14961 VAR_INIT_STATUS_INITIALIZED);
14963 if (op0 == 0 || op1 == 0)
14964 break;
14966 mem_loc_result = op0;
14967 add_loc_descr (&mem_loc_result, op1);
14968 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14969 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14970 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14971 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14972 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14973 break;
14975 case UDIV:
14976 if ((!dwarf_strict || dwarf_version >= 5)
14977 && SCALAR_INT_MODE_P (mode))
14979 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14981 op = DW_OP_div;
14982 goto do_binop;
14984 mem_loc_result = typed_binop (DW_OP_div, rtl,
14985 base_type_for_mode (mode, 1),
14986 mode, mem_mode);
14988 break;
14990 case NOT:
14991 op = DW_OP_not;
14992 goto do_unop;
14994 case ABS:
14995 op = DW_OP_abs;
14996 goto do_unop;
14998 case NEG:
14999 op = DW_OP_neg;
15000 goto do_unop;
15002 do_unop:
15003 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15004 VAR_INIT_STATUS_INITIALIZED);
15006 if (op0 == 0)
15007 break;
15009 mem_loc_result = op0;
15010 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15011 break;
15013 case CONST_INT:
15014 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15015 #ifdef POINTERS_EXTEND_UNSIGNED
15016 || (mode == Pmode
15017 && mem_mode != VOIDmode
15018 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15019 #endif
15022 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15023 break;
15025 if ((!dwarf_strict || dwarf_version >= 5)
15026 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15027 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15029 dw_die_ref type_die = base_type_for_mode (mode, 1);
15030 machine_mode amode;
15031 if (type_die == NULL)
15032 return NULL;
15033 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15034 MODE_INT, 0);
15035 if (INTVAL (rtl) >= 0
15036 && amode != BLKmode
15037 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15038 /* const DW_OP_convert <XXX> vs.
15039 DW_OP_const_type <XXX, 1, const>. */
15040 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15041 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15043 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15044 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15045 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15046 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15047 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15048 add_loc_descr (&mem_loc_result, op0);
15049 return mem_loc_result;
15051 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15052 INTVAL (rtl));
15053 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15054 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15055 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15056 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15057 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15058 else
15060 mem_loc_result->dw_loc_oprnd2.val_class
15061 = dw_val_class_const_double;
15062 mem_loc_result->dw_loc_oprnd2.v.val_double
15063 = double_int::from_shwi (INTVAL (rtl));
15066 break;
15068 case CONST_DOUBLE:
15069 if (!dwarf_strict || dwarf_version >= 5)
15071 dw_die_ref type_die;
15073 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15074 CONST_DOUBLE rtx could represent either a large integer
15075 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15076 the value is always a floating point constant.
15078 When it is an integer, a CONST_DOUBLE is used whenever
15079 the constant requires 2 HWIs to be adequately represented.
15080 We output CONST_DOUBLEs as blocks. */
15081 if (mode == VOIDmode
15082 || (GET_MODE (rtl) == VOIDmode
15083 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15084 break;
15085 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15086 if (type_die == NULL)
15087 return NULL;
15088 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15089 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15090 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15091 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15092 #if TARGET_SUPPORTS_WIDE_INT == 0
15093 if (!SCALAR_FLOAT_MODE_P (mode))
15095 mem_loc_result->dw_loc_oprnd2.val_class
15096 = dw_val_class_const_double;
15097 mem_loc_result->dw_loc_oprnd2.v.val_double
15098 = rtx_to_double_int (rtl);
15100 else
15101 #endif
15103 unsigned int length = GET_MODE_SIZE (mode);
15104 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15106 insert_float (rtl, array);
15107 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15108 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15109 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15110 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15113 break;
15115 case CONST_WIDE_INT:
15116 if (!dwarf_strict || dwarf_version >= 5)
15118 dw_die_ref type_die;
15120 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15121 if (type_die == NULL)
15122 return NULL;
15123 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15124 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15125 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15126 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15127 mem_loc_result->dw_loc_oprnd2.val_class
15128 = dw_val_class_wide_int;
15129 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15130 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
15132 break;
15134 case EQ:
15135 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15136 break;
15138 case GE:
15139 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15140 break;
15142 case GT:
15143 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15144 break;
15146 case LE:
15147 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15148 break;
15150 case LT:
15151 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15152 break;
15154 case NE:
15155 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15156 break;
15158 case GEU:
15159 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15160 break;
15162 case GTU:
15163 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15164 break;
15166 case LEU:
15167 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15168 break;
15170 case LTU:
15171 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15172 break;
15174 case UMIN:
15175 case UMAX:
15176 if (!SCALAR_INT_MODE_P (mode))
15177 break;
15178 /* FALLTHRU */
15179 case SMIN:
15180 case SMAX:
15181 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15182 break;
15184 case ZERO_EXTRACT:
15185 case SIGN_EXTRACT:
15186 if (CONST_INT_P (XEXP (rtl, 1))
15187 && CONST_INT_P (XEXP (rtl, 2))
15188 && ((unsigned) INTVAL (XEXP (rtl, 1))
15189 + (unsigned) INTVAL (XEXP (rtl, 2))
15190 <= GET_MODE_BITSIZE (mode))
15191 && SCALAR_INT_MODE_P (mode)
15192 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15193 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15195 int shift, size;
15196 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15197 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15198 if (op0 == 0)
15199 break;
15200 if (GET_CODE (rtl) == SIGN_EXTRACT)
15201 op = DW_OP_shra;
15202 else
15203 op = DW_OP_shr;
15204 mem_loc_result = op0;
15205 size = INTVAL (XEXP (rtl, 1));
15206 shift = INTVAL (XEXP (rtl, 2));
15207 if (BITS_BIG_ENDIAN)
15208 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15209 - shift - size;
15210 if (shift + size != (int) DWARF2_ADDR_SIZE)
15212 add_loc_descr (&mem_loc_result,
15213 int_loc_descriptor (DWARF2_ADDR_SIZE
15214 - shift - size));
15215 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15217 if (size != (int) DWARF2_ADDR_SIZE)
15219 add_loc_descr (&mem_loc_result,
15220 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15221 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15224 break;
15226 case IF_THEN_ELSE:
15228 dw_loc_descr_ref op2, bra_node, drop_node;
15229 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15230 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15231 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15232 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15233 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15234 VAR_INIT_STATUS_INITIALIZED);
15235 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15236 VAR_INIT_STATUS_INITIALIZED);
15237 if (op0 == NULL || op1 == NULL || op2 == NULL)
15238 break;
15240 mem_loc_result = op1;
15241 add_loc_descr (&mem_loc_result, op2);
15242 add_loc_descr (&mem_loc_result, op0);
15243 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15244 add_loc_descr (&mem_loc_result, bra_node);
15245 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15246 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15247 add_loc_descr (&mem_loc_result, drop_node);
15248 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15249 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15251 break;
15253 case FLOAT_EXTEND:
15254 case FLOAT_TRUNCATE:
15255 case FLOAT:
15256 case UNSIGNED_FLOAT:
15257 case FIX:
15258 case UNSIGNED_FIX:
15259 if (!dwarf_strict || dwarf_version >= 5)
15261 dw_die_ref type_die;
15262 dw_loc_descr_ref cvt;
15264 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15265 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15266 if (op0 == NULL)
15267 break;
15268 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15269 && (GET_CODE (rtl) == FLOAT
15270 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15271 <= DWARF2_ADDR_SIZE))
15273 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15274 GET_CODE (rtl) == UNSIGNED_FLOAT);
15275 if (type_die == NULL)
15276 break;
15277 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15278 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15279 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15280 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15281 add_loc_descr (&op0, cvt);
15283 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15284 if (type_die == NULL)
15285 break;
15286 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15287 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15288 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15289 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15290 add_loc_descr (&op0, cvt);
15291 if (SCALAR_INT_MODE_P (mode)
15292 && (GET_CODE (rtl) == FIX
15293 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15295 op0 = convert_descriptor_to_mode (mode, op0);
15296 if (op0 == NULL)
15297 break;
15299 mem_loc_result = op0;
15301 break;
15303 case CLZ:
15304 case CTZ:
15305 case FFS:
15306 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15307 break;
15309 case POPCOUNT:
15310 case PARITY:
15311 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15312 break;
15314 case BSWAP:
15315 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15316 break;
15318 case ROTATE:
15319 case ROTATERT:
15320 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15321 break;
15323 case COMPARE:
15324 /* In theory, we could implement the above. */
15325 /* DWARF cannot represent the unsigned compare operations
15326 natively. */
15327 case SS_MULT:
15328 case US_MULT:
15329 case SS_DIV:
15330 case US_DIV:
15331 case SS_PLUS:
15332 case US_PLUS:
15333 case SS_MINUS:
15334 case US_MINUS:
15335 case SS_NEG:
15336 case US_NEG:
15337 case SS_ABS:
15338 case SS_ASHIFT:
15339 case US_ASHIFT:
15340 case SS_TRUNCATE:
15341 case US_TRUNCATE:
15342 case UNORDERED:
15343 case ORDERED:
15344 case UNEQ:
15345 case UNGE:
15346 case UNGT:
15347 case UNLE:
15348 case UNLT:
15349 case LTGT:
15350 case FRACT_CONVERT:
15351 case UNSIGNED_FRACT_CONVERT:
15352 case SAT_FRACT:
15353 case UNSIGNED_SAT_FRACT:
15354 case SQRT:
15355 case ASM_OPERANDS:
15356 case VEC_MERGE:
15357 case VEC_SELECT:
15358 case VEC_CONCAT:
15359 case VEC_DUPLICATE:
15360 case UNSPEC:
15361 case HIGH:
15362 case FMA:
15363 case STRICT_LOW_PART:
15364 case CONST_VECTOR:
15365 case CONST_FIXED:
15366 case CLRSB:
15367 case CLOBBER:
15368 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15369 can't express it in the debug info. This can happen e.g. with some
15370 TLS UNSPECs. */
15371 break;
15373 case CONST_STRING:
15374 resolve_one_addr (&rtl);
15375 goto symref;
15377 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15378 the expression. An UNSPEC rtx represents a raw DWARF operation,
15379 new_loc_descr is called for it to build the operation directly.
15380 Otherwise mem_loc_descriptor is called recursively. */
15381 case PARALLEL:
15383 int index = 0;
15384 dw_loc_descr_ref exp_result = NULL;
15386 for (; index < XVECLEN (rtl, 0); index++)
15388 rtx elem = XVECEXP (rtl, 0, index);
15389 if (GET_CODE (elem) == UNSPEC)
15391 /* Each DWARF operation UNSPEC contain two operands, if
15392 one operand is not used for the operation, const0_rtx is
15393 passed. */
15394 gcc_assert (XVECLEN (elem, 0) == 2);
15396 HOST_WIDE_INT dw_op = XINT (elem, 1);
15397 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15398 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15399 exp_result
15400 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15401 oprnd2);
15403 else
15404 exp_result
15405 = mem_loc_descriptor (elem, mode, mem_mode,
15406 VAR_INIT_STATUS_INITIALIZED);
15408 if (!mem_loc_result)
15409 mem_loc_result = exp_result;
15410 else
15411 add_loc_descr (&mem_loc_result, exp_result);
15414 break;
15417 default:
15418 if (flag_checking)
15420 print_rtl (stderr, rtl);
15421 gcc_unreachable ();
15423 break;
15426 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15427 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15429 return mem_loc_result;
15432 /* Return a descriptor that describes the concatenation of two locations.
15433 This is typically a complex variable. */
15435 static dw_loc_descr_ref
15436 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15438 dw_loc_descr_ref cc_loc_result = NULL;
15439 dw_loc_descr_ref x0_ref
15440 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15441 dw_loc_descr_ref x1_ref
15442 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15444 if (x0_ref == 0 || x1_ref == 0)
15445 return 0;
15447 cc_loc_result = x0_ref;
15448 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15450 add_loc_descr (&cc_loc_result, x1_ref);
15451 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15453 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15454 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15456 return cc_loc_result;
15459 /* Return a descriptor that describes the concatenation of N
15460 locations. */
15462 static dw_loc_descr_ref
15463 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15465 unsigned int i;
15466 dw_loc_descr_ref cc_loc_result = NULL;
15467 unsigned int n = XVECLEN (concatn, 0);
15469 for (i = 0; i < n; ++i)
15471 dw_loc_descr_ref ref;
15472 rtx x = XVECEXP (concatn, 0, i);
15474 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15475 if (ref == NULL)
15476 return NULL;
15478 add_loc_descr (&cc_loc_result, ref);
15479 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15482 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15483 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15485 return cc_loc_result;
15488 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15489 for DEBUG_IMPLICIT_PTR RTL. */
15491 static dw_loc_descr_ref
15492 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15494 dw_loc_descr_ref ret;
15495 dw_die_ref ref;
15497 if (dwarf_strict && dwarf_version < 5)
15498 return NULL;
15499 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15500 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15501 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15502 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15503 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15504 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15505 if (ref)
15507 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15508 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15509 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15511 else
15513 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15514 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15516 return ret;
15519 /* Output a proper Dwarf location descriptor for a variable or parameter
15520 which is either allocated in a register or in a memory location. For a
15521 register, we just generate an OP_REG and the register number. For a
15522 memory location we provide a Dwarf postfix expression describing how to
15523 generate the (dynamic) address of the object onto the address stack.
15525 MODE is mode of the decl if this loc_descriptor is going to be used in
15526 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15527 allowed, VOIDmode otherwise.
15529 If we don't know how to describe it, return 0. */
15531 static dw_loc_descr_ref
15532 loc_descriptor (rtx rtl, machine_mode mode,
15533 enum var_init_status initialized)
15535 dw_loc_descr_ref loc_result = NULL;
15537 switch (GET_CODE (rtl))
15539 case SUBREG:
15540 /* The case of a subreg may arise when we have a local (register)
15541 variable or a formal (register) parameter which doesn't quite fill
15542 up an entire register. For now, just assume that it is
15543 legitimate to make the Dwarf info refer to the whole register which
15544 contains the given subreg. */
15545 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15546 loc_result = loc_descriptor (SUBREG_REG (rtl),
15547 GET_MODE (SUBREG_REG (rtl)), initialized);
15548 else
15549 goto do_default;
15550 break;
15552 case REG:
15553 loc_result = reg_loc_descriptor (rtl, initialized);
15554 break;
15556 case MEM:
15557 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15558 GET_MODE (rtl), initialized);
15559 if (loc_result == NULL)
15560 loc_result = tls_mem_loc_descriptor (rtl);
15561 if (loc_result == NULL)
15563 rtx new_rtl = avoid_constant_pool_reference (rtl);
15564 if (new_rtl != rtl)
15565 loc_result = loc_descriptor (new_rtl, mode, initialized);
15567 break;
15569 case CONCAT:
15570 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15571 initialized);
15572 break;
15574 case CONCATN:
15575 loc_result = concatn_loc_descriptor (rtl, initialized);
15576 break;
15578 case VAR_LOCATION:
15579 /* Single part. */
15580 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15582 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15583 if (GET_CODE (loc) == EXPR_LIST)
15584 loc = XEXP (loc, 0);
15585 loc_result = loc_descriptor (loc, mode, initialized);
15586 break;
15589 rtl = XEXP (rtl, 1);
15590 /* FALLTHRU */
15592 case PARALLEL:
15594 rtvec par_elems = XVEC (rtl, 0);
15595 int num_elem = GET_NUM_ELEM (par_elems);
15596 machine_mode mode;
15597 int i;
15599 /* Create the first one, so we have something to add to. */
15600 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15601 VOIDmode, initialized);
15602 if (loc_result == NULL)
15603 return NULL;
15604 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15605 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15606 for (i = 1; i < num_elem; i++)
15608 dw_loc_descr_ref temp;
15610 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15611 VOIDmode, initialized);
15612 if (temp == NULL)
15613 return NULL;
15614 add_loc_descr (&loc_result, temp);
15615 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15616 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15619 break;
15621 case CONST_INT:
15622 if (mode != VOIDmode && mode != BLKmode)
15623 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15624 INTVAL (rtl));
15625 break;
15627 case CONST_DOUBLE:
15628 if (mode == VOIDmode)
15629 mode = GET_MODE (rtl);
15631 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15633 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15635 /* Note that a CONST_DOUBLE rtx could represent either an integer
15636 or a floating-point constant. A CONST_DOUBLE is used whenever
15637 the constant requires more than one word in order to be
15638 adequately represented. We output CONST_DOUBLEs as blocks. */
15639 loc_result = new_loc_descr (DW_OP_implicit_value,
15640 GET_MODE_SIZE (mode), 0);
15641 #if TARGET_SUPPORTS_WIDE_INT == 0
15642 if (!SCALAR_FLOAT_MODE_P (mode))
15644 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15645 loc_result->dw_loc_oprnd2.v.val_double
15646 = rtx_to_double_int (rtl);
15648 else
15649 #endif
15651 unsigned int length = GET_MODE_SIZE (mode);
15652 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15654 insert_float (rtl, array);
15655 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15656 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15657 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15658 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15661 break;
15663 case CONST_WIDE_INT:
15664 if (mode == VOIDmode)
15665 mode = GET_MODE (rtl);
15667 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15669 loc_result = new_loc_descr (DW_OP_implicit_value,
15670 GET_MODE_SIZE (mode), 0);
15671 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15672 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15673 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
15675 break;
15677 case CONST_VECTOR:
15678 if (mode == VOIDmode)
15679 mode = GET_MODE (rtl);
15681 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15683 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15684 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15685 unsigned char *array
15686 = ggc_vec_alloc<unsigned char> (length * elt_size);
15687 unsigned int i;
15688 unsigned char *p;
15689 machine_mode imode = GET_MODE_INNER (mode);
15691 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15692 switch (GET_MODE_CLASS (mode))
15694 case MODE_VECTOR_INT:
15695 for (i = 0, p = array; i < length; i++, p += elt_size)
15697 rtx elt = CONST_VECTOR_ELT (rtl, i);
15698 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15700 break;
15702 case MODE_VECTOR_FLOAT:
15703 for (i = 0, p = array; i < length; i++, p += elt_size)
15705 rtx elt = CONST_VECTOR_ELT (rtl, i);
15706 insert_float (elt, p);
15708 break;
15710 default:
15711 gcc_unreachable ();
15714 loc_result = new_loc_descr (DW_OP_implicit_value,
15715 length * elt_size, 0);
15716 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15717 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15718 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15719 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15721 break;
15723 case CONST:
15724 if (mode == VOIDmode
15725 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15726 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15727 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15729 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15730 break;
15732 /* FALLTHROUGH */
15733 case SYMBOL_REF:
15734 if (!const_ok_for_output (rtl))
15735 break;
15736 /* FALLTHROUGH */
15737 case LABEL_REF:
15738 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15739 && (dwarf_version >= 4 || !dwarf_strict))
15741 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15742 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15743 vec_safe_push (used_rtx_array, rtl);
15745 break;
15747 case DEBUG_IMPLICIT_PTR:
15748 loc_result = implicit_ptr_descriptor (rtl, 0);
15749 break;
15751 case PLUS:
15752 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15753 && CONST_INT_P (XEXP (rtl, 1)))
15755 loc_result
15756 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15757 break;
15759 /* FALLTHRU */
15760 do_default:
15761 default:
15762 if ((SCALAR_INT_MODE_P (mode)
15763 && GET_MODE (rtl) == mode
15764 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15765 && dwarf_version >= 4)
15766 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15768 /* Value expression. */
15769 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15770 if (loc_result)
15771 add_loc_descr (&loc_result,
15772 new_loc_descr (DW_OP_stack_value, 0, 0));
15774 break;
15777 return loc_result;
15780 /* We need to figure out what section we should use as the base for the
15781 address ranges where a given location is valid.
15782 1. If this particular DECL has a section associated with it, use that.
15783 2. If this function has a section associated with it, use that.
15784 3. Otherwise, use the text section.
15785 XXX: If you split a variable across multiple sections, we won't notice. */
15787 static const char *
15788 secname_for_decl (const_tree decl)
15790 const char *secname;
15792 if (VAR_OR_FUNCTION_DECL_P (decl)
15793 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15794 && DECL_SECTION_NAME (decl))
15795 secname = DECL_SECTION_NAME (decl);
15796 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15797 secname = DECL_SECTION_NAME (current_function_decl);
15798 else if (cfun && in_cold_section_p)
15799 secname = crtl->subsections.cold_section_label;
15800 else
15801 secname = text_section_label;
15803 return secname;
15806 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15808 static bool
15809 decl_by_reference_p (tree decl)
15811 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15812 || VAR_P (decl))
15813 && DECL_BY_REFERENCE (decl));
15816 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15817 for VARLOC. */
15819 static dw_loc_descr_ref
15820 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15821 enum var_init_status initialized)
15823 int have_address = 0;
15824 dw_loc_descr_ref descr;
15825 machine_mode mode;
15827 if (want_address != 2)
15829 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15830 /* Single part. */
15831 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15833 varloc = PAT_VAR_LOCATION_LOC (varloc);
15834 if (GET_CODE (varloc) == EXPR_LIST)
15835 varloc = XEXP (varloc, 0);
15836 mode = GET_MODE (varloc);
15837 if (MEM_P (varloc))
15839 rtx addr = XEXP (varloc, 0);
15840 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15841 mode, initialized);
15842 if (descr)
15843 have_address = 1;
15844 else
15846 rtx x = avoid_constant_pool_reference (varloc);
15847 if (x != varloc)
15848 descr = mem_loc_descriptor (x, mode, VOIDmode,
15849 initialized);
15852 else
15853 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15855 else
15856 return 0;
15858 else
15860 if (GET_CODE (varloc) == VAR_LOCATION)
15861 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15862 else
15863 mode = DECL_MODE (loc);
15864 descr = loc_descriptor (varloc, mode, initialized);
15865 have_address = 1;
15868 if (!descr)
15869 return 0;
15871 if (want_address == 2 && !have_address
15872 && (dwarf_version >= 4 || !dwarf_strict))
15874 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15876 expansion_failed (loc, NULL_RTX,
15877 "DWARF address size mismatch");
15878 return 0;
15880 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15881 have_address = 1;
15883 /* Show if we can't fill the request for an address. */
15884 if (want_address && !have_address)
15886 expansion_failed (loc, NULL_RTX,
15887 "Want address and only have value");
15888 return 0;
15891 /* If we've got an address and don't want one, dereference. */
15892 if (!want_address && have_address)
15894 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15895 enum dwarf_location_atom op;
15897 if (size > DWARF2_ADDR_SIZE || size == -1)
15899 expansion_failed (loc, NULL_RTX,
15900 "DWARF address size mismatch");
15901 return 0;
15903 else if (size == DWARF2_ADDR_SIZE)
15904 op = DW_OP_deref;
15905 else
15906 op = DW_OP_deref_size;
15908 add_loc_descr (&descr, new_loc_descr (op, size, 0));
15911 return descr;
15914 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15915 if it is not possible. */
15917 static dw_loc_descr_ref
15918 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15920 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15921 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15922 else if (dwarf_version >= 3 || !dwarf_strict)
15923 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15924 else
15925 return NULL;
15928 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15929 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
15931 static dw_loc_descr_ref
15932 dw_sra_loc_expr (tree decl, rtx loc)
15934 rtx p;
15935 unsigned HOST_WIDE_INT padsize = 0;
15936 dw_loc_descr_ref descr, *descr_tail;
15937 unsigned HOST_WIDE_INT decl_size;
15938 rtx varloc;
15939 enum var_init_status initialized;
15941 if (DECL_SIZE (decl) == NULL
15942 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
15943 return NULL;
15945 decl_size = tree_to_uhwi (DECL_SIZE (decl));
15946 descr = NULL;
15947 descr_tail = &descr;
15949 for (p = loc; p; p = XEXP (p, 1))
15951 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
15952 rtx loc_note = *decl_piece_varloc_ptr (p);
15953 dw_loc_descr_ref cur_descr;
15954 dw_loc_descr_ref *tail, last = NULL;
15955 unsigned HOST_WIDE_INT opsize = 0;
15957 if (loc_note == NULL_RTX
15958 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15960 padsize += bitsize;
15961 continue;
15963 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15964 varloc = NOTE_VAR_LOCATION (loc_note);
15965 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15966 if (cur_descr == NULL)
15968 padsize += bitsize;
15969 continue;
15972 /* Check that cur_descr either doesn't use
15973 DW_OP_*piece operations, or their sum is equal
15974 to bitsize. Otherwise we can't embed it. */
15975 for (tail = &cur_descr; *tail != NULL;
15976 tail = &(*tail)->dw_loc_next)
15977 if ((*tail)->dw_loc_opc == DW_OP_piece)
15979 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
15980 * BITS_PER_UNIT;
15981 last = *tail;
15983 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
15985 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
15986 last = *tail;
15989 if (last != NULL && opsize != bitsize)
15991 padsize += bitsize;
15992 /* Discard the current piece of the descriptor and release any
15993 addr_table entries it uses. */
15994 remove_loc_list_addr_table_entries (cur_descr);
15995 continue;
15998 /* If there is a hole, add DW_OP_*piece after empty DWARF
15999 expression, which means that those bits are optimized out. */
16000 if (padsize)
16002 if (padsize > decl_size)
16004 remove_loc_list_addr_table_entries (cur_descr);
16005 goto discard_descr;
16007 decl_size -= padsize;
16008 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16009 if (*descr_tail == NULL)
16011 remove_loc_list_addr_table_entries (cur_descr);
16012 goto discard_descr;
16014 descr_tail = &(*descr_tail)->dw_loc_next;
16015 padsize = 0;
16017 *descr_tail = cur_descr;
16018 descr_tail = tail;
16019 if (bitsize > decl_size)
16020 goto discard_descr;
16021 decl_size -= bitsize;
16022 if (last == NULL)
16024 HOST_WIDE_INT offset = 0;
16025 if (GET_CODE (varloc) == VAR_LOCATION
16026 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16028 varloc = PAT_VAR_LOCATION_LOC (varloc);
16029 if (GET_CODE (varloc) == EXPR_LIST)
16030 varloc = XEXP (varloc, 0);
16034 if (GET_CODE (varloc) == CONST
16035 || GET_CODE (varloc) == SIGN_EXTEND
16036 || GET_CODE (varloc) == ZERO_EXTEND)
16037 varloc = XEXP (varloc, 0);
16038 else if (GET_CODE (varloc) == SUBREG)
16039 varloc = SUBREG_REG (varloc);
16040 else
16041 break;
16043 while (1);
16044 /* DW_OP_bit_size offset should be zero for register
16045 or implicit location descriptions and empty location
16046 descriptions, but for memory addresses needs big endian
16047 adjustment. */
16048 if (MEM_P (varloc))
16050 unsigned HOST_WIDE_INT memsize
16051 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16052 if (memsize != bitsize)
16054 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16055 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16056 goto discard_descr;
16057 if (memsize < bitsize)
16058 goto discard_descr;
16059 if (BITS_BIG_ENDIAN)
16060 offset = memsize - bitsize;
16064 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16065 if (*descr_tail == NULL)
16066 goto discard_descr;
16067 descr_tail = &(*descr_tail)->dw_loc_next;
16071 /* If there were any non-empty expressions, add padding till the end of
16072 the decl. */
16073 if (descr != NULL && decl_size != 0)
16075 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16076 if (*descr_tail == NULL)
16077 goto discard_descr;
16079 return descr;
16081 discard_descr:
16082 /* Discard the descriptor and release any addr_table entries it uses. */
16083 remove_loc_list_addr_table_entries (descr);
16084 return NULL;
16087 /* Return the dwarf representation of the location list LOC_LIST of
16088 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16089 function. */
16091 static dw_loc_list_ref
16092 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16094 const char *endname, *secname;
16095 rtx varloc;
16096 enum var_init_status initialized;
16097 struct var_loc_node *node;
16098 dw_loc_descr_ref descr;
16099 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16100 dw_loc_list_ref list = NULL;
16101 dw_loc_list_ref *listp = &list;
16103 /* Now that we know what section we are using for a base,
16104 actually construct the list of locations.
16105 The first location information is what is passed to the
16106 function that creates the location list, and the remaining
16107 locations just get added on to that list.
16108 Note that we only know the start address for a location
16109 (IE location changes), so to build the range, we use
16110 the range [current location start, next location start].
16111 This means we have to special case the last node, and generate
16112 a range of [last location start, end of function label]. */
16114 secname = secname_for_decl (decl);
16116 for (node = loc_list->first; node; node = node->next)
16117 if (GET_CODE (node->loc) == EXPR_LIST
16118 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16120 if (GET_CODE (node->loc) == EXPR_LIST)
16122 /* This requires DW_OP_{,bit_}piece, which is not usable
16123 inside DWARF expressions. */
16124 if (want_address != 2)
16125 continue;
16126 descr = dw_sra_loc_expr (decl, node->loc);
16127 if (descr == NULL)
16128 continue;
16130 else
16132 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16133 varloc = NOTE_VAR_LOCATION (node->loc);
16134 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16136 if (descr)
16138 bool range_across_switch = false;
16139 /* If section switch happens in between node->label
16140 and node->next->label (or end of function) and
16141 we can't emit it as a single entry list,
16142 emit two ranges, first one ending at the end
16143 of first partition and second one starting at the
16144 beginning of second partition. */
16145 if (node == loc_list->last_before_switch
16146 && (node != loc_list->first || loc_list->first->next)
16147 && current_function_decl)
16149 endname = cfun->fde->dw_fde_end;
16150 range_across_switch = true;
16152 /* The variable has a location between NODE->LABEL and
16153 NODE->NEXT->LABEL. */
16154 else if (node->next)
16155 endname = node->next->label;
16156 /* If the variable has a location at the last label
16157 it keeps its location until the end of function. */
16158 else if (!current_function_decl)
16159 endname = text_end_label;
16160 else
16162 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16163 current_function_funcdef_no);
16164 endname = ggc_strdup (label_id);
16167 *listp = new_loc_list (descr, node->label, endname, secname);
16168 if (TREE_CODE (decl) == PARM_DECL
16169 && node == loc_list->first
16170 && NOTE_P (node->loc)
16171 && strcmp (node->label, endname) == 0)
16172 (*listp)->force = true;
16173 listp = &(*listp)->dw_loc_next;
16175 if (range_across_switch)
16177 if (GET_CODE (node->loc) == EXPR_LIST)
16178 descr = dw_sra_loc_expr (decl, node->loc);
16179 else
16181 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16182 varloc = NOTE_VAR_LOCATION (node->loc);
16183 descr = dw_loc_list_1 (decl, varloc, want_address,
16184 initialized);
16186 gcc_assert (descr);
16187 /* The variable has a location between NODE->LABEL and
16188 NODE->NEXT->LABEL. */
16189 if (node->next)
16190 endname = node->next->label;
16191 else
16192 endname = cfun->fde->dw_fde_second_end;
16193 *listp = new_loc_list (descr,
16194 cfun->fde->dw_fde_second_begin,
16195 endname, secname);
16196 listp = &(*listp)->dw_loc_next;
16201 /* Try to avoid the overhead of a location list emitting a location
16202 expression instead, but only if we didn't have more than one
16203 location entry in the first place. If some entries were not
16204 representable, we don't want to pretend a single entry that was
16205 applies to the entire scope in which the variable is
16206 available. */
16207 if (list && loc_list->first->next)
16208 gen_llsym (list);
16210 return list;
16213 /* Return if the loc_list has only single element and thus can be represented
16214 as location description. */
16216 static bool
16217 single_element_loc_list_p (dw_loc_list_ref list)
16219 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16220 return !list->ll_symbol;
16223 /* To each location in list LIST add loc descr REF. */
16225 static void
16226 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16228 dw_loc_descr_ref copy;
16229 add_loc_descr (&list->expr, ref);
16230 list = list->dw_loc_next;
16231 while (list)
16233 copy = ggc_alloc<dw_loc_descr_node> ();
16234 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16235 add_loc_descr (&list->expr, copy);
16236 while (copy->dw_loc_next)
16238 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
16239 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16240 copy->dw_loc_next = new_copy;
16241 copy = new_copy;
16243 list = list->dw_loc_next;
16247 /* Given two lists RET and LIST
16248 produce location list that is result of adding expression in LIST
16249 to expression in RET on each position in program.
16250 Might be destructive on both RET and LIST.
16252 TODO: We handle only simple cases of RET or LIST having at most one
16253 element. General case would inolve sorting the lists in program order
16254 and merging them that will need some additional work.
16255 Adding that will improve quality of debug info especially for SRA-ed
16256 structures. */
16258 static void
16259 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16261 if (!list)
16262 return;
16263 if (!*ret)
16265 *ret = list;
16266 return;
16268 if (!list->dw_loc_next)
16270 add_loc_descr_to_each (*ret, list->expr);
16271 return;
16273 if (!(*ret)->dw_loc_next)
16275 add_loc_descr_to_each (list, (*ret)->expr);
16276 *ret = list;
16277 return;
16279 expansion_failed (NULL_TREE, NULL_RTX,
16280 "Don't know how to merge two non-trivial"
16281 " location lists.\n");
16282 *ret = NULL;
16283 return;
16286 /* LOC is constant expression. Try a luck, look it up in constant
16287 pool and return its loc_descr of its address. */
16289 static dw_loc_descr_ref
16290 cst_pool_loc_descr (tree loc)
16292 /* Get an RTL for this, if something has been emitted. */
16293 rtx rtl = lookup_constant_def (loc);
16295 if (!rtl || !MEM_P (rtl))
16297 gcc_assert (!rtl);
16298 return 0;
16300 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16302 /* TODO: We might get more coverage if we was actually delaying expansion
16303 of all expressions till end of compilation when constant pools are fully
16304 populated. */
16305 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16307 expansion_failed (loc, NULL_RTX,
16308 "CST value in contant pool but not marked.");
16309 return 0;
16311 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16312 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16315 /* Return dw_loc_list representing address of addr_expr LOC
16316 by looking for inner INDIRECT_REF expression and turning
16317 it into simple arithmetics.
16319 See loc_list_from_tree for the meaning of CONTEXT. */
16321 static dw_loc_list_ref
16322 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16323 loc_descr_context *context)
16325 tree obj, offset;
16326 HOST_WIDE_INT bitsize, bitpos, bytepos;
16327 machine_mode mode;
16328 int unsignedp, reversep, volatilep = 0;
16329 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16331 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16332 &bitsize, &bitpos, &offset, &mode,
16333 &unsignedp, &reversep, &volatilep);
16334 STRIP_NOPS (obj);
16335 if (bitpos % BITS_PER_UNIT)
16337 expansion_failed (loc, NULL_RTX, "bitfield access");
16338 return 0;
16340 if (!INDIRECT_REF_P (obj))
16342 expansion_failed (obj,
16343 NULL_RTX, "no indirect ref in inner refrence");
16344 return 0;
16346 if (!offset && !bitpos)
16347 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16348 context);
16349 else if (toplev
16350 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16351 && (dwarf_version >= 4 || !dwarf_strict))
16353 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16354 if (!list_ret)
16355 return 0;
16356 if (offset)
16358 /* Variable offset. */
16359 list_ret1 = loc_list_from_tree (offset, 0, context);
16360 if (list_ret1 == 0)
16361 return 0;
16362 add_loc_list (&list_ret, list_ret1);
16363 if (!list_ret)
16364 return 0;
16365 add_loc_descr_to_each (list_ret,
16366 new_loc_descr (DW_OP_plus, 0, 0));
16368 bytepos = bitpos / BITS_PER_UNIT;
16369 if (bytepos > 0)
16370 add_loc_descr_to_each (list_ret,
16371 new_loc_descr (DW_OP_plus_uconst,
16372 bytepos, 0));
16373 else if (bytepos < 0)
16374 loc_list_plus_const (list_ret, bytepos);
16375 add_loc_descr_to_each (list_ret,
16376 new_loc_descr (DW_OP_stack_value, 0, 0));
16378 return list_ret;
16381 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16382 all operations from LOC are nops, move to the last one. Insert in NOPS all
16383 operations that are skipped. */
16385 static void
16386 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16387 hash_set<dw_loc_descr_ref> &nops)
16389 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16391 nops.add (loc);
16392 loc = loc->dw_loc_next;
16396 /* Helper for loc_descr_without_nops: free the location description operation
16397 P. */
16399 bool
16400 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16402 ggc_free (loc);
16403 return true;
16406 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16407 finishes LOC. */
16409 static void
16410 loc_descr_without_nops (dw_loc_descr_ref &loc)
16412 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16413 return;
16415 /* Set of all DW_OP_nop operations we remove. */
16416 hash_set<dw_loc_descr_ref> nops;
16418 /* First, strip all prefix NOP operations in order to keep the head of the
16419 operations list. */
16420 loc_descr_to_next_no_nop (loc, nops);
16422 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16424 /* For control flow operations: strip "prefix" nops in destination
16425 labels. */
16426 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16427 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16428 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16429 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16431 /* Do the same for the operations that follow, then move to the next
16432 iteration. */
16433 if (cur->dw_loc_next != NULL)
16434 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16435 cur = cur->dw_loc_next;
16438 nops.traverse<void *, free_loc_descr> (NULL);
16442 struct dwarf_procedure_info;
16444 /* Helper structure for location descriptions generation. */
16445 struct loc_descr_context
16447 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16448 NULL_TREE if DW_OP_push_object_address in invalid for this location
16449 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16450 tree context_type;
16451 /* The ..._DECL node that should be translated as a
16452 DW_OP_push_object_address operation. */
16453 tree base_decl;
16454 /* Information about the DWARF procedure we are currently generating. NULL if
16455 we are not generating a DWARF procedure. */
16456 struct dwarf_procedure_info *dpi;
16457 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16458 by consumer. Used for DW_TAG_generic_subrange attributes. */
16459 bool placeholder_arg;
16460 /* True if PLACEHOLDER_EXPR has been seen. */
16461 bool placeholder_seen;
16464 /* DWARF procedures generation
16466 DWARF expressions (aka. location descriptions) are used to encode variable
16467 things such as sizes or offsets. Such computations can have redundant parts
16468 that can be factorized in order to reduce the size of the output debug
16469 information. This is the whole point of DWARF procedures.
16471 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16472 already factorized into functions ("size functions") in order to handle very
16473 big and complex types. Such functions are quite simple: they have integral
16474 arguments, they return an integral result and their body contains only a
16475 return statement with arithmetic expressions. This is the only kind of
16476 function we are interested in translating into DWARF procedures, here.
16478 DWARF expressions and DWARF procedure are executed using a stack, so we have
16479 to define some calling convention for them to interact. Let's say that:
16481 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16482 all arguments in reverse order (right-to-left) so that when the DWARF
16483 procedure execution starts, the first argument is the top of the stack.
16485 - Then, when returning, the DWARF procedure must have consumed all arguments
16486 on the stack, must have pushed the result and touched nothing else.
16488 - Each integral argument and the result are integral types can be hold in a
16489 single stack slot.
16491 - We call "frame offset" the number of stack slots that are "under DWARF
16492 procedure control": it includes the arguments slots, the temporaries and
16493 the result slot. Thus, it is equal to the number of arguments when the
16494 procedure execution starts and must be equal to one (the result) when it
16495 returns. */
16497 /* Helper structure used when generating operations for a DWARF procedure. */
16498 struct dwarf_procedure_info
16500 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16501 currently translated. */
16502 tree fndecl;
16503 /* The number of arguments FNDECL takes. */
16504 unsigned args_count;
16507 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16508 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16509 equate it to this DIE. */
16511 static dw_die_ref
16512 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16513 dw_die_ref parent_die)
16515 dw_die_ref dwarf_proc_die;
16517 if ((dwarf_version < 3 && dwarf_strict)
16518 || location == NULL)
16519 return NULL;
16521 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16522 if (fndecl)
16523 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16524 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16525 return dwarf_proc_die;
16528 /* Return whether TYPE is a supported type as a DWARF procedure argument
16529 type or return type (we handle only scalar types and pointer types that
16530 aren't wider than the DWARF expression evaluation stack. */
16532 static bool
16533 is_handled_procedure_type (tree type)
16535 return ((INTEGRAL_TYPE_P (type)
16536 || TREE_CODE (type) == OFFSET_TYPE
16537 || TREE_CODE (type) == POINTER_TYPE)
16538 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16541 /* Helper for resolve_args_picking: do the same but stop when coming across
16542 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16543 offset *before* evaluating the corresponding operation. */
16545 static bool
16546 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16547 struct dwarf_procedure_info *dpi,
16548 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16550 /* The "frame_offset" identifier is already used to name a macro... */
16551 unsigned frame_offset_ = initial_frame_offset;
16552 dw_loc_descr_ref l;
16554 for (l = loc; l != NULL;)
16556 bool existed;
16557 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16559 /* If we already met this node, there is nothing to compute anymore. */
16560 if (existed)
16562 /* Make sure that the stack size is consistent wherever the execution
16563 flow comes from. */
16564 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16565 break;
16567 l_frame_offset = frame_offset_;
16569 /* If needed, relocate the picking offset with respect to the frame
16570 offset. */
16571 if (l->frame_offset_rel)
16573 unsigned HOST_WIDE_INT off;
16574 switch (l->dw_loc_opc)
16576 case DW_OP_pick:
16577 off = l->dw_loc_oprnd1.v.val_unsigned;
16578 break;
16579 case DW_OP_dup:
16580 off = 0;
16581 break;
16582 case DW_OP_over:
16583 off = 1;
16584 break;
16585 default:
16586 gcc_unreachable ();
16588 /* frame_offset_ is the size of the current stack frame, including
16589 incoming arguments. Besides, the arguments are pushed
16590 right-to-left. Thus, in order to access the Nth argument from
16591 this operation node, the picking has to skip temporaries *plus*
16592 one stack slot per argument (0 for the first one, 1 for the second
16593 one, etc.).
16595 The targetted argument number (N) is already set as the operand,
16596 and the number of temporaries can be computed with:
16597 frame_offsets_ - dpi->args_count */
16598 off += frame_offset_ - dpi->args_count;
16600 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16601 if (off > 255)
16602 return false;
16604 if (off == 0)
16606 l->dw_loc_opc = DW_OP_dup;
16607 l->dw_loc_oprnd1.v.val_unsigned = 0;
16609 else if (off == 1)
16611 l->dw_loc_opc = DW_OP_over;
16612 l->dw_loc_oprnd1.v.val_unsigned = 0;
16614 else
16616 l->dw_loc_opc = DW_OP_pick;
16617 l->dw_loc_oprnd1.v.val_unsigned = off;
16621 /* Update frame_offset according to the effect the current operation has
16622 on the stack. */
16623 switch (l->dw_loc_opc)
16625 case DW_OP_deref:
16626 case DW_OP_swap:
16627 case DW_OP_rot:
16628 case DW_OP_abs:
16629 case DW_OP_neg:
16630 case DW_OP_not:
16631 case DW_OP_plus_uconst:
16632 case DW_OP_skip:
16633 case DW_OP_reg0:
16634 case DW_OP_reg1:
16635 case DW_OP_reg2:
16636 case DW_OP_reg3:
16637 case DW_OP_reg4:
16638 case DW_OP_reg5:
16639 case DW_OP_reg6:
16640 case DW_OP_reg7:
16641 case DW_OP_reg8:
16642 case DW_OP_reg9:
16643 case DW_OP_reg10:
16644 case DW_OP_reg11:
16645 case DW_OP_reg12:
16646 case DW_OP_reg13:
16647 case DW_OP_reg14:
16648 case DW_OP_reg15:
16649 case DW_OP_reg16:
16650 case DW_OP_reg17:
16651 case DW_OP_reg18:
16652 case DW_OP_reg19:
16653 case DW_OP_reg20:
16654 case DW_OP_reg21:
16655 case DW_OP_reg22:
16656 case DW_OP_reg23:
16657 case DW_OP_reg24:
16658 case DW_OP_reg25:
16659 case DW_OP_reg26:
16660 case DW_OP_reg27:
16661 case DW_OP_reg28:
16662 case DW_OP_reg29:
16663 case DW_OP_reg30:
16664 case DW_OP_reg31:
16665 case DW_OP_bregx:
16666 case DW_OP_piece:
16667 case DW_OP_deref_size:
16668 case DW_OP_nop:
16669 case DW_OP_bit_piece:
16670 case DW_OP_implicit_value:
16671 case DW_OP_stack_value:
16672 break;
16674 case DW_OP_addr:
16675 case DW_OP_const1u:
16676 case DW_OP_const1s:
16677 case DW_OP_const2u:
16678 case DW_OP_const2s:
16679 case DW_OP_const4u:
16680 case DW_OP_const4s:
16681 case DW_OP_const8u:
16682 case DW_OP_const8s:
16683 case DW_OP_constu:
16684 case DW_OP_consts:
16685 case DW_OP_dup:
16686 case DW_OP_over:
16687 case DW_OP_pick:
16688 case DW_OP_lit0:
16689 case DW_OP_lit1:
16690 case DW_OP_lit2:
16691 case DW_OP_lit3:
16692 case DW_OP_lit4:
16693 case DW_OP_lit5:
16694 case DW_OP_lit6:
16695 case DW_OP_lit7:
16696 case DW_OP_lit8:
16697 case DW_OP_lit9:
16698 case DW_OP_lit10:
16699 case DW_OP_lit11:
16700 case DW_OP_lit12:
16701 case DW_OP_lit13:
16702 case DW_OP_lit14:
16703 case DW_OP_lit15:
16704 case DW_OP_lit16:
16705 case DW_OP_lit17:
16706 case DW_OP_lit18:
16707 case DW_OP_lit19:
16708 case DW_OP_lit20:
16709 case DW_OP_lit21:
16710 case DW_OP_lit22:
16711 case DW_OP_lit23:
16712 case DW_OP_lit24:
16713 case DW_OP_lit25:
16714 case DW_OP_lit26:
16715 case DW_OP_lit27:
16716 case DW_OP_lit28:
16717 case DW_OP_lit29:
16718 case DW_OP_lit30:
16719 case DW_OP_lit31:
16720 case DW_OP_breg0:
16721 case DW_OP_breg1:
16722 case DW_OP_breg2:
16723 case DW_OP_breg3:
16724 case DW_OP_breg4:
16725 case DW_OP_breg5:
16726 case DW_OP_breg6:
16727 case DW_OP_breg7:
16728 case DW_OP_breg8:
16729 case DW_OP_breg9:
16730 case DW_OP_breg10:
16731 case DW_OP_breg11:
16732 case DW_OP_breg12:
16733 case DW_OP_breg13:
16734 case DW_OP_breg14:
16735 case DW_OP_breg15:
16736 case DW_OP_breg16:
16737 case DW_OP_breg17:
16738 case DW_OP_breg18:
16739 case DW_OP_breg19:
16740 case DW_OP_breg20:
16741 case DW_OP_breg21:
16742 case DW_OP_breg22:
16743 case DW_OP_breg23:
16744 case DW_OP_breg24:
16745 case DW_OP_breg25:
16746 case DW_OP_breg26:
16747 case DW_OP_breg27:
16748 case DW_OP_breg28:
16749 case DW_OP_breg29:
16750 case DW_OP_breg30:
16751 case DW_OP_breg31:
16752 case DW_OP_fbreg:
16753 case DW_OP_push_object_address:
16754 case DW_OP_call_frame_cfa:
16755 ++frame_offset_;
16756 break;
16758 case DW_OP_drop:
16759 case DW_OP_xderef:
16760 case DW_OP_and:
16761 case DW_OP_div:
16762 case DW_OP_minus:
16763 case DW_OP_mod:
16764 case DW_OP_mul:
16765 case DW_OP_or:
16766 case DW_OP_plus:
16767 case DW_OP_shl:
16768 case DW_OP_shr:
16769 case DW_OP_shra:
16770 case DW_OP_xor:
16771 case DW_OP_bra:
16772 case DW_OP_eq:
16773 case DW_OP_ge:
16774 case DW_OP_gt:
16775 case DW_OP_le:
16776 case DW_OP_lt:
16777 case DW_OP_ne:
16778 case DW_OP_regx:
16779 case DW_OP_xderef_size:
16780 --frame_offset_;
16781 break;
16783 case DW_OP_call2:
16784 case DW_OP_call4:
16785 case DW_OP_call_ref:
16787 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16788 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16790 if (stack_usage == NULL)
16791 return false;
16792 frame_offset_ += *stack_usage;
16793 break;
16796 case DW_OP_implicit_pointer:
16797 case DW_OP_entry_value:
16798 case DW_OP_const_type:
16799 case DW_OP_regval_type:
16800 case DW_OP_deref_type:
16801 case DW_OP_convert:
16802 case DW_OP_reinterpret:
16803 case DW_OP_form_tls_address:
16804 case DW_OP_GNU_push_tls_address:
16805 case DW_OP_GNU_uninit:
16806 case DW_OP_GNU_encoded_addr:
16807 case DW_OP_GNU_implicit_pointer:
16808 case DW_OP_GNU_entry_value:
16809 case DW_OP_GNU_const_type:
16810 case DW_OP_GNU_regval_type:
16811 case DW_OP_GNU_deref_type:
16812 case DW_OP_GNU_convert:
16813 case DW_OP_GNU_reinterpret:
16814 case DW_OP_GNU_parameter_ref:
16815 /* loc_list_from_tree will probably not output these operations for
16816 size functions, so assume they will not appear here. */
16817 /* Fall through... */
16819 default:
16820 gcc_unreachable ();
16823 /* Now, follow the control flow (except subroutine calls). */
16824 switch (l->dw_loc_opc)
16826 case DW_OP_bra:
16827 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16828 frame_offsets))
16829 return false;
16830 /* Fall through. */
16832 case DW_OP_skip:
16833 l = l->dw_loc_oprnd1.v.val_loc;
16834 break;
16836 case DW_OP_stack_value:
16837 return true;
16839 default:
16840 l = l->dw_loc_next;
16841 break;
16845 return true;
16848 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16849 operations) in order to resolve the operand of DW_OP_pick operations that
16850 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16851 offset *before* LOC is executed. Return if all relocations were
16852 successful. */
16854 static bool
16855 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16856 struct dwarf_procedure_info *dpi)
16858 /* Associate to all visited operations the frame offset *before* evaluating
16859 this operation. */
16860 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16862 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16863 frame_offsets);
16866 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
16867 Return NULL if it is not possible. */
16869 static dw_die_ref
16870 function_to_dwarf_procedure (tree fndecl)
16872 struct loc_descr_context ctx;
16873 struct dwarf_procedure_info dpi;
16874 dw_die_ref dwarf_proc_die;
16875 tree tree_body = DECL_SAVED_TREE (fndecl);
16876 dw_loc_descr_ref loc_body, epilogue;
16878 tree cursor;
16879 unsigned i;
16881 /* Do not generate multiple DWARF procedures for the same function
16882 declaration. */
16883 dwarf_proc_die = lookup_decl_die (fndecl);
16884 if (dwarf_proc_die != NULL)
16885 return dwarf_proc_die;
16887 /* DWARF procedures are available starting with the DWARFv3 standard. */
16888 if (dwarf_version < 3 && dwarf_strict)
16889 return NULL;
16891 /* We handle only functions for which we still have a body, that return a
16892 supported type and that takes arguments with supported types. Note that
16893 there is no point translating functions that return nothing. */
16894 if (tree_body == NULL_TREE
16895 || DECL_RESULT (fndecl) == NULL_TREE
16896 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
16897 return NULL;
16899 for (cursor = DECL_ARGUMENTS (fndecl);
16900 cursor != NULL_TREE;
16901 cursor = TREE_CHAIN (cursor))
16902 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
16903 return NULL;
16905 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
16906 if (TREE_CODE (tree_body) != RETURN_EXPR)
16907 return NULL;
16908 tree_body = TREE_OPERAND (tree_body, 0);
16909 if (TREE_CODE (tree_body) != MODIFY_EXPR
16910 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
16911 return NULL;
16912 tree_body = TREE_OPERAND (tree_body, 1);
16914 /* Try to translate the body expression itself. Note that this will probably
16915 cause an infinite recursion if its call graph has a cycle. This is very
16916 unlikely for size functions, however, so don't bother with such things at
16917 the moment. */
16918 ctx.context_type = NULL_TREE;
16919 ctx.base_decl = NULL_TREE;
16920 ctx.dpi = &dpi;
16921 ctx.placeholder_arg = false;
16922 ctx.placeholder_seen = false;
16923 dpi.fndecl = fndecl;
16924 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
16925 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
16926 if (!loc_body)
16927 return NULL;
16929 /* After evaluating all operands in "loc_body", we should still have on the
16930 stack all arguments plus the desired function result (top of the stack).
16931 Generate code in order to keep only the result in our stack frame. */
16932 epilogue = NULL;
16933 for (i = 0; i < dpi.args_count; ++i)
16935 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
16936 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
16937 op_couple->dw_loc_next->dw_loc_next = epilogue;
16938 epilogue = op_couple;
16940 add_loc_descr (&loc_body, epilogue);
16941 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
16942 return NULL;
16944 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
16945 because they are considered useful. Now there is an epilogue, they are
16946 not anymore, so give it another try. */
16947 loc_descr_without_nops (loc_body);
16949 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
16950 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
16951 though, given that size functions do not come from source, so they should
16952 not have a dedicated DW_TAG_subprogram DIE. */
16953 dwarf_proc_die
16954 = new_dwarf_proc_die (loc_body, fndecl,
16955 get_context_die (DECL_CONTEXT (fndecl)));
16957 /* The called DWARF procedure consumes one stack slot per argument and
16958 returns one stack slot. */
16959 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
16961 return dwarf_proc_die;
16965 /* Generate Dwarf location list representing LOC.
16966 If WANT_ADDRESS is false, expression computing LOC will be computed
16967 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
16968 if WANT_ADDRESS is 2, expression computing address useable in location
16969 will be returned (i.e. DW_OP_reg can be used
16970 to refer to register values).
16972 CONTEXT provides information to customize the location descriptions
16973 generation. Its context_type field specifies what type is implicitly
16974 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
16975 will not be generated.
16977 Its DPI field determines whether we are generating a DWARF expression for a
16978 DWARF procedure, so PARM_DECL references are processed specifically.
16980 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
16981 and dpi fields were null. */
16983 static dw_loc_list_ref
16984 loc_list_from_tree_1 (tree loc, int want_address,
16985 struct loc_descr_context *context)
16987 dw_loc_descr_ref ret = NULL, ret1 = NULL;
16988 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16989 int have_address = 0;
16990 enum dwarf_location_atom op;
16992 /* ??? Most of the time we do not take proper care for sign/zero
16993 extending the values properly. Hopefully this won't be a real
16994 problem... */
16996 if (context != NULL
16997 && context->base_decl == loc
16998 && want_address == 0)
17000 if (dwarf_version >= 3 || !dwarf_strict)
17001 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17002 NULL, NULL, NULL);
17003 else
17004 return NULL;
17007 switch (TREE_CODE (loc))
17009 case ERROR_MARK:
17010 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17011 return 0;
17013 case PLACEHOLDER_EXPR:
17014 /* This case involves extracting fields from an object to determine the
17015 position of other fields. It is supposed to appear only as the first
17016 operand of COMPONENT_REF nodes and to reference precisely the type
17017 that the context allows. */
17018 if (context != NULL
17019 && TREE_TYPE (loc) == context->context_type
17020 && want_address >= 1)
17022 if (dwarf_version >= 3 || !dwarf_strict)
17024 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17025 have_address = 1;
17026 break;
17028 else
17029 return NULL;
17031 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17032 the single argument passed by consumer. */
17033 else if (context != NULL
17034 && context->placeholder_arg
17035 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17036 && want_address == 0)
17038 ret = new_loc_descr (DW_OP_pick, 0, 0);
17039 ret->frame_offset_rel = 1;
17040 context->placeholder_seen = true;
17041 break;
17043 else
17044 expansion_failed (loc, NULL_RTX,
17045 "PLACEHOLDER_EXPR for an unexpected type");
17046 break;
17048 case CALL_EXPR:
17050 const int nargs = call_expr_nargs (loc);
17051 tree callee = get_callee_fndecl (loc);
17052 int i;
17053 dw_die_ref dwarf_proc;
17055 if (callee == NULL_TREE)
17056 goto call_expansion_failed;
17058 /* We handle only functions that return an integer. */
17059 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17060 goto call_expansion_failed;
17062 dwarf_proc = function_to_dwarf_procedure (callee);
17063 if (dwarf_proc == NULL)
17064 goto call_expansion_failed;
17066 /* Evaluate arguments right-to-left so that the first argument will
17067 be the top-most one on the stack. */
17068 for (i = nargs - 1; i >= 0; --i)
17070 dw_loc_descr_ref loc_descr
17071 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17072 context);
17074 if (loc_descr == NULL)
17075 goto call_expansion_failed;
17077 add_loc_descr (&ret, loc_descr);
17080 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17081 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17082 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17083 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17084 add_loc_descr (&ret, ret1);
17085 break;
17087 call_expansion_failed:
17088 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17089 /* There are no opcodes for these operations. */
17090 return 0;
17093 case PREINCREMENT_EXPR:
17094 case PREDECREMENT_EXPR:
17095 case POSTINCREMENT_EXPR:
17096 case POSTDECREMENT_EXPR:
17097 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17098 /* There are no opcodes for these operations. */
17099 return 0;
17101 case ADDR_EXPR:
17102 /* If we already want an address, see if there is INDIRECT_REF inside
17103 e.g. for &this->field. */
17104 if (want_address)
17106 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17107 (loc, want_address == 2, context);
17108 if (list_ret)
17109 have_address = 1;
17110 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17111 && (ret = cst_pool_loc_descr (loc)))
17112 have_address = 1;
17114 /* Otherwise, process the argument and look for the address. */
17115 if (!list_ret && !ret)
17116 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17117 else
17119 if (want_address)
17120 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17121 return NULL;
17123 break;
17125 case VAR_DECL:
17126 if (DECL_THREAD_LOCAL_P (loc))
17128 rtx rtl;
17129 enum dwarf_location_atom tls_op;
17130 enum dtprel_bool dtprel = dtprel_false;
17132 if (targetm.have_tls)
17134 /* If this is not defined, we have no way to emit the
17135 data. */
17136 if (!targetm.asm_out.output_dwarf_dtprel)
17137 return 0;
17139 /* The way DW_OP_GNU_push_tls_address is specified, we
17140 can only look up addresses of objects in the current
17141 module. We used DW_OP_addr as first op, but that's
17142 wrong, because DW_OP_addr is relocated by the debug
17143 info consumer, while DW_OP_GNU_push_tls_address
17144 operand shouldn't be. */
17145 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17146 return 0;
17147 dtprel = dtprel_true;
17148 /* We check for DWARF 5 here because gdb did not implement
17149 DW_OP_form_tls_address until after 7.12. */
17150 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17151 : DW_OP_GNU_push_tls_address);
17153 else
17155 if (!targetm.emutls.debug_form_tls_address
17156 || !(dwarf_version >= 3 || !dwarf_strict))
17157 return 0;
17158 /* We stuffed the control variable into the DECL_VALUE_EXPR
17159 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17160 no longer appear in gimple code. We used the control
17161 variable in specific so that we could pick it up here. */
17162 loc = DECL_VALUE_EXPR (loc);
17163 tls_op = DW_OP_form_tls_address;
17166 rtl = rtl_for_decl_location (loc);
17167 if (rtl == NULL_RTX)
17168 return 0;
17170 if (!MEM_P (rtl))
17171 return 0;
17172 rtl = XEXP (rtl, 0);
17173 if (! CONSTANT_P (rtl))
17174 return 0;
17176 ret = new_addr_loc_descr (rtl, dtprel);
17177 ret1 = new_loc_descr (tls_op, 0, 0);
17178 add_loc_descr (&ret, ret1);
17180 have_address = 1;
17181 break;
17183 /* FALLTHRU */
17185 case PARM_DECL:
17186 if (context != NULL && context->dpi != NULL
17187 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17189 /* We are generating code for a DWARF procedure and we want to access
17190 one of its arguments: find the appropriate argument offset and let
17191 the resolve_args_picking pass compute the offset that complies
17192 with the stack frame size. */
17193 unsigned i = 0;
17194 tree cursor;
17196 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17197 cursor != NULL_TREE && cursor != loc;
17198 cursor = TREE_CHAIN (cursor), ++i)
17200 /* If we are translating a DWARF procedure, all referenced parameters
17201 must belong to the current function. */
17202 gcc_assert (cursor != NULL_TREE);
17204 ret = new_loc_descr (DW_OP_pick, i, 0);
17205 ret->frame_offset_rel = 1;
17206 break;
17208 /* FALLTHRU */
17210 case RESULT_DECL:
17211 if (DECL_HAS_VALUE_EXPR_P (loc))
17212 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17213 want_address, context);
17214 /* FALLTHRU */
17216 case FUNCTION_DECL:
17218 rtx rtl;
17219 var_loc_list *loc_list = lookup_decl_loc (loc);
17221 if (loc_list && loc_list->first)
17223 list_ret = dw_loc_list (loc_list, loc, want_address);
17224 have_address = want_address != 0;
17225 break;
17227 rtl = rtl_for_decl_location (loc);
17228 if (rtl == NULL_RTX)
17230 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17231 return 0;
17233 else if (CONST_INT_P (rtl))
17235 HOST_WIDE_INT val = INTVAL (rtl);
17236 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17237 val &= GET_MODE_MASK (DECL_MODE (loc));
17238 ret = int_loc_descriptor (val);
17240 else if (GET_CODE (rtl) == CONST_STRING)
17242 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17243 return 0;
17245 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17246 ret = new_addr_loc_descr (rtl, dtprel_false);
17247 else
17249 machine_mode mode, mem_mode;
17251 /* Certain constructs can only be represented at top-level. */
17252 if (want_address == 2)
17254 ret = loc_descriptor (rtl, VOIDmode,
17255 VAR_INIT_STATUS_INITIALIZED);
17256 have_address = 1;
17258 else
17260 mode = GET_MODE (rtl);
17261 mem_mode = VOIDmode;
17262 if (MEM_P (rtl))
17264 mem_mode = mode;
17265 mode = get_address_mode (rtl);
17266 rtl = XEXP (rtl, 0);
17267 have_address = 1;
17269 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17270 VAR_INIT_STATUS_INITIALIZED);
17272 if (!ret)
17273 expansion_failed (loc, rtl,
17274 "failed to produce loc descriptor for rtl");
17277 break;
17279 case MEM_REF:
17280 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17282 have_address = 1;
17283 goto do_plus;
17285 /* Fallthru. */
17286 case INDIRECT_REF:
17287 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17288 have_address = 1;
17289 break;
17291 case TARGET_MEM_REF:
17292 case SSA_NAME:
17293 case DEBUG_EXPR_DECL:
17294 return NULL;
17296 case COMPOUND_EXPR:
17297 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17298 context);
17300 CASE_CONVERT:
17301 case VIEW_CONVERT_EXPR:
17302 case SAVE_EXPR:
17303 case MODIFY_EXPR:
17304 case NON_LVALUE_EXPR:
17305 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17306 context);
17308 case COMPONENT_REF:
17309 case BIT_FIELD_REF:
17310 case ARRAY_REF:
17311 case ARRAY_RANGE_REF:
17312 case REALPART_EXPR:
17313 case IMAGPART_EXPR:
17315 tree obj, offset;
17316 HOST_WIDE_INT bitsize, bitpos, bytepos;
17317 machine_mode mode;
17318 int unsignedp, reversep, volatilep = 0;
17320 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17321 &unsignedp, &reversep, &volatilep);
17323 gcc_assert (obj != loc);
17325 list_ret = loc_list_from_tree_1 (obj,
17326 want_address == 2
17327 && !bitpos && !offset ? 2 : 1,
17328 context);
17329 /* TODO: We can extract value of the small expression via shifting even
17330 for nonzero bitpos. */
17331 if (list_ret == 0)
17332 return 0;
17333 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17335 expansion_failed (loc, NULL_RTX,
17336 "bitfield access");
17337 return 0;
17340 if (offset != NULL_TREE)
17342 /* Variable offset. */
17343 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17344 if (list_ret1 == 0)
17345 return 0;
17346 add_loc_list (&list_ret, list_ret1);
17347 if (!list_ret)
17348 return 0;
17349 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17352 bytepos = bitpos / BITS_PER_UNIT;
17353 if (bytepos > 0)
17354 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17355 else if (bytepos < 0)
17356 loc_list_plus_const (list_ret, bytepos);
17358 have_address = 1;
17359 break;
17362 case INTEGER_CST:
17363 if ((want_address || !tree_fits_shwi_p (loc))
17364 && (ret = cst_pool_loc_descr (loc)))
17365 have_address = 1;
17366 else if (want_address == 2
17367 && tree_fits_shwi_p (loc)
17368 && (ret = address_of_int_loc_descriptor
17369 (int_size_in_bytes (TREE_TYPE (loc)),
17370 tree_to_shwi (loc))))
17371 have_address = 1;
17372 else if (tree_fits_shwi_p (loc))
17373 ret = int_loc_descriptor (tree_to_shwi (loc));
17374 else if (tree_fits_uhwi_p (loc))
17375 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17376 else
17378 expansion_failed (loc, NULL_RTX,
17379 "Integer operand is not host integer");
17380 return 0;
17382 break;
17384 case CONSTRUCTOR:
17385 case REAL_CST:
17386 case STRING_CST:
17387 case COMPLEX_CST:
17388 if ((ret = cst_pool_loc_descr (loc)))
17389 have_address = 1;
17390 else if (TREE_CODE (loc) == CONSTRUCTOR)
17392 tree type = TREE_TYPE (loc);
17393 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17394 unsigned HOST_WIDE_INT offset = 0;
17395 unsigned HOST_WIDE_INT cnt;
17396 constructor_elt *ce;
17398 if (TREE_CODE (type) == RECORD_TYPE)
17400 /* This is very limited, but it's enough to output
17401 pointers to member functions, as long as the
17402 referenced function is defined in the current
17403 translation unit. */
17404 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17406 tree val = ce->value;
17408 tree field = ce->index;
17410 if (val)
17411 STRIP_NOPS (val);
17413 if (!field || DECL_BIT_FIELD (field))
17415 expansion_failed (loc, NULL_RTX,
17416 "bitfield in record type constructor");
17417 size = offset = (unsigned HOST_WIDE_INT)-1;
17418 ret = NULL;
17419 break;
17422 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17423 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17424 gcc_assert (pos + fieldsize <= size);
17425 if (pos < offset)
17427 expansion_failed (loc, NULL_RTX,
17428 "out-of-order fields in record constructor");
17429 size = offset = (unsigned HOST_WIDE_INT)-1;
17430 ret = NULL;
17431 break;
17433 if (pos > offset)
17435 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17436 add_loc_descr (&ret, ret1);
17437 offset = pos;
17439 if (val && fieldsize != 0)
17441 ret1 = loc_descriptor_from_tree (val, want_address, context);
17442 if (!ret1)
17444 expansion_failed (loc, NULL_RTX,
17445 "unsupported expression in field");
17446 size = offset = (unsigned HOST_WIDE_INT)-1;
17447 ret = NULL;
17448 break;
17450 add_loc_descr (&ret, ret1);
17452 if (fieldsize)
17454 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17455 add_loc_descr (&ret, ret1);
17456 offset = pos + fieldsize;
17460 if (offset != size)
17462 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17463 add_loc_descr (&ret, ret1);
17464 offset = size;
17467 have_address = !!want_address;
17469 else
17470 expansion_failed (loc, NULL_RTX,
17471 "constructor of non-record type");
17473 else
17474 /* We can construct small constants here using int_loc_descriptor. */
17475 expansion_failed (loc, NULL_RTX,
17476 "constructor or constant not in constant pool");
17477 break;
17479 case TRUTH_AND_EXPR:
17480 case TRUTH_ANDIF_EXPR:
17481 case BIT_AND_EXPR:
17482 op = DW_OP_and;
17483 goto do_binop;
17485 case TRUTH_XOR_EXPR:
17486 case BIT_XOR_EXPR:
17487 op = DW_OP_xor;
17488 goto do_binop;
17490 case TRUTH_OR_EXPR:
17491 case TRUTH_ORIF_EXPR:
17492 case BIT_IOR_EXPR:
17493 op = DW_OP_or;
17494 goto do_binop;
17496 case FLOOR_DIV_EXPR:
17497 case CEIL_DIV_EXPR:
17498 case ROUND_DIV_EXPR:
17499 case TRUNC_DIV_EXPR:
17500 case EXACT_DIV_EXPR:
17501 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17502 return 0;
17503 op = DW_OP_div;
17504 goto do_binop;
17506 case MINUS_EXPR:
17507 op = DW_OP_minus;
17508 goto do_binop;
17510 case FLOOR_MOD_EXPR:
17511 case CEIL_MOD_EXPR:
17512 case ROUND_MOD_EXPR:
17513 case TRUNC_MOD_EXPR:
17514 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17516 op = DW_OP_mod;
17517 goto do_binop;
17519 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17520 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17521 if (list_ret == 0 || list_ret1 == 0)
17522 return 0;
17524 add_loc_list (&list_ret, list_ret1);
17525 if (list_ret == 0)
17526 return 0;
17527 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17528 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17529 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17530 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17531 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17532 break;
17534 case MULT_EXPR:
17535 op = DW_OP_mul;
17536 goto do_binop;
17538 case LSHIFT_EXPR:
17539 op = DW_OP_shl;
17540 goto do_binop;
17542 case RSHIFT_EXPR:
17543 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17544 goto do_binop;
17546 case POINTER_PLUS_EXPR:
17547 case PLUS_EXPR:
17548 do_plus:
17549 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17551 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17552 smarter to encode their opposite. The DW_OP_plus_uconst operation
17553 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17554 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17555 bytes, Y being the size of the operation that pushes the opposite
17556 of the addend. So let's choose the smallest representation. */
17557 const tree tree_addend = TREE_OPERAND (loc, 1);
17558 offset_int wi_addend;
17559 HOST_WIDE_INT shwi_addend;
17560 dw_loc_descr_ref loc_naddend;
17562 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17563 if (list_ret == 0)
17564 return 0;
17566 /* Try to get the literal to push. It is the opposite of the addend,
17567 so as we rely on wrapping during DWARF evaluation, first decode
17568 the literal as a "DWARF-sized" signed number. */
17569 wi_addend = wi::to_offset (tree_addend);
17570 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17571 shwi_addend = wi_addend.to_shwi ();
17572 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17573 ? int_loc_descriptor (-shwi_addend)
17574 : NULL;
17576 if (loc_naddend != NULL
17577 && ((unsigned) size_of_uleb128 (shwi_addend)
17578 > size_of_loc_descr (loc_naddend)))
17580 add_loc_descr_to_each (list_ret, loc_naddend);
17581 add_loc_descr_to_each (list_ret,
17582 new_loc_descr (DW_OP_minus, 0, 0));
17584 else
17586 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17588 loc_naddend = loc_cur;
17589 loc_cur = loc_cur->dw_loc_next;
17590 ggc_free (loc_naddend);
17592 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17594 break;
17597 op = DW_OP_plus;
17598 goto do_binop;
17600 case LE_EXPR:
17601 op = DW_OP_le;
17602 goto do_comp_binop;
17604 case GE_EXPR:
17605 op = DW_OP_ge;
17606 goto do_comp_binop;
17608 case LT_EXPR:
17609 op = DW_OP_lt;
17610 goto do_comp_binop;
17612 case GT_EXPR:
17613 op = DW_OP_gt;
17614 goto do_comp_binop;
17616 do_comp_binop:
17617 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17619 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17620 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17621 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17622 TREE_CODE (loc));
17623 break;
17625 else
17626 goto do_binop;
17628 case EQ_EXPR:
17629 op = DW_OP_eq;
17630 goto do_binop;
17632 case NE_EXPR:
17633 op = DW_OP_ne;
17634 goto do_binop;
17636 do_binop:
17637 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17638 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17639 if (list_ret == 0 || list_ret1 == 0)
17640 return 0;
17642 add_loc_list (&list_ret, list_ret1);
17643 if (list_ret == 0)
17644 return 0;
17645 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17646 break;
17648 case TRUTH_NOT_EXPR:
17649 case BIT_NOT_EXPR:
17650 op = DW_OP_not;
17651 goto do_unop;
17653 case ABS_EXPR:
17654 op = DW_OP_abs;
17655 goto do_unop;
17657 case NEGATE_EXPR:
17658 op = DW_OP_neg;
17659 goto do_unop;
17661 do_unop:
17662 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17663 if (list_ret == 0)
17664 return 0;
17666 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17667 break;
17669 case MIN_EXPR:
17670 case MAX_EXPR:
17672 const enum tree_code code =
17673 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17675 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17676 build2 (code, integer_type_node,
17677 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17678 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17681 /* fall through */
17683 case COND_EXPR:
17685 dw_loc_descr_ref lhs
17686 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17687 dw_loc_list_ref rhs
17688 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17689 dw_loc_descr_ref bra_node, jump_node, tmp;
17691 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17692 if (list_ret == 0 || lhs == 0 || rhs == 0)
17693 return 0;
17695 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17696 add_loc_descr_to_each (list_ret, bra_node);
17698 add_loc_list (&list_ret, rhs);
17699 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17700 add_loc_descr_to_each (list_ret, jump_node);
17702 add_loc_descr_to_each (list_ret, lhs);
17703 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17704 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17706 /* ??? Need a node to point the skip at. Use a nop. */
17707 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17708 add_loc_descr_to_each (list_ret, tmp);
17709 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17710 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17712 break;
17714 case FIX_TRUNC_EXPR:
17715 return 0;
17717 default:
17718 /* Leave front-end specific codes as simply unknown. This comes
17719 up, for instance, with the C STMT_EXPR. */
17720 if ((unsigned int) TREE_CODE (loc)
17721 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17723 expansion_failed (loc, NULL_RTX,
17724 "language specific tree node");
17725 return 0;
17728 /* Otherwise this is a generic code; we should just lists all of
17729 these explicitly. We forgot one. */
17730 if (flag_checking)
17731 gcc_unreachable ();
17733 /* In a release build, we want to degrade gracefully: better to
17734 generate incomplete debugging information than to crash. */
17735 return NULL;
17738 if (!ret && !list_ret)
17739 return 0;
17741 if (want_address == 2 && !have_address
17742 && (dwarf_version >= 4 || !dwarf_strict))
17744 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17746 expansion_failed (loc, NULL_RTX,
17747 "DWARF address size mismatch");
17748 return 0;
17750 if (ret)
17751 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17752 else
17753 add_loc_descr_to_each (list_ret,
17754 new_loc_descr (DW_OP_stack_value, 0, 0));
17755 have_address = 1;
17757 /* Show if we can't fill the request for an address. */
17758 if (want_address && !have_address)
17760 expansion_failed (loc, NULL_RTX,
17761 "Want address and only have value");
17762 return 0;
17765 gcc_assert (!ret || !list_ret);
17767 /* If we've got an address and don't want one, dereference. */
17768 if (!want_address && have_address)
17770 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17772 if (size > DWARF2_ADDR_SIZE || size == -1)
17774 expansion_failed (loc, NULL_RTX,
17775 "DWARF address size mismatch");
17776 return 0;
17778 else if (size == DWARF2_ADDR_SIZE)
17779 op = DW_OP_deref;
17780 else
17781 op = DW_OP_deref_size;
17783 if (ret)
17784 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17785 else
17786 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17788 if (ret)
17789 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17791 return list_ret;
17794 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17795 expressions. */
17797 static dw_loc_list_ref
17798 loc_list_from_tree (tree loc, int want_address,
17799 struct loc_descr_context *context)
17801 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17803 for (dw_loc_list_ref loc_cur = result;
17804 loc_cur != NULL; loc_cur =
17805 loc_cur->dw_loc_next)
17806 loc_descr_without_nops (loc_cur->expr);
17807 return result;
17810 /* Same as above but return only single location expression. */
17811 static dw_loc_descr_ref
17812 loc_descriptor_from_tree (tree loc, int want_address,
17813 struct loc_descr_context *context)
17815 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17816 if (!ret)
17817 return NULL;
17818 if (ret->dw_loc_next)
17820 expansion_failed (loc, NULL_RTX,
17821 "Location list where only loc descriptor needed");
17822 return NULL;
17824 return ret->expr;
17827 /* Given a value, round it up to the lowest multiple of `boundary'
17828 which is not less than the value itself. */
17830 static inline HOST_WIDE_INT
17831 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17833 return (((value + boundary - 1) / boundary) * boundary);
17836 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17837 pointer to the declared type for the relevant field variable, or return
17838 `integer_type_node' if the given node turns out to be an
17839 ERROR_MARK node. */
17841 static inline tree
17842 field_type (const_tree decl)
17844 tree type;
17846 if (TREE_CODE (decl) == ERROR_MARK)
17847 return integer_type_node;
17849 type = DECL_BIT_FIELD_TYPE (decl);
17850 if (type == NULL_TREE)
17851 type = TREE_TYPE (decl);
17853 return type;
17856 /* Given a pointer to a tree node, return the alignment in bits for
17857 it, or else return BITS_PER_WORD if the node actually turns out to
17858 be an ERROR_MARK node. */
17860 static inline unsigned
17861 simple_type_align_in_bits (const_tree type)
17863 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
17866 static inline unsigned
17867 simple_decl_align_in_bits (const_tree decl)
17869 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
17872 /* Return the result of rounding T up to ALIGN. */
17874 static inline offset_int
17875 round_up_to_align (const offset_int &t, unsigned int align)
17877 return wi::udiv_trunc (t + align - 1, align) * align;
17880 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
17881 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
17882 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
17883 if we fail to return the size in one of these two forms. */
17885 static dw_loc_descr_ref
17886 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
17888 tree tree_size;
17889 struct loc_descr_context ctx;
17891 /* Return a constant integer in priority, if possible. */
17892 *cst_size = int_size_in_bytes (type);
17893 if (*cst_size != -1)
17894 return NULL;
17896 ctx.context_type = const_cast<tree> (type);
17897 ctx.base_decl = NULL_TREE;
17898 ctx.dpi = NULL;
17899 ctx.placeholder_arg = false;
17900 ctx.placeholder_seen = false;
17902 type = TYPE_MAIN_VARIANT (type);
17903 tree_size = TYPE_SIZE_UNIT (type);
17904 return ((tree_size != NULL_TREE)
17905 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
17906 : NULL);
17909 /* Helper structure for RECORD_TYPE processing. */
17910 struct vlr_context
17912 /* Root RECORD_TYPE. It is needed to generate data member location
17913 descriptions in variable-length records (VLR), but also to cope with
17914 variants, which are composed of nested structures multiplexed with
17915 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
17916 function processing a FIELD_DECL, it is required to be non null. */
17917 tree struct_type;
17918 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
17919 QUAL_UNION_TYPE), this holds an expression that computes the offset for
17920 this variant part as part of the root record (in storage units). For
17921 regular records, it must be NULL_TREE. */
17922 tree variant_part_offset;
17925 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
17926 addressed byte of the "containing object" for the given FIELD_DECL. If
17927 possible, return a native constant through CST_OFFSET (in which case NULL is
17928 returned); otherwise return a DWARF expression that computes the offset.
17930 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
17931 that offset is, either because the argument turns out to be a pointer to an
17932 ERROR_MARK node, or because the offset expression is too complex for us.
17934 CTX is required: see the comment for VLR_CONTEXT. */
17936 static dw_loc_descr_ref
17937 field_byte_offset (const_tree decl, struct vlr_context *ctx,
17938 HOST_WIDE_INT *cst_offset)
17940 offset_int object_offset_in_bits;
17941 offset_int object_offset_in_bytes;
17942 offset_int bitpos_int;
17943 bool is_byte_offset_cst, is_bit_offset_cst;
17944 tree tree_result;
17945 dw_loc_list_ref loc_result;
17947 *cst_offset = 0;
17949 if (TREE_CODE (decl) == ERROR_MARK)
17950 return NULL;
17951 else
17952 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
17954 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
17955 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
17957 /* We cannot handle variable bit offsets at the moment, so abort if it's the
17958 case. */
17959 if (is_bit_offset_cst)
17960 return NULL;
17962 #ifdef PCC_BITFIELD_TYPE_MATTERS
17963 /* We used to handle only constant offsets in all cases. Now, we handle
17964 properly dynamic byte offsets only when PCC bitfield type doesn't
17965 matter. */
17966 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
17968 tree type;
17969 tree field_size_tree;
17970 offset_int deepest_bitpos;
17971 offset_int field_size_in_bits;
17972 unsigned int type_align_in_bits;
17973 unsigned int decl_align_in_bits;
17974 offset_int type_size_in_bits;
17976 bitpos_int = wi::to_offset (bit_position (decl));
17977 type = field_type (decl);
17978 type_size_in_bits = offset_int_type_size_in_bits (type);
17979 type_align_in_bits = simple_type_align_in_bits (type);
17981 field_size_tree = DECL_SIZE (decl);
17983 /* The size could be unspecified if there was an error, or for
17984 a flexible array member. */
17985 if (!field_size_tree)
17986 field_size_tree = bitsize_zero_node;
17988 /* If the size of the field is not constant, use the type size. */
17989 if (TREE_CODE (field_size_tree) == INTEGER_CST)
17990 field_size_in_bits = wi::to_offset (field_size_tree);
17991 else
17992 field_size_in_bits = type_size_in_bits;
17994 decl_align_in_bits = simple_decl_align_in_bits (decl);
17996 /* The GCC front-end doesn't make any attempt to keep track of the
17997 starting bit offset (relative to the start of the containing
17998 structure type) of the hypothetical "containing object" for a
17999 bit-field. Thus, when computing the byte offset value for the
18000 start of the "containing object" of a bit-field, we must deduce
18001 this information on our own. This can be rather tricky to do in
18002 some cases. For example, handling the following structure type
18003 definition when compiling for an i386/i486 target (which only
18004 aligns long long's to 32-bit boundaries) can be very tricky:
18006 struct S { int field1; long long field2:31; };
18008 Fortunately, there is a simple rule-of-thumb which can be used
18009 in such cases. When compiling for an i386/i486, GCC will
18010 allocate 8 bytes for the structure shown above. It decides to
18011 do this based upon one simple rule for bit-field allocation.
18012 GCC allocates each "containing object" for each bit-field at
18013 the first (i.e. lowest addressed) legitimate alignment boundary
18014 (based upon the required minimum alignment for the declared
18015 type of the field) which it can possibly use, subject to the
18016 condition that there is still enough available space remaining
18017 in the containing object (when allocated at the selected point)
18018 to fully accommodate all of the bits of the bit-field itself.
18020 This simple rule makes it obvious why GCC allocates 8 bytes for
18021 each object of the structure type shown above. When looking
18022 for a place to allocate the "containing object" for `field2',
18023 the compiler simply tries to allocate a 64-bit "containing
18024 object" at each successive 32-bit boundary (starting at zero)
18025 until it finds a place to allocate that 64- bit field such that
18026 at least 31 contiguous (and previously unallocated) bits remain
18027 within that selected 64 bit field. (As it turns out, for the
18028 example above, the compiler finds it is OK to allocate the
18029 "containing object" 64-bit field at bit-offset zero within the
18030 structure type.)
18032 Here we attempt to work backwards from the limited set of facts
18033 we're given, and we try to deduce from those facts, where GCC
18034 must have believed that the containing object started (within
18035 the structure type). The value we deduce is then used (by the
18036 callers of this routine) to generate DW_AT_location and
18037 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18038 the case of DW_AT_location, regular fields as well). */
18040 /* Figure out the bit-distance from the start of the structure to
18041 the "deepest" bit of the bit-field. */
18042 deepest_bitpos = bitpos_int + field_size_in_bits;
18044 /* This is the tricky part. Use some fancy footwork to deduce
18045 where the lowest addressed bit of the containing object must
18046 be. */
18047 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18049 /* Round up to type_align by default. This works best for
18050 bitfields. */
18051 object_offset_in_bits
18052 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18054 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18056 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18058 /* Round up to decl_align instead. */
18059 object_offset_in_bits
18060 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18063 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18065 tree_result = byte_position (decl);
18066 if (ctx->variant_part_offset != NULL_TREE)
18067 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18068 ctx->variant_part_offset, tree_result));
18070 /* If the byte offset is a constant, it's simplier to handle a native
18071 constant rather than a DWARF expression. */
18072 if (TREE_CODE (tree_result) == INTEGER_CST)
18074 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18075 return NULL;
18077 struct loc_descr_context loc_ctx = {
18078 ctx->struct_type, /* context_type */
18079 NULL_TREE, /* base_decl */
18080 NULL, /* dpi */
18081 false, /* placeholder_arg */
18082 false /* placeholder_seen */
18084 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18086 /* We want a DWARF expression: abort if we only have a location list with
18087 multiple elements. */
18088 if (!loc_result || !single_element_loc_list_p (loc_result))
18089 return NULL;
18090 else
18091 return loc_result->expr;
18094 /* The following routines define various Dwarf attributes and any data
18095 associated with them. */
18097 /* Add a location description attribute value to a DIE.
18099 This emits location attributes suitable for whole variables and
18100 whole parameters. Note that the location attributes for struct fields are
18101 generated by the routine `data_member_location_attribute' below. */
18103 static inline void
18104 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18105 dw_loc_list_ref descr)
18107 if (descr == 0)
18108 return;
18109 if (single_element_loc_list_p (descr))
18110 add_AT_loc (die, attr_kind, descr->expr);
18111 else
18112 add_AT_loc_list (die, attr_kind, descr);
18115 /* Add DW_AT_accessibility attribute to DIE if needed. */
18117 static void
18118 add_accessibility_attribute (dw_die_ref die, tree decl)
18120 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18121 children, otherwise the default is DW_ACCESS_public. In DWARF2
18122 the default has always been DW_ACCESS_public. */
18123 if (TREE_PROTECTED (decl))
18124 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18125 else if (TREE_PRIVATE (decl))
18127 if (dwarf_version == 2
18128 || die->die_parent == NULL
18129 || die->die_parent->die_tag != DW_TAG_class_type)
18130 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18132 else if (dwarf_version > 2
18133 && die->die_parent
18134 && die->die_parent->die_tag == DW_TAG_class_type)
18135 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18138 /* Attach the specialized form of location attribute used for data members of
18139 struct and union types. In the special case of a FIELD_DECL node which
18140 represents a bit-field, the "offset" part of this special location
18141 descriptor must indicate the distance in bytes from the lowest-addressed
18142 byte of the containing struct or union type to the lowest-addressed byte of
18143 the "containing object" for the bit-field. (See the `field_byte_offset'
18144 function above).
18146 For any given bit-field, the "containing object" is a hypothetical object
18147 (of some integral or enum type) within which the given bit-field lives. The
18148 type of this hypothetical "containing object" is always the same as the
18149 declared type of the individual bit-field itself (for GCC anyway... the
18150 DWARF spec doesn't actually mandate this). Note that it is the size (in
18151 bytes) of the hypothetical "containing object" which will be given in the
18152 DW_AT_byte_size attribute for this bit-field. (See the
18153 `byte_size_attribute' function below.) It is also used when calculating the
18154 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18155 function below.)
18157 CTX is required: see the comment for VLR_CONTEXT. */
18159 static void
18160 add_data_member_location_attribute (dw_die_ref die,
18161 tree decl,
18162 struct vlr_context *ctx)
18164 HOST_WIDE_INT offset;
18165 dw_loc_descr_ref loc_descr = 0;
18167 if (TREE_CODE (decl) == TREE_BINFO)
18169 /* We're working on the TAG_inheritance for a base class. */
18170 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18172 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18173 aren't at a fixed offset from all (sub)objects of the same
18174 type. We need to extract the appropriate offset from our
18175 vtable. The following dwarf expression means
18177 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18179 This is specific to the V3 ABI, of course. */
18181 dw_loc_descr_ref tmp;
18183 /* Make a copy of the object address. */
18184 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18185 add_loc_descr (&loc_descr, tmp);
18187 /* Extract the vtable address. */
18188 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18189 add_loc_descr (&loc_descr, tmp);
18191 /* Calculate the address of the offset. */
18192 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18193 gcc_assert (offset < 0);
18195 tmp = int_loc_descriptor (-offset);
18196 add_loc_descr (&loc_descr, tmp);
18197 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18198 add_loc_descr (&loc_descr, tmp);
18200 /* Extract the offset. */
18201 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18202 add_loc_descr (&loc_descr, tmp);
18204 /* Add it to the object address. */
18205 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18206 add_loc_descr (&loc_descr, tmp);
18208 else
18209 offset = tree_to_shwi (BINFO_OFFSET (decl));
18211 else
18213 loc_descr = field_byte_offset (decl, ctx, &offset);
18215 /* If loc_descr is available then we know the field offset is dynamic.
18216 However, GDB does not handle dynamic field offsets very well at the
18217 moment. */
18218 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18220 loc_descr = NULL;
18221 offset = 0;
18224 /* Data member location evalutation starts with the base address on the
18225 stack. Compute the field offset and add it to this base address. */
18226 else if (loc_descr != NULL)
18227 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18230 if (! loc_descr)
18232 if (dwarf_version > 2)
18234 /* Don't need to output a location expression, just the constant. */
18235 if (offset < 0)
18236 add_AT_int (die, DW_AT_data_member_location, offset);
18237 else
18238 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18239 return;
18241 else
18243 enum dwarf_location_atom op;
18245 /* The DWARF2 standard says that we should assume that the structure
18246 address is already on the stack, so we can specify a structure
18247 field address by using DW_OP_plus_uconst. */
18248 op = DW_OP_plus_uconst;
18249 loc_descr = new_loc_descr (op, offset, 0);
18253 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18256 /* Writes integer values to dw_vec_const array. */
18258 static void
18259 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18261 while (size != 0)
18263 *dest++ = val & 0xff;
18264 val >>= 8;
18265 --size;
18269 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18271 static HOST_WIDE_INT
18272 extract_int (const unsigned char *src, unsigned int size)
18274 HOST_WIDE_INT val = 0;
18276 src += size;
18277 while (size != 0)
18279 val <<= 8;
18280 val |= *--src & 0xff;
18281 --size;
18283 return val;
18286 /* Writes wide_int values to dw_vec_const array. */
18288 static void
18289 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18291 int i;
18293 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18295 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18296 return;
18299 /* We'd have to extend this code to support odd sizes. */
18300 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18302 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18304 if (WORDS_BIG_ENDIAN)
18305 for (i = n - 1; i >= 0; i--)
18307 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18308 dest += sizeof (HOST_WIDE_INT);
18310 else
18311 for (i = 0; i < n; i++)
18313 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18314 dest += sizeof (HOST_WIDE_INT);
18318 /* Writes floating point values to dw_vec_const array. */
18320 static void
18321 insert_float (const_rtx rtl, unsigned char *array)
18323 long val[4];
18324 int i;
18326 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18328 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18329 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18331 insert_int (val[i], 4, array);
18332 array += 4;
18336 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18337 does not have a "location" either in memory or in a register. These
18338 things can arise in GNU C when a constant is passed as an actual parameter
18339 to an inlined function. They can also arise in C++ where declared
18340 constants do not necessarily get memory "homes". */
18342 static bool
18343 add_const_value_attribute (dw_die_ref die, rtx rtl)
18345 switch (GET_CODE (rtl))
18347 case CONST_INT:
18349 HOST_WIDE_INT val = INTVAL (rtl);
18351 if (val < 0)
18352 add_AT_int (die, DW_AT_const_value, val);
18353 else
18354 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18356 return true;
18358 case CONST_WIDE_INT:
18360 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
18361 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18362 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18363 wide_int w = wi::zext (w1, prec);
18364 add_AT_wide (die, DW_AT_const_value, w);
18366 return true;
18368 case CONST_DOUBLE:
18369 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18370 floating-point constant. A CONST_DOUBLE is used whenever the
18371 constant requires more than one word in order to be adequately
18372 represented. */
18374 machine_mode mode = GET_MODE (rtl);
18376 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18377 add_AT_double (die, DW_AT_const_value,
18378 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18379 else
18381 unsigned int length = GET_MODE_SIZE (mode);
18382 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18384 insert_float (rtl, array);
18385 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18388 return true;
18390 case CONST_VECTOR:
18392 machine_mode mode = GET_MODE (rtl);
18393 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18394 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18395 unsigned char *array
18396 = ggc_vec_alloc<unsigned char> (length * elt_size);
18397 unsigned int i;
18398 unsigned char *p;
18399 machine_mode imode = GET_MODE_INNER (mode);
18401 switch (GET_MODE_CLASS (mode))
18403 case MODE_VECTOR_INT:
18404 for (i = 0, p = array; i < length; i++, p += elt_size)
18406 rtx elt = CONST_VECTOR_ELT (rtl, i);
18407 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
18409 break;
18411 case MODE_VECTOR_FLOAT:
18412 for (i = 0, p = array; i < length; i++, p += elt_size)
18414 rtx elt = CONST_VECTOR_ELT (rtl, i);
18415 insert_float (elt, p);
18417 break;
18419 default:
18420 gcc_unreachable ();
18423 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18425 return true;
18427 case CONST_STRING:
18428 if (dwarf_version >= 4 || !dwarf_strict)
18430 dw_loc_descr_ref loc_result;
18431 resolve_one_addr (&rtl);
18432 rtl_addr:
18433 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18434 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18435 add_AT_loc (die, DW_AT_location, loc_result);
18436 vec_safe_push (used_rtx_array, rtl);
18437 return true;
18439 return false;
18441 case CONST:
18442 if (CONSTANT_P (XEXP (rtl, 0)))
18443 return add_const_value_attribute (die, XEXP (rtl, 0));
18444 /* FALLTHROUGH */
18445 case SYMBOL_REF:
18446 if (!const_ok_for_output (rtl))
18447 return false;
18448 /* FALLTHROUGH */
18449 case LABEL_REF:
18450 if (dwarf_version >= 4 || !dwarf_strict)
18451 goto rtl_addr;
18452 return false;
18454 case PLUS:
18455 /* In cases where an inlined instance of an inline function is passed
18456 the address of an `auto' variable (which is local to the caller) we
18457 can get a situation where the DECL_RTL of the artificial local
18458 variable (for the inlining) which acts as a stand-in for the
18459 corresponding formal parameter (of the inline function) will look
18460 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18461 exactly a compile-time constant expression, but it isn't the address
18462 of the (artificial) local variable either. Rather, it represents the
18463 *value* which the artificial local variable always has during its
18464 lifetime. We currently have no way to represent such quasi-constant
18465 values in Dwarf, so for now we just punt and generate nothing. */
18466 return false;
18468 case HIGH:
18469 case CONST_FIXED:
18470 return false;
18472 case MEM:
18473 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18474 && MEM_READONLY_P (rtl)
18475 && GET_MODE (rtl) == BLKmode)
18477 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18478 return true;
18480 return false;
18482 default:
18483 /* No other kinds of rtx should be possible here. */
18484 gcc_unreachable ();
18486 return false;
18489 /* Determine whether the evaluation of EXPR references any variables
18490 or functions which aren't otherwise used (and therefore may not be
18491 output). */
18492 static tree
18493 reference_to_unused (tree * tp, int * walk_subtrees,
18494 void * data ATTRIBUTE_UNUSED)
18496 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18497 *walk_subtrees = 0;
18499 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18500 && ! TREE_ASM_WRITTEN (*tp))
18501 return *tp;
18502 /* ??? The C++ FE emits debug information for using decls, so
18503 putting gcc_unreachable here falls over. See PR31899. For now
18504 be conservative. */
18505 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18506 return *tp;
18507 else if (VAR_P (*tp))
18509 varpool_node *node = varpool_node::get (*tp);
18510 if (!node || !node->definition)
18511 return *tp;
18513 else if (TREE_CODE (*tp) == FUNCTION_DECL
18514 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18516 /* The call graph machinery must have finished analyzing,
18517 optimizing and gimplifying the CU by now.
18518 So if *TP has no call graph node associated
18519 to it, it means *TP will not be emitted. */
18520 if (!cgraph_node::get (*tp))
18521 return *tp;
18523 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18524 return *tp;
18526 return NULL_TREE;
18529 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18530 for use in a later add_const_value_attribute call. */
18532 static rtx
18533 rtl_for_decl_init (tree init, tree type)
18535 rtx rtl = NULL_RTX;
18537 STRIP_NOPS (init);
18539 /* If a variable is initialized with a string constant without embedded
18540 zeros, build CONST_STRING. */
18541 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18543 tree enttype = TREE_TYPE (type);
18544 tree domain = TYPE_DOMAIN (type);
18545 machine_mode mode = TYPE_MODE (enttype);
18547 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18548 && domain
18549 && integer_zerop (TYPE_MIN_VALUE (domain))
18550 && compare_tree_int (TYPE_MAX_VALUE (domain),
18551 TREE_STRING_LENGTH (init) - 1) == 0
18552 && ((size_t) TREE_STRING_LENGTH (init)
18553 == strlen (TREE_STRING_POINTER (init)) + 1))
18555 rtl = gen_rtx_CONST_STRING (VOIDmode,
18556 ggc_strdup (TREE_STRING_POINTER (init)));
18557 rtl = gen_rtx_MEM (BLKmode, rtl);
18558 MEM_READONLY_P (rtl) = 1;
18561 /* Other aggregates, and complex values, could be represented using
18562 CONCAT: FIXME! */
18563 else if (AGGREGATE_TYPE_P (type)
18564 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18565 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18566 || TREE_CODE (type) == COMPLEX_TYPE)
18568 /* Vectors only work if their mode is supported by the target.
18569 FIXME: generic vectors ought to work too. */
18570 else if (TREE_CODE (type) == VECTOR_TYPE
18571 && !VECTOR_MODE_P (TYPE_MODE (type)))
18573 /* If the initializer is something that we know will expand into an
18574 immediate RTL constant, expand it now. We must be careful not to
18575 reference variables which won't be output. */
18576 else if (initializer_constant_valid_p (init, type)
18577 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18579 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18580 possible. */
18581 if (TREE_CODE (type) == VECTOR_TYPE)
18582 switch (TREE_CODE (init))
18584 case VECTOR_CST:
18585 break;
18586 case CONSTRUCTOR:
18587 if (TREE_CONSTANT (init))
18589 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18590 bool constant_p = true;
18591 tree value;
18592 unsigned HOST_WIDE_INT ix;
18594 /* Even when ctor is constant, it might contain non-*_CST
18595 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18596 belong into VECTOR_CST nodes. */
18597 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18598 if (!CONSTANT_CLASS_P (value))
18600 constant_p = false;
18601 break;
18604 if (constant_p)
18606 init = build_vector_from_ctor (type, elts);
18607 break;
18610 /* FALLTHRU */
18612 default:
18613 return NULL;
18616 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18618 /* If expand_expr returns a MEM, it wasn't immediate. */
18619 gcc_assert (!rtl || !MEM_P (rtl));
18622 return rtl;
18625 /* Generate RTL for the variable DECL to represent its location. */
18627 static rtx
18628 rtl_for_decl_location (tree decl)
18630 rtx rtl;
18632 /* Here we have to decide where we are going to say the parameter "lives"
18633 (as far as the debugger is concerned). We only have a couple of
18634 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18636 DECL_RTL normally indicates where the parameter lives during most of the
18637 activation of the function. If optimization is enabled however, this
18638 could be either NULL or else a pseudo-reg. Both of those cases indicate
18639 that the parameter doesn't really live anywhere (as far as the code
18640 generation parts of GCC are concerned) during most of the function's
18641 activation. That will happen (for example) if the parameter is never
18642 referenced within the function.
18644 We could just generate a location descriptor here for all non-NULL
18645 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18646 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18647 where DECL_RTL is NULL or is a pseudo-reg.
18649 Note however that we can only get away with using DECL_INCOMING_RTL as
18650 a backup substitute for DECL_RTL in certain limited cases. In cases
18651 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18652 we can be sure that the parameter was passed using the same type as it is
18653 declared to have within the function, and that its DECL_INCOMING_RTL
18654 points us to a place where a value of that type is passed.
18656 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18657 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18658 because in these cases DECL_INCOMING_RTL points us to a value of some
18659 type which is *different* from the type of the parameter itself. Thus,
18660 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18661 such cases, the debugger would end up (for example) trying to fetch a
18662 `float' from a place which actually contains the first part of a
18663 `double'. That would lead to really incorrect and confusing
18664 output at debug-time.
18666 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18667 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18668 are a couple of exceptions however. On little-endian machines we can
18669 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18670 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18671 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18672 when (on a little-endian machine) a non-prototyped function has a
18673 parameter declared to be of type `short' or `char'. In such cases,
18674 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18675 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18676 passed `int' value. If the debugger then uses that address to fetch
18677 a `short' or a `char' (on a little-endian machine) the result will be
18678 the correct data, so we allow for such exceptional cases below.
18680 Note that our goal here is to describe the place where the given formal
18681 parameter lives during most of the function's activation (i.e. between the
18682 end of the prologue and the start of the epilogue). We'll do that as best
18683 as we can. Note however that if the given formal parameter is modified
18684 sometime during the execution of the function, then a stack backtrace (at
18685 debug-time) will show the function as having been called with the *new*
18686 value rather than the value which was originally passed in. This happens
18687 rarely enough that it is not a major problem, but it *is* a problem, and
18688 I'd like to fix it.
18690 A future version of dwarf2out.c may generate two additional attributes for
18691 any given DW_TAG_formal_parameter DIE which will describe the "passed
18692 type" and the "passed location" for the given formal parameter in addition
18693 to the attributes we now generate to indicate the "declared type" and the
18694 "active location" for each parameter. This additional set of attributes
18695 could be used by debuggers for stack backtraces. Separately, note that
18696 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18697 This happens (for example) for inlined-instances of inline function formal
18698 parameters which are never referenced. This really shouldn't be
18699 happening. All PARM_DECL nodes should get valid non-NULL
18700 DECL_INCOMING_RTL values. FIXME. */
18702 /* Use DECL_RTL as the "location" unless we find something better. */
18703 rtl = DECL_RTL_IF_SET (decl);
18705 /* When generating abstract instances, ignore everything except
18706 constants, symbols living in memory, and symbols living in
18707 fixed registers. */
18708 if (! reload_completed)
18710 if (rtl
18711 && (CONSTANT_P (rtl)
18712 || (MEM_P (rtl)
18713 && CONSTANT_P (XEXP (rtl, 0)))
18714 || (REG_P (rtl)
18715 && VAR_P (decl)
18716 && TREE_STATIC (decl))))
18718 rtl = targetm.delegitimize_address (rtl);
18719 return rtl;
18721 rtl = NULL_RTX;
18723 else if (TREE_CODE (decl) == PARM_DECL)
18725 if (rtl == NULL_RTX
18726 || is_pseudo_reg (rtl)
18727 || (MEM_P (rtl)
18728 && is_pseudo_reg (XEXP (rtl, 0))
18729 && DECL_INCOMING_RTL (decl)
18730 && MEM_P (DECL_INCOMING_RTL (decl))
18731 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18733 tree declared_type = TREE_TYPE (decl);
18734 tree passed_type = DECL_ARG_TYPE (decl);
18735 machine_mode dmode = TYPE_MODE (declared_type);
18736 machine_mode pmode = TYPE_MODE (passed_type);
18738 /* This decl represents a formal parameter which was optimized out.
18739 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18740 all cases where (rtl == NULL_RTX) just below. */
18741 if (dmode == pmode)
18742 rtl = DECL_INCOMING_RTL (decl);
18743 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18744 && SCALAR_INT_MODE_P (dmode)
18745 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18746 && DECL_INCOMING_RTL (decl))
18748 rtx inc = DECL_INCOMING_RTL (decl);
18749 if (REG_P (inc))
18750 rtl = inc;
18751 else if (MEM_P (inc))
18753 if (BYTES_BIG_ENDIAN)
18754 rtl = adjust_address_nv (inc, dmode,
18755 GET_MODE_SIZE (pmode)
18756 - GET_MODE_SIZE (dmode));
18757 else
18758 rtl = inc;
18763 /* If the parm was passed in registers, but lives on the stack, then
18764 make a big endian correction if the mode of the type of the
18765 parameter is not the same as the mode of the rtl. */
18766 /* ??? This is the same series of checks that are made in dbxout.c before
18767 we reach the big endian correction code there. It isn't clear if all
18768 of these checks are necessary here, but keeping them all is the safe
18769 thing to do. */
18770 else if (MEM_P (rtl)
18771 && XEXP (rtl, 0) != const0_rtx
18772 && ! CONSTANT_P (XEXP (rtl, 0))
18773 /* Not passed in memory. */
18774 && !MEM_P (DECL_INCOMING_RTL (decl))
18775 /* Not passed by invisible reference. */
18776 && (!REG_P (XEXP (rtl, 0))
18777 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18778 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18779 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18780 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18781 #endif
18783 /* Big endian correction check. */
18784 && BYTES_BIG_ENDIAN
18785 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18786 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18787 < UNITS_PER_WORD))
18789 machine_mode addr_mode = get_address_mode (rtl);
18790 int offset = (UNITS_PER_WORD
18791 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18793 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18794 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18797 else if (VAR_P (decl)
18798 && rtl
18799 && MEM_P (rtl)
18800 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18801 && BYTES_BIG_ENDIAN)
18803 machine_mode addr_mode = get_address_mode (rtl);
18804 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18805 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18807 /* If a variable is declared "register" yet is smaller than
18808 a register, then if we store the variable to memory, it
18809 looks like we're storing a register-sized value, when in
18810 fact we are not. We need to adjust the offset of the
18811 storage location to reflect the actual value's bytes,
18812 else gdb will not be able to display it. */
18813 if (rsize > dsize)
18814 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18815 plus_constant (addr_mode, XEXP (rtl, 0),
18816 rsize - dsize));
18819 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
18820 and will have been substituted directly into all expressions that use it.
18821 C does not have such a concept, but C++ and other languages do. */
18822 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
18823 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
18825 if (rtl)
18826 rtl = targetm.delegitimize_address (rtl);
18828 /* If we don't look past the constant pool, we risk emitting a
18829 reference to a constant pool entry that isn't referenced from
18830 code, and thus is not emitted. */
18831 if (rtl)
18832 rtl = avoid_constant_pool_reference (rtl);
18834 /* Try harder to get a rtl. If this symbol ends up not being emitted
18835 in the current CU, resolve_addr will remove the expression referencing
18836 it. */
18837 if (rtl == NULL_RTX
18838 && VAR_P (decl)
18839 && !DECL_EXTERNAL (decl)
18840 && TREE_STATIC (decl)
18841 && DECL_NAME (decl)
18842 && !DECL_HARD_REGISTER (decl)
18843 && DECL_MODE (decl) != VOIDmode)
18845 rtl = make_decl_rtl_for_debug (decl);
18846 if (!MEM_P (rtl)
18847 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
18848 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
18849 rtl = NULL_RTX;
18852 return rtl;
18855 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
18856 returned. If so, the decl for the COMMON block is returned, and the
18857 value is the offset into the common block for the symbol. */
18859 static tree
18860 fortran_common (tree decl, HOST_WIDE_INT *value)
18862 tree val_expr, cvar;
18863 machine_mode mode;
18864 HOST_WIDE_INT bitsize, bitpos;
18865 tree offset;
18866 int unsignedp, reversep, volatilep = 0;
18868 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
18869 it does not have a value (the offset into the common area), or if it
18870 is thread local (as opposed to global) then it isn't common, and shouldn't
18871 be handled as such. */
18872 if (!VAR_P (decl)
18873 || !TREE_STATIC (decl)
18874 || !DECL_HAS_VALUE_EXPR_P (decl)
18875 || !is_fortran ())
18876 return NULL_TREE;
18878 val_expr = DECL_VALUE_EXPR (decl);
18879 if (TREE_CODE (val_expr) != COMPONENT_REF)
18880 return NULL_TREE;
18882 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
18883 &unsignedp, &reversep, &volatilep);
18885 if (cvar == NULL_TREE
18886 || !VAR_P (cvar)
18887 || DECL_ARTIFICIAL (cvar)
18888 || !TREE_PUBLIC (cvar))
18889 return NULL_TREE;
18891 *value = 0;
18892 if (offset != NULL)
18894 if (!tree_fits_shwi_p (offset))
18895 return NULL_TREE;
18896 *value = tree_to_shwi (offset);
18898 if (bitpos != 0)
18899 *value += bitpos / BITS_PER_UNIT;
18901 return cvar;
18904 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
18905 data attribute for a variable or a parameter. We generate the
18906 DW_AT_const_value attribute only in those cases where the given variable
18907 or parameter does not have a true "location" either in memory or in a
18908 register. This can happen (for example) when a constant is passed as an
18909 actual argument in a call to an inline function. (It's possible that
18910 these things can crop up in other ways also.) Note that one type of
18911 constant value which can be passed into an inlined function is a constant
18912 pointer. This can happen for example if an actual argument in an inlined
18913 function call evaluates to a compile-time constant address.
18915 CACHE_P is true if it is worth caching the location list for DECL,
18916 so that future calls can reuse it rather than regenerate it from scratch.
18917 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
18918 since we will need to refer to them each time the function is inlined. */
18920 static bool
18921 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
18923 rtx rtl;
18924 dw_loc_list_ref list;
18925 var_loc_list *loc_list;
18926 cached_dw_loc_list *cache;
18928 if (early_dwarf)
18929 return false;
18931 if (TREE_CODE (decl) == ERROR_MARK)
18932 return false;
18934 if (get_AT (die, DW_AT_location)
18935 || get_AT (die, DW_AT_const_value))
18936 return true;
18938 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
18939 || TREE_CODE (decl) == RESULT_DECL);
18941 /* Try to get some constant RTL for this decl, and use that as the value of
18942 the location. */
18944 rtl = rtl_for_decl_location (decl);
18945 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18946 && add_const_value_attribute (die, rtl))
18947 return true;
18949 /* See if we have single element location list that is equivalent to
18950 a constant value. That way we are better to use add_const_value_attribute
18951 rather than expanding constant value equivalent. */
18952 loc_list = lookup_decl_loc (decl);
18953 if (loc_list
18954 && loc_list->first
18955 && loc_list->first->next == NULL
18956 && NOTE_P (loc_list->first->loc)
18957 && NOTE_VAR_LOCATION (loc_list->first->loc)
18958 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
18960 struct var_loc_node *node;
18962 node = loc_list->first;
18963 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
18964 if (GET_CODE (rtl) == EXPR_LIST)
18965 rtl = XEXP (rtl, 0);
18966 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18967 && add_const_value_attribute (die, rtl))
18968 return true;
18970 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
18971 list several times. See if we've already cached the contents. */
18972 list = NULL;
18973 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
18974 cache_p = false;
18975 if (cache_p)
18977 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
18978 if (cache)
18979 list = cache->loc_list;
18981 if (list == NULL)
18983 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
18984 NULL);
18985 /* It is usually worth caching this result if the decl is from
18986 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
18987 if (cache_p && list && list->dw_loc_next)
18989 cached_dw_loc_list **slot
18990 = cached_dw_loc_list_table->find_slot_with_hash (decl,
18991 DECL_UID (decl),
18992 INSERT);
18993 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
18994 cache->decl_id = DECL_UID (decl);
18995 cache->loc_list = list;
18996 *slot = cache;
18999 if (list)
19001 add_AT_location_description (die, DW_AT_location, list);
19002 return true;
19004 /* None of that worked, so it must not really have a location;
19005 try adding a constant value attribute from the DECL_INITIAL. */
19006 return tree_add_const_value_attribute_for_decl (die, decl);
19009 /* Helper function for tree_add_const_value_attribute. Natively encode
19010 initializer INIT into an array. Return true if successful. */
19012 static bool
19013 native_encode_initializer (tree init, unsigned char *array, int size)
19015 tree type;
19017 if (init == NULL_TREE)
19018 return false;
19020 STRIP_NOPS (init);
19021 switch (TREE_CODE (init))
19023 case STRING_CST:
19024 type = TREE_TYPE (init);
19025 if (TREE_CODE (type) == ARRAY_TYPE)
19027 tree enttype = TREE_TYPE (type);
19028 machine_mode mode = TYPE_MODE (enttype);
19030 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19031 return false;
19032 if (int_size_in_bytes (type) != size)
19033 return false;
19034 if (size > TREE_STRING_LENGTH (init))
19036 memcpy (array, TREE_STRING_POINTER (init),
19037 TREE_STRING_LENGTH (init));
19038 memset (array + TREE_STRING_LENGTH (init),
19039 '\0', size - TREE_STRING_LENGTH (init));
19041 else
19042 memcpy (array, TREE_STRING_POINTER (init), size);
19043 return true;
19045 return false;
19046 case CONSTRUCTOR:
19047 type = TREE_TYPE (init);
19048 if (int_size_in_bytes (type) != size)
19049 return false;
19050 if (TREE_CODE (type) == ARRAY_TYPE)
19052 HOST_WIDE_INT min_index;
19053 unsigned HOST_WIDE_INT cnt;
19054 int curpos = 0, fieldsize;
19055 constructor_elt *ce;
19057 if (TYPE_DOMAIN (type) == NULL_TREE
19058 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19059 return false;
19061 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19062 if (fieldsize <= 0)
19063 return false;
19065 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19066 memset (array, '\0', size);
19067 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19069 tree val = ce->value;
19070 tree index = ce->index;
19071 int pos = curpos;
19072 if (index && TREE_CODE (index) == RANGE_EXPR)
19073 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19074 * fieldsize;
19075 else if (index)
19076 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19078 if (val)
19080 STRIP_NOPS (val);
19081 if (!native_encode_initializer (val, array + pos, fieldsize))
19082 return false;
19084 curpos = pos + fieldsize;
19085 if (index && TREE_CODE (index) == RANGE_EXPR)
19087 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19088 - tree_to_shwi (TREE_OPERAND (index, 0));
19089 while (count-- > 0)
19091 if (val)
19092 memcpy (array + curpos, array + pos, fieldsize);
19093 curpos += fieldsize;
19096 gcc_assert (curpos <= size);
19098 return true;
19100 else if (TREE_CODE (type) == RECORD_TYPE
19101 || TREE_CODE (type) == UNION_TYPE)
19103 tree field = NULL_TREE;
19104 unsigned HOST_WIDE_INT cnt;
19105 constructor_elt *ce;
19107 if (int_size_in_bytes (type) != size)
19108 return false;
19110 if (TREE_CODE (type) == RECORD_TYPE)
19111 field = TYPE_FIELDS (type);
19113 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19115 tree val = ce->value;
19116 int pos, fieldsize;
19118 if (ce->index != 0)
19119 field = ce->index;
19121 if (val)
19122 STRIP_NOPS (val);
19124 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19125 return false;
19127 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19128 && TYPE_DOMAIN (TREE_TYPE (field))
19129 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19130 return false;
19131 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19132 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19133 return false;
19134 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19135 pos = int_byte_position (field);
19136 gcc_assert (pos + fieldsize <= size);
19137 if (val && fieldsize != 0
19138 && !native_encode_initializer (val, array + pos, fieldsize))
19139 return false;
19141 return true;
19143 return false;
19144 case VIEW_CONVERT_EXPR:
19145 case NON_LVALUE_EXPR:
19146 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19147 default:
19148 return native_encode_expr (init, array, size) == size;
19152 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19153 attribute is the const value T. */
19155 static bool
19156 tree_add_const_value_attribute (dw_die_ref die, tree t)
19158 tree init;
19159 tree type = TREE_TYPE (t);
19160 rtx rtl;
19162 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19163 return false;
19165 init = t;
19166 gcc_assert (!DECL_P (init));
19168 if (! early_dwarf)
19170 rtl = rtl_for_decl_init (init, type);
19171 if (rtl)
19172 return add_const_value_attribute (die, rtl);
19174 /* If the host and target are sane, try harder. */
19175 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19176 && initializer_constant_valid_p (init, type))
19178 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19179 if (size > 0 && (int) size == size)
19181 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19183 if (native_encode_initializer (init, array, size))
19185 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19186 return true;
19188 ggc_free (array);
19191 return false;
19194 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19195 attribute is the const value of T, where T is an integral constant
19196 variable with static storage duration
19197 (so it can't be a PARM_DECL or a RESULT_DECL). */
19199 static bool
19200 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19203 if (!decl
19204 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19205 || (VAR_P (decl) && !TREE_STATIC (decl)))
19206 return false;
19208 if (TREE_READONLY (decl)
19209 && ! TREE_THIS_VOLATILE (decl)
19210 && DECL_INITIAL (decl))
19211 /* OK */;
19212 else
19213 return false;
19215 /* Don't add DW_AT_const_value if abstract origin already has one. */
19216 if (get_AT (var_die, DW_AT_const_value))
19217 return false;
19219 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19222 /* Convert the CFI instructions for the current function into a
19223 location list. This is used for DW_AT_frame_base when we targeting
19224 a dwarf2 consumer that does not support the dwarf3
19225 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19226 expressions. */
19228 static dw_loc_list_ref
19229 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19231 int ix;
19232 dw_fde_ref fde;
19233 dw_loc_list_ref list, *list_tail;
19234 dw_cfi_ref cfi;
19235 dw_cfa_location last_cfa, next_cfa;
19236 const char *start_label, *last_label, *section;
19237 dw_cfa_location remember;
19239 fde = cfun->fde;
19240 gcc_assert (fde != NULL);
19242 section = secname_for_decl (current_function_decl);
19243 list_tail = &list;
19244 list = NULL;
19246 memset (&next_cfa, 0, sizeof (next_cfa));
19247 next_cfa.reg = INVALID_REGNUM;
19248 remember = next_cfa;
19250 start_label = fde->dw_fde_begin;
19252 /* ??? Bald assumption that the CIE opcode list does not contain
19253 advance opcodes. */
19254 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19255 lookup_cfa_1 (cfi, &next_cfa, &remember);
19257 last_cfa = next_cfa;
19258 last_label = start_label;
19260 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19262 /* If the first partition contained no CFI adjustments, the
19263 CIE opcodes apply to the whole first partition. */
19264 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19265 fde->dw_fde_begin, fde->dw_fde_end, section);
19266 list_tail =&(*list_tail)->dw_loc_next;
19267 start_label = last_label = fde->dw_fde_second_begin;
19270 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19272 switch (cfi->dw_cfi_opc)
19274 case DW_CFA_set_loc:
19275 case DW_CFA_advance_loc1:
19276 case DW_CFA_advance_loc2:
19277 case DW_CFA_advance_loc4:
19278 if (!cfa_equal_p (&last_cfa, &next_cfa))
19280 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19281 start_label, last_label, section);
19283 list_tail = &(*list_tail)->dw_loc_next;
19284 last_cfa = next_cfa;
19285 start_label = last_label;
19287 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19288 break;
19290 case DW_CFA_advance_loc:
19291 /* The encoding is complex enough that we should never emit this. */
19292 gcc_unreachable ();
19294 default:
19295 lookup_cfa_1 (cfi, &next_cfa, &remember);
19296 break;
19298 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19300 if (!cfa_equal_p (&last_cfa, &next_cfa))
19302 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19303 start_label, last_label, section);
19305 list_tail = &(*list_tail)->dw_loc_next;
19306 last_cfa = next_cfa;
19307 start_label = last_label;
19309 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19310 start_label, fde->dw_fde_end, section);
19311 list_tail = &(*list_tail)->dw_loc_next;
19312 start_label = last_label = fde->dw_fde_second_begin;
19316 if (!cfa_equal_p (&last_cfa, &next_cfa))
19318 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19319 start_label, last_label, section);
19320 list_tail = &(*list_tail)->dw_loc_next;
19321 start_label = last_label;
19324 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19325 start_label,
19326 fde->dw_fde_second_begin
19327 ? fde->dw_fde_second_end : fde->dw_fde_end,
19328 section);
19330 if (list && list->dw_loc_next)
19331 gen_llsym (list);
19333 return list;
19336 /* Compute a displacement from the "steady-state frame pointer" to the
19337 frame base (often the same as the CFA), and store it in
19338 frame_pointer_fb_offset. OFFSET is added to the displacement
19339 before the latter is negated. */
19341 static void
19342 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19344 rtx reg, elim;
19346 #ifdef FRAME_POINTER_CFA_OFFSET
19347 reg = frame_pointer_rtx;
19348 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19349 #else
19350 reg = arg_pointer_rtx;
19351 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19352 #endif
19354 elim = (ira_use_lra_p
19355 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19356 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19357 if (GET_CODE (elim) == PLUS)
19359 offset += INTVAL (XEXP (elim, 1));
19360 elim = XEXP (elim, 0);
19363 frame_pointer_fb_offset = -offset;
19365 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19366 in which to eliminate. This is because it's stack pointer isn't
19367 directly accessible as a register within the ISA. To work around
19368 this, assume that while we cannot provide a proper value for
19369 frame_pointer_fb_offset, we won't need one either. */
19370 frame_pointer_fb_offset_valid
19371 = ((SUPPORTS_STACK_ALIGNMENT
19372 && (elim == hard_frame_pointer_rtx
19373 || elim == stack_pointer_rtx))
19374 || elim == (frame_pointer_needed
19375 ? hard_frame_pointer_rtx
19376 : stack_pointer_rtx));
19379 /* Generate a DW_AT_name attribute given some string value to be included as
19380 the value of the attribute. */
19382 static void
19383 add_name_attribute (dw_die_ref die, const char *name_string)
19385 if (name_string != NULL && *name_string != 0)
19387 if (demangle_name_func)
19388 name_string = (*demangle_name_func) (name_string);
19390 add_AT_string (die, DW_AT_name, name_string);
19394 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19395 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19396 of TYPE accordingly.
19398 ??? This is a temporary measure until after we're able to generate
19399 regular DWARF for the complex Ada type system. */
19401 static void
19402 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19403 dw_die_ref context_die)
19405 tree dtype;
19406 dw_die_ref dtype_die;
19408 if (!lang_hooks.types.descriptive_type)
19409 return;
19411 dtype = lang_hooks.types.descriptive_type (type);
19412 if (!dtype)
19413 return;
19415 dtype_die = lookup_type_die (dtype);
19416 if (!dtype_die)
19418 gen_type_die (dtype, context_die);
19419 dtype_die = lookup_type_die (dtype);
19420 gcc_assert (dtype_die);
19423 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19426 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19428 static const char *
19429 comp_dir_string (void)
19431 const char *wd;
19432 char *wd1;
19433 static const char *cached_wd = NULL;
19435 if (cached_wd != NULL)
19436 return cached_wd;
19438 wd = get_src_pwd ();
19439 if (wd == NULL)
19440 return NULL;
19442 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19444 int wdlen;
19446 wdlen = strlen (wd);
19447 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19448 strcpy (wd1, wd);
19449 wd1 [wdlen] = DIR_SEPARATOR;
19450 wd1 [wdlen + 1] = 0;
19451 wd = wd1;
19454 cached_wd = remap_debug_filename (wd);
19455 return cached_wd;
19458 /* Generate a DW_AT_comp_dir attribute for DIE. */
19460 static void
19461 add_comp_dir_attribute (dw_die_ref die)
19463 const char * wd = comp_dir_string ();
19464 if (wd != NULL)
19465 add_AT_string (die, DW_AT_comp_dir, wd);
19468 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19469 pointer computation, ...), output a representation for that bound according
19470 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19471 loc_list_from_tree for the meaning of CONTEXT. */
19473 static void
19474 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19475 int forms, struct loc_descr_context *context)
19477 dw_die_ref context_die, decl_die;
19478 dw_loc_list_ref list;
19479 bool strip_conversions = true;
19480 bool placeholder_seen = false;
19482 while (strip_conversions)
19483 switch (TREE_CODE (value))
19485 case ERROR_MARK:
19486 case SAVE_EXPR:
19487 return;
19489 CASE_CONVERT:
19490 case VIEW_CONVERT_EXPR:
19491 value = TREE_OPERAND (value, 0);
19492 break;
19494 default:
19495 strip_conversions = false;
19496 break;
19499 /* If possible and permitted, output the attribute as a constant. */
19500 if ((forms & dw_scalar_form_constant) != 0
19501 && TREE_CODE (value) == INTEGER_CST)
19503 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19505 /* If HOST_WIDE_INT is big enough then represent the bound as
19506 a constant value. We need to choose a form based on
19507 whether the type is signed or unsigned. We cannot just
19508 call add_AT_unsigned if the value itself is positive
19509 (add_AT_unsigned might add the unsigned value encoded as
19510 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19511 bounds type and then sign extend any unsigned values found
19512 for signed types. This is needed only for
19513 DW_AT_{lower,upper}_bound, since for most other attributes,
19514 consumers will treat DW_FORM_data[1248] as unsigned values,
19515 regardless of the underlying type. */
19516 if (prec <= HOST_BITS_PER_WIDE_INT
19517 || tree_fits_uhwi_p (value))
19519 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19520 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19521 else
19522 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19524 else
19525 /* Otherwise represent the bound as an unsigned value with
19526 the precision of its type. The precision and signedness
19527 of the type will be necessary to re-interpret it
19528 unambiguously. */
19529 add_AT_wide (die, attr, value);
19530 return;
19533 /* Otherwise, if it's possible and permitted too, output a reference to
19534 another DIE. */
19535 if ((forms & dw_scalar_form_reference) != 0)
19537 tree decl = NULL_TREE;
19539 /* Some type attributes reference an outer type. For instance, the upper
19540 bound of an array may reference an embedding record (this happens in
19541 Ada). */
19542 if (TREE_CODE (value) == COMPONENT_REF
19543 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19544 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19545 decl = TREE_OPERAND (value, 1);
19547 else if (VAR_P (value)
19548 || TREE_CODE (value) == PARM_DECL
19549 || TREE_CODE (value) == RESULT_DECL)
19550 decl = value;
19552 if (decl != NULL_TREE)
19554 dw_die_ref decl_die = lookup_decl_die (decl);
19556 /* ??? Can this happen, or should the variable have been bound
19557 first? Probably it can, since I imagine that we try to create
19558 the types of parameters in the order in which they exist in
19559 the list, and won't have created a forward reference to a
19560 later parameter. */
19561 if (decl_die != NULL)
19563 add_AT_die_ref (die, attr, decl_die);
19564 return;
19569 /* Last chance: try to create a stack operation procedure to evaluate the
19570 value. Do nothing if even that is not possible or permitted. */
19571 if ((forms & dw_scalar_form_exprloc) == 0)
19572 return;
19574 list = loc_list_from_tree (value, 2, context);
19575 if (context && context->placeholder_arg)
19577 placeholder_seen = context->placeholder_seen;
19578 context->placeholder_seen = false;
19580 if (list == NULL || single_element_loc_list_p (list))
19582 /* If this attribute is not a reference nor constant, it is
19583 a DWARF expression rather than location description. For that
19584 loc_list_from_tree (value, 0, &context) is needed. */
19585 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19586 if (list2 && single_element_loc_list_p (list2))
19588 if (placeholder_seen)
19590 struct dwarf_procedure_info dpi;
19591 dpi.fndecl = NULL_TREE;
19592 dpi.args_count = 1;
19593 if (!resolve_args_picking (list2->expr, 1, &dpi))
19594 return;
19596 add_AT_loc (die, attr, list2->expr);
19597 return;
19601 /* If that failed to give a single element location list, fall back to
19602 outputting this as a reference... still if permitted. */
19603 if (list == NULL
19604 || (forms & dw_scalar_form_reference) == 0
19605 || placeholder_seen)
19606 return;
19608 if (current_function_decl == 0)
19609 context_die = comp_unit_die ();
19610 else
19611 context_die = lookup_decl_die (current_function_decl);
19613 decl_die = new_die (DW_TAG_variable, context_die, value);
19614 add_AT_flag (decl_die, DW_AT_artificial, 1);
19615 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19616 context_die);
19617 add_AT_location_description (decl_die, DW_AT_location, list);
19618 add_AT_die_ref (die, attr, decl_die);
19621 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19622 default. */
19624 static int
19625 lower_bound_default (void)
19627 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19629 case DW_LANG_C:
19630 case DW_LANG_C89:
19631 case DW_LANG_C99:
19632 case DW_LANG_C11:
19633 case DW_LANG_C_plus_plus:
19634 case DW_LANG_C_plus_plus_11:
19635 case DW_LANG_C_plus_plus_14:
19636 case DW_LANG_ObjC:
19637 case DW_LANG_ObjC_plus_plus:
19638 case DW_LANG_Java:
19639 return 0;
19640 case DW_LANG_Fortran77:
19641 case DW_LANG_Fortran90:
19642 case DW_LANG_Fortran95:
19643 case DW_LANG_Fortran03:
19644 case DW_LANG_Fortran08:
19645 return 1;
19646 case DW_LANG_UPC:
19647 case DW_LANG_D:
19648 case DW_LANG_Python:
19649 return dwarf_version >= 4 ? 0 : -1;
19650 case DW_LANG_Ada95:
19651 case DW_LANG_Ada83:
19652 case DW_LANG_Cobol74:
19653 case DW_LANG_Cobol85:
19654 case DW_LANG_Pascal83:
19655 case DW_LANG_Modula2:
19656 case DW_LANG_PLI:
19657 return dwarf_version >= 4 ? 1 : -1;
19658 default:
19659 return -1;
19663 /* Given a tree node describing an array bound (either lower or upper) output
19664 a representation for that bound. */
19666 static void
19667 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19668 tree bound, struct loc_descr_context *context)
19670 int dflt;
19672 while (1)
19673 switch (TREE_CODE (bound))
19675 /* Strip all conversions. */
19676 CASE_CONVERT:
19677 case VIEW_CONVERT_EXPR:
19678 bound = TREE_OPERAND (bound, 0);
19679 break;
19681 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19682 are even omitted when they are the default. */
19683 case INTEGER_CST:
19684 /* If the value for this bound is the default one, we can even omit the
19685 attribute. */
19686 if (bound_attr == DW_AT_lower_bound
19687 && tree_fits_shwi_p (bound)
19688 && (dflt = lower_bound_default ()) != -1
19689 && tree_to_shwi (bound) == dflt)
19690 return;
19692 /* FALLTHRU */
19694 default:
19695 /* Because of the complex interaction there can be with other GNAT
19696 encodings, GDB isn't ready yet to handle proper DWARF description
19697 for self-referencial subrange bounds: let GNAT encodings do the
19698 magic in such a case. */
19699 if (is_ada ()
19700 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19701 && contains_placeholder_p (bound))
19702 return;
19704 add_scalar_info (subrange_die, bound_attr, bound,
19705 dw_scalar_form_constant
19706 | dw_scalar_form_exprloc
19707 | dw_scalar_form_reference,
19708 context);
19709 return;
19713 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19714 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19715 Note that the block of subscript information for an array type also
19716 includes information about the element type of the given array type.
19718 This function reuses previously set type and bound information if
19719 available. */
19721 static void
19722 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19724 unsigned dimension_number;
19725 tree lower, upper;
19726 dw_die_ref child = type_die->die_child;
19728 for (dimension_number = 0;
19729 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19730 type = TREE_TYPE (type), dimension_number++)
19732 tree domain = TYPE_DOMAIN (type);
19734 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19735 break;
19737 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19738 and (in GNU C only) variable bounds. Handle all three forms
19739 here. */
19741 /* Find and reuse a previously generated DW_TAG_subrange_type if
19742 available.
19744 For multi-dimensional arrays, as we iterate through the
19745 various dimensions in the enclosing for loop above, we also
19746 iterate through the DIE children and pick at each
19747 DW_TAG_subrange_type previously generated (if available).
19748 Each child DW_TAG_subrange_type DIE describes the range of
19749 the current dimension. At this point we should have as many
19750 DW_TAG_subrange_type's as we have dimensions in the
19751 array. */
19752 dw_die_ref subrange_die = NULL;
19753 if (child)
19754 while (1)
19756 child = child->die_sib;
19757 if (child->die_tag == DW_TAG_subrange_type)
19758 subrange_die = child;
19759 if (child == type_die->die_child)
19761 /* If we wrapped around, stop looking next time. */
19762 child = NULL;
19763 break;
19765 if (child->die_tag == DW_TAG_subrange_type)
19766 break;
19768 if (!subrange_die)
19769 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19771 if (domain)
19773 /* We have an array type with specified bounds. */
19774 lower = TYPE_MIN_VALUE (domain);
19775 upper = TYPE_MAX_VALUE (domain);
19777 /* Define the index type. */
19778 if (TREE_TYPE (domain)
19779 && !get_AT (subrange_die, DW_AT_type))
19781 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19782 TREE_TYPE field. We can't emit debug info for this
19783 because it is an unnamed integral type. */
19784 if (TREE_CODE (domain) == INTEGER_TYPE
19785 && TYPE_NAME (domain) == NULL_TREE
19786 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19787 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19789 else
19790 add_type_attribute (subrange_die, TREE_TYPE (domain),
19791 TYPE_UNQUALIFIED, false, type_die);
19794 /* ??? If upper is NULL, the array has unspecified length,
19795 but it does have a lower bound. This happens with Fortran
19796 dimension arr(N:*)
19797 Since the debugger is definitely going to need to know N
19798 to produce useful results, go ahead and output the lower
19799 bound solo, and hope the debugger can cope. */
19801 if (!get_AT (subrange_die, DW_AT_lower_bound))
19802 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19803 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19804 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19807 /* Otherwise we have an array type with an unspecified length. The
19808 DWARF-2 spec does not say how to handle this; let's just leave out the
19809 bounds. */
19813 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19815 static void
19816 add_byte_size_attribute (dw_die_ref die, tree tree_node)
19818 dw_die_ref decl_die;
19819 HOST_WIDE_INT size;
19820 dw_loc_descr_ref size_expr = NULL;
19822 switch (TREE_CODE (tree_node))
19824 case ERROR_MARK:
19825 size = 0;
19826 break;
19827 case ENUMERAL_TYPE:
19828 case RECORD_TYPE:
19829 case UNION_TYPE:
19830 case QUAL_UNION_TYPE:
19831 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
19832 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
19834 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
19835 return;
19837 size_expr = type_byte_size (tree_node, &size);
19838 break;
19839 case FIELD_DECL:
19840 /* For a data member of a struct or union, the DW_AT_byte_size is
19841 generally given as the number of bytes normally allocated for an
19842 object of the *declared* type of the member itself. This is true
19843 even for bit-fields. */
19844 size = int_size_in_bytes (field_type (tree_node));
19845 break;
19846 default:
19847 gcc_unreachable ();
19850 /* Support for dynamically-sized objects was introduced by DWARFv3.
19851 At the moment, GDB does not handle variable byte sizes very well,
19852 though. */
19853 if ((dwarf_version >= 3 || !dwarf_strict)
19854 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
19855 && size_expr != NULL)
19856 add_AT_loc (die, DW_AT_byte_size, size_expr);
19858 /* Note that `size' might be -1 when we get to this point. If it is, that
19859 indicates that the byte size of the entity in question is variable and
19860 that we could not generate a DWARF expression that computes it. */
19861 if (size >= 0)
19862 add_AT_unsigned (die, DW_AT_byte_size, size);
19865 /* For a FIELD_DECL node which represents a bit-field, output an attribute
19866 which specifies the distance in bits from the highest order bit of the
19867 "containing object" for the bit-field to the highest order bit of the
19868 bit-field itself.
19870 For any given bit-field, the "containing object" is a hypothetical object
19871 (of some integral or enum type) within which the given bit-field lives. The
19872 type of this hypothetical "containing object" is always the same as the
19873 declared type of the individual bit-field itself. The determination of the
19874 exact location of the "containing object" for a bit-field is rather
19875 complicated. It's handled by the `field_byte_offset' function (above).
19877 CTX is required: see the comment for VLR_CONTEXT.
19879 Note that it is the size (in bytes) of the hypothetical "containing object"
19880 which will be given in the DW_AT_byte_size attribute for this bit-field.
19881 (See `byte_size_attribute' above). */
19883 static inline void
19884 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
19886 HOST_WIDE_INT object_offset_in_bytes;
19887 tree original_type = DECL_BIT_FIELD_TYPE (decl);
19888 HOST_WIDE_INT bitpos_int;
19889 HOST_WIDE_INT highest_order_object_bit_offset;
19890 HOST_WIDE_INT highest_order_field_bit_offset;
19891 HOST_WIDE_INT bit_offset;
19893 field_byte_offset (decl, ctx, &object_offset_in_bytes);
19895 /* Must be a field and a bit field. */
19896 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
19898 /* We can't yet handle bit-fields whose offsets are variable, so if we
19899 encounter such things, just return without generating any attribute
19900 whatsoever. Likewise for variable or too large size. */
19901 if (! tree_fits_shwi_p (bit_position (decl))
19902 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
19903 return;
19905 bitpos_int = int_bit_position (decl);
19907 /* Note that the bit offset is always the distance (in bits) from the
19908 highest-order bit of the "containing object" to the highest-order bit of
19909 the bit-field itself. Since the "high-order end" of any object or field
19910 is different on big-endian and little-endian machines, the computation
19911 below must take account of these differences. */
19912 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
19913 highest_order_field_bit_offset = bitpos_int;
19915 if (! BYTES_BIG_ENDIAN)
19917 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
19918 highest_order_object_bit_offset +=
19919 simple_type_size_in_bits (original_type);
19922 bit_offset
19923 = (! BYTES_BIG_ENDIAN
19924 ? highest_order_object_bit_offset - highest_order_field_bit_offset
19925 : highest_order_field_bit_offset - highest_order_object_bit_offset);
19927 if (bit_offset < 0)
19928 add_AT_int (die, DW_AT_bit_offset, bit_offset);
19929 else
19930 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
19933 /* For a FIELD_DECL node which represents a bit field, output an attribute
19934 which specifies the length in bits of the given field. */
19936 static inline void
19937 add_bit_size_attribute (dw_die_ref die, tree decl)
19939 /* Must be a field and a bit field. */
19940 gcc_assert (TREE_CODE (decl) == FIELD_DECL
19941 && DECL_BIT_FIELD_TYPE (decl));
19943 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
19944 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
19947 /* If the compiled language is ANSI C, then add a 'prototyped'
19948 attribute, if arg types are given for the parameters of a function. */
19950 static inline void
19951 add_prototyped_attribute (dw_die_ref die, tree func_type)
19953 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19955 case DW_LANG_C:
19956 case DW_LANG_C89:
19957 case DW_LANG_C99:
19958 case DW_LANG_C11:
19959 case DW_LANG_ObjC:
19960 if (prototype_p (func_type))
19961 add_AT_flag (die, DW_AT_prototyped, 1);
19962 break;
19963 default:
19964 break;
19968 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
19969 by looking in the type declaration, the object declaration equate table or
19970 the block mapping. */
19972 static inline dw_die_ref
19973 add_abstract_origin_attribute (dw_die_ref die, tree origin)
19975 dw_die_ref origin_die = NULL;
19977 if (TREE_CODE (origin) != FUNCTION_DECL
19978 && TREE_CODE (origin) != BLOCK)
19980 /* We may have gotten separated from the block for the inlined
19981 function, if we're in an exception handler or some such; make
19982 sure that the abstract function has been written out.
19984 Doing this for nested functions is wrong, however; functions are
19985 distinct units, and our context might not even be inline. */
19986 tree fn = origin;
19988 if (TYPE_P (fn))
19989 fn = TYPE_STUB_DECL (fn);
19991 fn = decl_function_context (fn);
19992 if (fn)
19993 dwarf2out_abstract_function (fn);
19996 if (DECL_P (origin))
19997 origin_die = lookup_decl_die (origin);
19998 else if (TYPE_P (origin))
19999 origin_die = lookup_type_die (origin);
20000 else if (TREE_CODE (origin) == BLOCK)
20001 origin_die = BLOCK_DIE (origin);
20003 /* XXX: Functions that are never lowered don't always have correct block
20004 trees (in the case of java, they simply have no block tree, in some other
20005 languages). For these functions, there is nothing we can really do to
20006 output correct debug info for inlined functions in all cases. Rather
20007 than die, we'll just produce deficient debug info now, in that we will
20008 have variables without a proper abstract origin. In the future, when all
20009 functions are lowered, we should re-add a gcc_assert (origin_die)
20010 here. */
20012 if (origin_die)
20013 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20014 return origin_die;
20017 /* We do not currently support the pure_virtual attribute. */
20019 static inline void
20020 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20022 if (DECL_VINDEX (func_decl))
20024 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20026 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20027 add_AT_loc (die, DW_AT_vtable_elem_location,
20028 new_loc_descr (DW_OP_constu,
20029 tree_to_shwi (DECL_VINDEX (func_decl)),
20030 0));
20032 /* GNU extension: Record what type this method came from originally. */
20033 if (debug_info_level > DINFO_LEVEL_TERSE
20034 && DECL_CONTEXT (func_decl))
20035 add_AT_die_ref (die, DW_AT_containing_type,
20036 lookup_type_die (DECL_CONTEXT (func_decl)));
20040 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20041 given decl. This used to be a vendor extension until after DWARF 4
20042 standardized it. */
20044 static void
20045 add_linkage_attr (dw_die_ref die, tree decl)
20047 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20049 /* Mimic what assemble_name_raw does with a leading '*'. */
20050 if (name[0] == '*')
20051 name = &name[1];
20053 if (dwarf_version >= 4)
20054 add_AT_string (die, DW_AT_linkage_name, name);
20055 else
20056 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20059 /* Add source coordinate attributes for the given decl. */
20061 static void
20062 add_src_coords_attributes (dw_die_ref die, tree decl)
20064 expanded_location s;
20066 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20067 return;
20068 s = expand_location (DECL_SOURCE_LOCATION (decl));
20069 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20070 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20073 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20075 static void
20076 add_linkage_name_raw (dw_die_ref die, tree decl)
20078 /* Defer until we have an assembler name set. */
20079 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20081 limbo_die_node *asm_name;
20083 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20084 asm_name->die = die;
20085 asm_name->created_for = decl;
20086 asm_name->next = deferred_asm_name;
20087 deferred_asm_name = asm_name;
20089 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20090 add_linkage_attr (die, decl);
20093 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20095 static void
20096 add_linkage_name (dw_die_ref die, tree decl)
20098 if (debug_info_level > DINFO_LEVEL_NONE
20099 && VAR_OR_FUNCTION_DECL_P (decl)
20100 && TREE_PUBLIC (decl)
20101 && !(VAR_P (decl) && DECL_REGISTER (decl))
20102 && die->die_tag != DW_TAG_member)
20103 add_linkage_name_raw (die, decl);
20106 /* Add a DW_AT_name attribute and source coordinate attribute for the
20107 given decl, but only if it actually has a name. */
20109 static void
20110 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20111 bool no_linkage_name)
20113 tree decl_name;
20115 decl_name = DECL_NAME (decl);
20116 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20118 const char *name = dwarf2_name (decl, 0);
20119 if (name)
20120 add_name_attribute (die, name);
20121 if (! DECL_ARTIFICIAL (decl))
20122 add_src_coords_attributes (die, decl);
20124 if (!no_linkage_name)
20125 add_linkage_name (die, decl);
20128 #ifdef VMS_DEBUGGING_INFO
20129 /* Get the function's name, as described by its RTL. This may be different
20130 from the DECL_NAME name used in the source file. */
20131 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20133 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20134 XEXP (DECL_RTL (decl), 0), false);
20135 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20137 #endif /* VMS_DEBUGGING_INFO */
20140 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20142 static void
20143 add_discr_value (dw_die_ref die, dw_discr_value *value)
20145 dw_attr_node attr;
20147 attr.dw_attr = DW_AT_discr_value;
20148 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20149 attr.dw_attr_val.val_entry = NULL;
20150 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20151 if (value->pos)
20152 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20153 else
20154 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20155 add_dwarf_attr (die, &attr);
20158 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20160 static void
20161 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20163 dw_attr_node attr;
20165 attr.dw_attr = DW_AT_discr_list;
20166 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20167 attr.dw_attr_val.val_entry = NULL;
20168 attr.dw_attr_val.v.val_discr_list = discr_list;
20169 add_dwarf_attr (die, &attr);
20172 static inline dw_discr_list_ref
20173 AT_discr_list (dw_attr_node *attr)
20175 return attr->dw_attr_val.v.val_discr_list;
20178 #ifdef VMS_DEBUGGING_INFO
20179 /* Output the debug main pointer die for VMS */
20181 void
20182 dwarf2out_vms_debug_main_pointer (void)
20184 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20185 dw_die_ref die;
20187 /* Allocate the VMS debug main subprogram die. */
20188 die = ggc_cleared_alloc<die_node> ();
20189 die->die_tag = DW_TAG_subprogram;
20190 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20191 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20192 current_function_funcdef_no);
20193 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20195 /* Make it the first child of comp_unit_die (). */
20196 die->die_parent = comp_unit_die ();
20197 if (comp_unit_die ()->die_child)
20199 die->die_sib = comp_unit_die ()->die_child->die_sib;
20200 comp_unit_die ()->die_child->die_sib = die;
20202 else
20204 die->die_sib = die;
20205 comp_unit_die ()->die_child = die;
20208 #endif /* VMS_DEBUGGING_INFO */
20210 /* Push a new declaration scope. */
20212 static void
20213 push_decl_scope (tree scope)
20215 vec_safe_push (decl_scope_table, scope);
20218 /* Pop a declaration scope. */
20220 static inline void
20221 pop_decl_scope (void)
20223 decl_scope_table->pop ();
20226 /* walk_tree helper function for uses_local_type, below. */
20228 static tree
20229 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20231 if (!TYPE_P (*tp))
20232 *walk_subtrees = 0;
20233 else
20235 tree name = TYPE_NAME (*tp);
20236 if (name && DECL_P (name) && decl_function_context (name))
20237 return *tp;
20239 return NULL_TREE;
20242 /* If TYPE involves a function-local type (including a local typedef to a
20243 non-local type), returns that type; otherwise returns NULL_TREE. */
20245 static tree
20246 uses_local_type (tree type)
20248 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20249 return used;
20252 /* Return the DIE for the scope that immediately contains this type.
20253 Non-named types that do not involve a function-local type get global
20254 scope. Named types nested in namespaces or other types get their
20255 containing scope. All other types (i.e. function-local named types) get
20256 the current active scope. */
20258 static dw_die_ref
20259 scope_die_for (tree t, dw_die_ref context_die)
20261 dw_die_ref scope_die = NULL;
20262 tree containing_scope;
20264 /* Non-types always go in the current scope. */
20265 gcc_assert (TYPE_P (t));
20267 /* Use the scope of the typedef, rather than the scope of the type
20268 it refers to. */
20269 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20270 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20271 else
20272 containing_scope = TYPE_CONTEXT (t);
20274 /* Use the containing namespace if there is one. */
20275 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20277 if (context_die == lookup_decl_die (containing_scope))
20278 /* OK */;
20279 else if (debug_info_level > DINFO_LEVEL_TERSE)
20280 context_die = get_context_die (containing_scope);
20281 else
20282 containing_scope = NULL_TREE;
20285 /* Ignore function type "scopes" from the C frontend. They mean that
20286 a tagged type is local to a parmlist of a function declarator, but
20287 that isn't useful to DWARF. */
20288 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20289 containing_scope = NULL_TREE;
20291 if (SCOPE_FILE_SCOPE_P (containing_scope))
20293 /* If T uses a local type keep it local as well, to avoid references
20294 to function-local DIEs from outside the function. */
20295 if (current_function_decl && uses_local_type (t))
20296 scope_die = context_die;
20297 else
20298 scope_die = comp_unit_die ();
20300 else if (TYPE_P (containing_scope))
20302 /* For types, we can just look up the appropriate DIE. */
20303 if (debug_info_level > DINFO_LEVEL_TERSE)
20304 scope_die = get_context_die (containing_scope);
20305 else
20307 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20308 if (scope_die == NULL)
20309 scope_die = comp_unit_die ();
20312 else
20313 scope_die = context_die;
20315 return scope_die;
20318 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20320 static inline int
20321 local_scope_p (dw_die_ref context_die)
20323 for (; context_die; context_die = context_die->die_parent)
20324 if (context_die->die_tag == DW_TAG_inlined_subroutine
20325 || context_die->die_tag == DW_TAG_subprogram)
20326 return 1;
20328 return 0;
20331 /* Returns nonzero if CONTEXT_DIE is a class. */
20333 static inline int
20334 class_scope_p (dw_die_ref context_die)
20336 return (context_die
20337 && (context_die->die_tag == DW_TAG_structure_type
20338 || context_die->die_tag == DW_TAG_class_type
20339 || context_die->die_tag == DW_TAG_interface_type
20340 || context_die->die_tag == DW_TAG_union_type));
20343 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20344 whether or not to treat a DIE in this context as a declaration. */
20346 static inline int
20347 class_or_namespace_scope_p (dw_die_ref context_die)
20349 return (class_scope_p (context_die)
20350 || (context_die && context_die->die_tag == DW_TAG_namespace));
20353 /* Many forms of DIEs require a "type description" attribute. This
20354 routine locates the proper "type descriptor" die for the type given
20355 by 'type' plus any additional qualifiers given by 'cv_quals', and
20356 adds a DW_AT_type attribute below the given die. */
20358 static void
20359 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20360 bool reverse, dw_die_ref context_die)
20362 enum tree_code code = TREE_CODE (type);
20363 dw_die_ref type_die = NULL;
20365 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20366 or fixed-point type, use the inner type. This is because we have no
20367 support for unnamed types in base_type_die. This can happen if this is
20368 an Ada subrange type. Correct solution is emit a subrange type die. */
20369 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20370 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20371 type = TREE_TYPE (type), code = TREE_CODE (type);
20373 if (code == ERROR_MARK
20374 /* Handle a special case. For functions whose return type is void, we
20375 generate *no* type attribute. (Note that no object may have type
20376 `void', so this only applies to function return types). */
20377 || code == VOID_TYPE)
20378 return;
20380 type_die = modified_type_die (type,
20381 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20382 reverse,
20383 context_die);
20385 if (type_die != NULL)
20386 add_AT_die_ref (object_die, DW_AT_type, type_die);
20389 /* Given an object die, add the calling convention attribute for the
20390 function call type. */
20391 static void
20392 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20394 enum dwarf_calling_convention value = DW_CC_normal;
20396 value = ((enum dwarf_calling_convention)
20397 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20399 if (is_fortran ()
20400 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20402 /* DWARF 2 doesn't provide a way to identify a program's source-level
20403 entry point. DW_AT_calling_convention attributes are only meant
20404 to describe functions' calling conventions. However, lacking a
20405 better way to signal the Fortran main program, we used this for
20406 a long time, following existing custom. Now, DWARF 4 has
20407 DW_AT_main_subprogram, which we add below, but some tools still
20408 rely on the old way, which we thus keep. */
20409 value = DW_CC_program;
20411 if (dwarf_version >= 4 || !dwarf_strict)
20412 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20415 /* Only add the attribute if the backend requests it, and
20416 is not DW_CC_normal. */
20417 if (value && (value != DW_CC_normal))
20418 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20421 /* Given a tree pointer to a struct, class, union, or enum type node, return
20422 a pointer to the (string) tag name for the given type, or zero if the type
20423 was declared without a tag. */
20425 static const char *
20426 type_tag (const_tree type)
20428 const char *name = 0;
20430 if (TYPE_NAME (type) != 0)
20432 tree t = 0;
20434 /* Find the IDENTIFIER_NODE for the type name. */
20435 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20436 && !TYPE_NAMELESS (type))
20437 t = TYPE_NAME (type);
20439 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20440 a TYPE_DECL node, regardless of whether or not a `typedef' was
20441 involved. */
20442 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20443 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20445 /* We want to be extra verbose. Don't call dwarf_name if
20446 DECL_NAME isn't set. The default hook for decl_printable_name
20447 doesn't like that, and in this context it's correct to return
20448 0, instead of "<anonymous>" or the like. */
20449 if (DECL_NAME (TYPE_NAME (type))
20450 && !DECL_NAMELESS (TYPE_NAME (type)))
20451 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20454 /* Now get the name as a string, or invent one. */
20455 if (!name && t != 0)
20456 name = IDENTIFIER_POINTER (t);
20459 return (name == 0 || *name == '\0') ? 0 : name;
20462 /* Return the type associated with a data member, make a special check
20463 for bit field types. */
20465 static inline tree
20466 member_declared_type (const_tree member)
20468 return (DECL_BIT_FIELD_TYPE (member)
20469 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20472 /* Get the decl's label, as described by its RTL. This may be different
20473 from the DECL_NAME name used in the source file. */
20475 #if 0
20476 static const char *
20477 decl_start_label (tree decl)
20479 rtx x;
20480 const char *fnname;
20482 x = DECL_RTL (decl);
20483 gcc_assert (MEM_P (x));
20485 x = XEXP (x, 0);
20486 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20488 fnname = XSTR (x, 0);
20489 return fnname;
20491 #endif
20493 /* For variable-length arrays that have been previously generated, but
20494 may be incomplete due to missing subscript info, fill the subscript
20495 info. Return TRUE if this is one of those cases. */
20496 static bool
20497 fill_variable_array_bounds (tree type)
20499 if (TREE_ASM_WRITTEN (type)
20500 && TREE_CODE (type) == ARRAY_TYPE
20501 && variably_modified_type_p (type, NULL))
20503 dw_die_ref array_die = lookup_type_die (type);
20504 if (!array_die)
20505 return false;
20506 add_subscript_info (array_die, type, !is_ada ());
20507 return true;
20509 return false;
20512 /* These routines generate the internal representation of the DIE's for
20513 the compilation unit. Debugging information is collected by walking
20514 the declaration trees passed in from dwarf2out_decl(). */
20516 static void
20517 gen_array_type_die (tree type, dw_die_ref context_die)
20519 dw_die_ref array_die;
20521 /* GNU compilers represent multidimensional array types as sequences of one
20522 dimensional array types whose element types are themselves array types.
20523 We sometimes squish that down to a single array_type DIE with multiple
20524 subscripts in the Dwarf debugging info. The draft Dwarf specification
20525 say that we are allowed to do this kind of compression in C, because
20526 there is no difference between an array of arrays and a multidimensional
20527 array. We don't do this for Ada to remain as close as possible to the
20528 actual representation, which is especially important against the language
20529 flexibilty wrt arrays of variable size. */
20531 bool collapse_nested_arrays = !is_ada ();
20533 if (fill_variable_array_bounds (type))
20534 return;
20536 dw_die_ref scope_die = scope_die_for (type, context_die);
20537 tree element_type;
20539 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20540 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20541 if (TYPE_STRING_FLAG (type)
20542 && TREE_CODE (type) == ARRAY_TYPE
20543 && is_fortran ()
20544 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20546 HOST_WIDE_INT size;
20548 array_die = new_die (DW_TAG_string_type, scope_die, type);
20549 add_name_attribute (array_die, type_tag (type));
20550 equate_type_number_to_die (type, array_die);
20551 size = int_size_in_bytes (type);
20552 if (size >= 0)
20553 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20554 else if (TYPE_DOMAIN (type) != NULL_TREE
20555 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20557 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20558 tree rszdecl = szdecl;
20559 HOST_WIDE_INT rsize = 0;
20561 size = int_size_in_bytes (TREE_TYPE (szdecl));
20562 if (!DECL_P (szdecl))
20564 if (TREE_CODE (szdecl) == INDIRECT_REF
20565 && DECL_P (TREE_OPERAND (szdecl, 0)))
20567 rszdecl = TREE_OPERAND (szdecl, 0);
20568 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
20569 if (rsize <= 0)
20570 size = 0;
20572 else
20573 size = 0;
20575 if (size > 0)
20577 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
20578 if (loc == NULL
20579 && early_dwarf
20580 && current_function_decl
20581 && DECL_CONTEXT (rszdecl) == current_function_decl)
20583 dw_die_ref ref = lookup_decl_die (rszdecl);
20584 dw_loc_descr_ref l = NULL;
20585 if (ref)
20587 l = new_loc_descr (DW_OP_call4, 0, 0);
20588 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20589 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
20590 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
20592 else if (TREE_CODE (rszdecl) == PARM_DECL
20593 && string_types)
20595 l = new_loc_descr (DW_OP_call4, 0, 0);
20596 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
20597 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
20598 string_types->safe_push (array_die);
20600 if (l && rszdecl != szdecl)
20602 if (rsize == DWARF2_ADDR_SIZE)
20603 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
20604 0, 0));
20605 else
20606 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
20607 rsize, 0));
20609 if (l)
20610 loc = new_loc_list (l, NULL, NULL, NULL);
20612 if (loc)
20614 add_AT_location_description (array_die, DW_AT_string_length,
20615 loc);
20616 if (size != DWARF2_ADDR_SIZE)
20617 add_AT_unsigned (array_die, dwarf_version >= 5
20618 ? DW_AT_string_length_byte_size
20619 : DW_AT_byte_size, size);
20623 return;
20626 array_die = new_die (DW_TAG_array_type, scope_die, type);
20627 add_name_attribute (array_die, type_tag (type));
20628 equate_type_number_to_die (type, array_die);
20630 if (TREE_CODE (type) == VECTOR_TYPE)
20631 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20633 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20634 if (is_fortran ()
20635 && TREE_CODE (type) == ARRAY_TYPE
20636 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20637 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20638 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20640 #if 0
20641 /* We default the array ordering. SDB will probably do
20642 the right things even if DW_AT_ordering is not present. It's not even
20643 an issue until we start to get into multidimensional arrays anyway. If
20644 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20645 then we'll have to put the DW_AT_ordering attribute back in. (But if
20646 and when we find out that we need to put these in, we will only do so
20647 for multidimensional arrays. */
20648 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20649 #endif
20651 if (TREE_CODE (type) == VECTOR_TYPE)
20653 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20654 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20655 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20656 add_bound_info (subrange_die, DW_AT_upper_bound,
20657 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20659 else
20660 add_subscript_info (array_die, type, collapse_nested_arrays);
20662 /* Add representation of the type of the elements of this array type and
20663 emit the corresponding DIE if we haven't done it already. */
20664 element_type = TREE_TYPE (type);
20665 if (collapse_nested_arrays)
20666 while (TREE_CODE (element_type) == ARRAY_TYPE)
20668 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20669 break;
20670 element_type = TREE_TYPE (element_type);
20673 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20674 TREE_CODE (type) == ARRAY_TYPE
20675 && TYPE_REVERSE_STORAGE_ORDER (type),
20676 context_die);
20678 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20679 if (TYPE_ARTIFICIAL (type))
20680 add_AT_flag (array_die, DW_AT_artificial, 1);
20682 if (get_AT (array_die, DW_AT_name))
20683 add_pubtype (type, array_die);
20686 /* After all arguments are created, adjust any DW_TAG_string_type
20687 DIEs DW_AT_string_length attributes. */
20689 static void
20690 adjust_string_types (void)
20692 dw_die_ref array_die;
20693 unsigned int i;
20694 FOR_EACH_VEC_ELT (*string_types, i, array_die)
20696 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
20697 if (a == NULL)
20698 continue;
20699 dw_loc_descr_ref loc = AT_loc (a);
20700 gcc_assert (loc->dw_loc_opc == DW_OP_call4
20701 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
20702 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
20703 if (ref)
20705 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20706 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
20707 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
20709 else
20711 remove_AT (array_die, DW_AT_string_length);
20712 remove_AT (array_die, dwarf_version >= 5
20713 ? DW_AT_string_length_byte_size
20714 : DW_AT_byte_size);
20719 /* This routine generates DIE for array with hidden descriptor, details
20720 are filled into *info by a langhook. */
20722 static void
20723 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20724 dw_die_ref context_die)
20726 const dw_die_ref scope_die = scope_die_for (type, context_die);
20727 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20728 struct loc_descr_context context = { type, info->base_decl, NULL,
20729 false, false };
20730 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20731 int dim;
20733 add_name_attribute (array_die, type_tag (type));
20734 equate_type_number_to_die (type, array_die);
20736 if (info->ndimensions > 1)
20737 switch (info->ordering)
20739 case array_descr_ordering_row_major:
20740 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20741 break;
20742 case array_descr_ordering_column_major:
20743 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20744 break;
20745 default:
20746 break;
20749 if (dwarf_version >= 3 || !dwarf_strict)
20751 if (info->data_location)
20752 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20753 dw_scalar_form_exprloc, &context);
20754 if (info->associated)
20755 add_scalar_info (array_die, DW_AT_associated, info->associated,
20756 dw_scalar_form_constant
20757 | dw_scalar_form_exprloc
20758 | dw_scalar_form_reference, &context);
20759 if (info->allocated)
20760 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20761 dw_scalar_form_constant
20762 | dw_scalar_form_exprloc
20763 | dw_scalar_form_reference, &context);
20764 if (info->stride)
20766 const enum dwarf_attribute attr
20767 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20768 const int forms
20769 = (info->stride_in_bits)
20770 ? dw_scalar_form_constant
20771 : (dw_scalar_form_constant
20772 | dw_scalar_form_exprloc
20773 | dw_scalar_form_reference);
20775 add_scalar_info (array_die, attr, info->stride, forms, &context);
20778 if (dwarf_version >= 5)
20780 if (info->rank)
20782 add_scalar_info (array_die, DW_AT_rank, info->rank,
20783 dw_scalar_form_constant
20784 | dw_scalar_form_exprloc, &context);
20785 subrange_tag = DW_TAG_generic_subrange;
20786 context.placeholder_arg = true;
20790 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20792 for (dim = 0; dim < info->ndimensions; dim++)
20794 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20796 if (info->dimen[dim].bounds_type)
20797 add_type_attribute (subrange_die,
20798 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20799 false, context_die);
20800 if (info->dimen[dim].lower_bound)
20801 add_bound_info (subrange_die, DW_AT_lower_bound,
20802 info->dimen[dim].lower_bound, &context);
20803 if (info->dimen[dim].upper_bound)
20804 add_bound_info (subrange_die, DW_AT_upper_bound,
20805 info->dimen[dim].upper_bound, &context);
20806 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20807 add_scalar_info (subrange_die, DW_AT_byte_stride,
20808 info->dimen[dim].stride,
20809 dw_scalar_form_constant
20810 | dw_scalar_form_exprloc
20811 | dw_scalar_form_reference,
20812 &context);
20815 gen_type_die (info->element_type, context_die);
20816 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20817 TREE_CODE (type) == ARRAY_TYPE
20818 && TYPE_REVERSE_STORAGE_ORDER (type),
20819 context_die);
20821 if (get_AT (array_die, DW_AT_name))
20822 add_pubtype (type, array_die);
20825 #if 0
20826 static void
20827 gen_entry_point_die (tree decl, dw_die_ref context_die)
20829 tree origin = decl_ultimate_origin (decl);
20830 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20832 if (origin != NULL)
20833 add_abstract_origin_attribute (decl_die, origin);
20834 else
20836 add_name_and_src_coords_attributes (decl_die, decl);
20837 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20838 TYPE_UNQUALIFIED, false, context_die);
20841 if (DECL_ABSTRACT_P (decl))
20842 equate_decl_number_to_die (decl, decl_die);
20843 else
20844 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
20846 #endif
20848 /* Walk through the list of incomplete types again, trying once more to
20849 emit full debugging info for them. */
20851 static void
20852 retry_incomplete_types (void)
20854 set_early_dwarf s;
20855 int i;
20857 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
20858 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
20859 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
20860 vec_safe_truncate (incomplete_types, 0);
20863 /* Determine what tag to use for a record type. */
20865 static enum dwarf_tag
20866 record_type_tag (tree type)
20868 if (! lang_hooks.types.classify_record)
20869 return DW_TAG_structure_type;
20871 switch (lang_hooks.types.classify_record (type))
20873 case RECORD_IS_STRUCT:
20874 return DW_TAG_structure_type;
20876 case RECORD_IS_CLASS:
20877 return DW_TAG_class_type;
20879 case RECORD_IS_INTERFACE:
20880 if (dwarf_version >= 3 || !dwarf_strict)
20881 return DW_TAG_interface_type;
20882 return DW_TAG_structure_type;
20884 default:
20885 gcc_unreachable ();
20889 /* Generate a DIE to represent an enumeration type. Note that these DIEs
20890 include all of the information about the enumeration values also. Each
20891 enumerated type name/value is listed as a child of the enumerated type
20892 DIE. */
20894 static dw_die_ref
20895 gen_enumeration_type_die (tree type, dw_die_ref context_die)
20897 dw_die_ref type_die = lookup_type_die (type);
20899 if (type_die == NULL)
20901 type_die = new_die (DW_TAG_enumeration_type,
20902 scope_die_for (type, context_die), type);
20903 equate_type_number_to_die (type, type_die);
20904 add_name_attribute (type_die, type_tag (type));
20905 if (dwarf_version >= 4 || !dwarf_strict)
20907 if (ENUM_IS_SCOPED (type))
20908 add_AT_flag (type_die, DW_AT_enum_class, 1);
20909 if (ENUM_IS_OPAQUE (type))
20910 add_AT_flag (type_die, DW_AT_declaration, 1);
20913 else if (! TYPE_SIZE (type))
20914 return type_die;
20915 else
20916 remove_AT (type_die, DW_AT_declaration);
20918 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
20919 given enum type is incomplete, do not generate the DW_AT_byte_size
20920 attribute or the DW_AT_element_list attribute. */
20921 if (TYPE_SIZE (type))
20923 tree link;
20925 TREE_ASM_WRITTEN (type) = 1;
20926 add_byte_size_attribute (type_die, type);
20927 if (dwarf_version >= 3 || !dwarf_strict)
20929 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
20930 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
20931 context_die);
20933 if (TYPE_STUB_DECL (type) != NULL_TREE)
20935 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20936 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20939 /* If the first reference to this type was as the return type of an
20940 inline function, then it may not have a parent. Fix this now. */
20941 if (type_die->die_parent == NULL)
20942 add_child_die (scope_die_for (type, context_die), type_die);
20944 for (link = TYPE_VALUES (type);
20945 link != NULL; link = TREE_CHAIN (link))
20947 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
20948 tree value = TREE_VALUE (link);
20950 add_name_attribute (enum_die,
20951 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
20953 if (TREE_CODE (value) == CONST_DECL)
20954 value = DECL_INITIAL (value);
20956 if (simple_type_size_in_bits (TREE_TYPE (value))
20957 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
20959 /* For constant forms created by add_AT_unsigned DWARF
20960 consumers (GDB, elfutils, etc.) always zero extend
20961 the value. Only when the actual value is negative
20962 do we need to use add_AT_int to generate a constant
20963 form that can represent negative values. */
20964 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
20965 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
20966 add_AT_unsigned (enum_die, DW_AT_const_value,
20967 (unsigned HOST_WIDE_INT) val);
20968 else
20969 add_AT_int (enum_die, DW_AT_const_value, val);
20971 else
20972 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
20973 that here. TODO: This should be re-worked to use correct
20974 signed/unsigned double tags for all cases. */
20975 add_AT_wide (enum_die, DW_AT_const_value, value);
20978 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20979 if (TYPE_ARTIFICIAL (type))
20980 add_AT_flag (type_die, DW_AT_artificial, 1);
20982 else
20983 add_AT_flag (type_die, DW_AT_declaration, 1);
20985 add_pubtype (type, type_die);
20987 return type_die;
20990 /* Generate a DIE to represent either a real live formal parameter decl or to
20991 represent just the type of some formal parameter position in some function
20992 type.
20994 Note that this routine is a bit unusual because its argument may be a
20995 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
20996 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
20997 node. If it's the former then this function is being called to output a
20998 DIE to represent a formal parameter object (or some inlining thereof). If
20999 it's the latter, then this function is only being called to output a
21000 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21001 argument type of some subprogram type.
21002 If EMIT_NAME_P is true, name and source coordinate attributes
21003 are emitted. */
21005 static dw_die_ref
21006 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21007 dw_die_ref context_die)
21009 tree node_or_origin = node ? node : origin;
21010 tree ultimate_origin;
21011 dw_die_ref parm_die = NULL;
21013 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21015 parm_die = lookup_decl_die (node);
21017 /* If the contexts differ, we may not be talking about the same
21018 thing. */
21019 if (parm_die && parm_die->die_parent != context_die)
21021 if (!DECL_ABSTRACT_P (node))
21023 /* This can happen when creating an inlined instance, in
21024 which case we need to create a new DIE that will get
21025 annotated with DW_AT_abstract_origin. */
21026 parm_die = NULL;
21028 else
21030 /* FIXME: Reuse DIE even with a differing context.
21032 This can happen when calling
21033 dwarf2out_abstract_function to build debug info for
21034 the abstract instance of a function for which we have
21035 already generated a DIE in
21036 dwarf2out_early_global_decl.
21038 Once we remove dwarf2out_abstract_function, we should
21039 have a call to gcc_unreachable here. */
21043 if (parm_die && parm_die->die_parent == NULL)
21045 /* Check that parm_die already has the right attributes that
21046 we would have added below. If any attributes are
21047 missing, fall through to add them. */
21048 if (! DECL_ABSTRACT_P (node_or_origin)
21049 && !get_AT (parm_die, DW_AT_location)
21050 && !get_AT (parm_die, DW_AT_const_value))
21051 /* We are missing location info, and are about to add it. */
21053 else
21055 add_child_die (context_die, parm_die);
21056 return parm_die;
21061 /* If we have a previously generated DIE, use it, unless this is an
21062 concrete instance (origin != NULL), in which case we need a new
21063 DIE with a corresponding DW_AT_abstract_origin. */
21064 bool reusing_die;
21065 if (parm_die && origin == NULL)
21066 reusing_die = true;
21067 else
21069 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21070 reusing_die = false;
21073 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21075 case tcc_declaration:
21076 ultimate_origin = decl_ultimate_origin (node_or_origin);
21077 if (node || ultimate_origin)
21078 origin = ultimate_origin;
21080 if (reusing_die)
21081 goto add_location;
21083 if (origin != NULL)
21084 add_abstract_origin_attribute (parm_die, origin);
21085 else if (emit_name_p)
21086 add_name_and_src_coords_attributes (parm_die, node);
21087 if (origin == NULL
21088 || (! DECL_ABSTRACT_P (node_or_origin)
21089 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21090 decl_function_context
21091 (node_or_origin))))
21093 tree type = TREE_TYPE (node_or_origin);
21094 if (decl_by_reference_p (node_or_origin))
21095 add_type_attribute (parm_die, TREE_TYPE (type),
21096 TYPE_UNQUALIFIED,
21097 false, context_die);
21098 else
21099 add_type_attribute (parm_die, type,
21100 decl_quals (node_or_origin),
21101 false, context_die);
21103 if (origin == NULL && DECL_ARTIFICIAL (node))
21104 add_AT_flag (parm_die, DW_AT_artificial, 1);
21105 add_location:
21106 if (node && node != origin)
21107 equate_decl_number_to_die (node, parm_die);
21108 if (! DECL_ABSTRACT_P (node_or_origin))
21109 add_location_or_const_value_attribute (parm_die, node_or_origin,
21110 node == NULL);
21112 break;
21114 case tcc_type:
21115 /* We were called with some kind of a ..._TYPE node. */
21116 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21117 context_die);
21118 break;
21120 default:
21121 gcc_unreachable ();
21124 return parm_die;
21127 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21128 children DW_TAG_formal_parameter DIEs representing the arguments of the
21129 parameter pack.
21131 PARM_PACK must be a function parameter pack.
21132 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21133 must point to the subsequent arguments of the function PACK_ARG belongs to.
21134 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21135 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21136 following the last one for which a DIE was generated. */
21138 static dw_die_ref
21139 gen_formal_parameter_pack_die (tree parm_pack,
21140 tree pack_arg,
21141 dw_die_ref subr_die,
21142 tree *next_arg)
21144 tree arg;
21145 dw_die_ref parm_pack_die;
21147 gcc_assert (parm_pack
21148 && lang_hooks.function_parameter_pack_p (parm_pack)
21149 && subr_die);
21151 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21152 add_src_coords_attributes (parm_pack_die, parm_pack);
21154 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21156 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21157 parm_pack))
21158 break;
21159 gen_formal_parameter_die (arg, NULL,
21160 false /* Don't emit name attribute. */,
21161 parm_pack_die);
21163 if (next_arg)
21164 *next_arg = arg;
21165 return parm_pack_die;
21168 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21169 at the end of an (ANSI prototyped) formal parameters list. */
21171 static void
21172 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21174 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21177 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21178 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21179 parameters as specified in some function type specification (except for
21180 those which appear as part of a function *definition*). */
21182 static void
21183 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21185 tree link;
21186 tree formal_type = NULL;
21187 tree first_parm_type;
21188 tree arg;
21190 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21192 arg = DECL_ARGUMENTS (function_or_method_type);
21193 function_or_method_type = TREE_TYPE (function_or_method_type);
21195 else
21196 arg = NULL_TREE;
21198 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21200 /* Make our first pass over the list of formal parameter types and output a
21201 DW_TAG_formal_parameter DIE for each one. */
21202 for (link = first_parm_type; link; )
21204 dw_die_ref parm_die;
21206 formal_type = TREE_VALUE (link);
21207 if (formal_type == void_type_node)
21208 break;
21210 /* Output a (nameless) DIE to represent the formal parameter itself. */
21211 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21213 parm_die = gen_formal_parameter_die (formal_type, NULL,
21214 true /* Emit name attribute. */,
21215 context_die);
21216 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21217 && link == first_parm_type)
21219 add_AT_flag (parm_die, DW_AT_artificial, 1);
21220 if (dwarf_version >= 3 || !dwarf_strict)
21221 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21223 else if (arg && DECL_ARTIFICIAL (arg))
21224 add_AT_flag (parm_die, DW_AT_artificial, 1);
21227 link = TREE_CHAIN (link);
21228 if (arg)
21229 arg = DECL_CHAIN (arg);
21232 /* If this function type has an ellipsis, add a
21233 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21234 if (formal_type != void_type_node)
21235 gen_unspecified_parameters_die (function_or_method_type, context_die);
21237 /* Make our second (and final) pass over the list of formal parameter types
21238 and output DIEs to represent those types (as necessary). */
21239 for (link = TYPE_ARG_TYPES (function_or_method_type);
21240 link && TREE_VALUE (link);
21241 link = TREE_CHAIN (link))
21242 gen_type_die (TREE_VALUE (link), context_die);
21245 /* We want to generate the DIE for TYPE so that we can generate the
21246 die for MEMBER, which has been defined; we will need to refer back
21247 to the member declaration nested within TYPE. If we're trying to
21248 generate minimal debug info for TYPE, processing TYPE won't do the
21249 trick; we need to attach the member declaration by hand. */
21251 static void
21252 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21254 gen_type_die (type, context_die);
21256 /* If we're trying to avoid duplicate debug info, we may not have
21257 emitted the member decl for this function. Emit it now. */
21258 if (TYPE_STUB_DECL (type)
21259 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21260 && ! lookup_decl_die (member))
21262 dw_die_ref type_die;
21263 gcc_assert (!decl_ultimate_origin (member));
21265 push_decl_scope (type);
21266 type_die = lookup_type_die_strip_naming_typedef (type);
21267 if (TREE_CODE (member) == FUNCTION_DECL)
21268 gen_subprogram_die (member, type_die);
21269 else if (TREE_CODE (member) == FIELD_DECL)
21271 /* Ignore the nameless fields that are used to skip bits but handle
21272 C++ anonymous unions and structs. */
21273 if (DECL_NAME (member) != NULL_TREE
21274 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21275 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21277 struct vlr_context vlr_ctx = {
21278 DECL_CONTEXT (member), /* struct_type */
21279 NULL_TREE /* variant_part_offset */
21281 gen_type_die (member_declared_type (member), type_die);
21282 gen_field_die (member, &vlr_ctx, type_die);
21285 else
21286 gen_variable_die (member, NULL_TREE, type_die);
21288 pop_decl_scope ();
21292 /* Forward declare these functions, because they are mutually recursive
21293 with their set_block_* pairing functions. */
21294 static void set_decl_origin_self (tree);
21295 static void set_decl_abstract_flags (tree, vec<tree> &);
21297 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21298 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21299 that it points to the node itself, thus indicating that the node is its
21300 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21301 the given node is NULL, recursively descend the decl/block tree which
21302 it is the root of, and for each other ..._DECL or BLOCK node contained
21303 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21304 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21305 values to point to themselves. */
21307 static void
21308 set_block_origin_self (tree stmt)
21310 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21312 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21315 tree local_decl;
21317 for (local_decl = BLOCK_VARS (stmt);
21318 local_decl != NULL_TREE;
21319 local_decl = DECL_CHAIN (local_decl))
21320 /* Do not recurse on nested functions since the inlining status
21321 of parent and child can be different as per the DWARF spec. */
21322 if (TREE_CODE (local_decl) != FUNCTION_DECL
21323 && !DECL_EXTERNAL (local_decl))
21324 set_decl_origin_self (local_decl);
21328 tree subblock;
21330 for (subblock = BLOCK_SUBBLOCKS (stmt);
21331 subblock != NULL_TREE;
21332 subblock = BLOCK_CHAIN (subblock))
21333 set_block_origin_self (subblock); /* Recurse. */
21338 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21339 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21340 node to so that it points to the node itself, thus indicating that the
21341 node represents its own (abstract) origin. Additionally, if the
21342 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21343 the decl/block tree of which the given node is the root of, and for
21344 each other ..._DECL or BLOCK node contained therein whose
21345 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21346 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21347 point to themselves. */
21349 static void
21350 set_decl_origin_self (tree decl)
21352 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21354 DECL_ABSTRACT_ORIGIN (decl) = decl;
21355 if (TREE_CODE (decl) == FUNCTION_DECL)
21357 tree arg;
21359 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21360 DECL_ABSTRACT_ORIGIN (arg) = arg;
21361 if (DECL_INITIAL (decl) != NULL_TREE
21362 && DECL_INITIAL (decl) != error_mark_node)
21363 set_block_origin_self (DECL_INITIAL (decl));
21368 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21369 and if it wasn't 1 before, push it to abstract_vec vector.
21370 For all local decls and all local sub-blocks (recursively) do it
21371 too. */
21373 static void
21374 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21376 tree local_decl;
21377 tree subblock;
21378 unsigned int i;
21380 if (!BLOCK_ABSTRACT (stmt))
21382 abstract_vec.safe_push (stmt);
21383 BLOCK_ABSTRACT (stmt) = 1;
21386 for (local_decl = BLOCK_VARS (stmt);
21387 local_decl != NULL_TREE;
21388 local_decl = DECL_CHAIN (local_decl))
21389 if (! DECL_EXTERNAL (local_decl))
21390 set_decl_abstract_flags (local_decl, abstract_vec);
21392 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21394 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21395 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21396 || TREE_CODE (local_decl) == PARM_DECL)
21397 set_decl_abstract_flags (local_decl, abstract_vec);
21400 for (subblock = BLOCK_SUBBLOCKS (stmt);
21401 subblock != NULL_TREE;
21402 subblock = BLOCK_CHAIN (subblock))
21403 set_block_abstract_flags (subblock, abstract_vec);
21406 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21407 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21408 In the case where the decl is a FUNCTION_DECL also set the abstract
21409 flags for all of the parameters, local vars, local
21410 blocks and sub-blocks (recursively). */
21412 static void
21413 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21415 if (!DECL_ABSTRACT_P (decl))
21417 abstract_vec.safe_push (decl);
21418 DECL_ABSTRACT_P (decl) = 1;
21421 if (TREE_CODE (decl) == FUNCTION_DECL)
21423 tree arg;
21425 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21426 if (!DECL_ABSTRACT_P (arg))
21428 abstract_vec.safe_push (arg);
21429 DECL_ABSTRACT_P (arg) = 1;
21431 if (DECL_INITIAL (decl) != NULL_TREE
21432 && DECL_INITIAL (decl) != error_mark_node)
21433 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21437 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21438 may later generate inlined and/or out-of-line instances of.
21440 FIXME: In the early-dwarf world, this function, and most of the
21441 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21442 the abstract instance. All we would need to do is annotate
21443 the early DIE with the appropriate DW_AT_inline in late
21444 dwarf (perhaps in gen_inlined_subroutine_die).
21446 However, we can't do this yet, because LTO streaming of DIEs
21447 has not been implemented yet. */
21449 static void
21450 dwarf2out_abstract_function (tree decl)
21452 dw_die_ref old_die;
21453 tree save_fn;
21454 tree context;
21455 hash_table<decl_loc_hasher> *old_decl_loc_table;
21456 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21457 int old_call_site_count, old_tail_call_site_count;
21458 struct call_arg_loc_node *old_call_arg_locations;
21460 /* Make sure we have the actual abstract inline, not a clone. */
21461 decl = DECL_ORIGIN (decl);
21463 old_die = lookup_decl_die (decl);
21464 if (old_die && get_AT (old_die, DW_AT_inline))
21465 /* We've already generated the abstract instance. */
21466 return;
21468 /* We can be called while recursively when seeing block defining inlined subroutine
21469 DIE. Be sure to not clobber the outer location table nor use it or we would
21470 get locations in abstract instantces. */
21471 old_decl_loc_table = decl_loc_table;
21472 decl_loc_table = NULL;
21473 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21474 cached_dw_loc_list_table = NULL;
21475 old_call_arg_locations = call_arg_locations;
21476 call_arg_locations = NULL;
21477 old_call_site_count = call_site_count;
21478 call_site_count = -1;
21479 old_tail_call_site_count = tail_call_site_count;
21480 tail_call_site_count = -1;
21482 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21483 we don't get confused by DECL_ABSTRACT_P. */
21484 if (debug_info_level > DINFO_LEVEL_TERSE)
21486 context = decl_class_context (decl);
21487 if (context)
21488 gen_type_die_for_member
21489 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21492 /* Pretend we've just finished compiling this function. */
21493 save_fn = current_function_decl;
21494 current_function_decl = decl;
21496 auto_vec<tree, 64> abstract_vec;
21497 set_decl_abstract_flags (decl, abstract_vec);
21498 dwarf2out_decl (decl);
21499 unsigned int i;
21500 tree t;
21501 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21502 if (TREE_CODE (t) == BLOCK)
21503 BLOCK_ABSTRACT (t) = 0;
21504 else
21505 DECL_ABSTRACT_P (t) = 0;
21507 current_function_decl = save_fn;
21508 decl_loc_table = old_decl_loc_table;
21509 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21510 call_arg_locations = old_call_arg_locations;
21511 call_site_count = old_call_site_count;
21512 tail_call_site_count = old_tail_call_site_count;
21515 /* Helper function of premark_used_types() which gets called through
21516 htab_traverse.
21518 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21519 marked as unused by prune_unused_types. */
21521 bool
21522 premark_used_types_helper (tree const &type, void *)
21524 dw_die_ref die;
21526 die = lookup_type_die (type);
21527 if (die != NULL)
21528 die->die_perennial_p = 1;
21529 return true;
21532 /* Helper function of premark_types_used_by_global_vars which gets called
21533 through htab_traverse.
21535 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21536 marked as unused by prune_unused_types. The DIE of the type is marked
21537 only if the global variable using the type will actually be emitted. */
21540 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21541 void *)
21543 struct types_used_by_vars_entry *entry;
21544 dw_die_ref die;
21546 entry = (struct types_used_by_vars_entry *) *slot;
21547 gcc_assert (entry->type != NULL
21548 && entry->var_decl != NULL);
21549 die = lookup_type_die (entry->type);
21550 if (die)
21552 /* Ask cgraph if the global variable really is to be emitted.
21553 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21554 varpool_node *node = varpool_node::get (entry->var_decl);
21555 if (node && node->definition)
21557 die->die_perennial_p = 1;
21558 /* Keep the parent DIEs as well. */
21559 while ((die = die->die_parent) && die->die_perennial_p == 0)
21560 die->die_perennial_p = 1;
21563 return 1;
21566 /* Mark all members of used_types_hash as perennial. */
21568 static void
21569 premark_used_types (struct function *fun)
21571 if (fun && fun->used_types_hash)
21572 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21575 /* Mark all members of types_used_by_vars_entry as perennial. */
21577 static void
21578 premark_types_used_by_global_vars (void)
21580 if (types_used_by_vars_hash)
21581 types_used_by_vars_hash
21582 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21585 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21586 for CA_LOC call arg loc node. */
21588 static dw_die_ref
21589 gen_call_site_die (tree decl, dw_die_ref subr_die,
21590 struct call_arg_loc_node *ca_loc)
21592 dw_die_ref stmt_die = NULL, die;
21593 tree block = ca_loc->block;
21595 while (block
21596 && block != DECL_INITIAL (decl)
21597 && TREE_CODE (block) == BLOCK)
21599 stmt_die = BLOCK_DIE (block);
21600 if (stmt_die)
21601 break;
21602 block = BLOCK_SUPERCONTEXT (block);
21604 if (stmt_die == NULL)
21605 stmt_die = subr_die;
21606 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21607 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21608 if (ca_loc->tail_call_p)
21609 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21610 if (ca_loc->symbol_ref)
21612 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21613 if (tdie)
21614 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21615 else
21616 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21617 false);
21619 return die;
21622 /* Generate a DIE to represent a declared function (either file-scope or
21623 block-local). */
21625 static void
21626 gen_subprogram_die (tree decl, dw_die_ref context_die)
21628 tree origin = decl_ultimate_origin (decl);
21629 dw_die_ref subr_die;
21630 dw_die_ref old_die = lookup_decl_die (decl);
21632 /* This function gets called multiple times for different stages of
21633 the debug process. For example, for func() in this code:
21635 namespace S
21637 void func() { ... }
21640 ...we get called 4 times. Twice in early debug and twice in
21641 late debug:
21643 Early debug
21644 -----------
21646 1. Once while generating func() within the namespace. This is
21647 the declaration. The declaration bit below is set, as the
21648 context is the namespace.
21650 A new DIE will be generated with DW_AT_declaration set.
21652 2. Once for func() itself. This is the specification. The
21653 declaration bit below is clear as the context is the CU.
21655 We will use the cached DIE from (1) to create a new DIE with
21656 DW_AT_specification pointing to the declaration in (1).
21658 Late debug via rest_of_handle_final()
21659 -------------------------------------
21661 3. Once generating func() within the namespace. This is also the
21662 declaration, as in (1), but this time we will early exit below
21663 as we have a cached DIE and a declaration needs no additional
21664 annotations (no locations), as the source declaration line
21665 info is enough.
21667 4. Once for func() itself. As in (2), this is the specification,
21668 but this time we will re-use the cached DIE, and just annotate
21669 it with the location information that should now be available.
21671 For something without namespaces, but with abstract instances, we
21672 are also called a multiple times:
21674 class Base
21676 public:
21677 Base (); // constructor declaration (1)
21680 Base::Base () { } // constructor specification (2)
21682 Early debug
21683 -----------
21685 1. Once for the Base() constructor by virtue of it being a
21686 member of the Base class. This is done via
21687 rest_of_type_compilation.
21689 This is a declaration, so a new DIE will be created with
21690 DW_AT_declaration.
21692 2. Once for the Base() constructor definition, but this time
21693 while generating the abstract instance of the base
21694 constructor (__base_ctor) which is being generated via early
21695 debug of reachable functions.
21697 Even though we have a cached version of the declaration (1),
21698 we will create a DW_AT_specification of the declaration DIE
21699 in (1).
21701 3. Once for the __base_ctor itself, but this time, we generate
21702 an DW_AT_abstract_origin version of the DW_AT_specification in
21703 (2).
21705 Late debug via rest_of_handle_final
21706 -----------------------------------
21708 4. One final time for the __base_ctor (which will have a cached
21709 DIE with DW_AT_abstract_origin created in (3). This time,
21710 we will just annotate the location information now
21711 available.
21713 int declaration = (current_function_decl != decl
21714 || class_or_namespace_scope_p (context_die));
21716 /* Now that the C++ front end lazily declares artificial member fns, we
21717 might need to retrofit the declaration into its class. */
21718 if (!declaration && !origin && !old_die
21719 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21720 && !class_or_namespace_scope_p (context_die)
21721 && debug_info_level > DINFO_LEVEL_TERSE)
21722 old_die = force_decl_die (decl);
21724 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21725 if (origin != NULL)
21727 gcc_assert (!declaration || local_scope_p (context_die));
21729 /* Fixup die_parent for the abstract instance of a nested
21730 inline function. */
21731 if (old_die && old_die->die_parent == NULL)
21732 add_child_die (context_die, old_die);
21734 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21736 /* If we have a DW_AT_abstract_origin we have a working
21737 cached version. */
21738 subr_die = old_die;
21740 else
21742 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21743 add_abstract_origin_attribute (subr_die, origin);
21744 /* This is where the actual code for a cloned function is.
21745 Let's emit linkage name attribute for it. This helps
21746 debuggers to e.g, set breakpoints into
21747 constructors/destructors when the user asks "break
21748 K::K". */
21749 add_linkage_name (subr_die, decl);
21752 /* A cached copy, possibly from early dwarf generation. Reuse as
21753 much as possible. */
21754 else if (old_die)
21756 /* A declaration that has been previously dumped needs no
21757 additional information. */
21758 if (declaration)
21759 return;
21761 if (!get_AT_flag (old_die, DW_AT_declaration)
21762 /* We can have a normal definition following an inline one in the
21763 case of redefinition of GNU C extern inlines.
21764 It seems reasonable to use AT_specification in this case. */
21765 && !get_AT (old_die, DW_AT_inline))
21767 /* Detect and ignore this case, where we are trying to output
21768 something we have already output. */
21769 if (get_AT (old_die, DW_AT_low_pc)
21770 || get_AT (old_die, DW_AT_ranges))
21771 return;
21773 /* If we have no location information, this must be a
21774 partially generated DIE from early dwarf generation.
21775 Fall through and generate it. */
21778 /* If the definition comes from the same place as the declaration,
21779 maybe use the old DIE. We always want the DIE for this function
21780 that has the *_pc attributes to be under comp_unit_die so the
21781 debugger can find it. We also need to do this for abstract
21782 instances of inlines, since the spec requires the out-of-line copy
21783 to have the same parent. For local class methods, this doesn't
21784 apply; we just use the old DIE. */
21785 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21786 struct dwarf_file_data * file_index = lookup_filename (s.file);
21787 if ((is_cu_die (old_die->die_parent)
21788 /* This condition fixes the inconsistency/ICE with the
21789 following Fortran test (or some derivative thereof) while
21790 building libgfortran:
21792 module some_m
21793 contains
21794 logical function funky (FLAG)
21795 funky = .true.
21796 end function
21797 end module
21799 || (old_die->die_parent
21800 && old_die->die_parent->die_tag == DW_TAG_module)
21801 || context_die == NULL)
21802 && (DECL_ARTIFICIAL (decl)
21803 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21804 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21805 == (unsigned) s.line))))
21807 subr_die = old_die;
21809 /* Clear out the declaration attribute, but leave the
21810 parameters so they can be augmented with location
21811 information later. Unless this was a declaration, in
21812 which case, wipe out the nameless parameters and recreate
21813 them further down. */
21814 if (remove_AT (subr_die, DW_AT_declaration))
21817 remove_AT (subr_die, DW_AT_object_pointer);
21818 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21821 /* Make a specification pointing to the previously built
21822 declaration. */
21823 else
21825 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21826 add_AT_specification (subr_die, old_die);
21827 add_pubname (decl, subr_die);
21828 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21829 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21830 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21831 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21833 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21834 emit the real type on the definition die. */
21835 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
21837 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
21838 if (die == auto_die || die == decltype_auto_die)
21839 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21840 TYPE_UNQUALIFIED, false, context_die);
21843 /* When we process the method declaration, we haven't seen
21844 the out-of-class defaulted definition yet, so we have to
21845 recheck now. */
21846 if ((dwarf_version >= 5 || ! dwarf_strict)
21847 && !get_AT (subr_die, DW_AT_defaulted))
21849 int defaulted
21850 = lang_hooks.decls.decl_dwarf_attribute (decl,
21851 DW_AT_defaulted);
21852 if (defaulted != -1)
21854 /* Other values must have been handled before. */
21855 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
21856 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21861 /* Create a fresh DIE for anything else. */
21862 else
21864 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21866 if (TREE_PUBLIC (decl))
21867 add_AT_flag (subr_die, DW_AT_external, 1);
21869 add_name_and_src_coords_attributes (subr_die, decl);
21870 add_pubname (decl, subr_die);
21871 if (debug_info_level > DINFO_LEVEL_TERSE)
21873 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
21874 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21875 TYPE_UNQUALIFIED, false, context_die);
21878 add_pure_or_virtual_attribute (subr_die, decl);
21879 if (DECL_ARTIFICIAL (decl))
21880 add_AT_flag (subr_die, DW_AT_artificial, 1);
21882 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
21883 add_AT_flag (subr_die, DW_AT_noreturn, 1);
21885 add_accessibility_attribute (subr_die, decl);
21888 /* Unless we have an existing non-declaration DIE, equate the new
21889 DIE. */
21890 if (!old_die || is_declaration_die (old_die))
21891 equate_decl_number_to_die (decl, subr_die);
21893 if (declaration)
21895 if (!old_die || !get_AT (old_die, DW_AT_inline))
21897 add_AT_flag (subr_die, DW_AT_declaration, 1);
21899 /* If this is an explicit function declaration then generate
21900 a DW_AT_explicit attribute. */
21901 if ((dwarf_version >= 3 || !dwarf_strict)
21902 && lang_hooks.decls.decl_dwarf_attribute (decl,
21903 DW_AT_explicit) == 1)
21904 add_AT_flag (subr_die, DW_AT_explicit, 1);
21906 /* If this is a C++11 deleted special function member then generate
21907 a DW_AT_deleted attribute. */
21908 if ((dwarf_version >= 5 || !dwarf_strict)
21909 && lang_hooks.decls.decl_dwarf_attribute (decl,
21910 DW_AT_deleted) == 1)
21911 add_AT_flag (subr_die, DW_AT_deleted, 1);
21913 /* If this is a C++11 defaulted special function member then
21914 generate a DW_AT_defaulted attribute. */
21915 if (dwarf_version >= 5 || !dwarf_strict)
21917 int defaulted
21918 = lang_hooks.decls.decl_dwarf_attribute (decl,
21919 DW_AT_defaulted);
21920 if (defaulted != -1)
21921 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21924 /* If this is a C++11 non-static member function with & ref-qualifier
21925 then generate a DW_AT_reference attribute. */
21926 if ((dwarf_version >= 5 || !dwarf_strict)
21927 && lang_hooks.decls.decl_dwarf_attribute (decl,
21928 DW_AT_reference) == 1)
21929 add_AT_flag (subr_die, DW_AT_reference, 1);
21931 /* If this is a C++11 non-static member function with &&
21932 ref-qualifier then generate a DW_AT_reference attribute. */
21933 if ((dwarf_version >= 5 || !dwarf_strict)
21934 && lang_hooks.decls.decl_dwarf_attribute (decl,
21935 DW_AT_rvalue_reference)
21936 == 1)
21937 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
21940 /* Tag abstract instances with DW_AT_inline. */
21941 else if (DECL_ABSTRACT_P (decl))
21943 if (DECL_DECLARED_INLINE_P (decl))
21945 if (cgraph_function_possibly_inlined_p (decl))
21946 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
21947 else
21948 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
21950 else
21952 if (cgraph_function_possibly_inlined_p (decl))
21953 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
21954 else
21955 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
21958 if (DECL_DECLARED_INLINE_P (decl)
21959 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21960 add_AT_flag (subr_die, DW_AT_artificial, 1);
21962 /* For non DECL_EXTERNALs, if range information is available, fill
21963 the DIE with it. */
21964 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
21966 HOST_WIDE_INT cfa_fb_offset;
21968 struct function *fun = DECL_STRUCT_FUNCTION (decl);
21970 if (!flag_reorder_blocks_and_partition)
21972 dw_fde_ref fde = fun->fde;
21973 if (fde->dw_fde_begin)
21975 /* We have already generated the labels. */
21976 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
21977 fde->dw_fde_end, false);
21979 else
21981 /* Create start/end labels and add the range. */
21982 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
21983 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
21984 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
21985 current_function_funcdef_no);
21986 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
21987 current_function_funcdef_no);
21988 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
21989 false);
21992 #if VMS_DEBUGGING_INFO
21993 /* HP OpenVMS Industry Standard 64: DWARF Extensions
21994 Section 2.3 Prologue and Epilogue Attributes:
21995 When a breakpoint is set on entry to a function, it is generally
21996 desirable for execution to be suspended, not on the very first
21997 instruction of the function, but rather at a point after the
21998 function's frame has been set up, after any language defined local
21999 declaration processing has been completed, and before execution of
22000 the first statement of the function begins. Debuggers generally
22001 cannot properly determine where this point is. Similarly for a
22002 breakpoint set on exit from a function. The prologue and epilogue
22003 attributes allow a compiler to communicate the location(s) to use. */
22006 if (fde->dw_fde_vms_end_prologue)
22007 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22008 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22010 if (fde->dw_fde_vms_begin_epilogue)
22011 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22012 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22014 #endif
22017 else
22019 /* Generate pubnames entries for the split function code ranges. */
22020 dw_fde_ref fde = fun->fde;
22022 if (fde->dw_fde_second_begin)
22024 if (dwarf_version >= 3 || !dwarf_strict)
22026 /* We should use ranges for non-contiguous code section
22027 addresses. Use the actual code range for the initial
22028 section, since the HOT/COLD labels might precede an
22029 alignment offset. */
22030 bool range_list_added = false;
22031 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22032 fde->dw_fde_end, &range_list_added,
22033 false);
22034 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22035 fde->dw_fde_second_end,
22036 &range_list_added, false);
22037 if (range_list_added)
22038 add_ranges (NULL);
22040 else
22042 /* There is no real support in DW2 for this .. so we make
22043 a work-around. First, emit the pub name for the segment
22044 containing the function label. Then make and emit a
22045 simplified subprogram DIE for the second segment with the
22046 name pre-fixed by __hot/cold_sect_of_. We use the same
22047 linkage name for the second die so that gdb will find both
22048 sections when given "b foo". */
22049 const char *name = NULL;
22050 tree decl_name = DECL_NAME (decl);
22051 dw_die_ref seg_die;
22053 /* Do the 'primary' section. */
22054 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22055 fde->dw_fde_end, false);
22057 /* Build a minimal DIE for the secondary section. */
22058 seg_die = new_die (DW_TAG_subprogram,
22059 subr_die->die_parent, decl);
22061 if (TREE_PUBLIC (decl))
22062 add_AT_flag (seg_die, DW_AT_external, 1);
22064 if (decl_name != NULL
22065 && IDENTIFIER_POINTER (decl_name) != NULL)
22067 name = dwarf2_name (decl, 1);
22068 if (! DECL_ARTIFICIAL (decl))
22069 add_src_coords_attributes (seg_die, decl);
22071 add_linkage_name (seg_die, decl);
22073 gcc_assert (name != NULL);
22074 add_pure_or_virtual_attribute (seg_die, decl);
22075 if (DECL_ARTIFICIAL (decl))
22076 add_AT_flag (seg_die, DW_AT_artificial, 1);
22078 name = concat ("__second_sect_of_", name, NULL);
22079 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22080 fde->dw_fde_second_end, false);
22081 add_name_attribute (seg_die, name);
22082 if (want_pubnames ())
22083 add_pubname_string (name, seg_die);
22086 else
22087 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22088 false);
22091 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22093 /* We define the "frame base" as the function's CFA. This is more
22094 convenient for several reasons: (1) It's stable across the prologue
22095 and epilogue, which makes it better than just a frame pointer,
22096 (2) With dwarf3, there exists a one-byte encoding that allows us
22097 to reference the .debug_frame data by proxy, but failing that,
22098 (3) We can at least reuse the code inspection and interpretation
22099 code that determines the CFA position at various points in the
22100 function. */
22101 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22103 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22104 add_AT_loc (subr_die, DW_AT_frame_base, op);
22106 else
22108 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22109 if (list->dw_loc_next)
22110 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22111 else
22112 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22115 /* Compute a displacement from the "steady-state frame pointer" to
22116 the CFA. The former is what all stack slots and argument slots
22117 will reference in the rtl; the latter is what we've told the
22118 debugger about. We'll need to adjust all frame_base references
22119 by this displacement. */
22120 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22122 if (fun->static_chain_decl)
22124 /* DWARF requires here a location expression that computes the
22125 address of the enclosing subprogram's frame base. The machinery
22126 in tree-nested.c is supposed to store this specific address in the
22127 last field of the FRAME record. */
22128 const tree frame_type
22129 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22130 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22132 tree fb_expr
22133 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22134 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22135 fb_expr, fb_decl, NULL_TREE);
22137 add_AT_location_description (subr_die, DW_AT_static_link,
22138 loc_list_from_tree (fb_expr, 0, NULL));
22142 /* Generate child dies for template paramaters. */
22143 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22144 gen_generic_params_dies (decl);
22146 /* Now output descriptions of the arguments for this function. This gets
22147 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22148 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22149 `...' at the end of the formal parameter list. In order to find out if
22150 there was a trailing ellipsis or not, we must instead look at the type
22151 associated with the FUNCTION_DECL. This will be a node of type
22152 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22153 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22154 an ellipsis at the end. */
22156 /* In the case where we are describing a mere function declaration, all we
22157 need to do here (and all we *can* do here) is to describe the *types* of
22158 its formal parameters. */
22159 if (debug_info_level <= DINFO_LEVEL_TERSE)
22161 else if (declaration)
22162 gen_formal_types_die (decl, subr_die);
22163 else
22165 /* Generate DIEs to represent all known formal parameters. */
22166 tree parm = DECL_ARGUMENTS (decl);
22167 tree generic_decl = early_dwarf
22168 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22169 tree generic_decl_parm = generic_decl
22170 ? DECL_ARGUMENTS (generic_decl)
22171 : NULL;
22172 auto_vec<dw_die_ref> string_types_vec;
22173 if (string_types == NULL)
22174 string_types = &string_types_vec;
22176 /* Now we want to walk the list of parameters of the function and
22177 emit their relevant DIEs.
22179 We consider the case of DECL being an instance of a generic function
22180 as well as it being a normal function.
22182 If DECL is an instance of a generic function we walk the
22183 parameters of the generic function declaration _and_ the parameters of
22184 DECL itself. This is useful because we want to emit specific DIEs for
22185 function parameter packs and those are declared as part of the
22186 generic function declaration. In that particular case,
22187 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22188 That DIE has children DIEs representing the set of arguments
22189 of the pack. Note that the set of pack arguments can be empty.
22190 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22191 children DIE.
22193 Otherwise, we just consider the parameters of DECL. */
22194 while (generic_decl_parm || parm)
22196 if (generic_decl_parm
22197 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22198 gen_formal_parameter_pack_die (generic_decl_parm,
22199 parm, subr_die,
22200 &parm);
22201 else if (parm && !POINTER_BOUNDS_P (parm))
22203 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22205 if (parm == DECL_ARGUMENTS (decl)
22206 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22207 && parm_die
22208 && (dwarf_version >= 3 || !dwarf_strict))
22209 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22211 parm = DECL_CHAIN (parm);
22213 else if (parm)
22214 parm = DECL_CHAIN (parm);
22216 if (generic_decl_parm)
22217 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22220 /* Decide whether we need an unspecified_parameters DIE at the end.
22221 There are 2 more cases to do this for: 1) the ansi ... declaration -
22222 this is detectable when the end of the arg list is not a
22223 void_type_node 2) an unprototyped function declaration (not a
22224 definition). This just means that we have no info about the
22225 parameters at all. */
22226 if (early_dwarf)
22228 if (prototype_p (TREE_TYPE (decl)))
22230 /* This is the prototyped case, check for.... */
22231 if (stdarg_p (TREE_TYPE (decl)))
22232 gen_unspecified_parameters_die (decl, subr_die);
22234 else if (DECL_INITIAL (decl) == NULL_TREE)
22235 gen_unspecified_parameters_die (decl, subr_die);
22238 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
22239 have DIEs. */
22240 if (string_types == &string_types_vec)
22242 adjust_string_types ();
22243 string_types = NULL;
22247 if (subr_die != old_die)
22248 /* Add the calling convention attribute if requested. */
22249 add_calling_convention_attribute (subr_die, decl);
22251 /* Output Dwarf info for all of the stuff within the body of the function
22252 (if it has one - it may be just a declaration).
22254 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22255 a function. This BLOCK actually represents the outermost binding contour
22256 for the function, i.e. the contour in which the function's formal
22257 parameters and labels get declared. Curiously, it appears that the front
22258 end doesn't actually put the PARM_DECL nodes for the current function onto
22259 the BLOCK_VARS list for this outer scope, but are strung off of the
22260 DECL_ARGUMENTS list for the function instead.
22262 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22263 the LABEL_DECL nodes for the function however, and we output DWARF info
22264 for those in decls_for_scope. Just within the `outer_scope' there will be
22265 a BLOCK node representing the function's outermost pair of curly braces,
22266 and any blocks used for the base and member initializers of a C++
22267 constructor function. */
22268 tree outer_scope = DECL_INITIAL (decl);
22269 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22271 int call_site_note_count = 0;
22272 int tail_call_site_note_count = 0;
22274 /* Emit a DW_TAG_variable DIE for a named return value. */
22275 if (DECL_NAME (DECL_RESULT (decl)))
22276 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22278 /* The first time through decls_for_scope we will generate the
22279 DIEs for the locals. The second time, we fill in the
22280 location info. */
22281 decls_for_scope (outer_scope, subr_die);
22283 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22285 struct call_arg_loc_node *ca_loc;
22286 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22288 dw_die_ref die = NULL;
22289 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22290 rtx arg, next_arg;
22292 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22293 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22294 : NULL_RTX);
22295 arg; arg = next_arg)
22297 dw_loc_descr_ref reg, val;
22298 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22299 dw_die_ref cdie, tdie = NULL;
22301 next_arg = XEXP (arg, 1);
22302 if (REG_P (XEXP (XEXP (arg, 0), 0))
22303 && next_arg
22304 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22305 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22306 && REGNO (XEXP (XEXP (arg, 0), 0))
22307 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22308 next_arg = XEXP (next_arg, 1);
22309 if (mode == VOIDmode)
22311 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22312 if (mode == VOIDmode)
22313 mode = GET_MODE (XEXP (arg, 0));
22315 if (mode == VOIDmode || mode == BLKmode)
22316 continue;
22317 /* Get dynamic information about call target only if we
22318 have no static information: we cannot generate both
22319 DW_AT_call_origin and DW_AT_call_target
22320 attributes. */
22321 if (ca_loc->symbol_ref == NULL_RTX)
22323 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22325 tloc = XEXP (XEXP (arg, 0), 1);
22326 continue;
22328 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22329 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22331 tlocc = XEXP (XEXP (arg, 0), 1);
22332 continue;
22335 reg = NULL;
22336 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22337 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22338 VAR_INIT_STATUS_INITIALIZED);
22339 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22341 rtx mem = XEXP (XEXP (arg, 0), 0);
22342 reg = mem_loc_descriptor (XEXP (mem, 0),
22343 get_address_mode (mem),
22344 GET_MODE (mem),
22345 VAR_INIT_STATUS_INITIALIZED);
22347 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22348 == DEBUG_PARAMETER_REF)
22350 tree tdecl
22351 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22352 tdie = lookup_decl_die (tdecl);
22353 if (tdie == NULL)
22354 continue;
22356 else
22357 continue;
22358 if (reg == NULL
22359 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22360 != DEBUG_PARAMETER_REF)
22361 continue;
22362 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22363 VOIDmode,
22364 VAR_INIT_STATUS_INITIALIZED);
22365 if (val == NULL)
22366 continue;
22367 if (die == NULL)
22368 die = gen_call_site_die (decl, subr_die, ca_loc);
22369 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22370 NULL_TREE);
22371 if (reg != NULL)
22372 add_AT_loc (cdie, DW_AT_location, reg);
22373 else if (tdie != NULL)
22374 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22375 tdie);
22376 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22377 if (next_arg != XEXP (arg, 1))
22379 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22380 if (mode == VOIDmode)
22381 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22382 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22383 0), 1),
22384 mode, VOIDmode,
22385 VAR_INIT_STATUS_INITIALIZED);
22386 if (val != NULL)
22387 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22388 val);
22391 if (die == NULL
22392 && (ca_loc->symbol_ref || tloc))
22393 die = gen_call_site_die (decl, subr_die, ca_loc);
22394 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22396 dw_loc_descr_ref tval = NULL;
22398 if (tloc != NULL_RTX)
22399 tval = mem_loc_descriptor (tloc,
22400 GET_MODE (tloc) == VOIDmode
22401 ? Pmode : GET_MODE (tloc),
22402 VOIDmode,
22403 VAR_INIT_STATUS_INITIALIZED);
22404 if (tval)
22405 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22406 else if (tlocc != NULL_RTX)
22408 tval = mem_loc_descriptor (tlocc,
22409 GET_MODE (tlocc) == VOIDmode
22410 ? Pmode : GET_MODE (tlocc),
22411 VOIDmode,
22412 VAR_INIT_STATUS_INITIALIZED);
22413 if (tval)
22414 add_AT_loc (die,
22415 dwarf_AT (DW_AT_call_target_clobbered),
22416 tval);
22419 if (die != NULL)
22421 call_site_note_count++;
22422 if (ca_loc->tail_call_p)
22423 tail_call_site_note_count++;
22427 call_arg_locations = NULL;
22428 call_arg_loc_last = NULL;
22429 if (tail_call_site_count >= 0
22430 && tail_call_site_count == tail_call_site_note_count
22431 && (!dwarf_strict || dwarf_version >= 5))
22433 if (call_site_count >= 0
22434 && call_site_count == call_site_note_count)
22435 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22436 else
22437 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22439 call_site_count = -1;
22440 tail_call_site_count = -1;
22443 /* Mark used types after we have created DIEs for the functions scopes. */
22444 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22447 /* Returns a hash value for X (which really is a die_struct). */
22449 hashval_t
22450 block_die_hasher::hash (die_struct *d)
22452 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22455 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22456 as decl_id and die_parent of die_struct Y. */
22458 bool
22459 block_die_hasher::equal (die_struct *x, die_struct *y)
22461 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22464 /* Return TRUE if DECL, which may have been previously generated as
22465 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22466 true if decl (or its origin) is either an extern declaration or a
22467 class/namespace scoped declaration.
22469 The declare_in_namespace support causes us to get two DIEs for one
22470 variable, both of which are declarations. We want to avoid
22471 considering one to be a specification, so we must test for
22472 DECLARATION and DW_AT_declaration. */
22473 static inline bool
22474 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22476 return (old_die && TREE_STATIC (decl) && !declaration
22477 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22480 /* Return true if DECL is a local static. */
22482 static inline bool
22483 local_function_static (tree decl)
22485 gcc_assert (VAR_P (decl));
22486 return TREE_STATIC (decl)
22487 && DECL_CONTEXT (decl)
22488 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22491 /* Generate a DIE to represent a declared data object.
22492 Either DECL or ORIGIN must be non-null. */
22494 static void
22495 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22497 HOST_WIDE_INT off = 0;
22498 tree com_decl;
22499 tree decl_or_origin = decl ? decl : origin;
22500 tree ultimate_origin;
22501 dw_die_ref var_die;
22502 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22503 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22504 || class_or_namespace_scope_p (context_die));
22505 bool specialization_p = false;
22506 bool no_linkage_name = false;
22508 /* While C++ inline static data members have definitions inside of the
22509 class, force the first DIE to be a declaration, then let gen_member_die
22510 reparent it to the class context and call gen_variable_die again
22511 to create the outside of the class DIE for the definition. */
22512 if (!declaration
22513 && old_die == NULL
22514 && decl
22515 && DECL_CONTEXT (decl)
22516 && TYPE_P (DECL_CONTEXT (decl))
22517 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22519 declaration = true;
22520 no_linkage_name = true;
22523 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22524 if (decl || ultimate_origin)
22525 origin = ultimate_origin;
22526 com_decl = fortran_common (decl_or_origin, &off);
22528 /* Symbol in common gets emitted as a child of the common block, in the form
22529 of a data member. */
22530 if (com_decl)
22532 dw_die_ref com_die;
22533 dw_loc_list_ref loc = NULL;
22534 die_node com_die_arg;
22536 var_die = lookup_decl_die (decl_or_origin);
22537 if (var_die)
22539 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22541 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22542 if (loc)
22544 if (off)
22546 /* Optimize the common case. */
22547 if (single_element_loc_list_p (loc)
22548 && loc->expr->dw_loc_opc == DW_OP_addr
22549 && loc->expr->dw_loc_next == NULL
22550 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22551 == SYMBOL_REF)
22553 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22554 loc->expr->dw_loc_oprnd1.v.val_addr
22555 = plus_constant (GET_MODE (x), x , off);
22557 else
22558 loc_list_plus_const (loc, off);
22560 add_AT_location_description (var_die, DW_AT_location, loc);
22561 remove_AT (var_die, DW_AT_declaration);
22564 return;
22567 if (common_block_die_table == NULL)
22568 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22570 com_die_arg.decl_id = DECL_UID (com_decl);
22571 com_die_arg.die_parent = context_die;
22572 com_die = common_block_die_table->find (&com_die_arg);
22573 if (! early_dwarf)
22574 loc = loc_list_from_tree (com_decl, 2, NULL);
22575 if (com_die == NULL)
22577 const char *cnam
22578 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22579 die_node **slot;
22581 com_die = new_die (DW_TAG_common_block, context_die, decl);
22582 add_name_and_src_coords_attributes (com_die, com_decl);
22583 if (loc)
22585 add_AT_location_description (com_die, DW_AT_location, loc);
22586 /* Avoid sharing the same loc descriptor between
22587 DW_TAG_common_block and DW_TAG_variable. */
22588 loc = loc_list_from_tree (com_decl, 2, NULL);
22590 else if (DECL_EXTERNAL (decl_or_origin))
22591 add_AT_flag (com_die, DW_AT_declaration, 1);
22592 if (want_pubnames ())
22593 add_pubname_string (cnam, com_die); /* ??? needed? */
22594 com_die->decl_id = DECL_UID (com_decl);
22595 slot = common_block_die_table->find_slot (com_die, INSERT);
22596 *slot = com_die;
22598 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22600 add_AT_location_description (com_die, DW_AT_location, loc);
22601 loc = loc_list_from_tree (com_decl, 2, NULL);
22602 remove_AT (com_die, DW_AT_declaration);
22604 var_die = new_die (DW_TAG_variable, com_die, decl);
22605 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22606 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22607 decl_quals (decl_or_origin), false,
22608 context_die);
22609 add_AT_flag (var_die, DW_AT_external, 1);
22610 if (loc)
22612 if (off)
22614 /* Optimize the common case. */
22615 if (single_element_loc_list_p (loc)
22616 && loc->expr->dw_loc_opc == DW_OP_addr
22617 && loc->expr->dw_loc_next == NULL
22618 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22620 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22621 loc->expr->dw_loc_oprnd1.v.val_addr
22622 = plus_constant (GET_MODE (x), x, off);
22624 else
22625 loc_list_plus_const (loc, off);
22627 add_AT_location_description (var_die, DW_AT_location, loc);
22629 else if (DECL_EXTERNAL (decl_or_origin))
22630 add_AT_flag (var_die, DW_AT_declaration, 1);
22631 if (decl)
22632 equate_decl_number_to_die (decl, var_die);
22633 return;
22636 if (old_die)
22638 if (declaration)
22640 /* A declaration that has been previously dumped, needs no
22641 further annotations, since it doesn't need location on
22642 the second pass. */
22643 return;
22645 else if (decl_will_get_specification_p (old_die, decl, declaration)
22646 && !get_AT (old_die, DW_AT_specification))
22648 /* Fall-thru so we can make a new variable die along with a
22649 DW_AT_specification. */
22651 else if (origin && old_die->die_parent != context_die)
22653 /* If we will be creating an inlined instance, we need a
22654 new DIE that will get annotated with
22655 DW_AT_abstract_origin. Clear things so we can get a
22656 new DIE. */
22657 gcc_assert (!DECL_ABSTRACT_P (decl));
22658 old_die = NULL;
22660 else
22662 /* If a DIE was dumped early, it still needs location info.
22663 Skip to where we fill the location bits. */
22664 var_die = old_die;
22665 goto gen_variable_die_location;
22669 /* For static data members, the declaration in the class is supposed
22670 to have DW_TAG_member tag; the specification should still be
22671 DW_TAG_variable referencing the DW_TAG_member DIE. */
22672 if (declaration && class_scope_p (context_die))
22673 var_die = new_die (DW_TAG_member, context_die, decl);
22674 else
22675 var_die = new_die (DW_TAG_variable, context_die, decl);
22677 if (origin != NULL)
22678 add_abstract_origin_attribute (var_die, origin);
22680 /* Loop unrolling can create multiple blocks that refer to the same
22681 static variable, so we must test for the DW_AT_declaration flag.
22683 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22684 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22685 sharing them.
22687 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22688 else if (decl_will_get_specification_p (old_die, decl, declaration))
22690 /* This is a definition of a C++ class level static. */
22691 add_AT_specification (var_die, old_die);
22692 specialization_p = true;
22693 if (DECL_NAME (decl))
22695 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22696 struct dwarf_file_data * file_index = lookup_filename (s.file);
22698 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22699 add_AT_file (var_die, DW_AT_decl_file, file_index);
22701 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22702 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22704 if (old_die->die_tag == DW_TAG_member)
22705 add_linkage_name (var_die, decl);
22708 else
22709 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22711 if ((origin == NULL && !specialization_p)
22712 || (origin != NULL
22713 && !DECL_ABSTRACT_P (decl_or_origin)
22714 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22715 decl_function_context
22716 (decl_or_origin))))
22718 tree type = TREE_TYPE (decl_or_origin);
22720 if (decl_by_reference_p (decl_or_origin))
22721 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22722 context_die);
22723 else
22724 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22725 context_die);
22728 if (origin == NULL && !specialization_p)
22730 if (TREE_PUBLIC (decl))
22731 add_AT_flag (var_die, DW_AT_external, 1);
22733 if (DECL_ARTIFICIAL (decl))
22734 add_AT_flag (var_die, DW_AT_artificial, 1);
22736 add_accessibility_attribute (var_die, decl);
22739 if (declaration)
22740 add_AT_flag (var_die, DW_AT_declaration, 1);
22742 if (decl && (DECL_ABSTRACT_P (decl)
22743 || !old_die || is_declaration_die (old_die)))
22744 equate_decl_number_to_die (decl, var_die);
22746 gen_variable_die_location:
22747 if (! declaration
22748 && (! DECL_ABSTRACT_P (decl_or_origin)
22749 /* Local static vars are shared between all clones/inlines,
22750 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22751 already set. */
22752 || (VAR_P (decl_or_origin)
22753 && TREE_STATIC (decl_or_origin)
22754 && DECL_RTL_SET_P (decl_or_origin))))
22756 if (early_dwarf)
22757 add_pubname (decl_or_origin, var_die);
22758 else
22759 add_location_or_const_value_attribute (var_die, decl_or_origin,
22760 decl == NULL);
22762 else
22763 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22765 if ((dwarf_version >= 4 || !dwarf_strict)
22766 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22767 DW_AT_const_expr) == 1
22768 && !get_AT (var_die, DW_AT_const_expr)
22769 && !specialization_p)
22770 add_AT_flag (var_die, DW_AT_const_expr, 1);
22772 if (!dwarf_strict)
22774 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22775 DW_AT_inline);
22776 if (inl != -1
22777 && !get_AT (var_die, DW_AT_inline)
22778 && !specialization_p)
22779 add_AT_unsigned (var_die, DW_AT_inline, inl);
22783 /* Generate a DIE to represent a named constant. */
22785 static void
22786 gen_const_die (tree decl, dw_die_ref context_die)
22788 dw_die_ref const_die;
22789 tree type = TREE_TYPE (decl);
22791 const_die = lookup_decl_die (decl);
22792 if (const_die)
22793 return;
22795 const_die = new_die (DW_TAG_constant, context_die, decl);
22796 equate_decl_number_to_die (decl, const_die);
22797 add_name_and_src_coords_attributes (const_die, decl);
22798 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22799 if (TREE_PUBLIC (decl))
22800 add_AT_flag (const_die, DW_AT_external, 1);
22801 if (DECL_ARTIFICIAL (decl))
22802 add_AT_flag (const_die, DW_AT_artificial, 1);
22803 tree_add_const_value_attribute_for_decl (const_die, decl);
22806 /* Generate a DIE to represent a label identifier. */
22808 static void
22809 gen_label_die (tree decl, dw_die_ref context_die)
22811 tree origin = decl_ultimate_origin (decl);
22812 dw_die_ref lbl_die = lookup_decl_die (decl);
22813 rtx insn;
22814 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22816 if (!lbl_die)
22818 lbl_die = new_die (DW_TAG_label, context_die, decl);
22819 equate_decl_number_to_die (decl, lbl_die);
22821 if (origin != NULL)
22822 add_abstract_origin_attribute (lbl_die, origin);
22823 else
22824 add_name_and_src_coords_attributes (lbl_die, decl);
22827 if (DECL_ABSTRACT_P (decl))
22828 equate_decl_number_to_die (decl, lbl_die);
22829 else if (! early_dwarf)
22831 insn = DECL_RTL_IF_SET (decl);
22833 /* Deleted labels are programmer specified labels which have been
22834 eliminated because of various optimizations. We still emit them
22835 here so that it is possible to put breakpoints on them. */
22836 if (insn
22837 && (LABEL_P (insn)
22838 || ((NOTE_P (insn)
22839 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
22841 /* When optimization is enabled (via -O) some parts of the compiler
22842 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
22843 represent source-level labels which were explicitly declared by
22844 the user. This really shouldn't be happening though, so catch
22845 it if it ever does happen. */
22846 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
22848 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
22849 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22851 else if (insn
22852 && NOTE_P (insn)
22853 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
22854 && CODE_LABEL_NUMBER (insn) != -1)
22856 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
22857 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22862 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
22863 attributes to the DIE for a block STMT, to describe where the inlined
22864 function was called from. This is similar to add_src_coords_attributes. */
22866 static inline void
22867 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
22869 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
22871 if (dwarf_version >= 3 || !dwarf_strict)
22873 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
22874 add_AT_unsigned (die, DW_AT_call_line, s.line);
22879 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
22880 Add low_pc and high_pc attributes to the DIE for a block STMT. */
22882 static inline void
22883 add_high_low_attributes (tree stmt, dw_die_ref die)
22885 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22887 if (BLOCK_FRAGMENT_CHAIN (stmt)
22888 && (dwarf_version >= 3 || !dwarf_strict))
22890 tree chain, superblock = NULL_TREE;
22891 dw_die_ref pdie;
22892 dw_attr_node *attr = NULL;
22894 if (inlined_function_outer_scope_p (stmt))
22896 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22897 BLOCK_NUMBER (stmt));
22898 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22901 /* Optimize duplicate .debug_ranges lists or even tails of
22902 lists. If this BLOCK has same ranges as its supercontext,
22903 lookup DW_AT_ranges attribute in the supercontext (and
22904 recursively so), verify that the ranges_table contains the
22905 right values and use it instead of adding a new .debug_range. */
22906 for (chain = stmt, pdie = die;
22907 BLOCK_SAME_RANGE (chain);
22908 chain = BLOCK_SUPERCONTEXT (chain))
22910 dw_attr_node *new_attr;
22912 pdie = pdie->die_parent;
22913 if (pdie == NULL)
22914 break;
22915 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
22916 break;
22917 new_attr = get_AT (pdie, DW_AT_ranges);
22918 if (new_attr == NULL
22919 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
22920 break;
22921 attr = new_attr;
22922 superblock = BLOCK_SUPERCONTEXT (chain);
22924 if (attr != NULL
22925 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
22926 == BLOCK_NUMBER (superblock))
22927 && BLOCK_FRAGMENT_CHAIN (superblock))
22929 unsigned long off = attr->dw_attr_val.v.val_offset;
22930 unsigned long supercnt = 0, thiscnt = 0;
22931 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
22932 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
22934 ++supercnt;
22935 gcc_checking_assert ((*ranges_table)[off + supercnt].num
22936 == BLOCK_NUMBER (chain));
22938 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
22939 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
22940 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
22941 ++thiscnt;
22942 gcc_assert (supercnt >= thiscnt);
22943 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
22944 false);
22945 note_rnglist_head (off + supercnt - thiscnt);
22946 return;
22949 unsigned int offset = add_ranges (stmt, true);
22950 add_AT_range_list (die, DW_AT_ranges, offset, false);
22951 note_rnglist_head (offset);
22953 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
22954 chain = BLOCK_FRAGMENT_CHAIN (stmt);
22957 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
22958 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
22959 chain = BLOCK_FRAGMENT_CHAIN (chain);
22961 while (chain);
22962 add_ranges (NULL);
22964 else
22966 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
22967 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22968 BLOCK_NUMBER (stmt));
22969 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
22970 BLOCK_NUMBER (stmt));
22971 add_AT_low_high_pc (die, label, label_high, false);
22975 /* Generate a DIE for a lexical block. */
22977 static void
22978 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
22980 dw_die_ref old_die = BLOCK_DIE (stmt);
22981 dw_die_ref stmt_die = NULL;
22982 if (!old_die)
22984 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
22985 BLOCK_DIE (stmt) = stmt_die;
22988 if (BLOCK_ABSTRACT (stmt))
22990 if (old_die)
22992 /* This must have been generated early and it won't even
22993 need location information since it's a DW_AT_inline
22994 function. */
22995 if (flag_checking)
22996 for (dw_die_ref c = context_die; c; c = c->die_parent)
22997 if (c->die_tag == DW_TAG_inlined_subroutine
22998 || c->die_tag == DW_TAG_subprogram)
23000 gcc_assert (get_AT (c, DW_AT_inline));
23001 break;
23003 return;
23006 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23008 /* If this is an inlined instance, create a new lexical die for
23009 anything below to attach DW_AT_abstract_origin to. */
23010 if (old_die)
23012 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23013 BLOCK_DIE (stmt) = stmt_die;
23014 old_die = NULL;
23017 tree origin = block_ultimate_origin (stmt);
23018 if (origin != NULL_TREE && origin != stmt)
23019 add_abstract_origin_attribute (stmt_die, origin);
23022 if (old_die)
23023 stmt_die = old_die;
23025 /* A non abstract block whose blocks have already been reordered
23026 should have the instruction range for this block. If so, set the
23027 high/low attributes. */
23028 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23030 gcc_assert (stmt_die);
23031 add_high_low_attributes (stmt, stmt_die);
23034 decls_for_scope (stmt, stmt_die);
23037 /* Generate a DIE for an inlined subprogram. */
23039 static void
23040 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23042 tree decl;
23044 /* The instance of function that is effectively being inlined shall not
23045 be abstract. */
23046 gcc_assert (! BLOCK_ABSTRACT (stmt));
23048 decl = block_ultimate_origin (stmt);
23050 /* Make sure any inlined functions are known to be inlineable. */
23051 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23052 || cgraph_function_possibly_inlined_p (decl));
23054 /* Emit info for the abstract instance first, if we haven't yet. We
23055 must emit this even if the block is abstract, otherwise when we
23056 emit the block below (or elsewhere), we may end up trying to emit
23057 a die whose origin die hasn't been emitted, and crashing. */
23058 dwarf2out_abstract_function (decl);
23060 if (! BLOCK_ABSTRACT (stmt))
23062 dw_die_ref subr_die
23063 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23065 if (call_arg_locations)
23066 BLOCK_DIE (stmt) = subr_die;
23067 add_abstract_origin_attribute (subr_die, decl);
23068 if (TREE_ASM_WRITTEN (stmt))
23069 add_high_low_attributes (stmt, subr_die);
23070 add_call_src_coords_attributes (stmt, subr_die);
23072 decls_for_scope (stmt, subr_die);
23076 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23077 the comment for VLR_CONTEXT. */
23079 static void
23080 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23082 dw_die_ref decl_die;
23084 if (TREE_TYPE (decl) == error_mark_node)
23085 return;
23087 decl_die = new_die (DW_TAG_member, context_die, decl);
23088 add_name_and_src_coords_attributes (decl_die, decl);
23089 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23090 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23091 context_die);
23093 if (DECL_BIT_FIELD_TYPE (decl))
23095 add_byte_size_attribute (decl_die, decl);
23096 add_bit_size_attribute (decl_die, decl);
23097 add_bit_offset_attribute (decl_die, decl, ctx);
23100 /* If we have a variant part offset, then we are supposed to process a member
23101 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23102 trees. */
23103 gcc_assert (ctx->variant_part_offset == NULL_TREE
23104 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23105 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23106 add_data_member_location_attribute (decl_die, decl, ctx);
23108 if (DECL_ARTIFICIAL (decl))
23109 add_AT_flag (decl_die, DW_AT_artificial, 1);
23111 add_accessibility_attribute (decl_die, decl);
23113 /* Equate decl number to die, so that we can look up this decl later on. */
23114 equate_decl_number_to_die (decl, decl_die);
23117 #if 0
23118 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23119 Use modified_type_die instead.
23120 We keep this code here just in case these types of DIEs may be needed to
23121 represent certain things in other languages (e.g. Pascal) someday. */
23123 static void
23124 gen_pointer_type_die (tree type, dw_die_ref context_die)
23126 dw_die_ref ptr_die
23127 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23129 equate_type_number_to_die (type, ptr_die);
23130 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23131 context_die);
23132 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23135 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23136 Use modified_type_die instead.
23137 We keep this code here just in case these types of DIEs may be needed to
23138 represent certain things in other languages (e.g. Pascal) someday. */
23140 static void
23141 gen_reference_type_die (tree type, dw_die_ref context_die)
23143 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23145 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23146 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23147 else
23148 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23150 equate_type_number_to_die (type, ref_die);
23151 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23152 context_die);
23153 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23155 #endif
23157 /* Generate a DIE for a pointer to a member type. TYPE can be an
23158 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23159 pointer to member function. */
23161 static void
23162 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23164 if (lookup_type_die (type))
23165 return;
23167 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23168 scope_die_for (type, context_die), type);
23170 equate_type_number_to_die (type, ptr_die);
23171 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23172 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23173 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23174 context_die);
23176 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23177 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23179 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23180 add_AT_loc (ptr_die, DW_AT_use_location, op);
23184 static char *producer_string;
23186 /* Return a heap allocated producer string including command line options
23187 if -grecord-gcc-switches. */
23189 static char *
23190 gen_producer_string (void)
23192 size_t j;
23193 auto_vec<const char *> switches;
23194 const char *language_string = lang_hooks.name;
23195 char *producer, *tail;
23196 const char *p;
23197 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23198 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23200 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23201 switch (save_decoded_options[j].opt_index)
23203 case OPT_o:
23204 case OPT_d:
23205 case OPT_dumpbase:
23206 case OPT_dumpdir:
23207 case OPT_auxbase:
23208 case OPT_auxbase_strip:
23209 case OPT_quiet:
23210 case OPT_version:
23211 case OPT_v:
23212 case OPT_w:
23213 case OPT_L:
23214 case OPT_D:
23215 case OPT_I:
23216 case OPT_U:
23217 case OPT_SPECIAL_unknown:
23218 case OPT_SPECIAL_ignore:
23219 case OPT_SPECIAL_program_name:
23220 case OPT_SPECIAL_input_file:
23221 case OPT_grecord_gcc_switches:
23222 case OPT_gno_record_gcc_switches:
23223 case OPT__output_pch_:
23224 case OPT_fdiagnostics_show_location_:
23225 case OPT_fdiagnostics_show_option:
23226 case OPT_fdiagnostics_show_caret:
23227 case OPT_fdiagnostics_color_:
23228 case OPT_fverbose_asm:
23229 case OPT____:
23230 case OPT__sysroot_:
23231 case OPT_nostdinc:
23232 case OPT_nostdinc__:
23233 case OPT_fpreprocessed:
23234 case OPT_fltrans_output_list_:
23235 case OPT_fresolution_:
23236 case OPT_fdebug_prefix_map_:
23237 /* Ignore these. */
23238 continue;
23239 default:
23240 if (cl_options[save_decoded_options[j].opt_index].flags
23241 & CL_NO_DWARF_RECORD)
23242 continue;
23243 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23244 == '-');
23245 switch (save_decoded_options[j].canonical_option[0][1])
23247 case 'M':
23248 case 'i':
23249 case 'W':
23250 continue;
23251 case 'f':
23252 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23253 "dump", 4) == 0)
23254 continue;
23255 break;
23256 default:
23257 break;
23259 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23260 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23261 break;
23264 producer = XNEWVEC (char, plen + 1 + len + 1);
23265 tail = producer;
23266 sprintf (tail, "%s %s", language_string, version_string);
23267 tail += plen;
23269 FOR_EACH_VEC_ELT (switches, j, p)
23271 len = strlen (p);
23272 *tail = ' ';
23273 memcpy (tail + 1, p, len);
23274 tail += len + 1;
23277 *tail = '\0';
23278 return producer;
23281 /* Given a C and/or C++ language/version string return the "highest".
23282 C++ is assumed to be "higher" than C in this case. Used for merging
23283 LTO translation unit languages. */
23284 static const char *
23285 highest_c_language (const char *lang1, const char *lang2)
23287 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23288 return "GNU C++14";
23289 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23290 return "GNU C++11";
23291 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23292 return "GNU C++98";
23294 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23295 return "GNU C11";
23296 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23297 return "GNU C99";
23298 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23299 return "GNU C89";
23301 gcc_unreachable ();
23305 /* Generate the DIE for the compilation unit. */
23307 static dw_die_ref
23308 gen_compile_unit_die (const char *filename)
23310 dw_die_ref die;
23311 const char *language_string = lang_hooks.name;
23312 int language;
23314 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23316 if (filename)
23318 add_name_attribute (die, filename);
23319 /* Don't add cwd for <built-in>. */
23320 if (filename[0] != '<')
23321 add_comp_dir_attribute (die);
23324 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23326 /* If our producer is LTO try to figure out a common language to use
23327 from the global list of translation units. */
23328 if (strcmp (language_string, "GNU GIMPLE") == 0)
23330 unsigned i;
23331 tree t;
23332 const char *common_lang = NULL;
23334 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23336 if (!TRANSLATION_UNIT_LANGUAGE (t))
23337 continue;
23338 if (!common_lang)
23339 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23340 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23342 else if (strncmp (common_lang, "GNU C", 5) == 0
23343 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23344 /* Mixing C and C++ is ok, use C++ in that case. */
23345 common_lang = highest_c_language (common_lang,
23346 TRANSLATION_UNIT_LANGUAGE (t));
23347 else
23349 /* Fall back to C. */
23350 common_lang = NULL;
23351 break;
23355 if (common_lang)
23356 language_string = common_lang;
23359 language = DW_LANG_C;
23360 if (strncmp (language_string, "GNU C", 5) == 0
23361 && ISDIGIT (language_string[5]))
23363 language = DW_LANG_C89;
23364 if (dwarf_version >= 3 || !dwarf_strict)
23366 if (strcmp (language_string, "GNU C89") != 0)
23367 language = DW_LANG_C99;
23369 if (dwarf_version >= 5 /* || !dwarf_strict */)
23370 if (strcmp (language_string, "GNU C11") == 0)
23371 language = DW_LANG_C11;
23374 else if (strncmp (language_string, "GNU C++", 7) == 0)
23376 language = DW_LANG_C_plus_plus;
23377 if (dwarf_version >= 5 /* || !dwarf_strict */)
23379 if (strcmp (language_string, "GNU C++11") == 0)
23380 language = DW_LANG_C_plus_plus_11;
23381 else if (strcmp (language_string, "GNU C++14") == 0)
23382 language = DW_LANG_C_plus_plus_14;
23385 else if (strcmp (language_string, "GNU F77") == 0)
23386 language = DW_LANG_Fortran77;
23387 else if (strcmp (language_string, "GNU Pascal") == 0)
23388 language = DW_LANG_Pascal83;
23389 else if (dwarf_version >= 3 || !dwarf_strict)
23391 if (strcmp (language_string, "GNU Ada") == 0)
23392 language = DW_LANG_Ada95;
23393 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23395 language = DW_LANG_Fortran95;
23396 if (dwarf_version >= 5 /* || !dwarf_strict */)
23398 if (strcmp (language_string, "GNU Fortran2003") == 0)
23399 language = DW_LANG_Fortran03;
23400 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23401 language = DW_LANG_Fortran08;
23404 else if (strcmp (language_string, "GNU Java") == 0)
23405 language = DW_LANG_Java;
23406 else if (strcmp (language_string, "GNU Objective-C") == 0)
23407 language = DW_LANG_ObjC;
23408 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23409 language = DW_LANG_ObjC_plus_plus;
23410 else if (dwarf_version >= 5 || !dwarf_strict)
23412 if (strcmp (language_string, "GNU Go") == 0)
23413 language = DW_LANG_Go;
23416 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23417 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23418 language = DW_LANG_Fortran90;
23420 add_AT_unsigned (die, DW_AT_language, language);
23422 switch (language)
23424 case DW_LANG_Fortran77:
23425 case DW_LANG_Fortran90:
23426 case DW_LANG_Fortran95:
23427 case DW_LANG_Fortran03:
23428 case DW_LANG_Fortran08:
23429 /* Fortran has case insensitive identifiers and the front-end
23430 lowercases everything. */
23431 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23432 break;
23433 default:
23434 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23435 break;
23437 return die;
23440 /* Generate the DIE for a base class. */
23442 static void
23443 gen_inheritance_die (tree binfo, tree access, tree type,
23444 dw_die_ref context_die)
23446 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23447 struct vlr_context ctx = { type, NULL };
23449 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23450 context_die);
23451 add_data_member_location_attribute (die, binfo, &ctx);
23453 if (BINFO_VIRTUAL_P (binfo))
23454 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23456 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23457 children, otherwise the default is DW_ACCESS_public. In DWARF2
23458 the default has always been DW_ACCESS_private. */
23459 if (access == access_public_node)
23461 if (dwarf_version == 2
23462 || context_die->die_tag == DW_TAG_class_type)
23463 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23465 else if (access == access_protected_node)
23466 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23467 else if (dwarf_version > 2
23468 && context_die->die_tag != DW_TAG_class_type)
23469 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23472 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23473 structure. */
23474 static bool
23475 is_variant_part (tree decl)
23477 return (TREE_CODE (decl) == FIELD_DECL
23478 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23481 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23482 return the FIELD_DECL. Return NULL_TREE otherwise. */
23484 static tree
23485 analyze_discr_in_predicate (tree operand, tree struct_type)
23487 bool continue_stripping = true;
23488 while (continue_stripping)
23489 switch (TREE_CODE (operand))
23491 CASE_CONVERT:
23492 operand = TREE_OPERAND (operand, 0);
23493 break;
23494 default:
23495 continue_stripping = false;
23496 break;
23499 /* Match field access to members of struct_type only. */
23500 if (TREE_CODE (operand) == COMPONENT_REF
23501 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23502 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23503 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23504 return TREE_OPERAND (operand, 1);
23505 else
23506 return NULL_TREE;
23509 /* Check that SRC is a constant integer that can be represented as a native
23510 integer constant (either signed or unsigned). If so, store it into DEST and
23511 return true. Return false otherwise. */
23513 static bool
23514 get_discr_value (tree src, dw_discr_value *dest)
23516 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23518 if (TREE_CODE (src) != INTEGER_CST
23519 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23520 return false;
23522 dest->pos = is_unsigned;
23523 if (is_unsigned)
23524 dest->v.uval = tree_to_uhwi (src);
23525 else
23526 dest->v.sval = tree_to_shwi (src);
23528 return true;
23531 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23532 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23533 store NULL_TREE in DISCR_DECL. Otherwise:
23535 - store the discriminant field in STRUCT_TYPE that controls the variant
23536 part to *DISCR_DECL
23538 - put in *DISCR_LISTS_P an array where for each variant, the item
23539 represents the corresponding matching list of discriminant values.
23541 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23542 the above array.
23544 Note that when the array is allocated (i.e. when the analysis is
23545 successful), it is up to the caller to free the array. */
23547 static void
23548 analyze_variants_discr (tree variant_part_decl,
23549 tree struct_type,
23550 tree *discr_decl,
23551 dw_discr_list_ref **discr_lists_p,
23552 unsigned *discr_lists_length)
23554 tree variant_part_type = TREE_TYPE (variant_part_decl);
23555 tree variant;
23556 dw_discr_list_ref *discr_lists;
23557 unsigned i;
23559 /* Compute how many variants there are in this variant part. */
23560 *discr_lists_length = 0;
23561 for (variant = TYPE_FIELDS (variant_part_type);
23562 variant != NULL_TREE;
23563 variant = DECL_CHAIN (variant))
23564 ++*discr_lists_length;
23566 *discr_decl = NULL_TREE;
23567 *discr_lists_p
23568 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23569 sizeof (**discr_lists_p));
23570 discr_lists = *discr_lists_p;
23572 /* And then analyze all variants to extract discriminant information for all
23573 of them. This analysis is conservative: as soon as we detect something we
23574 do not support, abort everything and pretend we found nothing. */
23575 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23576 variant != NULL_TREE;
23577 variant = DECL_CHAIN (variant), ++i)
23579 tree match_expr = DECL_QUALIFIER (variant);
23581 /* Now, try to analyze the predicate and deduce a discriminant for
23582 it. */
23583 if (match_expr == boolean_true_node)
23584 /* Typically happens for the default variant: it matches all cases that
23585 previous variants rejected. Don't output any matching value for
23586 this one. */
23587 continue;
23589 /* The following loop tries to iterate over each discriminant
23590 possibility: single values or ranges. */
23591 while (match_expr != NULL_TREE)
23593 tree next_round_match_expr;
23594 tree candidate_discr = NULL_TREE;
23595 dw_discr_list_ref new_node = NULL;
23597 /* Possibilities are matched one after the other by nested
23598 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23599 continue with the rest at next iteration. */
23600 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23602 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23603 match_expr = TREE_OPERAND (match_expr, 1);
23605 else
23606 next_round_match_expr = NULL_TREE;
23608 if (match_expr == boolean_false_node)
23609 /* This sub-expression matches nothing: just wait for the next
23610 one. */
23613 else if (TREE_CODE (match_expr) == EQ_EXPR)
23615 /* We are matching: <discr_field> == <integer_cst>
23616 This sub-expression matches a single value. */
23617 tree integer_cst = TREE_OPERAND (match_expr, 1);
23619 candidate_discr
23620 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23621 struct_type);
23623 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23624 if (!get_discr_value (integer_cst,
23625 &new_node->dw_discr_lower_bound))
23626 goto abort;
23627 new_node->dw_discr_range = false;
23630 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23632 /* We are matching:
23633 <discr_field> > <integer_cst>
23634 && <discr_field> < <integer_cst>.
23635 This sub-expression matches the range of values between the
23636 two matched integer constants. Note that comparisons can be
23637 inclusive or exclusive. */
23638 tree candidate_discr_1, candidate_discr_2;
23639 tree lower_cst, upper_cst;
23640 bool lower_cst_included, upper_cst_included;
23641 tree lower_op = TREE_OPERAND (match_expr, 0);
23642 tree upper_op = TREE_OPERAND (match_expr, 1);
23644 /* When the comparison is exclusive, the integer constant is not
23645 the discriminant range bound we are looking for: we will have
23646 to increment or decrement it. */
23647 if (TREE_CODE (lower_op) == GE_EXPR)
23648 lower_cst_included = true;
23649 else if (TREE_CODE (lower_op) == GT_EXPR)
23650 lower_cst_included = false;
23651 else
23652 goto abort;
23654 if (TREE_CODE (upper_op) == LE_EXPR)
23655 upper_cst_included = true;
23656 else if (TREE_CODE (upper_op) == LT_EXPR)
23657 upper_cst_included = false;
23658 else
23659 goto abort;
23661 /* Extract the discriminant from the first operand and check it
23662 is consistant with the same analysis in the second
23663 operand. */
23664 candidate_discr_1
23665 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23666 struct_type);
23667 candidate_discr_2
23668 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23669 struct_type);
23670 if (candidate_discr_1 == candidate_discr_2)
23671 candidate_discr = candidate_discr_1;
23672 else
23673 goto abort;
23675 /* Extract bounds from both. */
23676 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23677 lower_cst = TREE_OPERAND (lower_op, 1);
23678 upper_cst = TREE_OPERAND (upper_op, 1);
23680 if (!lower_cst_included)
23681 lower_cst
23682 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
23683 lower_cst,
23684 build_int_cst (TREE_TYPE (lower_cst), 1)));
23685 if (!upper_cst_included)
23686 upper_cst
23687 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
23688 upper_cst,
23689 build_int_cst (TREE_TYPE (upper_cst), 1)));
23691 if (!get_discr_value (lower_cst,
23692 &new_node->dw_discr_lower_bound)
23693 || !get_discr_value (upper_cst,
23694 &new_node->dw_discr_upper_bound))
23695 goto abort;
23697 new_node->dw_discr_range = true;
23700 else
23701 /* Unsupported sub-expression: we cannot determine the set of
23702 matching discriminant values. Abort everything. */
23703 goto abort;
23705 /* If the discriminant info is not consistant with what we saw so
23706 far, consider the analysis failed and abort everything. */
23707 if (candidate_discr == NULL_TREE
23708 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23709 goto abort;
23710 else
23711 *discr_decl = candidate_discr;
23713 if (new_node != NULL)
23715 new_node->dw_discr_next = discr_lists[i];
23716 discr_lists[i] = new_node;
23718 match_expr = next_round_match_expr;
23722 /* If we reach this point, we could match everything we were interested
23723 in. */
23724 return;
23726 abort:
23727 /* Clean all data structure and return no result. */
23728 free (*discr_lists_p);
23729 *discr_lists_p = NULL;
23730 *discr_decl = NULL_TREE;
23733 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23734 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23735 under CONTEXT_DIE.
23737 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23738 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23739 this type, which are record types, represent the available variants and each
23740 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23741 values are inferred from these attributes.
23743 In trees, the offsets for the fields inside these sub-records are relative
23744 to the variant part itself, whereas the corresponding DIEs should have
23745 offset attributes that are relative to the embedding record base address.
23746 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23747 must be an expression that computes the offset of the variant part to
23748 describe in DWARF. */
23750 static void
23751 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23752 dw_die_ref context_die)
23754 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23755 tree variant_part_offset = vlr_ctx->variant_part_offset;
23756 struct loc_descr_context ctx = {
23757 vlr_ctx->struct_type, /* context_type */
23758 NULL_TREE, /* base_decl */
23759 NULL, /* dpi */
23760 false, /* placeholder_arg */
23761 false /* placeholder_seen */
23764 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23765 NULL_TREE if there is no such field. */
23766 tree discr_decl = NULL_TREE;
23767 dw_discr_list_ref *discr_lists;
23768 unsigned discr_lists_length = 0;
23769 unsigned i;
23771 dw_die_ref dwarf_proc_die = NULL;
23772 dw_die_ref variant_part_die
23773 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23775 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23777 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23778 &discr_decl, &discr_lists, &discr_lists_length);
23780 if (discr_decl != NULL_TREE)
23782 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23784 if (discr_die)
23785 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23786 else
23787 /* We have no DIE for the discriminant, so just discard all
23788 discrimimant information in the output. */
23789 discr_decl = NULL_TREE;
23792 /* If the offset for this variant part is more complex than a constant,
23793 create a DWARF procedure for it so that we will not have to generate DWARF
23794 expressions for it for each member. */
23795 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23796 && (dwarf_version >= 3 || !dwarf_strict))
23798 const tree dwarf_proc_fndecl
23799 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23800 build_function_type (TREE_TYPE (variant_part_offset),
23801 NULL_TREE));
23802 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23803 const dw_loc_descr_ref dwarf_proc_body
23804 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23806 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23807 dwarf_proc_fndecl, context_die);
23808 if (dwarf_proc_die != NULL)
23809 variant_part_offset = dwarf_proc_call;
23812 /* Output DIEs for all variants. */
23813 i = 0;
23814 for (tree variant = TYPE_FIELDS (variant_part_type);
23815 variant != NULL_TREE;
23816 variant = DECL_CHAIN (variant), ++i)
23818 tree variant_type = TREE_TYPE (variant);
23819 dw_die_ref variant_die;
23821 /* All variants (i.e. members of a variant part) are supposed to be
23822 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23823 under these records. */
23824 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23826 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23827 equate_decl_number_to_die (variant, variant_die);
23829 /* Output discriminant values this variant matches, if any. */
23830 if (discr_decl == NULL || discr_lists[i] == NULL)
23831 /* In the case we have discriminant information at all, this is
23832 probably the default variant: as the standard says, don't
23833 output any discriminant value/list attribute. */
23835 else if (discr_lists[i]->dw_discr_next == NULL
23836 && !discr_lists[i]->dw_discr_range)
23837 /* If there is only one accepted value, don't bother outputting a
23838 list. */
23839 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23840 else
23841 add_discr_list (variant_die, discr_lists[i]);
23843 for (tree member = TYPE_FIELDS (variant_type);
23844 member != NULL_TREE;
23845 member = DECL_CHAIN (member))
23847 struct vlr_context vlr_sub_ctx = {
23848 vlr_ctx->struct_type, /* struct_type */
23849 NULL /* variant_part_offset */
23851 if (is_variant_part (member))
23853 /* All offsets for fields inside variant parts are relative to
23854 the top-level embedding RECORD_TYPE's base address. On the
23855 other hand, offsets in GCC's types are relative to the
23856 nested-most variant part. So we have to sum offsets each time
23857 we recurse. */
23859 vlr_sub_ctx.variant_part_offset
23860 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
23861 variant_part_offset, byte_position (member)));
23862 gen_variant_part (member, &vlr_sub_ctx, variant_die);
23864 else
23866 vlr_sub_ctx.variant_part_offset = variant_part_offset;
23867 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
23872 free (discr_lists);
23875 /* Generate a DIE for a class member. */
23877 static void
23878 gen_member_die (tree type, dw_die_ref context_die)
23880 tree member;
23881 tree binfo = TYPE_BINFO (type);
23882 dw_die_ref child;
23884 /* If this is not an incomplete type, output descriptions of each of its
23885 members. Note that as we output the DIEs necessary to represent the
23886 members of this record or union type, we will also be trying to output
23887 DIEs to represent the *types* of those members. However the `type'
23888 function (above) will specifically avoid generating type DIEs for member
23889 types *within* the list of member DIEs for this (containing) type except
23890 for those types (of members) which are explicitly marked as also being
23891 members of this (containing) type themselves. The g++ front- end can
23892 force any given type to be treated as a member of some other (containing)
23893 type by setting the TYPE_CONTEXT of the given (member) type to point to
23894 the TREE node representing the appropriate (containing) type. */
23896 /* First output info about the base classes. */
23897 if (binfo)
23899 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
23900 int i;
23901 tree base;
23903 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
23904 gen_inheritance_die (base,
23905 (accesses ? (*accesses)[i] : access_public_node),
23906 type,
23907 context_die);
23910 /* Now output info about the data members and type members. */
23911 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
23913 struct vlr_context vlr_ctx = { type, NULL_TREE };
23915 /* If we thought we were generating minimal debug info for TYPE
23916 and then changed our minds, some of the member declarations
23917 may have already been defined. Don't define them again, but
23918 do put them in the right order. */
23920 child = lookup_decl_die (member);
23921 if (child)
23923 /* Handle inline static data members, which only have in-class
23924 declarations. */
23925 if (child->die_tag == DW_TAG_variable
23926 && child->die_parent == comp_unit_die ()
23927 && get_AT (child, DW_AT_specification) == NULL)
23929 reparent_child (child, context_die);
23930 child->die_tag = DW_TAG_member;
23932 else
23933 splice_child_die (context_die, child);
23936 /* Do not generate standard DWARF for variant parts if we are generating
23937 the corresponding GNAT encodings: DIEs generated for both would
23938 conflict in our mappings. */
23939 else if (is_variant_part (member)
23940 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
23942 vlr_ctx.variant_part_offset = byte_position (member);
23943 gen_variant_part (member, &vlr_ctx, context_die);
23945 else
23947 vlr_ctx.variant_part_offset = NULL_TREE;
23948 gen_decl_die (member, NULL, &vlr_ctx, context_die);
23951 /* For C++ inline static data members emit immediately a DW_TAG_variable
23952 DIE that will refer to that DW_TAG_member through
23953 DW_AT_specification. */
23954 if (TREE_STATIC (member)
23955 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
23956 != -1))
23958 int old_extern = DECL_EXTERNAL (member);
23959 DECL_EXTERNAL (member) = 0;
23960 gen_decl_die (member, NULL, NULL, comp_unit_die ());
23961 DECL_EXTERNAL (member) = old_extern;
23965 /* We do not keep type methods in type variants. */
23966 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
23967 /* Now output info about the function members (if any). */
23968 if (TYPE_METHODS (type) != error_mark_node)
23969 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
23971 /* Don't include clones in the member list. */
23972 if (DECL_ABSTRACT_ORIGIN (member))
23973 continue;
23974 /* Nor constructors for anonymous classes. */
23975 if (DECL_ARTIFICIAL (member)
23976 && dwarf2_name (member, 0) == NULL)
23977 continue;
23979 child = lookup_decl_die (member);
23980 if (child)
23981 splice_child_die (context_die, child);
23982 else
23983 gen_decl_die (member, NULL, NULL, context_die);
23987 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
23988 is set, we pretend that the type was never defined, so we only get the
23989 member DIEs needed by later specification DIEs. */
23991 static void
23992 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
23993 enum debug_info_usage usage)
23995 if (TREE_ASM_WRITTEN (type))
23997 /* Fill in the bound of variable-length fields in late dwarf if
23998 still incomplete. */
23999 if (!early_dwarf && variably_modified_type_p (type, NULL))
24000 for (tree member = TYPE_FIELDS (type);
24001 member;
24002 member = DECL_CHAIN (member))
24003 fill_variable_array_bounds (TREE_TYPE (member));
24004 return;
24007 dw_die_ref type_die = lookup_type_die (type);
24008 dw_die_ref scope_die = 0;
24009 int nested = 0;
24010 int complete = (TYPE_SIZE (type)
24011 && (! TYPE_STUB_DECL (type)
24012 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24013 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24014 complete = complete && should_emit_struct_debug (type, usage);
24016 if (type_die && ! complete)
24017 return;
24019 if (TYPE_CONTEXT (type) != NULL_TREE
24020 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24021 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24022 nested = 1;
24024 scope_die = scope_die_for (type, context_die);
24026 /* Generate child dies for template paramaters. */
24027 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24028 schedule_generic_params_dies_gen (type);
24030 if (! type_die || (nested && is_cu_die (scope_die)))
24031 /* First occurrence of type or toplevel definition of nested class. */
24033 dw_die_ref old_die = type_die;
24035 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24036 ? record_type_tag (type) : DW_TAG_union_type,
24037 scope_die, type);
24038 equate_type_number_to_die (type, type_die);
24039 if (old_die)
24040 add_AT_specification (type_die, old_die);
24041 else
24042 add_name_attribute (type_die, type_tag (type));
24044 else
24045 remove_AT (type_die, DW_AT_declaration);
24047 /* If this type has been completed, then give it a byte_size attribute and
24048 then give a list of members. */
24049 if (complete && !ns_decl)
24051 /* Prevent infinite recursion in cases where the type of some member of
24052 this type is expressed in terms of this type itself. */
24053 TREE_ASM_WRITTEN (type) = 1;
24054 add_byte_size_attribute (type_die, type);
24055 if (TYPE_STUB_DECL (type) != NULL_TREE)
24057 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24058 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24061 /* If the first reference to this type was as the return type of an
24062 inline function, then it may not have a parent. Fix this now. */
24063 if (type_die->die_parent == NULL)
24064 add_child_die (scope_die, type_die);
24066 push_decl_scope (type);
24067 gen_member_die (type, type_die);
24068 pop_decl_scope ();
24070 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24071 if (TYPE_ARTIFICIAL (type))
24072 add_AT_flag (type_die, DW_AT_artificial, 1);
24074 /* GNU extension: Record what type our vtable lives in. */
24075 if (TYPE_VFIELD (type))
24077 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24079 gen_type_die (vtype, context_die);
24080 add_AT_die_ref (type_die, DW_AT_containing_type,
24081 lookup_type_die (vtype));
24084 else
24086 add_AT_flag (type_die, DW_AT_declaration, 1);
24088 /* We don't need to do this for function-local types. */
24089 if (TYPE_STUB_DECL (type)
24090 && ! decl_function_context (TYPE_STUB_DECL (type)))
24091 vec_safe_push (incomplete_types, type);
24094 if (get_AT (type_die, DW_AT_name))
24095 add_pubtype (type, type_die);
24098 /* Generate a DIE for a subroutine _type_. */
24100 static void
24101 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24103 tree return_type = TREE_TYPE (type);
24104 dw_die_ref subr_die
24105 = new_die (DW_TAG_subroutine_type,
24106 scope_die_for (type, context_die), type);
24108 equate_type_number_to_die (type, subr_die);
24109 add_prototyped_attribute (subr_die, type);
24110 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24111 context_die);
24112 gen_formal_types_die (type, subr_die);
24114 if (get_AT (subr_die, DW_AT_name))
24115 add_pubtype (type, subr_die);
24116 if ((dwarf_version >= 5 || !dwarf_strict)
24117 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24118 add_AT_flag (subr_die, DW_AT_reference, 1);
24119 if ((dwarf_version >= 5 || !dwarf_strict)
24120 && lang_hooks.types.type_dwarf_attribute (type,
24121 DW_AT_rvalue_reference) != -1)
24122 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24125 /* Generate a DIE for a type definition. */
24127 static void
24128 gen_typedef_die (tree decl, dw_die_ref context_die)
24130 dw_die_ref type_die;
24131 tree origin;
24133 if (TREE_ASM_WRITTEN (decl))
24135 if (DECL_ORIGINAL_TYPE (decl))
24136 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24137 return;
24140 TREE_ASM_WRITTEN (decl) = 1;
24141 type_die = new_die (DW_TAG_typedef, context_die, decl);
24142 origin = decl_ultimate_origin (decl);
24143 if (origin != NULL)
24144 add_abstract_origin_attribute (type_die, origin);
24145 else
24147 tree type;
24149 add_name_and_src_coords_attributes (type_die, decl);
24150 if (DECL_ORIGINAL_TYPE (decl))
24152 type = DECL_ORIGINAL_TYPE (decl);
24154 if (type == error_mark_node)
24155 return;
24157 gcc_assert (type != TREE_TYPE (decl));
24158 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24160 else
24162 type = TREE_TYPE (decl);
24164 if (type == error_mark_node)
24165 return;
24167 if (is_naming_typedef_decl (TYPE_NAME (type)))
24169 /* Here, we are in the case of decl being a typedef naming
24170 an anonymous type, e.g:
24171 typedef struct {...} foo;
24172 In that case TREE_TYPE (decl) is not a typedef variant
24173 type and TYPE_NAME of the anonymous type is set to the
24174 TYPE_DECL of the typedef. This construct is emitted by
24175 the C++ FE.
24177 TYPE is the anonymous struct named by the typedef
24178 DECL. As we need the DW_AT_type attribute of the
24179 DW_TAG_typedef to point to the DIE of TYPE, let's
24180 generate that DIE right away. add_type_attribute
24181 called below will then pick (via lookup_type_die) that
24182 anonymous struct DIE. */
24183 if (!TREE_ASM_WRITTEN (type))
24184 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24186 /* This is a GNU Extension. We are adding a
24187 DW_AT_linkage_name attribute to the DIE of the
24188 anonymous struct TYPE. The value of that attribute
24189 is the name of the typedef decl naming the anonymous
24190 struct. This greatly eases the work of consumers of
24191 this debug info. */
24192 add_linkage_name_raw (lookup_type_die (type), decl);
24196 add_type_attribute (type_die, type, decl_quals (decl), false,
24197 context_die);
24199 if (is_naming_typedef_decl (decl))
24200 /* We want that all subsequent calls to lookup_type_die with
24201 TYPE in argument yield the DW_TAG_typedef we have just
24202 created. */
24203 equate_type_number_to_die (type, type_die);
24205 add_accessibility_attribute (type_die, decl);
24208 if (DECL_ABSTRACT_P (decl))
24209 equate_decl_number_to_die (decl, type_die);
24211 if (get_AT (type_die, DW_AT_name))
24212 add_pubtype (decl, type_die);
24215 /* Generate a DIE for a struct, class, enum or union type. */
24217 static void
24218 gen_tagged_type_die (tree type,
24219 dw_die_ref context_die,
24220 enum debug_info_usage usage)
24222 int need_pop;
24224 if (type == NULL_TREE
24225 || !is_tagged_type (type))
24226 return;
24228 if (TREE_ASM_WRITTEN (type))
24229 need_pop = 0;
24230 /* If this is a nested type whose containing class hasn't been written
24231 out yet, writing it out will cover this one, too. This does not apply
24232 to instantiations of member class templates; they need to be added to
24233 the containing class as they are generated. FIXME: This hurts the
24234 idea of combining type decls from multiple TUs, since we can't predict
24235 what set of template instantiations we'll get. */
24236 else if (TYPE_CONTEXT (type)
24237 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24238 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24240 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24242 if (TREE_ASM_WRITTEN (type))
24243 return;
24245 /* If that failed, attach ourselves to the stub. */
24246 push_decl_scope (TYPE_CONTEXT (type));
24247 context_die = lookup_type_die (TYPE_CONTEXT (type));
24248 need_pop = 1;
24250 else if (TYPE_CONTEXT (type) != NULL_TREE
24251 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24253 /* If this type is local to a function that hasn't been written
24254 out yet, use a NULL context for now; it will be fixed up in
24255 decls_for_scope. */
24256 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24257 /* A declaration DIE doesn't count; nested types need to go in the
24258 specification. */
24259 if (context_die && is_declaration_die (context_die))
24260 context_die = NULL;
24261 need_pop = 0;
24263 else
24265 context_die = declare_in_namespace (type, context_die);
24266 need_pop = 0;
24269 if (TREE_CODE (type) == ENUMERAL_TYPE)
24271 /* This might have been written out by the call to
24272 declare_in_namespace. */
24273 if (!TREE_ASM_WRITTEN (type))
24274 gen_enumeration_type_die (type, context_die);
24276 else
24277 gen_struct_or_union_type_die (type, context_die, usage);
24279 if (need_pop)
24280 pop_decl_scope ();
24282 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24283 it up if it is ever completed. gen_*_type_die will set it for us
24284 when appropriate. */
24287 /* Generate a type description DIE. */
24289 static void
24290 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24291 enum debug_info_usage usage)
24293 struct array_descr_info info;
24295 if (type == NULL_TREE || type == error_mark_node)
24296 return;
24298 if (flag_checking && type)
24299 verify_type (type);
24301 if (TYPE_NAME (type) != NULL_TREE
24302 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24303 && is_redundant_typedef (TYPE_NAME (type))
24304 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24305 /* The DECL of this type is a typedef we don't want to emit debug
24306 info for but we want debug info for its underlying typedef.
24307 This can happen for e.g, the injected-class-name of a C++
24308 type. */
24309 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24311 /* If TYPE is a typedef type variant, let's generate debug info
24312 for the parent typedef which TYPE is a type of. */
24313 if (typedef_variant_p (type))
24315 if (TREE_ASM_WRITTEN (type))
24316 return;
24318 /* Prevent broken recursion; we can't hand off to the same type. */
24319 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24321 /* Give typedefs the right scope. */
24322 context_die = scope_die_for (type, context_die);
24324 TREE_ASM_WRITTEN (type) = 1;
24326 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24327 return;
24330 /* If type is an anonymous tagged type named by a typedef, let's
24331 generate debug info for the typedef. */
24332 if (is_naming_typedef_decl (TYPE_NAME (type)))
24334 /* Use the DIE of the containing namespace as the parent DIE of
24335 the type description DIE we want to generate. */
24336 if (DECL_CONTEXT (TYPE_NAME (type))
24337 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24338 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24340 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24341 return;
24344 if (lang_hooks.types.get_debug_type)
24346 tree debug_type = lang_hooks.types.get_debug_type (type);
24348 if (debug_type != NULL_TREE && debug_type != type)
24350 gen_type_die_with_usage (debug_type, context_die, usage);
24351 return;
24355 /* We are going to output a DIE to represent the unqualified version
24356 of this type (i.e. without any const or volatile qualifiers) so
24357 get the main variant (i.e. the unqualified version) of this type
24358 now. (Vectors and arrays are special because the debugging info is in the
24359 cloned type itself. Similarly function/method types can contain extra
24360 ref-qualification). */
24361 if (TREE_CODE (type) == FUNCTION_TYPE
24362 || TREE_CODE (type) == METHOD_TYPE)
24364 /* For function/method types, can't use type_main_variant here,
24365 because that can have different ref-qualifiers for C++,
24366 but try to canonicalize. */
24367 tree main = TYPE_MAIN_VARIANT (type);
24368 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24369 if (check_base_type (t, main) && check_lang_type (t, type))
24370 type = t;
24372 else if (TREE_CODE (type) != VECTOR_TYPE
24373 && TREE_CODE (type) != ARRAY_TYPE)
24374 type = type_main_variant (type);
24376 /* If this is an array type with hidden descriptor, handle it first. */
24377 if (!TREE_ASM_WRITTEN (type)
24378 && lang_hooks.types.get_array_descr_info)
24380 memset (&info, 0, sizeof (info));
24381 if (lang_hooks.types.get_array_descr_info (type, &info))
24383 /* Fortran sometimes emits array types with no dimension. */
24384 gcc_assert (info.ndimensions >= 0
24385 && (info.ndimensions
24386 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24387 gen_descr_array_type_die (type, &info, context_die);
24388 TREE_ASM_WRITTEN (type) = 1;
24389 return;
24393 if (TREE_ASM_WRITTEN (type))
24395 /* Variable-length types may be incomplete even if
24396 TREE_ASM_WRITTEN. For such types, fall through to
24397 gen_array_type_die() and possibly fill in
24398 DW_AT_{upper,lower}_bound attributes. */
24399 if ((TREE_CODE (type) != ARRAY_TYPE
24400 && TREE_CODE (type) != RECORD_TYPE
24401 && TREE_CODE (type) != UNION_TYPE
24402 && TREE_CODE (type) != QUAL_UNION_TYPE)
24403 || !variably_modified_type_p (type, NULL))
24404 return;
24407 switch (TREE_CODE (type))
24409 case ERROR_MARK:
24410 break;
24412 case POINTER_TYPE:
24413 case REFERENCE_TYPE:
24414 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24415 ensures that the gen_type_die recursion will terminate even if the
24416 type is recursive. Recursive types are possible in Ada. */
24417 /* ??? We could perhaps do this for all types before the switch
24418 statement. */
24419 TREE_ASM_WRITTEN (type) = 1;
24421 /* For these types, all that is required is that we output a DIE (or a
24422 set of DIEs) to represent the "basis" type. */
24423 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24424 DINFO_USAGE_IND_USE);
24425 break;
24427 case OFFSET_TYPE:
24428 /* This code is used for C++ pointer-to-data-member types.
24429 Output a description of the relevant class type. */
24430 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24431 DINFO_USAGE_IND_USE);
24433 /* Output a description of the type of the object pointed to. */
24434 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24435 DINFO_USAGE_IND_USE);
24437 /* Now output a DIE to represent this pointer-to-data-member type
24438 itself. */
24439 gen_ptr_to_mbr_type_die (type, context_die);
24440 break;
24442 case FUNCTION_TYPE:
24443 /* Force out return type (in case it wasn't forced out already). */
24444 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24445 DINFO_USAGE_DIR_USE);
24446 gen_subroutine_type_die (type, context_die);
24447 break;
24449 case METHOD_TYPE:
24450 /* Force out return type (in case it wasn't forced out already). */
24451 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24452 DINFO_USAGE_DIR_USE);
24453 gen_subroutine_type_die (type, context_die);
24454 break;
24456 case ARRAY_TYPE:
24457 case VECTOR_TYPE:
24458 gen_array_type_die (type, context_die);
24459 break;
24461 case ENUMERAL_TYPE:
24462 case RECORD_TYPE:
24463 case UNION_TYPE:
24464 case QUAL_UNION_TYPE:
24465 gen_tagged_type_die (type, context_die, usage);
24466 return;
24468 case VOID_TYPE:
24469 case INTEGER_TYPE:
24470 case REAL_TYPE:
24471 case FIXED_POINT_TYPE:
24472 case COMPLEX_TYPE:
24473 case BOOLEAN_TYPE:
24474 case POINTER_BOUNDS_TYPE:
24475 /* No DIEs needed for fundamental types. */
24476 break;
24478 case NULLPTR_TYPE:
24479 case LANG_TYPE:
24480 /* Just use DW_TAG_unspecified_type. */
24482 dw_die_ref type_die = lookup_type_die (type);
24483 if (type_die == NULL)
24485 tree name = TYPE_IDENTIFIER (type);
24486 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24487 type);
24488 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24489 equate_type_number_to_die (type, type_die);
24492 break;
24494 default:
24495 if (is_cxx_auto (type))
24497 tree name = TYPE_IDENTIFIER (type);
24498 dw_die_ref *die = (name == get_identifier ("auto")
24499 ? &auto_die : &decltype_auto_die);
24500 if (!*die)
24502 *die = new_die (DW_TAG_unspecified_type,
24503 comp_unit_die (), NULL_TREE);
24504 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24506 equate_type_number_to_die (type, *die);
24507 break;
24509 gcc_unreachable ();
24512 TREE_ASM_WRITTEN (type) = 1;
24515 static void
24516 gen_type_die (tree type, dw_die_ref context_die)
24518 if (type != error_mark_node)
24520 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24521 if (flag_checking)
24523 dw_die_ref die = lookup_type_die (type);
24524 if (die)
24525 check_die (die);
24530 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24531 things which are local to the given block. */
24533 static void
24534 gen_block_die (tree stmt, dw_die_ref context_die)
24536 int must_output_die = 0;
24537 bool inlined_func;
24539 /* Ignore blocks that are NULL. */
24540 if (stmt == NULL_TREE)
24541 return;
24543 inlined_func = inlined_function_outer_scope_p (stmt);
24545 /* If the block is one fragment of a non-contiguous block, do not
24546 process the variables, since they will have been done by the
24547 origin block. Do process subblocks. */
24548 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24550 tree sub;
24552 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24553 gen_block_die (sub, context_die);
24555 return;
24558 /* Determine if we need to output any Dwarf DIEs at all to represent this
24559 block. */
24560 if (inlined_func)
24561 /* The outer scopes for inlinings *must* always be represented. We
24562 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24563 must_output_die = 1;
24564 else
24566 /* Determine if this block directly contains any "significant"
24567 local declarations which we will need to output DIEs for. */
24568 if (debug_info_level > DINFO_LEVEL_TERSE)
24569 /* We are not in terse mode so *any* local declaration counts
24570 as being a "significant" one. */
24571 must_output_die = ((BLOCK_VARS (stmt) != NULL
24572 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24573 && (TREE_USED (stmt)
24574 || TREE_ASM_WRITTEN (stmt)
24575 || BLOCK_ABSTRACT (stmt)));
24576 else if ((TREE_USED (stmt)
24577 || TREE_ASM_WRITTEN (stmt)
24578 || BLOCK_ABSTRACT (stmt))
24579 && !dwarf2out_ignore_block (stmt))
24580 must_output_die = 1;
24583 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24584 DIE for any block which contains no significant local declarations at
24585 all. Rather, in such cases we just call `decls_for_scope' so that any
24586 needed Dwarf info for any sub-blocks will get properly generated. Note
24587 that in terse mode, our definition of what constitutes a "significant"
24588 local declaration gets restricted to include only inlined function
24589 instances and local (nested) function definitions. */
24590 if (must_output_die)
24592 if (inlined_func)
24594 /* If STMT block is abstract, that means we have been called
24595 indirectly from dwarf2out_abstract_function.
24596 That function rightfully marks the descendent blocks (of
24597 the abstract function it is dealing with) as being abstract,
24598 precisely to prevent us from emitting any
24599 DW_TAG_inlined_subroutine DIE as a descendent
24600 of an abstract function instance. So in that case, we should
24601 not call gen_inlined_subroutine_die.
24603 Later though, when cgraph asks dwarf2out to emit info
24604 for the concrete instance of the function decl into which
24605 the concrete instance of STMT got inlined, the later will lead
24606 to the generation of a DW_TAG_inlined_subroutine DIE. */
24607 if (! BLOCK_ABSTRACT (stmt))
24608 gen_inlined_subroutine_die (stmt, context_die);
24610 else
24611 gen_lexical_block_die (stmt, context_die);
24613 else
24614 decls_for_scope (stmt, context_die);
24617 /* Process variable DECL (or variable with origin ORIGIN) within
24618 block STMT and add it to CONTEXT_DIE. */
24619 static void
24620 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24622 dw_die_ref die;
24623 tree decl_or_origin = decl ? decl : origin;
24625 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24626 die = lookup_decl_die (decl_or_origin);
24627 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24629 if (TYPE_DECL_IS_STUB (decl_or_origin))
24630 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24631 else
24632 die = lookup_decl_die (decl_or_origin);
24633 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24634 if (! die && ! early_dwarf)
24635 return;
24637 else
24638 die = NULL;
24640 if (die != NULL && die->die_parent == NULL)
24641 add_child_die (context_die, die);
24642 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24644 if (early_dwarf)
24645 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24646 stmt, context_die);
24648 else
24649 gen_decl_die (decl, origin, NULL, context_die);
24652 /* Generate all of the decls declared within a given scope and (recursively)
24653 all of its sub-blocks. */
24655 static void
24656 decls_for_scope (tree stmt, dw_die_ref context_die)
24658 tree decl;
24659 unsigned int i;
24660 tree subblocks;
24662 /* Ignore NULL blocks. */
24663 if (stmt == NULL_TREE)
24664 return;
24666 /* Output the DIEs to represent all of the data objects and typedefs
24667 declared directly within this block but not within any nested
24668 sub-blocks. Also, nested function and tag DIEs have been
24669 generated with a parent of NULL; fix that up now. We don't
24670 have to do this if we're at -g1. */
24671 if (debug_info_level > DINFO_LEVEL_TERSE)
24673 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24674 process_scope_var (stmt, decl, NULL_TREE, context_die);
24675 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24676 origin - avoid doing this twice as we have no good way to see
24677 if we've done it once already. */
24678 if (! early_dwarf)
24679 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24680 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
24681 context_die);
24684 /* Even if we're at -g1, we need to process the subblocks in order to get
24685 inlined call information. */
24687 /* Output the DIEs to represent all sub-blocks (and the items declared
24688 therein) of this block. */
24689 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24690 subblocks != NULL;
24691 subblocks = BLOCK_CHAIN (subblocks))
24692 gen_block_die (subblocks, context_die);
24695 /* Is this a typedef we can avoid emitting? */
24697 bool
24698 is_redundant_typedef (const_tree decl)
24700 if (TYPE_DECL_IS_STUB (decl))
24701 return true;
24703 if (DECL_ARTIFICIAL (decl)
24704 && DECL_CONTEXT (decl)
24705 && is_tagged_type (DECL_CONTEXT (decl))
24706 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24707 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24708 /* Also ignore the artificial member typedef for the class name. */
24709 return true;
24711 return false;
24714 /* Return TRUE if TYPE is a typedef that names a type for linkage
24715 purposes. This kind of typedefs is produced by the C++ FE for
24716 constructs like:
24718 typedef struct {...} foo;
24720 In that case, there is no typedef variant type produced for foo.
24721 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24722 struct type. */
24724 static bool
24725 is_naming_typedef_decl (const_tree decl)
24727 if (decl == NULL_TREE
24728 || TREE_CODE (decl) != TYPE_DECL
24729 || DECL_NAMELESS (decl)
24730 || !is_tagged_type (TREE_TYPE (decl))
24731 || DECL_IS_BUILTIN (decl)
24732 || is_redundant_typedef (decl)
24733 /* It looks like Ada produces TYPE_DECLs that are very similar
24734 to C++ naming typedefs but that have different
24735 semantics. Let's be specific to c++ for now. */
24736 || !is_cxx ())
24737 return FALSE;
24739 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24740 && TYPE_NAME (TREE_TYPE (decl)) == decl
24741 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24742 != TYPE_NAME (TREE_TYPE (decl))));
24745 /* Looks up the DIE for a context. */
24747 static inline dw_die_ref
24748 lookup_context_die (tree context)
24750 if (context)
24752 /* Find die that represents this context. */
24753 if (TYPE_P (context))
24755 context = TYPE_MAIN_VARIANT (context);
24756 dw_die_ref ctx = lookup_type_die (context);
24757 if (!ctx)
24758 return NULL;
24759 return strip_naming_typedef (context, ctx);
24761 else
24762 return lookup_decl_die (context);
24764 return comp_unit_die ();
24767 /* Returns the DIE for a context. */
24769 static inline dw_die_ref
24770 get_context_die (tree context)
24772 if (context)
24774 /* Find die that represents this context. */
24775 if (TYPE_P (context))
24777 context = TYPE_MAIN_VARIANT (context);
24778 return strip_naming_typedef (context, force_type_die (context));
24780 else
24781 return force_decl_die (context);
24783 return comp_unit_die ();
24786 /* Returns the DIE for decl. A DIE will always be returned. */
24788 static dw_die_ref
24789 force_decl_die (tree decl)
24791 dw_die_ref decl_die;
24792 unsigned saved_external_flag;
24793 tree save_fn = NULL_TREE;
24794 decl_die = lookup_decl_die (decl);
24795 if (!decl_die)
24797 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24799 decl_die = lookup_decl_die (decl);
24800 if (decl_die)
24801 return decl_die;
24803 switch (TREE_CODE (decl))
24805 case FUNCTION_DECL:
24806 /* Clear current_function_decl, so that gen_subprogram_die thinks
24807 that this is a declaration. At this point, we just want to force
24808 declaration die. */
24809 save_fn = current_function_decl;
24810 current_function_decl = NULL_TREE;
24811 gen_subprogram_die (decl, context_die);
24812 current_function_decl = save_fn;
24813 break;
24815 case VAR_DECL:
24816 /* Set external flag to force declaration die. Restore it after
24817 gen_decl_die() call. */
24818 saved_external_flag = DECL_EXTERNAL (decl);
24819 DECL_EXTERNAL (decl) = 1;
24820 gen_decl_die (decl, NULL, NULL, context_die);
24821 DECL_EXTERNAL (decl) = saved_external_flag;
24822 break;
24824 case NAMESPACE_DECL:
24825 if (dwarf_version >= 3 || !dwarf_strict)
24826 dwarf2out_decl (decl);
24827 else
24828 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
24829 decl_die = comp_unit_die ();
24830 break;
24832 case TRANSLATION_UNIT_DECL:
24833 decl_die = comp_unit_die ();
24834 break;
24836 default:
24837 gcc_unreachable ();
24840 /* We should be able to find the DIE now. */
24841 if (!decl_die)
24842 decl_die = lookup_decl_die (decl);
24843 gcc_assert (decl_die);
24846 return decl_die;
24849 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
24850 always returned. */
24852 static dw_die_ref
24853 force_type_die (tree type)
24855 dw_die_ref type_die;
24857 type_die = lookup_type_die (type);
24858 if (!type_die)
24860 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
24862 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
24863 false, context_die);
24864 gcc_assert (type_die);
24866 return type_die;
24869 /* Force out any required namespaces to be able to output DECL,
24870 and return the new context_die for it, if it's changed. */
24872 static dw_die_ref
24873 setup_namespace_context (tree thing, dw_die_ref context_die)
24875 tree context = (DECL_P (thing)
24876 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
24877 if (context && TREE_CODE (context) == NAMESPACE_DECL)
24878 /* Force out the namespace. */
24879 context_die = force_decl_die (context);
24881 return context_die;
24884 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
24885 type) within its namespace, if appropriate.
24887 For compatibility with older debuggers, namespace DIEs only contain
24888 declarations; all definitions are emitted at CU scope, with
24889 DW_AT_specification pointing to the declaration (like with class
24890 members). */
24892 static dw_die_ref
24893 declare_in_namespace (tree thing, dw_die_ref context_die)
24895 dw_die_ref ns_context;
24897 if (debug_info_level <= DINFO_LEVEL_TERSE)
24898 return context_die;
24900 /* External declarations in the local scope only need to be emitted
24901 once, not once in the namespace and once in the scope.
24903 This avoids declaring the `extern' below in the
24904 namespace DIE as well as in the innermost scope:
24906 namespace S
24908 int i=5;
24909 int foo()
24911 int i=8;
24912 extern int i;
24913 return i;
24917 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
24918 return context_die;
24920 /* If this decl is from an inlined function, then don't try to emit it in its
24921 namespace, as we will get confused. It would have already been emitted
24922 when the abstract instance of the inline function was emitted anyways. */
24923 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
24924 return context_die;
24926 ns_context = setup_namespace_context (thing, context_die);
24928 if (ns_context != context_die)
24930 if (is_fortran ())
24931 return ns_context;
24932 if (DECL_P (thing))
24933 gen_decl_die (thing, NULL, NULL, ns_context);
24934 else
24935 gen_type_die (thing, ns_context);
24937 return context_die;
24940 /* Generate a DIE for a namespace or namespace alias. */
24942 static void
24943 gen_namespace_die (tree decl, dw_die_ref context_die)
24945 dw_die_ref namespace_die;
24947 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
24948 they are an alias of. */
24949 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
24951 /* Output a real namespace or module. */
24952 context_die = setup_namespace_context (decl, comp_unit_die ());
24953 namespace_die = new_die (is_fortran ()
24954 ? DW_TAG_module : DW_TAG_namespace,
24955 context_die, decl);
24956 /* For Fortran modules defined in different CU don't add src coords. */
24957 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
24959 const char *name = dwarf2_name (decl, 0);
24960 if (name)
24961 add_name_attribute (namespace_die, name);
24963 else
24964 add_name_and_src_coords_attributes (namespace_die, decl);
24965 if (DECL_EXTERNAL (decl))
24966 add_AT_flag (namespace_die, DW_AT_declaration, 1);
24967 equate_decl_number_to_die (decl, namespace_die);
24969 else
24971 /* Output a namespace alias. */
24973 /* Force out the namespace we are an alias of, if necessary. */
24974 dw_die_ref origin_die
24975 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
24977 if (DECL_FILE_SCOPE_P (decl)
24978 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
24979 context_die = setup_namespace_context (decl, comp_unit_die ());
24980 /* Now create the namespace alias DIE. */
24981 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
24982 add_name_and_src_coords_attributes (namespace_die, decl);
24983 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
24984 equate_decl_number_to_die (decl, namespace_die);
24986 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
24987 if (want_pubnames ())
24988 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
24991 /* Generate Dwarf debug information for a decl described by DECL.
24992 The return value is currently only meaningful for PARM_DECLs,
24993 for all other decls it returns NULL.
24995 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
24996 It can be NULL otherwise. */
24998 static dw_die_ref
24999 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25000 dw_die_ref context_die)
25002 tree decl_or_origin = decl ? decl : origin;
25003 tree class_origin = NULL, ultimate_origin;
25005 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25006 return NULL;
25008 /* Ignore pointer bounds decls. */
25009 if (DECL_P (decl_or_origin)
25010 && TREE_TYPE (decl_or_origin)
25011 && POINTER_BOUNDS_P (decl_or_origin))
25012 return NULL;
25014 switch (TREE_CODE (decl_or_origin))
25016 case ERROR_MARK:
25017 break;
25019 case CONST_DECL:
25020 if (!is_fortran () && !is_ada ())
25022 /* The individual enumerators of an enum type get output when we output
25023 the Dwarf representation of the relevant enum type itself. */
25024 break;
25027 /* Emit its type. */
25028 gen_type_die (TREE_TYPE (decl), context_die);
25030 /* And its containing namespace. */
25031 context_die = declare_in_namespace (decl, context_die);
25033 gen_const_die (decl, context_die);
25034 break;
25036 case FUNCTION_DECL:
25037 /* Don't output any DIEs to represent mere function declarations,
25038 unless they are class members or explicit block externs. */
25039 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25040 && DECL_FILE_SCOPE_P (decl_or_origin)
25041 && (current_function_decl == NULL_TREE
25042 || DECL_ARTIFICIAL (decl_or_origin)))
25043 break;
25045 #if 0
25046 /* FIXME */
25047 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25048 on local redeclarations of global functions. That seems broken. */
25049 if (current_function_decl != decl)
25050 /* This is only a declaration. */;
25051 #endif
25053 /* If we're emitting a clone, emit info for the abstract instance. */
25054 if (origin || DECL_ORIGIN (decl) != decl)
25055 dwarf2out_abstract_function (origin
25056 ? DECL_ORIGIN (origin)
25057 : DECL_ABSTRACT_ORIGIN (decl));
25059 /* If we're emitting an out-of-line copy of an inline function,
25060 emit info for the abstract instance and set up to refer to it. */
25061 else if (cgraph_function_possibly_inlined_p (decl)
25062 && ! DECL_ABSTRACT_P (decl)
25063 && ! class_or_namespace_scope_p (context_die)
25064 /* dwarf2out_abstract_function won't emit a die if this is just
25065 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25066 that case, because that works only if we have a die. */
25067 && DECL_INITIAL (decl) != NULL_TREE)
25069 dwarf2out_abstract_function (decl);
25070 set_decl_origin_self (decl);
25073 /* Otherwise we're emitting the primary DIE for this decl. */
25074 else if (debug_info_level > DINFO_LEVEL_TERSE)
25076 /* Before we describe the FUNCTION_DECL itself, make sure that we
25077 have its containing type. */
25078 if (!origin)
25079 origin = decl_class_context (decl);
25080 if (origin != NULL_TREE)
25081 gen_type_die (origin, context_die);
25083 /* And its return type. */
25084 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25086 /* And its virtual context. */
25087 if (DECL_VINDEX (decl) != NULL_TREE)
25088 gen_type_die (DECL_CONTEXT (decl), context_die);
25090 /* Make sure we have a member DIE for decl. */
25091 if (origin != NULL_TREE)
25092 gen_type_die_for_member (origin, decl, context_die);
25094 /* And its containing namespace. */
25095 context_die = declare_in_namespace (decl, context_die);
25098 /* Now output a DIE to represent the function itself. */
25099 if (decl)
25100 gen_subprogram_die (decl, context_die);
25101 break;
25103 case TYPE_DECL:
25104 /* If we are in terse mode, don't generate any DIEs to represent any
25105 actual typedefs. */
25106 if (debug_info_level <= DINFO_LEVEL_TERSE)
25107 break;
25109 /* In the special case of a TYPE_DECL node representing the declaration
25110 of some type tag, if the given TYPE_DECL is marked as having been
25111 instantiated from some other (original) TYPE_DECL node (e.g. one which
25112 was generated within the original definition of an inline function) we
25113 used to generate a special (abbreviated) DW_TAG_structure_type,
25114 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25115 should be actually referencing those DIEs, as variable DIEs with that
25116 type would be emitted already in the abstract origin, so it was always
25117 removed during unused type prunning. Don't add anything in this
25118 case. */
25119 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25120 break;
25122 if (is_redundant_typedef (decl))
25123 gen_type_die (TREE_TYPE (decl), context_die);
25124 else
25125 /* Output a DIE to represent the typedef itself. */
25126 gen_typedef_die (decl, context_die);
25127 break;
25129 case LABEL_DECL:
25130 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25131 gen_label_die (decl, context_die);
25132 break;
25134 case VAR_DECL:
25135 case RESULT_DECL:
25136 /* If we are in terse mode, don't generate any DIEs to represent any
25137 variable declarations or definitions. */
25138 if (debug_info_level <= DINFO_LEVEL_TERSE)
25139 break;
25141 /* Output any DIEs that are needed to specify the type of this data
25142 object. */
25143 if (decl_by_reference_p (decl_or_origin))
25144 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25145 else
25146 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25148 /* And its containing type. */
25149 class_origin = decl_class_context (decl_or_origin);
25150 if (class_origin != NULL_TREE)
25151 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25153 /* And its containing namespace. */
25154 context_die = declare_in_namespace (decl_or_origin, context_die);
25156 /* Now output the DIE to represent the data object itself. This gets
25157 complicated because of the possibility that the VAR_DECL really
25158 represents an inlined instance of a formal parameter for an inline
25159 function. */
25160 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25161 if (ultimate_origin != NULL_TREE
25162 && TREE_CODE (ultimate_origin) == PARM_DECL)
25163 gen_formal_parameter_die (decl, origin,
25164 true /* Emit name attribute. */,
25165 context_die);
25166 else
25167 gen_variable_die (decl, origin, context_die);
25168 break;
25170 case FIELD_DECL:
25171 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25172 /* Ignore the nameless fields that are used to skip bits but handle C++
25173 anonymous unions and structs. */
25174 if (DECL_NAME (decl) != NULL_TREE
25175 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25176 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25178 gen_type_die (member_declared_type (decl), context_die);
25179 gen_field_die (decl, ctx, context_die);
25181 break;
25183 case PARM_DECL:
25184 if (DECL_BY_REFERENCE (decl_or_origin))
25185 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25186 else
25187 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25188 return gen_formal_parameter_die (decl, origin,
25189 true /* Emit name attribute. */,
25190 context_die);
25192 case NAMESPACE_DECL:
25193 if (dwarf_version >= 3 || !dwarf_strict)
25194 gen_namespace_die (decl, context_die);
25195 break;
25197 case IMPORTED_DECL:
25198 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25199 DECL_CONTEXT (decl), context_die);
25200 break;
25202 case NAMELIST_DECL:
25203 gen_namelist_decl (DECL_NAME (decl), context_die,
25204 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25205 break;
25207 default:
25208 /* Probably some frontend-internal decl. Assume we don't care. */
25209 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25210 break;
25213 return NULL;
25216 /* Output initial debug information for global DECL. Called at the
25217 end of the parsing process.
25219 This is the initial debug generation process. As such, the DIEs
25220 generated may be incomplete. A later debug generation pass
25221 (dwarf2out_late_global_decl) will augment the information generated
25222 in this pass (e.g., with complete location info). */
25224 static void
25225 dwarf2out_early_global_decl (tree decl)
25227 set_early_dwarf s;
25229 /* gen_decl_die() will set DECL_ABSTRACT because
25230 cgraph_function_possibly_inlined_p() returns true. This is in
25231 turn will cause DW_AT_inline attributes to be set.
25233 This happens because at early dwarf generation, there is no
25234 cgraph information, causing cgraph_function_possibly_inlined_p()
25235 to return true. Trick cgraph_function_possibly_inlined_p()
25236 while we generate dwarf early. */
25237 bool save = symtab->global_info_ready;
25238 symtab->global_info_ready = true;
25240 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25241 other DECLs and they can point to template types or other things
25242 that dwarf2out can't handle when done via dwarf2out_decl. */
25243 if (TREE_CODE (decl) != TYPE_DECL
25244 && TREE_CODE (decl) != PARM_DECL)
25246 tree save_fndecl = current_function_decl;
25247 if (TREE_CODE (decl) == FUNCTION_DECL)
25249 /* No cfun means the symbol has no body, so there's nothing
25250 to emit. */
25251 if (!DECL_STRUCT_FUNCTION (decl))
25252 goto early_decl_exit;
25254 /* For nested functions, make sure we have DIEs for the parents first
25255 so that all nested DIEs are generated at the proper scope in the
25256 first shot. */
25257 tree context = decl_function_context (decl);
25258 if (context != NULL && lookup_decl_die (context) == NULL)
25260 current_function_decl = context;
25261 dwarf2out_decl (context);
25264 current_function_decl = decl;
25266 dwarf2out_decl (decl);
25267 if (TREE_CODE (decl) == FUNCTION_DECL)
25268 current_function_decl = save_fndecl;
25270 early_decl_exit:
25271 symtab->global_info_ready = save;
25274 /* Output debug information for global decl DECL. Called from
25275 toplev.c after compilation proper has finished. */
25277 static void
25278 dwarf2out_late_global_decl (tree decl)
25280 /* Fill-in any location information we were unable to determine
25281 on the first pass. */
25282 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25284 dw_die_ref die = lookup_decl_die (decl);
25286 /* We have to generate early debug late for LTO. */
25287 if (! die && in_lto_p)
25289 dwarf2out_decl (decl);
25290 die = lookup_decl_die (decl);
25293 if (die)
25295 /* We get called via the symtab code invoking late_global_decl
25296 for symbols that are optimized out. Do not add locations
25297 for those. */
25298 varpool_node *node = varpool_node::get (decl);
25299 if (! node || ! node->definition)
25300 tree_add_const_value_attribute_for_decl (die, decl);
25301 else
25302 add_location_or_const_value_attribute (die, decl, false);
25307 /* Output debug information for type decl DECL. Called from toplev.c
25308 and from language front ends (to record built-in types). */
25309 static void
25310 dwarf2out_type_decl (tree decl, int local)
25312 if (!local)
25314 set_early_dwarf s;
25315 dwarf2out_decl (decl);
25319 /* Output debug information for imported module or decl DECL.
25320 NAME is non-NULL name in the lexical block if the decl has been renamed.
25321 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25322 that DECL belongs to.
25323 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25324 static void
25325 dwarf2out_imported_module_or_decl_1 (tree decl,
25326 tree name,
25327 tree lexical_block,
25328 dw_die_ref lexical_block_die)
25330 expanded_location xloc;
25331 dw_die_ref imported_die = NULL;
25332 dw_die_ref at_import_die;
25334 if (TREE_CODE (decl) == IMPORTED_DECL)
25336 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25337 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25338 gcc_assert (decl);
25340 else
25341 xloc = expand_location (input_location);
25343 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25345 at_import_die = force_type_die (TREE_TYPE (decl));
25346 /* For namespace N { typedef void T; } using N::T; base_type_die
25347 returns NULL, but DW_TAG_imported_declaration requires
25348 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25349 if (!at_import_die)
25351 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25352 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25353 at_import_die = lookup_type_die (TREE_TYPE (decl));
25354 gcc_assert (at_import_die);
25357 else
25359 at_import_die = lookup_decl_die (decl);
25360 if (!at_import_die)
25362 /* If we're trying to avoid duplicate debug info, we may not have
25363 emitted the member decl for this field. Emit it now. */
25364 if (TREE_CODE (decl) == FIELD_DECL)
25366 tree type = DECL_CONTEXT (decl);
25368 if (TYPE_CONTEXT (type)
25369 && TYPE_P (TYPE_CONTEXT (type))
25370 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25371 DINFO_USAGE_DIR_USE))
25372 return;
25373 gen_type_die_for_member (type, decl,
25374 get_context_die (TYPE_CONTEXT (type)));
25376 if (TREE_CODE (decl) == NAMELIST_DECL)
25377 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25378 get_context_die (DECL_CONTEXT (decl)),
25379 NULL_TREE);
25380 else
25381 at_import_die = force_decl_die (decl);
25385 if (TREE_CODE (decl) == NAMESPACE_DECL)
25387 if (dwarf_version >= 3 || !dwarf_strict)
25388 imported_die = new_die (DW_TAG_imported_module,
25389 lexical_block_die,
25390 lexical_block);
25391 else
25392 return;
25394 else
25395 imported_die = new_die (DW_TAG_imported_declaration,
25396 lexical_block_die,
25397 lexical_block);
25399 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25400 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25401 if (name)
25402 add_AT_string (imported_die, DW_AT_name,
25403 IDENTIFIER_POINTER (name));
25404 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25407 /* Output debug information for imported module or decl DECL.
25408 NAME is non-NULL name in context if the decl has been renamed.
25409 CHILD is true if decl is one of the renamed decls as part of
25410 importing whole module. */
25412 static void
25413 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25414 bool child)
25416 /* dw_die_ref at_import_die; */
25417 dw_die_ref scope_die;
25419 if (debug_info_level <= DINFO_LEVEL_TERSE)
25420 return;
25422 gcc_assert (decl);
25424 set_early_dwarf s;
25426 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25427 We need decl DIE for reference and scope die. First, get DIE for the decl
25428 itself. */
25430 /* Get the scope die for decl context. Use comp_unit_die for global module
25431 or decl. If die is not found for non globals, force new die. */
25432 if (context
25433 && TYPE_P (context)
25434 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25435 return;
25437 scope_die = get_context_die (context);
25439 if (child)
25441 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25442 there is nothing we can do, here. */
25443 if (dwarf_version < 3 && dwarf_strict)
25444 return;
25446 gcc_assert (scope_die->die_child);
25447 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25448 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25449 scope_die = scope_die->die_child;
25452 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25453 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25456 /* Output debug information for namelists. */
25458 static dw_die_ref
25459 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25461 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25462 tree value;
25463 unsigned i;
25465 if (debug_info_level <= DINFO_LEVEL_TERSE)
25466 return NULL;
25468 gcc_assert (scope_die != NULL);
25469 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25470 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25472 /* If there are no item_decls, we have a nondefining namelist, e.g.
25473 with USE association; hence, set DW_AT_declaration. */
25474 if (item_decls == NULL_TREE)
25476 add_AT_flag (nml_die, DW_AT_declaration, 1);
25477 return nml_die;
25480 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25482 nml_item_ref_die = lookup_decl_die (value);
25483 if (!nml_item_ref_die)
25484 nml_item_ref_die = force_decl_die (value);
25486 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25487 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25489 return nml_die;
25493 /* Write the debugging output for DECL and return the DIE. */
25495 static void
25496 dwarf2out_decl (tree decl)
25498 dw_die_ref context_die = comp_unit_die ();
25500 switch (TREE_CODE (decl))
25502 case ERROR_MARK:
25503 return;
25505 case FUNCTION_DECL:
25506 /* What we would really like to do here is to filter out all mere
25507 file-scope declarations of file-scope functions which are never
25508 referenced later within this translation unit (and keep all of ones
25509 that *are* referenced later on) but we aren't clairvoyant, so we have
25510 no idea which functions will be referenced in the future (i.e. later
25511 on within the current translation unit). So here we just ignore all
25512 file-scope function declarations which are not also definitions. If
25513 and when the debugger needs to know something about these functions,
25514 it will have to hunt around and find the DWARF information associated
25515 with the definition of the function.
25517 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25518 nodes represent definitions and which ones represent mere
25519 declarations. We have to check DECL_INITIAL instead. That's because
25520 the C front-end supports some weird semantics for "extern inline"
25521 function definitions. These can get inlined within the current
25522 translation unit (and thus, we need to generate Dwarf info for their
25523 abstract instances so that the Dwarf info for the concrete inlined
25524 instances can have something to refer to) but the compiler never
25525 generates any out-of-lines instances of such things (despite the fact
25526 that they *are* definitions).
25528 The important point is that the C front-end marks these "extern
25529 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25530 them anyway. Note that the C++ front-end also plays some similar games
25531 for inline function definitions appearing within include files which
25532 also contain `#pragma interface' pragmas.
25534 If we are called from dwarf2out_abstract_function output a DIE
25535 anyway. We can end up here this way with early inlining and LTO
25536 where the inlined function is output in a different LTRANS unit
25537 or not at all. */
25538 if (DECL_INITIAL (decl) == NULL_TREE
25539 && ! DECL_ABSTRACT_P (decl))
25540 return;
25542 /* If we're a nested function, initially use a parent of NULL; if we're
25543 a plain function, this will be fixed up in decls_for_scope. If
25544 we're a method, it will be ignored, since we already have a DIE. */
25545 if (decl_function_context (decl)
25546 /* But if we're in terse mode, we don't care about scope. */
25547 && debug_info_level > DINFO_LEVEL_TERSE)
25548 context_die = NULL;
25549 break;
25551 case VAR_DECL:
25552 /* For local statics lookup proper context die. */
25553 if (local_function_static (decl))
25554 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25556 /* If we are in terse mode, don't generate any DIEs to represent any
25557 variable declarations or definitions. */
25558 if (debug_info_level <= DINFO_LEVEL_TERSE)
25559 return;
25560 break;
25562 case CONST_DECL:
25563 if (debug_info_level <= DINFO_LEVEL_TERSE)
25564 return;
25565 if (!is_fortran () && !is_ada ())
25566 return;
25567 if (TREE_STATIC (decl) && decl_function_context (decl))
25568 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25569 break;
25571 case NAMESPACE_DECL:
25572 case IMPORTED_DECL:
25573 if (debug_info_level <= DINFO_LEVEL_TERSE)
25574 return;
25575 if (lookup_decl_die (decl) != NULL)
25576 return;
25577 break;
25579 case TYPE_DECL:
25580 /* Don't emit stubs for types unless they are needed by other DIEs. */
25581 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25582 return;
25584 /* Don't bother trying to generate any DIEs to represent any of the
25585 normal built-in types for the language we are compiling. */
25586 if (DECL_IS_BUILTIN (decl))
25587 return;
25589 /* If we are in terse mode, don't generate any DIEs for types. */
25590 if (debug_info_level <= DINFO_LEVEL_TERSE)
25591 return;
25593 /* If we're a function-scope tag, initially use a parent of NULL;
25594 this will be fixed up in decls_for_scope. */
25595 if (decl_function_context (decl))
25596 context_die = NULL;
25598 break;
25600 case NAMELIST_DECL:
25601 break;
25603 default:
25604 return;
25607 gen_decl_die (decl, NULL, NULL, context_die);
25609 if (flag_checking)
25611 dw_die_ref die = lookup_decl_die (decl);
25612 if (die)
25613 check_die (die);
25617 /* Write the debugging output for DECL. */
25619 static void
25620 dwarf2out_function_decl (tree decl)
25622 dwarf2out_decl (decl);
25623 call_arg_locations = NULL;
25624 call_arg_loc_last = NULL;
25625 call_site_count = -1;
25626 tail_call_site_count = -1;
25627 decl_loc_table->empty ();
25628 cached_dw_loc_list_table->empty ();
25631 /* Output a marker (i.e. a label) for the beginning of the generated code for
25632 a lexical block. */
25634 static void
25635 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25636 unsigned int blocknum)
25638 switch_to_section (current_function_section ());
25639 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25642 /* Output a marker (i.e. a label) for the end of the generated code for a
25643 lexical block. */
25645 static void
25646 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25648 switch_to_section (current_function_section ());
25649 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25652 /* Returns nonzero if it is appropriate not to emit any debugging
25653 information for BLOCK, because it doesn't contain any instructions.
25655 Don't allow this for blocks with nested functions or local classes
25656 as we would end up with orphans, and in the presence of scheduling
25657 we may end up calling them anyway. */
25659 static bool
25660 dwarf2out_ignore_block (const_tree block)
25662 tree decl;
25663 unsigned int i;
25665 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25666 if (TREE_CODE (decl) == FUNCTION_DECL
25667 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25668 return 0;
25669 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25671 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25672 if (TREE_CODE (decl) == FUNCTION_DECL
25673 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25674 return 0;
25677 return 1;
25680 /* Hash table routines for file_hash. */
25682 bool
25683 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25685 return filename_cmp (p1->filename, p2) == 0;
25688 hashval_t
25689 dwarf_file_hasher::hash (dwarf_file_data *p)
25691 return htab_hash_string (p->filename);
25694 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25695 dwarf2out.c) and return its "index". The index of each (known) filename is
25696 just a unique number which is associated with only that one filename. We
25697 need such numbers for the sake of generating labels (in the .debug_sfnames
25698 section) and references to those files numbers (in the .debug_srcinfo
25699 and .debug_macinfo sections). If the filename given as an argument is not
25700 found in our current list, add it to the list and assign it the next
25701 available unique index number. */
25703 static struct dwarf_file_data *
25704 lookup_filename (const char *file_name)
25706 struct dwarf_file_data * created;
25708 if (!file_name)
25709 return NULL;
25711 dwarf_file_data **slot
25712 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25713 INSERT);
25714 if (*slot)
25715 return *slot;
25717 created = ggc_alloc<dwarf_file_data> ();
25718 created->filename = file_name;
25719 created->emitted_number = 0;
25720 *slot = created;
25721 return created;
25724 /* If the assembler will construct the file table, then translate the compiler
25725 internal file table number into the assembler file table number, and emit
25726 a .file directive if we haven't already emitted one yet. The file table
25727 numbers are different because we prune debug info for unused variables and
25728 types, which may include filenames. */
25730 static int
25731 maybe_emit_file (struct dwarf_file_data * fd)
25733 if (! fd->emitted_number)
25735 if (last_emitted_file)
25736 fd->emitted_number = last_emitted_file->emitted_number + 1;
25737 else
25738 fd->emitted_number = 1;
25739 last_emitted_file = fd;
25741 if (DWARF2_ASM_LINE_DEBUG_INFO)
25743 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25744 output_quoted_string (asm_out_file,
25745 remap_debug_filename (fd->filename));
25746 fputc ('\n', asm_out_file);
25750 return fd->emitted_number;
25753 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25754 That generation should happen after function debug info has been
25755 generated. The value of the attribute is the constant value of ARG. */
25757 static void
25758 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25760 die_arg_entry entry;
25762 if (!die || !arg)
25763 return;
25765 gcc_assert (early_dwarf);
25767 if (!tmpl_value_parm_die_table)
25768 vec_alloc (tmpl_value_parm_die_table, 32);
25770 entry.die = die;
25771 entry.arg = arg;
25772 vec_safe_push (tmpl_value_parm_die_table, entry);
25775 /* Return TRUE if T is an instance of generic type, FALSE
25776 otherwise. */
25778 static bool
25779 generic_type_p (tree t)
25781 if (t == NULL_TREE || !TYPE_P (t))
25782 return false;
25783 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25786 /* Schedule the generation of the generic parameter dies for the
25787 instance of generic type T. The proper generation itself is later
25788 done by gen_scheduled_generic_parms_dies. */
25790 static void
25791 schedule_generic_params_dies_gen (tree t)
25793 if (!generic_type_p (t))
25794 return;
25796 gcc_assert (early_dwarf);
25798 if (!generic_type_instances)
25799 vec_alloc (generic_type_instances, 256);
25801 vec_safe_push (generic_type_instances, t);
25804 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25805 by append_entry_to_tmpl_value_parm_die_table. This function must
25806 be called after function DIEs have been generated. */
25808 static void
25809 gen_remaining_tmpl_value_param_die_attribute (void)
25811 if (tmpl_value_parm_die_table)
25813 unsigned i, j;
25814 die_arg_entry *e;
25816 /* We do this in two phases - first get the cases we can
25817 handle during early-finish, preserving those we cannot
25818 (containing symbolic constants where we don't yet know
25819 whether we are going to output the referenced symbols).
25820 For those we try again at late-finish. */
25821 j = 0;
25822 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
25824 if (!tree_add_const_value_attribute (e->die, e->arg))
25826 dw_loc_descr_ref loc = NULL;
25827 if (! early_dwarf
25828 && (dwarf_version >= 5 || !dwarf_strict))
25829 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
25830 if (loc)
25831 add_AT_loc (e->die, DW_AT_location, loc);
25832 else
25833 (*tmpl_value_parm_die_table)[j++] = *e;
25836 tmpl_value_parm_die_table->truncate (j);
25840 /* Generate generic parameters DIEs for instances of generic types
25841 that have been previously scheduled by
25842 schedule_generic_params_dies_gen. This function must be called
25843 after all the types of the CU have been laid out. */
25845 static void
25846 gen_scheduled_generic_parms_dies (void)
25848 unsigned i;
25849 tree t;
25851 if (!generic_type_instances)
25852 return;
25854 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
25855 if (COMPLETE_TYPE_P (t))
25856 gen_generic_params_dies (t);
25858 generic_type_instances = NULL;
25862 /* Replace DW_AT_name for the decl with name. */
25864 static void
25865 dwarf2out_set_name (tree decl, tree name)
25867 dw_die_ref die;
25868 dw_attr_node *attr;
25869 const char *dname;
25871 die = TYPE_SYMTAB_DIE (decl);
25872 if (!die)
25873 return;
25875 dname = dwarf2_name (name, 0);
25876 if (!dname)
25877 return;
25879 attr = get_AT (die, DW_AT_name);
25880 if (attr)
25882 struct indirect_string_node *node;
25884 node = find_AT_string (dname);
25885 /* replace the string. */
25886 attr->dw_attr_val.v.val_str = node;
25889 else
25890 add_name_attribute (die, dname);
25893 /* True if before or during processing of the first function being emitted. */
25894 static bool in_first_function_p = true;
25895 /* True if loc_note during dwarf2out_var_location call might still be
25896 before first real instruction at address equal to .Ltext0. */
25897 static bool maybe_at_text_label_p = true;
25898 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
25899 static unsigned int first_loclabel_num_not_at_text_label;
25901 /* Called by the final INSN scan whenever we see a var location. We
25902 use it to drop labels in the right places, and throw the location in
25903 our lookup table. */
25905 static void
25906 dwarf2out_var_location (rtx_insn *loc_note)
25908 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
25909 struct var_loc_node *newloc;
25910 rtx_insn *next_real, *next_note;
25911 rtx_insn *call_insn = NULL;
25912 static const char *last_label;
25913 static const char *last_postcall_label;
25914 static bool last_in_cold_section_p;
25915 static rtx_insn *expected_next_loc_note;
25916 tree decl;
25917 bool var_loc_p;
25919 if (!NOTE_P (loc_note))
25921 if (CALL_P (loc_note))
25923 call_site_count++;
25924 if (SIBLING_CALL_P (loc_note))
25925 tail_call_site_count++;
25926 if (optimize == 0 && !flag_var_tracking)
25928 /* When the var-tracking pass is not running, there is no note
25929 for indirect calls whose target is compile-time known. In this
25930 case, process such calls specifically so that we generate call
25931 sites for them anyway. */
25932 rtx x = PATTERN (loc_note);
25933 if (GET_CODE (x) == PARALLEL)
25934 x = XVECEXP (x, 0, 0);
25935 if (GET_CODE (x) == SET)
25936 x = SET_SRC (x);
25937 if (GET_CODE (x) == CALL)
25938 x = XEXP (x, 0);
25939 if (!MEM_P (x)
25940 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
25941 || !SYMBOL_REF_DECL (XEXP (x, 0))
25942 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
25943 != FUNCTION_DECL))
25945 call_insn = loc_note;
25946 loc_note = NULL;
25947 var_loc_p = false;
25949 next_real = next_real_insn (call_insn);
25950 next_note = NULL;
25951 cached_next_real_insn = NULL;
25952 goto create_label;
25956 return;
25959 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
25960 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
25961 return;
25963 /* Optimize processing a large consecutive sequence of location
25964 notes so we don't spend too much time in next_real_insn. If the
25965 next insn is another location note, remember the next_real_insn
25966 calculation for next time. */
25967 next_real = cached_next_real_insn;
25968 if (next_real)
25970 if (expected_next_loc_note != loc_note)
25971 next_real = NULL;
25974 next_note = NEXT_INSN (loc_note);
25975 if (! next_note
25976 || next_note->deleted ()
25977 || ! NOTE_P (next_note)
25978 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
25979 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
25980 next_note = NULL;
25982 if (! next_real)
25983 next_real = next_real_insn (loc_note);
25985 if (next_note)
25987 expected_next_loc_note = next_note;
25988 cached_next_real_insn = next_real;
25990 else
25991 cached_next_real_insn = NULL;
25993 /* If there are no instructions which would be affected by this note,
25994 don't do anything. */
25995 if (var_loc_p
25996 && next_real == NULL_RTX
25997 && !NOTE_DURING_CALL_P (loc_note))
25998 return;
26000 create_label:
26002 if (next_real == NULL_RTX)
26003 next_real = get_last_insn ();
26005 /* If there were any real insns between note we processed last time
26006 and this note (or if it is the first note), clear
26007 last_{,postcall_}label so that they are not reused this time. */
26008 if (last_var_location_insn == NULL_RTX
26009 || last_var_location_insn != next_real
26010 || last_in_cold_section_p != in_cold_section_p)
26012 last_label = NULL;
26013 last_postcall_label = NULL;
26016 if (var_loc_p)
26018 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26019 newloc = add_var_loc_to_decl (decl, loc_note,
26020 NOTE_DURING_CALL_P (loc_note)
26021 ? last_postcall_label : last_label);
26022 if (newloc == NULL)
26023 return;
26025 else
26027 decl = NULL_TREE;
26028 newloc = NULL;
26031 /* If there were no real insns between note we processed last time
26032 and this note, use the label we emitted last time. Otherwise
26033 create a new label and emit it. */
26034 if (last_label == NULL)
26036 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26037 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26038 loclabel_num++;
26039 last_label = ggc_strdup (loclabel);
26040 /* See if loclabel might be equal to .Ltext0. If yes,
26041 bump first_loclabel_num_not_at_text_label. */
26042 if (!have_multiple_function_sections
26043 && in_first_function_p
26044 && maybe_at_text_label_p)
26046 static rtx_insn *last_start;
26047 rtx_insn *insn;
26048 for (insn = loc_note; insn; insn = previous_insn (insn))
26049 if (insn == last_start)
26050 break;
26051 else if (!NONDEBUG_INSN_P (insn))
26052 continue;
26053 else
26055 rtx body = PATTERN (insn);
26056 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26057 continue;
26058 /* Inline asm could occupy zero bytes. */
26059 else if (GET_CODE (body) == ASM_INPUT
26060 || asm_noperands (body) >= 0)
26061 continue;
26062 #ifdef HAVE_attr_length
26063 else if (get_attr_min_length (insn) == 0)
26064 continue;
26065 #endif
26066 else
26068 /* Assume insn has non-zero length. */
26069 maybe_at_text_label_p = false;
26070 break;
26073 if (maybe_at_text_label_p)
26075 last_start = loc_note;
26076 first_loclabel_num_not_at_text_label = loclabel_num;
26081 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26082 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26084 if (!var_loc_p)
26086 struct call_arg_loc_node *ca_loc
26087 = ggc_cleared_alloc<call_arg_loc_node> ();
26088 rtx_insn *prev
26089 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26091 ca_loc->call_arg_loc_note = loc_note;
26092 ca_loc->next = NULL;
26093 ca_loc->label = last_label;
26094 gcc_assert (prev
26095 && (CALL_P (prev)
26096 || (NONJUMP_INSN_P (prev)
26097 && GET_CODE (PATTERN (prev)) == SEQUENCE
26098 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26099 if (!CALL_P (prev))
26100 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26101 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26103 /* Look for a SYMBOL_REF in the "prev" instruction. */
26104 rtx x = get_call_rtx_from (PATTERN (prev));
26105 if (x)
26107 /* Try to get the call symbol, if any. */
26108 if (MEM_P (XEXP (x, 0)))
26109 x = XEXP (x, 0);
26110 /* First, look for a memory access to a symbol_ref. */
26111 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26112 && SYMBOL_REF_DECL (XEXP (x, 0))
26113 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26114 ca_loc->symbol_ref = XEXP (x, 0);
26115 /* Otherwise, look at a compile-time known user-level function
26116 declaration. */
26117 else if (MEM_P (x)
26118 && MEM_EXPR (x)
26119 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26120 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26123 ca_loc->block = insn_scope (prev);
26124 if (call_arg_locations)
26125 call_arg_loc_last->next = ca_loc;
26126 else
26127 call_arg_locations = ca_loc;
26128 call_arg_loc_last = ca_loc;
26130 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26131 newloc->label = last_label;
26132 else
26134 if (!last_postcall_label)
26136 sprintf (loclabel, "%s-1", last_label);
26137 last_postcall_label = ggc_strdup (loclabel);
26139 newloc->label = last_postcall_label;
26142 last_var_location_insn = next_real;
26143 last_in_cold_section_p = in_cold_section_p;
26146 /* Called from finalize_size_functions for size functions so that their body
26147 can be encoded in the debug info to describe the layout of variable-length
26148 structures. */
26150 static void
26151 dwarf2out_size_function (tree decl)
26153 function_to_dwarf_procedure (decl);
26156 /* Note in one location list that text section has changed. */
26159 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26161 var_loc_list *list = *slot;
26162 if (list->first)
26163 list->last_before_switch
26164 = list->last->next ? list->last->next : list->last;
26165 return 1;
26168 /* Note in all location lists that text section has changed. */
26170 static void
26171 var_location_switch_text_section (void)
26173 if (decl_loc_table == NULL)
26174 return;
26176 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26179 /* Create a new line number table. */
26181 static dw_line_info_table *
26182 new_line_info_table (void)
26184 dw_line_info_table *table;
26186 table = ggc_cleared_alloc<dw_line_info_table> ();
26187 table->file_num = 1;
26188 table->line_num = 1;
26189 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26191 return table;
26194 /* Lookup the "current" table into which we emit line info, so
26195 that we don't have to do it for every source line. */
26197 static void
26198 set_cur_line_info_table (section *sec)
26200 dw_line_info_table *table;
26202 if (sec == text_section)
26203 table = text_section_line_info;
26204 else if (sec == cold_text_section)
26206 table = cold_text_section_line_info;
26207 if (!table)
26209 cold_text_section_line_info = table = new_line_info_table ();
26210 table->end_label = cold_end_label;
26213 else
26215 const char *end_label;
26217 if (flag_reorder_blocks_and_partition)
26219 if (in_cold_section_p)
26220 end_label = crtl->subsections.cold_section_end_label;
26221 else
26222 end_label = crtl->subsections.hot_section_end_label;
26224 else
26226 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26227 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26228 current_function_funcdef_no);
26229 end_label = ggc_strdup (label);
26232 table = new_line_info_table ();
26233 table->end_label = end_label;
26235 vec_safe_push (separate_line_info, table);
26238 if (DWARF2_ASM_LINE_DEBUG_INFO)
26239 table->is_stmt = (cur_line_info_table
26240 ? cur_line_info_table->is_stmt
26241 : DWARF_LINE_DEFAULT_IS_STMT_START);
26242 cur_line_info_table = table;
26246 /* We need to reset the locations at the beginning of each
26247 function. We can't do this in the end_function hook, because the
26248 declarations that use the locations won't have been output when
26249 that hook is called. Also compute have_multiple_function_sections here. */
26251 static void
26252 dwarf2out_begin_function (tree fun)
26254 section *sec = function_section (fun);
26256 if (sec != text_section)
26257 have_multiple_function_sections = true;
26259 if (flag_reorder_blocks_and_partition && !cold_text_section)
26261 gcc_assert (current_function_decl == fun);
26262 cold_text_section = unlikely_text_section ();
26263 switch_to_section (cold_text_section);
26264 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26265 switch_to_section (sec);
26268 dwarf2out_note_section_used ();
26269 call_site_count = 0;
26270 tail_call_site_count = 0;
26272 set_cur_line_info_table (sec);
26275 /* Helper function of dwarf2out_end_function, called only after emitting
26276 the very first function into assembly. Check if some .debug_loc range
26277 might end with a .LVL* label that could be equal to .Ltext0.
26278 In that case we must force using absolute addresses in .debug_loc ranges,
26279 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26280 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26281 list terminator.
26282 Set have_multiple_function_sections to true in that case and
26283 terminate htab traversal. */
26286 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26288 var_loc_list *entry = *slot;
26289 struct var_loc_node *node;
26291 node = entry->first;
26292 if (node && node->next && node->next->label)
26294 unsigned int i;
26295 const char *label = node->next->label;
26296 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26298 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26300 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26301 if (strcmp (label, loclabel) == 0)
26303 have_multiple_function_sections = true;
26304 return 0;
26308 return 1;
26311 /* Hook called after emitting a function into assembly.
26312 This does something only for the very first function emitted. */
26314 static void
26315 dwarf2out_end_function (unsigned int)
26317 if (in_first_function_p
26318 && !have_multiple_function_sections
26319 && first_loclabel_num_not_at_text_label
26320 && decl_loc_table)
26321 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26322 in_first_function_p = false;
26323 maybe_at_text_label_p = false;
26326 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26327 front-ends register a translation unit even before dwarf2out_init is
26328 called. */
26329 static tree main_translation_unit = NULL_TREE;
26331 /* Hook called by front-ends after they built their main translation unit.
26332 Associate comp_unit_die to UNIT. */
26334 static void
26335 dwarf2out_register_main_translation_unit (tree unit)
26337 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26338 && main_translation_unit == NULL_TREE);
26339 main_translation_unit = unit;
26340 /* If dwarf2out_init has not been called yet, it will perform the association
26341 itself looking at main_translation_unit. */
26342 if (decl_die_table != NULL)
26343 equate_decl_number_to_die (unit, comp_unit_die ());
26346 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26348 static void
26349 push_dw_line_info_entry (dw_line_info_table *table,
26350 enum dw_line_info_opcode opcode, unsigned int val)
26352 dw_line_info_entry e;
26353 e.opcode = opcode;
26354 e.val = val;
26355 vec_safe_push (table->entries, e);
26358 /* Output a label to mark the beginning of a source code line entry
26359 and record information relating to this source line, in
26360 'line_info_table' for later output of the .debug_line section. */
26361 /* ??? The discriminator parameter ought to be unsigned. */
26363 static void
26364 dwarf2out_source_line (unsigned int line, const char *filename,
26365 int discriminator, bool is_stmt)
26367 unsigned int file_num;
26368 dw_line_info_table *table;
26370 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26371 return;
26373 /* The discriminator column was added in dwarf4. Simplify the below
26374 by simply removing it if we're not supposed to output it. */
26375 if (dwarf_version < 4 && dwarf_strict)
26376 discriminator = 0;
26378 table = cur_line_info_table;
26379 file_num = maybe_emit_file (lookup_filename (filename));
26381 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26382 the debugger has used the second (possibly duplicate) line number
26383 at the beginning of the function to mark the end of the prologue.
26384 We could eliminate any other duplicates within the function. For
26385 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26386 that second line number entry. */
26387 /* Recall that this end-of-prologue indication is *not* the same thing
26388 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26389 to which the hook corresponds, follows the last insn that was
26390 emitted by gen_prologue. What we need is to precede the first insn
26391 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26392 insn that corresponds to something the user wrote. These may be
26393 very different locations once scheduling is enabled. */
26395 if (0 && file_num == table->file_num
26396 && line == table->line_num
26397 && discriminator == table->discrim_num
26398 && is_stmt == table->is_stmt)
26399 return;
26401 switch_to_section (current_function_section ());
26403 /* If requested, emit something human-readable. */
26404 if (flag_debug_asm)
26405 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
26407 if (DWARF2_ASM_LINE_DEBUG_INFO)
26409 /* Emit the .loc directive understood by GNU as. */
26410 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26411 file_num, line, is_stmt, discriminator */
26412 fputs ("\t.loc ", asm_out_file);
26413 fprint_ul (asm_out_file, file_num);
26414 putc (' ', asm_out_file);
26415 fprint_ul (asm_out_file, line);
26416 putc (' ', asm_out_file);
26417 putc ('0', asm_out_file);
26419 if (is_stmt != table->is_stmt)
26421 fputs (" is_stmt ", asm_out_file);
26422 putc (is_stmt ? '1' : '0', asm_out_file);
26424 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26426 gcc_assert (discriminator > 0);
26427 fputs (" discriminator ", asm_out_file);
26428 fprint_ul (asm_out_file, (unsigned long) discriminator);
26430 putc ('\n', asm_out_file);
26432 else
26434 unsigned int label_num = ++line_info_label_num;
26436 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26438 push_dw_line_info_entry (table, LI_set_address, label_num);
26439 if (file_num != table->file_num)
26440 push_dw_line_info_entry (table, LI_set_file, file_num);
26441 if (discriminator != table->discrim_num)
26442 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26443 if (is_stmt != table->is_stmt)
26444 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26445 push_dw_line_info_entry (table, LI_set_line, line);
26448 table->file_num = file_num;
26449 table->line_num = line;
26450 table->discrim_num = discriminator;
26451 table->is_stmt = is_stmt;
26452 table->in_use = true;
26455 /* Record the beginning of a new source file. */
26457 static void
26458 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26460 if (flag_eliminate_dwarf2_dups)
26462 /* Record the beginning of the file for break_out_includes. */
26463 dw_die_ref bincl_die;
26465 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26466 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26469 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26471 macinfo_entry e;
26472 e.code = DW_MACINFO_start_file;
26473 e.lineno = lineno;
26474 e.info = ggc_strdup (filename);
26475 vec_safe_push (macinfo_table, e);
26479 /* Record the end of a source file. */
26481 static void
26482 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26484 if (flag_eliminate_dwarf2_dups)
26485 /* Record the end of the file for break_out_includes. */
26486 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26488 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26490 macinfo_entry e;
26491 e.code = DW_MACINFO_end_file;
26492 e.lineno = lineno;
26493 e.info = NULL;
26494 vec_safe_push (macinfo_table, e);
26498 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26499 the tail part of the directive line, i.e. the part which is past the
26500 initial whitespace, #, whitespace, directive-name, whitespace part. */
26502 static void
26503 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26504 const char *buffer ATTRIBUTE_UNUSED)
26506 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26508 macinfo_entry e;
26509 /* Insert a dummy first entry to be able to optimize the whole
26510 predefined macro block using DW_MACRO_import. */
26511 if (macinfo_table->is_empty () && lineno <= 1)
26513 e.code = 0;
26514 e.lineno = 0;
26515 e.info = NULL;
26516 vec_safe_push (macinfo_table, e);
26518 e.code = DW_MACINFO_define;
26519 e.lineno = lineno;
26520 e.info = ggc_strdup (buffer);
26521 vec_safe_push (macinfo_table, e);
26525 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26526 the tail part of the directive line, i.e. the part which is past the
26527 initial whitespace, #, whitespace, directive-name, whitespace part. */
26529 static void
26530 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26531 const char *buffer ATTRIBUTE_UNUSED)
26533 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26535 macinfo_entry e;
26536 /* Insert a dummy first entry to be able to optimize the whole
26537 predefined macro block using DW_MACRO_import. */
26538 if (macinfo_table->is_empty () && lineno <= 1)
26540 e.code = 0;
26541 e.lineno = 0;
26542 e.info = NULL;
26543 vec_safe_push (macinfo_table, e);
26545 e.code = DW_MACINFO_undef;
26546 e.lineno = lineno;
26547 e.info = ggc_strdup (buffer);
26548 vec_safe_push (macinfo_table, e);
26552 /* Helpers to manipulate hash table of CUs. */
26554 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26556 static inline hashval_t hash (const macinfo_entry *);
26557 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26560 inline hashval_t
26561 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26563 return htab_hash_string (entry->info);
26566 inline bool
26567 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26568 const macinfo_entry *entry2)
26570 return !strcmp (entry1->info, entry2->info);
26573 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26575 /* Output a single .debug_macinfo entry. */
26577 static void
26578 output_macinfo_op (macinfo_entry *ref)
26580 int file_num;
26581 size_t len;
26582 struct indirect_string_node *node;
26583 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26584 struct dwarf_file_data *fd;
26586 switch (ref->code)
26588 case DW_MACINFO_start_file:
26589 fd = lookup_filename (ref->info);
26590 file_num = maybe_emit_file (fd);
26591 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26592 dw2_asm_output_data_uleb128 (ref->lineno,
26593 "Included from line number %lu",
26594 (unsigned long) ref->lineno);
26595 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26596 break;
26597 case DW_MACINFO_end_file:
26598 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26599 break;
26600 case DW_MACINFO_define:
26601 case DW_MACINFO_undef:
26602 len = strlen (ref->info) + 1;
26603 if (!dwarf_strict
26604 && len > DWARF_OFFSET_SIZE
26605 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26606 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26608 ref->code = ref->code == DW_MACINFO_define
26609 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26610 output_macinfo_op (ref);
26611 return;
26613 dw2_asm_output_data (1, ref->code,
26614 ref->code == DW_MACINFO_define
26615 ? "Define macro" : "Undefine macro");
26616 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26617 (unsigned long) ref->lineno);
26618 dw2_asm_output_nstring (ref->info, -1, "The macro");
26619 break;
26620 case DW_MACRO_define_strp:
26621 case DW_MACRO_undef_strp:
26622 node = find_AT_string (ref->info);
26623 gcc_assert (node
26624 && (node->form == DW_FORM_strp
26625 || node->form == DW_FORM_GNU_str_index));
26626 dw2_asm_output_data (1, ref->code,
26627 ref->code == DW_MACRO_define_strp
26628 ? "Define macro strp"
26629 : "Undefine macro strp");
26630 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26631 (unsigned long) ref->lineno);
26632 if (node->form == DW_FORM_strp)
26633 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26634 debug_str_section, "The macro: \"%s\"",
26635 ref->info);
26636 else
26637 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26638 ref->info);
26639 break;
26640 case DW_MACRO_import:
26641 dw2_asm_output_data (1, ref->code, "Import");
26642 ASM_GENERATE_INTERNAL_LABEL (label,
26643 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26644 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26645 break;
26646 default:
26647 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26648 ASM_COMMENT_START, (unsigned long) ref->code);
26649 break;
26653 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26654 other compilation unit .debug_macinfo sections. IDX is the first
26655 index of a define/undef, return the number of ops that should be
26656 emitted in a comdat .debug_macinfo section and emit
26657 a DW_MACRO_import entry referencing it.
26658 If the define/undef entry should be emitted normally, return 0. */
26660 static unsigned
26661 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26662 macinfo_hash_type **macinfo_htab)
26664 macinfo_entry *first, *second, *cur, *inc;
26665 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26666 unsigned char checksum[16];
26667 struct md5_ctx ctx;
26668 char *grp_name, *tail;
26669 const char *base;
26670 unsigned int i, count, encoded_filename_len, linebuf_len;
26671 macinfo_entry **slot;
26673 first = &(*macinfo_table)[idx];
26674 second = &(*macinfo_table)[idx + 1];
26676 /* Optimize only if there are at least two consecutive define/undef ops,
26677 and either all of them are before first DW_MACINFO_start_file
26678 with lineno {0,1} (i.e. predefined macro block), or all of them are
26679 in some included header file. */
26680 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26681 return 0;
26682 if (vec_safe_is_empty (files))
26684 if (first->lineno > 1 || second->lineno > 1)
26685 return 0;
26687 else if (first->lineno == 0)
26688 return 0;
26690 /* Find the last define/undef entry that can be grouped together
26691 with first and at the same time compute md5 checksum of their
26692 codes, linenumbers and strings. */
26693 md5_init_ctx (&ctx);
26694 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26695 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26696 break;
26697 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26698 break;
26699 else
26701 unsigned char code = cur->code;
26702 md5_process_bytes (&code, 1, &ctx);
26703 checksum_uleb128 (cur->lineno, &ctx);
26704 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26706 md5_finish_ctx (&ctx, checksum);
26707 count = i - idx;
26709 /* From the containing include filename (if any) pick up just
26710 usable characters from its basename. */
26711 if (vec_safe_is_empty (files))
26712 base = "";
26713 else
26714 base = lbasename (files->last ().info);
26715 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26716 if (ISIDNUM (base[i]) || base[i] == '.')
26717 encoded_filename_len++;
26718 /* Count . at the end. */
26719 if (encoded_filename_len)
26720 encoded_filename_len++;
26722 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26723 linebuf_len = strlen (linebuf);
26725 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26726 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26727 + 16 * 2 + 1);
26728 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26729 tail = grp_name + 4;
26730 if (encoded_filename_len)
26732 for (i = 0; base[i]; i++)
26733 if (ISIDNUM (base[i]) || base[i] == '.')
26734 *tail++ = base[i];
26735 *tail++ = '.';
26737 memcpy (tail, linebuf, linebuf_len);
26738 tail += linebuf_len;
26739 *tail++ = '.';
26740 for (i = 0; i < 16; i++)
26741 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26743 /* Construct a macinfo_entry for DW_MACRO_import
26744 in the empty vector entry before the first define/undef. */
26745 inc = &(*macinfo_table)[idx - 1];
26746 inc->code = DW_MACRO_import;
26747 inc->lineno = 0;
26748 inc->info = ggc_strdup (grp_name);
26749 if (!*macinfo_htab)
26750 *macinfo_htab = new macinfo_hash_type (10);
26751 /* Avoid emitting duplicates. */
26752 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26753 if (*slot != NULL)
26755 inc->code = 0;
26756 inc->info = NULL;
26757 /* If such an entry has been used before, just emit
26758 a DW_MACRO_import op. */
26759 inc = *slot;
26760 output_macinfo_op (inc);
26761 /* And clear all macinfo_entry in the range to avoid emitting them
26762 in the second pass. */
26763 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26765 cur->code = 0;
26766 cur->info = NULL;
26769 else
26771 *slot = inc;
26772 inc->lineno = (*macinfo_htab)->elements ();
26773 output_macinfo_op (inc);
26775 return count;
26778 /* Save any strings needed by the macinfo table in the debug str
26779 table. All strings must be collected into the table by the time
26780 index_string is called. */
26782 static void
26783 save_macinfo_strings (void)
26785 unsigned len;
26786 unsigned i;
26787 macinfo_entry *ref;
26789 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26791 switch (ref->code)
26793 /* Match the logic in output_macinfo_op to decide on
26794 indirect strings. */
26795 case DW_MACINFO_define:
26796 case DW_MACINFO_undef:
26797 len = strlen (ref->info) + 1;
26798 if (!dwarf_strict
26799 && len > DWARF_OFFSET_SIZE
26800 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26801 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26802 set_indirect_string (find_AT_string (ref->info));
26803 break;
26804 case DW_MACRO_define_strp:
26805 case DW_MACRO_undef_strp:
26806 set_indirect_string (find_AT_string (ref->info));
26807 break;
26808 default:
26809 break;
26814 /* Output macinfo section(s). */
26816 static void
26817 output_macinfo (void)
26819 unsigned i;
26820 unsigned long length = vec_safe_length (macinfo_table);
26821 macinfo_entry *ref;
26822 vec<macinfo_entry, va_gc> *files = NULL;
26823 macinfo_hash_type *macinfo_htab = NULL;
26825 if (! length)
26826 return;
26828 /* output_macinfo* uses these interchangeably. */
26829 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
26830 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
26831 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
26832 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
26834 /* For .debug_macro emit the section header. */
26835 if (!dwarf_strict || dwarf_version >= 5)
26837 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26838 "DWARF macro version number");
26839 if (DWARF_OFFSET_SIZE == 8)
26840 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
26841 else
26842 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
26843 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
26844 (!dwarf_split_debug_info ? debug_line_section_label
26845 : debug_skeleton_line_section_label),
26846 debug_line_section, NULL);
26849 /* In the first loop, it emits the primary .debug_macinfo section
26850 and after each emitted op the macinfo_entry is cleared.
26851 If a longer range of define/undef ops can be optimized using
26852 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
26853 the vector before the first define/undef in the range and the
26854 whole range of define/undef ops is not emitted and kept. */
26855 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26857 switch (ref->code)
26859 case DW_MACINFO_start_file:
26860 vec_safe_push (files, *ref);
26861 break;
26862 case DW_MACINFO_end_file:
26863 if (!vec_safe_is_empty (files))
26864 files->pop ();
26865 break;
26866 case DW_MACINFO_define:
26867 case DW_MACINFO_undef:
26868 if ((!dwarf_strict || dwarf_version >= 5)
26869 && HAVE_COMDAT_GROUP
26870 && vec_safe_length (files) != 1
26871 && i > 0
26872 && i + 1 < length
26873 && (*macinfo_table)[i - 1].code == 0)
26875 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
26876 if (count)
26878 i += count - 1;
26879 continue;
26882 break;
26883 case 0:
26884 /* A dummy entry may be inserted at the beginning to be able
26885 to optimize the whole block of predefined macros. */
26886 if (i == 0)
26887 continue;
26888 default:
26889 break;
26891 output_macinfo_op (ref);
26892 ref->info = NULL;
26893 ref->code = 0;
26896 if (!macinfo_htab)
26897 return;
26899 delete macinfo_htab;
26900 macinfo_htab = NULL;
26902 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
26903 terminate the current chain and switch to a new comdat .debug_macinfo
26904 section and emit the define/undef entries within it. */
26905 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26906 switch (ref->code)
26908 case 0:
26909 continue;
26910 case DW_MACRO_import:
26912 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26913 tree comdat_key = get_identifier (ref->info);
26914 /* Terminate the previous .debug_macinfo section. */
26915 dw2_asm_output_data (1, 0, "End compilation unit");
26916 targetm.asm_out.named_section (debug_macinfo_section_name,
26917 SECTION_DEBUG
26918 | SECTION_LINKONCE,
26919 comdat_key);
26920 ASM_GENERATE_INTERNAL_LABEL (label,
26921 DEBUG_MACRO_SECTION_LABEL,
26922 ref->lineno);
26923 ASM_OUTPUT_LABEL (asm_out_file, label);
26924 ref->code = 0;
26925 ref->info = NULL;
26926 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26927 "DWARF macro version number");
26928 if (DWARF_OFFSET_SIZE == 8)
26929 dw2_asm_output_data (1, 1, "Flags: 64-bit");
26930 else
26931 dw2_asm_output_data (1, 0, "Flags: 32-bit");
26933 break;
26934 case DW_MACINFO_define:
26935 case DW_MACINFO_undef:
26936 output_macinfo_op (ref);
26937 ref->code = 0;
26938 ref->info = NULL;
26939 break;
26940 default:
26941 gcc_unreachable ();
26945 /* Initialize the various sections and labels for dwarf output. */
26947 static void
26948 init_sections_and_labels (void)
26950 if (!dwarf_split_debug_info)
26952 debug_info_section = get_section (DEBUG_INFO_SECTION,
26953 SECTION_DEBUG, NULL);
26954 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
26955 SECTION_DEBUG, NULL);
26956 debug_loc_section = get_section (dwarf_version >= 5
26957 ? DEBUG_LOCLISTS_SECTION
26958 : DEBUG_LOC_SECTION,
26959 SECTION_DEBUG, NULL);
26960 debug_macinfo_section_name
26961 = (dwarf_strict && dwarf_version < 5)
26962 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
26963 debug_macinfo_section = get_section (debug_macinfo_section_name,
26964 SECTION_DEBUG, NULL);
26966 else
26968 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
26969 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
26970 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
26971 SECTION_DEBUG | SECTION_EXCLUDE,
26972 NULL);
26973 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
26974 SECTION_DEBUG, NULL);
26975 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
26976 SECTION_DEBUG, NULL);
26977 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
26978 SECTION_DEBUG, NULL);
26979 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
26980 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
26982 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
26983 the main .o, but the skeleton_line goes into the split off dwo. */
26984 debug_skeleton_line_section
26985 = get_section (DEBUG_DWO_LINE_SECTION,
26986 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
26987 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
26988 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
26989 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
26990 SECTION_DEBUG | SECTION_EXCLUDE,
26991 NULL);
26992 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
26993 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
26994 debug_loc_section = get_section (dwarf_version >= 5
26995 ? DEBUG_DWO_LOCLISTS_SECTION
26996 : DEBUG_DWO_LOC_SECTION,
26997 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
26998 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
26999 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27000 debug_macinfo_section_name
27001 = (dwarf_strict && dwarf_version < 5)
27002 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27003 debug_macinfo_section = get_section (debug_macinfo_section_name,
27004 SECTION_DEBUG | SECTION_EXCLUDE,
27005 NULL);
27007 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27008 SECTION_DEBUG, NULL);
27009 debug_line_section = get_section (DEBUG_LINE_SECTION,
27010 SECTION_DEBUG, NULL);
27011 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27012 SECTION_DEBUG, NULL);
27013 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27014 SECTION_DEBUG, NULL);
27015 debug_str_section = get_section (DEBUG_STR_SECTION,
27016 DEBUG_STR_SECTION_FLAGS, NULL);
27017 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27018 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27019 DEBUG_STR_SECTION_FLAGS, NULL);
27021 debug_ranges_section = get_section (dwarf_version >= 5
27022 ? DEBUG_RNGLISTS_SECTION
27023 : DEBUG_RANGES_SECTION,
27024 SECTION_DEBUG, NULL);
27025 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27026 SECTION_DEBUG, NULL);
27028 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27029 DEBUG_ABBREV_SECTION_LABEL, 0);
27030 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27031 DEBUG_INFO_SECTION_LABEL, 0);
27032 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27033 DEBUG_LINE_SECTION_LABEL, 0);
27034 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27035 DEBUG_RANGES_SECTION_LABEL, 0);
27036 if (dwarf_version >= 5 && dwarf_split_debug_info)
27037 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27038 DEBUG_RANGES_SECTION_LABEL, 1);
27039 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27040 DEBUG_ADDR_SECTION_LABEL, 0);
27041 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27042 (dwarf_strict && dwarf_version < 5)
27043 ? DEBUG_MACINFO_SECTION_LABEL
27044 : DEBUG_MACRO_SECTION_LABEL, 0);
27045 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27048 /* Set up for Dwarf output at the start of compilation. */
27050 static void
27051 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27053 /* This option is currently broken, see (PR53118 and PR46102). */
27054 if (flag_eliminate_dwarf2_dups
27055 && strstr (lang_hooks.name, "C++"))
27057 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27058 flag_eliminate_dwarf2_dups = 0;
27061 /* Allocate the file_table. */
27062 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27064 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27065 /* Allocate the decl_die_table. */
27066 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27068 /* Allocate the decl_loc_table. */
27069 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27071 /* Allocate the cached_dw_loc_list_table. */
27072 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27074 /* Allocate the initial hunk of the decl_scope_table. */
27075 vec_alloc (decl_scope_table, 256);
27077 /* Allocate the initial hunk of the abbrev_die_table. */
27078 vec_alloc (abbrev_die_table, 256);
27079 /* Zero-th entry is allocated, but unused. */
27080 abbrev_die_table->quick_push (NULL);
27082 /* Allocate the dwarf_proc_stack_usage_map. */
27083 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27085 /* Allocate the pubtypes and pubnames vectors. */
27086 vec_alloc (pubname_table, 32);
27087 vec_alloc (pubtype_table, 32);
27089 vec_alloc (incomplete_types, 64);
27091 vec_alloc (used_rtx_array, 32);
27093 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27094 vec_alloc (macinfo_table, 64);
27095 #endif
27097 /* If front-ends already registered a main translation unit but we were not
27098 ready to perform the association, do this now. */
27099 if (main_translation_unit != NULL_TREE)
27100 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27103 /* Called before compile () starts outputtting functions, variables
27104 and toplevel asms into assembly. */
27106 static void
27107 dwarf2out_assembly_start (void)
27109 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27110 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27111 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27112 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27113 COLD_TEXT_SECTION_LABEL, 0);
27114 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27116 switch_to_section (text_section);
27117 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27118 #endif
27120 /* Make sure the line number table for .text always exists. */
27121 text_section_line_info = new_line_info_table ();
27122 text_section_line_info->end_label = text_end_label;
27124 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27125 cur_line_info_table = text_section_line_info;
27126 #endif
27128 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27129 && dwarf2out_do_cfi_asm ()
27130 && (!(flag_unwind_tables || flag_exceptions)
27131 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27132 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27135 /* A helper function for dwarf2out_finish called through
27136 htab_traverse. Assign a string its index. All strings must be
27137 collected into the table by the time index_string is called,
27138 because the indexing code relies on htab_traverse to traverse nodes
27139 in the same order for each run. */
27142 index_string (indirect_string_node **h, unsigned int *index)
27144 indirect_string_node *node = *h;
27146 find_string_form (node);
27147 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27149 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27150 node->index = *index;
27151 *index += 1;
27153 return 1;
27156 /* A helper function for output_indirect_strings called through
27157 htab_traverse. Output the offset to a string and update the
27158 current offset. */
27161 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27163 indirect_string_node *node = *h;
27165 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27167 /* Assert that this node has been assigned an index. */
27168 gcc_assert (node->index != NO_INDEX_ASSIGNED
27169 && node->index != NOT_INDEXED);
27170 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27171 "indexed string 0x%x: %s", node->index, node->str);
27172 *offset += strlen (node->str) + 1;
27174 return 1;
27177 /* A helper function for dwarf2out_finish called through
27178 htab_traverse. Output the indexed string. */
27181 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27183 struct indirect_string_node *node = *h;
27185 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27187 /* Assert that the strings are output in the same order as their
27188 indexes were assigned. */
27189 gcc_assert (*cur_idx == node->index);
27190 assemble_string (node->str, strlen (node->str) + 1);
27191 *cur_idx += 1;
27193 return 1;
27196 /* A helper function for dwarf2out_finish called through
27197 htab_traverse. Emit one queued .debug_str string. */
27200 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27202 struct indirect_string_node *node = *h;
27204 node->form = find_string_form (node);
27205 if (node->form == form && node->refcount > 0)
27207 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27208 assemble_string (node->str, strlen (node->str) + 1);
27211 return 1;
27214 /* Output the indexed string table. */
27216 static void
27217 output_indirect_strings (void)
27219 switch_to_section (debug_str_section);
27220 if (!dwarf_split_debug_info)
27221 debug_str_hash->traverse<enum dwarf_form,
27222 output_indirect_string> (DW_FORM_strp);
27223 else
27225 unsigned int offset = 0;
27226 unsigned int cur_idx = 0;
27228 skeleton_debug_str_hash->traverse<enum dwarf_form,
27229 output_indirect_string> (DW_FORM_strp);
27231 switch_to_section (debug_str_offsets_section);
27232 debug_str_hash->traverse_noresize
27233 <unsigned int *, output_index_string_offset> (&offset);
27234 switch_to_section (debug_str_dwo_section);
27235 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27236 (&cur_idx);
27240 /* Callback for htab_traverse to assign an index to an entry in the
27241 table, and to write that entry to the .debug_addr section. */
27244 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27246 addr_table_entry *entry = *slot;
27248 if (entry->refcount == 0)
27250 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27251 || entry->index == NOT_INDEXED);
27252 return 1;
27255 gcc_assert (entry->index == *cur_index);
27256 (*cur_index)++;
27258 switch (entry->kind)
27260 case ate_kind_rtx:
27261 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27262 "0x%x", entry->index);
27263 break;
27264 case ate_kind_rtx_dtprel:
27265 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27266 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27267 DWARF2_ADDR_SIZE,
27268 entry->addr.rtl);
27269 fputc ('\n', asm_out_file);
27270 break;
27271 case ate_kind_label:
27272 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27273 "0x%x", entry->index);
27274 break;
27275 default:
27276 gcc_unreachable ();
27278 return 1;
27281 /* Produce the .debug_addr section. */
27283 static void
27284 output_addr_table (void)
27286 unsigned int index = 0;
27287 if (addr_index_table == NULL || addr_index_table->size () == 0)
27288 return;
27290 switch_to_section (debug_addr_section);
27291 addr_index_table
27292 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27295 #if ENABLE_ASSERT_CHECKING
27296 /* Verify that all marks are clear. */
27298 static void
27299 verify_marks_clear (dw_die_ref die)
27301 dw_die_ref c;
27303 gcc_assert (! die->die_mark);
27304 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27306 #endif /* ENABLE_ASSERT_CHECKING */
27308 /* Clear the marks for a die and its children.
27309 Be cool if the mark isn't set. */
27311 static void
27312 prune_unmark_dies (dw_die_ref die)
27314 dw_die_ref c;
27316 if (die->die_mark)
27317 die->die_mark = 0;
27318 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27321 /* Given LOC that is referenced by a DIE we're marking as used, find all
27322 referenced DWARF procedures it references and mark them as used. */
27324 static void
27325 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27327 for (; loc != NULL; loc = loc->dw_loc_next)
27328 switch (loc->dw_loc_opc)
27330 case DW_OP_implicit_pointer:
27331 case DW_OP_convert:
27332 case DW_OP_reinterpret:
27333 case DW_OP_GNU_implicit_pointer:
27334 case DW_OP_GNU_convert:
27335 case DW_OP_GNU_reinterpret:
27336 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27337 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27338 break;
27339 case DW_OP_call2:
27340 case DW_OP_call4:
27341 case DW_OP_call_ref:
27342 case DW_OP_const_type:
27343 case DW_OP_GNU_const_type:
27344 case DW_OP_GNU_parameter_ref:
27345 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27346 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27347 break;
27348 case DW_OP_regval_type:
27349 case DW_OP_deref_type:
27350 case DW_OP_GNU_regval_type:
27351 case DW_OP_GNU_deref_type:
27352 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27353 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27354 break;
27355 case DW_OP_entry_value:
27356 case DW_OP_GNU_entry_value:
27357 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27358 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27359 break;
27360 default:
27361 break;
27365 /* Given DIE that we're marking as used, find any other dies
27366 it references as attributes and mark them as used. */
27368 static void
27369 prune_unused_types_walk_attribs (dw_die_ref die)
27371 dw_attr_node *a;
27372 unsigned ix;
27374 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27376 switch (AT_class (a))
27378 /* Make sure DWARF procedures referenced by location descriptions will
27379 get emitted. */
27380 case dw_val_class_loc:
27381 prune_unused_types_walk_loc_descr (AT_loc (a));
27382 break;
27383 case dw_val_class_loc_list:
27384 for (dw_loc_list_ref list = AT_loc_list (a);
27385 list != NULL;
27386 list = list->dw_loc_next)
27387 prune_unused_types_walk_loc_descr (list->expr);
27388 break;
27390 case dw_val_class_die_ref:
27391 /* A reference to another DIE.
27392 Make sure that it will get emitted.
27393 If it was broken out into a comdat group, don't follow it. */
27394 if (! AT_ref (a)->comdat_type_p
27395 || a->dw_attr == DW_AT_specification)
27396 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27397 break;
27399 case dw_val_class_str:
27400 /* Set the string's refcount to 0 so that prune_unused_types_mark
27401 accounts properly for it. */
27402 a->dw_attr_val.v.val_str->refcount = 0;
27403 break;
27405 default:
27406 break;
27411 /* Mark the generic parameters and arguments children DIEs of DIE. */
27413 static void
27414 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27416 dw_die_ref c;
27418 if (die == NULL || die->die_child == NULL)
27419 return;
27420 c = die->die_child;
27423 if (is_template_parameter (c))
27424 prune_unused_types_mark (c, 1);
27425 c = c->die_sib;
27426 } while (c && c != die->die_child);
27429 /* Mark DIE as being used. If DOKIDS is true, then walk down
27430 to DIE's children. */
27432 static void
27433 prune_unused_types_mark (dw_die_ref die, int dokids)
27435 dw_die_ref c;
27437 if (die->die_mark == 0)
27439 /* We haven't done this node yet. Mark it as used. */
27440 die->die_mark = 1;
27441 /* If this is the DIE of a generic type instantiation,
27442 mark the children DIEs that describe its generic parms and
27443 args. */
27444 prune_unused_types_mark_generic_parms_dies (die);
27446 /* We also have to mark its parents as used.
27447 (But we don't want to mark our parent's kids due to this,
27448 unless it is a class.) */
27449 if (die->die_parent)
27450 prune_unused_types_mark (die->die_parent,
27451 class_scope_p (die->die_parent));
27453 /* Mark any referenced nodes. */
27454 prune_unused_types_walk_attribs (die);
27456 /* If this node is a specification,
27457 also mark the definition, if it exists. */
27458 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27459 prune_unused_types_mark (die->die_definition, 1);
27462 if (dokids && die->die_mark != 2)
27464 /* We need to walk the children, but haven't done so yet.
27465 Remember that we've walked the kids. */
27466 die->die_mark = 2;
27468 /* If this is an array type, we need to make sure our
27469 kids get marked, even if they're types. If we're
27470 breaking out types into comdat sections, do this
27471 for all type definitions. */
27472 if (die->die_tag == DW_TAG_array_type
27473 || (use_debug_types
27474 && is_type_die (die) && ! is_declaration_die (die)))
27475 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27476 else
27477 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27481 /* For local classes, look if any static member functions were emitted
27482 and if so, mark them. */
27484 static void
27485 prune_unused_types_walk_local_classes (dw_die_ref die)
27487 dw_die_ref c;
27489 if (die->die_mark == 2)
27490 return;
27492 switch (die->die_tag)
27494 case DW_TAG_structure_type:
27495 case DW_TAG_union_type:
27496 case DW_TAG_class_type:
27497 break;
27499 case DW_TAG_subprogram:
27500 if (!get_AT_flag (die, DW_AT_declaration)
27501 || die->die_definition != NULL)
27502 prune_unused_types_mark (die, 1);
27503 return;
27505 default:
27506 return;
27509 /* Mark children. */
27510 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27513 /* Walk the tree DIE and mark types that we actually use. */
27515 static void
27516 prune_unused_types_walk (dw_die_ref die)
27518 dw_die_ref c;
27520 /* Don't do anything if this node is already marked and
27521 children have been marked as well. */
27522 if (die->die_mark == 2)
27523 return;
27525 switch (die->die_tag)
27527 case DW_TAG_structure_type:
27528 case DW_TAG_union_type:
27529 case DW_TAG_class_type:
27530 if (die->die_perennial_p)
27531 break;
27533 for (c = die->die_parent; c; c = c->die_parent)
27534 if (c->die_tag == DW_TAG_subprogram)
27535 break;
27537 /* Finding used static member functions inside of classes
27538 is needed just for local classes, because for other classes
27539 static member function DIEs with DW_AT_specification
27540 are emitted outside of the DW_TAG_*_type. If we ever change
27541 it, we'd need to call this even for non-local classes. */
27542 if (c)
27543 prune_unused_types_walk_local_classes (die);
27545 /* It's a type node --- don't mark it. */
27546 return;
27548 case DW_TAG_const_type:
27549 case DW_TAG_packed_type:
27550 case DW_TAG_pointer_type:
27551 case DW_TAG_reference_type:
27552 case DW_TAG_rvalue_reference_type:
27553 case DW_TAG_volatile_type:
27554 case DW_TAG_typedef:
27555 case DW_TAG_array_type:
27556 case DW_TAG_interface_type:
27557 case DW_TAG_friend:
27558 case DW_TAG_enumeration_type:
27559 case DW_TAG_subroutine_type:
27560 case DW_TAG_string_type:
27561 case DW_TAG_set_type:
27562 case DW_TAG_subrange_type:
27563 case DW_TAG_ptr_to_member_type:
27564 case DW_TAG_file_type:
27565 /* Type nodes are useful only when other DIEs reference them --- don't
27566 mark them. */
27567 /* FALLTHROUGH */
27569 case DW_TAG_dwarf_procedure:
27570 /* Likewise for DWARF procedures. */
27572 if (die->die_perennial_p)
27573 break;
27575 return;
27577 default:
27578 /* Mark everything else. */
27579 break;
27582 if (die->die_mark == 0)
27584 die->die_mark = 1;
27586 /* Now, mark any dies referenced from here. */
27587 prune_unused_types_walk_attribs (die);
27590 die->die_mark = 2;
27592 /* Mark children. */
27593 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27596 /* Increment the string counts on strings referred to from DIE's
27597 attributes. */
27599 static void
27600 prune_unused_types_update_strings (dw_die_ref die)
27602 dw_attr_node *a;
27603 unsigned ix;
27605 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27606 if (AT_class (a) == dw_val_class_str)
27608 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27609 s->refcount++;
27610 /* Avoid unnecessarily putting strings that are used less than
27611 twice in the hash table. */
27612 if (s->refcount
27613 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27615 indirect_string_node **slot
27616 = debug_str_hash->find_slot_with_hash (s->str,
27617 htab_hash_string (s->str),
27618 INSERT);
27619 gcc_assert (*slot == NULL);
27620 *slot = s;
27625 /* Mark DIE and its children as removed. */
27627 static void
27628 mark_removed (dw_die_ref die)
27630 dw_die_ref c;
27631 die->removed = true;
27632 FOR_EACH_CHILD (die, c, mark_removed (c));
27635 /* Remove from the tree DIE any dies that aren't marked. */
27637 static void
27638 prune_unused_types_prune (dw_die_ref die)
27640 dw_die_ref c;
27642 gcc_assert (die->die_mark);
27643 prune_unused_types_update_strings (die);
27645 if (! die->die_child)
27646 return;
27648 c = die->die_child;
27649 do {
27650 dw_die_ref prev = c, next;
27651 for (c = c->die_sib; ! c->die_mark; c = next)
27652 if (c == die->die_child)
27654 /* No marked children between 'prev' and the end of the list. */
27655 if (prev == c)
27656 /* No marked children at all. */
27657 die->die_child = NULL;
27658 else
27660 prev->die_sib = c->die_sib;
27661 die->die_child = prev;
27663 c->die_sib = NULL;
27664 mark_removed (c);
27665 return;
27667 else
27669 next = c->die_sib;
27670 c->die_sib = NULL;
27671 mark_removed (c);
27674 if (c != prev->die_sib)
27675 prev->die_sib = c;
27676 prune_unused_types_prune (c);
27677 } while (c != die->die_child);
27680 /* Remove dies representing declarations that we never use. */
27682 static void
27683 prune_unused_types (void)
27685 unsigned int i;
27686 limbo_die_node *node;
27687 comdat_type_node *ctnode;
27688 pubname_entry *pub;
27689 dw_die_ref base_type;
27691 #if ENABLE_ASSERT_CHECKING
27692 /* All the marks should already be clear. */
27693 verify_marks_clear (comp_unit_die ());
27694 for (node = limbo_die_list; node; node = node->next)
27695 verify_marks_clear (node->die);
27696 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27697 verify_marks_clear (ctnode->root_die);
27698 #endif /* ENABLE_ASSERT_CHECKING */
27700 /* Mark types that are used in global variables. */
27701 premark_types_used_by_global_vars ();
27703 /* Set the mark on nodes that are actually used. */
27704 prune_unused_types_walk (comp_unit_die ());
27705 for (node = limbo_die_list; node; node = node->next)
27706 prune_unused_types_walk (node->die);
27707 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27709 prune_unused_types_walk (ctnode->root_die);
27710 prune_unused_types_mark (ctnode->type_die, 1);
27713 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27714 are unusual in that they are pubnames that are the children of pubtypes.
27715 They should only be marked via their parent DW_TAG_enumeration_type die,
27716 not as roots in themselves. */
27717 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27718 if (pub->die->die_tag != DW_TAG_enumerator)
27719 prune_unused_types_mark (pub->die, 1);
27720 for (i = 0; base_types.iterate (i, &base_type); i++)
27721 prune_unused_types_mark (base_type, 1);
27723 if (debug_str_hash)
27724 debug_str_hash->empty ();
27725 if (skeleton_debug_str_hash)
27726 skeleton_debug_str_hash->empty ();
27727 prune_unused_types_prune (comp_unit_die ());
27728 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27730 node = *pnode;
27731 if (!node->die->die_mark)
27732 *pnode = node->next;
27733 else
27735 prune_unused_types_prune (node->die);
27736 pnode = &node->next;
27739 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27740 prune_unused_types_prune (ctnode->root_die);
27742 /* Leave the marks clear. */
27743 prune_unmark_dies (comp_unit_die ());
27744 for (node = limbo_die_list; node; node = node->next)
27745 prune_unmark_dies (node->die);
27746 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27747 prune_unmark_dies (ctnode->root_die);
27750 /* Helpers to manipulate hash table of comdat type units. */
27752 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27754 static inline hashval_t hash (const comdat_type_node *);
27755 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27758 inline hashval_t
27759 comdat_type_hasher::hash (const comdat_type_node *type_node)
27761 hashval_t h;
27762 memcpy (&h, type_node->signature, sizeof (h));
27763 return h;
27766 inline bool
27767 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27768 const comdat_type_node *type_node_2)
27770 return (! memcmp (type_node_1->signature, type_node_2->signature,
27771 DWARF_TYPE_SIGNATURE_SIZE));
27774 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
27775 to the location it would have been added, should we know its
27776 DECL_ASSEMBLER_NAME when we added other attributes. This will
27777 probably improve compactness of debug info, removing equivalent
27778 abbrevs, and hide any differences caused by deferring the
27779 computation of the assembler name, triggered by e.g. PCH. */
27781 static inline void
27782 move_linkage_attr (dw_die_ref die)
27784 unsigned ix = vec_safe_length (die->die_attr);
27785 dw_attr_node linkage = (*die->die_attr)[ix - 1];
27787 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
27788 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
27790 while (--ix > 0)
27792 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
27794 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
27795 break;
27798 if (ix != vec_safe_length (die->die_attr) - 1)
27800 die->die_attr->pop ();
27801 die->die_attr->quick_insert (ix, linkage);
27805 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
27806 referenced from typed stack ops and count how often they are used. */
27808 static void
27809 mark_base_types (dw_loc_descr_ref loc)
27811 dw_die_ref base_type = NULL;
27813 for (; loc; loc = loc->dw_loc_next)
27815 switch (loc->dw_loc_opc)
27817 case DW_OP_regval_type:
27818 case DW_OP_deref_type:
27819 case DW_OP_GNU_regval_type:
27820 case DW_OP_GNU_deref_type:
27821 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
27822 break;
27823 case DW_OP_convert:
27824 case DW_OP_reinterpret:
27825 case DW_OP_GNU_convert:
27826 case DW_OP_GNU_reinterpret:
27827 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
27828 continue;
27829 /* FALLTHRU */
27830 case DW_OP_const_type:
27831 case DW_OP_GNU_const_type:
27832 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
27833 break;
27834 case DW_OP_entry_value:
27835 case DW_OP_GNU_entry_value:
27836 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
27837 continue;
27838 default:
27839 continue;
27841 gcc_assert (base_type->die_parent == comp_unit_die ());
27842 if (base_type->die_mark)
27843 base_type->die_mark++;
27844 else
27846 base_types.safe_push (base_type);
27847 base_type->die_mark = 1;
27852 /* Comparison function for sorting marked base types. */
27854 static int
27855 base_type_cmp (const void *x, const void *y)
27857 dw_die_ref dx = *(const dw_die_ref *) x;
27858 dw_die_ref dy = *(const dw_die_ref *) y;
27859 unsigned int byte_size1, byte_size2;
27860 unsigned int encoding1, encoding2;
27861 if (dx->die_mark > dy->die_mark)
27862 return -1;
27863 if (dx->die_mark < dy->die_mark)
27864 return 1;
27865 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
27866 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
27867 if (byte_size1 < byte_size2)
27868 return 1;
27869 if (byte_size1 > byte_size2)
27870 return -1;
27871 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
27872 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
27873 if (encoding1 < encoding2)
27874 return 1;
27875 if (encoding1 > encoding2)
27876 return -1;
27877 return 0;
27880 /* Move base types marked by mark_base_types as early as possible
27881 in the CU, sorted by decreasing usage count both to make the
27882 uleb128 references as small as possible and to make sure they
27883 will have die_offset already computed by calc_die_sizes when
27884 sizes of typed stack loc ops is computed. */
27886 static void
27887 move_marked_base_types (void)
27889 unsigned int i;
27890 dw_die_ref base_type, die, c;
27892 if (base_types.is_empty ())
27893 return;
27895 /* Sort by decreasing usage count, they will be added again in that
27896 order later on. */
27897 base_types.qsort (base_type_cmp);
27898 die = comp_unit_die ();
27899 c = die->die_child;
27902 dw_die_ref prev = c;
27903 c = c->die_sib;
27904 while (c->die_mark)
27906 remove_child_with_prev (c, prev);
27907 /* As base types got marked, there must be at least
27908 one node other than DW_TAG_base_type. */
27909 gcc_assert (die->die_child != NULL);
27910 c = prev->die_sib;
27913 while (c != die->die_child);
27914 gcc_assert (die->die_child);
27915 c = die->die_child;
27916 for (i = 0; base_types.iterate (i, &base_type); i++)
27918 base_type->die_mark = 0;
27919 base_type->die_sib = c->die_sib;
27920 c->die_sib = base_type;
27921 c = base_type;
27925 /* Helper function for resolve_addr, attempt to resolve
27926 one CONST_STRING, return true if successful. Similarly verify that
27927 SYMBOL_REFs refer to variables emitted in the current CU. */
27929 static bool
27930 resolve_one_addr (rtx *addr)
27932 rtx rtl = *addr;
27934 if (GET_CODE (rtl) == CONST_STRING)
27936 size_t len = strlen (XSTR (rtl, 0)) + 1;
27937 tree t = build_string (len, XSTR (rtl, 0));
27938 tree tlen = size_int (len - 1);
27939 TREE_TYPE (t)
27940 = build_array_type (char_type_node, build_index_type (tlen));
27941 rtl = lookup_constant_def (t);
27942 if (!rtl || !MEM_P (rtl))
27943 return false;
27944 rtl = XEXP (rtl, 0);
27945 if (GET_CODE (rtl) == SYMBOL_REF
27946 && SYMBOL_REF_DECL (rtl)
27947 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
27948 return false;
27949 vec_safe_push (used_rtx_array, rtl);
27950 *addr = rtl;
27951 return true;
27954 if (GET_CODE (rtl) == SYMBOL_REF
27955 && SYMBOL_REF_DECL (rtl))
27957 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
27959 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
27960 return false;
27962 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
27963 return false;
27966 if (GET_CODE (rtl) == CONST)
27968 subrtx_ptr_iterator::array_type array;
27969 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
27970 if (!resolve_one_addr (*iter))
27971 return false;
27974 return true;
27977 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
27978 if possible, and create DW_TAG_dwarf_procedure that can be referenced
27979 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
27981 static rtx
27982 string_cst_pool_decl (tree t)
27984 rtx rtl = output_constant_def (t, 1);
27985 unsigned char *array;
27986 dw_loc_descr_ref l;
27987 tree decl;
27988 size_t len;
27989 dw_die_ref ref;
27991 if (!rtl || !MEM_P (rtl))
27992 return NULL_RTX;
27993 rtl = XEXP (rtl, 0);
27994 if (GET_CODE (rtl) != SYMBOL_REF
27995 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
27996 return NULL_RTX;
27998 decl = SYMBOL_REF_DECL (rtl);
27999 if (!lookup_decl_die (decl))
28001 len = TREE_STRING_LENGTH (t);
28002 vec_safe_push (used_rtx_array, rtl);
28003 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28004 array = ggc_vec_alloc<unsigned char> (len);
28005 memcpy (array, TREE_STRING_POINTER (t), len);
28006 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28007 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28008 l->dw_loc_oprnd2.v.val_vec.length = len;
28009 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28010 l->dw_loc_oprnd2.v.val_vec.array = array;
28011 add_AT_loc (ref, DW_AT_location, l);
28012 equate_decl_number_to_die (decl, ref);
28014 return rtl;
28017 /* Helper function of resolve_addr_in_expr. LOC is
28018 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28019 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28020 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28021 with DW_OP_implicit_pointer if possible
28022 and return true, if unsuccessful, return false. */
28024 static bool
28025 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28027 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28028 HOST_WIDE_INT offset = 0;
28029 dw_die_ref ref = NULL;
28030 tree decl;
28032 if (GET_CODE (rtl) == CONST
28033 && GET_CODE (XEXP (rtl, 0)) == PLUS
28034 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28036 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28037 rtl = XEXP (XEXP (rtl, 0), 0);
28039 if (GET_CODE (rtl) == CONST_STRING)
28041 size_t len = strlen (XSTR (rtl, 0)) + 1;
28042 tree t = build_string (len, XSTR (rtl, 0));
28043 tree tlen = size_int (len - 1);
28045 TREE_TYPE (t)
28046 = build_array_type (char_type_node, build_index_type (tlen));
28047 rtl = string_cst_pool_decl (t);
28048 if (!rtl)
28049 return false;
28051 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28053 decl = SYMBOL_REF_DECL (rtl);
28054 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28056 ref = lookup_decl_die (decl);
28057 if (ref && (get_AT (ref, DW_AT_location)
28058 || get_AT (ref, DW_AT_const_value)))
28060 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28061 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28062 loc->dw_loc_oprnd1.val_entry = NULL;
28063 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28064 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28065 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28066 loc->dw_loc_oprnd2.v.val_int = offset;
28067 return true;
28071 return false;
28074 /* Helper function for resolve_addr, handle one location
28075 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28076 the location list couldn't be resolved. */
28078 static bool
28079 resolve_addr_in_expr (dw_loc_descr_ref loc)
28081 dw_loc_descr_ref keep = NULL;
28082 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28083 switch (loc->dw_loc_opc)
28085 case DW_OP_addr:
28086 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28088 if ((prev == NULL
28089 || prev->dw_loc_opc == DW_OP_piece
28090 || prev->dw_loc_opc == DW_OP_bit_piece)
28091 && loc->dw_loc_next
28092 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28093 && (!dwarf_strict || dwarf_version >= 5)
28094 && optimize_one_addr_into_implicit_ptr (loc))
28095 break;
28096 return false;
28098 break;
28099 case DW_OP_GNU_addr_index:
28100 case DW_OP_GNU_const_index:
28101 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28102 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28104 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28105 if (!resolve_one_addr (&rtl))
28106 return false;
28107 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28108 loc->dw_loc_oprnd1.val_entry
28109 = add_addr_table_entry (rtl, ate_kind_rtx);
28111 break;
28112 case DW_OP_const4u:
28113 case DW_OP_const8u:
28114 if (loc->dtprel
28115 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28116 return false;
28117 break;
28118 case DW_OP_plus_uconst:
28119 if (size_of_loc_descr (loc)
28120 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28122 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28124 dw_loc_descr_ref repl
28125 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28126 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28127 add_loc_descr (&repl, loc->dw_loc_next);
28128 *loc = *repl;
28130 break;
28131 case DW_OP_implicit_value:
28132 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28133 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28134 return false;
28135 break;
28136 case DW_OP_implicit_pointer:
28137 case DW_OP_GNU_implicit_pointer:
28138 case DW_OP_GNU_parameter_ref:
28139 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28141 dw_die_ref ref
28142 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28143 if (ref == NULL)
28144 return false;
28145 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28146 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28147 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28149 break;
28150 case DW_OP_const_type:
28151 case DW_OP_regval_type:
28152 case DW_OP_deref_type:
28153 case DW_OP_convert:
28154 case DW_OP_reinterpret:
28155 case DW_OP_GNU_const_type:
28156 case DW_OP_GNU_regval_type:
28157 case DW_OP_GNU_deref_type:
28158 case DW_OP_GNU_convert:
28159 case DW_OP_GNU_reinterpret:
28160 while (loc->dw_loc_next
28161 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28162 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28164 dw_die_ref base1, base2;
28165 unsigned enc1, enc2, size1, size2;
28166 if (loc->dw_loc_opc == DW_OP_regval_type
28167 || loc->dw_loc_opc == DW_OP_deref_type
28168 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28169 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28170 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28171 else if (loc->dw_loc_oprnd1.val_class
28172 == dw_val_class_unsigned_const)
28173 break;
28174 else
28175 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28176 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28177 == dw_val_class_unsigned_const)
28178 break;
28179 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28180 gcc_assert (base1->die_tag == DW_TAG_base_type
28181 && base2->die_tag == DW_TAG_base_type);
28182 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28183 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28184 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28185 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28186 if (size1 == size2
28187 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28188 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28189 && loc != keep)
28190 || enc1 == enc2))
28192 /* Optimize away next DW_OP_convert after
28193 adjusting LOC's base type die reference. */
28194 if (loc->dw_loc_opc == DW_OP_regval_type
28195 || loc->dw_loc_opc == DW_OP_deref_type
28196 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28197 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28198 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28199 else
28200 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28201 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28202 continue;
28204 /* Don't change integer DW_OP_convert after e.g. floating
28205 point typed stack entry. */
28206 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28207 keep = loc->dw_loc_next;
28208 break;
28210 break;
28211 default:
28212 break;
28214 return true;
28217 /* Helper function of resolve_addr. DIE had DW_AT_location of
28218 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28219 and DW_OP_addr couldn't be resolved. resolve_addr has already
28220 removed the DW_AT_location attribute. This function attempts to
28221 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28222 to it or DW_AT_const_value attribute, if possible. */
28224 static void
28225 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28227 if (!VAR_P (decl)
28228 || lookup_decl_die (decl) != die
28229 || DECL_EXTERNAL (decl)
28230 || !TREE_STATIC (decl)
28231 || DECL_INITIAL (decl) == NULL_TREE
28232 || DECL_P (DECL_INITIAL (decl))
28233 || get_AT (die, DW_AT_const_value))
28234 return;
28236 tree init = DECL_INITIAL (decl);
28237 HOST_WIDE_INT offset = 0;
28238 /* For variables that have been optimized away and thus
28239 don't have a memory location, see if we can emit
28240 DW_AT_const_value instead. */
28241 if (tree_add_const_value_attribute (die, init))
28242 return;
28243 if (dwarf_strict && dwarf_version < 5)
28244 return;
28245 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28246 and ADDR_EXPR refers to a decl that has DW_AT_location or
28247 DW_AT_const_value (but isn't addressable, otherwise
28248 resolving the original DW_OP_addr wouldn't fail), see if
28249 we can add DW_OP_implicit_pointer. */
28250 STRIP_NOPS (init);
28251 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28252 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28254 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28255 init = TREE_OPERAND (init, 0);
28256 STRIP_NOPS (init);
28258 if (TREE_CODE (init) != ADDR_EXPR)
28259 return;
28260 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28261 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28262 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28263 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28264 && TREE_OPERAND (init, 0) != decl))
28266 dw_die_ref ref;
28267 dw_loc_descr_ref l;
28269 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28271 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28272 if (!rtl)
28273 return;
28274 decl = SYMBOL_REF_DECL (rtl);
28276 else
28277 decl = TREE_OPERAND (init, 0);
28278 ref = lookup_decl_die (decl);
28279 if (ref == NULL
28280 || (!get_AT (ref, DW_AT_location)
28281 && !get_AT (ref, DW_AT_const_value)))
28282 return;
28283 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28284 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28285 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28286 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28287 add_AT_loc (die, DW_AT_location, l);
28291 /* Return NULL if l is a DWARF expression, or first op that is not
28292 valid DWARF expression. */
28294 static dw_loc_descr_ref
28295 non_dwarf_expression (dw_loc_descr_ref l)
28297 while (l)
28299 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28300 return l;
28301 switch (l->dw_loc_opc)
28303 case DW_OP_regx:
28304 case DW_OP_implicit_value:
28305 case DW_OP_stack_value:
28306 case DW_OP_implicit_pointer:
28307 case DW_OP_GNU_implicit_pointer:
28308 case DW_OP_GNU_parameter_ref:
28309 case DW_OP_piece:
28310 case DW_OP_bit_piece:
28311 return l;
28312 default:
28313 break;
28315 l = l->dw_loc_next;
28317 return NULL;
28320 /* Return adjusted copy of EXPR:
28321 If it is empty DWARF expression, return it.
28322 If it is valid non-empty DWARF expression,
28323 return copy of EXPR with copy of DEREF appended to it.
28324 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28325 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
28326 and no DEREF.
28327 If it is DWARF expression followed by DW_OP_stack_value, return
28328 copy of the DWARF expression without anything appended.
28329 Otherwise, return NULL. */
28331 static dw_loc_descr_ref
28332 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
28335 if (expr == NULL)
28336 return NULL;
28338 dw_loc_descr_ref l = non_dwarf_expression (expr);
28339 if (l && l->dw_loc_next)
28340 return NULL;
28342 if (l)
28344 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28345 deref = new_loc_descr ((enum dwarf_location_atom)
28346 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28347 0, 0);
28348 else
28349 switch (l->dw_loc_opc)
28351 case DW_OP_regx:
28352 deref = new_loc_descr (DW_OP_bregx,
28353 l->dw_loc_oprnd1.v.val_unsigned, 0);
28354 break;
28355 case DW_OP_stack_value:
28356 deref = NULL;
28357 break;
28358 default:
28359 return NULL;
28362 else
28363 deref = new_loc_descr (deref->dw_loc_opc,
28364 deref->dw_loc_oprnd1.v.val_int, 0);
28366 dw_loc_descr_ref ret = NULL, *p = &ret;
28367 while (expr != l)
28369 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28370 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28371 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28372 p = &(*p)->dw_loc_next;
28373 expr = expr->dw_loc_next;
28375 *p = deref;
28376 return ret;
28379 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
28380 or argument, adjust it if needed and return:
28381 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28382 attribute if present should be removed
28383 0 keep the attribute as is if the referenced var or argument has
28384 only DWARF expression that covers all ranges
28385 1 if the attribute has been successfully adjusted. */
28387 static int
28388 optimize_string_length (dw_attr_node *a)
28390 dw_loc_descr_ref l = AT_loc (a), lv;
28391 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
28392 dw_attr_node *av = get_AT (die, DW_AT_location);
28393 dw_loc_list_ref d;
28394 bool non_dwarf_expr = false;
28396 if (av == NULL)
28397 return -1;
28398 switch (AT_class (av))
28400 case dw_val_class_loc_list:
28401 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28402 if (d->expr && non_dwarf_expression (d->expr))
28403 non_dwarf_expr = true;
28404 break;
28405 case dw_val_class_loc:
28406 lv = AT_loc (av);
28407 if (lv == NULL)
28408 return -1;
28409 if (non_dwarf_expression (lv))
28410 non_dwarf_expr = true;
28411 break;
28412 default:
28413 return -1;
28416 /* If it is safe to keep DW_OP_call4 in, keep it. */
28417 if (!non_dwarf_expr
28418 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
28419 return 0;
28421 /* If not dereferencing the DW_OP_call4 afterwards, we can just
28422 copy over the DW_AT_location attribute from die to a. */
28423 if (l->dw_loc_next == NULL)
28425 a->dw_attr_val = av->dw_attr_val;
28426 return 1;
28429 dw_loc_list_ref list, *p;
28430 switch (AT_class (av))
28432 case dw_val_class_loc_list:
28433 p = &list;
28434 list = NULL;
28435 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28437 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
28438 if (lv)
28440 *p = new_loc_list (lv, d->begin, d->end, d->section);
28441 p = &(*p)->dw_loc_next;
28444 if (list == NULL)
28445 return -1;
28446 a->dw_attr_val.val_class = dw_val_class_loc_list;
28447 gen_llsym (list);
28448 *AT_loc_list_ptr (a) = list;
28449 return 1;
28450 case dw_val_class_loc:
28451 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
28452 if (lv == NULL)
28453 return -1;
28454 a->dw_attr_val.v.val_loc = lv;
28455 return 1;
28456 default:
28457 gcc_unreachable ();
28461 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28462 an address in .rodata section if the string literal is emitted there,
28463 or remove the containing location list or replace DW_AT_const_value
28464 with DW_AT_location and empty location expression, if it isn't found
28465 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28466 to something that has been emitted in the current CU. */
28468 static void
28469 resolve_addr (dw_die_ref die)
28471 dw_die_ref c;
28472 dw_attr_node *a;
28473 dw_loc_list_ref *curr, *start, loc;
28474 unsigned ix;
28475 bool remove_AT_byte_size = false;
28477 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28478 switch (AT_class (a))
28480 case dw_val_class_loc_list:
28481 start = curr = AT_loc_list_ptr (a);
28482 loc = *curr;
28483 gcc_assert (loc);
28484 /* The same list can be referenced more than once. See if we have
28485 already recorded the result from a previous pass. */
28486 if (loc->replaced)
28487 *curr = loc->dw_loc_next;
28488 else if (!loc->resolved_addr)
28490 /* As things stand, we do not expect or allow one die to
28491 reference a suffix of another die's location list chain.
28492 References must be identical or completely separate.
28493 There is therefore no need to cache the result of this
28494 pass on any list other than the first; doing so
28495 would lead to unnecessary writes. */
28496 while (*curr)
28498 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28499 if (!resolve_addr_in_expr ((*curr)->expr))
28501 dw_loc_list_ref next = (*curr)->dw_loc_next;
28502 dw_loc_descr_ref l = (*curr)->expr;
28504 if (next && (*curr)->ll_symbol)
28506 gcc_assert (!next->ll_symbol);
28507 next->ll_symbol = (*curr)->ll_symbol;
28509 if (dwarf_split_debug_info)
28510 remove_loc_list_addr_table_entries (l);
28511 *curr = next;
28513 else
28515 mark_base_types ((*curr)->expr);
28516 curr = &(*curr)->dw_loc_next;
28519 if (loc == *start)
28520 loc->resolved_addr = 1;
28521 else
28523 loc->replaced = 1;
28524 loc->dw_loc_next = *start;
28527 if (!*start)
28529 remove_AT (die, a->dw_attr);
28530 ix--;
28532 break;
28533 case dw_val_class_loc:
28535 dw_loc_descr_ref l = AT_loc (a);
28536 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
28537 DW_AT_string_length is only a rough approximation; unfortunately
28538 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
28539 needs a DWARF expression, while DW_AT_location of the referenced
28540 variable or argument might be any location description. */
28541 if (a->dw_attr == DW_AT_string_length
28542 && l
28543 && l->dw_loc_opc == DW_OP_call4
28544 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
28545 && (l->dw_loc_next == NULL
28546 || (l->dw_loc_next->dw_loc_next == NULL
28547 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
28548 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
28550 switch (optimize_string_length (a))
28552 case -1:
28553 remove_AT (die, a->dw_attr);
28554 ix--;
28555 /* If we drop DW_AT_string_length, we need to drop also
28556 DW_AT_{string_length_,}byte_size. */
28557 remove_AT_byte_size = true;
28558 continue;
28559 default:
28560 break;
28561 case 1:
28562 /* Even if we keep the optimized DW_AT_string_length,
28563 it might have changed AT_class, so process it again. */
28564 ix--;
28565 continue;
28568 /* For -gdwarf-2 don't attempt to optimize
28569 DW_AT_data_member_location containing
28570 DW_OP_plus_uconst - older consumers might
28571 rely on it being that op instead of a more complex,
28572 but shorter, location description. */
28573 if ((dwarf_version > 2
28574 || a->dw_attr != DW_AT_data_member_location
28575 || l == NULL
28576 || l->dw_loc_opc != DW_OP_plus_uconst
28577 || l->dw_loc_next != NULL)
28578 && !resolve_addr_in_expr (l))
28580 if (dwarf_split_debug_info)
28581 remove_loc_list_addr_table_entries (l);
28582 if (l != NULL
28583 && l->dw_loc_next == NULL
28584 && l->dw_loc_opc == DW_OP_addr
28585 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28586 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28587 && a->dw_attr == DW_AT_location)
28589 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28590 remove_AT (die, a->dw_attr);
28591 ix--;
28592 optimize_location_into_implicit_ptr (die, decl);
28593 break;
28595 remove_AT (die, a->dw_attr);
28596 ix--;
28598 else
28599 mark_base_types (l);
28601 break;
28602 case dw_val_class_addr:
28603 if (a->dw_attr == DW_AT_const_value
28604 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28606 if (AT_index (a) != NOT_INDEXED)
28607 remove_addr_table_entry (a->dw_attr_val.val_entry);
28608 remove_AT (die, a->dw_attr);
28609 ix--;
28611 if ((die->die_tag == DW_TAG_call_site
28612 && a->dw_attr == DW_AT_call_origin)
28613 || (die->die_tag == DW_TAG_GNU_call_site
28614 && a->dw_attr == DW_AT_abstract_origin))
28616 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28617 dw_die_ref tdie = lookup_decl_die (tdecl);
28618 dw_die_ref cdie;
28619 if (tdie == NULL
28620 && DECL_EXTERNAL (tdecl)
28621 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28622 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28624 /* Creating a full DIE for tdecl is overly expensive and
28625 at this point even wrong when in the LTO phase
28626 as it can end up generating new type DIEs we didn't
28627 output and thus optimize_external_refs will crash. */
28628 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28629 add_AT_flag (tdie, DW_AT_external, 1);
28630 add_AT_flag (tdie, DW_AT_declaration, 1);
28631 add_linkage_attr (tdie, tdecl);
28632 add_name_and_src_coords_attributes (tdie, tdecl);
28633 equate_decl_number_to_die (tdecl, tdie);
28635 if (tdie)
28637 a->dw_attr_val.val_class = dw_val_class_die_ref;
28638 a->dw_attr_val.v.val_die_ref.die = tdie;
28639 a->dw_attr_val.v.val_die_ref.external = 0;
28641 else
28643 if (AT_index (a) != NOT_INDEXED)
28644 remove_addr_table_entry (a->dw_attr_val.val_entry);
28645 remove_AT (die, a->dw_attr);
28646 ix--;
28649 break;
28650 default:
28651 break;
28654 if (remove_AT_byte_size)
28655 remove_AT (die, dwarf_version >= 5
28656 ? DW_AT_string_length_byte_size
28657 : DW_AT_byte_size);
28659 FOR_EACH_CHILD (die, c, resolve_addr (c));
28662 /* Helper routines for optimize_location_lists.
28663 This pass tries to share identical local lists in .debug_loc
28664 section. */
28666 /* Iteratively hash operands of LOC opcode into HSTATE. */
28668 static void
28669 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28671 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28672 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28674 switch (loc->dw_loc_opc)
28676 case DW_OP_const4u:
28677 case DW_OP_const8u:
28678 if (loc->dtprel)
28679 goto hash_addr;
28680 /* FALLTHRU */
28681 case DW_OP_const1u:
28682 case DW_OP_const1s:
28683 case DW_OP_const2u:
28684 case DW_OP_const2s:
28685 case DW_OP_const4s:
28686 case DW_OP_const8s:
28687 case DW_OP_constu:
28688 case DW_OP_consts:
28689 case DW_OP_pick:
28690 case DW_OP_plus_uconst:
28691 case DW_OP_breg0:
28692 case DW_OP_breg1:
28693 case DW_OP_breg2:
28694 case DW_OP_breg3:
28695 case DW_OP_breg4:
28696 case DW_OP_breg5:
28697 case DW_OP_breg6:
28698 case DW_OP_breg7:
28699 case DW_OP_breg8:
28700 case DW_OP_breg9:
28701 case DW_OP_breg10:
28702 case DW_OP_breg11:
28703 case DW_OP_breg12:
28704 case DW_OP_breg13:
28705 case DW_OP_breg14:
28706 case DW_OP_breg15:
28707 case DW_OP_breg16:
28708 case DW_OP_breg17:
28709 case DW_OP_breg18:
28710 case DW_OP_breg19:
28711 case DW_OP_breg20:
28712 case DW_OP_breg21:
28713 case DW_OP_breg22:
28714 case DW_OP_breg23:
28715 case DW_OP_breg24:
28716 case DW_OP_breg25:
28717 case DW_OP_breg26:
28718 case DW_OP_breg27:
28719 case DW_OP_breg28:
28720 case DW_OP_breg29:
28721 case DW_OP_breg30:
28722 case DW_OP_breg31:
28723 case DW_OP_regx:
28724 case DW_OP_fbreg:
28725 case DW_OP_piece:
28726 case DW_OP_deref_size:
28727 case DW_OP_xderef_size:
28728 hstate.add_object (val1->v.val_int);
28729 break;
28730 case DW_OP_skip:
28731 case DW_OP_bra:
28733 int offset;
28735 gcc_assert (val1->val_class == dw_val_class_loc);
28736 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
28737 hstate.add_object (offset);
28739 break;
28740 case DW_OP_implicit_value:
28741 hstate.add_object (val1->v.val_unsigned);
28742 switch (val2->val_class)
28744 case dw_val_class_const:
28745 hstate.add_object (val2->v.val_int);
28746 break;
28747 case dw_val_class_vec:
28749 unsigned int elt_size = val2->v.val_vec.elt_size;
28750 unsigned int len = val2->v.val_vec.length;
28752 hstate.add_int (elt_size);
28753 hstate.add_int (len);
28754 hstate.add (val2->v.val_vec.array, len * elt_size);
28756 break;
28757 case dw_val_class_const_double:
28758 hstate.add_object (val2->v.val_double.low);
28759 hstate.add_object (val2->v.val_double.high);
28760 break;
28761 case dw_val_class_wide_int:
28762 hstate.add (val2->v.val_wide->get_val (),
28763 get_full_len (*val2->v.val_wide)
28764 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28765 break;
28766 case dw_val_class_addr:
28767 inchash::add_rtx (val2->v.val_addr, hstate);
28768 break;
28769 default:
28770 gcc_unreachable ();
28772 break;
28773 case DW_OP_bregx:
28774 case DW_OP_bit_piece:
28775 hstate.add_object (val1->v.val_int);
28776 hstate.add_object (val2->v.val_int);
28777 break;
28778 case DW_OP_addr:
28779 hash_addr:
28780 if (loc->dtprel)
28782 unsigned char dtprel = 0xd1;
28783 hstate.add_object (dtprel);
28785 inchash::add_rtx (val1->v.val_addr, hstate);
28786 break;
28787 case DW_OP_GNU_addr_index:
28788 case DW_OP_GNU_const_index:
28790 if (loc->dtprel)
28792 unsigned char dtprel = 0xd1;
28793 hstate.add_object (dtprel);
28795 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
28797 break;
28798 case DW_OP_implicit_pointer:
28799 case DW_OP_GNU_implicit_pointer:
28800 hstate.add_int (val2->v.val_int);
28801 break;
28802 case DW_OP_entry_value:
28803 case DW_OP_GNU_entry_value:
28804 hstate.add_object (val1->v.val_loc);
28805 break;
28806 case DW_OP_regval_type:
28807 case DW_OP_deref_type:
28808 case DW_OP_GNU_regval_type:
28809 case DW_OP_GNU_deref_type:
28811 unsigned int byte_size
28812 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
28813 unsigned int encoding
28814 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
28815 hstate.add_object (val1->v.val_int);
28816 hstate.add_object (byte_size);
28817 hstate.add_object (encoding);
28819 break;
28820 case DW_OP_convert:
28821 case DW_OP_reinterpret:
28822 case DW_OP_GNU_convert:
28823 case DW_OP_GNU_reinterpret:
28824 if (val1->val_class == dw_val_class_unsigned_const)
28826 hstate.add_object (val1->v.val_unsigned);
28827 break;
28829 /* FALLTHRU */
28830 case DW_OP_const_type:
28831 case DW_OP_GNU_const_type:
28833 unsigned int byte_size
28834 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
28835 unsigned int encoding
28836 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
28837 hstate.add_object (byte_size);
28838 hstate.add_object (encoding);
28839 if (loc->dw_loc_opc != DW_OP_const_type
28840 && loc->dw_loc_opc != DW_OP_GNU_const_type)
28841 break;
28842 hstate.add_object (val2->val_class);
28843 switch (val2->val_class)
28845 case dw_val_class_const:
28846 hstate.add_object (val2->v.val_int);
28847 break;
28848 case dw_val_class_vec:
28850 unsigned int elt_size = val2->v.val_vec.elt_size;
28851 unsigned int len = val2->v.val_vec.length;
28853 hstate.add_object (elt_size);
28854 hstate.add_object (len);
28855 hstate.add (val2->v.val_vec.array, len * elt_size);
28857 break;
28858 case dw_val_class_const_double:
28859 hstate.add_object (val2->v.val_double.low);
28860 hstate.add_object (val2->v.val_double.high);
28861 break;
28862 case dw_val_class_wide_int:
28863 hstate.add (val2->v.val_wide->get_val (),
28864 get_full_len (*val2->v.val_wide)
28865 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28866 break;
28867 default:
28868 gcc_unreachable ();
28871 break;
28873 default:
28874 /* Other codes have no operands. */
28875 break;
28879 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
28881 static inline void
28882 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
28884 dw_loc_descr_ref l;
28885 bool sizes_computed = false;
28886 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
28887 size_of_locs (loc);
28889 for (l = loc; l != NULL; l = l->dw_loc_next)
28891 enum dwarf_location_atom opc = l->dw_loc_opc;
28892 hstate.add_object (opc);
28893 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
28895 size_of_locs (loc);
28896 sizes_computed = true;
28898 hash_loc_operands (l, hstate);
28902 /* Compute hash of the whole location list LIST_HEAD. */
28904 static inline void
28905 hash_loc_list (dw_loc_list_ref list_head)
28907 dw_loc_list_ref curr = list_head;
28908 inchash::hash hstate;
28910 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
28912 hstate.add (curr->begin, strlen (curr->begin) + 1);
28913 hstate.add (curr->end, strlen (curr->end) + 1);
28914 if (curr->section)
28915 hstate.add (curr->section, strlen (curr->section) + 1);
28916 hash_locs (curr->expr, hstate);
28918 list_head->hash = hstate.end ();
28921 /* Return true if X and Y opcodes have the same operands. */
28923 static inline bool
28924 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
28926 dw_val_ref valx1 = &x->dw_loc_oprnd1;
28927 dw_val_ref valx2 = &x->dw_loc_oprnd2;
28928 dw_val_ref valy1 = &y->dw_loc_oprnd1;
28929 dw_val_ref valy2 = &y->dw_loc_oprnd2;
28931 switch (x->dw_loc_opc)
28933 case DW_OP_const4u:
28934 case DW_OP_const8u:
28935 if (x->dtprel)
28936 goto hash_addr;
28937 /* FALLTHRU */
28938 case DW_OP_const1u:
28939 case DW_OP_const1s:
28940 case DW_OP_const2u:
28941 case DW_OP_const2s:
28942 case DW_OP_const4s:
28943 case DW_OP_const8s:
28944 case DW_OP_constu:
28945 case DW_OP_consts:
28946 case DW_OP_pick:
28947 case DW_OP_plus_uconst:
28948 case DW_OP_breg0:
28949 case DW_OP_breg1:
28950 case DW_OP_breg2:
28951 case DW_OP_breg3:
28952 case DW_OP_breg4:
28953 case DW_OP_breg5:
28954 case DW_OP_breg6:
28955 case DW_OP_breg7:
28956 case DW_OP_breg8:
28957 case DW_OP_breg9:
28958 case DW_OP_breg10:
28959 case DW_OP_breg11:
28960 case DW_OP_breg12:
28961 case DW_OP_breg13:
28962 case DW_OP_breg14:
28963 case DW_OP_breg15:
28964 case DW_OP_breg16:
28965 case DW_OP_breg17:
28966 case DW_OP_breg18:
28967 case DW_OP_breg19:
28968 case DW_OP_breg20:
28969 case DW_OP_breg21:
28970 case DW_OP_breg22:
28971 case DW_OP_breg23:
28972 case DW_OP_breg24:
28973 case DW_OP_breg25:
28974 case DW_OP_breg26:
28975 case DW_OP_breg27:
28976 case DW_OP_breg28:
28977 case DW_OP_breg29:
28978 case DW_OP_breg30:
28979 case DW_OP_breg31:
28980 case DW_OP_regx:
28981 case DW_OP_fbreg:
28982 case DW_OP_piece:
28983 case DW_OP_deref_size:
28984 case DW_OP_xderef_size:
28985 return valx1->v.val_int == valy1->v.val_int;
28986 case DW_OP_skip:
28987 case DW_OP_bra:
28988 /* If splitting debug info, the use of DW_OP_GNU_addr_index
28989 can cause irrelevant differences in dw_loc_addr. */
28990 gcc_assert (valx1->val_class == dw_val_class_loc
28991 && valy1->val_class == dw_val_class_loc
28992 && (dwarf_split_debug_info
28993 || x->dw_loc_addr == y->dw_loc_addr));
28994 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
28995 case DW_OP_implicit_value:
28996 if (valx1->v.val_unsigned != valy1->v.val_unsigned
28997 || valx2->val_class != valy2->val_class)
28998 return false;
28999 switch (valx2->val_class)
29001 case dw_val_class_const:
29002 return valx2->v.val_int == valy2->v.val_int;
29003 case dw_val_class_vec:
29004 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29005 && valx2->v.val_vec.length == valy2->v.val_vec.length
29006 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29007 valx2->v.val_vec.elt_size
29008 * valx2->v.val_vec.length) == 0;
29009 case dw_val_class_const_double:
29010 return valx2->v.val_double.low == valy2->v.val_double.low
29011 && valx2->v.val_double.high == valy2->v.val_double.high;
29012 case dw_val_class_wide_int:
29013 return *valx2->v.val_wide == *valy2->v.val_wide;
29014 case dw_val_class_addr:
29015 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29016 default:
29017 gcc_unreachable ();
29019 case DW_OP_bregx:
29020 case DW_OP_bit_piece:
29021 return valx1->v.val_int == valy1->v.val_int
29022 && valx2->v.val_int == valy2->v.val_int;
29023 case DW_OP_addr:
29024 hash_addr:
29025 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29026 case DW_OP_GNU_addr_index:
29027 case DW_OP_GNU_const_index:
29029 rtx ax1 = valx1->val_entry->addr.rtl;
29030 rtx ay1 = valy1->val_entry->addr.rtl;
29031 return rtx_equal_p (ax1, ay1);
29033 case DW_OP_implicit_pointer:
29034 case DW_OP_GNU_implicit_pointer:
29035 return valx1->val_class == dw_val_class_die_ref
29036 && valx1->val_class == valy1->val_class
29037 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29038 && valx2->v.val_int == valy2->v.val_int;
29039 case DW_OP_entry_value:
29040 case DW_OP_GNU_entry_value:
29041 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29042 case DW_OP_const_type:
29043 case DW_OP_GNU_const_type:
29044 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29045 || valx2->val_class != valy2->val_class)
29046 return false;
29047 switch (valx2->val_class)
29049 case dw_val_class_const:
29050 return valx2->v.val_int == valy2->v.val_int;
29051 case dw_val_class_vec:
29052 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29053 && valx2->v.val_vec.length == valy2->v.val_vec.length
29054 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29055 valx2->v.val_vec.elt_size
29056 * valx2->v.val_vec.length) == 0;
29057 case dw_val_class_const_double:
29058 return valx2->v.val_double.low == valy2->v.val_double.low
29059 && valx2->v.val_double.high == valy2->v.val_double.high;
29060 case dw_val_class_wide_int:
29061 return *valx2->v.val_wide == *valy2->v.val_wide;
29062 default:
29063 gcc_unreachable ();
29065 case DW_OP_regval_type:
29066 case DW_OP_deref_type:
29067 case DW_OP_GNU_regval_type:
29068 case DW_OP_GNU_deref_type:
29069 return valx1->v.val_int == valy1->v.val_int
29070 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29071 case DW_OP_convert:
29072 case DW_OP_reinterpret:
29073 case DW_OP_GNU_convert:
29074 case DW_OP_GNU_reinterpret:
29075 if (valx1->val_class != valy1->val_class)
29076 return false;
29077 if (valx1->val_class == dw_val_class_unsigned_const)
29078 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29079 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29080 case DW_OP_GNU_parameter_ref:
29081 return valx1->val_class == dw_val_class_die_ref
29082 && valx1->val_class == valy1->val_class
29083 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29084 default:
29085 /* Other codes have no operands. */
29086 return true;
29090 /* Return true if DWARF location expressions X and Y are the same. */
29092 static inline bool
29093 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29095 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29096 if (x->dw_loc_opc != y->dw_loc_opc
29097 || x->dtprel != y->dtprel
29098 || !compare_loc_operands (x, y))
29099 break;
29100 return x == NULL && y == NULL;
29103 /* Hashtable helpers. */
29105 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29107 static inline hashval_t hash (const dw_loc_list_struct *);
29108 static inline bool equal (const dw_loc_list_struct *,
29109 const dw_loc_list_struct *);
29112 /* Return precomputed hash of location list X. */
29114 inline hashval_t
29115 loc_list_hasher::hash (const dw_loc_list_struct *x)
29117 return x->hash;
29120 /* Return true if location lists A and B are the same. */
29122 inline bool
29123 loc_list_hasher::equal (const dw_loc_list_struct *a,
29124 const dw_loc_list_struct *b)
29126 if (a == b)
29127 return 1;
29128 if (a->hash != b->hash)
29129 return 0;
29130 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29131 if (strcmp (a->begin, b->begin) != 0
29132 || strcmp (a->end, b->end) != 0
29133 || (a->section == NULL) != (b->section == NULL)
29134 || (a->section && strcmp (a->section, b->section) != 0)
29135 || !compare_locs (a->expr, b->expr))
29136 break;
29137 return a == NULL && b == NULL;
29140 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29143 /* Recursively optimize location lists referenced from DIE
29144 children and share them whenever possible. */
29146 static void
29147 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29149 dw_die_ref c;
29150 dw_attr_node *a;
29151 unsigned ix;
29152 dw_loc_list_struct **slot;
29154 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29155 if (AT_class (a) == dw_val_class_loc_list)
29157 dw_loc_list_ref list = AT_loc_list (a);
29158 /* TODO: perform some optimizations here, before hashing
29159 it and storing into the hash table. */
29160 hash_loc_list (list);
29161 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29162 if (*slot == NULL)
29163 *slot = list;
29164 else
29165 a->dw_attr_val.v.val_loc_list = *slot;
29168 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29172 /* Recursively assign each location list a unique index into the debug_addr
29173 section. */
29175 static void
29176 index_location_lists (dw_die_ref die)
29178 dw_die_ref c;
29179 dw_attr_node *a;
29180 unsigned ix;
29182 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29183 if (AT_class (a) == dw_val_class_loc_list)
29185 dw_loc_list_ref list = AT_loc_list (a);
29186 dw_loc_list_ref curr;
29187 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29189 /* Don't index an entry that has already been indexed
29190 or won't be output. */
29191 if (curr->begin_entry != NULL
29192 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29193 continue;
29195 curr->begin_entry
29196 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29200 FOR_EACH_CHILD (die, c, index_location_lists (c));
29203 /* Optimize location lists referenced from DIE
29204 children and share them whenever possible. */
29206 static void
29207 optimize_location_lists (dw_die_ref die)
29209 loc_list_hash_type htab (500);
29210 optimize_location_lists_1 (die, &htab);
29213 /* Traverse the limbo die list, and add parent/child links. The only
29214 dies without parents that should be here are concrete instances of
29215 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29216 For concrete instances, we can get the parent die from the abstract
29217 instance. */
29219 static void
29220 flush_limbo_die_list (void)
29222 limbo_die_node *node;
29224 /* get_context_die calls force_decl_die, which can put new DIEs on the
29225 limbo list in LTO mode when nested functions are put in a different
29226 partition than that of their parent function. */
29227 while ((node = limbo_die_list))
29229 dw_die_ref die = node->die;
29230 limbo_die_list = node->next;
29232 if (die->die_parent == NULL)
29234 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29236 if (origin && origin->die_parent)
29237 add_child_die (origin->die_parent, die);
29238 else if (is_cu_die (die))
29240 else if (seen_error ())
29241 /* It's OK to be confused by errors in the input. */
29242 add_child_die (comp_unit_die (), die);
29243 else
29245 /* In certain situations, the lexical block containing a
29246 nested function can be optimized away, which results
29247 in the nested function die being orphaned. Likewise
29248 with the return type of that nested function. Force
29249 this to be a child of the containing function.
29251 It may happen that even the containing function got fully
29252 inlined and optimized out. In that case we are lost and
29253 assign the empty child. This should not be big issue as
29254 the function is likely unreachable too. */
29255 gcc_assert (node->created_for);
29257 if (DECL_P (node->created_for))
29258 origin = get_context_die (DECL_CONTEXT (node->created_for));
29259 else if (TYPE_P (node->created_for))
29260 origin = scope_die_for (node->created_for, comp_unit_die ());
29261 else
29262 origin = comp_unit_die ();
29264 add_child_die (origin, die);
29270 /* Output stuff that dwarf requires at the end of every file,
29271 and generate the DWARF-2 debugging info. */
29273 static void
29274 dwarf2out_finish (const char *)
29276 comdat_type_node *ctnode;
29277 dw_die_ref main_comp_unit_die;
29278 unsigned char checksum[16];
29280 /* Flush out any latecomers to the limbo party. */
29281 flush_limbo_die_list ();
29283 if (flag_checking)
29285 verify_die (comp_unit_die ());
29286 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29287 verify_die (node->die);
29290 /* We shouldn't have any symbols with delayed asm names for
29291 DIEs generated after early finish. */
29292 gcc_assert (deferred_asm_name == NULL);
29294 gen_remaining_tmpl_value_param_die_attribute ();
29296 #if ENABLE_ASSERT_CHECKING
29298 dw_die_ref die = comp_unit_die (), c;
29299 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29301 #endif
29302 resolve_addr (comp_unit_die ());
29303 move_marked_base_types ();
29305 /* Initialize sections and labels used for actual assembler output. */
29306 init_sections_and_labels ();
29308 /* Traverse the DIE's and add sibling attributes to those DIE's that
29309 have children. */
29310 add_sibling_attributes (comp_unit_die ());
29311 limbo_die_node *node;
29312 for (node = cu_die_list; node; node = node->next)
29313 add_sibling_attributes (node->die);
29314 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29315 add_sibling_attributes (ctnode->root_die);
29317 /* When splitting DWARF info, we put some attributes in the
29318 skeleton compile_unit DIE that remains in the .o, while
29319 most attributes go in the DWO compile_unit_die. */
29320 if (dwarf_split_debug_info)
29322 limbo_die_node *cu;
29323 main_comp_unit_die = gen_compile_unit_die (NULL);
29324 if (dwarf_version >= 5)
29325 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29326 cu = limbo_die_list;
29327 gcc_assert (cu->die == main_comp_unit_die);
29328 limbo_die_list = limbo_die_list->next;
29329 cu->next = cu_die_list;
29330 cu_die_list = cu;
29332 else
29333 main_comp_unit_die = comp_unit_die ();
29335 /* Output a terminator label for the .text section. */
29336 switch_to_section (text_section);
29337 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29338 if (cold_text_section)
29340 switch_to_section (cold_text_section);
29341 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29344 /* We can only use the low/high_pc attributes if all of the code was
29345 in .text. */
29346 if (!have_multiple_function_sections
29347 || (dwarf_version < 3 && dwarf_strict))
29349 /* Don't add if the CU has no associated code. */
29350 if (text_section_used)
29351 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29352 text_end_label, true);
29354 else
29356 unsigned fde_idx;
29357 dw_fde_ref fde;
29358 bool range_list_added = false;
29360 if (text_section_used)
29361 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29362 text_end_label, &range_list_added, true);
29363 if (cold_text_section_used)
29364 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29365 cold_end_label, &range_list_added, true);
29367 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29369 if (DECL_IGNORED_P (fde->decl))
29370 continue;
29371 if (!fde->in_std_section)
29372 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29373 fde->dw_fde_end, &range_list_added,
29374 true);
29375 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29376 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29377 fde->dw_fde_second_end, &range_list_added,
29378 true);
29381 if (range_list_added)
29383 /* We need to give .debug_loc and .debug_ranges an appropriate
29384 "base address". Use zero so that these addresses become
29385 absolute. Historically, we've emitted the unexpected
29386 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29387 Emit both to give time for other tools to adapt. */
29388 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29389 if (! dwarf_strict && dwarf_version < 4)
29390 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29392 add_ranges (NULL);
29396 if (debug_info_level >= DINFO_LEVEL_TERSE)
29397 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29398 debug_line_section_label);
29400 if (have_macinfo)
29401 add_AT_macptr (comp_unit_die (),
29402 dwarf_version >= 5 ? DW_AT_macros
29403 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29404 macinfo_section_label);
29406 if (dwarf_split_debug_info)
29408 if (have_location_lists)
29410 if (dwarf_version >= 5)
29411 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29412 loc_section_label);
29413 /* optimize_location_lists calculates the size of the lists,
29414 so index them first, and assign indices to the entries.
29415 Although optimize_location_lists will remove entries from
29416 the table, it only does so for duplicates, and therefore
29417 only reduces ref_counts to 1. */
29418 index_location_lists (comp_unit_die ());
29421 if (addr_index_table != NULL)
29423 unsigned int index = 0;
29424 addr_index_table
29425 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29426 (&index);
29430 loc_list_idx = 0;
29431 if (have_location_lists)
29433 optimize_location_lists (comp_unit_die ());
29434 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29435 if (dwarf_version >= 5 && dwarf_split_debug_info)
29436 assign_location_list_indexes (comp_unit_die ());
29439 save_macinfo_strings ();
29441 if (dwarf_split_debug_info)
29443 unsigned int index = 0;
29445 /* Add attributes common to skeleton compile_units and
29446 type_units. Because these attributes include strings, it
29447 must be done before freezing the string table. Top-level
29448 skeleton die attrs are added when the skeleton type unit is
29449 created, so ensure it is created by this point. */
29450 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29451 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29454 /* Output all of the compilation units. We put the main one last so that
29455 the offsets are available to output_pubnames. */
29456 for (node = cu_die_list; node; node = node->next)
29457 output_comp_unit (node->die, 0, NULL);
29459 hash_table<comdat_type_hasher> comdat_type_table (100);
29460 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29462 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29464 /* Don't output duplicate types. */
29465 if (*slot != HTAB_EMPTY_ENTRY)
29466 continue;
29468 /* Add a pointer to the line table for the main compilation unit
29469 so that the debugger can make sense of DW_AT_decl_file
29470 attributes. */
29471 if (debug_info_level >= DINFO_LEVEL_TERSE)
29472 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29473 (!dwarf_split_debug_info
29474 ? debug_line_section_label
29475 : debug_skeleton_line_section_label));
29477 output_comdat_type_unit (ctnode);
29478 *slot = ctnode;
29481 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29482 both the main_cu and all skeleton TUs. Making this call unconditional
29483 would end up either adding a second copy of the AT_pubnames attribute, or
29484 requiring a special case in add_top_level_skeleton_die_attrs. */
29485 if (!dwarf_split_debug_info)
29486 add_AT_pubnames (comp_unit_die ());
29488 if (dwarf_split_debug_info)
29490 int mark;
29491 struct md5_ctx ctx;
29493 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29494 index_rnglists ();
29496 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29497 md5_init_ctx (&ctx);
29498 mark = 0;
29499 die_checksum (comp_unit_die (), &ctx, &mark);
29500 unmark_all_dies (comp_unit_die ());
29501 md5_finish_ctx (&ctx, checksum);
29503 if (dwarf_version < 5)
29505 /* Use the first 8 bytes of the checksum as the dwo_id,
29506 and add it to both comp-unit DIEs. */
29507 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29508 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29511 /* Add the base offset of the ranges table to the skeleton
29512 comp-unit DIE. */
29513 if (!vec_safe_is_empty (ranges_table))
29515 if (dwarf_version >= 5)
29516 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29517 ranges_base_label);
29518 else
29519 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29520 ranges_section_label);
29523 switch_to_section (debug_addr_section);
29524 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29525 output_addr_table ();
29528 /* Output the main compilation unit if non-empty or if .debug_macinfo
29529 or .debug_macro will be emitted. */
29530 output_comp_unit (comp_unit_die (), have_macinfo,
29531 dwarf_split_debug_info ? checksum : NULL);
29533 if (dwarf_split_debug_info && info_section_emitted)
29534 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29536 /* Output the abbreviation table. */
29537 if (vec_safe_length (abbrev_die_table) != 1)
29539 switch_to_section (debug_abbrev_section);
29540 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29541 output_abbrev_section ();
29544 /* Output location list section if necessary. */
29545 if (have_location_lists)
29547 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29548 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29549 /* Output the location lists info. */
29550 switch_to_section (debug_loc_section);
29551 if (dwarf_version >= 5)
29553 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29554 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29555 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29556 dw2_asm_output_data (4, 0xffffffff,
29557 "Initial length escape value indicating "
29558 "64-bit DWARF extension");
29559 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29560 "Length of Location Lists");
29561 ASM_OUTPUT_LABEL (asm_out_file, l1);
29562 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29563 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29564 dw2_asm_output_data (1, 0, "Segment Size");
29565 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29566 "Offset Entry Count");
29568 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29569 if (dwarf_version >= 5 && dwarf_split_debug_info)
29571 unsigned int save_loc_list_idx = loc_list_idx;
29572 loc_list_idx = 0;
29573 output_loclists_offsets (comp_unit_die ());
29574 gcc_assert (save_loc_list_idx == loc_list_idx);
29576 output_location_lists (comp_unit_die ());
29577 if (dwarf_version >= 5)
29578 ASM_OUTPUT_LABEL (asm_out_file, l2);
29581 output_pubtables ();
29583 /* Output the address range information if a CU (.debug_info section)
29584 was emitted. We output an empty table even if we had no functions
29585 to put in it. This because the consumer has no way to tell the
29586 difference between an empty table that we omitted and failure to
29587 generate a table that would have contained data. */
29588 if (info_section_emitted)
29590 switch_to_section (debug_aranges_section);
29591 output_aranges ();
29594 /* Output ranges section if necessary. */
29595 if (!vec_safe_is_empty (ranges_table))
29597 if (dwarf_version >= 5)
29598 output_rnglists ();
29599 else
29600 output_ranges ();
29603 /* Have to end the macro section. */
29604 if (have_macinfo)
29606 switch_to_section (debug_macinfo_section);
29607 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29608 output_macinfo ();
29609 dw2_asm_output_data (1, 0, "End compilation unit");
29612 /* Output the source line correspondence table. We must do this
29613 even if there is no line information. Otherwise, on an empty
29614 translation unit, we will generate a present, but empty,
29615 .debug_info section. IRIX 6.5 `nm' will then complain when
29616 examining the file. This is done late so that any filenames
29617 used by the debug_info section are marked as 'used'. */
29618 switch_to_section (debug_line_section);
29619 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29620 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29621 output_line_info (false);
29623 if (dwarf_split_debug_info && info_section_emitted)
29625 switch_to_section (debug_skeleton_line_section);
29626 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29627 output_line_info (true);
29630 /* If we emitted any indirect strings, output the string table too. */
29631 if (debug_str_hash || skeleton_debug_str_hash)
29632 output_indirect_strings ();
29633 if (debug_line_str_hash)
29635 switch_to_section (debug_line_str_section);
29636 const enum dwarf_form form = DW_FORM_line_strp;
29637 debug_line_str_hash->traverse<enum dwarf_form,
29638 output_indirect_string> (form);
29642 /* Perform any cleanups needed after the early debug generation pass
29643 has run. */
29645 static void
29646 dwarf2out_early_finish (const char *filename)
29648 set_early_dwarf s;
29650 /* PCH might result in DW_AT_producer string being restored from the
29651 header compilation, so always fill it with empty string initially
29652 and overwrite only here. */
29653 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
29654 producer_string = gen_producer_string ();
29655 producer->dw_attr_val.v.val_str->refcount--;
29656 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
29658 /* Add the name for the main input file now. We delayed this from
29659 dwarf2out_init to avoid complications with PCH. */
29660 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
29661 add_comp_dir_attribute (comp_unit_die ());
29663 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
29664 DW_AT_comp_dir into .debug_line_str section. */
29665 if (!DWARF2_ASM_LINE_DEBUG_INFO
29666 && dwarf_version >= 5
29667 && DWARF5_USE_DEBUG_LINE_STR)
29669 for (int i = 0; i < 2; i++)
29671 dw_attr_node *a = get_AT (comp_unit_die (),
29672 i ? DW_AT_comp_dir : DW_AT_name);
29673 if (a == NULL
29674 || AT_class (a) != dw_val_class_str
29675 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
29676 continue;
29678 if (! debug_line_str_hash)
29679 debug_line_str_hash
29680 = hash_table<indirect_string_hasher>::create_ggc (10);
29682 struct indirect_string_node *node
29683 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
29684 set_indirect_string (node);
29685 node->form = DW_FORM_line_strp;
29686 a->dw_attr_val.v.val_str->refcount--;
29687 a->dw_attr_val.v.val_str = node;
29691 /* With LTO early dwarf was really finished at compile-time, so make
29692 sure to adjust the phase after annotating the LTRANS CU DIE. */
29693 if (in_lto_p)
29695 early_dwarf_finished = true;
29696 return;
29699 /* Walk through the list of incomplete types again, trying once more to
29700 emit full debugging info for them. */
29701 retry_incomplete_types ();
29703 /* The point here is to flush out the limbo list so that it is empty
29704 and we don't need to stream it for LTO. */
29705 flush_limbo_die_list ();
29707 gen_scheduled_generic_parms_dies ();
29708 gen_remaining_tmpl_value_param_die_attribute ();
29710 /* Add DW_AT_linkage_name for all deferred DIEs. */
29711 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
29713 tree decl = node->created_for;
29714 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
29715 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
29716 ended up in deferred_asm_name before we knew it was
29717 constant and never written to disk. */
29718 && DECL_ASSEMBLER_NAME (decl))
29720 add_linkage_attr (node->die, decl);
29721 move_linkage_attr (node->die);
29724 deferred_asm_name = NULL;
29726 if (flag_eliminate_unused_debug_types)
29727 prune_unused_types ();
29729 /* Generate separate COMDAT sections for type DIEs. */
29730 if (use_debug_types)
29732 break_out_comdat_types (comp_unit_die ());
29734 /* Each new type_unit DIE was added to the limbo die list when created.
29735 Since these have all been added to comdat_type_list, clear the
29736 limbo die list. */
29737 limbo_die_list = NULL;
29739 /* For each new comdat type unit, copy declarations for incomplete
29740 types to make the new unit self-contained (i.e., no direct
29741 references to the main compile unit). */
29742 for (comdat_type_node *ctnode = comdat_type_list;
29743 ctnode != NULL; ctnode = ctnode->next)
29744 copy_decls_for_unworthy_types (ctnode->root_die);
29745 copy_decls_for_unworthy_types (comp_unit_die ());
29747 /* In the process of copying declarations from one unit to another,
29748 we may have left some declarations behind that are no longer
29749 referenced. Prune them. */
29750 prune_unused_types ();
29753 /* Generate separate CUs for each of the include files we've seen.
29754 They will go into limbo_die_list and from there to cu_die_list. */
29755 if (flag_eliminate_dwarf2_dups)
29757 gcc_assert (limbo_die_list == NULL);
29758 break_out_includes (comp_unit_die ());
29759 limbo_die_node *cu;
29760 while ((cu = limbo_die_list))
29762 limbo_die_list = cu->next;
29763 cu->next = cu_die_list;
29764 cu_die_list = cu;
29768 /* The early debug phase is now finished. */
29769 early_dwarf_finished = true;
29772 /* Reset all state within dwarf2out.c so that we can rerun the compiler
29773 within the same process. For use by toplev::finalize. */
29775 void
29776 dwarf2out_c_finalize (void)
29778 last_var_location_insn = NULL;
29779 cached_next_real_insn = NULL;
29780 used_rtx_array = NULL;
29781 incomplete_types = NULL;
29782 decl_scope_table = NULL;
29783 debug_info_section = NULL;
29784 debug_skeleton_info_section = NULL;
29785 debug_abbrev_section = NULL;
29786 debug_skeleton_abbrev_section = NULL;
29787 debug_aranges_section = NULL;
29788 debug_addr_section = NULL;
29789 debug_macinfo_section = NULL;
29790 debug_line_section = NULL;
29791 debug_skeleton_line_section = NULL;
29792 debug_loc_section = NULL;
29793 debug_pubnames_section = NULL;
29794 debug_pubtypes_section = NULL;
29795 debug_str_section = NULL;
29796 debug_line_str_section = NULL;
29797 debug_str_dwo_section = NULL;
29798 debug_str_offsets_section = NULL;
29799 debug_ranges_section = NULL;
29800 debug_frame_section = NULL;
29801 fde_vec = NULL;
29802 debug_str_hash = NULL;
29803 debug_line_str_hash = NULL;
29804 skeleton_debug_str_hash = NULL;
29805 dw2_string_counter = 0;
29806 have_multiple_function_sections = false;
29807 text_section_used = false;
29808 cold_text_section_used = false;
29809 cold_text_section = NULL;
29810 current_unit_personality = NULL;
29812 next_die_offset = 0;
29813 single_comp_unit_die = NULL;
29814 comdat_type_list = NULL;
29815 limbo_die_list = NULL;
29816 file_table = NULL;
29817 decl_die_table = NULL;
29818 common_block_die_table = NULL;
29819 decl_loc_table = NULL;
29820 call_arg_locations = NULL;
29821 call_arg_loc_last = NULL;
29822 call_site_count = -1;
29823 tail_call_site_count = -1;
29824 cached_dw_loc_list_table = NULL;
29825 abbrev_die_table = NULL;
29826 delete dwarf_proc_stack_usage_map;
29827 dwarf_proc_stack_usage_map = NULL;
29828 line_info_label_num = 0;
29829 cur_line_info_table = NULL;
29830 text_section_line_info = NULL;
29831 cold_text_section_line_info = NULL;
29832 separate_line_info = NULL;
29833 info_section_emitted = false;
29834 pubname_table = NULL;
29835 pubtype_table = NULL;
29836 macinfo_table = NULL;
29837 ranges_table = NULL;
29838 ranges_by_label = NULL;
29839 rnglist_idx = 0;
29840 have_location_lists = false;
29841 loclabel_num = 0;
29842 poc_label_num = 0;
29843 last_emitted_file = NULL;
29844 label_num = 0;
29845 tmpl_value_parm_die_table = NULL;
29846 generic_type_instances = NULL;
29847 frame_pointer_fb_offset = 0;
29848 frame_pointer_fb_offset_valid = false;
29849 base_types.release ();
29850 XDELETEVEC (producer_string);
29851 producer_string = NULL;
29854 #include "gt-dwarf2out.h"