Fix native windows build by adding signal.h back into the include list.
[official-gcc.git] / gcc / dwarf2out.c
blob1dfff38a8624685c4da1c873460f50aabb9cd9d3
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 #ifdef HAVE_AS_LEB128
9554 const char *last_section = NULL;
9555 const char *base_label = NULL;
9556 #endif
9558 /* Walk the location list, and output each range + expression. */
9559 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9561 unsigned long size;
9562 /* Don't output an entry that starts and ends at the same address. */
9563 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9564 continue;
9565 size = size_of_locs (curr->expr);
9566 /* If the expression is too large, drop it on the floor. We could
9567 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9568 in the expression, but >= 64KB expressions for a single value
9569 in a single range are unlikely very useful. */
9570 if (size > 0xffff)
9571 continue;
9572 if (dwarf_version >= 5)
9574 if (dwarf_split_debug_info)
9576 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9577 uleb128 index into .debug_addr and uleb128 length. */
9578 dw2_asm_output_data (1, DW_LLE_startx_length,
9579 "DW_LLE_startx_length (%s)",
9580 list_head->ll_symbol);
9581 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9582 "Location list range start index "
9583 "(%s)", curr->begin);
9584 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9585 For that case we probably need to emit DW_LLE_startx_endx,
9586 but we'd need 2 .debug_addr entries rather than just one. */
9587 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9588 "Location list length (%s)",
9589 list_head->ll_symbol);
9591 #ifdef HAVE_AS_LEB128
9592 else if (!have_multiple_function_sections)
9594 /* If all code is in .text section, the base address is
9595 already provided by the CU attributes. Use
9596 DW_LLE_offset_pair where both addresses are uleb128 encoded
9597 offsets against that base. */
9598 dw2_asm_output_data (1, DW_LLE_offset_pair,
9599 "DW_LLE_offset_pair (%s)",
9600 list_head->ll_symbol);
9601 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9602 "Location list begin address (%s)",
9603 list_head->ll_symbol);
9604 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9605 "Location list end address (%s)",
9606 list_head->ll_symbol);
9608 else
9610 /* Otherwise, find out how many consecutive entries could share
9611 the same base entry. If just one, emit DW_LLE_start_length,
9612 otherwise emit DW_LLE_base_address for the base address
9613 followed by a series of DW_LLE_offset_pair. */
9614 if (last_section == NULL || curr->section != last_section)
9616 dw_loc_list_ref curr2;
9617 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9618 curr2 = curr2->dw_loc_next)
9620 if (strcmp (curr2->begin, curr2->end) == 0
9621 && !curr2->force)
9622 continue;
9623 if ((unsigned long) size_of_locs (curr2->expr) > 0xffff)
9624 continue;
9625 break;
9627 if (curr2 == NULL || curr->section != curr2->section)
9628 last_section = NULL;
9629 else
9631 last_section = curr->section;
9632 base_label = curr->begin;
9633 dw2_asm_output_data (1, DW_LLE_base_address,
9634 "DW_LLE_base_address (%s)",
9635 list_head->ll_symbol);
9636 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9637 "Base address (%s)",
9638 list_head->ll_symbol);
9641 /* Only one entry with the same base address. Use
9642 DW_LLE_start_length with absolute address and uleb128
9643 length. */
9644 if (last_section == NULL)
9646 dw2_asm_output_data (1, DW_LLE_start_length,
9647 "DW_LLE_start_length (%s)",
9648 list_head->ll_symbol);
9649 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9650 "Location list begin address (%s)",
9651 list_head->ll_symbol);
9652 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9653 "Location list length "
9654 "(%s)", list_head->ll_symbol);
9656 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9657 DW_LLE_base_address. */
9658 else
9660 dw2_asm_output_data (1, DW_LLE_offset_pair,
9661 "DW_LLE_offset_pair (%s)",
9662 list_head->ll_symbol);
9663 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9664 "Location list begin address "
9665 "(%s)", list_head->ll_symbol);
9666 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9667 "Location list end address "
9668 "(%s)", list_head->ll_symbol);
9671 #else
9672 /* The assembler does not support .uleb128 directive. Emit
9673 DW_LLE_start_end with a pair of absolute addresses. */
9674 else
9676 dw2_asm_output_data (1, DW_LLE_start_end,
9677 "DW_LLE_start_end (%s)",
9678 list_head->ll_symbol);
9679 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9680 "Location list begin address (%s)",
9681 list_head->ll_symbol);
9682 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9683 "Location list end address (%s)",
9684 list_head->ll_symbol);
9686 #endif
9688 else if (dwarf_split_debug_info)
9690 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9691 and 4 byte length. */
9692 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9693 "Location list start/length entry (%s)",
9694 list_head->ll_symbol);
9695 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9696 "Location list range start index (%s)",
9697 curr->begin);
9698 /* The length field is 4 bytes. If we ever need to support
9699 an 8-byte length, we can add a new DW_LLE code or fall back
9700 to DW_LLE_GNU_start_end_entry. */
9701 dw2_asm_output_delta (4, curr->end, curr->begin,
9702 "Location list range length (%s)",
9703 list_head->ll_symbol);
9705 else if (!have_multiple_function_sections)
9707 /* Pair of relative addresses against start of text section. */
9708 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9709 "Location list begin address (%s)",
9710 list_head->ll_symbol);
9711 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9712 "Location list end address (%s)",
9713 list_head->ll_symbol);
9715 else
9717 /* Pair of absolute addresses. */
9718 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9719 "Location list begin address (%s)",
9720 list_head->ll_symbol);
9721 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9722 "Location list end address (%s)",
9723 list_head->ll_symbol);
9726 /* Output the block length for this list of location operations. */
9727 gcc_assert (size <= 0xffff);
9728 dw2_asm_output_data (2, size, "%s", "Location expression size");
9730 output_loc_sequence (curr->expr, -1);
9733 /* And finally list termination. */
9734 if (dwarf_version >= 5)
9735 dw2_asm_output_data (1, DW_LLE_end_of_list,
9736 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9737 else if (dwarf_split_debug_info)
9738 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9739 "Location list terminator (%s)",
9740 list_head->ll_symbol);
9741 else
9743 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9744 "Location list terminator begin (%s)",
9745 list_head->ll_symbol);
9746 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9747 "Location list terminator end (%s)",
9748 list_head->ll_symbol);
9752 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9753 section. Emit a relocated reference if val_entry is NULL, otherwise,
9754 emit an indirect reference. */
9756 static void
9757 output_range_list_offset (dw_attr_node *a)
9759 const char *name = dwarf_attr_name (a->dw_attr);
9761 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9763 if (dwarf_version >= 5)
9765 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9766 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9767 debug_ranges_section, "%s", name);
9769 else
9771 char *p = strchr (ranges_section_label, '\0');
9772 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9773 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9774 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9775 debug_ranges_section, "%s", name);
9776 *p = '\0';
9779 else if (dwarf_version >= 5)
9781 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9782 gcc_assert (rnglist_idx);
9783 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9785 else
9786 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9787 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9788 "%s (offset from %s)", name, ranges_section_label);
9791 /* Output the offset into the debug_loc section. */
9793 static void
9794 output_loc_list_offset (dw_attr_node *a)
9796 char *sym = AT_loc_list (a)->ll_symbol;
9798 gcc_assert (sym);
9799 if (!dwarf_split_debug_info)
9800 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9801 "%s", dwarf_attr_name (a->dw_attr));
9802 else if (dwarf_version >= 5)
9804 gcc_assert (AT_loc_list (a)->num_assigned);
9805 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9806 dwarf_attr_name (a->dw_attr),
9807 sym);
9809 else
9810 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9811 "%s", dwarf_attr_name (a->dw_attr));
9814 /* Output an attribute's index or value appropriately. */
9816 static void
9817 output_attr_index_or_value (dw_attr_node *a)
9819 const char *name = dwarf_attr_name (a->dw_attr);
9821 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9823 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9824 return;
9826 switch (AT_class (a))
9828 case dw_val_class_addr:
9829 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9830 break;
9831 case dw_val_class_high_pc:
9832 case dw_val_class_lbl_id:
9833 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9834 break;
9835 default:
9836 gcc_unreachable ();
9840 /* Output a type signature. */
9842 static inline void
9843 output_signature (const char *sig, const char *name)
9845 int i;
9847 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9848 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9851 /* Output a discriminant value. */
9853 static inline void
9854 output_discr_value (dw_discr_value *discr_value, const char *name)
9856 if (discr_value->pos)
9857 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9858 else
9859 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9862 /* Output the DIE and its attributes. Called recursively to generate
9863 the definitions of each child DIE. */
9865 static void
9866 output_die (dw_die_ref die)
9868 dw_attr_node *a;
9869 dw_die_ref c;
9870 unsigned long size;
9871 unsigned ix;
9873 /* If someone in another CU might refer to us, set up a symbol for
9874 them to point to. */
9875 if (! die->comdat_type_p && die->die_id.die_symbol)
9876 output_die_symbol (die);
9878 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9879 (unsigned long)die->die_offset,
9880 dwarf_tag_name (die->die_tag));
9882 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9884 const char *name = dwarf_attr_name (a->dw_attr);
9886 switch (AT_class (a))
9888 case dw_val_class_addr:
9889 output_attr_index_or_value (a);
9890 break;
9892 case dw_val_class_offset:
9893 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9894 "%s", name);
9895 break;
9897 case dw_val_class_range_list:
9898 output_range_list_offset (a);
9899 break;
9901 case dw_val_class_loc:
9902 size = size_of_locs (AT_loc (a));
9904 /* Output the block length for this list of location operations. */
9905 if (dwarf_version >= 4)
9906 dw2_asm_output_data_uleb128 (size, "%s", name);
9907 else
9908 dw2_asm_output_data (constant_size (size), size, "%s", name);
9910 output_loc_sequence (AT_loc (a), -1);
9911 break;
9913 case dw_val_class_const:
9914 /* ??? It would be slightly more efficient to use a scheme like is
9915 used for unsigned constants below, but gdb 4.x does not sign
9916 extend. Gdb 5.x does sign extend. */
9917 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9918 break;
9920 case dw_val_class_unsigned_const:
9922 int csize = constant_size (AT_unsigned (a));
9923 if (dwarf_version == 3
9924 && a->dw_attr == DW_AT_data_member_location
9925 && csize >= 4)
9926 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9927 else
9928 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9930 break;
9932 case dw_val_class_const_implicit:
9933 if (flag_debug_asm)
9934 fprintf (asm_out_file, "\t\t\t%s %s ("
9935 HOST_WIDE_INT_PRINT_DEC ")\n",
9936 ASM_COMMENT_START, name, AT_int (a));
9937 break;
9939 case dw_val_class_unsigned_const_implicit:
9940 if (flag_debug_asm)
9941 fprintf (asm_out_file, "\t\t\t%s %s ("
9942 HOST_WIDE_INT_PRINT_HEX ")\n",
9943 ASM_COMMENT_START, name, AT_unsigned (a));
9944 break;
9946 case dw_val_class_const_double:
9948 unsigned HOST_WIDE_INT first, second;
9950 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9951 dw2_asm_output_data (1,
9952 HOST_BITS_PER_DOUBLE_INT
9953 / HOST_BITS_PER_CHAR,
9954 NULL);
9956 if (WORDS_BIG_ENDIAN)
9958 first = a->dw_attr_val.v.val_double.high;
9959 second = a->dw_attr_val.v.val_double.low;
9961 else
9963 first = a->dw_attr_val.v.val_double.low;
9964 second = a->dw_attr_val.v.val_double.high;
9967 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9968 first, "%s", name);
9969 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9970 second, NULL);
9972 break;
9974 case dw_val_class_wide_int:
9976 int i;
9977 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9978 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9979 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9980 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
9981 * l, NULL);
9983 if (WORDS_BIG_ENDIAN)
9984 for (i = len - 1; i >= 0; --i)
9986 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9987 "%s", name);
9988 name = "";
9990 else
9991 for (i = 0; i < len; ++i)
9993 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9994 "%s", name);
9995 name = "";
9998 break;
10000 case dw_val_class_vec:
10002 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10003 unsigned int len = a->dw_attr_val.v.val_vec.length;
10004 unsigned int i;
10005 unsigned char *p;
10007 dw2_asm_output_data (constant_size (len * elt_size),
10008 len * elt_size, "%s", name);
10009 if (elt_size > sizeof (HOST_WIDE_INT))
10011 elt_size /= 2;
10012 len *= 2;
10014 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10015 i < len;
10016 i++, p += elt_size)
10017 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10018 "fp or vector constant word %u", i);
10019 break;
10022 case dw_val_class_flag:
10023 if (dwarf_version >= 4)
10025 /* Currently all add_AT_flag calls pass in 1 as last argument,
10026 so DW_FORM_flag_present can be used. If that ever changes,
10027 we'll need to use DW_FORM_flag and have some optimization
10028 in build_abbrev_table that will change those to
10029 DW_FORM_flag_present if it is set to 1 in all DIEs using
10030 the same abbrev entry. */
10031 gcc_assert (AT_flag (a) == 1);
10032 if (flag_debug_asm)
10033 fprintf (asm_out_file, "\t\t\t%s %s\n",
10034 ASM_COMMENT_START, name);
10035 break;
10037 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10038 break;
10040 case dw_val_class_loc_list:
10041 output_loc_list_offset (a);
10042 break;
10044 case dw_val_class_die_ref:
10045 if (AT_ref_external (a))
10047 if (AT_ref (a)->comdat_type_p)
10049 comdat_type_node *type_node
10050 = AT_ref (a)->die_id.die_type_node;
10052 gcc_assert (type_node);
10053 output_signature (type_node->signature, name);
10055 else
10057 const char *sym = AT_ref (a)->die_id.die_symbol;
10058 int size;
10060 gcc_assert (sym);
10061 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10062 length, whereas in DWARF3 it's always sized as an
10063 offset. */
10064 if (dwarf_version == 2)
10065 size = DWARF2_ADDR_SIZE;
10066 else
10067 size = DWARF_OFFSET_SIZE;
10068 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10069 name);
10072 else
10074 gcc_assert (AT_ref (a)->die_offset);
10075 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10076 "%s", name);
10078 break;
10080 case dw_val_class_fde_ref:
10082 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10084 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10085 a->dw_attr_val.v.val_fde_index * 2);
10086 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10087 "%s", name);
10089 break;
10091 case dw_val_class_vms_delta:
10092 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10093 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10094 AT_vms_delta2 (a), AT_vms_delta1 (a),
10095 "%s", name);
10096 #else
10097 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10098 AT_vms_delta2 (a), AT_vms_delta1 (a),
10099 "%s", name);
10100 #endif
10101 break;
10103 case dw_val_class_lbl_id:
10104 output_attr_index_or_value (a);
10105 break;
10107 case dw_val_class_lineptr:
10108 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10109 debug_line_section, "%s", name);
10110 break;
10112 case dw_val_class_macptr:
10113 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10114 debug_macinfo_section, "%s", name);
10115 break;
10117 case dw_val_class_loclistsptr:
10118 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10119 debug_loc_section, "%s", name);
10120 break;
10122 case dw_val_class_str:
10123 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10124 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10125 a->dw_attr_val.v.val_str->label,
10126 debug_str_section,
10127 "%s: \"%s\"", name, AT_string (a));
10128 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10129 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10130 a->dw_attr_val.v.val_str->label,
10131 debug_line_str_section,
10132 "%s: \"%s\"", name, AT_string (a));
10133 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10134 dw2_asm_output_data_uleb128 (AT_index (a),
10135 "%s: \"%s\"", name, AT_string (a));
10136 else
10137 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10138 break;
10140 case dw_val_class_file:
10142 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10144 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10145 a->dw_attr_val.v.val_file->filename);
10146 break;
10149 case dw_val_class_file_implicit:
10150 if (flag_debug_asm)
10151 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10152 ASM_COMMENT_START, name,
10153 maybe_emit_file (a->dw_attr_val.v.val_file),
10154 a->dw_attr_val.v.val_file->filename);
10155 break;
10157 case dw_val_class_data8:
10159 int i;
10161 for (i = 0; i < 8; i++)
10162 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10163 i == 0 ? "%s" : NULL, name);
10164 break;
10167 case dw_val_class_high_pc:
10168 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10169 get_AT_low_pc (die), "DW_AT_high_pc");
10170 break;
10172 case dw_val_class_discr_value:
10173 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10174 break;
10176 case dw_val_class_discr_list:
10178 dw_discr_list_ref list = AT_discr_list (a);
10179 const int size = size_of_discr_list (list);
10181 /* This is a block, so output its length first. */
10182 dw2_asm_output_data (constant_size (size), size,
10183 "%s: block size", name);
10185 for (; list != NULL; list = list->dw_discr_next)
10187 /* One byte for the discriminant value descriptor, and then as
10188 many LEB128 numbers as required. */
10189 if (list->dw_discr_range)
10190 dw2_asm_output_data (1, DW_DSC_range,
10191 "%s: DW_DSC_range", name);
10192 else
10193 dw2_asm_output_data (1, DW_DSC_label,
10194 "%s: DW_DSC_label", name);
10196 output_discr_value (&list->dw_discr_lower_bound, name);
10197 if (list->dw_discr_range)
10198 output_discr_value (&list->dw_discr_upper_bound, name);
10200 break;
10203 default:
10204 gcc_unreachable ();
10208 FOR_EACH_CHILD (die, c, output_die (c));
10210 /* Add null byte to terminate sibling list. */
10211 if (die->die_child != NULL)
10212 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10213 (unsigned long) die->die_offset);
10216 /* Output the compilation unit that appears at the beginning of the
10217 .debug_info section, and precedes the DIE descriptions. */
10219 static void
10220 output_compilation_unit_header (enum dwarf_unit_type ut)
10222 if (!XCOFF_DEBUGGING_INFO)
10224 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10225 dw2_asm_output_data (4, 0xffffffff,
10226 "Initial length escape value indicating 64-bit DWARF extension");
10227 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10228 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10229 "Length of Compilation Unit Info");
10232 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10233 if (dwarf_version >= 5)
10235 const char *name;
10236 switch (ut)
10238 case DW_UT_compile: name = "DW_UT_compile"; break;
10239 case DW_UT_type: name = "DW_UT_type"; break;
10240 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10241 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10242 default: gcc_unreachable ();
10244 dw2_asm_output_data (1, ut, name);
10245 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10247 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10248 debug_abbrev_section,
10249 "Offset Into Abbrev. Section");
10250 if (dwarf_version < 5)
10251 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10254 /* Output the compilation unit DIE and its children. */
10256 static void
10257 output_comp_unit (dw_die_ref die, int output_if_empty,
10258 const unsigned char *dwo_id)
10260 const char *secname, *oldsym;
10261 char *tmp;
10263 /* Unless we are outputting main CU, we may throw away empty ones. */
10264 if (!output_if_empty && die->die_child == NULL)
10265 return;
10267 /* Even if there are no children of this DIE, we must output the information
10268 about the compilation unit. Otherwise, on an empty translation unit, we
10269 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10270 will then complain when examining the file. First mark all the DIEs in
10271 this CU so we know which get local refs. */
10272 mark_dies (die);
10274 external_ref_hash_type *extern_map = optimize_external_refs (die);
10276 /* For now, optimize only the main CU, in order to optimize the rest
10277 we'd need to see all of them earlier. Leave the rest for post-linking
10278 tools like DWZ. */
10279 if (die == comp_unit_die ())
10280 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10282 build_abbrev_table (die, extern_map);
10284 optimize_abbrev_table ();
10286 delete extern_map;
10288 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10289 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
10290 calc_die_sizes (die);
10292 oldsym = die->die_id.die_symbol;
10293 if (oldsym)
10295 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10297 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10298 secname = tmp;
10299 die->die_id.die_symbol = NULL;
10300 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10302 else
10304 switch_to_section (debug_info_section);
10305 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10306 info_section_emitted = true;
10309 /* Output debugging information. */
10310 output_compilation_unit_header (dwo_id
10311 ? DW_UT_split_compile : DW_UT_compile);
10312 if (dwarf_version >= 5)
10314 if (dwo_id != NULL)
10315 for (int i = 0; i < 8; i++)
10316 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10317 else
10318 /* Hope all the padding will be removed for DWARF 5 final for
10319 DW_AT_compile and DW_AT_partial. */
10320 dw2_asm_output_data (8, 0, "Padding 1");
10322 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Padding 2");
10324 output_die (die);
10326 /* Leave the marks on the main CU, so we can check them in
10327 output_pubnames. */
10328 if (oldsym)
10330 unmark_dies (die);
10331 die->die_id.die_symbol = oldsym;
10335 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10336 and .debug_pubtypes. This is configured per-target, but can be
10337 overridden by the -gpubnames or -gno-pubnames options. */
10339 static inline bool
10340 want_pubnames (void)
10342 if (debug_info_level <= DINFO_LEVEL_TERSE)
10343 return false;
10344 if (debug_generate_pub_sections != -1)
10345 return debug_generate_pub_sections;
10346 return targetm.want_debug_pub_sections;
10349 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10351 static void
10352 add_AT_pubnames (dw_die_ref die)
10354 if (want_pubnames ())
10355 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10358 /* Add a string attribute value to a skeleton DIE. */
10360 static inline void
10361 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10362 const char *str)
10364 dw_attr_node attr;
10365 struct indirect_string_node *node;
10367 if (! skeleton_debug_str_hash)
10368 skeleton_debug_str_hash
10369 = hash_table<indirect_string_hasher>::create_ggc (10);
10371 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10372 find_string_form (node);
10373 if (node->form == DW_FORM_GNU_str_index)
10374 node->form = DW_FORM_strp;
10376 attr.dw_attr = attr_kind;
10377 attr.dw_attr_val.val_class = dw_val_class_str;
10378 attr.dw_attr_val.val_entry = NULL;
10379 attr.dw_attr_val.v.val_str = node;
10380 add_dwarf_attr (die, &attr);
10383 /* Helper function to generate top-level dies for skeleton debug_info and
10384 debug_types. */
10386 static void
10387 add_top_level_skeleton_die_attrs (dw_die_ref die)
10389 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10390 const char *comp_dir = comp_dir_string ();
10392 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10393 if (comp_dir != NULL)
10394 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10395 add_AT_pubnames (die);
10396 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10399 /* Output skeleton debug sections that point to the dwo file. */
10401 static void
10402 output_skeleton_debug_sections (dw_die_ref comp_unit,
10403 const unsigned char *dwo_id)
10405 /* These attributes will be found in the full debug_info section. */
10406 remove_AT (comp_unit, DW_AT_producer);
10407 remove_AT (comp_unit, DW_AT_language);
10409 switch_to_section (debug_skeleton_info_section);
10410 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10412 /* Produce the skeleton compilation-unit header. This one differs enough from
10413 a normal CU header that it's better not to call output_compilation_unit
10414 header. */
10415 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10416 dw2_asm_output_data (4, 0xffffffff,
10417 "Initial length escape value indicating 64-bit DWARF extension");
10419 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10420 DWARF_COMPILE_UNIT_HEADER_SIZE
10421 - DWARF_INITIAL_LENGTH_SIZE
10422 + size_of_die (comp_unit),
10423 "Length of Compilation Unit Info");
10424 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10425 if (dwarf_version >= 5)
10427 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10428 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10430 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10431 debug_skeleton_abbrev_section,
10432 "Offset Into Abbrev. Section");
10433 if (dwarf_version < 5)
10434 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10435 else
10437 for (int i = 0; i < 8; i++)
10438 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10440 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Padding 2");
10443 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10444 output_die (comp_unit);
10446 /* Build the skeleton debug_abbrev section. */
10447 switch_to_section (debug_skeleton_abbrev_section);
10448 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10450 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10452 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10455 /* Output a comdat type unit DIE and its children. */
10457 static void
10458 output_comdat_type_unit (comdat_type_node *node)
10460 const char *secname;
10461 char *tmp;
10462 int i;
10463 #if defined (OBJECT_FORMAT_ELF)
10464 tree comdat_key;
10465 #endif
10467 /* First mark all the DIEs in this CU so we know which get local refs. */
10468 mark_dies (node->root_die);
10470 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10472 build_abbrev_table (node->root_die, extern_map);
10474 delete extern_map;
10475 extern_map = NULL;
10477 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10478 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10479 calc_die_sizes (node->root_die);
10481 #if defined (OBJECT_FORMAT_ELF)
10482 if (dwarf_version >= 5)
10484 if (!dwarf_split_debug_info)
10485 secname = ".debug_info";
10486 else
10487 secname = ".debug_info.dwo";
10489 else if (!dwarf_split_debug_info)
10490 secname = ".debug_types";
10491 else
10492 secname = ".debug_types.dwo";
10494 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10495 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10496 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10497 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10498 comdat_key = get_identifier (tmp);
10499 targetm.asm_out.named_section (secname,
10500 SECTION_DEBUG | SECTION_LINKONCE,
10501 comdat_key);
10502 #else
10503 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10504 sprintf (tmp, (dwarf_version >= 5
10505 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10506 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10507 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10508 secname = tmp;
10509 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10510 #endif
10512 /* Output debugging information. */
10513 output_compilation_unit_header (dwarf_split_debug_info
10514 ? DW_UT_split_type : DW_UT_type);
10515 output_signature (node->signature, "Type Signature");
10516 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10517 "Offset to Type DIE");
10518 output_die (node->root_die);
10520 unmark_dies (node->root_die);
10523 /* Return the DWARF2/3 pubname associated with a decl. */
10525 static const char *
10526 dwarf2_name (tree decl, int scope)
10528 if (DECL_NAMELESS (decl))
10529 return NULL;
10530 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10533 /* Add a new entry to .debug_pubnames if appropriate. */
10535 static void
10536 add_pubname_string (const char *str, dw_die_ref die)
10538 pubname_entry e;
10540 e.die = die;
10541 e.name = xstrdup (str);
10542 vec_safe_push (pubname_table, e);
10545 static void
10546 add_pubname (tree decl, dw_die_ref die)
10548 if (!want_pubnames ())
10549 return;
10551 /* Don't add items to the table when we expect that the consumer will have
10552 just read the enclosing die. For example, if the consumer is looking at a
10553 class_member, it will either be inside the class already, or will have just
10554 looked up the class to find the member. Either way, searching the class is
10555 faster than searching the index. */
10556 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10557 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10559 const char *name = dwarf2_name (decl, 1);
10561 if (name)
10562 add_pubname_string (name, die);
10566 /* Add an enumerator to the pubnames section. */
10568 static void
10569 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10571 pubname_entry e;
10573 gcc_assert (scope_name);
10574 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10575 e.die = die;
10576 vec_safe_push (pubname_table, e);
10579 /* Add a new entry to .debug_pubtypes if appropriate. */
10581 static void
10582 add_pubtype (tree decl, dw_die_ref die)
10584 pubname_entry e;
10586 if (!want_pubnames ())
10587 return;
10589 if ((TREE_PUBLIC (decl)
10590 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10591 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10593 tree scope = NULL;
10594 const char *scope_name = "";
10595 const char *sep = is_cxx () ? "::" : ".";
10596 const char *name;
10598 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10599 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10601 scope_name = lang_hooks.dwarf_name (scope, 1);
10602 if (scope_name != NULL && scope_name[0] != '\0')
10603 scope_name = concat (scope_name, sep, NULL);
10604 else
10605 scope_name = "";
10608 if (TYPE_P (decl))
10609 name = type_tag (decl);
10610 else
10611 name = lang_hooks.dwarf_name (decl, 1);
10613 /* If we don't have a name for the type, there's no point in adding
10614 it to the table. */
10615 if (name != NULL && name[0] != '\0')
10617 e.die = die;
10618 e.name = concat (scope_name, name, NULL);
10619 vec_safe_push (pubtype_table, e);
10622 /* Although it might be more consistent to add the pubinfo for the
10623 enumerators as their dies are created, they should only be added if the
10624 enum type meets the criteria above. So rather than re-check the parent
10625 enum type whenever an enumerator die is created, just output them all
10626 here. This isn't protected by the name conditional because anonymous
10627 enums don't have names. */
10628 if (die->die_tag == DW_TAG_enumeration_type)
10630 dw_die_ref c;
10632 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10637 /* Output a single entry in the pubnames table. */
10639 static void
10640 output_pubname (dw_offset die_offset, pubname_entry *entry)
10642 dw_die_ref die = entry->die;
10643 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10645 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10647 if (debug_generate_pub_sections == 2)
10649 /* This logic follows gdb's method for determining the value of the flag
10650 byte. */
10651 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10652 switch (die->die_tag)
10654 case DW_TAG_typedef:
10655 case DW_TAG_base_type:
10656 case DW_TAG_subrange_type:
10657 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10658 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10659 break;
10660 case DW_TAG_enumerator:
10661 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10662 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10663 if (!is_cxx () && !is_java ())
10664 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10665 break;
10666 case DW_TAG_subprogram:
10667 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10668 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10669 if (!is_ada ())
10670 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10671 break;
10672 case DW_TAG_constant:
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_variable:
10678 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10679 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10680 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10681 break;
10682 case DW_TAG_namespace:
10683 case DW_TAG_imported_declaration:
10684 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10685 break;
10686 case DW_TAG_class_type:
10687 case DW_TAG_interface_type:
10688 case DW_TAG_structure_type:
10689 case DW_TAG_union_type:
10690 case DW_TAG_enumeration_type:
10691 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10692 if (!is_cxx () && !is_java ())
10693 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10694 break;
10695 default:
10696 /* An unusual tag. Leave the flag-byte empty. */
10697 break;
10699 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10700 "GDB-index flags");
10703 dw2_asm_output_nstring (entry->name, -1, "external name");
10707 /* Output the public names table used to speed up access to externally
10708 visible names; or the public types table used to find type definitions. */
10710 static void
10711 output_pubnames (vec<pubname_entry, va_gc> *names)
10713 unsigned i;
10714 unsigned long pubnames_length = size_of_pubnames (names);
10715 pubname_entry *pub;
10717 if (!XCOFF_DEBUGGING_INFO)
10719 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10720 dw2_asm_output_data (4, 0xffffffff,
10721 "Initial length escape value indicating 64-bit DWARF extension");
10722 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10723 "Pub Info Length");
10726 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10727 dw2_asm_output_data (2, 2, "DWARF Version");
10729 if (dwarf_split_debug_info)
10730 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10731 debug_skeleton_info_section,
10732 "Offset of Compilation Unit Info");
10733 else
10734 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10735 debug_info_section,
10736 "Offset of Compilation Unit Info");
10737 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10738 "Compilation Unit Length");
10740 FOR_EACH_VEC_ELT (*names, i, pub)
10742 if (include_pubname_in_output (names, pub))
10744 dw_offset die_offset = pub->die->die_offset;
10746 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10747 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10748 gcc_assert (pub->die->die_mark);
10750 /* If we're putting types in their own .debug_types sections,
10751 the .debug_pubtypes table will still point to the compile
10752 unit (not the type unit), so we want to use the offset of
10753 the skeleton DIE (if there is one). */
10754 if (pub->die->comdat_type_p && names == pubtype_table)
10756 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10758 if (type_node != NULL)
10759 die_offset = (type_node->skeleton_die != NULL
10760 ? type_node->skeleton_die->die_offset
10761 : comp_unit_die ()->die_offset);
10764 output_pubname (die_offset, pub);
10768 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10771 /* Output public names and types tables if necessary. */
10773 static void
10774 output_pubtables (void)
10776 if (!want_pubnames () || !info_section_emitted)
10777 return;
10779 switch_to_section (debug_pubnames_section);
10780 output_pubnames (pubname_table);
10781 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10782 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10783 simply won't look for the section. */
10784 switch_to_section (debug_pubtypes_section);
10785 output_pubnames (pubtype_table);
10789 /* Output the information that goes into the .debug_aranges table.
10790 Namely, define the beginning and ending address range of the
10791 text section generated for this compilation unit. */
10793 static void
10794 output_aranges (void)
10796 unsigned i;
10797 unsigned long aranges_length = size_of_aranges ();
10799 if (!XCOFF_DEBUGGING_INFO)
10801 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10802 dw2_asm_output_data (4, 0xffffffff,
10803 "Initial length escape value indicating 64-bit DWARF extension");
10804 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10805 "Length of Address Ranges Info");
10808 /* Version number for aranges is still 2, even up to DWARF5. */
10809 dw2_asm_output_data (2, 2, "DWARF Version");
10810 if (dwarf_split_debug_info)
10811 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10812 debug_skeleton_info_section,
10813 "Offset of Compilation Unit Info");
10814 else
10815 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10816 debug_info_section,
10817 "Offset of Compilation Unit Info");
10818 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10819 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10821 /* We need to align to twice the pointer size here. */
10822 if (DWARF_ARANGES_PAD_SIZE)
10824 /* Pad using a 2 byte words so that padding is correct for any
10825 pointer size. */
10826 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10827 2 * DWARF2_ADDR_SIZE);
10828 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10829 dw2_asm_output_data (2, 0, NULL);
10832 /* It is necessary not to output these entries if the sections were
10833 not used; if the sections were not used, the length will be 0 and
10834 the address may end up as 0 if the section is discarded by ld
10835 --gc-sections, leaving an invalid (0, 0) entry that can be
10836 confused with the terminator. */
10837 if (text_section_used)
10839 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10840 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10841 text_section_label, "Length");
10843 if (cold_text_section_used)
10845 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10846 "Address");
10847 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10848 cold_text_section_label, "Length");
10851 if (have_multiple_function_sections)
10853 unsigned fde_idx;
10854 dw_fde_ref fde;
10856 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10858 if (DECL_IGNORED_P (fde->decl))
10859 continue;
10860 if (!fde->in_std_section)
10862 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10863 "Address");
10864 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10865 fde->dw_fde_begin, "Length");
10867 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10869 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10870 "Address");
10871 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10872 fde->dw_fde_second_begin, "Length");
10877 /* Output the terminator words. */
10878 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10879 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10882 /* Add a new entry to .debug_ranges. Return its index into
10883 ranges_table vector. */
10885 static unsigned int
10886 add_ranges_num (int num, bool maybe_new_sec)
10888 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10889 vec_safe_push (ranges_table, r);
10890 return vec_safe_length (ranges_table) - 1;
10893 /* Add a new entry to .debug_ranges corresponding to a block, or a
10894 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10895 this entry might be in a different section from previous range. */
10897 static unsigned int
10898 add_ranges (const_tree block, bool maybe_new_sec)
10900 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10903 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10904 chain, or middle entry of a chain that will be directly referred to. */
10906 static void
10907 note_rnglist_head (unsigned int offset)
10909 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10910 return;
10911 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
10914 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10915 When using dwarf_split_debug_info, address attributes in dies destined
10916 for the final executable should be direct references--setting the
10917 parameter force_direct ensures this behavior. */
10919 static void
10920 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10921 bool *added, bool force_direct)
10923 unsigned int in_use = vec_safe_length (ranges_by_label);
10924 unsigned int offset;
10925 dw_ranges_by_label rbl = { begin, end };
10926 vec_safe_push (ranges_by_label, rbl);
10927 offset = add_ranges_num (-(int)in_use - 1, true);
10928 if (!*added)
10930 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10931 *added = true;
10932 note_rnglist_head (offset);
10936 /* Emit .debug_ranges section. */
10938 static void
10939 output_ranges (void)
10941 unsigned i;
10942 static const char *const start_fmt = "Offset %#x";
10943 const char *fmt = start_fmt;
10944 dw_ranges *r;
10946 switch_to_section (debug_ranges_section);
10947 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
10948 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
10950 int block_num = r->num;
10952 if (block_num > 0)
10954 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10955 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10957 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10958 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10960 /* If all code is in the text section, then the compilation
10961 unit base address defaults to DW_AT_low_pc, which is the
10962 base of the text section. */
10963 if (!have_multiple_function_sections)
10965 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10966 text_section_label,
10967 fmt, i * 2 * DWARF2_ADDR_SIZE);
10968 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10969 text_section_label, NULL);
10972 /* Otherwise, the compilation unit base address is zero,
10973 which allows us to use absolute addresses, and not worry
10974 about whether the target supports cross-section
10975 arithmetic. */
10976 else
10978 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10979 fmt, i * 2 * DWARF2_ADDR_SIZE);
10980 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10983 fmt = NULL;
10986 /* Negative block_num stands for an index into ranges_by_label. */
10987 else if (block_num < 0)
10989 int lab_idx = - block_num - 1;
10991 if (!have_multiple_function_sections)
10993 gcc_unreachable ();
10994 #if 0
10995 /* If we ever use add_ranges_by_labels () for a single
10996 function section, all we have to do is to take out
10997 the #if 0 above. */
10998 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10999 (*ranges_by_label)[lab_idx].begin,
11000 text_section_label,
11001 fmt, i * 2 * DWARF2_ADDR_SIZE);
11002 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11003 (*ranges_by_label)[lab_idx].end,
11004 text_section_label, NULL);
11005 #endif
11007 else
11009 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11010 (*ranges_by_label)[lab_idx].begin,
11011 fmt, i * 2 * DWARF2_ADDR_SIZE);
11012 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11013 (*ranges_by_label)[lab_idx].end,
11014 NULL);
11017 else
11019 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11020 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11021 fmt = start_fmt;
11026 /* Non-zero if .debug_line_str should be used for .debug_line section
11027 strings or strings that are likely shareable with those. */
11028 #define DWARF5_USE_DEBUG_LINE_STR \
11029 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11030 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11031 /* FIXME: there is no .debug_line_str.dwo section, \
11032 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11033 && !dwarf_split_debug_info)
11035 /* Assign .debug_rnglists indexes. */
11037 static void
11038 index_rnglists (void)
11040 unsigned i;
11041 dw_ranges *r;
11043 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11044 if (r->label)
11045 r->idx = rnglist_idx++;
11048 /* Emit .debug_rnglists section. */
11050 static void
11051 output_rnglists (void)
11053 unsigned i;
11054 dw_ranges *r;
11055 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11056 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11057 #ifdef HAVE_AS_LEB128
11058 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11059 #endif
11061 switch_to_section (debug_ranges_section);
11062 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11063 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11064 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11065 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11066 dw2_asm_output_data (4, 0xffffffff,
11067 "Initial length escape value indicating "
11068 "64-bit DWARF extension");
11069 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11070 "Length of Range Lists");
11071 ASM_OUTPUT_LABEL (asm_out_file, l1);
11072 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11073 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11074 dw2_asm_output_data (1, 0, "Segment Size");
11075 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11076 about relocation sizes and primarily care about the size of .debug*
11077 sections in linked shared libraries and executables, then
11078 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11079 into it are usually larger than just DW_FORM_sec_offset offsets
11080 into the .debug_rnglists section. */
11081 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11082 "Offset Entry Count");
11083 if (dwarf_split_debug_info)
11085 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11086 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11087 if (r->label)
11088 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11089 ranges_base_label, NULL);
11092 const char *lab = "";
11093 #ifdef HAVE_AS_LEB128
11094 unsigned int len = vec_safe_length (ranges_table);
11095 const char *base = NULL;
11096 #endif
11097 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11099 int block_num = r->num;
11101 if (r->label)
11103 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11104 lab = r->label;
11106 #ifdef HAVE_AS_LEB128
11107 if (r->label || r->maybe_new_sec)
11108 base = NULL;
11109 #endif
11110 if (block_num > 0)
11112 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11113 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11115 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11116 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11118 #ifdef HAVE_AS_LEB128
11119 /* If all code is in the text section, then the compilation
11120 unit base address defaults to DW_AT_low_pc, which is the
11121 base of the text section. */
11122 if (!have_multiple_function_sections)
11124 dw2_asm_output_data (1, DW_RLE_offset_pair,
11125 "DW_RLE_offset_pair (%s)", lab);
11126 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11127 "Range begin address (%s)", lab);
11128 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11129 "Range end address (%s)", lab);
11130 continue;
11132 if (base == NULL)
11134 dw_ranges *r2 = NULL;
11135 if (i < len - 1)
11136 r2 = &(*ranges_table)[i + 1];
11137 if (r2
11138 && r2->num != 0
11139 && r2->label == NULL
11140 && !r2->maybe_new_sec)
11142 dw2_asm_output_data (1, DW_RLE_base_address,
11143 "DW_RLE_base_address (%s)", lab);
11144 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11145 "Base address (%s)", lab);
11146 strcpy (basebuf, blabel);
11147 base = basebuf;
11150 if (base)
11152 dw2_asm_output_data (1, DW_RLE_offset_pair,
11153 "DW_RLE_offset_pair (%s)", lab);
11154 dw2_asm_output_delta_uleb128 (blabel, base,
11155 "Range begin address (%s)", lab);
11156 dw2_asm_output_delta_uleb128 (elabel, base,
11157 "Range end address (%s)", lab);
11158 continue;
11160 dw2_asm_output_data (1, DW_RLE_start_length,
11161 "DW_RLE_start_length (%s)", lab);
11162 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11163 "Range begin address (%s)", lab);
11164 dw2_asm_output_delta_uleb128 (elabel, blabel,
11165 "Range length (%s)", lab);
11166 #else
11167 dw2_asm_output_data (1, DW_RLE_start_end,
11168 "DW_RLE_start_end (%s)", lab);
11169 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11170 "Range begin address (%s)", lab);
11171 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11172 "Range end address (%s)", lab);
11173 #endif
11176 /* Negative block_num stands for an index into ranges_by_label. */
11177 else if (block_num < 0)
11179 int lab_idx = - block_num - 1;
11180 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11181 const char *elabel = (*ranges_by_label)[lab_idx].end;
11183 if (!have_multiple_function_sections)
11184 gcc_unreachable ();
11185 #ifdef HAVE_AS_LEB128
11186 dw2_asm_output_data (1, DW_RLE_start_length,
11187 "DW_RLE_start_length (%s)", lab);
11188 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11189 "Range begin address (%s)", lab);
11190 dw2_asm_output_delta_uleb128 (elabel, blabel,
11191 "Range length (%s)", lab);
11192 #else
11193 dw2_asm_output_data (1, DW_RLE_start_end,
11194 "DW_RLE_start_end (%s)", lab);
11195 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11196 "Range begin address (%s)", lab);
11197 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11198 "Range end address (%s)", lab);
11199 #endif
11201 else
11202 dw2_asm_output_data (1, DW_RLE_end_of_list,
11203 "DW_RLE_end_of_list (%s)", lab);
11205 ASM_OUTPUT_LABEL (asm_out_file, l2);
11208 /* Data structure containing information about input files. */
11209 struct file_info
11211 const char *path; /* Complete file name. */
11212 const char *fname; /* File name part. */
11213 int length; /* Length of entire string. */
11214 struct dwarf_file_data * file_idx; /* Index in input file table. */
11215 int dir_idx; /* Index in directory table. */
11218 /* Data structure containing information about directories with source
11219 files. */
11220 struct dir_info
11222 const char *path; /* Path including directory name. */
11223 int length; /* Path length. */
11224 int prefix; /* Index of directory entry which is a prefix. */
11225 int count; /* Number of files in this directory. */
11226 int dir_idx; /* Index of directory used as base. */
11229 /* Callback function for file_info comparison. We sort by looking at
11230 the directories in the path. */
11232 static int
11233 file_info_cmp (const void *p1, const void *p2)
11235 const struct file_info *const s1 = (const struct file_info *) p1;
11236 const struct file_info *const s2 = (const struct file_info *) p2;
11237 const unsigned char *cp1;
11238 const unsigned char *cp2;
11240 /* Take care of file names without directories. We need to make sure that
11241 we return consistent values to qsort since some will get confused if
11242 we return the same value when identical operands are passed in opposite
11243 orders. So if neither has a directory, return 0 and otherwise return
11244 1 or -1 depending on which one has the directory. */
11245 if ((s1->path == s1->fname || s2->path == s2->fname))
11246 return (s2->path == s2->fname) - (s1->path == s1->fname);
11248 cp1 = (const unsigned char *) s1->path;
11249 cp2 = (const unsigned char *) s2->path;
11251 while (1)
11253 ++cp1;
11254 ++cp2;
11255 /* Reached the end of the first path? If so, handle like above. */
11256 if ((cp1 == (const unsigned char *) s1->fname)
11257 || (cp2 == (const unsigned char *) s2->fname))
11258 return ((cp2 == (const unsigned char *) s2->fname)
11259 - (cp1 == (const unsigned char *) s1->fname));
11261 /* Character of current path component the same? */
11262 else if (*cp1 != *cp2)
11263 return *cp1 - *cp2;
11267 struct file_name_acquire_data
11269 struct file_info *files;
11270 int used_files;
11271 int max_files;
11274 /* Traversal function for the hash table. */
11277 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11279 struct dwarf_file_data *d = *slot;
11280 struct file_info *fi;
11281 const char *f;
11283 gcc_assert (fnad->max_files >= d->emitted_number);
11285 if (! d->emitted_number)
11286 return 1;
11288 gcc_assert (fnad->max_files != fnad->used_files);
11290 fi = fnad->files + fnad->used_files++;
11292 /* Skip all leading "./". */
11293 f = d->filename;
11294 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11295 f += 2;
11297 /* Create a new array entry. */
11298 fi->path = f;
11299 fi->length = strlen (f);
11300 fi->file_idx = d;
11302 /* Search for the file name part. */
11303 f = strrchr (f, DIR_SEPARATOR);
11304 #if defined (DIR_SEPARATOR_2)
11306 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11308 if (g != NULL)
11310 if (f == NULL || f < g)
11311 f = g;
11314 #endif
11316 fi->fname = f == NULL ? fi->path : f + 1;
11317 return 1;
11320 /* Helper function for output_file_names. Emit a FORM encoded
11321 string STR, with assembly comment start ENTRY_KIND and
11322 index IDX */
11324 static void
11325 output_line_string (enum dwarf_form form, const char *str,
11326 const char *entry_kind, unsigned int idx)
11328 switch (form)
11330 case DW_FORM_string:
11331 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11332 break;
11333 case DW_FORM_line_strp:
11334 if (!debug_line_str_hash)
11335 debug_line_str_hash
11336 = hash_table<indirect_string_hasher>::create_ggc (10);
11338 struct indirect_string_node *node;
11339 node = find_AT_string_in_table (str, debug_line_str_hash);
11340 set_indirect_string (node);
11341 node->form = form;
11342 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11343 debug_line_str_section, "%s: %#x: \"%s\"",
11344 entry_kind, 0, node->str);
11345 break;
11346 default:
11347 gcc_unreachable ();
11351 /* Output the directory table and the file name table. We try to minimize
11352 the total amount of memory needed. A heuristic is used to avoid large
11353 slowdowns with many input files. */
11355 static void
11356 output_file_names (void)
11358 struct file_name_acquire_data fnad;
11359 int numfiles;
11360 struct file_info *files;
11361 struct dir_info *dirs;
11362 int *saved;
11363 int *savehere;
11364 int *backmap;
11365 int ndirs;
11366 int idx_offset;
11367 int i;
11369 if (!last_emitted_file)
11371 if (dwarf_version >= 5)
11373 dw2_asm_output_data (1, 0, "Directory entry format count");
11374 dw2_asm_output_data_uleb128 (0, "Directories count");
11375 dw2_asm_output_data (1, 0, "File name entry format count");
11376 dw2_asm_output_data_uleb128 (0, "File names count");
11378 else
11380 dw2_asm_output_data (1, 0, "End directory table");
11381 dw2_asm_output_data (1, 0, "End file name table");
11383 return;
11386 numfiles = last_emitted_file->emitted_number;
11388 /* Allocate the various arrays we need. */
11389 files = XALLOCAVEC (struct file_info, numfiles);
11390 dirs = XALLOCAVEC (struct dir_info, numfiles);
11392 fnad.files = files;
11393 fnad.used_files = 0;
11394 fnad.max_files = numfiles;
11395 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11396 gcc_assert (fnad.used_files == fnad.max_files);
11398 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11400 /* Find all the different directories used. */
11401 dirs[0].path = files[0].path;
11402 dirs[0].length = files[0].fname - files[0].path;
11403 dirs[0].prefix = -1;
11404 dirs[0].count = 1;
11405 dirs[0].dir_idx = 0;
11406 files[0].dir_idx = 0;
11407 ndirs = 1;
11409 for (i = 1; i < numfiles; i++)
11410 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11411 && memcmp (dirs[ndirs - 1].path, files[i].path,
11412 dirs[ndirs - 1].length) == 0)
11414 /* Same directory as last entry. */
11415 files[i].dir_idx = ndirs - 1;
11416 ++dirs[ndirs - 1].count;
11418 else
11420 int j;
11422 /* This is a new directory. */
11423 dirs[ndirs].path = files[i].path;
11424 dirs[ndirs].length = files[i].fname - files[i].path;
11425 dirs[ndirs].count = 1;
11426 dirs[ndirs].dir_idx = ndirs;
11427 files[i].dir_idx = ndirs;
11429 /* Search for a prefix. */
11430 dirs[ndirs].prefix = -1;
11431 for (j = 0; j < ndirs; j++)
11432 if (dirs[j].length < dirs[ndirs].length
11433 && dirs[j].length > 1
11434 && (dirs[ndirs].prefix == -1
11435 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11436 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11437 dirs[ndirs].prefix = j;
11439 ++ndirs;
11442 /* Now to the actual work. We have to find a subset of the directories which
11443 allow expressing the file name using references to the directory table
11444 with the least amount of characters. We do not do an exhaustive search
11445 where we would have to check out every combination of every single
11446 possible prefix. Instead we use a heuristic which provides nearly optimal
11447 results in most cases and never is much off. */
11448 saved = XALLOCAVEC (int, ndirs);
11449 savehere = XALLOCAVEC (int, ndirs);
11451 memset (saved, '\0', ndirs * sizeof (saved[0]));
11452 for (i = 0; i < ndirs; i++)
11454 int j;
11455 int total;
11457 /* We can always save some space for the current directory. But this
11458 does not mean it will be enough to justify adding the directory. */
11459 savehere[i] = dirs[i].length;
11460 total = (savehere[i] - saved[i]) * dirs[i].count;
11462 for (j = i + 1; j < ndirs; j++)
11464 savehere[j] = 0;
11465 if (saved[j] < dirs[i].length)
11467 /* Determine whether the dirs[i] path is a prefix of the
11468 dirs[j] path. */
11469 int k;
11471 k = dirs[j].prefix;
11472 while (k != -1 && k != (int) i)
11473 k = dirs[k].prefix;
11475 if (k == (int) i)
11477 /* Yes it is. We can possibly save some memory by
11478 writing the filenames in dirs[j] relative to
11479 dirs[i]. */
11480 savehere[j] = dirs[i].length;
11481 total += (savehere[j] - saved[j]) * dirs[j].count;
11486 /* Check whether we can save enough to justify adding the dirs[i]
11487 directory. */
11488 if (total > dirs[i].length + 1)
11490 /* It's worthwhile adding. */
11491 for (j = i; j < ndirs; j++)
11492 if (savehere[j] > 0)
11494 /* Remember how much we saved for this directory so far. */
11495 saved[j] = savehere[j];
11497 /* Remember the prefix directory. */
11498 dirs[j].dir_idx = i;
11503 /* Emit the directory name table. */
11504 idx_offset = dirs[0].length > 0 ? 1 : 0;
11505 enum dwarf_form str_form = DW_FORM_string;
11506 enum dwarf_form idx_form = DW_FORM_udata;
11507 if (dwarf_version >= 5)
11509 const char *comp_dir = comp_dir_string ();
11510 if (comp_dir == NULL)
11511 comp_dir = "";
11512 dw2_asm_output_data (1, 1, "Directory entry format count");
11513 if (DWARF5_USE_DEBUG_LINE_STR)
11514 str_form = DW_FORM_line_strp;
11515 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11516 dw2_asm_output_data_uleb128 (str_form, get_DW_FORM_name (str_form));
11517 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11518 if (str_form == DW_FORM_string)
11520 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11521 for (i = 1 - idx_offset; i < ndirs; i++)
11522 dw2_asm_output_nstring (dirs[i].path,
11523 dirs[i].length
11524 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11525 "Directory Entry: %#x", i + idx_offset);
11527 else
11529 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11530 for (i = 1 - idx_offset; i < ndirs; i++)
11532 const char *str
11533 = ggc_alloc_string (dirs[i].path,
11534 dirs[i].length
11535 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11536 output_line_string (str_form, str, "Directory Entry",
11537 (unsigned) i + idx_offset);
11541 else
11543 for (i = 1 - idx_offset; i < ndirs; i++)
11544 dw2_asm_output_nstring (dirs[i].path,
11545 dirs[i].length
11546 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11547 "Directory Entry: %#x", i + idx_offset);
11549 dw2_asm_output_data (1, 0, "End directory table");
11552 /* We have to emit them in the order of emitted_number since that's
11553 used in the debug info generation. To do this efficiently we
11554 generate a back-mapping of the indices first. */
11555 backmap = XALLOCAVEC (int, numfiles);
11556 for (i = 0; i < numfiles; i++)
11557 backmap[files[i].file_idx->emitted_number - 1] = i;
11559 if (dwarf_version >= 5)
11561 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11562 if (filename0 == NULL)
11563 filename0 = "";
11564 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11565 DW_FORM_data2. Choose one based on the number of directories
11566 and how much space would they occupy in each encoding.
11567 If we have at most 256 directories, all indexes fit into
11568 a single byte, so DW_FORM_data1 is most compact (if there
11569 are at most 128 directories, DW_FORM_udata would be as
11570 compact as that, but not shorter and slower to decode). */
11571 if (ndirs + idx_offset <= 256)
11572 idx_form = DW_FORM_data1;
11573 /* If there are more than 65536 directories, we have to use
11574 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11575 Otherwise, compute what space would occupy if all the indexes
11576 used DW_FORM_udata - sum - and compare that to how large would
11577 be DW_FORM_data2 encoding, and pick the more efficient one. */
11578 else if (ndirs + idx_offset <= 65536)
11580 unsigned HOST_WIDE_INT sum = 1;
11581 for (i = 0; i < numfiles; i++)
11583 int file_idx = backmap[i];
11584 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11585 sum += size_of_uleb128 (dir_idx);
11587 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11588 idx_form = DW_FORM_data2;
11590 #ifdef VMS_DEBUGGING_INFO
11591 dw2_asm_output_data (1, 4, "File name entry format count");
11592 #else
11593 dw2_asm_output_data (1, 2, "File name entry format count");
11594 #endif
11595 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11596 dw2_asm_output_data_uleb128 (str_form, get_DW_FORM_name (str_form));
11597 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11598 "DW_LNCT_directory_index");
11599 dw2_asm_output_data_uleb128 (idx_form, get_DW_FORM_name (idx_form));
11600 #ifdef VMS_DEBUGGING_INFO
11601 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11602 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11603 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11604 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11605 #endif
11606 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11608 output_line_string (str_form, filename0, "File Entry", 0);
11610 /* Include directory index. */
11611 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11612 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11613 0, NULL);
11614 else
11615 dw2_asm_output_data_uleb128 (0, NULL);
11617 #ifdef VMS_DEBUGGING_INFO
11618 dw2_asm_output_data_uleb128 (0, NULL);
11619 dw2_asm_output_data_uleb128 (0, NULL);
11620 #endif
11623 /* Now write all the file names. */
11624 for (i = 0; i < numfiles; i++)
11626 int file_idx = backmap[i];
11627 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11629 #ifdef VMS_DEBUGGING_INFO
11630 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11632 /* Setting these fields can lead to debugger miscomparisons,
11633 but VMS Debug requires them to be set correctly. */
11635 int ver;
11636 long long cdt;
11637 long siz;
11638 int maxfilelen = (strlen (files[file_idx].path)
11639 + dirs[dir_idx].length
11640 + MAX_VMS_VERSION_LEN + 1);
11641 char *filebuf = XALLOCAVEC (char, maxfilelen);
11643 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11644 snprintf (filebuf, maxfilelen, "%s;%d",
11645 files[file_idx].path + dirs[dir_idx].length, ver);
11647 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11649 /* Include directory index. */
11650 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11651 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11652 dir_idx + idx_offset, NULL);
11653 else
11654 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11656 /* Modification time. */
11657 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11658 &cdt, 0, 0, 0) == 0)
11659 ? cdt : 0, NULL);
11661 /* File length in bytes. */
11662 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11663 0, &siz, 0, 0) == 0)
11664 ? siz : 0, NULL);
11665 #else
11666 output_line_string (str_form,
11667 files[file_idx].path + dirs[dir_idx].length,
11668 "File Entry", (unsigned) i + 1);
11670 /* Include directory index. */
11671 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11672 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11673 dir_idx + idx_offset, NULL);
11674 else
11675 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11677 if (dwarf_version >= 5)
11678 continue;
11680 /* Modification time. */
11681 dw2_asm_output_data_uleb128 (0, NULL);
11683 /* File length in bytes. */
11684 dw2_asm_output_data_uleb128 (0, NULL);
11685 #endif /* VMS_DEBUGGING_INFO */
11688 if (dwarf_version < 5)
11689 dw2_asm_output_data (1, 0, "End file name table");
11693 /* Output one line number table into the .debug_line section. */
11695 static void
11696 output_one_line_info_table (dw_line_info_table *table)
11698 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11699 unsigned int current_line = 1;
11700 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11701 dw_line_info_entry *ent;
11702 size_t i;
11704 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11706 switch (ent->opcode)
11708 case LI_set_address:
11709 /* ??? Unfortunately, we have little choice here currently, and
11710 must always use the most general form. GCC does not know the
11711 address delta itself, so we can't use DW_LNS_advance_pc. Many
11712 ports do have length attributes which will give an upper bound
11713 on the address range. We could perhaps use length attributes
11714 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11715 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11717 /* This can handle any delta. This takes
11718 4+DWARF2_ADDR_SIZE bytes. */
11719 dw2_asm_output_data (1, 0, "set address %s", line_label);
11720 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11721 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11722 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11723 break;
11725 case LI_set_line:
11726 if (ent->val == current_line)
11728 /* We still need to start a new row, so output a copy insn. */
11729 dw2_asm_output_data (1, DW_LNS_copy,
11730 "copy line %u", current_line);
11732 else
11734 int line_offset = ent->val - current_line;
11735 int line_delta = line_offset - DWARF_LINE_BASE;
11737 current_line = ent->val;
11738 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11740 /* This can handle deltas from -10 to 234, using the current
11741 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11742 This takes 1 byte. */
11743 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11744 "line %u", current_line);
11746 else
11748 /* This can handle any delta. This takes at least 4 bytes,
11749 depending on the value being encoded. */
11750 dw2_asm_output_data (1, DW_LNS_advance_line,
11751 "advance to line %u", current_line);
11752 dw2_asm_output_data_sleb128 (line_offset, NULL);
11753 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11756 break;
11758 case LI_set_file:
11759 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11760 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11761 break;
11763 case LI_set_column:
11764 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11765 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11766 break;
11768 case LI_negate_stmt:
11769 current_is_stmt = !current_is_stmt;
11770 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11771 "is_stmt %d", current_is_stmt);
11772 break;
11774 case LI_set_prologue_end:
11775 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11776 "set prologue end");
11777 break;
11779 case LI_set_epilogue_begin:
11780 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11781 "set epilogue begin");
11782 break;
11784 case LI_set_discriminator:
11785 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11786 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11787 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11788 dw2_asm_output_data_uleb128 (ent->val, NULL);
11789 break;
11793 /* Emit debug info for the address of the end of the table. */
11794 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11795 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11796 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11797 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11799 dw2_asm_output_data (1, 0, "end sequence");
11800 dw2_asm_output_data_uleb128 (1, NULL);
11801 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11804 /* Output the source line number correspondence information. This
11805 information goes into the .debug_line section. */
11807 static void
11808 output_line_info (bool prologue_only)
11810 static unsigned int generation;
11811 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11812 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11813 bool saw_one = false;
11814 int opc;
11816 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11817 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11818 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11819 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11821 if (!XCOFF_DEBUGGING_INFO)
11823 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11824 dw2_asm_output_data (4, 0xffffffff,
11825 "Initial length escape value indicating 64-bit DWARF extension");
11826 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11827 "Length of Source Line Info");
11830 ASM_OUTPUT_LABEL (asm_out_file, l1);
11832 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11833 if (dwarf_version >= 5)
11835 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11836 dw2_asm_output_data (1, 0, "Segment Size");
11838 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11839 ASM_OUTPUT_LABEL (asm_out_file, p1);
11841 /* Define the architecture-dependent minimum instruction length (in bytes).
11842 In this implementation of DWARF, this field is used for information
11843 purposes only. Since GCC generates assembly language, we have no
11844 a priori knowledge of how many instruction bytes are generated for each
11845 source line, and therefore can use only the DW_LNE_set_address and
11846 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11847 this as '1', which is "correct enough" for all architectures,
11848 and don't let the target override. */
11849 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11851 if (dwarf_version >= 4)
11852 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11853 "Maximum Operations Per Instruction");
11854 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11855 "Default is_stmt_start flag");
11856 dw2_asm_output_data (1, DWARF_LINE_BASE,
11857 "Line Base Value (Special Opcodes)");
11858 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11859 "Line Range Value (Special Opcodes)");
11860 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11861 "Special Opcode Base");
11863 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11865 int n_op_args;
11866 switch (opc)
11868 case DW_LNS_advance_pc:
11869 case DW_LNS_advance_line:
11870 case DW_LNS_set_file:
11871 case DW_LNS_set_column:
11872 case DW_LNS_fixed_advance_pc:
11873 case DW_LNS_set_isa:
11874 n_op_args = 1;
11875 break;
11876 default:
11877 n_op_args = 0;
11878 break;
11881 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11882 opc, n_op_args);
11885 /* Write out the information about the files we use. */
11886 output_file_names ();
11887 ASM_OUTPUT_LABEL (asm_out_file, p2);
11888 if (prologue_only)
11890 /* Output the marker for the end of the line number info. */
11891 ASM_OUTPUT_LABEL (asm_out_file, l2);
11892 return;
11895 if (separate_line_info)
11897 dw_line_info_table *table;
11898 size_t i;
11900 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11901 if (table->in_use)
11903 output_one_line_info_table (table);
11904 saw_one = true;
11907 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
11909 output_one_line_info_table (cold_text_section_line_info);
11910 saw_one = true;
11913 /* ??? Some Darwin linkers crash on a .debug_line section with no
11914 sequences. Further, merely a DW_LNE_end_sequence entry is not
11915 sufficient -- the address column must also be initialized.
11916 Make sure to output at least one set_address/end_sequence pair,
11917 choosing .text since that section is always present. */
11918 if (text_section_line_info->in_use || !saw_one)
11919 output_one_line_info_table (text_section_line_info);
11921 /* Output the marker for the end of the line number info. */
11922 ASM_OUTPUT_LABEL (asm_out_file, l2);
11925 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
11927 static inline bool
11928 need_endianity_attribute_p (bool reverse)
11930 return reverse && (dwarf_version >= 3 || !dwarf_strict);
11933 /* Given a pointer to a tree node for some base type, return a pointer to
11934 a DIE that describes the given type. REVERSE is true if the type is
11935 to be interpreted in the reverse storage order wrt the target order.
11937 This routine must only be called for GCC type nodes that correspond to
11938 Dwarf base (fundamental) types. */
11940 static dw_die_ref
11941 base_type_die (tree type, bool reverse)
11943 dw_die_ref base_type_result;
11944 enum dwarf_type encoding;
11945 bool fpt_used = false;
11946 struct fixed_point_type_info fpt_info;
11947 tree type_bias = NULL_TREE;
11949 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11950 return 0;
11952 /* If this is a subtype that should not be emitted as a subrange type,
11953 use the base type. See subrange_type_for_debug_p. */
11954 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11955 type = TREE_TYPE (type);
11957 switch (TREE_CODE (type))
11959 case INTEGER_TYPE:
11960 if ((dwarf_version >= 4 || !dwarf_strict)
11961 && TYPE_NAME (type)
11962 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11963 && DECL_IS_BUILTIN (TYPE_NAME (type))
11964 && DECL_NAME (TYPE_NAME (type)))
11966 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
11967 if (strcmp (name, "char16_t") == 0
11968 || strcmp (name, "char32_t") == 0)
11970 encoding = DW_ATE_UTF;
11971 break;
11974 if ((dwarf_version >= 3 || !dwarf_strict)
11975 && lang_hooks.types.get_fixed_point_type_info)
11977 memset (&fpt_info, 0, sizeof (fpt_info));
11978 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
11980 fpt_used = true;
11981 encoding = ((TYPE_UNSIGNED (type))
11982 ? DW_ATE_unsigned_fixed
11983 : DW_ATE_signed_fixed);
11984 break;
11987 if (TYPE_STRING_FLAG (type))
11989 if (TYPE_UNSIGNED (type))
11990 encoding = DW_ATE_unsigned_char;
11991 else
11992 encoding = DW_ATE_signed_char;
11994 else if (TYPE_UNSIGNED (type))
11995 encoding = DW_ATE_unsigned;
11996 else
11997 encoding = DW_ATE_signed;
11999 if (!dwarf_strict
12000 && lang_hooks.types.get_type_bias)
12001 type_bias = lang_hooks.types.get_type_bias (type);
12002 break;
12004 case REAL_TYPE:
12005 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12007 if (dwarf_version >= 3 || !dwarf_strict)
12008 encoding = DW_ATE_decimal_float;
12009 else
12010 encoding = DW_ATE_lo_user;
12012 else
12013 encoding = DW_ATE_float;
12014 break;
12016 case FIXED_POINT_TYPE:
12017 if (!(dwarf_version >= 3 || !dwarf_strict))
12018 encoding = DW_ATE_lo_user;
12019 else if (TYPE_UNSIGNED (type))
12020 encoding = DW_ATE_unsigned_fixed;
12021 else
12022 encoding = DW_ATE_signed_fixed;
12023 break;
12025 /* Dwarf2 doesn't know anything about complex ints, so use
12026 a user defined type for it. */
12027 case COMPLEX_TYPE:
12028 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12029 encoding = DW_ATE_complex_float;
12030 else
12031 encoding = DW_ATE_lo_user;
12032 break;
12034 case BOOLEAN_TYPE:
12035 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12036 encoding = DW_ATE_boolean;
12037 break;
12039 default:
12040 /* No other TREE_CODEs are Dwarf fundamental types. */
12041 gcc_unreachable ();
12044 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12046 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12047 int_size_in_bytes (type));
12048 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12050 if (need_endianity_attribute_p (reverse))
12051 add_AT_unsigned (base_type_result, DW_AT_endianity,
12052 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12054 if (fpt_used)
12056 switch (fpt_info.scale_factor_kind)
12058 case fixed_point_scale_factor_binary:
12059 add_AT_int (base_type_result, DW_AT_binary_scale,
12060 fpt_info.scale_factor.binary);
12061 break;
12063 case fixed_point_scale_factor_decimal:
12064 add_AT_int (base_type_result, DW_AT_decimal_scale,
12065 fpt_info.scale_factor.decimal);
12066 break;
12068 case fixed_point_scale_factor_arbitrary:
12069 /* Arbitrary scale factors cannot be described in standard DWARF,
12070 yet. */
12071 if (!dwarf_strict)
12073 /* Describe the scale factor as a rational constant. */
12074 const dw_die_ref scale_factor
12075 = new_die (DW_TAG_constant, comp_unit_die (), type);
12077 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12078 fpt_info.scale_factor.arbitrary.numerator);
12079 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12080 fpt_info.scale_factor.arbitrary.denominator);
12082 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12084 break;
12086 default:
12087 gcc_unreachable ();
12091 if (type_bias)
12092 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12093 dw_scalar_form_constant
12094 | dw_scalar_form_exprloc
12095 | dw_scalar_form_reference,
12096 NULL);
12098 add_pubtype (type, base_type_result);
12100 return base_type_result;
12103 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12104 named 'auto' in its type: return true for it, false otherwise. */
12106 static inline bool
12107 is_cxx_auto (tree type)
12109 if (is_cxx ())
12111 tree name = TYPE_IDENTIFIER (type);
12112 if (name == get_identifier ("auto")
12113 || name == get_identifier ("decltype(auto)"))
12114 return true;
12116 return false;
12119 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12120 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12122 static inline int
12123 is_base_type (tree type)
12125 switch (TREE_CODE (type))
12127 case ERROR_MARK:
12128 case VOID_TYPE:
12129 case INTEGER_TYPE:
12130 case REAL_TYPE:
12131 case FIXED_POINT_TYPE:
12132 case COMPLEX_TYPE:
12133 case BOOLEAN_TYPE:
12134 case POINTER_BOUNDS_TYPE:
12135 return 1;
12137 case ARRAY_TYPE:
12138 case RECORD_TYPE:
12139 case UNION_TYPE:
12140 case QUAL_UNION_TYPE:
12141 case ENUMERAL_TYPE:
12142 case FUNCTION_TYPE:
12143 case METHOD_TYPE:
12144 case POINTER_TYPE:
12145 case REFERENCE_TYPE:
12146 case NULLPTR_TYPE:
12147 case OFFSET_TYPE:
12148 case LANG_TYPE:
12149 case VECTOR_TYPE:
12150 return 0;
12152 default:
12153 if (is_cxx_auto (type))
12154 return 0;
12155 gcc_unreachable ();
12158 return 0;
12161 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12162 node, return the size in bits for the type if it is a constant, or else
12163 return the alignment for the type if the type's size is not constant, or
12164 else return BITS_PER_WORD if the type actually turns out to be an
12165 ERROR_MARK node. */
12167 static inline unsigned HOST_WIDE_INT
12168 simple_type_size_in_bits (const_tree type)
12170 if (TREE_CODE (type) == ERROR_MARK)
12171 return BITS_PER_WORD;
12172 else if (TYPE_SIZE (type) == NULL_TREE)
12173 return 0;
12174 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12175 return tree_to_uhwi (TYPE_SIZE (type));
12176 else
12177 return TYPE_ALIGN (type);
12180 /* Similarly, but return an offset_int instead of UHWI. */
12182 static inline offset_int
12183 offset_int_type_size_in_bits (const_tree type)
12185 if (TREE_CODE (type) == ERROR_MARK)
12186 return BITS_PER_WORD;
12187 else if (TYPE_SIZE (type) == NULL_TREE)
12188 return 0;
12189 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12190 return wi::to_offset (TYPE_SIZE (type));
12191 else
12192 return TYPE_ALIGN (type);
12195 /* Given a pointer to a tree node for a subrange type, return a pointer
12196 to a DIE that describes the given type. */
12198 static dw_die_ref
12199 subrange_type_die (tree type, tree low, tree high, tree bias,
12200 dw_die_ref context_die)
12202 dw_die_ref subrange_die;
12203 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12205 if (context_die == NULL)
12206 context_die = comp_unit_die ();
12208 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12210 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12212 /* The size of the subrange type and its base type do not match,
12213 so we need to generate a size attribute for the subrange type. */
12214 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12217 if (low)
12218 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12219 if (high)
12220 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12221 if (bias && !dwarf_strict)
12222 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12223 dw_scalar_form_constant
12224 | dw_scalar_form_exprloc
12225 | dw_scalar_form_reference,
12226 NULL);
12228 return subrange_die;
12231 /* Returns the (const and/or volatile) cv_qualifiers associated with
12232 the decl node. This will normally be augmented with the
12233 cv_qualifiers of the underlying type in add_type_attribute. */
12235 static int
12236 decl_quals (const_tree decl)
12238 return ((TREE_READONLY (decl)
12239 /* The C++ front-end correctly marks reference-typed
12240 variables as readonly, but from a language (and debug
12241 info) standpoint they are not const-qualified. */
12242 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12243 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12244 | (TREE_THIS_VOLATILE (decl)
12245 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12248 /* Determine the TYPE whose qualifiers match the largest strict subset
12249 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12250 qualifiers outside QUAL_MASK. */
12252 static int
12253 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12255 tree t;
12256 int best_rank = 0, best_qual = 0, max_rank;
12258 type_quals &= qual_mask;
12259 max_rank = popcount_hwi (type_quals) - 1;
12261 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12262 t = TYPE_NEXT_VARIANT (t))
12264 int q = TYPE_QUALS (t) & qual_mask;
12266 if ((q & type_quals) == q && q != type_quals
12267 && check_base_type (t, type))
12269 int rank = popcount_hwi (q);
12271 if (rank > best_rank)
12273 best_rank = rank;
12274 best_qual = q;
12279 return best_qual;
12282 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12283 static const dwarf_qual_info_t dwarf_qual_info[] =
12285 { TYPE_QUAL_CONST, DW_TAG_const_type },
12286 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12287 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12288 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12290 static const unsigned int dwarf_qual_info_size
12291 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12293 /* If DIE is a qualified DIE of some base DIE with the same parent,
12294 return the base DIE, otherwise return NULL. Set MASK to the
12295 qualifiers added compared to the returned DIE. */
12297 static dw_die_ref
12298 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12300 unsigned int i;
12301 for (i = 0; i < dwarf_qual_info_size; i++)
12302 if (die->die_tag == dwarf_qual_info[i].t)
12303 break;
12304 if (i == dwarf_qual_info_size)
12305 return NULL;
12306 if (vec_safe_length (die->die_attr) != 1)
12307 return NULL;
12308 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12309 if (type == NULL || type->die_parent != die->die_parent)
12310 return NULL;
12311 *mask |= dwarf_qual_info[i].q;
12312 if (depth)
12314 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12315 if (ret)
12316 return ret;
12318 return type;
12321 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12322 entry that chains the modifiers specified by CV_QUALS in front of the
12323 given type. REVERSE is true if the type is to be interpreted in the
12324 reverse storage order wrt the target order. */
12326 static dw_die_ref
12327 modified_type_die (tree type, int cv_quals, bool reverse,
12328 dw_die_ref context_die)
12330 enum tree_code code = TREE_CODE (type);
12331 dw_die_ref mod_type_die;
12332 dw_die_ref sub_die = NULL;
12333 tree item_type = NULL;
12334 tree qualified_type;
12335 tree name, low, high;
12336 dw_die_ref mod_scope;
12337 /* Only these cv-qualifiers are currently handled. */
12338 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12339 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12341 if (code == ERROR_MARK)
12342 return NULL;
12344 if (lang_hooks.types.get_debug_type)
12346 tree debug_type = lang_hooks.types.get_debug_type (type);
12348 if (debug_type != NULL_TREE && debug_type != type)
12349 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12352 cv_quals &= cv_qual_mask;
12354 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12355 tag modifier (and not an attribute) old consumers won't be able
12356 to handle it. */
12357 if (dwarf_version < 3)
12358 cv_quals &= ~TYPE_QUAL_RESTRICT;
12360 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12361 if (dwarf_version < 5)
12362 cv_quals &= ~TYPE_QUAL_ATOMIC;
12364 /* See if we already have the appropriately qualified variant of
12365 this type. */
12366 qualified_type = get_qualified_type (type, cv_quals);
12368 if (qualified_type == sizetype
12369 && TYPE_NAME (qualified_type)
12370 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12372 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12374 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12375 && TYPE_PRECISION (t)
12376 == TYPE_PRECISION (qualified_type)
12377 && TYPE_UNSIGNED (t)
12378 == TYPE_UNSIGNED (qualified_type));
12379 qualified_type = t;
12382 /* If we do, then we can just use its DIE, if it exists. */
12383 if (qualified_type)
12385 mod_type_die = lookup_type_die (qualified_type);
12387 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12388 if (mod_type_die
12389 && (!need_endianity_attribute_p (reverse)
12390 || !is_base_type (type)
12391 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12392 return mod_type_die;
12395 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12397 /* Handle C typedef types. */
12398 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12399 && !DECL_ARTIFICIAL (name))
12401 tree dtype = TREE_TYPE (name);
12403 if (qualified_type == dtype)
12405 /* For a named type, use the typedef. */
12406 gen_type_die (qualified_type, context_die);
12407 return lookup_type_die (qualified_type);
12409 else
12411 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12412 dquals &= cv_qual_mask;
12413 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12414 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12415 /* cv-unqualified version of named type. Just use
12416 the unnamed type to which it refers. */
12417 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12418 reverse, context_die);
12419 /* Else cv-qualified version of named type; fall through. */
12423 mod_scope = scope_die_for (type, context_die);
12425 if (cv_quals)
12427 int sub_quals = 0, first_quals = 0;
12428 unsigned i;
12429 dw_die_ref first = NULL, last = NULL;
12431 /* Determine a lesser qualified type that most closely matches
12432 this one. Then generate DW_TAG_* entries for the remaining
12433 qualifiers. */
12434 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12435 cv_qual_mask);
12436 if (sub_quals && use_debug_types)
12438 bool needed = false;
12439 /* If emitting type units, make sure the order of qualifiers
12440 is canonical. Thus, start from unqualified type if
12441 an earlier qualifier is missing in sub_quals, but some later
12442 one is present there. */
12443 for (i = 0; i < dwarf_qual_info_size; i++)
12444 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12445 needed = true;
12446 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12448 sub_quals = 0;
12449 break;
12452 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12453 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12455 /* As not all intermediate qualified DIEs have corresponding
12456 tree types, ensure that qualified DIEs in the same scope
12457 as their DW_AT_type are emitted after their DW_AT_type,
12458 only with other qualified DIEs for the same type possibly
12459 in between them. Determine the range of such qualified
12460 DIEs now (first being the base type, last being corresponding
12461 last qualified DIE for it). */
12462 unsigned int count = 0;
12463 first = qualified_die_p (mod_type_die, &first_quals,
12464 dwarf_qual_info_size);
12465 if (first == NULL)
12466 first = mod_type_die;
12467 gcc_assert ((first_quals & ~sub_quals) == 0);
12468 for (count = 0, last = first;
12469 count < (1U << dwarf_qual_info_size);
12470 count++, last = last->die_sib)
12472 int quals = 0;
12473 if (last == mod_scope->die_child)
12474 break;
12475 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12476 != first)
12477 break;
12481 for (i = 0; i < dwarf_qual_info_size; i++)
12482 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12484 dw_die_ref d;
12485 if (first && first != last)
12487 for (d = first->die_sib; ; d = d->die_sib)
12489 int quals = 0;
12490 qualified_die_p (d, &quals, dwarf_qual_info_size);
12491 if (quals == (first_quals | dwarf_qual_info[i].q))
12492 break;
12493 if (d == last)
12495 d = NULL;
12496 break;
12499 if (d)
12501 mod_type_die = d;
12502 continue;
12505 if (first)
12507 d = ggc_cleared_alloc<die_node> ();
12508 d->die_tag = dwarf_qual_info[i].t;
12509 add_child_die_after (mod_scope, d, last);
12510 last = d;
12512 else
12513 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12514 if (mod_type_die)
12515 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12516 mod_type_die = d;
12517 first_quals |= dwarf_qual_info[i].q;
12520 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12522 dwarf_tag tag = DW_TAG_pointer_type;
12523 if (code == REFERENCE_TYPE)
12525 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12526 tag = DW_TAG_rvalue_reference_type;
12527 else
12528 tag = DW_TAG_reference_type;
12530 mod_type_die = new_die (tag, mod_scope, type);
12532 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12533 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12534 item_type = TREE_TYPE (type);
12536 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12537 if (!ADDR_SPACE_GENERIC_P (as))
12539 int action = targetm.addr_space.debug (as);
12540 if (action >= 0)
12542 /* Positive values indicate an address_class. */
12543 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12545 else
12547 /* Negative values indicate an (inverted) segment base reg. */
12548 dw_loc_descr_ref d
12549 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12550 add_AT_loc (mod_type_die, DW_AT_segment, d);
12554 else if (code == INTEGER_TYPE
12555 && TREE_TYPE (type) != NULL_TREE
12556 && subrange_type_for_debug_p (type, &low, &high))
12558 tree bias = NULL_TREE;
12559 if (lang_hooks.types.get_type_bias)
12560 bias = lang_hooks.types.get_type_bias (type);
12561 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12562 item_type = TREE_TYPE (type);
12564 else if (is_base_type (type))
12565 mod_type_die = base_type_die (type, reverse);
12566 else
12568 gen_type_die (type, context_die);
12570 /* We have to get the type_main_variant here (and pass that to the
12571 `lookup_type_die' routine) because the ..._TYPE node we have
12572 might simply be a *copy* of some original type node (where the
12573 copy was created to help us keep track of typedef names) and
12574 that copy might have a different TYPE_UID from the original
12575 ..._TYPE node. */
12576 if (TREE_CODE (type) == FUNCTION_TYPE
12577 || TREE_CODE (type) == METHOD_TYPE)
12579 /* For function/method types, can't just use type_main_variant here,
12580 because that can have different ref-qualifiers for C++,
12581 but try to canonicalize. */
12582 tree main = TYPE_MAIN_VARIANT (type);
12583 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12584 if (check_base_type (t, main) && check_lang_type (t, type))
12585 return lookup_type_die (t);
12586 return lookup_type_die (type);
12588 else if (TREE_CODE (type) != VECTOR_TYPE
12589 && TREE_CODE (type) != ARRAY_TYPE)
12590 return lookup_type_die (type_main_variant (type));
12591 else
12592 /* Vectors have the debugging information in the type,
12593 not the main variant. */
12594 return lookup_type_die (type);
12597 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12598 don't output a DW_TAG_typedef, since there isn't one in the
12599 user's program; just attach a DW_AT_name to the type.
12600 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12601 if the base type already has the same name. */
12602 if (name
12603 && ((TREE_CODE (name) != TYPE_DECL
12604 && (qualified_type == TYPE_MAIN_VARIANT (type)
12605 || (cv_quals == TYPE_UNQUALIFIED)))
12606 || (TREE_CODE (name) == TYPE_DECL
12607 && TREE_TYPE (name) == qualified_type
12608 && DECL_NAME (name))))
12610 if (TREE_CODE (name) == TYPE_DECL)
12611 /* Could just call add_name_and_src_coords_attributes here,
12612 but since this is a builtin type it doesn't have any
12613 useful source coordinates anyway. */
12614 name = DECL_NAME (name);
12615 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12617 /* This probably indicates a bug. */
12618 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12620 name = TYPE_IDENTIFIER (type);
12621 add_name_attribute (mod_type_die,
12622 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12625 if (qualified_type)
12626 equate_type_number_to_die (qualified_type, mod_type_die);
12628 if (item_type)
12629 /* We must do this after the equate_type_number_to_die call, in case
12630 this is a recursive type. This ensures that the modified_type_die
12631 recursion will terminate even if the type is recursive. Recursive
12632 types are possible in Ada. */
12633 sub_die = modified_type_die (item_type,
12634 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12635 reverse,
12636 context_die);
12638 if (sub_die != NULL)
12639 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12641 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12642 if (TYPE_ARTIFICIAL (type))
12643 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12645 return mod_type_die;
12648 /* Generate DIEs for the generic parameters of T.
12649 T must be either a generic type or a generic function.
12650 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12652 static void
12653 gen_generic_params_dies (tree t)
12655 tree parms, args;
12656 int parms_num, i;
12657 dw_die_ref die = NULL;
12658 int non_default;
12660 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12661 return;
12663 if (TYPE_P (t))
12664 die = lookup_type_die (t);
12665 else if (DECL_P (t))
12666 die = lookup_decl_die (t);
12668 gcc_assert (die);
12670 parms = lang_hooks.get_innermost_generic_parms (t);
12671 if (!parms)
12672 /* T has no generic parameter. It means T is neither a generic type
12673 or function. End of story. */
12674 return;
12676 parms_num = TREE_VEC_LENGTH (parms);
12677 args = lang_hooks.get_innermost_generic_args (t);
12678 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12679 non_default = int_cst_value (TREE_CHAIN (args));
12680 else
12681 non_default = TREE_VEC_LENGTH (args);
12682 for (i = 0; i < parms_num; i++)
12684 tree parm, arg, arg_pack_elems;
12685 dw_die_ref parm_die;
12687 parm = TREE_VEC_ELT (parms, i);
12688 arg = TREE_VEC_ELT (args, i);
12689 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12690 gcc_assert (parm && TREE_VALUE (parm) && arg);
12692 if (parm && TREE_VALUE (parm) && arg)
12694 /* If PARM represents a template parameter pack,
12695 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12696 by DW_TAG_template_*_parameter DIEs for the argument
12697 pack elements of ARG. Note that ARG would then be
12698 an argument pack. */
12699 if (arg_pack_elems)
12700 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12701 arg_pack_elems,
12702 die);
12703 else
12704 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12705 true /* emit name */, die);
12706 if (i >= non_default)
12707 add_AT_flag (parm_die, DW_AT_default_value, 1);
12712 /* Create and return a DIE for PARM which should be
12713 the representation of a generic type parameter.
12714 For instance, in the C++ front end, PARM would be a template parameter.
12715 ARG is the argument to PARM.
12716 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12717 name of the PARM.
12718 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12719 as a child node. */
12721 static dw_die_ref
12722 generic_parameter_die (tree parm, tree arg,
12723 bool emit_name_p,
12724 dw_die_ref parent_die)
12726 dw_die_ref tmpl_die = NULL;
12727 const char *name = NULL;
12729 if (!parm || !DECL_NAME (parm) || !arg)
12730 return NULL;
12732 /* We support non-type generic parameters and arguments,
12733 type generic parameters and arguments, as well as
12734 generic generic parameters (a.k.a. template template parameters in C++)
12735 and arguments. */
12736 if (TREE_CODE (parm) == PARM_DECL)
12737 /* PARM is a nontype generic parameter */
12738 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12739 else if (TREE_CODE (parm) == TYPE_DECL)
12740 /* PARM is a type generic parameter. */
12741 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12742 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12743 /* PARM is a generic generic parameter.
12744 Its DIE is a GNU extension. It shall have a
12745 DW_AT_name attribute to represent the name of the template template
12746 parameter, and a DW_AT_GNU_template_name attribute to represent the
12747 name of the template template argument. */
12748 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12749 parent_die, parm);
12750 else
12751 gcc_unreachable ();
12753 if (tmpl_die)
12755 tree tmpl_type;
12757 /* If PARM is a generic parameter pack, it means we are
12758 emitting debug info for a template argument pack element.
12759 In other terms, ARG is a template argument pack element.
12760 In that case, we don't emit any DW_AT_name attribute for
12761 the die. */
12762 if (emit_name_p)
12764 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12765 gcc_assert (name);
12766 add_AT_string (tmpl_die, DW_AT_name, name);
12769 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12771 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12772 TMPL_DIE should have a child DW_AT_type attribute that is set
12773 to the type of the argument to PARM, which is ARG.
12774 If PARM is a type generic parameter, TMPL_DIE should have a
12775 child DW_AT_type that is set to ARG. */
12776 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12777 add_type_attribute (tmpl_die, tmpl_type,
12778 (TREE_THIS_VOLATILE (tmpl_type)
12779 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12780 false, parent_die);
12782 else
12784 /* So TMPL_DIE is a DIE representing a
12785 a generic generic template parameter, a.k.a template template
12786 parameter in C++ and arg is a template. */
12788 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12789 to the name of the argument. */
12790 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12791 if (name)
12792 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12795 if (TREE_CODE (parm) == PARM_DECL)
12796 /* So PARM is a non-type generic parameter.
12797 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12798 attribute of TMPL_DIE which value represents the value
12799 of ARG.
12800 We must be careful here:
12801 The value of ARG might reference some function decls.
12802 We might currently be emitting debug info for a generic
12803 type and types are emitted before function decls, we don't
12804 know if the function decls referenced by ARG will actually be
12805 emitted after cgraph computations.
12806 So must defer the generation of the DW_AT_const_value to
12807 after cgraph is ready. */
12808 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12811 return tmpl_die;
12814 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12815 PARM_PACK must be a template parameter pack. The returned DIE
12816 will be child DIE of PARENT_DIE. */
12818 static dw_die_ref
12819 template_parameter_pack_die (tree parm_pack,
12820 tree parm_pack_args,
12821 dw_die_ref parent_die)
12823 dw_die_ref die;
12824 int j;
12826 gcc_assert (parent_die && parm_pack);
12828 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12829 add_name_and_src_coords_attributes (die, parm_pack);
12830 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12831 generic_parameter_die (parm_pack,
12832 TREE_VEC_ELT (parm_pack_args, j),
12833 false /* Don't emit DW_AT_name */,
12834 die);
12835 return die;
12838 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12839 an enumerated type. */
12841 static inline int
12842 type_is_enum (const_tree type)
12844 return TREE_CODE (type) == ENUMERAL_TYPE;
12847 /* Return the DBX register number described by a given RTL node. */
12849 static unsigned int
12850 dbx_reg_number (const_rtx rtl)
12852 unsigned regno = REGNO (rtl);
12854 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12856 #ifdef LEAF_REG_REMAP
12857 if (crtl->uses_only_leaf_regs)
12859 int leaf_reg = LEAF_REG_REMAP (regno);
12860 if (leaf_reg != -1)
12861 regno = (unsigned) leaf_reg;
12863 #endif
12865 regno = DBX_REGISTER_NUMBER (regno);
12866 gcc_assert (regno != INVALID_REGNUM);
12867 return regno;
12870 /* Optionally add a DW_OP_piece term to a location description expression.
12871 DW_OP_piece is only added if the location description expression already
12872 doesn't end with DW_OP_piece. */
12874 static void
12875 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12877 dw_loc_descr_ref loc;
12879 if (*list_head != NULL)
12881 /* Find the end of the chain. */
12882 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12885 if (loc->dw_loc_opc != DW_OP_piece)
12886 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12890 /* Return a location descriptor that designates a machine register or
12891 zero if there is none. */
12893 static dw_loc_descr_ref
12894 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12896 rtx regs;
12898 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12899 return 0;
12901 /* We only use "frame base" when we're sure we're talking about the
12902 post-prologue local stack frame. We do this by *not* running
12903 register elimination until this point, and recognizing the special
12904 argument pointer and soft frame pointer rtx's.
12905 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
12906 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
12907 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
12909 dw_loc_descr_ref result = NULL;
12911 if (dwarf_version >= 4 || !dwarf_strict)
12913 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
12914 initialized);
12915 if (result)
12916 add_loc_descr (&result,
12917 new_loc_descr (DW_OP_stack_value, 0, 0));
12919 return result;
12922 regs = targetm.dwarf_register_span (rtl);
12924 if (REG_NREGS (rtl) > 1 || regs)
12925 return multiple_reg_loc_descriptor (rtl, regs, initialized);
12926 else
12928 unsigned int dbx_regnum = dbx_reg_number (rtl);
12929 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12930 return 0;
12931 return one_reg_loc_descriptor (dbx_regnum, initialized);
12935 /* Return a location descriptor that designates a machine register for
12936 a given hard register number. */
12938 static dw_loc_descr_ref
12939 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12941 dw_loc_descr_ref reg_loc_descr;
12943 if (regno <= 31)
12944 reg_loc_descr
12945 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12946 else
12947 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12949 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12950 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12952 return reg_loc_descr;
12955 /* Given an RTL of a register, return a location descriptor that
12956 designates a value that spans more than one register. */
12958 static dw_loc_descr_ref
12959 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
12960 enum var_init_status initialized)
12962 int size, i;
12963 dw_loc_descr_ref loc_result = NULL;
12965 /* Simple, contiguous registers. */
12966 if (regs == NULL_RTX)
12968 unsigned reg = REGNO (rtl);
12969 int nregs;
12971 #ifdef LEAF_REG_REMAP
12972 if (crtl->uses_only_leaf_regs)
12974 int leaf_reg = LEAF_REG_REMAP (reg);
12975 if (leaf_reg != -1)
12976 reg = (unsigned) leaf_reg;
12978 #endif
12980 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
12981 nregs = REG_NREGS (rtl);
12983 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
12985 loc_result = NULL;
12986 while (nregs--)
12988 dw_loc_descr_ref t;
12990 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
12991 VAR_INIT_STATUS_INITIALIZED);
12992 add_loc_descr (&loc_result, t);
12993 add_loc_descr_op_piece (&loc_result, size);
12994 ++reg;
12996 return loc_result;
12999 /* Now onto stupid register sets in non contiguous locations. */
13001 gcc_assert (GET_CODE (regs) == PARALLEL);
13003 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13004 loc_result = NULL;
13006 for (i = 0; i < XVECLEN (regs, 0); ++i)
13008 dw_loc_descr_ref t;
13010 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13011 VAR_INIT_STATUS_INITIALIZED);
13012 add_loc_descr (&loc_result, t);
13013 add_loc_descr_op_piece (&loc_result, size);
13016 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13017 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13018 return loc_result;
13021 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13023 /* Return a location descriptor that designates a constant i,
13024 as a compound operation from constant (i >> shift), constant shift
13025 and DW_OP_shl. */
13027 static dw_loc_descr_ref
13028 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13030 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13031 add_loc_descr (&ret, int_loc_descriptor (shift));
13032 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13033 return ret;
13036 /* Return a location descriptor that designates a constant. */
13038 static dw_loc_descr_ref
13039 int_loc_descriptor (HOST_WIDE_INT i)
13041 enum dwarf_location_atom op;
13043 /* Pick the smallest representation of a constant, rather than just
13044 defaulting to the LEB encoding. */
13045 if (i >= 0)
13047 int clz = clz_hwi (i);
13048 int ctz = ctz_hwi (i);
13049 if (i <= 31)
13050 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13051 else if (i <= 0xff)
13052 op = DW_OP_const1u;
13053 else if (i <= 0xffff)
13054 op = DW_OP_const2u;
13055 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13056 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13057 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13058 DW_OP_litX DW_OP_const1u Y 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 - 5);
13061 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13062 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13063 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13064 while DW_OP_const4u is 5 bytes. */
13065 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13067 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13068 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13069 <= 4)
13071 /* As i >= 2**31, the double cast above will yield a negative number.
13072 Since wrapping is defined in DWARF expressions we can output big
13073 positive integers as small negative ones, regardless of the size
13074 of host wide ints.
13076 Here, since the evaluator will handle 32-bit values and since i >=
13077 2**31, we know it's going to be interpreted as a negative literal:
13078 store it this way if we can do better than 5 bytes this way. */
13079 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13081 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13082 op = DW_OP_const4u;
13084 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13085 least 6 bytes: see if we can do better before falling back to it. */
13086 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13087 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13088 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13089 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13090 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13091 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13092 >= HOST_BITS_PER_WIDE_INT)
13093 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13094 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13095 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13096 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13097 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13098 && size_of_uleb128 (i) > 6)
13099 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13100 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13101 else
13102 op = DW_OP_constu;
13104 else
13106 if (i >= -0x80)
13107 op = DW_OP_const1s;
13108 else if (i >= -0x8000)
13109 op = DW_OP_const2s;
13110 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13112 if (size_of_int_loc_descriptor (i) < 5)
13114 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13115 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13116 return ret;
13118 op = DW_OP_const4s;
13120 else
13122 if (size_of_int_loc_descriptor (i)
13123 < (unsigned long) 1 + size_of_sleb128 (i))
13125 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13126 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13127 return ret;
13129 op = DW_OP_consts;
13133 return new_loc_descr (op, i, 0);
13136 /* Likewise, for unsigned constants. */
13138 static dw_loc_descr_ref
13139 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13141 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13142 const unsigned HOST_WIDE_INT max_uint
13143 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13145 /* If possible, use the clever signed constants handling. */
13146 if (i <= max_int)
13147 return int_loc_descriptor ((HOST_WIDE_INT) i);
13149 /* Here, we are left with positive numbers that cannot be represented as
13150 HOST_WIDE_INT, i.e.:
13151 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13153 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13154 whereas may be better to output a negative integer: thanks to integer
13155 wrapping, we know that:
13156 x = x - 2 ** DWARF2_ADDR_SIZE
13157 = x - 2 * (max (HOST_WIDE_INT) + 1)
13158 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13159 small negative integers. Let's try that in cases it will clearly improve
13160 the encoding: there is no gain turning DW_OP_const4u into
13161 DW_OP_const4s. */
13162 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13163 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13164 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13166 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13168 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13169 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13170 const HOST_WIDE_INT second_shift
13171 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13173 /* So we finally have:
13174 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13175 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13176 return int_loc_descriptor (second_shift);
13179 /* Last chance: fallback to a simple constant operation. */
13180 return new_loc_descr
13181 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13182 ? DW_OP_const4u
13183 : DW_OP_const8u,
13184 i, 0);
13187 /* Generate and return a location description that computes the unsigned
13188 comparison of the two stack top entries (a OP b where b is the top-most
13189 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13190 LE_EXPR, GT_EXPR or GE_EXPR. */
13192 static dw_loc_descr_ref
13193 uint_comparison_loc_list (enum tree_code kind)
13195 enum dwarf_location_atom op, flip_op;
13196 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13198 switch (kind)
13200 case LT_EXPR:
13201 op = DW_OP_lt;
13202 break;
13203 case LE_EXPR:
13204 op = DW_OP_le;
13205 break;
13206 case GT_EXPR:
13207 op = DW_OP_gt;
13208 break;
13209 case GE_EXPR:
13210 op = DW_OP_ge;
13211 break;
13212 default:
13213 gcc_unreachable ();
13216 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13217 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13219 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13220 possible to perform unsigned comparisons: we just have to distinguish
13221 three cases:
13223 1. when a and b have the same sign (as signed integers); then we should
13224 return: a OP(signed) b;
13226 2. when a is a negative signed integer while b is a positive one, then a
13227 is a greater unsigned integer than b; likewise when a and b's roles
13228 are flipped.
13230 So first, compare the sign of the two operands. */
13231 ret = new_loc_descr (DW_OP_over, 0, 0);
13232 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13233 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13234 /* If they have different signs (i.e. they have different sign bits), then
13235 the stack top value has now the sign bit set and thus it's smaller than
13236 zero. */
13237 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13238 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13239 add_loc_descr (&ret, bra_node);
13241 /* We are in case 1. At this point, we know both operands have the same
13242 sign, to it's safe to use the built-in signed comparison. */
13243 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13244 add_loc_descr (&ret, jmp_node);
13246 /* We are in case 2. Here, we know both operands do not have the same sign,
13247 so we have to flip the signed comparison. */
13248 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13249 tmp = new_loc_descr (flip_op, 0, 0);
13250 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13251 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13252 add_loc_descr (&ret, tmp);
13254 /* This dummy operation is necessary to make the two branches join. */
13255 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13256 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13257 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13258 add_loc_descr (&ret, tmp);
13260 return ret;
13263 /* Likewise, but takes the location description lists (might be destructive on
13264 them). Return NULL if either is NULL or if concatenation fails. */
13266 static dw_loc_list_ref
13267 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13268 enum tree_code kind)
13270 if (left == NULL || right == NULL)
13271 return NULL;
13273 add_loc_list (&left, right);
13274 if (left == NULL)
13275 return NULL;
13277 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13278 return left;
13281 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13282 without actually allocating it. */
13284 static unsigned long
13285 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13287 return size_of_int_loc_descriptor (i >> shift)
13288 + size_of_int_loc_descriptor (shift)
13289 + 1;
13292 /* Return size_of_locs (int_loc_descriptor (i)) without
13293 actually allocating it. */
13295 static unsigned long
13296 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13298 unsigned long s;
13300 if (i >= 0)
13302 int clz, ctz;
13303 if (i <= 31)
13304 return 1;
13305 else if (i <= 0xff)
13306 return 2;
13307 else if (i <= 0xffff)
13308 return 3;
13309 clz = clz_hwi (i);
13310 ctz = ctz_hwi (i);
13311 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13312 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13313 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13314 - clz - 5);
13315 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13316 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13317 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13318 - clz - 8);
13319 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13320 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13321 <= 4)
13322 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13323 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13324 return 5;
13325 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13326 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13327 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13328 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13329 - clz - 8);
13330 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13331 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13332 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13333 - clz - 16);
13334 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13335 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13336 && s > 6)
13337 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13338 - clz - 32);
13339 else
13340 return 1 + s;
13342 else
13344 if (i >= -0x80)
13345 return 2;
13346 else if (i >= -0x8000)
13347 return 3;
13348 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13350 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13352 s = size_of_int_loc_descriptor (-i) + 1;
13353 if (s < 5)
13354 return s;
13356 return 5;
13358 else
13360 unsigned long r = 1 + size_of_sleb128 (i);
13361 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13363 s = size_of_int_loc_descriptor (-i) + 1;
13364 if (s < r)
13365 return s;
13367 return r;
13372 /* Return loc description representing "address" of integer value.
13373 This can appear only as toplevel expression. */
13375 static dw_loc_descr_ref
13376 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13378 int litsize;
13379 dw_loc_descr_ref loc_result = NULL;
13381 if (!(dwarf_version >= 4 || !dwarf_strict))
13382 return NULL;
13384 litsize = size_of_int_loc_descriptor (i);
13385 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13386 is more compact. For DW_OP_stack_value we need:
13387 litsize + 1 (DW_OP_stack_value)
13388 and for DW_OP_implicit_value:
13389 1 (DW_OP_implicit_value) + 1 (length) + size. */
13390 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13392 loc_result = int_loc_descriptor (i);
13393 add_loc_descr (&loc_result,
13394 new_loc_descr (DW_OP_stack_value, 0, 0));
13395 return loc_result;
13398 loc_result = new_loc_descr (DW_OP_implicit_value,
13399 size, 0);
13400 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13401 loc_result->dw_loc_oprnd2.v.val_int = i;
13402 return loc_result;
13405 /* Return a location descriptor that designates a base+offset location. */
13407 static dw_loc_descr_ref
13408 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13409 enum var_init_status initialized)
13411 unsigned int regno;
13412 dw_loc_descr_ref result;
13413 dw_fde_ref fde = cfun->fde;
13415 /* We only use "frame base" when we're sure we're talking about the
13416 post-prologue local stack frame. We do this by *not* running
13417 register elimination until this point, and recognizing the special
13418 argument pointer and soft frame pointer rtx's. */
13419 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13421 rtx elim = (ira_use_lra_p
13422 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13423 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13425 if (elim != reg)
13427 if (GET_CODE (elim) == PLUS)
13429 offset += INTVAL (XEXP (elim, 1));
13430 elim = XEXP (elim, 0);
13432 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13433 && (elim == hard_frame_pointer_rtx
13434 || elim == stack_pointer_rtx))
13435 || elim == (frame_pointer_needed
13436 ? hard_frame_pointer_rtx
13437 : stack_pointer_rtx));
13439 /* If drap register is used to align stack, use frame
13440 pointer + offset to access stack variables. If stack
13441 is aligned without drap, use stack pointer + offset to
13442 access stack variables. */
13443 if (crtl->stack_realign_tried
13444 && reg == frame_pointer_rtx)
13446 int base_reg
13447 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13448 ? HARD_FRAME_POINTER_REGNUM
13449 : REGNO (elim));
13450 return new_reg_loc_descr (base_reg, offset);
13453 gcc_assert (frame_pointer_fb_offset_valid);
13454 offset += frame_pointer_fb_offset;
13455 return new_loc_descr (DW_OP_fbreg, offset, 0);
13459 regno = REGNO (reg);
13460 #ifdef LEAF_REG_REMAP
13461 if (crtl->uses_only_leaf_regs)
13463 int leaf_reg = LEAF_REG_REMAP (regno);
13464 if (leaf_reg != -1)
13465 regno = (unsigned) leaf_reg;
13467 #endif
13468 regno = DWARF_FRAME_REGNUM (regno);
13470 if (!optimize && fde
13471 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13473 /* Use cfa+offset to represent the location of arguments passed
13474 on the stack when drap is used to align stack.
13475 Only do this when not optimizing, for optimized code var-tracking
13476 is supposed to track where the arguments live and the register
13477 used as vdrap or drap in some spot might be used for something
13478 else in other part of the routine. */
13479 return new_loc_descr (DW_OP_fbreg, offset, 0);
13482 if (regno <= 31)
13483 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13484 offset, 0);
13485 else
13486 result = new_loc_descr (DW_OP_bregx, regno, offset);
13488 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13489 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13491 return result;
13494 /* Return true if this RTL expression describes a base+offset calculation. */
13496 static inline int
13497 is_based_loc (const_rtx rtl)
13499 return (GET_CODE (rtl) == PLUS
13500 && ((REG_P (XEXP (rtl, 0))
13501 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13502 && CONST_INT_P (XEXP (rtl, 1)))));
13505 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13506 failed. */
13508 static dw_loc_descr_ref
13509 tls_mem_loc_descriptor (rtx mem)
13511 tree base;
13512 dw_loc_descr_ref loc_result;
13514 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13515 return NULL;
13517 base = get_base_address (MEM_EXPR (mem));
13518 if (base == NULL
13519 || !VAR_P (base)
13520 || !DECL_THREAD_LOCAL_P (base))
13521 return NULL;
13523 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13524 if (loc_result == NULL)
13525 return NULL;
13527 if (MEM_OFFSET (mem))
13528 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13530 return loc_result;
13533 /* Output debug info about reason why we failed to expand expression as dwarf
13534 expression. */
13536 static void
13537 expansion_failed (tree expr, rtx rtl, char const *reason)
13539 if (dump_file && (dump_flags & TDF_DETAILS))
13541 fprintf (dump_file, "Failed to expand as dwarf: ");
13542 if (expr)
13543 print_generic_expr (dump_file, expr, dump_flags);
13544 if (rtl)
13546 fprintf (dump_file, "\n");
13547 print_rtl (dump_file, rtl);
13549 fprintf (dump_file, "\nReason: %s\n", reason);
13553 /* Helper function for const_ok_for_output. */
13555 static bool
13556 const_ok_for_output_1 (rtx rtl)
13558 if (GET_CODE (rtl) == UNSPEC)
13560 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13561 we can't express it in the debug info. */
13562 /* Don't complain about TLS UNSPECs, those are just too hard to
13563 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13564 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13565 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13566 if (flag_checking
13567 && (XVECLEN (rtl, 0) == 0
13568 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13569 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13570 inform (current_function_decl
13571 ? DECL_SOURCE_LOCATION (current_function_decl)
13572 : UNKNOWN_LOCATION,
13573 #if NUM_UNSPEC_VALUES > 0
13574 "non-delegitimized UNSPEC %s (%d) found in variable location",
13575 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13576 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13577 XINT (rtl, 1));
13578 #else
13579 "non-delegitimized UNSPEC %d found in variable location",
13580 XINT (rtl, 1));
13581 #endif
13582 expansion_failed (NULL_TREE, rtl,
13583 "UNSPEC hasn't been delegitimized.\n");
13584 return false;
13587 if (targetm.const_not_ok_for_debug_p (rtl))
13589 expansion_failed (NULL_TREE, rtl,
13590 "Expression rejected for debug by the backend.\n");
13591 return false;
13594 /* FIXME: Refer to PR60655. It is possible for simplification
13595 of rtl expressions in var tracking to produce such expressions.
13596 We should really identify / validate expressions
13597 enclosed in CONST that can be handled by assemblers on various
13598 targets and only handle legitimate cases here. */
13599 if (GET_CODE (rtl) != SYMBOL_REF)
13601 if (GET_CODE (rtl) == NOT)
13602 return false;
13603 return true;
13606 if (CONSTANT_POOL_ADDRESS_P (rtl))
13608 bool marked;
13609 get_pool_constant_mark (rtl, &marked);
13610 /* If all references to this pool constant were optimized away,
13611 it was not output and thus we can't represent it. */
13612 if (!marked)
13614 expansion_failed (NULL_TREE, rtl,
13615 "Constant was removed from constant pool.\n");
13616 return false;
13620 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13621 return false;
13623 /* Avoid references to external symbols in debug info, on several targets
13624 the linker might even refuse to link when linking a shared library,
13625 and in many other cases the relocations for .debug_info/.debug_loc are
13626 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13627 to be defined within the same shared library or executable are fine. */
13628 if (SYMBOL_REF_EXTERNAL_P (rtl))
13630 tree decl = SYMBOL_REF_DECL (rtl);
13632 if (decl == NULL || !targetm.binds_local_p (decl))
13634 expansion_failed (NULL_TREE, rtl,
13635 "Symbol not defined in current TU.\n");
13636 return false;
13640 return true;
13643 /* Return true if constant RTL can be emitted in DW_OP_addr or
13644 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13645 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13647 static bool
13648 const_ok_for_output (rtx rtl)
13650 if (GET_CODE (rtl) == SYMBOL_REF)
13651 return const_ok_for_output_1 (rtl);
13653 if (GET_CODE (rtl) == CONST)
13655 subrtx_var_iterator::array_type array;
13656 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13657 if (!const_ok_for_output_1 (*iter))
13658 return false;
13659 return true;
13662 return true;
13665 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13666 if possible, NULL otherwise. */
13668 static dw_die_ref
13669 base_type_for_mode (machine_mode mode, bool unsignedp)
13671 dw_die_ref type_die;
13672 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13674 if (type == NULL)
13675 return NULL;
13676 switch (TREE_CODE (type))
13678 case INTEGER_TYPE:
13679 case REAL_TYPE:
13680 break;
13681 default:
13682 return NULL;
13684 type_die = lookup_type_die (type);
13685 if (!type_die)
13686 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13687 comp_unit_die ());
13688 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13689 return NULL;
13690 return type_die;
13693 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13694 type matching MODE, or, if MODE is narrower than or as wide as
13695 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13696 possible. */
13698 static dw_loc_descr_ref
13699 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13701 machine_mode outer_mode = mode;
13702 dw_die_ref type_die;
13703 dw_loc_descr_ref cvt;
13705 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13707 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13708 return op;
13710 type_die = base_type_for_mode (outer_mode, 1);
13711 if (type_die == NULL)
13712 return NULL;
13713 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13714 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13715 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13716 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13717 add_loc_descr (&op, cvt);
13718 return op;
13721 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13723 static dw_loc_descr_ref
13724 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13725 dw_loc_descr_ref op1)
13727 dw_loc_descr_ref ret = op0;
13728 add_loc_descr (&ret, op1);
13729 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13730 if (STORE_FLAG_VALUE != 1)
13732 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13733 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13735 return ret;
13738 /* Return location descriptor for signed comparison OP RTL. */
13740 static dw_loc_descr_ref
13741 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13742 machine_mode mem_mode)
13744 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13745 dw_loc_descr_ref op0, op1;
13746 int shift;
13748 if (op_mode == VOIDmode)
13749 op_mode = GET_MODE (XEXP (rtl, 1));
13750 if (op_mode == VOIDmode)
13751 return NULL;
13753 if (dwarf_strict
13754 && dwarf_version < 5
13755 && (!SCALAR_INT_MODE_P (op_mode)
13756 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13757 return NULL;
13759 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13760 VAR_INIT_STATUS_INITIALIZED);
13761 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13762 VAR_INIT_STATUS_INITIALIZED);
13764 if (op0 == NULL || op1 == NULL)
13765 return NULL;
13767 if (!SCALAR_INT_MODE_P (op_mode)
13768 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13769 return compare_loc_descriptor (op, op0, op1);
13771 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13773 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13774 dw_loc_descr_ref cvt;
13776 if (type_die == NULL)
13777 return NULL;
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 (&op0, cvt);
13783 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13784 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13785 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13786 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13787 add_loc_descr (&op1, cvt);
13788 return compare_loc_descriptor (op, op0, op1);
13791 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13792 /* For eq/ne, if the operands are known to be zero-extended,
13793 there is no need to do the fancy shifting up. */
13794 if (op == DW_OP_eq || op == DW_OP_ne)
13796 dw_loc_descr_ref last0, last1;
13797 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13799 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13801 /* deref_size zero extends, and for constants we can check
13802 whether they are zero extended or not. */
13803 if (((last0->dw_loc_opc == DW_OP_deref_size
13804 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13805 || (CONST_INT_P (XEXP (rtl, 0))
13806 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13807 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13808 && ((last1->dw_loc_opc == DW_OP_deref_size
13809 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13810 || (CONST_INT_P (XEXP (rtl, 1))
13811 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13812 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13813 return compare_loc_descriptor (op, op0, op1);
13815 /* EQ/NE comparison against constant in narrower type than
13816 DWARF2_ADDR_SIZE can be performed either as
13817 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13818 DW_OP_{eq,ne}
13820 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13821 DW_OP_{eq,ne}. Pick whatever is shorter. */
13822 if (CONST_INT_P (XEXP (rtl, 1))
13823 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13824 && (size_of_int_loc_descriptor (shift) + 1
13825 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
13826 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13827 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13828 & GET_MODE_MASK (op_mode))))
13830 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13831 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13832 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13833 & GET_MODE_MASK (op_mode));
13834 return compare_loc_descriptor (op, op0, op1);
13837 add_loc_descr (&op0, int_loc_descriptor (shift));
13838 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13839 if (CONST_INT_P (XEXP (rtl, 1)))
13840 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13841 else
13843 add_loc_descr (&op1, int_loc_descriptor (shift));
13844 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13846 return compare_loc_descriptor (op, op0, op1);
13849 /* Return location descriptor for unsigned comparison OP RTL. */
13851 static dw_loc_descr_ref
13852 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13853 machine_mode mem_mode)
13855 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13856 dw_loc_descr_ref op0, op1;
13858 if (op_mode == VOIDmode)
13859 op_mode = GET_MODE (XEXP (rtl, 1));
13860 if (op_mode == VOIDmode)
13861 return NULL;
13862 if (!SCALAR_INT_MODE_P (op_mode))
13863 return NULL;
13865 if (dwarf_strict
13866 && dwarf_version < 5
13867 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13868 return NULL;
13870 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13871 VAR_INIT_STATUS_INITIALIZED);
13872 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13873 VAR_INIT_STATUS_INITIALIZED);
13875 if (op0 == NULL || op1 == NULL)
13876 return NULL;
13878 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13880 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13881 dw_loc_descr_ref last0, last1;
13882 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13884 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13886 if (CONST_INT_P (XEXP (rtl, 0)))
13887 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13888 /* deref_size zero extends, so no need to mask it again. */
13889 else if (last0->dw_loc_opc != DW_OP_deref_size
13890 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13892 add_loc_descr (&op0, int_loc_descriptor (mask));
13893 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13895 if (CONST_INT_P (XEXP (rtl, 1)))
13896 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13897 /* deref_size zero extends, so no need to mask it again. */
13898 else if (last1->dw_loc_opc != DW_OP_deref_size
13899 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13901 add_loc_descr (&op1, int_loc_descriptor (mask));
13902 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13905 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13907 HOST_WIDE_INT bias = 1;
13908 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13909 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13910 if (CONST_INT_P (XEXP (rtl, 1)))
13911 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13912 + INTVAL (XEXP (rtl, 1)));
13913 else
13914 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13915 bias, 0));
13917 return compare_loc_descriptor (op, op0, op1);
13920 /* Return location descriptor for {U,S}{MIN,MAX}. */
13922 static dw_loc_descr_ref
13923 minmax_loc_descriptor (rtx rtl, machine_mode mode,
13924 machine_mode mem_mode)
13926 enum dwarf_location_atom op;
13927 dw_loc_descr_ref op0, op1, ret;
13928 dw_loc_descr_ref bra_node, drop_node;
13930 if (dwarf_strict
13931 && dwarf_version < 5
13932 && (!SCALAR_INT_MODE_P (mode)
13933 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
13934 return NULL;
13936 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13937 VAR_INIT_STATUS_INITIALIZED);
13938 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13939 VAR_INIT_STATUS_INITIALIZED);
13941 if (op0 == NULL || op1 == NULL)
13942 return NULL;
13944 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13945 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13946 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13947 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13949 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13951 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
13952 add_loc_descr (&op0, int_loc_descriptor (mask));
13953 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13954 add_loc_descr (&op1, int_loc_descriptor (mask));
13955 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13957 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13959 HOST_WIDE_INT bias = 1;
13960 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13961 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13962 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13965 else if (!SCALAR_INT_MODE_P (mode)
13966 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13968 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
13969 add_loc_descr (&op0, int_loc_descriptor (shift));
13970 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13971 add_loc_descr (&op1, int_loc_descriptor (shift));
13972 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13974 else if (SCALAR_INT_MODE_P (mode)
13975 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13977 dw_die_ref type_die = base_type_for_mode (mode, 0);
13978 dw_loc_descr_ref cvt;
13979 if (type_die == NULL)
13980 return NULL;
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 (&op0, cvt);
13986 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13987 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13988 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13989 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13990 add_loc_descr (&op1, cvt);
13993 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
13994 op = DW_OP_lt;
13995 else
13996 op = DW_OP_gt;
13997 ret = op0;
13998 add_loc_descr (&ret, op1);
13999 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14000 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14001 add_loc_descr (&ret, bra_node);
14002 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14003 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14004 add_loc_descr (&ret, drop_node);
14005 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14006 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14007 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14008 && SCALAR_INT_MODE_P (mode)
14009 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14010 ret = convert_descriptor_to_mode (mode, ret);
14011 return ret;
14014 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14015 but after converting arguments to type_die, afterwards
14016 convert back to unsigned. */
14018 static dw_loc_descr_ref
14019 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14020 machine_mode mode, machine_mode mem_mode)
14022 dw_loc_descr_ref cvt, op0, op1;
14024 if (type_die == NULL)
14025 return NULL;
14026 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14027 VAR_INIT_STATUS_INITIALIZED);
14028 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14029 VAR_INIT_STATUS_INITIALIZED);
14030 if (op0 == NULL || op1 == NULL)
14031 return NULL;
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 (&op0, cvt);
14037 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14038 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14039 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14040 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14041 add_loc_descr (&op1, cvt);
14042 add_loc_descr (&op0, op1);
14043 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14044 return convert_descriptor_to_mode (mode, op0);
14047 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14048 const0 is DW_OP_lit0 or corresponding typed constant,
14049 const1 is DW_OP_lit1 or corresponding typed constant
14050 and constMSB is constant with just the MSB bit set
14051 for the mode):
14052 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14053 L1: const0 DW_OP_swap
14054 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14055 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14056 L3: DW_OP_drop
14057 L4: DW_OP_nop
14059 CTZ is similar:
14060 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14061 L1: const0 DW_OP_swap
14062 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14063 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14064 L3: DW_OP_drop
14065 L4: DW_OP_nop
14067 FFS is similar:
14068 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14069 L1: const1 DW_OP_swap
14070 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14071 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14072 L3: DW_OP_drop
14073 L4: DW_OP_nop */
14075 static dw_loc_descr_ref
14076 clz_loc_descriptor (rtx rtl, machine_mode mode,
14077 machine_mode mem_mode)
14079 dw_loc_descr_ref op0, ret, tmp;
14080 HOST_WIDE_INT valv;
14081 dw_loc_descr_ref l1jump, l1label;
14082 dw_loc_descr_ref l2jump, l2label;
14083 dw_loc_descr_ref l3jump, l3label;
14084 dw_loc_descr_ref l4jump, l4label;
14085 rtx msb;
14087 if (!SCALAR_INT_MODE_P (mode)
14088 || GET_MODE (XEXP (rtl, 0)) != mode)
14089 return NULL;
14091 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14092 VAR_INIT_STATUS_INITIALIZED);
14093 if (op0 == NULL)
14094 return NULL;
14095 ret = op0;
14096 if (GET_CODE (rtl) == CLZ)
14098 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14099 valv = GET_MODE_BITSIZE (mode);
14101 else if (GET_CODE (rtl) == FFS)
14102 valv = 0;
14103 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14104 valv = GET_MODE_BITSIZE (mode);
14105 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14106 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14107 add_loc_descr (&ret, l1jump);
14108 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14109 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14110 VAR_INIT_STATUS_INITIALIZED);
14111 if (tmp == NULL)
14112 return NULL;
14113 add_loc_descr (&ret, tmp);
14114 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14115 add_loc_descr (&ret, l4jump);
14116 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14117 ? const1_rtx : const0_rtx,
14118 mode, mem_mode,
14119 VAR_INIT_STATUS_INITIALIZED);
14120 if (l1label == NULL)
14121 return NULL;
14122 add_loc_descr (&ret, l1label);
14123 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14124 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14125 add_loc_descr (&ret, l2label);
14126 if (GET_CODE (rtl) != CLZ)
14127 msb = const1_rtx;
14128 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14129 msb = GEN_INT (HOST_WIDE_INT_1U
14130 << (GET_MODE_BITSIZE (mode) - 1));
14131 else
14132 msb = immed_wide_int_const
14133 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14134 GET_MODE_PRECISION (mode)), mode);
14135 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14136 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14137 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14138 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14139 else
14140 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14141 VAR_INIT_STATUS_INITIALIZED);
14142 if (tmp == NULL)
14143 return NULL;
14144 add_loc_descr (&ret, tmp);
14145 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14146 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14147 add_loc_descr (&ret, l3jump);
14148 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14149 VAR_INIT_STATUS_INITIALIZED);
14150 if (tmp == NULL)
14151 return NULL;
14152 add_loc_descr (&ret, tmp);
14153 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14154 ? DW_OP_shl : DW_OP_shr, 0, 0));
14155 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14156 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14157 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14158 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14159 add_loc_descr (&ret, l2jump);
14160 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14161 add_loc_descr (&ret, l3label);
14162 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14163 add_loc_descr (&ret, l4label);
14164 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14165 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14166 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14167 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14168 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14169 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14170 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14171 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14172 return ret;
14175 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14176 const1 is DW_OP_lit1 or corresponding typed constant):
14177 const0 DW_OP_swap
14178 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14179 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14180 L2: DW_OP_drop
14182 PARITY is similar:
14183 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14184 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14185 L2: DW_OP_drop */
14187 static dw_loc_descr_ref
14188 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14189 machine_mode mem_mode)
14191 dw_loc_descr_ref op0, ret, tmp;
14192 dw_loc_descr_ref l1jump, l1label;
14193 dw_loc_descr_ref l2jump, l2label;
14195 if (!SCALAR_INT_MODE_P (mode)
14196 || GET_MODE (XEXP (rtl, 0)) != mode)
14197 return NULL;
14199 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14200 VAR_INIT_STATUS_INITIALIZED);
14201 if (op0 == NULL)
14202 return NULL;
14203 ret = op0;
14204 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14205 VAR_INIT_STATUS_INITIALIZED);
14206 if (tmp == NULL)
14207 return NULL;
14208 add_loc_descr (&ret, tmp);
14209 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14210 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14211 add_loc_descr (&ret, l1label);
14212 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14213 add_loc_descr (&ret, l2jump);
14214 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14215 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14216 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14217 VAR_INIT_STATUS_INITIALIZED);
14218 if (tmp == NULL)
14219 return NULL;
14220 add_loc_descr (&ret, tmp);
14221 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14222 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14223 ? DW_OP_plus : DW_OP_xor, 0, 0));
14224 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14225 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14226 VAR_INIT_STATUS_INITIALIZED);
14227 add_loc_descr (&ret, tmp);
14228 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14229 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14230 add_loc_descr (&ret, l1jump);
14231 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14232 add_loc_descr (&ret, l2label);
14233 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14234 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14235 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14236 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14237 return ret;
14240 /* BSWAP (constS is initial shift count, either 56 or 24):
14241 constS const0
14242 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14243 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14244 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14245 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14246 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14248 static dw_loc_descr_ref
14249 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14250 machine_mode mem_mode)
14252 dw_loc_descr_ref op0, ret, tmp;
14253 dw_loc_descr_ref l1jump, l1label;
14254 dw_loc_descr_ref l2jump, l2label;
14256 if (!SCALAR_INT_MODE_P (mode)
14257 || BITS_PER_UNIT != 8
14258 || (GET_MODE_BITSIZE (mode) != 32
14259 && GET_MODE_BITSIZE (mode) != 64))
14260 return NULL;
14262 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14263 VAR_INIT_STATUS_INITIALIZED);
14264 if (op0 == NULL)
14265 return NULL;
14267 ret = op0;
14268 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14269 mode, mem_mode,
14270 VAR_INIT_STATUS_INITIALIZED);
14271 if (tmp == NULL)
14272 return NULL;
14273 add_loc_descr (&ret, tmp);
14274 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14275 VAR_INIT_STATUS_INITIALIZED);
14276 if (tmp == NULL)
14277 return NULL;
14278 add_loc_descr (&ret, tmp);
14279 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14280 add_loc_descr (&ret, l1label);
14281 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14282 mode, mem_mode,
14283 VAR_INIT_STATUS_INITIALIZED);
14284 add_loc_descr (&ret, tmp);
14285 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14286 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14287 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14288 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14289 VAR_INIT_STATUS_INITIALIZED);
14290 if (tmp == NULL)
14291 return NULL;
14292 add_loc_descr (&ret, tmp);
14293 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14294 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14295 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14296 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14297 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14298 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14299 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14300 VAR_INIT_STATUS_INITIALIZED);
14301 add_loc_descr (&ret, tmp);
14302 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14303 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14304 add_loc_descr (&ret, l2jump);
14305 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14306 VAR_INIT_STATUS_INITIALIZED);
14307 add_loc_descr (&ret, tmp);
14308 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14309 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14310 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14311 add_loc_descr (&ret, l1jump);
14312 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14313 add_loc_descr (&ret, l2label);
14314 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14315 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14316 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14317 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14318 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14319 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14320 return ret;
14323 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14324 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14325 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14326 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14328 ROTATERT is similar:
14329 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14330 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14331 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14333 static dw_loc_descr_ref
14334 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14335 machine_mode mem_mode)
14337 rtx rtlop1 = XEXP (rtl, 1);
14338 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14339 int i;
14341 if (!SCALAR_INT_MODE_P (mode))
14342 return NULL;
14344 if (GET_MODE (rtlop1) != VOIDmode
14345 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14346 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14347 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14348 VAR_INIT_STATUS_INITIALIZED);
14349 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14350 VAR_INIT_STATUS_INITIALIZED);
14351 if (op0 == NULL || op1 == NULL)
14352 return NULL;
14353 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14354 for (i = 0; i < 2; i++)
14356 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14357 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14358 mode, mem_mode,
14359 VAR_INIT_STATUS_INITIALIZED);
14360 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14361 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14362 ? DW_OP_const4u
14363 : HOST_BITS_PER_WIDE_INT == 64
14364 ? DW_OP_const8u : DW_OP_constu,
14365 GET_MODE_MASK (mode), 0);
14366 else
14367 mask[i] = NULL;
14368 if (mask[i] == NULL)
14369 return NULL;
14370 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14372 ret = op0;
14373 add_loc_descr (&ret, op1);
14374 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14375 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14376 if (GET_CODE (rtl) == ROTATERT)
14378 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14379 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14380 GET_MODE_BITSIZE (mode), 0));
14382 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14383 if (mask[0] != NULL)
14384 add_loc_descr (&ret, mask[0]);
14385 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14386 if (mask[1] != NULL)
14388 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14389 add_loc_descr (&ret, mask[1]);
14390 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14392 if (GET_CODE (rtl) == ROTATE)
14394 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14395 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14396 GET_MODE_BITSIZE (mode), 0));
14398 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14399 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14400 return ret;
14403 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14404 for DEBUG_PARAMETER_REF RTL. */
14406 static dw_loc_descr_ref
14407 parameter_ref_descriptor (rtx rtl)
14409 dw_loc_descr_ref ret;
14410 dw_die_ref ref;
14412 if (dwarf_strict)
14413 return NULL;
14414 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14415 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14416 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14417 if (ref)
14419 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14420 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14421 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14423 else
14425 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14426 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14428 return ret;
14431 /* The following routine converts the RTL for a variable or parameter
14432 (resident in memory) into an equivalent Dwarf representation of a
14433 mechanism for getting the address of that same variable onto the top of a
14434 hypothetical "address evaluation" stack.
14436 When creating memory location descriptors, we are effectively transforming
14437 the RTL for a memory-resident object into its Dwarf postfix expression
14438 equivalent. This routine recursively descends an RTL tree, turning
14439 it into Dwarf postfix code as it goes.
14441 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14443 MEM_MODE is the mode of the memory reference, needed to handle some
14444 autoincrement addressing modes.
14446 Return 0 if we can't represent the location. */
14448 dw_loc_descr_ref
14449 mem_loc_descriptor (rtx rtl, machine_mode mode,
14450 machine_mode mem_mode,
14451 enum var_init_status initialized)
14453 dw_loc_descr_ref mem_loc_result = NULL;
14454 enum dwarf_location_atom op;
14455 dw_loc_descr_ref op0, op1;
14456 rtx inner = NULL_RTX;
14458 if (mode == VOIDmode)
14459 mode = GET_MODE (rtl);
14461 /* Note that for a dynamically sized array, the location we will generate a
14462 description of here will be the lowest numbered location which is
14463 actually within the array. That's *not* necessarily the same as the
14464 zeroth element of the array. */
14466 rtl = targetm.delegitimize_address (rtl);
14468 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14469 return NULL;
14471 switch (GET_CODE (rtl))
14473 case POST_INC:
14474 case POST_DEC:
14475 case POST_MODIFY:
14476 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14478 case SUBREG:
14479 /* The case of a subreg may arise when we have a local (register)
14480 variable or a formal (register) parameter which doesn't quite fill
14481 up an entire register. For now, just assume that it is
14482 legitimate to make the Dwarf info refer to the whole register which
14483 contains the given subreg. */
14484 if (!subreg_lowpart_p (rtl))
14485 break;
14486 inner = SUBREG_REG (rtl);
14487 /* FALLTHRU */
14488 case TRUNCATE:
14489 if (inner == NULL_RTX)
14490 inner = XEXP (rtl, 0);
14491 if (SCALAR_INT_MODE_P (mode)
14492 && SCALAR_INT_MODE_P (GET_MODE (inner))
14493 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14494 #ifdef POINTERS_EXTEND_UNSIGNED
14495 || (mode == Pmode && mem_mode != VOIDmode)
14496 #endif
14498 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14500 mem_loc_result = mem_loc_descriptor (inner,
14501 GET_MODE (inner),
14502 mem_mode, initialized);
14503 break;
14505 if (dwarf_strict && dwarf_version < 5)
14506 break;
14507 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14508 break;
14509 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14510 && (!SCALAR_INT_MODE_P (mode)
14511 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14512 break;
14513 else
14515 dw_die_ref type_die;
14516 dw_loc_descr_ref cvt;
14518 mem_loc_result = mem_loc_descriptor (inner,
14519 GET_MODE (inner),
14520 mem_mode, initialized);
14521 if (mem_loc_result == NULL)
14522 break;
14523 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14524 if (type_die == NULL)
14526 mem_loc_result = NULL;
14527 break;
14529 if (GET_MODE_SIZE (mode)
14530 != GET_MODE_SIZE (GET_MODE (inner)))
14531 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14532 else
14533 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14534 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14535 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14536 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14537 add_loc_descr (&mem_loc_result, cvt);
14538 if (SCALAR_INT_MODE_P (mode)
14539 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14541 /* Convert it to untyped afterwards. */
14542 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14543 add_loc_descr (&mem_loc_result, cvt);
14546 break;
14548 case REG:
14549 if (! SCALAR_INT_MODE_P (mode)
14550 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14551 && rtl != arg_pointer_rtx
14552 && rtl != frame_pointer_rtx
14553 #ifdef POINTERS_EXTEND_UNSIGNED
14554 && (mode != Pmode || mem_mode == VOIDmode)
14555 #endif
14558 dw_die_ref type_die;
14559 unsigned int dbx_regnum;
14561 if (dwarf_strict && dwarf_version < 5)
14562 break;
14563 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14564 break;
14565 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14566 if (type_die == NULL)
14567 break;
14569 dbx_regnum = dbx_reg_number (rtl);
14570 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14571 break;
14572 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14573 dbx_regnum, 0);
14574 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14575 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14576 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14577 break;
14579 /* Whenever a register number forms a part of the description of the
14580 method for calculating the (dynamic) address of a memory resident
14581 object, DWARF rules require the register number be referred to as
14582 a "base register". This distinction is not based in any way upon
14583 what category of register the hardware believes the given register
14584 belongs to. This is strictly DWARF terminology we're dealing with
14585 here. Note that in cases where the location of a memory-resident
14586 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14587 OP_CONST (0)) the actual DWARF location descriptor that we generate
14588 may just be OP_BASEREG (basereg). This may look deceptively like
14589 the object in question was allocated to a register (rather than in
14590 memory) so DWARF consumers need to be aware of the subtle
14591 distinction between OP_REG and OP_BASEREG. */
14592 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14593 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14594 else if (stack_realign_drap
14595 && crtl->drap_reg
14596 && crtl->args.internal_arg_pointer == rtl
14597 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14599 /* If RTL is internal_arg_pointer, which has been optimized
14600 out, use DRAP instead. */
14601 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14602 VAR_INIT_STATUS_INITIALIZED);
14604 break;
14606 case SIGN_EXTEND:
14607 case ZERO_EXTEND:
14608 if (!SCALAR_INT_MODE_P (mode))
14609 break;
14610 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14611 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14612 if (op0 == 0)
14613 break;
14614 else if (GET_CODE (rtl) == ZERO_EXTEND
14615 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14616 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14617 < HOST_BITS_PER_WIDE_INT
14618 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14619 to expand zero extend as two shifts instead of
14620 masking. */
14621 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14623 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14624 mem_loc_result = op0;
14625 add_loc_descr (&mem_loc_result,
14626 int_loc_descriptor (GET_MODE_MASK (imode)));
14627 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14629 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14631 int shift = DWARF2_ADDR_SIZE
14632 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14633 shift *= BITS_PER_UNIT;
14634 if (GET_CODE (rtl) == SIGN_EXTEND)
14635 op = DW_OP_shra;
14636 else
14637 op = DW_OP_shr;
14638 mem_loc_result = op0;
14639 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14640 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14641 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14642 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14644 else if (!dwarf_strict || dwarf_version >= 5)
14646 dw_die_ref type_die1, type_die2;
14647 dw_loc_descr_ref cvt;
14649 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14650 GET_CODE (rtl) == ZERO_EXTEND);
14651 if (type_die1 == NULL)
14652 break;
14653 type_die2 = base_type_for_mode (mode, 1);
14654 if (type_die2 == NULL)
14655 break;
14656 mem_loc_result = op0;
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_die1;
14660 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14661 add_loc_descr (&mem_loc_result, cvt);
14662 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14663 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14664 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14665 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14666 add_loc_descr (&mem_loc_result, cvt);
14668 break;
14670 case MEM:
14672 rtx new_rtl = avoid_constant_pool_reference (rtl);
14673 if (new_rtl != rtl)
14675 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14676 initialized);
14677 if (mem_loc_result != NULL)
14678 return mem_loc_result;
14681 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14682 get_address_mode (rtl), mode,
14683 VAR_INIT_STATUS_INITIALIZED);
14684 if (mem_loc_result == NULL)
14685 mem_loc_result = tls_mem_loc_descriptor (rtl);
14686 if (mem_loc_result != NULL)
14688 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14689 || !SCALAR_INT_MODE_P(mode))
14691 dw_die_ref type_die;
14692 dw_loc_descr_ref deref;
14694 if (dwarf_strict && dwarf_version < 5)
14695 return NULL;
14696 type_die
14697 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14698 if (type_die == NULL)
14699 return NULL;
14700 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14701 GET_MODE_SIZE (mode), 0);
14702 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14703 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14704 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14705 add_loc_descr (&mem_loc_result, deref);
14707 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14708 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14709 else
14710 add_loc_descr (&mem_loc_result,
14711 new_loc_descr (DW_OP_deref_size,
14712 GET_MODE_SIZE (mode), 0));
14714 break;
14716 case LO_SUM:
14717 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14719 case LABEL_REF:
14720 /* Some ports can transform a symbol ref into a label ref, because
14721 the symbol ref is too far away and has to be dumped into a constant
14722 pool. */
14723 case CONST:
14724 case SYMBOL_REF:
14725 if (!SCALAR_INT_MODE_P (mode)
14726 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14727 #ifdef POINTERS_EXTEND_UNSIGNED
14728 && (mode != Pmode || mem_mode == VOIDmode)
14729 #endif
14731 break;
14732 if (GET_CODE (rtl) == SYMBOL_REF
14733 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14735 dw_loc_descr_ref temp;
14737 /* If this is not defined, we have no way to emit the data. */
14738 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14739 break;
14741 temp = new_addr_loc_descr (rtl, dtprel_true);
14743 /* We check for DWARF 5 here because gdb did not implement
14744 DW_OP_form_tls_address until after 7.12. */
14745 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14746 ? DW_OP_form_tls_address
14747 : DW_OP_GNU_push_tls_address),
14748 0, 0);
14749 add_loc_descr (&mem_loc_result, temp);
14751 break;
14754 if (!const_ok_for_output (rtl))
14756 if (GET_CODE (rtl) == CONST)
14757 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14758 initialized);
14759 break;
14762 symref:
14763 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14764 vec_safe_push (used_rtx_array, rtl);
14765 break;
14767 case CONCAT:
14768 case CONCATN:
14769 case VAR_LOCATION:
14770 case DEBUG_IMPLICIT_PTR:
14771 expansion_failed (NULL_TREE, rtl,
14772 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14773 return 0;
14775 case ENTRY_VALUE:
14776 if (dwarf_strict && dwarf_version < 5)
14777 return NULL;
14778 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14780 if (!SCALAR_INT_MODE_P (mode)
14781 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14782 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14783 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14784 else
14786 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14787 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14788 return NULL;
14789 op0 = one_reg_loc_descriptor (dbx_regnum,
14790 VAR_INIT_STATUS_INITIALIZED);
14793 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14794 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14796 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14797 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14798 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14799 return NULL;
14801 else
14802 gcc_unreachable ();
14803 if (op0 == NULL)
14804 return NULL;
14805 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14806 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14807 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14808 break;
14810 case DEBUG_PARAMETER_REF:
14811 mem_loc_result = parameter_ref_descriptor (rtl);
14812 break;
14814 case PRE_MODIFY:
14815 /* Extract the PLUS expression nested inside and fall into
14816 PLUS code below. */
14817 rtl = XEXP (rtl, 1);
14818 goto plus;
14820 case PRE_INC:
14821 case PRE_DEC:
14822 /* Turn these into a PLUS expression and fall into the PLUS code
14823 below. */
14824 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14825 gen_int_mode (GET_CODE (rtl) == PRE_INC
14826 ? GET_MODE_UNIT_SIZE (mem_mode)
14827 : -GET_MODE_UNIT_SIZE (mem_mode),
14828 mode));
14830 /* fall through */
14832 case PLUS:
14833 plus:
14834 if (is_based_loc (rtl)
14835 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14836 || XEXP (rtl, 0) == arg_pointer_rtx
14837 || XEXP (rtl, 0) == frame_pointer_rtx)
14838 && SCALAR_INT_MODE_P (mode))
14839 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14840 INTVAL (XEXP (rtl, 1)),
14841 VAR_INIT_STATUS_INITIALIZED);
14842 else
14844 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14845 VAR_INIT_STATUS_INITIALIZED);
14846 if (mem_loc_result == 0)
14847 break;
14849 if (CONST_INT_P (XEXP (rtl, 1))
14850 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14851 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14852 else
14854 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14855 VAR_INIT_STATUS_INITIALIZED);
14856 if (op1 == 0)
14857 return NULL;
14858 add_loc_descr (&mem_loc_result, op1);
14859 add_loc_descr (&mem_loc_result,
14860 new_loc_descr (DW_OP_plus, 0, 0));
14863 break;
14865 /* If a pseudo-reg is optimized away, it is possible for it to
14866 be replaced with a MEM containing a multiply or shift. */
14867 case MINUS:
14868 op = DW_OP_minus;
14869 goto do_binop;
14871 case MULT:
14872 op = DW_OP_mul;
14873 goto do_binop;
14875 case DIV:
14876 if ((!dwarf_strict || dwarf_version >= 5)
14877 && SCALAR_INT_MODE_P (mode)
14878 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14880 mem_loc_result = typed_binop (DW_OP_div, rtl,
14881 base_type_for_mode (mode, 0),
14882 mode, mem_mode);
14883 break;
14885 op = DW_OP_div;
14886 goto do_binop;
14888 case UMOD:
14889 op = DW_OP_mod;
14890 goto do_binop;
14892 case ASHIFT:
14893 op = DW_OP_shl;
14894 goto do_shift;
14896 case ASHIFTRT:
14897 op = DW_OP_shra;
14898 goto do_shift;
14900 case LSHIFTRT:
14901 op = DW_OP_shr;
14902 goto do_shift;
14904 do_shift:
14905 if (!SCALAR_INT_MODE_P (mode))
14906 break;
14907 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14908 VAR_INIT_STATUS_INITIALIZED);
14910 rtx rtlop1 = XEXP (rtl, 1);
14911 if (GET_MODE (rtlop1) != VOIDmode
14912 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
14913 < GET_MODE_BITSIZE (mode))
14914 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14915 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14916 VAR_INIT_STATUS_INITIALIZED);
14919 if (op0 == 0 || op1 == 0)
14920 break;
14922 mem_loc_result = op0;
14923 add_loc_descr (&mem_loc_result, op1);
14924 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14925 break;
14927 case AND:
14928 op = DW_OP_and;
14929 goto do_binop;
14931 case IOR:
14932 op = DW_OP_or;
14933 goto do_binop;
14935 case XOR:
14936 op = DW_OP_xor;
14937 goto do_binop;
14939 do_binop:
14940 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14941 VAR_INIT_STATUS_INITIALIZED);
14942 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14943 VAR_INIT_STATUS_INITIALIZED);
14945 if (op0 == 0 || op1 == 0)
14946 break;
14948 mem_loc_result = op0;
14949 add_loc_descr (&mem_loc_result, op1);
14950 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14951 break;
14953 case MOD:
14954 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14955 && (!dwarf_strict || dwarf_version >= 5))
14957 mem_loc_result = typed_binop (DW_OP_mod, rtl,
14958 base_type_for_mode (mode, 0),
14959 mode, mem_mode);
14960 break;
14963 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14964 VAR_INIT_STATUS_INITIALIZED);
14965 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14966 VAR_INIT_STATUS_INITIALIZED);
14968 if (op0 == 0 || op1 == 0)
14969 break;
14971 mem_loc_result = op0;
14972 add_loc_descr (&mem_loc_result, op1);
14973 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14974 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14975 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14976 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14977 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14978 break;
14980 case UDIV:
14981 if ((!dwarf_strict || dwarf_version >= 5)
14982 && SCALAR_INT_MODE_P (mode))
14984 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
14986 op = DW_OP_div;
14987 goto do_binop;
14989 mem_loc_result = typed_binop (DW_OP_div, rtl,
14990 base_type_for_mode (mode, 1),
14991 mode, mem_mode);
14993 break;
14995 case NOT:
14996 op = DW_OP_not;
14997 goto do_unop;
14999 case ABS:
15000 op = DW_OP_abs;
15001 goto do_unop;
15003 case NEG:
15004 op = DW_OP_neg;
15005 goto do_unop;
15007 do_unop:
15008 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15009 VAR_INIT_STATUS_INITIALIZED);
15011 if (op0 == 0)
15012 break;
15014 mem_loc_result = op0;
15015 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15016 break;
15018 case CONST_INT:
15019 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15020 #ifdef POINTERS_EXTEND_UNSIGNED
15021 || (mode == Pmode
15022 && mem_mode != VOIDmode
15023 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15024 #endif
15027 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15028 break;
15030 if ((!dwarf_strict || dwarf_version >= 5)
15031 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15032 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15034 dw_die_ref type_die = base_type_for_mode (mode, 1);
15035 machine_mode amode;
15036 if (type_die == NULL)
15037 return NULL;
15038 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15039 MODE_INT, 0);
15040 if (INTVAL (rtl) >= 0
15041 && amode != BLKmode
15042 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15043 /* const DW_OP_convert <XXX> vs.
15044 DW_OP_const_type <XXX, 1, const>. */
15045 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15046 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15048 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15049 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15050 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15051 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15052 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15053 add_loc_descr (&mem_loc_result, op0);
15054 return mem_loc_result;
15056 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15057 INTVAL (rtl));
15058 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15059 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15060 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15061 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15062 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15063 else
15065 mem_loc_result->dw_loc_oprnd2.val_class
15066 = dw_val_class_const_double;
15067 mem_loc_result->dw_loc_oprnd2.v.val_double
15068 = double_int::from_shwi (INTVAL (rtl));
15071 break;
15073 case CONST_DOUBLE:
15074 if (!dwarf_strict || dwarf_version >= 5)
15076 dw_die_ref type_die;
15078 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15079 CONST_DOUBLE rtx could represent either a large integer
15080 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15081 the value is always a floating point constant.
15083 When it is an integer, a CONST_DOUBLE is used whenever
15084 the constant requires 2 HWIs to be adequately represented.
15085 We output CONST_DOUBLEs as blocks. */
15086 if (mode == VOIDmode
15087 || (GET_MODE (rtl) == VOIDmode
15088 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15089 break;
15090 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15091 if (type_die == NULL)
15092 return NULL;
15093 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15094 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15095 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15096 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15097 #if TARGET_SUPPORTS_WIDE_INT == 0
15098 if (!SCALAR_FLOAT_MODE_P (mode))
15100 mem_loc_result->dw_loc_oprnd2.val_class
15101 = dw_val_class_const_double;
15102 mem_loc_result->dw_loc_oprnd2.v.val_double
15103 = rtx_to_double_int (rtl);
15105 else
15106 #endif
15108 unsigned int length = GET_MODE_SIZE (mode);
15109 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15111 insert_float (rtl, array);
15112 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15113 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15114 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15115 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15118 break;
15120 case CONST_WIDE_INT:
15121 if (!dwarf_strict || dwarf_version >= 5)
15123 dw_die_ref type_die;
15125 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15126 if (type_die == NULL)
15127 return NULL;
15128 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15129 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15130 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15131 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15132 mem_loc_result->dw_loc_oprnd2.val_class
15133 = dw_val_class_wide_int;
15134 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15135 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
15137 break;
15139 case EQ:
15140 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15141 break;
15143 case GE:
15144 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15145 break;
15147 case GT:
15148 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15149 break;
15151 case LE:
15152 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15153 break;
15155 case LT:
15156 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15157 break;
15159 case NE:
15160 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15161 break;
15163 case GEU:
15164 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15165 break;
15167 case GTU:
15168 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15169 break;
15171 case LEU:
15172 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15173 break;
15175 case LTU:
15176 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15177 break;
15179 case UMIN:
15180 case UMAX:
15181 if (!SCALAR_INT_MODE_P (mode))
15182 break;
15183 /* FALLTHRU */
15184 case SMIN:
15185 case SMAX:
15186 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15187 break;
15189 case ZERO_EXTRACT:
15190 case SIGN_EXTRACT:
15191 if (CONST_INT_P (XEXP (rtl, 1))
15192 && CONST_INT_P (XEXP (rtl, 2))
15193 && ((unsigned) INTVAL (XEXP (rtl, 1))
15194 + (unsigned) INTVAL (XEXP (rtl, 2))
15195 <= GET_MODE_BITSIZE (mode))
15196 && SCALAR_INT_MODE_P (mode)
15197 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15198 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15200 int shift, size;
15201 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15202 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15203 if (op0 == 0)
15204 break;
15205 if (GET_CODE (rtl) == SIGN_EXTRACT)
15206 op = DW_OP_shra;
15207 else
15208 op = DW_OP_shr;
15209 mem_loc_result = op0;
15210 size = INTVAL (XEXP (rtl, 1));
15211 shift = INTVAL (XEXP (rtl, 2));
15212 if (BITS_BIG_ENDIAN)
15213 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15214 - shift - size;
15215 if (shift + size != (int) DWARF2_ADDR_SIZE)
15217 add_loc_descr (&mem_loc_result,
15218 int_loc_descriptor (DWARF2_ADDR_SIZE
15219 - shift - size));
15220 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15222 if (size != (int) DWARF2_ADDR_SIZE)
15224 add_loc_descr (&mem_loc_result,
15225 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15226 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15229 break;
15231 case IF_THEN_ELSE:
15233 dw_loc_descr_ref op2, bra_node, drop_node;
15234 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15235 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15236 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15237 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15238 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15239 VAR_INIT_STATUS_INITIALIZED);
15240 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15241 VAR_INIT_STATUS_INITIALIZED);
15242 if (op0 == NULL || op1 == NULL || op2 == NULL)
15243 break;
15245 mem_loc_result = op1;
15246 add_loc_descr (&mem_loc_result, op2);
15247 add_loc_descr (&mem_loc_result, op0);
15248 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15249 add_loc_descr (&mem_loc_result, bra_node);
15250 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15251 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15252 add_loc_descr (&mem_loc_result, drop_node);
15253 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15254 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15256 break;
15258 case FLOAT_EXTEND:
15259 case FLOAT_TRUNCATE:
15260 case FLOAT:
15261 case UNSIGNED_FLOAT:
15262 case FIX:
15263 case UNSIGNED_FIX:
15264 if (!dwarf_strict || dwarf_version >= 5)
15266 dw_die_ref type_die;
15267 dw_loc_descr_ref cvt;
15269 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15270 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15271 if (op0 == NULL)
15272 break;
15273 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15274 && (GET_CODE (rtl) == FLOAT
15275 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15276 <= DWARF2_ADDR_SIZE))
15278 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15279 GET_CODE (rtl) == UNSIGNED_FLOAT);
15280 if (type_die == NULL)
15281 break;
15282 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15283 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15284 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15285 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15286 add_loc_descr (&op0, cvt);
15288 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15289 if (type_die == NULL)
15290 break;
15291 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15292 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15293 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15294 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15295 add_loc_descr (&op0, cvt);
15296 if (SCALAR_INT_MODE_P (mode)
15297 && (GET_CODE (rtl) == FIX
15298 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15300 op0 = convert_descriptor_to_mode (mode, op0);
15301 if (op0 == NULL)
15302 break;
15304 mem_loc_result = op0;
15306 break;
15308 case CLZ:
15309 case CTZ:
15310 case FFS:
15311 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15312 break;
15314 case POPCOUNT:
15315 case PARITY:
15316 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15317 break;
15319 case BSWAP:
15320 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15321 break;
15323 case ROTATE:
15324 case ROTATERT:
15325 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15326 break;
15328 case COMPARE:
15329 /* In theory, we could implement the above. */
15330 /* DWARF cannot represent the unsigned compare operations
15331 natively. */
15332 case SS_MULT:
15333 case US_MULT:
15334 case SS_DIV:
15335 case US_DIV:
15336 case SS_PLUS:
15337 case US_PLUS:
15338 case SS_MINUS:
15339 case US_MINUS:
15340 case SS_NEG:
15341 case US_NEG:
15342 case SS_ABS:
15343 case SS_ASHIFT:
15344 case US_ASHIFT:
15345 case SS_TRUNCATE:
15346 case US_TRUNCATE:
15347 case UNORDERED:
15348 case ORDERED:
15349 case UNEQ:
15350 case UNGE:
15351 case UNGT:
15352 case UNLE:
15353 case UNLT:
15354 case LTGT:
15355 case FRACT_CONVERT:
15356 case UNSIGNED_FRACT_CONVERT:
15357 case SAT_FRACT:
15358 case UNSIGNED_SAT_FRACT:
15359 case SQRT:
15360 case ASM_OPERANDS:
15361 case VEC_MERGE:
15362 case VEC_SELECT:
15363 case VEC_CONCAT:
15364 case VEC_DUPLICATE:
15365 case UNSPEC:
15366 case HIGH:
15367 case FMA:
15368 case STRICT_LOW_PART:
15369 case CONST_VECTOR:
15370 case CONST_FIXED:
15371 case CLRSB:
15372 case CLOBBER:
15373 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15374 can't express it in the debug info. This can happen e.g. with some
15375 TLS UNSPECs. */
15376 break;
15378 case CONST_STRING:
15379 resolve_one_addr (&rtl);
15380 goto symref;
15382 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15383 the expression. An UNSPEC rtx represents a raw DWARF operation,
15384 new_loc_descr is called for it to build the operation directly.
15385 Otherwise mem_loc_descriptor is called recursively. */
15386 case PARALLEL:
15388 int index = 0;
15389 dw_loc_descr_ref exp_result = NULL;
15391 for (; index < XVECLEN (rtl, 0); index++)
15393 rtx elem = XVECEXP (rtl, 0, index);
15394 if (GET_CODE (elem) == UNSPEC)
15396 /* Each DWARF operation UNSPEC contain two operands, if
15397 one operand is not used for the operation, const0_rtx is
15398 passed. */
15399 gcc_assert (XVECLEN (elem, 0) == 2);
15401 HOST_WIDE_INT dw_op = XINT (elem, 1);
15402 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15403 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15404 exp_result
15405 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15406 oprnd2);
15408 else
15409 exp_result
15410 = mem_loc_descriptor (elem, mode, mem_mode,
15411 VAR_INIT_STATUS_INITIALIZED);
15413 if (!mem_loc_result)
15414 mem_loc_result = exp_result;
15415 else
15416 add_loc_descr (&mem_loc_result, exp_result);
15419 break;
15422 default:
15423 if (flag_checking)
15425 print_rtl (stderr, rtl);
15426 gcc_unreachable ();
15428 break;
15431 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15432 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15434 return mem_loc_result;
15437 /* Return a descriptor that describes the concatenation of two locations.
15438 This is typically a complex variable. */
15440 static dw_loc_descr_ref
15441 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15443 dw_loc_descr_ref cc_loc_result = NULL;
15444 dw_loc_descr_ref x0_ref
15445 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15446 dw_loc_descr_ref x1_ref
15447 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15449 if (x0_ref == 0 || x1_ref == 0)
15450 return 0;
15452 cc_loc_result = x0_ref;
15453 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15455 add_loc_descr (&cc_loc_result, x1_ref);
15456 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15458 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15459 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15461 return cc_loc_result;
15464 /* Return a descriptor that describes the concatenation of N
15465 locations. */
15467 static dw_loc_descr_ref
15468 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15470 unsigned int i;
15471 dw_loc_descr_ref cc_loc_result = NULL;
15472 unsigned int n = XVECLEN (concatn, 0);
15474 for (i = 0; i < n; ++i)
15476 dw_loc_descr_ref ref;
15477 rtx x = XVECEXP (concatn, 0, i);
15479 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15480 if (ref == NULL)
15481 return NULL;
15483 add_loc_descr (&cc_loc_result, ref);
15484 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15487 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15488 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15490 return cc_loc_result;
15493 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15494 for DEBUG_IMPLICIT_PTR RTL. */
15496 static dw_loc_descr_ref
15497 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15499 dw_loc_descr_ref ret;
15500 dw_die_ref ref;
15502 if (dwarf_strict && dwarf_version < 5)
15503 return NULL;
15504 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15505 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15506 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15507 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15508 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15509 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15510 if (ref)
15512 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15513 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15514 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15516 else
15518 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15519 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15521 return ret;
15524 /* Output a proper Dwarf location descriptor for a variable or parameter
15525 which is either allocated in a register or in a memory location. For a
15526 register, we just generate an OP_REG and the register number. For a
15527 memory location we provide a Dwarf postfix expression describing how to
15528 generate the (dynamic) address of the object onto the address stack.
15530 MODE is mode of the decl if this loc_descriptor is going to be used in
15531 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15532 allowed, VOIDmode otherwise.
15534 If we don't know how to describe it, return 0. */
15536 static dw_loc_descr_ref
15537 loc_descriptor (rtx rtl, machine_mode mode,
15538 enum var_init_status initialized)
15540 dw_loc_descr_ref loc_result = NULL;
15542 switch (GET_CODE (rtl))
15544 case SUBREG:
15545 /* The case of a subreg may arise when we have a local (register)
15546 variable or a formal (register) parameter which doesn't quite fill
15547 up an entire register. For now, just assume that it is
15548 legitimate to make the Dwarf info refer to the whole register which
15549 contains the given subreg. */
15550 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15551 loc_result = loc_descriptor (SUBREG_REG (rtl),
15552 GET_MODE (SUBREG_REG (rtl)), initialized);
15553 else
15554 goto do_default;
15555 break;
15557 case REG:
15558 loc_result = reg_loc_descriptor (rtl, initialized);
15559 break;
15561 case MEM:
15562 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15563 GET_MODE (rtl), initialized);
15564 if (loc_result == NULL)
15565 loc_result = tls_mem_loc_descriptor (rtl);
15566 if (loc_result == NULL)
15568 rtx new_rtl = avoid_constant_pool_reference (rtl);
15569 if (new_rtl != rtl)
15570 loc_result = loc_descriptor (new_rtl, mode, initialized);
15572 break;
15574 case CONCAT:
15575 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15576 initialized);
15577 break;
15579 case CONCATN:
15580 loc_result = concatn_loc_descriptor (rtl, initialized);
15581 break;
15583 case VAR_LOCATION:
15584 /* Single part. */
15585 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15587 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15588 if (GET_CODE (loc) == EXPR_LIST)
15589 loc = XEXP (loc, 0);
15590 loc_result = loc_descriptor (loc, mode, initialized);
15591 break;
15594 rtl = XEXP (rtl, 1);
15595 /* FALLTHRU */
15597 case PARALLEL:
15599 rtvec par_elems = XVEC (rtl, 0);
15600 int num_elem = GET_NUM_ELEM (par_elems);
15601 machine_mode mode;
15602 int i;
15604 /* Create the first one, so we have something to add to. */
15605 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15606 VOIDmode, initialized);
15607 if (loc_result == NULL)
15608 return NULL;
15609 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15610 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15611 for (i = 1; i < num_elem; i++)
15613 dw_loc_descr_ref temp;
15615 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15616 VOIDmode, initialized);
15617 if (temp == NULL)
15618 return NULL;
15619 add_loc_descr (&loc_result, temp);
15620 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15621 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15624 break;
15626 case CONST_INT:
15627 if (mode != VOIDmode && mode != BLKmode)
15628 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15629 INTVAL (rtl));
15630 break;
15632 case CONST_DOUBLE:
15633 if (mode == VOIDmode)
15634 mode = GET_MODE (rtl);
15636 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15638 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15640 /* Note that a CONST_DOUBLE rtx could represent either an integer
15641 or a floating-point constant. A CONST_DOUBLE is used whenever
15642 the constant requires more than one word in order to be
15643 adequately represented. We output CONST_DOUBLEs as blocks. */
15644 loc_result = new_loc_descr (DW_OP_implicit_value,
15645 GET_MODE_SIZE (mode), 0);
15646 #if TARGET_SUPPORTS_WIDE_INT == 0
15647 if (!SCALAR_FLOAT_MODE_P (mode))
15649 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15650 loc_result->dw_loc_oprnd2.v.val_double
15651 = rtx_to_double_int (rtl);
15653 else
15654 #endif
15656 unsigned int length = GET_MODE_SIZE (mode);
15657 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15659 insert_float (rtl, array);
15660 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15661 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15662 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15663 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15666 break;
15668 case CONST_WIDE_INT:
15669 if (mode == VOIDmode)
15670 mode = GET_MODE (rtl);
15672 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15674 loc_result = new_loc_descr (DW_OP_implicit_value,
15675 GET_MODE_SIZE (mode), 0);
15676 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15677 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15678 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
15680 break;
15682 case CONST_VECTOR:
15683 if (mode == VOIDmode)
15684 mode = GET_MODE (rtl);
15686 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15688 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15689 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15690 unsigned char *array
15691 = ggc_vec_alloc<unsigned char> (length * elt_size);
15692 unsigned int i;
15693 unsigned char *p;
15694 machine_mode imode = GET_MODE_INNER (mode);
15696 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15697 switch (GET_MODE_CLASS (mode))
15699 case MODE_VECTOR_INT:
15700 for (i = 0, p = array; i < length; i++, p += elt_size)
15702 rtx elt = CONST_VECTOR_ELT (rtl, i);
15703 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15705 break;
15707 case MODE_VECTOR_FLOAT:
15708 for (i = 0, p = array; i < length; i++, p += elt_size)
15710 rtx elt = CONST_VECTOR_ELT (rtl, i);
15711 insert_float (elt, p);
15713 break;
15715 default:
15716 gcc_unreachable ();
15719 loc_result = new_loc_descr (DW_OP_implicit_value,
15720 length * elt_size, 0);
15721 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15722 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15723 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15724 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15726 break;
15728 case CONST:
15729 if (mode == VOIDmode
15730 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15731 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15732 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15734 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15735 break;
15737 /* FALLTHROUGH */
15738 case SYMBOL_REF:
15739 if (!const_ok_for_output (rtl))
15740 break;
15741 /* FALLTHROUGH */
15742 case LABEL_REF:
15743 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15744 && (dwarf_version >= 4 || !dwarf_strict))
15746 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15747 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15748 vec_safe_push (used_rtx_array, rtl);
15750 break;
15752 case DEBUG_IMPLICIT_PTR:
15753 loc_result = implicit_ptr_descriptor (rtl, 0);
15754 break;
15756 case PLUS:
15757 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15758 && CONST_INT_P (XEXP (rtl, 1)))
15760 loc_result
15761 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15762 break;
15764 /* FALLTHRU */
15765 do_default:
15766 default:
15767 if ((SCALAR_INT_MODE_P (mode)
15768 && GET_MODE (rtl) == mode
15769 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15770 && dwarf_version >= 4)
15771 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15773 /* Value expression. */
15774 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15775 if (loc_result)
15776 add_loc_descr (&loc_result,
15777 new_loc_descr (DW_OP_stack_value, 0, 0));
15779 break;
15782 return loc_result;
15785 /* We need to figure out what section we should use as the base for the
15786 address ranges where a given location is valid.
15787 1. If this particular DECL has a section associated with it, use that.
15788 2. If this function has a section associated with it, use that.
15789 3. Otherwise, use the text section.
15790 XXX: If you split a variable across multiple sections, we won't notice. */
15792 static const char *
15793 secname_for_decl (const_tree decl)
15795 const char *secname;
15797 if (VAR_OR_FUNCTION_DECL_P (decl)
15798 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15799 && DECL_SECTION_NAME (decl))
15800 secname = DECL_SECTION_NAME (decl);
15801 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15802 secname = DECL_SECTION_NAME (current_function_decl);
15803 else if (cfun && in_cold_section_p)
15804 secname = crtl->subsections.cold_section_label;
15805 else
15806 secname = text_section_label;
15808 return secname;
15811 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15813 static bool
15814 decl_by_reference_p (tree decl)
15816 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15817 || VAR_P (decl))
15818 && DECL_BY_REFERENCE (decl));
15821 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15822 for VARLOC. */
15824 static dw_loc_descr_ref
15825 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15826 enum var_init_status initialized)
15828 int have_address = 0;
15829 dw_loc_descr_ref descr;
15830 machine_mode mode;
15832 if (want_address != 2)
15834 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15835 /* Single part. */
15836 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15838 varloc = PAT_VAR_LOCATION_LOC (varloc);
15839 if (GET_CODE (varloc) == EXPR_LIST)
15840 varloc = XEXP (varloc, 0);
15841 mode = GET_MODE (varloc);
15842 if (MEM_P (varloc))
15844 rtx addr = XEXP (varloc, 0);
15845 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15846 mode, initialized);
15847 if (descr)
15848 have_address = 1;
15849 else
15851 rtx x = avoid_constant_pool_reference (varloc);
15852 if (x != varloc)
15853 descr = mem_loc_descriptor (x, mode, VOIDmode,
15854 initialized);
15857 else
15858 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15860 else
15861 return 0;
15863 else
15865 if (GET_CODE (varloc) == VAR_LOCATION)
15866 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15867 else
15868 mode = DECL_MODE (loc);
15869 descr = loc_descriptor (varloc, mode, initialized);
15870 have_address = 1;
15873 if (!descr)
15874 return 0;
15876 if (want_address == 2 && !have_address
15877 && (dwarf_version >= 4 || !dwarf_strict))
15879 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15881 expansion_failed (loc, NULL_RTX,
15882 "DWARF address size mismatch");
15883 return 0;
15885 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15886 have_address = 1;
15888 /* Show if we can't fill the request for an address. */
15889 if (want_address && !have_address)
15891 expansion_failed (loc, NULL_RTX,
15892 "Want address and only have value");
15893 return 0;
15896 /* If we've got an address and don't want one, dereference. */
15897 if (!want_address && have_address)
15899 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15900 enum dwarf_location_atom op;
15902 if (size > DWARF2_ADDR_SIZE || size == -1)
15904 expansion_failed (loc, NULL_RTX,
15905 "DWARF address size mismatch");
15906 return 0;
15908 else if (size == DWARF2_ADDR_SIZE)
15909 op = DW_OP_deref;
15910 else
15911 op = DW_OP_deref_size;
15913 add_loc_descr (&descr, new_loc_descr (op, size, 0));
15916 return descr;
15919 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15920 if it is not possible. */
15922 static dw_loc_descr_ref
15923 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15925 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15926 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15927 else if (dwarf_version >= 3 || !dwarf_strict)
15928 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15929 else
15930 return NULL;
15933 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15934 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
15936 static dw_loc_descr_ref
15937 dw_sra_loc_expr (tree decl, rtx loc)
15939 rtx p;
15940 unsigned HOST_WIDE_INT padsize = 0;
15941 dw_loc_descr_ref descr, *descr_tail;
15942 unsigned HOST_WIDE_INT decl_size;
15943 rtx varloc;
15944 enum var_init_status initialized;
15946 if (DECL_SIZE (decl) == NULL
15947 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
15948 return NULL;
15950 decl_size = tree_to_uhwi (DECL_SIZE (decl));
15951 descr = NULL;
15952 descr_tail = &descr;
15954 for (p = loc; p; p = XEXP (p, 1))
15956 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
15957 rtx loc_note = *decl_piece_varloc_ptr (p);
15958 dw_loc_descr_ref cur_descr;
15959 dw_loc_descr_ref *tail, last = NULL;
15960 unsigned HOST_WIDE_INT opsize = 0;
15962 if (loc_note == NULL_RTX
15963 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15965 padsize += bitsize;
15966 continue;
15968 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15969 varloc = NOTE_VAR_LOCATION (loc_note);
15970 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15971 if (cur_descr == NULL)
15973 padsize += bitsize;
15974 continue;
15977 /* Check that cur_descr either doesn't use
15978 DW_OP_*piece operations, or their sum is equal
15979 to bitsize. Otherwise we can't embed it. */
15980 for (tail = &cur_descr; *tail != NULL;
15981 tail = &(*tail)->dw_loc_next)
15982 if ((*tail)->dw_loc_opc == DW_OP_piece)
15984 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
15985 * BITS_PER_UNIT;
15986 last = *tail;
15988 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
15990 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
15991 last = *tail;
15994 if (last != NULL && opsize != bitsize)
15996 padsize += bitsize;
15997 /* Discard the current piece of the descriptor and release any
15998 addr_table entries it uses. */
15999 remove_loc_list_addr_table_entries (cur_descr);
16000 continue;
16003 /* If there is a hole, add DW_OP_*piece after empty DWARF
16004 expression, which means that those bits are optimized out. */
16005 if (padsize)
16007 if (padsize > decl_size)
16009 remove_loc_list_addr_table_entries (cur_descr);
16010 goto discard_descr;
16012 decl_size -= padsize;
16013 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16014 if (*descr_tail == NULL)
16016 remove_loc_list_addr_table_entries (cur_descr);
16017 goto discard_descr;
16019 descr_tail = &(*descr_tail)->dw_loc_next;
16020 padsize = 0;
16022 *descr_tail = cur_descr;
16023 descr_tail = tail;
16024 if (bitsize > decl_size)
16025 goto discard_descr;
16026 decl_size -= bitsize;
16027 if (last == NULL)
16029 HOST_WIDE_INT offset = 0;
16030 if (GET_CODE (varloc) == VAR_LOCATION
16031 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16033 varloc = PAT_VAR_LOCATION_LOC (varloc);
16034 if (GET_CODE (varloc) == EXPR_LIST)
16035 varloc = XEXP (varloc, 0);
16039 if (GET_CODE (varloc) == CONST
16040 || GET_CODE (varloc) == SIGN_EXTEND
16041 || GET_CODE (varloc) == ZERO_EXTEND)
16042 varloc = XEXP (varloc, 0);
16043 else if (GET_CODE (varloc) == SUBREG)
16044 varloc = SUBREG_REG (varloc);
16045 else
16046 break;
16048 while (1);
16049 /* DW_OP_bit_size offset should be zero for register
16050 or implicit location descriptions and empty location
16051 descriptions, but for memory addresses needs big endian
16052 adjustment. */
16053 if (MEM_P (varloc))
16055 unsigned HOST_WIDE_INT memsize
16056 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16057 if (memsize != bitsize)
16059 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16060 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16061 goto discard_descr;
16062 if (memsize < bitsize)
16063 goto discard_descr;
16064 if (BITS_BIG_ENDIAN)
16065 offset = memsize - bitsize;
16069 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16070 if (*descr_tail == NULL)
16071 goto discard_descr;
16072 descr_tail = &(*descr_tail)->dw_loc_next;
16076 /* If there were any non-empty expressions, add padding till the end of
16077 the decl. */
16078 if (descr != NULL && decl_size != 0)
16080 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16081 if (*descr_tail == NULL)
16082 goto discard_descr;
16084 return descr;
16086 discard_descr:
16087 /* Discard the descriptor and release any addr_table entries it uses. */
16088 remove_loc_list_addr_table_entries (descr);
16089 return NULL;
16092 /* Return the dwarf representation of the location list LOC_LIST of
16093 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16094 function. */
16096 static dw_loc_list_ref
16097 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16099 const char *endname, *secname;
16100 rtx varloc;
16101 enum var_init_status initialized;
16102 struct var_loc_node *node;
16103 dw_loc_descr_ref descr;
16104 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16105 dw_loc_list_ref list = NULL;
16106 dw_loc_list_ref *listp = &list;
16108 /* Now that we know what section we are using for a base,
16109 actually construct the list of locations.
16110 The first location information is what is passed to the
16111 function that creates the location list, and the remaining
16112 locations just get added on to that list.
16113 Note that we only know the start address for a location
16114 (IE location changes), so to build the range, we use
16115 the range [current location start, next location start].
16116 This means we have to special case the last node, and generate
16117 a range of [last location start, end of function label]. */
16119 secname = secname_for_decl (decl);
16121 for (node = loc_list->first; node; node = node->next)
16122 if (GET_CODE (node->loc) == EXPR_LIST
16123 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16125 if (GET_CODE (node->loc) == EXPR_LIST)
16127 /* This requires DW_OP_{,bit_}piece, which is not usable
16128 inside DWARF expressions. */
16129 if (want_address != 2)
16130 continue;
16131 descr = dw_sra_loc_expr (decl, node->loc);
16132 if (descr == NULL)
16133 continue;
16135 else
16137 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16138 varloc = NOTE_VAR_LOCATION (node->loc);
16139 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16141 if (descr)
16143 bool range_across_switch = false;
16144 /* If section switch happens in between node->label
16145 and node->next->label (or end of function) and
16146 we can't emit it as a single entry list,
16147 emit two ranges, first one ending at the end
16148 of first partition and second one starting at the
16149 beginning of second partition. */
16150 if (node == loc_list->last_before_switch
16151 && (node != loc_list->first || loc_list->first->next)
16152 && current_function_decl)
16154 endname = cfun->fde->dw_fde_end;
16155 range_across_switch = true;
16157 /* The variable has a location between NODE->LABEL and
16158 NODE->NEXT->LABEL. */
16159 else if (node->next)
16160 endname = node->next->label;
16161 /* If the variable has a location at the last label
16162 it keeps its location until the end of function. */
16163 else if (!current_function_decl)
16164 endname = text_end_label;
16165 else
16167 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16168 current_function_funcdef_no);
16169 endname = ggc_strdup (label_id);
16172 *listp = new_loc_list (descr, node->label, endname, secname);
16173 if (TREE_CODE (decl) == PARM_DECL
16174 && node == loc_list->first
16175 && NOTE_P (node->loc)
16176 && strcmp (node->label, endname) == 0)
16177 (*listp)->force = true;
16178 listp = &(*listp)->dw_loc_next;
16180 if (range_across_switch)
16182 if (GET_CODE (node->loc) == EXPR_LIST)
16183 descr = dw_sra_loc_expr (decl, node->loc);
16184 else
16186 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16187 varloc = NOTE_VAR_LOCATION (node->loc);
16188 descr = dw_loc_list_1 (decl, varloc, want_address,
16189 initialized);
16191 gcc_assert (descr);
16192 /* The variable has a location between NODE->LABEL and
16193 NODE->NEXT->LABEL. */
16194 if (node->next)
16195 endname = node->next->label;
16196 else
16197 endname = cfun->fde->dw_fde_second_end;
16198 *listp = new_loc_list (descr,
16199 cfun->fde->dw_fde_second_begin,
16200 endname, secname);
16201 listp = &(*listp)->dw_loc_next;
16206 /* Try to avoid the overhead of a location list emitting a location
16207 expression instead, but only if we didn't have more than one
16208 location entry in the first place. If some entries were not
16209 representable, we don't want to pretend a single entry that was
16210 applies to the entire scope in which the variable is
16211 available. */
16212 if (list && loc_list->first->next)
16213 gen_llsym (list);
16215 return list;
16218 /* Return if the loc_list has only single element and thus can be represented
16219 as location description. */
16221 static bool
16222 single_element_loc_list_p (dw_loc_list_ref list)
16224 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16225 return !list->ll_symbol;
16228 /* To each location in list LIST add loc descr REF. */
16230 static void
16231 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16233 dw_loc_descr_ref copy;
16234 add_loc_descr (&list->expr, ref);
16235 list = list->dw_loc_next;
16236 while (list)
16238 copy = ggc_alloc<dw_loc_descr_node> ();
16239 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16240 add_loc_descr (&list->expr, copy);
16241 while (copy->dw_loc_next)
16243 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
16244 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16245 copy->dw_loc_next = new_copy;
16246 copy = new_copy;
16248 list = list->dw_loc_next;
16252 /* Given two lists RET and LIST
16253 produce location list that is result of adding expression in LIST
16254 to expression in RET on each position in program.
16255 Might be destructive on both RET and LIST.
16257 TODO: We handle only simple cases of RET or LIST having at most one
16258 element. General case would inolve sorting the lists in program order
16259 and merging them that will need some additional work.
16260 Adding that will improve quality of debug info especially for SRA-ed
16261 structures. */
16263 static void
16264 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16266 if (!list)
16267 return;
16268 if (!*ret)
16270 *ret = list;
16271 return;
16273 if (!list->dw_loc_next)
16275 add_loc_descr_to_each (*ret, list->expr);
16276 return;
16278 if (!(*ret)->dw_loc_next)
16280 add_loc_descr_to_each (list, (*ret)->expr);
16281 *ret = list;
16282 return;
16284 expansion_failed (NULL_TREE, NULL_RTX,
16285 "Don't know how to merge two non-trivial"
16286 " location lists.\n");
16287 *ret = NULL;
16288 return;
16291 /* LOC is constant expression. Try a luck, look it up in constant
16292 pool and return its loc_descr of its address. */
16294 static dw_loc_descr_ref
16295 cst_pool_loc_descr (tree loc)
16297 /* Get an RTL for this, if something has been emitted. */
16298 rtx rtl = lookup_constant_def (loc);
16300 if (!rtl || !MEM_P (rtl))
16302 gcc_assert (!rtl);
16303 return 0;
16305 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16307 /* TODO: We might get more coverage if we was actually delaying expansion
16308 of all expressions till end of compilation when constant pools are fully
16309 populated. */
16310 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16312 expansion_failed (loc, NULL_RTX,
16313 "CST value in contant pool but not marked.");
16314 return 0;
16316 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16317 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16320 /* Return dw_loc_list representing address of addr_expr LOC
16321 by looking for inner INDIRECT_REF expression and turning
16322 it into simple arithmetics.
16324 See loc_list_from_tree for the meaning of CONTEXT. */
16326 static dw_loc_list_ref
16327 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16328 loc_descr_context *context)
16330 tree obj, offset;
16331 HOST_WIDE_INT bitsize, bitpos, bytepos;
16332 machine_mode mode;
16333 int unsignedp, reversep, volatilep = 0;
16334 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16336 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16337 &bitsize, &bitpos, &offset, &mode,
16338 &unsignedp, &reversep, &volatilep);
16339 STRIP_NOPS (obj);
16340 if (bitpos % BITS_PER_UNIT)
16342 expansion_failed (loc, NULL_RTX, "bitfield access");
16343 return 0;
16345 if (!INDIRECT_REF_P (obj))
16347 expansion_failed (obj,
16348 NULL_RTX, "no indirect ref in inner refrence");
16349 return 0;
16351 if (!offset && !bitpos)
16352 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16353 context);
16354 else if (toplev
16355 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16356 && (dwarf_version >= 4 || !dwarf_strict))
16358 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16359 if (!list_ret)
16360 return 0;
16361 if (offset)
16363 /* Variable offset. */
16364 list_ret1 = loc_list_from_tree (offset, 0, context);
16365 if (list_ret1 == 0)
16366 return 0;
16367 add_loc_list (&list_ret, list_ret1);
16368 if (!list_ret)
16369 return 0;
16370 add_loc_descr_to_each (list_ret,
16371 new_loc_descr (DW_OP_plus, 0, 0));
16373 bytepos = bitpos / BITS_PER_UNIT;
16374 if (bytepos > 0)
16375 add_loc_descr_to_each (list_ret,
16376 new_loc_descr (DW_OP_plus_uconst,
16377 bytepos, 0));
16378 else if (bytepos < 0)
16379 loc_list_plus_const (list_ret, bytepos);
16380 add_loc_descr_to_each (list_ret,
16381 new_loc_descr (DW_OP_stack_value, 0, 0));
16383 return list_ret;
16386 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16387 all operations from LOC are nops, move to the last one. Insert in NOPS all
16388 operations that are skipped. */
16390 static void
16391 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16392 hash_set<dw_loc_descr_ref> &nops)
16394 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16396 nops.add (loc);
16397 loc = loc->dw_loc_next;
16401 /* Helper for loc_descr_without_nops: free the location description operation
16402 P. */
16404 bool
16405 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16407 ggc_free (loc);
16408 return true;
16411 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16412 finishes LOC. */
16414 static void
16415 loc_descr_without_nops (dw_loc_descr_ref &loc)
16417 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16418 return;
16420 /* Set of all DW_OP_nop operations we remove. */
16421 hash_set<dw_loc_descr_ref> nops;
16423 /* First, strip all prefix NOP operations in order to keep the head of the
16424 operations list. */
16425 loc_descr_to_next_no_nop (loc, nops);
16427 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16429 /* For control flow operations: strip "prefix" nops in destination
16430 labels. */
16431 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16432 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16433 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16434 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16436 /* Do the same for the operations that follow, then move to the next
16437 iteration. */
16438 if (cur->dw_loc_next != NULL)
16439 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16440 cur = cur->dw_loc_next;
16443 nops.traverse<void *, free_loc_descr> (NULL);
16447 struct dwarf_procedure_info;
16449 /* Helper structure for location descriptions generation. */
16450 struct loc_descr_context
16452 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16453 NULL_TREE if DW_OP_push_object_address in invalid for this location
16454 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16455 tree context_type;
16456 /* The ..._DECL node that should be translated as a
16457 DW_OP_push_object_address operation. */
16458 tree base_decl;
16459 /* Information about the DWARF procedure we are currently generating. NULL if
16460 we are not generating a DWARF procedure. */
16461 struct dwarf_procedure_info *dpi;
16462 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16463 by consumer. Used for DW_TAG_generic_subrange attributes. */
16464 bool placeholder_arg;
16465 /* True if PLACEHOLDER_EXPR has been seen. */
16466 bool placeholder_seen;
16469 /* DWARF procedures generation
16471 DWARF expressions (aka. location descriptions) are used to encode variable
16472 things such as sizes or offsets. Such computations can have redundant parts
16473 that can be factorized in order to reduce the size of the output debug
16474 information. This is the whole point of DWARF procedures.
16476 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16477 already factorized into functions ("size functions") in order to handle very
16478 big and complex types. Such functions are quite simple: they have integral
16479 arguments, they return an integral result and their body contains only a
16480 return statement with arithmetic expressions. This is the only kind of
16481 function we are interested in translating into DWARF procedures, here.
16483 DWARF expressions and DWARF procedure are executed using a stack, so we have
16484 to define some calling convention for them to interact. Let's say that:
16486 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16487 all arguments in reverse order (right-to-left) so that when the DWARF
16488 procedure execution starts, the first argument is the top of the stack.
16490 - Then, when returning, the DWARF procedure must have consumed all arguments
16491 on the stack, must have pushed the result and touched nothing else.
16493 - Each integral argument and the result are integral types can be hold in a
16494 single stack slot.
16496 - We call "frame offset" the number of stack slots that are "under DWARF
16497 procedure control": it includes the arguments slots, the temporaries and
16498 the result slot. Thus, it is equal to the number of arguments when the
16499 procedure execution starts and must be equal to one (the result) when it
16500 returns. */
16502 /* Helper structure used when generating operations for a DWARF procedure. */
16503 struct dwarf_procedure_info
16505 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16506 currently translated. */
16507 tree fndecl;
16508 /* The number of arguments FNDECL takes. */
16509 unsigned args_count;
16512 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16513 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16514 equate it to this DIE. */
16516 static dw_die_ref
16517 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16518 dw_die_ref parent_die)
16520 dw_die_ref dwarf_proc_die;
16522 if ((dwarf_version < 3 && dwarf_strict)
16523 || location == NULL)
16524 return NULL;
16526 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16527 if (fndecl)
16528 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16529 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16530 return dwarf_proc_die;
16533 /* Return whether TYPE is a supported type as a DWARF procedure argument
16534 type or return type (we handle only scalar types and pointer types that
16535 aren't wider than the DWARF expression evaluation stack. */
16537 static bool
16538 is_handled_procedure_type (tree type)
16540 return ((INTEGRAL_TYPE_P (type)
16541 || TREE_CODE (type) == OFFSET_TYPE
16542 || TREE_CODE (type) == POINTER_TYPE)
16543 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16546 /* Helper for resolve_args_picking: do the same but stop when coming across
16547 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16548 offset *before* evaluating the corresponding operation. */
16550 static bool
16551 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16552 struct dwarf_procedure_info *dpi,
16553 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16555 /* The "frame_offset" identifier is already used to name a macro... */
16556 unsigned frame_offset_ = initial_frame_offset;
16557 dw_loc_descr_ref l;
16559 for (l = loc; l != NULL;)
16561 bool existed;
16562 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16564 /* If we already met this node, there is nothing to compute anymore. */
16565 if (existed)
16567 /* Make sure that the stack size is consistent wherever the execution
16568 flow comes from. */
16569 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16570 break;
16572 l_frame_offset = frame_offset_;
16574 /* If needed, relocate the picking offset with respect to the frame
16575 offset. */
16576 if (l->frame_offset_rel)
16578 unsigned HOST_WIDE_INT off;
16579 switch (l->dw_loc_opc)
16581 case DW_OP_pick:
16582 off = l->dw_loc_oprnd1.v.val_unsigned;
16583 break;
16584 case DW_OP_dup:
16585 off = 0;
16586 break;
16587 case DW_OP_over:
16588 off = 1;
16589 break;
16590 default:
16591 gcc_unreachable ();
16593 /* frame_offset_ is the size of the current stack frame, including
16594 incoming arguments. Besides, the arguments are pushed
16595 right-to-left. Thus, in order to access the Nth argument from
16596 this operation node, the picking has to skip temporaries *plus*
16597 one stack slot per argument (0 for the first one, 1 for the second
16598 one, etc.).
16600 The targetted argument number (N) is already set as the operand,
16601 and the number of temporaries can be computed with:
16602 frame_offsets_ - dpi->args_count */
16603 off += frame_offset_ - dpi->args_count;
16605 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16606 if (off > 255)
16607 return false;
16609 if (off == 0)
16611 l->dw_loc_opc = DW_OP_dup;
16612 l->dw_loc_oprnd1.v.val_unsigned = 0;
16614 else if (off == 1)
16616 l->dw_loc_opc = DW_OP_over;
16617 l->dw_loc_oprnd1.v.val_unsigned = 0;
16619 else
16621 l->dw_loc_opc = DW_OP_pick;
16622 l->dw_loc_oprnd1.v.val_unsigned = off;
16626 /* Update frame_offset according to the effect the current operation has
16627 on the stack. */
16628 switch (l->dw_loc_opc)
16630 case DW_OP_deref:
16631 case DW_OP_swap:
16632 case DW_OP_rot:
16633 case DW_OP_abs:
16634 case DW_OP_neg:
16635 case DW_OP_not:
16636 case DW_OP_plus_uconst:
16637 case DW_OP_skip:
16638 case DW_OP_reg0:
16639 case DW_OP_reg1:
16640 case DW_OP_reg2:
16641 case DW_OP_reg3:
16642 case DW_OP_reg4:
16643 case DW_OP_reg5:
16644 case DW_OP_reg6:
16645 case DW_OP_reg7:
16646 case DW_OP_reg8:
16647 case DW_OP_reg9:
16648 case DW_OP_reg10:
16649 case DW_OP_reg11:
16650 case DW_OP_reg12:
16651 case DW_OP_reg13:
16652 case DW_OP_reg14:
16653 case DW_OP_reg15:
16654 case DW_OP_reg16:
16655 case DW_OP_reg17:
16656 case DW_OP_reg18:
16657 case DW_OP_reg19:
16658 case DW_OP_reg20:
16659 case DW_OP_reg21:
16660 case DW_OP_reg22:
16661 case DW_OP_reg23:
16662 case DW_OP_reg24:
16663 case DW_OP_reg25:
16664 case DW_OP_reg26:
16665 case DW_OP_reg27:
16666 case DW_OP_reg28:
16667 case DW_OP_reg29:
16668 case DW_OP_reg30:
16669 case DW_OP_reg31:
16670 case DW_OP_bregx:
16671 case DW_OP_piece:
16672 case DW_OP_deref_size:
16673 case DW_OP_nop:
16674 case DW_OP_bit_piece:
16675 case DW_OP_implicit_value:
16676 case DW_OP_stack_value:
16677 break;
16679 case DW_OP_addr:
16680 case DW_OP_const1u:
16681 case DW_OP_const1s:
16682 case DW_OP_const2u:
16683 case DW_OP_const2s:
16684 case DW_OP_const4u:
16685 case DW_OP_const4s:
16686 case DW_OP_const8u:
16687 case DW_OP_const8s:
16688 case DW_OP_constu:
16689 case DW_OP_consts:
16690 case DW_OP_dup:
16691 case DW_OP_over:
16692 case DW_OP_pick:
16693 case DW_OP_lit0:
16694 case DW_OP_lit1:
16695 case DW_OP_lit2:
16696 case DW_OP_lit3:
16697 case DW_OP_lit4:
16698 case DW_OP_lit5:
16699 case DW_OP_lit6:
16700 case DW_OP_lit7:
16701 case DW_OP_lit8:
16702 case DW_OP_lit9:
16703 case DW_OP_lit10:
16704 case DW_OP_lit11:
16705 case DW_OP_lit12:
16706 case DW_OP_lit13:
16707 case DW_OP_lit14:
16708 case DW_OP_lit15:
16709 case DW_OP_lit16:
16710 case DW_OP_lit17:
16711 case DW_OP_lit18:
16712 case DW_OP_lit19:
16713 case DW_OP_lit20:
16714 case DW_OP_lit21:
16715 case DW_OP_lit22:
16716 case DW_OP_lit23:
16717 case DW_OP_lit24:
16718 case DW_OP_lit25:
16719 case DW_OP_lit26:
16720 case DW_OP_lit27:
16721 case DW_OP_lit28:
16722 case DW_OP_lit29:
16723 case DW_OP_lit30:
16724 case DW_OP_lit31:
16725 case DW_OP_breg0:
16726 case DW_OP_breg1:
16727 case DW_OP_breg2:
16728 case DW_OP_breg3:
16729 case DW_OP_breg4:
16730 case DW_OP_breg5:
16731 case DW_OP_breg6:
16732 case DW_OP_breg7:
16733 case DW_OP_breg8:
16734 case DW_OP_breg9:
16735 case DW_OP_breg10:
16736 case DW_OP_breg11:
16737 case DW_OP_breg12:
16738 case DW_OP_breg13:
16739 case DW_OP_breg14:
16740 case DW_OP_breg15:
16741 case DW_OP_breg16:
16742 case DW_OP_breg17:
16743 case DW_OP_breg18:
16744 case DW_OP_breg19:
16745 case DW_OP_breg20:
16746 case DW_OP_breg21:
16747 case DW_OP_breg22:
16748 case DW_OP_breg23:
16749 case DW_OP_breg24:
16750 case DW_OP_breg25:
16751 case DW_OP_breg26:
16752 case DW_OP_breg27:
16753 case DW_OP_breg28:
16754 case DW_OP_breg29:
16755 case DW_OP_breg30:
16756 case DW_OP_breg31:
16757 case DW_OP_fbreg:
16758 case DW_OP_push_object_address:
16759 case DW_OP_call_frame_cfa:
16760 ++frame_offset_;
16761 break;
16763 case DW_OP_drop:
16764 case DW_OP_xderef:
16765 case DW_OP_and:
16766 case DW_OP_div:
16767 case DW_OP_minus:
16768 case DW_OP_mod:
16769 case DW_OP_mul:
16770 case DW_OP_or:
16771 case DW_OP_plus:
16772 case DW_OP_shl:
16773 case DW_OP_shr:
16774 case DW_OP_shra:
16775 case DW_OP_xor:
16776 case DW_OP_bra:
16777 case DW_OP_eq:
16778 case DW_OP_ge:
16779 case DW_OP_gt:
16780 case DW_OP_le:
16781 case DW_OP_lt:
16782 case DW_OP_ne:
16783 case DW_OP_regx:
16784 case DW_OP_xderef_size:
16785 --frame_offset_;
16786 break;
16788 case DW_OP_call2:
16789 case DW_OP_call4:
16790 case DW_OP_call_ref:
16792 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16793 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16795 if (stack_usage == NULL)
16796 return false;
16797 frame_offset_ += *stack_usage;
16798 break;
16801 case DW_OP_implicit_pointer:
16802 case DW_OP_entry_value:
16803 case DW_OP_const_type:
16804 case DW_OP_regval_type:
16805 case DW_OP_deref_type:
16806 case DW_OP_convert:
16807 case DW_OP_reinterpret:
16808 case DW_OP_form_tls_address:
16809 case DW_OP_GNU_push_tls_address:
16810 case DW_OP_GNU_uninit:
16811 case DW_OP_GNU_encoded_addr:
16812 case DW_OP_GNU_implicit_pointer:
16813 case DW_OP_GNU_entry_value:
16814 case DW_OP_GNU_const_type:
16815 case DW_OP_GNU_regval_type:
16816 case DW_OP_GNU_deref_type:
16817 case DW_OP_GNU_convert:
16818 case DW_OP_GNU_reinterpret:
16819 case DW_OP_GNU_parameter_ref:
16820 /* loc_list_from_tree will probably not output these operations for
16821 size functions, so assume they will not appear here. */
16822 /* Fall through... */
16824 default:
16825 gcc_unreachable ();
16828 /* Now, follow the control flow (except subroutine calls). */
16829 switch (l->dw_loc_opc)
16831 case DW_OP_bra:
16832 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16833 frame_offsets))
16834 return false;
16835 /* Fall through. */
16837 case DW_OP_skip:
16838 l = l->dw_loc_oprnd1.v.val_loc;
16839 break;
16841 case DW_OP_stack_value:
16842 return true;
16844 default:
16845 l = l->dw_loc_next;
16846 break;
16850 return true;
16853 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16854 operations) in order to resolve the operand of DW_OP_pick operations that
16855 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16856 offset *before* LOC is executed. Return if all relocations were
16857 successful. */
16859 static bool
16860 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16861 struct dwarf_procedure_info *dpi)
16863 /* Associate to all visited operations the frame offset *before* evaluating
16864 this operation. */
16865 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16867 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16868 frame_offsets);
16871 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
16872 Return NULL if it is not possible. */
16874 static dw_die_ref
16875 function_to_dwarf_procedure (tree fndecl)
16877 struct loc_descr_context ctx;
16878 struct dwarf_procedure_info dpi;
16879 dw_die_ref dwarf_proc_die;
16880 tree tree_body = DECL_SAVED_TREE (fndecl);
16881 dw_loc_descr_ref loc_body, epilogue;
16883 tree cursor;
16884 unsigned i;
16886 /* Do not generate multiple DWARF procedures for the same function
16887 declaration. */
16888 dwarf_proc_die = lookup_decl_die (fndecl);
16889 if (dwarf_proc_die != NULL)
16890 return dwarf_proc_die;
16892 /* DWARF procedures are available starting with the DWARFv3 standard. */
16893 if (dwarf_version < 3 && dwarf_strict)
16894 return NULL;
16896 /* We handle only functions for which we still have a body, that return a
16897 supported type and that takes arguments with supported types. Note that
16898 there is no point translating functions that return nothing. */
16899 if (tree_body == NULL_TREE
16900 || DECL_RESULT (fndecl) == NULL_TREE
16901 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
16902 return NULL;
16904 for (cursor = DECL_ARGUMENTS (fndecl);
16905 cursor != NULL_TREE;
16906 cursor = TREE_CHAIN (cursor))
16907 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
16908 return NULL;
16910 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
16911 if (TREE_CODE (tree_body) != RETURN_EXPR)
16912 return NULL;
16913 tree_body = TREE_OPERAND (tree_body, 0);
16914 if (TREE_CODE (tree_body) != MODIFY_EXPR
16915 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
16916 return NULL;
16917 tree_body = TREE_OPERAND (tree_body, 1);
16919 /* Try to translate the body expression itself. Note that this will probably
16920 cause an infinite recursion if its call graph has a cycle. This is very
16921 unlikely for size functions, however, so don't bother with such things at
16922 the moment. */
16923 ctx.context_type = NULL_TREE;
16924 ctx.base_decl = NULL_TREE;
16925 ctx.dpi = &dpi;
16926 ctx.placeholder_arg = false;
16927 ctx.placeholder_seen = false;
16928 dpi.fndecl = fndecl;
16929 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
16930 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
16931 if (!loc_body)
16932 return NULL;
16934 /* After evaluating all operands in "loc_body", we should still have on the
16935 stack all arguments plus the desired function result (top of the stack).
16936 Generate code in order to keep only the result in our stack frame. */
16937 epilogue = NULL;
16938 for (i = 0; i < dpi.args_count; ++i)
16940 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
16941 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
16942 op_couple->dw_loc_next->dw_loc_next = epilogue;
16943 epilogue = op_couple;
16945 add_loc_descr (&loc_body, epilogue);
16946 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
16947 return NULL;
16949 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
16950 because they are considered useful. Now there is an epilogue, they are
16951 not anymore, so give it another try. */
16952 loc_descr_without_nops (loc_body);
16954 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
16955 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
16956 though, given that size functions do not come from source, so they should
16957 not have a dedicated DW_TAG_subprogram DIE. */
16958 dwarf_proc_die
16959 = new_dwarf_proc_die (loc_body, fndecl,
16960 get_context_die (DECL_CONTEXT (fndecl)));
16962 /* The called DWARF procedure consumes one stack slot per argument and
16963 returns one stack slot. */
16964 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
16966 return dwarf_proc_die;
16970 /* Generate Dwarf location list representing LOC.
16971 If WANT_ADDRESS is false, expression computing LOC will be computed
16972 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
16973 if WANT_ADDRESS is 2, expression computing address useable in location
16974 will be returned (i.e. DW_OP_reg can be used
16975 to refer to register values).
16977 CONTEXT provides information to customize the location descriptions
16978 generation. Its context_type field specifies what type is implicitly
16979 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
16980 will not be generated.
16982 Its DPI field determines whether we are generating a DWARF expression for a
16983 DWARF procedure, so PARM_DECL references are processed specifically.
16985 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
16986 and dpi fields were null. */
16988 static dw_loc_list_ref
16989 loc_list_from_tree_1 (tree loc, int want_address,
16990 struct loc_descr_context *context)
16992 dw_loc_descr_ref ret = NULL, ret1 = NULL;
16993 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16994 int have_address = 0;
16995 enum dwarf_location_atom op;
16997 /* ??? Most of the time we do not take proper care for sign/zero
16998 extending the values properly. Hopefully this won't be a real
16999 problem... */
17001 if (context != NULL
17002 && context->base_decl == loc
17003 && want_address == 0)
17005 if (dwarf_version >= 3 || !dwarf_strict)
17006 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17007 NULL, NULL, NULL);
17008 else
17009 return NULL;
17012 switch (TREE_CODE (loc))
17014 case ERROR_MARK:
17015 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17016 return 0;
17018 case PLACEHOLDER_EXPR:
17019 /* This case involves extracting fields from an object to determine the
17020 position of other fields. It is supposed to appear only as the first
17021 operand of COMPONENT_REF nodes and to reference precisely the type
17022 that the context allows. */
17023 if (context != NULL
17024 && TREE_TYPE (loc) == context->context_type
17025 && want_address >= 1)
17027 if (dwarf_version >= 3 || !dwarf_strict)
17029 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17030 have_address = 1;
17031 break;
17033 else
17034 return NULL;
17036 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17037 the single argument passed by consumer. */
17038 else if (context != NULL
17039 && context->placeholder_arg
17040 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17041 && want_address == 0)
17043 ret = new_loc_descr (DW_OP_pick, 0, 0);
17044 ret->frame_offset_rel = 1;
17045 context->placeholder_seen = true;
17046 break;
17048 else
17049 expansion_failed (loc, NULL_RTX,
17050 "PLACEHOLDER_EXPR for an unexpected type");
17051 break;
17053 case CALL_EXPR:
17055 const int nargs = call_expr_nargs (loc);
17056 tree callee = get_callee_fndecl (loc);
17057 int i;
17058 dw_die_ref dwarf_proc;
17060 if (callee == NULL_TREE)
17061 goto call_expansion_failed;
17063 /* We handle only functions that return an integer. */
17064 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17065 goto call_expansion_failed;
17067 dwarf_proc = function_to_dwarf_procedure (callee);
17068 if (dwarf_proc == NULL)
17069 goto call_expansion_failed;
17071 /* Evaluate arguments right-to-left so that the first argument will
17072 be the top-most one on the stack. */
17073 for (i = nargs - 1; i >= 0; --i)
17075 dw_loc_descr_ref loc_descr
17076 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17077 context);
17079 if (loc_descr == NULL)
17080 goto call_expansion_failed;
17082 add_loc_descr (&ret, loc_descr);
17085 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17086 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17087 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17088 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17089 add_loc_descr (&ret, ret1);
17090 break;
17092 call_expansion_failed:
17093 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17094 /* There are no opcodes for these operations. */
17095 return 0;
17098 case PREINCREMENT_EXPR:
17099 case PREDECREMENT_EXPR:
17100 case POSTINCREMENT_EXPR:
17101 case POSTDECREMENT_EXPR:
17102 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17103 /* There are no opcodes for these operations. */
17104 return 0;
17106 case ADDR_EXPR:
17107 /* If we already want an address, see if there is INDIRECT_REF inside
17108 e.g. for &this->field. */
17109 if (want_address)
17111 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17112 (loc, want_address == 2, context);
17113 if (list_ret)
17114 have_address = 1;
17115 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17116 && (ret = cst_pool_loc_descr (loc)))
17117 have_address = 1;
17119 /* Otherwise, process the argument and look for the address. */
17120 if (!list_ret && !ret)
17121 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17122 else
17124 if (want_address)
17125 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17126 return NULL;
17128 break;
17130 case VAR_DECL:
17131 if (DECL_THREAD_LOCAL_P (loc))
17133 rtx rtl;
17134 enum dwarf_location_atom tls_op;
17135 enum dtprel_bool dtprel = dtprel_false;
17137 if (targetm.have_tls)
17139 /* If this is not defined, we have no way to emit the
17140 data. */
17141 if (!targetm.asm_out.output_dwarf_dtprel)
17142 return 0;
17144 /* The way DW_OP_GNU_push_tls_address is specified, we
17145 can only look up addresses of objects in the current
17146 module. We used DW_OP_addr as first op, but that's
17147 wrong, because DW_OP_addr is relocated by the debug
17148 info consumer, while DW_OP_GNU_push_tls_address
17149 operand shouldn't be. */
17150 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17151 return 0;
17152 dtprel = dtprel_true;
17153 /* We check for DWARF 5 here because gdb did not implement
17154 DW_OP_form_tls_address until after 7.12. */
17155 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17156 : DW_OP_GNU_push_tls_address);
17158 else
17160 if (!targetm.emutls.debug_form_tls_address
17161 || !(dwarf_version >= 3 || !dwarf_strict))
17162 return 0;
17163 /* We stuffed the control variable into the DECL_VALUE_EXPR
17164 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17165 no longer appear in gimple code. We used the control
17166 variable in specific so that we could pick it up here. */
17167 loc = DECL_VALUE_EXPR (loc);
17168 tls_op = DW_OP_form_tls_address;
17171 rtl = rtl_for_decl_location (loc);
17172 if (rtl == NULL_RTX)
17173 return 0;
17175 if (!MEM_P (rtl))
17176 return 0;
17177 rtl = XEXP (rtl, 0);
17178 if (! CONSTANT_P (rtl))
17179 return 0;
17181 ret = new_addr_loc_descr (rtl, dtprel);
17182 ret1 = new_loc_descr (tls_op, 0, 0);
17183 add_loc_descr (&ret, ret1);
17185 have_address = 1;
17186 break;
17188 /* FALLTHRU */
17190 case PARM_DECL:
17191 if (context != NULL && context->dpi != NULL
17192 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17194 /* We are generating code for a DWARF procedure and we want to access
17195 one of its arguments: find the appropriate argument offset and let
17196 the resolve_args_picking pass compute the offset that complies
17197 with the stack frame size. */
17198 unsigned i = 0;
17199 tree cursor;
17201 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17202 cursor != NULL_TREE && cursor != loc;
17203 cursor = TREE_CHAIN (cursor), ++i)
17205 /* If we are translating a DWARF procedure, all referenced parameters
17206 must belong to the current function. */
17207 gcc_assert (cursor != NULL_TREE);
17209 ret = new_loc_descr (DW_OP_pick, i, 0);
17210 ret->frame_offset_rel = 1;
17211 break;
17213 /* FALLTHRU */
17215 case RESULT_DECL:
17216 if (DECL_HAS_VALUE_EXPR_P (loc))
17217 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17218 want_address, context);
17219 /* FALLTHRU */
17221 case FUNCTION_DECL:
17223 rtx rtl;
17224 var_loc_list *loc_list = lookup_decl_loc (loc);
17226 if (loc_list && loc_list->first)
17228 list_ret = dw_loc_list (loc_list, loc, want_address);
17229 have_address = want_address != 0;
17230 break;
17232 rtl = rtl_for_decl_location (loc);
17233 if (rtl == NULL_RTX)
17235 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17236 return 0;
17238 else if (CONST_INT_P (rtl))
17240 HOST_WIDE_INT val = INTVAL (rtl);
17241 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17242 val &= GET_MODE_MASK (DECL_MODE (loc));
17243 ret = int_loc_descriptor (val);
17245 else if (GET_CODE (rtl) == CONST_STRING)
17247 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17248 return 0;
17250 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17251 ret = new_addr_loc_descr (rtl, dtprel_false);
17252 else
17254 machine_mode mode, mem_mode;
17256 /* Certain constructs can only be represented at top-level. */
17257 if (want_address == 2)
17259 ret = loc_descriptor (rtl, VOIDmode,
17260 VAR_INIT_STATUS_INITIALIZED);
17261 have_address = 1;
17263 else
17265 mode = GET_MODE (rtl);
17266 mem_mode = VOIDmode;
17267 if (MEM_P (rtl))
17269 mem_mode = mode;
17270 mode = get_address_mode (rtl);
17271 rtl = XEXP (rtl, 0);
17272 have_address = 1;
17274 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17275 VAR_INIT_STATUS_INITIALIZED);
17277 if (!ret)
17278 expansion_failed (loc, rtl,
17279 "failed to produce loc descriptor for rtl");
17282 break;
17284 case MEM_REF:
17285 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17287 have_address = 1;
17288 goto do_plus;
17290 /* Fallthru. */
17291 case INDIRECT_REF:
17292 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17293 have_address = 1;
17294 break;
17296 case TARGET_MEM_REF:
17297 case SSA_NAME:
17298 case DEBUG_EXPR_DECL:
17299 return NULL;
17301 case COMPOUND_EXPR:
17302 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17303 context);
17305 CASE_CONVERT:
17306 case VIEW_CONVERT_EXPR:
17307 case SAVE_EXPR:
17308 case MODIFY_EXPR:
17309 case NON_LVALUE_EXPR:
17310 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17311 context);
17313 case COMPONENT_REF:
17314 case BIT_FIELD_REF:
17315 case ARRAY_REF:
17316 case ARRAY_RANGE_REF:
17317 case REALPART_EXPR:
17318 case IMAGPART_EXPR:
17320 tree obj, offset;
17321 HOST_WIDE_INT bitsize, bitpos, bytepos;
17322 machine_mode mode;
17323 int unsignedp, reversep, volatilep = 0;
17325 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17326 &unsignedp, &reversep, &volatilep);
17328 gcc_assert (obj != loc);
17330 list_ret = loc_list_from_tree_1 (obj,
17331 want_address == 2
17332 && !bitpos && !offset ? 2 : 1,
17333 context);
17334 /* TODO: We can extract value of the small expression via shifting even
17335 for nonzero bitpos. */
17336 if (list_ret == 0)
17337 return 0;
17338 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17340 expansion_failed (loc, NULL_RTX,
17341 "bitfield access");
17342 return 0;
17345 if (offset != NULL_TREE)
17347 /* Variable offset. */
17348 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17349 if (list_ret1 == 0)
17350 return 0;
17351 add_loc_list (&list_ret, list_ret1);
17352 if (!list_ret)
17353 return 0;
17354 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17357 bytepos = bitpos / BITS_PER_UNIT;
17358 if (bytepos > 0)
17359 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17360 else if (bytepos < 0)
17361 loc_list_plus_const (list_ret, bytepos);
17363 have_address = 1;
17364 break;
17367 case INTEGER_CST:
17368 if ((want_address || !tree_fits_shwi_p (loc))
17369 && (ret = cst_pool_loc_descr (loc)))
17370 have_address = 1;
17371 else if (want_address == 2
17372 && tree_fits_shwi_p (loc)
17373 && (ret = address_of_int_loc_descriptor
17374 (int_size_in_bytes (TREE_TYPE (loc)),
17375 tree_to_shwi (loc))))
17376 have_address = 1;
17377 else if (tree_fits_shwi_p (loc))
17378 ret = int_loc_descriptor (tree_to_shwi (loc));
17379 else if (tree_fits_uhwi_p (loc))
17380 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17381 else
17383 expansion_failed (loc, NULL_RTX,
17384 "Integer operand is not host integer");
17385 return 0;
17387 break;
17389 case CONSTRUCTOR:
17390 case REAL_CST:
17391 case STRING_CST:
17392 case COMPLEX_CST:
17393 if ((ret = cst_pool_loc_descr (loc)))
17394 have_address = 1;
17395 else if (TREE_CODE (loc) == CONSTRUCTOR)
17397 tree type = TREE_TYPE (loc);
17398 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17399 unsigned HOST_WIDE_INT offset = 0;
17400 unsigned HOST_WIDE_INT cnt;
17401 constructor_elt *ce;
17403 if (TREE_CODE (type) == RECORD_TYPE)
17405 /* This is very limited, but it's enough to output
17406 pointers to member functions, as long as the
17407 referenced function is defined in the current
17408 translation unit. */
17409 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17411 tree val = ce->value;
17413 tree field = ce->index;
17415 if (val)
17416 STRIP_NOPS (val);
17418 if (!field || DECL_BIT_FIELD (field))
17420 expansion_failed (loc, NULL_RTX,
17421 "bitfield in record type constructor");
17422 size = offset = (unsigned HOST_WIDE_INT)-1;
17423 ret = NULL;
17424 break;
17427 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17428 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17429 gcc_assert (pos + fieldsize <= size);
17430 if (pos < offset)
17432 expansion_failed (loc, NULL_RTX,
17433 "out-of-order fields in record constructor");
17434 size = offset = (unsigned HOST_WIDE_INT)-1;
17435 ret = NULL;
17436 break;
17438 if (pos > offset)
17440 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17441 add_loc_descr (&ret, ret1);
17442 offset = pos;
17444 if (val && fieldsize != 0)
17446 ret1 = loc_descriptor_from_tree (val, want_address, context);
17447 if (!ret1)
17449 expansion_failed (loc, NULL_RTX,
17450 "unsupported expression in field");
17451 size = offset = (unsigned HOST_WIDE_INT)-1;
17452 ret = NULL;
17453 break;
17455 add_loc_descr (&ret, ret1);
17457 if (fieldsize)
17459 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17460 add_loc_descr (&ret, ret1);
17461 offset = pos + fieldsize;
17465 if (offset != size)
17467 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17468 add_loc_descr (&ret, ret1);
17469 offset = size;
17472 have_address = !!want_address;
17474 else
17475 expansion_failed (loc, NULL_RTX,
17476 "constructor of non-record type");
17478 else
17479 /* We can construct small constants here using int_loc_descriptor. */
17480 expansion_failed (loc, NULL_RTX,
17481 "constructor or constant not in constant pool");
17482 break;
17484 case TRUTH_AND_EXPR:
17485 case TRUTH_ANDIF_EXPR:
17486 case BIT_AND_EXPR:
17487 op = DW_OP_and;
17488 goto do_binop;
17490 case TRUTH_XOR_EXPR:
17491 case BIT_XOR_EXPR:
17492 op = DW_OP_xor;
17493 goto do_binop;
17495 case TRUTH_OR_EXPR:
17496 case TRUTH_ORIF_EXPR:
17497 case BIT_IOR_EXPR:
17498 op = DW_OP_or;
17499 goto do_binop;
17501 case FLOOR_DIV_EXPR:
17502 case CEIL_DIV_EXPR:
17503 case ROUND_DIV_EXPR:
17504 case TRUNC_DIV_EXPR:
17505 case EXACT_DIV_EXPR:
17506 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17507 return 0;
17508 op = DW_OP_div;
17509 goto do_binop;
17511 case MINUS_EXPR:
17512 op = DW_OP_minus;
17513 goto do_binop;
17515 case FLOOR_MOD_EXPR:
17516 case CEIL_MOD_EXPR:
17517 case ROUND_MOD_EXPR:
17518 case TRUNC_MOD_EXPR:
17519 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17521 op = DW_OP_mod;
17522 goto do_binop;
17524 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17525 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17526 if (list_ret == 0 || list_ret1 == 0)
17527 return 0;
17529 add_loc_list (&list_ret, list_ret1);
17530 if (list_ret == 0)
17531 return 0;
17532 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17533 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17534 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17535 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17536 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17537 break;
17539 case MULT_EXPR:
17540 op = DW_OP_mul;
17541 goto do_binop;
17543 case LSHIFT_EXPR:
17544 op = DW_OP_shl;
17545 goto do_binop;
17547 case RSHIFT_EXPR:
17548 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17549 goto do_binop;
17551 case POINTER_PLUS_EXPR:
17552 case PLUS_EXPR:
17553 do_plus:
17554 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17556 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17557 smarter to encode their opposite. The DW_OP_plus_uconst operation
17558 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17559 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17560 bytes, Y being the size of the operation that pushes the opposite
17561 of the addend. So let's choose the smallest representation. */
17562 const tree tree_addend = TREE_OPERAND (loc, 1);
17563 offset_int wi_addend;
17564 HOST_WIDE_INT shwi_addend;
17565 dw_loc_descr_ref loc_naddend;
17567 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17568 if (list_ret == 0)
17569 return 0;
17571 /* Try to get the literal to push. It is the opposite of the addend,
17572 so as we rely on wrapping during DWARF evaluation, first decode
17573 the literal as a "DWARF-sized" signed number. */
17574 wi_addend = wi::to_offset (tree_addend);
17575 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17576 shwi_addend = wi_addend.to_shwi ();
17577 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17578 ? int_loc_descriptor (-shwi_addend)
17579 : NULL;
17581 if (loc_naddend != NULL
17582 && ((unsigned) size_of_uleb128 (shwi_addend)
17583 > size_of_loc_descr (loc_naddend)))
17585 add_loc_descr_to_each (list_ret, loc_naddend);
17586 add_loc_descr_to_each (list_ret,
17587 new_loc_descr (DW_OP_minus, 0, 0));
17589 else
17591 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17593 loc_naddend = loc_cur;
17594 loc_cur = loc_cur->dw_loc_next;
17595 ggc_free (loc_naddend);
17597 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17599 break;
17602 op = DW_OP_plus;
17603 goto do_binop;
17605 case LE_EXPR:
17606 op = DW_OP_le;
17607 goto do_comp_binop;
17609 case GE_EXPR:
17610 op = DW_OP_ge;
17611 goto do_comp_binop;
17613 case LT_EXPR:
17614 op = DW_OP_lt;
17615 goto do_comp_binop;
17617 case GT_EXPR:
17618 op = DW_OP_gt;
17619 goto do_comp_binop;
17621 do_comp_binop:
17622 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17624 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17625 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17626 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17627 TREE_CODE (loc));
17628 break;
17630 else
17631 goto do_binop;
17633 case EQ_EXPR:
17634 op = DW_OP_eq;
17635 goto do_binop;
17637 case NE_EXPR:
17638 op = DW_OP_ne;
17639 goto do_binop;
17641 do_binop:
17642 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17643 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17644 if (list_ret == 0 || list_ret1 == 0)
17645 return 0;
17647 add_loc_list (&list_ret, list_ret1);
17648 if (list_ret == 0)
17649 return 0;
17650 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17651 break;
17653 case TRUTH_NOT_EXPR:
17654 case BIT_NOT_EXPR:
17655 op = DW_OP_not;
17656 goto do_unop;
17658 case ABS_EXPR:
17659 op = DW_OP_abs;
17660 goto do_unop;
17662 case NEGATE_EXPR:
17663 op = DW_OP_neg;
17664 goto do_unop;
17666 do_unop:
17667 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17668 if (list_ret == 0)
17669 return 0;
17671 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17672 break;
17674 case MIN_EXPR:
17675 case MAX_EXPR:
17677 const enum tree_code code =
17678 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17680 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17681 build2 (code, integer_type_node,
17682 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17683 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17686 /* fall through */
17688 case COND_EXPR:
17690 dw_loc_descr_ref lhs
17691 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17692 dw_loc_list_ref rhs
17693 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17694 dw_loc_descr_ref bra_node, jump_node, tmp;
17696 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17697 if (list_ret == 0 || lhs == 0 || rhs == 0)
17698 return 0;
17700 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17701 add_loc_descr_to_each (list_ret, bra_node);
17703 add_loc_list (&list_ret, rhs);
17704 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17705 add_loc_descr_to_each (list_ret, jump_node);
17707 add_loc_descr_to_each (list_ret, lhs);
17708 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17709 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17711 /* ??? Need a node to point the skip at. Use a nop. */
17712 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17713 add_loc_descr_to_each (list_ret, tmp);
17714 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17715 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17717 break;
17719 case FIX_TRUNC_EXPR:
17720 return 0;
17722 default:
17723 /* Leave front-end specific codes as simply unknown. This comes
17724 up, for instance, with the C STMT_EXPR. */
17725 if ((unsigned int) TREE_CODE (loc)
17726 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17728 expansion_failed (loc, NULL_RTX,
17729 "language specific tree node");
17730 return 0;
17733 /* Otherwise this is a generic code; we should just lists all of
17734 these explicitly. We forgot one. */
17735 if (flag_checking)
17736 gcc_unreachable ();
17738 /* In a release build, we want to degrade gracefully: better to
17739 generate incomplete debugging information than to crash. */
17740 return NULL;
17743 if (!ret && !list_ret)
17744 return 0;
17746 if (want_address == 2 && !have_address
17747 && (dwarf_version >= 4 || !dwarf_strict))
17749 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17751 expansion_failed (loc, NULL_RTX,
17752 "DWARF address size mismatch");
17753 return 0;
17755 if (ret)
17756 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17757 else
17758 add_loc_descr_to_each (list_ret,
17759 new_loc_descr (DW_OP_stack_value, 0, 0));
17760 have_address = 1;
17762 /* Show if we can't fill the request for an address. */
17763 if (want_address && !have_address)
17765 expansion_failed (loc, NULL_RTX,
17766 "Want address and only have value");
17767 return 0;
17770 gcc_assert (!ret || !list_ret);
17772 /* If we've got an address and don't want one, dereference. */
17773 if (!want_address && have_address)
17775 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17777 if (size > DWARF2_ADDR_SIZE || size == -1)
17779 expansion_failed (loc, NULL_RTX,
17780 "DWARF address size mismatch");
17781 return 0;
17783 else if (size == DWARF2_ADDR_SIZE)
17784 op = DW_OP_deref;
17785 else
17786 op = DW_OP_deref_size;
17788 if (ret)
17789 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17790 else
17791 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17793 if (ret)
17794 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17796 return list_ret;
17799 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17800 expressions. */
17802 static dw_loc_list_ref
17803 loc_list_from_tree (tree loc, int want_address,
17804 struct loc_descr_context *context)
17806 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17808 for (dw_loc_list_ref loc_cur = result;
17809 loc_cur != NULL; loc_cur =
17810 loc_cur->dw_loc_next)
17811 loc_descr_without_nops (loc_cur->expr);
17812 return result;
17815 /* Same as above but return only single location expression. */
17816 static dw_loc_descr_ref
17817 loc_descriptor_from_tree (tree loc, int want_address,
17818 struct loc_descr_context *context)
17820 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17821 if (!ret)
17822 return NULL;
17823 if (ret->dw_loc_next)
17825 expansion_failed (loc, NULL_RTX,
17826 "Location list where only loc descriptor needed");
17827 return NULL;
17829 return ret->expr;
17832 /* Given a value, round it up to the lowest multiple of `boundary'
17833 which is not less than the value itself. */
17835 static inline HOST_WIDE_INT
17836 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17838 return (((value + boundary - 1) / boundary) * boundary);
17841 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17842 pointer to the declared type for the relevant field variable, or return
17843 `integer_type_node' if the given node turns out to be an
17844 ERROR_MARK node. */
17846 static inline tree
17847 field_type (const_tree decl)
17849 tree type;
17851 if (TREE_CODE (decl) == ERROR_MARK)
17852 return integer_type_node;
17854 type = DECL_BIT_FIELD_TYPE (decl);
17855 if (type == NULL_TREE)
17856 type = TREE_TYPE (decl);
17858 return type;
17861 /* Given a pointer to a tree node, return the alignment in bits for
17862 it, or else return BITS_PER_WORD if the node actually turns out to
17863 be an ERROR_MARK node. */
17865 static inline unsigned
17866 simple_type_align_in_bits (const_tree type)
17868 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
17871 static inline unsigned
17872 simple_decl_align_in_bits (const_tree decl)
17874 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
17877 /* Return the result of rounding T up to ALIGN. */
17879 static inline offset_int
17880 round_up_to_align (const offset_int &t, unsigned int align)
17882 return wi::udiv_trunc (t + align - 1, align) * align;
17885 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
17886 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
17887 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
17888 if we fail to return the size in one of these two forms. */
17890 static dw_loc_descr_ref
17891 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
17893 tree tree_size;
17894 struct loc_descr_context ctx;
17896 /* Return a constant integer in priority, if possible. */
17897 *cst_size = int_size_in_bytes (type);
17898 if (*cst_size != -1)
17899 return NULL;
17901 ctx.context_type = const_cast<tree> (type);
17902 ctx.base_decl = NULL_TREE;
17903 ctx.dpi = NULL;
17904 ctx.placeholder_arg = false;
17905 ctx.placeholder_seen = false;
17907 type = TYPE_MAIN_VARIANT (type);
17908 tree_size = TYPE_SIZE_UNIT (type);
17909 return ((tree_size != NULL_TREE)
17910 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
17911 : NULL);
17914 /* Helper structure for RECORD_TYPE processing. */
17915 struct vlr_context
17917 /* Root RECORD_TYPE. It is needed to generate data member location
17918 descriptions in variable-length records (VLR), but also to cope with
17919 variants, which are composed of nested structures multiplexed with
17920 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
17921 function processing a FIELD_DECL, it is required to be non null. */
17922 tree struct_type;
17923 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
17924 QUAL_UNION_TYPE), this holds an expression that computes the offset for
17925 this variant part as part of the root record (in storage units). For
17926 regular records, it must be NULL_TREE. */
17927 tree variant_part_offset;
17930 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
17931 addressed byte of the "containing object" for the given FIELD_DECL. If
17932 possible, return a native constant through CST_OFFSET (in which case NULL is
17933 returned); otherwise return a DWARF expression that computes the offset.
17935 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
17936 that offset is, either because the argument turns out to be a pointer to an
17937 ERROR_MARK node, or because the offset expression is too complex for us.
17939 CTX is required: see the comment for VLR_CONTEXT. */
17941 static dw_loc_descr_ref
17942 field_byte_offset (const_tree decl, struct vlr_context *ctx,
17943 HOST_WIDE_INT *cst_offset)
17945 offset_int object_offset_in_bits;
17946 offset_int object_offset_in_bytes;
17947 offset_int bitpos_int;
17948 bool is_byte_offset_cst, is_bit_offset_cst;
17949 tree tree_result;
17950 dw_loc_list_ref loc_result;
17952 *cst_offset = 0;
17954 if (TREE_CODE (decl) == ERROR_MARK)
17955 return NULL;
17956 else
17957 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
17959 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
17960 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
17962 /* We cannot handle variable bit offsets at the moment, so abort if it's the
17963 case. */
17964 if (is_bit_offset_cst)
17965 return NULL;
17967 #ifdef PCC_BITFIELD_TYPE_MATTERS
17968 /* We used to handle only constant offsets in all cases. Now, we handle
17969 properly dynamic byte offsets only when PCC bitfield type doesn't
17970 matter. */
17971 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
17973 tree type;
17974 tree field_size_tree;
17975 offset_int deepest_bitpos;
17976 offset_int field_size_in_bits;
17977 unsigned int type_align_in_bits;
17978 unsigned int decl_align_in_bits;
17979 offset_int type_size_in_bits;
17981 bitpos_int = wi::to_offset (bit_position (decl));
17982 type = field_type (decl);
17983 type_size_in_bits = offset_int_type_size_in_bits (type);
17984 type_align_in_bits = simple_type_align_in_bits (type);
17986 field_size_tree = DECL_SIZE (decl);
17988 /* The size could be unspecified if there was an error, or for
17989 a flexible array member. */
17990 if (!field_size_tree)
17991 field_size_tree = bitsize_zero_node;
17993 /* If the size of the field is not constant, use the type size. */
17994 if (TREE_CODE (field_size_tree) == INTEGER_CST)
17995 field_size_in_bits = wi::to_offset (field_size_tree);
17996 else
17997 field_size_in_bits = type_size_in_bits;
17999 decl_align_in_bits = simple_decl_align_in_bits (decl);
18001 /* The GCC front-end doesn't make any attempt to keep track of the
18002 starting bit offset (relative to the start of the containing
18003 structure type) of the hypothetical "containing object" for a
18004 bit-field. Thus, when computing the byte offset value for the
18005 start of the "containing object" of a bit-field, we must deduce
18006 this information on our own. This can be rather tricky to do in
18007 some cases. For example, handling the following structure type
18008 definition when compiling for an i386/i486 target (which only
18009 aligns long long's to 32-bit boundaries) can be very tricky:
18011 struct S { int field1; long long field2:31; };
18013 Fortunately, there is a simple rule-of-thumb which can be used
18014 in such cases. When compiling for an i386/i486, GCC will
18015 allocate 8 bytes for the structure shown above. It decides to
18016 do this based upon one simple rule for bit-field allocation.
18017 GCC allocates each "containing object" for each bit-field at
18018 the first (i.e. lowest addressed) legitimate alignment boundary
18019 (based upon the required minimum alignment for the declared
18020 type of the field) which it can possibly use, subject to the
18021 condition that there is still enough available space remaining
18022 in the containing object (when allocated at the selected point)
18023 to fully accommodate all of the bits of the bit-field itself.
18025 This simple rule makes it obvious why GCC allocates 8 bytes for
18026 each object of the structure type shown above. When looking
18027 for a place to allocate the "containing object" for `field2',
18028 the compiler simply tries to allocate a 64-bit "containing
18029 object" at each successive 32-bit boundary (starting at zero)
18030 until it finds a place to allocate that 64- bit field such that
18031 at least 31 contiguous (and previously unallocated) bits remain
18032 within that selected 64 bit field. (As it turns out, for the
18033 example above, the compiler finds it is OK to allocate the
18034 "containing object" 64-bit field at bit-offset zero within the
18035 structure type.)
18037 Here we attempt to work backwards from the limited set of facts
18038 we're given, and we try to deduce from those facts, where GCC
18039 must have believed that the containing object started (within
18040 the structure type). The value we deduce is then used (by the
18041 callers of this routine) to generate DW_AT_location and
18042 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18043 the case of DW_AT_location, regular fields as well). */
18045 /* Figure out the bit-distance from the start of the structure to
18046 the "deepest" bit of the bit-field. */
18047 deepest_bitpos = bitpos_int + field_size_in_bits;
18049 /* This is the tricky part. Use some fancy footwork to deduce
18050 where the lowest addressed bit of the containing object must
18051 be. */
18052 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18054 /* Round up to type_align by default. This works best for
18055 bitfields. */
18056 object_offset_in_bits
18057 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18059 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18061 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18063 /* Round up to decl_align instead. */
18064 object_offset_in_bits
18065 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18068 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18070 tree_result = byte_position (decl);
18071 if (ctx->variant_part_offset != NULL_TREE)
18072 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18073 ctx->variant_part_offset, tree_result));
18075 /* If the byte offset is a constant, it's simplier to handle a native
18076 constant rather than a DWARF expression. */
18077 if (TREE_CODE (tree_result) == INTEGER_CST)
18079 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18080 return NULL;
18082 struct loc_descr_context loc_ctx = {
18083 ctx->struct_type, /* context_type */
18084 NULL_TREE, /* base_decl */
18085 NULL, /* dpi */
18086 false, /* placeholder_arg */
18087 false /* placeholder_seen */
18089 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18091 /* We want a DWARF expression: abort if we only have a location list with
18092 multiple elements. */
18093 if (!loc_result || !single_element_loc_list_p (loc_result))
18094 return NULL;
18095 else
18096 return loc_result->expr;
18099 /* The following routines define various Dwarf attributes and any data
18100 associated with them. */
18102 /* Add a location description attribute value to a DIE.
18104 This emits location attributes suitable for whole variables and
18105 whole parameters. Note that the location attributes for struct fields are
18106 generated by the routine `data_member_location_attribute' below. */
18108 static inline void
18109 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18110 dw_loc_list_ref descr)
18112 if (descr == 0)
18113 return;
18114 if (single_element_loc_list_p (descr))
18115 add_AT_loc (die, attr_kind, descr->expr);
18116 else
18117 add_AT_loc_list (die, attr_kind, descr);
18120 /* Add DW_AT_accessibility attribute to DIE if needed. */
18122 static void
18123 add_accessibility_attribute (dw_die_ref die, tree decl)
18125 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18126 children, otherwise the default is DW_ACCESS_public. In DWARF2
18127 the default has always been DW_ACCESS_public. */
18128 if (TREE_PROTECTED (decl))
18129 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18130 else if (TREE_PRIVATE (decl))
18132 if (dwarf_version == 2
18133 || die->die_parent == NULL
18134 || die->die_parent->die_tag != DW_TAG_class_type)
18135 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18137 else if (dwarf_version > 2
18138 && die->die_parent
18139 && die->die_parent->die_tag == DW_TAG_class_type)
18140 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18143 /* Attach the specialized form of location attribute used for data members of
18144 struct and union types. In the special case of a FIELD_DECL node which
18145 represents a bit-field, the "offset" part of this special location
18146 descriptor must indicate the distance in bytes from the lowest-addressed
18147 byte of the containing struct or union type to the lowest-addressed byte of
18148 the "containing object" for the bit-field. (See the `field_byte_offset'
18149 function above).
18151 For any given bit-field, the "containing object" is a hypothetical object
18152 (of some integral or enum type) within which the given bit-field lives. The
18153 type of this hypothetical "containing object" is always the same as the
18154 declared type of the individual bit-field itself (for GCC anyway... the
18155 DWARF spec doesn't actually mandate this). Note that it is the size (in
18156 bytes) of the hypothetical "containing object" which will be given in the
18157 DW_AT_byte_size attribute for this bit-field. (See the
18158 `byte_size_attribute' function below.) It is also used when calculating the
18159 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18160 function below.)
18162 CTX is required: see the comment for VLR_CONTEXT. */
18164 static void
18165 add_data_member_location_attribute (dw_die_ref die,
18166 tree decl,
18167 struct vlr_context *ctx)
18169 HOST_WIDE_INT offset;
18170 dw_loc_descr_ref loc_descr = 0;
18172 if (TREE_CODE (decl) == TREE_BINFO)
18174 /* We're working on the TAG_inheritance for a base class. */
18175 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18177 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18178 aren't at a fixed offset from all (sub)objects of the same
18179 type. We need to extract the appropriate offset from our
18180 vtable. The following dwarf expression means
18182 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18184 This is specific to the V3 ABI, of course. */
18186 dw_loc_descr_ref tmp;
18188 /* Make a copy of the object address. */
18189 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18190 add_loc_descr (&loc_descr, tmp);
18192 /* Extract the vtable address. */
18193 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18194 add_loc_descr (&loc_descr, tmp);
18196 /* Calculate the address of the offset. */
18197 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18198 gcc_assert (offset < 0);
18200 tmp = int_loc_descriptor (-offset);
18201 add_loc_descr (&loc_descr, tmp);
18202 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18203 add_loc_descr (&loc_descr, tmp);
18205 /* Extract the offset. */
18206 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18207 add_loc_descr (&loc_descr, tmp);
18209 /* Add it to the object address. */
18210 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18211 add_loc_descr (&loc_descr, tmp);
18213 else
18214 offset = tree_to_shwi (BINFO_OFFSET (decl));
18216 else
18218 loc_descr = field_byte_offset (decl, ctx, &offset);
18220 /* If loc_descr is available then we know the field offset is dynamic.
18221 However, GDB does not handle dynamic field offsets very well at the
18222 moment. */
18223 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18225 loc_descr = NULL;
18226 offset = 0;
18229 /* Data member location evalutation starts with the base address on the
18230 stack. Compute the field offset and add it to this base address. */
18231 else if (loc_descr != NULL)
18232 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18235 if (! loc_descr)
18237 if (dwarf_version > 2)
18239 /* Don't need to output a location expression, just the constant. */
18240 if (offset < 0)
18241 add_AT_int (die, DW_AT_data_member_location, offset);
18242 else
18243 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18244 return;
18246 else
18248 enum dwarf_location_atom op;
18250 /* The DWARF2 standard says that we should assume that the structure
18251 address is already on the stack, so we can specify a structure
18252 field address by using DW_OP_plus_uconst. */
18253 op = DW_OP_plus_uconst;
18254 loc_descr = new_loc_descr (op, offset, 0);
18258 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18261 /* Writes integer values to dw_vec_const array. */
18263 static void
18264 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18266 while (size != 0)
18268 *dest++ = val & 0xff;
18269 val >>= 8;
18270 --size;
18274 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18276 static HOST_WIDE_INT
18277 extract_int (const unsigned char *src, unsigned int size)
18279 HOST_WIDE_INT val = 0;
18281 src += size;
18282 while (size != 0)
18284 val <<= 8;
18285 val |= *--src & 0xff;
18286 --size;
18288 return val;
18291 /* Writes wide_int values to dw_vec_const array. */
18293 static void
18294 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18296 int i;
18298 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18300 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18301 return;
18304 /* We'd have to extend this code to support odd sizes. */
18305 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18307 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18309 if (WORDS_BIG_ENDIAN)
18310 for (i = n - 1; i >= 0; i--)
18312 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18313 dest += sizeof (HOST_WIDE_INT);
18315 else
18316 for (i = 0; i < n; i++)
18318 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18319 dest += sizeof (HOST_WIDE_INT);
18323 /* Writes floating point values to dw_vec_const array. */
18325 static void
18326 insert_float (const_rtx rtl, unsigned char *array)
18328 long val[4];
18329 int i;
18331 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18333 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18334 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18336 insert_int (val[i], 4, array);
18337 array += 4;
18341 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18342 does not have a "location" either in memory or in a register. These
18343 things can arise in GNU C when a constant is passed as an actual parameter
18344 to an inlined function. They can also arise in C++ where declared
18345 constants do not necessarily get memory "homes". */
18347 static bool
18348 add_const_value_attribute (dw_die_ref die, rtx rtl)
18350 switch (GET_CODE (rtl))
18352 case CONST_INT:
18354 HOST_WIDE_INT val = INTVAL (rtl);
18356 if (val < 0)
18357 add_AT_int (die, DW_AT_const_value, val);
18358 else
18359 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18361 return true;
18363 case CONST_WIDE_INT:
18365 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
18366 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18367 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18368 wide_int w = wi::zext (w1, prec);
18369 add_AT_wide (die, DW_AT_const_value, w);
18371 return true;
18373 case CONST_DOUBLE:
18374 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18375 floating-point constant. A CONST_DOUBLE is used whenever the
18376 constant requires more than one word in order to be adequately
18377 represented. */
18379 machine_mode mode = GET_MODE (rtl);
18381 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18382 add_AT_double (die, DW_AT_const_value,
18383 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18384 else
18386 unsigned int length = GET_MODE_SIZE (mode);
18387 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18389 insert_float (rtl, array);
18390 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18393 return true;
18395 case CONST_VECTOR:
18397 machine_mode mode = GET_MODE (rtl);
18398 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18399 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18400 unsigned char *array
18401 = ggc_vec_alloc<unsigned char> (length * elt_size);
18402 unsigned int i;
18403 unsigned char *p;
18404 machine_mode imode = GET_MODE_INNER (mode);
18406 switch (GET_MODE_CLASS (mode))
18408 case MODE_VECTOR_INT:
18409 for (i = 0, p = array; i < length; i++, p += elt_size)
18411 rtx elt = CONST_VECTOR_ELT (rtl, i);
18412 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
18414 break;
18416 case MODE_VECTOR_FLOAT:
18417 for (i = 0, p = array; i < length; i++, p += elt_size)
18419 rtx elt = CONST_VECTOR_ELT (rtl, i);
18420 insert_float (elt, p);
18422 break;
18424 default:
18425 gcc_unreachable ();
18428 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18430 return true;
18432 case CONST_STRING:
18433 if (dwarf_version >= 4 || !dwarf_strict)
18435 dw_loc_descr_ref loc_result;
18436 resolve_one_addr (&rtl);
18437 rtl_addr:
18438 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18439 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18440 add_AT_loc (die, DW_AT_location, loc_result);
18441 vec_safe_push (used_rtx_array, rtl);
18442 return true;
18444 return false;
18446 case CONST:
18447 if (CONSTANT_P (XEXP (rtl, 0)))
18448 return add_const_value_attribute (die, XEXP (rtl, 0));
18449 /* FALLTHROUGH */
18450 case SYMBOL_REF:
18451 if (!const_ok_for_output (rtl))
18452 return false;
18453 /* FALLTHROUGH */
18454 case LABEL_REF:
18455 if (dwarf_version >= 4 || !dwarf_strict)
18456 goto rtl_addr;
18457 return false;
18459 case PLUS:
18460 /* In cases where an inlined instance of an inline function is passed
18461 the address of an `auto' variable (which is local to the caller) we
18462 can get a situation where the DECL_RTL of the artificial local
18463 variable (for the inlining) which acts as a stand-in for the
18464 corresponding formal parameter (of the inline function) will look
18465 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18466 exactly a compile-time constant expression, but it isn't the address
18467 of the (artificial) local variable either. Rather, it represents the
18468 *value* which the artificial local variable always has during its
18469 lifetime. We currently have no way to represent such quasi-constant
18470 values in Dwarf, so for now we just punt and generate nothing. */
18471 return false;
18473 case HIGH:
18474 case CONST_FIXED:
18475 return false;
18477 case MEM:
18478 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18479 && MEM_READONLY_P (rtl)
18480 && GET_MODE (rtl) == BLKmode)
18482 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18483 return true;
18485 return false;
18487 default:
18488 /* No other kinds of rtx should be possible here. */
18489 gcc_unreachable ();
18491 return false;
18494 /* Determine whether the evaluation of EXPR references any variables
18495 or functions which aren't otherwise used (and therefore may not be
18496 output). */
18497 static tree
18498 reference_to_unused (tree * tp, int * walk_subtrees,
18499 void * data ATTRIBUTE_UNUSED)
18501 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18502 *walk_subtrees = 0;
18504 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18505 && ! TREE_ASM_WRITTEN (*tp))
18506 return *tp;
18507 /* ??? The C++ FE emits debug information for using decls, so
18508 putting gcc_unreachable here falls over. See PR31899. For now
18509 be conservative. */
18510 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18511 return *tp;
18512 else if (VAR_P (*tp))
18514 varpool_node *node = varpool_node::get (*tp);
18515 if (!node || !node->definition)
18516 return *tp;
18518 else if (TREE_CODE (*tp) == FUNCTION_DECL
18519 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18521 /* The call graph machinery must have finished analyzing,
18522 optimizing and gimplifying the CU by now.
18523 So if *TP has no call graph node associated
18524 to it, it means *TP will not be emitted. */
18525 if (!cgraph_node::get (*tp))
18526 return *tp;
18528 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18529 return *tp;
18531 return NULL_TREE;
18534 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18535 for use in a later add_const_value_attribute call. */
18537 static rtx
18538 rtl_for_decl_init (tree init, tree type)
18540 rtx rtl = NULL_RTX;
18542 STRIP_NOPS (init);
18544 /* If a variable is initialized with a string constant without embedded
18545 zeros, build CONST_STRING. */
18546 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18548 tree enttype = TREE_TYPE (type);
18549 tree domain = TYPE_DOMAIN (type);
18550 machine_mode mode = TYPE_MODE (enttype);
18552 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18553 && domain
18554 && integer_zerop (TYPE_MIN_VALUE (domain))
18555 && compare_tree_int (TYPE_MAX_VALUE (domain),
18556 TREE_STRING_LENGTH (init) - 1) == 0
18557 && ((size_t) TREE_STRING_LENGTH (init)
18558 == strlen (TREE_STRING_POINTER (init)) + 1))
18560 rtl = gen_rtx_CONST_STRING (VOIDmode,
18561 ggc_strdup (TREE_STRING_POINTER (init)));
18562 rtl = gen_rtx_MEM (BLKmode, rtl);
18563 MEM_READONLY_P (rtl) = 1;
18566 /* Other aggregates, and complex values, could be represented using
18567 CONCAT: FIXME! */
18568 else if (AGGREGATE_TYPE_P (type)
18569 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18570 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18571 || TREE_CODE (type) == COMPLEX_TYPE)
18573 /* Vectors only work if their mode is supported by the target.
18574 FIXME: generic vectors ought to work too. */
18575 else if (TREE_CODE (type) == VECTOR_TYPE
18576 && !VECTOR_MODE_P (TYPE_MODE (type)))
18578 /* If the initializer is something that we know will expand into an
18579 immediate RTL constant, expand it now. We must be careful not to
18580 reference variables which won't be output. */
18581 else if (initializer_constant_valid_p (init, type)
18582 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18584 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18585 possible. */
18586 if (TREE_CODE (type) == VECTOR_TYPE)
18587 switch (TREE_CODE (init))
18589 case VECTOR_CST:
18590 break;
18591 case CONSTRUCTOR:
18592 if (TREE_CONSTANT (init))
18594 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18595 bool constant_p = true;
18596 tree value;
18597 unsigned HOST_WIDE_INT ix;
18599 /* Even when ctor is constant, it might contain non-*_CST
18600 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18601 belong into VECTOR_CST nodes. */
18602 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18603 if (!CONSTANT_CLASS_P (value))
18605 constant_p = false;
18606 break;
18609 if (constant_p)
18611 init = build_vector_from_ctor (type, elts);
18612 break;
18615 /* FALLTHRU */
18617 default:
18618 return NULL;
18621 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18623 /* If expand_expr returns a MEM, it wasn't immediate. */
18624 gcc_assert (!rtl || !MEM_P (rtl));
18627 return rtl;
18630 /* Generate RTL for the variable DECL to represent its location. */
18632 static rtx
18633 rtl_for_decl_location (tree decl)
18635 rtx rtl;
18637 /* Here we have to decide where we are going to say the parameter "lives"
18638 (as far as the debugger is concerned). We only have a couple of
18639 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18641 DECL_RTL normally indicates where the parameter lives during most of the
18642 activation of the function. If optimization is enabled however, this
18643 could be either NULL or else a pseudo-reg. Both of those cases indicate
18644 that the parameter doesn't really live anywhere (as far as the code
18645 generation parts of GCC are concerned) during most of the function's
18646 activation. That will happen (for example) if the parameter is never
18647 referenced within the function.
18649 We could just generate a location descriptor here for all non-NULL
18650 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18651 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18652 where DECL_RTL is NULL or is a pseudo-reg.
18654 Note however that we can only get away with using DECL_INCOMING_RTL as
18655 a backup substitute for DECL_RTL in certain limited cases. In cases
18656 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18657 we can be sure that the parameter was passed using the same type as it is
18658 declared to have within the function, and that its DECL_INCOMING_RTL
18659 points us to a place where a value of that type is passed.
18661 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18662 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18663 because in these cases DECL_INCOMING_RTL points us to a value of some
18664 type which is *different* from the type of the parameter itself. Thus,
18665 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18666 such cases, the debugger would end up (for example) trying to fetch a
18667 `float' from a place which actually contains the first part of a
18668 `double'. That would lead to really incorrect and confusing
18669 output at debug-time.
18671 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18672 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18673 are a couple of exceptions however. On little-endian machines we can
18674 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18675 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18676 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18677 when (on a little-endian machine) a non-prototyped function has a
18678 parameter declared to be of type `short' or `char'. In such cases,
18679 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18680 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18681 passed `int' value. If the debugger then uses that address to fetch
18682 a `short' or a `char' (on a little-endian machine) the result will be
18683 the correct data, so we allow for such exceptional cases below.
18685 Note that our goal here is to describe the place where the given formal
18686 parameter lives during most of the function's activation (i.e. between the
18687 end of the prologue and the start of the epilogue). We'll do that as best
18688 as we can. Note however that if the given formal parameter is modified
18689 sometime during the execution of the function, then a stack backtrace (at
18690 debug-time) will show the function as having been called with the *new*
18691 value rather than the value which was originally passed in. This happens
18692 rarely enough that it is not a major problem, but it *is* a problem, and
18693 I'd like to fix it.
18695 A future version of dwarf2out.c may generate two additional attributes for
18696 any given DW_TAG_formal_parameter DIE which will describe the "passed
18697 type" and the "passed location" for the given formal parameter in addition
18698 to the attributes we now generate to indicate the "declared type" and the
18699 "active location" for each parameter. This additional set of attributes
18700 could be used by debuggers for stack backtraces. Separately, note that
18701 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18702 This happens (for example) for inlined-instances of inline function formal
18703 parameters which are never referenced. This really shouldn't be
18704 happening. All PARM_DECL nodes should get valid non-NULL
18705 DECL_INCOMING_RTL values. FIXME. */
18707 /* Use DECL_RTL as the "location" unless we find something better. */
18708 rtl = DECL_RTL_IF_SET (decl);
18710 /* When generating abstract instances, ignore everything except
18711 constants, symbols living in memory, and symbols living in
18712 fixed registers. */
18713 if (! reload_completed)
18715 if (rtl
18716 && (CONSTANT_P (rtl)
18717 || (MEM_P (rtl)
18718 && CONSTANT_P (XEXP (rtl, 0)))
18719 || (REG_P (rtl)
18720 && VAR_P (decl)
18721 && TREE_STATIC (decl))))
18723 rtl = targetm.delegitimize_address (rtl);
18724 return rtl;
18726 rtl = NULL_RTX;
18728 else if (TREE_CODE (decl) == PARM_DECL)
18730 if (rtl == NULL_RTX
18731 || is_pseudo_reg (rtl)
18732 || (MEM_P (rtl)
18733 && is_pseudo_reg (XEXP (rtl, 0))
18734 && DECL_INCOMING_RTL (decl)
18735 && MEM_P (DECL_INCOMING_RTL (decl))
18736 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18738 tree declared_type = TREE_TYPE (decl);
18739 tree passed_type = DECL_ARG_TYPE (decl);
18740 machine_mode dmode = TYPE_MODE (declared_type);
18741 machine_mode pmode = TYPE_MODE (passed_type);
18743 /* This decl represents a formal parameter which was optimized out.
18744 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18745 all cases where (rtl == NULL_RTX) just below. */
18746 if (dmode == pmode)
18747 rtl = DECL_INCOMING_RTL (decl);
18748 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18749 && SCALAR_INT_MODE_P (dmode)
18750 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18751 && DECL_INCOMING_RTL (decl))
18753 rtx inc = DECL_INCOMING_RTL (decl);
18754 if (REG_P (inc))
18755 rtl = inc;
18756 else if (MEM_P (inc))
18758 if (BYTES_BIG_ENDIAN)
18759 rtl = adjust_address_nv (inc, dmode,
18760 GET_MODE_SIZE (pmode)
18761 - GET_MODE_SIZE (dmode));
18762 else
18763 rtl = inc;
18768 /* If the parm was passed in registers, but lives on the stack, then
18769 make a big endian correction if the mode of the type of the
18770 parameter is not the same as the mode of the rtl. */
18771 /* ??? This is the same series of checks that are made in dbxout.c before
18772 we reach the big endian correction code there. It isn't clear if all
18773 of these checks are necessary here, but keeping them all is the safe
18774 thing to do. */
18775 else if (MEM_P (rtl)
18776 && XEXP (rtl, 0) != const0_rtx
18777 && ! CONSTANT_P (XEXP (rtl, 0))
18778 /* Not passed in memory. */
18779 && !MEM_P (DECL_INCOMING_RTL (decl))
18780 /* Not passed by invisible reference. */
18781 && (!REG_P (XEXP (rtl, 0))
18782 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18783 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18784 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18785 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18786 #endif
18788 /* Big endian correction check. */
18789 && BYTES_BIG_ENDIAN
18790 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18791 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18792 < UNITS_PER_WORD))
18794 machine_mode addr_mode = get_address_mode (rtl);
18795 int offset = (UNITS_PER_WORD
18796 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18798 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18799 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18802 else if (VAR_P (decl)
18803 && rtl
18804 && MEM_P (rtl)
18805 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18806 && BYTES_BIG_ENDIAN)
18808 machine_mode addr_mode = get_address_mode (rtl);
18809 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18810 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18812 /* If a variable is declared "register" yet is smaller than
18813 a register, then if we store the variable to memory, it
18814 looks like we're storing a register-sized value, when in
18815 fact we are not. We need to adjust the offset of the
18816 storage location to reflect the actual value's bytes,
18817 else gdb will not be able to display it. */
18818 if (rsize > dsize)
18819 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18820 plus_constant (addr_mode, XEXP (rtl, 0),
18821 rsize - dsize));
18824 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
18825 and will have been substituted directly into all expressions that use it.
18826 C does not have such a concept, but C++ and other languages do. */
18827 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
18828 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
18830 if (rtl)
18831 rtl = targetm.delegitimize_address (rtl);
18833 /* If we don't look past the constant pool, we risk emitting a
18834 reference to a constant pool entry that isn't referenced from
18835 code, and thus is not emitted. */
18836 if (rtl)
18837 rtl = avoid_constant_pool_reference (rtl);
18839 /* Try harder to get a rtl. If this symbol ends up not being emitted
18840 in the current CU, resolve_addr will remove the expression referencing
18841 it. */
18842 if (rtl == NULL_RTX
18843 && VAR_P (decl)
18844 && !DECL_EXTERNAL (decl)
18845 && TREE_STATIC (decl)
18846 && DECL_NAME (decl)
18847 && !DECL_HARD_REGISTER (decl)
18848 && DECL_MODE (decl) != VOIDmode)
18850 rtl = make_decl_rtl_for_debug (decl);
18851 if (!MEM_P (rtl)
18852 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
18853 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
18854 rtl = NULL_RTX;
18857 return rtl;
18860 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
18861 returned. If so, the decl for the COMMON block is returned, and the
18862 value is the offset into the common block for the symbol. */
18864 static tree
18865 fortran_common (tree decl, HOST_WIDE_INT *value)
18867 tree val_expr, cvar;
18868 machine_mode mode;
18869 HOST_WIDE_INT bitsize, bitpos;
18870 tree offset;
18871 int unsignedp, reversep, volatilep = 0;
18873 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
18874 it does not have a value (the offset into the common area), or if it
18875 is thread local (as opposed to global) then it isn't common, and shouldn't
18876 be handled as such. */
18877 if (!VAR_P (decl)
18878 || !TREE_STATIC (decl)
18879 || !DECL_HAS_VALUE_EXPR_P (decl)
18880 || !is_fortran ())
18881 return NULL_TREE;
18883 val_expr = DECL_VALUE_EXPR (decl);
18884 if (TREE_CODE (val_expr) != COMPONENT_REF)
18885 return NULL_TREE;
18887 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
18888 &unsignedp, &reversep, &volatilep);
18890 if (cvar == NULL_TREE
18891 || !VAR_P (cvar)
18892 || DECL_ARTIFICIAL (cvar)
18893 || !TREE_PUBLIC (cvar))
18894 return NULL_TREE;
18896 *value = 0;
18897 if (offset != NULL)
18899 if (!tree_fits_shwi_p (offset))
18900 return NULL_TREE;
18901 *value = tree_to_shwi (offset);
18903 if (bitpos != 0)
18904 *value += bitpos / BITS_PER_UNIT;
18906 return cvar;
18909 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
18910 data attribute for a variable or a parameter. We generate the
18911 DW_AT_const_value attribute only in those cases where the given variable
18912 or parameter does not have a true "location" either in memory or in a
18913 register. This can happen (for example) when a constant is passed as an
18914 actual argument in a call to an inline function. (It's possible that
18915 these things can crop up in other ways also.) Note that one type of
18916 constant value which can be passed into an inlined function is a constant
18917 pointer. This can happen for example if an actual argument in an inlined
18918 function call evaluates to a compile-time constant address.
18920 CACHE_P is true if it is worth caching the location list for DECL,
18921 so that future calls can reuse it rather than regenerate it from scratch.
18922 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
18923 since we will need to refer to them each time the function is inlined. */
18925 static bool
18926 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
18928 rtx rtl;
18929 dw_loc_list_ref list;
18930 var_loc_list *loc_list;
18931 cached_dw_loc_list *cache;
18933 if (early_dwarf)
18934 return false;
18936 if (TREE_CODE (decl) == ERROR_MARK)
18937 return false;
18939 if (get_AT (die, DW_AT_location)
18940 || get_AT (die, DW_AT_const_value))
18941 return true;
18943 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
18944 || TREE_CODE (decl) == RESULT_DECL);
18946 /* Try to get some constant RTL for this decl, and use that as the value of
18947 the location. */
18949 rtl = rtl_for_decl_location (decl);
18950 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18951 && add_const_value_attribute (die, rtl))
18952 return true;
18954 /* See if we have single element location list that is equivalent to
18955 a constant value. That way we are better to use add_const_value_attribute
18956 rather than expanding constant value equivalent. */
18957 loc_list = lookup_decl_loc (decl);
18958 if (loc_list
18959 && loc_list->first
18960 && loc_list->first->next == NULL
18961 && NOTE_P (loc_list->first->loc)
18962 && NOTE_VAR_LOCATION (loc_list->first->loc)
18963 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
18965 struct var_loc_node *node;
18967 node = loc_list->first;
18968 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
18969 if (GET_CODE (rtl) == EXPR_LIST)
18970 rtl = XEXP (rtl, 0);
18971 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18972 && add_const_value_attribute (die, rtl))
18973 return true;
18975 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
18976 list several times. See if we've already cached the contents. */
18977 list = NULL;
18978 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
18979 cache_p = false;
18980 if (cache_p)
18982 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
18983 if (cache)
18984 list = cache->loc_list;
18986 if (list == NULL)
18988 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
18989 NULL);
18990 /* It is usually worth caching this result if the decl is from
18991 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
18992 if (cache_p && list && list->dw_loc_next)
18994 cached_dw_loc_list **slot
18995 = cached_dw_loc_list_table->find_slot_with_hash (decl,
18996 DECL_UID (decl),
18997 INSERT);
18998 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
18999 cache->decl_id = DECL_UID (decl);
19000 cache->loc_list = list;
19001 *slot = cache;
19004 if (list)
19006 add_AT_location_description (die, DW_AT_location, list);
19007 return true;
19009 /* None of that worked, so it must not really have a location;
19010 try adding a constant value attribute from the DECL_INITIAL. */
19011 return tree_add_const_value_attribute_for_decl (die, decl);
19014 /* Helper function for tree_add_const_value_attribute. Natively encode
19015 initializer INIT into an array. Return true if successful. */
19017 static bool
19018 native_encode_initializer (tree init, unsigned char *array, int size)
19020 tree type;
19022 if (init == NULL_TREE)
19023 return false;
19025 STRIP_NOPS (init);
19026 switch (TREE_CODE (init))
19028 case STRING_CST:
19029 type = TREE_TYPE (init);
19030 if (TREE_CODE (type) == ARRAY_TYPE)
19032 tree enttype = TREE_TYPE (type);
19033 machine_mode mode = TYPE_MODE (enttype);
19035 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19036 return false;
19037 if (int_size_in_bytes (type) != size)
19038 return false;
19039 if (size > TREE_STRING_LENGTH (init))
19041 memcpy (array, TREE_STRING_POINTER (init),
19042 TREE_STRING_LENGTH (init));
19043 memset (array + TREE_STRING_LENGTH (init),
19044 '\0', size - TREE_STRING_LENGTH (init));
19046 else
19047 memcpy (array, TREE_STRING_POINTER (init), size);
19048 return true;
19050 return false;
19051 case CONSTRUCTOR:
19052 type = TREE_TYPE (init);
19053 if (int_size_in_bytes (type) != size)
19054 return false;
19055 if (TREE_CODE (type) == ARRAY_TYPE)
19057 HOST_WIDE_INT min_index;
19058 unsigned HOST_WIDE_INT cnt;
19059 int curpos = 0, fieldsize;
19060 constructor_elt *ce;
19062 if (TYPE_DOMAIN (type) == NULL_TREE
19063 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19064 return false;
19066 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19067 if (fieldsize <= 0)
19068 return false;
19070 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19071 memset (array, '\0', size);
19072 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19074 tree val = ce->value;
19075 tree index = ce->index;
19076 int pos = curpos;
19077 if (index && TREE_CODE (index) == RANGE_EXPR)
19078 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19079 * fieldsize;
19080 else if (index)
19081 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19083 if (val)
19085 STRIP_NOPS (val);
19086 if (!native_encode_initializer (val, array + pos, fieldsize))
19087 return false;
19089 curpos = pos + fieldsize;
19090 if (index && TREE_CODE (index) == RANGE_EXPR)
19092 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19093 - tree_to_shwi (TREE_OPERAND (index, 0));
19094 while (count-- > 0)
19096 if (val)
19097 memcpy (array + curpos, array + pos, fieldsize);
19098 curpos += fieldsize;
19101 gcc_assert (curpos <= size);
19103 return true;
19105 else if (TREE_CODE (type) == RECORD_TYPE
19106 || TREE_CODE (type) == UNION_TYPE)
19108 tree field = NULL_TREE;
19109 unsigned HOST_WIDE_INT cnt;
19110 constructor_elt *ce;
19112 if (int_size_in_bytes (type) != size)
19113 return false;
19115 if (TREE_CODE (type) == RECORD_TYPE)
19116 field = TYPE_FIELDS (type);
19118 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19120 tree val = ce->value;
19121 int pos, fieldsize;
19123 if (ce->index != 0)
19124 field = ce->index;
19126 if (val)
19127 STRIP_NOPS (val);
19129 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19130 return false;
19132 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19133 && TYPE_DOMAIN (TREE_TYPE (field))
19134 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19135 return false;
19136 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19137 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19138 return false;
19139 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19140 pos = int_byte_position (field);
19141 gcc_assert (pos + fieldsize <= size);
19142 if (val && fieldsize != 0
19143 && !native_encode_initializer (val, array + pos, fieldsize))
19144 return false;
19146 return true;
19148 return false;
19149 case VIEW_CONVERT_EXPR:
19150 case NON_LVALUE_EXPR:
19151 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19152 default:
19153 return native_encode_expr (init, array, size) == size;
19157 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19158 attribute is the const value T. */
19160 static bool
19161 tree_add_const_value_attribute (dw_die_ref die, tree t)
19163 tree init;
19164 tree type = TREE_TYPE (t);
19165 rtx rtl;
19167 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19168 return false;
19170 init = t;
19171 gcc_assert (!DECL_P (init));
19173 if (! early_dwarf)
19175 rtl = rtl_for_decl_init (init, type);
19176 if (rtl)
19177 return add_const_value_attribute (die, rtl);
19179 /* If the host and target are sane, try harder. */
19180 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19181 && initializer_constant_valid_p (init, type))
19183 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19184 if (size > 0 && (int) size == size)
19186 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19188 if (native_encode_initializer (init, array, size))
19190 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19191 return true;
19193 ggc_free (array);
19196 return false;
19199 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19200 attribute is the const value of T, where T is an integral constant
19201 variable with static storage duration
19202 (so it can't be a PARM_DECL or a RESULT_DECL). */
19204 static bool
19205 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19208 if (!decl
19209 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19210 || (VAR_P (decl) && !TREE_STATIC (decl)))
19211 return false;
19213 if (TREE_READONLY (decl)
19214 && ! TREE_THIS_VOLATILE (decl)
19215 && DECL_INITIAL (decl))
19216 /* OK */;
19217 else
19218 return false;
19220 /* Don't add DW_AT_const_value if abstract origin already has one. */
19221 if (get_AT (var_die, DW_AT_const_value))
19222 return false;
19224 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19227 /* Convert the CFI instructions for the current function into a
19228 location list. This is used for DW_AT_frame_base when we targeting
19229 a dwarf2 consumer that does not support the dwarf3
19230 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19231 expressions. */
19233 static dw_loc_list_ref
19234 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19236 int ix;
19237 dw_fde_ref fde;
19238 dw_loc_list_ref list, *list_tail;
19239 dw_cfi_ref cfi;
19240 dw_cfa_location last_cfa, next_cfa;
19241 const char *start_label, *last_label, *section;
19242 dw_cfa_location remember;
19244 fde = cfun->fde;
19245 gcc_assert (fde != NULL);
19247 section = secname_for_decl (current_function_decl);
19248 list_tail = &list;
19249 list = NULL;
19251 memset (&next_cfa, 0, sizeof (next_cfa));
19252 next_cfa.reg = INVALID_REGNUM;
19253 remember = next_cfa;
19255 start_label = fde->dw_fde_begin;
19257 /* ??? Bald assumption that the CIE opcode list does not contain
19258 advance opcodes. */
19259 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19260 lookup_cfa_1 (cfi, &next_cfa, &remember);
19262 last_cfa = next_cfa;
19263 last_label = start_label;
19265 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19267 /* If the first partition contained no CFI adjustments, the
19268 CIE opcodes apply to the whole first partition. */
19269 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19270 fde->dw_fde_begin, fde->dw_fde_end, section);
19271 list_tail =&(*list_tail)->dw_loc_next;
19272 start_label = last_label = fde->dw_fde_second_begin;
19275 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19277 switch (cfi->dw_cfi_opc)
19279 case DW_CFA_set_loc:
19280 case DW_CFA_advance_loc1:
19281 case DW_CFA_advance_loc2:
19282 case DW_CFA_advance_loc4:
19283 if (!cfa_equal_p (&last_cfa, &next_cfa))
19285 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19286 start_label, last_label, section);
19288 list_tail = &(*list_tail)->dw_loc_next;
19289 last_cfa = next_cfa;
19290 start_label = last_label;
19292 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19293 break;
19295 case DW_CFA_advance_loc:
19296 /* The encoding is complex enough that we should never emit this. */
19297 gcc_unreachable ();
19299 default:
19300 lookup_cfa_1 (cfi, &next_cfa, &remember);
19301 break;
19303 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19305 if (!cfa_equal_p (&last_cfa, &next_cfa))
19307 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19308 start_label, last_label, section);
19310 list_tail = &(*list_tail)->dw_loc_next;
19311 last_cfa = next_cfa;
19312 start_label = last_label;
19314 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19315 start_label, fde->dw_fde_end, section);
19316 list_tail = &(*list_tail)->dw_loc_next;
19317 start_label = last_label = fde->dw_fde_second_begin;
19321 if (!cfa_equal_p (&last_cfa, &next_cfa))
19323 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19324 start_label, last_label, section);
19325 list_tail = &(*list_tail)->dw_loc_next;
19326 start_label = last_label;
19329 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19330 start_label,
19331 fde->dw_fde_second_begin
19332 ? fde->dw_fde_second_end : fde->dw_fde_end,
19333 section);
19335 if (list && list->dw_loc_next)
19336 gen_llsym (list);
19338 return list;
19341 /* Compute a displacement from the "steady-state frame pointer" to the
19342 frame base (often the same as the CFA), and store it in
19343 frame_pointer_fb_offset. OFFSET is added to the displacement
19344 before the latter is negated. */
19346 static void
19347 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19349 rtx reg, elim;
19351 #ifdef FRAME_POINTER_CFA_OFFSET
19352 reg = frame_pointer_rtx;
19353 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19354 #else
19355 reg = arg_pointer_rtx;
19356 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19357 #endif
19359 elim = (ira_use_lra_p
19360 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19361 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19362 if (GET_CODE (elim) == PLUS)
19364 offset += INTVAL (XEXP (elim, 1));
19365 elim = XEXP (elim, 0);
19368 frame_pointer_fb_offset = -offset;
19370 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19371 in which to eliminate. This is because it's stack pointer isn't
19372 directly accessible as a register within the ISA. To work around
19373 this, assume that while we cannot provide a proper value for
19374 frame_pointer_fb_offset, we won't need one either. */
19375 frame_pointer_fb_offset_valid
19376 = ((SUPPORTS_STACK_ALIGNMENT
19377 && (elim == hard_frame_pointer_rtx
19378 || elim == stack_pointer_rtx))
19379 || elim == (frame_pointer_needed
19380 ? hard_frame_pointer_rtx
19381 : stack_pointer_rtx));
19384 /* Generate a DW_AT_name attribute given some string value to be included as
19385 the value of the attribute. */
19387 static void
19388 add_name_attribute (dw_die_ref die, const char *name_string)
19390 if (name_string != NULL && *name_string != 0)
19392 if (demangle_name_func)
19393 name_string = (*demangle_name_func) (name_string);
19395 add_AT_string (die, DW_AT_name, name_string);
19399 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19400 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19401 of TYPE accordingly.
19403 ??? This is a temporary measure until after we're able to generate
19404 regular DWARF for the complex Ada type system. */
19406 static void
19407 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19408 dw_die_ref context_die)
19410 tree dtype;
19411 dw_die_ref dtype_die;
19413 if (!lang_hooks.types.descriptive_type)
19414 return;
19416 dtype = lang_hooks.types.descriptive_type (type);
19417 if (!dtype)
19418 return;
19420 dtype_die = lookup_type_die (dtype);
19421 if (!dtype_die)
19423 gen_type_die (dtype, context_die);
19424 dtype_die = lookup_type_die (dtype);
19425 gcc_assert (dtype_die);
19428 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19431 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19433 static const char *
19434 comp_dir_string (void)
19436 const char *wd;
19437 char *wd1;
19438 static const char *cached_wd = NULL;
19440 if (cached_wd != NULL)
19441 return cached_wd;
19443 wd = get_src_pwd ();
19444 if (wd == NULL)
19445 return NULL;
19447 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19449 int wdlen;
19451 wdlen = strlen (wd);
19452 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19453 strcpy (wd1, wd);
19454 wd1 [wdlen] = DIR_SEPARATOR;
19455 wd1 [wdlen + 1] = 0;
19456 wd = wd1;
19459 cached_wd = remap_debug_filename (wd);
19460 return cached_wd;
19463 /* Generate a DW_AT_comp_dir attribute for DIE. */
19465 static void
19466 add_comp_dir_attribute (dw_die_ref die)
19468 const char * wd = comp_dir_string ();
19469 if (wd != NULL)
19470 add_AT_string (die, DW_AT_comp_dir, wd);
19473 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19474 pointer computation, ...), output a representation for that bound according
19475 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19476 loc_list_from_tree for the meaning of CONTEXT. */
19478 static void
19479 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19480 int forms, struct loc_descr_context *context)
19482 dw_die_ref context_die, decl_die;
19483 dw_loc_list_ref list;
19484 bool strip_conversions = true;
19485 bool placeholder_seen = false;
19487 while (strip_conversions)
19488 switch (TREE_CODE (value))
19490 case ERROR_MARK:
19491 case SAVE_EXPR:
19492 return;
19494 CASE_CONVERT:
19495 case VIEW_CONVERT_EXPR:
19496 value = TREE_OPERAND (value, 0);
19497 break;
19499 default:
19500 strip_conversions = false;
19501 break;
19504 /* If possible and permitted, output the attribute as a constant. */
19505 if ((forms & dw_scalar_form_constant) != 0
19506 && TREE_CODE (value) == INTEGER_CST)
19508 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19510 /* If HOST_WIDE_INT is big enough then represent the bound as
19511 a constant value. We need to choose a form based on
19512 whether the type is signed or unsigned. We cannot just
19513 call add_AT_unsigned if the value itself is positive
19514 (add_AT_unsigned might add the unsigned value encoded as
19515 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19516 bounds type and then sign extend any unsigned values found
19517 for signed types. This is needed only for
19518 DW_AT_{lower,upper}_bound, since for most other attributes,
19519 consumers will treat DW_FORM_data[1248] as unsigned values,
19520 regardless of the underlying type. */
19521 if (prec <= HOST_BITS_PER_WIDE_INT
19522 || tree_fits_uhwi_p (value))
19524 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19525 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19526 else
19527 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19529 else
19530 /* Otherwise represent the bound as an unsigned value with
19531 the precision of its type. The precision and signedness
19532 of the type will be necessary to re-interpret it
19533 unambiguously. */
19534 add_AT_wide (die, attr, value);
19535 return;
19538 /* Otherwise, if it's possible and permitted too, output a reference to
19539 another DIE. */
19540 if ((forms & dw_scalar_form_reference) != 0)
19542 tree decl = NULL_TREE;
19544 /* Some type attributes reference an outer type. For instance, the upper
19545 bound of an array may reference an embedding record (this happens in
19546 Ada). */
19547 if (TREE_CODE (value) == COMPONENT_REF
19548 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19549 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19550 decl = TREE_OPERAND (value, 1);
19552 else if (VAR_P (value)
19553 || TREE_CODE (value) == PARM_DECL
19554 || TREE_CODE (value) == RESULT_DECL)
19555 decl = value;
19557 if (decl != NULL_TREE)
19559 dw_die_ref decl_die = lookup_decl_die (decl);
19561 /* ??? Can this happen, or should the variable have been bound
19562 first? Probably it can, since I imagine that we try to create
19563 the types of parameters in the order in which they exist in
19564 the list, and won't have created a forward reference to a
19565 later parameter. */
19566 if (decl_die != NULL)
19568 add_AT_die_ref (die, attr, decl_die);
19569 return;
19574 /* Last chance: try to create a stack operation procedure to evaluate the
19575 value. Do nothing if even that is not possible or permitted. */
19576 if ((forms & dw_scalar_form_exprloc) == 0)
19577 return;
19579 list = loc_list_from_tree (value, 2, context);
19580 if (context && context->placeholder_arg)
19582 placeholder_seen = context->placeholder_seen;
19583 context->placeholder_seen = false;
19585 if (list == NULL || single_element_loc_list_p (list))
19587 /* If this attribute is not a reference nor constant, it is
19588 a DWARF expression rather than location description. For that
19589 loc_list_from_tree (value, 0, &context) is needed. */
19590 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19591 if (list2 && single_element_loc_list_p (list2))
19593 if (placeholder_seen)
19595 struct dwarf_procedure_info dpi;
19596 dpi.fndecl = NULL_TREE;
19597 dpi.args_count = 1;
19598 if (!resolve_args_picking (list2->expr, 1, &dpi))
19599 return;
19601 add_AT_loc (die, attr, list2->expr);
19602 return;
19606 /* If that failed to give a single element location list, fall back to
19607 outputting this as a reference... still if permitted. */
19608 if (list == NULL
19609 || (forms & dw_scalar_form_reference) == 0
19610 || placeholder_seen)
19611 return;
19613 if (current_function_decl == 0)
19614 context_die = comp_unit_die ();
19615 else
19616 context_die = lookup_decl_die (current_function_decl);
19618 decl_die = new_die (DW_TAG_variable, context_die, value);
19619 add_AT_flag (decl_die, DW_AT_artificial, 1);
19620 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19621 context_die);
19622 add_AT_location_description (decl_die, DW_AT_location, list);
19623 add_AT_die_ref (die, attr, decl_die);
19626 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19627 default. */
19629 static int
19630 lower_bound_default (void)
19632 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19634 case DW_LANG_C:
19635 case DW_LANG_C89:
19636 case DW_LANG_C99:
19637 case DW_LANG_C11:
19638 case DW_LANG_C_plus_plus:
19639 case DW_LANG_C_plus_plus_11:
19640 case DW_LANG_C_plus_plus_14:
19641 case DW_LANG_ObjC:
19642 case DW_LANG_ObjC_plus_plus:
19643 case DW_LANG_Java:
19644 return 0;
19645 case DW_LANG_Fortran77:
19646 case DW_LANG_Fortran90:
19647 case DW_LANG_Fortran95:
19648 case DW_LANG_Fortran03:
19649 case DW_LANG_Fortran08:
19650 return 1;
19651 case DW_LANG_UPC:
19652 case DW_LANG_D:
19653 case DW_LANG_Python:
19654 return dwarf_version >= 4 ? 0 : -1;
19655 case DW_LANG_Ada95:
19656 case DW_LANG_Ada83:
19657 case DW_LANG_Cobol74:
19658 case DW_LANG_Cobol85:
19659 case DW_LANG_Pascal83:
19660 case DW_LANG_Modula2:
19661 case DW_LANG_PLI:
19662 return dwarf_version >= 4 ? 1 : -1;
19663 default:
19664 return -1;
19668 /* Given a tree node describing an array bound (either lower or upper) output
19669 a representation for that bound. */
19671 static void
19672 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19673 tree bound, struct loc_descr_context *context)
19675 int dflt;
19677 while (1)
19678 switch (TREE_CODE (bound))
19680 /* Strip all conversions. */
19681 CASE_CONVERT:
19682 case VIEW_CONVERT_EXPR:
19683 bound = TREE_OPERAND (bound, 0);
19684 break;
19686 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19687 are even omitted when they are the default. */
19688 case INTEGER_CST:
19689 /* If the value for this bound is the default one, we can even omit the
19690 attribute. */
19691 if (bound_attr == DW_AT_lower_bound
19692 && tree_fits_shwi_p (bound)
19693 && (dflt = lower_bound_default ()) != -1
19694 && tree_to_shwi (bound) == dflt)
19695 return;
19697 /* FALLTHRU */
19699 default:
19700 /* Because of the complex interaction there can be with other GNAT
19701 encodings, GDB isn't ready yet to handle proper DWARF description
19702 for self-referencial subrange bounds: let GNAT encodings do the
19703 magic in such a case. */
19704 if (is_ada ()
19705 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19706 && contains_placeholder_p (bound))
19707 return;
19709 add_scalar_info (subrange_die, bound_attr, bound,
19710 dw_scalar_form_constant
19711 | dw_scalar_form_exprloc
19712 | dw_scalar_form_reference,
19713 context);
19714 return;
19718 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19719 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19720 Note that the block of subscript information for an array type also
19721 includes information about the element type of the given array type.
19723 This function reuses previously set type and bound information if
19724 available. */
19726 static void
19727 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19729 unsigned dimension_number;
19730 tree lower, upper;
19731 dw_die_ref child = type_die->die_child;
19733 for (dimension_number = 0;
19734 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19735 type = TREE_TYPE (type), dimension_number++)
19737 tree domain = TYPE_DOMAIN (type);
19739 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19740 break;
19742 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19743 and (in GNU C only) variable bounds. Handle all three forms
19744 here. */
19746 /* Find and reuse a previously generated DW_TAG_subrange_type if
19747 available.
19749 For multi-dimensional arrays, as we iterate through the
19750 various dimensions in the enclosing for loop above, we also
19751 iterate through the DIE children and pick at each
19752 DW_TAG_subrange_type previously generated (if available).
19753 Each child DW_TAG_subrange_type DIE describes the range of
19754 the current dimension. At this point we should have as many
19755 DW_TAG_subrange_type's as we have dimensions in the
19756 array. */
19757 dw_die_ref subrange_die = NULL;
19758 if (child)
19759 while (1)
19761 child = child->die_sib;
19762 if (child->die_tag == DW_TAG_subrange_type)
19763 subrange_die = child;
19764 if (child == type_die->die_child)
19766 /* If we wrapped around, stop looking next time. */
19767 child = NULL;
19768 break;
19770 if (child->die_tag == DW_TAG_subrange_type)
19771 break;
19773 if (!subrange_die)
19774 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19776 if (domain)
19778 /* We have an array type with specified bounds. */
19779 lower = TYPE_MIN_VALUE (domain);
19780 upper = TYPE_MAX_VALUE (domain);
19782 /* Define the index type. */
19783 if (TREE_TYPE (domain)
19784 && !get_AT (subrange_die, DW_AT_type))
19786 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19787 TREE_TYPE field. We can't emit debug info for this
19788 because it is an unnamed integral type. */
19789 if (TREE_CODE (domain) == INTEGER_TYPE
19790 && TYPE_NAME (domain) == NULL_TREE
19791 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19792 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19794 else
19795 add_type_attribute (subrange_die, TREE_TYPE (domain),
19796 TYPE_UNQUALIFIED, false, type_die);
19799 /* ??? If upper is NULL, the array has unspecified length,
19800 but it does have a lower bound. This happens with Fortran
19801 dimension arr(N:*)
19802 Since the debugger is definitely going to need to know N
19803 to produce useful results, go ahead and output the lower
19804 bound solo, and hope the debugger can cope. */
19806 if (!get_AT (subrange_die, DW_AT_lower_bound))
19807 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19808 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19809 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19812 /* Otherwise we have an array type with an unspecified length. The
19813 DWARF-2 spec does not say how to handle this; let's just leave out the
19814 bounds. */
19818 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19820 static void
19821 add_byte_size_attribute (dw_die_ref die, tree tree_node)
19823 dw_die_ref decl_die;
19824 HOST_WIDE_INT size;
19825 dw_loc_descr_ref size_expr = NULL;
19827 switch (TREE_CODE (tree_node))
19829 case ERROR_MARK:
19830 size = 0;
19831 break;
19832 case ENUMERAL_TYPE:
19833 case RECORD_TYPE:
19834 case UNION_TYPE:
19835 case QUAL_UNION_TYPE:
19836 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
19837 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
19839 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
19840 return;
19842 size_expr = type_byte_size (tree_node, &size);
19843 break;
19844 case FIELD_DECL:
19845 /* For a data member of a struct or union, the DW_AT_byte_size is
19846 generally given as the number of bytes normally allocated for an
19847 object of the *declared* type of the member itself. This is true
19848 even for bit-fields. */
19849 size = int_size_in_bytes (field_type (tree_node));
19850 break;
19851 default:
19852 gcc_unreachable ();
19855 /* Support for dynamically-sized objects was introduced by DWARFv3.
19856 At the moment, GDB does not handle variable byte sizes very well,
19857 though. */
19858 if ((dwarf_version >= 3 || !dwarf_strict)
19859 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
19860 && size_expr != NULL)
19861 add_AT_loc (die, DW_AT_byte_size, size_expr);
19863 /* Note that `size' might be -1 when we get to this point. If it is, that
19864 indicates that the byte size of the entity in question is variable and
19865 that we could not generate a DWARF expression that computes it. */
19866 if (size >= 0)
19867 add_AT_unsigned (die, DW_AT_byte_size, size);
19870 /* For a FIELD_DECL node which represents a bit-field, output an attribute
19871 which specifies the distance in bits from the highest order bit of the
19872 "containing object" for the bit-field to the highest order bit of the
19873 bit-field itself.
19875 For any given bit-field, the "containing object" is a hypothetical object
19876 (of some integral or enum type) within which the given bit-field lives. The
19877 type of this hypothetical "containing object" is always the same as the
19878 declared type of the individual bit-field itself. The determination of the
19879 exact location of the "containing object" for a bit-field is rather
19880 complicated. It's handled by the `field_byte_offset' function (above).
19882 CTX is required: see the comment for VLR_CONTEXT.
19884 Note that it is the size (in bytes) of the hypothetical "containing object"
19885 which will be given in the DW_AT_byte_size attribute for this bit-field.
19886 (See `byte_size_attribute' above). */
19888 static inline void
19889 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
19891 HOST_WIDE_INT object_offset_in_bytes;
19892 tree original_type = DECL_BIT_FIELD_TYPE (decl);
19893 HOST_WIDE_INT bitpos_int;
19894 HOST_WIDE_INT highest_order_object_bit_offset;
19895 HOST_WIDE_INT highest_order_field_bit_offset;
19896 HOST_WIDE_INT bit_offset;
19898 field_byte_offset (decl, ctx, &object_offset_in_bytes);
19900 /* Must be a field and a bit field. */
19901 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
19903 /* We can't yet handle bit-fields whose offsets are variable, so if we
19904 encounter such things, just return without generating any attribute
19905 whatsoever. Likewise for variable or too large size. */
19906 if (! tree_fits_shwi_p (bit_position (decl))
19907 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
19908 return;
19910 bitpos_int = int_bit_position (decl);
19912 /* Note that the bit offset is always the distance (in bits) from the
19913 highest-order bit of the "containing object" to the highest-order bit of
19914 the bit-field itself. Since the "high-order end" of any object or field
19915 is different on big-endian and little-endian machines, the computation
19916 below must take account of these differences. */
19917 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
19918 highest_order_field_bit_offset = bitpos_int;
19920 if (! BYTES_BIG_ENDIAN)
19922 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
19923 highest_order_object_bit_offset +=
19924 simple_type_size_in_bits (original_type);
19927 bit_offset
19928 = (! BYTES_BIG_ENDIAN
19929 ? highest_order_object_bit_offset - highest_order_field_bit_offset
19930 : highest_order_field_bit_offset - highest_order_object_bit_offset);
19932 if (bit_offset < 0)
19933 add_AT_int (die, DW_AT_bit_offset, bit_offset);
19934 else
19935 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
19938 /* For a FIELD_DECL node which represents a bit field, output an attribute
19939 which specifies the length in bits of the given field. */
19941 static inline void
19942 add_bit_size_attribute (dw_die_ref die, tree decl)
19944 /* Must be a field and a bit field. */
19945 gcc_assert (TREE_CODE (decl) == FIELD_DECL
19946 && DECL_BIT_FIELD_TYPE (decl));
19948 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
19949 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
19952 /* If the compiled language is ANSI C, then add a 'prototyped'
19953 attribute, if arg types are given for the parameters of a function. */
19955 static inline void
19956 add_prototyped_attribute (dw_die_ref die, tree func_type)
19958 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19960 case DW_LANG_C:
19961 case DW_LANG_C89:
19962 case DW_LANG_C99:
19963 case DW_LANG_C11:
19964 case DW_LANG_ObjC:
19965 if (prototype_p (func_type))
19966 add_AT_flag (die, DW_AT_prototyped, 1);
19967 break;
19968 default:
19969 break;
19973 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
19974 by looking in the type declaration, the object declaration equate table or
19975 the block mapping. */
19977 static inline dw_die_ref
19978 add_abstract_origin_attribute (dw_die_ref die, tree origin)
19980 dw_die_ref origin_die = NULL;
19982 if (TREE_CODE (origin) != FUNCTION_DECL
19983 && TREE_CODE (origin) != BLOCK)
19985 /* We may have gotten separated from the block for the inlined
19986 function, if we're in an exception handler or some such; make
19987 sure that the abstract function has been written out.
19989 Doing this for nested functions is wrong, however; functions are
19990 distinct units, and our context might not even be inline. */
19991 tree fn = origin;
19993 if (TYPE_P (fn))
19994 fn = TYPE_STUB_DECL (fn);
19996 fn = decl_function_context (fn);
19997 if (fn)
19998 dwarf2out_abstract_function (fn);
20001 if (DECL_P (origin))
20002 origin_die = lookup_decl_die (origin);
20003 else if (TYPE_P (origin))
20004 origin_die = lookup_type_die (origin);
20005 else if (TREE_CODE (origin) == BLOCK)
20006 origin_die = BLOCK_DIE (origin);
20008 /* XXX: Functions that are never lowered don't always have correct block
20009 trees (in the case of java, they simply have no block tree, in some other
20010 languages). For these functions, there is nothing we can really do to
20011 output correct debug info for inlined functions in all cases. Rather
20012 than die, we'll just produce deficient debug info now, in that we will
20013 have variables without a proper abstract origin. In the future, when all
20014 functions are lowered, we should re-add a gcc_assert (origin_die)
20015 here. */
20017 if (origin_die)
20018 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20019 return origin_die;
20022 /* We do not currently support the pure_virtual attribute. */
20024 static inline void
20025 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20027 if (DECL_VINDEX (func_decl))
20029 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20031 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20032 add_AT_loc (die, DW_AT_vtable_elem_location,
20033 new_loc_descr (DW_OP_constu,
20034 tree_to_shwi (DECL_VINDEX (func_decl)),
20035 0));
20037 /* GNU extension: Record what type this method came from originally. */
20038 if (debug_info_level > DINFO_LEVEL_TERSE
20039 && DECL_CONTEXT (func_decl))
20040 add_AT_die_ref (die, DW_AT_containing_type,
20041 lookup_type_die (DECL_CONTEXT (func_decl)));
20045 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20046 given decl. This used to be a vendor extension until after DWARF 4
20047 standardized it. */
20049 static void
20050 add_linkage_attr (dw_die_ref die, tree decl)
20052 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20054 /* Mimic what assemble_name_raw does with a leading '*'. */
20055 if (name[0] == '*')
20056 name = &name[1];
20058 if (dwarf_version >= 4)
20059 add_AT_string (die, DW_AT_linkage_name, name);
20060 else
20061 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20064 /* Add source coordinate attributes for the given decl. */
20066 static void
20067 add_src_coords_attributes (dw_die_ref die, tree decl)
20069 expanded_location s;
20071 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20072 return;
20073 s = expand_location (DECL_SOURCE_LOCATION (decl));
20074 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20075 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20078 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20080 static void
20081 add_linkage_name_raw (dw_die_ref die, tree decl)
20083 /* Defer until we have an assembler name set. */
20084 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20086 limbo_die_node *asm_name;
20088 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20089 asm_name->die = die;
20090 asm_name->created_for = decl;
20091 asm_name->next = deferred_asm_name;
20092 deferred_asm_name = asm_name;
20094 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20095 add_linkage_attr (die, decl);
20098 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20100 static void
20101 add_linkage_name (dw_die_ref die, tree decl)
20103 if (debug_info_level > DINFO_LEVEL_NONE
20104 && VAR_OR_FUNCTION_DECL_P (decl)
20105 && TREE_PUBLIC (decl)
20106 && !(VAR_P (decl) && DECL_REGISTER (decl))
20107 && die->die_tag != DW_TAG_member)
20108 add_linkage_name_raw (die, decl);
20111 /* Add a DW_AT_name attribute and source coordinate attribute for the
20112 given decl, but only if it actually has a name. */
20114 static void
20115 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20116 bool no_linkage_name)
20118 tree decl_name;
20120 decl_name = DECL_NAME (decl);
20121 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20123 const char *name = dwarf2_name (decl, 0);
20124 if (name)
20125 add_name_attribute (die, name);
20126 if (! DECL_ARTIFICIAL (decl))
20127 add_src_coords_attributes (die, decl);
20129 if (!no_linkage_name)
20130 add_linkage_name (die, decl);
20133 #ifdef VMS_DEBUGGING_INFO
20134 /* Get the function's name, as described by its RTL. This may be different
20135 from the DECL_NAME name used in the source file. */
20136 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20138 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20139 XEXP (DECL_RTL (decl), 0), false);
20140 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20142 #endif /* VMS_DEBUGGING_INFO */
20145 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20147 static void
20148 add_discr_value (dw_die_ref die, dw_discr_value *value)
20150 dw_attr_node attr;
20152 attr.dw_attr = DW_AT_discr_value;
20153 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20154 attr.dw_attr_val.val_entry = NULL;
20155 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20156 if (value->pos)
20157 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20158 else
20159 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20160 add_dwarf_attr (die, &attr);
20163 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20165 static void
20166 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20168 dw_attr_node attr;
20170 attr.dw_attr = DW_AT_discr_list;
20171 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20172 attr.dw_attr_val.val_entry = NULL;
20173 attr.dw_attr_val.v.val_discr_list = discr_list;
20174 add_dwarf_attr (die, &attr);
20177 static inline dw_discr_list_ref
20178 AT_discr_list (dw_attr_node *attr)
20180 return attr->dw_attr_val.v.val_discr_list;
20183 #ifdef VMS_DEBUGGING_INFO
20184 /* Output the debug main pointer die for VMS */
20186 void
20187 dwarf2out_vms_debug_main_pointer (void)
20189 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20190 dw_die_ref die;
20192 /* Allocate the VMS debug main subprogram die. */
20193 die = ggc_cleared_alloc<die_node> ();
20194 die->die_tag = DW_TAG_subprogram;
20195 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20196 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20197 current_function_funcdef_no);
20198 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20200 /* Make it the first child of comp_unit_die (). */
20201 die->die_parent = comp_unit_die ();
20202 if (comp_unit_die ()->die_child)
20204 die->die_sib = comp_unit_die ()->die_child->die_sib;
20205 comp_unit_die ()->die_child->die_sib = die;
20207 else
20209 die->die_sib = die;
20210 comp_unit_die ()->die_child = die;
20213 #endif /* VMS_DEBUGGING_INFO */
20215 /* Push a new declaration scope. */
20217 static void
20218 push_decl_scope (tree scope)
20220 vec_safe_push (decl_scope_table, scope);
20223 /* Pop a declaration scope. */
20225 static inline void
20226 pop_decl_scope (void)
20228 decl_scope_table->pop ();
20231 /* walk_tree helper function for uses_local_type, below. */
20233 static tree
20234 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20236 if (!TYPE_P (*tp))
20237 *walk_subtrees = 0;
20238 else
20240 tree name = TYPE_NAME (*tp);
20241 if (name && DECL_P (name) && decl_function_context (name))
20242 return *tp;
20244 return NULL_TREE;
20247 /* If TYPE involves a function-local type (including a local typedef to a
20248 non-local type), returns that type; otherwise returns NULL_TREE. */
20250 static tree
20251 uses_local_type (tree type)
20253 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20254 return used;
20257 /* Return the DIE for the scope that immediately contains this type.
20258 Non-named types that do not involve a function-local type get global
20259 scope. Named types nested in namespaces or other types get their
20260 containing scope. All other types (i.e. function-local named types) get
20261 the current active scope. */
20263 static dw_die_ref
20264 scope_die_for (tree t, dw_die_ref context_die)
20266 dw_die_ref scope_die = NULL;
20267 tree containing_scope;
20269 /* Non-types always go in the current scope. */
20270 gcc_assert (TYPE_P (t));
20272 /* Use the scope of the typedef, rather than the scope of the type
20273 it refers to. */
20274 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20275 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20276 else
20277 containing_scope = TYPE_CONTEXT (t);
20279 /* Use the containing namespace if there is one. */
20280 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20282 if (context_die == lookup_decl_die (containing_scope))
20283 /* OK */;
20284 else if (debug_info_level > DINFO_LEVEL_TERSE)
20285 context_die = get_context_die (containing_scope);
20286 else
20287 containing_scope = NULL_TREE;
20290 /* Ignore function type "scopes" from the C frontend. They mean that
20291 a tagged type is local to a parmlist of a function declarator, but
20292 that isn't useful to DWARF. */
20293 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20294 containing_scope = NULL_TREE;
20296 if (SCOPE_FILE_SCOPE_P (containing_scope))
20298 /* If T uses a local type keep it local as well, to avoid references
20299 to function-local DIEs from outside the function. */
20300 if (current_function_decl && uses_local_type (t))
20301 scope_die = context_die;
20302 else
20303 scope_die = comp_unit_die ();
20305 else if (TYPE_P (containing_scope))
20307 /* For types, we can just look up the appropriate DIE. */
20308 if (debug_info_level > DINFO_LEVEL_TERSE)
20309 scope_die = get_context_die (containing_scope);
20310 else
20312 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20313 if (scope_die == NULL)
20314 scope_die = comp_unit_die ();
20317 else
20318 scope_die = context_die;
20320 return scope_die;
20323 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20325 static inline int
20326 local_scope_p (dw_die_ref context_die)
20328 for (; context_die; context_die = context_die->die_parent)
20329 if (context_die->die_tag == DW_TAG_inlined_subroutine
20330 || context_die->die_tag == DW_TAG_subprogram)
20331 return 1;
20333 return 0;
20336 /* Returns nonzero if CONTEXT_DIE is a class. */
20338 static inline int
20339 class_scope_p (dw_die_ref context_die)
20341 return (context_die
20342 && (context_die->die_tag == DW_TAG_structure_type
20343 || context_die->die_tag == DW_TAG_class_type
20344 || context_die->die_tag == DW_TAG_interface_type
20345 || context_die->die_tag == DW_TAG_union_type));
20348 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20349 whether or not to treat a DIE in this context as a declaration. */
20351 static inline int
20352 class_or_namespace_scope_p (dw_die_ref context_die)
20354 return (class_scope_p (context_die)
20355 || (context_die && context_die->die_tag == DW_TAG_namespace));
20358 /* Many forms of DIEs require a "type description" attribute. This
20359 routine locates the proper "type descriptor" die for the type given
20360 by 'type' plus any additional qualifiers given by 'cv_quals', and
20361 adds a DW_AT_type attribute below the given die. */
20363 static void
20364 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20365 bool reverse, dw_die_ref context_die)
20367 enum tree_code code = TREE_CODE (type);
20368 dw_die_ref type_die = NULL;
20370 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20371 or fixed-point type, use the inner type. This is because we have no
20372 support for unnamed types in base_type_die. This can happen if this is
20373 an Ada subrange type. Correct solution is emit a subrange type die. */
20374 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20375 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20376 type = TREE_TYPE (type), code = TREE_CODE (type);
20378 if (code == ERROR_MARK
20379 /* Handle a special case. For functions whose return type is void, we
20380 generate *no* type attribute. (Note that no object may have type
20381 `void', so this only applies to function return types). */
20382 || code == VOID_TYPE)
20383 return;
20385 type_die = modified_type_die (type,
20386 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20387 reverse,
20388 context_die);
20390 if (type_die != NULL)
20391 add_AT_die_ref (object_die, DW_AT_type, type_die);
20394 /* Given an object die, add the calling convention attribute for the
20395 function call type. */
20396 static void
20397 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20399 enum dwarf_calling_convention value = DW_CC_normal;
20401 value = ((enum dwarf_calling_convention)
20402 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20404 if (is_fortran ()
20405 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20407 /* DWARF 2 doesn't provide a way to identify a program's source-level
20408 entry point. DW_AT_calling_convention attributes are only meant
20409 to describe functions' calling conventions. However, lacking a
20410 better way to signal the Fortran main program, we used this for
20411 a long time, following existing custom. Now, DWARF 4 has
20412 DW_AT_main_subprogram, which we add below, but some tools still
20413 rely on the old way, which we thus keep. */
20414 value = DW_CC_program;
20416 if (dwarf_version >= 4 || !dwarf_strict)
20417 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20420 /* Only add the attribute if the backend requests it, and
20421 is not DW_CC_normal. */
20422 if (value && (value != DW_CC_normal))
20423 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20426 /* Given a tree pointer to a struct, class, union, or enum type node, return
20427 a pointer to the (string) tag name for the given type, or zero if the type
20428 was declared without a tag. */
20430 static const char *
20431 type_tag (const_tree type)
20433 const char *name = 0;
20435 if (TYPE_NAME (type) != 0)
20437 tree t = 0;
20439 /* Find the IDENTIFIER_NODE for the type name. */
20440 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20441 && !TYPE_NAMELESS (type))
20442 t = TYPE_NAME (type);
20444 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20445 a TYPE_DECL node, regardless of whether or not a `typedef' was
20446 involved. */
20447 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20448 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20450 /* We want to be extra verbose. Don't call dwarf_name if
20451 DECL_NAME isn't set. The default hook for decl_printable_name
20452 doesn't like that, and in this context it's correct to return
20453 0, instead of "<anonymous>" or the like. */
20454 if (DECL_NAME (TYPE_NAME (type))
20455 && !DECL_NAMELESS (TYPE_NAME (type)))
20456 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20459 /* Now get the name as a string, or invent one. */
20460 if (!name && t != 0)
20461 name = IDENTIFIER_POINTER (t);
20464 return (name == 0 || *name == '\0') ? 0 : name;
20467 /* Return the type associated with a data member, make a special check
20468 for bit field types. */
20470 static inline tree
20471 member_declared_type (const_tree member)
20473 return (DECL_BIT_FIELD_TYPE (member)
20474 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20477 /* Get the decl's label, as described by its RTL. This may be different
20478 from the DECL_NAME name used in the source file. */
20480 #if 0
20481 static const char *
20482 decl_start_label (tree decl)
20484 rtx x;
20485 const char *fnname;
20487 x = DECL_RTL (decl);
20488 gcc_assert (MEM_P (x));
20490 x = XEXP (x, 0);
20491 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20493 fnname = XSTR (x, 0);
20494 return fnname;
20496 #endif
20498 /* For variable-length arrays that have been previously generated, but
20499 may be incomplete due to missing subscript info, fill the subscript
20500 info. Return TRUE if this is one of those cases. */
20501 static bool
20502 fill_variable_array_bounds (tree type)
20504 if (TREE_ASM_WRITTEN (type)
20505 && TREE_CODE (type) == ARRAY_TYPE
20506 && variably_modified_type_p (type, NULL))
20508 dw_die_ref array_die = lookup_type_die (type);
20509 if (!array_die)
20510 return false;
20511 add_subscript_info (array_die, type, !is_ada ());
20512 return true;
20514 return false;
20517 /* These routines generate the internal representation of the DIE's for
20518 the compilation unit. Debugging information is collected by walking
20519 the declaration trees passed in from dwarf2out_decl(). */
20521 static void
20522 gen_array_type_die (tree type, dw_die_ref context_die)
20524 dw_die_ref array_die;
20526 /* GNU compilers represent multidimensional array types as sequences of one
20527 dimensional array types whose element types are themselves array types.
20528 We sometimes squish that down to a single array_type DIE with multiple
20529 subscripts in the Dwarf debugging info. The draft Dwarf specification
20530 say that we are allowed to do this kind of compression in C, because
20531 there is no difference between an array of arrays and a multidimensional
20532 array. We don't do this for Ada to remain as close as possible to the
20533 actual representation, which is especially important against the language
20534 flexibilty wrt arrays of variable size. */
20536 bool collapse_nested_arrays = !is_ada ();
20538 if (fill_variable_array_bounds (type))
20539 return;
20541 dw_die_ref scope_die = scope_die_for (type, context_die);
20542 tree element_type;
20544 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20545 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20546 if (TYPE_STRING_FLAG (type)
20547 && TREE_CODE (type) == ARRAY_TYPE
20548 && is_fortran ()
20549 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20551 HOST_WIDE_INT size;
20553 array_die = new_die (DW_TAG_string_type, scope_die, type);
20554 add_name_attribute (array_die, type_tag (type));
20555 equate_type_number_to_die (type, array_die);
20556 size = int_size_in_bytes (type);
20557 if (size >= 0)
20558 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20559 else if (TYPE_DOMAIN (type) != NULL_TREE
20560 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20562 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20563 tree rszdecl = szdecl;
20564 HOST_WIDE_INT rsize = 0;
20566 size = int_size_in_bytes (TREE_TYPE (szdecl));
20567 if (!DECL_P (szdecl))
20569 if (TREE_CODE (szdecl) == INDIRECT_REF
20570 && DECL_P (TREE_OPERAND (szdecl, 0)))
20572 rszdecl = TREE_OPERAND (szdecl, 0);
20573 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
20574 if (rsize <= 0)
20575 size = 0;
20577 else
20578 size = 0;
20580 if (size > 0)
20582 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
20583 if (loc == NULL
20584 && early_dwarf
20585 && current_function_decl
20586 && DECL_CONTEXT (rszdecl) == current_function_decl)
20588 dw_die_ref ref = lookup_decl_die (rszdecl);
20589 dw_loc_descr_ref l = NULL;
20590 if (ref)
20592 l = new_loc_descr (DW_OP_call4, 0, 0);
20593 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20594 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
20595 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
20597 else if (TREE_CODE (rszdecl) == PARM_DECL
20598 && string_types)
20600 l = new_loc_descr (DW_OP_call4, 0, 0);
20601 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
20602 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
20603 string_types->safe_push (array_die);
20605 if (l && rszdecl != szdecl)
20607 if (rsize == DWARF2_ADDR_SIZE)
20608 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
20609 0, 0));
20610 else
20611 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
20612 rsize, 0));
20614 if (l)
20615 loc = new_loc_list (l, NULL, NULL, NULL);
20617 if (loc)
20619 add_AT_location_description (array_die, DW_AT_string_length,
20620 loc);
20621 if (size != DWARF2_ADDR_SIZE)
20622 add_AT_unsigned (array_die, dwarf_version >= 5
20623 ? DW_AT_string_length_byte_size
20624 : DW_AT_byte_size, size);
20628 return;
20631 array_die = new_die (DW_TAG_array_type, scope_die, type);
20632 add_name_attribute (array_die, type_tag (type));
20633 equate_type_number_to_die (type, array_die);
20635 if (TREE_CODE (type) == VECTOR_TYPE)
20636 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20638 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20639 if (is_fortran ()
20640 && TREE_CODE (type) == ARRAY_TYPE
20641 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20642 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20643 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20645 #if 0
20646 /* We default the array ordering. SDB will probably do
20647 the right things even if DW_AT_ordering is not present. It's not even
20648 an issue until we start to get into multidimensional arrays anyway. If
20649 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20650 then we'll have to put the DW_AT_ordering attribute back in. (But if
20651 and when we find out that we need to put these in, we will only do so
20652 for multidimensional arrays. */
20653 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20654 #endif
20656 if (TREE_CODE (type) == VECTOR_TYPE)
20658 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20659 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20660 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20661 add_bound_info (subrange_die, DW_AT_upper_bound,
20662 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20664 else
20665 add_subscript_info (array_die, type, collapse_nested_arrays);
20667 /* Add representation of the type of the elements of this array type and
20668 emit the corresponding DIE if we haven't done it already. */
20669 element_type = TREE_TYPE (type);
20670 if (collapse_nested_arrays)
20671 while (TREE_CODE (element_type) == ARRAY_TYPE)
20673 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20674 break;
20675 element_type = TREE_TYPE (element_type);
20678 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20679 TREE_CODE (type) == ARRAY_TYPE
20680 && TYPE_REVERSE_STORAGE_ORDER (type),
20681 context_die);
20683 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20684 if (TYPE_ARTIFICIAL (type))
20685 add_AT_flag (array_die, DW_AT_artificial, 1);
20687 if (get_AT (array_die, DW_AT_name))
20688 add_pubtype (type, array_die);
20691 /* After all arguments are created, adjust any DW_TAG_string_type
20692 DIEs DW_AT_string_length attributes. */
20694 static void
20695 adjust_string_types (void)
20697 dw_die_ref array_die;
20698 unsigned int i;
20699 FOR_EACH_VEC_ELT (*string_types, i, array_die)
20701 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
20702 if (a == NULL)
20703 continue;
20704 dw_loc_descr_ref loc = AT_loc (a);
20705 gcc_assert (loc->dw_loc_opc == DW_OP_call4
20706 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
20707 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
20708 if (ref)
20710 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20711 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
20712 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
20714 else
20716 remove_AT (array_die, DW_AT_string_length);
20717 remove_AT (array_die, dwarf_version >= 5
20718 ? DW_AT_string_length_byte_size
20719 : DW_AT_byte_size);
20724 /* This routine generates DIE for array with hidden descriptor, details
20725 are filled into *info by a langhook. */
20727 static void
20728 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20729 dw_die_ref context_die)
20731 const dw_die_ref scope_die = scope_die_for (type, context_die);
20732 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20733 struct loc_descr_context context = { type, info->base_decl, NULL,
20734 false, false };
20735 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20736 int dim;
20738 add_name_attribute (array_die, type_tag (type));
20739 equate_type_number_to_die (type, array_die);
20741 if (info->ndimensions > 1)
20742 switch (info->ordering)
20744 case array_descr_ordering_row_major:
20745 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20746 break;
20747 case array_descr_ordering_column_major:
20748 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20749 break;
20750 default:
20751 break;
20754 if (dwarf_version >= 3 || !dwarf_strict)
20756 if (info->data_location)
20757 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20758 dw_scalar_form_exprloc, &context);
20759 if (info->associated)
20760 add_scalar_info (array_die, DW_AT_associated, info->associated,
20761 dw_scalar_form_constant
20762 | dw_scalar_form_exprloc
20763 | dw_scalar_form_reference, &context);
20764 if (info->allocated)
20765 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20766 dw_scalar_form_constant
20767 | dw_scalar_form_exprloc
20768 | dw_scalar_form_reference, &context);
20769 if (info->stride)
20771 const enum dwarf_attribute attr
20772 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20773 const int forms
20774 = (info->stride_in_bits)
20775 ? dw_scalar_form_constant
20776 : (dw_scalar_form_constant
20777 | dw_scalar_form_exprloc
20778 | dw_scalar_form_reference);
20780 add_scalar_info (array_die, attr, info->stride, forms, &context);
20783 if (dwarf_version >= 5)
20785 if (info->rank)
20787 add_scalar_info (array_die, DW_AT_rank, info->rank,
20788 dw_scalar_form_constant
20789 | dw_scalar_form_exprloc, &context);
20790 subrange_tag = DW_TAG_generic_subrange;
20791 context.placeholder_arg = true;
20795 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20797 for (dim = 0; dim < info->ndimensions; dim++)
20799 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20801 if (info->dimen[dim].bounds_type)
20802 add_type_attribute (subrange_die,
20803 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20804 false, context_die);
20805 if (info->dimen[dim].lower_bound)
20806 add_bound_info (subrange_die, DW_AT_lower_bound,
20807 info->dimen[dim].lower_bound, &context);
20808 if (info->dimen[dim].upper_bound)
20809 add_bound_info (subrange_die, DW_AT_upper_bound,
20810 info->dimen[dim].upper_bound, &context);
20811 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20812 add_scalar_info (subrange_die, DW_AT_byte_stride,
20813 info->dimen[dim].stride,
20814 dw_scalar_form_constant
20815 | dw_scalar_form_exprloc
20816 | dw_scalar_form_reference,
20817 &context);
20820 gen_type_die (info->element_type, context_die);
20821 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20822 TREE_CODE (type) == ARRAY_TYPE
20823 && TYPE_REVERSE_STORAGE_ORDER (type),
20824 context_die);
20826 if (get_AT (array_die, DW_AT_name))
20827 add_pubtype (type, array_die);
20830 #if 0
20831 static void
20832 gen_entry_point_die (tree decl, dw_die_ref context_die)
20834 tree origin = decl_ultimate_origin (decl);
20835 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20837 if (origin != NULL)
20838 add_abstract_origin_attribute (decl_die, origin);
20839 else
20841 add_name_and_src_coords_attributes (decl_die, decl);
20842 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20843 TYPE_UNQUALIFIED, false, context_die);
20846 if (DECL_ABSTRACT_P (decl))
20847 equate_decl_number_to_die (decl, decl_die);
20848 else
20849 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
20851 #endif
20853 /* Walk through the list of incomplete types again, trying once more to
20854 emit full debugging info for them. */
20856 static void
20857 retry_incomplete_types (void)
20859 set_early_dwarf s;
20860 int i;
20862 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
20863 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
20864 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
20865 vec_safe_truncate (incomplete_types, 0);
20868 /* Determine what tag to use for a record type. */
20870 static enum dwarf_tag
20871 record_type_tag (tree type)
20873 if (! lang_hooks.types.classify_record)
20874 return DW_TAG_structure_type;
20876 switch (lang_hooks.types.classify_record (type))
20878 case RECORD_IS_STRUCT:
20879 return DW_TAG_structure_type;
20881 case RECORD_IS_CLASS:
20882 return DW_TAG_class_type;
20884 case RECORD_IS_INTERFACE:
20885 if (dwarf_version >= 3 || !dwarf_strict)
20886 return DW_TAG_interface_type;
20887 return DW_TAG_structure_type;
20889 default:
20890 gcc_unreachable ();
20894 /* Generate a DIE to represent an enumeration type. Note that these DIEs
20895 include all of the information about the enumeration values also. Each
20896 enumerated type name/value is listed as a child of the enumerated type
20897 DIE. */
20899 static dw_die_ref
20900 gen_enumeration_type_die (tree type, dw_die_ref context_die)
20902 dw_die_ref type_die = lookup_type_die (type);
20904 if (type_die == NULL)
20906 type_die = new_die (DW_TAG_enumeration_type,
20907 scope_die_for (type, context_die), type);
20908 equate_type_number_to_die (type, type_die);
20909 add_name_attribute (type_die, type_tag (type));
20910 if (dwarf_version >= 4 || !dwarf_strict)
20912 if (ENUM_IS_SCOPED (type))
20913 add_AT_flag (type_die, DW_AT_enum_class, 1);
20914 if (ENUM_IS_OPAQUE (type))
20915 add_AT_flag (type_die, DW_AT_declaration, 1);
20918 else if (! TYPE_SIZE (type))
20919 return type_die;
20920 else
20921 remove_AT (type_die, DW_AT_declaration);
20923 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
20924 given enum type is incomplete, do not generate the DW_AT_byte_size
20925 attribute or the DW_AT_element_list attribute. */
20926 if (TYPE_SIZE (type))
20928 tree link;
20930 TREE_ASM_WRITTEN (type) = 1;
20931 add_byte_size_attribute (type_die, type);
20932 if (dwarf_version >= 3 || !dwarf_strict)
20934 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
20935 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
20936 context_die);
20938 if (TYPE_STUB_DECL (type) != NULL_TREE)
20940 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20941 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20944 /* If the first reference to this type was as the return type of an
20945 inline function, then it may not have a parent. Fix this now. */
20946 if (type_die->die_parent == NULL)
20947 add_child_die (scope_die_for (type, context_die), type_die);
20949 for (link = TYPE_VALUES (type);
20950 link != NULL; link = TREE_CHAIN (link))
20952 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
20953 tree value = TREE_VALUE (link);
20955 add_name_attribute (enum_die,
20956 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
20958 if (TREE_CODE (value) == CONST_DECL)
20959 value = DECL_INITIAL (value);
20961 if (simple_type_size_in_bits (TREE_TYPE (value))
20962 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
20964 /* For constant forms created by add_AT_unsigned DWARF
20965 consumers (GDB, elfutils, etc.) always zero extend
20966 the value. Only when the actual value is negative
20967 do we need to use add_AT_int to generate a constant
20968 form that can represent negative values. */
20969 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
20970 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
20971 add_AT_unsigned (enum_die, DW_AT_const_value,
20972 (unsigned HOST_WIDE_INT) val);
20973 else
20974 add_AT_int (enum_die, DW_AT_const_value, val);
20976 else
20977 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
20978 that here. TODO: This should be re-worked to use correct
20979 signed/unsigned double tags for all cases. */
20980 add_AT_wide (enum_die, DW_AT_const_value, value);
20983 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20984 if (TYPE_ARTIFICIAL (type))
20985 add_AT_flag (type_die, DW_AT_artificial, 1);
20987 else
20988 add_AT_flag (type_die, DW_AT_declaration, 1);
20990 add_pubtype (type, type_die);
20992 return type_die;
20995 /* Generate a DIE to represent either a real live formal parameter decl or to
20996 represent just the type of some formal parameter position in some function
20997 type.
20999 Note that this routine is a bit unusual because its argument may be a
21000 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21001 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21002 node. If it's the former then this function is being called to output a
21003 DIE to represent a formal parameter object (or some inlining thereof). If
21004 it's the latter, then this function is only being called to output a
21005 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21006 argument type of some subprogram type.
21007 If EMIT_NAME_P is true, name and source coordinate attributes
21008 are emitted. */
21010 static dw_die_ref
21011 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21012 dw_die_ref context_die)
21014 tree node_or_origin = node ? node : origin;
21015 tree ultimate_origin;
21016 dw_die_ref parm_die = NULL;
21018 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21020 parm_die = lookup_decl_die (node);
21022 /* If the contexts differ, we may not be talking about the same
21023 thing. */
21024 if (parm_die && parm_die->die_parent != context_die)
21026 if (!DECL_ABSTRACT_P (node))
21028 /* This can happen when creating an inlined instance, in
21029 which case we need to create a new DIE that will get
21030 annotated with DW_AT_abstract_origin. */
21031 parm_die = NULL;
21033 else
21035 /* FIXME: Reuse DIE even with a differing context.
21037 This can happen when calling
21038 dwarf2out_abstract_function to build debug info for
21039 the abstract instance of a function for which we have
21040 already generated a DIE in
21041 dwarf2out_early_global_decl.
21043 Once we remove dwarf2out_abstract_function, we should
21044 have a call to gcc_unreachable here. */
21048 if (parm_die && parm_die->die_parent == NULL)
21050 /* Check that parm_die already has the right attributes that
21051 we would have added below. If any attributes are
21052 missing, fall through to add them. */
21053 if (! DECL_ABSTRACT_P (node_or_origin)
21054 && !get_AT (parm_die, DW_AT_location)
21055 && !get_AT (parm_die, DW_AT_const_value))
21056 /* We are missing location info, and are about to add it. */
21058 else
21060 add_child_die (context_die, parm_die);
21061 return parm_die;
21066 /* If we have a previously generated DIE, use it, unless this is an
21067 concrete instance (origin != NULL), in which case we need a new
21068 DIE with a corresponding DW_AT_abstract_origin. */
21069 bool reusing_die;
21070 if (parm_die && origin == NULL)
21071 reusing_die = true;
21072 else
21074 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21075 reusing_die = false;
21078 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21080 case tcc_declaration:
21081 ultimate_origin = decl_ultimate_origin (node_or_origin);
21082 if (node || ultimate_origin)
21083 origin = ultimate_origin;
21085 if (reusing_die)
21086 goto add_location;
21088 if (origin != NULL)
21089 add_abstract_origin_attribute (parm_die, origin);
21090 else if (emit_name_p)
21091 add_name_and_src_coords_attributes (parm_die, node);
21092 if (origin == NULL
21093 || (! DECL_ABSTRACT_P (node_or_origin)
21094 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21095 decl_function_context
21096 (node_or_origin))))
21098 tree type = TREE_TYPE (node_or_origin);
21099 if (decl_by_reference_p (node_or_origin))
21100 add_type_attribute (parm_die, TREE_TYPE (type),
21101 TYPE_UNQUALIFIED,
21102 false, context_die);
21103 else
21104 add_type_attribute (parm_die, type,
21105 decl_quals (node_or_origin),
21106 false, context_die);
21108 if (origin == NULL && DECL_ARTIFICIAL (node))
21109 add_AT_flag (parm_die, DW_AT_artificial, 1);
21110 add_location:
21111 if (node && node != origin)
21112 equate_decl_number_to_die (node, parm_die);
21113 if (! DECL_ABSTRACT_P (node_or_origin))
21114 add_location_or_const_value_attribute (parm_die, node_or_origin,
21115 node == NULL);
21117 break;
21119 case tcc_type:
21120 /* We were called with some kind of a ..._TYPE node. */
21121 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21122 context_die);
21123 break;
21125 default:
21126 gcc_unreachable ();
21129 return parm_die;
21132 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21133 children DW_TAG_formal_parameter DIEs representing the arguments of the
21134 parameter pack.
21136 PARM_PACK must be a function parameter pack.
21137 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21138 must point to the subsequent arguments of the function PACK_ARG belongs to.
21139 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21140 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21141 following the last one for which a DIE was generated. */
21143 static dw_die_ref
21144 gen_formal_parameter_pack_die (tree parm_pack,
21145 tree pack_arg,
21146 dw_die_ref subr_die,
21147 tree *next_arg)
21149 tree arg;
21150 dw_die_ref parm_pack_die;
21152 gcc_assert (parm_pack
21153 && lang_hooks.function_parameter_pack_p (parm_pack)
21154 && subr_die);
21156 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21157 add_src_coords_attributes (parm_pack_die, parm_pack);
21159 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21161 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21162 parm_pack))
21163 break;
21164 gen_formal_parameter_die (arg, NULL,
21165 false /* Don't emit name attribute. */,
21166 parm_pack_die);
21168 if (next_arg)
21169 *next_arg = arg;
21170 return parm_pack_die;
21173 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21174 at the end of an (ANSI prototyped) formal parameters list. */
21176 static void
21177 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21179 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21182 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21183 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21184 parameters as specified in some function type specification (except for
21185 those which appear as part of a function *definition*). */
21187 static void
21188 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21190 tree link;
21191 tree formal_type = NULL;
21192 tree first_parm_type;
21193 tree arg;
21195 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21197 arg = DECL_ARGUMENTS (function_or_method_type);
21198 function_or_method_type = TREE_TYPE (function_or_method_type);
21200 else
21201 arg = NULL_TREE;
21203 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21205 /* Make our first pass over the list of formal parameter types and output a
21206 DW_TAG_formal_parameter DIE for each one. */
21207 for (link = first_parm_type; link; )
21209 dw_die_ref parm_die;
21211 formal_type = TREE_VALUE (link);
21212 if (formal_type == void_type_node)
21213 break;
21215 /* Output a (nameless) DIE to represent the formal parameter itself. */
21216 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21218 parm_die = gen_formal_parameter_die (formal_type, NULL,
21219 true /* Emit name attribute. */,
21220 context_die);
21221 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21222 && link == first_parm_type)
21224 add_AT_flag (parm_die, DW_AT_artificial, 1);
21225 if (dwarf_version >= 3 || !dwarf_strict)
21226 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21228 else if (arg && DECL_ARTIFICIAL (arg))
21229 add_AT_flag (parm_die, DW_AT_artificial, 1);
21232 link = TREE_CHAIN (link);
21233 if (arg)
21234 arg = DECL_CHAIN (arg);
21237 /* If this function type has an ellipsis, add a
21238 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21239 if (formal_type != void_type_node)
21240 gen_unspecified_parameters_die (function_or_method_type, context_die);
21242 /* Make our second (and final) pass over the list of formal parameter types
21243 and output DIEs to represent those types (as necessary). */
21244 for (link = TYPE_ARG_TYPES (function_or_method_type);
21245 link && TREE_VALUE (link);
21246 link = TREE_CHAIN (link))
21247 gen_type_die (TREE_VALUE (link), context_die);
21250 /* We want to generate the DIE for TYPE so that we can generate the
21251 die for MEMBER, which has been defined; we will need to refer back
21252 to the member declaration nested within TYPE. If we're trying to
21253 generate minimal debug info for TYPE, processing TYPE won't do the
21254 trick; we need to attach the member declaration by hand. */
21256 static void
21257 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21259 gen_type_die (type, context_die);
21261 /* If we're trying to avoid duplicate debug info, we may not have
21262 emitted the member decl for this function. Emit it now. */
21263 if (TYPE_STUB_DECL (type)
21264 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21265 && ! lookup_decl_die (member))
21267 dw_die_ref type_die;
21268 gcc_assert (!decl_ultimate_origin (member));
21270 push_decl_scope (type);
21271 type_die = lookup_type_die_strip_naming_typedef (type);
21272 if (TREE_CODE (member) == FUNCTION_DECL)
21273 gen_subprogram_die (member, type_die);
21274 else if (TREE_CODE (member) == FIELD_DECL)
21276 /* Ignore the nameless fields that are used to skip bits but handle
21277 C++ anonymous unions and structs. */
21278 if (DECL_NAME (member) != NULL_TREE
21279 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21280 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21282 struct vlr_context vlr_ctx = {
21283 DECL_CONTEXT (member), /* struct_type */
21284 NULL_TREE /* variant_part_offset */
21286 gen_type_die (member_declared_type (member), type_die);
21287 gen_field_die (member, &vlr_ctx, type_die);
21290 else
21291 gen_variable_die (member, NULL_TREE, type_die);
21293 pop_decl_scope ();
21297 /* Forward declare these functions, because they are mutually recursive
21298 with their set_block_* pairing functions. */
21299 static void set_decl_origin_self (tree);
21300 static void set_decl_abstract_flags (tree, vec<tree> &);
21302 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21303 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21304 that it points to the node itself, thus indicating that the node is its
21305 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21306 the given node is NULL, recursively descend the decl/block tree which
21307 it is the root of, and for each other ..._DECL or BLOCK node contained
21308 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21309 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21310 values to point to themselves. */
21312 static void
21313 set_block_origin_self (tree stmt)
21315 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21317 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21320 tree local_decl;
21322 for (local_decl = BLOCK_VARS (stmt);
21323 local_decl != NULL_TREE;
21324 local_decl = DECL_CHAIN (local_decl))
21325 /* Do not recurse on nested functions since the inlining status
21326 of parent and child can be different as per the DWARF spec. */
21327 if (TREE_CODE (local_decl) != FUNCTION_DECL
21328 && !DECL_EXTERNAL (local_decl))
21329 set_decl_origin_self (local_decl);
21333 tree subblock;
21335 for (subblock = BLOCK_SUBBLOCKS (stmt);
21336 subblock != NULL_TREE;
21337 subblock = BLOCK_CHAIN (subblock))
21338 set_block_origin_self (subblock); /* Recurse. */
21343 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21344 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21345 node to so that it points to the node itself, thus indicating that the
21346 node represents its own (abstract) origin. Additionally, if the
21347 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21348 the decl/block tree of which the given node is the root of, and for
21349 each other ..._DECL or BLOCK node contained therein whose
21350 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21351 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21352 point to themselves. */
21354 static void
21355 set_decl_origin_self (tree decl)
21357 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21359 DECL_ABSTRACT_ORIGIN (decl) = decl;
21360 if (TREE_CODE (decl) == FUNCTION_DECL)
21362 tree arg;
21364 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21365 DECL_ABSTRACT_ORIGIN (arg) = arg;
21366 if (DECL_INITIAL (decl) != NULL_TREE
21367 && DECL_INITIAL (decl) != error_mark_node)
21368 set_block_origin_self (DECL_INITIAL (decl));
21373 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21374 and if it wasn't 1 before, push it to abstract_vec vector.
21375 For all local decls and all local sub-blocks (recursively) do it
21376 too. */
21378 static void
21379 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21381 tree local_decl;
21382 tree subblock;
21383 unsigned int i;
21385 if (!BLOCK_ABSTRACT (stmt))
21387 abstract_vec.safe_push (stmt);
21388 BLOCK_ABSTRACT (stmt) = 1;
21391 for (local_decl = BLOCK_VARS (stmt);
21392 local_decl != NULL_TREE;
21393 local_decl = DECL_CHAIN (local_decl))
21394 if (! DECL_EXTERNAL (local_decl))
21395 set_decl_abstract_flags (local_decl, abstract_vec);
21397 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21399 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21400 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21401 || TREE_CODE (local_decl) == PARM_DECL)
21402 set_decl_abstract_flags (local_decl, abstract_vec);
21405 for (subblock = BLOCK_SUBBLOCKS (stmt);
21406 subblock != NULL_TREE;
21407 subblock = BLOCK_CHAIN (subblock))
21408 set_block_abstract_flags (subblock, abstract_vec);
21411 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21412 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21413 In the case where the decl is a FUNCTION_DECL also set the abstract
21414 flags for all of the parameters, local vars, local
21415 blocks and sub-blocks (recursively). */
21417 static void
21418 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21420 if (!DECL_ABSTRACT_P (decl))
21422 abstract_vec.safe_push (decl);
21423 DECL_ABSTRACT_P (decl) = 1;
21426 if (TREE_CODE (decl) == FUNCTION_DECL)
21428 tree arg;
21430 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21431 if (!DECL_ABSTRACT_P (arg))
21433 abstract_vec.safe_push (arg);
21434 DECL_ABSTRACT_P (arg) = 1;
21436 if (DECL_INITIAL (decl) != NULL_TREE
21437 && DECL_INITIAL (decl) != error_mark_node)
21438 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21442 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21443 may later generate inlined and/or out-of-line instances of.
21445 FIXME: In the early-dwarf world, this function, and most of the
21446 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21447 the abstract instance. All we would need to do is annotate
21448 the early DIE with the appropriate DW_AT_inline in late
21449 dwarf (perhaps in gen_inlined_subroutine_die).
21451 However, we can't do this yet, because LTO streaming of DIEs
21452 has not been implemented yet. */
21454 static void
21455 dwarf2out_abstract_function (tree decl)
21457 dw_die_ref old_die;
21458 tree save_fn;
21459 tree context;
21460 hash_table<decl_loc_hasher> *old_decl_loc_table;
21461 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21462 int old_call_site_count, old_tail_call_site_count;
21463 struct call_arg_loc_node *old_call_arg_locations;
21465 /* Make sure we have the actual abstract inline, not a clone. */
21466 decl = DECL_ORIGIN (decl);
21468 old_die = lookup_decl_die (decl);
21469 if (old_die && get_AT (old_die, DW_AT_inline))
21470 /* We've already generated the abstract instance. */
21471 return;
21473 /* We can be called while recursively when seeing block defining inlined subroutine
21474 DIE. Be sure to not clobber the outer location table nor use it or we would
21475 get locations in abstract instantces. */
21476 old_decl_loc_table = decl_loc_table;
21477 decl_loc_table = NULL;
21478 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21479 cached_dw_loc_list_table = NULL;
21480 old_call_arg_locations = call_arg_locations;
21481 call_arg_locations = NULL;
21482 old_call_site_count = call_site_count;
21483 call_site_count = -1;
21484 old_tail_call_site_count = tail_call_site_count;
21485 tail_call_site_count = -1;
21487 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21488 we don't get confused by DECL_ABSTRACT_P. */
21489 if (debug_info_level > DINFO_LEVEL_TERSE)
21491 context = decl_class_context (decl);
21492 if (context)
21493 gen_type_die_for_member
21494 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21497 /* Pretend we've just finished compiling this function. */
21498 save_fn = current_function_decl;
21499 current_function_decl = decl;
21501 auto_vec<tree, 64> abstract_vec;
21502 set_decl_abstract_flags (decl, abstract_vec);
21503 dwarf2out_decl (decl);
21504 unsigned int i;
21505 tree t;
21506 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21507 if (TREE_CODE (t) == BLOCK)
21508 BLOCK_ABSTRACT (t) = 0;
21509 else
21510 DECL_ABSTRACT_P (t) = 0;
21512 current_function_decl = save_fn;
21513 decl_loc_table = old_decl_loc_table;
21514 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21515 call_arg_locations = old_call_arg_locations;
21516 call_site_count = old_call_site_count;
21517 tail_call_site_count = old_tail_call_site_count;
21520 /* Helper function of premark_used_types() which gets called through
21521 htab_traverse.
21523 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21524 marked as unused by prune_unused_types. */
21526 bool
21527 premark_used_types_helper (tree const &type, void *)
21529 dw_die_ref die;
21531 die = lookup_type_die (type);
21532 if (die != NULL)
21533 die->die_perennial_p = 1;
21534 return true;
21537 /* Helper function of premark_types_used_by_global_vars which gets called
21538 through htab_traverse.
21540 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21541 marked as unused by prune_unused_types. The DIE of the type is marked
21542 only if the global variable using the type will actually be emitted. */
21545 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21546 void *)
21548 struct types_used_by_vars_entry *entry;
21549 dw_die_ref die;
21551 entry = (struct types_used_by_vars_entry *) *slot;
21552 gcc_assert (entry->type != NULL
21553 && entry->var_decl != NULL);
21554 die = lookup_type_die (entry->type);
21555 if (die)
21557 /* Ask cgraph if the global variable really is to be emitted.
21558 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21559 varpool_node *node = varpool_node::get (entry->var_decl);
21560 if (node && node->definition)
21562 die->die_perennial_p = 1;
21563 /* Keep the parent DIEs as well. */
21564 while ((die = die->die_parent) && die->die_perennial_p == 0)
21565 die->die_perennial_p = 1;
21568 return 1;
21571 /* Mark all members of used_types_hash as perennial. */
21573 static void
21574 premark_used_types (struct function *fun)
21576 if (fun && fun->used_types_hash)
21577 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21580 /* Mark all members of types_used_by_vars_entry as perennial. */
21582 static void
21583 premark_types_used_by_global_vars (void)
21585 if (types_used_by_vars_hash)
21586 types_used_by_vars_hash
21587 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21590 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21591 for CA_LOC call arg loc node. */
21593 static dw_die_ref
21594 gen_call_site_die (tree decl, dw_die_ref subr_die,
21595 struct call_arg_loc_node *ca_loc)
21597 dw_die_ref stmt_die = NULL, die;
21598 tree block = ca_loc->block;
21600 while (block
21601 && block != DECL_INITIAL (decl)
21602 && TREE_CODE (block) == BLOCK)
21604 stmt_die = BLOCK_DIE (block);
21605 if (stmt_die)
21606 break;
21607 block = BLOCK_SUPERCONTEXT (block);
21609 if (stmt_die == NULL)
21610 stmt_die = subr_die;
21611 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21612 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21613 if (ca_loc->tail_call_p)
21614 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21615 if (ca_loc->symbol_ref)
21617 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21618 if (tdie)
21619 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21620 else
21621 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21622 false);
21624 return die;
21627 /* Generate a DIE to represent a declared function (either file-scope or
21628 block-local). */
21630 static void
21631 gen_subprogram_die (tree decl, dw_die_ref context_die)
21633 tree origin = decl_ultimate_origin (decl);
21634 dw_die_ref subr_die;
21635 dw_die_ref old_die = lookup_decl_die (decl);
21637 /* This function gets called multiple times for different stages of
21638 the debug process. For example, for func() in this code:
21640 namespace S
21642 void func() { ... }
21645 ...we get called 4 times. Twice in early debug and twice in
21646 late debug:
21648 Early debug
21649 -----------
21651 1. Once while generating func() within the namespace. This is
21652 the declaration. The declaration bit below is set, as the
21653 context is the namespace.
21655 A new DIE will be generated with DW_AT_declaration set.
21657 2. Once for func() itself. This is the specification. The
21658 declaration bit below is clear as the context is the CU.
21660 We will use the cached DIE from (1) to create a new DIE with
21661 DW_AT_specification pointing to the declaration in (1).
21663 Late debug via rest_of_handle_final()
21664 -------------------------------------
21666 3. Once generating func() within the namespace. This is also the
21667 declaration, as in (1), but this time we will early exit below
21668 as we have a cached DIE and a declaration needs no additional
21669 annotations (no locations), as the source declaration line
21670 info is enough.
21672 4. Once for func() itself. As in (2), this is the specification,
21673 but this time we will re-use the cached DIE, and just annotate
21674 it with the location information that should now be available.
21676 For something without namespaces, but with abstract instances, we
21677 are also called a multiple times:
21679 class Base
21681 public:
21682 Base (); // constructor declaration (1)
21685 Base::Base () { } // constructor specification (2)
21687 Early debug
21688 -----------
21690 1. Once for the Base() constructor by virtue of it being a
21691 member of the Base class. This is done via
21692 rest_of_type_compilation.
21694 This is a declaration, so a new DIE will be created with
21695 DW_AT_declaration.
21697 2. Once for the Base() constructor definition, but this time
21698 while generating the abstract instance of the base
21699 constructor (__base_ctor) which is being generated via early
21700 debug of reachable functions.
21702 Even though we have a cached version of the declaration (1),
21703 we will create a DW_AT_specification of the declaration DIE
21704 in (1).
21706 3. Once for the __base_ctor itself, but this time, we generate
21707 an DW_AT_abstract_origin version of the DW_AT_specification in
21708 (2).
21710 Late debug via rest_of_handle_final
21711 -----------------------------------
21713 4. One final time for the __base_ctor (which will have a cached
21714 DIE with DW_AT_abstract_origin created in (3). This time,
21715 we will just annotate the location information now
21716 available.
21718 int declaration = (current_function_decl != decl
21719 || class_or_namespace_scope_p (context_die));
21721 /* Now that the C++ front end lazily declares artificial member fns, we
21722 might need to retrofit the declaration into its class. */
21723 if (!declaration && !origin && !old_die
21724 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21725 && !class_or_namespace_scope_p (context_die)
21726 && debug_info_level > DINFO_LEVEL_TERSE)
21727 old_die = force_decl_die (decl);
21729 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21730 if (origin != NULL)
21732 gcc_assert (!declaration || local_scope_p (context_die));
21734 /* Fixup die_parent for the abstract instance of a nested
21735 inline function. */
21736 if (old_die && old_die->die_parent == NULL)
21737 add_child_die (context_die, old_die);
21739 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21741 /* If we have a DW_AT_abstract_origin we have a working
21742 cached version. */
21743 subr_die = old_die;
21745 else
21747 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21748 add_abstract_origin_attribute (subr_die, origin);
21749 /* This is where the actual code for a cloned function is.
21750 Let's emit linkage name attribute for it. This helps
21751 debuggers to e.g, set breakpoints into
21752 constructors/destructors when the user asks "break
21753 K::K". */
21754 add_linkage_name (subr_die, decl);
21757 /* A cached copy, possibly from early dwarf generation. Reuse as
21758 much as possible. */
21759 else if (old_die)
21761 /* A declaration that has been previously dumped needs no
21762 additional information. */
21763 if (declaration)
21764 return;
21766 if (!get_AT_flag (old_die, DW_AT_declaration)
21767 /* We can have a normal definition following an inline one in the
21768 case of redefinition of GNU C extern inlines.
21769 It seems reasonable to use AT_specification in this case. */
21770 && !get_AT (old_die, DW_AT_inline))
21772 /* Detect and ignore this case, where we are trying to output
21773 something we have already output. */
21774 if (get_AT (old_die, DW_AT_low_pc)
21775 || get_AT (old_die, DW_AT_ranges))
21776 return;
21778 /* If we have no location information, this must be a
21779 partially generated DIE from early dwarf generation.
21780 Fall through and generate it. */
21783 /* If the definition comes from the same place as the declaration,
21784 maybe use the old DIE. We always want the DIE for this function
21785 that has the *_pc attributes to be under comp_unit_die so the
21786 debugger can find it. We also need to do this for abstract
21787 instances of inlines, since the spec requires the out-of-line copy
21788 to have the same parent. For local class methods, this doesn't
21789 apply; we just use the old DIE. */
21790 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21791 struct dwarf_file_data * file_index = lookup_filename (s.file);
21792 if ((is_cu_die (old_die->die_parent)
21793 /* This condition fixes the inconsistency/ICE with the
21794 following Fortran test (or some derivative thereof) while
21795 building libgfortran:
21797 module some_m
21798 contains
21799 logical function funky (FLAG)
21800 funky = .true.
21801 end function
21802 end module
21804 || (old_die->die_parent
21805 && old_die->die_parent->die_tag == DW_TAG_module)
21806 || context_die == NULL)
21807 && (DECL_ARTIFICIAL (decl)
21808 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21809 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21810 == (unsigned) s.line))))
21812 subr_die = old_die;
21814 /* Clear out the declaration attribute, but leave the
21815 parameters so they can be augmented with location
21816 information later. Unless this was a declaration, in
21817 which case, wipe out the nameless parameters and recreate
21818 them further down. */
21819 if (remove_AT (subr_die, DW_AT_declaration))
21822 remove_AT (subr_die, DW_AT_object_pointer);
21823 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21826 /* Make a specification pointing to the previously built
21827 declaration. */
21828 else
21830 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21831 add_AT_specification (subr_die, old_die);
21832 add_pubname (decl, subr_die);
21833 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21834 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21835 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21836 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21838 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21839 emit the real type on the definition die. */
21840 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
21842 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
21843 if (die == auto_die || die == decltype_auto_die)
21844 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21845 TYPE_UNQUALIFIED, false, context_die);
21848 /* When we process the method declaration, we haven't seen
21849 the out-of-class defaulted definition yet, so we have to
21850 recheck now. */
21851 if ((dwarf_version >= 5 || ! dwarf_strict)
21852 && !get_AT (subr_die, DW_AT_defaulted))
21854 int defaulted
21855 = lang_hooks.decls.decl_dwarf_attribute (decl,
21856 DW_AT_defaulted);
21857 if (defaulted != -1)
21859 /* Other values must have been handled before. */
21860 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
21861 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21866 /* Create a fresh DIE for anything else. */
21867 else
21869 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21871 if (TREE_PUBLIC (decl))
21872 add_AT_flag (subr_die, DW_AT_external, 1);
21874 add_name_and_src_coords_attributes (subr_die, decl);
21875 add_pubname (decl, subr_die);
21876 if (debug_info_level > DINFO_LEVEL_TERSE)
21878 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
21879 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21880 TYPE_UNQUALIFIED, false, context_die);
21883 add_pure_or_virtual_attribute (subr_die, decl);
21884 if (DECL_ARTIFICIAL (decl))
21885 add_AT_flag (subr_die, DW_AT_artificial, 1);
21887 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
21888 add_AT_flag (subr_die, DW_AT_noreturn, 1);
21890 add_accessibility_attribute (subr_die, decl);
21893 /* Unless we have an existing non-declaration DIE, equate the new
21894 DIE. */
21895 if (!old_die || is_declaration_die (old_die))
21896 equate_decl_number_to_die (decl, subr_die);
21898 if (declaration)
21900 if (!old_die || !get_AT (old_die, DW_AT_inline))
21902 add_AT_flag (subr_die, DW_AT_declaration, 1);
21904 /* If this is an explicit function declaration then generate
21905 a DW_AT_explicit attribute. */
21906 if ((dwarf_version >= 3 || !dwarf_strict)
21907 && lang_hooks.decls.decl_dwarf_attribute (decl,
21908 DW_AT_explicit) == 1)
21909 add_AT_flag (subr_die, DW_AT_explicit, 1);
21911 /* If this is a C++11 deleted special function member then generate
21912 a DW_AT_deleted attribute. */
21913 if ((dwarf_version >= 5 || !dwarf_strict)
21914 && lang_hooks.decls.decl_dwarf_attribute (decl,
21915 DW_AT_deleted) == 1)
21916 add_AT_flag (subr_die, DW_AT_deleted, 1);
21918 /* If this is a C++11 defaulted special function member then
21919 generate a DW_AT_defaulted attribute. */
21920 if (dwarf_version >= 5 || !dwarf_strict)
21922 int defaulted
21923 = lang_hooks.decls.decl_dwarf_attribute (decl,
21924 DW_AT_defaulted);
21925 if (defaulted != -1)
21926 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21929 /* If this is a C++11 non-static member function with & ref-qualifier
21930 then generate a DW_AT_reference attribute. */
21931 if ((dwarf_version >= 5 || !dwarf_strict)
21932 && lang_hooks.decls.decl_dwarf_attribute (decl,
21933 DW_AT_reference) == 1)
21934 add_AT_flag (subr_die, DW_AT_reference, 1);
21936 /* If this is a C++11 non-static member function with &&
21937 ref-qualifier then generate a DW_AT_reference attribute. */
21938 if ((dwarf_version >= 5 || !dwarf_strict)
21939 && lang_hooks.decls.decl_dwarf_attribute (decl,
21940 DW_AT_rvalue_reference)
21941 == 1)
21942 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
21945 /* Tag abstract instances with DW_AT_inline. */
21946 else if (DECL_ABSTRACT_P (decl))
21948 if (DECL_DECLARED_INLINE_P (decl))
21950 if (cgraph_function_possibly_inlined_p (decl))
21951 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
21952 else
21953 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
21955 else
21957 if (cgraph_function_possibly_inlined_p (decl))
21958 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
21959 else
21960 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
21963 if (DECL_DECLARED_INLINE_P (decl)
21964 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21965 add_AT_flag (subr_die, DW_AT_artificial, 1);
21967 /* For non DECL_EXTERNALs, if range information is available, fill
21968 the DIE with it. */
21969 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
21971 HOST_WIDE_INT cfa_fb_offset;
21973 struct function *fun = DECL_STRUCT_FUNCTION (decl);
21975 if (!flag_reorder_blocks_and_partition)
21977 dw_fde_ref fde = fun->fde;
21978 if (fde->dw_fde_begin)
21980 /* We have already generated the labels. */
21981 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
21982 fde->dw_fde_end, false);
21984 else
21986 /* Create start/end labels and add the range. */
21987 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
21988 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
21989 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
21990 current_function_funcdef_no);
21991 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
21992 current_function_funcdef_no);
21993 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
21994 false);
21997 #if VMS_DEBUGGING_INFO
21998 /* HP OpenVMS Industry Standard 64: DWARF Extensions
21999 Section 2.3 Prologue and Epilogue Attributes:
22000 When a breakpoint is set on entry to a function, it is generally
22001 desirable for execution to be suspended, not on the very first
22002 instruction of the function, but rather at a point after the
22003 function's frame has been set up, after any language defined local
22004 declaration processing has been completed, and before execution of
22005 the first statement of the function begins. Debuggers generally
22006 cannot properly determine where this point is. Similarly for a
22007 breakpoint set on exit from a function. The prologue and epilogue
22008 attributes allow a compiler to communicate the location(s) to use. */
22011 if (fde->dw_fde_vms_end_prologue)
22012 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22013 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22015 if (fde->dw_fde_vms_begin_epilogue)
22016 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22017 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22019 #endif
22022 else
22024 /* Generate pubnames entries for the split function code ranges. */
22025 dw_fde_ref fde = fun->fde;
22027 if (fde->dw_fde_second_begin)
22029 if (dwarf_version >= 3 || !dwarf_strict)
22031 /* We should use ranges for non-contiguous code section
22032 addresses. Use the actual code range for the initial
22033 section, since the HOT/COLD labels might precede an
22034 alignment offset. */
22035 bool range_list_added = false;
22036 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22037 fde->dw_fde_end, &range_list_added,
22038 false);
22039 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22040 fde->dw_fde_second_end,
22041 &range_list_added, false);
22042 if (range_list_added)
22043 add_ranges (NULL);
22045 else
22047 /* There is no real support in DW2 for this .. so we make
22048 a work-around. First, emit the pub name for the segment
22049 containing the function label. Then make and emit a
22050 simplified subprogram DIE for the second segment with the
22051 name pre-fixed by __hot/cold_sect_of_. We use the same
22052 linkage name for the second die so that gdb will find both
22053 sections when given "b foo". */
22054 const char *name = NULL;
22055 tree decl_name = DECL_NAME (decl);
22056 dw_die_ref seg_die;
22058 /* Do the 'primary' section. */
22059 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22060 fde->dw_fde_end, false);
22062 /* Build a minimal DIE for the secondary section. */
22063 seg_die = new_die (DW_TAG_subprogram,
22064 subr_die->die_parent, decl);
22066 if (TREE_PUBLIC (decl))
22067 add_AT_flag (seg_die, DW_AT_external, 1);
22069 if (decl_name != NULL
22070 && IDENTIFIER_POINTER (decl_name) != NULL)
22072 name = dwarf2_name (decl, 1);
22073 if (! DECL_ARTIFICIAL (decl))
22074 add_src_coords_attributes (seg_die, decl);
22076 add_linkage_name (seg_die, decl);
22078 gcc_assert (name != NULL);
22079 add_pure_or_virtual_attribute (seg_die, decl);
22080 if (DECL_ARTIFICIAL (decl))
22081 add_AT_flag (seg_die, DW_AT_artificial, 1);
22083 name = concat ("__second_sect_of_", name, NULL);
22084 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22085 fde->dw_fde_second_end, false);
22086 add_name_attribute (seg_die, name);
22087 if (want_pubnames ())
22088 add_pubname_string (name, seg_die);
22091 else
22092 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22093 false);
22096 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22098 /* We define the "frame base" as the function's CFA. This is more
22099 convenient for several reasons: (1) It's stable across the prologue
22100 and epilogue, which makes it better than just a frame pointer,
22101 (2) With dwarf3, there exists a one-byte encoding that allows us
22102 to reference the .debug_frame data by proxy, but failing that,
22103 (3) We can at least reuse the code inspection and interpretation
22104 code that determines the CFA position at various points in the
22105 function. */
22106 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22108 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22109 add_AT_loc (subr_die, DW_AT_frame_base, op);
22111 else
22113 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22114 if (list->dw_loc_next)
22115 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22116 else
22117 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22120 /* Compute a displacement from the "steady-state frame pointer" to
22121 the CFA. The former is what all stack slots and argument slots
22122 will reference in the rtl; the latter is what we've told the
22123 debugger about. We'll need to adjust all frame_base references
22124 by this displacement. */
22125 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22127 if (fun->static_chain_decl)
22129 /* DWARF requires here a location expression that computes the
22130 address of the enclosing subprogram's frame base. The machinery
22131 in tree-nested.c is supposed to store this specific address in the
22132 last field of the FRAME record. */
22133 const tree frame_type
22134 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22135 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22137 tree fb_expr
22138 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22139 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22140 fb_expr, fb_decl, NULL_TREE);
22142 add_AT_location_description (subr_die, DW_AT_static_link,
22143 loc_list_from_tree (fb_expr, 0, NULL));
22147 /* Generate child dies for template paramaters. */
22148 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22149 gen_generic_params_dies (decl);
22151 /* Now output descriptions of the arguments for this function. This gets
22152 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22153 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22154 `...' at the end of the formal parameter list. In order to find out if
22155 there was a trailing ellipsis or not, we must instead look at the type
22156 associated with the FUNCTION_DECL. This will be a node of type
22157 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22158 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22159 an ellipsis at the end. */
22161 /* In the case where we are describing a mere function declaration, all we
22162 need to do here (and all we *can* do here) is to describe the *types* of
22163 its formal parameters. */
22164 if (debug_info_level <= DINFO_LEVEL_TERSE)
22166 else if (declaration)
22167 gen_formal_types_die (decl, subr_die);
22168 else
22170 /* Generate DIEs to represent all known formal parameters. */
22171 tree parm = DECL_ARGUMENTS (decl);
22172 tree generic_decl = early_dwarf
22173 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22174 tree generic_decl_parm = generic_decl
22175 ? DECL_ARGUMENTS (generic_decl)
22176 : NULL;
22177 auto_vec<dw_die_ref> string_types_vec;
22178 if (string_types == NULL)
22179 string_types = &string_types_vec;
22181 /* Now we want to walk the list of parameters of the function and
22182 emit their relevant DIEs.
22184 We consider the case of DECL being an instance of a generic function
22185 as well as it being a normal function.
22187 If DECL is an instance of a generic function we walk the
22188 parameters of the generic function declaration _and_ the parameters of
22189 DECL itself. This is useful because we want to emit specific DIEs for
22190 function parameter packs and those are declared as part of the
22191 generic function declaration. In that particular case,
22192 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22193 That DIE has children DIEs representing the set of arguments
22194 of the pack. Note that the set of pack arguments can be empty.
22195 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22196 children DIE.
22198 Otherwise, we just consider the parameters of DECL. */
22199 while (generic_decl_parm || parm)
22201 if (generic_decl_parm
22202 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22203 gen_formal_parameter_pack_die (generic_decl_parm,
22204 parm, subr_die,
22205 &parm);
22206 else if (parm && !POINTER_BOUNDS_P (parm))
22208 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22210 if (parm == DECL_ARGUMENTS (decl)
22211 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22212 && parm_die
22213 && (dwarf_version >= 3 || !dwarf_strict))
22214 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22216 parm = DECL_CHAIN (parm);
22218 else if (parm)
22219 parm = DECL_CHAIN (parm);
22221 if (generic_decl_parm)
22222 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22225 /* Decide whether we need an unspecified_parameters DIE at the end.
22226 There are 2 more cases to do this for: 1) the ansi ... declaration -
22227 this is detectable when the end of the arg list is not a
22228 void_type_node 2) an unprototyped function declaration (not a
22229 definition). This just means that we have no info about the
22230 parameters at all. */
22231 if (early_dwarf)
22233 if (prototype_p (TREE_TYPE (decl)))
22235 /* This is the prototyped case, check for.... */
22236 if (stdarg_p (TREE_TYPE (decl)))
22237 gen_unspecified_parameters_die (decl, subr_die);
22239 else if (DECL_INITIAL (decl) == NULL_TREE)
22240 gen_unspecified_parameters_die (decl, subr_die);
22243 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
22244 have DIEs. */
22245 if (string_types == &string_types_vec)
22247 adjust_string_types ();
22248 string_types = NULL;
22252 if (subr_die != old_die)
22253 /* Add the calling convention attribute if requested. */
22254 add_calling_convention_attribute (subr_die, decl);
22256 /* Output Dwarf info for all of the stuff within the body of the function
22257 (if it has one - it may be just a declaration).
22259 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22260 a function. This BLOCK actually represents the outermost binding contour
22261 for the function, i.e. the contour in which the function's formal
22262 parameters and labels get declared. Curiously, it appears that the front
22263 end doesn't actually put the PARM_DECL nodes for the current function onto
22264 the BLOCK_VARS list for this outer scope, but are strung off of the
22265 DECL_ARGUMENTS list for the function instead.
22267 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22268 the LABEL_DECL nodes for the function however, and we output DWARF info
22269 for those in decls_for_scope. Just within the `outer_scope' there will be
22270 a BLOCK node representing the function's outermost pair of curly braces,
22271 and any blocks used for the base and member initializers of a C++
22272 constructor function. */
22273 tree outer_scope = DECL_INITIAL (decl);
22274 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22276 int call_site_note_count = 0;
22277 int tail_call_site_note_count = 0;
22279 /* Emit a DW_TAG_variable DIE for a named return value. */
22280 if (DECL_NAME (DECL_RESULT (decl)))
22281 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22283 /* The first time through decls_for_scope we will generate the
22284 DIEs for the locals. The second time, we fill in the
22285 location info. */
22286 decls_for_scope (outer_scope, subr_die);
22288 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22290 struct call_arg_loc_node *ca_loc;
22291 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22293 dw_die_ref die = NULL;
22294 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22295 rtx arg, next_arg;
22297 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22298 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22299 : NULL_RTX);
22300 arg; arg = next_arg)
22302 dw_loc_descr_ref reg, val;
22303 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22304 dw_die_ref cdie, tdie = NULL;
22306 next_arg = XEXP (arg, 1);
22307 if (REG_P (XEXP (XEXP (arg, 0), 0))
22308 && next_arg
22309 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22310 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22311 && REGNO (XEXP (XEXP (arg, 0), 0))
22312 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22313 next_arg = XEXP (next_arg, 1);
22314 if (mode == VOIDmode)
22316 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22317 if (mode == VOIDmode)
22318 mode = GET_MODE (XEXP (arg, 0));
22320 if (mode == VOIDmode || mode == BLKmode)
22321 continue;
22322 /* Get dynamic information about call target only if we
22323 have no static information: we cannot generate both
22324 DW_AT_call_origin and DW_AT_call_target
22325 attributes. */
22326 if (ca_loc->symbol_ref == NULL_RTX)
22328 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22330 tloc = XEXP (XEXP (arg, 0), 1);
22331 continue;
22333 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22334 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22336 tlocc = XEXP (XEXP (arg, 0), 1);
22337 continue;
22340 reg = NULL;
22341 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22342 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22343 VAR_INIT_STATUS_INITIALIZED);
22344 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22346 rtx mem = XEXP (XEXP (arg, 0), 0);
22347 reg = mem_loc_descriptor (XEXP (mem, 0),
22348 get_address_mode (mem),
22349 GET_MODE (mem),
22350 VAR_INIT_STATUS_INITIALIZED);
22352 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22353 == DEBUG_PARAMETER_REF)
22355 tree tdecl
22356 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22357 tdie = lookup_decl_die (tdecl);
22358 if (tdie == NULL)
22359 continue;
22361 else
22362 continue;
22363 if (reg == NULL
22364 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22365 != DEBUG_PARAMETER_REF)
22366 continue;
22367 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22368 VOIDmode,
22369 VAR_INIT_STATUS_INITIALIZED);
22370 if (val == NULL)
22371 continue;
22372 if (die == NULL)
22373 die = gen_call_site_die (decl, subr_die, ca_loc);
22374 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22375 NULL_TREE);
22376 if (reg != NULL)
22377 add_AT_loc (cdie, DW_AT_location, reg);
22378 else if (tdie != NULL)
22379 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22380 tdie);
22381 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22382 if (next_arg != XEXP (arg, 1))
22384 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22385 if (mode == VOIDmode)
22386 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22387 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22388 0), 1),
22389 mode, VOIDmode,
22390 VAR_INIT_STATUS_INITIALIZED);
22391 if (val != NULL)
22392 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22393 val);
22396 if (die == NULL
22397 && (ca_loc->symbol_ref || tloc))
22398 die = gen_call_site_die (decl, subr_die, ca_loc);
22399 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22401 dw_loc_descr_ref tval = NULL;
22403 if (tloc != NULL_RTX)
22404 tval = mem_loc_descriptor (tloc,
22405 GET_MODE (tloc) == VOIDmode
22406 ? Pmode : GET_MODE (tloc),
22407 VOIDmode,
22408 VAR_INIT_STATUS_INITIALIZED);
22409 if (tval)
22410 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22411 else if (tlocc != NULL_RTX)
22413 tval = mem_loc_descriptor (tlocc,
22414 GET_MODE (tlocc) == VOIDmode
22415 ? Pmode : GET_MODE (tlocc),
22416 VOIDmode,
22417 VAR_INIT_STATUS_INITIALIZED);
22418 if (tval)
22419 add_AT_loc (die,
22420 dwarf_AT (DW_AT_call_target_clobbered),
22421 tval);
22424 if (die != NULL)
22426 call_site_note_count++;
22427 if (ca_loc->tail_call_p)
22428 tail_call_site_note_count++;
22432 call_arg_locations = NULL;
22433 call_arg_loc_last = NULL;
22434 if (tail_call_site_count >= 0
22435 && tail_call_site_count == tail_call_site_note_count
22436 && (!dwarf_strict || dwarf_version >= 5))
22438 if (call_site_count >= 0
22439 && call_site_count == call_site_note_count)
22440 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22441 else
22442 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22444 call_site_count = -1;
22445 tail_call_site_count = -1;
22448 /* Mark used types after we have created DIEs for the functions scopes. */
22449 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22452 /* Returns a hash value for X (which really is a die_struct). */
22454 hashval_t
22455 block_die_hasher::hash (die_struct *d)
22457 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22460 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22461 as decl_id and die_parent of die_struct Y. */
22463 bool
22464 block_die_hasher::equal (die_struct *x, die_struct *y)
22466 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22469 /* Return TRUE if DECL, which may have been previously generated as
22470 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22471 true if decl (or its origin) is either an extern declaration or a
22472 class/namespace scoped declaration.
22474 The declare_in_namespace support causes us to get two DIEs for one
22475 variable, both of which are declarations. We want to avoid
22476 considering one to be a specification, so we must test for
22477 DECLARATION and DW_AT_declaration. */
22478 static inline bool
22479 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22481 return (old_die && TREE_STATIC (decl) && !declaration
22482 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22485 /* Return true if DECL is a local static. */
22487 static inline bool
22488 local_function_static (tree decl)
22490 gcc_assert (VAR_P (decl));
22491 return TREE_STATIC (decl)
22492 && DECL_CONTEXT (decl)
22493 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22496 /* Generate a DIE to represent a declared data object.
22497 Either DECL or ORIGIN must be non-null. */
22499 static void
22500 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22502 HOST_WIDE_INT off = 0;
22503 tree com_decl;
22504 tree decl_or_origin = decl ? decl : origin;
22505 tree ultimate_origin;
22506 dw_die_ref var_die;
22507 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22508 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22509 || class_or_namespace_scope_p (context_die));
22510 bool specialization_p = false;
22511 bool no_linkage_name = false;
22513 /* While C++ inline static data members have definitions inside of the
22514 class, force the first DIE to be a declaration, then let gen_member_die
22515 reparent it to the class context and call gen_variable_die again
22516 to create the outside of the class DIE for the definition. */
22517 if (!declaration
22518 && old_die == NULL
22519 && decl
22520 && DECL_CONTEXT (decl)
22521 && TYPE_P (DECL_CONTEXT (decl))
22522 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22524 declaration = true;
22525 no_linkage_name = true;
22528 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22529 if (decl || ultimate_origin)
22530 origin = ultimate_origin;
22531 com_decl = fortran_common (decl_or_origin, &off);
22533 /* Symbol in common gets emitted as a child of the common block, in the form
22534 of a data member. */
22535 if (com_decl)
22537 dw_die_ref com_die;
22538 dw_loc_list_ref loc = NULL;
22539 die_node com_die_arg;
22541 var_die = lookup_decl_die (decl_or_origin);
22542 if (var_die)
22544 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22546 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22547 if (loc)
22549 if (off)
22551 /* Optimize the common case. */
22552 if (single_element_loc_list_p (loc)
22553 && loc->expr->dw_loc_opc == DW_OP_addr
22554 && loc->expr->dw_loc_next == NULL
22555 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22556 == SYMBOL_REF)
22558 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22559 loc->expr->dw_loc_oprnd1.v.val_addr
22560 = plus_constant (GET_MODE (x), x , off);
22562 else
22563 loc_list_plus_const (loc, off);
22565 add_AT_location_description (var_die, DW_AT_location, loc);
22566 remove_AT (var_die, DW_AT_declaration);
22569 return;
22572 if (common_block_die_table == NULL)
22573 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22575 com_die_arg.decl_id = DECL_UID (com_decl);
22576 com_die_arg.die_parent = context_die;
22577 com_die = common_block_die_table->find (&com_die_arg);
22578 if (! early_dwarf)
22579 loc = loc_list_from_tree (com_decl, 2, NULL);
22580 if (com_die == NULL)
22582 const char *cnam
22583 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22584 die_node **slot;
22586 com_die = new_die (DW_TAG_common_block, context_die, decl);
22587 add_name_and_src_coords_attributes (com_die, com_decl);
22588 if (loc)
22590 add_AT_location_description (com_die, DW_AT_location, loc);
22591 /* Avoid sharing the same loc descriptor between
22592 DW_TAG_common_block and DW_TAG_variable. */
22593 loc = loc_list_from_tree (com_decl, 2, NULL);
22595 else if (DECL_EXTERNAL (decl_or_origin))
22596 add_AT_flag (com_die, DW_AT_declaration, 1);
22597 if (want_pubnames ())
22598 add_pubname_string (cnam, com_die); /* ??? needed? */
22599 com_die->decl_id = DECL_UID (com_decl);
22600 slot = common_block_die_table->find_slot (com_die, INSERT);
22601 *slot = com_die;
22603 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22605 add_AT_location_description (com_die, DW_AT_location, loc);
22606 loc = loc_list_from_tree (com_decl, 2, NULL);
22607 remove_AT (com_die, DW_AT_declaration);
22609 var_die = new_die (DW_TAG_variable, com_die, decl);
22610 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22611 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22612 decl_quals (decl_or_origin), false,
22613 context_die);
22614 add_AT_flag (var_die, DW_AT_external, 1);
22615 if (loc)
22617 if (off)
22619 /* Optimize the common case. */
22620 if (single_element_loc_list_p (loc)
22621 && loc->expr->dw_loc_opc == DW_OP_addr
22622 && loc->expr->dw_loc_next == NULL
22623 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22625 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22626 loc->expr->dw_loc_oprnd1.v.val_addr
22627 = plus_constant (GET_MODE (x), x, off);
22629 else
22630 loc_list_plus_const (loc, off);
22632 add_AT_location_description (var_die, DW_AT_location, loc);
22634 else if (DECL_EXTERNAL (decl_or_origin))
22635 add_AT_flag (var_die, DW_AT_declaration, 1);
22636 if (decl)
22637 equate_decl_number_to_die (decl, var_die);
22638 return;
22641 if (old_die)
22643 if (declaration)
22645 /* A declaration that has been previously dumped, needs no
22646 further annotations, since it doesn't need location on
22647 the second pass. */
22648 return;
22650 else if (decl_will_get_specification_p (old_die, decl, declaration)
22651 && !get_AT (old_die, DW_AT_specification))
22653 /* Fall-thru so we can make a new variable die along with a
22654 DW_AT_specification. */
22656 else if (origin && old_die->die_parent != context_die)
22658 /* If we will be creating an inlined instance, we need a
22659 new DIE that will get annotated with
22660 DW_AT_abstract_origin. Clear things so we can get a
22661 new DIE. */
22662 gcc_assert (!DECL_ABSTRACT_P (decl));
22663 old_die = NULL;
22665 else
22667 /* If a DIE was dumped early, it still needs location info.
22668 Skip to where we fill the location bits. */
22669 var_die = old_die;
22670 goto gen_variable_die_location;
22674 /* For static data members, the declaration in the class is supposed
22675 to have DW_TAG_member tag; the specification should still be
22676 DW_TAG_variable referencing the DW_TAG_member DIE. */
22677 if (declaration && class_scope_p (context_die))
22678 var_die = new_die (DW_TAG_member, context_die, decl);
22679 else
22680 var_die = new_die (DW_TAG_variable, context_die, decl);
22682 if (origin != NULL)
22683 add_abstract_origin_attribute (var_die, origin);
22685 /* Loop unrolling can create multiple blocks that refer to the same
22686 static variable, so we must test for the DW_AT_declaration flag.
22688 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22689 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22690 sharing them.
22692 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22693 else if (decl_will_get_specification_p (old_die, decl, declaration))
22695 /* This is a definition of a C++ class level static. */
22696 add_AT_specification (var_die, old_die);
22697 specialization_p = true;
22698 if (DECL_NAME (decl))
22700 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22701 struct dwarf_file_data * file_index = lookup_filename (s.file);
22703 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22704 add_AT_file (var_die, DW_AT_decl_file, file_index);
22706 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22707 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22709 if (old_die->die_tag == DW_TAG_member)
22710 add_linkage_name (var_die, decl);
22713 else
22714 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22716 if ((origin == NULL && !specialization_p)
22717 || (origin != NULL
22718 && !DECL_ABSTRACT_P (decl_or_origin)
22719 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22720 decl_function_context
22721 (decl_or_origin))))
22723 tree type = TREE_TYPE (decl_or_origin);
22725 if (decl_by_reference_p (decl_or_origin))
22726 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22727 context_die);
22728 else
22729 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22730 context_die);
22733 if (origin == NULL && !specialization_p)
22735 if (TREE_PUBLIC (decl))
22736 add_AT_flag (var_die, DW_AT_external, 1);
22738 if (DECL_ARTIFICIAL (decl))
22739 add_AT_flag (var_die, DW_AT_artificial, 1);
22741 add_accessibility_attribute (var_die, decl);
22744 if (declaration)
22745 add_AT_flag (var_die, DW_AT_declaration, 1);
22747 if (decl && (DECL_ABSTRACT_P (decl)
22748 || !old_die || is_declaration_die (old_die)))
22749 equate_decl_number_to_die (decl, var_die);
22751 gen_variable_die_location:
22752 if (! declaration
22753 && (! DECL_ABSTRACT_P (decl_or_origin)
22754 /* Local static vars are shared between all clones/inlines,
22755 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22756 already set. */
22757 || (VAR_P (decl_or_origin)
22758 && TREE_STATIC (decl_or_origin)
22759 && DECL_RTL_SET_P (decl_or_origin))))
22761 if (early_dwarf)
22762 add_pubname (decl_or_origin, var_die);
22763 else
22764 add_location_or_const_value_attribute (var_die, decl_or_origin,
22765 decl == NULL);
22767 else
22768 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22770 if ((dwarf_version >= 4 || !dwarf_strict)
22771 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22772 DW_AT_const_expr) == 1
22773 && !get_AT (var_die, DW_AT_const_expr)
22774 && !specialization_p)
22775 add_AT_flag (var_die, DW_AT_const_expr, 1);
22777 if (!dwarf_strict)
22779 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22780 DW_AT_inline);
22781 if (inl != -1
22782 && !get_AT (var_die, DW_AT_inline)
22783 && !specialization_p)
22784 add_AT_unsigned (var_die, DW_AT_inline, inl);
22788 /* Generate a DIE to represent a named constant. */
22790 static void
22791 gen_const_die (tree decl, dw_die_ref context_die)
22793 dw_die_ref const_die;
22794 tree type = TREE_TYPE (decl);
22796 const_die = lookup_decl_die (decl);
22797 if (const_die)
22798 return;
22800 const_die = new_die (DW_TAG_constant, context_die, decl);
22801 equate_decl_number_to_die (decl, const_die);
22802 add_name_and_src_coords_attributes (const_die, decl);
22803 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22804 if (TREE_PUBLIC (decl))
22805 add_AT_flag (const_die, DW_AT_external, 1);
22806 if (DECL_ARTIFICIAL (decl))
22807 add_AT_flag (const_die, DW_AT_artificial, 1);
22808 tree_add_const_value_attribute_for_decl (const_die, decl);
22811 /* Generate a DIE to represent a label identifier. */
22813 static void
22814 gen_label_die (tree decl, dw_die_ref context_die)
22816 tree origin = decl_ultimate_origin (decl);
22817 dw_die_ref lbl_die = lookup_decl_die (decl);
22818 rtx insn;
22819 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22821 if (!lbl_die)
22823 lbl_die = new_die (DW_TAG_label, context_die, decl);
22824 equate_decl_number_to_die (decl, lbl_die);
22826 if (origin != NULL)
22827 add_abstract_origin_attribute (lbl_die, origin);
22828 else
22829 add_name_and_src_coords_attributes (lbl_die, decl);
22832 if (DECL_ABSTRACT_P (decl))
22833 equate_decl_number_to_die (decl, lbl_die);
22834 else if (! early_dwarf)
22836 insn = DECL_RTL_IF_SET (decl);
22838 /* Deleted labels are programmer specified labels which have been
22839 eliminated because of various optimizations. We still emit them
22840 here so that it is possible to put breakpoints on them. */
22841 if (insn
22842 && (LABEL_P (insn)
22843 || ((NOTE_P (insn)
22844 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
22846 /* When optimization is enabled (via -O) some parts of the compiler
22847 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
22848 represent source-level labels which were explicitly declared by
22849 the user. This really shouldn't be happening though, so catch
22850 it if it ever does happen. */
22851 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
22853 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
22854 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22856 else if (insn
22857 && NOTE_P (insn)
22858 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
22859 && CODE_LABEL_NUMBER (insn) != -1)
22861 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
22862 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22867 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
22868 attributes to the DIE for a block STMT, to describe where the inlined
22869 function was called from. This is similar to add_src_coords_attributes. */
22871 static inline void
22872 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
22874 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
22876 if (dwarf_version >= 3 || !dwarf_strict)
22878 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
22879 add_AT_unsigned (die, DW_AT_call_line, s.line);
22884 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
22885 Add low_pc and high_pc attributes to the DIE for a block STMT. */
22887 static inline void
22888 add_high_low_attributes (tree stmt, dw_die_ref die)
22890 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22892 if (BLOCK_FRAGMENT_CHAIN (stmt)
22893 && (dwarf_version >= 3 || !dwarf_strict))
22895 tree chain, superblock = NULL_TREE;
22896 dw_die_ref pdie;
22897 dw_attr_node *attr = NULL;
22899 if (inlined_function_outer_scope_p (stmt))
22901 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22902 BLOCK_NUMBER (stmt));
22903 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22906 /* Optimize duplicate .debug_ranges lists or even tails of
22907 lists. If this BLOCK has same ranges as its supercontext,
22908 lookup DW_AT_ranges attribute in the supercontext (and
22909 recursively so), verify that the ranges_table contains the
22910 right values and use it instead of adding a new .debug_range. */
22911 for (chain = stmt, pdie = die;
22912 BLOCK_SAME_RANGE (chain);
22913 chain = BLOCK_SUPERCONTEXT (chain))
22915 dw_attr_node *new_attr;
22917 pdie = pdie->die_parent;
22918 if (pdie == NULL)
22919 break;
22920 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
22921 break;
22922 new_attr = get_AT (pdie, DW_AT_ranges);
22923 if (new_attr == NULL
22924 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
22925 break;
22926 attr = new_attr;
22927 superblock = BLOCK_SUPERCONTEXT (chain);
22929 if (attr != NULL
22930 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
22931 == BLOCK_NUMBER (superblock))
22932 && BLOCK_FRAGMENT_CHAIN (superblock))
22934 unsigned long off = attr->dw_attr_val.v.val_offset;
22935 unsigned long supercnt = 0, thiscnt = 0;
22936 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
22937 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
22939 ++supercnt;
22940 gcc_checking_assert ((*ranges_table)[off + supercnt].num
22941 == BLOCK_NUMBER (chain));
22943 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
22944 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
22945 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
22946 ++thiscnt;
22947 gcc_assert (supercnt >= thiscnt);
22948 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
22949 false);
22950 note_rnglist_head (off + supercnt - thiscnt);
22951 return;
22954 unsigned int offset = add_ranges (stmt, true);
22955 add_AT_range_list (die, DW_AT_ranges, offset, false);
22956 note_rnglist_head (offset);
22958 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
22959 chain = BLOCK_FRAGMENT_CHAIN (stmt);
22962 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
22963 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
22964 chain = BLOCK_FRAGMENT_CHAIN (chain);
22966 while (chain);
22967 add_ranges (NULL);
22969 else
22971 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
22972 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22973 BLOCK_NUMBER (stmt));
22974 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
22975 BLOCK_NUMBER (stmt));
22976 add_AT_low_high_pc (die, label, label_high, false);
22980 /* Generate a DIE for a lexical block. */
22982 static void
22983 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
22985 dw_die_ref old_die = BLOCK_DIE (stmt);
22986 dw_die_ref stmt_die = NULL;
22987 if (!old_die)
22989 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
22990 BLOCK_DIE (stmt) = stmt_die;
22993 if (BLOCK_ABSTRACT (stmt))
22995 if (old_die)
22997 /* This must have been generated early and it won't even
22998 need location information since it's a DW_AT_inline
22999 function. */
23000 if (flag_checking)
23001 for (dw_die_ref c = context_die; c; c = c->die_parent)
23002 if (c->die_tag == DW_TAG_inlined_subroutine
23003 || c->die_tag == DW_TAG_subprogram)
23005 gcc_assert (get_AT (c, DW_AT_inline));
23006 break;
23008 return;
23011 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23013 /* If this is an inlined instance, create a new lexical die for
23014 anything below to attach DW_AT_abstract_origin to. */
23015 if (old_die)
23017 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23018 BLOCK_DIE (stmt) = stmt_die;
23019 old_die = NULL;
23022 tree origin = block_ultimate_origin (stmt);
23023 if (origin != NULL_TREE && origin != stmt)
23024 add_abstract_origin_attribute (stmt_die, origin);
23027 if (old_die)
23028 stmt_die = old_die;
23030 /* A non abstract block whose blocks have already been reordered
23031 should have the instruction range for this block. If so, set the
23032 high/low attributes. */
23033 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23035 gcc_assert (stmt_die);
23036 add_high_low_attributes (stmt, stmt_die);
23039 decls_for_scope (stmt, stmt_die);
23042 /* Generate a DIE for an inlined subprogram. */
23044 static void
23045 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23047 tree decl;
23049 /* The instance of function that is effectively being inlined shall not
23050 be abstract. */
23051 gcc_assert (! BLOCK_ABSTRACT (stmt));
23053 decl = block_ultimate_origin (stmt);
23055 /* Make sure any inlined functions are known to be inlineable. */
23056 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23057 || cgraph_function_possibly_inlined_p (decl));
23059 /* Emit info for the abstract instance first, if we haven't yet. We
23060 must emit this even if the block is abstract, otherwise when we
23061 emit the block below (or elsewhere), we may end up trying to emit
23062 a die whose origin die hasn't been emitted, and crashing. */
23063 dwarf2out_abstract_function (decl);
23065 if (! BLOCK_ABSTRACT (stmt))
23067 dw_die_ref subr_die
23068 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23070 if (call_arg_locations)
23071 BLOCK_DIE (stmt) = subr_die;
23072 add_abstract_origin_attribute (subr_die, decl);
23073 if (TREE_ASM_WRITTEN (stmt))
23074 add_high_low_attributes (stmt, subr_die);
23075 add_call_src_coords_attributes (stmt, subr_die);
23077 decls_for_scope (stmt, subr_die);
23081 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23082 the comment for VLR_CONTEXT. */
23084 static void
23085 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23087 dw_die_ref decl_die;
23089 if (TREE_TYPE (decl) == error_mark_node)
23090 return;
23092 decl_die = new_die (DW_TAG_member, context_die, decl);
23093 add_name_and_src_coords_attributes (decl_die, decl);
23094 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23095 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23096 context_die);
23098 if (DECL_BIT_FIELD_TYPE (decl))
23100 add_byte_size_attribute (decl_die, decl);
23101 add_bit_size_attribute (decl_die, decl);
23102 add_bit_offset_attribute (decl_die, decl, ctx);
23105 /* If we have a variant part offset, then we are supposed to process a member
23106 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23107 trees. */
23108 gcc_assert (ctx->variant_part_offset == NULL_TREE
23109 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23110 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23111 add_data_member_location_attribute (decl_die, decl, ctx);
23113 if (DECL_ARTIFICIAL (decl))
23114 add_AT_flag (decl_die, DW_AT_artificial, 1);
23116 add_accessibility_attribute (decl_die, decl);
23118 /* Equate decl number to die, so that we can look up this decl later on. */
23119 equate_decl_number_to_die (decl, decl_die);
23122 #if 0
23123 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23124 Use modified_type_die instead.
23125 We keep this code here just in case these types of DIEs may be needed to
23126 represent certain things in other languages (e.g. Pascal) someday. */
23128 static void
23129 gen_pointer_type_die (tree type, dw_die_ref context_die)
23131 dw_die_ref ptr_die
23132 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23134 equate_type_number_to_die (type, ptr_die);
23135 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23136 context_die);
23137 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23140 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23141 Use modified_type_die instead.
23142 We keep this code here just in case these types of DIEs may be needed to
23143 represent certain things in other languages (e.g. Pascal) someday. */
23145 static void
23146 gen_reference_type_die (tree type, dw_die_ref context_die)
23148 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23150 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23151 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23152 else
23153 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23155 equate_type_number_to_die (type, ref_die);
23156 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23157 context_die);
23158 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23160 #endif
23162 /* Generate a DIE for a pointer to a member type. TYPE can be an
23163 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23164 pointer to member function. */
23166 static void
23167 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23169 if (lookup_type_die (type))
23170 return;
23172 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23173 scope_die_for (type, context_die), type);
23175 equate_type_number_to_die (type, ptr_die);
23176 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23177 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23178 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23179 context_die);
23181 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23182 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23184 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23185 add_AT_loc (ptr_die, DW_AT_use_location, op);
23189 static char *producer_string;
23191 /* Return a heap allocated producer string including command line options
23192 if -grecord-gcc-switches. */
23194 static char *
23195 gen_producer_string (void)
23197 size_t j;
23198 auto_vec<const char *> switches;
23199 const char *language_string = lang_hooks.name;
23200 char *producer, *tail;
23201 const char *p;
23202 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23203 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23205 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23206 switch (save_decoded_options[j].opt_index)
23208 case OPT_o:
23209 case OPT_d:
23210 case OPT_dumpbase:
23211 case OPT_dumpdir:
23212 case OPT_auxbase:
23213 case OPT_auxbase_strip:
23214 case OPT_quiet:
23215 case OPT_version:
23216 case OPT_v:
23217 case OPT_w:
23218 case OPT_L:
23219 case OPT_D:
23220 case OPT_I:
23221 case OPT_U:
23222 case OPT_SPECIAL_unknown:
23223 case OPT_SPECIAL_ignore:
23224 case OPT_SPECIAL_program_name:
23225 case OPT_SPECIAL_input_file:
23226 case OPT_grecord_gcc_switches:
23227 case OPT_gno_record_gcc_switches:
23228 case OPT__output_pch_:
23229 case OPT_fdiagnostics_show_location_:
23230 case OPT_fdiagnostics_show_option:
23231 case OPT_fdiagnostics_show_caret:
23232 case OPT_fdiagnostics_color_:
23233 case OPT_fverbose_asm:
23234 case OPT____:
23235 case OPT__sysroot_:
23236 case OPT_nostdinc:
23237 case OPT_nostdinc__:
23238 case OPT_fpreprocessed:
23239 case OPT_fltrans_output_list_:
23240 case OPT_fresolution_:
23241 case OPT_fdebug_prefix_map_:
23242 /* Ignore these. */
23243 continue;
23244 default:
23245 if (cl_options[save_decoded_options[j].opt_index].flags
23246 & CL_NO_DWARF_RECORD)
23247 continue;
23248 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23249 == '-');
23250 switch (save_decoded_options[j].canonical_option[0][1])
23252 case 'M':
23253 case 'i':
23254 case 'W':
23255 continue;
23256 case 'f':
23257 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23258 "dump", 4) == 0)
23259 continue;
23260 break;
23261 default:
23262 break;
23264 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23265 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23266 break;
23269 producer = XNEWVEC (char, plen + 1 + len + 1);
23270 tail = producer;
23271 sprintf (tail, "%s %s", language_string, version_string);
23272 tail += plen;
23274 FOR_EACH_VEC_ELT (switches, j, p)
23276 len = strlen (p);
23277 *tail = ' ';
23278 memcpy (tail + 1, p, len);
23279 tail += len + 1;
23282 *tail = '\0';
23283 return producer;
23286 /* Given a C and/or C++ language/version string return the "highest".
23287 C++ is assumed to be "higher" than C in this case. Used for merging
23288 LTO translation unit languages. */
23289 static const char *
23290 highest_c_language (const char *lang1, const char *lang2)
23292 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23293 return "GNU C++14";
23294 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23295 return "GNU C++11";
23296 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23297 return "GNU C++98";
23299 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23300 return "GNU C11";
23301 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23302 return "GNU C99";
23303 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23304 return "GNU C89";
23306 gcc_unreachable ();
23310 /* Generate the DIE for the compilation unit. */
23312 static dw_die_ref
23313 gen_compile_unit_die (const char *filename)
23315 dw_die_ref die;
23316 const char *language_string = lang_hooks.name;
23317 int language;
23319 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23321 if (filename)
23323 add_name_attribute (die, filename);
23324 /* Don't add cwd for <built-in>. */
23325 if (filename[0] != '<')
23326 add_comp_dir_attribute (die);
23329 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23331 /* If our producer is LTO try to figure out a common language to use
23332 from the global list of translation units. */
23333 if (strcmp (language_string, "GNU GIMPLE") == 0)
23335 unsigned i;
23336 tree t;
23337 const char *common_lang = NULL;
23339 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23341 if (!TRANSLATION_UNIT_LANGUAGE (t))
23342 continue;
23343 if (!common_lang)
23344 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23345 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23347 else if (strncmp (common_lang, "GNU C", 5) == 0
23348 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23349 /* Mixing C and C++ is ok, use C++ in that case. */
23350 common_lang = highest_c_language (common_lang,
23351 TRANSLATION_UNIT_LANGUAGE (t));
23352 else
23354 /* Fall back to C. */
23355 common_lang = NULL;
23356 break;
23360 if (common_lang)
23361 language_string = common_lang;
23364 language = DW_LANG_C;
23365 if (strncmp (language_string, "GNU C", 5) == 0
23366 && ISDIGIT (language_string[5]))
23368 language = DW_LANG_C89;
23369 if (dwarf_version >= 3 || !dwarf_strict)
23371 if (strcmp (language_string, "GNU C89") != 0)
23372 language = DW_LANG_C99;
23374 if (dwarf_version >= 5 /* || !dwarf_strict */)
23375 if (strcmp (language_string, "GNU C11") == 0)
23376 language = DW_LANG_C11;
23379 else if (strncmp (language_string, "GNU C++", 7) == 0)
23381 language = DW_LANG_C_plus_plus;
23382 if (dwarf_version >= 5 /* || !dwarf_strict */)
23384 if (strcmp (language_string, "GNU C++11") == 0)
23385 language = DW_LANG_C_plus_plus_11;
23386 else if (strcmp (language_string, "GNU C++14") == 0)
23387 language = DW_LANG_C_plus_plus_14;
23390 else if (strcmp (language_string, "GNU F77") == 0)
23391 language = DW_LANG_Fortran77;
23392 else if (strcmp (language_string, "GNU Pascal") == 0)
23393 language = DW_LANG_Pascal83;
23394 else if (dwarf_version >= 3 || !dwarf_strict)
23396 if (strcmp (language_string, "GNU Ada") == 0)
23397 language = DW_LANG_Ada95;
23398 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23400 language = DW_LANG_Fortran95;
23401 if (dwarf_version >= 5 /* || !dwarf_strict */)
23403 if (strcmp (language_string, "GNU Fortran2003") == 0)
23404 language = DW_LANG_Fortran03;
23405 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23406 language = DW_LANG_Fortran08;
23409 else if (strcmp (language_string, "GNU Java") == 0)
23410 language = DW_LANG_Java;
23411 else if (strcmp (language_string, "GNU Objective-C") == 0)
23412 language = DW_LANG_ObjC;
23413 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23414 language = DW_LANG_ObjC_plus_plus;
23415 else if (dwarf_version >= 5 || !dwarf_strict)
23417 if (strcmp (language_string, "GNU Go") == 0)
23418 language = DW_LANG_Go;
23421 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23422 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23423 language = DW_LANG_Fortran90;
23425 add_AT_unsigned (die, DW_AT_language, language);
23427 switch (language)
23429 case DW_LANG_Fortran77:
23430 case DW_LANG_Fortran90:
23431 case DW_LANG_Fortran95:
23432 case DW_LANG_Fortran03:
23433 case DW_LANG_Fortran08:
23434 /* Fortran has case insensitive identifiers and the front-end
23435 lowercases everything. */
23436 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23437 break;
23438 default:
23439 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23440 break;
23442 return die;
23445 /* Generate the DIE for a base class. */
23447 static void
23448 gen_inheritance_die (tree binfo, tree access, tree type,
23449 dw_die_ref context_die)
23451 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23452 struct vlr_context ctx = { type, NULL };
23454 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23455 context_die);
23456 add_data_member_location_attribute (die, binfo, &ctx);
23458 if (BINFO_VIRTUAL_P (binfo))
23459 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23461 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23462 children, otherwise the default is DW_ACCESS_public. In DWARF2
23463 the default has always been DW_ACCESS_private. */
23464 if (access == access_public_node)
23466 if (dwarf_version == 2
23467 || context_die->die_tag == DW_TAG_class_type)
23468 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23470 else if (access == access_protected_node)
23471 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23472 else if (dwarf_version > 2
23473 && context_die->die_tag != DW_TAG_class_type)
23474 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23477 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23478 structure. */
23479 static bool
23480 is_variant_part (tree decl)
23482 return (TREE_CODE (decl) == FIELD_DECL
23483 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23486 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23487 return the FIELD_DECL. Return NULL_TREE otherwise. */
23489 static tree
23490 analyze_discr_in_predicate (tree operand, tree struct_type)
23492 bool continue_stripping = true;
23493 while (continue_stripping)
23494 switch (TREE_CODE (operand))
23496 CASE_CONVERT:
23497 operand = TREE_OPERAND (operand, 0);
23498 break;
23499 default:
23500 continue_stripping = false;
23501 break;
23504 /* Match field access to members of struct_type only. */
23505 if (TREE_CODE (operand) == COMPONENT_REF
23506 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23507 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23508 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23509 return TREE_OPERAND (operand, 1);
23510 else
23511 return NULL_TREE;
23514 /* Check that SRC is a constant integer that can be represented as a native
23515 integer constant (either signed or unsigned). If so, store it into DEST and
23516 return true. Return false otherwise. */
23518 static bool
23519 get_discr_value (tree src, dw_discr_value *dest)
23521 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23523 if (TREE_CODE (src) != INTEGER_CST
23524 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23525 return false;
23527 dest->pos = is_unsigned;
23528 if (is_unsigned)
23529 dest->v.uval = tree_to_uhwi (src);
23530 else
23531 dest->v.sval = tree_to_shwi (src);
23533 return true;
23536 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23537 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23538 store NULL_TREE in DISCR_DECL. Otherwise:
23540 - store the discriminant field in STRUCT_TYPE that controls the variant
23541 part to *DISCR_DECL
23543 - put in *DISCR_LISTS_P an array where for each variant, the item
23544 represents the corresponding matching list of discriminant values.
23546 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23547 the above array.
23549 Note that when the array is allocated (i.e. when the analysis is
23550 successful), it is up to the caller to free the array. */
23552 static void
23553 analyze_variants_discr (tree variant_part_decl,
23554 tree struct_type,
23555 tree *discr_decl,
23556 dw_discr_list_ref **discr_lists_p,
23557 unsigned *discr_lists_length)
23559 tree variant_part_type = TREE_TYPE (variant_part_decl);
23560 tree variant;
23561 dw_discr_list_ref *discr_lists;
23562 unsigned i;
23564 /* Compute how many variants there are in this variant part. */
23565 *discr_lists_length = 0;
23566 for (variant = TYPE_FIELDS (variant_part_type);
23567 variant != NULL_TREE;
23568 variant = DECL_CHAIN (variant))
23569 ++*discr_lists_length;
23571 *discr_decl = NULL_TREE;
23572 *discr_lists_p
23573 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23574 sizeof (**discr_lists_p));
23575 discr_lists = *discr_lists_p;
23577 /* And then analyze all variants to extract discriminant information for all
23578 of them. This analysis is conservative: as soon as we detect something we
23579 do not support, abort everything and pretend we found nothing. */
23580 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23581 variant != NULL_TREE;
23582 variant = DECL_CHAIN (variant), ++i)
23584 tree match_expr = DECL_QUALIFIER (variant);
23586 /* Now, try to analyze the predicate and deduce a discriminant for
23587 it. */
23588 if (match_expr == boolean_true_node)
23589 /* Typically happens for the default variant: it matches all cases that
23590 previous variants rejected. Don't output any matching value for
23591 this one. */
23592 continue;
23594 /* The following loop tries to iterate over each discriminant
23595 possibility: single values or ranges. */
23596 while (match_expr != NULL_TREE)
23598 tree next_round_match_expr;
23599 tree candidate_discr = NULL_TREE;
23600 dw_discr_list_ref new_node = NULL;
23602 /* Possibilities are matched one after the other by nested
23603 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23604 continue with the rest at next iteration. */
23605 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23607 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23608 match_expr = TREE_OPERAND (match_expr, 1);
23610 else
23611 next_round_match_expr = NULL_TREE;
23613 if (match_expr == boolean_false_node)
23614 /* This sub-expression matches nothing: just wait for the next
23615 one. */
23618 else if (TREE_CODE (match_expr) == EQ_EXPR)
23620 /* We are matching: <discr_field> == <integer_cst>
23621 This sub-expression matches a single value. */
23622 tree integer_cst = TREE_OPERAND (match_expr, 1);
23624 candidate_discr
23625 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23626 struct_type);
23628 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23629 if (!get_discr_value (integer_cst,
23630 &new_node->dw_discr_lower_bound))
23631 goto abort;
23632 new_node->dw_discr_range = false;
23635 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23637 /* We are matching:
23638 <discr_field> > <integer_cst>
23639 && <discr_field> < <integer_cst>.
23640 This sub-expression matches the range of values between the
23641 two matched integer constants. Note that comparisons can be
23642 inclusive or exclusive. */
23643 tree candidate_discr_1, candidate_discr_2;
23644 tree lower_cst, upper_cst;
23645 bool lower_cst_included, upper_cst_included;
23646 tree lower_op = TREE_OPERAND (match_expr, 0);
23647 tree upper_op = TREE_OPERAND (match_expr, 1);
23649 /* When the comparison is exclusive, the integer constant is not
23650 the discriminant range bound we are looking for: we will have
23651 to increment or decrement it. */
23652 if (TREE_CODE (lower_op) == GE_EXPR)
23653 lower_cst_included = true;
23654 else if (TREE_CODE (lower_op) == GT_EXPR)
23655 lower_cst_included = false;
23656 else
23657 goto abort;
23659 if (TREE_CODE (upper_op) == LE_EXPR)
23660 upper_cst_included = true;
23661 else if (TREE_CODE (upper_op) == LT_EXPR)
23662 upper_cst_included = false;
23663 else
23664 goto abort;
23666 /* Extract the discriminant from the first operand and check it
23667 is consistant with the same analysis in the second
23668 operand. */
23669 candidate_discr_1
23670 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23671 struct_type);
23672 candidate_discr_2
23673 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23674 struct_type);
23675 if (candidate_discr_1 == candidate_discr_2)
23676 candidate_discr = candidate_discr_1;
23677 else
23678 goto abort;
23680 /* Extract bounds from both. */
23681 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23682 lower_cst = TREE_OPERAND (lower_op, 1);
23683 upper_cst = TREE_OPERAND (upper_op, 1);
23685 if (!lower_cst_included)
23686 lower_cst
23687 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
23688 lower_cst,
23689 build_int_cst (TREE_TYPE (lower_cst), 1)));
23690 if (!upper_cst_included)
23691 upper_cst
23692 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
23693 upper_cst,
23694 build_int_cst (TREE_TYPE (upper_cst), 1)));
23696 if (!get_discr_value (lower_cst,
23697 &new_node->dw_discr_lower_bound)
23698 || !get_discr_value (upper_cst,
23699 &new_node->dw_discr_upper_bound))
23700 goto abort;
23702 new_node->dw_discr_range = true;
23705 else
23706 /* Unsupported sub-expression: we cannot determine the set of
23707 matching discriminant values. Abort everything. */
23708 goto abort;
23710 /* If the discriminant info is not consistant with what we saw so
23711 far, consider the analysis failed and abort everything. */
23712 if (candidate_discr == NULL_TREE
23713 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23714 goto abort;
23715 else
23716 *discr_decl = candidate_discr;
23718 if (new_node != NULL)
23720 new_node->dw_discr_next = discr_lists[i];
23721 discr_lists[i] = new_node;
23723 match_expr = next_round_match_expr;
23727 /* If we reach this point, we could match everything we were interested
23728 in. */
23729 return;
23731 abort:
23732 /* Clean all data structure and return no result. */
23733 free (*discr_lists_p);
23734 *discr_lists_p = NULL;
23735 *discr_decl = NULL_TREE;
23738 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23739 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23740 under CONTEXT_DIE.
23742 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23743 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23744 this type, which are record types, represent the available variants and each
23745 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23746 values are inferred from these attributes.
23748 In trees, the offsets for the fields inside these sub-records are relative
23749 to the variant part itself, whereas the corresponding DIEs should have
23750 offset attributes that are relative to the embedding record base address.
23751 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23752 must be an expression that computes the offset of the variant part to
23753 describe in DWARF. */
23755 static void
23756 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23757 dw_die_ref context_die)
23759 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23760 tree variant_part_offset = vlr_ctx->variant_part_offset;
23761 struct loc_descr_context ctx = {
23762 vlr_ctx->struct_type, /* context_type */
23763 NULL_TREE, /* base_decl */
23764 NULL, /* dpi */
23765 false, /* placeholder_arg */
23766 false /* placeholder_seen */
23769 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23770 NULL_TREE if there is no such field. */
23771 tree discr_decl = NULL_TREE;
23772 dw_discr_list_ref *discr_lists;
23773 unsigned discr_lists_length = 0;
23774 unsigned i;
23776 dw_die_ref dwarf_proc_die = NULL;
23777 dw_die_ref variant_part_die
23778 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23780 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23782 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23783 &discr_decl, &discr_lists, &discr_lists_length);
23785 if (discr_decl != NULL_TREE)
23787 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23789 if (discr_die)
23790 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23791 else
23792 /* We have no DIE for the discriminant, so just discard all
23793 discrimimant information in the output. */
23794 discr_decl = NULL_TREE;
23797 /* If the offset for this variant part is more complex than a constant,
23798 create a DWARF procedure for it so that we will not have to generate DWARF
23799 expressions for it for each member. */
23800 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23801 && (dwarf_version >= 3 || !dwarf_strict))
23803 const tree dwarf_proc_fndecl
23804 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23805 build_function_type (TREE_TYPE (variant_part_offset),
23806 NULL_TREE));
23807 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23808 const dw_loc_descr_ref dwarf_proc_body
23809 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23811 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23812 dwarf_proc_fndecl, context_die);
23813 if (dwarf_proc_die != NULL)
23814 variant_part_offset = dwarf_proc_call;
23817 /* Output DIEs for all variants. */
23818 i = 0;
23819 for (tree variant = TYPE_FIELDS (variant_part_type);
23820 variant != NULL_TREE;
23821 variant = DECL_CHAIN (variant), ++i)
23823 tree variant_type = TREE_TYPE (variant);
23824 dw_die_ref variant_die;
23826 /* All variants (i.e. members of a variant part) are supposed to be
23827 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23828 under these records. */
23829 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23831 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23832 equate_decl_number_to_die (variant, variant_die);
23834 /* Output discriminant values this variant matches, if any. */
23835 if (discr_decl == NULL || discr_lists[i] == NULL)
23836 /* In the case we have discriminant information at all, this is
23837 probably the default variant: as the standard says, don't
23838 output any discriminant value/list attribute. */
23840 else if (discr_lists[i]->dw_discr_next == NULL
23841 && !discr_lists[i]->dw_discr_range)
23842 /* If there is only one accepted value, don't bother outputting a
23843 list. */
23844 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23845 else
23846 add_discr_list (variant_die, discr_lists[i]);
23848 for (tree member = TYPE_FIELDS (variant_type);
23849 member != NULL_TREE;
23850 member = DECL_CHAIN (member))
23852 struct vlr_context vlr_sub_ctx = {
23853 vlr_ctx->struct_type, /* struct_type */
23854 NULL /* variant_part_offset */
23856 if (is_variant_part (member))
23858 /* All offsets for fields inside variant parts are relative to
23859 the top-level embedding RECORD_TYPE's base address. On the
23860 other hand, offsets in GCC's types are relative to the
23861 nested-most variant part. So we have to sum offsets each time
23862 we recurse. */
23864 vlr_sub_ctx.variant_part_offset
23865 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
23866 variant_part_offset, byte_position (member)));
23867 gen_variant_part (member, &vlr_sub_ctx, variant_die);
23869 else
23871 vlr_sub_ctx.variant_part_offset = variant_part_offset;
23872 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
23877 free (discr_lists);
23880 /* Generate a DIE for a class member. */
23882 static void
23883 gen_member_die (tree type, dw_die_ref context_die)
23885 tree member;
23886 tree binfo = TYPE_BINFO (type);
23887 dw_die_ref child;
23889 /* If this is not an incomplete type, output descriptions of each of its
23890 members. Note that as we output the DIEs necessary to represent the
23891 members of this record or union type, we will also be trying to output
23892 DIEs to represent the *types* of those members. However the `type'
23893 function (above) will specifically avoid generating type DIEs for member
23894 types *within* the list of member DIEs for this (containing) type except
23895 for those types (of members) which are explicitly marked as also being
23896 members of this (containing) type themselves. The g++ front- end can
23897 force any given type to be treated as a member of some other (containing)
23898 type by setting the TYPE_CONTEXT of the given (member) type to point to
23899 the TREE node representing the appropriate (containing) type. */
23901 /* First output info about the base classes. */
23902 if (binfo)
23904 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
23905 int i;
23906 tree base;
23908 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
23909 gen_inheritance_die (base,
23910 (accesses ? (*accesses)[i] : access_public_node),
23911 type,
23912 context_die);
23915 /* Now output info about the data members and type members. */
23916 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
23918 struct vlr_context vlr_ctx = { type, NULL_TREE };
23920 /* If we thought we were generating minimal debug info for TYPE
23921 and then changed our minds, some of the member declarations
23922 may have already been defined. Don't define them again, but
23923 do put them in the right order. */
23925 child = lookup_decl_die (member);
23926 if (child)
23928 /* Handle inline static data members, which only have in-class
23929 declarations. */
23930 if (child->die_tag == DW_TAG_variable
23931 && child->die_parent == comp_unit_die ()
23932 && get_AT (child, DW_AT_specification) == NULL)
23934 reparent_child (child, context_die);
23935 child->die_tag = DW_TAG_member;
23937 else
23938 splice_child_die (context_die, child);
23941 /* Do not generate standard DWARF for variant parts if we are generating
23942 the corresponding GNAT encodings: DIEs generated for both would
23943 conflict in our mappings. */
23944 else if (is_variant_part (member)
23945 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
23947 vlr_ctx.variant_part_offset = byte_position (member);
23948 gen_variant_part (member, &vlr_ctx, context_die);
23950 else
23952 vlr_ctx.variant_part_offset = NULL_TREE;
23953 gen_decl_die (member, NULL, &vlr_ctx, context_die);
23956 /* For C++ inline static data members emit immediately a DW_TAG_variable
23957 DIE that will refer to that DW_TAG_member through
23958 DW_AT_specification. */
23959 if (TREE_STATIC (member)
23960 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
23961 != -1))
23963 int old_extern = DECL_EXTERNAL (member);
23964 DECL_EXTERNAL (member) = 0;
23965 gen_decl_die (member, NULL, NULL, comp_unit_die ());
23966 DECL_EXTERNAL (member) = old_extern;
23970 /* We do not keep type methods in type variants. */
23971 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
23972 /* Now output info about the function members (if any). */
23973 if (TYPE_METHODS (type) != error_mark_node)
23974 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
23976 /* Don't include clones in the member list. */
23977 if (DECL_ABSTRACT_ORIGIN (member))
23978 continue;
23979 /* Nor constructors for anonymous classes. */
23980 if (DECL_ARTIFICIAL (member)
23981 && dwarf2_name (member, 0) == NULL)
23982 continue;
23984 child = lookup_decl_die (member);
23985 if (child)
23986 splice_child_die (context_die, child);
23987 else
23988 gen_decl_die (member, NULL, NULL, context_die);
23992 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
23993 is set, we pretend that the type was never defined, so we only get the
23994 member DIEs needed by later specification DIEs. */
23996 static void
23997 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
23998 enum debug_info_usage usage)
24000 if (TREE_ASM_WRITTEN (type))
24002 /* Fill in the bound of variable-length fields in late dwarf if
24003 still incomplete. */
24004 if (!early_dwarf && variably_modified_type_p (type, NULL))
24005 for (tree member = TYPE_FIELDS (type);
24006 member;
24007 member = DECL_CHAIN (member))
24008 fill_variable_array_bounds (TREE_TYPE (member));
24009 return;
24012 dw_die_ref type_die = lookup_type_die (type);
24013 dw_die_ref scope_die = 0;
24014 int nested = 0;
24015 int complete = (TYPE_SIZE (type)
24016 && (! TYPE_STUB_DECL (type)
24017 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24018 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24019 complete = complete && should_emit_struct_debug (type, usage);
24021 if (type_die && ! complete)
24022 return;
24024 if (TYPE_CONTEXT (type) != NULL_TREE
24025 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24026 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24027 nested = 1;
24029 scope_die = scope_die_for (type, context_die);
24031 /* Generate child dies for template paramaters. */
24032 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24033 schedule_generic_params_dies_gen (type);
24035 if (! type_die || (nested && is_cu_die (scope_die)))
24036 /* First occurrence of type or toplevel definition of nested class. */
24038 dw_die_ref old_die = type_die;
24040 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24041 ? record_type_tag (type) : DW_TAG_union_type,
24042 scope_die, type);
24043 equate_type_number_to_die (type, type_die);
24044 if (old_die)
24045 add_AT_specification (type_die, old_die);
24046 else
24047 add_name_attribute (type_die, type_tag (type));
24049 else
24050 remove_AT (type_die, DW_AT_declaration);
24052 /* If this type has been completed, then give it a byte_size attribute and
24053 then give a list of members. */
24054 if (complete && !ns_decl)
24056 /* Prevent infinite recursion in cases where the type of some member of
24057 this type is expressed in terms of this type itself. */
24058 TREE_ASM_WRITTEN (type) = 1;
24059 add_byte_size_attribute (type_die, type);
24060 if (TYPE_STUB_DECL (type) != NULL_TREE)
24062 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24063 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24066 /* If the first reference to this type was as the return type of an
24067 inline function, then it may not have a parent. Fix this now. */
24068 if (type_die->die_parent == NULL)
24069 add_child_die (scope_die, type_die);
24071 push_decl_scope (type);
24072 gen_member_die (type, type_die);
24073 pop_decl_scope ();
24075 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24076 if (TYPE_ARTIFICIAL (type))
24077 add_AT_flag (type_die, DW_AT_artificial, 1);
24079 /* GNU extension: Record what type our vtable lives in. */
24080 if (TYPE_VFIELD (type))
24082 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24084 gen_type_die (vtype, context_die);
24085 add_AT_die_ref (type_die, DW_AT_containing_type,
24086 lookup_type_die (vtype));
24089 else
24091 add_AT_flag (type_die, DW_AT_declaration, 1);
24093 /* We don't need to do this for function-local types. */
24094 if (TYPE_STUB_DECL (type)
24095 && ! decl_function_context (TYPE_STUB_DECL (type)))
24096 vec_safe_push (incomplete_types, type);
24099 if (get_AT (type_die, DW_AT_name))
24100 add_pubtype (type, type_die);
24103 /* Generate a DIE for a subroutine _type_. */
24105 static void
24106 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24108 tree return_type = TREE_TYPE (type);
24109 dw_die_ref subr_die
24110 = new_die (DW_TAG_subroutine_type,
24111 scope_die_for (type, context_die), type);
24113 equate_type_number_to_die (type, subr_die);
24114 add_prototyped_attribute (subr_die, type);
24115 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24116 context_die);
24117 gen_formal_types_die (type, subr_die);
24119 if (get_AT (subr_die, DW_AT_name))
24120 add_pubtype (type, subr_die);
24121 if ((dwarf_version >= 5 || !dwarf_strict)
24122 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24123 add_AT_flag (subr_die, DW_AT_reference, 1);
24124 if ((dwarf_version >= 5 || !dwarf_strict)
24125 && lang_hooks.types.type_dwarf_attribute (type,
24126 DW_AT_rvalue_reference) != -1)
24127 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24130 /* Generate a DIE for a type definition. */
24132 static void
24133 gen_typedef_die (tree decl, dw_die_ref context_die)
24135 dw_die_ref type_die;
24136 tree origin;
24138 if (TREE_ASM_WRITTEN (decl))
24140 if (DECL_ORIGINAL_TYPE (decl))
24141 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24142 return;
24145 TREE_ASM_WRITTEN (decl) = 1;
24146 type_die = new_die (DW_TAG_typedef, context_die, decl);
24147 origin = decl_ultimate_origin (decl);
24148 if (origin != NULL)
24149 add_abstract_origin_attribute (type_die, origin);
24150 else
24152 tree type;
24154 add_name_and_src_coords_attributes (type_die, decl);
24155 if (DECL_ORIGINAL_TYPE (decl))
24157 type = DECL_ORIGINAL_TYPE (decl);
24159 if (type == error_mark_node)
24160 return;
24162 gcc_assert (type != TREE_TYPE (decl));
24163 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24165 else
24167 type = TREE_TYPE (decl);
24169 if (type == error_mark_node)
24170 return;
24172 if (is_naming_typedef_decl (TYPE_NAME (type)))
24174 /* Here, we are in the case of decl being a typedef naming
24175 an anonymous type, e.g:
24176 typedef struct {...} foo;
24177 In that case TREE_TYPE (decl) is not a typedef variant
24178 type and TYPE_NAME of the anonymous type is set to the
24179 TYPE_DECL of the typedef. This construct is emitted by
24180 the C++ FE.
24182 TYPE is the anonymous struct named by the typedef
24183 DECL. As we need the DW_AT_type attribute of the
24184 DW_TAG_typedef to point to the DIE of TYPE, let's
24185 generate that DIE right away. add_type_attribute
24186 called below will then pick (via lookup_type_die) that
24187 anonymous struct DIE. */
24188 if (!TREE_ASM_WRITTEN (type))
24189 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24191 /* This is a GNU Extension. We are adding a
24192 DW_AT_linkage_name attribute to the DIE of the
24193 anonymous struct TYPE. The value of that attribute
24194 is the name of the typedef decl naming the anonymous
24195 struct. This greatly eases the work of consumers of
24196 this debug info. */
24197 add_linkage_name_raw (lookup_type_die (type), decl);
24201 add_type_attribute (type_die, type, decl_quals (decl), false,
24202 context_die);
24204 if (is_naming_typedef_decl (decl))
24205 /* We want that all subsequent calls to lookup_type_die with
24206 TYPE in argument yield the DW_TAG_typedef we have just
24207 created. */
24208 equate_type_number_to_die (type, type_die);
24210 add_accessibility_attribute (type_die, decl);
24213 if (DECL_ABSTRACT_P (decl))
24214 equate_decl_number_to_die (decl, type_die);
24216 if (get_AT (type_die, DW_AT_name))
24217 add_pubtype (decl, type_die);
24220 /* Generate a DIE for a struct, class, enum or union type. */
24222 static void
24223 gen_tagged_type_die (tree type,
24224 dw_die_ref context_die,
24225 enum debug_info_usage usage)
24227 int need_pop;
24229 if (type == NULL_TREE
24230 || !is_tagged_type (type))
24231 return;
24233 if (TREE_ASM_WRITTEN (type))
24234 need_pop = 0;
24235 /* If this is a nested type whose containing class hasn't been written
24236 out yet, writing it out will cover this one, too. This does not apply
24237 to instantiations of member class templates; they need to be added to
24238 the containing class as they are generated. FIXME: This hurts the
24239 idea of combining type decls from multiple TUs, since we can't predict
24240 what set of template instantiations we'll get. */
24241 else if (TYPE_CONTEXT (type)
24242 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24243 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24245 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24247 if (TREE_ASM_WRITTEN (type))
24248 return;
24250 /* If that failed, attach ourselves to the stub. */
24251 push_decl_scope (TYPE_CONTEXT (type));
24252 context_die = lookup_type_die (TYPE_CONTEXT (type));
24253 need_pop = 1;
24255 else if (TYPE_CONTEXT (type) != NULL_TREE
24256 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24258 /* If this type is local to a function that hasn't been written
24259 out yet, use a NULL context for now; it will be fixed up in
24260 decls_for_scope. */
24261 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24262 /* A declaration DIE doesn't count; nested types need to go in the
24263 specification. */
24264 if (context_die && is_declaration_die (context_die))
24265 context_die = NULL;
24266 need_pop = 0;
24268 else
24270 context_die = declare_in_namespace (type, context_die);
24271 need_pop = 0;
24274 if (TREE_CODE (type) == ENUMERAL_TYPE)
24276 /* This might have been written out by the call to
24277 declare_in_namespace. */
24278 if (!TREE_ASM_WRITTEN (type))
24279 gen_enumeration_type_die (type, context_die);
24281 else
24282 gen_struct_or_union_type_die (type, context_die, usage);
24284 if (need_pop)
24285 pop_decl_scope ();
24287 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24288 it up if it is ever completed. gen_*_type_die will set it for us
24289 when appropriate. */
24292 /* Generate a type description DIE. */
24294 static void
24295 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24296 enum debug_info_usage usage)
24298 struct array_descr_info info;
24300 if (type == NULL_TREE || type == error_mark_node)
24301 return;
24303 if (flag_checking && type)
24304 verify_type (type);
24306 if (TYPE_NAME (type) != NULL_TREE
24307 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24308 && is_redundant_typedef (TYPE_NAME (type))
24309 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24310 /* The DECL of this type is a typedef we don't want to emit debug
24311 info for but we want debug info for its underlying typedef.
24312 This can happen for e.g, the injected-class-name of a C++
24313 type. */
24314 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24316 /* If TYPE is a typedef type variant, let's generate debug info
24317 for the parent typedef which TYPE is a type of. */
24318 if (typedef_variant_p (type))
24320 if (TREE_ASM_WRITTEN (type))
24321 return;
24323 /* Prevent broken recursion; we can't hand off to the same type. */
24324 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24326 /* Give typedefs the right scope. */
24327 context_die = scope_die_for (type, context_die);
24329 TREE_ASM_WRITTEN (type) = 1;
24331 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24332 return;
24335 /* If type is an anonymous tagged type named by a typedef, let's
24336 generate debug info for the typedef. */
24337 if (is_naming_typedef_decl (TYPE_NAME (type)))
24339 /* Use the DIE of the containing namespace as the parent DIE of
24340 the type description DIE we want to generate. */
24341 if (DECL_CONTEXT (TYPE_NAME (type))
24342 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24343 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24345 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24346 return;
24349 if (lang_hooks.types.get_debug_type)
24351 tree debug_type = lang_hooks.types.get_debug_type (type);
24353 if (debug_type != NULL_TREE && debug_type != type)
24355 gen_type_die_with_usage (debug_type, context_die, usage);
24356 return;
24360 /* We are going to output a DIE to represent the unqualified version
24361 of this type (i.e. without any const or volatile qualifiers) so
24362 get the main variant (i.e. the unqualified version) of this type
24363 now. (Vectors and arrays are special because the debugging info is in the
24364 cloned type itself. Similarly function/method types can contain extra
24365 ref-qualification). */
24366 if (TREE_CODE (type) == FUNCTION_TYPE
24367 || TREE_CODE (type) == METHOD_TYPE)
24369 /* For function/method types, can't use type_main_variant here,
24370 because that can have different ref-qualifiers for C++,
24371 but try to canonicalize. */
24372 tree main = TYPE_MAIN_VARIANT (type);
24373 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24374 if (check_base_type (t, main) && check_lang_type (t, type))
24375 type = t;
24377 else if (TREE_CODE (type) != VECTOR_TYPE
24378 && TREE_CODE (type) != ARRAY_TYPE)
24379 type = type_main_variant (type);
24381 /* If this is an array type with hidden descriptor, handle it first. */
24382 if (!TREE_ASM_WRITTEN (type)
24383 && lang_hooks.types.get_array_descr_info)
24385 memset (&info, 0, sizeof (info));
24386 if (lang_hooks.types.get_array_descr_info (type, &info))
24388 /* Fortran sometimes emits array types with no dimension. */
24389 gcc_assert (info.ndimensions >= 0
24390 && (info.ndimensions
24391 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24392 gen_descr_array_type_die (type, &info, context_die);
24393 TREE_ASM_WRITTEN (type) = 1;
24394 return;
24398 if (TREE_ASM_WRITTEN (type))
24400 /* Variable-length types may be incomplete even if
24401 TREE_ASM_WRITTEN. For such types, fall through to
24402 gen_array_type_die() and possibly fill in
24403 DW_AT_{upper,lower}_bound attributes. */
24404 if ((TREE_CODE (type) != ARRAY_TYPE
24405 && TREE_CODE (type) != RECORD_TYPE
24406 && TREE_CODE (type) != UNION_TYPE
24407 && TREE_CODE (type) != QUAL_UNION_TYPE)
24408 || !variably_modified_type_p (type, NULL))
24409 return;
24412 switch (TREE_CODE (type))
24414 case ERROR_MARK:
24415 break;
24417 case POINTER_TYPE:
24418 case REFERENCE_TYPE:
24419 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24420 ensures that the gen_type_die recursion will terminate even if the
24421 type is recursive. Recursive types are possible in Ada. */
24422 /* ??? We could perhaps do this for all types before the switch
24423 statement. */
24424 TREE_ASM_WRITTEN (type) = 1;
24426 /* For these types, all that is required is that we output a DIE (or a
24427 set of DIEs) to represent the "basis" type. */
24428 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24429 DINFO_USAGE_IND_USE);
24430 break;
24432 case OFFSET_TYPE:
24433 /* This code is used for C++ pointer-to-data-member types.
24434 Output a description of the relevant class type. */
24435 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24436 DINFO_USAGE_IND_USE);
24438 /* Output a description of the type of the object pointed to. */
24439 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24440 DINFO_USAGE_IND_USE);
24442 /* Now output a DIE to represent this pointer-to-data-member type
24443 itself. */
24444 gen_ptr_to_mbr_type_die (type, context_die);
24445 break;
24447 case FUNCTION_TYPE:
24448 /* Force out return type (in case it wasn't forced out already). */
24449 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24450 DINFO_USAGE_DIR_USE);
24451 gen_subroutine_type_die (type, context_die);
24452 break;
24454 case METHOD_TYPE:
24455 /* Force out return type (in case it wasn't forced out already). */
24456 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24457 DINFO_USAGE_DIR_USE);
24458 gen_subroutine_type_die (type, context_die);
24459 break;
24461 case ARRAY_TYPE:
24462 case VECTOR_TYPE:
24463 gen_array_type_die (type, context_die);
24464 break;
24466 case ENUMERAL_TYPE:
24467 case RECORD_TYPE:
24468 case UNION_TYPE:
24469 case QUAL_UNION_TYPE:
24470 gen_tagged_type_die (type, context_die, usage);
24471 return;
24473 case VOID_TYPE:
24474 case INTEGER_TYPE:
24475 case REAL_TYPE:
24476 case FIXED_POINT_TYPE:
24477 case COMPLEX_TYPE:
24478 case BOOLEAN_TYPE:
24479 case POINTER_BOUNDS_TYPE:
24480 /* No DIEs needed for fundamental types. */
24481 break;
24483 case NULLPTR_TYPE:
24484 case LANG_TYPE:
24485 /* Just use DW_TAG_unspecified_type. */
24487 dw_die_ref type_die = lookup_type_die (type);
24488 if (type_die == NULL)
24490 tree name = TYPE_IDENTIFIER (type);
24491 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24492 type);
24493 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24494 equate_type_number_to_die (type, type_die);
24497 break;
24499 default:
24500 if (is_cxx_auto (type))
24502 tree name = TYPE_IDENTIFIER (type);
24503 dw_die_ref *die = (name == get_identifier ("auto")
24504 ? &auto_die : &decltype_auto_die);
24505 if (!*die)
24507 *die = new_die (DW_TAG_unspecified_type,
24508 comp_unit_die (), NULL_TREE);
24509 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24511 equate_type_number_to_die (type, *die);
24512 break;
24514 gcc_unreachable ();
24517 TREE_ASM_WRITTEN (type) = 1;
24520 static void
24521 gen_type_die (tree type, dw_die_ref context_die)
24523 if (type != error_mark_node)
24525 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24526 if (flag_checking)
24528 dw_die_ref die = lookup_type_die (type);
24529 if (die)
24530 check_die (die);
24535 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24536 things which are local to the given block. */
24538 static void
24539 gen_block_die (tree stmt, dw_die_ref context_die)
24541 int must_output_die = 0;
24542 bool inlined_func;
24544 /* Ignore blocks that are NULL. */
24545 if (stmt == NULL_TREE)
24546 return;
24548 inlined_func = inlined_function_outer_scope_p (stmt);
24550 /* If the block is one fragment of a non-contiguous block, do not
24551 process the variables, since they will have been done by the
24552 origin block. Do process subblocks. */
24553 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24555 tree sub;
24557 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24558 gen_block_die (sub, context_die);
24560 return;
24563 /* Determine if we need to output any Dwarf DIEs at all to represent this
24564 block. */
24565 if (inlined_func)
24566 /* The outer scopes for inlinings *must* always be represented. We
24567 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24568 must_output_die = 1;
24569 else
24571 /* Determine if this block directly contains any "significant"
24572 local declarations which we will need to output DIEs for. */
24573 if (debug_info_level > DINFO_LEVEL_TERSE)
24574 /* We are not in terse mode so *any* local declaration counts
24575 as being a "significant" one. */
24576 must_output_die = ((BLOCK_VARS (stmt) != NULL
24577 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24578 && (TREE_USED (stmt)
24579 || TREE_ASM_WRITTEN (stmt)
24580 || BLOCK_ABSTRACT (stmt)));
24581 else if ((TREE_USED (stmt)
24582 || TREE_ASM_WRITTEN (stmt)
24583 || BLOCK_ABSTRACT (stmt))
24584 && !dwarf2out_ignore_block (stmt))
24585 must_output_die = 1;
24588 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24589 DIE for any block which contains no significant local declarations at
24590 all. Rather, in such cases we just call `decls_for_scope' so that any
24591 needed Dwarf info for any sub-blocks will get properly generated. Note
24592 that in terse mode, our definition of what constitutes a "significant"
24593 local declaration gets restricted to include only inlined function
24594 instances and local (nested) function definitions. */
24595 if (must_output_die)
24597 if (inlined_func)
24599 /* If STMT block is abstract, that means we have been called
24600 indirectly from dwarf2out_abstract_function.
24601 That function rightfully marks the descendent blocks (of
24602 the abstract function it is dealing with) as being abstract,
24603 precisely to prevent us from emitting any
24604 DW_TAG_inlined_subroutine DIE as a descendent
24605 of an abstract function instance. So in that case, we should
24606 not call gen_inlined_subroutine_die.
24608 Later though, when cgraph asks dwarf2out to emit info
24609 for the concrete instance of the function decl into which
24610 the concrete instance of STMT got inlined, the later will lead
24611 to the generation of a DW_TAG_inlined_subroutine DIE. */
24612 if (! BLOCK_ABSTRACT (stmt))
24613 gen_inlined_subroutine_die (stmt, context_die);
24615 else
24616 gen_lexical_block_die (stmt, context_die);
24618 else
24619 decls_for_scope (stmt, context_die);
24622 /* Process variable DECL (or variable with origin ORIGIN) within
24623 block STMT and add it to CONTEXT_DIE. */
24624 static void
24625 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24627 dw_die_ref die;
24628 tree decl_or_origin = decl ? decl : origin;
24630 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24631 die = lookup_decl_die (decl_or_origin);
24632 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24634 if (TYPE_DECL_IS_STUB (decl_or_origin))
24635 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24636 else
24637 die = lookup_decl_die (decl_or_origin);
24638 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24639 if (! die && ! early_dwarf)
24640 return;
24642 else
24643 die = NULL;
24645 if (die != NULL && die->die_parent == NULL)
24646 add_child_die (context_die, die);
24647 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24649 if (early_dwarf)
24650 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24651 stmt, context_die);
24653 else
24654 gen_decl_die (decl, origin, NULL, context_die);
24657 /* Generate all of the decls declared within a given scope and (recursively)
24658 all of its sub-blocks. */
24660 static void
24661 decls_for_scope (tree stmt, dw_die_ref context_die)
24663 tree decl;
24664 unsigned int i;
24665 tree subblocks;
24667 /* Ignore NULL blocks. */
24668 if (stmt == NULL_TREE)
24669 return;
24671 /* Output the DIEs to represent all of the data objects and typedefs
24672 declared directly within this block but not within any nested
24673 sub-blocks. Also, nested function and tag DIEs have been
24674 generated with a parent of NULL; fix that up now. We don't
24675 have to do this if we're at -g1. */
24676 if (debug_info_level > DINFO_LEVEL_TERSE)
24678 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24679 process_scope_var (stmt, decl, NULL_TREE, context_die);
24680 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24681 origin - avoid doing this twice as we have no good way to see
24682 if we've done it once already. */
24683 if (! early_dwarf)
24684 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24685 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
24686 context_die);
24689 /* Even if we're at -g1, we need to process the subblocks in order to get
24690 inlined call information. */
24692 /* Output the DIEs to represent all sub-blocks (and the items declared
24693 therein) of this block. */
24694 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24695 subblocks != NULL;
24696 subblocks = BLOCK_CHAIN (subblocks))
24697 gen_block_die (subblocks, context_die);
24700 /* Is this a typedef we can avoid emitting? */
24702 bool
24703 is_redundant_typedef (const_tree decl)
24705 if (TYPE_DECL_IS_STUB (decl))
24706 return true;
24708 if (DECL_ARTIFICIAL (decl)
24709 && DECL_CONTEXT (decl)
24710 && is_tagged_type (DECL_CONTEXT (decl))
24711 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24712 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24713 /* Also ignore the artificial member typedef for the class name. */
24714 return true;
24716 return false;
24719 /* Return TRUE if TYPE is a typedef that names a type for linkage
24720 purposes. This kind of typedefs is produced by the C++ FE for
24721 constructs like:
24723 typedef struct {...} foo;
24725 In that case, there is no typedef variant type produced for foo.
24726 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24727 struct type. */
24729 static bool
24730 is_naming_typedef_decl (const_tree decl)
24732 if (decl == NULL_TREE
24733 || TREE_CODE (decl) != TYPE_DECL
24734 || DECL_NAMELESS (decl)
24735 || !is_tagged_type (TREE_TYPE (decl))
24736 || DECL_IS_BUILTIN (decl)
24737 || is_redundant_typedef (decl)
24738 /* It looks like Ada produces TYPE_DECLs that are very similar
24739 to C++ naming typedefs but that have different
24740 semantics. Let's be specific to c++ for now. */
24741 || !is_cxx ())
24742 return FALSE;
24744 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24745 && TYPE_NAME (TREE_TYPE (decl)) == decl
24746 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24747 != TYPE_NAME (TREE_TYPE (decl))));
24750 /* Looks up the DIE for a context. */
24752 static inline dw_die_ref
24753 lookup_context_die (tree context)
24755 if (context)
24757 /* Find die that represents this context. */
24758 if (TYPE_P (context))
24760 context = TYPE_MAIN_VARIANT (context);
24761 dw_die_ref ctx = lookup_type_die (context);
24762 if (!ctx)
24763 return NULL;
24764 return strip_naming_typedef (context, ctx);
24766 else
24767 return lookup_decl_die (context);
24769 return comp_unit_die ();
24772 /* Returns the DIE for a context. */
24774 static inline dw_die_ref
24775 get_context_die (tree context)
24777 if (context)
24779 /* Find die that represents this context. */
24780 if (TYPE_P (context))
24782 context = TYPE_MAIN_VARIANT (context);
24783 return strip_naming_typedef (context, force_type_die (context));
24785 else
24786 return force_decl_die (context);
24788 return comp_unit_die ();
24791 /* Returns the DIE for decl. A DIE will always be returned. */
24793 static dw_die_ref
24794 force_decl_die (tree decl)
24796 dw_die_ref decl_die;
24797 unsigned saved_external_flag;
24798 tree save_fn = NULL_TREE;
24799 decl_die = lookup_decl_die (decl);
24800 if (!decl_die)
24802 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24804 decl_die = lookup_decl_die (decl);
24805 if (decl_die)
24806 return decl_die;
24808 switch (TREE_CODE (decl))
24810 case FUNCTION_DECL:
24811 /* Clear current_function_decl, so that gen_subprogram_die thinks
24812 that this is a declaration. At this point, we just want to force
24813 declaration die. */
24814 save_fn = current_function_decl;
24815 current_function_decl = NULL_TREE;
24816 gen_subprogram_die (decl, context_die);
24817 current_function_decl = save_fn;
24818 break;
24820 case VAR_DECL:
24821 /* Set external flag to force declaration die. Restore it after
24822 gen_decl_die() call. */
24823 saved_external_flag = DECL_EXTERNAL (decl);
24824 DECL_EXTERNAL (decl) = 1;
24825 gen_decl_die (decl, NULL, NULL, context_die);
24826 DECL_EXTERNAL (decl) = saved_external_flag;
24827 break;
24829 case NAMESPACE_DECL:
24830 if (dwarf_version >= 3 || !dwarf_strict)
24831 dwarf2out_decl (decl);
24832 else
24833 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
24834 decl_die = comp_unit_die ();
24835 break;
24837 case TRANSLATION_UNIT_DECL:
24838 decl_die = comp_unit_die ();
24839 break;
24841 default:
24842 gcc_unreachable ();
24845 /* We should be able to find the DIE now. */
24846 if (!decl_die)
24847 decl_die = lookup_decl_die (decl);
24848 gcc_assert (decl_die);
24851 return decl_die;
24854 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
24855 always returned. */
24857 static dw_die_ref
24858 force_type_die (tree type)
24860 dw_die_ref type_die;
24862 type_die = lookup_type_die (type);
24863 if (!type_die)
24865 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
24867 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
24868 false, context_die);
24869 gcc_assert (type_die);
24871 return type_die;
24874 /* Force out any required namespaces to be able to output DECL,
24875 and return the new context_die for it, if it's changed. */
24877 static dw_die_ref
24878 setup_namespace_context (tree thing, dw_die_ref context_die)
24880 tree context = (DECL_P (thing)
24881 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
24882 if (context && TREE_CODE (context) == NAMESPACE_DECL)
24883 /* Force out the namespace. */
24884 context_die = force_decl_die (context);
24886 return context_die;
24889 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
24890 type) within its namespace, if appropriate.
24892 For compatibility with older debuggers, namespace DIEs only contain
24893 declarations; all definitions are emitted at CU scope, with
24894 DW_AT_specification pointing to the declaration (like with class
24895 members). */
24897 static dw_die_ref
24898 declare_in_namespace (tree thing, dw_die_ref context_die)
24900 dw_die_ref ns_context;
24902 if (debug_info_level <= DINFO_LEVEL_TERSE)
24903 return context_die;
24905 /* External declarations in the local scope only need to be emitted
24906 once, not once in the namespace and once in the scope.
24908 This avoids declaring the `extern' below in the
24909 namespace DIE as well as in the innermost scope:
24911 namespace S
24913 int i=5;
24914 int foo()
24916 int i=8;
24917 extern int i;
24918 return i;
24922 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
24923 return context_die;
24925 /* If this decl is from an inlined function, then don't try to emit it in its
24926 namespace, as we will get confused. It would have already been emitted
24927 when the abstract instance of the inline function was emitted anyways. */
24928 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
24929 return context_die;
24931 ns_context = setup_namespace_context (thing, context_die);
24933 if (ns_context != context_die)
24935 if (is_fortran ())
24936 return ns_context;
24937 if (DECL_P (thing))
24938 gen_decl_die (thing, NULL, NULL, ns_context);
24939 else
24940 gen_type_die (thing, ns_context);
24942 return context_die;
24945 /* Generate a DIE for a namespace or namespace alias. */
24947 static void
24948 gen_namespace_die (tree decl, dw_die_ref context_die)
24950 dw_die_ref namespace_die;
24952 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
24953 they are an alias of. */
24954 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
24956 /* Output a real namespace or module. */
24957 context_die = setup_namespace_context (decl, comp_unit_die ());
24958 namespace_die = new_die (is_fortran ()
24959 ? DW_TAG_module : DW_TAG_namespace,
24960 context_die, decl);
24961 /* For Fortran modules defined in different CU don't add src coords. */
24962 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
24964 const char *name = dwarf2_name (decl, 0);
24965 if (name)
24966 add_name_attribute (namespace_die, name);
24968 else
24969 add_name_and_src_coords_attributes (namespace_die, decl);
24970 if (DECL_EXTERNAL (decl))
24971 add_AT_flag (namespace_die, DW_AT_declaration, 1);
24972 equate_decl_number_to_die (decl, namespace_die);
24974 else
24976 /* Output a namespace alias. */
24978 /* Force out the namespace we are an alias of, if necessary. */
24979 dw_die_ref origin_die
24980 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
24982 if (DECL_FILE_SCOPE_P (decl)
24983 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
24984 context_die = setup_namespace_context (decl, comp_unit_die ());
24985 /* Now create the namespace alias DIE. */
24986 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
24987 add_name_and_src_coords_attributes (namespace_die, decl);
24988 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
24989 equate_decl_number_to_die (decl, namespace_die);
24991 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
24992 if (want_pubnames ())
24993 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
24996 /* Generate Dwarf debug information for a decl described by DECL.
24997 The return value is currently only meaningful for PARM_DECLs,
24998 for all other decls it returns NULL.
25000 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25001 It can be NULL otherwise. */
25003 static dw_die_ref
25004 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25005 dw_die_ref context_die)
25007 tree decl_or_origin = decl ? decl : origin;
25008 tree class_origin = NULL, ultimate_origin;
25010 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25011 return NULL;
25013 /* Ignore pointer bounds decls. */
25014 if (DECL_P (decl_or_origin)
25015 && TREE_TYPE (decl_or_origin)
25016 && POINTER_BOUNDS_P (decl_or_origin))
25017 return NULL;
25019 switch (TREE_CODE (decl_or_origin))
25021 case ERROR_MARK:
25022 break;
25024 case CONST_DECL:
25025 if (!is_fortran () && !is_ada ())
25027 /* The individual enumerators of an enum type get output when we output
25028 the Dwarf representation of the relevant enum type itself. */
25029 break;
25032 /* Emit its type. */
25033 gen_type_die (TREE_TYPE (decl), context_die);
25035 /* And its containing namespace. */
25036 context_die = declare_in_namespace (decl, context_die);
25038 gen_const_die (decl, context_die);
25039 break;
25041 case FUNCTION_DECL:
25042 /* Don't output any DIEs to represent mere function declarations,
25043 unless they are class members or explicit block externs. */
25044 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25045 && DECL_FILE_SCOPE_P (decl_or_origin)
25046 && (current_function_decl == NULL_TREE
25047 || DECL_ARTIFICIAL (decl_or_origin)))
25048 break;
25050 #if 0
25051 /* FIXME */
25052 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25053 on local redeclarations of global functions. That seems broken. */
25054 if (current_function_decl != decl)
25055 /* This is only a declaration. */;
25056 #endif
25058 /* If we're emitting a clone, emit info for the abstract instance. */
25059 if (origin || DECL_ORIGIN (decl) != decl)
25060 dwarf2out_abstract_function (origin
25061 ? DECL_ORIGIN (origin)
25062 : DECL_ABSTRACT_ORIGIN (decl));
25064 /* If we're emitting an out-of-line copy of an inline function,
25065 emit info for the abstract instance and set up to refer to it. */
25066 else if (cgraph_function_possibly_inlined_p (decl)
25067 && ! DECL_ABSTRACT_P (decl)
25068 && ! class_or_namespace_scope_p (context_die)
25069 /* dwarf2out_abstract_function won't emit a die if this is just
25070 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25071 that case, because that works only if we have a die. */
25072 && DECL_INITIAL (decl) != NULL_TREE)
25074 dwarf2out_abstract_function (decl);
25075 set_decl_origin_self (decl);
25078 /* Otherwise we're emitting the primary DIE for this decl. */
25079 else if (debug_info_level > DINFO_LEVEL_TERSE)
25081 /* Before we describe the FUNCTION_DECL itself, make sure that we
25082 have its containing type. */
25083 if (!origin)
25084 origin = decl_class_context (decl);
25085 if (origin != NULL_TREE)
25086 gen_type_die (origin, context_die);
25088 /* And its return type. */
25089 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25091 /* And its virtual context. */
25092 if (DECL_VINDEX (decl) != NULL_TREE)
25093 gen_type_die (DECL_CONTEXT (decl), context_die);
25095 /* Make sure we have a member DIE for decl. */
25096 if (origin != NULL_TREE)
25097 gen_type_die_for_member (origin, decl, context_die);
25099 /* And its containing namespace. */
25100 context_die = declare_in_namespace (decl, context_die);
25103 /* Now output a DIE to represent the function itself. */
25104 if (decl)
25105 gen_subprogram_die (decl, context_die);
25106 break;
25108 case TYPE_DECL:
25109 /* If we are in terse mode, don't generate any DIEs to represent any
25110 actual typedefs. */
25111 if (debug_info_level <= DINFO_LEVEL_TERSE)
25112 break;
25114 /* In the special case of a TYPE_DECL node representing the declaration
25115 of some type tag, if the given TYPE_DECL is marked as having been
25116 instantiated from some other (original) TYPE_DECL node (e.g. one which
25117 was generated within the original definition of an inline function) we
25118 used to generate a special (abbreviated) DW_TAG_structure_type,
25119 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25120 should be actually referencing those DIEs, as variable DIEs with that
25121 type would be emitted already in the abstract origin, so it was always
25122 removed during unused type prunning. Don't add anything in this
25123 case. */
25124 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25125 break;
25127 if (is_redundant_typedef (decl))
25128 gen_type_die (TREE_TYPE (decl), context_die);
25129 else
25130 /* Output a DIE to represent the typedef itself. */
25131 gen_typedef_die (decl, context_die);
25132 break;
25134 case LABEL_DECL:
25135 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25136 gen_label_die (decl, context_die);
25137 break;
25139 case VAR_DECL:
25140 case RESULT_DECL:
25141 /* If we are in terse mode, don't generate any DIEs to represent any
25142 variable declarations or definitions. */
25143 if (debug_info_level <= DINFO_LEVEL_TERSE)
25144 break;
25146 /* Output any DIEs that are needed to specify the type of this data
25147 object. */
25148 if (decl_by_reference_p (decl_or_origin))
25149 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25150 else
25151 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25153 /* And its containing type. */
25154 class_origin = decl_class_context (decl_or_origin);
25155 if (class_origin != NULL_TREE)
25156 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25158 /* And its containing namespace. */
25159 context_die = declare_in_namespace (decl_or_origin, context_die);
25161 /* Now output the DIE to represent the data object itself. This gets
25162 complicated because of the possibility that the VAR_DECL really
25163 represents an inlined instance of a formal parameter for an inline
25164 function. */
25165 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25166 if (ultimate_origin != NULL_TREE
25167 && TREE_CODE (ultimate_origin) == PARM_DECL)
25168 gen_formal_parameter_die (decl, origin,
25169 true /* Emit name attribute. */,
25170 context_die);
25171 else
25172 gen_variable_die (decl, origin, context_die);
25173 break;
25175 case FIELD_DECL:
25176 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25177 /* Ignore the nameless fields that are used to skip bits but handle C++
25178 anonymous unions and structs. */
25179 if (DECL_NAME (decl) != NULL_TREE
25180 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25181 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25183 gen_type_die (member_declared_type (decl), context_die);
25184 gen_field_die (decl, ctx, context_die);
25186 break;
25188 case PARM_DECL:
25189 if (DECL_BY_REFERENCE (decl_or_origin))
25190 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25191 else
25192 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25193 return gen_formal_parameter_die (decl, origin,
25194 true /* Emit name attribute. */,
25195 context_die);
25197 case NAMESPACE_DECL:
25198 if (dwarf_version >= 3 || !dwarf_strict)
25199 gen_namespace_die (decl, context_die);
25200 break;
25202 case IMPORTED_DECL:
25203 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25204 DECL_CONTEXT (decl), context_die);
25205 break;
25207 case NAMELIST_DECL:
25208 gen_namelist_decl (DECL_NAME (decl), context_die,
25209 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25210 break;
25212 default:
25213 /* Probably some frontend-internal decl. Assume we don't care. */
25214 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25215 break;
25218 return NULL;
25221 /* Output initial debug information for global DECL. Called at the
25222 end of the parsing process.
25224 This is the initial debug generation process. As such, the DIEs
25225 generated may be incomplete. A later debug generation pass
25226 (dwarf2out_late_global_decl) will augment the information generated
25227 in this pass (e.g., with complete location info). */
25229 static void
25230 dwarf2out_early_global_decl (tree decl)
25232 set_early_dwarf s;
25234 /* gen_decl_die() will set DECL_ABSTRACT because
25235 cgraph_function_possibly_inlined_p() returns true. This is in
25236 turn will cause DW_AT_inline attributes to be set.
25238 This happens because at early dwarf generation, there is no
25239 cgraph information, causing cgraph_function_possibly_inlined_p()
25240 to return true. Trick cgraph_function_possibly_inlined_p()
25241 while we generate dwarf early. */
25242 bool save = symtab->global_info_ready;
25243 symtab->global_info_ready = true;
25245 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25246 other DECLs and they can point to template types or other things
25247 that dwarf2out can't handle when done via dwarf2out_decl. */
25248 if (TREE_CODE (decl) != TYPE_DECL
25249 && TREE_CODE (decl) != PARM_DECL)
25251 tree save_fndecl = current_function_decl;
25252 if (TREE_CODE (decl) == FUNCTION_DECL)
25254 /* No cfun means the symbol has no body, so there's nothing
25255 to emit. */
25256 if (!DECL_STRUCT_FUNCTION (decl))
25257 goto early_decl_exit;
25259 /* For nested functions, emit DIEs for the parents first so that all
25260 nested DIEs are generated at the proper scope in the first
25261 shot. */
25262 tree context = decl_function_context (decl);
25263 if (context != NULL)
25265 current_function_decl = context;
25266 dwarf2out_decl (context);
25269 current_function_decl = decl;
25271 dwarf2out_decl (decl);
25272 if (TREE_CODE (decl) == FUNCTION_DECL)
25273 current_function_decl = save_fndecl;
25275 early_decl_exit:
25276 symtab->global_info_ready = save;
25279 /* Output debug information for global decl DECL. Called from
25280 toplev.c after compilation proper has finished. */
25282 static void
25283 dwarf2out_late_global_decl (tree decl)
25285 /* Fill-in any location information we were unable to determine
25286 on the first pass. */
25287 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25289 dw_die_ref die = lookup_decl_die (decl);
25291 /* We have to generate early debug late for LTO. */
25292 if (! die && in_lto_p)
25294 dwarf2out_decl (decl);
25295 die = lookup_decl_die (decl);
25298 if (die)
25300 /* We get called via the symtab code invoking late_global_decl
25301 for symbols that are optimized out. Do not add locations
25302 for those. */
25303 varpool_node *node = varpool_node::get (decl);
25304 if (! node || ! node->definition)
25305 tree_add_const_value_attribute_for_decl (die, decl);
25306 else
25307 add_location_or_const_value_attribute (die, decl, false);
25312 /* Output debug information for type decl DECL. Called from toplev.c
25313 and from language front ends (to record built-in types). */
25314 static void
25315 dwarf2out_type_decl (tree decl, int local)
25317 if (!local)
25319 set_early_dwarf s;
25320 dwarf2out_decl (decl);
25324 /* Output debug information for imported module or decl DECL.
25325 NAME is non-NULL name in the lexical block if the decl has been renamed.
25326 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25327 that DECL belongs to.
25328 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25329 static void
25330 dwarf2out_imported_module_or_decl_1 (tree decl,
25331 tree name,
25332 tree lexical_block,
25333 dw_die_ref lexical_block_die)
25335 expanded_location xloc;
25336 dw_die_ref imported_die = NULL;
25337 dw_die_ref at_import_die;
25339 if (TREE_CODE (decl) == IMPORTED_DECL)
25341 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25342 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25343 gcc_assert (decl);
25345 else
25346 xloc = expand_location (input_location);
25348 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25350 at_import_die = force_type_die (TREE_TYPE (decl));
25351 /* For namespace N { typedef void T; } using N::T; base_type_die
25352 returns NULL, but DW_TAG_imported_declaration requires
25353 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25354 if (!at_import_die)
25356 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25357 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25358 at_import_die = lookup_type_die (TREE_TYPE (decl));
25359 gcc_assert (at_import_die);
25362 else
25364 at_import_die = lookup_decl_die (decl);
25365 if (!at_import_die)
25367 /* If we're trying to avoid duplicate debug info, we may not have
25368 emitted the member decl for this field. Emit it now. */
25369 if (TREE_CODE (decl) == FIELD_DECL)
25371 tree type = DECL_CONTEXT (decl);
25373 if (TYPE_CONTEXT (type)
25374 && TYPE_P (TYPE_CONTEXT (type))
25375 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25376 DINFO_USAGE_DIR_USE))
25377 return;
25378 gen_type_die_for_member (type, decl,
25379 get_context_die (TYPE_CONTEXT (type)));
25381 if (TREE_CODE (decl) == NAMELIST_DECL)
25382 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25383 get_context_die (DECL_CONTEXT (decl)),
25384 NULL_TREE);
25385 else
25386 at_import_die = force_decl_die (decl);
25390 if (TREE_CODE (decl) == NAMESPACE_DECL)
25392 if (dwarf_version >= 3 || !dwarf_strict)
25393 imported_die = new_die (DW_TAG_imported_module,
25394 lexical_block_die,
25395 lexical_block);
25396 else
25397 return;
25399 else
25400 imported_die = new_die (DW_TAG_imported_declaration,
25401 lexical_block_die,
25402 lexical_block);
25404 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25405 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25406 if (name)
25407 add_AT_string (imported_die, DW_AT_name,
25408 IDENTIFIER_POINTER (name));
25409 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25412 /* Output debug information for imported module or decl DECL.
25413 NAME is non-NULL name in context if the decl has been renamed.
25414 CHILD is true if decl is one of the renamed decls as part of
25415 importing whole module. */
25417 static void
25418 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25419 bool child)
25421 /* dw_die_ref at_import_die; */
25422 dw_die_ref scope_die;
25424 if (debug_info_level <= DINFO_LEVEL_TERSE)
25425 return;
25427 gcc_assert (decl);
25429 set_early_dwarf s;
25431 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25432 We need decl DIE for reference and scope die. First, get DIE for the decl
25433 itself. */
25435 /* Get the scope die for decl context. Use comp_unit_die for global module
25436 or decl. If die is not found for non globals, force new die. */
25437 if (context
25438 && TYPE_P (context)
25439 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25440 return;
25442 scope_die = get_context_die (context);
25444 if (child)
25446 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25447 there is nothing we can do, here. */
25448 if (dwarf_version < 3 && dwarf_strict)
25449 return;
25451 gcc_assert (scope_die->die_child);
25452 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25453 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25454 scope_die = scope_die->die_child;
25457 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25458 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25461 /* Output debug information for namelists. */
25463 static dw_die_ref
25464 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25466 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25467 tree value;
25468 unsigned i;
25470 if (debug_info_level <= DINFO_LEVEL_TERSE)
25471 return NULL;
25473 gcc_assert (scope_die != NULL);
25474 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25475 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25477 /* If there are no item_decls, we have a nondefining namelist, e.g.
25478 with USE association; hence, set DW_AT_declaration. */
25479 if (item_decls == NULL_TREE)
25481 add_AT_flag (nml_die, DW_AT_declaration, 1);
25482 return nml_die;
25485 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25487 nml_item_ref_die = lookup_decl_die (value);
25488 if (!nml_item_ref_die)
25489 nml_item_ref_die = force_decl_die (value);
25491 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25492 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25494 return nml_die;
25498 /* Write the debugging output for DECL and return the DIE. */
25500 static void
25501 dwarf2out_decl (tree decl)
25503 dw_die_ref context_die = comp_unit_die ();
25505 switch (TREE_CODE (decl))
25507 case ERROR_MARK:
25508 return;
25510 case FUNCTION_DECL:
25511 /* What we would really like to do here is to filter out all mere
25512 file-scope declarations of file-scope functions which are never
25513 referenced later within this translation unit (and keep all of ones
25514 that *are* referenced later on) but we aren't clairvoyant, so we have
25515 no idea which functions will be referenced in the future (i.e. later
25516 on within the current translation unit). So here we just ignore all
25517 file-scope function declarations which are not also definitions. If
25518 and when the debugger needs to know something about these functions,
25519 it will have to hunt around and find the DWARF information associated
25520 with the definition of the function.
25522 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25523 nodes represent definitions and which ones represent mere
25524 declarations. We have to check DECL_INITIAL instead. That's because
25525 the C front-end supports some weird semantics for "extern inline"
25526 function definitions. These can get inlined within the current
25527 translation unit (and thus, we need to generate Dwarf info for their
25528 abstract instances so that the Dwarf info for the concrete inlined
25529 instances can have something to refer to) but the compiler never
25530 generates any out-of-lines instances of such things (despite the fact
25531 that they *are* definitions).
25533 The important point is that the C front-end marks these "extern
25534 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25535 them anyway. Note that the C++ front-end also plays some similar games
25536 for inline function definitions appearing within include files which
25537 also contain `#pragma interface' pragmas.
25539 If we are called from dwarf2out_abstract_function output a DIE
25540 anyway. We can end up here this way with early inlining and LTO
25541 where the inlined function is output in a different LTRANS unit
25542 or not at all. */
25543 if (DECL_INITIAL (decl) == NULL_TREE
25544 && ! DECL_ABSTRACT_P (decl))
25545 return;
25547 /* If we're a nested function, initially use a parent of NULL; if we're
25548 a plain function, this will be fixed up in decls_for_scope. If
25549 we're a method, it will be ignored, since we already have a DIE. */
25550 if (decl_function_context (decl)
25551 /* But if we're in terse mode, we don't care about scope. */
25552 && debug_info_level > DINFO_LEVEL_TERSE)
25553 context_die = NULL;
25554 break;
25556 case VAR_DECL:
25557 /* For local statics lookup proper context die. */
25558 if (local_function_static (decl))
25559 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25561 /* If we are in terse mode, don't generate any DIEs to represent any
25562 variable declarations or definitions. */
25563 if (debug_info_level <= DINFO_LEVEL_TERSE)
25564 return;
25565 break;
25567 case CONST_DECL:
25568 if (debug_info_level <= DINFO_LEVEL_TERSE)
25569 return;
25570 if (!is_fortran () && !is_ada ())
25571 return;
25572 if (TREE_STATIC (decl) && decl_function_context (decl))
25573 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25574 break;
25576 case NAMESPACE_DECL:
25577 case IMPORTED_DECL:
25578 if (debug_info_level <= DINFO_LEVEL_TERSE)
25579 return;
25580 if (lookup_decl_die (decl) != NULL)
25581 return;
25582 break;
25584 case TYPE_DECL:
25585 /* Don't emit stubs for types unless they are needed by other DIEs. */
25586 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25587 return;
25589 /* Don't bother trying to generate any DIEs to represent any of the
25590 normal built-in types for the language we are compiling. */
25591 if (DECL_IS_BUILTIN (decl))
25592 return;
25594 /* If we are in terse mode, don't generate any DIEs for types. */
25595 if (debug_info_level <= DINFO_LEVEL_TERSE)
25596 return;
25598 /* If we're a function-scope tag, initially use a parent of NULL;
25599 this will be fixed up in decls_for_scope. */
25600 if (decl_function_context (decl))
25601 context_die = NULL;
25603 break;
25605 case NAMELIST_DECL:
25606 break;
25608 default:
25609 return;
25612 gen_decl_die (decl, NULL, NULL, context_die);
25614 if (flag_checking)
25616 dw_die_ref die = lookup_decl_die (decl);
25617 if (die)
25618 check_die (die);
25622 /* Write the debugging output for DECL. */
25624 static void
25625 dwarf2out_function_decl (tree decl)
25627 dwarf2out_decl (decl);
25628 call_arg_locations = NULL;
25629 call_arg_loc_last = NULL;
25630 call_site_count = -1;
25631 tail_call_site_count = -1;
25632 decl_loc_table->empty ();
25633 cached_dw_loc_list_table->empty ();
25636 /* Output a marker (i.e. a label) for the beginning of the generated code for
25637 a lexical block. */
25639 static void
25640 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25641 unsigned int blocknum)
25643 switch_to_section (current_function_section ());
25644 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25647 /* Output a marker (i.e. a label) for the end of the generated code for a
25648 lexical block. */
25650 static void
25651 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25653 switch_to_section (current_function_section ());
25654 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25657 /* Returns nonzero if it is appropriate not to emit any debugging
25658 information for BLOCK, because it doesn't contain any instructions.
25660 Don't allow this for blocks with nested functions or local classes
25661 as we would end up with orphans, and in the presence of scheduling
25662 we may end up calling them anyway. */
25664 static bool
25665 dwarf2out_ignore_block (const_tree block)
25667 tree decl;
25668 unsigned int i;
25670 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25671 if (TREE_CODE (decl) == FUNCTION_DECL
25672 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25673 return 0;
25674 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25676 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25677 if (TREE_CODE (decl) == FUNCTION_DECL
25678 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25679 return 0;
25682 return 1;
25685 /* Hash table routines for file_hash. */
25687 bool
25688 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25690 return filename_cmp (p1->filename, p2) == 0;
25693 hashval_t
25694 dwarf_file_hasher::hash (dwarf_file_data *p)
25696 return htab_hash_string (p->filename);
25699 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25700 dwarf2out.c) and return its "index". The index of each (known) filename is
25701 just a unique number which is associated with only that one filename. We
25702 need such numbers for the sake of generating labels (in the .debug_sfnames
25703 section) and references to those files numbers (in the .debug_srcinfo
25704 and .debug_macinfo sections). If the filename given as an argument is not
25705 found in our current list, add it to the list and assign it the next
25706 available unique index number. */
25708 static struct dwarf_file_data *
25709 lookup_filename (const char *file_name)
25711 struct dwarf_file_data * created;
25713 if (!file_name)
25714 return NULL;
25716 dwarf_file_data **slot
25717 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25718 INSERT);
25719 if (*slot)
25720 return *slot;
25722 created = ggc_alloc<dwarf_file_data> ();
25723 created->filename = file_name;
25724 created->emitted_number = 0;
25725 *slot = created;
25726 return created;
25729 /* If the assembler will construct the file table, then translate the compiler
25730 internal file table number into the assembler file table number, and emit
25731 a .file directive if we haven't already emitted one yet. The file table
25732 numbers are different because we prune debug info for unused variables and
25733 types, which may include filenames. */
25735 static int
25736 maybe_emit_file (struct dwarf_file_data * fd)
25738 if (! fd->emitted_number)
25740 if (last_emitted_file)
25741 fd->emitted_number = last_emitted_file->emitted_number + 1;
25742 else
25743 fd->emitted_number = 1;
25744 last_emitted_file = fd;
25746 if (DWARF2_ASM_LINE_DEBUG_INFO)
25748 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25749 output_quoted_string (asm_out_file,
25750 remap_debug_filename (fd->filename));
25751 fputc ('\n', asm_out_file);
25755 return fd->emitted_number;
25758 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25759 That generation should happen after function debug info has been
25760 generated. The value of the attribute is the constant value of ARG. */
25762 static void
25763 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25765 die_arg_entry entry;
25767 if (!die || !arg)
25768 return;
25770 gcc_assert (early_dwarf);
25772 if (!tmpl_value_parm_die_table)
25773 vec_alloc (tmpl_value_parm_die_table, 32);
25775 entry.die = die;
25776 entry.arg = arg;
25777 vec_safe_push (tmpl_value_parm_die_table, entry);
25780 /* Return TRUE if T is an instance of generic type, FALSE
25781 otherwise. */
25783 static bool
25784 generic_type_p (tree t)
25786 if (t == NULL_TREE || !TYPE_P (t))
25787 return false;
25788 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25791 /* Schedule the generation of the generic parameter dies for the
25792 instance of generic type T. The proper generation itself is later
25793 done by gen_scheduled_generic_parms_dies. */
25795 static void
25796 schedule_generic_params_dies_gen (tree t)
25798 if (!generic_type_p (t))
25799 return;
25801 gcc_assert (early_dwarf);
25803 if (!generic_type_instances)
25804 vec_alloc (generic_type_instances, 256);
25806 vec_safe_push (generic_type_instances, t);
25809 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25810 by append_entry_to_tmpl_value_parm_die_table. This function must
25811 be called after function DIEs have been generated. */
25813 static void
25814 gen_remaining_tmpl_value_param_die_attribute (void)
25816 if (tmpl_value_parm_die_table)
25818 unsigned i, j;
25819 die_arg_entry *e;
25821 /* We do this in two phases - first get the cases we can
25822 handle during early-finish, preserving those we cannot
25823 (containing symbolic constants where we don't yet know
25824 whether we are going to output the referenced symbols).
25825 For those we try again at late-finish. */
25826 j = 0;
25827 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
25829 if (!tree_add_const_value_attribute (e->die, e->arg))
25831 dw_loc_descr_ref loc = NULL;
25832 if (! early_dwarf
25833 && (dwarf_version >= 5 || !dwarf_strict))
25834 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
25835 if (loc)
25836 add_AT_loc (e->die, DW_AT_location, loc);
25837 else
25838 (*tmpl_value_parm_die_table)[j++] = *e;
25841 tmpl_value_parm_die_table->truncate (j);
25845 /* Generate generic parameters DIEs for instances of generic types
25846 that have been previously scheduled by
25847 schedule_generic_params_dies_gen. This function must be called
25848 after all the types of the CU have been laid out. */
25850 static void
25851 gen_scheduled_generic_parms_dies (void)
25853 unsigned i;
25854 tree t;
25856 if (!generic_type_instances)
25857 return;
25859 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
25860 if (COMPLETE_TYPE_P (t))
25861 gen_generic_params_dies (t);
25863 generic_type_instances = NULL;
25867 /* Replace DW_AT_name for the decl with name. */
25869 static void
25870 dwarf2out_set_name (tree decl, tree name)
25872 dw_die_ref die;
25873 dw_attr_node *attr;
25874 const char *dname;
25876 die = TYPE_SYMTAB_DIE (decl);
25877 if (!die)
25878 return;
25880 dname = dwarf2_name (name, 0);
25881 if (!dname)
25882 return;
25884 attr = get_AT (die, DW_AT_name);
25885 if (attr)
25887 struct indirect_string_node *node;
25889 node = find_AT_string (dname);
25890 /* replace the string. */
25891 attr->dw_attr_val.v.val_str = node;
25894 else
25895 add_name_attribute (die, dname);
25898 /* True if before or during processing of the first function being emitted. */
25899 static bool in_first_function_p = true;
25900 /* True if loc_note during dwarf2out_var_location call might still be
25901 before first real instruction at address equal to .Ltext0. */
25902 static bool maybe_at_text_label_p = true;
25903 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
25904 static unsigned int first_loclabel_num_not_at_text_label;
25906 /* Called by the final INSN scan whenever we see a var location. We
25907 use it to drop labels in the right places, and throw the location in
25908 our lookup table. */
25910 static void
25911 dwarf2out_var_location (rtx_insn *loc_note)
25913 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
25914 struct var_loc_node *newloc;
25915 rtx_insn *next_real, *next_note;
25916 rtx_insn *call_insn = NULL;
25917 static const char *last_label;
25918 static const char *last_postcall_label;
25919 static bool last_in_cold_section_p;
25920 static rtx_insn *expected_next_loc_note;
25921 tree decl;
25922 bool var_loc_p;
25924 if (!NOTE_P (loc_note))
25926 if (CALL_P (loc_note))
25928 call_site_count++;
25929 if (SIBLING_CALL_P (loc_note))
25930 tail_call_site_count++;
25931 if (optimize == 0 && !flag_var_tracking)
25933 /* When the var-tracking pass is not running, there is no note
25934 for indirect calls whose target is compile-time known. In this
25935 case, process such calls specifically so that we generate call
25936 sites for them anyway. */
25937 rtx x = PATTERN (loc_note);
25938 if (GET_CODE (x) == PARALLEL)
25939 x = XVECEXP (x, 0, 0);
25940 if (GET_CODE (x) == SET)
25941 x = SET_SRC (x);
25942 if (GET_CODE (x) == CALL)
25943 x = XEXP (x, 0);
25944 if (!MEM_P (x)
25945 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
25946 || !SYMBOL_REF_DECL (XEXP (x, 0))
25947 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
25948 != FUNCTION_DECL))
25950 call_insn = loc_note;
25951 loc_note = NULL;
25952 var_loc_p = false;
25954 next_real = next_real_insn (call_insn);
25955 next_note = NULL;
25956 cached_next_real_insn = NULL;
25957 goto create_label;
25961 return;
25964 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
25965 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
25966 return;
25968 /* Optimize processing a large consecutive sequence of location
25969 notes so we don't spend too much time in next_real_insn. If the
25970 next insn is another location note, remember the next_real_insn
25971 calculation for next time. */
25972 next_real = cached_next_real_insn;
25973 if (next_real)
25975 if (expected_next_loc_note != loc_note)
25976 next_real = NULL;
25979 next_note = NEXT_INSN (loc_note);
25980 if (! next_note
25981 || next_note->deleted ()
25982 || ! NOTE_P (next_note)
25983 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
25984 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
25985 next_note = NULL;
25987 if (! next_real)
25988 next_real = next_real_insn (loc_note);
25990 if (next_note)
25992 expected_next_loc_note = next_note;
25993 cached_next_real_insn = next_real;
25995 else
25996 cached_next_real_insn = NULL;
25998 /* If there are no instructions which would be affected by this note,
25999 don't do anything. */
26000 if (var_loc_p
26001 && next_real == NULL_RTX
26002 && !NOTE_DURING_CALL_P (loc_note))
26003 return;
26005 create_label:
26007 if (next_real == NULL_RTX)
26008 next_real = get_last_insn ();
26010 /* If there were any real insns between note we processed last time
26011 and this note (or if it is the first note), clear
26012 last_{,postcall_}label so that they are not reused this time. */
26013 if (last_var_location_insn == NULL_RTX
26014 || last_var_location_insn != next_real
26015 || last_in_cold_section_p != in_cold_section_p)
26017 last_label = NULL;
26018 last_postcall_label = NULL;
26021 if (var_loc_p)
26023 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26024 newloc = add_var_loc_to_decl (decl, loc_note,
26025 NOTE_DURING_CALL_P (loc_note)
26026 ? last_postcall_label : last_label);
26027 if (newloc == NULL)
26028 return;
26030 else
26032 decl = NULL_TREE;
26033 newloc = NULL;
26036 /* If there were no real insns between note we processed last time
26037 and this note, use the label we emitted last time. Otherwise
26038 create a new label and emit it. */
26039 if (last_label == NULL)
26041 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26042 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26043 loclabel_num++;
26044 last_label = ggc_strdup (loclabel);
26045 /* See if loclabel might be equal to .Ltext0. If yes,
26046 bump first_loclabel_num_not_at_text_label. */
26047 if (!have_multiple_function_sections
26048 && in_first_function_p
26049 && maybe_at_text_label_p)
26051 static rtx_insn *last_start;
26052 rtx_insn *insn;
26053 for (insn = loc_note; insn; insn = previous_insn (insn))
26054 if (insn == last_start)
26055 break;
26056 else if (!NONDEBUG_INSN_P (insn))
26057 continue;
26058 else
26060 rtx body = PATTERN (insn);
26061 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26062 continue;
26063 /* Inline asm could occupy zero bytes. */
26064 else if (GET_CODE (body) == ASM_INPUT
26065 || asm_noperands (body) >= 0)
26066 continue;
26067 #ifdef HAVE_attr_length
26068 else if (get_attr_min_length (insn) == 0)
26069 continue;
26070 #endif
26071 else
26073 /* Assume insn has non-zero length. */
26074 maybe_at_text_label_p = false;
26075 break;
26078 if (maybe_at_text_label_p)
26080 last_start = loc_note;
26081 first_loclabel_num_not_at_text_label = loclabel_num;
26086 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26087 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26089 if (!var_loc_p)
26091 struct call_arg_loc_node *ca_loc
26092 = ggc_cleared_alloc<call_arg_loc_node> ();
26093 rtx_insn *prev
26094 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26096 ca_loc->call_arg_loc_note = loc_note;
26097 ca_loc->next = NULL;
26098 ca_loc->label = last_label;
26099 gcc_assert (prev
26100 && (CALL_P (prev)
26101 || (NONJUMP_INSN_P (prev)
26102 && GET_CODE (PATTERN (prev)) == SEQUENCE
26103 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26104 if (!CALL_P (prev))
26105 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26106 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26108 /* Look for a SYMBOL_REF in the "prev" instruction. */
26109 rtx x = get_call_rtx_from (PATTERN (prev));
26110 if (x)
26112 /* Try to get the call symbol, if any. */
26113 if (MEM_P (XEXP (x, 0)))
26114 x = XEXP (x, 0);
26115 /* First, look for a memory access to a symbol_ref. */
26116 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26117 && SYMBOL_REF_DECL (XEXP (x, 0))
26118 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26119 ca_loc->symbol_ref = XEXP (x, 0);
26120 /* Otherwise, look at a compile-time known user-level function
26121 declaration. */
26122 else if (MEM_P (x)
26123 && MEM_EXPR (x)
26124 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26125 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26128 ca_loc->block = insn_scope (prev);
26129 if (call_arg_locations)
26130 call_arg_loc_last->next = ca_loc;
26131 else
26132 call_arg_locations = ca_loc;
26133 call_arg_loc_last = ca_loc;
26135 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26136 newloc->label = last_label;
26137 else
26139 if (!last_postcall_label)
26141 sprintf (loclabel, "%s-1", last_label);
26142 last_postcall_label = ggc_strdup (loclabel);
26144 newloc->label = last_postcall_label;
26147 last_var_location_insn = next_real;
26148 last_in_cold_section_p = in_cold_section_p;
26151 /* Called from finalize_size_functions for size functions so that their body
26152 can be encoded in the debug info to describe the layout of variable-length
26153 structures. */
26155 static void
26156 dwarf2out_size_function (tree decl)
26158 function_to_dwarf_procedure (decl);
26161 /* Note in one location list that text section has changed. */
26164 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26166 var_loc_list *list = *slot;
26167 if (list->first)
26168 list->last_before_switch
26169 = list->last->next ? list->last->next : list->last;
26170 return 1;
26173 /* Note in all location lists that text section has changed. */
26175 static void
26176 var_location_switch_text_section (void)
26178 if (decl_loc_table == NULL)
26179 return;
26181 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26184 /* Create a new line number table. */
26186 static dw_line_info_table *
26187 new_line_info_table (void)
26189 dw_line_info_table *table;
26191 table = ggc_cleared_alloc<dw_line_info_table> ();
26192 table->file_num = 1;
26193 table->line_num = 1;
26194 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26196 return table;
26199 /* Lookup the "current" table into which we emit line info, so
26200 that we don't have to do it for every source line. */
26202 static void
26203 set_cur_line_info_table (section *sec)
26205 dw_line_info_table *table;
26207 if (sec == text_section)
26208 table = text_section_line_info;
26209 else if (sec == cold_text_section)
26211 table = cold_text_section_line_info;
26212 if (!table)
26214 cold_text_section_line_info = table = new_line_info_table ();
26215 table->end_label = cold_end_label;
26218 else
26220 const char *end_label;
26222 if (flag_reorder_blocks_and_partition)
26224 if (in_cold_section_p)
26225 end_label = crtl->subsections.cold_section_end_label;
26226 else
26227 end_label = crtl->subsections.hot_section_end_label;
26229 else
26231 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26232 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26233 current_function_funcdef_no);
26234 end_label = ggc_strdup (label);
26237 table = new_line_info_table ();
26238 table->end_label = end_label;
26240 vec_safe_push (separate_line_info, table);
26243 if (DWARF2_ASM_LINE_DEBUG_INFO)
26244 table->is_stmt = (cur_line_info_table
26245 ? cur_line_info_table->is_stmt
26246 : DWARF_LINE_DEFAULT_IS_STMT_START);
26247 cur_line_info_table = table;
26251 /* We need to reset the locations at the beginning of each
26252 function. We can't do this in the end_function hook, because the
26253 declarations that use the locations won't have been output when
26254 that hook is called. Also compute have_multiple_function_sections here. */
26256 static void
26257 dwarf2out_begin_function (tree fun)
26259 section *sec = function_section (fun);
26261 if (sec != text_section)
26262 have_multiple_function_sections = true;
26264 if (flag_reorder_blocks_and_partition && !cold_text_section)
26266 gcc_assert (current_function_decl == fun);
26267 cold_text_section = unlikely_text_section ();
26268 switch_to_section (cold_text_section);
26269 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26270 switch_to_section (sec);
26273 dwarf2out_note_section_used ();
26274 call_site_count = 0;
26275 tail_call_site_count = 0;
26277 set_cur_line_info_table (sec);
26280 /* Helper function of dwarf2out_end_function, called only after emitting
26281 the very first function into assembly. Check if some .debug_loc range
26282 might end with a .LVL* label that could be equal to .Ltext0.
26283 In that case we must force using absolute addresses in .debug_loc ranges,
26284 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26285 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26286 list terminator.
26287 Set have_multiple_function_sections to true in that case and
26288 terminate htab traversal. */
26291 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26293 var_loc_list *entry = *slot;
26294 struct var_loc_node *node;
26296 node = entry->first;
26297 if (node && node->next && node->next->label)
26299 unsigned int i;
26300 const char *label = node->next->label;
26301 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26303 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26305 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26306 if (strcmp (label, loclabel) == 0)
26308 have_multiple_function_sections = true;
26309 return 0;
26313 return 1;
26316 /* Hook called after emitting a function into assembly.
26317 This does something only for the very first function emitted. */
26319 static void
26320 dwarf2out_end_function (unsigned int)
26322 if (in_first_function_p
26323 && !have_multiple_function_sections
26324 && first_loclabel_num_not_at_text_label
26325 && decl_loc_table)
26326 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26327 in_first_function_p = false;
26328 maybe_at_text_label_p = false;
26331 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26332 front-ends register a translation unit even before dwarf2out_init is
26333 called. */
26334 static tree main_translation_unit = NULL_TREE;
26336 /* Hook called by front-ends after they built their main translation unit.
26337 Associate comp_unit_die to UNIT. */
26339 static void
26340 dwarf2out_register_main_translation_unit (tree unit)
26342 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26343 && main_translation_unit == NULL_TREE);
26344 main_translation_unit = unit;
26345 /* If dwarf2out_init has not been called yet, it will perform the association
26346 itself looking at main_translation_unit. */
26347 if (decl_die_table != NULL)
26348 equate_decl_number_to_die (unit, comp_unit_die ());
26351 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26353 static void
26354 push_dw_line_info_entry (dw_line_info_table *table,
26355 enum dw_line_info_opcode opcode, unsigned int val)
26357 dw_line_info_entry e;
26358 e.opcode = opcode;
26359 e.val = val;
26360 vec_safe_push (table->entries, e);
26363 /* Output a label to mark the beginning of a source code line entry
26364 and record information relating to this source line, in
26365 'line_info_table' for later output of the .debug_line section. */
26366 /* ??? The discriminator parameter ought to be unsigned. */
26368 static void
26369 dwarf2out_source_line (unsigned int line, const char *filename,
26370 int discriminator, bool is_stmt)
26372 unsigned int file_num;
26373 dw_line_info_table *table;
26375 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26376 return;
26378 /* The discriminator column was added in dwarf4. Simplify the below
26379 by simply removing it if we're not supposed to output it. */
26380 if (dwarf_version < 4 && dwarf_strict)
26381 discriminator = 0;
26383 table = cur_line_info_table;
26384 file_num = maybe_emit_file (lookup_filename (filename));
26386 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26387 the debugger has used the second (possibly duplicate) line number
26388 at the beginning of the function to mark the end of the prologue.
26389 We could eliminate any other duplicates within the function. For
26390 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26391 that second line number entry. */
26392 /* Recall that this end-of-prologue indication is *not* the same thing
26393 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26394 to which the hook corresponds, follows the last insn that was
26395 emitted by gen_prologue. What we need is to precede the first insn
26396 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26397 insn that corresponds to something the user wrote. These may be
26398 very different locations once scheduling is enabled. */
26400 if (0 && file_num == table->file_num
26401 && line == table->line_num
26402 && discriminator == table->discrim_num
26403 && is_stmt == table->is_stmt)
26404 return;
26406 switch_to_section (current_function_section ());
26408 /* If requested, emit something human-readable. */
26409 if (flag_debug_asm)
26410 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
26412 if (DWARF2_ASM_LINE_DEBUG_INFO)
26414 /* Emit the .loc directive understood by GNU as. */
26415 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26416 file_num, line, is_stmt, discriminator */
26417 fputs ("\t.loc ", asm_out_file);
26418 fprint_ul (asm_out_file, file_num);
26419 putc (' ', asm_out_file);
26420 fprint_ul (asm_out_file, line);
26421 putc (' ', asm_out_file);
26422 putc ('0', asm_out_file);
26424 if (is_stmt != table->is_stmt)
26426 fputs (" is_stmt ", asm_out_file);
26427 putc (is_stmt ? '1' : '0', asm_out_file);
26429 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26431 gcc_assert (discriminator > 0);
26432 fputs (" discriminator ", asm_out_file);
26433 fprint_ul (asm_out_file, (unsigned long) discriminator);
26435 putc ('\n', asm_out_file);
26437 else
26439 unsigned int label_num = ++line_info_label_num;
26441 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26443 push_dw_line_info_entry (table, LI_set_address, label_num);
26444 if (file_num != table->file_num)
26445 push_dw_line_info_entry (table, LI_set_file, file_num);
26446 if (discriminator != table->discrim_num)
26447 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26448 if (is_stmt != table->is_stmt)
26449 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26450 push_dw_line_info_entry (table, LI_set_line, line);
26453 table->file_num = file_num;
26454 table->line_num = line;
26455 table->discrim_num = discriminator;
26456 table->is_stmt = is_stmt;
26457 table->in_use = true;
26460 /* Record the beginning of a new source file. */
26462 static void
26463 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26465 if (flag_eliminate_dwarf2_dups)
26467 /* Record the beginning of the file for break_out_includes. */
26468 dw_die_ref bincl_die;
26470 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26471 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26474 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26476 macinfo_entry e;
26477 e.code = DW_MACINFO_start_file;
26478 e.lineno = lineno;
26479 e.info = ggc_strdup (filename);
26480 vec_safe_push (macinfo_table, e);
26484 /* Record the end of a source file. */
26486 static void
26487 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26489 if (flag_eliminate_dwarf2_dups)
26490 /* Record the end of the file for break_out_includes. */
26491 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26493 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26495 macinfo_entry e;
26496 e.code = DW_MACINFO_end_file;
26497 e.lineno = lineno;
26498 e.info = NULL;
26499 vec_safe_push (macinfo_table, e);
26503 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26504 the tail part of the directive line, i.e. the part which is past the
26505 initial whitespace, #, whitespace, directive-name, whitespace part. */
26507 static void
26508 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26509 const char *buffer ATTRIBUTE_UNUSED)
26511 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26513 macinfo_entry e;
26514 /* Insert a dummy first entry to be able to optimize the whole
26515 predefined macro block using DW_MACRO_import. */
26516 if (macinfo_table->is_empty () && lineno <= 1)
26518 e.code = 0;
26519 e.lineno = 0;
26520 e.info = NULL;
26521 vec_safe_push (macinfo_table, e);
26523 e.code = DW_MACINFO_define;
26524 e.lineno = lineno;
26525 e.info = ggc_strdup (buffer);
26526 vec_safe_push (macinfo_table, e);
26530 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26531 the tail part of the directive line, i.e. the part which is past the
26532 initial whitespace, #, whitespace, directive-name, whitespace part. */
26534 static void
26535 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26536 const char *buffer ATTRIBUTE_UNUSED)
26538 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26540 macinfo_entry e;
26541 /* Insert a dummy first entry to be able to optimize the whole
26542 predefined macro block using DW_MACRO_import. */
26543 if (macinfo_table->is_empty () && lineno <= 1)
26545 e.code = 0;
26546 e.lineno = 0;
26547 e.info = NULL;
26548 vec_safe_push (macinfo_table, e);
26550 e.code = DW_MACINFO_undef;
26551 e.lineno = lineno;
26552 e.info = ggc_strdup (buffer);
26553 vec_safe_push (macinfo_table, e);
26557 /* Helpers to manipulate hash table of CUs. */
26559 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26561 static inline hashval_t hash (const macinfo_entry *);
26562 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26565 inline hashval_t
26566 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26568 return htab_hash_string (entry->info);
26571 inline bool
26572 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26573 const macinfo_entry *entry2)
26575 return !strcmp (entry1->info, entry2->info);
26578 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26580 /* Output a single .debug_macinfo entry. */
26582 static void
26583 output_macinfo_op (macinfo_entry *ref)
26585 int file_num;
26586 size_t len;
26587 struct indirect_string_node *node;
26588 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26589 struct dwarf_file_data *fd;
26591 switch (ref->code)
26593 case DW_MACINFO_start_file:
26594 fd = lookup_filename (ref->info);
26595 file_num = maybe_emit_file (fd);
26596 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26597 dw2_asm_output_data_uleb128 (ref->lineno,
26598 "Included from line number %lu",
26599 (unsigned long) ref->lineno);
26600 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26601 break;
26602 case DW_MACINFO_end_file:
26603 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26604 break;
26605 case DW_MACINFO_define:
26606 case DW_MACINFO_undef:
26607 len = strlen (ref->info) + 1;
26608 if (!dwarf_strict
26609 && len > DWARF_OFFSET_SIZE
26610 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26611 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26613 ref->code = ref->code == DW_MACINFO_define
26614 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26615 output_macinfo_op (ref);
26616 return;
26618 dw2_asm_output_data (1, ref->code,
26619 ref->code == DW_MACINFO_define
26620 ? "Define macro" : "Undefine macro");
26621 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26622 (unsigned long) ref->lineno);
26623 dw2_asm_output_nstring (ref->info, -1, "The macro");
26624 break;
26625 case DW_MACRO_define_strp:
26626 case DW_MACRO_undef_strp:
26627 node = find_AT_string (ref->info);
26628 gcc_assert (node
26629 && (node->form == DW_FORM_strp
26630 || node->form == DW_FORM_GNU_str_index));
26631 dw2_asm_output_data (1, ref->code,
26632 ref->code == DW_MACRO_define_strp
26633 ? "Define macro strp"
26634 : "Undefine macro strp");
26635 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26636 (unsigned long) ref->lineno);
26637 if (node->form == DW_FORM_strp)
26638 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26639 debug_str_section, "The macro: \"%s\"",
26640 ref->info);
26641 else
26642 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26643 ref->info);
26644 break;
26645 case DW_MACRO_import:
26646 dw2_asm_output_data (1, ref->code, "Import");
26647 ASM_GENERATE_INTERNAL_LABEL (label,
26648 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26649 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26650 break;
26651 default:
26652 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26653 ASM_COMMENT_START, (unsigned long) ref->code);
26654 break;
26658 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26659 other compilation unit .debug_macinfo sections. IDX is the first
26660 index of a define/undef, return the number of ops that should be
26661 emitted in a comdat .debug_macinfo section and emit
26662 a DW_MACRO_import entry referencing it.
26663 If the define/undef entry should be emitted normally, return 0. */
26665 static unsigned
26666 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26667 macinfo_hash_type **macinfo_htab)
26669 macinfo_entry *first, *second, *cur, *inc;
26670 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26671 unsigned char checksum[16];
26672 struct md5_ctx ctx;
26673 char *grp_name, *tail;
26674 const char *base;
26675 unsigned int i, count, encoded_filename_len, linebuf_len;
26676 macinfo_entry **slot;
26678 first = &(*macinfo_table)[idx];
26679 second = &(*macinfo_table)[idx + 1];
26681 /* Optimize only if there are at least two consecutive define/undef ops,
26682 and either all of them are before first DW_MACINFO_start_file
26683 with lineno {0,1} (i.e. predefined macro block), or all of them are
26684 in some included header file. */
26685 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26686 return 0;
26687 if (vec_safe_is_empty (files))
26689 if (first->lineno > 1 || second->lineno > 1)
26690 return 0;
26692 else if (first->lineno == 0)
26693 return 0;
26695 /* Find the last define/undef entry that can be grouped together
26696 with first and at the same time compute md5 checksum of their
26697 codes, linenumbers and strings. */
26698 md5_init_ctx (&ctx);
26699 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26700 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26701 break;
26702 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26703 break;
26704 else
26706 unsigned char code = cur->code;
26707 md5_process_bytes (&code, 1, &ctx);
26708 checksum_uleb128 (cur->lineno, &ctx);
26709 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26711 md5_finish_ctx (&ctx, checksum);
26712 count = i - idx;
26714 /* From the containing include filename (if any) pick up just
26715 usable characters from its basename. */
26716 if (vec_safe_is_empty (files))
26717 base = "";
26718 else
26719 base = lbasename (files->last ().info);
26720 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26721 if (ISIDNUM (base[i]) || base[i] == '.')
26722 encoded_filename_len++;
26723 /* Count . at the end. */
26724 if (encoded_filename_len)
26725 encoded_filename_len++;
26727 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26728 linebuf_len = strlen (linebuf);
26730 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26731 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26732 + 16 * 2 + 1);
26733 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26734 tail = grp_name + 4;
26735 if (encoded_filename_len)
26737 for (i = 0; base[i]; i++)
26738 if (ISIDNUM (base[i]) || base[i] == '.')
26739 *tail++ = base[i];
26740 *tail++ = '.';
26742 memcpy (tail, linebuf, linebuf_len);
26743 tail += linebuf_len;
26744 *tail++ = '.';
26745 for (i = 0; i < 16; i++)
26746 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26748 /* Construct a macinfo_entry for DW_MACRO_import
26749 in the empty vector entry before the first define/undef. */
26750 inc = &(*macinfo_table)[idx - 1];
26751 inc->code = DW_MACRO_import;
26752 inc->lineno = 0;
26753 inc->info = ggc_strdup (grp_name);
26754 if (!*macinfo_htab)
26755 *macinfo_htab = new macinfo_hash_type (10);
26756 /* Avoid emitting duplicates. */
26757 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26758 if (*slot != NULL)
26760 inc->code = 0;
26761 inc->info = NULL;
26762 /* If such an entry has been used before, just emit
26763 a DW_MACRO_import op. */
26764 inc = *slot;
26765 output_macinfo_op (inc);
26766 /* And clear all macinfo_entry in the range to avoid emitting them
26767 in the second pass. */
26768 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26770 cur->code = 0;
26771 cur->info = NULL;
26774 else
26776 *slot = inc;
26777 inc->lineno = (*macinfo_htab)->elements ();
26778 output_macinfo_op (inc);
26780 return count;
26783 /* Save any strings needed by the macinfo table in the debug str
26784 table. All strings must be collected into the table by the time
26785 index_string is called. */
26787 static void
26788 save_macinfo_strings (void)
26790 unsigned len;
26791 unsigned i;
26792 macinfo_entry *ref;
26794 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26796 switch (ref->code)
26798 /* Match the logic in output_macinfo_op to decide on
26799 indirect strings. */
26800 case DW_MACINFO_define:
26801 case DW_MACINFO_undef:
26802 len = strlen (ref->info) + 1;
26803 if (!dwarf_strict
26804 && len > DWARF_OFFSET_SIZE
26805 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26806 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26807 set_indirect_string (find_AT_string (ref->info));
26808 break;
26809 case DW_MACRO_define_strp:
26810 case DW_MACRO_undef_strp:
26811 set_indirect_string (find_AT_string (ref->info));
26812 break;
26813 default:
26814 break;
26819 /* Output macinfo section(s). */
26821 static void
26822 output_macinfo (void)
26824 unsigned i;
26825 unsigned long length = vec_safe_length (macinfo_table);
26826 macinfo_entry *ref;
26827 vec<macinfo_entry, va_gc> *files = NULL;
26828 macinfo_hash_type *macinfo_htab = NULL;
26830 if (! length)
26831 return;
26833 /* output_macinfo* uses these interchangeably. */
26834 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
26835 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
26836 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
26837 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
26839 /* For .debug_macro emit the section header. */
26840 if (!dwarf_strict || dwarf_version >= 5)
26842 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26843 "DWARF macro version number");
26844 if (DWARF_OFFSET_SIZE == 8)
26845 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
26846 else
26847 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
26848 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
26849 (!dwarf_split_debug_info ? debug_line_section_label
26850 : debug_skeleton_line_section_label),
26851 debug_line_section, NULL);
26854 /* In the first loop, it emits the primary .debug_macinfo section
26855 and after each emitted op the macinfo_entry is cleared.
26856 If a longer range of define/undef ops can be optimized using
26857 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
26858 the vector before the first define/undef in the range and the
26859 whole range of define/undef ops is not emitted and kept. */
26860 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26862 switch (ref->code)
26864 case DW_MACINFO_start_file:
26865 vec_safe_push (files, *ref);
26866 break;
26867 case DW_MACINFO_end_file:
26868 if (!vec_safe_is_empty (files))
26869 files->pop ();
26870 break;
26871 case DW_MACINFO_define:
26872 case DW_MACINFO_undef:
26873 if ((!dwarf_strict || dwarf_version >= 5)
26874 && HAVE_COMDAT_GROUP
26875 && vec_safe_length (files) != 1
26876 && i > 0
26877 && i + 1 < length
26878 && (*macinfo_table)[i - 1].code == 0)
26880 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
26881 if (count)
26883 i += count - 1;
26884 continue;
26887 break;
26888 case 0:
26889 /* A dummy entry may be inserted at the beginning to be able
26890 to optimize the whole block of predefined macros. */
26891 if (i == 0)
26892 continue;
26893 default:
26894 break;
26896 output_macinfo_op (ref);
26897 ref->info = NULL;
26898 ref->code = 0;
26901 if (!macinfo_htab)
26902 return;
26904 delete macinfo_htab;
26905 macinfo_htab = NULL;
26907 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
26908 terminate the current chain and switch to a new comdat .debug_macinfo
26909 section and emit the define/undef entries within it. */
26910 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26911 switch (ref->code)
26913 case 0:
26914 continue;
26915 case DW_MACRO_import:
26917 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26918 tree comdat_key = get_identifier (ref->info);
26919 /* Terminate the previous .debug_macinfo section. */
26920 dw2_asm_output_data (1, 0, "End compilation unit");
26921 targetm.asm_out.named_section (debug_macinfo_section_name,
26922 SECTION_DEBUG
26923 | SECTION_LINKONCE,
26924 comdat_key);
26925 ASM_GENERATE_INTERNAL_LABEL (label,
26926 DEBUG_MACRO_SECTION_LABEL,
26927 ref->lineno);
26928 ASM_OUTPUT_LABEL (asm_out_file, label);
26929 ref->code = 0;
26930 ref->info = NULL;
26931 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26932 "DWARF macro version number");
26933 if (DWARF_OFFSET_SIZE == 8)
26934 dw2_asm_output_data (1, 1, "Flags: 64-bit");
26935 else
26936 dw2_asm_output_data (1, 0, "Flags: 32-bit");
26938 break;
26939 case DW_MACINFO_define:
26940 case DW_MACINFO_undef:
26941 output_macinfo_op (ref);
26942 ref->code = 0;
26943 ref->info = NULL;
26944 break;
26945 default:
26946 gcc_unreachable ();
26950 /* Initialize the various sections and labels for dwarf output. */
26952 static void
26953 init_sections_and_labels (void)
26955 if (!dwarf_split_debug_info)
26957 debug_info_section = get_section (DEBUG_INFO_SECTION,
26958 SECTION_DEBUG, NULL);
26959 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
26960 SECTION_DEBUG, NULL);
26961 debug_loc_section = get_section (dwarf_version >= 5
26962 ? DEBUG_LOCLISTS_SECTION
26963 : DEBUG_LOC_SECTION,
26964 SECTION_DEBUG, NULL);
26965 debug_macinfo_section_name
26966 = (dwarf_strict && dwarf_version < 5)
26967 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
26968 debug_macinfo_section = get_section (debug_macinfo_section_name,
26969 SECTION_DEBUG, NULL);
26971 else
26973 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
26974 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
26975 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
26976 SECTION_DEBUG | SECTION_EXCLUDE,
26977 NULL);
26978 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
26979 SECTION_DEBUG, NULL);
26980 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
26981 SECTION_DEBUG, NULL);
26982 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
26983 SECTION_DEBUG, NULL);
26984 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
26985 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
26987 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
26988 the main .o, but the skeleton_line goes into the split off dwo. */
26989 debug_skeleton_line_section
26990 = get_section (DEBUG_DWO_LINE_SECTION,
26991 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
26992 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
26993 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
26994 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
26995 SECTION_DEBUG | SECTION_EXCLUDE,
26996 NULL);
26997 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
26998 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
26999 debug_loc_section = get_section (dwarf_version >= 5
27000 ? DEBUG_DWO_LOCLISTS_SECTION
27001 : DEBUG_DWO_LOC_SECTION,
27002 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27003 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27004 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27005 debug_macinfo_section_name
27006 = (dwarf_strict && dwarf_version < 5)
27007 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27008 debug_macinfo_section = get_section (debug_macinfo_section_name,
27009 SECTION_DEBUG | SECTION_EXCLUDE,
27010 NULL);
27012 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27013 SECTION_DEBUG, NULL);
27014 debug_line_section = get_section (DEBUG_LINE_SECTION,
27015 SECTION_DEBUG, NULL);
27016 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27017 SECTION_DEBUG, NULL);
27018 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27019 SECTION_DEBUG, NULL);
27020 debug_str_section = get_section (DEBUG_STR_SECTION,
27021 DEBUG_STR_SECTION_FLAGS, NULL);
27022 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27023 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27024 DEBUG_STR_SECTION_FLAGS, NULL);
27026 debug_ranges_section = get_section (dwarf_version >= 5
27027 ? DEBUG_RNGLISTS_SECTION
27028 : DEBUG_RANGES_SECTION,
27029 SECTION_DEBUG, NULL);
27030 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27031 SECTION_DEBUG, NULL);
27033 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27034 DEBUG_ABBREV_SECTION_LABEL, 0);
27035 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27036 DEBUG_INFO_SECTION_LABEL, 0);
27037 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27038 DEBUG_LINE_SECTION_LABEL, 0);
27039 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27040 DEBUG_RANGES_SECTION_LABEL, 0);
27041 if (dwarf_version >= 5 && dwarf_split_debug_info)
27042 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27043 DEBUG_RANGES_SECTION_LABEL, 1);
27044 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27045 DEBUG_ADDR_SECTION_LABEL, 0);
27046 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27047 (dwarf_strict && dwarf_version < 5)
27048 ? DEBUG_MACINFO_SECTION_LABEL
27049 : DEBUG_MACRO_SECTION_LABEL, 0);
27050 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27053 /* Set up for Dwarf output at the start of compilation. */
27055 static void
27056 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27058 /* This option is currently broken, see (PR53118 and PR46102). */
27059 if (flag_eliminate_dwarf2_dups
27060 && strstr (lang_hooks.name, "C++"))
27062 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27063 flag_eliminate_dwarf2_dups = 0;
27066 /* Allocate the file_table. */
27067 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27069 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27070 /* Allocate the decl_die_table. */
27071 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27073 /* Allocate the decl_loc_table. */
27074 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27076 /* Allocate the cached_dw_loc_list_table. */
27077 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27079 /* Allocate the initial hunk of the decl_scope_table. */
27080 vec_alloc (decl_scope_table, 256);
27082 /* Allocate the initial hunk of the abbrev_die_table. */
27083 vec_alloc (abbrev_die_table, 256);
27084 /* Zero-th entry is allocated, but unused. */
27085 abbrev_die_table->quick_push (NULL);
27087 /* Allocate the dwarf_proc_stack_usage_map. */
27088 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27090 /* Allocate the pubtypes and pubnames vectors. */
27091 vec_alloc (pubname_table, 32);
27092 vec_alloc (pubtype_table, 32);
27094 vec_alloc (incomplete_types, 64);
27096 vec_alloc (used_rtx_array, 32);
27098 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27099 vec_alloc (macinfo_table, 64);
27100 #endif
27102 /* If front-ends already registered a main translation unit but we were not
27103 ready to perform the association, do this now. */
27104 if (main_translation_unit != NULL_TREE)
27105 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27108 /* Called before compile () starts outputtting functions, variables
27109 and toplevel asms into assembly. */
27111 static void
27112 dwarf2out_assembly_start (void)
27114 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27115 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27116 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27117 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27118 COLD_TEXT_SECTION_LABEL, 0);
27119 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27121 switch_to_section (text_section);
27122 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27123 #endif
27125 /* Make sure the line number table for .text always exists. */
27126 text_section_line_info = new_line_info_table ();
27127 text_section_line_info->end_label = text_end_label;
27129 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27130 cur_line_info_table = text_section_line_info;
27131 #endif
27133 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27134 && dwarf2out_do_cfi_asm ()
27135 && (!(flag_unwind_tables || flag_exceptions)
27136 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27137 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27140 /* A helper function for dwarf2out_finish called through
27141 htab_traverse. Assign a string its index. All strings must be
27142 collected into the table by the time index_string is called,
27143 because the indexing code relies on htab_traverse to traverse nodes
27144 in the same order for each run. */
27147 index_string (indirect_string_node **h, unsigned int *index)
27149 indirect_string_node *node = *h;
27151 find_string_form (node);
27152 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27154 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27155 node->index = *index;
27156 *index += 1;
27158 return 1;
27161 /* A helper function for output_indirect_strings called through
27162 htab_traverse. Output the offset to a string and update the
27163 current offset. */
27166 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27168 indirect_string_node *node = *h;
27170 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27172 /* Assert that this node has been assigned an index. */
27173 gcc_assert (node->index != NO_INDEX_ASSIGNED
27174 && node->index != NOT_INDEXED);
27175 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27176 "indexed string 0x%x: %s", node->index, node->str);
27177 *offset += strlen (node->str) + 1;
27179 return 1;
27182 /* A helper function for dwarf2out_finish called through
27183 htab_traverse. Output the indexed string. */
27186 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27188 struct indirect_string_node *node = *h;
27190 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27192 /* Assert that the strings are output in the same order as their
27193 indexes were assigned. */
27194 gcc_assert (*cur_idx == node->index);
27195 assemble_string (node->str, strlen (node->str) + 1);
27196 *cur_idx += 1;
27198 return 1;
27201 /* A helper function for dwarf2out_finish called through
27202 htab_traverse. Emit one queued .debug_str string. */
27205 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27207 struct indirect_string_node *node = *h;
27209 node->form = find_string_form (node);
27210 if (node->form == form && node->refcount > 0)
27212 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27213 assemble_string (node->str, strlen (node->str) + 1);
27216 return 1;
27219 /* Output the indexed string table. */
27221 static void
27222 output_indirect_strings (void)
27224 switch_to_section (debug_str_section);
27225 if (!dwarf_split_debug_info)
27226 debug_str_hash->traverse<enum dwarf_form,
27227 output_indirect_string> (DW_FORM_strp);
27228 else
27230 unsigned int offset = 0;
27231 unsigned int cur_idx = 0;
27233 skeleton_debug_str_hash->traverse<enum dwarf_form,
27234 output_indirect_string> (DW_FORM_strp);
27236 switch_to_section (debug_str_offsets_section);
27237 debug_str_hash->traverse_noresize
27238 <unsigned int *, output_index_string_offset> (&offset);
27239 switch_to_section (debug_str_dwo_section);
27240 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27241 (&cur_idx);
27245 /* Callback for htab_traverse to assign an index to an entry in the
27246 table, and to write that entry to the .debug_addr section. */
27249 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27251 addr_table_entry *entry = *slot;
27253 if (entry->refcount == 0)
27255 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27256 || entry->index == NOT_INDEXED);
27257 return 1;
27260 gcc_assert (entry->index == *cur_index);
27261 (*cur_index)++;
27263 switch (entry->kind)
27265 case ate_kind_rtx:
27266 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27267 "0x%x", entry->index);
27268 break;
27269 case ate_kind_rtx_dtprel:
27270 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27271 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27272 DWARF2_ADDR_SIZE,
27273 entry->addr.rtl);
27274 fputc ('\n', asm_out_file);
27275 break;
27276 case ate_kind_label:
27277 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27278 "0x%x", entry->index);
27279 break;
27280 default:
27281 gcc_unreachable ();
27283 return 1;
27286 /* Produce the .debug_addr section. */
27288 static void
27289 output_addr_table (void)
27291 unsigned int index = 0;
27292 if (addr_index_table == NULL || addr_index_table->size () == 0)
27293 return;
27295 switch_to_section (debug_addr_section);
27296 addr_index_table
27297 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27300 #if ENABLE_ASSERT_CHECKING
27301 /* Verify that all marks are clear. */
27303 static void
27304 verify_marks_clear (dw_die_ref die)
27306 dw_die_ref c;
27308 gcc_assert (! die->die_mark);
27309 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27311 #endif /* ENABLE_ASSERT_CHECKING */
27313 /* Clear the marks for a die and its children.
27314 Be cool if the mark isn't set. */
27316 static void
27317 prune_unmark_dies (dw_die_ref die)
27319 dw_die_ref c;
27321 if (die->die_mark)
27322 die->die_mark = 0;
27323 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27326 /* Given LOC that is referenced by a DIE we're marking as used, find all
27327 referenced DWARF procedures it references and mark them as used. */
27329 static void
27330 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27332 for (; loc != NULL; loc = loc->dw_loc_next)
27333 switch (loc->dw_loc_opc)
27335 case DW_OP_implicit_pointer:
27336 case DW_OP_convert:
27337 case DW_OP_reinterpret:
27338 case DW_OP_GNU_implicit_pointer:
27339 case DW_OP_GNU_convert:
27340 case DW_OP_GNU_reinterpret:
27341 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27342 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27343 break;
27344 case DW_OP_call2:
27345 case DW_OP_call4:
27346 case DW_OP_call_ref:
27347 case DW_OP_const_type:
27348 case DW_OP_GNU_const_type:
27349 case DW_OP_GNU_parameter_ref:
27350 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27351 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27352 break;
27353 case DW_OP_regval_type:
27354 case DW_OP_deref_type:
27355 case DW_OP_GNU_regval_type:
27356 case DW_OP_GNU_deref_type:
27357 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27358 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27359 break;
27360 case DW_OP_entry_value:
27361 case DW_OP_GNU_entry_value:
27362 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27363 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27364 break;
27365 default:
27366 break;
27370 /* Given DIE that we're marking as used, find any other dies
27371 it references as attributes and mark them as used. */
27373 static void
27374 prune_unused_types_walk_attribs (dw_die_ref die)
27376 dw_attr_node *a;
27377 unsigned ix;
27379 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27381 switch (AT_class (a))
27383 /* Make sure DWARF procedures referenced by location descriptions will
27384 get emitted. */
27385 case dw_val_class_loc:
27386 prune_unused_types_walk_loc_descr (AT_loc (a));
27387 break;
27388 case dw_val_class_loc_list:
27389 for (dw_loc_list_ref list = AT_loc_list (a);
27390 list != NULL;
27391 list = list->dw_loc_next)
27392 prune_unused_types_walk_loc_descr (list->expr);
27393 break;
27395 case dw_val_class_die_ref:
27396 /* A reference to another DIE.
27397 Make sure that it will get emitted.
27398 If it was broken out into a comdat group, don't follow it. */
27399 if (! AT_ref (a)->comdat_type_p
27400 || a->dw_attr == DW_AT_specification)
27401 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27402 break;
27404 case dw_val_class_str:
27405 /* Set the string's refcount to 0 so that prune_unused_types_mark
27406 accounts properly for it. */
27407 a->dw_attr_val.v.val_str->refcount = 0;
27408 break;
27410 default:
27411 break;
27416 /* Mark the generic parameters and arguments children DIEs of DIE. */
27418 static void
27419 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27421 dw_die_ref c;
27423 if (die == NULL || die->die_child == NULL)
27424 return;
27425 c = die->die_child;
27428 if (is_template_parameter (c))
27429 prune_unused_types_mark (c, 1);
27430 c = c->die_sib;
27431 } while (c && c != die->die_child);
27434 /* Mark DIE as being used. If DOKIDS is true, then walk down
27435 to DIE's children. */
27437 static void
27438 prune_unused_types_mark (dw_die_ref die, int dokids)
27440 dw_die_ref c;
27442 if (die->die_mark == 0)
27444 /* We haven't done this node yet. Mark it as used. */
27445 die->die_mark = 1;
27446 /* If this is the DIE of a generic type instantiation,
27447 mark the children DIEs that describe its generic parms and
27448 args. */
27449 prune_unused_types_mark_generic_parms_dies (die);
27451 /* We also have to mark its parents as used.
27452 (But we don't want to mark our parent's kids due to this,
27453 unless it is a class.) */
27454 if (die->die_parent)
27455 prune_unused_types_mark (die->die_parent,
27456 class_scope_p (die->die_parent));
27458 /* Mark any referenced nodes. */
27459 prune_unused_types_walk_attribs (die);
27461 /* If this node is a specification,
27462 also mark the definition, if it exists. */
27463 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27464 prune_unused_types_mark (die->die_definition, 1);
27467 if (dokids && die->die_mark != 2)
27469 /* We need to walk the children, but haven't done so yet.
27470 Remember that we've walked the kids. */
27471 die->die_mark = 2;
27473 /* If this is an array type, we need to make sure our
27474 kids get marked, even if they're types. If we're
27475 breaking out types into comdat sections, do this
27476 for all type definitions. */
27477 if (die->die_tag == DW_TAG_array_type
27478 || (use_debug_types
27479 && is_type_die (die) && ! is_declaration_die (die)))
27480 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27481 else
27482 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27486 /* For local classes, look if any static member functions were emitted
27487 and if so, mark them. */
27489 static void
27490 prune_unused_types_walk_local_classes (dw_die_ref die)
27492 dw_die_ref c;
27494 if (die->die_mark == 2)
27495 return;
27497 switch (die->die_tag)
27499 case DW_TAG_structure_type:
27500 case DW_TAG_union_type:
27501 case DW_TAG_class_type:
27502 break;
27504 case DW_TAG_subprogram:
27505 if (!get_AT_flag (die, DW_AT_declaration)
27506 || die->die_definition != NULL)
27507 prune_unused_types_mark (die, 1);
27508 return;
27510 default:
27511 return;
27514 /* Mark children. */
27515 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27518 /* Walk the tree DIE and mark types that we actually use. */
27520 static void
27521 prune_unused_types_walk (dw_die_ref die)
27523 dw_die_ref c;
27525 /* Don't do anything if this node is already marked and
27526 children have been marked as well. */
27527 if (die->die_mark == 2)
27528 return;
27530 switch (die->die_tag)
27532 case DW_TAG_structure_type:
27533 case DW_TAG_union_type:
27534 case DW_TAG_class_type:
27535 if (die->die_perennial_p)
27536 break;
27538 for (c = die->die_parent; c; c = c->die_parent)
27539 if (c->die_tag == DW_TAG_subprogram)
27540 break;
27542 /* Finding used static member functions inside of classes
27543 is needed just for local classes, because for other classes
27544 static member function DIEs with DW_AT_specification
27545 are emitted outside of the DW_TAG_*_type. If we ever change
27546 it, we'd need to call this even for non-local classes. */
27547 if (c)
27548 prune_unused_types_walk_local_classes (die);
27550 /* It's a type node --- don't mark it. */
27551 return;
27553 case DW_TAG_const_type:
27554 case DW_TAG_packed_type:
27555 case DW_TAG_pointer_type:
27556 case DW_TAG_reference_type:
27557 case DW_TAG_rvalue_reference_type:
27558 case DW_TAG_volatile_type:
27559 case DW_TAG_typedef:
27560 case DW_TAG_array_type:
27561 case DW_TAG_interface_type:
27562 case DW_TAG_friend:
27563 case DW_TAG_enumeration_type:
27564 case DW_TAG_subroutine_type:
27565 case DW_TAG_string_type:
27566 case DW_TAG_set_type:
27567 case DW_TAG_subrange_type:
27568 case DW_TAG_ptr_to_member_type:
27569 case DW_TAG_file_type:
27570 /* Type nodes are useful only when other DIEs reference them --- don't
27571 mark them. */
27572 /* FALLTHROUGH */
27574 case DW_TAG_dwarf_procedure:
27575 /* Likewise for DWARF procedures. */
27577 if (die->die_perennial_p)
27578 break;
27580 return;
27582 default:
27583 /* Mark everything else. */
27584 break;
27587 if (die->die_mark == 0)
27589 die->die_mark = 1;
27591 /* Now, mark any dies referenced from here. */
27592 prune_unused_types_walk_attribs (die);
27595 die->die_mark = 2;
27597 /* Mark children. */
27598 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27601 /* Increment the string counts on strings referred to from DIE's
27602 attributes. */
27604 static void
27605 prune_unused_types_update_strings (dw_die_ref die)
27607 dw_attr_node *a;
27608 unsigned ix;
27610 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27611 if (AT_class (a) == dw_val_class_str)
27613 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27614 s->refcount++;
27615 /* Avoid unnecessarily putting strings that are used less than
27616 twice in the hash table. */
27617 if (s->refcount
27618 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27620 indirect_string_node **slot
27621 = debug_str_hash->find_slot_with_hash (s->str,
27622 htab_hash_string (s->str),
27623 INSERT);
27624 gcc_assert (*slot == NULL);
27625 *slot = s;
27630 /* Mark DIE and its children as removed. */
27632 static void
27633 mark_removed (dw_die_ref die)
27635 dw_die_ref c;
27636 die->removed = true;
27637 FOR_EACH_CHILD (die, c, mark_removed (c));
27640 /* Remove from the tree DIE any dies that aren't marked. */
27642 static void
27643 prune_unused_types_prune (dw_die_ref die)
27645 dw_die_ref c;
27647 gcc_assert (die->die_mark);
27648 prune_unused_types_update_strings (die);
27650 if (! die->die_child)
27651 return;
27653 c = die->die_child;
27654 do {
27655 dw_die_ref prev = c, next;
27656 for (c = c->die_sib; ! c->die_mark; c = next)
27657 if (c == die->die_child)
27659 /* No marked children between 'prev' and the end of the list. */
27660 if (prev == c)
27661 /* No marked children at all. */
27662 die->die_child = NULL;
27663 else
27665 prev->die_sib = c->die_sib;
27666 die->die_child = prev;
27668 c->die_sib = NULL;
27669 mark_removed (c);
27670 return;
27672 else
27674 next = c->die_sib;
27675 c->die_sib = NULL;
27676 mark_removed (c);
27679 if (c != prev->die_sib)
27680 prev->die_sib = c;
27681 prune_unused_types_prune (c);
27682 } while (c != die->die_child);
27685 /* Remove dies representing declarations that we never use. */
27687 static void
27688 prune_unused_types (void)
27690 unsigned int i;
27691 limbo_die_node *node;
27692 comdat_type_node *ctnode;
27693 pubname_entry *pub;
27694 dw_die_ref base_type;
27696 #if ENABLE_ASSERT_CHECKING
27697 /* All the marks should already be clear. */
27698 verify_marks_clear (comp_unit_die ());
27699 for (node = limbo_die_list; node; node = node->next)
27700 verify_marks_clear (node->die);
27701 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27702 verify_marks_clear (ctnode->root_die);
27703 #endif /* ENABLE_ASSERT_CHECKING */
27705 /* Mark types that are used in global variables. */
27706 premark_types_used_by_global_vars ();
27708 /* Set the mark on nodes that are actually used. */
27709 prune_unused_types_walk (comp_unit_die ());
27710 for (node = limbo_die_list; node; node = node->next)
27711 prune_unused_types_walk (node->die);
27712 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27714 prune_unused_types_walk (ctnode->root_die);
27715 prune_unused_types_mark (ctnode->type_die, 1);
27718 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27719 are unusual in that they are pubnames that are the children of pubtypes.
27720 They should only be marked via their parent DW_TAG_enumeration_type die,
27721 not as roots in themselves. */
27722 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27723 if (pub->die->die_tag != DW_TAG_enumerator)
27724 prune_unused_types_mark (pub->die, 1);
27725 for (i = 0; base_types.iterate (i, &base_type); i++)
27726 prune_unused_types_mark (base_type, 1);
27728 if (debug_str_hash)
27729 debug_str_hash->empty ();
27730 if (skeleton_debug_str_hash)
27731 skeleton_debug_str_hash->empty ();
27732 prune_unused_types_prune (comp_unit_die ());
27733 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27735 node = *pnode;
27736 if (!node->die->die_mark)
27737 *pnode = node->next;
27738 else
27740 prune_unused_types_prune (node->die);
27741 pnode = &node->next;
27744 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27745 prune_unused_types_prune (ctnode->root_die);
27747 /* Leave the marks clear. */
27748 prune_unmark_dies (comp_unit_die ());
27749 for (node = limbo_die_list; node; node = node->next)
27750 prune_unmark_dies (node->die);
27751 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27752 prune_unmark_dies (ctnode->root_die);
27755 /* Helpers to manipulate hash table of comdat type units. */
27757 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27759 static inline hashval_t hash (const comdat_type_node *);
27760 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27763 inline hashval_t
27764 comdat_type_hasher::hash (const comdat_type_node *type_node)
27766 hashval_t h;
27767 memcpy (&h, type_node->signature, sizeof (h));
27768 return h;
27771 inline bool
27772 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27773 const comdat_type_node *type_node_2)
27775 return (! memcmp (type_node_1->signature, type_node_2->signature,
27776 DWARF_TYPE_SIGNATURE_SIZE));
27779 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
27780 to the location it would have been added, should we know its
27781 DECL_ASSEMBLER_NAME when we added other attributes. This will
27782 probably improve compactness of debug info, removing equivalent
27783 abbrevs, and hide any differences caused by deferring the
27784 computation of the assembler name, triggered by e.g. PCH. */
27786 static inline void
27787 move_linkage_attr (dw_die_ref die)
27789 unsigned ix = vec_safe_length (die->die_attr);
27790 dw_attr_node linkage = (*die->die_attr)[ix - 1];
27792 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
27793 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
27795 while (--ix > 0)
27797 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
27799 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
27800 break;
27803 if (ix != vec_safe_length (die->die_attr) - 1)
27805 die->die_attr->pop ();
27806 die->die_attr->quick_insert (ix, linkage);
27810 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
27811 referenced from typed stack ops and count how often they are used. */
27813 static void
27814 mark_base_types (dw_loc_descr_ref loc)
27816 dw_die_ref base_type = NULL;
27818 for (; loc; loc = loc->dw_loc_next)
27820 switch (loc->dw_loc_opc)
27822 case DW_OP_regval_type:
27823 case DW_OP_deref_type:
27824 case DW_OP_GNU_regval_type:
27825 case DW_OP_GNU_deref_type:
27826 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
27827 break;
27828 case DW_OP_convert:
27829 case DW_OP_reinterpret:
27830 case DW_OP_GNU_convert:
27831 case DW_OP_GNU_reinterpret:
27832 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
27833 continue;
27834 /* FALLTHRU */
27835 case DW_OP_const_type:
27836 case DW_OP_GNU_const_type:
27837 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
27838 break;
27839 case DW_OP_entry_value:
27840 case DW_OP_GNU_entry_value:
27841 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
27842 continue;
27843 default:
27844 continue;
27846 gcc_assert (base_type->die_parent == comp_unit_die ());
27847 if (base_type->die_mark)
27848 base_type->die_mark++;
27849 else
27851 base_types.safe_push (base_type);
27852 base_type->die_mark = 1;
27857 /* Comparison function for sorting marked base types. */
27859 static int
27860 base_type_cmp (const void *x, const void *y)
27862 dw_die_ref dx = *(const dw_die_ref *) x;
27863 dw_die_ref dy = *(const dw_die_ref *) y;
27864 unsigned int byte_size1, byte_size2;
27865 unsigned int encoding1, encoding2;
27866 if (dx->die_mark > dy->die_mark)
27867 return -1;
27868 if (dx->die_mark < dy->die_mark)
27869 return 1;
27870 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
27871 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
27872 if (byte_size1 < byte_size2)
27873 return 1;
27874 if (byte_size1 > byte_size2)
27875 return -1;
27876 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
27877 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
27878 if (encoding1 < encoding2)
27879 return 1;
27880 if (encoding1 > encoding2)
27881 return -1;
27882 return 0;
27885 /* Move base types marked by mark_base_types as early as possible
27886 in the CU, sorted by decreasing usage count both to make the
27887 uleb128 references as small as possible and to make sure they
27888 will have die_offset already computed by calc_die_sizes when
27889 sizes of typed stack loc ops is computed. */
27891 static void
27892 move_marked_base_types (void)
27894 unsigned int i;
27895 dw_die_ref base_type, die, c;
27897 if (base_types.is_empty ())
27898 return;
27900 /* Sort by decreasing usage count, they will be added again in that
27901 order later on. */
27902 base_types.qsort (base_type_cmp);
27903 die = comp_unit_die ();
27904 c = die->die_child;
27907 dw_die_ref prev = c;
27908 c = c->die_sib;
27909 while (c->die_mark)
27911 remove_child_with_prev (c, prev);
27912 /* As base types got marked, there must be at least
27913 one node other than DW_TAG_base_type. */
27914 gcc_assert (die->die_child != NULL);
27915 c = prev->die_sib;
27918 while (c != die->die_child);
27919 gcc_assert (die->die_child);
27920 c = die->die_child;
27921 for (i = 0; base_types.iterate (i, &base_type); i++)
27923 base_type->die_mark = 0;
27924 base_type->die_sib = c->die_sib;
27925 c->die_sib = base_type;
27926 c = base_type;
27930 /* Helper function for resolve_addr, attempt to resolve
27931 one CONST_STRING, return true if successful. Similarly verify that
27932 SYMBOL_REFs refer to variables emitted in the current CU. */
27934 static bool
27935 resolve_one_addr (rtx *addr)
27937 rtx rtl = *addr;
27939 if (GET_CODE (rtl) == CONST_STRING)
27941 size_t len = strlen (XSTR (rtl, 0)) + 1;
27942 tree t = build_string (len, XSTR (rtl, 0));
27943 tree tlen = size_int (len - 1);
27944 TREE_TYPE (t)
27945 = build_array_type (char_type_node, build_index_type (tlen));
27946 rtl = lookup_constant_def (t);
27947 if (!rtl || !MEM_P (rtl))
27948 return false;
27949 rtl = XEXP (rtl, 0);
27950 if (GET_CODE (rtl) == SYMBOL_REF
27951 && SYMBOL_REF_DECL (rtl)
27952 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
27953 return false;
27954 vec_safe_push (used_rtx_array, rtl);
27955 *addr = rtl;
27956 return true;
27959 if (GET_CODE (rtl) == SYMBOL_REF
27960 && SYMBOL_REF_DECL (rtl))
27962 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
27964 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
27965 return false;
27967 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
27968 return false;
27971 if (GET_CODE (rtl) == CONST)
27973 subrtx_ptr_iterator::array_type array;
27974 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
27975 if (!resolve_one_addr (*iter))
27976 return false;
27979 return true;
27982 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
27983 if possible, and create DW_TAG_dwarf_procedure that can be referenced
27984 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
27986 static rtx
27987 string_cst_pool_decl (tree t)
27989 rtx rtl = output_constant_def (t, 1);
27990 unsigned char *array;
27991 dw_loc_descr_ref l;
27992 tree decl;
27993 size_t len;
27994 dw_die_ref ref;
27996 if (!rtl || !MEM_P (rtl))
27997 return NULL_RTX;
27998 rtl = XEXP (rtl, 0);
27999 if (GET_CODE (rtl) != SYMBOL_REF
28000 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28001 return NULL_RTX;
28003 decl = SYMBOL_REF_DECL (rtl);
28004 if (!lookup_decl_die (decl))
28006 len = TREE_STRING_LENGTH (t);
28007 vec_safe_push (used_rtx_array, rtl);
28008 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28009 array = ggc_vec_alloc<unsigned char> (len);
28010 memcpy (array, TREE_STRING_POINTER (t), len);
28011 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28012 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28013 l->dw_loc_oprnd2.v.val_vec.length = len;
28014 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28015 l->dw_loc_oprnd2.v.val_vec.array = array;
28016 add_AT_loc (ref, DW_AT_location, l);
28017 equate_decl_number_to_die (decl, ref);
28019 return rtl;
28022 /* Helper function of resolve_addr_in_expr. LOC is
28023 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28024 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28025 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28026 with DW_OP_implicit_pointer if possible
28027 and return true, if unsuccessful, return false. */
28029 static bool
28030 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28032 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28033 HOST_WIDE_INT offset = 0;
28034 dw_die_ref ref = NULL;
28035 tree decl;
28037 if (GET_CODE (rtl) == CONST
28038 && GET_CODE (XEXP (rtl, 0)) == PLUS
28039 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28041 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28042 rtl = XEXP (XEXP (rtl, 0), 0);
28044 if (GET_CODE (rtl) == CONST_STRING)
28046 size_t len = strlen (XSTR (rtl, 0)) + 1;
28047 tree t = build_string (len, XSTR (rtl, 0));
28048 tree tlen = size_int (len - 1);
28050 TREE_TYPE (t)
28051 = build_array_type (char_type_node, build_index_type (tlen));
28052 rtl = string_cst_pool_decl (t);
28053 if (!rtl)
28054 return false;
28056 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28058 decl = SYMBOL_REF_DECL (rtl);
28059 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28061 ref = lookup_decl_die (decl);
28062 if (ref && (get_AT (ref, DW_AT_location)
28063 || get_AT (ref, DW_AT_const_value)))
28065 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28066 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28067 loc->dw_loc_oprnd1.val_entry = NULL;
28068 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28069 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28070 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28071 loc->dw_loc_oprnd2.v.val_int = offset;
28072 return true;
28076 return false;
28079 /* Helper function for resolve_addr, handle one location
28080 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28081 the location list couldn't be resolved. */
28083 static bool
28084 resolve_addr_in_expr (dw_loc_descr_ref loc)
28086 dw_loc_descr_ref keep = NULL;
28087 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28088 switch (loc->dw_loc_opc)
28090 case DW_OP_addr:
28091 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28093 if ((prev == NULL
28094 || prev->dw_loc_opc == DW_OP_piece
28095 || prev->dw_loc_opc == DW_OP_bit_piece)
28096 && loc->dw_loc_next
28097 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28098 && (!dwarf_strict || dwarf_version >= 5)
28099 && optimize_one_addr_into_implicit_ptr (loc))
28100 break;
28101 return false;
28103 break;
28104 case DW_OP_GNU_addr_index:
28105 case DW_OP_GNU_const_index:
28106 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28107 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28109 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28110 if (!resolve_one_addr (&rtl))
28111 return false;
28112 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28113 loc->dw_loc_oprnd1.val_entry
28114 = add_addr_table_entry (rtl, ate_kind_rtx);
28116 break;
28117 case DW_OP_const4u:
28118 case DW_OP_const8u:
28119 if (loc->dtprel
28120 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28121 return false;
28122 break;
28123 case DW_OP_plus_uconst:
28124 if (size_of_loc_descr (loc)
28125 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28127 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28129 dw_loc_descr_ref repl
28130 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28131 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28132 add_loc_descr (&repl, loc->dw_loc_next);
28133 *loc = *repl;
28135 break;
28136 case DW_OP_implicit_value:
28137 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28138 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28139 return false;
28140 break;
28141 case DW_OP_implicit_pointer:
28142 case DW_OP_GNU_implicit_pointer:
28143 case DW_OP_GNU_parameter_ref:
28144 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28146 dw_die_ref ref
28147 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28148 if (ref == NULL)
28149 return false;
28150 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28151 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28152 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28154 break;
28155 case DW_OP_const_type:
28156 case DW_OP_regval_type:
28157 case DW_OP_deref_type:
28158 case DW_OP_convert:
28159 case DW_OP_reinterpret:
28160 case DW_OP_GNU_const_type:
28161 case DW_OP_GNU_regval_type:
28162 case DW_OP_GNU_deref_type:
28163 case DW_OP_GNU_convert:
28164 case DW_OP_GNU_reinterpret:
28165 while (loc->dw_loc_next
28166 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28167 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28169 dw_die_ref base1, base2;
28170 unsigned enc1, enc2, size1, size2;
28171 if (loc->dw_loc_opc == DW_OP_regval_type
28172 || loc->dw_loc_opc == DW_OP_deref_type
28173 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28174 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28175 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28176 else if (loc->dw_loc_oprnd1.val_class
28177 == dw_val_class_unsigned_const)
28178 break;
28179 else
28180 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28181 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28182 == dw_val_class_unsigned_const)
28183 break;
28184 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28185 gcc_assert (base1->die_tag == DW_TAG_base_type
28186 && base2->die_tag == DW_TAG_base_type);
28187 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28188 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28189 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28190 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28191 if (size1 == size2
28192 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28193 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28194 && loc != keep)
28195 || enc1 == enc2))
28197 /* Optimize away next DW_OP_convert after
28198 adjusting LOC's base type die reference. */
28199 if (loc->dw_loc_opc == DW_OP_regval_type
28200 || loc->dw_loc_opc == DW_OP_deref_type
28201 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28202 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28203 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28204 else
28205 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28206 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28207 continue;
28209 /* Don't change integer DW_OP_convert after e.g. floating
28210 point typed stack entry. */
28211 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28212 keep = loc->dw_loc_next;
28213 break;
28215 break;
28216 default:
28217 break;
28219 return true;
28222 /* Helper function of resolve_addr. DIE had DW_AT_location of
28223 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28224 and DW_OP_addr couldn't be resolved. resolve_addr has already
28225 removed the DW_AT_location attribute. This function attempts to
28226 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28227 to it or DW_AT_const_value attribute, if possible. */
28229 static void
28230 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28232 if (!VAR_P (decl)
28233 || lookup_decl_die (decl) != die
28234 || DECL_EXTERNAL (decl)
28235 || !TREE_STATIC (decl)
28236 || DECL_INITIAL (decl) == NULL_TREE
28237 || DECL_P (DECL_INITIAL (decl))
28238 || get_AT (die, DW_AT_const_value))
28239 return;
28241 tree init = DECL_INITIAL (decl);
28242 HOST_WIDE_INT offset = 0;
28243 /* For variables that have been optimized away and thus
28244 don't have a memory location, see if we can emit
28245 DW_AT_const_value instead. */
28246 if (tree_add_const_value_attribute (die, init))
28247 return;
28248 if (dwarf_strict && dwarf_version < 5)
28249 return;
28250 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28251 and ADDR_EXPR refers to a decl that has DW_AT_location or
28252 DW_AT_const_value (but isn't addressable, otherwise
28253 resolving the original DW_OP_addr wouldn't fail), see if
28254 we can add DW_OP_implicit_pointer. */
28255 STRIP_NOPS (init);
28256 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28257 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28259 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28260 init = TREE_OPERAND (init, 0);
28261 STRIP_NOPS (init);
28263 if (TREE_CODE (init) != ADDR_EXPR)
28264 return;
28265 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28266 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28267 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28268 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28269 && TREE_OPERAND (init, 0) != decl))
28271 dw_die_ref ref;
28272 dw_loc_descr_ref l;
28274 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28276 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28277 if (!rtl)
28278 return;
28279 decl = SYMBOL_REF_DECL (rtl);
28281 else
28282 decl = TREE_OPERAND (init, 0);
28283 ref = lookup_decl_die (decl);
28284 if (ref == NULL
28285 || (!get_AT (ref, DW_AT_location)
28286 && !get_AT (ref, DW_AT_const_value)))
28287 return;
28288 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28289 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28290 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28291 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28292 add_AT_loc (die, DW_AT_location, l);
28296 /* Return NULL if l is a DWARF expression, or first op that is not
28297 valid DWARF expression. */
28299 static dw_loc_descr_ref
28300 non_dwarf_expression (dw_loc_descr_ref l)
28302 while (l)
28304 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28305 return l;
28306 switch (l->dw_loc_opc)
28308 case DW_OP_regx:
28309 case DW_OP_implicit_value:
28310 case DW_OP_stack_value:
28311 case DW_OP_implicit_pointer:
28312 case DW_OP_GNU_implicit_pointer:
28313 case DW_OP_GNU_parameter_ref:
28314 case DW_OP_piece:
28315 case DW_OP_bit_piece:
28316 return l;
28317 default:
28318 break;
28320 l = l->dw_loc_next;
28322 return NULL;
28325 /* Return adjusted copy of EXPR:
28326 If it is empty DWARF expression, return it.
28327 If it is valid non-empty DWARF expression,
28328 return copy of EXPR with copy of DEREF appended to it.
28329 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28330 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
28331 and no DEREF.
28332 If it is DWARF expression followed by DW_OP_stack_value, return
28333 copy of the DWARF expression without anything appended.
28334 Otherwise, return NULL. */
28336 static dw_loc_descr_ref
28337 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
28340 if (expr == NULL)
28341 return NULL;
28343 dw_loc_descr_ref l = non_dwarf_expression (expr);
28344 if (l && l->dw_loc_next)
28345 return NULL;
28347 if (l)
28349 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28350 deref = new_loc_descr ((enum dwarf_location_atom)
28351 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28352 0, 0);
28353 else
28354 switch (l->dw_loc_opc)
28356 case DW_OP_regx:
28357 deref = new_loc_descr (DW_OP_bregx,
28358 l->dw_loc_oprnd1.v.val_unsigned, 0);
28359 break;
28360 case DW_OP_stack_value:
28361 deref = NULL;
28362 break;
28363 default:
28364 return NULL;
28367 else
28368 deref = new_loc_descr (deref->dw_loc_opc,
28369 deref->dw_loc_oprnd1.v.val_int, 0);
28371 dw_loc_descr_ref ret = NULL, *p = &ret;
28372 while (expr != l)
28374 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28375 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28376 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28377 p = &(*p)->dw_loc_next;
28378 expr = expr->dw_loc_next;
28380 *p = deref;
28381 return ret;
28384 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
28385 or argument, adjust it if needed and return:
28386 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28387 attribute if present should be removed
28388 0 keep the attribute as is if the referenced var or argument has
28389 only DWARF expression that covers all ranges
28390 1 if the attribute has been successfully adjusted. */
28392 static int
28393 optimize_string_length (dw_attr_node *a)
28395 dw_loc_descr_ref l = AT_loc (a), lv;
28396 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
28397 dw_attr_node *av = get_AT (die, DW_AT_location);
28398 dw_loc_list_ref d;
28399 bool non_dwarf_expr = false;
28401 if (av == NULL)
28402 return -1;
28403 switch (AT_class (av))
28405 case dw_val_class_loc_list:
28406 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28407 if (d->expr && non_dwarf_expression (d->expr))
28408 non_dwarf_expr = true;
28409 break;
28410 case dw_val_class_loc:
28411 lv = AT_loc (av);
28412 if (lv == NULL)
28413 return -1;
28414 if (non_dwarf_expression (lv))
28415 non_dwarf_expr = true;
28416 break;
28417 default:
28418 return -1;
28421 /* If it is safe to keep DW_OP_call4 in, keep it. */
28422 if (!non_dwarf_expr
28423 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
28424 return 0;
28426 /* If not dereferencing the DW_OP_call4 afterwards, we can just
28427 copy over the DW_AT_location attribute from die to a. */
28428 if (l->dw_loc_next == NULL)
28430 a->dw_attr_val = av->dw_attr_val;
28431 return 1;
28434 dw_loc_list_ref list, *p;
28435 switch (AT_class (av))
28437 case dw_val_class_loc_list:
28438 p = &list;
28439 list = NULL;
28440 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28442 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
28443 if (lv)
28445 *p = new_loc_list (lv, d->begin, d->end, d->section);
28446 p = &(*p)->dw_loc_next;
28449 if (list == NULL)
28450 return -1;
28451 a->dw_attr_val.val_class = dw_val_class_loc_list;
28452 gen_llsym (list);
28453 *AT_loc_list_ptr (a) = list;
28454 return 1;
28455 case dw_val_class_loc:
28456 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
28457 if (lv == NULL)
28458 return -1;
28459 a->dw_attr_val.v.val_loc = lv;
28460 return 1;
28461 default:
28462 gcc_unreachable ();
28466 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28467 an address in .rodata section if the string literal is emitted there,
28468 or remove the containing location list or replace DW_AT_const_value
28469 with DW_AT_location and empty location expression, if it isn't found
28470 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28471 to something that has been emitted in the current CU. */
28473 static void
28474 resolve_addr (dw_die_ref die)
28476 dw_die_ref c;
28477 dw_attr_node *a;
28478 dw_loc_list_ref *curr, *start, loc;
28479 unsigned ix;
28480 bool remove_AT_byte_size = false;
28482 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28483 switch (AT_class (a))
28485 case dw_val_class_loc_list:
28486 start = curr = AT_loc_list_ptr (a);
28487 loc = *curr;
28488 gcc_assert (loc);
28489 /* The same list can be referenced more than once. See if we have
28490 already recorded the result from a previous pass. */
28491 if (loc->replaced)
28492 *curr = loc->dw_loc_next;
28493 else if (!loc->resolved_addr)
28495 /* As things stand, we do not expect or allow one die to
28496 reference a suffix of another die's location list chain.
28497 References must be identical or completely separate.
28498 There is therefore no need to cache the result of this
28499 pass on any list other than the first; doing so
28500 would lead to unnecessary writes. */
28501 while (*curr)
28503 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28504 if (!resolve_addr_in_expr ((*curr)->expr))
28506 dw_loc_list_ref next = (*curr)->dw_loc_next;
28507 dw_loc_descr_ref l = (*curr)->expr;
28509 if (next && (*curr)->ll_symbol)
28511 gcc_assert (!next->ll_symbol);
28512 next->ll_symbol = (*curr)->ll_symbol;
28514 if (dwarf_split_debug_info)
28515 remove_loc_list_addr_table_entries (l);
28516 *curr = next;
28518 else
28520 mark_base_types ((*curr)->expr);
28521 curr = &(*curr)->dw_loc_next;
28524 if (loc == *start)
28525 loc->resolved_addr = 1;
28526 else
28528 loc->replaced = 1;
28529 loc->dw_loc_next = *start;
28532 if (!*start)
28534 remove_AT (die, a->dw_attr);
28535 ix--;
28537 break;
28538 case dw_val_class_loc:
28540 dw_loc_descr_ref l = AT_loc (a);
28541 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
28542 DW_AT_string_length is only a rough approximation; unfortunately
28543 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
28544 needs a DWARF expression, while DW_AT_location of the referenced
28545 variable or argument might be any location description. */
28546 if (a->dw_attr == DW_AT_string_length
28547 && l
28548 && l->dw_loc_opc == DW_OP_call4
28549 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
28550 && (l->dw_loc_next == NULL
28551 || (l->dw_loc_next->dw_loc_next == NULL
28552 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
28553 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
28555 switch (optimize_string_length (a))
28557 case -1:
28558 remove_AT (die, a->dw_attr);
28559 ix--;
28560 /* If we drop DW_AT_string_length, we need to drop also
28561 DW_AT_{string_length_,}byte_size. */
28562 remove_AT_byte_size = true;
28563 continue;
28564 default:
28565 break;
28566 case 1:
28567 /* Even if we keep the optimized DW_AT_string_length,
28568 it might have changed AT_class, so process it again. */
28569 ix--;
28570 continue;
28573 /* For -gdwarf-2 don't attempt to optimize
28574 DW_AT_data_member_location containing
28575 DW_OP_plus_uconst - older consumers might
28576 rely on it being that op instead of a more complex,
28577 but shorter, location description. */
28578 if ((dwarf_version > 2
28579 || a->dw_attr != DW_AT_data_member_location
28580 || l == NULL
28581 || l->dw_loc_opc != DW_OP_plus_uconst
28582 || l->dw_loc_next != NULL)
28583 && !resolve_addr_in_expr (l))
28585 if (dwarf_split_debug_info)
28586 remove_loc_list_addr_table_entries (l);
28587 if (l != NULL
28588 && l->dw_loc_next == NULL
28589 && l->dw_loc_opc == DW_OP_addr
28590 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28591 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28592 && a->dw_attr == DW_AT_location)
28594 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28595 remove_AT (die, a->dw_attr);
28596 ix--;
28597 optimize_location_into_implicit_ptr (die, decl);
28598 break;
28600 remove_AT (die, a->dw_attr);
28601 ix--;
28603 else
28604 mark_base_types (l);
28606 break;
28607 case dw_val_class_addr:
28608 if (a->dw_attr == DW_AT_const_value
28609 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28611 if (AT_index (a) != NOT_INDEXED)
28612 remove_addr_table_entry (a->dw_attr_val.val_entry);
28613 remove_AT (die, a->dw_attr);
28614 ix--;
28616 if ((die->die_tag == DW_TAG_call_site
28617 && a->dw_attr == DW_AT_call_origin)
28618 || (die->die_tag == DW_TAG_GNU_call_site
28619 && a->dw_attr == DW_AT_abstract_origin))
28621 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28622 dw_die_ref tdie = lookup_decl_die (tdecl);
28623 dw_die_ref cdie;
28624 if (tdie == NULL
28625 && DECL_EXTERNAL (tdecl)
28626 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28627 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28629 /* Creating a full DIE for tdecl is overly expensive and
28630 at this point even wrong when in the LTO phase
28631 as it can end up generating new type DIEs we didn't
28632 output and thus optimize_external_refs will crash. */
28633 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28634 add_AT_flag (tdie, DW_AT_external, 1);
28635 add_AT_flag (tdie, DW_AT_declaration, 1);
28636 add_linkage_attr (tdie, tdecl);
28637 add_name_and_src_coords_attributes (tdie, tdecl);
28638 equate_decl_number_to_die (tdecl, tdie);
28640 if (tdie)
28642 a->dw_attr_val.val_class = dw_val_class_die_ref;
28643 a->dw_attr_val.v.val_die_ref.die = tdie;
28644 a->dw_attr_val.v.val_die_ref.external = 0;
28646 else
28648 if (AT_index (a) != NOT_INDEXED)
28649 remove_addr_table_entry (a->dw_attr_val.val_entry);
28650 remove_AT (die, a->dw_attr);
28651 ix--;
28654 break;
28655 default:
28656 break;
28659 if (remove_AT_byte_size)
28660 remove_AT (die, dwarf_version >= 5
28661 ? DW_AT_string_length_byte_size
28662 : DW_AT_byte_size);
28664 FOR_EACH_CHILD (die, c, resolve_addr (c));
28667 /* Helper routines for optimize_location_lists.
28668 This pass tries to share identical local lists in .debug_loc
28669 section. */
28671 /* Iteratively hash operands of LOC opcode into HSTATE. */
28673 static void
28674 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28676 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28677 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28679 switch (loc->dw_loc_opc)
28681 case DW_OP_const4u:
28682 case DW_OP_const8u:
28683 if (loc->dtprel)
28684 goto hash_addr;
28685 /* FALLTHRU */
28686 case DW_OP_const1u:
28687 case DW_OP_const1s:
28688 case DW_OP_const2u:
28689 case DW_OP_const2s:
28690 case DW_OP_const4s:
28691 case DW_OP_const8s:
28692 case DW_OP_constu:
28693 case DW_OP_consts:
28694 case DW_OP_pick:
28695 case DW_OP_plus_uconst:
28696 case DW_OP_breg0:
28697 case DW_OP_breg1:
28698 case DW_OP_breg2:
28699 case DW_OP_breg3:
28700 case DW_OP_breg4:
28701 case DW_OP_breg5:
28702 case DW_OP_breg6:
28703 case DW_OP_breg7:
28704 case DW_OP_breg8:
28705 case DW_OP_breg9:
28706 case DW_OP_breg10:
28707 case DW_OP_breg11:
28708 case DW_OP_breg12:
28709 case DW_OP_breg13:
28710 case DW_OP_breg14:
28711 case DW_OP_breg15:
28712 case DW_OP_breg16:
28713 case DW_OP_breg17:
28714 case DW_OP_breg18:
28715 case DW_OP_breg19:
28716 case DW_OP_breg20:
28717 case DW_OP_breg21:
28718 case DW_OP_breg22:
28719 case DW_OP_breg23:
28720 case DW_OP_breg24:
28721 case DW_OP_breg25:
28722 case DW_OP_breg26:
28723 case DW_OP_breg27:
28724 case DW_OP_breg28:
28725 case DW_OP_breg29:
28726 case DW_OP_breg30:
28727 case DW_OP_breg31:
28728 case DW_OP_regx:
28729 case DW_OP_fbreg:
28730 case DW_OP_piece:
28731 case DW_OP_deref_size:
28732 case DW_OP_xderef_size:
28733 hstate.add_object (val1->v.val_int);
28734 break;
28735 case DW_OP_skip:
28736 case DW_OP_bra:
28738 int offset;
28740 gcc_assert (val1->val_class == dw_val_class_loc);
28741 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
28742 hstate.add_object (offset);
28744 break;
28745 case DW_OP_implicit_value:
28746 hstate.add_object (val1->v.val_unsigned);
28747 switch (val2->val_class)
28749 case dw_val_class_const:
28750 hstate.add_object (val2->v.val_int);
28751 break;
28752 case dw_val_class_vec:
28754 unsigned int elt_size = val2->v.val_vec.elt_size;
28755 unsigned int len = val2->v.val_vec.length;
28757 hstate.add_int (elt_size);
28758 hstate.add_int (len);
28759 hstate.add (val2->v.val_vec.array, len * elt_size);
28761 break;
28762 case dw_val_class_const_double:
28763 hstate.add_object (val2->v.val_double.low);
28764 hstate.add_object (val2->v.val_double.high);
28765 break;
28766 case dw_val_class_wide_int:
28767 hstate.add (val2->v.val_wide->get_val (),
28768 get_full_len (*val2->v.val_wide)
28769 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28770 break;
28771 case dw_val_class_addr:
28772 inchash::add_rtx (val2->v.val_addr, hstate);
28773 break;
28774 default:
28775 gcc_unreachable ();
28777 break;
28778 case DW_OP_bregx:
28779 case DW_OP_bit_piece:
28780 hstate.add_object (val1->v.val_int);
28781 hstate.add_object (val2->v.val_int);
28782 break;
28783 case DW_OP_addr:
28784 hash_addr:
28785 if (loc->dtprel)
28787 unsigned char dtprel = 0xd1;
28788 hstate.add_object (dtprel);
28790 inchash::add_rtx (val1->v.val_addr, hstate);
28791 break;
28792 case DW_OP_GNU_addr_index:
28793 case DW_OP_GNU_const_index:
28795 if (loc->dtprel)
28797 unsigned char dtprel = 0xd1;
28798 hstate.add_object (dtprel);
28800 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
28802 break;
28803 case DW_OP_implicit_pointer:
28804 case DW_OP_GNU_implicit_pointer:
28805 hstate.add_int (val2->v.val_int);
28806 break;
28807 case DW_OP_entry_value:
28808 case DW_OP_GNU_entry_value:
28809 hstate.add_object (val1->v.val_loc);
28810 break;
28811 case DW_OP_regval_type:
28812 case DW_OP_deref_type:
28813 case DW_OP_GNU_regval_type:
28814 case DW_OP_GNU_deref_type:
28816 unsigned int byte_size
28817 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
28818 unsigned int encoding
28819 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
28820 hstate.add_object (val1->v.val_int);
28821 hstate.add_object (byte_size);
28822 hstate.add_object (encoding);
28824 break;
28825 case DW_OP_convert:
28826 case DW_OP_reinterpret:
28827 case DW_OP_GNU_convert:
28828 case DW_OP_GNU_reinterpret:
28829 if (val1->val_class == dw_val_class_unsigned_const)
28831 hstate.add_object (val1->v.val_unsigned);
28832 break;
28834 /* FALLTHRU */
28835 case DW_OP_const_type:
28836 case DW_OP_GNU_const_type:
28838 unsigned int byte_size
28839 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
28840 unsigned int encoding
28841 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
28842 hstate.add_object (byte_size);
28843 hstate.add_object (encoding);
28844 if (loc->dw_loc_opc != DW_OP_const_type
28845 && loc->dw_loc_opc != DW_OP_GNU_const_type)
28846 break;
28847 hstate.add_object (val2->val_class);
28848 switch (val2->val_class)
28850 case dw_val_class_const:
28851 hstate.add_object (val2->v.val_int);
28852 break;
28853 case dw_val_class_vec:
28855 unsigned int elt_size = val2->v.val_vec.elt_size;
28856 unsigned int len = val2->v.val_vec.length;
28858 hstate.add_object (elt_size);
28859 hstate.add_object (len);
28860 hstate.add (val2->v.val_vec.array, len * elt_size);
28862 break;
28863 case dw_val_class_const_double:
28864 hstate.add_object (val2->v.val_double.low);
28865 hstate.add_object (val2->v.val_double.high);
28866 break;
28867 case dw_val_class_wide_int:
28868 hstate.add (val2->v.val_wide->get_val (),
28869 get_full_len (*val2->v.val_wide)
28870 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28871 break;
28872 default:
28873 gcc_unreachable ();
28876 break;
28878 default:
28879 /* Other codes have no operands. */
28880 break;
28884 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
28886 static inline void
28887 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
28889 dw_loc_descr_ref l;
28890 bool sizes_computed = false;
28891 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
28892 size_of_locs (loc);
28894 for (l = loc; l != NULL; l = l->dw_loc_next)
28896 enum dwarf_location_atom opc = l->dw_loc_opc;
28897 hstate.add_object (opc);
28898 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
28900 size_of_locs (loc);
28901 sizes_computed = true;
28903 hash_loc_operands (l, hstate);
28907 /* Compute hash of the whole location list LIST_HEAD. */
28909 static inline void
28910 hash_loc_list (dw_loc_list_ref list_head)
28912 dw_loc_list_ref curr = list_head;
28913 inchash::hash hstate;
28915 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
28917 hstate.add (curr->begin, strlen (curr->begin) + 1);
28918 hstate.add (curr->end, strlen (curr->end) + 1);
28919 if (curr->section)
28920 hstate.add (curr->section, strlen (curr->section) + 1);
28921 hash_locs (curr->expr, hstate);
28923 list_head->hash = hstate.end ();
28926 /* Return true if X and Y opcodes have the same operands. */
28928 static inline bool
28929 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
28931 dw_val_ref valx1 = &x->dw_loc_oprnd1;
28932 dw_val_ref valx2 = &x->dw_loc_oprnd2;
28933 dw_val_ref valy1 = &y->dw_loc_oprnd1;
28934 dw_val_ref valy2 = &y->dw_loc_oprnd2;
28936 switch (x->dw_loc_opc)
28938 case DW_OP_const4u:
28939 case DW_OP_const8u:
28940 if (x->dtprel)
28941 goto hash_addr;
28942 /* FALLTHRU */
28943 case DW_OP_const1u:
28944 case DW_OP_const1s:
28945 case DW_OP_const2u:
28946 case DW_OP_const2s:
28947 case DW_OP_const4s:
28948 case DW_OP_const8s:
28949 case DW_OP_constu:
28950 case DW_OP_consts:
28951 case DW_OP_pick:
28952 case DW_OP_plus_uconst:
28953 case DW_OP_breg0:
28954 case DW_OP_breg1:
28955 case DW_OP_breg2:
28956 case DW_OP_breg3:
28957 case DW_OP_breg4:
28958 case DW_OP_breg5:
28959 case DW_OP_breg6:
28960 case DW_OP_breg7:
28961 case DW_OP_breg8:
28962 case DW_OP_breg9:
28963 case DW_OP_breg10:
28964 case DW_OP_breg11:
28965 case DW_OP_breg12:
28966 case DW_OP_breg13:
28967 case DW_OP_breg14:
28968 case DW_OP_breg15:
28969 case DW_OP_breg16:
28970 case DW_OP_breg17:
28971 case DW_OP_breg18:
28972 case DW_OP_breg19:
28973 case DW_OP_breg20:
28974 case DW_OP_breg21:
28975 case DW_OP_breg22:
28976 case DW_OP_breg23:
28977 case DW_OP_breg24:
28978 case DW_OP_breg25:
28979 case DW_OP_breg26:
28980 case DW_OP_breg27:
28981 case DW_OP_breg28:
28982 case DW_OP_breg29:
28983 case DW_OP_breg30:
28984 case DW_OP_breg31:
28985 case DW_OP_regx:
28986 case DW_OP_fbreg:
28987 case DW_OP_piece:
28988 case DW_OP_deref_size:
28989 case DW_OP_xderef_size:
28990 return valx1->v.val_int == valy1->v.val_int;
28991 case DW_OP_skip:
28992 case DW_OP_bra:
28993 /* If splitting debug info, the use of DW_OP_GNU_addr_index
28994 can cause irrelevant differences in dw_loc_addr. */
28995 gcc_assert (valx1->val_class == dw_val_class_loc
28996 && valy1->val_class == dw_val_class_loc
28997 && (dwarf_split_debug_info
28998 || x->dw_loc_addr == y->dw_loc_addr));
28999 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29000 case DW_OP_implicit_value:
29001 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29002 || valx2->val_class != valy2->val_class)
29003 return false;
29004 switch (valx2->val_class)
29006 case dw_val_class_const:
29007 return valx2->v.val_int == valy2->v.val_int;
29008 case dw_val_class_vec:
29009 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29010 && valx2->v.val_vec.length == valy2->v.val_vec.length
29011 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29012 valx2->v.val_vec.elt_size
29013 * valx2->v.val_vec.length) == 0;
29014 case dw_val_class_const_double:
29015 return valx2->v.val_double.low == valy2->v.val_double.low
29016 && valx2->v.val_double.high == valy2->v.val_double.high;
29017 case dw_val_class_wide_int:
29018 return *valx2->v.val_wide == *valy2->v.val_wide;
29019 case dw_val_class_addr:
29020 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29021 default:
29022 gcc_unreachable ();
29024 case DW_OP_bregx:
29025 case DW_OP_bit_piece:
29026 return valx1->v.val_int == valy1->v.val_int
29027 && valx2->v.val_int == valy2->v.val_int;
29028 case DW_OP_addr:
29029 hash_addr:
29030 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29031 case DW_OP_GNU_addr_index:
29032 case DW_OP_GNU_const_index:
29034 rtx ax1 = valx1->val_entry->addr.rtl;
29035 rtx ay1 = valy1->val_entry->addr.rtl;
29036 return rtx_equal_p (ax1, ay1);
29038 case DW_OP_implicit_pointer:
29039 case DW_OP_GNU_implicit_pointer:
29040 return valx1->val_class == dw_val_class_die_ref
29041 && valx1->val_class == valy1->val_class
29042 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29043 && valx2->v.val_int == valy2->v.val_int;
29044 case DW_OP_entry_value:
29045 case DW_OP_GNU_entry_value:
29046 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29047 case DW_OP_const_type:
29048 case DW_OP_GNU_const_type:
29049 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29050 || valx2->val_class != valy2->val_class)
29051 return false;
29052 switch (valx2->val_class)
29054 case dw_val_class_const:
29055 return valx2->v.val_int == valy2->v.val_int;
29056 case dw_val_class_vec:
29057 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29058 && valx2->v.val_vec.length == valy2->v.val_vec.length
29059 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29060 valx2->v.val_vec.elt_size
29061 * valx2->v.val_vec.length) == 0;
29062 case dw_val_class_const_double:
29063 return valx2->v.val_double.low == valy2->v.val_double.low
29064 && valx2->v.val_double.high == valy2->v.val_double.high;
29065 case dw_val_class_wide_int:
29066 return *valx2->v.val_wide == *valy2->v.val_wide;
29067 default:
29068 gcc_unreachable ();
29070 case DW_OP_regval_type:
29071 case DW_OP_deref_type:
29072 case DW_OP_GNU_regval_type:
29073 case DW_OP_GNU_deref_type:
29074 return valx1->v.val_int == valy1->v.val_int
29075 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29076 case DW_OP_convert:
29077 case DW_OP_reinterpret:
29078 case DW_OP_GNU_convert:
29079 case DW_OP_GNU_reinterpret:
29080 if (valx1->val_class != valy1->val_class)
29081 return false;
29082 if (valx1->val_class == dw_val_class_unsigned_const)
29083 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29084 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29085 case DW_OP_GNU_parameter_ref:
29086 return valx1->val_class == dw_val_class_die_ref
29087 && valx1->val_class == valy1->val_class
29088 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29089 default:
29090 /* Other codes have no operands. */
29091 return true;
29095 /* Return true if DWARF location expressions X and Y are the same. */
29097 static inline bool
29098 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29100 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29101 if (x->dw_loc_opc != y->dw_loc_opc
29102 || x->dtprel != y->dtprel
29103 || !compare_loc_operands (x, y))
29104 break;
29105 return x == NULL && y == NULL;
29108 /* Hashtable helpers. */
29110 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29112 static inline hashval_t hash (const dw_loc_list_struct *);
29113 static inline bool equal (const dw_loc_list_struct *,
29114 const dw_loc_list_struct *);
29117 /* Return precomputed hash of location list X. */
29119 inline hashval_t
29120 loc_list_hasher::hash (const dw_loc_list_struct *x)
29122 return x->hash;
29125 /* Return true if location lists A and B are the same. */
29127 inline bool
29128 loc_list_hasher::equal (const dw_loc_list_struct *a,
29129 const dw_loc_list_struct *b)
29131 if (a == b)
29132 return 1;
29133 if (a->hash != b->hash)
29134 return 0;
29135 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29136 if (strcmp (a->begin, b->begin) != 0
29137 || strcmp (a->end, b->end) != 0
29138 || (a->section == NULL) != (b->section == NULL)
29139 || (a->section && strcmp (a->section, b->section) != 0)
29140 || !compare_locs (a->expr, b->expr))
29141 break;
29142 return a == NULL && b == NULL;
29145 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29148 /* Recursively optimize location lists referenced from DIE
29149 children and share them whenever possible. */
29151 static void
29152 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29154 dw_die_ref c;
29155 dw_attr_node *a;
29156 unsigned ix;
29157 dw_loc_list_struct **slot;
29159 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29160 if (AT_class (a) == dw_val_class_loc_list)
29162 dw_loc_list_ref list = AT_loc_list (a);
29163 /* TODO: perform some optimizations here, before hashing
29164 it and storing into the hash table. */
29165 hash_loc_list (list);
29166 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29167 if (*slot == NULL)
29168 *slot = list;
29169 else
29170 a->dw_attr_val.v.val_loc_list = *slot;
29173 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29177 /* Recursively assign each location list a unique index into the debug_addr
29178 section. */
29180 static void
29181 index_location_lists (dw_die_ref die)
29183 dw_die_ref c;
29184 dw_attr_node *a;
29185 unsigned ix;
29187 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29188 if (AT_class (a) == dw_val_class_loc_list)
29190 dw_loc_list_ref list = AT_loc_list (a);
29191 dw_loc_list_ref curr;
29192 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29194 /* Don't index an entry that has already been indexed
29195 or won't be output. */
29196 if (curr->begin_entry != NULL
29197 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29198 continue;
29200 curr->begin_entry
29201 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29205 FOR_EACH_CHILD (die, c, index_location_lists (c));
29208 /* Optimize location lists referenced from DIE
29209 children and share them whenever possible. */
29211 static void
29212 optimize_location_lists (dw_die_ref die)
29214 loc_list_hash_type htab (500);
29215 optimize_location_lists_1 (die, &htab);
29218 /* Traverse the limbo die list, and add parent/child links. The only
29219 dies without parents that should be here are concrete instances of
29220 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29221 For concrete instances, we can get the parent die from the abstract
29222 instance. */
29224 static void
29225 flush_limbo_die_list (void)
29227 limbo_die_node *node;
29229 /* get_context_die calls force_decl_die, which can put new DIEs on the
29230 limbo list in LTO mode when nested functions are put in a different
29231 partition than that of their parent function. */
29232 while ((node = limbo_die_list))
29234 dw_die_ref die = node->die;
29235 limbo_die_list = node->next;
29237 if (die->die_parent == NULL)
29239 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29241 if (origin && origin->die_parent)
29242 add_child_die (origin->die_parent, die);
29243 else if (is_cu_die (die))
29245 else if (seen_error ())
29246 /* It's OK to be confused by errors in the input. */
29247 add_child_die (comp_unit_die (), die);
29248 else
29250 /* In certain situations, the lexical block containing a
29251 nested function can be optimized away, which results
29252 in the nested function die being orphaned. Likewise
29253 with the return type of that nested function. Force
29254 this to be a child of the containing function.
29256 It may happen that even the containing function got fully
29257 inlined and optimized out. In that case we are lost and
29258 assign the empty child. This should not be big issue as
29259 the function is likely unreachable too. */
29260 gcc_assert (node->created_for);
29262 if (DECL_P (node->created_for))
29263 origin = get_context_die (DECL_CONTEXT (node->created_for));
29264 else if (TYPE_P (node->created_for))
29265 origin = scope_die_for (node->created_for, comp_unit_die ());
29266 else
29267 origin = comp_unit_die ();
29269 add_child_die (origin, die);
29275 /* Output stuff that dwarf requires at the end of every file,
29276 and generate the DWARF-2 debugging info. */
29278 static void
29279 dwarf2out_finish (const char *)
29281 comdat_type_node *ctnode;
29282 dw_die_ref main_comp_unit_die;
29283 unsigned char checksum[16];
29285 /* Flush out any latecomers to the limbo party. */
29286 flush_limbo_die_list ();
29288 if (flag_checking)
29290 verify_die (comp_unit_die ());
29291 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29292 verify_die (node->die);
29295 /* We shouldn't have any symbols with delayed asm names for
29296 DIEs generated after early finish. */
29297 gcc_assert (deferred_asm_name == NULL);
29299 gen_remaining_tmpl_value_param_die_attribute ();
29301 #if ENABLE_ASSERT_CHECKING
29303 dw_die_ref die = comp_unit_die (), c;
29304 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29306 #endif
29307 resolve_addr (comp_unit_die ());
29308 move_marked_base_types ();
29310 /* Initialize sections and labels used for actual assembler output. */
29311 init_sections_and_labels ();
29313 /* Traverse the DIE's and add sibling attributes to those DIE's that
29314 have children. */
29315 add_sibling_attributes (comp_unit_die ());
29316 limbo_die_node *node;
29317 for (node = cu_die_list; node; node = node->next)
29318 add_sibling_attributes (node->die);
29319 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29320 add_sibling_attributes (ctnode->root_die);
29322 /* When splitting DWARF info, we put some attributes in the
29323 skeleton compile_unit DIE that remains in the .o, while
29324 most attributes go in the DWO compile_unit_die. */
29325 if (dwarf_split_debug_info)
29327 limbo_die_node *cu;
29328 main_comp_unit_die = gen_compile_unit_die (NULL);
29329 if (dwarf_version >= 5)
29330 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29331 cu = limbo_die_list;
29332 gcc_assert (cu->die == main_comp_unit_die);
29333 limbo_die_list = limbo_die_list->next;
29334 cu->next = cu_die_list;
29335 cu_die_list = cu;
29337 else
29338 main_comp_unit_die = comp_unit_die ();
29340 /* Output a terminator label for the .text section. */
29341 switch_to_section (text_section);
29342 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29343 if (cold_text_section)
29345 switch_to_section (cold_text_section);
29346 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29349 /* We can only use the low/high_pc attributes if all of the code was
29350 in .text. */
29351 if (!have_multiple_function_sections
29352 || (dwarf_version < 3 && dwarf_strict))
29354 /* Don't add if the CU has no associated code. */
29355 if (text_section_used)
29356 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29357 text_end_label, true);
29359 else
29361 unsigned fde_idx;
29362 dw_fde_ref fde;
29363 bool range_list_added = false;
29365 if (text_section_used)
29366 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29367 text_end_label, &range_list_added, true);
29368 if (cold_text_section_used)
29369 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29370 cold_end_label, &range_list_added, true);
29372 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29374 if (DECL_IGNORED_P (fde->decl))
29375 continue;
29376 if (!fde->in_std_section)
29377 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29378 fde->dw_fde_end, &range_list_added,
29379 true);
29380 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29381 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29382 fde->dw_fde_second_end, &range_list_added,
29383 true);
29386 if (range_list_added)
29388 /* We need to give .debug_loc and .debug_ranges an appropriate
29389 "base address". Use zero so that these addresses become
29390 absolute. Historically, we've emitted the unexpected
29391 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29392 Emit both to give time for other tools to adapt. */
29393 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29394 if (! dwarf_strict && dwarf_version < 4)
29395 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29397 add_ranges (NULL);
29401 if (debug_info_level >= DINFO_LEVEL_TERSE)
29402 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29403 debug_line_section_label);
29405 if (have_macinfo)
29406 add_AT_macptr (comp_unit_die (),
29407 dwarf_version >= 5 ? DW_AT_macros
29408 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29409 macinfo_section_label);
29411 if (dwarf_split_debug_info)
29413 if (have_location_lists)
29415 if (dwarf_version >= 5)
29416 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29417 loc_section_label);
29418 /* optimize_location_lists calculates the size of the lists,
29419 so index them first, and assign indices to the entries.
29420 Although optimize_location_lists will remove entries from
29421 the table, it only does so for duplicates, and therefore
29422 only reduces ref_counts to 1. */
29423 index_location_lists (comp_unit_die ());
29426 if (addr_index_table != NULL)
29428 unsigned int index = 0;
29429 addr_index_table
29430 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29431 (&index);
29435 loc_list_idx = 0;
29436 if (have_location_lists)
29438 optimize_location_lists (comp_unit_die ());
29439 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29440 if (dwarf_version >= 5 && dwarf_split_debug_info)
29441 assign_location_list_indexes (comp_unit_die ());
29444 save_macinfo_strings ();
29446 if (dwarf_split_debug_info)
29448 unsigned int index = 0;
29450 /* Add attributes common to skeleton compile_units and
29451 type_units. Because these attributes include strings, it
29452 must be done before freezing the string table. Top-level
29453 skeleton die attrs are added when the skeleton type unit is
29454 created, so ensure it is created by this point. */
29455 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29456 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29459 /* Output all of the compilation units. We put the main one last so that
29460 the offsets are available to output_pubnames. */
29461 for (node = cu_die_list; node; node = node->next)
29462 output_comp_unit (node->die, 0, NULL);
29464 hash_table<comdat_type_hasher> comdat_type_table (100);
29465 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29467 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29469 /* Don't output duplicate types. */
29470 if (*slot != HTAB_EMPTY_ENTRY)
29471 continue;
29473 /* Add a pointer to the line table for the main compilation unit
29474 so that the debugger can make sense of DW_AT_decl_file
29475 attributes. */
29476 if (debug_info_level >= DINFO_LEVEL_TERSE)
29477 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29478 (!dwarf_split_debug_info
29479 ? debug_line_section_label
29480 : debug_skeleton_line_section_label));
29482 output_comdat_type_unit (ctnode);
29483 *slot = ctnode;
29486 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29487 both the main_cu and all skeleton TUs. Making this call unconditional
29488 would end up either adding a second copy of the AT_pubnames attribute, or
29489 requiring a special case in add_top_level_skeleton_die_attrs. */
29490 if (!dwarf_split_debug_info)
29491 add_AT_pubnames (comp_unit_die ());
29493 if (dwarf_split_debug_info)
29495 int mark;
29496 struct md5_ctx ctx;
29498 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29499 index_rnglists ();
29501 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29502 md5_init_ctx (&ctx);
29503 mark = 0;
29504 die_checksum (comp_unit_die (), &ctx, &mark);
29505 unmark_all_dies (comp_unit_die ());
29506 md5_finish_ctx (&ctx, checksum);
29508 if (dwarf_version < 5)
29510 /* Use the first 8 bytes of the checksum as the dwo_id,
29511 and add it to both comp-unit DIEs. */
29512 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29513 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29516 /* Add the base offset of the ranges table to the skeleton
29517 comp-unit DIE. */
29518 if (!vec_safe_is_empty (ranges_table))
29520 if (dwarf_version >= 5)
29521 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29522 ranges_base_label);
29523 else
29524 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29525 ranges_section_label);
29528 switch_to_section (debug_addr_section);
29529 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29530 output_addr_table ();
29533 /* Output the main compilation unit if non-empty or if .debug_macinfo
29534 or .debug_macro will be emitted. */
29535 output_comp_unit (comp_unit_die (), have_macinfo,
29536 dwarf_split_debug_info ? checksum : NULL);
29538 if (dwarf_split_debug_info && info_section_emitted)
29539 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29541 /* Output the abbreviation table. */
29542 if (vec_safe_length (abbrev_die_table) != 1)
29544 switch_to_section (debug_abbrev_section);
29545 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29546 output_abbrev_section ();
29549 /* Output location list section if necessary. */
29550 if (have_location_lists)
29552 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29553 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29554 /* Output the location lists info. */
29555 switch_to_section (debug_loc_section);
29556 if (dwarf_version >= 5)
29558 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29559 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29560 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29561 dw2_asm_output_data (4, 0xffffffff,
29562 "Initial length escape value indicating "
29563 "64-bit DWARF extension");
29564 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29565 "Length of Location Lists");
29566 ASM_OUTPUT_LABEL (asm_out_file, l1);
29567 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29568 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29569 dw2_asm_output_data (1, 0, "Segment Size");
29570 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29571 "Offset Entry Count");
29573 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29574 if (dwarf_version >= 5 && dwarf_split_debug_info)
29576 unsigned int save_loc_list_idx = loc_list_idx;
29577 loc_list_idx = 0;
29578 output_loclists_offsets (comp_unit_die ());
29579 gcc_assert (save_loc_list_idx == loc_list_idx);
29581 output_location_lists (comp_unit_die ());
29582 if (dwarf_version >= 5)
29583 ASM_OUTPUT_LABEL (asm_out_file, l2);
29586 output_pubtables ();
29588 /* Output the address range information if a CU (.debug_info section)
29589 was emitted. We output an empty table even if we had no functions
29590 to put in it. This because the consumer has no way to tell the
29591 difference between an empty table that we omitted and failure to
29592 generate a table that would have contained data. */
29593 if (info_section_emitted)
29595 switch_to_section (debug_aranges_section);
29596 output_aranges ();
29599 /* Output ranges section if necessary. */
29600 if (!vec_safe_is_empty (ranges_table))
29602 if (dwarf_version >= 5)
29603 output_rnglists ();
29604 else
29605 output_ranges ();
29608 /* Have to end the macro section. */
29609 if (have_macinfo)
29611 switch_to_section (debug_macinfo_section);
29612 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29613 output_macinfo ();
29614 dw2_asm_output_data (1, 0, "End compilation unit");
29617 /* Output the source line correspondence table. We must do this
29618 even if there is no line information. Otherwise, on an empty
29619 translation unit, we will generate a present, but empty,
29620 .debug_info section. IRIX 6.5 `nm' will then complain when
29621 examining the file. This is done late so that any filenames
29622 used by the debug_info section are marked as 'used'. */
29623 switch_to_section (debug_line_section);
29624 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29625 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29626 output_line_info (false);
29628 if (dwarf_split_debug_info && info_section_emitted)
29630 switch_to_section (debug_skeleton_line_section);
29631 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29632 output_line_info (true);
29635 /* If we emitted any indirect strings, output the string table too. */
29636 if (debug_str_hash || skeleton_debug_str_hash)
29637 output_indirect_strings ();
29638 if (debug_line_str_hash)
29640 switch_to_section (debug_line_str_section);
29641 const enum dwarf_form form = DW_FORM_line_strp;
29642 debug_line_str_hash->traverse<enum dwarf_form,
29643 output_indirect_string> (form);
29647 /* Perform any cleanups needed after the early debug generation pass
29648 has run. */
29650 static void
29651 dwarf2out_early_finish (const char *filename)
29653 set_early_dwarf s;
29655 /* PCH might result in DW_AT_producer string being restored from the
29656 header compilation, so always fill it with empty string initially
29657 and overwrite only here. */
29658 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
29659 producer_string = gen_producer_string ();
29660 producer->dw_attr_val.v.val_str->refcount--;
29661 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
29663 /* Add the name for the main input file now. We delayed this from
29664 dwarf2out_init to avoid complications with PCH. */
29665 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
29666 add_comp_dir_attribute (comp_unit_die ());
29668 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
29669 DW_AT_comp_dir into .debug_line_str section. */
29670 if (!DWARF2_ASM_LINE_DEBUG_INFO
29671 && dwarf_version >= 5
29672 && DWARF5_USE_DEBUG_LINE_STR)
29674 for (int i = 0; i < 2; i++)
29676 dw_attr_node *a = get_AT (comp_unit_die (),
29677 i ? DW_AT_comp_dir : DW_AT_name);
29678 if (a == NULL
29679 || AT_class (a) != dw_val_class_str
29680 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
29681 continue;
29683 if (! debug_line_str_hash)
29684 debug_line_str_hash
29685 = hash_table<indirect_string_hasher>::create_ggc (10);
29687 struct indirect_string_node *node
29688 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
29689 set_indirect_string (node);
29690 node->form = DW_FORM_line_strp;
29691 a->dw_attr_val.v.val_str->refcount--;
29692 a->dw_attr_val.v.val_str = node;
29696 /* With LTO early dwarf was really finished at compile-time, so make
29697 sure to adjust the phase after annotating the LTRANS CU DIE. */
29698 if (in_lto_p)
29700 early_dwarf_finished = true;
29701 return;
29704 /* Walk through the list of incomplete types again, trying once more to
29705 emit full debugging info for them. */
29706 retry_incomplete_types ();
29708 /* The point here is to flush out the limbo list so that it is empty
29709 and we don't need to stream it for LTO. */
29710 flush_limbo_die_list ();
29712 gen_scheduled_generic_parms_dies ();
29713 gen_remaining_tmpl_value_param_die_attribute ();
29715 /* Add DW_AT_linkage_name for all deferred DIEs. */
29716 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
29718 tree decl = node->created_for;
29719 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
29720 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
29721 ended up in deferred_asm_name before we knew it was
29722 constant and never written to disk. */
29723 && DECL_ASSEMBLER_NAME (decl))
29725 add_linkage_attr (node->die, decl);
29726 move_linkage_attr (node->die);
29729 deferred_asm_name = NULL;
29731 if (flag_eliminate_unused_debug_types)
29732 prune_unused_types ();
29734 /* Generate separate COMDAT sections for type DIEs. */
29735 if (use_debug_types)
29737 break_out_comdat_types (comp_unit_die ());
29739 /* Each new type_unit DIE was added to the limbo die list when created.
29740 Since these have all been added to comdat_type_list, clear the
29741 limbo die list. */
29742 limbo_die_list = NULL;
29744 /* For each new comdat type unit, copy declarations for incomplete
29745 types to make the new unit self-contained (i.e., no direct
29746 references to the main compile unit). */
29747 for (comdat_type_node *ctnode = comdat_type_list;
29748 ctnode != NULL; ctnode = ctnode->next)
29749 copy_decls_for_unworthy_types (ctnode->root_die);
29750 copy_decls_for_unworthy_types (comp_unit_die ());
29752 /* In the process of copying declarations from one unit to another,
29753 we may have left some declarations behind that are no longer
29754 referenced. Prune them. */
29755 prune_unused_types ();
29758 /* Generate separate CUs for each of the include files we've seen.
29759 They will go into limbo_die_list and from there to cu_die_list. */
29760 if (flag_eliminate_dwarf2_dups)
29762 gcc_assert (limbo_die_list == NULL);
29763 break_out_includes (comp_unit_die ());
29764 limbo_die_node *cu;
29765 while ((cu = limbo_die_list))
29767 limbo_die_list = cu->next;
29768 cu->next = cu_die_list;
29769 cu_die_list = cu;
29773 /* The early debug phase is now finished. */
29774 early_dwarf_finished = true;
29777 /* Reset all state within dwarf2out.c so that we can rerun the compiler
29778 within the same process. For use by toplev::finalize. */
29780 void
29781 dwarf2out_c_finalize (void)
29783 last_var_location_insn = NULL;
29784 cached_next_real_insn = NULL;
29785 used_rtx_array = NULL;
29786 incomplete_types = NULL;
29787 decl_scope_table = NULL;
29788 debug_info_section = NULL;
29789 debug_skeleton_info_section = NULL;
29790 debug_abbrev_section = NULL;
29791 debug_skeleton_abbrev_section = NULL;
29792 debug_aranges_section = NULL;
29793 debug_addr_section = NULL;
29794 debug_macinfo_section = NULL;
29795 debug_line_section = NULL;
29796 debug_skeleton_line_section = NULL;
29797 debug_loc_section = NULL;
29798 debug_pubnames_section = NULL;
29799 debug_pubtypes_section = NULL;
29800 debug_str_section = NULL;
29801 debug_line_str_section = NULL;
29802 debug_str_dwo_section = NULL;
29803 debug_str_offsets_section = NULL;
29804 debug_ranges_section = NULL;
29805 debug_frame_section = NULL;
29806 fde_vec = NULL;
29807 debug_str_hash = NULL;
29808 debug_line_str_hash = NULL;
29809 skeleton_debug_str_hash = NULL;
29810 dw2_string_counter = 0;
29811 have_multiple_function_sections = false;
29812 text_section_used = false;
29813 cold_text_section_used = false;
29814 cold_text_section = NULL;
29815 current_unit_personality = NULL;
29817 next_die_offset = 0;
29818 single_comp_unit_die = NULL;
29819 comdat_type_list = NULL;
29820 limbo_die_list = NULL;
29821 file_table = NULL;
29822 decl_die_table = NULL;
29823 common_block_die_table = NULL;
29824 decl_loc_table = NULL;
29825 call_arg_locations = NULL;
29826 call_arg_loc_last = NULL;
29827 call_site_count = -1;
29828 tail_call_site_count = -1;
29829 cached_dw_loc_list_table = NULL;
29830 abbrev_die_table = NULL;
29831 delete dwarf_proc_stack_usage_map;
29832 dwarf_proc_stack_usage_map = NULL;
29833 line_info_label_num = 0;
29834 cur_line_info_table = NULL;
29835 text_section_line_info = NULL;
29836 cold_text_section_line_info = NULL;
29837 separate_line_info = NULL;
29838 info_section_emitted = false;
29839 pubname_table = NULL;
29840 pubtype_table = NULL;
29841 macinfo_table = NULL;
29842 ranges_table = NULL;
29843 ranges_by_label = NULL;
29844 rnglist_idx = 0;
29845 have_location_lists = false;
29846 loclabel_num = 0;
29847 poc_label_num = 0;
29848 last_emitted_file = NULL;
29849 label_num = 0;
29850 tmpl_value_parm_die_table = NULL;
29851 generic_type_instances = NULL;
29852 frame_pointer_fb_offset = 0;
29853 frame_pointer_fb_offset_valid = false;
29854 base_types.release ();
29855 XDELETEVEC (producer_string);
29856 producer_string = NULL;
29859 #include "gt-dwarf2out.h"