dwarf2out.c: fix jit issue with early_dwarf_finished
[official-gcc.git] / gcc / dwarf2out.c
blob8dc85237288cf617ed8b7cdab457744c506c966d
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
1518 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1519 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1523 /* Add a constant OFFSET to a location list. */
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1528 dw_loc_list_ref d;
1529 for (d = list_head; d != NULL; d = d->dw_loc_next)
1530 loc_descr_plus_const (&d->expr, offset);
1533 #define DWARF_REF_SIZE \
1534 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1536 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1537 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1538 DW_FORM_data16 with 128 bits. */
1539 #define DWARF_LARGEST_DATA_FORM_BITS \
1540 (dwarf_version >= 5 ? 128 : 64)
1542 /* Utility inline function for construction of ops that were GNU extension
1543 before DWARF 5. */
1544 static inline enum dwarf_location_atom
1545 dwarf_OP (enum dwarf_location_atom op)
1547 switch (op)
1549 case DW_OP_implicit_pointer:
1550 if (dwarf_version < 5)
1551 return DW_OP_GNU_implicit_pointer;
1552 break;
1554 case DW_OP_entry_value:
1555 if (dwarf_version < 5)
1556 return DW_OP_GNU_entry_value;
1557 break;
1559 case DW_OP_const_type:
1560 if (dwarf_version < 5)
1561 return DW_OP_GNU_const_type;
1562 break;
1564 case DW_OP_regval_type:
1565 if (dwarf_version < 5)
1566 return DW_OP_GNU_regval_type;
1567 break;
1569 case DW_OP_deref_type:
1570 if (dwarf_version < 5)
1571 return DW_OP_GNU_deref_type;
1572 break;
1574 case DW_OP_convert:
1575 if (dwarf_version < 5)
1576 return DW_OP_GNU_convert;
1577 break;
1579 case DW_OP_reinterpret:
1580 if (dwarf_version < 5)
1581 return DW_OP_GNU_reinterpret;
1582 break;
1584 default:
1585 break;
1587 return op;
1590 /* Similarly for attributes. */
1591 static inline enum dwarf_attribute
1592 dwarf_AT (enum dwarf_attribute at)
1594 switch (at)
1596 case DW_AT_call_return_pc:
1597 if (dwarf_version < 5)
1598 return DW_AT_low_pc;
1599 break;
1601 case DW_AT_call_tail_call:
1602 if (dwarf_version < 5)
1603 return DW_AT_GNU_tail_call;
1604 break;
1606 case DW_AT_call_origin:
1607 if (dwarf_version < 5)
1608 return DW_AT_abstract_origin;
1609 break;
1611 case DW_AT_call_target:
1612 if (dwarf_version < 5)
1613 return DW_AT_GNU_call_site_target;
1614 break;
1616 case DW_AT_call_target_clobbered:
1617 if (dwarf_version < 5)
1618 return DW_AT_GNU_call_site_target_clobbered;
1619 break;
1621 case DW_AT_call_parameter:
1622 if (dwarf_version < 5)
1623 return DW_AT_abstract_origin;
1624 break;
1626 case DW_AT_call_value:
1627 if (dwarf_version < 5)
1628 return DW_AT_GNU_call_site_value;
1629 break;
1631 case DW_AT_call_data_value:
1632 if (dwarf_version < 5)
1633 return DW_AT_GNU_call_site_data_value;
1634 break;
1636 case DW_AT_call_all_calls:
1637 if (dwarf_version < 5)
1638 return DW_AT_GNU_all_call_sites;
1639 break;
1641 case DW_AT_call_all_tail_calls:
1642 if (dwarf_version < 5)
1643 return DW_AT_GNU_all_tail_call_sites;
1644 break;
1646 case DW_AT_dwo_name:
1647 if (dwarf_version < 5)
1648 return DW_AT_GNU_dwo_name;
1649 break;
1651 default:
1652 break;
1654 return at;
1657 /* And similarly for tags. */
1658 static inline enum dwarf_tag
1659 dwarf_TAG (enum dwarf_tag tag)
1661 switch (tag)
1663 case DW_TAG_call_site:
1664 if (dwarf_version < 5)
1665 return DW_TAG_GNU_call_site;
1666 break;
1668 case DW_TAG_call_site_parameter:
1669 if (dwarf_version < 5)
1670 return DW_TAG_GNU_call_site_parameter;
1671 break;
1673 default:
1674 break;
1676 return tag;
1679 static unsigned long int get_base_type_offset (dw_die_ref);
1681 /* Return the size of a location descriptor. */
1683 static unsigned long
1684 size_of_loc_descr (dw_loc_descr_ref loc)
1686 unsigned long size = 1;
1688 switch (loc->dw_loc_opc)
1690 case DW_OP_addr:
1691 size += DWARF2_ADDR_SIZE;
1692 break;
1693 case DW_OP_GNU_addr_index:
1694 case DW_OP_GNU_const_index:
1695 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1696 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1697 break;
1698 case DW_OP_const1u:
1699 case DW_OP_const1s:
1700 size += 1;
1701 break;
1702 case DW_OP_const2u:
1703 case DW_OP_const2s:
1704 size += 2;
1705 break;
1706 case DW_OP_const4u:
1707 case DW_OP_const4s:
1708 size += 4;
1709 break;
1710 case DW_OP_const8u:
1711 case DW_OP_const8s:
1712 size += 8;
1713 break;
1714 case DW_OP_constu:
1715 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1716 break;
1717 case DW_OP_consts:
1718 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1719 break;
1720 case DW_OP_pick:
1721 size += 1;
1722 break;
1723 case DW_OP_plus_uconst:
1724 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1725 break;
1726 case DW_OP_skip:
1727 case DW_OP_bra:
1728 size += 2;
1729 break;
1730 case DW_OP_breg0:
1731 case DW_OP_breg1:
1732 case DW_OP_breg2:
1733 case DW_OP_breg3:
1734 case DW_OP_breg4:
1735 case DW_OP_breg5:
1736 case DW_OP_breg6:
1737 case DW_OP_breg7:
1738 case DW_OP_breg8:
1739 case DW_OP_breg9:
1740 case DW_OP_breg10:
1741 case DW_OP_breg11:
1742 case DW_OP_breg12:
1743 case DW_OP_breg13:
1744 case DW_OP_breg14:
1745 case DW_OP_breg15:
1746 case DW_OP_breg16:
1747 case DW_OP_breg17:
1748 case DW_OP_breg18:
1749 case DW_OP_breg19:
1750 case DW_OP_breg20:
1751 case DW_OP_breg21:
1752 case DW_OP_breg22:
1753 case DW_OP_breg23:
1754 case DW_OP_breg24:
1755 case DW_OP_breg25:
1756 case DW_OP_breg26:
1757 case DW_OP_breg27:
1758 case DW_OP_breg28:
1759 case DW_OP_breg29:
1760 case DW_OP_breg30:
1761 case DW_OP_breg31:
1762 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1763 break;
1764 case DW_OP_regx:
1765 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1766 break;
1767 case DW_OP_fbreg:
1768 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1769 break;
1770 case DW_OP_bregx:
1771 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1772 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1773 break;
1774 case DW_OP_piece:
1775 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1776 break;
1777 case DW_OP_bit_piece:
1778 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1779 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1780 break;
1781 case DW_OP_deref_size:
1782 case DW_OP_xderef_size:
1783 size += 1;
1784 break;
1785 case DW_OP_call2:
1786 size += 2;
1787 break;
1788 case DW_OP_call4:
1789 size += 4;
1790 break;
1791 case DW_OP_call_ref:
1792 size += DWARF_REF_SIZE;
1793 break;
1794 case DW_OP_implicit_value:
1795 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1796 + loc->dw_loc_oprnd1.v.val_unsigned;
1797 break;
1798 case DW_OP_implicit_pointer:
1799 case DW_OP_GNU_implicit_pointer:
1800 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1801 break;
1802 case DW_OP_entry_value:
1803 case DW_OP_GNU_entry_value:
1805 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1806 size += size_of_uleb128 (op_size) + op_size;
1807 break;
1809 case DW_OP_const_type:
1810 case DW_OP_GNU_const_type:
1812 unsigned long o
1813 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1814 size += size_of_uleb128 (o) + 1;
1815 switch (loc->dw_loc_oprnd2.val_class)
1817 case dw_val_class_vec:
1818 size += loc->dw_loc_oprnd2.v.val_vec.length
1819 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1820 break;
1821 case dw_val_class_const:
1822 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1823 break;
1824 case dw_val_class_const_double:
1825 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1826 break;
1827 case dw_val_class_wide_int:
1828 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1829 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1830 break;
1831 default:
1832 gcc_unreachable ();
1834 break;
1836 case DW_OP_regval_type:
1837 case DW_OP_GNU_regval_type:
1839 unsigned long o
1840 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1841 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1842 + size_of_uleb128 (o);
1844 break;
1845 case DW_OP_deref_type:
1846 case DW_OP_GNU_deref_type:
1848 unsigned long o
1849 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1850 size += 1 + size_of_uleb128 (o);
1852 break;
1853 case DW_OP_convert:
1854 case DW_OP_reinterpret:
1855 case DW_OP_GNU_convert:
1856 case DW_OP_GNU_reinterpret:
1857 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1858 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1859 else
1861 unsigned long o
1862 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1863 size += size_of_uleb128 (o);
1865 break;
1866 case DW_OP_GNU_parameter_ref:
1867 size += 4;
1868 break;
1869 default:
1870 break;
1873 return size;
1876 /* Return the size of a series of location descriptors. */
1878 unsigned long
1879 size_of_locs (dw_loc_descr_ref loc)
1881 dw_loc_descr_ref l;
1882 unsigned long size;
1884 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1885 field, to avoid writing to a PCH file. */
1886 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1888 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1889 break;
1890 size += size_of_loc_descr (l);
1892 if (! l)
1893 return size;
1895 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1897 l->dw_loc_addr = size;
1898 size += size_of_loc_descr (l);
1901 return size;
1904 /* Return the size of the value in a DW_AT_discr_value attribute. */
1906 static int
1907 size_of_discr_value (dw_discr_value *discr_value)
1909 if (discr_value->pos)
1910 return size_of_uleb128 (discr_value->v.uval);
1911 else
1912 return size_of_sleb128 (discr_value->v.sval);
1915 /* Return the size of the value in a DW_AT_discr_list attribute. */
1917 static int
1918 size_of_discr_list (dw_discr_list_ref discr_list)
1920 int size = 0;
1922 for (dw_discr_list_ref list = discr_list;
1923 list != NULL;
1924 list = list->dw_discr_next)
1926 /* One byte for the discriminant value descriptor, and then one or two
1927 LEB128 numbers, depending on whether it's a single case label or a
1928 range label. */
1929 size += 1;
1930 size += size_of_discr_value (&list->dw_discr_lower_bound);
1931 if (list->dw_discr_range != 0)
1932 size += size_of_discr_value (&list->dw_discr_upper_bound);
1934 return size;
1937 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1938 static void get_ref_die_offset_label (char *, dw_die_ref);
1939 static unsigned long int get_ref_die_offset (dw_die_ref);
1941 /* Output location description stack opcode's operands (if any).
1942 The for_eh_or_skip parameter controls whether register numbers are
1943 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1944 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1945 info). This should be suppressed for the cases that have not been converted
1946 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1948 static void
1949 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1951 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1952 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1954 switch (loc->dw_loc_opc)
1956 #ifdef DWARF2_DEBUGGING_INFO
1957 case DW_OP_const2u:
1958 case DW_OP_const2s:
1959 dw2_asm_output_data (2, val1->v.val_int, NULL);
1960 break;
1961 case DW_OP_const4u:
1962 if (loc->dtprel)
1964 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1965 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1966 val1->v.val_addr);
1967 fputc ('\n', asm_out_file);
1968 break;
1970 /* FALLTHRU */
1971 case DW_OP_const4s:
1972 dw2_asm_output_data (4, val1->v.val_int, NULL);
1973 break;
1974 case DW_OP_const8u:
1975 if (loc->dtprel)
1977 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1978 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1979 val1->v.val_addr);
1980 fputc ('\n', asm_out_file);
1981 break;
1983 /* FALLTHRU */
1984 case DW_OP_const8s:
1985 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1986 dw2_asm_output_data (8, val1->v.val_int, NULL);
1987 break;
1988 case DW_OP_skip:
1989 case DW_OP_bra:
1991 int offset;
1993 gcc_assert (val1->val_class == dw_val_class_loc);
1994 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1996 dw2_asm_output_data (2, offset, NULL);
1998 break;
1999 case DW_OP_implicit_value:
2000 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2001 switch (val2->val_class)
2003 case dw_val_class_const:
2004 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2005 break;
2006 case dw_val_class_vec:
2008 unsigned int elt_size = val2->v.val_vec.elt_size;
2009 unsigned int len = val2->v.val_vec.length;
2010 unsigned int i;
2011 unsigned char *p;
2013 if (elt_size > sizeof (HOST_WIDE_INT))
2015 elt_size /= 2;
2016 len *= 2;
2018 for (i = 0, p = val2->v.val_vec.array;
2019 i < len;
2020 i++, p += elt_size)
2021 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2022 "fp or vector constant word %u", i);
2024 break;
2025 case dw_val_class_const_double:
2027 unsigned HOST_WIDE_INT first, second;
2029 if (WORDS_BIG_ENDIAN)
2031 first = val2->v.val_double.high;
2032 second = val2->v.val_double.low;
2034 else
2036 first = val2->v.val_double.low;
2037 second = val2->v.val_double.high;
2039 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2040 first, NULL);
2041 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2042 second, NULL);
2044 break;
2045 case dw_val_class_wide_int:
2047 int i;
2048 int len = get_full_len (*val2->v.val_wide);
2049 if (WORDS_BIG_ENDIAN)
2050 for (i = len - 1; i >= 0; --i)
2051 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2052 val2->v.val_wide->elt (i), NULL);
2053 else
2054 for (i = 0; i < len; ++i)
2055 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2056 val2->v.val_wide->elt (i), NULL);
2058 break;
2059 case dw_val_class_addr:
2060 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2061 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2062 break;
2063 default:
2064 gcc_unreachable ();
2066 break;
2067 #else
2068 case DW_OP_const2u:
2069 case DW_OP_const2s:
2070 case DW_OP_const4u:
2071 case DW_OP_const4s:
2072 case DW_OP_const8u:
2073 case DW_OP_const8s:
2074 case DW_OP_skip:
2075 case DW_OP_bra:
2076 case DW_OP_implicit_value:
2077 /* We currently don't make any attempt to make sure these are
2078 aligned properly like we do for the main unwind info, so
2079 don't support emitting things larger than a byte if we're
2080 only doing unwinding. */
2081 gcc_unreachable ();
2082 #endif
2083 case DW_OP_const1u:
2084 case DW_OP_const1s:
2085 dw2_asm_output_data (1, val1->v.val_int, NULL);
2086 break;
2087 case DW_OP_constu:
2088 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2089 break;
2090 case DW_OP_consts:
2091 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2092 break;
2093 case DW_OP_pick:
2094 dw2_asm_output_data (1, val1->v.val_int, NULL);
2095 break;
2096 case DW_OP_plus_uconst:
2097 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2098 break;
2099 case DW_OP_breg0:
2100 case DW_OP_breg1:
2101 case DW_OP_breg2:
2102 case DW_OP_breg3:
2103 case DW_OP_breg4:
2104 case DW_OP_breg5:
2105 case DW_OP_breg6:
2106 case DW_OP_breg7:
2107 case DW_OP_breg8:
2108 case DW_OP_breg9:
2109 case DW_OP_breg10:
2110 case DW_OP_breg11:
2111 case DW_OP_breg12:
2112 case DW_OP_breg13:
2113 case DW_OP_breg14:
2114 case DW_OP_breg15:
2115 case DW_OP_breg16:
2116 case DW_OP_breg17:
2117 case DW_OP_breg18:
2118 case DW_OP_breg19:
2119 case DW_OP_breg20:
2120 case DW_OP_breg21:
2121 case DW_OP_breg22:
2122 case DW_OP_breg23:
2123 case DW_OP_breg24:
2124 case DW_OP_breg25:
2125 case DW_OP_breg26:
2126 case DW_OP_breg27:
2127 case DW_OP_breg28:
2128 case DW_OP_breg29:
2129 case DW_OP_breg30:
2130 case DW_OP_breg31:
2131 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2132 break;
2133 case DW_OP_regx:
2135 unsigned r = val1->v.val_unsigned;
2136 if (for_eh_or_skip >= 0)
2137 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2138 gcc_assert (size_of_uleb128 (r)
2139 == size_of_uleb128 (val1->v.val_unsigned));
2140 dw2_asm_output_data_uleb128 (r, NULL);
2142 break;
2143 case DW_OP_fbreg:
2144 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2145 break;
2146 case DW_OP_bregx:
2148 unsigned r = val1->v.val_unsigned;
2149 if (for_eh_or_skip >= 0)
2150 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2151 gcc_assert (size_of_uleb128 (r)
2152 == size_of_uleb128 (val1->v.val_unsigned));
2153 dw2_asm_output_data_uleb128 (r, NULL);
2154 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2156 break;
2157 case DW_OP_piece:
2158 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2159 break;
2160 case DW_OP_bit_piece:
2161 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2162 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2163 break;
2164 case DW_OP_deref_size:
2165 case DW_OP_xderef_size:
2166 dw2_asm_output_data (1, val1->v.val_int, NULL);
2167 break;
2169 case DW_OP_addr:
2170 if (loc->dtprel)
2172 if (targetm.asm_out.output_dwarf_dtprel)
2174 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2175 DWARF2_ADDR_SIZE,
2176 val1->v.val_addr);
2177 fputc ('\n', asm_out_file);
2179 else
2180 gcc_unreachable ();
2182 else
2184 #ifdef DWARF2_DEBUGGING_INFO
2185 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2186 #else
2187 gcc_unreachable ();
2188 #endif
2190 break;
2192 case DW_OP_GNU_addr_index:
2193 case DW_OP_GNU_const_index:
2194 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2195 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2196 "(index into .debug_addr)");
2197 break;
2199 case DW_OP_call2:
2200 case DW_OP_call4:
2202 unsigned long die_offset
2203 = get_ref_die_offset (val1->v.val_die_ref.die);
2204 /* Make sure the offset has been computed and that we can encode it as
2205 an operand. */
2206 gcc_assert (die_offset > 0
2207 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2208 ? 0xffff
2209 : 0xffffffff));
2210 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2211 die_offset, NULL);
2213 break;
2215 case DW_OP_implicit_pointer:
2216 case DW_OP_GNU_implicit_pointer:
2218 char label[MAX_ARTIFICIAL_LABEL_BYTES
2219 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2220 gcc_assert (val1->val_class == dw_val_class_die_ref);
2221 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2222 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2223 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2225 break;
2227 case DW_OP_entry_value:
2228 case DW_OP_GNU_entry_value:
2229 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2230 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2231 break;
2233 case DW_OP_const_type:
2234 case DW_OP_GNU_const_type:
2236 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2237 gcc_assert (o);
2238 dw2_asm_output_data_uleb128 (o, NULL);
2239 switch (val2->val_class)
2241 case dw_val_class_const:
2242 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2243 dw2_asm_output_data (1, l, NULL);
2244 dw2_asm_output_data (l, val2->v.val_int, NULL);
2245 break;
2246 case dw_val_class_vec:
2248 unsigned int elt_size = val2->v.val_vec.elt_size;
2249 unsigned int len = val2->v.val_vec.length;
2250 unsigned int i;
2251 unsigned char *p;
2253 l = len * elt_size;
2254 dw2_asm_output_data (1, l, NULL);
2255 if (elt_size > sizeof (HOST_WIDE_INT))
2257 elt_size /= 2;
2258 len *= 2;
2260 for (i = 0, p = val2->v.val_vec.array;
2261 i < len;
2262 i++, p += elt_size)
2263 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2264 "fp or vector constant word %u", i);
2266 break;
2267 case dw_val_class_const_double:
2269 unsigned HOST_WIDE_INT first, second;
2270 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2272 dw2_asm_output_data (1, 2 * l, NULL);
2273 if (WORDS_BIG_ENDIAN)
2275 first = val2->v.val_double.high;
2276 second = val2->v.val_double.low;
2278 else
2280 first = val2->v.val_double.low;
2281 second = val2->v.val_double.high;
2283 dw2_asm_output_data (l, first, NULL);
2284 dw2_asm_output_data (l, second, NULL);
2286 break;
2287 case dw_val_class_wide_int:
2289 int i;
2290 int len = get_full_len (*val2->v.val_wide);
2291 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2293 dw2_asm_output_data (1, len * l, NULL);
2294 if (WORDS_BIG_ENDIAN)
2295 for (i = len - 1; i >= 0; --i)
2296 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2297 else
2298 for (i = 0; i < len; ++i)
2299 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2301 break;
2302 default:
2303 gcc_unreachable ();
2306 break;
2307 case DW_OP_regval_type:
2308 case DW_OP_GNU_regval_type:
2310 unsigned r = val1->v.val_unsigned;
2311 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2312 gcc_assert (o);
2313 if (for_eh_or_skip >= 0)
2315 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2316 gcc_assert (size_of_uleb128 (r)
2317 == size_of_uleb128 (val1->v.val_unsigned));
2319 dw2_asm_output_data_uleb128 (r, NULL);
2320 dw2_asm_output_data_uleb128 (o, NULL);
2322 break;
2323 case DW_OP_deref_type:
2324 case DW_OP_GNU_deref_type:
2326 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2327 gcc_assert (o);
2328 dw2_asm_output_data (1, val1->v.val_int, NULL);
2329 dw2_asm_output_data_uleb128 (o, NULL);
2331 break;
2332 case DW_OP_convert:
2333 case DW_OP_reinterpret:
2334 case DW_OP_GNU_convert:
2335 case DW_OP_GNU_reinterpret:
2336 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2337 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2338 else
2340 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2341 gcc_assert (o);
2342 dw2_asm_output_data_uleb128 (o, NULL);
2344 break;
2346 case DW_OP_GNU_parameter_ref:
2348 unsigned long o;
2349 gcc_assert (val1->val_class == dw_val_class_die_ref);
2350 o = get_ref_die_offset (val1->v.val_die_ref.die);
2351 dw2_asm_output_data (4, o, NULL);
2353 break;
2355 default:
2356 /* Other codes have no operands. */
2357 break;
2361 /* Output a sequence of location operations.
2362 The for_eh_or_skip parameter controls whether register numbers are
2363 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2364 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2365 info). This should be suppressed for the cases that have not been converted
2366 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2368 void
2369 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2371 for (; loc != NULL; loc = loc->dw_loc_next)
2373 enum dwarf_location_atom opc = loc->dw_loc_opc;
2374 /* Output the opcode. */
2375 if (for_eh_or_skip >= 0
2376 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2378 unsigned r = (opc - DW_OP_breg0);
2379 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2380 gcc_assert (r <= 31);
2381 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2383 else if (for_eh_or_skip >= 0
2384 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2386 unsigned r = (opc - DW_OP_reg0);
2387 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2388 gcc_assert (r <= 31);
2389 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2392 dw2_asm_output_data (1, opc,
2393 "%s", dwarf_stack_op_name (opc));
2395 /* Output the operand(s) (if any). */
2396 output_loc_operands (loc, for_eh_or_skip);
2400 /* Output location description stack opcode's operands (if any).
2401 The output is single bytes on a line, suitable for .cfi_escape. */
2403 static void
2404 output_loc_operands_raw (dw_loc_descr_ref loc)
2406 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2407 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2409 switch (loc->dw_loc_opc)
2411 case DW_OP_addr:
2412 case DW_OP_GNU_addr_index:
2413 case DW_OP_GNU_const_index:
2414 case DW_OP_implicit_value:
2415 /* We cannot output addresses in .cfi_escape, only bytes. */
2416 gcc_unreachable ();
2418 case DW_OP_const1u:
2419 case DW_OP_const1s:
2420 case DW_OP_pick:
2421 case DW_OP_deref_size:
2422 case DW_OP_xderef_size:
2423 fputc (',', asm_out_file);
2424 dw2_asm_output_data_raw (1, val1->v.val_int);
2425 break;
2427 case DW_OP_const2u:
2428 case DW_OP_const2s:
2429 fputc (',', asm_out_file);
2430 dw2_asm_output_data_raw (2, val1->v.val_int);
2431 break;
2433 case DW_OP_const4u:
2434 case DW_OP_const4s:
2435 fputc (',', asm_out_file);
2436 dw2_asm_output_data_raw (4, val1->v.val_int);
2437 break;
2439 case DW_OP_const8u:
2440 case DW_OP_const8s:
2441 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2442 fputc (',', asm_out_file);
2443 dw2_asm_output_data_raw (8, val1->v.val_int);
2444 break;
2446 case DW_OP_skip:
2447 case DW_OP_bra:
2449 int offset;
2451 gcc_assert (val1->val_class == dw_val_class_loc);
2452 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2454 fputc (',', asm_out_file);
2455 dw2_asm_output_data_raw (2, offset);
2457 break;
2459 case DW_OP_regx:
2461 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2462 gcc_assert (size_of_uleb128 (r)
2463 == size_of_uleb128 (val1->v.val_unsigned));
2464 fputc (',', asm_out_file);
2465 dw2_asm_output_data_uleb128_raw (r);
2467 break;
2469 case DW_OP_constu:
2470 case DW_OP_plus_uconst:
2471 case DW_OP_piece:
2472 fputc (',', asm_out_file);
2473 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2474 break;
2476 case DW_OP_bit_piece:
2477 fputc (',', asm_out_file);
2478 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2479 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2480 break;
2482 case DW_OP_consts:
2483 case DW_OP_breg0:
2484 case DW_OP_breg1:
2485 case DW_OP_breg2:
2486 case DW_OP_breg3:
2487 case DW_OP_breg4:
2488 case DW_OP_breg5:
2489 case DW_OP_breg6:
2490 case DW_OP_breg7:
2491 case DW_OP_breg8:
2492 case DW_OP_breg9:
2493 case DW_OP_breg10:
2494 case DW_OP_breg11:
2495 case DW_OP_breg12:
2496 case DW_OP_breg13:
2497 case DW_OP_breg14:
2498 case DW_OP_breg15:
2499 case DW_OP_breg16:
2500 case DW_OP_breg17:
2501 case DW_OP_breg18:
2502 case DW_OP_breg19:
2503 case DW_OP_breg20:
2504 case DW_OP_breg21:
2505 case DW_OP_breg22:
2506 case DW_OP_breg23:
2507 case DW_OP_breg24:
2508 case DW_OP_breg25:
2509 case DW_OP_breg26:
2510 case DW_OP_breg27:
2511 case DW_OP_breg28:
2512 case DW_OP_breg29:
2513 case DW_OP_breg30:
2514 case DW_OP_breg31:
2515 case DW_OP_fbreg:
2516 fputc (',', asm_out_file);
2517 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2518 break;
2520 case DW_OP_bregx:
2522 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2523 gcc_assert (size_of_uleb128 (r)
2524 == size_of_uleb128 (val1->v.val_unsigned));
2525 fputc (',', asm_out_file);
2526 dw2_asm_output_data_uleb128_raw (r);
2527 fputc (',', asm_out_file);
2528 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2530 break;
2532 case DW_OP_implicit_pointer:
2533 case DW_OP_entry_value:
2534 case DW_OP_const_type:
2535 case DW_OP_regval_type:
2536 case DW_OP_deref_type:
2537 case DW_OP_convert:
2538 case DW_OP_reinterpret:
2539 case DW_OP_GNU_implicit_pointer:
2540 case DW_OP_GNU_entry_value:
2541 case DW_OP_GNU_const_type:
2542 case DW_OP_GNU_regval_type:
2543 case DW_OP_GNU_deref_type:
2544 case DW_OP_GNU_convert:
2545 case DW_OP_GNU_reinterpret:
2546 case DW_OP_GNU_parameter_ref:
2547 gcc_unreachable ();
2548 break;
2550 default:
2551 /* Other codes have no operands. */
2552 break;
2556 void
2557 output_loc_sequence_raw (dw_loc_descr_ref loc)
2559 while (1)
2561 enum dwarf_location_atom opc = loc->dw_loc_opc;
2562 /* Output the opcode. */
2563 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2565 unsigned r = (opc - DW_OP_breg0);
2566 r = DWARF2_FRAME_REG_OUT (r, 1);
2567 gcc_assert (r <= 31);
2568 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2570 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2572 unsigned r = (opc - DW_OP_reg0);
2573 r = DWARF2_FRAME_REG_OUT (r, 1);
2574 gcc_assert (r <= 31);
2575 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2577 /* Output the opcode. */
2578 fprintf (asm_out_file, "%#x", opc);
2579 output_loc_operands_raw (loc);
2581 if (!loc->dw_loc_next)
2582 break;
2583 loc = loc->dw_loc_next;
2585 fputc (',', asm_out_file);
2589 /* This function builds a dwarf location descriptor sequence from a
2590 dw_cfa_location, adding the given OFFSET to the result of the
2591 expression. */
2593 struct dw_loc_descr_node *
2594 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2596 struct dw_loc_descr_node *head, *tmp;
2598 offset += cfa->offset;
2600 if (cfa->indirect)
2602 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2603 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2604 head->dw_loc_oprnd1.val_entry = NULL;
2605 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2606 add_loc_descr (&head, tmp);
2607 if (offset != 0)
2609 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2610 add_loc_descr (&head, tmp);
2613 else
2614 head = new_reg_loc_descr (cfa->reg, offset);
2616 return head;
2619 /* This function builds a dwarf location descriptor sequence for
2620 the address at OFFSET from the CFA when stack is aligned to
2621 ALIGNMENT byte. */
2623 struct dw_loc_descr_node *
2624 build_cfa_aligned_loc (dw_cfa_location *cfa,
2625 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2627 struct dw_loc_descr_node *head;
2628 unsigned int dwarf_fp
2629 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2631 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2632 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2634 head = new_reg_loc_descr (dwarf_fp, 0);
2635 add_loc_descr (&head, int_loc_descriptor (alignment));
2636 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2637 loc_descr_plus_const (&head, offset);
2639 else
2640 head = new_reg_loc_descr (dwarf_fp, offset);
2641 return head;
2644 /* And now, the support for symbolic debugging information. */
2646 /* .debug_str support. */
2648 static void dwarf2out_init (const char *);
2649 static void dwarf2out_finish (const char *);
2650 static void dwarf2out_early_finish (const char *);
2651 static void dwarf2out_assembly_start (void);
2652 static void dwarf2out_define (unsigned int, const char *);
2653 static void dwarf2out_undef (unsigned int, const char *);
2654 static void dwarf2out_start_source_file (unsigned, const char *);
2655 static void dwarf2out_end_source_file (unsigned);
2656 static void dwarf2out_function_decl (tree);
2657 static void dwarf2out_begin_block (unsigned, unsigned);
2658 static void dwarf2out_end_block (unsigned, unsigned);
2659 static bool dwarf2out_ignore_block (const_tree);
2660 static void dwarf2out_early_global_decl (tree);
2661 static void dwarf2out_late_global_decl (tree);
2662 static void dwarf2out_type_decl (tree, int);
2663 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2664 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2665 dw_die_ref);
2666 static void dwarf2out_abstract_function (tree);
2667 static void dwarf2out_var_location (rtx_insn *);
2668 static void dwarf2out_size_function (tree);
2669 static void dwarf2out_begin_function (tree);
2670 static void dwarf2out_end_function (unsigned int);
2671 static void dwarf2out_register_main_translation_unit (tree unit);
2672 static void dwarf2out_set_name (tree, tree);
2674 /* The debug hooks structure. */
2676 const struct gcc_debug_hooks dwarf2_debug_hooks =
2678 dwarf2out_init,
2679 dwarf2out_finish,
2680 dwarf2out_early_finish,
2681 dwarf2out_assembly_start,
2682 dwarf2out_define,
2683 dwarf2out_undef,
2684 dwarf2out_start_source_file,
2685 dwarf2out_end_source_file,
2686 dwarf2out_begin_block,
2687 dwarf2out_end_block,
2688 dwarf2out_ignore_block,
2689 dwarf2out_source_line,
2690 dwarf2out_begin_prologue,
2691 #if VMS_DEBUGGING_INFO
2692 dwarf2out_vms_end_prologue,
2693 dwarf2out_vms_begin_epilogue,
2694 #else
2695 debug_nothing_int_charstar,
2696 debug_nothing_int_charstar,
2697 #endif
2698 dwarf2out_end_epilogue,
2699 dwarf2out_begin_function,
2700 dwarf2out_end_function, /* end_function */
2701 dwarf2out_register_main_translation_unit,
2702 dwarf2out_function_decl, /* function_decl */
2703 dwarf2out_early_global_decl,
2704 dwarf2out_late_global_decl,
2705 dwarf2out_type_decl, /* type_decl */
2706 dwarf2out_imported_module_or_decl,
2707 debug_nothing_tree, /* deferred_inline_function */
2708 /* The DWARF 2 backend tries to reduce debugging bloat by not
2709 emitting the abstract description of inline functions until
2710 something tries to reference them. */
2711 dwarf2out_abstract_function, /* outlining_inline_function */
2712 debug_nothing_rtx_code_label, /* label */
2713 debug_nothing_int, /* handle_pch */
2714 dwarf2out_var_location,
2715 dwarf2out_size_function, /* size_function */
2716 dwarf2out_switch_text_section,
2717 dwarf2out_set_name,
2718 1, /* start_end_main_source_file */
2719 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2722 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2724 dwarf2out_init,
2725 debug_nothing_charstar,
2726 debug_nothing_charstar,
2727 dwarf2out_assembly_start,
2728 debug_nothing_int_charstar,
2729 debug_nothing_int_charstar,
2730 debug_nothing_int_charstar,
2731 debug_nothing_int,
2732 debug_nothing_int_int, /* begin_block */
2733 debug_nothing_int_int, /* end_block */
2734 debug_true_const_tree, /* ignore_block */
2735 dwarf2out_source_line, /* source_line */
2736 debug_nothing_int_charstar, /* begin_prologue */
2737 debug_nothing_int_charstar, /* end_prologue */
2738 debug_nothing_int_charstar, /* begin_epilogue */
2739 debug_nothing_int_charstar, /* end_epilogue */
2740 debug_nothing_tree, /* begin_function */
2741 debug_nothing_int, /* end_function */
2742 debug_nothing_tree, /* register_main_translation_unit */
2743 debug_nothing_tree, /* function_decl */
2744 debug_nothing_tree, /* early_global_decl */
2745 debug_nothing_tree, /* late_global_decl */
2746 debug_nothing_tree_int, /* type_decl */
2747 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2748 debug_nothing_tree, /* deferred_inline_function */
2749 debug_nothing_tree, /* outlining_inline_function */
2750 debug_nothing_rtx_code_label, /* label */
2751 debug_nothing_int, /* handle_pch */
2752 debug_nothing_rtx_insn, /* var_location */
2753 debug_nothing_tree, /* size_function */
2754 debug_nothing_void, /* switch_text_section */
2755 debug_nothing_tree_tree, /* set_name */
2756 0, /* start_end_main_source_file */
2757 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2760 /* NOTE: In the comments in this file, many references are made to
2761 "Debugging Information Entries". This term is abbreviated as `DIE'
2762 throughout the remainder of this file. */
2764 /* An internal representation of the DWARF output is built, and then
2765 walked to generate the DWARF debugging info. The walk of the internal
2766 representation is done after the entire program has been compiled.
2767 The types below are used to describe the internal representation. */
2769 /* Whether to put type DIEs into their own section .debug_types instead
2770 of making them part of the .debug_info section. Only supported for
2771 Dwarf V4 or higher and the user didn't disable them through
2772 -fno-debug-types-section. It is more efficient to put them in a
2773 separate comdat sections since the linker will then be able to
2774 remove duplicates. But not all tools support .debug_types sections
2775 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2776 it is DW_UT_type unit type in .debug_info section. */
2778 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2780 /* Various DIE's use offsets relative to the beginning of the
2781 .debug_info section to refer to each other. */
2783 typedef long int dw_offset;
2785 struct comdat_type_node;
2787 /* The entries in the line_info table more-or-less mirror the opcodes
2788 that are used in the real dwarf line table. Arrays of these entries
2789 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2790 supported. */
2792 enum dw_line_info_opcode {
2793 /* Emit DW_LNE_set_address; the operand is the label index. */
2794 LI_set_address,
2796 /* Emit a row to the matrix with the given line. This may be done
2797 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2798 special opcodes. */
2799 LI_set_line,
2801 /* Emit a DW_LNS_set_file. */
2802 LI_set_file,
2804 /* Emit a DW_LNS_set_column. */
2805 LI_set_column,
2807 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2808 LI_negate_stmt,
2810 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2811 LI_set_prologue_end,
2812 LI_set_epilogue_begin,
2814 /* Emit a DW_LNE_set_discriminator. */
2815 LI_set_discriminator
2818 typedef struct GTY(()) dw_line_info_struct {
2819 enum dw_line_info_opcode opcode;
2820 unsigned int val;
2821 } dw_line_info_entry;
2824 struct GTY(()) dw_line_info_table {
2825 /* The label that marks the end of this section. */
2826 const char *end_label;
2828 /* The values for the last row of the matrix, as collected in the table.
2829 These are used to minimize the changes to the next row. */
2830 unsigned int file_num;
2831 unsigned int line_num;
2832 unsigned int column_num;
2833 int discrim_num;
2834 bool is_stmt;
2835 bool in_use;
2837 vec<dw_line_info_entry, va_gc> *entries;
2841 /* Each DIE attribute has a field specifying the attribute kind,
2842 a link to the next attribute in the chain, and an attribute value.
2843 Attributes are typically linked below the DIE they modify. */
2845 typedef struct GTY(()) dw_attr_struct {
2846 enum dwarf_attribute dw_attr;
2847 dw_val_node dw_attr_val;
2849 dw_attr_node;
2852 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2853 The children of each node form a circular list linked by
2854 die_sib. die_child points to the node *before* the "first" child node. */
2856 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2857 union die_symbol_or_type_node
2859 const char * GTY ((tag ("0"))) die_symbol;
2860 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2862 GTY ((desc ("%0.comdat_type_p"))) die_id;
2863 vec<dw_attr_node, va_gc> *die_attr;
2864 dw_die_ref die_parent;
2865 dw_die_ref die_child;
2866 dw_die_ref die_sib;
2867 dw_die_ref die_definition; /* ref from a specification to its definition */
2868 dw_offset die_offset;
2869 unsigned long die_abbrev;
2870 int die_mark;
2871 unsigned int decl_id;
2872 enum dwarf_tag die_tag;
2873 /* Die is used and must not be pruned as unused. */
2874 BOOL_BITFIELD die_perennial_p : 1;
2875 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2876 /* Whether this DIE was removed from the DIE tree, for example via
2877 prune_unused_types. We don't consider those present from the
2878 DIE lookup routines. */
2879 BOOL_BITFIELD removed : 1;
2880 /* Lots of spare bits. */
2882 die_node;
2884 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2885 static bool early_dwarf;
2886 static bool early_dwarf_finished;
2887 struct set_early_dwarf {
2888 bool saved;
2889 set_early_dwarf () : saved(early_dwarf)
2891 gcc_assert (! early_dwarf_finished);
2892 early_dwarf = true;
2894 ~set_early_dwarf () { early_dwarf = saved; }
2897 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2898 #define FOR_EACH_CHILD(die, c, expr) do { \
2899 c = die->die_child; \
2900 if (c) do { \
2901 c = c->die_sib; \
2902 expr; \
2903 } while (c != die->die_child); \
2904 } while (0)
2906 /* The pubname structure */
2908 typedef struct GTY(()) pubname_struct {
2909 dw_die_ref die;
2910 const char *name;
2912 pubname_entry;
2915 struct GTY(()) dw_ranges {
2916 const char *label;
2917 /* If this is positive, it's a block number, otherwise it's a
2918 bitwise-negated index into dw_ranges_by_label. */
2919 int num;
2920 /* Index for the range list for DW_FORM_rnglistx. */
2921 unsigned int idx : 31;
2922 /* True if this range might be possibly in a different section
2923 from previous entry. */
2924 unsigned int maybe_new_sec : 1;
2927 /* A structure to hold a macinfo entry. */
2929 typedef struct GTY(()) macinfo_struct {
2930 unsigned char code;
2931 unsigned HOST_WIDE_INT lineno;
2932 const char *info;
2934 macinfo_entry;
2937 struct GTY(()) dw_ranges_by_label {
2938 const char *begin;
2939 const char *end;
2942 /* The comdat type node structure. */
2943 struct GTY(()) comdat_type_node
2945 dw_die_ref root_die;
2946 dw_die_ref type_die;
2947 dw_die_ref skeleton_die;
2948 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2949 comdat_type_node *next;
2952 /* A list of DIEs for which we can't determine ancestry (parent_die
2953 field) just yet. Later in dwarf2out_finish we will fill in the
2954 missing bits. */
2955 typedef struct GTY(()) limbo_die_struct {
2956 dw_die_ref die;
2957 /* The tree for which this DIE was created. We use this to
2958 determine ancestry later. */
2959 tree created_for;
2960 struct limbo_die_struct *next;
2962 limbo_die_node;
2964 typedef struct skeleton_chain_struct
2966 dw_die_ref old_die;
2967 dw_die_ref new_die;
2968 struct skeleton_chain_struct *parent;
2970 skeleton_chain_node;
2972 /* Define a macro which returns nonzero for a TYPE_DECL which was
2973 implicitly generated for a type.
2975 Note that, unlike the C front-end (which generates a NULL named
2976 TYPE_DECL node for each complete tagged type, each array type,
2977 and each function type node created) the C++ front-end generates
2978 a _named_ TYPE_DECL node for each tagged type node created.
2979 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2980 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2981 front-end, but for each type, tagged or not. */
2983 #define TYPE_DECL_IS_STUB(decl) \
2984 (DECL_NAME (decl) == NULL_TREE \
2985 || (DECL_ARTIFICIAL (decl) \
2986 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2987 /* This is necessary for stub decls that \
2988 appear in nested inline functions. */ \
2989 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2990 && (decl_ultimate_origin (decl) \
2991 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2993 /* Information concerning the compilation unit's programming
2994 language, and compiler version. */
2996 /* Fixed size portion of the DWARF compilation unit header. */
2997 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2998 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
2999 + (dwarf_version >= 5 \
3000 ? 4 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE : 3))
3002 /* Fixed size portion of the DWARF comdat type unit header. */
3003 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3004 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3005 + (dwarf_version >= 5 \
3006 ? 0 : DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE))
3008 /* Fixed size portion of public names info. */
3009 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3011 /* Fixed size portion of the address range info. */
3012 #define DWARF_ARANGES_HEADER_SIZE \
3013 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3014 DWARF2_ADDR_SIZE * 2) \
3015 - DWARF_INITIAL_LENGTH_SIZE)
3017 /* Size of padding portion in the address range info. It must be
3018 aligned to twice the pointer size. */
3019 #define DWARF_ARANGES_PAD_SIZE \
3020 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3021 DWARF2_ADDR_SIZE * 2) \
3022 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3024 /* Use assembler line directives if available. */
3025 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3026 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3027 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3028 #else
3029 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3030 #endif
3031 #endif
3033 /* Minimum line offset in a special line info. opcode.
3034 This value was chosen to give a reasonable range of values. */
3035 #define DWARF_LINE_BASE -10
3037 /* First special line opcode - leave room for the standard opcodes. */
3038 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3040 /* Range of line offsets in a special line info. opcode. */
3041 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3043 /* Flag that indicates the initial value of the is_stmt_start flag.
3044 In the present implementation, we do not mark any lines as
3045 the beginning of a source statement, because that information
3046 is not made available by the GCC front-end. */
3047 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3049 /* Maximum number of operations per instruction bundle. */
3050 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3051 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3052 #endif
3054 /* This location is used by calc_die_sizes() to keep track
3055 the offset of each DIE within the .debug_info section. */
3056 static unsigned long next_die_offset;
3058 /* Record the root of the DIE's built for the current compilation unit. */
3059 static GTY(()) dw_die_ref single_comp_unit_die;
3061 /* A list of type DIEs that have been separated into comdat sections. */
3062 static GTY(()) comdat_type_node *comdat_type_list;
3064 /* A list of CU DIEs that have been separated. */
3065 static GTY(()) limbo_die_node *cu_die_list;
3067 /* A list of DIEs with a NULL parent waiting to be relocated. */
3068 static GTY(()) limbo_die_node *limbo_die_list;
3070 /* A list of DIEs for which we may have to generate
3071 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3072 static GTY(()) limbo_die_node *deferred_asm_name;
3074 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3076 typedef const char *compare_type;
3078 static hashval_t hash (dwarf_file_data *);
3079 static bool equal (dwarf_file_data *, const char *);
3082 /* Filenames referenced by this compilation unit. */
3083 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3085 struct decl_die_hasher : ggc_ptr_hash<die_node>
3087 typedef tree compare_type;
3089 static hashval_t hash (die_node *);
3090 static bool equal (die_node *, tree);
3092 /* A hash table of references to DIE's that describe declarations.
3093 The key is a DECL_UID() which is a unique number identifying each decl. */
3094 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3096 struct block_die_hasher : ggc_ptr_hash<die_struct>
3098 static hashval_t hash (die_struct *);
3099 static bool equal (die_struct *, die_struct *);
3102 /* A hash table of references to DIE's that describe COMMON blocks.
3103 The key is DECL_UID() ^ die_parent. */
3104 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3106 typedef struct GTY(()) die_arg_entry_struct {
3107 dw_die_ref die;
3108 tree arg;
3109 } die_arg_entry;
3112 /* Node of the variable location list. */
3113 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3114 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3115 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3116 in mode of the EXPR_LIST node and first EXPR_LIST operand
3117 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3118 location or NULL for padding. For larger bitsizes,
3119 mode is 0 and first operand is a CONCAT with bitsize
3120 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3121 NULL as second operand. */
3122 rtx GTY (()) loc;
3123 const char * GTY (()) label;
3124 struct var_loc_node * GTY (()) next;
3127 /* Variable location list. */
3128 struct GTY ((for_user)) var_loc_list_def {
3129 struct var_loc_node * GTY (()) first;
3131 /* Pointer to the last but one or last element of the
3132 chained list. If the list is empty, both first and
3133 last are NULL, if the list contains just one node
3134 or the last node certainly is not redundant, it points
3135 to the last node, otherwise points to the last but one.
3136 Do not mark it for GC because it is marked through the chain. */
3137 struct var_loc_node * GTY ((skip ("%h"))) last;
3139 /* Pointer to the last element before section switch,
3140 if NULL, either sections weren't switched or first
3141 is after section switch. */
3142 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3144 /* DECL_UID of the variable decl. */
3145 unsigned int decl_id;
3147 typedef struct var_loc_list_def var_loc_list;
3149 /* Call argument location list. */
3150 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3151 rtx GTY (()) call_arg_loc_note;
3152 const char * GTY (()) label;
3153 tree GTY (()) block;
3154 bool tail_call_p;
3155 rtx GTY (()) symbol_ref;
3156 struct call_arg_loc_node * GTY (()) next;
3160 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3162 typedef const_tree compare_type;
3164 static hashval_t hash (var_loc_list *);
3165 static bool equal (var_loc_list *, const_tree);
3168 /* Table of decl location linked lists. */
3169 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3171 /* Head and tail of call_arg_loc chain. */
3172 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3173 static struct call_arg_loc_node *call_arg_loc_last;
3175 /* Number of call sites in the current function. */
3176 static int call_site_count = -1;
3177 /* Number of tail call sites in the current function. */
3178 static int tail_call_site_count = -1;
3180 /* A cached location list. */
3181 struct GTY ((for_user)) cached_dw_loc_list_def {
3182 /* The DECL_UID of the decl that this entry describes. */
3183 unsigned int decl_id;
3185 /* The cached location list. */
3186 dw_loc_list_ref loc_list;
3188 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3190 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3193 typedef const_tree compare_type;
3195 static hashval_t hash (cached_dw_loc_list *);
3196 static bool equal (cached_dw_loc_list *, const_tree);
3199 /* Table of cached location lists. */
3200 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3202 /* A vector of references to DIE's that are uniquely identified by their tag,
3203 presence/absence of children DIE's, and list of attribute/value pairs. */
3204 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3206 /* A hash map to remember the stack usage for DWARF procedures. The value
3207 stored is the stack size difference between before the DWARF procedure
3208 invokation and after it returned. In other words, for a DWARF procedure
3209 that consumes N stack slots and that pushes M ones, this stores M - N. */
3210 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3212 /* A global counter for generating labels for line number data. */
3213 static unsigned int line_info_label_num;
3215 /* The current table to which we should emit line number information
3216 for the current function. This will be set up at the beginning of
3217 assembly for the function. */
3218 static GTY(()) dw_line_info_table *cur_line_info_table;
3220 /* The two default tables of line number info. */
3221 static GTY(()) dw_line_info_table *text_section_line_info;
3222 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3224 /* The set of all non-default tables of line number info. */
3225 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3227 /* A flag to tell pubnames/types export if there is an info section to
3228 refer to. */
3229 static bool info_section_emitted;
3231 /* A pointer to the base of a table that contains a list of publicly
3232 accessible names. */
3233 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3235 /* A pointer to the base of a table that contains a list of publicly
3236 accessible types. */
3237 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3239 /* A pointer to the base of a table that contains a list of macro
3240 defines/undefines (and file start/end markers). */
3241 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3243 /* True if .debug_macinfo or .debug_macros section is going to be
3244 emitted. */
3245 #define have_macinfo \
3246 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3247 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3248 && !macinfo_table->is_empty ())
3250 /* Vector of dies for which we should generate .debug_ranges info. */
3251 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3253 /* Vector of pairs of labels referenced in ranges_table. */
3254 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3256 /* Whether we have location lists that need outputting */
3257 static GTY(()) bool have_location_lists;
3259 /* Unique label counter. */
3260 static GTY(()) unsigned int loclabel_num;
3262 /* Unique label counter for point-of-call tables. */
3263 static GTY(()) unsigned int poc_label_num;
3265 /* The last file entry emitted by maybe_emit_file(). */
3266 static GTY(()) struct dwarf_file_data * last_emitted_file;
3268 /* Number of internal labels generated by gen_internal_sym(). */
3269 static GTY(()) int label_num;
3271 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3273 /* Instances of generic types for which we need to generate debug
3274 info that describe their generic parameters and arguments. That
3275 generation needs to happen once all types are properly laid out so
3276 we do it at the end of compilation. */
3277 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3279 /* Offset from the "steady-state frame pointer" to the frame base,
3280 within the current function. */
3281 static HOST_WIDE_INT frame_pointer_fb_offset;
3282 static bool frame_pointer_fb_offset_valid;
3284 static vec<dw_die_ref> base_types;
3286 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3287 once all arguments are parsed. */
3288 static vec<dw_die_ref> *string_types;
3290 /* Flags to represent a set of attribute classes for attributes that represent
3291 a scalar value (bounds, pointers, ...). */
3292 enum dw_scalar_form
3294 dw_scalar_form_constant = 0x01,
3295 dw_scalar_form_exprloc = 0x02,
3296 dw_scalar_form_reference = 0x04
3299 /* Forward declarations for functions defined in this file. */
3301 static int is_pseudo_reg (const_rtx);
3302 static tree type_main_variant (tree);
3303 static int is_tagged_type (const_tree);
3304 static const char *dwarf_tag_name (unsigned);
3305 static const char *dwarf_attr_name (unsigned);
3306 static const char *dwarf_form_name (unsigned);
3307 static tree decl_ultimate_origin (const_tree);
3308 static tree decl_class_context (tree);
3309 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3310 static inline enum dw_val_class AT_class (dw_attr_node *);
3311 static inline unsigned int AT_index (dw_attr_node *);
3312 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3313 static inline unsigned AT_flag (dw_attr_node *);
3314 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3315 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3316 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3317 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3318 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3319 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3320 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3321 unsigned int, unsigned char *);
3322 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3323 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3324 static inline const char *AT_string (dw_attr_node *);
3325 static enum dwarf_form AT_string_form (dw_attr_node *);
3326 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3327 static void add_AT_specification (dw_die_ref, dw_die_ref);
3328 static inline dw_die_ref AT_ref (dw_attr_node *);
3329 static inline int AT_ref_external (dw_attr_node *);
3330 static inline void set_AT_ref_external (dw_attr_node *, int);
3331 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3332 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3333 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3334 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3335 dw_loc_list_ref);
3336 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3337 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3338 static void remove_addr_table_entry (addr_table_entry *);
3339 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3340 static inline rtx AT_addr (dw_attr_node *);
3341 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3342 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3343 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3344 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3345 const char *);
3346 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3347 unsigned HOST_WIDE_INT);
3348 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3349 unsigned long, bool);
3350 static inline const char *AT_lbl (dw_attr_node *);
3351 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3352 static const char *get_AT_low_pc (dw_die_ref);
3353 static const char *get_AT_hi_pc (dw_die_ref);
3354 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3355 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3356 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3357 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3358 static bool is_cxx (void);
3359 static bool is_fortran (void);
3360 static bool is_ada (void);
3361 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3362 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3363 static void add_child_die (dw_die_ref, dw_die_ref);
3364 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3365 static dw_die_ref lookup_type_die (tree);
3366 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3367 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3368 static void equate_type_number_to_die (tree, dw_die_ref);
3369 static dw_die_ref lookup_decl_die (tree);
3370 static var_loc_list *lookup_decl_loc (const_tree);
3371 static void equate_decl_number_to_die (tree, dw_die_ref);
3372 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3373 static void print_spaces (FILE *);
3374 static void print_die (dw_die_ref, FILE *);
3375 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3376 static dw_die_ref pop_compile_unit (dw_die_ref);
3377 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3378 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3379 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3380 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3381 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3382 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3383 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3384 struct md5_ctx *, int *);
3385 struct checksum_attributes;
3386 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3387 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3388 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3389 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3390 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3391 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3392 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3393 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3394 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3395 static void compute_section_prefix (dw_die_ref);
3396 static int is_type_die (dw_die_ref);
3397 static int is_comdat_die (dw_die_ref);
3398 static int is_symbol_die (dw_die_ref);
3399 static inline bool is_template_instantiation (dw_die_ref);
3400 static void assign_symbol_names (dw_die_ref);
3401 static void break_out_includes (dw_die_ref);
3402 static int is_declaration_die (dw_die_ref);
3403 static int should_move_die_to_comdat (dw_die_ref);
3404 static dw_die_ref clone_as_declaration (dw_die_ref);
3405 static dw_die_ref clone_die (dw_die_ref);
3406 static dw_die_ref clone_tree (dw_die_ref);
3407 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3408 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3409 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3410 static dw_die_ref generate_skeleton (dw_die_ref);
3411 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3412 dw_die_ref,
3413 dw_die_ref);
3414 static void break_out_comdat_types (dw_die_ref);
3415 static void copy_decls_for_unworthy_types (dw_die_ref);
3417 static void add_sibling_attributes (dw_die_ref);
3418 static void output_location_lists (dw_die_ref);
3419 static int constant_size (unsigned HOST_WIDE_INT);
3420 static unsigned long size_of_die (dw_die_ref);
3421 static void calc_die_sizes (dw_die_ref);
3422 static void calc_base_type_die_sizes (void);
3423 static void mark_dies (dw_die_ref);
3424 static void unmark_dies (dw_die_ref);
3425 static void unmark_all_dies (dw_die_ref);
3426 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3427 static unsigned long size_of_aranges (void);
3428 static enum dwarf_form value_format (dw_attr_node *);
3429 static void output_value_format (dw_attr_node *);
3430 static void output_abbrev_section (void);
3431 static void output_die_abbrevs (unsigned long, dw_die_ref);
3432 static void output_die_symbol (dw_die_ref);
3433 static void output_die (dw_die_ref);
3434 static void output_compilation_unit_header (enum dwarf_unit_type);
3435 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3436 static void output_comdat_type_unit (comdat_type_node *);
3437 static const char *dwarf2_name (tree, int);
3438 static void add_pubname (tree, dw_die_ref);
3439 static void add_enumerator_pubname (const char *, dw_die_ref);
3440 static void add_pubname_string (const char *, dw_die_ref);
3441 static void add_pubtype (tree, dw_die_ref);
3442 static void output_pubnames (vec<pubname_entry, va_gc> *);
3443 static void output_aranges (void);
3444 static unsigned int add_ranges (const_tree, bool = false);
3445 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3446 bool *, bool);
3447 static void output_ranges (void);
3448 static dw_line_info_table *new_line_info_table (void);
3449 static void output_line_info (bool);
3450 static void output_file_names (void);
3451 static dw_die_ref base_type_die (tree, bool);
3452 static int is_base_type (tree);
3453 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3454 static int decl_quals (const_tree);
3455 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3456 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3457 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3458 static int type_is_enum (const_tree);
3459 static unsigned int dbx_reg_number (const_rtx);
3460 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3461 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3462 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3463 enum var_init_status);
3464 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3465 enum var_init_status);
3466 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3467 enum var_init_status);
3468 static int is_based_loc (const_rtx);
3469 static bool resolve_one_addr (rtx *);
3470 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3471 enum var_init_status);
3472 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3473 enum var_init_status);
3474 struct loc_descr_context;
3475 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3476 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3477 static dw_loc_list_ref loc_list_from_tree (tree, int,
3478 struct loc_descr_context *);
3479 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3480 struct loc_descr_context *);
3481 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3482 static tree field_type (const_tree);
3483 static unsigned int simple_type_align_in_bits (const_tree);
3484 static unsigned int simple_decl_align_in_bits (const_tree);
3485 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3486 struct vlr_context;
3487 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3488 HOST_WIDE_INT *);
3489 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3490 dw_loc_list_ref);
3491 static void add_data_member_location_attribute (dw_die_ref, tree,
3492 struct vlr_context *);
3493 static bool add_const_value_attribute (dw_die_ref, rtx);
3494 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3495 static void insert_wide_int (const wide_int &, unsigned char *, int);
3496 static void insert_float (const_rtx, unsigned char *);
3497 static rtx rtl_for_decl_location (tree);
3498 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3499 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3500 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3501 static void add_name_attribute (dw_die_ref, const char *);
3502 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3503 static void add_comp_dir_attribute (dw_die_ref);
3504 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3505 struct loc_descr_context *);
3506 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3507 struct loc_descr_context *);
3508 static void add_subscript_info (dw_die_ref, tree, bool);
3509 static void add_byte_size_attribute (dw_die_ref, tree);
3510 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3511 struct vlr_context *);
3512 static void add_bit_size_attribute (dw_die_ref, tree);
3513 static void add_prototyped_attribute (dw_die_ref, tree);
3514 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3515 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3516 static void add_src_coords_attributes (dw_die_ref, tree);
3517 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3518 static void add_discr_value (dw_die_ref, dw_discr_value *);
3519 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3520 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3521 static void push_decl_scope (tree);
3522 static void pop_decl_scope (void);
3523 static dw_die_ref scope_die_for (tree, dw_die_ref);
3524 static inline int local_scope_p (dw_die_ref);
3525 static inline int class_scope_p (dw_die_ref);
3526 static inline int class_or_namespace_scope_p (dw_die_ref);
3527 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3528 static void add_calling_convention_attribute (dw_die_ref, tree);
3529 static const char *type_tag (const_tree);
3530 static tree member_declared_type (const_tree);
3531 #if 0
3532 static const char *decl_start_label (tree);
3533 #endif
3534 static void gen_array_type_die (tree, dw_die_ref);
3535 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3536 #if 0
3537 static void gen_entry_point_die (tree, dw_die_ref);
3538 #endif
3539 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3540 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3541 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3542 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3543 static void gen_formal_types_die (tree, dw_die_ref);
3544 static void gen_subprogram_die (tree, dw_die_ref);
3545 static void gen_variable_die (tree, tree, dw_die_ref);
3546 static void gen_const_die (tree, dw_die_ref);
3547 static void gen_label_die (tree, dw_die_ref);
3548 static void gen_lexical_block_die (tree, dw_die_ref);
3549 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3550 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3551 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3552 static dw_die_ref gen_compile_unit_die (const char *);
3553 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3554 static void gen_member_die (tree, dw_die_ref);
3555 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3556 enum debug_info_usage);
3557 static void gen_subroutine_type_die (tree, dw_die_ref);
3558 static void gen_typedef_die (tree, dw_die_ref);
3559 static void gen_type_die (tree, dw_die_ref);
3560 static void gen_block_die (tree, dw_die_ref);
3561 static void decls_for_scope (tree, dw_die_ref);
3562 static bool is_naming_typedef_decl (const_tree);
3563 static inline dw_die_ref get_context_die (tree);
3564 static void gen_namespace_die (tree, dw_die_ref);
3565 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3566 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3567 static dw_die_ref force_decl_die (tree);
3568 static dw_die_ref force_type_die (tree);
3569 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3570 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3571 static struct dwarf_file_data * lookup_filename (const char *);
3572 static void retry_incomplete_types (void);
3573 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3574 static void gen_generic_params_dies (tree);
3575 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3576 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3577 static void splice_child_die (dw_die_ref, dw_die_ref);
3578 static int file_info_cmp (const void *, const void *);
3579 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3580 const char *, const char *);
3581 static void output_loc_list (dw_loc_list_ref);
3582 static char *gen_internal_sym (const char *);
3583 static bool want_pubnames (void);
3585 static void prune_unmark_dies (dw_die_ref);
3586 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3587 static void prune_unused_types_mark (dw_die_ref, int);
3588 static void prune_unused_types_walk (dw_die_ref);
3589 static void prune_unused_types_walk_attribs (dw_die_ref);
3590 static void prune_unused_types_prune (dw_die_ref);
3591 static void prune_unused_types (void);
3592 static int maybe_emit_file (struct dwarf_file_data *fd);
3593 static inline const char *AT_vms_delta1 (dw_attr_node *);
3594 static inline const char *AT_vms_delta2 (dw_attr_node *);
3595 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3596 const char *, const char *);
3597 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3598 static void gen_remaining_tmpl_value_param_die_attribute (void);
3599 static bool generic_type_p (tree);
3600 static void schedule_generic_params_dies_gen (tree t);
3601 static void gen_scheduled_generic_parms_dies (void);
3603 static const char *comp_dir_string (void);
3605 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3607 /* enum for tracking thread-local variables whose address is really an offset
3608 relative to the TLS pointer, which will need link-time relocation, but will
3609 not need relocation by the DWARF consumer. */
3611 enum dtprel_bool
3613 dtprel_false = 0,
3614 dtprel_true = 1
3617 /* Return the operator to use for an address of a variable. For dtprel_true, we
3618 use DW_OP_const*. For regular variables, which need both link-time
3619 relocation and consumer-level relocation (e.g., to account for shared objects
3620 loaded at a random address), we use DW_OP_addr*. */
3622 static inline enum dwarf_location_atom
3623 dw_addr_op (enum dtprel_bool dtprel)
3625 if (dtprel == dtprel_true)
3626 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3627 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3628 else
3629 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3632 /* Return a pointer to a newly allocated address location description. If
3633 dwarf_split_debug_info is true, then record the address with the appropriate
3634 relocation. */
3635 static inline dw_loc_descr_ref
3636 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3638 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3640 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3641 ref->dw_loc_oprnd1.v.val_addr = addr;
3642 ref->dtprel = dtprel;
3643 if (dwarf_split_debug_info)
3644 ref->dw_loc_oprnd1.val_entry
3645 = add_addr_table_entry (addr,
3646 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3647 else
3648 ref->dw_loc_oprnd1.val_entry = NULL;
3650 return ref;
3653 /* Section names used to hold DWARF debugging information. */
3655 #ifndef DEBUG_INFO_SECTION
3656 #define DEBUG_INFO_SECTION ".debug_info"
3657 #endif
3658 #ifndef DEBUG_DWO_INFO_SECTION
3659 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3660 #endif
3661 #ifndef DEBUG_ABBREV_SECTION
3662 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3663 #endif
3664 #ifndef DEBUG_DWO_ABBREV_SECTION
3665 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3666 #endif
3667 #ifndef DEBUG_ARANGES_SECTION
3668 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3669 #endif
3670 #ifndef DEBUG_ADDR_SECTION
3671 #define DEBUG_ADDR_SECTION ".debug_addr"
3672 #endif
3673 #ifndef DEBUG_MACINFO_SECTION
3674 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3675 #endif
3676 #ifndef DEBUG_DWO_MACINFO_SECTION
3677 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3678 #endif
3679 #ifndef DEBUG_DWO_MACRO_SECTION
3680 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3681 #endif
3682 #ifndef DEBUG_MACRO_SECTION
3683 #define DEBUG_MACRO_SECTION ".debug_macro"
3684 #endif
3685 #ifndef DEBUG_LINE_SECTION
3686 #define DEBUG_LINE_SECTION ".debug_line"
3687 #endif
3688 #ifndef DEBUG_DWO_LINE_SECTION
3689 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3690 #endif
3691 #ifndef DEBUG_LOC_SECTION
3692 #define DEBUG_LOC_SECTION ".debug_loc"
3693 #endif
3694 #ifndef DEBUG_DWO_LOC_SECTION
3695 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3696 #endif
3697 #ifndef DEBUG_LOCLISTS_SECTION
3698 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3699 #endif
3700 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3701 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3702 #endif
3703 #ifndef DEBUG_PUBNAMES_SECTION
3704 #define DEBUG_PUBNAMES_SECTION \
3705 ((debug_generate_pub_sections == 2) \
3706 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3707 #endif
3708 #ifndef DEBUG_PUBTYPES_SECTION
3709 #define DEBUG_PUBTYPES_SECTION \
3710 ((debug_generate_pub_sections == 2) \
3711 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3712 #endif
3713 #ifndef DEBUG_STR_OFFSETS_SECTION
3714 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3715 #endif
3716 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3717 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3718 #endif
3719 #ifndef DEBUG_STR_DWO_SECTION
3720 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3721 #endif
3722 #ifndef DEBUG_STR_SECTION
3723 #define DEBUG_STR_SECTION ".debug_str"
3724 #endif
3725 #ifndef DEBUG_RANGES_SECTION
3726 #define DEBUG_RANGES_SECTION ".debug_ranges"
3727 #endif
3728 #ifndef DEBUG_RNGLISTS_SECTION
3729 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3730 #endif
3731 #ifndef DEBUG_LINE_STR_SECTION
3732 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3733 #endif
3735 /* Standard ELF section names for compiled code and data. */
3736 #ifndef TEXT_SECTION_NAME
3737 #define TEXT_SECTION_NAME ".text"
3738 #endif
3740 /* Section flags for .debug_str section. */
3741 #define DEBUG_STR_SECTION_FLAGS \
3742 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3743 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3744 : SECTION_DEBUG)
3746 /* Section flags for .debug_str.dwo section. */
3747 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3749 /* Labels we insert at beginning sections we can reference instead of
3750 the section names themselves. */
3752 #ifndef TEXT_SECTION_LABEL
3753 #define TEXT_SECTION_LABEL "Ltext"
3754 #endif
3755 #ifndef COLD_TEXT_SECTION_LABEL
3756 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3757 #endif
3758 #ifndef DEBUG_LINE_SECTION_LABEL
3759 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3760 #endif
3761 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3762 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3763 #endif
3764 #ifndef DEBUG_INFO_SECTION_LABEL
3765 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3766 #endif
3767 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3768 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3769 #endif
3770 #ifndef DEBUG_ABBREV_SECTION_LABEL
3771 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3772 #endif
3773 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3774 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3775 #endif
3776 #ifndef DEBUG_ADDR_SECTION_LABEL
3777 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3778 #endif
3779 #ifndef DEBUG_LOC_SECTION_LABEL
3780 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3781 #endif
3782 #ifndef DEBUG_RANGES_SECTION_LABEL
3783 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3784 #endif
3785 #ifndef DEBUG_MACINFO_SECTION_LABEL
3786 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3787 #endif
3788 #ifndef DEBUG_MACRO_SECTION_LABEL
3789 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3790 #endif
3791 #define SKELETON_COMP_DIE_ABBREV 1
3792 #define SKELETON_TYPE_DIE_ABBREV 2
3794 /* Definitions of defaults for formats and names of various special
3795 (artificial) labels which may be generated within this file (when the -g
3796 options is used and DWARF2_DEBUGGING_INFO is in effect.
3797 If necessary, these may be overridden from within the tm.h file, but
3798 typically, overriding these defaults is unnecessary. */
3800 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3801 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3802 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3803 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3804 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3805 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3806 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3807 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3808 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3809 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3810 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3811 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3812 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3813 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3814 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3816 #ifndef TEXT_END_LABEL
3817 #define TEXT_END_LABEL "Letext"
3818 #endif
3819 #ifndef COLD_END_LABEL
3820 #define COLD_END_LABEL "Letext_cold"
3821 #endif
3822 #ifndef BLOCK_BEGIN_LABEL
3823 #define BLOCK_BEGIN_LABEL "LBB"
3824 #endif
3825 #ifndef BLOCK_END_LABEL
3826 #define BLOCK_END_LABEL "LBE"
3827 #endif
3828 #ifndef LINE_CODE_LABEL
3829 #define LINE_CODE_LABEL "LM"
3830 #endif
3833 /* Return the root of the DIE's built for the current compilation unit. */
3834 static dw_die_ref
3835 comp_unit_die (void)
3837 if (!single_comp_unit_die)
3838 single_comp_unit_die = gen_compile_unit_die (NULL);
3839 return single_comp_unit_die;
3842 /* We allow a language front-end to designate a function that is to be
3843 called to "demangle" any name before it is put into a DIE. */
3845 static const char *(*demangle_name_func) (const char *);
3847 void
3848 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3850 demangle_name_func = func;
3853 /* Test if rtl node points to a pseudo register. */
3855 static inline int
3856 is_pseudo_reg (const_rtx rtl)
3858 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3859 || (GET_CODE (rtl) == SUBREG
3860 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3863 /* Return a reference to a type, with its const and volatile qualifiers
3864 removed. */
3866 static inline tree
3867 type_main_variant (tree type)
3869 type = TYPE_MAIN_VARIANT (type);
3871 /* ??? There really should be only one main variant among any group of
3872 variants of a given type (and all of the MAIN_VARIANT values for all
3873 members of the group should point to that one type) but sometimes the C
3874 front-end messes this up for array types, so we work around that bug
3875 here. */
3876 if (TREE_CODE (type) == ARRAY_TYPE)
3877 while (type != TYPE_MAIN_VARIANT (type))
3878 type = TYPE_MAIN_VARIANT (type);
3880 return type;
3883 /* Return nonzero if the given type node represents a tagged type. */
3885 static inline int
3886 is_tagged_type (const_tree type)
3888 enum tree_code code = TREE_CODE (type);
3890 return (code == RECORD_TYPE || code == UNION_TYPE
3891 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3894 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3896 static void
3897 get_ref_die_offset_label (char *label, dw_die_ref ref)
3899 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3902 /* Return die_offset of a DIE reference to a base type. */
3904 static unsigned long int
3905 get_base_type_offset (dw_die_ref ref)
3907 if (ref->die_offset)
3908 return ref->die_offset;
3909 if (comp_unit_die ()->die_abbrev)
3911 calc_base_type_die_sizes ();
3912 gcc_assert (ref->die_offset);
3914 return ref->die_offset;
3917 /* Return die_offset of a DIE reference other than base type. */
3919 static unsigned long int
3920 get_ref_die_offset (dw_die_ref ref)
3922 gcc_assert (ref->die_offset);
3923 return ref->die_offset;
3926 /* Convert a DIE tag into its string name. */
3928 static const char *
3929 dwarf_tag_name (unsigned int tag)
3931 const char *name = get_DW_TAG_name (tag);
3933 if (name != NULL)
3934 return name;
3936 return "DW_TAG_<unknown>";
3939 /* Convert a DWARF attribute code into its string name. */
3941 static const char *
3942 dwarf_attr_name (unsigned int attr)
3944 const char *name;
3946 switch (attr)
3948 #if VMS_DEBUGGING_INFO
3949 case DW_AT_HP_prologue:
3950 return "DW_AT_HP_prologue";
3951 #else
3952 case DW_AT_MIPS_loop_unroll_factor:
3953 return "DW_AT_MIPS_loop_unroll_factor";
3954 #endif
3956 #if VMS_DEBUGGING_INFO
3957 case DW_AT_HP_epilogue:
3958 return "DW_AT_HP_epilogue";
3959 #else
3960 case DW_AT_MIPS_stride:
3961 return "DW_AT_MIPS_stride";
3962 #endif
3965 name = get_DW_AT_name (attr);
3967 if (name != NULL)
3968 return name;
3970 return "DW_AT_<unknown>";
3973 /* Convert a DWARF value form code into its string name. */
3975 static const char *
3976 dwarf_form_name (unsigned int form)
3978 const char *name = get_DW_FORM_name (form);
3980 if (name != NULL)
3981 return name;
3983 return "DW_FORM_<unknown>";
3986 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3987 instance of an inlined instance of a decl which is local to an inline
3988 function, so we have to trace all of the way back through the origin chain
3989 to find out what sort of node actually served as the original seed for the
3990 given block. */
3992 static tree
3993 decl_ultimate_origin (const_tree decl)
3995 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3996 return NULL_TREE;
3998 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3999 we're trying to output the abstract instance of this function. */
4000 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4001 return NULL_TREE;
4003 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4004 most distant ancestor, this should never happen. */
4005 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4007 return DECL_ABSTRACT_ORIGIN (decl);
4010 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4011 of a virtual function may refer to a base class, so we check the 'this'
4012 parameter. */
4014 static tree
4015 decl_class_context (tree decl)
4017 tree context = NULL_TREE;
4019 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4020 context = DECL_CONTEXT (decl);
4021 else
4022 context = TYPE_MAIN_VARIANT
4023 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4025 if (context && !TYPE_P (context))
4026 context = NULL_TREE;
4028 return context;
4031 /* Add an attribute/value pair to a DIE. */
4033 static inline void
4034 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4036 /* Maybe this should be an assert? */
4037 if (die == NULL)
4038 return;
4040 vec_safe_reserve (die->die_attr, 1);
4041 vec_safe_push (die->die_attr, *attr);
4044 static inline enum dw_val_class
4045 AT_class (dw_attr_node *a)
4047 return a->dw_attr_val.val_class;
4050 /* Return the index for any attribute that will be referenced with a
4051 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4052 are stored in dw_attr_val.v.val_str for reference counting
4053 pruning. */
4055 static inline unsigned int
4056 AT_index (dw_attr_node *a)
4058 if (AT_class (a) == dw_val_class_str)
4059 return a->dw_attr_val.v.val_str->index;
4060 else if (a->dw_attr_val.val_entry != NULL)
4061 return a->dw_attr_val.val_entry->index;
4062 return NOT_INDEXED;
4065 /* Add a flag value attribute to a DIE. */
4067 static inline void
4068 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4070 dw_attr_node attr;
4072 attr.dw_attr = attr_kind;
4073 attr.dw_attr_val.val_class = dw_val_class_flag;
4074 attr.dw_attr_val.val_entry = NULL;
4075 attr.dw_attr_val.v.val_flag = flag;
4076 add_dwarf_attr (die, &attr);
4079 static inline unsigned
4080 AT_flag (dw_attr_node *a)
4082 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4083 return a->dw_attr_val.v.val_flag;
4086 /* Add a signed integer attribute value to a DIE. */
4088 static inline void
4089 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4091 dw_attr_node attr;
4093 attr.dw_attr = attr_kind;
4094 attr.dw_attr_val.val_class = dw_val_class_const;
4095 attr.dw_attr_val.val_entry = NULL;
4096 attr.dw_attr_val.v.val_int = int_val;
4097 add_dwarf_attr (die, &attr);
4100 static inline HOST_WIDE_INT
4101 AT_int (dw_attr_node *a)
4103 gcc_assert (a && (AT_class (a) == dw_val_class_const
4104 || AT_class (a) == dw_val_class_const_implicit));
4105 return a->dw_attr_val.v.val_int;
4108 /* Add an unsigned integer attribute value to a DIE. */
4110 static inline void
4111 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4112 unsigned HOST_WIDE_INT unsigned_val)
4114 dw_attr_node attr;
4116 attr.dw_attr = attr_kind;
4117 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4118 attr.dw_attr_val.val_entry = NULL;
4119 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4120 add_dwarf_attr (die, &attr);
4123 static inline unsigned HOST_WIDE_INT
4124 AT_unsigned (dw_attr_node *a)
4126 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4127 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4128 return a->dw_attr_val.v.val_unsigned;
4131 /* Add an unsigned wide integer attribute value to a DIE. */
4133 static inline void
4134 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4135 const wide_int& w)
4137 dw_attr_node attr;
4139 attr.dw_attr = attr_kind;
4140 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4141 attr.dw_attr_val.val_entry = NULL;
4142 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4143 *attr.dw_attr_val.v.val_wide = w;
4144 add_dwarf_attr (die, &attr);
4147 /* Add an unsigned double integer attribute value to a DIE. */
4149 static inline void
4150 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4151 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4153 dw_attr_node attr;
4155 attr.dw_attr = attr_kind;
4156 attr.dw_attr_val.val_class = dw_val_class_const_double;
4157 attr.dw_attr_val.val_entry = NULL;
4158 attr.dw_attr_val.v.val_double.high = high;
4159 attr.dw_attr_val.v.val_double.low = low;
4160 add_dwarf_attr (die, &attr);
4163 /* Add a floating point attribute value to a DIE and return it. */
4165 static inline void
4166 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4167 unsigned int length, unsigned int elt_size, unsigned char *array)
4169 dw_attr_node attr;
4171 attr.dw_attr = attr_kind;
4172 attr.dw_attr_val.val_class = dw_val_class_vec;
4173 attr.dw_attr_val.val_entry = NULL;
4174 attr.dw_attr_val.v.val_vec.length = length;
4175 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4176 attr.dw_attr_val.v.val_vec.array = array;
4177 add_dwarf_attr (die, &attr);
4180 /* Add an 8-byte data attribute value to a DIE. */
4182 static inline void
4183 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4184 unsigned char data8[8])
4186 dw_attr_node attr;
4188 attr.dw_attr = attr_kind;
4189 attr.dw_attr_val.val_class = dw_val_class_data8;
4190 attr.dw_attr_val.val_entry = NULL;
4191 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4192 add_dwarf_attr (die, &attr);
4195 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4196 dwarf_split_debug_info, address attributes in dies destined for the
4197 final executable have force_direct set to avoid using indexed
4198 references. */
4200 static inline void
4201 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4202 bool force_direct)
4204 dw_attr_node attr;
4205 char * lbl_id;
4207 lbl_id = xstrdup (lbl_low);
4208 attr.dw_attr = DW_AT_low_pc;
4209 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4210 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4211 if (dwarf_split_debug_info && !force_direct)
4212 attr.dw_attr_val.val_entry
4213 = add_addr_table_entry (lbl_id, ate_kind_label);
4214 else
4215 attr.dw_attr_val.val_entry = NULL;
4216 add_dwarf_attr (die, &attr);
4218 attr.dw_attr = DW_AT_high_pc;
4219 if (dwarf_version < 4)
4220 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4221 else
4222 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4223 lbl_id = xstrdup (lbl_high);
4224 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4225 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4226 && dwarf_split_debug_info && !force_direct)
4227 attr.dw_attr_val.val_entry
4228 = add_addr_table_entry (lbl_id, ate_kind_label);
4229 else
4230 attr.dw_attr_val.val_entry = NULL;
4231 add_dwarf_attr (die, &attr);
4234 /* Hash and equality functions for debug_str_hash. */
4236 hashval_t
4237 indirect_string_hasher::hash (indirect_string_node *x)
4239 return htab_hash_string (x->str);
4242 bool
4243 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4245 return strcmp (x1->str, x2) == 0;
4248 /* Add STR to the given string hash table. */
4250 static struct indirect_string_node *
4251 find_AT_string_in_table (const char *str,
4252 hash_table<indirect_string_hasher> *table)
4254 struct indirect_string_node *node;
4256 indirect_string_node **slot
4257 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4258 if (*slot == NULL)
4260 node = ggc_cleared_alloc<indirect_string_node> ();
4261 node->str = ggc_strdup (str);
4262 *slot = node;
4264 else
4265 node = *slot;
4267 node->refcount++;
4268 return node;
4271 /* Add STR to the indirect string hash table. */
4273 static struct indirect_string_node *
4274 find_AT_string (const char *str)
4276 if (! debug_str_hash)
4277 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4279 return find_AT_string_in_table (str, debug_str_hash);
4282 /* Add a string attribute value to a DIE. */
4284 static inline void
4285 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4287 dw_attr_node attr;
4288 struct indirect_string_node *node;
4290 node = find_AT_string (str);
4292 attr.dw_attr = attr_kind;
4293 attr.dw_attr_val.val_class = dw_val_class_str;
4294 attr.dw_attr_val.val_entry = NULL;
4295 attr.dw_attr_val.v.val_str = node;
4296 add_dwarf_attr (die, &attr);
4299 static inline const char *
4300 AT_string (dw_attr_node *a)
4302 gcc_assert (a && AT_class (a) == dw_val_class_str);
4303 return a->dw_attr_val.v.val_str->str;
4306 /* Call this function directly to bypass AT_string_form's logic to put
4307 the string inline in the die. */
4309 static void
4310 set_indirect_string (struct indirect_string_node *node)
4312 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4313 /* Already indirect is a no op. */
4314 if (node->form == DW_FORM_strp
4315 || node->form == DW_FORM_line_strp
4316 || node->form == DW_FORM_GNU_str_index)
4318 gcc_assert (node->label);
4319 return;
4321 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4322 ++dw2_string_counter;
4323 node->label = xstrdup (label);
4325 if (!dwarf_split_debug_info)
4327 node->form = DW_FORM_strp;
4328 node->index = NOT_INDEXED;
4330 else
4332 node->form = DW_FORM_GNU_str_index;
4333 node->index = NO_INDEX_ASSIGNED;
4337 /* Find out whether a string should be output inline in DIE
4338 or out-of-line in .debug_str section. */
4340 static enum dwarf_form
4341 find_string_form (struct indirect_string_node *node)
4343 unsigned int len;
4345 if (node->form)
4346 return node->form;
4348 len = strlen (node->str) + 1;
4350 /* If the string is shorter or equal to the size of the reference, it is
4351 always better to put it inline. */
4352 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4353 return node->form = DW_FORM_string;
4355 /* If we cannot expect the linker to merge strings in .debug_str
4356 section, only put it into .debug_str if it is worth even in this
4357 single module. */
4358 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4359 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4360 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4361 return node->form = DW_FORM_string;
4363 set_indirect_string (node);
4365 return node->form;
4368 /* Find out whether the string referenced from the attribute should be
4369 output inline in DIE or out-of-line in .debug_str section. */
4371 static enum dwarf_form
4372 AT_string_form (dw_attr_node *a)
4374 gcc_assert (a && AT_class (a) == dw_val_class_str);
4375 return find_string_form (a->dw_attr_val.v.val_str);
4378 /* Add a DIE reference attribute value to a DIE. */
4380 static inline void
4381 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4383 dw_attr_node attr;
4384 gcc_checking_assert (targ_die != NULL);
4386 /* With LTO we can end up trying to reference something we didn't create
4387 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4388 if (targ_die == NULL)
4389 return;
4391 attr.dw_attr = attr_kind;
4392 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4393 attr.dw_attr_val.val_entry = NULL;
4394 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4395 attr.dw_attr_val.v.val_die_ref.external = 0;
4396 add_dwarf_attr (die, &attr);
4399 /* Change DIE reference REF to point to NEW_DIE instead. */
4401 static inline void
4402 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4404 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4405 ref->dw_attr_val.v.val_die_ref.die = new_die;
4406 ref->dw_attr_val.v.val_die_ref.external = 0;
4409 /* Add an AT_specification attribute to a DIE, and also make the back
4410 pointer from the specification to the definition. */
4412 static inline void
4413 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4415 add_AT_die_ref (die, DW_AT_specification, targ_die);
4416 gcc_assert (!targ_die->die_definition);
4417 targ_die->die_definition = die;
4420 static inline dw_die_ref
4421 AT_ref (dw_attr_node *a)
4423 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4424 return a->dw_attr_val.v.val_die_ref.die;
4427 static inline int
4428 AT_ref_external (dw_attr_node *a)
4430 if (a && AT_class (a) == dw_val_class_die_ref)
4431 return a->dw_attr_val.v.val_die_ref.external;
4433 return 0;
4436 static inline void
4437 set_AT_ref_external (dw_attr_node *a, int i)
4439 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4440 a->dw_attr_val.v.val_die_ref.external = i;
4443 /* Add an FDE reference attribute value to a DIE. */
4445 static inline void
4446 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4448 dw_attr_node attr;
4450 attr.dw_attr = attr_kind;
4451 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4452 attr.dw_attr_val.val_entry = NULL;
4453 attr.dw_attr_val.v.val_fde_index = targ_fde;
4454 add_dwarf_attr (die, &attr);
4457 /* Add a location description attribute value to a DIE. */
4459 static inline void
4460 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4462 dw_attr_node attr;
4464 attr.dw_attr = attr_kind;
4465 attr.dw_attr_val.val_class = dw_val_class_loc;
4466 attr.dw_attr_val.val_entry = NULL;
4467 attr.dw_attr_val.v.val_loc = loc;
4468 add_dwarf_attr (die, &attr);
4471 static inline dw_loc_descr_ref
4472 AT_loc (dw_attr_node *a)
4474 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4475 return a->dw_attr_val.v.val_loc;
4478 static inline void
4479 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4481 dw_attr_node attr;
4483 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4484 return;
4486 attr.dw_attr = attr_kind;
4487 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4488 attr.dw_attr_val.val_entry = NULL;
4489 attr.dw_attr_val.v.val_loc_list = loc_list;
4490 add_dwarf_attr (die, &attr);
4491 have_location_lists = true;
4494 static inline dw_loc_list_ref
4495 AT_loc_list (dw_attr_node *a)
4497 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4498 return a->dw_attr_val.v.val_loc_list;
4501 static inline dw_loc_list_ref *
4502 AT_loc_list_ptr (dw_attr_node *a)
4504 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4505 return &a->dw_attr_val.v.val_loc_list;
4508 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4510 static hashval_t hash (addr_table_entry *);
4511 static bool equal (addr_table_entry *, addr_table_entry *);
4514 /* Table of entries into the .debug_addr section. */
4516 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4518 /* Hash an address_table_entry. */
4520 hashval_t
4521 addr_hasher::hash (addr_table_entry *a)
4523 inchash::hash hstate;
4524 switch (a->kind)
4526 case ate_kind_rtx:
4527 hstate.add_int (0);
4528 break;
4529 case ate_kind_rtx_dtprel:
4530 hstate.add_int (1);
4531 break;
4532 case ate_kind_label:
4533 return htab_hash_string (a->addr.label);
4534 default:
4535 gcc_unreachable ();
4537 inchash::add_rtx (a->addr.rtl, hstate);
4538 return hstate.end ();
4541 /* Determine equality for two address_table_entries. */
4543 bool
4544 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4546 if (a1->kind != a2->kind)
4547 return 0;
4548 switch (a1->kind)
4550 case ate_kind_rtx:
4551 case ate_kind_rtx_dtprel:
4552 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4553 case ate_kind_label:
4554 return strcmp (a1->addr.label, a2->addr.label) == 0;
4555 default:
4556 gcc_unreachable ();
4560 /* Initialize an addr_table_entry. */
4562 void
4563 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4565 e->kind = kind;
4566 switch (kind)
4568 case ate_kind_rtx:
4569 case ate_kind_rtx_dtprel:
4570 e->addr.rtl = (rtx) addr;
4571 break;
4572 case ate_kind_label:
4573 e->addr.label = (char *) addr;
4574 break;
4576 e->refcount = 0;
4577 e->index = NO_INDEX_ASSIGNED;
4580 /* Add attr to the address table entry to the table. Defer setting an
4581 index until output time. */
4583 static addr_table_entry *
4584 add_addr_table_entry (void *addr, enum ate_kind kind)
4586 addr_table_entry *node;
4587 addr_table_entry finder;
4589 gcc_assert (dwarf_split_debug_info);
4590 if (! addr_index_table)
4591 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4592 init_addr_table_entry (&finder, kind, addr);
4593 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4595 if (*slot == HTAB_EMPTY_ENTRY)
4597 node = ggc_cleared_alloc<addr_table_entry> ();
4598 init_addr_table_entry (node, kind, addr);
4599 *slot = node;
4601 else
4602 node = *slot;
4604 node->refcount++;
4605 return node;
4608 /* Remove an entry from the addr table by decrementing its refcount.
4609 Strictly, decrementing the refcount would be enough, but the
4610 assertion that the entry is actually in the table has found
4611 bugs. */
4613 static void
4614 remove_addr_table_entry (addr_table_entry *entry)
4616 gcc_assert (dwarf_split_debug_info && addr_index_table);
4617 /* After an index is assigned, the table is frozen. */
4618 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4619 entry->refcount--;
4622 /* Given a location list, remove all addresses it refers to from the
4623 address_table. */
4625 static void
4626 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4628 for (; descr; descr = descr->dw_loc_next)
4629 if (descr->dw_loc_oprnd1.val_entry != NULL)
4631 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4632 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4636 /* A helper function for dwarf2out_finish called through
4637 htab_traverse. Assign an addr_table_entry its index. All entries
4638 must be collected into the table when this function is called,
4639 because the indexing code relies on htab_traverse to traverse nodes
4640 in the same order for each run. */
4643 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4645 addr_table_entry *node = *h;
4647 /* Don't index unreferenced nodes. */
4648 if (node->refcount == 0)
4649 return 1;
4651 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4652 node->index = *index;
4653 *index += 1;
4655 return 1;
4658 /* Add an address constant attribute value to a DIE. When using
4659 dwarf_split_debug_info, address attributes in dies destined for the
4660 final executable should be direct references--setting the parameter
4661 force_direct ensures this behavior. */
4663 static inline void
4664 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4665 bool force_direct)
4667 dw_attr_node attr;
4669 attr.dw_attr = attr_kind;
4670 attr.dw_attr_val.val_class = dw_val_class_addr;
4671 attr.dw_attr_val.v.val_addr = addr;
4672 if (dwarf_split_debug_info && !force_direct)
4673 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4674 else
4675 attr.dw_attr_val.val_entry = NULL;
4676 add_dwarf_attr (die, &attr);
4679 /* Get the RTX from to an address DIE attribute. */
4681 static inline rtx
4682 AT_addr (dw_attr_node *a)
4684 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4685 return a->dw_attr_val.v.val_addr;
4688 /* Add a file attribute value to a DIE. */
4690 static inline void
4691 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4692 struct dwarf_file_data *fd)
4694 dw_attr_node attr;
4696 attr.dw_attr = attr_kind;
4697 attr.dw_attr_val.val_class = dw_val_class_file;
4698 attr.dw_attr_val.val_entry = NULL;
4699 attr.dw_attr_val.v.val_file = fd;
4700 add_dwarf_attr (die, &attr);
4703 /* Get the dwarf_file_data from a file DIE attribute. */
4705 static inline struct dwarf_file_data *
4706 AT_file (dw_attr_node *a)
4708 gcc_assert (a && (AT_class (a) == dw_val_class_file
4709 || AT_class (a) == dw_val_class_file_implicit));
4710 return a->dw_attr_val.v.val_file;
4713 /* Add a vms delta attribute value to a DIE. */
4715 static inline void
4716 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4717 const char *lbl1, const char *lbl2)
4719 dw_attr_node attr;
4721 attr.dw_attr = attr_kind;
4722 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4723 attr.dw_attr_val.val_entry = NULL;
4724 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4725 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4726 add_dwarf_attr (die, &attr);
4729 /* Add a label identifier attribute value to a DIE. */
4731 static inline void
4732 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4733 const char *lbl_id)
4735 dw_attr_node attr;
4737 attr.dw_attr = attr_kind;
4738 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4739 attr.dw_attr_val.val_entry = NULL;
4740 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4741 if (dwarf_split_debug_info)
4742 attr.dw_attr_val.val_entry
4743 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4744 ate_kind_label);
4745 add_dwarf_attr (die, &attr);
4748 /* Add a section offset attribute value to a DIE, an offset into the
4749 debug_line section. */
4751 static inline void
4752 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4753 const char *label)
4755 dw_attr_node attr;
4757 attr.dw_attr = attr_kind;
4758 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4759 attr.dw_attr_val.val_entry = NULL;
4760 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4761 add_dwarf_attr (die, &attr);
4764 /* Add a section offset attribute value to a DIE, an offset into the
4765 debug_loclists section. */
4767 static inline void
4768 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4769 const char *label)
4771 dw_attr_node attr;
4773 attr.dw_attr = attr_kind;
4774 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4775 attr.dw_attr_val.val_entry = NULL;
4776 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4777 add_dwarf_attr (die, &attr);
4780 /* Add a section offset attribute value to a DIE, an offset into the
4781 debug_macinfo section. */
4783 static inline void
4784 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4785 const char *label)
4787 dw_attr_node attr;
4789 attr.dw_attr = attr_kind;
4790 attr.dw_attr_val.val_class = dw_val_class_macptr;
4791 attr.dw_attr_val.val_entry = NULL;
4792 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4793 add_dwarf_attr (die, &attr);
4796 /* Add an offset attribute value to a DIE. */
4798 static inline void
4799 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4800 unsigned HOST_WIDE_INT offset)
4802 dw_attr_node attr;
4804 attr.dw_attr = attr_kind;
4805 attr.dw_attr_val.val_class = dw_val_class_offset;
4806 attr.dw_attr_val.val_entry = NULL;
4807 attr.dw_attr_val.v.val_offset = offset;
4808 add_dwarf_attr (die, &attr);
4811 /* Add a range_list attribute value to a DIE. When using
4812 dwarf_split_debug_info, address attributes in dies destined for the
4813 final executable should be direct references--setting the parameter
4814 force_direct ensures this behavior. */
4816 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4817 #define RELOCATED_OFFSET (NULL)
4819 static void
4820 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4821 long unsigned int offset, bool force_direct)
4823 dw_attr_node attr;
4825 attr.dw_attr = attr_kind;
4826 attr.dw_attr_val.val_class = dw_val_class_range_list;
4827 /* For the range_list attribute, use val_entry to store whether the
4828 offset should follow split-debug-info or normal semantics. This
4829 value is read in output_range_list_offset. */
4830 if (dwarf_split_debug_info && !force_direct)
4831 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4832 else
4833 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4834 attr.dw_attr_val.v.val_offset = offset;
4835 add_dwarf_attr (die, &attr);
4838 /* Return the start label of a delta attribute. */
4840 static inline const char *
4841 AT_vms_delta1 (dw_attr_node *a)
4843 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4844 return a->dw_attr_val.v.val_vms_delta.lbl1;
4847 /* Return the end label of a delta attribute. */
4849 static inline const char *
4850 AT_vms_delta2 (dw_attr_node *a)
4852 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4853 return a->dw_attr_val.v.val_vms_delta.lbl2;
4856 static inline const char *
4857 AT_lbl (dw_attr_node *a)
4859 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4860 || AT_class (a) == dw_val_class_lineptr
4861 || AT_class (a) == dw_val_class_macptr
4862 || AT_class (a) == dw_val_class_loclistsptr
4863 || AT_class (a) == dw_val_class_high_pc));
4864 return a->dw_attr_val.v.val_lbl_id;
4867 /* Get the attribute of type attr_kind. */
4869 static dw_attr_node *
4870 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4872 dw_attr_node *a;
4873 unsigned ix;
4874 dw_die_ref spec = NULL;
4876 if (! die)
4877 return NULL;
4879 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4880 if (a->dw_attr == attr_kind)
4881 return a;
4882 else if (a->dw_attr == DW_AT_specification
4883 || a->dw_attr == DW_AT_abstract_origin)
4884 spec = AT_ref (a);
4886 if (spec)
4887 return get_AT (spec, attr_kind);
4889 return NULL;
4892 /* Returns the parent of the declaration of DIE. */
4894 static dw_die_ref
4895 get_die_parent (dw_die_ref die)
4897 dw_die_ref t;
4899 if (!die)
4900 return NULL;
4902 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4903 || (t = get_AT_ref (die, DW_AT_specification)))
4904 die = t;
4906 return die->die_parent;
4909 /* Return the "low pc" attribute value, typically associated with a subprogram
4910 DIE. Return null if the "low pc" attribute is either not present, or if it
4911 cannot be represented as an assembler label identifier. */
4913 static inline const char *
4914 get_AT_low_pc (dw_die_ref die)
4916 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4918 return a ? AT_lbl (a) : NULL;
4921 /* Return the "high pc" attribute value, typically associated with a subprogram
4922 DIE. Return null if the "high pc" attribute is either not present, or if it
4923 cannot be represented as an assembler label identifier. */
4925 static inline const char *
4926 get_AT_hi_pc (dw_die_ref die)
4928 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4930 return a ? AT_lbl (a) : NULL;
4933 /* Return the value of the string attribute designated by ATTR_KIND, or
4934 NULL if it is not present. */
4936 static inline const char *
4937 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4939 dw_attr_node *a = get_AT (die, attr_kind);
4941 return a ? AT_string (a) : NULL;
4944 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4945 if it is not present. */
4947 static inline int
4948 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4950 dw_attr_node *a = get_AT (die, attr_kind);
4952 return a ? AT_flag (a) : 0;
4955 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4956 if it is not present. */
4958 static inline unsigned
4959 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4961 dw_attr_node *a = get_AT (die, attr_kind);
4963 return a ? AT_unsigned (a) : 0;
4966 static inline dw_die_ref
4967 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4969 dw_attr_node *a = get_AT (die, attr_kind);
4971 return a ? AT_ref (a) : NULL;
4974 static inline struct dwarf_file_data *
4975 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4977 dw_attr_node *a = get_AT (die, attr_kind);
4979 return a ? AT_file (a) : NULL;
4982 /* Return TRUE if the language is C++. */
4984 static inline bool
4985 is_cxx (void)
4987 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4989 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4990 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4993 /* Return TRUE if the language is Java. */
4995 static inline bool
4996 is_java (void)
4998 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5000 return lang == DW_LANG_Java;
5003 /* Return TRUE if the language is Fortran. */
5005 static inline bool
5006 is_fortran (void)
5008 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5010 return (lang == DW_LANG_Fortran77
5011 || lang == DW_LANG_Fortran90
5012 || lang == DW_LANG_Fortran95
5013 || lang == DW_LANG_Fortran03
5014 || lang == DW_LANG_Fortran08);
5017 /* Return TRUE if the language is Ada. */
5019 static inline bool
5020 is_ada (void)
5022 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5024 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5027 /* Remove the specified attribute if present. Return TRUE if removal
5028 was successful. */
5030 static bool
5031 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5033 dw_attr_node *a;
5034 unsigned ix;
5036 if (! die)
5037 return false;
5039 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5040 if (a->dw_attr == attr_kind)
5042 if (AT_class (a) == dw_val_class_str)
5043 if (a->dw_attr_val.v.val_str->refcount)
5044 a->dw_attr_val.v.val_str->refcount--;
5046 /* vec::ordered_remove should help reduce the number of abbrevs
5047 that are needed. */
5048 die->die_attr->ordered_remove (ix);
5049 return true;
5051 return false;
5054 /* Remove CHILD from its parent. PREV must have the property that
5055 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5057 static void
5058 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5060 gcc_assert (child->die_parent == prev->die_parent);
5061 gcc_assert (prev->die_sib == child);
5062 if (prev == child)
5064 gcc_assert (child->die_parent->die_child == child);
5065 prev = NULL;
5067 else
5068 prev->die_sib = child->die_sib;
5069 if (child->die_parent->die_child == child)
5070 child->die_parent->die_child = prev;
5071 child->die_sib = NULL;
5074 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5075 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5077 static void
5078 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5080 dw_die_ref parent = old_child->die_parent;
5082 gcc_assert (parent == prev->die_parent);
5083 gcc_assert (prev->die_sib == old_child);
5085 new_child->die_parent = parent;
5086 if (prev == old_child)
5088 gcc_assert (parent->die_child == old_child);
5089 new_child->die_sib = new_child;
5091 else
5093 prev->die_sib = new_child;
5094 new_child->die_sib = old_child->die_sib;
5096 if (old_child->die_parent->die_child == old_child)
5097 old_child->die_parent->die_child = new_child;
5098 old_child->die_sib = NULL;
5101 /* Move all children from OLD_PARENT to NEW_PARENT. */
5103 static void
5104 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5106 dw_die_ref c;
5107 new_parent->die_child = old_parent->die_child;
5108 old_parent->die_child = NULL;
5109 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5112 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5113 matches TAG. */
5115 static void
5116 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5118 dw_die_ref c;
5120 c = die->die_child;
5121 if (c) do {
5122 dw_die_ref prev = c;
5123 c = c->die_sib;
5124 while (c->die_tag == tag)
5126 remove_child_with_prev (c, prev);
5127 c->die_parent = NULL;
5128 /* Might have removed every child. */
5129 if (die->die_child == NULL)
5130 return;
5131 c = prev->die_sib;
5133 } while (c != die->die_child);
5136 /* Add a CHILD_DIE as the last child of DIE. */
5138 static void
5139 add_child_die (dw_die_ref die, dw_die_ref child_die)
5141 /* FIXME this should probably be an assert. */
5142 if (! die || ! child_die)
5143 return;
5144 gcc_assert (die != child_die);
5146 child_die->die_parent = die;
5147 if (die->die_child)
5149 child_die->die_sib = die->die_child->die_sib;
5150 die->die_child->die_sib = child_die;
5152 else
5153 child_die->die_sib = child_die;
5154 die->die_child = child_die;
5157 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5159 static void
5160 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5161 dw_die_ref after_die)
5163 gcc_assert (die
5164 && child_die
5165 && after_die
5166 && die->die_child
5167 && die != child_die);
5169 child_die->die_parent = die;
5170 child_die->die_sib = after_die->die_sib;
5171 after_die->die_sib = child_die;
5172 if (die->die_child == after_die)
5173 die->die_child = child_die;
5176 /* Unassociate CHILD from its parent, and make its parent be
5177 NEW_PARENT. */
5179 static void
5180 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5182 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5183 if (p->die_sib == child)
5185 remove_child_with_prev (child, p);
5186 break;
5188 add_child_die (new_parent, child);
5191 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5192 is the specification, to the end of PARENT's list of children.
5193 This is done by removing and re-adding it. */
5195 static void
5196 splice_child_die (dw_die_ref parent, dw_die_ref child)
5198 /* We want the declaration DIE from inside the class, not the
5199 specification DIE at toplevel. */
5200 if (child->die_parent != parent)
5202 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5204 if (tmp)
5205 child = tmp;
5208 gcc_assert (child->die_parent == parent
5209 || (child->die_parent
5210 == get_AT_ref (parent, DW_AT_specification)));
5212 reparent_child (child, parent);
5215 /* Create and return a new die with a parent of PARENT_DIE. If
5216 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5217 associated tree T must be supplied to determine parenthood
5218 later. */
5220 static inline dw_die_ref
5221 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5223 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5225 die->die_tag = tag_value;
5227 if (parent_die != NULL)
5228 add_child_die (parent_die, die);
5229 else
5231 limbo_die_node *limbo_node;
5233 /* No DIEs created after early dwarf should end up in limbo,
5234 because the limbo list should not persist past LTO
5235 streaming. */
5236 if (tag_value != DW_TAG_compile_unit
5237 /* These are allowed because they're generated while
5238 breaking out COMDAT units late. */
5239 && tag_value != DW_TAG_type_unit
5240 && tag_value != DW_TAG_skeleton_unit
5241 && !early_dwarf
5242 /* Allow nested functions to live in limbo because they will
5243 only temporarily live there, as decls_for_scope will fix
5244 them up. */
5245 && (TREE_CODE (t) != FUNCTION_DECL
5246 || !decl_function_context (t))
5247 /* Same as nested functions above but for types. Types that
5248 are local to a function will be fixed in
5249 decls_for_scope. */
5250 && (!RECORD_OR_UNION_TYPE_P (t)
5251 || !TYPE_CONTEXT (t)
5252 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5253 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5254 especially in the ltrans stage, but once we implement LTO
5255 dwarf streaming, we should remove this exception. */
5256 && !in_lto_p)
5258 fprintf (stderr, "symbol ended up in limbo too late:");
5259 debug_generic_stmt (t);
5260 gcc_unreachable ();
5263 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5264 limbo_node->die = die;
5265 limbo_node->created_for = t;
5266 limbo_node->next = limbo_die_list;
5267 limbo_die_list = limbo_node;
5270 return die;
5273 /* Return the DIE associated with the given type specifier. */
5275 static inline dw_die_ref
5276 lookup_type_die (tree type)
5278 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5279 if (die && die->removed)
5281 TYPE_SYMTAB_DIE (type) = NULL;
5282 return NULL;
5284 return die;
5287 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5288 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5289 anonymous type instead the one of the naming typedef. */
5291 static inline dw_die_ref
5292 strip_naming_typedef (tree type, dw_die_ref type_die)
5294 if (type
5295 && TREE_CODE (type) == RECORD_TYPE
5296 && type_die
5297 && type_die->die_tag == DW_TAG_typedef
5298 && is_naming_typedef_decl (TYPE_NAME (type)))
5299 type_die = get_AT_ref (type_die, DW_AT_type);
5300 return type_die;
5303 /* Like lookup_type_die, but if type is an anonymous type named by a
5304 typedef[1], return the DIE of the anonymous type instead the one of
5305 the naming typedef. This is because in gen_typedef_die, we did
5306 equate the anonymous struct named by the typedef with the DIE of
5307 the naming typedef. So by default, lookup_type_die on an anonymous
5308 struct yields the DIE of the naming typedef.
5310 [1]: Read the comment of is_naming_typedef_decl to learn about what
5311 a naming typedef is. */
5313 static inline dw_die_ref
5314 lookup_type_die_strip_naming_typedef (tree type)
5316 dw_die_ref die = lookup_type_die (type);
5317 return strip_naming_typedef (type, die);
5320 /* Equate a DIE to a given type specifier. */
5322 static inline void
5323 equate_type_number_to_die (tree type, dw_die_ref type_die)
5325 TYPE_SYMTAB_DIE (type) = type_die;
5328 /* Returns a hash value for X (which really is a die_struct). */
5330 inline hashval_t
5331 decl_die_hasher::hash (die_node *x)
5333 return (hashval_t) x->decl_id;
5336 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5338 inline bool
5339 decl_die_hasher::equal (die_node *x, tree y)
5341 return (x->decl_id == DECL_UID (y));
5344 /* Return the DIE associated with a given declaration. */
5346 static inline dw_die_ref
5347 lookup_decl_die (tree decl)
5349 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5350 NO_INSERT);
5351 if (!die)
5352 return NULL;
5353 if ((*die)->removed)
5355 decl_die_table->clear_slot (die);
5356 return NULL;
5358 return *die;
5361 /* Returns a hash value for X (which really is a var_loc_list). */
5363 inline hashval_t
5364 decl_loc_hasher::hash (var_loc_list *x)
5366 return (hashval_t) x->decl_id;
5369 /* Return nonzero if decl_id of var_loc_list X is the same as
5370 UID of decl *Y. */
5372 inline bool
5373 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5375 return (x->decl_id == DECL_UID (y));
5378 /* Return the var_loc list associated with a given declaration. */
5380 static inline var_loc_list *
5381 lookup_decl_loc (const_tree decl)
5383 if (!decl_loc_table)
5384 return NULL;
5385 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5388 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5390 inline hashval_t
5391 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5393 return (hashval_t) x->decl_id;
5396 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5397 UID of decl *Y. */
5399 inline bool
5400 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5402 return (x->decl_id == DECL_UID (y));
5405 /* Equate a DIE to a particular declaration. */
5407 static void
5408 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5410 unsigned int decl_id = DECL_UID (decl);
5412 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5413 decl_die->decl_id = decl_id;
5416 /* Return how many bits covers PIECE EXPR_LIST. */
5418 static HOST_WIDE_INT
5419 decl_piece_bitsize (rtx piece)
5421 int ret = (int) GET_MODE (piece);
5422 if (ret)
5423 return ret;
5424 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5425 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5426 return INTVAL (XEXP (XEXP (piece, 0), 0));
5429 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5431 static rtx *
5432 decl_piece_varloc_ptr (rtx piece)
5434 if ((int) GET_MODE (piece))
5435 return &XEXP (piece, 0);
5436 else
5437 return &XEXP (XEXP (piece, 0), 1);
5440 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5441 Next is the chain of following piece nodes. */
5443 static rtx_expr_list *
5444 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5446 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5447 return alloc_EXPR_LIST (bitsize, loc_note, next);
5448 else
5449 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5450 GEN_INT (bitsize),
5451 loc_note), next);
5454 /* Return rtx that should be stored into loc field for
5455 LOC_NOTE and BITPOS/BITSIZE. */
5457 static rtx
5458 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5459 HOST_WIDE_INT bitsize)
5461 if (bitsize != -1)
5463 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5464 if (bitpos != 0)
5465 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5467 return loc_note;
5470 /* This function either modifies location piece list *DEST in
5471 place (if SRC and INNER is NULL), or copies location piece list
5472 *SRC to *DEST while modifying it. Location BITPOS is modified
5473 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5474 not copied and if needed some padding around it is added.
5475 When modifying in place, DEST should point to EXPR_LIST where
5476 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5477 to the start of the whole list and INNER points to the EXPR_LIST
5478 where earlier pieces cover PIECE_BITPOS bits. */
5480 static void
5481 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5482 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5483 HOST_WIDE_INT bitsize, rtx loc_note)
5485 HOST_WIDE_INT diff;
5486 bool copy = inner != NULL;
5488 if (copy)
5490 /* First copy all nodes preceding the current bitpos. */
5491 while (src != inner)
5493 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5494 decl_piece_bitsize (*src), NULL_RTX);
5495 dest = &XEXP (*dest, 1);
5496 src = &XEXP (*src, 1);
5499 /* Add padding if needed. */
5500 if (bitpos != piece_bitpos)
5502 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5503 copy ? NULL_RTX : *dest);
5504 dest = &XEXP (*dest, 1);
5506 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5508 gcc_assert (!copy);
5509 /* A piece with correct bitpos and bitsize already exist,
5510 just update the location for it and return. */
5511 *decl_piece_varloc_ptr (*dest) = loc_note;
5512 return;
5514 /* Add the piece that changed. */
5515 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5516 dest = &XEXP (*dest, 1);
5517 /* Skip over pieces that overlap it. */
5518 diff = bitpos - piece_bitpos + bitsize;
5519 if (!copy)
5520 src = dest;
5521 while (diff > 0 && *src)
5523 rtx piece = *src;
5524 diff -= decl_piece_bitsize (piece);
5525 if (copy)
5526 src = &XEXP (piece, 1);
5527 else
5529 *src = XEXP (piece, 1);
5530 free_EXPR_LIST_node (piece);
5533 /* Add padding if needed. */
5534 if (diff < 0 && *src)
5536 if (!copy)
5537 dest = src;
5538 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5539 dest = &XEXP (*dest, 1);
5541 if (!copy)
5542 return;
5543 /* Finally copy all nodes following it. */
5544 while (*src)
5546 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5547 decl_piece_bitsize (*src), NULL_RTX);
5548 dest = &XEXP (*dest, 1);
5549 src = &XEXP (*src, 1);
5553 /* Add a variable location node to the linked list for DECL. */
5555 static struct var_loc_node *
5556 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5558 unsigned int decl_id;
5559 var_loc_list *temp;
5560 struct var_loc_node *loc = NULL;
5561 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5563 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5565 tree realdecl = DECL_DEBUG_EXPR (decl);
5566 if (handled_component_p (realdecl)
5567 || (TREE_CODE (realdecl) == MEM_REF
5568 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5570 HOST_WIDE_INT maxsize;
5571 bool reverse;
5572 tree innerdecl
5573 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5574 &reverse);
5575 if (!DECL_P (innerdecl)
5576 || DECL_IGNORED_P (innerdecl)
5577 || TREE_STATIC (innerdecl)
5578 || bitsize <= 0
5579 || bitpos + bitsize > 256
5580 || bitsize != maxsize)
5581 return NULL;
5582 decl = innerdecl;
5586 decl_id = DECL_UID (decl);
5587 var_loc_list **slot
5588 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5589 if (*slot == NULL)
5591 temp = ggc_cleared_alloc<var_loc_list> ();
5592 temp->decl_id = decl_id;
5593 *slot = temp;
5595 else
5596 temp = *slot;
5598 /* For PARM_DECLs try to keep around the original incoming value,
5599 even if that means we'll emit a zero-range .debug_loc entry. */
5600 if (temp->last
5601 && temp->first == temp->last
5602 && TREE_CODE (decl) == PARM_DECL
5603 && NOTE_P (temp->first->loc)
5604 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5605 && DECL_INCOMING_RTL (decl)
5606 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5607 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5608 == GET_CODE (DECL_INCOMING_RTL (decl))
5609 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5610 && (bitsize != -1
5611 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5612 NOTE_VAR_LOCATION_LOC (loc_note))
5613 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5614 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5616 loc = ggc_cleared_alloc<var_loc_node> ();
5617 temp->first->next = loc;
5618 temp->last = loc;
5619 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5621 else if (temp->last)
5623 struct var_loc_node *last = temp->last, *unused = NULL;
5624 rtx *piece_loc = NULL, last_loc_note;
5625 HOST_WIDE_INT piece_bitpos = 0;
5626 if (last->next)
5628 last = last->next;
5629 gcc_assert (last->next == NULL);
5631 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5633 piece_loc = &last->loc;
5636 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5637 if (piece_bitpos + cur_bitsize > bitpos)
5638 break;
5639 piece_bitpos += cur_bitsize;
5640 piece_loc = &XEXP (*piece_loc, 1);
5642 while (*piece_loc);
5644 /* TEMP->LAST here is either pointer to the last but one or
5645 last element in the chained list, LAST is pointer to the
5646 last element. */
5647 if (label && strcmp (last->label, label) == 0)
5649 /* For SRA optimized variables if there weren't any real
5650 insns since last note, just modify the last node. */
5651 if (piece_loc != NULL)
5653 adjust_piece_list (piece_loc, NULL, NULL,
5654 bitpos, piece_bitpos, bitsize, loc_note);
5655 return NULL;
5657 /* If the last note doesn't cover any instructions, remove it. */
5658 if (temp->last != last)
5660 temp->last->next = NULL;
5661 unused = last;
5662 last = temp->last;
5663 gcc_assert (strcmp (last->label, label) != 0);
5665 else
5667 gcc_assert (temp->first == temp->last
5668 || (temp->first->next == temp->last
5669 && TREE_CODE (decl) == PARM_DECL));
5670 memset (temp->last, '\0', sizeof (*temp->last));
5671 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5672 return temp->last;
5675 if (bitsize == -1 && NOTE_P (last->loc))
5676 last_loc_note = last->loc;
5677 else if (piece_loc != NULL
5678 && *piece_loc != NULL_RTX
5679 && piece_bitpos == bitpos
5680 && decl_piece_bitsize (*piece_loc) == bitsize)
5681 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5682 else
5683 last_loc_note = NULL_RTX;
5684 /* If the current location is the same as the end of the list,
5685 and either both or neither of the locations is uninitialized,
5686 we have nothing to do. */
5687 if (last_loc_note == NULL_RTX
5688 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5689 NOTE_VAR_LOCATION_LOC (loc_note)))
5690 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5691 != NOTE_VAR_LOCATION_STATUS (loc_note))
5692 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5693 == VAR_INIT_STATUS_UNINITIALIZED)
5694 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5695 == VAR_INIT_STATUS_UNINITIALIZED))))
5697 /* Add LOC to the end of list and update LAST. If the last
5698 element of the list has been removed above, reuse its
5699 memory for the new node, otherwise allocate a new one. */
5700 if (unused)
5702 loc = unused;
5703 memset (loc, '\0', sizeof (*loc));
5705 else
5706 loc = ggc_cleared_alloc<var_loc_node> ();
5707 if (bitsize == -1 || piece_loc == NULL)
5708 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5709 else
5710 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5711 bitpos, piece_bitpos, bitsize, loc_note);
5712 last->next = loc;
5713 /* Ensure TEMP->LAST will point either to the new last but one
5714 element of the chain, or to the last element in it. */
5715 if (last != temp->last)
5716 temp->last = last;
5718 else if (unused)
5719 ggc_free (unused);
5721 else
5723 loc = ggc_cleared_alloc<var_loc_node> ();
5724 temp->first = loc;
5725 temp->last = loc;
5726 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5728 return loc;
5731 /* Keep track of the number of spaces used to indent the
5732 output of the debugging routines that print the structure of
5733 the DIE internal representation. */
5734 static int print_indent;
5736 /* Indent the line the number of spaces given by print_indent. */
5738 static inline void
5739 print_spaces (FILE *outfile)
5741 fprintf (outfile, "%*s", print_indent, "");
5744 /* Print a type signature in hex. */
5746 static inline void
5747 print_signature (FILE *outfile, char *sig)
5749 int i;
5751 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5752 fprintf (outfile, "%02x", sig[i] & 0xff);
5755 static inline void
5756 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5758 if (discr_value->pos)
5759 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5760 else
5761 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5764 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5766 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5767 RECURSE, output location descriptor operations. */
5769 static void
5770 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5772 switch (val->val_class)
5774 case dw_val_class_addr:
5775 fprintf (outfile, "address");
5776 break;
5777 case dw_val_class_offset:
5778 fprintf (outfile, "offset");
5779 break;
5780 case dw_val_class_loc:
5781 fprintf (outfile, "location descriptor");
5782 if (val->v.val_loc == NULL)
5783 fprintf (outfile, " -> <null>\n");
5784 else if (recurse)
5786 fprintf (outfile, ":\n");
5787 print_indent += 4;
5788 print_loc_descr (val->v.val_loc, outfile);
5789 print_indent -= 4;
5791 else
5792 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5793 break;
5794 case dw_val_class_loc_list:
5795 fprintf (outfile, "location list -> label:%s",
5796 val->v.val_loc_list->ll_symbol);
5797 break;
5798 case dw_val_class_range_list:
5799 fprintf (outfile, "range list");
5800 break;
5801 case dw_val_class_const:
5802 case dw_val_class_const_implicit:
5803 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5804 break;
5805 case dw_val_class_unsigned_const:
5806 case dw_val_class_unsigned_const_implicit:
5807 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5808 break;
5809 case dw_val_class_const_double:
5810 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5811 HOST_WIDE_INT_PRINT_UNSIGNED")",
5812 val->v.val_double.high,
5813 val->v.val_double.low);
5814 break;
5815 case dw_val_class_wide_int:
5817 int i = val->v.val_wide->get_len ();
5818 fprintf (outfile, "constant (");
5819 gcc_assert (i > 0);
5820 if (val->v.val_wide->elt (i - 1) == 0)
5821 fprintf (outfile, "0x");
5822 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5823 val->v.val_wide->elt (--i));
5824 while (--i >= 0)
5825 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5826 val->v.val_wide->elt (i));
5827 fprintf (outfile, ")");
5828 break;
5830 case dw_val_class_vec:
5831 fprintf (outfile, "floating-point or vector constant");
5832 break;
5833 case dw_val_class_flag:
5834 fprintf (outfile, "%u", val->v.val_flag);
5835 break;
5836 case dw_val_class_die_ref:
5837 if (val->v.val_die_ref.die != NULL)
5839 dw_die_ref die = val->v.val_die_ref.die;
5841 if (die->comdat_type_p)
5843 fprintf (outfile, "die -> signature: ");
5844 print_signature (outfile,
5845 die->die_id.die_type_node->signature);
5847 else if (die->die_id.die_symbol)
5848 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5849 else
5850 fprintf (outfile, "die -> %ld", die->die_offset);
5851 fprintf (outfile, " (%p)", (void *) die);
5853 else
5854 fprintf (outfile, "die -> <null>");
5855 break;
5856 case dw_val_class_vms_delta:
5857 fprintf (outfile, "delta: @slotcount(%s-%s)",
5858 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5859 break;
5860 case dw_val_class_lbl_id:
5861 case dw_val_class_lineptr:
5862 case dw_val_class_macptr:
5863 case dw_val_class_loclistsptr:
5864 case dw_val_class_high_pc:
5865 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5866 break;
5867 case dw_val_class_str:
5868 if (val->v.val_str->str != NULL)
5869 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5870 else
5871 fprintf (outfile, "<null>");
5872 break;
5873 case dw_val_class_file:
5874 case dw_val_class_file_implicit:
5875 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5876 val->v.val_file->emitted_number);
5877 break;
5878 case dw_val_class_data8:
5880 int i;
5882 for (i = 0; i < 8; i++)
5883 fprintf (outfile, "%02x", val->v.val_data8[i]);
5884 break;
5886 case dw_val_class_discr_value:
5887 print_discr_value (outfile, &val->v.val_discr_value);
5888 break;
5889 case dw_val_class_discr_list:
5890 for (dw_discr_list_ref node = val->v.val_discr_list;
5891 node != NULL;
5892 node = node->dw_discr_next)
5894 if (node->dw_discr_range)
5896 fprintf (outfile, " .. ");
5897 print_discr_value (outfile, &node->dw_discr_lower_bound);
5898 print_discr_value (outfile, &node->dw_discr_upper_bound);
5900 else
5901 print_discr_value (outfile, &node->dw_discr_lower_bound);
5903 if (node->dw_discr_next != NULL)
5904 fprintf (outfile, " | ");
5906 default:
5907 break;
5911 /* Likewise, for a DIE attribute. */
5913 static void
5914 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5916 print_dw_val (&a->dw_attr_val, recurse, outfile);
5920 /* Print the list of operands in the LOC location description to OUTFILE. This
5921 routine is a debugging aid only. */
5923 static void
5924 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5926 dw_loc_descr_ref l = loc;
5928 if (loc == NULL)
5930 print_spaces (outfile);
5931 fprintf (outfile, "<null>\n");
5932 return;
5935 for (l = loc; l != NULL; l = l->dw_loc_next)
5937 print_spaces (outfile);
5938 fprintf (outfile, "(%p) %s",
5939 (void *) l,
5940 dwarf_stack_op_name (l->dw_loc_opc));
5941 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5943 fprintf (outfile, " ");
5944 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5946 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5948 fprintf (outfile, ", ");
5949 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5951 fprintf (outfile, "\n");
5955 /* Print the information associated with a given DIE, and its children.
5956 This routine is a debugging aid only. */
5958 static void
5959 print_die (dw_die_ref die, FILE *outfile)
5961 dw_attr_node *a;
5962 dw_die_ref c;
5963 unsigned ix;
5965 print_spaces (outfile);
5966 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5967 die->die_offset, dwarf_tag_name (die->die_tag),
5968 (void*) die);
5969 print_spaces (outfile);
5970 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5971 fprintf (outfile, " offset: %ld", die->die_offset);
5972 fprintf (outfile, " mark: %d\n", die->die_mark);
5974 if (die->comdat_type_p)
5976 print_spaces (outfile);
5977 fprintf (outfile, " signature: ");
5978 print_signature (outfile, die->die_id.die_type_node->signature);
5979 fprintf (outfile, "\n");
5982 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5984 print_spaces (outfile);
5985 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5987 print_attribute (a, true, outfile);
5988 fprintf (outfile, "\n");
5991 if (die->die_child != NULL)
5993 print_indent += 4;
5994 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5995 print_indent -= 4;
5997 if (print_indent == 0)
5998 fprintf (outfile, "\n");
6001 /* Print the list of operations in the LOC location description. */
6003 DEBUG_FUNCTION void
6004 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6006 print_loc_descr (loc, stderr);
6009 /* Print the information collected for a given DIE. */
6011 DEBUG_FUNCTION void
6012 debug_dwarf_die (dw_die_ref die)
6014 print_die (die, stderr);
6017 DEBUG_FUNCTION void
6018 debug (die_struct &ref)
6020 print_die (&ref, stderr);
6023 DEBUG_FUNCTION void
6024 debug (die_struct *ptr)
6026 if (ptr)
6027 debug (*ptr);
6028 else
6029 fprintf (stderr, "<nil>\n");
6033 /* Print all DWARF information collected for the compilation unit.
6034 This routine is a debugging aid only. */
6036 DEBUG_FUNCTION void
6037 debug_dwarf (void)
6039 print_indent = 0;
6040 print_die (comp_unit_die (), stderr);
6043 /* Verify the DIE tree structure. */
6045 DEBUG_FUNCTION void
6046 verify_die (dw_die_ref die)
6048 gcc_assert (!die->die_mark);
6049 if (die->die_parent == NULL
6050 && die->die_sib == NULL)
6051 return;
6052 /* Verify the die_sib list is cyclic. */
6053 dw_die_ref x = die;
6056 x->die_mark = 1;
6057 x = x->die_sib;
6059 while (x && !x->die_mark);
6060 gcc_assert (x == die);
6061 x = die;
6064 /* Verify all dies have the same parent. */
6065 gcc_assert (x->die_parent == die->die_parent);
6066 if (x->die_child)
6068 /* Verify the child has the proper parent and recurse. */
6069 gcc_assert (x->die_child->die_parent == x);
6070 verify_die (x->die_child);
6072 x->die_mark = 0;
6073 x = x->die_sib;
6075 while (x && x->die_mark);
6078 /* Sanity checks on DIEs. */
6080 static void
6081 check_die (dw_die_ref die)
6083 unsigned ix;
6084 dw_attr_node *a;
6085 bool inline_found = false;
6086 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6087 int n_decl_line = 0, n_decl_file = 0;
6088 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6090 switch (a->dw_attr)
6092 case DW_AT_inline:
6093 if (a->dw_attr_val.v.val_unsigned)
6094 inline_found = true;
6095 break;
6096 case DW_AT_location:
6097 ++n_location;
6098 break;
6099 case DW_AT_low_pc:
6100 ++n_low_pc;
6101 break;
6102 case DW_AT_high_pc:
6103 ++n_high_pc;
6104 break;
6105 case DW_AT_artificial:
6106 ++n_artificial;
6107 break;
6108 case DW_AT_decl_line:
6109 ++n_decl_line;
6110 break;
6111 case DW_AT_decl_file:
6112 ++n_decl_file;
6113 break;
6114 default:
6115 break;
6118 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6119 || n_decl_line > 1 || n_decl_file > 1)
6121 fprintf (stderr, "Duplicate attributes in DIE:\n");
6122 debug_dwarf_die (die);
6123 gcc_unreachable ();
6125 if (inline_found)
6127 /* A debugging information entry that is a member of an abstract
6128 instance tree [that has DW_AT_inline] should not contain any
6129 attributes which describe aspects of the subroutine which vary
6130 between distinct inlined expansions or distinct out-of-line
6131 expansions. */
6132 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6133 gcc_assert (a->dw_attr != DW_AT_low_pc
6134 && a->dw_attr != DW_AT_high_pc
6135 && a->dw_attr != DW_AT_location
6136 && a->dw_attr != DW_AT_frame_base
6137 && a->dw_attr != DW_AT_call_all_calls
6138 && a->dw_attr != DW_AT_GNU_all_call_sites);
6142 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6143 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6144 DIE that marks the start of the DIEs for this include file. */
6146 static dw_die_ref
6147 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6149 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6150 dw_die_ref new_unit = gen_compile_unit_die (filename);
6152 new_unit->die_sib = old_unit;
6153 return new_unit;
6156 /* Close an include-file CU and reopen the enclosing one. */
6158 static dw_die_ref
6159 pop_compile_unit (dw_die_ref old_unit)
6161 dw_die_ref new_unit = old_unit->die_sib;
6163 old_unit->die_sib = NULL;
6164 return new_unit;
6167 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6168 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6169 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6171 /* Calculate the checksum of a location expression. */
6173 static inline void
6174 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6176 int tem;
6177 inchash::hash hstate;
6178 hashval_t hash;
6180 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6181 CHECKSUM (tem);
6182 hash_loc_operands (loc, hstate);
6183 hash = hstate.end();
6184 CHECKSUM (hash);
6187 /* Calculate the checksum of an attribute. */
6189 static void
6190 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6192 dw_loc_descr_ref loc;
6193 rtx r;
6195 CHECKSUM (at->dw_attr);
6197 /* We don't care that this was compiled with a different compiler
6198 snapshot; if the output is the same, that's what matters. */
6199 if (at->dw_attr == DW_AT_producer)
6200 return;
6202 switch (AT_class (at))
6204 case dw_val_class_const:
6205 case dw_val_class_const_implicit:
6206 CHECKSUM (at->dw_attr_val.v.val_int);
6207 break;
6208 case dw_val_class_unsigned_const:
6209 case dw_val_class_unsigned_const_implicit:
6210 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6211 break;
6212 case dw_val_class_const_double:
6213 CHECKSUM (at->dw_attr_val.v.val_double);
6214 break;
6215 case dw_val_class_wide_int:
6216 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6217 get_full_len (*at->dw_attr_val.v.val_wide)
6218 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6219 break;
6220 case dw_val_class_vec:
6221 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6222 (at->dw_attr_val.v.val_vec.length
6223 * at->dw_attr_val.v.val_vec.elt_size));
6224 break;
6225 case dw_val_class_flag:
6226 CHECKSUM (at->dw_attr_val.v.val_flag);
6227 break;
6228 case dw_val_class_str:
6229 CHECKSUM_STRING (AT_string (at));
6230 break;
6232 case dw_val_class_addr:
6233 r = AT_addr (at);
6234 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6235 CHECKSUM_STRING (XSTR (r, 0));
6236 break;
6238 case dw_val_class_offset:
6239 CHECKSUM (at->dw_attr_val.v.val_offset);
6240 break;
6242 case dw_val_class_loc:
6243 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6244 loc_checksum (loc, ctx);
6245 break;
6247 case dw_val_class_die_ref:
6248 die_checksum (AT_ref (at), ctx, mark);
6249 break;
6251 case dw_val_class_fde_ref:
6252 case dw_val_class_vms_delta:
6253 case dw_val_class_lbl_id:
6254 case dw_val_class_lineptr:
6255 case dw_val_class_macptr:
6256 case dw_val_class_loclistsptr:
6257 case dw_val_class_high_pc:
6258 break;
6260 case dw_val_class_file:
6261 case dw_val_class_file_implicit:
6262 CHECKSUM_STRING (AT_file (at)->filename);
6263 break;
6265 case dw_val_class_data8:
6266 CHECKSUM (at->dw_attr_val.v.val_data8);
6267 break;
6269 default:
6270 break;
6274 /* Calculate the checksum of a DIE. */
6276 static void
6277 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6279 dw_die_ref c;
6280 dw_attr_node *a;
6281 unsigned ix;
6283 /* To avoid infinite recursion. */
6284 if (die->die_mark)
6286 CHECKSUM (die->die_mark);
6287 return;
6289 die->die_mark = ++(*mark);
6291 CHECKSUM (die->die_tag);
6293 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6294 attr_checksum (a, ctx, mark);
6296 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6299 #undef CHECKSUM
6300 #undef CHECKSUM_BLOCK
6301 #undef CHECKSUM_STRING
6303 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6304 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6305 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6306 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6307 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6308 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6309 #define CHECKSUM_ATTR(FOO) \
6310 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6312 /* Calculate the checksum of a number in signed LEB128 format. */
6314 static void
6315 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6317 unsigned char byte;
6318 bool more;
6320 while (1)
6322 byte = (value & 0x7f);
6323 value >>= 7;
6324 more = !((value == 0 && (byte & 0x40) == 0)
6325 || (value == -1 && (byte & 0x40) != 0));
6326 if (more)
6327 byte |= 0x80;
6328 CHECKSUM (byte);
6329 if (!more)
6330 break;
6334 /* Calculate the checksum of a number in unsigned LEB128 format. */
6336 static void
6337 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6339 while (1)
6341 unsigned char byte = (value & 0x7f);
6342 value >>= 7;
6343 if (value != 0)
6344 /* More bytes to follow. */
6345 byte |= 0x80;
6346 CHECKSUM (byte);
6347 if (value == 0)
6348 break;
6352 /* Checksum the context of the DIE. This adds the names of any
6353 surrounding namespaces or structures to the checksum. */
6355 static void
6356 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6358 const char *name;
6359 dw_die_ref spec;
6360 int tag = die->die_tag;
6362 if (tag != DW_TAG_namespace
6363 && tag != DW_TAG_structure_type
6364 && tag != DW_TAG_class_type)
6365 return;
6367 name = get_AT_string (die, DW_AT_name);
6369 spec = get_AT_ref (die, DW_AT_specification);
6370 if (spec != NULL)
6371 die = spec;
6373 if (die->die_parent != NULL)
6374 checksum_die_context (die->die_parent, ctx);
6376 CHECKSUM_ULEB128 ('C');
6377 CHECKSUM_ULEB128 (tag);
6378 if (name != NULL)
6379 CHECKSUM_STRING (name);
6382 /* Calculate the checksum of a location expression. */
6384 static inline void
6385 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6387 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6388 were emitted as a DW_FORM_sdata instead of a location expression. */
6389 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6391 CHECKSUM_ULEB128 (DW_FORM_sdata);
6392 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6393 return;
6396 /* Otherwise, just checksum the raw location expression. */
6397 while (loc != NULL)
6399 inchash::hash hstate;
6400 hashval_t hash;
6402 CHECKSUM_ULEB128 (loc->dtprel);
6403 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6404 hash_loc_operands (loc, hstate);
6405 hash = hstate.end ();
6406 CHECKSUM (hash);
6407 loc = loc->dw_loc_next;
6411 /* Calculate the checksum of an attribute. */
6413 static void
6414 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6415 struct md5_ctx *ctx, int *mark)
6417 dw_loc_descr_ref loc;
6418 rtx r;
6420 if (AT_class (at) == dw_val_class_die_ref)
6422 dw_die_ref target_die = AT_ref (at);
6424 /* For pointer and reference types, we checksum only the (qualified)
6425 name of the target type (if there is a name). For friend entries,
6426 we checksum only the (qualified) name of the target type or function.
6427 This allows the checksum to remain the same whether the target type
6428 is complete or not. */
6429 if ((at->dw_attr == DW_AT_type
6430 && (tag == DW_TAG_pointer_type
6431 || tag == DW_TAG_reference_type
6432 || tag == DW_TAG_rvalue_reference_type
6433 || tag == DW_TAG_ptr_to_member_type))
6434 || (at->dw_attr == DW_AT_friend
6435 && tag == DW_TAG_friend))
6437 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6439 if (name_attr != NULL)
6441 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6443 if (decl == NULL)
6444 decl = target_die;
6445 CHECKSUM_ULEB128 ('N');
6446 CHECKSUM_ULEB128 (at->dw_attr);
6447 if (decl->die_parent != NULL)
6448 checksum_die_context (decl->die_parent, ctx);
6449 CHECKSUM_ULEB128 ('E');
6450 CHECKSUM_STRING (AT_string (name_attr));
6451 return;
6455 /* For all other references to another DIE, we check to see if the
6456 target DIE has already been visited. If it has, we emit a
6457 backward reference; if not, we descend recursively. */
6458 if (target_die->die_mark > 0)
6460 CHECKSUM_ULEB128 ('R');
6461 CHECKSUM_ULEB128 (at->dw_attr);
6462 CHECKSUM_ULEB128 (target_die->die_mark);
6464 else
6466 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6468 if (decl == NULL)
6469 decl = target_die;
6470 target_die->die_mark = ++(*mark);
6471 CHECKSUM_ULEB128 ('T');
6472 CHECKSUM_ULEB128 (at->dw_attr);
6473 if (decl->die_parent != NULL)
6474 checksum_die_context (decl->die_parent, ctx);
6475 die_checksum_ordered (target_die, ctx, mark);
6477 return;
6480 CHECKSUM_ULEB128 ('A');
6481 CHECKSUM_ULEB128 (at->dw_attr);
6483 switch (AT_class (at))
6485 case dw_val_class_const:
6486 case dw_val_class_const_implicit:
6487 CHECKSUM_ULEB128 (DW_FORM_sdata);
6488 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6489 break;
6491 case dw_val_class_unsigned_const:
6492 case dw_val_class_unsigned_const_implicit:
6493 CHECKSUM_ULEB128 (DW_FORM_sdata);
6494 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6495 break;
6497 case dw_val_class_const_double:
6498 CHECKSUM_ULEB128 (DW_FORM_block);
6499 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6500 CHECKSUM (at->dw_attr_val.v.val_double);
6501 break;
6503 case dw_val_class_wide_int:
6504 CHECKSUM_ULEB128 (DW_FORM_block);
6505 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6506 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6507 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6508 get_full_len (*at->dw_attr_val.v.val_wide)
6509 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6510 break;
6512 case dw_val_class_vec:
6513 CHECKSUM_ULEB128 (DW_FORM_block);
6514 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6515 * at->dw_attr_val.v.val_vec.elt_size);
6516 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6517 (at->dw_attr_val.v.val_vec.length
6518 * at->dw_attr_val.v.val_vec.elt_size));
6519 break;
6521 case dw_val_class_flag:
6522 CHECKSUM_ULEB128 (DW_FORM_flag);
6523 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6524 break;
6526 case dw_val_class_str:
6527 CHECKSUM_ULEB128 (DW_FORM_string);
6528 CHECKSUM_STRING (AT_string (at));
6529 break;
6531 case dw_val_class_addr:
6532 r = AT_addr (at);
6533 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6534 CHECKSUM_ULEB128 (DW_FORM_string);
6535 CHECKSUM_STRING (XSTR (r, 0));
6536 break;
6538 case dw_val_class_offset:
6539 CHECKSUM_ULEB128 (DW_FORM_sdata);
6540 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6541 break;
6543 case dw_val_class_loc:
6544 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6545 loc_checksum_ordered (loc, ctx);
6546 break;
6548 case dw_val_class_fde_ref:
6549 case dw_val_class_lbl_id:
6550 case dw_val_class_lineptr:
6551 case dw_val_class_macptr:
6552 case dw_val_class_loclistsptr:
6553 case dw_val_class_high_pc:
6554 break;
6556 case dw_val_class_file:
6557 case dw_val_class_file_implicit:
6558 CHECKSUM_ULEB128 (DW_FORM_string);
6559 CHECKSUM_STRING (AT_file (at)->filename);
6560 break;
6562 case dw_val_class_data8:
6563 CHECKSUM (at->dw_attr_val.v.val_data8);
6564 break;
6566 default:
6567 break;
6571 struct checksum_attributes
6573 dw_attr_node *at_name;
6574 dw_attr_node *at_type;
6575 dw_attr_node *at_friend;
6576 dw_attr_node *at_accessibility;
6577 dw_attr_node *at_address_class;
6578 dw_attr_node *at_allocated;
6579 dw_attr_node *at_artificial;
6580 dw_attr_node *at_associated;
6581 dw_attr_node *at_binary_scale;
6582 dw_attr_node *at_bit_offset;
6583 dw_attr_node *at_bit_size;
6584 dw_attr_node *at_bit_stride;
6585 dw_attr_node *at_byte_size;
6586 dw_attr_node *at_byte_stride;
6587 dw_attr_node *at_const_value;
6588 dw_attr_node *at_containing_type;
6589 dw_attr_node *at_count;
6590 dw_attr_node *at_data_location;
6591 dw_attr_node *at_data_member_location;
6592 dw_attr_node *at_decimal_scale;
6593 dw_attr_node *at_decimal_sign;
6594 dw_attr_node *at_default_value;
6595 dw_attr_node *at_digit_count;
6596 dw_attr_node *at_discr;
6597 dw_attr_node *at_discr_list;
6598 dw_attr_node *at_discr_value;
6599 dw_attr_node *at_encoding;
6600 dw_attr_node *at_endianity;
6601 dw_attr_node *at_explicit;
6602 dw_attr_node *at_is_optional;
6603 dw_attr_node *at_location;
6604 dw_attr_node *at_lower_bound;
6605 dw_attr_node *at_mutable;
6606 dw_attr_node *at_ordering;
6607 dw_attr_node *at_picture_string;
6608 dw_attr_node *at_prototyped;
6609 dw_attr_node *at_small;
6610 dw_attr_node *at_segment;
6611 dw_attr_node *at_string_length;
6612 dw_attr_node *at_string_length_bit_size;
6613 dw_attr_node *at_string_length_byte_size;
6614 dw_attr_node *at_threads_scaled;
6615 dw_attr_node *at_upper_bound;
6616 dw_attr_node *at_use_location;
6617 dw_attr_node *at_use_UTF8;
6618 dw_attr_node *at_variable_parameter;
6619 dw_attr_node *at_virtuality;
6620 dw_attr_node *at_visibility;
6621 dw_attr_node *at_vtable_elem_location;
6624 /* Collect the attributes that we will want to use for the checksum. */
6626 static void
6627 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6629 dw_attr_node *a;
6630 unsigned ix;
6632 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6634 switch (a->dw_attr)
6636 case DW_AT_name:
6637 attrs->at_name = a;
6638 break;
6639 case DW_AT_type:
6640 attrs->at_type = a;
6641 break;
6642 case DW_AT_friend:
6643 attrs->at_friend = a;
6644 break;
6645 case DW_AT_accessibility:
6646 attrs->at_accessibility = a;
6647 break;
6648 case DW_AT_address_class:
6649 attrs->at_address_class = a;
6650 break;
6651 case DW_AT_allocated:
6652 attrs->at_allocated = a;
6653 break;
6654 case DW_AT_artificial:
6655 attrs->at_artificial = a;
6656 break;
6657 case DW_AT_associated:
6658 attrs->at_associated = a;
6659 break;
6660 case DW_AT_binary_scale:
6661 attrs->at_binary_scale = a;
6662 break;
6663 case DW_AT_bit_offset:
6664 attrs->at_bit_offset = a;
6665 break;
6666 case DW_AT_bit_size:
6667 attrs->at_bit_size = a;
6668 break;
6669 case DW_AT_bit_stride:
6670 attrs->at_bit_stride = a;
6671 break;
6672 case DW_AT_byte_size:
6673 attrs->at_byte_size = a;
6674 break;
6675 case DW_AT_byte_stride:
6676 attrs->at_byte_stride = a;
6677 break;
6678 case DW_AT_const_value:
6679 attrs->at_const_value = a;
6680 break;
6681 case DW_AT_containing_type:
6682 attrs->at_containing_type = a;
6683 break;
6684 case DW_AT_count:
6685 attrs->at_count = a;
6686 break;
6687 case DW_AT_data_location:
6688 attrs->at_data_location = a;
6689 break;
6690 case DW_AT_data_member_location:
6691 attrs->at_data_member_location = a;
6692 break;
6693 case DW_AT_decimal_scale:
6694 attrs->at_decimal_scale = a;
6695 break;
6696 case DW_AT_decimal_sign:
6697 attrs->at_decimal_sign = a;
6698 break;
6699 case DW_AT_default_value:
6700 attrs->at_default_value = a;
6701 break;
6702 case DW_AT_digit_count:
6703 attrs->at_digit_count = a;
6704 break;
6705 case DW_AT_discr:
6706 attrs->at_discr = a;
6707 break;
6708 case DW_AT_discr_list:
6709 attrs->at_discr_list = a;
6710 break;
6711 case DW_AT_discr_value:
6712 attrs->at_discr_value = a;
6713 break;
6714 case DW_AT_encoding:
6715 attrs->at_encoding = a;
6716 break;
6717 case DW_AT_endianity:
6718 attrs->at_endianity = a;
6719 break;
6720 case DW_AT_explicit:
6721 attrs->at_explicit = a;
6722 break;
6723 case DW_AT_is_optional:
6724 attrs->at_is_optional = a;
6725 break;
6726 case DW_AT_location:
6727 attrs->at_location = a;
6728 break;
6729 case DW_AT_lower_bound:
6730 attrs->at_lower_bound = a;
6731 break;
6732 case DW_AT_mutable:
6733 attrs->at_mutable = a;
6734 break;
6735 case DW_AT_ordering:
6736 attrs->at_ordering = a;
6737 break;
6738 case DW_AT_picture_string:
6739 attrs->at_picture_string = a;
6740 break;
6741 case DW_AT_prototyped:
6742 attrs->at_prototyped = a;
6743 break;
6744 case DW_AT_small:
6745 attrs->at_small = a;
6746 break;
6747 case DW_AT_segment:
6748 attrs->at_segment = a;
6749 break;
6750 case DW_AT_string_length:
6751 attrs->at_string_length = a;
6752 break;
6753 case DW_AT_string_length_bit_size:
6754 attrs->at_string_length_bit_size = a;
6755 break;
6756 case DW_AT_string_length_byte_size:
6757 attrs->at_string_length_byte_size = a;
6758 break;
6759 case DW_AT_threads_scaled:
6760 attrs->at_threads_scaled = a;
6761 break;
6762 case DW_AT_upper_bound:
6763 attrs->at_upper_bound = a;
6764 break;
6765 case DW_AT_use_location:
6766 attrs->at_use_location = a;
6767 break;
6768 case DW_AT_use_UTF8:
6769 attrs->at_use_UTF8 = a;
6770 break;
6771 case DW_AT_variable_parameter:
6772 attrs->at_variable_parameter = a;
6773 break;
6774 case DW_AT_virtuality:
6775 attrs->at_virtuality = a;
6776 break;
6777 case DW_AT_visibility:
6778 attrs->at_visibility = a;
6779 break;
6780 case DW_AT_vtable_elem_location:
6781 attrs->at_vtable_elem_location = a;
6782 break;
6783 default:
6784 break;
6789 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6791 static void
6792 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6794 dw_die_ref c;
6795 dw_die_ref decl;
6796 struct checksum_attributes attrs;
6798 CHECKSUM_ULEB128 ('D');
6799 CHECKSUM_ULEB128 (die->die_tag);
6801 memset (&attrs, 0, sizeof (attrs));
6803 decl = get_AT_ref (die, DW_AT_specification);
6804 if (decl != NULL)
6805 collect_checksum_attributes (&attrs, decl);
6806 collect_checksum_attributes (&attrs, die);
6808 CHECKSUM_ATTR (attrs.at_name);
6809 CHECKSUM_ATTR (attrs.at_accessibility);
6810 CHECKSUM_ATTR (attrs.at_address_class);
6811 CHECKSUM_ATTR (attrs.at_allocated);
6812 CHECKSUM_ATTR (attrs.at_artificial);
6813 CHECKSUM_ATTR (attrs.at_associated);
6814 CHECKSUM_ATTR (attrs.at_binary_scale);
6815 CHECKSUM_ATTR (attrs.at_bit_offset);
6816 CHECKSUM_ATTR (attrs.at_bit_size);
6817 CHECKSUM_ATTR (attrs.at_bit_stride);
6818 CHECKSUM_ATTR (attrs.at_byte_size);
6819 CHECKSUM_ATTR (attrs.at_byte_stride);
6820 CHECKSUM_ATTR (attrs.at_const_value);
6821 CHECKSUM_ATTR (attrs.at_containing_type);
6822 CHECKSUM_ATTR (attrs.at_count);
6823 CHECKSUM_ATTR (attrs.at_data_location);
6824 CHECKSUM_ATTR (attrs.at_data_member_location);
6825 CHECKSUM_ATTR (attrs.at_decimal_scale);
6826 CHECKSUM_ATTR (attrs.at_decimal_sign);
6827 CHECKSUM_ATTR (attrs.at_default_value);
6828 CHECKSUM_ATTR (attrs.at_digit_count);
6829 CHECKSUM_ATTR (attrs.at_discr);
6830 CHECKSUM_ATTR (attrs.at_discr_list);
6831 CHECKSUM_ATTR (attrs.at_discr_value);
6832 CHECKSUM_ATTR (attrs.at_encoding);
6833 CHECKSUM_ATTR (attrs.at_endianity);
6834 CHECKSUM_ATTR (attrs.at_explicit);
6835 CHECKSUM_ATTR (attrs.at_is_optional);
6836 CHECKSUM_ATTR (attrs.at_location);
6837 CHECKSUM_ATTR (attrs.at_lower_bound);
6838 CHECKSUM_ATTR (attrs.at_mutable);
6839 CHECKSUM_ATTR (attrs.at_ordering);
6840 CHECKSUM_ATTR (attrs.at_picture_string);
6841 CHECKSUM_ATTR (attrs.at_prototyped);
6842 CHECKSUM_ATTR (attrs.at_small);
6843 CHECKSUM_ATTR (attrs.at_segment);
6844 CHECKSUM_ATTR (attrs.at_string_length);
6845 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6846 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6847 CHECKSUM_ATTR (attrs.at_threads_scaled);
6848 CHECKSUM_ATTR (attrs.at_upper_bound);
6849 CHECKSUM_ATTR (attrs.at_use_location);
6850 CHECKSUM_ATTR (attrs.at_use_UTF8);
6851 CHECKSUM_ATTR (attrs.at_variable_parameter);
6852 CHECKSUM_ATTR (attrs.at_virtuality);
6853 CHECKSUM_ATTR (attrs.at_visibility);
6854 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6855 CHECKSUM_ATTR (attrs.at_type);
6856 CHECKSUM_ATTR (attrs.at_friend);
6858 /* Checksum the child DIEs. */
6859 c = die->die_child;
6860 if (c) do {
6861 dw_attr_node *name_attr;
6863 c = c->die_sib;
6864 name_attr = get_AT (c, DW_AT_name);
6865 if (is_template_instantiation (c))
6867 /* Ignore instantiations of member type and function templates. */
6869 else if (name_attr != NULL
6870 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6872 /* Use a shallow checksum for named nested types and member
6873 functions. */
6874 CHECKSUM_ULEB128 ('S');
6875 CHECKSUM_ULEB128 (c->die_tag);
6876 CHECKSUM_STRING (AT_string (name_attr));
6878 else
6880 /* Use a deep checksum for other children. */
6881 /* Mark this DIE so it gets processed when unmarking. */
6882 if (c->die_mark == 0)
6883 c->die_mark = -1;
6884 die_checksum_ordered (c, ctx, mark);
6886 } while (c != die->die_child);
6888 CHECKSUM_ULEB128 (0);
6891 /* Add a type name and tag to a hash. */
6892 static void
6893 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6895 CHECKSUM_ULEB128 (tag);
6896 CHECKSUM_STRING (name);
6899 #undef CHECKSUM
6900 #undef CHECKSUM_STRING
6901 #undef CHECKSUM_ATTR
6902 #undef CHECKSUM_LEB128
6903 #undef CHECKSUM_ULEB128
6905 /* Generate the type signature for DIE. This is computed by generating an
6906 MD5 checksum over the DIE's tag, its relevant attributes, and its
6907 children. Attributes that are references to other DIEs are processed
6908 by recursion, using the MARK field to prevent infinite recursion.
6909 If the DIE is nested inside a namespace or another type, we also
6910 need to include that context in the signature. The lower 64 bits
6911 of the resulting MD5 checksum comprise the signature. */
6913 static void
6914 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6916 int mark;
6917 const char *name;
6918 unsigned char checksum[16];
6919 struct md5_ctx ctx;
6920 dw_die_ref decl;
6921 dw_die_ref parent;
6923 name = get_AT_string (die, DW_AT_name);
6924 decl = get_AT_ref (die, DW_AT_specification);
6925 parent = get_die_parent (die);
6927 /* First, compute a signature for just the type name (and its surrounding
6928 context, if any. This is stored in the type unit DIE for link-time
6929 ODR (one-definition rule) checking. */
6931 if (is_cxx () && name != NULL)
6933 md5_init_ctx (&ctx);
6935 /* Checksum the names of surrounding namespaces and structures. */
6936 if (parent != NULL)
6937 checksum_die_context (parent, &ctx);
6939 /* Checksum the current DIE. */
6940 die_odr_checksum (die->die_tag, name, &ctx);
6941 md5_finish_ctx (&ctx, checksum);
6943 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6946 /* Next, compute the complete type signature. */
6948 md5_init_ctx (&ctx);
6949 mark = 1;
6950 die->die_mark = mark;
6952 /* Checksum the names of surrounding namespaces and structures. */
6953 if (parent != NULL)
6954 checksum_die_context (parent, &ctx);
6956 /* Checksum the DIE and its children. */
6957 die_checksum_ordered (die, &ctx, &mark);
6958 unmark_all_dies (die);
6959 md5_finish_ctx (&ctx, checksum);
6961 /* Store the signature in the type node and link the type DIE and the
6962 type node together. */
6963 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6964 DWARF_TYPE_SIGNATURE_SIZE);
6965 die->comdat_type_p = true;
6966 die->die_id.die_type_node = type_node;
6967 type_node->type_die = die;
6969 /* If the DIE is a specification, link its declaration to the type node
6970 as well. */
6971 if (decl != NULL)
6973 decl->comdat_type_p = true;
6974 decl->die_id.die_type_node = type_node;
6978 /* Do the location expressions look same? */
6979 static inline int
6980 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6982 return loc1->dw_loc_opc == loc2->dw_loc_opc
6983 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6984 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6987 /* Do the values look the same? */
6988 static int
6989 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6991 dw_loc_descr_ref loc1, loc2;
6992 rtx r1, r2;
6994 if (v1->val_class != v2->val_class)
6995 return 0;
6997 switch (v1->val_class)
6999 case dw_val_class_const:
7000 case dw_val_class_const_implicit:
7001 return v1->v.val_int == v2->v.val_int;
7002 case dw_val_class_unsigned_const:
7003 case dw_val_class_unsigned_const_implicit:
7004 return v1->v.val_unsigned == v2->v.val_unsigned;
7005 case dw_val_class_const_double:
7006 return v1->v.val_double.high == v2->v.val_double.high
7007 && v1->v.val_double.low == v2->v.val_double.low;
7008 case dw_val_class_wide_int:
7009 return *v1->v.val_wide == *v2->v.val_wide;
7010 case dw_val_class_vec:
7011 if (v1->v.val_vec.length != v2->v.val_vec.length
7012 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7013 return 0;
7014 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7015 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7016 return 0;
7017 return 1;
7018 case dw_val_class_flag:
7019 return v1->v.val_flag == v2->v.val_flag;
7020 case dw_val_class_str:
7021 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7023 case dw_val_class_addr:
7024 r1 = v1->v.val_addr;
7025 r2 = v2->v.val_addr;
7026 if (GET_CODE (r1) != GET_CODE (r2))
7027 return 0;
7028 return !rtx_equal_p (r1, r2);
7030 case dw_val_class_offset:
7031 return v1->v.val_offset == v2->v.val_offset;
7033 case dw_val_class_loc:
7034 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7035 loc1 && loc2;
7036 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7037 if (!same_loc_p (loc1, loc2, mark))
7038 return 0;
7039 return !loc1 && !loc2;
7041 case dw_val_class_die_ref:
7042 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7044 case dw_val_class_fde_ref:
7045 case dw_val_class_vms_delta:
7046 case dw_val_class_lbl_id:
7047 case dw_val_class_lineptr:
7048 case dw_val_class_macptr:
7049 case dw_val_class_loclistsptr:
7050 case dw_val_class_high_pc:
7051 return 1;
7053 case dw_val_class_file:
7054 case dw_val_class_file_implicit:
7055 return v1->v.val_file == v2->v.val_file;
7057 case dw_val_class_data8:
7058 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7060 default:
7061 return 1;
7065 /* Do the attributes look the same? */
7067 static int
7068 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7070 if (at1->dw_attr != at2->dw_attr)
7071 return 0;
7073 /* We don't care that this was compiled with a different compiler
7074 snapshot; if the output is the same, that's what matters. */
7075 if (at1->dw_attr == DW_AT_producer)
7076 return 1;
7078 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7081 /* Do the dies look the same? */
7083 static int
7084 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7086 dw_die_ref c1, c2;
7087 dw_attr_node *a1;
7088 unsigned ix;
7090 /* To avoid infinite recursion. */
7091 if (die1->die_mark)
7092 return die1->die_mark == die2->die_mark;
7093 die1->die_mark = die2->die_mark = ++(*mark);
7095 if (die1->die_tag != die2->die_tag)
7096 return 0;
7098 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7099 return 0;
7101 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7102 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7103 return 0;
7105 c1 = die1->die_child;
7106 c2 = die2->die_child;
7107 if (! c1)
7109 if (c2)
7110 return 0;
7112 else
7113 for (;;)
7115 if (!same_die_p (c1, c2, mark))
7116 return 0;
7117 c1 = c1->die_sib;
7118 c2 = c2->die_sib;
7119 if (c1 == die1->die_child)
7121 if (c2 == die2->die_child)
7122 break;
7123 else
7124 return 0;
7128 return 1;
7131 /* Do the dies look the same? Wrapper around same_die_p. */
7133 static int
7134 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7136 int mark = 0;
7137 int ret = same_die_p (die1, die2, &mark);
7139 unmark_all_dies (die1);
7140 unmark_all_dies (die2);
7142 return ret;
7145 /* The prefix to attach to symbols on DIEs in the current comdat debug
7146 info section. */
7147 static const char *comdat_symbol_id;
7149 /* The index of the current symbol within the current comdat CU. */
7150 static unsigned int comdat_symbol_number;
7152 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7153 children, and set comdat_symbol_id accordingly. */
7155 static void
7156 compute_section_prefix (dw_die_ref unit_die)
7158 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7159 const char *base = die_name ? lbasename (die_name) : "anonymous";
7160 char *name = XALLOCAVEC (char, strlen (base) + 64);
7161 char *p;
7162 int i, mark;
7163 unsigned char checksum[16];
7164 struct md5_ctx ctx;
7166 /* Compute the checksum of the DIE, then append part of it as hex digits to
7167 the name filename of the unit. */
7169 md5_init_ctx (&ctx);
7170 mark = 0;
7171 die_checksum (unit_die, &ctx, &mark);
7172 unmark_all_dies (unit_die);
7173 md5_finish_ctx (&ctx, checksum);
7175 sprintf (name, "%s.", base);
7176 clean_symbol_name (name);
7178 p = name + strlen (name);
7179 for (i = 0; i < 4; i++)
7181 sprintf (p, "%.2x", checksum[i]);
7182 p += 2;
7185 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7186 comdat_symbol_number = 0;
7189 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7191 static int
7192 is_type_die (dw_die_ref die)
7194 switch (die->die_tag)
7196 case DW_TAG_array_type:
7197 case DW_TAG_class_type:
7198 case DW_TAG_interface_type:
7199 case DW_TAG_enumeration_type:
7200 case DW_TAG_pointer_type:
7201 case DW_TAG_reference_type:
7202 case DW_TAG_rvalue_reference_type:
7203 case DW_TAG_string_type:
7204 case DW_TAG_structure_type:
7205 case DW_TAG_subroutine_type:
7206 case DW_TAG_union_type:
7207 case DW_TAG_ptr_to_member_type:
7208 case DW_TAG_set_type:
7209 case DW_TAG_subrange_type:
7210 case DW_TAG_base_type:
7211 case DW_TAG_const_type:
7212 case DW_TAG_file_type:
7213 case DW_TAG_packed_type:
7214 case DW_TAG_volatile_type:
7215 case DW_TAG_typedef:
7216 return 1;
7217 default:
7218 return 0;
7222 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7223 Basically, we want to choose the bits that are likely to be shared between
7224 compilations (types) and leave out the bits that are specific to individual
7225 compilations (functions). */
7227 static int
7228 is_comdat_die (dw_die_ref c)
7230 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7231 we do for stabs. The advantage is a greater likelihood of sharing between
7232 objects that don't include headers in the same order (and therefore would
7233 put the base types in a different comdat). jason 8/28/00 */
7235 if (c->die_tag == DW_TAG_base_type)
7236 return 0;
7238 if (c->die_tag == DW_TAG_pointer_type
7239 || c->die_tag == DW_TAG_reference_type
7240 || c->die_tag == DW_TAG_rvalue_reference_type
7241 || c->die_tag == DW_TAG_const_type
7242 || c->die_tag == DW_TAG_volatile_type)
7244 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7246 return t ? is_comdat_die (t) : 0;
7249 return is_type_die (c);
7252 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7253 compilation unit. */
7255 static int
7256 is_symbol_die (dw_die_ref c)
7258 return (is_type_die (c)
7259 || is_declaration_die (c)
7260 || c->die_tag == DW_TAG_namespace
7261 || c->die_tag == DW_TAG_module);
7264 /* Returns true iff C is a compile-unit DIE. */
7266 static inline bool
7267 is_cu_die (dw_die_ref c)
7269 return c && (c->die_tag == DW_TAG_compile_unit
7270 || c->die_tag == DW_TAG_skeleton_unit);
7273 /* Returns true iff C is a unit DIE of some sort. */
7275 static inline bool
7276 is_unit_die (dw_die_ref c)
7278 return c && (c->die_tag == DW_TAG_compile_unit
7279 || c->die_tag == DW_TAG_partial_unit
7280 || c->die_tag == DW_TAG_type_unit
7281 || c->die_tag == DW_TAG_skeleton_unit);
7284 /* Returns true iff C is a namespace DIE. */
7286 static inline bool
7287 is_namespace_die (dw_die_ref c)
7289 return c && c->die_tag == DW_TAG_namespace;
7292 /* Returns true iff C is a class or structure DIE. */
7294 static inline bool
7295 is_class_die (dw_die_ref c)
7297 return c && (c->die_tag == DW_TAG_class_type
7298 || c->die_tag == DW_TAG_structure_type);
7301 /* Return non-zero if this DIE is a template parameter. */
7303 static inline bool
7304 is_template_parameter (dw_die_ref die)
7306 switch (die->die_tag)
7308 case DW_TAG_template_type_param:
7309 case DW_TAG_template_value_param:
7310 case DW_TAG_GNU_template_template_param:
7311 case DW_TAG_GNU_template_parameter_pack:
7312 return true;
7313 default:
7314 return false;
7318 /* Return non-zero if this DIE represents a template instantiation. */
7320 static inline bool
7321 is_template_instantiation (dw_die_ref die)
7323 dw_die_ref c;
7325 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7326 return false;
7327 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7328 return false;
7331 static char *
7332 gen_internal_sym (const char *prefix)
7334 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7336 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7337 return xstrdup (buf);
7340 /* Assign symbols to all worthy DIEs under DIE. */
7342 static void
7343 assign_symbol_names (dw_die_ref die)
7345 dw_die_ref c;
7347 if (is_symbol_die (die) && !die->comdat_type_p)
7349 if (comdat_symbol_id)
7351 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7353 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7354 comdat_symbol_id, comdat_symbol_number++);
7355 die->die_id.die_symbol = xstrdup (p);
7357 else
7358 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7361 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7364 struct cu_hash_table_entry
7366 dw_die_ref cu;
7367 unsigned min_comdat_num, max_comdat_num;
7368 struct cu_hash_table_entry *next;
7371 /* Helpers to manipulate hash table of CUs. */
7373 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7375 typedef die_struct *compare_type;
7376 static inline hashval_t hash (const cu_hash_table_entry *);
7377 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7378 static inline void remove (cu_hash_table_entry *);
7381 inline hashval_t
7382 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7384 return htab_hash_string (entry->cu->die_id.die_symbol);
7387 inline bool
7388 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7389 const die_struct *entry2)
7391 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7394 inline void
7395 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7397 struct cu_hash_table_entry *next;
7399 while (entry)
7401 next = entry->next;
7402 free (entry);
7403 entry = next;
7407 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7409 /* Check whether we have already seen this CU and set up SYM_NUM
7410 accordingly. */
7411 static int
7412 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7414 struct cu_hash_table_entry dummy;
7415 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7417 dummy.max_comdat_num = 0;
7419 slot = htable->find_slot_with_hash (cu,
7420 htab_hash_string (cu->die_id.die_symbol),
7421 INSERT);
7422 entry = *slot;
7424 for (; entry; last = entry, entry = entry->next)
7426 if (same_die_p_wrap (cu, entry->cu))
7427 break;
7430 if (entry)
7432 *sym_num = entry->min_comdat_num;
7433 return 1;
7436 entry = XCNEW (struct cu_hash_table_entry);
7437 entry->cu = cu;
7438 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7439 entry->next = *slot;
7440 *slot = entry;
7442 return 0;
7445 /* Record SYM_NUM to record of CU in HTABLE. */
7446 static void
7447 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7448 unsigned int sym_num)
7450 struct cu_hash_table_entry **slot, *entry;
7452 slot = htable->find_slot_with_hash (cu,
7453 htab_hash_string (cu->die_id.die_symbol),
7454 NO_INSERT);
7455 entry = *slot;
7457 entry->max_comdat_num = sym_num;
7460 /* Traverse the DIE (which is always comp_unit_die), and set up
7461 additional compilation units for each of the include files we see
7462 bracketed by BINCL/EINCL. */
7464 static void
7465 break_out_includes (dw_die_ref die)
7467 dw_die_ref c;
7468 dw_die_ref unit = NULL;
7469 limbo_die_node *node, **pnode;
7471 c = die->die_child;
7472 if (c) do {
7473 dw_die_ref prev = c;
7474 c = c->die_sib;
7475 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7476 || (unit && is_comdat_die (c)))
7478 dw_die_ref next = c->die_sib;
7480 /* This DIE is for a secondary CU; remove it from the main one. */
7481 remove_child_with_prev (c, prev);
7483 if (c->die_tag == DW_TAG_GNU_BINCL)
7484 unit = push_new_compile_unit (unit, c);
7485 else if (c->die_tag == DW_TAG_GNU_EINCL)
7486 unit = pop_compile_unit (unit);
7487 else
7488 add_child_die (unit, c);
7489 c = next;
7490 if (c == die->die_child)
7491 break;
7493 } while (c != die->die_child);
7495 #if 0
7496 /* We can only use this in debugging, since the frontend doesn't check
7497 to make sure that we leave every include file we enter. */
7498 gcc_assert (!unit);
7499 #endif
7501 assign_symbol_names (die);
7502 cu_hash_type cu_hash_table (10);
7503 for (node = limbo_die_list, pnode = &limbo_die_list;
7504 node;
7505 node = node->next)
7507 int is_dupl;
7509 compute_section_prefix (node->die);
7510 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7511 &comdat_symbol_number);
7512 assign_symbol_names (node->die);
7513 if (is_dupl)
7514 *pnode = node->next;
7515 else
7517 pnode = &node->next;
7518 record_comdat_symbol_number (node->die, &cu_hash_table,
7519 comdat_symbol_number);
7524 /* Return non-zero if this DIE is a declaration. */
7526 static int
7527 is_declaration_die (dw_die_ref die)
7529 dw_attr_node *a;
7530 unsigned ix;
7532 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7533 if (a->dw_attr == DW_AT_declaration)
7534 return 1;
7536 return 0;
7539 /* Return non-zero if this DIE is nested inside a subprogram. */
7541 static int
7542 is_nested_in_subprogram (dw_die_ref die)
7544 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7546 if (decl == NULL)
7547 decl = die;
7548 return local_scope_p (decl);
7551 /* Return non-zero if this DIE contains a defining declaration of a
7552 subprogram. */
7554 static int
7555 contains_subprogram_definition (dw_die_ref die)
7557 dw_die_ref c;
7559 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7560 return 1;
7561 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7562 return 0;
7565 /* Return non-zero if this is a type DIE that should be moved to a
7566 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7567 unit type. */
7569 static int
7570 should_move_die_to_comdat (dw_die_ref die)
7572 switch (die->die_tag)
7574 case DW_TAG_class_type:
7575 case DW_TAG_structure_type:
7576 case DW_TAG_enumeration_type:
7577 case DW_TAG_union_type:
7578 /* Don't move declarations, inlined instances, types nested in a
7579 subprogram, or types that contain subprogram definitions. */
7580 if (is_declaration_die (die)
7581 || get_AT (die, DW_AT_abstract_origin)
7582 || is_nested_in_subprogram (die)
7583 || contains_subprogram_definition (die))
7584 return 0;
7585 return 1;
7586 case DW_TAG_array_type:
7587 case DW_TAG_interface_type:
7588 case DW_TAG_pointer_type:
7589 case DW_TAG_reference_type:
7590 case DW_TAG_rvalue_reference_type:
7591 case DW_TAG_string_type:
7592 case DW_TAG_subroutine_type:
7593 case DW_TAG_ptr_to_member_type:
7594 case DW_TAG_set_type:
7595 case DW_TAG_subrange_type:
7596 case DW_TAG_base_type:
7597 case DW_TAG_const_type:
7598 case DW_TAG_file_type:
7599 case DW_TAG_packed_type:
7600 case DW_TAG_volatile_type:
7601 case DW_TAG_typedef:
7602 default:
7603 return 0;
7607 /* Make a clone of DIE. */
7609 static dw_die_ref
7610 clone_die (dw_die_ref die)
7612 dw_die_ref clone;
7613 dw_attr_node *a;
7614 unsigned ix;
7616 clone = ggc_cleared_alloc<die_node> ();
7617 clone->die_tag = die->die_tag;
7619 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7620 add_dwarf_attr (clone, a);
7622 return clone;
7625 /* Make a clone of the tree rooted at DIE. */
7627 static dw_die_ref
7628 clone_tree (dw_die_ref die)
7630 dw_die_ref c;
7631 dw_die_ref clone = clone_die (die);
7633 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7635 return clone;
7638 /* Make a clone of DIE as a declaration. */
7640 static dw_die_ref
7641 clone_as_declaration (dw_die_ref die)
7643 dw_die_ref clone;
7644 dw_die_ref decl;
7645 dw_attr_node *a;
7646 unsigned ix;
7648 /* If the DIE is already a declaration, just clone it. */
7649 if (is_declaration_die (die))
7650 return clone_die (die);
7652 /* If the DIE is a specification, just clone its declaration DIE. */
7653 decl = get_AT_ref (die, DW_AT_specification);
7654 if (decl != NULL)
7656 clone = clone_die (decl);
7657 if (die->comdat_type_p)
7658 add_AT_die_ref (clone, DW_AT_signature, die);
7659 return clone;
7662 clone = ggc_cleared_alloc<die_node> ();
7663 clone->die_tag = die->die_tag;
7665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7667 /* We don't want to copy over all attributes.
7668 For example we don't want DW_AT_byte_size because otherwise we will no
7669 longer have a declaration and GDB will treat it as a definition. */
7671 switch (a->dw_attr)
7673 case DW_AT_abstract_origin:
7674 case DW_AT_artificial:
7675 case DW_AT_containing_type:
7676 case DW_AT_external:
7677 case DW_AT_name:
7678 case DW_AT_type:
7679 case DW_AT_virtuality:
7680 case DW_AT_linkage_name:
7681 case DW_AT_MIPS_linkage_name:
7682 add_dwarf_attr (clone, a);
7683 break;
7684 case DW_AT_byte_size:
7685 default:
7686 break;
7690 if (die->comdat_type_p)
7691 add_AT_die_ref (clone, DW_AT_signature, die);
7693 add_AT_flag (clone, DW_AT_declaration, 1);
7694 return clone;
7698 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7700 struct decl_table_entry
7702 dw_die_ref orig;
7703 dw_die_ref copy;
7706 /* Helpers to manipulate hash table of copied declarations. */
7708 /* Hashtable helpers. */
7710 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7712 typedef die_struct *compare_type;
7713 static inline hashval_t hash (const decl_table_entry *);
7714 static inline bool equal (const decl_table_entry *, const die_struct *);
7717 inline hashval_t
7718 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7720 return htab_hash_pointer (entry->orig);
7723 inline bool
7724 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7725 const die_struct *entry2)
7727 return entry1->orig == entry2;
7730 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7732 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7733 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7734 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7735 to check if the ancestor has already been copied into UNIT. */
7737 static dw_die_ref
7738 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7739 decl_hash_type *decl_table)
7741 dw_die_ref parent = die->die_parent;
7742 dw_die_ref new_parent = unit;
7743 dw_die_ref copy;
7744 decl_table_entry **slot = NULL;
7745 struct decl_table_entry *entry = NULL;
7747 if (decl_table)
7749 /* Check if the entry has already been copied to UNIT. */
7750 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7751 INSERT);
7752 if (*slot != HTAB_EMPTY_ENTRY)
7754 entry = *slot;
7755 return entry->copy;
7758 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7759 entry = XCNEW (struct decl_table_entry);
7760 entry->orig = die;
7761 entry->copy = NULL;
7762 *slot = entry;
7765 if (parent != NULL)
7767 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7768 if (spec != NULL)
7769 parent = spec;
7770 if (!is_unit_die (parent))
7771 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7774 copy = clone_as_declaration (die);
7775 add_child_die (new_parent, copy);
7777 if (decl_table)
7779 /* Record the pointer to the copy. */
7780 entry->copy = copy;
7783 return copy;
7785 /* Copy the declaration context to the new type unit DIE. This includes
7786 any surrounding namespace or type declarations. If the DIE has an
7787 AT_specification attribute, it also includes attributes and children
7788 attached to the specification, and returns a pointer to the original
7789 parent of the declaration DIE. Returns NULL otherwise. */
7791 static dw_die_ref
7792 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7794 dw_die_ref decl;
7795 dw_die_ref new_decl;
7796 dw_die_ref orig_parent = NULL;
7798 decl = get_AT_ref (die, DW_AT_specification);
7799 if (decl == NULL)
7800 decl = die;
7801 else
7803 unsigned ix;
7804 dw_die_ref c;
7805 dw_attr_node *a;
7807 /* The original DIE will be changed to a declaration, and must
7808 be moved to be a child of the original declaration DIE. */
7809 orig_parent = decl->die_parent;
7811 /* Copy the type node pointer from the new DIE to the original
7812 declaration DIE so we can forward references later. */
7813 decl->comdat_type_p = true;
7814 decl->die_id.die_type_node = die->die_id.die_type_node;
7816 remove_AT (die, DW_AT_specification);
7818 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7820 if (a->dw_attr != DW_AT_name
7821 && a->dw_attr != DW_AT_declaration
7822 && a->dw_attr != DW_AT_external)
7823 add_dwarf_attr (die, a);
7826 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7829 if (decl->die_parent != NULL
7830 && !is_unit_die (decl->die_parent))
7832 new_decl = copy_ancestor_tree (unit, decl, NULL);
7833 if (new_decl != NULL)
7835 remove_AT (new_decl, DW_AT_signature);
7836 add_AT_specification (die, new_decl);
7840 return orig_parent;
7843 /* Generate the skeleton ancestor tree for the given NODE, then clone
7844 the DIE and add the clone into the tree. */
7846 static void
7847 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7849 if (node->new_die != NULL)
7850 return;
7852 node->new_die = clone_as_declaration (node->old_die);
7854 if (node->parent != NULL)
7856 generate_skeleton_ancestor_tree (node->parent);
7857 add_child_die (node->parent->new_die, node->new_die);
7861 /* Generate a skeleton tree of DIEs containing any declarations that are
7862 found in the original tree. We traverse the tree looking for declaration
7863 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7865 static void
7866 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7868 skeleton_chain_node node;
7869 dw_die_ref c;
7870 dw_die_ref first;
7871 dw_die_ref prev = NULL;
7872 dw_die_ref next = NULL;
7874 node.parent = parent;
7876 first = c = parent->old_die->die_child;
7877 if (c)
7878 next = c->die_sib;
7879 if (c) do {
7880 if (prev == NULL || prev->die_sib == c)
7881 prev = c;
7882 c = next;
7883 next = (c == first ? NULL : c->die_sib);
7884 node.old_die = c;
7885 node.new_die = NULL;
7886 if (is_declaration_die (c))
7888 if (is_template_instantiation (c))
7890 /* Instantiated templates do not need to be cloned into the
7891 type unit. Just move the DIE and its children back to
7892 the skeleton tree (in the main CU). */
7893 remove_child_with_prev (c, prev);
7894 add_child_die (parent->new_die, c);
7895 c = prev;
7897 else
7899 /* Clone the existing DIE, move the original to the skeleton
7900 tree (which is in the main CU), and put the clone, with
7901 all the original's children, where the original came from
7902 (which is about to be moved to the type unit). */
7903 dw_die_ref clone = clone_die (c);
7904 move_all_children (c, clone);
7906 /* If the original has a DW_AT_object_pointer attribute,
7907 it would now point to a child DIE just moved to the
7908 cloned tree, so we need to remove that attribute from
7909 the original. */
7910 remove_AT (c, DW_AT_object_pointer);
7912 replace_child (c, clone, prev);
7913 generate_skeleton_ancestor_tree (parent);
7914 add_child_die (parent->new_die, c);
7915 node.new_die = c;
7916 c = clone;
7919 generate_skeleton_bottom_up (&node);
7920 } while (next != NULL);
7923 /* Wrapper function for generate_skeleton_bottom_up. */
7925 static dw_die_ref
7926 generate_skeleton (dw_die_ref die)
7928 skeleton_chain_node node;
7930 node.old_die = die;
7931 node.new_die = NULL;
7932 node.parent = NULL;
7934 /* If this type definition is nested inside another type,
7935 and is not an instantiation of a template, always leave
7936 at least a declaration in its place. */
7937 if (die->die_parent != NULL
7938 && is_type_die (die->die_parent)
7939 && !is_template_instantiation (die))
7940 node.new_die = clone_as_declaration (die);
7942 generate_skeleton_bottom_up (&node);
7943 return node.new_die;
7946 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7947 declaration. The original DIE is moved to a new compile unit so that
7948 existing references to it follow it to the new location. If any of the
7949 original DIE's descendants is a declaration, we need to replace the
7950 original DIE with a skeleton tree and move the declarations back into the
7951 skeleton tree. */
7953 static dw_die_ref
7954 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7955 dw_die_ref prev)
7957 dw_die_ref skeleton, orig_parent;
7959 /* Copy the declaration context to the type unit DIE. If the returned
7960 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7961 that DIE. */
7962 orig_parent = copy_declaration_context (unit, child);
7964 skeleton = generate_skeleton (child);
7965 if (skeleton == NULL)
7966 remove_child_with_prev (child, prev);
7967 else
7969 skeleton->comdat_type_p = true;
7970 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7972 /* If the original DIE was a specification, we need to put
7973 the skeleton under the parent DIE of the declaration.
7974 This leaves the original declaration in the tree, but
7975 it will be pruned later since there are no longer any
7976 references to it. */
7977 if (orig_parent != NULL)
7979 remove_child_with_prev (child, prev);
7980 add_child_die (orig_parent, skeleton);
7982 else
7983 replace_child (child, skeleton, prev);
7986 return skeleton;
7989 static void
7990 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7991 comdat_type_node *type_node,
7992 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7994 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7995 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7996 DWARF procedure references in the DW_AT_location attribute. */
7998 static dw_die_ref
7999 copy_dwarf_procedure (dw_die_ref die,
8000 comdat_type_node *type_node,
8001 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8003 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8005 /* DWARF procedures are not supposed to have children... */
8006 gcc_assert (die->die_child == NULL);
8008 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8009 gcc_assert (vec_safe_length (die->die_attr) == 1
8010 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8012 /* Do not copy more than once DWARF procedures. */
8013 bool existed;
8014 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8015 if (existed)
8016 return die_copy;
8018 die_copy = clone_die (die);
8019 add_child_die (type_node->root_die, die_copy);
8020 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8021 return die_copy;
8024 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8025 procedures in DIE's attributes. */
8027 static void
8028 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8029 comdat_type_node *type_node,
8030 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8032 dw_attr_node *a;
8033 unsigned i;
8035 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8037 dw_loc_descr_ref loc;
8039 if (a->dw_attr_val.val_class != dw_val_class_loc)
8040 continue;
8042 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8044 switch (loc->dw_loc_opc)
8046 case DW_OP_call2:
8047 case DW_OP_call4:
8048 case DW_OP_call_ref:
8049 gcc_assert (loc->dw_loc_oprnd1.val_class
8050 == dw_val_class_die_ref);
8051 loc->dw_loc_oprnd1.v.val_die_ref.die
8052 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8053 type_node,
8054 copied_dwarf_procs);
8056 default:
8057 break;
8063 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8064 rewrite references to point to the copies.
8066 References are looked for in DIE's attributes and recursively in all its
8067 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8068 mapping from old DWARF procedures to their copy. It is used not to copy
8069 twice the same DWARF procedure under TYPE_NODE. */
8071 static void
8072 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8073 comdat_type_node *type_node,
8074 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8076 dw_die_ref c;
8078 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8079 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8080 type_node,
8081 copied_dwarf_procs));
8084 /* Traverse the DIE and set up additional .debug_types or .debug_info
8085 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8086 section. */
8088 static void
8089 break_out_comdat_types (dw_die_ref die)
8091 dw_die_ref c;
8092 dw_die_ref first;
8093 dw_die_ref prev = NULL;
8094 dw_die_ref next = NULL;
8095 dw_die_ref unit = NULL;
8097 first = c = die->die_child;
8098 if (c)
8099 next = c->die_sib;
8100 if (c) do {
8101 if (prev == NULL || prev->die_sib == c)
8102 prev = c;
8103 c = next;
8104 next = (c == first ? NULL : c->die_sib);
8105 if (should_move_die_to_comdat (c))
8107 dw_die_ref replacement;
8108 comdat_type_node *type_node;
8110 /* Break out nested types into their own type units. */
8111 break_out_comdat_types (c);
8113 /* Create a new type unit DIE as the root for the new tree, and
8114 add it to the list of comdat types. */
8115 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8116 add_AT_unsigned (unit, DW_AT_language,
8117 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8118 type_node = ggc_cleared_alloc<comdat_type_node> ();
8119 type_node->root_die = unit;
8120 type_node->next = comdat_type_list;
8121 comdat_type_list = type_node;
8123 /* Generate the type signature. */
8124 generate_type_signature (c, type_node);
8126 /* Copy the declaration context, attributes, and children of the
8127 declaration into the new type unit DIE, then remove this DIE
8128 from the main CU (or replace it with a skeleton if necessary). */
8129 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8130 type_node->skeleton_die = replacement;
8132 /* Add the DIE to the new compunit. */
8133 add_child_die (unit, c);
8135 /* Types can reference DWARF procedures for type size or data location
8136 expressions. Calls in DWARF expressions cannot target procedures
8137 that are not in the same section. So we must copy DWARF procedures
8138 along with this type and then rewrite references to them. */
8139 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8140 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8142 if (replacement != NULL)
8143 c = replacement;
8145 else if (c->die_tag == DW_TAG_namespace
8146 || c->die_tag == DW_TAG_class_type
8147 || c->die_tag == DW_TAG_structure_type
8148 || c->die_tag == DW_TAG_union_type)
8150 /* Look for nested types that can be broken out. */
8151 break_out_comdat_types (c);
8153 } while (next != NULL);
8156 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8157 Enter all the cloned children into the hash table decl_table. */
8159 static dw_die_ref
8160 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8162 dw_die_ref c;
8163 dw_die_ref clone;
8164 struct decl_table_entry *entry;
8165 decl_table_entry **slot;
8167 if (die->die_tag == DW_TAG_subprogram)
8168 clone = clone_as_declaration (die);
8169 else
8170 clone = clone_die (die);
8172 slot = decl_table->find_slot_with_hash (die,
8173 htab_hash_pointer (die), INSERT);
8175 /* Assert that DIE isn't in the hash table yet. If it would be there
8176 before, the ancestors would be necessarily there as well, therefore
8177 clone_tree_partial wouldn't be called. */
8178 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8180 entry = XCNEW (struct decl_table_entry);
8181 entry->orig = die;
8182 entry->copy = clone;
8183 *slot = entry;
8185 if (die->die_tag != DW_TAG_subprogram)
8186 FOR_EACH_CHILD (die, c,
8187 add_child_die (clone, clone_tree_partial (c, decl_table)));
8189 return clone;
8192 /* Walk the DIE and its children, looking for references to incomplete
8193 or trivial types that are unmarked (i.e., that are not in the current
8194 type_unit). */
8196 static void
8197 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8199 dw_die_ref c;
8200 dw_attr_node *a;
8201 unsigned ix;
8203 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8205 if (AT_class (a) == dw_val_class_die_ref)
8207 dw_die_ref targ = AT_ref (a);
8208 decl_table_entry **slot;
8209 struct decl_table_entry *entry;
8211 if (targ->die_mark != 0 || targ->comdat_type_p)
8212 continue;
8214 slot = decl_table->find_slot_with_hash (targ,
8215 htab_hash_pointer (targ),
8216 INSERT);
8218 if (*slot != HTAB_EMPTY_ENTRY)
8220 /* TARG has already been copied, so we just need to
8221 modify the reference to point to the copy. */
8222 entry = *slot;
8223 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8225 else
8227 dw_die_ref parent = unit;
8228 dw_die_ref copy = clone_die (targ);
8230 /* Record in DECL_TABLE that TARG has been copied.
8231 Need to do this now, before the recursive call,
8232 because DECL_TABLE may be expanded and SLOT
8233 would no longer be a valid pointer. */
8234 entry = XCNEW (struct decl_table_entry);
8235 entry->orig = targ;
8236 entry->copy = copy;
8237 *slot = entry;
8239 /* If TARG is not a declaration DIE, we need to copy its
8240 children. */
8241 if (!is_declaration_die (targ))
8243 FOR_EACH_CHILD (
8244 targ, c,
8245 add_child_die (copy,
8246 clone_tree_partial (c, decl_table)));
8249 /* Make sure the cloned tree is marked as part of the
8250 type unit. */
8251 mark_dies (copy);
8253 /* If TARG has surrounding context, copy its ancestor tree
8254 into the new type unit. */
8255 if (targ->die_parent != NULL
8256 && !is_unit_die (targ->die_parent))
8257 parent = copy_ancestor_tree (unit, targ->die_parent,
8258 decl_table);
8260 add_child_die (parent, copy);
8261 a->dw_attr_val.v.val_die_ref.die = copy;
8263 /* Make sure the newly-copied DIE is walked. If it was
8264 installed in a previously-added context, it won't
8265 get visited otherwise. */
8266 if (parent != unit)
8268 /* Find the highest point of the newly-added tree,
8269 mark each node along the way, and walk from there. */
8270 parent->die_mark = 1;
8271 while (parent->die_parent
8272 && parent->die_parent->die_mark == 0)
8274 parent = parent->die_parent;
8275 parent->die_mark = 1;
8277 copy_decls_walk (unit, parent, decl_table);
8283 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8286 /* Copy declarations for "unworthy" types into the new comdat section.
8287 Incomplete types, modified types, and certain other types aren't broken
8288 out into comdat sections of their own, so they don't have a signature,
8289 and we need to copy the declaration into the same section so that we
8290 don't have an external reference. */
8292 static void
8293 copy_decls_for_unworthy_types (dw_die_ref unit)
8295 mark_dies (unit);
8296 decl_hash_type decl_table (10);
8297 copy_decls_walk (unit, unit, &decl_table);
8298 unmark_dies (unit);
8301 /* Traverse the DIE and add a sibling attribute if it may have the
8302 effect of speeding up access to siblings. To save some space,
8303 avoid generating sibling attributes for DIE's without children. */
8305 static void
8306 add_sibling_attributes (dw_die_ref die)
8308 dw_die_ref c;
8310 if (! die->die_child)
8311 return;
8313 if (die->die_parent && die != die->die_parent->die_child)
8314 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8316 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8319 /* Output all location lists for the DIE and its children. */
8321 static void
8322 output_location_lists (dw_die_ref die)
8324 dw_die_ref c;
8325 dw_attr_node *a;
8326 unsigned ix;
8328 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8329 if (AT_class (a) == dw_val_class_loc_list)
8330 output_loc_list (AT_loc_list (a));
8332 FOR_EACH_CHILD (die, c, output_location_lists (c));
8335 /* During assign_location_list_indexes and output_loclists_offset the
8336 current index, after it the number of assigned indexes (i.e. how
8337 large the .debug_loclists* offset table should be). */
8338 static unsigned int loc_list_idx;
8340 /* Output all location list offsets for the DIE and its children. */
8342 static void
8343 output_loclists_offsets (dw_die_ref die)
8345 dw_die_ref c;
8346 dw_attr_node *a;
8347 unsigned ix;
8349 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8350 if (AT_class (a) == dw_val_class_loc_list)
8352 dw_loc_list_ref l = AT_loc_list (a);
8353 if (l->offset_emitted)
8354 continue;
8355 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8356 loc_section_label, NULL);
8357 gcc_assert (l->hash == loc_list_idx);
8358 loc_list_idx++;
8359 l->offset_emitted = true;
8362 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8365 /* Recursively set indexes of location lists. */
8367 static void
8368 assign_location_list_indexes (dw_die_ref die)
8370 dw_die_ref c;
8371 dw_attr_node *a;
8372 unsigned ix;
8374 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8375 if (AT_class (a) == dw_val_class_loc_list)
8377 dw_loc_list_ref list = AT_loc_list (a);
8378 if (!list->num_assigned)
8380 list->num_assigned = true;
8381 list->hash = loc_list_idx++;
8385 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8388 /* We want to limit the number of external references, because they are
8389 larger than local references: a relocation takes multiple words, and
8390 even a sig8 reference is always eight bytes, whereas a local reference
8391 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8392 So if we encounter multiple external references to the same type DIE, we
8393 make a local typedef stub for it and redirect all references there.
8395 This is the element of the hash table for keeping track of these
8396 references. */
8398 struct external_ref
8400 dw_die_ref type;
8401 dw_die_ref stub;
8402 unsigned n_refs;
8405 /* Hashtable helpers. */
8407 struct external_ref_hasher : free_ptr_hash <external_ref>
8409 static inline hashval_t hash (const external_ref *);
8410 static inline bool equal (const external_ref *, const external_ref *);
8413 inline hashval_t
8414 external_ref_hasher::hash (const external_ref *r)
8416 dw_die_ref die = r->type;
8417 hashval_t h = 0;
8419 /* We can't use the address of the DIE for hashing, because
8420 that will make the order of the stub DIEs non-deterministic. */
8421 if (! die->comdat_type_p)
8422 /* We have a symbol; use it to compute a hash. */
8423 h = htab_hash_string (die->die_id.die_symbol);
8424 else
8426 /* We have a type signature; use a subset of the bits as the hash.
8427 The 8-byte signature is at least as large as hashval_t. */
8428 comdat_type_node *type_node = die->die_id.die_type_node;
8429 memcpy (&h, type_node->signature, sizeof (h));
8431 return h;
8434 inline bool
8435 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8437 return r1->type == r2->type;
8440 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8442 /* Return a pointer to the external_ref for references to DIE. */
8444 static struct external_ref *
8445 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8447 struct external_ref ref, *ref_p;
8448 external_ref **slot;
8450 ref.type = die;
8451 slot = map->find_slot (&ref, INSERT);
8452 if (*slot != HTAB_EMPTY_ENTRY)
8453 return *slot;
8455 ref_p = XCNEW (struct external_ref);
8456 ref_p->type = die;
8457 *slot = ref_p;
8458 return ref_p;
8461 /* Subroutine of optimize_external_refs, below.
8463 If we see a type skeleton, record it as our stub. If we see external
8464 references, remember how many we've seen. */
8466 static void
8467 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8469 dw_die_ref c;
8470 dw_attr_node *a;
8471 unsigned ix;
8472 struct external_ref *ref_p;
8474 if (is_type_die (die)
8475 && (c = get_AT_ref (die, DW_AT_signature)))
8477 /* This is a local skeleton; use it for local references. */
8478 ref_p = lookup_external_ref (map, c);
8479 ref_p->stub = die;
8482 /* Scan the DIE references, and remember any that refer to DIEs from
8483 other CUs (i.e. those which are not marked). */
8484 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8485 if (AT_class (a) == dw_val_class_die_ref
8486 && (c = AT_ref (a))->die_mark == 0
8487 && is_type_die (c))
8489 ref_p = lookup_external_ref (map, c);
8490 ref_p->n_refs++;
8493 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8496 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8497 points to an external_ref, DATA is the CU we're processing. If we don't
8498 already have a local stub, and we have multiple refs, build a stub. */
8501 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8503 struct external_ref *ref_p = *slot;
8505 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8507 /* We have multiple references to this type, so build a small stub.
8508 Both of these forms are a bit dodgy from the perspective of the
8509 DWARF standard, since technically they should have names. */
8510 dw_die_ref cu = data;
8511 dw_die_ref type = ref_p->type;
8512 dw_die_ref stub = NULL;
8514 if (type->comdat_type_p)
8516 /* If we refer to this type via sig8, use AT_signature. */
8517 stub = new_die (type->die_tag, cu, NULL_TREE);
8518 add_AT_die_ref (stub, DW_AT_signature, type);
8520 else
8522 /* Otherwise, use a typedef with no name. */
8523 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8524 add_AT_die_ref (stub, DW_AT_type, type);
8527 stub->die_mark++;
8528 ref_p->stub = stub;
8530 return 1;
8533 /* DIE is a unit; look through all the DIE references to see if there are
8534 any external references to types, and if so, create local stubs for
8535 them which will be applied in build_abbrev_table. This is useful because
8536 references to local DIEs are smaller. */
8538 static external_ref_hash_type *
8539 optimize_external_refs (dw_die_ref die)
8541 external_ref_hash_type *map = new external_ref_hash_type (10);
8542 optimize_external_refs_1 (die, map);
8543 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8544 return map;
8547 /* The following 3 variables are temporaries that are computed only during the
8548 build_abbrev_table call and used and released during the following
8549 optimize_abbrev_table call. */
8551 /* First abbrev_id that can be optimized based on usage. */
8552 static unsigned int abbrev_opt_start;
8554 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8555 abbrev_id smaller than this, because they must be already sized
8556 during build_abbrev_table). */
8557 static unsigned int abbrev_opt_base_type_end;
8559 /* Vector of usage counts during build_abbrev_table. Indexed by
8560 abbrev_id - abbrev_opt_start. */
8561 static vec<unsigned int> abbrev_usage_count;
8563 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8564 static vec<dw_die_ref> sorted_abbrev_dies;
8566 /* The format of each DIE (and its attribute value pairs) is encoded in an
8567 abbreviation table. This routine builds the abbreviation table and assigns
8568 a unique abbreviation id for each abbreviation entry. The children of each
8569 die are visited recursively. */
8571 static void
8572 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8574 unsigned int abbrev_id = 0;
8575 dw_die_ref c;
8576 dw_attr_node *a;
8577 unsigned ix;
8578 dw_die_ref abbrev;
8580 /* Scan the DIE references, and replace any that refer to
8581 DIEs from other CUs (i.e. those which are not marked) with
8582 the local stubs we built in optimize_external_refs. */
8583 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8584 if (AT_class (a) == dw_val_class_die_ref
8585 && (c = AT_ref (a))->die_mark == 0)
8587 struct external_ref *ref_p;
8588 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8590 ref_p = lookup_external_ref (extern_map, c);
8591 if (ref_p->stub && ref_p->stub != die)
8592 change_AT_die_ref (a, ref_p->stub);
8593 else
8594 /* We aren't changing this reference, so mark it external. */
8595 set_AT_ref_external (a, 1);
8598 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8600 dw_attr_node *die_a, *abbrev_a;
8601 unsigned ix;
8602 bool ok = true;
8604 if (abbrev_id == 0)
8605 continue;
8606 if (abbrev->die_tag != die->die_tag)
8607 continue;
8608 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8609 continue;
8611 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8612 continue;
8614 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8616 abbrev_a = &(*abbrev->die_attr)[ix];
8617 if ((abbrev_a->dw_attr != die_a->dw_attr)
8618 || (value_format (abbrev_a) != value_format (die_a)))
8620 ok = false;
8621 break;
8624 if (ok)
8625 break;
8628 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8630 vec_safe_push (abbrev_die_table, die);
8631 if (abbrev_opt_start)
8632 abbrev_usage_count.safe_push (0);
8634 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8636 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8637 sorted_abbrev_dies.safe_push (die);
8640 die->die_abbrev = abbrev_id;
8641 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8644 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8645 by die_abbrev's usage count, from the most commonly used
8646 abbreviation to the least. */
8648 static int
8649 die_abbrev_cmp (const void *p1, const void *p2)
8651 dw_die_ref die1 = *(const dw_die_ref *) p1;
8652 dw_die_ref die2 = *(const dw_die_ref *) p2;
8654 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8655 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8657 if (die1->die_abbrev >= abbrev_opt_base_type_end
8658 && die2->die_abbrev >= abbrev_opt_base_type_end)
8660 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8661 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8662 return -1;
8663 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8664 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8665 return 1;
8668 /* Stabilize the sort. */
8669 if (die1->die_abbrev < die2->die_abbrev)
8670 return -1;
8671 if (die1->die_abbrev > die2->die_abbrev)
8672 return 1;
8674 return 0;
8677 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8678 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8679 into dw_val_class_const_implicit or
8680 dw_val_class_unsigned_const_implicit. */
8682 static void
8683 optimize_implicit_const (unsigned int first_id, unsigned int end,
8684 vec<bool> &implicit_consts)
8686 /* It never makes sense if there is just one DIE using the abbreviation. */
8687 if (end < first_id + 2)
8688 return;
8690 dw_attr_node *a;
8691 unsigned ix, i;
8692 dw_die_ref die = sorted_abbrev_dies[first_id];
8693 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8694 if (implicit_consts[ix])
8696 enum dw_val_class new_class = dw_val_class_none;
8697 switch (AT_class (a))
8699 case dw_val_class_unsigned_const:
8700 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8701 continue;
8703 /* The .debug_abbrev section will grow by
8704 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8705 in all the DIEs using that abbreviation. */
8706 if (constant_size (AT_unsigned (a)) * (end - first_id)
8707 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8708 continue;
8710 new_class = dw_val_class_unsigned_const_implicit;
8711 break;
8713 case dw_val_class_const:
8714 new_class = dw_val_class_const_implicit;
8715 break;
8717 case dw_val_class_file:
8718 new_class = dw_val_class_file_implicit;
8719 break;
8721 default:
8722 continue;
8724 for (i = first_id; i < end; i++)
8725 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8726 = new_class;
8730 /* Attempt to optimize abbreviation table from abbrev_opt_start
8731 abbreviation above. */
8733 static void
8734 optimize_abbrev_table (void)
8736 if (abbrev_opt_start
8737 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8738 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8740 auto_vec<bool, 32> implicit_consts;
8741 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8743 unsigned int abbrev_id = abbrev_opt_start - 1;
8744 unsigned int first_id = ~0U;
8745 unsigned int last_abbrev_id = 0;
8746 unsigned int i;
8747 dw_die_ref die;
8748 if (abbrev_opt_base_type_end > abbrev_opt_start)
8749 abbrev_id = abbrev_opt_base_type_end - 1;
8750 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8751 most commonly used abbreviations come first. */
8752 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8754 dw_attr_node *a;
8755 unsigned ix;
8757 /* If calc_base_type_die_sizes has been called, the CU and
8758 base types after it can't be optimized, because we've already
8759 calculated their DIE offsets. We've sorted them first. */
8760 if (die->die_abbrev < abbrev_opt_base_type_end)
8761 continue;
8762 if (die->die_abbrev != last_abbrev_id)
8764 last_abbrev_id = die->die_abbrev;
8765 if (dwarf_version >= 5 && first_id != ~0U)
8766 optimize_implicit_const (first_id, i, implicit_consts);
8767 abbrev_id++;
8768 (*abbrev_die_table)[abbrev_id] = die;
8769 if (dwarf_version >= 5)
8771 first_id = i;
8772 implicit_consts.truncate (0);
8774 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8775 switch (AT_class (a))
8777 case dw_val_class_const:
8778 case dw_val_class_unsigned_const:
8779 case dw_val_class_file:
8780 implicit_consts.safe_push (true);
8781 break;
8782 default:
8783 implicit_consts.safe_push (false);
8784 break;
8788 else if (dwarf_version >= 5)
8790 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8791 if (!implicit_consts[ix])
8792 continue;
8793 else
8795 dw_attr_node *other_a
8796 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8797 if (!dw_val_equal_p (&a->dw_attr_val,
8798 &other_a->dw_attr_val))
8799 implicit_consts[ix] = false;
8802 die->die_abbrev = abbrev_id;
8804 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8805 if (dwarf_version >= 5 && first_id != ~0U)
8806 optimize_implicit_const (first_id, i, implicit_consts);
8809 abbrev_opt_start = 0;
8810 abbrev_opt_base_type_end = 0;
8811 abbrev_usage_count.release ();
8812 sorted_abbrev_dies.release ();
8815 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8817 static int
8818 constant_size (unsigned HOST_WIDE_INT value)
8820 int log;
8822 if (value == 0)
8823 log = 0;
8824 else
8825 log = floor_log2 (value);
8827 log = log / 8;
8828 log = 1 << (floor_log2 (log) + 1);
8830 return log;
8833 /* Return the size of a DIE as it is represented in the
8834 .debug_info section. */
8836 static unsigned long
8837 size_of_die (dw_die_ref die)
8839 unsigned long size = 0;
8840 dw_attr_node *a;
8841 unsigned ix;
8842 enum dwarf_form form;
8844 size += size_of_uleb128 (die->die_abbrev);
8845 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8847 switch (AT_class (a))
8849 case dw_val_class_addr:
8850 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8852 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8853 size += size_of_uleb128 (AT_index (a));
8855 else
8856 size += DWARF2_ADDR_SIZE;
8857 break;
8858 case dw_val_class_offset:
8859 size += DWARF_OFFSET_SIZE;
8860 break;
8861 case dw_val_class_loc:
8863 unsigned long lsize = size_of_locs (AT_loc (a));
8865 /* Block length. */
8866 if (dwarf_version >= 4)
8867 size += size_of_uleb128 (lsize);
8868 else
8869 size += constant_size (lsize);
8870 size += lsize;
8872 break;
8873 case dw_val_class_loc_list:
8874 if (dwarf_split_debug_info && dwarf_version >= 5)
8876 gcc_assert (AT_loc_list (a)->num_assigned);
8877 size += size_of_uleb128 (AT_loc_list (a)->hash);
8879 else
8880 size += DWARF_OFFSET_SIZE;
8881 break;
8882 case dw_val_class_range_list:
8883 if (value_format (a) == DW_FORM_rnglistx)
8885 gcc_assert (rnglist_idx);
8886 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8887 size += size_of_uleb128 (r->idx);
8889 else
8890 size += DWARF_OFFSET_SIZE;
8891 break;
8892 case dw_val_class_const:
8893 size += size_of_sleb128 (AT_int (a));
8894 break;
8895 case dw_val_class_unsigned_const:
8897 int csize = constant_size (AT_unsigned (a));
8898 if (dwarf_version == 3
8899 && a->dw_attr == DW_AT_data_member_location
8900 && csize >= 4)
8901 size += size_of_uleb128 (AT_unsigned (a));
8902 else
8903 size += csize;
8905 break;
8906 case dw_val_class_const_implicit:
8907 case dw_val_class_unsigned_const_implicit:
8908 case dw_val_class_file_implicit:
8909 /* These occupy no size in the DIE, just an extra sleb128 in
8910 .debug_abbrev. */
8911 break;
8912 case dw_val_class_const_double:
8913 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8914 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8915 size++; /* block */
8916 break;
8917 case dw_val_class_wide_int:
8918 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8919 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8920 if (get_full_len (*a->dw_attr_val.v.val_wide)
8921 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
8922 size++; /* block */
8923 break;
8924 case dw_val_class_vec:
8925 size += constant_size (a->dw_attr_val.v.val_vec.length
8926 * a->dw_attr_val.v.val_vec.elt_size)
8927 + a->dw_attr_val.v.val_vec.length
8928 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8929 break;
8930 case dw_val_class_flag:
8931 if (dwarf_version >= 4)
8932 /* Currently all add_AT_flag calls pass in 1 as last argument,
8933 so DW_FORM_flag_present can be used. If that ever changes,
8934 we'll need to use DW_FORM_flag and have some optimization
8935 in build_abbrev_table that will change those to
8936 DW_FORM_flag_present if it is set to 1 in all DIEs using
8937 the same abbrev entry. */
8938 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8939 else
8940 size += 1;
8941 break;
8942 case dw_val_class_die_ref:
8943 if (AT_ref_external (a))
8945 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8946 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8947 is sized by target address length, whereas in DWARF3
8948 it's always sized as an offset. */
8949 if (use_debug_types)
8950 size += DWARF_TYPE_SIGNATURE_SIZE;
8951 else if (dwarf_version == 2)
8952 size += DWARF2_ADDR_SIZE;
8953 else
8954 size += DWARF_OFFSET_SIZE;
8956 else
8957 size += DWARF_OFFSET_SIZE;
8958 break;
8959 case dw_val_class_fde_ref:
8960 size += DWARF_OFFSET_SIZE;
8961 break;
8962 case dw_val_class_lbl_id:
8963 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8965 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8966 size += size_of_uleb128 (AT_index (a));
8968 else
8969 size += DWARF2_ADDR_SIZE;
8970 break;
8971 case dw_val_class_lineptr:
8972 case dw_val_class_macptr:
8973 case dw_val_class_loclistsptr:
8974 size += DWARF_OFFSET_SIZE;
8975 break;
8976 case dw_val_class_str:
8977 form = AT_string_form (a);
8978 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
8979 size += DWARF_OFFSET_SIZE;
8980 else if (form == DW_FORM_GNU_str_index)
8981 size += size_of_uleb128 (AT_index (a));
8982 else
8983 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8984 break;
8985 case dw_val_class_file:
8986 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8987 break;
8988 case dw_val_class_data8:
8989 size += 8;
8990 break;
8991 case dw_val_class_vms_delta:
8992 size += DWARF_OFFSET_SIZE;
8993 break;
8994 case dw_val_class_high_pc:
8995 size += DWARF2_ADDR_SIZE;
8996 break;
8997 case dw_val_class_discr_value:
8998 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8999 break;
9000 case dw_val_class_discr_list:
9002 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9004 /* This is a block, so we have the block length and then its
9005 data. */
9006 size += constant_size (block_size) + block_size;
9008 break;
9009 default:
9010 gcc_unreachable ();
9014 return size;
9017 /* Size the debugging information associated with a given DIE. Visits the
9018 DIE's children recursively. Updates the global variable next_die_offset, on
9019 each time through. Uses the current value of next_die_offset to update the
9020 die_offset field in each DIE. */
9022 static void
9023 calc_die_sizes (dw_die_ref die)
9025 dw_die_ref c;
9027 gcc_assert (die->die_offset == 0
9028 || (unsigned long int) die->die_offset == next_die_offset);
9029 die->die_offset = next_die_offset;
9030 next_die_offset += size_of_die (die);
9032 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9034 if (die->die_child != NULL)
9035 /* Count the null byte used to terminate sibling lists. */
9036 next_die_offset += 1;
9039 /* Size just the base type children at the start of the CU.
9040 This is needed because build_abbrev needs to size locs
9041 and sizing of type based stack ops needs to know die_offset
9042 values for the base types. */
9044 static void
9045 calc_base_type_die_sizes (void)
9047 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9048 unsigned int i;
9049 dw_die_ref base_type;
9050 #if ENABLE_ASSERT_CHECKING
9051 dw_die_ref prev = comp_unit_die ()->die_child;
9052 #endif
9054 die_offset += size_of_die (comp_unit_die ());
9055 for (i = 0; base_types.iterate (i, &base_type); i++)
9057 #if ENABLE_ASSERT_CHECKING
9058 gcc_assert (base_type->die_offset == 0
9059 && prev->die_sib == base_type
9060 && base_type->die_child == NULL
9061 && base_type->die_abbrev);
9062 prev = base_type;
9063 #endif
9064 if (abbrev_opt_start
9065 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9066 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9067 base_type->die_offset = die_offset;
9068 die_offset += size_of_die (base_type);
9072 /* Set the marks for a die and its children. We do this so
9073 that we know whether or not a reference needs to use FORM_ref_addr; only
9074 DIEs in the same CU will be marked. We used to clear out the offset
9075 and use that as the flag, but ran into ordering problems. */
9077 static void
9078 mark_dies (dw_die_ref die)
9080 dw_die_ref c;
9082 gcc_assert (!die->die_mark);
9084 die->die_mark = 1;
9085 FOR_EACH_CHILD (die, c, mark_dies (c));
9088 /* Clear the marks for a die and its children. */
9090 static void
9091 unmark_dies (dw_die_ref die)
9093 dw_die_ref c;
9095 if (! use_debug_types)
9096 gcc_assert (die->die_mark);
9098 die->die_mark = 0;
9099 FOR_EACH_CHILD (die, c, unmark_dies (c));
9102 /* Clear the marks for a die, its children and referred dies. */
9104 static void
9105 unmark_all_dies (dw_die_ref die)
9107 dw_die_ref c;
9108 dw_attr_node *a;
9109 unsigned ix;
9111 if (!die->die_mark)
9112 return;
9113 die->die_mark = 0;
9115 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9117 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9118 if (AT_class (a) == dw_val_class_die_ref)
9119 unmark_all_dies (AT_ref (a));
9122 /* Calculate if the entry should appear in the final output file. It may be
9123 from a pruned a type. */
9125 static bool
9126 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9128 /* By limiting gnu pubnames to definitions only, gold can generate a
9129 gdb index without entries for declarations, which don't include
9130 enough information to be useful. */
9131 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9132 return false;
9134 if (table == pubname_table)
9136 /* Enumerator names are part of the pubname table, but the
9137 parent DW_TAG_enumeration_type die may have been pruned.
9138 Don't output them if that is the case. */
9139 if (p->die->die_tag == DW_TAG_enumerator &&
9140 (p->die->die_parent == NULL
9141 || !p->die->die_parent->die_perennial_p))
9142 return false;
9144 /* Everything else in the pubname table is included. */
9145 return true;
9148 /* The pubtypes table shouldn't include types that have been
9149 pruned. */
9150 return (p->die->die_offset != 0
9151 || !flag_eliminate_unused_debug_types);
9154 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9155 generated for the compilation unit. */
9157 static unsigned long
9158 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9160 unsigned long size;
9161 unsigned i;
9162 pubname_entry *p;
9163 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9165 size = DWARF_PUBNAMES_HEADER_SIZE;
9166 FOR_EACH_VEC_ELT (*names, i, p)
9167 if (include_pubname_in_output (names, p))
9168 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9170 size += DWARF_OFFSET_SIZE;
9171 return size;
9174 /* Return the size of the information in the .debug_aranges section. */
9176 static unsigned long
9177 size_of_aranges (void)
9179 unsigned long size;
9181 size = DWARF_ARANGES_HEADER_SIZE;
9183 /* Count the address/length pair for this compilation unit. */
9184 if (text_section_used)
9185 size += 2 * DWARF2_ADDR_SIZE;
9186 if (cold_text_section_used)
9187 size += 2 * DWARF2_ADDR_SIZE;
9188 if (have_multiple_function_sections)
9190 unsigned fde_idx;
9191 dw_fde_ref fde;
9193 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9195 if (DECL_IGNORED_P (fde->decl))
9196 continue;
9197 if (!fde->in_std_section)
9198 size += 2 * DWARF2_ADDR_SIZE;
9199 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9200 size += 2 * DWARF2_ADDR_SIZE;
9204 /* Count the two zero words used to terminated the address range table. */
9205 size += 2 * DWARF2_ADDR_SIZE;
9206 return size;
9209 /* Select the encoding of an attribute value. */
9211 static enum dwarf_form
9212 value_format (dw_attr_node *a)
9214 switch (AT_class (a))
9216 case dw_val_class_addr:
9217 /* Only very few attributes allow DW_FORM_addr. */
9218 switch (a->dw_attr)
9220 case DW_AT_low_pc:
9221 case DW_AT_high_pc:
9222 case DW_AT_entry_pc:
9223 case DW_AT_trampoline:
9224 return (AT_index (a) == NOT_INDEXED
9225 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9226 default:
9227 break;
9229 switch (DWARF2_ADDR_SIZE)
9231 case 1:
9232 return DW_FORM_data1;
9233 case 2:
9234 return DW_FORM_data2;
9235 case 4:
9236 return DW_FORM_data4;
9237 case 8:
9238 return DW_FORM_data8;
9239 default:
9240 gcc_unreachable ();
9242 case dw_val_class_loc_list:
9243 if (dwarf_split_debug_info
9244 && dwarf_version >= 5
9245 && AT_loc_list (a)->num_assigned)
9246 return DW_FORM_loclistx;
9247 /* FALLTHRU */
9248 case dw_val_class_range_list:
9249 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9250 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9251 care about sizes of .debug* sections in shared libraries and
9252 executables and don't take into account relocations that affect just
9253 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9254 table in the .debug_rnglists section. */
9255 if (dwarf_split_debug_info
9256 && dwarf_version >= 5
9257 && AT_class (a) == dw_val_class_range_list
9258 && rnglist_idx
9259 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9260 return DW_FORM_rnglistx;
9261 if (dwarf_version >= 4)
9262 return DW_FORM_sec_offset;
9263 /* FALLTHRU */
9264 case dw_val_class_vms_delta:
9265 case dw_val_class_offset:
9266 switch (DWARF_OFFSET_SIZE)
9268 case 4:
9269 return DW_FORM_data4;
9270 case 8:
9271 return DW_FORM_data8;
9272 default:
9273 gcc_unreachable ();
9275 case dw_val_class_loc:
9276 if (dwarf_version >= 4)
9277 return DW_FORM_exprloc;
9278 switch (constant_size (size_of_locs (AT_loc (a))))
9280 case 1:
9281 return DW_FORM_block1;
9282 case 2:
9283 return DW_FORM_block2;
9284 case 4:
9285 return DW_FORM_block4;
9286 default:
9287 gcc_unreachable ();
9289 case dw_val_class_const:
9290 return DW_FORM_sdata;
9291 case dw_val_class_unsigned_const:
9292 switch (constant_size (AT_unsigned (a)))
9294 case 1:
9295 return DW_FORM_data1;
9296 case 2:
9297 return DW_FORM_data2;
9298 case 4:
9299 /* In DWARF3 DW_AT_data_member_location with
9300 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9301 constant, so we need to use DW_FORM_udata if we need
9302 a large constant. */
9303 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9304 return DW_FORM_udata;
9305 return DW_FORM_data4;
9306 case 8:
9307 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9308 return DW_FORM_udata;
9309 return DW_FORM_data8;
9310 default:
9311 gcc_unreachable ();
9313 case dw_val_class_const_implicit:
9314 case dw_val_class_unsigned_const_implicit:
9315 case dw_val_class_file_implicit:
9316 return DW_FORM_implicit_const;
9317 case dw_val_class_const_double:
9318 switch (HOST_BITS_PER_WIDE_INT)
9320 case 8:
9321 return DW_FORM_data2;
9322 case 16:
9323 return DW_FORM_data4;
9324 case 32:
9325 return DW_FORM_data8;
9326 case 64:
9327 if (dwarf_version >= 5)
9328 return DW_FORM_data16;
9329 /* FALLTHRU */
9330 default:
9331 return DW_FORM_block1;
9333 case dw_val_class_wide_int:
9334 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9336 case 8:
9337 return DW_FORM_data1;
9338 case 16:
9339 return DW_FORM_data2;
9340 case 32:
9341 return DW_FORM_data4;
9342 case 64:
9343 return DW_FORM_data8;
9344 case 128:
9345 if (dwarf_version >= 5)
9346 return DW_FORM_data16;
9347 /* FALLTHRU */
9348 default:
9349 return DW_FORM_block1;
9351 case dw_val_class_vec:
9352 switch (constant_size (a->dw_attr_val.v.val_vec.length
9353 * a->dw_attr_val.v.val_vec.elt_size))
9355 case 1:
9356 return DW_FORM_block1;
9357 case 2:
9358 return DW_FORM_block2;
9359 case 4:
9360 return DW_FORM_block4;
9361 default:
9362 gcc_unreachable ();
9364 case dw_val_class_flag:
9365 if (dwarf_version >= 4)
9367 /* Currently all add_AT_flag calls pass in 1 as last argument,
9368 so DW_FORM_flag_present can be used. If that ever changes,
9369 we'll need to use DW_FORM_flag and have some optimization
9370 in build_abbrev_table that will change those to
9371 DW_FORM_flag_present if it is set to 1 in all DIEs using
9372 the same abbrev entry. */
9373 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9374 return DW_FORM_flag_present;
9376 return DW_FORM_flag;
9377 case dw_val_class_die_ref:
9378 if (AT_ref_external (a))
9379 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9380 else
9381 return DW_FORM_ref;
9382 case dw_val_class_fde_ref:
9383 return DW_FORM_data;
9384 case dw_val_class_lbl_id:
9385 return (AT_index (a) == NOT_INDEXED
9386 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9387 case dw_val_class_lineptr:
9388 case dw_val_class_macptr:
9389 case dw_val_class_loclistsptr:
9390 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9391 case dw_val_class_str:
9392 return AT_string_form (a);
9393 case dw_val_class_file:
9394 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9396 case 1:
9397 return DW_FORM_data1;
9398 case 2:
9399 return DW_FORM_data2;
9400 case 4:
9401 return DW_FORM_data4;
9402 default:
9403 gcc_unreachable ();
9406 case dw_val_class_data8:
9407 return DW_FORM_data8;
9409 case dw_val_class_high_pc:
9410 switch (DWARF2_ADDR_SIZE)
9412 case 1:
9413 return DW_FORM_data1;
9414 case 2:
9415 return DW_FORM_data2;
9416 case 4:
9417 return DW_FORM_data4;
9418 case 8:
9419 return DW_FORM_data8;
9420 default:
9421 gcc_unreachable ();
9424 case dw_val_class_discr_value:
9425 return (a->dw_attr_val.v.val_discr_value.pos
9426 ? DW_FORM_udata
9427 : DW_FORM_sdata);
9428 case dw_val_class_discr_list:
9429 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9431 case 1:
9432 return DW_FORM_block1;
9433 case 2:
9434 return DW_FORM_block2;
9435 case 4:
9436 return DW_FORM_block4;
9437 default:
9438 gcc_unreachable ();
9441 default:
9442 gcc_unreachable ();
9446 /* Output the encoding of an attribute value. */
9448 static void
9449 output_value_format (dw_attr_node *a)
9451 enum dwarf_form form = value_format (a);
9453 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9456 /* Given a die and id, produce the appropriate abbreviations. */
9458 static void
9459 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9461 unsigned ix;
9462 dw_attr_node *a_attr;
9464 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9465 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9466 dwarf_tag_name (abbrev->die_tag));
9468 if (abbrev->die_child != NULL)
9469 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9470 else
9471 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9473 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9475 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9476 dwarf_attr_name (a_attr->dw_attr));
9477 output_value_format (a_attr);
9478 if (value_format (a_attr) == DW_FORM_implicit_const)
9480 if (AT_class (a_attr) == dw_val_class_file_implicit)
9482 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9483 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9484 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9486 else
9487 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9491 dw2_asm_output_data (1, 0, NULL);
9492 dw2_asm_output_data (1, 0, NULL);
9496 /* Output the .debug_abbrev section which defines the DIE abbreviation
9497 table. */
9499 static void
9500 output_abbrev_section (void)
9502 unsigned int abbrev_id;
9503 dw_die_ref abbrev;
9505 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9506 if (abbrev_id != 0)
9507 output_die_abbrevs (abbrev_id, abbrev);
9509 /* Terminate the table. */
9510 dw2_asm_output_data (1, 0, NULL);
9513 /* Output a symbol we can use to refer to this DIE from another CU. */
9515 static inline void
9516 output_die_symbol (dw_die_ref die)
9518 const char *sym = die->die_id.die_symbol;
9520 gcc_assert (!die->comdat_type_p);
9522 if (sym == 0)
9523 return;
9525 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9526 /* We make these global, not weak; if the target doesn't support
9527 .linkonce, it doesn't support combining the sections, so debugging
9528 will break. */
9529 targetm.asm_out.globalize_label (asm_out_file, sym);
9531 ASM_OUTPUT_LABEL (asm_out_file, sym);
9534 /* Return a new location list, given the begin and end range, and the
9535 expression. */
9537 static inline dw_loc_list_ref
9538 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9539 const char *section)
9541 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9543 retlist->begin = begin;
9544 retlist->begin_entry = NULL;
9545 retlist->end = end;
9546 retlist->expr = expr;
9547 retlist->section = section;
9549 return retlist;
9552 /* Generate a new internal symbol for this location list node, if it
9553 hasn't got one yet. */
9555 static inline void
9556 gen_llsym (dw_loc_list_ref list)
9558 gcc_assert (!list->ll_symbol);
9559 list->ll_symbol = gen_internal_sym ("LLST");
9562 /* Output the location list given to us. */
9564 static void
9565 output_loc_list (dw_loc_list_ref list_head)
9567 if (list_head->emitted)
9568 return;
9569 list_head->emitted = true;
9571 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9573 dw_loc_list_ref curr = list_head;
9574 const char *last_section = NULL;
9575 const char *base_label = NULL;
9577 /* Walk the location list, and output each range + expression. */
9578 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9580 unsigned long size;
9581 /* Don't output an entry that starts and ends at the same address. */
9582 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9583 continue;
9584 size = size_of_locs (curr->expr);
9585 /* If the expression is too large, drop it on the floor. We could
9586 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9587 in the expression, but >= 64KB expressions for a single value
9588 in a single range are unlikely very useful. */
9589 if (size > 0xffff)
9590 continue;
9591 if (dwarf_version >= 5)
9593 if (dwarf_split_debug_info)
9595 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9596 uleb128 index into .debug_addr and uleb128 length. */
9597 dw2_asm_output_data (1, DW_LLE_startx_length,
9598 "DW_LLE_startx_length (%s)",
9599 list_head->ll_symbol);
9600 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9601 "Location list range start index "
9602 "(%s)", curr->begin);
9603 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9604 For that case we probably need to emit DW_LLE_startx_endx,
9605 but we'd need 2 .debug_addr entries rather than just one. */
9606 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9607 "Location list length (%s)",
9608 list_head->ll_symbol);
9610 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9612 /* If all code is in .text section, the base address is
9613 already provided by the CU attributes. Use
9614 DW_LLE_offset_pair where both addresses are uleb128 encoded
9615 offsets against that base. */
9616 dw2_asm_output_data (1, DW_LLE_offset_pair,
9617 "DW_LLE_offset_pair (%s)",
9618 list_head->ll_symbol);
9619 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9620 "Location list begin address (%s)",
9621 list_head->ll_symbol);
9622 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9623 "Location list end address (%s)",
9624 list_head->ll_symbol);
9626 else if (HAVE_AS_LEB128)
9628 /* Otherwise, find out how many consecutive entries could share
9629 the same base entry. If just one, emit DW_LLE_start_length,
9630 otherwise emit DW_LLE_base_address for the base address
9631 followed by a series of DW_LLE_offset_pair. */
9632 if (last_section == NULL || curr->section != last_section)
9634 dw_loc_list_ref curr2;
9635 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9636 curr2 = curr2->dw_loc_next)
9638 if (strcmp (curr2->begin, curr2->end) == 0
9639 && !curr2->force)
9640 continue;
9641 if ((unsigned long) size_of_locs (curr2->expr) > 0xffff)
9642 continue;
9643 break;
9645 if (curr2 == NULL || curr->section != curr2->section)
9646 last_section = NULL;
9647 else
9649 last_section = curr->section;
9650 base_label = curr->begin;
9651 dw2_asm_output_data (1, DW_LLE_base_address,
9652 "DW_LLE_base_address (%s)",
9653 list_head->ll_symbol);
9654 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9655 "Base address (%s)",
9656 list_head->ll_symbol);
9659 /* Only one entry with the same base address. Use
9660 DW_LLE_start_length with absolute address and uleb128
9661 length. */
9662 if (last_section == NULL)
9664 dw2_asm_output_data (1, DW_LLE_start_length,
9665 "DW_LLE_start_length (%s)",
9666 list_head->ll_symbol);
9667 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9668 "Location list begin address (%s)",
9669 list_head->ll_symbol);
9670 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9671 "Location list length "
9672 "(%s)", list_head->ll_symbol);
9674 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9675 DW_LLE_base_address. */
9676 else
9678 dw2_asm_output_data (1, DW_LLE_offset_pair,
9679 "DW_LLE_offset_pair (%s)",
9680 list_head->ll_symbol);
9681 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9682 "Location list begin address "
9683 "(%s)", list_head->ll_symbol);
9684 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9685 "Location list end address "
9686 "(%s)", list_head->ll_symbol);
9689 /* The assembler does not support .uleb128 directive. Emit
9690 DW_LLE_start_end with a pair of absolute addresses. */
9691 else
9693 dw2_asm_output_data (1, DW_LLE_start_end,
9694 "DW_LLE_start_end (%s)",
9695 list_head->ll_symbol);
9696 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9697 "Location list begin address (%s)",
9698 list_head->ll_symbol);
9699 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9700 "Location list end address (%s)",
9701 list_head->ll_symbol);
9704 else if (dwarf_split_debug_info)
9706 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9707 and 4 byte length. */
9708 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9709 "Location list start/length entry (%s)",
9710 list_head->ll_symbol);
9711 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9712 "Location list range start index (%s)",
9713 curr->begin);
9714 /* The length field is 4 bytes. If we ever need to support
9715 an 8-byte length, we can add a new DW_LLE code or fall back
9716 to DW_LLE_GNU_start_end_entry. */
9717 dw2_asm_output_delta (4, curr->end, curr->begin,
9718 "Location list range length (%s)",
9719 list_head->ll_symbol);
9721 else if (!have_multiple_function_sections)
9723 /* Pair of relative addresses against start of text section. */
9724 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9725 "Location list begin address (%s)",
9726 list_head->ll_symbol);
9727 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9728 "Location list end address (%s)",
9729 list_head->ll_symbol);
9731 else
9733 /* Pair of absolute addresses. */
9734 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9735 "Location list begin address (%s)",
9736 list_head->ll_symbol);
9737 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9738 "Location list end address (%s)",
9739 list_head->ll_symbol);
9742 /* Output the block length for this list of location operations. */
9743 gcc_assert (size <= 0xffff);
9744 dw2_asm_output_data (2, size, "%s", "Location expression size");
9746 output_loc_sequence (curr->expr, -1);
9749 /* And finally list termination. */
9750 if (dwarf_version >= 5)
9751 dw2_asm_output_data (1, DW_LLE_end_of_list,
9752 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9753 else if (dwarf_split_debug_info)
9754 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9755 "Location list terminator (%s)",
9756 list_head->ll_symbol);
9757 else
9759 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9760 "Location list terminator begin (%s)",
9761 list_head->ll_symbol);
9762 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9763 "Location list terminator end (%s)",
9764 list_head->ll_symbol);
9768 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9769 section. Emit a relocated reference if val_entry is NULL, otherwise,
9770 emit an indirect reference. */
9772 static void
9773 output_range_list_offset (dw_attr_node *a)
9775 const char *name = dwarf_attr_name (a->dw_attr);
9777 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9779 if (dwarf_version >= 5)
9781 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9782 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9783 debug_ranges_section, "%s", name);
9785 else
9787 char *p = strchr (ranges_section_label, '\0');
9788 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9789 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9790 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9791 debug_ranges_section, "%s", name);
9792 *p = '\0';
9795 else if (dwarf_version >= 5)
9797 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9798 gcc_assert (rnglist_idx);
9799 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9801 else
9802 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9803 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9804 "%s (offset from %s)", name, ranges_section_label);
9807 /* Output the offset into the debug_loc section. */
9809 static void
9810 output_loc_list_offset (dw_attr_node *a)
9812 char *sym = AT_loc_list (a)->ll_symbol;
9814 gcc_assert (sym);
9815 if (!dwarf_split_debug_info)
9816 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9817 "%s", dwarf_attr_name (a->dw_attr));
9818 else if (dwarf_version >= 5)
9820 gcc_assert (AT_loc_list (a)->num_assigned);
9821 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9822 dwarf_attr_name (a->dw_attr),
9823 sym);
9825 else
9826 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9827 "%s", dwarf_attr_name (a->dw_attr));
9830 /* Output an attribute's index or value appropriately. */
9832 static void
9833 output_attr_index_or_value (dw_attr_node *a)
9835 const char *name = dwarf_attr_name (a->dw_attr);
9837 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9839 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9840 return;
9842 switch (AT_class (a))
9844 case dw_val_class_addr:
9845 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9846 break;
9847 case dw_val_class_high_pc:
9848 case dw_val_class_lbl_id:
9849 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9850 break;
9851 default:
9852 gcc_unreachable ();
9856 /* Output a type signature. */
9858 static inline void
9859 output_signature (const char *sig, const char *name)
9861 int i;
9863 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9864 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9867 /* Output a discriminant value. */
9869 static inline void
9870 output_discr_value (dw_discr_value *discr_value, const char *name)
9872 if (discr_value->pos)
9873 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9874 else
9875 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9878 /* Output the DIE and its attributes. Called recursively to generate
9879 the definitions of each child DIE. */
9881 static void
9882 output_die (dw_die_ref die)
9884 dw_attr_node *a;
9885 dw_die_ref c;
9886 unsigned long size;
9887 unsigned ix;
9889 /* If someone in another CU might refer to us, set up a symbol for
9890 them to point to. */
9891 if (! die->comdat_type_p && die->die_id.die_symbol)
9892 output_die_symbol (die);
9894 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9895 (unsigned long)die->die_offset,
9896 dwarf_tag_name (die->die_tag));
9898 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9900 const char *name = dwarf_attr_name (a->dw_attr);
9902 switch (AT_class (a))
9904 case dw_val_class_addr:
9905 output_attr_index_or_value (a);
9906 break;
9908 case dw_val_class_offset:
9909 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9910 "%s", name);
9911 break;
9913 case dw_val_class_range_list:
9914 output_range_list_offset (a);
9915 break;
9917 case dw_val_class_loc:
9918 size = size_of_locs (AT_loc (a));
9920 /* Output the block length for this list of location operations. */
9921 if (dwarf_version >= 4)
9922 dw2_asm_output_data_uleb128 (size, "%s", name);
9923 else
9924 dw2_asm_output_data (constant_size (size), size, "%s", name);
9926 output_loc_sequence (AT_loc (a), -1);
9927 break;
9929 case dw_val_class_const:
9930 /* ??? It would be slightly more efficient to use a scheme like is
9931 used for unsigned constants below, but gdb 4.x does not sign
9932 extend. Gdb 5.x does sign extend. */
9933 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9934 break;
9936 case dw_val_class_unsigned_const:
9938 int csize = constant_size (AT_unsigned (a));
9939 if (dwarf_version == 3
9940 && a->dw_attr == DW_AT_data_member_location
9941 && csize >= 4)
9942 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9943 else
9944 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9946 break;
9948 case dw_val_class_const_implicit:
9949 if (flag_debug_asm)
9950 fprintf (asm_out_file, "\t\t\t%s %s ("
9951 HOST_WIDE_INT_PRINT_DEC ")\n",
9952 ASM_COMMENT_START, name, AT_int (a));
9953 break;
9955 case dw_val_class_unsigned_const_implicit:
9956 if (flag_debug_asm)
9957 fprintf (asm_out_file, "\t\t\t%s %s ("
9958 HOST_WIDE_INT_PRINT_HEX ")\n",
9959 ASM_COMMENT_START, name, AT_unsigned (a));
9960 break;
9962 case dw_val_class_const_double:
9964 unsigned HOST_WIDE_INT first, second;
9966 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9967 dw2_asm_output_data (1,
9968 HOST_BITS_PER_DOUBLE_INT
9969 / HOST_BITS_PER_CHAR,
9970 NULL);
9972 if (WORDS_BIG_ENDIAN)
9974 first = a->dw_attr_val.v.val_double.high;
9975 second = a->dw_attr_val.v.val_double.low;
9977 else
9979 first = a->dw_attr_val.v.val_double.low;
9980 second = a->dw_attr_val.v.val_double.high;
9983 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9984 first, "%s", name);
9985 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9986 second, NULL);
9988 break;
9990 case dw_val_class_wide_int:
9992 int i;
9993 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9994 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9995 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9996 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
9997 * l, NULL);
9999 if (WORDS_BIG_ENDIAN)
10000 for (i = len - 1; i >= 0; --i)
10002 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10003 "%s", name);
10004 name = "";
10006 else
10007 for (i = 0; i < len; ++i)
10009 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10010 "%s", name);
10011 name = "";
10014 break;
10016 case dw_val_class_vec:
10018 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10019 unsigned int len = a->dw_attr_val.v.val_vec.length;
10020 unsigned int i;
10021 unsigned char *p;
10023 dw2_asm_output_data (constant_size (len * elt_size),
10024 len * elt_size, "%s", name);
10025 if (elt_size > sizeof (HOST_WIDE_INT))
10027 elt_size /= 2;
10028 len *= 2;
10030 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10031 i < len;
10032 i++, p += elt_size)
10033 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10034 "fp or vector constant word %u", i);
10035 break;
10038 case dw_val_class_flag:
10039 if (dwarf_version >= 4)
10041 /* Currently all add_AT_flag calls pass in 1 as last argument,
10042 so DW_FORM_flag_present can be used. If that ever changes,
10043 we'll need to use DW_FORM_flag and have some optimization
10044 in build_abbrev_table that will change those to
10045 DW_FORM_flag_present if it is set to 1 in all DIEs using
10046 the same abbrev entry. */
10047 gcc_assert (AT_flag (a) == 1);
10048 if (flag_debug_asm)
10049 fprintf (asm_out_file, "\t\t\t%s %s\n",
10050 ASM_COMMENT_START, name);
10051 break;
10053 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10054 break;
10056 case dw_val_class_loc_list:
10057 output_loc_list_offset (a);
10058 break;
10060 case dw_val_class_die_ref:
10061 if (AT_ref_external (a))
10063 if (AT_ref (a)->comdat_type_p)
10065 comdat_type_node *type_node
10066 = AT_ref (a)->die_id.die_type_node;
10068 gcc_assert (type_node);
10069 output_signature (type_node->signature, name);
10071 else
10073 const char *sym = AT_ref (a)->die_id.die_symbol;
10074 int size;
10076 gcc_assert (sym);
10077 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10078 length, whereas in DWARF3 it's always sized as an
10079 offset. */
10080 if (dwarf_version == 2)
10081 size = DWARF2_ADDR_SIZE;
10082 else
10083 size = DWARF_OFFSET_SIZE;
10084 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10085 name);
10088 else
10090 gcc_assert (AT_ref (a)->die_offset);
10091 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10092 "%s", name);
10094 break;
10096 case dw_val_class_fde_ref:
10098 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10100 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10101 a->dw_attr_val.v.val_fde_index * 2);
10102 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10103 "%s", name);
10105 break;
10107 case dw_val_class_vms_delta:
10108 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10109 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10110 AT_vms_delta2 (a), AT_vms_delta1 (a),
10111 "%s", name);
10112 #else
10113 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10114 AT_vms_delta2 (a), AT_vms_delta1 (a),
10115 "%s", name);
10116 #endif
10117 break;
10119 case dw_val_class_lbl_id:
10120 output_attr_index_or_value (a);
10121 break;
10123 case dw_val_class_lineptr:
10124 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10125 debug_line_section, "%s", name);
10126 break;
10128 case dw_val_class_macptr:
10129 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10130 debug_macinfo_section, "%s", name);
10131 break;
10133 case dw_val_class_loclistsptr:
10134 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10135 debug_loc_section, "%s", name);
10136 break;
10138 case dw_val_class_str:
10139 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10140 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10141 a->dw_attr_val.v.val_str->label,
10142 debug_str_section,
10143 "%s: \"%s\"", name, AT_string (a));
10144 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10145 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10146 a->dw_attr_val.v.val_str->label,
10147 debug_line_str_section,
10148 "%s: \"%s\"", name, AT_string (a));
10149 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10150 dw2_asm_output_data_uleb128 (AT_index (a),
10151 "%s: \"%s\"", name, AT_string (a));
10152 else
10153 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10154 break;
10156 case dw_val_class_file:
10158 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10160 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10161 a->dw_attr_val.v.val_file->filename);
10162 break;
10165 case dw_val_class_file_implicit:
10166 if (flag_debug_asm)
10167 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10168 ASM_COMMENT_START, name,
10169 maybe_emit_file (a->dw_attr_val.v.val_file),
10170 a->dw_attr_val.v.val_file->filename);
10171 break;
10173 case dw_val_class_data8:
10175 int i;
10177 for (i = 0; i < 8; i++)
10178 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10179 i == 0 ? "%s" : NULL, name);
10180 break;
10183 case dw_val_class_high_pc:
10184 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10185 get_AT_low_pc (die), "DW_AT_high_pc");
10186 break;
10188 case dw_val_class_discr_value:
10189 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10190 break;
10192 case dw_val_class_discr_list:
10194 dw_discr_list_ref list = AT_discr_list (a);
10195 const int size = size_of_discr_list (list);
10197 /* This is a block, so output its length first. */
10198 dw2_asm_output_data (constant_size (size), size,
10199 "%s: block size", name);
10201 for (; list != NULL; list = list->dw_discr_next)
10203 /* One byte for the discriminant value descriptor, and then as
10204 many LEB128 numbers as required. */
10205 if (list->dw_discr_range)
10206 dw2_asm_output_data (1, DW_DSC_range,
10207 "%s: DW_DSC_range", name);
10208 else
10209 dw2_asm_output_data (1, DW_DSC_label,
10210 "%s: DW_DSC_label", name);
10212 output_discr_value (&list->dw_discr_lower_bound, name);
10213 if (list->dw_discr_range)
10214 output_discr_value (&list->dw_discr_upper_bound, name);
10216 break;
10219 default:
10220 gcc_unreachable ();
10224 FOR_EACH_CHILD (die, c, output_die (c));
10226 /* Add null byte to terminate sibling list. */
10227 if (die->die_child != NULL)
10228 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10229 (unsigned long) die->die_offset);
10232 /* Output the compilation unit that appears at the beginning of the
10233 .debug_info section, and precedes the DIE descriptions. */
10235 static void
10236 output_compilation_unit_header (enum dwarf_unit_type ut)
10238 if (!XCOFF_DEBUGGING_INFO)
10240 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10241 dw2_asm_output_data (4, 0xffffffff,
10242 "Initial length escape value indicating 64-bit DWARF extension");
10243 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10244 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10245 "Length of Compilation Unit Info");
10248 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10249 if (dwarf_version >= 5)
10251 const char *name;
10252 switch (ut)
10254 case DW_UT_compile: name = "DW_UT_compile"; break;
10255 case DW_UT_type: name = "DW_UT_type"; break;
10256 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10257 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10258 default: gcc_unreachable ();
10260 dw2_asm_output_data (1, ut, name);
10261 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10263 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10264 debug_abbrev_section,
10265 "Offset Into Abbrev. Section");
10266 if (dwarf_version < 5)
10267 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10270 /* Output the compilation unit DIE and its children. */
10272 static void
10273 output_comp_unit (dw_die_ref die, int output_if_empty,
10274 const unsigned char *dwo_id)
10276 const char *secname, *oldsym;
10277 char *tmp;
10279 /* Unless we are outputting main CU, we may throw away empty ones. */
10280 if (!output_if_empty && die->die_child == NULL)
10281 return;
10283 /* Even if there are no children of this DIE, we must output the information
10284 about the compilation unit. Otherwise, on an empty translation unit, we
10285 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10286 will then complain when examining the file. First mark all the DIEs in
10287 this CU so we know which get local refs. */
10288 mark_dies (die);
10290 external_ref_hash_type *extern_map = optimize_external_refs (die);
10292 /* For now, optimize only the main CU, in order to optimize the rest
10293 we'd need to see all of them earlier. Leave the rest for post-linking
10294 tools like DWZ. */
10295 if (die == comp_unit_die ())
10296 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10298 build_abbrev_table (die, extern_map);
10300 optimize_abbrev_table ();
10302 delete extern_map;
10304 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10305 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
10306 calc_die_sizes (die);
10308 oldsym = die->die_id.die_symbol;
10309 if (oldsym)
10311 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10313 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10314 secname = tmp;
10315 die->die_id.die_symbol = NULL;
10316 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10318 else
10320 switch_to_section (debug_info_section);
10321 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10322 info_section_emitted = true;
10325 /* Output debugging information. */
10326 output_compilation_unit_header (dwo_id
10327 ? DW_UT_split_compile : DW_UT_compile);
10328 if (dwarf_version >= 5)
10330 if (dwo_id != NULL)
10331 for (int i = 0; i < 8; i++)
10332 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10333 else
10334 /* Hope all the padding will be removed for DWARF 5 final for
10335 DW_AT_compile and DW_AT_partial. */
10336 dw2_asm_output_data (8, 0, "Padding 1");
10338 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Padding 2");
10340 output_die (die);
10342 /* Leave the marks on the main CU, so we can check them in
10343 output_pubnames. */
10344 if (oldsym)
10346 unmark_dies (die);
10347 die->die_id.die_symbol = oldsym;
10351 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10352 and .debug_pubtypes. This is configured per-target, but can be
10353 overridden by the -gpubnames or -gno-pubnames options. */
10355 static inline bool
10356 want_pubnames (void)
10358 if (debug_info_level <= DINFO_LEVEL_TERSE)
10359 return false;
10360 if (debug_generate_pub_sections != -1)
10361 return debug_generate_pub_sections;
10362 return targetm.want_debug_pub_sections;
10365 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10367 static void
10368 add_AT_pubnames (dw_die_ref die)
10370 if (want_pubnames ())
10371 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10374 /* Add a string attribute value to a skeleton DIE. */
10376 static inline void
10377 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10378 const char *str)
10380 dw_attr_node attr;
10381 struct indirect_string_node *node;
10383 if (! skeleton_debug_str_hash)
10384 skeleton_debug_str_hash
10385 = hash_table<indirect_string_hasher>::create_ggc (10);
10387 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10388 find_string_form (node);
10389 if (node->form == DW_FORM_GNU_str_index)
10390 node->form = DW_FORM_strp;
10392 attr.dw_attr = attr_kind;
10393 attr.dw_attr_val.val_class = dw_val_class_str;
10394 attr.dw_attr_val.val_entry = NULL;
10395 attr.dw_attr_val.v.val_str = node;
10396 add_dwarf_attr (die, &attr);
10399 /* Helper function to generate top-level dies for skeleton debug_info and
10400 debug_types. */
10402 static void
10403 add_top_level_skeleton_die_attrs (dw_die_ref die)
10405 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10406 const char *comp_dir = comp_dir_string ();
10408 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10409 if (comp_dir != NULL)
10410 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10411 add_AT_pubnames (die);
10412 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10415 /* Output skeleton debug sections that point to the dwo file. */
10417 static void
10418 output_skeleton_debug_sections (dw_die_ref comp_unit,
10419 const unsigned char *dwo_id)
10421 /* These attributes will be found in the full debug_info section. */
10422 remove_AT (comp_unit, DW_AT_producer);
10423 remove_AT (comp_unit, DW_AT_language);
10425 switch_to_section (debug_skeleton_info_section);
10426 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10428 /* Produce the skeleton compilation-unit header. This one differs enough from
10429 a normal CU header that it's better not to call output_compilation_unit
10430 header. */
10431 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10432 dw2_asm_output_data (4, 0xffffffff,
10433 "Initial length escape value indicating 64-bit DWARF extension");
10435 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10436 DWARF_COMPILE_UNIT_HEADER_SIZE
10437 - DWARF_INITIAL_LENGTH_SIZE
10438 + size_of_die (comp_unit),
10439 "Length of Compilation Unit Info");
10440 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10441 if (dwarf_version >= 5)
10443 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10444 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10446 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10447 debug_skeleton_abbrev_section,
10448 "Offset Into Abbrev. Section");
10449 if (dwarf_version < 5)
10450 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10451 else
10453 for (int i = 0; i < 8; i++)
10454 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10456 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Padding 2");
10459 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10460 output_die (comp_unit);
10462 /* Build the skeleton debug_abbrev section. */
10463 switch_to_section (debug_skeleton_abbrev_section);
10464 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10466 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10468 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10471 /* Output a comdat type unit DIE and its children. */
10473 static void
10474 output_comdat_type_unit (comdat_type_node *node)
10476 const char *secname;
10477 char *tmp;
10478 int i;
10479 #if defined (OBJECT_FORMAT_ELF)
10480 tree comdat_key;
10481 #endif
10483 /* First mark all the DIEs in this CU so we know which get local refs. */
10484 mark_dies (node->root_die);
10486 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10488 build_abbrev_table (node->root_die, extern_map);
10490 delete extern_map;
10491 extern_map = NULL;
10493 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10494 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10495 calc_die_sizes (node->root_die);
10497 #if defined (OBJECT_FORMAT_ELF)
10498 if (dwarf_version >= 5)
10500 if (!dwarf_split_debug_info)
10501 secname = ".debug_info";
10502 else
10503 secname = ".debug_info.dwo";
10505 else if (!dwarf_split_debug_info)
10506 secname = ".debug_types";
10507 else
10508 secname = ".debug_types.dwo";
10510 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10511 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10512 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10513 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10514 comdat_key = get_identifier (tmp);
10515 targetm.asm_out.named_section (secname,
10516 SECTION_DEBUG | SECTION_LINKONCE,
10517 comdat_key);
10518 #else
10519 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10520 sprintf (tmp, (dwarf_version >= 5
10521 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10522 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10523 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10524 secname = tmp;
10525 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10526 #endif
10528 /* Output debugging information. */
10529 output_compilation_unit_header (dwarf_split_debug_info
10530 ? DW_UT_split_type : DW_UT_type);
10531 output_signature (node->signature, "Type Signature");
10532 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10533 "Offset to Type DIE");
10534 output_die (node->root_die);
10536 unmark_dies (node->root_die);
10539 /* Return the DWARF2/3 pubname associated with a decl. */
10541 static const char *
10542 dwarf2_name (tree decl, int scope)
10544 if (DECL_NAMELESS (decl))
10545 return NULL;
10546 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10549 /* Add a new entry to .debug_pubnames if appropriate. */
10551 static void
10552 add_pubname_string (const char *str, dw_die_ref die)
10554 pubname_entry e;
10556 e.die = die;
10557 e.name = xstrdup (str);
10558 vec_safe_push (pubname_table, e);
10561 static void
10562 add_pubname (tree decl, dw_die_ref die)
10564 if (!want_pubnames ())
10565 return;
10567 /* Don't add items to the table when we expect that the consumer will have
10568 just read the enclosing die. For example, if the consumer is looking at a
10569 class_member, it will either be inside the class already, or will have just
10570 looked up the class to find the member. Either way, searching the class is
10571 faster than searching the index. */
10572 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10573 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10575 const char *name = dwarf2_name (decl, 1);
10577 if (name)
10578 add_pubname_string (name, die);
10582 /* Add an enumerator to the pubnames section. */
10584 static void
10585 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10587 pubname_entry e;
10589 gcc_assert (scope_name);
10590 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10591 e.die = die;
10592 vec_safe_push (pubname_table, e);
10595 /* Add a new entry to .debug_pubtypes if appropriate. */
10597 static void
10598 add_pubtype (tree decl, dw_die_ref die)
10600 pubname_entry e;
10602 if (!want_pubnames ())
10603 return;
10605 if ((TREE_PUBLIC (decl)
10606 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10607 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10609 tree scope = NULL;
10610 const char *scope_name = "";
10611 const char *sep = is_cxx () ? "::" : ".";
10612 const char *name;
10614 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10615 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10617 scope_name = lang_hooks.dwarf_name (scope, 1);
10618 if (scope_name != NULL && scope_name[0] != '\0')
10619 scope_name = concat (scope_name, sep, NULL);
10620 else
10621 scope_name = "";
10624 if (TYPE_P (decl))
10625 name = type_tag (decl);
10626 else
10627 name = lang_hooks.dwarf_name (decl, 1);
10629 /* If we don't have a name for the type, there's no point in adding
10630 it to the table. */
10631 if (name != NULL && name[0] != '\0')
10633 e.die = die;
10634 e.name = concat (scope_name, name, NULL);
10635 vec_safe_push (pubtype_table, e);
10638 /* Although it might be more consistent to add the pubinfo for the
10639 enumerators as their dies are created, they should only be added if the
10640 enum type meets the criteria above. So rather than re-check the parent
10641 enum type whenever an enumerator die is created, just output them all
10642 here. This isn't protected by the name conditional because anonymous
10643 enums don't have names. */
10644 if (die->die_tag == DW_TAG_enumeration_type)
10646 dw_die_ref c;
10648 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10653 /* Output a single entry in the pubnames table. */
10655 static void
10656 output_pubname (dw_offset die_offset, pubname_entry *entry)
10658 dw_die_ref die = entry->die;
10659 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10661 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10663 if (debug_generate_pub_sections == 2)
10665 /* This logic follows gdb's method for determining the value of the flag
10666 byte. */
10667 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10668 switch (die->die_tag)
10670 case DW_TAG_typedef:
10671 case DW_TAG_base_type:
10672 case DW_TAG_subrange_type:
10673 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10674 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10675 break;
10676 case DW_TAG_enumerator:
10677 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10678 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10679 if (!is_cxx () && !is_java ())
10680 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10681 break;
10682 case DW_TAG_subprogram:
10683 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10684 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10685 if (!is_ada ())
10686 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10687 break;
10688 case DW_TAG_constant:
10689 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10690 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10691 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10692 break;
10693 case DW_TAG_variable:
10694 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10695 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10696 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10697 break;
10698 case DW_TAG_namespace:
10699 case DW_TAG_imported_declaration:
10700 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10701 break;
10702 case DW_TAG_class_type:
10703 case DW_TAG_interface_type:
10704 case DW_TAG_structure_type:
10705 case DW_TAG_union_type:
10706 case DW_TAG_enumeration_type:
10707 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10708 if (!is_cxx () && !is_java ())
10709 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10710 break;
10711 default:
10712 /* An unusual tag. Leave the flag-byte empty. */
10713 break;
10715 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10716 "GDB-index flags");
10719 dw2_asm_output_nstring (entry->name, -1, "external name");
10723 /* Output the public names table used to speed up access to externally
10724 visible names; or the public types table used to find type definitions. */
10726 static void
10727 output_pubnames (vec<pubname_entry, va_gc> *names)
10729 unsigned i;
10730 unsigned long pubnames_length = size_of_pubnames (names);
10731 pubname_entry *pub;
10733 if (!XCOFF_DEBUGGING_INFO)
10735 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10736 dw2_asm_output_data (4, 0xffffffff,
10737 "Initial length escape value indicating 64-bit DWARF extension");
10738 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10739 "Pub Info Length");
10742 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10743 dw2_asm_output_data (2, 2, "DWARF Version");
10745 if (dwarf_split_debug_info)
10746 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10747 debug_skeleton_info_section,
10748 "Offset of Compilation Unit Info");
10749 else
10750 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10751 debug_info_section,
10752 "Offset of Compilation Unit Info");
10753 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10754 "Compilation Unit Length");
10756 FOR_EACH_VEC_ELT (*names, i, pub)
10758 if (include_pubname_in_output (names, pub))
10760 dw_offset die_offset = pub->die->die_offset;
10762 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10763 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10764 gcc_assert (pub->die->die_mark);
10766 /* If we're putting types in their own .debug_types sections,
10767 the .debug_pubtypes table will still point to the compile
10768 unit (not the type unit), so we want to use the offset of
10769 the skeleton DIE (if there is one). */
10770 if (pub->die->comdat_type_p && names == pubtype_table)
10772 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10774 if (type_node != NULL)
10775 die_offset = (type_node->skeleton_die != NULL
10776 ? type_node->skeleton_die->die_offset
10777 : comp_unit_die ()->die_offset);
10780 output_pubname (die_offset, pub);
10784 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10787 /* Output public names and types tables if necessary. */
10789 static void
10790 output_pubtables (void)
10792 if (!want_pubnames () || !info_section_emitted)
10793 return;
10795 switch_to_section (debug_pubnames_section);
10796 output_pubnames (pubname_table);
10797 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10798 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10799 simply won't look for the section. */
10800 switch_to_section (debug_pubtypes_section);
10801 output_pubnames (pubtype_table);
10805 /* Output the information that goes into the .debug_aranges table.
10806 Namely, define the beginning and ending address range of the
10807 text section generated for this compilation unit. */
10809 static void
10810 output_aranges (void)
10812 unsigned i;
10813 unsigned long aranges_length = size_of_aranges ();
10815 if (!XCOFF_DEBUGGING_INFO)
10817 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10818 dw2_asm_output_data (4, 0xffffffff,
10819 "Initial length escape value indicating 64-bit DWARF extension");
10820 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10821 "Length of Address Ranges Info");
10824 /* Version number for aranges is still 2, even up to DWARF5. */
10825 dw2_asm_output_data (2, 2, "DWARF Version");
10826 if (dwarf_split_debug_info)
10827 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10828 debug_skeleton_info_section,
10829 "Offset of Compilation Unit Info");
10830 else
10831 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10832 debug_info_section,
10833 "Offset of Compilation Unit Info");
10834 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10835 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10837 /* We need to align to twice the pointer size here. */
10838 if (DWARF_ARANGES_PAD_SIZE)
10840 /* Pad using a 2 byte words so that padding is correct for any
10841 pointer size. */
10842 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10843 2 * DWARF2_ADDR_SIZE);
10844 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10845 dw2_asm_output_data (2, 0, NULL);
10848 /* It is necessary not to output these entries if the sections were
10849 not used; if the sections were not used, the length will be 0 and
10850 the address may end up as 0 if the section is discarded by ld
10851 --gc-sections, leaving an invalid (0, 0) entry that can be
10852 confused with the terminator. */
10853 if (text_section_used)
10855 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10856 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10857 text_section_label, "Length");
10859 if (cold_text_section_used)
10861 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10862 "Address");
10863 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10864 cold_text_section_label, "Length");
10867 if (have_multiple_function_sections)
10869 unsigned fde_idx;
10870 dw_fde_ref fde;
10872 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10874 if (DECL_IGNORED_P (fde->decl))
10875 continue;
10876 if (!fde->in_std_section)
10878 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10879 "Address");
10880 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10881 fde->dw_fde_begin, "Length");
10883 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10885 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10886 "Address");
10887 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10888 fde->dw_fde_second_begin, "Length");
10893 /* Output the terminator words. */
10894 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10895 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10898 /* Add a new entry to .debug_ranges. Return its index into
10899 ranges_table vector. */
10901 static unsigned int
10902 add_ranges_num (int num, bool maybe_new_sec)
10904 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10905 vec_safe_push (ranges_table, r);
10906 return vec_safe_length (ranges_table) - 1;
10909 /* Add a new entry to .debug_ranges corresponding to a block, or a
10910 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10911 this entry might be in a different section from previous range. */
10913 static unsigned int
10914 add_ranges (const_tree block, bool maybe_new_sec)
10916 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10919 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10920 chain, or middle entry of a chain that will be directly referred to. */
10922 static void
10923 note_rnglist_head (unsigned int offset)
10925 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10926 return;
10927 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
10930 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10931 When using dwarf_split_debug_info, address attributes in dies destined
10932 for the final executable should be direct references--setting the
10933 parameter force_direct ensures this behavior. */
10935 static void
10936 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10937 bool *added, bool force_direct)
10939 unsigned int in_use = vec_safe_length (ranges_by_label);
10940 unsigned int offset;
10941 dw_ranges_by_label rbl = { begin, end };
10942 vec_safe_push (ranges_by_label, rbl);
10943 offset = add_ranges_num (-(int)in_use - 1, true);
10944 if (!*added)
10946 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10947 *added = true;
10948 note_rnglist_head (offset);
10952 /* Emit .debug_ranges section. */
10954 static void
10955 output_ranges (void)
10957 unsigned i;
10958 static const char *const start_fmt = "Offset %#x";
10959 const char *fmt = start_fmt;
10960 dw_ranges *r;
10962 switch_to_section (debug_ranges_section);
10963 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
10964 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
10966 int block_num = r->num;
10968 if (block_num > 0)
10970 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10971 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10973 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10974 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10976 /* If all code is in the text section, then the compilation
10977 unit base address defaults to DW_AT_low_pc, which is the
10978 base of the text section. */
10979 if (!have_multiple_function_sections)
10981 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10982 text_section_label,
10983 fmt, i * 2 * DWARF2_ADDR_SIZE);
10984 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10985 text_section_label, NULL);
10988 /* Otherwise, the compilation unit base address is zero,
10989 which allows us to use absolute addresses, and not worry
10990 about whether the target supports cross-section
10991 arithmetic. */
10992 else
10994 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10995 fmt, i * 2 * DWARF2_ADDR_SIZE);
10996 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10999 fmt = NULL;
11002 /* Negative block_num stands for an index into ranges_by_label. */
11003 else if (block_num < 0)
11005 int lab_idx = - block_num - 1;
11007 if (!have_multiple_function_sections)
11009 gcc_unreachable ();
11010 #if 0
11011 /* If we ever use add_ranges_by_labels () for a single
11012 function section, all we have to do is to take out
11013 the #if 0 above. */
11014 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11015 (*ranges_by_label)[lab_idx].begin,
11016 text_section_label,
11017 fmt, i * 2 * DWARF2_ADDR_SIZE);
11018 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11019 (*ranges_by_label)[lab_idx].end,
11020 text_section_label, NULL);
11021 #endif
11023 else
11025 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11026 (*ranges_by_label)[lab_idx].begin,
11027 fmt, i * 2 * DWARF2_ADDR_SIZE);
11028 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11029 (*ranges_by_label)[lab_idx].end,
11030 NULL);
11033 else
11035 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11036 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11037 fmt = start_fmt;
11042 /* Non-zero if .debug_line_str should be used for .debug_line section
11043 strings or strings that are likely shareable with those. */
11044 #define DWARF5_USE_DEBUG_LINE_STR \
11045 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11046 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11047 /* FIXME: there is no .debug_line_str.dwo section, \
11048 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11049 && !dwarf_split_debug_info)
11051 /* Assign .debug_rnglists indexes. */
11053 static void
11054 index_rnglists (void)
11056 unsigned i;
11057 dw_ranges *r;
11059 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11060 if (r->label)
11061 r->idx = rnglist_idx++;
11064 /* Emit .debug_rnglists section. */
11066 static void
11067 output_rnglists (void)
11069 unsigned i;
11070 dw_ranges *r;
11071 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11072 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11073 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11075 switch_to_section (debug_ranges_section);
11076 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11077 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11078 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11079 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11080 dw2_asm_output_data (4, 0xffffffff,
11081 "Initial length escape value indicating "
11082 "64-bit DWARF extension");
11083 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11084 "Length of Range Lists");
11085 ASM_OUTPUT_LABEL (asm_out_file, l1);
11086 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11087 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11088 dw2_asm_output_data (1, 0, "Segment Size");
11089 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11090 about relocation sizes and primarily care about the size of .debug*
11091 sections in linked shared libraries and executables, then
11092 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11093 into it are usually larger than just DW_FORM_sec_offset offsets
11094 into the .debug_rnglists section. */
11095 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11096 "Offset Entry Count");
11097 if (dwarf_split_debug_info)
11099 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11100 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11101 if (r->label)
11102 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11103 ranges_base_label, NULL);
11106 const char *lab = "";
11107 unsigned int len = vec_safe_length (ranges_table);
11108 const char *base = NULL;
11109 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11111 int block_num = r->num;
11113 if (r->label)
11115 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11116 lab = r->label;
11118 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11119 base = NULL;
11120 if (block_num > 0)
11122 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11123 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11125 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11126 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11128 if (HAVE_AS_LEB128)
11130 /* If all code is in the text section, then the compilation
11131 unit base address defaults to DW_AT_low_pc, which is the
11132 base of the text section. */
11133 if (!have_multiple_function_sections)
11135 dw2_asm_output_data (1, DW_RLE_offset_pair,
11136 "DW_RLE_offset_pair (%s)", lab);
11137 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11138 "Range begin address (%s)", lab);
11139 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11140 "Range end address (%s)", lab);
11141 continue;
11143 if (base == NULL)
11145 dw_ranges *r2 = NULL;
11146 if (i < len - 1)
11147 r2 = &(*ranges_table)[i + 1];
11148 if (r2
11149 && r2->num != 0
11150 && r2->label == NULL
11151 && !r2->maybe_new_sec)
11153 dw2_asm_output_data (1, DW_RLE_base_address,
11154 "DW_RLE_base_address (%s)", lab);
11155 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11156 "Base address (%s)", lab);
11157 strcpy (basebuf, blabel);
11158 base = basebuf;
11161 if (base)
11163 dw2_asm_output_data (1, DW_RLE_offset_pair,
11164 "DW_RLE_offset_pair (%s)", lab);
11165 dw2_asm_output_delta_uleb128 (blabel, base,
11166 "Range begin address (%s)", lab);
11167 dw2_asm_output_delta_uleb128 (elabel, base,
11168 "Range end address (%s)", lab);
11169 continue;
11171 dw2_asm_output_data (1, DW_RLE_start_length,
11172 "DW_RLE_start_length (%s)", lab);
11173 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11174 "Range begin address (%s)", lab);
11175 dw2_asm_output_delta_uleb128 (elabel, blabel,
11176 "Range length (%s)", lab);
11178 else
11180 dw2_asm_output_data (1, DW_RLE_start_end,
11181 "DW_RLE_start_end (%s)", lab);
11182 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11183 "Range begin address (%s)", lab);
11184 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11185 "Range end address (%s)", lab);
11189 /* Negative block_num stands for an index into ranges_by_label. */
11190 else if (block_num < 0)
11192 int lab_idx = - block_num - 1;
11193 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11194 const char *elabel = (*ranges_by_label)[lab_idx].end;
11196 if (!have_multiple_function_sections)
11197 gcc_unreachable ();
11198 if (HAVE_AS_LEB128)
11200 dw2_asm_output_data (1, DW_RLE_start_length,
11201 "DW_RLE_start_length (%s)", lab);
11202 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11203 "Range begin address (%s)", lab);
11204 dw2_asm_output_delta_uleb128 (elabel, blabel,
11205 "Range length (%s)", lab);
11207 else
11209 dw2_asm_output_data (1, DW_RLE_start_end,
11210 "DW_RLE_start_end (%s)", lab);
11211 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11212 "Range begin address (%s)", lab);
11213 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11214 "Range end address (%s)", lab);
11217 else
11218 dw2_asm_output_data (1, DW_RLE_end_of_list,
11219 "DW_RLE_end_of_list (%s)", lab);
11221 ASM_OUTPUT_LABEL (asm_out_file, l2);
11224 /* Data structure containing information about input files. */
11225 struct file_info
11227 const char *path; /* Complete file name. */
11228 const char *fname; /* File name part. */
11229 int length; /* Length of entire string. */
11230 struct dwarf_file_data * file_idx; /* Index in input file table. */
11231 int dir_idx; /* Index in directory table. */
11234 /* Data structure containing information about directories with source
11235 files. */
11236 struct dir_info
11238 const char *path; /* Path including directory name. */
11239 int length; /* Path length. */
11240 int prefix; /* Index of directory entry which is a prefix. */
11241 int count; /* Number of files in this directory. */
11242 int dir_idx; /* Index of directory used as base. */
11245 /* Callback function for file_info comparison. We sort by looking at
11246 the directories in the path. */
11248 static int
11249 file_info_cmp (const void *p1, const void *p2)
11251 const struct file_info *const s1 = (const struct file_info *) p1;
11252 const struct file_info *const s2 = (const struct file_info *) p2;
11253 const unsigned char *cp1;
11254 const unsigned char *cp2;
11256 /* Take care of file names without directories. We need to make sure that
11257 we return consistent values to qsort since some will get confused if
11258 we return the same value when identical operands are passed in opposite
11259 orders. So if neither has a directory, return 0 and otherwise return
11260 1 or -1 depending on which one has the directory. */
11261 if ((s1->path == s1->fname || s2->path == s2->fname))
11262 return (s2->path == s2->fname) - (s1->path == s1->fname);
11264 cp1 = (const unsigned char *) s1->path;
11265 cp2 = (const unsigned char *) s2->path;
11267 while (1)
11269 ++cp1;
11270 ++cp2;
11271 /* Reached the end of the first path? If so, handle like above. */
11272 if ((cp1 == (const unsigned char *) s1->fname)
11273 || (cp2 == (const unsigned char *) s2->fname))
11274 return ((cp2 == (const unsigned char *) s2->fname)
11275 - (cp1 == (const unsigned char *) s1->fname));
11277 /* Character of current path component the same? */
11278 else if (*cp1 != *cp2)
11279 return *cp1 - *cp2;
11283 struct file_name_acquire_data
11285 struct file_info *files;
11286 int used_files;
11287 int max_files;
11290 /* Traversal function for the hash table. */
11293 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11295 struct dwarf_file_data *d = *slot;
11296 struct file_info *fi;
11297 const char *f;
11299 gcc_assert (fnad->max_files >= d->emitted_number);
11301 if (! d->emitted_number)
11302 return 1;
11304 gcc_assert (fnad->max_files != fnad->used_files);
11306 fi = fnad->files + fnad->used_files++;
11308 /* Skip all leading "./". */
11309 f = d->filename;
11310 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11311 f += 2;
11313 /* Create a new array entry. */
11314 fi->path = f;
11315 fi->length = strlen (f);
11316 fi->file_idx = d;
11318 /* Search for the file name part. */
11319 f = strrchr (f, DIR_SEPARATOR);
11320 #if defined (DIR_SEPARATOR_2)
11322 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11324 if (g != NULL)
11326 if (f == NULL || f < g)
11327 f = g;
11330 #endif
11332 fi->fname = f == NULL ? fi->path : f + 1;
11333 return 1;
11336 /* Helper function for output_file_names. Emit a FORM encoded
11337 string STR, with assembly comment start ENTRY_KIND and
11338 index IDX */
11340 static void
11341 output_line_string (enum dwarf_form form, const char *str,
11342 const char *entry_kind, unsigned int idx)
11344 switch (form)
11346 case DW_FORM_string:
11347 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11348 break;
11349 case DW_FORM_line_strp:
11350 if (!debug_line_str_hash)
11351 debug_line_str_hash
11352 = hash_table<indirect_string_hasher>::create_ggc (10);
11354 struct indirect_string_node *node;
11355 node = find_AT_string_in_table (str, debug_line_str_hash);
11356 set_indirect_string (node);
11357 node->form = form;
11358 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11359 debug_line_str_section, "%s: %#x: \"%s\"",
11360 entry_kind, 0, node->str);
11361 break;
11362 default:
11363 gcc_unreachable ();
11367 /* Output the directory table and the file name table. We try to minimize
11368 the total amount of memory needed. A heuristic is used to avoid large
11369 slowdowns with many input files. */
11371 static void
11372 output_file_names (void)
11374 struct file_name_acquire_data fnad;
11375 int numfiles;
11376 struct file_info *files;
11377 struct dir_info *dirs;
11378 int *saved;
11379 int *savehere;
11380 int *backmap;
11381 int ndirs;
11382 int idx_offset;
11383 int i;
11385 if (!last_emitted_file)
11387 if (dwarf_version >= 5)
11389 dw2_asm_output_data (1, 0, "Directory entry format count");
11390 dw2_asm_output_data_uleb128 (0, "Directories count");
11391 dw2_asm_output_data (1, 0, "File name entry format count");
11392 dw2_asm_output_data_uleb128 (0, "File names count");
11394 else
11396 dw2_asm_output_data (1, 0, "End directory table");
11397 dw2_asm_output_data (1, 0, "End file name table");
11399 return;
11402 numfiles = last_emitted_file->emitted_number;
11404 /* Allocate the various arrays we need. */
11405 files = XALLOCAVEC (struct file_info, numfiles);
11406 dirs = XALLOCAVEC (struct dir_info, numfiles);
11408 fnad.files = files;
11409 fnad.used_files = 0;
11410 fnad.max_files = numfiles;
11411 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11412 gcc_assert (fnad.used_files == fnad.max_files);
11414 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11416 /* Find all the different directories used. */
11417 dirs[0].path = files[0].path;
11418 dirs[0].length = files[0].fname - files[0].path;
11419 dirs[0].prefix = -1;
11420 dirs[0].count = 1;
11421 dirs[0].dir_idx = 0;
11422 files[0].dir_idx = 0;
11423 ndirs = 1;
11425 for (i = 1; i < numfiles; i++)
11426 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11427 && memcmp (dirs[ndirs - 1].path, files[i].path,
11428 dirs[ndirs - 1].length) == 0)
11430 /* Same directory as last entry. */
11431 files[i].dir_idx = ndirs - 1;
11432 ++dirs[ndirs - 1].count;
11434 else
11436 int j;
11438 /* This is a new directory. */
11439 dirs[ndirs].path = files[i].path;
11440 dirs[ndirs].length = files[i].fname - files[i].path;
11441 dirs[ndirs].count = 1;
11442 dirs[ndirs].dir_idx = ndirs;
11443 files[i].dir_idx = ndirs;
11445 /* Search for a prefix. */
11446 dirs[ndirs].prefix = -1;
11447 for (j = 0; j < ndirs; j++)
11448 if (dirs[j].length < dirs[ndirs].length
11449 && dirs[j].length > 1
11450 && (dirs[ndirs].prefix == -1
11451 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11452 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11453 dirs[ndirs].prefix = j;
11455 ++ndirs;
11458 /* Now to the actual work. We have to find a subset of the directories which
11459 allow expressing the file name using references to the directory table
11460 with the least amount of characters. We do not do an exhaustive search
11461 where we would have to check out every combination of every single
11462 possible prefix. Instead we use a heuristic which provides nearly optimal
11463 results in most cases and never is much off. */
11464 saved = XALLOCAVEC (int, ndirs);
11465 savehere = XALLOCAVEC (int, ndirs);
11467 memset (saved, '\0', ndirs * sizeof (saved[0]));
11468 for (i = 0; i < ndirs; i++)
11470 int j;
11471 int total;
11473 /* We can always save some space for the current directory. But this
11474 does not mean it will be enough to justify adding the directory. */
11475 savehere[i] = dirs[i].length;
11476 total = (savehere[i] - saved[i]) * dirs[i].count;
11478 for (j = i + 1; j < ndirs; j++)
11480 savehere[j] = 0;
11481 if (saved[j] < dirs[i].length)
11483 /* Determine whether the dirs[i] path is a prefix of the
11484 dirs[j] path. */
11485 int k;
11487 k = dirs[j].prefix;
11488 while (k != -1 && k != (int) i)
11489 k = dirs[k].prefix;
11491 if (k == (int) i)
11493 /* Yes it is. We can possibly save some memory by
11494 writing the filenames in dirs[j] relative to
11495 dirs[i]. */
11496 savehere[j] = dirs[i].length;
11497 total += (savehere[j] - saved[j]) * dirs[j].count;
11502 /* Check whether we can save enough to justify adding the dirs[i]
11503 directory. */
11504 if (total > dirs[i].length + 1)
11506 /* It's worthwhile adding. */
11507 for (j = i; j < ndirs; j++)
11508 if (savehere[j] > 0)
11510 /* Remember how much we saved for this directory so far. */
11511 saved[j] = savehere[j];
11513 /* Remember the prefix directory. */
11514 dirs[j].dir_idx = i;
11519 /* Emit the directory name table. */
11520 idx_offset = dirs[0].length > 0 ? 1 : 0;
11521 enum dwarf_form str_form = DW_FORM_string;
11522 enum dwarf_form idx_form = DW_FORM_udata;
11523 if (dwarf_version >= 5)
11525 const char *comp_dir = comp_dir_string ();
11526 if (comp_dir == NULL)
11527 comp_dir = "";
11528 dw2_asm_output_data (1, 1, "Directory entry format count");
11529 if (DWARF5_USE_DEBUG_LINE_STR)
11530 str_form = DW_FORM_line_strp;
11531 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11532 dw2_asm_output_data_uleb128 (str_form, get_DW_FORM_name (str_form));
11533 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11534 if (str_form == DW_FORM_string)
11536 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11537 for (i = 1 - idx_offset; i < ndirs; i++)
11538 dw2_asm_output_nstring (dirs[i].path,
11539 dirs[i].length
11540 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11541 "Directory Entry: %#x", i + idx_offset);
11543 else
11545 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11546 for (i = 1 - idx_offset; i < ndirs; i++)
11548 const char *str
11549 = ggc_alloc_string (dirs[i].path,
11550 dirs[i].length
11551 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11552 output_line_string (str_form, str, "Directory Entry",
11553 (unsigned) i + idx_offset);
11557 else
11559 for (i = 1 - idx_offset; i < ndirs; i++)
11560 dw2_asm_output_nstring (dirs[i].path,
11561 dirs[i].length
11562 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11563 "Directory Entry: %#x", i + idx_offset);
11565 dw2_asm_output_data (1, 0, "End directory table");
11568 /* We have to emit them in the order of emitted_number since that's
11569 used in the debug info generation. To do this efficiently we
11570 generate a back-mapping of the indices first. */
11571 backmap = XALLOCAVEC (int, numfiles);
11572 for (i = 0; i < numfiles; i++)
11573 backmap[files[i].file_idx->emitted_number - 1] = i;
11575 if (dwarf_version >= 5)
11577 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11578 if (filename0 == NULL)
11579 filename0 = "";
11580 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11581 DW_FORM_data2. Choose one based on the number of directories
11582 and how much space would they occupy in each encoding.
11583 If we have at most 256 directories, all indexes fit into
11584 a single byte, so DW_FORM_data1 is most compact (if there
11585 are at most 128 directories, DW_FORM_udata would be as
11586 compact as that, but not shorter and slower to decode). */
11587 if (ndirs + idx_offset <= 256)
11588 idx_form = DW_FORM_data1;
11589 /* If there are more than 65536 directories, we have to use
11590 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11591 Otherwise, compute what space would occupy if all the indexes
11592 used DW_FORM_udata - sum - and compare that to how large would
11593 be DW_FORM_data2 encoding, and pick the more efficient one. */
11594 else if (ndirs + idx_offset <= 65536)
11596 unsigned HOST_WIDE_INT sum = 1;
11597 for (i = 0; i < numfiles; i++)
11599 int file_idx = backmap[i];
11600 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11601 sum += size_of_uleb128 (dir_idx);
11603 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11604 idx_form = DW_FORM_data2;
11606 #ifdef VMS_DEBUGGING_INFO
11607 dw2_asm_output_data (1, 4, "File name entry format count");
11608 #else
11609 dw2_asm_output_data (1, 2, "File name entry format count");
11610 #endif
11611 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11612 dw2_asm_output_data_uleb128 (str_form, get_DW_FORM_name (str_form));
11613 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11614 "DW_LNCT_directory_index");
11615 dw2_asm_output_data_uleb128 (idx_form, get_DW_FORM_name (idx_form));
11616 #ifdef VMS_DEBUGGING_INFO
11617 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11618 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11619 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11620 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11621 #endif
11622 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11624 output_line_string (str_form, filename0, "File Entry", 0);
11626 /* Include directory index. */
11627 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11628 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11629 0, NULL);
11630 else
11631 dw2_asm_output_data_uleb128 (0, NULL);
11633 #ifdef VMS_DEBUGGING_INFO
11634 dw2_asm_output_data_uleb128 (0, NULL);
11635 dw2_asm_output_data_uleb128 (0, NULL);
11636 #endif
11639 /* Now write all the file names. */
11640 for (i = 0; i < numfiles; i++)
11642 int file_idx = backmap[i];
11643 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11645 #ifdef VMS_DEBUGGING_INFO
11646 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11648 /* Setting these fields can lead to debugger miscomparisons,
11649 but VMS Debug requires them to be set correctly. */
11651 int ver;
11652 long long cdt;
11653 long siz;
11654 int maxfilelen = (strlen (files[file_idx].path)
11655 + dirs[dir_idx].length
11656 + MAX_VMS_VERSION_LEN + 1);
11657 char *filebuf = XALLOCAVEC (char, maxfilelen);
11659 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11660 snprintf (filebuf, maxfilelen, "%s;%d",
11661 files[file_idx].path + dirs[dir_idx].length, ver);
11663 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11665 /* Include directory index. */
11666 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11667 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11668 dir_idx + idx_offset, NULL);
11669 else
11670 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11672 /* Modification time. */
11673 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11674 &cdt, 0, 0, 0) == 0)
11675 ? cdt : 0, NULL);
11677 /* File length in bytes. */
11678 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11679 0, &siz, 0, 0) == 0)
11680 ? siz : 0, NULL);
11681 #else
11682 output_line_string (str_form,
11683 files[file_idx].path + dirs[dir_idx].length,
11684 "File Entry", (unsigned) i + 1);
11686 /* Include directory index. */
11687 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11688 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11689 dir_idx + idx_offset, NULL);
11690 else
11691 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11693 if (dwarf_version >= 5)
11694 continue;
11696 /* Modification time. */
11697 dw2_asm_output_data_uleb128 (0, NULL);
11699 /* File length in bytes. */
11700 dw2_asm_output_data_uleb128 (0, NULL);
11701 #endif /* VMS_DEBUGGING_INFO */
11704 if (dwarf_version < 5)
11705 dw2_asm_output_data (1, 0, "End file name table");
11709 /* Output one line number table into the .debug_line section. */
11711 static void
11712 output_one_line_info_table (dw_line_info_table *table)
11714 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11715 unsigned int current_line = 1;
11716 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11717 dw_line_info_entry *ent;
11718 size_t i;
11720 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11722 switch (ent->opcode)
11724 case LI_set_address:
11725 /* ??? Unfortunately, we have little choice here currently, and
11726 must always use the most general form. GCC does not know the
11727 address delta itself, so we can't use DW_LNS_advance_pc. Many
11728 ports do have length attributes which will give an upper bound
11729 on the address range. We could perhaps use length attributes
11730 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11731 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11733 /* This can handle any delta. This takes
11734 4+DWARF2_ADDR_SIZE bytes. */
11735 dw2_asm_output_data (1, 0, "set address %s", line_label);
11736 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11737 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11738 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11739 break;
11741 case LI_set_line:
11742 if (ent->val == current_line)
11744 /* We still need to start a new row, so output a copy insn. */
11745 dw2_asm_output_data (1, DW_LNS_copy,
11746 "copy line %u", current_line);
11748 else
11750 int line_offset = ent->val - current_line;
11751 int line_delta = line_offset - DWARF_LINE_BASE;
11753 current_line = ent->val;
11754 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11756 /* This can handle deltas from -10 to 234, using the current
11757 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11758 This takes 1 byte. */
11759 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11760 "line %u", current_line);
11762 else
11764 /* This can handle any delta. This takes at least 4 bytes,
11765 depending on the value being encoded. */
11766 dw2_asm_output_data (1, DW_LNS_advance_line,
11767 "advance to line %u", current_line);
11768 dw2_asm_output_data_sleb128 (line_offset, NULL);
11769 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11772 break;
11774 case LI_set_file:
11775 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11776 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11777 break;
11779 case LI_set_column:
11780 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11781 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11782 break;
11784 case LI_negate_stmt:
11785 current_is_stmt = !current_is_stmt;
11786 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11787 "is_stmt %d", current_is_stmt);
11788 break;
11790 case LI_set_prologue_end:
11791 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11792 "set prologue end");
11793 break;
11795 case LI_set_epilogue_begin:
11796 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11797 "set epilogue begin");
11798 break;
11800 case LI_set_discriminator:
11801 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11802 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11803 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11804 dw2_asm_output_data_uleb128 (ent->val, NULL);
11805 break;
11809 /* Emit debug info for the address of the end of the table. */
11810 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11811 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11812 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11813 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11815 dw2_asm_output_data (1, 0, "end sequence");
11816 dw2_asm_output_data_uleb128 (1, NULL);
11817 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11820 /* Output the source line number correspondence information. This
11821 information goes into the .debug_line section. */
11823 static void
11824 output_line_info (bool prologue_only)
11826 static unsigned int generation;
11827 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11828 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11829 bool saw_one = false;
11830 int opc;
11832 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11833 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11834 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11835 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11837 if (!XCOFF_DEBUGGING_INFO)
11839 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11840 dw2_asm_output_data (4, 0xffffffff,
11841 "Initial length escape value indicating 64-bit DWARF extension");
11842 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11843 "Length of Source Line Info");
11846 ASM_OUTPUT_LABEL (asm_out_file, l1);
11848 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11849 if (dwarf_version >= 5)
11851 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11852 dw2_asm_output_data (1, 0, "Segment Size");
11854 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11855 ASM_OUTPUT_LABEL (asm_out_file, p1);
11857 /* Define the architecture-dependent minimum instruction length (in bytes).
11858 In this implementation of DWARF, this field is used for information
11859 purposes only. Since GCC generates assembly language, we have no
11860 a priori knowledge of how many instruction bytes are generated for each
11861 source line, and therefore can use only the DW_LNE_set_address and
11862 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11863 this as '1', which is "correct enough" for all architectures,
11864 and don't let the target override. */
11865 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11867 if (dwarf_version >= 4)
11868 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11869 "Maximum Operations Per Instruction");
11870 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11871 "Default is_stmt_start flag");
11872 dw2_asm_output_data (1, DWARF_LINE_BASE,
11873 "Line Base Value (Special Opcodes)");
11874 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11875 "Line Range Value (Special Opcodes)");
11876 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11877 "Special Opcode Base");
11879 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11881 int n_op_args;
11882 switch (opc)
11884 case DW_LNS_advance_pc:
11885 case DW_LNS_advance_line:
11886 case DW_LNS_set_file:
11887 case DW_LNS_set_column:
11888 case DW_LNS_fixed_advance_pc:
11889 case DW_LNS_set_isa:
11890 n_op_args = 1;
11891 break;
11892 default:
11893 n_op_args = 0;
11894 break;
11897 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11898 opc, n_op_args);
11901 /* Write out the information about the files we use. */
11902 output_file_names ();
11903 ASM_OUTPUT_LABEL (asm_out_file, p2);
11904 if (prologue_only)
11906 /* Output the marker for the end of the line number info. */
11907 ASM_OUTPUT_LABEL (asm_out_file, l2);
11908 return;
11911 if (separate_line_info)
11913 dw_line_info_table *table;
11914 size_t i;
11916 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11917 if (table->in_use)
11919 output_one_line_info_table (table);
11920 saw_one = true;
11923 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
11925 output_one_line_info_table (cold_text_section_line_info);
11926 saw_one = true;
11929 /* ??? Some Darwin linkers crash on a .debug_line section with no
11930 sequences. Further, merely a DW_LNE_end_sequence entry is not
11931 sufficient -- the address column must also be initialized.
11932 Make sure to output at least one set_address/end_sequence pair,
11933 choosing .text since that section is always present. */
11934 if (text_section_line_info->in_use || !saw_one)
11935 output_one_line_info_table (text_section_line_info);
11937 /* Output the marker for the end of the line number info. */
11938 ASM_OUTPUT_LABEL (asm_out_file, l2);
11941 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
11943 static inline bool
11944 need_endianity_attribute_p (bool reverse)
11946 return reverse && (dwarf_version >= 3 || !dwarf_strict);
11949 /* Given a pointer to a tree node for some base type, return a pointer to
11950 a DIE that describes the given type. REVERSE is true if the type is
11951 to be interpreted in the reverse storage order wrt the target order.
11953 This routine must only be called for GCC type nodes that correspond to
11954 Dwarf base (fundamental) types. */
11956 static dw_die_ref
11957 base_type_die (tree type, bool reverse)
11959 dw_die_ref base_type_result;
11960 enum dwarf_type encoding;
11961 bool fpt_used = false;
11962 struct fixed_point_type_info fpt_info;
11963 tree type_bias = NULL_TREE;
11965 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11966 return 0;
11968 /* If this is a subtype that should not be emitted as a subrange type,
11969 use the base type. See subrange_type_for_debug_p. */
11970 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11971 type = TREE_TYPE (type);
11973 switch (TREE_CODE (type))
11975 case INTEGER_TYPE:
11976 if ((dwarf_version >= 4 || !dwarf_strict)
11977 && TYPE_NAME (type)
11978 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11979 && DECL_IS_BUILTIN (TYPE_NAME (type))
11980 && DECL_NAME (TYPE_NAME (type)))
11982 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
11983 if (strcmp (name, "char16_t") == 0
11984 || strcmp (name, "char32_t") == 0)
11986 encoding = DW_ATE_UTF;
11987 break;
11990 if ((dwarf_version >= 3 || !dwarf_strict)
11991 && lang_hooks.types.get_fixed_point_type_info)
11993 memset (&fpt_info, 0, sizeof (fpt_info));
11994 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
11996 fpt_used = true;
11997 encoding = ((TYPE_UNSIGNED (type))
11998 ? DW_ATE_unsigned_fixed
11999 : DW_ATE_signed_fixed);
12000 break;
12003 if (TYPE_STRING_FLAG (type))
12005 if (TYPE_UNSIGNED (type))
12006 encoding = DW_ATE_unsigned_char;
12007 else
12008 encoding = DW_ATE_signed_char;
12010 else if (TYPE_UNSIGNED (type))
12011 encoding = DW_ATE_unsigned;
12012 else
12013 encoding = DW_ATE_signed;
12015 if (!dwarf_strict
12016 && lang_hooks.types.get_type_bias)
12017 type_bias = lang_hooks.types.get_type_bias (type);
12018 break;
12020 case REAL_TYPE:
12021 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12023 if (dwarf_version >= 3 || !dwarf_strict)
12024 encoding = DW_ATE_decimal_float;
12025 else
12026 encoding = DW_ATE_lo_user;
12028 else
12029 encoding = DW_ATE_float;
12030 break;
12032 case FIXED_POINT_TYPE:
12033 if (!(dwarf_version >= 3 || !dwarf_strict))
12034 encoding = DW_ATE_lo_user;
12035 else if (TYPE_UNSIGNED (type))
12036 encoding = DW_ATE_unsigned_fixed;
12037 else
12038 encoding = DW_ATE_signed_fixed;
12039 break;
12041 /* Dwarf2 doesn't know anything about complex ints, so use
12042 a user defined type for it. */
12043 case COMPLEX_TYPE:
12044 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12045 encoding = DW_ATE_complex_float;
12046 else
12047 encoding = DW_ATE_lo_user;
12048 break;
12050 case BOOLEAN_TYPE:
12051 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12052 encoding = DW_ATE_boolean;
12053 break;
12055 default:
12056 /* No other TREE_CODEs are Dwarf fundamental types. */
12057 gcc_unreachable ();
12060 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12062 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12063 int_size_in_bytes (type));
12064 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12066 if (need_endianity_attribute_p (reverse))
12067 add_AT_unsigned (base_type_result, DW_AT_endianity,
12068 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12070 if (fpt_used)
12072 switch (fpt_info.scale_factor_kind)
12074 case fixed_point_scale_factor_binary:
12075 add_AT_int (base_type_result, DW_AT_binary_scale,
12076 fpt_info.scale_factor.binary);
12077 break;
12079 case fixed_point_scale_factor_decimal:
12080 add_AT_int (base_type_result, DW_AT_decimal_scale,
12081 fpt_info.scale_factor.decimal);
12082 break;
12084 case fixed_point_scale_factor_arbitrary:
12085 /* Arbitrary scale factors cannot be described in standard DWARF,
12086 yet. */
12087 if (!dwarf_strict)
12089 /* Describe the scale factor as a rational constant. */
12090 const dw_die_ref scale_factor
12091 = new_die (DW_TAG_constant, comp_unit_die (), type);
12093 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12094 fpt_info.scale_factor.arbitrary.numerator);
12095 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12096 fpt_info.scale_factor.arbitrary.denominator);
12098 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12100 break;
12102 default:
12103 gcc_unreachable ();
12107 if (type_bias)
12108 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12109 dw_scalar_form_constant
12110 | dw_scalar_form_exprloc
12111 | dw_scalar_form_reference,
12112 NULL);
12114 add_pubtype (type, base_type_result);
12116 return base_type_result;
12119 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12120 named 'auto' in its type: return true for it, false otherwise. */
12122 static inline bool
12123 is_cxx_auto (tree type)
12125 if (is_cxx ())
12127 tree name = TYPE_IDENTIFIER (type);
12128 if (name == get_identifier ("auto")
12129 || name == get_identifier ("decltype(auto)"))
12130 return true;
12132 return false;
12135 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12136 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12138 static inline int
12139 is_base_type (tree type)
12141 switch (TREE_CODE (type))
12143 case ERROR_MARK:
12144 case VOID_TYPE:
12145 case INTEGER_TYPE:
12146 case REAL_TYPE:
12147 case FIXED_POINT_TYPE:
12148 case COMPLEX_TYPE:
12149 case BOOLEAN_TYPE:
12150 case POINTER_BOUNDS_TYPE:
12151 return 1;
12153 case ARRAY_TYPE:
12154 case RECORD_TYPE:
12155 case UNION_TYPE:
12156 case QUAL_UNION_TYPE:
12157 case ENUMERAL_TYPE:
12158 case FUNCTION_TYPE:
12159 case METHOD_TYPE:
12160 case POINTER_TYPE:
12161 case REFERENCE_TYPE:
12162 case NULLPTR_TYPE:
12163 case OFFSET_TYPE:
12164 case LANG_TYPE:
12165 case VECTOR_TYPE:
12166 return 0;
12168 default:
12169 if (is_cxx_auto (type))
12170 return 0;
12171 gcc_unreachable ();
12174 return 0;
12177 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12178 node, return the size in bits for the type if it is a constant, or else
12179 return the alignment for the type if the type's size is not constant, or
12180 else return BITS_PER_WORD if the type actually turns out to be an
12181 ERROR_MARK node. */
12183 static inline unsigned HOST_WIDE_INT
12184 simple_type_size_in_bits (const_tree type)
12186 if (TREE_CODE (type) == ERROR_MARK)
12187 return BITS_PER_WORD;
12188 else if (TYPE_SIZE (type) == NULL_TREE)
12189 return 0;
12190 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12191 return tree_to_uhwi (TYPE_SIZE (type));
12192 else
12193 return TYPE_ALIGN (type);
12196 /* Similarly, but return an offset_int instead of UHWI. */
12198 static inline offset_int
12199 offset_int_type_size_in_bits (const_tree type)
12201 if (TREE_CODE (type) == ERROR_MARK)
12202 return BITS_PER_WORD;
12203 else if (TYPE_SIZE (type) == NULL_TREE)
12204 return 0;
12205 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12206 return wi::to_offset (TYPE_SIZE (type));
12207 else
12208 return TYPE_ALIGN (type);
12211 /* Given a pointer to a tree node for a subrange type, return a pointer
12212 to a DIE that describes the given type. */
12214 static dw_die_ref
12215 subrange_type_die (tree type, tree low, tree high, tree bias,
12216 dw_die_ref context_die)
12218 dw_die_ref subrange_die;
12219 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12221 if (context_die == NULL)
12222 context_die = comp_unit_die ();
12224 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12226 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12228 /* The size of the subrange type and its base type do not match,
12229 so we need to generate a size attribute for the subrange type. */
12230 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12233 if (low)
12234 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12235 if (high)
12236 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12237 if (bias && !dwarf_strict)
12238 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12239 dw_scalar_form_constant
12240 | dw_scalar_form_exprloc
12241 | dw_scalar_form_reference,
12242 NULL);
12244 return subrange_die;
12247 /* Returns the (const and/or volatile) cv_qualifiers associated with
12248 the decl node. This will normally be augmented with the
12249 cv_qualifiers of the underlying type in add_type_attribute. */
12251 static int
12252 decl_quals (const_tree decl)
12254 return ((TREE_READONLY (decl)
12255 /* The C++ front-end correctly marks reference-typed
12256 variables as readonly, but from a language (and debug
12257 info) standpoint they are not const-qualified. */
12258 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12259 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12260 | (TREE_THIS_VOLATILE (decl)
12261 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12264 /* Determine the TYPE whose qualifiers match the largest strict subset
12265 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12266 qualifiers outside QUAL_MASK. */
12268 static int
12269 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12271 tree t;
12272 int best_rank = 0, best_qual = 0, max_rank;
12274 type_quals &= qual_mask;
12275 max_rank = popcount_hwi (type_quals) - 1;
12277 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12278 t = TYPE_NEXT_VARIANT (t))
12280 int q = TYPE_QUALS (t) & qual_mask;
12282 if ((q & type_quals) == q && q != type_quals
12283 && check_base_type (t, type))
12285 int rank = popcount_hwi (q);
12287 if (rank > best_rank)
12289 best_rank = rank;
12290 best_qual = q;
12295 return best_qual;
12298 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12299 static const dwarf_qual_info_t dwarf_qual_info[] =
12301 { TYPE_QUAL_CONST, DW_TAG_const_type },
12302 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12303 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12304 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12306 static const unsigned int dwarf_qual_info_size
12307 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12309 /* If DIE is a qualified DIE of some base DIE with the same parent,
12310 return the base DIE, otherwise return NULL. Set MASK to the
12311 qualifiers added compared to the returned DIE. */
12313 static dw_die_ref
12314 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12316 unsigned int i;
12317 for (i = 0; i < dwarf_qual_info_size; i++)
12318 if (die->die_tag == dwarf_qual_info[i].t)
12319 break;
12320 if (i == dwarf_qual_info_size)
12321 return NULL;
12322 if (vec_safe_length (die->die_attr) != 1)
12323 return NULL;
12324 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12325 if (type == NULL || type->die_parent != die->die_parent)
12326 return NULL;
12327 *mask |= dwarf_qual_info[i].q;
12328 if (depth)
12330 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12331 if (ret)
12332 return ret;
12334 return type;
12337 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12338 entry that chains the modifiers specified by CV_QUALS in front of the
12339 given type. REVERSE is true if the type is to be interpreted in the
12340 reverse storage order wrt the target order. */
12342 static dw_die_ref
12343 modified_type_die (tree type, int cv_quals, bool reverse,
12344 dw_die_ref context_die)
12346 enum tree_code code = TREE_CODE (type);
12347 dw_die_ref mod_type_die;
12348 dw_die_ref sub_die = NULL;
12349 tree item_type = NULL;
12350 tree qualified_type;
12351 tree name, low, high;
12352 dw_die_ref mod_scope;
12353 /* Only these cv-qualifiers are currently handled. */
12354 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12355 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12357 if (code == ERROR_MARK)
12358 return NULL;
12360 if (lang_hooks.types.get_debug_type)
12362 tree debug_type = lang_hooks.types.get_debug_type (type);
12364 if (debug_type != NULL_TREE && debug_type != type)
12365 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12368 cv_quals &= cv_qual_mask;
12370 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12371 tag modifier (and not an attribute) old consumers won't be able
12372 to handle it. */
12373 if (dwarf_version < 3)
12374 cv_quals &= ~TYPE_QUAL_RESTRICT;
12376 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12377 if (dwarf_version < 5)
12378 cv_quals &= ~TYPE_QUAL_ATOMIC;
12380 /* See if we already have the appropriately qualified variant of
12381 this type. */
12382 qualified_type = get_qualified_type (type, cv_quals);
12384 if (qualified_type == sizetype
12385 && TYPE_NAME (qualified_type)
12386 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12388 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12390 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12391 && TYPE_PRECISION (t)
12392 == TYPE_PRECISION (qualified_type)
12393 && TYPE_UNSIGNED (t)
12394 == TYPE_UNSIGNED (qualified_type));
12395 qualified_type = t;
12398 /* If we do, then we can just use its DIE, if it exists. */
12399 if (qualified_type)
12401 mod_type_die = lookup_type_die (qualified_type);
12403 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12404 if (mod_type_die
12405 && (!need_endianity_attribute_p (reverse)
12406 || !is_base_type (type)
12407 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12408 return mod_type_die;
12411 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12413 /* Handle C typedef types. */
12414 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12415 && !DECL_ARTIFICIAL (name))
12417 tree dtype = TREE_TYPE (name);
12419 if (qualified_type == dtype)
12421 /* For a named type, use the typedef. */
12422 gen_type_die (qualified_type, context_die);
12423 return lookup_type_die (qualified_type);
12425 else
12427 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12428 dquals &= cv_qual_mask;
12429 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12430 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12431 /* cv-unqualified version of named type. Just use
12432 the unnamed type to which it refers. */
12433 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12434 reverse, context_die);
12435 /* Else cv-qualified version of named type; fall through. */
12439 mod_scope = scope_die_for (type, context_die);
12441 if (cv_quals)
12443 int sub_quals = 0, first_quals = 0;
12444 unsigned i;
12445 dw_die_ref first = NULL, last = NULL;
12447 /* Determine a lesser qualified type that most closely matches
12448 this one. Then generate DW_TAG_* entries for the remaining
12449 qualifiers. */
12450 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12451 cv_qual_mask);
12452 if (sub_quals && use_debug_types)
12454 bool needed = false;
12455 /* If emitting type units, make sure the order of qualifiers
12456 is canonical. Thus, start from unqualified type if
12457 an earlier qualifier is missing in sub_quals, but some later
12458 one is present there. */
12459 for (i = 0; i < dwarf_qual_info_size; i++)
12460 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12461 needed = true;
12462 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12464 sub_quals = 0;
12465 break;
12468 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12469 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12471 /* As not all intermediate qualified DIEs have corresponding
12472 tree types, ensure that qualified DIEs in the same scope
12473 as their DW_AT_type are emitted after their DW_AT_type,
12474 only with other qualified DIEs for the same type possibly
12475 in between them. Determine the range of such qualified
12476 DIEs now (first being the base type, last being corresponding
12477 last qualified DIE for it). */
12478 unsigned int count = 0;
12479 first = qualified_die_p (mod_type_die, &first_quals,
12480 dwarf_qual_info_size);
12481 if (first == NULL)
12482 first = mod_type_die;
12483 gcc_assert ((first_quals & ~sub_quals) == 0);
12484 for (count = 0, last = first;
12485 count < (1U << dwarf_qual_info_size);
12486 count++, last = last->die_sib)
12488 int quals = 0;
12489 if (last == mod_scope->die_child)
12490 break;
12491 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12492 != first)
12493 break;
12497 for (i = 0; i < dwarf_qual_info_size; i++)
12498 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12500 dw_die_ref d;
12501 if (first && first != last)
12503 for (d = first->die_sib; ; d = d->die_sib)
12505 int quals = 0;
12506 qualified_die_p (d, &quals, dwarf_qual_info_size);
12507 if (quals == (first_quals | dwarf_qual_info[i].q))
12508 break;
12509 if (d == last)
12511 d = NULL;
12512 break;
12515 if (d)
12517 mod_type_die = d;
12518 continue;
12521 if (first)
12523 d = ggc_cleared_alloc<die_node> ();
12524 d->die_tag = dwarf_qual_info[i].t;
12525 add_child_die_after (mod_scope, d, last);
12526 last = d;
12528 else
12529 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12530 if (mod_type_die)
12531 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12532 mod_type_die = d;
12533 first_quals |= dwarf_qual_info[i].q;
12536 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12538 dwarf_tag tag = DW_TAG_pointer_type;
12539 if (code == REFERENCE_TYPE)
12541 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12542 tag = DW_TAG_rvalue_reference_type;
12543 else
12544 tag = DW_TAG_reference_type;
12546 mod_type_die = new_die (tag, mod_scope, type);
12548 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12549 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12550 item_type = TREE_TYPE (type);
12552 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12553 if (!ADDR_SPACE_GENERIC_P (as))
12555 int action = targetm.addr_space.debug (as);
12556 if (action >= 0)
12558 /* Positive values indicate an address_class. */
12559 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12561 else
12563 /* Negative values indicate an (inverted) segment base reg. */
12564 dw_loc_descr_ref d
12565 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12566 add_AT_loc (mod_type_die, DW_AT_segment, d);
12570 else if (code == INTEGER_TYPE
12571 && TREE_TYPE (type) != NULL_TREE
12572 && subrange_type_for_debug_p (type, &low, &high))
12574 tree bias = NULL_TREE;
12575 if (lang_hooks.types.get_type_bias)
12576 bias = lang_hooks.types.get_type_bias (type);
12577 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12578 item_type = TREE_TYPE (type);
12580 else if (is_base_type (type))
12581 mod_type_die = base_type_die (type, reverse);
12582 else
12584 gen_type_die (type, context_die);
12586 /* We have to get the type_main_variant here (and pass that to the
12587 `lookup_type_die' routine) because the ..._TYPE node we have
12588 might simply be a *copy* of some original type node (where the
12589 copy was created to help us keep track of typedef names) and
12590 that copy might have a different TYPE_UID from the original
12591 ..._TYPE node. */
12592 if (TREE_CODE (type) == FUNCTION_TYPE
12593 || TREE_CODE (type) == METHOD_TYPE)
12595 /* For function/method types, can't just use type_main_variant here,
12596 because that can have different ref-qualifiers for C++,
12597 but try to canonicalize. */
12598 tree main = TYPE_MAIN_VARIANT (type);
12599 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12600 if (check_base_type (t, main) && check_lang_type (t, type))
12601 return lookup_type_die (t);
12602 return lookup_type_die (type);
12604 else if (TREE_CODE (type) != VECTOR_TYPE
12605 && TREE_CODE (type) != ARRAY_TYPE)
12606 return lookup_type_die (type_main_variant (type));
12607 else
12608 /* Vectors have the debugging information in the type,
12609 not the main variant. */
12610 return lookup_type_die (type);
12613 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12614 don't output a DW_TAG_typedef, since there isn't one in the
12615 user's program; just attach a DW_AT_name to the type.
12616 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12617 if the base type already has the same name. */
12618 if (name
12619 && ((TREE_CODE (name) != TYPE_DECL
12620 && (qualified_type == TYPE_MAIN_VARIANT (type)
12621 || (cv_quals == TYPE_UNQUALIFIED)))
12622 || (TREE_CODE (name) == TYPE_DECL
12623 && TREE_TYPE (name) == qualified_type
12624 && DECL_NAME (name))))
12626 if (TREE_CODE (name) == TYPE_DECL)
12627 /* Could just call add_name_and_src_coords_attributes here,
12628 but since this is a builtin type it doesn't have any
12629 useful source coordinates anyway. */
12630 name = DECL_NAME (name);
12631 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12633 /* This probably indicates a bug. */
12634 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12636 name = TYPE_IDENTIFIER (type);
12637 add_name_attribute (mod_type_die,
12638 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12641 if (qualified_type)
12642 equate_type_number_to_die (qualified_type, mod_type_die);
12644 if (item_type)
12645 /* We must do this after the equate_type_number_to_die call, in case
12646 this is a recursive type. This ensures that the modified_type_die
12647 recursion will terminate even if the type is recursive. Recursive
12648 types are possible in Ada. */
12649 sub_die = modified_type_die (item_type,
12650 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12651 reverse,
12652 context_die);
12654 if (sub_die != NULL)
12655 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12657 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12658 if (TYPE_ARTIFICIAL (type))
12659 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12661 return mod_type_die;
12664 /* Generate DIEs for the generic parameters of T.
12665 T must be either a generic type or a generic function.
12666 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12668 static void
12669 gen_generic_params_dies (tree t)
12671 tree parms, args;
12672 int parms_num, i;
12673 dw_die_ref die = NULL;
12674 int non_default;
12676 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12677 return;
12679 if (TYPE_P (t))
12680 die = lookup_type_die (t);
12681 else if (DECL_P (t))
12682 die = lookup_decl_die (t);
12684 gcc_assert (die);
12686 parms = lang_hooks.get_innermost_generic_parms (t);
12687 if (!parms)
12688 /* T has no generic parameter. It means T is neither a generic type
12689 or function. End of story. */
12690 return;
12692 parms_num = TREE_VEC_LENGTH (parms);
12693 args = lang_hooks.get_innermost_generic_args (t);
12694 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12695 non_default = int_cst_value (TREE_CHAIN (args));
12696 else
12697 non_default = TREE_VEC_LENGTH (args);
12698 for (i = 0; i < parms_num; i++)
12700 tree parm, arg, arg_pack_elems;
12701 dw_die_ref parm_die;
12703 parm = TREE_VEC_ELT (parms, i);
12704 arg = TREE_VEC_ELT (args, i);
12705 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12706 gcc_assert (parm && TREE_VALUE (parm) && arg);
12708 if (parm && TREE_VALUE (parm) && arg)
12710 /* If PARM represents a template parameter pack,
12711 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12712 by DW_TAG_template_*_parameter DIEs for the argument
12713 pack elements of ARG. Note that ARG would then be
12714 an argument pack. */
12715 if (arg_pack_elems)
12716 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12717 arg_pack_elems,
12718 die);
12719 else
12720 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12721 true /* emit name */, die);
12722 if (i >= non_default)
12723 add_AT_flag (parm_die, DW_AT_default_value, 1);
12728 /* Create and return a DIE for PARM which should be
12729 the representation of a generic type parameter.
12730 For instance, in the C++ front end, PARM would be a template parameter.
12731 ARG is the argument to PARM.
12732 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12733 name of the PARM.
12734 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12735 as a child node. */
12737 static dw_die_ref
12738 generic_parameter_die (tree parm, tree arg,
12739 bool emit_name_p,
12740 dw_die_ref parent_die)
12742 dw_die_ref tmpl_die = NULL;
12743 const char *name = NULL;
12745 if (!parm || !DECL_NAME (parm) || !arg)
12746 return NULL;
12748 /* We support non-type generic parameters and arguments,
12749 type generic parameters and arguments, as well as
12750 generic generic parameters (a.k.a. template template parameters in C++)
12751 and arguments. */
12752 if (TREE_CODE (parm) == PARM_DECL)
12753 /* PARM is a nontype generic parameter */
12754 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12755 else if (TREE_CODE (parm) == TYPE_DECL)
12756 /* PARM is a type generic parameter. */
12757 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12758 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12759 /* PARM is a generic generic parameter.
12760 Its DIE is a GNU extension. It shall have a
12761 DW_AT_name attribute to represent the name of the template template
12762 parameter, and a DW_AT_GNU_template_name attribute to represent the
12763 name of the template template argument. */
12764 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12765 parent_die, parm);
12766 else
12767 gcc_unreachable ();
12769 if (tmpl_die)
12771 tree tmpl_type;
12773 /* If PARM is a generic parameter pack, it means we are
12774 emitting debug info for a template argument pack element.
12775 In other terms, ARG is a template argument pack element.
12776 In that case, we don't emit any DW_AT_name attribute for
12777 the die. */
12778 if (emit_name_p)
12780 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12781 gcc_assert (name);
12782 add_AT_string (tmpl_die, DW_AT_name, name);
12785 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12787 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12788 TMPL_DIE should have a child DW_AT_type attribute that is set
12789 to the type of the argument to PARM, which is ARG.
12790 If PARM is a type generic parameter, TMPL_DIE should have a
12791 child DW_AT_type that is set to ARG. */
12792 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12793 add_type_attribute (tmpl_die, tmpl_type,
12794 (TREE_THIS_VOLATILE (tmpl_type)
12795 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12796 false, parent_die);
12798 else
12800 /* So TMPL_DIE is a DIE representing a
12801 a generic generic template parameter, a.k.a template template
12802 parameter in C++ and arg is a template. */
12804 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12805 to the name of the argument. */
12806 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12807 if (name)
12808 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12811 if (TREE_CODE (parm) == PARM_DECL)
12812 /* So PARM is a non-type generic parameter.
12813 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12814 attribute of TMPL_DIE which value represents the value
12815 of ARG.
12816 We must be careful here:
12817 The value of ARG might reference some function decls.
12818 We might currently be emitting debug info for a generic
12819 type and types are emitted before function decls, we don't
12820 know if the function decls referenced by ARG will actually be
12821 emitted after cgraph computations.
12822 So must defer the generation of the DW_AT_const_value to
12823 after cgraph is ready. */
12824 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12827 return tmpl_die;
12830 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12831 PARM_PACK must be a template parameter pack. The returned DIE
12832 will be child DIE of PARENT_DIE. */
12834 static dw_die_ref
12835 template_parameter_pack_die (tree parm_pack,
12836 tree parm_pack_args,
12837 dw_die_ref parent_die)
12839 dw_die_ref die;
12840 int j;
12842 gcc_assert (parent_die && parm_pack);
12844 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12845 add_name_and_src_coords_attributes (die, parm_pack);
12846 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12847 generic_parameter_die (parm_pack,
12848 TREE_VEC_ELT (parm_pack_args, j),
12849 false /* Don't emit DW_AT_name */,
12850 die);
12851 return die;
12854 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12855 an enumerated type. */
12857 static inline int
12858 type_is_enum (const_tree type)
12860 return TREE_CODE (type) == ENUMERAL_TYPE;
12863 /* Return the DBX register number described by a given RTL node. */
12865 static unsigned int
12866 dbx_reg_number (const_rtx rtl)
12868 unsigned regno = REGNO (rtl);
12870 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12872 #ifdef LEAF_REG_REMAP
12873 if (crtl->uses_only_leaf_regs)
12875 int leaf_reg = LEAF_REG_REMAP (regno);
12876 if (leaf_reg != -1)
12877 regno = (unsigned) leaf_reg;
12879 #endif
12881 regno = DBX_REGISTER_NUMBER (regno);
12882 gcc_assert (regno != INVALID_REGNUM);
12883 return regno;
12886 /* Optionally add a DW_OP_piece term to a location description expression.
12887 DW_OP_piece is only added if the location description expression already
12888 doesn't end with DW_OP_piece. */
12890 static void
12891 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12893 dw_loc_descr_ref loc;
12895 if (*list_head != NULL)
12897 /* Find the end of the chain. */
12898 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12901 if (loc->dw_loc_opc != DW_OP_piece)
12902 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12906 /* Return a location descriptor that designates a machine register or
12907 zero if there is none. */
12909 static dw_loc_descr_ref
12910 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12912 rtx regs;
12914 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12915 return 0;
12917 /* We only use "frame base" when we're sure we're talking about the
12918 post-prologue local stack frame. We do this by *not* running
12919 register elimination until this point, and recognizing the special
12920 argument pointer and soft frame pointer rtx's.
12921 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
12922 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
12923 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
12925 dw_loc_descr_ref result = NULL;
12927 if (dwarf_version >= 4 || !dwarf_strict)
12929 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
12930 initialized);
12931 if (result)
12932 add_loc_descr (&result,
12933 new_loc_descr (DW_OP_stack_value, 0, 0));
12935 return result;
12938 regs = targetm.dwarf_register_span (rtl);
12940 if (REG_NREGS (rtl) > 1 || regs)
12941 return multiple_reg_loc_descriptor (rtl, regs, initialized);
12942 else
12944 unsigned int dbx_regnum = dbx_reg_number (rtl);
12945 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12946 return 0;
12947 return one_reg_loc_descriptor (dbx_regnum, initialized);
12951 /* Return a location descriptor that designates a machine register for
12952 a given hard register number. */
12954 static dw_loc_descr_ref
12955 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12957 dw_loc_descr_ref reg_loc_descr;
12959 if (regno <= 31)
12960 reg_loc_descr
12961 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12962 else
12963 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12965 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12966 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12968 return reg_loc_descr;
12971 /* Given an RTL of a register, return a location descriptor that
12972 designates a value that spans more than one register. */
12974 static dw_loc_descr_ref
12975 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
12976 enum var_init_status initialized)
12978 int size, i;
12979 dw_loc_descr_ref loc_result = NULL;
12981 /* Simple, contiguous registers. */
12982 if (regs == NULL_RTX)
12984 unsigned reg = REGNO (rtl);
12985 int nregs;
12987 #ifdef LEAF_REG_REMAP
12988 if (crtl->uses_only_leaf_regs)
12990 int leaf_reg = LEAF_REG_REMAP (reg);
12991 if (leaf_reg != -1)
12992 reg = (unsigned) leaf_reg;
12994 #endif
12996 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
12997 nregs = REG_NREGS (rtl);
12999 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13001 loc_result = NULL;
13002 while (nregs--)
13004 dw_loc_descr_ref t;
13006 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13007 VAR_INIT_STATUS_INITIALIZED);
13008 add_loc_descr (&loc_result, t);
13009 add_loc_descr_op_piece (&loc_result, size);
13010 ++reg;
13012 return loc_result;
13015 /* Now onto stupid register sets in non contiguous locations. */
13017 gcc_assert (GET_CODE (regs) == PARALLEL);
13019 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13020 loc_result = NULL;
13022 for (i = 0; i < XVECLEN (regs, 0); ++i)
13024 dw_loc_descr_ref t;
13026 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13027 VAR_INIT_STATUS_INITIALIZED);
13028 add_loc_descr (&loc_result, t);
13029 add_loc_descr_op_piece (&loc_result, size);
13032 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13033 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13034 return loc_result;
13037 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13039 /* Return a location descriptor that designates a constant i,
13040 as a compound operation from constant (i >> shift), constant shift
13041 and DW_OP_shl. */
13043 static dw_loc_descr_ref
13044 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13046 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13047 add_loc_descr (&ret, int_loc_descriptor (shift));
13048 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13049 return ret;
13052 /* Return a location descriptor that designates a constant. */
13054 static dw_loc_descr_ref
13055 int_loc_descriptor (HOST_WIDE_INT i)
13057 enum dwarf_location_atom op;
13059 /* Pick the smallest representation of a constant, rather than just
13060 defaulting to the LEB encoding. */
13061 if (i >= 0)
13063 int clz = clz_hwi (i);
13064 int ctz = ctz_hwi (i);
13065 if (i <= 31)
13066 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13067 else if (i <= 0xff)
13068 op = DW_OP_const1u;
13069 else if (i <= 0xffff)
13070 op = DW_OP_const2u;
13071 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13072 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13073 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13074 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13075 while DW_OP_const4u is 5 bytes. */
13076 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13077 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13078 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13079 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13080 while DW_OP_const4u is 5 bytes. */
13081 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13083 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13084 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13085 <= 4)
13087 /* As i >= 2**31, the double cast above will yield a negative number.
13088 Since wrapping is defined in DWARF expressions we can output big
13089 positive integers as small negative ones, regardless of the size
13090 of host wide ints.
13092 Here, since the evaluator will handle 32-bit values and since i >=
13093 2**31, we know it's going to be interpreted as a negative literal:
13094 store it this way if we can do better than 5 bytes this way. */
13095 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13097 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13098 op = DW_OP_const4u;
13100 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13101 least 6 bytes: see if we can do better before falling back to it. */
13102 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13103 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13104 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13105 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13106 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13107 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13108 >= HOST_BITS_PER_WIDE_INT)
13109 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13110 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13111 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13112 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13113 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13114 && size_of_uleb128 (i) > 6)
13115 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13116 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13117 else
13118 op = DW_OP_constu;
13120 else
13122 if (i >= -0x80)
13123 op = DW_OP_const1s;
13124 else if (i >= -0x8000)
13125 op = DW_OP_const2s;
13126 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13128 if (size_of_int_loc_descriptor (i) < 5)
13130 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13131 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13132 return ret;
13134 op = DW_OP_const4s;
13136 else
13138 if (size_of_int_loc_descriptor (i)
13139 < (unsigned long) 1 + size_of_sleb128 (i))
13141 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13142 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13143 return ret;
13145 op = DW_OP_consts;
13149 return new_loc_descr (op, i, 0);
13152 /* Likewise, for unsigned constants. */
13154 static dw_loc_descr_ref
13155 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13157 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13158 const unsigned HOST_WIDE_INT max_uint
13159 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13161 /* If possible, use the clever signed constants handling. */
13162 if (i <= max_int)
13163 return int_loc_descriptor ((HOST_WIDE_INT) i);
13165 /* Here, we are left with positive numbers that cannot be represented as
13166 HOST_WIDE_INT, i.e.:
13167 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13169 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13170 whereas may be better to output a negative integer: thanks to integer
13171 wrapping, we know that:
13172 x = x - 2 ** DWARF2_ADDR_SIZE
13173 = x - 2 * (max (HOST_WIDE_INT) + 1)
13174 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13175 small negative integers. Let's try that in cases it will clearly improve
13176 the encoding: there is no gain turning DW_OP_const4u into
13177 DW_OP_const4s. */
13178 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13179 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13180 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13182 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13184 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13185 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13186 const HOST_WIDE_INT second_shift
13187 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13189 /* So we finally have:
13190 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13191 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13192 return int_loc_descriptor (second_shift);
13195 /* Last chance: fallback to a simple constant operation. */
13196 return new_loc_descr
13197 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13198 ? DW_OP_const4u
13199 : DW_OP_const8u,
13200 i, 0);
13203 /* Generate and return a location description that computes the unsigned
13204 comparison of the two stack top entries (a OP b where b is the top-most
13205 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13206 LE_EXPR, GT_EXPR or GE_EXPR. */
13208 static dw_loc_descr_ref
13209 uint_comparison_loc_list (enum tree_code kind)
13211 enum dwarf_location_atom op, flip_op;
13212 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13214 switch (kind)
13216 case LT_EXPR:
13217 op = DW_OP_lt;
13218 break;
13219 case LE_EXPR:
13220 op = DW_OP_le;
13221 break;
13222 case GT_EXPR:
13223 op = DW_OP_gt;
13224 break;
13225 case GE_EXPR:
13226 op = DW_OP_ge;
13227 break;
13228 default:
13229 gcc_unreachable ();
13232 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13233 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13235 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13236 possible to perform unsigned comparisons: we just have to distinguish
13237 three cases:
13239 1. when a and b have the same sign (as signed integers); then we should
13240 return: a OP(signed) b;
13242 2. when a is a negative signed integer while b is a positive one, then a
13243 is a greater unsigned integer than b; likewise when a and b's roles
13244 are flipped.
13246 So first, compare the sign of the two operands. */
13247 ret = new_loc_descr (DW_OP_over, 0, 0);
13248 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13249 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13250 /* If they have different signs (i.e. they have different sign bits), then
13251 the stack top value has now the sign bit set and thus it's smaller than
13252 zero. */
13253 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13254 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13255 add_loc_descr (&ret, bra_node);
13257 /* We are in case 1. At this point, we know both operands have the same
13258 sign, to it's safe to use the built-in signed comparison. */
13259 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13260 add_loc_descr (&ret, jmp_node);
13262 /* We are in case 2. Here, we know both operands do not have the same sign,
13263 so we have to flip the signed comparison. */
13264 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13265 tmp = new_loc_descr (flip_op, 0, 0);
13266 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13267 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13268 add_loc_descr (&ret, tmp);
13270 /* This dummy operation is necessary to make the two branches join. */
13271 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13272 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13273 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13274 add_loc_descr (&ret, tmp);
13276 return ret;
13279 /* Likewise, but takes the location description lists (might be destructive on
13280 them). Return NULL if either is NULL or if concatenation fails. */
13282 static dw_loc_list_ref
13283 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13284 enum tree_code kind)
13286 if (left == NULL || right == NULL)
13287 return NULL;
13289 add_loc_list (&left, right);
13290 if (left == NULL)
13291 return NULL;
13293 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13294 return left;
13297 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13298 without actually allocating it. */
13300 static unsigned long
13301 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13303 return size_of_int_loc_descriptor (i >> shift)
13304 + size_of_int_loc_descriptor (shift)
13305 + 1;
13308 /* Return size_of_locs (int_loc_descriptor (i)) without
13309 actually allocating it. */
13311 static unsigned long
13312 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13314 unsigned long s;
13316 if (i >= 0)
13318 int clz, ctz;
13319 if (i <= 31)
13320 return 1;
13321 else if (i <= 0xff)
13322 return 2;
13323 else if (i <= 0xffff)
13324 return 3;
13325 clz = clz_hwi (i);
13326 ctz = ctz_hwi (i);
13327 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13328 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13329 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13330 - clz - 5);
13331 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13332 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13333 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13334 - clz - 8);
13335 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13336 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13337 <= 4)
13338 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13339 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13340 return 5;
13341 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13342 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13343 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13344 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13345 - clz - 8);
13346 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13347 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13348 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13349 - clz - 16);
13350 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13351 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13352 && s > 6)
13353 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13354 - clz - 32);
13355 else
13356 return 1 + s;
13358 else
13360 if (i >= -0x80)
13361 return 2;
13362 else if (i >= -0x8000)
13363 return 3;
13364 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13366 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13368 s = size_of_int_loc_descriptor (-i) + 1;
13369 if (s < 5)
13370 return s;
13372 return 5;
13374 else
13376 unsigned long r = 1 + size_of_sleb128 (i);
13377 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13379 s = size_of_int_loc_descriptor (-i) + 1;
13380 if (s < r)
13381 return s;
13383 return r;
13388 /* Return loc description representing "address" of integer value.
13389 This can appear only as toplevel expression. */
13391 static dw_loc_descr_ref
13392 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13394 int litsize;
13395 dw_loc_descr_ref loc_result = NULL;
13397 if (!(dwarf_version >= 4 || !dwarf_strict))
13398 return NULL;
13400 litsize = size_of_int_loc_descriptor (i);
13401 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13402 is more compact. For DW_OP_stack_value we need:
13403 litsize + 1 (DW_OP_stack_value)
13404 and for DW_OP_implicit_value:
13405 1 (DW_OP_implicit_value) + 1 (length) + size. */
13406 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13408 loc_result = int_loc_descriptor (i);
13409 add_loc_descr (&loc_result,
13410 new_loc_descr (DW_OP_stack_value, 0, 0));
13411 return loc_result;
13414 loc_result = new_loc_descr (DW_OP_implicit_value,
13415 size, 0);
13416 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13417 loc_result->dw_loc_oprnd2.v.val_int = i;
13418 return loc_result;
13421 /* Return a location descriptor that designates a base+offset location. */
13423 static dw_loc_descr_ref
13424 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13425 enum var_init_status initialized)
13427 unsigned int regno;
13428 dw_loc_descr_ref result;
13429 dw_fde_ref fde = cfun->fde;
13431 /* We only use "frame base" when we're sure we're talking about the
13432 post-prologue local stack frame. We do this by *not* running
13433 register elimination until this point, and recognizing the special
13434 argument pointer and soft frame pointer rtx's. */
13435 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13437 rtx elim = (ira_use_lra_p
13438 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13439 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13441 if (elim != reg)
13443 if (GET_CODE (elim) == PLUS)
13445 offset += INTVAL (XEXP (elim, 1));
13446 elim = XEXP (elim, 0);
13448 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13449 && (elim == hard_frame_pointer_rtx
13450 || elim == stack_pointer_rtx))
13451 || elim == (frame_pointer_needed
13452 ? hard_frame_pointer_rtx
13453 : stack_pointer_rtx));
13455 /* If drap register is used to align stack, use frame
13456 pointer + offset to access stack variables. If stack
13457 is aligned without drap, use stack pointer + offset to
13458 access stack variables. */
13459 if (crtl->stack_realign_tried
13460 && reg == frame_pointer_rtx)
13462 int base_reg
13463 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13464 ? HARD_FRAME_POINTER_REGNUM
13465 : REGNO (elim));
13466 return new_reg_loc_descr (base_reg, offset);
13469 gcc_assert (frame_pointer_fb_offset_valid);
13470 offset += frame_pointer_fb_offset;
13471 return new_loc_descr (DW_OP_fbreg, offset, 0);
13475 regno = REGNO (reg);
13476 #ifdef LEAF_REG_REMAP
13477 if (crtl->uses_only_leaf_regs)
13479 int leaf_reg = LEAF_REG_REMAP (regno);
13480 if (leaf_reg != -1)
13481 regno = (unsigned) leaf_reg;
13483 #endif
13484 regno = DWARF_FRAME_REGNUM (regno);
13486 if (!optimize && fde
13487 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13489 /* Use cfa+offset to represent the location of arguments passed
13490 on the stack when drap is used to align stack.
13491 Only do this when not optimizing, for optimized code var-tracking
13492 is supposed to track where the arguments live and the register
13493 used as vdrap or drap in some spot might be used for something
13494 else in other part of the routine. */
13495 return new_loc_descr (DW_OP_fbreg, offset, 0);
13498 if (regno <= 31)
13499 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13500 offset, 0);
13501 else
13502 result = new_loc_descr (DW_OP_bregx, regno, offset);
13504 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13505 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13507 return result;
13510 /* Return true if this RTL expression describes a base+offset calculation. */
13512 static inline int
13513 is_based_loc (const_rtx rtl)
13515 return (GET_CODE (rtl) == PLUS
13516 && ((REG_P (XEXP (rtl, 0))
13517 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13518 && CONST_INT_P (XEXP (rtl, 1)))));
13521 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13522 failed. */
13524 static dw_loc_descr_ref
13525 tls_mem_loc_descriptor (rtx mem)
13527 tree base;
13528 dw_loc_descr_ref loc_result;
13530 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13531 return NULL;
13533 base = get_base_address (MEM_EXPR (mem));
13534 if (base == NULL
13535 || !VAR_P (base)
13536 || !DECL_THREAD_LOCAL_P (base))
13537 return NULL;
13539 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13540 if (loc_result == NULL)
13541 return NULL;
13543 if (MEM_OFFSET (mem))
13544 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13546 return loc_result;
13549 /* Output debug info about reason why we failed to expand expression as dwarf
13550 expression. */
13552 static void
13553 expansion_failed (tree expr, rtx rtl, char const *reason)
13555 if (dump_file && (dump_flags & TDF_DETAILS))
13557 fprintf (dump_file, "Failed to expand as dwarf: ");
13558 if (expr)
13559 print_generic_expr (dump_file, expr, dump_flags);
13560 if (rtl)
13562 fprintf (dump_file, "\n");
13563 print_rtl (dump_file, rtl);
13565 fprintf (dump_file, "\nReason: %s\n", reason);
13569 /* Helper function for const_ok_for_output. */
13571 static bool
13572 const_ok_for_output_1 (rtx rtl)
13574 if (GET_CODE (rtl) == UNSPEC)
13576 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13577 we can't express it in the debug info. */
13578 /* Don't complain about TLS UNSPECs, those are just too hard to
13579 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13580 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13581 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13582 if (flag_checking
13583 && (XVECLEN (rtl, 0) == 0
13584 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13585 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13586 inform (current_function_decl
13587 ? DECL_SOURCE_LOCATION (current_function_decl)
13588 : UNKNOWN_LOCATION,
13589 #if NUM_UNSPEC_VALUES > 0
13590 "non-delegitimized UNSPEC %s (%d) found in variable location",
13591 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13592 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13593 XINT (rtl, 1));
13594 #else
13595 "non-delegitimized UNSPEC %d found in variable location",
13596 XINT (rtl, 1));
13597 #endif
13598 expansion_failed (NULL_TREE, rtl,
13599 "UNSPEC hasn't been delegitimized.\n");
13600 return false;
13603 if (targetm.const_not_ok_for_debug_p (rtl))
13605 expansion_failed (NULL_TREE, rtl,
13606 "Expression rejected for debug by the backend.\n");
13607 return false;
13610 /* FIXME: Refer to PR60655. It is possible for simplification
13611 of rtl expressions in var tracking to produce such expressions.
13612 We should really identify / validate expressions
13613 enclosed in CONST that can be handled by assemblers on various
13614 targets and only handle legitimate cases here. */
13615 if (GET_CODE (rtl) != SYMBOL_REF)
13617 if (GET_CODE (rtl) == NOT)
13618 return false;
13619 return true;
13622 if (CONSTANT_POOL_ADDRESS_P (rtl))
13624 bool marked;
13625 get_pool_constant_mark (rtl, &marked);
13626 /* If all references to this pool constant were optimized away,
13627 it was not output and thus we can't represent it. */
13628 if (!marked)
13630 expansion_failed (NULL_TREE, rtl,
13631 "Constant was removed from constant pool.\n");
13632 return false;
13636 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13637 return false;
13639 /* Avoid references to external symbols in debug info, on several targets
13640 the linker might even refuse to link when linking a shared library,
13641 and in many other cases the relocations for .debug_info/.debug_loc are
13642 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13643 to be defined within the same shared library or executable are fine. */
13644 if (SYMBOL_REF_EXTERNAL_P (rtl))
13646 tree decl = SYMBOL_REF_DECL (rtl);
13648 if (decl == NULL || !targetm.binds_local_p (decl))
13650 expansion_failed (NULL_TREE, rtl,
13651 "Symbol not defined in current TU.\n");
13652 return false;
13656 return true;
13659 /* Return true if constant RTL can be emitted in DW_OP_addr or
13660 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13661 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13663 static bool
13664 const_ok_for_output (rtx rtl)
13666 if (GET_CODE (rtl) == SYMBOL_REF)
13667 return const_ok_for_output_1 (rtl);
13669 if (GET_CODE (rtl) == CONST)
13671 subrtx_var_iterator::array_type array;
13672 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13673 if (!const_ok_for_output_1 (*iter))
13674 return false;
13675 return true;
13678 return true;
13681 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13682 if possible, NULL otherwise. */
13684 static dw_die_ref
13685 base_type_for_mode (machine_mode mode, bool unsignedp)
13687 dw_die_ref type_die;
13688 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13690 if (type == NULL)
13691 return NULL;
13692 switch (TREE_CODE (type))
13694 case INTEGER_TYPE:
13695 case REAL_TYPE:
13696 break;
13697 default:
13698 return NULL;
13700 type_die = lookup_type_die (type);
13701 if (!type_die)
13702 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13703 comp_unit_die ());
13704 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13705 return NULL;
13706 return type_die;
13709 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13710 type matching MODE, or, if MODE is narrower than or as wide as
13711 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13712 possible. */
13714 static dw_loc_descr_ref
13715 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13717 machine_mode outer_mode = mode;
13718 dw_die_ref type_die;
13719 dw_loc_descr_ref cvt;
13721 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13723 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13724 return op;
13726 type_die = base_type_for_mode (outer_mode, 1);
13727 if (type_die == NULL)
13728 return NULL;
13729 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13730 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13731 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13732 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13733 add_loc_descr (&op, cvt);
13734 return op;
13737 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13739 static dw_loc_descr_ref
13740 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13741 dw_loc_descr_ref op1)
13743 dw_loc_descr_ref ret = op0;
13744 add_loc_descr (&ret, op1);
13745 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13746 if (STORE_FLAG_VALUE != 1)
13748 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13749 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13751 return ret;
13754 /* Return location descriptor for signed comparison OP RTL. */
13756 static dw_loc_descr_ref
13757 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13758 machine_mode mem_mode)
13760 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13761 dw_loc_descr_ref op0, op1;
13762 int shift;
13764 if (op_mode == VOIDmode)
13765 op_mode = GET_MODE (XEXP (rtl, 1));
13766 if (op_mode == VOIDmode)
13767 return NULL;
13769 if (dwarf_strict
13770 && dwarf_version < 5
13771 && (!SCALAR_INT_MODE_P (op_mode)
13772 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13773 return NULL;
13775 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13776 VAR_INIT_STATUS_INITIALIZED);
13777 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13778 VAR_INIT_STATUS_INITIALIZED);
13780 if (op0 == NULL || op1 == NULL)
13781 return NULL;
13783 if (!SCALAR_INT_MODE_P (op_mode)
13784 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13785 return compare_loc_descriptor (op, op0, op1);
13787 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13789 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13790 dw_loc_descr_ref cvt;
13792 if (type_die == NULL)
13793 return NULL;
13794 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13795 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13796 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13797 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13798 add_loc_descr (&op0, cvt);
13799 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13800 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13801 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13802 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13803 add_loc_descr (&op1, cvt);
13804 return compare_loc_descriptor (op, op0, op1);
13807 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13808 /* For eq/ne, if the operands are known to be zero-extended,
13809 there is no need to do the fancy shifting up. */
13810 if (op == DW_OP_eq || op == DW_OP_ne)
13812 dw_loc_descr_ref last0, last1;
13813 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13815 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13817 /* deref_size zero extends, and for constants we can check
13818 whether they are zero extended or not. */
13819 if (((last0->dw_loc_opc == DW_OP_deref_size
13820 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13821 || (CONST_INT_P (XEXP (rtl, 0))
13822 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13823 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13824 && ((last1->dw_loc_opc == DW_OP_deref_size
13825 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13826 || (CONST_INT_P (XEXP (rtl, 1))
13827 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13828 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13829 return compare_loc_descriptor (op, op0, op1);
13831 /* EQ/NE comparison against constant in narrower type than
13832 DWARF2_ADDR_SIZE can be performed either as
13833 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13834 DW_OP_{eq,ne}
13836 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13837 DW_OP_{eq,ne}. Pick whatever is shorter. */
13838 if (CONST_INT_P (XEXP (rtl, 1))
13839 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13840 && (size_of_int_loc_descriptor (shift) + 1
13841 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13842 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13843 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13844 & GET_MODE_MASK (op_mode))))
13846 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13847 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13848 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13849 & GET_MODE_MASK (op_mode));
13850 return compare_loc_descriptor (op, op0, op1);
13853 add_loc_descr (&op0, int_loc_descriptor (shift));
13854 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13855 if (CONST_INT_P (XEXP (rtl, 1)))
13856 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13857 else
13859 add_loc_descr (&op1, int_loc_descriptor (shift));
13860 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13862 return compare_loc_descriptor (op, op0, op1);
13865 /* Return location descriptor for unsigned comparison OP RTL. */
13867 static dw_loc_descr_ref
13868 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13869 machine_mode mem_mode)
13871 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13872 dw_loc_descr_ref op0, op1;
13874 if (op_mode == VOIDmode)
13875 op_mode = GET_MODE (XEXP (rtl, 1));
13876 if (op_mode == VOIDmode)
13877 return NULL;
13878 if (!SCALAR_INT_MODE_P (op_mode))
13879 return NULL;
13881 if (dwarf_strict
13882 && dwarf_version < 5
13883 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13884 return NULL;
13886 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13887 VAR_INIT_STATUS_INITIALIZED);
13888 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13889 VAR_INIT_STATUS_INITIALIZED);
13891 if (op0 == NULL || op1 == NULL)
13892 return NULL;
13894 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13896 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13897 dw_loc_descr_ref last0, last1;
13898 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13900 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13902 if (CONST_INT_P (XEXP (rtl, 0)))
13903 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13904 /* deref_size zero extends, so no need to mask it again. */
13905 else if (last0->dw_loc_opc != DW_OP_deref_size
13906 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13908 add_loc_descr (&op0, int_loc_descriptor (mask));
13909 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13911 if (CONST_INT_P (XEXP (rtl, 1)))
13912 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13913 /* deref_size zero extends, so no need to mask it again. */
13914 else if (last1->dw_loc_opc != DW_OP_deref_size
13915 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13917 add_loc_descr (&op1, int_loc_descriptor (mask));
13918 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13921 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13923 HOST_WIDE_INT bias = 1;
13924 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13925 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13926 if (CONST_INT_P (XEXP (rtl, 1)))
13927 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13928 + INTVAL (XEXP (rtl, 1)));
13929 else
13930 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13931 bias, 0));
13933 return compare_loc_descriptor (op, op0, op1);
13936 /* Return location descriptor for {U,S}{MIN,MAX}. */
13938 static dw_loc_descr_ref
13939 minmax_loc_descriptor (rtx rtl, machine_mode mode,
13940 machine_mode mem_mode)
13942 enum dwarf_location_atom op;
13943 dw_loc_descr_ref op0, op1, ret;
13944 dw_loc_descr_ref bra_node, drop_node;
13946 if (dwarf_strict
13947 && dwarf_version < 5
13948 && (!SCALAR_INT_MODE_P (mode)
13949 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
13950 return NULL;
13952 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13953 VAR_INIT_STATUS_INITIALIZED);
13954 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13955 VAR_INIT_STATUS_INITIALIZED);
13957 if (op0 == NULL || op1 == NULL)
13958 return NULL;
13960 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13961 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13962 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13963 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13965 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13967 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
13968 add_loc_descr (&op0, int_loc_descriptor (mask));
13969 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13970 add_loc_descr (&op1, int_loc_descriptor (mask));
13971 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13973 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13975 HOST_WIDE_INT bias = 1;
13976 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13977 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13978 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13981 else if (!SCALAR_INT_MODE_P (mode)
13982 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13984 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
13985 add_loc_descr (&op0, int_loc_descriptor (shift));
13986 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13987 add_loc_descr (&op1, int_loc_descriptor (shift));
13988 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13990 else if (SCALAR_INT_MODE_P (mode)
13991 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13993 dw_die_ref type_die = base_type_for_mode (mode, 0);
13994 dw_loc_descr_ref cvt;
13995 if (type_die == NULL)
13996 return NULL;
13997 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13998 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13999 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14000 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14001 add_loc_descr (&op0, cvt);
14002 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14003 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14004 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14005 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14006 add_loc_descr (&op1, cvt);
14009 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14010 op = DW_OP_lt;
14011 else
14012 op = DW_OP_gt;
14013 ret = op0;
14014 add_loc_descr (&ret, op1);
14015 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14016 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14017 add_loc_descr (&ret, bra_node);
14018 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14019 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14020 add_loc_descr (&ret, drop_node);
14021 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14022 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14023 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14024 && SCALAR_INT_MODE_P (mode)
14025 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14026 ret = convert_descriptor_to_mode (mode, ret);
14027 return ret;
14030 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14031 but after converting arguments to type_die, afterwards
14032 convert back to unsigned. */
14034 static dw_loc_descr_ref
14035 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14036 machine_mode mode, machine_mode mem_mode)
14038 dw_loc_descr_ref cvt, op0, op1;
14040 if (type_die == NULL)
14041 return NULL;
14042 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14043 VAR_INIT_STATUS_INITIALIZED);
14044 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14045 VAR_INIT_STATUS_INITIALIZED);
14046 if (op0 == NULL || op1 == NULL)
14047 return NULL;
14048 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14049 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14050 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14051 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14052 add_loc_descr (&op0, cvt);
14053 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14054 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14055 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14056 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14057 add_loc_descr (&op1, cvt);
14058 add_loc_descr (&op0, op1);
14059 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14060 return convert_descriptor_to_mode (mode, op0);
14063 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14064 const0 is DW_OP_lit0 or corresponding typed constant,
14065 const1 is DW_OP_lit1 or corresponding typed constant
14066 and constMSB is constant with just the MSB bit set
14067 for the mode):
14068 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14069 L1: const0 DW_OP_swap
14070 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
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 CTZ is similar:
14076 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14077 L1: const0 DW_OP_swap
14078 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14079 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14080 L3: DW_OP_drop
14081 L4: DW_OP_nop
14083 FFS is similar:
14084 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14085 L1: const1 DW_OP_swap
14086 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14087 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14088 L3: DW_OP_drop
14089 L4: DW_OP_nop */
14091 static dw_loc_descr_ref
14092 clz_loc_descriptor (rtx rtl, machine_mode mode,
14093 machine_mode mem_mode)
14095 dw_loc_descr_ref op0, ret, tmp;
14096 HOST_WIDE_INT valv;
14097 dw_loc_descr_ref l1jump, l1label;
14098 dw_loc_descr_ref l2jump, l2label;
14099 dw_loc_descr_ref l3jump, l3label;
14100 dw_loc_descr_ref l4jump, l4label;
14101 rtx msb;
14103 if (!SCALAR_INT_MODE_P (mode)
14104 || GET_MODE (XEXP (rtl, 0)) != mode)
14105 return NULL;
14107 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14108 VAR_INIT_STATUS_INITIALIZED);
14109 if (op0 == NULL)
14110 return NULL;
14111 ret = op0;
14112 if (GET_CODE (rtl) == CLZ)
14114 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14115 valv = GET_MODE_BITSIZE (mode);
14117 else if (GET_CODE (rtl) == FFS)
14118 valv = 0;
14119 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14120 valv = GET_MODE_BITSIZE (mode);
14121 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14122 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14123 add_loc_descr (&ret, l1jump);
14124 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14125 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14126 VAR_INIT_STATUS_INITIALIZED);
14127 if (tmp == NULL)
14128 return NULL;
14129 add_loc_descr (&ret, tmp);
14130 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14131 add_loc_descr (&ret, l4jump);
14132 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14133 ? const1_rtx : const0_rtx,
14134 mode, mem_mode,
14135 VAR_INIT_STATUS_INITIALIZED);
14136 if (l1label == NULL)
14137 return NULL;
14138 add_loc_descr (&ret, l1label);
14139 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14140 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14141 add_loc_descr (&ret, l2label);
14142 if (GET_CODE (rtl) != CLZ)
14143 msb = const1_rtx;
14144 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14145 msb = GEN_INT (HOST_WIDE_INT_1U
14146 << (GET_MODE_BITSIZE (mode) - 1));
14147 else
14148 msb = immed_wide_int_const
14149 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14150 GET_MODE_PRECISION (mode)), mode);
14151 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14152 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14153 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14154 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14155 else
14156 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14157 VAR_INIT_STATUS_INITIALIZED);
14158 if (tmp == NULL)
14159 return NULL;
14160 add_loc_descr (&ret, tmp);
14161 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14162 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14163 add_loc_descr (&ret, l3jump);
14164 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14165 VAR_INIT_STATUS_INITIALIZED);
14166 if (tmp == NULL)
14167 return NULL;
14168 add_loc_descr (&ret, tmp);
14169 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14170 ? DW_OP_shl : DW_OP_shr, 0, 0));
14171 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14172 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14173 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14174 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14175 add_loc_descr (&ret, l2jump);
14176 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14177 add_loc_descr (&ret, l3label);
14178 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14179 add_loc_descr (&ret, l4label);
14180 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14181 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14182 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14183 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14184 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14185 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14186 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14187 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14188 return ret;
14191 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14192 const1 is DW_OP_lit1 or corresponding typed constant):
14193 const0 DW_OP_swap
14194 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14195 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14196 L2: DW_OP_drop
14198 PARITY is similar:
14199 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14200 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14201 L2: DW_OP_drop */
14203 static dw_loc_descr_ref
14204 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14205 machine_mode mem_mode)
14207 dw_loc_descr_ref op0, ret, tmp;
14208 dw_loc_descr_ref l1jump, l1label;
14209 dw_loc_descr_ref l2jump, l2label;
14211 if (!SCALAR_INT_MODE_P (mode)
14212 || GET_MODE (XEXP (rtl, 0)) != mode)
14213 return NULL;
14215 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14216 VAR_INIT_STATUS_INITIALIZED);
14217 if (op0 == NULL)
14218 return NULL;
14219 ret = op0;
14220 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14221 VAR_INIT_STATUS_INITIALIZED);
14222 if (tmp == NULL)
14223 return NULL;
14224 add_loc_descr (&ret, tmp);
14225 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14226 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14227 add_loc_descr (&ret, l1label);
14228 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14229 add_loc_descr (&ret, l2jump);
14230 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14231 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14232 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14233 VAR_INIT_STATUS_INITIALIZED);
14234 if (tmp == NULL)
14235 return NULL;
14236 add_loc_descr (&ret, tmp);
14237 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14238 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14239 ? DW_OP_plus : DW_OP_xor, 0, 0));
14240 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14241 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14242 VAR_INIT_STATUS_INITIALIZED);
14243 add_loc_descr (&ret, tmp);
14244 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14245 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14246 add_loc_descr (&ret, l1jump);
14247 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14248 add_loc_descr (&ret, l2label);
14249 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14250 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14251 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14252 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14253 return ret;
14256 /* BSWAP (constS is initial shift count, either 56 or 24):
14257 constS const0
14258 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14259 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14260 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14261 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14262 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14264 static dw_loc_descr_ref
14265 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14266 machine_mode mem_mode)
14268 dw_loc_descr_ref op0, ret, tmp;
14269 dw_loc_descr_ref l1jump, l1label;
14270 dw_loc_descr_ref l2jump, l2label;
14272 if (!SCALAR_INT_MODE_P (mode)
14273 || BITS_PER_UNIT != 8
14274 || (GET_MODE_BITSIZE (mode) != 32
14275 && GET_MODE_BITSIZE (mode) != 64))
14276 return NULL;
14278 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14279 VAR_INIT_STATUS_INITIALIZED);
14280 if (op0 == NULL)
14281 return NULL;
14283 ret = op0;
14284 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14285 mode, mem_mode,
14286 VAR_INIT_STATUS_INITIALIZED);
14287 if (tmp == NULL)
14288 return NULL;
14289 add_loc_descr (&ret, tmp);
14290 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14291 VAR_INIT_STATUS_INITIALIZED);
14292 if (tmp == NULL)
14293 return NULL;
14294 add_loc_descr (&ret, tmp);
14295 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14296 add_loc_descr (&ret, l1label);
14297 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14298 mode, mem_mode,
14299 VAR_INIT_STATUS_INITIALIZED);
14300 add_loc_descr (&ret, tmp);
14301 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14302 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14303 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14304 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14305 VAR_INIT_STATUS_INITIALIZED);
14306 if (tmp == NULL)
14307 return NULL;
14308 add_loc_descr (&ret, tmp);
14309 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14310 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14311 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14312 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14313 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14314 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14315 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14316 VAR_INIT_STATUS_INITIALIZED);
14317 add_loc_descr (&ret, tmp);
14318 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14319 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14320 add_loc_descr (&ret, l2jump);
14321 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14322 VAR_INIT_STATUS_INITIALIZED);
14323 add_loc_descr (&ret, tmp);
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14325 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14326 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14327 add_loc_descr (&ret, l1jump);
14328 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14329 add_loc_descr (&ret, l2label);
14330 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14331 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14332 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14333 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14334 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14335 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14336 return ret;
14339 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14340 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14341 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14342 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14344 ROTATERT is similar:
14345 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14346 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14347 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14349 static dw_loc_descr_ref
14350 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14351 machine_mode mem_mode)
14353 rtx rtlop1 = XEXP (rtl, 1);
14354 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14355 int i;
14357 if (!SCALAR_INT_MODE_P (mode))
14358 return NULL;
14360 if (GET_MODE (rtlop1) != VOIDmode
14361 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14362 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14363 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14364 VAR_INIT_STATUS_INITIALIZED);
14365 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14366 VAR_INIT_STATUS_INITIALIZED);
14367 if (op0 == NULL || op1 == NULL)
14368 return NULL;
14369 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14370 for (i = 0; i < 2; i++)
14372 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14373 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14374 mode, mem_mode,
14375 VAR_INIT_STATUS_INITIALIZED);
14376 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14377 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14378 ? DW_OP_const4u
14379 : HOST_BITS_PER_WIDE_INT == 64
14380 ? DW_OP_const8u : DW_OP_constu,
14381 GET_MODE_MASK (mode), 0);
14382 else
14383 mask[i] = NULL;
14384 if (mask[i] == NULL)
14385 return NULL;
14386 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14388 ret = op0;
14389 add_loc_descr (&ret, op1);
14390 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14391 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14392 if (GET_CODE (rtl) == ROTATERT)
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_shl, 0, 0));
14399 if (mask[0] != NULL)
14400 add_loc_descr (&ret, mask[0]);
14401 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14402 if (mask[1] != NULL)
14404 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14405 add_loc_descr (&ret, mask[1]);
14406 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14408 if (GET_CODE (rtl) == ROTATE)
14410 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14411 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14412 GET_MODE_BITSIZE (mode), 0));
14414 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14415 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14416 return ret;
14419 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14420 for DEBUG_PARAMETER_REF RTL. */
14422 static dw_loc_descr_ref
14423 parameter_ref_descriptor (rtx rtl)
14425 dw_loc_descr_ref ret;
14426 dw_die_ref ref;
14428 if (dwarf_strict)
14429 return NULL;
14430 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14431 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14432 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14433 if (ref)
14435 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14436 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14437 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14439 else
14441 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14442 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14444 return ret;
14447 /* The following routine converts the RTL for a variable or parameter
14448 (resident in memory) into an equivalent Dwarf representation of a
14449 mechanism for getting the address of that same variable onto the top of a
14450 hypothetical "address evaluation" stack.
14452 When creating memory location descriptors, we are effectively transforming
14453 the RTL for a memory-resident object into its Dwarf postfix expression
14454 equivalent. This routine recursively descends an RTL tree, turning
14455 it into Dwarf postfix code as it goes.
14457 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14459 MEM_MODE is the mode of the memory reference, needed to handle some
14460 autoincrement addressing modes.
14462 Return 0 if we can't represent the location. */
14464 dw_loc_descr_ref
14465 mem_loc_descriptor (rtx rtl, machine_mode mode,
14466 machine_mode mem_mode,
14467 enum var_init_status initialized)
14469 dw_loc_descr_ref mem_loc_result = NULL;
14470 enum dwarf_location_atom op;
14471 dw_loc_descr_ref op0, op1;
14472 rtx inner = NULL_RTX;
14474 if (mode == VOIDmode)
14475 mode = GET_MODE (rtl);
14477 /* Note that for a dynamically sized array, the location we will generate a
14478 description of here will be the lowest numbered location which is
14479 actually within the array. That's *not* necessarily the same as the
14480 zeroth element of the array. */
14482 rtl = targetm.delegitimize_address (rtl);
14484 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14485 return NULL;
14487 switch (GET_CODE (rtl))
14489 case POST_INC:
14490 case POST_DEC:
14491 case POST_MODIFY:
14492 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14494 case SUBREG:
14495 /* The case of a subreg may arise when we have a local (register)
14496 variable or a formal (register) parameter which doesn't quite fill
14497 up an entire register. For now, just assume that it is
14498 legitimate to make the Dwarf info refer to the whole register which
14499 contains the given subreg. */
14500 if (!subreg_lowpart_p (rtl))
14501 break;
14502 inner = SUBREG_REG (rtl);
14503 /* FALLTHRU */
14504 case TRUNCATE:
14505 if (inner == NULL_RTX)
14506 inner = XEXP (rtl, 0);
14507 if (SCALAR_INT_MODE_P (mode)
14508 && SCALAR_INT_MODE_P (GET_MODE (inner))
14509 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14510 #ifdef POINTERS_EXTEND_UNSIGNED
14511 || (mode == Pmode && mem_mode != VOIDmode)
14512 #endif
14514 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14516 mem_loc_result = mem_loc_descriptor (inner,
14517 GET_MODE (inner),
14518 mem_mode, initialized);
14519 break;
14521 if (dwarf_strict && dwarf_version < 5)
14522 break;
14523 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14524 break;
14525 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14526 && (!SCALAR_INT_MODE_P (mode)
14527 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14528 break;
14529 else
14531 dw_die_ref type_die;
14532 dw_loc_descr_ref cvt;
14534 mem_loc_result = mem_loc_descriptor (inner,
14535 GET_MODE (inner),
14536 mem_mode, initialized);
14537 if (mem_loc_result == NULL)
14538 break;
14539 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14540 if (type_die == NULL)
14542 mem_loc_result = NULL;
14543 break;
14545 if (GET_MODE_SIZE (mode)
14546 != GET_MODE_SIZE (GET_MODE (inner)))
14547 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14548 else
14549 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14550 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14551 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14552 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14553 add_loc_descr (&mem_loc_result, cvt);
14554 if (SCALAR_INT_MODE_P (mode)
14555 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14557 /* Convert it to untyped afterwards. */
14558 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14559 add_loc_descr (&mem_loc_result, cvt);
14562 break;
14564 case REG:
14565 if (! SCALAR_INT_MODE_P (mode)
14566 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14567 && rtl != arg_pointer_rtx
14568 && rtl != frame_pointer_rtx
14569 #ifdef POINTERS_EXTEND_UNSIGNED
14570 && (mode != Pmode || mem_mode == VOIDmode)
14571 #endif
14574 dw_die_ref type_die;
14575 unsigned int dbx_regnum;
14577 if (dwarf_strict && dwarf_version < 5)
14578 break;
14579 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14580 break;
14581 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14582 if (type_die == NULL)
14583 break;
14585 dbx_regnum = dbx_reg_number (rtl);
14586 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14587 break;
14588 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14589 dbx_regnum, 0);
14590 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14591 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14592 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14593 break;
14595 /* Whenever a register number forms a part of the description of the
14596 method for calculating the (dynamic) address of a memory resident
14597 object, DWARF rules require the register number be referred to as
14598 a "base register". This distinction is not based in any way upon
14599 what category of register the hardware believes the given register
14600 belongs to. This is strictly DWARF terminology we're dealing with
14601 here. Note that in cases where the location of a memory-resident
14602 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14603 OP_CONST (0)) the actual DWARF location descriptor that we generate
14604 may just be OP_BASEREG (basereg). This may look deceptively like
14605 the object in question was allocated to a register (rather than in
14606 memory) so DWARF consumers need to be aware of the subtle
14607 distinction between OP_REG and OP_BASEREG. */
14608 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14609 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14610 else if (stack_realign_drap
14611 && crtl->drap_reg
14612 && crtl->args.internal_arg_pointer == rtl
14613 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14615 /* If RTL is internal_arg_pointer, which has been optimized
14616 out, use DRAP instead. */
14617 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14618 VAR_INIT_STATUS_INITIALIZED);
14620 break;
14622 case SIGN_EXTEND:
14623 case ZERO_EXTEND:
14624 if (!SCALAR_INT_MODE_P (mode))
14625 break;
14626 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14627 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14628 if (op0 == 0)
14629 break;
14630 else if (GET_CODE (rtl) == ZERO_EXTEND
14631 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14632 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14633 < HOST_BITS_PER_WIDE_INT
14634 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14635 to expand zero extend as two shifts instead of
14636 masking. */
14637 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14639 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14640 mem_loc_result = op0;
14641 add_loc_descr (&mem_loc_result,
14642 int_loc_descriptor (GET_MODE_MASK (imode)));
14643 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14645 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14647 int shift = DWARF2_ADDR_SIZE
14648 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14649 shift *= BITS_PER_UNIT;
14650 if (GET_CODE (rtl) == SIGN_EXTEND)
14651 op = DW_OP_shra;
14652 else
14653 op = DW_OP_shr;
14654 mem_loc_result = op0;
14655 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14656 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14657 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14658 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14660 else if (!dwarf_strict || dwarf_version >= 5)
14662 dw_die_ref type_die1, type_die2;
14663 dw_loc_descr_ref cvt;
14665 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14666 GET_CODE (rtl) == ZERO_EXTEND);
14667 if (type_die1 == NULL)
14668 break;
14669 type_die2 = base_type_for_mode (mode, 1);
14670 if (type_die2 == NULL)
14671 break;
14672 mem_loc_result = op0;
14673 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14674 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14675 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14676 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14677 add_loc_descr (&mem_loc_result, cvt);
14678 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14679 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14680 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14681 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14682 add_loc_descr (&mem_loc_result, cvt);
14684 break;
14686 case MEM:
14688 rtx new_rtl = avoid_constant_pool_reference (rtl);
14689 if (new_rtl != rtl)
14691 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14692 initialized);
14693 if (mem_loc_result != NULL)
14694 return mem_loc_result;
14697 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14698 get_address_mode (rtl), mode,
14699 VAR_INIT_STATUS_INITIALIZED);
14700 if (mem_loc_result == NULL)
14701 mem_loc_result = tls_mem_loc_descriptor (rtl);
14702 if (mem_loc_result != NULL)
14704 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14705 || !SCALAR_INT_MODE_P(mode))
14707 dw_die_ref type_die;
14708 dw_loc_descr_ref deref;
14710 if (dwarf_strict && dwarf_version < 5)
14711 return NULL;
14712 type_die
14713 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14714 if (type_die == NULL)
14715 return NULL;
14716 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14717 GET_MODE_SIZE (mode), 0);
14718 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14719 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14720 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14721 add_loc_descr (&mem_loc_result, deref);
14723 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14724 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14725 else
14726 add_loc_descr (&mem_loc_result,
14727 new_loc_descr (DW_OP_deref_size,
14728 GET_MODE_SIZE (mode), 0));
14730 break;
14732 case LO_SUM:
14733 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14735 case LABEL_REF:
14736 /* Some ports can transform a symbol ref into a label ref, because
14737 the symbol ref is too far away and has to be dumped into a constant
14738 pool. */
14739 case CONST:
14740 case SYMBOL_REF:
14741 if (!SCALAR_INT_MODE_P (mode)
14742 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14743 #ifdef POINTERS_EXTEND_UNSIGNED
14744 && (mode != Pmode || mem_mode == VOIDmode)
14745 #endif
14747 break;
14748 if (GET_CODE (rtl) == SYMBOL_REF
14749 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14751 dw_loc_descr_ref temp;
14753 /* If this is not defined, we have no way to emit the data. */
14754 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14755 break;
14757 temp = new_addr_loc_descr (rtl, dtprel_true);
14759 /* We check for DWARF 5 here because gdb did not implement
14760 DW_OP_form_tls_address until after 7.12. */
14761 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14762 ? DW_OP_form_tls_address
14763 : DW_OP_GNU_push_tls_address),
14764 0, 0);
14765 add_loc_descr (&mem_loc_result, temp);
14767 break;
14770 if (!const_ok_for_output (rtl))
14772 if (GET_CODE (rtl) == CONST)
14773 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14774 initialized);
14775 break;
14778 symref:
14779 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14780 vec_safe_push (used_rtx_array, rtl);
14781 break;
14783 case CONCAT:
14784 case CONCATN:
14785 case VAR_LOCATION:
14786 case DEBUG_IMPLICIT_PTR:
14787 expansion_failed (NULL_TREE, rtl,
14788 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14789 return 0;
14791 case ENTRY_VALUE:
14792 if (dwarf_strict && dwarf_version < 5)
14793 return NULL;
14794 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14796 if (!SCALAR_INT_MODE_P (mode)
14797 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14798 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14799 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14800 else
14802 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14803 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14804 return NULL;
14805 op0 = one_reg_loc_descriptor (dbx_regnum,
14806 VAR_INIT_STATUS_INITIALIZED);
14809 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14810 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14812 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14813 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14814 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14815 return NULL;
14817 else
14818 gcc_unreachable ();
14819 if (op0 == NULL)
14820 return NULL;
14821 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14822 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14823 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14824 break;
14826 case DEBUG_PARAMETER_REF:
14827 mem_loc_result = parameter_ref_descriptor (rtl);
14828 break;
14830 case PRE_MODIFY:
14831 /* Extract the PLUS expression nested inside and fall into
14832 PLUS code below. */
14833 rtl = XEXP (rtl, 1);
14834 goto plus;
14836 case PRE_INC:
14837 case PRE_DEC:
14838 /* Turn these into a PLUS expression and fall into the PLUS code
14839 below. */
14840 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14841 gen_int_mode (GET_CODE (rtl) == PRE_INC
14842 ? GET_MODE_UNIT_SIZE (mem_mode)
14843 : -GET_MODE_UNIT_SIZE (mem_mode),
14844 mode));
14846 /* fall through */
14848 case PLUS:
14849 plus:
14850 if (is_based_loc (rtl)
14851 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14852 || XEXP (rtl, 0) == arg_pointer_rtx
14853 || XEXP (rtl, 0) == frame_pointer_rtx)
14854 && SCALAR_INT_MODE_P (mode))
14855 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14856 INTVAL (XEXP (rtl, 1)),
14857 VAR_INIT_STATUS_INITIALIZED);
14858 else
14860 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14861 VAR_INIT_STATUS_INITIALIZED);
14862 if (mem_loc_result == 0)
14863 break;
14865 if (CONST_INT_P (XEXP (rtl, 1))
14866 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14867 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14868 else
14870 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14871 VAR_INIT_STATUS_INITIALIZED);
14872 if (op1 == 0)
14873 return NULL;
14874 add_loc_descr (&mem_loc_result, op1);
14875 add_loc_descr (&mem_loc_result,
14876 new_loc_descr (DW_OP_plus, 0, 0));
14879 break;
14881 /* If a pseudo-reg is optimized away, it is possible for it to
14882 be replaced with a MEM containing a multiply or shift. */
14883 case MINUS:
14884 op = DW_OP_minus;
14885 goto do_binop;
14887 case MULT:
14888 op = DW_OP_mul;
14889 goto do_binop;
14891 case DIV:
14892 if ((!dwarf_strict || dwarf_version >= 5)
14893 && SCALAR_INT_MODE_P (mode)
14894 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14896 mem_loc_result = typed_binop (DW_OP_div, rtl,
14897 base_type_for_mode (mode, 0),
14898 mode, mem_mode);
14899 break;
14901 op = DW_OP_div;
14902 goto do_binop;
14904 case UMOD:
14905 op = DW_OP_mod;
14906 goto do_binop;
14908 case ASHIFT:
14909 op = DW_OP_shl;
14910 goto do_shift;
14912 case ASHIFTRT:
14913 op = DW_OP_shra;
14914 goto do_shift;
14916 case LSHIFTRT:
14917 op = DW_OP_shr;
14918 goto do_shift;
14920 do_shift:
14921 if (!SCALAR_INT_MODE_P (mode))
14922 break;
14923 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14924 VAR_INIT_STATUS_INITIALIZED);
14926 rtx rtlop1 = XEXP (rtl, 1);
14927 if (GET_MODE (rtlop1) != VOIDmode
14928 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
14929 < GET_MODE_BITSIZE (mode))
14930 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14931 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14932 VAR_INIT_STATUS_INITIALIZED);
14935 if (op0 == 0 || op1 == 0)
14936 break;
14938 mem_loc_result = op0;
14939 add_loc_descr (&mem_loc_result, op1);
14940 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14941 break;
14943 case AND:
14944 op = DW_OP_and;
14945 goto do_binop;
14947 case IOR:
14948 op = DW_OP_or;
14949 goto do_binop;
14951 case XOR:
14952 op = DW_OP_xor;
14953 goto do_binop;
14955 do_binop:
14956 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14957 VAR_INIT_STATUS_INITIALIZED);
14958 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14959 VAR_INIT_STATUS_INITIALIZED);
14961 if (op0 == 0 || op1 == 0)
14962 break;
14964 mem_loc_result = op0;
14965 add_loc_descr (&mem_loc_result, op1);
14966 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14967 break;
14969 case MOD:
14970 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14971 && (!dwarf_strict || dwarf_version >= 5))
14973 mem_loc_result = typed_binop (DW_OP_mod, rtl,
14974 base_type_for_mode (mode, 0),
14975 mode, mem_mode);
14976 break;
14979 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14980 VAR_INIT_STATUS_INITIALIZED);
14981 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14982 VAR_INIT_STATUS_INITIALIZED);
14984 if (op0 == 0 || op1 == 0)
14985 break;
14987 mem_loc_result = op0;
14988 add_loc_descr (&mem_loc_result, op1);
14989 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14990 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14991 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14992 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14993 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14994 break;
14996 case UDIV:
14997 if ((!dwarf_strict || dwarf_version >= 5)
14998 && SCALAR_INT_MODE_P (mode))
15000 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15002 op = DW_OP_div;
15003 goto do_binop;
15005 mem_loc_result = typed_binop (DW_OP_div, rtl,
15006 base_type_for_mode (mode, 1),
15007 mode, mem_mode);
15009 break;
15011 case NOT:
15012 op = DW_OP_not;
15013 goto do_unop;
15015 case ABS:
15016 op = DW_OP_abs;
15017 goto do_unop;
15019 case NEG:
15020 op = DW_OP_neg;
15021 goto do_unop;
15023 do_unop:
15024 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15025 VAR_INIT_STATUS_INITIALIZED);
15027 if (op0 == 0)
15028 break;
15030 mem_loc_result = op0;
15031 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15032 break;
15034 case CONST_INT:
15035 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15036 #ifdef POINTERS_EXTEND_UNSIGNED
15037 || (mode == Pmode
15038 && mem_mode != VOIDmode
15039 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15040 #endif
15043 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15044 break;
15046 if ((!dwarf_strict || dwarf_version >= 5)
15047 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15048 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15050 dw_die_ref type_die = base_type_for_mode (mode, 1);
15051 machine_mode amode;
15052 if (type_die == NULL)
15053 return NULL;
15054 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15055 MODE_INT, 0);
15056 if (INTVAL (rtl) >= 0
15057 && amode != BLKmode
15058 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15059 /* const DW_OP_convert <XXX> vs.
15060 DW_OP_const_type <XXX, 1, const>. */
15061 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15062 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15064 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15065 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15066 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15067 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15068 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15069 add_loc_descr (&mem_loc_result, op0);
15070 return mem_loc_result;
15072 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15073 INTVAL (rtl));
15074 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15075 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15076 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15077 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15078 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15079 else
15081 mem_loc_result->dw_loc_oprnd2.val_class
15082 = dw_val_class_const_double;
15083 mem_loc_result->dw_loc_oprnd2.v.val_double
15084 = double_int::from_shwi (INTVAL (rtl));
15087 break;
15089 case CONST_DOUBLE:
15090 if (!dwarf_strict || dwarf_version >= 5)
15092 dw_die_ref type_die;
15094 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15095 CONST_DOUBLE rtx could represent either a large integer
15096 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15097 the value is always a floating point constant.
15099 When it is an integer, a CONST_DOUBLE is used whenever
15100 the constant requires 2 HWIs to be adequately represented.
15101 We output CONST_DOUBLEs as blocks. */
15102 if (mode == VOIDmode
15103 || (GET_MODE (rtl) == VOIDmode
15104 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15105 break;
15106 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15107 if (type_die == NULL)
15108 return NULL;
15109 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15110 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15111 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15112 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15113 #if TARGET_SUPPORTS_WIDE_INT == 0
15114 if (!SCALAR_FLOAT_MODE_P (mode))
15116 mem_loc_result->dw_loc_oprnd2.val_class
15117 = dw_val_class_const_double;
15118 mem_loc_result->dw_loc_oprnd2.v.val_double
15119 = rtx_to_double_int (rtl);
15121 else
15122 #endif
15124 unsigned int length = GET_MODE_SIZE (mode);
15125 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15127 insert_float (rtl, array);
15128 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15129 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15130 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15131 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15134 break;
15136 case CONST_WIDE_INT:
15137 if (!dwarf_strict || dwarf_version >= 5)
15139 dw_die_ref type_die;
15141 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15142 if (type_die == NULL)
15143 return NULL;
15144 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15145 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15146 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15147 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15148 mem_loc_result->dw_loc_oprnd2.val_class
15149 = dw_val_class_wide_int;
15150 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15151 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15153 break;
15155 case EQ:
15156 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15157 break;
15159 case GE:
15160 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15161 break;
15163 case GT:
15164 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15165 break;
15167 case LE:
15168 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15169 break;
15171 case LT:
15172 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15173 break;
15175 case NE:
15176 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15177 break;
15179 case GEU:
15180 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15181 break;
15183 case GTU:
15184 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15185 break;
15187 case LEU:
15188 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15189 break;
15191 case LTU:
15192 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15193 break;
15195 case UMIN:
15196 case UMAX:
15197 if (!SCALAR_INT_MODE_P (mode))
15198 break;
15199 /* FALLTHRU */
15200 case SMIN:
15201 case SMAX:
15202 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15203 break;
15205 case ZERO_EXTRACT:
15206 case SIGN_EXTRACT:
15207 if (CONST_INT_P (XEXP (rtl, 1))
15208 && CONST_INT_P (XEXP (rtl, 2))
15209 && ((unsigned) INTVAL (XEXP (rtl, 1))
15210 + (unsigned) INTVAL (XEXP (rtl, 2))
15211 <= GET_MODE_BITSIZE (mode))
15212 && SCALAR_INT_MODE_P (mode)
15213 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15214 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15216 int shift, size;
15217 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15218 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15219 if (op0 == 0)
15220 break;
15221 if (GET_CODE (rtl) == SIGN_EXTRACT)
15222 op = DW_OP_shra;
15223 else
15224 op = DW_OP_shr;
15225 mem_loc_result = op0;
15226 size = INTVAL (XEXP (rtl, 1));
15227 shift = INTVAL (XEXP (rtl, 2));
15228 if (BITS_BIG_ENDIAN)
15229 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15230 - shift - size;
15231 if (shift + size != (int) DWARF2_ADDR_SIZE)
15233 add_loc_descr (&mem_loc_result,
15234 int_loc_descriptor (DWARF2_ADDR_SIZE
15235 - shift - size));
15236 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15238 if (size != (int) DWARF2_ADDR_SIZE)
15240 add_loc_descr (&mem_loc_result,
15241 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15242 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15245 break;
15247 case IF_THEN_ELSE:
15249 dw_loc_descr_ref op2, bra_node, drop_node;
15250 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15251 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15252 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15253 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15254 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15255 VAR_INIT_STATUS_INITIALIZED);
15256 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15257 VAR_INIT_STATUS_INITIALIZED);
15258 if (op0 == NULL || op1 == NULL || op2 == NULL)
15259 break;
15261 mem_loc_result = op1;
15262 add_loc_descr (&mem_loc_result, op2);
15263 add_loc_descr (&mem_loc_result, op0);
15264 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15265 add_loc_descr (&mem_loc_result, bra_node);
15266 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15267 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15268 add_loc_descr (&mem_loc_result, drop_node);
15269 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15270 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15272 break;
15274 case FLOAT_EXTEND:
15275 case FLOAT_TRUNCATE:
15276 case FLOAT:
15277 case UNSIGNED_FLOAT:
15278 case FIX:
15279 case UNSIGNED_FIX:
15280 if (!dwarf_strict || dwarf_version >= 5)
15282 dw_die_ref type_die;
15283 dw_loc_descr_ref cvt;
15285 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15286 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15287 if (op0 == NULL)
15288 break;
15289 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15290 && (GET_CODE (rtl) == FLOAT
15291 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15292 <= DWARF2_ADDR_SIZE))
15294 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15295 GET_CODE (rtl) == UNSIGNED_FLOAT);
15296 if (type_die == NULL)
15297 break;
15298 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15299 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15300 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15301 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15302 add_loc_descr (&op0, cvt);
15304 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15305 if (type_die == NULL)
15306 break;
15307 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15308 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15309 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15310 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15311 add_loc_descr (&op0, cvt);
15312 if (SCALAR_INT_MODE_P (mode)
15313 && (GET_CODE (rtl) == FIX
15314 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15316 op0 = convert_descriptor_to_mode (mode, op0);
15317 if (op0 == NULL)
15318 break;
15320 mem_loc_result = op0;
15322 break;
15324 case CLZ:
15325 case CTZ:
15326 case FFS:
15327 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15328 break;
15330 case POPCOUNT:
15331 case PARITY:
15332 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15333 break;
15335 case BSWAP:
15336 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15337 break;
15339 case ROTATE:
15340 case ROTATERT:
15341 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15342 break;
15344 case COMPARE:
15345 /* In theory, we could implement the above. */
15346 /* DWARF cannot represent the unsigned compare operations
15347 natively. */
15348 case SS_MULT:
15349 case US_MULT:
15350 case SS_DIV:
15351 case US_DIV:
15352 case SS_PLUS:
15353 case US_PLUS:
15354 case SS_MINUS:
15355 case US_MINUS:
15356 case SS_NEG:
15357 case US_NEG:
15358 case SS_ABS:
15359 case SS_ASHIFT:
15360 case US_ASHIFT:
15361 case SS_TRUNCATE:
15362 case US_TRUNCATE:
15363 case UNORDERED:
15364 case ORDERED:
15365 case UNEQ:
15366 case UNGE:
15367 case UNGT:
15368 case UNLE:
15369 case UNLT:
15370 case LTGT:
15371 case FRACT_CONVERT:
15372 case UNSIGNED_FRACT_CONVERT:
15373 case SAT_FRACT:
15374 case UNSIGNED_SAT_FRACT:
15375 case SQRT:
15376 case ASM_OPERANDS:
15377 case VEC_MERGE:
15378 case VEC_SELECT:
15379 case VEC_CONCAT:
15380 case VEC_DUPLICATE:
15381 case UNSPEC:
15382 case HIGH:
15383 case FMA:
15384 case STRICT_LOW_PART:
15385 case CONST_VECTOR:
15386 case CONST_FIXED:
15387 case CLRSB:
15388 case CLOBBER:
15389 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15390 can't express it in the debug info. This can happen e.g. with some
15391 TLS UNSPECs. */
15392 break;
15394 case CONST_STRING:
15395 resolve_one_addr (&rtl);
15396 goto symref;
15398 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15399 the expression. An UNSPEC rtx represents a raw DWARF operation,
15400 new_loc_descr is called for it to build the operation directly.
15401 Otherwise mem_loc_descriptor is called recursively. */
15402 case PARALLEL:
15404 int index = 0;
15405 dw_loc_descr_ref exp_result = NULL;
15407 for (; index < XVECLEN (rtl, 0); index++)
15409 rtx elem = XVECEXP (rtl, 0, index);
15410 if (GET_CODE (elem) == UNSPEC)
15412 /* Each DWARF operation UNSPEC contain two operands, if
15413 one operand is not used for the operation, const0_rtx is
15414 passed. */
15415 gcc_assert (XVECLEN (elem, 0) == 2);
15417 HOST_WIDE_INT dw_op = XINT (elem, 1);
15418 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15419 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15420 exp_result
15421 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15422 oprnd2);
15424 else
15425 exp_result
15426 = mem_loc_descriptor (elem, mode, mem_mode,
15427 VAR_INIT_STATUS_INITIALIZED);
15429 if (!mem_loc_result)
15430 mem_loc_result = exp_result;
15431 else
15432 add_loc_descr (&mem_loc_result, exp_result);
15435 break;
15438 default:
15439 if (flag_checking)
15441 print_rtl (stderr, rtl);
15442 gcc_unreachable ();
15444 break;
15447 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15448 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15450 return mem_loc_result;
15453 /* Return a descriptor that describes the concatenation of two locations.
15454 This is typically a complex variable. */
15456 static dw_loc_descr_ref
15457 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15459 dw_loc_descr_ref cc_loc_result = NULL;
15460 dw_loc_descr_ref x0_ref
15461 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15462 dw_loc_descr_ref x1_ref
15463 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15465 if (x0_ref == 0 || x1_ref == 0)
15466 return 0;
15468 cc_loc_result = x0_ref;
15469 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15471 add_loc_descr (&cc_loc_result, x1_ref);
15472 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15474 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15475 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15477 return cc_loc_result;
15480 /* Return a descriptor that describes the concatenation of N
15481 locations. */
15483 static dw_loc_descr_ref
15484 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15486 unsigned int i;
15487 dw_loc_descr_ref cc_loc_result = NULL;
15488 unsigned int n = XVECLEN (concatn, 0);
15490 for (i = 0; i < n; ++i)
15492 dw_loc_descr_ref ref;
15493 rtx x = XVECEXP (concatn, 0, i);
15495 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15496 if (ref == NULL)
15497 return NULL;
15499 add_loc_descr (&cc_loc_result, ref);
15500 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15503 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15504 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15506 return cc_loc_result;
15509 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15510 for DEBUG_IMPLICIT_PTR RTL. */
15512 static dw_loc_descr_ref
15513 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15515 dw_loc_descr_ref ret;
15516 dw_die_ref ref;
15518 if (dwarf_strict && dwarf_version < 5)
15519 return NULL;
15520 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15521 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15522 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15523 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15524 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15525 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15526 if (ref)
15528 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15529 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15530 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15532 else
15534 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15535 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15537 return ret;
15540 /* Output a proper Dwarf location descriptor for a variable or parameter
15541 which is either allocated in a register or in a memory location. For a
15542 register, we just generate an OP_REG and the register number. For a
15543 memory location we provide a Dwarf postfix expression describing how to
15544 generate the (dynamic) address of the object onto the address stack.
15546 MODE is mode of the decl if this loc_descriptor is going to be used in
15547 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15548 allowed, VOIDmode otherwise.
15550 If we don't know how to describe it, return 0. */
15552 static dw_loc_descr_ref
15553 loc_descriptor (rtx rtl, machine_mode mode,
15554 enum var_init_status initialized)
15556 dw_loc_descr_ref loc_result = NULL;
15558 switch (GET_CODE (rtl))
15560 case SUBREG:
15561 /* The case of a subreg may arise when we have a local (register)
15562 variable or a formal (register) parameter which doesn't quite fill
15563 up an entire register. For now, just assume that it is
15564 legitimate to make the Dwarf info refer to the whole register which
15565 contains the given subreg. */
15566 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15567 loc_result = loc_descriptor (SUBREG_REG (rtl),
15568 GET_MODE (SUBREG_REG (rtl)), initialized);
15569 else
15570 goto do_default;
15571 break;
15573 case REG:
15574 loc_result = reg_loc_descriptor (rtl, initialized);
15575 break;
15577 case MEM:
15578 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15579 GET_MODE (rtl), initialized);
15580 if (loc_result == NULL)
15581 loc_result = tls_mem_loc_descriptor (rtl);
15582 if (loc_result == NULL)
15584 rtx new_rtl = avoid_constant_pool_reference (rtl);
15585 if (new_rtl != rtl)
15586 loc_result = loc_descriptor (new_rtl, mode, initialized);
15588 break;
15590 case CONCAT:
15591 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15592 initialized);
15593 break;
15595 case CONCATN:
15596 loc_result = concatn_loc_descriptor (rtl, initialized);
15597 break;
15599 case VAR_LOCATION:
15600 /* Single part. */
15601 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15603 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15604 if (GET_CODE (loc) == EXPR_LIST)
15605 loc = XEXP (loc, 0);
15606 loc_result = loc_descriptor (loc, mode, initialized);
15607 break;
15610 rtl = XEXP (rtl, 1);
15611 /* FALLTHRU */
15613 case PARALLEL:
15615 rtvec par_elems = XVEC (rtl, 0);
15616 int num_elem = GET_NUM_ELEM (par_elems);
15617 machine_mode mode;
15618 int i;
15620 /* Create the first one, so we have something to add to. */
15621 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15622 VOIDmode, initialized);
15623 if (loc_result == NULL)
15624 return NULL;
15625 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15626 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15627 for (i = 1; i < num_elem; i++)
15629 dw_loc_descr_ref temp;
15631 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15632 VOIDmode, initialized);
15633 if (temp == NULL)
15634 return NULL;
15635 add_loc_descr (&loc_result, temp);
15636 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15637 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15640 break;
15642 case CONST_INT:
15643 if (mode != VOIDmode && mode != BLKmode)
15644 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15645 INTVAL (rtl));
15646 break;
15648 case CONST_DOUBLE:
15649 if (mode == VOIDmode)
15650 mode = GET_MODE (rtl);
15652 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15654 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15656 /* Note that a CONST_DOUBLE rtx could represent either an integer
15657 or a floating-point constant. A CONST_DOUBLE is used whenever
15658 the constant requires more than one word in order to be
15659 adequately represented. We output CONST_DOUBLEs as blocks. */
15660 loc_result = new_loc_descr (DW_OP_implicit_value,
15661 GET_MODE_SIZE (mode), 0);
15662 #if TARGET_SUPPORTS_WIDE_INT == 0
15663 if (!SCALAR_FLOAT_MODE_P (mode))
15665 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15666 loc_result->dw_loc_oprnd2.v.val_double
15667 = rtx_to_double_int (rtl);
15669 else
15670 #endif
15672 unsigned int length = GET_MODE_SIZE (mode);
15673 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15675 insert_float (rtl, array);
15676 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15677 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15678 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15679 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15682 break;
15684 case CONST_WIDE_INT:
15685 if (mode == VOIDmode)
15686 mode = GET_MODE (rtl);
15688 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15690 loc_result = new_loc_descr (DW_OP_implicit_value,
15691 GET_MODE_SIZE (mode), 0);
15692 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15693 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15694 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15696 break;
15698 case CONST_VECTOR:
15699 if (mode == VOIDmode)
15700 mode = GET_MODE (rtl);
15702 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15704 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15705 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15706 unsigned char *array
15707 = ggc_vec_alloc<unsigned char> (length * elt_size);
15708 unsigned int i;
15709 unsigned char *p;
15710 machine_mode imode = GET_MODE_INNER (mode);
15712 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15713 switch (GET_MODE_CLASS (mode))
15715 case MODE_VECTOR_INT:
15716 for (i = 0, p = array; i < length; i++, p += elt_size)
15718 rtx elt = CONST_VECTOR_ELT (rtl, i);
15719 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15721 break;
15723 case MODE_VECTOR_FLOAT:
15724 for (i = 0, p = array; i < length; i++, p += elt_size)
15726 rtx elt = CONST_VECTOR_ELT (rtl, i);
15727 insert_float (elt, p);
15729 break;
15731 default:
15732 gcc_unreachable ();
15735 loc_result = new_loc_descr (DW_OP_implicit_value,
15736 length * elt_size, 0);
15737 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15738 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15739 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15740 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15742 break;
15744 case CONST:
15745 if (mode == VOIDmode
15746 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15747 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15748 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15750 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15751 break;
15753 /* FALLTHROUGH */
15754 case SYMBOL_REF:
15755 if (!const_ok_for_output (rtl))
15756 break;
15757 /* FALLTHROUGH */
15758 case LABEL_REF:
15759 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15760 && (dwarf_version >= 4 || !dwarf_strict))
15762 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15763 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15764 vec_safe_push (used_rtx_array, rtl);
15766 break;
15768 case DEBUG_IMPLICIT_PTR:
15769 loc_result = implicit_ptr_descriptor (rtl, 0);
15770 break;
15772 case PLUS:
15773 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15774 && CONST_INT_P (XEXP (rtl, 1)))
15776 loc_result
15777 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15778 break;
15780 /* FALLTHRU */
15781 do_default:
15782 default:
15783 if ((SCALAR_INT_MODE_P (mode)
15784 && GET_MODE (rtl) == mode
15785 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15786 && dwarf_version >= 4)
15787 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15789 /* Value expression. */
15790 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15791 if (loc_result)
15792 add_loc_descr (&loc_result,
15793 new_loc_descr (DW_OP_stack_value, 0, 0));
15795 break;
15798 return loc_result;
15801 /* We need to figure out what section we should use as the base for the
15802 address ranges where a given location is valid.
15803 1. If this particular DECL has a section associated with it, use that.
15804 2. If this function has a section associated with it, use that.
15805 3. Otherwise, use the text section.
15806 XXX: If you split a variable across multiple sections, we won't notice. */
15808 static const char *
15809 secname_for_decl (const_tree decl)
15811 const char *secname;
15813 if (VAR_OR_FUNCTION_DECL_P (decl)
15814 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15815 && DECL_SECTION_NAME (decl))
15816 secname = DECL_SECTION_NAME (decl);
15817 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15818 secname = DECL_SECTION_NAME (current_function_decl);
15819 else if (cfun && in_cold_section_p)
15820 secname = crtl->subsections.cold_section_label;
15821 else
15822 secname = text_section_label;
15824 return secname;
15827 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15829 static bool
15830 decl_by_reference_p (tree decl)
15832 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15833 || VAR_P (decl))
15834 && DECL_BY_REFERENCE (decl));
15837 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15838 for VARLOC. */
15840 static dw_loc_descr_ref
15841 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15842 enum var_init_status initialized)
15844 int have_address = 0;
15845 dw_loc_descr_ref descr;
15846 machine_mode mode;
15848 if (want_address != 2)
15850 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15851 /* Single part. */
15852 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15854 varloc = PAT_VAR_LOCATION_LOC (varloc);
15855 if (GET_CODE (varloc) == EXPR_LIST)
15856 varloc = XEXP (varloc, 0);
15857 mode = GET_MODE (varloc);
15858 if (MEM_P (varloc))
15860 rtx addr = XEXP (varloc, 0);
15861 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15862 mode, initialized);
15863 if (descr)
15864 have_address = 1;
15865 else
15867 rtx x = avoid_constant_pool_reference (varloc);
15868 if (x != varloc)
15869 descr = mem_loc_descriptor (x, mode, VOIDmode,
15870 initialized);
15873 else
15874 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15876 else
15877 return 0;
15879 else
15881 if (GET_CODE (varloc) == VAR_LOCATION)
15882 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15883 else
15884 mode = DECL_MODE (loc);
15885 descr = loc_descriptor (varloc, mode, initialized);
15886 have_address = 1;
15889 if (!descr)
15890 return 0;
15892 if (want_address == 2 && !have_address
15893 && (dwarf_version >= 4 || !dwarf_strict))
15895 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15897 expansion_failed (loc, NULL_RTX,
15898 "DWARF address size mismatch");
15899 return 0;
15901 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15902 have_address = 1;
15904 /* Show if we can't fill the request for an address. */
15905 if (want_address && !have_address)
15907 expansion_failed (loc, NULL_RTX,
15908 "Want address and only have value");
15909 return 0;
15912 /* If we've got an address and don't want one, dereference. */
15913 if (!want_address && have_address)
15915 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15916 enum dwarf_location_atom op;
15918 if (size > DWARF2_ADDR_SIZE || size == -1)
15920 expansion_failed (loc, NULL_RTX,
15921 "DWARF address size mismatch");
15922 return 0;
15924 else if (size == DWARF2_ADDR_SIZE)
15925 op = DW_OP_deref;
15926 else
15927 op = DW_OP_deref_size;
15929 add_loc_descr (&descr, new_loc_descr (op, size, 0));
15932 return descr;
15935 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15936 if it is not possible. */
15938 static dw_loc_descr_ref
15939 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15941 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15942 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15943 else if (dwarf_version >= 3 || !dwarf_strict)
15944 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15945 else
15946 return NULL;
15949 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15950 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
15952 static dw_loc_descr_ref
15953 dw_sra_loc_expr (tree decl, rtx loc)
15955 rtx p;
15956 unsigned HOST_WIDE_INT padsize = 0;
15957 dw_loc_descr_ref descr, *descr_tail;
15958 unsigned HOST_WIDE_INT decl_size;
15959 rtx varloc;
15960 enum var_init_status initialized;
15962 if (DECL_SIZE (decl) == NULL
15963 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
15964 return NULL;
15966 decl_size = tree_to_uhwi (DECL_SIZE (decl));
15967 descr = NULL;
15968 descr_tail = &descr;
15970 for (p = loc; p; p = XEXP (p, 1))
15972 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
15973 rtx loc_note = *decl_piece_varloc_ptr (p);
15974 dw_loc_descr_ref cur_descr;
15975 dw_loc_descr_ref *tail, last = NULL;
15976 unsigned HOST_WIDE_INT opsize = 0;
15978 if (loc_note == NULL_RTX
15979 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15981 padsize += bitsize;
15982 continue;
15984 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15985 varloc = NOTE_VAR_LOCATION (loc_note);
15986 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15987 if (cur_descr == NULL)
15989 padsize += bitsize;
15990 continue;
15993 /* Check that cur_descr either doesn't use
15994 DW_OP_*piece operations, or their sum is equal
15995 to bitsize. Otherwise we can't embed it. */
15996 for (tail = &cur_descr; *tail != NULL;
15997 tail = &(*tail)->dw_loc_next)
15998 if ((*tail)->dw_loc_opc == DW_OP_piece)
16000 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16001 * BITS_PER_UNIT;
16002 last = *tail;
16004 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16006 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16007 last = *tail;
16010 if (last != NULL && opsize != bitsize)
16012 padsize += bitsize;
16013 /* Discard the current piece of the descriptor and release any
16014 addr_table entries it uses. */
16015 remove_loc_list_addr_table_entries (cur_descr);
16016 continue;
16019 /* If there is a hole, add DW_OP_*piece after empty DWARF
16020 expression, which means that those bits are optimized out. */
16021 if (padsize)
16023 if (padsize > decl_size)
16025 remove_loc_list_addr_table_entries (cur_descr);
16026 goto discard_descr;
16028 decl_size -= padsize;
16029 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16030 if (*descr_tail == NULL)
16032 remove_loc_list_addr_table_entries (cur_descr);
16033 goto discard_descr;
16035 descr_tail = &(*descr_tail)->dw_loc_next;
16036 padsize = 0;
16038 *descr_tail = cur_descr;
16039 descr_tail = tail;
16040 if (bitsize > decl_size)
16041 goto discard_descr;
16042 decl_size -= bitsize;
16043 if (last == NULL)
16045 HOST_WIDE_INT offset = 0;
16046 if (GET_CODE (varloc) == VAR_LOCATION
16047 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16049 varloc = PAT_VAR_LOCATION_LOC (varloc);
16050 if (GET_CODE (varloc) == EXPR_LIST)
16051 varloc = XEXP (varloc, 0);
16055 if (GET_CODE (varloc) == CONST
16056 || GET_CODE (varloc) == SIGN_EXTEND
16057 || GET_CODE (varloc) == ZERO_EXTEND)
16058 varloc = XEXP (varloc, 0);
16059 else if (GET_CODE (varloc) == SUBREG)
16060 varloc = SUBREG_REG (varloc);
16061 else
16062 break;
16064 while (1);
16065 /* DW_OP_bit_size offset should be zero for register
16066 or implicit location descriptions and empty location
16067 descriptions, but for memory addresses needs big endian
16068 adjustment. */
16069 if (MEM_P (varloc))
16071 unsigned HOST_WIDE_INT memsize
16072 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16073 if (memsize != bitsize)
16075 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16076 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16077 goto discard_descr;
16078 if (memsize < bitsize)
16079 goto discard_descr;
16080 if (BITS_BIG_ENDIAN)
16081 offset = memsize - bitsize;
16085 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16086 if (*descr_tail == NULL)
16087 goto discard_descr;
16088 descr_tail = &(*descr_tail)->dw_loc_next;
16092 /* If there were any non-empty expressions, add padding till the end of
16093 the decl. */
16094 if (descr != NULL && decl_size != 0)
16096 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16097 if (*descr_tail == NULL)
16098 goto discard_descr;
16100 return descr;
16102 discard_descr:
16103 /* Discard the descriptor and release any addr_table entries it uses. */
16104 remove_loc_list_addr_table_entries (descr);
16105 return NULL;
16108 /* Return the dwarf representation of the location list LOC_LIST of
16109 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16110 function. */
16112 static dw_loc_list_ref
16113 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16115 const char *endname, *secname;
16116 rtx varloc;
16117 enum var_init_status initialized;
16118 struct var_loc_node *node;
16119 dw_loc_descr_ref descr;
16120 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16121 dw_loc_list_ref list = NULL;
16122 dw_loc_list_ref *listp = &list;
16124 /* Now that we know what section we are using for a base,
16125 actually construct the list of locations.
16126 The first location information is what is passed to the
16127 function that creates the location list, and the remaining
16128 locations just get added on to that list.
16129 Note that we only know the start address for a location
16130 (IE location changes), so to build the range, we use
16131 the range [current location start, next location start].
16132 This means we have to special case the last node, and generate
16133 a range of [last location start, end of function label]. */
16135 secname = secname_for_decl (decl);
16137 for (node = loc_list->first; node; node = node->next)
16138 if (GET_CODE (node->loc) == EXPR_LIST
16139 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16141 if (GET_CODE (node->loc) == EXPR_LIST)
16143 /* This requires DW_OP_{,bit_}piece, which is not usable
16144 inside DWARF expressions. */
16145 if (want_address != 2)
16146 continue;
16147 descr = dw_sra_loc_expr (decl, node->loc);
16148 if (descr == NULL)
16149 continue;
16151 else
16153 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16154 varloc = NOTE_VAR_LOCATION (node->loc);
16155 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16157 if (descr)
16159 bool range_across_switch = false;
16160 /* If section switch happens in between node->label
16161 and node->next->label (or end of function) and
16162 we can't emit it as a single entry list,
16163 emit two ranges, first one ending at the end
16164 of first partition and second one starting at the
16165 beginning of second partition. */
16166 if (node == loc_list->last_before_switch
16167 && (node != loc_list->first || loc_list->first->next)
16168 && current_function_decl)
16170 endname = cfun->fde->dw_fde_end;
16171 range_across_switch = true;
16173 /* The variable has a location between NODE->LABEL and
16174 NODE->NEXT->LABEL. */
16175 else if (node->next)
16176 endname = node->next->label;
16177 /* If the variable has a location at the last label
16178 it keeps its location until the end of function. */
16179 else if (!current_function_decl)
16180 endname = text_end_label;
16181 else
16183 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16184 current_function_funcdef_no);
16185 endname = ggc_strdup (label_id);
16188 *listp = new_loc_list (descr, node->label, endname, secname);
16189 if (TREE_CODE (decl) == PARM_DECL
16190 && node == loc_list->first
16191 && NOTE_P (node->loc)
16192 && strcmp (node->label, endname) == 0)
16193 (*listp)->force = true;
16194 listp = &(*listp)->dw_loc_next;
16196 if (range_across_switch)
16198 if (GET_CODE (node->loc) == EXPR_LIST)
16199 descr = dw_sra_loc_expr (decl, node->loc);
16200 else
16202 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16203 varloc = NOTE_VAR_LOCATION (node->loc);
16204 descr = dw_loc_list_1 (decl, varloc, want_address,
16205 initialized);
16207 gcc_assert (descr);
16208 /* The variable has a location between NODE->LABEL and
16209 NODE->NEXT->LABEL. */
16210 if (node->next)
16211 endname = node->next->label;
16212 else
16213 endname = cfun->fde->dw_fde_second_end;
16214 *listp = new_loc_list (descr,
16215 cfun->fde->dw_fde_second_begin,
16216 endname, secname);
16217 listp = &(*listp)->dw_loc_next;
16222 /* Try to avoid the overhead of a location list emitting a location
16223 expression instead, but only if we didn't have more than one
16224 location entry in the first place. If some entries were not
16225 representable, we don't want to pretend a single entry that was
16226 applies to the entire scope in which the variable is
16227 available. */
16228 if (list && loc_list->first->next)
16229 gen_llsym (list);
16231 return list;
16234 /* Return if the loc_list has only single element and thus can be represented
16235 as location description. */
16237 static bool
16238 single_element_loc_list_p (dw_loc_list_ref list)
16240 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16241 return !list->ll_symbol;
16244 /* To each location in list LIST add loc descr REF. */
16246 static void
16247 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16249 dw_loc_descr_ref copy;
16250 add_loc_descr (&list->expr, ref);
16251 list = list->dw_loc_next;
16252 while (list)
16254 copy = ggc_alloc<dw_loc_descr_node> ();
16255 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16256 add_loc_descr (&list->expr, copy);
16257 while (copy->dw_loc_next)
16259 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
16260 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16261 copy->dw_loc_next = new_copy;
16262 copy = new_copy;
16264 list = list->dw_loc_next;
16268 /* Given two lists RET and LIST
16269 produce location list that is result of adding expression in LIST
16270 to expression in RET on each position in program.
16271 Might be destructive on both RET and LIST.
16273 TODO: We handle only simple cases of RET or LIST having at most one
16274 element. General case would inolve sorting the lists in program order
16275 and merging them that will need some additional work.
16276 Adding that will improve quality of debug info especially for SRA-ed
16277 structures. */
16279 static void
16280 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16282 if (!list)
16283 return;
16284 if (!*ret)
16286 *ret = list;
16287 return;
16289 if (!list->dw_loc_next)
16291 add_loc_descr_to_each (*ret, list->expr);
16292 return;
16294 if (!(*ret)->dw_loc_next)
16296 add_loc_descr_to_each (list, (*ret)->expr);
16297 *ret = list;
16298 return;
16300 expansion_failed (NULL_TREE, NULL_RTX,
16301 "Don't know how to merge two non-trivial"
16302 " location lists.\n");
16303 *ret = NULL;
16304 return;
16307 /* LOC is constant expression. Try a luck, look it up in constant
16308 pool and return its loc_descr of its address. */
16310 static dw_loc_descr_ref
16311 cst_pool_loc_descr (tree loc)
16313 /* Get an RTL for this, if something has been emitted. */
16314 rtx rtl = lookup_constant_def (loc);
16316 if (!rtl || !MEM_P (rtl))
16318 gcc_assert (!rtl);
16319 return 0;
16321 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16323 /* TODO: We might get more coverage if we was actually delaying expansion
16324 of all expressions till end of compilation when constant pools are fully
16325 populated. */
16326 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16328 expansion_failed (loc, NULL_RTX,
16329 "CST value in contant pool but not marked.");
16330 return 0;
16332 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16333 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16336 /* Return dw_loc_list representing address of addr_expr LOC
16337 by looking for inner INDIRECT_REF expression and turning
16338 it into simple arithmetics.
16340 See loc_list_from_tree for the meaning of CONTEXT. */
16342 static dw_loc_list_ref
16343 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16344 loc_descr_context *context)
16346 tree obj, offset;
16347 HOST_WIDE_INT bitsize, bitpos, bytepos;
16348 machine_mode mode;
16349 int unsignedp, reversep, volatilep = 0;
16350 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16352 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16353 &bitsize, &bitpos, &offset, &mode,
16354 &unsignedp, &reversep, &volatilep);
16355 STRIP_NOPS (obj);
16356 if (bitpos % BITS_PER_UNIT)
16358 expansion_failed (loc, NULL_RTX, "bitfield access");
16359 return 0;
16361 if (!INDIRECT_REF_P (obj))
16363 expansion_failed (obj,
16364 NULL_RTX, "no indirect ref in inner refrence");
16365 return 0;
16367 if (!offset && !bitpos)
16368 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16369 context);
16370 else if (toplev
16371 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16372 && (dwarf_version >= 4 || !dwarf_strict))
16374 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16375 if (!list_ret)
16376 return 0;
16377 if (offset)
16379 /* Variable offset. */
16380 list_ret1 = loc_list_from_tree (offset, 0, context);
16381 if (list_ret1 == 0)
16382 return 0;
16383 add_loc_list (&list_ret, list_ret1);
16384 if (!list_ret)
16385 return 0;
16386 add_loc_descr_to_each (list_ret,
16387 new_loc_descr (DW_OP_plus, 0, 0));
16389 bytepos = bitpos / BITS_PER_UNIT;
16390 if (bytepos > 0)
16391 add_loc_descr_to_each (list_ret,
16392 new_loc_descr (DW_OP_plus_uconst,
16393 bytepos, 0));
16394 else if (bytepos < 0)
16395 loc_list_plus_const (list_ret, bytepos);
16396 add_loc_descr_to_each (list_ret,
16397 new_loc_descr (DW_OP_stack_value, 0, 0));
16399 return list_ret;
16402 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16403 all operations from LOC are nops, move to the last one. Insert in NOPS all
16404 operations that are skipped. */
16406 static void
16407 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16408 hash_set<dw_loc_descr_ref> &nops)
16410 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16412 nops.add (loc);
16413 loc = loc->dw_loc_next;
16417 /* Helper for loc_descr_without_nops: free the location description operation
16418 P. */
16420 bool
16421 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16423 ggc_free (loc);
16424 return true;
16427 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16428 finishes LOC. */
16430 static void
16431 loc_descr_without_nops (dw_loc_descr_ref &loc)
16433 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16434 return;
16436 /* Set of all DW_OP_nop operations we remove. */
16437 hash_set<dw_loc_descr_ref> nops;
16439 /* First, strip all prefix NOP operations in order to keep the head of the
16440 operations list. */
16441 loc_descr_to_next_no_nop (loc, nops);
16443 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16445 /* For control flow operations: strip "prefix" nops in destination
16446 labels. */
16447 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16448 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16449 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16450 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16452 /* Do the same for the operations that follow, then move to the next
16453 iteration. */
16454 if (cur->dw_loc_next != NULL)
16455 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16456 cur = cur->dw_loc_next;
16459 nops.traverse<void *, free_loc_descr> (NULL);
16463 struct dwarf_procedure_info;
16465 /* Helper structure for location descriptions generation. */
16466 struct loc_descr_context
16468 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16469 NULL_TREE if DW_OP_push_object_address in invalid for this location
16470 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16471 tree context_type;
16472 /* The ..._DECL node that should be translated as a
16473 DW_OP_push_object_address operation. */
16474 tree base_decl;
16475 /* Information about the DWARF procedure we are currently generating. NULL if
16476 we are not generating a DWARF procedure. */
16477 struct dwarf_procedure_info *dpi;
16478 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16479 by consumer. Used for DW_TAG_generic_subrange attributes. */
16480 bool placeholder_arg;
16481 /* True if PLACEHOLDER_EXPR has been seen. */
16482 bool placeholder_seen;
16485 /* DWARF procedures generation
16487 DWARF expressions (aka. location descriptions) are used to encode variable
16488 things such as sizes or offsets. Such computations can have redundant parts
16489 that can be factorized in order to reduce the size of the output debug
16490 information. This is the whole point of DWARF procedures.
16492 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16493 already factorized into functions ("size functions") in order to handle very
16494 big and complex types. Such functions are quite simple: they have integral
16495 arguments, they return an integral result and their body contains only a
16496 return statement with arithmetic expressions. This is the only kind of
16497 function we are interested in translating into DWARF procedures, here.
16499 DWARF expressions and DWARF procedure are executed using a stack, so we have
16500 to define some calling convention for them to interact. Let's say that:
16502 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16503 all arguments in reverse order (right-to-left) so that when the DWARF
16504 procedure execution starts, the first argument is the top of the stack.
16506 - Then, when returning, the DWARF procedure must have consumed all arguments
16507 on the stack, must have pushed the result and touched nothing else.
16509 - Each integral argument and the result are integral types can be hold in a
16510 single stack slot.
16512 - We call "frame offset" the number of stack slots that are "under DWARF
16513 procedure control": it includes the arguments slots, the temporaries and
16514 the result slot. Thus, it is equal to the number of arguments when the
16515 procedure execution starts and must be equal to one (the result) when it
16516 returns. */
16518 /* Helper structure used when generating operations for a DWARF procedure. */
16519 struct dwarf_procedure_info
16521 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16522 currently translated. */
16523 tree fndecl;
16524 /* The number of arguments FNDECL takes. */
16525 unsigned args_count;
16528 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16529 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16530 equate it to this DIE. */
16532 static dw_die_ref
16533 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16534 dw_die_ref parent_die)
16536 dw_die_ref dwarf_proc_die;
16538 if ((dwarf_version < 3 && dwarf_strict)
16539 || location == NULL)
16540 return NULL;
16542 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16543 if (fndecl)
16544 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16545 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16546 return dwarf_proc_die;
16549 /* Return whether TYPE is a supported type as a DWARF procedure argument
16550 type or return type (we handle only scalar types and pointer types that
16551 aren't wider than the DWARF expression evaluation stack. */
16553 static bool
16554 is_handled_procedure_type (tree type)
16556 return ((INTEGRAL_TYPE_P (type)
16557 || TREE_CODE (type) == OFFSET_TYPE
16558 || TREE_CODE (type) == POINTER_TYPE)
16559 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16562 /* Helper for resolve_args_picking: do the same but stop when coming across
16563 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16564 offset *before* evaluating the corresponding operation. */
16566 static bool
16567 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16568 struct dwarf_procedure_info *dpi,
16569 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16571 /* The "frame_offset" identifier is already used to name a macro... */
16572 unsigned frame_offset_ = initial_frame_offset;
16573 dw_loc_descr_ref l;
16575 for (l = loc; l != NULL;)
16577 bool existed;
16578 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16580 /* If we already met this node, there is nothing to compute anymore. */
16581 if (existed)
16583 /* Make sure that the stack size is consistent wherever the execution
16584 flow comes from. */
16585 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16586 break;
16588 l_frame_offset = frame_offset_;
16590 /* If needed, relocate the picking offset with respect to the frame
16591 offset. */
16592 if (l->frame_offset_rel)
16594 unsigned HOST_WIDE_INT off;
16595 switch (l->dw_loc_opc)
16597 case DW_OP_pick:
16598 off = l->dw_loc_oprnd1.v.val_unsigned;
16599 break;
16600 case DW_OP_dup:
16601 off = 0;
16602 break;
16603 case DW_OP_over:
16604 off = 1;
16605 break;
16606 default:
16607 gcc_unreachable ();
16609 /* frame_offset_ is the size of the current stack frame, including
16610 incoming arguments. Besides, the arguments are pushed
16611 right-to-left. Thus, in order to access the Nth argument from
16612 this operation node, the picking has to skip temporaries *plus*
16613 one stack slot per argument (0 for the first one, 1 for the second
16614 one, etc.).
16616 The targetted argument number (N) is already set as the operand,
16617 and the number of temporaries can be computed with:
16618 frame_offsets_ - dpi->args_count */
16619 off += frame_offset_ - dpi->args_count;
16621 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16622 if (off > 255)
16623 return false;
16625 if (off == 0)
16627 l->dw_loc_opc = DW_OP_dup;
16628 l->dw_loc_oprnd1.v.val_unsigned = 0;
16630 else if (off == 1)
16632 l->dw_loc_opc = DW_OP_over;
16633 l->dw_loc_oprnd1.v.val_unsigned = 0;
16635 else
16637 l->dw_loc_opc = DW_OP_pick;
16638 l->dw_loc_oprnd1.v.val_unsigned = off;
16642 /* Update frame_offset according to the effect the current operation has
16643 on the stack. */
16644 switch (l->dw_loc_opc)
16646 case DW_OP_deref:
16647 case DW_OP_swap:
16648 case DW_OP_rot:
16649 case DW_OP_abs:
16650 case DW_OP_neg:
16651 case DW_OP_not:
16652 case DW_OP_plus_uconst:
16653 case DW_OP_skip:
16654 case DW_OP_reg0:
16655 case DW_OP_reg1:
16656 case DW_OP_reg2:
16657 case DW_OP_reg3:
16658 case DW_OP_reg4:
16659 case DW_OP_reg5:
16660 case DW_OP_reg6:
16661 case DW_OP_reg7:
16662 case DW_OP_reg8:
16663 case DW_OP_reg9:
16664 case DW_OP_reg10:
16665 case DW_OP_reg11:
16666 case DW_OP_reg12:
16667 case DW_OP_reg13:
16668 case DW_OP_reg14:
16669 case DW_OP_reg15:
16670 case DW_OP_reg16:
16671 case DW_OP_reg17:
16672 case DW_OP_reg18:
16673 case DW_OP_reg19:
16674 case DW_OP_reg20:
16675 case DW_OP_reg21:
16676 case DW_OP_reg22:
16677 case DW_OP_reg23:
16678 case DW_OP_reg24:
16679 case DW_OP_reg25:
16680 case DW_OP_reg26:
16681 case DW_OP_reg27:
16682 case DW_OP_reg28:
16683 case DW_OP_reg29:
16684 case DW_OP_reg30:
16685 case DW_OP_reg31:
16686 case DW_OP_bregx:
16687 case DW_OP_piece:
16688 case DW_OP_deref_size:
16689 case DW_OP_nop:
16690 case DW_OP_bit_piece:
16691 case DW_OP_implicit_value:
16692 case DW_OP_stack_value:
16693 break;
16695 case DW_OP_addr:
16696 case DW_OP_const1u:
16697 case DW_OP_const1s:
16698 case DW_OP_const2u:
16699 case DW_OP_const2s:
16700 case DW_OP_const4u:
16701 case DW_OP_const4s:
16702 case DW_OP_const8u:
16703 case DW_OP_const8s:
16704 case DW_OP_constu:
16705 case DW_OP_consts:
16706 case DW_OP_dup:
16707 case DW_OP_over:
16708 case DW_OP_pick:
16709 case DW_OP_lit0:
16710 case DW_OP_lit1:
16711 case DW_OP_lit2:
16712 case DW_OP_lit3:
16713 case DW_OP_lit4:
16714 case DW_OP_lit5:
16715 case DW_OP_lit6:
16716 case DW_OP_lit7:
16717 case DW_OP_lit8:
16718 case DW_OP_lit9:
16719 case DW_OP_lit10:
16720 case DW_OP_lit11:
16721 case DW_OP_lit12:
16722 case DW_OP_lit13:
16723 case DW_OP_lit14:
16724 case DW_OP_lit15:
16725 case DW_OP_lit16:
16726 case DW_OP_lit17:
16727 case DW_OP_lit18:
16728 case DW_OP_lit19:
16729 case DW_OP_lit20:
16730 case DW_OP_lit21:
16731 case DW_OP_lit22:
16732 case DW_OP_lit23:
16733 case DW_OP_lit24:
16734 case DW_OP_lit25:
16735 case DW_OP_lit26:
16736 case DW_OP_lit27:
16737 case DW_OP_lit28:
16738 case DW_OP_lit29:
16739 case DW_OP_lit30:
16740 case DW_OP_lit31:
16741 case DW_OP_breg0:
16742 case DW_OP_breg1:
16743 case DW_OP_breg2:
16744 case DW_OP_breg3:
16745 case DW_OP_breg4:
16746 case DW_OP_breg5:
16747 case DW_OP_breg6:
16748 case DW_OP_breg7:
16749 case DW_OP_breg8:
16750 case DW_OP_breg9:
16751 case DW_OP_breg10:
16752 case DW_OP_breg11:
16753 case DW_OP_breg12:
16754 case DW_OP_breg13:
16755 case DW_OP_breg14:
16756 case DW_OP_breg15:
16757 case DW_OP_breg16:
16758 case DW_OP_breg17:
16759 case DW_OP_breg18:
16760 case DW_OP_breg19:
16761 case DW_OP_breg20:
16762 case DW_OP_breg21:
16763 case DW_OP_breg22:
16764 case DW_OP_breg23:
16765 case DW_OP_breg24:
16766 case DW_OP_breg25:
16767 case DW_OP_breg26:
16768 case DW_OP_breg27:
16769 case DW_OP_breg28:
16770 case DW_OP_breg29:
16771 case DW_OP_breg30:
16772 case DW_OP_breg31:
16773 case DW_OP_fbreg:
16774 case DW_OP_push_object_address:
16775 case DW_OP_call_frame_cfa:
16776 ++frame_offset_;
16777 break;
16779 case DW_OP_drop:
16780 case DW_OP_xderef:
16781 case DW_OP_and:
16782 case DW_OP_div:
16783 case DW_OP_minus:
16784 case DW_OP_mod:
16785 case DW_OP_mul:
16786 case DW_OP_or:
16787 case DW_OP_plus:
16788 case DW_OP_shl:
16789 case DW_OP_shr:
16790 case DW_OP_shra:
16791 case DW_OP_xor:
16792 case DW_OP_bra:
16793 case DW_OP_eq:
16794 case DW_OP_ge:
16795 case DW_OP_gt:
16796 case DW_OP_le:
16797 case DW_OP_lt:
16798 case DW_OP_ne:
16799 case DW_OP_regx:
16800 case DW_OP_xderef_size:
16801 --frame_offset_;
16802 break;
16804 case DW_OP_call2:
16805 case DW_OP_call4:
16806 case DW_OP_call_ref:
16808 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16809 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16811 if (stack_usage == NULL)
16812 return false;
16813 frame_offset_ += *stack_usage;
16814 break;
16817 case DW_OP_implicit_pointer:
16818 case DW_OP_entry_value:
16819 case DW_OP_const_type:
16820 case DW_OP_regval_type:
16821 case DW_OP_deref_type:
16822 case DW_OP_convert:
16823 case DW_OP_reinterpret:
16824 case DW_OP_form_tls_address:
16825 case DW_OP_GNU_push_tls_address:
16826 case DW_OP_GNU_uninit:
16827 case DW_OP_GNU_encoded_addr:
16828 case DW_OP_GNU_implicit_pointer:
16829 case DW_OP_GNU_entry_value:
16830 case DW_OP_GNU_const_type:
16831 case DW_OP_GNU_regval_type:
16832 case DW_OP_GNU_deref_type:
16833 case DW_OP_GNU_convert:
16834 case DW_OP_GNU_reinterpret:
16835 case DW_OP_GNU_parameter_ref:
16836 /* loc_list_from_tree will probably not output these operations for
16837 size functions, so assume they will not appear here. */
16838 /* Fall through... */
16840 default:
16841 gcc_unreachable ();
16844 /* Now, follow the control flow (except subroutine calls). */
16845 switch (l->dw_loc_opc)
16847 case DW_OP_bra:
16848 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16849 frame_offsets))
16850 return false;
16851 /* Fall through. */
16853 case DW_OP_skip:
16854 l = l->dw_loc_oprnd1.v.val_loc;
16855 break;
16857 case DW_OP_stack_value:
16858 return true;
16860 default:
16861 l = l->dw_loc_next;
16862 break;
16866 return true;
16869 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16870 operations) in order to resolve the operand of DW_OP_pick operations that
16871 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16872 offset *before* LOC is executed. Return if all relocations were
16873 successful. */
16875 static bool
16876 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16877 struct dwarf_procedure_info *dpi)
16879 /* Associate to all visited operations the frame offset *before* evaluating
16880 this operation. */
16881 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16883 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16884 frame_offsets);
16887 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
16888 Return NULL if it is not possible. */
16890 static dw_die_ref
16891 function_to_dwarf_procedure (tree fndecl)
16893 struct loc_descr_context ctx;
16894 struct dwarf_procedure_info dpi;
16895 dw_die_ref dwarf_proc_die;
16896 tree tree_body = DECL_SAVED_TREE (fndecl);
16897 dw_loc_descr_ref loc_body, epilogue;
16899 tree cursor;
16900 unsigned i;
16902 /* Do not generate multiple DWARF procedures for the same function
16903 declaration. */
16904 dwarf_proc_die = lookup_decl_die (fndecl);
16905 if (dwarf_proc_die != NULL)
16906 return dwarf_proc_die;
16908 /* DWARF procedures are available starting with the DWARFv3 standard. */
16909 if (dwarf_version < 3 && dwarf_strict)
16910 return NULL;
16912 /* We handle only functions for which we still have a body, that return a
16913 supported type and that takes arguments with supported types. Note that
16914 there is no point translating functions that return nothing. */
16915 if (tree_body == NULL_TREE
16916 || DECL_RESULT (fndecl) == NULL_TREE
16917 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
16918 return NULL;
16920 for (cursor = DECL_ARGUMENTS (fndecl);
16921 cursor != NULL_TREE;
16922 cursor = TREE_CHAIN (cursor))
16923 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
16924 return NULL;
16926 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
16927 if (TREE_CODE (tree_body) != RETURN_EXPR)
16928 return NULL;
16929 tree_body = TREE_OPERAND (tree_body, 0);
16930 if (TREE_CODE (tree_body) != MODIFY_EXPR
16931 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
16932 return NULL;
16933 tree_body = TREE_OPERAND (tree_body, 1);
16935 /* Try to translate the body expression itself. Note that this will probably
16936 cause an infinite recursion if its call graph has a cycle. This is very
16937 unlikely for size functions, however, so don't bother with such things at
16938 the moment. */
16939 ctx.context_type = NULL_TREE;
16940 ctx.base_decl = NULL_TREE;
16941 ctx.dpi = &dpi;
16942 ctx.placeholder_arg = false;
16943 ctx.placeholder_seen = false;
16944 dpi.fndecl = fndecl;
16945 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
16946 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
16947 if (!loc_body)
16948 return NULL;
16950 /* After evaluating all operands in "loc_body", we should still have on the
16951 stack all arguments plus the desired function result (top of the stack).
16952 Generate code in order to keep only the result in our stack frame. */
16953 epilogue = NULL;
16954 for (i = 0; i < dpi.args_count; ++i)
16956 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
16957 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
16958 op_couple->dw_loc_next->dw_loc_next = epilogue;
16959 epilogue = op_couple;
16961 add_loc_descr (&loc_body, epilogue);
16962 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
16963 return NULL;
16965 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
16966 because they are considered useful. Now there is an epilogue, they are
16967 not anymore, so give it another try. */
16968 loc_descr_without_nops (loc_body);
16970 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
16971 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
16972 though, given that size functions do not come from source, so they should
16973 not have a dedicated DW_TAG_subprogram DIE. */
16974 dwarf_proc_die
16975 = new_dwarf_proc_die (loc_body, fndecl,
16976 get_context_die (DECL_CONTEXT (fndecl)));
16978 /* The called DWARF procedure consumes one stack slot per argument and
16979 returns one stack slot. */
16980 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
16982 return dwarf_proc_die;
16986 /* Generate Dwarf location list representing LOC.
16987 If WANT_ADDRESS is false, expression computing LOC will be computed
16988 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
16989 if WANT_ADDRESS is 2, expression computing address useable in location
16990 will be returned (i.e. DW_OP_reg can be used
16991 to refer to register values).
16993 CONTEXT provides information to customize the location descriptions
16994 generation. Its context_type field specifies what type is implicitly
16995 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
16996 will not be generated.
16998 Its DPI field determines whether we are generating a DWARF expression for a
16999 DWARF procedure, so PARM_DECL references are processed specifically.
17001 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17002 and dpi fields were null. */
17004 static dw_loc_list_ref
17005 loc_list_from_tree_1 (tree loc, int want_address,
17006 struct loc_descr_context *context)
17008 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17009 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17010 int have_address = 0;
17011 enum dwarf_location_atom op;
17013 /* ??? Most of the time we do not take proper care for sign/zero
17014 extending the values properly. Hopefully this won't be a real
17015 problem... */
17017 if (context != NULL
17018 && context->base_decl == loc
17019 && want_address == 0)
17021 if (dwarf_version >= 3 || !dwarf_strict)
17022 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17023 NULL, NULL, NULL);
17024 else
17025 return NULL;
17028 switch (TREE_CODE (loc))
17030 case ERROR_MARK:
17031 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17032 return 0;
17034 case PLACEHOLDER_EXPR:
17035 /* This case involves extracting fields from an object to determine the
17036 position of other fields. It is supposed to appear only as the first
17037 operand of COMPONENT_REF nodes and to reference precisely the type
17038 that the context allows. */
17039 if (context != NULL
17040 && TREE_TYPE (loc) == context->context_type
17041 && want_address >= 1)
17043 if (dwarf_version >= 3 || !dwarf_strict)
17045 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17046 have_address = 1;
17047 break;
17049 else
17050 return NULL;
17052 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17053 the single argument passed by consumer. */
17054 else if (context != NULL
17055 && context->placeholder_arg
17056 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17057 && want_address == 0)
17059 ret = new_loc_descr (DW_OP_pick, 0, 0);
17060 ret->frame_offset_rel = 1;
17061 context->placeholder_seen = true;
17062 break;
17064 else
17065 expansion_failed (loc, NULL_RTX,
17066 "PLACEHOLDER_EXPR for an unexpected type");
17067 break;
17069 case CALL_EXPR:
17071 const int nargs = call_expr_nargs (loc);
17072 tree callee = get_callee_fndecl (loc);
17073 int i;
17074 dw_die_ref dwarf_proc;
17076 if (callee == NULL_TREE)
17077 goto call_expansion_failed;
17079 /* We handle only functions that return an integer. */
17080 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17081 goto call_expansion_failed;
17083 dwarf_proc = function_to_dwarf_procedure (callee);
17084 if (dwarf_proc == NULL)
17085 goto call_expansion_failed;
17087 /* Evaluate arguments right-to-left so that the first argument will
17088 be the top-most one on the stack. */
17089 for (i = nargs - 1; i >= 0; --i)
17091 dw_loc_descr_ref loc_descr
17092 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17093 context);
17095 if (loc_descr == NULL)
17096 goto call_expansion_failed;
17098 add_loc_descr (&ret, loc_descr);
17101 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17102 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17103 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17104 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17105 add_loc_descr (&ret, ret1);
17106 break;
17108 call_expansion_failed:
17109 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17110 /* There are no opcodes for these operations. */
17111 return 0;
17114 case PREINCREMENT_EXPR:
17115 case PREDECREMENT_EXPR:
17116 case POSTINCREMENT_EXPR:
17117 case POSTDECREMENT_EXPR:
17118 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17119 /* There are no opcodes for these operations. */
17120 return 0;
17122 case ADDR_EXPR:
17123 /* If we already want an address, see if there is INDIRECT_REF inside
17124 e.g. for &this->field. */
17125 if (want_address)
17127 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17128 (loc, want_address == 2, context);
17129 if (list_ret)
17130 have_address = 1;
17131 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17132 && (ret = cst_pool_loc_descr (loc)))
17133 have_address = 1;
17135 /* Otherwise, process the argument and look for the address. */
17136 if (!list_ret && !ret)
17137 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17138 else
17140 if (want_address)
17141 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17142 return NULL;
17144 break;
17146 case VAR_DECL:
17147 if (DECL_THREAD_LOCAL_P (loc))
17149 rtx rtl;
17150 enum dwarf_location_atom tls_op;
17151 enum dtprel_bool dtprel = dtprel_false;
17153 if (targetm.have_tls)
17155 /* If this is not defined, we have no way to emit the
17156 data. */
17157 if (!targetm.asm_out.output_dwarf_dtprel)
17158 return 0;
17160 /* The way DW_OP_GNU_push_tls_address is specified, we
17161 can only look up addresses of objects in the current
17162 module. We used DW_OP_addr as first op, but that's
17163 wrong, because DW_OP_addr is relocated by the debug
17164 info consumer, while DW_OP_GNU_push_tls_address
17165 operand shouldn't be. */
17166 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17167 return 0;
17168 dtprel = dtprel_true;
17169 /* We check for DWARF 5 here because gdb did not implement
17170 DW_OP_form_tls_address until after 7.12. */
17171 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17172 : DW_OP_GNU_push_tls_address);
17174 else
17176 if (!targetm.emutls.debug_form_tls_address
17177 || !(dwarf_version >= 3 || !dwarf_strict))
17178 return 0;
17179 /* We stuffed the control variable into the DECL_VALUE_EXPR
17180 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17181 no longer appear in gimple code. We used the control
17182 variable in specific so that we could pick it up here. */
17183 loc = DECL_VALUE_EXPR (loc);
17184 tls_op = DW_OP_form_tls_address;
17187 rtl = rtl_for_decl_location (loc);
17188 if (rtl == NULL_RTX)
17189 return 0;
17191 if (!MEM_P (rtl))
17192 return 0;
17193 rtl = XEXP (rtl, 0);
17194 if (! CONSTANT_P (rtl))
17195 return 0;
17197 ret = new_addr_loc_descr (rtl, dtprel);
17198 ret1 = new_loc_descr (tls_op, 0, 0);
17199 add_loc_descr (&ret, ret1);
17201 have_address = 1;
17202 break;
17204 /* FALLTHRU */
17206 case PARM_DECL:
17207 if (context != NULL && context->dpi != NULL
17208 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17210 /* We are generating code for a DWARF procedure and we want to access
17211 one of its arguments: find the appropriate argument offset and let
17212 the resolve_args_picking pass compute the offset that complies
17213 with the stack frame size. */
17214 unsigned i = 0;
17215 tree cursor;
17217 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17218 cursor != NULL_TREE && cursor != loc;
17219 cursor = TREE_CHAIN (cursor), ++i)
17221 /* If we are translating a DWARF procedure, all referenced parameters
17222 must belong to the current function. */
17223 gcc_assert (cursor != NULL_TREE);
17225 ret = new_loc_descr (DW_OP_pick, i, 0);
17226 ret->frame_offset_rel = 1;
17227 break;
17229 /* FALLTHRU */
17231 case RESULT_DECL:
17232 if (DECL_HAS_VALUE_EXPR_P (loc))
17233 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17234 want_address, context);
17235 /* FALLTHRU */
17237 case FUNCTION_DECL:
17239 rtx rtl;
17240 var_loc_list *loc_list = lookup_decl_loc (loc);
17242 if (loc_list && loc_list->first)
17244 list_ret = dw_loc_list (loc_list, loc, want_address);
17245 have_address = want_address != 0;
17246 break;
17248 rtl = rtl_for_decl_location (loc);
17249 if (rtl == NULL_RTX)
17251 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17252 return 0;
17254 else if (CONST_INT_P (rtl))
17256 HOST_WIDE_INT val = INTVAL (rtl);
17257 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17258 val &= GET_MODE_MASK (DECL_MODE (loc));
17259 ret = int_loc_descriptor (val);
17261 else if (GET_CODE (rtl) == CONST_STRING)
17263 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17264 return 0;
17266 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17267 ret = new_addr_loc_descr (rtl, dtprel_false);
17268 else
17270 machine_mode mode, mem_mode;
17272 /* Certain constructs can only be represented at top-level. */
17273 if (want_address == 2)
17275 ret = loc_descriptor (rtl, VOIDmode,
17276 VAR_INIT_STATUS_INITIALIZED);
17277 have_address = 1;
17279 else
17281 mode = GET_MODE (rtl);
17282 mem_mode = VOIDmode;
17283 if (MEM_P (rtl))
17285 mem_mode = mode;
17286 mode = get_address_mode (rtl);
17287 rtl = XEXP (rtl, 0);
17288 have_address = 1;
17290 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17291 VAR_INIT_STATUS_INITIALIZED);
17293 if (!ret)
17294 expansion_failed (loc, rtl,
17295 "failed to produce loc descriptor for rtl");
17298 break;
17300 case MEM_REF:
17301 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17303 have_address = 1;
17304 goto do_plus;
17306 /* Fallthru. */
17307 case INDIRECT_REF:
17308 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17309 have_address = 1;
17310 break;
17312 case TARGET_MEM_REF:
17313 case SSA_NAME:
17314 case DEBUG_EXPR_DECL:
17315 return NULL;
17317 case COMPOUND_EXPR:
17318 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17319 context);
17321 CASE_CONVERT:
17322 case VIEW_CONVERT_EXPR:
17323 case SAVE_EXPR:
17324 case MODIFY_EXPR:
17325 case NON_LVALUE_EXPR:
17326 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17327 context);
17329 case COMPONENT_REF:
17330 case BIT_FIELD_REF:
17331 case ARRAY_REF:
17332 case ARRAY_RANGE_REF:
17333 case REALPART_EXPR:
17334 case IMAGPART_EXPR:
17336 tree obj, offset;
17337 HOST_WIDE_INT bitsize, bitpos, bytepos;
17338 machine_mode mode;
17339 int unsignedp, reversep, volatilep = 0;
17341 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17342 &unsignedp, &reversep, &volatilep);
17344 gcc_assert (obj != loc);
17346 list_ret = loc_list_from_tree_1 (obj,
17347 want_address == 2
17348 && !bitpos && !offset ? 2 : 1,
17349 context);
17350 /* TODO: We can extract value of the small expression via shifting even
17351 for nonzero bitpos. */
17352 if (list_ret == 0)
17353 return 0;
17354 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17356 expansion_failed (loc, NULL_RTX,
17357 "bitfield access");
17358 return 0;
17361 if (offset != NULL_TREE)
17363 /* Variable offset. */
17364 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17365 if (list_ret1 == 0)
17366 return 0;
17367 add_loc_list (&list_ret, list_ret1);
17368 if (!list_ret)
17369 return 0;
17370 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17373 bytepos = bitpos / BITS_PER_UNIT;
17374 if (bytepos > 0)
17375 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17376 else if (bytepos < 0)
17377 loc_list_plus_const (list_ret, bytepos);
17379 have_address = 1;
17380 break;
17383 case INTEGER_CST:
17384 if ((want_address || !tree_fits_shwi_p (loc))
17385 && (ret = cst_pool_loc_descr (loc)))
17386 have_address = 1;
17387 else if (want_address == 2
17388 && tree_fits_shwi_p (loc)
17389 && (ret = address_of_int_loc_descriptor
17390 (int_size_in_bytes (TREE_TYPE (loc)),
17391 tree_to_shwi (loc))))
17392 have_address = 1;
17393 else if (tree_fits_shwi_p (loc))
17394 ret = int_loc_descriptor (tree_to_shwi (loc));
17395 else if (tree_fits_uhwi_p (loc))
17396 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17397 else
17399 expansion_failed (loc, NULL_RTX,
17400 "Integer operand is not host integer");
17401 return 0;
17403 break;
17405 case CONSTRUCTOR:
17406 case REAL_CST:
17407 case STRING_CST:
17408 case COMPLEX_CST:
17409 if ((ret = cst_pool_loc_descr (loc)))
17410 have_address = 1;
17411 else if (TREE_CODE (loc) == CONSTRUCTOR)
17413 tree type = TREE_TYPE (loc);
17414 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17415 unsigned HOST_WIDE_INT offset = 0;
17416 unsigned HOST_WIDE_INT cnt;
17417 constructor_elt *ce;
17419 if (TREE_CODE (type) == RECORD_TYPE)
17421 /* This is very limited, but it's enough to output
17422 pointers to member functions, as long as the
17423 referenced function is defined in the current
17424 translation unit. */
17425 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17427 tree val = ce->value;
17429 tree field = ce->index;
17431 if (val)
17432 STRIP_NOPS (val);
17434 if (!field || DECL_BIT_FIELD (field))
17436 expansion_failed (loc, NULL_RTX,
17437 "bitfield in record type constructor");
17438 size = offset = (unsigned HOST_WIDE_INT)-1;
17439 ret = NULL;
17440 break;
17443 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17444 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17445 gcc_assert (pos + fieldsize <= size);
17446 if (pos < offset)
17448 expansion_failed (loc, NULL_RTX,
17449 "out-of-order fields in record constructor");
17450 size = offset = (unsigned HOST_WIDE_INT)-1;
17451 ret = NULL;
17452 break;
17454 if (pos > offset)
17456 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17457 add_loc_descr (&ret, ret1);
17458 offset = pos;
17460 if (val && fieldsize != 0)
17462 ret1 = loc_descriptor_from_tree (val, want_address, context);
17463 if (!ret1)
17465 expansion_failed (loc, NULL_RTX,
17466 "unsupported expression in field");
17467 size = offset = (unsigned HOST_WIDE_INT)-1;
17468 ret = NULL;
17469 break;
17471 add_loc_descr (&ret, ret1);
17473 if (fieldsize)
17475 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17476 add_loc_descr (&ret, ret1);
17477 offset = pos + fieldsize;
17481 if (offset != size)
17483 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17484 add_loc_descr (&ret, ret1);
17485 offset = size;
17488 have_address = !!want_address;
17490 else
17491 expansion_failed (loc, NULL_RTX,
17492 "constructor of non-record type");
17494 else
17495 /* We can construct small constants here using int_loc_descriptor. */
17496 expansion_failed (loc, NULL_RTX,
17497 "constructor or constant not in constant pool");
17498 break;
17500 case TRUTH_AND_EXPR:
17501 case TRUTH_ANDIF_EXPR:
17502 case BIT_AND_EXPR:
17503 op = DW_OP_and;
17504 goto do_binop;
17506 case TRUTH_XOR_EXPR:
17507 case BIT_XOR_EXPR:
17508 op = DW_OP_xor;
17509 goto do_binop;
17511 case TRUTH_OR_EXPR:
17512 case TRUTH_ORIF_EXPR:
17513 case BIT_IOR_EXPR:
17514 op = DW_OP_or;
17515 goto do_binop;
17517 case FLOOR_DIV_EXPR:
17518 case CEIL_DIV_EXPR:
17519 case ROUND_DIV_EXPR:
17520 case TRUNC_DIV_EXPR:
17521 case EXACT_DIV_EXPR:
17522 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17523 return 0;
17524 op = DW_OP_div;
17525 goto do_binop;
17527 case MINUS_EXPR:
17528 op = DW_OP_minus;
17529 goto do_binop;
17531 case FLOOR_MOD_EXPR:
17532 case CEIL_MOD_EXPR:
17533 case ROUND_MOD_EXPR:
17534 case TRUNC_MOD_EXPR:
17535 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17537 op = DW_OP_mod;
17538 goto do_binop;
17540 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17541 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17542 if (list_ret == 0 || list_ret1 == 0)
17543 return 0;
17545 add_loc_list (&list_ret, list_ret1);
17546 if (list_ret == 0)
17547 return 0;
17548 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17549 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17550 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17551 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17552 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17553 break;
17555 case MULT_EXPR:
17556 op = DW_OP_mul;
17557 goto do_binop;
17559 case LSHIFT_EXPR:
17560 op = DW_OP_shl;
17561 goto do_binop;
17563 case RSHIFT_EXPR:
17564 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17565 goto do_binop;
17567 case POINTER_PLUS_EXPR:
17568 case PLUS_EXPR:
17569 do_plus:
17570 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17572 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17573 smarter to encode their opposite. The DW_OP_plus_uconst operation
17574 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17575 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17576 bytes, Y being the size of the operation that pushes the opposite
17577 of the addend. So let's choose the smallest representation. */
17578 const tree tree_addend = TREE_OPERAND (loc, 1);
17579 offset_int wi_addend;
17580 HOST_WIDE_INT shwi_addend;
17581 dw_loc_descr_ref loc_naddend;
17583 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17584 if (list_ret == 0)
17585 return 0;
17587 /* Try to get the literal to push. It is the opposite of the addend,
17588 so as we rely on wrapping during DWARF evaluation, first decode
17589 the literal as a "DWARF-sized" signed number. */
17590 wi_addend = wi::to_offset (tree_addend);
17591 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17592 shwi_addend = wi_addend.to_shwi ();
17593 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17594 ? int_loc_descriptor (-shwi_addend)
17595 : NULL;
17597 if (loc_naddend != NULL
17598 && ((unsigned) size_of_uleb128 (shwi_addend)
17599 > size_of_loc_descr (loc_naddend)))
17601 add_loc_descr_to_each (list_ret, loc_naddend);
17602 add_loc_descr_to_each (list_ret,
17603 new_loc_descr (DW_OP_minus, 0, 0));
17605 else
17607 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17609 loc_naddend = loc_cur;
17610 loc_cur = loc_cur->dw_loc_next;
17611 ggc_free (loc_naddend);
17613 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17615 break;
17618 op = DW_OP_plus;
17619 goto do_binop;
17621 case LE_EXPR:
17622 op = DW_OP_le;
17623 goto do_comp_binop;
17625 case GE_EXPR:
17626 op = DW_OP_ge;
17627 goto do_comp_binop;
17629 case LT_EXPR:
17630 op = DW_OP_lt;
17631 goto do_comp_binop;
17633 case GT_EXPR:
17634 op = DW_OP_gt;
17635 goto do_comp_binop;
17637 do_comp_binop:
17638 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17640 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17641 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17642 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17643 TREE_CODE (loc));
17644 break;
17646 else
17647 goto do_binop;
17649 case EQ_EXPR:
17650 op = DW_OP_eq;
17651 goto do_binop;
17653 case NE_EXPR:
17654 op = DW_OP_ne;
17655 goto do_binop;
17657 do_binop:
17658 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17659 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17660 if (list_ret == 0 || list_ret1 == 0)
17661 return 0;
17663 add_loc_list (&list_ret, list_ret1);
17664 if (list_ret == 0)
17665 return 0;
17666 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17667 break;
17669 case TRUTH_NOT_EXPR:
17670 case BIT_NOT_EXPR:
17671 op = DW_OP_not;
17672 goto do_unop;
17674 case ABS_EXPR:
17675 op = DW_OP_abs;
17676 goto do_unop;
17678 case NEGATE_EXPR:
17679 op = DW_OP_neg;
17680 goto do_unop;
17682 do_unop:
17683 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17684 if (list_ret == 0)
17685 return 0;
17687 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17688 break;
17690 case MIN_EXPR:
17691 case MAX_EXPR:
17693 const enum tree_code code =
17694 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17696 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17697 build2 (code, integer_type_node,
17698 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17699 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17702 /* fall through */
17704 case COND_EXPR:
17706 dw_loc_descr_ref lhs
17707 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17708 dw_loc_list_ref rhs
17709 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17710 dw_loc_descr_ref bra_node, jump_node, tmp;
17712 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17713 if (list_ret == 0 || lhs == 0 || rhs == 0)
17714 return 0;
17716 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17717 add_loc_descr_to_each (list_ret, bra_node);
17719 add_loc_list (&list_ret, rhs);
17720 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17721 add_loc_descr_to_each (list_ret, jump_node);
17723 add_loc_descr_to_each (list_ret, lhs);
17724 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17725 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17727 /* ??? Need a node to point the skip at. Use a nop. */
17728 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17729 add_loc_descr_to_each (list_ret, tmp);
17730 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17731 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17733 break;
17735 case FIX_TRUNC_EXPR:
17736 return 0;
17738 default:
17739 /* Leave front-end specific codes as simply unknown. This comes
17740 up, for instance, with the C STMT_EXPR. */
17741 if ((unsigned int) TREE_CODE (loc)
17742 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17744 expansion_failed (loc, NULL_RTX,
17745 "language specific tree node");
17746 return 0;
17749 /* Otherwise this is a generic code; we should just lists all of
17750 these explicitly. We forgot one. */
17751 if (flag_checking)
17752 gcc_unreachable ();
17754 /* In a release build, we want to degrade gracefully: better to
17755 generate incomplete debugging information than to crash. */
17756 return NULL;
17759 if (!ret && !list_ret)
17760 return 0;
17762 if (want_address == 2 && !have_address
17763 && (dwarf_version >= 4 || !dwarf_strict))
17765 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17767 expansion_failed (loc, NULL_RTX,
17768 "DWARF address size mismatch");
17769 return 0;
17771 if (ret)
17772 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17773 else
17774 add_loc_descr_to_each (list_ret,
17775 new_loc_descr (DW_OP_stack_value, 0, 0));
17776 have_address = 1;
17778 /* Show if we can't fill the request for an address. */
17779 if (want_address && !have_address)
17781 expansion_failed (loc, NULL_RTX,
17782 "Want address and only have value");
17783 return 0;
17786 gcc_assert (!ret || !list_ret);
17788 /* If we've got an address and don't want one, dereference. */
17789 if (!want_address && have_address)
17791 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17793 if (size > DWARF2_ADDR_SIZE || size == -1)
17795 expansion_failed (loc, NULL_RTX,
17796 "DWARF address size mismatch");
17797 return 0;
17799 else if (size == DWARF2_ADDR_SIZE)
17800 op = DW_OP_deref;
17801 else
17802 op = DW_OP_deref_size;
17804 if (ret)
17805 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17806 else
17807 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17809 if (ret)
17810 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17812 return list_ret;
17815 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17816 expressions. */
17818 static dw_loc_list_ref
17819 loc_list_from_tree (tree loc, int want_address,
17820 struct loc_descr_context *context)
17822 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17824 for (dw_loc_list_ref loc_cur = result;
17825 loc_cur != NULL; loc_cur =
17826 loc_cur->dw_loc_next)
17827 loc_descr_without_nops (loc_cur->expr);
17828 return result;
17831 /* Same as above but return only single location expression. */
17832 static dw_loc_descr_ref
17833 loc_descriptor_from_tree (tree loc, int want_address,
17834 struct loc_descr_context *context)
17836 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17837 if (!ret)
17838 return NULL;
17839 if (ret->dw_loc_next)
17841 expansion_failed (loc, NULL_RTX,
17842 "Location list where only loc descriptor needed");
17843 return NULL;
17845 return ret->expr;
17848 /* Given a value, round it up to the lowest multiple of `boundary'
17849 which is not less than the value itself. */
17851 static inline HOST_WIDE_INT
17852 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17854 return (((value + boundary - 1) / boundary) * boundary);
17857 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17858 pointer to the declared type for the relevant field variable, or return
17859 `integer_type_node' if the given node turns out to be an
17860 ERROR_MARK node. */
17862 static inline tree
17863 field_type (const_tree decl)
17865 tree type;
17867 if (TREE_CODE (decl) == ERROR_MARK)
17868 return integer_type_node;
17870 type = DECL_BIT_FIELD_TYPE (decl);
17871 if (type == NULL_TREE)
17872 type = TREE_TYPE (decl);
17874 return type;
17877 /* Given a pointer to a tree node, return the alignment in bits for
17878 it, or else return BITS_PER_WORD if the node actually turns out to
17879 be an ERROR_MARK node. */
17881 static inline unsigned
17882 simple_type_align_in_bits (const_tree type)
17884 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
17887 static inline unsigned
17888 simple_decl_align_in_bits (const_tree decl)
17890 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
17893 /* Return the result of rounding T up to ALIGN. */
17895 static inline offset_int
17896 round_up_to_align (const offset_int &t, unsigned int align)
17898 return wi::udiv_trunc (t + align - 1, align) * align;
17901 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
17902 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
17903 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
17904 if we fail to return the size in one of these two forms. */
17906 static dw_loc_descr_ref
17907 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
17909 tree tree_size;
17910 struct loc_descr_context ctx;
17912 /* Return a constant integer in priority, if possible. */
17913 *cst_size = int_size_in_bytes (type);
17914 if (*cst_size != -1)
17915 return NULL;
17917 ctx.context_type = const_cast<tree> (type);
17918 ctx.base_decl = NULL_TREE;
17919 ctx.dpi = NULL;
17920 ctx.placeholder_arg = false;
17921 ctx.placeholder_seen = false;
17923 type = TYPE_MAIN_VARIANT (type);
17924 tree_size = TYPE_SIZE_UNIT (type);
17925 return ((tree_size != NULL_TREE)
17926 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
17927 : NULL);
17930 /* Helper structure for RECORD_TYPE processing. */
17931 struct vlr_context
17933 /* Root RECORD_TYPE. It is needed to generate data member location
17934 descriptions in variable-length records (VLR), but also to cope with
17935 variants, which are composed of nested structures multiplexed with
17936 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
17937 function processing a FIELD_DECL, it is required to be non null. */
17938 tree struct_type;
17939 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
17940 QUAL_UNION_TYPE), this holds an expression that computes the offset for
17941 this variant part as part of the root record (in storage units). For
17942 regular records, it must be NULL_TREE. */
17943 tree variant_part_offset;
17946 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
17947 addressed byte of the "containing object" for the given FIELD_DECL. If
17948 possible, return a native constant through CST_OFFSET (in which case NULL is
17949 returned); otherwise return a DWARF expression that computes the offset.
17951 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
17952 that offset is, either because the argument turns out to be a pointer to an
17953 ERROR_MARK node, or because the offset expression is too complex for us.
17955 CTX is required: see the comment for VLR_CONTEXT. */
17957 static dw_loc_descr_ref
17958 field_byte_offset (const_tree decl, struct vlr_context *ctx,
17959 HOST_WIDE_INT *cst_offset)
17961 offset_int object_offset_in_bits;
17962 offset_int object_offset_in_bytes;
17963 offset_int bitpos_int;
17964 bool is_byte_offset_cst, is_bit_offset_cst;
17965 tree tree_result;
17966 dw_loc_list_ref loc_result;
17968 *cst_offset = 0;
17970 if (TREE_CODE (decl) == ERROR_MARK)
17971 return NULL;
17972 else
17973 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
17975 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
17976 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
17978 /* We cannot handle variable bit offsets at the moment, so abort if it's the
17979 case. */
17980 if (is_bit_offset_cst)
17981 return NULL;
17983 #ifdef PCC_BITFIELD_TYPE_MATTERS
17984 /* We used to handle only constant offsets in all cases. Now, we handle
17985 properly dynamic byte offsets only when PCC bitfield type doesn't
17986 matter. */
17987 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
17989 tree type;
17990 tree field_size_tree;
17991 offset_int deepest_bitpos;
17992 offset_int field_size_in_bits;
17993 unsigned int type_align_in_bits;
17994 unsigned int decl_align_in_bits;
17995 offset_int type_size_in_bits;
17997 bitpos_int = wi::to_offset (bit_position (decl));
17998 type = field_type (decl);
17999 type_size_in_bits = offset_int_type_size_in_bits (type);
18000 type_align_in_bits = simple_type_align_in_bits (type);
18002 field_size_tree = DECL_SIZE (decl);
18004 /* The size could be unspecified if there was an error, or for
18005 a flexible array member. */
18006 if (!field_size_tree)
18007 field_size_tree = bitsize_zero_node;
18009 /* If the size of the field is not constant, use the type size. */
18010 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18011 field_size_in_bits = wi::to_offset (field_size_tree);
18012 else
18013 field_size_in_bits = type_size_in_bits;
18015 decl_align_in_bits = simple_decl_align_in_bits (decl);
18017 /* The GCC front-end doesn't make any attempt to keep track of the
18018 starting bit offset (relative to the start of the containing
18019 structure type) of the hypothetical "containing object" for a
18020 bit-field. Thus, when computing the byte offset value for the
18021 start of the "containing object" of a bit-field, we must deduce
18022 this information on our own. This can be rather tricky to do in
18023 some cases. For example, handling the following structure type
18024 definition when compiling for an i386/i486 target (which only
18025 aligns long long's to 32-bit boundaries) can be very tricky:
18027 struct S { int field1; long long field2:31; };
18029 Fortunately, there is a simple rule-of-thumb which can be used
18030 in such cases. When compiling for an i386/i486, GCC will
18031 allocate 8 bytes for the structure shown above. It decides to
18032 do this based upon one simple rule for bit-field allocation.
18033 GCC allocates each "containing object" for each bit-field at
18034 the first (i.e. lowest addressed) legitimate alignment boundary
18035 (based upon the required minimum alignment for the declared
18036 type of the field) which it can possibly use, subject to the
18037 condition that there is still enough available space remaining
18038 in the containing object (when allocated at the selected point)
18039 to fully accommodate all of the bits of the bit-field itself.
18041 This simple rule makes it obvious why GCC allocates 8 bytes for
18042 each object of the structure type shown above. When looking
18043 for a place to allocate the "containing object" for `field2',
18044 the compiler simply tries to allocate a 64-bit "containing
18045 object" at each successive 32-bit boundary (starting at zero)
18046 until it finds a place to allocate that 64- bit field such that
18047 at least 31 contiguous (and previously unallocated) bits remain
18048 within that selected 64 bit field. (As it turns out, for the
18049 example above, the compiler finds it is OK to allocate the
18050 "containing object" 64-bit field at bit-offset zero within the
18051 structure type.)
18053 Here we attempt to work backwards from the limited set of facts
18054 we're given, and we try to deduce from those facts, where GCC
18055 must have believed that the containing object started (within
18056 the structure type). The value we deduce is then used (by the
18057 callers of this routine) to generate DW_AT_location and
18058 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18059 the case of DW_AT_location, regular fields as well). */
18061 /* Figure out the bit-distance from the start of the structure to
18062 the "deepest" bit of the bit-field. */
18063 deepest_bitpos = bitpos_int + field_size_in_bits;
18065 /* This is the tricky part. Use some fancy footwork to deduce
18066 where the lowest addressed bit of the containing object must
18067 be. */
18068 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18070 /* Round up to type_align by default. This works best for
18071 bitfields. */
18072 object_offset_in_bits
18073 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18075 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18077 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18079 /* Round up to decl_align instead. */
18080 object_offset_in_bits
18081 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18084 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18086 tree_result = byte_position (decl);
18087 if (ctx->variant_part_offset != NULL_TREE)
18088 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18089 ctx->variant_part_offset, tree_result));
18091 /* If the byte offset is a constant, it's simplier to handle a native
18092 constant rather than a DWARF expression. */
18093 if (TREE_CODE (tree_result) == INTEGER_CST)
18095 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18096 return NULL;
18098 struct loc_descr_context loc_ctx = {
18099 ctx->struct_type, /* context_type */
18100 NULL_TREE, /* base_decl */
18101 NULL, /* dpi */
18102 false, /* placeholder_arg */
18103 false /* placeholder_seen */
18105 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18107 /* We want a DWARF expression: abort if we only have a location list with
18108 multiple elements. */
18109 if (!loc_result || !single_element_loc_list_p (loc_result))
18110 return NULL;
18111 else
18112 return loc_result->expr;
18115 /* The following routines define various Dwarf attributes and any data
18116 associated with them. */
18118 /* Add a location description attribute value to a DIE.
18120 This emits location attributes suitable for whole variables and
18121 whole parameters. Note that the location attributes for struct fields are
18122 generated by the routine `data_member_location_attribute' below. */
18124 static inline void
18125 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18126 dw_loc_list_ref descr)
18128 if (descr == 0)
18129 return;
18130 if (single_element_loc_list_p (descr))
18131 add_AT_loc (die, attr_kind, descr->expr);
18132 else
18133 add_AT_loc_list (die, attr_kind, descr);
18136 /* Add DW_AT_accessibility attribute to DIE if needed. */
18138 static void
18139 add_accessibility_attribute (dw_die_ref die, tree decl)
18141 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18142 children, otherwise the default is DW_ACCESS_public. In DWARF2
18143 the default has always been DW_ACCESS_public. */
18144 if (TREE_PROTECTED (decl))
18145 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18146 else if (TREE_PRIVATE (decl))
18148 if (dwarf_version == 2
18149 || die->die_parent == NULL
18150 || die->die_parent->die_tag != DW_TAG_class_type)
18151 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18153 else if (dwarf_version > 2
18154 && die->die_parent
18155 && die->die_parent->die_tag == DW_TAG_class_type)
18156 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18159 /* Attach the specialized form of location attribute used for data members of
18160 struct and union types. In the special case of a FIELD_DECL node which
18161 represents a bit-field, the "offset" part of this special location
18162 descriptor must indicate the distance in bytes from the lowest-addressed
18163 byte of the containing struct or union type to the lowest-addressed byte of
18164 the "containing object" for the bit-field. (See the `field_byte_offset'
18165 function above).
18167 For any given bit-field, the "containing object" is a hypothetical object
18168 (of some integral or enum type) within which the given bit-field lives. The
18169 type of this hypothetical "containing object" is always the same as the
18170 declared type of the individual bit-field itself (for GCC anyway... the
18171 DWARF spec doesn't actually mandate this). Note that it is the size (in
18172 bytes) of the hypothetical "containing object" which will be given in the
18173 DW_AT_byte_size attribute for this bit-field. (See the
18174 `byte_size_attribute' function below.) It is also used when calculating the
18175 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18176 function below.)
18178 CTX is required: see the comment for VLR_CONTEXT. */
18180 static void
18181 add_data_member_location_attribute (dw_die_ref die,
18182 tree decl,
18183 struct vlr_context *ctx)
18185 HOST_WIDE_INT offset;
18186 dw_loc_descr_ref loc_descr = 0;
18188 if (TREE_CODE (decl) == TREE_BINFO)
18190 /* We're working on the TAG_inheritance for a base class. */
18191 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18193 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18194 aren't at a fixed offset from all (sub)objects of the same
18195 type. We need to extract the appropriate offset from our
18196 vtable. The following dwarf expression means
18198 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18200 This is specific to the V3 ABI, of course. */
18202 dw_loc_descr_ref tmp;
18204 /* Make a copy of the object address. */
18205 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18206 add_loc_descr (&loc_descr, tmp);
18208 /* Extract the vtable address. */
18209 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18210 add_loc_descr (&loc_descr, tmp);
18212 /* Calculate the address of the offset. */
18213 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18214 gcc_assert (offset < 0);
18216 tmp = int_loc_descriptor (-offset);
18217 add_loc_descr (&loc_descr, tmp);
18218 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18219 add_loc_descr (&loc_descr, tmp);
18221 /* Extract the offset. */
18222 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18223 add_loc_descr (&loc_descr, tmp);
18225 /* Add it to the object address. */
18226 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18227 add_loc_descr (&loc_descr, tmp);
18229 else
18230 offset = tree_to_shwi (BINFO_OFFSET (decl));
18232 else
18234 loc_descr = field_byte_offset (decl, ctx, &offset);
18236 /* If loc_descr is available then we know the field offset is dynamic.
18237 However, GDB does not handle dynamic field offsets very well at the
18238 moment. */
18239 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18241 loc_descr = NULL;
18242 offset = 0;
18245 /* Data member location evalutation starts with the base address on the
18246 stack. Compute the field offset and add it to this base address. */
18247 else if (loc_descr != NULL)
18248 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18251 if (! loc_descr)
18253 if (dwarf_version > 2)
18255 /* Don't need to output a location expression, just the constant. */
18256 if (offset < 0)
18257 add_AT_int (die, DW_AT_data_member_location, offset);
18258 else
18259 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18260 return;
18262 else
18264 enum dwarf_location_atom op;
18266 /* The DWARF2 standard says that we should assume that the structure
18267 address is already on the stack, so we can specify a structure
18268 field address by using DW_OP_plus_uconst. */
18269 op = DW_OP_plus_uconst;
18270 loc_descr = new_loc_descr (op, offset, 0);
18274 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18277 /* Writes integer values to dw_vec_const array. */
18279 static void
18280 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18282 while (size != 0)
18284 *dest++ = val & 0xff;
18285 val >>= 8;
18286 --size;
18290 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18292 static HOST_WIDE_INT
18293 extract_int (const unsigned char *src, unsigned int size)
18295 HOST_WIDE_INT val = 0;
18297 src += size;
18298 while (size != 0)
18300 val <<= 8;
18301 val |= *--src & 0xff;
18302 --size;
18304 return val;
18307 /* Writes wide_int values to dw_vec_const array. */
18309 static void
18310 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18312 int i;
18314 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18316 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18317 return;
18320 /* We'd have to extend this code to support odd sizes. */
18321 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18323 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18325 if (WORDS_BIG_ENDIAN)
18326 for (i = n - 1; i >= 0; i--)
18328 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18329 dest += sizeof (HOST_WIDE_INT);
18331 else
18332 for (i = 0; i < n; i++)
18334 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18335 dest += sizeof (HOST_WIDE_INT);
18339 /* Writes floating point values to dw_vec_const array. */
18341 static void
18342 insert_float (const_rtx rtl, unsigned char *array)
18344 long val[4];
18345 int i;
18347 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18349 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18350 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18352 insert_int (val[i], 4, array);
18353 array += 4;
18357 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18358 does not have a "location" either in memory or in a register. These
18359 things can arise in GNU C when a constant is passed as an actual parameter
18360 to an inlined function. They can also arise in C++ where declared
18361 constants do not necessarily get memory "homes". */
18363 static bool
18364 add_const_value_attribute (dw_die_ref die, rtx rtl)
18366 switch (GET_CODE (rtl))
18368 case CONST_INT:
18370 HOST_WIDE_INT val = INTVAL (rtl);
18372 if (val < 0)
18373 add_AT_int (die, DW_AT_const_value, val);
18374 else
18375 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18377 return true;
18379 case CONST_WIDE_INT:
18381 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18382 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18383 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18384 wide_int w = wi::zext (w1, prec);
18385 add_AT_wide (die, DW_AT_const_value, w);
18387 return true;
18389 case CONST_DOUBLE:
18390 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18391 floating-point constant. A CONST_DOUBLE is used whenever the
18392 constant requires more than one word in order to be adequately
18393 represented. */
18395 machine_mode mode = GET_MODE (rtl);
18397 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18398 add_AT_double (die, DW_AT_const_value,
18399 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18400 else
18402 unsigned int length = GET_MODE_SIZE (mode);
18403 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18405 insert_float (rtl, array);
18406 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18409 return true;
18411 case CONST_VECTOR:
18413 machine_mode mode = GET_MODE (rtl);
18414 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18415 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18416 unsigned char *array
18417 = ggc_vec_alloc<unsigned char> (length * elt_size);
18418 unsigned int i;
18419 unsigned char *p;
18420 machine_mode imode = GET_MODE_INNER (mode);
18422 switch (GET_MODE_CLASS (mode))
18424 case MODE_VECTOR_INT:
18425 for (i = 0, p = array; i < length; i++, p += elt_size)
18427 rtx elt = CONST_VECTOR_ELT (rtl, i);
18428 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18430 break;
18432 case MODE_VECTOR_FLOAT:
18433 for (i = 0, p = array; i < length; i++, p += elt_size)
18435 rtx elt = CONST_VECTOR_ELT (rtl, i);
18436 insert_float (elt, p);
18438 break;
18440 default:
18441 gcc_unreachable ();
18444 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18446 return true;
18448 case CONST_STRING:
18449 if (dwarf_version >= 4 || !dwarf_strict)
18451 dw_loc_descr_ref loc_result;
18452 resolve_one_addr (&rtl);
18453 rtl_addr:
18454 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18455 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18456 add_AT_loc (die, DW_AT_location, loc_result);
18457 vec_safe_push (used_rtx_array, rtl);
18458 return true;
18460 return false;
18462 case CONST:
18463 if (CONSTANT_P (XEXP (rtl, 0)))
18464 return add_const_value_attribute (die, XEXP (rtl, 0));
18465 /* FALLTHROUGH */
18466 case SYMBOL_REF:
18467 if (!const_ok_for_output (rtl))
18468 return false;
18469 /* FALLTHROUGH */
18470 case LABEL_REF:
18471 if (dwarf_version >= 4 || !dwarf_strict)
18472 goto rtl_addr;
18473 return false;
18475 case PLUS:
18476 /* In cases where an inlined instance of an inline function is passed
18477 the address of an `auto' variable (which is local to the caller) we
18478 can get a situation where the DECL_RTL of the artificial local
18479 variable (for the inlining) which acts as a stand-in for the
18480 corresponding formal parameter (of the inline function) will look
18481 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18482 exactly a compile-time constant expression, but it isn't the address
18483 of the (artificial) local variable either. Rather, it represents the
18484 *value* which the artificial local variable always has during its
18485 lifetime. We currently have no way to represent such quasi-constant
18486 values in Dwarf, so for now we just punt and generate nothing. */
18487 return false;
18489 case HIGH:
18490 case CONST_FIXED:
18491 return false;
18493 case MEM:
18494 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18495 && MEM_READONLY_P (rtl)
18496 && GET_MODE (rtl) == BLKmode)
18498 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18499 return true;
18501 return false;
18503 default:
18504 /* No other kinds of rtx should be possible here. */
18505 gcc_unreachable ();
18507 return false;
18510 /* Determine whether the evaluation of EXPR references any variables
18511 or functions which aren't otherwise used (and therefore may not be
18512 output). */
18513 static tree
18514 reference_to_unused (tree * tp, int * walk_subtrees,
18515 void * data ATTRIBUTE_UNUSED)
18517 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18518 *walk_subtrees = 0;
18520 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18521 && ! TREE_ASM_WRITTEN (*tp))
18522 return *tp;
18523 /* ??? The C++ FE emits debug information for using decls, so
18524 putting gcc_unreachable here falls over. See PR31899. For now
18525 be conservative. */
18526 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18527 return *tp;
18528 else if (VAR_P (*tp))
18530 varpool_node *node = varpool_node::get (*tp);
18531 if (!node || !node->definition)
18532 return *tp;
18534 else if (TREE_CODE (*tp) == FUNCTION_DECL
18535 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18537 /* The call graph machinery must have finished analyzing,
18538 optimizing and gimplifying the CU by now.
18539 So if *TP has no call graph node associated
18540 to it, it means *TP will not be emitted. */
18541 if (!cgraph_node::get (*tp))
18542 return *tp;
18544 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18545 return *tp;
18547 return NULL_TREE;
18550 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18551 for use in a later add_const_value_attribute call. */
18553 static rtx
18554 rtl_for_decl_init (tree init, tree type)
18556 rtx rtl = NULL_RTX;
18558 STRIP_NOPS (init);
18560 /* If a variable is initialized with a string constant without embedded
18561 zeros, build CONST_STRING. */
18562 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18564 tree enttype = TREE_TYPE (type);
18565 tree domain = TYPE_DOMAIN (type);
18566 machine_mode mode = TYPE_MODE (enttype);
18568 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18569 && domain
18570 && integer_zerop (TYPE_MIN_VALUE (domain))
18571 && compare_tree_int (TYPE_MAX_VALUE (domain),
18572 TREE_STRING_LENGTH (init) - 1) == 0
18573 && ((size_t) TREE_STRING_LENGTH (init)
18574 == strlen (TREE_STRING_POINTER (init)) + 1))
18576 rtl = gen_rtx_CONST_STRING (VOIDmode,
18577 ggc_strdup (TREE_STRING_POINTER (init)));
18578 rtl = gen_rtx_MEM (BLKmode, rtl);
18579 MEM_READONLY_P (rtl) = 1;
18582 /* Other aggregates, and complex values, could be represented using
18583 CONCAT: FIXME! */
18584 else if (AGGREGATE_TYPE_P (type)
18585 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18586 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18587 || TREE_CODE (type) == COMPLEX_TYPE)
18589 /* Vectors only work if their mode is supported by the target.
18590 FIXME: generic vectors ought to work too. */
18591 else if (TREE_CODE (type) == VECTOR_TYPE
18592 && !VECTOR_MODE_P (TYPE_MODE (type)))
18594 /* If the initializer is something that we know will expand into an
18595 immediate RTL constant, expand it now. We must be careful not to
18596 reference variables which won't be output. */
18597 else if (initializer_constant_valid_p (init, type)
18598 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18600 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18601 possible. */
18602 if (TREE_CODE (type) == VECTOR_TYPE)
18603 switch (TREE_CODE (init))
18605 case VECTOR_CST:
18606 break;
18607 case CONSTRUCTOR:
18608 if (TREE_CONSTANT (init))
18610 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18611 bool constant_p = true;
18612 tree value;
18613 unsigned HOST_WIDE_INT ix;
18615 /* Even when ctor is constant, it might contain non-*_CST
18616 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18617 belong into VECTOR_CST nodes. */
18618 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18619 if (!CONSTANT_CLASS_P (value))
18621 constant_p = false;
18622 break;
18625 if (constant_p)
18627 init = build_vector_from_ctor (type, elts);
18628 break;
18631 /* FALLTHRU */
18633 default:
18634 return NULL;
18637 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18639 /* If expand_expr returns a MEM, it wasn't immediate. */
18640 gcc_assert (!rtl || !MEM_P (rtl));
18643 return rtl;
18646 /* Generate RTL for the variable DECL to represent its location. */
18648 static rtx
18649 rtl_for_decl_location (tree decl)
18651 rtx rtl;
18653 /* Here we have to decide where we are going to say the parameter "lives"
18654 (as far as the debugger is concerned). We only have a couple of
18655 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18657 DECL_RTL normally indicates where the parameter lives during most of the
18658 activation of the function. If optimization is enabled however, this
18659 could be either NULL or else a pseudo-reg. Both of those cases indicate
18660 that the parameter doesn't really live anywhere (as far as the code
18661 generation parts of GCC are concerned) during most of the function's
18662 activation. That will happen (for example) if the parameter is never
18663 referenced within the function.
18665 We could just generate a location descriptor here for all non-NULL
18666 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18667 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18668 where DECL_RTL is NULL or is a pseudo-reg.
18670 Note however that we can only get away with using DECL_INCOMING_RTL as
18671 a backup substitute for DECL_RTL in certain limited cases. In cases
18672 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18673 we can be sure that the parameter was passed using the same type as it is
18674 declared to have within the function, and that its DECL_INCOMING_RTL
18675 points us to a place where a value of that type is passed.
18677 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18678 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18679 because in these cases DECL_INCOMING_RTL points us to a value of some
18680 type which is *different* from the type of the parameter itself. Thus,
18681 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18682 such cases, the debugger would end up (for example) trying to fetch a
18683 `float' from a place which actually contains the first part of a
18684 `double'. That would lead to really incorrect and confusing
18685 output at debug-time.
18687 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18688 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18689 are a couple of exceptions however. On little-endian machines we can
18690 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18691 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18692 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18693 when (on a little-endian machine) a non-prototyped function has a
18694 parameter declared to be of type `short' or `char'. In such cases,
18695 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18696 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18697 passed `int' value. If the debugger then uses that address to fetch
18698 a `short' or a `char' (on a little-endian machine) the result will be
18699 the correct data, so we allow for such exceptional cases below.
18701 Note that our goal here is to describe the place where the given formal
18702 parameter lives during most of the function's activation (i.e. between the
18703 end of the prologue and the start of the epilogue). We'll do that as best
18704 as we can. Note however that if the given formal parameter is modified
18705 sometime during the execution of the function, then a stack backtrace (at
18706 debug-time) will show the function as having been called with the *new*
18707 value rather than the value which was originally passed in. This happens
18708 rarely enough that it is not a major problem, but it *is* a problem, and
18709 I'd like to fix it.
18711 A future version of dwarf2out.c may generate two additional attributes for
18712 any given DW_TAG_formal_parameter DIE which will describe the "passed
18713 type" and the "passed location" for the given formal parameter in addition
18714 to the attributes we now generate to indicate the "declared type" and the
18715 "active location" for each parameter. This additional set of attributes
18716 could be used by debuggers for stack backtraces. Separately, note that
18717 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18718 This happens (for example) for inlined-instances of inline function formal
18719 parameters which are never referenced. This really shouldn't be
18720 happening. All PARM_DECL nodes should get valid non-NULL
18721 DECL_INCOMING_RTL values. FIXME. */
18723 /* Use DECL_RTL as the "location" unless we find something better. */
18724 rtl = DECL_RTL_IF_SET (decl);
18726 /* When generating abstract instances, ignore everything except
18727 constants, symbols living in memory, and symbols living in
18728 fixed registers. */
18729 if (! reload_completed)
18731 if (rtl
18732 && (CONSTANT_P (rtl)
18733 || (MEM_P (rtl)
18734 && CONSTANT_P (XEXP (rtl, 0)))
18735 || (REG_P (rtl)
18736 && VAR_P (decl)
18737 && TREE_STATIC (decl))))
18739 rtl = targetm.delegitimize_address (rtl);
18740 return rtl;
18742 rtl = NULL_RTX;
18744 else if (TREE_CODE (decl) == PARM_DECL)
18746 if (rtl == NULL_RTX
18747 || is_pseudo_reg (rtl)
18748 || (MEM_P (rtl)
18749 && is_pseudo_reg (XEXP (rtl, 0))
18750 && DECL_INCOMING_RTL (decl)
18751 && MEM_P (DECL_INCOMING_RTL (decl))
18752 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18754 tree declared_type = TREE_TYPE (decl);
18755 tree passed_type = DECL_ARG_TYPE (decl);
18756 machine_mode dmode = TYPE_MODE (declared_type);
18757 machine_mode pmode = TYPE_MODE (passed_type);
18759 /* This decl represents a formal parameter which was optimized out.
18760 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18761 all cases where (rtl == NULL_RTX) just below. */
18762 if (dmode == pmode)
18763 rtl = DECL_INCOMING_RTL (decl);
18764 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18765 && SCALAR_INT_MODE_P (dmode)
18766 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18767 && DECL_INCOMING_RTL (decl))
18769 rtx inc = DECL_INCOMING_RTL (decl);
18770 if (REG_P (inc))
18771 rtl = inc;
18772 else if (MEM_P (inc))
18774 if (BYTES_BIG_ENDIAN)
18775 rtl = adjust_address_nv (inc, dmode,
18776 GET_MODE_SIZE (pmode)
18777 - GET_MODE_SIZE (dmode));
18778 else
18779 rtl = inc;
18784 /* If the parm was passed in registers, but lives on the stack, then
18785 make a big endian correction if the mode of the type of the
18786 parameter is not the same as the mode of the rtl. */
18787 /* ??? This is the same series of checks that are made in dbxout.c before
18788 we reach the big endian correction code there. It isn't clear if all
18789 of these checks are necessary here, but keeping them all is the safe
18790 thing to do. */
18791 else if (MEM_P (rtl)
18792 && XEXP (rtl, 0) != const0_rtx
18793 && ! CONSTANT_P (XEXP (rtl, 0))
18794 /* Not passed in memory. */
18795 && !MEM_P (DECL_INCOMING_RTL (decl))
18796 /* Not passed by invisible reference. */
18797 && (!REG_P (XEXP (rtl, 0))
18798 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18799 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18800 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18801 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18802 #endif
18804 /* Big endian correction check. */
18805 && BYTES_BIG_ENDIAN
18806 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18807 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18808 < UNITS_PER_WORD))
18810 machine_mode addr_mode = get_address_mode (rtl);
18811 int offset = (UNITS_PER_WORD
18812 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18814 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18815 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18818 else if (VAR_P (decl)
18819 && rtl
18820 && MEM_P (rtl)
18821 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18822 && BYTES_BIG_ENDIAN)
18824 machine_mode addr_mode = get_address_mode (rtl);
18825 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18826 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18828 /* If a variable is declared "register" yet is smaller than
18829 a register, then if we store the variable to memory, it
18830 looks like we're storing a register-sized value, when in
18831 fact we are not. We need to adjust the offset of the
18832 storage location to reflect the actual value's bytes,
18833 else gdb will not be able to display it. */
18834 if (rsize > dsize)
18835 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18836 plus_constant (addr_mode, XEXP (rtl, 0),
18837 rsize - dsize));
18840 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
18841 and will have been substituted directly into all expressions that use it.
18842 C does not have such a concept, but C++ and other languages do. */
18843 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
18844 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
18846 if (rtl)
18847 rtl = targetm.delegitimize_address (rtl);
18849 /* If we don't look past the constant pool, we risk emitting a
18850 reference to a constant pool entry that isn't referenced from
18851 code, and thus is not emitted. */
18852 if (rtl)
18853 rtl = avoid_constant_pool_reference (rtl);
18855 /* Try harder to get a rtl. If this symbol ends up not being emitted
18856 in the current CU, resolve_addr will remove the expression referencing
18857 it. */
18858 if (rtl == NULL_RTX
18859 && VAR_P (decl)
18860 && !DECL_EXTERNAL (decl)
18861 && TREE_STATIC (decl)
18862 && DECL_NAME (decl)
18863 && !DECL_HARD_REGISTER (decl)
18864 && DECL_MODE (decl) != VOIDmode)
18866 rtl = make_decl_rtl_for_debug (decl);
18867 if (!MEM_P (rtl)
18868 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
18869 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
18870 rtl = NULL_RTX;
18873 return rtl;
18876 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
18877 returned. If so, the decl for the COMMON block is returned, and the
18878 value is the offset into the common block for the symbol. */
18880 static tree
18881 fortran_common (tree decl, HOST_WIDE_INT *value)
18883 tree val_expr, cvar;
18884 machine_mode mode;
18885 HOST_WIDE_INT bitsize, bitpos;
18886 tree offset;
18887 int unsignedp, reversep, volatilep = 0;
18889 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
18890 it does not have a value (the offset into the common area), or if it
18891 is thread local (as opposed to global) then it isn't common, and shouldn't
18892 be handled as such. */
18893 if (!VAR_P (decl)
18894 || !TREE_STATIC (decl)
18895 || !DECL_HAS_VALUE_EXPR_P (decl)
18896 || !is_fortran ())
18897 return NULL_TREE;
18899 val_expr = DECL_VALUE_EXPR (decl);
18900 if (TREE_CODE (val_expr) != COMPONENT_REF)
18901 return NULL_TREE;
18903 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
18904 &unsignedp, &reversep, &volatilep);
18906 if (cvar == NULL_TREE
18907 || !VAR_P (cvar)
18908 || DECL_ARTIFICIAL (cvar)
18909 || !TREE_PUBLIC (cvar))
18910 return NULL_TREE;
18912 *value = 0;
18913 if (offset != NULL)
18915 if (!tree_fits_shwi_p (offset))
18916 return NULL_TREE;
18917 *value = tree_to_shwi (offset);
18919 if (bitpos != 0)
18920 *value += bitpos / BITS_PER_UNIT;
18922 return cvar;
18925 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
18926 data attribute for a variable or a parameter. We generate the
18927 DW_AT_const_value attribute only in those cases where the given variable
18928 or parameter does not have a true "location" either in memory or in a
18929 register. This can happen (for example) when a constant is passed as an
18930 actual argument in a call to an inline function. (It's possible that
18931 these things can crop up in other ways also.) Note that one type of
18932 constant value which can be passed into an inlined function is a constant
18933 pointer. This can happen for example if an actual argument in an inlined
18934 function call evaluates to a compile-time constant address.
18936 CACHE_P is true if it is worth caching the location list for DECL,
18937 so that future calls can reuse it rather than regenerate it from scratch.
18938 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
18939 since we will need to refer to them each time the function is inlined. */
18941 static bool
18942 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
18944 rtx rtl;
18945 dw_loc_list_ref list;
18946 var_loc_list *loc_list;
18947 cached_dw_loc_list *cache;
18949 if (early_dwarf)
18950 return false;
18952 if (TREE_CODE (decl) == ERROR_MARK)
18953 return false;
18955 if (get_AT (die, DW_AT_location)
18956 || get_AT (die, DW_AT_const_value))
18957 return true;
18959 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
18960 || TREE_CODE (decl) == RESULT_DECL);
18962 /* Try to get some constant RTL for this decl, and use that as the value of
18963 the location. */
18965 rtl = rtl_for_decl_location (decl);
18966 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18967 && add_const_value_attribute (die, rtl))
18968 return true;
18970 /* See if we have single element location list that is equivalent to
18971 a constant value. That way we are better to use add_const_value_attribute
18972 rather than expanding constant value equivalent. */
18973 loc_list = lookup_decl_loc (decl);
18974 if (loc_list
18975 && loc_list->first
18976 && loc_list->first->next == NULL
18977 && NOTE_P (loc_list->first->loc)
18978 && NOTE_VAR_LOCATION (loc_list->first->loc)
18979 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
18981 struct var_loc_node *node;
18983 node = loc_list->first;
18984 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
18985 if (GET_CODE (rtl) == EXPR_LIST)
18986 rtl = XEXP (rtl, 0);
18987 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18988 && add_const_value_attribute (die, rtl))
18989 return true;
18991 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
18992 list several times. See if we've already cached the contents. */
18993 list = NULL;
18994 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
18995 cache_p = false;
18996 if (cache_p)
18998 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
18999 if (cache)
19000 list = cache->loc_list;
19002 if (list == NULL)
19004 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19005 NULL);
19006 /* It is usually worth caching this result if the decl is from
19007 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19008 if (cache_p && list && list->dw_loc_next)
19010 cached_dw_loc_list **slot
19011 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19012 DECL_UID (decl),
19013 INSERT);
19014 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19015 cache->decl_id = DECL_UID (decl);
19016 cache->loc_list = list;
19017 *slot = cache;
19020 if (list)
19022 add_AT_location_description (die, DW_AT_location, list);
19023 return true;
19025 /* None of that worked, so it must not really have a location;
19026 try adding a constant value attribute from the DECL_INITIAL. */
19027 return tree_add_const_value_attribute_for_decl (die, decl);
19030 /* Helper function for tree_add_const_value_attribute. Natively encode
19031 initializer INIT into an array. Return true if successful. */
19033 static bool
19034 native_encode_initializer (tree init, unsigned char *array, int size)
19036 tree type;
19038 if (init == NULL_TREE)
19039 return false;
19041 STRIP_NOPS (init);
19042 switch (TREE_CODE (init))
19044 case STRING_CST:
19045 type = TREE_TYPE (init);
19046 if (TREE_CODE (type) == ARRAY_TYPE)
19048 tree enttype = TREE_TYPE (type);
19049 machine_mode mode = TYPE_MODE (enttype);
19051 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19052 return false;
19053 if (int_size_in_bytes (type) != size)
19054 return false;
19055 if (size > TREE_STRING_LENGTH (init))
19057 memcpy (array, TREE_STRING_POINTER (init),
19058 TREE_STRING_LENGTH (init));
19059 memset (array + TREE_STRING_LENGTH (init),
19060 '\0', size - TREE_STRING_LENGTH (init));
19062 else
19063 memcpy (array, TREE_STRING_POINTER (init), size);
19064 return true;
19066 return false;
19067 case CONSTRUCTOR:
19068 type = TREE_TYPE (init);
19069 if (int_size_in_bytes (type) != size)
19070 return false;
19071 if (TREE_CODE (type) == ARRAY_TYPE)
19073 HOST_WIDE_INT min_index;
19074 unsigned HOST_WIDE_INT cnt;
19075 int curpos = 0, fieldsize;
19076 constructor_elt *ce;
19078 if (TYPE_DOMAIN (type) == NULL_TREE
19079 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19080 return false;
19082 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19083 if (fieldsize <= 0)
19084 return false;
19086 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19087 memset (array, '\0', size);
19088 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19090 tree val = ce->value;
19091 tree index = ce->index;
19092 int pos = curpos;
19093 if (index && TREE_CODE (index) == RANGE_EXPR)
19094 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19095 * fieldsize;
19096 else if (index)
19097 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19099 if (val)
19101 STRIP_NOPS (val);
19102 if (!native_encode_initializer (val, array + pos, fieldsize))
19103 return false;
19105 curpos = pos + fieldsize;
19106 if (index && TREE_CODE (index) == RANGE_EXPR)
19108 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19109 - tree_to_shwi (TREE_OPERAND (index, 0));
19110 while (count-- > 0)
19112 if (val)
19113 memcpy (array + curpos, array + pos, fieldsize);
19114 curpos += fieldsize;
19117 gcc_assert (curpos <= size);
19119 return true;
19121 else if (TREE_CODE (type) == RECORD_TYPE
19122 || TREE_CODE (type) == UNION_TYPE)
19124 tree field = NULL_TREE;
19125 unsigned HOST_WIDE_INT cnt;
19126 constructor_elt *ce;
19128 if (int_size_in_bytes (type) != size)
19129 return false;
19131 if (TREE_CODE (type) == RECORD_TYPE)
19132 field = TYPE_FIELDS (type);
19134 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19136 tree val = ce->value;
19137 int pos, fieldsize;
19139 if (ce->index != 0)
19140 field = ce->index;
19142 if (val)
19143 STRIP_NOPS (val);
19145 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19146 return false;
19148 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19149 && TYPE_DOMAIN (TREE_TYPE (field))
19150 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19151 return false;
19152 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19153 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19154 return false;
19155 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19156 pos = int_byte_position (field);
19157 gcc_assert (pos + fieldsize <= size);
19158 if (val && fieldsize != 0
19159 && !native_encode_initializer (val, array + pos, fieldsize))
19160 return false;
19162 return true;
19164 return false;
19165 case VIEW_CONVERT_EXPR:
19166 case NON_LVALUE_EXPR:
19167 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19168 default:
19169 return native_encode_expr (init, array, size) == size;
19173 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19174 attribute is the const value T. */
19176 static bool
19177 tree_add_const_value_attribute (dw_die_ref die, tree t)
19179 tree init;
19180 tree type = TREE_TYPE (t);
19181 rtx rtl;
19183 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19184 return false;
19186 init = t;
19187 gcc_assert (!DECL_P (init));
19189 if (! early_dwarf)
19191 rtl = rtl_for_decl_init (init, type);
19192 if (rtl)
19193 return add_const_value_attribute (die, rtl);
19195 /* If the host and target are sane, try harder. */
19196 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19197 && initializer_constant_valid_p (init, type))
19199 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19200 if (size > 0 && (int) size == size)
19202 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19204 if (native_encode_initializer (init, array, size))
19206 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19207 return true;
19209 ggc_free (array);
19212 return false;
19215 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19216 attribute is the const value of T, where T is an integral constant
19217 variable with static storage duration
19218 (so it can't be a PARM_DECL or a RESULT_DECL). */
19220 static bool
19221 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19224 if (!decl
19225 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19226 || (VAR_P (decl) && !TREE_STATIC (decl)))
19227 return false;
19229 if (TREE_READONLY (decl)
19230 && ! TREE_THIS_VOLATILE (decl)
19231 && DECL_INITIAL (decl))
19232 /* OK */;
19233 else
19234 return false;
19236 /* Don't add DW_AT_const_value if abstract origin already has one. */
19237 if (get_AT (var_die, DW_AT_const_value))
19238 return false;
19240 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19243 /* Convert the CFI instructions for the current function into a
19244 location list. This is used for DW_AT_frame_base when we targeting
19245 a dwarf2 consumer that does not support the dwarf3
19246 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19247 expressions. */
19249 static dw_loc_list_ref
19250 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19252 int ix;
19253 dw_fde_ref fde;
19254 dw_loc_list_ref list, *list_tail;
19255 dw_cfi_ref cfi;
19256 dw_cfa_location last_cfa, next_cfa;
19257 const char *start_label, *last_label, *section;
19258 dw_cfa_location remember;
19260 fde = cfun->fde;
19261 gcc_assert (fde != NULL);
19263 section = secname_for_decl (current_function_decl);
19264 list_tail = &list;
19265 list = NULL;
19267 memset (&next_cfa, 0, sizeof (next_cfa));
19268 next_cfa.reg = INVALID_REGNUM;
19269 remember = next_cfa;
19271 start_label = fde->dw_fde_begin;
19273 /* ??? Bald assumption that the CIE opcode list does not contain
19274 advance opcodes. */
19275 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19276 lookup_cfa_1 (cfi, &next_cfa, &remember);
19278 last_cfa = next_cfa;
19279 last_label = start_label;
19281 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19283 /* If the first partition contained no CFI adjustments, the
19284 CIE opcodes apply to the whole first partition. */
19285 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19286 fde->dw_fde_begin, fde->dw_fde_end, section);
19287 list_tail =&(*list_tail)->dw_loc_next;
19288 start_label = last_label = fde->dw_fde_second_begin;
19291 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19293 switch (cfi->dw_cfi_opc)
19295 case DW_CFA_set_loc:
19296 case DW_CFA_advance_loc1:
19297 case DW_CFA_advance_loc2:
19298 case DW_CFA_advance_loc4:
19299 if (!cfa_equal_p (&last_cfa, &next_cfa))
19301 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19302 start_label, last_label, section);
19304 list_tail = &(*list_tail)->dw_loc_next;
19305 last_cfa = next_cfa;
19306 start_label = last_label;
19308 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19309 break;
19311 case DW_CFA_advance_loc:
19312 /* The encoding is complex enough that we should never emit this. */
19313 gcc_unreachable ();
19315 default:
19316 lookup_cfa_1 (cfi, &next_cfa, &remember);
19317 break;
19319 if (ix + 1 == fde->dw_fde_switch_cfi_index)
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);
19326 list_tail = &(*list_tail)->dw_loc_next;
19327 last_cfa = next_cfa;
19328 start_label = last_label;
19330 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19331 start_label, fde->dw_fde_end, section);
19332 list_tail = &(*list_tail)->dw_loc_next;
19333 start_label = last_label = fde->dw_fde_second_begin;
19337 if (!cfa_equal_p (&last_cfa, &next_cfa))
19339 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19340 start_label, last_label, section);
19341 list_tail = &(*list_tail)->dw_loc_next;
19342 start_label = last_label;
19345 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19346 start_label,
19347 fde->dw_fde_second_begin
19348 ? fde->dw_fde_second_end : fde->dw_fde_end,
19349 section);
19351 if (list && list->dw_loc_next)
19352 gen_llsym (list);
19354 return list;
19357 /* Compute a displacement from the "steady-state frame pointer" to the
19358 frame base (often the same as the CFA), and store it in
19359 frame_pointer_fb_offset. OFFSET is added to the displacement
19360 before the latter is negated. */
19362 static void
19363 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19365 rtx reg, elim;
19367 #ifdef FRAME_POINTER_CFA_OFFSET
19368 reg = frame_pointer_rtx;
19369 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19370 #else
19371 reg = arg_pointer_rtx;
19372 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19373 #endif
19375 elim = (ira_use_lra_p
19376 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19377 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19378 if (GET_CODE (elim) == PLUS)
19380 offset += INTVAL (XEXP (elim, 1));
19381 elim = XEXP (elim, 0);
19384 frame_pointer_fb_offset = -offset;
19386 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19387 in which to eliminate. This is because it's stack pointer isn't
19388 directly accessible as a register within the ISA. To work around
19389 this, assume that while we cannot provide a proper value for
19390 frame_pointer_fb_offset, we won't need one either. */
19391 frame_pointer_fb_offset_valid
19392 = ((SUPPORTS_STACK_ALIGNMENT
19393 && (elim == hard_frame_pointer_rtx
19394 || elim == stack_pointer_rtx))
19395 || elim == (frame_pointer_needed
19396 ? hard_frame_pointer_rtx
19397 : stack_pointer_rtx));
19400 /* Generate a DW_AT_name attribute given some string value to be included as
19401 the value of the attribute. */
19403 static void
19404 add_name_attribute (dw_die_ref die, const char *name_string)
19406 if (name_string != NULL && *name_string != 0)
19408 if (demangle_name_func)
19409 name_string = (*demangle_name_func) (name_string);
19411 add_AT_string (die, DW_AT_name, name_string);
19415 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19416 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19417 of TYPE accordingly.
19419 ??? This is a temporary measure until after we're able to generate
19420 regular DWARF for the complex Ada type system. */
19422 static void
19423 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19424 dw_die_ref context_die)
19426 tree dtype;
19427 dw_die_ref dtype_die;
19429 if (!lang_hooks.types.descriptive_type)
19430 return;
19432 dtype = lang_hooks.types.descriptive_type (type);
19433 if (!dtype)
19434 return;
19436 dtype_die = lookup_type_die (dtype);
19437 if (!dtype_die)
19439 gen_type_die (dtype, context_die);
19440 dtype_die = lookup_type_die (dtype);
19441 gcc_assert (dtype_die);
19444 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19447 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19449 static const char *
19450 comp_dir_string (void)
19452 const char *wd;
19453 char *wd1;
19454 static const char *cached_wd = NULL;
19456 if (cached_wd != NULL)
19457 return cached_wd;
19459 wd = get_src_pwd ();
19460 if (wd == NULL)
19461 return NULL;
19463 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19465 int wdlen;
19467 wdlen = strlen (wd);
19468 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19469 strcpy (wd1, wd);
19470 wd1 [wdlen] = DIR_SEPARATOR;
19471 wd1 [wdlen + 1] = 0;
19472 wd = wd1;
19475 cached_wd = remap_debug_filename (wd);
19476 return cached_wd;
19479 /* Generate a DW_AT_comp_dir attribute for DIE. */
19481 static void
19482 add_comp_dir_attribute (dw_die_ref die)
19484 const char * wd = comp_dir_string ();
19485 if (wd != NULL)
19486 add_AT_string (die, DW_AT_comp_dir, wd);
19489 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19490 pointer computation, ...), output a representation for that bound according
19491 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19492 loc_list_from_tree for the meaning of CONTEXT. */
19494 static void
19495 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19496 int forms, struct loc_descr_context *context)
19498 dw_die_ref context_die, decl_die;
19499 dw_loc_list_ref list;
19500 bool strip_conversions = true;
19501 bool placeholder_seen = false;
19503 while (strip_conversions)
19504 switch (TREE_CODE (value))
19506 case ERROR_MARK:
19507 case SAVE_EXPR:
19508 return;
19510 CASE_CONVERT:
19511 case VIEW_CONVERT_EXPR:
19512 value = TREE_OPERAND (value, 0);
19513 break;
19515 default:
19516 strip_conversions = false;
19517 break;
19520 /* If possible and permitted, output the attribute as a constant. */
19521 if ((forms & dw_scalar_form_constant) != 0
19522 && TREE_CODE (value) == INTEGER_CST)
19524 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19526 /* If HOST_WIDE_INT is big enough then represent the bound as
19527 a constant value. We need to choose a form based on
19528 whether the type is signed or unsigned. We cannot just
19529 call add_AT_unsigned if the value itself is positive
19530 (add_AT_unsigned might add the unsigned value encoded as
19531 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19532 bounds type and then sign extend any unsigned values found
19533 for signed types. This is needed only for
19534 DW_AT_{lower,upper}_bound, since for most other attributes,
19535 consumers will treat DW_FORM_data[1248] as unsigned values,
19536 regardless of the underlying type. */
19537 if (prec <= HOST_BITS_PER_WIDE_INT
19538 || tree_fits_uhwi_p (value))
19540 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19541 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19542 else
19543 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19545 else
19546 /* Otherwise represent the bound as an unsigned value with
19547 the precision of its type. The precision and signedness
19548 of the type will be necessary to re-interpret it
19549 unambiguously. */
19550 add_AT_wide (die, attr, value);
19551 return;
19554 /* Otherwise, if it's possible and permitted too, output a reference to
19555 another DIE. */
19556 if ((forms & dw_scalar_form_reference) != 0)
19558 tree decl = NULL_TREE;
19560 /* Some type attributes reference an outer type. For instance, the upper
19561 bound of an array may reference an embedding record (this happens in
19562 Ada). */
19563 if (TREE_CODE (value) == COMPONENT_REF
19564 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19565 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19566 decl = TREE_OPERAND (value, 1);
19568 else if (VAR_P (value)
19569 || TREE_CODE (value) == PARM_DECL
19570 || TREE_CODE (value) == RESULT_DECL)
19571 decl = value;
19573 if (decl != NULL_TREE)
19575 dw_die_ref decl_die = lookup_decl_die (decl);
19577 /* ??? Can this happen, or should the variable have been bound
19578 first? Probably it can, since I imagine that we try to create
19579 the types of parameters in the order in which they exist in
19580 the list, and won't have created a forward reference to a
19581 later parameter. */
19582 if (decl_die != NULL)
19584 add_AT_die_ref (die, attr, decl_die);
19585 return;
19590 /* Last chance: try to create a stack operation procedure to evaluate the
19591 value. Do nothing if even that is not possible or permitted. */
19592 if ((forms & dw_scalar_form_exprloc) == 0)
19593 return;
19595 list = loc_list_from_tree (value, 2, context);
19596 if (context && context->placeholder_arg)
19598 placeholder_seen = context->placeholder_seen;
19599 context->placeholder_seen = false;
19601 if (list == NULL || single_element_loc_list_p (list))
19603 /* If this attribute is not a reference nor constant, it is
19604 a DWARF expression rather than location description. For that
19605 loc_list_from_tree (value, 0, &context) is needed. */
19606 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19607 if (list2 && single_element_loc_list_p (list2))
19609 if (placeholder_seen)
19611 struct dwarf_procedure_info dpi;
19612 dpi.fndecl = NULL_TREE;
19613 dpi.args_count = 1;
19614 if (!resolve_args_picking (list2->expr, 1, &dpi))
19615 return;
19617 add_AT_loc (die, attr, list2->expr);
19618 return;
19622 /* If that failed to give a single element location list, fall back to
19623 outputting this as a reference... still if permitted. */
19624 if (list == NULL
19625 || (forms & dw_scalar_form_reference) == 0
19626 || placeholder_seen)
19627 return;
19629 if (current_function_decl == 0)
19630 context_die = comp_unit_die ();
19631 else
19632 context_die = lookup_decl_die (current_function_decl);
19634 decl_die = new_die (DW_TAG_variable, context_die, value);
19635 add_AT_flag (decl_die, DW_AT_artificial, 1);
19636 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19637 context_die);
19638 add_AT_location_description (decl_die, DW_AT_location, list);
19639 add_AT_die_ref (die, attr, decl_die);
19642 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19643 default. */
19645 static int
19646 lower_bound_default (void)
19648 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19650 case DW_LANG_C:
19651 case DW_LANG_C89:
19652 case DW_LANG_C99:
19653 case DW_LANG_C11:
19654 case DW_LANG_C_plus_plus:
19655 case DW_LANG_C_plus_plus_11:
19656 case DW_LANG_C_plus_plus_14:
19657 case DW_LANG_ObjC:
19658 case DW_LANG_ObjC_plus_plus:
19659 case DW_LANG_Java:
19660 return 0;
19661 case DW_LANG_Fortran77:
19662 case DW_LANG_Fortran90:
19663 case DW_LANG_Fortran95:
19664 case DW_LANG_Fortran03:
19665 case DW_LANG_Fortran08:
19666 return 1;
19667 case DW_LANG_UPC:
19668 case DW_LANG_D:
19669 case DW_LANG_Python:
19670 return dwarf_version >= 4 ? 0 : -1;
19671 case DW_LANG_Ada95:
19672 case DW_LANG_Ada83:
19673 case DW_LANG_Cobol74:
19674 case DW_LANG_Cobol85:
19675 case DW_LANG_Pascal83:
19676 case DW_LANG_Modula2:
19677 case DW_LANG_PLI:
19678 return dwarf_version >= 4 ? 1 : -1;
19679 default:
19680 return -1;
19684 /* Given a tree node describing an array bound (either lower or upper) output
19685 a representation for that bound. */
19687 static void
19688 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19689 tree bound, struct loc_descr_context *context)
19691 int dflt;
19693 while (1)
19694 switch (TREE_CODE (bound))
19696 /* Strip all conversions. */
19697 CASE_CONVERT:
19698 case VIEW_CONVERT_EXPR:
19699 bound = TREE_OPERAND (bound, 0);
19700 break;
19702 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19703 are even omitted when they are the default. */
19704 case INTEGER_CST:
19705 /* If the value for this bound is the default one, we can even omit the
19706 attribute. */
19707 if (bound_attr == DW_AT_lower_bound
19708 && tree_fits_shwi_p (bound)
19709 && (dflt = lower_bound_default ()) != -1
19710 && tree_to_shwi (bound) == dflt)
19711 return;
19713 /* FALLTHRU */
19715 default:
19716 /* Because of the complex interaction there can be with other GNAT
19717 encodings, GDB isn't ready yet to handle proper DWARF description
19718 for self-referencial subrange bounds: let GNAT encodings do the
19719 magic in such a case. */
19720 if (is_ada ()
19721 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19722 && contains_placeholder_p (bound))
19723 return;
19725 add_scalar_info (subrange_die, bound_attr, bound,
19726 dw_scalar_form_constant
19727 | dw_scalar_form_exprloc
19728 | dw_scalar_form_reference,
19729 context);
19730 return;
19734 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19735 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19736 Note that the block of subscript information for an array type also
19737 includes information about the element type of the given array type.
19739 This function reuses previously set type and bound information if
19740 available. */
19742 static void
19743 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19745 unsigned dimension_number;
19746 tree lower, upper;
19747 dw_die_ref child = type_die->die_child;
19749 for (dimension_number = 0;
19750 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19751 type = TREE_TYPE (type), dimension_number++)
19753 tree domain = TYPE_DOMAIN (type);
19755 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19756 break;
19758 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19759 and (in GNU C only) variable bounds. Handle all three forms
19760 here. */
19762 /* Find and reuse a previously generated DW_TAG_subrange_type if
19763 available.
19765 For multi-dimensional arrays, as we iterate through the
19766 various dimensions in the enclosing for loop above, we also
19767 iterate through the DIE children and pick at each
19768 DW_TAG_subrange_type previously generated (if available).
19769 Each child DW_TAG_subrange_type DIE describes the range of
19770 the current dimension. At this point we should have as many
19771 DW_TAG_subrange_type's as we have dimensions in the
19772 array. */
19773 dw_die_ref subrange_die = NULL;
19774 if (child)
19775 while (1)
19777 child = child->die_sib;
19778 if (child->die_tag == DW_TAG_subrange_type)
19779 subrange_die = child;
19780 if (child == type_die->die_child)
19782 /* If we wrapped around, stop looking next time. */
19783 child = NULL;
19784 break;
19786 if (child->die_tag == DW_TAG_subrange_type)
19787 break;
19789 if (!subrange_die)
19790 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19792 if (domain)
19794 /* We have an array type with specified bounds. */
19795 lower = TYPE_MIN_VALUE (domain);
19796 upper = TYPE_MAX_VALUE (domain);
19798 /* Define the index type. */
19799 if (TREE_TYPE (domain)
19800 && !get_AT (subrange_die, DW_AT_type))
19802 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19803 TREE_TYPE field. We can't emit debug info for this
19804 because it is an unnamed integral type. */
19805 if (TREE_CODE (domain) == INTEGER_TYPE
19806 && TYPE_NAME (domain) == NULL_TREE
19807 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19808 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19810 else
19811 add_type_attribute (subrange_die, TREE_TYPE (domain),
19812 TYPE_UNQUALIFIED, false, type_die);
19815 /* ??? If upper is NULL, the array has unspecified length,
19816 but it does have a lower bound. This happens with Fortran
19817 dimension arr(N:*)
19818 Since the debugger is definitely going to need to know N
19819 to produce useful results, go ahead and output the lower
19820 bound solo, and hope the debugger can cope. */
19822 if (!get_AT (subrange_die, DW_AT_lower_bound))
19823 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19824 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19825 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19828 /* Otherwise we have an array type with an unspecified length. The
19829 DWARF-2 spec does not say how to handle this; let's just leave out the
19830 bounds. */
19834 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19836 static void
19837 add_byte_size_attribute (dw_die_ref die, tree tree_node)
19839 dw_die_ref decl_die;
19840 HOST_WIDE_INT size;
19841 dw_loc_descr_ref size_expr = NULL;
19843 switch (TREE_CODE (tree_node))
19845 case ERROR_MARK:
19846 size = 0;
19847 break;
19848 case ENUMERAL_TYPE:
19849 case RECORD_TYPE:
19850 case UNION_TYPE:
19851 case QUAL_UNION_TYPE:
19852 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
19853 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
19855 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
19856 return;
19858 size_expr = type_byte_size (tree_node, &size);
19859 break;
19860 case FIELD_DECL:
19861 /* For a data member of a struct or union, the DW_AT_byte_size is
19862 generally given as the number of bytes normally allocated for an
19863 object of the *declared* type of the member itself. This is true
19864 even for bit-fields. */
19865 size = int_size_in_bytes (field_type (tree_node));
19866 break;
19867 default:
19868 gcc_unreachable ();
19871 /* Support for dynamically-sized objects was introduced by DWARFv3.
19872 At the moment, GDB does not handle variable byte sizes very well,
19873 though. */
19874 if ((dwarf_version >= 3 || !dwarf_strict)
19875 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
19876 && size_expr != NULL)
19877 add_AT_loc (die, DW_AT_byte_size, size_expr);
19879 /* Note that `size' might be -1 when we get to this point. If it is, that
19880 indicates that the byte size of the entity in question is variable and
19881 that we could not generate a DWARF expression that computes it. */
19882 if (size >= 0)
19883 add_AT_unsigned (die, DW_AT_byte_size, size);
19886 /* For a FIELD_DECL node which represents a bit-field, output an attribute
19887 which specifies the distance in bits from the highest order bit of the
19888 "containing object" for the bit-field to the highest order bit of the
19889 bit-field itself.
19891 For any given bit-field, the "containing object" is a hypothetical object
19892 (of some integral or enum type) within which the given bit-field lives. The
19893 type of this hypothetical "containing object" is always the same as the
19894 declared type of the individual bit-field itself. The determination of the
19895 exact location of the "containing object" for a bit-field is rather
19896 complicated. It's handled by the `field_byte_offset' function (above).
19898 CTX is required: see the comment for VLR_CONTEXT.
19900 Note that it is the size (in bytes) of the hypothetical "containing object"
19901 which will be given in the DW_AT_byte_size attribute for this bit-field.
19902 (See `byte_size_attribute' above). */
19904 static inline void
19905 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
19907 HOST_WIDE_INT object_offset_in_bytes;
19908 tree original_type = DECL_BIT_FIELD_TYPE (decl);
19909 HOST_WIDE_INT bitpos_int;
19910 HOST_WIDE_INT highest_order_object_bit_offset;
19911 HOST_WIDE_INT highest_order_field_bit_offset;
19912 HOST_WIDE_INT bit_offset;
19914 field_byte_offset (decl, ctx, &object_offset_in_bytes);
19916 /* Must be a field and a bit field. */
19917 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
19919 /* We can't yet handle bit-fields whose offsets are variable, so if we
19920 encounter such things, just return without generating any attribute
19921 whatsoever. Likewise for variable or too large size. */
19922 if (! tree_fits_shwi_p (bit_position (decl))
19923 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
19924 return;
19926 bitpos_int = int_bit_position (decl);
19928 /* Note that the bit offset is always the distance (in bits) from the
19929 highest-order bit of the "containing object" to the highest-order bit of
19930 the bit-field itself. Since the "high-order end" of any object or field
19931 is different on big-endian and little-endian machines, the computation
19932 below must take account of these differences. */
19933 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
19934 highest_order_field_bit_offset = bitpos_int;
19936 if (! BYTES_BIG_ENDIAN)
19938 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
19939 highest_order_object_bit_offset +=
19940 simple_type_size_in_bits (original_type);
19943 bit_offset
19944 = (! BYTES_BIG_ENDIAN
19945 ? highest_order_object_bit_offset - highest_order_field_bit_offset
19946 : highest_order_field_bit_offset - highest_order_object_bit_offset);
19948 if (bit_offset < 0)
19949 add_AT_int (die, DW_AT_bit_offset, bit_offset);
19950 else
19951 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
19954 /* For a FIELD_DECL node which represents a bit field, output an attribute
19955 which specifies the length in bits of the given field. */
19957 static inline void
19958 add_bit_size_attribute (dw_die_ref die, tree decl)
19960 /* Must be a field and a bit field. */
19961 gcc_assert (TREE_CODE (decl) == FIELD_DECL
19962 && DECL_BIT_FIELD_TYPE (decl));
19964 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
19965 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
19968 /* If the compiled language is ANSI C, then add a 'prototyped'
19969 attribute, if arg types are given for the parameters of a function. */
19971 static inline void
19972 add_prototyped_attribute (dw_die_ref die, tree func_type)
19974 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19976 case DW_LANG_C:
19977 case DW_LANG_C89:
19978 case DW_LANG_C99:
19979 case DW_LANG_C11:
19980 case DW_LANG_ObjC:
19981 if (prototype_p (func_type))
19982 add_AT_flag (die, DW_AT_prototyped, 1);
19983 break;
19984 default:
19985 break;
19989 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
19990 by looking in the type declaration, the object declaration equate table or
19991 the block mapping. */
19993 static inline dw_die_ref
19994 add_abstract_origin_attribute (dw_die_ref die, tree origin)
19996 dw_die_ref origin_die = NULL;
19998 if (TREE_CODE (origin) != FUNCTION_DECL
19999 && TREE_CODE (origin) != BLOCK)
20001 /* We may have gotten separated from the block for the inlined
20002 function, if we're in an exception handler or some such; make
20003 sure that the abstract function has been written out.
20005 Doing this for nested functions is wrong, however; functions are
20006 distinct units, and our context might not even be inline. */
20007 tree fn = origin;
20009 if (TYPE_P (fn))
20010 fn = TYPE_STUB_DECL (fn);
20012 fn = decl_function_context (fn);
20013 if (fn)
20014 dwarf2out_abstract_function (fn);
20017 if (DECL_P (origin))
20018 origin_die = lookup_decl_die (origin);
20019 else if (TYPE_P (origin))
20020 origin_die = lookup_type_die (origin);
20021 else if (TREE_CODE (origin) == BLOCK)
20022 origin_die = BLOCK_DIE (origin);
20024 /* XXX: Functions that are never lowered don't always have correct block
20025 trees (in the case of java, they simply have no block tree, in some other
20026 languages). For these functions, there is nothing we can really do to
20027 output correct debug info for inlined functions in all cases. Rather
20028 than die, we'll just produce deficient debug info now, in that we will
20029 have variables without a proper abstract origin. In the future, when all
20030 functions are lowered, we should re-add a gcc_assert (origin_die)
20031 here. */
20033 if (origin_die)
20034 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20035 return origin_die;
20038 /* We do not currently support the pure_virtual attribute. */
20040 static inline void
20041 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20043 if (DECL_VINDEX (func_decl))
20045 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20047 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20048 add_AT_loc (die, DW_AT_vtable_elem_location,
20049 new_loc_descr (DW_OP_constu,
20050 tree_to_shwi (DECL_VINDEX (func_decl)),
20051 0));
20053 /* GNU extension: Record what type this method came from originally. */
20054 if (debug_info_level > DINFO_LEVEL_TERSE
20055 && DECL_CONTEXT (func_decl))
20056 add_AT_die_ref (die, DW_AT_containing_type,
20057 lookup_type_die (DECL_CONTEXT (func_decl)));
20061 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20062 given decl. This used to be a vendor extension until after DWARF 4
20063 standardized it. */
20065 static void
20066 add_linkage_attr (dw_die_ref die, tree decl)
20068 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20070 /* Mimic what assemble_name_raw does with a leading '*'. */
20071 if (name[0] == '*')
20072 name = &name[1];
20074 if (dwarf_version >= 4)
20075 add_AT_string (die, DW_AT_linkage_name, name);
20076 else
20077 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20080 /* Add source coordinate attributes for the given decl. */
20082 static void
20083 add_src_coords_attributes (dw_die_ref die, tree decl)
20085 expanded_location s;
20087 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20088 return;
20089 s = expand_location (DECL_SOURCE_LOCATION (decl));
20090 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20091 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20094 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20096 static void
20097 add_linkage_name_raw (dw_die_ref die, tree decl)
20099 /* Defer until we have an assembler name set. */
20100 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20102 limbo_die_node *asm_name;
20104 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20105 asm_name->die = die;
20106 asm_name->created_for = decl;
20107 asm_name->next = deferred_asm_name;
20108 deferred_asm_name = asm_name;
20110 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20111 add_linkage_attr (die, decl);
20114 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20116 static void
20117 add_linkage_name (dw_die_ref die, tree decl)
20119 if (debug_info_level > DINFO_LEVEL_NONE
20120 && VAR_OR_FUNCTION_DECL_P (decl)
20121 && TREE_PUBLIC (decl)
20122 && !(VAR_P (decl) && DECL_REGISTER (decl))
20123 && die->die_tag != DW_TAG_member)
20124 add_linkage_name_raw (die, decl);
20127 /* Add a DW_AT_name attribute and source coordinate attribute for the
20128 given decl, but only if it actually has a name. */
20130 static void
20131 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20132 bool no_linkage_name)
20134 tree decl_name;
20136 decl_name = DECL_NAME (decl);
20137 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20139 const char *name = dwarf2_name (decl, 0);
20140 if (name)
20141 add_name_attribute (die, name);
20142 if (! DECL_ARTIFICIAL (decl))
20143 add_src_coords_attributes (die, decl);
20145 if (!no_linkage_name)
20146 add_linkage_name (die, decl);
20149 #ifdef VMS_DEBUGGING_INFO
20150 /* Get the function's name, as described by its RTL. This may be different
20151 from the DECL_NAME name used in the source file. */
20152 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20154 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20155 XEXP (DECL_RTL (decl), 0), false);
20156 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20158 #endif /* VMS_DEBUGGING_INFO */
20161 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20163 static void
20164 add_discr_value (dw_die_ref die, dw_discr_value *value)
20166 dw_attr_node attr;
20168 attr.dw_attr = DW_AT_discr_value;
20169 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20170 attr.dw_attr_val.val_entry = NULL;
20171 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20172 if (value->pos)
20173 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20174 else
20175 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20176 add_dwarf_attr (die, &attr);
20179 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20181 static void
20182 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20184 dw_attr_node attr;
20186 attr.dw_attr = DW_AT_discr_list;
20187 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20188 attr.dw_attr_val.val_entry = NULL;
20189 attr.dw_attr_val.v.val_discr_list = discr_list;
20190 add_dwarf_attr (die, &attr);
20193 static inline dw_discr_list_ref
20194 AT_discr_list (dw_attr_node *attr)
20196 return attr->dw_attr_val.v.val_discr_list;
20199 #ifdef VMS_DEBUGGING_INFO
20200 /* Output the debug main pointer die for VMS */
20202 void
20203 dwarf2out_vms_debug_main_pointer (void)
20205 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20206 dw_die_ref die;
20208 /* Allocate the VMS debug main subprogram die. */
20209 die = ggc_cleared_alloc<die_node> ();
20210 die->die_tag = DW_TAG_subprogram;
20211 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20212 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20213 current_function_funcdef_no);
20214 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20216 /* Make it the first child of comp_unit_die (). */
20217 die->die_parent = comp_unit_die ();
20218 if (comp_unit_die ()->die_child)
20220 die->die_sib = comp_unit_die ()->die_child->die_sib;
20221 comp_unit_die ()->die_child->die_sib = die;
20223 else
20225 die->die_sib = die;
20226 comp_unit_die ()->die_child = die;
20229 #endif /* VMS_DEBUGGING_INFO */
20231 /* Push a new declaration scope. */
20233 static void
20234 push_decl_scope (tree scope)
20236 vec_safe_push (decl_scope_table, scope);
20239 /* Pop a declaration scope. */
20241 static inline void
20242 pop_decl_scope (void)
20244 decl_scope_table->pop ();
20247 /* walk_tree helper function for uses_local_type, below. */
20249 static tree
20250 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20252 if (!TYPE_P (*tp))
20253 *walk_subtrees = 0;
20254 else
20256 tree name = TYPE_NAME (*tp);
20257 if (name && DECL_P (name) && decl_function_context (name))
20258 return *tp;
20260 return NULL_TREE;
20263 /* If TYPE involves a function-local type (including a local typedef to a
20264 non-local type), returns that type; otherwise returns NULL_TREE. */
20266 static tree
20267 uses_local_type (tree type)
20269 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20270 return used;
20273 /* Return the DIE for the scope that immediately contains this type.
20274 Non-named types that do not involve a function-local type get global
20275 scope. Named types nested in namespaces or other types get their
20276 containing scope. All other types (i.e. function-local named types) get
20277 the current active scope. */
20279 static dw_die_ref
20280 scope_die_for (tree t, dw_die_ref context_die)
20282 dw_die_ref scope_die = NULL;
20283 tree containing_scope;
20285 /* Non-types always go in the current scope. */
20286 gcc_assert (TYPE_P (t));
20288 /* Use the scope of the typedef, rather than the scope of the type
20289 it refers to. */
20290 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20291 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20292 else
20293 containing_scope = TYPE_CONTEXT (t);
20295 /* Use the containing namespace if there is one. */
20296 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20298 if (context_die == lookup_decl_die (containing_scope))
20299 /* OK */;
20300 else if (debug_info_level > DINFO_LEVEL_TERSE)
20301 context_die = get_context_die (containing_scope);
20302 else
20303 containing_scope = NULL_TREE;
20306 /* Ignore function type "scopes" from the C frontend. They mean that
20307 a tagged type is local to a parmlist of a function declarator, but
20308 that isn't useful to DWARF. */
20309 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20310 containing_scope = NULL_TREE;
20312 if (SCOPE_FILE_SCOPE_P (containing_scope))
20314 /* If T uses a local type keep it local as well, to avoid references
20315 to function-local DIEs from outside the function. */
20316 if (current_function_decl && uses_local_type (t))
20317 scope_die = context_die;
20318 else
20319 scope_die = comp_unit_die ();
20321 else if (TYPE_P (containing_scope))
20323 /* For types, we can just look up the appropriate DIE. */
20324 if (debug_info_level > DINFO_LEVEL_TERSE)
20325 scope_die = get_context_die (containing_scope);
20326 else
20328 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20329 if (scope_die == NULL)
20330 scope_die = comp_unit_die ();
20333 else
20334 scope_die = context_die;
20336 return scope_die;
20339 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20341 static inline int
20342 local_scope_p (dw_die_ref context_die)
20344 for (; context_die; context_die = context_die->die_parent)
20345 if (context_die->die_tag == DW_TAG_inlined_subroutine
20346 || context_die->die_tag == DW_TAG_subprogram)
20347 return 1;
20349 return 0;
20352 /* Returns nonzero if CONTEXT_DIE is a class. */
20354 static inline int
20355 class_scope_p (dw_die_ref context_die)
20357 return (context_die
20358 && (context_die->die_tag == DW_TAG_structure_type
20359 || context_die->die_tag == DW_TAG_class_type
20360 || context_die->die_tag == DW_TAG_interface_type
20361 || context_die->die_tag == DW_TAG_union_type));
20364 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20365 whether or not to treat a DIE in this context as a declaration. */
20367 static inline int
20368 class_or_namespace_scope_p (dw_die_ref context_die)
20370 return (class_scope_p (context_die)
20371 || (context_die && context_die->die_tag == DW_TAG_namespace));
20374 /* Many forms of DIEs require a "type description" attribute. This
20375 routine locates the proper "type descriptor" die for the type given
20376 by 'type' plus any additional qualifiers given by 'cv_quals', and
20377 adds a DW_AT_type attribute below the given die. */
20379 static void
20380 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20381 bool reverse, dw_die_ref context_die)
20383 enum tree_code code = TREE_CODE (type);
20384 dw_die_ref type_die = NULL;
20386 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20387 or fixed-point type, use the inner type. This is because we have no
20388 support for unnamed types in base_type_die. This can happen if this is
20389 an Ada subrange type. Correct solution is emit a subrange type die. */
20390 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20391 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20392 type = TREE_TYPE (type), code = TREE_CODE (type);
20394 if (code == ERROR_MARK
20395 /* Handle a special case. For functions whose return type is void, we
20396 generate *no* type attribute. (Note that no object may have type
20397 `void', so this only applies to function return types). */
20398 || code == VOID_TYPE)
20399 return;
20401 type_die = modified_type_die (type,
20402 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20403 reverse,
20404 context_die);
20406 if (type_die != NULL)
20407 add_AT_die_ref (object_die, DW_AT_type, type_die);
20410 /* Given an object die, add the calling convention attribute for the
20411 function call type. */
20412 static void
20413 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20415 enum dwarf_calling_convention value = DW_CC_normal;
20417 value = ((enum dwarf_calling_convention)
20418 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20420 if (is_fortran ()
20421 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20423 /* DWARF 2 doesn't provide a way to identify a program's source-level
20424 entry point. DW_AT_calling_convention attributes are only meant
20425 to describe functions' calling conventions. However, lacking a
20426 better way to signal the Fortran main program, we used this for
20427 a long time, following existing custom. Now, DWARF 4 has
20428 DW_AT_main_subprogram, which we add below, but some tools still
20429 rely on the old way, which we thus keep. */
20430 value = DW_CC_program;
20432 if (dwarf_version >= 4 || !dwarf_strict)
20433 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20436 /* Only add the attribute if the backend requests it, and
20437 is not DW_CC_normal. */
20438 if (value && (value != DW_CC_normal))
20439 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20442 /* Given a tree pointer to a struct, class, union, or enum type node, return
20443 a pointer to the (string) tag name for the given type, or zero if the type
20444 was declared without a tag. */
20446 static const char *
20447 type_tag (const_tree type)
20449 const char *name = 0;
20451 if (TYPE_NAME (type) != 0)
20453 tree t = 0;
20455 /* Find the IDENTIFIER_NODE for the type name. */
20456 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20457 && !TYPE_NAMELESS (type))
20458 t = TYPE_NAME (type);
20460 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20461 a TYPE_DECL node, regardless of whether or not a `typedef' was
20462 involved. */
20463 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20464 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20466 /* We want to be extra verbose. Don't call dwarf_name if
20467 DECL_NAME isn't set. The default hook for decl_printable_name
20468 doesn't like that, and in this context it's correct to return
20469 0, instead of "<anonymous>" or the like. */
20470 if (DECL_NAME (TYPE_NAME (type))
20471 && !DECL_NAMELESS (TYPE_NAME (type)))
20472 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20475 /* Now get the name as a string, or invent one. */
20476 if (!name && t != 0)
20477 name = IDENTIFIER_POINTER (t);
20480 return (name == 0 || *name == '\0') ? 0 : name;
20483 /* Return the type associated with a data member, make a special check
20484 for bit field types. */
20486 static inline tree
20487 member_declared_type (const_tree member)
20489 return (DECL_BIT_FIELD_TYPE (member)
20490 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20493 /* Get the decl's label, as described by its RTL. This may be different
20494 from the DECL_NAME name used in the source file. */
20496 #if 0
20497 static const char *
20498 decl_start_label (tree decl)
20500 rtx x;
20501 const char *fnname;
20503 x = DECL_RTL (decl);
20504 gcc_assert (MEM_P (x));
20506 x = XEXP (x, 0);
20507 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20509 fnname = XSTR (x, 0);
20510 return fnname;
20512 #endif
20514 /* For variable-length arrays that have been previously generated, but
20515 may be incomplete due to missing subscript info, fill the subscript
20516 info. Return TRUE if this is one of those cases. */
20517 static bool
20518 fill_variable_array_bounds (tree type)
20520 if (TREE_ASM_WRITTEN (type)
20521 && TREE_CODE (type) == ARRAY_TYPE
20522 && variably_modified_type_p (type, NULL))
20524 dw_die_ref array_die = lookup_type_die (type);
20525 if (!array_die)
20526 return false;
20527 add_subscript_info (array_die, type, !is_ada ());
20528 return true;
20530 return false;
20533 /* These routines generate the internal representation of the DIE's for
20534 the compilation unit. Debugging information is collected by walking
20535 the declaration trees passed in from dwarf2out_decl(). */
20537 static void
20538 gen_array_type_die (tree type, dw_die_ref context_die)
20540 dw_die_ref array_die;
20542 /* GNU compilers represent multidimensional array types as sequences of one
20543 dimensional array types whose element types are themselves array types.
20544 We sometimes squish that down to a single array_type DIE with multiple
20545 subscripts in the Dwarf debugging info. The draft Dwarf specification
20546 say that we are allowed to do this kind of compression in C, because
20547 there is no difference between an array of arrays and a multidimensional
20548 array. We don't do this for Ada to remain as close as possible to the
20549 actual representation, which is especially important against the language
20550 flexibilty wrt arrays of variable size. */
20552 bool collapse_nested_arrays = !is_ada ();
20554 if (fill_variable_array_bounds (type))
20555 return;
20557 dw_die_ref scope_die = scope_die_for (type, context_die);
20558 tree element_type;
20560 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20561 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20562 if (TYPE_STRING_FLAG (type)
20563 && TREE_CODE (type) == ARRAY_TYPE
20564 && is_fortran ()
20565 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20567 HOST_WIDE_INT size;
20569 array_die = new_die (DW_TAG_string_type, scope_die, type);
20570 add_name_attribute (array_die, type_tag (type));
20571 equate_type_number_to_die (type, array_die);
20572 size = int_size_in_bytes (type);
20573 if (size >= 0)
20574 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20575 else if (TYPE_DOMAIN (type) != NULL_TREE
20576 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20578 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20579 tree rszdecl = szdecl;
20580 HOST_WIDE_INT rsize = 0;
20582 size = int_size_in_bytes (TREE_TYPE (szdecl));
20583 if (!DECL_P (szdecl))
20585 if (TREE_CODE (szdecl) == INDIRECT_REF
20586 && DECL_P (TREE_OPERAND (szdecl, 0)))
20588 rszdecl = TREE_OPERAND (szdecl, 0);
20589 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
20590 if (rsize <= 0)
20591 size = 0;
20593 else
20594 size = 0;
20596 if (size > 0)
20598 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
20599 if (loc == NULL
20600 && early_dwarf
20601 && current_function_decl
20602 && DECL_CONTEXT (rszdecl) == current_function_decl)
20604 dw_die_ref ref = lookup_decl_die (rszdecl);
20605 dw_loc_descr_ref l = NULL;
20606 if (ref)
20608 l = new_loc_descr (DW_OP_call4, 0, 0);
20609 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20610 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
20611 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
20613 else if (TREE_CODE (rszdecl) == PARM_DECL
20614 && string_types)
20616 l = new_loc_descr (DW_OP_call4, 0, 0);
20617 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
20618 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
20619 string_types->safe_push (array_die);
20621 if (l && rszdecl != szdecl)
20623 if (rsize == DWARF2_ADDR_SIZE)
20624 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
20625 0, 0));
20626 else
20627 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
20628 rsize, 0));
20630 if (l)
20631 loc = new_loc_list (l, NULL, NULL, NULL);
20633 if (loc)
20635 add_AT_location_description (array_die, DW_AT_string_length,
20636 loc);
20637 if (size != DWARF2_ADDR_SIZE)
20638 add_AT_unsigned (array_die, dwarf_version >= 5
20639 ? DW_AT_string_length_byte_size
20640 : DW_AT_byte_size, size);
20644 return;
20647 array_die = new_die (DW_TAG_array_type, scope_die, type);
20648 add_name_attribute (array_die, type_tag (type));
20649 equate_type_number_to_die (type, array_die);
20651 if (TREE_CODE (type) == VECTOR_TYPE)
20652 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20654 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20655 if (is_fortran ()
20656 && TREE_CODE (type) == ARRAY_TYPE
20657 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20658 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20659 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20661 #if 0
20662 /* We default the array ordering. SDB will probably do
20663 the right things even if DW_AT_ordering is not present. It's not even
20664 an issue until we start to get into multidimensional arrays anyway. If
20665 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20666 then we'll have to put the DW_AT_ordering attribute back in. (But if
20667 and when we find out that we need to put these in, we will only do so
20668 for multidimensional arrays. */
20669 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20670 #endif
20672 if (TREE_CODE (type) == VECTOR_TYPE)
20674 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20675 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20676 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20677 add_bound_info (subrange_die, DW_AT_upper_bound,
20678 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20680 else
20681 add_subscript_info (array_die, type, collapse_nested_arrays);
20683 /* Add representation of the type of the elements of this array type and
20684 emit the corresponding DIE if we haven't done it already. */
20685 element_type = TREE_TYPE (type);
20686 if (collapse_nested_arrays)
20687 while (TREE_CODE (element_type) == ARRAY_TYPE)
20689 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20690 break;
20691 element_type = TREE_TYPE (element_type);
20694 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20695 TREE_CODE (type) == ARRAY_TYPE
20696 && TYPE_REVERSE_STORAGE_ORDER (type),
20697 context_die);
20699 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20700 if (TYPE_ARTIFICIAL (type))
20701 add_AT_flag (array_die, DW_AT_artificial, 1);
20703 if (get_AT (array_die, DW_AT_name))
20704 add_pubtype (type, array_die);
20707 /* After all arguments are created, adjust any DW_TAG_string_type
20708 DIEs DW_AT_string_length attributes. */
20710 static void
20711 adjust_string_types (void)
20713 dw_die_ref array_die;
20714 unsigned int i;
20715 FOR_EACH_VEC_ELT (*string_types, i, array_die)
20717 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
20718 if (a == NULL)
20719 continue;
20720 dw_loc_descr_ref loc = AT_loc (a);
20721 gcc_assert (loc->dw_loc_opc == DW_OP_call4
20722 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
20723 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
20724 if (ref)
20726 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20727 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
20728 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
20730 else
20732 remove_AT (array_die, DW_AT_string_length);
20733 remove_AT (array_die, dwarf_version >= 5
20734 ? DW_AT_string_length_byte_size
20735 : DW_AT_byte_size);
20740 /* This routine generates DIE for array with hidden descriptor, details
20741 are filled into *info by a langhook. */
20743 static void
20744 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20745 dw_die_ref context_die)
20747 const dw_die_ref scope_die = scope_die_for (type, context_die);
20748 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20749 struct loc_descr_context context = { type, info->base_decl, NULL,
20750 false, false };
20751 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20752 int dim;
20754 add_name_attribute (array_die, type_tag (type));
20755 equate_type_number_to_die (type, array_die);
20757 if (info->ndimensions > 1)
20758 switch (info->ordering)
20760 case array_descr_ordering_row_major:
20761 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20762 break;
20763 case array_descr_ordering_column_major:
20764 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20765 break;
20766 default:
20767 break;
20770 if (dwarf_version >= 3 || !dwarf_strict)
20772 if (info->data_location)
20773 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20774 dw_scalar_form_exprloc, &context);
20775 if (info->associated)
20776 add_scalar_info (array_die, DW_AT_associated, info->associated,
20777 dw_scalar_form_constant
20778 | dw_scalar_form_exprloc
20779 | dw_scalar_form_reference, &context);
20780 if (info->allocated)
20781 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20782 dw_scalar_form_constant
20783 | dw_scalar_form_exprloc
20784 | dw_scalar_form_reference, &context);
20785 if (info->stride)
20787 const enum dwarf_attribute attr
20788 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20789 const int forms
20790 = (info->stride_in_bits)
20791 ? dw_scalar_form_constant
20792 : (dw_scalar_form_constant
20793 | dw_scalar_form_exprloc
20794 | dw_scalar_form_reference);
20796 add_scalar_info (array_die, attr, info->stride, forms, &context);
20799 if (dwarf_version >= 5)
20801 if (info->rank)
20803 add_scalar_info (array_die, DW_AT_rank, info->rank,
20804 dw_scalar_form_constant
20805 | dw_scalar_form_exprloc, &context);
20806 subrange_tag = DW_TAG_generic_subrange;
20807 context.placeholder_arg = true;
20811 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20813 for (dim = 0; dim < info->ndimensions; dim++)
20815 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20817 if (info->dimen[dim].bounds_type)
20818 add_type_attribute (subrange_die,
20819 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20820 false, context_die);
20821 if (info->dimen[dim].lower_bound)
20822 add_bound_info (subrange_die, DW_AT_lower_bound,
20823 info->dimen[dim].lower_bound, &context);
20824 if (info->dimen[dim].upper_bound)
20825 add_bound_info (subrange_die, DW_AT_upper_bound,
20826 info->dimen[dim].upper_bound, &context);
20827 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20828 add_scalar_info (subrange_die, DW_AT_byte_stride,
20829 info->dimen[dim].stride,
20830 dw_scalar_form_constant
20831 | dw_scalar_form_exprloc
20832 | dw_scalar_form_reference,
20833 &context);
20836 gen_type_die (info->element_type, context_die);
20837 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20838 TREE_CODE (type) == ARRAY_TYPE
20839 && TYPE_REVERSE_STORAGE_ORDER (type),
20840 context_die);
20842 if (get_AT (array_die, DW_AT_name))
20843 add_pubtype (type, array_die);
20846 #if 0
20847 static void
20848 gen_entry_point_die (tree decl, dw_die_ref context_die)
20850 tree origin = decl_ultimate_origin (decl);
20851 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20853 if (origin != NULL)
20854 add_abstract_origin_attribute (decl_die, origin);
20855 else
20857 add_name_and_src_coords_attributes (decl_die, decl);
20858 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20859 TYPE_UNQUALIFIED, false, context_die);
20862 if (DECL_ABSTRACT_P (decl))
20863 equate_decl_number_to_die (decl, decl_die);
20864 else
20865 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
20867 #endif
20869 /* Walk through the list of incomplete types again, trying once more to
20870 emit full debugging info for them. */
20872 static void
20873 retry_incomplete_types (void)
20875 set_early_dwarf s;
20876 int i;
20878 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
20879 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
20880 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
20881 vec_safe_truncate (incomplete_types, 0);
20884 /* Determine what tag to use for a record type. */
20886 static enum dwarf_tag
20887 record_type_tag (tree type)
20889 if (! lang_hooks.types.classify_record)
20890 return DW_TAG_structure_type;
20892 switch (lang_hooks.types.classify_record (type))
20894 case RECORD_IS_STRUCT:
20895 return DW_TAG_structure_type;
20897 case RECORD_IS_CLASS:
20898 return DW_TAG_class_type;
20900 case RECORD_IS_INTERFACE:
20901 if (dwarf_version >= 3 || !dwarf_strict)
20902 return DW_TAG_interface_type;
20903 return DW_TAG_structure_type;
20905 default:
20906 gcc_unreachable ();
20910 /* Generate a DIE to represent an enumeration type. Note that these DIEs
20911 include all of the information about the enumeration values also. Each
20912 enumerated type name/value is listed as a child of the enumerated type
20913 DIE. */
20915 static dw_die_ref
20916 gen_enumeration_type_die (tree type, dw_die_ref context_die)
20918 dw_die_ref type_die = lookup_type_die (type);
20920 if (type_die == NULL)
20922 type_die = new_die (DW_TAG_enumeration_type,
20923 scope_die_for (type, context_die), type);
20924 equate_type_number_to_die (type, type_die);
20925 add_name_attribute (type_die, type_tag (type));
20926 if (dwarf_version >= 4 || !dwarf_strict)
20928 if (ENUM_IS_SCOPED (type))
20929 add_AT_flag (type_die, DW_AT_enum_class, 1);
20930 if (ENUM_IS_OPAQUE (type))
20931 add_AT_flag (type_die, DW_AT_declaration, 1);
20934 else if (! TYPE_SIZE (type))
20935 return type_die;
20936 else
20937 remove_AT (type_die, DW_AT_declaration);
20939 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
20940 given enum type is incomplete, do not generate the DW_AT_byte_size
20941 attribute or the DW_AT_element_list attribute. */
20942 if (TYPE_SIZE (type))
20944 tree link;
20946 TREE_ASM_WRITTEN (type) = 1;
20947 add_byte_size_attribute (type_die, type);
20948 if (dwarf_version >= 3 || !dwarf_strict)
20950 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
20951 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
20952 context_die);
20954 if (TYPE_STUB_DECL (type) != NULL_TREE)
20956 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20957 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20960 /* If the first reference to this type was as the return type of an
20961 inline function, then it may not have a parent. Fix this now. */
20962 if (type_die->die_parent == NULL)
20963 add_child_die (scope_die_for (type, context_die), type_die);
20965 for (link = TYPE_VALUES (type);
20966 link != NULL; link = TREE_CHAIN (link))
20968 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
20969 tree value = TREE_VALUE (link);
20971 add_name_attribute (enum_die,
20972 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
20974 if (TREE_CODE (value) == CONST_DECL)
20975 value = DECL_INITIAL (value);
20977 if (simple_type_size_in_bits (TREE_TYPE (value))
20978 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
20980 /* For constant forms created by add_AT_unsigned DWARF
20981 consumers (GDB, elfutils, etc.) always zero extend
20982 the value. Only when the actual value is negative
20983 do we need to use add_AT_int to generate a constant
20984 form that can represent negative values. */
20985 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
20986 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
20987 add_AT_unsigned (enum_die, DW_AT_const_value,
20988 (unsigned HOST_WIDE_INT) val);
20989 else
20990 add_AT_int (enum_die, DW_AT_const_value, val);
20992 else
20993 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
20994 that here. TODO: This should be re-worked to use correct
20995 signed/unsigned double tags for all cases. */
20996 add_AT_wide (enum_die, DW_AT_const_value, value);
20999 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21000 if (TYPE_ARTIFICIAL (type))
21001 add_AT_flag (type_die, DW_AT_artificial, 1);
21003 else
21004 add_AT_flag (type_die, DW_AT_declaration, 1);
21006 add_pubtype (type, type_die);
21008 return type_die;
21011 /* Generate a DIE to represent either a real live formal parameter decl or to
21012 represent just the type of some formal parameter position in some function
21013 type.
21015 Note that this routine is a bit unusual because its argument may be a
21016 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21017 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21018 node. If it's the former then this function is being called to output a
21019 DIE to represent a formal parameter object (or some inlining thereof). If
21020 it's the latter, then this function is only being called to output a
21021 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21022 argument type of some subprogram type.
21023 If EMIT_NAME_P is true, name and source coordinate attributes
21024 are emitted. */
21026 static dw_die_ref
21027 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21028 dw_die_ref context_die)
21030 tree node_or_origin = node ? node : origin;
21031 tree ultimate_origin;
21032 dw_die_ref parm_die = NULL;
21034 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21036 parm_die = lookup_decl_die (node);
21038 /* If the contexts differ, we may not be talking about the same
21039 thing. */
21040 if (parm_die && parm_die->die_parent != context_die)
21042 if (!DECL_ABSTRACT_P (node))
21044 /* This can happen when creating an inlined instance, in
21045 which case we need to create a new DIE that will get
21046 annotated with DW_AT_abstract_origin. */
21047 parm_die = NULL;
21049 else
21051 /* FIXME: Reuse DIE even with a differing context.
21053 This can happen when calling
21054 dwarf2out_abstract_function to build debug info for
21055 the abstract instance of a function for which we have
21056 already generated a DIE in
21057 dwarf2out_early_global_decl.
21059 Once we remove dwarf2out_abstract_function, we should
21060 have a call to gcc_unreachable here. */
21064 if (parm_die && parm_die->die_parent == NULL)
21066 /* Check that parm_die already has the right attributes that
21067 we would have added below. If any attributes are
21068 missing, fall through to add them. */
21069 if (! DECL_ABSTRACT_P (node_or_origin)
21070 && !get_AT (parm_die, DW_AT_location)
21071 && !get_AT (parm_die, DW_AT_const_value))
21072 /* We are missing location info, and are about to add it. */
21074 else
21076 add_child_die (context_die, parm_die);
21077 return parm_die;
21082 /* If we have a previously generated DIE, use it, unless this is an
21083 concrete instance (origin != NULL), in which case we need a new
21084 DIE with a corresponding DW_AT_abstract_origin. */
21085 bool reusing_die;
21086 if (parm_die && origin == NULL)
21087 reusing_die = true;
21088 else
21090 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21091 reusing_die = false;
21094 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21096 case tcc_declaration:
21097 ultimate_origin = decl_ultimate_origin (node_or_origin);
21098 if (node || ultimate_origin)
21099 origin = ultimate_origin;
21101 if (reusing_die)
21102 goto add_location;
21104 if (origin != NULL)
21105 add_abstract_origin_attribute (parm_die, origin);
21106 else if (emit_name_p)
21107 add_name_and_src_coords_attributes (parm_die, node);
21108 if (origin == NULL
21109 || (! DECL_ABSTRACT_P (node_or_origin)
21110 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21111 decl_function_context
21112 (node_or_origin))))
21114 tree type = TREE_TYPE (node_or_origin);
21115 if (decl_by_reference_p (node_or_origin))
21116 add_type_attribute (parm_die, TREE_TYPE (type),
21117 TYPE_UNQUALIFIED,
21118 false, context_die);
21119 else
21120 add_type_attribute (parm_die, type,
21121 decl_quals (node_or_origin),
21122 false, context_die);
21124 if (origin == NULL && DECL_ARTIFICIAL (node))
21125 add_AT_flag (parm_die, DW_AT_artificial, 1);
21126 add_location:
21127 if (node && node != origin)
21128 equate_decl_number_to_die (node, parm_die);
21129 if (! DECL_ABSTRACT_P (node_or_origin))
21130 add_location_or_const_value_attribute (parm_die, node_or_origin,
21131 node == NULL);
21133 break;
21135 case tcc_type:
21136 /* We were called with some kind of a ..._TYPE node. */
21137 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21138 context_die);
21139 break;
21141 default:
21142 gcc_unreachable ();
21145 return parm_die;
21148 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21149 children DW_TAG_formal_parameter DIEs representing the arguments of the
21150 parameter pack.
21152 PARM_PACK must be a function parameter pack.
21153 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21154 must point to the subsequent arguments of the function PACK_ARG belongs to.
21155 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21156 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21157 following the last one for which a DIE was generated. */
21159 static dw_die_ref
21160 gen_formal_parameter_pack_die (tree parm_pack,
21161 tree pack_arg,
21162 dw_die_ref subr_die,
21163 tree *next_arg)
21165 tree arg;
21166 dw_die_ref parm_pack_die;
21168 gcc_assert (parm_pack
21169 && lang_hooks.function_parameter_pack_p (parm_pack)
21170 && subr_die);
21172 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21173 add_src_coords_attributes (parm_pack_die, parm_pack);
21175 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21177 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21178 parm_pack))
21179 break;
21180 gen_formal_parameter_die (arg, NULL,
21181 false /* Don't emit name attribute. */,
21182 parm_pack_die);
21184 if (next_arg)
21185 *next_arg = arg;
21186 return parm_pack_die;
21189 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21190 at the end of an (ANSI prototyped) formal parameters list. */
21192 static void
21193 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21195 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21198 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21199 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21200 parameters as specified in some function type specification (except for
21201 those which appear as part of a function *definition*). */
21203 static void
21204 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21206 tree link;
21207 tree formal_type = NULL;
21208 tree first_parm_type;
21209 tree arg;
21211 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21213 arg = DECL_ARGUMENTS (function_or_method_type);
21214 function_or_method_type = TREE_TYPE (function_or_method_type);
21216 else
21217 arg = NULL_TREE;
21219 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21221 /* Make our first pass over the list of formal parameter types and output a
21222 DW_TAG_formal_parameter DIE for each one. */
21223 for (link = first_parm_type; link; )
21225 dw_die_ref parm_die;
21227 formal_type = TREE_VALUE (link);
21228 if (formal_type == void_type_node)
21229 break;
21231 /* Output a (nameless) DIE to represent the formal parameter itself. */
21232 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21234 parm_die = gen_formal_parameter_die (formal_type, NULL,
21235 true /* Emit name attribute. */,
21236 context_die);
21237 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21238 && link == first_parm_type)
21240 add_AT_flag (parm_die, DW_AT_artificial, 1);
21241 if (dwarf_version >= 3 || !dwarf_strict)
21242 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21244 else if (arg && DECL_ARTIFICIAL (arg))
21245 add_AT_flag (parm_die, DW_AT_artificial, 1);
21248 link = TREE_CHAIN (link);
21249 if (arg)
21250 arg = DECL_CHAIN (arg);
21253 /* If this function type has an ellipsis, add a
21254 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21255 if (formal_type != void_type_node)
21256 gen_unspecified_parameters_die (function_or_method_type, context_die);
21258 /* Make our second (and final) pass over the list of formal parameter types
21259 and output DIEs to represent those types (as necessary). */
21260 for (link = TYPE_ARG_TYPES (function_or_method_type);
21261 link && TREE_VALUE (link);
21262 link = TREE_CHAIN (link))
21263 gen_type_die (TREE_VALUE (link), context_die);
21266 /* We want to generate the DIE for TYPE so that we can generate the
21267 die for MEMBER, which has been defined; we will need to refer back
21268 to the member declaration nested within TYPE. If we're trying to
21269 generate minimal debug info for TYPE, processing TYPE won't do the
21270 trick; we need to attach the member declaration by hand. */
21272 static void
21273 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21275 gen_type_die (type, context_die);
21277 /* If we're trying to avoid duplicate debug info, we may not have
21278 emitted the member decl for this function. Emit it now. */
21279 if (TYPE_STUB_DECL (type)
21280 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21281 && ! lookup_decl_die (member))
21283 dw_die_ref type_die;
21284 gcc_assert (!decl_ultimate_origin (member));
21286 push_decl_scope (type);
21287 type_die = lookup_type_die_strip_naming_typedef (type);
21288 if (TREE_CODE (member) == FUNCTION_DECL)
21289 gen_subprogram_die (member, type_die);
21290 else if (TREE_CODE (member) == FIELD_DECL)
21292 /* Ignore the nameless fields that are used to skip bits but handle
21293 C++ anonymous unions and structs. */
21294 if (DECL_NAME (member) != NULL_TREE
21295 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21296 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21298 struct vlr_context vlr_ctx = {
21299 DECL_CONTEXT (member), /* struct_type */
21300 NULL_TREE /* variant_part_offset */
21302 gen_type_die (member_declared_type (member), type_die);
21303 gen_field_die (member, &vlr_ctx, type_die);
21306 else
21307 gen_variable_die (member, NULL_TREE, type_die);
21309 pop_decl_scope ();
21313 /* Forward declare these functions, because they are mutually recursive
21314 with their set_block_* pairing functions. */
21315 static void set_decl_origin_self (tree);
21316 static void set_decl_abstract_flags (tree, vec<tree> &);
21318 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21319 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21320 that it points to the node itself, thus indicating that the node is its
21321 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21322 the given node is NULL, recursively descend the decl/block tree which
21323 it is the root of, and for each other ..._DECL or BLOCK node contained
21324 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21325 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21326 values to point to themselves. */
21328 static void
21329 set_block_origin_self (tree stmt)
21331 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21333 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21336 tree local_decl;
21338 for (local_decl = BLOCK_VARS (stmt);
21339 local_decl != NULL_TREE;
21340 local_decl = DECL_CHAIN (local_decl))
21341 /* Do not recurse on nested functions since the inlining status
21342 of parent and child can be different as per the DWARF spec. */
21343 if (TREE_CODE (local_decl) != FUNCTION_DECL
21344 && !DECL_EXTERNAL (local_decl))
21345 set_decl_origin_self (local_decl);
21349 tree subblock;
21351 for (subblock = BLOCK_SUBBLOCKS (stmt);
21352 subblock != NULL_TREE;
21353 subblock = BLOCK_CHAIN (subblock))
21354 set_block_origin_self (subblock); /* Recurse. */
21359 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21360 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21361 node to so that it points to the node itself, thus indicating that the
21362 node represents its own (abstract) origin. Additionally, if the
21363 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21364 the decl/block tree of which the given node is the root of, and for
21365 each other ..._DECL or BLOCK node contained therein whose
21366 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21367 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21368 point to themselves. */
21370 static void
21371 set_decl_origin_self (tree decl)
21373 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21375 DECL_ABSTRACT_ORIGIN (decl) = decl;
21376 if (TREE_CODE (decl) == FUNCTION_DECL)
21378 tree arg;
21380 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21381 DECL_ABSTRACT_ORIGIN (arg) = arg;
21382 if (DECL_INITIAL (decl) != NULL_TREE
21383 && DECL_INITIAL (decl) != error_mark_node)
21384 set_block_origin_self (DECL_INITIAL (decl));
21389 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21390 and if it wasn't 1 before, push it to abstract_vec vector.
21391 For all local decls and all local sub-blocks (recursively) do it
21392 too. */
21394 static void
21395 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21397 tree local_decl;
21398 tree subblock;
21399 unsigned int i;
21401 if (!BLOCK_ABSTRACT (stmt))
21403 abstract_vec.safe_push (stmt);
21404 BLOCK_ABSTRACT (stmt) = 1;
21407 for (local_decl = BLOCK_VARS (stmt);
21408 local_decl != NULL_TREE;
21409 local_decl = DECL_CHAIN (local_decl))
21410 if (! DECL_EXTERNAL (local_decl))
21411 set_decl_abstract_flags (local_decl, abstract_vec);
21413 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21415 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21416 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21417 || TREE_CODE (local_decl) == PARM_DECL)
21418 set_decl_abstract_flags (local_decl, abstract_vec);
21421 for (subblock = BLOCK_SUBBLOCKS (stmt);
21422 subblock != NULL_TREE;
21423 subblock = BLOCK_CHAIN (subblock))
21424 set_block_abstract_flags (subblock, abstract_vec);
21427 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21428 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21429 In the case where the decl is a FUNCTION_DECL also set the abstract
21430 flags for all of the parameters, local vars, local
21431 blocks and sub-blocks (recursively). */
21433 static void
21434 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21436 if (!DECL_ABSTRACT_P (decl))
21438 abstract_vec.safe_push (decl);
21439 DECL_ABSTRACT_P (decl) = 1;
21442 if (TREE_CODE (decl) == FUNCTION_DECL)
21444 tree arg;
21446 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21447 if (!DECL_ABSTRACT_P (arg))
21449 abstract_vec.safe_push (arg);
21450 DECL_ABSTRACT_P (arg) = 1;
21452 if (DECL_INITIAL (decl) != NULL_TREE
21453 && DECL_INITIAL (decl) != error_mark_node)
21454 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21458 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21459 may later generate inlined and/or out-of-line instances of.
21461 FIXME: In the early-dwarf world, this function, and most of the
21462 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21463 the abstract instance. All we would need to do is annotate
21464 the early DIE with the appropriate DW_AT_inline in late
21465 dwarf (perhaps in gen_inlined_subroutine_die).
21467 However, we can't do this yet, because LTO streaming of DIEs
21468 has not been implemented yet. */
21470 static void
21471 dwarf2out_abstract_function (tree decl)
21473 dw_die_ref old_die;
21474 tree save_fn;
21475 tree context;
21476 hash_table<decl_loc_hasher> *old_decl_loc_table;
21477 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21478 int old_call_site_count, old_tail_call_site_count;
21479 struct call_arg_loc_node *old_call_arg_locations;
21481 /* Make sure we have the actual abstract inline, not a clone. */
21482 decl = DECL_ORIGIN (decl);
21484 old_die = lookup_decl_die (decl);
21485 if (old_die && get_AT (old_die, DW_AT_inline))
21486 /* We've already generated the abstract instance. */
21487 return;
21489 /* We can be called while recursively when seeing block defining inlined subroutine
21490 DIE. Be sure to not clobber the outer location table nor use it or we would
21491 get locations in abstract instantces. */
21492 old_decl_loc_table = decl_loc_table;
21493 decl_loc_table = NULL;
21494 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21495 cached_dw_loc_list_table = NULL;
21496 old_call_arg_locations = call_arg_locations;
21497 call_arg_locations = NULL;
21498 old_call_site_count = call_site_count;
21499 call_site_count = -1;
21500 old_tail_call_site_count = tail_call_site_count;
21501 tail_call_site_count = -1;
21503 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21504 we don't get confused by DECL_ABSTRACT_P. */
21505 if (debug_info_level > DINFO_LEVEL_TERSE)
21507 context = decl_class_context (decl);
21508 if (context)
21509 gen_type_die_for_member
21510 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21513 /* Pretend we've just finished compiling this function. */
21514 save_fn = current_function_decl;
21515 current_function_decl = decl;
21517 auto_vec<tree, 64> abstract_vec;
21518 set_decl_abstract_flags (decl, abstract_vec);
21519 dwarf2out_decl (decl);
21520 unsigned int i;
21521 tree t;
21522 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21523 if (TREE_CODE (t) == BLOCK)
21524 BLOCK_ABSTRACT (t) = 0;
21525 else
21526 DECL_ABSTRACT_P (t) = 0;
21528 current_function_decl = save_fn;
21529 decl_loc_table = old_decl_loc_table;
21530 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21531 call_arg_locations = old_call_arg_locations;
21532 call_site_count = old_call_site_count;
21533 tail_call_site_count = old_tail_call_site_count;
21536 /* Helper function of premark_used_types() which gets called through
21537 htab_traverse.
21539 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21540 marked as unused by prune_unused_types. */
21542 bool
21543 premark_used_types_helper (tree const &type, void *)
21545 dw_die_ref die;
21547 die = lookup_type_die (type);
21548 if (die != NULL)
21549 die->die_perennial_p = 1;
21550 return true;
21553 /* Helper function of premark_types_used_by_global_vars which gets called
21554 through htab_traverse.
21556 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21557 marked as unused by prune_unused_types. The DIE of the type is marked
21558 only if the global variable using the type will actually be emitted. */
21561 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21562 void *)
21564 struct types_used_by_vars_entry *entry;
21565 dw_die_ref die;
21567 entry = (struct types_used_by_vars_entry *) *slot;
21568 gcc_assert (entry->type != NULL
21569 && entry->var_decl != NULL);
21570 die = lookup_type_die (entry->type);
21571 if (die)
21573 /* Ask cgraph if the global variable really is to be emitted.
21574 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21575 varpool_node *node = varpool_node::get (entry->var_decl);
21576 if (node && node->definition)
21578 die->die_perennial_p = 1;
21579 /* Keep the parent DIEs as well. */
21580 while ((die = die->die_parent) && die->die_perennial_p == 0)
21581 die->die_perennial_p = 1;
21584 return 1;
21587 /* Mark all members of used_types_hash as perennial. */
21589 static void
21590 premark_used_types (struct function *fun)
21592 if (fun && fun->used_types_hash)
21593 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21596 /* Mark all members of types_used_by_vars_entry as perennial. */
21598 static void
21599 premark_types_used_by_global_vars (void)
21601 if (types_used_by_vars_hash)
21602 types_used_by_vars_hash
21603 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21606 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21607 for CA_LOC call arg loc node. */
21609 static dw_die_ref
21610 gen_call_site_die (tree decl, dw_die_ref subr_die,
21611 struct call_arg_loc_node *ca_loc)
21613 dw_die_ref stmt_die = NULL, die;
21614 tree block = ca_loc->block;
21616 while (block
21617 && block != DECL_INITIAL (decl)
21618 && TREE_CODE (block) == BLOCK)
21620 stmt_die = BLOCK_DIE (block);
21621 if (stmt_die)
21622 break;
21623 block = BLOCK_SUPERCONTEXT (block);
21625 if (stmt_die == NULL)
21626 stmt_die = subr_die;
21627 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21628 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21629 if (ca_loc->tail_call_p)
21630 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21631 if (ca_loc->symbol_ref)
21633 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21634 if (tdie)
21635 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21636 else
21637 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21638 false);
21640 return die;
21643 /* Generate a DIE to represent a declared function (either file-scope or
21644 block-local). */
21646 static void
21647 gen_subprogram_die (tree decl, dw_die_ref context_die)
21649 tree origin = decl_ultimate_origin (decl);
21650 dw_die_ref subr_die;
21651 dw_die_ref old_die = lookup_decl_die (decl);
21653 /* This function gets called multiple times for different stages of
21654 the debug process. For example, for func() in this code:
21656 namespace S
21658 void func() { ... }
21661 ...we get called 4 times. Twice in early debug and twice in
21662 late debug:
21664 Early debug
21665 -----------
21667 1. Once while generating func() within the namespace. This is
21668 the declaration. The declaration bit below is set, as the
21669 context is the namespace.
21671 A new DIE will be generated with DW_AT_declaration set.
21673 2. Once for func() itself. This is the specification. The
21674 declaration bit below is clear as the context is the CU.
21676 We will use the cached DIE from (1) to create a new DIE with
21677 DW_AT_specification pointing to the declaration in (1).
21679 Late debug via rest_of_handle_final()
21680 -------------------------------------
21682 3. Once generating func() within the namespace. This is also the
21683 declaration, as in (1), but this time we will early exit below
21684 as we have a cached DIE and a declaration needs no additional
21685 annotations (no locations), as the source declaration line
21686 info is enough.
21688 4. Once for func() itself. As in (2), this is the specification,
21689 but this time we will re-use the cached DIE, and just annotate
21690 it with the location information that should now be available.
21692 For something without namespaces, but with abstract instances, we
21693 are also called a multiple times:
21695 class Base
21697 public:
21698 Base (); // constructor declaration (1)
21701 Base::Base () { } // constructor specification (2)
21703 Early debug
21704 -----------
21706 1. Once for the Base() constructor by virtue of it being a
21707 member of the Base class. This is done via
21708 rest_of_type_compilation.
21710 This is a declaration, so a new DIE will be created with
21711 DW_AT_declaration.
21713 2. Once for the Base() constructor definition, but this time
21714 while generating the abstract instance of the base
21715 constructor (__base_ctor) which is being generated via early
21716 debug of reachable functions.
21718 Even though we have a cached version of the declaration (1),
21719 we will create a DW_AT_specification of the declaration DIE
21720 in (1).
21722 3. Once for the __base_ctor itself, but this time, we generate
21723 an DW_AT_abstract_origin version of the DW_AT_specification in
21724 (2).
21726 Late debug via rest_of_handle_final
21727 -----------------------------------
21729 4. One final time for the __base_ctor (which will have a cached
21730 DIE with DW_AT_abstract_origin created in (3). This time,
21731 we will just annotate the location information now
21732 available.
21734 int declaration = (current_function_decl != decl
21735 || class_or_namespace_scope_p (context_die));
21737 /* Now that the C++ front end lazily declares artificial member fns, we
21738 might need to retrofit the declaration into its class. */
21739 if (!declaration && !origin && !old_die
21740 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21741 && !class_or_namespace_scope_p (context_die)
21742 && debug_info_level > DINFO_LEVEL_TERSE)
21743 old_die = force_decl_die (decl);
21745 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21746 if (origin != NULL)
21748 gcc_assert (!declaration || local_scope_p (context_die));
21750 /* Fixup die_parent for the abstract instance of a nested
21751 inline function. */
21752 if (old_die && old_die->die_parent == NULL)
21753 add_child_die (context_die, old_die);
21755 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21757 /* If we have a DW_AT_abstract_origin we have a working
21758 cached version. */
21759 subr_die = old_die;
21761 else
21763 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21764 add_abstract_origin_attribute (subr_die, origin);
21765 /* This is where the actual code for a cloned function is.
21766 Let's emit linkage name attribute for it. This helps
21767 debuggers to e.g, set breakpoints into
21768 constructors/destructors when the user asks "break
21769 K::K". */
21770 add_linkage_name (subr_die, decl);
21773 /* A cached copy, possibly from early dwarf generation. Reuse as
21774 much as possible. */
21775 else if (old_die)
21777 /* A declaration that has been previously dumped needs no
21778 additional information. */
21779 if (declaration)
21780 return;
21782 if (!get_AT_flag (old_die, DW_AT_declaration)
21783 /* We can have a normal definition following an inline one in the
21784 case of redefinition of GNU C extern inlines.
21785 It seems reasonable to use AT_specification in this case. */
21786 && !get_AT (old_die, DW_AT_inline))
21788 /* Detect and ignore this case, where we are trying to output
21789 something we have already output. */
21790 if (get_AT (old_die, DW_AT_low_pc)
21791 || get_AT (old_die, DW_AT_ranges))
21792 return;
21794 /* If we have no location information, this must be a
21795 partially generated DIE from early dwarf generation.
21796 Fall through and generate it. */
21799 /* If the definition comes from the same place as the declaration,
21800 maybe use the old DIE. We always want the DIE for this function
21801 that has the *_pc attributes to be under comp_unit_die so the
21802 debugger can find it. We also need to do this for abstract
21803 instances of inlines, since the spec requires the out-of-line copy
21804 to have the same parent. For local class methods, this doesn't
21805 apply; we just use the old DIE. */
21806 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21807 struct dwarf_file_data * file_index = lookup_filename (s.file);
21808 if ((is_cu_die (old_die->die_parent)
21809 /* This condition fixes the inconsistency/ICE with the
21810 following Fortran test (or some derivative thereof) while
21811 building libgfortran:
21813 module some_m
21814 contains
21815 logical function funky (FLAG)
21816 funky = .true.
21817 end function
21818 end module
21820 || (old_die->die_parent
21821 && old_die->die_parent->die_tag == DW_TAG_module)
21822 || context_die == NULL)
21823 && (DECL_ARTIFICIAL (decl)
21824 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21825 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21826 == (unsigned) s.line))))
21828 subr_die = old_die;
21830 /* Clear out the declaration attribute, but leave the
21831 parameters so they can be augmented with location
21832 information later. Unless this was a declaration, in
21833 which case, wipe out the nameless parameters and recreate
21834 them further down. */
21835 if (remove_AT (subr_die, DW_AT_declaration))
21838 remove_AT (subr_die, DW_AT_object_pointer);
21839 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21842 /* Make a specification pointing to the previously built
21843 declaration. */
21844 else
21846 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21847 add_AT_specification (subr_die, old_die);
21848 add_pubname (decl, subr_die);
21849 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21850 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21851 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21852 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21854 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21855 emit the real type on the definition die. */
21856 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
21858 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
21859 if (die == auto_die || die == decltype_auto_die)
21860 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21861 TYPE_UNQUALIFIED, false, context_die);
21864 /* When we process the method declaration, we haven't seen
21865 the out-of-class defaulted definition yet, so we have to
21866 recheck now. */
21867 if ((dwarf_version >= 5 || ! dwarf_strict)
21868 && !get_AT (subr_die, DW_AT_defaulted))
21870 int defaulted
21871 = lang_hooks.decls.decl_dwarf_attribute (decl,
21872 DW_AT_defaulted);
21873 if (defaulted != -1)
21875 /* Other values must have been handled before. */
21876 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
21877 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21882 /* Create a fresh DIE for anything else. */
21883 else
21885 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21887 if (TREE_PUBLIC (decl))
21888 add_AT_flag (subr_die, DW_AT_external, 1);
21890 add_name_and_src_coords_attributes (subr_die, decl);
21891 add_pubname (decl, subr_die);
21892 if (debug_info_level > DINFO_LEVEL_TERSE)
21894 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
21895 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21896 TYPE_UNQUALIFIED, false, context_die);
21899 add_pure_or_virtual_attribute (subr_die, decl);
21900 if (DECL_ARTIFICIAL (decl))
21901 add_AT_flag (subr_die, DW_AT_artificial, 1);
21903 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
21904 add_AT_flag (subr_die, DW_AT_noreturn, 1);
21906 add_accessibility_attribute (subr_die, decl);
21909 /* Unless we have an existing non-declaration DIE, equate the new
21910 DIE. */
21911 if (!old_die || is_declaration_die (old_die))
21912 equate_decl_number_to_die (decl, subr_die);
21914 if (declaration)
21916 if (!old_die || !get_AT (old_die, DW_AT_inline))
21918 add_AT_flag (subr_die, DW_AT_declaration, 1);
21920 /* If this is an explicit function declaration then generate
21921 a DW_AT_explicit attribute. */
21922 if ((dwarf_version >= 3 || !dwarf_strict)
21923 && lang_hooks.decls.decl_dwarf_attribute (decl,
21924 DW_AT_explicit) == 1)
21925 add_AT_flag (subr_die, DW_AT_explicit, 1);
21927 /* If this is a C++11 deleted special function member then generate
21928 a DW_AT_deleted attribute. */
21929 if ((dwarf_version >= 5 || !dwarf_strict)
21930 && lang_hooks.decls.decl_dwarf_attribute (decl,
21931 DW_AT_deleted) == 1)
21932 add_AT_flag (subr_die, DW_AT_deleted, 1);
21934 /* If this is a C++11 defaulted special function member then
21935 generate a DW_AT_defaulted attribute. */
21936 if (dwarf_version >= 5 || !dwarf_strict)
21938 int defaulted
21939 = lang_hooks.decls.decl_dwarf_attribute (decl,
21940 DW_AT_defaulted);
21941 if (defaulted != -1)
21942 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21945 /* If this is a C++11 non-static member function with & ref-qualifier
21946 then generate a DW_AT_reference attribute. */
21947 if ((dwarf_version >= 5 || !dwarf_strict)
21948 && lang_hooks.decls.decl_dwarf_attribute (decl,
21949 DW_AT_reference) == 1)
21950 add_AT_flag (subr_die, DW_AT_reference, 1);
21952 /* If this is a C++11 non-static member function with &&
21953 ref-qualifier then generate a DW_AT_reference attribute. */
21954 if ((dwarf_version >= 5 || !dwarf_strict)
21955 && lang_hooks.decls.decl_dwarf_attribute (decl,
21956 DW_AT_rvalue_reference)
21957 == 1)
21958 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
21961 /* Tag abstract instances with DW_AT_inline. */
21962 else if (DECL_ABSTRACT_P (decl))
21964 if (DECL_DECLARED_INLINE_P (decl))
21966 if (cgraph_function_possibly_inlined_p (decl))
21967 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
21968 else
21969 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
21971 else
21973 if (cgraph_function_possibly_inlined_p (decl))
21974 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
21975 else
21976 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
21979 if (DECL_DECLARED_INLINE_P (decl)
21980 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21981 add_AT_flag (subr_die, DW_AT_artificial, 1);
21983 /* For non DECL_EXTERNALs, if range information is available, fill
21984 the DIE with it. */
21985 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
21987 HOST_WIDE_INT cfa_fb_offset;
21989 struct function *fun = DECL_STRUCT_FUNCTION (decl);
21991 if (!flag_reorder_blocks_and_partition)
21993 dw_fde_ref fde = fun->fde;
21994 if (fde->dw_fde_begin)
21996 /* We have already generated the labels. */
21997 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
21998 fde->dw_fde_end, false);
22000 else
22002 /* Create start/end labels and add the range. */
22003 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22004 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22005 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22006 current_function_funcdef_no);
22007 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22008 current_function_funcdef_no);
22009 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22010 false);
22013 #if VMS_DEBUGGING_INFO
22014 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22015 Section 2.3 Prologue and Epilogue Attributes:
22016 When a breakpoint is set on entry to a function, it is generally
22017 desirable for execution to be suspended, not on the very first
22018 instruction of the function, but rather at a point after the
22019 function's frame has been set up, after any language defined local
22020 declaration processing has been completed, and before execution of
22021 the first statement of the function begins. Debuggers generally
22022 cannot properly determine where this point is. Similarly for a
22023 breakpoint set on exit from a function. The prologue and epilogue
22024 attributes allow a compiler to communicate the location(s) to use. */
22027 if (fde->dw_fde_vms_end_prologue)
22028 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22029 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22031 if (fde->dw_fde_vms_begin_epilogue)
22032 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22033 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22035 #endif
22038 else
22040 /* Generate pubnames entries for the split function code ranges. */
22041 dw_fde_ref fde = fun->fde;
22043 if (fde->dw_fde_second_begin)
22045 if (dwarf_version >= 3 || !dwarf_strict)
22047 /* We should use ranges for non-contiguous code section
22048 addresses. Use the actual code range for the initial
22049 section, since the HOT/COLD labels might precede an
22050 alignment offset. */
22051 bool range_list_added = false;
22052 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22053 fde->dw_fde_end, &range_list_added,
22054 false);
22055 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22056 fde->dw_fde_second_end,
22057 &range_list_added, false);
22058 if (range_list_added)
22059 add_ranges (NULL);
22061 else
22063 /* There is no real support in DW2 for this .. so we make
22064 a work-around. First, emit the pub name for the segment
22065 containing the function label. Then make and emit a
22066 simplified subprogram DIE for the second segment with the
22067 name pre-fixed by __hot/cold_sect_of_. We use the same
22068 linkage name for the second die so that gdb will find both
22069 sections when given "b foo". */
22070 const char *name = NULL;
22071 tree decl_name = DECL_NAME (decl);
22072 dw_die_ref seg_die;
22074 /* Do the 'primary' section. */
22075 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22076 fde->dw_fde_end, false);
22078 /* Build a minimal DIE for the secondary section. */
22079 seg_die = new_die (DW_TAG_subprogram,
22080 subr_die->die_parent, decl);
22082 if (TREE_PUBLIC (decl))
22083 add_AT_flag (seg_die, DW_AT_external, 1);
22085 if (decl_name != NULL
22086 && IDENTIFIER_POINTER (decl_name) != NULL)
22088 name = dwarf2_name (decl, 1);
22089 if (! DECL_ARTIFICIAL (decl))
22090 add_src_coords_attributes (seg_die, decl);
22092 add_linkage_name (seg_die, decl);
22094 gcc_assert (name != NULL);
22095 add_pure_or_virtual_attribute (seg_die, decl);
22096 if (DECL_ARTIFICIAL (decl))
22097 add_AT_flag (seg_die, DW_AT_artificial, 1);
22099 name = concat ("__second_sect_of_", name, NULL);
22100 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22101 fde->dw_fde_second_end, false);
22102 add_name_attribute (seg_die, name);
22103 if (want_pubnames ())
22104 add_pubname_string (name, seg_die);
22107 else
22108 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22109 false);
22112 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22114 /* We define the "frame base" as the function's CFA. This is more
22115 convenient for several reasons: (1) It's stable across the prologue
22116 and epilogue, which makes it better than just a frame pointer,
22117 (2) With dwarf3, there exists a one-byte encoding that allows us
22118 to reference the .debug_frame data by proxy, but failing that,
22119 (3) We can at least reuse the code inspection and interpretation
22120 code that determines the CFA position at various points in the
22121 function. */
22122 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22124 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22125 add_AT_loc (subr_die, DW_AT_frame_base, op);
22127 else
22129 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22130 if (list->dw_loc_next)
22131 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22132 else
22133 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22136 /* Compute a displacement from the "steady-state frame pointer" to
22137 the CFA. The former is what all stack slots and argument slots
22138 will reference in the rtl; the latter is what we've told the
22139 debugger about. We'll need to adjust all frame_base references
22140 by this displacement. */
22141 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22143 if (fun->static_chain_decl)
22145 /* DWARF requires here a location expression that computes the
22146 address of the enclosing subprogram's frame base. The machinery
22147 in tree-nested.c is supposed to store this specific address in the
22148 last field of the FRAME record. */
22149 const tree frame_type
22150 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22151 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22153 tree fb_expr
22154 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22155 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22156 fb_expr, fb_decl, NULL_TREE);
22158 add_AT_location_description (subr_die, DW_AT_static_link,
22159 loc_list_from_tree (fb_expr, 0, NULL));
22163 /* Generate child dies for template paramaters. */
22164 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22165 gen_generic_params_dies (decl);
22167 /* Now output descriptions of the arguments for this function. This gets
22168 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22169 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22170 `...' at the end of the formal parameter list. In order to find out if
22171 there was a trailing ellipsis or not, we must instead look at the type
22172 associated with the FUNCTION_DECL. This will be a node of type
22173 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22174 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22175 an ellipsis at the end. */
22177 /* In the case where we are describing a mere function declaration, all we
22178 need to do here (and all we *can* do here) is to describe the *types* of
22179 its formal parameters. */
22180 if (debug_info_level <= DINFO_LEVEL_TERSE)
22182 else if (declaration)
22183 gen_formal_types_die (decl, subr_die);
22184 else
22186 /* Generate DIEs to represent all known formal parameters. */
22187 tree parm = DECL_ARGUMENTS (decl);
22188 tree generic_decl = early_dwarf
22189 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22190 tree generic_decl_parm = generic_decl
22191 ? DECL_ARGUMENTS (generic_decl)
22192 : NULL;
22193 auto_vec<dw_die_ref> string_types_vec;
22194 if (string_types == NULL)
22195 string_types = &string_types_vec;
22197 /* Now we want to walk the list of parameters of the function and
22198 emit their relevant DIEs.
22200 We consider the case of DECL being an instance of a generic function
22201 as well as it being a normal function.
22203 If DECL is an instance of a generic function we walk the
22204 parameters of the generic function declaration _and_ the parameters of
22205 DECL itself. This is useful because we want to emit specific DIEs for
22206 function parameter packs and those are declared as part of the
22207 generic function declaration. In that particular case,
22208 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22209 That DIE has children DIEs representing the set of arguments
22210 of the pack. Note that the set of pack arguments can be empty.
22211 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22212 children DIE.
22214 Otherwise, we just consider the parameters of DECL. */
22215 while (generic_decl_parm || parm)
22217 if (generic_decl_parm
22218 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22219 gen_formal_parameter_pack_die (generic_decl_parm,
22220 parm, subr_die,
22221 &parm);
22222 else if (parm && !POINTER_BOUNDS_P (parm))
22224 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22226 if (parm == DECL_ARGUMENTS (decl)
22227 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22228 && parm_die
22229 && (dwarf_version >= 3 || !dwarf_strict))
22230 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22232 parm = DECL_CHAIN (parm);
22234 else if (parm)
22235 parm = DECL_CHAIN (parm);
22237 if (generic_decl_parm)
22238 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22241 /* Decide whether we need an unspecified_parameters DIE at the end.
22242 There are 2 more cases to do this for: 1) the ansi ... declaration -
22243 this is detectable when the end of the arg list is not a
22244 void_type_node 2) an unprototyped function declaration (not a
22245 definition). This just means that we have no info about the
22246 parameters at all. */
22247 if (early_dwarf)
22249 if (prototype_p (TREE_TYPE (decl)))
22251 /* This is the prototyped case, check for.... */
22252 if (stdarg_p (TREE_TYPE (decl)))
22253 gen_unspecified_parameters_die (decl, subr_die);
22255 else if (DECL_INITIAL (decl) == NULL_TREE)
22256 gen_unspecified_parameters_die (decl, subr_die);
22259 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
22260 have DIEs. */
22261 if (string_types == &string_types_vec)
22263 adjust_string_types ();
22264 string_types = NULL;
22268 if (subr_die != old_die)
22269 /* Add the calling convention attribute if requested. */
22270 add_calling_convention_attribute (subr_die, decl);
22272 /* Output Dwarf info for all of the stuff within the body of the function
22273 (if it has one - it may be just a declaration).
22275 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22276 a function. This BLOCK actually represents the outermost binding contour
22277 for the function, i.e. the contour in which the function's formal
22278 parameters and labels get declared. Curiously, it appears that the front
22279 end doesn't actually put the PARM_DECL nodes for the current function onto
22280 the BLOCK_VARS list for this outer scope, but are strung off of the
22281 DECL_ARGUMENTS list for the function instead.
22283 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22284 the LABEL_DECL nodes for the function however, and we output DWARF info
22285 for those in decls_for_scope. Just within the `outer_scope' there will be
22286 a BLOCK node representing the function's outermost pair of curly braces,
22287 and any blocks used for the base and member initializers of a C++
22288 constructor function. */
22289 tree outer_scope = DECL_INITIAL (decl);
22290 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22292 int call_site_note_count = 0;
22293 int tail_call_site_note_count = 0;
22295 /* Emit a DW_TAG_variable DIE for a named return value. */
22296 if (DECL_NAME (DECL_RESULT (decl)))
22297 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22299 /* The first time through decls_for_scope we will generate the
22300 DIEs for the locals. The second time, we fill in the
22301 location info. */
22302 decls_for_scope (outer_scope, subr_die);
22304 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22306 struct call_arg_loc_node *ca_loc;
22307 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22309 dw_die_ref die = NULL;
22310 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22311 rtx arg, next_arg;
22313 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22314 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22315 : NULL_RTX);
22316 arg; arg = next_arg)
22318 dw_loc_descr_ref reg, val;
22319 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22320 dw_die_ref cdie, tdie = NULL;
22322 next_arg = XEXP (arg, 1);
22323 if (REG_P (XEXP (XEXP (arg, 0), 0))
22324 && next_arg
22325 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22326 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22327 && REGNO (XEXP (XEXP (arg, 0), 0))
22328 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22329 next_arg = XEXP (next_arg, 1);
22330 if (mode == VOIDmode)
22332 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22333 if (mode == VOIDmode)
22334 mode = GET_MODE (XEXP (arg, 0));
22336 if (mode == VOIDmode || mode == BLKmode)
22337 continue;
22338 /* Get dynamic information about call target only if we
22339 have no static information: we cannot generate both
22340 DW_AT_call_origin and DW_AT_call_target
22341 attributes. */
22342 if (ca_loc->symbol_ref == NULL_RTX)
22344 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22346 tloc = XEXP (XEXP (arg, 0), 1);
22347 continue;
22349 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22350 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22352 tlocc = XEXP (XEXP (arg, 0), 1);
22353 continue;
22356 reg = NULL;
22357 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22358 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22359 VAR_INIT_STATUS_INITIALIZED);
22360 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22362 rtx mem = XEXP (XEXP (arg, 0), 0);
22363 reg = mem_loc_descriptor (XEXP (mem, 0),
22364 get_address_mode (mem),
22365 GET_MODE (mem),
22366 VAR_INIT_STATUS_INITIALIZED);
22368 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22369 == DEBUG_PARAMETER_REF)
22371 tree tdecl
22372 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22373 tdie = lookup_decl_die (tdecl);
22374 if (tdie == NULL)
22375 continue;
22377 else
22378 continue;
22379 if (reg == NULL
22380 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22381 != DEBUG_PARAMETER_REF)
22382 continue;
22383 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22384 VOIDmode,
22385 VAR_INIT_STATUS_INITIALIZED);
22386 if (val == NULL)
22387 continue;
22388 if (die == NULL)
22389 die = gen_call_site_die (decl, subr_die, ca_loc);
22390 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22391 NULL_TREE);
22392 if (reg != NULL)
22393 add_AT_loc (cdie, DW_AT_location, reg);
22394 else if (tdie != NULL)
22395 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22396 tdie);
22397 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22398 if (next_arg != XEXP (arg, 1))
22400 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22401 if (mode == VOIDmode)
22402 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22403 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22404 0), 1),
22405 mode, VOIDmode,
22406 VAR_INIT_STATUS_INITIALIZED);
22407 if (val != NULL)
22408 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22409 val);
22412 if (die == NULL
22413 && (ca_loc->symbol_ref || tloc))
22414 die = gen_call_site_die (decl, subr_die, ca_loc);
22415 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22417 dw_loc_descr_ref tval = NULL;
22419 if (tloc != NULL_RTX)
22420 tval = mem_loc_descriptor (tloc,
22421 GET_MODE (tloc) == VOIDmode
22422 ? Pmode : GET_MODE (tloc),
22423 VOIDmode,
22424 VAR_INIT_STATUS_INITIALIZED);
22425 if (tval)
22426 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22427 else if (tlocc != NULL_RTX)
22429 tval = mem_loc_descriptor (tlocc,
22430 GET_MODE (tlocc) == VOIDmode
22431 ? Pmode : GET_MODE (tlocc),
22432 VOIDmode,
22433 VAR_INIT_STATUS_INITIALIZED);
22434 if (tval)
22435 add_AT_loc (die,
22436 dwarf_AT (DW_AT_call_target_clobbered),
22437 tval);
22440 if (die != NULL)
22442 call_site_note_count++;
22443 if (ca_loc->tail_call_p)
22444 tail_call_site_note_count++;
22448 call_arg_locations = NULL;
22449 call_arg_loc_last = NULL;
22450 if (tail_call_site_count >= 0
22451 && tail_call_site_count == tail_call_site_note_count
22452 && (!dwarf_strict || dwarf_version >= 5))
22454 if (call_site_count >= 0
22455 && call_site_count == call_site_note_count)
22456 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22457 else
22458 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22460 call_site_count = -1;
22461 tail_call_site_count = -1;
22464 /* Mark used types after we have created DIEs for the functions scopes. */
22465 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22468 /* Returns a hash value for X (which really is a die_struct). */
22470 hashval_t
22471 block_die_hasher::hash (die_struct *d)
22473 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22476 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22477 as decl_id and die_parent of die_struct Y. */
22479 bool
22480 block_die_hasher::equal (die_struct *x, die_struct *y)
22482 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22485 /* Return TRUE if DECL, which may have been previously generated as
22486 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22487 true if decl (or its origin) is either an extern declaration or a
22488 class/namespace scoped declaration.
22490 The declare_in_namespace support causes us to get two DIEs for one
22491 variable, both of which are declarations. We want to avoid
22492 considering one to be a specification, so we must test for
22493 DECLARATION and DW_AT_declaration. */
22494 static inline bool
22495 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22497 return (old_die && TREE_STATIC (decl) && !declaration
22498 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22501 /* Return true if DECL is a local static. */
22503 static inline bool
22504 local_function_static (tree decl)
22506 gcc_assert (VAR_P (decl));
22507 return TREE_STATIC (decl)
22508 && DECL_CONTEXT (decl)
22509 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22512 /* Generate a DIE to represent a declared data object.
22513 Either DECL or ORIGIN must be non-null. */
22515 static void
22516 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22518 HOST_WIDE_INT off = 0;
22519 tree com_decl;
22520 tree decl_or_origin = decl ? decl : origin;
22521 tree ultimate_origin;
22522 dw_die_ref var_die;
22523 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22524 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22525 || class_or_namespace_scope_p (context_die));
22526 bool specialization_p = false;
22527 bool no_linkage_name = false;
22529 /* While C++ inline static data members have definitions inside of the
22530 class, force the first DIE to be a declaration, then let gen_member_die
22531 reparent it to the class context and call gen_variable_die again
22532 to create the outside of the class DIE for the definition. */
22533 if (!declaration
22534 && old_die == NULL
22535 && decl
22536 && DECL_CONTEXT (decl)
22537 && TYPE_P (DECL_CONTEXT (decl))
22538 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22540 declaration = true;
22541 no_linkage_name = true;
22544 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22545 if (decl || ultimate_origin)
22546 origin = ultimate_origin;
22547 com_decl = fortran_common (decl_or_origin, &off);
22549 /* Symbol in common gets emitted as a child of the common block, in the form
22550 of a data member. */
22551 if (com_decl)
22553 dw_die_ref com_die;
22554 dw_loc_list_ref loc = NULL;
22555 die_node com_die_arg;
22557 var_die = lookup_decl_die (decl_or_origin);
22558 if (var_die)
22560 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22562 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22563 if (loc)
22565 if (off)
22567 /* Optimize the common case. */
22568 if (single_element_loc_list_p (loc)
22569 && loc->expr->dw_loc_opc == DW_OP_addr
22570 && loc->expr->dw_loc_next == NULL
22571 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22572 == SYMBOL_REF)
22574 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22575 loc->expr->dw_loc_oprnd1.v.val_addr
22576 = plus_constant (GET_MODE (x), x , off);
22578 else
22579 loc_list_plus_const (loc, off);
22581 add_AT_location_description (var_die, DW_AT_location, loc);
22582 remove_AT (var_die, DW_AT_declaration);
22585 return;
22588 if (common_block_die_table == NULL)
22589 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22591 com_die_arg.decl_id = DECL_UID (com_decl);
22592 com_die_arg.die_parent = context_die;
22593 com_die = common_block_die_table->find (&com_die_arg);
22594 if (! early_dwarf)
22595 loc = loc_list_from_tree (com_decl, 2, NULL);
22596 if (com_die == NULL)
22598 const char *cnam
22599 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22600 die_node **slot;
22602 com_die = new_die (DW_TAG_common_block, context_die, decl);
22603 add_name_and_src_coords_attributes (com_die, com_decl);
22604 if (loc)
22606 add_AT_location_description (com_die, DW_AT_location, loc);
22607 /* Avoid sharing the same loc descriptor between
22608 DW_TAG_common_block and DW_TAG_variable. */
22609 loc = loc_list_from_tree (com_decl, 2, NULL);
22611 else if (DECL_EXTERNAL (decl_or_origin))
22612 add_AT_flag (com_die, DW_AT_declaration, 1);
22613 if (want_pubnames ())
22614 add_pubname_string (cnam, com_die); /* ??? needed? */
22615 com_die->decl_id = DECL_UID (com_decl);
22616 slot = common_block_die_table->find_slot (com_die, INSERT);
22617 *slot = com_die;
22619 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22621 add_AT_location_description (com_die, DW_AT_location, loc);
22622 loc = loc_list_from_tree (com_decl, 2, NULL);
22623 remove_AT (com_die, DW_AT_declaration);
22625 var_die = new_die (DW_TAG_variable, com_die, decl);
22626 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22627 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22628 decl_quals (decl_or_origin), false,
22629 context_die);
22630 add_AT_flag (var_die, DW_AT_external, 1);
22631 if (loc)
22633 if (off)
22635 /* Optimize the common case. */
22636 if (single_element_loc_list_p (loc)
22637 && loc->expr->dw_loc_opc == DW_OP_addr
22638 && loc->expr->dw_loc_next == NULL
22639 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22641 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22642 loc->expr->dw_loc_oprnd1.v.val_addr
22643 = plus_constant (GET_MODE (x), x, off);
22645 else
22646 loc_list_plus_const (loc, off);
22648 add_AT_location_description (var_die, DW_AT_location, loc);
22650 else if (DECL_EXTERNAL (decl_or_origin))
22651 add_AT_flag (var_die, DW_AT_declaration, 1);
22652 if (decl)
22653 equate_decl_number_to_die (decl, var_die);
22654 return;
22657 if (old_die)
22659 if (declaration)
22661 /* A declaration that has been previously dumped, needs no
22662 further annotations, since it doesn't need location on
22663 the second pass. */
22664 return;
22666 else if (decl_will_get_specification_p (old_die, decl, declaration)
22667 && !get_AT (old_die, DW_AT_specification))
22669 /* Fall-thru so we can make a new variable die along with a
22670 DW_AT_specification. */
22672 else if (origin && old_die->die_parent != context_die)
22674 /* If we will be creating an inlined instance, we need a
22675 new DIE that will get annotated with
22676 DW_AT_abstract_origin. Clear things so we can get a
22677 new DIE. */
22678 gcc_assert (!DECL_ABSTRACT_P (decl));
22679 old_die = NULL;
22681 else
22683 /* If a DIE was dumped early, it still needs location info.
22684 Skip to where we fill the location bits. */
22685 var_die = old_die;
22686 goto gen_variable_die_location;
22690 /* For static data members, the declaration in the class is supposed
22691 to have DW_TAG_member tag; the specification should still be
22692 DW_TAG_variable referencing the DW_TAG_member DIE. */
22693 if (declaration && class_scope_p (context_die))
22694 var_die = new_die (DW_TAG_member, context_die, decl);
22695 else
22696 var_die = new_die (DW_TAG_variable, context_die, decl);
22698 if (origin != NULL)
22699 add_abstract_origin_attribute (var_die, origin);
22701 /* Loop unrolling can create multiple blocks that refer to the same
22702 static variable, so we must test for the DW_AT_declaration flag.
22704 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22705 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22706 sharing them.
22708 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22709 else if (decl_will_get_specification_p (old_die, decl, declaration))
22711 /* This is a definition of a C++ class level static. */
22712 add_AT_specification (var_die, old_die);
22713 specialization_p = true;
22714 if (DECL_NAME (decl))
22716 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22717 struct dwarf_file_data * file_index = lookup_filename (s.file);
22719 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22720 add_AT_file (var_die, DW_AT_decl_file, file_index);
22722 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22723 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22725 if (old_die->die_tag == DW_TAG_member)
22726 add_linkage_name (var_die, decl);
22729 else
22730 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22732 if ((origin == NULL && !specialization_p)
22733 || (origin != NULL
22734 && !DECL_ABSTRACT_P (decl_or_origin)
22735 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22736 decl_function_context
22737 (decl_or_origin))))
22739 tree type = TREE_TYPE (decl_or_origin);
22741 if (decl_by_reference_p (decl_or_origin))
22742 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22743 context_die);
22744 else
22745 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22746 context_die);
22749 if (origin == NULL && !specialization_p)
22751 if (TREE_PUBLIC (decl))
22752 add_AT_flag (var_die, DW_AT_external, 1);
22754 if (DECL_ARTIFICIAL (decl))
22755 add_AT_flag (var_die, DW_AT_artificial, 1);
22757 add_accessibility_attribute (var_die, decl);
22760 if (declaration)
22761 add_AT_flag (var_die, DW_AT_declaration, 1);
22763 if (decl && (DECL_ABSTRACT_P (decl)
22764 || !old_die || is_declaration_die (old_die)))
22765 equate_decl_number_to_die (decl, var_die);
22767 gen_variable_die_location:
22768 if (! declaration
22769 && (! DECL_ABSTRACT_P (decl_or_origin)
22770 /* Local static vars are shared between all clones/inlines,
22771 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22772 already set. */
22773 || (VAR_P (decl_or_origin)
22774 && TREE_STATIC (decl_or_origin)
22775 && DECL_RTL_SET_P (decl_or_origin))))
22777 if (early_dwarf)
22778 add_pubname (decl_or_origin, var_die);
22779 else
22780 add_location_or_const_value_attribute (var_die, decl_or_origin,
22781 decl == NULL);
22783 else
22784 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22786 if ((dwarf_version >= 4 || !dwarf_strict)
22787 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22788 DW_AT_const_expr) == 1
22789 && !get_AT (var_die, DW_AT_const_expr)
22790 && !specialization_p)
22791 add_AT_flag (var_die, DW_AT_const_expr, 1);
22793 if (!dwarf_strict)
22795 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22796 DW_AT_inline);
22797 if (inl != -1
22798 && !get_AT (var_die, DW_AT_inline)
22799 && !specialization_p)
22800 add_AT_unsigned (var_die, DW_AT_inline, inl);
22804 /* Generate a DIE to represent a named constant. */
22806 static void
22807 gen_const_die (tree decl, dw_die_ref context_die)
22809 dw_die_ref const_die;
22810 tree type = TREE_TYPE (decl);
22812 const_die = lookup_decl_die (decl);
22813 if (const_die)
22814 return;
22816 const_die = new_die (DW_TAG_constant, context_die, decl);
22817 equate_decl_number_to_die (decl, const_die);
22818 add_name_and_src_coords_attributes (const_die, decl);
22819 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22820 if (TREE_PUBLIC (decl))
22821 add_AT_flag (const_die, DW_AT_external, 1);
22822 if (DECL_ARTIFICIAL (decl))
22823 add_AT_flag (const_die, DW_AT_artificial, 1);
22824 tree_add_const_value_attribute_for_decl (const_die, decl);
22827 /* Generate a DIE to represent a label identifier. */
22829 static void
22830 gen_label_die (tree decl, dw_die_ref context_die)
22832 tree origin = decl_ultimate_origin (decl);
22833 dw_die_ref lbl_die = lookup_decl_die (decl);
22834 rtx insn;
22835 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22837 if (!lbl_die)
22839 lbl_die = new_die (DW_TAG_label, context_die, decl);
22840 equate_decl_number_to_die (decl, lbl_die);
22842 if (origin != NULL)
22843 add_abstract_origin_attribute (lbl_die, origin);
22844 else
22845 add_name_and_src_coords_attributes (lbl_die, decl);
22848 if (DECL_ABSTRACT_P (decl))
22849 equate_decl_number_to_die (decl, lbl_die);
22850 else if (! early_dwarf)
22852 insn = DECL_RTL_IF_SET (decl);
22854 /* Deleted labels are programmer specified labels which have been
22855 eliminated because of various optimizations. We still emit them
22856 here so that it is possible to put breakpoints on them. */
22857 if (insn
22858 && (LABEL_P (insn)
22859 || ((NOTE_P (insn)
22860 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
22862 /* When optimization is enabled (via -O) some parts of the compiler
22863 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
22864 represent source-level labels which were explicitly declared by
22865 the user. This really shouldn't be happening though, so catch
22866 it if it ever does happen. */
22867 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
22869 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
22870 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22872 else if (insn
22873 && NOTE_P (insn)
22874 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
22875 && CODE_LABEL_NUMBER (insn) != -1)
22877 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
22878 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22883 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
22884 attributes to the DIE for a block STMT, to describe where the inlined
22885 function was called from. This is similar to add_src_coords_attributes. */
22887 static inline void
22888 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
22890 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
22892 if (dwarf_version >= 3 || !dwarf_strict)
22894 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
22895 add_AT_unsigned (die, DW_AT_call_line, s.line);
22900 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
22901 Add low_pc and high_pc attributes to the DIE for a block STMT. */
22903 static inline void
22904 add_high_low_attributes (tree stmt, dw_die_ref die)
22906 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22908 if (BLOCK_FRAGMENT_CHAIN (stmt)
22909 && (dwarf_version >= 3 || !dwarf_strict))
22911 tree chain, superblock = NULL_TREE;
22912 dw_die_ref pdie;
22913 dw_attr_node *attr = NULL;
22915 if (inlined_function_outer_scope_p (stmt))
22917 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22918 BLOCK_NUMBER (stmt));
22919 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22922 /* Optimize duplicate .debug_ranges lists or even tails of
22923 lists. If this BLOCK has same ranges as its supercontext,
22924 lookup DW_AT_ranges attribute in the supercontext (and
22925 recursively so), verify that the ranges_table contains the
22926 right values and use it instead of adding a new .debug_range. */
22927 for (chain = stmt, pdie = die;
22928 BLOCK_SAME_RANGE (chain);
22929 chain = BLOCK_SUPERCONTEXT (chain))
22931 dw_attr_node *new_attr;
22933 pdie = pdie->die_parent;
22934 if (pdie == NULL)
22935 break;
22936 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
22937 break;
22938 new_attr = get_AT (pdie, DW_AT_ranges);
22939 if (new_attr == NULL
22940 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
22941 break;
22942 attr = new_attr;
22943 superblock = BLOCK_SUPERCONTEXT (chain);
22945 if (attr != NULL
22946 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
22947 == BLOCK_NUMBER (superblock))
22948 && BLOCK_FRAGMENT_CHAIN (superblock))
22950 unsigned long off = attr->dw_attr_val.v.val_offset;
22951 unsigned long supercnt = 0, thiscnt = 0;
22952 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
22953 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
22955 ++supercnt;
22956 gcc_checking_assert ((*ranges_table)[off + supercnt].num
22957 == BLOCK_NUMBER (chain));
22959 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
22960 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
22961 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
22962 ++thiscnt;
22963 gcc_assert (supercnt >= thiscnt);
22964 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
22965 false);
22966 note_rnglist_head (off + supercnt - thiscnt);
22967 return;
22970 unsigned int offset = add_ranges (stmt, true);
22971 add_AT_range_list (die, DW_AT_ranges, offset, false);
22972 note_rnglist_head (offset);
22974 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
22975 chain = BLOCK_FRAGMENT_CHAIN (stmt);
22978 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
22979 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
22980 chain = BLOCK_FRAGMENT_CHAIN (chain);
22982 while (chain);
22983 add_ranges (NULL);
22985 else
22987 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
22988 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22989 BLOCK_NUMBER (stmt));
22990 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
22991 BLOCK_NUMBER (stmt));
22992 add_AT_low_high_pc (die, label, label_high, false);
22996 /* Generate a DIE for a lexical block. */
22998 static void
22999 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23001 dw_die_ref old_die = BLOCK_DIE (stmt);
23002 dw_die_ref stmt_die = NULL;
23003 if (!old_die)
23005 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23006 BLOCK_DIE (stmt) = stmt_die;
23009 if (BLOCK_ABSTRACT (stmt))
23011 if (old_die)
23013 /* This must have been generated early and it won't even
23014 need location information since it's a DW_AT_inline
23015 function. */
23016 if (flag_checking)
23017 for (dw_die_ref c = context_die; c; c = c->die_parent)
23018 if (c->die_tag == DW_TAG_inlined_subroutine
23019 || c->die_tag == DW_TAG_subprogram)
23021 gcc_assert (get_AT (c, DW_AT_inline));
23022 break;
23024 return;
23027 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23029 /* If this is an inlined instance, create a new lexical die for
23030 anything below to attach DW_AT_abstract_origin to. */
23031 if (old_die)
23033 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23034 BLOCK_DIE (stmt) = stmt_die;
23035 old_die = NULL;
23038 tree origin = block_ultimate_origin (stmt);
23039 if (origin != NULL_TREE && origin != stmt)
23040 add_abstract_origin_attribute (stmt_die, origin);
23043 if (old_die)
23044 stmt_die = old_die;
23046 /* A non abstract block whose blocks have already been reordered
23047 should have the instruction range for this block. If so, set the
23048 high/low attributes. */
23049 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23051 gcc_assert (stmt_die);
23052 add_high_low_attributes (stmt, stmt_die);
23055 decls_for_scope (stmt, stmt_die);
23058 /* Generate a DIE for an inlined subprogram. */
23060 static void
23061 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23063 tree decl;
23065 /* The instance of function that is effectively being inlined shall not
23066 be abstract. */
23067 gcc_assert (! BLOCK_ABSTRACT (stmt));
23069 decl = block_ultimate_origin (stmt);
23071 /* Make sure any inlined functions are known to be inlineable. */
23072 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23073 || cgraph_function_possibly_inlined_p (decl));
23075 /* Emit info for the abstract instance first, if we haven't yet. We
23076 must emit this even if the block is abstract, otherwise when we
23077 emit the block below (or elsewhere), we may end up trying to emit
23078 a die whose origin die hasn't been emitted, and crashing. */
23079 dwarf2out_abstract_function (decl);
23081 if (! BLOCK_ABSTRACT (stmt))
23083 dw_die_ref subr_die
23084 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23086 if (call_arg_locations)
23087 BLOCK_DIE (stmt) = subr_die;
23088 add_abstract_origin_attribute (subr_die, decl);
23089 if (TREE_ASM_WRITTEN (stmt))
23090 add_high_low_attributes (stmt, subr_die);
23091 add_call_src_coords_attributes (stmt, subr_die);
23093 decls_for_scope (stmt, subr_die);
23097 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23098 the comment for VLR_CONTEXT. */
23100 static void
23101 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23103 dw_die_ref decl_die;
23105 if (TREE_TYPE (decl) == error_mark_node)
23106 return;
23108 decl_die = new_die (DW_TAG_member, context_die, decl);
23109 add_name_and_src_coords_attributes (decl_die, decl);
23110 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23111 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23112 context_die);
23114 if (DECL_BIT_FIELD_TYPE (decl))
23116 add_byte_size_attribute (decl_die, decl);
23117 add_bit_size_attribute (decl_die, decl);
23118 add_bit_offset_attribute (decl_die, decl, ctx);
23121 /* If we have a variant part offset, then we are supposed to process a member
23122 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23123 trees. */
23124 gcc_assert (ctx->variant_part_offset == NULL_TREE
23125 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23126 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23127 add_data_member_location_attribute (decl_die, decl, ctx);
23129 if (DECL_ARTIFICIAL (decl))
23130 add_AT_flag (decl_die, DW_AT_artificial, 1);
23132 add_accessibility_attribute (decl_die, decl);
23134 /* Equate decl number to die, so that we can look up this decl later on. */
23135 equate_decl_number_to_die (decl, decl_die);
23138 #if 0
23139 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23140 Use modified_type_die instead.
23141 We keep this code here just in case these types of DIEs may be needed to
23142 represent certain things in other languages (e.g. Pascal) someday. */
23144 static void
23145 gen_pointer_type_die (tree type, dw_die_ref context_die)
23147 dw_die_ref ptr_die
23148 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23150 equate_type_number_to_die (type, ptr_die);
23151 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23152 context_die);
23153 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23156 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23157 Use modified_type_die instead.
23158 We keep this code here just in case these types of DIEs may be needed to
23159 represent certain things in other languages (e.g. Pascal) someday. */
23161 static void
23162 gen_reference_type_die (tree type, dw_die_ref context_die)
23164 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23166 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23167 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23168 else
23169 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23171 equate_type_number_to_die (type, ref_die);
23172 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23173 context_die);
23174 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23176 #endif
23178 /* Generate a DIE for a pointer to a member type. TYPE can be an
23179 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23180 pointer to member function. */
23182 static void
23183 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23185 if (lookup_type_die (type))
23186 return;
23188 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23189 scope_die_for (type, context_die), type);
23191 equate_type_number_to_die (type, ptr_die);
23192 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23193 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23194 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23195 context_die);
23197 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23198 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23200 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23201 add_AT_loc (ptr_die, DW_AT_use_location, op);
23205 static char *producer_string;
23207 /* Return a heap allocated producer string including command line options
23208 if -grecord-gcc-switches. */
23210 static char *
23211 gen_producer_string (void)
23213 size_t j;
23214 auto_vec<const char *> switches;
23215 const char *language_string = lang_hooks.name;
23216 char *producer, *tail;
23217 const char *p;
23218 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23219 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23221 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23222 switch (save_decoded_options[j].opt_index)
23224 case OPT_o:
23225 case OPT_d:
23226 case OPT_dumpbase:
23227 case OPT_dumpdir:
23228 case OPT_auxbase:
23229 case OPT_auxbase_strip:
23230 case OPT_quiet:
23231 case OPT_version:
23232 case OPT_v:
23233 case OPT_w:
23234 case OPT_L:
23235 case OPT_D:
23236 case OPT_I:
23237 case OPT_U:
23238 case OPT_SPECIAL_unknown:
23239 case OPT_SPECIAL_ignore:
23240 case OPT_SPECIAL_program_name:
23241 case OPT_SPECIAL_input_file:
23242 case OPT_grecord_gcc_switches:
23243 case OPT_gno_record_gcc_switches:
23244 case OPT__output_pch_:
23245 case OPT_fdiagnostics_show_location_:
23246 case OPT_fdiagnostics_show_option:
23247 case OPT_fdiagnostics_show_caret:
23248 case OPT_fdiagnostics_color_:
23249 case OPT_fverbose_asm:
23250 case OPT____:
23251 case OPT__sysroot_:
23252 case OPT_nostdinc:
23253 case OPT_nostdinc__:
23254 case OPT_fpreprocessed:
23255 case OPT_fltrans_output_list_:
23256 case OPT_fresolution_:
23257 case OPT_fdebug_prefix_map_:
23258 /* Ignore these. */
23259 continue;
23260 default:
23261 if (cl_options[save_decoded_options[j].opt_index].flags
23262 & CL_NO_DWARF_RECORD)
23263 continue;
23264 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23265 == '-');
23266 switch (save_decoded_options[j].canonical_option[0][1])
23268 case 'M':
23269 case 'i':
23270 case 'W':
23271 continue;
23272 case 'f':
23273 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23274 "dump", 4) == 0)
23275 continue;
23276 break;
23277 default:
23278 break;
23280 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23281 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23282 break;
23285 producer = XNEWVEC (char, plen + 1 + len + 1);
23286 tail = producer;
23287 sprintf (tail, "%s %s", language_string, version_string);
23288 tail += plen;
23290 FOR_EACH_VEC_ELT (switches, j, p)
23292 len = strlen (p);
23293 *tail = ' ';
23294 memcpy (tail + 1, p, len);
23295 tail += len + 1;
23298 *tail = '\0';
23299 return producer;
23302 /* Given a C and/or C++ language/version string return the "highest".
23303 C++ is assumed to be "higher" than C in this case. Used for merging
23304 LTO translation unit languages. */
23305 static const char *
23306 highest_c_language (const char *lang1, const char *lang2)
23308 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23309 return "GNU C++14";
23310 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23311 return "GNU C++11";
23312 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23313 return "GNU C++98";
23315 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23316 return "GNU C11";
23317 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23318 return "GNU C99";
23319 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23320 return "GNU C89";
23322 gcc_unreachable ();
23326 /* Generate the DIE for the compilation unit. */
23328 static dw_die_ref
23329 gen_compile_unit_die (const char *filename)
23331 dw_die_ref die;
23332 const char *language_string = lang_hooks.name;
23333 int language;
23335 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23337 if (filename)
23339 add_name_attribute (die, filename);
23340 /* Don't add cwd for <built-in>. */
23341 if (filename[0] != '<')
23342 add_comp_dir_attribute (die);
23345 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23347 /* If our producer is LTO try to figure out a common language to use
23348 from the global list of translation units. */
23349 if (strcmp (language_string, "GNU GIMPLE") == 0)
23351 unsigned i;
23352 tree t;
23353 const char *common_lang = NULL;
23355 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23357 if (!TRANSLATION_UNIT_LANGUAGE (t))
23358 continue;
23359 if (!common_lang)
23360 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23361 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23363 else if (strncmp (common_lang, "GNU C", 5) == 0
23364 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23365 /* Mixing C and C++ is ok, use C++ in that case. */
23366 common_lang = highest_c_language (common_lang,
23367 TRANSLATION_UNIT_LANGUAGE (t));
23368 else
23370 /* Fall back to C. */
23371 common_lang = NULL;
23372 break;
23376 if (common_lang)
23377 language_string = common_lang;
23380 language = DW_LANG_C;
23381 if (strncmp (language_string, "GNU C", 5) == 0
23382 && ISDIGIT (language_string[5]))
23384 language = DW_LANG_C89;
23385 if (dwarf_version >= 3 || !dwarf_strict)
23387 if (strcmp (language_string, "GNU C89") != 0)
23388 language = DW_LANG_C99;
23390 if (dwarf_version >= 5 /* || !dwarf_strict */)
23391 if (strcmp (language_string, "GNU C11") == 0)
23392 language = DW_LANG_C11;
23395 else if (strncmp (language_string, "GNU C++", 7) == 0)
23397 language = DW_LANG_C_plus_plus;
23398 if (dwarf_version >= 5 /* || !dwarf_strict */)
23400 if (strcmp (language_string, "GNU C++11") == 0)
23401 language = DW_LANG_C_plus_plus_11;
23402 else if (strcmp (language_string, "GNU C++14") == 0)
23403 language = DW_LANG_C_plus_plus_14;
23406 else if (strcmp (language_string, "GNU F77") == 0)
23407 language = DW_LANG_Fortran77;
23408 else if (strcmp (language_string, "GNU Pascal") == 0)
23409 language = DW_LANG_Pascal83;
23410 else if (dwarf_version >= 3 || !dwarf_strict)
23412 if (strcmp (language_string, "GNU Ada") == 0)
23413 language = DW_LANG_Ada95;
23414 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23416 language = DW_LANG_Fortran95;
23417 if (dwarf_version >= 5 /* || !dwarf_strict */)
23419 if (strcmp (language_string, "GNU Fortran2003") == 0)
23420 language = DW_LANG_Fortran03;
23421 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23422 language = DW_LANG_Fortran08;
23425 else if (strcmp (language_string, "GNU Java") == 0)
23426 language = DW_LANG_Java;
23427 else if (strcmp (language_string, "GNU Objective-C") == 0)
23428 language = DW_LANG_ObjC;
23429 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23430 language = DW_LANG_ObjC_plus_plus;
23431 else if (dwarf_version >= 5 || !dwarf_strict)
23433 if (strcmp (language_string, "GNU Go") == 0)
23434 language = DW_LANG_Go;
23437 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23438 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23439 language = DW_LANG_Fortran90;
23441 add_AT_unsigned (die, DW_AT_language, language);
23443 switch (language)
23445 case DW_LANG_Fortran77:
23446 case DW_LANG_Fortran90:
23447 case DW_LANG_Fortran95:
23448 case DW_LANG_Fortran03:
23449 case DW_LANG_Fortran08:
23450 /* Fortran has case insensitive identifiers and the front-end
23451 lowercases everything. */
23452 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23453 break;
23454 default:
23455 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23456 break;
23458 return die;
23461 /* Generate the DIE for a base class. */
23463 static void
23464 gen_inheritance_die (tree binfo, tree access, tree type,
23465 dw_die_ref context_die)
23467 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23468 struct vlr_context ctx = { type, NULL };
23470 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23471 context_die);
23472 add_data_member_location_attribute (die, binfo, &ctx);
23474 if (BINFO_VIRTUAL_P (binfo))
23475 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23477 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23478 children, otherwise the default is DW_ACCESS_public. In DWARF2
23479 the default has always been DW_ACCESS_private. */
23480 if (access == access_public_node)
23482 if (dwarf_version == 2
23483 || context_die->die_tag == DW_TAG_class_type)
23484 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23486 else if (access == access_protected_node)
23487 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23488 else if (dwarf_version > 2
23489 && context_die->die_tag != DW_TAG_class_type)
23490 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23493 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23494 structure. */
23495 static bool
23496 is_variant_part (tree decl)
23498 return (TREE_CODE (decl) == FIELD_DECL
23499 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23502 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23503 return the FIELD_DECL. Return NULL_TREE otherwise. */
23505 static tree
23506 analyze_discr_in_predicate (tree operand, tree struct_type)
23508 bool continue_stripping = true;
23509 while (continue_stripping)
23510 switch (TREE_CODE (operand))
23512 CASE_CONVERT:
23513 operand = TREE_OPERAND (operand, 0);
23514 break;
23515 default:
23516 continue_stripping = false;
23517 break;
23520 /* Match field access to members of struct_type only. */
23521 if (TREE_CODE (operand) == COMPONENT_REF
23522 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23523 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23524 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23525 return TREE_OPERAND (operand, 1);
23526 else
23527 return NULL_TREE;
23530 /* Check that SRC is a constant integer that can be represented as a native
23531 integer constant (either signed or unsigned). If so, store it into DEST and
23532 return true. Return false otherwise. */
23534 static bool
23535 get_discr_value (tree src, dw_discr_value *dest)
23537 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23539 if (TREE_CODE (src) != INTEGER_CST
23540 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23541 return false;
23543 dest->pos = is_unsigned;
23544 if (is_unsigned)
23545 dest->v.uval = tree_to_uhwi (src);
23546 else
23547 dest->v.sval = tree_to_shwi (src);
23549 return true;
23552 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23553 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23554 store NULL_TREE in DISCR_DECL. Otherwise:
23556 - store the discriminant field in STRUCT_TYPE that controls the variant
23557 part to *DISCR_DECL
23559 - put in *DISCR_LISTS_P an array where for each variant, the item
23560 represents the corresponding matching list of discriminant values.
23562 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23563 the above array.
23565 Note that when the array is allocated (i.e. when the analysis is
23566 successful), it is up to the caller to free the array. */
23568 static void
23569 analyze_variants_discr (tree variant_part_decl,
23570 tree struct_type,
23571 tree *discr_decl,
23572 dw_discr_list_ref **discr_lists_p,
23573 unsigned *discr_lists_length)
23575 tree variant_part_type = TREE_TYPE (variant_part_decl);
23576 tree variant;
23577 dw_discr_list_ref *discr_lists;
23578 unsigned i;
23580 /* Compute how many variants there are in this variant part. */
23581 *discr_lists_length = 0;
23582 for (variant = TYPE_FIELDS (variant_part_type);
23583 variant != NULL_TREE;
23584 variant = DECL_CHAIN (variant))
23585 ++*discr_lists_length;
23587 *discr_decl = NULL_TREE;
23588 *discr_lists_p
23589 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23590 sizeof (**discr_lists_p));
23591 discr_lists = *discr_lists_p;
23593 /* And then analyze all variants to extract discriminant information for all
23594 of them. This analysis is conservative: as soon as we detect something we
23595 do not support, abort everything and pretend we found nothing. */
23596 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23597 variant != NULL_TREE;
23598 variant = DECL_CHAIN (variant), ++i)
23600 tree match_expr = DECL_QUALIFIER (variant);
23602 /* Now, try to analyze the predicate and deduce a discriminant for
23603 it. */
23604 if (match_expr == boolean_true_node)
23605 /* Typically happens for the default variant: it matches all cases that
23606 previous variants rejected. Don't output any matching value for
23607 this one. */
23608 continue;
23610 /* The following loop tries to iterate over each discriminant
23611 possibility: single values or ranges. */
23612 while (match_expr != NULL_TREE)
23614 tree next_round_match_expr;
23615 tree candidate_discr = NULL_TREE;
23616 dw_discr_list_ref new_node = NULL;
23618 /* Possibilities are matched one after the other by nested
23619 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23620 continue with the rest at next iteration. */
23621 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23623 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23624 match_expr = TREE_OPERAND (match_expr, 1);
23626 else
23627 next_round_match_expr = NULL_TREE;
23629 if (match_expr == boolean_false_node)
23630 /* This sub-expression matches nothing: just wait for the next
23631 one. */
23634 else if (TREE_CODE (match_expr) == EQ_EXPR)
23636 /* We are matching: <discr_field> == <integer_cst>
23637 This sub-expression matches a single value. */
23638 tree integer_cst = TREE_OPERAND (match_expr, 1);
23640 candidate_discr
23641 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23642 struct_type);
23644 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23645 if (!get_discr_value (integer_cst,
23646 &new_node->dw_discr_lower_bound))
23647 goto abort;
23648 new_node->dw_discr_range = false;
23651 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23653 /* We are matching:
23654 <discr_field> > <integer_cst>
23655 && <discr_field> < <integer_cst>.
23656 This sub-expression matches the range of values between the
23657 two matched integer constants. Note that comparisons can be
23658 inclusive or exclusive. */
23659 tree candidate_discr_1, candidate_discr_2;
23660 tree lower_cst, upper_cst;
23661 bool lower_cst_included, upper_cst_included;
23662 tree lower_op = TREE_OPERAND (match_expr, 0);
23663 tree upper_op = TREE_OPERAND (match_expr, 1);
23665 /* When the comparison is exclusive, the integer constant is not
23666 the discriminant range bound we are looking for: we will have
23667 to increment or decrement it. */
23668 if (TREE_CODE (lower_op) == GE_EXPR)
23669 lower_cst_included = true;
23670 else if (TREE_CODE (lower_op) == GT_EXPR)
23671 lower_cst_included = false;
23672 else
23673 goto abort;
23675 if (TREE_CODE (upper_op) == LE_EXPR)
23676 upper_cst_included = true;
23677 else if (TREE_CODE (upper_op) == LT_EXPR)
23678 upper_cst_included = false;
23679 else
23680 goto abort;
23682 /* Extract the discriminant from the first operand and check it
23683 is consistant with the same analysis in the second
23684 operand. */
23685 candidate_discr_1
23686 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23687 struct_type);
23688 candidate_discr_2
23689 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23690 struct_type);
23691 if (candidate_discr_1 == candidate_discr_2)
23692 candidate_discr = candidate_discr_1;
23693 else
23694 goto abort;
23696 /* Extract bounds from both. */
23697 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23698 lower_cst = TREE_OPERAND (lower_op, 1);
23699 upper_cst = TREE_OPERAND (upper_op, 1);
23701 if (!lower_cst_included)
23702 lower_cst
23703 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
23704 lower_cst,
23705 build_int_cst (TREE_TYPE (lower_cst), 1)));
23706 if (!upper_cst_included)
23707 upper_cst
23708 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
23709 upper_cst,
23710 build_int_cst (TREE_TYPE (upper_cst), 1)));
23712 if (!get_discr_value (lower_cst,
23713 &new_node->dw_discr_lower_bound)
23714 || !get_discr_value (upper_cst,
23715 &new_node->dw_discr_upper_bound))
23716 goto abort;
23718 new_node->dw_discr_range = true;
23721 else
23722 /* Unsupported sub-expression: we cannot determine the set of
23723 matching discriminant values. Abort everything. */
23724 goto abort;
23726 /* If the discriminant info is not consistant with what we saw so
23727 far, consider the analysis failed and abort everything. */
23728 if (candidate_discr == NULL_TREE
23729 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23730 goto abort;
23731 else
23732 *discr_decl = candidate_discr;
23734 if (new_node != NULL)
23736 new_node->dw_discr_next = discr_lists[i];
23737 discr_lists[i] = new_node;
23739 match_expr = next_round_match_expr;
23743 /* If we reach this point, we could match everything we were interested
23744 in. */
23745 return;
23747 abort:
23748 /* Clean all data structure and return no result. */
23749 free (*discr_lists_p);
23750 *discr_lists_p = NULL;
23751 *discr_decl = NULL_TREE;
23754 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23755 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23756 under CONTEXT_DIE.
23758 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23759 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23760 this type, which are record types, represent the available variants and each
23761 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23762 values are inferred from these attributes.
23764 In trees, the offsets for the fields inside these sub-records are relative
23765 to the variant part itself, whereas the corresponding DIEs should have
23766 offset attributes that are relative to the embedding record base address.
23767 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23768 must be an expression that computes the offset of the variant part to
23769 describe in DWARF. */
23771 static void
23772 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23773 dw_die_ref context_die)
23775 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23776 tree variant_part_offset = vlr_ctx->variant_part_offset;
23777 struct loc_descr_context ctx = {
23778 vlr_ctx->struct_type, /* context_type */
23779 NULL_TREE, /* base_decl */
23780 NULL, /* dpi */
23781 false, /* placeholder_arg */
23782 false /* placeholder_seen */
23785 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23786 NULL_TREE if there is no such field. */
23787 tree discr_decl = NULL_TREE;
23788 dw_discr_list_ref *discr_lists;
23789 unsigned discr_lists_length = 0;
23790 unsigned i;
23792 dw_die_ref dwarf_proc_die = NULL;
23793 dw_die_ref variant_part_die
23794 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23796 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23798 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23799 &discr_decl, &discr_lists, &discr_lists_length);
23801 if (discr_decl != NULL_TREE)
23803 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23805 if (discr_die)
23806 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23807 else
23808 /* We have no DIE for the discriminant, so just discard all
23809 discrimimant information in the output. */
23810 discr_decl = NULL_TREE;
23813 /* If the offset for this variant part is more complex than a constant,
23814 create a DWARF procedure for it so that we will not have to generate DWARF
23815 expressions for it for each member. */
23816 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23817 && (dwarf_version >= 3 || !dwarf_strict))
23819 const tree dwarf_proc_fndecl
23820 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23821 build_function_type (TREE_TYPE (variant_part_offset),
23822 NULL_TREE));
23823 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23824 const dw_loc_descr_ref dwarf_proc_body
23825 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23827 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23828 dwarf_proc_fndecl, context_die);
23829 if (dwarf_proc_die != NULL)
23830 variant_part_offset = dwarf_proc_call;
23833 /* Output DIEs for all variants. */
23834 i = 0;
23835 for (tree variant = TYPE_FIELDS (variant_part_type);
23836 variant != NULL_TREE;
23837 variant = DECL_CHAIN (variant), ++i)
23839 tree variant_type = TREE_TYPE (variant);
23840 dw_die_ref variant_die;
23842 /* All variants (i.e. members of a variant part) are supposed to be
23843 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23844 under these records. */
23845 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23847 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23848 equate_decl_number_to_die (variant, variant_die);
23850 /* Output discriminant values this variant matches, if any. */
23851 if (discr_decl == NULL || discr_lists[i] == NULL)
23852 /* In the case we have discriminant information at all, this is
23853 probably the default variant: as the standard says, don't
23854 output any discriminant value/list attribute. */
23856 else if (discr_lists[i]->dw_discr_next == NULL
23857 && !discr_lists[i]->dw_discr_range)
23858 /* If there is only one accepted value, don't bother outputting a
23859 list. */
23860 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23861 else
23862 add_discr_list (variant_die, discr_lists[i]);
23864 for (tree member = TYPE_FIELDS (variant_type);
23865 member != NULL_TREE;
23866 member = DECL_CHAIN (member))
23868 struct vlr_context vlr_sub_ctx = {
23869 vlr_ctx->struct_type, /* struct_type */
23870 NULL /* variant_part_offset */
23872 if (is_variant_part (member))
23874 /* All offsets for fields inside variant parts are relative to
23875 the top-level embedding RECORD_TYPE's base address. On the
23876 other hand, offsets in GCC's types are relative to the
23877 nested-most variant part. So we have to sum offsets each time
23878 we recurse. */
23880 vlr_sub_ctx.variant_part_offset
23881 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
23882 variant_part_offset, byte_position (member)));
23883 gen_variant_part (member, &vlr_sub_ctx, variant_die);
23885 else
23887 vlr_sub_ctx.variant_part_offset = variant_part_offset;
23888 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
23893 free (discr_lists);
23896 /* Generate a DIE for a class member. */
23898 static void
23899 gen_member_die (tree type, dw_die_ref context_die)
23901 tree member;
23902 tree binfo = TYPE_BINFO (type);
23903 dw_die_ref child;
23905 /* If this is not an incomplete type, output descriptions of each of its
23906 members. Note that as we output the DIEs necessary to represent the
23907 members of this record or union type, we will also be trying to output
23908 DIEs to represent the *types* of those members. However the `type'
23909 function (above) will specifically avoid generating type DIEs for member
23910 types *within* the list of member DIEs for this (containing) type except
23911 for those types (of members) which are explicitly marked as also being
23912 members of this (containing) type themselves. The g++ front- end can
23913 force any given type to be treated as a member of some other (containing)
23914 type by setting the TYPE_CONTEXT of the given (member) type to point to
23915 the TREE node representing the appropriate (containing) type. */
23917 /* First output info about the base classes. */
23918 if (binfo)
23920 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
23921 int i;
23922 tree base;
23924 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
23925 gen_inheritance_die (base,
23926 (accesses ? (*accesses)[i] : access_public_node),
23927 type,
23928 context_die);
23931 /* Now output info about the data members and type members. */
23932 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
23934 struct vlr_context vlr_ctx = { type, NULL_TREE };
23936 /* If we thought we were generating minimal debug info for TYPE
23937 and then changed our minds, some of the member declarations
23938 may have already been defined. Don't define them again, but
23939 do put them in the right order. */
23941 child = lookup_decl_die (member);
23942 if (child)
23944 /* Handle inline static data members, which only have in-class
23945 declarations. */
23946 if (child->die_tag == DW_TAG_variable
23947 && child->die_parent == comp_unit_die ()
23948 && get_AT (child, DW_AT_specification) == NULL)
23950 reparent_child (child, context_die);
23951 child->die_tag = DW_TAG_member;
23953 else
23954 splice_child_die (context_die, child);
23957 /* Do not generate standard DWARF for variant parts if we are generating
23958 the corresponding GNAT encodings: DIEs generated for both would
23959 conflict in our mappings. */
23960 else if (is_variant_part (member)
23961 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
23963 vlr_ctx.variant_part_offset = byte_position (member);
23964 gen_variant_part (member, &vlr_ctx, context_die);
23966 else
23968 vlr_ctx.variant_part_offset = NULL_TREE;
23969 gen_decl_die (member, NULL, &vlr_ctx, context_die);
23972 /* For C++ inline static data members emit immediately a DW_TAG_variable
23973 DIE that will refer to that DW_TAG_member through
23974 DW_AT_specification. */
23975 if (TREE_STATIC (member)
23976 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
23977 != -1))
23979 int old_extern = DECL_EXTERNAL (member);
23980 DECL_EXTERNAL (member) = 0;
23981 gen_decl_die (member, NULL, NULL, comp_unit_die ());
23982 DECL_EXTERNAL (member) = old_extern;
23986 /* We do not keep type methods in type variants. */
23987 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
23988 /* Now output info about the function members (if any). */
23989 if (TYPE_METHODS (type) != error_mark_node)
23990 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
23992 /* Don't include clones in the member list. */
23993 if (DECL_ABSTRACT_ORIGIN (member))
23994 continue;
23995 /* Nor constructors for anonymous classes. */
23996 if (DECL_ARTIFICIAL (member)
23997 && dwarf2_name (member, 0) == NULL)
23998 continue;
24000 child = lookup_decl_die (member);
24001 if (child)
24002 splice_child_die (context_die, child);
24003 else
24004 gen_decl_die (member, NULL, NULL, context_die);
24008 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24009 is set, we pretend that the type was never defined, so we only get the
24010 member DIEs needed by later specification DIEs. */
24012 static void
24013 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24014 enum debug_info_usage usage)
24016 if (TREE_ASM_WRITTEN (type))
24018 /* Fill in the bound of variable-length fields in late dwarf if
24019 still incomplete. */
24020 if (!early_dwarf && variably_modified_type_p (type, NULL))
24021 for (tree member = TYPE_FIELDS (type);
24022 member;
24023 member = DECL_CHAIN (member))
24024 fill_variable_array_bounds (TREE_TYPE (member));
24025 return;
24028 dw_die_ref type_die = lookup_type_die (type);
24029 dw_die_ref scope_die = 0;
24030 int nested = 0;
24031 int complete = (TYPE_SIZE (type)
24032 && (! TYPE_STUB_DECL (type)
24033 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24034 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24035 complete = complete && should_emit_struct_debug (type, usage);
24037 if (type_die && ! complete)
24038 return;
24040 if (TYPE_CONTEXT (type) != NULL_TREE
24041 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24042 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24043 nested = 1;
24045 scope_die = scope_die_for (type, context_die);
24047 /* Generate child dies for template paramaters. */
24048 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24049 schedule_generic_params_dies_gen (type);
24051 if (! type_die || (nested && is_cu_die (scope_die)))
24052 /* First occurrence of type or toplevel definition of nested class. */
24054 dw_die_ref old_die = type_die;
24056 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24057 ? record_type_tag (type) : DW_TAG_union_type,
24058 scope_die, type);
24059 equate_type_number_to_die (type, type_die);
24060 if (old_die)
24061 add_AT_specification (type_die, old_die);
24062 else
24063 add_name_attribute (type_die, type_tag (type));
24065 else
24066 remove_AT (type_die, DW_AT_declaration);
24068 /* If this type has been completed, then give it a byte_size attribute and
24069 then give a list of members. */
24070 if (complete && !ns_decl)
24072 /* Prevent infinite recursion in cases where the type of some member of
24073 this type is expressed in terms of this type itself. */
24074 TREE_ASM_WRITTEN (type) = 1;
24075 add_byte_size_attribute (type_die, type);
24076 if (TYPE_STUB_DECL (type) != NULL_TREE)
24078 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24079 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24082 /* If the first reference to this type was as the return type of an
24083 inline function, then it may not have a parent. Fix this now. */
24084 if (type_die->die_parent == NULL)
24085 add_child_die (scope_die, type_die);
24087 push_decl_scope (type);
24088 gen_member_die (type, type_die);
24089 pop_decl_scope ();
24091 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24092 if (TYPE_ARTIFICIAL (type))
24093 add_AT_flag (type_die, DW_AT_artificial, 1);
24095 /* GNU extension: Record what type our vtable lives in. */
24096 if (TYPE_VFIELD (type))
24098 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24100 gen_type_die (vtype, context_die);
24101 add_AT_die_ref (type_die, DW_AT_containing_type,
24102 lookup_type_die (vtype));
24105 else
24107 add_AT_flag (type_die, DW_AT_declaration, 1);
24109 /* We don't need to do this for function-local types. */
24110 if (TYPE_STUB_DECL (type)
24111 && ! decl_function_context (TYPE_STUB_DECL (type)))
24112 vec_safe_push (incomplete_types, type);
24115 if (get_AT (type_die, DW_AT_name))
24116 add_pubtype (type, type_die);
24119 /* Generate a DIE for a subroutine _type_. */
24121 static void
24122 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24124 tree return_type = TREE_TYPE (type);
24125 dw_die_ref subr_die
24126 = new_die (DW_TAG_subroutine_type,
24127 scope_die_for (type, context_die), type);
24129 equate_type_number_to_die (type, subr_die);
24130 add_prototyped_attribute (subr_die, type);
24131 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24132 context_die);
24133 gen_formal_types_die (type, subr_die);
24135 if (get_AT (subr_die, DW_AT_name))
24136 add_pubtype (type, subr_die);
24137 if ((dwarf_version >= 5 || !dwarf_strict)
24138 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24139 add_AT_flag (subr_die, DW_AT_reference, 1);
24140 if ((dwarf_version >= 5 || !dwarf_strict)
24141 && lang_hooks.types.type_dwarf_attribute (type,
24142 DW_AT_rvalue_reference) != -1)
24143 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24146 /* Generate a DIE for a type definition. */
24148 static void
24149 gen_typedef_die (tree decl, dw_die_ref context_die)
24151 dw_die_ref type_die;
24152 tree origin;
24154 if (TREE_ASM_WRITTEN (decl))
24156 if (DECL_ORIGINAL_TYPE (decl))
24157 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24158 return;
24161 TREE_ASM_WRITTEN (decl) = 1;
24162 type_die = new_die (DW_TAG_typedef, context_die, decl);
24163 origin = decl_ultimate_origin (decl);
24164 if (origin != NULL)
24165 add_abstract_origin_attribute (type_die, origin);
24166 else
24168 tree type;
24170 add_name_and_src_coords_attributes (type_die, decl);
24171 if (DECL_ORIGINAL_TYPE (decl))
24173 type = DECL_ORIGINAL_TYPE (decl);
24175 if (type == error_mark_node)
24176 return;
24178 gcc_assert (type != TREE_TYPE (decl));
24179 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24181 else
24183 type = TREE_TYPE (decl);
24185 if (type == error_mark_node)
24186 return;
24188 if (is_naming_typedef_decl (TYPE_NAME (type)))
24190 /* Here, we are in the case of decl being a typedef naming
24191 an anonymous type, e.g:
24192 typedef struct {...} foo;
24193 In that case TREE_TYPE (decl) is not a typedef variant
24194 type and TYPE_NAME of the anonymous type is set to the
24195 TYPE_DECL of the typedef. This construct is emitted by
24196 the C++ FE.
24198 TYPE is the anonymous struct named by the typedef
24199 DECL. As we need the DW_AT_type attribute of the
24200 DW_TAG_typedef to point to the DIE of TYPE, let's
24201 generate that DIE right away. add_type_attribute
24202 called below will then pick (via lookup_type_die) that
24203 anonymous struct DIE. */
24204 if (!TREE_ASM_WRITTEN (type))
24205 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24207 /* This is a GNU Extension. We are adding a
24208 DW_AT_linkage_name attribute to the DIE of the
24209 anonymous struct TYPE. The value of that attribute
24210 is the name of the typedef decl naming the anonymous
24211 struct. This greatly eases the work of consumers of
24212 this debug info. */
24213 add_linkage_name_raw (lookup_type_die (type), decl);
24217 add_type_attribute (type_die, type, decl_quals (decl), false,
24218 context_die);
24220 if (is_naming_typedef_decl (decl))
24221 /* We want that all subsequent calls to lookup_type_die with
24222 TYPE in argument yield the DW_TAG_typedef we have just
24223 created. */
24224 equate_type_number_to_die (type, type_die);
24226 add_accessibility_attribute (type_die, decl);
24229 if (DECL_ABSTRACT_P (decl))
24230 equate_decl_number_to_die (decl, type_die);
24232 if (get_AT (type_die, DW_AT_name))
24233 add_pubtype (decl, type_die);
24236 /* Generate a DIE for a struct, class, enum or union type. */
24238 static void
24239 gen_tagged_type_die (tree type,
24240 dw_die_ref context_die,
24241 enum debug_info_usage usage)
24243 int need_pop;
24245 if (type == NULL_TREE
24246 || !is_tagged_type (type))
24247 return;
24249 if (TREE_ASM_WRITTEN (type))
24250 need_pop = 0;
24251 /* If this is a nested type whose containing class hasn't been written
24252 out yet, writing it out will cover this one, too. This does not apply
24253 to instantiations of member class templates; they need to be added to
24254 the containing class as they are generated. FIXME: This hurts the
24255 idea of combining type decls from multiple TUs, since we can't predict
24256 what set of template instantiations we'll get. */
24257 else if (TYPE_CONTEXT (type)
24258 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24259 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24261 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24263 if (TREE_ASM_WRITTEN (type))
24264 return;
24266 /* If that failed, attach ourselves to the stub. */
24267 push_decl_scope (TYPE_CONTEXT (type));
24268 context_die = lookup_type_die (TYPE_CONTEXT (type));
24269 need_pop = 1;
24271 else if (TYPE_CONTEXT (type) != NULL_TREE
24272 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24274 /* If this type is local to a function that hasn't been written
24275 out yet, use a NULL context for now; it will be fixed up in
24276 decls_for_scope. */
24277 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24278 /* A declaration DIE doesn't count; nested types need to go in the
24279 specification. */
24280 if (context_die && is_declaration_die (context_die))
24281 context_die = NULL;
24282 need_pop = 0;
24284 else
24286 context_die = declare_in_namespace (type, context_die);
24287 need_pop = 0;
24290 if (TREE_CODE (type) == ENUMERAL_TYPE)
24292 /* This might have been written out by the call to
24293 declare_in_namespace. */
24294 if (!TREE_ASM_WRITTEN (type))
24295 gen_enumeration_type_die (type, context_die);
24297 else
24298 gen_struct_or_union_type_die (type, context_die, usage);
24300 if (need_pop)
24301 pop_decl_scope ();
24303 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24304 it up if it is ever completed. gen_*_type_die will set it for us
24305 when appropriate. */
24308 /* Generate a type description DIE. */
24310 static void
24311 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24312 enum debug_info_usage usage)
24314 struct array_descr_info info;
24316 if (type == NULL_TREE || type == error_mark_node)
24317 return;
24319 if (flag_checking && type)
24320 verify_type (type);
24322 if (TYPE_NAME (type) != NULL_TREE
24323 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24324 && is_redundant_typedef (TYPE_NAME (type))
24325 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24326 /* The DECL of this type is a typedef we don't want to emit debug
24327 info for but we want debug info for its underlying typedef.
24328 This can happen for e.g, the injected-class-name of a C++
24329 type. */
24330 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24332 /* If TYPE is a typedef type variant, let's generate debug info
24333 for the parent typedef which TYPE is a type of. */
24334 if (typedef_variant_p (type))
24336 if (TREE_ASM_WRITTEN (type))
24337 return;
24339 /* Prevent broken recursion; we can't hand off to the same type. */
24340 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24342 /* Give typedefs the right scope. */
24343 context_die = scope_die_for (type, context_die);
24345 TREE_ASM_WRITTEN (type) = 1;
24347 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24348 return;
24351 /* If type is an anonymous tagged type named by a typedef, let's
24352 generate debug info for the typedef. */
24353 if (is_naming_typedef_decl (TYPE_NAME (type)))
24355 /* Use the DIE of the containing namespace as the parent DIE of
24356 the type description DIE we want to generate. */
24357 if (DECL_CONTEXT (TYPE_NAME (type))
24358 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24359 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24361 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24362 return;
24365 if (lang_hooks.types.get_debug_type)
24367 tree debug_type = lang_hooks.types.get_debug_type (type);
24369 if (debug_type != NULL_TREE && debug_type != type)
24371 gen_type_die_with_usage (debug_type, context_die, usage);
24372 return;
24376 /* We are going to output a DIE to represent the unqualified version
24377 of this type (i.e. without any const or volatile qualifiers) so
24378 get the main variant (i.e. the unqualified version) of this type
24379 now. (Vectors and arrays are special because the debugging info is in the
24380 cloned type itself. Similarly function/method types can contain extra
24381 ref-qualification). */
24382 if (TREE_CODE (type) == FUNCTION_TYPE
24383 || TREE_CODE (type) == METHOD_TYPE)
24385 /* For function/method types, can't use type_main_variant here,
24386 because that can have different ref-qualifiers for C++,
24387 but try to canonicalize. */
24388 tree main = TYPE_MAIN_VARIANT (type);
24389 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24390 if (check_base_type (t, main) && check_lang_type (t, type))
24391 type = t;
24393 else if (TREE_CODE (type) != VECTOR_TYPE
24394 && TREE_CODE (type) != ARRAY_TYPE)
24395 type = type_main_variant (type);
24397 /* If this is an array type with hidden descriptor, handle it first. */
24398 if (!TREE_ASM_WRITTEN (type)
24399 && lang_hooks.types.get_array_descr_info)
24401 memset (&info, 0, sizeof (info));
24402 if (lang_hooks.types.get_array_descr_info (type, &info))
24404 /* Fortran sometimes emits array types with no dimension. */
24405 gcc_assert (info.ndimensions >= 0
24406 && (info.ndimensions
24407 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24408 gen_descr_array_type_die (type, &info, context_die);
24409 TREE_ASM_WRITTEN (type) = 1;
24410 return;
24414 if (TREE_ASM_WRITTEN (type))
24416 /* Variable-length types may be incomplete even if
24417 TREE_ASM_WRITTEN. For such types, fall through to
24418 gen_array_type_die() and possibly fill in
24419 DW_AT_{upper,lower}_bound attributes. */
24420 if ((TREE_CODE (type) != ARRAY_TYPE
24421 && TREE_CODE (type) != RECORD_TYPE
24422 && TREE_CODE (type) != UNION_TYPE
24423 && TREE_CODE (type) != QUAL_UNION_TYPE)
24424 || !variably_modified_type_p (type, NULL))
24425 return;
24428 switch (TREE_CODE (type))
24430 case ERROR_MARK:
24431 break;
24433 case POINTER_TYPE:
24434 case REFERENCE_TYPE:
24435 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24436 ensures that the gen_type_die recursion will terminate even if the
24437 type is recursive. Recursive types are possible in Ada. */
24438 /* ??? We could perhaps do this for all types before the switch
24439 statement. */
24440 TREE_ASM_WRITTEN (type) = 1;
24442 /* For these types, all that is required is that we output a DIE (or a
24443 set of DIEs) to represent the "basis" type. */
24444 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24445 DINFO_USAGE_IND_USE);
24446 break;
24448 case OFFSET_TYPE:
24449 /* This code is used for C++ pointer-to-data-member types.
24450 Output a description of the relevant class type. */
24451 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24452 DINFO_USAGE_IND_USE);
24454 /* Output a description of the type of the object pointed to. */
24455 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24456 DINFO_USAGE_IND_USE);
24458 /* Now output a DIE to represent this pointer-to-data-member type
24459 itself. */
24460 gen_ptr_to_mbr_type_die (type, context_die);
24461 break;
24463 case FUNCTION_TYPE:
24464 /* Force out return type (in case it wasn't forced out already). */
24465 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24466 DINFO_USAGE_DIR_USE);
24467 gen_subroutine_type_die (type, context_die);
24468 break;
24470 case METHOD_TYPE:
24471 /* Force out return type (in case it wasn't forced out already). */
24472 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24473 DINFO_USAGE_DIR_USE);
24474 gen_subroutine_type_die (type, context_die);
24475 break;
24477 case ARRAY_TYPE:
24478 case VECTOR_TYPE:
24479 gen_array_type_die (type, context_die);
24480 break;
24482 case ENUMERAL_TYPE:
24483 case RECORD_TYPE:
24484 case UNION_TYPE:
24485 case QUAL_UNION_TYPE:
24486 gen_tagged_type_die (type, context_die, usage);
24487 return;
24489 case VOID_TYPE:
24490 case INTEGER_TYPE:
24491 case REAL_TYPE:
24492 case FIXED_POINT_TYPE:
24493 case COMPLEX_TYPE:
24494 case BOOLEAN_TYPE:
24495 case POINTER_BOUNDS_TYPE:
24496 /* No DIEs needed for fundamental types. */
24497 break;
24499 case NULLPTR_TYPE:
24500 case LANG_TYPE:
24501 /* Just use DW_TAG_unspecified_type. */
24503 dw_die_ref type_die = lookup_type_die (type);
24504 if (type_die == NULL)
24506 tree name = TYPE_IDENTIFIER (type);
24507 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24508 type);
24509 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24510 equate_type_number_to_die (type, type_die);
24513 break;
24515 default:
24516 if (is_cxx_auto (type))
24518 tree name = TYPE_IDENTIFIER (type);
24519 dw_die_ref *die = (name == get_identifier ("auto")
24520 ? &auto_die : &decltype_auto_die);
24521 if (!*die)
24523 *die = new_die (DW_TAG_unspecified_type,
24524 comp_unit_die (), NULL_TREE);
24525 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24527 equate_type_number_to_die (type, *die);
24528 break;
24530 gcc_unreachable ();
24533 TREE_ASM_WRITTEN (type) = 1;
24536 static void
24537 gen_type_die (tree type, dw_die_ref context_die)
24539 if (type != error_mark_node)
24541 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24542 if (flag_checking)
24544 dw_die_ref die = lookup_type_die (type);
24545 if (die)
24546 check_die (die);
24551 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24552 things which are local to the given block. */
24554 static void
24555 gen_block_die (tree stmt, dw_die_ref context_die)
24557 int must_output_die = 0;
24558 bool inlined_func;
24560 /* Ignore blocks that are NULL. */
24561 if (stmt == NULL_TREE)
24562 return;
24564 inlined_func = inlined_function_outer_scope_p (stmt);
24566 /* If the block is one fragment of a non-contiguous block, do not
24567 process the variables, since they will have been done by the
24568 origin block. Do process subblocks. */
24569 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24571 tree sub;
24573 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24574 gen_block_die (sub, context_die);
24576 return;
24579 /* Determine if we need to output any Dwarf DIEs at all to represent this
24580 block. */
24581 if (inlined_func)
24582 /* The outer scopes for inlinings *must* always be represented. We
24583 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24584 must_output_die = 1;
24585 else
24587 /* Determine if this block directly contains any "significant"
24588 local declarations which we will need to output DIEs for. */
24589 if (debug_info_level > DINFO_LEVEL_TERSE)
24590 /* We are not in terse mode so *any* local declaration counts
24591 as being a "significant" one. */
24592 must_output_die = ((BLOCK_VARS (stmt) != NULL
24593 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24594 && (TREE_USED (stmt)
24595 || TREE_ASM_WRITTEN (stmt)
24596 || BLOCK_ABSTRACT (stmt)));
24597 else if ((TREE_USED (stmt)
24598 || TREE_ASM_WRITTEN (stmt)
24599 || BLOCK_ABSTRACT (stmt))
24600 && !dwarf2out_ignore_block (stmt))
24601 must_output_die = 1;
24604 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24605 DIE for any block which contains no significant local declarations at
24606 all. Rather, in such cases we just call `decls_for_scope' so that any
24607 needed Dwarf info for any sub-blocks will get properly generated. Note
24608 that in terse mode, our definition of what constitutes a "significant"
24609 local declaration gets restricted to include only inlined function
24610 instances and local (nested) function definitions. */
24611 if (must_output_die)
24613 if (inlined_func)
24615 /* If STMT block is abstract, that means we have been called
24616 indirectly from dwarf2out_abstract_function.
24617 That function rightfully marks the descendent blocks (of
24618 the abstract function it is dealing with) as being abstract,
24619 precisely to prevent us from emitting any
24620 DW_TAG_inlined_subroutine DIE as a descendent
24621 of an abstract function instance. So in that case, we should
24622 not call gen_inlined_subroutine_die.
24624 Later though, when cgraph asks dwarf2out to emit info
24625 for the concrete instance of the function decl into which
24626 the concrete instance of STMT got inlined, the later will lead
24627 to the generation of a DW_TAG_inlined_subroutine DIE. */
24628 if (! BLOCK_ABSTRACT (stmt))
24629 gen_inlined_subroutine_die (stmt, context_die);
24631 else
24632 gen_lexical_block_die (stmt, context_die);
24634 else
24635 decls_for_scope (stmt, context_die);
24638 /* Process variable DECL (or variable with origin ORIGIN) within
24639 block STMT and add it to CONTEXT_DIE. */
24640 static void
24641 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24643 dw_die_ref die;
24644 tree decl_or_origin = decl ? decl : origin;
24646 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24647 die = lookup_decl_die (decl_or_origin);
24648 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24650 if (TYPE_DECL_IS_STUB (decl_or_origin))
24651 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24652 else
24653 die = lookup_decl_die (decl_or_origin);
24654 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24655 if (! die && ! early_dwarf)
24656 return;
24658 else
24659 die = NULL;
24661 if (die != NULL && die->die_parent == NULL)
24662 add_child_die (context_die, die);
24663 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24665 if (early_dwarf)
24666 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24667 stmt, context_die);
24669 else
24670 gen_decl_die (decl, origin, NULL, context_die);
24673 /* Generate all of the decls declared within a given scope and (recursively)
24674 all of its sub-blocks. */
24676 static void
24677 decls_for_scope (tree stmt, dw_die_ref context_die)
24679 tree decl;
24680 unsigned int i;
24681 tree subblocks;
24683 /* Ignore NULL blocks. */
24684 if (stmt == NULL_TREE)
24685 return;
24687 /* Output the DIEs to represent all of the data objects and typedefs
24688 declared directly within this block but not within any nested
24689 sub-blocks. Also, nested function and tag DIEs have been
24690 generated with a parent of NULL; fix that up now. We don't
24691 have to do this if we're at -g1. */
24692 if (debug_info_level > DINFO_LEVEL_TERSE)
24694 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24695 process_scope_var (stmt, decl, NULL_TREE, context_die);
24696 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24697 origin - avoid doing this twice as we have no good way to see
24698 if we've done it once already. */
24699 if (! early_dwarf)
24700 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24701 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
24702 context_die);
24705 /* Even if we're at -g1, we need to process the subblocks in order to get
24706 inlined call information. */
24708 /* Output the DIEs to represent all sub-blocks (and the items declared
24709 therein) of this block. */
24710 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24711 subblocks != NULL;
24712 subblocks = BLOCK_CHAIN (subblocks))
24713 gen_block_die (subblocks, context_die);
24716 /* Is this a typedef we can avoid emitting? */
24718 bool
24719 is_redundant_typedef (const_tree decl)
24721 if (TYPE_DECL_IS_STUB (decl))
24722 return true;
24724 if (DECL_ARTIFICIAL (decl)
24725 && DECL_CONTEXT (decl)
24726 && is_tagged_type (DECL_CONTEXT (decl))
24727 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24728 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24729 /* Also ignore the artificial member typedef for the class name. */
24730 return true;
24732 return false;
24735 /* Return TRUE if TYPE is a typedef that names a type for linkage
24736 purposes. This kind of typedefs is produced by the C++ FE for
24737 constructs like:
24739 typedef struct {...} foo;
24741 In that case, there is no typedef variant type produced for foo.
24742 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24743 struct type. */
24745 static bool
24746 is_naming_typedef_decl (const_tree decl)
24748 if (decl == NULL_TREE
24749 || TREE_CODE (decl) != TYPE_DECL
24750 || DECL_NAMELESS (decl)
24751 || !is_tagged_type (TREE_TYPE (decl))
24752 || DECL_IS_BUILTIN (decl)
24753 || is_redundant_typedef (decl)
24754 /* It looks like Ada produces TYPE_DECLs that are very similar
24755 to C++ naming typedefs but that have different
24756 semantics. Let's be specific to c++ for now. */
24757 || !is_cxx ())
24758 return FALSE;
24760 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24761 && TYPE_NAME (TREE_TYPE (decl)) == decl
24762 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24763 != TYPE_NAME (TREE_TYPE (decl))));
24766 /* Looks up the DIE for a context. */
24768 static inline dw_die_ref
24769 lookup_context_die (tree context)
24771 if (context)
24773 /* Find die that represents this context. */
24774 if (TYPE_P (context))
24776 context = TYPE_MAIN_VARIANT (context);
24777 dw_die_ref ctx = lookup_type_die (context);
24778 if (!ctx)
24779 return NULL;
24780 return strip_naming_typedef (context, ctx);
24782 else
24783 return lookup_decl_die (context);
24785 return comp_unit_die ();
24788 /* Returns the DIE for a context. */
24790 static inline dw_die_ref
24791 get_context_die (tree context)
24793 if (context)
24795 /* Find die that represents this context. */
24796 if (TYPE_P (context))
24798 context = TYPE_MAIN_VARIANT (context);
24799 return strip_naming_typedef (context, force_type_die (context));
24801 else
24802 return force_decl_die (context);
24804 return comp_unit_die ();
24807 /* Returns the DIE for decl. A DIE will always be returned. */
24809 static dw_die_ref
24810 force_decl_die (tree decl)
24812 dw_die_ref decl_die;
24813 unsigned saved_external_flag;
24814 tree save_fn = NULL_TREE;
24815 decl_die = lookup_decl_die (decl);
24816 if (!decl_die)
24818 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24820 decl_die = lookup_decl_die (decl);
24821 if (decl_die)
24822 return decl_die;
24824 switch (TREE_CODE (decl))
24826 case FUNCTION_DECL:
24827 /* Clear current_function_decl, so that gen_subprogram_die thinks
24828 that this is a declaration. At this point, we just want to force
24829 declaration die. */
24830 save_fn = current_function_decl;
24831 current_function_decl = NULL_TREE;
24832 gen_subprogram_die (decl, context_die);
24833 current_function_decl = save_fn;
24834 break;
24836 case VAR_DECL:
24837 /* Set external flag to force declaration die. Restore it after
24838 gen_decl_die() call. */
24839 saved_external_flag = DECL_EXTERNAL (decl);
24840 DECL_EXTERNAL (decl) = 1;
24841 gen_decl_die (decl, NULL, NULL, context_die);
24842 DECL_EXTERNAL (decl) = saved_external_flag;
24843 break;
24845 case NAMESPACE_DECL:
24846 if (dwarf_version >= 3 || !dwarf_strict)
24847 dwarf2out_decl (decl);
24848 else
24849 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
24850 decl_die = comp_unit_die ();
24851 break;
24853 case TRANSLATION_UNIT_DECL:
24854 decl_die = comp_unit_die ();
24855 break;
24857 default:
24858 gcc_unreachable ();
24861 /* We should be able to find the DIE now. */
24862 if (!decl_die)
24863 decl_die = lookup_decl_die (decl);
24864 gcc_assert (decl_die);
24867 return decl_die;
24870 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
24871 always returned. */
24873 static dw_die_ref
24874 force_type_die (tree type)
24876 dw_die_ref type_die;
24878 type_die = lookup_type_die (type);
24879 if (!type_die)
24881 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
24883 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
24884 false, context_die);
24885 gcc_assert (type_die);
24887 return type_die;
24890 /* Force out any required namespaces to be able to output DECL,
24891 and return the new context_die for it, if it's changed. */
24893 static dw_die_ref
24894 setup_namespace_context (tree thing, dw_die_ref context_die)
24896 tree context = (DECL_P (thing)
24897 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
24898 if (context && TREE_CODE (context) == NAMESPACE_DECL)
24899 /* Force out the namespace. */
24900 context_die = force_decl_die (context);
24902 return context_die;
24905 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
24906 type) within its namespace, if appropriate.
24908 For compatibility with older debuggers, namespace DIEs only contain
24909 declarations; all definitions are emitted at CU scope, with
24910 DW_AT_specification pointing to the declaration (like with class
24911 members). */
24913 static dw_die_ref
24914 declare_in_namespace (tree thing, dw_die_ref context_die)
24916 dw_die_ref ns_context;
24918 if (debug_info_level <= DINFO_LEVEL_TERSE)
24919 return context_die;
24921 /* External declarations in the local scope only need to be emitted
24922 once, not once in the namespace and once in the scope.
24924 This avoids declaring the `extern' below in the
24925 namespace DIE as well as in the innermost scope:
24927 namespace S
24929 int i=5;
24930 int foo()
24932 int i=8;
24933 extern int i;
24934 return i;
24938 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
24939 return context_die;
24941 /* If this decl is from an inlined function, then don't try to emit it in its
24942 namespace, as we will get confused. It would have already been emitted
24943 when the abstract instance of the inline function was emitted anyways. */
24944 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
24945 return context_die;
24947 ns_context = setup_namespace_context (thing, context_die);
24949 if (ns_context != context_die)
24951 if (is_fortran ())
24952 return ns_context;
24953 if (DECL_P (thing))
24954 gen_decl_die (thing, NULL, NULL, ns_context);
24955 else
24956 gen_type_die (thing, ns_context);
24958 return context_die;
24961 /* Generate a DIE for a namespace or namespace alias. */
24963 static void
24964 gen_namespace_die (tree decl, dw_die_ref context_die)
24966 dw_die_ref namespace_die;
24968 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
24969 they are an alias of. */
24970 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
24972 /* Output a real namespace or module. */
24973 context_die = setup_namespace_context (decl, comp_unit_die ());
24974 namespace_die = new_die (is_fortran ()
24975 ? DW_TAG_module : DW_TAG_namespace,
24976 context_die, decl);
24977 /* For Fortran modules defined in different CU don't add src coords. */
24978 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
24980 const char *name = dwarf2_name (decl, 0);
24981 if (name)
24982 add_name_attribute (namespace_die, name);
24984 else
24985 add_name_and_src_coords_attributes (namespace_die, decl);
24986 if (DECL_EXTERNAL (decl))
24987 add_AT_flag (namespace_die, DW_AT_declaration, 1);
24988 equate_decl_number_to_die (decl, namespace_die);
24990 else
24992 /* Output a namespace alias. */
24994 /* Force out the namespace we are an alias of, if necessary. */
24995 dw_die_ref origin_die
24996 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
24998 if (DECL_FILE_SCOPE_P (decl)
24999 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25000 context_die = setup_namespace_context (decl, comp_unit_die ());
25001 /* Now create the namespace alias DIE. */
25002 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25003 add_name_and_src_coords_attributes (namespace_die, decl);
25004 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25005 equate_decl_number_to_die (decl, namespace_die);
25007 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25008 if (want_pubnames ())
25009 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25012 /* Generate Dwarf debug information for a decl described by DECL.
25013 The return value is currently only meaningful for PARM_DECLs,
25014 for all other decls it returns NULL.
25016 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25017 It can be NULL otherwise. */
25019 static dw_die_ref
25020 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25021 dw_die_ref context_die)
25023 tree decl_or_origin = decl ? decl : origin;
25024 tree class_origin = NULL, ultimate_origin;
25026 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25027 return NULL;
25029 /* Ignore pointer bounds decls. */
25030 if (DECL_P (decl_or_origin)
25031 && TREE_TYPE (decl_or_origin)
25032 && POINTER_BOUNDS_P (decl_or_origin))
25033 return NULL;
25035 switch (TREE_CODE (decl_or_origin))
25037 case ERROR_MARK:
25038 break;
25040 case CONST_DECL:
25041 if (!is_fortran () && !is_ada ())
25043 /* The individual enumerators of an enum type get output when we output
25044 the Dwarf representation of the relevant enum type itself. */
25045 break;
25048 /* Emit its type. */
25049 gen_type_die (TREE_TYPE (decl), context_die);
25051 /* And its containing namespace. */
25052 context_die = declare_in_namespace (decl, context_die);
25054 gen_const_die (decl, context_die);
25055 break;
25057 case FUNCTION_DECL:
25058 /* Don't output any DIEs to represent mere function declarations,
25059 unless they are class members or explicit block externs. */
25060 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25061 && DECL_FILE_SCOPE_P (decl_or_origin)
25062 && (current_function_decl == NULL_TREE
25063 || DECL_ARTIFICIAL (decl_or_origin)))
25064 break;
25066 #if 0
25067 /* FIXME */
25068 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25069 on local redeclarations of global functions. That seems broken. */
25070 if (current_function_decl != decl)
25071 /* This is only a declaration. */;
25072 #endif
25074 /* If we're emitting a clone, emit info for the abstract instance. */
25075 if (origin || DECL_ORIGIN (decl) != decl)
25076 dwarf2out_abstract_function (origin
25077 ? DECL_ORIGIN (origin)
25078 : DECL_ABSTRACT_ORIGIN (decl));
25080 /* If we're emitting an out-of-line copy of an inline function,
25081 emit info for the abstract instance and set up to refer to it. */
25082 else if (cgraph_function_possibly_inlined_p (decl)
25083 && ! DECL_ABSTRACT_P (decl)
25084 && ! class_or_namespace_scope_p (context_die)
25085 /* dwarf2out_abstract_function won't emit a die if this is just
25086 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25087 that case, because that works only if we have a die. */
25088 && DECL_INITIAL (decl) != NULL_TREE)
25090 dwarf2out_abstract_function (decl);
25091 set_decl_origin_self (decl);
25094 /* Otherwise we're emitting the primary DIE for this decl. */
25095 else if (debug_info_level > DINFO_LEVEL_TERSE)
25097 /* Before we describe the FUNCTION_DECL itself, make sure that we
25098 have its containing type. */
25099 if (!origin)
25100 origin = decl_class_context (decl);
25101 if (origin != NULL_TREE)
25102 gen_type_die (origin, context_die);
25104 /* And its return type. */
25105 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25107 /* And its virtual context. */
25108 if (DECL_VINDEX (decl) != NULL_TREE)
25109 gen_type_die (DECL_CONTEXT (decl), context_die);
25111 /* Make sure we have a member DIE for decl. */
25112 if (origin != NULL_TREE)
25113 gen_type_die_for_member (origin, decl, context_die);
25115 /* And its containing namespace. */
25116 context_die = declare_in_namespace (decl, context_die);
25119 /* Now output a DIE to represent the function itself. */
25120 if (decl)
25121 gen_subprogram_die (decl, context_die);
25122 break;
25124 case TYPE_DECL:
25125 /* If we are in terse mode, don't generate any DIEs to represent any
25126 actual typedefs. */
25127 if (debug_info_level <= DINFO_LEVEL_TERSE)
25128 break;
25130 /* In the special case of a TYPE_DECL node representing the declaration
25131 of some type tag, if the given TYPE_DECL is marked as having been
25132 instantiated from some other (original) TYPE_DECL node (e.g. one which
25133 was generated within the original definition of an inline function) we
25134 used to generate a special (abbreviated) DW_TAG_structure_type,
25135 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25136 should be actually referencing those DIEs, as variable DIEs with that
25137 type would be emitted already in the abstract origin, so it was always
25138 removed during unused type prunning. Don't add anything in this
25139 case. */
25140 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25141 break;
25143 if (is_redundant_typedef (decl))
25144 gen_type_die (TREE_TYPE (decl), context_die);
25145 else
25146 /* Output a DIE to represent the typedef itself. */
25147 gen_typedef_die (decl, context_die);
25148 break;
25150 case LABEL_DECL:
25151 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25152 gen_label_die (decl, context_die);
25153 break;
25155 case VAR_DECL:
25156 case RESULT_DECL:
25157 /* If we are in terse mode, don't generate any DIEs to represent any
25158 variable declarations or definitions. */
25159 if (debug_info_level <= DINFO_LEVEL_TERSE)
25160 break;
25162 /* Output any DIEs that are needed to specify the type of this data
25163 object. */
25164 if (decl_by_reference_p (decl_or_origin))
25165 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25166 else
25167 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25169 /* And its containing type. */
25170 class_origin = decl_class_context (decl_or_origin);
25171 if (class_origin != NULL_TREE)
25172 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25174 /* And its containing namespace. */
25175 context_die = declare_in_namespace (decl_or_origin, context_die);
25177 /* Now output the DIE to represent the data object itself. This gets
25178 complicated because of the possibility that the VAR_DECL really
25179 represents an inlined instance of a formal parameter for an inline
25180 function. */
25181 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25182 if (ultimate_origin != NULL_TREE
25183 && TREE_CODE (ultimate_origin) == PARM_DECL)
25184 gen_formal_parameter_die (decl, origin,
25185 true /* Emit name attribute. */,
25186 context_die);
25187 else
25188 gen_variable_die (decl, origin, context_die);
25189 break;
25191 case FIELD_DECL:
25192 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25193 /* Ignore the nameless fields that are used to skip bits but handle C++
25194 anonymous unions and structs. */
25195 if (DECL_NAME (decl) != NULL_TREE
25196 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25197 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25199 gen_type_die (member_declared_type (decl), context_die);
25200 gen_field_die (decl, ctx, context_die);
25202 break;
25204 case PARM_DECL:
25205 if (DECL_BY_REFERENCE (decl_or_origin))
25206 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25207 else
25208 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25209 return gen_formal_parameter_die (decl, origin,
25210 true /* Emit name attribute. */,
25211 context_die);
25213 case NAMESPACE_DECL:
25214 if (dwarf_version >= 3 || !dwarf_strict)
25215 gen_namespace_die (decl, context_die);
25216 break;
25218 case IMPORTED_DECL:
25219 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25220 DECL_CONTEXT (decl), context_die);
25221 break;
25223 case NAMELIST_DECL:
25224 gen_namelist_decl (DECL_NAME (decl), context_die,
25225 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25226 break;
25228 default:
25229 /* Probably some frontend-internal decl. Assume we don't care. */
25230 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25231 break;
25234 return NULL;
25237 /* Output initial debug information for global DECL. Called at the
25238 end of the parsing process.
25240 This is the initial debug generation process. As such, the DIEs
25241 generated may be incomplete. A later debug generation pass
25242 (dwarf2out_late_global_decl) will augment the information generated
25243 in this pass (e.g., with complete location info). */
25245 static void
25246 dwarf2out_early_global_decl (tree decl)
25248 set_early_dwarf s;
25250 /* gen_decl_die() will set DECL_ABSTRACT because
25251 cgraph_function_possibly_inlined_p() returns true. This is in
25252 turn will cause DW_AT_inline attributes to be set.
25254 This happens because at early dwarf generation, there is no
25255 cgraph information, causing cgraph_function_possibly_inlined_p()
25256 to return true. Trick cgraph_function_possibly_inlined_p()
25257 while we generate dwarf early. */
25258 bool save = symtab->global_info_ready;
25259 symtab->global_info_ready = true;
25261 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25262 other DECLs and they can point to template types or other things
25263 that dwarf2out can't handle when done via dwarf2out_decl. */
25264 if (TREE_CODE (decl) != TYPE_DECL
25265 && TREE_CODE (decl) != PARM_DECL)
25267 tree save_fndecl = current_function_decl;
25268 if (TREE_CODE (decl) == FUNCTION_DECL)
25270 /* No cfun means the symbol has no body, so there's nothing
25271 to emit. */
25272 if (!DECL_STRUCT_FUNCTION (decl))
25273 goto early_decl_exit;
25275 /* For nested functions, make sure we have DIEs for the parents first
25276 so that all nested DIEs are generated at the proper scope in the
25277 first shot. */
25278 tree context = decl_function_context (decl);
25279 if (context != NULL && lookup_decl_die (context) == NULL)
25281 current_function_decl = context;
25282 dwarf2out_decl (context);
25285 current_function_decl = decl;
25287 dwarf2out_decl (decl);
25288 if (TREE_CODE (decl) == FUNCTION_DECL)
25289 current_function_decl = save_fndecl;
25291 early_decl_exit:
25292 symtab->global_info_ready = save;
25295 /* Output debug information for global decl DECL. Called from
25296 toplev.c after compilation proper has finished. */
25298 static void
25299 dwarf2out_late_global_decl (tree decl)
25301 /* Fill-in any location information we were unable to determine
25302 on the first pass. */
25303 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25305 dw_die_ref die = lookup_decl_die (decl);
25307 /* We have to generate early debug late for LTO. */
25308 if (! die && in_lto_p)
25310 dwarf2out_decl (decl);
25311 die = lookup_decl_die (decl);
25314 if (die)
25316 /* We get called via the symtab code invoking late_global_decl
25317 for symbols that are optimized out. Do not add locations
25318 for those. */
25319 varpool_node *node = varpool_node::get (decl);
25320 if (! node || ! node->definition)
25321 tree_add_const_value_attribute_for_decl (die, decl);
25322 else
25323 add_location_or_const_value_attribute (die, decl, false);
25328 /* Output debug information for type decl DECL. Called from toplev.c
25329 and from language front ends (to record built-in types). */
25330 static void
25331 dwarf2out_type_decl (tree decl, int local)
25333 if (!local)
25335 set_early_dwarf s;
25336 dwarf2out_decl (decl);
25340 /* Output debug information for imported module or decl DECL.
25341 NAME is non-NULL name in the lexical block if the decl has been renamed.
25342 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25343 that DECL belongs to.
25344 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25345 static void
25346 dwarf2out_imported_module_or_decl_1 (tree decl,
25347 tree name,
25348 tree lexical_block,
25349 dw_die_ref lexical_block_die)
25351 expanded_location xloc;
25352 dw_die_ref imported_die = NULL;
25353 dw_die_ref at_import_die;
25355 if (TREE_CODE (decl) == IMPORTED_DECL)
25357 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25358 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25359 gcc_assert (decl);
25361 else
25362 xloc = expand_location (input_location);
25364 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25366 at_import_die = force_type_die (TREE_TYPE (decl));
25367 /* For namespace N { typedef void T; } using N::T; base_type_die
25368 returns NULL, but DW_TAG_imported_declaration requires
25369 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25370 if (!at_import_die)
25372 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25373 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25374 at_import_die = lookup_type_die (TREE_TYPE (decl));
25375 gcc_assert (at_import_die);
25378 else
25380 at_import_die = lookup_decl_die (decl);
25381 if (!at_import_die)
25383 /* If we're trying to avoid duplicate debug info, we may not have
25384 emitted the member decl for this field. Emit it now. */
25385 if (TREE_CODE (decl) == FIELD_DECL)
25387 tree type = DECL_CONTEXT (decl);
25389 if (TYPE_CONTEXT (type)
25390 && TYPE_P (TYPE_CONTEXT (type))
25391 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25392 DINFO_USAGE_DIR_USE))
25393 return;
25394 gen_type_die_for_member (type, decl,
25395 get_context_die (TYPE_CONTEXT (type)));
25397 if (TREE_CODE (decl) == NAMELIST_DECL)
25398 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25399 get_context_die (DECL_CONTEXT (decl)),
25400 NULL_TREE);
25401 else
25402 at_import_die = force_decl_die (decl);
25406 if (TREE_CODE (decl) == NAMESPACE_DECL)
25408 if (dwarf_version >= 3 || !dwarf_strict)
25409 imported_die = new_die (DW_TAG_imported_module,
25410 lexical_block_die,
25411 lexical_block);
25412 else
25413 return;
25415 else
25416 imported_die = new_die (DW_TAG_imported_declaration,
25417 lexical_block_die,
25418 lexical_block);
25420 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25421 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25422 if (name)
25423 add_AT_string (imported_die, DW_AT_name,
25424 IDENTIFIER_POINTER (name));
25425 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25428 /* Output debug information for imported module or decl DECL.
25429 NAME is non-NULL name in context if the decl has been renamed.
25430 CHILD is true if decl is one of the renamed decls as part of
25431 importing whole module. */
25433 static void
25434 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25435 bool child)
25437 /* dw_die_ref at_import_die; */
25438 dw_die_ref scope_die;
25440 if (debug_info_level <= DINFO_LEVEL_TERSE)
25441 return;
25443 gcc_assert (decl);
25445 set_early_dwarf s;
25447 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25448 We need decl DIE for reference and scope die. First, get DIE for the decl
25449 itself. */
25451 /* Get the scope die for decl context. Use comp_unit_die for global module
25452 or decl. If die is not found for non globals, force new die. */
25453 if (context
25454 && TYPE_P (context)
25455 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25456 return;
25458 scope_die = get_context_die (context);
25460 if (child)
25462 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25463 there is nothing we can do, here. */
25464 if (dwarf_version < 3 && dwarf_strict)
25465 return;
25467 gcc_assert (scope_die->die_child);
25468 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25469 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25470 scope_die = scope_die->die_child;
25473 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25474 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25477 /* Output debug information for namelists. */
25479 static dw_die_ref
25480 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25482 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25483 tree value;
25484 unsigned i;
25486 if (debug_info_level <= DINFO_LEVEL_TERSE)
25487 return NULL;
25489 gcc_assert (scope_die != NULL);
25490 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25491 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25493 /* If there are no item_decls, we have a nondefining namelist, e.g.
25494 with USE association; hence, set DW_AT_declaration. */
25495 if (item_decls == NULL_TREE)
25497 add_AT_flag (nml_die, DW_AT_declaration, 1);
25498 return nml_die;
25501 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25503 nml_item_ref_die = lookup_decl_die (value);
25504 if (!nml_item_ref_die)
25505 nml_item_ref_die = force_decl_die (value);
25507 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25508 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25510 return nml_die;
25514 /* Write the debugging output for DECL and return the DIE. */
25516 static void
25517 dwarf2out_decl (tree decl)
25519 dw_die_ref context_die = comp_unit_die ();
25521 switch (TREE_CODE (decl))
25523 case ERROR_MARK:
25524 return;
25526 case FUNCTION_DECL:
25527 /* What we would really like to do here is to filter out all mere
25528 file-scope declarations of file-scope functions which are never
25529 referenced later within this translation unit (and keep all of ones
25530 that *are* referenced later on) but we aren't clairvoyant, so we have
25531 no idea which functions will be referenced in the future (i.e. later
25532 on within the current translation unit). So here we just ignore all
25533 file-scope function declarations which are not also definitions. If
25534 and when the debugger needs to know something about these functions,
25535 it will have to hunt around and find the DWARF information associated
25536 with the definition of the function.
25538 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25539 nodes represent definitions and which ones represent mere
25540 declarations. We have to check DECL_INITIAL instead. That's because
25541 the C front-end supports some weird semantics for "extern inline"
25542 function definitions. These can get inlined within the current
25543 translation unit (and thus, we need to generate Dwarf info for their
25544 abstract instances so that the Dwarf info for the concrete inlined
25545 instances can have something to refer to) but the compiler never
25546 generates any out-of-lines instances of such things (despite the fact
25547 that they *are* definitions).
25549 The important point is that the C front-end marks these "extern
25550 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25551 them anyway. Note that the C++ front-end also plays some similar games
25552 for inline function definitions appearing within include files which
25553 also contain `#pragma interface' pragmas.
25555 If we are called from dwarf2out_abstract_function output a DIE
25556 anyway. We can end up here this way with early inlining and LTO
25557 where the inlined function is output in a different LTRANS unit
25558 or not at all. */
25559 if (DECL_INITIAL (decl) == NULL_TREE
25560 && ! DECL_ABSTRACT_P (decl))
25561 return;
25563 /* If we're a nested function, initially use a parent of NULL; if we're
25564 a plain function, this will be fixed up in decls_for_scope. If
25565 we're a method, it will be ignored, since we already have a DIE. */
25566 if (decl_function_context (decl)
25567 /* But if we're in terse mode, we don't care about scope. */
25568 && debug_info_level > DINFO_LEVEL_TERSE)
25569 context_die = NULL;
25570 break;
25572 case VAR_DECL:
25573 /* For local statics lookup proper context die. */
25574 if (local_function_static (decl))
25575 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25577 /* If we are in terse mode, don't generate any DIEs to represent any
25578 variable declarations or definitions. */
25579 if (debug_info_level <= DINFO_LEVEL_TERSE)
25580 return;
25581 break;
25583 case CONST_DECL:
25584 if (debug_info_level <= DINFO_LEVEL_TERSE)
25585 return;
25586 if (!is_fortran () && !is_ada ())
25587 return;
25588 if (TREE_STATIC (decl) && decl_function_context (decl))
25589 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25590 break;
25592 case NAMESPACE_DECL:
25593 case IMPORTED_DECL:
25594 if (debug_info_level <= DINFO_LEVEL_TERSE)
25595 return;
25596 if (lookup_decl_die (decl) != NULL)
25597 return;
25598 break;
25600 case TYPE_DECL:
25601 /* Don't emit stubs for types unless they are needed by other DIEs. */
25602 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25603 return;
25605 /* Don't bother trying to generate any DIEs to represent any of the
25606 normal built-in types for the language we are compiling. */
25607 if (DECL_IS_BUILTIN (decl))
25608 return;
25610 /* If we are in terse mode, don't generate any DIEs for types. */
25611 if (debug_info_level <= DINFO_LEVEL_TERSE)
25612 return;
25614 /* If we're a function-scope tag, initially use a parent of NULL;
25615 this will be fixed up in decls_for_scope. */
25616 if (decl_function_context (decl))
25617 context_die = NULL;
25619 break;
25621 case NAMELIST_DECL:
25622 break;
25624 default:
25625 return;
25628 gen_decl_die (decl, NULL, NULL, context_die);
25630 if (flag_checking)
25632 dw_die_ref die = lookup_decl_die (decl);
25633 if (die)
25634 check_die (die);
25638 /* Write the debugging output for DECL. */
25640 static void
25641 dwarf2out_function_decl (tree decl)
25643 dwarf2out_decl (decl);
25644 call_arg_locations = NULL;
25645 call_arg_loc_last = NULL;
25646 call_site_count = -1;
25647 tail_call_site_count = -1;
25648 decl_loc_table->empty ();
25649 cached_dw_loc_list_table->empty ();
25652 /* Output a marker (i.e. a label) for the beginning of the generated code for
25653 a lexical block. */
25655 static void
25656 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25657 unsigned int blocknum)
25659 switch_to_section (current_function_section ());
25660 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25663 /* Output a marker (i.e. a label) for the end of the generated code for a
25664 lexical block. */
25666 static void
25667 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25669 switch_to_section (current_function_section ());
25670 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25673 /* Returns nonzero if it is appropriate not to emit any debugging
25674 information for BLOCK, because it doesn't contain any instructions.
25676 Don't allow this for blocks with nested functions or local classes
25677 as we would end up with orphans, and in the presence of scheduling
25678 we may end up calling them anyway. */
25680 static bool
25681 dwarf2out_ignore_block (const_tree block)
25683 tree decl;
25684 unsigned int i;
25686 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25687 if (TREE_CODE (decl) == FUNCTION_DECL
25688 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25689 return 0;
25690 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25692 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25693 if (TREE_CODE (decl) == FUNCTION_DECL
25694 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25695 return 0;
25698 return 1;
25701 /* Hash table routines for file_hash. */
25703 bool
25704 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25706 return filename_cmp (p1->filename, p2) == 0;
25709 hashval_t
25710 dwarf_file_hasher::hash (dwarf_file_data *p)
25712 return htab_hash_string (p->filename);
25715 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25716 dwarf2out.c) and return its "index". The index of each (known) filename is
25717 just a unique number which is associated with only that one filename. We
25718 need such numbers for the sake of generating labels (in the .debug_sfnames
25719 section) and references to those files numbers (in the .debug_srcinfo
25720 and .debug_macinfo sections). If the filename given as an argument is not
25721 found in our current list, add it to the list and assign it the next
25722 available unique index number. */
25724 static struct dwarf_file_data *
25725 lookup_filename (const char *file_name)
25727 struct dwarf_file_data * created;
25729 if (!file_name)
25730 return NULL;
25732 dwarf_file_data **slot
25733 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25734 INSERT);
25735 if (*slot)
25736 return *slot;
25738 created = ggc_alloc<dwarf_file_data> ();
25739 created->filename = file_name;
25740 created->emitted_number = 0;
25741 *slot = created;
25742 return created;
25745 /* If the assembler will construct the file table, then translate the compiler
25746 internal file table number into the assembler file table number, and emit
25747 a .file directive if we haven't already emitted one yet. The file table
25748 numbers are different because we prune debug info for unused variables and
25749 types, which may include filenames. */
25751 static int
25752 maybe_emit_file (struct dwarf_file_data * fd)
25754 if (! fd->emitted_number)
25756 if (last_emitted_file)
25757 fd->emitted_number = last_emitted_file->emitted_number + 1;
25758 else
25759 fd->emitted_number = 1;
25760 last_emitted_file = fd;
25762 if (DWARF2_ASM_LINE_DEBUG_INFO)
25764 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25765 output_quoted_string (asm_out_file,
25766 remap_debug_filename (fd->filename));
25767 fputc ('\n', asm_out_file);
25771 return fd->emitted_number;
25774 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25775 That generation should happen after function debug info has been
25776 generated. The value of the attribute is the constant value of ARG. */
25778 static void
25779 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25781 die_arg_entry entry;
25783 if (!die || !arg)
25784 return;
25786 gcc_assert (early_dwarf);
25788 if (!tmpl_value_parm_die_table)
25789 vec_alloc (tmpl_value_parm_die_table, 32);
25791 entry.die = die;
25792 entry.arg = arg;
25793 vec_safe_push (tmpl_value_parm_die_table, entry);
25796 /* Return TRUE if T is an instance of generic type, FALSE
25797 otherwise. */
25799 static bool
25800 generic_type_p (tree t)
25802 if (t == NULL_TREE || !TYPE_P (t))
25803 return false;
25804 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25807 /* Schedule the generation of the generic parameter dies for the
25808 instance of generic type T. The proper generation itself is later
25809 done by gen_scheduled_generic_parms_dies. */
25811 static void
25812 schedule_generic_params_dies_gen (tree t)
25814 if (!generic_type_p (t))
25815 return;
25817 gcc_assert (early_dwarf);
25819 if (!generic_type_instances)
25820 vec_alloc (generic_type_instances, 256);
25822 vec_safe_push (generic_type_instances, t);
25825 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25826 by append_entry_to_tmpl_value_parm_die_table. This function must
25827 be called after function DIEs have been generated. */
25829 static void
25830 gen_remaining_tmpl_value_param_die_attribute (void)
25832 if (tmpl_value_parm_die_table)
25834 unsigned i, j;
25835 die_arg_entry *e;
25837 /* We do this in two phases - first get the cases we can
25838 handle during early-finish, preserving those we cannot
25839 (containing symbolic constants where we don't yet know
25840 whether we are going to output the referenced symbols).
25841 For those we try again at late-finish. */
25842 j = 0;
25843 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
25845 if (!tree_add_const_value_attribute (e->die, e->arg))
25847 dw_loc_descr_ref loc = NULL;
25848 if (! early_dwarf
25849 && (dwarf_version >= 5 || !dwarf_strict))
25850 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
25851 if (loc)
25852 add_AT_loc (e->die, DW_AT_location, loc);
25853 else
25854 (*tmpl_value_parm_die_table)[j++] = *e;
25857 tmpl_value_parm_die_table->truncate (j);
25861 /* Generate generic parameters DIEs for instances of generic types
25862 that have been previously scheduled by
25863 schedule_generic_params_dies_gen. This function must be called
25864 after all the types of the CU have been laid out. */
25866 static void
25867 gen_scheduled_generic_parms_dies (void)
25869 unsigned i;
25870 tree t;
25872 if (!generic_type_instances)
25873 return;
25875 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
25876 if (COMPLETE_TYPE_P (t))
25877 gen_generic_params_dies (t);
25879 generic_type_instances = NULL;
25883 /* Replace DW_AT_name for the decl with name. */
25885 static void
25886 dwarf2out_set_name (tree decl, tree name)
25888 dw_die_ref die;
25889 dw_attr_node *attr;
25890 const char *dname;
25892 die = TYPE_SYMTAB_DIE (decl);
25893 if (!die)
25894 return;
25896 dname = dwarf2_name (name, 0);
25897 if (!dname)
25898 return;
25900 attr = get_AT (die, DW_AT_name);
25901 if (attr)
25903 struct indirect_string_node *node;
25905 node = find_AT_string (dname);
25906 /* replace the string. */
25907 attr->dw_attr_val.v.val_str = node;
25910 else
25911 add_name_attribute (die, dname);
25914 /* True if before or during processing of the first function being emitted. */
25915 static bool in_first_function_p = true;
25916 /* True if loc_note during dwarf2out_var_location call might still be
25917 before first real instruction at address equal to .Ltext0. */
25918 static bool maybe_at_text_label_p = true;
25919 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
25920 static unsigned int first_loclabel_num_not_at_text_label;
25922 /* Called by the final INSN scan whenever we see a var location. We
25923 use it to drop labels in the right places, and throw the location in
25924 our lookup table. */
25926 static void
25927 dwarf2out_var_location (rtx_insn *loc_note)
25929 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
25930 struct var_loc_node *newloc;
25931 rtx_insn *next_real, *next_note;
25932 rtx_insn *call_insn = NULL;
25933 static const char *last_label;
25934 static const char *last_postcall_label;
25935 static bool last_in_cold_section_p;
25936 static rtx_insn *expected_next_loc_note;
25937 tree decl;
25938 bool var_loc_p;
25940 if (!NOTE_P (loc_note))
25942 if (CALL_P (loc_note))
25944 call_site_count++;
25945 if (SIBLING_CALL_P (loc_note))
25946 tail_call_site_count++;
25947 if (optimize == 0 && !flag_var_tracking)
25949 /* When the var-tracking pass is not running, there is no note
25950 for indirect calls whose target is compile-time known. In this
25951 case, process such calls specifically so that we generate call
25952 sites for them anyway. */
25953 rtx x = PATTERN (loc_note);
25954 if (GET_CODE (x) == PARALLEL)
25955 x = XVECEXP (x, 0, 0);
25956 if (GET_CODE (x) == SET)
25957 x = SET_SRC (x);
25958 if (GET_CODE (x) == CALL)
25959 x = XEXP (x, 0);
25960 if (!MEM_P (x)
25961 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
25962 || !SYMBOL_REF_DECL (XEXP (x, 0))
25963 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
25964 != FUNCTION_DECL))
25966 call_insn = loc_note;
25967 loc_note = NULL;
25968 var_loc_p = false;
25970 next_real = next_real_insn (call_insn);
25971 next_note = NULL;
25972 cached_next_real_insn = NULL;
25973 goto create_label;
25977 return;
25980 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
25981 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
25982 return;
25984 /* Optimize processing a large consecutive sequence of location
25985 notes so we don't spend too much time in next_real_insn. If the
25986 next insn is another location note, remember the next_real_insn
25987 calculation for next time. */
25988 next_real = cached_next_real_insn;
25989 if (next_real)
25991 if (expected_next_loc_note != loc_note)
25992 next_real = NULL;
25995 next_note = NEXT_INSN (loc_note);
25996 if (! next_note
25997 || next_note->deleted ()
25998 || ! NOTE_P (next_note)
25999 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26000 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26001 next_note = NULL;
26003 if (! next_real)
26004 next_real = next_real_insn (loc_note);
26006 if (next_note)
26008 expected_next_loc_note = next_note;
26009 cached_next_real_insn = next_real;
26011 else
26012 cached_next_real_insn = NULL;
26014 /* If there are no instructions which would be affected by this note,
26015 don't do anything. */
26016 if (var_loc_p
26017 && next_real == NULL_RTX
26018 && !NOTE_DURING_CALL_P (loc_note))
26019 return;
26021 create_label:
26023 if (next_real == NULL_RTX)
26024 next_real = get_last_insn ();
26026 /* If there were any real insns between note we processed last time
26027 and this note (or if it is the first note), clear
26028 last_{,postcall_}label so that they are not reused this time. */
26029 if (last_var_location_insn == NULL_RTX
26030 || last_var_location_insn != next_real
26031 || last_in_cold_section_p != in_cold_section_p)
26033 last_label = NULL;
26034 last_postcall_label = NULL;
26037 if (var_loc_p)
26039 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26040 newloc = add_var_loc_to_decl (decl, loc_note,
26041 NOTE_DURING_CALL_P (loc_note)
26042 ? last_postcall_label : last_label);
26043 if (newloc == NULL)
26044 return;
26046 else
26048 decl = NULL_TREE;
26049 newloc = NULL;
26052 /* If there were no real insns between note we processed last time
26053 and this note, use the label we emitted last time. Otherwise
26054 create a new label and emit it. */
26055 if (last_label == NULL)
26057 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26058 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26059 loclabel_num++;
26060 last_label = ggc_strdup (loclabel);
26061 /* See if loclabel might be equal to .Ltext0. If yes,
26062 bump first_loclabel_num_not_at_text_label. */
26063 if (!have_multiple_function_sections
26064 && in_first_function_p
26065 && maybe_at_text_label_p)
26067 static rtx_insn *last_start;
26068 rtx_insn *insn;
26069 for (insn = loc_note; insn; insn = previous_insn (insn))
26070 if (insn == last_start)
26071 break;
26072 else if (!NONDEBUG_INSN_P (insn))
26073 continue;
26074 else
26076 rtx body = PATTERN (insn);
26077 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26078 continue;
26079 /* Inline asm could occupy zero bytes. */
26080 else if (GET_CODE (body) == ASM_INPUT
26081 || asm_noperands (body) >= 0)
26082 continue;
26083 #ifdef HAVE_attr_length
26084 else if (get_attr_min_length (insn) == 0)
26085 continue;
26086 #endif
26087 else
26089 /* Assume insn has non-zero length. */
26090 maybe_at_text_label_p = false;
26091 break;
26094 if (maybe_at_text_label_p)
26096 last_start = loc_note;
26097 first_loclabel_num_not_at_text_label = loclabel_num;
26102 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26103 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26105 if (!var_loc_p)
26107 struct call_arg_loc_node *ca_loc
26108 = ggc_cleared_alloc<call_arg_loc_node> ();
26109 rtx_insn *prev
26110 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26112 ca_loc->call_arg_loc_note = loc_note;
26113 ca_loc->next = NULL;
26114 ca_loc->label = last_label;
26115 gcc_assert (prev
26116 && (CALL_P (prev)
26117 || (NONJUMP_INSN_P (prev)
26118 && GET_CODE (PATTERN (prev)) == SEQUENCE
26119 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26120 if (!CALL_P (prev))
26121 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26122 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26124 /* Look for a SYMBOL_REF in the "prev" instruction. */
26125 rtx x = get_call_rtx_from (PATTERN (prev));
26126 if (x)
26128 /* Try to get the call symbol, if any. */
26129 if (MEM_P (XEXP (x, 0)))
26130 x = XEXP (x, 0);
26131 /* First, look for a memory access to a symbol_ref. */
26132 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26133 && SYMBOL_REF_DECL (XEXP (x, 0))
26134 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26135 ca_loc->symbol_ref = XEXP (x, 0);
26136 /* Otherwise, look at a compile-time known user-level function
26137 declaration. */
26138 else if (MEM_P (x)
26139 && MEM_EXPR (x)
26140 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26141 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26144 ca_loc->block = insn_scope (prev);
26145 if (call_arg_locations)
26146 call_arg_loc_last->next = ca_loc;
26147 else
26148 call_arg_locations = ca_loc;
26149 call_arg_loc_last = ca_loc;
26151 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26152 newloc->label = last_label;
26153 else
26155 if (!last_postcall_label)
26157 sprintf (loclabel, "%s-1", last_label);
26158 last_postcall_label = ggc_strdup (loclabel);
26160 newloc->label = last_postcall_label;
26163 last_var_location_insn = next_real;
26164 last_in_cold_section_p = in_cold_section_p;
26167 /* Called from finalize_size_functions for size functions so that their body
26168 can be encoded in the debug info to describe the layout of variable-length
26169 structures. */
26171 static void
26172 dwarf2out_size_function (tree decl)
26174 function_to_dwarf_procedure (decl);
26177 /* Note in one location list that text section has changed. */
26180 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26182 var_loc_list *list = *slot;
26183 if (list->first)
26184 list->last_before_switch
26185 = list->last->next ? list->last->next : list->last;
26186 return 1;
26189 /* Note in all location lists that text section has changed. */
26191 static void
26192 var_location_switch_text_section (void)
26194 if (decl_loc_table == NULL)
26195 return;
26197 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26200 /* Create a new line number table. */
26202 static dw_line_info_table *
26203 new_line_info_table (void)
26205 dw_line_info_table *table;
26207 table = ggc_cleared_alloc<dw_line_info_table> ();
26208 table->file_num = 1;
26209 table->line_num = 1;
26210 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26212 return table;
26215 /* Lookup the "current" table into which we emit line info, so
26216 that we don't have to do it for every source line. */
26218 static void
26219 set_cur_line_info_table (section *sec)
26221 dw_line_info_table *table;
26223 if (sec == text_section)
26224 table = text_section_line_info;
26225 else if (sec == cold_text_section)
26227 table = cold_text_section_line_info;
26228 if (!table)
26230 cold_text_section_line_info = table = new_line_info_table ();
26231 table->end_label = cold_end_label;
26234 else
26236 const char *end_label;
26238 if (flag_reorder_blocks_and_partition)
26240 if (in_cold_section_p)
26241 end_label = crtl->subsections.cold_section_end_label;
26242 else
26243 end_label = crtl->subsections.hot_section_end_label;
26245 else
26247 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26248 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26249 current_function_funcdef_no);
26250 end_label = ggc_strdup (label);
26253 table = new_line_info_table ();
26254 table->end_label = end_label;
26256 vec_safe_push (separate_line_info, table);
26259 if (DWARF2_ASM_LINE_DEBUG_INFO)
26260 table->is_stmt = (cur_line_info_table
26261 ? cur_line_info_table->is_stmt
26262 : DWARF_LINE_DEFAULT_IS_STMT_START);
26263 cur_line_info_table = table;
26267 /* We need to reset the locations at the beginning of each
26268 function. We can't do this in the end_function hook, because the
26269 declarations that use the locations won't have been output when
26270 that hook is called. Also compute have_multiple_function_sections here. */
26272 static void
26273 dwarf2out_begin_function (tree fun)
26275 section *sec = function_section (fun);
26277 if (sec != text_section)
26278 have_multiple_function_sections = true;
26280 if (flag_reorder_blocks_and_partition && !cold_text_section)
26282 gcc_assert (current_function_decl == fun);
26283 cold_text_section = unlikely_text_section ();
26284 switch_to_section (cold_text_section);
26285 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26286 switch_to_section (sec);
26289 dwarf2out_note_section_used ();
26290 call_site_count = 0;
26291 tail_call_site_count = 0;
26293 set_cur_line_info_table (sec);
26296 /* Helper function of dwarf2out_end_function, called only after emitting
26297 the very first function into assembly. Check if some .debug_loc range
26298 might end with a .LVL* label that could be equal to .Ltext0.
26299 In that case we must force using absolute addresses in .debug_loc ranges,
26300 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26301 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26302 list terminator.
26303 Set have_multiple_function_sections to true in that case and
26304 terminate htab traversal. */
26307 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26309 var_loc_list *entry = *slot;
26310 struct var_loc_node *node;
26312 node = entry->first;
26313 if (node && node->next && node->next->label)
26315 unsigned int i;
26316 const char *label = node->next->label;
26317 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26319 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26321 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26322 if (strcmp (label, loclabel) == 0)
26324 have_multiple_function_sections = true;
26325 return 0;
26329 return 1;
26332 /* Hook called after emitting a function into assembly.
26333 This does something only for the very first function emitted. */
26335 static void
26336 dwarf2out_end_function (unsigned int)
26338 if (in_first_function_p
26339 && !have_multiple_function_sections
26340 && first_loclabel_num_not_at_text_label
26341 && decl_loc_table)
26342 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26343 in_first_function_p = false;
26344 maybe_at_text_label_p = false;
26347 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26348 front-ends register a translation unit even before dwarf2out_init is
26349 called. */
26350 static tree main_translation_unit = NULL_TREE;
26352 /* Hook called by front-ends after they built their main translation unit.
26353 Associate comp_unit_die to UNIT. */
26355 static void
26356 dwarf2out_register_main_translation_unit (tree unit)
26358 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26359 && main_translation_unit == NULL_TREE);
26360 main_translation_unit = unit;
26361 /* If dwarf2out_init has not been called yet, it will perform the association
26362 itself looking at main_translation_unit. */
26363 if (decl_die_table != NULL)
26364 equate_decl_number_to_die (unit, comp_unit_die ());
26367 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26369 static void
26370 push_dw_line_info_entry (dw_line_info_table *table,
26371 enum dw_line_info_opcode opcode, unsigned int val)
26373 dw_line_info_entry e;
26374 e.opcode = opcode;
26375 e.val = val;
26376 vec_safe_push (table->entries, e);
26379 /* Output a label to mark the beginning of a source code line entry
26380 and record information relating to this source line, in
26381 'line_info_table' for later output of the .debug_line section. */
26382 /* ??? The discriminator parameter ought to be unsigned. */
26384 static void
26385 dwarf2out_source_line (unsigned int line, const char *filename,
26386 int discriminator, bool is_stmt)
26388 unsigned int file_num;
26389 dw_line_info_table *table;
26391 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26392 return;
26394 /* The discriminator column was added in dwarf4. Simplify the below
26395 by simply removing it if we're not supposed to output it. */
26396 if (dwarf_version < 4 && dwarf_strict)
26397 discriminator = 0;
26399 table = cur_line_info_table;
26400 file_num = maybe_emit_file (lookup_filename (filename));
26402 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26403 the debugger has used the second (possibly duplicate) line number
26404 at the beginning of the function to mark the end of the prologue.
26405 We could eliminate any other duplicates within the function. For
26406 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26407 that second line number entry. */
26408 /* Recall that this end-of-prologue indication is *not* the same thing
26409 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26410 to which the hook corresponds, follows the last insn that was
26411 emitted by gen_prologue. What we need is to precede the first insn
26412 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26413 insn that corresponds to something the user wrote. These may be
26414 very different locations once scheduling is enabled. */
26416 if (0 && file_num == table->file_num
26417 && line == table->line_num
26418 && discriminator == table->discrim_num
26419 && is_stmt == table->is_stmt)
26420 return;
26422 switch_to_section (current_function_section ());
26424 /* If requested, emit something human-readable. */
26425 if (flag_debug_asm)
26426 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
26428 if (DWARF2_ASM_LINE_DEBUG_INFO)
26430 /* Emit the .loc directive understood by GNU as. */
26431 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26432 file_num, line, is_stmt, discriminator */
26433 fputs ("\t.loc ", asm_out_file);
26434 fprint_ul (asm_out_file, file_num);
26435 putc (' ', asm_out_file);
26436 fprint_ul (asm_out_file, line);
26437 putc (' ', asm_out_file);
26438 putc ('0', asm_out_file);
26440 if (is_stmt != table->is_stmt)
26442 fputs (" is_stmt ", asm_out_file);
26443 putc (is_stmt ? '1' : '0', asm_out_file);
26445 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26447 gcc_assert (discriminator > 0);
26448 fputs (" discriminator ", asm_out_file);
26449 fprint_ul (asm_out_file, (unsigned long) discriminator);
26451 putc ('\n', asm_out_file);
26453 else
26455 unsigned int label_num = ++line_info_label_num;
26457 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26459 push_dw_line_info_entry (table, LI_set_address, label_num);
26460 if (file_num != table->file_num)
26461 push_dw_line_info_entry (table, LI_set_file, file_num);
26462 if (discriminator != table->discrim_num)
26463 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26464 if (is_stmt != table->is_stmt)
26465 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26466 push_dw_line_info_entry (table, LI_set_line, line);
26469 table->file_num = file_num;
26470 table->line_num = line;
26471 table->discrim_num = discriminator;
26472 table->is_stmt = is_stmt;
26473 table->in_use = true;
26476 /* Record the beginning of a new source file. */
26478 static void
26479 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26481 if (flag_eliminate_dwarf2_dups)
26483 /* Record the beginning of the file for break_out_includes. */
26484 dw_die_ref bincl_die;
26486 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26487 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26490 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26492 macinfo_entry e;
26493 e.code = DW_MACINFO_start_file;
26494 e.lineno = lineno;
26495 e.info = ggc_strdup (filename);
26496 vec_safe_push (macinfo_table, e);
26500 /* Record the end of a source file. */
26502 static void
26503 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26505 if (flag_eliminate_dwarf2_dups)
26506 /* Record the end of the file for break_out_includes. */
26507 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26509 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26511 macinfo_entry e;
26512 e.code = DW_MACINFO_end_file;
26513 e.lineno = lineno;
26514 e.info = NULL;
26515 vec_safe_push (macinfo_table, e);
26519 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26520 the tail part of the directive line, i.e. the part which is past the
26521 initial whitespace, #, whitespace, directive-name, whitespace part. */
26523 static void
26524 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26525 const char *buffer ATTRIBUTE_UNUSED)
26527 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26529 macinfo_entry e;
26530 /* Insert a dummy first entry to be able to optimize the whole
26531 predefined macro block using DW_MACRO_import. */
26532 if (macinfo_table->is_empty () && lineno <= 1)
26534 e.code = 0;
26535 e.lineno = 0;
26536 e.info = NULL;
26537 vec_safe_push (macinfo_table, e);
26539 e.code = DW_MACINFO_define;
26540 e.lineno = lineno;
26541 e.info = ggc_strdup (buffer);
26542 vec_safe_push (macinfo_table, e);
26546 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26547 the tail part of the directive line, i.e. the part which is past the
26548 initial whitespace, #, whitespace, directive-name, whitespace part. */
26550 static void
26551 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26552 const char *buffer ATTRIBUTE_UNUSED)
26554 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26556 macinfo_entry e;
26557 /* Insert a dummy first entry to be able to optimize the whole
26558 predefined macro block using DW_MACRO_import. */
26559 if (macinfo_table->is_empty () && lineno <= 1)
26561 e.code = 0;
26562 e.lineno = 0;
26563 e.info = NULL;
26564 vec_safe_push (macinfo_table, e);
26566 e.code = DW_MACINFO_undef;
26567 e.lineno = lineno;
26568 e.info = ggc_strdup (buffer);
26569 vec_safe_push (macinfo_table, e);
26573 /* Helpers to manipulate hash table of CUs. */
26575 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26577 static inline hashval_t hash (const macinfo_entry *);
26578 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26581 inline hashval_t
26582 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26584 return htab_hash_string (entry->info);
26587 inline bool
26588 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26589 const macinfo_entry *entry2)
26591 return !strcmp (entry1->info, entry2->info);
26594 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26596 /* Output a single .debug_macinfo entry. */
26598 static void
26599 output_macinfo_op (macinfo_entry *ref)
26601 int file_num;
26602 size_t len;
26603 struct indirect_string_node *node;
26604 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26605 struct dwarf_file_data *fd;
26607 switch (ref->code)
26609 case DW_MACINFO_start_file:
26610 fd = lookup_filename (ref->info);
26611 file_num = maybe_emit_file (fd);
26612 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26613 dw2_asm_output_data_uleb128 (ref->lineno,
26614 "Included from line number %lu",
26615 (unsigned long) ref->lineno);
26616 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26617 break;
26618 case DW_MACINFO_end_file:
26619 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26620 break;
26621 case DW_MACINFO_define:
26622 case DW_MACINFO_undef:
26623 len = strlen (ref->info) + 1;
26624 if (!dwarf_strict
26625 && len > DWARF_OFFSET_SIZE
26626 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26627 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26629 ref->code = ref->code == DW_MACINFO_define
26630 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26631 output_macinfo_op (ref);
26632 return;
26634 dw2_asm_output_data (1, ref->code,
26635 ref->code == DW_MACINFO_define
26636 ? "Define macro" : "Undefine macro");
26637 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26638 (unsigned long) ref->lineno);
26639 dw2_asm_output_nstring (ref->info, -1, "The macro");
26640 break;
26641 case DW_MACRO_define_strp:
26642 case DW_MACRO_undef_strp:
26643 node = find_AT_string (ref->info);
26644 gcc_assert (node
26645 && (node->form == DW_FORM_strp
26646 || node->form == DW_FORM_GNU_str_index));
26647 dw2_asm_output_data (1, ref->code,
26648 ref->code == DW_MACRO_define_strp
26649 ? "Define macro strp"
26650 : "Undefine macro strp");
26651 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26652 (unsigned long) ref->lineno);
26653 if (node->form == DW_FORM_strp)
26654 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26655 debug_str_section, "The macro: \"%s\"",
26656 ref->info);
26657 else
26658 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26659 ref->info);
26660 break;
26661 case DW_MACRO_import:
26662 dw2_asm_output_data (1, ref->code, "Import");
26663 ASM_GENERATE_INTERNAL_LABEL (label,
26664 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26665 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26666 break;
26667 default:
26668 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26669 ASM_COMMENT_START, (unsigned long) ref->code);
26670 break;
26674 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26675 other compilation unit .debug_macinfo sections. IDX is the first
26676 index of a define/undef, return the number of ops that should be
26677 emitted in a comdat .debug_macinfo section and emit
26678 a DW_MACRO_import entry referencing it.
26679 If the define/undef entry should be emitted normally, return 0. */
26681 static unsigned
26682 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26683 macinfo_hash_type **macinfo_htab)
26685 macinfo_entry *first, *second, *cur, *inc;
26686 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26687 unsigned char checksum[16];
26688 struct md5_ctx ctx;
26689 char *grp_name, *tail;
26690 const char *base;
26691 unsigned int i, count, encoded_filename_len, linebuf_len;
26692 macinfo_entry **slot;
26694 first = &(*macinfo_table)[idx];
26695 second = &(*macinfo_table)[idx + 1];
26697 /* Optimize only if there are at least two consecutive define/undef ops,
26698 and either all of them are before first DW_MACINFO_start_file
26699 with lineno {0,1} (i.e. predefined macro block), or all of them are
26700 in some included header file. */
26701 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26702 return 0;
26703 if (vec_safe_is_empty (files))
26705 if (first->lineno > 1 || second->lineno > 1)
26706 return 0;
26708 else if (first->lineno == 0)
26709 return 0;
26711 /* Find the last define/undef entry that can be grouped together
26712 with first and at the same time compute md5 checksum of their
26713 codes, linenumbers and strings. */
26714 md5_init_ctx (&ctx);
26715 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26716 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26717 break;
26718 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26719 break;
26720 else
26722 unsigned char code = cur->code;
26723 md5_process_bytes (&code, 1, &ctx);
26724 checksum_uleb128 (cur->lineno, &ctx);
26725 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26727 md5_finish_ctx (&ctx, checksum);
26728 count = i - idx;
26730 /* From the containing include filename (if any) pick up just
26731 usable characters from its basename. */
26732 if (vec_safe_is_empty (files))
26733 base = "";
26734 else
26735 base = lbasename (files->last ().info);
26736 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26737 if (ISIDNUM (base[i]) || base[i] == '.')
26738 encoded_filename_len++;
26739 /* Count . at the end. */
26740 if (encoded_filename_len)
26741 encoded_filename_len++;
26743 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26744 linebuf_len = strlen (linebuf);
26746 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26747 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26748 + 16 * 2 + 1);
26749 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26750 tail = grp_name + 4;
26751 if (encoded_filename_len)
26753 for (i = 0; base[i]; i++)
26754 if (ISIDNUM (base[i]) || base[i] == '.')
26755 *tail++ = base[i];
26756 *tail++ = '.';
26758 memcpy (tail, linebuf, linebuf_len);
26759 tail += linebuf_len;
26760 *tail++ = '.';
26761 for (i = 0; i < 16; i++)
26762 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26764 /* Construct a macinfo_entry for DW_MACRO_import
26765 in the empty vector entry before the first define/undef. */
26766 inc = &(*macinfo_table)[idx - 1];
26767 inc->code = DW_MACRO_import;
26768 inc->lineno = 0;
26769 inc->info = ggc_strdup (grp_name);
26770 if (!*macinfo_htab)
26771 *macinfo_htab = new macinfo_hash_type (10);
26772 /* Avoid emitting duplicates. */
26773 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26774 if (*slot != NULL)
26776 inc->code = 0;
26777 inc->info = NULL;
26778 /* If such an entry has been used before, just emit
26779 a DW_MACRO_import op. */
26780 inc = *slot;
26781 output_macinfo_op (inc);
26782 /* And clear all macinfo_entry in the range to avoid emitting them
26783 in the second pass. */
26784 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26786 cur->code = 0;
26787 cur->info = NULL;
26790 else
26792 *slot = inc;
26793 inc->lineno = (*macinfo_htab)->elements ();
26794 output_macinfo_op (inc);
26796 return count;
26799 /* Save any strings needed by the macinfo table in the debug str
26800 table. All strings must be collected into the table by the time
26801 index_string is called. */
26803 static void
26804 save_macinfo_strings (void)
26806 unsigned len;
26807 unsigned i;
26808 macinfo_entry *ref;
26810 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26812 switch (ref->code)
26814 /* Match the logic in output_macinfo_op to decide on
26815 indirect strings. */
26816 case DW_MACINFO_define:
26817 case DW_MACINFO_undef:
26818 len = strlen (ref->info) + 1;
26819 if (!dwarf_strict
26820 && len > DWARF_OFFSET_SIZE
26821 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26822 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26823 set_indirect_string (find_AT_string (ref->info));
26824 break;
26825 case DW_MACRO_define_strp:
26826 case DW_MACRO_undef_strp:
26827 set_indirect_string (find_AT_string (ref->info));
26828 break;
26829 default:
26830 break;
26835 /* Output macinfo section(s). */
26837 static void
26838 output_macinfo (void)
26840 unsigned i;
26841 unsigned long length = vec_safe_length (macinfo_table);
26842 macinfo_entry *ref;
26843 vec<macinfo_entry, va_gc> *files = NULL;
26844 macinfo_hash_type *macinfo_htab = NULL;
26846 if (! length)
26847 return;
26849 /* output_macinfo* uses these interchangeably. */
26850 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
26851 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
26852 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
26853 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
26855 /* For .debug_macro emit the section header. */
26856 if (!dwarf_strict || dwarf_version >= 5)
26858 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26859 "DWARF macro version number");
26860 if (DWARF_OFFSET_SIZE == 8)
26861 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
26862 else
26863 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
26864 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
26865 (!dwarf_split_debug_info ? debug_line_section_label
26866 : debug_skeleton_line_section_label),
26867 debug_line_section, NULL);
26870 /* In the first loop, it emits the primary .debug_macinfo section
26871 and after each emitted op the macinfo_entry is cleared.
26872 If a longer range of define/undef ops can be optimized using
26873 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
26874 the vector before the first define/undef in the range and the
26875 whole range of define/undef ops is not emitted and kept. */
26876 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26878 switch (ref->code)
26880 case DW_MACINFO_start_file:
26881 vec_safe_push (files, *ref);
26882 break;
26883 case DW_MACINFO_end_file:
26884 if (!vec_safe_is_empty (files))
26885 files->pop ();
26886 break;
26887 case DW_MACINFO_define:
26888 case DW_MACINFO_undef:
26889 if ((!dwarf_strict || dwarf_version >= 5)
26890 && HAVE_COMDAT_GROUP
26891 && vec_safe_length (files) != 1
26892 && i > 0
26893 && i + 1 < length
26894 && (*macinfo_table)[i - 1].code == 0)
26896 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
26897 if (count)
26899 i += count - 1;
26900 continue;
26903 break;
26904 case 0:
26905 /* A dummy entry may be inserted at the beginning to be able
26906 to optimize the whole block of predefined macros. */
26907 if (i == 0)
26908 continue;
26909 default:
26910 break;
26912 output_macinfo_op (ref);
26913 ref->info = NULL;
26914 ref->code = 0;
26917 if (!macinfo_htab)
26918 return;
26920 delete macinfo_htab;
26921 macinfo_htab = NULL;
26923 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
26924 terminate the current chain and switch to a new comdat .debug_macinfo
26925 section and emit the define/undef entries within it. */
26926 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26927 switch (ref->code)
26929 case 0:
26930 continue;
26931 case DW_MACRO_import:
26933 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26934 tree comdat_key = get_identifier (ref->info);
26935 /* Terminate the previous .debug_macinfo section. */
26936 dw2_asm_output_data (1, 0, "End compilation unit");
26937 targetm.asm_out.named_section (debug_macinfo_section_name,
26938 SECTION_DEBUG
26939 | SECTION_LINKONCE,
26940 comdat_key);
26941 ASM_GENERATE_INTERNAL_LABEL (label,
26942 DEBUG_MACRO_SECTION_LABEL,
26943 ref->lineno);
26944 ASM_OUTPUT_LABEL (asm_out_file, label);
26945 ref->code = 0;
26946 ref->info = NULL;
26947 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26948 "DWARF macro version number");
26949 if (DWARF_OFFSET_SIZE == 8)
26950 dw2_asm_output_data (1, 1, "Flags: 64-bit");
26951 else
26952 dw2_asm_output_data (1, 0, "Flags: 32-bit");
26954 break;
26955 case DW_MACINFO_define:
26956 case DW_MACINFO_undef:
26957 output_macinfo_op (ref);
26958 ref->code = 0;
26959 ref->info = NULL;
26960 break;
26961 default:
26962 gcc_unreachable ();
26966 /* Initialize the various sections and labels for dwarf output. */
26968 static void
26969 init_sections_and_labels (void)
26971 if (!dwarf_split_debug_info)
26973 debug_info_section = get_section (DEBUG_INFO_SECTION,
26974 SECTION_DEBUG, NULL);
26975 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
26976 SECTION_DEBUG, NULL);
26977 debug_loc_section = get_section (dwarf_version >= 5
26978 ? DEBUG_LOCLISTS_SECTION
26979 : DEBUG_LOC_SECTION,
26980 SECTION_DEBUG, NULL);
26981 debug_macinfo_section_name
26982 = (dwarf_strict && dwarf_version < 5)
26983 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
26984 debug_macinfo_section = get_section (debug_macinfo_section_name,
26985 SECTION_DEBUG, NULL);
26987 else
26989 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
26990 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
26991 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
26992 SECTION_DEBUG | SECTION_EXCLUDE,
26993 NULL);
26994 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
26995 SECTION_DEBUG, NULL);
26996 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
26997 SECTION_DEBUG, NULL);
26998 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
26999 SECTION_DEBUG, NULL);
27000 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27001 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27003 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27004 the main .o, but the skeleton_line goes into the split off dwo. */
27005 debug_skeleton_line_section
27006 = get_section (DEBUG_DWO_LINE_SECTION,
27007 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27008 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27009 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27010 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27011 SECTION_DEBUG | SECTION_EXCLUDE,
27012 NULL);
27013 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27014 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27015 debug_loc_section = get_section (dwarf_version >= 5
27016 ? DEBUG_DWO_LOCLISTS_SECTION
27017 : DEBUG_DWO_LOC_SECTION,
27018 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27019 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27020 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27021 debug_macinfo_section_name
27022 = (dwarf_strict && dwarf_version < 5)
27023 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27024 debug_macinfo_section = get_section (debug_macinfo_section_name,
27025 SECTION_DEBUG | SECTION_EXCLUDE,
27026 NULL);
27028 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27029 SECTION_DEBUG, NULL);
27030 debug_line_section = get_section (DEBUG_LINE_SECTION,
27031 SECTION_DEBUG, NULL);
27032 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27033 SECTION_DEBUG, NULL);
27034 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27035 SECTION_DEBUG, NULL);
27036 debug_str_section = get_section (DEBUG_STR_SECTION,
27037 DEBUG_STR_SECTION_FLAGS, NULL);
27038 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27039 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27040 DEBUG_STR_SECTION_FLAGS, NULL);
27042 debug_ranges_section = get_section (dwarf_version >= 5
27043 ? DEBUG_RNGLISTS_SECTION
27044 : DEBUG_RANGES_SECTION,
27045 SECTION_DEBUG, NULL);
27046 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27047 SECTION_DEBUG, NULL);
27049 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27050 DEBUG_ABBREV_SECTION_LABEL, 0);
27051 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27052 DEBUG_INFO_SECTION_LABEL, 0);
27053 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27054 DEBUG_LINE_SECTION_LABEL, 0);
27055 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27056 DEBUG_RANGES_SECTION_LABEL, 0);
27057 if (dwarf_version >= 5 && dwarf_split_debug_info)
27058 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27059 DEBUG_RANGES_SECTION_LABEL, 1);
27060 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27061 DEBUG_ADDR_SECTION_LABEL, 0);
27062 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27063 (dwarf_strict && dwarf_version < 5)
27064 ? DEBUG_MACINFO_SECTION_LABEL
27065 : DEBUG_MACRO_SECTION_LABEL, 0);
27066 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27069 /* Set up for Dwarf output at the start of compilation. */
27071 static void
27072 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27074 /* This option is currently broken, see (PR53118 and PR46102). */
27075 if (flag_eliminate_dwarf2_dups
27076 && strstr (lang_hooks.name, "C++"))
27078 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27079 flag_eliminate_dwarf2_dups = 0;
27082 /* Allocate the file_table. */
27083 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27085 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27086 /* Allocate the decl_die_table. */
27087 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27089 /* Allocate the decl_loc_table. */
27090 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27092 /* Allocate the cached_dw_loc_list_table. */
27093 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27095 /* Allocate the initial hunk of the decl_scope_table. */
27096 vec_alloc (decl_scope_table, 256);
27098 /* Allocate the initial hunk of the abbrev_die_table. */
27099 vec_alloc (abbrev_die_table, 256);
27100 /* Zero-th entry is allocated, but unused. */
27101 abbrev_die_table->quick_push (NULL);
27103 /* Allocate the dwarf_proc_stack_usage_map. */
27104 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27106 /* Allocate the pubtypes and pubnames vectors. */
27107 vec_alloc (pubname_table, 32);
27108 vec_alloc (pubtype_table, 32);
27110 vec_alloc (incomplete_types, 64);
27112 vec_alloc (used_rtx_array, 32);
27114 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27115 vec_alloc (macinfo_table, 64);
27116 #endif
27118 /* If front-ends already registered a main translation unit but we were not
27119 ready to perform the association, do this now. */
27120 if (main_translation_unit != NULL_TREE)
27121 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27124 /* Called before compile () starts outputtting functions, variables
27125 and toplevel asms into assembly. */
27127 static void
27128 dwarf2out_assembly_start (void)
27130 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27131 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27132 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27133 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27134 COLD_TEXT_SECTION_LABEL, 0);
27135 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27137 switch_to_section (text_section);
27138 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27139 #endif
27141 /* Make sure the line number table for .text always exists. */
27142 text_section_line_info = new_line_info_table ();
27143 text_section_line_info->end_label = text_end_label;
27145 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27146 cur_line_info_table = text_section_line_info;
27147 #endif
27149 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27150 && dwarf2out_do_cfi_asm ()
27151 && (!(flag_unwind_tables || flag_exceptions)
27152 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27153 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27156 /* A helper function for dwarf2out_finish called through
27157 htab_traverse. Assign a string its index. All strings must be
27158 collected into the table by the time index_string is called,
27159 because the indexing code relies on htab_traverse to traverse nodes
27160 in the same order for each run. */
27163 index_string (indirect_string_node **h, unsigned int *index)
27165 indirect_string_node *node = *h;
27167 find_string_form (node);
27168 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27170 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27171 node->index = *index;
27172 *index += 1;
27174 return 1;
27177 /* A helper function for output_indirect_strings called through
27178 htab_traverse. Output the offset to a string and update the
27179 current offset. */
27182 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27184 indirect_string_node *node = *h;
27186 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27188 /* Assert that this node has been assigned an index. */
27189 gcc_assert (node->index != NO_INDEX_ASSIGNED
27190 && node->index != NOT_INDEXED);
27191 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27192 "indexed string 0x%x: %s", node->index, node->str);
27193 *offset += strlen (node->str) + 1;
27195 return 1;
27198 /* A helper function for dwarf2out_finish called through
27199 htab_traverse. Output the indexed string. */
27202 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27204 struct indirect_string_node *node = *h;
27206 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27208 /* Assert that the strings are output in the same order as their
27209 indexes were assigned. */
27210 gcc_assert (*cur_idx == node->index);
27211 assemble_string (node->str, strlen (node->str) + 1);
27212 *cur_idx += 1;
27214 return 1;
27217 /* A helper function for dwarf2out_finish called through
27218 htab_traverse. Emit one queued .debug_str string. */
27221 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27223 struct indirect_string_node *node = *h;
27225 node->form = find_string_form (node);
27226 if (node->form == form && node->refcount > 0)
27228 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27229 assemble_string (node->str, strlen (node->str) + 1);
27232 return 1;
27235 /* Output the indexed string table. */
27237 static void
27238 output_indirect_strings (void)
27240 switch_to_section (debug_str_section);
27241 if (!dwarf_split_debug_info)
27242 debug_str_hash->traverse<enum dwarf_form,
27243 output_indirect_string> (DW_FORM_strp);
27244 else
27246 unsigned int offset = 0;
27247 unsigned int cur_idx = 0;
27249 skeleton_debug_str_hash->traverse<enum dwarf_form,
27250 output_indirect_string> (DW_FORM_strp);
27252 switch_to_section (debug_str_offsets_section);
27253 debug_str_hash->traverse_noresize
27254 <unsigned int *, output_index_string_offset> (&offset);
27255 switch_to_section (debug_str_dwo_section);
27256 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27257 (&cur_idx);
27261 /* Callback for htab_traverse to assign an index to an entry in the
27262 table, and to write that entry to the .debug_addr section. */
27265 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27267 addr_table_entry *entry = *slot;
27269 if (entry->refcount == 0)
27271 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27272 || entry->index == NOT_INDEXED);
27273 return 1;
27276 gcc_assert (entry->index == *cur_index);
27277 (*cur_index)++;
27279 switch (entry->kind)
27281 case ate_kind_rtx:
27282 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27283 "0x%x", entry->index);
27284 break;
27285 case ate_kind_rtx_dtprel:
27286 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27287 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27288 DWARF2_ADDR_SIZE,
27289 entry->addr.rtl);
27290 fputc ('\n', asm_out_file);
27291 break;
27292 case ate_kind_label:
27293 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27294 "0x%x", entry->index);
27295 break;
27296 default:
27297 gcc_unreachable ();
27299 return 1;
27302 /* Produce the .debug_addr section. */
27304 static void
27305 output_addr_table (void)
27307 unsigned int index = 0;
27308 if (addr_index_table == NULL || addr_index_table->size () == 0)
27309 return;
27311 switch_to_section (debug_addr_section);
27312 addr_index_table
27313 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27316 #if ENABLE_ASSERT_CHECKING
27317 /* Verify that all marks are clear. */
27319 static void
27320 verify_marks_clear (dw_die_ref die)
27322 dw_die_ref c;
27324 gcc_assert (! die->die_mark);
27325 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27327 #endif /* ENABLE_ASSERT_CHECKING */
27329 /* Clear the marks for a die and its children.
27330 Be cool if the mark isn't set. */
27332 static void
27333 prune_unmark_dies (dw_die_ref die)
27335 dw_die_ref c;
27337 if (die->die_mark)
27338 die->die_mark = 0;
27339 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27342 /* Given LOC that is referenced by a DIE we're marking as used, find all
27343 referenced DWARF procedures it references and mark them as used. */
27345 static void
27346 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27348 for (; loc != NULL; loc = loc->dw_loc_next)
27349 switch (loc->dw_loc_opc)
27351 case DW_OP_implicit_pointer:
27352 case DW_OP_convert:
27353 case DW_OP_reinterpret:
27354 case DW_OP_GNU_implicit_pointer:
27355 case DW_OP_GNU_convert:
27356 case DW_OP_GNU_reinterpret:
27357 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27358 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27359 break;
27360 case DW_OP_call2:
27361 case DW_OP_call4:
27362 case DW_OP_call_ref:
27363 case DW_OP_const_type:
27364 case DW_OP_GNU_const_type:
27365 case DW_OP_GNU_parameter_ref:
27366 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27367 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27368 break;
27369 case DW_OP_regval_type:
27370 case DW_OP_deref_type:
27371 case DW_OP_GNU_regval_type:
27372 case DW_OP_GNU_deref_type:
27373 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27374 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27375 break;
27376 case DW_OP_entry_value:
27377 case DW_OP_GNU_entry_value:
27378 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27379 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27380 break;
27381 default:
27382 break;
27386 /* Given DIE that we're marking as used, find any other dies
27387 it references as attributes and mark them as used. */
27389 static void
27390 prune_unused_types_walk_attribs (dw_die_ref die)
27392 dw_attr_node *a;
27393 unsigned ix;
27395 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27397 switch (AT_class (a))
27399 /* Make sure DWARF procedures referenced by location descriptions will
27400 get emitted. */
27401 case dw_val_class_loc:
27402 prune_unused_types_walk_loc_descr (AT_loc (a));
27403 break;
27404 case dw_val_class_loc_list:
27405 for (dw_loc_list_ref list = AT_loc_list (a);
27406 list != NULL;
27407 list = list->dw_loc_next)
27408 prune_unused_types_walk_loc_descr (list->expr);
27409 break;
27411 case dw_val_class_die_ref:
27412 /* A reference to another DIE.
27413 Make sure that it will get emitted.
27414 If it was broken out into a comdat group, don't follow it. */
27415 if (! AT_ref (a)->comdat_type_p
27416 || a->dw_attr == DW_AT_specification)
27417 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27418 break;
27420 case dw_val_class_str:
27421 /* Set the string's refcount to 0 so that prune_unused_types_mark
27422 accounts properly for it. */
27423 a->dw_attr_val.v.val_str->refcount = 0;
27424 break;
27426 default:
27427 break;
27432 /* Mark the generic parameters and arguments children DIEs of DIE. */
27434 static void
27435 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27437 dw_die_ref c;
27439 if (die == NULL || die->die_child == NULL)
27440 return;
27441 c = die->die_child;
27444 if (is_template_parameter (c))
27445 prune_unused_types_mark (c, 1);
27446 c = c->die_sib;
27447 } while (c && c != die->die_child);
27450 /* Mark DIE as being used. If DOKIDS is true, then walk down
27451 to DIE's children. */
27453 static void
27454 prune_unused_types_mark (dw_die_ref die, int dokids)
27456 dw_die_ref c;
27458 if (die->die_mark == 0)
27460 /* We haven't done this node yet. Mark it as used. */
27461 die->die_mark = 1;
27462 /* If this is the DIE of a generic type instantiation,
27463 mark the children DIEs that describe its generic parms and
27464 args. */
27465 prune_unused_types_mark_generic_parms_dies (die);
27467 /* We also have to mark its parents as used.
27468 (But we don't want to mark our parent's kids due to this,
27469 unless it is a class.) */
27470 if (die->die_parent)
27471 prune_unused_types_mark (die->die_parent,
27472 class_scope_p (die->die_parent));
27474 /* Mark any referenced nodes. */
27475 prune_unused_types_walk_attribs (die);
27477 /* If this node is a specification,
27478 also mark the definition, if it exists. */
27479 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27480 prune_unused_types_mark (die->die_definition, 1);
27483 if (dokids && die->die_mark != 2)
27485 /* We need to walk the children, but haven't done so yet.
27486 Remember that we've walked the kids. */
27487 die->die_mark = 2;
27489 /* If this is an array type, we need to make sure our
27490 kids get marked, even if they're types. If we're
27491 breaking out types into comdat sections, do this
27492 for all type definitions. */
27493 if (die->die_tag == DW_TAG_array_type
27494 || (use_debug_types
27495 && is_type_die (die) && ! is_declaration_die (die)))
27496 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27497 else
27498 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27502 /* For local classes, look if any static member functions were emitted
27503 and if so, mark them. */
27505 static void
27506 prune_unused_types_walk_local_classes (dw_die_ref die)
27508 dw_die_ref c;
27510 if (die->die_mark == 2)
27511 return;
27513 switch (die->die_tag)
27515 case DW_TAG_structure_type:
27516 case DW_TAG_union_type:
27517 case DW_TAG_class_type:
27518 break;
27520 case DW_TAG_subprogram:
27521 if (!get_AT_flag (die, DW_AT_declaration)
27522 || die->die_definition != NULL)
27523 prune_unused_types_mark (die, 1);
27524 return;
27526 default:
27527 return;
27530 /* Mark children. */
27531 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27534 /* Walk the tree DIE and mark types that we actually use. */
27536 static void
27537 prune_unused_types_walk (dw_die_ref die)
27539 dw_die_ref c;
27541 /* Don't do anything if this node is already marked and
27542 children have been marked as well. */
27543 if (die->die_mark == 2)
27544 return;
27546 switch (die->die_tag)
27548 case DW_TAG_structure_type:
27549 case DW_TAG_union_type:
27550 case DW_TAG_class_type:
27551 if (die->die_perennial_p)
27552 break;
27554 for (c = die->die_parent; c; c = c->die_parent)
27555 if (c->die_tag == DW_TAG_subprogram)
27556 break;
27558 /* Finding used static member functions inside of classes
27559 is needed just for local classes, because for other classes
27560 static member function DIEs with DW_AT_specification
27561 are emitted outside of the DW_TAG_*_type. If we ever change
27562 it, we'd need to call this even for non-local classes. */
27563 if (c)
27564 prune_unused_types_walk_local_classes (die);
27566 /* It's a type node --- don't mark it. */
27567 return;
27569 case DW_TAG_const_type:
27570 case DW_TAG_packed_type:
27571 case DW_TAG_pointer_type:
27572 case DW_TAG_reference_type:
27573 case DW_TAG_rvalue_reference_type:
27574 case DW_TAG_volatile_type:
27575 case DW_TAG_typedef:
27576 case DW_TAG_array_type:
27577 case DW_TAG_interface_type:
27578 case DW_TAG_friend:
27579 case DW_TAG_enumeration_type:
27580 case DW_TAG_subroutine_type:
27581 case DW_TAG_string_type:
27582 case DW_TAG_set_type:
27583 case DW_TAG_subrange_type:
27584 case DW_TAG_ptr_to_member_type:
27585 case DW_TAG_file_type:
27586 /* Type nodes are useful only when other DIEs reference them --- don't
27587 mark them. */
27588 /* FALLTHROUGH */
27590 case DW_TAG_dwarf_procedure:
27591 /* Likewise for DWARF procedures. */
27593 if (die->die_perennial_p)
27594 break;
27596 return;
27598 default:
27599 /* Mark everything else. */
27600 break;
27603 if (die->die_mark == 0)
27605 die->die_mark = 1;
27607 /* Now, mark any dies referenced from here. */
27608 prune_unused_types_walk_attribs (die);
27611 die->die_mark = 2;
27613 /* Mark children. */
27614 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27617 /* Increment the string counts on strings referred to from DIE's
27618 attributes. */
27620 static void
27621 prune_unused_types_update_strings (dw_die_ref die)
27623 dw_attr_node *a;
27624 unsigned ix;
27626 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27627 if (AT_class (a) == dw_val_class_str)
27629 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27630 s->refcount++;
27631 /* Avoid unnecessarily putting strings that are used less than
27632 twice in the hash table. */
27633 if (s->refcount
27634 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27636 indirect_string_node **slot
27637 = debug_str_hash->find_slot_with_hash (s->str,
27638 htab_hash_string (s->str),
27639 INSERT);
27640 gcc_assert (*slot == NULL);
27641 *slot = s;
27646 /* Mark DIE and its children as removed. */
27648 static void
27649 mark_removed (dw_die_ref die)
27651 dw_die_ref c;
27652 die->removed = true;
27653 FOR_EACH_CHILD (die, c, mark_removed (c));
27656 /* Remove from the tree DIE any dies that aren't marked. */
27658 static void
27659 prune_unused_types_prune (dw_die_ref die)
27661 dw_die_ref c;
27663 gcc_assert (die->die_mark);
27664 prune_unused_types_update_strings (die);
27666 if (! die->die_child)
27667 return;
27669 c = die->die_child;
27670 do {
27671 dw_die_ref prev = c, next;
27672 for (c = c->die_sib; ! c->die_mark; c = next)
27673 if (c == die->die_child)
27675 /* No marked children between 'prev' and the end of the list. */
27676 if (prev == c)
27677 /* No marked children at all. */
27678 die->die_child = NULL;
27679 else
27681 prev->die_sib = c->die_sib;
27682 die->die_child = prev;
27684 c->die_sib = NULL;
27685 mark_removed (c);
27686 return;
27688 else
27690 next = c->die_sib;
27691 c->die_sib = NULL;
27692 mark_removed (c);
27695 if (c != prev->die_sib)
27696 prev->die_sib = c;
27697 prune_unused_types_prune (c);
27698 } while (c != die->die_child);
27701 /* Remove dies representing declarations that we never use. */
27703 static void
27704 prune_unused_types (void)
27706 unsigned int i;
27707 limbo_die_node *node;
27708 comdat_type_node *ctnode;
27709 pubname_entry *pub;
27710 dw_die_ref base_type;
27712 #if ENABLE_ASSERT_CHECKING
27713 /* All the marks should already be clear. */
27714 verify_marks_clear (comp_unit_die ());
27715 for (node = limbo_die_list; node; node = node->next)
27716 verify_marks_clear (node->die);
27717 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27718 verify_marks_clear (ctnode->root_die);
27719 #endif /* ENABLE_ASSERT_CHECKING */
27721 /* Mark types that are used in global variables. */
27722 premark_types_used_by_global_vars ();
27724 /* Set the mark on nodes that are actually used. */
27725 prune_unused_types_walk (comp_unit_die ());
27726 for (node = limbo_die_list; node; node = node->next)
27727 prune_unused_types_walk (node->die);
27728 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27730 prune_unused_types_walk (ctnode->root_die);
27731 prune_unused_types_mark (ctnode->type_die, 1);
27734 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27735 are unusual in that they are pubnames that are the children of pubtypes.
27736 They should only be marked via their parent DW_TAG_enumeration_type die,
27737 not as roots in themselves. */
27738 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27739 if (pub->die->die_tag != DW_TAG_enumerator)
27740 prune_unused_types_mark (pub->die, 1);
27741 for (i = 0; base_types.iterate (i, &base_type); i++)
27742 prune_unused_types_mark (base_type, 1);
27744 if (debug_str_hash)
27745 debug_str_hash->empty ();
27746 if (skeleton_debug_str_hash)
27747 skeleton_debug_str_hash->empty ();
27748 prune_unused_types_prune (comp_unit_die ());
27749 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27751 node = *pnode;
27752 if (!node->die->die_mark)
27753 *pnode = node->next;
27754 else
27756 prune_unused_types_prune (node->die);
27757 pnode = &node->next;
27760 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27761 prune_unused_types_prune (ctnode->root_die);
27763 /* Leave the marks clear. */
27764 prune_unmark_dies (comp_unit_die ());
27765 for (node = limbo_die_list; node; node = node->next)
27766 prune_unmark_dies (node->die);
27767 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27768 prune_unmark_dies (ctnode->root_die);
27771 /* Helpers to manipulate hash table of comdat type units. */
27773 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27775 static inline hashval_t hash (const comdat_type_node *);
27776 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27779 inline hashval_t
27780 comdat_type_hasher::hash (const comdat_type_node *type_node)
27782 hashval_t h;
27783 memcpy (&h, type_node->signature, sizeof (h));
27784 return h;
27787 inline bool
27788 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27789 const comdat_type_node *type_node_2)
27791 return (! memcmp (type_node_1->signature, type_node_2->signature,
27792 DWARF_TYPE_SIGNATURE_SIZE));
27795 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
27796 to the location it would have been added, should we know its
27797 DECL_ASSEMBLER_NAME when we added other attributes. This will
27798 probably improve compactness of debug info, removing equivalent
27799 abbrevs, and hide any differences caused by deferring the
27800 computation of the assembler name, triggered by e.g. PCH. */
27802 static inline void
27803 move_linkage_attr (dw_die_ref die)
27805 unsigned ix = vec_safe_length (die->die_attr);
27806 dw_attr_node linkage = (*die->die_attr)[ix - 1];
27808 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
27809 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
27811 while (--ix > 0)
27813 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
27815 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
27816 break;
27819 if (ix != vec_safe_length (die->die_attr) - 1)
27821 die->die_attr->pop ();
27822 die->die_attr->quick_insert (ix, linkage);
27826 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
27827 referenced from typed stack ops and count how often they are used. */
27829 static void
27830 mark_base_types (dw_loc_descr_ref loc)
27832 dw_die_ref base_type = NULL;
27834 for (; loc; loc = loc->dw_loc_next)
27836 switch (loc->dw_loc_opc)
27838 case DW_OP_regval_type:
27839 case DW_OP_deref_type:
27840 case DW_OP_GNU_regval_type:
27841 case DW_OP_GNU_deref_type:
27842 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
27843 break;
27844 case DW_OP_convert:
27845 case DW_OP_reinterpret:
27846 case DW_OP_GNU_convert:
27847 case DW_OP_GNU_reinterpret:
27848 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
27849 continue;
27850 /* FALLTHRU */
27851 case DW_OP_const_type:
27852 case DW_OP_GNU_const_type:
27853 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
27854 break;
27855 case DW_OP_entry_value:
27856 case DW_OP_GNU_entry_value:
27857 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
27858 continue;
27859 default:
27860 continue;
27862 gcc_assert (base_type->die_parent == comp_unit_die ());
27863 if (base_type->die_mark)
27864 base_type->die_mark++;
27865 else
27867 base_types.safe_push (base_type);
27868 base_type->die_mark = 1;
27873 /* Comparison function for sorting marked base types. */
27875 static int
27876 base_type_cmp (const void *x, const void *y)
27878 dw_die_ref dx = *(const dw_die_ref *) x;
27879 dw_die_ref dy = *(const dw_die_ref *) y;
27880 unsigned int byte_size1, byte_size2;
27881 unsigned int encoding1, encoding2;
27882 if (dx->die_mark > dy->die_mark)
27883 return -1;
27884 if (dx->die_mark < dy->die_mark)
27885 return 1;
27886 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
27887 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
27888 if (byte_size1 < byte_size2)
27889 return 1;
27890 if (byte_size1 > byte_size2)
27891 return -1;
27892 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
27893 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
27894 if (encoding1 < encoding2)
27895 return 1;
27896 if (encoding1 > encoding2)
27897 return -1;
27898 return 0;
27901 /* Move base types marked by mark_base_types as early as possible
27902 in the CU, sorted by decreasing usage count both to make the
27903 uleb128 references as small as possible and to make sure they
27904 will have die_offset already computed by calc_die_sizes when
27905 sizes of typed stack loc ops is computed. */
27907 static void
27908 move_marked_base_types (void)
27910 unsigned int i;
27911 dw_die_ref base_type, die, c;
27913 if (base_types.is_empty ())
27914 return;
27916 /* Sort by decreasing usage count, they will be added again in that
27917 order later on. */
27918 base_types.qsort (base_type_cmp);
27919 die = comp_unit_die ();
27920 c = die->die_child;
27923 dw_die_ref prev = c;
27924 c = c->die_sib;
27925 while (c->die_mark)
27927 remove_child_with_prev (c, prev);
27928 /* As base types got marked, there must be at least
27929 one node other than DW_TAG_base_type. */
27930 gcc_assert (die->die_child != NULL);
27931 c = prev->die_sib;
27934 while (c != die->die_child);
27935 gcc_assert (die->die_child);
27936 c = die->die_child;
27937 for (i = 0; base_types.iterate (i, &base_type); i++)
27939 base_type->die_mark = 0;
27940 base_type->die_sib = c->die_sib;
27941 c->die_sib = base_type;
27942 c = base_type;
27946 /* Helper function for resolve_addr, attempt to resolve
27947 one CONST_STRING, return true if successful. Similarly verify that
27948 SYMBOL_REFs refer to variables emitted in the current CU. */
27950 static bool
27951 resolve_one_addr (rtx *addr)
27953 rtx rtl = *addr;
27955 if (GET_CODE (rtl) == CONST_STRING)
27957 size_t len = strlen (XSTR (rtl, 0)) + 1;
27958 tree t = build_string (len, XSTR (rtl, 0));
27959 tree tlen = size_int (len - 1);
27960 TREE_TYPE (t)
27961 = build_array_type (char_type_node, build_index_type (tlen));
27962 rtl = lookup_constant_def (t);
27963 if (!rtl || !MEM_P (rtl))
27964 return false;
27965 rtl = XEXP (rtl, 0);
27966 if (GET_CODE (rtl) == SYMBOL_REF
27967 && SYMBOL_REF_DECL (rtl)
27968 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
27969 return false;
27970 vec_safe_push (used_rtx_array, rtl);
27971 *addr = rtl;
27972 return true;
27975 if (GET_CODE (rtl) == SYMBOL_REF
27976 && SYMBOL_REF_DECL (rtl))
27978 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
27980 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
27981 return false;
27983 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
27984 return false;
27987 if (GET_CODE (rtl) == CONST)
27989 subrtx_ptr_iterator::array_type array;
27990 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
27991 if (!resolve_one_addr (*iter))
27992 return false;
27995 return true;
27998 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
27999 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28000 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28002 static rtx
28003 string_cst_pool_decl (tree t)
28005 rtx rtl = output_constant_def (t, 1);
28006 unsigned char *array;
28007 dw_loc_descr_ref l;
28008 tree decl;
28009 size_t len;
28010 dw_die_ref ref;
28012 if (!rtl || !MEM_P (rtl))
28013 return NULL_RTX;
28014 rtl = XEXP (rtl, 0);
28015 if (GET_CODE (rtl) != SYMBOL_REF
28016 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28017 return NULL_RTX;
28019 decl = SYMBOL_REF_DECL (rtl);
28020 if (!lookup_decl_die (decl))
28022 len = TREE_STRING_LENGTH (t);
28023 vec_safe_push (used_rtx_array, rtl);
28024 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28025 array = ggc_vec_alloc<unsigned char> (len);
28026 memcpy (array, TREE_STRING_POINTER (t), len);
28027 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28028 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28029 l->dw_loc_oprnd2.v.val_vec.length = len;
28030 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28031 l->dw_loc_oprnd2.v.val_vec.array = array;
28032 add_AT_loc (ref, DW_AT_location, l);
28033 equate_decl_number_to_die (decl, ref);
28035 return rtl;
28038 /* Helper function of resolve_addr_in_expr. LOC is
28039 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28040 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28041 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28042 with DW_OP_implicit_pointer if possible
28043 and return true, if unsuccessful, return false. */
28045 static bool
28046 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28048 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28049 HOST_WIDE_INT offset = 0;
28050 dw_die_ref ref = NULL;
28051 tree decl;
28053 if (GET_CODE (rtl) == CONST
28054 && GET_CODE (XEXP (rtl, 0)) == PLUS
28055 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28057 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28058 rtl = XEXP (XEXP (rtl, 0), 0);
28060 if (GET_CODE (rtl) == CONST_STRING)
28062 size_t len = strlen (XSTR (rtl, 0)) + 1;
28063 tree t = build_string (len, XSTR (rtl, 0));
28064 tree tlen = size_int (len - 1);
28066 TREE_TYPE (t)
28067 = build_array_type (char_type_node, build_index_type (tlen));
28068 rtl = string_cst_pool_decl (t);
28069 if (!rtl)
28070 return false;
28072 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28074 decl = SYMBOL_REF_DECL (rtl);
28075 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28077 ref = lookup_decl_die (decl);
28078 if (ref && (get_AT (ref, DW_AT_location)
28079 || get_AT (ref, DW_AT_const_value)))
28081 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28082 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28083 loc->dw_loc_oprnd1.val_entry = NULL;
28084 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28085 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28086 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28087 loc->dw_loc_oprnd2.v.val_int = offset;
28088 return true;
28092 return false;
28095 /* Helper function for resolve_addr, handle one location
28096 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28097 the location list couldn't be resolved. */
28099 static bool
28100 resolve_addr_in_expr (dw_loc_descr_ref loc)
28102 dw_loc_descr_ref keep = NULL;
28103 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28104 switch (loc->dw_loc_opc)
28106 case DW_OP_addr:
28107 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28109 if ((prev == NULL
28110 || prev->dw_loc_opc == DW_OP_piece
28111 || prev->dw_loc_opc == DW_OP_bit_piece)
28112 && loc->dw_loc_next
28113 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28114 && (!dwarf_strict || dwarf_version >= 5)
28115 && optimize_one_addr_into_implicit_ptr (loc))
28116 break;
28117 return false;
28119 break;
28120 case DW_OP_GNU_addr_index:
28121 case DW_OP_GNU_const_index:
28122 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28123 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28125 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28126 if (!resolve_one_addr (&rtl))
28127 return false;
28128 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28129 loc->dw_loc_oprnd1.val_entry
28130 = add_addr_table_entry (rtl, ate_kind_rtx);
28132 break;
28133 case DW_OP_const4u:
28134 case DW_OP_const8u:
28135 if (loc->dtprel
28136 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28137 return false;
28138 break;
28139 case DW_OP_plus_uconst:
28140 if (size_of_loc_descr (loc)
28141 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28143 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28145 dw_loc_descr_ref repl
28146 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28147 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28148 add_loc_descr (&repl, loc->dw_loc_next);
28149 *loc = *repl;
28151 break;
28152 case DW_OP_implicit_value:
28153 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28154 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28155 return false;
28156 break;
28157 case DW_OP_implicit_pointer:
28158 case DW_OP_GNU_implicit_pointer:
28159 case DW_OP_GNU_parameter_ref:
28160 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28162 dw_die_ref ref
28163 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28164 if (ref == NULL)
28165 return false;
28166 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28167 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28168 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28170 break;
28171 case DW_OP_const_type:
28172 case DW_OP_regval_type:
28173 case DW_OP_deref_type:
28174 case DW_OP_convert:
28175 case DW_OP_reinterpret:
28176 case DW_OP_GNU_const_type:
28177 case DW_OP_GNU_regval_type:
28178 case DW_OP_GNU_deref_type:
28179 case DW_OP_GNU_convert:
28180 case DW_OP_GNU_reinterpret:
28181 while (loc->dw_loc_next
28182 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28183 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28185 dw_die_ref base1, base2;
28186 unsigned enc1, enc2, size1, size2;
28187 if (loc->dw_loc_opc == DW_OP_regval_type
28188 || loc->dw_loc_opc == DW_OP_deref_type
28189 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28190 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28191 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28192 else if (loc->dw_loc_oprnd1.val_class
28193 == dw_val_class_unsigned_const)
28194 break;
28195 else
28196 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28197 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28198 == dw_val_class_unsigned_const)
28199 break;
28200 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28201 gcc_assert (base1->die_tag == DW_TAG_base_type
28202 && base2->die_tag == DW_TAG_base_type);
28203 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28204 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28205 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28206 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28207 if (size1 == size2
28208 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28209 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28210 && loc != keep)
28211 || enc1 == enc2))
28213 /* Optimize away next DW_OP_convert after
28214 adjusting LOC's base type die reference. */
28215 if (loc->dw_loc_opc == DW_OP_regval_type
28216 || loc->dw_loc_opc == DW_OP_deref_type
28217 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28218 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28219 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28220 else
28221 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28222 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28223 continue;
28225 /* Don't change integer DW_OP_convert after e.g. floating
28226 point typed stack entry. */
28227 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28228 keep = loc->dw_loc_next;
28229 break;
28231 break;
28232 default:
28233 break;
28235 return true;
28238 /* Helper function of resolve_addr. DIE had DW_AT_location of
28239 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28240 and DW_OP_addr couldn't be resolved. resolve_addr has already
28241 removed the DW_AT_location attribute. This function attempts to
28242 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28243 to it or DW_AT_const_value attribute, if possible. */
28245 static void
28246 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28248 if (!VAR_P (decl)
28249 || lookup_decl_die (decl) != die
28250 || DECL_EXTERNAL (decl)
28251 || !TREE_STATIC (decl)
28252 || DECL_INITIAL (decl) == NULL_TREE
28253 || DECL_P (DECL_INITIAL (decl))
28254 || get_AT (die, DW_AT_const_value))
28255 return;
28257 tree init = DECL_INITIAL (decl);
28258 HOST_WIDE_INT offset = 0;
28259 /* For variables that have been optimized away and thus
28260 don't have a memory location, see if we can emit
28261 DW_AT_const_value instead. */
28262 if (tree_add_const_value_attribute (die, init))
28263 return;
28264 if (dwarf_strict && dwarf_version < 5)
28265 return;
28266 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28267 and ADDR_EXPR refers to a decl that has DW_AT_location or
28268 DW_AT_const_value (but isn't addressable, otherwise
28269 resolving the original DW_OP_addr wouldn't fail), see if
28270 we can add DW_OP_implicit_pointer. */
28271 STRIP_NOPS (init);
28272 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28273 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28275 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28276 init = TREE_OPERAND (init, 0);
28277 STRIP_NOPS (init);
28279 if (TREE_CODE (init) != ADDR_EXPR)
28280 return;
28281 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28282 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28283 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28284 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28285 && TREE_OPERAND (init, 0) != decl))
28287 dw_die_ref ref;
28288 dw_loc_descr_ref l;
28290 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28292 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28293 if (!rtl)
28294 return;
28295 decl = SYMBOL_REF_DECL (rtl);
28297 else
28298 decl = TREE_OPERAND (init, 0);
28299 ref = lookup_decl_die (decl);
28300 if (ref == NULL
28301 || (!get_AT (ref, DW_AT_location)
28302 && !get_AT (ref, DW_AT_const_value)))
28303 return;
28304 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28305 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28306 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28307 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28308 add_AT_loc (die, DW_AT_location, l);
28312 /* Return NULL if l is a DWARF expression, or first op that is not
28313 valid DWARF expression. */
28315 static dw_loc_descr_ref
28316 non_dwarf_expression (dw_loc_descr_ref l)
28318 while (l)
28320 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28321 return l;
28322 switch (l->dw_loc_opc)
28324 case DW_OP_regx:
28325 case DW_OP_implicit_value:
28326 case DW_OP_stack_value:
28327 case DW_OP_implicit_pointer:
28328 case DW_OP_GNU_implicit_pointer:
28329 case DW_OP_GNU_parameter_ref:
28330 case DW_OP_piece:
28331 case DW_OP_bit_piece:
28332 return l;
28333 default:
28334 break;
28336 l = l->dw_loc_next;
28338 return NULL;
28341 /* Return adjusted copy of EXPR:
28342 If it is empty DWARF expression, return it.
28343 If it is valid non-empty DWARF expression,
28344 return copy of EXPR with copy of DEREF appended to it.
28345 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28346 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
28347 and no DEREF.
28348 If it is DWARF expression followed by DW_OP_stack_value, return
28349 copy of the DWARF expression without anything appended.
28350 Otherwise, return NULL. */
28352 static dw_loc_descr_ref
28353 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
28356 if (expr == NULL)
28357 return NULL;
28359 dw_loc_descr_ref l = non_dwarf_expression (expr);
28360 if (l && l->dw_loc_next)
28361 return NULL;
28363 if (l)
28365 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28366 deref = new_loc_descr ((enum dwarf_location_atom)
28367 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28368 0, 0);
28369 else
28370 switch (l->dw_loc_opc)
28372 case DW_OP_regx:
28373 deref = new_loc_descr (DW_OP_bregx,
28374 l->dw_loc_oprnd1.v.val_unsigned, 0);
28375 break;
28376 case DW_OP_stack_value:
28377 deref = NULL;
28378 break;
28379 default:
28380 return NULL;
28383 else
28384 deref = new_loc_descr (deref->dw_loc_opc,
28385 deref->dw_loc_oprnd1.v.val_int, 0);
28387 dw_loc_descr_ref ret = NULL, *p = &ret;
28388 while (expr != l)
28390 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28391 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28392 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28393 p = &(*p)->dw_loc_next;
28394 expr = expr->dw_loc_next;
28396 *p = deref;
28397 return ret;
28400 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
28401 or argument, adjust it if needed and return:
28402 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28403 attribute if present should be removed
28404 0 keep the attribute as is if the referenced var or argument has
28405 only DWARF expression that covers all ranges
28406 1 if the attribute has been successfully adjusted. */
28408 static int
28409 optimize_string_length (dw_attr_node *a)
28411 dw_loc_descr_ref l = AT_loc (a), lv;
28412 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
28413 dw_attr_node *av = get_AT (die, DW_AT_location);
28414 dw_loc_list_ref d;
28415 bool non_dwarf_expr = false;
28417 if (av == NULL)
28418 return -1;
28419 switch (AT_class (av))
28421 case dw_val_class_loc_list:
28422 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28423 if (d->expr && non_dwarf_expression (d->expr))
28424 non_dwarf_expr = true;
28425 break;
28426 case dw_val_class_loc:
28427 lv = AT_loc (av);
28428 if (lv == NULL)
28429 return -1;
28430 if (non_dwarf_expression (lv))
28431 non_dwarf_expr = true;
28432 break;
28433 default:
28434 return -1;
28437 /* If it is safe to keep DW_OP_call4 in, keep it. */
28438 if (!non_dwarf_expr
28439 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
28440 return 0;
28442 /* If not dereferencing the DW_OP_call4 afterwards, we can just
28443 copy over the DW_AT_location attribute from die to a. */
28444 if (l->dw_loc_next == NULL)
28446 a->dw_attr_val = av->dw_attr_val;
28447 return 1;
28450 dw_loc_list_ref list, *p;
28451 switch (AT_class (av))
28453 case dw_val_class_loc_list:
28454 p = &list;
28455 list = NULL;
28456 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28458 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
28459 if (lv)
28461 *p = new_loc_list (lv, d->begin, d->end, d->section);
28462 p = &(*p)->dw_loc_next;
28465 if (list == NULL)
28466 return -1;
28467 a->dw_attr_val.val_class = dw_val_class_loc_list;
28468 gen_llsym (list);
28469 *AT_loc_list_ptr (a) = list;
28470 return 1;
28471 case dw_val_class_loc:
28472 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
28473 if (lv == NULL)
28474 return -1;
28475 a->dw_attr_val.v.val_loc = lv;
28476 return 1;
28477 default:
28478 gcc_unreachable ();
28482 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28483 an address in .rodata section if the string literal is emitted there,
28484 or remove the containing location list or replace DW_AT_const_value
28485 with DW_AT_location and empty location expression, if it isn't found
28486 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28487 to something that has been emitted in the current CU. */
28489 static void
28490 resolve_addr (dw_die_ref die)
28492 dw_die_ref c;
28493 dw_attr_node *a;
28494 dw_loc_list_ref *curr, *start, loc;
28495 unsigned ix;
28496 bool remove_AT_byte_size = false;
28498 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28499 switch (AT_class (a))
28501 case dw_val_class_loc_list:
28502 start = curr = AT_loc_list_ptr (a);
28503 loc = *curr;
28504 gcc_assert (loc);
28505 /* The same list can be referenced more than once. See if we have
28506 already recorded the result from a previous pass. */
28507 if (loc->replaced)
28508 *curr = loc->dw_loc_next;
28509 else if (!loc->resolved_addr)
28511 /* As things stand, we do not expect or allow one die to
28512 reference a suffix of another die's location list chain.
28513 References must be identical or completely separate.
28514 There is therefore no need to cache the result of this
28515 pass on any list other than the first; doing so
28516 would lead to unnecessary writes. */
28517 while (*curr)
28519 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28520 if (!resolve_addr_in_expr ((*curr)->expr))
28522 dw_loc_list_ref next = (*curr)->dw_loc_next;
28523 dw_loc_descr_ref l = (*curr)->expr;
28525 if (next && (*curr)->ll_symbol)
28527 gcc_assert (!next->ll_symbol);
28528 next->ll_symbol = (*curr)->ll_symbol;
28530 if (dwarf_split_debug_info)
28531 remove_loc_list_addr_table_entries (l);
28532 *curr = next;
28534 else
28536 mark_base_types ((*curr)->expr);
28537 curr = &(*curr)->dw_loc_next;
28540 if (loc == *start)
28541 loc->resolved_addr = 1;
28542 else
28544 loc->replaced = 1;
28545 loc->dw_loc_next = *start;
28548 if (!*start)
28550 remove_AT (die, a->dw_attr);
28551 ix--;
28553 break;
28554 case dw_val_class_loc:
28556 dw_loc_descr_ref l = AT_loc (a);
28557 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
28558 DW_AT_string_length is only a rough approximation; unfortunately
28559 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
28560 needs a DWARF expression, while DW_AT_location of the referenced
28561 variable or argument might be any location description. */
28562 if (a->dw_attr == DW_AT_string_length
28563 && l
28564 && l->dw_loc_opc == DW_OP_call4
28565 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
28566 && (l->dw_loc_next == NULL
28567 || (l->dw_loc_next->dw_loc_next == NULL
28568 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
28569 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
28571 switch (optimize_string_length (a))
28573 case -1:
28574 remove_AT (die, a->dw_attr);
28575 ix--;
28576 /* If we drop DW_AT_string_length, we need to drop also
28577 DW_AT_{string_length_,}byte_size. */
28578 remove_AT_byte_size = true;
28579 continue;
28580 default:
28581 break;
28582 case 1:
28583 /* Even if we keep the optimized DW_AT_string_length,
28584 it might have changed AT_class, so process it again. */
28585 ix--;
28586 continue;
28589 /* For -gdwarf-2 don't attempt to optimize
28590 DW_AT_data_member_location containing
28591 DW_OP_plus_uconst - older consumers might
28592 rely on it being that op instead of a more complex,
28593 but shorter, location description. */
28594 if ((dwarf_version > 2
28595 || a->dw_attr != DW_AT_data_member_location
28596 || l == NULL
28597 || l->dw_loc_opc != DW_OP_plus_uconst
28598 || l->dw_loc_next != NULL)
28599 && !resolve_addr_in_expr (l))
28601 if (dwarf_split_debug_info)
28602 remove_loc_list_addr_table_entries (l);
28603 if (l != NULL
28604 && l->dw_loc_next == NULL
28605 && l->dw_loc_opc == DW_OP_addr
28606 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28607 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28608 && a->dw_attr == DW_AT_location)
28610 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28611 remove_AT (die, a->dw_attr);
28612 ix--;
28613 optimize_location_into_implicit_ptr (die, decl);
28614 break;
28616 remove_AT (die, a->dw_attr);
28617 ix--;
28619 else
28620 mark_base_types (l);
28622 break;
28623 case dw_val_class_addr:
28624 if (a->dw_attr == DW_AT_const_value
28625 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28627 if (AT_index (a) != NOT_INDEXED)
28628 remove_addr_table_entry (a->dw_attr_val.val_entry);
28629 remove_AT (die, a->dw_attr);
28630 ix--;
28632 if ((die->die_tag == DW_TAG_call_site
28633 && a->dw_attr == DW_AT_call_origin)
28634 || (die->die_tag == DW_TAG_GNU_call_site
28635 && a->dw_attr == DW_AT_abstract_origin))
28637 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28638 dw_die_ref tdie = lookup_decl_die (tdecl);
28639 dw_die_ref cdie;
28640 if (tdie == NULL
28641 && DECL_EXTERNAL (tdecl)
28642 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28643 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28645 /* Creating a full DIE for tdecl is overly expensive and
28646 at this point even wrong when in the LTO phase
28647 as it can end up generating new type DIEs we didn't
28648 output and thus optimize_external_refs will crash. */
28649 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28650 add_AT_flag (tdie, DW_AT_external, 1);
28651 add_AT_flag (tdie, DW_AT_declaration, 1);
28652 add_linkage_attr (tdie, tdecl);
28653 add_name_and_src_coords_attributes (tdie, tdecl);
28654 equate_decl_number_to_die (tdecl, tdie);
28656 if (tdie)
28658 a->dw_attr_val.val_class = dw_val_class_die_ref;
28659 a->dw_attr_val.v.val_die_ref.die = tdie;
28660 a->dw_attr_val.v.val_die_ref.external = 0;
28662 else
28664 if (AT_index (a) != NOT_INDEXED)
28665 remove_addr_table_entry (a->dw_attr_val.val_entry);
28666 remove_AT (die, a->dw_attr);
28667 ix--;
28670 break;
28671 default:
28672 break;
28675 if (remove_AT_byte_size)
28676 remove_AT (die, dwarf_version >= 5
28677 ? DW_AT_string_length_byte_size
28678 : DW_AT_byte_size);
28680 FOR_EACH_CHILD (die, c, resolve_addr (c));
28683 /* Helper routines for optimize_location_lists.
28684 This pass tries to share identical local lists in .debug_loc
28685 section. */
28687 /* Iteratively hash operands of LOC opcode into HSTATE. */
28689 static void
28690 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28692 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28693 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28695 switch (loc->dw_loc_opc)
28697 case DW_OP_const4u:
28698 case DW_OP_const8u:
28699 if (loc->dtprel)
28700 goto hash_addr;
28701 /* FALLTHRU */
28702 case DW_OP_const1u:
28703 case DW_OP_const1s:
28704 case DW_OP_const2u:
28705 case DW_OP_const2s:
28706 case DW_OP_const4s:
28707 case DW_OP_const8s:
28708 case DW_OP_constu:
28709 case DW_OP_consts:
28710 case DW_OP_pick:
28711 case DW_OP_plus_uconst:
28712 case DW_OP_breg0:
28713 case DW_OP_breg1:
28714 case DW_OP_breg2:
28715 case DW_OP_breg3:
28716 case DW_OP_breg4:
28717 case DW_OP_breg5:
28718 case DW_OP_breg6:
28719 case DW_OP_breg7:
28720 case DW_OP_breg8:
28721 case DW_OP_breg9:
28722 case DW_OP_breg10:
28723 case DW_OP_breg11:
28724 case DW_OP_breg12:
28725 case DW_OP_breg13:
28726 case DW_OP_breg14:
28727 case DW_OP_breg15:
28728 case DW_OP_breg16:
28729 case DW_OP_breg17:
28730 case DW_OP_breg18:
28731 case DW_OP_breg19:
28732 case DW_OP_breg20:
28733 case DW_OP_breg21:
28734 case DW_OP_breg22:
28735 case DW_OP_breg23:
28736 case DW_OP_breg24:
28737 case DW_OP_breg25:
28738 case DW_OP_breg26:
28739 case DW_OP_breg27:
28740 case DW_OP_breg28:
28741 case DW_OP_breg29:
28742 case DW_OP_breg30:
28743 case DW_OP_breg31:
28744 case DW_OP_regx:
28745 case DW_OP_fbreg:
28746 case DW_OP_piece:
28747 case DW_OP_deref_size:
28748 case DW_OP_xderef_size:
28749 hstate.add_object (val1->v.val_int);
28750 break;
28751 case DW_OP_skip:
28752 case DW_OP_bra:
28754 int offset;
28756 gcc_assert (val1->val_class == dw_val_class_loc);
28757 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
28758 hstate.add_object (offset);
28760 break;
28761 case DW_OP_implicit_value:
28762 hstate.add_object (val1->v.val_unsigned);
28763 switch (val2->val_class)
28765 case dw_val_class_const:
28766 hstate.add_object (val2->v.val_int);
28767 break;
28768 case dw_val_class_vec:
28770 unsigned int elt_size = val2->v.val_vec.elt_size;
28771 unsigned int len = val2->v.val_vec.length;
28773 hstate.add_int (elt_size);
28774 hstate.add_int (len);
28775 hstate.add (val2->v.val_vec.array, len * elt_size);
28777 break;
28778 case dw_val_class_const_double:
28779 hstate.add_object (val2->v.val_double.low);
28780 hstate.add_object (val2->v.val_double.high);
28781 break;
28782 case dw_val_class_wide_int:
28783 hstate.add (val2->v.val_wide->get_val (),
28784 get_full_len (*val2->v.val_wide)
28785 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28786 break;
28787 case dw_val_class_addr:
28788 inchash::add_rtx (val2->v.val_addr, hstate);
28789 break;
28790 default:
28791 gcc_unreachable ();
28793 break;
28794 case DW_OP_bregx:
28795 case DW_OP_bit_piece:
28796 hstate.add_object (val1->v.val_int);
28797 hstate.add_object (val2->v.val_int);
28798 break;
28799 case DW_OP_addr:
28800 hash_addr:
28801 if (loc->dtprel)
28803 unsigned char dtprel = 0xd1;
28804 hstate.add_object (dtprel);
28806 inchash::add_rtx (val1->v.val_addr, hstate);
28807 break;
28808 case DW_OP_GNU_addr_index:
28809 case DW_OP_GNU_const_index:
28811 if (loc->dtprel)
28813 unsigned char dtprel = 0xd1;
28814 hstate.add_object (dtprel);
28816 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
28818 break;
28819 case DW_OP_implicit_pointer:
28820 case DW_OP_GNU_implicit_pointer:
28821 hstate.add_int (val2->v.val_int);
28822 break;
28823 case DW_OP_entry_value:
28824 case DW_OP_GNU_entry_value:
28825 hstate.add_object (val1->v.val_loc);
28826 break;
28827 case DW_OP_regval_type:
28828 case DW_OP_deref_type:
28829 case DW_OP_GNU_regval_type:
28830 case DW_OP_GNU_deref_type:
28832 unsigned int byte_size
28833 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
28834 unsigned int encoding
28835 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
28836 hstate.add_object (val1->v.val_int);
28837 hstate.add_object (byte_size);
28838 hstate.add_object (encoding);
28840 break;
28841 case DW_OP_convert:
28842 case DW_OP_reinterpret:
28843 case DW_OP_GNU_convert:
28844 case DW_OP_GNU_reinterpret:
28845 if (val1->val_class == dw_val_class_unsigned_const)
28847 hstate.add_object (val1->v.val_unsigned);
28848 break;
28850 /* FALLTHRU */
28851 case DW_OP_const_type:
28852 case DW_OP_GNU_const_type:
28854 unsigned int byte_size
28855 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
28856 unsigned int encoding
28857 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
28858 hstate.add_object (byte_size);
28859 hstate.add_object (encoding);
28860 if (loc->dw_loc_opc != DW_OP_const_type
28861 && loc->dw_loc_opc != DW_OP_GNU_const_type)
28862 break;
28863 hstate.add_object (val2->val_class);
28864 switch (val2->val_class)
28866 case dw_val_class_const:
28867 hstate.add_object (val2->v.val_int);
28868 break;
28869 case dw_val_class_vec:
28871 unsigned int elt_size = val2->v.val_vec.elt_size;
28872 unsigned int len = val2->v.val_vec.length;
28874 hstate.add_object (elt_size);
28875 hstate.add_object (len);
28876 hstate.add (val2->v.val_vec.array, len * elt_size);
28878 break;
28879 case dw_val_class_const_double:
28880 hstate.add_object (val2->v.val_double.low);
28881 hstate.add_object (val2->v.val_double.high);
28882 break;
28883 case dw_val_class_wide_int:
28884 hstate.add (val2->v.val_wide->get_val (),
28885 get_full_len (*val2->v.val_wide)
28886 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28887 break;
28888 default:
28889 gcc_unreachable ();
28892 break;
28894 default:
28895 /* Other codes have no operands. */
28896 break;
28900 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
28902 static inline void
28903 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
28905 dw_loc_descr_ref l;
28906 bool sizes_computed = false;
28907 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
28908 size_of_locs (loc);
28910 for (l = loc; l != NULL; l = l->dw_loc_next)
28912 enum dwarf_location_atom opc = l->dw_loc_opc;
28913 hstate.add_object (opc);
28914 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
28916 size_of_locs (loc);
28917 sizes_computed = true;
28919 hash_loc_operands (l, hstate);
28923 /* Compute hash of the whole location list LIST_HEAD. */
28925 static inline void
28926 hash_loc_list (dw_loc_list_ref list_head)
28928 dw_loc_list_ref curr = list_head;
28929 inchash::hash hstate;
28931 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
28933 hstate.add (curr->begin, strlen (curr->begin) + 1);
28934 hstate.add (curr->end, strlen (curr->end) + 1);
28935 if (curr->section)
28936 hstate.add (curr->section, strlen (curr->section) + 1);
28937 hash_locs (curr->expr, hstate);
28939 list_head->hash = hstate.end ();
28942 /* Return true if X and Y opcodes have the same operands. */
28944 static inline bool
28945 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
28947 dw_val_ref valx1 = &x->dw_loc_oprnd1;
28948 dw_val_ref valx2 = &x->dw_loc_oprnd2;
28949 dw_val_ref valy1 = &y->dw_loc_oprnd1;
28950 dw_val_ref valy2 = &y->dw_loc_oprnd2;
28952 switch (x->dw_loc_opc)
28954 case DW_OP_const4u:
28955 case DW_OP_const8u:
28956 if (x->dtprel)
28957 goto hash_addr;
28958 /* FALLTHRU */
28959 case DW_OP_const1u:
28960 case DW_OP_const1s:
28961 case DW_OP_const2u:
28962 case DW_OP_const2s:
28963 case DW_OP_const4s:
28964 case DW_OP_const8s:
28965 case DW_OP_constu:
28966 case DW_OP_consts:
28967 case DW_OP_pick:
28968 case DW_OP_plus_uconst:
28969 case DW_OP_breg0:
28970 case DW_OP_breg1:
28971 case DW_OP_breg2:
28972 case DW_OP_breg3:
28973 case DW_OP_breg4:
28974 case DW_OP_breg5:
28975 case DW_OP_breg6:
28976 case DW_OP_breg7:
28977 case DW_OP_breg8:
28978 case DW_OP_breg9:
28979 case DW_OP_breg10:
28980 case DW_OP_breg11:
28981 case DW_OP_breg12:
28982 case DW_OP_breg13:
28983 case DW_OP_breg14:
28984 case DW_OP_breg15:
28985 case DW_OP_breg16:
28986 case DW_OP_breg17:
28987 case DW_OP_breg18:
28988 case DW_OP_breg19:
28989 case DW_OP_breg20:
28990 case DW_OP_breg21:
28991 case DW_OP_breg22:
28992 case DW_OP_breg23:
28993 case DW_OP_breg24:
28994 case DW_OP_breg25:
28995 case DW_OP_breg26:
28996 case DW_OP_breg27:
28997 case DW_OP_breg28:
28998 case DW_OP_breg29:
28999 case DW_OP_breg30:
29000 case DW_OP_breg31:
29001 case DW_OP_regx:
29002 case DW_OP_fbreg:
29003 case DW_OP_piece:
29004 case DW_OP_deref_size:
29005 case DW_OP_xderef_size:
29006 return valx1->v.val_int == valy1->v.val_int;
29007 case DW_OP_skip:
29008 case DW_OP_bra:
29009 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29010 can cause irrelevant differences in dw_loc_addr. */
29011 gcc_assert (valx1->val_class == dw_val_class_loc
29012 && valy1->val_class == dw_val_class_loc
29013 && (dwarf_split_debug_info
29014 || x->dw_loc_addr == y->dw_loc_addr));
29015 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29016 case DW_OP_implicit_value:
29017 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29018 || valx2->val_class != valy2->val_class)
29019 return false;
29020 switch (valx2->val_class)
29022 case dw_val_class_const:
29023 return valx2->v.val_int == valy2->v.val_int;
29024 case dw_val_class_vec:
29025 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29026 && valx2->v.val_vec.length == valy2->v.val_vec.length
29027 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29028 valx2->v.val_vec.elt_size
29029 * valx2->v.val_vec.length) == 0;
29030 case dw_val_class_const_double:
29031 return valx2->v.val_double.low == valy2->v.val_double.low
29032 && valx2->v.val_double.high == valy2->v.val_double.high;
29033 case dw_val_class_wide_int:
29034 return *valx2->v.val_wide == *valy2->v.val_wide;
29035 case dw_val_class_addr:
29036 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29037 default:
29038 gcc_unreachable ();
29040 case DW_OP_bregx:
29041 case DW_OP_bit_piece:
29042 return valx1->v.val_int == valy1->v.val_int
29043 && valx2->v.val_int == valy2->v.val_int;
29044 case DW_OP_addr:
29045 hash_addr:
29046 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29047 case DW_OP_GNU_addr_index:
29048 case DW_OP_GNU_const_index:
29050 rtx ax1 = valx1->val_entry->addr.rtl;
29051 rtx ay1 = valy1->val_entry->addr.rtl;
29052 return rtx_equal_p (ax1, ay1);
29054 case DW_OP_implicit_pointer:
29055 case DW_OP_GNU_implicit_pointer:
29056 return valx1->val_class == dw_val_class_die_ref
29057 && valx1->val_class == valy1->val_class
29058 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29059 && valx2->v.val_int == valy2->v.val_int;
29060 case DW_OP_entry_value:
29061 case DW_OP_GNU_entry_value:
29062 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29063 case DW_OP_const_type:
29064 case DW_OP_GNU_const_type:
29065 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29066 || valx2->val_class != valy2->val_class)
29067 return false;
29068 switch (valx2->val_class)
29070 case dw_val_class_const:
29071 return valx2->v.val_int == valy2->v.val_int;
29072 case dw_val_class_vec:
29073 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29074 && valx2->v.val_vec.length == valy2->v.val_vec.length
29075 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29076 valx2->v.val_vec.elt_size
29077 * valx2->v.val_vec.length) == 0;
29078 case dw_val_class_const_double:
29079 return valx2->v.val_double.low == valy2->v.val_double.low
29080 && valx2->v.val_double.high == valy2->v.val_double.high;
29081 case dw_val_class_wide_int:
29082 return *valx2->v.val_wide == *valy2->v.val_wide;
29083 default:
29084 gcc_unreachable ();
29086 case DW_OP_regval_type:
29087 case DW_OP_deref_type:
29088 case DW_OP_GNU_regval_type:
29089 case DW_OP_GNU_deref_type:
29090 return valx1->v.val_int == valy1->v.val_int
29091 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29092 case DW_OP_convert:
29093 case DW_OP_reinterpret:
29094 case DW_OP_GNU_convert:
29095 case DW_OP_GNU_reinterpret:
29096 if (valx1->val_class != valy1->val_class)
29097 return false;
29098 if (valx1->val_class == dw_val_class_unsigned_const)
29099 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29100 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29101 case DW_OP_GNU_parameter_ref:
29102 return valx1->val_class == dw_val_class_die_ref
29103 && valx1->val_class == valy1->val_class
29104 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29105 default:
29106 /* Other codes have no operands. */
29107 return true;
29111 /* Return true if DWARF location expressions X and Y are the same. */
29113 static inline bool
29114 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29116 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29117 if (x->dw_loc_opc != y->dw_loc_opc
29118 || x->dtprel != y->dtprel
29119 || !compare_loc_operands (x, y))
29120 break;
29121 return x == NULL && y == NULL;
29124 /* Hashtable helpers. */
29126 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29128 static inline hashval_t hash (const dw_loc_list_struct *);
29129 static inline bool equal (const dw_loc_list_struct *,
29130 const dw_loc_list_struct *);
29133 /* Return precomputed hash of location list X. */
29135 inline hashval_t
29136 loc_list_hasher::hash (const dw_loc_list_struct *x)
29138 return x->hash;
29141 /* Return true if location lists A and B are the same. */
29143 inline bool
29144 loc_list_hasher::equal (const dw_loc_list_struct *a,
29145 const dw_loc_list_struct *b)
29147 if (a == b)
29148 return 1;
29149 if (a->hash != b->hash)
29150 return 0;
29151 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29152 if (strcmp (a->begin, b->begin) != 0
29153 || strcmp (a->end, b->end) != 0
29154 || (a->section == NULL) != (b->section == NULL)
29155 || (a->section && strcmp (a->section, b->section) != 0)
29156 || !compare_locs (a->expr, b->expr))
29157 break;
29158 return a == NULL && b == NULL;
29161 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29164 /* Recursively optimize location lists referenced from DIE
29165 children and share them whenever possible. */
29167 static void
29168 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29170 dw_die_ref c;
29171 dw_attr_node *a;
29172 unsigned ix;
29173 dw_loc_list_struct **slot;
29175 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29176 if (AT_class (a) == dw_val_class_loc_list)
29178 dw_loc_list_ref list = AT_loc_list (a);
29179 /* TODO: perform some optimizations here, before hashing
29180 it and storing into the hash table. */
29181 hash_loc_list (list);
29182 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29183 if (*slot == NULL)
29184 *slot = list;
29185 else
29186 a->dw_attr_val.v.val_loc_list = *slot;
29189 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29193 /* Recursively assign each location list a unique index into the debug_addr
29194 section. */
29196 static void
29197 index_location_lists (dw_die_ref die)
29199 dw_die_ref c;
29200 dw_attr_node *a;
29201 unsigned ix;
29203 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29204 if (AT_class (a) == dw_val_class_loc_list)
29206 dw_loc_list_ref list = AT_loc_list (a);
29207 dw_loc_list_ref curr;
29208 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29210 /* Don't index an entry that has already been indexed
29211 or won't be output. */
29212 if (curr->begin_entry != NULL
29213 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29214 continue;
29216 curr->begin_entry
29217 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29221 FOR_EACH_CHILD (die, c, index_location_lists (c));
29224 /* Optimize location lists referenced from DIE
29225 children and share them whenever possible. */
29227 static void
29228 optimize_location_lists (dw_die_ref die)
29230 loc_list_hash_type htab (500);
29231 optimize_location_lists_1 (die, &htab);
29234 /* Traverse the limbo die list, and add parent/child links. The only
29235 dies without parents that should be here are concrete instances of
29236 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29237 For concrete instances, we can get the parent die from the abstract
29238 instance. */
29240 static void
29241 flush_limbo_die_list (void)
29243 limbo_die_node *node;
29245 /* get_context_die calls force_decl_die, which can put new DIEs on the
29246 limbo list in LTO mode when nested functions are put in a different
29247 partition than that of their parent function. */
29248 while ((node = limbo_die_list))
29250 dw_die_ref die = node->die;
29251 limbo_die_list = node->next;
29253 if (die->die_parent == NULL)
29255 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29257 if (origin && origin->die_parent)
29258 add_child_die (origin->die_parent, die);
29259 else if (is_cu_die (die))
29261 else if (seen_error ())
29262 /* It's OK to be confused by errors in the input. */
29263 add_child_die (comp_unit_die (), die);
29264 else
29266 /* In certain situations, the lexical block containing a
29267 nested function can be optimized away, which results
29268 in the nested function die being orphaned. Likewise
29269 with the return type of that nested function. Force
29270 this to be a child of the containing function.
29272 It may happen that even the containing function got fully
29273 inlined and optimized out. In that case we are lost and
29274 assign the empty child. This should not be big issue as
29275 the function is likely unreachable too. */
29276 gcc_assert (node->created_for);
29278 if (DECL_P (node->created_for))
29279 origin = get_context_die (DECL_CONTEXT (node->created_for));
29280 else if (TYPE_P (node->created_for))
29281 origin = scope_die_for (node->created_for, comp_unit_die ());
29282 else
29283 origin = comp_unit_die ();
29285 add_child_die (origin, die);
29291 /* Output stuff that dwarf requires at the end of every file,
29292 and generate the DWARF-2 debugging info. */
29294 static void
29295 dwarf2out_finish (const char *)
29297 comdat_type_node *ctnode;
29298 dw_die_ref main_comp_unit_die;
29299 unsigned char checksum[16];
29301 /* Flush out any latecomers to the limbo party. */
29302 flush_limbo_die_list ();
29304 if (flag_checking)
29306 verify_die (comp_unit_die ());
29307 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29308 verify_die (node->die);
29311 /* We shouldn't have any symbols with delayed asm names for
29312 DIEs generated after early finish. */
29313 gcc_assert (deferred_asm_name == NULL);
29315 gen_remaining_tmpl_value_param_die_attribute ();
29317 #if ENABLE_ASSERT_CHECKING
29319 dw_die_ref die = comp_unit_die (), c;
29320 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29322 #endif
29323 resolve_addr (comp_unit_die ());
29324 move_marked_base_types ();
29326 /* Initialize sections and labels used for actual assembler output. */
29327 init_sections_and_labels ();
29329 /* Traverse the DIE's and add sibling attributes to those DIE's that
29330 have children. */
29331 add_sibling_attributes (comp_unit_die ());
29332 limbo_die_node *node;
29333 for (node = cu_die_list; node; node = node->next)
29334 add_sibling_attributes (node->die);
29335 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29336 add_sibling_attributes (ctnode->root_die);
29338 /* When splitting DWARF info, we put some attributes in the
29339 skeleton compile_unit DIE that remains in the .o, while
29340 most attributes go in the DWO compile_unit_die. */
29341 if (dwarf_split_debug_info)
29343 limbo_die_node *cu;
29344 main_comp_unit_die = gen_compile_unit_die (NULL);
29345 if (dwarf_version >= 5)
29346 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29347 cu = limbo_die_list;
29348 gcc_assert (cu->die == main_comp_unit_die);
29349 limbo_die_list = limbo_die_list->next;
29350 cu->next = cu_die_list;
29351 cu_die_list = cu;
29353 else
29354 main_comp_unit_die = comp_unit_die ();
29356 /* Output a terminator label for the .text section. */
29357 switch_to_section (text_section);
29358 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29359 if (cold_text_section)
29361 switch_to_section (cold_text_section);
29362 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29365 /* We can only use the low/high_pc attributes if all of the code was
29366 in .text. */
29367 if (!have_multiple_function_sections
29368 || (dwarf_version < 3 && dwarf_strict))
29370 /* Don't add if the CU has no associated code. */
29371 if (text_section_used)
29372 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29373 text_end_label, true);
29375 else
29377 unsigned fde_idx;
29378 dw_fde_ref fde;
29379 bool range_list_added = false;
29381 if (text_section_used)
29382 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29383 text_end_label, &range_list_added, true);
29384 if (cold_text_section_used)
29385 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29386 cold_end_label, &range_list_added, true);
29388 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29390 if (DECL_IGNORED_P (fde->decl))
29391 continue;
29392 if (!fde->in_std_section)
29393 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29394 fde->dw_fde_end, &range_list_added,
29395 true);
29396 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29397 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29398 fde->dw_fde_second_end, &range_list_added,
29399 true);
29402 if (range_list_added)
29404 /* We need to give .debug_loc and .debug_ranges an appropriate
29405 "base address". Use zero so that these addresses become
29406 absolute. Historically, we've emitted the unexpected
29407 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29408 Emit both to give time for other tools to adapt. */
29409 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29410 if (! dwarf_strict && dwarf_version < 4)
29411 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29413 add_ranges (NULL);
29417 if (debug_info_level >= DINFO_LEVEL_TERSE)
29418 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29419 debug_line_section_label);
29421 if (have_macinfo)
29422 add_AT_macptr (comp_unit_die (),
29423 dwarf_version >= 5 ? DW_AT_macros
29424 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29425 macinfo_section_label);
29427 if (dwarf_split_debug_info)
29429 if (have_location_lists)
29431 if (dwarf_version >= 5)
29432 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29433 loc_section_label);
29434 /* optimize_location_lists calculates the size of the lists,
29435 so index them first, and assign indices to the entries.
29436 Although optimize_location_lists will remove entries from
29437 the table, it only does so for duplicates, and therefore
29438 only reduces ref_counts to 1. */
29439 index_location_lists (comp_unit_die ());
29442 if (addr_index_table != NULL)
29444 unsigned int index = 0;
29445 addr_index_table
29446 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29447 (&index);
29451 loc_list_idx = 0;
29452 if (have_location_lists)
29454 optimize_location_lists (comp_unit_die ());
29455 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29456 if (dwarf_version >= 5 && dwarf_split_debug_info)
29457 assign_location_list_indexes (comp_unit_die ());
29460 save_macinfo_strings ();
29462 if (dwarf_split_debug_info)
29464 unsigned int index = 0;
29466 /* Add attributes common to skeleton compile_units and
29467 type_units. Because these attributes include strings, it
29468 must be done before freezing the string table. Top-level
29469 skeleton die attrs are added when the skeleton type unit is
29470 created, so ensure it is created by this point. */
29471 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29472 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29475 /* Output all of the compilation units. We put the main one last so that
29476 the offsets are available to output_pubnames. */
29477 for (node = cu_die_list; node; node = node->next)
29478 output_comp_unit (node->die, 0, NULL);
29480 hash_table<comdat_type_hasher> comdat_type_table (100);
29481 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29483 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29485 /* Don't output duplicate types. */
29486 if (*slot != HTAB_EMPTY_ENTRY)
29487 continue;
29489 /* Add a pointer to the line table for the main compilation unit
29490 so that the debugger can make sense of DW_AT_decl_file
29491 attributes. */
29492 if (debug_info_level >= DINFO_LEVEL_TERSE)
29493 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29494 (!dwarf_split_debug_info
29495 ? debug_line_section_label
29496 : debug_skeleton_line_section_label));
29498 output_comdat_type_unit (ctnode);
29499 *slot = ctnode;
29502 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29503 both the main_cu and all skeleton TUs. Making this call unconditional
29504 would end up either adding a second copy of the AT_pubnames attribute, or
29505 requiring a special case in add_top_level_skeleton_die_attrs. */
29506 if (!dwarf_split_debug_info)
29507 add_AT_pubnames (comp_unit_die ());
29509 if (dwarf_split_debug_info)
29511 int mark;
29512 struct md5_ctx ctx;
29514 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29515 index_rnglists ();
29517 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29518 md5_init_ctx (&ctx);
29519 mark = 0;
29520 die_checksum (comp_unit_die (), &ctx, &mark);
29521 unmark_all_dies (comp_unit_die ());
29522 md5_finish_ctx (&ctx, checksum);
29524 if (dwarf_version < 5)
29526 /* Use the first 8 bytes of the checksum as the dwo_id,
29527 and add it to both comp-unit DIEs. */
29528 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29529 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29532 /* Add the base offset of the ranges table to the skeleton
29533 comp-unit DIE. */
29534 if (!vec_safe_is_empty (ranges_table))
29536 if (dwarf_version >= 5)
29537 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29538 ranges_base_label);
29539 else
29540 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29541 ranges_section_label);
29544 switch_to_section (debug_addr_section);
29545 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29546 output_addr_table ();
29549 /* Output the main compilation unit if non-empty or if .debug_macinfo
29550 or .debug_macro will be emitted. */
29551 output_comp_unit (comp_unit_die (), have_macinfo,
29552 dwarf_split_debug_info ? checksum : NULL);
29554 if (dwarf_split_debug_info && info_section_emitted)
29555 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29557 /* Output the abbreviation table. */
29558 if (vec_safe_length (abbrev_die_table) != 1)
29560 switch_to_section (debug_abbrev_section);
29561 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29562 output_abbrev_section ();
29565 /* Output location list section if necessary. */
29566 if (have_location_lists)
29568 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29569 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29570 /* Output the location lists info. */
29571 switch_to_section (debug_loc_section);
29572 if (dwarf_version >= 5)
29574 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29575 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29576 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29577 dw2_asm_output_data (4, 0xffffffff,
29578 "Initial length escape value indicating "
29579 "64-bit DWARF extension");
29580 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29581 "Length of Location Lists");
29582 ASM_OUTPUT_LABEL (asm_out_file, l1);
29583 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29584 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29585 dw2_asm_output_data (1, 0, "Segment Size");
29586 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29587 "Offset Entry Count");
29589 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29590 if (dwarf_version >= 5 && dwarf_split_debug_info)
29592 unsigned int save_loc_list_idx = loc_list_idx;
29593 loc_list_idx = 0;
29594 output_loclists_offsets (comp_unit_die ());
29595 gcc_assert (save_loc_list_idx == loc_list_idx);
29597 output_location_lists (comp_unit_die ());
29598 if (dwarf_version >= 5)
29599 ASM_OUTPUT_LABEL (asm_out_file, l2);
29602 output_pubtables ();
29604 /* Output the address range information if a CU (.debug_info section)
29605 was emitted. We output an empty table even if we had no functions
29606 to put in it. This because the consumer has no way to tell the
29607 difference between an empty table that we omitted and failure to
29608 generate a table that would have contained data. */
29609 if (info_section_emitted)
29611 switch_to_section (debug_aranges_section);
29612 output_aranges ();
29615 /* Output ranges section if necessary. */
29616 if (!vec_safe_is_empty (ranges_table))
29618 if (dwarf_version >= 5)
29619 output_rnglists ();
29620 else
29621 output_ranges ();
29624 /* Have to end the macro section. */
29625 if (have_macinfo)
29627 switch_to_section (debug_macinfo_section);
29628 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29629 output_macinfo ();
29630 dw2_asm_output_data (1, 0, "End compilation unit");
29633 /* Output the source line correspondence table. We must do this
29634 even if there is no line information. Otherwise, on an empty
29635 translation unit, we will generate a present, but empty,
29636 .debug_info section. IRIX 6.5 `nm' will then complain when
29637 examining the file. This is done late so that any filenames
29638 used by the debug_info section are marked as 'used'. */
29639 switch_to_section (debug_line_section);
29640 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29641 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29642 output_line_info (false);
29644 if (dwarf_split_debug_info && info_section_emitted)
29646 switch_to_section (debug_skeleton_line_section);
29647 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29648 output_line_info (true);
29651 /* If we emitted any indirect strings, output the string table too. */
29652 if (debug_str_hash || skeleton_debug_str_hash)
29653 output_indirect_strings ();
29654 if (debug_line_str_hash)
29656 switch_to_section (debug_line_str_section);
29657 const enum dwarf_form form = DW_FORM_line_strp;
29658 debug_line_str_hash->traverse<enum dwarf_form,
29659 output_indirect_string> (form);
29663 /* Perform any cleanups needed after the early debug generation pass
29664 has run. */
29666 static void
29667 dwarf2out_early_finish (const char *filename)
29669 set_early_dwarf s;
29671 /* PCH might result in DW_AT_producer string being restored from the
29672 header compilation, so always fill it with empty string initially
29673 and overwrite only here. */
29674 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
29675 producer_string = gen_producer_string ();
29676 producer->dw_attr_val.v.val_str->refcount--;
29677 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
29679 /* Add the name for the main input file now. We delayed this from
29680 dwarf2out_init to avoid complications with PCH. */
29681 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
29682 add_comp_dir_attribute (comp_unit_die ());
29684 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
29685 DW_AT_comp_dir into .debug_line_str section. */
29686 if (!DWARF2_ASM_LINE_DEBUG_INFO
29687 && dwarf_version >= 5
29688 && DWARF5_USE_DEBUG_LINE_STR)
29690 for (int i = 0; i < 2; i++)
29692 dw_attr_node *a = get_AT (comp_unit_die (),
29693 i ? DW_AT_comp_dir : DW_AT_name);
29694 if (a == NULL
29695 || AT_class (a) != dw_val_class_str
29696 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
29697 continue;
29699 if (! debug_line_str_hash)
29700 debug_line_str_hash
29701 = hash_table<indirect_string_hasher>::create_ggc (10);
29703 struct indirect_string_node *node
29704 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
29705 set_indirect_string (node);
29706 node->form = DW_FORM_line_strp;
29707 a->dw_attr_val.v.val_str->refcount--;
29708 a->dw_attr_val.v.val_str = node;
29712 /* With LTO early dwarf was really finished at compile-time, so make
29713 sure to adjust the phase after annotating the LTRANS CU DIE. */
29714 if (in_lto_p)
29716 early_dwarf_finished = true;
29717 return;
29720 /* Walk through the list of incomplete types again, trying once more to
29721 emit full debugging info for them. */
29722 retry_incomplete_types ();
29724 /* The point here is to flush out the limbo list so that it is empty
29725 and we don't need to stream it for LTO. */
29726 flush_limbo_die_list ();
29728 gen_scheduled_generic_parms_dies ();
29729 gen_remaining_tmpl_value_param_die_attribute ();
29731 /* Add DW_AT_linkage_name for all deferred DIEs. */
29732 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
29734 tree decl = node->created_for;
29735 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
29736 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
29737 ended up in deferred_asm_name before we knew it was
29738 constant and never written to disk. */
29739 && DECL_ASSEMBLER_NAME (decl))
29741 add_linkage_attr (node->die, decl);
29742 move_linkage_attr (node->die);
29745 deferred_asm_name = NULL;
29747 if (flag_eliminate_unused_debug_types)
29748 prune_unused_types ();
29750 /* Generate separate COMDAT sections for type DIEs. */
29751 if (use_debug_types)
29753 break_out_comdat_types (comp_unit_die ());
29755 /* Each new type_unit DIE was added to the limbo die list when created.
29756 Since these have all been added to comdat_type_list, clear the
29757 limbo die list. */
29758 limbo_die_list = NULL;
29760 /* For each new comdat type unit, copy declarations for incomplete
29761 types to make the new unit self-contained (i.e., no direct
29762 references to the main compile unit). */
29763 for (comdat_type_node *ctnode = comdat_type_list;
29764 ctnode != NULL; ctnode = ctnode->next)
29765 copy_decls_for_unworthy_types (ctnode->root_die);
29766 copy_decls_for_unworthy_types (comp_unit_die ());
29768 /* In the process of copying declarations from one unit to another,
29769 we may have left some declarations behind that are no longer
29770 referenced. Prune them. */
29771 prune_unused_types ();
29774 /* Generate separate CUs for each of the include files we've seen.
29775 They will go into limbo_die_list and from there to cu_die_list. */
29776 if (flag_eliminate_dwarf2_dups)
29778 gcc_assert (limbo_die_list == NULL);
29779 break_out_includes (comp_unit_die ());
29780 limbo_die_node *cu;
29781 while ((cu = limbo_die_list))
29783 limbo_die_list = cu->next;
29784 cu->next = cu_die_list;
29785 cu_die_list = cu;
29789 /* The early debug phase is now finished. */
29790 early_dwarf_finished = true;
29793 /* Reset all state within dwarf2out.c so that we can rerun the compiler
29794 within the same process. For use by toplev::finalize. */
29796 void
29797 dwarf2out_c_finalize (void)
29799 last_var_location_insn = NULL;
29800 cached_next_real_insn = NULL;
29801 used_rtx_array = NULL;
29802 incomplete_types = NULL;
29803 decl_scope_table = NULL;
29804 debug_info_section = NULL;
29805 debug_skeleton_info_section = NULL;
29806 debug_abbrev_section = NULL;
29807 debug_skeleton_abbrev_section = NULL;
29808 debug_aranges_section = NULL;
29809 debug_addr_section = NULL;
29810 debug_macinfo_section = NULL;
29811 debug_line_section = NULL;
29812 debug_skeleton_line_section = NULL;
29813 debug_loc_section = NULL;
29814 debug_pubnames_section = NULL;
29815 debug_pubtypes_section = NULL;
29816 debug_str_section = NULL;
29817 debug_line_str_section = NULL;
29818 debug_str_dwo_section = NULL;
29819 debug_str_offsets_section = NULL;
29820 debug_ranges_section = NULL;
29821 debug_frame_section = NULL;
29822 fde_vec = NULL;
29823 debug_str_hash = NULL;
29824 debug_line_str_hash = NULL;
29825 skeleton_debug_str_hash = NULL;
29826 dw2_string_counter = 0;
29827 have_multiple_function_sections = false;
29828 text_section_used = false;
29829 cold_text_section_used = false;
29830 cold_text_section = NULL;
29831 current_unit_personality = NULL;
29833 early_dwarf = false;
29834 early_dwarf_finished = false;
29836 next_die_offset = 0;
29837 single_comp_unit_die = NULL;
29838 comdat_type_list = NULL;
29839 limbo_die_list = NULL;
29840 file_table = NULL;
29841 decl_die_table = NULL;
29842 common_block_die_table = NULL;
29843 decl_loc_table = NULL;
29844 call_arg_locations = NULL;
29845 call_arg_loc_last = NULL;
29846 call_site_count = -1;
29847 tail_call_site_count = -1;
29848 cached_dw_loc_list_table = NULL;
29849 abbrev_die_table = NULL;
29850 delete dwarf_proc_stack_usage_map;
29851 dwarf_proc_stack_usage_map = NULL;
29852 line_info_label_num = 0;
29853 cur_line_info_table = NULL;
29854 text_section_line_info = NULL;
29855 cold_text_section_line_info = NULL;
29856 separate_line_info = NULL;
29857 info_section_emitted = false;
29858 pubname_table = NULL;
29859 pubtype_table = NULL;
29860 macinfo_table = NULL;
29861 ranges_table = NULL;
29862 ranges_by_label = NULL;
29863 rnglist_idx = 0;
29864 have_location_lists = false;
29865 loclabel_num = 0;
29866 poc_label_num = 0;
29867 last_emitted_file = NULL;
29868 label_num = 0;
29869 tmpl_value_parm_die_table = NULL;
29870 generic_type_instances = NULL;
29871 frame_pointer_fb_offset = 0;
29872 frame_pointer_fb_offset_valid = false;
29873 base_types.release ();
29874 XDELETEVEC (producer_string);
29875 producer_string = NULL;
29878 #include "gt-dwarf2out.h"