* es.po: Update.
[official-gcc.git] / gcc / dwarf2out.c
blob66a4919187db718954c995dcb6b21c3b28e6aa33
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx_insn *last_var_location_insn;
98 static rtx_insn *cached_next_real_insn;
99 static void dwarf2out_decl (tree);
101 #ifndef XCOFF_DEBUGGING_INFO
102 #define XCOFF_DEBUGGING_INFO 0
103 #endif
105 #ifndef HAVE_XCOFF_DWARF_EXTRAS
106 #define HAVE_XCOFF_DWARF_EXTRAS 0
107 #endif
109 #ifdef VMS_DEBUGGING_INFO
110 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
112 /* Define this macro to be a nonzero value if the directory specifications
113 which are output in the debug info should end with a separator. */
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
115 /* Define this macro to evaluate to a nonzero value if GCC should refrain
116 from generating indirect strings in DWARF2 debug information, for instance
117 if your target is stuck with an old version of GDB that is unable to
118 process them properly or uses VMS Debug. */
119 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
120 #else
121 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
123 #endif
125 /* ??? Poison these here until it can be done generically. They've been
126 totally replaced in this file; make sure it stays that way. */
127 #undef DWARF2_UNWIND_INFO
128 #undef DWARF2_FRAME_INFO
129 #if (GCC_VERSION >= 3000)
130 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
131 #endif
133 /* The size of the target's pointer type. */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
138 /* Array of RTXes referenced by the debugging information, which therefore
139 must be kept around forever. */
140 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
142 /* A pointer to the base of a list of incomplete types which might be
143 completed at some later time. incomplete_types_list needs to be a
144 vec<tree, va_gc> *because we want to tell the garbage collector about
145 it. */
146 static GTY(()) vec<tree, va_gc> *incomplete_types;
148 /* A pointer to the base of a table of references to declaration
149 scopes. This table is a display which tracks the nesting
150 of declaration scopes at the current scope and containing
151 scopes. This table is used to find the proper place to
152 define type declaration DIE's. */
153 static GTY(()) vec<tree, va_gc> *decl_scope_table;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_skeleton_info_section;
158 static GTY(()) section *debug_abbrev_section;
159 static GTY(()) section *debug_skeleton_abbrev_section;
160 static GTY(()) section *debug_aranges_section;
161 static GTY(()) section *debug_addr_section;
162 static GTY(()) section *debug_macinfo_section;
163 static const char *debug_macinfo_section_name;
164 static GTY(()) section *debug_line_section;
165 static GTY(()) section *debug_skeleton_line_section;
166 static GTY(()) section *debug_loc_section;
167 static GTY(()) section *debug_pubnames_section;
168 static GTY(()) section *debug_pubtypes_section;
169 static GTY(()) section *debug_str_section;
170 static GTY(()) section *debug_line_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_frame_section;
176 /* Maximum size (in bytes) of an artificially generated label. */
177 #define MAX_ARTIFICIAL_LABEL_BYTES 30
179 /* According to the (draft) DWARF 3 specification, the initial length
180 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
181 bytes are 0xffffffff, followed by the length stored in the next 8
182 bytes.
184 However, the SGI/MIPS ABI uses an initial length which is equal to
185 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
187 #ifndef DWARF_INITIAL_LENGTH_SIZE
188 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
189 #endif
191 /* Round SIZE up to the nearest BOUNDARY. */
192 #define DWARF_ROUND(SIZE,BOUNDARY) \
193 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
195 /* CIE identifier. */
196 #if HOST_BITS_PER_WIDE_INT >= 64
197 #define DWARF_CIE_ID \
198 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
199 #else
200 #define DWARF_CIE_ID DW_CIE_ID
201 #endif
204 /* A vector for a table that contains frame description
205 information for each routine. */
206 #define NOT_INDEXED (-1U)
207 #define NO_INDEX_ASSIGNED (-2U)
209 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
211 struct GTY((for_user)) indirect_string_node {
212 const char *str;
213 unsigned int refcount;
214 enum dwarf_form form;
215 char *label;
216 unsigned int index;
219 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
221 typedef const char *compare_type;
223 static hashval_t hash (indirect_string_node *);
224 static bool equal (indirect_string_node *, const char *);
227 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
229 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
231 /* With split_debug_info, both the comp_dir and dwo_name go in the
232 main object file, rather than the dwo, similar to the force_direct
233 parameter elsewhere but with additional complications:
235 1) The string is needed in both the main object file and the dwo.
236 That is, the comp_dir and dwo_name will appear in both places.
238 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
239 DW_FORM_line_strp or DW_FORM_GNU_str_index.
241 3) GCC chooses the form to use late, depending on the size and
242 reference count.
244 Rather than forcing the all debug string handling functions and
245 callers to deal with these complications, simply use a separate,
246 special-cased string table for any attribute that should go in the
247 main object file. This limits the complexity to just the places
248 that need it. */
250 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
252 static GTY(()) int dw2_string_counter;
254 /* True if the compilation unit places functions in more than one section. */
255 static GTY(()) bool have_multiple_function_sections = false;
257 /* Whether the default text and cold text sections have been used at all. */
259 static GTY(()) bool text_section_used = false;
260 static GTY(()) bool cold_text_section_used = false;
262 /* The default cold text section. */
263 static GTY(()) section *cold_text_section;
265 /* The DIE for C++14 'auto' in a function return type. */
266 static GTY(()) dw_die_ref auto_die;
268 /* The DIE for C++14 'decltype(auto)' in a function return type. */
269 static GTY(()) dw_die_ref decltype_auto_die;
271 /* Forward declarations for functions defined in this file. */
273 static void output_call_frame_info (int);
274 static void dwarf2out_note_section_used (void);
276 /* Personality decl of current unit. Used only when assembler does not support
277 personality CFI. */
278 static GTY(()) rtx current_unit_personality;
280 /* .debug_rnglists next index. */
281 static unsigned int rnglist_idx;
283 /* Data and reference forms for relocatable data. */
284 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
285 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
287 #ifndef DEBUG_FRAME_SECTION
288 #define DEBUG_FRAME_SECTION ".debug_frame"
289 #endif
291 #ifndef FUNC_BEGIN_LABEL
292 #define FUNC_BEGIN_LABEL "LFB"
293 #endif
295 #ifndef FUNC_END_LABEL
296 #define FUNC_END_LABEL "LFE"
297 #endif
299 #ifndef PROLOGUE_END_LABEL
300 #define PROLOGUE_END_LABEL "LPE"
301 #endif
303 #ifndef EPILOGUE_BEGIN_LABEL
304 #define EPILOGUE_BEGIN_LABEL "LEB"
305 #endif
307 #ifndef FRAME_BEGIN_LABEL
308 #define FRAME_BEGIN_LABEL "Lframe"
309 #endif
310 #define CIE_AFTER_SIZE_LABEL "LSCIE"
311 #define CIE_END_LABEL "LECIE"
312 #define FDE_LABEL "LSFDE"
313 #define FDE_AFTER_SIZE_LABEL "LASFDE"
314 #define FDE_END_LABEL "LEFDE"
315 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
316 #define LINE_NUMBER_END_LABEL "LELT"
317 #define LN_PROLOG_AS_LABEL "LASLTP"
318 #define LN_PROLOG_END_LABEL "LELTP"
319 #define DIE_LABEL_PREFIX "DW"
321 /* Match the base name of a file to the base name of a compilation unit. */
323 static int
324 matches_main_base (const char *path)
326 /* Cache the last query. */
327 static const char *last_path = NULL;
328 static int last_match = 0;
329 if (path != last_path)
331 const char *base;
332 int length = base_of_path (path, &base);
333 last_path = path;
334 last_match = (length == main_input_baselength
335 && memcmp (base, main_input_basename, length) == 0);
337 return last_match;
340 #ifdef DEBUG_DEBUG_STRUCT
342 static int
343 dump_struct_debug (tree type, enum debug_info_usage usage,
344 enum debug_struct_file criterion, int generic,
345 int matches, int result)
347 /* Find the type name. */
348 tree type_decl = TYPE_STUB_DECL (type);
349 tree t = type_decl;
350 const char *name = 0;
351 if (TREE_CODE (t) == TYPE_DECL)
352 t = DECL_NAME (t);
353 if (t)
354 name = IDENTIFIER_POINTER (t);
356 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
357 criterion,
358 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
359 matches ? "bas" : "hdr",
360 generic ? "gen" : "ord",
361 usage == DINFO_USAGE_DFN ? ";" :
362 usage == DINFO_USAGE_DIR_USE ? "." : "*",
363 result,
364 (void*) type_decl, name);
365 return result;
367 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
368 dump_struct_debug (type, usage, criterion, generic, matches, result)
370 #else
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 (result)
375 #endif
377 /* Get the number of HOST_WIDE_INTs needed to represent the precision
378 of the number. Some constants have a large uniform precision, so
379 we get the precision needed for the actual value of the number. */
381 static unsigned int
382 get_full_len (const wide_int &op)
384 int prec = wi::min_precision (op, UNSIGNED);
385 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
386 / HOST_BITS_PER_WIDE_INT);
389 static bool
390 should_emit_struct_debug (tree type, enum debug_info_usage usage)
392 enum debug_struct_file criterion;
393 tree type_decl;
394 bool generic = lang_hooks.types.generic_p (type);
396 if (generic)
397 criterion = debug_struct_generic[usage];
398 else
399 criterion = debug_struct_ordinary[usage];
401 if (criterion == DINFO_STRUCT_FILE_NONE)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
403 if (criterion == DINFO_STRUCT_FILE_ANY)
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
408 if (type_decl != NULL)
410 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
413 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
414 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
417 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
420 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
421 switch to the data section instead, and write out a synthetic start label
422 for collect2 the first time around. */
424 static void
425 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
427 if (eh_frame_section == 0)
429 int flags;
431 if (EH_TABLES_CAN_BE_READ_ONLY)
433 int fde_encoding;
434 int per_encoding;
435 int lsda_encoding;
437 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
438 /*global=*/0);
439 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
440 /*global=*/1);
441 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
442 /*global=*/0);
443 flags = ((! flag_pic
444 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
445 && (fde_encoding & 0x70) != DW_EH_PE_aligned
446 && (per_encoding & 0x70) != DW_EH_PE_absptr
447 && (per_encoding & 0x70) != DW_EH_PE_aligned
448 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
449 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
450 ? 0 : SECTION_WRITE);
452 else
453 flags = SECTION_WRITE;
455 #ifdef EH_FRAME_SECTION_NAME
456 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
457 #else
458 eh_frame_section = ((flags == SECTION_WRITE)
459 ? data_section : readonly_data_section);
460 #endif /* EH_FRAME_SECTION_NAME */
463 switch_to_section (eh_frame_section);
465 #ifdef EH_FRAME_THROUGH_COLLECT2
466 /* We have no special eh_frame section. Emit special labels to guide
467 collect2. */
468 if (!back)
470 tree label = get_file_function_name ("F");
471 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
472 targetm.asm_out.globalize_label (asm_out_file,
473 IDENTIFIER_POINTER (label));
474 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
476 #endif
479 /* Switch [BACK] to the eh or debug frame table section, depending on
480 FOR_EH. */
482 static void
483 switch_to_frame_table_section (int for_eh, bool back)
485 if (for_eh)
486 switch_to_eh_frame_section (back);
487 else
489 if (!debug_frame_section)
490 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
491 SECTION_DEBUG, NULL);
492 switch_to_section (debug_frame_section);
496 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
498 enum dw_cfi_oprnd_type
499 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
501 switch (cfi)
503 case DW_CFA_nop:
504 case DW_CFA_GNU_window_save:
505 case DW_CFA_remember_state:
506 case DW_CFA_restore_state:
507 return dw_cfi_oprnd_unused;
509 case DW_CFA_set_loc:
510 case DW_CFA_advance_loc1:
511 case DW_CFA_advance_loc2:
512 case DW_CFA_advance_loc4:
513 case DW_CFA_MIPS_advance_loc8:
514 return dw_cfi_oprnd_addr;
516 case DW_CFA_offset:
517 case DW_CFA_offset_extended:
518 case DW_CFA_def_cfa:
519 case DW_CFA_offset_extended_sf:
520 case DW_CFA_def_cfa_sf:
521 case DW_CFA_restore:
522 case DW_CFA_restore_extended:
523 case DW_CFA_undefined:
524 case DW_CFA_same_value:
525 case DW_CFA_def_cfa_register:
526 case DW_CFA_register:
527 case DW_CFA_expression:
528 case DW_CFA_val_expression:
529 return dw_cfi_oprnd_reg_num;
531 case DW_CFA_def_cfa_offset:
532 case DW_CFA_GNU_args_size:
533 case DW_CFA_def_cfa_offset_sf:
534 return dw_cfi_oprnd_offset;
536 case DW_CFA_def_cfa_expression:
537 return dw_cfi_oprnd_loc;
539 default:
540 gcc_unreachable ();
544 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
546 enum dw_cfi_oprnd_type
547 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
549 switch (cfi)
551 case DW_CFA_def_cfa:
552 case DW_CFA_def_cfa_sf:
553 case DW_CFA_offset:
554 case DW_CFA_offset_extended_sf:
555 case DW_CFA_offset_extended:
556 return dw_cfi_oprnd_offset;
558 case DW_CFA_register:
559 return dw_cfi_oprnd_reg_num;
561 case DW_CFA_expression:
562 case DW_CFA_val_expression:
563 return dw_cfi_oprnd_loc;
565 default:
566 return dw_cfi_oprnd_unused;
570 /* Output one FDE. */
572 static void
573 output_fde (dw_fde_ref fde, bool for_eh, bool second,
574 char *section_start_label, int fde_encoding, char *augmentation,
575 bool any_lsda_needed, int lsda_encoding)
577 const char *begin, *end;
578 static unsigned int j;
579 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
581 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
582 /* empty */ 0);
583 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
584 for_eh + j);
585 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
587 if (!XCOFF_DEBUGGING_INFO || for_eh)
589 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
590 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
591 " indicating 64-bit DWARF extension");
592 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
593 "FDE Length");
595 ASM_OUTPUT_LABEL (asm_out_file, l1);
597 if (for_eh)
598 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
599 else
600 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
601 debug_frame_section, "FDE CIE offset");
603 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
604 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
606 if (for_eh)
608 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
609 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
610 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
611 "FDE initial location");
612 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
613 end, begin, "FDE address range");
615 else
617 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
618 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
621 if (augmentation[0])
623 if (any_lsda_needed)
625 int size = size_of_encoded_value (lsda_encoding);
627 if (lsda_encoding == DW_EH_PE_aligned)
629 int offset = ( 4 /* Length */
630 + 4 /* CIE offset */
631 + 2 * size_of_encoded_value (fde_encoding)
632 + 1 /* Augmentation size */ );
633 int pad = -offset & (PTR_SIZE - 1);
635 size += pad;
636 gcc_assert (size_of_uleb128 (size) == 1);
639 dw2_asm_output_data_uleb128 (size, "Augmentation size");
641 if (fde->uses_eh_lsda)
643 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
644 fde->funcdef_number);
645 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
646 gen_rtx_SYMBOL_REF (Pmode, l1),
647 false,
648 "Language Specific Data Area");
650 else
652 if (lsda_encoding == DW_EH_PE_aligned)
653 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
654 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
655 "Language Specific Data Area (none)");
658 else
659 dw2_asm_output_data_uleb128 (0, "Augmentation size");
662 /* Loop through the Call Frame Instructions associated with this FDE. */
663 fde->dw_fde_current_label = begin;
665 size_t from, until, i;
667 from = 0;
668 until = vec_safe_length (fde->dw_fde_cfi);
670 if (fde->dw_fde_second_begin == NULL)
672 else if (!second)
673 until = fde->dw_fde_switch_cfi_index;
674 else
675 from = fde->dw_fde_switch_cfi_index;
677 for (i = from; i < until; i++)
678 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
681 /* If we are to emit a ref/link from function bodies to their frame tables,
682 do it now. This is typically performed to make sure that tables
683 associated with functions are dragged with them and not discarded in
684 garbage collecting links. We need to do this on a per function basis to
685 cope with -ffunction-sections. */
687 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
688 /* Switch to the function section, emit the ref to the tables, and
689 switch *back* into the table section. */
690 switch_to_section (function_section (fde->decl));
691 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
692 switch_to_frame_table_section (for_eh, true);
693 #endif
695 /* Pad the FDE out to an address sized boundary. */
696 ASM_OUTPUT_ALIGN (asm_out_file,
697 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
698 ASM_OUTPUT_LABEL (asm_out_file, l2);
700 j += 2;
703 /* Return true if frame description entry FDE is needed for EH. */
705 static bool
706 fde_needed_for_eh_p (dw_fde_ref fde)
708 if (flag_asynchronous_unwind_tables)
709 return true;
711 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
712 return true;
714 if (fde->uses_eh_lsda)
715 return true;
717 /* If exceptions are enabled, we have collected nothrow info. */
718 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
719 return false;
721 return true;
724 /* Output the call frame information used to record information
725 that relates to calculating the frame pointer, and records the
726 location of saved registers. */
728 static void
729 output_call_frame_info (int for_eh)
731 unsigned int i;
732 dw_fde_ref fde;
733 dw_cfi_ref cfi;
734 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
735 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
736 bool any_lsda_needed = false;
737 char augmentation[6];
738 int augmentation_size;
739 int fde_encoding = DW_EH_PE_absptr;
740 int per_encoding = DW_EH_PE_absptr;
741 int lsda_encoding = DW_EH_PE_absptr;
742 int return_reg;
743 rtx personality = NULL;
744 int dw_cie_version;
746 /* Don't emit a CIE if there won't be any FDEs. */
747 if (!fde_vec)
748 return;
750 /* Nothing to do if the assembler's doing it all. */
751 if (dwarf2out_do_cfi_asm ())
752 return;
754 /* If we don't have any functions we'll want to unwind out of, don't emit
755 any EH unwind information. If we make FDEs linkonce, we may have to
756 emit an empty label for an FDE that wouldn't otherwise be emitted. We
757 want to avoid having an FDE kept around when the function it refers to
758 is discarded. Example where this matters: a primary function template
759 in C++ requires EH information, an explicit specialization doesn't. */
760 if (for_eh)
762 bool any_eh_needed = false;
764 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
766 if (fde->uses_eh_lsda)
767 any_eh_needed = any_lsda_needed = true;
768 else if (fde_needed_for_eh_p (fde))
769 any_eh_needed = true;
770 else if (TARGET_USES_WEAK_UNWIND_INFO)
771 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
774 if (!any_eh_needed)
775 return;
778 /* We're going to be generating comments, so turn on app. */
779 if (flag_debug_asm)
780 app_enable ();
782 /* Switch to the proper frame section, first time. */
783 switch_to_frame_table_section (for_eh, false);
785 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
786 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
788 /* Output the CIE. */
789 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
790 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
791 if (!XCOFF_DEBUGGING_INFO || for_eh)
793 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
794 dw2_asm_output_data (4, 0xffffffff,
795 "Initial length escape value indicating 64-bit DWARF extension");
796 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
797 "Length of Common Information Entry");
799 ASM_OUTPUT_LABEL (asm_out_file, l1);
801 /* Now that the CIE pointer is PC-relative for EH,
802 use 0 to identify the CIE. */
803 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
804 (for_eh ? 0 : DWARF_CIE_ID),
805 "CIE Identifier Tag");
807 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
808 use CIE version 1, unless that would produce incorrect results
809 due to overflowing the return register column. */
810 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
811 dw_cie_version = 1;
812 if (return_reg >= 256 || dwarf_version > 2)
813 dw_cie_version = 3;
814 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
816 augmentation[0] = 0;
817 augmentation_size = 0;
819 personality = current_unit_personality;
820 if (for_eh)
822 char *p;
824 /* Augmentation:
825 z Indicates that a uleb128 is present to size the
826 augmentation section.
827 L Indicates the encoding (and thus presence) of
828 an LSDA pointer in the FDE augmentation.
829 R Indicates a non-default pointer encoding for
830 FDE code pointers.
831 P Indicates the presence of an encoding + language
832 personality routine in the CIE augmentation. */
834 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
835 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
836 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
838 p = augmentation + 1;
839 if (personality)
841 *p++ = 'P';
842 augmentation_size += 1 + size_of_encoded_value (per_encoding);
843 assemble_external_libcall (personality);
845 if (any_lsda_needed)
847 *p++ = 'L';
848 augmentation_size += 1;
850 if (fde_encoding != DW_EH_PE_absptr)
852 *p++ = 'R';
853 augmentation_size += 1;
855 if (p > augmentation + 1)
857 augmentation[0] = 'z';
858 *p = '\0';
861 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
862 if (personality && per_encoding == DW_EH_PE_aligned)
864 int offset = ( 4 /* Length */
865 + 4 /* CIE Id */
866 + 1 /* CIE version */
867 + strlen (augmentation) + 1 /* Augmentation */
868 + size_of_uleb128 (1) /* Code alignment */
869 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
870 + 1 /* RA column */
871 + 1 /* Augmentation size */
872 + 1 /* Personality encoding */ );
873 int pad = -offset & (PTR_SIZE - 1);
875 augmentation_size += pad;
877 /* Augmentations should be small, so there's scarce need to
878 iterate for a solution. Die if we exceed one uleb128 byte. */
879 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
883 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
884 if (dw_cie_version >= 4)
886 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
887 dw2_asm_output_data (1, 0, "CIE Segment Size");
889 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
890 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
891 "CIE Data Alignment Factor");
893 if (dw_cie_version == 1)
894 dw2_asm_output_data (1, return_reg, "CIE RA Column");
895 else
896 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
898 if (augmentation[0])
900 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
901 if (personality)
903 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
904 eh_data_format_name (per_encoding));
905 dw2_asm_output_encoded_addr_rtx (per_encoding,
906 personality,
907 true, NULL);
910 if (any_lsda_needed)
911 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
912 eh_data_format_name (lsda_encoding));
914 if (fde_encoding != DW_EH_PE_absptr)
915 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
916 eh_data_format_name (fde_encoding));
919 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
920 output_cfi (cfi, NULL, for_eh);
922 /* Pad the CIE out to an address sized boundary. */
923 ASM_OUTPUT_ALIGN (asm_out_file,
924 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
925 ASM_OUTPUT_LABEL (asm_out_file, l2);
927 /* Loop through all of the FDE's. */
928 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
930 unsigned int k;
932 /* Don't emit EH unwind info for leaf functions that don't need it. */
933 if (for_eh && !fde_needed_for_eh_p (fde))
934 continue;
936 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
937 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
938 augmentation, any_lsda_needed, lsda_encoding);
941 if (for_eh && targetm.terminate_dw2_eh_frame_info)
942 dw2_asm_output_data (4, 0, "End of Table");
944 /* Turn off app to make assembly quicker. */
945 if (flag_debug_asm)
946 app_disable ();
949 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
951 static void
952 dwarf2out_do_cfi_startproc (bool second)
954 int enc;
955 rtx ref;
956 rtx personality = get_personality_function (current_function_decl);
958 fprintf (asm_out_file, "\t.cfi_startproc\n");
960 if (personality)
962 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
963 ref = personality;
965 /* ??? The GAS support isn't entirely consistent. We have to
966 handle indirect support ourselves, but PC-relative is done
967 in the assembler. Further, the assembler can't handle any
968 of the weirder relocation types. */
969 if (enc & DW_EH_PE_indirect)
970 ref = dw2_force_const_mem (ref, true);
972 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
973 output_addr_const (asm_out_file, ref);
974 fputc ('\n', asm_out_file);
977 if (crtl->uses_eh_lsda)
979 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
981 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
982 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
983 current_function_funcdef_no);
984 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
985 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
987 if (enc & DW_EH_PE_indirect)
988 ref = dw2_force_const_mem (ref, true);
990 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
991 output_addr_const (asm_out_file, ref);
992 fputc ('\n', asm_out_file);
996 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
997 this allocation may be done before pass_final. */
999 dw_fde_ref
1000 dwarf2out_alloc_current_fde (void)
1002 dw_fde_ref fde;
1004 fde = ggc_cleared_alloc<dw_fde_node> ();
1005 fde->decl = current_function_decl;
1006 fde->funcdef_number = current_function_funcdef_no;
1007 fde->fde_index = vec_safe_length (fde_vec);
1008 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1009 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1010 fde->nothrow = crtl->nothrow;
1011 fde->drap_reg = INVALID_REGNUM;
1012 fde->vdrap_reg = INVALID_REGNUM;
1014 /* Record the FDE associated with this function. */
1015 cfun->fde = fde;
1016 vec_safe_push (fde_vec, fde);
1018 return fde;
1021 /* Output a marker (i.e. a label) for the beginning of a function, before
1022 the prologue. */
1024 void
1025 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1026 const char *file ATTRIBUTE_UNUSED)
1028 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1029 char * dup_label;
1030 dw_fde_ref fde;
1031 section *fnsec;
1032 bool do_frame;
1034 current_function_func_begin_label = NULL;
1036 do_frame = dwarf2out_do_frame ();
1038 /* ??? current_function_func_begin_label is also used by except.c for
1039 call-site information. We must emit this label if it might be used. */
1040 if (!do_frame
1041 && (!flag_exceptions
1042 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1043 return;
1045 fnsec = function_section (current_function_decl);
1046 switch_to_section (fnsec);
1047 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1048 current_function_funcdef_no);
1049 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1050 current_function_funcdef_no);
1051 dup_label = xstrdup (label);
1052 current_function_func_begin_label = dup_label;
1054 /* We can elide the fde allocation if we're not emitting debug info. */
1055 if (!do_frame)
1056 return;
1058 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1059 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1060 would include pass_dwarf2_frame. If we've not created the FDE yet,
1061 do so now. */
1062 fde = cfun->fde;
1063 if (fde == NULL)
1064 fde = dwarf2out_alloc_current_fde ();
1066 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1067 fde->dw_fde_begin = dup_label;
1068 fde->dw_fde_current_label = dup_label;
1069 fde->in_std_section = (fnsec == text_section
1070 || (cold_text_section && fnsec == cold_text_section));
1072 /* We only want to output line number information for the genuine dwarf2
1073 prologue case, not the eh frame case. */
1074 #ifdef DWARF2_DEBUGGING_INFO
1075 if (file)
1076 dwarf2out_source_line (line, file, 0, true);
1077 #endif
1079 if (dwarf2out_do_cfi_asm ())
1080 dwarf2out_do_cfi_startproc (false);
1081 else
1083 rtx personality = get_personality_function (current_function_decl);
1084 if (!current_unit_personality)
1085 current_unit_personality = personality;
1087 /* We cannot keep a current personality per function as without CFI
1088 asm, at the point where we emit the CFI data, there is no current
1089 function anymore. */
1090 if (personality && current_unit_personality != personality)
1091 sorry ("multiple EH personalities are supported only with assemblers "
1092 "supporting .cfi_personality directive");
1096 /* Output a marker (i.e. a label) for the end of the generated code
1097 for a function prologue. This gets called *after* the prologue code has
1098 been generated. */
1100 void
1101 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1102 const char *file ATTRIBUTE_UNUSED)
1104 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1106 /* Output a label to mark the endpoint of the code generated for this
1107 function. */
1108 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1109 current_function_funcdef_no);
1110 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1111 current_function_funcdef_no);
1112 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1115 /* Output a marker (i.e. a label) for the beginning of the generated code
1116 for a function epilogue. This gets called *before* the prologue code has
1117 been generated. */
1119 void
1120 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1121 const char *file ATTRIBUTE_UNUSED)
1123 dw_fde_ref fde = cfun->fde;
1124 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1126 if (fde->dw_fde_vms_begin_epilogue)
1127 return;
1129 /* Output a label to mark the endpoint of the code generated for this
1130 function. */
1131 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1132 current_function_funcdef_no);
1133 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1134 current_function_funcdef_no);
1135 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1138 /* Output a marker (i.e. a label) for the absolute end of the generated code
1139 for a function definition. This gets called *after* the epilogue code has
1140 been generated. */
1142 void
1143 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1144 const char *file ATTRIBUTE_UNUSED)
1146 dw_fde_ref fde;
1147 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1149 last_var_location_insn = NULL;
1150 cached_next_real_insn = NULL;
1152 if (dwarf2out_do_cfi_asm ())
1153 fprintf (asm_out_file, "\t.cfi_endproc\n");
1155 /* Output a label to mark the endpoint of the code generated for this
1156 function. */
1157 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1158 current_function_funcdef_no);
1159 ASM_OUTPUT_LABEL (asm_out_file, label);
1160 fde = cfun->fde;
1161 gcc_assert (fde != NULL);
1162 if (fde->dw_fde_second_begin == NULL)
1163 fde->dw_fde_end = xstrdup (label);
1166 void
1167 dwarf2out_frame_finish (void)
1169 /* Output call frame information. */
1170 if (targetm.debug_unwind_info () == UI_DWARF2)
1171 output_call_frame_info (0);
1173 /* Output another copy for the unwinder. */
1174 if ((flag_unwind_tables || flag_exceptions)
1175 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1176 output_call_frame_info (1);
1179 /* Note that the current function section is being used for code. */
1181 static void
1182 dwarf2out_note_section_used (void)
1184 section *sec = current_function_section ();
1185 if (sec == text_section)
1186 text_section_used = true;
1187 else if (sec == cold_text_section)
1188 cold_text_section_used = true;
1191 static void var_location_switch_text_section (void);
1192 static void set_cur_line_info_table (section *);
1194 void
1195 dwarf2out_switch_text_section (void)
1197 section *sect;
1198 dw_fde_ref fde = cfun->fde;
1200 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1202 if (!in_cold_section_p)
1204 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1205 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1206 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1208 else
1210 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1211 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1212 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1214 have_multiple_function_sections = true;
1216 /* There is no need to mark used sections when not debugging. */
1217 if (cold_text_section != NULL)
1218 dwarf2out_note_section_used ();
1220 if (dwarf2out_do_cfi_asm ())
1221 fprintf (asm_out_file, "\t.cfi_endproc\n");
1223 /* Now do the real section switch. */
1224 sect = current_function_section ();
1225 switch_to_section (sect);
1227 fde->second_in_std_section
1228 = (sect == text_section
1229 || (cold_text_section && sect == cold_text_section));
1231 if (dwarf2out_do_cfi_asm ())
1232 dwarf2out_do_cfi_startproc (true);
1234 var_location_switch_text_section ();
1236 if (cold_text_section != NULL)
1237 set_cur_line_info_table (sect);
1240 /* And now, the subset of the debugging information support code necessary
1241 for emitting location expressions. */
1243 /* Data about a single source file. */
1244 struct GTY((for_user)) dwarf_file_data {
1245 const char * filename;
1246 int emitted_number;
1249 /* Describe an entry into the .debug_addr section. */
1251 enum ate_kind {
1252 ate_kind_rtx,
1253 ate_kind_rtx_dtprel,
1254 ate_kind_label
1257 struct GTY((for_user)) addr_table_entry {
1258 enum ate_kind kind;
1259 unsigned int refcount;
1260 unsigned int index;
1261 union addr_table_entry_struct_union
1263 rtx GTY ((tag ("0"))) rtl;
1264 char * GTY ((tag ("1"))) label;
1266 GTY ((desc ("%1.kind"))) addr;
1269 /* Location lists are ranges + location descriptions for that range,
1270 so you can track variables that are in different places over
1271 their entire life. */
1272 typedef struct GTY(()) dw_loc_list_struct {
1273 dw_loc_list_ref dw_loc_next;
1274 const char *begin; /* Label and addr_entry for start of range */
1275 addr_table_entry *begin_entry;
1276 const char *end; /* Label for end of range */
1277 char *ll_symbol; /* Label for beginning of location list.
1278 Only on head of list */
1279 const char *section; /* Section this loclist is relative to */
1280 dw_loc_descr_ref expr;
1281 hashval_t hash;
1282 /* True if all addresses in this and subsequent lists are known to be
1283 resolved. */
1284 bool resolved_addr;
1285 /* True if this list has been replaced by dw_loc_next. */
1286 bool replaced;
1287 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1288 section. */
1289 unsigned char emitted : 1;
1290 /* True if hash field is index rather than hash value. */
1291 unsigned char num_assigned : 1;
1292 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1293 unsigned char offset_emitted : 1;
1294 /* True if the range should be emitted even if begin and end
1295 are the same. */
1296 bool force;
1297 } dw_loc_list_node;
1299 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1300 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1302 /* Convert a DWARF stack opcode into its string name. */
1304 static const char *
1305 dwarf_stack_op_name (unsigned int op)
1307 const char *name = get_DW_OP_name (op);
1309 if (name != NULL)
1310 return name;
1312 return "OP_<unknown>";
1315 /* Return a pointer to a newly allocated location description. Location
1316 descriptions are simple expression terms that can be strung
1317 together to form more complicated location (address) descriptions. */
1319 static inline dw_loc_descr_ref
1320 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1321 unsigned HOST_WIDE_INT oprnd2)
1323 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1325 descr->dw_loc_opc = op;
1326 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1327 descr->dw_loc_oprnd1.val_entry = NULL;
1328 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1329 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1330 descr->dw_loc_oprnd2.val_entry = NULL;
1331 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1333 return descr;
1336 /* Return a pointer to a newly allocated location description for
1337 REG and OFFSET. */
1339 static inline dw_loc_descr_ref
1340 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1342 if (reg <= 31)
1343 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1344 offset, 0);
1345 else
1346 return new_loc_descr (DW_OP_bregx, reg, offset);
1349 /* Add a location description term to a location description expression. */
1351 static inline void
1352 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1354 dw_loc_descr_ref *d;
1356 /* Find the end of the chain. */
1357 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1360 *d = descr;
1363 /* Compare two location operands for exact equality. */
1365 static bool
1366 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1368 if (a->val_class != b->val_class)
1369 return false;
1370 switch (a->val_class)
1372 case dw_val_class_none:
1373 return true;
1374 case dw_val_class_addr:
1375 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1377 case dw_val_class_offset:
1378 case dw_val_class_unsigned_const:
1379 case dw_val_class_const:
1380 case dw_val_class_unsigned_const_implicit:
1381 case dw_val_class_const_implicit:
1382 case dw_val_class_range_list:
1383 /* These are all HOST_WIDE_INT, signed or unsigned. */
1384 return a->v.val_unsigned == b->v.val_unsigned;
1386 case dw_val_class_loc:
1387 return a->v.val_loc == b->v.val_loc;
1388 case dw_val_class_loc_list:
1389 return a->v.val_loc_list == b->v.val_loc_list;
1390 case dw_val_class_die_ref:
1391 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1392 case dw_val_class_fde_ref:
1393 return a->v.val_fde_index == b->v.val_fde_index;
1394 case dw_val_class_lbl_id:
1395 case dw_val_class_lineptr:
1396 case dw_val_class_macptr:
1397 case dw_val_class_loclistsptr:
1398 case dw_val_class_high_pc:
1399 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1400 case dw_val_class_str:
1401 return a->v.val_str == b->v.val_str;
1402 case dw_val_class_flag:
1403 return a->v.val_flag == b->v.val_flag;
1404 case dw_val_class_file:
1405 case dw_val_class_file_implicit:
1406 return a->v.val_file == b->v.val_file;
1407 case dw_val_class_decl_ref:
1408 return a->v.val_decl_ref == b->v.val_decl_ref;
1410 case dw_val_class_const_double:
1411 return (a->v.val_double.high == b->v.val_double.high
1412 && a->v.val_double.low == b->v.val_double.low);
1414 case dw_val_class_wide_int:
1415 return *a->v.val_wide == *b->v.val_wide;
1417 case dw_val_class_vec:
1419 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1420 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1422 return (a_len == b_len
1423 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1426 case dw_val_class_data8:
1427 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1429 case dw_val_class_vms_delta:
1430 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1431 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1433 case dw_val_class_discr_value:
1434 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1435 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1436 case dw_val_class_discr_list:
1437 /* It makes no sense comparing two discriminant value lists. */
1438 return false;
1440 gcc_unreachable ();
1443 /* Compare two location atoms for exact equality. */
1445 static bool
1446 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1448 if (a->dw_loc_opc != b->dw_loc_opc)
1449 return false;
1451 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1452 address size, but since we always allocate cleared storage it
1453 should be zero for other types of locations. */
1454 if (a->dtprel != b->dtprel)
1455 return false;
1457 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1458 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1461 /* Compare two complete location expressions for exact equality. */
1463 bool
1464 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1466 while (1)
1468 if (a == b)
1469 return true;
1470 if (a == NULL || b == NULL)
1471 return false;
1472 if (!loc_descr_equal_p_1 (a, b))
1473 return false;
1475 a = a->dw_loc_next;
1476 b = b->dw_loc_next;
1481 /* Add a constant OFFSET to a location expression. */
1483 static void
1484 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1486 dw_loc_descr_ref loc;
1487 HOST_WIDE_INT *p;
1489 gcc_assert (*list_head != NULL);
1491 if (!offset)
1492 return;
1494 /* Find the end of the chain. */
1495 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1498 p = NULL;
1499 if (loc->dw_loc_opc == DW_OP_fbreg
1500 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1501 p = &loc->dw_loc_oprnd1.v.val_int;
1502 else if (loc->dw_loc_opc == DW_OP_bregx)
1503 p = &loc->dw_loc_oprnd2.v.val_int;
1505 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1506 offset. Don't optimize if an signed integer overflow would happen. */
1507 if (p != NULL
1508 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1509 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1510 *p += offset;
1512 else if (offset > 0)
1513 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1515 else
1517 loc->dw_loc_next = int_loc_descriptor (-offset);
1518 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1522 /* Add a constant OFFSET to a location list. */
1524 static void
1525 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1527 dw_loc_list_ref d;
1528 for (d = list_head; d != NULL; d = d->dw_loc_next)
1529 loc_descr_plus_const (&d->expr, offset);
1532 #define DWARF_REF_SIZE \
1533 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1535 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1536 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1537 DW_FORM_data16 with 128 bits. */
1538 #define DWARF_LARGEST_DATA_FORM_BITS \
1539 (dwarf_version >= 5 ? 128 : 64)
1541 /* Utility inline function for construction of ops that were GNU extension
1542 before DWARF 5. */
1543 static inline enum dwarf_location_atom
1544 dwarf_OP (enum dwarf_location_atom op)
1546 switch (op)
1548 case DW_OP_implicit_pointer:
1549 if (dwarf_version < 5)
1550 return DW_OP_GNU_implicit_pointer;
1551 break;
1553 case DW_OP_entry_value:
1554 if (dwarf_version < 5)
1555 return DW_OP_GNU_entry_value;
1556 break;
1558 case DW_OP_const_type:
1559 if (dwarf_version < 5)
1560 return DW_OP_GNU_const_type;
1561 break;
1563 case DW_OP_regval_type:
1564 if (dwarf_version < 5)
1565 return DW_OP_GNU_regval_type;
1566 break;
1568 case DW_OP_deref_type:
1569 if (dwarf_version < 5)
1570 return DW_OP_GNU_deref_type;
1571 break;
1573 case DW_OP_convert:
1574 if (dwarf_version < 5)
1575 return DW_OP_GNU_convert;
1576 break;
1578 case DW_OP_reinterpret:
1579 if (dwarf_version < 5)
1580 return DW_OP_GNU_reinterpret;
1581 break;
1583 default:
1584 break;
1586 return op;
1589 /* Similarly for attributes. */
1590 static inline enum dwarf_attribute
1591 dwarf_AT (enum dwarf_attribute at)
1593 switch (at)
1595 case DW_AT_call_return_pc:
1596 if (dwarf_version < 5)
1597 return DW_AT_low_pc;
1598 break;
1600 case DW_AT_call_tail_call:
1601 if (dwarf_version < 5)
1602 return DW_AT_GNU_tail_call;
1603 break;
1605 case DW_AT_call_origin:
1606 if (dwarf_version < 5)
1607 return DW_AT_abstract_origin;
1608 break;
1610 case DW_AT_call_target:
1611 if (dwarf_version < 5)
1612 return DW_AT_GNU_call_site_target;
1613 break;
1615 case DW_AT_call_target_clobbered:
1616 if (dwarf_version < 5)
1617 return DW_AT_GNU_call_site_target_clobbered;
1618 break;
1620 case DW_AT_call_parameter:
1621 if (dwarf_version < 5)
1622 return DW_AT_abstract_origin;
1623 break;
1625 case DW_AT_call_value:
1626 if (dwarf_version < 5)
1627 return DW_AT_GNU_call_site_value;
1628 break;
1630 case DW_AT_call_data_value:
1631 if (dwarf_version < 5)
1632 return DW_AT_GNU_call_site_data_value;
1633 break;
1635 case DW_AT_call_all_calls:
1636 if (dwarf_version < 5)
1637 return DW_AT_GNU_all_call_sites;
1638 break;
1640 case DW_AT_call_all_tail_calls:
1641 if (dwarf_version < 5)
1642 return DW_AT_GNU_all_tail_call_sites;
1643 break;
1645 case DW_AT_dwo_name:
1646 if (dwarf_version < 5)
1647 return DW_AT_GNU_dwo_name;
1648 break;
1650 default:
1651 break;
1653 return at;
1656 /* And similarly for tags. */
1657 static inline enum dwarf_tag
1658 dwarf_TAG (enum dwarf_tag tag)
1660 switch (tag)
1662 case DW_TAG_call_site:
1663 if (dwarf_version < 5)
1664 return DW_TAG_GNU_call_site;
1665 break;
1667 case DW_TAG_call_site_parameter:
1668 if (dwarf_version < 5)
1669 return DW_TAG_GNU_call_site_parameter;
1670 break;
1672 default:
1673 break;
1675 return tag;
1678 static unsigned long int get_base_type_offset (dw_die_ref);
1680 /* Return the size of a location descriptor. */
1682 static unsigned long
1683 size_of_loc_descr (dw_loc_descr_ref loc)
1685 unsigned long size = 1;
1687 switch (loc->dw_loc_opc)
1689 case DW_OP_addr:
1690 size += DWARF2_ADDR_SIZE;
1691 break;
1692 case DW_OP_GNU_addr_index:
1693 case DW_OP_GNU_const_index:
1694 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1695 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1696 break;
1697 case DW_OP_const1u:
1698 case DW_OP_const1s:
1699 size += 1;
1700 break;
1701 case DW_OP_const2u:
1702 case DW_OP_const2s:
1703 size += 2;
1704 break;
1705 case DW_OP_const4u:
1706 case DW_OP_const4s:
1707 size += 4;
1708 break;
1709 case DW_OP_const8u:
1710 case DW_OP_const8s:
1711 size += 8;
1712 break;
1713 case DW_OP_constu:
1714 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1715 break;
1716 case DW_OP_consts:
1717 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1718 break;
1719 case DW_OP_pick:
1720 size += 1;
1721 break;
1722 case DW_OP_plus_uconst:
1723 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1724 break;
1725 case DW_OP_skip:
1726 case DW_OP_bra:
1727 size += 2;
1728 break;
1729 case DW_OP_breg0:
1730 case DW_OP_breg1:
1731 case DW_OP_breg2:
1732 case DW_OP_breg3:
1733 case DW_OP_breg4:
1734 case DW_OP_breg5:
1735 case DW_OP_breg6:
1736 case DW_OP_breg7:
1737 case DW_OP_breg8:
1738 case DW_OP_breg9:
1739 case DW_OP_breg10:
1740 case DW_OP_breg11:
1741 case DW_OP_breg12:
1742 case DW_OP_breg13:
1743 case DW_OP_breg14:
1744 case DW_OP_breg15:
1745 case DW_OP_breg16:
1746 case DW_OP_breg17:
1747 case DW_OP_breg18:
1748 case DW_OP_breg19:
1749 case DW_OP_breg20:
1750 case DW_OP_breg21:
1751 case DW_OP_breg22:
1752 case DW_OP_breg23:
1753 case DW_OP_breg24:
1754 case DW_OP_breg25:
1755 case DW_OP_breg26:
1756 case DW_OP_breg27:
1757 case DW_OP_breg28:
1758 case DW_OP_breg29:
1759 case DW_OP_breg30:
1760 case DW_OP_breg31:
1761 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1762 break;
1763 case DW_OP_regx:
1764 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1765 break;
1766 case DW_OP_fbreg:
1767 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1768 break;
1769 case DW_OP_bregx:
1770 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1771 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1772 break;
1773 case DW_OP_piece:
1774 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1775 break;
1776 case DW_OP_bit_piece:
1777 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1778 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1779 break;
1780 case DW_OP_deref_size:
1781 case DW_OP_xderef_size:
1782 size += 1;
1783 break;
1784 case DW_OP_call2:
1785 size += 2;
1786 break;
1787 case DW_OP_call4:
1788 size += 4;
1789 break;
1790 case DW_OP_call_ref:
1791 size += DWARF_REF_SIZE;
1792 break;
1793 case DW_OP_implicit_value:
1794 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1795 + loc->dw_loc_oprnd1.v.val_unsigned;
1796 break;
1797 case DW_OP_implicit_pointer:
1798 case DW_OP_GNU_implicit_pointer:
1799 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1800 break;
1801 case DW_OP_entry_value:
1802 case DW_OP_GNU_entry_value:
1804 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1805 size += size_of_uleb128 (op_size) + op_size;
1806 break;
1808 case DW_OP_const_type:
1809 case DW_OP_GNU_const_type:
1811 unsigned long o
1812 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1813 size += size_of_uleb128 (o) + 1;
1814 switch (loc->dw_loc_oprnd2.val_class)
1816 case dw_val_class_vec:
1817 size += loc->dw_loc_oprnd2.v.val_vec.length
1818 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1819 break;
1820 case dw_val_class_const:
1821 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1822 break;
1823 case dw_val_class_const_double:
1824 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1825 break;
1826 case dw_val_class_wide_int:
1827 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1828 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1829 break;
1830 default:
1831 gcc_unreachable ();
1833 break;
1835 case DW_OP_regval_type:
1836 case DW_OP_GNU_regval_type:
1838 unsigned long o
1839 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1840 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1841 + size_of_uleb128 (o);
1843 break;
1844 case DW_OP_deref_type:
1845 case DW_OP_GNU_deref_type:
1847 unsigned long o
1848 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1849 size += 1 + size_of_uleb128 (o);
1851 break;
1852 case DW_OP_convert:
1853 case DW_OP_reinterpret:
1854 case DW_OP_GNU_convert:
1855 case DW_OP_GNU_reinterpret:
1856 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1857 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1858 else
1860 unsigned long o
1861 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1862 size += size_of_uleb128 (o);
1864 break;
1865 case DW_OP_GNU_parameter_ref:
1866 size += 4;
1867 break;
1868 default:
1869 break;
1872 return size;
1875 /* Return the size of a series of location descriptors. */
1877 unsigned long
1878 size_of_locs (dw_loc_descr_ref loc)
1880 dw_loc_descr_ref l;
1881 unsigned long size;
1883 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1884 field, to avoid writing to a PCH file. */
1885 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1887 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1888 break;
1889 size += size_of_loc_descr (l);
1891 if (! l)
1892 return size;
1894 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1896 l->dw_loc_addr = size;
1897 size += size_of_loc_descr (l);
1900 return size;
1903 /* Return the size of the value in a DW_AT_discr_value attribute. */
1905 static int
1906 size_of_discr_value (dw_discr_value *discr_value)
1908 if (discr_value->pos)
1909 return size_of_uleb128 (discr_value->v.uval);
1910 else
1911 return size_of_sleb128 (discr_value->v.sval);
1914 /* Return the size of the value in a DW_AT_discr_list attribute. */
1916 static int
1917 size_of_discr_list (dw_discr_list_ref discr_list)
1919 int size = 0;
1921 for (dw_discr_list_ref list = discr_list;
1922 list != NULL;
1923 list = list->dw_discr_next)
1925 /* One byte for the discriminant value descriptor, and then one or two
1926 LEB128 numbers, depending on whether it's a single case label or a
1927 range label. */
1928 size += 1;
1929 size += size_of_discr_value (&list->dw_discr_lower_bound);
1930 if (list->dw_discr_range != 0)
1931 size += size_of_discr_value (&list->dw_discr_upper_bound);
1933 return size;
1936 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1937 static void get_ref_die_offset_label (char *, dw_die_ref);
1938 static unsigned long int get_ref_die_offset (dw_die_ref);
1940 /* Output location description stack opcode's operands (if any).
1941 The for_eh_or_skip parameter controls whether register numbers are
1942 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1943 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1944 info). This should be suppressed for the cases that have not been converted
1945 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1947 static void
1948 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1950 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1951 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1953 switch (loc->dw_loc_opc)
1955 #ifdef DWARF2_DEBUGGING_INFO
1956 case DW_OP_const2u:
1957 case DW_OP_const2s:
1958 dw2_asm_output_data (2, val1->v.val_int, NULL);
1959 break;
1960 case DW_OP_const4u:
1961 if (loc->dtprel)
1963 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1964 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1965 val1->v.val_addr);
1966 fputc ('\n', asm_out_file);
1967 break;
1969 /* FALLTHRU */
1970 case DW_OP_const4s:
1971 dw2_asm_output_data (4, val1->v.val_int, NULL);
1972 break;
1973 case DW_OP_const8u:
1974 if (loc->dtprel)
1976 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1977 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1978 val1->v.val_addr);
1979 fputc ('\n', asm_out_file);
1980 break;
1982 /* FALLTHRU */
1983 case DW_OP_const8s:
1984 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1985 dw2_asm_output_data (8, val1->v.val_int, NULL);
1986 break;
1987 case DW_OP_skip:
1988 case DW_OP_bra:
1990 int offset;
1992 gcc_assert (val1->val_class == dw_val_class_loc);
1993 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1995 dw2_asm_output_data (2, offset, NULL);
1997 break;
1998 case DW_OP_implicit_value:
1999 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2000 switch (val2->val_class)
2002 case dw_val_class_const:
2003 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2004 break;
2005 case dw_val_class_vec:
2007 unsigned int elt_size = val2->v.val_vec.elt_size;
2008 unsigned int len = val2->v.val_vec.length;
2009 unsigned int i;
2010 unsigned char *p;
2012 if (elt_size > sizeof (HOST_WIDE_INT))
2014 elt_size /= 2;
2015 len *= 2;
2017 for (i = 0, p = val2->v.val_vec.array;
2018 i < len;
2019 i++, p += elt_size)
2020 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2021 "fp or vector constant word %u", i);
2023 break;
2024 case dw_val_class_const_double:
2026 unsigned HOST_WIDE_INT first, second;
2028 if (WORDS_BIG_ENDIAN)
2030 first = val2->v.val_double.high;
2031 second = val2->v.val_double.low;
2033 else
2035 first = val2->v.val_double.low;
2036 second = val2->v.val_double.high;
2038 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2039 first, NULL);
2040 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2041 second, NULL);
2043 break;
2044 case dw_val_class_wide_int:
2046 int i;
2047 int len = get_full_len (*val2->v.val_wide);
2048 if (WORDS_BIG_ENDIAN)
2049 for (i = len - 1; i >= 0; --i)
2050 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2051 val2->v.val_wide->elt (i), NULL);
2052 else
2053 for (i = 0; i < len; ++i)
2054 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2055 val2->v.val_wide->elt (i), NULL);
2057 break;
2058 case dw_val_class_addr:
2059 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2060 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2061 break;
2062 default:
2063 gcc_unreachable ();
2065 break;
2066 #else
2067 case DW_OP_const2u:
2068 case DW_OP_const2s:
2069 case DW_OP_const4u:
2070 case DW_OP_const4s:
2071 case DW_OP_const8u:
2072 case DW_OP_const8s:
2073 case DW_OP_skip:
2074 case DW_OP_bra:
2075 case DW_OP_implicit_value:
2076 /* We currently don't make any attempt to make sure these are
2077 aligned properly like we do for the main unwind info, so
2078 don't support emitting things larger than a byte if we're
2079 only doing unwinding. */
2080 gcc_unreachable ();
2081 #endif
2082 case DW_OP_const1u:
2083 case DW_OP_const1s:
2084 dw2_asm_output_data (1, val1->v.val_int, NULL);
2085 break;
2086 case DW_OP_constu:
2087 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2088 break;
2089 case DW_OP_consts:
2090 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2091 break;
2092 case DW_OP_pick:
2093 dw2_asm_output_data (1, val1->v.val_int, NULL);
2094 break;
2095 case DW_OP_plus_uconst:
2096 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2097 break;
2098 case DW_OP_breg0:
2099 case DW_OP_breg1:
2100 case DW_OP_breg2:
2101 case DW_OP_breg3:
2102 case DW_OP_breg4:
2103 case DW_OP_breg5:
2104 case DW_OP_breg6:
2105 case DW_OP_breg7:
2106 case DW_OP_breg8:
2107 case DW_OP_breg9:
2108 case DW_OP_breg10:
2109 case DW_OP_breg11:
2110 case DW_OP_breg12:
2111 case DW_OP_breg13:
2112 case DW_OP_breg14:
2113 case DW_OP_breg15:
2114 case DW_OP_breg16:
2115 case DW_OP_breg17:
2116 case DW_OP_breg18:
2117 case DW_OP_breg19:
2118 case DW_OP_breg20:
2119 case DW_OP_breg21:
2120 case DW_OP_breg22:
2121 case DW_OP_breg23:
2122 case DW_OP_breg24:
2123 case DW_OP_breg25:
2124 case DW_OP_breg26:
2125 case DW_OP_breg27:
2126 case DW_OP_breg28:
2127 case DW_OP_breg29:
2128 case DW_OP_breg30:
2129 case DW_OP_breg31:
2130 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2131 break;
2132 case DW_OP_regx:
2134 unsigned r = val1->v.val_unsigned;
2135 if (for_eh_or_skip >= 0)
2136 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2137 gcc_assert (size_of_uleb128 (r)
2138 == size_of_uleb128 (val1->v.val_unsigned));
2139 dw2_asm_output_data_uleb128 (r, NULL);
2141 break;
2142 case DW_OP_fbreg:
2143 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2144 break;
2145 case DW_OP_bregx:
2147 unsigned r = val1->v.val_unsigned;
2148 if (for_eh_or_skip >= 0)
2149 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2150 gcc_assert (size_of_uleb128 (r)
2151 == size_of_uleb128 (val1->v.val_unsigned));
2152 dw2_asm_output_data_uleb128 (r, NULL);
2153 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2155 break;
2156 case DW_OP_piece:
2157 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2158 break;
2159 case DW_OP_bit_piece:
2160 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2161 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2162 break;
2163 case DW_OP_deref_size:
2164 case DW_OP_xderef_size:
2165 dw2_asm_output_data (1, val1->v.val_int, NULL);
2166 break;
2168 case DW_OP_addr:
2169 if (loc->dtprel)
2171 if (targetm.asm_out.output_dwarf_dtprel)
2173 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2174 DWARF2_ADDR_SIZE,
2175 val1->v.val_addr);
2176 fputc ('\n', asm_out_file);
2178 else
2179 gcc_unreachable ();
2181 else
2183 #ifdef DWARF2_DEBUGGING_INFO
2184 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2185 #else
2186 gcc_unreachable ();
2187 #endif
2189 break;
2191 case DW_OP_GNU_addr_index:
2192 case DW_OP_GNU_const_index:
2193 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2194 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2195 "(index into .debug_addr)");
2196 break;
2198 case DW_OP_call2:
2199 case DW_OP_call4:
2201 unsigned long die_offset
2202 = get_ref_die_offset (val1->v.val_die_ref.die);
2203 /* Make sure the offset has been computed and that we can encode it as
2204 an operand. */
2205 gcc_assert (die_offset > 0
2206 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2207 ? 0xffff
2208 : 0xffffffff));
2209 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2210 die_offset, NULL);
2212 break;
2214 case DW_OP_implicit_pointer:
2215 case DW_OP_GNU_implicit_pointer:
2217 char label[MAX_ARTIFICIAL_LABEL_BYTES
2218 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2219 gcc_assert (val1->val_class == dw_val_class_die_ref);
2220 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2221 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2222 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2224 break;
2226 case DW_OP_entry_value:
2227 case DW_OP_GNU_entry_value:
2228 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2229 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2230 break;
2232 case DW_OP_const_type:
2233 case DW_OP_GNU_const_type:
2235 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2236 gcc_assert (o);
2237 dw2_asm_output_data_uleb128 (o, NULL);
2238 switch (val2->val_class)
2240 case dw_val_class_const:
2241 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2242 dw2_asm_output_data (1, l, NULL);
2243 dw2_asm_output_data (l, val2->v.val_int, NULL);
2244 break;
2245 case dw_val_class_vec:
2247 unsigned int elt_size = val2->v.val_vec.elt_size;
2248 unsigned int len = val2->v.val_vec.length;
2249 unsigned int i;
2250 unsigned char *p;
2252 l = len * elt_size;
2253 dw2_asm_output_data (1, l, NULL);
2254 if (elt_size > sizeof (HOST_WIDE_INT))
2256 elt_size /= 2;
2257 len *= 2;
2259 for (i = 0, p = val2->v.val_vec.array;
2260 i < len;
2261 i++, p += elt_size)
2262 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2263 "fp or vector constant word %u", i);
2265 break;
2266 case dw_val_class_const_double:
2268 unsigned HOST_WIDE_INT first, second;
2269 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2271 dw2_asm_output_data (1, 2 * l, NULL);
2272 if (WORDS_BIG_ENDIAN)
2274 first = val2->v.val_double.high;
2275 second = val2->v.val_double.low;
2277 else
2279 first = val2->v.val_double.low;
2280 second = val2->v.val_double.high;
2282 dw2_asm_output_data (l, first, NULL);
2283 dw2_asm_output_data (l, second, NULL);
2285 break;
2286 case dw_val_class_wide_int:
2288 int i;
2289 int len = get_full_len (*val2->v.val_wide);
2290 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2292 dw2_asm_output_data (1, len * l, NULL);
2293 if (WORDS_BIG_ENDIAN)
2294 for (i = len - 1; i >= 0; --i)
2295 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2296 else
2297 for (i = 0; i < len; ++i)
2298 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2300 break;
2301 default:
2302 gcc_unreachable ();
2305 break;
2306 case DW_OP_regval_type:
2307 case DW_OP_GNU_regval_type:
2309 unsigned r = val1->v.val_unsigned;
2310 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2311 gcc_assert (o);
2312 if (for_eh_or_skip >= 0)
2314 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2315 gcc_assert (size_of_uleb128 (r)
2316 == size_of_uleb128 (val1->v.val_unsigned));
2318 dw2_asm_output_data_uleb128 (r, NULL);
2319 dw2_asm_output_data_uleb128 (o, NULL);
2321 break;
2322 case DW_OP_deref_type:
2323 case DW_OP_GNU_deref_type:
2325 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2326 gcc_assert (o);
2327 dw2_asm_output_data (1, val1->v.val_int, NULL);
2328 dw2_asm_output_data_uleb128 (o, NULL);
2330 break;
2331 case DW_OP_convert:
2332 case DW_OP_reinterpret:
2333 case DW_OP_GNU_convert:
2334 case DW_OP_GNU_reinterpret:
2335 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2336 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2337 else
2339 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2340 gcc_assert (o);
2341 dw2_asm_output_data_uleb128 (o, NULL);
2343 break;
2345 case DW_OP_GNU_parameter_ref:
2347 unsigned long o;
2348 gcc_assert (val1->val_class == dw_val_class_die_ref);
2349 o = get_ref_die_offset (val1->v.val_die_ref.die);
2350 dw2_asm_output_data (4, o, NULL);
2352 break;
2354 default:
2355 /* Other codes have no operands. */
2356 break;
2360 /* Output a sequence of location operations.
2361 The for_eh_or_skip parameter controls whether register numbers are
2362 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2363 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2364 info). This should be suppressed for the cases that have not been converted
2365 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2367 void
2368 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2370 for (; loc != NULL; loc = loc->dw_loc_next)
2372 enum dwarf_location_atom opc = loc->dw_loc_opc;
2373 /* Output the opcode. */
2374 if (for_eh_or_skip >= 0
2375 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2377 unsigned r = (opc - DW_OP_breg0);
2378 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2379 gcc_assert (r <= 31);
2380 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2382 else if (for_eh_or_skip >= 0
2383 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2385 unsigned r = (opc - DW_OP_reg0);
2386 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2387 gcc_assert (r <= 31);
2388 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2391 dw2_asm_output_data (1, opc,
2392 "%s", dwarf_stack_op_name (opc));
2394 /* Output the operand(s) (if any). */
2395 output_loc_operands (loc, for_eh_or_skip);
2399 /* Output location description stack opcode's operands (if any).
2400 The output is single bytes on a line, suitable for .cfi_escape. */
2402 static void
2403 output_loc_operands_raw (dw_loc_descr_ref loc)
2405 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2406 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2408 switch (loc->dw_loc_opc)
2410 case DW_OP_addr:
2411 case DW_OP_GNU_addr_index:
2412 case DW_OP_GNU_const_index:
2413 case DW_OP_implicit_value:
2414 /* We cannot output addresses in .cfi_escape, only bytes. */
2415 gcc_unreachable ();
2417 case DW_OP_const1u:
2418 case DW_OP_const1s:
2419 case DW_OP_pick:
2420 case DW_OP_deref_size:
2421 case DW_OP_xderef_size:
2422 fputc (',', asm_out_file);
2423 dw2_asm_output_data_raw (1, val1->v.val_int);
2424 break;
2426 case DW_OP_const2u:
2427 case DW_OP_const2s:
2428 fputc (',', asm_out_file);
2429 dw2_asm_output_data_raw (2, val1->v.val_int);
2430 break;
2432 case DW_OP_const4u:
2433 case DW_OP_const4s:
2434 fputc (',', asm_out_file);
2435 dw2_asm_output_data_raw (4, val1->v.val_int);
2436 break;
2438 case DW_OP_const8u:
2439 case DW_OP_const8s:
2440 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2441 fputc (',', asm_out_file);
2442 dw2_asm_output_data_raw (8, val1->v.val_int);
2443 break;
2445 case DW_OP_skip:
2446 case DW_OP_bra:
2448 int offset;
2450 gcc_assert (val1->val_class == dw_val_class_loc);
2451 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2453 fputc (',', asm_out_file);
2454 dw2_asm_output_data_raw (2, offset);
2456 break;
2458 case DW_OP_regx:
2460 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2461 gcc_assert (size_of_uleb128 (r)
2462 == size_of_uleb128 (val1->v.val_unsigned));
2463 fputc (',', asm_out_file);
2464 dw2_asm_output_data_uleb128_raw (r);
2466 break;
2468 case DW_OP_constu:
2469 case DW_OP_plus_uconst:
2470 case DW_OP_piece:
2471 fputc (',', asm_out_file);
2472 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2473 break;
2475 case DW_OP_bit_piece:
2476 fputc (',', asm_out_file);
2477 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2478 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2479 break;
2481 case DW_OP_consts:
2482 case DW_OP_breg0:
2483 case DW_OP_breg1:
2484 case DW_OP_breg2:
2485 case DW_OP_breg3:
2486 case DW_OP_breg4:
2487 case DW_OP_breg5:
2488 case DW_OP_breg6:
2489 case DW_OP_breg7:
2490 case DW_OP_breg8:
2491 case DW_OP_breg9:
2492 case DW_OP_breg10:
2493 case DW_OP_breg11:
2494 case DW_OP_breg12:
2495 case DW_OP_breg13:
2496 case DW_OP_breg14:
2497 case DW_OP_breg15:
2498 case DW_OP_breg16:
2499 case DW_OP_breg17:
2500 case DW_OP_breg18:
2501 case DW_OP_breg19:
2502 case DW_OP_breg20:
2503 case DW_OP_breg21:
2504 case DW_OP_breg22:
2505 case DW_OP_breg23:
2506 case DW_OP_breg24:
2507 case DW_OP_breg25:
2508 case DW_OP_breg26:
2509 case DW_OP_breg27:
2510 case DW_OP_breg28:
2511 case DW_OP_breg29:
2512 case DW_OP_breg30:
2513 case DW_OP_breg31:
2514 case DW_OP_fbreg:
2515 fputc (',', asm_out_file);
2516 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2517 break;
2519 case DW_OP_bregx:
2521 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2522 gcc_assert (size_of_uleb128 (r)
2523 == size_of_uleb128 (val1->v.val_unsigned));
2524 fputc (',', asm_out_file);
2525 dw2_asm_output_data_uleb128_raw (r);
2526 fputc (',', asm_out_file);
2527 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2529 break;
2531 case DW_OP_implicit_pointer:
2532 case DW_OP_entry_value:
2533 case DW_OP_const_type:
2534 case DW_OP_regval_type:
2535 case DW_OP_deref_type:
2536 case DW_OP_convert:
2537 case DW_OP_reinterpret:
2538 case DW_OP_GNU_implicit_pointer:
2539 case DW_OP_GNU_entry_value:
2540 case DW_OP_GNU_const_type:
2541 case DW_OP_GNU_regval_type:
2542 case DW_OP_GNU_deref_type:
2543 case DW_OP_GNU_convert:
2544 case DW_OP_GNU_reinterpret:
2545 case DW_OP_GNU_parameter_ref:
2546 gcc_unreachable ();
2547 break;
2549 default:
2550 /* Other codes have no operands. */
2551 break;
2555 void
2556 output_loc_sequence_raw (dw_loc_descr_ref loc)
2558 while (1)
2560 enum dwarf_location_atom opc = loc->dw_loc_opc;
2561 /* Output the opcode. */
2562 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2564 unsigned r = (opc - DW_OP_breg0);
2565 r = DWARF2_FRAME_REG_OUT (r, 1);
2566 gcc_assert (r <= 31);
2567 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2569 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2571 unsigned r = (opc - DW_OP_reg0);
2572 r = DWARF2_FRAME_REG_OUT (r, 1);
2573 gcc_assert (r <= 31);
2574 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2576 /* Output the opcode. */
2577 fprintf (asm_out_file, "%#x", opc);
2578 output_loc_operands_raw (loc);
2580 if (!loc->dw_loc_next)
2581 break;
2582 loc = loc->dw_loc_next;
2584 fputc (',', asm_out_file);
2588 /* This function builds a dwarf location descriptor sequence from a
2589 dw_cfa_location, adding the given OFFSET to the result of the
2590 expression. */
2592 struct dw_loc_descr_node *
2593 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2595 struct dw_loc_descr_node *head, *tmp;
2597 offset += cfa->offset;
2599 if (cfa->indirect)
2601 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2602 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2603 head->dw_loc_oprnd1.val_entry = NULL;
2604 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2605 add_loc_descr (&head, tmp);
2606 if (offset != 0)
2608 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2609 add_loc_descr (&head, tmp);
2612 else
2613 head = new_reg_loc_descr (cfa->reg, offset);
2615 return head;
2618 /* This function builds a dwarf location descriptor sequence for
2619 the address at OFFSET from the CFA when stack is aligned to
2620 ALIGNMENT byte. */
2622 struct dw_loc_descr_node *
2623 build_cfa_aligned_loc (dw_cfa_location *cfa,
2624 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2626 struct dw_loc_descr_node *head;
2627 unsigned int dwarf_fp
2628 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2630 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2631 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2633 head = new_reg_loc_descr (dwarf_fp, 0);
2634 add_loc_descr (&head, int_loc_descriptor (alignment));
2635 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2636 loc_descr_plus_const (&head, offset);
2638 else
2639 head = new_reg_loc_descr (dwarf_fp, offset);
2640 return head;
2643 /* And now, the support for symbolic debugging information. */
2645 /* .debug_str support. */
2647 static void dwarf2out_init (const char *);
2648 static void dwarf2out_finish (const char *);
2649 static void dwarf2out_early_finish (const char *);
2650 static void dwarf2out_assembly_start (void);
2651 static void dwarf2out_define (unsigned int, const char *);
2652 static void dwarf2out_undef (unsigned int, const char *);
2653 static void dwarf2out_start_source_file (unsigned, const char *);
2654 static void dwarf2out_end_source_file (unsigned);
2655 static void dwarf2out_function_decl (tree);
2656 static void dwarf2out_begin_block (unsigned, unsigned);
2657 static void dwarf2out_end_block (unsigned, unsigned);
2658 static bool dwarf2out_ignore_block (const_tree);
2659 static void dwarf2out_early_global_decl (tree);
2660 static void dwarf2out_late_global_decl (tree);
2661 static void dwarf2out_type_decl (tree, int);
2662 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2663 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2664 dw_die_ref);
2665 static void dwarf2out_abstract_function (tree);
2666 static void dwarf2out_var_location (rtx_insn *);
2667 static void dwarf2out_size_function (tree);
2668 static void dwarf2out_begin_function (tree);
2669 static void dwarf2out_end_function (unsigned int);
2670 static void dwarf2out_register_main_translation_unit (tree unit);
2671 static void dwarf2out_set_name (tree, tree);
2673 /* The debug hooks structure. */
2675 const struct gcc_debug_hooks dwarf2_debug_hooks =
2677 dwarf2out_init,
2678 dwarf2out_finish,
2679 dwarf2out_early_finish,
2680 dwarf2out_assembly_start,
2681 dwarf2out_define,
2682 dwarf2out_undef,
2683 dwarf2out_start_source_file,
2684 dwarf2out_end_source_file,
2685 dwarf2out_begin_block,
2686 dwarf2out_end_block,
2687 dwarf2out_ignore_block,
2688 dwarf2out_source_line,
2689 dwarf2out_begin_prologue,
2690 #if VMS_DEBUGGING_INFO
2691 dwarf2out_vms_end_prologue,
2692 dwarf2out_vms_begin_epilogue,
2693 #else
2694 debug_nothing_int_charstar,
2695 debug_nothing_int_charstar,
2696 #endif
2697 dwarf2out_end_epilogue,
2698 dwarf2out_begin_function,
2699 dwarf2out_end_function, /* end_function */
2700 dwarf2out_register_main_translation_unit,
2701 dwarf2out_function_decl, /* function_decl */
2702 dwarf2out_early_global_decl,
2703 dwarf2out_late_global_decl,
2704 dwarf2out_type_decl, /* type_decl */
2705 dwarf2out_imported_module_or_decl,
2706 debug_nothing_tree, /* deferred_inline_function */
2707 /* The DWARF 2 backend tries to reduce debugging bloat by not
2708 emitting the abstract description of inline functions until
2709 something tries to reference them. */
2710 dwarf2out_abstract_function, /* outlining_inline_function */
2711 debug_nothing_rtx_code_label, /* label */
2712 debug_nothing_int, /* handle_pch */
2713 dwarf2out_var_location,
2714 dwarf2out_size_function, /* size_function */
2715 dwarf2out_switch_text_section,
2716 dwarf2out_set_name,
2717 1, /* start_end_main_source_file */
2718 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2721 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2723 dwarf2out_init,
2724 debug_nothing_charstar,
2725 debug_nothing_charstar,
2726 dwarf2out_assembly_start,
2727 debug_nothing_int_charstar,
2728 debug_nothing_int_charstar,
2729 debug_nothing_int_charstar,
2730 debug_nothing_int,
2731 debug_nothing_int_int, /* begin_block */
2732 debug_nothing_int_int, /* end_block */
2733 debug_true_const_tree, /* ignore_block */
2734 dwarf2out_source_line, /* source_line */
2735 debug_nothing_int_charstar, /* begin_prologue */
2736 debug_nothing_int_charstar, /* end_prologue */
2737 debug_nothing_int_charstar, /* begin_epilogue */
2738 debug_nothing_int_charstar, /* end_epilogue */
2739 debug_nothing_tree, /* begin_function */
2740 debug_nothing_int, /* end_function */
2741 debug_nothing_tree, /* register_main_translation_unit */
2742 debug_nothing_tree, /* function_decl */
2743 debug_nothing_tree, /* early_global_decl */
2744 debug_nothing_tree, /* late_global_decl */
2745 debug_nothing_tree_int, /* type_decl */
2746 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2747 debug_nothing_tree, /* deferred_inline_function */
2748 debug_nothing_tree, /* outlining_inline_function */
2749 debug_nothing_rtx_code_label, /* label */
2750 debug_nothing_int, /* handle_pch */
2751 debug_nothing_rtx_insn, /* var_location */
2752 debug_nothing_tree, /* size_function */
2753 debug_nothing_void, /* switch_text_section */
2754 debug_nothing_tree_tree, /* set_name */
2755 0, /* start_end_main_source_file */
2756 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2759 /* NOTE: In the comments in this file, many references are made to
2760 "Debugging Information Entries". This term is abbreviated as `DIE'
2761 throughout the remainder of this file. */
2763 /* An internal representation of the DWARF output is built, and then
2764 walked to generate the DWARF debugging info. The walk of the internal
2765 representation is done after the entire program has been compiled.
2766 The types below are used to describe the internal representation. */
2768 /* Whether to put type DIEs into their own section .debug_types instead
2769 of making them part of the .debug_info section. Only supported for
2770 Dwarf V4 or higher and the user didn't disable them through
2771 -fno-debug-types-section. It is more efficient to put them in a
2772 separate comdat sections since the linker will then be able to
2773 remove duplicates. But not all tools support .debug_types sections
2774 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2775 it is DW_UT_type unit type in .debug_info section. */
2777 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2779 /* Various DIE's use offsets relative to the beginning of the
2780 .debug_info section to refer to each other. */
2782 typedef long int dw_offset;
2784 struct comdat_type_node;
2786 /* The entries in the line_info table more-or-less mirror the opcodes
2787 that are used in the real dwarf line table. Arrays of these entries
2788 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2789 supported. */
2791 enum dw_line_info_opcode {
2792 /* Emit DW_LNE_set_address; the operand is the label index. */
2793 LI_set_address,
2795 /* Emit a row to the matrix with the given line. This may be done
2796 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2797 special opcodes. */
2798 LI_set_line,
2800 /* Emit a DW_LNS_set_file. */
2801 LI_set_file,
2803 /* Emit a DW_LNS_set_column. */
2804 LI_set_column,
2806 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2807 LI_negate_stmt,
2809 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2810 LI_set_prologue_end,
2811 LI_set_epilogue_begin,
2813 /* Emit a DW_LNE_set_discriminator. */
2814 LI_set_discriminator
2817 typedef struct GTY(()) dw_line_info_struct {
2818 enum dw_line_info_opcode opcode;
2819 unsigned int val;
2820 } dw_line_info_entry;
2823 struct GTY(()) dw_line_info_table {
2824 /* The label that marks the end of this section. */
2825 const char *end_label;
2827 /* The values for the last row of the matrix, as collected in the table.
2828 These are used to minimize the changes to the next row. */
2829 unsigned int file_num;
2830 unsigned int line_num;
2831 unsigned int column_num;
2832 int discrim_num;
2833 bool is_stmt;
2834 bool in_use;
2836 vec<dw_line_info_entry, va_gc> *entries;
2840 /* Each DIE attribute has a field specifying the attribute kind,
2841 a link to the next attribute in the chain, and an attribute value.
2842 Attributes are typically linked below the DIE they modify. */
2844 typedef struct GTY(()) dw_attr_struct {
2845 enum dwarf_attribute dw_attr;
2846 dw_val_node dw_attr_val;
2848 dw_attr_node;
2851 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2852 The children of each node form a circular list linked by
2853 die_sib. die_child points to the node *before* the "first" child node. */
2855 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2856 union die_symbol_or_type_node
2858 const char * GTY ((tag ("0"))) die_symbol;
2859 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2861 GTY ((desc ("%0.comdat_type_p"))) die_id;
2862 vec<dw_attr_node, va_gc> *die_attr;
2863 dw_die_ref die_parent;
2864 dw_die_ref die_child;
2865 dw_die_ref die_sib;
2866 dw_die_ref die_definition; /* ref from a specification to its definition */
2867 dw_offset die_offset;
2868 unsigned long die_abbrev;
2869 int die_mark;
2870 unsigned int decl_id;
2871 enum dwarf_tag die_tag;
2872 /* Die is used and must not be pruned as unused. */
2873 BOOL_BITFIELD die_perennial_p : 1;
2874 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2875 /* Whether this DIE was removed from the DIE tree, for example via
2876 prune_unused_types. We don't consider those present from the
2877 DIE lookup routines. */
2878 BOOL_BITFIELD removed : 1;
2879 /* Lots of spare bits. */
2881 die_node;
2883 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2884 static bool early_dwarf;
2885 static bool early_dwarf_finished;
2886 struct set_early_dwarf {
2887 bool saved;
2888 set_early_dwarf () : saved(early_dwarf)
2890 gcc_assert (! early_dwarf_finished);
2891 early_dwarf = true;
2893 ~set_early_dwarf () { early_dwarf = saved; }
2896 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2897 #define FOR_EACH_CHILD(die, c, expr) do { \
2898 c = die->die_child; \
2899 if (c) do { \
2900 c = c->die_sib; \
2901 expr; \
2902 } while (c != die->die_child); \
2903 } while (0)
2905 /* The pubname structure */
2907 typedef struct GTY(()) pubname_struct {
2908 dw_die_ref die;
2909 const char *name;
2911 pubname_entry;
2914 struct GTY(()) dw_ranges {
2915 const char *label;
2916 /* If this is positive, it's a block number, otherwise it's a
2917 bitwise-negated index into dw_ranges_by_label. */
2918 int num;
2919 /* Index for the range list for DW_FORM_rnglistx. */
2920 unsigned int idx : 31;
2921 /* True if this range might be possibly in a different section
2922 from previous entry. */
2923 unsigned int maybe_new_sec : 1;
2926 /* A structure to hold a macinfo entry. */
2928 typedef struct GTY(()) macinfo_struct {
2929 unsigned char code;
2930 unsigned HOST_WIDE_INT lineno;
2931 const char *info;
2933 macinfo_entry;
2936 struct GTY(()) dw_ranges_by_label {
2937 const char *begin;
2938 const char *end;
2941 /* The comdat type node structure. */
2942 struct GTY(()) comdat_type_node
2944 dw_die_ref root_die;
2945 dw_die_ref type_die;
2946 dw_die_ref skeleton_die;
2947 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2948 comdat_type_node *next;
2951 /* A list of DIEs for which we can't determine ancestry (parent_die
2952 field) just yet. Later in dwarf2out_finish we will fill in the
2953 missing bits. */
2954 typedef struct GTY(()) limbo_die_struct {
2955 dw_die_ref die;
2956 /* The tree for which this DIE was created. We use this to
2957 determine ancestry later. */
2958 tree created_for;
2959 struct limbo_die_struct *next;
2961 limbo_die_node;
2963 typedef struct skeleton_chain_struct
2965 dw_die_ref old_die;
2966 dw_die_ref new_die;
2967 struct skeleton_chain_struct *parent;
2969 skeleton_chain_node;
2971 /* Define a macro which returns nonzero for a TYPE_DECL which was
2972 implicitly generated for a type.
2974 Note that, unlike the C front-end (which generates a NULL named
2975 TYPE_DECL node for each complete tagged type, each array type,
2976 and each function type node created) the C++ front-end generates
2977 a _named_ TYPE_DECL node for each tagged type node created.
2978 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2979 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2980 front-end, but for each type, tagged or not. */
2982 #define TYPE_DECL_IS_STUB(decl) \
2983 (DECL_NAME (decl) == NULL_TREE \
2984 || (DECL_ARTIFICIAL (decl) \
2985 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2986 /* This is necessary for stub decls that \
2987 appear in nested inline functions. */ \
2988 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2989 && (decl_ultimate_origin (decl) \
2990 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2992 /* Information concerning the compilation unit's programming
2993 language, and compiler version. */
2995 /* Fixed size portion of the DWARF compilation unit header. */
2996 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2997 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
2998 + (dwarf_version >= 5 \
2999 ? 4 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE : 3))
3001 /* Fixed size portion of the DWARF comdat type unit header. */
3002 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3003 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3004 + (dwarf_version >= 5 \
3005 ? 0 : DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE))
3007 /* Fixed size portion of public names info. */
3008 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3010 /* Fixed size portion of the address range info. */
3011 #define DWARF_ARANGES_HEADER_SIZE \
3012 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3013 DWARF2_ADDR_SIZE * 2) \
3014 - DWARF_INITIAL_LENGTH_SIZE)
3016 /* Size of padding portion in the address range info. It must be
3017 aligned to twice the pointer size. */
3018 #define DWARF_ARANGES_PAD_SIZE \
3019 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3020 DWARF2_ADDR_SIZE * 2) \
3021 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3023 /* Use assembler line directives if available. */
3024 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3025 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3026 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3027 #else
3028 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3029 #endif
3030 #endif
3032 /* Minimum line offset in a special line info. opcode.
3033 This value was chosen to give a reasonable range of values. */
3034 #define DWARF_LINE_BASE -10
3036 /* First special line opcode - leave room for the standard opcodes. */
3037 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3039 /* Range of line offsets in a special line info. opcode. */
3040 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3042 /* Flag that indicates the initial value of the is_stmt_start flag.
3043 In the present implementation, we do not mark any lines as
3044 the beginning of a source statement, because that information
3045 is not made available by the GCC front-end. */
3046 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3048 /* Maximum number of operations per instruction bundle. */
3049 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3050 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3051 #endif
3053 /* This location is used by calc_die_sizes() to keep track
3054 the offset of each DIE within the .debug_info section. */
3055 static unsigned long next_die_offset;
3057 /* Record the root of the DIE's built for the current compilation unit. */
3058 static GTY(()) dw_die_ref single_comp_unit_die;
3060 /* A list of type DIEs that have been separated into comdat sections. */
3061 static GTY(()) comdat_type_node *comdat_type_list;
3063 /* A list of CU DIEs that have been separated. */
3064 static GTY(()) limbo_die_node *cu_die_list;
3066 /* A list of DIEs with a NULL parent waiting to be relocated. */
3067 static GTY(()) limbo_die_node *limbo_die_list;
3069 /* A list of DIEs for which we may have to generate
3070 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3071 static GTY(()) limbo_die_node *deferred_asm_name;
3073 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3075 typedef const char *compare_type;
3077 static hashval_t hash (dwarf_file_data *);
3078 static bool equal (dwarf_file_data *, const char *);
3081 /* Filenames referenced by this compilation unit. */
3082 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3084 struct decl_die_hasher : ggc_ptr_hash<die_node>
3086 typedef tree compare_type;
3088 static hashval_t hash (die_node *);
3089 static bool equal (die_node *, tree);
3091 /* A hash table of references to DIE's that describe declarations.
3092 The key is a DECL_UID() which is a unique number identifying each decl. */
3093 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3095 struct block_die_hasher : ggc_ptr_hash<die_struct>
3097 static hashval_t hash (die_struct *);
3098 static bool equal (die_struct *, die_struct *);
3101 /* A hash table of references to DIE's that describe COMMON blocks.
3102 The key is DECL_UID() ^ die_parent. */
3103 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3105 typedef struct GTY(()) die_arg_entry_struct {
3106 dw_die_ref die;
3107 tree arg;
3108 } die_arg_entry;
3111 /* Node of the variable location list. */
3112 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3113 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3114 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3115 in mode of the EXPR_LIST node and first EXPR_LIST operand
3116 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3117 location or NULL for padding. For larger bitsizes,
3118 mode is 0 and first operand is a CONCAT with bitsize
3119 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3120 NULL as second operand. */
3121 rtx GTY (()) loc;
3122 const char * GTY (()) label;
3123 struct var_loc_node * GTY (()) next;
3126 /* Variable location list. */
3127 struct GTY ((for_user)) var_loc_list_def {
3128 struct var_loc_node * GTY (()) first;
3130 /* Pointer to the last but one or last element of the
3131 chained list. If the list is empty, both first and
3132 last are NULL, if the list contains just one node
3133 or the last node certainly is not redundant, it points
3134 to the last node, otherwise points to the last but one.
3135 Do not mark it for GC because it is marked through the chain. */
3136 struct var_loc_node * GTY ((skip ("%h"))) last;
3138 /* Pointer to the last element before section switch,
3139 if NULL, either sections weren't switched or first
3140 is after section switch. */
3141 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3143 /* DECL_UID of the variable decl. */
3144 unsigned int decl_id;
3146 typedef struct var_loc_list_def var_loc_list;
3148 /* Call argument location list. */
3149 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3150 rtx GTY (()) call_arg_loc_note;
3151 const char * GTY (()) label;
3152 tree GTY (()) block;
3153 bool tail_call_p;
3154 rtx GTY (()) symbol_ref;
3155 struct call_arg_loc_node * GTY (()) next;
3159 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3161 typedef const_tree compare_type;
3163 static hashval_t hash (var_loc_list *);
3164 static bool equal (var_loc_list *, const_tree);
3167 /* Table of decl location linked lists. */
3168 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3170 /* Head and tail of call_arg_loc chain. */
3171 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3172 static struct call_arg_loc_node *call_arg_loc_last;
3174 /* Number of call sites in the current function. */
3175 static int call_site_count = -1;
3176 /* Number of tail call sites in the current function. */
3177 static int tail_call_site_count = -1;
3179 /* A cached location list. */
3180 struct GTY ((for_user)) cached_dw_loc_list_def {
3181 /* The DECL_UID of the decl that this entry describes. */
3182 unsigned int decl_id;
3184 /* The cached location list. */
3185 dw_loc_list_ref loc_list;
3187 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3189 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3192 typedef const_tree compare_type;
3194 static hashval_t hash (cached_dw_loc_list *);
3195 static bool equal (cached_dw_loc_list *, const_tree);
3198 /* Table of cached location lists. */
3199 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3201 /* A vector of references to DIE's that are uniquely identified by their tag,
3202 presence/absence of children DIE's, and list of attribute/value pairs. */
3203 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3205 /* A hash map to remember the stack usage for DWARF procedures. The value
3206 stored is the stack size difference between before the DWARF procedure
3207 invokation and after it returned. In other words, for a DWARF procedure
3208 that consumes N stack slots and that pushes M ones, this stores M - N. */
3209 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3211 /* A global counter for generating labels for line number data. */
3212 static unsigned int line_info_label_num;
3214 /* The current table to which we should emit line number information
3215 for the current function. This will be set up at the beginning of
3216 assembly for the function. */
3217 static GTY(()) dw_line_info_table *cur_line_info_table;
3219 /* The two default tables of line number info. */
3220 static GTY(()) dw_line_info_table *text_section_line_info;
3221 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3223 /* The set of all non-default tables of line number info. */
3224 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3226 /* A flag to tell pubnames/types export if there is an info section to
3227 refer to. */
3228 static bool info_section_emitted;
3230 /* A pointer to the base of a table that contains a list of publicly
3231 accessible names. */
3232 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3234 /* A pointer to the base of a table that contains a list of publicly
3235 accessible types. */
3236 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3238 /* A pointer to the base of a table that contains a list of macro
3239 defines/undefines (and file start/end markers). */
3240 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3242 /* True if .debug_macinfo or .debug_macros section is going to be
3243 emitted. */
3244 #define have_macinfo \
3245 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3246 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3247 && !macinfo_table->is_empty ())
3249 /* Vector of dies for which we should generate .debug_ranges info. */
3250 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3252 /* Vector of pairs of labels referenced in ranges_table. */
3253 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3255 /* Whether we have location lists that need outputting */
3256 static GTY(()) bool have_location_lists;
3258 /* Unique label counter. */
3259 static GTY(()) unsigned int loclabel_num;
3261 /* Unique label counter for point-of-call tables. */
3262 static GTY(()) unsigned int poc_label_num;
3264 /* The last file entry emitted by maybe_emit_file(). */
3265 static GTY(()) struct dwarf_file_data * last_emitted_file;
3267 /* Number of internal labels generated by gen_internal_sym(). */
3268 static GTY(()) int label_num;
3270 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3272 /* Instances of generic types for which we need to generate debug
3273 info that describe their generic parameters and arguments. That
3274 generation needs to happen once all types are properly laid out so
3275 we do it at the end of compilation. */
3276 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3278 /* Offset from the "steady-state frame pointer" to the frame base,
3279 within the current function. */
3280 static HOST_WIDE_INT frame_pointer_fb_offset;
3281 static bool frame_pointer_fb_offset_valid;
3283 static vec<dw_die_ref> base_types;
3285 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3286 once all arguments are parsed. */
3287 static vec<dw_die_ref> *string_types;
3289 /* Flags to represent a set of attribute classes for attributes that represent
3290 a scalar value (bounds, pointers, ...). */
3291 enum dw_scalar_form
3293 dw_scalar_form_constant = 0x01,
3294 dw_scalar_form_exprloc = 0x02,
3295 dw_scalar_form_reference = 0x04
3298 /* Forward declarations for functions defined in this file. */
3300 static int is_pseudo_reg (const_rtx);
3301 static tree type_main_variant (tree);
3302 static int is_tagged_type (const_tree);
3303 static const char *dwarf_tag_name (unsigned);
3304 static const char *dwarf_attr_name (unsigned);
3305 static const char *dwarf_form_name (unsigned);
3306 static tree decl_ultimate_origin (const_tree);
3307 static tree decl_class_context (tree);
3308 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3309 static inline enum dw_val_class AT_class (dw_attr_node *);
3310 static inline unsigned int AT_index (dw_attr_node *);
3311 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3312 static inline unsigned AT_flag (dw_attr_node *);
3313 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3314 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3315 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3316 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3317 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3318 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3319 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3320 unsigned int, unsigned char *);
3321 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3322 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3323 static inline const char *AT_string (dw_attr_node *);
3324 static enum dwarf_form AT_string_form (dw_attr_node *);
3325 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3326 static void add_AT_specification (dw_die_ref, dw_die_ref);
3327 static inline dw_die_ref AT_ref (dw_attr_node *);
3328 static inline int AT_ref_external (dw_attr_node *);
3329 static inline void set_AT_ref_external (dw_attr_node *, int);
3330 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3331 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3332 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3333 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3334 dw_loc_list_ref);
3335 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3336 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3337 static void remove_addr_table_entry (addr_table_entry *);
3338 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3339 static inline rtx AT_addr (dw_attr_node *);
3340 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3341 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3342 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3343 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3344 const char *);
3345 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3346 unsigned HOST_WIDE_INT);
3347 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3348 unsigned long, bool);
3349 static inline const char *AT_lbl (dw_attr_node *);
3350 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3351 static const char *get_AT_low_pc (dw_die_ref);
3352 static const char *get_AT_hi_pc (dw_die_ref);
3353 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3354 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3355 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3356 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3357 static bool is_cxx (void);
3358 static bool is_fortran (void);
3359 static bool is_ada (void);
3360 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3361 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3362 static void add_child_die (dw_die_ref, dw_die_ref);
3363 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3364 static dw_die_ref lookup_type_die (tree);
3365 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3366 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3367 static void equate_type_number_to_die (tree, dw_die_ref);
3368 static dw_die_ref lookup_decl_die (tree);
3369 static var_loc_list *lookup_decl_loc (const_tree);
3370 static void equate_decl_number_to_die (tree, dw_die_ref);
3371 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3372 static void print_spaces (FILE *);
3373 static void print_die (dw_die_ref, FILE *);
3374 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3375 static dw_die_ref pop_compile_unit (dw_die_ref);
3376 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3377 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3378 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3379 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3380 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3381 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3382 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3383 struct md5_ctx *, int *);
3384 struct checksum_attributes;
3385 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3386 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3387 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3388 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3389 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3390 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3391 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3392 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3393 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3394 static void compute_section_prefix (dw_die_ref);
3395 static int is_type_die (dw_die_ref);
3396 static int is_comdat_die (dw_die_ref);
3397 static int is_symbol_die (dw_die_ref);
3398 static inline bool is_template_instantiation (dw_die_ref);
3399 static void assign_symbol_names (dw_die_ref);
3400 static void break_out_includes (dw_die_ref);
3401 static int is_declaration_die (dw_die_ref);
3402 static int should_move_die_to_comdat (dw_die_ref);
3403 static dw_die_ref clone_as_declaration (dw_die_ref);
3404 static dw_die_ref clone_die (dw_die_ref);
3405 static dw_die_ref clone_tree (dw_die_ref);
3406 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3407 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3408 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3409 static dw_die_ref generate_skeleton (dw_die_ref);
3410 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3411 dw_die_ref,
3412 dw_die_ref);
3413 static void break_out_comdat_types (dw_die_ref);
3414 static void copy_decls_for_unworthy_types (dw_die_ref);
3416 static void add_sibling_attributes (dw_die_ref);
3417 static void output_location_lists (dw_die_ref);
3418 static int constant_size (unsigned HOST_WIDE_INT);
3419 static unsigned long size_of_die (dw_die_ref);
3420 static void calc_die_sizes (dw_die_ref);
3421 static void calc_base_type_die_sizes (void);
3422 static void mark_dies (dw_die_ref);
3423 static void unmark_dies (dw_die_ref);
3424 static void unmark_all_dies (dw_die_ref);
3425 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3426 static unsigned long size_of_aranges (void);
3427 static enum dwarf_form value_format (dw_attr_node *);
3428 static void output_value_format (dw_attr_node *);
3429 static void output_abbrev_section (void);
3430 static void output_die_abbrevs (unsigned long, dw_die_ref);
3431 static void output_die_symbol (dw_die_ref);
3432 static void output_die (dw_die_ref);
3433 static void output_compilation_unit_header (enum dwarf_unit_type);
3434 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3435 static void output_comdat_type_unit (comdat_type_node *);
3436 static const char *dwarf2_name (tree, int);
3437 static void add_pubname (tree, dw_die_ref);
3438 static void add_enumerator_pubname (const char *, dw_die_ref);
3439 static void add_pubname_string (const char *, dw_die_ref);
3440 static void add_pubtype (tree, dw_die_ref);
3441 static void output_pubnames (vec<pubname_entry, va_gc> *);
3442 static void output_aranges (void);
3443 static unsigned int add_ranges (const_tree, bool = false);
3444 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3445 bool *, bool);
3446 static void output_ranges (void);
3447 static dw_line_info_table *new_line_info_table (void);
3448 static void output_line_info (bool);
3449 static void output_file_names (void);
3450 static dw_die_ref base_type_die (tree, bool);
3451 static int is_base_type (tree);
3452 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3453 static int decl_quals (const_tree);
3454 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3455 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3456 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3457 static int type_is_enum (const_tree);
3458 static unsigned int dbx_reg_number (const_rtx);
3459 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3460 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3461 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3462 enum var_init_status);
3463 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3464 enum var_init_status);
3465 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3466 enum var_init_status);
3467 static int is_based_loc (const_rtx);
3468 static bool resolve_one_addr (rtx *);
3469 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3470 enum var_init_status);
3471 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3472 enum var_init_status);
3473 struct loc_descr_context;
3474 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3475 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3476 static dw_loc_list_ref loc_list_from_tree (tree, int,
3477 struct loc_descr_context *);
3478 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3479 struct loc_descr_context *);
3480 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3481 static tree field_type (const_tree);
3482 static unsigned int simple_type_align_in_bits (const_tree);
3483 static unsigned int simple_decl_align_in_bits (const_tree);
3484 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3485 struct vlr_context;
3486 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3487 HOST_WIDE_INT *);
3488 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3489 dw_loc_list_ref);
3490 static void add_data_member_location_attribute (dw_die_ref, tree,
3491 struct vlr_context *);
3492 static bool add_const_value_attribute (dw_die_ref, rtx);
3493 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3494 static void insert_wide_int (const wide_int &, unsigned char *, int);
3495 static void insert_float (const_rtx, unsigned char *);
3496 static rtx rtl_for_decl_location (tree);
3497 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3498 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3499 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3500 static void add_name_attribute (dw_die_ref, const char *);
3501 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3502 static void add_comp_dir_attribute (dw_die_ref);
3503 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3504 struct loc_descr_context *);
3505 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3506 struct loc_descr_context *);
3507 static void add_subscript_info (dw_die_ref, tree, bool);
3508 static void add_byte_size_attribute (dw_die_ref, tree);
3509 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3510 struct vlr_context *);
3511 static void add_bit_size_attribute (dw_die_ref, tree);
3512 static void add_prototyped_attribute (dw_die_ref, tree);
3513 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3514 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3515 static void add_src_coords_attributes (dw_die_ref, tree);
3516 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3517 static void add_discr_value (dw_die_ref, dw_discr_value *);
3518 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3519 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3520 static void push_decl_scope (tree);
3521 static void pop_decl_scope (void);
3522 static dw_die_ref scope_die_for (tree, dw_die_ref);
3523 static inline int local_scope_p (dw_die_ref);
3524 static inline int class_scope_p (dw_die_ref);
3525 static inline int class_or_namespace_scope_p (dw_die_ref);
3526 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3527 static void add_calling_convention_attribute (dw_die_ref, tree);
3528 static const char *type_tag (const_tree);
3529 static tree member_declared_type (const_tree);
3530 #if 0
3531 static const char *decl_start_label (tree);
3532 #endif
3533 static void gen_array_type_die (tree, dw_die_ref);
3534 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3535 #if 0
3536 static void gen_entry_point_die (tree, dw_die_ref);
3537 #endif
3538 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3539 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3540 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3541 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3542 static void gen_formal_types_die (tree, dw_die_ref);
3543 static void gen_subprogram_die (tree, dw_die_ref);
3544 static void gen_variable_die (tree, tree, dw_die_ref);
3545 static void gen_const_die (tree, dw_die_ref);
3546 static void gen_label_die (tree, dw_die_ref);
3547 static void gen_lexical_block_die (tree, dw_die_ref);
3548 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3549 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3550 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3551 static dw_die_ref gen_compile_unit_die (const char *);
3552 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3553 static void gen_member_die (tree, dw_die_ref);
3554 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3555 enum debug_info_usage);
3556 static void gen_subroutine_type_die (tree, dw_die_ref);
3557 static void gen_typedef_die (tree, dw_die_ref);
3558 static void gen_type_die (tree, dw_die_ref);
3559 static void gen_block_die (tree, dw_die_ref);
3560 static void decls_for_scope (tree, dw_die_ref);
3561 static bool is_naming_typedef_decl (const_tree);
3562 static inline dw_die_ref get_context_die (tree);
3563 static void gen_namespace_die (tree, dw_die_ref);
3564 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3565 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3566 static dw_die_ref force_decl_die (tree);
3567 static dw_die_ref force_type_die (tree);
3568 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3569 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3570 static struct dwarf_file_data * lookup_filename (const char *);
3571 static void retry_incomplete_types (void);
3572 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3573 static void gen_generic_params_dies (tree);
3574 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3575 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3576 static void splice_child_die (dw_die_ref, dw_die_ref);
3577 static int file_info_cmp (const void *, const void *);
3578 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3579 const char *, const char *);
3580 static void output_loc_list (dw_loc_list_ref);
3581 static char *gen_internal_sym (const char *);
3582 static bool want_pubnames (void);
3584 static void prune_unmark_dies (dw_die_ref);
3585 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3586 static void prune_unused_types_mark (dw_die_ref, int);
3587 static void prune_unused_types_walk (dw_die_ref);
3588 static void prune_unused_types_walk_attribs (dw_die_ref);
3589 static void prune_unused_types_prune (dw_die_ref);
3590 static void prune_unused_types (void);
3591 static int maybe_emit_file (struct dwarf_file_data *fd);
3592 static inline const char *AT_vms_delta1 (dw_attr_node *);
3593 static inline const char *AT_vms_delta2 (dw_attr_node *);
3594 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3595 const char *, const char *);
3596 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3597 static void gen_remaining_tmpl_value_param_die_attribute (void);
3598 static bool generic_type_p (tree);
3599 static void schedule_generic_params_dies_gen (tree t);
3600 static void gen_scheduled_generic_parms_dies (void);
3602 static const char *comp_dir_string (void);
3604 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3606 /* enum for tracking thread-local variables whose address is really an offset
3607 relative to the TLS pointer, which will need link-time relocation, but will
3608 not need relocation by the DWARF consumer. */
3610 enum dtprel_bool
3612 dtprel_false = 0,
3613 dtprel_true = 1
3616 /* Return the operator to use for an address of a variable. For dtprel_true, we
3617 use DW_OP_const*. For regular variables, which need both link-time
3618 relocation and consumer-level relocation (e.g., to account for shared objects
3619 loaded at a random address), we use DW_OP_addr*. */
3621 static inline enum dwarf_location_atom
3622 dw_addr_op (enum dtprel_bool dtprel)
3624 if (dtprel == dtprel_true)
3625 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3626 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3627 else
3628 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3631 /* Return a pointer to a newly allocated address location description. If
3632 dwarf_split_debug_info is true, then record the address with the appropriate
3633 relocation. */
3634 static inline dw_loc_descr_ref
3635 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3637 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3639 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3640 ref->dw_loc_oprnd1.v.val_addr = addr;
3641 ref->dtprel = dtprel;
3642 if (dwarf_split_debug_info)
3643 ref->dw_loc_oprnd1.val_entry
3644 = add_addr_table_entry (addr,
3645 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3646 else
3647 ref->dw_loc_oprnd1.val_entry = NULL;
3649 return ref;
3652 /* Section names used to hold DWARF debugging information. */
3654 #ifndef DEBUG_INFO_SECTION
3655 #define DEBUG_INFO_SECTION ".debug_info"
3656 #endif
3657 #ifndef DEBUG_DWO_INFO_SECTION
3658 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3659 #endif
3660 #ifndef DEBUG_ABBREV_SECTION
3661 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3662 #endif
3663 #ifndef DEBUG_DWO_ABBREV_SECTION
3664 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3665 #endif
3666 #ifndef DEBUG_ARANGES_SECTION
3667 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3668 #endif
3669 #ifndef DEBUG_ADDR_SECTION
3670 #define DEBUG_ADDR_SECTION ".debug_addr"
3671 #endif
3672 #ifndef DEBUG_MACINFO_SECTION
3673 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3674 #endif
3675 #ifndef DEBUG_DWO_MACINFO_SECTION
3676 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3677 #endif
3678 #ifndef DEBUG_DWO_MACRO_SECTION
3679 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3680 #endif
3681 #ifndef DEBUG_MACRO_SECTION
3682 #define DEBUG_MACRO_SECTION ".debug_macro"
3683 #endif
3684 #ifndef DEBUG_LINE_SECTION
3685 #define DEBUG_LINE_SECTION ".debug_line"
3686 #endif
3687 #ifndef DEBUG_DWO_LINE_SECTION
3688 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3689 #endif
3690 #ifndef DEBUG_LOC_SECTION
3691 #define DEBUG_LOC_SECTION ".debug_loc"
3692 #endif
3693 #ifndef DEBUG_DWO_LOC_SECTION
3694 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3695 #endif
3696 #ifndef DEBUG_LOCLISTS_SECTION
3697 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3698 #endif
3699 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3700 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3701 #endif
3702 #ifndef DEBUG_PUBNAMES_SECTION
3703 #define DEBUG_PUBNAMES_SECTION \
3704 ((debug_generate_pub_sections == 2) \
3705 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3706 #endif
3707 #ifndef DEBUG_PUBTYPES_SECTION
3708 #define DEBUG_PUBTYPES_SECTION \
3709 ((debug_generate_pub_sections == 2) \
3710 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3711 #endif
3712 #ifndef DEBUG_STR_OFFSETS_SECTION
3713 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3714 #endif
3715 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3716 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3717 #endif
3718 #ifndef DEBUG_STR_DWO_SECTION
3719 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3720 #endif
3721 #ifndef DEBUG_STR_SECTION
3722 #define DEBUG_STR_SECTION ".debug_str"
3723 #endif
3724 #ifndef DEBUG_RANGES_SECTION
3725 #define DEBUG_RANGES_SECTION ".debug_ranges"
3726 #endif
3727 #ifndef DEBUG_RNGLISTS_SECTION
3728 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3729 #endif
3730 #ifndef DEBUG_LINE_STR_SECTION
3731 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3732 #endif
3734 /* Standard ELF section names for compiled code and data. */
3735 #ifndef TEXT_SECTION_NAME
3736 #define TEXT_SECTION_NAME ".text"
3737 #endif
3739 /* Section flags for .debug_str section. */
3740 #define DEBUG_STR_SECTION_FLAGS \
3741 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3742 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3743 : SECTION_DEBUG)
3745 /* Section flags for .debug_str.dwo section. */
3746 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3748 /* Labels we insert at beginning sections we can reference instead of
3749 the section names themselves. */
3751 #ifndef TEXT_SECTION_LABEL
3752 #define TEXT_SECTION_LABEL "Ltext"
3753 #endif
3754 #ifndef COLD_TEXT_SECTION_LABEL
3755 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3756 #endif
3757 #ifndef DEBUG_LINE_SECTION_LABEL
3758 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3759 #endif
3760 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3761 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3762 #endif
3763 #ifndef DEBUG_INFO_SECTION_LABEL
3764 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3765 #endif
3766 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3767 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3768 #endif
3769 #ifndef DEBUG_ABBREV_SECTION_LABEL
3770 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3771 #endif
3772 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3773 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3774 #endif
3775 #ifndef DEBUG_ADDR_SECTION_LABEL
3776 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3777 #endif
3778 #ifndef DEBUG_LOC_SECTION_LABEL
3779 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3780 #endif
3781 #ifndef DEBUG_RANGES_SECTION_LABEL
3782 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3783 #endif
3784 #ifndef DEBUG_MACINFO_SECTION_LABEL
3785 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3786 #endif
3787 #ifndef DEBUG_MACRO_SECTION_LABEL
3788 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3789 #endif
3790 #define SKELETON_COMP_DIE_ABBREV 1
3791 #define SKELETON_TYPE_DIE_ABBREV 2
3793 /* Definitions of defaults for formats and names of various special
3794 (artificial) labels which may be generated within this file (when the -g
3795 options is used and DWARF2_DEBUGGING_INFO is in effect.
3796 If necessary, these may be overridden from within the tm.h file, but
3797 typically, overriding these defaults is unnecessary. */
3799 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3800 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3801 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3802 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3803 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3804 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3805 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3806 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3807 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3808 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3809 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3810 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3811 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3812 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3813 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3815 #ifndef TEXT_END_LABEL
3816 #define TEXT_END_LABEL "Letext"
3817 #endif
3818 #ifndef COLD_END_LABEL
3819 #define COLD_END_LABEL "Letext_cold"
3820 #endif
3821 #ifndef BLOCK_BEGIN_LABEL
3822 #define BLOCK_BEGIN_LABEL "LBB"
3823 #endif
3824 #ifndef BLOCK_END_LABEL
3825 #define BLOCK_END_LABEL "LBE"
3826 #endif
3827 #ifndef LINE_CODE_LABEL
3828 #define LINE_CODE_LABEL "LM"
3829 #endif
3832 /* Return the root of the DIE's built for the current compilation unit. */
3833 static dw_die_ref
3834 comp_unit_die (void)
3836 if (!single_comp_unit_die)
3837 single_comp_unit_die = gen_compile_unit_die (NULL);
3838 return single_comp_unit_die;
3841 /* We allow a language front-end to designate a function that is to be
3842 called to "demangle" any name before it is put into a DIE. */
3844 static const char *(*demangle_name_func) (const char *);
3846 void
3847 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3849 demangle_name_func = func;
3852 /* Test if rtl node points to a pseudo register. */
3854 static inline int
3855 is_pseudo_reg (const_rtx rtl)
3857 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3858 || (GET_CODE (rtl) == SUBREG
3859 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3862 /* Return a reference to a type, with its const and volatile qualifiers
3863 removed. */
3865 static inline tree
3866 type_main_variant (tree type)
3868 type = TYPE_MAIN_VARIANT (type);
3870 /* ??? There really should be only one main variant among any group of
3871 variants of a given type (and all of the MAIN_VARIANT values for all
3872 members of the group should point to that one type) but sometimes the C
3873 front-end messes this up for array types, so we work around that bug
3874 here. */
3875 if (TREE_CODE (type) == ARRAY_TYPE)
3876 while (type != TYPE_MAIN_VARIANT (type))
3877 type = TYPE_MAIN_VARIANT (type);
3879 return type;
3882 /* Return nonzero if the given type node represents a tagged type. */
3884 static inline int
3885 is_tagged_type (const_tree type)
3887 enum tree_code code = TREE_CODE (type);
3889 return (code == RECORD_TYPE || code == UNION_TYPE
3890 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3893 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3895 static void
3896 get_ref_die_offset_label (char *label, dw_die_ref ref)
3898 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3901 /* Return die_offset of a DIE reference to a base type. */
3903 static unsigned long int
3904 get_base_type_offset (dw_die_ref ref)
3906 if (ref->die_offset)
3907 return ref->die_offset;
3908 if (comp_unit_die ()->die_abbrev)
3910 calc_base_type_die_sizes ();
3911 gcc_assert (ref->die_offset);
3913 return ref->die_offset;
3916 /* Return die_offset of a DIE reference other than base type. */
3918 static unsigned long int
3919 get_ref_die_offset (dw_die_ref ref)
3921 gcc_assert (ref->die_offset);
3922 return ref->die_offset;
3925 /* Convert a DIE tag into its string name. */
3927 static const char *
3928 dwarf_tag_name (unsigned int tag)
3930 const char *name = get_DW_TAG_name (tag);
3932 if (name != NULL)
3933 return name;
3935 return "DW_TAG_<unknown>";
3938 /* Convert a DWARF attribute code into its string name. */
3940 static const char *
3941 dwarf_attr_name (unsigned int attr)
3943 const char *name;
3945 switch (attr)
3947 #if VMS_DEBUGGING_INFO
3948 case DW_AT_HP_prologue:
3949 return "DW_AT_HP_prologue";
3950 #else
3951 case DW_AT_MIPS_loop_unroll_factor:
3952 return "DW_AT_MIPS_loop_unroll_factor";
3953 #endif
3955 #if VMS_DEBUGGING_INFO
3956 case DW_AT_HP_epilogue:
3957 return "DW_AT_HP_epilogue";
3958 #else
3959 case DW_AT_MIPS_stride:
3960 return "DW_AT_MIPS_stride";
3961 #endif
3964 name = get_DW_AT_name (attr);
3966 if (name != NULL)
3967 return name;
3969 return "DW_AT_<unknown>";
3972 /* Convert a DWARF value form code into its string name. */
3974 static const char *
3975 dwarf_form_name (unsigned int form)
3977 const char *name = get_DW_FORM_name (form);
3979 if (name != NULL)
3980 return name;
3982 return "DW_FORM_<unknown>";
3985 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3986 instance of an inlined instance of a decl which is local to an inline
3987 function, so we have to trace all of the way back through the origin chain
3988 to find out what sort of node actually served as the original seed for the
3989 given block. */
3991 static tree
3992 decl_ultimate_origin (const_tree decl)
3994 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3995 return NULL_TREE;
3997 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3998 we're trying to output the abstract instance of this function. */
3999 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4000 return NULL_TREE;
4002 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4003 most distant ancestor, this should never happen. */
4004 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4006 return DECL_ABSTRACT_ORIGIN (decl);
4009 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4010 of a virtual function may refer to a base class, so we check the 'this'
4011 parameter. */
4013 static tree
4014 decl_class_context (tree decl)
4016 tree context = NULL_TREE;
4018 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4019 context = DECL_CONTEXT (decl);
4020 else
4021 context = TYPE_MAIN_VARIANT
4022 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4024 if (context && !TYPE_P (context))
4025 context = NULL_TREE;
4027 return context;
4030 /* Add an attribute/value pair to a DIE. */
4032 static inline void
4033 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4035 /* Maybe this should be an assert? */
4036 if (die == NULL)
4037 return;
4039 vec_safe_reserve (die->die_attr, 1);
4040 vec_safe_push (die->die_attr, *attr);
4043 static inline enum dw_val_class
4044 AT_class (dw_attr_node *a)
4046 return a->dw_attr_val.val_class;
4049 /* Return the index for any attribute that will be referenced with a
4050 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4051 are stored in dw_attr_val.v.val_str for reference counting
4052 pruning. */
4054 static inline unsigned int
4055 AT_index (dw_attr_node *a)
4057 if (AT_class (a) == dw_val_class_str)
4058 return a->dw_attr_val.v.val_str->index;
4059 else if (a->dw_attr_val.val_entry != NULL)
4060 return a->dw_attr_val.val_entry->index;
4061 return NOT_INDEXED;
4064 /* Add a flag value attribute to a DIE. */
4066 static inline void
4067 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4069 dw_attr_node attr;
4071 attr.dw_attr = attr_kind;
4072 attr.dw_attr_val.val_class = dw_val_class_flag;
4073 attr.dw_attr_val.val_entry = NULL;
4074 attr.dw_attr_val.v.val_flag = flag;
4075 add_dwarf_attr (die, &attr);
4078 static inline unsigned
4079 AT_flag (dw_attr_node *a)
4081 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4082 return a->dw_attr_val.v.val_flag;
4085 /* Add a signed integer attribute value to a DIE. */
4087 static inline void
4088 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4090 dw_attr_node attr;
4092 attr.dw_attr = attr_kind;
4093 attr.dw_attr_val.val_class = dw_val_class_const;
4094 attr.dw_attr_val.val_entry = NULL;
4095 attr.dw_attr_val.v.val_int = int_val;
4096 add_dwarf_attr (die, &attr);
4099 static inline HOST_WIDE_INT
4100 AT_int (dw_attr_node *a)
4102 gcc_assert (a && (AT_class (a) == dw_val_class_const
4103 || AT_class (a) == dw_val_class_const_implicit));
4104 return a->dw_attr_val.v.val_int;
4107 /* Add an unsigned integer attribute value to a DIE. */
4109 static inline void
4110 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4111 unsigned HOST_WIDE_INT unsigned_val)
4113 dw_attr_node attr;
4115 attr.dw_attr = attr_kind;
4116 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4117 attr.dw_attr_val.val_entry = NULL;
4118 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4119 add_dwarf_attr (die, &attr);
4122 static inline unsigned HOST_WIDE_INT
4123 AT_unsigned (dw_attr_node *a)
4125 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4126 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4127 return a->dw_attr_val.v.val_unsigned;
4130 /* Add an unsigned wide integer attribute value to a DIE. */
4132 static inline void
4133 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4134 const wide_int& w)
4136 dw_attr_node attr;
4138 attr.dw_attr = attr_kind;
4139 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4140 attr.dw_attr_val.val_entry = NULL;
4141 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4142 *attr.dw_attr_val.v.val_wide = w;
4143 add_dwarf_attr (die, &attr);
4146 /* Add an unsigned double integer attribute value to a DIE. */
4148 static inline void
4149 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4150 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4152 dw_attr_node attr;
4154 attr.dw_attr = attr_kind;
4155 attr.dw_attr_val.val_class = dw_val_class_const_double;
4156 attr.dw_attr_val.val_entry = NULL;
4157 attr.dw_attr_val.v.val_double.high = high;
4158 attr.dw_attr_val.v.val_double.low = low;
4159 add_dwarf_attr (die, &attr);
4162 /* Add a floating point attribute value to a DIE and return it. */
4164 static inline void
4165 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4166 unsigned int length, unsigned int elt_size, unsigned char *array)
4168 dw_attr_node attr;
4170 attr.dw_attr = attr_kind;
4171 attr.dw_attr_val.val_class = dw_val_class_vec;
4172 attr.dw_attr_val.val_entry = NULL;
4173 attr.dw_attr_val.v.val_vec.length = length;
4174 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4175 attr.dw_attr_val.v.val_vec.array = array;
4176 add_dwarf_attr (die, &attr);
4179 /* Add an 8-byte data attribute value to a DIE. */
4181 static inline void
4182 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4183 unsigned char data8[8])
4185 dw_attr_node attr;
4187 attr.dw_attr = attr_kind;
4188 attr.dw_attr_val.val_class = dw_val_class_data8;
4189 attr.dw_attr_val.val_entry = NULL;
4190 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4191 add_dwarf_attr (die, &attr);
4194 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4195 dwarf_split_debug_info, address attributes in dies destined for the
4196 final executable have force_direct set to avoid using indexed
4197 references. */
4199 static inline void
4200 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4201 bool force_direct)
4203 dw_attr_node attr;
4204 char * lbl_id;
4206 lbl_id = xstrdup (lbl_low);
4207 attr.dw_attr = DW_AT_low_pc;
4208 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4209 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4210 if (dwarf_split_debug_info && !force_direct)
4211 attr.dw_attr_val.val_entry
4212 = add_addr_table_entry (lbl_id, ate_kind_label);
4213 else
4214 attr.dw_attr_val.val_entry = NULL;
4215 add_dwarf_attr (die, &attr);
4217 attr.dw_attr = DW_AT_high_pc;
4218 if (dwarf_version < 4)
4219 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4220 else
4221 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4222 lbl_id = xstrdup (lbl_high);
4223 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4224 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4225 && dwarf_split_debug_info && !force_direct)
4226 attr.dw_attr_val.val_entry
4227 = add_addr_table_entry (lbl_id, ate_kind_label);
4228 else
4229 attr.dw_attr_val.val_entry = NULL;
4230 add_dwarf_attr (die, &attr);
4233 /* Hash and equality functions for debug_str_hash. */
4235 hashval_t
4236 indirect_string_hasher::hash (indirect_string_node *x)
4238 return htab_hash_string (x->str);
4241 bool
4242 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4244 return strcmp (x1->str, x2) == 0;
4247 /* Add STR to the given string hash table. */
4249 static struct indirect_string_node *
4250 find_AT_string_in_table (const char *str,
4251 hash_table<indirect_string_hasher> *table)
4253 struct indirect_string_node *node;
4255 indirect_string_node **slot
4256 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4257 if (*slot == NULL)
4259 node = ggc_cleared_alloc<indirect_string_node> ();
4260 node->str = ggc_strdup (str);
4261 *slot = node;
4263 else
4264 node = *slot;
4266 node->refcount++;
4267 return node;
4270 /* Add STR to the indirect string hash table. */
4272 static struct indirect_string_node *
4273 find_AT_string (const char *str)
4275 if (! debug_str_hash)
4276 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4278 return find_AT_string_in_table (str, debug_str_hash);
4281 /* Add a string attribute value to a DIE. */
4283 static inline void
4284 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4286 dw_attr_node attr;
4287 struct indirect_string_node *node;
4289 node = find_AT_string (str);
4291 attr.dw_attr = attr_kind;
4292 attr.dw_attr_val.val_class = dw_val_class_str;
4293 attr.dw_attr_val.val_entry = NULL;
4294 attr.dw_attr_val.v.val_str = node;
4295 add_dwarf_attr (die, &attr);
4298 static inline const char *
4299 AT_string (dw_attr_node *a)
4301 gcc_assert (a && AT_class (a) == dw_val_class_str);
4302 return a->dw_attr_val.v.val_str->str;
4305 /* Call this function directly to bypass AT_string_form's logic to put
4306 the string inline in the die. */
4308 static void
4309 set_indirect_string (struct indirect_string_node *node)
4311 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4312 /* Already indirect is a no op. */
4313 if (node->form == DW_FORM_strp
4314 || node->form == DW_FORM_line_strp
4315 || node->form == DW_FORM_GNU_str_index)
4317 gcc_assert (node->label);
4318 return;
4320 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4321 ++dw2_string_counter;
4322 node->label = xstrdup (label);
4324 if (!dwarf_split_debug_info)
4326 node->form = DW_FORM_strp;
4327 node->index = NOT_INDEXED;
4329 else
4331 node->form = DW_FORM_GNU_str_index;
4332 node->index = NO_INDEX_ASSIGNED;
4336 /* Find out whether a string should be output inline in DIE
4337 or out-of-line in .debug_str section. */
4339 static enum dwarf_form
4340 find_string_form (struct indirect_string_node *node)
4342 unsigned int len;
4344 if (node->form)
4345 return node->form;
4347 len = strlen (node->str) + 1;
4349 /* If the string is shorter or equal to the size of the reference, it is
4350 always better to put it inline. */
4351 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4352 return node->form = DW_FORM_string;
4354 /* If we cannot expect the linker to merge strings in .debug_str
4355 section, only put it into .debug_str if it is worth even in this
4356 single module. */
4357 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4358 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4359 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4360 return node->form = DW_FORM_string;
4362 set_indirect_string (node);
4364 return node->form;
4367 /* Find out whether the string referenced from the attribute should be
4368 output inline in DIE or out-of-line in .debug_str section. */
4370 static enum dwarf_form
4371 AT_string_form (dw_attr_node *a)
4373 gcc_assert (a && AT_class (a) == dw_val_class_str);
4374 return find_string_form (a->dw_attr_val.v.val_str);
4377 /* Add a DIE reference attribute value to a DIE. */
4379 static inline void
4380 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4382 dw_attr_node attr;
4383 gcc_checking_assert (targ_die != NULL);
4385 /* With LTO we can end up trying to reference something we didn't create
4386 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4387 if (targ_die == NULL)
4388 return;
4390 attr.dw_attr = attr_kind;
4391 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4392 attr.dw_attr_val.val_entry = NULL;
4393 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4394 attr.dw_attr_val.v.val_die_ref.external = 0;
4395 add_dwarf_attr (die, &attr);
4398 /* Change DIE reference REF to point to NEW_DIE instead. */
4400 static inline void
4401 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4403 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4404 ref->dw_attr_val.v.val_die_ref.die = new_die;
4405 ref->dw_attr_val.v.val_die_ref.external = 0;
4408 /* Add an AT_specification attribute to a DIE, and also make the back
4409 pointer from the specification to the definition. */
4411 static inline void
4412 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4414 add_AT_die_ref (die, DW_AT_specification, targ_die);
4415 gcc_assert (!targ_die->die_definition);
4416 targ_die->die_definition = die;
4419 static inline dw_die_ref
4420 AT_ref (dw_attr_node *a)
4422 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4423 return a->dw_attr_val.v.val_die_ref.die;
4426 static inline int
4427 AT_ref_external (dw_attr_node *a)
4429 if (a && AT_class (a) == dw_val_class_die_ref)
4430 return a->dw_attr_val.v.val_die_ref.external;
4432 return 0;
4435 static inline void
4436 set_AT_ref_external (dw_attr_node *a, int i)
4438 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4439 a->dw_attr_val.v.val_die_ref.external = i;
4442 /* Add an FDE reference attribute value to a DIE. */
4444 static inline void
4445 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4447 dw_attr_node attr;
4449 attr.dw_attr = attr_kind;
4450 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4451 attr.dw_attr_val.val_entry = NULL;
4452 attr.dw_attr_val.v.val_fde_index = targ_fde;
4453 add_dwarf_attr (die, &attr);
4456 /* Add a location description attribute value to a DIE. */
4458 static inline void
4459 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4461 dw_attr_node attr;
4463 attr.dw_attr = attr_kind;
4464 attr.dw_attr_val.val_class = dw_val_class_loc;
4465 attr.dw_attr_val.val_entry = NULL;
4466 attr.dw_attr_val.v.val_loc = loc;
4467 add_dwarf_attr (die, &attr);
4470 static inline dw_loc_descr_ref
4471 AT_loc (dw_attr_node *a)
4473 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4474 return a->dw_attr_val.v.val_loc;
4477 static inline void
4478 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4480 dw_attr_node attr;
4482 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4483 return;
4485 attr.dw_attr = attr_kind;
4486 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4487 attr.dw_attr_val.val_entry = NULL;
4488 attr.dw_attr_val.v.val_loc_list = loc_list;
4489 add_dwarf_attr (die, &attr);
4490 have_location_lists = true;
4493 static inline dw_loc_list_ref
4494 AT_loc_list (dw_attr_node *a)
4496 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4497 return a->dw_attr_val.v.val_loc_list;
4500 static inline dw_loc_list_ref *
4501 AT_loc_list_ptr (dw_attr_node *a)
4503 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4504 return &a->dw_attr_val.v.val_loc_list;
4507 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4509 static hashval_t hash (addr_table_entry *);
4510 static bool equal (addr_table_entry *, addr_table_entry *);
4513 /* Table of entries into the .debug_addr section. */
4515 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4517 /* Hash an address_table_entry. */
4519 hashval_t
4520 addr_hasher::hash (addr_table_entry *a)
4522 inchash::hash hstate;
4523 switch (a->kind)
4525 case ate_kind_rtx:
4526 hstate.add_int (0);
4527 break;
4528 case ate_kind_rtx_dtprel:
4529 hstate.add_int (1);
4530 break;
4531 case ate_kind_label:
4532 return htab_hash_string (a->addr.label);
4533 default:
4534 gcc_unreachable ();
4536 inchash::add_rtx (a->addr.rtl, hstate);
4537 return hstate.end ();
4540 /* Determine equality for two address_table_entries. */
4542 bool
4543 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4545 if (a1->kind != a2->kind)
4546 return 0;
4547 switch (a1->kind)
4549 case ate_kind_rtx:
4550 case ate_kind_rtx_dtprel:
4551 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4552 case ate_kind_label:
4553 return strcmp (a1->addr.label, a2->addr.label) == 0;
4554 default:
4555 gcc_unreachable ();
4559 /* Initialize an addr_table_entry. */
4561 void
4562 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4564 e->kind = kind;
4565 switch (kind)
4567 case ate_kind_rtx:
4568 case ate_kind_rtx_dtprel:
4569 e->addr.rtl = (rtx) addr;
4570 break;
4571 case ate_kind_label:
4572 e->addr.label = (char *) addr;
4573 break;
4575 e->refcount = 0;
4576 e->index = NO_INDEX_ASSIGNED;
4579 /* Add attr to the address table entry to the table. Defer setting an
4580 index until output time. */
4582 static addr_table_entry *
4583 add_addr_table_entry (void *addr, enum ate_kind kind)
4585 addr_table_entry *node;
4586 addr_table_entry finder;
4588 gcc_assert (dwarf_split_debug_info);
4589 if (! addr_index_table)
4590 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4591 init_addr_table_entry (&finder, kind, addr);
4592 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4594 if (*slot == HTAB_EMPTY_ENTRY)
4596 node = ggc_cleared_alloc<addr_table_entry> ();
4597 init_addr_table_entry (node, kind, addr);
4598 *slot = node;
4600 else
4601 node = *slot;
4603 node->refcount++;
4604 return node;
4607 /* Remove an entry from the addr table by decrementing its refcount.
4608 Strictly, decrementing the refcount would be enough, but the
4609 assertion that the entry is actually in the table has found
4610 bugs. */
4612 static void
4613 remove_addr_table_entry (addr_table_entry *entry)
4615 gcc_assert (dwarf_split_debug_info && addr_index_table);
4616 /* After an index is assigned, the table is frozen. */
4617 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4618 entry->refcount--;
4621 /* Given a location list, remove all addresses it refers to from the
4622 address_table. */
4624 static void
4625 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4627 for (; descr; descr = descr->dw_loc_next)
4628 if (descr->dw_loc_oprnd1.val_entry != NULL)
4630 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4631 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4635 /* A helper function for dwarf2out_finish called through
4636 htab_traverse. Assign an addr_table_entry its index. All entries
4637 must be collected into the table when this function is called,
4638 because the indexing code relies on htab_traverse to traverse nodes
4639 in the same order for each run. */
4642 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4644 addr_table_entry *node = *h;
4646 /* Don't index unreferenced nodes. */
4647 if (node->refcount == 0)
4648 return 1;
4650 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4651 node->index = *index;
4652 *index += 1;
4654 return 1;
4657 /* Add an address constant attribute value to a DIE. When using
4658 dwarf_split_debug_info, address attributes in dies destined for the
4659 final executable should be direct references--setting the parameter
4660 force_direct ensures this behavior. */
4662 static inline void
4663 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4664 bool force_direct)
4666 dw_attr_node attr;
4668 attr.dw_attr = attr_kind;
4669 attr.dw_attr_val.val_class = dw_val_class_addr;
4670 attr.dw_attr_val.v.val_addr = addr;
4671 if (dwarf_split_debug_info && !force_direct)
4672 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4673 else
4674 attr.dw_attr_val.val_entry = NULL;
4675 add_dwarf_attr (die, &attr);
4678 /* Get the RTX from to an address DIE attribute. */
4680 static inline rtx
4681 AT_addr (dw_attr_node *a)
4683 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4684 return a->dw_attr_val.v.val_addr;
4687 /* Add a file attribute value to a DIE. */
4689 static inline void
4690 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4691 struct dwarf_file_data *fd)
4693 dw_attr_node attr;
4695 attr.dw_attr = attr_kind;
4696 attr.dw_attr_val.val_class = dw_val_class_file;
4697 attr.dw_attr_val.val_entry = NULL;
4698 attr.dw_attr_val.v.val_file = fd;
4699 add_dwarf_attr (die, &attr);
4702 /* Get the dwarf_file_data from a file DIE attribute. */
4704 static inline struct dwarf_file_data *
4705 AT_file (dw_attr_node *a)
4707 gcc_assert (a && (AT_class (a) == dw_val_class_file
4708 || AT_class (a) == dw_val_class_file_implicit));
4709 return a->dw_attr_val.v.val_file;
4712 /* Add a vms delta attribute value to a DIE. */
4714 static inline void
4715 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4716 const char *lbl1, const char *lbl2)
4718 dw_attr_node attr;
4720 attr.dw_attr = attr_kind;
4721 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4722 attr.dw_attr_val.val_entry = NULL;
4723 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4724 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4725 add_dwarf_attr (die, &attr);
4728 /* Add a label identifier attribute value to a DIE. */
4730 static inline void
4731 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4732 const char *lbl_id)
4734 dw_attr_node attr;
4736 attr.dw_attr = attr_kind;
4737 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4738 attr.dw_attr_val.val_entry = NULL;
4739 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4740 if (dwarf_split_debug_info)
4741 attr.dw_attr_val.val_entry
4742 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4743 ate_kind_label);
4744 add_dwarf_attr (die, &attr);
4747 /* Add a section offset attribute value to a DIE, an offset into the
4748 debug_line section. */
4750 static inline void
4751 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4752 const char *label)
4754 dw_attr_node attr;
4756 attr.dw_attr = attr_kind;
4757 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4758 attr.dw_attr_val.val_entry = NULL;
4759 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4760 add_dwarf_attr (die, &attr);
4763 /* Add a section offset attribute value to a DIE, an offset into the
4764 debug_loclists section. */
4766 static inline void
4767 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4768 const char *label)
4770 dw_attr_node attr;
4772 attr.dw_attr = attr_kind;
4773 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4774 attr.dw_attr_val.val_entry = NULL;
4775 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4776 add_dwarf_attr (die, &attr);
4779 /* Add a section offset attribute value to a DIE, an offset into the
4780 debug_macinfo section. */
4782 static inline void
4783 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4784 const char *label)
4786 dw_attr_node attr;
4788 attr.dw_attr = attr_kind;
4789 attr.dw_attr_val.val_class = dw_val_class_macptr;
4790 attr.dw_attr_val.val_entry = NULL;
4791 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4792 add_dwarf_attr (die, &attr);
4795 /* Add an offset attribute value to a DIE. */
4797 static inline void
4798 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4799 unsigned HOST_WIDE_INT offset)
4801 dw_attr_node attr;
4803 attr.dw_attr = attr_kind;
4804 attr.dw_attr_val.val_class = dw_val_class_offset;
4805 attr.dw_attr_val.val_entry = NULL;
4806 attr.dw_attr_val.v.val_offset = offset;
4807 add_dwarf_attr (die, &attr);
4810 /* Add a range_list attribute value to a DIE. When using
4811 dwarf_split_debug_info, address attributes in dies destined for the
4812 final executable should be direct references--setting the parameter
4813 force_direct ensures this behavior. */
4815 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4816 #define RELOCATED_OFFSET (NULL)
4818 static void
4819 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4820 long unsigned int offset, bool force_direct)
4822 dw_attr_node attr;
4824 attr.dw_attr = attr_kind;
4825 attr.dw_attr_val.val_class = dw_val_class_range_list;
4826 /* For the range_list attribute, use val_entry to store whether the
4827 offset should follow split-debug-info or normal semantics. This
4828 value is read in output_range_list_offset. */
4829 if (dwarf_split_debug_info && !force_direct)
4830 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4831 else
4832 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4833 attr.dw_attr_val.v.val_offset = offset;
4834 add_dwarf_attr (die, &attr);
4837 /* Return the start label of a delta attribute. */
4839 static inline const char *
4840 AT_vms_delta1 (dw_attr_node *a)
4842 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4843 return a->dw_attr_val.v.val_vms_delta.lbl1;
4846 /* Return the end label of a delta attribute. */
4848 static inline const char *
4849 AT_vms_delta2 (dw_attr_node *a)
4851 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4852 return a->dw_attr_val.v.val_vms_delta.lbl2;
4855 static inline const char *
4856 AT_lbl (dw_attr_node *a)
4858 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4859 || AT_class (a) == dw_val_class_lineptr
4860 || AT_class (a) == dw_val_class_macptr
4861 || AT_class (a) == dw_val_class_loclistsptr
4862 || AT_class (a) == dw_val_class_high_pc));
4863 return a->dw_attr_val.v.val_lbl_id;
4866 /* Get the attribute of type attr_kind. */
4868 static dw_attr_node *
4869 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4871 dw_attr_node *a;
4872 unsigned ix;
4873 dw_die_ref spec = NULL;
4875 if (! die)
4876 return NULL;
4878 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4879 if (a->dw_attr == attr_kind)
4880 return a;
4881 else if (a->dw_attr == DW_AT_specification
4882 || a->dw_attr == DW_AT_abstract_origin)
4883 spec = AT_ref (a);
4885 if (spec)
4886 return get_AT (spec, attr_kind);
4888 return NULL;
4891 /* Returns the parent of the declaration of DIE. */
4893 static dw_die_ref
4894 get_die_parent (dw_die_ref die)
4896 dw_die_ref t;
4898 if (!die)
4899 return NULL;
4901 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4902 || (t = get_AT_ref (die, DW_AT_specification)))
4903 die = t;
4905 return die->die_parent;
4908 /* Return the "low pc" attribute value, typically associated with a subprogram
4909 DIE. Return null if the "low pc" attribute is either not present, or if it
4910 cannot be represented as an assembler label identifier. */
4912 static inline const char *
4913 get_AT_low_pc (dw_die_ref die)
4915 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4917 return a ? AT_lbl (a) : NULL;
4920 /* Return the "high pc" attribute value, typically associated with a subprogram
4921 DIE. Return null if the "high pc" attribute is either not present, or if it
4922 cannot be represented as an assembler label identifier. */
4924 static inline const char *
4925 get_AT_hi_pc (dw_die_ref die)
4927 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4929 return a ? AT_lbl (a) : NULL;
4932 /* Return the value of the string attribute designated by ATTR_KIND, or
4933 NULL if it is not present. */
4935 static inline const char *
4936 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4938 dw_attr_node *a = get_AT (die, attr_kind);
4940 return a ? AT_string (a) : NULL;
4943 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4944 if it is not present. */
4946 static inline int
4947 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4949 dw_attr_node *a = get_AT (die, attr_kind);
4951 return a ? AT_flag (a) : 0;
4954 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4955 if it is not present. */
4957 static inline unsigned
4958 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4960 dw_attr_node *a = get_AT (die, attr_kind);
4962 return a ? AT_unsigned (a) : 0;
4965 static inline dw_die_ref
4966 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4968 dw_attr_node *a = get_AT (die, attr_kind);
4970 return a ? AT_ref (a) : NULL;
4973 static inline struct dwarf_file_data *
4974 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4976 dw_attr_node *a = get_AT (die, attr_kind);
4978 return a ? AT_file (a) : NULL;
4981 /* Return TRUE if the language is C++. */
4983 static inline bool
4984 is_cxx (void)
4986 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4988 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4989 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4992 /* Return TRUE if the language is Java. */
4994 static inline bool
4995 is_java (void)
4997 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4999 return lang == DW_LANG_Java;
5002 /* Return TRUE if the language is Fortran. */
5004 static inline bool
5005 is_fortran (void)
5007 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5009 return (lang == DW_LANG_Fortran77
5010 || lang == DW_LANG_Fortran90
5011 || lang == DW_LANG_Fortran95
5012 || lang == DW_LANG_Fortran03
5013 || lang == DW_LANG_Fortran08);
5016 /* Return TRUE if the language is Ada. */
5018 static inline bool
5019 is_ada (void)
5021 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5023 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5026 /* Remove the specified attribute if present. Return TRUE if removal
5027 was successful. */
5029 static bool
5030 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5032 dw_attr_node *a;
5033 unsigned ix;
5035 if (! die)
5036 return false;
5038 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5039 if (a->dw_attr == attr_kind)
5041 if (AT_class (a) == dw_val_class_str)
5042 if (a->dw_attr_val.v.val_str->refcount)
5043 a->dw_attr_val.v.val_str->refcount--;
5045 /* vec::ordered_remove should help reduce the number of abbrevs
5046 that are needed. */
5047 die->die_attr->ordered_remove (ix);
5048 return true;
5050 return false;
5053 /* Remove CHILD from its parent. PREV must have the property that
5054 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5056 static void
5057 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5059 gcc_assert (child->die_parent == prev->die_parent);
5060 gcc_assert (prev->die_sib == child);
5061 if (prev == child)
5063 gcc_assert (child->die_parent->die_child == child);
5064 prev = NULL;
5066 else
5067 prev->die_sib = child->die_sib;
5068 if (child->die_parent->die_child == child)
5069 child->die_parent->die_child = prev;
5070 child->die_sib = NULL;
5073 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5074 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5076 static void
5077 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5079 dw_die_ref parent = old_child->die_parent;
5081 gcc_assert (parent == prev->die_parent);
5082 gcc_assert (prev->die_sib == old_child);
5084 new_child->die_parent = parent;
5085 if (prev == old_child)
5087 gcc_assert (parent->die_child == old_child);
5088 new_child->die_sib = new_child;
5090 else
5092 prev->die_sib = new_child;
5093 new_child->die_sib = old_child->die_sib;
5095 if (old_child->die_parent->die_child == old_child)
5096 old_child->die_parent->die_child = new_child;
5097 old_child->die_sib = NULL;
5100 /* Move all children from OLD_PARENT to NEW_PARENT. */
5102 static void
5103 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5105 dw_die_ref c;
5106 new_parent->die_child = old_parent->die_child;
5107 old_parent->die_child = NULL;
5108 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5111 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5112 matches TAG. */
5114 static void
5115 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5117 dw_die_ref c;
5119 c = die->die_child;
5120 if (c) do {
5121 dw_die_ref prev = c;
5122 c = c->die_sib;
5123 while (c->die_tag == tag)
5125 remove_child_with_prev (c, prev);
5126 c->die_parent = NULL;
5127 /* Might have removed every child. */
5128 if (die->die_child == NULL)
5129 return;
5130 c = prev->die_sib;
5132 } while (c != die->die_child);
5135 /* Add a CHILD_DIE as the last child of DIE. */
5137 static void
5138 add_child_die (dw_die_ref die, dw_die_ref child_die)
5140 /* FIXME this should probably be an assert. */
5141 if (! die || ! child_die)
5142 return;
5143 gcc_assert (die != child_die);
5145 child_die->die_parent = die;
5146 if (die->die_child)
5148 child_die->die_sib = die->die_child->die_sib;
5149 die->die_child->die_sib = child_die;
5151 else
5152 child_die->die_sib = child_die;
5153 die->die_child = child_die;
5156 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5158 static void
5159 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5160 dw_die_ref after_die)
5162 gcc_assert (die
5163 && child_die
5164 && after_die
5165 && die->die_child
5166 && die != child_die);
5168 child_die->die_parent = die;
5169 child_die->die_sib = after_die->die_sib;
5170 after_die->die_sib = child_die;
5171 if (die->die_child == after_die)
5172 die->die_child = child_die;
5175 /* Unassociate CHILD from its parent, and make its parent be
5176 NEW_PARENT. */
5178 static void
5179 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5181 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5182 if (p->die_sib == child)
5184 remove_child_with_prev (child, p);
5185 break;
5187 add_child_die (new_parent, child);
5190 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5191 is the specification, to the end of PARENT's list of children.
5192 This is done by removing and re-adding it. */
5194 static void
5195 splice_child_die (dw_die_ref parent, dw_die_ref child)
5197 /* We want the declaration DIE from inside the class, not the
5198 specification DIE at toplevel. */
5199 if (child->die_parent != parent)
5201 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5203 if (tmp)
5204 child = tmp;
5207 gcc_assert (child->die_parent == parent
5208 || (child->die_parent
5209 == get_AT_ref (parent, DW_AT_specification)));
5211 reparent_child (child, parent);
5214 /* Create and return a new die with a parent of PARENT_DIE. If
5215 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5216 associated tree T must be supplied to determine parenthood
5217 later. */
5219 static inline dw_die_ref
5220 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5222 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5224 die->die_tag = tag_value;
5226 if (parent_die != NULL)
5227 add_child_die (parent_die, die);
5228 else
5230 limbo_die_node *limbo_node;
5232 /* No DIEs created after early dwarf should end up in limbo,
5233 because the limbo list should not persist past LTO
5234 streaming. */
5235 if (tag_value != DW_TAG_compile_unit
5236 /* These are allowed because they're generated while
5237 breaking out COMDAT units late. */
5238 && tag_value != DW_TAG_type_unit
5239 && tag_value != DW_TAG_skeleton_unit
5240 && !early_dwarf
5241 /* Allow nested functions to live in limbo because they will
5242 only temporarily live there, as decls_for_scope will fix
5243 them up. */
5244 && (TREE_CODE (t) != FUNCTION_DECL
5245 || !decl_function_context (t))
5246 /* Same as nested functions above but for types. Types that
5247 are local to a function will be fixed in
5248 decls_for_scope. */
5249 && (!RECORD_OR_UNION_TYPE_P (t)
5250 || !TYPE_CONTEXT (t)
5251 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5252 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5253 especially in the ltrans stage, but once we implement LTO
5254 dwarf streaming, we should remove this exception. */
5255 && !in_lto_p)
5257 fprintf (stderr, "symbol ended up in limbo too late:");
5258 debug_generic_stmt (t);
5259 gcc_unreachable ();
5262 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5263 limbo_node->die = die;
5264 limbo_node->created_for = t;
5265 limbo_node->next = limbo_die_list;
5266 limbo_die_list = limbo_node;
5269 return die;
5272 /* Return the DIE associated with the given type specifier. */
5274 static inline dw_die_ref
5275 lookup_type_die (tree type)
5277 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5278 if (die && die->removed)
5280 TYPE_SYMTAB_DIE (type) = NULL;
5281 return NULL;
5283 return die;
5286 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5287 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5288 anonymous type instead the one of the naming typedef. */
5290 static inline dw_die_ref
5291 strip_naming_typedef (tree type, dw_die_ref type_die)
5293 if (type
5294 && TREE_CODE (type) == RECORD_TYPE
5295 && type_die
5296 && type_die->die_tag == DW_TAG_typedef
5297 && is_naming_typedef_decl (TYPE_NAME (type)))
5298 type_die = get_AT_ref (type_die, DW_AT_type);
5299 return type_die;
5302 /* Like lookup_type_die, but if type is an anonymous type named by a
5303 typedef[1], return the DIE of the anonymous type instead the one of
5304 the naming typedef. This is because in gen_typedef_die, we did
5305 equate the anonymous struct named by the typedef with the DIE of
5306 the naming typedef. So by default, lookup_type_die on an anonymous
5307 struct yields the DIE of the naming typedef.
5309 [1]: Read the comment of is_naming_typedef_decl to learn about what
5310 a naming typedef is. */
5312 static inline dw_die_ref
5313 lookup_type_die_strip_naming_typedef (tree type)
5315 dw_die_ref die = lookup_type_die (type);
5316 return strip_naming_typedef (type, die);
5319 /* Equate a DIE to a given type specifier. */
5321 static inline void
5322 equate_type_number_to_die (tree type, dw_die_ref type_die)
5324 TYPE_SYMTAB_DIE (type) = type_die;
5327 /* Returns a hash value for X (which really is a die_struct). */
5329 inline hashval_t
5330 decl_die_hasher::hash (die_node *x)
5332 return (hashval_t) x->decl_id;
5335 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5337 inline bool
5338 decl_die_hasher::equal (die_node *x, tree y)
5340 return (x->decl_id == DECL_UID (y));
5343 /* Return the DIE associated with a given declaration. */
5345 static inline dw_die_ref
5346 lookup_decl_die (tree decl)
5348 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5349 NO_INSERT);
5350 if (!die)
5351 return NULL;
5352 if ((*die)->removed)
5354 decl_die_table->clear_slot (die);
5355 return NULL;
5357 return *die;
5360 /* Returns a hash value for X (which really is a var_loc_list). */
5362 inline hashval_t
5363 decl_loc_hasher::hash (var_loc_list *x)
5365 return (hashval_t) x->decl_id;
5368 /* Return nonzero if decl_id of var_loc_list X is the same as
5369 UID of decl *Y. */
5371 inline bool
5372 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5374 return (x->decl_id == DECL_UID (y));
5377 /* Return the var_loc list associated with a given declaration. */
5379 static inline var_loc_list *
5380 lookup_decl_loc (const_tree decl)
5382 if (!decl_loc_table)
5383 return NULL;
5384 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5387 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5389 inline hashval_t
5390 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5392 return (hashval_t) x->decl_id;
5395 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5396 UID of decl *Y. */
5398 inline bool
5399 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5401 return (x->decl_id == DECL_UID (y));
5404 /* Equate a DIE to a particular declaration. */
5406 static void
5407 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5409 unsigned int decl_id = DECL_UID (decl);
5411 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5412 decl_die->decl_id = decl_id;
5415 /* Return how many bits covers PIECE EXPR_LIST. */
5417 static HOST_WIDE_INT
5418 decl_piece_bitsize (rtx piece)
5420 int ret = (int) GET_MODE (piece);
5421 if (ret)
5422 return ret;
5423 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5424 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5425 return INTVAL (XEXP (XEXP (piece, 0), 0));
5428 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5430 static rtx *
5431 decl_piece_varloc_ptr (rtx piece)
5433 if ((int) GET_MODE (piece))
5434 return &XEXP (piece, 0);
5435 else
5436 return &XEXP (XEXP (piece, 0), 1);
5439 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5440 Next is the chain of following piece nodes. */
5442 static rtx_expr_list *
5443 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5445 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5446 return alloc_EXPR_LIST (bitsize, loc_note, next);
5447 else
5448 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5449 GEN_INT (bitsize),
5450 loc_note), next);
5453 /* Return rtx that should be stored into loc field for
5454 LOC_NOTE and BITPOS/BITSIZE. */
5456 static rtx
5457 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5458 HOST_WIDE_INT bitsize)
5460 if (bitsize != -1)
5462 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5463 if (bitpos != 0)
5464 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5466 return loc_note;
5469 /* This function either modifies location piece list *DEST in
5470 place (if SRC and INNER is NULL), or copies location piece list
5471 *SRC to *DEST while modifying it. Location BITPOS is modified
5472 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5473 not copied and if needed some padding around it is added.
5474 When modifying in place, DEST should point to EXPR_LIST where
5475 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5476 to the start of the whole list and INNER points to the EXPR_LIST
5477 where earlier pieces cover PIECE_BITPOS bits. */
5479 static void
5480 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5481 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5482 HOST_WIDE_INT bitsize, rtx loc_note)
5484 HOST_WIDE_INT diff;
5485 bool copy = inner != NULL;
5487 if (copy)
5489 /* First copy all nodes preceding the current bitpos. */
5490 while (src != inner)
5492 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5493 decl_piece_bitsize (*src), NULL_RTX);
5494 dest = &XEXP (*dest, 1);
5495 src = &XEXP (*src, 1);
5498 /* Add padding if needed. */
5499 if (bitpos != piece_bitpos)
5501 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5502 copy ? NULL_RTX : *dest);
5503 dest = &XEXP (*dest, 1);
5505 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5507 gcc_assert (!copy);
5508 /* A piece with correct bitpos and bitsize already exist,
5509 just update the location for it and return. */
5510 *decl_piece_varloc_ptr (*dest) = loc_note;
5511 return;
5513 /* Add the piece that changed. */
5514 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5515 dest = &XEXP (*dest, 1);
5516 /* Skip over pieces that overlap it. */
5517 diff = bitpos - piece_bitpos + bitsize;
5518 if (!copy)
5519 src = dest;
5520 while (diff > 0 && *src)
5522 rtx piece = *src;
5523 diff -= decl_piece_bitsize (piece);
5524 if (copy)
5525 src = &XEXP (piece, 1);
5526 else
5528 *src = XEXP (piece, 1);
5529 free_EXPR_LIST_node (piece);
5532 /* Add padding if needed. */
5533 if (diff < 0 && *src)
5535 if (!copy)
5536 dest = src;
5537 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5538 dest = &XEXP (*dest, 1);
5540 if (!copy)
5541 return;
5542 /* Finally copy all nodes following it. */
5543 while (*src)
5545 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5546 decl_piece_bitsize (*src), NULL_RTX);
5547 dest = &XEXP (*dest, 1);
5548 src = &XEXP (*src, 1);
5552 /* Add a variable location node to the linked list for DECL. */
5554 static struct var_loc_node *
5555 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5557 unsigned int decl_id;
5558 var_loc_list *temp;
5559 struct var_loc_node *loc = NULL;
5560 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5562 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5564 tree realdecl = DECL_DEBUG_EXPR (decl);
5565 if (handled_component_p (realdecl)
5566 || (TREE_CODE (realdecl) == MEM_REF
5567 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5569 HOST_WIDE_INT maxsize;
5570 bool reverse;
5571 tree innerdecl
5572 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5573 &reverse);
5574 if (!DECL_P (innerdecl)
5575 || DECL_IGNORED_P (innerdecl)
5576 || TREE_STATIC (innerdecl)
5577 || bitsize <= 0
5578 || bitpos + bitsize > 256
5579 || bitsize != maxsize)
5580 return NULL;
5581 decl = innerdecl;
5585 decl_id = DECL_UID (decl);
5586 var_loc_list **slot
5587 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5588 if (*slot == NULL)
5590 temp = ggc_cleared_alloc<var_loc_list> ();
5591 temp->decl_id = decl_id;
5592 *slot = temp;
5594 else
5595 temp = *slot;
5597 /* For PARM_DECLs try to keep around the original incoming value,
5598 even if that means we'll emit a zero-range .debug_loc entry. */
5599 if (temp->last
5600 && temp->first == temp->last
5601 && TREE_CODE (decl) == PARM_DECL
5602 && NOTE_P (temp->first->loc)
5603 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5604 && DECL_INCOMING_RTL (decl)
5605 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5606 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5607 == GET_CODE (DECL_INCOMING_RTL (decl))
5608 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5609 && (bitsize != -1
5610 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5611 NOTE_VAR_LOCATION_LOC (loc_note))
5612 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5613 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5615 loc = ggc_cleared_alloc<var_loc_node> ();
5616 temp->first->next = loc;
5617 temp->last = loc;
5618 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5620 else if (temp->last)
5622 struct var_loc_node *last = temp->last, *unused = NULL;
5623 rtx *piece_loc = NULL, last_loc_note;
5624 HOST_WIDE_INT piece_bitpos = 0;
5625 if (last->next)
5627 last = last->next;
5628 gcc_assert (last->next == NULL);
5630 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5632 piece_loc = &last->loc;
5635 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5636 if (piece_bitpos + cur_bitsize > bitpos)
5637 break;
5638 piece_bitpos += cur_bitsize;
5639 piece_loc = &XEXP (*piece_loc, 1);
5641 while (*piece_loc);
5643 /* TEMP->LAST here is either pointer to the last but one or
5644 last element in the chained list, LAST is pointer to the
5645 last element. */
5646 if (label && strcmp (last->label, label) == 0)
5648 /* For SRA optimized variables if there weren't any real
5649 insns since last note, just modify the last node. */
5650 if (piece_loc != NULL)
5652 adjust_piece_list (piece_loc, NULL, NULL,
5653 bitpos, piece_bitpos, bitsize, loc_note);
5654 return NULL;
5656 /* If the last note doesn't cover any instructions, remove it. */
5657 if (temp->last != last)
5659 temp->last->next = NULL;
5660 unused = last;
5661 last = temp->last;
5662 gcc_assert (strcmp (last->label, label) != 0);
5664 else
5666 gcc_assert (temp->first == temp->last
5667 || (temp->first->next == temp->last
5668 && TREE_CODE (decl) == PARM_DECL));
5669 memset (temp->last, '\0', sizeof (*temp->last));
5670 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5671 return temp->last;
5674 if (bitsize == -1 && NOTE_P (last->loc))
5675 last_loc_note = last->loc;
5676 else if (piece_loc != NULL
5677 && *piece_loc != NULL_RTX
5678 && piece_bitpos == bitpos
5679 && decl_piece_bitsize (*piece_loc) == bitsize)
5680 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5681 else
5682 last_loc_note = NULL_RTX;
5683 /* If the current location is the same as the end of the list,
5684 and either both or neither of the locations is uninitialized,
5685 we have nothing to do. */
5686 if (last_loc_note == NULL_RTX
5687 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5688 NOTE_VAR_LOCATION_LOC (loc_note)))
5689 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5690 != NOTE_VAR_LOCATION_STATUS (loc_note))
5691 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5692 == VAR_INIT_STATUS_UNINITIALIZED)
5693 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5694 == VAR_INIT_STATUS_UNINITIALIZED))))
5696 /* Add LOC to the end of list and update LAST. If the last
5697 element of the list has been removed above, reuse its
5698 memory for the new node, otherwise allocate a new one. */
5699 if (unused)
5701 loc = unused;
5702 memset (loc, '\0', sizeof (*loc));
5704 else
5705 loc = ggc_cleared_alloc<var_loc_node> ();
5706 if (bitsize == -1 || piece_loc == NULL)
5707 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5708 else
5709 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5710 bitpos, piece_bitpos, bitsize, loc_note);
5711 last->next = loc;
5712 /* Ensure TEMP->LAST will point either to the new last but one
5713 element of the chain, or to the last element in it. */
5714 if (last != temp->last)
5715 temp->last = last;
5717 else if (unused)
5718 ggc_free (unused);
5720 else
5722 loc = ggc_cleared_alloc<var_loc_node> ();
5723 temp->first = loc;
5724 temp->last = loc;
5725 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5727 return loc;
5730 /* Keep track of the number of spaces used to indent the
5731 output of the debugging routines that print the structure of
5732 the DIE internal representation. */
5733 static int print_indent;
5735 /* Indent the line the number of spaces given by print_indent. */
5737 static inline void
5738 print_spaces (FILE *outfile)
5740 fprintf (outfile, "%*s", print_indent, "");
5743 /* Print a type signature in hex. */
5745 static inline void
5746 print_signature (FILE *outfile, char *sig)
5748 int i;
5750 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5751 fprintf (outfile, "%02x", sig[i] & 0xff);
5754 static inline void
5755 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5757 if (discr_value->pos)
5758 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5759 else
5760 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5763 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5765 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5766 RECURSE, output location descriptor operations. */
5768 static void
5769 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5771 switch (val->val_class)
5773 case dw_val_class_addr:
5774 fprintf (outfile, "address");
5775 break;
5776 case dw_val_class_offset:
5777 fprintf (outfile, "offset");
5778 break;
5779 case dw_val_class_loc:
5780 fprintf (outfile, "location descriptor");
5781 if (val->v.val_loc == NULL)
5782 fprintf (outfile, " -> <null>\n");
5783 else if (recurse)
5785 fprintf (outfile, ":\n");
5786 print_indent += 4;
5787 print_loc_descr (val->v.val_loc, outfile);
5788 print_indent -= 4;
5790 else
5791 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5792 break;
5793 case dw_val_class_loc_list:
5794 fprintf (outfile, "location list -> label:%s",
5795 val->v.val_loc_list->ll_symbol);
5796 break;
5797 case dw_val_class_range_list:
5798 fprintf (outfile, "range list");
5799 break;
5800 case dw_val_class_const:
5801 case dw_val_class_const_implicit:
5802 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5803 break;
5804 case dw_val_class_unsigned_const:
5805 case dw_val_class_unsigned_const_implicit:
5806 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5807 break;
5808 case dw_val_class_const_double:
5809 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5810 HOST_WIDE_INT_PRINT_UNSIGNED")",
5811 val->v.val_double.high,
5812 val->v.val_double.low);
5813 break;
5814 case dw_val_class_wide_int:
5816 int i = val->v.val_wide->get_len ();
5817 fprintf (outfile, "constant (");
5818 gcc_assert (i > 0);
5819 if (val->v.val_wide->elt (i - 1) == 0)
5820 fprintf (outfile, "0x");
5821 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5822 val->v.val_wide->elt (--i));
5823 while (--i >= 0)
5824 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5825 val->v.val_wide->elt (i));
5826 fprintf (outfile, ")");
5827 break;
5829 case dw_val_class_vec:
5830 fprintf (outfile, "floating-point or vector constant");
5831 break;
5832 case dw_val_class_flag:
5833 fprintf (outfile, "%u", val->v.val_flag);
5834 break;
5835 case dw_val_class_die_ref:
5836 if (val->v.val_die_ref.die != NULL)
5838 dw_die_ref die = val->v.val_die_ref.die;
5840 if (die->comdat_type_p)
5842 fprintf (outfile, "die -> signature: ");
5843 print_signature (outfile,
5844 die->die_id.die_type_node->signature);
5846 else if (die->die_id.die_symbol)
5847 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5848 else
5849 fprintf (outfile, "die -> %ld", die->die_offset);
5850 fprintf (outfile, " (%p)", (void *) die);
5852 else
5853 fprintf (outfile, "die -> <null>");
5854 break;
5855 case dw_val_class_vms_delta:
5856 fprintf (outfile, "delta: @slotcount(%s-%s)",
5857 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5858 break;
5859 case dw_val_class_lbl_id:
5860 case dw_val_class_lineptr:
5861 case dw_val_class_macptr:
5862 case dw_val_class_loclistsptr:
5863 case dw_val_class_high_pc:
5864 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5865 break;
5866 case dw_val_class_str:
5867 if (val->v.val_str->str != NULL)
5868 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5869 else
5870 fprintf (outfile, "<null>");
5871 break;
5872 case dw_val_class_file:
5873 case dw_val_class_file_implicit:
5874 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5875 val->v.val_file->emitted_number);
5876 break;
5877 case dw_val_class_data8:
5879 int i;
5881 for (i = 0; i < 8; i++)
5882 fprintf (outfile, "%02x", val->v.val_data8[i]);
5883 break;
5885 case dw_val_class_discr_value:
5886 print_discr_value (outfile, &val->v.val_discr_value);
5887 break;
5888 case dw_val_class_discr_list:
5889 for (dw_discr_list_ref node = val->v.val_discr_list;
5890 node != NULL;
5891 node = node->dw_discr_next)
5893 if (node->dw_discr_range)
5895 fprintf (outfile, " .. ");
5896 print_discr_value (outfile, &node->dw_discr_lower_bound);
5897 print_discr_value (outfile, &node->dw_discr_upper_bound);
5899 else
5900 print_discr_value (outfile, &node->dw_discr_lower_bound);
5902 if (node->dw_discr_next != NULL)
5903 fprintf (outfile, " | ");
5905 default:
5906 break;
5910 /* Likewise, for a DIE attribute. */
5912 static void
5913 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5915 print_dw_val (&a->dw_attr_val, recurse, outfile);
5919 /* Print the list of operands in the LOC location description to OUTFILE. This
5920 routine is a debugging aid only. */
5922 static void
5923 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5925 dw_loc_descr_ref l = loc;
5927 if (loc == NULL)
5929 print_spaces (outfile);
5930 fprintf (outfile, "<null>\n");
5931 return;
5934 for (l = loc; l != NULL; l = l->dw_loc_next)
5936 print_spaces (outfile);
5937 fprintf (outfile, "(%p) %s",
5938 (void *) l,
5939 dwarf_stack_op_name (l->dw_loc_opc));
5940 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5942 fprintf (outfile, " ");
5943 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5945 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5947 fprintf (outfile, ", ");
5948 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5950 fprintf (outfile, "\n");
5954 /* Print the information associated with a given DIE, and its children.
5955 This routine is a debugging aid only. */
5957 static void
5958 print_die (dw_die_ref die, FILE *outfile)
5960 dw_attr_node *a;
5961 dw_die_ref c;
5962 unsigned ix;
5964 print_spaces (outfile);
5965 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5966 die->die_offset, dwarf_tag_name (die->die_tag),
5967 (void*) die);
5968 print_spaces (outfile);
5969 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5970 fprintf (outfile, " offset: %ld", die->die_offset);
5971 fprintf (outfile, " mark: %d\n", die->die_mark);
5973 if (die->comdat_type_p)
5975 print_spaces (outfile);
5976 fprintf (outfile, " signature: ");
5977 print_signature (outfile, die->die_id.die_type_node->signature);
5978 fprintf (outfile, "\n");
5981 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5983 print_spaces (outfile);
5984 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5986 print_attribute (a, true, outfile);
5987 fprintf (outfile, "\n");
5990 if (die->die_child != NULL)
5992 print_indent += 4;
5993 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5994 print_indent -= 4;
5996 if (print_indent == 0)
5997 fprintf (outfile, "\n");
6000 /* Print the list of operations in the LOC location description. */
6002 DEBUG_FUNCTION void
6003 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6005 print_loc_descr (loc, stderr);
6008 /* Print the information collected for a given DIE. */
6010 DEBUG_FUNCTION void
6011 debug_dwarf_die (dw_die_ref die)
6013 print_die (die, stderr);
6016 DEBUG_FUNCTION void
6017 debug (die_struct &ref)
6019 print_die (&ref, stderr);
6022 DEBUG_FUNCTION void
6023 debug (die_struct *ptr)
6025 if (ptr)
6026 debug (*ptr);
6027 else
6028 fprintf (stderr, "<nil>\n");
6032 /* Print all DWARF information collected for the compilation unit.
6033 This routine is a debugging aid only. */
6035 DEBUG_FUNCTION void
6036 debug_dwarf (void)
6038 print_indent = 0;
6039 print_die (comp_unit_die (), stderr);
6042 /* Verify the DIE tree structure. */
6044 DEBUG_FUNCTION void
6045 verify_die (dw_die_ref die)
6047 gcc_assert (!die->die_mark);
6048 if (die->die_parent == NULL
6049 && die->die_sib == NULL)
6050 return;
6051 /* Verify the die_sib list is cyclic. */
6052 dw_die_ref x = die;
6055 x->die_mark = 1;
6056 x = x->die_sib;
6058 while (x && !x->die_mark);
6059 gcc_assert (x == die);
6060 x = die;
6063 /* Verify all dies have the same parent. */
6064 gcc_assert (x->die_parent == die->die_parent);
6065 if (x->die_child)
6067 /* Verify the child has the proper parent and recurse. */
6068 gcc_assert (x->die_child->die_parent == x);
6069 verify_die (x->die_child);
6071 x->die_mark = 0;
6072 x = x->die_sib;
6074 while (x && x->die_mark);
6077 /* Sanity checks on DIEs. */
6079 static void
6080 check_die (dw_die_ref die)
6082 unsigned ix;
6083 dw_attr_node *a;
6084 bool inline_found = false;
6085 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6086 int n_decl_line = 0, n_decl_file = 0;
6087 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6089 switch (a->dw_attr)
6091 case DW_AT_inline:
6092 if (a->dw_attr_val.v.val_unsigned)
6093 inline_found = true;
6094 break;
6095 case DW_AT_location:
6096 ++n_location;
6097 break;
6098 case DW_AT_low_pc:
6099 ++n_low_pc;
6100 break;
6101 case DW_AT_high_pc:
6102 ++n_high_pc;
6103 break;
6104 case DW_AT_artificial:
6105 ++n_artificial;
6106 break;
6107 case DW_AT_decl_line:
6108 ++n_decl_line;
6109 break;
6110 case DW_AT_decl_file:
6111 ++n_decl_file;
6112 break;
6113 default:
6114 break;
6117 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6118 || n_decl_line > 1 || n_decl_file > 1)
6120 fprintf (stderr, "Duplicate attributes in DIE:\n");
6121 debug_dwarf_die (die);
6122 gcc_unreachable ();
6124 if (inline_found)
6126 /* A debugging information entry that is a member of an abstract
6127 instance tree [that has DW_AT_inline] should not contain any
6128 attributes which describe aspects of the subroutine which vary
6129 between distinct inlined expansions or distinct out-of-line
6130 expansions. */
6131 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6132 gcc_assert (a->dw_attr != DW_AT_low_pc
6133 && a->dw_attr != DW_AT_high_pc
6134 && a->dw_attr != DW_AT_location
6135 && a->dw_attr != DW_AT_frame_base
6136 && a->dw_attr != DW_AT_call_all_calls
6137 && a->dw_attr != DW_AT_GNU_all_call_sites);
6141 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6142 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6143 DIE that marks the start of the DIEs for this include file. */
6145 static dw_die_ref
6146 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6148 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6149 dw_die_ref new_unit = gen_compile_unit_die (filename);
6151 new_unit->die_sib = old_unit;
6152 return new_unit;
6155 /* Close an include-file CU and reopen the enclosing one. */
6157 static dw_die_ref
6158 pop_compile_unit (dw_die_ref old_unit)
6160 dw_die_ref new_unit = old_unit->die_sib;
6162 old_unit->die_sib = NULL;
6163 return new_unit;
6166 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6167 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6168 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6170 /* Calculate the checksum of a location expression. */
6172 static inline void
6173 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6175 int tem;
6176 inchash::hash hstate;
6177 hashval_t hash;
6179 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6180 CHECKSUM (tem);
6181 hash_loc_operands (loc, hstate);
6182 hash = hstate.end();
6183 CHECKSUM (hash);
6186 /* Calculate the checksum of an attribute. */
6188 static void
6189 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6191 dw_loc_descr_ref loc;
6192 rtx r;
6194 CHECKSUM (at->dw_attr);
6196 /* We don't care that this was compiled with a different compiler
6197 snapshot; if the output is the same, that's what matters. */
6198 if (at->dw_attr == DW_AT_producer)
6199 return;
6201 switch (AT_class (at))
6203 case dw_val_class_const:
6204 case dw_val_class_const_implicit:
6205 CHECKSUM (at->dw_attr_val.v.val_int);
6206 break;
6207 case dw_val_class_unsigned_const:
6208 case dw_val_class_unsigned_const_implicit:
6209 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6210 break;
6211 case dw_val_class_const_double:
6212 CHECKSUM (at->dw_attr_val.v.val_double);
6213 break;
6214 case dw_val_class_wide_int:
6215 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6216 get_full_len (*at->dw_attr_val.v.val_wide)
6217 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6218 break;
6219 case dw_val_class_vec:
6220 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6221 (at->dw_attr_val.v.val_vec.length
6222 * at->dw_attr_val.v.val_vec.elt_size));
6223 break;
6224 case dw_val_class_flag:
6225 CHECKSUM (at->dw_attr_val.v.val_flag);
6226 break;
6227 case dw_val_class_str:
6228 CHECKSUM_STRING (AT_string (at));
6229 break;
6231 case dw_val_class_addr:
6232 r = AT_addr (at);
6233 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6234 CHECKSUM_STRING (XSTR (r, 0));
6235 break;
6237 case dw_val_class_offset:
6238 CHECKSUM (at->dw_attr_val.v.val_offset);
6239 break;
6241 case dw_val_class_loc:
6242 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6243 loc_checksum (loc, ctx);
6244 break;
6246 case dw_val_class_die_ref:
6247 die_checksum (AT_ref (at), ctx, mark);
6248 break;
6250 case dw_val_class_fde_ref:
6251 case dw_val_class_vms_delta:
6252 case dw_val_class_lbl_id:
6253 case dw_val_class_lineptr:
6254 case dw_val_class_macptr:
6255 case dw_val_class_loclistsptr:
6256 case dw_val_class_high_pc:
6257 break;
6259 case dw_val_class_file:
6260 case dw_val_class_file_implicit:
6261 CHECKSUM_STRING (AT_file (at)->filename);
6262 break;
6264 case dw_val_class_data8:
6265 CHECKSUM (at->dw_attr_val.v.val_data8);
6266 break;
6268 default:
6269 break;
6273 /* Calculate the checksum of a DIE. */
6275 static void
6276 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6278 dw_die_ref c;
6279 dw_attr_node *a;
6280 unsigned ix;
6282 /* To avoid infinite recursion. */
6283 if (die->die_mark)
6285 CHECKSUM (die->die_mark);
6286 return;
6288 die->die_mark = ++(*mark);
6290 CHECKSUM (die->die_tag);
6292 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6293 attr_checksum (a, ctx, mark);
6295 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6298 #undef CHECKSUM
6299 #undef CHECKSUM_BLOCK
6300 #undef CHECKSUM_STRING
6302 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6303 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6304 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6305 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6306 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6307 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6308 #define CHECKSUM_ATTR(FOO) \
6309 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6311 /* Calculate the checksum of a number in signed LEB128 format. */
6313 static void
6314 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6316 unsigned char byte;
6317 bool more;
6319 while (1)
6321 byte = (value & 0x7f);
6322 value >>= 7;
6323 more = !((value == 0 && (byte & 0x40) == 0)
6324 || (value == -1 && (byte & 0x40) != 0));
6325 if (more)
6326 byte |= 0x80;
6327 CHECKSUM (byte);
6328 if (!more)
6329 break;
6333 /* Calculate the checksum of a number in unsigned LEB128 format. */
6335 static void
6336 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6338 while (1)
6340 unsigned char byte = (value & 0x7f);
6341 value >>= 7;
6342 if (value != 0)
6343 /* More bytes to follow. */
6344 byte |= 0x80;
6345 CHECKSUM (byte);
6346 if (value == 0)
6347 break;
6351 /* Checksum the context of the DIE. This adds the names of any
6352 surrounding namespaces or structures to the checksum. */
6354 static void
6355 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6357 const char *name;
6358 dw_die_ref spec;
6359 int tag = die->die_tag;
6361 if (tag != DW_TAG_namespace
6362 && tag != DW_TAG_structure_type
6363 && tag != DW_TAG_class_type)
6364 return;
6366 name = get_AT_string (die, DW_AT_name);
6368 spec = get_AT_ref (die, DW_AT_specification);
6369 if (spec != NULL)
6370 die = spec;
6372 if (die->die_parent != NULL)
6373 checksum_die_context (die->die_parent, ctx);
6375 CHECKSUM_ULEB128 ('C');
6376 CHECKSUM_ULEB128 (tag);
6377 if (name != NULL)
6378 CHECKSUM_STRING (name);
6381 /* Calculate the checksum of a location expression. */
6383 static inline void
6384 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6386 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6387 were emitted as a DW_FORM_sdata instead of a location expression. */
6388 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6390 CHECKSUM_ULEB128 (DW_FORM_sdata);
6391 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6392 return;
6395 /* Otherwise, just checksum the raw location expression. */
6396 while (loc != NULL)
6398 inchash::hash hstate;
6399 hashval_t hash;
6401 CHECKSUM_ULEB128 (loc->dtprel);
6402 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6403 hash_loc_operands (loc, hstate);
6404 hash = hstate.end ();
6405 CHECKSUM (hash);
6406 loc = loc->dw_loc_next;
6410 /* Calculate the checksum of an attribute. */
6412 static void
6413 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6414 struct md5_ctx *ctx, int *mark)
6416 dw_loc_descr_ref loc;
6417 rtx r;
6419 if (AT_class (at) == dw_val_class_die_ref)
6421 dw_die_ref target_die = AT_ref (at);
6423 /* For pointer and reference types, we checksum only the (qualified)
6424 name of the target type (if there is a name). For friend entries,
6425 we checksum only the (qualified) name of the target type or function.
6426 This allows the checksum to remain the same whether the target type
6427 is complete or not. */
6428 if ((at->dw_attr == DW_AT_type
6429 && (tag == DW_TAG_pointer_type
6430 || tag == DW_TAG_reference_type
6431 || tag == DW_TAG_rvalue_reference_type
6432 || tag == DW_TAG_ptr_to_member_type))
6433 || (at->dw_attr == DW_AT_friend
6434 && tag == DW_TAG_friend))
6436 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6438 if (name_attr != NULL)
6440 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6442 if (decl == NULL)
6443 decl = target_die;
6444 CHECKSUM_ULEB128 ('N');
6445 CHECKSUM_ULEB128 (at->dw_attr);
6446 if (decl->die_parent != NULL)
6447 checksum_die_context (decl->die_parent, ctx);
6448 CHECKSUM_ULEB128 ('E');
6449 CHECKSUM_STRING (AT_string (name_attr));
6450 return;
6454 /* For all other references to another DIE, we check to see if the
6455 target DIE has already been visited. If it has, we emit a
6456 backward reference; if not, we descend recursively. */
6457 if (target_die->die_mark > 0)
6459 CHECKSUM_ULEB128 ('R');
6460 CHECKSUM_ULEB128 (at->dw_attr);
6461 CHECKSUM_ULEB128 (target_die->die_mark);
6463 else
6465 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6467 if (decl == NULL)
6468 decl = target_die;
6469 target_die->die_mark = ++(*mark);
6470 CHECKSUM_ULEB128 ('T');
6471 CHECKSUM_ULEB128 (at->dw_attr);
6472 if (decl->die_parent != NULL)
6473 checksum_die_context (decl->die_parent, ctx);
6474 die_checksum_ordered (target_die, ctx, mark);
6476 return;
6479 CHECKSUM_ULEB128 ('A');
6480 CHECKSUM_ULEB128 (at->dw_attr);
6482 switch (AT_class (at))
6484 case dw_val_class_const:
6485 case dw_val_class_const_implicit:
6486 CHECKSUM_ULEB128 (DW_FORM_sdata);
6487 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6488 break;
6490 case dw_val_class_unsigned_const:
6491 case dw_val_class_unsigned_const_implicit:
6492 CHECKSUM_ULEB128 (DW_FORM_sdata);
6493 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6494 break;
6496 case dw_val_class_const_double:
6497 CHECKSUM_ULEB128 (DW_FORM_block);
6498 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6499 CHECKSUM (at->dw_attr_val.v.val_double);
6500 break;
6502 case dw_val_class_wide_int:
6503 CHECKSUM_ULEB128 (DW_FORM_block);
6504 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6505 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6506 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6507 get_full_len (*at->dw_attr_val.v.val_wide)
6508 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6509 break;
6511 case dw_val_class_vec:
6512 CHECKSUM_ULEB128 (DW_FORM_block);
6513 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6514 * at->dw_attr_val.v.val_vec.elt_size);
6515 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6516 (at->dw_attr_val.v.val_vec.length
6517 * at->dw_attr_val.v.val_vec.elt_size));
6518 break;
6520 case dw_val_class_flag:
6521 CHECKSUM_ULEB128 (DW_FORM_flag);
6522 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6523 break;
6525 case dw_val_class_str:
6526 CHECKSUM_ULEB128 (DW_FORM_string);
6527 CHECKSUM_STRING (AT_string (at));
6528 break;
6530 case dw_val_class_addr:
6531 r = AT_addr (at);
6532 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6533 CHECKSUM_ULEB128 (DW_FORM_string);
6534 CHECKSUM_STRING (XSTR (r, 0));
6535 break;
6537 case dw_val_class_offset:
6538 CHECKSUM_ULEB128 (DW_FORM_sdata);
6539 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6540 break;
6542 case dw_val_class_loc:
6543 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6544 loc_checksum_ordered (loc, ctx);
6545 break;
6547 case dw_val_class_fde_ref:
6548 case dw_val_class_lbl_id:
6549 case dw_val_class_lineptr:
6550 case dw_val_class_macptr:
6551 case dw_val_class_loclistsptr:
6552 case dw_val_class_high_pc:
6553 break;
6555 case dw_val_class_file:
6556 case dw_val_class_file_implicit:
6557 CHECKSUM_ULEB128 (DW_FORM_string);
6558 CHECKSUM_STRING (AT_file (at)->filename);
6559 break;
6561 case dw_val_class_data8:
6562 CHECKSUM (at->dw_attr_val.v.val_data8);
6563 break;
6565 default:
6566 break;
6570 struct checksum_attributes
6572 dw_attr_node *at_name;
6573 dw_attr_node *at_type;
6574 dw_attr_node *at_friend;
6575 dw_attr_node *at_accessibility;
6576 dw_attr_node *at_address_class;
6577 dw_attr_node *at_allocated;
6578 dw_attr_node *at_artificial;
6579 dw_attr_node *at_associated;
6580 dw_attr_node *at_binary_scale;
6581 dw_attr_node *at_bit_offset;
6582 dw_attr_node *at_bit_size;
6583 dw_attr_node *at_bit_stride;
6584 dw_attr_node *at_byte_size;
6585 dw_attr_node *at_byte_stride;
6586 dw_attr_node *at_const_value;
6587 dw_attr_node *at_containing_type;
6588 dw_attr_node *at_count;
6589 dw_attr_node *at_data_location;
6590 dw_attr_node *at_data_member_location;
6591 dw_attr_node *at_decimal_scale;
6592 dw_attr_node *at_decimal_sign;
6593 dw_attr_node *at_default_value;
6594 dw_attr_node *at_digit_count;
6595 dw_attr_node *at_discr;
6596 dw_attr_node *at_discr_list;
6597 dw_attr_node *at_discr_value;
6598 dw_attr_node *at_encoding;
6599 dw_attr_node *at_endianity;
6600 dw_attr_node *at_explicit;
6601 dw_attr_node *at_is_optional;
6602 dw_attr_node *at_location;
6603 dw_attr_node *at_lower_bound;
6604 dw_attr_node *at_mutable;
6605 dw_attr_node *at_ordering;
6606 dw_attr_node *at_picture_string;
6607 dw_attr_node *at_prototyped;
6608 dw_attr_node *at_small;
6609 dw_attr_node *at_segment;
6610 dw_attr_node *at_string_length;
6611 dw_attr_node *at_string_length_bit_size;
6612 dw_attr_node *at_string_length_byte_size;
6613 dw_attr_node *at_threads_scaled;
6614 dw_attr_node *at_upper_bound;
6615 dw_attr_node *at_use_location;
6616 dw_attr_node *at_use_UTF8;
6617 dw_attr_node *at_variable_parameter;
6618 dw_attr_node *at_virtuality;
6619 dw_attr_node *at_visibility;
6620 dw_attr_node *at_vtable_elem_location;
6623 /* Collect the attributes that we will want to use for the checksum. */
6625 static void
6626 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6628 dw_attr_node *a;
6629 unsigned ix;
6631 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6633 switch (a->dw_attr)
6635 case DW_AT_name:
6636 attrs->at_name = a;
6637 break;
6638 case DW_AT_type:
6639 attrs->at_type = a;
6640 break;
6641 case DW_AT_friend:
6642 attrs->at_friend = a;
6643 break;
6644 case DW_AT_accessibility:
6645 attrs->at_accessibility = a;
6646 break;
6647 case DW_AT_address_class:
6648 attrs->at_address_class = a;
6649 break;
6650 case DW_AT_allocated:
6651 attrs->at_allocated = a;
6652 break;
6653 case DW_AT_artificial:
6654 attrs->at_artificial = a;
6655 break;
6656 case DW_AT_associated:
6657 attrs->at_associated = a;
6658 break;
6659 case DW_AT_binary_scale:
6660 attrs->at_binary_scale = a;
6661 break;
6662 case DW_AT_bit_offset:
6663 attrs->at_bit_offset = a;
6664 break;
6665 case DW_AT_bit_size:
6666 attrs->at_bit_size = a;
6667 break;
6668 case DW_AT_bit_stride:
6669 attrs->at_bit_stride = a;
6670 break;
6671 case DW_AT_byte_size:
6672 attrs->at_byte_size = a;
6673 break;
6674 case DW_AT_byte_stride:
6675 attrs->at_byte_stride = a;
6676 break;
6677 case DW_AT_const_value:
6678 attrs->at_const_value = a;
6679 break;
6680 case DW_AT_containing_type:
6681 attrs->at_containing_type = a;
6682 break;
6683 case DW_AT_count:
6684 attrs->at_count = a;
6685 break;
6686 case DW_AT_data_location:
6687 attrs->at_data_location = a;
6688 break;
6689 case DW_AT_data_member_location:
6690 attrs->at_data_member_location = a;
6691 break;
6692 case DW_AT_decimal_scale:
6693 attrs->at_decimal_scale = a;
6694 break;
6695 case DW_AT_decimal_sign:
6696 attrs->at_decimal_sign = a;
6697 break;
6698 case DW_AT_default_value:
6699 attrs->at_default_value = a;
6700 break;
6701 case DW_AT_digit_count:
6702 attrs->at_digit_count = a;
6703 break;
6704 case DW_AT_discr:
6705 attrs->at_discr = a;
6706 break;
6707 case DW_AT_discr_list:
6708 attrs->at_discr_list = a;
6709 break;
6710 case DW_AT_discr_value:
6711 attrs->at_discr_value = a;
6712 break;
6713 case DW_AT_encoding:
6714 attrs->at_encoding = a;
6715 break;
6716 case DW_AT_endianity:
6717 attrs->at_endianity = a;
6718 break;
6719 case DW_AT_explicit:
6720 attrs->at_explicit = a;
6721 break;
6722 case DW_AT_is_optional:
6723 attrs->at_is_optional = a;
6724 break;
6725 case DW_AT_location:
6726 attrs->at_location = a;
6727 break;
6728 case DW_AT_lower_bound:
6729 attrs->at_lower_bound = a;
6730 break;
6731 case DW_AT_mutable:
6732 attrs->at_mutable = a;
6733 break;
6734 case DW_AT_ordering:
6735 attrs->at_ordering = a;
6736 break;
6737 case DW_AT_picture_string:
6738 attrs->at_picture_string = a;
6739 break;
6740 case DW_AT_prototyped:
6741 attrs->at_prototyped = a;
6742 break;
6743 case DW_AT_small:
6744 attrs->at_small = a;
6745 break;
6746 case DW_AT_segment:
6747 attrs->at_segment = a;
6748 break;
6749 case DW_AT_string_length:
6750 attrs->at_string_length = a;
6751 break;
6752 case DW_AT_string_length_bit_size:
6753 attrs->at_string_length_bit_size = a;
6754 break;
6755 case DW_AT_string_length_byte_size:
6756 attrs->at_string_length_byte_size = a;
6757 break;
6758 case DW_AT_threads_scaled:
6759 attrs->at_threads_scaled = a;
6760 break;
6761 case DW_AT_upper_bound:
6762 attrs->at_upper_bound = a;
6763 break;
6764 case DW_AT_use_location:
6765 attrs->at_use_location = a;
6766 break;
6767 case DW_AT_use_UTF8:
6768 attrs->at_use_UTF8 = a;
6769 break;
6770 case DW_AT_variable_parameter:
6771 attrs->at_variable_parameter = a;
6772 break;
6773 case DW_AT_virtuality:
6774 attrs->at_virtuality = a;
6775 break;
6776 case DW_AT_visibility:
6777 attrs->at_visibility = a;
6778 break;
6779 case DW_AT_vtable_elem_location:
6780 attrs->at_vtable_elem_location = a;
6781 break;
6782 default:
6783 break;
6788 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6790 static void
6791 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6793 dw_die_ref c;
6794 dw_die_ref decl;
6795 struct checksum_attributes attrs;
6797 CHECKSUM_ULEB128 ('D');
6798 CHECKSUM_ULEB128 (die->die_tag);
6800 memset (&attrs, 0, sizeof (attrs));
6802 decl = get_AT_ref (die, DW_AT_specification);
6803 if (decl != NULL)
6804 collect_checksum_attributes (&attrs, decl);
6805 collect_checksum_attributes (&attrs, die);
6807 CHECKSUM_ATTR (attrs.at_name);
6808 CHECKSUM_ATTR (attrs.at_accessibility);
6809 CHECKSUM_ATTR (attrs.at_address_class);
6810 CHECKSUM_ATTR (attrs.at_allocated);
6811 CHECKSUM_ATTR (attrs.at_artificial);
6812 CHECKSUM_ATTR (attrs.at_associated);
6813 CHECKSUM_ATTR (attrs.at_binary_scale);
6814 CHECKSUM_ATTR (attrs.at_bit_offset);
6815 CHECKSUM_ATTR (attrs.at_bit_size);
6816 CHECKSUM_ATTR (attrs.at_bit_stride);
6817 CHECKSUM_ATTR (attrs.at_byte_size);
6818 CHECKSUM_ATTR (attrs.at_byte_stride);
6819 CHECKSUM_ATTR (attrs.at_const_value);
6820 CHECKSUM_ATTR (attrs.at_containing_type);
6821 CHECKSUM_ATTR (attrs.at_count);
6822 CHECKSUM_ATTR (attrs.at_data_location);
6823 CHECKSUM_ATTR (attrs.at_data_member_location);
6824 CHECKSUM_ATTR (attrs.at_decimal_scale);
6825 CHECKSUM_ATTR (attrs.at_decimal_sign);
6826 CHECKSUM_ATTR (attrs.at_default_value);
6827 CHECKSUM_ATTR (attrs.at_digit_count);
6828 CHECKSUM_ATTR (attrs.at_discr);
6829 CHECKSUM_ATTR (attrs.at_discr_list);
6830 CHECKSUM_ATTR (attrs.at_discr_value);
6831 CHECKSUM_ATTR (attrs.at_encoding);
6832 CHECKSUM_ATTR (attrs.at_endianity);
6833 CHECKSUM_ATTR (attrs.at_explicit);
6834 CHECKSUM_ATTR (attrs.at_is_optional);
6835 CHECKSUM_ATTR (attrs.at_location);
6836 CHECKSUM_ATTR (attrs.at_lower_bound);
6837 CHECKSUM_ATTR (attrs.at_mutable);
6838 CHECKSUM_ATTR (attrs.at_ordering);
6839 CHECKSUM_ATTR (attrs.at_picture_string);
6840 CHECKSUM_ATTR (attrs.at_prototyped);
6841 CHECKSUM_ATTR (attrs.at_small);
6842 CHECKSUM_ATTR (attrs.at_segment);
6843 CHECKSUM_ATTR (attrs.at_string_length);
6844 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6845 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6846 CHECKSUM_ATTR (attrs.at_threads_scaled);
6847 CHECKSUM_ATTR (attrs.at_upper_bound);
6848 CHECKSUM_ATTR (attrs.at_use_location);
6849 CHECKSUM_ATTR (attrs.at_use_UTF8);
6850 CHECKSUM_ATTR (attrs.at_variable_parameter);
6851 CHECKSUM_ATTR (attrs.at_virtuality);
6852 CHECKSUM_ATTR (attrs.at_visibility);
6853 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6854 CHECKSUM_ATTR (attrs.at_type);
6855 CHECKSUM_ATTR (attrs.at_friend);
6857 /* Checksum the child DIEs. */
6858 c = die->die_child;
6859 if (c) do {
6860 dw_attr_node *name_attr;
6862 c = c->die_sib;
6863 name_attr = get_AT (c, DW_AT_name);
6864 if (is_template_instantiation (c))
6866 /* Ignore instantiations of member type and function templates. */
6868 else if (name_attr != NULL
6869 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6871 /* Use a shallow checksum for named nested types and member
6872 functions. */
6873 CHECKSUM_ULEB128 ('S');
6874 CHECKSUM_ULEB128 (c->die_tag);
6875 CHECKSUM_STRING (AT_string (name_attr));
6877 else
6879 /* Use a deep checksum for other children. */
6880 /* Mark this DIE so it gets processed when unmarking. */
6881 if (c->die_mark == 0)
6882 c->die_mark = -1;
6883 die_checksum_ordered (c, ctx, mark);
6885 } while (c != die->die_child);
6887 CHECKSUM_ULEB128 (0);
6890 /* Add a type name and tag to a hash. */
6891 static void
6892 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6894 CHECKSUM_ULEB128 (tag);
6895 CHECKSUM_STRING (name);
6898 #undef CHECKSUM
6899 #undef CHECKSUM_STRING
6900 #undef CHECKSUM_ATTR
6901 #undef CHECKSUM_LEB128
6902 #undef CHECKSUM_ULEB128
6904 /* Generate the type signature for DIE. This is computed by generating an
6905 MD5 checksum over the DIE's tag, its relevant attributes, and its
6906 children. Attributes that are references to other DIEs are processed
6907 by recursion, using the MARK field to prevent infinite recursion.
6908 If the DIE is nested inside a namespace or another type, we also
6909 need to include that context in the signature. The lower 64 bits
6910 of the resulting MD5 checksum comprise the signature. */
6912 static void
6913 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6915 int mark;
6916 const char *name;
6917 unsigned char checksum[16];
6918 struct md5_ctx ctx;
6919 dw_die_ref decl;
6920 dw_die_ref parent;
6922 name = get_AT_string (die, DW_AT_name);
6923 decl = get_AT_ref (die, DW_AT_specification);
6924 parent = get_die_parent (die);
6926 /* First, compute a signature for just the type name (and its surrounding
6927 context, if any. This is stored in the type unit DIE for link-time
6928 ODR (one-definition rule) checking. */
6930 if (is_cxx () && name != NULL)
6932 md5_init_ctx (&ctx);
6934 /* Checksum the names of surrounding namespaces and structures. */
6935 if (parent != NULL)
6936 checksum_die_context (parent, &ctx);
6938 /* Checksum the current DIE. */
6939 die_odr_checksum (die->die_tag, name, &ctx);
6940 md5_finish_ctx (&ctx, checksum);
6942 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6945 /* Next, compute the complete type signature. */
6947 md5_init_ctx (&ctx);
6948 mark = 1;
6949 die->die_mark = mark;
6951 /* Checksum the names of surrounding namespaces and structures. */
6952 if (parent != NULL)
6953 checksum_die_context (parent, &ctx);
6955 /* Checksum the DIE and its children. */
6956 die_checksum_ordered (die, &ctx, &mark);
6957 unmark_all_dies (die);
6958 md5_finish_ctx (&ctx, checksum);
6960 /* Store the signature in the type node and link the type DIE and the
6961 type node together. */
6962 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6963 DWARF_TYPE_SIGNATURE_SIZE);
6964 die->comdat_type_p = true;
6965 die->die_id.die_type_node = type_node;
6966 type_node->type_die = die;
6968 /* If the DIE is a specification, link its declaration to the type node
6969 as well. */
6970 if (decl != NULL)
6972 decl->comdat_type_p = true;
6973 decl->die_id.die_type_node = type_node;
6977 /* Do the location expressions look same? */
6978 static inline int
6979 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6981 return loc1->dw_loc_opc == loc2->dw_loc_opc
6982 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6983 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6986 /* Do the values look the same? */
6987 static int
6988 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6990 dw_loc_descr_ref loc1, loc2;
6991 rtx r1, r2;
6993 if (v1->val_class != v2->val_class)
6994 return 0;
6996 switch (v1->val_class)
6998 case dw_val_class_const:
6999 case dw_val_class_const_implicit:
7000 return v1->v.val_int == v2->v.val_int;
7001 case dw_val_class_unsigned_const:
7002 case dw_val_class_unsigned_const_implicit:
7003 return v1->v.val_unsigned == v2->v.val_unsigned;
7004 case dw_val_class_const_double:
7005 return v1->v.val_double.high == v2->v.val_double.high
7006 && v1->v.val_double.low == v2->v.val_double.low;
7007 case dw_val_class_wide_int:
7008 return *v1->v.val_wide == *v2->v.val_wide;
7009 case dw_val_class_vec:
7010 if (v1->v.val_vec.length != v2->v.val_vec.length
7011 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7012 return 0;
7013 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7014 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7015 return 0;
7016 return 1;
7017 case dw_val_class_flag:
7018 return v1->v.val_flag == v2->v.val_flag;
7019 case dw_val_class_str:
7020 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7022 case dw_val_class_addr:
7023 r1 = v1->v.val_addr;
7024 r2 = v2->v.val_addr;
7025 if (GET_CODE (r1) != GET_CODE (r2))
7026 return 0;
7027 return !rtx_equal_p (r1, r2);
7029 case dw_val_class_offset:
7030 return v1->v.val_offset == v2->v.val_offset;
7032 case dw_val_class_loc:
7033 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7034 loc1 && loc2;
7035 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7036 if (!same_loc_p (loc1, loc2, mark))
7037 return 0;
7038 return !loc1 && !loc2;
7040 case dw_val_class_die_ref:
7041 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7043 case dw_val_class_fde_ref:
7044 case dw_val_class_vms_delta:
7045 case dw_val_class_lbl_id:
7046 case dw_val_class_lineptr:
7047 case dw_val_class_macptr:
7048 case dw_val_class_loclistsptr:
7049 case dw_val_class_high_pc:
7050 return 1;
7052 case dw_val_class_file:
7053 case dw_val_class_file_implicit:
7054 return v1->v.val_file == v2->v.val_file;
7056 case dw_val_class_data8:
7057 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7059 default:
7060 return 1;
7064 /* Do the attributes look the same? */
7066 static int
7067 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7069 if (at1->dw_attr != at2->dw_attr)
7070 return 0;
7072 /* We don't care that this was compiled with a different compiler
7073 snapshot; if the output is the same, that's what matters. */
7074 if (at1->dw_attr == DW_AT_producer)
7075 return 1;
7077 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7080 /* Do the dies look the same? */
7082 static int
7083 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7085 dw_die_ref c1, c2;
7086 dw_attr_node *a1;
7087 unsigned ix;
7089 /* To avoid infinite recursion. */
7090 if (die1->die_mark)
7091 return die1->die_mark == die2->die_mark;
7092 die1->die_mark = die2->die_mark = ++(*mark);
7094 if (die1->die_tag != die2->die_tag)
7095 return 0;
7097 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7098 return 0;
7100 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7101 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7102 return 0;
7104 c1 = die1->die_child;
7105 c2 = die2->die_child;
7106 if (! c1)
7108 if (c2)
7109 return 0;
7111 else
7112 for (;;)
7114 if (!same_die_p (c1, c2, mark))
7115 return 0;
7116 c1 = c1->die_sib;
7117 c2 = c2->die_sib;
7118 if (c1 == die1->die_child)
7120 if (c2 == die2->die_child)
7121 break;
7122 else
7123 return 0;
7127 return 1;
7130 /* Do the dies look the same? Wrapper around same_die_p. */
7132 static int
7133 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7135 int mark = 0;
7136 int ret = same_die_p (die1, die2, &mark);
7138 unmark_all_dies (die1);
7139 unmark_all_dies (die2);
7141 return ret;
7144 /* The prefix to attach to symbols on DIEs in the current comdat debug
7145 info section. */
7146 static const char *comdat_symbol_id;
7148 /* The index of the current symbol within the current comdat CU. */
7149 static unsigned int comdat_symbol_number;
7151 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7152 children, and set comdat_symbol_id accordingly. */
7154 static void
7155 compute_section_prefix (dw_die_ref unit_die)
7157 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7158 const char *base = die_name ? lbasename (die_name) : "anonymous";
7159 char *name = XALLOCAVEC (char, strlen (base) + 64);
7160 char *p;
7161 int i, mark;
7162 unsigned char checksum[16];
7163 struct md5_ctx ctx;
7165 /* Compute the checksum of the DIE, then append part of it as hex digits to
7166 the name filename of the unit. */
7168 md5_init_ctx (&ctx);
7169 mark = 0;
7170 die_checksum (unit_die, &ctx, &mark);
7171 unmark_all_dies (unit_die);
7172 md5_finish_ctx (&ctx, checksum);
7174 sprintf (name, "%s.", base);
7175 clean_symbol_name (name);
7177 p = name + strlen (name);
7178 for (i = 0; i < 4; i++)
7180 sprintf (p, "%.2x", checksum[i]);
7181 p += 2;
7184 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7185 comdat_symbol_number = 0;
7188 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7190 static int
7191 is_type_die (dw_die_ref die)
7193 switch (die->die_tag)
7195 case DW_TAG_array_type:
7196 case DW_TAG_class_type:
7197 case DW_TAG_interface_type:
7198 case DW_TAG_enumeration_type:
7199 case DW_TAG_pointer_type:
7200 case DW_TAG_reference_type:
7201 case DW_TAG_rvalue_reference_type:
7202 case DW_TAG_string_type:
7203 case DW_TAG_structure_type:
7204 case DW_TAG_subroutine_type:
7205 case DW_TAG_union_type:
7206 case DW_TAG_ptr_to_member_type:
7207 case DW_TAG_set_type:
7208 case DW_TAG_subrange_type:
7209 case DW_TAG_base_type:
7210 case DW_TAG_const_type:
7211 case DW_TAG_file_type:
7212 case DW_TAG_packed_type:
7213 case DW_TAG_volatile_type:
7214 case DW_TAG_typedef:
7215 return 1;
7216 default:
7217 return 0;
7221 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7222 Basically, we want to choose the bits that are likely to be shared between
7223 compilations (types) and leave out the bits that are specific to individual
7224 compilations (functions). */
7226 static int
7227 is_comdat_die (dw_die_ref c)
7229 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7230 we do for stabs. The advantage is a greater likelihood of sharing between
7231 objects that don't include headers in the same order (and therefore would
7232 put the base types in a different comdat). jason 8/28/00 */
7234 if (c->die_tag == DW_TAG_base_type)
7235 return 0;
7237 if (c->die_tag == DW_TAG_pointer_type
7238 || c->die_tag == DW_TAG_reference_type
7239 || c->die_tag == DW_TAG_rvalue_reference_type
7240 || c->die_tag == DW_TAG_const_type
7241 || c->die_tag == DW_TAG_volatile_type)
7243 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7245 return t ? is_comdat_die (t) : 0;
7248 return is_type_die (c);
7251 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7252 compilation unit. */
7254 static int
7255 is_symbol_die (dw_die_ref c)
7257 return (is_type_die (c)
7258 || is_declaration_die (c)
7259 || c->die_tag == DW_TAG_namespace
7260 || c->die_tag == DW_TAG_module);
7263 /* Returns true iff C is a compile-unit DIE. */
7265 static inline bool
7266 is_cu_die (dw_die_ref c)
7268 return c && (c->die_tag == DW_TAG_compile_unit
7269 || c->die_tag == DW_TAG_skeleton_unit);
7272 /* Returns true iff C is a unit DIE of some sort. */
7274 static inline bool
7275 is_unit_die (dw_die_ref c)
7277 return c && (c->die_tag == DW_TAG_compile_unit
7278 || c->die_tag == DW_TAG_partial_unit
7279 || c->die_tag == DW_TAG_type_unit
7280 || c->die_tag == DW_TAG_skeleton_unit);
7283 /* Returns true iff C is a namespace DIE. */
7285 static inline bool
7286 is_namespace_die (dw_die_ref c)
7288 return c && c->die_tag == DW_TAG_namespace;
7291 /* Returns true iff C is a class or structure DIE. */
7293 static inline bool
7294 is_class_die (dw_die_ref c)
7296 return c && (c->die_tag == DW_TAG_class_type
7297 || c->die_tag == DW_TAG_structure_type);
7300 /* Return non-zero if this DIE is a template parameter. */
7302 static inline bool
7303 is_template_parameter (dw_die_ref die)
7305 switch (die->die_tag)
7307 case DW_TAG_template_type_param:
7308 case DW_TAG_template_value_param:
7309 case DW_TAG_GNU_template_template_param:
7310 case DW_TAG_GNU_template_parameter_pack:
7311 return true;
7312 default:
7313 return false;
7317 /* Return non-zero if this DIE represents a template instantiation. */
7319 static inline bool
7320 is_template_instantiation (dw_die_ref die)
7322 dw_die_ref c;
7324 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7325 return false;
7326 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7327 return false;
7330 static char *
7331 gen_internal_sym (const char *prefix)
7333 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7335 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7336 return xstrdup (buf);
7339 /* Assign symbols to all worthy DIEs under DIE. */
7341 static void
7342 assign_symbol_names (dw_die_ref die)
7344 dw_die_ref c;
7346 if (is_symbol_die (die) && !die->comdat_type_p)
7348 if (comdat_symbol_id)
7350 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7352 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7353 comdat_symbol_id, comdat_symbol_number++);
7354 die->die_id.die_symbol = xstrdup (p);
7356 else
7357 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7360 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7363 struct cu_hash_table_entry
7365 dw_die_ref cu;
7366 unsigned min_comdat_num, max_comdat_num;
7367 struct cu_hash_table_entry *next;
7370 /* Helpers to manipulate hash table of CUs. */
7372 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7374 typedef die_struct *compare_type;
7375 static inline hashval_t hash (const cu_hash_table_entry *);
7376 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7377 static inline void remove (cu_hash_table_entry *);
7380 inline hashval_t
7381 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7383 return htab_hash_string (entry->cu->die_id.die_symbol);
7386 inline bool
7387 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7388 const die_struct *entry2)
7390 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7393 inline void
7394 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7396 struct cu_hash_table_entry *next;
7398 while (entry)
7400 next = entry->next;
7401 free (entry);
7402 entry = next;
7406 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7408 /* Check whether we have already seen this CU and set up SYM_NUM
7409 accordingly. */
7410 static int
7411 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7413 struct cu_hash_table_entry dummy;
7414 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7416 dummy.max_comdat_num = 0;
7418 slot = htable->find_slot_with_hash (cu,
7419 htab_hash_string (cu->die_id.die_symbol),
7420 INSERT);
7421 entry = *slot;
7423 for (; entry; last = entry, entry = entry->next)
7425 if (same_die_p_wrap (cu, entry->cu))
7426 break;
7429 if (entry)
7431 *sym_num = entry->min_comdat_num;
7432 return 1;
7435 entry = XCNEW (struct cu_hash_table_entry);
7436 entry->cu = cu;
7437 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7438 entry->next = *slot;
7439 *slot = entry;
7441 return 0;
7444 /* Record SYM_NUM to record of CU in HTABLE. */
7445 static void
7446 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7447 unsigned int sym_num)
7449 struct cu_hash_table_entry **slot, *entry;
7451 slot = htable->find_slot_with_hash (cu,
7452 htab_hash_string (cu->die_id.die_symbol),
7453 NO_INSERT);
7454 entry = *slot;
7456 entry->max_comdat_num = sym_num;
7459 /* Traverse the DIE (which is always comp_unit_die), and set up
7460 additional compilation units for each of the include files we see
7461 bracketed by BINCL/EINCL. */
7463 static void
7464 break_out_includes (dw_die_ref die)
7466 dw_die_ref c;
7467 dw_die_ref unit = NULL;
7468 limbo_die_node *node, **pnode;
7470 c = die->die_child;
7471 if (c) do {
7472 dw_die_ref prev = c;
7473 c = c->die_sib;
7474 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7475 || (unit && is_comdat_die (c)))
7477 dw_die_ref next = c->die_sib;
7479 /* This DIE is for a secondary CU; remove it from the main one. */
7480 remove_child_with_prev (c, prev);
7482 if (c->die_tag == DW_TAG_GNU_BINCL)
7483 unit = push_new_compile_unit (unit, c);
7484 else if (c->die_tag == DW_TAG_GNU_EINCL)
7485 unit = pop_compile_unit (unit);
7486 else
7487 add_child_die (unit, c);
7488 c = next;
7489 if (c == die->die_child)
7490 break;
7492 } while (c != die->die_child);
7494 #if 0
7495 /* We can only use this in debugging, since the frontend doesn't check
7496 to make sure that we leave every include file we enter. */
7497 gcc_assert (!unit);
7498 #endif
7500 assign_symbol_names (die);
7501 cu_hash_type cu_hash_table (10);
7502 for (node = limbo_die_list, pnode = &limbo_die_list;
7503 node;
7504 node = node->next)
7506 int is_dupl;
7508 compute_section_prefix (node->die);
7509 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7510 &comdat_symbol_number);
7511 assign_symbol_names (node->die);
7512 if (is_dupl)
7513 *pnode = node->next;
7514 else
7516 pnode = &node->next;
7517 record_comdat_symbol_number (node->die, &cu_hash_table,
7518 comdat_symbol_number);
7523 /* Return non-zero if this DIE is a declaration. */
7525 static int
7526 is_declaration_die (dw_die_ref die)
7528 dw_attr_node *a;
7529 unsigned ix;
7531 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7532 if (a->dw_attr == DW_AT_declaration)
7533 return 1;
7535 return 0;
7538 /* Return non-zero if this DIE is nested inside a subprogram. */
7540 static int
7541 is_nested_in_subprogram (dw_die_ref die)
7543 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7545 if (decl == NULL)
7546 decl = die;
7547 return local_scope_p (decl);
7550 /* Return non-zero if this DIE contains a defining declaration of a
7551 subprogram. */
7553 static int
7554 contains_subprogram_definition (dw_die_ref die)
7556 dw_die_ref c;
7558 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7559 return 1;
7560 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7561 return 0;
7564 /* Return non-zero if this is a type DIE that should be moved to a
7565 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7566 unit type. */
7568 static int
7569 should_move_die_to_comdat (dw_die_ref die)
7571 switch (die->die_tag)
7573 case DW_TAG_class_type:
7574 case DW_TAG_structure_type:
7575 case DW_TAG_enumeration_type:
7576 case DW_TAG_union_type:
7577 /* Don't move declarations, inlined instances, types nested in a
7578 subprogram, or types that contain subprogram definitions. */
7579 if (is_declaration_die (die)
7580 || get_AT (die, DW_AT_abstract_origin)
7581 || is_nested_in_subprogram (die)
7582 || contains_subprogram_definition (die))
7583 return 0;
7584 return 1;
7585 case DW_TAG_array_type:
7586 case DW_TAG_interface_type:
7587 case DW_TAG_pointer_type:
7588 case DW_TAG_reference_type:
7589 case DW_TAG_rvalue_reference_type:
7590 case DW_TAG_string_type:
7591 case DW_TAG_subroutine_type:
7592 case DW_TAG_ptr_to_member_type:
7593 case DW_TAG_set_type:
7594 case DW_TAG_subrange_type:
7595 case DW_TAG_base_type:
7596 case DW_TAG_const_type:
7597 case DW_TAG_file_type:
7598 case DW_TAG_packed_type:
7599 case DW_TAG_volatile_type:
7600 case DW_TAG_typedef:
7601 default:
7602 return 0;
7606 /* Make a clone of DIE. */
7608 static dw_die_ref
7609 clone_die (dw_die_ref die)
7611 dw_die_ref clone;
7612 dw_attr_node *a;
7613 unsigned ix;
7615 clone = ggc_cleared_alloc<die_node> ();
7616 clone->die_tag = die->die_tag;
7618 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7619 add_dwarf_attr (clone, a);
7621 return clone;
7624 /* Make a clone of the tree rooted at DIE. */
7626 static dw_die_ref
7627 clone_tree (dw_die_ref die)
7629 dw_die_ref c;
7630 dw_die_ref clone = clone_die (die);
7632 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7634 return clone;
7637 /* Make a clone of DIE as a declaration. */
7639 static dw_die_ref
7640 clone_as_declaration (dw_die_ref die)
7642 dw_die_ref clone;
7643 dw_die_ref decl;
7644 dw_attr_node *a;
7645 unsigned ix;
7647 /* If the DIE is already a declaration, just clone it. */
7648 if (is_declaration_die (die))
7649 return clone_die (die);
7651 /* If the DIE is a specification, just clone its declaration DIE. */
7652 decl = get_AT_ref (die, DW_AT_specification);
7653 if (decl != NULL)
7655 clone = clone_die (decl);
7656 if (die->comdat_type_p)
7657 add_AT_die_ref (clone, DW_AT_signature, die);
7658 return clone;
7661 clone = ggc_cleared_alloc<die_node> ();
7662 clone->die_tag = die->die_tag;
7664 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7666 /* We don't want to copy over all attributes.
7667 For example we don't want DW_AT_byte_size because otherwise we will no
7668 longer have a declaration and GDB will treat it as a definition. */
7670 switch (a->dw_attr)
7672 case DW_AT_abstract_origin:
7673 case DW_AT_artificial:
7674 case DW_AT_containing_type:
7675 case DW_AT_external:
7676 case DW_AT_name:
7677 case DW_AT_type:
7678 case DW_AT_virtuality:
7679 case DW_AT_linkage_name:
7680 case DW_AT_MIPS_linkage_name:
7681 add_dwarf_attr (clone, a);
7682 break;
7683 case DW_AT_byte_size:
7684 default:
7685 break;
7689 if (die->comdat_type_p)
7690 add_AT_die_ref (clone, DW_AT_signature, die);
7692 add_AT_flag (clone, DW_AT_declaration, 1);
7693 return clone;
7697 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7699 struct decl_table_entry
7701 dw_die_ref orig;
7702 dw_die_ref copy;
7705 /* Helpers to manipulate hash table of copied declarations. */
7707 /* Hashtable helpers. */
7709 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7711 typedef die_struct *compare_type;
7712 static inline hashval_t hash (const decl_table_entry *);
7713 static inline bool equal (const decl_table_entry *, const die_struct *);
7716 inline hashval_t
7717 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7719 return htab_hash_pointer (entry->orig);
7722 inline bool
7723 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7724 const die_struct *entry2)
7726 return entry1->orig == entry2;
7729 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7731 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7732 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7733 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7734 to check if the ancestor has already been copied into UNIT. */
7736 static dw_die_ref
7737 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7738 decl_hash_type *decl_table)
7740 dw_die_ref parent = die->die_parent;
7741 dw_die_ref new_parent = unit;
7742 dw_die_ref copy;
7743 decl_table_entry **slot = NULL;
7744 struct decl_table_entry *entry = NULL;
7746 if (decl_table)
7748 /* Check if the entry has already been copied to UNIT. */
7749 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7750 INSERT);
7751 if (*slot != HTAB_EMPTY_ENTRY)
7753 entry = *slot;
7754 return entry->copy;
7757 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7758 entry = XCNEW (struct decl_table_entry);
7759 entry->orig = die;
7760 entry->copy = NULL;
7761 *slot = entry;
7764 if (parent != NULL)
7766 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7767 if (spec != NULL)
7768 parent = spec;
7769 if (!is_unit_die (parent))
7770 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7773 copy = clone_as_declaration (die);
7774 add_child_die (new_parent, copy);
7776 if (decl_table)
7778 /* Record the pointer to the copy. */
7779 entry->copy = copy;
7782 return copy;
7784 /* Copy the declaration context to the new type unit DIE. This includes
7785 any surrounding namespace or type declarations. If the DIE has an
7786 AT_specification attribute, it also includes attributes and children
7787 attached to the specification, and returns a pointer to the original
7788 parent of the declaration DIE. Returns NULL otherwise. */
7790 static dw_die_ref
7791 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7793 dw_die_ref decl;
7794 dw_die_ref new_decl;
7795 dw_die_ref orig_parent = NULL;
7797 decl = get_AT_ref (die, DW_AT_specification);
7798 if (decl == NULL)
7799 decl = die;
7800 else
7802 unsigned ix;
7803 dw_die_ref c;
7804 dw_attr_node *a;
7806 /* The original DIE will be changed to a declaration, and must
7807 be moved to be a child of the original declaration DIE. */
7808 orig_parent = decl->die_parent;
7810 /* Copy the type node pointer from the new DIE to the original
7811 declaration DIE so we can forward references later. */
7812 decl->comdat_type_p = true;
7813 decl->die_id.die_type_node = die->die_id.die_type_node;
7815 remove_AT (die, DW_AT_specification);
7817 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7819 if (a->dw_attr != DW_AT_name
7820 && a->dw_attr != DW_AT_declaration
7821 && a->dw_attr != DW_AT_external)
7822 add_dwarf_attr (die, a);
7825 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7828 if (decl->die_parent != NULL
7829 && !is_unit_die (decl->die_parent))
7831 new_decl = copy_ancestor_tree (unit, decl, NULL);
7832 if (new_decl != NULL)
7834 remove_AT (new_decl, DW_AT_signature);
7835 add_AT_specification (die, new_decl);
7839 return orig_parent;
7842 /* Generate the skeleton ancestor tree for the given NODE, then clone
7843 the DIE and add the clone into the tree. */
7845 static void
7846 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7848 if (node->new_die != NULL)
7849 return;
7851 node->new_die = clone_as_declaration (node->old_die);
7853 if (node->parent != NULL)
7855 generate_skeleton_ancestor_tree (node->parent);
7856 add_child_die (node->parent->new_die, node->new_die);
7860 /* Generate a skeleton tree of DIEs containing any declarations that are
7861 found in the original tree. We traverse the tree looking for declaration
7862 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7864 static void
7865 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7867 skeleton_chain_node node;
7868 dw_die_ref c;
7869 dw_die_ref first;
7870 dw_die_ref prev = NULL;
7871 dw_die_ref next = NULL;
7873 node.parent = parent;
7875 first = c = parent->old_die->die_child;
7876 if (c)
7877 next = c->die_sib;
7878 if (c) do {
7879 if (prev == NULL || prev->die_sib == c)
7880 prev = c;
7881 c = next;
7882 next = (c == first ? NULL : c->die_sib);
7883 node.old_die = c;
7884 node.new_die = NULL;
7885 if (is_declaration_die (c))
7887 if (is_template_instantiation (c))
7889 /* Instantiated templates do not need to be cloned into the
7890 type unit. Just move the DIE and its children back to
7891 the skeleton tree (in the main CU). */
7892 remove_child_with_prev (c, prev);
7893 add_child_die (parent->new_die, c);
7894 c = prev;
7896 else
7898 /* Clone the existing DIE, move the original to the skeleton
7899 tree (which is in the main CU), and put the clone, with
7900 all the original's children, where the original came from
7901 (which is about to be moved to the type unit). */
7902 dw_die_ref clone = clone_die (c);
7903 move_all_children (c, clone);
7905 /* If the original has a DW_AT_object_pointer attribute,
7906 it would now point to a child DIE just moved to the
7907 cloned tree, so we need to remove that attribute from
7908 the original. */
7909 remove_AT (c, DW_AT_object_pointer);
7911 replace_child (c, clone, prev);
7912 generate_skeleton_ancestor_tree (parent);
7913 add_child_die (parent->new_die, c);
7914 node.new_die = c;
7915 c = clone;
7918 generate_skeleton_bottom_up (&node);
7919 } while (next != NULL);
7922 /* Wrapper function for generate_skeleton_bottom_up. */
7924 static dw_die_ref
7925 generate_skeleton (dw_die_ref die)
7927 skeleton_chain_node node;
7929 node.old_die = die;
7930 node.new_die = NULL;
7931 node.parent = NULL;
7933 /* If this type definition is nested inside another type,
7934 and is not an instantiation of a template, always leave
7935 at least a declaration in its place. */
7936 if (die->die_parent != NULL
7937 && is_type_die (die->die_parent)
7938 && !is_template_instantiation (die))
7939 node.new_die = clone_as_declaration (die);
7941 generate_skeleton_bottom_up (&node);
7942 return node.new_die;
7945 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7946 declaration. The original DIE is moved to a new compile unit so that
7947 existing references to it follow it to the new location. If any of the
7948 original DIE's descendants is a declaration, we need to replace the
7949 original DIE with a skeleton tree and move the declarations back into the
7950 skeleton tree. */
7952 static dw_die_ref
7953 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7954 dw_die_ref prev)
7956 dw_die_ref skeleton, orig_parent;
7958 /* Copy the declaration context to the type unit DIE. If the returned
7959 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7960 that DIE. */
7961 orig_parent = copy_declaration_context (unit, child);
7963 skeleton = generate_skeleton (child);
7964 if (skeleton == NULL)
7965 remove_child_with_prev (child, prev);
7966 else
7968 skeleton->comdat_type_p = true;
7969 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7971 /* If the original DIE was a specification, we need to put
7972 the skeleton under the parent DIE of the declaration.
7973 This leaves the original declaration in the tree, but
7974 it will be pruned later since there are no longer any
7975 references to it. */
7976 if (orig_parent != NULL)
7978 remove_child_with_prev (child, prev);
7979 add_child_die (orig_parent, skeleton);
7981 else
7982 replace_child (child, skeleton, prev);
7985 return skeleton;
7988 static void
7989 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7990 comdat_type_node *type_node,
7991 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7993 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7994 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7995 DWARF procedure references in the DW_AT_location attribute. */
7997 static dw_die_ref
7998 copy_dwarf_procedure (dw_die_ref die,
7999 comdat_type_node *type_node,
8000 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8002 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8004 /* DWARF procedures are not supposed to have children... */
8005 gcc_assert (die->die_child == NULL);
8007 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8008 gcc_assert (vec_safe_length (die->die_attr) == 1
8009 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8011 /* Do not copy more than once DWARF procedures. */
8012 bool existed;
8013 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8014 if (existed)
8015 return die_copy;
8017 die_copy = clone_die (die);
8018 add_child_die (type_node->root_die, die_copy);
8019 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8020 return die_copy;
8023 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8024 procedures in DIE's attributes. */
8026 static void
8027 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8028 comdat_type_node *type_node,
8029 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8031 dw_attr_node *a;
8032 unsigned i;
8034 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8036 dw_loc_descr_ref loc;
8038 if (a->dw_attr_val.val_class != dw_val_class_loc)
8039 continue;
8041 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8043 switch (loc->dw_loc_opc)
8045 case DW_OP_call2:
8046 case DW_OP_call4:
8047 case DW_OP_call_ref:
8048 gcc_assert (loc->dw_loc_oprnd1.val_class
8049 == dw_val_class_die_ref);
8050 loc->dw_loc_oprnd1.v.val_die_ref.die
8051 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8052 type_node,
8053 copied_dwarf_procs);
8055 default:
8056 break;
8062 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8063 rewrite references to point to the copies.
8065 References are looked for in DIE's attributes and recursively in all its
8066 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8067 mapping from old DWARF procedures to their copy. It is used not to copy
8068 twice the same DWARF procedure under TYPE_NODE. */
8070 static void
8071 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8072 comdat_type_node *type_node,
8073 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8075 dw_die_ref c;
8077 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8078 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8079 type_node,
8080 copied_dwarf_procs));
8083 /* Traverse the DIE and set up additional .debug_types or .debug_info
8084 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8085 section. */
8087 static void
8088 break_out_comdat_types (dw_die_ref die)
8090 dw_die_ref c;
8091 dw_die_ref first;
8092 dw_die_ref prev = NULL;
8093 dw_die_ref next = NULL;
8094 dw_die_ref unit = NULL;
8096 first = c = die->die_child;
8097 if (c)
8098 next = c->die_sib;
8099 if (c) do {
8100 if (prev == NULL || prev->die_sib == c)
8101 prev = c;
8102 c = next;
8103 next = (c == first ? NULL : c->die_sib);
8104 if (should_move_die_to_comdat (c))
8106 dw_die_ref replacement;
8107 comdat_type_node *type_node;
8109 /* Break out nested types into their own type units. */
8110 break_out_comdat_types (c);
8112 /* Create a new type unit DIE as the root for the new tree, and
8113 add it to the list of comdat types. */
8114 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8115 add_AT_unsigned (unit, DW_AT_language,
8116 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8117 type_node = ggc_cleared_alloc<comdat_type_node> ();
8118 type_node->root_die = unit;
8119 type_node->next = comdat_type_list;
8120 comdat_type_list = type_node;
8122 /* Generate the type signature. */
8123 generate_type_signature (c, type_node);
8125 /* Copy the declaration context, attributes, and children of the
8126 declaration into the new type unit DIE, then remove this DIE
8127 from the main CU (or replace it with a skeleton if necessary). */
8128 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8129 type_node->skeleton_die = replacement;
8131 /* Add the DIE to the new compunit. */
8132 add_child_die (unit, c);
8134 /* Types can reference DWARF procedures for type size or data location
8135 expressions. Calls in DWARF expressions cannot target procedures
8136 that are not in the same section. So we must copy DWARF procedures
8137 along with this type and then rewrite references to them. */
8138 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8139 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8141 if (replacement != NULL)
8142 c = replacement;
8144 else if (c->die_tag == DW_TAG_namespace
8145 || c->die_tag == DW_TAG_class_type
8146 || c->die_tag == DW_TAG_structure_type
8147 || c->die_tag == DW_TAG_union_type)
8149 /* Look for nested types that can be broken out. */
8150 break_out_comdat_types (c);
8152 } while (next != NULL);
8155 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8156 Enter all the cloned children into the hash table decl_table. */
8158 static dw_die_ref
8159 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8161 dw_die_ref c;
8162 dw_die_ref clone;
8163 struct decl_table_entry *entry;
8164 decl_table_entry **slot;
8166 if (die->die_tag == DW_TAG_subprogram)
8167 clone = clone_as_declaration (die);
8168 else
8169 clone = clone_die (die);
8171 slot = decl_table->find_slot_with_hash (die,
8172 htab_hash_pointer (die), INSERT);
8174 /* Assert that DIE isn't in the hash table yet. If it would be there
8175 before, the ancestors would be necessarily there as well, therefore
8176 clone_tree_partial wouldn't be called. */
8177 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8179 entry = XCNEW (struct decl_table_entry);
8180 entry->orig = die;
8181 entry->copy = clone;
8182 *slot = entry;
8184 if (die->die_tag != DW_TAG_subprogram)
8185 FOR_EACH_CHILD (die, c,
8186 add_child_die (clone, clone_tree_partial (c, decl_table)));
8188 return clone;
8191 /* Walk the DIE and its children, looking for references to incomplete
8192 or trivial types that are unmarked (i.e., that are not in the current
8193 type_unit). */
8195 static void
8196 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8198 dw_die_ref c;
8199 dw_attr_node *a;
8200 unsigned ix;
8202 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8204 if (AT_class (a) == dw_val_class_die_ref)
8206 dw_die_ref targ = AT_ref (a);
8207 decl_table_entry **slot;
8208 struct decl_table_entry *entry;
8210 if (targ->die_mark != 0 || targ->comdat_type_p)
8211 continue;
8213 slot = decl_table->find_slot_with_hash (targ,
8214 htab_hash_pointer (targ),
8215 INSERT);
8217 if (*slot != HTAB_EMPTY_ENTRY)
8219 /* TARG has already been copied, so we just need to
8220 modify the reference to point to the copy. */
8221 entry = *slot;
8222 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8224 else
8226 dw_die_ref parent = unit;
8227 dw_die_ref copy = clone_die (targ);
8229 /* Record in DECL_TABLE that TARG has been copied.
8230 Need to do this now, before the recursive call,
8231 because DECL_TABLE may be expanded and SLOT
8232 would no longer be a valid pointer. */
8233 entry = XCNEW (struct decl_table_entry);
8234 entry->orig = targ;
8235 entry->copy = copy;
8236 *slot = entry;
8238 /* If TARG is not a declaration DIE, we need to copy its
8239 children. */
8240 if (!is_declaration_die (targ))
8242 FOR_EACH_CHILD (
8243 targ, c,
8244 add_child_die (copy,
8245 clone_tree_partial (c, decl_table)));
8248 /* Make sure the cloned tree is marked as part of the
8249 type unit. */
8250 mark_dies (copy);
8252 /* If TARG has surrounding context, copy its ancestor tree
8253 into the new type unit. */
8254 if (targ->die_parent != NULL
8255 && !is_unit_die (targ->die_parent))
8256 parent = copy_ancestor_tree (unit, targ->die_parent,
8257 decl_table);
8259 add_child_die (parent, copy);
8260 a->dw_attr_val.v.val_die_ref.die = copy;
8262 /* Make sure the newly-copied DIE is walked. If it was
8263 installed in a previously-added context, it won't
8264 get visited otherwise. */
8265 if (parent != unit)
8267 /* Find the highest point of the newly-added tree,
8268 mark each node along the way, and walk from there. */
8269 parent->die_mark = 1;
8270 while (parent->die_parent
8271 && parent->die_parent->die_mark == 0)
8273 parent = parent->die_parent;
8274 parent->die_mark = 1;
8276 copy_decls_walk (unit, parent, decl_table);
8282 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8285 /* Copy declarations for "unworthy" types into the new comdat section.
8286 Incomplete types, modified types, and certain other types aren't broken
8287 out into comdat sections of their own, so they don't have a signature,
8288 and we need to copy the declaration into the same section so that we
8289 don't have an external reference. */
8291 static void
8292 copy_decls_for_unworthy_types (dw_die_ref unit)
8294 mark_dies (unit);
8295 decl_hash_type decl_table (10);
8296 copy_decls_walk (unit, unit, &decl_table);
8297 unmark_dies (unit);
8300 /* Traverse the DIE and add a sibling attribute if it may have the
8301 effect of speeding up access to siblings. To save some space,
8302 avoid generating sibling attributes for DIE's without children. */
8304 static void
8305 add_sibling_attributes (dw_die_ref die)
8307 dw_die_ref c;
8309 if (! die->die_child)
8310 return;
8312 if (die->die_parent && die != die->die_parent->die_child)
8313 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8315 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8318 /* Output all location lists for the DIE and its children. */
8320 static void
8321 output_location_lists (dw_die_ref die)
8323 dw_die_ref c;
8324 dw_attr_node *a;
8325 unsigned ix;
8327 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8328 if (AT_class (a) == dw_val_class_loc_list)
8329 output_loc_list (AT_loc_list (a));
8331 FOR_EACH_CHILD (die, c, output_location_lists (c));
8334 /* During assign_location_list_indexes and output_loclists_offset the
8335 current index, after it the number of assigned indexes (i.e. how
8336 large the .debug_loclists* offset table should be). */
8337 static unsigned int loc_list_idx;
8339 /* Output all location list offsets for the DIE and its children. */
8341 static void
8342 output_loclists_offsets (dw_die_ref die)
8344 dw_die_ref c;
8345 dw_attr_node *a;
8346 unsigned ix;
8348 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8349 if (AT_class (a) == dw_val_class_loc_list)
8351 dw_loc_list_ref l = AT_loc_list (a);
8352 if (l->offset_emitted)
8353 continue;
8354 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8355 loc_section_label, NULL);
8356 gcc_assert (l->hash == loc_list_idx);
8357 loc_list_idx++;
8358 l->offset_emitted = true;
8361 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8364 /* Recursively set indexes of location lists. */
8366 static void
8367 assign_location_list_indexes (dw_die_ref die)
8369 dw_die_ref c;
8370 dw_attr_node *a;
8371 unsigned ix;
8373 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8374 if (AT_class (a) == dw_val_class_loc_list)
8376 dw_loc_list_ref list = AT_loc_list (a);
8377 if (!list->num_assigned)
8379 list->num_assigned = true;
8380 list->hash = loc_list_idx++;
8384 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8387 /* We want to limit the number of external references, because they are
8388 larger than local references: a relocation takes multiple words, and
8389 even a sig8 reference is always eight bytes, whereas a local reference
8390 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8391 So if we encounter multiple external references to the same type DIE, we
8392 make a local typedef stub for it and redirect all references there.
8394 This is the element of the hash table for keeping track of these
8395 references. */
8397 struct external_ref
8399 dw_die_ref type;
8400 dw_die_ref stub;
8401 unsigned n_refs;
8404 /* Hashtable helpers. */
8406 struct external_ref_hasher : free_ptr_hash <external_ref>
8408 static inline hashval_t hash (const external_ref *);
8409 static inline bool equal (const external_ref *, const external_ref *);
8412 inline hashval_t
8413 external_ref_hasher::hash (const external_ref *r)
8415 dw_die_ref die = r->type;
8416 hashval_t h = 0;
8418 /* We can't use the address of the DIE for hashing, because
8419 that will make the order of the stub DIEs non-deterministic. */
8420 if (! die->comdat_type_p)
8421 /* We have a symbol; use it to compute a hash. */
8422 h = htab_hash_string (die->die_id.die_symbol);
8423 else
8425 /* We have a type signature; use a subset of the bits as the hash.
8426 The 8-byte signature is at least as large as hashval_t. */
8427 comdat_type_node *type_node = die->die_id.die_type_node;
8428 memcpy (&h, type_node->signature, sizeof (h));
8430 return h;
8433 inline bool
8434 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8436 return r1->type == r2->type;
8439 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8441 /* Return a pointer to the external_ref for references to DIE. */
8443 static struct external_ref *
8444 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8446 struct external_ref ref, *ref_p;
8447 external_ref **slot;
8449 ref.type = die;
8450 slot = map->find_slot (&ref, INSERT);
8451 if (*slot != HTAB_EMPTY_ENTRY)
8452 return *slot;
8454 ref_p = XCNEW (struct external_ref);
8455 ref_p->type = die;
8456 *slot = ref_p;
8457 return ref_p;
8460 /* Subroutine of optimize_external_refs, below.
8462 If we see a type skeleton, record it as our stub. If we see external
8463 references, remember how many we've seen. */
8465 static void
8466 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8468 dw_die_ref c;
8469 dw_attr_node *a;
8470 unsigned ix;
8471 struct external_ref *ref_p;
8473 if (is_type_die (die)
8474 && (c = get_AT_ref (die, DW_AT_signature)))
8476 /* This is a local skeleton; use it for local references. */
8477 ref_p = lookup_external_ref (map, c);
8478 ref_p->stub = die;
8481 /* Scan the DIE references, and remember any that refer to DIEs from
8482 other CUs (i.e. those which are not marked). */
8483 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8484 if (AT_class (a) == dw_val_class_die_ref
8485 && (c = AT_ref (a))->die_mark == 0
8486 && is_type_die (c))
8488 ref_p = lookup_external_ref (map, c);
8489 ref_p->n_refs++;
8492 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8495 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8496 points to an external_ref, DATA is the CU we're processing. If we don't
8497 already have a local stub, and we have multiple refs, build a stub. */
8500 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8502 struct external_ref *ref_p = *slot;
8504 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8506 /* We have multiple references to this type, so build a small stub.
8507 Both of these forms are a bit dodgy from the perspective of the
8508 DWARF standard, since technically they should have names. */
8509 dw_die_ref cu = data;
8510 dw_die_ref type = ref_p->type;
8511 dw_die_ref stub = NULL;
8513 if (type->comdat_type_p)
8515 /* If we refer to this type via sig8, use AT_signature. */
8516 stub = new_die (type->die_tag, cu, NULL_TREE);
8517 add_AT_die_ref (stub, DW_AT_signature, type);
8519 else
8521 /* Otherwise, use a typedef with no name. */
8522 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8523 add_AT_die_ref (stub, DW_AT_type, type);
8526 stub->die_mark++;
8527 ref_p->stub = stub;
8529 return 1;
8532 /* DIE is a unit; look through all the DIE references to see if there are
8533 any external references to types, and if so, create local stubs for
8534 them which will be applied in build_abbrev_table. This is useful because
8535 references to local DIEs are smaller. */
8537 static external_ref_hash_type *
8538 optimize_external_refs (dw_die_ref die)
8540 external_ref_hash_type *map = new external_ref_hash_type (10);
8541 optimize_external_refs_1 (die, map);
8542 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8543 return map;
8546 /* The following 3 variables are temporaries that are computed only during the
8547 build_abbrev_table call and used and released during the following
8548 optimize_abbrev_table call. */
8550 /* First abbrev_id that can be optimized based on usage. */
8551 static unsigned int abbrev_opt_start;
8553 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8554 abbrev_id smaller than this, because they must be already sized
8555 during build_abbrev_table). */
8556 static unsigned int abbrev_opt_base_type_end;
8558 /* Vector of usage counts during build_abbrev_table. Indexed by
8559 abbrev_id - abbrev_opt_start. */
8560 static vec<unsigned int> abbrev_usage_count;
8562 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8563 static vec<dw_die_ref> sorted_abbrev_dies;
8565 /* The format of each DIE (and its attribute value pairs) is encoded in an
8566 abbreviation table. This routine builds the abbreviation table and assigns
8567 a unique abbreviation id for each abbreviation entry. The children of each
8568 die are visited recursively. */
8570 static void
8571 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8573 unsigned int abbrev_id = 0;
8574 dw_die_ref c;
8575 dw_attr_node *a;
8576 unsigned ix;
8577 dw_die_ref abbrev;
8579 /* Scan the DIE references, and replace any that refer to
8580 DIEs from other CUs (i.e. those which are not marked) with
8581 the local stubs we built in optimize_external_refs. */
8582 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8583 if (AT_class (a) == dw_val_class_die_ref
8584 && (c = AT_ref (a))->die_mark == 0)
8586 struct external_ref *ref_p;
8587 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8589 ref_p = lookup_external_ref (extern_map, c);
8590 if (ref_p->stub && ref_p->stub != die)
8591 change_AT_die_ref (a, ref_p->stub);
8592 else
8593 /* We aren't changing this reference, so mark it external. */
8594 set_AT_ref_external (a, 1);
8597 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8599 dw_attr_node *die_a, *abbrev_a;
8600 unsigned ix;
8601 bool ok = true;
8603 if (abbrev_id == 0)
8604 continue;
8605 if (abbrev->die_tag != die->die_tag)
8606 continue;
8607 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8608 continue;
8610 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8611 continue;
8613 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8615 abbrev_a = &(*abbrev->die_attr)[ix];
8616 if ((abbrev_a->dw_attr != die_a->dw_attr)
8617 || (value_format (abbrev_a) != value_format (die_a)))
8619 ok = false;
8620 break;
8623 if (ok)
8624 break;
8627 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8629 vec_safe_push (abbrev_die_table, die);
8630 if (abbrev_opt_start)
8631 abbrev_usage_count.safe_push (0);
8633 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8635 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8636 sorted_abbrev_dies.safe_push (die);
8639 die->die_abbrev = abbrev_id;
8640 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8643 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8644 by die_abbrev's usage count, from the most commonly used
8645 abbreviation to the least. */
8647 static int
8648 die_abbrev_cmp (const void *p1, const void *p2)
8650 dw_die_ref die1 = *(const dw_die_ref *) p1;
8651 dw_die_ref die2 = *(const dw_die_ref *) p2;
8653 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8654 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8656 if (die1->die_abbrev >= abbrev_opt_base_type_end
8657 && die2->die_abbrev >= abbrev_opt_base_type_end)
8659 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8660 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8661 return -1;
8662 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8663 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8664 return 1;
8667 /* Stabilize the sort. */
8668 if (die1->die_abbrev < die2->die_abbrev)
8669 return -1;
8670 if (die1->die_abbrev > die2->die_abbrev)
8671 return 1;
8673 return 0;
8676 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8677 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8678 into dw_val_class_const_implicit or
8679 dw_val_class_unsigned_const_implicit. */
8681 static void
8682 optimize_implicit_const (unsigned int first_id, unsigned int end,
8683 vec<bool> &implicit_consts)
8685 /* It never makes sense if there is just one DIE using the abbreviation. */
8686 if (end < first_id + 2)
8687 return;
8689 dw_attr_node *a;
8690 unsigned ix, i;
8691 dw_die_ref die = sorted_abbrev_dies[first_id];
8692 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8693 if (implicit_consts[ix])
8695 enum dw_val_class new_class = dw_val_class_none;
8696 switch (AT_class (a))
8698 case dw_val_class_unsigned_const:
8699 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8700 continue;
8702 /* The .debug_abbrev section will grow by
8703 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8704 in all the DIEs using that abbreviation. */
8705 if (constant_size (AT_unsigned (a)) * (end - first_id)
8706 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8707 continue;
8709 new_class = dw_val_class_unsigned_const_implicit;
8710 break;
8712 case dw_val_class_const:
8713 new_class = dw_val_class_const_implicit;
8714 break;
8716 case dw_val_class_file:
8717 new_class = dw_val_class_file_implicit;
8718 break;
8720 default:
8721 continue;
8723 for (i = first_id; i < end; i++)
8724 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8725 = new_class;
8729 /* Attempt to optimize abbreviation table from abbrev_opt_start
8730 abbreviation above. */
8732 static void
8733 optimize_abbrev_table (void)
8735 if (abbrev_opt_start
8736 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8737 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8739 auto_vec<bool, 32> implicit_consts;
8740 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8742 unsigned int abbrev_id = abbrev_opt_start - 1;
8743 unsigned int first_id = ~0U;
8744 unsigned int last_abbrev_id = 0;
8745 unsigned int i;
8746 dw_die_ref die;
8747 if (abbrev_opt_base_type_end > abbrev_opt_start)
8748 abbrev_id = abbrev_opt_base_type_end - 1;
8749 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8750 most commonly used abbreviations come first. */
8751 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8753 dw_attr_node *a;
8754 unsigned ix;
8756 /* If calc_base_type_die_sizes has been called, the CU and
8757 base types after it can't be optimized, because we've already
8758 calculated their DIE offsets. We've sorted them first. */
8759 if (die->die_abbrev < abbrev_opt_base_type_end)
8760 continue;
8761 if (die->die_abbrev != last_abbrev_id)
8763 last_abbrev_id = die->die_abbrev;
8764 if (dwarf_version >= 5 && first_id != ~0U)
8765 optimize_implicit_const (first_id, i, implicit_consts);
8766 abbrev_id++;
8767 (*abbrev_die_table)[abbrev_id] = die;
8768 if (dwarf_version >= 5)
8770 first_id = i;
8771 implicit_consts.truncate (0);
8773 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8774 switch (AT_class (a))
8776 case dw_val_class_const:
8777 case dw_val_class_unsigned_const:
8778 case dw_val_class_file:
8779 implicit_consts.safe_push (true);
8780 break;
8781 default:
8782 implicit_consts.safe_push (false);
8783 break;
8787 else if (dwarf_version >= 5)
8789 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8790 if (!implicit_consts[ix])
8791 continue;
8792 else
8794 dw_attr_node *other_a
8795 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8796 if (!dw_val_equal_p (&a->dw_attr_val,
8797 &other_a->dw_attr_val))
8798 implicit_consts[ix] = false;
8801 die->die_abbrev = abbrev_id;
8803 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8804 if (dwarf_version >= 5 && first_id != ~0U)
8805 optimize_implicit_const (first_id, i, implicit_consts);
8808 abbrev_opt_start = 0;
8809 abbrev_opt_base_type_end = 0;
8810 abbrev_usage_count.release ();
8811 sorted_abbrev_dies.release ();
8814 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8816 static int
8817 constant_size (unsigned HOST_WIDE_INT value)
8819 int log;
8821 if (value == 0)
8822 log = 0;
8823 else
8824 log = floor_log2 (value);
8826 log = log / 8;
8827 log = 1 << (floor_log2 (log) + 1);
8829 return log;
8832 /* Return the size of a DIE as it is represented in the
8833 .debug_info section. */
8835 static unsigned long
8836 size_of_die (dw_die_ref die)
8838 unsigned long size = 0;
8839 dw_attr_node *a;
8840 unsigned ix;
8841 enum dwarf_form form;
8843 size += size_of_uleb128 (die->die_abbrev);
8844 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8846 switch (AT_class (a))
8848 case dw_val_class_addr:
8849 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8851 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8852 size += size_of_uleb128 (AT_index (a));
8854 else
8855 size += DWARF2_ADDR_SIZE;
8856 break;
8857 case dw_val_class_offset:
8858 size += DWARF_OFFSET_SIZE;
8859 break;
8860 case dw_val_class_loc:
8862 unsigned long lsize = size_of_locs (AT_loc (a));
8864 /* Block length. */
8865 if (dwarf_version >= 4)
8866 size += size_of_uleb128 (lsize);
8867 else
8868 size += constant_size (lsize);
8869 size += lsize;
8871 break;
8872 case dw_val_class_loc_list:
8873 if (dwarf_split_debug_info && dwarf_version >= 5)
8875 gcc_assert (AT_loc_list (a)->num_assigned);
8876 size += size_of_uleb128 (AT_loc_list (a)->hash);
8878 else
8879 size += DWARF_OFFSET_SIZE;
8880 break;
8881 case dw_val_class_range_list:
8882 if (value_format (a) == DW_FORM_rnglistx)
8884 gcc_assert (rnglist_idx);
8885 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8886 size += size_of_uleb128 (r->idx);
8888 else
8889 size += DWARF_OFFSET_SIZE;
8890 break;
8891 case dw_val_class_const:
8892 size += size_of_sleb128 (AT_int (a));
8893 break;
8894 case dw_val_class_unsigned_const:
8896 int csize = constant_size (AT_unsigned (a));
8897 if (dwarf_version == 3
8898 && a->dw_attr == DW_AT_data_member_location
8899 && csize >= 4)
8900 size += size_of_uleb128 (AT_unsigned (a));
8901 else
8902 size += csize;
8904 break;
8905 case dw_val_class_const_implicit:
8906 case dw_val_class_unsigned_const_implicit:
8907 case dw_val_class_file_implicit:
8908 /* These occupy no size in the DIE, just an extra sleb128 in
8909 .debug_abbrev. */
8910 break;
8911 case dw_val_class_const_double:
8912 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8913 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8914 size++; /* block */
8915 break;
8916 case dw_val_class_wide_int:
8917 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8918 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8919 if (get_full_len (*a->dw_attr_val.v.val_wide)
8920 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
8921 size++; /* block */
8922 break;
8923 case dw_val_class_vec:
8924 size += constant_size (a->dw_attr_val.v.val_vec.length
8925 * a->dw_attr_val.v.val_vec.elt_size)
8926 + a->dw_attr_val.v.val_vec.length
8927 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8928 break;
8929 case dw_val_class_flag:
8930 if (dwarf_version >= 4)
8931 /* Currently all add_AT_flag calls pass in 1 as last argument,
8932 so DW_FORM_flag_present can be used. If that ever changes,
8933 we'll need to use DW_FORM_flag and have some optimization
8934 in build_abbrev_table that will change those to
8935 DW_FORM_flag_present if it is set to 1 in all DIEs using
8936 the same abbrev entry. */
8937 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8938 else
8939 size += 1;
8940 break;
8941 case dw_val_class_die_ref:
8942 if (AT_ref_external (a))
8944 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8945 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8946 is sized by target address length, whereas in DWARF3
8947 it's always sized as an offset. */
8948 if (use_debug_types)
8949 size += DWARF_TYPE_SIGNATURE_SIZE;
8950 else if (dwarf_version == 2)
8951 size += DWARF2_ADDR_SIZE;
8952 else
8953 size += DWARF_OFFSET_SIZE;
8955 else
8956 size += DWARF_OFFSET_SIZE;
8957 break;
8958 case dw_val_class_fde_ref:
8959 size += DWARF_OFFSET_SIZE;
8960 break;
8961 case dw_val_class_lbl_id:
8962 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8964 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8965 size += size_of_uleb128 (AT_index (a));
8967 else
8968 size += DWARF2_ADDR_SIZE;
8969 break;
8970 case dw_val_class_lineptr:
8971 case dw_val_class_macptr:
8972 case dw_val_class_loclistsptr:
8973 size += DWARF_OFFSET_SIZE;
8974 break;
8975 case dw_val_class_str:
8976 form = AT_string_form (a);
8977 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
8978 size += DWARF_OFFSET_SIZE;
8979 else if (form == DW_FORM_GNU_str_index)
8980 size += size_of_uleb128 (AT_index (a));
8981 else
8982 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8983 break;
8984 case dw_val_class_file:
8985 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8986 break;
8987 case dw_val_class_data8:
8988 size += 8;
8989 break;
8990 case dw_val_class_vms_delta:
8991 size += DWARF_OFFSET_SIZE;
8992 break;
8993 case dw_val_class_high_pc:
8994 size += DWARF2_ADDR_SIZE;
8995 break;
8996 case dw_val_class_discr_value:
8997 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8998 break;
8999 case dw_val_class_discr_list:
9001 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9003 /* This is a block, so we have the block length and then its
9004 data. */
9005 size += constant_size (block_size) + block_size;
9007 break;
9008 default:
9009 gcc_unreachable ();
9013 return size;
9016 /* Size the debugging information associated with a given DIE. Visits the
9017 DIE's children recursively. Updates the global variable next_die_offset, on
9018 each time through. Uses the current value of next_die_offset to update the
9019 die_offset field in each DIE. */
9021 static void
9022 calc_die_sizes (dw_die_ref die)
9024 dw_die_ref c;
9026 gcc_assert (die->die_offset == 0
9027 || (unsigned long int) die->die_offset == next_die_offset);
9028 die->die_offset = next_die_offset;
9029 next_die_offset += size_of_die (die);
9031 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9033 if (die->die_child != NULL)
9034 /* Count the null byte used to terminate sibling lists. */
9035 next_die_offset += 1;
9038 /* Size just the base type children at the start of the CU.
9039 This is needed because build_abbrev needs to size locs
9040 and sizing of type based stack ops needs to know die_offset
9041 values for the base types. */
9043 static void
9044 calc_base_type_die_sizes (void)
9046 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9047 unsigned int i;
9048 dw_die_ref base_type;
9049 #if ENABLE_ASSERT_CHECKING
9050 dw_die_ref prev = comp_unit_die ()->die_child;
9051 #endif
9053 die_offset += size_of_die (comp_unit_die ());
9054 for (i = 0; base_types.iterate (i, &base_type); i++)
9056 #if ENABLE_ASSERT_CHECKING
9057 gcc_assert (base_type->die_offset == 0
9058 && prev->die_sib == base_type
9059 && base_type->die_child == NULL
9060 && base_type->die_abbrev);
9061 prev = base_type;
9062 #endif
9063 if (abbrev_opt_start
9064 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9065 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9066 base_type->die_offset = die_offset;
9067 die_offset += size_of_die (base_type);
9071 /* Set the marks for a die and its children. We do this so
9072 that we know whether or not a reference needs to use FORM_ref_addr; only
9073 DIEs in the same CU will be marked. We used to clear out the offset
9074 and use that as the flag, but ran into ordering problems. */
9076 static void
9077 mark_dies (dw_die_ref die)
9079 dw_die_ref c;
9081 gcc_assert (!die->die_mark);
9083 die->die_mark = 1;
9084 FOR_EACH_CHILD (die, c, mark_dies (c));
9087 /* Clear the marks for a die and its children. */
9089 static void
9090 unmark_dies (dw_die_ref die)
9092 dw_die_ref c;
9094 if (! use_debug_types)
9095 gcc_assert (die->die_mark);
9097 die->die_mark = 0;
9098 FOR_EACH_CHILD (die, c, unmark_dies (c));
9101 /* Clear the marks for a die, its children and referred dies. */
9103 static void
9104 unmark_all_dies (dw_die_ref die)
9106 dw_die_ref c;
9107 dw_attr_node *a;
9108 unsigned ix;
9110 if (!die->die_mark)
9111 return;
9112 die->die_mark = 0;
9114 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9116 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9117 if (AT_class (a) == dw_val_class_die_ref)
9118 unmark_all_dies (AT_ref (a));
9121 /* Calculate if the entry should appear in the final output file. It may be
9122 from a pruned a type. */
9124 static bool
9125 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9127 /* By limiting gnu pubnames to definitions only, gold can generate a
9128 gdb index without entries for declarations, which don't include
9129 enough information to be useful. */
9130 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9131 return false;
9133 if (table == pubname_table)
9135 /* Enumerator names are part of the pubname table, but the
9136 parent DW_TAG_enumeration_type die may have been pruned.
9137 Don't output them if that is the case. */
9138 if (p->die->die_tag == DW_TAG_enumerator &&
9139 (p->die->die_parent == NULL
9140 || !p->die->die_parent->die_perennial_p))
9141 return false;
9143 /* Everything else in the pubname table is included. */
9144 return true;
9147 /* The pubtypes table shouldn't include types that have been
9148 pruned. */
9149 return (p->die->die_offset != 0
9150 || !flag_eliminate_unused_debug_types);
9153 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9154 generated for the compilation unit. */
9156 static unsigned long
9157 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9159 unsigned long size;
9160 unsigned i;
9161 pubname_entry *p;
9162 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9164 size = DWARF_PUBNAMES_HEADER_SIZE;
9165 FOR_EACH_VEC_ELT (*names, i, p)
9166 if (include_pubname_in_output (names, p))
9167 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9169 size += DWARF_OFFSET_SIZE;
9170 return size;
9173 /* Return the size of the information in the .debug_aranges section. */
9175 static unsigned long
9176 size_of_aranges (void)
9178 unsigned long size;
9180 size = DWARF_ARANGES_HEADER_SIZE;
9182 /* Count the address/length pair for this compilation unit. */
9183 if (text_section_used)
9184 size += 2 * DWARF2_ADDR_SIZE;
9185 if (cold_text_section_used)
9186 size += 2 * DWARF2_ADDR_SIZE;
9187 if (have_multiple_function_sections)
9189 unsigned fde_idx;
9190 dw_fde_ref fde;
9192 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9194 if (DECL_IGNORED_P (fde->decl))
9195 continue;
9196 if (!fde->in_std_section)
9197 size += 2 * DWARF2_ADDR_SIZE;
9198 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9199 size += 2 * DWARF2_ADDR_SIZE;
9203 /* Count the two zero words used to terminated the address range table. */
9204 size += 2 * DWARF2_ADDR_SIZE;
9205 return size;
9208 /* Select the encoding of an attribute value. */
9210 static enum dwarf_form
9211 value_format (dw_attr_node *a)
9213 switch (AT_class (a))
9215 case dw_val_class_addr:
9216 /* Only very few attributes allow DW_FORM_addr. */
9217 switch (a->dw_attr)
9219 case DW_AT_low_pc:
9220 case DW_AT_high_pc:
9221 case DW_AT_entry_pc:
9222 case DW_AT_trampoline:
9223 return (AT_index (a) == NOT_INDEXED
9224 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9225 default:
9226 break;
9228 switch (DWARF2_ADDR_SIZE)
9230 case 1:
9231 return DW_FORM_data1;
9232 case 2:
9233 return DW_FORM_data2;
9234 case 4:
9235 return DW_FORM_data4;
9236 case 8:
9237 return DW_FORM_data8;
9238 default:
9239 gcc_unreachable ();
9241 case dw_val_class_loc_list:
9242 if (dwarf_split_debug_info
9243 && dwarf_version >= 5
9244 && AT_loc_list (a)->num_assigned)
9245 return DW_FORM_loclistx;
9246 /* FALLTHRU */
9247 case dw_val_class_range_list:
9248 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9249 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9250 care about sizes of .debug* sections in shared libraries and
9251 executables and don't take into account relocations that affect just
9252 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9253 table in the .debug_rnglists section. */
9254 if (dwarf_split_debug_info
9255 && dwarf_version >= 5
9256 && AT_class (a) == dw_val_class_range_list
9257 && rnglist_idx
9258 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9259 return DW_FORM_rnglistx;
9260 if (dwarf_version >= 4)
9261 return DW_FORM_sec_offset;
9262 /* FALLTHRU */
9263 case dw_val_class_vms_delta:
9264 case dw_val_class_offset:
9265 switch (DWARF_OFFSET_SIZE)
9267 case 4:
9268 return DW_FORM_data4;
9269 case 8:
9270 return DW_FORM_data8;
9271 default:
9272 gcc_unreachable ();
9274 case dw_val_class_loc:
9275 if (dwarf_version >= 4)
9276 return DW_FORM_exprloc;
9277 switch (constant_size (size_of_locs (AT_loc (a))))
9279 case 1:
9280 return DW_FORM_block1;
9281 case 2:
9282 return DW_FORM_block2;
9283 case 4:
9284 return DW_FORM_block4;
9285 default:
9286 gcc_unreachable ();
9288 case dw_val_class_const:
9289 return DW_FORM_sdata;
9290 case dw_val_class_unsigned_const:
9291 switch (constant_size (AT_unsigned (a)))
9293 case 1:
9294 return DW_FORM_data1;
9295 case 2:
9296 return DW_FORM_data2;
9297 case 4:
9298 /* In DWARF3 DW_AT_data_member_location with
9299 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9300 constant, so we need to use DW_FORM_udata if we need
9301 a large constant. */
9302 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9303 return DW_FORM_udata;
9304 return DW_FORM_data4;
9305 case 8:
9306 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9307 return DW_FORM_udata;
9308 return DW_FORM_data8;
9309 default:
9310 gcc_unreachable ();
9312 case dw_val_class_const_implicit:
9313 case dw_val_class_unsigned_const_implicit:
9314 case dw_val_class_file_implicit:
9315 return DW_FORM_implicit_const;
9316 case dw_val_class_const_double:
9317 switch (HOST_BITS_PER_WIDE_INT)
9319 case 8:
9320 return DW_FORM_data2;
9321 case 16:
9322 return DW_FORM_data4;
9323 case 32:
9324 return DW_FORM_data8;
9325 case 64:
9326 if (dwarf_version >= 5)
9327 return DW_FORM_data16;
9328 /* FALLTHRU */
9329 default:
9330 return DW_FORM_block1;
9332 case dw_val_class_wide_int:
9333 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9335 case 8:
9336 return DW_FORM_data1;
9337 case 16:
9338 return DW_FORM_data2;
9339 case 32:
9340 return DW_FORM_data4;
9341 case 64:
9342 return DW_FORM_data8;
9343 case 128:
9344 if (dwarf_version >= 5)
9345 return DW_FORM_data16;
9346 /* FALLTHRU */
9347 default:
9348 return DW_FORM_block1;
9350 case dw_val_class_vec:
9351 switch (constant_size (a->dw_attr_val.v.val_vec.length
9352 * a->dw_attr_val.v.val_vec.elt_size))
9354 case 1:
9355 return DW_FORM_block1;
9356 case 2:
9357 return DW_FORM_block2;
9358 case 4:
9359 return DW_FORM_block4;
9360 default:
9361 gcc_unreachable ();
9363 case dw_val_class_flag:
9364 if (dwarf_version >= 4)
9366 /* Currently all add_AT_flag calls pass in 1 as last argument,
9367 so DW_FORM_flag_present can be used. If that ever changes,
9368 we'll need to use DW_FORM_flag and have some optimization
9369 in build_abbrev_table that will change those to
9370 DW_FORM_flag_present if it is set to 1 in all DIEs using
9371 the same abbrev entry. */
9372 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9373 return DW_FORM_flag_present;
9375 return DW_FORM_flag;
9376 case dw_val_class_die_ref:
9377 if (AT_ref_external (a))
9378 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9379 else
9380 return DW_FORM_ref;
9381 case dw_val_class_fde_ref:
9382 return DW_FORM_data;
9383 case dw_val_class_lbl_id:
9384 return (AT_index (a) == NOT_INDEXED
9385 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9386 case dw_val_class_lineptr:
9387 case dw_val_class_macptr:
9388 case dw_val_class_loclistsptr:
9389 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9390 case dw_val_class_str:
9391 return AT_string_form (a);
9392 case dw_val_class_file:
9393 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9395 case 1:
9396 return DW_FORM_data1;
9397 case 2:
9398 return DW_FORM_data2;
9399 case 4:
9400 return DW_FORM_data4;
9401 default:
9402 gcc_unreachable ();
9405 case dw_val_class_data8:
9406 return DW_FORM_data8;
9408 case dw_val_class_high_pc:
9409 switch (DWARF2_ADDR_SIZE)
9411 case 1:
9412 return DW_FORM_data1;
9413 case 2:
9414 return DW_FORM_data2;
9415 case 4:
9416 return DW_FORM_data4;
9417 case 8:
9418 return DW_FORM_data8;
9419 default:
9420 gcc_unreachable ();
9423 case dw_val_class_discr_value:
9424 return (a->dw_attr_val.v.val_discr_value.pos
9425 ? DW_FORM_udata
9426 : DW_FORM_sdata);
9427 case dw_val_class_discr_list:
9428 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9430 case 1:
9431 return DW_FORM_block1;
9432 case 2:
9433 return DW_FORM_block2;
9434 case 4:
9435 return DW_FORM_block4;
9436 default:
9437 gcc_unreachable ();
9440 default:
9441 gcc_unreachable ();
9445 /* Output the encoding of an attribute value. */
9447 static void
9448 output_value_format (dw_attr_node *a)
9450 enum dwarf_form form = value_format (a);
9452 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9455 /* Given a die and id, produce the appropriate abbreviations. */
9457 static void
9458 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9460 unsigned ix;
9461 dw_attr_node *a_attr;
9463 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9464 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9465 dwarf_tag_name (abbrev->die_tag));
9467 if (abbrev->die_child != NULL)
9468 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9469 else
9470 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9472 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9474 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9475 dwarf_attr_name (a_attr->dw_attr));
9476 output_value_format (a_attr);
9477 if (value_format (a_attr) == DW_FORM_implicit_const)
9479 if (AT_class (a_attr) == dw_val_class_file_implicit)
9481 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9482 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9483 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9485 else
9486 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9490 dw2_asm_output_data (1, 0, NULL);
9491 dw2_asm_output_data (1, 0, NULL);
9495 /* Output the .debug_abbrev section which defines the DIE abbreviation
9496 table. */
9498 static void
9499 output_abbrev_section (void)
9501 unsigned int abbrev_id;
9502 dw_die_ref abbrev;
9504 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9505 if (abbrev_id != 0)
9506 output_die_abbrevs (abbrev_id, abbrev);
9508 /* Terminate the table. */
9509 dw2_asm_output_data (1, 0, NULL);
9512 /* Output a symbol we can use to refer to this DIE from another CU. */
9514 static inline void
9515 output_die_symbol (dw_die_ref die)
9517 const char *sym = die->die_id.die_symbol;
9519 gcc_assert (!die->comdat_type_p);
9521 if (sym == 0)
9522 return;
9524 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9525 /* We make these global, not weak; if the target doesn't support
9526 .linkonce, it doesn't support combining the sections, so debugging
9527 will break. */
9528 targetm.asm_out.globalize_label (asm_out_file, sym);
9530 ASM_OUTPUT_LABEL (asm_out_file, sym);
9533 /* Return a new location list, given the begin and end range, and the
9534 expression. */
9536 static inline dw_loc_list_ref
9537 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9538 const char *section)
9540 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9542 retlist->begin = begin;
9543 retlist->begin_entry = NULL;
9544 retlist->end = end;
9545 retlist->expr = expr;
9546 retlist->section = section;
9548 return retlist;
9551 /* Generate a new internal symbol for this location list node, if it
9552 hasn't got one yet. */
9554 static inline void
9555 gen_llsym (dw_loc_list_ref list)
9557 gcc_assert (!list->ll_symbol);
9558 list->ll_symbol = gen_internal_sym ("LLST");
9561 /* Output the location list given to us. */
9563 static void
9564 output_loc_list (dw_loc_list_ref list_head)
9566 if (list_head->emitted)
9567 return;
9568 list_head->emitted = true;
9570 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9572 dw_loc_list_ref curr = list_head;
9573 const char *last_section = NULL;
9574 const char *base_label = NULL;
9576 /* Walk the location list, and output each range + expression. */
9577 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9579 unsigned long size;
9580 /* Don't output an entry that starts and ends at the same address. */
9581 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9582 continue;
9583 size = size_of_locs (curr->expr);
9584 /* If the expression is too large, drop it on the floor. We could
9585 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9586 in the expression, but >= 64KB expressions for a single value
9587 in a single range are unlikely very useful. */
9588 if (size > 0xffff)
9589 continue;
9590 if (dwarf_version >= 5)
9592 if (dwarf_split_debug_info)
9594 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9595 uleb128 index into .debug_addr and uleb128 length. */
9596 dw2_asm_output_data (1, DW_LLE_startx_length,
9597 "DW_LLE_startx_length (%s)",
9598 list_head->ll_symbol);
9599 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9600 "Location list range start index "
9601 "(%s)", curr->begin);
9602 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9603 For that case we probably need to emit DW_LLE_startx_endx,
9604 but we'd need 2 .debug_addr entries rather than just one. */
9605 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9606 "Location list length (%s)",
9607 list_head->ll_symbol);
9609 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9611 /* If all code is in .text section, the base address is
9612 already provided by the CU attributes. Use
9613 DW_LLE_offset_pair where both addresses are uleb128 encoded
9614 offsets against that base. */
9615 dw2_asm_output_data (1, DW_LLE_offset_pair,
9616 "DW_LLE_offset_pair (%s)",
9617 list_head->ll_symbol);
9618 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9619 "Location list begin address (%s)",
9620 list_head->ll_symbol);
9621 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9622 "Location list end address (%s)",
9623 list_head->ll_symbol);
9625 else if (HAVE_AS_LEB128)
9627 /* Otherwise, find out how many consecutive entries could share
9628 the same base entry. If just one, emit DW_LLE_start_length,
9629 otherwise emit DW_LLE_base_address for the base address
9630 followed by a series of DW_LLE_offset_pair. */
9631 if (last_section == NULL || curr->section != last_section)
9633 dw_loc_list_ref curr2;
9634 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9635 curr2 = curr2->dw_loc_next)
9637 if (strcmp (curr2->begin, curr2->end) == 0
9638 && !curr2->force)
9639 continue;
9640 if ((unsigned long) size_of_locs (curr2->expr) > 0xffff)
9641 continue;
9642 break;
9644 if (curr2 == NULL || curr->section != curr2->section)
9645 last_section = NULL;
9646 else
9648 last_section = curr->section;
9649 base_label = curr->begin;
9650 dw2_asm_output_data (1, DW_LLE_base_address,
9651 "DW_LLE_base_address (%s)",
9652 list_head->ll_symbol);
9653 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9654 "Base address (%s)",
9655 list_head->ll_symbol);
9658 /* Only one entry with the same base address. Use
9659 DW_LLE_start_length with absolute address and uleb128
9660 length. */
9661 if (last_section == NULL)
9663 dw2_asm_output_data (1, DW_LLE_start_length,
9664 "DW_LLE_start_length (%s)",
9665 list_head->ll_symbol);
9666 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9667 "Location list begin address (%s)",
9668 list_head->ll_symbol);
9669 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9670 "Location list length "
9671 "(%s)", list_head->ll_symbol);
9673 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9674 DW_LLE_base_address. */
9675 else
9677 dw2_asm_output_data (1, DW_LLE_offset_pair,
9678 "DW_LLE_offset_pair (%s)",
9679 list_head->ll_symbol);
9680 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9681 "Location list begin address "
9682 "(%s)", list_head->ll_symbol);
9683 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9684 "Location list end address "
9685 "(%s)", list_head->ll_symbol);
9688 /* The assembler does not support .uleb128 directive. Emit
9689 DW_LLE_start_end with a pair of absolute addresses. */
9690 else
9692 dw2_asm_output_data (1, DW_LLE_start_end,
9693 "DW_LLE_start_end (%s)",
9694 list_head->ll_symbol);
9695 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9696 "Location list begin address (%s)",
9697 list_head->ll_symbol);
9698 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9699 "Location list end address (%s)",
9700 list_head->ll_symbol);
9703 else if (dwarf_split_debug_info)
9705 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9706 and 4 byte length. */
9707 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9708 "Location list start/length entry (%s)",
9709 list_head->ll_symbol);
9710 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9711 "Location list range start index (%s)",
9712 curr->begin);
9713 /* The length field is 4 bytes. If we ever need to support
9714 an 8-byte length, we can add a new DW_LLE code or fall back
9715 to DW_LLE_GNU_start_end_entry. */
9716 dw2_asm_output_delta (4, curr->end, curr->begin,
9717 "Location list range length (%s)",
9718 list_head->ll_symbol);
9720 else if (!have_multiple_function_sections)
9722 /* Pair of relative addresses against start of text section. */
9723 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9724 "Location list begin address (%s)",
9725 list_head->ll_symbol);
9726 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9727 "Location list end address (%s)",
9728 list_head->ll_symbol);
9730 else
9732 /* Pair of absolute addresses. */
9733 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9734 "Location list begin address (%s)",
9735 list_head->ll_symbol);
9736 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9737 "Location list end address (%s)",
9738 list_head->ll_symbol);
9741 /* Output the block length for this list of location operations. */
9742 gcc_assert (size <= 0xffff);
9743 dw2_asm_output_data (2, size, "%s", "Location expression size");
9745 output_loc_sequence (curr->expr, -1);
9748 /* And finally list termination. */
9749 if (dwarf_version >= 5)
9750 dw2_asm_output_data (1, DW_LLE_end_of_list,
9751 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9752 else if (dwarf_split_debug_info)
9753 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9754 "Location list terminator (%s)",
9755 list_head->ll_symbol);
9756 else
9758 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9759 "Location list terminator begin (%s)",
9760 list_head->ll_symbol);
9761 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9762 "Location list terminator end (%s)",
9763 list_head->ll_symbol);
9767 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9768 section. Emit a relocated reference if val_entry is NULL, otherwise,
9769 emit an indirect reference. */
9771 static void
9772 output_range_list_offset (dw_attr_node *a)
9774 const char *name = dwarf_attr_name (a->dw_attr);
9776 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9778 if (dwarf_version >= 5)
9780 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9781 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9782 debug_ranges_section, "%s", name);
9784 else
9786 char *p = strchr (ranges_section_label, '\0');
9787 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9788 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9789 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9790 debug_ranges_section, "%s", name);
9791 *p = '\0';
9794 else if (dwarf_version >= 5)
9796 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9797 gcc_assert (rnglist_idx);
9798 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9800 else
9801 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9802 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9803 "%s (offset from %s)", name, ranges_section_label);
9806 /* Output the offset into the debug_loc section. */
9808 static void
9809 output_loc_list_offset (dw_attr_node *a)
9811 char *sym = AT_loc_list (a)->ll_symbol;
9813 gcc_assert (sym);
9814 if (!dwarf_split_debug_info)
9815 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9816 "%s", dwarf_attr_name (a->dw_attr));
9817 else if (dwarf_version >= 5)
9819 gcc_assert (AT_loc_list (a)->num_assigned);
9820 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9821 dwarf_attr_name (a->dw_attr),
9822 sym);
9824 else
9825 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9826 "%s", dwarf_attr_name (a->dw_attr));
9829 /* Output an attribute's index or value appropriately. */
9831 static void
9832 output_attr_index_or_value (dw_attr_node *a)
9834 const char *name = dwarf_attr_name (a->dw_attr);
9836 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9838 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9839 return;
9841 switch (AT_class (a))
9843 case dw_val_class_addr:
9844 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9845 break;
9846 case dw_val_class_high_pc:
9847 case dw_val_class_lbl_id:
9848 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9849 break;
9850 default:
9851 gcc_unreachable ();
9855 /* Output a type signature. */
9857 static inline void
9858 output_signature (const char *sig, const char *name)
9860 int i;
9862 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9863 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9866 /* Output a discriminant value. */
9868 static inline void
9869 output_discr_value (dw_discr_value *discr_value, const char *name)
9871 if (discr_value->pos)
9872 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9873 else
9874 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9877 /* Output the DIE and its attributes. Called recursively to generate
9878 the definitions of each child DIE. */
9880 static void
9881 output_die (dw_die_ref die)
9883 dw_attr_node *a;
9884 dw_die_ref c;
9885 unsigned long size;
9886 unsigned ix;
9888 /* If someone in another CU might refer to us, set up a symbol for
9889 them to point to. */
9890 if (! die->comdat_type_p && die->die_id.die_symbol)
9891 output_die_symbol (die);
9893 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9894 (unsigned long)die->die_offset,
9895 dwarf_tag_name (die->die_tag));
9897 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9899 const char *name = dwarf_attr_name (a->dw_attr);
9901 switch (AT_class (a))
9903 case dw_val_class_addr:
9904 output_attr_index_or_value (a);
9905 break;
9907 case dw_val_class_offset:
9908 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9909 "%s", name);
9910 break;
9912 case dw_val_class_range_list:
9913 output_range_list_offset (a);
9914 break;
9916 case dw_val_class_loc:
9917 size = size_of_locs (AT_loc (a));
9919 /* Output the block length for this list of location operations. */
9920 if (dwarf_version >= 4)
9921 dw2_asm_output_data_uleb128 (size, "%s", name);
9922 else
9923 dw2_asm_output_data (constant_size (size), size, "%s", name);
9925 output_loc_sequence (AT_loc (a), -1);
9926 break;
9928 case dw_val_class_const:
9929 /* ??? It would be slightly more efficient to use a scheme like is
9930 used for unsigned constants below, but gdb 4.x does not sign
9931 extend. Gdb 5.x does sign extend. */
9932 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9933 break;
9935 case dw_val_class_unsigned_const:
9937 int csize = constant_size (AT_unsigned (a));
9938 if (dwarf_version == 3
9939 && a->dw_attr == DW_AT_data_member_location
9940 && csize >= 4)
9941 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9942 else
9943 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9945 break;
9947 case dw_val_class_const_implicit:
9948 if (flag_debug_asm)
9949 fprintf (asm_out_file, "\t\t\t%s %s ("
9950 HOST_WIDE_INT_PRINT_DEC ")\n",
9951 ASM_COMMENT_START, name, AT_int (a));
9952 break;
9954 case dw_val_class_unsigned_const_implicit:
9955 if (flag_debug_asm)
9956 fprintf (asm_out_file, "\t\t\t%s %s ("
9957 HOST_WIDE_INT_PRINT_HEX ")\n",
9958 ASM_COMMENT_START, name, AT_unsigned (a));
9959 break;
9961 case dw_val_class_const_double:
9963 unsigned HOST_WIDE_INT first, second;
9965 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9966 dw2_asm_output_data (1,
9967 HOST_BITS_PER_DOUBLE_INT
9968 / HOST_BITS_PER_CHAR,
9969 NULL);
9971 if (WORDS_BIG_ENDIAN)
9973 first = a->dw_attr_val.v.val_double.high;
9974 second = a->dw_attr_val.v.val_double.low;
9976 else
9978 first = a->dw_attr_val.v.val_double.low;
9979 second = a->dw_attr_val.v.val_double.high;
9982 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9983 first, "%s", name);
9984 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9985 second, NULL);
9987 break;
9989 case dw_val_class_wide_int:
9991 int i;
9992 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9993 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9994 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9995 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
9996 * l, NULL);
9998 if (WORDS_BIG_ENDIAN)
9999 for (i = len - 1; i >= 0; --i)
10001 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10002 "%s", name);
10003 name = "";
10005 else
10006 for (i = 0; i < len; ++i)
10008 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10009 "%s", name);
10010 name = "";
10013 break;
10015 case dw_val_class_vec:
10017 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10018 unsigned int len = a->dw_attr_val.v.val_vec.length;
10019 unsigned int i;
10020 unsigned char *p;
10022 dw2_asm_output_data (constant_size (len * elt_size),
10023 len * elt_size, "%s", name);
10024 if (elt_size > sizeof (HOST_WIDE_INT))
10026 elt_size /= 2;
10027 len *= 2;
10029 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10030 i < len;
10031 i++, p += elt_size)
10032 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10033 "fp or vector constant word %u", i);
10034 break;
10037 case dw_val_class_flag:
10038 if (dwarf_version >= 4)
10040 /* Currently all add_AT_flag calls pass in 1 as last argument,
10041 so DW_FORM_flag_present can be used. If that ever changes,
10042 we'll need to use DW_FORM_flag and have some optimization
10043 in build_abbrev_table that will change those to
10044 DW_FORM_flag_present if it is set to 1 in all DIEs using
10045 the same abbrev entry. */
10046 gcc_assert (AT_flag (a) == 1);
10047 if (flag_debug_asm)
10048 fprintf (asm_out_file, "\t\t\t%s %s\n",
10049 ASM_COMMENT_START, name);
10050 break;
10052 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10053 break;
10055 case dw_val_class_loc_list:
10056 output_loc_list_offset (a);
10057 break;
10059 case dw_val_class_die_ref:
10060 if (AT_ref_external (a))
10062 if (AT_ref (a)->comdat_type_p)
10064 comdat_type_node *type_node
10065 = AT_ref (a)->die_id.die_type_node;
10067 gcc_assert (type_node);
10068 output_signature (type_node->signature, name);
10070 else
10072 const char *sym = AT_ref (a)->die_id.die_symbol;
10073 int size;
10075 gcc_assert (sym);
10076 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10077 length, whereas in DWARF3 it's always sized as an
10078 offset. */
10079 if (dwarf_version == 2)
10080 size = DWARF2_ADDR_SIZE;
10081 else
10082 size = DWARF_OFFSET_SIZE;
10083 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10084 name);
10087 else
10089 gcc_assert (AT_ref (a)->die_offset);
10090 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10091 "%s", name);
10093 break;
10095 case dw_val_class_fde_ref:
10097 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10099 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10100 a->dw_attr_val.v.val_fde_index * 2);
10101 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10102 "%s", name);
10104 break;
10106 case dw_val_class_vms_delta:
10107 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10108 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10109 AT_vms_delta2 (a), AT_vms_delta1 (a),
10110 "%s", name);
10111 #else
10112 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10113 AT_vms_delta2 (a), AT_vms_delta1 (a),
10114 "%s", name);
10115 #endif
10116 break;
10118 case dw_val_class_lbl_id:
10119 output_attr_index_or_value (a);
10120 break;
10122 case dw_val_class_lineptr:
10123 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10124 debug_line_section, "%s", name);
10125 break;
10127 case dw_val_class_macptr:
10128 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10129 debug_macinfo_section, "%s", name);
10130 break;
10132 case dw_val_class_loclistsptr:
10133 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10134 debug_loc_section, "%s", name);
10135 break;
10137 case dw_val_class_str:
10138 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10139 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10140 a->dw_attr_val.v.val_str->label,
10141 debug_str_section,
10142 "%s: \"%s\"", name, AT_string (a));
10143 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10144 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10145 a->dw_attr_val.v.val_str->label,
10146 debug_line_str_section,
10147 "%s: \"%s\"", name, AT_string (a));
10148 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10149 dw2_asm_output_data_uleb128 (AT_index (a),
10150 "%s: \"%s\"", name, AT_string (a));
10151 else
10152 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10153 break;
10155 case dw_val_class_file:
10157 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10159 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10160 a->dw_attr_val.v.val_file->filename);
10161 break;
10164 case dw_val_class_file_implicit:
10165 if (flag_debug_asm)
10166 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10167 ASM_COMMENT_START, name,
10168 maybe_emit_file (a->dw_attr_val.v.val_file),
10169 a->dw_attr_val.v.val_file->filename);
10170 break;
10172 case dw_val_class_data8:
10174 int i;
10176 for (i = 0; i < 8; i++)
10177 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10178 i == 0 ? "%s" : NULL, name);
10179 break;
10182 case dw_val_class_high_pc:
10183 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10184 get_AT_low_pc (die), "DW_AT_high_pc");
10185 break;
10187 case dw_val_class_discr_value:
10188 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10189 break;
10191 case dw_val_class_discr_list:
10193 dw_discr_list_ref list = AT_discr_list (a);
10194 const int size = size_of_discr_list (list);
10196 /* This is a block, so output its length first. */
10197 dw2_asm_output_data (constant_size (size), size,
10198 "%s: block size", name);
10200 for (; list != NULL; list = list->dw_discr_next)
10202 /* One byte for the discriminant value descriptor, and then as
10203 many LEB128 numbers as required. */
10204 if (list->dw_discr_range)
10205 dw2_asm_output_data (1, DW_DSC_range,
10206 "%s: DW_DSC_range", name);
10207 else
10208 dw2_asm_output_data (1, DW_DSC_label,
10209 "%s: DW_DSC_label", name);
10211 output_discr_value (&list->dw_discr_lower_bound, name);
10212 if (list->dw_discr_range)
10213 output_discr_value (&list->dw_discr_upper_bound, name);
10215 break;
10218 default:
10219 gcc_unreachable ();
10223 FOR_EACH_CHILD (die, c, output_die (c));
10225 /* Add null byte to terminate sibling list. */
10226 if (die->die_child != NULL)
10227 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10228 (unsigned long) die->die_offset);
10231 /* Output the compilation unit that appears at the beginning of the
10232 .debug_info section, and precedes the DIE descriptions. */
10234 static void
10235 output_compilation_unit_header (enum dwarf_unit_type ut)
10237 if (!XCOFF_DEBUGGING_INFO)
10239 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10240 dw2_asm_output_data (4, 0xffffffff,
10241 "Initial length escape value indicating 64-bit DWARF extension");
10242 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10243 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10244 "Length of Compilation Unit Info");
10247 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10248 if (dwarf_version >= 5)
10250 const char *name;
10251 switch (ut)
10253 case DW_UT_compile: name = "DW_UT_compile"; break;
10254 case DW_UT_type: name = "DW_UT_type"; break;
10255 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10256 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10257 default: gcc_unreachable ();
10259 dw2_asm_output_data (1, ut, name);
10260 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10262 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10263 debug_abbrev_section,
10264 "Offset Into Abbrev. Section");
10265 if (dwarf_version < 5)
10266 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10269 /* Output the compilation unit DIE and its children. */
10271 static void
10272 output_comp_unit (dw_die_ref die, int output_if_empty,
10273 const unsigned char *dwo_id)
10275 const char *secname, *oldsym;
10276 char *tmp;
10278 /* Unless we are outputting main CU, we may throw away empty ones. */
10279 if (!output_if_empty && die->die_child == NULL)
10280 return;
10282 /* Even if there are no children of this DIE, we must output the information
10283 about the compilation unit. Otherwise, on an empty translation unit, we
10284 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10285 will then complain when examining the file. First mark all the DIEs in
10286 this CU so we know which get local refs. */
10287 mark_dies (die);
10289 external_ref_hash_type *extern_map = optimize_external_refs (die);
10291 /* For now, optimize only the main CU, in order to optimize the rest
10292 we'd need to see all of them earlier. Leave the rest for post-linking
10293 tools like DWZ. */
10294 if (die == comp_unit_die ())
10295 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10297 build_abbrev_table (die, extern_map);
10299 optimize_abbrev_table ();
10301 delete extern_map;
10303 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10304 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
10305 calc_die_sizes (die);
10307 oldsym = die->die_id.die_symbol;
10308 if (oldsym)
10310 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10312 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10313 secname = tmp;
10314 die->die_id.die_symbol = NULL;
10315 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10317 else
10319 switch_to_section (debug_info_section);
10320 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10321 info_section_emitted = true;
10324 /* Output debugging information. */
10325 output_compilation_unit_header (dwo_id
10326 ? DW_UT_split_compile : DW_UT_compile);
10327 if (dwarf_version >= 5)
10329 if (dwo_id != NULL)
10330 for (int i = 0; i < 8; i++)
10331 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10332 else
10333 /* Hope all the padding will be removed for DWARF 5 final for
10334 DW_AT_compile and DW_AT_partial. */
10335 dw2_asm_output_data (8, 0, "Padding 1");
10337 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Padding 2");
10339 output_die (die);
10341 /* Leave the marks on the main CU, so we can check them in
10342 output_pubnames. */
10343 if (oldsym)
10345 unmark_dies (die);
10346 die->die_id.die_symbol = oldsym;
10350 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10351 and .debug_pubtypes. This is configured per-target, but can be
10352 overridden by the -gpubnames or -gno-pubnames options. */
10354 static inline bool
10355 want_pubnames (void)
10357 if (debug_info_level <= DINFO_LEVEL_TERSE)
10358 return false;
10359 if (debug_generate_pub_sections != -1)
10360 return debug_generate_pub_sections;
10361 return targetm.want_debug_pub_sections;
10364 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10366 static void
10367 add_AT_pubnames (dw_die_ref die)
10369 if (want_pubnames ())
10370 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10373 /* Add a string attribute value to a skeleton DIE. */
10375 static inline void
10376 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10377 const char *str)
10379 dw_attr_node attr;
10380 struct indirect_string_node *node;
10382 if (! skeleton_debug_str_hash)
10383 skeleton_debug_str_hash
10384 = hash_table<indirect_string_hasher>::create_ggc (10);
10386 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10387 find_string_form (node);
10388 if (node->form == DW_FORM_GNU_str_index)
10389 node->form = DW_FORM_strp;
10391 attr.dw_attr = attr_kind;
10392 attr.dw_attr_val.val_class = dw_val_class_str;
10393 attr.dw_attr_val.val_entry = NULL;
10394 attr.dw_attr_val.v.val_str = node;
10395 add_dwarf_attr (die, &attr);
10398 /* Helper function to generate top-level dies for skeleton debug_info and
10399 debug_types. */
10401 static void
10402 add_top_level_skeleton_die_attrs (dw_die_ref die)
10404 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10405 const char *comp_dir = comp_dir_string ();
10407 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10408 if (comp_dir != NULL)
10409 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10410 add_AT_pubnames (die);
10411 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10414 /* Output skeleton debug sections that point to the dwo file. */
10416 static void
10417 output_skeleton_debug_sections (dw_die_ref comp_unit,
10418 const unsigned char *dwo_id)
10420 /* These attributes will be found in the full debug_info section. */
10421 remove_AT (comp_unit, DW_AT_producer);
10422 remove_AT (comp_unit, DW_AT_language);
10424 switch_to_section (debug_skeleton_info_section);
10425 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10427 /* Produce the skeleton compilation-unit header. This one differs enough from
10428 a normal CU header that it's better not to call output_compilation_unit
10429 header. */
10430 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10431 dw2_asm_output_data (4, 0xffffffff,
10432 "Initial length escape value indicating 64-bit DWARF extension");
10434 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10435 DWARF_COMPILE_UNIT_HEADER_SIZE
10436 - DWARF_INITIAL_LENGTH_SIZE
10437 + size_of_die (comp_unit),
10438 "Length of Compilation Unit Info");
10439 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10440 if (dwarf_version >= 5)
10442 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10443 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10445 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10446 debug_skeleton_abbrev_section,
10447 "Offset Into Abbrev. Section");
10448 if (dwarf_version < 5)
10449 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10450 else
10452 for (int i = 0; i < 8; i++)
10453 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10455 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Padding 2");
10458 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10459 output_die (comp_unit);
10461 /* Build the skeleton debug_abbrev section. */
10462 switch_to_section (debug_skeleton_abbrev_section);
10463 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10465 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10467 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10470 /* Output a comdat type unit DIE and its children. */
10472 static void
10473 output_comdat_type_unit (comdat_type_node *node)
10475 const char *secname;
10476 char *tmp;
10477 int i;
10478 #if defined (OBJECT_FORMAT_ELF)
10479 tree comdat_key;
10480 #endif
10482 /* First mark all the DIEs in this CU so we know which get local refs. */
10483 mark_dies (node->root_die);
10485 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10487 build_abbrev_table (node->root_die, extern_map);
10489 delete extern_map;
10490 extern_map = NULL;
10492 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10493 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10494 calc_die_sizes (node->root_die);
10496 #if defined (OBJECT_FORMAT_ELF)
10497 if (dwarf_version >= 5)
10499 if (!dwarf_split_debug_info)
10500 secname = ".debug_info";
10501 else
10502 secname = ".debug_info.dwo";
10504 else if (!dwarf_split_debug_info)
10505 secname = ".debug_types";
10506 else
10507 secname = ".debug_types.dwo";
10509 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10510 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10511 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10512 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10513 comdat_key = get_identifier (tmp);
10514 targetm.asm_out.named_section (secname,
10515 SECTION_DEBUG | SECTION_LINKONCE,
10516 comdat_key);
10517 #else
10518 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10519 sprintf (tmp, (dwarf_version >= 5
10520 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10521 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10522 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10523 secname = tmp;
10524 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10525 #endif
10527 /* Output debugging information. */
10528 output_compilation_unit_header (dwarf_split_debug_info
10529 ? DW_UT_split_type : DW_UT_type);
10530 output_signature (node->signature, "Type Signature");
10531 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10532 "Offset to Type DIE");
10533 output_die (node->root_die);
10535 unmark_dies (node->root_die);
10538 /* Return the DWARF2/3 pubname associated with a decl. */
10540 static const char *
10541 dwarf2_name (tree decl, int scope)
10543 if (DECL_NAMELESS (decl))
10544 return NULL;
10545 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10548 /* Add a new entry to .debug_pubnames if appropriate. */
10550 static void
10551 add_pubname_string (const char *str, dw_die_ref die)
10553 pubname_entry e;
10555 e.die = die;
10556 e.name = xstrdup (str);
10557 vec_safe_push (pubname_table, e);
10560 static void
10561 add_pubname (tree decl, dw_die_ref die)
10563 if (!want_pubnames ())
10564 return;
10566 /* Don't add items to the table when we expect that the consumer will have
10567 just read the enclosing die. For example, if the consumer is looking at a
10568 class_member, it will either be inside the class already, or will have just
10569 looked up the class to find the member. Either way, searching the class is
10570 faster than searching the index. */
10571 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10572 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10574 const char *name = dwarf2_name (decl, 1);
10576 if (name)
10577 add_pubname_string (name, die);
10581 /* Add an enumerator to the pubnames section. */
10583 static void
10584 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10586 pubname_entry e;
10588 gcc_assert (scope_name);
10589 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10590 e.die = die;
10591 vec_safe_push (pubname_table, e);
10594 /* Add a new entry to .debug_pubtypes if appropriate. */
10596 static void
10597 add_pubtype (tree decl, dw_die_ref die)
10599 pubname_entry e;
10601 if (!want_pubnames ())
10602 return;
10604 if ((TREE_PUBLIC (decl)
10605 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10606 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10608 tree scope = NULL;
10609 const char *scope_name = "";
10610 const char *sep = is_cxx () ? "::" : ".";
10611 const char *name;
10613 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10614 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10616 scope_name = lang_hooks.dwarf_name (scope, 1);
10617 if (scope_name != NULL && scope_name[0] != '\0')
10618 scope_name = concat (scope_name, sep, NULL);
10619 else
10620 scope_name = "";
10623 if (TYPE_P (decl))
10624 name = type_tag (decl);
10625 else
10626 name = lang_hooks.dwarf_name (decl, 1);
10628 /* If we don't have a name for the type, there's no point in adding
10629 it to the table. */
10630 if (name != NULL && name[0] != '\0')
10632 e.die = die;
10633 e.name = concat (scope_name, name, NULL);
10634 vec_safe_push (pubtype_table, e);
10637 /* Although it might be more consistent to add the pubinfo for the
10638 enumerators as their dies are created, they should only be added if the
10639 enum type meets the criteria above. So rather than re-check the parent
10640 enum type whenever an enumerator die is created, just output them all
10641 here. This isn't protected by the name conditional because anonymous
10642 enums don't have names. */
10643 if (die->die_tag == DW_TAG_enumeration_type)
10645 dw_die_ref c;
10647 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10652 /* Output a single entry in the pubnames table. */
10654 static void
10655 output_pubname (dw_offset die_offset, pubname_entry *entry)
10657 dw_die_ref die = entry->die;
10658 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10660 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10662 if (debug_generate_pub_sections == 2)
10664 /* This logic follows gdb's method for determining the value of the flag
10665 byte. */
10666 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10667 switch (die->die_tag)
10669 case DW_TAG_typedef:
10670 case DW_TAG_base_type:
10671 case DW_TAG_subrange_type:
10672 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10673 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10674 break;
10675 case DW_TAG_enumerator:
10676 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10677 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10678 if (!is_cxx () && !is_java ())
10679 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10680 break;
10681 case DW_TAG_subprogram:
10682 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10683 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10684 if (!is_ada ())
10685 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10686 break;
10687 case DW_TAG_constant:
10688 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10689 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10690 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10691 break;
10692 case DW_TAG_variable:
10693 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10694 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10695 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10696 break;
10697 case DW_TAG_namespace:
10698 case DW_TAG_imported_declaration:
10699 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10700 break;
10701 case DW_TAG_class_type:
10702 case DW_TAG_interface_type:
10703 case DW_TAG_structure_type:
10704 case DW_TAG_union_type:
10705 case DW_TAG_enumeration_type:
10706 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10707 if (!is_cxx () && !is_java ())
10708 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10709 break;
10710 default:
10711 /* An unusual tag. Leave the flag-byte empty. */
10712 break;
10714 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10715 "GDB-index flags");
10718 dw2_asm_output_nstring (entry->name, -1, "external name");
10722 /* Output the public names table used to speed up access to externally
10723 visible names; or the public types table used to find type definitions. */
10725 static void
10726 output_pubnames (vec<pubname_entry, va_gc> *names)
10728 unsigned i;
10729 unsigned long pubnames_length = size_of_pubnames (names);
10730 pubname_entry *pub;
10732 if (!XCOFF_DEBUGGING_INFO)
10734 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10735 dw2_asm_output_data (4, 0xffffffff,
10736 "Initial length escape value indicating 64-bit DWARF extension");
10737 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10738 "Pub Info Length");
10741 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10742 dw2_asm_output_data (2, 2, "DWARF Version");
10744 if (dwarf_split_debug_info)
10745 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10746 debug_skeleton_info_section,
10747 "Offset of Compilation Unit Info");
10748 else
10749 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10750 debug_info_section,
10751 "Offset of Compilation Unit Info");
10752 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10753 "Compilation Unit Length");
10755 FOR_EACH_VEC_ELT (*names, i, pub)
10757 if (include_pubname_in_output (names, pub))
10759 dw_offset die_offset = pub->die->die_offset;
10761 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10762 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10763 gcc_assert (pub->die->die_mark);
10765 /* If we're putting types in their own .debug_types sections,
10766 the .debug_pubtypes table will still point to the compile
10767 unit (not the type unit), so we want to use the offset of
10768 the skeleton DIE (if there is one). */
10769 if (pub->die->comdat_type_p && names == pubtype_table)
10771 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10773 if (type_node != NULL)
10774 die_offset = (type_node->skeleton_die != NULL
10775 ? type_node->skeleton_die->die_offset
10776 : comp_unit_die ()->die_offset);
10779 output_pubname (die_offset, pub);
10783 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10786 /* Output public names and types tables if necessary. */
10788 static void
10789 output_pubtables (void)
10791 if (!want_pubnames () || !info_section_emitted)
10792 return;
10794 switch_to_section (debug_pubnames_section);
10795 output_pubnames (pubname_table);
10796 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10797 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10798 simply won't look for the section. */
10799 switch_to_section (debug_pubtypes_section);
10800 output_pubnames (pubtype_table);
10804 /* Output the information that goes into the .debug_aranges table.
10805 Namely, define the beginning and ending address range of the
10806 text section generated for this compilation unit. */
10808 static void
10809 output_aranges (void)
10811 unsigned i;
10812 unsigned long aranges_length = size_of_aranges ();
10814 if (!XCOFF_DEBUGGING_INFO)
10816 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10817 dw2_asm_output_data (4, 0xffffffff,
10818 "Initial length escape value indicating 64-bit DWARF extension");
10819 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10820 "Length of Address Ranges Info");
10823 /* Version number for aranges is still 2, even up to DWARF5. */
10824 dw2_asm_output_data (2, 2, "DWARF Version");
10825 if (dwarf_split_debug_info)
10826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10827 debug_skeleton_info_section,
10828 "Offset of Compilation Unit Info");
10829 else
10830 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10831 debug_info_section,
10832 "Offset of Compilation Unit Info");
10833 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10834 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10836 /* We need to align to twice the pointer size here. */
10837 if (DWARF_ARANGES_PAD_SIZE)
10839 /* Pad using a 2 byte words so that padding is correct for any
10840 pointer size. */
10841 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10842 2 * DWARF2_ADDR_SIZE);
10843 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10844 dw2_asm_output_data (2, 0, NULL);
10847 /* It is necessary not to output these entries if the sections were
10848 not used; if the sections were not used, the length will be 0 and
10849 the address may end up as 0 if the section is discarded by ld
10850 --gc-sections, leaving an invalid (0, 0) entry that can be
10851 confused with the terminator. */
10852 if (text_section_used)
10854 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10855 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10856 text_section_label, "Length");
10858 if (cold_text_section_used)
10860 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10861 "Address");
10862 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10863 cold_text_section_label, "Length");
10866 if (have_multiple_function_sections)
10868 unsigned fde_idx;
10869 dw_fde_ref fde;
10871 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10873 if (DECL_IGNORED_P (fde->decl))
10874 continue;
10875 if (!fde->in_std_section)
10877 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10878 "Address");
10879 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10880 fde->dw_fde_begin, "Length");
10882 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10884 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10885 "Address");
10886 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10887 fde->dw_fde_second_begin, "Length");
10892 /* Output the terminator words. */
10893 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10894 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10897 /* Add a new entry to .debug_ranges. Return its index into
10898 ranges_table vector. */
10900 static unsigned int
10901 add_ranges_num (int num, bool maybe_new_sec)
10903 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10904 vec_safe_push (ranges_table, r);
10905 return vec_safe_length (ranges_table) - 1;
10908 /* Add a new entry to .debug_ranges corresponding to a block, or a
10909 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10910 this entry might be in a different section from previous range. */
10912 static unsigned int
10913 add_ranges (const_tree block, bool maybe_new_sec)
10915 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10918 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10919 chain, or middle entry of a chain that will be directly referred to. */
10921 static void
10922 note_rnglist_head (unsigned int offset)
10924 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10925 return;
10926 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
10929 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10930 When using dwarf_split_debug_info, address attributes in dies destined
10931 for the final executable should be direct references--setting the
10932 parameter force_direct ensures this behavior. */
10934 static void
10935 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10936 bool *added, bool force_direct)
10938 unsigned int in_use = vec_safe_length (ranges_by_label);
10939 unsigned int offset;
10940 dw_ranges_by_label rbl = { begin, end };
10941 vec_safe_push (ranges_by_label, rbl);
10942 offset = add_ranges_num (-(int)in_use - 1, true);
10943 if (!*added)
10945 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10946 *added = true;
10947 note_rnglist_head (offset);
10951 /* Emit .debug_ranges section. */
10953 static void
10954 output_ranges (void)
10956 unsigned i;
10957 static const char *const start_fmt = "Offset %#x";
10958 const char *fmt = start_fmt;
10959 dw_ranges *r;
10961 switch_to_section (debug_ranges_section);
10962 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
10963 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
10965 int block_num = r->num;
10967 if (block_num > 0)
10969 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10970 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10972 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10973 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10975 /* If all code is in the text section, then the compilation
10976 unit base address defaults to DW_AT_low_pc, which is the
10977 base of the text section. */
10978 if (!have_multiple_function_sections)
10980 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10981 text_section_label,
10982 fmt, i * 2 * DWARF2_ADDR_SIZE);
10983 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10984 text_section_label, NULL);
10987 /* Otherwise, the compilation unit base address is zero,
10988 which allows us to use absolute addresses, and not worry
10989 about whether the target supports cross-section
10990 arithmetic. */
10991 else
10993 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10994 fmt, i * 2 * DWARF2_ADDR_SIZE);
10995 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10998 fmt = NULL;
11001 /* Negative block_num stands for an index into ranges_by_label. */
11002 else if (block_num < 0)
11004 int lab_idx = - block_num - 1;
11006 if (!have_multiple_function_sections)
11008 gcc_unreachable ();
11009 #if 0
11010 /* If we ever use add_ranges_by_labels () for a single
11011 function section, all we have to do is to take out
11012 the #if 0 above. */
11013 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11014 (*ranges_by_label)[lab_idx].begin,
11015 text_section_label,
11016 fmt, i * 2 * DWARF2_ADDR_SIZE);
11017 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11018 (*ranges_by_label)[lab_idx].end,
11019 text_section_label, NULL);
11020 #endif
11022 else
11024 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11025 (*ranges_by_label)[lab_idx].begin,
11026 fmt, i * 2 * DWARF2_ADDR_SIZE);
11027 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11028 (*ranges_by_label)[lab_idx].end,
11029 NULL);
11032 else
11034 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11035 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11036 fmt = start_fmt;
11041 /* Non-zero if .debug_line_str should be used for .debug_line section
11042 strings or strings that are likely shareable with those. */
11043 #define DWARF5_USE_DEBUG_LINE_STR \
11044 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11045 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11046 /* FIXME: there is no .debug_line_str.dwo section, \
11047 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11048 && !dwarf_split_debug_info)
11050 /* Assign .debug_rnglists indexes. */
11052 static void
11053 index_rnglists (void)
11055 unsigned i;
11056 dw_ranges *r;
11058 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11059 if (r->label)
11060 r->idx = rnglist_idx++;
11063 /* Emit .debug_rnglists section. */
11065 static void
11066 output_rnglists (void)
11068 unsigned i;
11069 dw_ranges *r;
11070 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11071 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11072 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11074 switch_to_section (debug_ranges_section);
11075 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11076 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11077 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11078 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11079 dw2_asm_output_data (4, 0xffffffff,
11080 "Initial length escape value indicating "
11081 "64-bit DWARF extension");
11082 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11083 "Length of Range Lists");
11084 ASM_OUTPUT_LABEL (asm_out_file, l1);
11085 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11086 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11087 dw2_asm_output_data (1, 0, "Segment Size");
11088 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11089 about relocation sizes and primarily care about the size of .debug*
11090 sections in linked shared libraries and executables, then
11091 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11092 into it are usually larger than just DW_FORM_sec_offset offsets
11093 into the .debug_rnglists section. */
11094 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11095 "Offset Entry Count");
11096 if (dwarf_split_debug_info)
11098 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11099 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11100 if (r->label)
11101 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11102 ranges_base_label, NULL);
11105 const char *lab = "";
11106 unsigned int len = vec_safe_length (ranges_table);
11107 const char *base = NULL;
11108 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11110 int block_num = r->num;
11112 if (r->label)
11114 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11115 lab = r->label;
11117 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11118 base = NULL;
11119 if (block_num > 0)
11121 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11122 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11124 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11125 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11127 if (HAVE_AS_LEB128)
11129 /* If all code is in the text section, then the compilation
11130 unit base address defaults to DW_AT_low_pc, which is the
11131 base of the text section. */
11132 if (!have_multiple_function_sections)
11134 dw2_asm_output_data (1, DW_RLE_offset_pair,
11135 "DW_RLE_offset_pair (%s)", lab);
11136 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11137 "Range begin address (%s)", lab);
11138 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11139 "Range end address (%s)", lab);
11140 continue;
11142 if (base == NULL)
11144 dw_ranges *r2 = NULL;
11145 if (i < len - 1)
11146 r2 = &(*ranges_table)[i + 1];
11147 if (r2
11148 && r2->num != 0
11149 && r2->label == NULL
11150 && !r2->maybe_new_sec)
11152 dw2_asm_output_data (1, DW_RLE_base_address,
11153 "DW_RLE_base_address (%s)", lab);
11154 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11155 "Base address (%s)", lab);
11156 strcpy (basebuf, blabel);
11157 base = basebuf;
11160 if (base)
11162 dw2_asm_output_data (1, DW_RLE_offset_pair,
11163 "DW_RLE_offset_pair (%s)", lab);
11164 dw2_asm_output_delta_uleb128 (blabel, base,
11165 "Range begin address (%s)", lab);
11166 dw2_asm_output_delta_uleb128 (elabel, base,
11167 "Range end address (%s)", lab);
11168 continue;
11170 dw2_asm_output_data (1, DW_RLE_start_length,
11171 "DW_RLE_start_length (%s)", lab);
11172 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11173 "Range begin address (%s)", lab);
11174 dw2_asm_output_delta_uleb128 (elabel, blabel,
11175 "Range length (%s)", lab);
11177 else
11179 dw2_asm_output_data (1, DW_RLE_start_end,
11180 "DW_RLE_start_end (%s)", lab);
11181 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11182 "Range begin address (%s)", lab);
11183 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11184 "Range end address (%s)", lab);
11188 /* Negative block_num stands for an index into ranges_by_label. */
11189 else if (block_num < 0)
11191 int lab_idx = - block_num - 1;
11192 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11193 const char *elabel = (*ranges_by_label)[lab_idx].end;
11195 if (!have_multiple_function_sections)
11196 gcc_unreachable ();
11197 if (HAVE_AS_LEB128)
11199 dw2_asm_output_data (1, DW_RLE_start_length,
11200 "DW_RLE_start_length (%s)", lab);
11201 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11202 "Range begin address (%s)", lab);
11203 dw2_asm_output_delta_uleb128 (elabel, blabel,
11204 "Range length (%s)", lab);
11206 else
11208 dw2_asm_output_data (1, DW_RLE_start_end,
11209 "DW_RLE_start_end (%s)", lab);
11210 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11211 "Range begin address (%s)", lab);
11212 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11213 "Range end address (%s)", lab);
11216 else
11217 dw2_asm_output_data (1, DW_RLE_end_of_list,
11218 "DW_RLE_end_of_list (%s)", lab);
11220 ASM_OUTPUT_LABEL (asm_out_file, l2);
11223 /* Data structure containing information about input files. */
11224 struct file_info
11226 const char *path; /* Complete file name. */
11227 const char *fname; /* File name part. */
11228 int length; /* Length of entire string. */
11229 struct dwarf_file_data * file_idx; /* Index in input file table. */
11230 int dir_idx; /* Index in directory table. */
11233 /* Data structure containing information about directories with source
11234 files. */
11235 struct dir_info
11237 const char *path; /* Path including directory name. */
11238 int length; /* Path length. */
11239 int prefix; /* Index of directory entry which is a prefix. */
11240 int count; /* Number of files in this directory. */
11241 int dir_idx; /* Index of directory used as base. */
11244 /* Callback function for file_info comparison. We sort by looking at
11245 the directories in the path. */
11247 static int
11248 file_info_cmp (const void *p1, const void *p2)
11250 const struct file_info *const s1 = (const struct file_info *) p1;
11251 const struct file_info *const s2 = (const struct file_info *) p2;
11252 const unsigned char *cp1;
11253 const unsigned char *cp2;
11255 /* Take care of file names without directories. We need to make sure that
11256 we return consistent values to qsort since some will get confused if
11257 we return the same value when identical operands are passed in opposite
11258 orders. So if neither has a directory, return 0 and otherwise return
11259 1 or -1 depending on which one has the directory. */
11260 if ((s1->path == s1->fname || s2->path == s2->fname))
11261 return (s2->path == s2->fname) - (s1->path == s1->fname);
11263 cp1 = (const unsigned char *) s1->path;
11264 cp2 = (const unsigned char *) s2->path;
11266 while (1)
11268 ++cp1;
11269 ++cp2;
11270 /* Reached the end of the first path? If so, handle like above. */
11271 if ((cp1 == (const unsigned char *) s1->fname)
11272 || (cp2 == (const unsigned char *) s2->fname))
11273 return ((cp2 == (const unsigned char *) s2->fname)
11274 - (cp1 == (const unsigned char *) s1->fname));
11276 /* Character of current path component the same? */
11277 else if (*cp1 != *cp2)
11278 return *cp1 - *cp2;
11282 struct file_name_acquire_data
11284 struct file_info *files;
11285 int used_files;
11286 int max_files;
11289 /* Traversal function for the hash table. */
11292 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11294 struct dwarf_file_data *d = *slot;
11295 struct file_info *fi;
11296 const char *f;
11298 gcc_assert (fnad->max_files >= d->emitted_number);
11300 if (! d->emitted_number)
11301 return 1;
11303 gcc_assert (fnad->max_files != fnad->used_files);
11305 fi = fnad->files + fnad->used_files++;
11307 /* Skip all leading "./". */
11308 f = d->filename;
11309 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11310 f += 2;
11312 /* Create a new array entry. */
11313 fi->path = f;
11314 fi->length = strlen (f);
11315 fi->file_idx = d;
11317 /* Search for the file name part. */
11318 f = strrchr (f, DIR_SEPARATOR);
11319 #if defined (DIR_SEPARATOR_2)
11321 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11323 if (g != NULL)
11325 if (f == NULL || f < g)
11326 f = g;
11329 #endif
11331 fi->fname = f == NULL ? fi->path : f + 1;
11332 return 1;
11335 /* Helper function for output_file_names. Emit a FORM encoded
11336 string STR, with assembly comment start ENTRY_KIND and
11337 index IDX */
11339 static void
11340 output_line_string (enum dwarf_form form, const char *str,
11341 const char *entry_kind, unsigned int idx)
11343 switch (form)
11345 case DW_FORM_string:
11346 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11347 break;
11348 case DW_FORM_line_strp:
11349 if (!debug_line_str_hash)
11350 debug_line_str_hash
11351 = hash_table<indirect_string_hasher>::create_ggc (10);
11353 struct indirect_string_node *node;
11354 node = find_AT_string_in_table (str, debug_line_str_hash);
11355 set_indirect_string (node);
11356 node->form = form;
11357 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11358 debug_line_str_section, "%s: %#x: \"%s\"",
11359 entry_kind, 0, node->str);
11360 break;
11361 default:
11362 gcc_unreachable ();
11366 /* Output the directory table and the file name table. We try to minimize
11367 the total amount of memory needed. A heuristic is used to avoid large
11368 slowdowns with many input files. */
11370 static void
11371 output_file_names (void)
11373 struct file_name_acquire_data fnad;
11374 int numfiles;
11375 struct file_info *files;
11376 struct dir_info *dirs;
11377 int *saved;
11378 int *savehere;
11379 int *backmap;
11380 int ndirs;
11381 int idx_offset;
11382 int i;
11384 if (!last_emitted_file)
11386 if (dwarf_version >= 5)
11388 dw2_asm_output_data (1, 0, "Directory entry format count");
11389 dw2_asm_output_data_uleb128 (0, "Directories count");
11390 dw2_asm_output_data (1, 0, "File name entry format count");
11391 dw2_asm_output_data_uleb128 (0, "File names count");
11393 else
11395 dw2_asm_output_data (1, 0, "End directory table");
11396 dw2_asm_output_data (1, 0, "End file name table");
11398 return;
11401 numfiles = last_emitted_file->emitted_number;
11403 /* Allocate the various arrays we need. */
11404 files = XALLOCAVEC (struct file_info, numfiles);
11405 dirs = XALLOCAVEC (struct dir_info, numfiles);
11407 fnad.files = files;
11408 fnad.used_files = 0;
11409 fnad.max_files = numfiles;
11410 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11411 gcc_assert (fnad.used_files == fnad.max_files);
11413 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11415 /* Find all the different directories used. */
11416 dirs[0].path = files[0].path;
11417 dirs[0].length = files[0].fname - files[0].path;
11418 dirs[0].prefix = -1;
11419 dirs[0].count = 1;
11420 dirs[0].dir_idx = 0;
11421 files[0].dir_idx = 0;
11422 ndirs = 1;
11424 for (i = 1; i < numfiles; i++)
11425 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11426 && memcmp (dirs[ndirs - 1].path, files[i].path,
11427 dirs[ndirs - 1].length) == 0)
11429 /* Same directory as last entry. */
11430 files[i].dir_idx = ndirs - 1;
11431 ++dirs[ndirs - 1].count;
11433 else
11435 int j;
11437 /* This is a new directory. */
11438 dirs[ndirs].path = files[i].path;
11439 dirs[ndirs].length = files[i].fname - files[i].path;
11440 dirs[ndirs].count = 1;
11441 dirs[ndirs].dir_idx = ndirs;
11442 files[i].dir_idx = ndirs;
11444 /* Search for a prefix. */
11445 dirs[ndirs].prefix = -1;
11446 for (j = 0; j < ndirs; j++)
11447 if (dirs[j].length < dirs[ndirs].length
11448 && dirs[j].length > 1
11449 && (dirs[ndirs].prefix == -1
11450 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11451 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11452 dirs[ndirs].prefix = j;
11454 ++ndirs;
11457 /* Now to the actual work. We have to find a subset of the directories which
11458 allow expressing the file name using references to the directory table
11459 with the least amount of characters. We do not do an exhaustive search
11460 where we would have to check out every combination of every single
11461 possible prefix. Instead we use a heuristic which provides nearly optimal
11462 results in most cases and never is much off. */
11463 saved = XALLOCAVEC (int, ndirs);
11464 savehere = XALLOCAVEC (int, ndirs);
11466 memset (saved, '\0', ndirs * sizeof (saved[0]));
11467 for (i = 0; i < ndirs; i++)
11469 int j;
11470 int total;
11472 /* We can always save some space for the current directory. But this
11473 does not mean it will be enough to justify adding the directory. */
11474 savehere[i] = dirs[i].length;
11475 total = (savehere[i] - saved[i]) * dirs[i].count;
11477 for (j = i + 1; j < ndirs; j++)
11479 savehere[j] = 0;
11480 if (saved[j] < dirs[i].length)
11482 /* Determine whether the dirs[i] path is a prefix of the
11483 dirs[j] path. */
11484 int k;
11486 k = dirs[j].prefix;
11487 while (k != -1 && k != (int) i)
11488 k = dirs[k].prefix;
11490 if (k == (int) i)
11492 /* Yes it is. We can possibly save some memory by
11493 writing the filenames in dirs[j] relative to
11494 dirs[i]. */
11495 savehere[j] = dirs[i].length;
11496 total += (savehere[j] - saved[j]) * dirs[j].count;
11501 /* Check whether we can save enough to justify adding the dirs[i]
11502 directory. */
11503 if (total > dirs[i].length + 1)
11505 /* It's worthwhile adding. */
11506 for (j = i; j < ndirs; j++)
11507 if (savehere[j] > 0)
11509 /* Remember how much we saved for this directory so far. */
11510 saved[j] = savehere[j];
11512 /* Remember the prefix directory. */
11513 dirs[j].dir_idx = i;
11518 /* Emit the directory name table. */
11519 idx_offset = dirs[0].length > 0 ? 1 : 0;
11520 enum dwarf_form str_form = DW_FORM_string;
11521 enum dwarf_form idx_form = DW_FORM_udata;
11522 if (dwarf_version >= 5)
11524 const char *comp_dir = comp_dir_string ();
11525 if (comp_dir == NULL)
11526 comp_dir = "";
11527 dw2_asm_output_data (1, 1, "Directory entry format count");
11528 if (DWARF5_USE_DEBUG_LINE_STR)
11529 str_form = DW_FORM_line_strp;
11530 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11531 dw2_asm_output_data_uleb128 (str_form, get_DW_FORM_name (str_form));
11532 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11533 if (str_form == DW_FORM_string)
11535 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11536 for (i = 1 - idx_offset; i < ndirs; i++)
11537 dw2_asm_output_nstring (dirs[i].path,
11538 dirs[i].length
11539 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11540 "Directory Entry: %#x", i + idx_offset);
11542 else
11544 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11545 for (i = 1 - idx_offset; i < ndirs; i++)
11547 const char *str
11548 = ggc_alloc_string (dirs[i].path,
11549 dirs[i].length
11550 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11551 output_line_string (str_form, str, "Directory Entry",
11552 (unsigned) i + idx_offset);
11556 else
11558 for (i = 1 - idx_offset; i < ndirs; i++)
11559 dw2_asm_output_nstring (dirs[i].path,
11560 dirs[i].length
11561 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11562 "Directory Entry: %#x", i + idx_offset);
11564 dw2_asm_output_data (1, 0, "End directory table");
11567 /* We have to emit them in the order of emitted_number since that's
11568 used in the debug info generation. To do this efficiently we
11569 generate a back-mapping of the indices first. */
11570 backmap = XALLOCAVEC (int, numfiles);
11571 for (i = 0; i < numfiles; i++)
11572 backmap[files[i].file_idx->emitted_number - 1] = i;
11574 if (dwarf_version >= 5)
11576 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11577 if (filename0 == NULL)
11578 filename0 = "";
11579 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11580 DW_FORM_data2. Choose one based on the number of directories
11581 and how much space would they occupy in each encoding.
11582 If we have at most 256 directories, all indexes fit into
11583 a single byte, so DW_FORM_data1 is most compact (if there
11584 are at most 128 directories, DW_FORM_udata would be as
11585 compact as that, but not shorter and slower to decode). */
11586 if (ndirs + idx_offset <= 256)
11587 idx_form = DW_FORM_data1;
11588 /* If there are more than 65536 directories, we have to use
11589 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11590 Otherwise, compute what space would occupy if all the indexes
11591 used DW_FORM_udata - sum - and compare that to how large would
11592 be DW_FORM_data2 encoding, and pick the more efficient one. */
11593 else if (ndirs + idx_offset <= 65536)
11595 unsigned HOST_WIDE_INT sum = 1;
11596 for (i = 0; i < numfiles; i++)
11598 int file_idx = backmap[i];
11599 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11600 sum += size_of_uleb128 (dir_idx);
11602 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11603 idx_form = DW_FORM_data2;
11605 #ifdef VMS_DEBUGGING_INFO
11606 dw2_asm_output_data (1, 4, "File name entry format count");
11607 #else
11608 dw2_asm_output_data (1, 2, "File name entry format count");
11609 #endif
11610 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11611 dw2_asm_output_data_uleb128 (str_form, get_DW_FORM_name (str_form));
11612 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11613 "DW_LNCT_directory_index");
11614 dw2_asm_output_data_uleb128 (idx_form, get_DW_FORM_name (idx_form));
11615 #ifdef VMS_DEBUGGING_INFO
11616 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11617 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11618 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11619 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11620 #endif
11621 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11623 output_line_string (str_form, filename0, "File Entry", 0);
11625 /* Include directory index. */
11626 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11627 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11628 0, NULL);
11629 else
11630 dw2_asm_output_data_uleb128 (0, NULL);
11632 #ifdef VMS_DEBUGGING_INFO
11633 dw2_asm_output_data_uleb128 (0, NULL);
11634 dw2_asm_output_data_uleb128 (0, NULL);
11635 #endif
11638 /* Now write all the file names. */
11639 for (i = 0; i < numfiles; i++)
11641 int file_idx = backmap[i];
11642 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11644 #ifdef VMS_DEBUGGING_INFO
11645 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11647 /* Setting these fields can lead to debugger miscomparisons,
11648 but VMS Debug requires them to be set correctly. */
11650 int ver;
11651 long long cdt;
11652 long siz;
11653 int maxfilelen = (strlen (files[file_idx].path)
11654 + dirs[dir_idx].length
11655 + MAX_VMS_VERSION_LEN + 1);
11656 char *filebuf = XALLOCAVEC (char, maxfilelen);
11658 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11659 snprintf (filebuf, maxfilelen, "%s;%d",
11660 files[file_idx].path + dirs[dir_idx].length, ver);
11662 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11664 /* Include directory index. */
11665 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11666 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11667 dir_idx + idx_offset, NULL);
11668 else
11669 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11671 /* Modification time. */
11672 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11673 &cdt, 0, 0, 0) == 0)
11674 ? cdt : 0, NULL);
11676 /* File length in bytes. */
11677 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11678 0, &siz, 0, 0) == 0)
11679 ? siz : 0, NULL);
11680 #else
11681 output_line_string (str_form,
11682 files[file_idx].path + dirs[dir_idx].length,
11683 "File Entry", (unsigned) i + 1);
11685 /* Include directory index. */
11686 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11687 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11688 dir_idx + idx_offset, NULL);
11689 else
11690 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11692 if (dwarf_version >= 5)
11693 continue;
11695 /* Modification time. */
11696 dw2_asm_output_data_uleb128 (0, NULL);
11698 /* File length in bytes. */
11699 dw2_asm_output_data_uleb128 (0, NULL);
11700 #endif /* VMS_DEBUGGING_INFO */
11703 if (dwarf_version < 5)
11704 dw2_asm_output_data (1, 0, "End file name table");
11708 /* Output one line number table into the .debug_line section. */
11710 static void
11711 output_one_line_info_table (dw_line_info_table *table)
11713 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11714 unsigned int current_line = 1;
11715 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11716 dw_line_info_entry *ent;
11717 size_t i;
11719 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11721 switch (ent->opcode)
11723 case LI_set_address:
11724 /* ??? Unfortunately, we have little choice here currently, and
11725 must always use the most general form. GCC does not know the
11726 address delta itself, so we can't use DW_LNS_advance_pc. Many
11727 ports do have length attributes which will give an upper bound
11728 on the address range. We could perhaps use length attributes
11729 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11730 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11732 /* This can handle any delta. This takes
11733 4+DWARF2_ADDR_SIZE bytes. */
11734 dw2_asm_output_data (1, 0, "set address %s", line_label);
11735 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11736 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11737 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11738 break;
11740 case LI_set_line:
11741 if (ent->val == current_line)
11743 /* We still need to start a new row, so output a copy insn. */
11744 dw2_asm_output_data (1, DW_LNS_copy,
11745 "copy line %u", current_line);
11747 else
11749 int line_offset = ent->val - current_line;
11750 int line_delta = line_offset - DWARF_LINE_BASE;
11752 current_line = ent->val;
11753 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11755 /* This can handle deltas from -10 to 234, using the current
11756 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11757 This takes 1 byte. */
11758 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11759 "line %u", current_line);
11761 else
11763 /* This can handle any delta. This takes at least 4 bytes,
11764 depending on the value being encoded. */
11765 dw2_asm_output_data (1, DW_LNS_advance_line,
11766 "advance to line %u", current_line);
11767 dw2_asm_output_data_sleb128 (line_offset, NULL);
11768 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11771 break;
11773 case LI_set_file:
11774 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11775 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11776 break;
11778 case LI_set_column:
11779 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11780 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11781 break;
11783 case LI_negate_stmt:
11784 current_is_stmt = !current_is_stmt;
11785 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11786 "is_stmt %d", current_is_stmt);
11787 break;
11789 case LI_set_prologue_end:
11790 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11791 "set prologue end");
11792 break;
11794 case LI_set_epilogue_begin:
11795 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11796 "set epilogue begin");
11797 break;
11799 case LI_set_discriminator:
11800 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11801 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11802 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11803 dw2_asm_output_data_uleb128 (ent->val, NULL);
11804 break;
11808 /* Emit debug info for the address of the end of the table. */
11809 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11810 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11811 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11814 dw2_asm_output_data (1, 0, "end sequence");
11815 dw2_asm_output_data_uleb128 (1, NULL);
11816 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11819 /* Output the source line number correspondence information. This
11820 information goes into the .debug_line section. */
11822 static void
11823 output_line_info (bool prologue_only)
11825 static unsigned int generation;
11826 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11827 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11828 bool saw_one = false;
11829 int opc;
11831 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11832 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11833 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11834 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11836 if (!XCOFF_DEBUGGING_INFO)
11838 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11839 dw2_asm_output_data (4, 0xffffffff,
11840 "Initial length escape value indicating 64-bit DWARF extension");
11841 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11842 "Length of Source Line Info");
11845 ASM_OUTPUT_LABEL (asm_out_file, l1);
11847 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11848 if (dwarf_version >= 5)
11850 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11851 dw2_asm_output_data (1, 0, "Segment Size");
11853 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11854 ASM_OUTPUT_LABEL (asm_out_file, p1);
11856 /* Define the architecture-dependent minimum instruction length (in bytes).
11857 In this implementation of DWARF, this field is used for information
11858 purposes only. Since GCC generates assembly language, we have no
11859 a priori knowledge of how many instruction bytes are generated for each
11860 source line, and therefore can use only the DW_LNE_set_address and
11861 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11862 this as '1', which is "correct enough" for all architectures,
11863 and don't let the target override. */
11864 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11866 if (dwarf_version >= 4)
11867 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11868 "Maximum Operations Per Instruction");
11869 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11870 "Default is_stmt_start flag");
11871 dw2_asm_output_data (1, DWARF_LINE_BASE,
11872 "Line Base Value (Special Opcodes)");
11873 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11874 "Line Range Value (Special Opcodes)");
11875 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11876 "Special Opcode Base");
11878 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11880 int n_op_args;
11881 switch (opc)
11883 case DW_LNS_advance_pc:
11884 case DW_LNS_advance_line:
11885 case DW_LNS_set_file:
11886 case DW_LNS_set_column:
11887 case DW_LNS_fixed_advance_pc:
11888 case DW_LNS_set_isa:
11889 n_op_args = 1;
11890 break;
11891 default:
11892 n_op_args = 0;
11893 break;
11896 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11897 opc, n_op_args);
11900 /* Write out the information about the files we use. */
11901 output_file_names ();
11902 ASM_OUTPUT_LABEL (asm_out_file, p2);
11903 if (prologue_only)
11905 /* Output the marker for the end of the line number info. */
11906 ASM_OUTPUT_LABEL (asm_out_file, l2);
11907 return;
11910 if (separate_line_info)
11912 dw_line_info_table *table;
11913 size_t i;
11915 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11916 if (table->in_use)
11918 output_one_line_info_table (table);
11919 saw_one = true;
11922 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
11924 output_one_line_info_table (cold_text_section_line_info);
11925 saw_one = true;
11928 /* ??? Some Darwin linkers crash on a .debug_line section with no
11929 sequences. Further, merely a DW_LNE_end_sequence entry is not
11930 sufficient -- the address column must also be initialized.
11931 Make sure to output at least one set_address/end_sequence pair,
11932 choosing .text since that section is always present. */
11933 if (text_section_line_info->in_use || !saw_one)
11934 output_one_line_info_table (text_section_line_info);
11936 /* Output the marker for the end of the line number info. */
11937 ASM_OUTPUT_LABEL (asm_out_file, l2);
11940 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
11942 static inline bool
11943 need_endianity_attribute_p (bool reverse)
11945 return reverse && (dwarf_version >= 3 || !dwarf_strict);
11948 /* Given a pointer to a tree node for some base type, return a pointer to
11949 a DIE that describes the given type. REVERSE is true if the type is
11950 to be interpreted in the reverse storage order wrt the target order.
11952 This routine must only be called for GCC type nodes that correspond to
11953 Dwarf base (fundamental) types. */
11955 static dw_die_ref
11956 base_type_die (tree type, bool reverse)
11958 dw_die_ref base_type_result;
11959 enum dwarf_type encoding;
11960 bool fpt_used = false;
11961 struct fixed_point_type_info fpt_info;
11962 tree type_bias = NULL_TREE;
11964 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11965 return 0;
11967 /* If this is a subtype that should not be emitted as a subrange type,
11968 use the base type. See subrange_type_for_debug_p. */
11969 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11970 type = TREE_TYPE (type);
11972 switch (TREE_CODE (type))
11974 case INTEGER_TYPE:
11975 if ((dwarf_version >= 4 || !dwarf_strict)
11976 && TYPE_NAME (type)
11977 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11978 && DECL_IS_BUILTIN (TYPE_NAME (type))
11979 && DECL_NAME (TYPE_NAME (type)))
11981 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
11982 if (strcmp (name, "char16_t") == 0
11983 || strcmp (name, "char32_t") == 0)
11985 encoding = DW_ATE_UTF;
11986 break;
11989 if ((dwarf_version >= 3 || !dwarf_strict)
11990 && lang_hooks.types.get_fixed_point_type_info)
11992 memset (&fpt_info, 0, sizeof (fpt_info));
11993 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
11995 fpt_used = true;
11996 encoding = ((TYPE_UNSIGNED (type))
11997 ? DW_ATE_unsigned_fixed
11998 : DW_ATE_signed_fixed);
11999 break;
12002 if (TYPE_STRING_FLAG (type))
12004 if (TYPE_UNSIGNED (type))
12005 encoding = DW_ATE_unsigned_char;
12006 else
12007 encoding = DW_ATE_signed_char;
12009 else if (TYPE_UNSIGNED (type))
12010 encoding = DW_ATE_unsigned;
12011 else
12012 encoding = DW_ATE_signed;
12014 if (!dwarf_strict
12015 && lang_hooks.types.get_type_bias)
12016 type_bias = lang_hooks.types.get_type_bias (type);
12017 break;
12019 case REAL_TYPE:
12020 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12022 if (dwarf_version >= 3 || !dwarf_strict)
12023 encoding = DW_ATE_decimal_float;
12024 else
12025 encoding = DW_ATE_lo_user;
12027 else
12028 encoding = DW_ATE_float;
12029 break;
12031 case FIXED_POINT_TYPE:
12032 if (!(dwarf_version >= 3 || !dwarf_strict))
12033 encoding = DW_ATE_lo_user;
12034 else if (TYPE_UNSIGNED (type))
12035 encoding = DW_ATE_unsigned_fixed;
12036 else
12037 encoding = DW_ATE_signed_fixed;
12038 break;
12040 /* Dwarf2 doesn't know anything about complex ints, so use
12041 a user defined type for it. */
12042 case COMPLEX_TYPE:
12043 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12044 encoding = DW_ATE_complex_float;
12045 else
12046 encoding = DW_ATE_lo_user;
12047 break;
12049 case BOOLEAN_TYPE:
12050 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12051 encoding = DW_ATE_boolean;
12052 break;
12054 default:
12055 /* No other TREE_CODEs are Dwarf fundamental types. */
12056 gcc_unreachable ();
12059 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12061 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12062 int_size_in_bytes (type));
12063 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12065 if (need_endianity_attribute_p (reverse))
12066 add_AT_unsigned (base_type_result, DW_AT_endianity,
12067 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12069 if (fpt_used)
12071 switch (fpt_info.scale_factor_kind)
12073 case fixed_point_scale_factor_binary:
12074 add_AT_int (base_type_result, DW_AT_binary_scale,
12075 fpt_info.scale_factor.binary);
12076 break;
12078 case fixed_point_scale_factor_decimal:
12079 add_AT_int (base_type_result, DW_AT_decimal_scale,
12080 fpt_info.scale_factor.decimal);
12081 break;
12083 case fixed_point_scale_factor_arbitrary:
12084 /* Arbitrary scale factors cannot be described in standard DWARF,
12085 yet. */
12086 if (!dwarf_strict)
12088 /* Describe the scale factor as a rational constant. */
12089 const dw_die_ref scale_factor
12090 = new_die (DW_TAG_constant, comp_unit_die (), type);
12092 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12093 fpt_info.scale_factor.arbitrary.numerator);
12094 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12095 fpt_info.scale_factor.arbitrary.denominator);
12097 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12099 break;
12101 default:
12102 gcc_unreachable ();
12106 if (type_bias)
12107 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12108 dw_scalar_form_constant
12109 | dw_scalar_form_exprloc
12110 | dw_scalar_form_reference,
12111 NULL);
12113 add_pubtype (type, base_type_result);
12115 return base_type_result;
12118 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12119 named 'auto' in its type: return true for it, false otherwise. */
12121 static inline bool
12122 is_cxx_auto (tree type)
12124 if (is_cxx ())
12126 tree name = TYPE_IDENTIFIER (type);
12127 if (name == get_identifier ("auto")
12128 || name == get_identifier ("decltype(auto)"))
12129 return true;
12131 return false;
12134 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12135 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12137 static inline int
12138 is_base_type (tree type)
12140 switch (TREE_CODE (type))
12142 case ERROR_MARK:
12143 case VOID_TYPE:
12144 case INTEGER_TYPE:
12145 case REAL_TYPE:
12146 case FIXED_POINT_TYPE:
12147 case COMPLEX_TYPE:
12148 case BOOLEAN_TYPE:
12149 case POINTER_BOUNDS_TYPE:
12150 return 1;
12152 case ARRAY_TYPE:
12153 case RECORD_TYPE:
12154 case UNION_TYPE:
12155 case QUAL_UNION_TYPE:
12156 case ENUMERAL_TYPE:
12157 case FUNCTION_TYPE:
12158 case METHOD_TYPE:
12159 case POINTER_TYPE:
12160 case REFERENCE_TYPE:
12161 case NULLPTR_TYPE:
12162 case OFFSET_TYPE:
12163 case LANG_TYPE:
12164 case VECTOR_TYPE:
12165 return 0;
12167 default:
12168 if (is_cxx_auto (type))
12169 return 0;
12170 gcc_unreachable ();
12173 return 0;
12176 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12177 node, return the size in bits for the type if it is a constant, or else
12178 return the alignment for the type if the type's size is not constant, or
12179 else return BITS_PER_WORD if the type actually turns out to be an
12180 ERROR_MARK node. */
12182 static inline unsigned HOST_WIDE_INT
12183 simple_type_size_in_bits (const_tree type)
12185 if (TREE_CODE (type) == ERROR_MARK)
12186 return BITS_PER_WORD;
12187 else if (TYPE_SIZE (type) == NULL_TREE)
12188 return 0;
12189 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12190 return tree_to_uhwi (TYPE_SIZE (type));
12191 else
12192 return TYPE_ALIGN (type);
12195 /* Similarly, but return an offset_int instead of UHWI. */
12197 static inline offset_int
12198 offset_int_type_size_in_bits (const_tree type)
12200 if (TREE_CODE (type) == ERROR_MARK)
12201 return BITS_PER_WORD;
12202 else if (TYPE_SIZE (type) == NULL_TREE)
12203 return 0;
12204 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12205 return wi::to_offset (TYPE_SIZE (type));
12206 else
12207 return TYPE_ALIGN (type);
12210 /* Given a pointer to a tree node for a subrange type, return a pointer
12211 to a DIE that describes the given type. */
12213 static dw_die_ref
12214 subrange_type_die (tree type, tree low, tree high, tree bias,
12215 dw_die_ref context_die)
12217 dw_die_ref subrange_die;
12218 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12220 if (context_die == NULL)
12221 context_die = comp_unit_die ();
12223 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12225 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12227 /* The size of the subrange type and its base type do not match,
12228 so we need to generate a size attribute for the subrange type. */
12229 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12232 if (low)
12233 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12234 if (high)
12235 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12236 if (bias && !dwarf_strict)
12237 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12238 dw_scalar_form_constant
12239 | dw_scalar_form_exprloc
12240 | dw_scalar_form_reference,
12241 NULL);
12243 return subrange_die;
12246 /* Returns the (const and/or volatile) cv_qualifiers associated with
12247 the decl node. This will normally be augmented with the
12248 cv_qualifiers of the underlying type in add_type_attribute. */
12250 static int
12251 decl_quals (const_tree decl)
12253 return ((TREE_READONLY (decl)
12254 /* The C++ front-end correctly marks reference-typed
12255 variables as readonly, but from a language (and debug
12256 info) standpoint they are not const-qualified. */
12257 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12258 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12259 | (TREE_THIS_VOLATILE (decl)
12260 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12263 /* Determine the TYPE whose qualifiers match the largest strict subset
12264 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12265 qualifiers outside QUAL_MASK. */
12267 static int
12268 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12270 tree t;
12271 int best_rank = 0, best_qual = 0, max_rank;
12273 type_quals &= qual_mask;
12274 max_rank = popcount_hwi (type_quals) - 1;
12276 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12277 t = TYPE_NEXT_VARIANT (t))
12279 int q = TYPE_QUALS (t) & qual_mask;
12281 if ((q & type_quals) == q && q != type_quals
12282 && check_base_type (t, type))
12284 int rank = popcount_hwi (q);
12286 if (rank > best_rank)
12288 best_rank = rank;
12289 best_qual = q;
12294 return best_qual;
12297 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12298 static const dwarf_qual_info_t dwarf_qual_info[] =
12300 { TYPE_QUAL_CONST, DW_TAG_const_type },
12301 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12302 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12303 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12305 static const unsigned int dwarf_qual_info_size
12306 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12308 /* If DIE is a qualified DIE of some base DIE with the same parent,
12309 return the base DIE, otherwise return NULL. Set MASK to the
12310 qualifiers added compared to the returned DIE. */
12312 static dw_die_ref
12313 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12315 unsigned int i;
12316 for (i = 0; i < dwarf_qual_info_size; i++)
12317 if (die->die_tag == dwarf_qual_info[i].t)
12318 break;
12319 if (i == dwarf_qual_info_size)
12320 return NULL;
12321 if (vec_safe_length (die->die_attr) != 1)
12322 return NULL;
12323 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12324 if (type == NULL || type->die_parent != die->die_parent)
12325 return NULL;
12326 *mask |= dwarf_qual_info[i].q;
12327 if (depth)
12329 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12330 if (ret)
12331 return ret;
12333 return type;
12336 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12337 entry that chains the modifiers specified by CV_QUALS in front of the
12338 given type. REVERSE is true if the type is to be interpreted in the
12339 reverse storage order wrt the target order. */
12341 static dw_die_ref
12342 modified_type_die (tree type, int cv_quals, bool reverse,
12343 dw_die_ref context_die)
12345 enum tree_code code = TREE_CODE (type);
12346 dw_die_ref mod_type_die;
12347 dw_die_ref sub_die = NULL;
12348 tree item_type = NULL;
12349 tree qualified_type;
12350 tree name, low, high;
12351 dw_die_ref mod_scope;
12352 /* Only these cv-qualifiers are currently handled. */
12353 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12354 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12356 if (code == ERROR_MARK)
12357 return NULL;
12359 if (lang_hooks.types.get_debug_type)
12361 tree debug_type = lang_hooks.types.get_debug_type (type);
12363 if (debug_type != NULL_TREE && debug_type != type)
12364 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12367 cv_quals &= cv_qual_mask;
12369 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12370 tag modifier (and not an attribute) old consumers won't be able
12371 to handle it. */
12372 if (dwarf_version < 3)
12373 cv_quals &= ~TYPE_QUAL_RESTRICT;
12375 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12376 if (dwarf_version < 5)
12377 cv_quals &= ~TYPE_QUAL_ATOMIC;
12379 /* See if we already have the appropriately qualified variant of
12380 this type. */
12381 qualified_type = get_qualified_type (type, cv_quals);
12383 if (qualified_type == sizetype
12384 && TYPE_NAME (qualified_type)
12385 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12387 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12389 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12390 && TYPE_PRECISION (t)
12391 == TYPE_PRECISION (qualified_type)
12392 && TYPE_UNSIGNED (t)
12393 == TYPE_UNSIGNED (qualified_type));
12394 qualified_type = t;
12397 /* If we do, then we can just use its DIE, if it exists. */
12398 if (qualified_type)
12400 mod_type_die = lookup_type_die (qualified_type);
12402 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12403 if (mod_type_die
12404 && (!need_endianity_attribute_p (reverse)
12405 || !is_base_type (type)
12406 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12407 return mod_type_die;
12410 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12412 /* Handle C typedef types. */
12413 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12414 && !DECL_ARTIFICIAL (name))
12416 tree dtype = TREE_TYPE (name);
12418 if (qualified_type == dtype)
12420 /* For a named type, use the typedef. */
12421 gen_type_die (qualified_type, context_die);
12422 return lookup_type_die (qualified_type);
12424 else
12426 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12427 dquals &= cv_qual_mask;
12428 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12429 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12430 /* cv-unqualified version of named type. Just use
12431 the unnamed type to which it refers. */
12432 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12433 reverse, context_die);
12434 /* Else cv-qualified version of named type; fall through. */
12438 mod_scope = scope_die_for (type, context_die);
12440 if (cv_quals)
12442 int sub_quals = 0, first_quals = 0;
12443 unsigned i;
12444 dw_die_ref first = NULL, last = NULL;
12446 /* Determine a lesser qualified type that most closely matches
12447 this one. Then generate DW_TAG_* entries for the remaining
12448 qualifiers. */
12449 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12450 cv_qual_mask);
12451 if (sub_quals && use_debug_types)
12453 bool needed = false;
12454 /* If emitting type units, make sure the order of qualifiers
12455 is canonical. Thus, start from unqualified type if
12456 an earlier qualifier is missing in sub_quals, but some later
12457 one is present there. */
12458 for (i = 0; i < dwarf_qual_info_size; i++)
12459 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12460 needed = true;
12461 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12463 sub_quals = 0;
12464 break;
12467 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12468 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12470 /* As not all intermediate qualified DIEs have corresponding
12471 tree types, ensure that qualified DIEs in the same scope
12472 as their DW_AT_type are emitted after their DW_AT_type,
12473 only with other qualified DIEs for the same type possibly
12474 in between them. Determine the range of such qualified
12475 DIEs now (first being the base type, last being corresponding
12476 last qualified DIE for it). */
12477 unsigned int count = 0;
12478 first = qualified_die_p (mod_type_die, &first_quals,
12479 dwarf_qual_info_size);
12480 if (first == NULL)
12481 first = mod_type_die;
12482 gcc_assert ((first_quals & ~sub_quals) == 0);
12483 for (count = 0, last = first;
12484 count < (1U << dwarf_qual_info_size);
12485 count++, last = last->die_sib)
12487 int quals = 0;
12488 if (last == mod_scope->die_child)
12489 break;
12490 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12491 != first)
12492 break;
12496 for (i = 0; i < dwarf_qual_info_size; i++)
12497 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12499 dw_die_ref d;
12500 if (first && first != last)
12502 for (d = first->die_sib; ; d = d->die_sib)
12504 int quals = 0;
12505 qualified_die_p (d, &quals, dwarf_qual_info_size);
12506 if (quals == (first_quals | dwarf_qual_info[i].q))
12507 break;
12508 if (d == last)
12510 d = NULL;
12511 break;
12514 if (d)
12516 mod_type_die = d;
12517 continue;
12520 if (first)
12522 d = ggc_cleared_alloc<die_node> ();
12523 d->die_tag = dwarf_qual_info[i].t;
12524 add_child_die_after (mod_scope, d, last);
12525 last = d;
12527 else
12528 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12529 if (mod_type_die)
12530 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12531 mod_type_die = d;
12532 first_quals |= dwarf_qual_info[i].q;
12535 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12537 dwarf_tag tag = DW_TAG_pointer_type;
12538 if (code == REFERENCE_TYPE)
12540 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12541 tag = DW_TAG_rvalue_reference_type;
12542 else
12543 tag = DW_TAG_reference_type;
12545 mod_type_die = new_die (tag, mod_scope, type);
12547 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12548 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12549 item_type = TREE_TYPE (type);
12551 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12552 if (!ADDR_SPACE_GENERIC_P (as))
12554 int action = targetm.addr_space.debug (as);
12555 if (action >= 0)
12557 /* Positive values indicate an address_class. */
12558 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12560 else
12562 /* Negative values indicate an (inverted) segment base reg. */
12563 dw_loc_descr_ref d
12564 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12565 add_AT_loc (mod_type_die, DW_AT_segment, d);
12569 else if (code == INTEGER_TYPE
12570 && TREE_TYPE (type) != NULL_TREE
12571 && subrange_type_for_debug_p (type, &low, &high))
12573 tree bias = NULL_TREE;
12574 if (lang_hooks.types.get_type_bias)
12575 bias = lang_hooks.types.get_type_bias (type);
12576 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12577 item_type = TREE_TYPE (type);
12579 else if (is_base_type (type))
12580 mod_type_die = base_type_die (type, reverse);
12581 else
12583 gen_type_die (type, context_die);
12585 /* We have to get the type_main_variant here (and pass that to the
12586 `lookup_type_die' routine) because the ..._TYPE node we have
12587 might simply be a *copy* of some original type node (where the
12588 copy was created to help us keep track of typedef names) and
12589 that copy might have a different TYPE_UID from the original
12590 ..._TYPE node. */
12591 if (TREE_CODE (type) == FUNCTION_TYPE
12592 || TREE_CODE (type) == METHOD_TYPE)
12594 /* For function/method types, can't just use type_main_variant here,
12595 because that can have different ref-qualifiers for C++,
12596 but try to canonicalize. */
12597 tree main = TYPE_MAIN_VARIANT (type);
12598 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12599 if (check_base_type (t, main) && check_lang_type (t, type))
12600 return lookup_type_die (t);
12601 return lookup_type_die (type);
12603 else if (TREE_CODE (type) != VECTOR_TYPE
12604 && TREE_CODE (type) != ARRAY_TYPE)
12605 return lookup_type_die (type_main_variant (type));
12606 else
12607 /* Vectors have the debugging information in the type,
12608 not the main variant. */
12609 return lookup_type_die (type);
12612 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12613 don't output a DW_TAG_typedef, since there isn't one in the
12614 user's program; just attach a DW_AT_name to the type.
12615 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12616 if the base type already has the same name. */
12617 if (name
12618 && ((TREE_CODE (name) != TYPE_DECL
12619 && (qualified_type == TYPE_MAIN_VARIANT (type)
12620 || (cv_quals == TYPE_UNQUALIFIED)))
12621 || (TREE_CODE (name) == TYPE_DECL
12622 && TREE_TYPE (name) == qualified_type
12623 && DECL_NAME (name))))
12625 if (TREE_CODE (name) == TYPE_DECL)
12626 /* Could just call add_name_and_src_coords_attributes here,
12627 but since this is a builtin type it doesn't have any
12628 useful source coordinates anyway. */
12629 name = DECL_NAME (name);
12630 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12632 /* This probably indicates a bug. */
12633 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12635 name = TYPE_IDENTIFIER (type);
12636 add_name_attribute (mod_type_die,
12637 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12640 if (qualified_type)
12641 equate_type_number_to_die (qualified_type, mod_type_die);
12643 if (item_type)
12644 /* We must do this after the equate_type_number_to_die call, in case
12645 this is a recursive type. This ensures that the modified_type_die
12646 recursion will terminate even if the type is recursive. Recursive
12647 types are possible in Ada. */
12648 sub_die = modified_type_die (item_type,
12649 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12650 reverse,
12651 context_die);
12653 if (sub_die != NULL)
12654 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12656 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12657 if (TYPE_ARTIFICIAL (type))
12658 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12660 return mod_type_die;
12663 /* Generate DIEs for the generic parameters of T.
12664 T must be either a generic type or a generic function.
12665 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12667 static void
12668 gen_generic_params_dies (tree t)
12670 tree parms, args;
12671 int parms_num, i;
12672 dw_die_ref die = NULL;
12673 int non_default;
12675 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12676 return;
12678 if (TYPE_P (t))
12679 die = lookup_type_die (t);
12680 else if (DECL_P (t))
12681 die = lookup_decl_die (t);
12683 gcc_assert (die);
12685 parms = lang_hooks.get_innermost_generic_parms (t);
12686 if (!parms)
12687 /* T has no generic parameter. It means T is neither a generic type
12688 or function. End of story. */
12689 return;
12691 parms_num = TREE_VEC_LENGTH (parms);
12692 args = lang_hooks.get_innermost_generic_args (t);
12693 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12694 non_default = int_cst_value (TREE_CHAIN (args));
12695 else
12696 non_default = TREE_VEC_LENGTH (args);
12697 for (i = 0; i < parms_num; i++)
12699 tree parm, arg, arg_pack_elems;
12700 dw_die_ref parm_die;
12702 parm = TREE_VEC_ELT (parms, i);
12703 arg = TREE_VEC_ELT (args, i);
12704 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12705 gcc_assert (parm && TREE_VALUE (parm) && arg);
12707 if (parm && TREE_VALUE (parm) && arg)
12709 /* If PARM represents a template parameter pack,
12710 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12711 by DW_TAG_template_*_parameter DIEs for the argument
12712 pack elements of ARG. Note that ARG would then be
12713 an argument pack. */
12714 if (arg_pack_elems)
12715 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12716 arg_pack_elems,
12717 die);
12718 else
12719 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12720 true /* emit name */, die);
12721 if (i >= non_default)
12722 add_AT_flag (parm_die, DW_AT_default_value, 1);
12727 /* Create and return a DIE for PARM which should be
12728 the representation of a generic type parameter.
12729 For instance, in the C++ front end, PARM would be a template parameter.
12730 ARG is the argument to PARM.
12731 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12732 name of the PARM.
12733 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12734 as a child node. */
12736 static dw_die_ref
12737 generic_parameter_die (tree parm, tree arg,
12738 bool emit_name_p,
12739 dw_die_ref parent_die)
12741 dw_die_ref tmpl_die = NULL;
12742 const char *name = NULL;
12744 if (!parm || !DECL_NAME (parm) || !arg)
12745 return NULL;
12747 /* We support non-type generic parameters and arguments,
12748 type generic parameters and arguments, as well as
12749 generic generic parameters (a.k.a. template template parameters in C++)
12750 and arguments. */
12751 if (TREE_CODE (parm) == PARM_DECL)
12752 /* PARM is a nontype generic parameter */
12753 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12754 else if (TREE_CODE (parm) == TYPE_DECL)
12755 /* PARM is a type generic parameter. */
12756 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12757 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12758 /* PARM is a generic generic parameter.
12759 Its DIE is a GNU extension. It shall have a
12760 DW_AT_name attribute to represent the name of the template template
12761 parameter, and a DW_AT_GNU_template_name attribute to represent the
12762 name of the template template argument. */
12763 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12764 parent_die, parm);
12765 else
12766 gcc_unreachable ();
12768 if (tmpl_die)
12770 tree tmpl_type;
12772 /* If PARM is a generic parameter pack, it means we are
12773 emitting debug info for a template argument pack element.
12774 In other terms, ARG is a template argument pack element.
12775 In that case, we don't emit any DW_AT_name attribute for
12776 the die. */
12777 if (emit_name_p)
12779 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12780 gcc_assert (name);
12781 add_AT_string (tmpl_die, DW_AT_name, name);
12784 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12786 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12787 TMPL_DIE should have a child DW_AT_type attribute that is set
12788 to the type of the argument to PARM, which is ARG.
12789 If PARM is a type generic parameter, TMPL_DIE should have a
12790 child DW_AT_type that is set to ARG. */
12791 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12792 add_type_attribute (tmpl_die, tmpl_type,
12793 (TREE_THIS_VOLATILE (tmpl_type)
12794 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12795 false, parent_die);
12797 else
12799 /* So TMPL_DIE is a DIE representing a
12800 a generic generic template parameter, a.k.a template template
12801 parameter in C++ and arg is a template. */
12803 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12804 to the name of the argument. */
12805 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12806 if (name)
12807 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12810 if (TREE_CODE (parm) == PARM_DECL)
12811 /* So PARM is a non-type generic parameter.
12812 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12813 attribute of TMPL_DIE which value represents the value
12814 of ARG.
12815 We must be careful here:
12816 The value of ARG might reference some function decls.
12817 We might currently be emitting debug info for a generic
12818 type and types are emitted before function decls, we don't
12819 know if the function decls referenced by ARG will actually be
12820 emitted after cgraph computations.
12821 So must defer the generation of the DW_AT_const_value to
12822 after cgraph is ready. */
12823 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12826 return tmpl_die;
12829 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12830 PARM_PACK must be a template parameter pack. The returned DIE
12831 will be child DIE of PARENT_DIE. */
12833 static dw_die_ref
12834 template_parameter_pack_die (tree parm_pack,
12835 tree parm_pack_args,
12836 dw_die_ref parent_die)
12838 dw_die_ref die;
12839 int j;
12841 gcc_assert (parent_die && parm_pack);
12843 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12844 add_name_and_src_coords_attributes (die, parm_pack);
12845 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12846 generic_parameter_die (parm_pack,
12847 TREE_VEC_ELT (parm_pack_args, j),
12848 false /* Don't emit DW_AT_name */,
12849 die);
12850 return die;
12853 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12854 an enumerated type. */
12856 static inline int
12857 type_is_enum (const_tree type)
12859 return TREE_CODE (type) == ENUMERAL_TYPE;
12862 /* Return the DBX register number described by a given RTL node. */
12864 static unsigned int
12865 dbx_reg_number (const_rtx rtl)
12867 unsigned regno = REGNO (rtl);
12869 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12871 #ifdef LEAF_REG_REMAP
12872 if (crtl->uses_only_leaf_regs)
12874 int leaf_reg = LEAF_REG_REMAP (regno);
12875 if (leaf_reg != -1)
12876 regno = (unsigned) leaf_reg;
12878 #endif
12880 regno = DBX_REGISTER_NUMBER (regno);
12881 gcc_assert (regno != INVALID_REGNUM);
12882 return regno;
12885 /* Optionally add a DW_OP_piece term to a location description expression.
12886 DW_OP_piece is only added if the location description expression already
12887 doesn't end with DW_OP_piece. */
12889 static void
12890 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12892 dw_loc_descr_ref loc;
12894 if (*list_head != NULL)
12896 /* Find the end of the chain. */
12897 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12900 if (loc->dw_loc_opc != DW_OP_piece)
12901 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12905 /* Return a location descriptor that designates a machine register or
12906 zero if there is none. */
12908 static dw_loc_descr_ref
12909 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12911 rtx regs;
12913 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12914 return 0;
12916 /* We only use "frame base" when we're sure we're talking about the
12917 post-prologue local stack frame. We do this by *not* running
12918 register elimination until this point, and recognizing the special
12919 argument pointer and soft frame pointer rtx's.
12920 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
12921 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
12922 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
12924 dw_loc_descr_ref result = NULL;
12926 if (dwarf_version >= 4 || !dwarf_strict)
12928 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
12929 initialized);
12930 if (result)
12931 add_loc_descr (&result,
12932 new_loc_descr (DW_OP_stack_value, 0, 0));
12934 return result;
12937 regs = targetm.dwarf_register_span (rtl);
12939 if (REG_NREGS (rtl) > 1 || regs)
12940 return multiple_reg_loc_descriptor (rtl, regs, initialized);
12941 else
12943 unsigned int dbx_regnum = dbx_reg_number (rtl);
12944 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12945 return 0;
12946 return one_reg_loc_descriptor (dbx_regnum, initialized);
12950 /* Return a location descriptor that designates a machine register for
12951 a given hard register number. */
12953 static dw_loc_descr_ref
12954 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12956 dw_loc_descr_ref reg_loc_descr;
12958 if (regno <= 31)
12959 reg_loc_descr
12960 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12961 else
12962 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12964 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12965 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12967 return reg_loc_descr;
12970 /* Given an RTL of a register, return a location descriptor that
12971 designates a value that spans more than one register. */
12973 static dw_loc_descr_ref
12974 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
12975 enum var_init_status initialized)
12977 int size, i;
12978 dw_loc_descr_ref loc_result = NULL;
12980 /* Simple, contiguous registers. */
12981 if (regs == NULL_RTX)
12983 unsigned reg = REGNO (rtl);
12984 int nregs;
12986 #ifdef LEAF_REG_REMAP
12987 if (crtl->uses_only_leaf_regs)
12989 int leaf_reg = LEAF_REG_REMAP (reg);
12990 if (leaf_reg != -1)
12991 reg = (unsigned) leaf_reg;
12993 #endif
12995 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
12996 nregs = REG_NREGS (rtl);
12998 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13000 loc_result = NULL;
13001 while (nregs--)
13003 dw_loc_descr_ref t;
13005 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13006 VAR_INIT_STATUS_INITIALIZED);
13007 add_loc_descr (&loc_result, t);
13008 add_loc_descr_op_piece (&loc_result, size);
13009 ++reg;
13011 return loc_result;
13014 /* Now onto stupid register sets in non contiguous locations. */
13016 gcc_assert (GET_CODE (regs) == PARALLEL);
13018 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13019 loc_result = NULL;
13021 for (i = 0; i < XVECLEN (regs, 0); ++i)
13023 dw_loc_descr_ref t;
13025 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13026 VAR_INIT_STATUS_INITIALIZED);
13027 add_loc_descr (&loc_result, t);
13028 add_loc_descr_op_piece (&loc_result, size);
13031 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13032 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13033 return loc_result;
13036 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13038 /* Return a location descriptor that designates a constant i,
13039 as a compound operation from constant (i >> shift), constant shift
13040 and DW_OP_shl. */
13042 static dw_loc_descr_ref
13043 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13045 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13046 add_loc_descr (&ret, int_loc_descriptor (shift));
13047 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13048 return ret;
13051 /* Return a location descriptor that designates a constant. */
13053 static dw_loc_descr_ref
13054 int_loc_descriptor (HOST_WIDE_INT i)
13056 enum dwarf_location_atom op;
13058 /* Pick the smallest representation of a constant, rather than just
13059 defaulting to the LEB encoding. */
13060 if (i >= 0)
13062 int clz = clz_hwi (i);
13063 int ctz = ctz_hwi (i);
13064 if (i <= 31)
13065 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13066 else if (i <= 0xff)
13067 op = DW_OP_const1u;
13068 else if (i <= 0xffff)
13069 op = DW_OP_const2u;
13070 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13071 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13072 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13073 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13074 while DW_OP_const4u is 5 bytes. */
13075 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13076 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13077 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13078 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13079 while DW_OP_const4u is 5 bytes. */
13080 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13082 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13083 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13084 <= 4)
13086 /* As i >= 2**31, the double cast above will yield a negative number.
13087 Since wrapping is defined in DWARF expressions we can output big
13088 positive integers as small negative ones, regardless of the size
13089 of host wide ints.
13091 Here, since the evaluator will handle 32-bit values and since i >=
13092 2**31, we know it's going to be interpreted as a negative literal:
13093 store it this way if we can do better than 5 bytes this way. */
13094 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13096 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13097 op = DW_OP_const4u;
13099 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13100 least 6 bytes: see if we can do better before falling back to it. */
13101 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13102 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13103 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13104 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13105 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13106 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13107 >= HOST_BITS_PER_WIDE_INT)
13108 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13109 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13110 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13111 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13112 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13113 && size_of_uleb128 (i) > 6)
13114 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13115 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13116 else
13117 op = DW_OP_constu;
13119 else
13121 if (i >= -0x80)
13122 op = DW_OP_const1s;
13123 else if (i >= -0x8000)
13124 op = DW_OP_const2s;
13125 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13127 if (size_of_int_loc_descriptor (i) < 5)
13129 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13130 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13131 return ret;
13133 op = DW_OP_const4s;
13135 else
13137 if (size_of_int_loc_descriptor (i)
13138 < (unsigned long) 1 + size_of_sleb128 (i))
13140 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13141 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13142 return ret;
13144 op = DW_OP_consts;
13148 return new_loc_descr (op, i, 0);
13151 /* Likewise, for unsigned constants. */
13153 static dw_loc_descr_ref
13154 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13156 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13157 const unsigned HOST_WIDE_INT max_uint
13158 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13160 /* If possible, use the clever signed constants handling. */
13161 if (i <= max_int)
13162 return int_loc_descriptor ((HOST_WIDE_INT) i);
13164 /* Here, we are left with positive numbers that cannot be represented as
13165 HOST_WIDE_INT, i.e.:
13166 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13168 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13169 whereas may be better to output a negative integer: thanks to integer
13170 wrapping, we know that:
13171 x = x - 2 ** DWARF2_ADDR_SIZE
13172 = x - 2 * (max (HOST_WIDE_INT) + 1)
13173 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13174 small negative integers. Let's try that in cases it will clearly improve
13175 the encoding: there is no gain turning DW_OP_const4u into
13176 DW_OP_const4s. */
13177 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13178 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13179 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13181 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13183 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13184 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13185 const HOST_WIDE_INT second_shift
13186 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13188 /* So we finally have:
13189 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13190 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13191 return int_loc_descriptor (second_shift);
13194 /* Last chance: fallback to a simple constant operation. */
13195 return new_loc_descr
13196 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13197 ? DW_OP_const4u
13198 : DW_OP_const8u,
13199 i, 0);
13202 /* Generate and return a location description that computes the unsigned
13203 comparison of the two stack top entries (a OP b where b is the top-most
13204 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13205 LE_EXPR, GT_EXPR or GE_EXPR. */
13207 static dw_loc_descr_ref
13208 uint_comparison_loc_list (enum tree_code kind)
13210 enum dwarf_location_atom op, flip_op;
13211 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13213 switch (kind)
13215 case LT_EXPR:
13216 op = DW_OP_lt;
13217 break;
13218 case LE_EXPR:
13219 op = DW_OP_le;
13220 break;
13221 case GT_EXPR:
13222 op = DW_OP_gt;
13223 break;
13224 case GE_EXPR:
13225 op = DW_OP_ge;
13226 break;
13227 default:
13228 gcc_unreachable ();
13231 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13232 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13234 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13235 possible to perform unsigned comparisons: we just have to distinguish
13236 three cases:
13238 1. when a and b have the same sign (as signed integers); then we should
13239 return: a OP(signed) b;
13241 2. when a is a negative signed integer while b is a positive one, then a
13242 is a greater unsigned integer than b; likewise when a and b's roles
13243 are flipped.
13245 So first, compare the sign of the two operands. */
13246 ret = new_loc_descr (DW_OP_over, 0, 0);
13247 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13248 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13249 /* If they have different signs (i.e. they have different sign bits), then
13250 the stack top value has now the sign bit set and thus it's smaller than
13251 zero. */
13252 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13253 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13254 add_loc_descr (&ret, bra_node);
13256 /* We are in case 1. At this point, we know both operands have the same
13257 sign, to it's safe to use the built-in signed comparison. */
13258 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13259 add_loc_descr (&ret, jmp_node);
13261 /* We are in case 2. Here, we know both operands do not have the same sign,
13262 so we have to flip the signed comparison. */
13263 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13264 tmp = new_loc_descr (flip_op, 0, 0);
13265 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13266 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13267 add_loc_descr (&ret, tmp);
13269 /* This dummy operation is necessary to make the two branches join. */
13270 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13271 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13272 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13273 add_loc_descr (&ret, tmp);
13275 return ret;
13278 /* Likewise, but takes the location description lists (might be destructive on
13279 them). Return NULL if either is NULL or if concatenation fails. */
13281 static dw_loc_list_ref
13282 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13283 enum tree_code kind)
13285 if (left == NULL || right == NULL)
13286 return NULL;
13288 add_loc_list (&left, right);
13289 if (left == NULL)
13290 return NULL;
13292 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13293 return left;
13296 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13297 without actually allocating it. */
13299 static unsigned long
13300 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13302 return size_of_int_loc_descriptor (i >> shift)
13303 + size_of_int_loc_descriptor (shift)
13304 + 1;
13307 /* Return size_of_locs (int_loc_descriptor (i)) without
13308 actually allocating it. */
13310 static unsigned long
13311 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13313 unsigned long s;
13315 if (i >= 0)
13317 int clz, ctz;
13318 if (i <= 31)
13319 return 1;
13320 else if (i <= 0xff)
13321 return 2;
13322 else if (i <= 0xffff)
13323 return 3;
13324 clz = clz_hwi (i);
13325 ctz = ctz_hwi (i);
13326 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13327 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13328 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13329 - clz - 5);
13330 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13331 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13332 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13333 - clz - 8);
13334 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13335 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13336 <= 4)
13337 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13338 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13339 return 5;
13340 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13341 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13342 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13343 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13344 - clz - 8);
13345 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13346 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13347 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13348 - clz - 16);
13349 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13350 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13351 && s > 6)
13352 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13353 - clz - 32);
13354 else
13355 return 1 + s;
13357 else
13359 if (i >= -0x80)
13360 return 2;
13361 else if (i >= -0x8000)
13362 return 3;
13363 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13365 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13367 s = size_of_int_loc_descriptor (-i) + 1;
13368 if (s < 5)
13369 return s;
13371 return 5;
13373 else
13375 unsigned long r = 1 + size_of_sleb128 (i);
13376 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13378 s = size_of_int_loc_descriptor (-i) + 1;
13379 if (s < r)
13380 return s;
13382 return r;
13387 /* Return loc description representing "address" of integer value.
13388 This can appear only as toplevel expression. */
13390 static dw_loc_descr_ref
13391 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13393 int litsize;
13394 dw_loc_descr_ref loc_result = NULL;
13396 if (!(dwarf_version >= 4 || !dwarf_strict))
13397 return NULL;
13399 litsize = size_of_int_loc_descriptor (i);
13400 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13401 is more compact. For DW_OP_stack_value we need:
13402 litsize + 1 (DW_OP_stack_value)
13403 and for DW_OP_implicit_value:
13404 1 (DW_OP_implicit_value) + 1 (length) + size. */
13405 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13407 loc_result = int_loc_descriptor (i);
13408 add_loc_descr (&loc_result,
13409 new_loc_descr (DW_OP_stack_value, 0, 0));
13410 return loc_result;
13413 loc_result = new_loc_descr (DW_OP_implicit_value,
13414 size, 0);
13415 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13416 loc_result->dw_loc_oprnd2.v.val_int = i;
13417 return loc_result;
13420 /* Return a location descriptor that designates a base+offset location. */
13422 static dw_loc_descr_ref
13423 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13424 enum var_init_status initialized)
13426 unsigned int regno;
13427 dw_loc_descr_ref result;
13428 dw_fde_ref fde = cfun->fde;
13430 /* We only use "frame base" when we're sure we're talking about the
13431 post-prologue local stack frame. We do this by *not* running
13432 register elimination until this point, and recognizing the special
13433 argument pointer and soft frame pointer rtx's. */
13434 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13436 rtx elim = (ira_use_lra_p
13437 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13438 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13440 if (elim != reg)
13442 if (GET_CODE (elim) == PLUS)
13444 offset += INTVAL (XEXP (elim, 1));
13445 elim = XEXP (elim, 0);
13447 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13448 && (elim == hard_frame_pointer_rtx
13449 || elim == stack_pointer_rtx))
13450 || elim == (frame_pointer_needed
13451 ? hard_frame_pointer_rtx
13452 : stack_pointer_rtx));
13454 /* If drap register is used to align stack, use frame
13455 pointer + offset to access stack variables. If stack
13456 is aligned without drap, use stack pointer + offset to
13457 access stack variables. */
13458 if (crtl->stack_realign_tried
13459 && reg == frame_pointer_rtx)
13461 int base_reg
13462 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13463 ? HARD_FRAME_POINTER_REGNUM
13464 : REGNO (elim));
13465 return new_reg_loc_descr (base_reg, offset);
13468 gcc_assert (frame_pointer_fb_offset_valid);
13469 offset += frame_pointer_fb_offset;
13470 return new_loc_descr (DW_OP_fbreg, offset, 0);
13474 regno = REGNO (reg);
13475 #ifdef LEAF_REG_REMAP
13476 if (crtl->uses_only_leaf_regs)
13478 int leaf_reg = LEAF_REG_REMAP (regno);
13479 if (leaf_reg != -1)
13480 regno = (unsigned) leaf_reg;
13482 #endif
13483 regno = DWARF_FRAME_REGNUM (regno);
13485 if (!optimize && fde
13486 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13488 /* Use cfa+offset to represent the location of arguments passed
13489 on the stack when drap is used to align stack.
13490 Only do this when not optimizing, for optimized code var-tracking
13491 is supposed to track where the arguments live and the register
13492 used as vdrap or drap in some spot might be used for something
13493 else in other part of the routine. */
13494 return new_loc_descr (DW_OP_fbreg, offset, 0);
13497 if (regno <= 31)
13498 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13499 offset, 0);
13500 else
13501 result = new_loc_descr (DW_OP_bregx, regno, offset);
13503 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13504 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13506 return result;
13509 /* Return true if this RTL expression describes a base+offset calculation. */
13511 static inline int
13512 is_based_loc (const_rtx rtl)
13514 return (GET_CODE (rtl) == PLUS
13515 && ((REG_P (XEXP (rtl, 0))
13516 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13517 && CONST_INT_P (XEXP (rtl, 1)))));
13520 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13521 failed. */
13523 static dw_loc_descr_ref
13524 tls_mem_loc_descriptor (rtx mem)
13526 tree base;
13527 dw_loc_descr_ref loc_result;
13529 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13530 return NULL;
13532 base = get_base_address (MEM_EXPR (mem));
13533 if (base == NULL
13534 || !VAR_P (base)
13535 || !DECL_THREAD_LOCAL_P (base))
13536 return NULL;
13538 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13539 if (loc_result == NULL)
13540 return NULL;
13542 if (MEM_OFFSET (mem))
13543 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13545 return loc_result;
13548 /* Output debug info about reason why we failed to expand expression as dwarf
13549 expression. */
13551 static void
13552 expansion_failed (tree expr, rtx rtl, char const *reason)
13554 if (dump_file && (dump_flags & TDF_DETAILS))
13556 fprintf (dump_file, "Failed to expand as dwarf: ");
13557 if (expr)
13558 print_generic_expr (dump_file, expr, dump_flags);
13559 if (rtl)
13561 fprintf (dump_file, "\n");
13562 print_rtl (dump_file, rtl);
13564 fprintf (dump_file, "\nReason: %s\n", reason);
13568 /* Helper function for const_ok_for_output. */
13570 static bool
13571 const_ok_for_output_1 (rtx rtl)
13573 if (GET_CODE (rtl) == UNSPEC)
13575 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13576 we can't express it in the debug info. */
13577 /* Don't complain about TLS UNSPECs, those are just too hard to
13578 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13579 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13580 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13581 if (flag_checking
13582 && (XVECLEN (rtl, 0) == 0
13583 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13584 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13585 inform (current_function_decl
13586 ? DECL_SOURCE_LOCATION (current_function_decl)
13587 : UNKNOWN_LOCATION,
13588 #if NUM_UNSPEC_VALUES > 0
13589 "non-delegitimized UNSPEC %s (%d) found in variable location",
13590 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13591 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13592 XINT (rtl, 1));
13593 #else
13594 "non-delegitimized UNSPEC %d found in variable location",
13595 XINT (rtl, 1));
13596 #endif
13597 expansion_failed (NULL_TREE, rtl,
13598 "UNSPEC hasn't been delegitimized.\n");
13599 return false;
13602 if (targetm.const_not_ok_for_debug_p (rtl))
13604 expansion_failed (NULL_TREE, rtl,
13605 "Expression rejected for debug by the backend.\n");
13606 return false;
13609 /* FIXME: Refer to PR60655. It is possible for simplification
13610 of rtl expressions in var tracking to produce such expressions.
13611 We should really identify / validate expressions
13612 enclosed in CONST that can be handled by assemblers on various
13613 targets and only handle legitimate cases here. */
13614 if (GET_CODE (rtl) != SYMBOL_REF)
13616 if (GET_CODE (rtl) == NOT)
13617 return false;
13618 return true;
13621 if (CONSTANT_POOL_ADDRESS_P (rtl))
13623 bool marked;
13624 get_pool_constant_mark (rtl, &marked);
13625 /* If all references to this pool constant were optimized away,
13626 it was not output and thus we can't represent it. */
13627 if (!marked)
13629 expansion_failed (NULL_TREE, rtl,
13630 "Constant was removed from constant pool.\n");
13631 return false;
13635 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13636 return false;
13638 /* Avoid references to external symbols in debug info, on several targets
13639 the linker might even refuse to link when linking a shared library,
13640 and in many other cases the relocations for .debug_info/.debug_loc are
13641 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13642 to be defined within the same shared library or executable are fine. */
13643 if (SYMBOL_REF_EXTERNAL_P (rtl))
13645 tree decl = SYMBOL_REF_DECL (rtl);
13647 if (decl == NULL || !targetm.binds_local_p (decl))
13649 expansion_failed (NULL_TREE, rtl,
13650 "Symbol not defined in current TU.\n");
13651 return false;
13655 return true;
13658 /* Return true if constant RTL can be emitted in DW_OP_addr or
13659 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13660 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13662 static bool
13663 const_ok_for_output (rtx rtl)
13665 if (GET_CODE (rtl) == SYMBOL_REF)
13666 return const_ok_for_output_1 (rtl);
13668 if (GET_CODE (rtl) == CONST)
13670 subrtx_var_iterator::array_type array;
13671 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13672 if (!const_ok_for_output_1 (*iter))
13673 return false;
13674 return true;
13677 return true;
13680 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13681 if possible, NULL otherwise. */
13683 static dw_die_ref
13684 base_type_for_mode (machine_mode mode, bool unsignedp)
13686 dw_die_ref type_die;
13687 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13689 if (type == NULL)
13690 return NULL;
13691 switch (TREE_CODE (type))
13693 case INTEGER_TYPE:
13694 case REAL_TYPE:
13695 break;
13696 default:
13697 return NULL;
13699 type_die = lookup_type_die (type);
13700 if (!type_die)
13701 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13702 comp_unit_die ());
13703 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13704 return NULL;
13705 return type_die;
13708 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13709 type matching MODE, or, if MODE is narrower than or as wide as
13710 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13711 possible. */
13713 static dw_loc_descr_ref
13714 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13716 machine_mode outer_mode = mode;
13717 dw_die_ref type_die;
13718 dw_loc_descr_ref cvt;
13720 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13722 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13723 return op;
13725 type_die = base_type_for_mode (outer_mode, 1);
13726 if (type_die == NULL)
13727 return NULL;
13728 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13729 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13730 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13731 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13732 add_loc_descr (&op, cvt);
13733 return op;
13736 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13738 static dw_loc_descr_ref
13739 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13740 dw_loc_descr_ref op1)
13742 dw_loc_descr_ref ret = op0;
13743 add_loc_descr (&ret, op1);
13744 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13745 if (STORE_FLAG_VALUE != 1)
13747 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13748 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13750 return ret;
13753 /* Return location descriptor for signed comparison OP RTL. */
13755 static dw_loc_descr_ref
13756 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13757 machine_mode mem_mode)
13759 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13760 dw_loc_descr_ref op0, op1;
13761 int shift;
13763 if (op_mode == VOIDmode)
13764 op_mode = GET_MODE (XEXP (rtl, 1));
13765 if (op_mode == VOIDmode)
13766 return NULL;
13768 if (dwarf_strict
13769 && dwarf_version < 5
13770 && (!SCALAR_INT_MODE_P (op_mode)
13771 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13772 return NULL;
13774 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13775 VAR_INIT_STATUS_INITIALIZED);
13776 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13777 VAR_INIT_STATUS_INITIALIZED);
13779 if (op0 == NULL || op1 == NULL)
13780 return NULL;
13782 if (!SCALAR_INT_MODE_P (op_mode)
13783 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13784 return compare_loc_descriptor (op, op0, op1);
13786 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13788 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13789 dw_loc_descr_ref cvt;
13791 if (type_die == NULL)
13792 return NULL;
13793 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13794 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13795 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13796 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13797 add_loc_descr (&op0, cvt);
13798 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13799 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13800 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13801 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13802 add_loc_descr (&op1, cvt);
13803 return compare_loc_descriptor (op, op0, op1);
13806 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13807 /* For eq/ne, if the operands are known to be zero-extended,
13808 there is no need to do the fancy shifting up. */
13809 if (op == DW_OP_eq || op == DW_OP_ne)
13811 dw_loc_descr_ref last0, last1;
13812 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13814 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13816 /* deref_size zero extends, and for constants we can check
13817 whether they are zero extended or not. */
13818 if (((last0->dw_loc_opc == DW_OP_deref_size
13819 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13820 || (CONST_INT_P (XEXP (rtl, 0))
13821 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13822 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13823 && ((last1->dw_loc_opc == DW_OP_deref_size
13824 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13825 || (CONST_INT_P (XEXP (rtl, 1))
13826 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13827 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13828 return compare_loc_descriptor (op, op0, op1);
13830 /* EQ/NE comparison against constant in narrower type than
13831 DWARF2_ADDR_SIZE can be performed either as
13832 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13833 DW_OP_{eq,ne}
13835 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13836 DW_OP_{eq,ne}. Pick whatever is shorter. */
13837 if (CONST_INT_P (XEXP (rtl, 1))
13838 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13839 && (size_of_int_loc_descriptor (shift) + 1
13840 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
13841 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13842 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13843 & GET_MODE_MASK (op_mode))))
13845 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13846 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13847 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13848 & GET_MODE_MASK (op_mode));
13849 return compare_loc_descriptor (op, op0, op1);
13852 add_loc_descr (&op0, int_loc_descriptor (shift));
13853 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13854 if (CONST_INT_P (XEXP (rtl, 1)))
13855 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13856 else
13858 add_loc_descr (&op1, int_loc_descriptor (shift));
13859 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13861 return compare_loc_descriptor (op, op0, op1);
13864 /* Return location descriptor for unsigned comparison OP RTL. */
13866 static dw_loc_descr_ref
13867 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13868 machine_mode mem_mode)
13870 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13871 dw_loc_descr_ref op0, op1;
13873 if (op_mode == VOIDmode)
13874 op_mode = GET_MODE (XEXP (rtl, 1));
13875 if (op_mode == VOIDmode)
13876 return NULL;
13877 if (!SCALAR_INT_MODE_P (op_mode))
13878 return NULL;
13880 if (dwarf_strict
13881 && dwarf_version < 5
13882 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13883 return NULL;
13885 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13886 VAR_INIT_STATUS_INITIALIZED);
13887 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13888 VAR_INIT_STATUS_INITIALIZED);
13890 if (op0 == NULL || op1 == NULL)
13891 return NULL;
13893 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13895 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13896 dw_loc_descr_ref last0, last1;
13897 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13899 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13901 if (CONST_INT_P (XEXP (rtl, 0)))
13902 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13903 /* deref_size zero extends, so no need to mask it again. */
13904 else if (last0->dw_loc_opc != DW_OP_deref_size
13905 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13907 add_loc_descr (&op0, int_loc_descriptor (mask));
13908 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13910 if (CONST_INT_P (XEXP (rtl, 1)))
13911 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13912 /* deref_size zero extends, so no need to mask it again. */
13913 else if (last1->dw_loc_opc != DW_OP_deref_size
13914 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13916 add_loc_descr (&op1, int_loc_descriptor (mask));
13917 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13920 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13922 HOST_WIDE_INT bias = 1;
13923 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13924 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13925 if (CONST_INT_P (XEXP (rtl, 1)))
13926 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13927 + INTVAL (XEXP (rtl, 1)));
13928 else
13929 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13930 bias, 0));
13932 return compare_loc_descriptor (op, op0, op1);
13935 /* Return location descriptor for {U,S}{MIN,MAX}. */
13937 static dw_loc_descr_ref
13938 minmax_loc_descriptor (rtx rtl, machine_mode mode,
13939 machine_mode mem_mode)
13941 enum dwarf_location_atom op;
13942 dw_loc_descr_ref op0, op1, ret;
13943 dw_loc_descr_ref bra_node, drop_node;
13945 if (dwarf_strict
13946 && dwarf_version < 5
13947 && (!SCALAR_INT_MODE_P (mode)
13948 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
13949 return NULL;
13951 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13952 VAR_INIT_STATUS_INITIALIZED);
13953 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13954 VAR_INIT_STATUS_INITIALIZED);
13956 if (op0 == NULL || op1 == NULL)
13957 return NULL;
13959 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13960 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13961 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13962 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13964 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13966 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
13967 add_loc_descr (&op0, int_loc_descriptor (mask));
13968 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13969 add_loc_descr (&op1, int_loc_descriptor (mask));
13970 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13972 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13974 HOST_WIDE_INT bias = 1;
13975 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13976 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13977 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13980 else if (!SCALAR_INT_MODE_P (mode)
13981 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13983 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
13984 add_loc_descr (&op0, int_loc_descriptor (shift));
13985 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13986 add_loc_descr (&op1, int_loc_descriptor (shift));
13987 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13989 else if (SCALAR_INT_MODE_P (mode)
13990 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13992 dw_die_ref type_die = base_type_for_mode (mode, 0);
13993 dw_loc_descr_ref cvt;
13994 if (type_die == NULL)
13995 return NULL;
13996 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13997 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13998 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13999 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14000 add_loc_descr (&op0, cvt);
14001 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14002 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14003 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14004 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14005 add_loc_descr (&op1, cvt);
14008 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14009 op = DW_OP_lt;
14010 else
14011 op = DW_OP_gt;
14012 ret = op0;
14013 add_loc_descr (&ret, op1);
14014 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14015 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14016 add_loc_descr (&ret, bra_node);
14017 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14018 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14019 add_loc_descr (&ret, drop_node);
14020 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14021 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14022 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14023 && SCALAR_INT_MODE_P (mode)
14024 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14025 ret = convert_descriptor_to_mode (mode, ret);
14026 return ret;
14029 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14030 but after converting arguments to type_die, afterwards
14031 convert back to unsigned. */
14033 static dw_loc_descr_ref
14034 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14035 machine_mode mode, machine_mode mem_mode)
14037 dw_loc_descr_ref cvt, op0, op1;
14039 if (type_die == NULL)
14040 return NULL;
14041 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14042 VAR_INIT_STATUS_INITIALIZED);
14043 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14044 VAR_INIT_STATUS_INITIALIZED);
14045 if (op0 == NULL || op1 == NULL)
14046 return NULL;
14047 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14048 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14049 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14050 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14051 add_loc_descr (&op0, cvt);
14052 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14053 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14054 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14055 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14056 add_loc_descr (&op1, cvt);
14057 add_loc_descr (&op0, op1);
14058 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14059 return convert_descriptor_to_mode (mode, op0);
14062 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14063 const0 is DW_OP_lit0 or corresponding typed constant,
14064 const1 is DW_OP_lit1 or corresponding typed constant
14065 and constMSB is constant with just the MSB bit set
14066 for the mode):
14067 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14068 L1: const0 DW_OP_swap
14069 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14070 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14071 L3: DW_OP_drop
14072 L4: DW_OP_nop
14074 CTZ is similar:
14075 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14076 L1: const0 DW_OP_swap
14077 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14078 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14079 L3: DW_OP_drop
14080 L4: DW_OP_nop
14082 FFS is similar:
14083 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14084 L1: const1 DW_OP_swap
14085 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14086 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14087 L3: DW_OP_drop
14088 L4: DW_OP_nop */
14090 static dw_loc_descr_ref
14091 clz_loc_descriptor (rtx rtl, machine_mode mode,
14092 machine_mode mem_mode)
14094 dw_loc_descr_ref op0, ret, tmp;
14095 HOST_WIDE_INT valv;
14096 dw_loc_descr_ref l1jump, l1label;
14097 dw_loc_descr_ref l2jump, l2label;
14098 dw_loc_descr_ref l3jump, l3label;
14099 dw_loc_descr_ref l4jump, l4label;
14100 rtx msb;
14102 if (!SCALAR_INT_MODE_P (mode)
14103 || GET_MODE (XEXP (rtl, 0)) != mode)
14104 return NULL;
14106 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14107 VAR_INIT_STATUS_INITIALIZED);
14108 if (op0 == NULL)
14109 return NULL;
14110 ret = op0;
14111 if (GET_CODE (rtl) == CLZ)
14113 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14114 valv = GET_MODE_BITSIZE (mode);
14116 else if (GET_CODE (rtl) == FFS)
14117 valv = 0;
14118 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14119 valv = GET_MODE_BITSIZE (mode);
14120 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14121 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14122 add_loc_descr (&ret, l1jump);
14123 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14124 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14125 VAR_INIT_STATUS_INITIALIZED);
14126 if (tmp == NULL)
14127 return NULL;
14128 add_loc_descr (&ret, tmp);
14129 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14130 add_loc_descr (&ret, l4jump);
14131 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14132 ? const1_rtx : const0_rtx,
14133 mode, mem_mode,
14134 VAR_INIT_STATUS_INITIALIZED);
14135 if (l1label == NULL)
14136 return NULL;
14137 add_loc_descr (&ret, l1label);
14138 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14139 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14140 add_loc_descr (&ret, l2label);
14141 if (GET_CODE (rtl) != CLZ)
14142 msb = const1_rtx;
14143 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14144 msb = GEN_INT (HOST_WIDE_INT_1U
14145 << (GET_MODE_BITSIZE (mode) - 1));
14146 else
14147 msb = immed_wide_int_const
14148 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14149 GET_MODE_PRECISION (mode)), mode);
14150 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14151 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14152 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14153 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14154 else
14155 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14156 VAR_INIT_STATUS_INITIALIZED);
14157 if (tmp == NULL)
14158 return NULL;
14159 add_loc_descr (&ret, tmp);
14160 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14161 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14162 add_loc_descr (&ret, l3jump);
14163 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14164 VAR_INIT_STATUS_INITIALIZED);
14165 if (tmp == NULL)
14166 return NULL;
14167 add_loc_descr (&ret, tmp);
14168 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14169 ? DW_OP_shl : DW_OP_shr, 0, 0));
14170 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14171 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14172 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14173 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14174 add_loc_descr (&ret, l2jump);
14175 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14176 add_loc_descr (&ret, l3label);
14177 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14178 add_loc_descr (&ret, l4label);
14179 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14180 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14181 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14182 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14183 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14184 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14185 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14186 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14187 return ret;
14190 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14191 const1 is DW_OP_lit1 or corresponding typed constant):
14192 const0 DW_OP_swap
14193 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14194 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14195 L2: DW_OP_drop
14197 PARITY is similar:
14198 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14199 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14200 L2: DW_OP_drop */
14202 static dw_loc_descr_ref
14203 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14204 machine_mode mem_mode)
14206 dw_loc_descr_ref op0, ret, tmp;
14207 dw_loc_descr_ref l1jump, l1label;
14208 dw_loc_descr_ref l2jump, l2label;
14210 if (!SCALAR_INT_MODE_P (mode)
14211 || GET_MODE (XEXP (rtl, 0)) != mode)
14212 return NULL;
14214 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14215 VAR_INIT_STATUS_INITIALIZED);
14216 if (op0 == NULL)
14217 return NULL;
14218 ret = op0;
14219 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14220 VAR_INIT_STATUS_INITIALIZED);
14221 if (tmp == NULL)
14222 return NULL;
14223 add_loc_descr (&ret, tmp);
14224 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14225 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14226 add_loc_descr (&ret, l1label);
14227 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14228 add_loc_descr (&ret, l2jump);
14229 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14230 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14231 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14232 VAR_INIT_STATUS_INITIALIZED);
14233 if (tmp == NULL)
14234 return NULL;
14235 add_loc_descr (&ret, tmp);
14236 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14237 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14238 ? DW_OP_plus : DW_OP_xor, 0, 0));
14239 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14240 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14241 VAR_INIT_STATUS_INITIALIZED);
14242 add_loc_descr (&ret, tmp);
14243 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14244 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14245 add_loc_descr (&ret, l1jump);
14246 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14247 add_loc_descr (&ret, l2label);
14248 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14249 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14250 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14251 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14252 return ret;
14255 /* BSWAP (constS is initial shift count, either 56 or 24):
14256 constS const0
14257 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14258 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14259 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14260 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14261 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14263 static dw_loc_descr_ref
14264 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14265 machine_mode mem_mode)
14267 dw_loc_descr_ref op0, ret, tmp;
14268 dw_loc_descr_ref l1jump, l1label;
14269 dw_loc_descr_ref l2jump, l2label;
14271 if (!SCALAR_INT_MODE_P (mode)
14272 || BITS_PER_UNIT != 8
14273 || (GET_MODE_BITSIZE (mode) != 32
14274 && GET_MODE_BITSIZE (mode) != 64))
14275 return NULL;
14277 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14278 VAR_INIT_STATUS_INITIALIZED);
14279 if (op0 == NULL)
14280 return NULL;
14282 ret = op0;
14283 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14284 mode, mem_mode,
14285 VAR_INIT_STATUS_INITIALIZED);
14286 if (tmp == NULL)
14287 return NULL;
14288 add_loc_descr (&ret, tmp);
14289 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14290 VAR_INIT_STATUS_INITIALIZED);
14291 if (tmp == NULL)
14292 return NULL;
14293 add_loc_descr (&ret, tmp);
14294 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14295 add_loc_descr (&ret, l1label);
14296 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14297 mode, mem_mode,
14298 VAR_INIT_STATUS_INITIALIZED);
14299 add_loc_descr (&ret, tmp);
14300 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14301 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14302 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14303 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14304 VAR_INIT_STATUS_INITIALIZED);
14305 if (tmp == NULL)
14306 return NULL;
14307 add_loc_descr (&ret, tmp);
14308 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14309 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14310 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14311 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14312 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14313 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14314 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14315 VAR_INIT_STATUS_INITIALIZED);
14316 add_loc_descr (&ret, tmp);
14317 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14318 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14319 add_loc_descr (&ret, l2jump);
14320 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14321 VAR_INIT_STATUS_INITIALIZED);
14322 add_loc_descr (&ret, tmp);
14323 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14325 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14326 add_loc_descr (&ret, l1jump);
14327 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14328 add_loc_descr (&ret, l2label);
14329 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14330 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14331 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14332 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14333 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14334 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14335 return ret;
14338 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14339 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14340 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14341 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14343 ROTATERT is similar:
14344 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14345 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14346 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14348 static dw_loc_descr_ref
14349 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14350 machine_mode mem_mode)
14352 rtx rtlop1 = XEXP (rtl, 1);
14353 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14354 int i;
14356 if (!SCALAR_INT_MODE_P (mode))
14357 return NULL;
14359 if (GET_MODE (rtlop1) != VOIDmode
14360 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14361 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14362 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14363 VAR_INIT_STATUS_INITIALIZED);
14364 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14365 VAR_INIT_STATUS_INITIALIZED);
14366 if (op0 == NULL || op1 == NULL)
14367 return NULL;
14368 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14369 for (i = 0; i < 2; i++)
14371 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14372 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14373 mode, mem_mode,
14374 VAR_INIT_STATUS_INITIALIZED);
14375 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14376 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14377 ? DW_OP_const4u
14378 : HOST_BITS_PER_WIDE_INT == 64
14379 ? DW_OP_const8u : DW_OP_constu,
14380 GET_MODE_MASK (mode), 0);
14381 else
14382 mask[i] = NULL;
14383 if (mask[i] == NULL)
14384 return NULL;
14385 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14387 ret = op0;
14388 add_loc_descr (&ret, op1);
14389 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14390 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14391 if (GET_CODE (rtl) == ROTATERT)
14393 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14394 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14395 GET_MODE_BITSIZE (mode), 0));
14397 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14398 if (mask[0] != NULL)
14399 add_loc_descr (&ret, mask[0]);
14400 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14401 if (mask[1] != NULL)
14403 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14404 add_loc_descr (&ret, mask[1]);
14405 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14407 if (GET_CODE (rtl) == ROTATE)
14409 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14410 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14411 GET_MODE_BITSIZE (mode), 0));
14413 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14414 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14415 return ret;
14418 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14419 for DEBUG_PARAMETER_REF RTL. */
14421 static dw_loc_descr_ref
14422 parameter_ref_descriptor (rtx rtl)
14424 dw_loc_descr_ref ret;
14425 dw_die_ref ref;
14427 if (dwarf_strict)
14428 return NULL;
14429 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14430 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14431 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14432 if (ref)
14434 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14435 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14436 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14438 else
14440 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14441 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14443 return ret;
14446 /* The following routine converts the RTL for a variable or parameter
14447 (resident in memory) into an equivalent Dwarf representation of a
14448 mechanism for getting the address of that same variable onto the top of a
14449 hypothetical "address evaluation" stack.
14451 When creating memory location descriptors, we are effectively transforming
14452 the RTL for a memory-resident object into its Dwarf postfix expression
14453 equivalent. This routine recursively descends an RTL tree, turning
14454 it into Dwarf postfix code as it goes.
14456 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14458 MEM_MODE is the mode of the memory reference, needed to handle some
14459 autoincrement addressing modes.
14461 Return 0 if we can't represent the location. */
14463 dw_loc_descr_ref
14464 mem_loc_descriptor (rtx rtl, machine_mode mode,
14465 machine_mode mem_mode,
14466 enum var_init_status initialized)
14468 dw_loc_descr_ref mem_loc_result = NULL;
14469 enum dwarf_location_atom op;
14470 dw_loc_descr_ref op0, op1;
14471 rtx inner = NULL_RTX;
14473 if (mode == VOIDmode)
14474 mode = GET_MODE (rtl);
14476 /* Note that for a dynamically sized array, the location we will generate a
14477 description of here will be the lowest numbered location which is
14478 actually within the array. That's *not* necessarily the same as the
14479 zeroth element of the array. */
14481 rtl = targetm.delegitimize_address (rtl);
14483 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14484 return NULL;
14486 switch (GET_CODE (rtl))
14488 case POST_INC:
14489 case POST_DEC:
14490 case POST_MODIFY:
14491 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14493 case SUBREG:
14494 /* The case of a subreg may arise when we have a local (register)
14495 variable or a formal (register) parameter which doesn't quite fill
14496 up an entire register. For now, just assume that it is
14497 legitimate to make the Dwarf info refer to the whole register which
14498 contains the given subreg. */
14499 if (!subreg_lowpart_p (rtl))
14500 break;
14501 inner = SUBREG_REG (rtl);
14502 /* FALLTHRU */
14503 case TRUNCATE:
14504 if (inner == NULL_RTX)
14505 inner = XEXP (rtl, 0);
14506 if (SCALAR_INT_MODE_P (mode)
14507 && SCALAR_INT_MODE_P (GET_MODE (inner))
14508 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14509 #ifdef POINTERS_EXTEND_UNSIGNED
14510 || (mode == Pmode && mem_mode != VOIDmode)
14511 #endif
14513 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14515 mem_loc_result = mem_loc_descriptor (inner,
14516 GET_MODE (inner),
14517 mem_mode, initialized);
14518 break;
14520 if (dwarf_strict && dwarf_version < 5)
14521 break;
14522 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14523 break;
14524 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14525 && (!SCALAR_INT_MODE_P (mode)
14526 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14527 break;
14528 else
14530 dw_die_ref type_die;
14531 dw_loc_descr_ref cvt;
14533 mem_loc_result = mem_loc_descriptor (inner,
14534 GET_MODE (inner),
14535 mem_mode, initialized);
14536 if (mem_loc_result == NULL)
14537 break;
14538 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14539 if (type_die == NULL)
14541 mem_loc_result = NULL;
14542 break;
14544 if (GET_MODE_SIZE (mode)
14545 != GET_MODE_SIZE (GET_MODE (inner)))
14546 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14547 else
14548 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14549 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14550 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14551 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14552 add_loc_descr (&mem_loc_result, cvt);
14553 if (SCALAR_INT_MODE_P (mode)
14554 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14556 /* Convert it to untyped afterwards. */
14557 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14558 add_loc_descr (&mem_loc_result, cvt);
14561 break;
14563 case REG:
14564 if (! SCALAR_INT_MODE_P (mode)
14565 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14566 && rtl != arg_pointer_rtx
14567 && rtl != frame_pointer_rtx
14568 #ifdef POINTERS_EXTEND_UNSIGNED
14569 && (mode != Pmode || mem_mode == VOIDmode)
14570 #endif
14573 dw_die_ref type_die;
14574 unsigned int dbx_regnum;
14576 if (dwarf_strict && dwarf_version < 5)
14577 break;
14578 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14579 break;
14580 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14581 if (type_die == NULL)
14582 break;
14584 dbx_regnum = dbx_reg_number (rtl);
14585 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14586 break;
14587 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14588 dbx_regnum, 0);
14589 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14590 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14591 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14592 break;
14594 /* Whenever a register number forms a part of the description of the
14595 method for calculating the (dynamic) address of a memory resident
14596 object, DWARF rules require the register number be referred to as
14597 a "base register". This distinction is not based in any way upon
14598 what category of register the hardware believes the given register
14599 belongs to. This is strictly DWARF terminology we're dealing with
14600 here. Note that in cases where the location of a memory-resident
14601 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14602 OP_CONST (0)) the actual DWARF location descriptor that we generate
14603 may just be OP_BASEREG (basereg). This may look deceptively like
14604 the object in question was allocated to a register (rather than in
14605 memory) so DWARF consumers need to be aware of the subtle
14606 distinction between OP_REG and OP_BASEREG. */
14607 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14608 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14609 else if (stack_realign_drap
14610 && crtl->drap_reg
14611 && crtl->args.internal_arg_pointer == rtl
14612 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14614 /* If RTL is internal_arg_pointer, which has been optimized
14615 out, use DRAP instead. */
14616 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14617 VAR_INIT_STATUS_INITIALIZED);
14619 break;
14621 case SIGN_EXTEND:
14622 case ZERO_EXTEND:
14623 if (!SCALAR_INT_MODE_P (mode))
14624 break;
14625 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14626 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14627 if (op0 == 0)
14628 break;
14629 else if (GET_CODE (rtl) == ZERO_EXTEND
14630 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14631 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14632 < HOST_BITS_PER_WIDE_INT
14633 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14634 to expand zero extend as two shifts instead of
14635 masking. */
14636 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14638 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14639 mem_loc_result = op0;
14640 add_loc_descr (&mem_loc_result,
14641 int_loc_descriptor (GET_MODE_MASK (imode)));
14642 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14644 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14646 int shift = DWARF2_ADDR_SIZE
14647 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14648 shift *= BITS_PER_UNIT;
14649 if (GET_CODE (rtl) == SIGN_EXTEND)
14650 op = DW_OP_shra;
14651 else
14652 op = DW_OP_shr;
14653 mem_loc_result = op0;
14654 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14655 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14656 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14657 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14659 else if (!dwarf_strict || dwarf_version >= 5)
14661 dw_die_ref type_die1, type_die2;
14662 dw_loc_descr_ref cvt;
14664 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14665 GET_CODE (rtl) == ZERO_EXTEND);
14666 if (type_die1 == NULL)
14667 break;
14668 type_die2 = base_type_for_mode (mode, 1);
14669 if (type_die2 == NULL)
14670 break;
14671 mem_loc_result = op0;
14672 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14673 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14674 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14675 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14676 add_loc_descr (&mem_loc_result, cvt);
14677 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14678 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14679 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14680 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14681 add_loc_descr (&mem_loc_result, cvt);
14683 break;
14685 case MEM:
14687 rtx new_rtl = avoid_constant_pool_reference (rtl);
14688 if (new_rtl != rtl)
14690 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14691 initialized);
14692 if (mem_loc_result != NULL)
14693 return mem_loc_result;
14696 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14697 get_address_mode (rtl), mode,
14698 VAR_INIT_STATUS_INITIALIZED);
14699 if (mem_loc_result == NULL)
14700 mem_loc_result = tls_mem_loc_descriptor (rtl);
14701 if (mem_loc_result != NULL)
14703 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14704 || !SCALAR_INT_MODE_P(mode))
14706 dw_die_ref type_die;
14707 dw_loc_descr_ref deref;
14709 if (dwarf_strict && dwarf_version < 5)
14710 return NULL;
14711 type_die
14712 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14713 if (type_die == NULL)
14714 return NULL;
14715 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14716 GET_MODE_SIZE (mode), 0);
14717 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14718 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14719 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14720 add_loc_descr (&mem_loc_result, deref);
14722 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14723 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14724 else
14725 add_loc_descr (&mem_loc_result,
14726 new_loc_descr (DW_OP_deref_size,
14727 GET_MODE_SIZE (mode), 0));
14729 break;
14731 case LO_SUM:
14732 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14734 case LABEL_REF:
14735 /* Some ports can transform a symbol ref into a label ref, because
14736 the symbol ref is too far away and has to be dumped into a constant
14737 pool. */
14738 case CONST:
14739 case SYMBOL_REF:
14740 if (!SCALAR_INT_MODE_P (mode)
14741 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14742 #ifdef POINTERS_EXTEND_UNSIGNED
14743 && (mode != Pmode || mem_mode == VOIDmode)
14744 #endif
14746 break;
14747 if (GET_CODE (rtl) == SYMBOL_REF
14748 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14750 dw_loc_descr_ref temp;
14752 /* If this is not defined, we have no way to emit the data. */
14753 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14754 break;
14756 temp = new_addr_loc_descr (rtl, dtprel_true);
14758 /* We check for DWARF 5 here because gdb did not implement
14759 DW_OP_form_tls_address until after 7.12. */
14760 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14761 ? DW_OP_form_tls_address
14762 : DW_OP_GNU_push_tls_address),
14763 0, 0);
14764 add_loc_descr (&mem_loc_result, temp);
14766 break;
14769 if (!const_ok_for_output (rtl))
14771 if (GET_CODE (rtl) == CONST)
14772 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14773 initialized);
14774 break;
14777 symref:
14778 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14779 vec_safe_push (used_rtx_array, rtl);
14780 break;
14782 case CONCAT:
14783 case CONCATN:
14784 case VAR_LOCATION:
14785 case DEBUG_IMPLICIT_PTR:
14786 expansion_failed (NULL_TREE, rtl,
14787 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14788 return 0;
14790 case ENTRY_VALUE:
14791 if (dwarf_strict && dwarf_version < 5)
14792 return NULL;
14793 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14795 if (!SCALAR_INT_MODE_P (mode)
14796 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14797 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14798 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14799 else
14801 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14802 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14803 return NULL;
14804 op0 = one_reg_loc_descriptor (dbx_regnum,
14805 VAR_INIT_STATUS_INITIALIZED);
14808 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14809 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14811 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14812 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14813 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14814 return NULL;
14816 else
14817 gcc_unreachable ();
14818 if (op0 == NULL)
14819 return NULL;
14820 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14821 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14822 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14823 break;
14825 case DEBUG_PARAMETER_REF:
14826 mem_loc_result = parameter_ref_descriptor (rtl);
14827 break;
14829 case PRE_MODIFY:
14830 /* Extract the PLUS expression nested inside and fall into
14831 PLUS code below. */
14832 rtl = XEXP (rtl, 1);
14833 goto plus;
14835 case PRE_INC:
14836 case PRE_DEC:
14837 /* Turn these into a PLUS expression and fall into the PLUS code
14838 below. */
14839 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14840 gen_int_mode (GET_CODE (rtl) == PRE_INC
14841 ? GET_MODE_UNIT_SIZE (mem_mode)
14842 : -GET_MODE_UNIT_SIZE (mem_mode),
14843 mode));
14845 /* fall through */
14847 case PLUS:
14848 plus:
14849 if (is_based_loc (rtl)
14850 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14851 || XEXP (rtl, 0) == arg_pointer_rtx
14852 || XEXP (rtl, 0) == frame_pointer_rtx)
14853 && SCALAR_INT_MODE_P (mode))
14854 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14855 INTVAL (XEXP (rtl, 1)),
14856 VAR_INIT_STATUS_INITIALIZED);
14857 else
14859 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14860 VAR_INIT_STATUS_INITIALIZED);
14861 if (mem_loc_result == 0)
14862 break;
14864 if (CONST_INT_P (XEXP (rtl, 1))
14865 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14866 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14867 else
14869 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14870 VAR_INIT_STATUS_INITIALIZED);
14871 if (op1 == 0)
14872 return NULL;
14873 add_loc_descr (&mem_loc_result, op1);
14874 add_loc_descr (&mem_loc_result,
14875 new_loc_descr (DW_OP_plus, 0, 0));
14878 break;
14880 /* If a pseudo-reg is optimized away, it is possible for it to
14881 be replaced with a MEM containing a multiply or shift. */
14882 case MINUS:
14883 op = DW_OP_minus;
14884 goto do_binop;
14886 case MULT:
14887 op = DW_OP_mul;
14888 goto do_binop;
14890 case DIV:
14891 if ((!dwarf_strict || dwarf_version >= 5)
14892 && SCALAR_INT_MODE_P (mode)
14893 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14895 mem_loc_result = typed_binop (DW_OP_div, rtl,
14896 base_type_for_mode (mode, 0),
14897 mode, mem_mode);
14898 break;
14900 op = DW_OP_div;
14901 goto do_binop;
14903 case UMOD:
14904 op = DW_OP_mod;
14905 goto do_binop;
14907 case ASHIFT:
14908 op = DW_OP_shl;
14909 goto do_shift;
14911 case ASHIFTRT:
14912 op = DW_OP_shra;
14913 goto do_shift;
14915 case LSHIFTRT:
14916 op = DW_OP_shr;
14917 goto do_shift;
14919 do_shift:
14920 if (!SCALAR_INT_MODE_P (mode))
14921 break;
14922 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14923 VAR_INIT_STATUS_INITIALIZED);
14925 rtx rtlop1 = XEXP (rtl, 1);
14926 if (GET_MODE (rtlop1) != VOIDmode
14927 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
14928 < GET_MODE_BITSIZE (mode))
14929 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14930 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14931 VAR_INIT_STATUS_INITIALIZED);
14934 if (op0 == 0 || op1 == 0)
14935 break;
14937 mem_loc_result = op0;
14938 add_loc_descr (&mem_loc_result, op1);
14939 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14940 break;
14942 case AND:
14943 op = DW_OP_and;
14944 goto do_binop;
14946 case IOR:
14947 op = DW_OP_or;
14948 goto do_binop;
14950 case XOR:
14951 op = DW_OP_xor;
14952 goto do_binop;
14954 do_binop:
14955 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14956 VAR_INIT_STATUS_INITIALIZED);
14957 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14958 VAR_INIT_STATUS_INITIALIZED);
14960 if (op0 == 0 || op1 == 0)
14961 break;
14963 mem_loc_result = op0;
14964 add_loc_descr (&mem_loc_result, op1);
14965 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14966 break;
14968 case MOD:
14969 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14970 && (!dwarf_strict || dwarf_version >= 5))
14972 mem_loc_result = typed_binop (DW_OP_mod, rtl,
14973 base_type_for_mode (mode, 0),
14974 mode, mem_mode);
14975 break;
14978 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14979 VAR_INIT_STATUS_INITIALIZED);
14980 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14981 VAR_INIT_STATUS_INITIALIZED);
14983 if (op0 == 0 || op1 == 0)
14984 break;
14986 mem_loc_result = op0;
14987 add_loc_descr (&mem_loc_result, op1);
14988 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
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_div, 0, 0));
14991 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14992 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14993 break;
14995 case UDIV:
14996 if ((!dwarf_strict || dwarf_version >= 5)
14997 && SCALAR_INT_MODE_P (mode))
14999 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15001 op = DW_OP_div;
15002 goto do_binop;
15004 mem_loc_result = typed_binop (DW_OP_div, rtl,
15005 base_type_for_mode (mode, 1),
15006 mode, mem_mode);
15008 break;
15010 case NOT:
15011 op = DW_OP_not;
15012 goto do_unop;
15014 case ABS:
15015 op = DW_OP_abs;
15016 goto do_unop;
15018 case NEG:
15019 op = DW_OP_neg;
15020 goto do_unop;
15022 do_unop:
15023 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15024 VAR_INIT_STATUS_INITIALIZED);
15026 if (op0 == 0)
15027 break;
15029 mem_loc_result = op0;
15030 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15031 break;
15033 case CONST_INT:
15034 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15035 #ifdef POINTERS_EXTEND_UNSIGNED
15036 || (mode == Pmode
15037 && mem_mode != VOIDmode
15038 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15039 #endif
15042 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15043 break;
15045 if ((!dwarf_strict || dwarf_version >= 5)
15046 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15047 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15049 dw_die_ref type_die = base_type_for_mode (mode, 1);
15050 machine_mode amode;
15051 if (type_die == NULL)
15052 return NULL;
15053 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15054 MODE_INT, 0);
15055 if (INTVAL (rtl) >= 0
15056 && amode != BLKmode
15057 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15058 /* const DW_OP_convert <XXX> vs.
15059 DW_OP_const_type <XXX, 1, const>. */
15060 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15061 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15063 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15064 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15065 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15066 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15067 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15068 add_loc_descr (&mem_loc_result, op0);
15069 return mem_loc_result;
15071 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15072 INTVAL (rtl));
15073 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15074 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15075 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15076 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15077 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15078 else
15080 mem_loc_result->dw_loc_oprnd2.val_class
15081 = dw_val_class_const_double;
15082 mem_loc_result->dw_loc_oprnd2.v.val_double
15083 = double_int::from_shwi (INTVAL (rtl));
15086 break;
15088 case CONST_DOUBLE:
15089 if (!dwarf_strict || dwarf_version >= 5)
15091 dw_die_ref type_die;
15093 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15094 CONST_DOUBLE rtx could represent either a large integer
15095 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15096 the value is always a floating point constant.
15098 When it is an integer, a CONST_DOUBLE is used whenever
15099 the constant requires 2 HWIs to be adequately represented.
15100 We output CONST_DOUBLEs as blocks. */
15101 if (mode == VOIDmode
15102 || (GET_MODE (rtl) == VOIDmode
15103 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15104 break;
15105 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15106 if (type_die == NULL)
15107 return NULL;
15108 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15109 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15110 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15111 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15112 #if TARGET_SUPPORTS_WIDE_INT == 0
15113 if (!SCALAR_FLOAT_MODE_P (mode))
15115 mem_loc_result->dw_loc_oprnd2.val_class
15116 = dw_val_class_const_double;
15117 mem_loc_result->dw_loc_oprnd2.v.val_double
15118 = rtx_to_double_int (rtl);
15120 else
15121 #endif
15123 unsigned int length = GET_MODE_SIZE (mode);
15124 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15126 insert_float (rtl, array);
15127 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15128 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15129 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15130 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15133 break;
15135 case CONST_WIDE_INT:
15136 if (!dwarf_strict || dwarf_version >= 5)
15138 dw_die_ref type_die;
15140 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15141 if (type_die == NULL)
15142 return NULL;
15143 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15144 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15145 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15146 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15147 mem_loc_result->dw_loc_oprnd2.val_class
15148 = dw_val_class_wide_int;
15149 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15150 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15152 break;
15154 case EQ:
15155 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15156 break;
15158 case GE:
15159 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15160 break;
15162 case GT:
15163 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15164 break;
15166 case LE:
15167 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15168 break;
15170 case LT:
15171 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15172 break;
15174 case NE:
15175 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15176 break;
15178 case GEU:
15179 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15180 break;
15182 case GTU:
15183 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15184 break;
15186 case LEU:
15187 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15188 break;
15190 case LTU:
15191 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15192 break;
15194 case UMIN:
15195 case UMAX:
15196 if (!SCALAR_INT_MODE_P (mode))
15197 break;
15198 /* FALLTHRU */
15199 case SMIN:
15200 case SMAX:
15201 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15202 break;
15204 case ZERO_EXTRACT:
15205 case SIGN_EXTRACT:
15206 if (CONST_INT_P (XEXP (rtl, 1))
15207 && CONST_INT_P (XEXP (rtl, 2))
15208 && ((unsigned) INTVAL (XEXP (rtl, 1))
15209 + (unsigned) INTVAL (XEXP (rtl, 2))
15210 <= GET_MODE_BITSIZE (mode))
15211 && SCALAR_INT_MODE_P (mode)
15212 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15213 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15215 int shift, size;
15216 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15217 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15218 if (op0 == 0)
15219 break;
15220 if (GET_CODE (rtl) == SIGN_EXTRACT)
15221 op = DW_OP_shra;
15222 else
15223 op = DW_OP_shr;
15224 mem_loc_result = op0;
15225 size = INTVAL (XEXP (rtl, 1));
15226 shift = INTVAL (XEXP (rtl, 2));
15227 if (BITS_BIG_ENDIAN)
15228 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15229 - shift - size;
15230 if (shift + size != (int) DWARF2_ADDR_SIZE)
15232 add_loc_descr (&mem_loc_result,
15233 int_loc_descriptor (DWARF2_ADDR_SIZE
15234 - shift - size));
15235 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15237 if (size != (int) DWARF2_ADDR_SIZE)
15239 add_loc_descr (&mem_loc_result,
15240 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15241 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15244 break;
15246 case IF_THEN_ELSE:
15248 dw_loc_descr_ref op2, bra_node, drop_node;
15249 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15250 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15251 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15252 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15253 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15254 VAR_INIT_STATUS_INITIALIZED);
15255 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15256 VAR_INIT_STATUS_INITIALIZED);
15257 if (op0 == NULL || op1 == NULL || op2 == NULL)
15258 break;
15260 mem_loc_result = op1;
15261 add_loc_descr (&mem_loc_result, op2);
15262 add_loc_descr (&mem_loc_result, op0);
15263 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15264 add_loc_descr (&mem_loc_result, bra_node);
15265 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15266 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15267 add_loc_descr (&mem_loc_result, drop_node);
15268 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15269 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15271 break;
15273 case FLOAT_EXTEND:
15274 case FLOAT_TRUNCATE:
15275 case FLOAT:
15276 case UNSIGNED_FLOAT:
15277 case FIX:
15278 case UNSIGNED_FIX:
15279 if (!dwarf_strict || dwarf_version >= 5)
15281 dw_die_ref type_die;
15282 dw_loc_descr_ref cvt;
15284 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15285 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15286 if (op0 == NULL)
15287 break;
15288 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15289 && (GET_CODE (rtl) == FLOAT
15290 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15291 <= DWARF2_ADDR_SIZE))
15293 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15294 GET_CODE (rtl) == UNSIGNED_FLOAT);
15295 if (type_die == NULL)
15296 break;
15297 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15298 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15299 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15300 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15301 add_loc_descr (&op0, cvt);
15303 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15304 if (type_die == NULL)
15305 break;
15306 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15307 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15308 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15309 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15310 add_loc_descr (&op0, cvt);
15311 if (SCALAR_INT_MODE_P (mode)
15312 && (GET_CODE (rtl) == FIX
15313 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15315 op0 = convert_descriptor_to_mode (mode, op0);
15316 if (op0 == NULL)
15317 break;
15319 mem_loc_result = op0;
15321 break;
15323 case CLZ:
15324 case CTZ:
15325 case FFS:
15326 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15327 break;
15329 case POPCOUNT:
15330 case PARITY:
15331 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15332 break;
15334 case BSWAP:
15335 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15336 break;
15338 case ROTATE:
15339 case ROTATERT:
15340 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15341 break;
15343 case COMPARE:
15344 /* In theory, we could implement the above. */
15345 /* DWARF cannot represent the unsigned compare operations
15346 natively. */
15347 case SS_MULT:
15348 case US_MULT:
15349 case SS_DIV:
15350 case US_DIV:
15351 case SS_PLUS:
15352 case US_PLUS:
15353 case SS_MINUS:
15354 case US_MINUS:
15355 case SS_NEG:
15356 case US_NEG:
15357 case SS_ABS:
15358 case SS_ASHIFT:
15359 case US_ASHIFT:
15360 case SS_TRUNCATE:
15361 case US_TRUNCATE:
15362 case UNORDERED:
15363 case ORDERED:
15364 case UNEQ:
15365 case UNGE:
15366 case UNGT:
15367 case UNLE:
15368 case UNLT:
15369 case LTGT:
15370 case FRACT_CONVERT:
15371 case UNSIGNED_FRACT_CONVERT:
15372 case SAT_FRACT:
15373 case UNSIGNED_SAT_FRACT:
15374 case SQRT:
15375 case ASM_OPERANDS:
15376 case VEC_MERGE:
15377 case VEC_SELECT:
15378 case VEC_CONCAT:
15379 case VEC_DUPLICATE:
15380 case UNSPEC:
15381 case HIGH:
15382 case FMA:
15383 case STRICT_LOW_PART:
15384 case CONST_VECTOR:
15385 case CONST_FIXED:
15386 case CLRSB:
15387 case CLOBBER:
15388 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15389 can't express it in the debug info. This can happen e.g. with some
15390 TLS UNSPECs. */
15391 break;
15393 case CONST_STRING:
15394 resolve_one_addr (&rtl);
15395 goto symref;
15397 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15398 the expression. An UNSPEC rtx represents a raw DWARF operation,
15399 new_loc_descr is called for it to build the operation directly.
15400 Otherwise mem_loc_descriptor is called recursively. */
15401 case PARALLEL:
15403 int index = 0;
15404 dw_loc_descr_ref exp_result = NULL;
15406 for (; index < XVECLEN (rtl, 0); index++)
15408 rtx elem = XVECEXP (rtl, 0, index);
15409 if (GET_CODE (elem) == UNSPEC)
15411 /* Each DWARF operation UNSPEC contain two operands, if
15412 one operand is not used for the operation, const0_rtx is
15413 passed. */
15414 gcc_assert (XVECLEN (elem, 0) == 2);
15416 HOST_WIDE_INT dw_op = XINT (elem, 1);
15417 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15418 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15419 exp_result
15420 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15421 oprnd2);
15423 else
15424 exp_result
15425 = mem_loc_descriptor (elem, mode, mem_mode,
15426 VAR_INIT_STATUS_INITIALIZED);
15428 if (!mem_loc_result)
15429 mem_loc_result = exp_result;
15430 else
15431 add_loc_descr (&mem_loc_result, exp_result);
15434 break;
15437 default:
15438 if (flag_checking)
15440 print_rtl (stderr, rtl);
15441 gcc_unreachable ();
15443 break;
15446 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15447 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15449 return mem_loc_result;
15452 /* Return a descriptor that describes the concatenation of two locations.
15453 This is typically a complex variable. */
15455 static dw_loc_descr_ref
15456 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15458 dw_loc_descr_ref cc_loc_result = NULL;
15459 dw_loc_descr_ref x0_ref
15460 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15461 dw_loc_descr_ref x1_ref
15462 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15464 if (x0_ref == 0 || x1_ref == 0)
15465 return 0;
15467 cc_loc_result = x0_ref;
15468 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15470 add_loc_descr (&cc_loc_result, x1_ref);
15471 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15473 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15474 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15476 return cc_loc_result;
15479 /* Return a descriptor that describes the concatenation of N
15480 locations. */
15482 static dw_loc_descr_ref
15483 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15485 unsigned int i;
15486 dw_loc_descr_ref cc_loc_result = NULL;
15487 unsigned int n = XVECLEN (concatn, 0);
15489 for (i = 0; i < n; ++i)
15491 dw_loc_descr_ref ref;
15492 rtx x = XVECEXP (concatn, 0, i);
15494 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15495 if (ref == NULL)
15496 return NULL;
15498 add_loc_descr (&cc_loc_result, ref);
15499 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15502 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15503 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15505 return cc_loc_result;
15508 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15509 for DEBUG_IMPLICIT_PTR RTL. */
15511 static dw_loc_descr_ref
15512 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15514 dw_loc_descr_ref ret;
15515 dw_die_ref ref;
15517 if (dwarf_strict && dwarf_version < 5)
15518 return NULL;
15519 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15520 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15521 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15522 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15523 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15524 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15525 if (ref)
15527 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15528 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15529 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15531 else
15533 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15534 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15536 return ret;
15539 /* Output a proper Dwarf location descriptor for a variable or parameter
15540 which is either allocated in a register or in a memory location. For a
15541 register, we just generate an OP_REG and the register number. For a
15542 memory location we provide a Dwarf postfix expression describing how to
15543 generate the (dynamic) address of the object onto the address stack.
15545 MODE is mode of the decl if this loc_descriptor is going to be used in
15546 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15547 allowed, VOIDmode otherwise.
15549 If we don't know how to describe it, return 0. */
15551 static dw_loc_descr_ref
15552 loc_descriptor (rtx rtl, machine_mode mode,
15553 enum var_init_status initialized)
15555 dw_loc_descr_ref loc_result = NULL;
15557 switch (GET_CODE (rtl))
15559 case SUBREG:
15560 /* The case of a subreg may arise when we have a local (register)
15561 variable or a formal (register) parameter which doesn't quite fill
15562 up an entire register. For now, just assume that it is
15563 legitimate to make the Dwarf info refer to the whole register which
15564 contains the given subreg. */
15565 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15566 loc_result = loc_descriptor (SUBREG_REG (rtl),
15567 GET_MODE (SUBREG_REG (rtl)), initialized);
15568 else
15569 goto do_default;
15570 break;
15572 case REG:
15573 loc_result = reg_loc_descriptor (rtl, initialized);
15574 break;
15576 case MEM:
15577 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15578 GET_MODE (rtl), initialized);
15579 if (loc_result == NULL)
15580 loc_result = tls_mem_loc_descriptor (rtl);
15581 if (loc_result == NULL)
15583 rtx new_rtl = avoid_constant_pool_reference (rtl);
15584 if (new_rtl != rtl)
15585 loc_result = loc_descriptor (new_rtl, mode, initialized);
15587 break;
15589 case CONCAT:
15590 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15591 initialized);
15592 break;
15594 case CONCATN:
15595 loc_result = concatn_loc_descriptor (rtl, initialized);
15596 break;
15598 case VAR_LOCATION:
15599 /* Single part. */
15600 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15602 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15603 if (GET_CODE (loc) == EXPR_LIST)
15604 loc = XEXP (loc, 0);
15605 loc_result = loc_descriptor (loc, mode, initialized);
15606 break;
15609 rtl = XEXP (rtl, 1);
15610 /* FALLTHRU */
15612 case PARALLEL:
15614 rtvec par_elems = XVEC (rtl, 0);
15615 int num_elem = GET_NUM_ELEM (par_elems);
15616 machine_mode mode;
15617 int i;
15619 /* Create the first one, so we have something to add to. */
15620 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15621 VOIDmode, initialized);
15622 if (loc_result == NULL)
15623 return NULL;
15624 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15625 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15626 for (i = 1; i < num_elem; i++)
15628 dw_loc_descr_ref temp;
15630 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15631 VOIDmode, initialized);
15632 if (temp == NULL)
15633 return NULL;
15634 add_loc_descr (&loc_result, temp);
15635 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15636 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15639 break;
15641 case CONST_INT:
15642 if (mode != VOIDmode && mode != BLKmode)
15643 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15644 INTVAL (rtl));
15645 break;
15647 case CONST_DOUBLE:
15648 if (mode == VOIDmode)
15649 mode = GET_MODE (rtl);
15651 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15653 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15655 /* Note that a CONST_DOUBLE rtx could represent either an integer
15656 or a floating-point constant. A CONST_DOUBLE is used whenever
15657 the constant requires more than one word in order to be
15658 adequately represented. We output CONST_DOUBLEs as blocks. */
15659 loc_result = new_loc_descr (DW_OP_implicit_value,
15660 GET_MODE_SIZE (mode), 0);
15661 #if TARGET_SUPPORTS_WIDE_INT == 0
15662 if (!SCALAR_FLOAT_MODE_P (mode))
15664 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15665 loc_result->dw_loc_oprnd2.v.val_double
15666 = rtx_to_double_int (rtl);
15668 else
15669 #endif
15671 unsigned int length = GET_MODE_SIZE (mode);
15672 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15674 insert_float (rtl, array);
15675 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15676 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15677 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15678 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15681 break;
15683 case CONST_WIDE_INT:
15684 if (mode == VOIDmode)
15685 mode = GET_MODE (rtl);
15687 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15689 loc_result = new_loc_descr (DW_OP_implicit_value,
15690 GET_MODE_SIZE (mode), 0);
15691 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15692 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15693 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15695 break;
15697 case CONST_VECTOR:
15698 if (mode == VOIDmode)
15699 mode = GET_MODE (rtl);
15701 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15703 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15704 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15705 unsigned char *array
15706 = ggc_vec_alloc<unsigned char> (length * elt_size);
15707 unsigned int i;
15708 unsigned char *p;
15709 machine_mode imode = GET_MODE_INNER (mode);
15711 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15712 switch (GET_MODE_CLASS (mode))
15714 case MODE_VECTOR_INT:
15715 for (i = 0, p = array; i < length; i++, p += elt_size)
15717 rtx elt = CONST_VECTOR_ELT (rtl, i);
15718 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15720 break;
15722 case MODE_VECTOR_FLOAT:
15723 for (i = 0, p = array; i < length; i++, p += elt_size)
15725 rtx elt = CONST_VECTOR_ELT (rtl, i);
15726 insert_float (elt, p);
15728 break;
15730 default:
15731 gcc_unreachable ();
15734 loc_result = new_loc_descr (DW_OP_implicit_value,
15735 length * elt_size, 0);
15736 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15737 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15738 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15739 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15741 break;
15743 case CONST:
15744 if (mode == VOIDmode
15745 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15746 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15747 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15749 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15750 break;
15752 /* FALLTHROUGH */
15753 case SYMBOL_REF:
15754 if (!const_ok_for_output (rtl))
15755 break;
15756 /* FALLTHROUGH */
15757 case LABEL_REF:
15758 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15759 && (dwarf_version >= 4 || !dwarf_strict))
15761 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15762 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15763 vec_safe_push (used_rtx_array, rtl);
15765 break;
15767 case DEBUG_IMPLICIT_PTR:
15768 loc_result = implicit_ptr_descriptor (rtl, 0);
15769 break;
15771 case PLUS:
15772 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15773 && CONST_INT_P (XEXP (rtl, 1)))
15775 loc_result
15776 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15777 break;
15779 /* FALLTHRU */
15780 do_default:
15781 default:
15782 if ((SCALAR_INT_MODE_P (mode)
15783 && GET_MODE (rtl) == mode
15784 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15785 && dwarf_version >= 4)
15786 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15788 /* Value expression. */
15789 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15790 if (loc_result)
15791 add_loc_descr (&loc_result,
15792 new_loc_descr (DW_OP_stack_value, 0, 0));
15794 break;
15797 return loc_result;
15800 /* We need to figure out what section we should use as the base for the
15801 address ranges where a given location is valid.
15802 1. If this particular DECL has a section associated with it, use that.
15803 2. If this function has a section associated with it, use that.
15804 3. Otherwise, use the text section.
15805 XXX: If you split a variable across multiple sections, we won't notice. */
15807 static const char *
15808 secname_for_decl (const_tree decl)
15810 const char *secname;
15812 if (VAR_OR_FUNCTION_DECL_P (decl)
15813 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15814 && DECL_SECTION_NAME (decl))
15815 secname = DECL_SECTION_NAME (decl);
15816 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15817 secname = DECL_SECTION_NAME (current_function_decl);
15818 else if (cfun && in_cold_section_p)
15819 secname = crtl->subsections.cold_section_label;
15820 else
15821 secname = text_section_label;
15823 return secname;
15826 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15828 static bool
15829 decl_by_reference_p (tree decl)
15831 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15832 || VAR_P (decl))
15833 && DECL_BY_REFERENCE (decl));
15836 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15837 for VARLOC. */
15839 static dw_loc_descr_ref
15840 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15841 enum var_init_status initialized)
15843 int have_address = 0;
15844 dw_loc_descr_ref descr;
15845 machine_mode mode;
15847 if (want_address != 2)
15849 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15850 /* Single part. */
15851 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15853 varloc = PAT_VAR_LOCATION_LOC (varloc);
15854 if (GET_CODE (varloc) == EXPR_LIST)
15855 varloc = XEXP (varloc, 0);
15856 mode = GET_MODE (varloc);
15857 if (MEM_P (varloc))
15859 rtx addr = XEXP (varloc, 0);
15860 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15861 mode, initialized);
15862 if (descr)
15863 have_address = 1;
15864 else
15866 rtx x = avoid_constant_pool_reference (varloc);
15867 if (x != varloc)
15868 descr = mem_loc_descriptor (x, mode, VOIDmode,
15869 initialized);
15872 else
15873 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15875 else
15876 return 0;
15878 else
15880 if (GET_CODE (varloc) == VAR_LOCATION)
15881 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15882 else
15883 mode = DECL_MODE (loc);
15884 descr = loc_descriptor (varloc, mode, initialized);
15885 have_address = 1;
15888 if (!descr)
15889 return 0;
15891 if (want_address == 2 && !have_address
15892 && (dwarf_version >= 4 || !dwarf_strict))
15894 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15896 expansion_failed (loc, NULL_RTX,
15897 "DWARF address size mismatch");
15898 return 0;
15900 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15901 have_address = 1;
15903 /* Show if we can't fill the request for an address. */
15904 if (want_address && !have_address)
15906 expansion_failed (loc, NULL_RTX,
15907 "Want address and only have value");
15908 return 0;
15911 /* If we've got an address and don't want one, dereference. */
15912 if (!want_address && have_address)
15914 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15915 enum dwarf_location_atom op;
15917 if (size > DWARF2_ADDR_SIZE || size == -1)
15919 expansion_failed (loc, NULL_RTX,
15920 "DWARF address size mismatch");
15921 return 0;
15923 else if (size == DWARF2_ADDR_SIZE)
15924 op = DW_OP_deref;
15925 else
15926 op = DW_OP_deref_size;
15928 add_loc_descr (&descr, new_loc_descr (op, size, 0));
15931 return descr;
15934 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15935 if it is not possible. */
15937 static dw_loc_descr_ref
15938 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15940 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15941 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15942 else if (dwarf_version >= 3 || !dwarf_strict)
15943 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15944 else
15945 return NULL;
15948 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15949 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
15951 static dw_loc_descr_ref
15952 dw_sra_loc_expr (tree decl, rtx loc)
15954 rtx p;
15955 unsigned HOST_WIDE_INT padsize = 0;
15956 dw_loc_descr_ref descr, *descr_tail;
15957 unsigned HOST_WIDE_INT decl_size;
15958 rtx varloc;
15959 enum var_init_status initialized;
15961 if (DECL_SIZE (decl) == NULL
15962 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
15963 return NULL;
15965 decl_size = tree_to_uhwi (DECL_SIZE (decl));
15966 descr = NULL;
15967 descr_tail = &descr;
15969 for (p = loc; p; p = XEXP (p, 1))
15971 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
15972 rtx loc_note = *decl_piece_varloc_ptr (p);
15973 dw_loc_descr_ref cur_descr;
15974 dw_loc_descr_ref *tail, last = NULL;
15975 unsigned HOST_WIDE_INT opsize = 0;
15977 if (loc_note == NULL_RTX
15978 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15980 padsize += bitsize;
15981 continue;
15983 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15984 varloc = NOTE_VAR_LOCATION (loc_note);
15985 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15986 if (cur_descr == NULL)
15988 padsize += bitsize;
15989 continue;
15992 /* Check that cur_descr either doesn't use
15993 DW_OP_*piece operations, or their sum is equal
15994 to bitsize. Otherwise we can't embed it. */
15995 for (tail = &cur_descr; *tail != NULL;
15996 tail = &(*tail)->dw_loc_next)
15997 if ((*tail)->dw_loc_opc == DW_OP_piece)
15999 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16000 * BITS_PER_UNIT;
16001 last = *tail;
16003 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16005 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16006 last = *tail;
16009 if (last != NULL && opsize != bitsize)
16011 padsize += bitsize;
16012 /* Discard the current piece of the descriptor and release any
16013 addr_table entries it uses. */
16014 remove_loc_list_addr_table_entries (cur_descr);
16015 continue;
16018 /* If there is a hole, add DW_OP_*piece after empty DWARF
16019 expression, which means that those bits are optimized out. */
16020 if (padsize)
16022 if (padsize > decl_size)
16024 remove_loc_list_addr_table_entries (cur_descr);
16025 goto discard_descr;
16027 decl_size -= padsize;
16028 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16029 if (*descr_tail == NULL)
16031 remove_loc_list_addr_table_entries (cur_descr);
16032 goto discard_descr;
16034 descr_tail = &(*descr_tail)->dw_loc_next;
16035 padsize = 0;
16037 *descr_tail = cur_descr;
16038 descr_tail = tail;
16039 if (bitsize > decl_size)
16040 goto discard_descr;
16041 decl_size -= bitsize;
16042 if (last == NULL)
16044 HOST_WIDE_INT offset = 0;
16045 if (GET_CODE (varloc) == VAR_LOCATION
16046 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16048 varloc = PAT_VAR_LOCATION_LOC (varloc);
16049 if (GET_CODE (varloc) == EXPR_LIST)
16050 varloc = XEXP (varloc, 0);
16054 if (GET_CODE (varloc) == CONST
16055 || GET_CODE (varloc) == SIGN_EXTEND
16056 || GET_CODE (varloc) == ZERO_EXTEND)
16057 varloc = XEXP (varloc, 0);
16058 else if (GET_CODE (varloc) == SUBREG)
16059 varloc = SUBREG_REG (varloc);
16060 else
16061 break;
16063 while (1);
16064 /* DW_OP_bit_size offset should be zero for register
16065 or implicit location descriptions and empty location
16066 descriptions, but for memory addresses needs big endian
16067 adjustment. */
16068 if (MEM_P (varloc))
16070 unsigned HOST_WIDE_INT memsize
16071 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16072 if (memsize != bitsize)
16074 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16075 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16076 goto discard_descr;
16077 if (memsize < bitsize)
16078 goto discard_descr;
16079 if (BITS_BIG_ENDIAN)
16080 offset = memsize - bitsize;
16084 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16085 if (*descr_tail == NULL)
16086 goto discard_descr;
16087 descr_tail = &(*descr_tail)->dw_loc_next;
16091 /* If there were any non-empty expressions, add padding till the end of
16092 the decl. */
16093 if (descr != NULL && decl_size != 0)
16095 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16096 if (*descr_tail == NULL)
16097 goto discard_descr;
16099 return descr;
16101 discard_descr:
16102 /* Discard the descriptor and release any addr_table entries it uses. */
16103 remove_loc_list_addr_table_entries (descr);
16104 return NULL;
16107 /* Return the dwarf representation of the location list LOC_LIST of
16108 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16109 function. */
16111 static dw_loc_list_ref
16112 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16114 const char *endname, *secname;
16115 rtx varloc;
16116 enum var_init_status initialized;
16117 struct var_loc_node *node;
16118 dw_loc_descr_ref descr;
16119 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16120 dw_loc_list_ref list = NULL;
16121 dw_loc_list_ref *listp = &list;
16123 /* Now that we know what section we are using for a base,
16124 actually construct the list of locations.
16125 The first location information is what is passed to the
16126 function that creates the location list, and the remaining
16127 locations just get added on to that list.
16128 Note that we only know the start address for a location
16129 (IE location changes), so to build the range, we use
16130 the range [current location start, next location start].
16131 This means we have to special case the last node, and generate
16132 a range of [last location start, end of function label]. */
16134 secname = secname_for_decl (decl);
16136 for (node = loc_list->first; node; node = node->next)
16137 if (GET_CODE (node->loc) == EXPR_LIST
16138 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16140 if (GET_CODE (node->loc) == EXPR_LIST)
16142 /* This requires DW_OP_{,bit_}piece, which is not usable
16143 inside DWARF expressions. */
16144 if (want_address != 2)
16145 continue;
16146 descr = dw_sra_loc_expr (decl, node->loc);
16147 if (descr == NULL)
16148 continue;
16150 else
16152 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16153 varloc = NOTE_VAR_LOCATION (node->loc);
16154 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16156 if (descr)
16158 bool range_across_switch = false;
16159 /* If section switch happens in between node->label
16160 and node->next->label (or end of function) and
16161 we can't emit it as a single entry list,
16162 emit two ranges, first one ending at the end
16163 of first partition and second one starting at the
16164 beginning of second partition. */
16165 if (node == loc_list->last_before_switch
16166 && (node != loc_list->first || loc_list->first->next)
16167 && current_function_decl)
16169 endname = cfun->fde->dw_fde_end;
16170 range_across_switch = true;
16172 /* The variable has a location between NODE->LABEL and
16173 NODE->NEXT->LABEL. */
16174 else if (node->next)
16175 endname = node->next->label;
16176 /* If the variable has a location at the last label
16177 it keeps its location until the end of function. */
16178 else if (!current_function_decl)
16179 endname = text_end_label;
16180 else
16182 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16183 current_function_funcdef_no);
16184 endname = ggc_strdup (label_id);
16187 *listp = new_loc_list (descr, node->label, endname, secname);
16188 if (TREE_CODE (decl) == PARM_DECL
16189 && node == loc_list->first
16190 && NOTE_P (node->loc)
16191 && strcmp (node->label, endname) == 0)
16192 (*listp)->force = true;
16193 listp = &(*listp)->dw_loc_next;
16195 if (range_across_switch)
16197 if (GET_CODE (node->loc) == EXPR_LIST)
16198 descr = dw_sra_loc_expr (decl, node->loc);
16199 else
16201 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16202 varloc = NOTE_VAR_LOCATION (node->loc);
16203 descr = dw_loc_list_1 (decl, varloc, want_address,
16204 initialized);
16206 gcc_assert (descr);
16207 /* The variable has a location between NODE->LABEL and
16208 NODE->NEXT->LABEL. */
16209 if (node->next)
16210 endname = node->next->label;
16211 else
16212 endname = cfun->fde->dw_fde_second_end;
16213 *listp = new_loc_list (descr,
16214 cfun->fde->dw_fde_second_begin,
16215 endname, secname);
16216 listp = &(*listp)->dw_loc_next;
16221 /* Try to avoid the overhead of a location list emitting a location
16222 expression instead, but only if we didn't have more than one
16223 location entry in the first place. If some entries were not
16224 representable, we don't want to pretend a single entry that was
16225 applies to the entire scope in which the variable is
16226 available. */
16227 if (list && loc_list->first->next)
16228 gen_llsym (list);
16230 return list;
16233 /* Return if the loc_list has only single element and thus can be represented
16234 as location description. */
16236 static bool
16237 single_element_loc_list_p (dw_loc_list_ref list)
16239 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16240 return !list->ll_symbol;
16243 /* To each location in list LIST add loc descr REF. */
16245 static void
16246 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16248 dw_loc_descr_ref copy;
16249 add_loc_descr (&list->expr, ref);
16250 list = list->dw_loc_next;
16251 while (list)
16253 copy = ggc_alloc<dw_loc_descr_node> ();
16254 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16255 add_loc_descr (&list->expr, copy);
16256 while (copy->dw_loc_next)
16258 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
16259 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16260 copy->dw_loc_next = new_copy;
16261 copy = new_copy;
16263 list = list->dw_loc_next;
16267 /* Given two lists RET and LIST
16268 produce location list that is result of adding expression in LIST
16269 to expression in RET on each position in program.
16270 Might be destructive on both RET and LIST.
16272 TODO: We handle only simple cases of RET or LIST having at most one
16273 element. General case would inolve sorting the lists in program order
16274 and merging them that will need some additional work.
16275 Adding that will improve quality of debug info especially for SRA-ed
16276 structures. */
16278 static void
16279 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16281 if (!list)
16282 return;
16283 if (!*ret)
16285 *ret = list;
16286 return;
16288 if (!list->dw_loc_next)
16290 add_loc_descr_to_each (*ret, list->expr);
16291 return;
16293 if (!(*ret)->dw_loc_next)
16295 add_loc_descr_to_each (list, (*ret)->expr);
16296 *ret = list;
16297 return;
16299 expansion_failed (NULL_TREE, NULL_RTX,
16300 "Don't know how to merge two non-trivial"
16301 " location lists.\n");
16302 *ret = NULL;
16303 return;
16306 /* LOC is constant expression. Try a luck, look it up in constant
16307 pool and return its loc_descr of its address. */
16309 static dw_loc_descr_ref
16310 cst_pool_loc_descr (tree loc)
16312 /* Get an RTL for this, if something has been emitted. */
16313 rtx rtl = lookup_constant_def (loc);
16315 if (!rtl || !MEM_P (rtl))
16317 gcc_assert (!rtl);
16318 return 0;
16320 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16322 /* TODO: We might get more coverage if we was actually delaying expansion
16323 of all expressions till end of compilation when constant pools are fully
16324 populated. */
16325 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16327 expansion_failed (loc, NULL_RTX,
16328 "CST value in contant pool but not marked.");
16329 return 0;
16331 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16332 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16335 /* Return dw_loc_list representing address of addr_expr LOC
16336 by looking for inner INDIRECT_REF expression and turning
16337 it into simple arithmetics.
16339 See loc_list_from_tree for the meaning of CONTEXT. */
16341 static dw_loc_list_ref
16342 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16343 loc_descr_context *context)
16345 tree obj, offset;
16346 HOST_WIDE_INT bitsize, bitpos, bytepos;
16347 machine_mode mode;
16348 int unsignedp, reversep, volatilep = 0;
16349 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16351 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16352 &bitsize, &bitpos, &offset, &mode,
16353 &unsignedp, &reversep, &volatilep);
16354 STRIP_NOPS (obj);
16355 if (bitpos % BITS_PER_UNIT)
16357 expansion_failed (loc, NULL_RTX, "bitfield access");
16358 return 0;
16360 if (!INDIRECT_REF_P (obj))
16362 expansion_failed (obj,
16363 NULL_RTX, "no indirect ref in inner refrence");
16364 return 0;
16366 if (!offset && !bitpos)
16367 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16368 context);
16369 else if (toplev
16370 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16371 && (dwarf_version >= 4 || !dwarf_strict))
16373 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16374 if (!list_ret)
16375 return 0;
16376 if (offset)
16378 /* Variable offset. */
16379 list_ret1 = loc_list_from_tree (offset, 0, context);
16380 if (list_ret1 == 0)
16381 return 0;
16382 add_loc_list (&list_ret, list_ret1);
16383 if (!list_ret)
16384 return 0;
16385 add_loc_descr_to_each (list_ret,
16386 new_loc_descr (DW_OP_plus, 0, 0));
16388 bytepos = bitpos / BITS_PER_UNIT;
16389 if (bytepos > 0)
16390 add_loc_descr_to_each (list_ret,
16391 new_loc_descr (DW_OP_plus_uconst,
16392 bytepos, 0));
16393 else if (bytepos < 0)
16394 loc_list_plus_const (list_ret, bytepos);
16395 add_loc_descr_to_each (list_ret,
16396 new_loc_descr (DW_OP_stack_value, 0, 0));
16398 return list_ret;
16401 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16402 all operations from LOC are nops, move to the last one. Insert in NOPS all
16403 operations that are skipped. */
16405 static void
16406 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16407 hash_set<dw_loc_descr_ref> &nops)
16409 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16411 nops.add (loc);
16412 loc = loc->dw_loc_next;
16416 /* Helper for loc_descr_without_nops: free the location description operation
16417 P. */
16419 bool
16420 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16422 ggc_free (loc);
16423 return true;
16426 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16427 finishes LOC. */
16429 static void
16430 loc_descr_without_nops (dw_loc_descr_ref &loc)
16432 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16433 return;
16435 /* Set of all DW_OP_nop operations we remove. */
16436 hash_set<dw_loc_descr_ref> nops;
16438 /* First, strip all prefix NOP operations in order to keep the head of the
16439 operations list. */
16440 loc_descr_to_next_no_nop (loc, nops);
16442 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16444 /* For control flow operations: strip "prefix" nops in destination
16445 labels. */
16446 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16447 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16448 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16449 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16451 /* Do the same for the operations that follow, then move to the next
16452 iteration. */
16453 if (cur->dw_loc_next != NULL)
16454 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16455 cur = cur->dw_loc_next;
16458 nops.traverse<void *, free_loc_descr> (NULL);
16462 struct dwarf_procedure_info;
16464 /* Helper structure for location descriptions generation. */
16465 struct loc_descr_context
16467 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16468 NULL_TREE if DW_OP_push_object_address in invalid for this location
16469 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16470 tree context_type;
16471 /* The ..._DECL node that should be translated as a
16472 DW_OP_push_object_address operation. */
16473 tree base_decl;
16474 /* Information about the DWARF procedure we are currently generating. NULL if
16475 we are not generating a DWARF procedure. */
16476 struct dwarf_procedure_info *dpi;
16477 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16478 by consumer. Used for DW_TAG_generic_subrange attributes. */
16479 bool placeholder_arg;
16480 /* True if PLACEHOLDER_EXPR has been seen. */
16481 bool placeholder_seen;
16484 /* DWARF procedures generation
16486 DWARF expressions (aka. location descriptions) are used to encode variable
16487 things such as sizes or offsets. Such computations can have redundant parts
16488 that can be factorized in order to reduce the size of the output debug
16489 information. This is the whole point of DWARF procedures.
16491 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16492 already factorized into functions ("size functions") in order to handle very
16493 big and complex types. Such functions are quite simple: they have integral
16494 arguments, they return an integral result and their body contains only a
16495 return statement with arithmetic expressions. This is the only kind of
16496 function we are interested in translating into DWARF procedures, here.
16498 DWARF expressions and DWARF procedure are executed using a stack, so we have
16499 to define some calling convention for them to interact. Let's say that:
16501 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16502 all arguments in reverse order (right-to-left) so that when the DWARF
16503 procedure execution starts, the first argument is the top of the stack.
16505 - Then, when returning, the DWARF procedure must have consumed all arguments
16506 on the stack, must have pushed the result and touched nothing else.
16508 - Each integral argument and the result are integral types can be hold in a
16509 single stack slot.
16511 - We call "frame offset" the number of stack slots that are "under DWARF
16512 procedure control": it includes the arguments slots, the temporaries and
16513 the result slot. Thus, it is equal to the number of arguments when the
16514 procedure execution starts and must be equal to one (the result) when it
16515 returns. */
16517 /* Helper structure used when generating operations for a DWARF procedure. */
16518 struct dwarf_procedure_info
16520 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16521 currently translated. */
16522 tree fndecl;
16523 /* The number of arguments FNDECL takes. */
16524 unsigned args_count;
16527 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16528 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16529 equate it to this DIE. */
16531 static dw_die_ref
16532 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16533 dw_die_ref parent_die)
16535 dw_die_ref dwarf_proc_die;
16537 if ((dwarf_version < 3 && dwarf_strict)
16538 || location == NULL)
16539 return NULL;
16541 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16542 if (fndecl)
16543 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16544 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16545 return dwarf_proc_die;
16548 /* Return whether TYPE is a supported type as a DWARF procedure argument
16549 type or return type (we handle only scalar types and pointer types that
16550 aren't wider than the DWARF expression evaluation stack. */
16552 static bool
16553 is_handled_procedure_type (tree type)
16555 return ((INTEGRAL_TYPE_P (type)
16556 || TREE_CODE (type) == OFFSET_TYPE
16557 || TREE_CODE (type) == POINTER_TYPE)
16558 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16561 /* Helper for resolve_args_picking: do the same but stop when coming across
16562 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16563 offset *before* evaluating the corresponding operation. */
16565 static bool
16566 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16567 struct dwarf_procedure_info *dpi,
16568 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16570 /* The "frame_offset" identifier is already used to name a macro... */
16571 unsigned frame_offset_ = initial_frame_offset;
16572 dw_loc_descr_ref l;
16574 for (l = loc; l != NULL;)
16576 bool existed;
16577 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16579 /* If we already met this node, there is nothing to compute anymore. */
16580 if (existed)
16582 /* Make sure that the stack size is consistent wherever the execution
16583 flow comes from. */
16584 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16585 break;
16587 l_frame_offset = frame_offset_;
16589 /* If needed, relocate the picking offset with respect to the frame
16590 offset. */
16591 if (l->frame_offset_rel)
16593 unsigned HOST_WIDE_INT off;
16594 switch (l->dw_loc_opc)
16596 case DW_OP_pick:
16597 off = l->dw_loc_oprnd1.v.val_unsigned;
16598 break;
16599 case DW_OP_dup:
16600 off = 0;
16601 break;
16602 case DW_OP_over:
16603 off = 1;
16604 break;
16605 default:
16606 gcc_unreachable ();
16608 /* frame_offset_ is the size of the current stack frame, including
16609 incoming arguments. Besides, the arguments are pushed
16610 right-to-left. Thus, in order to access the Nth argument from
16611 this operation node, the picking has to skip temporaries *plus*
16612 one stack slot per argument (0 for the first one, 1 for the second
16613 one, etc.).
16615 The targetted argument number (N) is already set as the operand,
16616 and the number of temporaries can be computed with:
16617 frame_offsets_ - dpi->args_count */
16618 off += frame_offset_ - dpi->args_count;
16620 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16621 if (off > 255)
16622 return false;
16624 if (off == 0)
16626 l->dw_loc_opc = DW_OP_dup;
16627 l->dw_loc_oprnd1.v.val_unsigned = 0;
16629 else if (off == 1)
16631 l->dw_loc_opc = DW_OP_over;
16632 l->dw_loc_oprnd1.v.val_unsigned = 0;
16634 else
16636 l->dw_loc_opc = DW_OP_pick;
16637 l->dw_loc_oprnd1.v.val_unsigned = off;
16641 /* Update frame_offset according to the effect the current operation has
16642 on the stack. */
16643 switch (l->dw_loc_opc)
16645 case DW_OP_deref:
16646 case DW_OP_swap:
16647 case DW_OP_rot:
16648 case DW_OP_abs:
16649 case DW_OP_neg:
16650 case DW_OP_not:
16651 case DW_OP_plus_uconst:
16652 case DW_OP_skip:
16653 case DW_OP_reg0:
16654 case DW_OP_reg1:
16655 case DW_OP_reg2:
16656 case DW_OP_reg3:
16657 case DW_OP_reg4:
16658 case DW_OP_reg5:
16659 case DW_OP_reg6:
16660 case DW_OP_reg7:
16661 case DW_OP_reg8:
16662 case DW_OP_reg9:
16663 case DW_OP_reg10:
16664 case DW_OP_reg11:
16665 case DW_OP_reg12:
16666 case DW_OP_reg13:
16667 case DW_OP_reg14:
16668 case DW_OP_reg15:
16669 case DW_OP_reg16:
16670 case DW_OP_reg17:
16671 case DW_OP_reg18:
16672 case DW_OP_reg19:
16673 case DW_OP_reg20:
16674 case DW_OP_reg21:
16675 case DW_OP_reg22:
16676 case DW_OP_reg23:
16677 case DW_OP_reg24:
16678 case DW_OP_reg25:
16679 case DW_OP_reg26:
16680 case DW_OP_reg27:
16681 case DW_OP_reg28:
16682 case DW_OP_reg29:
16683 case DW_OP_reg30:
16684 case DW_OP_reg31:
16685 case DW_OP_bregx:
16686 case DW_OP_piece:
16687 case DW_OP_deref_size:
16688 case DW_OP_nop:
16689 case DW_OP_bit_piece:
16690 case DW_OP_implicit_value:
16691 case DW_OP_stack_value:
16692 break;
16694 case DW_OP_addr:
16695 case DW_OP_const1u:
16696 case DW_OP_const1s:
16697 case DW_OP_const2u:
16698 case DW_OP_const2s:
16699 case DW_OP_const4u:
16700 case DW_OP_const4s:
16701 case DW_OP_const8u:
16702 case DW_OP_const8s:
16703 case DW_OP_constu:
16704 case DW_OP_consts:
16705 case DW_OP_dup:
16706 case DW_OP_over:
16707 case DW_OP_pick:
16708 case DW_OP_lit0:
16709 case DW_OP_lit1:
16710 case DW_OP_lit2:
16711 case DW_OP_lit3:
16712 case DW_OP_lit4:
16713 case DW_OP_lit5:
16714 case DW_OP_lit6:
16715 case DW_OP_lit7:
16716 case DW_OP_lit8:
16717 case DW_OP_lit9:
16718 case DW_OP_lit10:
16719 case DW_OP_lit11:
16720 case DW_OP_lit12:
16721 case DW_OP_lit13:
16722 case DW_OP_lit14:
16723 case DW_OP_lit15:
16724 case DW_OP_lit16:
16725 case DW_OP_lit17:
16726 case DW_OP_lit18:
16727 case DW_OP_lit19:
16728 case DW_OP_lit20:
16729 case DW_OP_lit21:
16730 case DW_OP_lit22:
16731 case DW_OP_lit23:
16732 case DW_OP_lit24:
16733 case DW_OP_lit25:
16734 case DW_OP_lit26:
16735 case DW_OP_lit27:
16736 case DW_OP_lit28:
16737 case DW_OP_lit29:
16738 case DW_OP_lit30:
16739 case DW_OP_lit31:
16740 case DW_OP_breg0:
16741 case DW_OP_breg1:
16742 case DW_OP_breg2:
16743 case DW_OP_breg3:
16744 case DW_OP_breg4:
16745 case DW_OP_breg5:
16746 case DW_OP_breg6:
16747 case DW_OP_breg7:
16748 case DW_OP_breg8:
16749 case DW_OP_breg9:
16750 case DW_OP_breg10:
16751 case DW_OP_breg11:
16752 case DW_OP_breg12:
16753 case DW_OP_breg13:
16754 case DW_OP_breg14:
16755 case DW_OP_breg15:
16756 case DW_OP_breg16:
16757 case DW_OP_breg17:
16758 case DW_OP_breg18:
16759 case DW_OP_breg19:
16760 case DW_OP_breg20:
16761 case DW_OP_breg21:
16762 case DW_OP_breg22:
16763 case DW_OP_breg23:
16764 case DW_OP_breg24:
16765 case DW_OP_breg25:
16766 case DW_OP_breg26:
16767 case DW_OP_breg27:
16768 case DW_OP_breg28:
16769 case DW_OP_breg29:
16770 case DW_OP_breg30:
16771 case DW_OP_breg31:
16772 case DW_OP_fbreg:
16773 case DW_OP_push_object_address:
16774 case DW_OP_call_frame_cfa:
16775 ++frame_offset_;
16776 break;
16778 case DW_OP_drop:
16779 case DW_OP_xderef:
16780 case DW_OP_and:
16781 case DW_OP_div:
16782 case DW_OP_minus:
16783 case DW_OP_mod:
16784 case DW_OP_mul:
16785 case DW_OP_or:
16786 case DW_OP_plus:
16787 case DW_OP_shl:
16788 case DW_OP_shr:
16789 case DW_OP_shra:
16790 case DW_OP_xor:
16791 case DW_OP_bra:
16792 case DW_OP_eq:
16793 case DW_OP_ge:
16794 case DW_OP_gt:
16795 case DW_OP_le:
16796 case DW_OP_lt:
16797 case DW_OP_ne:
16798 case DW_OP_regx:
16799 case DW_OP_xderef_size:
16800 --frame_offset_;
16801 break;
16803 case DW_OP_call2:
16804 case DW_OP_call4:
16805 case DW_OP_call_ref:
16807 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16808 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16810 if (stack_usage == NULL)
16811 return false;
16812 frame_offset_ += *stack_usage;
16813 break;
16816 case DW_OP_implicit_pointer:
16817 case DW_OP_entry_value:
16818 case DW_OP_const_type:
16819 case DW_OP_regval_type:
16820 case DW_OP_deref_type:
16821 case DW_OP_convert:
16822 case DW_OP_reinterpret:
16823 case DW_OP_form_tls_address:
16824 case DW_OP_GNU_push_tls_address:
16825 case DW_OP_GNU_uninit:
16826 case DW_OP_GNU_encoded_addr:
16827 case DW_OP_GNU_implicit_pointer:
16828 case DW_OP_GNU_entry_value:
16829 case DW_OP_GNU_const_type:
16830 case DW_OP_GNU_regval_type:
16831 case DW_OP_GNU_deref_type:
16832 case DW_OP_GNU_convert:
16833 case DW_OP_GNU_reinterpret:
16834 case DW_OP_GNU_parameter_ref:
16835 /* loc_list_from_tree will probably not output these operations for
16836 size functions, so assume they will not appear here. */
16837 /* Fall through... */
16839 default:
16840 gcc_unreachable ();
16843 /* Now, follow the control flow (except subroutine calls). */
16844 switch (l->dw_loc_opc)
16846 case DW_OP_bra:
16847 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16848 frame_offsets))
16849 return false;
16850 /* Fall through. */
16852 case DW_OP_skip:
16853 l = l->dw_loc_oprnd1.v.val_loc;
16854 break;
16856 case DW_OP_stack_value:
16857 return true;
16859 default:
16860 l = l->dw_loc_next;
16861 break;
16865 return true;
16868 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16869 operations) in order to resolve the operand of DW_OP_pick operations that
16870 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16871 offset *before* LOC is executed. Return if all relocations were
16872 successful. */
16874 static bool
16875 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16876 struct dwarf_procedure_info *dpi)
16878 /* Associate to all visited operations the frame offset *before* evaluating
16879 this operation. */
16880 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16882 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16883 frame_offsets);
16886 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
16887 Return NULL if it is not possible. */
16889 static dw_die_ref
16890 function_to_dwarf_procedure (tree fndecl)
16892 struct loc_descr_context ctx;
16893 struct dwarf_procedure_info dpi;
16894 dw_die_ref dwarf_proc_die;
16895 tree tree_body = DECL_SAVED_TREE (fndecl);
16896 dw_loc_descr_ref loc_body, epilogue;
16898 tree cursor;
16899 unsigned i;
16901 /* Do not generate multiple DWARF procedures for the same function
16902 declaration. */
16903 dwarf_proc_die = lookup_decl_die (fndecl);
16904 if (dwarf_proc_die != NULL)
16905 return dwarf_proc_die;
16907 /* DWARF procedures are available starting with the DWARFv3 standard. */
16908 if (dwarf_version < 3 && dwarf_strict)
16909 return NULL;
16911 /* We handle only functions for which we still have a body, that return a
16912 supported type and that takes arguments with supported types. Note that
16913 there is no point translating functions that return nothing. */
16914 if (tree_body == NULL_TREE
16915 || DECL_RESULT (fndecl) == NULL_TREE
16916 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
16917 return NULL;
16919 for (cursor = DECL_ARGUMENTS (fndecl);
16920 cursor != NULL_TREE;
16921 cursor = TREE_CHAIN (cursor))
16922 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
16923 return NULL;
16925 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
16926 if (TREE_CODE (tree_body) != RETURN_EXPR)
16927 return NULL;
16928 tree_body = TREE_OPERAND (tree_body, 0);
16929 if (TREE_CODE (tree_body) != MODIFY_EXPR
16930 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
16931 return NULL;
16932 tree_body = TREE_OPERAND (tree_body, 1);
16934 /* Try to translate the body expression itself. Note that this will probably
16935 cause an infinite recursion if its call graph has a cycle. This is very
16936 unlikely for size functions, however, so don't bother with such things at
16937 the moment. */
16938 ctx.context_type = NULL_TREE;
16939 ctx.base_decl = NULL_TREE;
16940 ctx.dpi = &dpi;
16941 ctx.placeholder_arg = false;
16942 ctx.placeholder_seen = false;
16943 dpi.fndecl = fndecl;
16944 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
16945 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
16946 if (!loc_body)
16947 return NULL;
16949 /* After evaluating all operands in "loc_body", we should still have on the
16950 stack all arguments plus the desired function result (top of the stack).
16951 Generate code in order to keep only the result in our stack frame. */
16952 epilogue = NULL;
16953 for (i = 0; i < dpi.args_count; ++i)
16955 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
16956 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
16957 op_couple->dw_loc_next->dw_loc_next = epilogue;
16958 epilogue = op_couple;
16960 add_loc_descr (&loc_body, epilogue);
16961 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
16962 return NULL;
16964 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
16965 because they are considered useful. Now there is an epilogue, they are
16966 not anymore, so give it another try. */
16967 loc_descr_without_nops (loc_body);
16969 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
16970 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
16971 though, given that size functions do not come from source, so they should
16972 not have a dedicated DW_TAG_subprogram DIE. */
16973 dwarf_proc_die
16974 = new_dwarf_proc_die (loc_body, fndecl,
16975 get_context_die (DECL_CONTEXT (fndecl)));
16977 /* The called DWARF procedure consumes one stack slot per argument and
16978 returns one stack slot. */
16979 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
16981 return dwarf_proc_die;
16985 /* Generate Dwarf location list representing LOC.
16986 If WANT_ADDRESS is false, expression computing LOC will be computed
16987 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
16988 if WANT_ADDRESS is 2, expression computing address useable in location
16989 will be returned (i.e. DW_OP_reg can be used
16990 to refer to register values).
16992 CONTEXT provides information to customize the location descriptions
16993 generation. Its context_type field specifies what type is implicitly
16994 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
16995 will not be generated.
16997 Its DPI field determines whether we are generating a DWARF expression for a
16998 DWARF procedure, so PARM_DECL references are processed specifically.
17000 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17001 and dpi fields were null. */
17003 static dw_loc_list_ref
17004 loc_list_from_tree_1 (tree loc, int want_address,
17005 struct loc_descr_context *context)
17007 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17008 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17009 int have_address = 0;
17010 enum dwarf_location_atom op;
17012 /* ??? Most of the time we do not take proper care for sign/zero
17013 extending the values properly. Hopefully this won't be a real
17014 problem... */
17016 if (context != NULL
17017 && context->base_decl == loc
17018 && want_address == 0)
17020 if (dwarf_version >= 3 || !dwarf_strict)
17021 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17022 NULL, NULL, NULL);
17023 else
17024 return NULL;
17027 switch (TREE_CODE (loc))
17029 case ERROR_MARK:
17030 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17031 return 0;
17033 case PLACEHOLDER_EXPR:
17034 /* This case involves extracting fields from an object to determine the
17035 position of other fields. It is supposed to appear only as the first
17036 operand of COMPONENT_REF nodes and to reference precisely the type
17037 that the context allows. */
17038 if (context != NULL
17039 && TREE_TYPE (loc) == context->context_type
17040 && want_address >= 1)
17042 if (dwarf_version >= 3 || !dwarf_strict)
17044 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17045 have_address = 1;
17046 break;
17048 else
17049 return NULL;
17051 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17052 the single argument passed by consumer. */
17053 else if (context != NULL
17054 && context->placeholder_arg
17055 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17056 && want_address == 0)
17058 ret = new_loc_descr (DW_OP_pick, 0, 0);
17059 ret->frame_offset_rel = 1;
17060 context->placeholder_seen = true;
17061 break;
17063 else
17064 expansion_failed (loc, NULL_RTX,
17065 "PLACEHOLDER_EXPR for an unexpected type");
17066 break;
17068 case CALL_EXPR:
17070 const int nargs = call_expr_nargs (loc);
17071 tree callee = get_callee_fndecl (loc);
17072 int i;
17073 dw_die_ref dwarf_proc;
17075 if (callee == NULL_TREE)
17076 goto call_expansion_failed;
17078 /* We handle only functions that return an integer. */
17079 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17080 goto call_expansion_failed;
17082 dwarf_proc = function_to_dwarf_procedure (callee);
17083 if (dwarf_proc == NULL)
17084 goto call_expansion_failed;
17086 /* Evaluate arguments right-to-left so that the first argument will
17087 be the top-most one on the stack. */
17088 for (i = nargs - 1; i >= 0; --i)
17090 dw_loc_descr_ref loc_descr
17091 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17092 context);
17094 if (loc_descr == NULL)
17095 goto call_expansion_failed;
17097 add_loc_descr (&ret, loc_descr);
17100 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17101 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17102 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17103 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17104 add_loc_descr (&ret, ret1);
17105 break;
17107 call_expansion_failed:
17108 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17109 /* There are no opcodes for these operations. */
17110 return 0;
17113 case PREINCREMENT_EXPR:
17114 case PREDECREMENT_EXPR:
17115 case POSTINCREMENT_EXPR:
17116 case POSTDECREMENT_EXPR:
17117 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17118 /* There are no opcodes for these operations. */
17119 return 0;
17121 case ADDR_EXPR:
17122 /* If we already want an address, see if there is INDIRECT_REF inside
17123 e.g. for &this->field. */
17124 if (want_address)
17126 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17127 (loc, want_address == 2, context);
17128 if (list_ret)
17129 have_address = 1;
17130 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17131 && (ret = cst_pool_loc_descr (loc)))
17132 have_address = 1;
17134 /* Otherwise, process the argument and look for the address. */
17135 if (!list_ret && !ret)
17136 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17137 else
17139 if (want_address)
17140 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17141 return NULL;
17143 break;
17145 case VAR_DECL:
17146 if (DECL_THREAD_LOCAL_P (loc))
17148 rtx rtl;
17149 enum dwarf_location_atom tls_op;
17150 enum dtprel_bool dtprel = dtprel_false;
17152 if (targetm.have_tls)
17154 /* If this is not defined, we have no way to emit the
17155 data. */
17156 if (!targetm.asm_out.output_dwarf_dtprel)
17157 return 0;
17159 /* The way DW_OP_GNU_push_tls_address is specified, we
17160 can only look up addresses of objects in the current
17161 module. We used DW_OP_addr as first op, but that's
17162 wrong, because DW_OP_addr is relocated by the debug
17163 info consumer, while DW_OP_GNU_push_tls_address
17164 operand shouldn't be. */
17165 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17166 return 0;
17167 dtprel = dtprel_true;
17168 /* We check for DWARF 5 here because gdb did not implement
17169 DW_OP_form_tls_address until after 7.12. */
17170 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17171 : DW_OP_GNU_push_tls_address);
17173 else
17175 if (!targetm.emutls.debug_form_tls_address
17176 || !(dwarf_version >= 3 || !dwarf_strict))
17177 return 0;
17178 /* We stuffed the control variable into the DECL_VALUE_EXPR
17179 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17180 no longer appear in gimple code. We used the control
17181 variable in specific so that we could pick it up here. */
17182 loc = DECL_VALUE_EXPR (loc);
17183 tls_op = DW_OP_form_tls_address;
17186 rtl = rtl_for_decl_location (loc);
17187 if (rtl == NULL_RTX)
17188 return 0;
17190 if (!MEM_P (rtl))
17191 return 0;
17192 rtl = XEXP (rtl, 0);
17193 if (! CONSTANT_P (rtl))
17194 return 0;
17196 ret = new_addr_loc_descr (rtl, dtprel);
17197 ret1 = new_loc_descr (tls_op, 0, 0);
17198 add_loc_descr (&ret, ret1);
17200 have_address = 1;
17201 break;
17203 /* FALLTHRU */
17205 case PARM_DECL:
17206 if (context != NULL && context->dpi != NULL
17207 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17209 /* We are generating code for a DWARF procedure and we want to access
17210 one of its arguments: find the appropriate argument offset and let
17211 the resolve_args_picking pass compute the offset that complies
17212 with the stack frame size. */
17213 unsigned i = 0;
17214 tree cursor;
17216 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17217 cursor != NULL_TREE && cursor != loc;
17218 cursor = TREE_CHAIN (cursor), ++i)
17220 /* If we are translating a DWARF procedure, all referenced parameters
17221 must belong to the current function. */
17222 gcc_assert (cursor != NULL_TREE);
17224 ret = new_loc_descr (DW_OP_pick, i, 0);
17225 ret->frame_offset_rel = 1;
17226 break;
17228 /* FALLTHRU */
17230 case RESULT_DECL:
17231 if (DECL_HAS_VALUE_EXPR_P (loc))
17232 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17233 want_address, context);
17234 /* FALLTHRU */
17236 case FUNCTION_DECL:
17238 rtx rtl;
17239 var_loc_list *loc_list = lookup_decl_loc (loc);
17241 if (loc_list && loc_list->first)
17243 list_ret = dw_loc_list (loc_list, loc, want_address);
17244 have_address = want_address != 0;
17245 break;
17247 rtl = rtl_for_decl_location (loc);
17248 if (rtl == NULL_RTX)
17250 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17251 return 0;
17253 else if (CONST_INT_P (rtl))
17255 HOST_WIDE_INT val = INTVAL (rtl);
17256 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17257 val &= GET_MODE_MASK (DECL_MODE (loc));
17258 ret = int_loc_descriptor (val);
17260 else if (GET_CODE (rtl) == CONST_STRING)
17262 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17263 return 0;
17265 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17266 ret = new_addr_loc_descr (rtl, dtprel_false);
17267 else
17269 machine_mode mode, mem_mode;
17271 /* Certain constructs can only be represented at top-level. */
17272 if (want_address == 2)
17274 ret = loc_descriptor (rtl, VOIDmode,
17275 VAR_INIT_STATUS_INITIALIZED);
17276 have_address = 1;
17278 else
17280 mode = GET_MODE (rtl);
17281 mem_mode = VOIDmode;
17282 if (MEM_P (rtl))
17284 mem_mode = mode;
17285 mode = get_address_mode (rtl);
17286 rtl = XEXP (rtl, 0);
17287 have_address = 1;
17289 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17290 VAR_INIT_STATUS_INITIALIZED);
17292 if (!ret)
17293 expansion_failed (loc, rtl,
17294 "failed to produce loc descriptor for rtl");
17297 break;
17299 case MEM_REF:
17300 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17302 have_address = 1;
17303 goto do_plus;
17305 /* Fallthru. */
17306 case INDIRECT_REF:
17307 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17308 have_address = 1;
17309 break;
17311 case TARGET_MEM_REF:
17312 case SSA_NAME:
17313 case DEBUG_EXPR_DECL:
17314 return NULL;
17316 case COMPOUND_EXPR:
17317 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17318 context);
17320 CASE_CONVERT:
17321 case VIEW_CONVERT_EXPR:
17322 case SAVE_EXPR:
17323 case MODIFY_EXPR:
17324 case NON_LVALUE_EXPR:
17325 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17326 context);
17328 case COMPONENT_REF:
17329 case BIT_FIELD_REF:
17330 case ARRAY_REF:
17331 case ARRAY_RANGE_REF:
17332 case REALPART_EXPR:
17333 case IMAGPART_EXPR:
17335 tree obj, offset;
17336 HOST_WIDE_INT bitsize, bitpos, bytepos;
17337 machine_mode mode;
17338 int unsignedp, reversep, volatilep = 0;
17340 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17341 &unsignedp, &reversep, &volatilep);
17343 gcc_assert (obj != loc);
17345 list_ret = loc_list_from_tree_1 (obj,
17346 want_address == 2
17347 && !bitpos && !offset ? 2 : 1,
17348 context);
17349 /* TODO: We can extract value of the small expression via shifting even
17350 for nonzero bitpos. */
17351 if (list_ret == 0)
17352 return 0;
17353 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17355 expansion_failed (loc, NULL_RTX,
17356 "bitfield access");
17357 return 0;
17360 if (offset != NULL_TREE)
17362 /* Variable offset. */
17363 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17364 if (list_ret1 == 0)
17365 return 0;
17366 add_loc_list (&list_ret, list_ret1);
17367 if (!list_ret)
17368 return 0;
17369 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17372 bytepos = bitpos / BITS_PER_UNIT;
17373 if (bytepos > 0)
17374 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17375 else if (bytepos < 0)
17376 loc_list_plus_const (list_ret, bytepos);
17378 have_address = 1;
17379 break;
17382 case INTEGER_CST:
17383 if ((want_address || !tree_fits_shwi_p (loc))
17384 && (ret = cst_pool_loc_descr (loc)))
17385 have_address = 1;
17386 else if (want_address == 2
17387 && tree_fits_shwi_p (loc)
17388 && (ret = address_of_int_loc_descriptor
17389 (int_size_in_bytes (TREE_TYPE (loc)),
17390 tree_to_shwi (loc))))
17391 have_address = 1;
17392 else if (tree_fits_shwi_p (loc))
17393 ret = int_loc_descriptor (tree_to_shwi (loc));
17394 else if (tree_fits_uhwi_p (loc))
17395 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17396 else
17398 expansion_failed (loc, NULL_RTX,
17399 "Integer operand is not host integer");
17400 return 0;
17402 break;
17404 case CONSTRUCTOR:
17405 case REAL_CST:
17406 case STRING_CST:
17407 case COMPLEX_CST:
17408 if ((ret = cst_pool_loc_descr (loc)))
17409 have_address = 1;
17410 else if (TREE_CODE (loc) == CONSTRUCTOR)
17412 tree type = TREE_TYPE (loc);
17413 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17414 unsigned HOST_WIDE_INT offset = 0;
17415 unsigned HOST_WIDE_INT cnt;
17416 constructor_elt *ce;
17418 if (TREE_CODE (type) == RECORD_TYPE)
17420 /* This is very limited, but it's enough to output
17421 pointers to member functions, as long as the
17422 referenced function is defined in the current
17423 translation unit. */
17424 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17426 tree val = ce->value;
17428 tree field = ce->index;
17430 if (val)
17431 STRIP_NOPS (val);
17433 if (!field || DECL_BIT_FIELD (field))
17435 expansion_failed (loc, NULL_RTX,
17436 "bitfield in record type constructor");
17437 size = offset = (unsigned HOST_WIDE_INT)-1;
17438 ret = NULL;
17439 break;
17442 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17443 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17444 gcc_assert (pos + fieldsize <= size);
17445 if (pos < offset)
17447 expansion_failed (loc, NULL_RTX,
17448 "out-of-order fields in record constructor");
17449 size = offset = (unsigned HOST_WIDE_INT)-1;
17450 ret = NULL;
17451 break;
17453 if (pos > offset)
17455 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17456 add_loc_descr (&ret, ret1);
17457 offset = pos;
17459 if (val && fieldsize != 0)
17461 ret1 = loc_descriptor_from_tree (val, want_address, context);
17462 if (!ret1)
17464 expansion_failed (loc, NULL_RTX,
17465 "unsupported expression in field");
17466 size = offset = (unsigned HOST_WIDE_INT)-1;
17467 ret = NULL;
17468 break;
17470 add_loc_descr (&ret, ret1);
17472 if (fieldsize)
17474 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17475 add_loc_descr (&ret, ret1);
17476 offset = pos + fieldsize;
17480 if (offset != size)
17482 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17483 add_loc_descr (&ret, ret1);
17484 offset = size;
17487 have_address = !!want_address;
17489 else
17490 expansion_failed (loc, NULL_RTX,
17491 "constructor of non-record type");
17493 else
17494 /* We can construct small constants here using int_loc_descriptor. */
17495 expansion_failed (loc, NULL_RTX,
17496 "constructor or constant not in constant pool");
17497 break;
17499 case TRUTH_AND_EXPR:
17500 case TRUTH_ANDIF_EXPR:
17501 case BIT_AND_EXPR:
17502 op = DW_OP_and;
17503 goto do_binop;
17505 case TRUTH_XOR_EXPR:
17506 case BIT_XOR_EXPR:
17507 op = DW_OP_xor;
17508 goto do_binop;
17510 case TRUTH_OR_EXPR:
17511 case TRUTH_ORIF_EXPR:
17512 case BIT_IOR_EXPR:
17513 op = DW_OP_or;
17514 goto do_binop;
17516 case FLOOR_DIV_EXPR:
17517 case CEIL_DIV_EXPR:
17518 case ROUND_DIV_EXPR:
17519 case TRUNC_DIV_EXPR:
17520 case EXACT_DIV_EXPR:
17521 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17522 return 0;
17523 op = DW_OP_div;
17524 goto do_binop;
17526 case MINUS_EXPR:
17527 op = DW_OP_minus;
17528 goto do_binop;
17530 case FLOOR_MOD_EXPR:
17531 case CEIL_MOD_EXPR:
17532 case ROUND_MOD_EXPR:
17533 case TRUNC_MOD_EXPR:
17534 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17536 op = DW_OP_mod;
17537 goto do_binop;
17539 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17540 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17541 if (list_ret == 0 || list_ret1 == 0)
17542 return 0;
17544 add_loc_list (&list_ret, list_ret1);
17545 if (list_ret == 0)
17546 return 0;
17547 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 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_div, 0, 0));
17550 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17551 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17552 break;
17554 case MULT_EXPR:
17555 op = DW_OP_mul;
17556 goto do_binop;
17558 case LSHIFT_EXPR:
17559 op = DW_OP_shl;
17560 goto do_binop;
17562 case RSHIFT_EXPR:
17563 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17564 goto do_binop;
17566 case POINTER_PLUS_EXPR:
17567 case PLUS_EXPR:
17568 do_plus:
17569 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17571 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17572 smarter to encode their opposite. The DW_OP_plus_uconst operation
17573 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17574 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17575 bytes, Y being the size of the operation that pushes the opposite
17576 of the addend. So let's choose the smallest representation. */
17577 const tree tree_addend = TREE_OPERAND (loc, 1);
17578 offset_int wi_addend;
17579 HOST_WIDE_INT shwi_addend;
17580 dw_loc_descr_ref loc_naddend;
17582 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17583 if (list_ret == 0)
17584 return 0;
17586 /* Try to get the literal to push. It is the opposite of the addend,
17587 so as we rely on wrapping during DWARF evaluation, first decode
17588 the literal as a "DWARF-sized" signed number. */
17589 wi_addend = wi::to_offset (tree_addend);
17590 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17591 shwi_addend = wi_addend.to_shwi ();
17592 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17593 ? int_loc_descriptor (-shwi_addend)
17594 : NULL;
17596 if (loc_naddend != NULL
17597 && ((unsigned) size_of_uleb128 (shwi_addend)
17598 > size_of_loc_descr (loc_naddend)))
17600 add_loc_descr_to_each (list_ret, loc_naddend);
17601 add_loc_descr_to_each (list_ret,
17602 new_loc_descr (DW_OP_minus, 0, 0));
17604 else
17606 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17608 loc_naddend = loc_cur;
17609 loc_cur = loc_cur->dw_loc_next;
17610 ggc_free (loc_naddend);
17612 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17614 break;
17617 op = DW_OP_plus;
17618 goto do_binop;
17620 case LE_EXPR:
17621 op = DW_OP_le;
17622 goto do_comp_binop;
17624 case GE_EXPR:
17625 op = DW_OP_ge;
17626 goto do_comp_binop;
17628 case LT_EXPR:
17629 op = DW_OP_lt;
17630 goto do_comp_binop;
17632 case GT_EXPR:
17633 op = DW_OP_gt;
17634 goto do_comp_binop;
17636 do_comp_binop:
17637 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17639 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17640 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17641 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17642 TREE_CODE (loc));
17643 break;
17645 else
17646 goto do_binop;
17648 case EQ_EXPR:
17649 op = DW_OP_eq;
17650 goto do_binop;
17652 case NE_EXPR:
17653 op = DW_OP_ne;
17654 goto do_binop;
17656 do_binop:
17657 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17658 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17659 if (list_ret == 0 || list_ret1 == 0)
17660 return 0;
17662 add_loc_list (&list_ret, list_ret1);
17663 if (list_ret == 0)
17664 return 0;
17665 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17666 break;
17668 case TRUTH_NOT_EXPR:
17669 case BIT_NOT_EXPR:
17670 op = DW_OP_not;
17671 goto do_unop;
17673 case ABS_EXPR:
17674 op = DW_OP_abs;
17675 goto do_unop;
17677 case NEGATE_EXPR:
17678 op = DW_OP_neg;
17679 goto do_unop;
17681 do_unop:
17682 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17683 if (list_ret == 0)
17684 return 0;
17686 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17687 break;
17689 case MIN_EXPR:
17690 case MAX_EXPR:
17692 const enum tree_code code =
17693 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17695 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17696 build2 (code, integer_type_node,
17697 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17698 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17701 /* fall through */
17703 case COND_EXPR:
17705 dw_loc_descr_ref lhs
17706 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17707 dw_loc_list_ref rhs
17708 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17709 dw_loc_descr_ref bra_node, jump_node, tmp;
17711 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17712 if (list_ret == 0 || lhs == 0 || rhs == 0)
17713 return 0;
17715 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17716 add_loc_descr_to_each (list_ret, bra_node);
17718 add_loc_list (&list_ret, rhs);
17719 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17720 add_loc_descr_to_each (list_ret, jump_node);
17722 add_loc_descr_to_each (list_ret, lhs);
17723 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17724 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17726 /* ??? Need a node to point the skip at. Use a nop. */
17727 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17728 add_loc_descr_to_each (list_ret, tmp);
17729 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17730 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17732 break;
17734 case FIX_TRUNC_EXPR:
17735 return 0;
17737 default:
17738 /* Leave front-end specific codes as simply unknown. This comes
17739 up, for instance, with the C STMT_EXPR. */
17740 if ((unsigned int) TREE_CODE (loc)
17741 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17743 expansion_failed (loc, NULL_RTX,
17744 "language specific tree node");
17745 return 0;
17748 /* Otherwise this is a generic code; we should just lists all of
17749 these explicitly. We forgot one. */
17750 if (flag_checking)
17751 gcc_unreachable ();
17753 /* In a release build, we want to degrade gracefully: better to
17754 generate incomplete debugging information than to crash. */
17755 return NULL;
17758 if (!ret && !list_ret)
17759 return 0;
17761 if (want_address == 2 && !have_address
17762 && (dwarf_version >= 4 || !dwarf_strict))
17764 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17766 expansion_failed (loc, NULL_RTX,
17767 "DWARF address size mismatch");
17768 return 0;
17770 if (ret)
17771 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17772 else
17773 add_loc_descr_to_each (list_ret,
17774 new_loc_descr (DW_OP_stack_value, 0, 0));
17775 have_address = 1;
17777 /* Show if we can't fill the request for an address. */
17778 if (want_address && !have_address)
17780 expansion_failed (loc, NULL_RTX,
17781 "Want address and only have value");
17782 return 0;
17785 gcc_assert (!ret || !list_ret);
17787 /* If we've got an address and don't want one, dereference. */
17788 if (!want_address && have_address)
17790 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17792 if (size > DWARF2_ADDR_SIZE || size == -1)
17794 expansion_failed (loc, NULL_RTX,
17795 "DWARF address size mismatch");
17796 return 0;
17798 else if (size == DWARF2_ADDR_SIZE)
17799 op = DW_OP_deref;
17800 else
17801 op = DW_OP_deref_size;
17803 if (ret)
17804 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17805 else
17806 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17808 if (ret)
17809 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17811 return list_ret;
17814 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17815 expressions. */
17817 static dw_loc_list_ref
17818 loc_list_from_tree (tree loc, int want_address,
17819 struct loc_descr_context *context)
17821 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17823 for (dw_loc_list_ref loc_cur = result;
17824 loc_cur != NULL; loc_cur =
17825 loc_cur->dw_loc_next)
17826 loc_descr_without_nops (loc_cur->expr);
17827 return result;
17830 /* Same as above but return only single location expression. */
17831 static dw_loc_descr_ref
17832 loc_descriptor_from_tree (tree loc, int want_address,
17833 struct loc_descr_context *context)
17835 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17836 if (!ret)
17837 return NULL;
17838 if (ret->dw_loc_next)
17840 expansion_failed (loc, NULL_RTX,
17841 "Location list where only loc descriptor needed");
17842 return NULL;
17844 return ret->expr;
17847 /* Given a value, round it up to the lowest multiple of `boundary'
17848 which is not less than the value itself. */
17850 static inline HOST_WIDE_INT
17851 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17853 return (((value + boundary - 1) / boundary) * boundary);
17856 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17857 pointer to the declared type for the relevant field variable, or return
17858 `integer_type_node' if the given node turns out to be an
17859 ERROR_MARK node. */
17861 static inline tree
17862 field_type (const_tree decl)
17864 tree type;
17866 if (TREE_CODE (decl) == ERROR_MARK)
17867 return integer_type_node;
17869 type = DECL_BIT_FIELD_TYPE (decl);
17870 if (type == NULL_TREE)
17871 type = TREE_TYPE (decl);
17873 return type;
17876 /* Given a pointer to a tree node, return the alignment in bits for
17877 it, or else return BITS_PER_WORD if the node actually turns out to
17878 be an ERROR_MARK node. */
17880 static inline unsigned
17881 simple_type_align_in_bits (const_tree type)
17883 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
17886 static inline unsigned
17887 simple_decl_align_in_bits (const_tree decl)
17889 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
17892 /* Return the result of rounding T up to ALIGN. */
17894 static inline offset_int
17895 round_up_to_align (const offset_int &t, unsigned int align)
17897 return wi::udiv_trunc (t + align - 1, align) * align;
17900 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
17901 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
17902 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
17903 if we fail to return the size in one of these two forms. */
17905 static dw_loc_descr_ref
17906 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
17908 tree tree_size;
17909 struct loc_descr_context ctx;
17911 /* Return a constant integer in priority, if possible. */
17912 *cst_size = int_size_in_bytes (type);
17913 if (*cst_size != -1)
17914 return NULL;
17916 ctx.context_type = const_cast<tree> (type);
17917 ctx.base_decl = NULL_TREE;
17918 ctx.dpi = NULL;
17919 ctx.placeholder_arg = false;
17920 ctx.placeholder_seen = false;
17922 type = TYPE_MAIN_VARIANT (type);
17923 tree_size = TYPE_SIZE_UNIT (type);
17924 return ((tree_size != NULL_TREE)
17925 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
17926 : NULL);
17929 /* Helper structure for RECORD_TYPE processing. */
17930 struct vlr_context
17932 /* Root RECORD_TYPE. It is needed to generate data member location
17933 descriptions in variable-length records (VLR), but also to cope with
17934 variants, which are composed of nested structures multiplexed with
17935 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
17936 function processing a FIELD_DECL, it is required to be non null. */
17937 tree struct_type;
17938 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
17939 QUAL_UNION_TYPE), this holds an expression that computes the offset for
17940 this variant part as part of the root record (in storage units). For
17941 regular records, it must be NULL_TREE. */
17942 tree variant_part_offset;
17945 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
17946 addressed byte of the "containing object" for the given FIELD_DECL. If
17947 possible, return a native constant through CST_OFFSET (in which case NULL is
17948 returned); otherwise return a DWARF expression that computes the offset.
17950 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
17951 that offset is, either because the argument turns out to be a pointer to an
17952 ERROR_MARK node, or because the offset expression is too complex for us.
17954 CTX is required: see the comment for VLR_CONTEXT. */
17956 static dw_loc_descr_ref
17957 field_byte_offset (const_tree decl, struct vlr_context *ctx,
17958 HOST_WIDE_INT *cst_offset)
17960 offset_int object_offset_in_bits;
17961 offset_int object_offset_in_bytes;
17962 offset_int bitpos_int;
17963 bool is_byte_offset_cst, is_bit_offset_cst;
17964 tree tree_result;
17965 dw_loc_list_ref loc_result;
17967 *cst_offset = 0;
17969 if (TREE_CODE (decl) == ERROR_MARK)
17970 return NULL;
17971 else
17972 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
17974 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
17975 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
17977 /* We cannot handle variable bit offsets at the moment, so abort if it's the
17978 case. */
17979 if (is_bit_offset_cst)
17980 return NULL;
17982 #ifdef PCC_BITFIELD_TYPE_MATTERS
17983 /* We used to handle only constant offsets in all cases. Now, we handle
17984 properly dynamic byte offsets only when PCC bitfield type doesn't
17985 matter. */
17986 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
17988 tree type;
17989 tree field_size_tree;
17990 offset_int deepest_bitpos;
17991 offset_int field_size_in_bits;
17992 unsigned int type_align_in_bits;
17993 unsigned int decl_align_in_bits;
17994 offset_int type_size_in_bits;
17996 bitpos_int = wi::to_offset (bit_position (decl));
17997 type = field_type (decl);
17998 type_size_in_bits = offset_int_type_size_in_bits (type);
17999 type_align_in_bits = simple_type_align_in_bits (type);
18001 field_size_tree = DECL_SIZE (decl);
18003 /* The size could be unspecified if there was an error, or for
18004 a flexible array member. */
18005 if (!field_size_tree)
18006 field_size_tree = bitsize_zero_node;
18008 /* If the size of the field is not constant, use the type size. */
18009 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18010 field_size_in_bits = wi::to_offset (field_size_tree);
18011 else
18012 field_size_in_bits = type_size_in_bits;
18014 decl_align_in_bits = simple_decl_align_in_bits (decl);
18016 /* The GCC front-end doesn't make any attempt to keep track of the
18017 starting bit offset (relative to the start of the containing
18018 structure type) of the hypothetical "containing object" for a
18019 bit-field. Thus, when computing the byte offset value for the
18020 start of the "containing object" of a bit-field, we must deduce
18021 this information on our own. This can be rather tricky to do in
18022 some cases. For example, handling the following structure type
18023 definition when compiling for an i386/i486 target (which only
18024 aligns long long's to 32-bit boundaries) can be very tricky:
18026 struct S { int field1; long long field2:31; };
18028 Fortunately, there is a simple rule-of-thumb which can be used
18029 in such cases. When compiling for an i386/i486, GCC will
18030 allocate 8 bytes for the structure shown above. It decides to
18031 do this based upon one simple rule for bit-field allocation.
18032 GCC allocates each "containing object" for each bit-field at
18033 the first (i.e. lowest addressed) legitimate alignment boundary
18034 (based upon the required minimum alignment for the declared
18035 type of the field) which it can possibly use, subject to the
18036 condition that there is still enough available space remaining
18037 in the containing object (when allocated at the selected point)
18038 to fully accommodate all of the bits of the bit-field itself.
18040 This simple rule makes it obvious why GCC allocates 8 bytes for
18041 each object of the structure type shown above. When looking
18042 for a place to allocate the "containing object" for `field2',
18043 the compiler simply tries to allocate a 64-bit "containing
18044 object" at each successive 32-bit boundary (starting at zero)
18045 until it finds a place to allocate that 64- bit field such that
18046 at least 31 contiguous (and previously unallocated) bits remain
18047 within that selected 64 bit field. (As it turns out, for the
18048 example above, the compiler finds it is OK to allocate the
18049 "containing object" 64-bit field at bit-offset zero within the
18050 structure type.)
18052 Here we attempt to work backwards from the limited set of facts
18053 we're given, and we try to deduce from those facts, where GCC
18054 must have believed that the containing object started (within
18055 the structure type). The value we deduce is then used (by the
18056 callers of this routine) to generate DW_AT_location and
18057 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18058 the case of DW_AT_location, regular fields as well). */
18060 /* Figure out the bit-distance from the start of the structure to
18061 the "deepest" bit of the bit-field. */
18062 deepest_bitpos = bitpos_int + field_size_in_bits;
18064 /* This is the tricky part. Use some fancy footwork to deduce
18065 where the lowest addressed bit of the containing object must
18066 be. */
18067 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18069 /* Round up to type_align by default. This works best for
18070 bitfields. */
18071 object_offset_in_bits
18072 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18074 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18076 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18078 /* Round up to decl_align instead. */
18079 object_offset_in_bits
18080 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18083 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18085 tree_result = byte_position (decl);
18086 if (ctx->variant_part_offset != NULL_TREE)
18087 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18088 ctx->variant_part_offset, tree_result));
18090 /* If the byte offset is a constant, it's simplier to handle a native
18091 constant rather than a DWARF expression. */
18092 if (TREE_CODE (tree_result) == INTEGER_CST)
18094 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18095 return NULL;
18097 struct loc_descr_context loc_ctx = {
18098 ctx->struct_type, /* context_type */
18099 NULL_TREE, /* base_decl */
18100 NULL, /* dpi */
18101 false, /* placeholder_arg */
18102 false /* placeholder_seen */
18104 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18106 /* We want a DWARF expression: abort if we only have a location list with
18107 multiple elements. */
18108 if (!loc_result || !single_element_loc_list_p (loc_result))
18109 return NULL;
18110 else
18111 return loc_result->expr;
18114 /* The following routines define various Dwarf attributes and any data
18115 associated with them. */
18117 /* Add a location description attribute value to a DIE.
18119 This emits location attributes suitable for whole variables and
18120 whole parameters. Note that the location attributes for struct fields are
18121 generated by the routine `data_member_location_attribute' below. */
18123 static inline void
18124 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18125 dw_loc_list_ref descr)
18127 if (descr == 0)
18128 return;
18129 if (single_element_loc_list_p (descr))
18130 add_AT_loc (die, attr_kind, descr->expr);
18131 else
18132 add_AT_loc_list (die, attr_kind, descr);
18135 /* Add DW_AT_accessibility attribute to DIE if needed. */
18137 static void
18138 add_accessibility_attribute (dw_die_ref die, tree decl)
18140 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18141 children, otherwise the default is DW_ACCESS_public. In DWARF2
18142 the default has always been DW_ACCESS_public. */
18143 if (TREE_PROTECTED (decl))
18144 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18145 else if (TREE_PRIVATE (decl))
18147 if (dwarf_version == 2
18148 || die->die_parent == NULL
18149 || die->die_parent->die_tag != DW_TAG_class_type)
18150 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18152 else if (dwarf_version > 2
18153 && die->die_parent
18154 && die->die_parent->die_tag == DW_TAG_class_type)
18155 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18158 /* Attach the specialized form of location attribute used for data members of
18159 struct and union types. In the special case of a FIELD_DECL node which
18160 represents a bit-field, the "offset" part of this special location
18161 descriptor must indicate the distance in bytes from the lowest-addressed
18162 byte of the containing struct or union type to the lowest-addressed byte of
18163 the "containing object" for the bit-field. (See the `field_byte_offset'
18164 function above).
18166 For any given bit-field, the "containing object" is a hypothetical object
18167 (of some integral or enum type) within which the given bit-field lives. The
18168 type of this hypothetical "containing object" is always the same as the
18169 declared type of the individual bit-field itself (for GCC anyway... the
18170 DWARF spec doesn't actually mandate this). Note that it is the size (in
18171 bytes) of the hypothetical "containing object" which will be given in the
18172 DW_AT_byte_size attribute for this bit-field. (See the
18173 `byte_size_attribute' function below.) It is also used when calculating the
18174 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18175 function below.)
18177 CTX is required: see the comment for VLR_CONTEXT. */
18179 static void
18180 add_data_member_location_attribute (dw_die_ref die,
18181 tree decl,
18182 struct vlr_context *ctx)
18184 HOST_WIDE_INT offset;
18185 dw_loc_descr_ref loc_descr = 0;
18187 if (TREE_CODE (decl) == TREE_BINFO)
18189 /* We're working on the TAG_inheritance for a base class. */
18190 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18192 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18193 aren't at a fixed offset from all (sub)objects of the same
18194 type. We need to extract the appropriate offset from our
18195 vtable. The following dwarf expression means
18197 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18199 This is specific to the V3 ABI, of course. */
18201 dw_loc_descr_ref tmp;
18203 /* Make a copy of the object address. */
18204 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18205 add_loc_descr (&loc_descr, tmp);
18207 /* Extract the vtable address. */
18208 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18209 add_loc_descr (&loc_descr, tmp);
18211 /* Calculate the address of the offset. */
18212 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18213 gcc_assert (offset < 0);
18215 tmp = int_loc_descriptor (-offset);
18216 add_loc_descr (&loc_descr, tmp);
18217 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18218 add_loc_descr (&loc_descr, tmp);
18220 /* Extract the offset. */
18221 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18222 add_loc_descr (&loc_descr, tmp);
18224 /* Add it to the object address. */
18225 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18226 add_loc_descr (&loc_descr, tmp);
18228 else
18229 offset = tree_to_shwi (BINFO_OFFSET (decl));
18231 else
18233 loc_descr = field_byte_offset (decl, ctx, &offset);
18235 /* If loc_descr is available then we know the field offset is dynamic.
18236 However, GDB does not handle dynamic field offsets very well at the
18237 moment. */
18238 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18240 loc_descr = NULL;
18241 offset = 0;
18244 /* Data member location evalutation starts with the base address on the
18245 stack. Compute the field offset and add it to this base address. */
18246 else if (loc_descr != NULL)
18247 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18250 if (! loc_descr)
18252 if (dwarf_version > 2)
18254 /* Don't need to output a location expression, just the constant. */
18255 if (offset < 0)
18256 add_AT_int (die, DW_AT_data_member_location, offset);
18257 else
18258 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18259 return;
18261 else
18263 enum dwarf_location_atom op;
18265 /* The DWARF2 standard says that we should assume that the structure
18266 address is already on the stack, so we can specify a structure
18267 field address by using DW_OP_plus_uconst. */
18268 op = DW_OP_plus_uconst;
18269 loc_descr = new_loc_descr (op, offset, 0);
18273 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18276 /* Writes integer values to dw_vec_const array. */
18278 static void
18279 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18281 while (size != 0)
18283 *dest++ = val & 0xff;
18284 val >>= 8;
18285 --size;
18289 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18291 static HOST_WIDE_INT
18292 extract_int (const unsigned char *src, unsigned int size)
18294 HOST_WIDE_INT val = 0;
18296 src += size;
18297 while (size != 0)
18299 val <<= 8;
18300 val |= *--src & 0xff;
18301 --size;
18303 return val;
18306 /* Writes wide_int values to dw_vec_const array. */
18308 static void
18309 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18311 int i;
18313 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18315 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18316 return;
18319 /* We'd have to extend this code to support odd sizes. */
18320 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18322 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18324 if (WORDS_BIG_ENDIAN)
18325 for (i = n - 1; i >= 0; i--)
18327 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18328 dest += sizeof (HOST_WIDE_INT);
18330 else
18331 for (i = 0; i < n; i++)
18333 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18334 dest += sizeof (HOST_WIDE_INT);
18338 /* Writes floating point values to dw_vec_const array. */
18340 static void
18341 insert_float (const_rtx rtl, unsigned char *array)
18343 long val[4];
18344 int i;
18346 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18348 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18349 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18351 insert_int (val[i], 4, array);
18352 array += 4;
18356 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18357 does not have a "location" either in memory or in a register. These
18358 things can arise in GNU C when a constant is passed as an actual parameter
18359 to an inlined function. They can also arise in C++ where declared
18360 constants do not necessarily get memory "homes". */
18362 static bool
18363 add_const_value_attribute (dw_die_ref die, rtx rtl)
18365 switch (GET_CODE (rtl))
18367 case CONST_INT:
18369 HOST_WIDE_INT val = INTVAL (rtl);
18371 if (val < 0)
18372 add_AT_int (die, DW_AT_const_value, val);
18373 else
18374 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18376 return true;
18378 case CONST_WIDE_INT:
18380 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18381 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18382 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18383 wide_int w = wi::zext (w1, prec);
18384 add_AT_wide (die, DW_AT_const_value, w);
18386 return true;
18388 case CONST_DOUBLE:
18389 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18390 floating-point constant. A CONST_DOUBLE is used whenever the
18391 constant requires more than one word in order to be adequately
18392 represented. */
18394 machine_mode mode = GET_MODE (rtl);
18396 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18397 add_AT_double (die, DW_AT_const_value,
18398 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18399 else
18401 unsigned int length = GET_MODE_SIZE (mode);
18402 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18404 insert_float (rtl, array);
18405 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18408 return true;
18410 case CONST_VECTOR:
18412 machine_mode mode = GET_MODE (rtl);
18413 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18414 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18415 unsigned char *array
18416 = ggc_vec_alloc<unsigned char> (length * elt_size);
18417 unsigned int i;
18418 unsigned char *p;
18419 machine_mode imode = GET_MODE_INNER (mode);
18421 switch (GET_MODE_CLASS (mode))
18423 case MODE_VECTOR_INT:
18424 for (i = 0, p = array; i < length; i++, p += elt_size)
18426 rtx elt = CONST_VECTOR_ELT (rtl, i);
18427 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18429 break;
18431 case MODE_VECTOR_FLOAT:
18432 for (i = 0, p = array; i < length; i++, p += elt_size)
18434 rtx elt = CONST_VECTOR_ELT (rtl, i);
18435 insert_float (elt, p);
18437 break;
18439 default:
18440 gcc_unreachable ();
18443 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18445 return true;
18447 case CONST_STRING:
18448 if (dwarf_version >= 4 || !dwarf_strict)
18450 dw_loc_descr_ref loc_result;
18451 resolve_one_addr (&rtl);
18452 rtl_addr:
18453 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18454 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18455 add_AT_loc (die, DW_AT_location, loc_result);
18456 vec_safe_push (used_rtx_array, rtl);
18457 return true;
18459 return false;
18461 case CONST:
18462 if (CONSTANT_P (XEXP (rtl, 0)))
18463 return add_const_value_attribute (die, XEXP (rtl, 0));
18464 /* FALLTHROUGH */
18465 case SYMBOL_REF:
18466 if (!const_ok_for_output (rtl))
18467 return false;
18468 /* FALLTHROUGH */
18469 case LABEL_REF:
18470 if (dwarf_version >= 4 || !dwarf_strict)
18471 goto rtl_addr;
18472 return false;
18474 case PLUS:
18475 /* In cases where an inlined instance of an inline function is passed
18476 the address of an `auto' variable (which is local to the caller) we
18477 can get a situation where the DECL_RTL of the artificial local
18478 variable (for the inlining) which acts as a stand-in for the
18479 corresponding formal parameter (of the inline function) will look
18480 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18481 exactly a compile-time constant expression, but it isn't the address
18482 of the (artificial) local variable either. Rather, it represents the
18483 *value* which the artificial local variable always has during its
18484 lifetime. We currently have no way to represent such quasi-constant
18485 values in Dwarf, so for now we just punt and generate nothing. */
18486 return false;
18488 case HIGH:
18489 case CONST_FIXED:
18490 return false;
18492 case MEM:
18493 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18494 && MEM_READONLY_P (rtl)
18495 && GET_MODE (rtl) == BLKmode)
18497 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18498 return true;
18500 return false;
18502 default:
18503 /* No other kinds of rtx should be possible here. */
18504 gcc_unreachable ();
18506 return false;
18509 /* Determine whether the evaluation of EXPR references any variables
18510 or functions which aren't otherwise used (and therefore may not be
18511 output). */
18512 static tree
18513 reference_to_unused (tree * tp, int * walk_subtrees,
18514 void * data ATTRIBUTE_UNUSED)
18516 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18517 *walk_subtrees = 0;
18519 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18520 && ! TREE_ASM_WRITTEN (*tp))
18521 return *tp;
18522 /* ??? The C++ FE emits debug information for using decls, so
18523 putting gcc_unreachable here falls over. See PR31899. For now
18524 be conservative. */
18525 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18526 return *tp;
18527 else if (VAR_P (*tp))
18529 varpool_node *node = varpool_node::get (*tp);
18530 if (!node || !node->definition)
18531 return *tp;
18533 else if (TREE_CODE (*tp) == FUNCTION_DECL
18534 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18536 /* The call graph machinery must have finished analyzing,
18537 optimizing and gimplifying the CU by now.
18538 So if *TP has no call graph node associated
18539 to it, it means *TP will not be emitted. */
18540 if (!cgraph_node::get (*tp))
18541 return *tp;
18543 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18544 return *tp;
18546 return NULL_TREE;
18549 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18550 for use in a later add_const_value_attribute call. */
18552 static rtx
18553 rtl_for_decl_init (tree init, tree type)
18555 rtx rtl = NULL_RTX;
18557 STRIP_NOPS (init);
18559 /* If a variable is initialized with a string constant without embedded
18560 zeros, build CONST_STRING. */
18561 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18563 tree enttype = TREE_TYPE (type);
18564 tree domain = TYPE_DOMAIN (type);
18565 machine_mode mode = TYPE_MODE (enttype);
18567 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18568 && domain
18569 && integer_zerop (TYPE_MIN_VALUE (domain))
18570 && compare_tree_int (TYPE_MAX_VALUE (domain),
18571 TREE_STRING_LENGTH (init) - 1) == 0
18572 && ((size_t) TREE_STRING_LENGTH (init)
18573 == strlen (TREE_STRING_POINTER (init)) + 1))
18575 rtl = gen_rtx_CONST_STRING (VOIDmode,
18576 ggc_strdup (TREE_STRING_POINTER (init)));
18577 rtl = gen_rtx_MEM (BLKmode, rtl);
18578 MEM_READONLY_P (rtl) = 1;
18581 /* Other aggregates, and complex values, could be represented using
18582 CONCAT: FIXME! */
18583 else if (AGGREGATE_TYPE_P (type)
18584 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18585 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18586 || TREE_CODE (type) == COMPLEX_TYPE)
18588 /* Vectors only work if their mode is supported by the target.
18589 FIXME: generic vectors ought to work too. */
18590 else if (TREE_CODE (type) == VECTOR_TYPE
18591 && !VECTOR_MODE_P (TYPE_MODE (type)))
18593 /* If the initializer is something that we know will expand into an
18594 immediate RTL constant, expand it now. We must be careful not to
18595 reference variables which won't be output. */
18596 else if (initializer_constant_valid_p (init, type)
18597 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18599 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18600 possible. */
18601 if (TREE_CODE (type) == VECTOR_TYPE)
18602 switch (TREE_CODE (init))
18604 case VECTOR_CST:
18605 break;
18606 case CONSTRUCTOR:
18607 if (TREE_CONSTANT (init))
18609 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18610 bool constant_p = true;
18611 tree value;
18612 unsigned HOST_WIDE_INT ix;
18614 /* Even when ctor is constant, it might contain non-*_CST
18615 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18616 belong into VECTOR_CST nodes. */
18617 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18618 if (!CONSTANT_CLASS_P (value))
18620 constant_p = false;
18621 break;
18624 if (constant_p)
18626 init = build_vector_from_ctor (type, elts);
18627 break;
18630 /* FALLTHRU */
18632 default:
18633 return NULL;
18636 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18638 /* If expand_expr returns a MEM, it wasn't immediate. */
18639 gcc_assert (!rtl || !MEM_P (rtl));
18642 return rtl;
18645 /* Generate RTL for the variable DECL to represent its location. */
18647 static rtx
18648 rtl_for_decl_location (tree decl)
18650 rtx rtl;
18652 /* Here we have to decide where we are going to say the parameter "lives"
18653 (as far as the debugger is concerned). We only have a couple of
18654 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18656 DECL_RTL normally indicates where the parameter lives during most of the
18657 activation of the function. If optimization is enabled however, this
18658 could be either NULL or else a pseudo-reg. Both of those cases indicate
18659 that the parameter doesn't really live anywhere (as far as the code
18660 generation parts of GCC are concerned) during most of the function's
18661 activation. That will happen (for example) if the parameter is never
18662 referenced within the function.
18664 We could just generate a location descriptor here for all non-NULL
18665 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18666 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18667 where DECL_RTL is NULL or is a pseudo-reg.
18669 Note however that we can only get away with using DECL_INCOMING_RTL as
18670 a backup substitute for DECL_RTL in certain limited cases. In cases
18671 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18672 we can be sure that the parameter was passed using the same type as it is
18673 declared to have within the function, and that its DECL_INCOMING_RTL
18674 points us to a place where a value of that type is passed.
18676 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18677 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18678 because in these cases DECL_INCOMING_RTL points us to a value of some
18679 type which is *different* from the type of the parameter itself. Thus,
18680 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18681 such cases, the debugger would end up (for example) trying to fetch a
18682 `float' from a place which actually contains the first part of a
18683 `double'. That would lead to really incorrect and confusing
18684 output at debug-time.
18686 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18687 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18688 are a couple of exceptions however. On little-endian machines we can
18689 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18690 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18691 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18692 when (on a little-endian machine) a non-prototyped function has a
18693 parameter declared to be of type `short' or `char'. In such cases,
18694 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18695 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18696 passed `int' value. If the debugger then uses that address to fetch
18697 a `short' or a `char' (on a little-endian machine) the result will be
18698 the correct data, so we allow for such exceptional cases below.
18700 Note that our goal here is to describe the place where the given formal
18701 parameter lives during most of the function's activation (i.e. between the
18702 end of the prologue and the start of the epilogue). We'll do that as best
18703 as we can. Note however that if the given formal parameter is modified
18704 sometime during the execution of the function, then a stack backtrace (at
18705 debug-time) will show the function as having been called with the *new*
18706 value rather than the value which was originally passed in. This happens
18707 rarely enough that it is not a major problem, but it *is* a problem, and
18708 I'd like to fix it.
18710 A future version of dwarf2out.c may generate two additional attributes for
18711 any given DW_TAG_formal_parameter DIE which will describe the "passed
18712 type" and the "passed location" for the given formal parameter in addition
18713 to the attributes we now generate to indicate the "declared type" and the
18714 "active location" for each parameter. This additional set of attributes
18715 could be used by debuggers for stack backtraces. Separately, note that
18716 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18717 This happens (for example) for inlined-instances of inline function formal
18718 parameters which are never referenced. This really shouldn't be
18719 happening. All PARM_DECL nodes should get valid non-NULL
18720 DECL_INCOMING_RTL values. FIXME. */
18722 /* Use DECL_RTL as the "location" unless we find something better. */
18723 rtl = DECL_RTL_IF_SET (decl);
18725 /* When generating abstract instances, ignore everything except
18726 constants, symbols living in memory, and symbols living in
18727 fixed registers. */
18728 if (! reload_completed)
18730 if (rtl
18731 && (CONSTANT_P (rtl)
18732 || (MEM_P (rtl)
18733 && CONSTANT_P (XEXP (rtl, 0)))
18734 || (REG_P (rtl)
18735 && VAR_P (decl)
18736 && TREE_STATIC (decl))))
18738 rtl = targetm.delegitimize_address (rtl);
18739 return rtl;
18741 rtl = NULL_RTX;
18743 else if (TREE_CODE (decl) == PARM_DECL)
18745 if (rtl == NULL_RTX
18746 || is_pseudo_reg (rtl)
18747 || (MEM_P (rtl)
18748 && is_pseudo_reg (XEXP (rtl, 0))
18749 && DECL_INCOMING_RTL (decl)
18750 && MEM_P (DECL_INCOMING_RTL (decl))
18751 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18753 tree declared_type = TREE_TYPE (decl);
18754 tree passed_type = DECL_ARG_TYPE (decl);
18755 machine_mode dmode = TYPE_MODE (declared_type);
18756 machine_mode pmode = TYPE_MODE (passed_type);
18758 /* This decl represents a formal parameter which was optimized out.
18759 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18760 all cases where (rtl == NULL_RTX) just below. */
18761 if (dmode == pmode)
18762 rtl = DECL_INCOMING_RTL (decl);
18763 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18764 && SCALAR_INT_MODE_P (dmode)
18765 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18766 && DECL_INCOMING_RTL (decl))
18768 rtx inc = DECL_INCOMING_RTL (decl);
18769 if (REG_P (inc))
18770 rtl = inc;
18771 else if (MEM_P (inc))
18773 if (BYTES_BIG_ENDIAN)
18774 rtl = adjust_address_nv (inc, dmode,
18775 GET_MODE_SIZE (pmode)
18776 - GET_MODE_SIZE (dmode));
18777 else
18778 rtl = inc;
18783 /* If the parm was passed in registers, but lives on the stack, then
18784 make a big endian correction if the mode of the type of the
18785 parameter is not the same as the mode of the rtl. */
18786 /* ??? This is the same series of checks that are made in dbxout.c before
18787 we reach the big endian correction code there. It isn't clear if all
18788 of these checks are necessary here, but keeping them all is the safe
18789 thing to do. */
18790 else if (MEM_P (rtl)
18791 && XEXP (rtl, 0) != const0_rtx
18792 && ! CONSTANT_P (XEXP (rtl, 0))
18793 /* Not passed in memory. */
18794 && !MEM_P (DECL_INCOMING_RTL (decl))
18795 /* Not passed by invisible reference. */
18796 && (!REG_P (XEXP (rtl, 0))
18797 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18798 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18799 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18800 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18801 #endif
18803 /* Big endian correction check. */
18804 && BYTES_BIG_ENDIAN
18805 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18806 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18807 < UNITS_PER_WORD))
18809 machine_mode addr_mode = get_address_mode (rtl);
18810 int offset = (UNITS_PER_WORD
18811 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18813 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18814 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18817 else if (VAR_P (decl)
18818 && rtl
18819 && MEM_P (rtl)
18820 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18821 && BYTES_BIG_ENDIAN)
18823 machine_mode addr_mode = get_address_mode (rtl);
18824 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18825 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18827 /* If a variable is declared "register" yet is smaller than
18828 a register, then if we store the variable to memory, it
18829 looks like we're storing a register-sized value, when in
18830 fact we are not. We need to adjust the offset of the
18831 storage location to reflect the actual value's bytes,
18832 else gdb will not be able to display it. */
18833 if (rsize > dsize)
18834 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18835 plus_constant (addr_mode, XEXP (rtl, 0),
18836 rsize - dsize));
18839 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
18840 and will have been substituted directly into all expressions that use it.
18841 C does not have such a concept, but C++ and other languages do. */
18842 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
18843 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
18845 if (rtl)
18846 rtl = targetm.delegitimize_address (rtl);
18848 /* If we don't look past the constant pool, we risk emitting a
18849 reference to a constant pool entry that isn't referenced from
18850 code, and thus is not emitted. */
18851 if (rtl)
18852 rtl = avoid_constant_pool_reference (rtl);
18854 /* Try harder to get a rtl. If this symbol ends up not being emitted
18855 in the current CU, resolve_addr will remove the expression referencing
18856 it. */
18857 if (rtl == NULL_RTX
18858 && VAR_P (decl)
18859 && !DECL_EXTERNAL (decl)
18860 && TREE_STATIC (decl)
18861 && DECL_NAME (decl)
18862 && !DECL_HARD_REGISTER (decl)
18863 && DECL_MODE (decl) != VOIDmode)
18865 rtl = make_decl_rtl_for_debug (decl);
18866 if (!MEM_P (rtl)
18867 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
18868 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
18869 rtl = NULL_RTX;
18872 return rtl;
18875 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
18876 returned. If so, the decl for the COMMON block is returned, and the
18877 value is the offset into the common block for the symbol. */
18879 static tree
18880 fortran_common (tree decl, HOST_WIDE_INT *value)
18882 tree val_expr, cvar;
18883 machine_mode mode;
18884 HOST_WIDE_INT bitsize, bitpos;
18885 tree offset;
18886 int unsignedp, reversep, volatilep = 0;
18888 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
18889 it does not have a value (the offset into the common area), or if it
18890 is thread local (as opposed to global) then it isn't common, and shouldn't
18891 be handled as such. */
18892 if (!VAR_P (decl)
18893 || !TREE_STATIC (decl)
18894 || !DECL_HAS_VALUE_EXPR_P (decl)
18895 || !is_fortran ())
18896 return NULL_TREE;
18898 val_expr = DECL_VALUE_EXPR (decl);
18899 if (TREE_CODE (val_expr) != COMPONENT_REF)
18900 return NULL_TREE;
18902 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
18903 &unsignedp, &reversep, &volatilep);
18905 if (cvar == NULL_TREE
18906 || !VAR_P (cvar)
18907 || DECL_ARTIFICIAL (cvar)
18908 || !TREE_PUBLIC (cvar))
18909 return NULL_TREE;
18911 *value = 0;
18912 if (offset != NULL)
18914 if (!tree_fits_shwi_p (offset))
18915 return NULL_TREE;
18916 *value = tree_to_shwi (offset);
18918 if (bitpos != 0)
18919 *value += bitpos / BITS_PER_UNIT;
18921 return cvar;
18924 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
18925 data attribute for a variable or a parameter. We generate the
18926 DW_AT_const_value attribute only in those cases where the given variable
18927 or parameter does not have a true "location" either in memory or in a
18928 register. This can happen (for example) when a constant is passed as an
18929 actual argument in a call to an inline function. (It's possible that
18930 these things can crop up in other ways also.) Note that one type of
18931 constant value which can be passed into an inlined function is a constant
18932 pointer. This can happen for example if an actual argument in an inlined
18933 function call evaluates to a compile-time constant address.
18935 CACHE_P is true if it is worth caching the location list for DECL,
18936 so that future calls can reuse it rather than regenerate it from scratch.
18937 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
18938 since we will need to refer to them each time the function is inlined. */
18940 static bool
18941 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
18943 rtx rtl;
18944 dw_loc_list_ref list;
18945 var_loc_list *loc_list;
18946 cached_dw_loc_list *cache;
18948 if (early_dwarf)
18949 return false;
18951 if (TREE_CODE (decl) == ERROR_MARK)
18952 return false;
18954 if (get_AT (die, DW_AT_location)
18955 || get_AT (die, DW_AT_const_value))
18956 return true;
18958 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
18959 || TREE_CODE (decl) == RESULT_DECL);
18961 /* Try to get some constant RTL for this decl, and use that as the value of
18962 the location. */
18964 rtl = rtl_for_decl_location (decl);
18965 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18966 && add_const_value_attribute (die, rtl))
18967 return true;
18969 /* See if we have single element location list that is equivalent to
18970 a constant value. That way we are better to use add_const_value_attribute
18971 rather than expanding constant value equivalent. */
18972 loc_list = lookup_decl_loc (decl);
18973 if (loc_list
18974 && loc_list->first
18975 && loc_list->first->next == NULL
18976 && NOTE_P (loc_list->first->loc)
18977 && NOTE_VAR_LOCATION (loc_list->first->loc)
18978 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
18980 struct var_loc_node *node;
18982 node = loc_list->first;
18983 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
18984 if (GET_CODE (rtl) == EXPR_LIST)
18985 rtl = XEXP (rtl, 0);
18986 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18987 && add_const_value_attribute (die, rtl))
18988 return true;
18990 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
18991 list several times. See if we've already cached the contents. */
18992 list = NULL;
18993 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
18994 cache_p = false;
18995 if (cache_p)
18997 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
18998 if (cache)
18999 list = cache->loc_list;
19001 if (list == NULL)
19003 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19004 NULL);
19005 /* It is usually worth caching this result if the decl is from
19006 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19007 if (cache_p && list && list->dw_loc_next)
19009 cached_dw_loc_list **slot
19010 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19011 DECL_UID (decl),
19012 INSERT);
19013 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19014 cache->decl_id = DECL_UID (decl);
19015 cache->loc_list = list;
19016 *slot = cache;
19019 if (list)
19021 add_AT_location_description (die, DW_AT_location, list);
19022 return true;
19024 /* None of that worked, so it must not really have a location;
19025 try adding a constant value attribute from the DECL_INITIAL. */
19026 return tree_add_const_value_attribute_for_decl (die, decl);
19029 /* Helper function for tree_add_const_value_attribute. Natively encode
19030 initializer INIT into an array. Return true if successful. */
19032 static bool
19033 native_encode_initializer (tree init, unsigned char *array, int size)
19035 tree type;
19037 if (init == NULL_TREE)
19038 return false;
19040 STRIP_NOPS (init);
19041 switch (TREE_CODE (init))
19043 case STRING_CST:
19044 type = TREE_TYPE (init);
19045 if (TREE_CODE (type) == ARRAY_TYPE)
19047 tree enttype = TREE_TYPE (type);
19048 machine_mode mode = TYPE_MODE (enttype);
19050 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19051 return false;
19052 if (int_size_in_bytes (type) != size)
19053 return false;
19054 if (size > TREE_STRING_LENGTH (init))
19056 memcpy (array, TREE_STRING_POINTER (init),
19057 TREE_STRING_LENGTH (init));
19058 memset (array + TREE_STRING_LENGTH (init),
19059 '\0', size - TREE_STRING_LENGTH (init));
19061 else
19062 memcpy (array, TREE_STRING_POINTER (init), size);
19063 return true;
19065 return false;
19066 case CONSTRUCTOR:
19067 type = TREE_TYPE (init);
19068 if (int_size_in_bytes (type) != size)
19069 return false;
19070 if (TREE_CODE (type) == ARRAY_TYPE)
19072 HOST_WIDE_INT min_index;
19073 unsigned HOST_WIDE_INT cnt;
19074 int curpos = 0, fieldsize;
19075 constructor_elt *ce;
19077 if (TYPE_DOMAIN (type) == NULL_TREE
19078 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19079 return false;
19081 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19082 if (fieldsize <= 0)
19083 return false;
19085 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19086 memset (array, '\0', size);
19087 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19089 tree val = ce->value;
19090 tree index = ce->index;
19091 int pos = curpos;
19092 if (index && TREE_CODE (index) == RANGE_EXPR)
19093 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19094 * fieldsize;
19095 else if (index)
19096 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19098 if (val)
19100 STRIP_NOPS (val);
19101 if (!native_encode_initializer (val, array + pos, fieldsize))
19102 return false;
19104 curpos = pos + fieldsize;
19105 if (index && TREE_CODE (index) == RANGE_EXPR)
19107 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19108 - tree_to_shwi (TREE_OPERAND (index, 0));
19109 while (count-- > 0)
19111 if (val)
19112 memcpy (array + curpos, array + pos, fieldsize);
19113 curpos += fieldsize;
19116 gcc_assert (curpos <= size);
19118 return true;
19120 else if (TREE_CODE (type) == RECORD_TYPE
19121 || TREE_CODE (type) == UNION_TYPE)
19123 tree field = NULL_TREE;
19124 unsigned HOST_WIDE_INT cnt;
19125 constructor_elt *ce;
19127 if (int_size_in_bytes (type) != size)
19128 return false;
19130 if (TREE_CODE (type) == RECORD_TYPE)
19131 field = TYPE_FIELDS (type);
19133 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19135 tree val = ce->value;
19136 int pos, fieldsize;
19138 if (ce->index != 0)
19139 field = ce->index;
19141 if (val)
19142 STRIP_NOPS (val);
19144 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19145 return false;
19147 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19148 && TYPE_DOMAIN (TREE_TYPE (field))
19149 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19150 return false;
19151 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19152 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19153 return false;
19154 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19155 pos = int_byte_position (field);
19156 gcc_assert (pos + fieldsize <= size);
19157 if (val && fieldsize != 0
19158 && !native_encode_initializer (val, array + pos, fieldsize))
19159 return false;
19161 return true;
19163 return false;
19164 case VIEW_CONVERT_EXPR:
19165 case NON_LVALUE_EXPR:
19166 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19167 default:
19168 return native_encode_expr (init, array, size) == size;
19172 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19173 attribute is the const value T. */
19175 static bool
19176 tree_add_const_value_attribute (dw_die_ref die, tree t)
19178 tree init;
19179 tree type = TREE_TYPE (t);
19180 rtx rtl;
19182 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19183 return false;
19185 init = t;
19186 gcc_assert (!DECL_P (init));
19188 if (! early_dwarf)
19190 rtl = rtl_for_decl_init (init, type);
19191 if (rtl)
19192 return add_const_value_attribute (die, rtl);
19194 /* If the host and target are sane, try harder. */
19195 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19196 && initializer_constant_valid_p (init, type))
19198 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19199 if (size > 0 && (int) size == size)
19201 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19203 if (native_encode_initializer (init, array, size))
19205 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19206 return true;
19208 ggc_free (array);
19211 return false;
19214 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19215 attribute is the const value of T, where T is an integral constant
19216 variable with static storage duration
19217 (so it can't be a PARM_DECL or a RESULT_DECL). */
19219 static bool
19220 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19223 if (!decl
19224 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19225 || (VAR_P (decl) && !TREE_STATIC (decl)))
19226 return false;
19228 if (TREE_READONLY (decl)
19229 && ! TREE_THIS_VOLATILE (decl)
19230 && DECL_INITIAL (decl))
19231 /* OK */;
19232 else
19233 return false;
19235 /* Don't add DW_AT_const_value if abstract origin already has one. */
19236 if (get_AT (var_die, DW_AT_const_value))
19237 return false;
19239 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19242 /* Convert the CFI instructions for the current function into a
19243 location list. This is used for DW_AT_frame_base when we targeting
19244 a dwarf2 consumer that does not support the dwarf3
19245 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19246 expressions. */
19248 static dw_loc_list_ref
19249 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19251 int ix;
19252 dw_fde_ref fde;
19253 dw_loc_list_ref list, *list_tail;
19254 dw_cfi_ref cfi;
19255 dw_cfa_location last_cfa, next_cfa;
19256 const char *start_label, *last_label, *section;
19257 dw_cfa_location remember;
19259 fde = cfun->fde;
19260 gcc_assert (fde != NULL);
19262 section = secname_for_decl (current_function_decl);
19263 list_tail = &list;
19264 list = NULL;
19266 memset (&next_cfa, 0, sizeof (next_cfa));
19267 next_cfa.reg = INVALID_REGNUM;
19268 remember = next_cfa;
19270 start_label = fde->dw_fde_begin;
19272 /* ??? Bald assumption that the CIE opcode list does not contain
19273 advance opcodes. */
19274 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19275 lookup_cfa_1 (cfi, &next_cfa, &remember);
19277 last_cfa = next_cfa;
19278 last_label = start_label;
19280 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19282 /* If the first partition contained no CFI adjustments, the
19283 CIE opcodes apply to the whole first partition. */
19284 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19285 fde->dw_fde_begin, fde->dw_fde_end, section);
19286 list_tail =&(*list_tail)->dw_loc_next;
19287 start_label = last_label = fde->dw_fde_second_begin;
19290 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19292 switch (cfi->dw_cfi_opc)
19294 case DW_CFA_set_loc:
19295 case DW_CFA_advance_loc1:
19296 case DW_CFA_advance_loc2:
19297 case DW_CFA_advance_loc4:
19298 if (!cfa_equal_p (&last_cfa, &next_cfa))
19300 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19301 start_label, last_label, section);
19303 list_tail = &(*list_tail)->dw_loc_next;
19304 last_cfa = next_cfa;
19305 start_label = last_label;
19307 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19308 break;
19310 case DW_CFA_advance_loc:
19311 /* The encoding is complex enough that we should never emit this. */
19312 gcc_unreachable ();
19314 default:
19315 lookup_cfa_1 (cfi, &next_cfa, &remember);
19316 break;
19318 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19320 if (!cfa_equal_p (&last_cfa, &next_cfa))
19322 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19323 start_label, last_label, section);
19325 list_tail = &(*list_tail)->dw_loc_next;
19326 last_cfa = next_cfa;
19327 start_label = last_label;
19329 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19330 start_label, fde->dw_fde_end, section);
19331 list_tail = &(*list_tail)->dw_loc_next;
19332 start_label = last_label = fde->dw_fde_second_begin;
19336 if (!cfa_equal_p (&last_cfa, &next_cfa))
19338 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19339 start_label, last_label, section);
19340 list_tail = &(*list_tail)->dw_loc_next;
19341 start_label = last_label;
19344 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19345 start_label,
19346 fde->dw_fde_second_begin
19347 ? fde->dw_fde_second_end : fde->dw_fde_end,
19348 section);
19350 if (list && list->dw_loc_next)
19351 gen_llsym (list);
19353 return list;
19356 /* Compute a displacement from the "steady-state frame pointer" to the
19357 frame base (often the same as the CFA), and store it in
19358 frame_pointer_fb_offset. OFFSET is added to the displacement
19359 before the latter is negated. */
19361 static void
19362 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19364 rtx reg, elim;
19366 #ifdef FRAME_POINTER_CFA_OFFSET
19367 reg = frame_pointer_rtx;
19368 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19369 #else
19370 reg = arg_pointer_rtx;
19371 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19372 #endif
19374 elim = (ira_use_lra_p
19375 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19376 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19377 if (GET_CODE (elim) == PLUS)
19379 offset += INTVAL (XEXP (elim, 1));
19380 elim = XEXP (elim, 0);
19383 frame_pointer_fb_offset = -offset;
19385 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19386 in which to eliminate. This is because it's stack pointer isn't
19387 directly accessible as a register within the ISA. To work around
19388 this, assume that while we cannot provide a proper value for
19389 frame_pointer_fb_offset, we won't need one either. */
19390 frame_pointer_fb_offset_valid
19391 = ((SUPPORTS_STACK_ALIGNMENT
19392 && (elim == hard_frame_pointer_rtx
19393 || elim == stack_pointer_rtx))
19394 || elim == (frame_pointer_needed
19395 ? hard_frame_pointer_rtx
19396 : stack_pointer_rtx));
19399 /* Generate a DW_AT_name attribute given some string value to be included as
19400 the value of the attribute. */
19402 static void
19403 add_name_attribute (dw_die_ref die, const char *name_string)
19405 if (name_string != NULL && *name_string != 0)
19407 if (demangle_name_func)
19408 name_string = (*demangle_name_func) (name_string);
19410 add_AT_string (die, DW_AT_name, name_string);
19414 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19415 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19416 of TYPE accordingly.
19418 ??? This is a temporary measure until after we're able to generate
19419 regular DWARF for the complex Ada type system. */
19421 static void
19422 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19423 dw_die_ref context_die)
19425 tree dtype;
19426 dw_die_ref dtype_die;
19428 if (!lang_hooks.types.descriptive_type)
19429 return;
19431 dtype = lang_hooks.types.descriptive_type (type);
19432 if (!dtype)
19433 return;
19435 dtype_die = lookup_type_die (dtype);
19436 if (!dtype_die)
19438 gen_type_die (dtype, context_die);
19439 dtype_die = lookup_type_die (dtype);
19440 gcc_assert (dtype_die);
19443 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19446 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19448 static const char *
19449 comp_dir_string (void)
19451 const char *wd;
19452 char *wd1;
19453 static const char *cached_wd = NULL;
19455 if (cached_wd != NULL)
19456 return cached_wd;
19458 wd = get_src_pwd ();
19459 if (wd == NULL)
19460 return NULL;
19462 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19464 int wdlen;
19466 wdlen = strlen (wd);
19467 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19468 strcpy (wd1, wd);
19469 wd1 [wdlen] = DIR_SEPARATOR;
19470 wd1 [wdlen + 1] = 0;
19471 wd = wd1;
19474 cached_wd = remap_debug_filename (wd);
19475 return cached_wd;
19478 /* Generate a DW_AT_comp_dir attribute for DIE. */
19480 static void
19481 add_comp_dir_attribute (dw_die_ref die)
19483 const char * wd = comp_dir_string ();
19484 if (wd != NULL)
19485 add_AT_string (die, DW_AT_comp_dir, wd);
19488 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19489 pointer computation, ...), output a representation for that bound according
19490 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19491 loc_list_from_tree for the meaning of CONTEXT. */
19493 static void
19494 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19495 int forms, struct loc_descr_context *context)
19497 dw_die_ref context_die, decl_die;
19498 dw_loc_list_ref list;
19499 bool strip_conversions = true;
19500 bool placeholder_seen = false;
19502 while (strip_conversions)
19503 switch (TREE_CODE (value))
19505 case ERROR_MARK:
19506 case SAVE_EXPR:
19507 return;
19509 CASE_CONVERT:
19510 case VIEW_CONVERT_EXPR:
19511 value = TREE_OPERAND (value, 0);
19512 break;
19514 default:
19515 strip_conversions = false;
19516 break;
19519 /* If possible and permitted, output the attribute as a constant. */
19520 if ((forms & dw_scalar_form_constant) != 0
19521 && TREE_CODE (value) == INTEGER_CST)
19523 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19525 /* If HOST_WIDE_INT is big enough then represent the bound as
19526 a constant value. We need to choose a form based on
19527 whether the type is signed or unsigned. We cannot just
19528 call add_AT_unsigned if the value itself is positive
19529 (add_AT_unsigned might add the unsigned value encoded as
19530 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19531 bounds type and then sign extend any unsigned values found
19532 for signed types. This is needed only for
19533 DW_AT_{lower,upper}_bound, since for most other attributes,
19534 consumers will treat DW_FORM_data[1248] as unsigned values,
19535 regardless of the underlying type. */
19536 if (prec <= HOST_BITS_PER_WIDE_INT
19537 || tree_fits_uhwi_p (value))
19539 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19540 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19541 else
19542 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19544 else
19545 /* Otherwise represent the bound as an unsigned value with
19546 the precision of its type. The precision and signedness
19547 of the type will be necessary to re-interpret it
19548 unambiguously. */
19549 add_AT_wide (die, attr, value);
19550 return;
19553 /* Otherwise, if it's possible and permitted too, output a reference to
19554 another DIE. */
19555 if ((forms & dw_scalar_form_reference) != 0)
19557 tree decl = NULL_TREE;
19559 /* Some type attributes reference an outer type. For instance, the upper
19560 bound of an array may reference an embedding record (this happens in
19561 Ada). */
19562 if (TREE_CODE (value) == COMPONENT_REF
19563 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19564 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19565 decl = TREE_OPERAND (value, 1);
19567 else if (VAR_P (value)
19568 || TREE_CODE (value) == PARM_DECL
19569 || TREE_CODE (value) == RESULT_DECL)
19570 decl = value;
19572 if (decl != NULL_TREE)
19574 dw_die_ref decl_die = lookup_decl_die (decl);
19576 /* ??? Can this happen, or should the variable have been bound
19577 first? Probably it can, since I imagine that we try to create
19578 the types of parameters in the order in which they exist in
19579 the list, and won't have created a forward reference to a
19580 later parameter. */
19581 if (decl_die != NULL)
19583 add_AT_die_ref (die, attr, decl_die);
19584 return;
19589 /* Last chance: try to create a stack operation procedure to evaluate the
19590 value. Do nothing if even that is not possible or permitted. */
19591 if ((forms & dw_scalar_form_exprloc) == 0)
19592 return;
19594 list = loc_list_from_tree (value, 2, context);
19595 if (context && context->placeholder_arg)
19597 placeholder_seen = context->placeholder_seen;
19598 context->placeholder_seen = false;
19600 if (list == NULL || single_element_loc_list_p (list))
19602 /* If this attribute is not a reference nor constant, it is
19603 a DWARF expression rather than location description. For that
19604 loc_list_from_tree (value, 0, &context) is needed. */
19605 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19606 if (list2 && single_element_loc_list_p (list2))
19608 if (placeholder_seen)
19610 struct dwarf_procedure_info dpi;
19611 dpi.fndecl = NULL_TREE;
19612 dpi.args_count = 1;
19613 if (!resolve_args_picking (list2->expr, 1, &dpi))
19614 return;
19616 add_AT_loc (die, attr, list2->expr);
19617 return;
19621 /* If that failed to give a single element location list, fall back to
19622 outputting this as a reference... still if permitted. */
19623 if (list == NULL
19624 || (forms & dw_scalar_form_reference) == 0
19625 || placeholder_seen)
19626 return;
19628 if (current_function_decl == 0)
19629 context_die = comp_unit_die ();
19630 else
19631 context_die = lookup_decl_die (current_function_decl);
19633 decl_die = new_die (DW_TAG_variable, context_die, value);
19634 add_AT_flag (decl_die, DW_AT_artificial, 1);
19635 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19636 context_die);
19637 add_AT_location_description (decl_die, DW_AT_location, list);
19638 add_AT_die_ref (die, attr, decl_die);
19641 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19642 default. */
19644 static int
19645 lower_bound_default (void)
19647 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19649 case DW_LANG_C:
19650 case DW_LANG_C89:
19651 case DW_LANG_C99:
19652 case DW_LANG_C11:
19653 case DW_LANG_C_plus_plus:
19654 case DW_LANG_C_plus_plus_11:
19655 case DW_LANG_C_plus_plus_14:
19656 case DW_LANG_ObjC:
19657 case DW_LANG_ObjC_plus_plus:
19658 case DW_LANG_Java:
19659 return 0;
19660 case DW_LANG_Fortran77:
19661 case DW_LANG_Fortran90:
19662 case DW_LANG_Fortran95:
19663 case DW_LANG_Fortran03:
19664 case DW_LANG_Fortran08:
19665 return 1;
19666 case DW_LANG_UPC:
19667 case DW_LANG_D:
19668 case DW_LANG_Python:
19669 return dwarf_version >= 4 ? 0 : -1;
19670 case DW_LANG_Ada95:
19671 case DW_LANG_Ada83:
19672 case DW_LANG_Cobol74:
19673 case DW_LANG_Cobol85:
19674 case DW_LANG_Pascal83:
19675 case DW_LANG_Modula2:
19676 case DW_LANG_PLI:
19677 return dwarf_version >= 4 ? 1 : -1;
19678 default:
19679 return -1;
19683 /* Given a tree node describing an array bound (either lower or upper) output
19684 a representation for that bound. */
19686 static void
19687 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19688 tree bound, struct loc_descr_context *context)
19690 int dflt;
19692 while (1)
19693 switch (TREE_CODE (bound))
19695 /* Strip all conversions. */
19696 CASE_CONVERT:
19697 case VIEW_CONVERT_EXPR:
19698 bound = TREE_OPERAND (bound, 0);
19699 break;
19701 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19702 are even omitted when they are the default. */
19703 case INTEGER_CST:
19704 /* If the value for this bound is the default one, we can even omit the
19705 attribute. */
19706 if (bound_attr == DW_AT_lower_bound
19707 && tree_fits_shwi_p (bound)
19708 && (dflt = lower_bound_default ()) != -1
19709 && tree_to_shwi (bound) == dflt)
19710 return;
19712 /* FALLTHRU */
19714 default:
19715 /* Because of the complex interaction there can be with other GNAT
19716 encodings, GDB isn't ready yet to handle proper DWARF description
19717 for self-referencial subrange bounds: let GNAT encodings do the
19718 magic in such a case. */
19719 if (is_ada ()
19720 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19721 && contains_placeholder_p (bound))
19722 return;
19724 add_scalar_info (subrange_die, bound_attr, bound,
19725 dw_scalar_form_constant
19726 | dw_scalar_form_exprloc
19727 | dw_scalar_form_reference,
19728 context);
19729 return;
19733 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19734 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19735 Note that the block of subscript information for an array type also
19736 includes information about the element type of the given array type.
19738 This function reuses previously set type and bound information if
19739 available. */
19741 static void
19742 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19744 unsigned dimension_number;
19745 tree lower, upper;
19746 dw_die_ref child = type_die->die_child;
19748 for (dimension_number = 0;
19749 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19750 type = TREE_TYPE (type), dimension_number++)
19752 tree domain = TYPE_DOMAIN (type);
19754 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19755 break;
19757 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19758 and (in GNU C only) variable bounds. Handle all three forms
19759 here. */
19761 /* Find and reuse a previously generated DW_TAG_subrange_type if
19762 available.
19764 For multi-dimensional arrays, as we iterate through the
19765 various dimensions in the enclosing for loop above, we also
19766 iterate through the DIE children and pick at each
19767 DW_TAG_subrange_type previously generated (if available).
19768 Each child DW_TAG_subrange_type DIE describes the range of
19769 the current dimension. At this point we should have as many
19770 DW_TAG_subrange_type's as we have dimensions in the
19771 array. */
19772 dw_die_ref subrange_die = NULL;
19773 if (child)
19774 while (1)
19776 child = child->die_sib;
19777 if (child->die_tag == DW_TAG_subrange_type)
19778 subrange_die = child;
19779 if (child == type_die->die_child)
19781 /* If we wrapped around, stop looking next time. */
19782 child = NULL;
19783 break;
19785 if (child->die_tag == DW_TAG_subrange_type)
19786 break;
19788 if (!subrange_die)
19789 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19791 if (domain)
19793 /* We have an array type with specified bounds. */
19794 lower = TYPE_MIN_VALUE (domain);
19795 upper = TYPE_MAX_VALUE (domain);
19797 /* Define the index type. */
19798 if (TREE_TYPE (domain)
19799 && !get_AT (subrange_die, DW_AT_type))
19801 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19802 TREE_TYPE field. We can't emit debug info for this
19803 because it is an unnamed integral type. */
19804 if (TREE_CODE (domain) == INTEGER_TYPE
19805 && TYPE_NAME (domain) == NULL_TREE
19806 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19807 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19809 else
19810 add_type_attribute (subrange_die, TREE_TYPE (domain),
19811 TYPE_UNQUALIFIED, false, type_die);
19814 /* ??? If upper is NULL, the array has unspecified length,
19815 but it does have a lower bound. This happens with Fortran
19816 dimension arr(N:*)
19817 Since the debugger is definitely going to need to know N
19818 to produce useful results, go ahead and output the lower
19819 bound solo, and hope the debugger can cope. */
19821 if (!get_AT (subrange_die, DW_AT_lower_bound))
19822 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19823 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19824 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19827 /* Otherwise we have an array type with an unspecified length. The
19828 DWARF-2 spec does not say how to handle this; let's just leave out the
19829 bounds. */
19833 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19835 static void
19836 add_byte_size_attribute (dw_die_ref die, tree tree_node)
19838 dw_die_ref decl_die;
19839 HOST_WIDE_INT size;
19840 dw_loc_descr_ref size_expr = NULL;
19842 switch (TREE_CODE (tree_node))
19844 case ERROR_MARK:
19845 size = 0;
19846 break;
19847 case ENUMERAL_TYPE:
19848 case RECORD_TYPE:
19849 case UNION_TYPE:
19850 case QUAL_UNION_TYPE:
19851 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
19852 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
19854 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
19855 return;
19857 size_expr = type_byte_size (tree_node, &size);
19858 break;
19859 case FIELD_DECL:
19860 /* For a data member of a struct or union, the DW_AT_byte_size is
19861 generally given as the number of bytes normally allocated for an
19862 object of the *declared* type of the member itself. This is true
19863 even for bit-fields. */
19864 size = int_size_in_bytes (field_type (tree_node));
19865 break;
19866 default:
19867 gcc_unreachable ();
19870 /* Support for dynamically-sized objects was introduced by DWARFv3.
19871 At the moment, GDB does not handle variable byte sizes very well,
19872 though. */
19873 if ((dwarf_version >= 3 || !dwarf_strict)
19874 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
19875 && size_expr != NULL)
19876 add_AT_loc (die, DW_AT_byte_size, size_expr);
19878 /* Note that `size' might be -1 when we get to this point. If it is, that
19879 indicates that the byte size of the entity in question is variable and
19880 that we could not generate a DWARF expression that computes it. */
19881 if (size >= 0)
19882 add_AT_unsigned (die, DW_AT_byte_size, size);
19885 /* For a FIELD_DECL node which represents a bit-field, output an attribute
19886 which specifies the distance in bits from the highest order bit of the
19887 "containing object" for the bit-field to the highest order bit of the
19888 bit-field itself.
19890 For any given bit-field, the "containing object" is a hypothetical object
19891 (of some integral or enum type) within which the given bit-field lives. The
19892 type of this hypothetical "containing object" is always the same as the
19893 declared type of the individual bit-field itself. The determination of the
19894 exact location of the "containing object" for a bit-field is rather
19895 complicated. It's handled by the `field_byte_offset' function (above).
19897 CTX is required: see the comment for VLR_CONTEXT.
19899 Note that it is the size (in bytes) of the hypothetical "containing object"
19900 which will be given in the DW_AT_byte_size attribute for this bit-field.
19901 (See `byte_size_attribute' above). */
19903 static inline void
19904 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
19906 HOST_WIDE_INT object_offset_in_bytes;
19907 tree original_type = DECL_BIT_FIELD_TYPE (decl);
19908 HOST_WIDE_INT bitpos_int;
19909 HOST_WIDE_INT highest_order_object_bit_offset;
19910 HOST_WIDE_INT highest_order_field_bit_offset;
19911 HOST_WIDE_INT bit_offset;
19913 field_byte_offset (decl, ctx, &object_offset_in_bytes);
19915 /* Must be a field and a bit field. */
19916 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
19918 /* We can't yet handle bit-fields whose offsets are variable, so if we
19919 encounter such things, just return without generating any attribute
19920 whatsoever. Likewise for variable or too large size. */
19921 if (! tree_fits_shwi_p (bit_position (decl))
19922 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
19923 return;
19925 bitpos_int = int_bit_position (decl);
19927 /* Note that the bit offset is always the distance (in bits) from the
19928 highest-order bit of the "containing object" to the highest-order bit of
19929 the bit-field itself. Since the "high-order end" of any object or field
19930 is different on big-endian and little-endian machines, the computation
19931 below must take account of these differences. */
19932 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
19933 highest_order_field_bit_offset = bitpos_int;
19935 if (! BYTES_BIG_ENDIAN)
19937 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
19938 highest_order_object_bit_offset +=
19939 simple_type_size_in_bits (original_type);
19942 bit_offset
19943 = (! BYTES_BIG_ENDIAN
19944 ? highest_order_object_bit_offset - highest_order_field_bit_offset
19945 : highest_order_field_bit_offset - highest_order_object_bit_offset);
19947 if (bit_offset < 0)
19948 add_AT_int (die, DW_AT_bit_offset, bit_offset);
19949 else
19950 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
19953 /* For a FIELD_DECL node which represents a bit field, output an attribute
19954 which specifies the length in bits of the given field. */
19956 static inline void
19957 add_bit_size_attribute (dw_die_ref die, tree decl)
19959 /* Must be a field and a bit field. */
19960 gcc_assert (TREE_CODE (decl) == FIELD_DECL
19961 && DECL_BIT_FIELD_TYPE (decl));
19963 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
19964 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
19967 /* If the compiled language is ANSI C, then add a 'prototyped'
19968 attribute, if arg types are given for the parameters of a function. */
19970 static inline void
19971 add_prototyped_attribute (dw_die_ref die, tree func_type)
19973 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19975 case DW_LANG_C:
19976 case DW_LANG_C89:
19977 case DW_LANG_C99:
19978 case DW_LANG_C11:
19979 case DW_LANG_ObjC:
19980 if (prototype_p (func_type))
19981 add_AT_flag (die, DW_AT_prototyped, 1);
19982 break;
19983 default:
19984 break;
19988 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
19989 by looking in the type declaration, the object declaration equate table or
19990 the block mapping. */
19992 static inline dw_die_ref
19993 add_abstract_origin_attribute (dw_die_ref die, tree origin)
19995 dw_die_ref origin_die = NULL;
19997 if (TREE_CODE (origin) != FUNCTION_DECL
19998 && TREE_CODE (origin) != BLOCK)
20000 /* We may have gotten separated from the block for the inlined
20001 function, if we're in an exception handler or some such; make
20002 sure that the abstract function has been written out.
20004 Doing this for nested functions is wrong, however; functions are
20005 distinct units, and our context might not even be inline. */
20006 tree fn = origin;
20008 if (TYPE_P (fn))
20009 fn = TYPE_STUB_DECL (fn);
20011 fn = decl_function_context (fn);
20012 if (fn)
20013 dwarf2out_abstract_function (fn);
20016 if (DECL_P (origin))
20017 origin_die = lookup_decl_die (origin);
20018 else if (TYPE_P (origin))
20019 origin_die = lookup_type_die (origin);
20020 else if (TREE_CODE (origin) == BLOCK)
20021 origin_die = BLOCK_DIE (origin);
20023 /* XXX: Functions that are never lowered don't always have correct block
20024 trees (in the case of java, they simply have no block tree, in some other
20025 languages). For these functions, there is nothing we can really do to
20026 output correct debug info for inlined functions in all cases. Rather
20027 than die, we'll just produce deficient debug info now, in that we will
20028 have variables without a proper abstract origin. In the future, when all
20029 functions are lowered, we should re-add a gcc_assert (origin_die)
20030 here. */
20032 if (origin_die)
20033 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20034 return origin_die;
20037 /* We do not currently support the pure_virtual attribute. */
20039 static inline void
20040 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20042 if (DECL_VINDEX (func_decl))
20044 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20046 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20047 add_AT_loc (die, DW_AT_vtable_elem_location,
20048 new_loc_descr (DW_OP_constu,
20049 tree_to_shwi (DECL_VINDEX (func_decl)),
20050 0));
20052 /* GNU extension: Record what type this method came from originally. */
20053 if (debug_info_level > DINFO_LEVEL_TERSE
20054 && DECL_CONTEXT (func_decl))
20055 add_AT_die_ref (die, DW_AT_containing_type,
20056 lookup_type_die (DECL_CONTEXT (func_decl)));
20060 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20061 given decl. This used to be a vendor extension until after DWARF 4
20062 standardized it. */
20064 static void
20065 add_linkage_attr (dw_die_ref die, tree decl)
20067 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20069 /* Mimic what assemble_name_raw does with a leading '*'. */
20070 if (name[0] == '*')
20071 name = &name[1];
20073 if (dwarf_version >= 4)
20074 add_AT_string (die, DW_AT_linkage_name, name);
20075 else
20076 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20079 /* Add source coordinate attributes for the given decl. */
20081 static void
20082 add_src_coords_attributes (dw_die_ref die, tree decl)
20084 expanded_location s;
20086 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20087 return;
20088 s = expand_location (DECL_SOURCE_LOCATION (decl));
20089 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20090 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20093 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20095 static void
20096 add_linkage_name_raw (dw_die_ref die, tree decl)
20098 /* Defer until we have an assembler name set. */
20099 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20101 limbo_die_node *asm_name;
20103 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20104 asm_name->die = die;
20105 asm_name->created_for = decl;
20106 asm_name->next = deferred_asm_name;
20107 deferred_asm_name = asm_name;
20109 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20110 add_linkage_attr (die, decl);
20113 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20115 static void
20116 add_linkage_name (dw_die_ref die, tree decl)
20118 if (debug_info_level > DINFO_LEVEL_NONE
20119 && VAR_OR_FUNCTION_DECL_P (decl)
20120 && TREE_PUBLIC (decl)
20121 && !(VAR_P (decl) && DECL_REGISTER (decl))
20122 && die->die_tag != DW_TAG_member)
20123 add_linkage_name_raw (die, decl);
20126 /* Add a DW_AT_name attribute and source coordinate attribute for the
20127 given decl, but only if it actually has a name. */
20129 static void
20130 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20131 bool no_linkage_name)
20133 tree decl_name;
20135 decl_name = DECL_NAME (decl);
20136 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20138 const char *name = dwarf2_name (decl, 0);
20139 if (name)
20140 add_name_attribute (die, name);
20141 if (! DECL_ARTIFICIAL (decl))
20142 add_src_coords_attributes (die, decl);
20144 if (!no_linkage_name)
20145 add_linkage_name (die, decl);
20148 #ifdef VMS_DEBUGGING_INFO
20149 /* Get the function's name, as described by its RTL. This may be different
20150 from the DECL_NAME name used in the source file. */
20151 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20153 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20154 XEXP (DECL_RTL (decl), 0), false);
20155 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20157 #endif /* VMS_DEBUGGING_INFO */
20160 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20162 static void
20163 add_discr_value (dw_die_ref die, dw_discr_value *value)
20165 dw_attr_node attr;
20167 attr.dw_attr = DW_AT_discr_value;
20168 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20169 attr.dw_attr_val.val_entry = NULL;
20170 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20171 if (value->pos)
20172 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20173 else
20174 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20175 add_dwarf_attr (die, &attr);
20178 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20180 static void
20181 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20183 dw_attr_node attr;
20185 attr.dw_attr = DW_AT_discr_list;
20186 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20187 attr.dw_attr_val.val_entry = NULL;
20188 attr.dw_attr_val.v.val_discr_list = discr_list;
20189 add_dwarf_attr (die, &attr);
20192 static inline dw_discr_list_ref
20193 AT_discr_list (dw_attr_node *attr)
20195 return attr->dw_attr_val.v.val_discr_list;
20198 #ifdef VMS_DEBUGGING_INFO
20199 /* Output the debug main pointer die for VMS */
20201 void
20202 dwarf2out_vms_debug_main_pointer (void)
20204 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20205 dw_die_ref die;
20207 /* Allocate the VMS debug main subprogram die. */
20208 die = ggc_cleared_alloc<die_node> ();
20209 die->die_tag = DW_TAG_subprogram;
20210 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20211 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20212 current_function_funcdef_no);
20213 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20215 /* Make it the first child of comp_unit_die (). */
20216 die->die_parent = comp_unit_die ();
20217 if (comp_unit_die ()->die_child)
20219 die->die_sib = comp_unit_die ()->die_child->die_sib;
20220 comp_unit_die ()->die_child->die_sib = die;
20222 else
20224 die->die_sib = die;
20225 comp_unit_die ()->die_child = die;
20228 #endif /* VMS_DEBUGGING_INFO */
20230 /* Push a new declaration scope. */
20232 static void
20233 push_decl_scope (tree scope)
20235 vec_safe_push (decl_scope_table, scope);
20238 /* Pop a declaration scope. */
20240 static inline void
20241 pop_decl_scope (void)
20243 decl_scope_table->pop ();
20246 /* walk_tree helper function for uses_local_type, below. */
20248 static tree
20249 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20251 if (!TYPE_P (*tp))
20252 *walk_subtrees = 0;
20253 else
20255 tree name = TYPE_NAME (*tp);
20256 if (name && DECL_P (name) && decl_function_context (name))
20257 return *tp;
20259 return NULL_TREE;
20262 /* If TYPE involves a function-local type (including a local typedef to a
20263 non-local type), returns that type; otherwise returns NULL_TREE. */
20265 static tree
20266 uses_local_type (tree type)
20268 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20269 return used;
20272 /* Return the DIE for the scope that immediately contains this type.
20273 Non-named types that do not involve a function-local type get global
20274 scope. Named types nested in namespaces or other types get their
20275 containing scope. All other types (i.e. function-local named types) get
20276 the current active scope. */
20278 static dw_die_ref
20279 scope_die_for (tree t, dw_die_ref context_die)
20281 dw_die_ref scope_die = NULL;
20282 tree containing_scope;
20284 /* Non-types always go in the current scope. */
20285 gcc_assert (TYPE_P (t));
20287 /* Use the scope of the typedef, rather than the scope of the type
20288 it refers to. */
20289 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20290 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20291 else
20292 containing_scope = TYPE_CONTEXT (t);
20294 /* Use the containing namespace if there is one. */
20295 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20297 if (context_die == lookup_decl_die (containing_scope))
20298 /* OK */;
20299 else if (debug_info_level > DINFO_LEVEL_TERSE)
20300 context_die = get_context_die (containing_scope);
20301 else
20302 containing_scope = NULL_TREE;
20305 /* Ignore function type "scopes" from the C frontend. They mean that
20306 a tagged type is local to a parmlist of a function declarator, but
20307 that isn't useful to DWARF. */
20308 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20309 containing_scope = NULL_TREE;
20311 if (SCOPE_FILE_SCOPE_P (containing_scope))
20313 /* If T uses a local type keep it local as well, to avoid references
20314 to function-local DIEs from outside the function. */
20315 if (current_function_decl && uses_local_type (t))
20316 scope_die = context_die;
20317 else
20318 scope_die = comp_unit_die ();
20320 else if (TYPE_P (containing_scope))
20322 /* For types, we can just look up the appropriate DIE. */
20323 if (debug_info_level > DINFO_LEVEL_TERSE)
20324 scope_die = get_context_die (containing_scope);
20325 else
20327 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20328 if (scope_die == NULL)
20329 scope_die = comp_unit_die ();
20332 else
20333 scope_die = context_die;
20335 return scope_die;
20338 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20340 static inline int
20341 local_scope_p (dw_die_ref context_die)
20343 for (; context_die; context_die = context_die->die_parent)
20344 if (context_die->die_tag == DW_TAG_inlined_subroutine
20345 || context_die->die_tag == DW_TAG_subprogram)
20346 return 1;
20348 return 0;
20351 /* Returns nonzero if CONTEXT_DIE is a class. */
20353 static inline int
20354 class_scope_p (dw_die_ref context_die)
20356 return (context_die
20357 && (context_die->die_tag == DW_TAG_structure_type
20358 || context_die->die_tag == DW_TAG_class_type
20359 || context_die->die_tag == DW_TAG_interface_type
20360 || context_die->die_tag == DW_TAG_union_type));
20363 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20364 whether or not to treat a DIE in this context as a declaration. */
20366 static inline int
20367 class_or_namespace_scope_p (dw_die_ref context_die)
20369 return (class_scope_p (context_die)
20370 || (context_die && context_die->die_tag == DW_TAG_namespace));
20373 /* Many forms of DIEs require a "type description" attribute. This
20374 routine locates the proper "type descriptor" die for the type given
20375 by 'type' plus any additional qualifiers given by 'cv_quals', and
20376 adds a DW_AT_type attribute below the given die. */
20378 static void
20379 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20380 bool reverse, dw_die_ref context_die)
20382 enum tree_code code = TREE_CODE (type);
20383 dw_die_ref type_die = NULL;
20385 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20386 or fixed-point type, use the inner type. This is because we have no
20387 support for unnamed types in base_type_die. This can happen if this is
20388 an Ada subrange type. Correct solution is emit a subrange type die. */
20389 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20390 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20391 type = TREE_TYPE (type), code = TREE_CODE (type);
20393 if (code == ERROR_MARK
20394 /* Handle a special case. For functions whose return type is void, we
20395 generate *no* type attribute. (Note that no object may have type
20396 `void', so this only applies to function return types). */
20397 || code == VOID_TYPE)
20398 return;
20400 type_die = modified_type_die (type,
20401 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20402 reverse,
20403 context_die);
20405 if (type_die != NULL)
20406 add_AT_die_ref (object_die, DW_AT_type, type_die);
20409 /* Given an object die, add the calling convention attribute for the
20410 function call type. */
20411 static void
20412 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20414 enum dwarf_calling_convention value = DW_CC_normal;
20416 value = ((enum dwarf_calling_convention)
20417 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20419 if (is_fortran ()
20420 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20422 /* DWARF 2 doesn't provide a way to identify a program's source-level
20423 entry point. DW_AT_calling_convention attributes are only meant
20424 to describe functions' calling conventions. However, lacking a
20425 better way to signal the Fortran main program, we used this for
20426 a long time, following existing custom. Now, DWARF 4 has
20427 DW_AT_main_subprogram, which we add below, but some tools still
20428 rely on the old way, which we thus keep. */
20429 value = DW_CC_program;
20431 if (dwarf_version >= 4 || !dwarf_strict)
20432 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20435 /* Only add the attribute if the backend requests it, and
20436 is not DW_CC_normal. */
20437 if (value && (value != DW_CC_normal))
20438 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20441 /* Given a tree pointer to a struct, class, union, or enum type node, return
20442 a pointer to the (string) tag name for the given type, or zero if the type
20443 was declared without a tag. */
20445 static const char *
20446 type_tag (const_tree type)
20448 const char *name = 0;
20450 if (TYPE_NAME (type) != 0)
20452 tree t = 0;
20454 /* Find the IDENTIFIER_NODE for the type name. */
20455 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20456 && !TYPE_NAMELESS (type))
20457 t = TYPE_NAME (type);
20459 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20460 a TYPE_DECL node, regardless of whether or not a `typedef' was
20461 involved. */
20462 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20463 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20465 /* We want to be extra verbose. Don't call dwarf_name if
20466 DECL_NAME isn't set. The default hook for decl_printable_name
20467 doesn't like that, and in this context it's correct to return
20468 0, instead of "<anonymous>" or the like. */
20469 if (DECL_NAME (TYPE_NAME (type))
20470 && !DECL_NAMELESS (TYPE_NAME (type)))
20471 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20474 /* Now get the name as a string, or invent one. */
20475 if (!name && t != 0)
20476 name = IDENTIFIER_POINTER (t);
20479 return (name == 0 || *name == '\0') ? 0 : name;
20482 /* Return the type associated with a data member, make a special check
20483 for bit field types. */
20485 static inline tree
20486 member_declared_type (const_tree member)
20488 return (DECL_BIT_FIELD_TYPE (member)
20489 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20492 /* Get the decl's label, as described by its RTL. This may be different
20493 from the DECL_NAME name used in the source file. */
20495 #if 0
20496 static const char *
20497 decl_start_label (tree decl)
20499 rtx x;
20500 const char *fnname;
20502 x = DECL_RTL (decl);
20503 gcc_assert (MEM_P (x));
20505 x = XEXP (x, 0);
20506 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20508 fnname = XSTR (x, 0);
20509 return fnname;
20511 #endif
20513 /* For variable-length arrays that have been previously generated, but
20514 may be incomplete due to missing subscript info, fill the subscript
20515 info. Return TRUE if this is one of those cases. */
20516 static bool
20517 fill_variable_array_bounds (tree type)
20519 if (TREE_ASM_WRITTEN (type)
20520 && TREE_CODE (type) == ARRAY_TYPE
20521 && variably_modified_type_p (type, NULL))
20523 dw_die_ref array_die = lookup_type_die (type);
20524 if (!array_die)
20525 return false;
20526 add_subscript_info (array_die, type, !is_ada ());
20527 return true;
20529 return false;
20532 /* These routines generate the internal representation of the DIE's for
20533 the compilation unit. Debugging information is collected by walking
20534 the declaration trees passed in from dwarf2out_decl(). */
20536 static void
20537 gen_array_type_die (tree type, dw_die_ref context_die)
20539 dw_die_ref array_die;
20541 /* GNU compilers represent multidimensional array types as sequences of one
20542 dimensional array types whose element types are themselves array types.
20543 We sometimes squish that down to a single array_type DIE with multiple
20544 subscripts in the Dwarf debugging info. The draft Dwarf specification
20545 say that we are allowed to do this kind of compression in C, because
20546 there is no difference between an array of arrays and a multidimensional
20547 array. We don't do this for Ada to remain as close as possible to the
20548 actual representation, which is especially important against the language
20549 flexibilty wrt arrays of variable size. */
20551 bool collapse_nested_arrays = !is_ada ();
20553 if (fill_variable_array_bounds (type))
20554 return;
20556 dw_die_ref scope_die = scope_die_for (type, context_die);
20557 tree element_type;
20559 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20560 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20561 if (TYPE_STRING_FLAG (type)
20562 && TREE_CODE (type) == ARRAY_TYPE
20563 && is_fortran ()
20564 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20566 HOST_WIDE_INT size;
20568 array_die = new_die (DW_TAG_string_type, scope_die, type);
20569 add_name_attribute (array_die, type_tag (type));
20570 equate_type_number_to_die (type, array_die);
20571 size = int_size_in_bytes (type);
20572 if (size >= 0)
20573 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20574 else if (TYPE_DOMAIN (type) != NULL_TREE
20575 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20577 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20578 tree rszdecl = szdecl;
20579 HOST_WIDE_INT rsize = 0;
20581 size = int_size_in_bytes (TREE_TYPE (szdecl));
20582 if (!DECL_P (szdecl))
20584 if (TREE_CODE (szdecl) == INDIRECT_REF
20585 && DECL_P (TREE_OPERAND (szdecl, 0)))
20587 rszdecl = TREE_OPERAND (szdecl, 0);
20588 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
20589 if (rsize <= 0)
20590 size = 0;
20592 else
20593 size = 0;
20595 if (size > 0)
20597 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
20598 if (loc == NULL
20599 && early_dwarf
20600 && current_function_decl
20601 && DECL_CONTEXT (rszdecl) == current_function_decl)
20603 dw_die_ref ref = lookup_decl_die (rszdecl);
20604 dw_loc_descr_ref l = NULL;
20605 if (ref)
20607 l = new_loc_descr (DW_OP_call4, 0, 0);
20608 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20609 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
20610 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
20612 else if (TREE_CODE (rszdecl) == PARM_DECL
20613 && string_types)
20615 l = new_loc_descr (DW_OP_call4, 0, 0);
20616 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
20617 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
20618 string_types->safe_push (array_die);
20620 if (l && rszdecl != szdecl)
20622 if (rsize == DWARF2_ADDR_SIZE)
20623 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
20624 0, 0));
20625 else
20626 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
20627 rsize, 0));
20629 if (l)
20630 loc = new_loc_list (l, NULL, NULL, NULL);
20632 if (loc)
20634 add_AT_location_description (array_die, DW_AT_string_length,
20635 loc);
20636 if (size != DWARF2_ADDR_SIZE)
20637 add_AT_unsigned (array_die, dwarf_version >= 5
20638 ? DW_AT_string_length_byte_size
20639 : DW_AT_byte_size, size);
20643 return;
20646 array_die = new_die (DW_TAG_array_type, scope_die, type);
20647 add_name_attribute (array_die, type_tag (type));
20648 equate_type_number_to_die (type, array_die);
20650 if (TREE_CODE (type) == VECTOR_TYPE)
20651 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20653 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20654 if (is_fortran ()
20655 && TREE_CODE (type) == ARRAY_TYPE
20656 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20657 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20658 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20660 #if 0
20661 /* We default the array ordering. SDB will probably do
20662 the right things even if DW_AT_ordering is not present. It's not even
20663 an issue until we start to get into multidimensional arrays anyway. If
20664 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20665 then we'll have to put the DW_AT_ordering attribute back in. (But if
20666 and when we find out that we need to put these in, we will only do so
20667 for multidimensional arrays. */
20668 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20669 #endif
20671 if (TREE_CODE (type) == VECTOR_TYPE)
20673 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20674 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20675 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20676 add_bound_info (subrange_die, DW_AT_upper_bound,
20677 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20679 else
20680 add_subscript_info (array_die, type, collapse_nested_arrays);
20682 /* Add representation of the type of the elements of this array type and
20683 emit the corresponding DIE if we haven't done it already. */
20684 element_type = TREE_TYPE (type);
20685 if (collapse_nested_arrays)
20686 while (TREE_CODE (element_type) == ARRAY_TYPE)
20688 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20689 break;
20690 element_type = TREE_TYPE (element_type);
20693 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20694 TREE_CODE (type) == ARRAY_TYPE
20695 && TYPE_REVERSE_STORAGE_ORDER (type),
20696 context_die);
20698 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20699 if (TYPE_ARTIFICIAL (type))
20700 add_AT_flag (array_die, DW_AT_artificial, 1);
20702 if (get_AT (array_die, DW_AT_name))
20703 add_pubtype (type, array_die);
20706 /* After all arguments are created, adjust any DW_TAG_string_type
20707 DIEs DW_AT_string_length attributes. */
20709 static void
20710 adjust_string_types (void)
20712 dw_die_ref array_die;
20713 unsigned int i;
20714 FOR_EACH_VEC_ELT (*string_types, i, array_die)
20716 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
20717 if (a == NULL)
20718 continue;
20719 dw_loc_descr_ref loc = AT_loc (a);
20720 gcc_assert (loc->dw_loc_opc == DW_OP_call4
20721 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
20722 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
20723 if (ref)
20725 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20726 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
20727 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
20729 else
20731 remove_AT (array_die, DW_AT_string_length);
20732 remove_AT (array_die, dwarf_version >= 5
20733 ? DW_AT_string_length_byte_size
20734 : DW_AT_byte_size);
20739 /* This routine generates DIE for array with hidden descriptor, details
20740 are filled into *info by a langhook. */
20742 static void
20743 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20744 dw_die_ref context_die)
20746 const dw_die_ref scope_die = scope_die_for (type, context_die);
20747 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20748 struct loc_descr_context context = { type, info->base_decl, NULL,
20749 false, false };
20750 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20751 int dim;
20753 add_name_attribute (array_die, type_tag (type));
20754 equate_type_number_to_die (type, array_die);
20756 if (info->ndimensions > 1)
20757 switch (info->ordering)
20759 case array_descr_ordering_row_major:
20760 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20761 break;
20762 case array_descr_ordering_column_major:
20763 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20764 break;
20765 default:
20766 break;
20769 if (dwarf_version >= 3 || !dwarf_strict)
20771 if (info->data_location)
20772 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20773 dw_scalar_form_exprloc, &context);
20774 if (info->associated)
20775 add_scalar_info (array_die, DW_AT_associated, info->associated,
20776 dw_scalar_form_constant
20777 | dw_scalar_form_exprloc
20778 | dw_scalar_form_reference, &context);
20779 if (info->allocated)
20780 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20781 dw_scalar_form_constant
20782 | dw_scalar_form_exprloc
20783 | dw_scalar_form_reference, &context);
20784 if (info->stride)
20786 const enum dwarf_attribute attr
20787 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20788 const int forms
20789 = (info->stride_in_bits)
20790 ? dw_scalar_form_constant
20791 : (dw_scalar_form_constant
20792 | dw_scalar_form_exprloc
20793 | dw_scalar_form_reference);
20795 add_scalar_info (array_die, attr, info->stride, forms, &context);
20798 if (dwarf_version >= 5)
20800 if (info->rank)
20802 add_scalar_info (array_die, DW_AT_rank, info->rank,
20803 dw_scalar_form_constant
20804 | dw_scalar_form_exprloc, &context);
20805 subrange_tag = DW_TAG_generic_subrange;
20806 context.placeholder_arg = true;
20810 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20812 for (dim = 0; dim < info->ndimensions; dim++)
20814 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20816 if (info->dimen[dim].bounds_type)
20817 add_type_attribute (subrange_die,
20818 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20819 false, context_die);
20820 if (info->dimen[dim].lower_bound)
20821 add_bound_info (subrange_die, DW_AT_lower_bound,
20822 info->dimen[dim].lower_bound, &context);
20823 if (info->dimen[dim].upper_bound)
20824 add_bound_info (subrange_die, DW_AT_upper_bound,
20825 info->dimen[dim].upper_bound, &context);
20826 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20827 add_scalar_info (subrange_die, DW_AT_byte_stride,
20828 info->dimen[dim].stride,
20829 dw_scalar_form_constant
20830 | dw_scalar_form_exprloc
20831 | dw_scalar_form_reference,
20832 &context);
20835 gen_type_die (info->element_type, context_die);
20836 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20837 TREE_CODE (type) == ARRAY_TYPE
20838 && TYPE_REVERSE_STORAGE_ORDER (type),
20839 context_die);
20841 if (get_AT (array_die, DW_AT_name))
20842 add_pubtype (type, array_die);
20845 #if 0
20846 static void
20847 gen_entry_point_die (tree decl, dw_die_ref context_die)
20849 tree origin = decl_ultimate_origin (decl);
20850 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20852 if (origin != NULL)
20853 add_abstract_origin_attribute (decl_die, origin);
20854 else
20856 add_name_and_src_coords_attributes (decl_die, decl);
20857 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20858 TYPE_UNQUALIFIED, false, context_die);
20861 if (DECL_ABSTRACT_P (decl))
20862 equate_decl_number_to_die (decl, decl_die);
20863 else
20864 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
20866 #endif
20868 /* Walk through the list of incomplete types again, trying once more to
20869 emit full debugging info for them. */
20871 static void
20872 retry_incomplete_types (void)
20874 set_early_dwarf s;
20875 int i;
20877 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
20878 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
20879 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
20880 vec_safe_truncate (incomplete_types, 0);
20883 /* Determine what tag to use for a record type. */
20885 static enum dwarf_tag
20886 record_type_tag (tree type)
20888 if (! lang_hooks.types.classify_record)
20889 return DW_TAG_structure_type;
20891 switch (lang_hooks.types.classify_record (type))
20893 case RECORD_IS_STRUCT:
20894 return DW_TAG_structure_type;
20896 case RECORD_IS_CLASS:
20897 return DW_TAG_class_type;
20899 case RECORD_IS_INTERFACE:
20900 if (dwarf_version >= 3 || !dwarf_strict)
20901 return DW_TAG_interface_type;
20902 return DW_TAG_structure_type;
20904 default:
20905 gcc_unreachable ();
20909 /* Generate a DIE to represent an enumeration type. Note that these DIEs
20910 include all of the information about the enumeration values also. Each
20911 enumerated type name/value is listed as a child of the enumerated type
20912 DIE. */
20914 static dw_die_ref
20915 gen_enumeration_type_die (tree type, dw_die_ref context_die)
20917 dw_die_ref type_die = lookup_type_die (type);
20919 if (type_die == NULL)
20921 type_die = new_die (DW_TAG_enumeration_type,
20922 scope_die_for (type, context_die), type);
20923 equate_type_number_to_die (type, type_die);
20924 add_name_attribute (type_die, type_tag (type));
20925 if (dwarf_version >= 4 || !dwarf_strict)
20927 if (ENUM_IS_SCOPED (type))
20928 add_AT_flag (type_die, DW_AT_enum_class, 1);
20929 if (ENUM_IS_OPAQUE (type))
20930 add_AT_flag (type_die, DW_AT_declaration, 1);
20933 else if (! TYPE_SIZE (type))
20934 return type_die;
20935 else
20936 remove_AT (type_die, DW_AT_declaration);
20938 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
20939 given enum type is incomplete, do not generate the DW_AT_byte_size
20940 attribute or the DW_AT_element_list attribute. */
20941 if (TYPE_SIZE (type))
20943 tree link;
20945 TREE_ASM_WRITTEN (type) = 1;
20946 add_byte_size_attribute (type_die, type);
20947 if (dwarf_version >= 3 || !dwarf_strict)
20949 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
20950 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
20951 context_die);
20953 if (TYPE_STUB_DECL (type) != NULL_TREE)
20955 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20956 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20959 /* If the first reference to this type was as the return type of an
20960 inline function, then it may not have a parent. Fix this now. */
20961 if (type_die->die_parent == NULL)
20962 add_child_die (scope_die_for (type, context_die), type_die);
20964 for (link = TYPE_VALUES (type);
20965 link != NULL; link = TREE_CHAIN (link))
20967 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
20968 tree value = TREE_VALUE (link);
20970 add_name_attribute (enum_die,
20971 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
20973 if (TREE_CODE (value) == CONST_DECL)
20974 value = DECL_INITIAL (value);
20976 if (simple_type_size_in_bits (TREE_TYPE (value))
20977 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
20979 /* For constant forms created by add_AT_unsigned DWARF
20980 consumers (GDB, elfutils, etc.) always zero extend
20981 the value. Only when the actual value is negative
20982 do we need to use add_AT_int to generate a constant
20983 form that can represent negative values. */
20984 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
20985 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
20986 add_AT_unsigned (enum_die, DW_AT_const_value,
20987 (unsigned HOST_WIDE_INT) val);
20988 else
20989 add_AT_int (enum_die, DW_AT_const_value, val);
20991 else
20992 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
20993 that here. TODO: This should be re-worked to use correct
20994 signed/unsigned double tags for all cases. */
20995 add_AT_wide (enum_die, DW_AT_const_value, value);
20998 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20999 if (TYPE_ARTIFICIAL (type))
21000 add_AT_flag (type_die, DW_AT_artificial, 1);
21002 else
21003 add_AT_flag (type_die, DW_AT_declaration, 1);
21005 add_pubtype (type, type_die);
21007 return type_die;
21010 /* Generate a DIE to represent either a real live formal parameter decl or to
21011 represent just the type of some formal parameter position in some function
21012 type.
21014 Note that this routine is a bit unusual because its argument may be a
21015 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21016 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21017 node. If it's the former then this function is being called to output a
21018 DIE to represent a formal parameter object (or some inlining thereof). If
21019 it's the latter, then this function is only being called to output a
21020 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21021 argument type of some subprogram type.
21022 If EMIT_NAME_P is true, name and source coordinate attributes
21023 are emitted. */
21025 static dw_die_ref
21026 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21027 dw_die_ref context_die)
21029 tree node_or_origin = node ? node : origin;
21030 tree ultimate_origin;
21031 dw_die_ref parm_die = NULL;
21033 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21035 parm_die = lookup_decl_die (node);
21037 /* If the contexts differ, we may not be talking about the same
21038 thing. */
21039 if (parm_die && parm_die->die_parent != context_die)
21041 if (!DECL_ABSTRACT_P (node))
21043 /* This can happen when creating an inlined instance, in
21044 which case we need to create a new DIE that will get
21045 annotated with DW_AT_abstract_origin. */
21046 parm_die = NULL;
21048 else
21050 /* FIXME: Reuse DIE even with a differing context.
21052 This can happen when calling
21053 dwarf2out_abstract_function to build debug info for
21054 the abstract instance of a function for which we have
21055 already generated a DIE in
21056 dwarf2out_early_global_decl.
21058 Once we remove dwarf2out_abstract_function, we should
21059 have a call to gcc_unreachable here. */
21063 if (parm_die && parm_die->die_parent == NULL)
21065 /* Check that parm_die already has the right attributes that
21066 we would have added below. If any attributes are
21067 missing, fall through to add them. */
21068 if (! DECL_ABSTRACT_P (node_or_origin)
21069 && !get_AT (parm_die, DW_AT_location)
21070 && !get_AT (parm_die, DW_AT_const_value))
21071 /* We are missing location info, and are about to add it. */
21073 else
21075 add_child_die (context_die, parm_die);
21076 return parm_die;
21081 /* If we have a previously generated DIE, use it, unless this is an
21082 concrete instance (origin != NULL), in which case we need a new
21083 DIE with a corresponding DW_AT_abstract_origin. */
21084 bool reusing_die;
21085 if (parm_die && origin == NULL)
21086 reusing_die = true;
21087 else
21089 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21090 reusing_die = false;
21093 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21095 case tcc_declaration:
21096 ultimate_origin = decl_ultimate_origin (node_or_origin);
21097 if (node || ultimate_origin)
21098 origin = ultimate_origin;
21100 if (reusing_die)
21101 goto add_location;
21103 if (origin != NULL)
21104 add_abstract_origin_attribute (parm_die, origin);
21105 else if (emit_name_p)
21106 add_name_and_src_coords_attributes (parm_die, node);
21107 if (origin == NULL
21108 || (! DECL_ABSTRACT_P (node_or_origin)
21109 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21110 decl_function_context
21111 (node_or_origin))))
21113 tree type = TREE_TYPE (node_or_origin);
21114 if (decl_by_reference_p (node_or_origin))
21115 add_type_attribute (parm_die, TREE_TYPE (type),
21116 TYPE_UNQUALIFIED,
21117 false, context_die);
21118 else
21119 add_type_attribute (parm_die, type,
21120 decl_quals (node_or_origin),
21121 false, context_die);
21123 if (origin == NULL && DECL_ARTIFICIAL (node))
21124 add_AT_flag (parm_die, DW_AT_artificial, 1);
21125 add_location:
21126 if (node && node != origin)
21127 equate_decl_number_to_die (node, parm_die);
21128 if (! DECL_ABSTRACT_P (node_or_origin))
21129 add_location_or_const_value_attribute (parm_die, node_or_origin,
21130 node == NULL);
21132 break;
21134 case tcc_type:
21135 /* We were called with some kind of a ..._TYPE node. */
21136 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21137 context_die);
21138 break;
21140 default:
21141 gcc_unreachable ();
21144 return parm_die;
21147 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21148 children DW_TAG_formal_parameter DIEs representing the arguments of the
21149 parameter pack.
21151 PARM_PACK must be a function parameter pack.
21152 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21153 must point to the subsequent arguments of the function PACK_ARG belongs to.
21154 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21155 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21156 following the last one for which a DIE was generated. */
21158 static dw_die_ref
21159 gen_formal_parameter_pack_die (tree parm_pack,
21160 tree pack_arg,
21161 dw_die_ref subr_die,
21162 tree *next_arg)
21164 tree arg;
21165 dw_die_ref parm_pack_die;
21167 gcc_assert (parm_pack
21168 && lang_hooks.function_parameter_pack_p (parm_pack)
21169 && subr_die);
21171 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21172 add_src_coords_attributes (parm_pack_die, parm_pack);
21174 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21176 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21177 parm_pack))
21178 break;
21179 gen_formal_parameter_die (arg, NULL,
21180 false /* Don't emit name attribute. */,
21181 parm_pack_die);
21183 if (next_arg)
21184 *next_arg = arg;
21185 return parm_pack_die;
21188 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21189 at the end of an (ANSI prototyped) formal parameters list. */
21191 static void
21192 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21194 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21197 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21198 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21199 parameters as specified in some function type specification (except for
21200 those which appear as part of a function *definition*). */
21202 static void
21203 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21205 tree link;
21206 tree formal_type = NULL;
21207 tree first_parm_type;
21208 tree arg;
21210 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21212 arg = DECL_ARGUMENTS (function_or_method_type);
21213 function_or_method_type = TREE_TYPE (function_or_method_type);
21215 else
21216 arg = NULL_TREE;
21218 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21220 /* Make our first pass over the list of formal parameter types and output a
21221 DW_TAG_formal_parameter DIE for each one. */
21222 for (link = first_parm_type; link; )
21224 dw_die_ref parm_die;
21226 formal_type = TREE_VALUE (link);
21227 if (formal_type == void_type_node)
21228 break;
21230 /* Output a (nameless) DIE to represent the formal parameter itself. */
21231 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21233 parm_die = gen_formal_parameter_die (formal_type, NULL,
21234 true /* Emit name attribute. */,
21235 context_die);
21236 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21237 && link == first_parm_type)
21239 add_AT_flag (parm_die, DW_AT_artificial, 1);
21240 if (dwarf_version >= 3 || !dwarf_strict)
21241 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21243 else if (arg && DECL_ARTIFICIAL (arg))
21244 add_AT_flag (parm_die, DW_AT_artificial, 1);
21247 link = TREE_CHAIN (link);
21248 if (arg)
21249 arg = DECL_CHAIN (arg);
21252 /* If this function type has an ellipsis, add a
21253 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21254 if (formal_type != void_type_node)
21255 gen_unspecified_parameters_die (function_or_method_type, context_die);
21257 /* Make our second (and final) pass over the list of formal parameter types
21258 and output DIEs to represent those types (as necessary). */
21259 for (link = TYPE_ARG_TYPES (function_or_method_type);
21260 link && TREE_VALUE (link);
21261 link = TREE_CHAIN (link))
21262 gen_type_die (TREE_VALUE (link), context_die);
21265 /* We want to generate the DIE for TYPE so that we can generate the
21266 die for MEMBER, which has been defined; we will need to refer back
21267 to the member declaration nested within TYPE. If we're trying to
21268 generate minimal debug info for TYPE, processing TYPE won't do the
21269 trick; we need to attach the member declaration by hand. */
21271 static void
21272 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21274 gen_type_die (type, context_die);
21276 /* If we're trying to avoid duplicate debug info, we may not have
21277 emitted the member decl for this function. Emit it now. */
21278 if (TYPE_STUB_DECL (type)
21279 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21280 && ! lookup_decl_die (member))
21282 dw_die_ref type_die;
21283 gcc_assert (!decl_ultimate_origin (member));
21285 push_decl_scope (type);
21286 type_die = lookup_type_die_strip_naming_typedef (type);
21287 if (TREE_CODE (member) == FUNCTION_DECL)
21288 gen_subprogram_die (member, type_die);
21289 else if (TREE_CODE (member) == FIELD_DECL)
21291 /* Ignore the nameless fields that are used to skip bits but handle
21292 C++ anonymous unions and structs. */
21293 if (DECL_NAME (member) != NULL_TREE
21294 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21295 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21297 struct vlr_context vlr_ctx = {
21298 DECL_CONTEXT (member), /* struct_type */
21299 NULL_TREE /* variant_part_offset */
21301 gen_type_die (member_declared_type (member), type_die);
21302 gen_field_die (member, &vlr_ctx, type_die);
21305 else
21306 gen_variable_die (member, NULL_TREE, type_die);
21308 pop_decl_scope ();
21312 /* Forward declare these functions, because they are mutually recursive
21313 with their set_block_* pairing functions. */
21314 static void set_decl_origin_self (tree);
21315 static void set_decl_abstract_flags (tree, vec<tree> &);
21317 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21318 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21319 that it points to the node itself, thus indicating that the node is its
21320 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21321 the given node is NULL, recursively descend the decl/block tree which
21322 it is the root of, and for each other ..._DECL or BLOCK node contained
21323 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21324 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21325 values to point to themselves. */
21327 static void
21328 set_block_origin_self (tree stmt)
21330 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21332 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21335 tree local_decl;
21337 for (local_decl = BLOCK_VARS (stmt);
21338 local_decl != NULL_TREE;
21339 local_decl = DECL_CHAIN (local_decl))
21340 /* Do not recurse on nested functions since the inlining status
21341 of parent and child can be different as per the DWARF spec. */
21342 if (TREE_CODE (local_decl) != FUNCTION_DECL
21343 && !DECL_EXTERNAL (local_decl))
21344 set_decl_origin_self (local_decl);
21348 tree subblock;
21350 for (subblock = BLOCK_SUBBLOCKS (stmt);
21351 subblock != NULL_TREE;
21352 subblock = BLOCK_CHAIN (subblock))
21353 set_block_origin_self (subblock); /* Recurse. */
21358 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21359 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21360 node to so that it points to the node itself, thus indicating that the
21361 node represents its own (abstract) origin. Additionally, if the
21362 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21363 the decl/block tree of which the given node is the root of, and for
21364 each other ..._DECL or BLOCK node contained therein whose
21365 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21366 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21367 point to themselves. */
21369 static void
21370 set_decl_origin_self (tree decl)
21372 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21374 DECL_ABSTRACT_ORIGIN (decl) = decl;
21375 if (TREE_CODE (decl) == FUNCTION_DECL)
21377 tree arg;
21379 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21380 DECL_ABSTRACT_ORIGIN (arg) = arg;
21381 if (DECL_INITIAL (decl) != NULL_TREE
21382 && DECL_INITIAL (decl) != error_mark_node)
21383 set_block_origin_self (DECL_INITIAL (decl));
21388 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21389 and if it wasn't 1 before, push it to abstract_vec vector.
21390 For all local decls and all local sub-blocks (recursively) do it
21391 too. */
21393 static void
21394 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21396 tree local_decl;
21397 tree subblock;
21398 unsigned int i;
21400 if (!BLOCK_ABSTRACT (stmt))
21402 abstract_vec.safe_push (stmt);
21403 BLOCK_ABSTRACT (stmt) = 1;
21406 for (local_decl = BLOCK_VARS (stmt);
21407 local_decl != NULL_TREE;
21408 local_decl = DECL_CHAIN (local_decl))
21409 if (! DECL_EXTERNAL (local_decl))
21410 set_decl_abstract_flags (local_decl, abstract_vec);
21412 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21414 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21415 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21416 || TREE_CODE (local_decl) == PARM_DECL)
21417 set_decl_abstract_flags (local_decl, abstract_vec);
21420 for (subblock = BLOCK_SUBBLOCKS (stmt);
21421 subblock != NULL_TREE;
21422 subblock = BLOCK_CHAIN (subblock))
21423 set_block_abstract_flags (subblock, abstract_vec);
21426 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21427 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21428 In the case where the decl is a FUNCTION_DECL also set the abstract
21429 flags for all of the parameters, local vars, local
21430 blocks and sub-blocks (recursively). */
21432 static void
21433 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21435 if (!DECL_ABSTRACT_P (decl))
21437 abstract_vec.safe_push (decl);
21438 DECL_ABSTRACT_P (decl) = 1;
21441 if (TREE_CODE (decl) == FUNCTION_DECL)
21443 tree arg;
21445 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21446 if (!DECL_ABSTRACT_P (arg))
21448 abstract_vec.safe_push (arg);
21449 DECL_ABSTRACT_P (arg) = 1;
21451 if (DECL_INITIAL (decl) != NULL_TREE
21452 && DECL_INITIAL (decl) != error_mark_node)
21453 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21457 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21458 may later generate inlined and/or out-of-line instances of.
21460 FIXME: In the early-dwarf world, this function, and most of the
21461 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21462 the abstract instance. All we would need to do is annotate
21463 the early DIE with the appropriate DW_AT_inline in late
21464 dwarf (perhaps in gen_inlined_subroutine_die).
21466 However, we can't do this yet, because LTO streaming of DIEs
21467 has not been implemented yet. */
21469 static void
21470 dwarf2out_abstract_function (tree decl)
21472 dw_die_ref old_die;
21473 tree save_fn;
21474 tree context;
21475 hash_table<decl_loc_hasher> *old_decl_loc_table;
21476 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21477 int old_call_site_count, old_tail_call_site_count;
21478 struct call_arg_loc_node *old_call_arg_locations;
21480 /* Make sure we have the actual abstract inline, not a clone. */
21481 decl = DECL_ORIGIN (decl);
21483 old_die = lookup_decl_die (decl);
21484 if (old_die && get_AT (old_die, DW_AT_inline))
21485 /* We've already generated the abstract instance. */
21486 return;
21488 /* We can be called while recursively when seeing block defining inlined subroutine
21489 DIE. Be sure to not clobber the outer location table nor use it or we would
21490 get locations in abstract instantces. */
21491 old_decl_loc_table = decl_loc_table;
21492 decl_loc_table = NULL;
21493 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21494 cached_dw_loc_list_table = NULL;
21495 old_call_arg_locations = call_arg_locations;
21496 call_arg_locations = NULL;
21497 old_call_site_count = call_site_count;
21498 call_site_count = -1;
21499 old_tail_call_site_count = tail_call_site_count;
21500 tail_call_site_count = -1;
21502 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21503 we don't get confused by DECL_ABSTRACT_P. */
21504 if (debug_info_level > DINFO_LEVEL_TERSE)
21506 context = decl_class_context (decl);
21507 if (context)
21508 gen_type_die_for_member
21509 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21512 /* Pretend we've just finished compiling this function. */
21513 save_fn = current_function_decl;
21514 current_function_decl = decl;
21516 auto_vec<tree, 64> abstract_vec;
21517 set_decl_abstract_flags (decl, abstract_vec);
21518 dwarf2out_decl (decl);
21519 unsigned int i;
21520 tree t;
21521 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21522 if (TREE_CODE (t) == BLOCK)
21523 BLOCK_ABSTRACT (t) = 0;
21524 else
21525 DECL_ABSTRACT_P (t) = 0;
21527 current_function_decl = save_fn;
21528 decl_loc_table = old_decl_loc_table;
21529 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21530 call_arg_locations = old_call_arg_locations;
21531 call_site_count = old_call_site_count;
21532 tail_call_site_count = old_tail_call_site_count;
21535 /* Helper function of premark_used_types() which gets called through
21536 htab_traverse.
21538 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21539 marked as unused by prune_unused_types. */
21541 bool
21542 premark_used_types_helper (tree const &type, void *)
21544 dw_die_ref die;
21546 die = lookup_type_die (type);
21547 if (die != NULL)
21548 die->die_perennial_p = 1;
21549 return true;
21552 /* Helper function of premark_types_used_by_global_vars which gets called
21553 through htab_traverse.
21555 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21556 marked as unused by prune_unused_types. The DIE of the type is marked
21557 only if the global variable using the type will actually be emitted. */
21560 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21561 void *)
21563 struct types_used_by_vars_entry *entry;
21564 dw_die_ref die;
21566 entry = (struct types_used_by_vars_entry *) *slot;
21567 gcc_assert (entry->type != NULL
21568 && entry->var_decl != NULL);
21569 die = lookup_type_die (entry->type);
21570 if (die)
21572 /* Ask cgraph if the global variable really is to be emitted.
21573 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21574 varpool_node *node = varpool_node::get (entry->var_decl);
21575 if (node && node->definition)
21577 die->die_perennial_p = 1;
21578 /* Keep the parent DIEs as well. */
21579 while ((die = die->die_parent) && die->die_perennial_p == 0)
21580 die->die_perennial_p = 1;
21583 return 1;
21586 /* Mark all members of used_types_hash as perennial. */
21588 static void
21589 premark_used_types (struct function *fun)
21591 if (fun && fun->used_types_hash)
21592 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21595 /* Mark all members of types_used_by_vars_entry as perennial. */
21597 static void
21598 premark_types_used_by_global_vars (void)
21600 if (types_used_by_vars_hash)
21601 types_used_by_vars_hash
21602 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21605 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21606 for CA_LOC call arg loc node. */
21608 static dw_die_ref
21609 gen_call_site_die (tree decl, dw_die_ref subr_die,
21610 struct call_arg_loc_node *ca_loc)
21612 dw_die_ref stmt_die = NULL, die;
21613 tree block = ca_loc->block;
21615 while (block
21616 && block != DECL_INITIAL (decl)
21617 && TREE_CODE (block) == BLOCK)
21619 stmt_die = BLOCK_DIE (block);
21620 if (stmt_die)
21621 break;
21622 block = BLOCK_SUPERCONTEXT (block);
21624 if (stmt_die == NULL)
21625 stmt_die = subr_die;
21626 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21627 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21628 if (ca_loc->tail_call_p)
21629 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21630 if (ca_loc->symbol_ref)
21632 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21633 if (tdie)
21634 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21635 else
21636 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21637 false);
21639 return die;
21642 /* Generate a DIE to represent a declared function (either file-scope or
21643 block-local). */
21645 static void
21646 gen_subprogram_die (tree decl, dw_die_ref context_die)
21648 tree origin = decl_ultimate_origin (decl);
21649 dw_die_ref subr_die;
21650 dw_die_ref old_die = lookup_decl_die (decl);
21652 /* This function gets called multiple times for different stages of
21653 the debug process. For example, for func() in this code:
21655 namespace S
21657 void func() { ... }
21660 ...we get called 4 times. Twice in early debug and twice in
21661 late debug:
21663 Early debug
21664 -----------
21666 1. Once while generating func() within the namespace. This is
21667 the declaration. The declaration bit below is set, as the
21668 context is the namespace.
21670 A new DIE will be generated with DW_AT_declaration set.
21672 2. Once for func() itself. This is the specification. The
21673 declaration bit below is clear as the context is the CU.
21675 We will use the cached DIE from (1) to create a new DIE with
21676 DW_AT_specification pointing to the declaration in (1).
21678 Late debug via rest_of_handle_final()
21679 -------------------------------------
21681 3. Once generating func() within the namespace. This is also the
21682 declaration, as in (1), but this time we will early exit below
21683 as we have a cached DIE and a declaration needs no additional
21684 annotations (no locations), as the source declaration line
21685 info is enough.
21687 4. Once for func() itself. As in (2), this is the specification,
21688 but this time we will re-use the cached DIE, and just annotate
21689 it with the location information that should now be available.
21691 For something without namespaces, but with abstract instances, we
21692 are also called a multiple times:
21694 class Base
21696 public:
21697 Base (); // constructor declaration (1)
21700 Base::Base () { } // constructor specification (2)
21702 Early debug
21703 -----------
21705 1. Once for the Base() constructor by virtue of it being a
21706 member of the Base class. This is done via
21707 rest_of_type_compilation.
21709 This is a declaration, so a new DIE will be created with
21710 DW_AT_declaration.
21712 2. Once for the Base() constructor definition, but this time
21713 while generating the abstract instance of the base
21714 constructor (__base_ctor) which is being generated via early
21715 debug of reachable functions.
21717 Even though we have a cached version of the declaration (1),
21718 we will create a DW_AT_specification of the declaration DIE
21719 in (1).
21721 3. Once for the __base_ctor itself, but this time, we generate
21722 an DW_AT_abstract_origin version of the DW_AT_specification in
21723 (2).
21725 Late debug via rest_of_handle_final
21726 -----------------------------------
21728 4. One final time for the __base_ctor (which will have a cached
21729 DIE with DW_AT_abstract_origin created in (3). This time,
21730 we will just annotate the location information now
21731 available.
21733 int declaration = (current_function_decl != decl
21734 || class_or_namespace_scope_p (context_die));
21736 /* Now that the C++ front end lazily declares artificial member fns, we
21737 might need to retrofit the declaration into its class. */
21738 if (!declaration && !origin && !old_die
21739 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21740 && !class_or_namespace_scope_p (context_die)
21741 && debug_info_level > DINFO_LEVEL_TERSE)
21742 old_die = force_decl_die (decl);
21744 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21745 if (origin != NULL)
21747 gcc_assert (!declaration || local_scope_p (context_die));
21749 /* Fixup die_parent for the abstract instance of a nested
21750 inline function. */
21751 if (old_die && old_die->die_parent == NULL)
21752 add_child_die (context_die, old_die);
21754 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21756 /* If we have a DW_AT_abstract_origin we have a working
21757 cached version. */
21758 subr_die = old_die;
21760 else
21762 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21763 add_abstract_origin_attribute (subr_die, origin);
21764 /* This is where the actual code for a cloned function is.
21765 Let's emit linkage name attribute for it. This helps
21766 debuggers to e.g, set breakpoints into
21767 constructors/destructors when the user asks "break
21768 K::K". */
21769 add_linkage_name (subr_die, decl);
21772 /* A cached copy, possibly from early dwarf generation. Reuse as
21773 much as possible. */
21774 else if (old_die)
21776 /* A declaration that has been previously dumped needs no
21777 additional information. */
21778 if (declaration)
21779 return;
21781 if (!get_AT_flag (old_die, DW_AT_declaration)
21782 /* We can have a normal definition following an inline one in the
21783 case of redefinition of GNU C extern inlines.
21784 It seems reasonable to use AT_specification in this case. */
21785 && !get_AT (old_die, DW_AT_inline))
21787 /* Detect and ignore this case, where we are trying to output
21788 something we have already output. */
21789 if (get_AT (old_die, DW_AT_low_pc)
21790 || get_AT (old_die, DW_AT_ranges))
21791 return;
21793 /* If we have no location information, this must be a
21794 partially generated DIE from early dwarf generation.
21795 Fall through and generate it. */
21798 /* If the definition comes from the same place as the declaration,
21799 maybe use the old DIE. We always want the DIE for this function
21800 that has the *_pc attributes to be under comp_unit_die so the
21801 debugger can find it. We also need to do this for abstract
21802 instances of inlines, since the spec requires the out-of-line copy
21803 to have the same parent. For local class methods, this doesn't
21804 apply; we just use the old DIE. */
21805 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21806 struct dwarf_file_data * file_index = lookup_filename (s.file);
21807 if ((is_cu_die (old_die->die_parent)
21808 /* This condition fixes the inconsistency/ICE with the
21809 following Fortran test (or some derivative thereof) while
21810 building libgfortran:
21812 module some_m
21813 contains
21814 logical function funky (FLAG)
21815 funky = .true.
21816 end function
21817 end module
21819 || (old_die->die_parent
21820 && old_die->die_parent->die_tag == DW_TAG_module)
21821 || context_die == NULL)
21822 && (DECL_ARTIFICIAL (decl)
21823 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21824 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21825 == (unsigned) s.line))))
21827 subr_die = old_die;
21829 /* Clear out the declaration attribute, but leave the
21830 parameters so they can be augmented with location
21831 information later. Unless this was a declaration, in
21832 which case, wipe out the nameless parameters and recreate
21833 them further down. */
21834 if (remove_AT (subr_die, DW_AT_declaration))
21837 remove_AT (subr_die, DW_AT_object_pointer);
21838 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21841 /* Make a specification pointing to the previously built
21842 declaration. */
21843 else
21845 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21846 add_AT_specification (subr_die, old_die);
21847 add_pubname (decl, subr_die);
21848 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21849 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21850 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21851 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21853 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21854 emit the real type on the definition die. */
21855 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
21857 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
21858 if (die == auto_die || die == decltype_auto_die)
21859 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21860 TYPE_UNQUALIFIED, false, context_die);
21863 /* When we process the method declaration, we haven't seen
21864 the out-of-class defaulted definition yet, so we have to
21865 recheck now. */
21866 if ((dwarf_version >= 5 || ! dwarf_strict)
21867 && !get_AT (subr_die, DW_AT_defaulted))
21869 int defaulted
21870 = lang_hooks.decls.decl_dwarf_attribute (decl,
21871 DW_AT_defaulted);
21872 if (defaulted != -1)
21874 /* Other values must have been handled before. */
21875 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
21876 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21881 /* Create a fresh DIE for anything else. */
21882 else
21884 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21886 if (TREE_PUBLIC (decl))
21887 add_AT_flag (subr_die, DW_AT_external, 1);
21889 add_name_and_src_coords_attributes (subr_die, decl);
21890 add_pubname (decl, subr_die);
21891 if (debug_info_level > DINFO_LEVEL_TERSE)
21893 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
21894 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21895 TYPE_UNQUALIFIED, false, context_die);
21898 add_pure_or_virtual_attribute (subr_die, decl);
21899 if (DECL_ARTIFICIAL (decl))
21900 add_AT_flag (subr_die, DW_AT_artificial, 1);
21902 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
21903 add_AT_flag (subr_die, DW_AT_noreturn, 1);
21905 add_accessibility_attribute (subr_die, decl);
21908 /* Unless we have an existing non-declaration DIE, equate the new
21909 DIE. */
21910 if (!old_die || is_declaration_die (old_die))
21911 equate_decl_number_to_die (decl, subr_die);
21913 if (declaration)
21915 if (!old_die || !get_AT (old_die, DW_AT_inline))
21917 add_AT_flag (subr_die, DW_AT_declaration, 1);
21919 /* If this is an explicit function declaration then generate
21920 a DW_AT_explicit attribute. */
21921 if ((dwarf_version >= 3 || !dwarf_strict)
21922 && lang_hooks.decls.decl_dwarf_attribute (decl,
21923 DW_AT_explicit) == 1)
21924 add_AT_flag (subr_die, DW_AT_explicit, 1);
21926 /* If this is a C++11 deleted special function member then generate
21927 a DW_AT_deleted attribute. */
21928 if ((dwarf_version >= 5 || !dwarf_strict)
21929 && lang_hooks.decls.decl_dwarf_attribute (decl,
21930 DW_AT_deleted) == 1)
21931 add_AT_flag (subr_die, DW_AT_deleted, 1);
21933 /* If this is a C++11 defaulted special function member then
21934 generate a DW_AT_defaulted attribute. */
21935 if (dwarf_version >= 5 || !dwarf_strict)
21937 int defaulted
21938 = lang_hooks.decls.decl_dwarf_attribute (decl,
21939 DW_AT_defaulted);
21940 if (defaulted != -1)
21941 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
21944 /* If this is a C++11 non-static member function with & ref-qualifier
21945 then generate a DW_AT_reference attribute. */
21946 if ((dwarf_version >= 5 || !dwarf_strict)
21947 && lang_hooks.decls.decl_dwarf_attribute (decl,
21948 DW_AT_reference) == 1)
21949 add_AT_flag (subr_die, DW_AT_reference, 1);
21951 /* If this is a C++11 non-static member function with &&
21952 ref-qualifier then generate a DW_AT_reference attribute. */
21953 if ((dwarf_version >= 5 || !dwarf_strict)
21954 && lang_hooks.decls.decl_dwarf_attribute (decl,
21955 DW_AT_rvalue_reference)
21956 == 1)
21957 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
21960 /* Tag abstract instances with DW_AT_inline. */
21961 else if (DECL_ABSTRACT_P (decl))
21963 if (DECL_DECLARED_INLINE_P (decl))
21965 if (cgraph_function_possibly_inlined_p (decl))
21966 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
21967 else
21968 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
21970 else
21972 if (cgraph_function_possibly_inlined_p (decl))
21973 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
21974 else
21975 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
21978 if (DECL_DECLARED_INLINE_P (decl)
21979 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21980 add_AT_flag (subr_die, DW_AT_artificial, 1);
21982 /* For non DECL_EXTERNALs, if range information is available, fill
21983 the DIE with it. */
21984 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
21986 HOST_WIDE_INT cfa_fb_offset;
21988 struct function *fun = DECL_STRUCT_FUNCTION (decl);
21990 if (!flag_reorder_blocks_and_partition)
21992 dw_fde_ref fde = fun->fde;
21993 if (fde->dw_fde_begin)
21995 /* We have already generated the labels. */
21996 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
21997 fde->dw_fde_end, false);
21999 else
22001 /* Create start/end labels and add the range. */
22002 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22003 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22004 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22005 current_function_funcdef_no);
22006 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22007 current_function_funcdef_no);
22008 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22009 false);
22012 #if VMS_DEBUGGING_INFO
22013 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22014 Section 2.3 Prologue and Epilogue Attributes:
22015 When a breakpoint is set on entry to a function, it is generally
22016 desirable for execution to be suspended, not on the very first
22017 instruction of the function, but rather at a point after the
22018 function's frame has been set up, after any language defined local
22019 declaration processing has been completed, and before execution of
22020 the first statement of the function begins. Debuggers generally
22021 cannot properly determine where this point is. Similarly for a
22022 breakpoint set on exit from a function. The prologue and epilogue
22023 attributes allow a compiler to communicate the location(s) to use. */
22026 if (fde->dw_fde_vms_end_prologue)
22027 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22028 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22030 if (fde->dw_fde_vms_begin_epilogue)
22031 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22032 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22034 #endif
22037 else
22039 /* Generate pubnames entries for the split function code ranges. */
22040 dw_fde_ref fde = fun->fde;
22042 if (fde->dw_fde_second_begin)
22044 if (dwarf_version >= 3 || !dwarf_strict)
22046 /* We should use ranges for non-contiguous code section
22047 addresses. Use the actual code range for the initial
22048 section, since the HOT/COLD labels might precede an
22049 alignment offset. */
22050 bool range_list_added = false;
22051 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22052 fde->dw_fde_end, &range_list_added,
22053 false);
22054 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22055 fde->dw_fde_second_end,
22056 &range_list_added, false);
22057 if (range_list_added)
22058 add_ranges (NULL);
22060 else
22062 /* There is no real support in DW2 for this .. so we make
22063 a work-around. First, emit the pub name for the segment
22064 containing the function label. Then make and emit a
22065 simplified subprogram DIE for the second segment with the
22066 name pre-fixed by __hot/cold_sect_of_. We use the same
22067 linkage name for the second die so that gdb will find both
22068 sections when given "b foo". */
22069 const char *name = NULL;
22070 tree decl_name = DECL_NAME (decl);
22071 dw_die_ref seg_die;
22073 /* Do the 'primary' section. */
22074 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22075 fde->dw_fde_end, false);
22077 /* Build a minimal DIE for the secondary section. */
22078 seg_die = new_die (DW_TAG_subprogram,
22079 subr_die->die_parent, decl);
22081 if (TREE_PUBLIC (decl))
22082 add_AT_flag (seg_die, DW_AT_external, 1);
22084 if (decl_name != NULL
22085 && IDENTIFIER_POINTER (decl_name) != NULL)
22087 name = dwarf2_name (decl, 1);
22088 if (! DECL_ARTIFICIAL (decl))
22089 add_src_coords_attributes (seg_die, decl);
22091 add_linkage_name (seg_die, decl);
22093 gcc_assert (name != NULL);
22094 add_pure_or_virtual_attribute (seg_die, decl);
22095 if (DECL_ARTIFICIAL (decl))
22096 add_AT_flag (seg_die, DW_AT_artificial, 1);
22098 name = concat ("__second_sect_of_", name, NULL);
22099 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22100 fde->dw_fde_second_end, false);
22101 add_name_attribute (seg_die, name);
22102 if (want_pubnames ())
22103 add_pubname_string (name, seg_die);
22106 else
22107 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22108 false);
22111 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22113 /* We define the "frame base" as the function's CFA. This is more
22114 convenient for several reasons: (1) It's stable across the prologue
22115 and epilogue, which makes it better than just a frame pointer,
22116 (2) With dwarf3, there exists a one-byte encoding that allows us
22117 to reference the .debug_frame data by proxy, but failing that,
22118 (3) We can at least reuse the code inspection and interpretation
22119 code that determines the CFA position at various points in the
22120 function. */
22121 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22123 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22124 add_AT_loc (subr_die, DW_AT_frame_base, op);
22126 else
22128 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22129 if (list->dw_loc_next)
22130 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22131 else
22132 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22135 /* Compute a displacement from the "steady-state frame pointer" to
22136 the CFA. The former is what all stack slots and argument slots
22137 will reference in the rtl; the latter is what we've told the
22138 debugger about. We'll need to adjust all frame_base references
22139 by this displacement. */
22140 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22142 if (fun->static_chain_decl)
22144 /* DWARF requires here a location expression that computes the
22145 address of the enclosing subprogram's frame base. The machinery
22146 in tree-nested.c is supposed to store this specific address in the
22147 last field of the FRAME record. */
22148 const tree frame_type
22149 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22150 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22152 tree fb_expr
22153 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22154 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22155 fb_expr, fb_decl, NULL_TREE);
22157 add_AT_location_description (subr_die, DW_AT_static_link,
22158 loc_list_from_tree (fb_expr, 0, NULL));
22162 /* Generate child dies for template paramaters. */
22163 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22164 gen_generic_params_dies (decl);
22166 /* Now output descriptions of the arguments for this function. This gets
22167 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22168 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22169 `...' at the end of the formal parameter list. In order to find out if
22170 there was a trailing ellipsis or not, we must instead look at the type
22171 associated with the FUNCTION_DECL. This will be a node of type
22172 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22173 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22174 an ellipsis at the end. */
22176 /* In the case where we are describing a mere function declaration, all we
22177 need to do here (and all we *can* do here) is to describe the *types* of
22178 its formal parameters. */
22179 if (debug_info_level <= DINFO_LEVEL_TERSE)
22181 else if (declaration)
22182 gen_formal_types_die (decl, subr_die);
22183 else
22185 /* Generate DIEs to represent all known formal parameters. */
22186 tree parm = DECL_ARGUMENTS (decl);
22187 tree generic_decl = early_dwarf
22188 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22189 tree generic_decl_parm = generic_decl
22190 ? DECL_ARGUMENTS (generic_decl)
22191 : NULL;
22192 auto_vec<dw_die_ref> string_types_vec;
22193 if (string_types == NULL)
22194 string_types = &string_types_vec;
22196 /* Now we want to walk the list of parameters of the function and
22197 emit their relevant DIEs.
22199 We consider the case of DECL being an instance of a generic function
22200 as well as it being a normal function.
22202 If DECL is an instance of a generic function we walk the
22203 parameters of the generic function declaration _and_ the parameters of
22204 DECL itself. This is useful because we want to emit specific DIEs for
22205 function parameter packs and those are declared as part of the
22206 generic function declaration. In that particular case,
22207 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22208 That DIE has children DIEs representing the set of arguments
22209 of the pack. Note that the set of pack arguments can be empty.
22210 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22211 children DIE.
22213 Otherwise, we just consider the parameters of DECL. */
22214 while (generic_decl_parm || parm)
22216 if (generic_decl_parm
22217 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22218 gen_formal_parameter_pack_die (generic_decl_parm,
22219 parm, subr_die,
22220 &parm);
22221 else if (parm && !POINTER_BOUNDS_P (parm))
22223 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22225 if (parm == DECL_ARGUMENTS (decl)
22226 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22227 && parm_die
22228 && (dwarf_version >= 3 || !dwarf_strict))
22229 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22231 parm = DECL_CHAIN (parm);
22233 else if (parm)
22234 parm = DECL_CHAIN (parm);
22236 if (generic_decl_parm)
22237 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22240 /* Decide whether we need an unspecified_parameters DIE at the end.
22241 There are 2 more cases to do this for: 1) the ansi ... declaration -
22242 this is detectable when the end of the arg list is not a
22243 void_type_node 2) an unprototyped function declaration (not a
22244 definition). This just means that we have no info about the
22245 parameters at all. */
22246 if (early_dwarf)
22248 if (prototype_p (TREE_TYPE (decl)))
22250 /* This is the prototyped case, check for.... */
22251 if (stdarg_p (TREE_TYPE (decl)))
22252 gen_unspecified_parameters_die (decl, subr_die);
22254 else if (DECL_INITIAL (decl) == NULL_TREE)
22255 gen_unspecified_parameters_die (decl, subr_die);
22258 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
22259 have DIEs. */
22260 if (string_types == &string_types_vec)
22262 adjust_string_types ();
22263 string_types = NULL;
22267 if (subr_die != old_die)
22268 /* Add the calling convention attribute if requested. */
22269 add_calling_convention_attribute (subr_die, decl);
22271 /* Output Dwarf info for all of the stuff within the body of the function
22272 (if it has one - it may be just a declaration).
22274 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22275 a function. This BLOCK actually represents the outermost binding contour
22276 for the function, i.e. the contour in which the function's formal
22277 parameters and labels get declared. Curiously, it appears that the front
22278 end doesn't actually put the PARM_DECL nodes for the current function onto
22279 the BLOCK_VARS list for this outer scope, but are strung off of the
22280 DECL_ARGUMENTS list for the function instead.
22282 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22283 the LABEL_DECL nodes for the function however, and we output DWARF info
22284 for those in decls_for_scope. Just within the `outer_scope' there will be
22285 a BLOCK node representing the function's outermost pair of curly braces,
22286 and any blocks used for the base and member initializers of a C++
22287 constructor function. */
22288 tree outer_scope = DECL_INITIAL (decl);
22289 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22291 int call_site_note_count = 0;
22292 int tail_call_site_note_count = 0;
22294 /* Emit a DW_TAG_variable DIE for a named return value. */
22295 if (DECL_NAME (DECL_RESULT (decl)))
22296 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22298 /* The first time through decls_for_scope we will generate the
22299 DIEs for the locals. The second time, we fill in the
22300 location info. */
22301 decls_for_scope (outer_scope, subr_die);
22303 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22305 struct call_arg_loc_node *ca_loc;
22306 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22308 dw_die_ref die = NULL;
22309 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22310 rtx arg, next_arg;
22312 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22313 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22314 : NULL_RTX);
22315 arg; arg = next_arg)
22317 dw_loc_descr_ref reg, val;
22318 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22319 dw_die_ref cdie, tdie = NULL;
22321 next_arg = XEXP (arg, 1);
22322 if (REG_P (XEXP (XEXP (arg, 0), 0))
22323 && next_arg
22324 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22325 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22326 && REGNO (XEXP (XEXP (arg, 0), 0))
22327 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22328 next_arg = XEXP (next_arg, 1);
22329 if (mode == VOIDmode)
22331 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22332 if (mode == VOIDmode)
22333 mode = GET_MODE (XEXP (arg, 0));
22335 if (mode == VOIDmode || mode == BLKmode)
22336 continue;
22337 /* Get dynamic information about call target only if we
22338 have no static information: we cannot generate both
22339 DW_AT_call_origin and DW_AT_call_target
22340 attributes. */
22341 if (ca_loc->symbol_ref == NULL_RTX)
22343 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22345 tloc = XEXP (XEXP (arg, 0), 1);
22346 continue;
22348 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22349 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22351 tlocc = XEXP (XEXP (arg, 0), 1);
22352 continue;
22355 reg = NULL;
22356 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22357 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22358 VAR_INIT_STATUS_INITIALIZED);
22359 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22361 rtx mem = XEXP (XEXP (arg, 0), 0);
22362 reg = mem_loc_descriptor (XEXP (mem, 0),
22363 get_address_mode (mem),
22364 GET_MODE (mem),
22365 VAR_INIT_STATUS_INITIALIZED);
22367 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22368 == DEBUG_PARAMETER_REF)
22370 tree tdecl
22371 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22372 tdie = lookup_decl_die (tdecl);
22373 if (tdie == NULL)
22374 continue;
22376 else
22377 continue;
22378 if (reg == NULL
22379 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22380 != DEBUG_PARAMETER_REF)
22381 continue;
22382 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22383 VOIDmode,
22384 VAR_INIT_STATUS_INITIALIZED);
22385 if (val == NULL)
22386 continue;
22387 if (die == NULL)
22388 die = gen_call_site_die (decl, subr_die, ca_loc);
22389 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22390 NULL_TREE);
22391 if (reg != NULL)
22392 add_AT_loc (cdie, DW_AT_location, reg);
22393 else if (tdie != NULL)
22394 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22395 tdie);
22396 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22397 if (next_arg != XEXP (arg, 1))
22399 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22400 if (mode == VOIDmode)
22401 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22402 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22403 0), 1),
22404 mode, VOIDmode,
22405 VAR_INIT_STATUS_INITIALIZED);
22406 if (val != NULL)
22407 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22408 val);
22411 if (die == NULL
22412 && (ca_loc->symbol_ref || tloc))
22413 die = gen_call_site_die (decl, subr_die, ca_loc);
22414 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22416 dw_loc_descr_ref tval = NULL;
22418 if (tloc != NULL_RTX)
22419 tval = mem_loc_descriptor (tloc,
22420 GET_MODE (tloc) == VOIDmode
22421 ? Pmode : GET_MODE (tloc),
22422 VOIDmode,
22423 VAR_INIT_STATUS_INITIALIZED);
22424 if (tval)
22425 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22426 else if (tlocc != NULL_RTX)
22428 tval = mem_loc_descriptor (tlocc,
22429 GET_MODE (tlocc) == VOIDmode
22430 ? Pmode : GET_MODE (tlocc),
22431 VOIDmode,
22432 VAR_INIT_STATUS_INITIALIZED);
22433 if (tval)
22434 add_AT_loc (die,
22435 dwarf_AT (DW_AT_call_target_clobbered),
22436 tval);
22439 if (die != NULL)
22441 call_site_note_count++;
22442 if (ca_loc->tail_call_p)
22443 tail_call_site_note_count++;
22447 call_arg_locations = NULL;
22448 call_arg_loc_last = NULL;
22449 if (tail_call_site_count >= 0
22450 && tail_call_site_count == tail_call_site_note_count
22451 && (!dwarf_strict || dwarf_version >= 5))
22453 if (call_site_count >= 0
22454 && call_site_count == call_site_note_count)
22455 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22456 else
22457 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22459 call_site_count = -1;
22460 tail_call_site_count = -1;
22463 /* Mark used types after we have created DIEs for the functions scopes. */
22464 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22467 /* Returns a hash value for X (which really is a die_struct). */
22469 hashval_t
22470 block_die_hasher::hash (die_struct *d)
22472 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22475 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22476 as decl_id and die_parent of die_struct Y. */
22478 bool
22479 block_die_hasher::equal (die_struct *x, die_struct *y)
22481 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22484 /* Return TRUE if DECL, which may have been previously generated as
22485 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22486 true if decl (or its origin) is either an extern declaration or a
22487 class/namespace scoped declaration.
22489 The declare_in_namespace support causes us to get two DIEs for one
22490 variable, both of which are declarations. We want to avoid
22491 considering one to be a specification, so we must test for
22492 DECLARATION and DW_AT_declaration. */
22493 static inline bool
22494 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22496 return (old_die && TREE_STATIC (decl) && !declaration
22497 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22500 /* Return true if DECL is a local static. */
22502 static inline bool
22503 local_function_static (tree decl)
22505 gcc_assert (VAR_P (decl));
22506 return TREE_STATIC (decl)
22507 && DECL_CONTEXT (decl)
22508 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22511 /* Generate a DIE to represent a declared data object.
22512 Either DECL or ORIGIN must be non-null. */
22514 static void
22515 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22517 HOST_WIDE_INT off = 0;
22518 tree com_decl;
22519 tree decl_or_origin = decl ? decl : origin;
22520 tree ultimate_origin;
22521 dw_die_ref var_die;
22522 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22523 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22524 || class_or_namespace_scope_p (context_die));
22525 bool specialization_p = false;
22526 bool no_linkage_name = false;
22528 /* While C++ inline static data members have definitions inside of the
22529 class, force the first DIE to be a declaration, then let gen_member_die
22530 reparent it to the class context and call gen_variable_die again
22531 to create the outside of the class DIE for the definition. */
22532 if (!declaration
22533 && old_die == NULL
22534 && decl
22535 && DECL_CONTEXT (decl)
22536 && TYPE_P (DECL_CONTEXT (decl))
22537 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22539 declaration = true;
22540 no_linkage_name = true;
22543 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22544 if (decl || ultimate_origin)
22545 origin = ultimate_origin;
22546 com_decl = fortran_common (decl_or_origin, &off);
22548 /* Symbol in common gets emitted as a child of the common block, in the form
22549 of a data member. */
22550 if (com_decl)
22552 dw_die_ref com_die;
22553 dw_loc_list_ref loc = NULL;
22554 die_node com_die_arg;
22556 var_die = lookup_decl_die (decl_or_origin);
22557 if (var_die)
22559 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22561 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22562 if (loc)
22564 if (off)
22566 /* Optimize the common case. */
22567 if (single_element_loc_list_p (loc)
22568 && loc->expr->dw_loc_opc == DW_OP_addr
22569 && loc->expr->dw_loc_next == NULL
22570 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22571 == SYMBOL_REF)
22573 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22574 loc->expr->dw_loc_oprnd1.v.val_addr
22575 = plus_constant (GET_MODE (x), x , off);
22577 else
22578 loc_list_plus_const (loc, off);
22580 add_AT_location_description (var_die, DW_AT_location, loc);
22581 remove_AT (var_die, DW_AT_declaration);
22584 return;
22587 if (common_block_die_table == NULL)
22588 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22590 com_die_arg.decl_id = DECL_UID (com_decl);
22591 com_die_arg.die_parent = context_die;
22592 com_die = common_block_die_table->find (&com_die_arg);
22593 if (! early_dwarf)
22594 loc = loc_list_from_tree (com_decl, 2, NULL);
22595 if (com_die == NULL)
22597 const char *cnam
22598 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22599 die_node **slot;
22601 com_die = new_die (DW_TAG_common_block, context_die, decl);
22602 add_name_and_src_coords_attributes (com_die, com_decl);
22603 if (loc)
22605 add_AT_location_description (com_die, DW_AT_location, loc);
22606 /* Avoid sharing the same loc descriptor between
22607 DW_TAG_common_block and DW_TAG_variable. */
22608 loc = loc_list_from_tree (com_decl, 2, NULL);
22610 else if (DECL_EXTERNAL (decl_or_origin))
22611 add_AT_flag (com_die, DW_AT_declaration, 1);
22612 if (want_pubnames ())
22613 add_pubname_string (cnam, com_die); /* ??? needed? */
22614 com_die->decl_id = DECL_UID (com_decl);
22615 slot = common_block_die_table->find_slot (com_die, INSERT);
22616 *slot = com_die;
22618 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22620 add_AT_location_description (com_die, DW_AT_location, loc);
22621 loc = loc_list_from_tree (com_decl, 2, NULL);
22622 remove_AT (com_die, DW_AT_declaration);
22624 var_die = new_die (DW_TAG_variable, com_die, decl);
22625 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22626 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22627 decl_quals (decl_or_origin), false,
22628 context_die);
22629 add_AT_flag (var_die, DW_AT_external, 1);
22630 if (loc)
22632 if (off)
22634 /* Optimize the common case. */
22635 if (single_element_loc_list_p (loc)
22636 && loc->expr->dw_loc_opc == DW_OP_addr
22637 && loc->expr->dw_loc_next == NULL
22638 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22640 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22641 loc->expr->dw_loc_oprnd1.v.val_addr
22642 = plus_constant (GET_MODE (x), x, off);
22644 else
22645 loc_list_plus_const (loc, off);
22647 add_AT_location_description (var_die, DW_AT_location, loc);
22649 else if (DECL_EXTERNAL (decl_or_origin))
22650 add_AT_flag (var_die, DW_AT_declaration, 1);
22651 if (decl)
22652 equate_decl_number_to_die (decl, var_die);
22653 return;
22656 if (old_die)
22658 if (declaration)
22660 /* A declaration that has been previously dumped, needs no
22661 further annotations, since it doesn't need location on
22662 the second pass. */
22663 return;
22665 else if (decl_will_get_specification_p (old_die, decl, declaration)
22666 && !get_AT (old_die, DW_AT_specification))
22668 /* Fall-thru so we can make a new variable die along with a
22669 DW_AT_specification. */
22671 else if (origin && old_die->die_parent != context_die)
22673 /* If we will be creating an inlined instance, we need a
22674 new DIE that will get annotated with
22675 DW_AT_abstract_origin. Clear things so we can get a
22676 new DIE. */
22677 gcc_assert (!DECL_ABSTRACT_P (decl));
22678 old_die = NULL;
22680 else
22682 /* If a DIE was dumped early, it still needs location info.
22683 Skip to where we fill the location bits. */
22684 var_die = old_die;
22685 goto gen_variable_die_location;
22689 /* For static data members, the declaration in the class is supposed
22690 to have DW_TAG_member tag; the specification should still be
22691 DW_TAG_variable referencing the DW_TAG_member DIE. */
22692 if (declaration && class_scope_p (context_die))
22693 var_die = new_die (DW_TAG_member, context_die, decl);
22694 else
22695 var_die = new_die (DW_TAG_variable, context_die, decl);
22697 if (origin != NULL)
22698 add_abstract_origin_attribute (var_die, origin);
22700 /* Loop unrolling can create multiple blocks that refer to the same
22701 static variable, so we must test for the DW_AT_declaration flag.
22703 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22704 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22705 sharing them.
22707 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22708 else if (decl_will_get_specification_p (old_die, decl, declaration))
22710 /* This is a definition of a C++ class level static. */
22711 add_AT_specification (var_die, old_die);
22712 specialization_p = true;
22713 if (DECL_NAME (decl))
22715 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22716 struct dwarf_file_data * file_index = lookup_filename (s.file);
22718 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22719 add_AT_file (var_die, DW_AT_decl_file, file_index);
22721 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22722 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22724 if (old_die->die_tag == DW_TAG_member)
22725 add_linkage_name (var_die, decl);
22728 else
22729 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22731 if ((origin == NULL && !specialization_p)
22732 || (origin != NULL
22733 && !DECL_ABSTRACT_P (decl_or_origin)
22734 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22735 decl_function_context
22736 (decl_or_origin))))
22738 tree type = TREE_TYPE (decl_or_origin);
22740 if (decl_by_reference_p (decl_or_origin))
22741 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22742 context_die);
22743 else
22744 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22745 context_die);
22748 if (origin == NULL && !specialization_p)
22750 if (TREE_PUBLIC (decl))
22751 add_AT_flag (var_die, DW_AT_external, 1);
22753 if (DECL_ARTIFICIAL (decl))
22754 add_AT_flag (var_die, DW_AT_artificial, 1);
22756 add_accessibility_attribute (var_die, decl);
22759 if (declaration)
22760 add_AT_flag (var_die, DW_AT_declaration, 1);
22762 if (decl && (DECL_ABSTRACT_P (decl)
22763 || !old_die || is_declaration_die (old_die)))
22764 equate_decl_number_to_die (decl, var_die);
22766 gen_variable_die_location:
22767 if (! declaration
22768 && (! DECL_ABSTRACT_P (decl_or_origin)
22769 /* Local static vars are shared between all clones/inlines,
22770 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22771 already set. */
22772 || (VAR_P (decl_or_origin)
22773 && TREE_STATIC (decl_or_origin)
22774 && DECL_RTL_SET_P (decl_or_origin))))
22776 if (early_dwarf)
22777 add_pubname (decl_or_origin, var_die);
22778 else
22779 add_location_or_const_value_attribute (var_die, decl_or_origin,
22780 decl == NULL);
22782 else
22783 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22785 if ((dwarf_version >= 4 || !dwarf_strict)
22786 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22787 DW_AT_const_expr) == 1
22788 && !get_AT (var_die, DW_AT_const_expr)
22789 && !specialization_p)
22790 add_AT_flag (var_die, DW_AT_const_expr, 1);
22792 if (!dwarf_strict)
22794 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22795 DW_AT_inline);
22796 if (inl != -1
22797 && !get_AT (var_die, DW_AT_inline)
22798 && !specialization_p)
22799 add_AT_unsigned (var_die, DW_AT_inline, inl);
22803 /* Generate a DIE to represent a named constant. */
22805 static void
22806 gen_const_die (tree decl, dw_die_ref context_die)
22808 dw_die_ref const_die;
22809 tree type = TREE_TYPE (decl);
22811 const_die = lookup_decl_die (decl);
22812 if (const_die)
22813 return;
22815 const_die = new_die (DW_TAG_constant, context_die, decl);
22816 equate_decl_number_to_die (decl, const_die);
22817 add_name_and_src_coords_attributes (const_die, decl);
22818 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22819 if (TREE_PUBLIC (decl))
22820 add_AT_flag (const_die, DW_AT_external, 1);
22821 if (DECL_ARTIFICIAL (decl))
22822 add_AT_flag (const_die, DW_AT_artificial, 1);
22823 tree_add_const_value_attribute_for_decl (const_die, decl);
22826 /* Generate a DIE to represent a label identifier. */
22828 static void
22829 gen_label_die (tree decl, dw_die_ref context_die)
22831 tree origin = decl_ultimate_origin (decl);
22832 dw_die_ref lbl_die = lookup_decl_die (decl);
22833 rtx insn;
22834 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22836 if (!lbl_die)
22838 lbl_die = new_die (DW_TAG_label, context_die, decl);
22839 equate_decl_number_to_die (decl, lbl_die);
22841 if (origin != NULL)
22842 add_abstract_origin_attribute (lbl_die, origin);
22843 else
22844 add_name_and_src_coords_attributes (lbl_die, decl);
22847 if (DECL_ABSTRACT_P (decl))
22848 equate_decl_number_to_die (decl, lbl_die);
22849 else if (! early_dwarf)
22851 insn = DECL_RTL_IF_SET (decl);
22853 /* Deleted labels are programmer specified labels which have been
22854 eliminated because of various optimizations. We still emit them
22855 here so that it is possible to put breakpoints on them. */
22856 if (insn
22857 && (LABEL_P (insn)
22858 || ((NOTE_P (insn)
22859 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
22861 /* When optimization is enabled (via -O) some parts of the compiler
22862 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
22863 represent source-level labels which were explicitly declared by
22864 the user. This really shouldn't be happening though, so catch
22865 it if it ever does happen. */
22866 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
22868 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
22869 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22871 else if (insn
22872 && NOTE_P (insn)
22873 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
22874 && CODE_LABEL_NUMBER (insn) != -1)
22876 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
22877 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
22882 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
22883 attributes to the DIE for a block STMT, to describe where the inlined
22884 function was called from. This is similar to add_src_coords_attributes. */
22886 static inline void
22887 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
22889 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
22891 if (dwarf_version >= 3 || !dwarf_strict)
22893 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
22894 add_AT_unsigned (die, DW_AT_call_line, s.line);
22899 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
22900 Add low_pc and high_pc attributes to the DIE for a block STMT. */
22902 static inline void
22903 add_high_low_attributes (tree stmt, dw_die_ref die)
22905 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22907 if (BLOCK_FRAGMENT_CHAIN (stmt)
22908 && (dwarf_version >= 3 || !dwarf_strict))
22910 tree chain, superblock = NULL_TREE;
22911 dw_die_ref pdie;
22912 dw_attr_node *attr = NULL;
22914 if (inlined_function_outer_scope_p (stmt))
22916 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22917 BLOCK_NUMBER (stmt));
22918 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22921 /* Optimize duplicate .debug_ranges lists or even tails of
22922 lists. If this BLOCK has same ranges as its supercontext,
22923 lookup DW_AT_ranges attribute in the supercontext (and
22924 recursively so), verify that the ranges_table contains the
22925 right values and use it instead of adding a new .debug_range. */
22926 for (chain = stmt, pdie = die;
22927 BLOCK_SAME_RANGE (chain);
22928 chain = BLOCK_SUPERCONTEXT (chain))
22930 dw_attr_node *new_attr;
22932 pdie = pdie->die_parent;
22933 if (pdie == NULL)
22934 break;
22935 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
22936 break;
22937 new_attr = get_AT (pdie, DW_AT_ranges);
22938 if (new_attr == NULL
22939 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
22940 break;
22941 attr = new_attr;
22942 superblock = BLOCK_SUPERCONTEXT (chain);
22944 if (attr != NULL
22945 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
22946 == BLOCK_NUMBER (superblock))
22947 && BLOCK_FRAGMENT_CHAIN (superblock))
22949 unsigned long off = attr->dw_attr_val.v.val_offset;
22950 unsigned long supercnt = 0, thiscnt = 0;
22951 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
22952 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
22954 ++supercnt;
22955 gcc_checking_assert ((*ranges_table)[off + supercnt].num
22956 == BLOCK_NUMBER (chain));
22958 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
22959 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
22960 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
22961 ++thiscnt;
22962 gcc_assert (supercnt >= thiscnt);
22963 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
22964 false);
22965 note_rnglist_head (off + supercnt - thiscnt);
22966 return;
22969 unsigned int offset = add_ranges (stmt, true);
22970 add_AT_range_list (die, DW_AT_ranges, offset, false);
22971 note_rnglist_head (offset);
22973 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
22974 chain = BLOCK_FRAGMENT_CHAIN (stmt);
22977 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
22978 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
22979 chain = BLOCK_FRAGMENT_CHAIN (chain);
22981 while (chain);
22982 add_ranges (NULL);
22984 else
22986 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
22987 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
22988 BLOCK_NUMBER (stmt));
22989 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
22990 BLOCK_NUMBER (stmt));
22991 add_AT_low_high_pc (die, label, label_high, false);
22995 /* Generate a DIE for a lexical block. */
22997 static void
22998 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23000 dw_die_ref old_die = BLOCK_DIE (stmt);
23001 dw_die_ref stmt_die = NULL;
23002 if (!old_die)
23004 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23005 BLOCK_DIE (stmt) = stmt_die;
23008 if (BLOCK_ABSTRACT (stmt))
23010 if (old_die)
23012 /* This must have been generated early and it won't even
23013 need location information since it's a DW_AT_inline
23014 function. */
23015 if (flag_checking)
23016 for (dw_die_ref c = context_die; c; c = c->die_parent)
23017 if (c->die_tag == DW_TAG_inlined_subroutine
23018 || c->die_tag == DW_TAG_subprogram)
23020 gcc_assert (get_AT (c, DW_AT_inline));
23021 break;
23023 return;
23026 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23028 /* If this is an inlined instance, create a new lexical die for
23029 anything below to attach DW_AT_abstract_origin to. */
23030 if (old_die)
23032 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23033 BLOCK_DIE (stmt) = stmt_die;
23034 old_die = NULL;
23037 tree origin = block_ultimate_origin (stmt);
23038 if (origin != NULL_TREE && origin != stmt)
23039 add_abstract_origin_attribute (stmt_die, origin);
23042 if (old_die)
23043 stmt_die = old_die;
23045 /* A non abstract block whose blocks have already been reordered
23046 should have the instruction range for this block. If so, set the
23047 high/low attributes. */
23048 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23050 gcc_assert (stmt_die);
23051 add_high_low_attributes (stmt, stmt_die);
23054 decls_for_scope (stmt, stmt_die);
23057 /* Generate a DIE for an inlined subprogram. */
23059 static void
23060 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23062 tree decl;
23064 /* The instance of function that is effectively being inlined shall not
23065 be abstract. */
23066 gcc_assert (! BLOCK_ABSTRACT (stmt));
23068 decl = block_ultimate_origin (stmt);
23070 /* Make sure any inlined functions are known to be inlineable. */
23071 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23072 || cgraph_function_possibly_inlined_p (decl));
23074 /* Emit info for the abstract instance first, if we haven't yet. We
23075 must emit this even if the block is abstract, otherwise when we
23076 emit the block below (or elsewhere), we may end up trying to emit
23077 a die whose origin die hasn't been emitted, and crashing. */
23078 dwarf2out_abstract_function (decl);
23080 if (! BLOCK_ABSTRACT (stmt))
23082 dw_die_ref subr_die
23083 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23085 if (call_arg_locations)
23086 BLOCK_DIE (stmt) = subr_die;
23087 add_abstract_origin_attribute (subr_die, decl);
23088 if (TREE_ASM_WRITTEN (stmt))
23089 add_high_low_attributes (stmt, subr_die);
23090 add_call_src_coords_attributes (stmt, subr_die);
23092 decls_for_scope (stmt, subr_die);
23096 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23097 the comment for VLR_CONTEXT. */
23099 static void
23100 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23102 dw_die_ref decl_die;
23104 if (TREE_TYPE (decl) == error_mark_node)
23105 return;
23107 decl_die = new_die (DW_TAG_member, context_die, decl);
23108 add_name_and_src_coords_attributes (decl_die, decl);
23109 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23110 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23111 context_die);
23113 if (DECL_BIT_FIELD_TYPE (decl))
23115 add_byte_size_attribute (decl_die, decl);
23116 add_bit_size_attribute (decl_die, decl);
23117 add_bit_offset_attribute (decl_die, decl, ctx);
23120 /* If we have a variant part offset, then we are supposed to process a member
23121 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23122 trees. */
23123 gcc_assert (ctx->variant_part_offset == NULL_TREE
23124 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23125 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23126 add_data_member_location_attribute (decl_die, decl, ctx);
23128 if (DECL_ARTIFICIAL (decl))
23129 add_AT_flag (decl_die, DW_AT_artificial, 1);
23131 add_accessibility_attribute (decl_die, decl);
23133 /* Equate decl number to die, so that we can look up this decl later on. */
23134 equate_decl_number_to_die (decl, decl_die);
23137 #if 0
23138 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23139 Use modified_type_die instead.
23140 We keep this code here just in case these types of DIEs may be needed to
23141 represent certain things in other languages (e.g. Pascal) someday. */
23143 static void
23144 gen_pointer_type_die (tree type, dw_die_ref context_die)
23146 dw_die_ref ptr_die
23147 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23149 equate_type_number_to_die (type, ptr_die);
23150 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23151 context_die);
23152 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23155 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
23156 Use modified_type_die instead.
23157 We keep this code here just in case these types of DIEs may be needed to
23158 represent certain things in other languages (e.g. Pascal) someday. */
23160 static void
23161 gen_reference_type_die (tree type, dw_die_ref context_die)
23163 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23165 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23166 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23167 else
23168 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23170 equate_type_number_to_die (type, ref_die);
23171 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23172 context_die);
23173 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23175 #endif
23177 /* Generate a DIE for a pointer to a member type. TYPE can be an
23178 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23179 pointer to member function. */
23181 static void
23182 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23184 if (lookup_type_die (type))
23185 return;
23187 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23188 scope_die_for (type, context_die), type);
23190 equate_type_number_to_die (type, ptr_die);
23191 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23192 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23193 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23194 context_die);
23196 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23197 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23199 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23200 add_AT_loc (ptr_die, DW_AT_use_location, op);
23204 static char *producer_string;
23206 /* Return a heap allocated producer string including command line options
23207 if -grecord-gcc-switches. */
23209 static char *
23210 gen_producer_string (void)
23212 size_t j;
23213 auto_vec<const char *> switches;
23214 const char *language_string = lang_hooks.name;
23215 char *producer, *tail;
23216 const char *p;
23217 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23218 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23220 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23221 switch (save_decoded_options[j].opt_index)
23223 case OPT_o:
23224 case OPT_d:
23225 case OPT_dumpbase:
23226 case OPT_dumpdir:
23227 case OPT_auxbase:
23228 case OPT_auxbase_strip:
23229 case OPT_quiet:
23230 case OPT_version:
23231 case OPT_v:
23232 case OPT_w:
23233 case OPT_L:
23234 case OPT_D:
23235 case OPT_I:
23236 case OPT_U:
23237 case OPT_SPECIAL_unknown:
23238 case OPT_SPECIAL_ignore:
23239 case OPT_SPECIAL_program_name:
23240 case OPT_SPECIAL_input_file:
23241 case OPT_grecord_gcc_switches:
23242 case OPT_gno_record_gcc_switches:
23243 case OPT__output_pch_:
23244 case OPT_fdiagnostics_show_location_:
23245 case OPT_fdiagnostics_show_option:
23246 case OPT_fdiagnostics_show_caret:
23247 case OPT_fdiagnostics_color_:
23248 case OPT_fverbose_asm:
23249 case OPT____:
23250 case OPT__sysroot_:
23251 case OPT_nostdinc:
23252 case OPT_nostdinc__:
23253 case OPT_fpreprocessed:
23254 case OPT_fltrans_output_list_:
23255 case OPT_fresolution_:
23256 case OPT_fdebug_prefix_map_:
23257 /* Ignore these. */
23258 continue;
23259 default:
23260 if (cl_options[save_decoded_options[j].opt_index].flags
23261 & CL_NO_DWARF_RECORD)
23262 continue;
23263 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23264 == '-');
23265 switch (save_decoded_options[j].canonical_option[0][1])
23267 case 'M':
23268 case 'i':
23269 case 'W':
23270 continue;
23271 case 'f':
23272 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23273 "dump", 4) == 0)
23274 continue;
23275 break;
23276 default:
23277 break;
23279 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23280 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23281 break;
23284 producer = XNEWVEC (char, plen + 1 + len + 1);
23285 tail = producer;
23286 sprintf (tail, "%s %s", language_string, version_string);
23287 tail += plen;
23289 FOR_EACH_VEC_ELT (switches, j, p)
23291 len = strlen (p);
23292 *tail = ' ';
23293 memcpy (tail + 1, p, len);
23294 tail += len + 1;
23297 *tail = '\0';
23298 return producer;
23301 /* Given a C and/or C++ language/version string return the "highest".
23302 C++ is assumed to be "higher" than C in this case. Used for merging
23303 LTO translation unit languages. */
23304 static const char *
23305 highest_c_language (const char *lang1, const char *lang2)
23307 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23308 return "GNU C++14";
23309 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23310 return "GNU C++11";
23311 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23312 return "GNU C++98";
23314 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23315 return "GNU C11";
23316 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23317 return "GNU C99";
23318 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23319 return "GNU C89";
23321 gcc_unreachable ();
23325 /* Generate the DIE for the compilation unit. */
23327 static dw_die_ref
23328 gen_compile_unit_die (const char *filename)
23330 dw_die_ref die;
23331 const char *language_string = lang_hooks.name;
23332 int language;
23334 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23336 if (filename)
23338 add_name_attribute (die, filename);
23339 /* Don't add cwd for <built-in>. */
23340 if (filename[0] != '<')
23341 add_comp_dir_attribute (die);
23344 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23346 /* If our producer is LTO try to figure out a common language to use
23347 from the global list of translation units. */
23348 if (strcmp (language_string, "GNU GIMPLE") == 0)
23350 unsigned i;
23351 tree t;
23352 const char *common_lang = NULL;
23354 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23356 if (!TRANSLATION_UNIT_LANGUAGE (t))
23357 continue;
23358 if (!common_lang)
23359 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23360 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23362 else if (strncmp (common_lang, "GNU C", 5) == 0
23363 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23364 /* Mixing C and C++ is ok, use C++ in that case. */
23365 common_lang = highest_c_language (common_lang,
23366 TRANSLATION_UNIT_LANGUAGE (t));
23367 else
23369 /* Fall back to C. */
23370 common_lang = NULL;
23371 break;
23375 if (common_lang)
23376 language_string = common_lang;
23379 language = DW_LANG_C;
23380 if (strncmp (language_string, "GNU C", 5) == 0
23381 && ISDIGIT (language_string[5]))
23383 language = DW_LANG_C89;
23384 if (dwarf_version >= 3 || !dwarf_strict)
23386 if (strcmp (language_string, "GNU C89") != 0)
23387 language = DW_LANG_C99;
23389 if (dwarf_version >= 5 /* || !dwarf_strict */)
23390 if (strcmp (language_string, "GNU C11") == 0)
23391 language = DW_LANG_C11;
23394 else if (strncmp (language_string, "GNU C++", 7) == 0)
23396 language = DW_LANG_C_plus_plus;
23397 if (dwarf_version >= 5 /* || !dwarf_strict */)
23399 if (strcmp (language_string, "GNU C++11") == 0)
23400 language = DW_LANG_C_plus_plus_11;
23401 else if (strcmp (language_string, "GNU C++14") == 0)
23402 language = DW_LANG_C_plus_plus_14;
23405 else if (strcmp (language_string, "GNU F77") == 0)
23406 language = DW_LANG_Fortran77;
23407 else if (strcmp (language_string, "GNU Pascal") == 0)
23408 language = DW_LANG_Pascal83;
23409 else if (dwarf_version >= 3 || !dwarf_strict)
23411 if (strcmp (language_string, "GNU Ada") == 0)
23412 language = DW_LANG_Ada95;
23413 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23415 language = DW_LANG_Fortran95;
23416 if (dwarf_version >= 5 /* || !dwarf_strict */)
23418 if (strcmp (language_string, "GNU Fortran2003") == 0)
23419 language = DW_LANG_Fortran03;
23420 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23421 language = DW_LANG_Fortran08;
23424 else if (strcmp (language_string, "GNU Java") == 0)
23425 language = DW_LANG_Java;
23426 else if (strcmp (language_string, "GNU Objective-C") == 0)
23427 language = DW_LANG_ObjC;
23428 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23429 language = DW_LANG_ObjC_plus_plus;
23430 else if (dwarf_version >= 5 || !dwarf_strict)
23432 if (strcmp (language_string, "GNU Go") == 0)
23433 language = DW_LANG_Go;
23436 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23437 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23438 language = DW_LANG_Fortran90;
23440 add_AT_unsigned (die, DW_AT_language, language);
23442 switch (language)
23444 case DW_LANG_Fortran77:
23445 case DW_LANG_Fortran90:
23446 case DW_LANG_Fortran95:
23447 case DW_LANG_Fortran03:
23448 case DW_LANG_Fortran08:
23449 /* Fortran has case insensitive identifiers and the front-end
23450 lowercases everything. */
23451 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23452 break;
23453 default:
23454 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23455 break;
23457 return die;
23460 /* Generate the DIE for a base class. */
23462 static void
23463 gen_inheritance_die (tree binfo, tree access, tree type,
23464 dw_die_ref context_die)
23466 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23467 struct vlr_context ctx = { type, NULL };
23469 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23470 context_die);
23471 add_data_member_location_attribute (die, binfo, &ctx);
23473 if (BINFO_VIRTUAL_P (binfo))
23474 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23476 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23477 children, otherwise the default is DW_ACCESS_public. In DWARF2
23478 the default has always been DW_ACCESS_private. */
23479 if (access == access_public_node)
23481 if (dwarf_version == 2
23482 || context_die->die_tag == DW_TAG_class_type)
23483 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23485 else if (access == access_protected_node)
23486 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23487 else if (dwarf_version > 2
23488 && context_die->die_tag != DW_TAG_class_type)
23489 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23492 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23493 structure. */
23494 static bool
23495 is_variant_part (tree decl)
23497 return (TREE_CODE (decl) == FIELD_DECL
23498 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23501 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23502 return the FIELD_DECL. Return NULL_TREE otherwise. */
23504 static tree
23505 analyze_discr_in_predicate (tree operand, tree struct_type)
23507 bool continue_stripping = true;
23508 while (continue_stripping)
23509 switch (TREE_CODE (operand))
23511 CASE_CONVERT:
23512 operand = TREE_OPERAND (operand, 0);
23513 break;
23514 default:
23515 continue_stripping = false;
23516 break;
23519 /* Match field access to members of struct_type only. */
23520 if (TREE_CODE (operand) == COMPONENT_REF
23521 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23522 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23523 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23524 return TREE_OPERAND (operand, 1);
23525 else
23526 return NULL_TREE;
23529 /* Check that SRC is a constant integer that can be represented as a native
23530 integer constant (either signed or unsigned). If so, store it into DEST and
23531 return true. Return false otherwise. */
23533 static bool
23534 get_discr_value (tree src, dw_discr_value *dest)
23536 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23538 if (TREE_CODE (src) != INTEGER_CST
23539 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23540 return false;
23542 dest->pos = is_unsigned;
23543 if (is_unsigned)
23544 dest->v.uval = tree_to_uhwi (src);
23545 else
23546 dest->v.sval = tree_to_shwi (src);
23548 return true;
23551 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23552 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23553 store NULL_TREE in DISCR_DECL. Otherwise:
23555 - store the discriminant field in STRUCT_TYPE that controls the variant
23556 part to *DISCR_DECL
23558 - put in *DISCR_LISTS_P an array where for each variant, the item
23559 represents the corresponding matching list of discriminant values.
23561 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23562 the above array.
23564 Note that when the array is allocated (i.e. when the analysis is
23565 successful), it is up to the caller to free the array. */
23567 static void
23568 analyze_variants_discr (tree variant_part_decl,
23569 tree struct_type,
23570 tree *discr_decl,
23571 dw_discr_list_ref **discr_lists_p,
23572 unsigned *discr_lists_length)
23574 tree variant_part_type = TREE_TYPE (variant_part_decl);
23575 tree variant;
23576 dw_discr_list_ref *discr_lists;
23577 unsigned i;
23579 /* Compute how many variants there are in this variant part. */
23580 *discr_lists_length = 0;
23581 for (variant = TYPE_FIELDS (variant_part_type);
23582 variant != NULL_TREE;
23583 variant = DECL_CHAIN (variant))
23584 ++*discr_lists_length;
23586 *discr_decl = NULL_TREE;
23587 *discr_lists_p
23588 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23589 sizeof (**discr_lists_p));
23590 discr_lists = *discr_lists_p;
23592 /* And then analyze all variants to extract discriminant information for all
23593 of them. This analysis is conservative: as soon as we detect something we
23594 do not support, abort everything and pretend we found nothing. */
23595 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23596 variant != NULL_TREE;
23597 variant = DECL_CHAIN (variant), ++i)
23599 tree match_expr = DECL_QUALIFIER (variant);
23601 /* Now, try to analyze the predicate and deduce a discriminant for
23602 it. */
23603 if (match_expr == boolean_true_node)
23604 /* Typically happens for the default variant: it matches all cases that
23605 previous variants rejected. Don't output any matching value for
23606 this one. */
23607 continue;
23609 /* The following loop tries to iterate over each discriminant
23610 possibility: single values or ranges. */
23611 while (match_expr != NULL_TREE)
23613 tree next_round_match_expr;
23614 tree candidate_discr = NULL_TREE;
23615 dw_discr_list_ref new_node = NULL;
23617 /* Possibilities are matched one after the other by nested
23618 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23619 continue with the rest at next iteration. */
23620 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23622 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23623 match_expr = TREE_OPERAND (match_expr, 1);
23625 else
23626 next_round_match_expr = NULL_TREE;
23628 if (match_expr == boolean_false_node)
23629 /* This sub-expression matches nothing: just wait for the next
23630 one. */
23633 else if (TREE_CODE (match_expr) == EQ_EXPR)
23635 /* We are matching: <discr_field> == <integer_cst>
23636 This sub-expression matches a single value. */
23637 tree integer_cst = TREE_OPERAND (match_expr, 1);
23639 candidate_discr
23640 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23641 struct_type);
23643 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23644 if (!get_discr_value (integer_cst,
23645 &new_node->dw_discr_lower_bound))
23646 goto abort;
23647 new_node->dw_discr_range = false;
23650 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23652 /* We are matching:
23653 <discr_field> > <integer_cst>
23654 && <discr_field> < <integer_cst>.
23655 This sub-expression matches the range of values between the
23656 two matched integer constants. Note that comparisons can be
23657 inclusive or exclusive. */
23658 tree candidate_discr_1, candidate_discr_2;
23659 tree lower_cst, upper_cst;
23660 bool lower_cst_included, upper_cst_included;
23661 tree lower_op = TREE_OPERAND (match_expr, 0);
23662 tree upper_op = TREE_OPERAND (match_expr, 1);
23664 /* When the comparison is exclusive, the integer constant is not
23665 the discriminant range bound we are looking for: we will have
23666 to increment or decrement it. */
23667 if (TREE_CODE (lower_op) == GE_EXPR)
23668 lower_cst_included = true;
23669 else if (TREE_CODE (lower_op) == GT_EXPR)
23670 lower_cst_included = false;
23671 else
23672 goto abort;
23674 if (TREE_CODE (upper_op) == LE_EXPR)
23675 upper_cst_included = true;
23676 else if (TREE_CODE (upper_op) == LT_EXPR)
23677 upper_cst_included = false;
23678 else
23679 goto abort;
23681 /* Extract the discriminant from the first operand and check it
23682 is consistant with the same analysis in the second
23683 operand. */
23684 candidate_discr_1
23685 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23686 struct_type);
23687 candidate_discr_2
23688 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23689 struct_type);
23690 if (candidate_discr_1 == candidate_discr_2)
23691 candidate_discr = candidate_discr_1;
23692 else
23693 goto abort;
23695 /* Extract bounds from both. */
23696 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23697 lower_cst = TREE_OPERAND (lower_op, 1);
23698 upper_cst = TREE_OPERAND (upper_op, 1);
23700 if (!lower_cst_included)
23701 lower_cst
23702 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
23703 lower_cst,
23704 build_int_cst (TREE_TYPE (lower_cst), 1)));
23705 if (!upper_cst_included)
23706 upper_cst
23707 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
23708 upper_cst,
23709 build_int_cst (TREE_TYPE (upper_cst), 1)));
23711 if (!get_discr_value (lower_cst,
23712 &new_node->dw_discr_lower_bound)
23713 || !get_discr_value (upper_cst,
23714 &new_node->dw_discr_upper_bound))
23715 goto abort;
23717 new_node->dw_discr_range = true;
23720 else
23721 /* Unsupported sub-expression: we cannot determine the set of
23722 matching discriminant values. Abort everything. */
23723 goto abort;
23725 /* If the discriminant info is not consistant with what we saw so
23726 far, consider the analysis failed and abort everything. */
23727 if (candidate_discr == NULL_TREE
23728 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23729 goto abort;
23730 else
23731 *discr_decl = candidate_discr;
23733 if (new_node != NULL)
23735 new_node->dw_discr_next = discr_lists[i];
23736 discr_lists[i] = new_node;
23738 match_expr = next_round_match_expr;
23742 /* If we reach this point, we could match everything we were interested
23743 in. */
23744 return;
23746 abort:
23747 /* Clean all data structure and return no result. */
23748 free (*discr_lists_p);
23749 *discr_lists_p = NULL;
23750 *discr_decl = NULL_TREE;
23753 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23754 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23755 under CONTEXT_DIE.
23757 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23758 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23759 this type, which are record types, represent the available variants and each
23760 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23761 values are inferred from these attributes.
23763 In trees, the offsets for the fields inside these sub-records are relative
23764 to the variant part itself, whereas the corresponding DIEs should have
23765 offset attributes that are relative to the embedding record base address.
23766 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23767 must be an expression that computes the offset of the variant part to
23768 describe in DWARF. */
23770 static void
23771 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23772 dw_die_ref context_die)
23774 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23775 tree variant_part_offset = vlr_ctx->variant_part_offset;
23776 struct loc_descr_context ctx = {
23777 vlr_ctx->struct_type, /* context_type */
23778 NULL_TREE, /* base_decl */
23779 NULL, /* dpi */
23780 false, /* placeholder_arg */
23781 false /* placeholder_seen */
23784 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23785 NULL_TREE if there is no such field. */
23786 tree discr_decl = NULL_TREE;
23787 dw_discr_list_ref *discr_lists;
23788 unsigned discr_lists_length = 0;
23789 unsigned i;
23791 dw_die_ref dwarf_proc_die = NULL;
23792 dw_die_ref variant_part_die
23793 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23795 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23797 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23798 &discr_decl, &discr_lists, &discr_lists_length);
23800 if (discr_decl != NULL_TREE)
23802 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23804 if (discr_die)
23805 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23806 else
23807 /* We have no DIE for the discriminant, so just discard all
23808 discrimimant information in the output. */
23809 discr_decl = NULL_TREE;
23812 /* If the offset for this variant part is more complex than a constant,
23813 create a DWARF procedure for it so that we will not have to generate DWARF
23814 expressions for it for each member. */
23815 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23816 && (dwarf_version >= 3 || !dwarf_strict))
23818 const tree dwarf_proc_fndecl
23819 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23820 build_function_type (TREE_TYPE (variant_part_offset),
23821 NULL_TREE));
23822 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23823 const dw_loc_descr_ref dwarf_proc_body
23824 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23826 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23827 dwarf_proc_fndecl, context_die);
23828 if (dwarf_proc_die != NULL)
23829 variant_part_offset = dwarf_proc_call;
23832 /* Output DIEs for all variants. */
23833 i = 0;
23834 for (tree variant = TYPE_FIELDS (variant_part_type);
23835 variant != NULL_TREE;
23836 variant = DECL_CHAIN (variant), ++i)
23838 tree variant_type = TREE_TYPE (variant);
23839 dw_die_ref variant_die;
23841 /* All variants (i.e. members of a variant part) are supposed to be
23842 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23843 under these records. */
23844 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23846 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23847 equate_decl_number_to_die (variant, variant_die);
23849 /* Output discriminant values this variant matches, if any. */
23850 if (discr_decl == NULL || discr_lists[i] == NULL)
23851 /* In the case we have discriminant information at all, this is
23852 probably the default variant: as the standard says, don't
23853 output any discriminant value/list attribute. */
23855 else if (discr_lists[i]->dw_discr_next == NULL
23856 && !discr_lists[i]->dw_discr_range)
23857 /* If there is only one accepted value, don't bother outputting a
23858 list. */
23859 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23860 else
23861 add_discr_list (variant_die, discr_lists[i]);
23863 for (tree member = TYPE_FIELDS (variant_type);
23864 member != NULL_TREE;
23865 member = DECL_CHAIN (member))
23867 struct vlr_context vlr_sub_ctx = {
23868 vlr_ctx->struct_type, /* struct_type */
23869 NULL /* variant_part_offset */
23871 if (is_variant_part (member))
23873 /* All offsets for fields inside variant parts are relative to
23874 the top-level embedding RECORD_TYPE's base address. On the
23875 other hand, offsets in GCC's types are relative to the
23876 nested-most variant part. So we have to sum offsets each time
23877 we recurse. */
23879 vlr_sub_ctx.variant_part_offset
23880 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
23881 variant_part_offset, byte_position (member)));
23882 gen_variant_part (member, &vlr_sub_ctx, variant_die);
23884 else
23886 vlr_sub_ctx.variant_part_offset = variant_part_offset;
23887 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
23892 free (discr_lists);
23895 /* Generate a DIE for a class member. */
23897 static void
23898 gen_member_die (tree type, dw_die_ref context_die)
23900 tree member;
23901 tree binfo = TYPE_BINFO (type);
23902 dw_die_ref child;
23904 /* If this is not an incomplete type, output descriptions of each of its
23905 members. Note that as we output the DIEs necessary to represent the
23906 members of this record or union type, we will also be trying to output
23907 DIEs to represent the *types* of those members. However the `type'
23908 function (above) will specifically avoid generating type DIEs for member
23909 types *within* the list of member DIEs for this (containing) type except
23910 for those types (of members) which are explicitly marked as also being
23911 members of this (containing) type themselves. The g++ front- end can
23912 force any given type to be treated as a member of some other (containing)
23913 type by setting the TYPE_CONTEXT of the given (member) type to point to
23914 the TREE node representing the appropriate (containing) type. */
23916 /* First output info about the base classes. */
23917 if (binfo)
23919 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
23920 int i;
23921 tree base;
23923 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
23924 gen_inheritance_die (base,
23925 (accesses ? (*accesses)[i] : access_public_node),
23926 type,
23927 context_die);
23930 /* Now output info about the data members and type members. */
23931 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
23933 struct vlr_context vlr_ctx = { type, NULL_TREE };
23935 /* If we thought we were generating minimal debug info for TYPE
23936 and then changed our minds, some of the member declarations
23937 may have already been defined. Don't define them again, but
23938 do put them in the right order. */
23940 child = lookup_decl_die (member);
23941 if (child)
23943 /* Handle inline static data members, which only have in-class
23944 declarations. */
23945 if (child->die_tag == DW_TAG_variable
23946 && child->die_parent == comp_unit_die ()
23947 && get_AT (child, DW_AT_specification) == NULL)
23949 reparent_child (child, context_die);
23950 child->die_tag = DW_TAG_member;
23952 else
23953 splice_child_die (context_die, child);
23956 /* Do not generate standard DWARF for variant parts if we are generating
23957 the corresponding GNAT encodings: DIEs generated for both would
23958 conflict in our mappings. */
23959 else if (is_variant_part (member)
23960 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
23962 vlr_ctx.variant_part_offset = byte_position (member);
23963 gen_variant_part (member, &vlr_ctx, context_die);
23965 else
23967 vlr_ctx.variant_part_offset = NULL_TREE;
23968 gen_decl_die (member, NULL, &vlr_ctx, context_die);
23971 /* For C++ inline static data members emit immediately a DW_TAG_variable
23972 DIE that will refer to that DW_TAG_member through
23973 DW_AT_specification. */
23974 if (TREE_STATIC (member)
23975 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
23976 != -1))
23978 int old_extern = DECL_EXTERNAL (member);
23979 DECL_EXTERNAL (member) = 0;
23980 gen_decl_die (member, NULL, NULL, comp_unit_die ());
23981 DECL_EXTERNAL (member) = old_extern;
23985 /* We do not keep type methods in type variants. */
23986 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
23987 /* Now output info about the function members (if any). */
23988 if (TYPE_METHODS (type) != error_mark_node)
23989 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
23991 /* Don't include clones in the member list. */
23992 if (DECL_ABSTRACT_ORIGIN (member))
23993 continue;
23994 /* Nor constructors for anonymous classes. */
23995 if (DECL_ARTIFICIAL (member)
23996 && dwarf2_name (member, 0) == NULL)
23997 continue;
23999 child = lookup_decl_die (member);
24000 if (child)
24001 splice_child_die (context_die, child);
24002 else
24003 gen_decl_die (member, NULL, NULL, context_die);
24007 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24008 is set, we pretend that the type was never defined, so we only get the
24009 member DIEs needed by later specification DIEs. */
24011 static void
24012 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24013 enum debug_info_usage usage)
24015 if (TREE_ASM_WRITTEN (type))
24017 /* Fill in the bound of variable-length fields in late dwarf if
24018 still incomplete. */
24019 if (!early_dwarf && variably_modified_type_p (type, NULL))
24020 for (tree member = TYPE_FIELDS (type);
24021 member;
24022 member = DECL_CHAIN (member))
24023 fill_variable_array_bounds (TREE_TYPE (member));
24024 return;
24027 dw_die_ref type_die = lookup_type_die (type);
24028 dw_die_ref scope_die = 0;
24029 int nested = 0;
24030 int complete = (TYPE_SIZE (type)
24031 && (! TYPE_STUB_DECL (type)
24032 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24033 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24034 complete = complete && should_emit_struct_debug (type, usage);
24036 if (type_die && ! complete)
24037 return;
24039 if (TYPE_CONTEXT (type) != NULL_TREE
24040 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24041 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24042 nested = 1;
24044 scope_die = scope_die_for (type, context_die);
24046 /* Generate child dies for template paramaters. */
24047 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24048 schedule_generic_params_dies_gen (type);
24050 if (! type_die || (nested && is_cu_die (scope_die)))
24051 /* First occurrence of type or toplevel definition of nested class. */
24053 dw_die_ref old_die = type_die;
24055 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24056 ? record_type_tag (type) : DW_TAG_union_type,
24057 scope_die, type);
24058 equate_type_number_to_die (type, type_die);
24059 if (old_die)
24060 add_AT_specification (type_die, old_die);
24061 else
24062 add_name_attribute (type_die, type_tag (type));
24064 else
24065 remove_AT (type_die, DW_AT_declaration);
24067 /* If this type has been completed, then give it a byte_size attribute and
24068 then give a list of members. */
24069 if (complete && !ns_decl)
24071 /* Prevent infinite recursion in cases where the type of some member of
24072 this type is expressed in terms of this type itself. */
24073 TREE_ASM_WRITTEN (type) = 1;
24074 add_byte_size_attribute (type_die, type);
24075 if (TYPE_STUB_DECL (type) != NULL_TREE)
24077 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24078 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24081 /* If the first reference to this type was as the return type of an
24082 inline function, then it may not have a parent. Fix this now. */
24083 if (type_die->die_parent == NULL)
24084 add_child_die (scope_die, type_die);
24086 push_decl_scope (type);
24087 gen_member_die (type, type_die);
24088 pop_decl_scope ();
24090 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24091 if (TYPE_ARTIFICIAL (type))
24092 add_AT_flag (type_die, DW_AT_artificial, 1);
24094 /* GNU extension: Record what type our vtable lives in. */
24095 if (TYPE_VFIELD (type))
24097 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24099 gen_type_die (vtype, context_die);
24100 add_AT_die_ref (type_die, DW_AT_containing_type,
24101 lookup_type_die (vtype));
24104 else
24106 add_AT_flag (type_die, DW_AT_declaration, 1);
24108 /* We don't need to do this for function-local types. */
24109 if (TYPE_STUB_DECL (type)
24110 && ! decl_function_context (TYPE_STUB_DECL (type)))
24111 vec_safe_push (incomplete_types, type);
24114 if (get_AT (type_die, DW_AT_name))
24115 add_pubtype (type, type_die);
24118 /* Generate a DIE for a subroutine _type_. */
24120 static void
24121 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24123 tree return_type = TREE_TYPE (type);
24124 dw_die_ref subr_die
24125 = new_die (DW_TAG_subroutine_type,
24126 scope_die_for (type, context_die), type);
24128 equate_type_number_to_die (type, subr_die);
24129 add_prototyped_attribute (subr_die, type);
24130 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24131 context_die);
24132 gen_formal_types_die (type, subr_die);
24134 if (get_AT (subr_die, DW_AT_name))
24135 add_pubtype (type, subr_die);
24136 if ((dwarf_version >= 5 || !dwarf_strict)
24137 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24138 add_AT_flag (subr_die, DW_AT_reference, 1);
24139 if ((dwarf_version >= 5 || !dwarf_strict)
24140 && lang_hooks.types.type_dwarf_attribute (type,
24141 DW_AT_rvalue_reference) != -1)
24142 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24145 /* Generate a DIE for a type definition. */
24147 static void
24148 gen_typedef_die (tree decl, dw_die_ref context_die)
24150 dw_die_ref type_die;
24151 tree origin;
24153 if (TREE_ASM_WRITTEN (decl))
24155 if (DECL_ORIGINAL_TYPE (decl))
24156 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24157 return;
24160 TREE_ASM_WRITTEN (decl) = 1;
24161 type_die = new_die (DW_TAG_typedef, context_die, decl);
24162 origin = decl_ultimate_origin (decl);
24163 if (origin != NULL)
24164 add_abstract_origin_attribute (type_die, origin);
24165 else
24167 tree type;
24169 add_name_and_src_coords_attributes (type_die, decl);
24170 if (DECL_ORIGINAL_TYPE (decl))
24172 type = DECL_ORIGINAL_TYPE (decl);
24174 if (type == error_mark_node)
24175 return;
24177 gcc_assert (type != TREE_TYPE (decl));
24178 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24180 else
24182 type = TREE_TYPE (decl);
24184 if (type == error_mark_node)
24185 return;
24187 if (is_naming_typedef_decl (TYPE_NAME (type)))
24189 /* Here, we are in the case of decl being a typedef naming
24190 an anonymous type, e.g:
24191 typedef struct {...} foo;
24192 In that case TREE_TYPE (decl) is not a typedef variant
24193 type and TYPE_NAME of the anonymous type is set to the
24194 TYPE_DECL of the typedef. This construct is emitted by
24195 the C++ FE.
24197 TYPE is the anonymous struct named by the typedef
24198 DECL. As we need the DW_AT_type attribute of the
24199 DW_TAG_typedef to point to the DIE of TYPE, let's
24200 generate that DIE right away. add_type_attribute
24201 called below will then pick (via lookup_type_die) that
24202 anonymous struct DIE. */
24203 if (!TREE_ASM_WRITTEN (type))
24204 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24206 /* This is a GNU Extension. We are adding a
24207 DW_AT_linkage_name attribute to the DIE of the
24208 anonymous struct TYPE. The value of that attribute
24209 is the name of the typedef decl naming the anonymous
24210 struct. This greatly eases the work of consumers of
24211 this debug info. */
24212 add_linkage_name_raw (lookup_type_die (type), decl);
24216 add_type_attribute (type_die, type, decl_quals (decl), false,
24217 context_die);
24219 if (is_naming_typedef_decl (decl))
24220 /* We want that all subsequent calls to lookup_type_die with
24221 TYPE in argument yield the DW_TAG_typedef we have just
24222 created. */
24223 equate_type_number_to_die (type, type_die);
24225 add_accessibility_attribute (type_die, decl);
24228 if (DECL_ABSTRACT_P (decl))
24229 equate_decl_number_to_die (decl, type_die);
24231 if (get_AT (type_die, DW_AT_name))
24232 add_pubtype (decl, type_die);
24235 /* Generate a DIE for a struct, class, enum or union type. */
24237 static void
24238 gen_tagged_type_die (tree type,
24239 dw_die_ref context_die,
24240 enum debug_info_usage usage)
24242 int need_pop;
24244 if (type == NULL_TREE
24245 || !is_tagged_type (type))
24246 return;
24248 if (TREE_ASM_WRITTEN (type))
24249 need_pop = 0;
24250 /* If this is a nested type whose containing class hasn't been written
24251 out yet, writing it out will cover this one, too. This does not apply
24252 to instantiations of member class templates; they need to be added to
24253 the containing class as they are generated. FIXME: This hurts the
24254 idea of combining type decls from multiple TUs, since we can't predict
24255 what set of template instantiations we'll get. */
24256 else if (TYPE_CONTEXT (type)
24257 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24258 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24260 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24262 if (TREE_ASM_WRITTEN (type))
24263 return;
24265 /* If that failed, attach ourselves to the stub. */
24266 push_decl_scope (TYPE_CONTEXT (type));
24267 context_die = lookup_type_die (TYPE_CONTEXT (type));
24268 need_pop = 1;
24270 else if (TYPE_CONTEXT (type) != NULL_TREE
24271 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24273 /* If this type is local to a function that hasn't been written
24274 out yet, use a NULL context for now; it will be fixed up in
24275 decls_for_scope. */
24276 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24277 /* A declaration DIE doesn't count; nested types need to go in the
24278 specification. */
24279 if (context_die && is_declaration_die (context_die))
24280 context_die = NULL;
24281 need_pop = 0;
24283 else
24285 context_die = declare_in_namespace (type, context_die);
24286 need_pop = 0;
24289 if (TREE_CODE (type) == ENUMERAL_TYPE)
24291 /* This might have been written out by the call to
24292 declare_in_namespace. */
24293 if (!TREE_ASM_WRITTEN (type))
24294 gen_enumeration_type_die (type, context_die);
24296 else
24297 gen_struct_or_union_type_die (type, context_die, usage);
24299 if (need_pop)
24300 pop_decl_scope ();
24302 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24303 it up if it is ever completed. gen_*_type_die will set it for us
24304 when appropriate. */
24307 /* Generate a type description DIE. */
24309 static void
24310 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24311 enum debug_info_usage usage)
24313 struct array_descr_info info;
24315 if (type == NULL_TREE || type == error_mark_node)
24316 return;
24318 if (flag_checking && type)
24319 verify_type (type);
24321 if (TYPE_NAME (type) != NULL_TREE
24322 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24323 && is_redundant_typedef (TYPE_NAME (type))
24324 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24325 /* The DECL of this type is a typedef we don't want to emit debug
24326 info for but we want debug info for its underlying typedef.
24327 This can happen for e.g, the injected-class-name of a C++
24328 type. */
24329 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24331 /* If TYPE is a typedef type variant, let's generate debug info
24332 for the parent typedef which TYPE is a type of. */
24333 if (typedef_variant_p (type))
24335 if (TREE_ASM_WRITTEN (type))
24336 return;
24338 /* Prevent broken recursion; we can't hand off to the same type. */
24339 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24341 /* Give typedefs the right scope. */
24342 context_die = scope_die_for (type, context_die);
24344 TREE_ASM_WRITTEN (type) = 1;
24346 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24347 return;
24350 /* If type is an anonymous tagged type named by a typedef, let's
24351 generate debug info for the typedef. */
24352 if (is_naming_typedef_decl (TYPE_NAME (type)))
24354 /* Use the DIE of the containing namespace as the parent DIE of
24355 the type description DIE we want to generate. */
24356 if (DECL_CONTEXT (TYPE_NAME (type))
24357 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24358 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24360 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24361 return;
24364 if (lang_hooks.types.get_debug_type)
24366 tree debug_type = lang_hooks.types.get_debug_type (type);
24368 if (debug_type != NULL_TREE && debug_type != type)
24370 gen_type_die_with_usage (debug_type, context_die, usage);
24371 return;
24375 /* We are going to output a DIE to represent the unqualified version
24376 of this type (i.e. without any const or volatile qualifiers) so
24377 get the main variant (i.e. the unqualified version) of this type
24378 now. (Vectors and arrays are special because the debugging info is in the
24379 cloned type itself. Similarly function/method types can contain extra
24380 ref-qualification). */
24381 if (TREE_CODE (type) == FUNCTION_TYPE
24382 || TREE_CODE (type) == METHOD_TYPE)
24384 /* For function/method types, can't use type_main_variant here,
24385 because that can have different ref-qualifiers for C++,
24386 but try to canonicalize. */
24387 tree main = TYPE_MAIN_VARIANT (type);
24388 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24389 if (check_base_type (t, main) && check_lang_type (t, type))
24390 type = t;
24392 else if (TREE_CODE (type) != VECTOR_TYPE
24393 && TREE_CODE (type) != ARRAY_TYPE)
24394 type = type_main_variant (type);
24396 /* If this is an array type with hidden descriptor, handle it first. */
24397 if (!TREE_ASM_WRITTEN (type)
24398 && lang_hooks.types.get_array_descr_info)
24400 memset (&info, 0, sizeof (info));
24401 if (lang_hooks.types.get_array_descr_info (type, &info))
24403 /* Fortran sometimes emits array types with no dimension. */
24404 gcc_assert (info.ndimensions >= 0
24405 && (info.ndimensions
24406 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24407 gen_descr_array_type_die (type, &info, context_die);
24408 TREE_ASM_WRITTEN (type) = 1;
24409 return;
24413 if (TREE_ASM_WRITTEN (type))
24415 /* Variable-length types may be incomplete even if
24416 TREE_ASM_WRITTEN. For such types, fall through to
24417 gen_array_type_die() and possibly fill in
24418 DW_AT_{upper,lower}_bound attributes. */
24419 if ((TREE_CODE (type) != ARRAY_TYPE
24420 && TREE_CODE (type) != RECORD_TYPE
24421 && TREE_CODE (type) != UNION_TYPE
24422 && TREE_CODE (type) != QUAL_UNION_TYPE)
24423 || !variably_modified_type_p (type, NULL))
24424 return;
24427 switch (TREE_CODE (type))
24429 case ERROR_MARK:
24430 break;
24432 case POINTER_TYPE:
24433 case REFERENCE_TYPE:
24434 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24435 ensures that the gen_type_die recursion will terminate even if the
24436 type is recursive. Recursive types are possible in Ada. */
24437 /* ??? We could perhaps do this for all types before the switch
24438 statement. */
24439 TREE_ASM_WRITTEN (type) = 1;
24441 /* For these types, all that is required is that we output a DIE (or a
24442 set of DIEs) to represent the "basis" type. */
24443 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24444 DINFO_USAGE_IND_USE);
24445 break;
24447 case OFFSET_TYPE:
24448 /* This code is used for C++ pointer-to-data-member types.
24449 Output a description of the relevant class type. */
24450 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24451 DINFO_USAGE_IND_USE);
24453 /* Output a description of the type of the object pointed to. */
24454 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24455 DINFO_USAGE_IND_USE);
24457 /* Now output a DIE to represent this pointer-to-data-member type
24458 itself. */
24459 gen_ptr_to_mbr_type_die (type, context_die);
24460 break;
24462 case FUNCTION_TYPE:
24463 /* Force out return type (in case it wasn't forced out already). */
24464 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24465 DINFO_USAGE_DIR_USE);
24466 gen_subroutine_type_die (type, context_die);
24467 break;
24469 case METHOD_TYPE:
24470 /* Force out return type (in case it wasn't forced out already). */
24471 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24472 DINFO_USAGE_DIR_USE);
24473 gen_subroutine_type_die (type, context_die);
24474 break;
24476 case ARRAY_TYPE:
24477 case VECTOR_TYPE:
24478 gen_array_type_die (type, context_die);
24479 break;
24481 case ENUMERAL_TYPE:
24482 case RECORD_TYPE:
24483 case UNION_TYPE:
24484 case QUAL_UNION_TYPE:
24485 gen_tagged_type_die (type, context_die, usage);
24486 return;
24488 case VOID_TYPE:
24489 case INTEGER_TYPE:
24490 case REAL_TYPE:
24491 case FIXED_POINT_TYPE:
24492 case COMPLEX_TYPE:
24493 case BOOLEAN_TYPE:
24494 case POINTER_BOUNDS_TYPE:
24495 /* No DIEs needed for fundamental types. */
24496 break;
24498 case NULLPTR_TYPE:
24499 case LANG_TYPE:
24500 /* Just use DW_TAG_unspecified_type. */
24502 dw_die_ref type_die = lookup_type_die (type);
24503 if (type_die == NULL)
24505 tree name = TYPE_IDENTIFIER (type);
24506 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24507 type);
24508 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24509 equate_type_number_to_die (type, type_die);
24512 break;
24514 default:
24515 if (is_cxx_auto (type))
24517 tree name = TYPE_IDENTIFIER (type);
24518 dw_die_ref *die = (name == get_identifier ("auto")
24519 ? &auto_die : &decltype_auto_die);
24520 if (!*die)
24522 *die = new_die (DW_TAG_unspecified_type,
24523 comp_unit_die (), NULL_TREE);
24524 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24526 equate_type_number_to_die (type, *die);
24527 break;
24529 gcc_unreachable ();
24532 TREE_ASM_WRITTEN (type) = 1;
24535 static void
24536 gen_type_die (tree type, dw_die_ref context_die)
24538 if (type != error_mark_node)
24540 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24541 if (flag_checking)
24543 dw_die_ref die = lookup_type_die (type);
24544 if (die)
24545 check_die (die);
24550 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24551 things which are local to the given block. */
24553 static void
24554 gen_block_die (tree stmt, dw_die_ref context_die)
24556 int must_output_die = 0;
24557 bool inlined_func;
24559 /* Ignore blocks that are NULL. */
24560 if (stmt == NULL_TREE)
24561 return;
24563 inlined_func = inlined_function_outer_scope_p (stmt);
24565 /* If the block is one fragment of a non-contiguous block, do not
24566 process the variables, since they will have been done by the
24567 origin block. Do process subblocks. */
24568 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24570 tree sub;
24572 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24573 gen_block_die (sub, context_die);
24575 return;
24578 /* Determine if we need to output any Dwarf DIEs at all to represent this
24579 block. */
24580 if (inlined_func)
24581 /* The outer scopes for inlinings *must* always be represented. We
24582 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24583 must_output_die = 1;
24584 else
24586 /* Determine if this block directly contains any "significant"
24587 local declarations which we will need to output DIEs for. */
24588 if (debug_info_level > DINFO_LEVEL_TERSE)
24589 /* We are not in terse mode so *any* local declaration counts
24590 as being a "significant" one. */
24591 must_output_die = ((BLOCK_VARS (stmt) != NULL
24592 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24593 && (TREE_USED (stmt)
24594 || TREE_ASM_WRITTEN (stmt)
24595 || BLOCK_ABSTRACT (stmt)));
24596 else if ((TREE_USED (stmt)
24597 || TREE_ASM_WRITTEN (stmt)
24598 || BLOCK_ABSTRACT (stmt))
24599 && !dwarf2out_ignore_block (stmt))
24600 must_output_die = 1;
24603 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24604 DIE for any block which contains no significant local declarations at
24605 all. Rather, in such cases we just call `decls_for_scope' so that any
24606 needed Dwarf info for any sub-blocks will get properly generated. Note
24607 that in terse mode, our definition of what constitutes a "significant"
24608 local declaration gets restricted to include only inlined function
24609 instances and local (nested) function definitions. */
24610 if (must_output_die)
24612 if (inlined_func)
24614 /* If STMT block is abstract, that means we have been called
24615 indirectly from dwarf2out_abstract_function.
24616 That function rightfully marks the descendent blocks (of
24617 the abstract function it is dealing with) as being abstract,
24618 precisely to prevent us from emitting any
24619 DW_TAG_inlined_subroutine DIE as a descendent
24620 of an abstract function instance. So in that case, we should
24621 not call gen_inlined_subroutine_die.
24623 Later though, when cgraph asks dwarf2out to emit info
24624 for the concrete instance of the function decl into which
24625 the concrete instance of STMT got inlined, the later will lead
24626 to the generation of a DW_TAG_inlined_subroutine DIE. */
24627 if (! BLOCK_ABSTRACT (stmt))
24628 gen_inlined_subroutine_die (stmt, context_die);
24630 else
24631 gen_lexical_block_die (stmt, context_die);
24633 else
24634 decls_for_scope (stmt, context_die);
24637 /* Process variable DECL (or variable with origin ORIGIN) within
24638 block STMT and add it to CONTEXT_DIE. */
24639 static void
24640 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24642 dw_die_ref die;
24643 tree decl_or_origin = decl ? decl : origin;
24645 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24646 die = lookup_decl_die (decl_or_origin);
24647 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24649 if (TYPE_DECL_IS_STUB (decl_or_origin))
24650 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24651 else
24652 die = lookup_decl_die (decl_or_origin);
24653 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24654 if (! die && ! early_dwarf)
24655 return;
24657 else
24658 die = NULL;
24660 if (die != NULL && die->die_parent == NULL)
24661 add_child_die (context_die, die);
24662 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24664 if (early_dwarf)
24665 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24666 stmt, context_die);
24668 else
24669 gen_decl_die (decl, origin, NULL, context_die);
24672 /* Generate all of the decls declared within a given scope and (recursively)
24673 all of its sub-blocks. */
24675 static void
24676 decls_for_scope (tree stmt, dw_die_ref context_die)
24678 tree decl;
24679 unsigned int i;
24680 tree subblocks;
24682 /* Ignore NULL blocks. */
24683 if (stmt == NULL_TREE)
24684 return;
24686 /* Output the DIEs to represent all of the data objects and typedefs
24687 declared directly within this block but not within any nested
24688 sub-blocks. Also, nested function and tag DIEs have been
24689 generated with a parent of NULL; fix that up now. We don't
24690 have to do this if we're at -g1. */
24691 if (debug_info_level > DINFO_LEVEL_TERSE)
24693 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24694 process_scope_var (stmt, decl, NULL_TREE, context_die);
24695 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24696 origin - avoid doing this twice as we have no good way to see
24697 if we've done it once already. */
24698 if (! early_dwarf)
24699 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24700 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
24701 context_die);
24704 /* Even if we're at -g1, we need to process the subblocks in order to get
24705 inlined call information. */
24707 /* Output the DIEs to represent all sub-blocks (and the items declared
24708 therein) of this block. */
24709 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24710 subblocks != NULL;
24711 subblocks = BLOCK_CHAIN (subblocks))
24712 gen_block_die (subblocks, context_die);
24715 /* Is this a typedef we can avoid emitting? */
24717 bool
24718 is_redundant_typedef (const_tree decl)
24720 if (TYPE_DECL_IS_STUB (decl))
24721 return true;
24723 if (DECL_ARTIFICIAL (decl)
24724 && DECL_CONTEXT (decl)
24725 && is_tagged_type (DECL_CONTEXT (decl))
24726 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24727 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24728 /* Also ignore the artificial member typedef for the class name. */
24729 return true;
24731 return false;
24734 /* Return TRUE if TYPE is a typedef that names a type for linkage
24735 purposes. This kind of typedefs is produced by the C++ FE for
24736 constructs like:
24738 typedef struct {...} foo;
24740 In that case, there is no typedef variant type produced for foo.
24741 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24742 struct type. */
24744 static bool
24745 is_naming_typedef_decl (const_tree decl)
24747 if (decl == NULL_TREE
24748 || TREE_CODE (decl) != TYPE_DECL
24749 || DECL_NAMELESS (decl)
24750 || !is_tagged_type (TREE_TYPE (decl))
24751 || DECL_IS_BUILTIN (decl)
24752 || is_redundant_typedef (decl)
24753 /* It looks like Ada produces TYPE_DECLs that are very similar
24754 to C++ naming typedefs but that have different
24755 semantics. Let's be specific to c++ for now. */
24756 || !is_cxx ())
24757 return FALSE;
24759 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24760 && TYPE_NAME (TREE_TYPE (decl)) == decl
24761 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24762 != TYPE_NAME (TREE_TYPE (decl))));
24765 /* Looks up the DIE for a context. */
24767 static inline dw_die_ref
24768 lookup_context_die (tree context)
24770 if (context)
24772 /* Find die that represents this context. */
24773 if (TYPE_P (context))
24775 context = TYPE_MAIN_VARIANT (context);
24776 dw_die_ref ctx = lookup_type_die (context);
24777 if (!ctx)
24778 return NULL;
24779 return strip_naming_typedef (context, ctx);
24781 else
24782 return lookup_decl_die (context);
24784 return comp_unit_die ();
24787 /* Returns the DIE for a context. */
24789 static inline dw_die_ref
24790 get_context_die (tree context)
24792 if (context)
24794 /* Find die that represents this context. */
24795 if (TYPE_P (context))
24797 context = TYPE_MAIN_VARIANT (context);
24798 return strip_naming_typedef (context, force_type_die (context));
24800 else
24801 return force_decl_die (context);
24803 return comp_unit_die ();
24806 /* Returns the DIE for decl. A DIE will always be returned. */
24808 static dw_die_ref
24809 force_decl_die (tree decl)
24811 dw_die_ref decl_die;
24812 unsigned saved_external_flag;
24813 tree save_fn = NULL_TREE;
24814 decl_die = lookup_decl_die (decl);
24815 if (!decl_die)
24817 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24819 decl_die = lookup_decl_die (decl);
24820 if (decl_die)
24821 return decl_die;
24823 switch (TREE_CODE (decl))
24825 case FUNCTION_DECL:
24826 /* Clear current_function_decl, so that gen_subprogram_die thinks
24827 that this is a declaration. At this point, we just want to force
24828 declaration die. */
24829 save_fn = current_function_decl;
24830 current_function_decl = NULL_TREE;
24831 gen_subprogram_die (decl, context_die);
24832 current_function_decl = save_fn;
24833 break;
24835 case VAR_DECL:
24836 /* Set external flag to force declaration die. Restore it after
24837 gen_decl_die() call. */
24838 saved_external_flag = DECL_EXTERNAL (decl);
24839 DECL_EXTERNAL (decl) = 1;
24840 gen_decl_die (decl, NULL, NULL, context_die);
24841 DECL_EXTERNAL (decl) = saved_external_flag;
24842 break;
24844 case NAMESPACE_DECL:
24845 if (dwarf_version >= 3 || !dwarf_strict)
24846 dwarf2out_decl (decl);
24847 else
24848 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
24849 decl_die = comp_unit_die ();
24850 break;
24852 case TRANSLATION_UNIT_DECL:
24853 decl_die = comp_unit_die ();
24854 break;
24856 default:
24857 gcc_unreachable ();
24860 /* We should be able to find the DIE now. */
24861 if (!decl_die)
24862 decl_die = lookup_decl_die (decl);
24863 gcc_assert (decl_die);
24866 return decl_die;
24869 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
24870 always returned. */
24872 static dw_die_ref
24873 force_type_die (tree type)
24875 dw_die_ref type_die;
24877 type_die = lookup_type_die (type);
24878 if (!type_die)
24880 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
24882 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
24883 false, context_die);
24884 gcc_assert (type_die);
24886 return type_die;
24889 /* Force out any required namespaces to be able to output DECL,
24890 and return the new context_die for it, if it's changed. */
24892 static dw_die_ref
24893 setup_namespace_context (tree thing, dw_die_ref context_die)
24895 tree context = (DECL_P (thing)
24896 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
24897 if (context && TREE_CODE (context) == NAMESPACE_DECL)
24898 /* Force out the namespace. */
24899 context_die = force_decl_die (context);
24901 return context_die;
24904 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
24905 type) within its namespace, if appropriate.
24907 For compatibility with older debuggers, namespace DIEs only contain
24908 declarations; all definitions are emitted at CU scope, with
24909 DW_AT_specification pointing to the declaration (like with class
24910 members). */
24912 static dw_die_ref
24913 declare_in_namespace (tree thing, dw_die_ref context_die)
24915 dw_die_ref ns_context;
24917 if (debug_info_level <= DINFO_LEVEL_TERSE)
24918 return context_die;
24920 /* External declarations in the local scope only need to be emitted
24921 once, not once in the namespace and once in the scope.
24923 This avoids declaring the `extern' below in the
24924 namespace DIE as well as in the innermost scope:
24926 namespace S
24928 int i=5;
24929 int foo()
24931 int i=8;
24932 extern int i;
24933 return i;
24937 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
24938 return context_die;
24940 /* If this decl is from an inlined function, then don't try to emit it in its
24941 namespace, as we will get confused. It would have already been emitted
24942 when the abstract instance of the inline function was emitted anyways. */
24943 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
24944 return context_die;
24946 ns_context = setup_namespace_context (thing, context_die);
24948 if (ns_context != context_die)
24950 if (is_fortran ())
24951 return ns_context;
24952 if (DECL_P (thing))
24953 gen_decl_die (thing, NULL, NULL, ns_context);
24954 else
24955 gen_type_die (thing, ns_context);
24957 return context_die;
24960 /* Generate a DIE for a namespace or namespace alias. */
24962 static void
24963 gen_namespace_die (tree decl, dw_die_ref context_die)
24965 dw_die_ref namespace_die;
24967 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
24968 they are an alias of. */
24969 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
24971 /* Output a real namespace or module. */
24972 context_die = setup_namespace_context (decl, comp_unit_die ());
24973 namespace_die = new_die (is_fortran ()
24974 ? DW_TAG_module : DW_TAG_namespace,
24975 context_die, decl);
24976 /* For Fortran modules defined in different CU don't add src coords. */
24977 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
24979 const char *name = dwarf2_name (decl, 0);
24980 if (name)
24981 add_name_attribute (namespace_die, name);
24983 else
24984 add_name_and_src_coords_attributes (namespace_die, decl);
24985 if (DECL_EXTERNAL (decl))
24986 add_AT_flag (namespace_die, DW_AT_declaration, 1);
24987 equate_decl_number_to_die (decl, namespace_die);
24989 else
24991 /* Output a namespace alias. */
24993 /* Force out the namespace we are an alias of, if necessary. */
24994 dw_die_ref origin_die
24995 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
24997 if (DECL_FILE_SCOPE_P (decl)
24998 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
24999 context_die = setup_namespace_context (decl, comp_unit_die ());
25000 /* Now create the namespace alias DIE. */
25001 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25002 add_name_and_src_coords_attributes (namespace_die, decl);
25003 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25004 equate_decl_number_to_die (decl, namespace_die);
25006 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25007 if (want_pubnames ())
25008 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25011 /* Generate Dwarf debug information for a decl described by DECL.
25012 The return value is currently only meaningful for PARM_DECLs,
25013 for all other decls it returns NULL.
25015 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25016 It can be NULL otherwise. */
25018 static dw_die_ref
25019 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25020 dw_die_ref context_die)
25022 tree decl_or_origin = decl ? decl : origin;
25023 tree class_origin = NULL, ultimate_origin;
25025 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25026 return NULL;
25028 /* Ignore pointer bounds decls. */
25029 if (DECL_P (decl_or_origin)
25030 && TREE_TYPE (decl_or_origin)
25031 && POINTER_BOUNDS_P (decl_or_origin))
25032 return NULL;
25034 switch (TREE_CODE (decl_or_origin))
25036 case ERROR_MARK:
25037 break;
25039 case CONST_DECL:
25040 if (!is_fortran () && !is_ada ())
25042 /* The individual enumerators of an enum type get output when we output
25043 the Dwarf representation of the relevant enum type itself. */
25044 break;
25047 /* Emit its type. */
25048 gen_type_die (TREE_TYPE (decl), context_die);
25050 /* And its containing namespace. */
25051 context_die = declare_in_namespace (decl, context_die);
25053 gen_const_die (decl, context_die);
25054 break;
25056 case FUNCTION_DECL:
25057 /* Don't output any DIEs to represent mere function declarations,
25058 unless they are class members or explicit block externs. */
25059 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25060 && DECL_FILE_SCOPE_P (decl_or_origin)
25061 && (current_function_decl == NULL_TREE
25062 || DECL_ARTIFICIAL (decl_or_origin)))
25063 break;
25065 #if 0
25066 /* FIXME */
25067 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25068 on local redeclarations of global functions. That seems broken. */
25069 if (current_function_decl != decl)
25070 /* This is only a declaration. */;
25071 #endif
25073 /* If we're emitting a clone, emit info for the abstract instance. */
25074 if (origin || DECL_ORIGIN (decl) != decl)
25075 dwarf2out_abstract_function (origin
25076 ? DECL_ORIGIN (origin)
25077 : DECL_ABSTRACT_ORIGIN (decl));
25079 /* If we're emitting an out-of-line copy of an inline function,
25080 emit info for the abstract instance and set up to refer to it. */
25081 else if (cgraph_function_possibly_inlined_p (decl)
25082 && ! DECL_ABSTRACT_P (decl)
25083 && ! class_or_namespace_scope_p (context_die)
25084 /* dwarf2out_abstract_function won't emit a die if this is just
25085 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25086 that case, because that works only if we have a die. */
25087 && DECL_INITIAL (decl) != NULL_TREE)
25089 dwarf2out_abstract_function (decl);
25090 set_decl_origin_self (decl);
25093 /* Otherwise we're emitting the primary DIE for this decl. */
25094 else if (debug_info_level > DINFO_LEVEL_TERSE)
25096 /* Before we describe the FUNCTION_DECL itself, make sure that we
25097 have its containing type. */
25098 if (!origin)
25099 origin = decl_class_context (decl);
25100 if (origin != NULL_TREE)
25101 gen_type_die (origin, context_die);
25103 /* And its return type. */
25104 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25106 /* And its virtual context. */
25107 if (DECL_VINDEX (decl) != NULL_TREE)
25108 gen_type_die (DECL_CONTEXT (decl), context_die);
25110 /* Make sure we have a member DIE for decl. */
25111 if (origin != NULL_TREE)
25112 gen_type_die_for_member (origin, decl, context_die);
25114 /* And its containing namespace. */
25115 context_die = declare_in_namespace (decl, context_die);
25118 /* Now output a DIE to represent the function itself. */
25119 if (decl)
25120 gen_subprogram_die (decl, context_die);
25121 break;
25123 case TYPE_DECL:
25124 /* If we are in terse mode, don't generate any DIEs to represent any
25125 actual typedefs. */
25126 if (debug_info_level <= DINFO_LEVEL_TERSE)
25127 break;
25129 /* In the special case of a TYPE_DECL node representing the declaration
25130 of some type tag, if the given TYPE_DECL is marked as having been
25131 instantiated from some other (original) TYPE_DECL node (e.g. one which
25132 was generated within the original definition of an inline function) we
25133 used to generate a special (abbreviated) DW_TAG_structure_type,
25134 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25135 should be actually referencing those DIEs, as variable DIEs with that
25136 type would be emitted already in the abstract origin, so it was always
25137 removed during unused type prunning. Don't add anything in this
25138 case. */
25139 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25140 break;
25142 if (is_redundant_typedef (decl))
25143 gen_type_die (TREE_TYPE (decl), context_die);
25144 else
25145 /* Output a DIE to represent the typedef itself. */
25146 gen_typedef_die (decl, context_die);
25147 break;
25149 case LABEL_DECL:
25150 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25151 gen_label_die (decl, context_die);
25152 break;
25154 case VAR_DECL:
25155 case RESULT_DECL:
25156 /* If we are in terse mode, don't generate any DIEs to represent any
25157 variable declarations or definitions. */
25158 if (debug_info_level <= DINFO_LEVEL_TERSE)
25159 break;
25161 /* Output any DIEs that are needed to specify the type of this data
25162 object. */
25163 if (decl_by_reference_p (decl_or_origin))
25164 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25165 else
25166 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25168 /* And its containing type. */
25169 class_origin = decl_class_context (decl_or_origin);
25170 if (class_origin != NULL_TREE)
25171 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25173 /* And its containing namespace. */
25174 context_die = declare_in_namespace (decl_or_origin, context_die);
25176 /* Now output the DIE to represent the data object itself. This gets
25177 complicated because of the possibility that the VAR_DECL really
25178 represents an inlined instance of a formal parameter for an inline
25179 function. */
25180 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25181 if (ultimate_origin != NULL_TREE
25182 && TREE_CODE (ultimate_origin) == PARM_DECL)
25183 gen_formal_parameter_die (decl, origin,
25184 true /* Emit name attribute. */,
25185 context_die);
25186 else
25187 gen_variable_die (decl, origin, context_die);
25188 break;
25190 case FIELD_DECL:
25191 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25192 /* Ignore the nameless fields that are used to skip bits but handle C++
25193 anonymous unions and structs. */
25194 if (DECL_NAME (decl) != NULL_TREE
25195 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25196 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25198 gen_type_die (member_declared_type (decl), context_die);
25199 gen_field_die (decl, ctx, context_die);
25201 break;
25203 case PARM_DECL:
25204 if (DECL_BY_REFERENCE (decl_or_origin))
25205 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25206 else
25207 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25208 return gen_formal_parameter_die (decl, origin,
25209 true /* Emit name attribute. */,
25210 context_die);
25212 case NAMESPACE_DECL:
25213 if (dwarf_version >= 3 || !dwarf_strict)
25214 gen_namespace_die (decl, context_die);
25215 break;
25217 case IMPORTED_DECL:
25218 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25219 DECL_CONTEXT (decl), context_die);
25220 break;
25222 case NAMELIST_DECL:
25223 gen_namelist_decl (DECL_NAME (decl), context_die,
25224 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25225 break;
25227 default:
25228 /* Probably some frontend-internal decl. Assume we don't care. */
25229 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25230 break;
25233 return NULL;
25236 /* Output initial debug information for global DECL. Called at the
25237 end of the parsing process.
25239 This is the initial debug generation process. As such, the DIEs
25240 generated may be incomplete. A later debug generation pass
25241 (dwarf2out_late_global_decl) will augment the information generated
25242 in this pass (e.g., with complete location info). */
25244 static void
25245 dwarf2out_early_global_decl (tree decl)
25247 set_early_dwarf s;
25249 /* gen_decl_die() will set DECL_ABSTRACT because
25250 cgraph_function_possibly_inlined_p() returns true. This is in
25251 turn will cause DW_AT_inline attributes to be set.
25253 This happens because at early dwarf generation, there is no
25254 cgraph information, causing cgraph_function_possibly_inlined_p()
25255 to return true. Trick cgraph_function_possibly_inlined_p()
25256 while we generate dwarf early. */
25257 bool save = symtab->global_info_ready;
25258 symtab->global_info_ready = true;
25260 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25261 other DECLs and they can point to template types or other things
25262 that dwarf2out can't handle when done via dwarf2out_decl. */
25263 if (TREE_CODE (decl) != TYPE_DECL
25264 && TREE_CODE (decl) != PARM_DECL)
25266 tree save_fndecl = current_function_decl;
25267 if (TREE_CODE (decl) == FUNCTION_DECL)
25269 /* No cfun means the symbol has no body, so there's nothing
25270 to emit. */
25271 if (!DECL_STRUCT_FUNCTION (decl))
25272 goto early_decl_exit;
25274 /* For nested functions, make sure we have DIEs for the parents first
25275 so that all nested DIEs are generated at the proper scope in the
25276 first shot. */
25277 tree context = decl_function_context (decl);
25278 if (context != NULL && lookup_decl_die (context) == NULL)
25280 current_function_decl = context;
25281 dwarf2out_decl (context);
25284 current_function_decl = decl;
25286 dwarf2out_decl (decl);
25287 if (TREE_CODE (decl) == FUNCTION_DECL)
25288 current_function_decl = save_fndecl;
25290 early_decl_exit:
25291 symtab->global_info_ready = save;
25294 /* Output debug information for global decl DECL. Called from
25295 toplev.c after compilation proper has finished. */
25297 static void
25298 dwarf2out_late_global_decl (tree decl)
25300 /* Fill-in any location information we were unable to determine
25301 on the first pass. */
25302 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25304 dw_die_ref die = lookup_decl_die (decl);
25306 /* We have to generate early debug late for LTO. */
25307 if (! die && in_lto_p)
25309 dwarf2out_decl (decl);
25310 die = lookup_decl_die (decl);
25313 if (die)
25315 /* We get called via the symtab code invoking late_global_decl
25316 for symbols that are optimized out. Do not add locations
25317 for those. */
25318 varpool_node *node = varpool_node::get (decl);
25319 if (! node || ! node->definition)
25320 tree_add_const_value_attribute_for_decl (die, decl);
25321 else
25322 add_location_or_const_value_attribute (die, decl, false);
25327 /* Output debug information for type decl DECL. Called from toplev.c
25328 and from language front ends (to record built-in types). */
25329 static void
25330 dwarf2out_type_decl (tree decl, int local)
25332 if (!local)
25334 set_early_dwarf s;
25335 dwarf2out_decl (decl);
25339 /* Output debug information for imported module or decl DECL.
25340 NAME is non-NULL name in the lexical block if the decl has been renamed.
25341 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25342 that DECL belongs to.
25343 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25344 static void
25345 dwarf2out_imported_module_or_decl_1 (tree decl,
25346 tree name,
25347 tree lexical_block,
25348 dw_die_ref lexical_block_die)
25350 expanded_location xloc;
25351 dw_die_ref imported_die = NULL;
25352 dw_die_ref at_import_die;
25354 if (TREE_CODE (decl) == IMPORTED_DECL)
25356 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25357 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25358 gcc_assert (decl);
25360 else
25361 xloc = expand_location (input_location);
25363 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25365 at_import_die = force_type_die (TREE_TYPE (decl));
25366 /* For namespace N { typedef void T; } using N::T; base_type_die
25367 returns NULL, but DW_TAG_imported_declaration requires
25368 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25369 if (!at_import_die)
25371 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25372 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25373 at_import_die = lookup_type_die (TREE_TYPE (decl));
25374 gcc_assert (at_import_die);
25377 else
25379 at_import_die = lookup_decl_die (decl);
25380 if (!at_import_die)
25382 /* If we're trying to avoid duplicate debug info, we may not have
25383 emitted the member decl for this field. Emit it now. */
25384 if (TREE_CODE (decl) == FIELD_DECL)
25386 tree type = DECL_CONTEXT (decl);
25388 if (TYPE_CONTEXT (type)
25389 && TYPE_P (TYPE_CONTEXT (type))
25390 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25391 DINFO_USAGE_DIR_USE))
25392 return;
25393 gen_type_die_for_member (type, decl,
25394 get_context_die (TYPE_CONTEXT (type)));
25396 if (TREE_CODE (decl) == NAMELIST_DECL)
25397 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25398 get_context_die (DECL_CONTEXT (decl)),
25399 NULL_TREE);
25400 else
25401 at_import_die = force_decl_die (decl);
25405 if (TREE_CODE (decl) == NAMESPACE_DECL)
25407 if (dwarf_version >= 3 || !dwarf_strict)
25408 imported_die = new_die (DW_TAG_imported_module,
25409 lexical_block_die,
25410 lexical_block);
25411 else
25412 return;
25414 else
25415 imported_die = new_die (DW_TAG_imported_declaration,
25416 lexical_block_die,
25417 lexical_block);
25419 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25420 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25421 if (name)
25422 add_AT_string (imported_die, DW_AT_name,
25423 IDENTIFIER_POINTER (name));
25424 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25427 /* Output debug information for imported module or decl DECL.
25428 NAME is non-NULL name in context if the decl has been renamed.
25429 CHILD is true if decl is one of the renamed decls as part of
25430 importing whole module. */
25432 static void
25433 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25434 bool child)
25436 /* dw_die_ref at_import_die; */
25437 dw_die_ref scope_die;
25439 if (debug_info_level <= DINFO_LEVEL_TERSE)
25440 return;
25442 gcc_assert (decl);
25444 set_early_dwarf s;
25446 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25447 We need decl DIE for reference and scope die. First, get DIE for the decl
25448 itself. */
25450 /* Get the scope die for decl context. Use comp_unit_die for global module
25451 or decl. If die is not found for non globals, force new die. */
25452 if (context
25453 && TYPE_P (context)
25454 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25455 return;
25457 scope_die = get_context_die (context);
25459 if (child)
25461 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25462 there is nothing we can do, here. */
25463 if (dwarf_version < 3 && dwarf_strict)
25464 return;
25466 gcc_assert (scope_die->die_child);
25467 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25468 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25469 scope_die = scope_die->die_child;
25472 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25473 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25476 /* Output debug information for namelists. */
25478 static dw_die_ref
25479 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25481 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25482 tree value;
25483 unsigned i;
25485 if (debug_info_level <= DINFO_LEVEL_TERSE)
25486 return NULL;
25488 gcc_assert (scope_die != NULL);
25489 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25490 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25492 /* If there are no item_decls, we have a nondefining namelist, e.g.
25493 with USE association; hence, set DW_AT_declaration. */
25494 if (item_decls == NULL_TREE)
25496 add_AT_flag (nml_die, DW_AT_declaration, 1);
25497 return nml_die;
25500 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25502 nml_item_ref_die = lookup_decl_die (value);
25503 if (!nml_item_ref_die)
25504 nml_item_ref_die = force_decl_die (value);
25506 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25507 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25509 return nml_die;
25513 /* Write the debugging output for DECL and return the DIE. */
25515 static void
25516 dwarf2out_decl (tree decl)
25518 dw_die_ref context_die = comp_unit_die ();
25520 switch (TREE_CODE (decl))
25522 case ERROR_MARK:
25523 return;
25525 case FUNCTION_DECL:
25526 /* What we would really like to do here is to filter out all mere
25527 file-scope declarations of file-scope functions which are never
25528 referenced later within this translation unit (and keep all of ones
25529 that *are* referenced later on) but we aren't clairvoyant, so we have
25530 no idea which functions will be referenced in the future (i.e. later
25531 on within the current translation unit). So here we just ignore all
25532 file-scope function declarations which are not also definitions. If
25533 and when the debugger needs to know something about these functions,
25534 it will have to hunt around and find the DWARF information associated
25535 with the definition of the function.
25537 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25538 nodes represent definitions and which ones represent mere
25539 declarations. We have to check DECL_INITIAL instead. That's because
25540 the C front-end supports some weird semantics for "extern inline"
25541 function definitions. These can get inlined within the current
25542 translation unit (and thus, we need to generate Dwarf info for their
25543 abstract instances so that the Dwarf info for the concrete inlined
25544 instances can have something to refer to) but the compiler never
25545 generates any out-of-lines instances of such things (despite the fact
25546 that they *are* definitions).
25548 The important point is that the C front-end marks these "extern
25549 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25550 them anyway. Note that the C++ front-end also plays some similar games
25551 for inline function definitions appearing within include files which
25552 also contain `#pragma interface' pragmas.
25554 If we are called from dwarf2out_abstract_function output a DIE
25555 anyway. We can end up here this way with early inlining and LTO
25556 where the inlined function is output in a different LTRANS unit
25557 or not at all. */
25558 if (DECL_INITIAL (decl) == NULL_TREE
25559 && ! DECL_ABSTRACT_P (decl))
25560 return;
25562 /* If we're a nested function, initially use a parent of NULL; if we're
25563 a plain function, this will be fixed up in decls_for_scope. If
25564 we're a method, it will be ignored, since we already have a DIE. */
25565 if (decl_function_context (decl)
25566 /* But if we're in terse mode, we don't care about scope. */
25567 && debug_info_level > DINFO_LEVEL_TERSE)
25568 context_die = NULL;
25569 break;
25571 case VAR_DECL:
25572 /* For local statics lookup proper context die. */
25573 if (local_function_static (decl))
25574 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25576 /* If we are in terse mode, don't generate any DIEs to represent any
25577 variable declarations or definitions. */
25578 if (debug_info_level <= DINFO_LEVEL_TERSE)
25579 return;
25580 break;
25582 case CONST_DECL:
25583 if (debug_info_level <= DINFO_LEVEL_TERSE)
25584 return;
25585 if (!is_fortran () && !is_ada ())
25586 return;
25587 if (TREE_STATIC (decl) && decl_function_context (decl))
25588 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25589 break;
25591 case NAMESPACE_DECL:
25592 case IMPORTED_DECL:
25593 if (debug_info_level <= DINFO_LEVEL_TERSE)
25594 return;
25595 if (lookup_decl_die (decl) != NULL)
25596 return;
25597 break;
25599 case TYPE_DECL:
25600 /* Don't emit stubs for types unless they are needed by other DIEs. */
25601 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25602 return;
25604 /* Don't bother trying to generate any DIEs to represent any of the
25605 normal built-in types for the language we are compiling. */
25606 if (DECL_IS_BUILTIN (decl))
25607 return;
25609 /* If we are in terse mode, don't generate any DIEs for types. */
25610 if (debug_info_level <= DINFO_LEVEL_TERSE)
25611 return;
25613 /* If we're a function-scope tag, initially use a parent of NULL;
25614 this will be fixed up in decls_for_scope. */
25615 if (decl_function_context (decl))
25616 context_die = NULL;
25618 break;
25620 case NAMELIST_DECL:
25621 break;
25623 default:
25624 return;
25627 gen_decl_die (decl, NULL, NULL, context_die);
25629 if (flag_checking)
25631 dw_die_ref die = lookup_decl_die (decl);
25632 if (die)
25633 check_die (die);
25637 /* Write the debugging output for DECL. */
25639 static void
25640 dwarf2out_function_decl (tree decl)
25642 dwarf2out_decl (decl);
25643 call_arg_locations = NULL;
25644 call_arg_loc_last = NULL;
25645 call_site_count = -1;
25646 tail_call_site_count = -1;
25647 decl_loc_table->empty ();
25648 cached_dw_loc_list_table->empty ();
25651 /* Output a marker (i.e. a label) for the beginning of the generated code for
25652 a lexical block. */
25654 static void
25655 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25656 unsigned int blocknum)
25658 switch_to_section (current_function_section ());
25659 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25662 /* Output a marker (i.e. a label) for the end of the generated code for a
25663 lexical block. */
25665 static void
25666 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25668 switch_to_section (current_function_section ());
25669 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25672 /* Returns nonzero if it is appropriate not to emit any debugging
25673 information for BLOCK, because it doesn't contain any instructions.
25675 Don't allow this for blocks with nested functions or local classes
25676 as we would end up with orphans, and in the presence of scheduling
25677 we may end up calling them anyway. */
25679 static bool
25680 dwarf2out_ignore_block (const_tree block)
25682 tree decl;
25683 unsigned int i;
25685 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25686 if (TREE_CODE (decl) == FUNCTION_DECL
25687 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25688 return 0;
25689 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25691 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25692 if (TREE_CODE (decl) == FUNCTION_DECL
25693 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25694 return 0;
25697 return 1;
25700 /* Hash table routines for file_hash. */
25702 bool
25703 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25705 return filename_cmp (p1->filename, p2) == 0;
25708 hashval_t
25709 dwarf_file_hasher::hash (dwarf_file_data *p)
25711 return htab_hash_string (p->filename);
25714 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25715 dwarf2out.c) and return its "index". The index of each (known) filename is
25716 just a unique number which is associated with only that one filename. We
25717 need such numbers for the sake of generating labels (in the .debug_sfnames
25718 section) and references to those files numbers (in the .debug_srcinfo
25719 and .debug_macinfo sections). If the filename given as an argument is not
25720 found in our current list, add it to the list and assign it the next
25721 available unique index number. */
25723 static struct dwarf_file_data *
25724 lookup_filename (const char *file_name)
25726 struct dwarf_file_data * created;
25728 if (!file_name)
25729 return NULL;
25731 dwarf_file_data **slot
25732 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25733 INSERT);
25734 if (*slot)
25735 return *slot;
25737 created = ggc_alloc<dwarf_file_data> ();
25738 created->filename = file_name;
25739 created->emitted_number = 0;
25740 *slot = created;
25741 return created;
25744 /* If the assembler will construct the file table, then translate the compiler
25745 internal file table number into the assembler file table number, and emit
25746 a .file directive if we haven't already emitted one yet. The file table
25747 numbers are different because we prune debug info for unused variables and
25748 types, which may include filenames. */
25750 static int
25751 maybe_emit_file (struct dwarf_file_data * fd)
25753 if (! fd->emitted_number)
25755 if (last_emitted_file)
25756 fd->emitted_number = last_emitted_file->emitted_number + 1;
25757 else
25758 fd->emitted_number = 1;
25759 last_emitted_file = fd;
25761 if (DWARF2_ASM_LINE_DEBUG_INFO)
25763 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25764 output_quoted_string (asm_out_file,
25765 remap_debug_filename (fd->filename));
25766 fputc ('\n', asm_out_file);
25770 return fd->emitted_number;
25773 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25774 That generation should happen after function debug info has been
25775 generated. The value of the attribute is the constant value of ARG. */
25777 static void
25778 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25780 die_arg_entry entry;
25782 if (!die || !arg)
25783 return;
25785 gcc_assert (early_dwarf);
25787 if (!tmpl_value_parm_die_table)
25788 vec_alloc (tmpl_value_parm_die_table, 32);
25790 entry.die = die;
25791 entry.arg = arg;
25792 vec_safe_push (tmpl_value_parm_die_table, entry);
25795 /* Return TRUE if T is an instance of generic type, FALSE
25796 otherwise. */
25798 static bool
25799 generic_type_p (tree t)
25801 if (t == NULL_TREE || !TYPE_P (t))
25802 return false;
25803 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25806 /* Schedule the generation of the generic parameter dies for the
25807 instance of generic type T. The proper generation itself is later
25808 done by gen_scheduled_generic_parms_dies. */
25810 static void
25811 schedule_generic_params_dies_gen (tree t)
25813 if (!generic_type_p (t))
25814 return;
25816 gcc_assert (early_dwarf);
25818 if (!generic_type_instances)
25819 vec_alloc (generic_type_instances, 256);
25821 vec_safe_push (generic_type_instances, t);
25824 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25825 by append_entry_to_tmpl_value_parm_die_table. This function must
25826 be called after function DIEs have been generated. */
25828 static void
25829 gen_remaining_tmpl_value_param_die_attribute (void)
25831 if (tmpl_value_parm_die_table)
25833 unsigned i, j;
25834 die_arg_entry *e;
25836 /* We do this in two phases - first get the cases we can
25837 handle during early-finish, preserving those we cannot
25838 (containing symbolic constants where we don't yet know
25839 whether we are going to output the referenced symbols).
25840 For those we try again at late-finish. */
25841 j = 0;
25842 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
25844 if (!tree_add_const_value_attribute (e->die, e->arg))
25846 dw_loc_descr_ref loc = NULL;
25847 if (! early_dwarf
25848 && (dwarf_version >= 5 || !dwarf_strict))
25849 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
25850 if (loc)
25851 add_AT_loc (e->die, DW_AT_location, loc);
25852 else
25853 (*tmpl_value_parm_die_table)[j++] = *e;
25856 tmpl_value_parm_die_table->truncate (j);
25860 /* Generate generic parameters DIEs for instances of generic types
25861 that have been previously scheduled by
25862 schedule_generic_params_dies_gen. This function must be called
25863 after all the types of the CU have been laid out. */
25865 static void
25866 gen_scheduled_generic_parms_dies (void)
25868 unsigned i;
25869 tree t;
25871 if (!generic_type_instances)
25872 return;
25874 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
25875 if (COMPLETE_TYPE_P (t))
25876 gen_generic_params_dies (t);
25878 generic_type_instances = NULL;
25882 /* Replace DW_AT_name for the decl with name. */
25884 static void
25885 dwarf2out_set_name (tree decl, tree name)
25887 dw_die_ref die;
25888 dw_attr_node *attr;
25889 const char *dname;
25891 die = TYPE_SYMTAB_DIE (decl);
25892 if (!die)
25893 return;
25895 dname = dwarf2_name (name, 0);
25896 if (!dname)
25897 return;
25899 attr = get_AT (die, DW_AT_name);
25900 if (attr)
25902 struct indirect_string_node *node;
25904 node = find_AT_string (dname);
25905 /* replace the string. */
25906 attr->dw_attr_val.v.val_str = node;
25909 else
25910 add_name_attribute (die, dname);
25913 /* True if before or during processing of the first function being emitted. */
25914 static bool in_first_function_p = true;
25915 /* True if loc_note during dwarf2out_var_location call might still be
25916 before first real instruction at address equal to .Ltext0. */
25917 static bool maybe_at_text_label_p = true;
25918 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
25919 static unsigned int first_loclabel_num_not_at_text_label;
25921 /* Called by the final INSN scan whenever we see a var location. We
25922 use it to drop labels in the right places, and throw the location in
25923 our lookup table. */
25925 static void
25926 dwarf2out_var_location (rtx_insn *loc_note)
25928 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
25929 struct var_loc_node *newloc;
25930 rtx_insn *next_real, *next_note;
25931 rtx_insn *call_insn = NULL;
25932 static const char *last_label;
25933 static const char *last_postcall_label;
25934 static bool last_in_cold_section_p;
25935 static rtx_insn *expected_next_loc_note;
25936 tree decl;
25937 bool var_loc_p;
25939 if (!NOTE_P (loc_note))
25941 if (CALL_P (loc_note))
25943 call_site_count++;
25944 if (SIBLING_CALL_P (loc_note))
25945 tail_call_site_count++;
25946 if (optimize == 0 && !flag_var_tracking)
25948 /* When the var-tracking pass is not running, there is no note
25949 for indirect calls whose target is compile-time known. In this
25950 case, process such calls specifically so that we generate call
25951 sites for them anyway. */
25952 rtx x = PATTERN (loc_note);
25953 if (GET_CODE (x) == PARALLEL)
25954 x = XVECEXP (x, 0, 0);
25955 if (GET_CODE (x) == SET)
25956 x = SET_SRC (x);
25957 if (GET_CODE (x) == CALL)
25958 x = XEXP (x, 0);
25959 if (!MEM_P (x)
25960 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
25961 || !SYMBOL_REF_DECL (XEXP (x, 0))
25962 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
25963 != FUNCTION_DECL))
25965 call_insn = loc_note;
25966 loc_note = NULL;
25967 var_loc_p = false;
25969 next_real = next_real_insn (call_insn);
25970 next_note = NULL;
25971 cached_next_real_insn = NULL;
25972 goto create_label;
25976 return;
25979 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
25980 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
25981 return;
25983 /* Optimize processing a large consecutive sequence of location
25984 notes so we don't spend too much time in next_real_insn. If the
25985 next insn is another location note, remember the next_real_insn
25986 calculation for next time. */
25987 next_real = cached_next_real_insn;
25988 if (next_real)
25990 if (expected_next_loc_note != loc_note)
25991 next_real = NULL;
25994 next_note = NEXT_INSN (loc_note);
25995 if (! next_note
25996 || next_note->deleted ()
25997 || ! NOTE_P (next_note)
25998 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
25999 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26000 next_note = NULL;
26002 if (! next_real)
26003 next_real = next_real_insn (loc_note);
26005 if (next_note)
26007 expected_next_loc_note = next_note;
26008 cached_next_real_insn = next_real;
26010 else
26011 cached_next_real_insn = NULL;
26013 /* If there are no instructions which would be affected by this note,
26014 don't do anything. */
26015 if (var_loc_p
26016 && next_real == NULL_RTX
26017 && !NOTE_DURING_CALL_P (loc_note))
26018 return;
26020 create_label:
26022 if (next_real == NULL_RTX)
26023 next_real = get_last_insn ();
26025 /* If there were any real insns between note we processed last time
26026 and this note (or if it is the first note), clear
26027 last_{,postcall_}label so that they are not reused this time. */
26028 if (last_var_location_insn == NULL_RTX
26029 || last_var_location_insn != next_real
26030 || last_in_cold_section_p != in_cold_section_p)
26032 last_label = NULL;
26033 last_postcall_label = NULL;
26036 if (var_loc_p)
26038 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26039 newloc = add_var_loc_to_decl (decl, loc_note,
26040 NOTE_DURING_CALL_P (loc_note)
26041 ? last_postcall_label : last_label);
26042 if (newloc == NULL)
26043 return;
26045 else
26047 decl = NULL_TREE;
26048 newloc = NULL;
26051 /* If there were no real insns between note we processed last time
26052 and this note, use the label we emitted last time. Otherwise
26053 create a new label and emit it. */
26054 if (last_label == NULL)
26056 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26058 loclabel_num++;
26059 last_label = ggc_strdup (loclabel);
26060 /* See if loclabel might be equal to .Ltext0. If yes,
26061 bump first_loclabel_num_not_at_text_label. */
26062 if (!have_multiple_function_sections
26063 && in_first_function_p
26064 && maybe_at_text_label_p)
26066 static rtx_insn *last_start;
26067 rtx_insn *insn;
26068 for (insn = loc_note; insn; insn = previous_insn (insn))
26069 if (insn == last_start)
26070 break;
26071 else if (!NONDEBUG_INSN_P (insn))
26072 continue;
26073 else
26075 rtx body = PATTERN (insn);
26076 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26077 continue;
26078 /* Inline asm could occupy zero bytes. */
26079 else if (GET_CODE (body) == ASM_INPUT
26080 || asm_noperands (body) >= 0)
26081 continue;
26082 #ifdef HAVE_attr_length
26083 else if (get_attr_min_length (insn) == 0)
26084 continue;
26085 #endif
26086 else
26088 /* Assume insn has non-zero length. */
26089 maybe_at_text_label_p = false;
26090 break;
26093 if (maybe_at_text_label_p)
26095 last_start = loc_note;
26096 first_loclabel_num_not_at_text_label = loclabel_num;
26101 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26102 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26104 if (!var_loc_p)
26106 struct call_arg_loc_node *ca_loc
26107 = ggc_cleared_alloc<call_arg_loc_node> ();
26108 rtx_insn *prev
26109 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26111 ca_loc->call_arg_loc_note = loc_note;
26112 ca_loc->next = NULL;
26113 ca_loc->label = last_label;
26114 gcc_assert (prev
26115 && (CALL_P (prev)
26116 || (NONJUMP_INSN_P (prev)
26117 && GET_CODE (PATTERN (prev)) == SEQUENCE
26118 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26119 if (!CALL_P (prev))
26120 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26121 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26123 /* Look for a SYMBOL_REF in the "prev" instruction. */
26124 rtx x = get_call_rtx_from (PATTERN (prev));
26125 if (x)
26127 /* Try to get the call symbol, if any. */
26128 if (MEM_P (XEXP (x, 0)))
26129 x = XEXP (x, 0);
26130 /* First, look for a memory access to a symbol_ref. */
26131 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26132 && SYMBOL_REF_DECL (XEXP (x, 0))
26133 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26134 ca_loc->symbol_ref = XEXP (x, 0);
26135 /* Otherwise, look at a compile-time known user-level function
26136 declaration. */
26137 else if (MEM_P (x)
26138 && MEM_EXPR (x)
26139 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26140 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26143 ca_loc->block = insn_scope (prev);
26144 if (call_arg_locations)
26145 call_arg_loc_last->next = ca_loc;
26146 else
26147 call_arg_locations = ca_loc;
26148 call_arg_loc_last = ca_loc;
26150 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26151 newloc->label = last_label;
26152 else
26154 if (!last_postcall_label)
26156 sprintf (loclabel, "%s-1", last_label);
26157 last_postcall_label = ggc_strdup (loclabel);
26159 newloc->label = last_postcall_label;
26162 last_var_location_insn = next_real;
26163 last_in_cold_section_p = in_cold_section_p;
26166 /* Called from finalize_size_functions for size functions so that their body
26167 can be encoded in the debug info to describe the layout of variable-length
26168 structures. */
26170 static void
26171 dwarf2out_size_function (tree decl)
26173 function_to_dwarf_procedure (decl);
26176 /* Note in one location list that text section has changed. */
26179 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26181 var_loc_list *list = *slot;
26182 if (list->first)
26183 list->last_before_switch
26184 = list->last->next ? list->last->next : list->last;
26185 return 1;
26188 /* Note in all location lists that text section has changed. */
26190 static void
26191 var_location_switch_text_section (void)
26193 if (decl_loc_table == NULL)
26194 return;
26196 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26199 /* Create a new line number table. */
26201 static dw_line_info_table *
26202 new_line_info_table (void)
26204 dw_line_info_table *table;
26206 table = ggc_cleared_alloc<dw_line_info_table> ();
26207 table->file_num = 1;
26208 table->line_num = 1;
26209 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26211 return table;
26214 /* Lookup the "current" table into which we emit line info, so
26215 that we don't have to do it for every source line. */
26217 static void
26218 set_cur_line_info_table (section *sec)
26220 dw_line_info_table *table;
26222 if (sec == text_section)
26223 table = text_section_line_info;
26224 else if (sec == cold_text_section)
26226 table = cold_text_section_line_info;
26227 if (!table)
26229 cold_text_section_line_info = table = new_line_info_table ();
26230 table->end_label = cold_end_label;
26233 else
26235 const char *end_label;
26237 if (flag_reorder_blocks_and_partition)
26239 if (in_cold_section_p)
26240 end_label = crtl->subsections.cold_section_end_label;
26241 else
26242 end_label = crtl->subsections.hot_section_end_label;
26244 else
26246 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26247 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26248 current_function_funcdef_no);
26249 end_label = ggc_strdup (label);
26252 table = new_line_info_table ();
26253 table->end_label = end_label;
26255 vec_safe_push (separate_line_info, table);
26258 if (DWARF2_ASM_LINE_DEBUG_INFO)
26259 table->is_stmt = (cur_line_info_table
26260 ? cur_line_info_table->is_stmt
26261 : DWARF_LINE_DEFAULT_IS_STMT_START);
26262 cur_line_info_table = table;
26266 /* We need to reset the locations at the beginning of each
26267 function. We can't do this in the end_function hook, because the
26268 declarations that use the locations won't have been output when
26269 that hook is called. Also compute have_multiple_function_sections here. */
26271 static void
26272 dwarf2out_begin_function (tree fun)
26274 section *sec = function_section (fun);
26276 if (sec != text_section)
26277 have_multiple_function_sections = true;
26279 if (flag_reorder_blocks_and_partition && !cold_text_section)
26281 gcc_assert (current_function_decl == fun);
26282 cold_text_section = unlikely_text_section ();
26283 switch_to_section (cold_text_section);
26284 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26285 switch_to_section (sec);
26288 dwarf2out_note_section_used ();
26289 call_site_count = 0;
26290 tail_call_site_count = 0;
26292 set_cur_line_info_table (sec);
26295 /* Helper function of dwarf2out_end_function, called only after emitting
26296 the very first function into assembly. Check if some .debug_loc range
26297 might end with a .LVL* label that could be equal to .Ltext0.
26298 In that case we must force using absolute addresses in .debug_loc ranges,
26299 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26300 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26301 list terminator.
26302 Set have_multiple_function_sections to true in that case and
26303 terminate htab traversal. */
26306 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26308 var_loc_list *entry = *slot;
26309 struct var_loc_node *node;
26311 node = entry->first;
26312 if (node && node->next && node->next->label)
26314 unsigned int i;
26315 const char *label = node->next->label;
26316 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26318 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26320 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26321 if (strcmp (label, loclabel) == 0)
26323 have_multiple_function_sections = true;
26324 return 0;
26328 return 1;
26331 /* Hook called after emitting a function into assembly.
26332 This does something only for the very first function emitted. */
26334 static void
26335 dwarf2out_end_function (unsigned int)
26337 if (in_first_function_p
26338 && !have_multiple_function_sections
26339 && first_loclabel_num_not_at_text_label
26340 && decl_loc_table)
26341 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26342 in_first_function_p = false;
26343 maybe_at_text_label_p = false;
26346 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26347 front-ends register a translation unit even before dwarf2out_init is
26348 called. */
26349 static tree main_translation_unit = NULL_TREE;
26351 /* Hook called by front-ends after they built their main translation unit.
26352 Associate comp_unit_die to UNIT. */
26354 static void
26355 dwarf2out_register_main_translation_unit (tree unit)
26357 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26358 && main_translation_unit == NULL_TREE);
26359 main_translation_unit = unit;
26360 /* If dwarf2out_init has not been called yet, it will perform the association
26361 itself looking at main_translation_unit. */
26362 if (decl_die_table != NULL)
26363 equate_decl_number_to_die (unit, comp_unit_die ());
26366 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26368 static void
26369 push_dw_line_info_entry (dw_line_info_table *table,
26370 enum dw_line_info_opcode opcode, unsigned int val)
26372 dw_line_info_entry e;
26373 e.opcode = opcode;
26374 e.val = val;
26375 vec_safe_push (table->entries, e);
26378 /* Output a label to mark the beginning of a source code line entry
26379 and record information relating to this source line, in
26380 'line_info_table' for later output of the .debug_line section. */
26381 /* ??? The discriminator parameter ought to be unsigned. */
26383 static void
26384 dwarf2out_source_line (unsigned int line, const char *filename,
26385 int discriminator, bool is_stmt)
26387 unsigned int file_num;
26388 dw_line_info_table *table;
26390 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26391 return;
26393 /* The discriminator column was added in dwarf4. Simplify the below
26394 by simply removing it if we're not supposed to output it. */
26395 if (dwarf_version < 4 && dwarf_strict)
26396 discriminator = 0;
26398 table = cur_line_info_table;
26399 file_num = maybe_emit_file (lookup_filename (filename));
26401 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26402 the debugger has used the second (possibly duplicate) line number
26403 at the beginning of the function to mark the end of the prologue.
26404 We could eliminate any other duplicates within the function. For
26405 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26406 that second line number entry. */
26407 /* Recall that this end-of-prologue indication is *not* the same thing
26408 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26409 to which the hook corresponds, follows the last insn that was
26410 emitted by gen_prologue. What we need is to precede the first insn
26411 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26412 insn that corresponds to something the user wrote. These may be
26413 very different locations once scheduling is enabled. */
26415 if (0 && file_num == table->file_num
26416 && line == table->line_num
26417 && discriminator == table->discrim_num
26418 && is_stmt == table->is_stmt)
26419 return;
26421 switch_to_section (current_function_section ());
26423 /* If requested, emit something human-readable. */
26424 if (flag_debug_asm)
26425 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
26427 if (DWARF2_ASM_LINE_DEBUG_INFO)
26429 /* Emit the .loc directive understood by GNU as. */
26430 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26431 file_num, line, is_stmt, discriminator */
26432 fputs ("\t.loc ", asm_out_file);
26433 fprint_ul (asm_out_file, file_num);
26434 putc (' ', asm_out_file);
26435 fprint_ul (asm_out_file, line);
26436 putc (' ', asm_out_file);
26437 putc ('0', asm_out_file);
26439 if (is_stmt != table->is_stmt)
26441 fputs (" is_stmt ", asm_out_file);
26442 putc (is_stmt ? '1' : '0', asm_out_file);
26444 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26446 gcc_assert (discriminator > 0);
26447 fputs (" discriminator ", asm_out_file);
26448 fprint_ul (asm_out_file, (unsigned long) discriminator);
26450 putc ('\n', asm_out_file);
26452 else
26454 unsigned int label_num = ++line_info_label_num;
26456 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26458 push_dw_line_info_entry (table, LI_set_address, label_num);
26459 if (file_num != table->file_num)
26460 push_dw_line_info_entry (table, LI_set_file, file_num);
26461 if (discriminator != table->discrim_num)
26462 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26463 if (is_stmt != table->is_stmt)
26464 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26465 push_dw_line_info_entry (table, LI_set_line, line);
26468 table->file_num = file_num;
26469 table->line_num = line;
26470 table->discrim_num = discriminator;
26471 table->is_stmt = is_stmt;
26472 table->in_use = true;
26475 /* Record the beginning of a new source file. */
26477 static void
26478 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26480 if (flag_eliminate_dwarf2_dups)
26482 /* Record the beginning of the file for break_out_includes. */
26483 dw_die_ref bincl_die;
26485 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26486 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26489 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26491 macinfo_entry e;
26492 e.code = DW_MACINFO_start_file;
26493 e.lineno = lineno;
26494 e.info = ggc_strdup (filename);
26495 vec_safe_push (macinfo_table, e);
26499 /* Record the end of a source file. */
26501 static void
26502 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26504 if (flag_eliminate_dwarf2_dups)
26505 /* Record the end of the file for break_out_includes. */
26506 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26508 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26510 macinfo_entry e;
26511 e.code = DW_MACINFO_end_file;
26512 e.lineno = lineno;
26513 e.info = NULL;
26514 vec_safe_push (macinfo_table, e);
26518 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26519 the tail part of the directive line, i.e. the part which is past the
26520 initial whitespace, #, whitespace, directive-name, whitespace part. */
26522 static void
26523 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26524 const char *buffer ATTRIBUTE_UNUSED)
26526 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26528 macinfo_entry e;
26529 /* Insert a dummy first entry to be able to optimize the whole
26530 predefined macro block using DW_MACRO_import. */
26531 if (macinfo_table->is_empty () && lineno <= 1)
26533 e.code = 0;
26534 e.lineno = 0;
26535 e.info = NULL;
26536 vec_safe_push (macinfo_table, e);
26538 e.code = DW_MACINFO_define;
26539 e.lineno = lineno;
26540 e.info = ggc_strdup (buffer);
26541 vec_safe_push (macinfo_table, e);
26545 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26546 the tail part of the directive line, i.e. the part which is past the
26547 initial whitespace, #, whitespace, directive-name, whitespace part. */
26549 static void
26550 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26551 const char *buffer ATTRIBUTE_UNUSED)
26553 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26555 macinfo_entry e;
26556 /* Insert a dummy first entry to be able to optimize the whole
26557 predefined macro block using DW_MACRO_import. */
26558 if (macinfo_table->is_empty () && lineno <= 1)
26560 e.code = 0;
26561 e.lineno = 0;
26562 e.info = NULL;
26563 vec_safe_push (macinfo_table, e);
26565 e.code = DW_MACINFO_undef;
26566 e.lineno = lineno;
26567 e.info = ggc_strdup (buffer);
26568 vec_safe_push (macinfo_table, e);
26572 /* Helpers to manipulate hash table of CUs. */
26574 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26576 static inline hashval_t hash (const macinfo_entry *);
26577 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26580 inline hashval_t
26581 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26583 return htab_hash_string (entry->info);
26586 inline bool
26587 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26588 const macinfo_entry *entry2)
26590 return !strcmp (entry1->info, entry2->info);
26593 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26595 /* Output a single .debug_macinfo entry. */
26597 static void
26598 output_macinfo_op (macinfo_entry *ref)
26600 int file_num;
26601 size_t len;
26602 struct indirect_string_node *node;
26603 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26604 struct dwarf_file_data *fd;
26606 switch (ref->code)
26608 case DW_MACINFO_start_file:
26609 fd = lookup_filename (ref->info);
26610 file_num = maybe_emit_file (fd);
26611 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26612 dw2_asm_output_data_uleb128 (ref->lineno,
26613 "Included from line number %lu",
26614 (unsigned long) ref->lineno);
26615 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26616 break;
26617 case DW_MACINFO_end_file:
26618 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26619 break;
26620 case DW_MACINFO_define:
26621 case DW_MACINFO_undef:
26622 len = strlen (ref->info) + 1;
26623 if (!dwarf_strict
26624 && len > DWARF_OFFSET_SIZE
26625 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26626 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26628 ref->code = ref->code == DW_MACINFO_define
26629 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26630 output_macinfo_op (ref);
26631 return;
26633 dw2_asm_output_data (1, ref->code,
26634 ref->code == DW_MACINFO_define
26635 ? "Define macro" : "Undefine macro");
26636 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26637 (unsigned long) ref->lineno);
26638 dw2_asm_output_nstring (ref->info, -1, "The macro");
26639 break;
26640 case DW_MACRO_define_strp:
26641 case DW_MACRO_undef_strp:
26642 node = find_AT_string (ref->info);
26643 gcc_assert (node
26644 && (node->form == DW_FORM_strp
26645 || node->form == DW_FORM_GNU_str_index));
26646 dw2_asm_output_data (1, ref->code,
26647 ref->code == DW_MACRO_define_strp
26648 ? "Define macro strp"
26649 : "Undefine macro strp");
26650 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26651 (unsigned long) ref->lineno);
26652 if (node->form == DW_FORM_strp)
26653 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26654 debug_str_section, "The macro: \"%s\"",
26655 ref->info);
26656 else
26657 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26658 ref->info);
26659 break;
26660 case DW_MACRO_import:
26661 dw2_asm_output_data (1, ref->code, "Import");
26662 ASM_GENERATE_INTERNAL_LABEL (label,
26663 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26664 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26665 break;
26666 default:
26667 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26668 ASM_COMMENT_START, (unsigned long) ref->code);
26669 break;
26673 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26674 other compilation unit .debug_macinfo sections. IDX is the first
26675 index of a define/undef, return the number of ops that should be
26676 emitted in a comdat .debug_macinfo section and emit
26677 a DW_MACRO_import entry referencing it.
26678 If the define/undef entry should be emitted normally, return 0. */
26680 static unsigned
26681 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26682 macinfo_hash_type **macinfo_htab)
26684 macinfo_entry *first, *second, *cur, *inc;
26685 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26686 unsigned char checksum[16];
26687 struct md5_ctx ctx;
26688 char *grp_name, *tail;
26689 const char *base;
26690 unsigned int i, count, encoded_filename_len, linebuf_len;
26691 macinfo_entry **slot;
26693 first = &(*macinfo_table)[idx];
26694 second = &(*macinfo_table)[idx + 1];
26696 /* Optimize only if there are at least two consecutive define/undef ops,
26697 and either all of them are before first DW_MACINFO_start_file
26698 with lineno {0,1} (i.e. predefined macro block), or all of them are
26699 in some included header file. */
26700 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26701 return 0;
26702 if (vec_safe_is_empty (files))
26704 if (first->lineno > 1 || second->lineno > 1)
26705 return 0;
26707 else if (first->lineno == 0)
26708 return 0;
26710 /* Find the last define/undef entry that can be grouped together
26711 with first and at the same time compute md5 checksum of their
26712 codes, linenumbers and strings. */
26713 md5_init_ctx (&ctx);
26714 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26715 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26716 break;
26717 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26718 break;
26719 else
26721 unsigned char code = cur->code;
26722 md5_process_bytes (&code, 1, &ctx);
26723 checksum_uleb128 (cur->lineno, &ctx);
26724 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26726 md5_finish_ctx (&ctx, checksum);
26727 count = i - idx;
26729 /* From the containing include filename (if any) pick up just
26730 usable characters from its basename. */
26731 if (vec_safe_is_empty (files))
26732 base = "";
26733 else
26734 base = lbasename (files->last ().info);
26735 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26736 if (ISIDNUM (base[i]) || base[i] == '.')
26737 encoded_filename_len++;
26738 /* Count . at the end. */
26739 if (encoded_filename_len)
26740 encoded_filename_len++;
26742 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26743 linebuf_len = strlen (linebuf);
26745 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26746 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26747 + 16 * 2 + 1);
26748 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26749 tail = grp_name + 4;
26750 if (encoded_filename_len)
26752 for (i = 0; base[i]; i++)
26753 if (ISIDNUM (base[i]) || base[i] == '.')
26754 *tail++ = base[i];
26755 *tail++ = '.';
26757 memcpy (tail, linebuf, linebuf_len);
26758 tail += linebuf_len;
26759 *tail++ = '.';
26760 for (i = 0; i < 16; i++)
26761 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26763 /* Construct a macinfo_entry for DW_MACRO_import
26764 in the empty vector entry before the first define/undef. */
26765 inc = &(*macinfo_table)[idx - 1];
26766 inc->code = DW_MACRO_import;
26767 inc->lineno = 0;
26768 inc->info = ggc_strdup (grp_name);
26769 if (!*macinfo_htab)
26770 *macinfo_htab = new macinfo_hash_type (10);
26771 /* Avoid emitting duplicates. */
26772 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26773 if (*slot != NULL)
26775 inc->code = 0;
26776 inc->info = NULL;
26777 /* If such an entry has been used before, just emit
26778 a DW_MACRO_import op. */
26779 inc = *slot;
26780 output_macinfo_op (inc);
26781 /* And clear all macinfo_entry in the range to avoid emitting them
26782 in the second pass. */
26783 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26785 cur->code = 0;
26786 cur->info = NULL;
26789 else
26791 *slot = inc;
26792 inc->lineno = (*macinfo_htab)->elements ();
26793 output_macinfo_op (inc);
26795 return count;
26798 /* Save any strings needed by the macinfo table in the debug str
26799 table. All strings must be collected into the table by the time
26800 index_string is called. */
26802 static void
26803 save_macinfo_strings (void)
26805 unsigned len;
26806 unsigned i;
26807 macinfo_entry *ref;
26809 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26811 switch (ref->code)
26813 /* Match the logic in output_macinfo_op to decide on
26814 indirect strings. */
26815 case DW_MACINFO_define:
26816 case DW_MACINFO_undef:
26817 len = strlen (ref->info) + 1;
26818 if (!dwarf_strict
26819 && len > DWARF_OFFSET_SIZE
26820 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26821 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26822 set_indirect_string (find_AT_string (ref->info));
26823 break;
26824 case DW_MACRO_define_strp:
26825 case DW_MACRO_undef_strp:
26826 set_indirect_string (find_AT_string (ref->info));
26827 break;
26828 default:
26829 break;
26834 /* Output macinfo section(s). */
26836 static void
26837 output_macinfo (void)
26839 unsigned i;
26840 unsigned long length = vec_safe_length (macinfo_table);
26841 macinfo_entry *ref;
26842 vec<macinfo_entry, va_gc> *files = NULL;
26843 macinfo_hash_type *macinfo_htab = NULL;
26845 if (! length)
26846 return;
26848 /* output_macinfo* uses these interchangeably. */
26849 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
26850 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
26851 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
26852 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
26854 /* For .debug_macro emit the section header. */
26855 if (!dwarf_strict || dwarf_version >= 5)
26857 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26858 "DWARF macro version number");
26859 if (DWARF_OFFSET_SIZE == 8)
26860 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
26861 else
26862 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
26863 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
26864 (!dwarf_split_debug_info ? debug_line_section_label
26865 : debug_skeleton_line_section_label),
26866 debug_line_section, NULL);
26869 /* In the first loop, it emits the primary .debug_macinfo section
26870 and after each emitted op the macinfo_entry is cleared.
26871 If a longer range of define/undef ops can be optimized using
26872 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
26873 the vector before the first define/undef in the range and the
26874 whole range of define/undef ops is not emitted and kept. */
26875 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26877 switch (ref->code)
26879 case DW_MACINFO_start_file:
26880 vec_safe_push (files, *ref);
26881 break;
26882 case DW_MACINFO_end_file:
26883 if (!vec_safe_is_empty (files))
26884 files->pop ();
26885 break;
26886 case DW_MACINFO_define:
26887 case DW_MACINFO_undef:
26888 if ((!dwarf_strict || dwarf_version >= 5)
26889 && HAVE_COMDAT_GROUP
26890 && vec_safe_length (files) != 1
26891 && i > 0
26892 && i + 1 < length
26893 && (*macinfo_table)[i - 1].code == 0)
26895 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
26896 if (count)
26898 i += count - 1;
26899 continue;
26902 break;
26903 case 0:
26904 /* A dummy entry may be inserted at the beginning to be able
26905 to optimize the whole block of predefined macros. */
26906 if (i == 0)
26907 continue;
26908 default:
26909 break;
26911 output_macinfo_op (ref);
26912 ref->info = NULL;
26913 ref->code = 0;
26916 if (!macinfo_htab)
26917 return;
26919 delete macinfo_htab;
26920 macinfo_htab = NULL;
26922 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
26923 terminate the current chain and switch to a new comdat .debug_macinfo
26924 section and emit the define/undef entries within it. */
26925 for (i = 0; macinfo_table->iterate (i, &ref); i++)
26926 switch (ref->code)
26928 case 0:
26929 continue;
26930 case DW_MACRO_import:
26932 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26933 tree comdat_key = get_identifier (ref->info);
26934 /* Terminate the previous .debug_macinfo section. */
26935 dw2_asm_output_data (1, 0, "End compilation unit");
26936 targetm.asm_out.named_section (debug_macinfo_section_name,
26937 SECTION_DEBUG
26938 | SECTION_LINKONCE,
26939 comdat_key);
26940 ASM_GENERATE_INTERNAL_LABEL (label,
26941 DEBUG_MACRO_SECTION_LABEL,
26942 ref->lineno);
26943 ASM_OUTPUT_LABEL (asm_out_file, label);
26944 ref->code = 0;
26945 ref->info = NULL;
26946 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
26947 "DWARF macro version number");
26948 if (DWARF_OFFSET_SIZE == 8)
26949 dw2_asm_output_data (1, 1, "Flags: 64-bit");
26950 else
26951 dw2_asm_output_data (1, 0, "Flags: 32-bit");
26953 break;
26954 case DW_MACINFO_define:
26955 case DW_MACINFO_undef:
26956 output_macinfo_op (ref);
26957 ref->code = 0;
26958 ref->info = NULL;
26959 break;
26960 default:
26961 gcc_unreachable ();
26965 /* Initialize the various sections and labels for dwarf output. */
26967 static void
26968 init_sections_and_labels (void)
26970 if (!dwarf_split_debug_info)
26972 debug_info_section = get_section (DEBUG_INFO_SECTION,
26973 SECTION_DEBUG, NULL);
26974 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
26975 SECTION_DEBUG, NULL);
26976 debug_loc_section = get_section (dwarf_version >= 5
26977 ? DEBUG_LOCLISTS_SECTION
26978 : DEBUG_LOC_SECTION,
26979 SECTION_DEBUG, NULL);
26980 debug_macinfo_section_name
26981 = (dwarf_strict && dwarf_version < 5)
26982 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
26983 debug_macinfo_section = get_section (debug_macinfo_section_name,
26984 SECTION_DEBUG, NULL);
26986 else
26988 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
26989 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
26990 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
26991 SECTION_DEBUG | SECTION_EXCLUDE,
26992 NULL);
26993 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
26994 SECTION_DEBUG, NULL);
26995 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
26996 SECTION_DEBUG, NULL);
26997 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
26998 SECTION_DEBUG, NULL);
26999 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27000 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27002 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27003 the main .o, but the skeleton_line goes into the split off dwo. */
27004 debug_skeleton_line_section
27005 = get_section (DEBUG_DWO_LINE_SECTION,
27006 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27007 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27008 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27009 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27010 SECTION_DEBUG | SECTION_EXCLUDE,
27011 NULL);
27012 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27013 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27014 debug_loc_section = get_section (dwarf_version >= 5
27015 ? DEBUG_DWO_LOCLISTS_SECTION
27016 : DEBUG_DWO_LOC_SECTION,
27017 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27018 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27019 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27020 debug_macinfo_section_name
27021 = (dwarf_strict && dwarf_version < 5)
27022 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27023 debug_macinfo_section = get_section (debug_macinfo_section_name,
27024 SECTION_DEBUG | SECTION_EXCLUDE,
27025 NULL);
27027 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27028 SECTION_DEBUG, NULL);
27029 debug_line_section = get_section (DEBUG_LINE_SECTION,
27030 SECTION_DEBUG, NULL);
27031 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27032 SECTION_DEBUG, NULL);
27033 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27034 SECTION_DEBUG, NULL);
27035 debug_str_section = get_section (DEBUG_STR_SECTION,
27036 DEBUG_STR_SECTION_FLAGS, NULL);
27037 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27038 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27039 DEBUG_STR_SECTION_FLAGS, NULL);
27041 debug_ranges_section = get_section (dwarf_version >= 5
27042 ? DEBUG_RNGLISTS_SECTION
27043 : DEBUG_RANGES_SECTION,
27044 SECTION_DEBUG, NULL);
27045 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27046 SECTION_DEBUG, NULL);
27048 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27049 DEBUG_ABBREV_SECTION_LABEL, 0);
27050 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27051 DEBUG_INFO_SECTION_LABEL, 0);
27052 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27053 DEBUG_LINE_SECTION_LABEL, 0);
27054 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27055 DEBUG_RANGES_SECTION_LABEL, 0);
27056 if (dwarf_version >= 5 && dwarf_split_debug_info)
27057 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27058 DEBUG_RANGES_SECTION_LABEL, 1);
27059 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27060 DEBUG_ADDR_SECTION_LABEL, 0);
27061 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27062 (dwarf_strict && dwarf_version < 5)
27063 ? DEBUG_MACINFO_SECTION_LABEL
27064 : DEBUG_MACRO_SECTION_LABEL, 0);
27065 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27068 /* Set up for Dwarf output at the start of compilation. */
27070 static void
27071 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27073 /* This option is currently broken, see (PR53118 and PR46102). */
27074 if (flag_eliminate_dwarf2_dups
27075 && strstr (lang_hooks.name, "C++"))
27077 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27078 flag_eliminate_dwarf2_dups = 0;
27081 /* Allocate the file_table. */
27082 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27084 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27085 /* Allocate the decl_die_table. */
27086 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27088 /* Allocate the decl_loc_table. */
27089 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27091 /* Allocate the cached_dw_loc_list_table. */
27092 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27094 /* Allocate the initial hunk of the decl_scope_table. */
27095 vec_alloc (decl_scope_table, 256);
27097 /* Allocate the initial hunk of the abbrev_die_table. */
27098 vec_alloc (abbrev_die_table, 256);
27099 /* Zero-th entry is allocated, but unused. */
27100 abbrev_die_table->quick_push (NULL);
27102 /* Allocate the dwarf_proc_stack_usage_map. */
27103 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27105 /* Allocate the pubtypes and pubnames vectors. */
27106 vec_alloc (pubname_table, 32);
27107 vec_alloc (pubtype_table, 32);
27109 vec_alloc (incomplete_types, 64);
27111 vec_alloc (used_rtx_array, 32);
27113 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27114 vec_alloc (macinfo_table, 64);
27115 #endif
27117 /* If front-ends already registered a main translation unit but we were not
27118 ready to perform the association, do this now. */
27119 if (main_translation_unit != NULL_TREE)
27120 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27123 /* Called before compile () starts outputtting functions, variables
27124 and toplevel asms into assembly. */
27126 static void
27127 dwarf2out_assembly_start (void)
27129 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27130 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27131 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27132 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27133 COLD_TEXT_SECTION_LABEL, 0);
27134 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27136 switch_to_section (text_section);
27137 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27138 #endif
27140 /* Make sure the line number table for .text always exists. */
27141 text_section_line_info = new_line_info_table ();
27142 text_section_line_info->end_label = text_end_label;
27144 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27145 cur_line_info_table = text_section_line_info;
27146 #endif
27148 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27149 && dwarf2out_do_cfi_asm ()
27150 && (!(flag_unwind_tables || flag_exceptions)
27151 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27152 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27155 /* A helper function for dwarf2out_finish called through
27156 htab_traverse. Assign a string its index. All strings must be
27157 collected into the table by the time index_string is called,
27158 because the indexing code relies on htab_traverse to traverse nodes
27159 in the same order for each run. */
27162 index_string (indirect_string_node **h, unsigned int *index)
27164 indirect_string_node *node = *h;
27166 find_string_form (node);
27167 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27169 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27170 node->index = *index;
27171 *index += 1;
27173 return 1;
27176 /* A helper function for output_indirect_strings called through
27177 htab_traverse. Output the offset to a string and update the
27178 current offset. */
27181 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27183 indirect_string_node *node = *h;
27185 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27187 /* Assert that this node has been assigned an index. */
27188 gcc_assert (node->index != NO_INDEX_ASSIGNED
27189 && node->index != NOT_INDEXED);
27190 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27191 "indexed string 0x%x: %s", node->index, node->str);
27192 *offset += strlen (node->str) + 1;
27194 return 1;
27197 /* A helper function for dwarf2out_finish called through
27198 htab_traverse. Output the indexed string. */
27201 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27203 struct indirect_string_node *node = *h;
27205 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27207 /* Assert that the strings are output in the same order as their
27208 indexes were assigned. */
27209 gcc_assert (*cur_idx == node->index);
27210 assemble_string (node->str, strlen (node->str) + 1);
27211 *cur_idx += 1;
27213 return 1;
27216 /* A helper function for dwarf2out_finish called through
27217 htab_traverse. Emit one queued .debug_str string. */
27220 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27222 struct indirect_string_node *node = *h;
27224 node->form = find_string_form (node);
27225 if (node->form == form && node->refcount > 0)
27227 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27228 assemble_string (node->str, strlen (node->str) + 1);
27231 return 1;
27234 /* Output the indexed string table. */
27236 static void
27237 output_indirect_strings (void)
27239 switch_to_section (debug_str_section);
27240 if (!dwarf_split_debug_info)
27241 debug_str_hash->traverse<enum dwarf_form,
27242 output_indirect_string> (DW_FORM_strp);
27243 else
27245 unsigned int offset = 0;
27246 unsigned int cur_idx = 0;
27248 skeleton_debug_str_hash->traverse<enum dwarf_form,
27249 output_indirect_string> (DW_FORM_strp);
27251 switch_to_section (debug_str_offsets_section);
27252 debug_str_hash->traverse_noresize
27253 <unsigned int *, output_index_string_offset> (&offset);
27254 switch_to_section (debug_str_dwo_section);
27255 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27256 (&cur_idx);
27260 /* Callback for htab_traverse to assign an index to an entry in the
27261 table, and to write that entry to the .debug_addr section. */
27264 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27266 addr_table_entry *entry = *slot;
27268 if (entry->refcount == 0)
27270 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27271 || entry->index == NOT_INDEXED);
27272 return 1;
27275 gcc_assert (entry->index == *cur_index);
27276 (*cur_index)++;
27278 switch (entry->kind)
27280 case ate_kind_rtx:
27281 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27282 "0x%x", entry->index);
27283 break;
27284 case ate_kind_rtx_dtprel:
27285 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27286 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27287 DWARF2_ADDR_SIZE,
27288 entry->addr.rtl);
27289 fputc ('\n', asm_out_file);
27290 break;
27291 case ate_kind_label:
27292 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27293 "0x%x", entry->index);
27294 break;
27295 default:
27296 gcc_unreachable ();
27298 return 1;
27301 /* Produce the .debug_addr section. */
27303 static void
27304 output_addr_table (void)
27306 unsigned int index = 0;
27307 if (addr_index_table == NULL || addr_index_table->size () == 0)
27308 return;
27310 switch_to_section (debug_addr_section);
27311 addr_index_table
27312 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27315 #if ENABLE_ASSERT_CHECKING
27316 /* Verify that all marks are clear. */
27318 static void
27319 verify_marks_clear (dw_die_ref die)
27321 dw_die_ref c;
27323 gcc_assert (! die->die_mark);
27324 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27326 #endif /* ENABLE_ASSERT_CHECKING */
27328 /* Clear the marks for a die and its children.
27329 Be cool if the mark isn't set. */
27331 static void
27332 prune_unmark_dies (dw_die_ref die)
27334 dw_die_ref c;
27336 if (die->die_mark)
27337 die->die_mark = 0;
27338 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27341 /* Given LOC that is referenced by a DIE we're marking as used, find all
27342 referenced DWARF procedures it references and mark them as used. */
27344 static void
27345 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27347 for (; loc != NULL; loc = loc->dw_loc_next)
27348 switch (loc->dw_loc_opc)
27350 case DW_OP_implicit_pointer:
27351 case DW_OP_convert:
27352 case DW_OP_reinterpret:
27353 case DW_OP_GNU_implicit_pointer:
27354 case DW_OP_GNU_convert:
27355 case DW_OP_GNU_reinterpret:
27356 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27357 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27358 break;
27359 case DW_OP_call2:
27360 case DW_OP_call4:
27361 case DW_OP_call_ref:
27362 case DW_OP_const_type:
27363 case DW_OP_GNU_const_type:
27364 case DW_OP_GNU_parameter_ref:
27365 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27366 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27367 break;
27368 case DW_OP_regval_type:
27369 case DW_OP_deref_type:
27370 case DW_OP_GNU_regval_type:
27371 case DW_OP_GNU_deref_type:
27372 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27373 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27374 break;
27375 case DW_OP_entry_value:
27376 case DW_OP_GNU_entry_value:
27377 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27378 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27379 break;
27380 default:
27381 break;
27385 /* Given DIE that we're marking as used, find any other dies
27386 it references as attributes and mark them as used. */
27388 static void
27389 prune_unused_types_walk_attribs (dw_die_ref die)
27391 dw_attr_node *a;
27392 unsigned ix;
27394 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27396 switch (AT_class (a))
27398 /* Make sure DWARF procedures referenced by location descriptions will
27399 get emitted. */
27400 case dw_val_class_loc:
27401 prune_unused_types_walk_loc_descr (AT_loc (a));
27402 break;
27403 case dw_val_class_loc_list:
27404 for (dw_loc_list_ref list = AT_loc_list (a);
27405 list != NULL;
27406 list = list->dw_loc_next)
27407 prune_unused_types_walk_loc_descr (list->expr);
27408 break;
27410 case dw_val_class_die_ref:
27411 /* A reference to another DIE.
27412 Make sure that it will get emitted.
27413 If it was broken out into a comdat group, don't follow it. */
27414 if (! AT_ref (a)->comdat_type_p
27415 || a->dw_attr == DW_AT_specification)
27416 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27417 break;
27419 case dw_val_class_str:
27420 /* Set the string's refcount to 0 so that prune_unused_types_mark
27421 accounts properly for it. */
27422 a->dw_attr_val.v.val_str->refcount = 0;
27423 break;
27425 default:
27426 break;
27431 /* Mark the generic parameters and arguments children DIEs of DIE. */
27433 static void
27434 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27436 dw_die_ref c;
27438 if (die == NULL || die->die_child == NULL)
27439 return;
27440 c = die->die_child;
27443 if (is_template_parameter (c))
27444 prune_unused_types_mark (c, 1);
27445 c = c->die_sib;
27446 } while (c && c != die->die_child);
27449 /* Mark DIE as being used. If DOKIDS is true, then walk down
27450 to DIE's children. */
27452 static void
27453 prune_unused_types_mark (dw_die_ref die, int dokids)
27455 dw_die_ref c;
27457 if (die->die_mark == 0)
27459 /* We haven't done this node yet. Mark it as used. */
27460 die->die_mark = 1;
27461 /* If this is the DIE of a generic type instantiation,
27462 mark the children DIEs that describe its generic parms and
27463 args. */
27464 prune_unused_types_mark_generic_parms_dies (die);
27466 /* We also have to mark its parents as used.
27467 (But we don't want to mark our parent's kids due to this,
27468 unless it is a class.) */
27469 if (die->die_parent)
27470 prune_unused_types_mark (die->die_parent,
27471 class_scope_p (die->die_parent));
27473 /* Mark any referenced nodes. */
27474 prune_unused_types_walk_attribs (die);
27476 /* If this node is a specification,
27477 also mark the definition, if it exists. */
27478 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27479 prune_unused_types_mark (die->die_definition, 1);
27482 if (dokids && die->die_mark != 2)
27484 /* We need to walk the children, but haven't done so yet.
27485 Remember that we've walked the kids. */
27486 die->die_mark = 2;
27488 /* If this is an array type, we need to make sure our
27489 kids get marked, even if they're types. If we're
27490 breaking out types into comdat sections, do this
27491 for all type definitions. */
27492 if (die->die_tag == DW_TAG_array_type
27493 || (use_debug_types
27494 && is_type_die (die) && ! is_declaration_die (die)))
27495 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27496 else
27497 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27501 /* For local classes, look if any static member functions were emitted
27502 and if so, mark them. */
27504 static void
27505 prune_unused_types_walk_local_classes (dw_die_ref die)
27507 dw_die_ref c;
27509 if (die->die_mark == 2)
27510 return;
27512 switch (die->die_tag)
27514 case DW_TAG_structure_type:
27515 case DW_TAG_union_type:
27516 case DW_TAG_class_type:
27517 break;
27519 case DW_TAG_subprogram:
27520 if (!get_AT_flag (die, DW_AT_declaration)
27521 || die->die_definition != NULL)
27522 prune_unused_types_mark (die, 1);
27523 return;
27525 default:
27526 return;
27529 /* Mark children. */
27530 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27533 /* Walk the tree DIE and mark types that we actually use. */
27535 static void
27536 prune_unused_types_walk (dw_die_ref die)
27538 dw_die_ref c;
27540 /* Don't do anything if this node is already marked and
27541 children have been marked as well. */
27542 if (die->die_mark == 2)
27543 return;
27545 switch (die->die_tag)
27547 case DW_TAG_structure_type:
27548 case DW_TAG_union_type:
27549 case DW_TAG_class_type:
27550 if (die->die_perennial_p)
27551 break;
27553 for (c = die->die_parent; c; c = c->die_parent)
27554 if (c->die_tag == DW_TAG_subprogram)
27555 break;
27557 /* Finding used static member functions inside of classes
27558 is needed just for local classes, because for other classes
27559 static member function DIEs with DW_AT_specification
27560 are emitted outside of the DW_TAG_*_type. If we ever change
27561 it, we'd need to call this even for non-local classes. */
27562 if (c)
27563 prune_unused_types_walk_local_classes (die);
27565 /* It's a type node --- don't mark it. */
27566 return;
27568 case DW_TAG_const_type:
27569 case DW_TAG_packed_type:
27570 case DW_TAG_pointer_type:
27571 case DW_TAG_reference_type:
27572 case DW_TAG_rvalue_reference_type:
27573 case DW_TAG_volatile_type:
27574 case DW_TAG_typedef:
27575 case DW_TAG_array_type:
27576 case DW_TAG_interface_type:
27577 case DW_TAG_friend:
27578 case DW_TAG_enumeration_type:
27579 case DW_TAG_subroutine_type:
27580 case DW_TAG_string_type:
27581 case DW_TAG_set_type:
27582 case DW_TAG_subrange_type:
27583 case DW_TAG_ptr_to_member_type:
27584 case DW_TAG_file_type:
27585 /* Type nodes are useful only when other DIEs reference them --- don't
27586 mark them. */
27587 /* FALLTHROUGH */
27589 case DW_TAG_dwarf_procedure:
27590 /* Likewise for DWARF procedures. */
27592 if (die->die_perennial_p)
27593 break;
27595 return;
27597 default:
27598 /* Mark everything else. */
27599 break;
27602 if (die->die_mark == 0)
27604 die->die_mark = 1;
27606 /* Now, mark any dies referenced from here. */
27607 prune_unused_types_walk_attribs (die);
27610 die->die_mark = 2;
27612 /* Mark children. */
27613 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27616 /* Increment the string counts on strings referred to from DIE's
27617 attributes. */
27619 static void
27620 prune_unused_types_update_strings (dw_die_ref die)
27622 dw_attr_node *a;
27623 unsigned ix;
27625 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27626 if (AT_class (a) == dw_val_class_str)
27628 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27629 s->refcount++;
27630 /* Avoid unnecessarily putting strings that are used less than
27631 twice in the hash table. */
27632 if (s->refcount
27633 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27635 indirect_string_node **slot
27636 = debug_str_hash->find_slot_with_hash (s->str,
27637 htab_hash_string (s->str),
27638 INSERT);
27639 gcc_assert (*slot == NULL);
27640 *slot = s;
27645 /* Mark DIE and its children as removed. */
27647 static void
27648 mark_removed (dw_die_ref die)
27650 dw_die_ref c;
27651 die->removed = true;
27652 FOR_EACH_CHILD (die, c, mark_removed (c));
27655 /* Remove from the tree DIE any dies that aren't marked. */
27657 static void
27658 prune_unused_types_prune (dw_die_ref die)
27660 dw_die_ref c;
27662 gcc_assert (die->die_mark);
27663 prune_unused_types_update_strings (die);
27665 if (! die->die_child)
27666 return;
27668 c = die->die_child;
27669 do {
27670 dw_die_ref prev = c, next;
27671 for (c = c->die_sib; ! c->die_mark; c = next)
27672 if (c == die->die_child)
27674 /* No marked children between 'prev' and the end of the list. */
27675 if (prev == c)
27676 /* No marked children at all. */
27677 die->die_child = NULL;
27678 else
27680 prev->die_sib = c->die_sib;
27681 die->die_child = prev;
27683 c->die_sib = NULL;
27684 mark_removed (c);
27685 return;
27687 else
27689 next = c->die_sib;
27690 c->die_sib = NULL;
27691 mark_removed (c);
27694 if (c != prev->die_sib)
27695 prev->die_sib = c;
27696 prune_unused_types_prune (c);
27697 } while (c != die->die_child);
27700 /* Remove dies representing declarations that we never use. */
27702 static void
27703 prune_unused_types (void)
27705 unsigned int i;
27706 limbo_die_node *node;
27707 comdat_type_node *ctnode;
27708 pubname_entry *pub;
27709 dw_die_ref base_type;
27711 #if ENABLE_ASSERT_CHECKING
27712 /* All the marks should already be clear. */
27713 verify_marks_clear (comp_unit_die ());
27714 for (node = limbo_die_list; node; node = node->next)
27715 verify_marks_clear (node->die);
27716 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27717 verify_marks_clear (ctnode->root_die);
27718 #endif /* ENABLE_ASSERT_CHECKING */
27720 /* Mark types that are used in global variables. */
27721 premark_types_used_by_global_vars ();
27723 /* Set the mark on nodes that are actually used. */
27724 prune_unused_types_walk (comp_unit_die ());
27725 for (node = limbo_die_list; node; node = node->next)
27726 prune_unused_types_walk (node->die);
27727 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27729 prune_unused_types_walk (ctnode->root_die);
27730 prune_unused_types_mark (ctnode->type_die, 1);
27733 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27734 are unusual in that they are pubnames that are the children of pubtypes.
27735 They should only be marked via their parent DW_TAG_enumeration_type die,
27736 not as roots in themselves. */
27737 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27738 if (pub->die->die_tag != DW_TAG_enumerator)
27739 prune_unused_types_mark (pub->die, 1);
27740 for (i = 0; base_types.iterate (i, &base_type); i++)
27741 prune_unused_types_mark (base_type, 1);
27743 if (debug_str_hash)
27744 debug_str_hash->empty ();
27745 if (skeleton_debug_str_hash)
27746 skeleton_debug_str_hash->empty ();
27747 prune_unused_types_prune (comp_unit_die ());
27748 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27750 node = *pnode;
27751 if (!node->die->die_mark)
27752 *pnode = node->next;
27753 else
27755 prune_unused_types_prune (node->die);
27756 pnode = &node->next;
27759 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27760 prune_unused_types_prune (ctnode->root_die);
27762 /* Leave the marks clear. */
27763 prune_unmark_dies (comp_unit_die ());
27764 for (node = limbo_die_list; node; node = node->next)
27765 prune_unmark_dies (node->die);
27766 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27767 prune_unmark_dies (ctnode->root_die);
27770 /* Helpers to manipulate hash table of comdat type units. */
27772 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27774 static inline hashval_t hash (const comdat_type_node *);
27775 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27778 inline hashval_t
27779 comdat_type_hasher::hash (const comdat_type_node *type_node)
27781 hashval_t h;
27782 memcpy (&h, type_node->signature, sizeof (h));
27783 return h;
27786 inline bool
27787 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27788 const comdat_type_node *type_node_2)
27790 return (! memcmp (type_node_1->signature, type_node_2->signature,
27791 DWARF_TYPE_SIGNATURE_SIZE));
27794 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
27795 to the location it would have been added, should we know its
27796 DECL_ASSEMBLER_NAME when we added other attributes. This will
27797 probably improve compactness of debug info, removing equivalent
27798 abbrevs, and hide any differences caused by deferring the
27799 computation of the assembler name, triggered by e.g. PCH. */
27801 static inline void
27802 move_linkage_attr (dw_die_ref die)
27804 unsigned ix = vec_safe_length (die->die_attr);
27805 dw_attr_node linkage = (*die->die_attr)[ix - 1];
27807 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
27808 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
27810 while (--ix > 0)
27812 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
27814 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
27815 break;
27818 if (ix != vec_safe_length (die->die_attr) - 1)
27820 die->die_attr->pop ();
27821 die->die_attr->quick_insert (ix, linkage);
27825 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
27826 referenced from typed stack ops and count how often they are used. */
27828 static void
27829 mark_base_types (dw_loc_descr_ref loc)
27831 dw_die_ref base_type = NULL;
27833 for (; loc; loc = loc->dw_loc_next)
27835 switch (loc->dw_loc_opc)
27837 case DW_OP_regval_type:
27838 case DW_OP_deref_type:
27839 case DW_OP_GNU_regval_type:
27840 case DW_OP_GNU_deref_type:
27841 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
27842 break;
27843 case DW_OP_convert:
27844 case DW_OP_reinterpret:
27845 case DW_OP_GNU_convert:
27846 case DW_OP_GNU_reinterpret:
27847 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
27848 continue;
27849 /* FALLTHRU */
27850 case DW_OP_const_type:
27851 case DW_OP_GNU_const_type:
27852 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
27853 break;
27854 case DW_OP_entry_value:
27855 case DW_OP_GNU_entry_value:
27856 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
27857 continue;
27858 default:
27859 continue;
27861 gcc_assert (base_type->die_parent == comp_unit_die ());
27862 if (base_type->die_mark)
27863 base_type->die_mark++;
27864 else
27866 base_types.safe_push (base_type);
27867 base_type->die_mark = 1;
27872 /* Comparison function for sorting marked base types. */
27874 static int
27875 base_type_cmp (const void *x, const void *y)
27877 dw_die_ref dx = *(const dw_die_ref *) x;
27878 dw_die_ref dy = *(const dw_die_ref *) y;
27879 unsigned int byte_size1, byte_size2;
27880 unsigned int encoding1, encoding2;
27881 if (dx->die_mark > dy->die_mark)
27882 return -1;
27883 if (dx->die_mark < dy->die_mark)
27884 return 1;
27885 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
27886 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
27887 if (byte_size1 < byte_size2)
27888 return 1;
27889 if (byte_size1 > byte_size2)
27890 return -1;
27891 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
27892 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
27893 if (encoding1 < encoding2)
27894 return 1;
27895 if (encoding1 > encoding2)
27896 return -1;
27897 return 0;
27900 /* Move base types marked by mark_base_types as early as possible
27901 in the CU, sorted by decreasing usage count both to make the
27902 uleb128 references as small as possible and to make sure they
27903 will have die_offset already computed by calc_die_sizes when
27904 sizes of typed stack loc ops is computed. */
27906 static void
27907 move_marked_base_types (void)
27909 unsigned int i;
27910 dw_die_ref base_type, die, c;
27912 if (base_types.is_empty ())
27913 return;
27915 /* Sort by decreasing usage count, they will be added again in that
27916 order later on. */
27917 base_types.qsort (base_type_cmp);
27918 die = comp_unit_die ();
27919 c = die->die_child;
27922 dw_die_ref prev = c;
27923 c = c->die_sib;
27924 while (c->die_mark)
27926 remove_child_with_prev (c, prev);
27927 /* As base types got marked, there must be at least
27928 one node other than DW_TAG_base_type. */
27929 gcc_assert (die->die_child != NULL);
27930 c = prev->die_sib;
27933 while (c != die->die_child);
27934 gcc_assert (die->die_child);
27935 c = die->die_child;
27936 for (i = 0; base_types.iterate (i, &base_type); i++)
27938 base_type->die_mark = 0;
27939 base_type->die_sib = c->die_sib;
27940 c->die_sib = base_type;
27941 c = base_type;
27945 /* Helper function for resolve_addr, attempt to resolve
27946 one CONST_STRING, return true if successful. Similarly verify that
27947 SYMBOL_REFs refer to variables emitted in the current CU. */
27949 static bool
27950 resolve_one_addr (rtx *addr)
27952 rtx rtl = *addr;
27954 if (GET_CODE (rtl) == CONST_STRING)
27956 size_t len = strlen (XSTR (rtl, 0)) + 1;
27957 tree t = build_string (len, XSTR (rtl, 0));
27958 tree tlen = size_int (len - 1);
27959 TREE_TYPE (t)
27960 = build_array_type (char_type_node, build_index_type (tlen));
27961 rtl = lookup_constant_def (t);
27962 if (!rtl || !MEM_P (rtl))
27963 return false;
27964 rtl = XEXP (rtl, 0);
27965 if (GET_CODE (rtl) == SYMBOL_REF
27966 && SYMBOL_REF_DECL (rtl)
27967 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
27968 return false;
27969 vec_safe_push (used_rtx_array, rtl);
27970 *addr = rtl;
27971 return true;
27974 if (GET_CODE (rtl) == SYMBOL_REF
27975 && SYMBOL_REF_DECL (rtl))
27977 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
27979 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
27980 return false;
27982 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
27983 return false;
27986 if (GET_CODE (rtl) == CONST)
27988 subrtx_ptr_iterator::array_type array;
27989 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
27990 if (!resolve_one_addr (*iter))
27991 return false;
27994 return true;
27997 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
27998 if possible, and create DW_TAG_dwarf_procedure that can be referenced
27999 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28001 static rtx
28002 string_cst_pool_decl (tree t)
28004 rtx rtl = output_constant_def (t, 1);
28005 unsigned char *array;
28006 dw_loc_descr_ref l;
28007 tree decl;
28008 size_t len;
28009 dw_die_ref ref;
28011 if (!rtl || !MEM_P (rtl))
28012 return NULL_RTX;
28013 rtl = XEXP (rtl, 0);
28014 if (GET_CODE (rtl) != SYMBOL_REF
28015 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28016 return NULL_RTX;
28018 decl = SYMBOL_REF_DECL (rtl);
28019 if (!lookup_decl_die (decl))
28021 len = TREE_STRING_LENGTH (t);
28022 vec_safe_push (used_rtx_array, rtl);
28023 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28024 array = ggc_vec_alloc<unsigned char> (len);
28025 memcpy (array, TREE_STRING_POINTER (t), len);
28026 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28027 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28028 l->dw_loc_oprnd2.v.val_vec.length = len;
28029 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28030 l->dw_loc_oprnd2.v.val_vec.array = array;
28031 add_AT_loc (ref, DW_AT_location, l);
28032 equate_decl_number_to_die (decl, ref);
28034 return rtl;
28037 /* Helper function of resolve_addr_in_expr. LOC is
28038 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28039 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28040 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28041 with DW_OP_implicit_pointer if possible
28042 and return true, if unsuccessful, return false. */
28044 static bool
28045 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28047 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28048 HOST_WIDE_INT offset = 0;
28049 dw_die_ref ref = NULL;
28050 tree decl;
28052 if (GET_CODE (rtl) == CONST
28053 && GET_CODE (XEXP (rtl, 0)) == PLUS
28054 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28056 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28057 rtl = XEXP (XEXP (rtl, 0), 0);
28059 if (GET_CODE (rtl) == CONST_STRING)
28061 size_t len = strlen (XSTR (rtl, 0)) + 1;
28062 tree t = build_string (len, XSTR (rtl, 0));
28063 tree tlen = size_int (len - 1);
28065 TREE_TYPE (t)
28066 = build_array_type (char_type_node, build_index_type (tlen));
28067 rtl = string_cst_pool_decl (t);
28068 if (!rtl)
28069 return false;
28071 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28073 decl = SYMBOL_REF_DECL (rtl);
28074 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28076 ref = lookup_decl_die (decl);
28077 if (ref && (get_AT (ref, DW_AT_location)
28078 || get_AT (ref, DW_AT_const_value)))
28080 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28081 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28082 loc->dw_loc_oprnd1.val_entry = NULL;
28083 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28084 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28085 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28086 loc->dw_loc_oprnd2.v.val_int = offset;
28087 return true;
28091 return false;
28094 /* Helper function for resolve_addr, handle one location
28095 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28096 the location list couldn't be resolved. */
28098 static bool
28099 resolve_addr_in_expr (dw_loc_descr_ref loc)
28101 dw_loc_descr_ref keep = NULL;
28102 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28103 switch (loc->dw_loc_opc)
28105 case DW_OP_addr:
28106 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28108 if ((prev == NULL
28109 || prev->dw_loc_opc == DW_OP_piece
28110 || prev->dw_loc_opc == DW_OP_bit_piece)
28111 && loc->dw_loc_next
28112 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28113 && (!dwarf_strict || dwarf_version >= 5)
28114 && optimize_one_addr_into_implicit_ptr (loc))
28115 break;
28116 return false;
28118 break;
28119 case DW_OP_GNU_addr_index:
28120 case DW_OP_GNU_const_index:
28121 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28122 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28124 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28125 if (!resolve_one_addr (&rtl))
28126 return false;
28127 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28128 loc->dw_loc_oprnd1.val_entry
28129 = add_addr_table_entry (rtl, ate_kind_rtx);
28131 break;
28132 case DW_OP_const4u:
28133 case DW_OP_const8u:
28134 if (loc->dtprel
28135 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28136 return false;
28137 break;
28138 case DW_OP_plus_uconst:
28139 if (size_of_loc_descr (loc)
28140 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28142 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28144 dw_loc_descr_ref repl
28145 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28146 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28147 add_loc_descr (&repl, loc->dw_loc_next);
28148 *loc = *repl;
28150 break;
28151 case DW_OP_implicit_value:
28152 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28153 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28154 return false;
28155 break;
28156 case DW_OP_implicit_pointer:
28157 case DW_OP_GNU_implicit_pointer:
28158 case DW_OP_GNU_parameter_ref:
28159 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28161 dw_die_ref ref
28162 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28163 if (ref == NULL)
28164 return false;
28165 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28166 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28167 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28169 break;
28170 case DW_OP_const_type:
28171 case DW_OP_regval_type:
28172 case DW_OP_deref_type:
28173 case DW_OP_convert:
28174 case DW_OP_reinterpret:
28175 case DW_OP_GNU_const_type:
28176 case DW_OP_GNU_regval_type:
28177 case DW_OP_GNU_deref_type:
28178 case DW_OP_GNU_convert:
28179 case DW_OP_GNU_reinterpret:
28180 while (loc->dw_loc_next
28181 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28182 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28184 dw_die_ref base1, base2;
28185 unsigned enc1, enc2, size1, size2;
28186 if (loc->dw_loc_opc == DW_OP_regval_type
28187 || loc->dw_loc_opc == DW_OP_deref_type
28188 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28189 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28190 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28191 else if (loc->dw_loc_oprnd1.val_class
28192 == dw_val_class_unsigned_const)
28193 break;
28194 else
28195 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28196 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28197 == dw_val_class_unsigned_const)
28198 break;
28199 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28200 gcc_assert (base1->die_tag == DW_TAG_base_type
28201 && base2->die_tag == DW_TAG_base_type);
28202 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28203 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28204 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28205 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28206 if (size1 == size2
28207 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28208 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28209 && loc != keep)
28210 || enc1 == enc2))
28212 /* Optimize away next DW_OP_convert after
28213 adjusting LOC's base type die reference. */
28214 if (loc->dw_loc_opc == DW_OP_regval_type
28215 || loc->dw_loc_opc == DW_OP_deref_type
28216 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28217 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28218 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28219 else
28220 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28221 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28222 continue;
28224 /* Don't change integer DW_OP_convert after e.g. floating
28225 point typed stack entry. */
28226 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28227 keep = loc->dw_loc_next;
28228 break;
28230 break;
28231 default:
28232 break;
28234 return true;
28237 /* Helper function of resolve_addr. DIE had DW_AT_location of
28238 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28239 and DW_OP_addr couldn't be resolved. resolve_addr has already
28240 removed the DW_AT_location attribute. This function attempts to
28241 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28242 to it or DW_AT_const_value attribute, if possible. */
28244 static void
28245 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28247 if (!VAR_P (decl)
28248 || lookup_decl_die (decl) != die
28249 || DECL_EXTERNAL (decl)
28250 || !TREE_STATIC (decl)
28251 || DECL_INITIAL (decl) == NULL_TREE
28252 || DECL_P (DECL_INITIAL (decl))
28253 || get_AT (die, DW_AT_const_value))
28254 return;
28256 tree init = DECL_INITIAL (decl);
28257 HOST_WIDE_INT offset = 0;
28258 /* For variables that have been optimized away and thus
28259 don't have a memory location, see if we can emit
28260 DW_AT_const_value instead. */
28261 if (tree_add_const_value_attribute (die, init))
28262 return;
28263 if (dwarf_strict && dwarf_version < 5)
28264 return;
28265 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28266 and ADDR_EXPR refers to a decl that has DW_AT_location or
28267 DW_AT_const_value (but isn't addressable, otherwise
28268 resolving the original DW_OP_addr wouldn't fail), see if
28269 we can add DW_OP_implicit_pointer. */
28270 STRIP_NOPS (init);
28271 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28272 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28274 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28275 init = TREE_OPERAND (init, 0);
28276 STRIP_NOPS (init);
28278 if (TREE_CODE (init) != ADDR_EXPR)
28279 return;
28280 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28281 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28282 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28283 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28284 && TREE_OPERAND (init, 0) != decl))
28286 dw_die_ref ref;
28287 dw_loc_descr_ref l;
28289 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28291 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28292 if (!rtl)
28293 return;
28294 decl = SYMBOL_REF_DECL (rtl);
28296 else
28297 decl = TREE_OPERAND (init, 0);
28298 ref = lookup_decl_die (decl);
28299 if (ref == NULL
28300 || (!get_AT (ref, DW_AT_location)
28301 && !get_AT (ref, DW_AT_const_value)))
28302 return;
28303 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28304 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28305 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28306 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28307 add_AT_loc (die, DW_AT_location, l);
28311 /* Return NULL if l is a DWARF expression, or first op that is not
28312 valid DWARF expression. */
28314 static dw_loc_descr_ref
28315 non_dwarf_expression (dw_loc_descr_ref l)
28317 while (l)
28319 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28320 return l;
28321 switch (l->dw_loc_opc)
28323 case DW_OP_regx:
28324 case DW_OP_implicit_value:
28325 case DW_OP_stack_value:
28326 case DW_OP_implicit_pointer:
28327 case DW_OP_GNU_implicit_pointer:
28328 case DW_OP_GNU_parameter_ref:
28329 case DW_OP_piece:
28330 case DW_OP_bit_piece:
28331 return l;
28332 default:
28333 break;
28335 l = l->dw_loc_next;
28337 return NULL;
28340 /* Return adjusted copy of EXPR:
28341 If it is empty DWARF expression, return it.
28342 If it is valid non-empty DWARF expression,
28343 return copy of EXPR with copy of DEREF appended to it.
28344 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28345 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
28346 and no DEREF.
28347 If it is DWARF expression followed by DW_OP_stack_value, return
28348 copy of the DWARF expression without anything appended.
28349 Otherwise, return NULL. */
28351 static dw_loc_descr_ref
28352 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
28355 if (expr == NULL)
28356 return NULL;
28358 dw_loc_descr_ref l = non_dwarf_expression (expr);
28359 if (l && l->dw_loc_next)
28360 return NULL;
28362 if (l)
28364 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28365 deref = new_loc_descr ((enum dwarf_location_atom)
28366 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28367 0, 0);
28368 else
28369 switch (l->dw_loc_opc)
28371 case DW_OP_regx:
28372 deref = new_loc_descr (DW_OP_bregx,
28373 l->dw_loc_oprnd1.v.val_unsigned, 0);
28374 break;
28375 case DW_OP_stack_value:
28376 deref = NULL;
28377 break;
28378 default:
28379 return NULL;
28382 else
28383 deref = new_loc_descr (deref->dw_loc_opc,
28384 deref->dw_loc_oprnd1.v.val_int, 0);
28386 dw_loc_descr_ref ret = NULL, *p = &ret;
28387 while (expr != l)
28389 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28390 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28391 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28392 p = &(*p)->dw_loc_next;
28393 expr = expr->dw_loc_next;
28395 *p = deref;
28396 return ret;
28399 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
28400 or argument, adjust it if needed and return:
28401 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28402 attribute if present should be removed
28403 0 keep the attribute as is if the referenced var or argument has
28404 only DWARF expression that covers all ranges
28405 1 if the attribute has been successfully adjusted. */
28407 static int
28408 optimize_string_length (dw_attr_node *a)
28410 dw_loc_descr_ref l = AT_loc (a), lv;
28411 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
28412 dw_attr_node *av = get_AT (die, DW_AT_location);
28413 dw_loc_list_ref d;
28414 bool non_dwarf_expr = false;
28416 if (av == NULL)
28417 return -1;
28418 switch (AT_class (av))
28420 case dw_val_class_loc_list:
28421 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28422 if (d->expr && non_dwarf_expression (d->expr))
28423 non_dwarf_expr = true;
28424 break;
28425 case dw_val_class_loc:
28426 lv = AT_loc (av);
28427 if (lv == NULL)
28428 return -1;
28429 if (non_dwarf_expression (lv))
28430 non_dwarf_expr = true;
28431 break;
28432 default:
28433 return -1;
28436 /* If it is safe to keep DW_OP_call4 in, keep it. */
28437 if (!non_dwarf_expr
28438 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
28439 return 0;
28441 /* If not dereferencing the DW_OP_call4 afterwards, we can just
28442 copy over the DW_AT_location attribute from die to a. */
28443 if (l->dw_loc_next == NULL)
28445 a->dw_attr_val = av->dw_attr_val;
28446 return 1;
28449 dw_loc_list_ref list, *p;
28450 switch (AT_class (av))
28452 case dw_val_class_loc_list:
28453 p = &list;
28454 list = NULL;
28455 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28457 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
28458 if (lv)
28460 *p = new_loc_list (lv, d->begin, d->end, d->section);
28461 p = &(*p)->dw_loc_next;
28464 if (list == NULL)
28465 return -1;
28466 a->dw_attr_val.val_class = dw_val_class_loc_list;
28467 gen_llsym (list);
28468 *AT_loc_list_ptr (a) = list;
28469 return 1;
28470 case dw_val_class_loc:
28471 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
28472 if (lv == NULL)
28473 return -1;
28474 a->dw_attr_val.v.val_loc = lv;
28475 return 1;
28476 default:
28477 gcc_unreachable ();
28481 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28482 an address in .rodata section if the string literal is emitted there,
28483 or remove the containing location list or replace DW_AT_const_value
28484 with DW_AT_location and empty location expression, if it isn't found
28485 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28486 to something that has been emitted in the current CU. */
28488 static void
28489 resolve_addr (dw_die_ref die)
28491 dw_die_ref c;
28492 dw_attr_node *a;
28493 dw_loc_list_ref *curr, *start, loc;
28494 unsigned ix;
28495 bool remove_AT_byte_size = false;
28497 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28498 switch (AT_class (a))
28500 case dw_val_class_loc_list:
28501 start = curr = AT_loc_list_ptr (a);
28502 loc = *curr;
28503 gcc_assert (loc);
28504 /* The same list can be referenced more than once. See if we have
28505 already recorded the result from a previous pass. */
28506 if (loc->replaced)
28507 *curr = loc->dw_loc_next;
28508 else if (!loc->resolved_addr)
28510 /* As things stand, we do not expect or allow one die to
28511 reference a suffix of another die's location list chain.
28512 References must be identical or completely separate.
28513 There is therefore no need to cache the result of this
28514 pass on any list other than the first; doing so
28515 would lead to unnecessary writes. */
28516 while (*curr)
28518 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28519 if (!resolve_addr_in_expr ((*curr)->expr))
28521 dw_loc_list_ref next = (*curr)->dw_loc_next;
28522 dw_loc_descr_ref l = (*curr)->expr;
28524 if (next && (*curr)->ll_symbol)
28526 gcc_assert (!next->ll_symbol);
28527 next->ll_symbol = (*curr)->ll_symbol;
28529 if (dwarf_split_debug_info)
28530 remove_loc_list_addr_table_entries (l);
28531 *curr = next;
28533 else
28535 mark_base_types ((*curr)->expr);
28536 curr = &(*curr)->dw_loc_next;
28539 if (loc == *start)
28540 loc->resolved_addr = 1;
28541 else
28543 loc->replaced = 1;
28544 loc->dw_loc_next = *start;
28547 if (!*start)
28549 remove_AT (die, a->dw_attr);
28550 ix--;
28552 break;
28553 case dw_val_class_loc:
28555 dw_loc_descr_ref l = AT_loc (a);
28556 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
28557 DW_AT_string_length is only a rough approximation; unfortunately
28558 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
28559 needs a DWARF expression, while DW_AT_location of the referenced
28560 variable or argument might be any location description. */
28561 if (a->dw_attr == DW_AT_string_length
28562 && l
28563 && l->dw_loc_opc == DW_OP_call4
28564 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
28565 && (l->dw_loc_next == NULL
28566 || (l->dw_loc_next->dw_loc_next == NULL
28567 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
28568 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
28570 switch (optimize_string_length (a))
28572 case -1:
28573 remove_AT (die, a->dw_attr);
28574 ix--;
28575 /* If we drop DW_AT_string_length, we need to drop also
28576 DW_AT_{string_length_,}byte_size. */
28577 remove_AT_byte_size = true;
28578 continue;
28579 default:
28580 break;
28581 case 1:
28582 /* Even if we keep the optimized DW_AT_string_length,
28583 it might have changed AT_class, so process it again. */
28584 ix--;
28585 continue;
28588 /* For -gdwarf-2 don't attempt to optimize
28589 DW_AT_data_member_location containing
28590 DW_OP_plus_uconst - older consumers might
28591 rely on it being that op instead of a more complex,
28592 but shorter, location description. */
28593 if ((dwarf_version > 2
28594 || a->dw_attr != DW_AT_data_member_location
28595 || l == NULL
28596 || l->dw_loc_opc != DW_OP_plus_uconst
28597 || l->dw_loc_next != NULL)
28598 && !resolve_addr_in_expr (l))
28600 if (dwarf_split_debug_info)
28601 remove_loc_list_addr_table_entries (l);
28602 if (l != NULL
28603 && l->dw_loc_next == NULL
28604 && l->dw_loc_opc == DW_OP_addr
28605 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28606 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28607 && a->dw_attr == DW_AT_location)
28609 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28610 remove_AT (die, a->dw_attr);
28611 ix--;
28612 optimize_location_into_implicit_ptr (die, decl);
28613 break;
28615 remove_AT (die, a->dw_attr);
28616 ix--;
28618 else
28619 mark_base_types (l);
28621 break;
28622 case dw_val_class_addr:
28623 if (a->dw_attr == DW_AT_const_value
28624 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28626 if (AT_index (a) != NOT_INDEXED)
28627 remove_addr_table_entry (a->dw_attr_val.val_entry);
28628 remove_AT (die, a->dw_attr);
28629 ix--;
28631 if ((die->die_tag == DW_TAG_call_site
28632 && a->dw_attr == DW_AT_call_origin)
28633 || (die->die_tag == DW_TAG_GNU_call_site
28634 && a->dw_attr == DW_AT_abstract_origin))
28636 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28637 dw_die_ref tdie = lookup_decl_die (tdecl);
28638 dw_die_ref cdie;
28639 if (tdie == NULL
28640 && DECL_EXTERNAL (tdecl)
28641 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28642 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28644 /* Creating a full DIE for tdecl is overly expensive and
28645 at this point even wrong when in the LTO phase
28646 as it can end up generating new type DIEs we didn't
28647 output and thus optimize_external_refs will crash. */
28648 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28649 add_AT_flag (tdie, DW_AT_external, 1);
28650 add_AT_flag (tdie, DW_AT_declaration, 1);
28651 add_linkage_attr (tdie, tdecl);
28652 add_name_and_src_coords_attributes (tdie, tdecl);
28653 equate_decl_number_to_die (tdecl, tdie);
28655 if (tdie)
28657 a->dw_attr_val.val_class = dw_val_class_die_ref;
28658 a->dw_attr_val.v.val_die_ref.die = tdie;
28659 a->dw_attr_val.v.val_die_ref.external = 0;
28661 else
28663 if (AT_index (a) != NOT_INDEXED)
28664 remove_addr_table_entry (a->dw_attr_val.val_entry);
28665 remove_AT (die, a->dw_attr);
28666 ix--;
28669 break;
28670 default:
28671 break;
28674 if (remove_AT_byte_size)
28675 remove_AT (die, dwarf_version >= 5
28676 ? DW_AT_string_length_byte_size
28677 : DW_AT_byte_size);
28679 FOR_EACH_CHILD (die, c, resolve_addr (c));
28682 /* Helper routines for optimize_location_lists.
28683 This pass tries to share identical local lists in .debug_loc
28684 section. */
28686 /* Iteratively hash operands of LOC opcode into HSTATE. */
28688 static void
28689 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28691 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28692 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28694 switch (loc->dw_loc_opc)
28696 case DW_OP_const4u:
28697 case DW_OP_const8u:
28698 if (loc->dtprel)
28699 goto hash_addr;
28700 /* FALLTHRU */
28701 case DW_OP_const1u:
28702 case DW_OP_const1s:
28703 case DW_OP_const2u:
28704 case DW_OP_const2s:
28705 case DW_OP_const4s:
28706 case DW_OP_const8s:
28707 case DW_OP_constu:
28708 case DW_OP_consts:
28709 case DW_OP_pick:
28710 case DW_OP_plus_uconst:
28711 case DW_OP_breg0:
28712 case DW_OP_breg1:
28713 case DW_OP_breg2:
28714 case DW_OP_breg3:
28715 case DW_OP_breg4:
28716 case DW_OP_breg5:
28717 case DW_OP_breg6:
28718 case DW_OP_breg7:
28719 case DW_OP_breg8:
28720 case DW_OP_breg9:
28721 case DW_OP_breg10:
28722 case DW_OP_breg11:
28723 case DW_OP_breg12:
28724 case DW_OP_breg13:
28725 case DW_OP_breg14:
28726 case DW_OP_breg15:
28727 case DW_OP_breg16:
28728 case DW_OP_breg17:
28729 case DW_OP_breg18:
28730 case DW_OP_breg19:
28731 case DW_OP_breg20:
28732 case DW_OP_breg21:
28733 case DW_OP_breg22:
28734 case DW_OP_breg23:
28735 case DW_OP_breg24:
28736 case DW_OP_breg25:
28737 case DW_OP_breg26:
28738 case DW_OP_breg27:
28739 case DW_OP_breg28:
28740 case DW_OP_breg29:
28741 case DW_OP_breg30:
28742 case DW_OP_breg31:
28743 case DW_OP_regx:
28744 case DW_OP_fbreg:
28745 case DW_OP_piece:
28746 case DW_OP_deref_size:
28747 case DW_OP_xderef_size:
28748 hstate.add_object (val1->v.val_int);
28749 break;
28750 case DW_OP_skip:
28751 case DW_OP_bra:
28753 int offset;
28755 gcc_assert (val1->val_class == dw_val_class_loc);
28756 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
28757 hstate.add_object (offset);
28759 break;
28760 case DW_OP_implicit_value:
28761 hstate.add_object (val1->v.val_unsigned);
28762 switch (val2->val_class)
28764 case dw_val_class_const:
28765 hstate.add_object (val2->v.val_int);
28766 break;
28767 case dw_val_class_vec:
28769 unsigned int elt_size = val2->v.val_vec.elt_size;
28770 unsigned int len = val2->v.val_vec.length;
28772 hstate.add_int (elt_size);
28773 hstate.add_int (len);
28774 hstate.add (val2->v.val_vec.array, len * elt_size);
28776 break;
28777 case dw_val_class_const_double:
28778 hstate.add_object (val2->v.val_double.low);
28779 hstate.add_object (val2->v.val_double.high);
28780 break;
28781 case dw_val_class_wide_int:
28782 hstate.add (val2->v.val_wide->get_val (),
28783 get_full_len (*val2->v.val_wide)
28784 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28785 break;
28786 case dw_val_class_addr:
28787 inchash::add_rtx (val2->v.val_addr, hstate);
28788 break;
28789 default:
28790 gcc_unreachable ();
28792 break;
28793 case DW_OP_bregx:
28794 case DW_OP_bit_piece:
28795 hstate.add_object (val1->v.val_int);
28796 hstate.add_object (val2->v.val_int);
28797 break;
28798 case DW_OP_addr:
28799 hash_addr:
28800 if (loc->dtprel)
28802 unsigned char dtprel = 0xd1;
28803 hstate.add_object (dtprel);
28805 inchash::add_rtx (val1->v.val_addr, hstate);
28806 break;
28807 case DW_OP_GNU_addr_index:
28808 case DW_OP_GNU_const_index:
28810 if (loc->dtprel)
28812 unsigned char dtprel = 0xd1;
28813 hstate.add_object (dtprel);
28815 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
28817 break;
28818 case DW_OP_implicit_pointer:
28819 case DW_OP_GNU_implicit_pointer:
28820 hstate.add_int (val2->v.val_int);
28821 break;
28822 case DW_OP_entry_value:
28823 case DW_OP_GNU_entry_value:
28824 hstate.add_object (val1->v.val_loc);
28825 break;
28826 case DW_OP_regval_type:
28827 case DW_OP_deref_type:
28828 case DW_OP_GNU_regval_type:
28829 case DW_OP_GNU_deref_type:
28831 unsigned int byte_size
28832 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
28833 unsigned int encoding
28834 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
28835 hstate.add_object (val1->v.val_int);
28836 hstate.add_object (byte_size);
28837 hstate.add_object (encoding);
28839 break;
28840 case DW_OP_convert:
28841 case DW_OP_reinterpret:
28842 case DW_OP_GNU_convert:
28843 case DW_OP_GNU_reinterpret:
28844 if (val1->val_class == dw_val_class_unsigned_const)
28846 hstate.add_object (val1->v.val_unsigned);
28847 break;
28849 /* FALLTHRU */
28850 case DW_OP_const_type:
28851 case DW_OP_GNU_const_type:
28853 unsigned int byte_size
28854 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
28855 unsigned int encoding
28856 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
28857 hstate.add_object (byte_size);
28858 hstate.add_object (encoding);
28859 if (loc->dw_loc_opc != DW_OP_const_type
28860 && loc->dw_loc_opc != DW_OP_GNU_const_type)
28861 break;
28862 hstate.add_object (val2->val_class);
28863 switch (val2->val_class)
28865 case dw_val_class_const:
28866 hstate.add_object (val2->v.val_int);
28867 break;
28868 case dw_val_class_vec:
28870 unsigned int elt_size = val2->v.val_vec.elt_size;
28871 unsigned int len = val2->v.val_vec.length;
28873 hstate.add_object (elt_size);
28874 hstate.add_object (len);
28875 hstate.add (val2->v.val_vec.array, len * elt_size);
28877 break;
28878 case dw_val_class_const_double:
28879 hstate.add_object (val2->v.val_double.low);
28880 hstate.add_object (val2->v.val_double.high);
28881 break;
28882 case dw_val_class_wide_int:
28883 hstate.add (val2->v.val_wide->get_val (),
28884 get_full_len (*val2->v.val_wide)
28885 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
28886 break;
28887 default:
28888 gcc_unreachable ();
28891 break;
28893 default:
28894 /* Other codes have no operands. */
28895 break;
28899 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
28901 static inline void
28902 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
28904 dw_loc_descr_ref l;
28905 bool sizes_computed = false;
28906 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
28907 size_of_locs (loc);
28909 for (l = loc; l != NULL; l = l->dw_loc_next)
28911 enum dwarf_location_atom opc = l->dw_loc_opc;
28912 hstate.add_object (opc);
28913 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
28915 size_of_locs (loc);
28916 sizes_computed = true;
28918 hash_loc_operands (l, hstate);
28922 /* Compute hash of the whole location list LIST_HEAD. */
28924 static inline void
28925 hash_loc_list (dw_loc_list_ref list_head)
28927 dw_loc_list_ref curr = list_head;
28928 inchash::hash hstate;
28930 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
28932 hstate.add (curr->begin, strlen (curr->begin) + 1);
28933 hstate.add (curr->end, strlen (curr->end) + 1);
28934 if (curr->section)
28935 hstate.add (curr->section, strlen (curr->section) + 1);
28936 hash_locs (curr->expr, hstate);
28938 list_head->hash = hstate.end ();
28941 /* Return true if X and Y opcodes have the same operands. */
28943 static inline bool
28944 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
28946 dw_val_ref valx1 = &x->dw_loc_oprnd1;
28947 dw_val_ref valx2 = &x->dw_loc_oprnd2;
28948 dw_val_ref valy1 = &y->dw_loc_oprnd1;
28949 dw_val_ref valy2 = &y->dw_loc_oprnd2;
28951 switch (x->dw_loc_opc)
28953 case DW_OP_const4u:
28954 case DW_OP_const8u:
28955 if (x->dtprel)
28956 goto hash_addr;
28957 /* FALLTHRU */
28958 case DW_OP_const1u:
28959 case DW_OP_const1s:
28960 case DW_OP_const2u:
28961 case DW_OP_const2s:
28962 case DW_OP_const4s:
28963 case DW_OP_const8s:
28964 case DW_OP_constu:
28965 case DW_OP_consts:
28966 case DW_OP_pick:
28967 case DW_OP_plus_uconst:
28968 case DW_OP_breg0:
28969 case DW_OP_breg1:
28970 case DW_OP_breg2:
28971 case DW_OP_breg3:
28972 case DW_OP_breg4:
28973 case DW_OP_breg5:
28974 case DW_OP_breg6:
28975 case DW_OP_breg7:
28976 case DW_OP_breg8:
28977 case DW_OP_breg9:
28978 case DW_OP_breg10:
28979 case DW_OP_breg11:
28980 case DW_OP_breg12:
28981 case DW_OP_breg13:
28982 case DW_OP_breg14:
28983 case DW_OP_breg15:
28984 case DW_OP_breg16:
28985 case DW_OP_breg17:
28986 case DW_OP_breg18:
28987 case DW_OP_breg19:
28988 case DW_OP_breg20:
28989 case DW_OP_breg21:
28990 case DW_OP_breg22:
28991 case DW_OP_breg23:
28992 case DW_OP_breg24:
28993 case DW_OP_breg25:
28994 case DW_OP_breg26:
28995 case DW_OP_breg27:
28996 case DW_OP_breg28:
28997 case DW_OP_breg29:
28998 case DW_OP_breg30:
28999 case DW_OP_breg31:
29000 case DW_OP_regx:
29001 case DW_OP_fbreg:
29002 case DW_OP_piece:
29003 case DW_OP_deref_size:
29004 case DW_OP_xderef_size:
29005 return valx1->v.val_int == valy1->v.val_int;
29006 case DW_OP_skip:
29007 case DW_OP_bra:
29008 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29009 can cause irrelevant differences in dw_loc_addr. */
29010 gcc_assert (valx1->val_class == dw_val_class_loc
29011 && valy1->val_class == dw_val_class_loc
29012 && (dwarf_split_debug_info
29013 || x->dw_loc_addr == y->dw_loc_addr));
29014 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29015 case DW_OP_implicit_value:
29016 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29017 || valx2->val_class != valy2->val_class)
29018 return false;
29019 switch (valx2->val_class)
29021 case dw_val_class_const:
29022 return valx2->v.val_int == valy2->v.val_int;
29023 case dw_val_class_vec:
29024 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29025 && valx2->v.val_vec.length == valy2->v.val_vec.length
29026 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29027 valx2->v.val_vec.elt_size
29028 * valx2->v.val_vec.length) == 0;
29029 case dw_val_class_const_double:
29030 return valx2->v.val_double.low == valy2->v.val_double.low
29031 && valx2->v.val_double.high == valy2->v.val_double.high;
29032 case dw_val_class_wide_int:
29033 return *valx2->v.val_wide == *valy2->v.val_wide;
29034 case dw_val_class_addr:
29035 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29036 default:
29037 gcc_unreachable ();
29039 case DW_OP_bregx:
29040 case DW_OP_bit_piece:
29041 return valx1->v.val_int == valy1->v.val_int
29042 && valx2->v.val_int == valy2->v.val_int;
29043 case DW_OP_addr:
29044 hash_addr:
29045 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29046 case DW_OP_GNU_addr_index:
29047 case DW_OP_GNU_const_index:
29049 rtx ax1 = valx1->val_entry->addr.rtl;
29050 rtx ay1 = valy1->val_entry->addr.rtl;
29051 return rtx_equal_p (ax1, ay1);
29053 case DW_OP_implicit_pointer:
29054 case DW_OP_GNU_implicit_pointer:
29055 return valx1->val_class == dw_val_class_die_ref
29056 && valx1->val_class == valy1->val_class
29057 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29058 && valx2->v.val_int == valy2->v.val_int;
29059 case DW_OP_entry_value:
29060 case DW_OP_GNU_entry_value:
29061 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29062 case DW_OP_const_type:
29063 case DW_OP_GNU_const_type:
29064 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29065 || valx2->val_class != valy2->val_class)
29066 return false;
29067 switch (valx2->val_class)
29069 case dw_val_class_const:
29070 return valx2->v.val_int == valy2->v.val_int;
29071 case dw_val_class_vec:
29072 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29073 && valx2->v.val_vec.length == valy2->v.val_vec.length
29074 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29075 valx2->v.val_vec.elt_size
29076 * valx2->v.val_vec.length) == 0;
29077 case dw_val_class_const_double:
29078 return valx2->v.val_double.low == valy2->v.val_double.low
29079 && valx2->v.val_double.high == valy2->v.val_double.high;
29080 case dw_val_class_wide_int:
29081 return *valx2->v.val_wide == *valy2->v.val_wide;
29082 default:
29083 gcc_unreachable ();
29085 case DW_OP_regval_type:
29086 case DW_OP_deref_type:
29087 case DW_OP_GNU_regval_type:
29088 case DW_OP_GNU_deref_type:
29089 return valx1->v.val_int == valy1->v.val_int
29090 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29091 case DW_OP_convert:
29092 case DW_OP_reinterpret:
29093 case DW_OP_GNU_convert:
29094 case DW_OP_GNU_reinterpret:
29095 if (valx1->val_class != valy1->val_class)
29096 return false;
29097 if (valx1->val_class == dw_val_class_unsigned_const)
29098 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29099 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29100 case DW_OP_GNU_parameter_ref:
29101 return valx1->val_class == dw_val_class_die_ref
29102 && valx1->val_class == valy1->val_class
29103 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29104 default:
29105 /* Other codes have no operands. */
29106 return true;
29110 /* Return true if DWARF location expressions X and Y are the same. */
29112 static inline bool
29113 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29115 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29116 if (x->dw_loc_opc != y->dw_loc_opc
29117 || x->dtprel != y->dtprel
29118 || !compare_loc_operands (x, y))
29119 break;
29120 return x == NULL && y == NULL;
29123 /* Hashtable helpers. */
29125 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29127 static inline hashval_t hash (const dw_loc_list_struct *);
29128 static inline bool equal (const dw_loc_list_struct *,
29129 const dw_loc_list_struct *);
29132 /* Return precomputed hash of location list X. */
29134 inline hashval_t
29135 loc_list_hasher::hash (const dw_loc_list_struct *x)
29137 return x->hash;
29140 /* Return true if location lists A and B are the same. */
29142 inline bool
29143 loc_list_hasher::equal (const dw_loc_list_struct *a,
29144 const dw_loc_list_struct *b)
29146 if (a == b)
29147 return 1;
29148 if (a->hash != b->hash)
29149 return 0;
29150 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29151 if (strcmp (a->begin, b->begin) != 0
29152 || strcmp (a->end, b->end) != 0
29153 || (a->section == NULL) != (b->section == NULL)
29154 || (a->section && strcmp (a->section, b->section) != 0)
29155 || !compare_locs (a->expr, b->expr))
29156 break;
29157 return a == NULL && b == NULL;
29160 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29163 /* Recursively optimize location lists referenced from DIE
29164 children and share them whenever possible. */
29166 static void
29167 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29169 dw_die_ref c;
29170 dw_attr_node *a;
29171 unsigned ix;
29172 dw_loc_list_struct **slot;
29174 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29175 if (AT_class (a) == dw_val_class_loc_list)
29177 dw_loc_list_ref list = AT_loc_list (a);
29178 /* TODO: perform some optimizations here, before hashing
29179 it and storing into the hash table. */
29180 hash_loc_list (list);
29181 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29182 if (*slot == NULL)
29183 *slot = list;
29184 else
29185 a->dw_attr_val.v.val_loc_list = *slot;
29188 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29192 /* Recursively assign each location list a unique index into the debug_addr
29193 section. */
29195 static void
29196 index_location_lists (dw_die_ref die)
29198 dw_die_ref c;
29199 dw_attr_node *a;
29200 unsigned ix;
29202 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29203 if (AT_class (a) == dw_val_class_loc_list)
29205 dw_loc_list_ref list = AT_loc_list (a);
29206 dw_loc_list_ref curr;
29207 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29209 /* Don't index an entry that has already been indexed
29210 or won't be output. */
29211 if (curr->begin_entry != NULL
29212 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29213 continue;
29215 curr->begin_entry
29216 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29220 FOR_EACH_CHILD (die, c, index_location_lists (c));
29223 /* Optimize location lists referenced from DIE
29224 children and share them whenever possible. */
29226 static void
29227 optimize_location_lists (dw_die_ref die)
29229 loc_list_hash_type htab (500);
29230 optimize_location_lists_1 (die, &htab);
29233 /* Traverse the limbo die list, and add parent/child links. The only
29234 dies without parents that should be here are concrete instances of
29235 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29236 For concrete instances, we can get the parent die from the abstract
29237 instance. */
29239 static void
29240 flush_limbo_die_list (void)
29242 limbo_die_node *node;
29244 /* get_context_die calls force_decl_die, which can put new DIEs on the
29245 limbo list in LTO mode when nested functions are put in a different
29246 partition than that of their parent function. */
29247 while ((node = limbo_die_list))
29249 dw_die_ref die = node->die;
29250 limbo_die_list = node->next;
29252 if (die->die_parent == NULL)
29254 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29256 if (origin && origin->die_parent)
29257 add_child_die (origin->die_parent, die);
29258 else if (is_cu_die (die))
29260 else if (seen_error ())
29261 /* It's OK to be confused by errors in the input. */
29262 add_child_die (comp_unit_die (), die);
29263 else
29265 /* In certain situations, the lexical block containing a
29266 nested function can be optimized away, which results
29267 in the nested function die being orphaned. Likewise
29268 with the return type of that nested function. Force
29269 this to be a child of the containing function.
29271 It may happen that even the containing function got fully
29272 inlined and optimized out. In that case we are lost and
29273 assign the empty child. This should not be big issue as
29274 the function is likely unreachable too. */
29275 gcc_assert (node->created_for);
29277 if (DECL_P (node->created_for))
29278 origin = get_context_die (DECL_CONTEXT (node->created_for));
29279 else if (TYPE_P (node->created_for))
29280 origin = scope_die_for (node->created_for, comp_unit_die ());
29281 else
29282 origin = comp_unit_die ();
29284 add_child_die (origin, die);
29290 /* Output stuff that dwarf requires at the end of every file,
29291 and generate the DWARF-2 debugging info. */
29293 static void
29294 dwarf2out_finish (const char *)
29296 comdat_type_node *ctnode;
29297 dw_die_ref main_comp_unit_die;
29298 unsigned char checksum[16];
29300 /* Flush out any latecomers to the limbo party. */
29301 flush_limbo_die_list ();
29303 if (flag_checking)
29305 verify_die (comp_unit_die ());
29306 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29307 verify_die (node->die);
29310 /* We shouldn't have any symbols with delayed asm names for
29311 DIEs generated after early finish. */
29312 gcc_assert (deferred_asm_name == NULL);
29314 gen_remaining_tmpl_value_param_die_attribute ();
29316 #if ENABLE_ASSERT_CHECKING
29318 dw_die_ref die = comp_unit_die (), c;
29319 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29321 #endif
29322 resolve_addr (comp_unit_die ());
29323 move_marked_base_types ();
29325 /* Initialize sections and labels used for actual assembler output. */
29326 init_sections_and_labels ();
29328 /* Traverse the DIE's and add sibling attributes to those DIE's that
29329 have children. */
29330 add_sibling_attributes (comp_unit_die ());
29331 limbo_die_node *node;
29332 for (node = cu_die_list; node; node = node->next)
29333 add_sibling_attributes (node->die);
29334 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29335 add_sibling_attributes (ctnode->root_die);
29337 /* When splitting DWARF info, we put some attributes in the
29338 skeleton compile_unit DIE that remains in the .o, while
29339 most attributes go in the DWO compile_unit_die. */
29340 if (dwarf_split_debug_info)
29342 limbo_die_node *cu;
29343 main_comp_unit_die = gen_compile_unit_die (NULL);
29344 if (dwarf_version >= 5)
29345 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29346 cu = limbo_die_list;
29347 gcc_assert (cu->die == main_comp_unit_die);
29348 limbo_die_list = limbo_die_list->next;
29349 cu->next = cu_die_list;
29350 cu_die_list = cu;
29352 else
29353 main_comp_unit_die = comp_unit_die ();
29355 /* Output a terminator label for the .text section. */
29356 switch_to_section (text_section);
29357 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29358 if (cold_text_section)
29360 switch_to_section (cold_text_section);
29361 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29364 /* We can only use the low/high_pc attributes if all of the code was
29365 in .text. */
29366 if (!have_multiple_function_sections
29367 || (dwarf_version < 3 && dwarf_strict))
29369 /* Don't add if the CU has no associated code. */
29370 if (text_section_used)
29371 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29372 text_end_label, true);
29374 else
29376 unsigned fde_idx;
29377 dw_fde_ref fde;
29378 bool range_list_added = false;
29380 if (text_section_used)
29381 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29382 text_end_label, &range_list_added, true);
29383 if (cold_text_section_used)
29384 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29385 cold_end_label, &range_list_added, true);
29387 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29389 if (DECL_IGNORED_P (fde->decl))
29390 continue;
29391 if (!fde->in_std_section)
29392 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29393 fde->dw_fde_end, &range_list_added,
29394 true);
29395 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29396 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29397 fde->dw_fde_second_end, &range_list_added,
29398 true);
29401 if (range_list_added)
29403 /* We need to give .debug_loc and .debug_ranges an appropriate
29404 "base address". Use zero so that these addresses become
29405 absolute. Historically, we've emitted the unexpected
29406 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29407 Emit both to give time for other tools to adapt. */
29408 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29409 if (! dwarf_strict && dwarf_version < 4)
29410 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29412 add_ranges (NULL);
29416 if (debug_info_level >= DINFO_LEVEL_TERSE)
29417 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29418 debug_line_section_label);
29420 if (have_macinfo)
29421 add_AT_macptr (comp_unit_die (),
29422 dwarf_version >= 5 ? DW_AT_macros
29423 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29424 macinfo_section_label);
29426 if (dwarf_split_debug_info)
29428 if (have_location_lists)
29430 if (dwarf_version >= 5)
29431 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29432 loc_section_label);
29433 /* optimize_location_lists calculates the size of the lists,
29434 so index them first, and assign indices to the entries.
29435 Although optimize_location_lists will remove entries from
29436 the table, it only does so for duplicates, and therefore
29437 only reduces ref_counts to 1. */
29438 index_location_lists (comp_unit_die ());
29441 if (addr_index_table != NULL)
29443 unsigned int index = 0;
29444 addr_index_table
29445 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29446 (&index);
29450 loc_list_idx = 0;
29451 if (have_location_lists)
29453 optimize_location_lists (comp_unit_die ());
29454 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29455 if (dwarf_version >= 5 && dwarf_split_debug_info)
29456 assign_location_list_indexes (comp_unit_die ());
29459 save_macinfo_strings ();
29461 if (dwarf_split_debug_info)
29463 unsigned int index = 0;
29465 /* Add attributes common to skeleton compile_units and
29466 type_units. Because these attributes include strings, it
29467 must be done before freezing the string table. Top-level
29468 skeleton die attrs are added when the skeleton type unit is
29469 created, so ensure it is created by this point. */
29470 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29471 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29474 /* Output all of the compilation units. We put the main one last so that
29475 the offsets are available to output_pubnames. */
29476 for (node = cu_die_list; node; node = node->next)
29477 output_comp_unit (node->die, 0, NULL);
29479 hash_table<comdat_type_hasher> comdat_type_table (100);
29480 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29482 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29484 /* Don't output duplicate types. */
29485 if (*slot != HTAB_EMPTY_ENTRY)
29486 continue;
29488 /* Add a pointer to the line table for the main compilation unit
29489 so that the debugger can make sense of DW_AT_decl_file
29490 attributes. */
29491 if (debug_info_level >= DINFO_LEVEL_TERSE)
29492 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29493 (!dwarf_split_debug_info
29494 ? debug_line_section_label
29495 : debug_skeleton_line_section_label));
29497 output_comdat_type_unit (ctnode);
29498 *slot = ctnode;
29501 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29502 both the main_cu and all skeleton TUs. Making this call unconditional
29503 would end up either adding a second copy of the AT_pubnames attribute, or
29504 requiring a special case in add_top_level_skeleton_die_attrs. */
29505 if (!dwarf_split_debug_info)
29506 add_AT_pubnames (comp_unit_die ());
29508 if (dwarf_split_debug_info)
29510 int mark;
29511 struct md5_ctx ctx;
29513 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29514 index_rnglists ();
29516 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29517 md5_init_ctx (&ctx);
29518 mark = 0;
29519 die_checksum (comp_unit_die (), &ctx, &mark);
29520 unmark_all_dies (comp_unit_die ());
29521 md5_finish_ctx (&ctx, checksum);
29523 if (dwarf_version < 5)
29525 /* Use the first 8 bytes of the checksum as the dwo_id,
29526 and add it to both comp-unit DIEs. */
29527 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29528 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29531 /* Add the base offset of the ranges table to the skeleton
29532 comp-unit DIE. */
29533 if (!vec_safe_is_empty (ranges_table))
29535 if (dwarf_version >= 5)
29536 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29537 ranges_base_label);
29538 else
29539 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29540 ranges_section_label);
29543 switch_to_section (debug_addr_section);
29544 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29545 output_addr_table ();
29548 /* Output the main compilation unit if non-empty or if .debug_macinfo
29549 or .debug_macro will be emitted. */
29550 output_comp_unit (comp_unit_die (), have_macinfo,
29551 dwarf_split_debug_info ? checksum : NULL);
29553 if (dwarf_split_debug_info && info_section_emitted)
29554 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29556 /* Output the abbreviation table. */
29557 if (vec_safe_length (abbrev_die_table) != 1)
29559 switch_to_section (debug_abbrev_section);
29560 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29561 output_abbrev_section ();
29564 /* Output location list section if necessary. */
29565 if (have_location_lists)
29567 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29568 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29569 /* Output the location lists info. */
29570 switch_to_section (debug_loc_section);
29571 if (dwarf_version >= 5)
29573 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29574 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29575 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29576 dw2_asm_output_data (4, 0xffffffff,
29577 "Initial length escape value indicating "
29578 "64-bit DWARF extension");
29579 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29580 "Length of Location Lists");
29581 ASM_OUTPUT_LABEL (asm_out_file, l1);
29582 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29583 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29584 dw2_asm_output_data (1, 0, "Segment Size");
29585 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29586 "Offset Entry Count");
29588 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29589 if (dwarf_version >= 5 && dwarf_split_debug_info)
29591 unsigned int save_loc_list_idx = loc_list_idx;
29592 loc_list_idx = 0;
29593 output_loclists_offsets (comp_unit_die ());
29594 gcc_assert (save_loc_list_idx == loc_list_idx);
29596 output_location_lists (comp_unit_die ());
29597 if (dwarf_version >= 5)
29598 ASM_OUTPUT_LABEL (asm_out_file, l2);
29601 output_pubtables ();
29603 /* Output the address range information if a CU (.debug_info section)
29604 was emitted. We output an empty table even if we had no functions
29605 to put in it. This because the consumer has no way to tell the
29606 difference between an empty table that we omitted and failure to
29607 generate a table that would have contained data. */
29608 if (info_section_emitted)
29610 switch_to_section (debug_aranges_section);
29611 output_aranges ();
29614 /* Output ranges section if necessary. */
29615 if (!vec_safe_is_empty (ranges_table))
29617 if (dwarf_version >= 5)
29618 output_rnglists ();
29619 else
29620 output_ranges ();
29623 /* Have to end the macro section. */
29624 if (have_macinfo)
29626 switch_to_section (debug_macinfo_section);
29627 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29628 output_macinfo ();
29629 dw2_asm_output_data (1, 0, "End compilation unit");
29632 /* Output the source line correspondence table. We must do this
29633 even if there is no line information. Otherwise, on an empty
29634 translation unit, we will generate a present, but empty,
29635 .debug_info section. IRIX 6.5 `nm' will then complain when
29636 examining the file. This is done late so that any filenames
29637 used by the debug_info section are marked as 'used'. */
29638 switch_to_section (debug_line_section);
29639 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29640 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29641 output_line_info (false);
29643 if (dwarf_split_debug_info && info_section_emitted)
29645 switch_to_section (debug_skeleton_line_section);
29646 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29647 output_line_info (true);
29650 /* If we emitted any indirect strings, output the string table too. */
29651 if (debug_str_hash || skeleton_debug_str_hash)
29652 output_indirect_strings ();
29653 if (debug_line_str_hash)
29655 switch_to_section (debug_line_str_section);
29656 const enum dwarf_form form = DW_FORM_line_strp;
29657 debug_line_str_hash->traverse<enum dwarf_form,
29658 output_indirect_string> (form);
29662 /* Perform any cleanups needed after the early debug generation pass
29663 has run. */
29665 static void
29666 dwarf2out_early_finish (const char *filename)
29668 set_early_dwarf s;
29670 /* PCH might result in DW_AT_producer string being restored from the
29671 header compilation, so always fill it with empty string initially
29672 and overwrite only here. */
29673 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
29674 producer_string = gen_producer_string ();
29675 producer->dw_attr_val.v.val_str->refcount--;
29676 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
29678 /* Add the name for the main input file now. We delayed this from
29679 dwarf2out_init to avoid complications with PCH. */
29680 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
29681 add_comp_dir_attribute (comp_unit_die ());
29683 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
29684 DW_AT_comp_dir into .debug_line_str section. */
29685 if (!DWARF2_ASM_LINE_DEBUG_INFO
29686 && dwarf_version >= 5
29687 && DWARF5_USE_DEBUG_LINE_STR)
29689 for (int i = 0; i < 2; i++)
29691 dw_attr_node *a = get_AT (comp_unit_die (),
29692 i ? DW_AT_comp_dir : DW_AT_name);
29693 if (a == NULL
29694 || AT_class (a) != dw_val_class_str
29695 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
29696 continue;
29698 if (! debug_line_str_hash)
29699 debug_line_str_hash
29700 = hash_table<indirect_string_hasher>::create_ggc (10);
29702 struct indirect_string_node *node
29703 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
29704 set_indirect_string (node);
29705 node->form = DW_FORM_line_strp;
29706 a->dw_attr_val.v.val_str->refcount--;
29707 a->dw_attr_val.v.val_str = node;
29711 /* With LTO early dwarf was really finished at compile-time, so make
29712 sure to adjust the phase after annotating the LTRANS CU DIE. */
29713 if (in_lto_p)
29715 early_dwarf_finished = true;
29716 return;
29719 /* Walk through the list of incomplete types again, trying once more to
29720 emit full debugging info for them. */
29721 retry_incomplete_types ();
29723 /* The point here is to flush out the limbo list so that it is empty
29724 and we don't need to stream it for LTO. */
29725 flush_limbo_die_list ();
29727 gen_scheduled_generic_parms_dies ();
29728 gen_remaining_tmpl_value_param_die_attribute ();
29730 /* Add DW_AT_linkage_name for all deferred DIEs. */
29731 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
29733 tree decl = node->created_for;
29734 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
29735 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
29736 ended up in deferred_asm_name before we knew it was
29737 constant and never written to disk. */
29738 && DECL_ASSEMBLER_NAME (decl))
29740 add_linkage_attr (node->die, decl);
29741 move_linkage_attr (node->die);
29744 deferred_asm_name = NULL;
29746 if (flag_eliminate_unused_debug_types)
29747 prune_unused_types ();
29749 /* Generate separate COMDAT sections for type DIEs. */
29750 if (use_debug_types)
29752 break_out_comdat_types (comp_unit_die ());
29754 /* Each new type_unit DIE was added to the limbo die list when created.
29755 Since these have all been added to comdat_type_list, clear the
29756 limbo die list. */
29757 limbo_die_list = NULL;
29759 /* For each new comdat type unit, copy declarations for incomplete
29760 types to make the new unit self-contained (i.e., no direct
29761 references to the main compile unit). */
29762 for (comdat_type_node *ctnode = comdat_type_list;
29763 ctnode != NULL; ctnode = ctnode->next)
29764 copy_decls_for_unworthy_types (ctnode->root_die);
29765 copy_decls_for_unworthy_types (comp_unit_die ());
29767 /* In the process of copying declarations from one unit to another,
29768 we may have left some declarations behind that are no longer
29769 referenced. Prune them. */
29770 prune_unused_types ();
29773 /* Generate separate CUs for each of the include files we've seen.
29774 They will go into limbo_die_list and from there to cu_die_list. */
29775 if (flag_eliminate_dwarf2_dups)
29777 gcc_assert (limbo_die_list == NULL);
29778 break_out_includes (comp_unit_die ());
29779 limbo_die_node *cu;
29780 while ((cu = limbo_die_list))
29782 limbo_die_list = cu->next;
29783 cu->next = cu_die_list;
29784 cu_die_list = cu;
29788 /* The early debug phase is now finished. */
29789 early_dwarf_finished = true;
29792 /* Reset all state within dwarf2out.c so that we can rerun the compiler
29793 within the same process. For use by toplev::finalize. */
29795 void
29796 dwarf2out_c_finalize (void)
29798 last_var_location_insn = NULL;
29799 cached_next_real_insn = NULL;
29800 used_rtx_array = NULL;
29801 incomplete_types = NULL;
29802 decl_scope_table = NULL;
29803 debug_info_section = NULL;
29804 debug_skeleton_info_section = NULL;
29805 debug_abbrev_section = NULL;
29806 debug_skeleton_abbrev_section = NULL;
29807 debug_aranges_section = NULL;
29808 debug_addr_section = NULL;
29809 debug_macinfo_section = NULL;
29810 debug_line_section = NULL;
29811 debug_skeleton_line_section = NULL;
29812 debug_loc_section = NULL;
29813 debug_pubnames_section = NULL;
29814 debug_pubtypes_section = NULL;
29815 debug_str_section = NULL;
29816 debug_line_str_section = NULL;
29817 debug_str_dwo_section = NULL;
29818 debug_str_offsets_section = NULL;
29819 debug_ranges_section = NULL;
29820 debug_frame_section = NULL;
29821 fde_vec = NULL;
29822 debug_str_hash = NULL;
29823 debug_line_str_hash = NULL;
29824 skeleton_debug_str_hash = NULL;
29825 dw2_string_counter = 0;
29826 have_multiple_function_sections = false;
29827 text_section_used = false;
29828 cold_text_section_used = false;
29829 cold_text_section = NULL;
29830 current_unit_personality = NULL;
29832 next_die_offset = 0;
29833 single_comp_unit_die = NULL;
29834 comdat_type_list = NULL;
29835 limbo_die_list = NULL;
29836 file_table = NULL;
29837 decl_die_table = NULL;
29838 common_block_die_table = NULL;
29839 decl_loc_table = NULL;
29840 call_arg_locations = NULL;
29841 call_arg_loc_last = NULL;
29842 call_site_count = -1;
29843 tail_call_site_count = -1;
29844 cached_dw_loc_list_table = NULL;
29845 abbrev_die_table = NULL;
29846 delete dwarf_proc_stack_usage_map;
29847 dwarf_proc_stack_usage_map = NULL;
29848 line_info_label_num = 0;
29849 cur_line_info_table = NULL;
29850 text_section_line_info = NULL;
29851 cold_text_section_line_info = NULL;
29852 separate_line_info = NULL;
29853 info_section_emitted = false;
29854 pubname_table = NULL;
29855 pubtype_table = NULL;
29856 macinfo_table = NULL;
29857 ranges_table = NULL;
29858 ranges_by_label = NULL;
29859 rnglist_idx = 0;
29860 have_location_lists = false;
29861 loclabel_num = 0;
29862 poc_label_num = 0;
29863 last_emitted_file = NULL;
29864 label_num = 0;
29865 tmpl_value_parm_die_table = NULL;
29866 generic_type_instances = NULL;
29867 frame_pointer_fb_offset = 0;
29868 frame_pointer_fb_offset_valid = false;
29869 base_types.release ();
29870 XDELETEVEC (producer_string);
29871 producer_string = NULL;
29874 #include "gt-dwarf2out.h"