PR target/81369
[official-gcc.git] / gcc / dwarf2out.c
blob72d2c588c86294aaea47d13f773a6d4bfc69b797
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
96 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
97 int, bool);
98 static rtx_insn *last_var_location_insn;
99 static rtx_insn *cached_next_real_insn;
100 static void dwarf2out_decl (tree);
102 #ifndef XCOFF_DEBUGGING_INFO
103 #define XCOFF_DEBUGGING_INFO 0
104 #endif
106 #ifndef HAVE_XCOFF_DWARF_EXTRAS
107 #define HAVE_XCOFF_DWARF_EXTRAS 0
108 #endif
110 #ifdef VMS_DEBUGGING_INFO
111 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
113 /* Define this macro to be a nonzero value if the directory specifications
114 which are output in the debug info should end with a separator. */
115 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
116 /* Define this macro to evaluate to a nonzero value if GCC should refrain
117 from generating indirect strings in DWARF2 debug information, for instance
118 if your target is stuck with an old version of GDB that is unable to
119 process them properly or uses VMS Debug. */
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
121 #else
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
123 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
124 #endif
126 /* ??? Poison these here until it can be done generically. They've been
127 totally replaced in this file; make sure it stays that way. */
128 #undef DWARF2_UNWIND_INFO
129 #undef DWARF2_FRAME_INFO
130 #if (GCC_VERSION >= 3000)
131 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
132 #endif
134 /* The size of the target's pointer type. */
135 #ifndef PTR_SIZE
136 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
137 #endif
139 /* Array of RTXes referenced by the debugging information, which therefore
140 must be kept around forever. */
141 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
143 /* A pointer to the base of a list of incomplete types which might be
144 completed at some later time. incomplete_types_list needs to be a
145 vec<tree, va_gc> *because we want to tell the garbage collector about
146 it. */
147 static GTY(()) vec<tree, va_gc> *incomplete_types;
149 /* A pointer to the base of a table of references to declaration
150 scopes. This table is a display which tracks the nesting
151 of declaration scopes at the current scope and containing
152 scopes. This table is used to find the proper place to
153 define type declaration DIE's. */
154 static GTY(()) vec<tree, va_gc> *decl_scope_table;
156 /* Pointers to various DWARF2 sections. */
157 static GTY(()) section *debug_info_section;
158 static GTY(()) section *debug_skeleton_info_section;
159 static GTY(()) section *debug_abbrev_section;
160 static GTY(()) section *debug_skeleton_abbrev_section;
161 static GTY(()) section *debug_aranges_section;
162 static GTY(()) section *debug_addr_section;
163 static GTY(()) section *debug_macinfo_section;
164 static const char *debug_macinfo_section_name;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_frame_section;
177 /* Maximum size (in bytes) of an artificially generated label. */
178 #define MAX_ARTIFICIAL_LABEL_BYTES 30
180 /* According to the (draft) DWARF 3 specification, the initial length
181 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
182 bytes are 0xffffffff, followed by the length stored in the next 8
183 bytes.
185 However, the SGI/MIPS ABI uses an initial length which is equal to
186 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
188 #ifndef DWARF_INITIAL_LENGTH_SIZE
189 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
190 #endif
192 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
193 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
194 #endif
196 /* Round SIZE up to the nearest BOUNDARY. */
197 #define DWARF_ROUND(SIZE,BOUNDARY) \
198 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
200 /* CIE identifier. */
201 #if HOST_BITS_PER_WIDE_INT >= 64
202 #define DWARF_CIE_ID \
203 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
204 #else
205 #define DWARF_CIE_ID DW_CIE_ID
206 #endif
209 /* A vector for a table that contains frame description
210 information for each routine. */
211 #define NOT_INDEXED (-1U)
212 #define NO_INDEX_ASSIGNED (-2U)
214 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
216 struct GTY((for_user)) indirect_string_node {
217 const char *str;
218 unsigned int refcount;
219 enum dwarf_form form;
220 char *label;
221 unsigned int index;
224 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
226 typedef const char *compare_type;
228 static hashval_t hash (indirect_string_node *);
229 static bool equal (indirect_string_node *, const char *);
232 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
234 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
236 /* With split_debug_info, both the comp_dir and dwo_name go in the
237 main object file, rather than the dwo, similar to the force_direct
238 parameter elsewhere but with additional complications:
240 1) The string is needed in both the main object file and the dwo.
241 That is, the comp_dir and dwo_name will appear in both places.
243 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
244 DW_FORM_line_strp or DW_FORM_GNU_str_index.
246 3) GCC chooses the form to use late, depending on the size and
247 reference count.
249 Rather than forcing the all debug string handling functions and
250 callers to deal with these complications, simply use a separate,
251 special-cased string table for any attribute that should go in the
252 main object file. This limits the complexity to just the places
253 that need it. */
255 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
257 static GTY(()) int dw2_string_counter;
259 /* True if the compilation unit places functions in more than one section. */
260 static GTY(()) bool have_multiple_function_sections = false;
262 /* Whether the default text and cold text sections have been used at all. */
264 static GTY(()) bool text_section_used = false;
265 static GTY(()) bool cold_text_section_used = false;
267 /* The default cold text section. */
268 static GTY(()) section *cold_text_section;
270 /* The DIE for C++14 'auto' in a function return type. */
271 static GTY(()) dw_die_ref auto_die;
273 /* The DIE for C++14 'decltype(auto)' in a function return type. */
274 static GTY(()) dw_die_ref decltype_auto_die;
276 /* Forward declarations for functions defined in this file. */
278 static void output_call_frame_info (int);
279 static void dwarf2out_note_section_used (void);
281 /* Personality decl of current unit. Used only when assembler does not support
282 personality CFI. */
283 static GTY(()) rtx current_unit_personality;
285 /* .debug_rnglists next index. */
286 static unsigned int rnglist_idx;
288 /* Data and reference forms for relocatable data. */
289 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
290 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
292 #ifndef DEBUG_FRAME_SECTION
293 #define DEBUG_FRAME_SECTION ".debug_frame"
294 #endif
296 #ifndef FUNC_BEGIN_LABEL
297 #define FUNC_BEGIN_LABEL "LFB"
298 #endif
300 #ifndef FUNC_END_LABEL
301 #define FUNC_END_LABEL "LFE"
302 #endif
304 #ifndef PROLOGUE_END_LABEL
305 #define PROLOGUE_END_LABEL "LPE"
306 #endif
308 #ifndef EPILOGUE_BEGIN_LABEL
309 #define EPILOGUE_BEGIN_LABEL "LEB"
310 #endif
312 #ifndef FRAME_BEGIN_LABEL
313 #define FRAME_BEGIN_LABEL "Lframe"
314 #endif
315 #define CIE_AFTER_SIZE_LABEL "LSCIE"
316 #define CIE_END_LABEL "LECIE"
317 #define FDE_LABEL "LSFDE"
318 #define FDE_AFTER_SIZE_LABEL "LASFDE"
319 #define FDE_END_LABEL "LEFDE"
320 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
321 #define LINE_NUMBER_END_LABEL "LELT"
322 #define LN_PROLOG_AS_LABEL "LASLTP"
323 #define LN_PROLOG_END_LABEL "LELTP"
324 #define DIE_LABEL_PREFIX "DW"
326 /* Match the base name of a file to the base name of a compilation unit. */
328 static int
329 matches_main_base (const char *path)
331 /* Cache the last query. */
332 static const char *last_path = NULL;
333 static int last_match = 0;
334 if (path != last_path)
336 const char *base;
337 int length = base_of_path (path, &base);
338 last_path = path;
339 last_match = (length == main_input_baselength
340 && memcmp (base, main_input_basename, length) == 0);
342 return last_match;
345 #ifdef DEBUG_DEBUG_STRUCT
347 static int
348 dump_struct_debug (tree type, enum debug_info_usage usage,
349 enum debug_struct_file criterion, int generic,
350 int matches, int result)
352 /* Find the type name. */
353 tree type_decl = TYPE_STUB_DECL (type);
354 tree t = type_decl;
355 const char *name = 0;
356 if (TREE_CODE (t) == TYPE_DECL)
357 t = DECL_NAME (t);
358 if (t)
359 name = IDENTIFIER_POINTER (t);
361 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
362 criterion,
363 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
364 matches ? "bas" : "hdr",
365 generic ? "gen" : "ord",
366 usage == DINFO_USAGE_DFN ? ";" :
367 usage == DINFO_USAGE_DIR_USE ? "." : "*",
368 result,
369 (void*) type_decl, name);
370 return result;
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 dump_struct_debug (type, usage, criterion, generic, matches, result)
375 #else
377 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
378 (result)
380 #endif
382 /* Get the number of HOST_WIDE_INTs needed to represent the precision
383 of the number. Some constants have a large uniform precision, so
384 we get the precision needed for the actual value of the number. */
386 static unsigned int
387 get_full_len (const wide_int &op)
389 int prec = wi::min_precision (op, UNSIGNED);
390 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
391 / HOST_BITS_PER_WIDE_INT);
394 static bool
395 should_emit_struct_debug (tree type, enum debug_info_usage usage)
397 enum debug_struct_file criterion;
398 tree type_decl;
399 bool generic = lang_hooks.types.generic_p (type);
401 if (generic)
402 criterion = debug_struct_generic[usage];
403 else
404 criterion = debug_struct_ordinary[usage];
406 if (criterion == DINFO_STRUCT_FILE_NONE)
407 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
408 if (criterion == DINFO_STRUCT_FILE_ANY)
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
411 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
413 if (type_decl != NULL)
415 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
416 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
418 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
419 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
422 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
425 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
426 switch to the data section instead, and write out a synthetic start label
427 for collect2 the first time around. */
429 static void
430 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
432 if (eh_frame_section == 0)
434 int flags;
436 if (EH_TABLES_CAN_BE_READ_ONLY)
438 int fde_encoding;
439 int per_encoding;
440 int lsda_encoding;
442 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
443 /*global=*/0);
444 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
445 /*global=*/1);
446 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
447 /*global=*/0);
448 flags = ((! flag_pic
449 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
450 && (fde_encoding & 0x70) != DW_EH_PE_aligned
451 && (per_encoding & 0x70) != DW_EH_PE_absptr
452 && (per_encoding & 0x70) != DW_EH_PE_aligned
453 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
454 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
455 ? 0 : SECTION_WRITE);
457 else
458 flags = SECTION_WRITE;
460 #ifdef EH_FRAME_SECTION_NAME
461 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
462 #else
463 eh_frame_section = ((flags == SECTION_WRITE)
464 ? data_section : readonly_data_section);
465 #endif /* EH_FRAME_SECTION_NAME */
468 switch_to_section (eh_frame_section);
470 #ifdef EH_FRAME_THROUGH_COLLECT2
471 /* We have no special eh_frame section. Emit special labels to guide
472 collect2. */
473 if (!back)
475 tree label = get_file_function_name ("F");
476 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
477 targetm.asm_out.globalize_label (asm_out_file,
478 IDENTIFIER_POINTER (label));
479 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
481 #endif
484 /* Switch [BACK] to the eh or debug frame table section, depending on
485 FOR_EH. */
487 static void
488 switch_to_frame_table_section (int for_eh, bool back)
490 if (for_eh)
491 switch_to_eh_frame_section (back);
492 else
494 if (!debug_frame_section)
495 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
496 SECTION_DEBUG, NULL);
497 switch_to_section (debug_frame_section);
501 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
503 enum dw_cfi_oprnd_type
504 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
506 switch (cfi)
508 case DW_CFA_nop:
509 case DW_CFA_GNU_window_save:
510 case DW_CFA_remember_state:
511 case DW_CFA_restore_state:
512 return dw_cfi_oprnd_unused;
514 case DW_CFA_set_loc:
515 case DW_CFA_advance_loc1:
516 case DW_CFA_advance_loc2:
517 case DW_CFA_advance_loc4:
518 case DW_CFA_MIPS_advance_loc8:
519 return dw_cfi_oprnd_addr;
521 case DW_CFA_offset:
522 case DW_CFA_offset_extended:
523 case DW_CFA_def_cfa:
524 case DW_CFA_offset_extended_sf:
525 case DW_CFA_def_cfa_sf:
526 case DW_CFA_restore:
527 case DW_CFA_restore_extended:
528 case DW_CFA_undefined:
529 case DW_CFA_same_value:
530 case DW_CFA_def_cfa_register:
531 case DW_CFA_register:
532 case DW_CFA_expression:
533 case DW_CFA_val_expression:
534 return dw_cfi_oprnd_reg_num;
536 case DW_CFA_def_cfa_offset:
537 case DW_CFA_GNU_args_size:
538 case DW_CFA_def_cfa_offset_sf:
539 return dw_cfi_oprnd_offset;
541 case DW_CFA_def_cfa_expression:
542 return dw_cfi_oprnd_loc;
544 default:
545 gcc_unreachable ();
549 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
551 enum dw_cfi_oprnd_type
552 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
554 switch (cfi)
556 case DW_CFA_def_cfa:
557 case DW_CFA_def_cfa_sf:
558 case DW_CFA_offset:
559 case DW_CFA_offset_extended_sf:
560 case DW_CFA_offset_extended:
561 return dw_cfi_oprnd_offset;
563 case DW_CFA_register:
564 return dw_cfi_oprnd_reg_num;
566 case DW_CFA_expression:
567 case DW_CFA_val_expression:
568 return dw_cfi_oprnd_loc;
570 default:
571 return dw_cfi_oprnd_unused;
575 /* Output one FDE. */
577 static void
578 output_fde (dw_fde_ref fde, bool for_eh, bool second,
579 char *section_start_label, int fde_encoding, char *augmentation,
580 bool any_lsda_needed, int lsda_encoding)
582 const char *begin, *end;
583 static unsigned int j;
584 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
586 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
587 /* empty */ 0);
588 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
589 for_eh + j);
590 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
591 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
592 if (!XCOFF_DEBUGGING_INFO || for_eh)
594 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
595 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
596 " indicating 64-bit DWARF extension");
597 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
598 "FDE Length");
600 ASM_OUTPUT_LABEL (asm_out_file, l1);
602 if (for_eh)
603 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
604 else
605 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
606 debug_frame_section, "FDE CIE offset");
608 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
609 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
611 if (for_eh)
613 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
614 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
615 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
616 "FDE initial location");
617 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
618 end, begin, "FDE address range");
620 else
622 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
623 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
626 if (augmentation[0])
628 if (any_lsda_needed)
630 int size = size_of_encoded_value (lsda_encoding);
632 if (lsda_encoding == DW_EH_PE_aligned)
634 int offset = ( 4 /* Length */
635 + 4 /* CIE offset */
636 + 2 * size_of_encoded_value (fde_encoding)
637 + 1 /* Augmentation size */ );
638 int pad = -offset & (PTR_SIZE - 1);
640 size += pad;
641 gcc_assert (size_of_uleb128 (size) == 1);
644 dw2_asm_output_data_uleb128 (size, "Augmentation size");
646 if (fde->uses_eh_lsda)
648 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
649 fde->funcdef_number);
650 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
651 gen_rtx_SYMBOL_REF (Pmode, l1),
652 false,
653 "Language Specific Data Area");
655 else
657 if (lsda_encoding == DW_EH_PE_aligned)
658 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
659 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
660 "Language Specific Data Area (none)");
663 else
664 dw2_asm_output_data_uleb128 (0, "Augmentation size");
667 /* Loop through the Call Frame Instructions associated with this FDE. */
668 fde->dw_fde_current_label = begin;
670 size_t from, until, i;
672 from = 0;
673 until = vec_safe_length (fde->dw_fde_cfi);
675 if (fde->dw_fde_second_begin == NULL)
677 else if (!second)
678 until = fde->dw_fde_switch_cfi_index;
679 else
680 from = fde->dw_fde_switch_cfi_index;
682 for (i = from; i < until; i++)
683 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
686 /* If we are to emit a ref/link from function bodies to their frame tables,
687 do it now. This is typically performed to make sure that tables
688 associated with functions are dragged with them and not discarded in
689 garbage collecting links. We need to do this on a per function basis to
690 cope with -ffunction-sections. */
692 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
693 /* Switch to the function section, emit the ref to the tables, and
694 switch *back* into the table section. */
695 switch_to_section (function_section (fde->decl));
696 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
697 switch_to_frame_table_section (for_eh, true);
698 #endif
700 /* Pad the FDE out to an address sized boundary. */
701 ASM_OUTPUT_ALIGN (asm_out_file,
702 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
703 ASM_OUTPUT_LABEL (asm_out_file, l2);
705 j += 2;
708 /* Return true if frame description entry FDE is needed for EH. */
710 static bool
711 fde_needed_for_eh_p (dw_fde_ref fde)
713 if (flag_asynchronous_unwind_tables)
714 return true;
716 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
717 return true;
719 if (fde->uses_eh_lsda)
720 return true;
722 /* If exceptions are enabled, we have collected nothrow info. */
723 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
724 return false;
726 return true;
729 /* Output the call frame information used to record information
730 that relates to calculating the frame pointer, and records the
731 location of saved registers. */
733 static void
734 output_call_frame_info (int for_eh)
736 unsigned int i;
737 dw_fde_ref fde;
738 dw_cfi_ref cfi;
739 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
740 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
741 bool any_lsda_needed = false;
742 char augmentation[6];
743 int augmentation_size;
744 int fde_encoding = DW_EH_PE_absptr;
745 int per_encoding = DW_EH_PE_absptr;
746 int lsda_encoding = DW_EH_PE_absptr;
747 int return_reg;
748 rtx personality = NULL;
749 int dw_cie_version;
751 /* Don't emit a CIE if there won't be any FDEs. */
752 if (!fde_vec)
753 return;
755 /* Nothing to do if the assembler's doing it all. */
756 if (dwarf2out_do_cfi_asm ())
757 return;
759 /* If we don't have any functions we'll want to unwind out of, don't emit
760 any EH unwind information. If we make FDEs linkonce, we may have to
761 emit an empty label for an FDE that wouldn't otherwise be emitted. We
762 want to avoid having an FDE kept around when the function it refers to
763 is discarded. Example where this matters: a primary function template
764 in C++ requires EH information, an explicit specialization doesn't. */
765 if (for_eh)
767 bool any_eh_needed = false;
769 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
771 if (fde->uses_eh_lsda)
772 any_eh_needed = any_lsda_needed = true;
773 else if (fde_needed_for_eh_p (fde))
774 any_eh_needed = true;
775 else if (TARGET_USES_WEAK_UNWIND_INFO)
776 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
779 if (!any_eh_needed)
780 return;
783 /* We're going to be generating comments, so turn on app. */
784 if (flag_debug_asm)
785 app_enable ();
787 /* Switch to the proper frame section, first time. */
788 switch_to_frame_table_section (for_eh, false);
790 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
791 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
793 /* Output the CIE. */
794 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
795 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
796 if (!XCOFF_DEBUGGING_INFO || for_eh)
798 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
799 dw2_asm_output_data (4, 0xffffffff,
800 "Initial length escape value indicating 64-bit DWARF extension");
801 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
802 "Length of Common Information Entry");
804 ASM_OUTPUT_LABEL (asm_out_file, l1);
806 /* Now that the CIE pointer is PC-relative for EH,
807 use 0 to identify the CIE. */
808 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
809 (for_eh ? 0 : DWARF_CIE_ID),
810 "CIE Identifier Tag");
812 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
813 use CIE version 1, unless that would produce incorrect results
814 due to overflowing the return register column. */
815 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
816 dw_cie_version = 1;
817 if (return_reg >= 256 || dwarf_version > 2)
818 dw_cie_version = 3;
819 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
821 augmentation[0] = 0;
822 augmentation_size = 0;
824 personality = current_unit_personality;
825 if (for_eh)
827 char *p;
829 /* Augmentation:
830 z Indicates that a uleb128 is present to size the
831 augmentation section.
832 L Indicates the encoding (and thus presence) of
833 an LSDA pointer in the FDE augmentation.
834 R Indicates a non-default pointer encoding for
835 FDE code pointers.
836 P Indicates the presence of an encoding + language
837 personality routine in the CIE augmentation. */
839 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
840 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
841 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
843 p = augmentation + 1;
844 if (personality)
846 *p++ = 'P';
847 augmentation_size += 1 + size_of_encoded_value (per_encoding);
848 assemble_external_libcall (personality);
850 if (any_lsda_needed)
852 *p++ = 'L';
853 augmentation_size += 1;
855 if (fde_encoding != DW_EH_PE_absptr)
857 *p++ = 'R';
858 augmentation_size += 1;
860 if (p > augmentation + 1)
862 augmentation[0] = 'z';
863 *p = '\0';
866 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
867 if (personality && per_encoding == DW_EH_PE_aligned)
869 int offset = ( 4 /* Length */
870 + 4 /* CIE Id */
871 + 1 /* CIE version */
872 + strlen (augmentation) + 1 /* Augmentation */
873 + size_of_uleb128 (1) /* Code alignment */
874 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
875 + 1 /* RA column */
876 + 1 /* Augmentation size */
877 + 1 /* Personality encoding */ );
878 int pad = -offset & (PTR_SIZE - 1);
880 augmentation_size += pad;
882 /* Augmentations should be small, so there's scarce need to
883 iterate for a solution. Die if we exceed one uleb128 byte. */
884 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
888 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
889 if (dw_cie_version >= 4)
891 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
892 dw2_asm_output_data (1, 0, "CIE Segment Size");
894 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
895 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
896 "CIE Data Alignment Factor");
898 if (dw_cie_version == 1)
899 dw2_asm_output_data (1, return_reg, "CIE RA Column");
900 else
901 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
903 if (augmentation[0])
905 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
906 if (personality)
908 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
909 eh_data_format_name (per_encoding));
910 dw2_asm_output_encoded_addr_rtx (per_encoding,
911 personality,
912 true, NULL);
915 if (any_lsda_needed)
916 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
917 eh_data_format_name (lsda_encoding));
919 if (fde_encoding != DW_EH_PE_absptr)
920 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
921 eh_data_format_name (fde_encoding));
924 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
925 output_cfi (cfi, NULL, for_eh);
927 /* Pad the CIE out to an address sized boundary. */
928 ASM_OUTPUT_ALIGN (asm_out_file,
929 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
930 ASM_OUTPUT_LABEL (asm_out_file, l2);
932 /* Loop through all of the FDE's. */
933 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
935 unsigned int k;
937 /* Don't emit EH unwind info for leaf functions that don't need it. */
938 if (for_eh && !fde_needed_for_eh_p (fde))
939 continue;
941 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
942 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
943 augmentation, any_lsda_needed, lsda_encoding);
946 if (for_eh && targetm.terminate_dw2_eh_frame_info)
947 dw2_asm_output_data (4, 0, "End of Table");
949 /* Turn off app to make assembly quicker. */
950 if (flag_debug_asm)
951 app_disable ();
954 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
956 static void
957 dwarf2out_do_cfi_startproc (bool second)
959 int enc;
960 rtx ref;
961 rtx personality = get_personality_function (current_function_decl);
963 fprintf (asm_out_file, "\t.cfi_startproc\n");
965 if (personality)
967 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
968 ref = personality;
970 /* ??? The GAS support isn't entirely consistent. We have to
971 handle indirect support ourselves, but PC-relative is done
972 in the assembler. Further, the assembler can't handle any
973 of the weirder relocation types. */
974 if (enc & DW_EH_PE_indirect)
975 ref = dw2_force_const_mem (ref, true);
977 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
978 output_addr_const (asm_out_file, ref);
979 fputc ('\n', asm_out_file);
982 if (crtl->uses_eh_lsda)
984 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
986 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
987 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
988 current_function_funcdef_no);
989 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
990 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
992 if (enc & DW_EH_PE_indirect)
993 ref = dw2_force_const_mem (ref, true);
995 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
996 output_addr_const (asm_out_file, ref);
997 fputc ('\n', asm_out_file);
1001 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1002 this allocation may be done before pass_final. */
1004 dw_fde_ref
1005 dwarf2out_alloc_current_fde (void)
1007 dw_fde_ref fde;
1009 fde = ggc_cleared_alloc<dw_fde_node> ();
1010 fde->decl = current_function_decl;
1011 fde->funcdef_number = current_function_funcdef_no;
1012 fde->fde_index = vec_safe_length (fde_vec);
1013 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1014 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1015 fde->nothrow = crtl->nothrow;
1016 fde->drap_reg = INVALID_REGNUM;
1017 fde->vdrap_reg = INVALID_REGNUM;
1019 /* Record the FDE associated with this function. */
1020 cfun->fde = fde;
1021 vec_safe_push (fde_vec, fde);
1023 return fde;
1026 /* Output a marker (i.e. a label) for the beginning of a function, before
1027 the prologue. */
1029 void
1030 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1031 unsigned int column ATTRIBUTE_UNUSED,
1032 const char *file ATTRIBUTE_UNUSED)
1034 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1035 char * dup_label;
1036 dw_fde_ref fde;
1037 section *fnsec;
1038 bool do_frame;
1040 current_function_func_begin_label = NULL;
1042 do_frame = dwarf2out_do_frame ();
1044 /* ??? current_function_func_begin_label is also used by except.c for
1045 call-site information. We must emit this label if it might be used. */
1046 if (!do_frame
1047 && (!flag_exceptions
1048 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1049 return;
1051 fnsec = function_section (current_function_decl);
1052 switch_to_section (fnsec);
1053 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1054 current_function_funcdef_no);
1055 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 dup_label = xstrdup (label);
1058 current_function_func_begin_label = dup_label;
1060 /* We can elide the fde allocation if we're not emitting debug info. */
1061 if (!do_frame)
1062 return;
1064 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1065 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1066 would include pass_dwarf2_frame. If we've not created the FDE yet,
1067 do so now. */
1068 fde = cfun->fde;
1069 if (fde == NULL)
1070 fde = dwarf2out_alloc_current_fde ();
1072 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1073 fde->dw_fde_begin = dup_label;
1074 fde->dw_fde_current_label = dup_label;
1075 fde->in_std_section = (fnsec == text_section
1076 || (cold_text_section && fnsec == cold_text_section));
1078 /* We only want to output line number information for the genuine dwarf2
1079 prologue case, not the eh frame case. */
1080 #ifdef DWARF2_DEBUGGING_INFO
1081 if (file)
1082 dwarf2out_source_line (line, column, file, 0, true);
1083 #endif
1085 if (dwarf2out_do_cfi_asm ())
1086 dwarf2out_do_cfi_startproc (false);
1087 else
1089 rtx personality = get_personality_function (current_function_decl);
1090 if (!current_unit_personality)
1091 current_unit_personality = personality;
1093 /* We cannot keep a current personality per function as without CFI
1094 asm, at the point where we emit the CFI data, there is no current
1095 function anymore. */
1096 if (personality && current_unit_personality != personality)
1097 sorry ("multiple EH personalities are supported only with assemblers "
1098 "supporting .cfi_personality directive");
1102 /* Output a marker (i.e. a label) for the end of the generated code
1103 for a function prologue. This gets called *after* the prologue code has
1104 been generated. */
1106 void
1107 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1108 const char *file ATTRIBUTE_UNUSED)
1110 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1112 /* Output a label to mark the endpoint of the code generated for this
1113 function. */
1114 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1115 current_function_funcdef_no);
1116 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1121 /* Output a marker (i.e. a label) for the beginning of the generated code
1122 for a function epilogue. This gets called *before* the prologue code has
1123 been generated. */
1125 void
1126 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1127 const char *file ATTRIBUTE_UNUSED)
1129 dw_fde_ref fde = cfun->fde;
1130 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1132 if (fde->dw_fde_vms_begin_epilogue)
1133 return;
1135 /* Output a label to mark the endpoint of the code generated for this
1136 function. */
1137 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1138 current_function_funcdef_no);
1139 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1144 /* Output a marker (i.e. a label) for the absolute end of the generated code
1145 for a function definition. This gets called *after* the epilogue code has
1146 been generated. */
1148 void
1149 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1150 const char *file ATTRIBUTE_UNUSED)
1152 dw_fde_ref fde;
1153 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1155 last_var_location_insn = NULL;
1156 cached_next_real_insn = NULL;
1158 if (dwarf2out_do_cfi_asm ())
1159 fprintf (asm_out_file, "\t.cfi_endproc\n");
1161 /* Output a label to mark the endpoint of the code generated for this
1162 function. */
1163 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1164 current_function_funcdef_no);
1165 ASM_OUTPUT_LABEL (asm_out_file, label);
1166 fde = cfun->fde;
1167 gcc_assert (fde != NULL);
1168 if (fde->dw_fde_second_begin == NULL)
1169 fde->dw_fde_end = xstrdup (label);
1172 void
1173 dwarf2out_frame_finish (void)
1175 /* Output call frame information. */
1176 if (targetm.debug_unwind_info () == UI_DWARF2)
1177 output_call_frame_info (0);
1179 /* Output another copy for the unwinder. */
1180 if ((flag_unwind_tables || flag_exceptions)
1181 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1182 output_call_frame_info (1);
1185 /* Note that the current function section is being used for code. */
1187 static void
1188 dwarf2out_note_section_used (void)
1190 section *sec = current_function_section ();
1191 if (sec == text_section)
1192 text_section_used = true;
1193 else if (sec == cold_text_section)
1194 cold_text_section_used = true;
1197 static void var_location_switch_text_section (void);
1198 static void set_cur_line_info_table (section *);
1200 void
1201 dwarf2out_switch_text_section (void)
1203 section *sect;
1204 dw_fde_ref fde = cfun->fde;
1206 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1208 if (!in_cold_section_p)
1210 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1211 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1212 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1214 else
1216 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1217 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1218 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1220 have_multiple_function_sections = true;
1222 /* There is no need to mark used sections when not debugging. */
1223 if (cold_text_section != NULL)
1224 dwarf2out_note_section_used ();
1226 if (dwarf2out_do_cfi_asm ())
1227 fprintf (asm_out_file, "\t.cfi_endproc\n");
1229 /* Now do the real section switch. */
1230 sect = current_function_section ();
1231 switch_to_section (sect);
1233 fde->second_in_std_section
1234 = (sect == text_section
1235 || (cold_text_section && sect == cold_text_section));
1237 if (dwarf2out_do_cfi_asm ())
1238 dwarf2out_do_cfi_startproc (true);
1240 var_location_switch_text_section ();
1242 if (cold_text_section != NULL)
1243 set_cur_line_info_table (sect);
1246 /* And now, the subset of the debugging information support code necessary
1247 for emitting location expressions. */
1249 /* Data about a single source file. */
1250 struct GTY((for_user)) dwarf_file_data {
1251 const char * filename;
1252 int emitted_number;
1255 /* Describe an entry into the .debug_addr section. */
1257 enum ate_kind {
1258 ate_kind_rtx,
1259 ate_kind_rtx_dtprel,
1260 ate_kind_label
1263 struct GTY((for_user)) addr_table_entry {
1264 enum ate_kind kind;
1265 unsigned int refcount;
1266 unsigned int index;
1267 union addr_table_entry_struct_union
1269 rtx GTY ((tag ("0"))) rtl;
1270 char * GTY ((tag ("1"))) label;
1272 GTY ((desc ("%1.kind"))) addr;
1275 /* Location lists are ranges + location descriptions for that range,
1276 so you can track variables that are in different places over
1277 their entire life. */
1278 typedef struct GTY(()) dw_loc_list_struct {
1279 dw_loc_list_ref dw_loc_next;
1280 const char *begin; /* Label and addr_entry for start of range */
1281 addr_table_entry *begin_entry;
1282 const char *end; /* Label for end of range */
1283 char *ll_symbol; /* Label for beginning of location list.
1284 Only on head of list */
1285 const char *section; /* Section this loclist is relative to */
1286 dw_loc_descr_ref expr;
1287 hashval_t hash;
1288 /* True if all addresses in this and subsequent lists are known to be
1289 resolved. */
1290 bool resolved_addr;
1291 /* True if this list has been replaced by dw_loc_next. */
1292 bool replaced;
1293 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1294 section. */
1295 unsigned char emitted : 1;
1296 /* True if hash field is index rather than hash value. */
1297 unsigned char num_assigned : 1;
1298 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1299 unsigned char offset_emitted : 1;
1300 /* True if note_variable_value_in_expr has been called on it. */
1301 unsigned char noted_variable_value : 1;
1302 /* True if the range should be emitted even if begin and end
1303 are the same. */
1304 bool force;
1305 } dw_loc_list_node;
1307 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1308 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1310 /* Convert a DWARF stack opcode into its string name. */
1312 static const char *
1313 dwarf_stack_op_name (unsigned int op)
1315 const char *name = get_DW_OP_name (op);
1317 if (name != NULL)
1318 return name;
1320 return "OP_<unknown>";
1323 /* Return a pointer to a newly allocated location description. Location
1324 descriptions are simple expression terms that can be strung
1325 together to form more complicated location (address) descriptions. */
1327 static inline dw_loc_descr_ref
1328 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1329 unsigned HOST_WIDE_INT oprnd2)
1331 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1333 descr->dw_loc_opc = op;
1334 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1335 descr->dw_loc_oprnd1.val_entry = NULL;
1336 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1337 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1338 descr->dw_loc_oprnd2.val_entry = NULL;
1339 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1341 return descr;
1344 /* Return a pointer to a newly allocated location description for
1345 REG and OFFSET. */
1347 static inline dw_loc_descr_ref
1348 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1350 if (reg <= 31)
1351 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1352 offset, 0);
1353 else
1354 return new_loc_descr (DW_OP_bregx, reg, offset);
1357 /* Add a location description term to a location description expression. */
1359 static inline void
1360 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1362 dw_loc_descr_ref *d;
1364 /* Find the end of the chain. */
1365 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1368 *d = descr;
1371 /* Compare two location operands for exact equality. */
1373 static bool
1374 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1376 if (a->val_class != b->val_class)
1377 return false;
1378 switch (a->val_class)
1380 case dw_val_class_none:
1381 return true;
1382 case dw_val_class_addr:
1383 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1385 case dw_val_class_offset:
1386 case dw_val_class_unsigned_const:
1387 case dw_val_class_const:
1388 case dw_val_class_unsigned_const_implicit:
1389 case dw_val_class_const_implicit:
1390 case dw_val_class_range_list:
1391 /* These are all HOST_WIDE_INT, signed or unsigned. */
1392 return a->v.val_unsigned == b->v.val_unsigned;
1394 case dw_val_class_loc:
1395 return a->v.val_loc == b->v.val_loc;
1396 case dw_val_class_loc_list:
1397 return a->v.val_loc_list == b->v.val_loc_list;
1398 case dw_val_class_die_ref:
1399 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1400 case dw_val_class_fde_ref:
1401 return a->v.val_fde_index == b->v.val_fde_index;
1402 case dw_val_class_lbl_id:
1403 case dw_val_class_lineptr:
1404 case dw_val_class_macptr:
1405 case dw_val_class_loclistsptr:
1406 case dw_val_class_high_pc:
1407 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1408 case dw_val_class_str:
1409 return a->v.val_str == b->v.val_str;
1410 case dw_val_class_flag:
1411 return a->v.val_flag == b->v.val_flag;
1412 case dw_val_class_file:
1413 case dw_val_class_file_implicit:
1414 return a->v.val_file == b->v.val_file;
1415 case dw_val_class_decl_ref:
1416 return a->v.val_decl_ref == b->v.val_decl_ref;
1418 case dw_val_class_const_double:
1419 return (a->v.val_double.high == b->v.val_double.high
1420 && a->v.val_double.low == b->v.val_double.low);
1422 case dw_val_class_wide_int:
1423 return *a->v.val_wide == *b->v.val_wide;
1425 case dw_val_class_vec:
1427 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1428 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1430 return (a_len == b_len
1431 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1434 case dw_val_class_data8:
1435 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1437 case dw_val_class_vms_delta:
1438 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1439 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1441 case dw_val_class_discr_value:
1442 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1443 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1444 case dw_val_class_discr_list:
1445 /* It makes no sense comparing two discriminant value lists. */
1446 return false;
1448 gcc_unreachable ();
1451 /* Compare two location atoms for exact equality. */
1453 static bool
1454 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1456 if (a->dw_loc_opc != b->dw_loc_opc)
1457 return false;
1459 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1460 address size, but since we always allocate cleared storage it
1461 should be zero for other types of locations. */
1462 if (a->dtprel != b->dtprel)
1463 return false;
1465 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1466 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1469 /* Compare two complete location expressions for exact equality. */
1471 bool
1472 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1474 while (1)
1476 if (a == b)
1477 return true;
1478 if (a == NULL || b == NULL)
1479 return false;
1480 if (!loc_descr_equal_p_1 (a, b))
1481 return false;
1483 a = a->dw_loc_next;
1484 b = b->dw_loc_next;
1489 /* Add a constant OFFSET to a location expression. */
1491 static void
1492 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1494 dw_loc_descr_ref loc;
1495 HOST_WIDE_INT *p;
1497 gcc_assert (*list_head != NULL);
1499 if (!offset)
1500 return;
1502 /* Find the end of the chain. */
1503 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1506 p = NULL;
1507 if (loc->dw_loc_opc == DW_OP_fbreg
1508 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1509 p = &loc->dw_loc_oprnd1.v.val_int;
1510 else if (loc->dw_loc_opc == DW_OP_bregx)
1511 p = &loc->dw_loc_oprnd2.v.val_int;
1513 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1514 offset. Don't optimize if an signed integer overflow would happen. */
1515 if (p != NULL
1516 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1517 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1518 *p += offset;
1520 else if (offset > 0)
1521 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1523 else
1525 loc->dw_loc_next
1526 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1527 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1531 /* Add a constant OFFSET to a location list. */
1533 static void
1534 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1536 dw_loc_list_ref d;
1537 for (d = list_head; d != NULL; d = d->dw_loc_next)
1538 loc_descr_plus_const (&d->expr, offset);
1541 #define DWARF_REF_SIZE \
1542 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1544 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1545 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1546 DW_FORM_data16 with 128 bits. */
1547 #define DWARF_LARGEST_DATA_FORM_BITS \
1548 (dwarf_version >= 5 ? 128 : 64)
1550 /* Utility inline function for construction of ops that were GNU extension
1551 before DWARF 5. */
1552 static inline enum dwarf_location_atom
1553 dwarf_OP (enum dwarf_location_atom op)
1555 switch (op)
1557 case DW_OP_implicit_pointer:
1558 if (dwarf_version < 5)
1559 return DW_OP_GNU_implicit_pointer;
1560 break;
1562 case DW_OP_entry_value:
1563 if (dwarf_version < 5)
1564 return DW_OP_GNU_entry_value;
1565 break;
1567 case DW_OP_const_type:
1568 if (dwarf_version < 5)
1569 return DW_OP_GNU_const_type;
1570 break;
1572 case DW_OP_regval_type:
1573 if (dwarf_version < 5)
1574 return DW_OP_GNU_regval_type;
1575 break;
1577 case DW_OP_deref_type:
1578 if (dwarf_version < 5)
1579 return DW_OP_GNU_deref_type;
1580 break;
1582 case DW_OP_convert:
1583 if (dwarf_version < 5)
1584 return DW_OP_GNU_convert;
1585 break;
1587 case DW_OP_reinterpret:
1588 if (dwarf_version < 5)
1589 return DW_OP_GNU_reinterpret;
1590 break;
1592 default:
1593 break;
1595 return op;
1598 /* Similarly for attributes. */
1599 static inline enum dwarf_attribute
1600 dwarf_AT (enum dwarf_attribute at)
1602 switch (at)
1604 case DW_AT_call_return_pc:
1605 if (dwarf_version < 5)
1606 return DW_AT_low_pc;
1607 break;
1609 case DW_AT_call_tail_call:
1610 if (dwarf_version < 5)
1611 return DW_AT_GNU_tail_call;
1612 break;
1614 case DW_AT_call_origin:
1615 if (dwarf_version < 5)
1616 return DW_AT_abstract_origin;
1617 break;
1619 case DW_AT_call_target:
1620 if (dwarf_version < 5)
1621 return DW_AT_GNU_call_site_target;
1622 break;
1624 case DW_AT_call_target_clobbered:
1625 if (dwarf_version < 5)
1626 return DW_AT_GNU_call_site_target_clobbered;
1627 break;
1629 case DW_AT_call_parameter:
1630 if (dwarf_version < 5)
1631 return DW_AT_abstract_origin;
1632 break;
1634 case DW_AT_call_value:
1635 if (dwarf_version < 5)
1636 return DW_AT_GNU_call_site_value;
1637 break;
1639 case DW_AT_call_data_value:
1640 if (dwarf_version < 5)
1641 return DW_AT_GNU_call_site_data_value;
1642 break;
1644 case DW_AT_call_all_calls:
1645 if (dwarf_version < 5)
1646 return DW_AT_GNU_all_call_sites;
1647 break;
1649 case DW_AT_call_all_tail_calls:
1650 if (dwarf_version < 5)
1651 return DW_AT_GNU_all_tail_call_sites;
1652 break;
1654 case DW_AT_dwo_name:
1655 if (dwarf_version < 5)
1656 return DW_AT_GNU_dwo_name;
1657 break;
1659 default:
1660 break;
1662 return at;
1665 /* And similarly for tags. */
1666 static inline enum dwarf_tag
1667 dwarf_TAG (enum dwarf_tag tag)
1669 switch (tag)
1671 case DW_TAG_call_site:
1672 if (dwarf_version < 5)
1673 return DW_TAG_GNU_call_site;
1674 break;
1676 case DW_TAG_call_site_parameter:
1677 if (dwarf_version < 5)
1678 return DW_TAG_GNU_call_site_parameter;
1679 break;
1681 default:
1682 break;
1684 return tag;
1687 static unsigned long int get_base_type_offset (dw_die_ref);
1689 /* Return the size of a location descriptor. */
1691 static unsigned long
1692 size_of_loc_descr (dw_loc_descr_ref loc)
1694 unsigned long size = 1;
1696 switch (loc->dw_loc_opc)
1698 case DW_OP_addr:
1699 size += DWARF2_ADDR_SIZE;
1700 break;
1701 case DW_OP_GNU_addr_index:
1702 case DW_OP_GNU_const_index:
1703 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1704 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1705 break;
1706 case DW_OP_const1u:
1707 case DW_OP_const1s:
1708 size += 1;
1709 break;
1710 case DW_OP_const2u:
1711 case DW_OP_const2s:
1712 size += 2;
1713 break;
1714 case DW_OP_const4u:
1715 case DW_OP_const4s:
1716 size += 4;
1717 break;
1718 case DW_OP_const8u:
1719 case DW_OP_const8s:
1720 size += 8;
1721 break;
1722 case DW_OP_constu:
1723 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1724 break;
1725 case DW_OP_consts:
1726 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1727 break;
1728 case DW_OP_pick:
1729 size += 1;
1730 break;
1731 case DW_OP_plus_uconst:
1732 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1733 break;
1734 case DW_OP_skip:
1735 case DW_OP_bra:
1736 size += 2;
1737 break;
1738 case DW_OP_breg0:
1739 case DW_OP_breg1:
1740 case DW_OP_breg2:
1741 case DW_OP_breg3:
1742 case DW_OP_breg4:
1743 case DW_OP_breg5:
1744 case DW_OP_breg6:
1745 case DW_OP_breg7:
1746 case DW_OP_breg8:
1747 case DW_OP_breg9:
1748 case DW_OP_breg10:
1749 case DW_OP_breg11:
1750 case DW_OP_breg12:
1751 case DW_OP_breg13:
1752 case DW_OP_breg14:
1753 case DW_OP_breg15:
1754 case DW_OP_breg16:
1755 case DW_OP_breg17:
1756 case DW_OP_breg18:
1757 case DW_OP_breg19:
1758 case DW_OP_breg20:
1759 case DW_OP_breg21:
1760 case DW_OP_breg22:
1761 case DW_OP_breg23:
1762 case DW_OP_breg24:
1763 case DW_OP_breg25:
1764 case DW_OP_breg26:
1765 case DW_OP_breg27:
1766 case DW_OP_breg28:
1767 case DW_OP_breg29:
1768 case DW_OP_breg30:
1769 case DW_OP_breg31:
1770 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1771 break;
1772 case DW_OP_regx:
1773 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1774 break;
1775 case DW_OP_fbreg:
1776 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1777 break;
1778 case DW_OP_bregx:
1779 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1780 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1781 break;
1782 case DW_OP_piece:
1783 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1784 break;
1785 case DW_OP_bit_piece:
1786 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1787 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1788 break;
1789 case DW_OP_deref_size:
1790 case DW_OP_xderef_size:
1791 size += 1;
1792 break;
1793 case DW_OP_call2:
1794 size += 2;
1795 break;
1796 case DW_OP_call4:
1797 size += 4;
1798 break;
1799 case DW_OP_call_ref:
1800 case DW_OP_GNU_variable_value:
1801 size += DWARF_REF_SIZE;
1802 break;
1803 case DW_OP_implicit_value:
1804 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1805 + loc->dw_loc_oprnd1.v.val_unsigned;
1806 break;
1807 case DW_OP_implicit_pointer:
1808 case DW_OP_GNU_implicit_pointer:
1809 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1810 break;
1811 case DW_OP_entry_value:
1812 case DW_OP_GNU_entry_value:
1814 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1815 size += size_of_uleb128 (op_size) + op_size;
1816 break;
1818 case DW_OP_const_type:
1819 case DW_OP_GNU_const_type:
1821 unsigned long o
1822 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1823 size += size_of_uleb128 (o) + 1;
1824 switch (loc->dw_loc_oprnd2.val_class)
1826 case dw_val_class_vec:
1827 size += loc->dw_loc_oprnd2.v.val_vec.length
1828 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1829 break;
1830 case dw_val_class_const:
1831 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1832 break;
1833 case dw_val_class_const_double:
1834 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1835 break;
1836 case dw_val_class_wide_int:
1837 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1838 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1839 break;
1840 default:
1841 gcc_unreachable ();
1843 break;
1845 case DW_OP_regval_type:
1846 case DW_OP_GNU_regval_type:
1848 unsigned long o
1849 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1850 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1851 + size_of_uleb128 (o);
1853 break;
1854 case DW_OP_deref_type:
1855 case DW_OP_GNU_deref_type:
1857 unsigned long o
1858 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1859 size += 1 + size_of_uleb128 (o);
1861 break;
1862 case DW_OP_convert:
1863 case DW_OP_reinterpret:
1864 case DW_OP_GNU_convert:
1865 case DW_OP_GNU_reinterpret:
1866 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1867 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1868 else
1870 unsigned long o
1871 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1872 size += size_of_uleb128 (o);
1874 break;
1875 case DW_OP_GNU_parameter_ref:
1876 size += 4;
1877 break;
1878 default:
1879 break;
1882 return size;
1885 /* Return the size of a series of location descriptors. */
1887 unsigned long
1888 size_of_locs (dw_loc_descr_ref loc)
1890 dw_loc_descr_ref l;
1891 unsigned long size;
1893 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1894 field, to avoid writing to a PCH file. */
1895 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1897 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1898 break;
1899 size += size_of_loc_descr (l);
1901 if (! l)
1902 return size;
1904 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1906 l->dw_loc_addr = size;
1907 size += size_of_loc_descr (l);
1910 return size;
1913 /* Return the size of the value in a DW_AT_discr_value attribute. */
1915 static int
1916 size_of_discr_value (dw_discr_value *discr_value)
1918 if (discr_value->pos)
1919 return size_of_uleb128 (discr_value->v.uval);
1920 else
1921 return size_of_sleb128 (discr_value->v.sval);
1924 /* Return the size of the value in a DW_AT_discr_list attribute. */
1926 static int
1927 size_of_discr_list (dw_discr_list_ref discr_list)
1929 int size = 0;
1931 for (dw_discr_list_ref list = discr_list;
1932 list != NULL;
1933 list = list->dw_discr_next)
1935 /* One byte for the discriminant value descriptor, and then one or two
1936 LEB128 numbers, depending on whether it's a single case label or a
1937 range label. */
1938 size += 1;
1939 size += size_of_discr_value (&list->dw_discr_lower_bound);
1940 if (list->dw_discr_range != 0)
1941 size += size_of_discr_value (&list->dw_discr_upper_bound);
1943 return size;
1946 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1947 static void get_ref_die_offset_label (char *, dw_die_ref);
1948 static unsigned long int get_ref_die_offset (dw_die_ref);
1950 /* Output location description stack opcode's operands (if any).
1951 The for_eh_or_skip parameter controls whether register numbers are
1952 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1953 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1954 info). This should be suppressed for the cases that have not been converted
1955 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1957 static void
1958 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1960 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1961 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1963 switch (loc->dw_loc_opc)
1965 #ifdef DWARF2_DEBUGGING_INFO
1966 case DW_OP_const2u:
1967 case DW_OP_const2s:
1968 dw2_asm_output_data (2, val1->v.val_int, NULL);
1969 break;
1970 case DW_OP_const4u:
1971 if (loc->dtprel)
1973 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1974 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1975 val1->v.val_addr);
1976 fputc ('\n', asm_out_file);
1977 break;
1979 /* FALLTHRU */
1980 case DW_OP_const4s:
1981 dw2_asm_output_data (4, val1->v.val_int, NULL);
1982 break;
1983 case DW_OP_const8u:
1984 if (loc->dtprel)
1986 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1987 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1988 val1->v.val_addr);
1989 fputc ('\n', asm_out_file);
1990 break;
1992 /* FALLTHRU */
1993 case DW_OP_const8s:
1994 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1995 dw2_asm_output_data (8, val1->v.val_int, NULL);
1996 break;
1997 case DW_OP_skip:
1998 case DW_OP_bra:
2000 int offset;
2002 gcc_assert (val1->val_class == dw_val_class_loc);
2003 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2005 dw2_asm_output_data (2, offset, NULL);
2007 break;
2008 case DW_OP_implicit_value:
2009 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2010 switch (val2->val_class)
2012 case dw_val_class_const:
2013 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2014 break;
2015 case dw_val_class_vec:
2017 unsigned int elt_size = val2->v.val_vec.elt_size;
2018 unsigned int len = val2->v.val_vec.length;
2019 unsigned int i;
2020 unsigned char *p;
2022 if (elt_size > sizeof (HOST_WIDE_INT))
2024 elt_size /= 2;
2025 len *= 2;
2027 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2028 i < len;
2029 i++, p += elt_size)
2030 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2031 "fp or vector constant word %u", i);
2033 break;
2034 case dw_val_class_const_double:
2036 unsigned HOST_WIDE_INT first, second;
2038 if (WORDS_BIG_ENDIAN)
2040 first = val2->v.val_double.high;
2041 second = val2->v.val_double.low;
2043 else
2045 first = val2->v.val_double.low;
2046 second = val2->v.val_double.high;
2048 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2049 first, NULL);
2050 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2051 second, NULL);
2053 break;
2054 case dw_val_class_wide_int:
2056 int i;
2057 int len = get_full_len (*val2->v.val_wide);
2058 if (WORDS_BIG_ENDIAN)
2059 for (i = len - 1; i >= 0; --i)
2060 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2061 val2->v.val_wide->elt (i), NULL);
2062 else
2063 for (i = 0; i < len; ++i)
2064 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2065 val2->v.val_wide->elt (i), NULL);
2067 break;
2068 case dw_val_class_addr:
2069 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2070 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2071 break;
2072 default:
2073 gcc_unreachable ();
2075 break;
2076 #else
2077 case DW_OP_const2u:
2078 case DW_OP_const2s:
2079 case DW_OP_const4u:
2080 case DW_OP_const4s:
2081 case DW_OP_const8u:
2082 case DW_OP_const8s:
2083 case DW_OP_skip:
2084 case DW_OP_bra:
2085 case DW_OP_implicit_value:
2086 /* We currently don't make any attempt to make sure these are
2087 aligned properly like we do for the main unwind info, so
2088 don't support emitting things larger than a byte if we're
2089 only doing unwinding. */
2090 gcc_unreachable ();
2091 #endif
2092 case DW_OP_const1u:
2093 case DW_OP_const1s:
2094 dw2_asm_output_data (1, val1->v.val_int, NULL);
2095 break;
2096 case DW_OP_constu:
2097 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2098 break;
2099 case DW_OP_consts:
2100 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2101 break;
2102 case DW_OP_pick:
2103 dw2_asm_output_data (1, val1->v.val_int, NULL);
2104 break;
2105 case DW_OP_plus_uconst:
2106 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2107 break;
2108 case DW_OP_breg0:
2109 case DW_OP_breg1:
2110 case DW_OP_breg2:
2111 case DW_OP_breg3:
2112 case DW_OP_breg4:
2113 case DW_OP_breg5:
2114 case DW_OP_breg6:
2115 case DW_OP_breg7:
2116 case DW_OP_breg8:
2117 case DW_OP_breg9:
2118 case DW_OP_breg10:
2119 case DW_OP_breg11:
2120 case DW_OP_breg12:
2121 case DW_OP_breg13:
2122 case DW_OP_breg14:
2123 case DW_OP_breg15:
2124 case DW_OP_breg16:
2125 case DW_OP_breg17:
2126 case DW_OP_breg18:
2127 case DW_OP_breg19:
2128 case DW_OP_breg20:
2129 case DW_OP_breg21:
2130 case DW_OP_breg22:
2131 case DW_OP_breg23:
2132 case DW_OP_breg24:
2133 case DW_OP_breg25:
2134 case DW_OP_breg26:
2135 case DW_OP_breg27:
2136 case DW_OP_breg28:
2137 case DW_OP_breg29:
2138 case DW_OP_breg30:
2139 case DW_OP_breg31:
2140 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2141 break;
2142 case DW_OP_regx:
2144 unsigned r = val1->v.val_unsigned;
2145 if (for_eh_or_skip >= 0)
2146 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2147 gcc_assert (size_of_uleb128 (r)
2148 == size_of_uleb128 (val1->v.val_unsigned));
2149 dw2_asm_output_data_uleb128 (r, NULL);
2151 break;
2152 case DW_OP_fbreg:
2153 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2154 break;
2155 case DW_OP_bregx:
2157 unsigned r = val1->v.val_unsigned;
2158 if (for_eh_or_skip >= 0)
2159 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2160 gcc_assert (size_of_uleb128 (r)
2161 == size_of_uleb128 (val1->v.val_unsigned));
2162 dw2_asm_output_data_uleb128 (r, NULL);
2163 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2165 break;
2166 case DW_OP_piece:
2167 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2168 break;
2169 case DW_OP_bit_piece:
2170 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2171 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2172 break;
2173 case DW_OP_deref_size:
2174 case DW_OP_xderef_size:
2175 dw2_asm_output_data (1, val1->v.val_int, NULL);
2176 break;
2178 case DW_OP_addr:
2179 if (loc->dtprel)
2181 if (targetm.asm_out.output_dwarf_dtprel)
2183 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2184 DWARF2_ADDR_SIZE,
2185 val1->v.val_addr);
2186 fputc ('\n', asm_out_file);
2188 else
2189 gcc_unreachable ();
2191 else
2193 #ifdef DWARF2_DEBUGGING_INFO
2194 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2195 #else
2196 gcc_unreachable ();
2197 #endif
2199 break;
2201 case DW_OP_GNU_addr_index:
2202 case DW_OP_GNU_const_index:
2203 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2204 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2205 "(index into .debug_addr)");
2206 break;
2208 case DW_OP_call2:
2209 case DW_OP_call4:
2211 unsigned long die_offset
2212 = get_ref_die_offset (val1->v.val_die_ref.die);
2213 /* Make sure the offset has been computed and that we can encode it as
2214 an operand. */
2215 gcc_assert (die_offset > 0
2216 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2217 ? 0xffff
2218 : 0xffffffff));
2219 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2220 die_offset, NULL);
2222 break;
2224 case DW_OP_call_ref:
2225 case DW_OP_GNU_variable_value:
2227 char label[MAX_ARTIFICIAL_LABEL_BYTES
2228 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2229 gcc_assert (val1->val_class == dw_val_class_die_ref);
2230 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2231 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2233 break;
2235 case DW_OP_implicit_pointer:
2236 case DW_OP_GNU_implicit_pointer:
2238 char label[MAX_ARTIFICIAL_LABEL_BYTES
2239 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2240 gcc_assert (val1->val_class == dw_val_class_die_ref);
2241 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2242 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2243 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2245 break;
2247 case DW_OP_entry_value:
2248 case DW_OP_GNU_entry_value:
2249 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2250 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2251 break;
2253 case DW_OP_const_type:
2254 case DW_OP_GNU_const_type:
2256 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2257 gcc_assert (o);
2258 dw2_asm_output_data_uleb128 (o, NULL);
2259 switch (val2->val_class)
2261 case dw_val_class_const:
2262 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2263 dw2_asm_output_data (1, l, NULL);
2264 dw2_asm_output_data (l, val2->v.val_int, NULL);
2265 break;
2266 case dw_val_class_vec:
2268 unsigned int elt_size = val2->v.val_vec.elt_size;
2269 unsigned int len = val2->v.val_vec.length;
2270 unsigned int i;
2271 unsigned char *p;
2273 l = len * elt_size;
2274 dw2_asm_output_data (1, l, NULL);
2275 if (elt_size > sizeof (HOST_WIDE_INT))
2277 elt_size /= 2;
2278 len *= 2;
2280 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2281 i < len;
2282 i++, p += elt_size)
2283 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2284 "fp or vector constant word %u", i);
2286 break;
2287 case dw_val_class_const_double:
2289 unsigned HOST_WIDE_INT first, second;
2290 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2292 dw2_asm_output_data (1, 2 * l, NULL);
2293 if (WORDS_BIG_ENDIAN)
2295 first = val2->v.val_double.high;
2296 second = val2->v.val_double.low;
2298 else
2300 first = val2->v.val_double.low;
2301 second = val2->v.val_double.high;
2303 dw2_asm_output_data (l, first, NULL);
2304 dw2_asm_output_data (l, second, NULL);
2306 break;
2307 case dw_val_class_wide_int:
2309 int i;
2310 int len = get_full_len (*val2->v.val_wide);
2311 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2313 dw2_asm_output_data (1, len * l, NULL);
2314 if (WORDS_BIG_ENDIAN)
2315 for (i = len - 1; i >= 0; --i)
2316 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2317 else
2318 for (i = 0; i < len; ++i)
2319 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2321 break;
2322 default:
2323 gcc_unreachable ();
2326 break;
2327 case DW_OP_regval_type:
2328 case DW_OP_GNU_regval_type:
2330 unsigned r = val1->v.val_unsigned;
2331 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2332 gcc_assert (o);
2333 if (for_eh_or_skip >= 0)
2335 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2336 gcc_assert (size_of_uleb128 (r)
2337 == size_of_uleb128 (val1->v.val_unsigned));
2339 dw2_asm_output_data_uleb128 (r, NULL);
2340 dw2_asm_output_data_uleb128 (o, NULL);
2342 break;
2343 case DW_OP_deref_type:
2344 case DW_OP_GNU_deref_type:
2346 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2347 gcc_assert (o);
2348 dw2_asm_output_data (1, val1->v.val_int, NULL);
2349 dw2_asm_output_data_uleb128 (o, NULL);
2351 break;
2352 case DW_OP_convert:
2353 case DW_OP_reinterpret:
2354 case DW_OP_GNU_convert:
2355 case DW_OP_GNU_reinterpret:
2356 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2357 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2358 else
2360 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2361 gcc_assert (o);
2362 dw2_asm_output_data_uleb128 (o, NULL);
2364 break;
2366 case DW_OP_GNU_parameter_ref:
2368 unsigned long o;
2369 gcc_assert (val1->val_class == dw_val_class_die_ref);
2370 o = get_ref_die_offset (val1->v.val_die_ref.die);
2371 dw2_asm_output_data (4, o, NULL);
2373 break;
2375 default:
2376 /* Other codes have no operands. */
2377 break;
2381 /* Output a sequence of location operations.
2382 The for_eh_or_skip parameter controls whether register numbers are
2383 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2384 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2385 info). This should be suppressed for the cases that have not been converted
2386 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2388 void
2389 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2391 for (; loc != NULL; loc = loc->dw_loc_next)
2393 enum dwarf_location_atom opc = loc->dw_loc_opc;
2394 /* Output the opcode. */
2395 if (for_eh_or_skip >= 0
2396 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2398 unsigned r = (opc - DW_OP_breg0);
2399 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2400 gcc_assert (r <= 31);
2401 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2403 else if (for_eh_or_skip >= 0
2404 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2406 unsigned r = (opc - DW_OP_reg0);
2407 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2408 gcc_assert (r <= 31);
2409 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2412 dw2_asm_output_data (1, opc,
2413 "%s", dwarf_stack_op_name (opc));
2415 /* Output the operand(s) (if any). */
2416 output_loc_operands (loc, for_eh_or_skip);
2420 /* Output location description stack opcode's operands (if any).
2421 The output is single bytes on a line, suitable for .cfi_escape. */
2423 static void
2424 output_loc_operands_raw (dw_loc_descr_ref loc)
2426 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2427 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2429 switch (loc->dw_loc_opc)
2431 case DW_OP_addr:
2432 case DW_OP_GNU_addr_index:
2433 case DW_OP_GNU_const_index:
2434 case DW_OP_implicit_value:
2435 /* We cannot output addresses in .cfi_escape, only bytes. */
2436 gcc_unreachable ();
2438 case DW_OP_const1u:
2439 case DW_OP_const1s:
2440 case DW_OP_pick:
2441 case DW_OP_deref_size:
2442 case DW_OP_xderef_size:
2443 fputc (',', asm_out_file);
2444 dw2_asm_output_data_raw (1, val1->v.val_int);
2445 break;
2447 case DW_OP_const2u:
2448 case DW_OP_const2s:
2449 fputc (',', asm_out_file);
2450 dw2_asm_output_data_raw (2, val1->v.val_int);
2451 break;
2453 case DW_OP_const4u:
2454 case DW_OP_const4s:
2455 fputc (',', asm_out_file);
2456 dw2_asm_output_data_raw (4, val1->v.val_int);
2457 break;
2459 case DW_OP_const8u:
2460 case DW_OP_const8s:
2461 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2462 fputc (',', asm_out_file);
2463 dw2_asm_output_data_raw (8, val1->v.val_int);
2464 break;
2466 case DW_OP_skip:
2467 case DW_OP_bra:
2469 int offset;
2471 gcc_assert (val1->val_class == dw_val_class_loc);
2472 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2474 fputc (',', asm_out_file);
2475 dw2_asm_output_data_raw (2, offset);
2477 break;
2479 case DW_OP_regx:
2481 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2482 gcc_assert (size_of_uleb128 (r)
2483 == size_of_uleb128 (val1->v.val_unsigned));
2484 fputc (',', asm_out_file);
2485 dw2_asm_output_data_uleb128_raw (r);
2487 break;
2489 case DW_OP_constu:
2490 case DW_OP_plus_uconst:
2491 case DW_OP_piece:
2492 fputc (',', asm_out_file);
2493 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2494 break;
2496 case DW_OP_bit_piece:
2497 fputc (',', asm_out_file);
2498 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2499 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2500 break;
2502 case DW_OP_consts:
2503 case DW_OP_breg0:
2504 case DW_OP_breg1:
2505 case DW_OP_breg2:
2506 case DW_OP_breg3:
2507 case DW_OP_breg4:
2508 case DW_OP_breg5:
2509 case DW_OP_breg6:
2510 case DW_OP_breg7:
2511 case DW_OP_breg8:
2512 case DW_OP_breg9:
2513 case DW_OP_breg10:
2514 case DW_OP_breg11:
2515 case DW_OP_breg12:
2516 case DW_OP_breg13:
2517 case DW_OP_breg14:
2518 case DW_OP_breg15:
2519 case DW_OP_breg16:
2520 case DW_OP_breg17:
2521 case DW_OP_breg18:
2522 case DW_OP_breg19:
2523 case DW_OP_breg20:
2524 case DW_OP_breg21:
2525 case DW_OP_breg22:
2526 case DW_OP_breg23:
2527 case DW_OP_breg24:
2528 case DW_OP_breg25:
2529 case DW_OP_breg26:
2530 case DW_OP_breg27:
2531 case DW_OP_breg28:
2532 case DW_OP_breg29:
2533 case DW_OP_breg30:
2534 case DW_OP_breg31:
2535 case DW_OP_fbreg:
2536 fputc (',', asm_out_file);
2537 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2538 break;
2540 case DW_OP_bregx:
2542 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2543 gcc_assert (size_of_uleb128 (r)
2544 == size_of_uleb128 (val1->v.val_unsigned));
2545 fputc (',', asm_out_file);
2546 dw2_asm_output_data_uleb128_raw (r);
2547 fputc (',', asm_out_file);
2548 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2550 break;
2552 case DW_OP_implicit_pointer:
2553 case DW_OP_entry_value:
2554 case DW_OP_const_type:
2555 case DW_OP_regval_type:
2556 case DW_OP_deref_type:
2557 case DW_OP_convert:
2558 case DW_OP_reinterpret:
2559 case DW_OP_GNU_implicit_pointer:
2560 case DW_OP_GNU_entry_value:
2561 case DW_OP_GNU_const_type:
2562 case DW_OP_GNU_regval_type:
2563 case DW_OP_GNU_deref_type:
2564 case DW_OP_GNU_convert:
2565 case DW_OP_GNU_reinterpret:
2566 case DW_OP_GNU_parameter_ref:
2567 gcc_unreachable ();
2568 break;
2570 default:
2571 /* Other codes have no operands. */
2572 break;
2576 void
2577 output_loc_sequence_raw (dw_loc_descr_ref loc)
2579 while (1)
2581 enum dwarf_location_atom opc = loc->dw_loc_opc;
2582 /* Output the opcode. */
2583 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2585 unsigned r = (opc - DW_OP_breg0);
2586 r = DWARF2_FRAME_REG_OUT (r, 1);
2587 gcc_assert (r <= 31);
2588 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2590 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2592 unsigned r = (opc - DW_OP_reg0);
2593 r = DWARF2_FRAME_REG_OUT (r, 1);
2594 gcc_assert (r <= 31);
2595 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2597 /* Output the opcode. */
2598 fprintf (asm_out_file, "%#x", opc);
2599 output_loc_operands_raw (loc);
2601 if (!loc->dw_loc_next)
2602 break;
2603 loc = loc->dw_loc_next;
2605 fputc (',', asm_out_file);
2609 /* This function builds a dwarf location descriptor sequence from a
2610 dw_cfa_location, adding the given OFFSET to the result of the
2611 expression. */
2613 struct dw_loc_descr_node *
2614 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2616 struct dw_loc_descr_node *head, *tmp;
2618 offset += cfa->offset;
2620 if (cfa->indirect)
2622 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2623 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2624 head->dw_loc_oprnd1.val_entry = NULL;
2625 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2626 add_loc_descr (&head, tmp);
2627 if (offset != 0)
2629 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2630 add_loc_descr (&head, tmp);
2633 else
2634 head = new_reg_loc_descr (cfa->reg, offset);
2636 return head;
2639 /* This function builds a dwarf location descriptor sequence for
2640 the address at OFFSET from the CFA when stack is aligned to
2641 ALIGNMENT byte. */
2643 struct dw_loc_descr_node *
2644 build_cfa_aligned_loc (dw_cfa_location *cfa,
2645 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2647 struct dw_loc_descr_node *head;
2648 unsigned int dwarf_fp
2649 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2651 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2652 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2654 head = new_reg_loc_descr (dwarf_fp, 0);
2655 add_loc_descr (&head, int_loc_descriptor (alignment));
2656 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2657 loc_descr_plus_const (&head, offset);
2659 else
2660 head = new_reg_loc_descr (dwarf_fp, offset);
2661 return head;
2664 /* And now, the support for symbolic debugging information. */
2666 /* .debug_str support. */
2668 static void dwarf2out_init (const char *);
2669 static void dwarf2out_finish (const char *);
2670 static void dwarf2out_early_finish (const char *);
2671 static void dwarf2out_assembly_start (void);
2672 static void dwarf2out_define (unsigned int, const char *);
2673 static void dwarf2out_undef (unsigned int, const char *);
2674 static void dwarf2out_start_source_file (unsigned, const char *);
2675 static void dwarf2out_end_source_file (unsigned);
2676 static void dwarf2out_function_decl (tree);
2677 static void dwarf2out_begin_block (unsigned, unsigned);
2678 static void dwarf2out_end_block (unsigned, unsigned);
2679 static bool dwarf2out_ignore_block (const_tree);
2680 static void dwarf2out_early_global_decl (tree);
2681 static void dwarf2out_late_global_decl (tree);
2682 static void dwarf2out_type_decl (tree, int);
2683 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2684 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2685 dw_die_ref);
2686 static void dwarf2out_abstract_function (tree);
2687 static void dwarf2out_var_location (rtx_insn *);
2688 static void dwarf2out_size_function (tree);
2689 static void dwarf2out_begin_function (tree);
2690 static void dwarf2out_end_function (unsigned int);
2691 static void dwarf2out_register_main_translation_unit (tree unit);
2692 static void dwarf2out_set_name (tree, tree);
2694 /* The debug hooks structure. */
2696 const struct gcc_debug_hooks dwarf2_debug_hooks =
2698 dwarf2out_init,
2699 dwarf2out_finish,
2700 dwarf2out_early_finish,
2701 dwarf2out_assembly_start,
2702 dwarf2out_define,
2703 dwarf2out_undef,
2704 dwarf2out_start_source_file,
2705 dwarf2out_end_source_file,
2706 dwarf2out_begin_block,
2707 dwarf2out_end_block,
2708 dwarf2out_ignore_block,
2709 dwarf2out_source_line,
2710 dwarf2out_begin_prologue,
2711 #if VMS_DEBUGGING_INFO
2712 dwarf2out_vms_end_prologue,
2713 dwarf2out_vms_begin_epilogue,
2714 #else
2715 debug_nothing_int_charstar,
2716 debug_nothing_int_charstar,
2717 #endif
2718 dwarf2out_end_epilogue,
2719 dwarf2out_begin_function,
2720 dwarf2out_end_function, /* end_function */
2721 dwarf2out_register_main_translation_unit,
2722 dwarf2out_function_decl, /* function_decl */
2723 dwarf2out_early_global_decl,
2724 dwarf2out_late_global_decl,
2725 dwarf2out_type_decl, /* type_decl */
2726 dwarf2out_imported_module_or_decl,
2727 debug_nothing_tree, /* deferred_inline_function */
2728 /* The DWARF 2 backend tries to reduce debugging bloat by not
2729 emitting the abstract description of inline functions until
2730 something tries to reference them. */
2731 dwarf2out_abstract_function, /* outlining_inline_function */
2732 debug_nothing_rtx_code_label, /* label */
2733 debug_nothing_int, /* handle_pch */
2734 dwarf2out_var_location,
2735 dwarf2out_size_function, /* size_function */
2736 dwarf2out_switch_text_section,
2737 dwarf2out_set_name,
2738 1, /* start_end_main_source_file */
2739 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2742 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2744 dwarf2out_init,
2745 debug_nothing_charstar,
2746 debug_nothing_charstar,
2747 dwarf2out_assembly_start,
2748 debug_nothing_int_charstar,
2749 debug_nothing_int_charstar,
2750 debug_nothing_int_charstar,
2751 debug_nothing_int,
2752 debug_nothing_int_int, /* begin_block */
2753 debug_nothing_int_int, /* end_block */
2754 debug_true_const_tree, /* ignore_block */
2755 dwarf2out_source_line, /* source_line */
2756 debug_nothing_int_int_charstar, /* begin_prologue */
2757 debug_nothing_int_charstar, /* end_prologue */
2758 debug_nothing_int_charstar, /* begin_epilogue */
2759 debug_nothing_int_charstar, /* end_epilogue */
2760 debug_nothing_tree, /* begin_function */
2761 debug_nothing_int, /* end_function */
2762 debug_nothing_tree, /* register_main_translation_unit */
2763 debug_nothing_tree, /* function_decl */
2764 debug_nothing_tree, /* early_global_decl */
2765 debug_nothing_tree, /* late_global_decl */
2766 debug_nothing_tree_int, /* type_decl */
2767 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2768 debug_nothing_tree, /* deferred_inline_function */
2769 debug_nothing_tree, /* outlining_inline_function */
2770 debug_nothing_rtx_code_label, /* label */
2771 debug_nothing_int, /* handle_pch */
2772 debug_nothing_rtx_insn, /* var_location */
2773 debug_nothing_tree, /* size_function */
2774 debug_nothing_void, /* switch_text_section */
2775 debug_nothing_tree_tree, /* set_name */
2776 0, /* start_end_main_source_file */
2777 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2780 /* NOTE: In the comments in this file, many references are made to
2781 "Debugging Information Entries". This term is abbreviated as `DIE'
2782 throughout the remainder of this file. */
2784 /* An internal representation of the DWARF output is built, and then
2785 walked to generate the DWARF debugging info. The walk of the internal
2786 representation is done after the entire program has been compiled.
2787 The types below are used to describe the internal representation. */
2789 /* Whether to put type DIEs into their own section .debug_types instead
2790 of making them part of the .debug_info section. Only supported for
2791 Dwarf V4 or higher and the user didn't disable them through
2792 -fno-debug-types-section. It is more efficient to put them in a
2793 separate comdat sections since the linker will then be able to
2794 remove duplicates. But not all tools support .debug_types sections
2795 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2796 it is DW_UT_type unit type in .debug_info section. */
2798 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2800 /* Various DIE's use offsets relative to the beginning of the
2801 .debug_info section to refer to each other. */
2803 typedef long int dw_offset;
2805 struct comdat_type_node;
2807 /* The entries in the line_info table more-or-less mirror the opcodes
2808 that are used in the real dwarf line table. Arrays of these entries
2809 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2810 supported. */
2812 enum dw_line_info_opcode {
2813 /* Emit DW_LNE_set_address; the operand is the label index. */
2814 LI_set_address,
2816 /* Emit a row to the matrix with the given line. This may be done
2817 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2818 special opcodes. */
2819 LI_set_line,
2821 /* Emit a DW_LNS_set_file. */
2822 LI_set_file,
2824 /* Emit a DW_LNS_set_column. */
2825 LI_set_column,
2827 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2828 LI_negate_stmt,
2830 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2831 LI_set_prologue_end,
2832 LI_set_epilogue_begin,
2834 /* Emit a DW_LNE_set_discriminator. */
2835 LI_set_discriminator
2838 typedef struct GTY(()) dw_line_info_struct {
2839 enum dw_line_info_opcode opcode;
2840 unsigned int val;
2841 } dw_line_info_entry;
2844 struct GTY(()) dw_line_info_table {
2845 /* The label that marks the end of this section. */
2846 const char *end_label;
2848 /* The values for the last row of the matrix, as collected in the table.
2849 These are used to minimize the changes to the next row. */
2850 unsigned int file_num;
2851 unsigned int line_num;
2852 unsigned int column_num;
2853 int discrim_num;
2854 bool is_stmt;
2855 bool in_use;
2857 vec<dw_line_info_entry, va_gc> *entries;
2861 /* Each DIE attribute has a field specifying the attribute kind,
2862 a link to the next attribute in the chain, and an attribute value.
2863 Attributes are typically linked below the DIE they modify. */
2865 typedef struct GTY(()) dw_attr_struct {
2866 enum dwarf_attribute dw_attr;
2867 dw_val_node dw_attr_val;
2869 dw_attr_node;
2872 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2873 The children of each node form a circular list linked by
2874 die_sib. die_child points to the node *before* the "first" child node. */
2876 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2877 union die_symbol_or_type_node
2879 const char * GTY ((tag ("0"))) die_symbol;
2880 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2882 GTY ((desc ("%0.comdat_type_p"))) die_id;
2883 vec<dw_attr_node, va_gc> *die_attr;
2884 dw_die_ref die_parent;
2885 dw_die_ref die_child;
2886 dw_die_ref die_sib;
2887 dw_die_ref die_definition; /* ref from a specification to its definition */
2888 dw_offset die_offset;
2889 unsigned long die_abbrev;
2890 int die_mark;
2891 unsigned int decl_id;
2892 enum dwarf_tag die_tag;
2893 /* Die is used and must not be pruned as unused. */
2894 BOOL_BITFIELD die_perennial_p : 1;
2895 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2896 /* Whether this DIE was removed from the DIE tree, for example via
2897 prune_unused_types. We don't consider those present from the
2898 DIE lookup routines. */
2899 BOOL_BITFIELD removed : 1;
2900 /* Lots of spare bits. */
2902 die_node;
2904 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2905 static bool early_dwarf;
2906 static bool early_dwarf_finished;
2907 struct set_early_dwarf {
2908 bool saved;
2909 set_early_dwarf () : saved(early_dwarf)
2911 gcc_assert (! early_dwarf_finished);
2912 early_dwarf = true;
2914 ~set_early_dwarf () { early_dwarf = saved; }
2917 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2918 #define FOR_EACH_CHILD(die, c, expr) do { \
2919 c = die->die_child; \
2920 if (c) do { \
2921 c = c->die_sib; \
2922 expr; \
2923 } while (c != die->die_child); \
2924 } while (0)
2926 /* The pubname structure */
2928 typedef struct GTY(()) pubname_struct {
2929 dw_die_ref die;
2930 const char *name;
2932 pubname_entry;
2935 struct GTY(()) dw_ranges {
2936 const char *label;
2937 /* If this is positive, it's a block number, otherwise it's a
2938 bitwise-negated index into dw_ranges_by_label. */
2939 int num;
2940 /* Index for the range list for DW_FORM_rnglistx. */
2941 unsigned int idx : 31;
2942 /* True if this range might be possibly in a different section
2943 from previous entry. */
2944 unsigned int maybe_new_sec : 1;
2947 /* A structure to hold a macinfo entry. */
2949 typedef struct GTY(()) macinfo_struct {
2950 unsigned char code;
2951 unsigned HOST_WIDE_INT lineno;
2952 const char *info;
2954 macinfo_entry;
2957 struct GTY(()) dw_ranges_by_label {
2958 const char *begin;
2959 const char *end;
2962 /* The comdat type node structure. */
2963 struct GTY(()) comdat_type_node
2965 dw_die_ref root_die;
2966 dw_die_ref type_die;
2967 dw_die_ref skeleton_die;
2968 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2969 comdat_type_node *next;
2972 /* A list of DIEs for which we can't determine ancestry (parent_die
2973 field) just yet. Later in dwarf2out_finish we will fill in the
2974 missing bits. */
2975 typedef struct GTY(()) limbo_die_struct {
2976 dw_die_ref die;
2977 /* The tree for which this DIE was created. We use this to
2978 determine ancestry later. */
2979 tree created_for;
2980 struct limbo_die_struct *next;
2982 limbo_die_node;
2984 typedef struct skeleton_chain_struct
2986 dw_die_ref old_die;
2987 dw_die_ref new_die;
2988 struct skeleton_chain_struct *parent;
2990 skeleton_chain_node;
2992 /* Define a macro which returns nonzero for a TYPE_DECL which was
2993 implicitly generated for a type.
2995 Note that, unlike the C front-end (which generates a NULL named
2996 TYPE_DECL node for each complete tagged type, each array type,
2997 and each function type node created) the C++ front-end generates
2998 a _named_ TYPE_DECL node for each tagged type node created.
2999 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3000 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3001 front-end, but for each type, tagged or not. */
3003 #define TYPE_DECL_IS_STUB(decl) \
3004 (DECL_NAME (decl) == NULL_TREE \
3005 || (DECL_ARTIFICIAL (decl) \
3006 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3007 /* This is necessary for stub decls that \
3008 appear in nested inline functions. */ \
3009 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3010 && (decl_ultimate_origin (decl) \
3011 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3013 /* Information concerning the compilation unit's programming
3014 language, and compiler version. */
3016 /* Fixed size portion of the DWARF compilation unit header. */
3017 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3018 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3019 + (dwarf_version >= 5 ? 4 : 3))
3021 /* Fixed size portion of the DWARF comdat type unit header. */
3022 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3023 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3024 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3026 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3027 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3028 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3030 /* Fixed size portion of public names info. */
3031 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3033 /* Fixed size portion of the address range info. */
3034 #define DWARF_ARANGES_HEADER_SIZE \
3035 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3036 DWARF2_ADDR_SIZE * 2) \
3037 - DWARF_INITIAL_LENGTH_SIZE)
3039 /* Size of padding portion in the address range info. It must be
3040 aligned to twice the pointer size. */
3041 #define DWARF_ARANGES_PAD_SIZE \
3042 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3043 DWARF2_ADDR_SIZE * 2) \
3044 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3046 /* Use assembler line directives if available. */
3047 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3048 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3049 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3050 #else
3051 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3052 #endif
3053 #endif
3055 /* Minimum line offset in a special line info. opcode.
3056 This value was chosen to give a reasonable range of values. */
3057 #define DWARF_LINE_BASE -10
3059 /* First special line opcode - leave room for the standard opcodes. */
3060 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3062 /* Range of line offsets in a special line info. opcode. */
3063 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3065 /* Flag that indicates the initial value of the is_stmt_start flag.
3066 In the present implementation, we do not mark any lines as
3067 the beginning of a source statement, because that information
3068 is not made available by the GCC front-end. */
3069 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3071 /* Maximum number of operations per instruction bundle. */
3072 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3073 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3074 #endif
3076 /* This location is used by calc_die_sizes() to keep track
3077 the offset of each DIE within the .debug_info section. */
3078 static unsigned long next_die_offset;
3080 /* Record the root of the DIE's built for the current compilation unit. */
3081 static GTY(()) dw_die_ref single_comp_unit_die;
3083 /* A list of type DIEs that have been separated into comdat sections. */
3084 static GTY(()) comdat_type_node *comdat_type_list;
3086 /* A list of CU DIEs that have been separated. */
3087 static GTY(()) limbo_die_node *cu_die_list;
3089 /* A list of DIEs with a NULL parent waiting to be relocated. */
3090 static GTY(()) limbo_die_node *limbo_die_list;
3092 /* A list of DIEs for which we may have to generate
3093 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3094 static GTY(()) limbo_die_node *deferred_asm_name;
3096 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3098 typedef const char *compare_type;
3100 static hashval_t hash (dwarf_file_data *);
3101 static bool equal (dwarf_file_data *, const char *);
3104 /* Filenames referenced by this compilation unit. */
3105 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3107 struct decl_die_hasher : ggc_ptr_hash<die_node>
3109 typedef tree compare_type;
3111 static hashval_t hash (die_node *);
3112 static bool equal (die_node *, tree);
3114 /* A hash table of references to DIE's that describe declarations.
3115 The key is a DECL_UID() which is a unique number identifying each decl. */
3116 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3118 struct GTY ((for_user)) variable_value_struct {
3119 unsigned int decl_id;
3120 vec<dw_die_ref, va_gc> *dies;
3123 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3125 typedef tree compare_type;
3127 static hashval_t hash (variable_value_struct *);
3128 static bool equal (variable_value_struct *, tree);
3130 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3131 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3132 DECL_CONTEXT of the referenced VAR_DECLs. */
3133 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3135 struct block_die_hasher : ggc_ptr_hash<die_struct>
3137 static hashval_t hash (die_struct *);
3138 static bool equal (die_struct *, die_struct *);
3141 /* A hash table of references to DIE's that describe COMMON blocks.
3142 The key is DECL_UID() ^ die_parent. */
3143 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3145 typedef struct GTY(()) die_arg_entry_struct {
3146 dw_die_ref die;
3147 tree arg;
3148 } die_arg_entry;
3151 /* Node of the variable location list. */
3152 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3153 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3154 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3155 in mode of the EXPR_LIST node and first EXPR_LIST operand
3156 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3157 location or NULL for padding. For larger bitsizes,
3158 mode is 0 and first operand is a CONCAT with bitsize
3159 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3160 NULL as second operand. */
3161 rtx GTY (()) loc;
3162 const char * GTY (()) label;
3163 struct var_loc_node * GTY (()) next;
3166 /* Variable location list. */
3167 struct GTY ((for_user)) var_loc_list_def {
3168 struct var_loc_node * GTY (()) first;
3170 /* Pointer to the last but one or last element of the
3171 chained list. If the list is empty, both first and
3172 last are NULL, if the list contains just one node
3173 or the last node certainly is not redundant, it points
3174 to the last node, otherwise points to the last but one.
3175 Do not mark it for GC because it is marked through the chain. */
3176 struct var_loc_node * GTY ((skip ("%h"))) last;
3178 /* Pointer to the last element before section switch,
3179 if NULL, either sections weren't switched or first
3180 is after section switch. */
3181 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3183 /* DECL_UID of the variable decl. */
3184 unsigned int decl_id;
3186 typedef struct var_loc_list_def var_loc_list;
3188 /* Call argument location list. */
3189 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3190 rtx GTY (()) call_arg_loc_note;
3191 const char * GTY (()) label;
3192 tree GTY (()) block;
3193 bool tail_call_p;
3194 rtx GTY (()) symbol_ref;
3195 struct call_arg_loc_node * GTY (()) next;
3199 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3201 typedef const_tree compare_type;
3203 static hashval_t hash (var_loc_list *);
3204 static bool equal (var_loc_list *, const_tree);
3207 /* Table of decl location linked lists. */
3208 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3210 /* Head and tail of call_arg_loc chain. */
3211 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3212 static struct call_arg_loc_node *call_arg_loc_last;
3214 /* Number of call sites in the current function. */
3215 static int call_site_count = -1;
3216 /* Number of tail call sites in the current function. */
3217 static int tail_call_site_count = -1;
3219 /* A cached location list. */
3220 struct GTY ((for_user)) cached_dw_loc_list_def {
3221 /* The DECL_UID of the decl that this entry describes. */
3222 unsigned int decl_id;
3224 /* The cached location list. */
3225 dw_loc_list_ref loc_list;
3227 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3229 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3232 typedef const_tree compare_type;
3234 static hashval_t hash (cached_dw_loc_list *);
3235 static bool equal (cached_dw_loc_list *, const_tree);
3238 /* Table of cached location lists. */
3239 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3241 /* A vector of references to DIE's that are uniquely identified by their tag,
3242 presence/absence of children DIE's, and list of attribute/value pairs. */
3243 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3245 /* A hash map to remember the stack usage for DWARF procedures. The value
3246 stored is the stack size difference between before the DWARF procedure
3247 invokation and after it returned. In other words, for a DWARF procedure
3248 that consumes N stack slots and that pushes M ones, this stores M - N. */
3249 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3251 /* A global counter for generating labels for line number data. */
3252 static unsigned int line_info_label_num;
3254 /* The current table to which we should emit line number information
3255 for the current function. This will be set up at the beginning of
3256 assembly for the function. */
3257 static GTY(()) dw_line_info_table *cur_line_info_table;
3259 /* The two default tables of line number info. */
3260 static GTY(()) dw_line_info_table *text_section_line_info;
3261 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3263 /* The set of all non-default tables of line number info. */
3264 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3266 /* A flag to tell pubnames/types export if there is an info section to
3267 refer to. */
3268 static bool info_section_emitted;
3270 /* A pointer to the base of a table that contains a list of publicly
3271 accessible names. */
3272 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3274 /* A pointer to the base of a table that contains a list of publicly
3275 accessible types. */
3276 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3278 /* A pointer to the base of a table that contains a list of macro
3279 defines/undefines (and file start/end markers). */
3280 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3282 /* True if .debug_macinfo or .debug_macros section is going to be
3283 emitted. */
3284 #define have_macinfo \
3285 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3286 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3287 && !macinfo_table->is_empty ())
3289 /* Vector of dies for which we should generate .debug_ranges info. */
3290 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3292 /* Vector of pairs of labels referenced in ranges_table. */
3293 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3295 /* Whether we have location lists that need outputting */
3296 static GTY(()) bool have_location_lists;
3298 /* Unique label counter. */
3299 static GTY(()) unsigned int loclabel_num;
3301 /* Unique label counter for point-of-call tables. */
3302 static GTY(()) unsigned int poc_label_num;
3304 /* The last file entry emitted by maybe_emit_file(). */
3305 static GTY(()) struct dwarf_file_data * last_emitted_file;
3307 /* Number of internal labels generated by gen_internal_sym(). */
3308 static GTY(()) int label_num;
3310 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3312 /* Instances of generic types for which we need to generate debug
3313 info that describe their generic parameters and arguments. That
3314 generation needs to happen once all types are properly laid out so
3315 we do it at the end of compilation. */
3316 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3318 /* Offset from the "steady-state frame pointer" to the frame base,
3319 within the current function. */
3320 static HOST_WIDE_INT frame_pointer_fb_offset;
3321 static bool frame_pointer_fb_offset_valid;
3323 static vec<dw_die_ref> base_types;
3325 /* Flags to represent a set of attribute classes for attributes that represent
3326 a scalar value (bounds, pointers, ...). */
3327 enum dw_scalar_form
3329 dw_scalar_form_constant = 0x01,
3330 dw_scalar_form_exprloc = 0x02,
3331 dw_scalar_form_reference = 0x04
3334 /* Forward declarations for functions defined in this file. */
3336 static int is_pseudo_reg (const_rtx);
3337 static tree type_main_variant (tree);
3338 static int is_tagged_type (const_tree);
3339 static const char *dwarf_tag_name (unsigned);
3340 static const char *dwarf_attr_name (unsigned);
3341 static const char *dwarf_form_name (unsigned);
3342 static tree decl_ultimate_origin (const_tree);
3343 static tree decl_class_context (tree);
3344 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3345 static inline enum dw_val_class AT_class (dw_attr_node *);
3346 static inline unsigned int AT_index (dw_attr_node *);
3347 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3348 static inline unsigned AT_flag (dw_attr_node *);
3349 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3350 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3351 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3352 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3353 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3354 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3355 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3356 unsigned int, unsigned char *);
3357 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3358 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3359 static inline const char *AT_string (dw_attr_node *);
3360 static enum dwarf_form AT_string_form (dw_attr_node *);
3361 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3362 static void add_AT_specification (dw_die_ref, dw_die_ref);
3363 static inline dw_die_ref AT_ref (dw_attr_node *);
3364 static inline int AT_ref_external (dw_attr_node *);
3365 static inline void set_AT_ref_external (dw_attr_node *, int);
3366 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3367 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3368 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3369 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3370 dw_loc_list_ref);
3371 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3372 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3373 static void remove_addr_table_entry (addr_table_entry *);
3374 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3375 static inline rtx AT_addr (dw_attr_node *);
3376 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3377 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3378 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3379 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3380 const char *);
3381 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3382 unsigned HOST_WIDE_INT);
3383 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3384 unsigned long, bool);
3385 static inline const char *AT_lbl (dw_attr_node *);
3386 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3387 static const char *get_AT_low_pc (dw_die_ref);
3388 static const char *get_AT_hi_pc (dw_die_ref);
3389 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3390 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3391 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3392 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3393 static bool is_cxx (void);
3394 static bool is_cxx (const_tree);
3395 static bool is_fortran (void);
3396 static bool is_ada (void);
3397 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3398 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3399 static void add_child_die (dw_die_ref, dw_die_ref);
3400 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3401 static dw_die_ref lookup_type_die (tree);
3402 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3403 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3404 static void equate_type_number_to_die (tree, dw_die_ref);
3405 static dw_die_ref lookup_decl_die (tree);
3406 static var_loc_list *lookup_decl_loc (const_tree);
3407 static void equate_decl_number_to_die (tree, dw_die_ref);
3408 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3409 static void print_spaces (FILE *);
3410 static void print_die (dw_die_ref, FILE *);
3411 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3412 static dw_die_ref pop_compile_unit (dw_die_ref);
3413 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3414 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3415 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3416 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3417 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3418 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3419 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3420 struct md5_ctx *, int *);
3421 struct checksum_attributes;
3422 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3423 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3424 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3425 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3426 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3427 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3428 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3429 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3430 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3431 static void compute_section_prefix (dw_die_ref);
3432 static int is_type_die (dw_die_ref);
3433 static int is_comdat_die (dw_die_ref);
3434 static int is_symbol_die (dw_die_ref);
3435 static inline bool is_template_instantiation (dw_die_ref);
3436 static void assign_symbol_names (dw_die_ref);
3437 static void break_out_includes (dw_die_ref);
3438 static int is_declaration_die (dw_die_ref);
3439 static int should_move_die_to_comdat (dw_die_ref);
3440 static dw_die_ref clone_as_declaration (dw_die_ref);
3441 static dw_die_ref clone_die (dw_die_ref);
3442 static dw_die_ref clone_tree (dw_die_ref);
3443 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3444 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3445 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3446 static dw_die_ref generate_skeleton (dw_die_ref);
3447 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3448 dw_die_ref,
3449 dw_die_ref);
3450 static void break_out_comdat_types (dw_die_ref);
3451 static void copy_decls_for_unworthy_types (dw_die_ref);
3453 static void add_sibling_attributes (dw_die_ref);
3454 static void output_location_lists (dw_die_ref);
3455 static int constant_size (unsigned HOST_WIDE_INT);
3456 static unsigned long size_of_die (dw_die_ref);
3457 static void calc_die_sizes (dw_die_ref);
3458 static void calc_base_type_die_sizes (void);
3459 static void mark_dies (dw_die_ref);
3460 static void unmark_dies (dw_die_ref);
3461 static void unmark_all_dies (dw_die_ref);
3462 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3463 static unsigned long size_of_aranges (void);
3464 static enum dwarf_form value_format (dw_attr_node *);
3465 static void output_value_format (dw_attr_node *);
3466 static void output_abbrev_section (void);
3467 static void output_die_abbrevs (unsigned long, dw_die_ref);
3468 static void output_die_symbol (dw_die_ref);
3469 static void output_die (dw_die_ref);
3470 static void output_compilation_unit_header (enum dwarf_unit_type);
3471 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3472 static void output_comdat_type_unit (comdat_type_node *);
3473 static const char *dwarf2_name (tree, int);
3474 static void add_pubname (tree, dw_die_ref);
3475 static void add_enumerator_pubname (const char *, dw_die_ref);
3476 static void add_pubname_string (const char *, dw_die_ref);
3477 static void add_pubtype (tree, dw_die_ref);
3478 static void output_pubnames (vec<pubname_entry, va_gc> *);
3479 static void output_aranges (void);
3480 static unsigned int add_ranges (const_tree, bool = false);
3481 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3482 bool *, bool);
3483 static void output_ranges (void);
3484 static dw_line_info_table *new_line_info_table (void);
3485 static void output_line_info (bool);
3486 static void output_file_names (void);
3487 static dw_die_ref base_type_die (tree, bool);
3488 static int is_base_type (tree);
3489 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3490 static int decl_quals (const_tree);
3491 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3492 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3493 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3494 static int type_is_enum (const_tree);
3495 static unsigned int dbx_reg_number (const_rtx);
3496 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3497 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3498 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3499 enum var_init_status);
3500 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3501 enum var_init_status);
3502 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3503 enum var_init_status);
3504 static int is_based_loc (const_rtx);
3505 static bool resolve_one_addr (rtx *);
3506 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3507 enum var_init_status);
3508 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3509 enum var_init_status);
3510 struct loc_descr_context;
3511 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3512 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3513 static dw_loc_list_ref loc_list_from_tree (tree, int,
3514 struct loc_descr_context *);
3515 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3516 struct loc_descr_context *);
3517 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3518 static tree field_type (const_tree);
3519 static unsigned int simple_type_align_in_bits (const_tree);
3520 static unsigned int simple_decl_align_in_bits (const_tree);
3521 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3522 struct vlr_context;
3523 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3524 HOST_WIDE_INT *);
3525 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3526 dw_loc_list_ref);
3527 static void add_data_member_location_attribute (dw_die_ref, tree,
3528 struct vlr_context *);
3529 static bool add_const_value_attribute (dw_die_ref, rtx);
3530 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3531 static void insert_wide_int (const wide_int &, unsigned char *, int);
3532 static void insert_float (const_rtx, unsigned char *);
3533 static rtx rtl_for_decl_location (tree);
3534 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3535 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3536 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3537 static void add_name_attribute (dw_die_ref, const char *);
3538 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3539 static void add_comp_dir_attribute (dw_die_ref);
3540 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3541 struct loc_descr_context *);
3542 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3543 struct loc_descr_context *);
3544 static void add_subscript_info (dw_die_ref, tree, bool);
3545 static void add_byte_size_attribute (dw_die_ref, tree);
3546 static void add_alignment_attribute (dw_die_ref, tree);
3547 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3548 struct vlr_context *);
3549 static void add_bit_size_attribute (dw_die_ref, tree);
3550 static void add_prototyped_attribute (dw_die_ref, tree);
3551 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3552 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3553 static void add_src_coords_attributes (dw_die_ref, tree);
3554 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3555 static void add_discr_value (dw_die_ref, dw_discr_value *);
3556 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3557 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3558 static void push_decl_scope (tree);
3559 static void pop_decl_scope (void);
3560 static dw_die_ref scope_die_for (tree, dw_die_ref);
3561 static inline int local_scope_p (dw_die_ref);
3562 static inline int class_scope_p (dw_die_ref);
3563 static inline int class_or_namespace_scope_p (dw_die_ref);
3564 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3565 static void add_calling_convention_attribute (dw_die_ref, tree);
3566 static const char *type_tag (const_tree);
3567 static tree member_declared_type (const_tree);
3568 #if 0
3569 static const char *decl_start_label (tree);
3570 #endif
3571 static void gen_array_type_die (tree, dw_die_ref);
3572 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3573 #if 0
3574 static void gen_entry_point_die (tree, dw_die_ref);
3575 #endif
3576 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3577 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3578 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3579 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3580 static void gen_formal_types_die (tree, dw_die_ref);
3581 static void gen_subprogram_die (tree, dw_die_ref);
3582 static void gen_variable_die (tree, tree, dw_die_ref);
3583 static void gen_const_die (tree, dw_die_ref);
3584 static void gen_label_die (tree, dw_die_ref);
3585 static void gen_lexical_block_die (tree, dw_die_ref);
3586 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3587 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3588 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3589 static dw_die_ref gen_compile_unit_die (const char *);
3590 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3591 static void gen_member_die (tree, dw_die_ref);
3592 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3593 enum debug_info_usage);
3594 static void gen_subroutine_type_die (tree, dw_die_ref);
3595 static void gen_typedef_die (tree, dw_die_ref);
3596 static void gen_type_die (tree, dw_die_ref);
3597 static void gen_block_die (tree, dw_die_ref);
3598 static void decls_for_scope (tree, dw_die_ref);
3599 static bool is_naming_typedef_decl (const_tree);
3600 static inline dw_die_ref get_context_die (tree);
3601 static void gen_namespace_die (tree, dw_die_ref);
3602 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3603 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3604 static dw_die_ref force_decl_die (tree);
3605 static dw_die_ref force_type_die (tree);
3606 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3607 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3608 static struct dwarf_file_data * lookup_filename (const char *);
3609 static void retry_incomplete_types (void);
3610 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3611 static void gen_generic_params_dies (tree);
3612 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3613 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3614 static void splice_child_die (dw_die_ref, dw_die_ref);
3615 static int file_info_cmp (const void *, const void *);
3616 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3617 const char *, const char *);
3618 static void output_loc_list (dw_loc_list_ref);
3619 static char *gen_internal_sym (const char *);
3620 static bool want_pubnames (void);
3622 static void prune_unmark_dies (dw_die_ref);
3623 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3624 static void prune_unused_types_mark (dw_die_ref, int);
3625 static void prune_unused_types_walk (dw_die_ref);
3626 static void prune_unused_types_walk_attribs (dw_die_ref);
3627 static void prune_unused_types_prune (dw_die_ref);
3628 static void prune_unused_types (void);
3629 static int maybe_emit_file (struct dwarf_file_data *fd);
3630 static inline const char *AT_vms_delta1 (dw_attr_node *);
3631 static inline const char *AT_vms_delta2 (dw_attr_node *);
3632 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3633 const char *, const char *);
3634 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3635 static void gen_remaining_tmpl_value_param_die_attribute (void);
3636 static bool generic_type_p (tree);
3637 static void schedule_generic_params_dies_gen (tree t);
3638 static void gen_scheduled_generic_parms_dies (void);
3639 static void resolve_variable_values (void);
3641 static const char *comp_dir_string (void);
3643 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3645 /* enum for tracking thread-local variables whose address is really an offset
3646 relative to the TLS pointer, which will need link-time relocation, but will
3647 not need relocation by the DWARF consumer. */
3649 enum dtprel_bool
3651 dtprel_false = 0,
3652 dtprel_true = 1
3655 /* Return the operator to use for an address of a variable. For dtprel_true, we
3656 use DW_OP_const*. For regular variables, which need both link-time
3657 relocation and consumer-level relocation (e.g., to account for shared objects
3658 loaded at a random address), we use DW_OP_addr*. */
3660 static inline enum dwarf_location_atom
3661 dw_addr_op (enum dtprel_bool dtprel)
3663 if (dtprel == dtprel_true)
3664 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3665 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3666 else
3667 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3670 /* Return a pointer to a newly allocated address location description. If
3671 dwarf_split_debug_info is true, then record the address with the appropriate
3672 relocation. */
3673 static inline dw_loc_descr_ref
3674 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3676 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3678 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3679 ref->dw_loc_oprnd1.v.val_addr = addr;
3680 ref->dtprel = dtprel;
3681 if (dwarf_split_debug_info)
3682 ref->dw_loc_oprnd1.val_entry
3683 = add_addr_table_entry (addr,
3684 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3685 else
3686 ref->dw_loc_oprnd1.val_entry = NULL;
3688 return ref;
3691 /* Section names used to hold DWARF debugging information. */
3693 #ifndef DEBUG_INFO_SECTION
3694 #define DEBUG_INFO_SECTION ".debug_info"
3695 #endif
3696 #ifndef DEBUG_DWO_INFO_SECTION
3697 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3698 #endif
3699 #ifndef DEBUG_ABBREV_SECTION
3700 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3701 #endif
3702 #ifndef DEBUG_DWO_ABBREV_SECTION
3703 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3704 #endif
3705 #ifndef DEBUG_ARANGES_SECTION
3706 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3707 #endif
3708 #ifndef DEBUG_ADDR_SECTION
3709 #define DEBUG_ADDR_SECTION ".debug_addr"
3710 #endif
3711 #ifndef DEBUG_MACINFO_SECTION
3712 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3713 #endif
3714 #ifndef DEBUG_DWO_MACINFO_SECTION
3715 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3716 #endif
3717 #ifndef DEBUG_DWO_MACRO_SECTION
3718 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3719 #endif
3720 #ifndef DEBUG_MACRO_SECTION
3721 #define DEBUG_MACRO_SECTION ".debug_macro"
3722 #endif
3723 #ifndef DEBUG_LINE_SECTION
3724 #define DEBUG_LINE_SECTION ".debug_line"
3725 #endif
3726 #ifndef DEBUG_DWO_LINE_SECTION
3727 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3728 #endif
3729 #ifndef DEBUG_LOC_SECTION
3730 #define DEBUG_LOC_SECTION ".debug_loc"
3731 #endif
3732 #ifndef DEBUG_DWO_LOC_SECTION
3733 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3734 #endif
3735 #ifndef DEBUG_LOCLISTS_SECTION
3736 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3737 #endif
3738 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3739 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3740 #endif
3741 #ifndef DEBUG_PUBNAMES_SECTION
3742 #define DEBUG_PUBNAMES_SECTION \
3743 ((debug_generate_pub_sections == 2) \
3744 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3745 #endif
3746 #ifndef DEBUG_PUBTYPES_SECTION
3747 #define DEBUG_PUBTYPES_SECTION \
3748 ((debug_generate_pub_sections == 2) \
3749 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3750 #endif
3751 #ifndef DEBUG_STR_OFFSETS_SECTION
3752 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3753 #endif
3754 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3755 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3756 #endif
3757 #ifndef DEBUG_STR_DWO_SECTION
3758 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3759 #endif
3760 #ifndef DEBUG_STR_SECTION
3761 #define DEBUG_STR_SECTION ".debug_str"
3762 #endif
3763 #ifndef DEBUG_RANGES_SECTION
3764 #define DEBUG_RANGES_SECTION ".debug_ranges"
3765 #endif
3766 #ifndef DEBUG_RNGLISTS_SECTION
3767 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3768 #endif
3769 #ifndef DEBUG_LINE_STR_SECTION
3770 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3771 #endif
3773 /* Standard ELF section names for compiled code and data. */
3774 #ifndef TEXT_SECTION_NAME
3775 #define TEXT_SECTION_NAME ".text"
3776 #endif
3778 /* Section flags for .debug_str section. */
3779 #define DEBUG_STR_SECTION_FLAGS \
3780 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3781 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3782 : SECTION_DEBUG)
3784 /* Section flags for .debug_str.dwo section. */
3785 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3787 /* Labels we insert at beginning sections we can reference instead of
3788 the section names themselves. */
3790 #ifndef TEXT_SECTION_LABEL
3791 #define TEXT_SECTION_LABEL "Ltext"
3792 #endif
3793 #ifndef COLD_TEXT_SECTION_LABEL
3794 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3795 #endif
3796 #ifndef DEBUG_LINE_SECTION_LABEL
3797 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3798 #endif
3799 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3800 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3801 #endif
3802 #ifndef DEBUG_INFO_SECTION_LABEL
3803 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3804 #endif
3805 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3806 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3807 #endif
3808 #ifndef DEBUG_ABBREV_SECTION_LABEL
3809 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3810 #endif
3811 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3812 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3813 #endif
3814 #ifndef DEBUG_ADDR_SECTION_LABEL
3815 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3816 #endif
3817 #ifndef DEBUG_LOC_SECTION_LABEL
3818 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3819 #endif
3820 #ifndef DEBUG_RANGES_SECTION_LABEL
3821 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3822 #endif
3823 #ifndef DEBUG_MACINFO_SECTION_LABEL
3824 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3825 #endif
3826 #ifndef DEBUG_MACRO_SECTION_LABEL
3827 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3828 #endif
3829 #define SKELETON_COMP_DIE_ABBREV 1
3830 #define SKELETON_TYPE_DIE_ABBREV 2
3832 /* Definitions of defaults for formats and names of various special
3833 (artificial) labels which may be generated within this file (when the -g
3834 options is used and DWARF2_DEBUGGING_INFO is in effect.
3835 If necessary, these may be overridden from within the tm.h file, but
3836 typically, overriding these defaults is unnecessary. */
3838 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3839 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3840 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3841 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3842 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3843 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3844 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3845 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3846 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3847 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3848 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3849 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3850 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3851 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3852 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3854 #ifndef TEXT_END_LABEL
3855 #define TEXT_END_LABEL "Letext"
3856 #endif
3857 #ifndef COLD_END_LABEL
3858 #define COLD_END_LABEL "Letext_cold"
3859 #endif
3860 #ifndef BLOCK_BEGIN_LABEL
3861 #define BLOCK_BEGIN_LABEL "LBB"
3862 #endif
3863 #ifndef BLOCK_END_LABEL
3864 #define BLOCK_END_LABEL "LBE"
3865 #endif
3866 #ifndef LINE_CODE_LABEL
3867 #define LINE_CODE_LABEL "LM"
3868 #endif
3871 /* Return the root of the DIE's built for the current compilation unit. */
3872 static dw_die_ref
3873 comp_unit_die (void)
3875 if (!single_comp_unit_die)
3876 single_comp_unit_die = gen_compile_unit_die (NULL);
3877 return single_comp_unit_die;
3880 /* We allow a language front-end to designate a function that is to be
3881 called to "demangle" any name before it is put into a DIE. */
3883 static const char *(*demangle_name_func) (const char *);
3885 void
3886 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3888 demangle_name_func = func;
3891 /* Test if rtl node points to a pseudo register. */
3893 static inline int
3894 is_pseudo_reg (const_rtx rtl)
3896 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3897 || (GET_CODE (rtl) == SUBREG
3898 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3901 /* Return a reference to a type, with its const and volatile qualifiers
3902 removed. */
3904 static inline tree
3905 type_main_variant (tree type)
3907 type = TYPE_MAIN_VARIANT (type);
3909 /* ??? There really should be only one main variant among any group of
3910 variants of a given type (and all of the MAIN_VARIANT values for all
3911 members of the group should point to that one type) but sometimes the C
3912 front-end messes this up for array types, so we work around that bug
3913 here. */
3914 if (TREE_CODE (type) == ARRAY_TYPE)
3915 while (type != TYPE_MAIN_VARIANT (type))
3916 type = TYPE_MAIN_VARIANT (type);
3918 return type;
3921 /* Return nonzero if the given type node represents a tagged type. */
3923 static inline int
3924 is_tagged_type (const_tree type)
3926 enum tree_code code = TREE_CODE (type);
3928 return (code == RECORD_TYPE || code == UNION_TYPE
3929 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3932 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3934 static void
3935 get_ref_die_offset_label (char *label, dw_die_ref ref)
3937 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3940 /* Return die_offset of a DIE reference to a base type. */
3942 static unsigned long int
3943 get_base_type_offset (dw_die_ref ref)
3945 if (ref->die_offset)
3946 return ref->die_offset;
3947 if (comp_unit_die ()->die_abbrev)
3949 calc_base_type_die_sizes ();
3950 gcc_assert (ref->die_offset);
3952 return ref->die_offset;
3955 /* Return die_offset of a DIE reference other than base type. */
3957 static unsigned long int
3958 get_ref_die_offset (dw_die_ref ref)
3960 gcc_assert (ref->die_offset);
3961 return ref->die_offset;
3964 /* Convert a DIE tag into its string name. */
3966 static const char *
3967 dwarf_tag_name (unsigned int tag)
3969 const char *name = get_DW_TAG_name (tag);
3971 if (name != NULL)
3972 return name;
3974 return "DW_TAG_<unknown>";
3977 /* Convert a DWARF attribute code into its string name. */
3979 static const char *
3980 dwarf_attr_name (unsigned int attr)
3982 const char *name;
3984 switch (attr)
3986 #if VMS_DEBUGGING_INFO
3987 case DW_AT_HP_prologue:
3988 return "DW_AT_HP_prologue";
3989 #else
3990 case DW_AT_MIPS_loop_unroll_factor:
3991 return "DW_AT_MIPS_loop_unroll_factor";
3992 #endif
3994 #if VMS_DEBUGGING_INFO
3995 case DW_AT_HP_epilogue:
3996 return "DW_AT_HP_epilogue";
3997 #else
3998 case DW_AT_MIPS_stride:
3999 return "DW_AT_MIPS_stride";
4000 #endif
4003 name = get_DW_AT_name (attr);
4005 if (name != NULL)
4006 return name;
4008 return "DW_AT_<unknown>";
4011 /* Convert a DWARF value form code into its string name. */
4013 static const char *
4014 dwarf_form_name (unsigned int form)
4016 const char *name = get_DW_FORM_name (form);
4018 if (name != NULL)
4019 return name;
4021 return "DW_FORM_<unknown>";
4024 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4025 instance of an inlined instance of a decl which is local to an inline
4026 function, so we have to trace all of the way back through the origin chain
4027 to find out what sort of node actually served as the original seed for the
4028 given block. */
4030 static tree
4031 decl_ultimate_origin (const_tree decl)
4033 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4034 return NULL_TREE;
4036 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4037 we're trying to output the abstract instance of this function. */
4038 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4039 return NULL_TREE;
4041 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4042 most distant ancestor, this should never happen. */
4043 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4045 return DECL_ABSTRACT_ORIGIN (decl);
4048 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4049 of a virtual function may refer to a base class, so we check the 'this'
4050 parameter. */
4052 static tree
4053 decl_class_context (tree decl)
4055 tree context = NULL_TREE;
4057 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4058 context = DECL_CONTEXT (decl);
4059 else
4060 context = TYPE_MAIN_VARIANT
4061 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4063 if (context && !TYPE_P (context))
4064 context = NULL_TREE;
4066 return context;
4069 /* Add an attribute/value pair to a DIE. */
4071 static inline void
4072 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4074 /* Maybe this should be an assert? */
4075 if (die == NULL)
4076 return;
4078 vec_safe_reserve (die->die_attr, 1);
4079 vec_safe_push (die->die_attr, *attr);
4082 static inline enum dw_val_class
4083 AT_class (dw_attr_node *a)
4085 return a->dw_attr_val.val_class;
4088 /* Return the index for any attribute that will be referenced with a
4089 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4090 are stored in dw_attr_val.v.val_str for reference counting
4091 pruning. */
4093 static inline unsigned int
4094 AT_index (dw_attr_node *a)
4096 if (AT_class (a) == dw_val_class_str)
4097 return a->dw_attr_val.v.val_str->index;
4098 else if (a->dw_attr_val.val_entry != NULL)
4099 return a->dw_attr_val.val_entry->index;
4100 return NOT_INDEXED;
4103 /* Add a flag value attribute to a DIE. */
4105 static inline void
4106 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4108 dw_attr_node attr;
4110 attr.dw_attr = attr_kind;
4111 attr.dw_attr_val.val_class = dw_val_class_flag;
4112 attr.dw_attr_val.val_entry = NULL;
4113 attr.dw_attr_val.v.val_flag = flag;
4114 add_dwarf_attr (die, &attr);
4117 static inline unsigned
4118 AT_flag (dw_attr_node *a)
4120 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4121 return a->dw_attr_val.v.val_flag;
4124 /* Add a signed integer attribute value to a DIE. */
4126 static inline void
4127 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4129 dw_attr_node attr;
4131 attr.dw_attr = attr_kind;
4132 attr.dw_attr_val.val_class = dw_val_class_const;
4133 attr.dw_attr_val.val_entry = NULL;
4134 attr.dw_attr_val.v.val_int = int_val;
4135 add_dwarf_attr (die, &attr);
4138 static inline HOST_WIDE_INT
4139 AT_int (dw_attr_node *a)
4141 gcc_assert (a && (AT_class (a) == dw_val_class_const
4142 || AT_class (a) == dw_val_class_const_implicit));
4143 return a->dw_attr_val.v.val_int;
4146 /* Add an unsigned integer attribute value to a DIE. */
4148 static inline void
4149 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4150 unsigned HOST_WIDE_INT unsigned_val)
4152 dw_attr_node attr;
4154 attr.dw_attr = attr_kind;
4155 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4156 attr.dw_attr_val.val_entry = NULL;
4157 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4158 add_dwarf_attr (die, &attr);
4161 static inline unsigned HOST_WIDE_INT
4162 AT_unsigned (dw_attr_node *a)
4164 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4165 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4166 return a->dw_attr_val.v.val_unsigned;
4169 /* Add an unsigned wide integer attribute value to a DIE. */
4171 static inline void
4172 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4173 const wide_int& w)
4175 dw_attr_node attr;
4177 attr.dw_attr = attr_kind;
4178 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4179 attr.dw_attr_val.val_entry = NULL;
4180 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4181 *attr.dw_attr_val.v.val_wide = w;
4182 add_dwarf_attr (die, &attr);
4185 /* Add an unsigned double integer attribute value to a DIE. */
4187 static inline void
4188 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4189 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4191 dw_attr_node attr;
4193 attr.dw_attr = attr_kind;
4194 attr.dw_attr_val.val_class = dw_val_class_const_double;
4195 attr.dw_attr_val.val_entry = NULL;
4196 attr.dw_attr_val.v.val_double.high = high;
4197 attr.dw_attr_val.v.val_double.low = low;
4198 add_dwarf_attr (die, &attr);
4201 /* Add a floating point attribute value to a DIE and return it. */
4203 static inline void
4204 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4205 unsigned int length, unsigned int elt_size, unsigned char *array)
4207 dw_attr_node attr;
4209 attr.dw_attr = attr_kind;
4210 attr.dw_attr_val.val_class = dw_val_class_vec;
4211 attr.dw_attr_val.val_entry = NULL;
4212 attr.dw_attr_val.v.val_vec.length = length;
4213 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4214 attr.dw_attr_val.v.val_vec.array = array;
4215 add_dwarf_attr (die, &attr);
4218 /* Add an 8-byte data attribute value to a DIE. */
4220 static inline void
4221 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4222 unsigned char data8[8])
4224 dw_attr_node attr;
4226 attr.dw_attr = attr_kind;
4227 attr.dw_attr_val.val_class = dw_val_class_data8;
4228 attr.dw_attr_val.val_entry = NULL;
4229 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4230 add_dwarf_attr (die, &attr);
4233 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4234 dwarf_split_debug_info, address attributes in dies destined for the
4235 final executable have force_direct set to avoid using indexed
4236 references. */
4238 static inline void
4239 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4240 bool force_direct)
4242 dw_attr_node attr;
4243 char * lbl_id;
4245 lbl_id = xstrdup (lbl_low);
4246 attr.dw_attr = DW_AT_low_pc;
4247 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4248 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4249 if (dwarf_split_debug_info && !force_direct)
4250 attr.dw_attr_val.val_entry
4251 = add_addr_table_entry (lbl_id, ate_kind_label);
4252 else
4253 attr.dw_attr_val.val_entry = NULL;
4254 add_dwarf_attr (die, &attr);
4256 attr.dw_attr = DW_AT_high_pc;
4257 if (dwarf_version < 4)
4258 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4259 else
4260 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4261 lbl_id = xstrdup (lbl_high);
4262 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4263 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4264 && dwarf_split_debug_info && !force_direct)
4265 attr.dw_attr_val.val_entry
4266 = add_addr_table_entry (lbl_id, ate_kind_label);
4267 else
4268 attr.dw_attr_val.val_entry = NULL;
4269 add_dwarf_attr (die, &attr);
4272 /* Hash and equality functions for debug_str_hash. */
4274 hashval_t
4275 indirect_string_hasher::hash (indirect_string_node *x)
4277 return htab_hash_string (x->str);
4280 bool
4281 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4283 return strcmp (x1->str, x2) == 0;
4286 /* Add STR to the given string hash table. */
4288 static struct indirect_string_node *
4289 find_AT_string_in_table (const char *str,
4290 hash_table<indirect_string_hasher> *table)
4292 struct indirect_string_node *node;
4294 indirect_string_node **slot
4295 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4296 if (*slot == NULL)
4298 node = ggc_cleared_alloc<indirect_string_node> ();
4299 node->str = ggc_strdup (str);
4300 *slot = node;
4302 else
4303 node = *slot;
4305 node->refcount++;
4306 return node;
4309 /* Add STR to the indirect string hash table. */
4311 static struct indirect_string_node *
4312 find_AT_string (const char *str)
4314 if (! debug_str_hash)
4315 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4317 return find_AT_string_in_table (str, debug_str_hash);
4320 /* Add a string attribute value to a DIE. */
4322 static inline void
4323 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4325 dw_attr_node attr;
4326 struct indirect_string_node *node;
4328 node = find_AT_string (str);
4330 attr.dw_attr = attr_kind;
4331 attr.dw_attr_val.val_class = dw_val_class_str;
4332 attr.dw_attr_val.val_entry = NULL;
4333 attr.dw_attr_val.v.val_str = node;
4334 add_dwarf_attr (die, &attr);
4337 static inline const char *
4338 AT_string (dw_attr_node *a)
4340 gcc_assert (a && AT_class (a) == dw_val_class_str);
4341 return a->dw_attr_val.v.val_str->str;
4344 /* Call this function directly to bypass AT_string_form's logic to put
4345 the string inline in the die. */
4347 static void
4348 set_indirect_string (struct indirect_string_node *node)
4350 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4351 /* Already indirect is a no op. */
4352 if (node->form == DW_FORM_strp
4353 || node->form == DW_FORM_line_strp
4354 || node->form == DW_FORM_GNU_str_index)
4356 gcc_assert (node->label);
4357 return;
4359 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4360 ++dw2_string_counter;
4361 node->label = xstrdup (label);
4363 if (!dwarf_split_debug_info)
4365 node->form = DW_FORM_strp;
4366 node->index = NOT_INDEXED;
4368 else
4370 node->form = DW_FORM_GNU_str_index;
4371 node->index = NO_INDEX_ASSIGNED;
4375 /* Find out whether a string should be output inline in DIE
4376 or out-of-line in .debug_str section. */
4378 static enum dwarf_form
4379 find_string_form (struct indirect_string_node *node)
4381 unsigned int len;
4383 if (node->form)
4384 return node->form;
4386 len = strlen (node->str) + 1;
4388 /* If the string is shorter or equal to the size of the reference, it is
4389 always better to put it inline. */
4390 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4391 return node->form = DW_FORM_string;
4393 /* If we cannot expect the linker to merge strings in .debug_str
4394 section, only put it into .debug_str if it is worth even in this
4395 single module. */
4396 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4397 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4398 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4399 return node->form = DW_FORM_string;
4401 set_indirect_string (node);
4403 return node->form;
4406 /* Find out whether the string referenced from the attribute should be
4407 output inline in DIE or out-of-line in .debug_str section. */
4409 static enum dwarf_form
4410 AT_string_form (dw_attr_node *a)
4412 gcc_assert (a && AT_class (a) == dw_val_class_str);
4413 return find_string_form (a->dw_attr_val.v.val_str);
4416 /* Add a DIE reference attribute value to a DIE. */
4418 static inline void
4419 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4421 dw_attr_node attr;
4422 gcc_checking_assert (targ_die != NULL);
4424 /* With LTO we can end up trying to reference something we didn't create
4425 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4426 if (targ_die == NULL)
4427 return;
4429 attr.dw_attr = attr_kind;
4430 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4431 attr.dw_attr_val.val_entry = NULL;
4432 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4433 attr.dw_attr_val.v.val_die_ref.external = 0;
4434 add_dwarf_attr (die, &attr);
4437 /* Change DIE reference REF to point to NEW_DIE instead. */
4439 static inline void
4440 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4442 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4443 ref->dw_attr_val.v.val_die_ref.die = new_die;
4444 ref->dw_attr_val.v.val_die_ref.external = 0;
4447 /* Add an AT_specification attribute to a DIE, and also make the back
4448 pointer from the specification to the definition. */
4450 static inline void
4451 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4453 add_AT_die_ref (die, DW_AT_specification, targ_die);
4454 gcc_assert (!targ_die->die_definition);
4455 targ_die->die_definition = die;
4458 static inline dw_die_ref
4459 AT_ref (dw_attr_node *a)
4461 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4462 return a->dw_attr_val.v.val_die_ref.die;
4465 static inline int
4466 AT_ref_external (dw_attr_node *a)
4468 if (a && AT_class (a) == dw_val_class_die_ref)
4469 return a->dw_attr_val.v.val_die_ref.external;
4471 return 0;
4474 static inline void
4475 set_AT_ref_external (dw_attr_node *a, int i)
4477 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4478 a->dw_attr_val.v.val_die_ref.external = i;
4481 /* Add an FDE reference attribute value to a DIE. */
4483 static inline void
4484 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4486 dw_attr_node attr;
4488 attr.dw_attr = attr_kind;
4489 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4490 attr.dw_attr_val.val_entry = NULL;
4491 attr.dw_attr_val.v.val_fde_index = targ_fde;
4492 add_dwarf_attr (die, &attr);
4495 /* Add a location description attribute value to a DIE. */
4497 static inline void
4498 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4500 dw_attr_node attr;
4502 attr.dw_attr = attr_kind;
4503 attr.dw_attr_val.val_class = dw_val_class_loc;
4504 attr.dw_attr_val.val_entry = NULL;
4505 attr.dw_attr_val.v.val_loc = loc;
4506 add_dwarf_attr (die, &attr);
4509 static inline dw_loc_descr_ref
4510 AT_loc (dw_attr_node *a)
4512 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4513 return a->dw_attr_val.v.val_loc;
4516 static inline void
4517 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4519 dw_attr_node attr;
4521 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4522 return;
4524 attr.dw_attr = attr_kind;
4525 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4526 attr.dw_attr_val.val_entry = NULL;
4527 attr.dw_attr_val.v.val_loc_list = loc_list;
4528 add_dwarf_attr (die, &attr);
4529 have_location_lists = true;
4532 static inline dw_loc_list_ref
4533 AT_loc_list (dw_attr_node *a)
4535 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4536 return a->dw_attr_val.v.val_loc_list;
4539 static inline dw_loc_list_ref *
4540 AT_loc_list_ptr (dw_attr_node *a)
4542 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4543 return &a->dw_attr_val.v.val_loc_list;
4546 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4548 static hashval_t hash (addr_table_entry *);
4549 static bool equal (addr_table_entry *, addr_table_entry *);
4552 /* Table of entries into the .debug_addr section. */
4554 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4556 /* Hash an address_table_entry. */
4558 hashval_t
4559 addr_hasher::hash (addr_table_entry *a)
4561 inchash::hash hstate;
4562 switch (a->kind)
4564 case ate_kind_rtx:
4565 hstate.add_int (0);
4566 break;
4567 case ate_kind_rtx_dtprel:
4568 hstate.add_int (1);
4569 break;
4570 case ate_kind_label:
4571 return htab_hash_string (a->addr.label);
4572 default:
4573 gcc_unreachable ();
4575 inchash::add_rtx (a->addr.rtl, hstate);
4576 return hstate.end ();
4579 /* Determine equality for two address_table_entries. */
4581 bool
4582 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4584 if (a1->kind != a2->kind)
4585 return 0;
4586 switch (a1->kind)
4588 case ate_kind_rtx:
4589 case ate_kind_rtx_dtprel:
4590 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4591 case ate_kind_label:
4592 return strcmp (a1->addr.label, a2->addr.label) == 0;
4593 default:
4594 gcc_unreachable ();
4598 /* Initialize an addr_table_entry. */
4600 void
4601 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4603 e->kind = kind;
4604 switch (kind)
4606 case ate_kind_rtx:
4607 case ate_kind_rtx_dtprel:
4608 e->addr.rtl = (rtx) addr;
4609 break;
4610 case ate_kind_label:
4611 e->addr.label = (char *) addr;
4612 break;
4614 e->refcount = 0;
4615 e->index = NO_INDEX_ASSIGNED;
4618 /* Add attr to the address table entry to the table. Defer setting an
4619 index until output time. */
4621 static addr_table_entry *
4622 add_addr_table_entry (void *addr, enum ate_kind kind)
4624 addr_table_entry *node;
4625 addr_table_entry finder;
4627 gcc_assert (dwarf_split_debug_info);
4628 if (! addr_index_table)
4629 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4630 init_addr_table_entry (&finder, kind, addr);
4631 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4633 if (*slot == HTAB_EMPTY_ENTRY)
4635 node = ggc_cleared_alloc<addr_table_entry> ();
4636 init_addr_table_entry (node, kind, addr);
4637 *slot = node;
4639 else
4640 node = *slot;
4642 node->refcount++;
4643 return node;
4646 /* Remove an entry from the addr table by decrementing its refcount.
4647 Strictly, decrementing the refcount would be enough, but the
4648 assertion that the entry is actually in the table has found
4649 bugs. */
4651 static void
4652 remove_addr_table_entry (addr_table_entry *entry)
4654 gcc_assert (dwarf_split_debug_info && addr_index_table);
4655 /* After an index is assigned, the table is frozen. */
4656 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4657 entry->refcount--;
4660 /* Given a location list, remove all addresses it refers to from the
4661 address_table. */
4663 static void
4664 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4666 for (; descr; descr = descr->dw_loc_next)
4667 if (descr->dw_loc_oprnd1.val_entry != NULL)
4669 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4670 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4674 /* A helper function for dwarf2out_finish called through
4675 htab_traverse. Assign an addr_table_entry its index. All entries
4676 must be collected into the table when this function is called,
4677 because the indexing code relies on htab_traverse to traverse nodes
4678 in the same order for each run. */
4681 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4683 addr_table_entry *node = *h;
4685 /* Don't index unreferenced nodes. */
4686 if (node->refcount == 0)
4687 return 1;
4689 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4690 node->index = *index;
4691 *index += 1;
4693 return 1;
4696 /* Add an address constant attribute value to a DIE. When using
4697 dwarf_split_debug_info, address attributes in dies destined for the
4698 final executable should be direct references--setting the parameter
4699 force_direct ensures this behavior. */
4701 static inline void
4702 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4703 bool force_direct)
4705 dw_attr_node attr;
4707 attr.dw_attr = attr_kind;
4708 attr.dw_attr_val.val_class = dw_val_class_addr;
4709 attr.dw_attr_val.v.val_addr = addr;
4710 if (dwarf_split_debug_info && !force_direct)
4711 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4712 else
4713 attr.dw_attr_val.val_entry = NULL;
4714 add_dwarf_attr (die, &attr);
4717 /* Get the RTX from to an address DIE attribute. */
4719 static inline rtx
4720 AT_addr (dw_attr_node *a)
4722 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4723 return a->dw_attr_val.v.val_addr;
4726 /* Add a file attribute value to a DIE. */
4728 static inline void
4729 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4730 struct dwarf_file_data *fd)
4732 dw_attr_node attr;
4734 attr.dw_attr = attr_kind;
4735 attr.dw_attr_val.val_class = dw_val_class_file;
4736 attr.dw_attr_val.val_entry = NULL;
4737 attr.dw_attr_val.v.val_file = fd;
4738 add_dwarf_attr (die, &attr);
4741 /* Get the dwarf_file_data from a file DIE attribute. */
4743 static inline struct dwarf_file_data *
4744 AT_file (dw_attr_node *a)
4746 gcc_assert (a && (AT_class (a) == dw_val_class_file
4747 || AT_class (a) == dw_val_class_file_implicit));
4748 return a->dw_attr_val.v.val_file;
4751 /* Add a vms delta attribute value to a DIE. */
4753 static inline void
4754 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4755 const char *lbl1, const char *lbl2)
4757 dw_attr_node attr;
4759 attr.dw_attr = attr_kind;
4760 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4761 attr.dw_attr_val.val_entry = NULL;
4762 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4763 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4764 add_dwarf_attr (die, &attr);
4767 /* Add a label identifier attribute value to a DIE. */
4769 static inline void
4770 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4771 const char *lbl_id)
4773 dw_attr_node attr;
4775 attr.dw_attr = attr_kind;
4776 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4777 attr.dw_attr_val.val_entry = NULL;
4778 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4779 if (dwarf_split_debug_info)
4780 attr.dw_attr_val.val_entry
4781 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4782 ate_kind_label);
4783 add_dwarf_attr (die, &attr);
4786 /* Add a section offset attribute value to a DIE, an offset into the
4787 debug_line section. */
4789 static inline void
4790 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4791 const char *label)
4793 dw_attr_node attr;
4795 attr.dw_attr = attr_kind;
4796 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4797 attr.dw_attr_val.val_entry = NULL;
4798 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4799 add_dwarf_attr (die, &attr);
4802 /* Add a section offset attribute value to a DIE, an offset into the
4803 debug_loclists section. */
4805 static inline void
4806 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4807 const char *label)
4809 dw_attr_node attr;
4811 attr.dw_attr = attr_kind;
4812 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4813 attr.dw_attr_val.val_entry = NULL;
4814 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4815 add_dwarf_attr (die, &attr);
4818 /* Add a section offset attribute value to a DIE, an offset into the
4819 debug_macinfo section. */
4821 static inline void
4822 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4823 const char *label)
4825 dw_attr_node attr;
4827 attr.dw_attr = attr_kind;
4828 attr.dw_attr_val.val_class = dw_val_class_macptr;
4829 attr.dw_attr_val.val_entry = NULL;
4830 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4831 add_dwarf_attr (die, &attr);
4834 /* Add an offset attribute value to a DIE. */
4836 static inline void
4837 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4838 unsigned HOST_WIDE_INT offset)
4840 dw_attr_node attr;
4842 attr.dw_attr = attr_kind;
4843 attr.dw_attr_val.val_class = dw_val_class_offset;
4844 attr.dw_attr_val.val_entry = NULL;
4845 attr.dw_attr_val.v.val_offset = offset;
4846 add_dwarf_attr (die, &attr);
4849 /* Add a range_list attribute value to a DIE. When using
4850 dwarf_split_debug_info, address attributes in dies destined for the
4851 final executable should be direct references--setting the parameter
4852 force_direct ensures this behavior. */
4854 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4855 #define RELOCATED_OFFSET (NULL)
4857 static void
4858 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4859 long unsigned int offset, bool force_direct)
4861 dw_attr_node attr;
4863 attr.dw_attr = attr_kind;
4864 attr.dw_attr_val.val_class = dw_val_class_range_list;
4865 /* For the range_list attribute, use val_entry to store whether the
4866 offset should follow split-debug-info or normal semantics. This
4867 value is read in output_range_list_offset. */
4868 if (dwarf_split_debug_info && !force_direct)
4869 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4870 else
4871 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4872 attr.dw_attr_val.v.val_offset = offset;
4873 add_dwarf_attr (die, &attr);
4876 /* Return the start label of a delta attribute. */
4878 static inline const char *
4879 AT_vms_delta1 (dw_attr_node *a)
4881 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4882 return a->dw_attr_val.v.val_vms_delta.lbl1;
4885 /* Return the end label of a delta attribute. */
4887 static inline const char *
4888 AT_vms_delta2 (dw_attr_node *a)
4890 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4891 return a->dw_attr_val.v.val_vms_delta.lbl2;
4894 static inline const char *
4895 AT_lbl (dw_attr_node *a)
4897 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4898 || AT_class (a) == dw_val_class_lineptr
4899 || AT_class (a) == dw_val_class_macptr
4900 || AT_class (a) == dw_val_class_loclistsptr
4901 || AT_class (a) == dw_val_class_high_pc));
4902 return a->dw_attr_val.v.val_lbl_id;
4905 /* Get the attribute of type attr_kind. */
4907 static dw_attr_node *
4908 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4910 dw_attr_node *a;
4911 unsigned ix;
4912 dw_die_ref spec = NULL;
4914 if (! die)
4915 return NULL;
4917 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4918 if (a->dw_attr == attr_kind)
4919 return a;
4920 else if (a->dw_attr == DW_AT_specification
4921 || a->dw_attr == DW_AT_abstract_origin)
4922 spec = AT_ref (a);
4924 if (spec)
4925 return get_AT (spec, attr_kind);
4927 return NULL;
4930 /* Returns the parent of the declaration of DIE. */
4932 static dw_die_ref
4933 get_die_parent (dw_die_ref die)
4935 dw_die_ref t;
4937 if (!die)
4938 return NULL;
4940 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4941 || (t = get_AT_ref (die, DW_AT_specification)))
4942 die = t;
4944 return die->die_parent;
4947 /* Return the "low pc" attribute value, typically associated with a subprogram
4948 DIE. Return null if the "low pc" attribute is either not present, or if it
4949 cannot be represented as an assembler label identifier. */
4951 static inline const char *
4952 get_AT_low_pc (dw_die_ref die)
4954 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4956 return a ? AT_lbl (a) : NULL;
4959 /* Return the "high pc" attribute value, typically associated with a subprogram
4960 DIE. Return null if the "high pc" attribute is either not present, or if it
4961 cannot be represented as an assembler label identifier. */
4963 static inline const char *
4964 get_AT_hi_pc (dw_die_ref die)
4966 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4968 return a ? AT_lbl (a) : NULL;
4971 /* Return the value of the string attribute designated by ATTR_KIND, or
4972 NULL if it is not present. */
4974 static inline const char *
4975 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4977 dw_attr_node *a = get_AT (die, attr_kind);
4979 return a ? AT_string (a) : NULL;
4982 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4983 if it is not present. */
4985 static inline int
4986 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4988 dw_attr_node *a = get_AT (die, attr_kind);
4990 return a ? AT_flag (a) : 0;
4993 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4994 if it is not present. */
4996 static inline unsigned
4997 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4999 dw_attr_node *a = get_AT (die, attr_kind);
5001 return a ? AT_unsigned (a) : 0;
5004 static inline dw_die_ref
5005 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5007 dw_attr_node *a = get_AT (die, attr_kind);
5009 return a ? AT_ref (a) : NULL;
5012 static inline struct dwarf_file_data *
5013 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5015 dw_attr_node *a = get_AT (die, attr_kind);
5017 return a ? AT_file (a) : NULL;
5020 /* Return TRUE if the language is C++. */
5022 static inline bool
5023 is_cxx (void)
5025 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5027 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5028 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5031 /* Return TRUE if DECL was created by the C++ frontend. */
5033 static bool
5034 is_cxx (const_tree decl)
5036 if (in_lto_p)
5038 const_tree context = decl;
5039 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5041 if (TREE_CODE (context) == BLOCK)
5042 context = BLOCK_SUPERCONTEXT (context);
5043 else
5044 context = get_containing_scope (context);
5046 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5047 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5049 return is_cxx ();
5052 /* Return TRUE if the language is Fortran. */
5054 static inline bool
5055 is_fortran (void)
5057 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5059 return (lang == DW_LANG_Fortran77
5060 || lang == DW_LANG_Fortran90
5061 || lang == DW_LANG_Fortran95
5062 || lang == DW_LANG_Fortran03
5063 || lang == DW_LANG_Fortran08);
5066 /* Return TRUE if the language is Ada. */
5068 static inline bool
5069 is_ada (void)
5071 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5073 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5076 /* Remove the specified attribute if present. Return TRUE if removal
5077 was successful. */
5079 static bool
5080 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5082 dw_attr_node *a;
5083 unsigned ix;
5085 if (! die)
5086 return false;
5088 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5089 if (a->dw_attr == attr_kind)
5091 if (AT_class (a) == dw_val_class_str)
5092 if (a->dw_attr_val.v.val_str->refcount)
5093 a->dw_attr_val.v.val_str->refcount--;
5095 /* vec::ordered_remove should help reduce the number of abbrevs
5096 that are needed. */
5097 die->die_attr->ordered_remove (ix);
5098 return true;
5100 return false;
5103 /* Remove CHILD from its parent. PREV must have the property that
5104 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5106 static void
5107 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5109 gcc_assert (child->die_parent == prev->die_parent);
5110 gcc_assert (prev->die_sib == child);
5111 if (prev == child)
5113 gcc_assert (child->die_parent->die_child == child);
5114 prev = NULL;
5116 else
5117 prev->die_sib = child->die_sib;
5118 if (child->die_parent->die_child == child)
5119 child->die_parent->die_child = prev;
5120 child->die_sib = NULL;
5123 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5124 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5126 static void
5127 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5129 dw_die_ref parent = old_child->die_parent;
5131 gcc_assert (parent == prev->die_parent);
5132 gcc_assert (prev->die_sib == old_child);
5134 new_child->die_parent = parent;
5135 if (prev == old_child)
5137 gcc_assert (parent->die_child == old_child);
5138 new_child->die_sib = new_child;
5140 else
5142 prev->die_sib = new_child;
5143 new_child->die_sib = old_child->die_sib;
5145 if (old_child->die_parent->die_child == old_child)
5146 old_child->die_parent->die_child = new_child;
5147 old_child->die_sib = NULL;
5150 /* Move all children from OLD_PARENT to NEW_PARENT. */
5152 static void
5153 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5155 dw_die_ref c;
5156 new_parent->die_child = old_parent->die_child;
5157 old_parent->die_child = NULL;
5158 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5161 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5162 matches TAG. */
5164 static void
5165 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5167 dw_die_ref c;
5169 c = die->die_child;
5170 if (c) do {
5171 dw_die_ref prev = c;
5172 c = c->die_sib;
5173 while (c->die_tag == tag)
5175 remove_child_with_prev (c, prev);
5176 c->die_parent = NULL;
5177 /* Might have removed every child. */
5178 if (die->die_child == NULL)
5179 return;
5180 c = prev->die_sib;
5182 } while (c != die->die_child);
5185 /* Add a CHILD_DIE as the last child of DIE. */
5187 static void
5188 add_child_die (dw_die_ref die, dw_die_ref child_die)
5190 /* FIXME this should probably be an assert. */
5191 if (! die || ! child_die)
5192 return;
5193 gcc_assert (die != child_die);
5195 child_die->die_parent = die;
5196 if (die->die_child)
5198 child_die->die_sib = die->die_child->die_sib;
5199 die->die_child->die_sib = child_die;
5201 else
5202 child_die->die_sib = child_die;
5203 die->die_child = child_die;
5206 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5208 static void
5209 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5210 dw_die_ref after_die)
5212 gcc_assert (die
5213 && child_die
5214 && after_die
5215 && die->die_child
5216 && die != child_die);
5218 child_die->die_parent = die;
5219 child_die->die_sib = after_die->die_sib;
5220 after_die->die_sib = child_die;
5221 if (die->die_child == after_die)
5222 die->die_child = child_die;
5225 /* Unassociate CHILD from its parent, and make its parent be
5226 NEW_PARENT. */
5228 static void
5229 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5231 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5232 if (p->die_sib == child)
5234 remove_child_with_prev (child, p);
5235 break;
5237 add_child_die (new_parent, child);
5240 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5241 is the specification, to the end of PARENT's list of children.
5242 This is done by removing and re-adding it. */
5244 static void
5245 splice_child_die (dw_die_ref parent, dw_die_ref child)
5247 /* We want the declaration DIE from inside the class, not the
5248 specification DIE at toplevel. */
5249 if (child->die_parent != parent)
5251 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5253 if (tmp)
5254 child = tmp;
5257 gcc_assert (child->die_parent == parent
5258 || (child->die_parent
5259 == get_AT_ref (parent, DW_AT_specification)));
5261 reparent_child (child, parent);
5264 /* Create and return a new die with a parent of PARENT_DIE. If
5265 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5266 associated tree T must be supplied to determine parenthood
5267 later. */
5269 static inline dw_die_ref
5270 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5272 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5274 die->die_tag = tag_value;
5276 if (parent_die != NULL)
5277 add_child_die (parent_die, die);
5278 else
5280 limbo_die_node *limbo_node;
5282 /* No DIEs created after early dwarf should end up in limbo,
5283 because the limbo list should not persist past LTO
5284 streaming. */
5285 if (tag_value != DW_TAG_compile_unit
5286 /* These are allowed because they're generated while
5287 breaking out COMDAT units late. */
5288 && tag_value != DW_TAG_type_unit
5289 && tag_value != DW_TAG_skeleton_unit
5290 && !early_dwarf
5291 /* Allow nested functions to live in limbo because they will
5292 only temporarily live there, as decls_for_scope will fix
5293 them up. */
5294 && (TREE_CODE (t) != FUNCTION_DECL
5295 || !decl_function_context (t))
5296 /* Same as nested functions above but for types. Types that
5297 are local to a function will be fixed in
5298 decls_for_scope. */
5299 && (!RECORD_OR_UNION_TYPE_P (t)
5300 || !TYPE_CONTEXT (t)
5301 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5302 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5303 especially in the ltrans stage, but once we implement LTO
5304 dwarf streaming, we should remove this exception. */
5305 && !in_lto_p)
5307 fprintf (stderr, "symbol ended up in limbo too late:");
5308 debug_generic_stmt (t);
5309 gcc_unreachable ();
5312 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5313 limbo_node->die = die;
5314 limbo_node->created_for = t;
5315 limbo_node->next = limbo_die_list;
5316 limbo_die_list = limbo_node;
5319 return die;
5322 /* Return the DIE associated with the given type specifier. */
5324 static inline dw_die_ref
5325 lookup_type_die (tree type)
5327 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5328 if (die && die->removed)
5330 TYPE_SYMTAB_DIE (type) = NULL;
5331 return NULL;
5333 return die;
5336 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5337 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5338 anonymous type instead the one of the naming typedef. */
5340 static inline dw_die_ref
5341 strip_naming_typedef (tree type, dw_die_ref type_die)
5343 if (type
5344 && TREE_CODE (type) == RECORD_TYPE
5345 && type_die
5346 && type_die->die_tag == DW_TAG_typedef
5347 && is_naming_typedef_decl (TYPE_NAME (type)))
5348 type_die = get_AT_ref (type_die, DW_AT_type);
5349 return type_die;
5352 /* Like lookup_type_die, but if type is an anonymous type named by a
5353 typedef[1], return the DIE of the anonymous type instead the one of
5354 the naming typedef. This is because in gen_typedef_die, we did
5355 equate the anonymous struct named by the typedef with the DIE of
5356 the naming typedef. So by default, lookup_type_die on an anonymous
5357 struct yields the DIE of the naming typedef.
5359 [1]: Read the comment of is_naming_typedef_decl to learn about what
5360 a naming typedef is. */
5362 static inline dw_die_ref
5363 lookup_type_die_strip_naming_typedef (tree type)
5365 dw_die_ref die = lookup_type_die (type);
5366 return strip_naming_typedef (type, die);
5369 /* Equate a DIE to a given type specifier. */
5371 static inline void
5372 equate_type_number_to_die (tree type, dw_die_ref type_die)
5374 TYPE_SYMTAB_DIE (type) = type_die;
5377 /* Returns a hash value for X (which really is a die_struct). */
5379 inline hashval_t
5380 decl_die_hasher::hash (die_node *x)
5382 return (hashval_t) x->decl_id;
5385 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5387 inline bool
5388 decl_die_hasher::equal (die_node *x, tree y)
5390 return (x->decl_id == DECL_UID (y));
5393 /* Return the DIE associated with a given declaration. */
5395 static inline dw_die_ref
5396 lookup_decl_die (tree decl)
5398 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5399 NO_INSERT);
5400 if (!die)
5401 return NULL;
5402 if ((*die)->removed)
5404 decl_die_table->clear_slot (die);
5405 return NULL;
5407 return *die;
5410 /* Returns a hash value for X (which really is a var_loc_list). */
5412 inline hashval_t
5413 decl_loc_hasher::hash (var_loc_list *x)
5415 return (hashval_t) x->decl_id;
5418 /* Return nonzero if decl_id of var_loc_list X is the same as
5419 UID of decl *Y. */
5421 inline bool
5422 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5424 return (x->decl_id == DECL_UID (y));
5427 /* Return the var_loc list associated with a given declaration. */
5429 static inline var_loc_list *
5430 lookup_decl_loc (const_tree decl)
5432 if (!decl_loc_table)
5433 return NULL;
5434 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5437 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5439 inline hashval_t
5440 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5442 return (hashval_t) x->decl_id;
5445 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5446 UID of decl *Y. */
5448 inline bool
5449 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5451 return (x->decl_id == DECL_UID (y));
5454 /* Equate a DIE to a particular declaration. */
5456 static void
5457 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5459 unsigned int decl_id = DECL_UID (decl);
5461 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5462 decl_die->decl_id = decl_id;
5465 /* Return how many bits covers PIECE EXPR_LIST. */
5467 static HOST_WIDE_INT
5468 decl_piece_bitsize (rtx piece)
5470 int ret = (int) GET_MODE (piece);
5471 if (ret)
5472 return ret;
5473 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5474 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5475 return INTVAL (XEXP (XEXP (piece, 0), 0));
5478 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5480 static rtx *
5481 decl_piece_varloc_ptr (rtx piece)
5483 if ((int) GET_MODE (piece))
5484 return &XEXP (piece, 0);
5485 else
5486 return &XEXP (XEXP (piece, 0), 1);
5489 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5490 Next is the chain of following piece nodes. */
5492 static rtx_expr_list *
5493 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5495 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5496 return alloc_EXPR_LIST (bitsize, loc_note, next);
5497 else
5498 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5499 GEN_INT (bitsize),
5500 loc_note), next);
5503 /* Return rtx that should be stored into loc field for
5504 LOC_NOTE and BITPOS/BITSIZE. */
5506 static rtx
5507 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5508 HOST_WIDE_INT bitsize)
5510 if (bitsize != -1)
5512 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5513 if (bitpos != 0)
5514 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5516 return loc_note;
5519 /* This function either modifies location piece list *DEST in
5520 place (if SRC and INNER is NULL), or copies location piece list
5521 *SRC to *DEST while modifying it. Location BITPOS is modified
5522 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5523 not copied and if needed some padding around it is added.
5524 When modifying in place, DEST should point to EXPR_LIST where
5525 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5526 to the start of the whole list and INNER points to the EXPR_LIST
5527 where earlier pieces cover PIECE_BITPOS bits. */
5529 static void
5530 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5531 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5532 HOST_WIDE_INT bitsize, rtx loc_note)
5534 HOST_WIDE_INT diff;
5535 bool copy = inner != NULL;
5537 if (copy)
5539 /* First copy all nodes preceding the current bitpos. */
5540 while (src != inner)
5542 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5543 decl_piece_bitsize (*src), NULL_RTX);
5544 dest = &XEXP (*dest, 1);
5545 src = &XEXP (*src, 1);
5548 /* Add padding if needed. */
5549 if (bitpos != piece_bitpos)
5551 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5552 copy ? NULL_RTX : *dest);
5553 dest = &XEXP (*dest, 1);
5555 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5557 gcc_assert (!copy);
5558 /* A piece with correct bitpos and bitsize already exist,
5559 just update the location for it and return. */
5560 *decl_piece_varloc_ptr (*dest) = loc_note;
5561 return;
5563 /* Add the piece that changed. */
5564 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5565 dest = &XEXP (*dest, 1);
5566 /* Skip over pieces that overlap it. */
5567 diff = bitpos - piece_bitpos + bitsize;
5568 if (!copy)
5569 src = dest;
5570 while (diff > 0 && *src)
5572 rtx piece = *src;
5573 diff -= decl_piece_bitsize (piece);
5574 if (copy)
5575 src = &XEXP (piece, 1);
5576 else
5578 *src = XEXP (piece, 1);
5579 free_EXPR_LIST_node (piece);
5582 /* Add padding if needed. */
5583 if (diff < 0 && *src)
5585 if (!copy)
5586 dest = src;
5587 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5588 dest = &XEXP (*dest, 1);
5590 if (!copy)
5591 return;
5592 /* Finally copy all nodes following it. */
5593 while (*src)
5595 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5596 decl_piece_bitsize (*src), NULL_RTX);
5597 dest = &XEXP (*dest, 1);
5598 src = &XEXP (*src, 1);
5602 /* Add a variable location node to the linked list for DECL. */
5604 static struct var_loc_node *
5605 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5607 unsigned int decl_id;
5608 var_loc_list *temp;
5609 struct var_loc_node *loc = NULL;
5610 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5612 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5614 tree realdecl = DECL_DEBUG_EXPR (decl);
5615 if (handled_component_p (realdecl)
5616 || (TREE_CODE (realdecl) == MEM_REF
5617 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5619 HOST_WIDE_INT maxsize;
5620 bool reverse;
5621 tree innerdecl
5622 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5623 &reverse);
5624 if (!DECL_P (innerdecl)
5625 || DECL_IGNORED_P (innerdecl)
5626 || TREE_STATIC (innerdecl)
5627 || bitsize <= 0
5628 || bitpos + bitsize > 256
5629 || bitsize != maxsize)
5630 return NULL;
5631 decl = innerdecl;
5635 decl_id = DECL_UID (decl);
5636 var_loc_list **slot
5637 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5638 if (*slot == NULL)
5640 temp = ggc_cleared_alloc<var_loc_list> ();
5641 temp->decl_id = decl_id;
5642 *slot = temp;
5644 else
5645 temp = *slot;
5647 /* For PARM_DECLs try to keep around the original incoming value,
5648 even if that means we'll emit a zero-range .debug_loc entry. */
5649 if (temp->last
5650 && temp->first == temp->last
5651 && TREE_CODE (decl) == PARM_DECL
5652 && NOTE_P (temp->first->loc)
5653 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5654 && DECL_INCOMING_RTL (decl)
5655 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5656 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5657 == GET_CODE (DECL_INCOMING_RTL (decl))
5658 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5659 && (bitsize != -1
5660 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5661 NOTE_VAR_LOCATION_LOC (loc_note))
5662 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5663 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5665 loc = ggc_cleared_alloc<var_loc_node> ();
5666 temp->first->next = loc;
5667 temp->last = loc;
5668 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5670 else if (temp->last)
5672 struct var_loc_node *last = temp->last, *unused = NULL;
5673 rtx *piece_loc = NULL, last_loc_note;
5674 HOST_WIDE_INT piece_bitpos = 0;
5675 if (last->next)
5677 last = last->next;
5678 gcc_assert (last->next == NULL);
5680 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5682 piece_loc = &last->loc;
5685 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5686 if (piece_bitpos + cur_bitsize > bitpos)
5687 break;
5688 piece_bitpos += cur_bitsize;
5689 piece_loc = &XEXP (*piece_loc, 1);
5691 while (*piece_loc);
5693 /* TEMP->LAST here is either pointer to the last but one or
5694 last element in the chained list, LAST is pointer to the
5695 last element. */
5696 if (label && strcmp (last->label, label) == 0)
5698 /* For SRA optimized variables if there weren't any real
5699 insns since last note, just modify the last node. */
5700 if (piece_loc != NULL)
5702 adjust_piece_list (piece_loc, NULL, NULL,
5703 bitpos, piece_bitpos, bitsize, loc_note);
5704 return NULL;
5706 /* If the last note doesn't cover any instructions, remove it. */
5707 if (temp->last != last)
5709 temp->last->next = NULL;
5710 unused = last;
5711 last = temp->last;
5712 gcc_assert (strcmp (last->label, label) != 0);
5714 else
5716 gcc_assert (temp->first == temp->last
5717 || (temp->first->next == temp->last
5718 && TREE_CODE (decl) == PARM_DECL));
5719 memset (temp->last, '\0', sizeof (*temp->last));
5720 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5721 return temp->last;
5724 if (bitsize == -1 && NOTE_P (last->loc))
5725 last_loc_note = last->loc;
5726 else if (piece_loc != NULL
5727 && *piece_loc != NULL_RTX
5728 && piece_bitpos == bitpos
5729 && decl_piece_bitsize (*piece_loc) == bitsize)
5730 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5731 else
5732 last_loc_note = NULL_RTX;
5733 /* If the current location is the same as the end of the list,
5734 and either both or neither of the locations is uninitialized,
5735 we have nothing to do. */
5736 if (last_loc_note == NULL_RTX
5737 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5738 NOTE_VAR_LOCATION_LOC (loc_note)))
5739 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5740 != NOTE_VAR_LOCATION_STATUS (loc_note))
5741 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5742 == VAR_INIT_STATUS_UNINITIALIZED)
5743 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5744 == VAR_INIT_STATUS_UNINITIALIZED))))
5746 /* Add LOC to the end of list and update LAST. If the last
5747 element of the list has been removed above, reuse its
5748 memory for the new node, otherwise allocate a new one. */
5749 if (unused)
5751 loc = unused;
5752 memset (loc, '\0', sizeof (*loc));
5754 else
5755 loc = ggc_cleared_alloc<var_loc_node> ();
5756 if (bitsize == -1 || piece_loc == NULL)
5757 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5758 else
5759 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5760 bitpos, piece_bitpos, bitsize, loc_note);
5761 last->next = loc;
5762 /* Ensure TEMP->LAST will point either to the new last but one
5763 element of the chain, or to the last element in it. */
5764 if (last != temp->last)
5765 temp->last = last;
5767 else if (unused)
5768 ggc_free (unused);
5770 else
5772 loc = ggc_cleared_alloc<var_loc_node> ();
5773 temp->first = loc;
5774 temp->last = loc;
5775 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5777 return loc;
5780 /* Keep track of the number of spaces used to indent the
5781 output of the debugging routines that print the structure of
5782 the DIE internal representation. */
5783 static int print_indent;
5785 /* Indent the line the number of spaces given by print_indent. */
5787 static inline void
5788 print_spaces (FILE *outfile)
5790 fprintf (outfile, "%*s", print_indent, "");
5793 /* Print a type signature in hex. */
5795 static inline void
5796 print_signature (FILE *outfile, char *sig)
5798 int i;
5800 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5801 fprintf (outfile, "%02x", sig[i] & 0xff);
5804 static inline void
5805 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5807 if (discr_value->pos)
5808 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5809 else
5810 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5813 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5815 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5816 RECURSE, output location descriptor operations. */
5818 static void
5819 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5821 switch (val->val_class)
5823 case dw_val_class_addr:
5824 fprintf (outfile, "address");
5825 break;
5826 case dw_val_class_offset:
5827 fprintf (outfile, "offset");
5828 break;
5829 case dw_val_class_loc:
5830 fprintf (outfile, "location descriptor");
5831 if (val->v.val_loc == NULL)
5832 fprintf (outfile, " -> <null>\n");
5833 else if (recurse)
5835 fprintf (outfile, ":\n");
5836 print_indent += 4;
5837 print_loc_descr (val->v.val_loc, outfile);
5838 print_indent -= 4;
5840 else
5841 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5842 break;
5843 case dw_val_class_loc_list:
5844 fprintf (outfile, "location list -> label:%s",
5845 val->v.val_loc_list->ll_symbol);
5846 break;
5847 case dw_val_class_range_list:
5848 fprintf (outfile, "range list");
5849 break;
5850 case dw_val_class_const:
5851 case dw_val_class_const_implicit:
5852 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5853 break;
5854 case dw_val_class_unsigned_const:
5855 case dw_val_class_unsigned_const_implicit:
5856 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5857 break;
5858 case dw_val_class_const_double:
5859 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5860 HOST_WIDE_INT_PRINT_UNSIGNED")",
5861 val->v.val_double.high,
5862 val->v.val_double.low);
5863 break;
5864 case dw_val_class_wide_int:
5866 int i = val->v.val_wide->get_len ();
5867 fprintf (outfile, "constant (");
5868 gcc_assert (i > 0);
5869 if (val->v.val_wide->elt (i - 1) == 0)
5870 fprintf (outfile, "0x");
5871 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5872 val->v.val_wide->elt (--i));
5873 while (--i >= 0)
5874 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5875 val->v.val_wide->elt (i));
5876 fprintf (outfile, ")");
5877 break;
5879 case dw_val_class_vec:
5880 fprintf (outfile, "floating-point or vector constant");
5881 break;
5882 case dw_val_class_flag:
5883 fprintf (outfile, "%u", val->v.val_flag);
5884 break;
5885 case dw_val_class_die_ref:
5886 if (val->v.val_die_ref.die != NULL)
5888 dw_die_ref die = val->v.val_die_ref.die;
5890 if (die->comdat_type_p)
5892 fprintf (outfile, "die -> signature: ");
5893 print_signature (outfile,
5894 die->die_id.die_type_node->signature);
5896 else if (die->die_id.die_symbol)
5897 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5898 else
5899 fprintf (outfile, "die -> %ld", die->die_offset);
5900 fprintf (outfile, " (%p)", (void *) die);
5902 else
5903 fprintf (outfile, "die -> <null>");
5904 break;
5905 case dw_val_class_vms_delta:
5906 fprintf (outfile, "delta: @slotcount(%s-%s)",
5907 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5908 break;
5909 case dw_val_class_lbl_id:
5910 case dw_val_class_lineptr:
5911 case dw_val_class_macptr:
5912 case dw_val_class_loclistsptr:
5913 case dw_val_class_high_pc:
5914 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5915 break;
5916 case dw_val_class_str:
5917 if (val->v.val_str->str != NULL)
5918 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5919 else
5920 fprintf (outfile, "<null>");
5921 break;
5922 case dw_val_class_file:
5923 case dw_val_class_file_implicit:
5924 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5925 val->v.val_file->emitted_number);
5926 break;
5927 case dw_val_class_data8:
5929 int i;
5931 for (i = 0; i < 8; i++)
5932 fprintf (outfile, "%02x", val->v.val_data8[i]);
5933 break;
5935 case dw_val_class_discr_value:
5936 print_discr_value (outfile, &val->v.val_discr_value);
5937 break;
5938 case dw_val_class_discr_list:
5939 for (dw_discr_list_ref node = val->v.val_discr_list;
5940 node != NULL;
5941 node = node->dw_discr_next)
5943 if (node->dw_discr_range)
5945 fprintf (outfile, " .. ");
5946 print_discr_value (outfile, &node->dw_discr_lower_bound);
5947 print_discr_value (outfile, &node->dw_discr_upper_bound);
5949 else
5950 print_discr_value (outfile, &node->dw_discr_lower_bound);
5952 if (node->dw_discr_next != NULL)
5953 fprintf (outfile, " | ");
5955 default:
5956 break;
5960 /* Likewise, for a DIE attribute. */
5962 static void
5963 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5965 print_dw_val (&a->dw_attr_val, recurse, outfile);
5969 /* Print the list of operands in the LOC location description to OUTFILE. This
5970 routine is a debugging aid only. */
5972 static void
5973 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5975 dw_loc_descr_ref l = loc;
5977 if (loc == NULL)
5979 print_spaces (outfile);
5980 fprintf (outfile, "<null>\n");
5981 return;
5984 for (l = loc; l != NULL; l = l->dw_loc_next)
5986 print_spaces (outfile);
5987 fprintf (outfile, "(%p) %s",
5988 (void *) l,
5989 dwarf_stack_op_name (l->dw_loc_opc));
5990 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5992 fprintf (outfile, " ");
5993 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5995 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5997 fprintf (outfile, ", ");
5998 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6000 fprintf (outfile, "\n");
6004 /* Print the information associated with a given DIE, and its children.
6005 This routine is a debugging aid only. */
6007 static void
6008 print_die (dw_die_ref die, FILE *outfile)
6010 dw_attr_node *a;
6011 dw_die_ref c;
6012 unsigned ix;
6014 print_spaces (outfile);
6015 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6016 die->die_offset, dwarf_tag_name (die->die_tag),
6017 (void*) die);
6018 print_spaces (outfile);
6019 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6020 fprintf (outfile, " offset: %ld", die->die_offset);
6021 fprintf (outfile, " mark: %d\n", die->die_mark);
6023 if (die->comdat_type_p)
6025 print_spaces (outfile);
6026 fprintf (outfile, " signature: ");
6027 print_signature (outfile, die->die_id.die_type_node->signature);
6028 fprintf (outfile, "\n");
6031 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6033 print_spaces (outfile);
6034 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6036 print_attribute (a, true, outfile);
6037 fprintf (outfile, "\n");
6040 if (die->die_child != NULL)
6042 print_indent += 4;
6043 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6044 print_indent -= 4;
6046 if (print_indent == 0)
6047 fprintf (outfile, "\n");
6050 /* Print the list of operations in the LOC location description. */
6052 DEBUG_FUNCTION void
6053 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6055 print_loc_descr (loc, stderr);
6058 /* Print the information collected for a given DIE. */
6060 DEBUG_FUNCTION void
6061 debug_dwarf_die (dw_die_ref die)
6063 print_die (die, stderr);
6066 DEBUG_FUNCTION void
6067 debug (die_struct &ref)
6069 print_die (&ref, stderr);
6072 DEBUG_FUNCTION void
6073 debug (die_struct *ptr)
6075 if (ptr)
6076 debug (*ptr);
6077 else
6078 fprintf (stderr, "<nil>\n");
6082 /* Print all DWARF information collected for the compilation unit.
6083 This routine is a debugging aid only. */
6085 DEBUG_FUNCTION void
6086 debug_dwarf (void)
6088 print_indent = 0;
6089 print_die (comp_unit_die (), stderr);
6092 /* Verify the DIE tree structure. */
6094 DEBUG_FUNCTION void
6095 verify_die (dw_die_ref die)
6097 gcc_assert (!die->die_mark);
6098 if (die->die_parent == NULL
6099 && die->die_sib == NULL)
6100 return;
6101 /* Verify the die_sib list is cyclic. */
6102 dw_die_ref x = die;
6105 x->die_mark = 1;
6106 x = x->die_sib;
6108 while (x && !x->die_mark);
6109 gcc_assert (x == die);
6110 x = die;
6113 /* Verify all dies have the same parent. */
6114 gcc_assert (x->die_parent == die->die_parent);
6115 if (x->die_child)
6117 /* Verify the child has the proper parent and recurse. */
6118 gcc_assert (x->die_child->die_parent == x);
6119 verify_die (x->die_child);
6121 x->die_mark = 0;
6122 x = x->die_sib;
6124 while (x && x->die_mark);
6127 /* Sanity checks on DIEs. */
6129 static void
6130 check_die (dw_die_ref die)
6132 unsigned ix;
6133 dw_attr_node *a;
6134 bool inline_found = false;
6135 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6136 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6137 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6139 switch (a->dw_attr)
6141 case DW_AT_inline:
6142 if (a->dw_attr_val.v.val_unsigned)
6143 inline_found = true;
6144 break;
6145 case DW_AT_location:
6146 ++n_location;
6147 break;
6148 case DW_AT_low_pc:
6149 ++n_low_pc;
6150 break;
6151 case DW_AT_high_pc:
6152 ++n_high_pc;
6153 break;
6154 case DW_AT_artificial:
6155 ++n_artificial;
6156 break;
6157 case DW_AT_decl_column:
6158 ++n_decl_column;
6159 break;
6160 case DW_AT_decl_line:
6161 ++n_decl_line;
6162 break;
6163 case DW_AT_decl_file:
6164 ++n_decl_file;
6165 break;
6166 default:
6167 break;
6170 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6171 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6173 fprintf (stderr, "Duplicate attributes in DIE:\n");
6174 debug_dwarf_die (die);
6175 gcc_unreachable ();
6177 if (inline_found)
6179 /* A debugging information entry that is a member of an abstract
6180 instance tree [that has DW_AT_inline] should not contain any
6181 attributes which describe aspects of the subroutine which vary
6182 between distinct inlined expansions or distinct out-of-line
6183 expansions. */
6184 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6185 gcc_assert (a->dw_attr != DW_AT_low_pc
6186 && a->dw_attr != DW_AT_high_pc
6187 && a->dw_attr != DW_AT_location
6188 && a->dw_attr != DW_AT_frame_base
6189 && a->dw_attr != DW_AT_call_all_calls
6190 && a->dw_attr != DW_AT_GNU_all_call_sites);
6194 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6195 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6196 DIE that marks the start of the DIEs for this include file. */
6198 static dw_die_ref
6199 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6201 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6202 dw_die_ref new_unit = gen_compile_unit_die (filename);
6204 new_unit->die_sib = old_unit;
6205 return new_unit;
6208 /* Close an include-file CU and reopen the enclosing one. */
6210 static dw_die_ref
6211 pop_compile_unit (dw_die_ref old_unit)
6213 dw_die_ref new_unit = old_unit->die_sib;
6215 old_unit->die_sib = NULL;
6216 return new_unit;
6219 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6220 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6221 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6223 /* Calculate the checksum of a location expression. */
6225 static inline void
6226 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6228 int tem;
6229 inchash::hash hstate;
6230 hashval_t hash;
6232 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6233 CHECKSUM (tem);
6234 hash_loc_operands (loc, hstate);
6235 hash = hstate.end();
6236 CHECKSUM (hash);
6239 /* Calculate the checksum of an attribute. */
6241 static void
6242 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6244 dw_loc_descr_ref loc;
6245 rtx r;
6247 CHECKSUM (at->dw_attr);
6249 /* We don't care that this was compiled with a different compiler
6250 snapshot; if the output is the same, that's what matters. */
6251 if (at->dw_attr == DW_AT_producer)
6252 return;
6254 switch (AT_class (at))
6256 case dw_val_class_const:
6257 case dw_val_class_const_implicit:
6258 CHECKSUM (at->dw_attr_val.v.val_int);
6259 break;
6260 case dw_val_class_unsigned_const:
6261 case dw_val_class_unsigned_const_implicit:
6262 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6263 break;
6264 case dw_val_class_const_double:
6265 CHECKSUM (at->dw_attr_val.v.val_double);
6266 break;
6267 case dw_val_class_wide_int:
6268 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6269 get_full_len (*at->dw_attr_val.v.val_wide)
6270 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6271 break;
6272 case dw_val_class_vec:
6273 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6274 (at->dw_attr_val.v.val_vec.length
6275 * at->dw_attr_val.v.val_vec.elt_size));
6276 break;
6277 case dw_val_class_flag:
6278 CHECKSUM (at->dw_attr_val.v.val_flag);
6279 break;
6280 case dw_val_class_str:
6281 CHECKSUM_STRING (AT_string (at));
6282 break;
6284 case dw_val_class_addr:
6285 r = AT_addr (at);
6286 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6287 CHECKSUM_STRING (XSTR (r, 0));
6288 break;
6290 case dw_val_class_offset:
6291 CHECKSUM (at->dw_attr_val.v.val_offset);
6292 break;
6294 case dw_val_class_loc:
6295 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6296 loc_checksum (loc, ctx);
6297 break;
6299 case dw_val_class_die_ref:
6300 die_checksum (AT_ref (at), ctx, mark);
6301 break;
6303 case dw_val_class_fde_ref:
6304 case dw_val_class_vms_delta:
6305 case dw_val_class_lbl_id:
6306 case dw_val_class_lineptr:
6307 case dw_val_class_macptr:
6308 case dw_val_class_loclistsptr:
6309 case dw_val_class_high_pc:
6310 break;
6312 case dw_val_class_file:
6313 case dw_val_class_file_implicit:
6314 CHECKSUM_STRING (AT_file (at)->filename);
6315 break;
6317 case dw_val_class_data8:
6318 CHECKSUM (at->dw_attr_val.v.val_data8);
6319 break;
6321 default:
6322 break;
6326 /* Calculate the checksum of a DIE. */
6328 static void
6329 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6331 dw_die_ref c;
6332 dw_attr_node *a;
6333 unsigned ix;
6335 /* To avoid infinite recursion. */
6336 if (die->die_mark)
6338 CHECKSUM (die->die_mark);
6339 return;
6341 die->die_mark = ++(*mark);
6343 CHECKSUM (die->die_tag);
6345 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6346 attr_checksum (a, ctx, mark);
6348 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6351 #undef CHECKSUM
6352 #undef CHECKSUM_BLOCK
6353 #undef CHECKSUM_STRING
6355 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6356 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6357 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6358 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6359 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6360 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6361 #define CHECKSUM_ATTR(FOO) \
6362 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6364 /* Calculate the checksum of a number in signed LEB128 format. */
6366 static void
6367 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6369 unsigned char byte;
6370 bool more;
6372 while (1)
6374 byte = (value & 0x7f);
6375 value >>= 7;
6376 more = !((value == 0 && (byte & 0x40) == 0)
6377 || (value == -1 && (byte & 0x40) != 0));
6378 if (more)
6379 byte |= 0x80;
6380 CHECKSUM (byte);
6381 if (!more)
6382 break;
6386 /* Calculate the checksum of a number in unsigned LEB128 format. */
6388 static void
6389 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6391 while (1)
6393 unsigned char byte = (value & 0x7f);
6394 value >>= 7;
6395 if (value != 0)
6396 /* More bytes to follow. */
6397 byte |= 0x80;
6398 CHECKSUM (byte);
6399 if (value == 0)
6400 break;
6404 /* Checksum the context of the DIE. This adds the names of any
6405 surrounding namespaces or structures to the checksum. */
6407 static void
6408 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6410 const char *name;
6411 dw_die_ref spec;
6412 int tag = die->die_tag;
6414 if (tag != DW_TAG_namespace
6415 && tag != DW_TAG_structure_type
6416 && tag != DW_TAG_class_type)
6417 return;
6419 name = get_AT_string (die, DW_AT_name);
6421 spec = get_AT_ref (die, DW_AT_specification);
6422 if (spec != NULL)
6423 die = spec;
6425 if (die->die_parent != NULL)
6426 checksum_die_context (die->die_parent, ctx);
6428 CHECKSUM_ULEB128 ('C');
6429 CHECKSUM_ULEB128 (tag);
6430 if (name != NULL)
6431 CHECKSUM_STRING (name);
6434 /* Calculate the checksum of a location expression. */
6436 static inline void
6437 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6439 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6440 were emitted as a DW_FORM_sdata instead of a location expression. */
6441 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6443 CHECKSUM_ULEB128 (DW_FORM_sdata);
6444 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6445 return;
6448 /* Otherwise, just checksum the raw location expression. */
6449 while (loc != NULL)
6451 inchash::hash hstate;
6452 hashval_t hash;
6454 CHECKSUM_ULEB128 (loc->dtprel);
6455 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6456 hash_loc_operands (loc, hstate);
6457 hash = hstate.end ();
6458 CHECKSUM (hash);
6459 loc = loc->dw_loc_next;
6463 /* Calculate the checksum of an attribute. */
6465 static void
6466 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6467 struct md5_ctx *ctx, int *mark)
6469 dw_loc_descr_ref loc;
6470 rtx r;
6472 if (AT_class (at) == dw_val_class_die_ref)
6474 dw_die_ref target_die = AT_ref (at);
6476 /* For pointer and reference types, we checksum only the (qualified)
6477 name of the target type (if there is a name). For friend entries,
6478 we checksum only the (qualified) name of the target type or function.
6479 This allows the checksum to remain the same whether the target type
6480 is complete or not. */
6481 if ((at->dw_attr == DW_AT_type
6482 && (tag == DW_TAG_pointer_type
6483 || tag == DW_TAG_reference_type
6484 || tag == DW_TAG_rvalue_reference_type
6485 || tag == DW_TAG_ptr_to_member_type))
6486 || (at->dw_attr == DW_AT_friend
6487 && tag == DW_TAG_friend))
6489 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6491 if (name_attr != NULL)
6493 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6495 if (decl == NULL)
6496 decl = target_die;
6497 CHECKSUM_ULEB128 ('N');
6498 CHECKSUM_ULEB128 (at->dw_attr);
6499 if (decl->die_parent != NULL)
6500 checksum_die_context (decl->die_parent, ctx);
6501 CHECKSUM_ULEB128 ('E');
6502 CHECKSUM_STRING (AT_string (name_attr));
6503 return;
6507 /* For all other references to another DIE, we check to see if the
6508 target DIE has already been visited. If it has, we emit a
6509 backward reference; if not, we descend recursively. */
6510 if (target_die->die_mark > 0)
6512 CHECKSUM_ULEB128 ('R');
6513 CHECKSUM_ULEB128 (at->dw_attr);
6514 CHECKSUM_ULEB128 (target_die->die_mark);
6516 else
6518 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6520 if (decl == NULL)
6521 decl = target_die;
6522 target_die->die_mark = ++(*mark);
6523 CHECKSUM_ULEB128 ('T');
6524 CHECKSUM_ULEB128 (at->dw_attr);
6525 if (decl->die_parent != NULL)
6526 checksum_die_context (decl->die_parent, ctx);
6527 die_checksum_ordered (target_die, ctx, mark);
6529 return;
6532 CHECKSUM_ULEB128 ('A');
6533 CHECKSUM_ULEB128 (at->dw_attr);
6535 switch (AT_class (at))
6537 case dw_val_class_const:
6538 case dw_val_class_const_implicit:
6539 CHECKSUM_ULEB128 (DW_FORM_sdata);
6540 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6541 break;
6543 case dw_val_class_unsigned_const:
6544 case dw_val_class_unsigned_const_implicit:
6545 CHECKSUM_ULEB128 (DW_FORM_sdata);
6546 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6547 break;
6549 case dw_val_class_const_double:
6550 CHECKSUM_ULEB128 (DW_FORM_block);
6551 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6552 CHECKSUM (at->dw_attr_val.v.val_double);
6553 break;
6555 case dw_val_class_wide_int:
6556 CHECKSUM_ULEB128 (DW_FORM_block);
6557 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6558 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6559 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6560 get_full_len (*at->dw_attr_val.v.val_wide)
6561 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6562 break;
6564 case dw_val_class_vec:
6565 CHECKSUM_ULEB128 (DW_FORM_block);
6566 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6567 * at->dw_attr_val.v.val_vec.elt_size);
6568 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6569 (at->dw_attr_val.v.val_vec.length
6570 * at->dw_attr_val.v.val_vec.elt_size));
6571 break;
6573 case dw_val_class_flag:
6574 CHECKSUM_ULEB128 (DW_FORM_flag);
6575 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6576 break;
6578 case dw_val_class_str:
6579 CHECKSUM_ULEB128 (DW_FORM_string);
6580 CHECKSUM_STRING (AT_string (at));
6581 break;
6583 case dw_val_class_addr:
6584 r = AT_addr (at);
6585 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6586 CHECKSUM_ULEB128 (DW_FORM_string);
6587 CHECKSUM_STRING (XSTR (r, 0));
6588 break;
6590 case dw_val_class_offset:
6591 CHECKSUM_ULEB128 (DW_FORM_sdata);
6592 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6593 break;
6595 case dw_val_class_loc:
6596 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6597 loc_checksum_ordered (loc, ctx);
6598 break;
6600 case dw_val_class_fde_ref:
6601 case dw_val_class_lbl_id:
6602 case dw_val_class_lineptr:
6603 case dw_val_class_macptr:
6604 case dw_val_class_loclistsptr:
6605 case dw_val_class_high_pc:
6606 break;
6608 case dw_val_class_file:
6609 case dw_val_class_file_implicit:
6610 CHECKSUM_ULEB128 (DW_FORM_string);
6611 CHECKSUM_STRING (AT_file (at)->filename);
6612 break;
6614 case dw_val_class_data8:
6615 CHECKSUM (at->dw_attr_val.v.val_data8);
6616 break;
6618 default:
6619 break;
6623 struct checksum_attributes
6625 dw_attr_node *at_name;
6626 dw_attr_node *at_type;
6627 dw_attr_node *at_friend;
6628 dw_attr_node *at_accessibility;
6629 dw_attr_node *at_address_class;
6630 dw_attr_node *at_alignment;
6631 dw_attr_node *at_allocated;
6632 dw_attr_node *at_artificial;
6633 dw_attr_node *at_associated;
6634 dw_attr_node *at_binary_scale;
6635 dw_attr_node *at_bit_offset;
6636 dw_attr_node *at_bit_size;
6637 dw_attr_node *at_bit_stride;
6638 dw_attr_node *at_byte_size;
6639 dw_attr_node *at_byte_stride;
6640 dw_attr_node *at_const_value;
6641 dw_attr_node *at_containing_type;
6642 dw_attr_node *at_count;
6643 dw_attr_node *at_data_location;
6644 dw_attr_node *at_data_member_location;
6645 dw_attr_node *at_decimal_scale;
6646 dw_attr_node *at_decimal_sign;
6647 dw_attr_node *at_default_value;
6648 dw_attr_node *at_digit_count;
6649 dw_attr_node *at_discr;
6650 dw_attr_node *at_discr_list;
6651 dw_attr_node *at_discr_value;
6652 dw_attr_node *at_encoding;
6653 dw_attr_node *at_endianity;
6654 dw_attr_node *at_explicit;
6655 dw_attr_node *at_is_optional;
6656 dw_attr_node *at_location;
6657 dw_attr_node *at_lower_bound;
6658 dw_attr_node *at_mutable;
6659 dw_attr_node *at_ordering;
6660 dw_attr_node *at_picture_string;
6661 dw_attr_node *at_prototyped;
6662 dw_attr_node *at_small;
6663 dw_attr_node *at_segment;
6664 dw_attr_node *at_string_length;
6665 dw_attr_node *at_string_length_bit_size;
6666 dw_attr_node *at_string_length_byte_size;
6667 dw_attr_node *at_threads_scaled;
6668 dw_attr_node *at_upper_bound;
6669 dw_attr_node *at_use_location;
6670 dw_attr_node *at_use_UTF8;
6671 dw_attr_node *at_variable_parameter;
6672 dw_attr_node *at_virtuality;
6673 dw_attr_node *at_visibility;
6674 dw_attr_node *at_vtable_elem_location;
6677 /* Collect the attributes that we will want to use for the checksum. */
6679 static void
6680 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6682 dw_attr_node *a;
6683 unsigned ix;
6685 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6687 switch (a->dw_attr)
6689 case DW_AT_name:
6690 attrs->at_name = a;
6691 break;
6692 case DW_AT_type:
6693 attrs->at_type = a;
6694 break;
6695 case DW_AT_friend:
6696 attrs->at_friend = a;
6697 break;
6698 case DW_AT_accessibility:
6699 attrs->at_accessibility = a;
6700 break;
6701 case DW_AT_address_class:
6702 attrs->at_address_class = a;
6703 break;
6704 case DW_AT_alignment:
6705 attrs->at_alignment = a;
6706 break;
6707 case DW_AT_allocated:
6708 attrs->at_allocated = a;
6709 break;
6710 case DW_AT_artificial:
6711 attrs->at_artificial = a;
6712 break;
6713 case DW_AT_associated:
6714 attrs->at_associated = a;
6715 break;
6716 case DW_AT_binary_scale:
6717 attrs->at_binary_scale = a;
6718 break;
6719 case DW_AT_bit_offset:
6720 attrs->at_bit_offset = a;
6721 break;
6722 case DW_AT_bit_size:
6723 attrs->at_bit_size = a;
6724 break;
6725 case DW_AT_bit_stride:
6726 attrs->at_bit_stride = a;
6727 break;
6728 case DW_AT_byte_size:
6729 attrs->at_byte_size = a;
6730 break;
6731 case DW_AT_byte_stride:
6732 attrs->at_byte_stride = a;
6733 break;
6734 case DW_AT_const_value:
6735 attrs->at_const_value = a;
6736 break;
6737 case DW_AT_containing_type:
6738 attrs->at_containing_type = a;
6739 break;
6740 case DW_AT_count:
6741 attrs->at_count = a;
6742 break;
6743 case DW_AT_data_location:
6744 attrs->at_data_location = a;
6745 break;
6746 case DW_AT_data_member_location:
6747 attrs->at_data_member_location = a;
6748 break;
6749 case DW_AT_decimal_scale:
6750 attrs->at_decimal_scale = a;
6751 break;
6752 case DW_AT_decimal_sign:
6753 attrs->at_decimal_sign = a;
6754 break;
6755 case DW_AT_default_value:
6756 attrs->at_default_value = a;
6757 break;
6758 case DW_AT_digit_count:
6759 attrs->at_digit_count = a;
6760 break;
6761 case DW_AT_discr:
6762 attrs->at_discr = a;
6763 break;
6764 case DW_AT_discr_list:
6765 attrs->at_discr_list = a;
6766 break;
6767 case DW_AT_discr_value:
6768 attrs->at_discr_value = a;
6769 break;
6770 case DW_AT_encoding:
6771 attrs->at_encoding = a;
6772 break;
6773 case DW_AT_endianity:
6774 attrs->at_endianity = a;
6775 break;
6776 case DW_AT_explicit:
6777 attrs->at_explicit = a;
6778 break;
6779 case DW_AT_is_optional:
6780 attrs->at_is_optional = a;
6781 break;
6782 case DW_AT_location:
6783 attrs->at_location = a;
6784 break;
6785 case DW_AT_lower_bound:
6786 attrs->at_lower_bound = a;
6787 break;
6788 case DW_AT_mutable:
6789 attrs->at_mutable = a;
6790 break;
6791 case DW_AT_ordering:
6792 attrs->at_ordering = a;
6793 break;
6794 case DW_AT_picture_string:
6795 attrs->at_picture_string = a;
6796 break;
6797 case DW_AT_prototyped:
6798 attrs->at_prototyped = a;
6799 break;
6800 case DW_AT_small:
6801 attrs->at_small = a;
6802 break;
6803 case DW_AT_segment:
6804 attrs->at_segment = a;
6805 break;
6806 case DW_AT_string_length:
6807 attrs->at_string_length = a;
6808 break;
6809 case DW_AT_string_length_bit_size:
6810 attrs->at_string_length_bit_size = a;
6811 break;
6812 case DW_AT_string_length_byte_size:
6813 attrs->at_string_length_byte_size = a;
6814 break;
6815 case DW_AT_threads_scaled:
6816 attrs->at_threads_scaled = a;
6817 break;
6818 case DW_AT_upper_bound:
6819 attrs->at_upper_bound = a;
6820 break;
6821 case DW_AT_use_location:
6822 attrs->at_use_location = a;
6823 break;
6824 case DW_AT_use_UTF8:
6825 attrs->at_use_UTF8 = a;
6826 break;
6827 case DW_AT_variable_parameter:
6828 attrs->at_variable_parameter = a;
6829 break;
6830 case DW_AT_virtuality:
6831 attrs->at_virtuality = a;
6832 break;
6833 case DW_AT_visibility:
6834 attrs->at_visibility = a;
6835 break;
6836 case DW_AT_vtable_elem_location:
6837 attrs->at_vtable_elem_location = a;
6838 break;
6839 default:
6840 break;
6845 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6847 static void
6848 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6850 dw_die_ref c;
6851 dw_die_ref decl;
6852 struct checksum_attributes attrs;
6854 CHECKSUM_ULEB128 ('D');
6855 CHECKSUM_ULEB128 (die->die_tag);
6857 memset (&attrs, 0, sizeof (attrs));
6859 decl = get_AT_ref (die, DW_AT_specification);
6860 if (decl != NULL)
6861 collect_checksum_attributes (&attrs, decl);
6862 collect_checksum_attributes (&attrs, die);
6864 CHECKSUM_ATTR (attrs.at_name);
6865 CHECKSUM_ATTR (attrs.at_accessibility);
6866 CHECKSUM_ATTR (attrs.at_address_class);
6867 CHECKSUM_ATTR (attrs.at_allocated);
6868 CHECKSUM_ATTR (attrs.at_artificial);
6869 CHECKSUM_ATTR (attrs.at_associated);
6870 CHECKSUM_ATTR (attrs.at_binary_scale);
6871 CHECKSUM_ATTR (attrs.at_bit_offset);
6872 CHECKSUM_ATTR (attrs.at_bit_size);
6873 CHECKSUM_ATTR (attrs.at_bit_stride);
6874 CHECKSUM_ATTR (attrs.at_byte_size);
6875 CHECKSUM_ATTR (attrs.at_byte_stride);
6876 CHECKSUM_ATTR (attrs.at_const_value);
6877 CHECKSUM_ATTR (attrs.at_containing_type);
6878 CHECKSUM_ATTR (attrs.at_count);
6879 CHECKSUM_ATTR (attrs.at_data_location);
6880 CHECKSUM_ATTR (attrs.at_data_member_location);
6881 CHECKSUM_ATTR (attrs.at_decimal_scale);
6882 CHECKSUM_ATTR (attrs.at_decimal_sign);
6883 CHECKSUM_ATTR (attrs.at_default_value);
6884 CHECKSUM_ATTR (attrs.at_digit_count);
6885 CHECKSUM_ATTR (attrs.at_discr);
6886 CHECKSUM_ATTR (attrs.at_discr_list);
6887 CHECKSUM_ATTR (attrs.at_discr_value);
6888 CHECKSUM_ATTR (attrs.at_encoding);
6889 CHECKSUM_ATTR (attrs.at_endianity);
6890 CHECKSUM_ATTR (attrs.at_explicit);
6891 CHECKSUM_ATTR (attrs.at_is_optional);
6892 CHECKSUM_ATTR (attrs.at_location);
6893 CHECKSUM_ATTR (attrs.at_lower_bound);
6894 CHECKSUM_ATTR (attrs.at_mutable);
6895 CHECKSUM_ATTR (attrs.at_ordering);
6896 CHECKSUM_ATTR (attrs.at_picture_string);
6897 CHECKSUM_ATTR (attrs.at_prototyped);
6898 CHECKSUM_ATTR (attrs.at_small);
6899 CHECKSUM_ATTR (attrs.at_segment);
6900 CHECKSUM_ATTR (attrs.at_string_length);
6901 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6902 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6903 CHECKSUM_ATTR (attrs.at_threads_scaled);
6904 CHECKSUM_ATTR (attrs.at_upper_bound);
6905 CHECKSUM_ATTR (attrs.at_use_location);
6906 CHECKSUM_ATTR (attrs.at_use_UTF8);
6907 CHECKSUM_ATTR (attrs.at_variable_parameter);
6908 CHECKSUM_ATTR (attrs.at_virtuality);
6909 CHECKSUM_ATTR (attrs.at_visibility);
6910 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6911 CHECKSUM_ATTR (attrs.at_type);
6912 CHECKSUM_ATTR (attrs.at_friend);
6913 CHECKSUM_ATTR (attrs.at_alignment);
6915 /* Checksum the child DIEs. */
6916 c = die->die_child;
6917 if (c) do {
6918 dw_attr_node *name_attr;
6920 c = c->die_sib;
6921 name_attr = get_AT (c, DW_AT_name);
6922 if (is_template_instantiation (c))
6924 /* Ignore instantiations of member type and function templates. */
6926 else if (name_attr != NULL
6927 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6929 /* Use a shallow checksum for named nested types and member
6930 functions. */
6931 CHECKSUM_ULEB128 ('S');
6932 CHECKSUM_ULEB128 (c->die_tag);
6933 CHECKSUM_STRING (AT_string (name_attr));
6935 else
6937 /* Use a deep checksum for other children. */
6938 /* Mark this DIE so it gets processed when unmarking. */
6939 if (c->die_mark == 0)
6940 c->die_mark = -1;
6941 die_checksum_ordered (c, ctx, mark);
6943 } while (c != die->die_child);
6945 CHECKSUM_ULEB128 (0);
6948 /* Add a type name and tag to a hash. */
6949 static void
6950 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6952 CHECKSUM_ULEB128 (tag);
6953 CHECKSUM_STRING (name);
6956 #undef CHECKSUM
6957 #undef CHECKSUM_STRING
6958 #undef CHECKSUM_ATTR
6959 #undef CHECKSUM_LEB128
6960 #undef CHECKSUM_ULEB128
6962 /* Generate the type signature for DIE. This is computed by generating an
6963 MD5 checksum over the DIE's tag, its relevant attributes, and its
6964 children. Attributes that are references to other DIEs are processed
6965 by recursion, using the MARK field to prevent infinite recursion.
6966 If the DIE is nested inside a namespace or another type, we also
6967 need to include that context in the signature. The lower 64 bits
6968 of the resulting MD5 checksum comprise the signature. */
6970 static void
6971 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6973 int mark;
6974 const char *name;
6975 unsigned char checksum[16];
6976 struct md5_ctx ctx;
6977 dw_die_ref decl;
6978 dw_die_ref parent;
6980 name = get_AT_string (die, DW_AT_name);
6981 decl = get_AT_ref (die, DW_AT_specification);
6982 parent = get_die_parent (die);
6984 /* First, compute a signature for just the type name (and its surrounding
6985 context, if any. This is stored in the type unit DIE for link-time
6986 ODR (one-definition rule) checking. */
6988 if (is_cxx () && name != NULL)
6990 md5_init_ctx (&ctx);
6992 /* Checksum the names of surrounding namespaces and structures. */
6993 if (parent != NULL)
6994 checksum_die_context (parent, &ctx);
6996 /* Checksum the current DIE. */
6997 die_odr_checksum (die->die_tag, name, &ctx);
6998 md5_finish_ctx (&ctx, checksum);
7000 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7003 /* Next, compute the complete type signature. */
7005 md5_init_ctx (&ctx);
7006 mark = 1;
7007 die->die_mark = mark;
7009 /* Checksum the names of surrounding namespaces and structures. */
7010 if (parent != NULL)
7011 checksum_die_context (parent, &ctx);
7013 /* Checksum the DIE and its children. */
7014 die_checksum_ordered (die, &ctx, &mark);
7015 unmark_all_dies (die);
7016 md5_finish_ctx (&ctx, checksum);
7018 /* Store the signature in the type node and link the type DIE and the
7019 type node together. */
7020 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7021 DWARF_TYPE_SIGNATURE_SIZE);
7022 die->comdat_type_p = true;
7023 die->die_id.die_type_node = type_node;
7024 type_node->type_die = die;
7026 /* If the DIE is a specification, link its declaration to the type node
7027 as well. */
7028 if (decl != NULL)
7030 decl->comdat_type_p = true;
7031 decl->die_id.die_type_node = type_node;
7035 /* Do the location expressions look same? */
7036 static inline int
7037 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7039 return loc1->dw_loc_opc == loc2->dw_loc_opc
7040 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7041 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7044 /* Do the values look the same? */
7045 static int
7046 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7048 dw_loc_descr_ref loc1, loc2;
7049 rtx r1, r2;
7051 if (v1->val_class != v2->val_class)
7052 return 0;
7054 switch (v1->val_class)
7056 case dw_val_class_const:
7057 case dw_val_class_const_implicit:
7058 return v1->v.val_int == v2->v.val_int;
7059 case dw_val_class_unsigned_const:
7060 case dw_val_class_unsigned_const_implicit:
7061 return v1->v.val_unsigned == v2->v.val_unsigned;
7062 case dw_val_class_const_double:
7063 return v1->v.val_double.high == v2->v.val_double.high
7064 && v1->v.val_double.low == v2->v.val_double.low;
7065 case dw_val_class_wide_int:
7066 return *v1->v.val_wide == *v2->v.val_wide;
7067 case dw_val_class_vec:
7068 if (v1->v.val_vec.length != v2->v.val_vec.length
7069 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7070 return 0;
7071 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7072 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7073 return 0;
7074 return 1;
7075 case dw_val_class_flag:
7076 return v1->v.val_flag == v2->v.val_flag;
7077 case dw_val_class_str:
7078 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7080 case dw_val_class_addr:
7081 r1 = v1->v.val_addr;
7082 r2 = v2->v.val_addr;
7083 if (GET_CODE (r1) != GET_CODE (r2))
7084 return 0;
7085 return !rtx_equal_p (r1, r2);
7087 case dw_val_class_offset:
7088 return v1->v.val_offset == v2->v.val_offset;
7090 case dw_val_class_loc:
7091 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7092 loc1 && loc2;
7093 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7094 if (!same_loc_p (loc1, loc2, mark))
7095 return 0;
7096 return !loc1 && !loc2;
7098 case dw_val_class_die_ref:
7099 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7101 case dw_val_class_fde_ref:
7102 case dw_val_class_vms_delta:
7103 case dw_val_class_lbl_id:
7104 case dw_val_class_lineptr:
7105 case dw_val_class_macptr:
7106 case dw_val_class_loclistsptr:
7107 case dw_val_class_high_pc:
7108 return 1;
7110 case dw_val_class_file:
7111 case dw_val_class_file_implicit:
7112 return v1->v.val_file == v2->v.val_file;
7114 case dw_val_class_data8:
7115 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7117 default:
7118 return 1;
7122 /* Do the attributes look the same? */
7124 static int
7125 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7127 if (at1->dw_attr != at2->dw_attr)
7128 return 0;
7130 /* We don't care that this was compiled with a different compiler
7131 snapshot; if the output is the same, that's what matters. */
7132 if (at1->dw_attr == DW_AT_producer)
7133 return 1;
7135 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7138 /* Do the dies look the same? */
7140 static int
7141 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7143 dw_die_ref c1, c2;
7144 dw_attr_node *a1;
7145 unsigned ix;
7147 /* To avoid infinite recursion. */
7148 if (die1->die_mark)
7149 return die1->die_mark == die2->die_mark;
7150 die1->die_mark = die2->die_mark = ++(*mark);
7152 if (die1->die_tag != die2->die_tag)
7153 return 0;
7155 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7156 return 0;
7158 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7159 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7160 return 0;
7162 c1 = die1->die_child;
7163 c2 = die2->die_child;
7164 if (! c1)
7166 if (c2)
7167 return 0;
7169 else
7170 for (;;)
7172 if (!same_die_p (c1, c2, mark))
7173 return 0;
7174 c1 = c1->die_sib;
7175 c2 = c2->die_sib;
7176 if (c1 == die1->die_child)
7178 if (c2 == die2->die_child)
7179 break;
7180 else
7181 return 0;
7185 return 1;
7188 /* Do the dies look the same? Wrapper around same_die_p. */
7190 static int
7191 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7193 int mark = 0;
7194 int ret = same_die_p (die1, die2, &mark);
7196 unmark_all_dies (die1);
7197 unmark_all_dies (die2);
7199 return ret;
7202 /* The prefix to attach to symbols on DIEs in the current comdat debug
7203 info section. */
7204 static const char *comdat_symbol_id;
7206 /* The index of the current symbol within the current comdat CU. */
7207 static unsigned int comdat_symbol_number;
7209 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7210 children, and set comdat_symbol_id accordingly. */
7212 static void
7213 compute_section_prefix (dw_die_ref unit_die)
7215 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7216 const char *base = die_name ? lbasename (die_name) : "anonymous";
7217 char *name = XALLOCAVEC (char, strlen (base) + 64);
7218 char *p;
7219 int i, mark;
7220 unsigned char checksum[16];
7221 struct md5_ctx ctx;
7223 /* Compute the checksum of the DIE, then append part of it as hex digits to
7224 the name filename of the unit. */
7226 md5_init_ctx (&ctx);
7227 mark = 0;
7228 die_checksum (unit_die, &ctx, &mark);
7229 unmark_all_dies (unit_die);
7230 md5_finish_ctx (&ctx, checksum);
7232 sprintf (name, "%s.", base);
7233 clean_symbol_name (name);
7235 p = name + strlen (name);
7236 for (i = 0; i < 4; i++)
7238 sprintf (p, "%.2x", checksum[i]);
7239 p += 2;
7242 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7243 comdat_symbol_number = 0;
7246 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7248 static int
7249 is_type_die (dw_die_ref die)
7251 switch (die->die_tag)
7253 case DW_TAG_array_type:
7254 case DW_TAG_class_type:
7255 case DW_TAG_interface_type:
7256 case DW_TAG_enumeration_type:
7257 case DW_TAG_pointer_type:
7258 case DW_TAG_reference_type:
7259 case DW_TAG_rvalue_reference_type:
7260 case DW_TAG_string_type:
7261 case DW_TAG_structure_type:
7262 case DW_TAG_subroutine_type:
7263 case DW_TAG_union_type:
7264 case DW_TAG_ptr_to_member_type:
7265 case DW_TAG_set_type:
7266 case DW_TAG_subrange_type:
7267 case DW_TAG_base_type:
7268 case DW_TAG_const_type:
7269 case DW_TAG_file_type:
7270 case DW_TAG_packed_type:
7271 case DW_TAG_volatile_type:
7272 case DW_TAG_typedef:
7273 return 1;
7274 default:
7275 return 0;
7279 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7280 Basically, we want to choose the bits that are likely to be shared between
7281 compilations (types) and leave out the bits that are specific to individual
7282 compilations (functions). */
7284 static int
7285 is_comdat_die (dw_die_ref c)
7287 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7288 we do for stabs. The advantage is a greater likelihood of sharing between
7289 objects that don't include headers in the same order (and therefore would
7290 put the base types in a different comdat). jason 8/28/00 */
7292 if (c->die_tag == DW_TAG_base_type)
7293 return 0;
7295 if (c->die_tag == DW_TAG_pointer_type
7296 || c->die_tag == DW_TAG_reference_type
7297 || c->die_tag == DW_TAG_rvalue_reference_type
7298 || c->die_tag == DW_TAG_const_type
7299 || c->die_tag == DW_TAG_volatile_type)
7301 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7303 return t ? is_comdat_die (t) : 0;
7306 return is_type_die (c);
7309 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7310 compilation unit. */
7312 static int
7313 is_symbol_die (dw_die_ref c)
7315 return (is_type_die (c)
7316 || is_declaration_die (c)
7317 || c->die_tag == DW_TAG_namespace
7318 || c->die_tag == DW_TAG_module);
7321 /* Returns true iff C is a compile-unit DIE. */
7323 static inline bool
7324 is_cu_die (dw_die_ref c)
7326 return c && (c->die_tag == DW_TAG_compile_unit
7327 || c->die_tag == DW_TAG_skeleton_unit);
7330 /* Returns true iff C is a unit DIE of some sort. */
7332 static inline bool
7333 is_unit_die (dw_die_ref c)
7335 return c && (c->die_tag == DW_TAG_compile_unit
7336 || c->die_tag == DW_TAG_partial_unit
7337 || c->die_tag == DW_TAG_type_unit
7338 || c->die_tag == DW_TAG_skeleton_unit);
7341 /* Returns true iff C is a namespace DIE. */
7343 static inline bool
7344 is_namespace_die (dw_die_ref c)
7346 return c && c->die_tag == DW_TAG_namespace;
7349 /* Returns true iff C is a class or structure DIE. */
7351 static inline bool
7352 is_class_die (dw_die_ref c)
7354 return c && (c->die_tag == DW_TAG_class_type
7355 || c->die_tag == DW_TAG_structure_type);
7358 /* Return non-zero if this DIE is a template parameter. */
7360 static inline bool
7361 is_template_parameter (dw_die_ref die)
7363 switch (die->die_tag)
7365 case DW_TAG_template_type_param:
7366 case DW_TAG_template_value_param:
7367 case DW_TAG_GNU_template_template_param:
7368 case DW_TAG_GNU_template_parameter_pack:
7369 return true;
7370 default:
7371 return false;
7375 /* Return non-zero if this DIE represents a template instantiation. */
7377 static inline bool
7378 is_template_instantiation (dw_die_ref die)
7380 dw_die_ref c;
7382 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7383 return false;
7384 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7385 return false;
7388 static char *
7389 gen_internal_sym (const char *prefix)
7391 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7393 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7394 return xstrdup (buf);
7397 /* Assign symbols to all worthy DIEs under DIE. */
7399 static void
7400 assign_symbol_names (dw_die_ref die)
7402 dw_die_ref c;
7404 if (is_symbol_die (die) && !die->comdat_type_p)
7406 if (comdat_symbol_id)
7408 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7410 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7411 comdat_symbol_id, comdat_symbol_number++);
7412 die->die_id.die_symbol = xstrdup (p);
7414 else
7415 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7418 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7421 struct cu_hash_table_entry
7423 dw_die_ref cu;
7424 unsigned min_comdat_num, max_comdat_num;
7425 struct cu_hash_table_entry *next;
7428 /* Helpers to manipulate hash table of CUs. */
7430 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7432 typedef die_struct *compare_type;
7433 static inline hashval_t hash (const cu_hash_table_entry *);
7434 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7435 static inline void remove (cu_hash_table_entry *);
7438 inline hashval_t
7439 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7441 return htab_hash_string (entry->cu->die_id.die_symbol);
7444 inline bool
7445 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7446 const die_struct *entry2)
7448 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7451 inline void
7452 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7454 struct cu_hash_table_entry *next;
7456 while (entry)
7458 next = entry->next;
7459 free (entry);
7460 entry = next;
7464 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7466 /* Check whether we have already seen this CU and set up SYM_NUM
7467 accordingly. */
7468 static int
7469 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7471 struct cu_hash_table_entry dummy;
7472 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7474 dummy.max_comdat_num = 0;
7476 slot = htable->find_slot_with_hash (cu,
7477 htab_hash_string (cu->die_id.die_symbol),
7478 INSERT);
7479 entry = *slot;
7481 for (; entry; last = entry, entry = entry->next)
7483 if (same_die_p_wrap (cu, entry->cu))
7484 break;
7487 if (entry)
7489 *sym_num = entry->min_comdat_num;
7490 return 1;
7493 entry = XCNEW (struct cu_hash_table_entry);
7494 entry->cu = cu;
7495 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7496 entry->next = *slot;
7497 *slot = entry;
7499 return 0;
7502 /* Record SYM_NUM to record of CU in HTABLE. */
7503 static void
7504 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7505 unsigned int sym_num)
7507 struct cu_hash_table_entry **slot, *entry;
7509 slot = htable->find_slot_with_hash (cu,
7510 htab_hash_string (cu->die_id.die_symbol),
7511 NO_INSERT);
7512 entry = *slot;
7514 entry->max_comdat_num = sym_num;
7517 /* Traverse the DIE (which is always comp_unit_die), and set up
7518 additional compilation units for each of the include files we see
7519 bracketed by BINCL/EINCL. */
7521 static void
7522 break_out_includes (dw_die_ref die)
7524 dw_die_ref c;
7525 dw_die_ref unit = NULL;
7526 limbo_die_node *node, **pnode;
7528 c = die->die_child;
7529 if (c) do {
7530 dw_die_ref prev = c;
7531 c = c->die_sib;
7532 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7533 || (unit && is_comdat_die (c)))
7535 dw_die_ref next = c->die_sib;
7537 /* This DIE is for a secondary CU; remove it from the main one. */
7538 remove_child_with_prev (c, prev);
7540 if (c->die_tag == DW_TAG_GNU_BINCL)
7541 unit = push_new_compile_unit (unit, c);
7542 else if (c->die_tag == DW_TAG_GNU_EINCL)
7543 unit = pop_compile_unit (unit);
7544 else
7545 add_child_die (unit, c);
7546 c = next;
7547 if (c == die->die_child)
7548 break;
7550 } while (c != die->die_child);
7552 #if 0
7553 /* We can only use this in debugging, since the frontend doesn't check
7554 to make sure that we leave every include file we enter. */
7555 gcc_assert (!unit);
7556 #endif
7558 assign_symbol_names (die);
7559 cu_hash_type cu_hash_table (10);
7560 for (node = limbo_die_list, pnode = &limbo_die_list;
7561 node;
7562 node = node->next)
7564 int is_dupl;
7566 compute_section_prefix (node->die);
7567 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7568 &comdat_symbol_number);
7569 assign_symbol_names (node->die);
7570 if (is_dupl)
7571 *pnode = node->next;
7572 else
7574 pnode = &node->next;
7575 record_comdat_symbol_number (node->die, &cu_hash_table,
7576 comdat_symbol_number);
7581 /* Return non-zero if this DIE is a declaration. */
7583 static int
7584 is_declaration_die (dw_die_ref die)
7586 dw_attr_node *a;
7587 unsigned ix;
7589 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7590 if (a->dw_attr == DW_AT_declaration)
7591 return 1;
7593 return 0;
7596 /* Return non-zero if this DIE is nested inside a subprogram. */
7598 static int
7599 is_nested_in_subprogram (dw_die_ref die)
7601 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7603 if (decl == NULL)
7604 decl = die;
7605 return local_scope_p (decl);
7608 /* Return non-zero if this DIE contains a defining declaration of a
7609 subprogram. */
7611 static int
7612 contains_subprogram_definition (dw_die_ref die)
7614 dw_die_ref c;
7616 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7617 return 1;
7618 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7619 return 0;
7622 /* Return non-zero if this is a type DIE that should be moved to a
7623 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7624 unit type. */
7626 static int
7627 should_move_die_to_comdat (dw_die_ref die)
7629 switch (die->die_tag)
7631 case DW_TAG_class_type:
7632 case DW_TAG_structure_type:
7633 case DW_TAG_enumeration_type:
7634 case DW_TAG_union_type:
7635 /* Don't move declarations, inlined instances, types nested in a
7636 subprogram, or types that contain subprogram definitions. */
7637 if (is_declaration_die (die)
7638 || get_AT (die, DW_AT_abstract_origin)
7639 || is_nested_in_subprogram (die)
7640 || contains_subprogram_definition (die))
7641 return 0;
7642 return 1;
7643 case DW_TAG_array_type:
7644 case DW_TAG_interface_type:
7645 case DW_TAG_pointer_type:
7646 case DW_TAG_reference_type:
7647 case DW_TAG_rvalue_reference_type:
7648 case DW_TAG_string_type:
7649 case DW_TAG_subroutine_type:
7650 case DW_TAG_ptr_to_member_type:
7651 case DW_TAG_set_type:
7652 case DW_TAG_subrange_type:
7653 case DW_TAG_base_type:
7654 case DW_TAG_const_type:
7655 case DW_TAG_file_type:
7656 case DW_TAG_packed_type:
7657 case DW_TAG_volatile_type:
7658 case DW_TAG_typedef:
7659 default:
7660 return 0;
7664 /* Make a clone of DIE. */
7666 static dw_die_ref
7667 clone_die (dw_die_ref die)
7669 dw_die_ref clone;
7670 dw_attr_node *a;
7671 unsigned ix;
7673 clone = ggc_cleared_alloc<die_node> ();
7674 clone->die_tag = die->die_tag;
7676 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7677 add_dwarf_attr (clone, a);
7679 return clone;
7682 /* Make a clone of the tree rooted at DIE. */
7684 static dw_die_ref
7685 clone_tree (dw_die_ref die)
7687 dw_die_ref c;
7688 dw_die_ref clone = clone_die (die);
7690 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7692 return clone;
7695 /* Make a clone of DIE as a declaration. */
7697 static dw_die_ref
7698 clone_as_declaration (dw_die_ref die)
7700 dw_die_ref clone;
7701 dw_die_ref decl;
7702 dw_attr_node *a;
7703 unsigned ix;
7705 /* If the DIE is already a declaration, just clone it. */
7706 if (is_declaration_die (die))
7707 return clone_die (die);
7709 /* If the DIE is a specification, just clone its declaration DIE. */
7710 decl = get_AT_ref (die, DW_AT_specification);
7711 if (decl != NULL)
7713 clone = clone_die (decl);
7714 if (die->comdat_type_p)
7715 add_AT_die_ref (clone, DW_AT_signature, die);
7716 return clone;
7719 clone = ggc_cleared_alloc<die_node> ();
7720 clone->die_tag = die->die_tag;
7722 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7724 /* We don't want to copy over all attributes.
7725 For example we don't want DW_AT_byte_size because otherwise we will no
7726 longer have a declaration and GDB will treat it as a definition. */
7728 switch (a->dw_attr)
7730 case DW_AT_abstract_origin:
7731 case DW_AT_artificial:
7732 case DW_AT_containing_type:
7733 case DW_AT_external:
7734 case DW_AT_name:
7735 case DW_AT_type:
7736 case DW_AT_virtuality:
7737 case DW_AT_linkage_name:
7738 case DW_AT_MIPS_linkage_name:
7739 add_dwarf_attr (clone, a);
7740 break;
7741 case DW_AT_byte_size:
7742 case DW_AT_alignment:
7743 default:
7744 break;
7748 if (die->comdat_type_p)
7749 add_AT_die_ref (clone, DW_AT_signature, die);
7751 add_AT_flag (clone, DW_AT_declaration, 1);
7752 return clone;
7756 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7758 struct decl_table_entry
7760 dw_die_ref orig;
7761 dw_die_ref copy;
7764 /* Helpers to manipulate hash table of copied declarations. */
7766 /* Hashtable helpers. */
7768 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7770 typedef die_struct *compare_type;
7771 static inline hashval_t hash (const decl_table_entry *);
7772 static inline bool equal (const decl_table_entry *, const die_struct *);
7775 inline hashval_t
7776 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7778 return htab_hash_pointer (entry->orig);
7781 inline bool
7782 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7783 const die_struct *entry2)
7785 return entry1->orig == entry2;
7788 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7790 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7791 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7792 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7793 to check if the ancestor has already been copied into UNIT. */
7795 static dw_die_ref
7796 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7797 decl_hash_type *decl_table)
7799 dw_die_ref parent = die->die_parent;
7800 dw_die_ref new_parent = unit;
7801 dw_die_ref copy;
7802 decl_table_entry **slot = NULL;
7803 struct decl_table_entry *entry = NULL;
7805 if (decl_table)
7807 /* Check if the entry has already been copied to UNIT. */
7808 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7809 INSERT);
7810 if (*slot != HTAB_EMPTY_ENTRY)
7812 entry = *slot;
7813 return entry->copy;
7816 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7817 entry = XCNEW (struct decl_table_entry);
7818 entry->orig = die;
7819 entry->copy = NULL;
7820 *slot = entry;
7823 if (parent != NULL)
7825 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7826 if (spec != NULL)
7827 parent = spec;
7828 if (!is_unit_die (parent))
7829 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7832 copy = clone_as_declaration (die);
7833 add_child_die (new_parent, copy);
7835 if (decl_table)
7837 /* Record the pointer to the copy. */
7838 entry->copy = copy;
7841 return copy;
7843 /* Copy the declaration context to the new type unit DIE. This includes
7844 any surrounding namespace or type declarations. If the DIE has an
7845 AT_specification attribute, it also includes attributes and children
7846 attached to the specification, and returns a pointer to the original
7847 parent of the declaration DIE. Returns NULL otherwise. */
7849 static dw_die_ref
7850 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7852 dw_die_ref decl;
7853 dw_die_ref new_decl;
7854 dw_die_ref orig_parent = NULL;
7856 decl = get_AT_ref (die, DW_AT_specification);
7857 if (decl == NULL)
7858 decl = die;
7859 else
7861 unsigned ix;
7862 dw_die_ref c;
7863 dw_attr_node *a;
7865 /* The original DIE will be changed to a declaration, and must
7866 be moved to be a child of the original declaration DIE. */
7867 orig_parent = decl->die_parent;
7869 /* Copy the type node pointer from the new DIE to the original
7870 declaration DIE so we can forward references later. */
7871 decl->comdat_type_p = true;
7872 decl->die_id.die_type_node = die->die_id.die_type_node;
7874 remove_AT (die, DW_AT_specification);
7876 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7878 if (a->dw_attr != DW_AT_name
7879 && a->dw_attr != DW_AT_declaration
7880 && a->dw_attr != DW_AT_external)
7881 add_dwarf_attr (die, a);
7884 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7887 if (decl->die_parent != NULL
7888 && !is_unit_die (decl->die_parent))
7890 new_decl = copy_ancestor_tree (unit, decl, NULL);
7891 if (new_decl != NULL)
7893 remove_AT (new_decl, DW_AT_signature);
7894 add_AT_specification (die, new_decl);
7898 return orig_parent;
7901 /* Generate the skeleton ancestor tree for the given NODE, then clone
7902 the DIE and add the clone into the tree. */
7904 static void
7905 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7907 if (node->new_die != NULL)
7908 return;
7910 node->new_die = clone_as_declaration (node->old_die);
7912 if (node->parent != NULL)
7914 generate_skeleton_ancestor_tree (node->parent);
7915 add_child_die (node->parent->new_die, node->new_die);
7919 /* Generate a skeleton tree of DIEs containing any declarations that are
7920 found in the original tree. We traverse the tree looking for declaration
7921 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7923 static void
7924 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7926 skeleton_chain_node node;
7927 dw_die_ref c;
7928 dw_die_ref first;
7929 dw_die_ref prev = NULL;
7930 dw_die_ref next = NULL;
7932 node.parent = parent;
7934 first = c = parent->old_die->die_child;
7935 if (c)
7936 next = c->die_sib;
7937 if (c) do {
7938 if (prev == NULL || prev->die_sib == c)
7939 prev = c;
7940 c = next;
7941 next = (c == first ? NULL : c->die_sib);
7942 node.old_die = c;
7943 node.new_die = NULL;
7944 if (is_declaration_die (c))
7946 if (is_template_instantiation (c))
7948 /* Instantiated templates do not need to be cloned into the
7949 type unit. Just move the DIE and its children back to
7950 the skeleton tree (in the main CU). */
7951 remove_child_with_prev (c, prev);
7952 add_child_die (parent->new_die, c);
7953 c = prev;
7955 else if (c->comdat_type_p)
7957 /* This is the skeleton of earlier break_out_comdat_types
7958 type. Clone the existing DIE, but keep the children
7959 under the original (which is in the main CU). */
7960 dw_die_ref clone = clone_die (c);
7962 replace_child (c, clone, prev);
7963 generate_skeleton_ancestor_tree (parent);
7964 add_child_die (parent->new_die, c);
7965 c = clone;
7966 continue;
7968 else
7970 /* Clone the existing DIE, move the original to the skeleton
7971 tree (which is in the main CU), and put the clone, with
7972 all the original's children, where the original came from
7973 (which is about to be moved to the type unit). */
7974 dw_die_ref clone = clone_die (c);
7975 move_all_children (c, clone);
7977 /* If the original has a DW_AT_object_pointer attribute,
7978 it would now point to a child DIE just moved to the
7979 cloned tree, so we need to remove that attribute from
7980 the original. */
7981 remove_AT (c, DW_AT_object_pointer);
7983 replace_child (c, clone, prev);
7984 generate_skeleton_ancestor_tree (parent);
7985 add_child_die (parent->new_die, c);
7986 node.old_die = clone;
7987 node.new_die = c;
7988 c = clone;
7991 generate_skeleton_bottom_up (&node);
7992 } while (next != NULL);
7995 /* Wrapper function for generate_skeleton_bottom_up. */
7997 static dw_die_ref
7998 generate_skeleton (dw_die_ref die)
8000 skeleton_chain_node node;
8002 node.old_die = die;
8003 node.new_die = NULL;
8004 node.parent = NULL;
8006 /* If this type definition is nested inside another type,
8007 and is not an instantiation of a template, always leave
8008 at least a declaration in its place. */
8009 if (die->die_parent != NULL
8010 && is_type_die (die->die_parent)
8011 && !is_template_instantiation (die))
8012 node.new_die = clone_as_declaration (die);
8014 generate_skeleton_bottom_up (&node);
8015 return node.new_die;
8018 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8019 declaration. The original DIE is moved to a new compile unit so that
8020 existing references to it follow it to the new location. If any of the
8021 original DIE's descendants is a declaration, we need to replace the
8022 original DIE with a skeleton tree and move the declarations back into the
8023 skeleton tree. */
8025 static dw_die_ref
8026 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8027 dw_die_ref prev)
8029 dw_die_ref skeleton, orig_parent;
8031 /* Copy the declaration context to the type unit DIE. If the returned
8032 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8033 that DIE. */
8034 orig_parent = copy_declaration_context (unit, child);
8036 skeleton = generate_skeleton (child);
8037 if (skeleton == NULL)
8038 remove_child_with_prev (child, prev);
8039 else
8041 skeleton->comdat_type_p = true;
8042 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8044 /* If the original DIE was a specification, we need to put
8045 the skeleton under the parent DIE of the declaration.
8046 This leaves the original declaration in the tree, but
8047 it will be pruned later since there are no longer any
8048 references to it. */
8049 if (orig_parent != NULL)
8051 remove_child_with_prev (child, prev);
8052 add_child_die (orig_parent, skeleton);
8054 else
8055 replace_child (child, skeleton, prev);
8058 return skeleton;
8061 static void
8062 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8063 comdat_type_node *type_node,
8064 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8066 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8067 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8068 DWARF procedure references in the DW_AT_location attribute. */
8070 static dw_die_ref
8071 copy_dwarf_procedure (dw_die_ref die,
8072 comdat_type_node *type_node,
8073 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8075 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8077 /* DWARF procedures are not supposed to have children... */
8078 gcc_assert (die->die_child == NULL);
8080 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8081 gcc_assert (vec_safe_length (die->die_attr) == 1
8082 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8084 /* Do not copy more than once DWARF procedures. */
8085 bool existed;
8086 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8087 if (existed)
8088 return die_copy;
8090 die_copy = clone_die (die);
8091 add_child_die (type_node->root_die, die_copy);
8092 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8093 return die_copy;
8096 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8097 procedures in DIE's attributes. */
8099 static void
8100 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8101 comdat_type_node *type_node,
8102 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8104 dw_attr_node *a;
8105 unsigned i;
8107 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8109 dw_loc_descr_ref loc;
8111 if (a->dw_attr_val.val_class != dw_val_class_loc)
8112 continue;
8114 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8116 switch (loc->dw_loc_opc)
8118 case DW_OP_call2:
8119 case DW_OP_call4:
8120 case DW_OP_call_ref:
8121 gcc_assert (loc->dw_loc_oprnd1.val_class
8122 == dw_val_class_die_ref);
8123 loc->dw_loc_oprnd1.v.val_die_ref.die
8124 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8125 type_node,
8126 copied_dwarf_procs);
8128 default:
8129 break;
8135 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8136 rewrite references to point to the copies.
8138 References are looked for in DIE's attributes and recursively in all its
8139 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8140 mapping from old DWARF procedures to their copy. It is used not to copy
8141 twice the same DWARF procedure under TYPE_NODE. */
8143 static void
8144 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8145 comdat_type_node *type_node,
8146 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8148 dw_die_ref c;
8150 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8151 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8152 type_node,
8153 copied_dwarf_procs));
8156 /* Traverse the DIE and set up additional .debug_types or .debug_info
8157 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8158 section. */
8160 static void
8161 break_out_comdat_types (dw_die_ref die)
8163 dw_die_ref c;
8164 dw_die_ref first;
8165 dw_die_ref prev = NULL;
8166 dw_die_ref next = NULL;
8167 dw_die_ref unit = NULL;
8169 first = c = die->die_child;
8170 if (c)
8171 next = c->die_sib;
8172 if (c) do {
8173 if (prev == NULL || prev->die_sib == c)
8174 prev = c;
8175 c = next;
8176 next = (c == first ? NULL : c->die_sib);
8177 if (should_move_die_to_comdat (c))
8179 dw_die_ref replacement;
8180 comdat_type_node *type_node;
8182 /* Break out nested types into their own type units. */
8183 break_out_comdat_types (c);
8185 /* Create a new type unit DIE as the root for the new tree, and
8186 add it to the list of comdat types. */
8187 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8188 add_AT_unsigned (unit, DW_AT_language,
8189 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8190 type_node = ggc_cleared_alloc<comdat_type_node> ();
8191 type_node->root_die = unit;
8192 type_node->next = comdat_type_list;
8193 comdat_type_list = type_node;
8195 /* Generate the type signature. */
8196 generate_type_signature (c, type_node);
8198 /* Copy the declaration context, attributes, and children of the
8199 declaration into the new type unit DIE, then remove this DIE
8200 from the main CU (or replace it with a skeleton if necessary). */
8201 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8202 type_node->skeleton_die = replacement;
8204 /* Add the DIE to the new compunit. */
8205 add_child_die (unit, c);
8207 /* Types can reference DWARF procedures for type size or data location
8208 expressions. Calls in DWARF expressions cannot target procedures
8209 that are not in the same section. So we must copy DWARF procedures
8210 along with this type and then rewrite references to them. */
8211 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8212 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8214 if (replacement != NULL)
8215 c = replacement;
8217 else if (c->die_tag == DW_TAG_namespace
8218 || c->die_tag == DW_TAG_class_type
8219 || c->die_tag == DW_TAG_structure_type
8220 || c->die_tag == DW_TAG_union_type)
8222 /* Look for nested types that can be broken out. */
8223 break_out_comdat_types (c);
8225 } while (next != NULL);
8228 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8229 Enter all the cloned children into the hash table decl_table. */
8231 static dw_die_ref
8232 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8234 dw_die_ref c;
8235 dw_die_ref clone;
8236 struct decl_table_entry *entry;
8237 decl_table_entry **slot;
8239 if (die->die_tag == DW_TAG_subprogram)
8240 clone = clone_as_declaration (die);
8241 else
8242 clone = clone_die (die);
8244 slot = decl_table->find_slot_with_hash (die,
8245 htab_hash_pointer (die), INSERT);
8247 /* Assert that DIE isn't in the hash table yet. If it would be there
8248 before, the ancestors would be necessarily there as well, therefore
8249 clone_tree_partial wouldn't be called. */
8250 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8252 entry = XCNEW (struct decl_table_entry);
8253 entry->orig = die;
8254 entry->copy = clone;
8255 *slot = entry;
8257 if (die->die_tag != DW_TAG_subprogram)
8258 FOR_EACH_CHILD (die, c,
8259 add_child_die (clone, clone_tree_partial (c, decl_table)));
8261 return clone;
8264 /* Walk the DIE and its children, looking for references to incomplete
8265 or trivial types that are unmarked (i.e., that are not in the current
8266 type_unit). */
8268 static void
8269 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8271 dw_die_ref c;
8272 dw_attr_node *a;
8273 unsigned ix;
8275 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8277 if (AT_class (a) == dw_val_class_die_ref)
8279 dw_die_ref targ = AT_ref (a);
8280 decl_table_entry **slot;
8281 struct decl_table_entry *entry;
8283 if (targ->die_mark != 0 || targ->comdat_type_p)
8284 continue;
8286 slot = decl_table->find_slot_with_hash (targ,
8287 htab_hash_pointer (targ),
8288 INSERT);
8290 if (*slot != HTAB_EMPTY_ENTRY)
8292 /* TARG has already been copied, so we just need to
8293 modify the reference to point to the copy. */
8294 entry = *slot;
8295 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8297 else
8299 dw_die_ref parent = unit;
8300 dw_die_ref copy = clone_die (targ);
8302 /* Record in DECL_TABLE that TARG has been copied.
8303 Need to do this now, before the recursive call,
8304 because DECL_TABLE may be expanded and SLOT
8305 would no longer be a valid pointer. */
8306 entry = XCNEW (struct decl_table_entry);
8307 entry->orig = targ;
8308 entry->copy = copy;
8309 *slot = entry;
8311 /* If TARG is not a declaration DIE, we need to copy its
8312 children. */
8313 if (!is_declaration_die (targ))
8315 FOR_EACH_CHILD (
8316 targ, c,
8317 add_child_die (copy,
8318 clone_tree_partial (c, decl_table)));
8321 /* Make sure the cloned tree is marked as part of the
8322 type unit. */
8323 mark_dies (copy);
8325 /* If TARG has surrounding context, copy its ancestor tree
8326 into the new type unit. */
8327 if (targ->die_parent != NULL
8328 && !is_unit_die (targ->die_parent))
8329 parent = copy_ancestor_tree (unit, targ->die_parent,
8330 decl_table);
8332 add_child_die (parent, copy);
8333 a->dw_attr_val.v.val_die_ref.die = copy;
8335 /* Make sure the newly-copied DIE is walked. If it was
8336 installed in a previously-added context, it won't
8337 get visited otherwise. */
8338 if (parent != unit)
8340 /* Find the highest point of the newly-added tree,
8341 mark each node along the way, and walk from there. */
8342 parent->die_mark = 1;
8343 while (parent->die_parent
8344 && parent->die_parent->die_mark == 0)
8346 parent = parent->die_parent;
8347 parent->die_mark = 1;
8349 copy_decls_walk (unit, parent, decl_table);
8355 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8358 /* Copy declarations for "unworthy" types into the new comdat section.
8359 Incomplete types, modified types, and certain other types aren't broken
8360 out into comdat sections of their own, so they don't have a signature,
8361 and we need to copy the declaration into the same section so that we
8362 don't have an external reference. */
8364 static void
8365 copy_decls_for_unworthy_types (dw_die_ref unit)
8367 mark_dies (unit);
8368 decl_hash_type decl_table (10);
8369 copy_decls_walk (unit, unit, &decl_table);
8370 unmark_dies (unit);
8373 /* Traverse the DIE and add a sibling attribute if it may have the
8374 effect of speeding up access to siblings. To save some space,
8375 avoid generating sibling attributes for DIE's without children. */
8377 static void
8378 add_sibling_attributes (dw_die_ref die)
8380 dw_die_ref c;
8382 if (! die->die_child)
8383 return;
8385 if (die->die_parent && die != die->die_parent->die_child)
8386 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8388 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8391 /* Output all location lists for the DIE and its children. */
8393 static void
8394 output_location_lists (dw_die_ref die)
8396 dw_die_ref c;
8397 dw_attr_node *a;
8398 unsigned ix;
8400 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8401 if (AT_class (a) == dw_val_class_loc_list)
8402 output_loc_list (AT_loc_list (a));
8404 FOR_EACH_CHILD (die, c, output_location_lists (c));
8407 /* During assign_location_list_indexes and output_loclists_offset the
8408 current index, after it the number of assigned indexes (i.e. how
8409 large the .debug_loclists* offset table should be). */
8410 static unsigned int loc_list_idx;
8412 /* Output all location list offsets for the DIE and its children. */
8414 static void
8415 output_loclists_offsets (dw_die_ref die)
8417 dw_die_ref c;
8418 dw_attr_node *a;
8419 unsigned ix;
8421 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8422 if (AT_class (a) == dw_val_class_loc_list)
8424 dw_loc_list_ref l = AT_loc_list (a);
8425 if (l->offset_emitted)
8426 continue;
8427 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8428 loc_section_label, NULL);
8429 gcc_assert (l->hash == loc_list_idx);
8430 loc_list_idx++;
8431 l->offset_emitted = true;
8434 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8437 /* Recursively set indexes of location lists. */
8439 static void
8440 assign_location_list_indexes (dw_die_ref die)
8442 dw_die_ref c;
8443 dw_attr_node *a;
8444 unsigned ix;
8446 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8447 if (AT_class (a) == dw_val_class_loc_list)
8449 dw_loc_list_ref list = AT_loc_list (a);
8450 if (!list->num_assigned)
8452 list->num_assigned = true;
8453 list->hash = loc_list_idx++;
8457 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8460 /* We want to limit the number of external references, because they are
8461 larger than local references: a relocation takes multiple words, and
8462 even a sig8 reference is always eight bytes, whereas a local reference
8463 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8464 So if we encounter multiple external references to the same type DIE, we
8465 make a local typedef stub for it and redirect all references there.
8467 This is the element of the hash table for keeping track of these
8468 references. */
8470 struct external_ref
8472 dw_die_ref type;
8473 dw_die_ref stub;
8474 unsigned n_refs;
8477 /* Hashtable helpers. */
8479 struct external_ref_hasher : free_ptr_hash <external_ref>
8481 static inline hashval_t hash (const external_ref *);
8482 static inline bool equal (const external_ref *, const external_ref *);
8485 inline hashval_t
8486 external_ref_hasher::hash (const external_ref *r)
8488 dw_die_ref die = r->type;
8489 hashval_t h = 0;
8491 /* We can't use the address of the DIE for hashing, because
8492 that will make the order of the stub DIEs non-deterministic. */
8493 if (! die->comdat_type_p)
8494 /* We have a symbol; use it to compute a hash. */
8495 h = htab_hash_string (die->die_id.die_symbol);
8496 else
8498 /* We have a type signature; use a subset of the bits as the hash.
8499 The 8-byte signature is at least as large as hashval_t. */
8500 comdat_type_node *type_node = die->die_id.die_type_node;
8501 memcpy (&h, type_node->signature, sizeof (h));
8503 return h;
8506 inline bool
8507 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8509 return r1->type == r2->type;
8512 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8514 /* Return a pointer to the external_ref for references to DIE. */
8516 static struct external_ref *
8517 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8519 struct external_ref ref, *ref_p;
8520 external_ref **slot;
8522 ref.type = die;
8523 slot = map->find_slot (&ref, INSERT);
8524 if (*slot != HTAB_EMPTY_ENTRY)
8525 return *slot;
8527 ref_p = XCNEW (struct external_ref);
8528 ref_p->type = die;
8529 *slot = ref_p;
8530 return ref_p;
8533 /* Subroutine of optimize_external_refs, below.
8535 If we see a type skeleton, record it as our stub. If we see external
8536 references, remember how many we've seen. */
8538 static void
8539 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8541 dw_die_ref c;
8542 dw_attr_node *a;
8543 unsigned ix;
8544 struct external_ref *ref_p;
8546 if (is_type_die (die)
8547 && (c = get_AT_ref (die, DW_AT_signature)))
8549 /* This is a local skeleton; use it for local references. */
8550 ref_p = lookup_external_ref (map, c);
8551 ref_p->stub = die;
8554 /* Scan the DIE references, and remember any that refer to DIEs from
8555 other CUs (i.e. those which are not marked). */
8556 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8557 if (AT_class (a) == dw_val_class_die_ref
8558 && (c = AT_ref (a))->die_mark == 0
8559 && is_type_die (c))
8561 ref_p = lookup_external_ref (map, c);
8562 ref_p->n_refs++;
8565 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8568 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8569 points to an external_ref, DATA is the CU we're processing. If we don't
8570 already have a local stub, and we have multiple refs, build a stub. */
8573 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8575 struct external_ref *ref_p = *slot;
8577 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8579 /* We have multiple references to this type, so build a small stub.
8580 Both of these forms are a bit dodgy from the perspective of the
8581 DWARF standard, since technically they should have names. */
8582 dw_die_ref cu = data;
8583 dw_die_ref type = ref_p->type;
8584 dw_die_ref stub = NULL;
8586 if (type->comdat_type_p)
8588 /* If we refer to this type via sig8, use AT_signature. */
8589 stub = new_die (type->die_tag, cu, NULL_TREE);
8590 add_AT_die_ref (stub, DW_AT_signature, type);
8592 else
8594 /* Otherwise, use a typedef with no name. */
8595 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8596 add_AT_die_ref (stub, DW_AT_type, type);
8599 stub->die_mark++;
8600 ref_p->stub = stub;
8602 return 1;
8605 /* DIE is a unit; look through all the DIE references to see if there are
8606 any external references to types, and if so, create local stubs for
8607 them which will be applied in build_abbrev_table. This is useful because
8608 references to local DIEs are smaller. */
8610 static external_ref_hash_type *
8611 optimize_external_refs (dw_die_ref die)
8613 external_ref_hash_type *map = new external_ref_hash_type (10);
8614 optimize_external_refs_1 (die, map);
8615 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8616 return map;
8619 /* The following 3 variables are temporaries that are computed only during the
8620 build_abbrev_table call and used and released during the following
8621 optimize_abbrev_table call. */
8623 /* First abbrev_id that can be optimized based on usage. */
8624 static unsigned int abbrev_opt_start;
8626 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8627 abbrev_id smaller than this, because they must be already sized
8628 during build_abbrev_table). */
8629 static unsigned int abbrev_opt_base_type_end;
8631 /* Vector of usage counts during build_abbrev_table. Indexed by
8632 abbrev_id - abbrev_opt_start. */
8633 static vec<unsigned int> abbrev_usage_count;
8635 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8636 static vec<dw_die_ref> sorted_abbrev_dies;
8638 /* The format of each DIE (and its attribute value pairs) is encoded in an
8639 abbreviation table. This routine builds the abbreviation table and assigns
8640 a unique abbreviation id for each abbreviation entry. The children of each
8641 die are visited recursively. */
8643 static void
8644 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8646 unsigned int abbrev_id = 0;
8647 dw_die_ref c;
8648 dw_attr_node *a;
8649 unsigned ix;
8650 dw_die_ref abbrev;
8652 /* Scan the DIE references, and replace any that refer to
8653 DIEs from other CUs (i.e. those which are not marked) with
8654 the local stubs we built in optimize_external_refs. */
8655 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8656 if (AT_class (a) == dw_val_class_die_ref
8657 && (c = AT_ref (a))->die_mark == 0)
8659 struct external_ref *ref_p;
8660 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8662 ref_p = lookup_external_ref (extern_map, c);
8663 if (ref_p->stub && ref_p->stub != die)
8664 change_AT_die_ref (a, ref_p->stub);
8665 else
8666 /* We aren't changing this reference, so mark it external. */
8667 set_AT_ref_external (a, 1);
8670 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8672 dw_attr_node *die_a, *abbrev_a;
8673 unsigned ix;
8674 bool ok = true;
8676 if (abbrev_id == 0)
8677 continue;
8678 if (abbrev->die_tag != die->die_tag)
8679 continue;
8680 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8681 continue;
8683 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8684 continue;
8686 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8688 abbrev_a = &(*abbrev->die_attr)[ix];
8689 if ((abbrev_a->dw_attr != die_a->dw_attr)
8690 || (value_format (abbrev_a) != value_format (die_a)))
8692 ok = false;
8693 break;
8696 if (ok)
8697 break;
8700 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8702 vec_safe_push (abbrev_die_table, die);
8703 if (abbrev_opt_start)
8704 abbrev_usage_count.safe_push (0);
8706 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8708 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8709 sorted_abbrev_dies.safe_push (die);
8712 die->die_abbrev = abbrev_id;
8713 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8716 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8717 by die_abbrev's usage count, from the most commonly used
8718 abbreviation to the least. */
8720 static int
8721 die_abbrev_cmp (const void *p1, const void *p2)
8723 dw_die_ref die1 = *(const dw_die_ref *) p1;
8724 dw_die_ref die2 = *(const dw_die_ref *) p2;
8726 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8727 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8729 if (die1->die_abbrev >= abbrev_opt_base_type_end
8730 && die2->die_abbrev >= abbrev_opt_base_type_end)
8732 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8733 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8734 return -1;
8735 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8736 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8737 return 1;
8740 /* Stabilize the sort. */
8741 if (die1->die_abbrev < die2->die_abbrev)
8742 return -1;
8743 if (die1->die_abbrev > die2->die_abbrev)
8744 return 1;
8746 return 0;
8749 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8750 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8751 into dw_val_class_const_implicit or
8752 dw_val_class_unsigned_const_implicit. */
8754 static void
8755 optimize_implicit_const (unsigned int first_id, unsigned int end,
8756 vec<bool> &implicit_consts)
8758 /* It never makes sense if there is just one DIE using the abbreviation. */
8759 if (end < first_id + 2)
8760 return;
8762 dw_attr_node *a;
8763 unsigned ix, i;
8764 dw_die_ref die = sorted_abbrev_dies[first_id];
8765 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8766 if (implicit_consts[ix])
8768 enum dw_val_class new_class = dw_val_class_none;
8769 switch (AT_class (a))
8771 case dw_val_class_unsigned_const:
8772 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8773 continue;
8775 /* The .debug_abbrev section will grow by
8776 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8777 in all the DIEs using that abbreviation. */
8778 if (constant_size (AT_unsigned (a)) * (end - first_id)
8779 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8780 continue;
8782 new_class = dw_val_class_unsigned_const_implicit;
8783 break;
8785 case dw_val_class_const:
8786 new_class = dw_val_class_const_implicit;
8787 break;
8789 case dw_val_class_file:
8790 new_class = dw_val_class_file_implicit;
8791 break;
8793 default:
8794 continue;
8796 for (i = first_id; i < end; i++)
8797 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8798 = new_class;
8802 /* Attempt to optimize abbreviation table from abbrev_opt_start
8803 abbreviation above. */
8805 static void
8806 optimize_abbrev_table (void)
8808 if (abbrev_opt_start
8809 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8810 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8812 auto_vec<bool, 32> implicit_consts;
8813 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8815 unsigned int abbrev_id = abbrev_opt_start - 1;
8816 unsigned int first_id = ~0U;
8817 unsigned int last_abbrev_id = 0;
8818 unsigned int i;
8819 dw_die_ref die;
8820 if (abbrev_opt_base_type_end > abbrev_opt_start)
8821 abbrev_id = abbrev_opt_base_type_end - 1;
8822 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8823 most commonly used abbreviations come first. */
8824 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8826 dw_attr_node *a;
8827 unsigned ix;
8829 /* If calc_base_type_die_sizes has been called, the CU and
8830 base types after it can't be optimized, because we've already
8831 calculated their DIE offsets. We've sorted them first. */
8832 if (die->die_abbrev < abbrev_opt_base_type_end)
8833 continue;
8834 if (die->die_abbrev != last_abbrev_id)
8836 last_abbrev_id = die->die_abbrev;
8837 if (dwarf_version >= 5 && first_id != ~0U)
8838 optimize_implicit_const (first_id, i, implicit_consts);
8839 abbrev_id++;
8840 (*abbrev_die_table)[abbrev_id] = die;
8841 if (dwarf_version >= 5)
8843 first_id = i;
8844 implicit_consts.truncate (0);
8846 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8847 switch (AT_class (a))
8849 case dw_val_class_const:
8850 case dw_val_class_unsigned_const:
8851 case dw_val_class_file:
8852 implicit_consts.safe_push (true);
8853 break;
8854 default:
8855 implicit_consts.safe_push (false);
8856 break;
8860 else if (dwarf_version >= 5)
8862 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8863 if (!implicit_consts[ix])
8864 continue;
8865 else
8867 dw_attr_node *other_a
8868 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8869 if (!dw_val_equal_p (&a->dw_attr_val,
8870 &other_a->dw_attr_val))
8871 implicit_consts[ix] = false;
8874 die->die_abbrev = abbrev_id;
8876 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8877 if (dwarf_version >= 5 && first_id != ~0U)
8878 optimize_implicit_const (first_id, i, implicit_consts);
8881 abbrev_opt_start = 0;
8882 abbrev_opt_base_type_end = 0;
8883 abbrev_usage_count.release ();
8884 sorted_abbrev_dies.release ();
8887 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8889 static int
8890 constant_size (unsigned HOST_WIDE_INT value)
8892 int log;
8894 if (value == 0)
8895 log = 0;
8896 else
8897 log = floor_log2 (value);
8899 log = log / 8;
8900 log = 1 << (floor_log2 (log) + 1);
8902 return log;
8905 /* Return the size of a DIE as it is represented in the
8906 .debug_info section. */
8908 static unsigned long
8909 size_of_die (dw_die_ref die)
8911 unsigned long size = 0;
8912 dw_attr_node *a;
8913 unsigned ix;
8914 enum dwarf_form form;
8916 size += size_of_uleb128 (die->die_abbrev);
8917 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8919 switch (AT_class (a))
8921 case dw_val_class_addr:
8922 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8924 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8925 size += size_of_uleb128 (AT_index (a));
8927 else
8928 size += DWARF2_ADDR_SIZE;
8929 break;
8930 case dw_val_class_offset:
8931 size += DWARF_OFFSET_SIZE;
8932 break;
8933 case dw_val_class_loc:
8935 unsigned long lsize = size_of_locs (AT_loc (a));
8937 /* Block length. */
8938 if (dwarf_version >= 4)
8939 size += size_of_uleb128 (lsize);
8940 else
8941 size += constant_size (lsize);
8942 size += lsize;
8944 break;
8945 case dw_val_class_loc_list:
8946 if (dwarf_split_debug_info && dwarf_version >= 5)
8948 gcc_assert (AT_loc_list (a)->num_assigned);
8949 size += size_of_uleb128 (AT_loc_list (a)->hash);
8951 else
8952 size += DWARF_OFFSET_SIZE;
8953 break;
8954 case dw_val_class_range_list:
8955 if (value_format (a) == DW_FORM_rnglistx)
8957 gcc_assert (rnglist_idx);
8958 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8959 size += size_of_uleb128 (r->idx);
8961 else
8962 size += DWARF_OFFSET_SIZE;
8963 break;
8964 case dw_val_class_const:
8965 size += size_of_sleb128 (AT_int (a));
8966 break;
8967 case dw_val_class_unsigned_const:
8969 int csize = constant_size (AT_unsigned (a));
8970 if (dwarf_version == 3
8971 && a->dw_attr == DW_AT_data_member_location
8972 && csize >= 4)
8973 size += size_of_uleb128 (AT_unsigned (a));
8974 else
8975 size += csize;
8977 break;
8978 case dw_val_class_const_implicit:
8979 case dw_val_class_unsigned_const_implicit:
8980 case dw_val_class_file_implicit:
8981 /* These occupy no size in the DIE, just an extra sleb128 in
8982 .debug_abbrev. */
8983 break;
8984 case dw_val_class_const_double:
8985 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8986 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
8987 size++; /* block */
8988 break;
8989 case dw_val_class_wide_int:
8990 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8991 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8992 if (get_full_len (*a->dw_attr_val.v.val_wide)
8993 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
8994 size++; /* block */
8995 break;
8996 case dw_val_class_vec:
8997 size += constant_size (a->dw_attr_val.v.val_vec.length
8998 * a->dw_attr_val.v.val_vec.elt_size)
8999 + a->dw_attr_val.v.val_vec.length
9000 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9001 break;
9002 case dw_val_class_flag:
9003 if (dwarf_version >= 4)
9004 /* Currently all add_AT_flag calls pass in 1 as last argument,
9005 so DW_FORM_flag_present can be used. If that ever changes,
9006 we'll need to use DW_FORM_flag and have some optimization
9007 in build_abbrev_table that will change those to
9008 DW_FORM_flag_present if it is set to 1 in all DIEs using
9009 the same abbrev entry. */
9010 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9011 else
9012 size += 1;
9013 break;
9014 case dw_val_class_die_ref:
9015 if (AT_ref_external (a))
9017 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9018 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9019 is sized by target address length, whereas in DWARF3
9020 it's always sized as an offset. */
9021 if (use_debug_types)
9022 size += DWARF_TYPE_SIGNATURE_SIZE;
9023 else if (dwarf_version == 2)
9024 size += DWARF2_ADDR_SIZE;
9025 else
9026 size += DWARF_OFFSET_SIZE;
9028 else
9029 size += DWARF_OFFSET_SIZE;
9030 break;
9031 case dw_val_class_fde_ref:
9032 size += DWARF_OFFSET_SIZE;
9033 break;
9034 case dw_val_class_lbl_id:
9035 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9037 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9038 size += size_of_uleb128 (AT_index (a));
9040 else
9041 size += DWARF2_ADDR_SIZE;
9042 break;
9043 case dw_val_class_lineptr:
9044 case dw_val_class_macptr:
9045 case dw_val_class_loclistsptr:
9046 size += DWARF_OFFSET_SIZE;
9047 break;
9048 case dw_val_class_str:
9049 form = AT_string_form (a);
9050 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9051 size += DWARF_OFFSET_SIZE;
9052 else if (form == DW_FORM_GNU_str_index)
9053 size += size_of_uleb128 (AT_index (a));
9054 else
9055 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9056 break;
9057 case dw_val_class_file:
9058 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9059 break;
9060 case dw_val_class_data8:
9061 size += 8;
9062 break;
9063 case dw_val_class_vms_delta:
9064 size += DWARF_OFFSET_SIZE;
9065 break;
9066 case dw_val_class_high_pc:
9067 size += DWARF2_ADDR_SIZE;
9068 break;
9069 case dw_val_class_discr_value:
9070 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9071 break;
9072 case dw_val_class_discr_list:
9074 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9076 /* This is a block, so we have the block length and then its
9077 data. */
9078 size += constant_size (block_size) + block_size;
9080 break;
9081 default:
9082 gcc_unreachable ();
9086 return size;
9089 /* Size the debugging information associated with a given DIE. Visits the
9090 DIE's children recursively. Updates the global variable next_die_offset, on
9091 each time through. Uses the current value of next_die_offset to update the
9092 die_offset field in each DIE. */
9094 static void
9095 calc_die_sizes (dw_die_ref die)
9097 dw_die_ref c;
9099 gcc_assert (die->die_offset == 0
9100 || (unsigned long int) die->die_offset == next_die_offset);
9101 die->die_offset = next_die_offset;
9102 next_die_offset += size_of_die (die);
9104 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9106 if (die->die_child != NULL)
9107 /* Count the null byte used to terminate sibling lists. */
9108 next_die_offset += 1;
9111 /* Size just the base type children at the start of the CU.
9112 This is needed because build_abbrev needs to size locs
9113 and sizing of type based stack ops needs to know die_offset
9114 values for the base types. */
9116 static void
9117 calc_base_type_die_sizes (void)
9119 unsigned long die_offset = (dwarf_split_debug_info
9120 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9121 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9122 unsigned int i;
9123 dw_die_ref base_type;
9124 #if ENABLE_ASSERT_CHECKING
9125 dw_die_ref prev = comp_unit_die ()->die_child;
9126 #endif
9128 die_offset += size_of_die (comp_unit_die ());
9129 for (i = 0; base_types.iterate (i, &base_type); i++)
9131 #if ENABLE_ASSERT_CHECKING
9132 gcc_assert (base_type->die_offset == 0
9133 && prev->die_sib == base_type
9134 && base_type->die_child == NULL
9135 && base_type->die_abbrev);
9136 prev = base_type;
9137 #endif
9138 if (abbrev_opt_start
9139 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9140 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9141 base_type->die_offset = die_offset;
9142 die_offset += size_of_die (base_type);
9146 /* Set the marks for a die and its children. We do this so
9147 that we know whether or not a reference needs to use FORM_ref_addr; only
9148 DIEs in the same CU will be marked. We used to clear out the offset
9149 and use that as the flag, but ran into ordering problems. */
9151 static void
9152 mark_dies (dw_die_ref die)
9154 dw_die_ref c;
9156 gcc_assert (!die->die_mark);
9158 die->die_mark = 1;
9159 FOR_EACH_CHILD (die, c, mark_dies (c));
9162 /* Clear the marks for a die and its children. */
9164 static void
9165 unmark_dies (dw_die_ref die)
9167 dw_die_ref c;
9169 if (! use_debug_types)
9170 gcc_assert (die->die_mark);
9172 die->die_mark = 0;
9173 FOR_EACH_CHILD (die, c, unmark_dies (c));
9176 /* Clear the marks for a die, its children and referred dies. */
9178 static void
9179 unmark_all_dies (dw_die_ref die)
9181 dw_die_ref c;
9182 dw_attr_node *a;
9183 unsigned ix;
9185 if (!die->die_mark)
9186 return;
9187 die->die_mark = 0;
9189 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9191 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9192 if (AT_class (a) == dw_val_class_die_ref)
9193 unmark_all_dies (AT_ref (a));
9196 /* Calculate if the entry should appear in the final output file. It may be
9197 from a pruned a type. */
9199 static bool
9200 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9202 /* By limiting gnu pubnames to definitions only, gold can generate a
9203 gdb index without entries for declarations, which don't include
9204 enough information to be useful. */
9205 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9206 return false;
9208 if (table == pubname_table)
9210 /* Enumerator names are part of the pubname table, but the
9211 parent DW_TAG_enumeration_type die may have been pruned.
9212 Don't output them if that is the case. */
9213 if (p->die->die_tag == DW_TAG_enumerator &&
9214 (p->die->die_parent == NULL
9215 || !p->die->die_parent->die_perennial_p))
9216 return false;
9218 /* Everything else in the pubname table is included. */
9219 return true;
9222 /* The pubtypes table shouldn't include types that have been
9223 pruned. */
9224 return (p->die->die_offset != 0
9225 || !flag_eliminate_unused_debug_types);
9228 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9229 generated for the compilation unit. */
9231 static unsigned long
9232 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9234 unsigned long size;
9235 unsigned i;
9236 pubname_entry *p;
9237 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9239 size = DWARF_PUBNAMES_HEADER_SIZE;
9240 FOR_EACH_VEC_ELT (*names, i, p)
9241 if (include_pubname_in_output (names, p))
9242 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9244 size += DWARF_OFFSET_SIZE;
9245 return size;
9248 /* Return the size of the information in the .debug_aranges section. */
9250 static unsigned long
9251 size_of_aranges (void)
9253 unsigned long size;
9255 size = DWARF_ARANGES_HEADER_SIZE;
9257 /* Count the address/length pair for this compilation unit. */
9258 if (text_section_used)
9259 size += 2 * DWARF2_ADDR_SIZE;
9260 if (cold_text_section_used)
9261 size += 2 * DWARF2_ADDR_SIZE;
9262 if (have_multiple_function_sections)
9264 unsigned fde_idx;
9265 dw_fde_ref fde;
9267 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9269 if (DECL_IGNORED_P (fde->decl))
9270 continue;
9271 if (!fde->in_std_section)
9272 size += 2 * DWARF2_ADDR_SIZE;
9273 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9274 size += 2 * DWARF2_ADDR_SIZE;
9278 /* Count the two zero words used to terminated the address range table. */
9279 size += 2 * DWARF2_ADDR_SIZE;
9280 return size;
9283 /* Select the encoding of an attribute value. */
9285 static enum dwarf_form
9286 value_format (dw_attr_node *a)
9288 switch (AT_class (a))
9290 case dw_val_class_addr:
9291 /* Only very few attributes allow DW_FORM_addr. */
9292 switch (a->dw_attr)
9294 case DW_AT_low_pc:
9295 case DW_AT_high_pc:
9296 case DW_AT_entry_pc:
9297 case DW_AT_trampoline:
9298 return (AT_index (a) == NOT_INDEXED
9299 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9300 default:
9301 break;
9303 switch (DWARF2_ADDR_SIZE)
9305 case 1:
9306 return DW_FORM_data1;
9307 case 2:
9308 return DW_FORM_data2;
9309 case 4:
9310 return DW_FORM_data4;
9311 case 8:
9312 return DW_FORM_data8;
9313 default:
9314 gcc_unreachable ();
9316 case dw_val_class_loc_list:
9317 if (dwarf_split_debug_info
9318 && dwarf_version >= 5
9319 && AT_loc_list (a)->num_assigned)
9320 return DW_FORM_loclistx;
9321 /* FALLTHRU */
9322 case dw_val_class_range_list:
9323 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9324 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9325 care about sizes of .debug* sections in shared libraries and
9326 executables and don't take into account relocations that affect just
9327 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9328 table in the .debug_rnglists section. */
9329 if (dwarf_split_debug_info
9330 && dwarf_version >= 5
9331 && AT_class (a) == dw_val_class_range_list
9332 && rnglist_idx
9333 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9334 return DW_FORM_rnglistx;
9335 if (dwarf_version >= 4)
9336 return DW_FORM_sec_offset;
9337 /* FALLTHRU */
9338 case dw_val_class_vms_delta:
9339 case dw_val_class_offset:
9340 switch (DWARF_OFFSET_SIZE)
9342 case 4:
9343 return DW_FORM_data4;
9344 case 8:
9345 return DW_FORM_data8;
9346 default:
9347 gcc_unreachable ();
9349 case dw_val_class_loc:
9350 if (dwarf_version >= 4)
9351 return DW_FORM_exprloc;
9352 switch (constant_size (size_of_locs (AT_loc (a))))
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_const:
9364 return DW_FORM_sdata;
9365 case dw_val_class_unsigned_const:
9366 switch (constant_size (AT_unsigned (a)))
9368 case 1:
9369 return DW_FORM_data1;
9370 case 2:
9371 return DW_FORM_data2;
9372 case 4:
9373 /* In DWARF3 DW_AT_data_member_location with
9374 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9375 constant, so we need to use DW_FORM_udata if we need
9376 a large constant. */
9377 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9378 return DW_FORM_udata;
9379 return DW_FORM_data4;
9380 case 8:
9381 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9382 return DW_FORM_udata;
9383 return DW_FORM_data8;
9384 default:
9385 gcc_unreachable ();
9387 case dw_val_class_const_implicit:
9388 case dw_val_class_unsigned_const_implicit:
9389 case dw_val_class_file_implicit:
9390 return DW_FORM_implicit_const;
9391 case dw_val_class_const_double:
9392 switch (HOST_BITS_PER_WIDE_INT)
9394 case 8:
9395 return DW_FORM_data2;
9396 case 16:
9397 return DW_FORM_data4;
9398 case 32:
9399 return DW_FORM_data8;
9400 case 64:
9401 if (dwarf_version >= 5)
9402 return DW_FORM_data16;
9403 /* FALLTHRU */
9404 default:
9405 return DW_FORM_block1;
9407 case dw_val_class_wide_int:
9408 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9410 case 8:
9411 return DW_FORM_data1;
9412 case 16:
9413 return DW_FORM_data2;
9414 case 32:
9415 return DW_FORM_data4;
9416 case 64:
9417 return DW_FORM_data8;
9418 case 128:
9419 if (dwarf_version >= 5)
9420 return DW_FORM_data16;
9421 /* FALLTHRU */
9422 default:
9423 return DW_FORM_block1;
9425 case dw_val_class_vec:
9426 switch (constant_size (a->dw_attr_val.v.val_vec.length
9427 * a->dw_attr_val.v.val_vec.elt_size))
9429 case 1:
9430 return DW_FORM_block1;
9431 case 2:
9432 return DW_FORM_block2;
9433 case 4:
9434 return DW_FORM_block4;
9435 default:
9436 gcc_unreachable ();
9438 case dw_val_class_flag:
9439 if (dwarf_version >= 4)
9441 /* Currently all add_AT_flag calls pass in 1 as last argument,
9442 so DW_FORM_flag_present can be used. If that ever changes,
9443 we'll need to use DW_FORM_flag and have some optimization
9444 in build_abbrev_table that will change those to
9445 DW_FORM_flag_present if it is set to 1 in all DIEs using
9446 the same abbrev entry. */
9447 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9448 return DW_FORM_flag_present;
9450 return DW_FORM_flag;
9451 case dw_val_class_die_ref:
9452 if (AT_ref_external (a))
9453 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9454 else
9455 return DW_FORM_ref;
9456 case dw_val_class_fde_ref:
9457 return DW_FORM_data;
9458 case dw_val_class_lbl_id:
9459 return (AT_index (a) == NOT_INDEXED
9460 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9461 case dw_val_class_lineptr:
9462 case dw_val_class_macptr:
9463 case dw_val_class_loclistsptr:
9464 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9465 case dw_val_class_str:
9466 return AT_string_form (a);
9467 case dw_val_class_file:
9468 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9470 case 1:
9471 return DW_FORM_data1;
9472 case 2:
9473 return DW_FORM_data2;
9474 case 4:
9475 return DW_FORM_data4;
9476 default:
9477 gcc_unreachable ();
9480 case dw_val_class_data8:
9481 return DW_FORM_data8;
9483 case dw_val_class_high_pc:
9484 switch (DWARF2_ADDR_SIZE)
9486 case 1:
9487 return DW_FORM_data1;
9488 case 2:
9489 return DW_FORM_data2;
9490 case 4:
9491 return DW_FORM_data4;
9492 case 8:
9493 return DW_FORM_data8;
9494 default:
9495 gcc_unreachable ();
9498 case dw_val_class_discr_value:
9499 return (a->dw_attr_val.v.val_discr_value.pos
9500 ? DW_FORM_udata
9501 : DW_FORM_sdata);
9502 case dw_val_class_discr_list:
9503 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9505 case 1:
9506 return DW_FORM_block1;
9507 case 2:
9508 return DW_FORM_block2;
9509 case 4:
9510 return DW_FORM_block4;
9511 default:
9512 gcc_unreachable ();
9515 default:
9516 gcc_unreachable ();
9520 /* Output the encoding of an attribute value. */
9522 static void
9523 output_value_format (dw_attr_node *a)
9525 enum dwarf_form form = value_format (a);
9527 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9530 /* Given a die and id, produce the appropriate abbreviations. */
9532 static void
9533 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9535 unsigned ix;
9536 dw_attr_node *a_attr;
9538 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9539 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9540 dwarf_tag_name (abbrev->die_tag));
9542 if (abbrev->die_child != NULL)
9543 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9544 else
9545 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9547 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9549 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9550 dwarf_attr_name (a_attr->dw_attr));
9551 output_value_format (a_attr);
9552 if (value_format (a_attr) == DW_FORM_implicit_const)
9554 if (AT_class (a_attr) == dw_val_class_file_implicit)
9556 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9557 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9558 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9560 else
9561 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9565 dw2_asm_output_data (1, 0, NULL);
9566 dw2_asm_output_data (1, 0, NULL);
9570 /* Output the .debug_abbrev section which defines the DIE abbreviation
9571 table. */
9573 static void
9574 output_abbrev_section (void)
9576 unsigned int abbrev_id;
9577 dw_die_ref abbrev;
9579 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9580 if (abbrev_id != 0)
9581 output_die_abbrevs (abbrev_id, abbrev);
9583 /* Terminate the table. */
9584 dw2_asm_output_data (1, 0, NULL);
9587 /* Output a symbol we can use to refer to this DIE from another CU. */
9589 static inline void
9590 output_die_symbol (dw_die_ref die)
9592 const char *sym = die->die_id.die_symbol;
9594 gcc_assert (!die->comdat_type_p);
9596 if (sym == 0)
9597 return;
9599 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9600 /* We make these global, not weak; if the target doesn't support
9601 .linkonce, it doesn't support combining the sections, so debugging
9602 will break. */
9603 targetm.asm_out.globalize_label (asm_out_file, sym);
9605 ASM_OUTPUT_LABEL (asm_out_file, sym);
9608 /* Return a new location list, given the begin and end range, and the
9609 expression. */
9611 static inline dw_loc_list_ref
9612 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9613 const char *section)
9615 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9617 retlist->begin = begin;
9618 retlist->begin_entry = NULL;
9619 retlist->end = end;
9620 retlist->expr = expr;
9621 retlist->section = section;
9623 return retlist;
9626 /* Generate a new internal symbol for this location list node, if it
9627 hasn't got one yet. */
9629 static inline void
9630 gen_llsym (dw_loc_list_ref list)
9632 gcc_assert (!list->ll_symbol);
9633 list->ll_symbol = gen_internal_sym ("LLST");
9636 /* Output the location list given to us. */
9638 static void
9639 output_loc_list (dw_loc_list_ref list_head)
9641 if (list_head->emitted)
9642 return;
9643 list_head->emitted = true;
9645 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9647 dw_loc_list_ref curr = list_head;
9648 const char *last_section = NULL;
9649 const char *base_label = NULL;
9651 /* Walk the location list, and output each range + expression. */
9652 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9654 unsigned long size;
9655 /* Don't output an entry that starts and ends at the same address. */
9656 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9657 continue;
9658 size = size_of_locs (curr->expr);
9659 /* If the expression is too large, drop it on the floor. We could
9660 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9661 in the expression, but >= 64KB expressions for a single value
9662 in a single range are unlikely very useful. */
9663 if (dwarf_version < 5 && size > 0xffff)
9664 continue;
9665 if (dwarf_version >= 5)
9667 if (dwarf_split_debug_info)
9669 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9670 uleb128 index into .debug_addr and uleb128 length. */
9671 dw2_asm_output_data (1, DW_LLE_startx_length,
9672 "DW_LLE_startx_length (%s)",
9673 list_head->ll_symbol);
9674 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9675 "Location list range start index "
9676 "(%s)", curr->begin);
9677 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9678 For that case we probably need to emit DW_LLE_startx_endx,
9679 but we'd need 2 .debug_addr entries rather than just one. */
9680 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9681 "Location list length (%s)",
9682 list_head->ll_symbol);
9684 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9686 /* If all code is in .text section, the base address is
9687 already provided by the CU attributes. Use
9688 DW_LLE_offset_pair where both addresses are uleb128 encoded
9689 offsets against that base. */
9690 dw2_asm_output_data (1, DW_LLE_offset_pair,
9691 "DW_LLE_offset_pair (%s)",
9692 list_head->ll_symbol);
9693 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9694 "Location list begin address (%s)",
9695 list_head->ll_symbol);
9696 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9697 "Location list end address (%s)",
9698 list_head->ll_symbol);
9700 else if (HAVE_AS_LEB128)
9702 /* Otherwise, find out how many consecutive entries could share
9703 the same base entry. If just one, emit DW_LLE_start_length,
9704 otherwise emit DW_LLE_base_address for the base address
9705 followed by a series of DW_LLE_offset_pair. */
9706 if (last_section == NULL || curr->section != last_section)
9708 dw_loc_list_ref curr2;
9709 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9710 curr2 = curr2->dw_loc_next)
9712 if (strcmp (curr2->begin, curr2->end) == 0
9713 && !curr2->force)
9714 continue;
9715 break;
9717 if (curr2 == NULL || curr->section != curr2->section)
9718 last_section = NULL;
9719 else
9721 last_section = curr->section;
9722 base_label = curr->begin;
9723 dw2_asm_output_data (1, DW_LLE_base_address,
9724 "DW_LLE_base_address (%s)",
9725 list_head->ll_symbol);
9726 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9727 "Base address (%s)",
9728 list_head->ll_symbol);
9731 /* Only one entry with the same base address. Use
9732 DW_LLE_start_length with absolute address and uleb128
9733 length. */
9734 if (last_section == NULL)
9736 dw2_asm_output_data (1, DW_LLE_start_length,
9737 "DW_LLE_start_length (%s)",
9738 list_head->ll_symbol);
9739 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9740 "Location list begin address (%s)",
9741 list_head->ll_symbol);
9742 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9743 "Location list length "
9744 "(%s)", list_head->ll_symbol);
9746 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9747 DW_LLE_base_address. */
9748 else
9750 dw2_asm_output_data (1, DW_LLE_offset_pair,
9751 "DW_LLE_offset_pair (%s)",
9752 list_head->ll_symbol);
9753 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9754 "Location list begin address "
9755 "(%s)", list_head->ll_symbol);
9756 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9757 "Location list end address "
9758 "(%s)", list_head->ll_symbol);
9761 /* The assembler does not support .uleb128 directive. Emit
9762 DW_LLE_start_end with a pair of absolute addresses. */
9763 else
9765 dw2_asm_output_data (1, DW_LLE_start_end,
9766 "DW_LLE_start_end (%s)",
9767 list_head->ll_symbol);
9768 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9769 "Location list begin address (%s)",
9770 list_head->ll_symbol);
9771 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9772 "Location list end address (%s)",
9773 list_head->ll_symbol);
9776 else if (dwarf_split_debug_info)
9778 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9779 and 4 byte length. */
9780 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9781 "Location list start/length entry (%s)",
9782 list_head->ll_symbol);
9783 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9784 "Location list range start index (%s)",
9785 curr->begin);
9786 /* The length field is 4 bytes. If we ever need to support
9787 an 8-byte length, we can add a new DW_LLE code or fall back
9788 to DW_LLE_GNU_start_end_entry. */
9789 dw2_asm_output_delta (4, curr->end, curr->begin,
9790 "Location list range length (%s)",
9791 list_head->ll_symbol);
9793 else if (!have_multiple_function_sections)
9795 /* Pair of relative addresses against start of text section. */
9796 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9797 "Location list begin address (%s)",
9798 list_head->ll_symbol);
9799 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9800 "Location list end address (%s)",
9801 list_head->ll_symbol);
9803 else
9805 /* Pair of absolute addresses. */
9806 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9807 "Location list begin address (%s)",
9808 list_head->ll_symbol);
9809 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9810 "Location list end address (%s)",
9811 list_head->ll_symbol);
9814 /* Output the block length for this list of location operations. */
9815 if (dwarf_version >= 5)
9816 dw2_asm_output_data_uleb128 (size, "Location expression size");
9817 else
9819 gcc_assert (size <= 0xffff);
9820 dw2_asm_output_data (2, size, "Location expression size");
9823 output_loc_sequence (curr->expr, -1);
9826 /* And finally list termination. */
9827 if (dwarf_version >= 5)
9828 dw2_asm_output_data (1, DW_LLE_end_of_list,
9829 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9830 else if (dwarf_split_debug_info)
9831 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9832 "Location list terminator (%s)",
9833 list_head->ll_symbol);
9834 else
9836 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9837 "Location list terminator begin (%s)",
9838 list_head->ll_symbol);
9839 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9840 "Location list terminator end (%s)",
9841 list_head->ll_symbol);
9845 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9846 section. Emit a relocated reference if val_entry is NULL, otherwise,
9847 emit an indirect reference. */
9849 static void
9850 output_range_list_offset (dw_attr_node *a)
9852 const char *name = dwarf_attr_name (a->dw_attr);
9854 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9856 if (dwarf_version >= 5)
9858 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9859 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9860 debug_ranges_section, "%s", name);
9862 else
9864 char *p = strchr (ranges_section_label, '\0');
9865 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9866 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9867 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9868 debug_ranges_section, "%s", name);
9869 *p = '\0';
9872 else if (dwarf_version >= 5)
9874 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9875 gcc_assert (rnglist_idx);
9876 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9878 else
9879 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9880 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9881 "%s (offset from %s)", name, ranges_section_label);
9884 /* Output the offset into the debug_loc section. */
9886 static void
9887 output_loc_list_offset (dw_attr_node *a)
9889 char *sym = AT_loc_list (a)->ll_symbol;
9891 gcc_assert (sym);
9892 if (!dwarf_split_debug_info)
9893 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9894 "%s", dwarf_attr_name (a->dw_attr));
9895 else if (dwarf_version >= 5)
9897 gcc_assert (AT_loc_list (a)->num_assigned);
9898 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9899 dwarf_attr_name (a->dw_attr),
9900 sym);
9902 else
9903 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9904 "%s", dwarf_attr_name (a->dw_attr));
9907 /* Output an attribute's index or value appropriately. */
9909 static void
9910 output_attr_index_or_value (dw_attr_node *a)
9912 const char *name = dwarf_attr_name (a->dw_attr);
9914 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9916 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9917 return;
9919 switch (AT_class (a))
9921 case dw_val_class_addr:
9922 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9923 break;
9924 case dw_val_class_high_pc:
9925 case dw_val_class_lbl_id:
9926 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9927 break;
9928 default:
9929 gcc_unreachable ();
9933 /* Output a type signature. */
9935 static inline void
9936 output_signature (const char *sig, const char *name)
9938 int i;
9940 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9941 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9944 /* Output a discriminant value. */
9946 static inline void
9947 output_discr_value (dw_discr_value *discr_value, const char *name)
9949 if (discr_value->pos)
9950 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9951 else
9952 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9955 /* Output the DIE and its attributes. Called recursively to generate
9956 the definitions of each child DIE. */
9958 static void
9959 output_die (dw_die_ref die)
9961 dw_attr_node *a;
9962 dw_die_ref c;
9963 unsigned long size;
9964 unsigned ix;
9966 /* If someone in another CU might refer to us, set up a symbol for
9967 them to point to. */
9968 if (! die->comdat_type_p && die->die_id.die_symbol)
9969 output_die_symbol (die);
9971 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9972 (unsigned long)die->die_offset,
9973 dwarf_tag_name (die->die_tag));
9975 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9977 const char *name = dwarf_attr_name (a->dw_attr);
9979 switch (AT_class (a))
9981 case dw_val_class_addr:
9982 output_attr_index_or_value (a);
9983 break;
9985 case dw_val_class_offset:
9986 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9987 "%s", name);
9988 break;
9990 case dw_val_class_range_list:
9991 output_range_list_offset (a);
9992 break;
9994 case dw_val_class_loc:
9995 size = size_of_locs (AT_loc (a));
9997 /* Output the block length for this list of location operations. */
9998 if (dwarf_version >= 4)
9999 dw2_asm_output_data_uleb128 (size, "%s", name);
10000 else
10001 dw2_asm_output_data (constant_size (size), size, "%s", name);
10003 output_loc_sequence (AT_loc (a), -1);
10004 break;
10006 case dw_val_class_const:
10007 /* ??? It would be slightly more efficient to use a scheme like is
10008 used for unsigned constants below, but gdb 4.x does not sign
10009 extend. Gdb 5.x does sign extend. */
10010 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10011 break;
10013 case dw_val_class_unsigned_const:
10015 int csize = constant_size (AT_unsigned (a));
10016 if (dwarf_version == 3
10017 && a->dw_attr == DW_AT_data_member_location
10018 && csize >= 4)
10019 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10020 else
10021 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10023 break;
10025 case dw_val_class_const_implicit:
10026 if (flag_debug_asm)
10027 fprintf (asm_out_file, "\t\t\t%s %s ("
10028 HOST_WIDE_INT_PRINT_DEC ")\n",
10029 ASM_COMMENT_START, name, AT_int (a));
10030 break;
10032 case dw_val_class_unsigned_const_implicit:
10033 if (flag_debug_asm)
10034 fprintf (asm_out_file, "\t\t\t%s %s ("
10035 HOST_WIDE_INT_PRINT_HEX ")\n",
10036 ASM_COMMENT_START, name, AT_unsigned (a));
10037 break;
10039 case dw_val_class_const_double:
10041 unsigned HOST_WIDE_INT first, second;
10043 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10044 dw2_asm_output_data (1,
10045 HOST_BITS_PER_DOUBLE_INT
10046 / HOST_BITS_PER_CHAR,
10047 NULL);
10049 if (WORDS_BIG_ENDIAN)
10051 first = a->dw_attr_val.v.val_double.high;
10052 second = a->dw_attr_val.v.val_double.low;
10054 else
10056 first = a->dw_attr_val.v.val_double.low;
10057 second = a->dw_attr_val.v.val_double.high;
10060 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10061 first, "%s", name);
10062 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10063 second, NULL);
10065 break;
10067 case dw_val_class_wide_int:
10069 int i;
10070 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10071 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10072 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10073 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10074 * l, NULL);
10076 if (WORDS_BIG_ENDIAN)
10077 for (i = len - 1; i >= 0; --i)
10079 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10080 "%s", name);
10081 name = "";
10083 else
10084 for (i = 0; i < len; ++i)
10086 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10087 "%s", name);
10088 name = "";
10091 break;
10093 case dw_val_class_vec:
10095 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10096 unsigned int len = a->dw_attr_val.v.val_vec.length;
10097 unsigned int i;
10098 unsigned char *p;
10100 dw2_asm_output_data (constant_size (len * elt_size),
10101 len * elt_size, "%s", name);
10102 if (elt_size > sizeof (HOST_WIDE_INT))
10104 elt_size /= 2;
10105 len *= 2;
10107 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10108 i < len;
10109 i++, p += elt_size)
10110 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10111 "fp or vector constant word %u", i);
10112 break;
10115 case dw_val_class_flag:
10116 if (dwarf_version >= 4)
10118 /* Currently all add_AT_flag calls pass in 1 as last argument,
10119 so DW_FORM_flag_present can be used. If that ever changes,
10120 we'll need to use DW_FORM_flag and have some optimization
10121 in build_abbrev_table that will change those to
10122 DW_FORM_flag_present if it is set to 1 in all DIEs using
10123 the same abbrev entry. */
10124 gcc_assert (AT_flag (a) == 1);
10125 if (flag_debug_asm)
10126 fprintf (asm_out_file, "\t\t\t%s %s\n",
10127 ASM_COMMENT_START, name);
10128 break;
10130 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10131 break;
10133 case dw_val_class_loc_list:
10134 output_loc_list_offset (a);
10135 break;
10137 case dw_val_class_die_ref:
10138 if (AT_ref_external (a))
10140 if (AT_ref (a)->comdat_type_p)
10142 comdat_type_node *type_node
10143 = AT_ref (a)->die_id.die_type_node;
10145 gcc_assert (type_node);
10146 output_signature (type_node->signature, name);
10148 else
10150 const char *sym = AT_ref (a)->die_id.die_symbol;
10151 int size;
10153 gcc_assert (sym);
10154 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10155 length, whereas in DWARF3 it's always sized as an
10156 offset. */
10157 if (dwarf_version == 2)
10158 size = DWARF2_ADDR_SIZE;
10159 else
10160 size = DWARF_OFFSET_SIZE;
10161 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10162 name);
10165 else
10167 gcc_assert (AT_ref (a)->die_offset);
10168 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10169 "%s", name);
10171 break;
10173 case dw_val_class_fde_ref:
10175 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10177 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10178 a->dw_attr_val.v.val_fde_index * 2);
10179 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10180 "%s", name);
10182 break;
10184 case dw_val_class_vms_delta:
10185 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10186 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10187 AT_vms_delta2 (a), AT_vms_delta1 (a),
10188 "%s", name);
10189 #else
10190 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10191 AT_vms_delta2 (a), AT_vms_delta1 (a),
10192 "%s", name);
10193 #endif
10194 break;
10196 case dw_val_class_lbl_id:
10197 output_attr_index_or_value (a);
10198 break;
10200 case dw_val_class_lineptr:
10201 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10202 debug_line_section, "%s", name);
10203 break;
10205 case dw_val_class_macptr:
10206 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10207 debug_macinfo_section, "%s", name);
10208 break;
10210 case dw_val_class_loclistsptr:
10211 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10212 debug_loc_section, "%s", name);
10213 break;
10215 case dw_val_class_str:
10216 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10217 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10218 a->dw_attr_val.v.val_str->label,
10219 debug_str_section,
10220 "%s: \"%s\"", name, AT_string (a));
10221 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10222 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10223 a->dw_attr_val.v.val_str->label,
10224 debug_line_str_section,
10225 "%s: \"%s\"", name, AT_string (a));
10226 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10227 dw2_asm_output_data_uleb128 (AT_index (a),
10228 "%s: \"%s\"", name, AT_string (a));
10229 else
10230 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10231 break;
10233 case dw_val_class_file:
10235 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10237 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10238 a->dw_attr_val.v.val_file->filename);
10239 break;
10242 case dw_val_class_file_implicit:
10243 if (flag_debug_asm)
10244 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10245 ASM_COMMENT_START, name,
10246 maybe_emit_file (a->dw_attr_val.v.val_file),
10247 a->dw_attr_val.v.val_file->filename);
10248 break;
10250 case dw_val_class_data8:
10252 int i;
10254 for (i = 0; i < 8; i++)
10255 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10256 i == 0 ? "%s" : NULL, name);
10257 break;
10260 case dw_val_class_high_pc:
10261 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10262 get_AT_low_pc (die), "DW_AT_high_pc");
10263 break;
10265 case dw_val_class_discr_value:
10266 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10267 break;
10269 case dw_val_class_discr_list:
10271 dw_discr_list_ref list = AT_discr_list (a);
10272 const int size = size_of_discr_list (list);
10274 /* This is a block, so output its length first. */
10275 dw2_asm_output_data (constant_size (size), size,
10276 "%s: block size", name);
10278 for (; list != NULL; list = list->dw_discr_next)
10280 /* One byte for the discriminant value descriptor, and then as
10281 many LEB128 numbers as required. */
10282 if (list->dw_discr_range)
10283 dw2_asm_output_data (1, DW_DSC_range,
10284 "%s: DW_DSC_range", name);
10285 else
10286 dw2_asm_output_data (1, DW_DSC_label,
10287 "%s: DW_DSC_label", name);
10289 output_discr_value (&list->dw_discr_lower_bound, name);
10290 if (list->dw_discr_range)
10291 output_discr_value (&list->dw_discr_upper_bound, name);
10293 break;
10296 default:
10297 gcc_unreachable ();
10301 FOR_EACH_CHILD (die, c, output_die (c));
10303 /* Add null byte to terminate sibling list. */
10304 if (die->die_child != NULL)
10305 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10306 (unsigned long) die->die_offset);
10309 /* Output the compilation unit that appears at the beginning of the
10310 .debug_info section, and precedes the DIE descriptions. */
10312 static void
10313 output_compilation_unit_header (enum dwarf_unit_type ut)
10315 if (!XCOFF_DEBUGGING_INFO)
10317 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10318 dw2_asm_output_data (4, 0xffffffff,
10319 "Initial length escape value indicating 64-bit DWARF extension");
10320 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10321 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10322 "Length of Compilation Unit Info");
10325 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10326 if (dwarf_version >= 5)
10328 const char *name;
10329 switch (ut)
10331 case DW_UT_compile: name = "DW_UT_compile"; break;
10332 case DW_UT_type: name = "DW_UT_type"; break;
10333 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10334 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10335 default: gcc_unreachable ();
10337 dw2_asm_output_data (1, ut, "%s", name);
10338 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10340 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10341 debug_abbrev_section,
10342 "Offset Into Abbrev. Section");
10343 if (dwarf_version < 5)
10344 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10347 /* Output the compilation unit DIE and its children. */
10349 static void
10350 output_comp_unit (dw_die_ref die, int output_if_empty,
10351 const unsigned char *dwo_id)
10353 const char *secname, *oldsym;
10354 char *tmp;
10356 /* Unless we are outputting main CU, we may throw away empty ones. */
10357 if (!output_if_empty && die->die_child == NULL)
10358 return;
10360 /* Even if there are no children of this DIE, we must output the information
10361 about the compilation unit. Otherwise, on an empty translation unit, we
10362 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10363 will then complain when examining the file. First mark all the DIEs in
10364 this CU so we know which get local refs. */
10365 mark_dies (die);
10367 external_ref_hash_type *extern_map = optimize_external_refs (die);
10369 /* For now, optimize only the main CU, in order to optimize the rest
10370 we'd need to see all of them earlier. Leave the rest for post-linking
10371 tools like DWZ. */
10372 if (die == comp_unit_die ())
10373 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10375 build_abbrev_table (die, extern_map);
10377 optimize_abbrev_table ();
10379 delete extern_map;
10381 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10382 next_die_offset = (dwo_id
10383 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10384 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10385 calc_die_sizes (die);
10387 oldsym = die->die_id.die_symbol;
10388 if (oldsym)
10390 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10392 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10393 secname = tmp;
10394 die->die_id.die_symbol = NULL;
10395 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10397 else
10399 switch_to_section (debug_info_section);
10400 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10401 info_section_emitted = true;
10404 /* Output debugging information. */
10405 output_compilation_unit_header (dwo_id
10406 ? DW_UT_split_compile : DW_UT_compile);
10407 if (dwarf_version >= 5)
10409 if (dwo_id != NULL)
10410 for (int i = 0; i < 8; i++)
10411 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10413 output_die (die);
10415 /* Leave the marks on the main CU, so we can check them in
10416 output_pubnames. */
10417 if (oldsym)
10419 unmark_dies (die);
10420 die->die_id.die_symbol = oldsym;
10424 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10425 and .debug_pubtypes. This is configured per-target, but can be
10426 overridden by the -gpubnames or -gno-pubnames options. */
10428 static inline bool
10429 want_pubnames (void)
10431 if (debug_info_level <= DINFO_LEVEL_TERSE)
10432 return false;
10433 if (debug_generate_pub_sections != -1)
10434 return debug_generate_pub_sections;
10435 return targetm.want_debug_pub_sections;
10438 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10440 static void
10441 add_AT_pubnames (dw_die_ref die)
10443 if (want_pubnames ())
10444 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10447 /* Add a string attribute value to a skeleton DIE. */
10449 static inline void
10450 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10451 const char *str)
10453 dw_attr_node attr;
10454 struct indirect_string_node *node;
10456 if (! skeleton_debug_str_hash)
10457 skeleton_debug_str_hash
10458 = hash_table<indirect_string_hasher>::create_ggc (10);
10460 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10461 find_string_form (node);
10462 if (node->form == DW_FORM_GNU_str_index)
10463 node->form = DW_FORM_strp;
10465 attr.dw_attr = attr_kind;
10466 attr.dw_attr_val.val_class = dw_val_class_str;
10467 attr.dw_attr_val.val_entry = NULL;
10468 attr.dw_attr_val.v.val_str = node;
10469 add_dwarf_attr (die, &attr);
10472 /* Helper function to generate top-level dies for skeleton debug_info and
10473 debug_types. */
10475 static void
10476 add_top_level_skeleton_die_attrs (dw_die_ref die)
10478 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10479 const char *comp_dir = comp_dir_string ();
10481 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10482 if (comp_dir != NULL)
10483 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10484 add_AT_pubnames (die);
10485 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10488 /* Output skeleton debug sections that point to the dwo file. */
10490 static void
10491 output_skeleton_debug_sections (dw_die_ref comp_unit,
10492 const unsigned char *dwo_id)
10494 /* These attributes will be found in the full debug_info section. */
10495 remove_AT (comp_unit, DW_AT_producer);
10496 remove_AT (comp_unit, DW_AT_language);
10498 switch_to_section (debug_skeleton_info_section);
10499 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10501 /* Produce the skeleton compilation-unit header. This one differs enough from
10502 a normal CU header that it's better not to call output_compilation_unit
10503 header. */
10504 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10505 dw2_asm_output_data (4, 0xffffffff,
10506 "Initial length escape value indicating 64-bit "
10507 "DWARF extension");
10509 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10510 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10511 - DWARF_INITIAL_LENGTH_SIZE
10512 + size_of_die (comp_unit),
10513 "Length of Compilation Unit Info");
10514 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10515 if (dwarf_version >= 5)
10517 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10518 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10520 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10521 debug_skeleton_abbrev_section,
10522 "Offset Into Abbrev. Section");
10523 if (dwarf_version < 5)
10524 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10525 else
10526 for (int i = 0; i < 8; i++)
10527 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10529 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10530 output_die (comp_unit);
10532 /* Build the skeleton debug_abbrev section. */
10533 switch_to_section (debug_skeleton_abbrev_section);
10534 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10536 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10538 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10541 /* Output a comdat type unit DIE and its children. */
10543 static void
10544 output_comdat_type_unit (comdat_type_node *node)
10546 const char *secname;
10547 char *tmp;
10548 int i;
10549 #if defined (OBJECT_FORMAT_ELF)
10550 tree comdat_key;
10551 #endif
10553 /* First mark all the DIEs in this CU so we know which get local refs. */
10554 mark_dies (node->root_die);
10556 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10558 build_abbrev_table (node->root_die, extern_map);
10560 delete extern_map;
10561 extern_map = NULL;
10563 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10564 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10565 calc_die_sizes (node->root_die);
10567 #if defined (OBJECT_FORMAT_ELF)
10568 if (dwarf_version >= 5)
10570 if (!dwarf_split_debug_info)
10571 secname = ".debug_info";
10572 else
10573 secname = ".debug_info.dwo";
10575 else if (!dwarf_split_debug_info)
10576 secname = ".debug_types";
10577 else
10578 secname = ".debug_types.dwo";
10580 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10581 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10582 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10583 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10584 comdat_key = get_identifier (tmp);
10585 targetm.asm_out.named_section (secname,
10586 SECTION_DEBUG | SECTION_LINKONCE,
10587 comdat_key);
10588 #else
10589 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10590 sprintf (tmp, (dwarf_version >= 5
10591 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10592 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10593 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10594 secname = tmp;
10595 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10596 #endif
10598 /* Output debugging information. */
10599 output_compilation_unit_header (dwarf_split_debug_info
10600 ? DW_UT_split_type : DW_UT_type);
10601 output_signature (node->signature, "Type Signature");
10602 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10603 "Offset to Type DIE");
10604 output_die (node->root_die);
10606 unmark_dies (node->root_die);
10609 /* Return the DWARF2/3 pubname associated with a decl. */
10611 static const char *
10612 dwarf2_name (tree decl, int scope)
10614 if (DECL_NAMELESS (decl))
10615 return NULL;
10616 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10619 /* Add a new entry to .debug_pubnames if appropriate. */
10621 static void
10622 add_pubname_string (const char *str, dw_die_ref die)
10624 pubname_entry e;
10626 e.die = die;
10627 e.name = xstrdup (str);
10628 vec_safe_push (pubname_table, e);
10631 static void
10632 add_pubname (tree decl, dw_die_ref die)
10634 if (!want_pubnames ())
10635 return;
10637 /* Don't add items to the table when we expect that the consumer will have
10638 just read the enclosing die. For example, if the consumer is looking at a
10639 class_member, it will either be inside the class already, or will have just
10640 looked up the class to find the member. Either way, searching the class is
10641 faster than searching the index. */
10642 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10643 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10645 const char *name = dwarf2_name (decl, 1);
10647 if (name)
10648 add_pubname_string (name, die);
10652 /* Add an enumerator to the pubnames section. */
10654 static void
10655 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10657 pubname_entry e;
10659 gcc_assert (scope_name);
10660 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10661 e.die = die;
10662 vec_safe_push (pubname_table, e);
10665 /* Add a new entry to .debug_pubtypes if appropriate. */
10667 static void
10668 add_pubtype (tree decl, dw_die_ref die)
10670 pubname_entry e;
10672 if (!want_pubnames ())
10673 return;
10675 if ((TREE_PUBLIC (decl)
10676 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10677 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10679 tree scope = NULL;
10680 const char *scope_name = "";
10681 const char *sep = is_cxx () ? "::" : ".";
10682 const char *name;
10684 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10685 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10687 scope_name = lang_hooks.dwarf_name (scope, 1);
10688 if (scope_name != NULL && scope_name[0] != '\0')
10689 scope_name = concat (scope_name, sep, NULL);
10690 else
10691 scope_name = "";
10694 if (TYPE_P (decl))
10695 name = type_tag (decl);
10696 else
10697 name = lang_hooks.dwarf_name (decl, 1);
10699 /* If we don't have a name for the type, there's no point in adding
10700 it to the table. */
10701 if (name != NULL && name[0] != '\0')
10703 e.die = die;
10704 e.name = concat (scope_name, name, NULL);
10705 vec_safe_push (pubtype_table, e);
10708 /* Although it might be more consistent to add the pubinfo for the
10709 enumerators as their dies are created, they should only be added if the
10710 enum type meets the criteria above. So rather than re-check the parent
10711 enum type whenever an enumerator die is created, just output them all
10712 here. This isn't protected by the name conditional because anonymous
10713 enums don't have names. */
10714 if (die->die_tag == DW_TAG_enumeration_type)
10716 dw_die_ref c;
10718 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10723 /* Output a single entry in the pubnames table. */
10725 static void
10726 output_pubname (dw_offset die_offset, pubname_entry *entry)
10728 dw_die_ref die = entry->die;
10729 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10731 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10733 if (debug_generate_pub_sections == 2)
10735 /* This logic follows gdb's method for determining the value of the flag
10736 byte. */
10737 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10738 switch (die->die_tag)
10740 case DW_TAG_typedef:
10741 case DW_TAG_base_type:
10742 case DW_TAG_subrange_type:
10743 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10744 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10745 break;
10746 case DW_TAG_enumerator:
10747 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10748 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10749 if (!is_cxx ())
10750 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10751 break;
10752 case DW_TAG_subprogram:
10753 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10754 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10755 if (!is_ada ())
10756 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10757 break;
10758 case DW_TAG_constant:
10759 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10760 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10761 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10762 break;
10763 case DW_TAG_variable:
10764 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10765 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10766 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10767 break;
10768 case DW_TAG_namespace:
10769 case DW_TAG_imported_declaration:
10770 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10771 break;
10772 case DW_TAG_class_type:
10773 case DW_TAG_interface_type:
10774 case DW_TAG_structure_type:
10775 case DW_TAG_union_type:
10776 case DW_TAG_enumeration_type:
10777 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10778 if (!is_cxx ())
10779 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10780 break;
10781 default:
10782 /* An unusual tag. Leave the flag-byte empty. */
10783 break;
10785 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10786 "GDB-index flags");
10789 dw2_asm_output_nstring (entry->name, -1, "external name");
10793 /* Output the public names table used to speed up access to externally
10794 visible names; or the public types table used to find type definitions. */
10796 static void
10797 output_pubnames (vec<pubname_entry, va_gc> *names)
10799 unsigned i;
10800 unsigned long pubnames_length = size_of_pubnames (names);
10801 pubname_entry *pub;
10803 if (!XCOFF_DEBUGGING_INFO)
10805 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10806 dw2_asm_output_data (4, 0xffffffff,
10807 "Initial length escape value indicating 64-bit DWARF extension");
10808 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10809 "Pub Info Length");
10812 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10813 dw2_asm_output_data (2, 2, "DWARF Version");
10815 if (dwarf_split_debug_info)
10816 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10817 debug_skeleton_info_section,
10818 "Offset of Compilation Unit Info");
10819 else
10820 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10821 debug_info_section,
10822 "Offset of Compilation Unit Info");
10823 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10824 "Compilation Unit Length");
10826 FOR_EACH_VEC_ELT (*names, i, pub)
10828 if (include_pubname_in_output (names, pub))
10830 dw_offset die_offset = pub->die->die_offset;
10832 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10833 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10834 gcc_assert (pub->die->die_mark);
10836 /* If we're putting types in their own .debug_types sections,
10837 the .debug_pubtypes table will still point to the compile
10838 unit (not the type unit), so we want to use the offset of
10839 the skeleton DIE (if there is one). */
10840 if (pub->die->comdat_type_p && names == pubtype_table)
10842 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10844 if (type_node != NULL)
10845 die_offset = (type_node->skeleton_die != NULL
10846 ? type_node->skeleton_die->die_offset
10847 : comp_unit_die ()->die_offset);
10850 output_pubname (die_offset, pub);
10854 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10857 /* Output public names and types tables if necessary. */
10859 static void
10860 output_pubtables (void)
10862 if (!want_pubnames () || !info_section_emitted)
10863 return;
10865 switch_to_section (debug_pubnames_section);
10866 output_pubnames (pubname_table);
10867 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10868 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10869 simply won't look for the section. */
10870 switch_to_section (debug_pubtypes_section);
10871 output_pubnames (pubtype_table);
10875 /* Output the information that goes into the .debug_aranges table.
10876 Namely, define the beginning and ending address range of the
10877 text section generated for this compilation unit. */
10879 static void
10880 output_aranges (void)
10882 unsigned i;
10883 unsigned long aranges_length = size_of_aranges ();
10885 if (!XCOFF_DEBUGGING_INFO)
10887 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10888 dw2_asm_output_data (4, 0xffffffff,
10889 "Initial length escape value indicating 64-bit DWARF extension");
10890 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10891 "Length of Address Ranges Info");
10894 /* Version number for aranges is still 2, even up to DWARF5. */
10895 dw2_asm_output_data (2, 2, "DWARF Version");
10896 if (dwarf_split_debug_info)
10897 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10898 debug_skeleton_info_section,
10899 "Offset of Compilation Unit Info");
10900 else
10901 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10902 debug_info_section,
10903 "Offset of Compilation Unit Info");
10904 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10905 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10907 /* We need to align to twice the pointer size here. */
10908 if (DWARF_ARANGES_PAD_SIZE)
10910 /* Pad using a 2 byte words so that padding is correct for any
10911 pointer size. */
10912 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10913 2 * DWARF2_ADDR_SIZE);
10914 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10915 dw2_asm_output_data (2, 0, NULL);
10918 /* It is necessary not to output these entries if the sections were
10919 not used; if the sections were not used, the length will be 0 and
10920 the address may end up as 0 if the section is discarded by ld
10921 --gc-sections, leaving an invalid (0, 0) entry that can be
10922 confused with the terminator. */
10923 if (text_section_used)
10925 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10926 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10927 text_section_label, "Length");
10929 if (cold_text_section_used)
10931 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10932 "Address");
10933 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10934 cold_text_section_label, "Length");
10937 if (have_multiple_function_sections)
10939 unsigned fde_idx;
10940 dw_fde_ref fde;
10942 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10944 if (DECL_IGNORED_P (fde->decl))
10945 continue;
10946 if (!fde->in_std_section)
10948 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10949 "Address");
10950 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10951 fde->dw_fde_begin, "Length");
10953 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10955 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10956 "Address");
10957 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10958 fde->dw_fde_second_begin, "Length");
10963 /* Output the terminator words. */
10964 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10965 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10968 /* Add a new entry to .debug_ranges. Return its index into
10969 ranges_table vector. */
10971 static unsigned int
10972 add_ranges_num (int num, bool maybe_new_sec)
10974 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10975 vec_safe_push (ranges_table, r);
10976 return vec_safe_length (ranges_table) - 1;
10979 /* Add a new entry to .debug_ranges corresponding to a block, or a
10980 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10981 this entry might be in a different section from previous range. */
10983 static unsigned int
10984 add_ranges (const_tree block, bool maybe_new_sec)
10986 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
10989 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
10990 chain, or middle entry of a chain that will be directly referred to. */
10992 static void
10993 note_rnglist_head (unsigned int offset)
10995 if (dwarf_version < 5 || (*ranges_table)[offset].label)
10996 return;
10997 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11000 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11001 When using dwarf_split_debug_info, address attributes in dies destined
11002 for the final executable should be direct references--setting the
11003 parameter force_direct ensures this behavior. */
11005 static void
11006 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11007 bool *added, bool force_direct)
11009 unsigned int in_use = vec_safe_length (ranges_by_label);
11010 unsigned int offset;
11011 dw_ranges_by_label rbl = { begin, end };
11012 vec_safe_push (ranges_by_label, rbl);
11013 offset = add_ranges_num (-(int)in_use - 1, true);
11014 if (!*added)
11016 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11017 *added = true;
11018 note_rnglist_head (offset);
11022 /* Emit .debug_ranges section. */
11024 static void
11025 output_ranges (void)
11027 unsigned i;
11028 static const char *const start_fmt = "Offset %#x";
11029 const char *fmt = start_fmt;
11030 dw_ranges *r;
11032 switch_to_section (debug_ranges_section);
11033 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11034 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11036 int block_num = r->num;
11038 if (block_num > 0)
11040 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11041 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11043 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11044 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11046 /* If all code is in the text section, then the compilation
11047 unit base address defaults to DW_AT_low_pc, which is the
11048 base of the text section. */
11049 if (!have_multiple_function_sections)
11051 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11052 text_section_label,
11053 fmt, i * 2 * DWARF2_ADDR_SIZE);
11054 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11055 text_section_label, NULL);
11058 /* Otherwise, the compilation unit base address is zero,
11059 which allows us to use absolute addresses, and not worry
11060 about whether the target supports cross-section
11061 arithmetic. */
11062 else
11064 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11065 fmt, i * 2 * DWARF2_ADDR_SIZE);
11066 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11069 fmt = NULL;
11072 /* Negative block_num stands for an index into ranges_by_label. */
11073 else if (block_num < 0)
11075 int lab_idx = - block_num - 1;
11077 if (!have_multiple_function_sections)
11079 gcc_unreachable ();
11080 #if 0
11081 /* If we ever use add_ranges_by_labels () for a single
11082 function section, all we have to do is to take out
11083 the #if 0 above. */
11084 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11085 (*ranges_by_label)[lab_idx].begin,
11086 text_section_label,
11087 fmt, i * 2 * DWARF2_ADDR_SIZE);
11088 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11089 (*ranges_by_label)[lab_idx].end,
11090 text_section_label, NULL);
11091 #endif
11093 else
11095 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11096 (*ranges_by_label)[lab_idx].begin,
11097 fmt, i * 2 * DWARF2_ADDR_SIZE);
11098 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11099 (*ranges_by_label)[lab_idx].end,
11100 NULL);
11103 else
11105 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11106 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11107 fmt = start_fmt;
11112 /* Non-zero if .debug_line_str should be used for .debug_line section
11113 strings or strings that are likely shareable with those. */
11114 #define DWARF5_USE_DEBUG_LINE_STR \
11115 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11116 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11117 /* FIXME: there is no .debug_line_str.dwo section, \
11118 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11119 && !dwarf_split_debug_info)
11121 /* Assign .debug_rnglists indexes. */
11123 static void
11124 index_rnglists (void)
11126 unsigned i;
11127 dw_ranges *r;
11129 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11130 if (r->label)
11131 r->idx = rnglist_idx++;
11134 /* Emit .debug_rnglists section. */
11136 static void
11137 output_rnglists (void)
11139 unsigned i;
11140 dw_ranges *r;
11141 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11142 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11143 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11145 switch_to_section (debug_ranges_section);
11146 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11147 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11148 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11149 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11150 dw2_asm_output_data (4, 0xffffffff,
11151 "Initial length escape value indicating "
11152 "64-bit DWARF extension");
11153 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11154 "Length of Range Lists");
11155 ASM_OUTPUT_LABEL (asm_out_file, l1);
11156 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11157 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11158 dw2_asm_output_data (1, 0, "Segment Size");
11159 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11160 about relocation sizes and primarily care about the size of .debug*
11161 sections in linked shared libraries and executables, then
11162 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11163 into it are usually larger than just DW_FORM_sec_offset offsets
11164 into the .debug_rnglists section. */
11165 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11166 "Offset Entry Count");
11167 if (dwarf_split_debug_info)
11169 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11170 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11171 if (r->label)
11172 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11173 ranges_base_label, NULL);
11176 const char *lab = "";
11177 unsigned int len = vec_safe_length (ranges_table);
11178 const char *base = NULL;
11179 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11181 int block_num = r->num;
11183 if (r->label)
11185 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11186 lab = r->label;
11188 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11189 base = NULL;
11190 if (block_num > 0)
11192 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11193 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11195 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11196 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11198 if (HAVE_AS_LEB128)
11200 /* If all code is in the text section, then the compilation
11201 unit base address defaults to DW_AT_low_pc, which is the
11202 base of the text section. */
11203 if (!have_multiple_function_sections)
11205 dw2_asm_output_data (1, DW_RLE_offset_pair,
11206 "DW_RLE_offset_pair (%s)", lab);
11207 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11208 "Range begin address (%s)", lab);
11209 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11210 "Range end address (%s)", lab);
11211 continue;
11213 if (base == NULL)
11215 dw_ranges *r2 = NULL;
11216 if (i < len - 1)
11217 r2 = &(*ranges_table)[i + 1];
11218 if (r2
11219 && r2->num != 0
11220 && r2->label == NULL
11221 && !r2->maybe_new_sec)
11223 dw2_asm_output_data (1, DW_RLE_base_address,
11224 "DW_RLE_base_address (%s)", lab);
11225 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11226 "Base address (%s)", lab);
11227 strcpy (basebuf, blabel);
11228 base = basebuf;
11231 if (base)
11233 dw2_asm_output_data (1, DW_RLE_offset_pair,
11234 "DW_RLE_offset_pair (%s)", lab);
11235 dw2_asm_output_delta_uleb128 (blabel, base,
11236 "Range begin address (%s)", lab);
11237 dw2_asm_output_delta_uleb128 (elabel, base,
11238 "Range end address (%s)", lab);
11239 continue;
11241 dw2_asm_output_data (1, DW_RLE_start_length,
11242 "DW_RLE_start_length (%s)", lab);
11243 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11244 "Range begin address (%s)", lab);
11245 dw2_asm_output_delta_uleb128 (elabel, blabel,
11246 "Range length (%s)", lab);
11248 else
11250 dw2_asm_output_data (1, DW_RLE_start_end,
11251 "DW_RLE_start_end (%s)", lab);
11252 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11253 "Range begin address (%s)", lab);
11254 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11255 "Range end address (%s)", lab);
11259 /* Negative block_num stands for an index into ranges_by_label. */
11260 else if (block_num < 0)
11262 int lab_idx = - block_num - 1;
11263 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11264 const char *elabel = (*ranges_by_label)[lab_idx].end;
11266 if (!have_multiple_function_sections)
11267 gcc_unreachable ();
11268 if (HAVE_AS_LEB128)
11270 dw2_asm_output_data (1, DW_RLE_start_length,
11271 "DW_RLE_start_length (%s)", lab);
11272 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11273 "Range begin address (%s)", lab);
11274 dw2_asm_output_delta_uleb128 (elabel, blabel,
11275 "Range length (%s)", lab);
11277 else
11279 dw2_asm_output_data (1, DW_RLE_start_end,
11280 "DW_RLE_start_end (%s)", lab);
11281 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11282 "Range begin address (%s)", lab);
11283 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11284 "Range end address (%s)", lab);
11287 else
11288 dw2_asm_output_data (1, DW_RLE_end_of_list,
11289 "DW_RLE_end_of_list (%s)", lab);
11291 ASM_OUTPUT_LABEL (asm_out_file, l2);
11294 /* Data structure containing information about input files. */
11295 struct file_info
11297 const char *path; /* Complete file name. */
11298 const char *fname; /* File name part. */
11299 int length; /* Length of entire string. */
11300 struct dwarf_file_data * file_idx; /* Index in input file table. */
11301 int dir_idx; /* Index in directory table. */
11304 /* Data structure containing information about directories with source
11305 files. */
11306 struct dir_info
11308 const char *path; /* Path including directory name. */
11309 int length; /* Path length. */
11310 int prefix; /* Index of directory entry which is a prefix. */
11311 int count; /* Number of files in this directory. */
11312 int dir_idx; /* Index of directory used as base. */
11315 /* Callback function for file_info comparison. We sort by looking at
11316 the directories in the path. */
11318 static int
11319 file_info_cmp (const void *p1, const void *p2)
11321 const struct file_info *const s1 = (const struct file_info *) p1;
11322 const struct file_info *const s2 = (const struct file_info *) p2;
11323 const unsigned char *cp1;
11324 const unsigned char *cp2;
11326 /* Take care of file names without directories. We need to make sure that
11327 we return consistent values to qsort since some will get confused if
11328 we return the same value when identical operands are passed in opposite
11329 orders. So if neither has a directory, return 0 and otherwise return
11330 1 or -1 depending on which one has the directory. */
11331 if ((s1->path == s1->fname || s2->path == s2->fname))
11332 return (s2->path == s2->fname) - (s1->path == s1->fname);
11334 cp1 = (const unsigned char *) s1->path;
11335 cp2 = (const unsigned char *) s2->path;
11337 while (1)
11339 ++cp1;
11340 ++cp2;
11341 /* Reached the end of the first path? If so, handle like above. */
11342 if ((cp1 == (const unsigned char *) s1->fname)
11343 || (cp2 == (const unsigned char *) s2->fname))
11344 return ((cp2 == (const unsigned char *) s2->fname)
11345 - (cp1 == (const unsigned char *) s1->fname));
11347 /* Character of current path component the same? */
11348 else if (*cp1 != *cp2)
11349 return *cp1 - *cp2;
11353 struct file_name_acquire_data
11355 struct file_info *files;
11356 int used_files;
11357 int max_files;
11360 /* Traversal function for the hash table. */
11363 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11365 struct dwarf_file_data *d = *slot;
11366 struct file_info *fi;
11367 const char *f;
11369 gcc_assert (fnad->max_files >= d->emitted_number);
11371 if (! d->emitted_number)
11372 return 1;
11374 gcc_assert (fnad->max_files != fnad->used_files);
11376 fi = fnad->files + fnad->used_files++;
11378 /* Skip all leading "./". */
11379 f = d->filename;
11380 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11381 f += 2;
11383 /* Create a new array entry. */
11384 fi->path = f;
11385 fi->length = strlen (f);
11386 fi->file_idx = d;
11388 /* Search for the file name part. */
11389 f = strrchr (f, DIR_SEPARATOR);
11390 #if defined (DIR_SEPARATOR_2)
11392 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11394 if (g != NULL)
11396 if (f == NULL || f < g)
11397 f = g;
11400 #endif
11402 fi->fname = f == NULL ? fi->path : f + 1;
11403 return 1;
11406 /* Helper function for output_file_names. Emit a FORM encoded
11407 string STR, with assembly comment start ENTRY_KIND and
11408 index IDX */
11410 static void
11411 output_line_string (enum dwarf_form form, const char *str,
11412 const char *entry_kind, unsigned int idx)
11414 switch (form)
11416 case DW_FORM_string:
11417 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11418 break;
11419 case DW_FORM_line_strp:
11420 if (!debug_line_str_hash)
11421 debug_line_str_hash
11422 = hash_table<indirect_string_hasher>::create_ggc (10);
11424 struct indirect_string_node *node;
11425 node = find_AT_string_in_table (str, debug_line_str_hash);
11426 set_indirect_string (node);
11427 node->form = form;
11428 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11429 debug_line_str_section, "%s: %#x: \"%s\"",
11430 entry_kind, 0, node->str);
11431 break;
11432 default:
11433 gcc_unreachable ();
11437 /* Output the directory table and the file name table. We try to minimize
11438 the total amount of memory needed. A heuristic is used to avoid large
11439 slowdowns with many input files. */
11441 static void
11442 output_file_names (void)
11444 struct file_name_acquire_data fnad;
11445 int numfiles;
11446 struct file_info *files;
11447 struct dir_info *dirs;
11448 int *saved;
11449 int *savehere;
11450 int *backmap;
11451 int ndirs;
11452 int idx_offset;
11453 int i;
11455 if (!last_emitted_file)
11457 if (dwarf_version >= 5)
11459 dw2_asm_output_data (1, 0, "Directory entry format count");
11460 dw2_asm_output_data_uleb128 (0, "Directories count");
11461 dw2_asm_output_data (1, 0, "File name entry format count");
11462 dw2_asm_output_data_uleb128 (0, "File names count");
11464 else
11466 dw2_asm_output_data (1, 0, "End directory table");
11467 dw2_asm_output_data (1, 0, "End file name table");
11469 return;
11472 numfiles = last_emitted_file->emitted_number;
11474 /* Allocate the various arrays we need. */
11475 files = XALLOCAVEC (struct file_info, numfiles);
11476 dirs = XALLOCAVEC (struct dir_info, numfiles);
11478 fnad.files = files;
11479 fnad.used_files = 0;
11480 fnad.max_files = numfiles;
11481 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11482 gcc_assert (fnad.used_files == fnad.max_files);
11484 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11486 /* Find all the different directories used. */
11487 dirs[0].path = files[0].path;
11488 dirs[0].length = files[0].fname - files[0].path;
11489 dirs[0].prefix = -1;
11490 dirs[0].count = 1;
11491 dirs[0].dir_idx = 0;
11492 files[0].dir_idx = 0;
11493 ndirs = 1;
11495 for (i = 1; i < numfiles; i++)
11496 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11497 && memcmp (dirs[ndirs - 1].path, files[i].path,
11498 dirs[ndirs - 1].length) == 0)
11500 /* Same directory as last entry. */
11501 files[i].dir_idx = ndirs - 1;
11502 ++dirs[ndirs - 1].count;
11504 else
11506 int j;
11508 /* This is a new directory. */
11509 dirs[ndirs].path = files[i].path;
11510 dirs[ndirs].length = files[i].fname - files[i].path;
11511 dirs[ndirs].count = 1;
11512 dirs[ndirs].dir_idx = ndirs;
11513 files[i].dir_idx = ndirs;
11515 /* Search for a prefix. */
11516 dirs[ndirs].prefix = -1;
11517 for (j = 0; j < ndirs; j++)
11518 if (dirs[j].length < dirs[ndirs].length
11519 && dirs[j].length > 1
11520 && (dirs[ndirs].prefix == -1
11521 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11522 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11523 dirs[ndirs].prefix = j;
11525 ++ndirs;
11528 /* Now to the actual work. We have to find a subset of the directories which
11529 allow expressing the file name using references to the directory table
11530 with the least amount of characters. We do not do an exhaustive search
11531 where we would have to check out every combination of every single
11532 possible prefix. Instead we use a heuristic which provides nearly optimal
11533 results in most cases and never is much off. */
11534 saved = XALLOCAVEC (int, ndirs);
11535 savehere = XALLOCAVEC (int, ndirs);
11537 memset (saved, '\0', ndirs * sizeof (saved[0]));
11538 for (i = 0; i < ndirs; i++)
11540 int j;
11541 int total;
11543 /* We can always save some space for the current directory. But this
11544 does not mean it will be enough to justify adding the directory. */
11545 savehere[i] = dirs[i].length;
11546 total = (savehere[i] - saved[i]) * dirs[i].count;
11548 for (j = i + 1; j < ndirs; j++)
11550 savehere[j] = 0;
11551 if (saved[j] < dirs[i].length)
11553 /* Determine whether the dirs[i] path is a prefix of the
11554 dirs[j] path. */
11555 int k;
11557 k = dirs[j].prefix;
11558 while (k != -1 && k != (int) i)
11559 k = dirs[k].prefix;
11561 if (k == (int) i)
11563 /* Yes it is. We can possibly save some memory by
11564 writing the filenames in dirs[j] relative to
11565 dirs[i]. */
11566 savehere[j] = dirs[i].length;
11567 total += (savehere[j] - saved[j]) * dirs[j].count;
11572 /* Check whether we can save enough to justify adding the dirs[i]
11573 directory. */
11574 if (total > dirs[i].length + 1)
11576 /* It's worthwhile adding. */
11577 for (j = i; j < ndirs; j++)
11578 if (savehere[j] > 0)
11580 /* Remember how much we saved for this directory so far. */
11581 saved[j] = savehere[j];
11583 /* Remember the prefix directory. */
11584 dirs[j].dir_idx = i;
11589 /* Emit the directory name table. */
11590 idx_offset = dirs[0].length > 0 ? 1 : 0;
11591 enum dwarf_form str_form = DW_FORM_string;
11592 enum dwarf_form idx_form = DW_FORM_udata;
11593 if (dwarf_version >= 5)
11595 const char *comp_dir = comp_dir_string ();
11596 if (comp_dir == NULL)
11597 comp_dir = "";
11598 dw2_asm_output_data (1, 1, "Directory entry format count");
11599 if (DWARF5_USE_DEBUG_LINE_STR)
11600 str_form = DW_FORM_line_strp;
11601 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11602 dw2_asm_output_data_uleb128 (str_form, "%s",
11603 get_DW_FORM_name (str_form));
11604 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11605 if (str_form == DW_FORM_string)
11607 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11608 for (i = 1 - idx_offset; i < ndirs; i++)
11609 dw2_asm_output_nstring (dirs[i].path,
11610 dirs[i].length
11611 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11612 "Directory Entry: %#x", i + idx_offset);
11614 else
11616 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11617 for (i = 1 - idx_offset; i < ndirs; i++)
11619 const char *str
11620 = ggc_alloc_string (dirs[i].path,
11621 dirs[i].length
11622 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11623 output_line_string (str_form, str, "Directory Entry",
11624 (unsigned) i + idx_offset);
11628 else
11630 for (i = 1 - idx_offset; i < ndirs; i++)
11631 dw2_asm_output_nstring (dirs[i].path,
11632 dirs[i].length
11633 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11634 "Directory Entry: %#x", i + idx_offset);
11636 dw2_asm_output_data (1, 0, "End directory table");
11639 /* We have to emit them in the order of emitted_number since that's
11640 used in the debug info generation. To do this efficiently we
11641 generate a back-mapping of the indices first. */
11642 backmap = XALLOCAVEC (int, numfiles);
11643 for (i = 0; i < numfiles; i++)
11644 backmap[files[i].file_idx->emitted_number - 1] = i;
11646 if (dwarf_version >= 5)
11648 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11649 if (filename0 == NULL)
11650 filename0 = "";
11651 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11652 DW_FORM_data2. Choose one based on the number of directories
11653 and how much space would they occupy in each encoding.
11654 If we have at most 256 directories, all indexes fit into
11655 a single byte, so DW_FORM_data1 is most compact (if there
11656 are at most 128 directories, DW_FORM_udata would be as
11657 compact as that, but not shorter and slower to decode). */
11658 if (ndirs + idx_offset <= 256)
11659 idx_form = DW_FORM_data1;
11660 /* If there are more than 65536 directories, we have to use
11661 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11662 Otherwise, compute what space would occupy if all the indexes
11663 used DW_FORM_udata - sum - and compare that to how large would
11664 be DW_FORM_data2 encoding, and pick the more efficient one. */
11665 else if (ndirs + idx_offset <= 65536)
11667 unsigned HOST_WIDE_INT sum = 1;
11668 for (i = 0; i < numfiles; i++)
11670 int file_idx = backmap[i];
11671 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11672 sum += size_of_uleb128 (dir_idx);
11674 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11675 idx_form = DW_FORM_data2;
11677 #ifdef VMS_DEBUGGING_INFO
11678 dw2_asm_output_data (1, 4, "File name entry format count");
11679 #else
11680 dw2_asm_output_data (1, 2, "File name entry format count");
11681 #endif
11682 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11683 dw2_asm_output_data_uleb128 (str_form, "%s",
11684 get_DW_FORM_name (str_form));
11685 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11686 "DW_LNCT_directory_index");
11687 dw2_asm_output_data_uleb128 (idx_form, "%s",
11688 get_DW_FORM_name (idx_form));
11689 #ifdef VMS_DEBUGGING_INFO
11690 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11691 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11692 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11693 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11694 #endif
11695 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11697 output_line_string (str_form, filename0, "File Entry", 0);
11699 /* Include directory index. */
11700 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11701 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11702 0, NULL);
11703 else
11704 dw2_asm_output_data_uleb128 (0, NULL);
11706 #ifdef VMS_DEBUGGING_INFO
11707 dw2_asm_output_data_uleb128 (0, NULL);
11708 dw2_asm_output_data_uleb128 (0, NULL);
11709 #endif
11712 /* Now write all the file names. */
11713 for (i = 0; i < numfiles; i++)
11715 int file_idx = backmap[i];
11716 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11718 #ifdef VMS_DEBUGGING_INFO
11719 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11721 /* Setting these fields can lead to debugger miscomparisons,
11722 but VMS Debug requires them to be set correctly. */
11724 int ver;
11725 long long cdt;
11726 long siz;
11727 int maxfilelen = (strlen (files[file_idx].path)
11728 + dirs[dir_idx].length
11729 + MAX_VMS_VERSION_LEN + 1);
11730 char *filebuf = XALLOCAVEC (char, maxfilelen);
11732 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11733 snprintf (filebuf, maxfilelen, "%s;%d",
11734 files[file_idx].path + dirs[dir_idx].length, ver);
11736 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11738 /* Include directory index. */
11739 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11740 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11741 dir_idx + idx_offset, NULL);
11742 else
11743 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11745 /* Modification time. */
11746 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11747 &cdt, 0, 0, 0) == 0)
11748 ? cdt : 0, NULL);
11750 /* File length in bytes. */
11751 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11752 0, &siz, 0, 0) == 0)
11753 ? siz : 0, NULL);
11754 #else
11755 output_line_string (str_form,
11756 files[file_idx].path + dirs[dir_idx].length,
11757 "File Entry", (unsigned) i + 1);
11759 /* Include directory index. */
11760 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11761 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11762 dir_idx + idx_offset, NULL);
11763 else
11764 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11766 if (dwarf_version >= 5)
11767 continue;
11769 /* Modification time. */
11770 dw2_asm_output_data_uleb128 (0, NULL);
11772 /* File length in bytes. */
11773 dw2_asm_output_data_uleb128 (0, NULL);
11774 #endif /* VMS_DEBUGGING_INFO */
11777 if (dwarf_version < 5)
11778 dw2_asm_output_data (1, 0, "End file name table");
11782 /* Output one line number table into the .debug_line section. */
11784 static void
11785 output_one_line_info_table (dw_line_info_table *table)
11787 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11788 unsigned int current_line = 1;
11789 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11790 dw_line_info_entry *ent;
11791 size_t i;
11793 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11795 switch (ent->opcode)
11797 case LI_set_address:
11798 /* ??? Unfortunately, we have little choice here currently, and
11799 must always use the most general form. GCC does not know the
11800 address delta itself, so we can't use DW_LNS_advance_pc. Many
11801 ports do have length attributes which will give an upper bound
11802 on the address range. We could perhaps use length attributes
11803 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11804 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11806 /* This can handle any delta. This takes
11807 4+DWARF2_ADDR_SIZE bytes. */
11808 dw2_asm_output_data (1, 0, "set address %s", line_label);
11809 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11810 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11811 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11812 break;
11814 case LI_set_line:
11815 if (ent->val == current_line)
11817 /* We still need to start a new row, so output a copy insn. */
11818 dw2_asm_output_data (1, DW_LNS_copy,
11819 "copy line %u", current_line);
11821 else
11823 int line_offset = ent->val - current_line;
11824 int line_delta = line_offset - DWARF_LINE_BASE;
11826 current_line = ent->val;
11827 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11829 /* This can handle deltas from -10 to 234, using the current
11830 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11831 This takes 1 byte. */
11832 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11833 "line %u", current_line);
11835 else
11837 /* This can handle any delta. This takes at least 4 bytes,
11838 depending on the value being encoded. */
11839 dw2_asm_output_data (1, DW_LNS_advance_line,
11840 "advance to line %u", current_line);
11841 dw2_asm_output_data_sleb128 (line_offset, NULL);
11842 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11845 break;
11847 case LI_set_file:
11848 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11849 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11850 break;
11852 case LI_set_column:
11853 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11854 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11855 break;
11857 case LI_negate_stmt:
11858 current_is_stmt = !current_is_stmt;
11859 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11860 "is_stmt %d", current_is_stmt);
11861 break;
11863 case LI_set_prologue_end:
11864 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11865 "set prologue end");
11866 break;
11868 case LI_set_epilogue_begin:
11869 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11870 "set epilogue begin");
11871 break;
11873 case LI_set_discriminator:
11874 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11875 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11876 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11877 dw2_asm_output_data_uleb128 (ent->val, NULL);
11878 break;
11882 /* Emit debug info for the address of the end of the table. */
11883 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11884 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11885 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11886 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11888 dw2_asm_output_data (1, 0, "end sequence");
11889 dw2_asm_output_data_uleb128 (1, NULL);
11890 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11893 /* Output the source line number correspondence information. This
11894 information goes into the .debug_line section. */
11896 static void
11897 output_line_info (bool prologue_only)
11899 static unsigned int generation;
11900 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11901 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11902 bool saw_one = false;
11903 int opc;
11905 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11906 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11907 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11908 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11910 if (!XCOFF_DEBUGGING_INFO)
11912 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11913 dw2_asm_output_data (4, 0xffffffff,
11914 "Initial length escape value indicating 64-bit DWARF extension");
11915 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11916 "Length of Source Line Info");
11919 ASM_OUTPUT_LABEL (asm_out_file, l1);
11921 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11922 if (dwarf_version >= 5)
11924 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11925 dw2_asm_output_data (1, 0, "Segment Size");
11927 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11928 ASM_OUTPUT_LABEL (asm_out_file, p1);
11930 /* Define the architecture-dependent minimum instruction length (in bytes).
11931 In this implementation of DWARF, this field is used for information
11932 purposes only. Since GCC generates assembly language, we have no
11933 a priori knowledge of how many instruction bytes are generated for each
11934 source line, and therefore can use only the DW_LNE_set_address and
11935 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11936 this as '1', which is "correct enough" for all architectures,
11937 and don't let the target override. */
11938 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11940 if (dwarf_version >= 4)
11941 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11942 "Maximum Operations Per Instruction");
11943 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11944 "Default is_stmt_start flag");
11945 dw2_asm_output_data (1, DWARF_LINE_BASE,
11946 "Line Base Value (Special Opcodes)");
11947 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11948 "Line Range Value (Special Opcodes)");
11949 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11950 "Special Opcode Base");
11952 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11954 int n_op_args;
11955 switch (opc)
11957 case DW_LNS_advance_pc:
11958 case DW_LNS_advance_line:
11959 case DW_LNS_set_file:
11960 case DW_LNS_set_column:
11961 case DW_LNS_fixed_advance_pc:
11962 case DW_LNS_set_isa:
11963 n_op_args = 1;
11964 break;
11965 default:
11966 n_op_args = 0;
11967 break;
11970 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11971 opc, n_op_args);
11974 /* Write out the information about the files we use. */
11975 output_file_names ();
11976 ASM_OUTPUT_LABEL (asm_out_file, p2);
11977 if (prologue_only)
11979 /* Output the marker for the end of the line number info. */
11980 ASM_OUTPUT_LABEL (asm_out_file, l2);
11981 return;
11984 if (separate_line_info)
11986 dw_line_info_table *table;
11987 size_t i;
11989 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
11990 if (table->in_use)
11992 output_one_line_info_table (table);
11993 saw_one = true;
11996 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
11998 output_one_line_info_table (cold_text_section_line_info);
11999 saw_one = true;
12002 /* ??? Some Darwin linkers crash on a .debug_line section with no
12003 sequences. Further, merely a DW_LNE_end_sequence entry is not
12004 sufficient -- the address column must also be initialized.
12005 Make sure to output at least one set_address/end_sequence pair,
12006 choosing .text since that section is always present. */
12007 if (text_section_line_info->in_use || !saw_one)
12008 output_one_line_info_table (text_section_line_info);
12010 /* Output the marker for the end of the line number info. */
12011 ASM_OUTPUT_LABEL (asm_out_file, l2);
12014 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12016 static inline bool
12017 need_endianity_attribute_p (bool reverse)
12019 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12022 /* Given a pointer to a tree node for some base type, return a pointer to
12023 a DIE that describes the given type. REVERSE is true if the type is
12024 to be interpreted in the reverse storage order wrt the target order.
12026 This routine must only be called for GCC type nodes that correspond to
12027 Dwarf base (fundamental) types. */
12029 static dw_die_ref
12030 base_type_die (tree type, bool reverse)
12032 dw_die_ref base_type_result;
12033 enum dwarf_type encoding;
12034 bool fpt_used = false;
12035 struct fixed_point_type_info fpt_info;
12036 tree type_bias = NULL_TREE;
12038 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12039 return 0;
12041 /* If this is a subtype that should not be emitted as a subrange type,
12042 use the base type. See subrange_type_for_debug_p. */
12043 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12044 type = TREE_TYPE (type);
12046 switch (TREE_CODE (type))
12048 case INTEGER_TYPE:
12049 if ((dwarf_version >= 4 || !dwarf_strict)
12050 && TYPE_NAME (type)
12051 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12052 && DECL_IS_BUILTIN (TYPE_NAME (type))
12053 && DECL_NAME (TYPE_NAME (type)))
12055 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12056 if (strcmp (name, "char16_t") == 0
12057 || strcmp (name, "char32_t") == 0)
12059 encoding = DW_ATE_UTF;
12060 break;
12063 if ((dwarf_version >= 3 || !dwarf_strict)
12064 && lang_hooks.types.get_fixed_point_type_info)
12066 memset (&fpt_info, 0, sizeof (fpt_info));
12067 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12069 fpt_used = true;
12070 encoding = ((TYPE_UNSIGNED (type))
12071 ? DW_ATE_unsigned_fixed
12072 : DW_ATE_signed_fixed);
12073 break;
12076 if (TYPE_STRING_FLAG (type))
12078 if (TYPE_UNSIGNED (type))
12079 encoding = DW_ATE_unsigned_char;
12080 else
12081 encoding = DW_ATE_signed_char;
12083 else if (TYPE_UNSIGNED (type))
12084 encoding = DW_ATE_unsigned;
12085 else
12086 encoding = DW_ATE_signed;
12088 if (!dwarf_strict
12089 && lang_hooks.types.get_type_bias)
12090 type_bias = lang_hooks.types.get_type_bias (type);
12091 break;
12093 case REAL_TYPE:
12094 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12096 if (dwarf_version >= 3 || !dwarf_strict)
12097 encoding = DW_ATE_decimal_float;
12098 else
12099 encoding = DW_ATE_lo_user;
12101 else
12102 encoding = DW_ATE_float;
12103 break;
12105 case FIXED_POINT_TYPE:
12106 if (!(dwarf_version >= 3 || !dwarf_strict))
12107 encoding = DW_ATE_lo_user;
12108 else if (TYPE_UNSIGNED (type))
12109 encoding = DW_ATE_unsigned_fixed;
12110 else
12111 encoding = DW_ATE_signed_fixed;
12112 break;
12114 /* Dwarf2 doesn't know anything about complex ints, so use
12115 a user defined type for it. */
12116 case COMPLEX_TYPE:
12117 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12118 encoding = DW_ATE_complex_float;
12119 else
12120 encoding = DW_ATE_lo_user;
12121 break;
12123 case BOOLEAN_TYPE:
12124 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12125 encoding = DW_ATE_boolean;
12126 break;
12128 default:
12129 /* No other TREE_CODEs are Dwarf fundamental types. */
12130 gcc_unreachable ();
12133 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12135 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12136 int_size_in_bytes (type));
12137 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12139 if (need_endianity_attribute_p (reverse))
12140 add_AT_unsigned (base_type_result, DW_AT_endianity,
12141 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12143 add_alignment_attribute (base_type_result, type);
12145 if (fpt_used)
12147 switch (fpt_info.scale_factor_kind)
12149 case fixed_point_scale_factor_binary:
12150 add_AT_int (base_type_result, DW_AT_binary_scale,
12151 fpt_info.scale_factor.binary);
12152 break;
12154 case fixed_point_scale_factor_decimal:
12155 add_AT_int (base_type_result, DW_AT_decimal_scale,
12156 fpt_info.scale_factor.decimal);
12157 break;
12159 case fixed_point_scale_factor_arbitrary:
12160 /* Arbitrary scale factors cannot be described in standard DWARF,
12161 yet. */
12162 if (!dwarf_strict)
12164 /* Describe the scale factor as a rational constant. */
12165 const dw_die_ref scale_factor
12166 = new_die (DW_TAG_constant, comp_unit_die (), type);
12168 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12169 fpt_info.scale_factor.arbitrary.numerator);
12170 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12171 fpt_info.scale_factor.arbitrary.denominator);
12173 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12175 break;
12177 default:
12178 gcc_unreachable ();
12182 if (type_bias)
12183 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12184 dw_scalar_form_constant
12185 | dw_scalar_form_exprloc
12186 | dw_scalar_form_reference,
12187 NULL);
12189 add_pubtype (type, base_type_result);
12191 return base_type_result;
12194 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12195 named 'auto' in its type: return true for it, false otherwise. */
12197 static inline bool
12198 is_cxx_auto (tree type)
12200 if (is_cxx ())
12202 tree name = TYPE_IDENTIFIER (type);
12203 if (name == get_identifier ("auto")
12204 || name == get_identifier ("decltype(auto)"))
12205 return true;
12207 return false;
12210 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12211 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12213 static inline int
12214 is_base_type (tree type)
12216 switch (TREE_CODE (type))
12218 case ERROR_MARK:
12219 case VOID_TYPE:
12220 case INTEGER_TYPE:
12221 case REAL_TYPE:
12222 case FIXED_POINT_TYPE:
12223 case COMPLEX_TYPE:
12224 case BOOLEAN_TYPE:
12225 case POINTER_BOUNDS_TYPE:
12226 return 1;
12228 case ARRAY_TYPE:
12229 case RECORD_TYPE:
12230 case UNION_TYPE:
12231 case QUAL_UNION_TYPE:
12232 case ENUMERAL_TYPE:
12233 case FUNCTION_TYPE:
12234 case METHOD_TYPE:
12235 case POINTER_TYPE:
12236 case REFERENCE_TYPE:
12237 case NULLPTR_TYPE:
12238 case OFFSET_TYPE:
12239 case LANG_TYPE:
12240 case VECTOR_TYPE:
12241 return 0;
12243 default:
12244 if (is_cxx_auto (type))
12245 return 0;
12246 gcc_unreachable ();
12249 return 0;
12252 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12253 node, return the size in bits for the type if it is a constant, or else
12254 return the alignment for the type if the type's size is not constant, or
12255 else return BITS_PER_WORD if the type actually turns out to be an
12256 ERROR_MARK node. */
12258 static inline unsigned HOST_WIDE_INT
12259 simple_type_size_in_bits (const_tree type)
12261 if (TREE_CODE (type) == ERROR_MARK)
12262 return BITS_PER_WORD;
12263 else if (TYPE_SIZE (type) == NULL_TREE)
12264 return 0;
12265 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12266 return tree_to_uhwi (TYPE_SIZE (type));
12267 else
12268 return TYPE_ALIGN (type);
12271 /* Similarly, but return an offset_int instead of UHWI. */
12273 static inline offset_int
12274 offset_int_type_size_in_bits (const_tree type)
12276 if (TREE_CODE (type) == ERROR_MARK)
12277 return BITS_PER_WORD;
12278 else if (TYPE_SIZE (type) == NULL_TREE)
12279 return 0;
12280 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12281 return wi::to_offset (TYPE_SIZE (type));
12282 else
12283 return TYPE_ALIGN (type);
12286 /* Given a pointer to a tree node for a subrange type, return a pointer
12287 to a DIE that describes the given type. */
12289 static dw_die_ref
12290 subrange_type_die (tree type, tree low, tree high, tree bias,
12291 dw_die_ref context_die)
12293 dw_die_ref subrange_die;
12294 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12296 if (context_die == NULL)
12297 context_die = comp_unit_die ();
12299 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12301 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12303 /* The size of the subrange type and its base type do not match,
12304 so we need to generate a size attribute for the subrange type. */
12305 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12308 add_alignment_attribute (subrange_die, type);
12310 if (low)
12311 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12312 if (high)
12313 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12314 if (bias && !dwarf_strict)
12315 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12316 dw_scalar_form_constant
12317 | dw_scalar_form_exprloc
12318 | dw_scalar_form_reference,
12319 NULL);
12321 return subrange_die;
12324 /* Returns the (const and/or volatile) cv_qualifiers associated with
12325 the decl node. This will normally be augmented with the
12326 cv_qualifiers of the underlying type in add_type_attribute. */
12328 static int
12329 decl_quals (const_tree decl)
12331 return ((TREE_READONLY (decl)
12332 /* The C++ front-end correctly marks reference-typed
12333 variables as readonly, but from a language (and debug
12334 info) standpoint they are not const-qualified. */
12335 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12336 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12337 | (TREE_THIS_VOLATILE (decl)
12338 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12341 /* Determine the TYPE whose qualifiers match the largest strict subset
12342 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12343 qualifiers outside QUAL_MASK. */
12345 static int
12346 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12348 tree t;
12349 int best_rank = 0, best_qual = 0, max_rank;
12351 type_quals &= qual_mask;
12352 max_rank = popcount_hwi (type_quals) - 1;
12354 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12355 t = TYPE_NEXT_VARIANT (t))
12357 int q = TYPE_QUALS (t) & qual_mask;
12359 if ((q & type_quals) == q && q != type_quals
12360 && check_base_type (t, type))
12362 int rank = popcount_hwi (q);
12364 if (rank > best_rank)
12366 best_rank = rank;
12367 best_qual = q;
12372 return best_qual;
12375 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12376 static const dwarf_qual_info_t dwarf_qual_info[] =
12378 { TYPE_QUAL_CONST, DW_TAG_const_type },
12379 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12380 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12381 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12383 static const unsigned int dwarf_qual_info_size
12384 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12386 /* If DIE is a qualified DIE of some base DIE with the same parent,
12387 return the base DIE, otherwise return NULL. Set MASK to the
12388 qualifiers added compared to the returned DIE. */
12390 static dw_die_ref
12391 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12393 unsigned int i;
12394 for (i = 0; i < dwarf_qual_info_size; i++)
12395 if (die->die_tag == dwarf_qual_info[i].t)
12396 break;
12397 if (i == dwarf_qual_info_size)
12398 return NULL;
12399 if (vec_safe_length (die->die_attr) != 1)
12400 return NULL;
12401 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12402 if (type == NULL || type->die_parent != die->die_parent)
12403 return NULL;
12404 *mask |= dwarf_qual_info[i].q;
12405 if (depth)
12407 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12408 if (ret)
12409 return ret;
12411 return type;
12414 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12415 entry that chains the modifiers specified by CV_QUALS in front of the
12416 given type. REVERSE is true if the type is to be interpreted in the
12417 reverse storage order wrt the target order. */
12419 static dw_die_ref
12420 modified_type_die (tree type, int cv_quals, bool reverse,
12421 dw_die_ref context_die)
12423 enum tree_code code = TREE_CODE (type);
12424 dw_die_ref mod_type_die;
12425 dw_die_ref sub_die = NULL;
12426 tree item_type = NULL;
12427 tree qualified_type;
12428 tree name, low, high;
12429 dw_die_ref mod_scope;
12430 /* Only these cv-qualifiers are currently handled. */
12431 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12432 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12434 if (code == ERROR_MARK)
12435 return NULL;
12437 if (lang_hooks.types.get_debug_type)
12439 tree debug_type = lang_hooks.types.get_debug_type (type);
12441 if (debug_type != NULL_TREE && debug_type != type)
12442 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12445 cv_quals &= cv_qual_mask;
12447 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12448 tag modifier (and not an attribute) old consumers won't be able
12449 to handle it. */
12450 if (dwarf_version < 3)
12451 cv_quals &= ~TYPE_QUAL_RESTRICT;
12453 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12454 if (dwarf_version < 5)
12455 cv_quals &= ~TYPE_QUAL_ATOMIC;
12457 /* See if we already have the appropriately qualified variant of
12458 this type. */
12459 qualified_type = get_qualified_type (type, cv_quals);
12461 if (qualified_type == sizetype)
12463 /* Try not to expose the internal sizetype type's name. */
12464 if (TYPE_NAME (qualified_type)
12465 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12467 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12469 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12470 && (TYPE_PRECISION (t)
12471 == TYPE_PRECISION (qualified_type))
12472 && (TYPE_UNSIGNED (t)
12473 == TYPE_UNSIGNED (qualified_type)));
12474 qualified_type = t;
12476 else if (qualified_type == sizetype
12477 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12478 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12479 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12480 qualified_type = size_type_node;
12484 /* If we do, then we can just use its DIE, if it exists. */
12485 if (qualified_type)
12487 mod_type_die = lookup_type_die (qualified_type);
12489 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12490 if (mod_type_die
12491 && (!need_endianity_attribute_p (reverse)
12492 || !is_base_type (type)
12493 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12494 return mod_type_die;
12497 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12499 /* Handle C typedef types. */
12500 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12501 && !DECL_ARTIFICIAL (name))
12503 tree dtype = TREE_TYPE (name);
12505 if (qualified_type == dtype)
12507 /* For a named type, use the typedef. */
12508 gen_type_die (qualified_type, context_die);
12509 return lookup_type_die (qualified_type);
12511 else
12513 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12514 dquals &= cv_qual_mask;
12515 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12516 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12517 /* cv-unqualified version of named type. Just use
12518 the unnamed type to which it refers. */
12519 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12520 reverse, context_die);
12521 /* Else cv-qualified version of named type; fall through. */
12525 mod_scope = scope_die_for (type, context_die);
12527 if (cv_quals)
12529 int sub_quals = 0, first_quals = 0;
12530 unsigned i;
12531 dw_die_ref first = NULL, last = NULL;
12533 /* Determine a lesser qualified type that most closely matches
12534 this one. Then generate DW_TAG_* entries for the remaining
12535 qualifiers. */
12536 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12537 cv_qual_mask);
12538 if (sub_quals && use_debug_types)
12540 bool needed = false;
12541 /* If emitting type units, make sure the order of qualifiers
12542 is canonical. Thus, start from unqualified type if
12543 an earlier qualifier is missing in sub_quals, but some later
12544 one is present there. */
12545 for (i = 0; i < dwarf_qual_info_size; i++)
12546 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12547 needed = true;
12548 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12550 sub_quals = 0;
12551 break;
12554 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12555 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12557 /* As not all intermediate qualified DIEs have corresponding
12558 tree types, ensure that qualified DIEs in the same scope
12559 as their DW_AT_type are emitted after their DW_AT_type,
12560 only with other qualified DIEs for the same type possibly
12561 in between them. Determine the range of such qualified
12562 DIEs now (first being the base type, last being corresponding
12563 last qualified DIE for it). */
12564 unsigned int count = 0;
12565 first = qualified_die_p (mod_type_die, &first_quals,
12566 dwarf_qual_info_size);
12567 if (first == NULL)
12568 first = mod_type_die;
12569 gcc_assert ((first_quals & ~sub_quals) == 0);
12570 for (count = 0, last = first;
12571 count < (1U << dwarf_qual_info_size);
12572 count++, last = last->die_sib)
12574 int quals = 0;
12575 if (last == mod_scope->die_child)
12576 break;
12577 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12578 != first)
12579 break;
12583 for (i = 0; i < dwarf_qual_info_size; i++)
12584 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12586 dw_die_ref d;
12587 if (first && first != last)
12589 for (d = first->die_sib; ; d = d->die_sib)
12591 int quals = 0;
12592 qualified_die_p (d, &quals, dwarf_qual_info_size);
12593 if (quals == (first_quals | dwarf_qual_info[i].q))
12594 break;
12595 if (d == last)
12597 d = NULL;
12598 break;
12601 if (d)
12603 mod_type_die = d;
12604 continue;
12607 if (first)
12609 d = ggc_cleared_alloc<die_node> ();
12610 d->die_tag = dwarf_qual_info[i].t;
12611 add_child_die_after (mod_scope, d, last);
12612 last = d;
12614 else
12615 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12616 if (mod_type_die)
12617 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12618 mod_type_die = d;
12619 first_quals |= dwarf_qual_info[i].q;
12622 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12624 dwarf_tag tag = DW_TAG_pointer_type;
12625 if (code == REFERENCE_TYPE)
12627 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12628 tag = DW_TAG_rvalue_reference_type;
12629 else
12630 tag = DW_TAG_reference_type;
12632 mod_type_die = new_die (tag, mod_scope, type);
12634 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12635 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12636 add_alignment_attribute (mod_type_die, type);
12637 item_type = TREE_TYPE (type);
12639 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12640 if (!ADDR_SPACE_GENERIC_P (as))
12642 int action = targetm.addr_space.debug (as);
12643 if (action >= 0)
12645 /* Positive values indicate an address_class. */
12646 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12648 else
12650 /* Negative values indicate an (inverted) segment base reg. */
12651 dw_loc_descr_ref d
12652 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12653 add_AT_loc (mod_type_die, DW_AT_segment, d);
12657 else if (code == INTEGER_TYPE
12658 && TREE_TYPE (type) != NULL_TREE
12659 && subrange_type_for_debug_p (type, &low, &high))
12661 tree bias = NULL_TREE;
12662 if (lang_hooks.types.get_type_bias)
12663 bias = lang_hooks.types.get_type_bias (type);
12664 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12665 item_type = TREE_TYPE (type);
12667 else if (is_base_type (type))
12668 mod_type_die = base_type_die (type, reverse);
12669 else
12671 gen_type_die (type, context_die);
12673 /* We have to get the type_main_variant here (and pass that to the
12674 `lookup_type_die' routine) because the ..._TYPE node we have
12675 might simply be a *copy* of some original type node (where the
12676 copy was created to help us keep track of typedef names) and
12677 that copy might have a different TYPE_UID from the original
12678 ..._TYPE node. */
12679 if (TREE_CODE (type) == FUNCTION_TYPE
12680 || TREE_CODE (type) == METHOD_TYPE)
12682 /* For function/method types, can't just use type_main_variant here,
12683 because that can have different ref-qualifiers for C++,
12684 but try to canonicalize. */
12685 tree main = TYPE_MAIN_VARIANT (type);
12686 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12687 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12688 && check_base_type (t, main)
12689 && check_lang_type (t, type))
12690 return lookup_type_die (t);
12691 return lookup_type_die (type);
12693 else if (TREE_CODE (type) != VECTOR_TYPE
12694 && TREE_CODE (type) != ARRAY_TYPE)
12695 return lookup_type_die (type_main_variant (type));
12696 else
12697 /* Vectors have the debugging information in the type,
12698 not the main variant. */
12699 return lookup_type_die (type);
12702 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12703 don't output a DW_TAG_typedef, since there isn't one in the
12704 user's program; just attach a DW_AT_name to the type.
12705 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12706 if the base type already has the same name. */
12707 if (name
12708 && ((TREE_CODE (name) != TYPE_DECL
12709 && (qualified_type == TYPE_MAIN_VARIANT (type)
12710 || (cv_quals == TYPE_UNQUALIFIED)))
12711 || (TREE_CODE (name) == TYPE_DECL
12712 && TREE_TYPE (name) == qualified_type
12713 && DECL_NAME (name))))
12715 if (TREE_CODE (name) == TYPE_DECL)
12716 /* Could just call add_name_and_src_coords_attributes here,
12717 but since this is a builtin type it doesn't have any
12718 useful source coordinates anyway. */
12719 name = DECL_NAME (name);
12720 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12722 /* This probably indicates a bug. */
12723 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12725 name = TYPE_IDENTIFIER (type);
12726 add_name_attribute (mod_type_die,
12727 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12730 if (qualified_type)
12731 equate_type_number_to_die (qualified_type, mod_type_die);
12733 if (item_type)
12734 /* We must do this after the equate_type_number_to_die call, in case
12735 this is a recursive type. This ensures that the modified_type_die
12736 recursion will terminate even if the type is recursive. Recursive
12737 types are possible in Ada. */
12738 sub_die = modified_type_die (item_type,
12739 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12740 reverse,
12741 context_die);
12743 if (sub_die != NULL)
12744 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12746 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12747 if (TYPE_ARTIFICIAL (type))
12748 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12750 return mod_type_die;
12753 /* Generate DIEs for the generic parameters of T.
12754 T must be either a generic type or a generic function.
12755 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12757 static void
12758 gen_generic_params_dies (tree t)
12760 tree parms, args;
12761 int parms_num, i;
12762 dw_die_ref die = NULL;
12763 int non_default;
12765 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12766 return;
12768 if (TYPE_P (t))
12769 die = lookup_type_die (t);
12770 else if (DECL_P (t))
12771 die = lookup_decl_die (t);
12773 gcc_assert (die);
12775 parms = lang_hooks.get_innermost_generic_parms (t);
12776 if (!parms)
12777 /* T has no generic parameter. It means T is neither a generic type
12778 or function. End of story. */
12779 return;
12781 parms_num = TREE_VEC_LENGTH (parms);
12782 args = lang_hooks.get_innermost_generic_args (t);
12783 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12784 non_default = int_cst_value (TREE_CHAIN (args));
12785 else
12786 non_default = TREE_VEC_LENGTH (args);
12787 for (i = 0; i < parms_num; i++)
12789 tree parm, arg, arg_pack_elems;
12790 dw_die_ref parm_die;
12792 parm = TREE_VEC_ELT (parms, i);
12793 arg = TREE_VEC_ELT (args, i);
12794 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12795 gcc_assert (parm && TREE_VALUE (parm) && arg);
12797 if (parm && TREE_VALUE (parm) && arg)
12799 /* If PARM represents a template parameter pack,
12800 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12801 by DW_TAG_template_*_parameter DIEs for the argument
12802 pack elements of ARG. Note that ARG would then be
12803 an argument pack. */
12804 if (arg_pack_elems)
12805 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12806 arg_pack_elems,
12807 die);
12808 else
12809 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12810 true /* emit name */, die);
12811 if (i >= non_default)
12812 add_AT_flag (parm_die, DW_AT_default_value, 1);
12817 /* Create and return a DIE for PARM which should be
12818 the representation of a generic type parameter.
12819 For instance, in the C++ front end, PARM would be a template parameter.
12820 ARG is the argument to PARM.
12821 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12822 name of the PARM.
12823 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12824 as a child node. */
12826 static dw_die_ref
12827 generic_parameter_die (tree parm, tree arg,
12828 bool emit_name_p,
12829 dw_die_ref parent_die)
12831 dw_die_ref tmpl_die = NULL;
12832 const char *name = NULL;
12834 if (!parm || !DECL_NAME (parm) || !arg)
12835 return NULL;
12837 /* We support non-type generic parameters and arguments,
12838 type generic parameters and arguments, as well as
12839 generic generic parameters (a.k.a. template template parameters in C++)
12840 and arguments. */
12841 if (TREE_CODE (parm) == PARM_DECL)
12842 /* PARM is a nontype generic parameter */
12843 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12844 else if (TREE_CODE (parm) == TYPE_DECL)
12845 /* PARM is a type generic parameter. */
12846 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12847 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12848 /* PARM is a generic generic parameter.
12849 Its DIE is a GNU extension. It shall have a
12850 DW_AT_name attribute to represent the name of the template template
12851 parameter, and a DW_AT_GNU_template_name attribute to represent the
12852 name of the template template argument. */
12853 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12854 parent_die, parm);
12855 else
12856 gcc_unreachable ();
12858 if (tmpl_die)
12860 tree tmpl_type;
12862 /* If PARM is a generic parameter pack, it means we are
12863 emitting debug info for a template argument pack element.
12864 In other terms, ARG is a template argument pack element.
12865 In that case, we don't emit any DW_AT_name attribute for
12866 the die. */
12867 if (emit_name_p)
12869 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12870 gcc_assert (name);
12871 add_AT_string (tmpl_die, DW_AT_name, name);
12874 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12876 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12877 TMPL_DIE should have a child DW_AT_type attribute that is set
12878 to the type of the argument to PARM, which is ARG.
12879 If PARM is a type generic parameter, TMPL_DIE should have a
12880 child DW_AT_type that is set to ARG. */
12881 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12882 add_type_attribute (tmpl_die, tmpl_type,
12883 (TREE_THIS_VOLATILE (tmpl_type)
12884 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12885 false, parent_die);
12887 else
12889 /* So TMPL_DIE is a DIE representing a
12890 a generic generic template parameter, a.k.a template template
12891 parameter in C++ and arg is a template. */
12893 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12894 to the name of the argument. */
12895 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12896 if (name)
12897 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12900 if (TREE_CODE (parm) == PARM_DECL)
12901 /* So PARM is a non-type generic parameter.
12902 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12903 attribute of TMPL_DIE which value represents the value
12904 of ARG.
12905 We must be careful here:
12906 The value of ARG might reference some function decls.
12907 We might currently be emitting debug info for a generic
12908 type and types are emitted before function decls, we don't
12909 know if the function decls referenced by ARG will actually be
12910 emitted after cgraph computations.
12911 So must defer the generation of the DW_AT_const_value to
12912 after cgraph is ready. */
12913 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12916 return tmpl_die;
12919 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12920 PARM_PACK must be a template parameter pack. The returned DIE
12921 will be child DIE of PARENT_DIE. */
12923 static dw_die_ref
12924 template_parameter_pack_die (tree parm_pack,
12925 tree parm_pack_args,
12926 dw_die_ref parent_die)
12928 dw_die_ref die;
12929 int j;
12931 gcc_assert (parent_die && parm_pack);
12933 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12934 add_name_and_src_coords_attributes (die, parm_pack);
12935 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12936 generic_parameter_die (parm_pack,
12937 TREE_VEC_ELT (parm_pack_args, j),
12938 false /* Don't emit DW_AT_name */,
12939 die);
12940 return die;
12943 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12944 an enumerated type. */
12946 static inline int
12947 type_is_enum (const_tree type)
12949 return TREE_CODE (type) == ENUMERAL_TYPE;
12952 /* Return the DBX register number described by a given RTL node. */
12954 static unsigned int
12955 dbx_reg_number (const_rtx rtl)
12957 unsigned regno = REGNO (rtl);
12959 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12961 #ifdef LEAF_REG_REMAP
12962 if (crtl->uses_only_leaf_regs)
12964 int leaf_reg = LEAF_REG_REMAP (regno);
12965 if (leaf_reg != -1)
12966 regno = (unsigned) leaf_reg;
12968 #endif
12970 regno = DBX_REGISTER_NUMBER (regno);
12971 gcc_assert (regno != INVALID_REGNUM);
12972 return regno;
12975 /* Optionally add a DW_OP_piece term to a location description expression.
12976 DW_OP_piece is only added if the location description expression already
12977 doesn't end with DW_OP_piece. */
12979 static void
12980 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12982 dw_loc_descr_ref loc;
12984 if (*list_head != NULL)
12986 /* Find the end of the chain. */
12987 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12990 if (loc->dw_loc_opc != DW_OP_piece)
12991 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12995 /* Return a location descriptor that designates a machine register or
12996 zero if there is none. */
12998 static dw_loc_descr_ref
12999 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13001 rtx regs;
13003 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13004 return 0;
13006 /* We only use "frame base" when we're sure we're talking about the
13007 post-prologue local stack frame. We do this by *not* running
13008 register elimination until this point, and recognizing the special
13009 argument pointer and soft frame pointer rtx's.
13010 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13011 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13012 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13014 dw_loc_descr_ref result = NULL;
13016 if (dwarf_version >= 4 || !dwarf_strict)
13018 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13019 initialized);
13020 if (result)
13021 add_loc_descr (&result,
13022 new_loc_descr (DW_OP_stack_value, 0, 0));
13024 return result;
13027 regs = targetm.dwarf_register_span (rtl);
13029 if (REG_NREGS (rtl) > 1 || regs)
13030 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13031 else
13033 unsigned int dbx_regnum = dbx_reg_number (rtl);
13034 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13035 return 0;
13036 return one_reg_loc_descriptor (dbx_regnum, initialized);
13040 /* Return a location descriptor that designates a machine register for
13041 a given hard register number. */
13043 static dw_loc_descr_ref
13044 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13046 dw_loc_descr_ref reg_loc_descr;
13048 if (regno <= 31)
13049 reg_loc_descr
13050 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13051 else
13052 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13054 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13055 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13057 return reg_loc_descr;
13060 /* Given an RTL of a register, return a location descriptor that
13061 designates a value that spans more than one register. */
13063 static dw_loc_descr_ref
13064 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13065 enum var_init_status initialized)
13067 int size, i;
13068 dw_loc_descr_ref loc_result = NULL;
13070 /* Simple, contiguous registers. */
13071 if (regs == NULL_RTX)
13073 unsigned reg = REGNO (rtl);
13074 int nregs;
13076 #ifdef LEAF_REG_REMAP
13077 if (crtl->uses_only_leaf_regs)
13079 int leaf_reg = LEAF_REG_REMAP (reg);
13080 if (leaf_reg != -1)
13081 reg = (unsigned) leaf_reg;
13083 #endif
13085 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13086 nregs = REG_NREGS (rtl);
13088 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13090 loc_result = NULL;
13091 while (nregs--)
13093 dw_loc_descr_ref t;
13095 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13096 VAR_INIT_STATUS_INITIALIZED);
13097 add_loc_descr (&loc_result, t);
13098 add_loc_descr_op_piece (&loc_result, size);
13099 ++reg;
13101 return loc_result;
13104 /* Now onto stupid register sets in non contiguous locations. */
13106 gcc_assert (GET_CODE (regs) == PARALLEL);
13108 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13109 loc_result = NULL;
13111 for (i = 0; i < XVECLEN (regs, 0); ++i)
13113 dw_loc_descr_ref t;
13115 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13116 VAR_INIT_STATUS_INITIALIZED);
13117 add_loc_descr (&loc_result, t);
13118 add_loc_descr_op_piece (&loc_result, size);
13121 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13122 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13123 return loc_result;
13126 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13128 /* Return a location descriptor that designates a constant i,
13129 as a compound operation from constant (i >> shift), constant shift
13130 and DW_OP_shl. */
13132 static dw_loc_descr_ref
13133 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13135 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13136 add_loc_descr (&ret, int_loc_descriptor (shift));
13137 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13138 return ret;
13141 /* Return a location descriptor that designates a constant. */
13143 static dw_loc_descr_ref
13144 int_loc_descriptor (HOST_WIDE_INT i)
13146 enum dwarf_location_atom op;
13148 /* Pick the smallest representation of a constant, rather than just
13149 defaulting to the LEB encoding. */
13150 if (i >= 0)
13152 int clz = clz_hwi (i);
13153 int ctz = ctz_hwi (i);
13154 if (i <= 31)
13155 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13156 else if (i <= 0xff)
13157 op = DW_OP_const1u;
13158 else if (i <= 0xffff)
13159 op = DW_OP_const2u;
13160 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13161 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13162 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13163 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13164 while DW_OP_const4u is 5 bytes. */
13165 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13166 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13167 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13168 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13169 while DW_OP_const4u is 5 bytes. */
13170 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13172 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13173 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13174 <= 4)
13176 /* As i >= 2**31, the double cast above will yield a negative number.
13177 Since wrapping is defined in DWARF expressions we can output big
13178 positive integers as small negative ones, regardless of the size
13179 of host wide ints.
13181 Here, since the evaluator will handle 32-bit values and since i >=
13182 2**31, we know it's going to be interpreted as a negative literal:
13183 store it this way if we can do better than 5 bytes this way. */
13184 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13186 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13187 op = DW_OP_const4u;
13189 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13190 least 6 bytes: see if we can do better before falling back to it. */
13191 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13192 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13193 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13194 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13195 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13196 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13197 >= HOST_BITS_PER_WIDE_INT)
13198 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13199 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13200 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13201 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13202 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13203 && size_of_uleb128 (i) > 6)
13204 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13205 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13206 else
13207 op = DW_OP_constu;
13209 else
13211 if (i >= -0x80)
13212 op = DW_OP_const1s;
13213 else if (i >= -0x8000)
13214 op = DW_OP_const2s;
13215 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13217 if (size_of_int_loc_descriptor (i) < 5)
13219 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13220 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13221 return ret;
13223 op = DW_OP_const4s;
13225 else
13227 if (size_of_int_loc_descriptor (i)
13228 < (unsigned long) 1 + size_of_sleb128 (i))
13230 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13231 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13232 return ret;
13234 op = DW_OP_consts;
13238 return new_loc_descr (op, i, 0);
13241 /* Likewise, for unsigned constants. */
13243 static dw_loc_descr_ref
13244 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13246 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13247 const unsigned HOST_WIDE_INT max_uint
13248 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13250 /* If possible, use the clever signed constants handling. */
13251 if (i <= max_int)
13252 return int_loc_descriptor ((HOST_WIDE_INT) i);
13254 /* Here, we are left with positive numbers that cannot be represented as
13255 HOST_WIDE_INT, i.e.:
13256 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13258 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13259 whereas may be better to output a negative integer: thanks to integer
13260 wrapping, we know that:
13261 x = x - 2 ** DWARF2_ADDR_SIZE
13262 = x - 2 * (max (HOST_WIDE_INT) + 1)
13263 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13264 small negative integers. Let's try that in cases it will clearly improve
13265 the encoding: there is no gain turning DW_OP_const4u into
13266 DW_OP_const4s. */
13267 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13268 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13269 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13271 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13273 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13274 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13275 const HOST_WIDE_INT second_shift
13276 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13278 /* So we finally have:
13279 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13280 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13281 return int_loc_descriptor (second_shift);
13284 /* Last chance: fallback to a simple constant operation. */
13285 return new_loc_descr
13286 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13287 ? DW_OP_const4u
13288 : DW_OP_const8u,
13289 i, 0);
13292 /* Generate and return a location description that computes the unsigned
13293 comparison of the two stack top entries (a OP b where b is the top-most
13294 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13295 LE_EXPR, GT_EXPR or GE_EXPR. */
13297 static dw_loc_descr_ref
13298 uint_comparison_loc_list (enum tree_code kind)
13300 enum dwarf_location_atom op, flip_op;
13301 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13303 switch (kind)
13305 case LT_EXPR:
13306 op = DW_OP_lt;
13307 break;
13308 case LE_EXPR:
13309 op = DW_OP_le;
13310 break;
13311 case GT_EXPR:
13312 op = DW_OP_gt;
13313 break;
13314 case GE_EXPR:
13315 op = DW_OP_ge;
13316 break;
13317 default:
13318 gcc_unreachable ();
13321 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13322 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13324 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13325 possible to perform unsigned comparisons: we just have to distinguish
13326 three cases:
13328 1. when a and b have the same sign (as signed integers); then we should
13329 return: a OP(signed) b;
13331 2. when a is a negative signed integer while b is a positive one, then a
13332 is a greater unsigned integer than b; likewise when a and b's roles
13333 are flipped.
13335 So first, compare the sign of the two operands. */
13336 ret = new_loc_descr (DW_OP_over, 0, 0);
13337 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13338 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13339 /* If they have different signs (i.e. they have different sign bits), then
13340 the stack top value has now the sign bit set and thus it's smaller than
13341 zero. */
13342 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13343 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13344 add_loc_descr (&ret, bra_node);
13346 /* We are in case 1. At this point, we know both operands have the same
13347 sign, to it's safe to use the built-in signed comparison. */
13348 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13349 add_loc_descr (&ret, jmp_node);
13351 /* We are in case 2. Here, we know both operands do not have the same sign,
13352 so we have to flip the signed comparison. */
13353 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13354 tmp = new_loc_descr (flip_op, 0, 0);
13355 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13356 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13357 add_loc_descr (&ret, tmp);
13359 /* This dummy operation is necessary to make the two branches join. */
13360 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13361 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13362 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13363 add_loc_descr (&ret, tmp);
13365 return ret;
13368 /* Likewise, but takes the location description lists (might be destructive on
13369 them). Return NULL if either is NULL or if concatenation fails. */
13371 static dw_loc_list_ref
13372 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13373 enum tree_code kind)
13375 if (left == NULL || right == NULL)
13376 return NULL;
13378 add_loc_list (&left, right);
13379 if (left == NULL)
13380 return NULL;
13382 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13383 return left;
13386 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13387 without actually allocating it. */
13389 static unsigned long
13390 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13392 return size_of_int_loc_descriptor (i >> shift)
13393 + size_of_int_loc_descriptor (shift)
13394 + 1;
13397 /* Return size_of_locs (int_loc_descriptor (i)) without
13398 actually allocating it. */
13400 static unsigned long
13401 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13403 unsigned long s;
13405 if (i >= 0)
13407 int clz, ctz;
13408 if (i <= 31)
13409 return 1;
13410 else if (i <= 0xff)
13411 return 2;
13412 else if (i <= 0xffff)
13413 return 3;
13414 clz = clz_hwi (i);
13415 ctz = ctz_hwi (i);
13416 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13417 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13418 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13419 - clz - 5);
13420 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13421 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13422 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13423 - clz - 8);
13424 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13425 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13426 <= 4)
13427 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13428 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13429 return 5;
13430 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13431 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13432 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13433 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13434 - clz - 8);
13435 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13436 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13437 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13438 - clz - 16);
13439 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13440 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13441 && s > 6)
13442 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13443 - clz - 32);
13444 else
13445 return 1 + s;
13447 else
13449 if (i >= -0x80)
13450 return 2;
13451 else if (i >= -0x8000)
13452 return 3;
13453 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13455 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13457 s = size_of_int_loc_descriptor (-i) + 1;
13458 if (s < 5)
13459 return s;
13461 return 5;
13463 else
13465 unsigned long r = 1 + size_of_sleb128 (i);
13466 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13468 s = size_of_int_loc_descriptor (-i) + 1;
13469 if (s < r)
13470 return s;
13472 return r;
13477 /* Return loc description representing "address" of integer value.
13478 This can appear only as toplevel expression. */
13480 static dw_loc_descr_ref
13481 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13483 int litsize;
13484 dw_loc_descr_ref loc_result = NULL;
13486 if (!(dwarf_version >= 4 || !dwarf_strict))
13487 return NULL;
13489 litsize = size_of_int_loc_descriptor (i);
13490 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13491 is more compact. For DW_OP_stack_value we need:
13492 litsize + 1 (DW_OP_stack_value)
13493 and for DW_OP_implicit_value:
13494 1 (DW_OP_implicit_value) + 1 (length) + size. */
13495 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13497 loc_result = int_loc_descriptor (i);
13498 add_loc_descr (&loc_result,
13499 new_loc_descr (DW_OP_stack_value, 0, 0));
13500 return loc_result;
13503 loc_result = new_loc_descr (DW_OP_implicit_value,
13504 size, 0);
13505 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13506 loc_result->dw_loc_oprnd2.v.val_int = i;
13507 return loc_result;
13510 /* Return a location descriptor that designates a base+offset location. */
13512 static dw_loc_descr_ref
13513 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13514 enum var_init_status initialized)
13516 unsigned int regno;
13517 dw_loc_descr_ref result;
13518 dw_fde_ref fde = cfun->fde;
13520 /* We only use "frame base" when we're sure we're talking about the
13521 post-prologue local stack frame. We do this by *not* running
13522 register elimination until this point, and recognizing the special
13523 argument pointer and soft frame pointer rtx's. */
13524 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13526 rtx elim = (ira_use_lra_p
13527 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13528 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13530 if (elim != reg)
13532 if (GET_CODE (elim) == PLUS)
13534 offset += INTVAL (XEXP (elim, 1));
13535 elim = XEXP (elim, 0);
13537 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13538 && (elim == hard_frame_pointer_rtx
13539 || elim == stack_pointer_rtx))
13540 || elim == (frame_pointer_needed
13541 ? hard_frame_pointer_rtx
13542 : stack_pointer_rtx));
13544 /* If drap register is used to align stack, use frame
13545 pointer + offset to access stack variables. If stack
13546 is aligned without drap, use stack pointer + offset to
13547 access stack variables. */
13548 if (crtl->stack_realign_tried
13549 && reg == frame_pointer_rtx)
13551 int base_reg
13552 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13553 ? HARD_FRAME_POINTER_REGNUM
13554 : REGNO (elim));
13555 return new_reg_loc_descr (base_reg, offset);
13558 gcc_assert (frame_pointer_fb_offset_valid);
13559 offset += frame_pointer_fb_offset;
13560 return new_loc_descr (DW_OP_fbreg, offset, 0);
13564 regno = REGNO (reg);
13565 #ifdef LEAF_REG_REMAP
13566 if (crtl->uses_only_leaf_regs)
13568 int leaf_reg = LEAF_REG_REMAP (regno);
13569 if (leaf_reg != -1)
13570 regno = (unsigned) leaf_reg;
13572 #endif
13573 regno = DWARF_FRAME_REGNUM (regno);
13575 if (!optimize && fde
13576 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13578 /* Use cfa+offset to represent the location of arguments passed
13579 on the stack when drap is used to align stack.
13580 Only do this when not optimizing, for optimized code var-tracking
13581 is supposed to track where the arguments live and the register
13582 used as vdrap or drap in some spot might be used for something
13583 else in other part of the routine. */
13584 return new_loc_descr (DW_OP_fbreg, offset, 0);
13587 if (regno <= 31)
13588 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13589 offset, 0);
13590 else
13591 result = new_loc_descr (DW_OP_bregx, regno, offset);
13593 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13594 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13596 return result;
13599 /* Return true if this RTL expression describes a base+offset calculation. */
13601 static inline int
13602 is_based_loc (const_rtx rtl)
13604 return (GET_CODE (rtl) == PLUS
13605 && ((REG_P (XEXP (rtl, 0))
13606 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13607 && CONST_INT_P (XEXP (rtl, 1)))));
13610 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13611 failed. */
13613 static dw_loc_descr_ref
13614 tls_mem_loc_descriptor (rtx mem)
13616 tree base;
13617 dw_loc_descr_ref loc_result;
13619 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13620 return NULL;
13622 base = get_base_address (MEM_EXPR (mem));
13623 if (base == NULL
13624 || !VAR_P (base)
13625 || !DECL_THREAD_LOCAL_P (base))
13626 return NULL;
13628 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13629 if (loc_result == NULL)
13630 return NULL;
13632 if (MEM_OFFSET (mem))
13633 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13635 return loc_result;
13638 /* Output debug info about reason why we failed to expand expression as dwarf
13639 expression. */
13641 static void
13642 expansion_failed (tree expr, rtx rtl, char const *reason)
13644 if (dump_file && (dump_flags & TDF_DETAILS))
13646 fprintf (dump_file, "Failed to expand as dwarf: ");
13647 if (expr)
13648 print_generic_expr (dump_file, expr, dump_flags);
13649 if (rtl)
13651 fprintf (dump_file, "\n");
13652 print_rtl (dump_file, rtl);
13654 fprintf (dump_file, "\nReason: %s\n", reason);
13658 /* Helper function for const_ok_for_output. */
13660 static bool
13661 const_ok_for_output_1 (rtx rtl)
13663 if (GET_CODE (rtl) == UNSPEC)
13665 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13666 we can't express it in the debug info. */
13667 /* Don't complain about TLS UNSPECs, those are just too hard to
13668 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13669 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13670 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13671 if (flag_checking
13672 && (XVECLEN (rtl, 0) == 0
13673 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13674 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13675 inform (current_function_decl
13676 ? DECL_SOURCE_LOCATION (current_function_decl)
13677 : UNKNOWN_LOCATION,
13678 #if NUM_UNSPEC_VALUES > 0
13679 "non-delegitimized UNSPEC %s (%d) found in variable location",
13680 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13681 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13682 XINT (rtl, 1));
13683 #else
13684 "non-delegitimized UNSPEC %d found in variable location",
13685 XINT (rtl, 1));
13686 #endif
13687 expansion_failed (NULL_TREE, rtl,
13688 "UNSPEC hasn't been delegitimized.\n");
13689 return false;
13692 if (targetm.const_not_ok_for_debug_p (rtl))
13694 expansion_failed (NULL_TREE, rtl,
13695 "Expression rejected for debug by the backend.\n");
13696 return false;
13699 /* FIXME: Refer to PR60655. It is possible for simplification
13700 of rtl expressions in var tracking to produce such expressions.
13701 We should really identify / validate expressions
13702 enclosed in CONST that can be handled by assemblers on various
13703 targets and only handle legitimate cases here. */
13704 if (GET_CODE (rtl) != SYMBOL_REF)
13706 if (GET_CODE (rtl) == NOT)
13707 return false;
13708 return true;
13711 if (CONSTANT_POOL_ADDRESS_P (rtl))
13713 bool marked;
13714 get_pool_constant_mark (rtl, &marked);
13715 /* If all references to this pool constant were optimized away,
13716 it was not output and thus we can't represent it. */
13717 if (!marked)
13719 expansion_failed (NULL_TREE, rtl,
13720 "Constant was removed from constant pool.\n");
13721 return false;
13725 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13726 return false;
13728 /* Avoid references to external symbols in debug info, on several targets
13729 the linker might even refuse to link when linking a shared library,
13730 and in many other cases the relocations for .debug_info/.debug_loc are
13731 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13732 to be defined within the same shared library or executable are fine. */
13733 if (SYMBOL_REF_EXTERNAL_P (rtl))
13735 tree decl = SYMBOL_REF_DECL (rtl);
13737 if (decl == NULL || !targetm.binds_local_p (decl))
13739 expansion_failed (NULL_TREE, rtl,
13740 "Symbol not defined in current TU.\n");
13741 return false;
13745 return true;
13748 /* Return true if constant RTL can be emitted in DW_OP_addr or
13749 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13750 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13752 static bool
13753 const_ok_for_output (rtx rtl)
13755 if (GET_CODE (rtl) == SYMBOL_REF)
13756 return const_ok_for_output_1 (rtl);
13758 if (GET_CODE (rtl) == CONST)
13760 subrtx_var_iterator::array_type array;
13761 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13762 if (!const_ok_for_output_1 (*iter))
13763 return false;
13764 return true;
13767 return true;
13770 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13771 if possible, NULL otherwise. */
13773 static dw_die_ref
13774 base_type_for_mode (machine_mode mode, bool unsignedp)
13776 dw_die_ref type_die;
13777 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13779 if (type == NULL)
13780 return NULL;
13781 switch (TREE_CODE (type))
13783 case INTEGER_TYPE:
13784 case REAL_TYPE:
13785 break;
13786 default:
13787 return NULL;
13789 type_die = lookup_type_die (type);
13790 if (!type_die)
13791 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13792 comp_unit_die ());
13793 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13794 return NULL;
13795 return type_die;
13798 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13799 type matching MODE, or, if MODE is narrower than or as wide as
13800 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13801 possible. */
13803 static dw_loc_descr_ref
13804 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13806 machine_mode outer_mode = mode;
13807 dw_die_ref type_die;
13808 dw_loc_descr_ref cvt;
13810 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13812 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13813 return op;
13815 type_die = base_type_for_mode (outer_mode, 1);
13816 if (type_die == NULL)
13817 return NULL;
13818 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13819 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13820 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13821 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13822 add_loc_descr (&op, cvt);
13823 return op;
13826 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13828 static dw_loc_descr_ref
13829 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13830 dw_loc_descr_ref op1)
13832 dw_loc_descr_ref ret = op0;
13833 add_loc_descr (&ret, op1);
13834 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13835 if (STORE_FLAG_VALUE != 1)
13837 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13838 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13840 return ret;
13843 /* Return location descriptor for signed comparison OP RTL. */
13845 static dw_loc_descr_ref
13846 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13847 machine_mode mem_mode)
13849 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13850 dw_loc_descr_ref op0, op1;
13851 int shift;
13853 if (op_mode == VOIDmode)
13854 op_mode = GET_MODE (XEXP (rtl, 1));
13855 if (op_mode == VOIDmode)
13856 return NULL;
13858 if (dwarf_strict
13859 && dwarf_version < 5
13860 && (!SCALAR_INT_MODE_P (op_mode)
13861 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13862 return NULL;
13864 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13865 VAR_INIT_STATUS_INITIALIZED);
13866 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13867 VAR_INIT_STATUS_INITIALIZED);
13869 if (op0 == NULL || op1 == NULL)
13870 return NULL;
13872 if (!SCALAR_INT_MODE_P (op_mode)
13873 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13874 return compare_loc_descriptor (op, op0, op1);
13876 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13878 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13879 dw_loc_descr_ref cvt;
13881 if (type_die == NULL)
13882 return NULL;
13883 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13884 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13885 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13886 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13887 add_loc_descr (&op0, cvt);
13888 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13889 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13890 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13891 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13892 add_loc_descr (&op1, cvt);
13893 return compare_loc_descriptor (op, op0, op1);
13896 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13897 /* For eq/ne, if the operands are known to be zero-extended,
13898 there is no need to do the fancy shifting up. */
13899 if (op == DW_OP_eq || op == DW_OP_ne)
13901 dw_loc_descr_ref last0, last1;
13902 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13904 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13906 /* deref_size zero extends, and for constants we can check
13907 whether they are zero extended or not. */
13908 if (((last0->dw_loc_opc == DW_OP_deref_size
13909 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13910 || (CONST_INT_P (XEXP (rtl, 0))
13911 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13912 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13913 && ((last1->dw_loc_opc == DW_OP_deref_size
13914 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13915 || (CONST_INT_P (XEXP (rtl, 1))
13916 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13917 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13918 return compare_loc_descriptor (op, op0, op1);
13920 /* EQ/NE comparison against constant in narrower type than
13921 DWARF2_ADDR_SIZE can be performed either as
13922 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13923 DW_OP_{eq,ne}
13925 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13926 DW_OP_{eq,ne}. Pick whatever is shorter. */
13927 if (CONST_INT_P (XEXP (rtl, 1))
13928 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13929 && (size_of_int_loc_descriptor (shift) + 1
13930 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13931 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13932 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13933 & GET_MODE_MASK (op_mode))))
13935 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13936 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13937 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13938 & GET_MODE_MASK (op_mode));
13939 return compare_loc_descriptor (op, op0, op1);
13942 add_loc_descr (&op0, int_loc_descriptor (shift));
13943 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13944 if (CONST_INT_P (XEXP (rtl, 1)))
13945 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13946 else
13948 add_loc_descr (&op1, int_loc_descriptor (shift));
13949 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13951 return compare_loc_descriptor (op, op0, op1);
13954 /* Return location descriptor for unsigned comparison OP RTL. */
13956 static dw_loc_descr_ref
13957 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13958 machine_mode mem_mode)
13960 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13961 dw_loc_descr_ref op0, op1;
13963 if (op_mode == VOIDmode)
13964 op_mode = GET_MODE (XEXP (rtl, 1));
13965 if (op_mode == VOIDmode)
13966 return NULL;
13967 if (!SCALAR_INT_MODE_P (op_mode))
13968 return NULL;
13970 if (dwarf_strict
13971 && dwarf_version < 5
13972 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13973 return NULL;
13975 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13976 VAR_INIT_STATUS_INITIALIZED);
13977 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13978 VAR_INIT_STATUS_INITIALIZED);
13980 if (op0 == NULL || op1 == NULL)
13981 return NULL;
13983 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13985 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13986 dw_loc_descr_ref last0, last1;
13987 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13989 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13991 if (CONST_INT_P (XEXP (rtl, 0)))
13992 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13993 /* deref_size zero extends, so no need to mask it again. */
13994 else if (last0->dw_loc_opc != DW_OP_deref_size
13995 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13997 add_loc_descr (&op0, int_loc_descriptor (mask));
13998 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14000 if (CONST_INT_P (XEXP (rtl, 1)))
14001 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14002 /* deref_size zero extends, so no need to mask it again. */
14003 else if (last1->dw_loc_opc != DW_OP_deref_size
14004 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14006 add_loc_descr (&op1, int_loc_descriptor (mask));
14007 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14010 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14012 HOST_WIDE_INT bias = 1;
14013 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14014 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14015 if (CONST_INT_P (XEXP (rtl, 1)))
14016 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14017 + INTVAL (XEXP (rtl, 1)));
14018 else
14019 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14020 bias, 0));
14022 return compare_loc_descriptor (op, op0, op1);
14025 /* Return location descriptor for {U,S}{MIN,MAX}. */
14027 static dw_loc_descr_ref
14028 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14029 machine_mode mem_mode)
14031 enum dwarf_location_atom op;
14032 dw_loc_descr_ref op0, op1, ret;
14033 dw_loc_descr_ref bra_node, drop_node;
14035 if (dwarf_strict
14036 && dwarf_version < 5
14037 && (!SCALAR_INT_MODE_P (mode)
14038 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14039 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);
14046 if (op0 == NULL || op1 == NULL)
14047 return NULL;
14049 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14050 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14051 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14052 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14054 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14056 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14057 add_loc_descr (&op0, int_loc_descriptor (mask));
14058 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14059 add_loc_descr (&op1, int_loc_descriptor (mask));
14060 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14062 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14064 HOST_WIDE_INT bias = 1;
14065 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14066 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14067 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14070 else if (!SCALAR_INT_MODE_P (mode)
14071 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14073 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14074 add_loc_descr (&op0, int_loc_descriptor (shift));
14075 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14076 add_loc_descr (&op1, int_loc_descriptor (shift));
14077 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14079 else if (SCALAR_INT_MODE_P (mode)
14080 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14082 dw_die_ref type_die = base_type_for_mode (mode, 0);
14083 dw_loc_descr_ref cvt;
14084 if (type_die == NULL)
14085 return NULL;
14086 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14087 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14088 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14089 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14090 add_loc_descr (&op0, cvt);
14091 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14092 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14093 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14094 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14095 add_loc_descr (&op1, cvt);
14098 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14099 op = DW_OP_lt;
14100 else
14101 op = DW_OP_gt;
14102 ret = op0;
14103 add_loc_descr (&ret, op1);
14104 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14105 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14106 add_loc_descr (&ret, bra_node);
14107 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14108 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14109 add_loc_descr (&ret, drop_node);
14110 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14111 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14112 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14113 && SCALAR_INT_MODE_P (mode)
14114 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14115 ret = convert_descriptor_to_mode (mode, ret);
14116 return ret;
14119 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14120 but after converting arguments to type_die, afterwards
14121 convert back to unsigned. */
14123 static dw_loc_descr_ref
14124 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14125 machine_mode mode, machine_mode mem_mode)
14127 dw_loc_descr_ref cvt, op0, op1;
14129 if (type_die == NULL)
14130 return NULL;
14131 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14132 VAR_INIT_STATUS_INITIALIZED);
14133 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14134 VAR_INIT_STATUS_INITIALIZED);
14135 if (op0 == NULL || op1 == NULL)
14136 return NULL;
14137 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14138 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14139 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14140 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14141 add_loc_descr (&op0, cvt);
14142 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14143 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14144 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14145 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14146 add_loc_descr (&op1, cvt);
14147 add_loc_descr (&op0, op1);
14148 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14149 return convert_descriptor_to_mode (mode, op0);
14152 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14153 const0 is DW_OP_lit0 or corresponding typed constant,
14154 const1 is DW_OP_lit1 or corresponding typed constant
14155 and constMSB is constant with just the MSB bit set
14156 for the mode):
14157 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14158 L1: const0 DW_OP_swap
14159 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14160 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14161 L3: DW_OP_drop
14162 L4: DW_OP_nop
14164 CTZ is similar:
14165 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14166 L1: const0 DW_OP_swap
14167 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14168 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14169 L3: DW_OP_drop
14170 L4: DW_OP_nop
14172 FFS is similar:
14173 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14174 L1: const1 DW_OP_swap
14175 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14176 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14177 L3: DW_OP_drop
14178 L4: DW_OP_nop */
14180 static dw_loc_descr_ref
14181 clz_loc_descriptor (rtx rtl, machine_mode mode,
14182 machine_mode mem_mode)
14184 dw_loc_descr_ref op0, ret, tmp;
14185 HOST_WIDE_INT valv;
14186 dw_loc_descr_ref l1jump, l1label;
14187 dw_loc_descr_ref l2jump, l2label;
14188 dw_loc_descr_ref l3jump, l3label;
14189 dw_loc_descr_ref l4jump, l4label;
14190 rtx msb;
14192 if (!SCALAR_INT_MODE_P (mode)
14193 || GET_MODE (XEXP (rtl, 0)) != mode)
14194 return NULL;
14196 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14197 VAR_INIT_STATUS_INITIALIZED);
14198 if (op0 == NULL)
14199 return NULL;
14200 ret = op0;
14201 if (GET_CODE (rtl) == CLZ)
14203 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14204 valv = GET_MODE_BITSIZE (mode);
14206 else if (GET_CODE (rtl) == FFS)
14207 valv = 0;
14208 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14209 valv = GET_MODE_BITSIZE (mode);
14210 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14211 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14212 add_loc_descr (&ret, l1jump);
14213 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14214 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14215 VAR_INIT_STATUS_INITIALIZED);
14216 if (tmp == NULL)
14217 return NULL;
14218 add_loc_descr (&ret, tmp);
14219 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14220 add_loc_descr (&ret, l4jump);
14221 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14222 ? const1_rtx : const0_rtx,
14223 mode, mem_mode,
14224 VAR_INIT_STATUS_INITIALIZED);
14225 if (l1label == NULL)
14226 return NULL;
14227 add_loc_descr (&ret, l1label);
14228 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14229 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14230 add_loc_descr (&ret, l2label);
14231 if (GET_CODE (rtl) != CLZ)
14232 msb = const1_rtx;
14233 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14234 msb = GEN_INT (HOST_WIDE_INT_1U
14235 << (GET_MODE_BITSIZE (mode) - 1));
14236 else
14237 msb = immed_wide_int_const
14238 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14239 GET_MODE_PRECISION (mode)), mode);
14240 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14241 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14242 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14243 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14244 else
14245 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14246 VAR_INIT_STATUS_INITIALIZED);
14247 if (tmp == NULL)
14248 return NULL;
14249 add_loc_descr (&ret, tmp);
14250 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14251 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14252 add_loc_descr (&ret, l3jump);
14253 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14254 VAR_INIT_STATUS_INITIALIZED);
14255 if (tmp == NULL)
14256 return NULL;
14257 add_loc_descr (&ret, tmp);
14258 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14259 ? DW_OP_shl : DW_OP_shr, 0, 0));
14260 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14261 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14262 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14263 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14264 add_loc_descr (&ret, l2jump);
14265 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14266 add_loc_descr (&ret, l3label);
14267 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14268 add_loc_descr (&ret, l4label);
14269 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14270 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14271 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14272 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14273 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14274 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14275 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14276 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14277 return ret;
14280 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14281 const1 is DW_OP_lit1 or corresponding typed constant):
14282 const0 DW_OP_swap
14283 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14284 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14285 L2: DW_OP_drop
14287 PARITY is similar:
14288 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14289 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14290 L2: DW_OP_drop */
14292 static dw_loc_descr_ref
14293 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14294 machine_mode mem_mode)
14296 dw_loc_descr_ref op0, ret, tmp;
14297 dw_loc_descr_ref l1jump, l1label;
14298 dw_loc_descr_ref l2jump, l2label;
14300 if (!SCALAR_INT_MODE_P (mode)
14301 || GET_MODE (XEXP (rtl, 0)) != mode)
14302 return NULL;
14304 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14305 VAR_INIT_STATUS_INITIALIZED);
14306 if (op0 == NULL)
14307 return NULL;
14308 ret = op0;
14309 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14310 VAR_INIT_STATUS_INITIALIZED);
14311 if (tmp == NULL)
14312 return NULL;
14313 add_loc_descr (&ret, tmp);
14314 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14315 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14316 add_loc_descr (&ret, l1label);
14317 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14318 add_loc_descr (&ret, l2jump);
14319 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14320 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14321 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14322 VAR_INIT_STATUS_INITIALIZED);
14323 if (tmp == NULL)
14324 return NULL;
14325 add_loc_descr (&ret, tmp);
14326 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14327 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14328 ? DW_OP_plus : DW_OP_xor, 0, 0));
14329 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14330 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14331 VAR_INIT_STATUS_INITIALIZED);
14332 add_loc_descr (&ret, tmp);
14333 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14334 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14335 add_loc_descr (&ret, l1jump);
14336 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14337 add_loc_descr (&ret, l2label);
14338 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14339 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14340 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14341 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14342 return ret;
14345 /* BSWAP (constS is initial shift count, either 56 or 24):
14346 constS const0
14347 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14348 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14349 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14350 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14351 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14353 static dw_loc_descr_ref
14354 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14355 machine_mode mem_mode)
14357 dw_loc_descr_ref op0, ret, tmp;
14358 dw_loc_descr_ref l1jump, l1label;
14359 dw_loc_descr_ref l2jump, l2label;
14361 if (!SCALAR_INT_MODE_P (mode)
14362 || BITS_PER_UNIT != 8
14363 || (GET_MODE_BITSIZE (mode) != 32
14364 && GET_MODE_BITSIZE (mode) != 64))
14365 return NULL;
14367 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14368 VAR_INIT_STATUS_INITIALIZED);
14369 if (op0 == NULL)
14370 return NULL;
14372 ret = op0;
14373 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14374 mode, mem_mode,
14375 VAR_INIT_STATUS_INITIALIZED);
14376 if (tmp == NULL)
14377 return NULL;
14378 add_loc_descr (&ret, tmp);
14379 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14380 VAR_INIT_STATUS_INITIALIZED);
14381 if (tmp == NULL)
14382 return NULL;
14383 add_loc_descr (&ret, tmp);
14384 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14385 add_loc_descr (&ret, l1label);
14386 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14387 mode, mem_mode,
14388 VAR_INIT_STATUS_INITIALIZED);
14389 add_loc_descr (&ret, tmp);
14390 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14391 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14392 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14393 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14394 VAR_INIT_STATUS_INITIALIZED);
14395 if (tmp == NULL)
14396 return NULL;
14397 add_loc_descr (&ret, tmp);
14398 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14399 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14400 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14401 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14402 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14403 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14404 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14405 VAR_INIT_STATUS_INITIALIZED);
14406 add_loc_descr (&ret, tmp);
14407 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14408 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14409 add_loc_descr (&ret, l2jump);
14410 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14411 VAR_INIT_STATUS_INITIALIZED);
14412 add_loc_descr (&ret, tmp);
14413 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14414 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14415 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14416 add_loc_descr (&ret, l1jump);
14417 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14418 add_loc_descr (&ret, l2label);
14419 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14420 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14421 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14422 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14423 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14424 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14425 return ret;
14428 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14429 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14430 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14431 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14433 ROTATERT is similar:
14434 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14435 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14436 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14438 static dw_loc_descr_ref
14439 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14440 machine_mode mem_mode)
14442 rtx rtlop1 = XEXP (rtl, 1);
14443 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14444 int i;
14446 if (!SCALAR_INT_MODE_P (mode))
14447 return NULL;
14449 if (GET_MODE (rtlop1) != VOIDmode
14450 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14451 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14452 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14453 VAR_INIT_STATUS_INITIALIZED);
14454 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14455 VAR_INIT_STATUS_INITIALIZED);
14456 if (op0 == NULL || op1 == NULL)
14457 return NULL;
14458 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14459 for (i = 0; i < 2; i++)
14461 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14462 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14463 mode, mem_mode,
14464 VAR_INIT_STATUS_INITIALIZED);
14465 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14466 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14467 ? DW_OP_const4u
14468 : HOST_BITS_PER_WIDE_INT == 64
14469 ? DW_OP_const8u : DW_OP_constu,
14470 GET_MODE_MASK (mode), 0);
14471 else
14472 mask[i] = NULL;
14473 if (mask[i] == NULL)
14474 return NULL;
14475 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14477 ret = op0;
14478 add_loc_descr (&ret, op1);
14479 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14480 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14481 if (GET_CODE (rtl) == ROTATERT)
14483 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14484 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14485 GET_MODE_BITSIZE (mode), 0));
14487 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14488 if (mask[0] != NULL)
14489 add_loc_descr (&ret, mask[0]);
14490 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14491 if (mask[1] != NULL)
14493 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14494 add_loc_descr (&ret, mask[1]);
14495 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14497 if (GET_CODE (rtl) == ROTATE)
14499 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14500 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14501 GET_MODE_BITSIZE (mode), 0));
14503 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14504 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14505 return ret;
14508 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14509 for DEBUG_PARAMETER_REF RTL. */
14511 static dw_loc_descr_ref
14512 parameter_ref_descriptor (rtx rtl)
14514 dw_loc_descr_ref ret;
14515 dw_die_ref ref;
14517 if (dwarf_strict)
14518 return NULL;
14519 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14520 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14521 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14522 if (ref)
14524 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14525 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14526 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14528 else
14530 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14531 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14533 return ret;
14536 /* The following routine converts the RTL for a variable or parameter
14537 (resident in memory) into an equivalent Dwarf representation of a
14538 mechanism for getting the address of that same variable onto the top of a
14539 hypothetical "address evaluation" stack.
14541 When creating memory location descriptors, we are effectively transforming
14542 the RTL for a memory-resident object into its Dwarf postfix expression
14543 equivalent. This routine recursively descends an RTL tree, turning
14544 it into Dwarf postfix code as it goes.
14546 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14548 MEM_MODE is the mode of the memory reference, needed to handle some
14549 autoincrement addressing modes.
14551 Return 0 if we can't represent the location. */
14553 dw_loc_descr_ref
14554 mem_loc_descriptor (rtx rtl, machine_mode mode,
14555 machine_mode mem_mode,
14556 enum var_init_status initialized)
14558 dw_loc_descr_ref mem_loc_result = NULL;
14559 enum dwarf_location_atom op;
14560 dw_loc_descr_ref op0, op1;
14561 rtx inner = NULL_RTX;
14563 if (mode == VOIDmode)
14564 mode = GET_MODE (rtl);
14566 /* Note that for a dynamically sized array, the location we will generate a
14567 description of here will be the lowest numbered location which is
14568 actually within the array. That's *not* necessarily the same as the
14569 zeroth element of the array. */
14571 rtl = targetm.delegitimize_address (rtl);
14573 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14574 return NULL;
14576 switch (GET_CODE (rtl))
14578 case POST_INC:
14579 case POST_DEC:
14580 case POST_MODIFY:
14581 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14583 case SUBREG:
14584 /* The case of a subreg may arise when we have a local (register)
14585 variable or a formal (register) parameter which doesn't quite fill
14586 up an entire register. For now, just assume that it is
14587 legitimate to make the Dwarf info refer to the whole register which
14588 contains the given subreg. */
14589 if (!subreg_lowpart_p (rtl))
14590 break;
14591 inner = SUBREG_REG (rtl);
14592 /* FALLTHRU */
14593 case TRUNCATE:
14594 if (inner == NULL_RTX)
14595 inner = XEXP (rtl, 0);
14596 if (SCALAR_INT_MODE_P (mode)
14597 && SCALAR_INT_MODE_P (GET_MODE (inner))
14598 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14599 #ifdef POINTERS_EXTEND_UNSIGNED
14600 || (mode == Pmode && mem_mode != VOIDmode)
14601 #endif
14603 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14605 mem_loc_result = mem_loc_descriptor (inner,
14606 GET_MODE (inner),
14607 mem_mode, initialized);
14608 break;
14610 if (dwarf_strict && dwarf_version < 5)
14611 break;
14612 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14613 break;
14614 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14615 && (!SCALAR_INT_MODE_P (mode)
14616 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14617 break;
14618 else
14620 dw_die_ref type_die;
14621 dw_loc_descr_ref cvt;
14623 mem_loc_result = mem_loc_descriptor (inner,
14624 GET_MODE (inner),
14625 mem_mode, initialized);
14626 if (mem_loc_result == NULL)
14627 break;
14628 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14629 if (type_die == NULL)
14631 mem_loc_result = NULL;
14632 break;
14634 if (GET_MODE_SIZE (mode)
14635 != GET_MODE_SIZE (GET_MODE (inner)))
14636 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14637 else
14638 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14639 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14640 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14641 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14642 add_loc_descr (&mem_loc_result, cvt);
14643 if (SCALAR_INT_MODE_P (mode)
14644 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14646 /* Convert it to untyped afterwards. */
14647 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14648 add_loc_descr (&mem_loc_result, cvt);
14651 break;
14653 case REG:
14654 if (! SCALAR_INT_MODE_P (mode)
14655 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14656 && rtl != arg_pointer_rtx
14657 && rtl != frame_pointer_rtx
14658 #ifdef POINTERS_EXTEND_UNSIGNED
14659 && (mode != Pmode || mem_mode == VOIDmode)
14660 #endif
14663 dw_die_ref type_die;
14664 unsigned int dbx_regnum;
14666 if (dwarf_strict && dwarf_version < 5)
14667 break;
14668 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14669 break;
14670 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14671 if (type_die == NULL)
14672 break;
14674 dbx_regnum = dbx_reg_number (rtl);
14675 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14676 break;
14677 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14678 dbx_regnum, 0);
14679 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14680 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14681 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14682 break;
14684 /* Whenever a register number forms a part of the description of the
14685 method for calculating the (dynamic) address of a memory resident
14686 object, DWARF rules require the register number be referred to as
14687 a "base register". This distinction is not based in any way upon
14688 what category of register the hardware believes the given register
14689 belongs to. This is strictly DWARF terminology we're dealing with
14690 here. Note that in cases where the location of a memory-resident
14691 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14692 OP_CONST (0)) the actual DWARF location descriptor that we generate
14693 may just be OP_BASEREG (basereg). This may look deceptively like
14694 the object in question was allocated to a register (rather than in
14695 memory) so DWARF consumers need to be aware of the subtle
14696 distinction between OP_REG and OP_BASEREG. */
14697 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14698 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14699 else if (stack_realign_drap
14700 && crtl->drap_reg
14701 && crtl->args.internal_arg_pointer == rtl
14702 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14704 /* If RTL is internal_arg_pointer, which has been optimized
14705 out, use DRAP instead. */
14706 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14707 VAR_INIT_STATUS_INITIALIZED);
14709 break;
14711 case SIGN_EXTEND:
14712 case ZERO_EXTEND:
14713 if (!SCALAR_INT_MODE_P (mode))
14714 break;
14715 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14716 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14717 if (op0 == 0)
14718 break;
14719 else if (GET_CODE (rtl) == ZERO_EXTEND
14720 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14721 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14722 < HOST_BITS_PER_WIDE_INT
14723 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14724 to expand zero extend as two shifts instead of
14725 masking. */
14726 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14728 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14729 mem_loc_result = op0;
14730 add_loc_descr (&mem_loc_result,
14731 int_loc_descriptor (GET_MODE_MASK (imode)));
14732 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14734 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14736 int shift = DWARF2_ADDR_SIZE
14737 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14738 shift *= BITS_PER_UNIT;
14739 if (GET_CODE (rtl) == SIGN_EXTEND)
14740 op = DW_OP_shra;
14741 else
14742 op = DW_OP_shr;
14743 mem_loc_result = op0;
14744 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14745 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14746 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14747 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14749 else if (!dwarf_strict || dwarf_version >= 5)
14751 dw_die_ref type_die1, type_die2;
14752 dw_loc_descr_ref cvt;
14754 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14755 GET_CODE (rtl) == ZERO_EXTEND);
14756 if (type_die1 == NULL)
14757 break;
14758 type_die2 = base_type_for_mode (mode, 1);
14759 if (type_die2 == NULL)
14760 break;
14761 mem_loc_result = op0;
14762 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14763 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14764 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14765 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14766 add_loc_descr (&mem_loc_result, cvt);
14767 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14768 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14769 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14770 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14771 add_loc_descr (&mem_loc_result, cvt);
14773 break;
14775 case MEM:
14777 rtx new_rtl = avoid_constant_pool_reference (rtl);
14778 if (new_rtl != rtl)
14780 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14781 initialized);
14782 if (mem_loc_result != NULL)
14783 return mem_loc_result;
14786 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14787 get_address_mode (rtl), mode,
14788 VAR_INIT_STATUS_INITIALIZED);
14789 if (mem_loc_result == NULL)
14790 mem_loc_result = tls_mem_loc_descriptor (rtl);
14791 if (mem_loc_result != NULL)
14793 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14794 || !SCALAR_INT_MODE_P(mode))
14796 dw_die_ref type_die;
14797 dw_loc_descr_ref deref;
14799 if (dwarf_strict && dwarf_version < 5)
14800 return NULL;
14801 type_die
14802 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14803 if (type_die == NULL)
14804 return NULL;
14805 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14806 GET_MODE_SIZE (mode), 0);
14807 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14808 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14809 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14810 add_loc_descr (&mem_loc_result, deref);
14812 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14813 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14814 else
14815 add_loc_descr (&mem_loc_result,
14816 new_loc_descr (DW_OP_deref_size,
14817 GET_MODE_SIZE (mode), 0));
14819 break;
14821 case LO_SUM:
14822 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14824 case LABEL_REF:
14825 /* Some ports can transform a symbol ref into a label ref, because
14826 the symbol ref is too far away and has to be dumped into a constant
14827 pool. */
14828 case CONST:
14829 case SYMBOL_REF:
14830 if (!SCALAR_INT_MODE_P (mode)
14831 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14832 #ifdef POINTERS_EXTEND_UNSIGNED
14833 && (mode != Pmode || mem_mode == VOIDmode)
14834 #endif
14836 break;
14837 if (GET_CODE (rtl) == SYMBOL_REF
14838 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14840 dw_loc_descr_ref temp;
14842 /* If this is not defined, we have no way to emit the data. */
14843 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14844 break;
14846 temp = new_addr_loc_descr (rtl, dtprel_true);
14848 /* We check for DWARF 5 here because gdb did not implement
14849 DW_OP_form_tls_address until after 7.12. */
14850 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14851 ? DW_OP_form_tls_address
14852 : DW_OP_GNU_push_tls_address),
14853 0, 0);
14854 add_loc_descr (&mem_loc_result, temp);
14856 break;
14859 if (!const_ok_for_output (rtl))
14861 if (GET_CODE (rtl) == CONST)
14862 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14863 initialized);
14864 break;
14867 symref:
14868 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14869 vec_safe_push (used_rtx_array, rtl);
14870 break;
14872 case CONCAT:
14873 case CONCATN:
14874 case VAR_LOCATION:
14875 case DEBUG_IMPLICIT_PTR:
14876 expansion_failed (NULL_TREE, rtl,
14877 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14878 return 0;
14880 case ENTRY_VALUE:
14881 if (dwarf_strict && dwarf_version < 5)
14882 return NULL;
14883 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14885 if (!SCALAR_INT_MODE_P (mode)
14886 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14887 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14888 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14889 else
14891 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14892 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14893 return NULL;
14894 op0 = one_reg_loc_descriptor (dbx_regnum,
14895 VAR_INIT_STATUS_INITIALIZED);
14898 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14899 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14901 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14902 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14903 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14904 return NULL;
14906 else
14907 gcc_unreachable ();
14908 if (op0 == NULL)
14909 return NULL;
14910 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14911 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14912 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14913 break;
14915 case DEBUG_PARAMETER_REF:
14916 mem_loc_result = parameter_ref_descriptor (rtl);
14917 break;
14919 case PRE_MODIFY:
14920 /* Extract the PLUS expression nested inside and fall into
14921 PLUS code below. */
14922 rtl = XEXP (rtl, 1);
14923 goto plus;
14925 case PRE_INC:
14926 case PRE_DEC:
14927 /* Turn these into a PLUS expression and fall into the PLUS code
14928 below. */
14929 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14930 gen_int_mode (GET_CODE (rtl) == PRE_INC
14931 ? GET_MODE_UNIT_SIZE (mem_mode)
14932 : -GET_MODE_UNIT_SIZE (mem_mode),
14933 mode));
14935 /* fall through */
14937 case PLUS:
14938 plus:
14939 if (is_based_loc (rtl)
14940 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14941 || XEXP (rtl, 0) == arg_pointer_rtx
14942 || XEXP (rtl, 0) == frame_pointer_rtx)
14943 && SCALAR_INT_MODE_P (mode))
14944 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14945 INTVAL (XEXP (rtl, 1)),
14946 VAR_INIT_STATUS_INITIALIZED);
14947 else
14949 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14950 VAR_INIT_STATUS_INITIALIZED);
14951 if (mem_loc_result == 0)
14952 break;
14954 if (CONST_INT_P (XEXP (rtl, 1))
14955 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14956 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14957 else
14959 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14960 VAR_INIT_STATUS_INITIALIZED);
14961 if (op1 == 0)
14962 return NULL;
14963 add_loc_descr (&mem_loc_result, op1);
14964 add_loc_descr (&mem_loc_result,
14965 new_loc_descr (DW_OP_plus, 0, 0));
14968 break;
14970 /* If a pseudo-reg is optimized away, it is possible for it to
14971 be replaced with a MEM containing a multiply or shift. */
14972 case MINUS:
14973 op = DW_OP_minus;
14974 goto do_binop;
14976 case MULT:
14977 op = DW_OP_mul;
14978 goto do_binop;
14980 case DIV:
14981 if ((!dwarf_strict || dwarf_version >= 5)
14982 && SCALAR_INT_MODE_P (mode)
14983 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14985 mem_loc_result = typed_binop (DW_OP_div, rtl,
14986 base_type_for_mode (mode, 0),
14987 mode, mem_mode);
14988 break;
14990 op = DW_OP_div;
14991 goto do_binop;
14993 case UMOD:
14994 op = DW_OP_mod;
14995 goto do_binop;
14997 case ASHIFT:
14998 op = DW_OP_shl;
14999 goto do_shift;
15001 case ASHIFTRT:
15002 op = DW_OP_shra;
15003 goto do_shift;
15005 case LSHIFTRT:
15006 op = DW_OP_shr;
15007 goto do_shift;
15009 do_shift:
15010 if (!SCALAR_INT_MODE_P (mode))
15011 break;
15012 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15013 VAR_INIT_STATUS_INITIALIZED);
15015 rtx rtlop1 = XEXP (rtl, 1);
15016 if (GET_MODE (rtlop1) != VOIDmode
15017 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15018 < GET_MODE_BITSIZE (mode))
15019 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15020 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15021 VAR_INIT_STATUS_INITIALIZED);
15024 if (op0 == 0 || op1 == 0)
15025 break;
15027 mem_loc_result = op0;
15028 add_loc_descr (&mem_loc_result, op1);
15029 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15030 break;
15032 case AND:
15033 op = DW_OP_and;
15034 goto do_binop;
15036 case IOR:
15037 op = DW_OP_or;
15038 goto do_binop;
15040 case XOR:
15041 op = DW_OP_xor;
15042 goto do_binop;
15044 do_binop:
15045 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15046 VAR_INIT_STATUS_INITIALIZED);
15047 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15048 VAR_INIT_STATUS_INITIALIZED);
15050 if (op0 == 0 || op1 == 0)
15051 break;
15053 mem_loc_result = op0;
15054 add_loc_descr (&mem_loc_result, op1);
15055 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15056 break;
15058 case MOD:
15059 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15060 && (!dwarf_strict || dwarf_version >= 5))
15062 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15063 base_type_for_mode (mode, 0),
15064 mode, mem_mode);
15065 break;
15068 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15069 VAR_INIT_STATUS_INITIALIZED);
15070 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15071 VAR_INIT_STATUS_INITIALIZED);
15073 if (op0 == 0 || op1 == 0)
15074 break;
15076 mem_loc_result = op0;
15077 add_loc_descr (&mem_loc_result, op1);
15078 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15079 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15080 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15081 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15082 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15083 break;
15085 case UDIV:
15086 if ((!dwarf_strict || dwarf_version >= 5)
15087 && SCALAR_INT_MODE_P (mode))
15089 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15091 op = DW_OP_div;
15092 goto do_binop;
15094 mem_loc_result = typed_binop (DW_OP_div, rtl,
15095 base_type_for_mode (mode, 1),
15096 mode, mem_mode);
15098 break;
15100 case NOT:
15101 op = DW_OP_not;
15102 goto do_unop;
15104 case ABS:
15105 op = DW_OP_abs;
15106 goto do_unop;
15108 case NEG:
15109 op = DW_OP_neg;
15110 goto do_unop;
15112 do_unop:
15113 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15114 VAR_INIT_STATUS_INITIALIZED);
15116 if (op0 == 0)
15117 break;
15119 mem_loc_result = op0;
15120 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15121 break;
15123 case CONST_INT:
15124 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15125 #ifdef POINTERS_EXTEND_UNSIGNED
15126 || (mode == Pmode
15127 && mem_mode != VOIDmode
15128 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15129 #endif
15132 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15133 break;
15135 if ((!dwarf_strict || dwarf_version >= 5)
15136 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15137 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15139 dw_die_ref type_die = base_type_for_mode (mode, 1);
15140 machine_mode amode;
15141 if (type_die == NULL)
15142 return NULL;
15143 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15144 MODE_INT, 0);
15145 if (INTVAL (rtl) >= 0
15146 && amode != BLKmode
15147 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15148 /* const DW_OP_convert <XXX> vs.
15149 DW_OP_const_type <XXX, 1, const>. */
15150 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15151 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15153 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15154 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15155 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15156 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15157 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15158 add_loc_descr (&mem_loc_result, op0);
15159 return mem_loc_result;
15161 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15162 INTVAL (rtl));
15163 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15164 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15165 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15166 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15167 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15168 else
15170 mem_loc_result->dw_loc_oprnd2.val_class
15171 = dw_val_class_const_double;
15172 mem_loc_result->dw_loc_oprnd2.v.val_double
15173 = double_int::from_shwi (INTVAL (rtl));
15176 break;
15178 case CONST_DOUBLE:
15179 if (!dwarf_strict || dwarf_version >= 5)
15181 dw_die_ref type_die;
15183 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15184 CONST_DOUBLE rtx could represent either a large integer
15185 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15186 the value is always a floating point constant.
15188 When it is an integer, a CONST_DOUBLE is used whenever
15189 the constant requires 2 HWIs to be adequately represented.
15190 We output CONST_DOUBLEs as blocks. */
15191 if (mode == VOIDmode
15192 || (GET_MODE (rtl) == VOIDmode
15193 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15194 break;
15195 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15196 if (type_die == NULL)
15197 return NULL;
15198 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15199 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15200 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15201 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15202 #if TARGET_SUPPORTS_WIDE_INT == 0
15203 if (!SCALAR_FLOAT_MODE_P (mode))
15205 mem_loc_result->dw_loc_oprnd2.val_class
15206 = dw_val_class_const_double;
15207 mem_loc_result->dw_loc_oprnd2.v.val_double
15208 = rtx_to_double_int (rtl);
15210 else
15211 #endif
15213 unsigned int length = GET_MODE_SIZE (mode);
15214 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15216 insert_float (rtl, array);
15217 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15218 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15219 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15220 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15223 break;
15225 case CONST_WIDE_INT:
15226 if (!dwarf_strict || dwarf_version >= 5)
15228 dw_die_ref type_die;
15230 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15231 if (type_die == NULL)
15232 return NULL;
15233 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15234 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15235 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15236 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15237 mem_loc_result->dw_loc_oprnd2.val_class
15238 = dw_val_class_wide_int;
15239 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15240 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15242 break;
15244 case EQ:
15245 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15246 break;
15248 case GE:
15249 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15250 break;
15252 case GT:
15253 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15254 break;
15256 case LE:
15257 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15258 break;
15260 case LT:
15261 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15262 break;
15264 case NE:
15265 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15266 break;
15268 case GEU:
15269 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15270 break;
15272 case GTU:
15273 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15274 break;
15276 case LEU:
15277 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15278 break;
15280 case LTU:
15281 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15282 break;
15284 case UMIN:
15285 case UMAX:
15286 if (!SCALAR_INT_MODE_P (mode))
15287 break;
15288 /* FALLTHRU */
15289 case SMIN:
15290 case SMAX:
15291 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15292 break;
15294 case ZERO_EXTRACT:
15295 case SIGN_EXTRACT:
15296 if (CONST_INT_P (XEXP (rtl, 1))
15297 && CONST_INT_P (XEXP (rtl, 2))
15298 && ((unsigned) INTVAL (XEXP (rtl, 1))
15299 + (unsigned) INTVAL (XEXP (rtl, 2))
15300 <= GET_MODE_BITSIZE (mode))
15301 && SCALAR_INT_MODE_P (mode)
15302 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15303 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15305 int shift, size;
15306 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15307 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15308 if (op0 == 0)
15309 break;
15310 if (GET_CODE (rtl) == SIGN_EXTRACT)
15311 op = DW_OP_shra;
15312 else
15313 op = DW_OP_shr;
15314 mem_loc_result = op0;
15315 size = INTVAL (XEXP (rtl, 1));
15316 shift = INTVAL (XEXP (rtl, 2));
15317 if (BITS_BIG_ENDIAN)
15318 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15319 - shift - size;
15320 if (shift + size != (int) DWARF2_ADDR_SIZE)
15322 add_loc_descr (&mem_loc_result,
15323 int_loc_descriptor (DWARF2_ADDR_SIZE
15324 - shift - size));
15325 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15327 if (size != (int) DWARF2_ADDR_SIZE)
15329 add_loc_descr (&mem_loc_result,
15330 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15331 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15334 break;
15336 case IF_THEN_ELSE:
15338 dw_loc_descr_ref op2, bra_node, drop_node;
15339 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15340 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15341 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15342 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15343 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15344 VAR_INIT_STATUS_INITIALIZED);
15345 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15346 VAR_INIT_STATUS_INITIALIZED);
15347 if (op0 == NULL || op1 == NULL || op2 == NULL)
15348 break;
15350 mem_loc_result = op1;
15351 add_loc_descr (&mem_loc_result, op2);
15352 add_loc_descr (&mem_loc_result, op0);
15353 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15354 add_loc_descr (&mem_loc_result, bra_node);
15355 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15356 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15357 add_loc_descr (&mem_loc_result, drop_node);
15358 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15359 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15361 break;
15363 case FLOAT_EXTEND:
15364 case FLOAT_TRUNCATE:
15365 case FLOAT:
15366 case UNSIGNED_FLOAT:
15367 case FIX:
15368 case UNSIGNED_FIX:
15369 if (!dwarf_strict || dwarf_version >= 5)
15371 dw_die_ref type_die;
15372 dw_loc_descr_ref cvt;
15374 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15375 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15376 if (op0 == NULL)
15377 break;
15378 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15379 && (GET_CODE (rtl) == FLOAT
15380 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15381 <= DWARF2_ADDR_SIZE))
15383 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15384 GET_CODE (rtl) == UNSIGNED_FLOAT);
15385 if (type_die == NULL)
15386 break;
15387 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15388 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15389 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15390 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15391 add_loc_descr (&op0, cvt);
15393 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15394 if (type_die == NULL)
15395 break;
15396 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15397 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15398 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15399 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15400 add_loc_descr (&op0, cvt);
15401 if (SCALAR_INT_MODE_P (mode)
15402 && (GET_CODE (rtl) == FIX
15403 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15405 op0 = convert_descriptor_to_mode (mode, op0);
15406 if (op0 == NULL)
15407 break;
15409 mem_loc_result = op0;
15411 break;
15413 case CLZ:
15414 case CTZ:
15415 case FFS:
15416 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15417 break;
15419 case POPCOUNT:
15420 case PARITY:
15421 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15422 break;
15424 case BSWAP:
15425 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15426 break;
15428 case ROTATE:
15429 case ROTATERT:
15430 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15431 break;
15433 case COMPARE:
15434 /* In theory, we could implement the above. */
15435 /* DWARF cannot represent the unsigned compare operations
15436 natively. */
15437 case SS_MULT:
15438 case US_MULT:
15439 case SS_DIV:
15440 case US_DIV:
15441 case SS_PLUS:
15442 case US_PLUS:
15443 case SS_MINUS:
15444 case US_MINUS:
15445 case SS_NEG:
15446 case US_NEG:
15447 case SS_ABS:
15448 case SS_ASHIFT:
15449 case US_ASHIFT:
15450 case SS_TRUNCATE:
15451 case US_TRUNCATE:
15452 case UNORDERED:
15453 case ORDERED:
15454 case UNEQ:
15455 case UNGE:
15456 case UNGT:
15457 case UNLE:
15458 case UNLT:
15459 case LTGT:
15460 case FRACT_CONVERT:
15461 case UNSIGNED_FRACT_CONVERT:
15462 case SAT_FRACT:
15463 case UNSIGNED_SAT_FRACT:
15464 case SQRT:
15465 case ASM_OPERANDS:
15466 case VEC_MERGE:
15467 case VEC_SELECT:
15468 case VEC_CONCAT:
15469 case VEC_DUPLICATE:
15470 case UNSPEC:
15471 case HIGH:
15472 case FMA:
15473 case STRICT_LOW_PART:
15474 case CONST_VECTOR:
15475 case CONST_FIXED:
15476 case CLRSB:
15477 case CLOBBER:
15478 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15479 can't express it in the debug info. This can happen e.g. with some
15480 TLS UNSPECs. */
15481 break;
15483 case CONST_STRING:
15484 resolve_one_addr (&rtl);
15485 goto symref;
15487 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15488 the expression. An UNSPEC rtx represents a raw DWARF operation,
15489 new_loc_descr is called for it to build the operation directly.
15490 Otherwise mem_loc_descriptor is called recursively. */
15491 case PARALLEL:
15493 int index = 0;
15494 dw_loc_descr_ref exp_result = NULL;
15496 for (; index < XVECLEN (rtl, 0); index++)
15498 rtx elem = XVECEXP (rtl, 0, index);
15499 if (GET_CODE (elem) == UNSPEC)
15501 /* Each DWARF operation UNSPEC contain two operands, if
15502 one operand is not used for the operation, const0_rtx is
15503 passed. */
15504 gcc_assert (XVECLEN (elem, 0) == 2);
15506 HOST_WIDE_INT dw_op = XINT (elem, 1);
15507 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15508 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15509 exp_result
15510 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15511 oprnd2);
15513 else
15514 exp_result
15515 = mem_loc_descriptor (elem, mode, mem_mode,
15516 VAR_INIT_STATUS_INITIALIZED);
15518 if (!mem_loc_result)
15519 mem_loc_result = exp_result;
15520 else
15521 add_loc_descr (&mem_loc_result, exp_result);
15524 break;
15527 default:
15528 if (flag_checking)
15530 print_rtl (stderr, rtl);
15531 gcc_unreachable ();
15533 break;
15536 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15537 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15539 return mem_loc_result;
15542 /* Return a descriptor that describes the concatenation of two locations.
15543 This is typically a complex variable. */
15545 static dw_loc_descr_ref
15546 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15548 dw_loc_descr_ref cc_loc_result = NULL;
15549 dw_loc_descr_ref x0_ref
15550 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15551 dw_loc_descr_ref x1_ref
15552 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15554 if (x0_ref == 0 || x1_ref == 0)
15555 return 0;
15557 cc_loc_result = x0_ref;
15558 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15560 add_loc_descr (&cc_loc_result, x1_ref);
15561 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15563 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15564 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15566 return cc_loc_result;
15569 /* Return a descriptor that describes the concatenation of N
15570 locations. */
15572 static dw_loc_descr_ref
15573 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15575 unsigned int i;
15576 dw_loc_descr_ref cc_loc_result = NULL;
15577 unsigned int n = XVECLEN (concatn, 0);
15579 for (i = 0; i < n; ++i)
15581 dw_loc_descr_ref ref;
15582 rtx x = XVECEXP (concatn, 0, i);
15584 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15585 if (ref == NULL)
15586 return NULL;
15588 add_loc_descr (&cc_loc_result, ref);
15589 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15592 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15593 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15595 return cc_loc_result;
15598 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15599 for DEBUG_IMPLICIT_PTR RTL. */
15601 static dw_loc_descr_ref
15602 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15604 dw_loc_descr_ref ret;
15605 dw_die_ref ref;
15607 if (dwarf_strict && dwarf_version < 5)
15608 return NULL;
15609 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15610 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15611 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15612 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15613 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15614 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15615 if (ref)
15617 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15618 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15619 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15621 else
15623 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15624 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15626 return ret;
15629 /* Output a proper Dwarf location descriptor for a variable or parameter
15630 which is either allocated in a register or in a memory location. For a
15631 register, we just generate an OP_REG and the register number. For a
15632 memory location we provide a Dwarf postfix expression describing how to
15633 generate the (dynamic) address of the object onto the address stack.
15635 MODE is mode of the decl if this loc_descriptor is going to be used in
15636 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15637 allowed, VOIDmode otherwise.
15639 If we don't know how to describe it, return 0. */
15641 static dw_loc_descr_ref
15642 loc_descriptor (rtx rtl, machine_mode mode,
15643 enum var_init_status initialized)
15645 dw_loc_descr_ref loc_result = NULL;
15647 switch (GET_CODE (rtl))
15649 case SUBREG:
15650 /* The case of a subreg may arise when we have a local (register)
15651 variable or a formal (register) parameter which doesn't quite fill
15652 up an entire register. For now, just assume that it is
15653 legitimate to make the Dwarf info refer to the whole register which
15654 contains the given subreg. */
15655 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15656 loc_result = loc_descriptor (SUBREG_REG (rtl),
15657 GET_MODE (SUBREG_REG (rtl)), initialized);
15658 else
15659 goto do_default;
15660 break;
15662 case REG:
15663 loc_result = reg_loc_descriptor (rtl, initialized);
15664 break;
15666 case MEM:
15667 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15668 GET_MODE (rtl), initialized);
15669 if (loc_result == NULL)
15670 loc_result = tls_mem_loc_descriptor (rtl);
15671 if (loc_result == NULL)
15673 rtx new_rtl = avoid_constant_pool_reference (rtl);
15674 if (new_rtl != rtl)
15675 loc_result = loc_descriptor (new_rtl, mode, initialized);
15677 break;
15679 case CONCAT:
15680 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15681 initialized);
15682 break;
15684 case CONCATN:
15685 loc_result = concatn_loc_descriptor (rtl, initialized);
15686 break;
15688 case VAR_LOCATION:
15689 /* Single part. */
15690 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15692 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15693 if (GET_CODE (loc) == EXPR_LIST)
15694 loc = XEXP (loc, 0);
15695 loc_result = loc_descriptor (loc, mode, initialized);
15696 break;
15699 rtl = XEXP (rtl, 1);
15700 /* FALLTHRU */
15702 case PARALLEL:
15704 rtvec par_elems = XVEC (rtl, 0);
15705 int num_elem = GET_NUM_ELEM (par_elems);
15706 machine_mode mode;
15707 int i;
15709 /* Create the first one, so we have something to add to. */
15710 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15711 VOIDmode, initialized);
15712 if (loc_result == NULL)
15713 return NULL;
15714 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15715 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15716 for (i = 1; i < num_elem; i++)
15718 dw_loc_descr_ref temp;
15720 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15721 VOIDmode, initialized);
15722 if (temp == NULL)
15723 return NULL;
15724 add_loc_descr (&loc_result, temp);
15725 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15726 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15729 break;
15731 case CONST_INT:
15732 if (mode != VOIDmode && mode != BLKmode)
15733 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15734 INTVAL (rtl));
15735 break;
15737 case CONST_DOUBLE:
15738 if (mode == VOIDmode)
15739 mode = GET_MODE (rtl);
15741 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15743 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15745 /* Note that a CONST_DOUBLE rtx could represent either an integer
15746 or a floating-point constant. A CONST_DOUBLE is used whenever
15747 the constant requires more than one word in order to be
15748 adequately represented. We output CONST_DOUBLEs as blocks. */
15749 loc_result = new_loc_descr (DW_OP_implicit_value,
15750 GET_MODE_SIZE (mode), 0);
15751 #if TARGET_SUPPORTS_WIDE_INT == 0
15752 if (!SCALAR_FLOAT_MODE_P (mode))
15754 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15755 loc_result->dw_loc_oprnd2.v.val_double
15756 = rtx_to_double_int (rtl);
15758 else
15759 #endif
15761 unsigned int length = GET_MODE_SIZE (mode);
15762 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15764 insert_float (rtl, array);
15765 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15766 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15767 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15768 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15771 break;
15773 case CONST_WIDE_INT:
15774 if (mode == VOIDmode)
15775 mode = GET_MODE (rtl);
15777 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15779 loc_result = new_loc_descr (DW_OP_implicit_value,
15780 GET_MODE_SIZE (mode), 0);
15781 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15782 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15783 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15785 break;
15787 case CONST_VECTOR:
15788 if (mode == VOIDmode)
15789 mode = GET_MODE (rtl);
15791 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15793 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15794 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15795 unsigned char *array
15796 = ggc_vec_alloc<unsigned char> (length * elt_size);
15797 unsigned int i;
15798 unsigned char *p;
15799 machine_mode imode = GET_MODE_INNER (mode);
15801 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15802 switch (GET_MODE_CLASS (mode))
15804 case MODE_VECTOR_INT:
15805 for (i = 0, p = array; i < length; i++, p += elt_size)
15807 rtx elt = CONST_VECTOR_ELT (rtl, i);
15808 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15810 break;
15812 case MODE_VECTOR_FLOAT:
15813 for (i = 0, p = array; i < length; i++, p += elt_size)
15815 rtx elt = CONST_VECTOR_ELT (rtl, i);
15816 insert_float (elt, p);
15818 break;
15820 default:
15821 gcc_unreachable ();
15824 loc_result = new_loc_descr (DW_OP_implicit_value,
15825 length * elt_size, 0);
15826 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15827 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15828 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15829 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15831 break;
15833 case CONST:
15834 if (mode == VOIDmode
15835 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15836 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15837 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15839 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15840 break;
15842 /* FALLTHROUGH */
15843 case SYMBOL_REF:
15844 if (!const_ok_for_output (rtl))
15845 break;
15846 /* FALLTHROUGH */
15847 case LABEL_REF:
15848 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15849 && (dwarf_version >= 4 || !dwarf_strict))
15851 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15852 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15853 vec_safe_push (used_rtx_array, rtl);
15855 break;
15857 case DEBUG_IMPLICIT_PTR:
15858 loc_result = implicit_ptr_descriptor (rtl, 0);
15859 break;
15861 case PLUS:
15862 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15863 && CONST_INT_P (XEXP (rtl, 1)))
15865 loc_result
15866 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15867 break;
15869 /* FALLTHRU */
15870 do_default:
15871 default:
15872 if ((SCALAR_INT_MODE_P (mode)
15873 && GET_MODE (rtl) == mode
15874 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15875 && dwarf_version >= 4)
15876 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15878 /* Value expression. */
15879 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15880 if (loc_result)
15881 add_loc_descr (&loc_result,
15882 new_loc_descr (DW_OP_stack_value, 0, 0));
15884 break;
15887 return loc_result;
15890 /* We need to figure out what section we should use as the base for the
15891 address ranges where a given location is valid.
15892 1. If this particular DECL has a section associated with it, use that.
15893 2. If this function has a section associated with it, use that.
15894 3. Otherwise, use the text section.
15895 XXX: If you split a variable across multiple sections, we won't notice. */
15897 static const char *
15898 secname_for_decl (const_tree decl)
15900 const char *secname;
15902 if (VAR_OR_FUNCTION_DECL_P (decl)
15903 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15904 && DECL_SECTION_NAME (decl))
15905 secname = DECL_SECTION_NAME (decl);
15906 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15907 secname = DECL_SECTION_NAME (current_function_decl);
15908 else if (cfun && in_cold_section_p)
15909 secname = crtl->subsections.cold_section_label;
15910 else
15911 secname = text_section_label;
15913 return secname;
15916 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15918 static bool
15919 decl_by_reference_p (tree decl)
15921 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15922 || VAR_P (decl))
15923 && DECL_BY_REFERENCE (decl));
15926 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15927 for VARLOC. */
15929 static dw_loc_descr_ref
15930 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15931 enum var_init_status initialized)
15933 int have_address = 0;
15934 dw_loc_descr_ref descr;
15935 machine_mode mode;
15937 if (want_address != 2)
15939 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15940 /* Single part. */
15941 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15943 varloc = PAT_VAR_LOCATION_LOC (varloc);
15944 if (GET_CODE (varloc) == EXPR_LIST)
15945 varloc = XEXP (varloc, 0);
15946 mode = GET_MODE (varloc);
15947 if (MEM_P (varloc))
15949 rtx addr = XEXP (varloc, 0);
15950 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15951 mode, initialized);
15952 if (descr)
15953 have_address = 1;
15954 else
15956 rtx x = avoid_constant_pool_reference (varloc);
15957 if (x != varloc)
15958 descr = mem_loc_descriptor (x, mode, VOIDmode,
15959 initialized);
15962 else
15963 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15965 else
15966 return 0;
15968 else
15970 if (GET_CODE (varloc) == VAR_LOCATION)
15971 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15972 else
15973 mode = DECL_MODE (loc);
15974 descr = loc_descriptor (varloc, mode, initialized);
15975 have_address = 1;
15978 if (!descr)
15979 return 0;
15981 if (want_address == 2 && !have_address
15982 && (dwarf_version >= 4 || !dwarf_strict))
15984 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15986 expansion_failed (loc, NULL_RTX,
15987 "DWARF address size mismatch");
15988 return 0;
15990 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15991 have_address = 1;
15993 /* Show if we can't fill the request for an address. */
15994 if (want_address && !have_address)
15996 expansion_failed (loc, NULL_RTX,
15997 "Want address and only have value");
15998 return 0;
16001 /* If we've got an address and don't want one, dereference. */
16002 if (!want_address && have_address)
16004 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16005 enum dwarf_location_atom op;
16007 if (size > DWARF2_ADDR_SIZE || size == -1)
16009 expansion_failed (loc, NULL_RTX,
16010 "DWARF address size mismatch");
16011 return 0;
16013 else if (size == DWARF2_ADDR_SIZE)
16014 op = DW_OP_deref;
16015 else
16016 op = DW_OP_deref_size;
16018 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16021 return descr;
16024 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16025 if it is not possible. */
16027 static dw_loc_descr_ref
16028 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16030 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16031 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16032 else if (dwarf_version >= 3 || !dwarf_strict)
16033 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16034 else
16035 return NULL;
16038 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16039 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16041 static dw_loc_descr_ref
16042 dw_sra_loc_expr (tree decl, rtx loc)
16044 rtx p;
16045 unsigned HOST_WIDE_INT padsize = 0;
16046 dw_loc_descr_ref descr, *descr_tail;
16047 unsigned HOST_WIDE_INT decl_size;
16048 rtx varloc;
16049 enum var_init_status initialized;
16051 if (DECL_SIZE (decl) == NULL
16052 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16053 return NULL;
16055 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16056 descr = NULL;
16057 descr_tail = &descr;
16059 for (p = loc; p; p = XEXP (p, 1))
16061 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16062 rtx loc_note = *decl_piece_varloc_ptr (p);
16063 dw_loc_descr_ref cur_descr;
16064 dw_loc_descr_ref *tail, last = NULL;
16065 unsigned HOST_WIDE_INT opsize = 0;
16067 if (loc_note == NULL_RTX
16068 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16070 padsize += bitsize;
16071 continue;
16073 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16074 varloc = NOTE_VAR_LOCATION (loc_note);
16075 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16076 if (cur_descr == NULL)
16078 padsize += bitsize;
16079 continue;
16082 /* Check that cur_descr either doesn't use
16083 DW_OP_*piece operations, or their sum is equal
16084 to bitsize. Otherwise we can't embed it. */
16085 for (tail = &cur_descr; *tail != NULL;
16086 tail = &(*tail)->dw_loc_next)
16087 if ((*tail)->dw_loc_opc == DW_OP_piece)
16089 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16090 * BITS_PER_UNIT;
16091 last = *tail;
16093 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16095 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16096 last = *tail;
16099 if (last != NULL && opsize != bitsize)
16101 padsize += bitsize;
16102 /* Discard the current piece of the descriptor and release any
16103 addr_table entries it uses. */
16104 remove_loc_list_addr_table_entries (cur_descr);
16105 continue;
16108 /* If there is a hole, add DW_OP_*piece after empty DWARF
16109 expression, which means that those bits are optimized out. */
16110 if (padsize)
16112 if (padsize > decl_size)
16114 remove_loc_list_addr_table_entries (cur_descr);
16115 goto discard_descr;
16117 decl_size -= padsize;
16118 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16119 if (*descr_tail == NULL)
16121 remove_loc_list_addr_table_entries (cur_descr);
16122 goto discard_descr;
16124 descr_tail = &(*descr_tail)->dw_loc_next;
16125 padsize = 0;
16127 *descr_tail = cur_descr;
16128 descr_tail = tail;
16129 if (bitsize > decl_size)
16130 goto discard_descr;
16131 decl_size -= bitsize;
16132 if (last == NULL)
16134 HOST_WIDE_INT offset = 0;
16135 if (GET_CODE (varloc) == VAR_LOCATION
16136 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16138 varloc = PAT_VAR_LOCATION_LOC (varloc);
16139 if (GET_CODE (varloc) == EXPR_LIST)
16140 varloc = XEXP (varloc, 0);
16144 if (GET_CODE (varloc) == CONST
16145 || GET_CODE (varloc) == SIGN_EXTEND
16146 || GET_CODE (varloc) == ZERO_EXTEND)
16147 varloc = XEXP (varloc, 0);
16148 else if (GET_CODE (varloc) == SUBREG)
16149 varloc = SUBREG_REG (varloc);
16150 else
16151 break;
16153 while (1);
16154 /* DW_OP_bit_size offset should be zero for register
16155 or implicit location descriptions and empty location
16156 descriptions, but for memory addresses needs big endian
16157 adjustment. */
16158 if (MEM_P (varloc))
16160 unsigned HOST_WIDE_INT memsize
16161 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16162 if (memsize != bitsize)
16164 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16165 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16166 goto discard_descr;
16167 if (memsize < bitsize)
16168 goto discard_descr;
16169 if (BITS_BIG_ENDIAN)
16170 offset = memsize - bitsize;
16174 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16175 if (*descr_tail == NULL)
16176 goto discard_descr;
16177 descr_tail = &(*descr_tail)->dw_loc_next;
16181 /* If there were any non-empty expressions, add padding till the end of
16182 the decl. */
16183 if (descr != NULL && decl_size != 0)
16185 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16186 if (*descr_tail == NULL)
16187 goto discard_descr;
16189 return descr;
16191 discard_descr:
16192 /* Discard the descriptor and release any addr_table entries it uses. */
16193 remove_loc_list_addr_table_entries (descr);
16194 return NULL;
16197 /* Return the dwarf representation of the location list LOC_LIST of
16198 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16199 function. */
16201 static dw_loc_list_ref
16202 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16204 const char *endname, *secname;
16205 rtx varloc;
16206 enum var_init_status initialized;
16207 struct var_loc_node *node;
16208 dw_loc_descr_ref descr;
16209 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16210 dw_loc_list_ref list = NULL;
16211 dw_loc_list_ref *listp = &list;
16213 /* Now that we know what section we are using for a base,
16214 actually construct the list of locations.
16215 The first location information is what is passed to the
16216 function that creates the location list, and the remaining
16217 locations just get added on to that list.
16218 Note that we only know the start address for a location
16219 (IE location changes), so to build the range, we use
16220 the range [current location start, next location start].
16221 This means we have to special case the last node, and generate
16222 a range of [last location start, end of function label]. */
16224 secname = secname_for_decl (decl);
16226 for (node = loc_list->first; node; node = node->next)
16227 if (GET_CODE (node->loc) == EXPR_LIST
16228 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16230 if (GET_CODE (node->loc) == EXPR_LIST)
16232 /* This requires DW_OP_{,bit_}piece, which is not usable
16233 inside DWARF expressions. */
16234 if (want_address != 2)
16235 continue;
16236 descr = dw_sra_loc_expr (decl, node->loc);
16237 if (descr == NULL)
16238 continue;
16240 else
16242 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16243 varloc = NOTE_VAR_LOCATION (node->loc);
16244 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16246 if (descr)
16248 bool range_across_switch = false;
16249 /* If section switch happens in between node->label
16250 and node->next->label (or end of function) and
16251 we can't emit it as a single entry list,
16252 emit two ranges, first one ending at the end
16253 of first partition and second one starting at the
16254 beginning of second partition. */
16255 if (node == loc_list->last_before_switch
16256 && (node != loc_list->first || loc_list->first->next)
16257 && current_function_decl)
16259 endname = cfun->fde->dw_fde_end;
16260 range_across_switch = true;
16262 /* The variable has a location between NODE->LABEL and
16263 NODE->NEXT->LABEL. */
16264 else if (node->next)
16265 endname = node->next->label;
16266 /* If the variable has a location at the last label
16267 it keeps its location until the end of function. */
16268 else if (!current_function_decl)
16269 endname = text_end_label;
16270 else
16272 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16273 current_function_funcdef_no);
16274 endname = ggc_strdup (label_id);
16277 *listp = new_loc_list (descr, node->label, endname, secname);
16278 if (TREE_CODE (decl) == PARM_DECL
16279 && node == loc_list->first
16280 && NOTE_P (node->loc)
16281 && strcmp (node->label, endname) == 0)
16282 (*listp)->force = true;
16283 listp = &(*listp)->dw_loc_next;
16285 if (range_across_switch)
16287 if (GET_CODE (node->loc) == EXPR_LIST)
16288 descr = dw_sra_loc_expr (decl, node->loc);
16289 else
16291 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16292 varloc = NOTE_VAR_LOCATION (node->loc);
16293 descr = dw_loc_list_1 (decl, varloc, want_address,
16294 initialized);
16296 gcc_assert (descr);
16297 /* The variable has a location between NODE->LABEL and
16298 NODE->NEXT->LABEL. */
16299 if (node->next)
16300 endname = node->next->label;
16301 else
16302 endname = cfun->fde->dw_fde_second_end;
16303 *listp = new_loc_list (descr,
16304 cfun->fde->dw_fde_second_begin,
16305 endname, secname);
16306 listp = &(*listp)->dw_loc_next;
16311 /* Try to avoid the overhead of a location list emitting a location
16312 expression instead, but only if we didn't have more than one
16313 location entry in the first place. If some entries were not
16314 representable, we don't want to pretend a single entry that was
16315 applies to the entire scope in which the variable is
16316 available. */
16317 if (list && loc_list->first->next)
16318 gen_llsym (list);
16320 return list;
16323 /* Return if the loc_list has only single element and thus can be represented
16324 as location description. */
16326 static bool
16327 single_element_loc_list_p (dw_loc_list_ref list)
16329 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16330 return !list->ll_symbol;
16333 /* Duplicate a single element of location list. */
16335 static inline dw_loc_descr_ref
16336 copy_loc_descr (dw_loc_descr_ref ref)
16338 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16339 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16340 return copy;
16343 /* To each location in list LIST append loc descr REF. */
16345 static void
16346 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16348 dw_loc_descr_ref copy;
16349 add_loc_descr (&list->expr, ref);
16350 list = list->dw_loc_next;
16351 while (list)
16353 copy = copy_loc_descr (ref);
16354 add_loc_descr (&list->expr, copy);
16355 while (copy->dw_loc_next)
16356 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16357 list = list->dw_loc_next;
16361 /* To each location in list LIST prepend loc descr REF. */
16363 static void
16364 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16366 dw_loc_descr_ref copy;
16367 dw_loc_descr_ref ref_end = list->expr;
16368 add_loc_descr (&ref, list->expr);
16369 list->expr = ref;
16370 list = list->dw_loc_next;
16371 while (list)
16373 dw_loc_descr_ref end = list->expr;
16374 list->expr = copy = copy_loc_descr (ref);
16375 while (copy->dw_loc_next != ref_end)
16376 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16377 copy->dw_loc_next = end;
16378 list = list->dw_loc_next;
16382 /* Given two lists RET and LIST
16383 produce location list that is result of adding expression in LIST
16384 to expression in RET on each position in program.
16385 Might be destructive on both RET and LIST.
16387 TODO: We handle only simple cases of RET or LIST having at most one
16388 element. General case would involve sorting the lists in program order
16389 and merging them that will need some additional work.
16390 Adding that will improve quality of debug info especially for SRA-ed
16391 structures. */
16393 static void
16394 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16396 if (!list)
16397 return;
16398 if (!*ret)
16400 *ret = list;
16401 return;
16403 if (!list->dw_loc_next)
16405 add_loc_descr_to_each (*ret, list->expr);
16406 return;
16408 if (!(*ret)->dw_loc_next)
16410 prepend_loc_descr_to_each (list, (*ret)->expr);
16411 *ret = list;
16412 return;
16414 expansion_failed (NULL_TREE, NULL_RTX,
16415 "Don't know how to merge two non-trivial"
16416 " location lists.\n");
16417 *ret = NULL;
16418 return;
16421 /* LOC is constant expression. Try a luck, look it up in constant
16422 pool and return its loc_descr of its address. */
16424 static dw_loc_descr_ref
16425 cst_pool_loc_descr (tree loc)
16427 /* Get an RTL for this, if something has been emitted. */
16428 rtx rtl = lookup_constant_def (loc);
16430 if (!rtl || !MEM_P (rtl))
16432 gcc_assert (!rtl);
16433 return 0;
16435 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16437 /* TODO: We might get more coverage if we was actually delaying expansion
16438 of all expressions till end of compilation when constant pools are fully
16439 populated. */
16440 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16442 expansion_failed (loc, NULL_RTX,
16443 "CST value in contant pool but not marked.");
16444 return 0;
16446 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16447 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16450 /* Return dw_loc_list representing address of addr_expr LOC
16451 by looking for inner INDIRECT_REF expression and turning
16452 it into simple arithmetics.
16454 See loc_list_from_tree for the meaning of CONTEXT. */
16456 static dw_loc_list_ref
16457 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16458 loc_descr_context *context)
16460 tree obj, offset;
16461 HOST_WIDE_INT bitsize, bitpos, bytepos;
16462 machine_mode mode;
16463 int unsignedp, reversep, volatilep = 0;
16464 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16466 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16467 &bitsize, &bitpos, &offset, &mode,
16468 &unsignedp, &reversep, &volatilep);
16469 STRIP_NOPS (obj);
16470 if (bitpos % BITS_PER_UNIT)
16472 expansion_failed (loc, NULL_RTX, "bitfield access");
16473 return 0;
16475 if (!INDIRECT_REF_P (obj))
16477 expansion_failed (obj,
16478 NULL_RTX, "no indirect ref in inner refrence");
16479 return 0;
16481 if (!offset && !bitpos)
16482 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16483 context);
16484 else if (toplev
16485 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16486 && (dwarf_version >= 4 || !dwarf_strict))
16488 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16489 if (!list_ret)
16490 return 0;
16491 if (offset)
16493 /* Variable offset. */
16494 list_ret1 = loc_list_from_tree (offset, 0, context);
16495 if (list_ret1 == 0)
16496 return 0;
16497 add_loc_list (&list_ret, list_ret1);
16498 if (!list_ret)
16499 return 0;
16500 add_loc_descr_to_each (list_ret,
16501 new_loc_descr (DW_OP_plus, 0, 0));
16503 bytepos = bitpos / BITS_PER_UNIT;
16504 if (bytepos > 0)
16505 add_loc_descr_to_each (list_ret,
16506 new_loc_descr (DW_OP_plus_uconst,
16507 bytepos, 0));
16508 else if (bytepos < 0)
16509 loc_list_plus_const (list_ret, bytepos);
16510 add_loc_descr_to_each (list_ret,
16511 new_loc_descr (DW_OP_stack_value, 0, 0));
16513 return list_ret;
16516 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16517 all operations from LOC are nops, move to the last one. Insert in NOPS all
16518 operations that are skipped. */
16520 static void
16521 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16522 hash_set<dw_loc_descr_ref> &nops)
16524 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16526 nops.add (loc);
16527 loc = loc->dw_loc_next;
16531 /* Helper for loc_descr_without_nops: free the location description operation
16532 P. */
16534 bool
16535 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16537 ggc_free (loc);
16538 return true;
16541 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16542 finishes LOC. */
16544 static void
16545 loc_descr_without_nops (dw_loc_descr_ref &loc)
16547 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16548 return;
16550 /* Set of all DW_OP_nop operations we remove. */
16551 hash_set<dw_loc_descr_ref> nops;
16553 /* First, strip all prefix NOP operations in order to keep the head of the
16554 operations list. */
16555 loc_descr_to_next_no_nop (loc, nops);
16557 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16559 /* For control flow operations: strip "prefix" nops in destination
16560 labels. */
16561 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16562 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16563 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16564 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16566 /* Do the same for the operations that follow, then move to the next
16567 iteration. */
16568 if (cur->dw_loc_next != NULL)
16569 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16570 cur = cur->dw_loc_next;
16573 nops.traverse<void *, free_loc_descr> (NULL);
16577 struct dwarf_procedure_info;
16579 /* Helper structure for location descriptions generation. */
16580 struct loc_descr_context
16582 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16583 NULL_TREE if DW_OP_push_object_address in invalid for this location
16584 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16585 tree context_type;
16586 /* The ..._DECL node that should be translated as a
16587 DW_OP_push_object_address operation. */
16588 tree base_decl;
16589 /* Information about the DWARF procedure we are currently generating. NULL if
16590 we are not generating a DWARF procedure. */
16591 struct dwarf_procedure_info *dpi;
16592 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16593 by consumer. Used for DW_TAG_generic_subrange attributes. */
16594 bool placeholder_arg;
16595 /* True if PLACEHOLDER_EXPR has been seen. */
16596 bool placeholder_seen;
16599 /* DWARF procedures generation
16601 DWARF expressions (aka. location descriptions) are used to encode variable
16602 things such as sizes or offsets. Such computations can have redundant parts
16603 that can be factorized in order to reduce the size of the output debug
16604 information. This is the whole point of DWARF procedures.
16606 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16607 already factorized into functions ("size functions") in order to handle very
16608 big and complex types. Such functions are quite simple: they have integral
16609 arguments, they return an integral result and their body contains only a
16610 return statement with arithmetic expressions. This is the only kind of
16611 function we are interested in translating into DWARF procedures, here.
16613 DWARF expressions and DWARF procedure are executed using a stack, so we have
16614 to define some calling convention for them to interact. Let's say that:
16616 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16617 all arguments in reverse order (right-to-left) so that when the DWARF
16618 procedure execution starts, the first argument is the top of the stack.
16620 - Then, when returning, the DWARF procedure must have consumed all arguments
16621 on the stack, must have pushed the result and touched nothing else.
16623 - Each integral argument and the result are integral types can be hold in a
16624 single stack slot.
16626 - We call "frame offset" the number of stack slots that are "under DWARF
16627 procedure control": it includes the arguments slots, the temporaries and
16628 the result slot. Thus, it is equal to the number of arguments when the
16629 procedure execution starts and must be equal to one (the result) when it
16630 returns. */
16632 /* Helper structure used when generating operations for a DWARF procedure. */
16633 struct dwarf_procedure_info
16635 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16636 currently translated. */
16637 tree fndecl;
16638 /* The number of arguments FNDECL takes. */
16639 unsigned args_count;
16642 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16643 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16644 equate it to this DIE. */
16646 static dw_die_ref
16647 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16648 dw_die_ref parent_die)
16650 dw_die_ref dwarf_proc_die;
16652 if ((dwarf_version < 3 && dwarf_strict)
16653 || location == NULL)
16654 return NULL;
16656 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16657 if (fndecl)
16658 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16659 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16660 return dwarf_proc_die;
16663 /* Return whether TYPE is a supported type as a DWARF procedure argument
16664 type or return type (we handle only scalar types and pointer types that
16665 aren't wider than the DWARF expression evaluation stack. */
16667 static bool
16668 is_handled_procedure_type (tree type)
16670 return ((INTEGRAL_TYPE_P (type)
16671 || TREE_CODE (type) == OFFSET_TYPE
16672 || TREE_CODE (type) == POINTER_TYPE)
16673 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16676 /* Helper for resolve_args_picking: do the same but stop when coming across
16677 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16678 offset *before* evaluating the corresponding operation. */
16680 static bool
16681 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16682 struct dwarf_procedure_info *dpi,
16683 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16685 /* The "frame_offset" identifier is already used to name a macro... */
16686 unsigned frame_offset_ = initial_frame_offset;
16687 dw_loc_descr_ref l;
16689 for (l = loc; l != NULL;)
16691 bool existed;
16692 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16694 /* If we already met this node, there is nothing to compute anymore. */
16695 if (existed)
16697 /* Make sure that the stack size is consistent wherever the execution
16698 flow comes from. */
16699 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16700 break;
16702 l_frame_offset = frame_offset_;
16704 /* If needed, relocate the picking offset with respect to the frame
16705 offset. */
16706 if (l->frame_offset_rel)
16708 unsigned HOST_WIDE_INT off;
16709 switch (l->dw_loc_opc)
16711 case DW_OP_pick:
16712 off = l->dw_loc_oprnd1.v.val_unsigned;
16713 break;
16714 case DW_OP_dup:
16715 off = 0;
16716 break;
16717 case DW_OP_over:
16718 off = 1;
16719 break;
16720 default:
16721 gcc_unreachable ();
16723 /* frame_offset_ is the size of the current stack frame, including
16724 incoming arguments. Besides, the arguments are pushed
16725 right-to-left. Thus, in order to access the Nth argument from
16726 this operation node, the picking has to skip temporaries *plus*
16727 one stack slot per argument (0 for the first one, 1 for the second
16728 one, etc.).
16730 The targetted argument number (N) is already set as the operand,
16731 and the number of temporaries can be computed with:
16732 frame_offsets_ - dpi->args_count */
16733 off += frame_offset_ - dpi->args_count;
16735 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16736 if (off > 255)
16737 return false;
16739 if (off == 0)
16741 l->dw_loc_opc = DW_OP_dup;
16742 l->dw_loc_oprnd1.v.val_unsigned = 0;
16744 else if (off == 1)
16746 l->dw_loc_opc = DW_OP_over;
16747 l->dw_loc_oprnd1.v.val_unsigned = 0;
16749 else
16751 l->dw_loc_opc = DW_OP_pick;
16752 l->dw_loc_oprnd1.v.val_unsigned = off;
16756 /* Update frame_offset according to the effect the current operation has
16757 on the stack. */
16758 switch (l->dw_loc_opc)
16760 case DW_OP_deref:
16761 case DW_OP_swap:
16762 case DW_OP_rot:
16763 case DW_OP_abs:
16764 case DW_OP_neg:
16765 case DW_OP_not:
16766 case DW_OP_plus_uconst:
16767 case DW_OP_skip:
16768 case DW_OP_reg0:
16769 case DW_OP_reg1:
16770 case DW_OP_reg2:
16771 case DW_OP_reg3:
16772 case DW_OP_reg4:
16773 case DW_OP_reg5:
16774 case DW_OP_reg6:
16775 case DW_OP_reg7:
16776 case DW_OP_reg8:
16777 case DW_OP_reg9:
16778 case DW_OP_reg10:
16779 case DW_OP_reg11:
16780 case DW_OP_reg12:
16781 case DW_OP_reg13:
16782 case DW_OP_reg14:
16783 case DW_OP_reg15:
16784 case DW_OP_reg16:
16785 case DW_OP_reg17:
16786 case DW_OP_reg18:
16787 case DW_OP_reg19:
16788 case DW_OP_reg20:
16789 case DW_OP_reg21:
16790 case DW_OP_reg22:
16791 case DW_OP_reg23:
16792 case DW_OP_reg24:
16793 case DW_OP_reg25:
16794 case DW_OP_reg26:
16795 case DW_OP_reg27:
16796 case DW_OP_reg28:
16797 case DW_OP_reg29:
16798 case DW_OP_reg30:
16799 case DW_OP_reg31:
16800 case DW_OP_bregx:
16801 case DW_OP_piece:
16802 case DW_OP_deref_size:
16803 case DW_OP_nop:
16804 case DW_OP_bit_piece:
16805 case DW_OP_implicit_value:
16806 case DW_OP_stack_value:
16807 break;
16809 case DW_OP_addr:
16810 case DW_OP_const1u:
16811 case DW_OP_const1s:
16812 case DW_OP_const2u:
16813 case DW_OP_const2s:
16814 case DW_OP_const4u:
16815 case DW_OP_const4s:
16816 case DW_OP_const8u:
16817 case DW_OP_const8s:
16818 case DW_OP_constu:
16819 case DW_OP_consts:
16820 case DW_OP_dup:
16821 case DW_OP_over:
16822 case DW_OP_pick:
16823 case DW_OP_lit0:
16824 case DW_OP_lit1:
16825 case DW_OP_lit2:
16826 case DW_OP_lit3:
16827 case DW_OP_lit4:
16828 case DW_OP_lit5:
16829 case DW_OP_lit6:
16830 case DW_OP_lit7:
16831 case DW_OP_lit8:
16832 case DW_OP_lit9:
16833 case DW_OP_lit10:
16834 case DW_OP_lit11:
16835 case DW_OP_lit12:
16836 case DW_OP_lit13:
16837 case DW_OP_lit14:
16838 case DW_OP_lit15:
16839 case DW_OP_lit16:
16840 case DW_OP_lit17:
16841 case DW_OP_lit18:
16842 case DW_OP_lit19:
16843 case DW_OP_lit20:
16844 case DW_OP_lit21:
16845 case DW_OP_lit22:
16846 case DW_OP_lit23:
16847 case DW_OP_lit24:
16848 case DW_OP_lit25:
16849 case DW_OP_lit26:
16850 case DW_OP_lit27:
16851 case DW_OP_lit28:
16852 case DW_OP_lit29:
16853 case DW_OP_lit30:
16854 case DW_OP_lit31:
16855 case DW_OP_breg0:
16856 case DW_OP_breg1:
16857 case DW_OP_breg2:
16858 case DW_OP_breg3:
16859 case DW_OP_breg4:
16860 case DW_OP_breg5:
16861 case DW_OP_breg6:
16862 case DW_OP_breg7:
16863 case DW_OP_breg8:
16864 case DW_OP_breg9:
16865 case DW_OP_breg10:
16866 case DW_OP_breg11:
16867 case DW_OP_breg12:
16868 case DW_OP_breg13:
16869 case DW_OP_breg14:
16870 case DW_OP_breg15:
16871 case DW_OP_breg16:
16872 case DW_OP_breg17:
16873 case DW_OP_breg18:
16874 case DW_OP_breg19:
16875 case DW_OP_breg20:
16876 case DW_OP_breg21:
16877 case DW_OP_breg22:
16878 case DW_OP_breg23:
16879 case DW_OP_breg24:
16880 case DW_OP_breg25:
16881 case DW_OP_breg26:
16882 case DW_OP_breg27:
16883 case DW_OP_breg28:
16884 case DW_OP_breg29:
16885 case DW_OP_breg30:
16886 case DW_OP_breg31:
16887 case DW_OP_fbreg:
16888 case DW_OP_push_object_address:
16889 case DW_OP_call_frame_cfa:
16890 case DW_OP_GNU_variable_value:
16891 ++frame_offset_;
16892 break;
16894 case DW_OP_drop:
16895 case DW_OP_xderef:
16896 case DW_OP_and:
16897 case DW_OP_div:
16898 case DW_OP_minus:
16899 case DW_OP_mod:
16900 case DW_OP_mul:
16901 case DW_OP_or:
16902 case DW_OP_plus:
16903 case DW_OP_shl:
16904 case DW_OP_shr:
16905 case DW_OP_shra:
16906 case DW_OP_xor:
16907 case DW_OP_bra:
16908 case DW_OP_eq:
16909 case DW_OP_ge:
16910 case DW_OP_gt:
16911 case DW_OP_le:
16912 case DW_OP_lt:
16913 case DW_OP_ne:
16914 case DW_OP_regx:
16915 case DW_OP_xderef_size:
16916 --frame_offset_;
16917 break;
16919 case DW_OP_call2:
16920 case DW_OP_call4:
16921 case DW_OP_call_ref:
16923 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16924 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16926 if (stack_usage == NULL)
16927 return false;
16928 frame_offset_ += *stack_usage;
16929 break;
16932 case DW_OP_implicit_pointer:
16933 case DW_OP_entry_value:
16934 case DW_OP_const_type:
16935 case DW_OP_regval_type:
16936 case DW_OP_deref_type:
16937 case DW_OP_convert:
16938 case DW_OP_reinterpret:
16939 case DW_OP_form_tls_address:
16940 case DW_OP_GNU_push_tls_address:
16941 case DW_OP_GNU_uninit:
16942 case DW_OP_GNU_encoded_addr:
16943 case DW_OP_GNU_implicit_pointer:
16944 case DW_OP_GNU_entry_value:
16945 case DW_OP_GNU_const_type:
16946 case DW_OP_GNU_regval_type:
16947 case DW_OP_GNU_deref_type:
16948 case DW_OP_GNU_convert:
16949 case DW_OP_GNU_reinterpret:
16950 case DW_OP_GNU_parameter_ref:
16951 /* loc_list_from_tree will probably not output these operations for
16952 size functions, so assume they will not appear here. */
16953 /* Fall through... */
16955 default:
16956 gcc_unreachable ();
16959 /* Now, follow the control flow (except subroutine calls). */
16960 switch (l->dw_loc_opc)
16962 case DW_OP_bra:
16963 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16964 frame_offsets))
16965 return false;
16966 /* Fall through. */
16968 case DW_OP_skip:
16969 l = l->dw_loc_oprnd1.v.val_loc;
16970 break;
16972 case DW_OP_stack_value:
16973 return true;
16975 default:
16976 l = l->dw_loc_next;
16977 break;
16981 return true;
16984 /* Make a DFS over operations reachable through LOC (i.e. follow branch
16985 operations) in order to resolve the operand of DW_OP_pick operations that
16986 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
16987 offset *before* LOC is executed. Return if all relocations were
16988 successful. */
16990 static bool
16991 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16992 struct dwarf_procedure_info *dpi)
16994 /* Associate to all visited operations the frame offset *before* evaluating
16995 this operation. */
16996 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
16998 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
16999 frame_offsets);
17002 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17003 Return NULL if it is not possible. */
17005 static dw_die_ref
17006 function_to_dwarf_procedure (tree fndecl)
17008 struct loc_descr_context ctx;
17009 struct dwarf_procedure_info dpi;
17010 dw_die_ref dwarf_proc_die;
17011 tree tree_body = DECL_SAVED_TREE (fndecl);
17012 dw_loc_descr_ref loc_body, epilogue;
17014 tree cursor;
17015 unsigned i;
17017 /* Do not generate multiple DWARF procedures for the same function
17018 declaration. */
17019 dwarf_proc_die = lookup_decl_die (fndecl);
17020 if (dwarf_proc_die != NULL)
17021 return dwarf_proc_die;
17023 /* DWARF procedures are available starting with the DWARFv3 standard. */
17024 if (dwarf_version < 3 && dwarf_strict)
17025 return NULL;
17027 /* We handle only functions for which we still have a body, that return a
17028 supported type and that takes arguments with supported types. Note that
17029 there is no point translating functions that return nothing. */
17030 if (tree_body == NULL_TREE
17031 || DECL_RESULT (fndecl) == NULL_TREE
17032 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17033 return NULL;
17035 for (cursor = DECL_ARGUMENTS (fndecl);
17036 cursor != NULL_TREE;
17037 cursor = TREE_CHAIN (cursor))
17038 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17039 return NULL;
17041 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17042 if (TREE_CODE (tree_body) != RETURN_EXPR)
17043 return NULL;
17044 tree_body = TREE_OPERAND (tree_body, 0);
17045 if (TREE_CODE (tree_body) != MODIFY_EXPR
17046 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17047 return NULL;
17048 tree_body = TREE_OPERAND (tree_body, 1);
17050 /* Try to translate the body expression itself. Note that this will probably
17051 cause an infinite recursion if its call graph has a cycle. This is very
17052 unlikely for size functions, however, so don't bother with such things at
17053 the moment. */
17054 ctx.context_type = NULL_TREE;
17055 ctx.base_decl = NULL_TREE;
17056 ctx.dpi = &dpi;
17057 ctx.placeholder_arg = false;
17058 ctx.placeholder_seen = false;
17059 dpi.fndecl = fndecl;
17060 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17061 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17062 if (!loc_body)
17063 return NULL;
17065 /* After evaluating all operands in "loc_body", we should still have on the
17066 stack all arguments plus the desired function result (top of the stack).
17067 Generate code in order to keep only the result in our stack frame. */
17068 epilogue = NULL;
17069 for (i = 0; i < dpi.args_count; ++i)
17071 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17072 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17073 op_couple->dw_loc_next->dw_loc_next = epilogue;
17074 epilogue = op_couple;
17076 add_loc_descr (&loc_body, epilogue);
17077 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17078 return NULL;
17080 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17081 because they are considered useful. Now there is an epilogue, they are
17082 not anymore, so give it another try. */
17083 loc_descr_without_nops (loc_body);
17085 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17086 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17087 though, given that size functions do not come from source, so they should
17088 not have a dedicated DW_TAG_subprogram DIE. */
17089 dwarf_proc_die
17090 = new_dwarf_proc_die (loc_body, fndecl,
17091 get_context_die (DECL_CONTEXT (fndecl)));
17093 /* The called DWARF procedure consumes one stack slot per argument and
17094 returns one stack slot. */
17095 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17097 return dwarf_proc_die;
17101 /* Generate Dwarf location list representing LOC.
17102 If WANT_ADDRESS is false, expression computing LOC will be computed
17103 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17104 if WANT_ADDRESS is 2, expression computing address useable in location
17105 will be returned (i.e. DW_OP_reg can be used
17106 to refer to register values).
17108 CONTEXT provides information to customize the location descriptions
17109 generation. Its context_type field specifies what type is implicitly
17110 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17111 will not be generated.
17113 Its DPI field determines whether we are generating a DWARF expression for a
17114 DWARF procedure, so PARM_DECL references are processed specifically.
17116 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17117 and dpi fields were null. */
17119 static dw_loc_list_ref
17120 loc_list_from_tree_1 (tree loc, int want_address,
17121 struct loc_descr_context *context)
17123 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17124 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17125 int have_address = 0;
17126 enum dwarf_location_atom op;
17128 /* ??? Most of the time we do not take proper care for sign/zero
17129 extending the values properly. Hopefully this won't be a real
17130 problem... */
17132 if (context != NULL
17133 && context->base_decl == loc
17134 && want_address == 0)
17136 if (dwarf_version >= 3 || !dwarf_strict)
17137 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17138 NULL, NULL, NULL);
17139 else
17140 return NULL;
17143 switch (TREE_CODE (loc))
17145 case ERROR_MARK:
17146 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17147 return 0;
17149 case PLACEHOLDER_EXPR:
17150 /* This case involves extracting fields from an object to determine the
17151 position of other fields. It is supposed to appear only as the first
17152 operand of COMPONENT_REF nodes and to reference precisely the type
17153 that the context allows. */
17154 if (context != NULL
17155 && TREE_TYPE (loc) == context->context_type
17156 && want_address >= 1)
17158 if (dwarf_version >= 3 || !dwarf_strict)
17160 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17161 have_address = 1;
17162 break;
17164 else
17165 return NULL;
17167 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17168 the single argument passed by consumer. */
17169 else if (context != NULL
17170 && context->placeholder_arg
17171 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17172 && want_address == 0)
17174 ret = new_loc_descr (DW_OP_pick, 0, 0);
17175 ret->frame_offset_rel = 1;
17176 context->placeholder_seen = true;
17177 break;
17179 else
17180 expansion_failed (loc, NULL_RTX,
17181 "PLACEHOLDER_EXPR for an unexpected type");
17182 break;
17184 case CALL_EXPR:
17186 const int nargs = call_expr_nargs (loc);
17187 tree callee = get_callee_fndecl (loc);
17188 int i;
17189 dw_die_ref dwarf_proc;
17191 if (callee == NULL_TREE)
17192 goto call_expansion_failed;
17194 /* We handle only functions that return an integer. */
17195 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17196 goto call_expansion_failed;
17198 dwarf_proc = function_to_dwarf_procedure (callee);
17199 if (dwarf_proc == NULL)
17200 goto call_expansion_failed;
17202 /* Evaluate arguments right-to-left so that the first argument will
17203 be the top-most one on the stack. */
17204 for (i = nargs - 1; i >= 0; --i)
17206 dw_loc_descr_ref loc_descr
17207 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17208 context);
17210 if (loc_descr == NULL)
17211 goto call_expansion_failed;
17213 add_loc_descr (&ret, loc_descr);
17216 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17217 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17218 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17219 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17220 add_loc_descr (&ret, ret1);
17221 break;
17223 call_expansion_failed:
17224 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17225 /* There are no opcodes for these operations. */
17226 return 0;
17229 case PREINCREMENT_EXPR:
17230 case PREDECREMENT_EXPR:
17231 case POSTINCREMENT_EXPR:
17232 case POSTDECREMENT_EXPR:
17233 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17234 /* There are no opcodes for these operations. */
17235 return 0;
17237 case ADDR_EXPR:
17238 /* If we already want an address, see if there is INDIRECT_REF inside
17239 e.g. for &this->field. */
17240 if (want_address)
17242 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17243 (loc, want_address == 2, context);
17244 if (list_ret)
17245 have_address = 1;
17246 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17247 && (ret = cst_pool_loc_descr (loc)))
17248 have_address = 1;
17250 /* Otherwise, process the argument and look for the address. */
17251 if (!list_ret && !ret)
17252 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17253 else
17255 if (want_address)
17256 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17257 return NULL;
17259 break;
17261 case VAR_DECL:
17262 if (DECL_THREAD_LOCAL_P (loc))
17264 rtx rtl;
17265 enum dwarf_location_atom tls_op;
17266 enum dtprel_bool dtprel = dtprel_false;
17268 if (targetm.have_tls)
17270 /* If this is not defined, we have no way to emit the
17271 data. */
17272 if (!targetm.asm_out.output_dwarf_dtprel)
17273 return 0;
17275 /* The way DW_OP_GNU_push_tls_address is specified, we
17276 can only look up addresses of objects in the current
17277 module. We used DW_OP_addr as first op, but that's
17278 wrong, because DW_OP_addr is relocated by the debug
17279 info consumer, while DW_OP_GNU_push_tls_address
17280 operand shouldn't be. */
17281 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17282 return 0;
17283 dtprel = dtprel_true;
17284 /* We check for DWARF 5 here because gdb did not implement
17285 DW_OP_form_tls_address until after 7.12. */
17286 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17287 : DW_OP_GNU_push_tls_address);
17289 else
17291 if (!targetm.emutls.debug_form_tls_address
17292 || !(dwarf_version >= 3 || !dwarf_strict))
17293 return 0;
17294 /* We stuffed the control variable into the DECL_VALUE_EXPR
17295 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17296 no longer appear in gimple code. We used the control
17297 variable in specific so that we could pick it up here. */
17298 loc = DECL_VALUE_EXPR (loc);
17299 tls_op = DW_OP_form_tls_address;
17302 rtl = rtl_for_decl_location (loc);
17303 if (rtl == NULL_RTX)
17304 return 0;
17306 if (!MEM_P (rtl))
17307 return 0;
17308 rtl = XEXP (rtl, 0);
17309 if (! CONSTANT_P (rtl))
17310 return 0;
17312 ret = new_addr_loc_descr (rtl, dtprel);
17313 ret1 = new_loc_descr (tls_op, 0, 0);
17314 add_loc_descr (&ret, ret1);
17316 have_address = 1;
17317 break;
17319 /* FALLTHRU */
17321 case PARM_DECL:
17322 if (context != NULL && context->dpi != NULL
17323 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17325 /* We are generating code for a DWARF procedure and we want to access
17326 one of its arguments: find the appropriate argument offset and let
17327 the resolve_args_picking pass compute the offset that complies
17328 with the stack frame size. */
17329 unsigned i = 0;
17330 tree cursor;
17332 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17333 cursor != NULL_TREE && cursor != loc;
17334 cursor = TREE_CHAIN (cursor), ++i)
17336 /* If we are translating a DWARF procedure, all referenced parameters
17337 must belong to the current function. */
17338 gcc_assert (cursor != NULL_TREE);
17340 ret = new_loc_descr (DW_OP_pick, i, 0);
17341 ret->frame_offset_rel = 1;
17342 break;
17344 /* FALLTHRU */
17346 case RESULT_DECL:
17347 if (DECL_HAS_VALUE_EXPR_P (loc))
17348 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17349 want_address, context);
17350 /* FALLTHRU */
17352 case FUNCTION_DECL:
17354 rtx rtl;
17355 var_loc_list *loc_list = lookup_decl_loc (loc);
17357 if (loc_list && loc_list->first)
17359 list_ret = dw_loc_list (loc_list, loc, want_address);
17360 have_address = want_address != 0;
17361 break;
17363 rtl = rtl_for_decl_location (loc);
17364 if (rtl == NULL_RTX)
17366 if (TREE_CODE (loc) != FUNCTION_DECL
17367 && early_dwarf
17368 && current_function_decl
17369 && want_address != 1
17370 && ! DECL_IGNORED_P (loc)
17371 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17372 || POINTER_TYPE_P (TREE_TYPE (loc)))
17373 && DECL_CONTEXT (loc) == current_function_decl
17374 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17375 <= DWARF2_ADDR_SIZE))
17377 dw_die_ref ref = lookup_decl_die (loc);
17378 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17379 if (ref)
17381 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17382 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17383 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17385 else
17387 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17388 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17390 break;
17392 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17393 return 0;
17395 else if (CONST_INT_P (rtl))
17397 HOST_WIDE_INT val = INTVAL (rtl);
17398 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17399 val &= GET_MODE_MASK (DECL_MODE (loc));
17400 ret = int_loc_descriptor (val);
17402 else if (GET_CODE (rtl) == CONST_STRING)
17404 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17405 return 0;
17407 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17408 ret = new_addr_loc_descr (rtl, dtprel_false);
17409 else
17411 machine_mode mode, mem_mode;
17413 /* Certain constructs can only be represented at top-level. */
17414 if (want_address == 2)
17416 ret = loc_descriptor (rtl, VOIDmode,
17417 VAR_INIT_STATUS_INITIALIZED);
17418 have_address = 1;
17420 else
17422 mode = GET_MODE (rtl);
17423 mem_mode = VOIDmode;
17424 if (MEM_P (rtl))
17426 mem_mode = mode;
17427 mode = get_address_mode (rtl);
17428 rtl = XEXP (rtl, 0);
17429 have_address = 1;
17431 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17432 VAR_INIT_STATUS_INITIALIZED);
17434 if (!ret)
17435 expansion_failed (loc, rtl,
17436 "failed to produce loc descriptor for rtl");
17439 break;
17441 case MEM_REF:
17442 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17444 have_address = 1;
17445 goto do_plus;
17447 /* Fallthru. */
17448 case INDIRECT_REF:
17449 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17450 have_address = 1;
17451 break;
17453 case TARGET_MEM_REF:
17454 case SSA_NAME:
17455 case DEBUG_EXPR_DECL:
17456 return NULL;
17458 case COMPOUND_EXPR:
17459 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17460 context);
17462 CASE_CONVERT:
17463 case VIEW_CONVERT_EXPR:
17464 case SAVE_EXPR:
17465 case MODIFY_EXPR:
17466 case NON_LVALUE_EXPR:
17467 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17468 context);
17470 case COMPONENT_REF:
17471 case BIT_FIELD_REF:
17472 case ARRAY_REF:
17473 case ARRAY_RANGE_REF:
17474 case REALPART_EXPR:
17475 case IMAGPART_EXPR:
17477 tree obj, offset;
17478 HOST_WIDE_INT bitsize, bitpos, bytepos;
17479 machine_mode mode;
17480 int unsignedp, reversep, volatilep = 0;
17482 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17483 &unsignedp, &reversep, &volatilep);
17485 gcc_assert (obj != loc);
17487 list_ret = loc_list_from_tree_1 (obj,
17488 want_address == 2
17489 && !bitpos && !offset ? 2 : 1,
17490 context);
17491 /* TODO: We can extract value of the small expression via shifting even
17492 for nonzero bitpos. */
17493 if (list_ret == 0)
17494 return 0;
17495 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17497 expansion_failed (loc, NULL_RTX,
17498 "bitfield access");
17499 return 0;
17502 if (offset != NULL_TREE)
17504 /* Variable offset. */
17505 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17506 if (list_ret1 == 0)
17507 return 0;
17508 add_loc_list (&list_ret, list_ret1);
17509 if (!list_ret)
17510 return 0;
17511 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17514 bytepos = bitpos / BITS_PER_UNIT;
17515 if (bytepos > 0)
17516 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17517 else if (bytepos < 0)
17518 loc_list_plus_const (list_ret, bytepos);
17520 have_address = 1;
17521 break;
17524 case INTEGER_CST:
17525 if ((want_address || !tree_fits_shwi_p (loc))
17526 && (ret = cst_pool_loc_descr (loc)))
17527 have_address = 1;
17528 else if (want_address == 2
17529 && tree_fits_shwi_p (loc)
17530 && (ret = address_of_int_loc_descriptor
17531 (int_size_in_bytes (TREE_TYPE (loc)),
17532 tree_to_shwi (loc))))
17533 have_address = 1;
17534 else if (tree_fits_shwi_p (loc))
17535 ret = int_loc_descriptor (tree_to_shwi (loc));
17536 else if (tree_fits_uhwi_p (loc))
17537 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17538 else
17540 expansion_failed (loc, NULL_RTX,
17541 "Integer operand is not host integer");
17542 return 0;
17544 break;
17546 case CONSTRUCTOR:
17547 case REAL_CST:
17548 case STRING_CST:
17549 case COMPLEX_CST:
17550 if ((ret = cst_pool_loc_descr (loc)))
17551 have_address = 1;
17552 else if (TREE_CODE (loc) == CONSTRUCTOR)
17554 tree type = TREE_TYPE (loc);
17555 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17556 unsigned HOST_WIDE_INT offset = 0;
17557 unsigned HOST_WIDE_INT cnt;
17558 constructor_elt *ce;
17560 if (TREE_CODE (type) == RECORD_TYPE)
17562 /* This is very limited, but it's enough to output
17563 pointers to member functions, as long as the
17564 referenced function is defined in the current
17565 translation unit. */
17566 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17568 tree val = ce->value;
17570 tree field = ce->index;
17572 if (val)
17573 STRIP_NOPS (val);
17575 if (!field || DECL_BIT_FIELD (field))
17577 expansion_failed (loc, NULL_RTX,
17578 "bitfield in record type constructor");
17579 size = offset = (unsigned HOST_WIDE_INT)-1;
17580 ret = NULL;
17581 break;
17584 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17585 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17586 gcc_assert (pos + fieldsize <= size);
17587 if (pos < offset)
17589 expansion_failed (loc, NULL_RTX,
17590 "out-of-order fields in record constructor");
17591 size = offset = (unsigned HOST_WIDE_INT)-1;
17592 ret = NULL;
17593 break;
17595 if (pos > offset)
17597 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17598 add_loc_descr (&ret, ret1);
17599 offset = pos;
17601 if (val && fieldsize != 0)
17603 ret1 = loc_descriptor_from_tree (val, want_address, context);
17604 if (!ret1)
17606 expansion_failed (loc, NULL_RTX,
17607 "unsupported expression in field");
17608 size = offset = (unsigned HOST_WIDE_INT)-1;
17609 ret = NULL;
17610 break;
17612 add_loc_descr (&ret, ret1);
17614 if (fieldsize)
17616 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17617 add_loc_descr (&ret, ret1);
17618 offset = pos + fieldsize;
17622 if (offset != size)
17624 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17625 add_loc_descr (&ret, ret1);
17626 offset = size;
17629 have_address = !!want_address;
17631 else
17632 expansion_failed (loc, NULL_RTX,
17633 "constructor of non-record type");
17635 else
17636 /* We can construct small constants here using int_loc_descriptor. */
17637 expansion_failed (loc, NULL_RTX,
17638 "constructor or constant not in constant pool");
17639 break;
17641 case TRUTH_AND_EXPR:
17642 case TRUTH_ANDIF_EXPR:
17643 case BIT_AND_EXPR:
17644 op = DW_OP_and;
17645 goto do_binop;
17647 case TRUTH_XOR_EXPR:
17648 case BIT_XOR_EXPR:
17649 op = DW_OP_xor;
17650 goto do_binop;
17652 case TRUTH_OR_EXPR:
17653 case TRUTH_ORIF_EXPR:
17654 case BIT_IOR_EXPR:
17655 op = DW_OP_or;
17656 goto do_binop;
17658 case FLOOR_DIV_EXPR:
17659 case CEIL_DIV_EXPR:
17660 case ROUND_DIV_EXPR:
17661 case TRUNC_DIV_EXPR:
17662 case EXACT_DIV_EXPR:
17663 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17664 return 0;
17665 op = DW_OP_div;
17666 goto do_binop;
17668 case MINUS_EXPR:
17669 op = DW_OP_minus;
17670 goto do_binop;
17672 case FLOOR_MOD_EXPR:
17673 case CEIL_MOD_EXPR:
17674 case ROUND_MOD_EXPR:
17675 case TRUNC_MOD_EXPR:
17676 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17678 op = DW_OP_mod;
17679 goto do_binop;
17681 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17682 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17683 if (list_ret == 0 || list_ret1 == 0)
17684 return 0;
17686 add_loc_list (&list_ret, list_ret1);
17687 if (list_ret == 0)
17688 return 0;
17689 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17690 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17691 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17692 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17693 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17694 break;
17696 case MULT_EXPR:
17697 op = DW_OP_mul;
17698 goto do_binop;
17700 case LSHIFT_EXPR:
17701 op = DW_OP_shl;
17702 goto do_binop;
17704 case RSHIFT_EXPR:
17705 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17706 goto do_binop;
17708 case POINTER_PLUS_EXPR:
17709 case PLUS_EXPR:
17710 do_plus:
17711 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17713 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17714 smarter to encode their opposite. The DW_OP_plus_uconst operation
17715 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17716 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17717 bytes, Y being the size of the operation that pushes the opposite
17718 of the addend. So let's choose the smallest representation. */
17719 const tree tree_addend = TREE_OPERAND (loc, 1);
17720 offset_int wi_addend;
17721 HOST_WIDE_INT shwi_addend;
17722 dw_loc_descr_ref loc_naddend;
17724 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17725 if (list_ret == 0)
17726 return 0;
17728 /* Try to get the literal to push. It is the opposite of the addend,
17729 so as we rely on wrapping during DWARF evaluation, first decode
17730 the literal as a "DWARF-sized" signed number. */
17731 wi_addend = wi::to_offset (tree_addend);
17732 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17733 shwi_addend = wi_addend.to_shwi ();
17734 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17735 ? int_loc_descriptor (-shwi_addend)
17736 : NULL;
17738 if (loc_naddend != NULL
17739 && ((unsigned) size_of_uleb128 (shwi_addend)
17740 > size_of_loc_descr (loc_naddend)))
17742 add_loc_descr_to_each (list_ret, loc_naddend);
17743 add_loc_descr_to_each (list_ret,
17744 new_loc_descr (DW_OP_minus, 0, 0));
17746 else
17748 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17750 loc_naddend = loc_cur;
17751 loc_cur = loc_cur->dw_loc_next;
17752 ggc_free (loc_naddend);
17754 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17756 break;
17759 op = DW_OP_plus;
17760 goto do_binop;
17762 case LE_EXPR:
17763 op = DW_OP_le;
17764 goto do_comp_binop;
17766 case GE_EXPR:
17767 op = DW_OP_ge;
17768 goto do_comp_binop;
17770 case LT_EXPR:
17771 op = DW_OP_lt;
17772 goto do_comp_binop;
17774 case GT_EXPR:
17775 op = DW_OP_gt;
17776 goto do_comp_binop;
17778 do_comp_binop:
17779 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17781 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17782 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17783 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17784 TREE_CODE (loc));
17785 break;
17787 else
17788 goto do_binop;
17790 case EQ_EXPR:
17791 op = DW_OP_eq;
17792 goto do_binop;
17794 case NE_EXPR:
17795 op = DW_OP_ne;
17796 goto do_binop;
17798 do_binop:
17799 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17800 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17801 if (list_ret == 0 || list_ret1 == 0)
17802 return 0;
17804 add_loc_list (&list_ret, list_ret1);
17805 if (list_ret == 0)
17806 return 0;
17807 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17808 break;
17810 case TRUTH_NOT_EXPR:
17811 case BIT_NOT_EXPR:
17812 op = DW_OP_not;
17813 goto do_unop;
17815 case ABS_EXPR:
17816 op = DW_OP_abs;
17817 goto do_unop;
17819 case NEGATE_EXPR:
17820 op = DW_OP_neg;
17821 goto do_unop;
17823 do_unop:
17824 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17825 if (list_ret == 0)
17826 return 0;
17828 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17829 break;
17831 case MIN_EXPR:
17832 case MAX_EXPR:
17834 const enum tree_code code =
17835 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17837 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17838 build2 (code, integer_type_node,
17839 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17840 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17843 /* fall through */
17845 case COND_EXPR:
17847 dw_loc_descr_ref lhs
17848 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17849 dw_loc_list_ref rhs
17850 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17851 dw_loc_descr_ref bra_node, jump_node, tmp;
17853 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17854 if (list_ret == 0 || lhs == 0 || rhs == 0)
17855 return 0;
17857 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17858 add_loc_descr_to_each (list_ret, bra_node);
17860 add_loc_list (&list_ret, rhs);
17861 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17862 add_loc_descr_to_each (list_ret, jump_node);
17864 add_loc_descr_to_each (list_ret, lhs);
17865 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17866 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17868 /* ??? Need a node to point the skip at. Use a nop. */
17869 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17870 add_loc_descr_to_each (list_ret, tmp);
17871 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17872 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17874 break;
17876 case FIX_TRUNC_EXPR:
17877 return 0;
17879 default:
17880 /* Leave front-end specific codes as simply unknown. This comes
17881 up, for instance, with the C STMT_EXPR. */
17882 if ((unsigned int) TREE_CODE (loc)
17883 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17885 expansion_failed (loc, NULL_RTX,
17886 "language specific tree node");
17887 return 0;
17890 /* Otherwise this is a generic code; we should just lists all of
17891 these explicitly. We forgot one. */
17892 if (flag_checking)
17893 gcc_unreachable ();
17895 /* In a release build, we want to degrade gracefully: better to
17896 generate incomplete debugging information than to crash. */
17897 return NULL;
17900 if (!ret && !list_ret)
17901 return 0;
17903 if (want_address == 2 && !have_address
17904 && (dwarf_version >= 4 || !dwarf_strict))
17906 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17908 expansion_failed (loc, NULL_RTX,
17909 "DWARF address size mismatch");
17910 return 0;
17912 if (ret)
17913 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17914 else
17915 add_loc_descr_to_each (list_ret,
17916 new_loc_descr (DW_OP_stack_value, 0, 0));
17917 have_address = 1;
17919 /* Show if we can't fill the request for an address. */
17920 if (want_address && !have_address)
17922 expansion_failed (loc, NULL_RTX,
17923 "Want address and only have value");
17924 return 0;
17927 gcc_assert (!ret || !list_ret);
17929 /* If we've got an address and don't want one, dereference. */
17930 if (!want_address && have_address)
17932 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17934 if (size > DWARF2_ADDR_SIZE || size == -1)
17936 expansion_failed (loc, NULL_RTX,
17937 "DWARF address size mismatch");
17938 return 0;
17940 else if (size == DWARF2_ADDR_SIZE)
17941 op = DW_OP_deref;
17942 else
17943 op = DW_OP_deref_size;
17945 if (ret)
17946 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17947 else
17948 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17950 if (ret)
17951 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17953 return list_ret;
17956 /* Likewise, but strip useless DW_OP_nop operations in the resulting
17957 expressions. */
17959 static dw_loc_list_ref
17960 loc_list_from_tree (tree loc, int want_address,
17961 struct loc_descr_context *context)
17963 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17965 for (dw_loc_list_ref loc_cur = result;
17966 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17967 loc_descr_without_nops (loc_cur->expr);
17968 return result;
17971 /* Same as above but return only single location expression. */
17972 static dw_loc_descr_ref
17973 loc_descriptor_from_tree (tree loc, int want_address,
17974 struct loc_descr_context *context)
17976 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
17977 if (!ret)
17978 return NULL;
17979 if (ret->dw_loc_next)
17981 expansion_failed (loc, NULL_RTX,
17982 "Location list where only loc descriptor needed");
17983 return NULL;
17985 return ret->expr;
17988 /* Given a value, round it up to the lowest multiple of `boundary'
17989 which is not less than the value itself. */
17991 static inline HOST_WIDE_INT
17992 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17994 return (((value + boundary - 1) / boundary) * boundary);
17997 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17998 pointer to the declared type for the relevant field variable, or return
17999 `integer_type_node' if the given node turns out to be an
18000 ERROR_MARK node. */
18002 static inline tree
18003 field_type (const_tree decl)
18005 tree type;
18007 if (TREE_CODE (decl) == ERROR_MARK)
18008 return integer_type_node;
18010 type = DECL_BIT_FIELD_TYPE (decl);
18011 if (type == NULL_TREE)
18012 type = TREE_TYPE (decl);
18014 return type;
18017 /* Given a pointer to a tree node, return the alignment in bits for
18018 it, or else return BITS_PER_WORD if the node actually turns out to
18019 be an ERROR_MARK node. */
18021 static inline unsigned
18022 simple_type_align_in_bits (const_tree type)
18024 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18027 static inline unsigned
18028 simple_decl_align_in_bits (const_tree decl)
18030 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18033 /* Return the result of rounding T up to ALIGN. */
18035 static inline offset_int
18036 round_up_to_align (const offset_int &t, unsigned int align)
18038 return wi::udiv_trunc (t + align - 1, align) * align;
18041 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18042 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18043 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18044 if we fail to return the size in one of these two forms. */
18046 static dw_loc_descr_ref
18047 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18049 tree tree_size;
18050 struct loc_descr_context ctx;
18052 /* Return a constant integer in priority, if possible. */
18053 *cst_size = int_size_in_bytes (type);
18054 if (*cst_size != -1)
18055 return NULL;
18057 ctx.context_type = const_cast<tree> (type);
18058 ctx.base_decl = NULL_TREE;
18059 ctx.dpi = NULL;
18060 ctx.placeholder_arg = false;
18061 ctx.placeholder_seen = false;
18063 type = TYPE_MAIN_VARIANT (type);
18064 tree_size = TYPE_SIZE_UNIT (type);
18065 return ((tree_size != NULL_TREE)
18066 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18067 : NULL);
18070 /* Helper structure for RECORD_TYPE processing. */
18071 struct vlr_context
18073 /* Root RECORD_TYPE. It is needed to generate data member location
18074 descriptions in variable-length records (VLR), but also to cope with
18075 variants, which are composed of nested structures multiplexed with
18076 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18077 function processing a FIELD_DECL, it is required to be non null. */
18078 tree struct_type;
18079 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18080 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18081 this variant part as part of the root record (in storage units). For
18082 regular records, it must be NULL_TREE. */
18083 tree variant_part_offset;
18086 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18087 addressed byte of the "containing object" for the given FIELD_DECL. If
18088 possible, return a native constant through CST_OFFSET (in which case NULL is
18089 returned); otherwise return a DWARF expression that computes the offset.
18091 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18092 that offset is, either because the argument turns out to be a pointer to an
18093 ERROR_MARK node, or because the offset expression is too complex for us.
18095 CTX is required: see the comment for VLR_CONTEXT. */
18097 static dw_loc_descr_ref
18098 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18099 HOST_WIDE_INT *cst_offset)
18101 tree tree_result;
18102 dw_loc_list_ref loc_result;
18104 *cst_offset = 0;
18106 if (TREE_CODE (decl) == ERROR_MARK)
18107 return NULL;
18108 else
18109 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18111 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18112 case. */
18113 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18114 return NULL;
18116 #ifdef PCC_BITFIELD_TYPE_MATTERS
18117 /* We used to handle only constant offsets in all cases. Now, we handle
18118 properly dynamic byte offsets only when PCC bitfield type doesn't
18119 matter. */
18120 if (PCC_BITFIELD_TYPE_MATTERS
18121 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18123 offset_int object_offset_in_bits;
18124 offset_int object_offset_in_bytes;
18125 offset_int bitpos_int;
18126 tree type;
18127 tree field_size_tree;
18128 offset_int deepest_bitpos;
18129 offset_int field_size_in_bits;
18130 unsigned int type_align_in_bits;
18131 unsigned int decl_align_in_bits;
18132 offset_int type_size_in_bits;
18134 bitpos_int = wi::to_offset (bit_position (decl));
18135 type = field_type (decl);
18136 type_size_in_bits = offset_int_type_size_in_bits (type);
18137 type_align_in_bits = simple_type_align_in_bits (type);
18139 field_size_tree = DECL_SIZE (decl);
18141 /* The size could be unspecified if there was an error, or for
18142 a flexible array member. */
18143 if (!field_size_tree)
18144 field_size_tree = bitsize_zero_node;
18146 /* If the size of the field is not constant, use the type size. */
18147 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18148 field_size_in_bits = wi::to_offset (field_size_tree);
18149 else
18150 field_size_in_bits = type_size_in_bits;
18152 decl_align_in_bits = simple_decl_align_in_bits (decl);
18154 /* The GCC front-end doesn't make any attempt to keep track of the
18155 starting bit offset (relative to the start of the containing
18156 structure type) of the hypothetical "containing object" for a
18157 bit-field. Thus, when computing the byte offset value for the
18158 start of the "containing object" of a bit-field, we must deduce
18159 this information on our own. This can be rather tricky to do in
18160 some cases. For example, handling the following structure type
18161 definition when compiling for an i386/i486 target (which only
18162 aligns long long's to 32-bit boundaries) can be very tricky:
18164 struct S { int field1; long long field2:31; };
18166 Fortunately, there is a simple rule-of-thumb which can be used
18167 in such cases. When compiling for an i386/i486, GCC will
18168 allocate 8 bytes for the structure shown above. It decides to
18169 do this based upon one simple rule for bit-field allocation.
18170 GCC allocates each "containing object" for each bit-field at
18171 the first (i.e. lowest addressed) legitimate alignment boundary
18172 (based upon the required minimum alignment for the declared
18173 type of the field) which it can possibly use, subject to the
18174 condition that there is still enough available space remaining
18175 in the containing object (when allocated at the selected point)
18176 to fully accommodate all of the bits of the bit-field itself.
18178 This simple rule makes it obvious why GCC allocates 8 bytes for
18179 each object of the structure type shown above. When looking
18180 for a place to allocate the "containing object" for `field2',
18181 the compiler simply tries to allocate a 64-bit "containing
18182 object" at each successive 32-bit boundary (starting at zero)
18183 until it finds a place to allocate that 64- bit field such that
18184 at least 31 contiguous (and previously unallocated) bits remain
18185 within that selected 64 bit field. (As it turns out, for the
18186 example above, the compiler finds it is OK to allocate the
18187 "containing object" 64-bit field at bit-offset zero within the
18188 structure type.)
18190 Here we attempt to work backwards from the limited set of facts
18191 we're given, and we try to deduce from those facts, where GCC
18192 must have believed that the containing object started (within
18193 the structure type). The value we deduce is then used (by the
18194 callers of this routine) to generate DW_AT_location and
18195 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18196 the case of DW_AT_location, regular fields as well). */
18198 /* Figure out the bit-distance from the start of the structure to
18199 the "deepest" bit of the bit-field. */
18200 deepest_bitpos = bitpos_int + field_size_in_bits;
18202 /* This is the tricky part. Use some fancy footwork to deduce
18203 where the lowest addressed bit of the containing object must
18204 be. */
18205 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18207 /* Round up to type_align by default. This works best for
18208 bitfields. */
18209 object_offset_in_bits
18210 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18212 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18214 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18216 /* Round up to decl_align instead. */
18217 object_offset_in_bits
18218 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18221 object_offset_in_bytes
18222 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18223 if (ctx->variant_part_offset == NULL_TREE)
18225 *cst_offset = object_offset_in_bytes.to_shwi ();
18226 return NULL;
18228 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18230 else
18231 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18232 tree_result = byte_position (decl);
18234 if (ctx->variant_part_offset != NULL_TREE)
18235 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18236 ctx->variant_part_offset, tree_result);
18238 /* If the byte offset is a constant, it's simplier to handle a native
18239 constant rather than a DWARF expression. */
18240 if (TREE_CODE (tree_result) == INTEGER_CST)
18242 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18243 return NULL;
18245 struct loc_descr_context loc_ctx = {
18246 ctx->struct_type, /* context_type */
18247 NULL_TREE, /* base_decl */
18248 NULL, /* dpi */
18249 false, /* placeholder_arg */
18250 false /* placeholder_seen */
18252 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18254 /* We want a DWARF expression: abort if we only have a location list with
18255 multiple elements. */
18256 if (!loc_result || !single_element_loc_list_p (loc_result))
18257 return NULL;
18258 else
18259 return loc_result->expr;
18262 /* The following routines define various Dwarf attributes and any data
18263 associated with them. */
18265 /* Add a location description attribute value to a DIE.
18267 This emits location attributes suitable for whole variables and
18268 whole parameters. Note that the location attributes for struct fields are
18269 generated by the routine `data_member_location_attribute' below. */
18271 static inline void
18272 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18273 dw_loc_list_ref descr)
18275 if (descr == 0)
18276 return;
18277 if (single_element_loc_list_p (descr))
18278 add_AT_loc (die, attr_kind, descr->expr);
18279 else
18280 add_AT_loc_list (die, attr_kind, descr);
18283 /* Add DW_AT_accessibility attribute to DIE if needed. */
18285 static void
18286 add_accessibility_attribute (dw_die_ref die, tree decl)
18288 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18289 children, otherwise the default is DW_ACCESS_public. In DWARF2
18290 the default has always been DW_ACCESS_public. */
18291 if (TREE_PROTECTED (decl))
18292 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18293 else if (TREE_PRIVATE (decl))
18295 if (dwarf_version == 2
18296 || die->die_parent == NULL
18297 || die->die_parent->die_tag != DW_TAG_class_type)
18298 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18300 else if (dwarf_version > 2
18301 && die->die_parent
18302 && die->die_parent->die_tag == DW_TAG_class_type)
18303 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18306 /* Attach the specialized form of location attribute used for data members of
18307 struct and union types. In the special case of a FIELD_DECL node which
18308 represents a bit-field, the "offset" part of this special location
18309 descriptor must indicate the distance in bytes from the lowest-addressed
18310 byte of the containing struct or union type to the lowest-addressed byte of
18311 the "containing object" for the bit-field. (See the `field_byte_offset'
18312 function above).
18314 For any given bit-field, the "containing object" is a hypothetical object
18315 (of some integral or enum type) within which the given bit-field lives. The
18316 type of this hypothetical "containing object" is always the same as the
18317 declared type of the individual bit-field itself (for GCC anyway... the
18318 DWARF spec doesn't actually mandate this). Note that it is the size (in
18319 bytes) of the hypothetical "containing object" which will be given in the
18320 DW_AT_byte_size attribute for this bit-field. (See the
18321 `byte_size_attribute' function below.) It is also used when calculating the
18322 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18323 function below.)
18325 CTX is required: see the comment for VLR_CONTEXT. */
18327 static void
18328 add_data_member_location_attribute (dw_die_ref die,
18329 tree decl,
18330 struct vlr_context *ctx)
18332 HOST_WIDE_INT offset;
18333 dw_loc_descr_ref loc_descr = 0;
18335 if (TREE_CODE (decl) == TREE_BINFO)
18337 /* We're working on the TAG_inheritance for a base class. */
18338 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18340 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18341 aren't at a fixed offset from all (sub)objects of the same
18342 type. We need to extract the appropriate offset from our
18343 vtable. The following dwarf expression means
18345 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18347 This is specific to the V3 ABI, of course. */
18349 dw_loc_descr_ref tmp;
18351 /* Make a copy of the object address. */
18352 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18353 add_loc_descr (&loc_descr, tmp);
18355 /* Extract the vtable address. */
18356 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18357 add_loc_descr (&loc_descr, tmp);
18359 /* Calculate the address of the offset. */
18360 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18361 gcc_assert (offset < 0);
18363 tmp = int_loc_descriptor (-offset);
18364 add_loc_descr (&loc_descr, tmp);
18365 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18366 add_loc_descr (&loc_descr, tmp);
18368 /* Extract the offset. */
18369 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18370 add_loc_descr (&loc_descr, tmp);
18372 /* Add it to the object address. */
18373 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18374 add_loc_descr (&loc_descr, tmp);
18376 else
18377 offset = tree_to_shwi (BINFO_OFFSET (decl));
18379 else
18381 loc_descr = field_byte_offset (decl, ctx, &offset);
18383 /* If loc_descr is available then we know the field offset is dynamic.
18384 However, GDB does not handle dynamic field offsets very well at the
18385 moment. */
18386 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18388 loc_descr = NULL;
18389 offset = 0;
18392 /* Data member location evalutation starts with the base address on the
18393 stack. Compute the field offset and add it to this base address. */
18394 else if (loc_descr != NULL)
18395 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18398 if (! loc_descr)
18400 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18401 e.g. GDB only added support to it in November 2016. For DWARF5
18402 we need newer debug info consumers anyway. We might change this
18403 to dwarf_version >= 4 once most consumers catched up. */
18404 if (dwarf_version >= 5
18405 && TREE_CODE (decl) == FIELD_DECL
18406 && DECL_BIT_FIELD_TYPE (decl))
18408 tree off = bit_position (decl);
18409 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18411 remove_AT (die, DW_AT_byte_size);
18412 remove_AT (die, DW_AT_bit_offset);
18413 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18414 return;
18417 if (dwarf_version > 2)
18419 /* Don't need to output a location expression, just the constant. */
18420 if (offset < 0)
18421 add_AT_int (die, DW_AT_data_member_location, offset);
18422 else
18423 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18424 return;
18426 else
18428 enum dwarf_location_atom op;
18430 /* The DWARF2 standard says that we should assume that the structure
18431 address is already on the stack, so we can specify a structure
18432 field address by using DW_OP_plus_uconst. */
18433 op = DW_OP_plus_uconst;
18434 loc_descr = new_loc_descr (op, offset, 0);
18438 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18441 /* Writes integer values to dw_vec_const array. */
18443 static void
18444 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18446 while (size != 0)
18448 *dest++ = val & 0xff;
18449 val >>= 8;
18450 --size;
18454 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18456 static HOST_WIDE_INT
18457 extract_int (const unsigned char *src, unsigned int size)
18459 HOST_WIDE_INT val = 0;
18461 src += size;
18462 while (size != 0)
18464 val <<= 8;
18465 val |= *--src & 0xff;
18466 --size;
18468 return val;
18471 /* Writes wide_int values to dw_vec_const array. */
18473 static void
18474 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18476 int i;
18478 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18480 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18481 return;
18484 /* We'd have to extend this code to support odd sizes. */
18485 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18487 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18489 if (WORDS_BIG_ENDIAN)
18490 for (i = n - 1; i >= 0; i--)
18492 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18493 dest += sizeof (HOST_WIDE_INT);
18495 else
18496 for (i = 0; i < n; i++)
18498 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18499 dest += sizeof (HOST_WIDE_INT);
18503 /* Writes floating point values to dw_vec_const array. */
18505 static void
18506 insert_float (const_rtx rtl, unsigned char *array)
18508 long val[4];
18509 int i;
18511 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18513 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18514 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18516 insert_int (val[i], 4, array);
18517 array += 4;
18521 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18522 does not have a "location" either in memory or in a register. These
18523 things can arise in GNU C when a constant is passed as an actual parameter
18524 to an inlined function. They can also arise in C++ where declared
18525 constants do not necessarily get memory "homes". */
18527 static bool
18528 add_const_value_attribute (dw_die_ref die, rtx rtl)
18530 switch (GET_CODE (rtl))
18532 case CONST_INT:
18534 HOST_WIDE_INT val = INTVAL (rtl);
18536 if (val < 0)
18537 add_AT_int (die, DW_AT_const_value, val);
18538 else
18539 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18541 return true;
18543 case CONST_WIDE_INT:
18545 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18546 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18547 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18548 wide_int w = wi::zext (w1, prec);
18549 add_AT_wide (die, DW_AT_const_value, w);
18551 return true;
18553 case CONST_DOUBLE:
18554 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18555 floating-point constant. A CONST_DOUBLE is used whenever the
18556 constant requires more than one word in order to be adequately
18557 represented. */
18559 machine_mode mode = GET_MODE (rtl);
18561 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18562 add_AT_double (die, DW_AT_const_value,
18563 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18564 else
18566 unsigned int length = GET_MODE_SIZE (mode);
18567 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18569 insert_float (rtl, array);
18570 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18573 return true;
18575 case CONST_VECTOR:
18577 machine_mode mode = GET_MODE (rtl);
18578 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18579 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18580 unsigned char *array
18581 = ggc_vec_alloc<unsigned char> (length * elt_size);
18582 unsigned int i;
18583 unsigned char *p;
18584 machine_mode imode = GET_MODE_INNER (mode);
18586 switch (GET_MODE_CLASS (mode))
18588 case MODE_VECTOR_INT:
18589 for (i = 0, p = array; i < length; i++, p += elt_size)
18591 rtx elt = CONST_VECTOR_ELT (rtl, i);
18592 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18594 break;
18596 case MODE_VECTOR_FLOAT:
18597 for (i = 0, p = array; i < length; i++, p += elt_size)
18599 rtx elt = CONST_VECTOR_ELT (rtl, i);
18600 insert_float (elt, p);
18602 break;
18604 default:
18605 gcc_unreachable ();
18608 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18610 return true;
18612 case CONST_STRING:
18613 if (dwarf_version >= 4 || !dwarf_strict)
18615 dw_loc_descr_ref loc_result;
18616 resolve_one_addr (&rtl);
18617 rtl_addr:
18618 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18619 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18620 add_AT_loc (die, DW_AT_location, loc_result);
18621 vec_safe_push (used_rtx_array, rtl);
18622 return true;
18624 return false;
18626 case CONST:
18627 if (CONSTANT_P (XEXP (rtl, 0)))
18628 return add_const_value_attribute (die, XEXP (rtl, 0));
18629 /* FALLTHROUGH */
18630 case SYMBOL_REF:
18631 if (!const_ok_for_output (rtl))
18632 return false;
18633 /* FALLTHROUGH */
18634 case LABEL_REF:
18635 if (dwarf_version >= 4 || !dwarf_strict)
18636 goto rtl_addr;
18637 return false;
18639 case PLUS:
18640 /* In cases where an inlined instance of an inline function is passed
18641 the address of an `auto' variable (which is local to the caller) we
18642 can get a situation where the DECL_RTL of the artificial local
18643 variable (for the inlining) which acts as a stand-in for the
18644 corresponding formal parameter (of the inline function) will look
18645 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18646 exactly a compile-time constant expression, but it isn't the address
18647 of the (artificial) local variable either. Rather, it represents the
18648 *value* which the artificial local variable always has during its
18649 lifetime. We currently have no way to represent such quasi-constant
18650 values in Dwarf, so for now we just punt and generate nothing. */
18651 return false;
18653 case HIGH:
18654 case CONST_FIXED:
18655 return false;
18657 case MEM:
18658 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18659 && MEM_READONLY_P (rtl)
18660 && GET_MODE (rtl) == BLKmode)
18662 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18663 return true;
18665 return false;
18667 default:
18668 /* No other kinds of rtx should be possible here. */
18669 gcc_unreachable ();
18671 return false;
18674 /* Determine whether the evaluation of EXPR references any variables
18675 or functions which aren't otherwise used (and therefore may not be
18676 output). */
18677 static tree
18678 reference_to_unused (tree * tp, int * walk_subtrees,
18679 void * data ATTRIBUTE_UNUSED)
18681 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18682 *walk_subtrees = 0;
18684 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18685 && ! TREE_ASM_WRITTEN (*tp))
18686 return *tp;
18687 /* ??? The C++ FE emits debug information for using decls, so
18688 putting gcc_unreachable here falls over. See PR31899. For now
18689 be conservative. */
18690 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18691 return *tp;
18692 else if (VAR_P (*tp))
18694 varpool_node *node = varpool_node::get (*tp);
18695 if (!node || !node->definition)
18696 return *tp;
18698 else if (TREE_CODE (*tp) == FUNCTION_DECL
18699 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18701 /* The call graph machinery must have finished analyzing,
18702 optimizing and gimplifying the CU by now.
18703 So if *TP has no call graph node associated
18704 to it, it means *TP will not be emitted. */
18705 if (!cgraph_node::get (*tp))
18706 return *tp;
18708 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18709 return *tp;
18711 return NULL_TREE;
18714 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18715 for use in a later add_const_value_attribute call. */
18717 static rtx
18718 rtl_for_decl_init (tree init, tree type)
18720 rtx rtl = NULL_RTX;
18722 STRIP_NOPS (init);
18724 /* If a variable is initialized with a string constant without embedded
18725 zeros, build CONST_STRING. */
18726 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18728 tree enttype = TREE_TYPE (type);
18729 tree domain = TYPE_DOMAIN (type);
18730 machine_mode mode = TYPE_MODE (enttype);
18732 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18733 && domain
18734 && integer_zerop (TYPE_MIN_VALUE (domain))
18735 && compare_tree_int (TYPE_MAX_VALUE (domain),
18736 TREE_STRING_LENGTH (init) - 1) == 0
18737 && ((size_t) TREE_STRING_LENGTH (init)
18738 == strlen (TREE_STRING_POINTER (init)) + 1))
18740 rtl = gen_rtx_CONST_STRING (VOIDmode,
18741 ggc_strdup (TREE_STRING_POINTER (init)));
18742 rtl = gen_rtx_MEM (BLKmode, rtl);
18743 MEM_READONLY_P (rtl) = 1;
18746 /* Other aggregates, and complex values, could be represented using
18747 CONCAT: FIXME! */
18748 else if (AGGREGATE_TYPE_P (type)
18749 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18750 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18751 || TREE_CODE (type) == COMPLEX_TYPE)
18753 /* Vectors only work if their mode is supported by the target.
18754 FIXME: generic vectors ought to work too. */
18755 else if (TREE_CODE (type) == VECTOR_TYPE
18756 && !VECTOR_MODE_P (TYPE_MODE (type)))
18758 /* If the initializer is something that we know will expand into an
18759 immediate RTL constant, expand it now. We must be careful not to
18760 reference variables which won't be output. */
18761 else if (initializer_constant_valid_p (init, type)
18762 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18764 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18765 possible. */
18766 if (TREE_CODE (type) == VECTOR_TYPE)
18767 switch (TREE_CODE (init))
18769 case VECTOR_CST:
18770 break;
18771 case CONSTRUCTOR:
18772 if (TREE_CONSTANT (init))
18774 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18775 bool constant_p = true;
18776 tree value;
18777 unsigned HOST_WIDE_INT ix;
18779 /* Even when ctor is constant, it might contain non-*_CST
18780 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18781 belong into VECTOR_CST nodes. */
18782 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18783 if (!CONSTANT_CLASS_P (value))
18785 constant_p = false;
18786 break;
18789 if (constant_p)
18791 init = build_vector_from_ctor (type, elts);
18792 break;
18795 /* FALLTHRU */
18797 default:
18798 return NULL;
18801 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18803 /* If expand_expr returns a MEM, it wasn't immediate. */
18804 gcc_assert (!rtl || !MEM_P (rtl));
18807 return rtl;
18810 /* Generate RTL for the variable DECL to represent its location. */
18812 static rtx
18813 rtl_for_decl_location (tree decl)
18815 rtx rtl;
18817 /* Here we have to decide where we are going to say the parameter "lives"
18818 (as far as the debugger is concerned). We only have a couple of
18819 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18821 DECL_RTL normally indicates where the parameter lives during most of the
18822 activation of the function. If optimization is enabled however, this
18823 could be either NULL or else a pseudo-reg. Both of those cases indicate
18824 that the parameter doesn't really live anywhere (as far as the code
18825 generation parts of GCC are concerned) during most of the function's
18826 activation. That will happen (for example) if the parameter is never
18827 referenced within the function.
18829 We could just generate a location descriptor here for all non-NULL
18830 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18831 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18832 where DECL_RTL is NULL or is a pseudo-reg.
18834 Note however that we can only get away with using DECL_INCOMING_RTL as
18835 a backup substitute for DECL_RTL in certain limited cases. In cases
18836 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18837 we can be sure that the parameter was passed using the same type as it is
18838 declared to have within the function, and that its DECL_INCOMING_RTL
18839 points us to a place where a value of that type is passed.
18841 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18842 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18843 because in these cases DECL_INCOMING_RTL points us to a value of some
18844 type which is *different* from the type of the parameter itself. Thus,
18845 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18846 such cases, the debugger would end up (for example) trying to fetch a
18847 `float' from a place which actually contains the first part of a
18848 `double'. That would lead to really incorrect and confusing
18849 output at debug-time.
18851 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18852 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18853 are a couple of exceptions however. On little-endian machines we can
18854 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18855 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18856 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18857 when (on a little-endian machine) a non-prototyped function has a
18858 parameter declared to be of type `short' or `char'. In such cases,
18859 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18860 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18861 passed `int' value. If the debugger then uses that address to fetch
18862 a `short' or a `char' (on a little-endian machine) the result will be
18863 the correct data, so we allow for such exceptional cases below.
18865 Note that our goal here is to describe the place where the given formal
18866 parameter lives during most of the function's activation (i.e. between the
18867 end of the prologue and the start of the epilogue). We'll do that as best
18868 as we can. Note however that if the given formal parameter is modified
18869 sometime during the execution of the function, then a stack backtrace (at
18870 debug-time) will show the function as having been called with the *new*
18871 value rather than the value which was originally passed in. This happens
18872 rarely enough that it is not a major problem, but it *is* a problem, and
18873 I'd like to fix it.
18875 A future version of dwarf2out.c may generate two additional attributes for
18876 any given DW_TAG_formal_parameter DIE which will describe the "passed
18877 type" and the "passed location" for the given formal parameter in addition
18878 to the attributes we now generate to indicate the "declared type" and the
18879 "active location" for each parameter. This additional set of attributes
18880 could be used by debuggers for stack backtraces. Separately, note that
18881 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18882 This happens (for example) for inlined-instances of inline function formal
18883 parameters which are never referenced. This really shouldn't be
18884 happening. All PARM_DECL nodes should get valid non-NULL
18885 DECL_INCOMING_RTL values. FIXME. */
18887 /* Use DECL_RTL as the "location" unless we find something better. */
18888 rtl = DECL_RTL_IF_SET (decl);
18890 /* When generating abstract instances, ignore everything except
18891 constants, symbols living in memory, and symbols living in
18892 fixed registers. */
18893 if (! reload_completed)
18895 if (rtl
18896 && (CONSTANT_P (rtl)
18897 || (MEM_P (rtl)
18898 && CONSTANT_P (XEXP (rtl, 0)))
18899 || (REG_P (rtl)
18900 && VAR_P (decl)
18901 && TREE_STATIC (decl))))
18903 rtl = targetm.delegitimize_address (rtl);
18904 return rtl;
18906 rtl = NULL_RTX;
18908 else if (TREE_CODE (decl) == PARM_DECL)
18910 if (rtl == NULL_RTX
18911 || is_pseudo_reg (rtl)
18912 || (MEM_P (rtl)
18913 && is_pseudo_reg (XEXP (rtl, 0))
18914 && DECL_INCOMING_RTL (decl)
18915 && MEM_P (DECL_INCOMING_RTL (decl))
18916 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18918 tree declared_type = TREE_TYPE (decl);
18919 tree passed_type = DECL_ARG_TYPE (decl);
18920 machine_mode dmode = TYPE_MODE (declared_type);
18921 machine_mode pmode = TYPE_MODE (passed_type);
18923 /* This decl represents a formal parameter which was optimized out.
18924 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18925 all cases where (rtl == NULL_RTX) just below. */
18926 if (dmode == pmode)
18927 rtl = DECL_INCOMING_RTL (decl);
18928 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18929 && SCALAR_INT_MODE_P (dmode)
18930 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18931 && DECL_INCOMING_RTL (decl))
18933 rtx inc = DECL_INCOMING_RTL (decl);
18934 if (REG_P (inc))
18935 rtl = inc;
18936 else if (MEM_P (inc))
18938 if (BYTES_BIG_ENDIAN)
18939 rtl = adjust_address_nv (inc, dmode,
18940 GET_MODE_SIZE (pmode)
18941 - GET_MODE_SIZE (dmode));
18942 else
18943 rtl = inc;
18948 /* If the parm was passed in registers, but lives on the stack, then
18949 make a big endian correction if the mode of the type of the
18950 parameter is not the same as the mode of the rtl. */
18951 /* ??? This is the same series of checks that are made in dbxout.c before
18952 we reach the big endian correction code there. It isn't clear if all
18953 of these checks are necessary here, but keeping them all is the safe
18954 thing to do. */
18955 else if (MEM_P (rtl)
18956 && XEXP (rtl, 0) != const0_rtx
18957 && ! CONSTANT_P (XEXP (rtl, 0))
18958 /* Not passed in memory. */
18959 && !MEM_P (DECL_INCOMING_RTL (decl))
18960 /* Not passed by invisible reference. */
18961 && (!REG_P (XEXP (rtl, 0))
18962 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18963 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18964 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
18965 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18966 #endif
18968 /* Big endian correction check. */
18969 && BYTES_BIG_ENDIAN
18970 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18971 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18972 < UNITS_PER_WORD))
18974 machine_mode addr_mode = get_address_mode (rtl);
18975 int offset = (UNITS_PER_WORD
18976 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
18978 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
18979 plus_constant (addr_mode, XEXP (rtl, 0), offset));
18982 else if (VAR_P (decl)
18983 && rtl
18984 && MEM_P (rtl)
18985 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
18986 && BYTES_BIG_ENDIAN)
18988 machine_mode addr_mode = get_address_mode (rtl);
18989 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
18990 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
18992 /* If a variable is declared "register" yet is smaller than
18993 a register, then if we store the variable to memory, it
18994 looks like we're storing a register-sized value, when in
18995 fact we are not. We need to adjust the offset of the
18996 storage location to reflect the actual value's bytes,
18997 else gdb will not be able to display it. */
18998 if (rsize > dsize)
18999 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19000 plus_constant (addr_mode, XEXP (rtl, 0),
19001 rsize - dsize));
19004 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19005 and will have been substituted directly into all expressions that use it.
19006 C does not have such a concept, but C++ and other languages do. */
19007 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19008 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19010 if (rtl)
19011 rtl = targetm.delegitimize_address (rtl);
19013 /* If we don't look past the constant pool, we risk emitting a
19014 reference to a constant pool entry that isn't referenced from
19015 code, and thus is not emitted. */
19016 if (rtl)
19017 rtl = avoid_constant_pool_reference (rtl);
19019 /* Try harder to get a rtl. If this symbol ends up not being emitted
19020 in the current CU, resolve_addr will remove the expression referencing
19021 it. */
19022 if (rtl == NULL_RTX
19023 && VAR_P (decl)
19024 && !DECL_EXTERNAL (decl)
19025 && TREE_STATIC (decl)
19026 && DECL_NAME (decl)
19027 && !DECL_HARD_REGISTER (decl)
19028 && DECL_MODE (decl) != VOIDmode)
19030 rtl = make_decl_rtl_for_debug (decl);
19031 if (!MEM_P (rtl)
19032 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19033 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19034 rtl = NULL_RTX;
19037 return rtl;
19040 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19041 returned. If so, the decl for the COMMON block is returned, and the
19042 value is the offset into the common block for the symbol. */
19044 static tree
19045 fortran_common (tree decl, HOST_WIDE_INT *value)
19047 tree val_expr, cvar;
19048 machine_mode mode;
19049 HOST_WIDE_INT bitsize, bitpos;
19050 tree offset;
19051 int unsignedp, reversep, volatilep = 0;
19053 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19054 it does not have a value (the offset into the common area), or if it
19055 is thread local (as opposed to global) then it isn't common, and shouldn't
19056 be handled as such. */
19057 if (!VAR_P (decl)
19058 || !TREE_STATIC (decl)
19059 || !DECL_HAS_VALUE_EXPR_P (decl)
19060 || !is_fortran ())
19061 return NULL_TREE;
19063 val_expr = DECL_VALUE_EXPR (decl);
19064 if (TREE_CODE (val_expr) != COMPONENT_REF)
19065 return NULL_TREE;
19067 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19068 &unsignedp, &reversep, &volatilep);
19070 if (cvar == NULL_TREE
19071 || !VAR_P (cvar)
19072 || DECL_ARTIFICIAL (cvar)
19073 || !TREE_PUBLIC (cvar))
19074 return NULL_TREE;
19076 *value = 0;
19077 if (offset != NULL)
19079 if (!tree_fits_shwi_p (offset))
19080 return NULL_TREE;
19081 *value = tree_to_shwi (offset);
19083 if (bitpos != 0)
19084 *value += bitpos / BITS_PER_UNIT;
19086 return cvar;
19089 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19090 data attribute for a variable or a parameter. We generate the
19091 DW_AT_const_value attribute only in those cases where the given variable
19092 or parameter does not have a true "location" either in memory or in a
19093 register. This can happen (for example) when a constant is passed as an
19094 actual argument in a call to an inline function. (It's possible that
19095 these things can crop up in other ways also.) Note that one type of
19096 constant value which can be passed into an inlined function is a constant
19097 pointer. This can happen for example if an actual argument in an inlined
19098 function call evaluates to a compile-time constant address.
19100 CACHE_P is true if it is worth caching the location list for DECL,
19101 so that future calls can reuse it rather than regenerate it from scratch.
19102 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19103 since we will need to refer to them each time the function is inlined. */
19105 static bool
19106 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19108 rtx rtl;
19109 dw_loc_list_ref list;
19110 var_loc_list *loc_list;
19111 cached_dw_loc_list *cache;
19113 if (early_dwarf)
19114 return false;
19116 if (TREE_CODE (decl) == ERROR_MARK)
19117 return false;
19119 if (get_AT (die, DW_AT_location)
19120 || get_AT (die, DW_AT_const_value))
19121 return true;
19123 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19124 || TREE_CODE (decl) == RESULT_DECL);
19126 /* Try to get some constant RTL for this decl, and use that as the value of
19127 the location. */
19129 rtl = rtl_for_decl_location (decl);
19130 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19131 && add_const_value_attribute (die, rtl))
19132 return true;
19134 /* See if we have single element location list that is equivalent to
19135 a constant value. That way we are better to use add_const_value_attribute
19136 rather than expanding constant value equivalent. */
19137 loc_list = lookup_decl_loc (decl);
19138 if (loc_list
19139 && loc_list->first
19140 && loc_list->first->next == NULL
19141 && NOTE_P (loc_list->first->loc)
19142 && NOTE_VAR_LOCATION (loc_list->first->loc)
19143 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19145 struct var_loc_node *node;
19147 node = loc_list->first;
19148 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19149 if (GET_CODE (rtl) == EXPR_LIST)
19150 rtl = XEXP (rtl, 0);
19151 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19152 && add_const_value_attribute (die, rtl))
19153 return true;
19155 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19156 list several times. See if we've already cached the contents. */
19157 list = NULL;
19158 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19159 cache_p = false;
19160 if (cache_p)
19162 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19163 if (cache)
19164 list = cache->loc_list;
19166 if (list == NULL)
19168 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19169 NULL);
19170 /* It is usually worth caching this result if the decl is from
19171 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19172 if (cache_p && list && list->dw_loc_next)
19174 cached_dw_loc_list **slot
19175 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19176 DECL_UID (decl),
19177 INSERT);
19178 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19179 cache->decl_id = DECL_UID (decl);
19180 cache->loc_list = list;
19181 *slot = cache;
19184 if (list)
19186 add_AT_location_description (die, DW_AT_location, list);
19187 return true;
19189 /* None of that worked, so it must not really have a location;
19190 try adding a constant value attribute from the DECL_INITIAL. */
19191 return tree_add_const_value_attribute_for_decl (die, decl);
19194 /* Helper function for tree_add_const_value_attribute. Natively encode
19195 initializer INIT into an array. Return true if successful. */
19197 static bool
19198 native_encode_initializer (tree init, unsigned char *array, int size)
19200 tree type;
19202 if (init == NULL_TREE)
19203 return false;
19205 STRIP_NOPS (init);
19206 switch (TREE_CODE (init))
19208 case STRING_CST:
19209 type = TREE_TYPE (init);
19210 if (TREE_CODE (type) == ARRAY_TYPE)
19212 tree enttype = TREE_TYPE (type);
19213 machine_mode mode = TYPE_MODE (enttype);
19215 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19216 return false;
19217 if (int_size_in_bytes (type) != size)
19218 return false;
19219 if (size > TREE_STRING_LENGTH (init))
19221 memcpy (array, TREE_STRING_POINTER (init),
19222 TREE_STRING_LENGTH (init));
19223 memset (array + TREE_STRING_LENGTH (init),
19224 '\0', size - TREE_STRING_LENGTH (init));
19226 else
19227 memcpy (array, TREE_STRING_POINTER (init), size);
19228 return true;
19230 return false;
19231 case CONSTRUCTOR:
19232 type = TREE_TYPE (init);
19233 if (int_size_in_bytes (type) != size)
19234 return false;
19235 if (TREE_CODE (type) == ARRAY_TYPE)
19237 HOST_WIDE_INT min_index;
19238 unsigned HOST_WIDE_INT cnt;
19239 int curpos = 0, fieldsize;
19240 constructor_elt *ce;
19242 if (TYPE_DOMAIN (type) == NULL_TREE
19243 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19244 return false;
19246 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19247 if (fieldsize <= 0)
19248 return false;
19250 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19251 memset (array, '\0', size);
19252 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19254 tree val = ce->value;
19255 tree index = ce->index;
19256 int pos = curpos;
19257 if (index && TREE_CODE (index) == RANGE_EXPR)
19258 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19259 * fieldsize;
19260 else if (index)
19261 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19263 if (val)
19265 STRIP_NOPS (val);
19266 if (!native_encode_initializer (val, array + pos, fieldsize))
19267 return false;
19269 curpos = pos + fieldsize;
19270 if (index && TREE_CODE (index) == RANGE_EXPR)
19272 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19273 - tree_to_shwi (TREE_OPERAND (index, 0));
19274 while (count-- > 0)
19276 if (val)
19277 memcpy (array + curpos, array + pos, fieldsize);
19278 curpos += fieldsize;
19281 gcc_assert (curpos <= size);
19283 return true;
19285 else if (TREE_CODE (type) == RECORD_TYPE
19286 || TREE_CODE (type) == UNION_TYPE)
19288 tree field = NULL_TREE;
19289 unsigned HOST_WIDE_INT cnt;
19290 constructor_elt *ce;
19292 if (int_size_in_bytes (type) != size)
19293 return false;
19295 if (TREE_CODE (type) == RECORD_TYPE)
19296 field = TYPE_FIELDS (type);
19298 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19300 tree val = ce->value;
19301 int pos, fieldsize;
19303 if (ce->index != 0)
19304 field = ce->index;
19306 if (val)
19307 STRIP_NOPS (val);
19309 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19310 return false;
19312 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19313 && TYPE_DOMAIN (TREE_TYPE (field))
19314 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19315 return false;
19316 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19317 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19318 return false;
19319 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19320 pos = int_byte_position (field);
19321 gcc_assert (pos + fieldsize <= size);
19322 if (val && fieldsize != 0
19323 && !native_encode_initializer (val, array + pos, fieldsize))
19324 return false;
19326 return true;
19328 return false;
19329 case VIEW_CONVERT_EXPR:
19330 case NON_LVALUE_EXPR:
19331 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19332 default:
19333 return native_encode_expr (init, array, size) == size;
19337 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19338 attribute is the const value T. */
19340 static bool
19341 tree_add_const_value_attribute (dw_die_ref die, tree t)
19343 tree init;
19344 tree type = TREE_TYPE (t);
19345 rtx rtl;
19347 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19348 return false;
19350 init = t;
19351 gcc_assert (!DECL_P (init));
19353 if (! early_dwarf)
19355 rtl = rtl_for_decl_init (init, type);
19356 if (rtl)
19357 return add_const_value_attribute (die, rtl);
19359 /* If the host and target are sane, try harder. */
19360 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19361 && initializer_constant_valid_p (init, type))
19363 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19364 if (size > 0 && (int) size == size)
19366 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19368 if (native_encode_initializer (init, array, size))
19370 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19371 return true;
19373 ggc_free (array);
19376 return false;
19379 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19380 attribute is the const value of T, where T is an integral constant
19381 variable with static storage duration
19382 (so it can't be a PARM_DECL or a RESULT_DECL). */
19384 static bool
19385 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19388 if (!decl
19389 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19390 || (VAR_P (decl) && !TREE_STATIC (decl)))
19391 return false;
19393 if (TREE_READONLY (decl)
19394 && ! TREE_THIS_VOLATILE (decl)
19395 && DECL_INITIAL (decl))
19396 /* OK */;
19397 else
19398 return false;
19400 /* Don't add DW_AT_const_value if abstract origin already has one. */
19401 if (get_AT (var_die, DW_AT_const_value))
19402 return false;
19404 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19407 /* Convert the CFI instructions for the current function into a
19408 location list. This is used for DW_AT_frame_base when we targeting
19409 a dwarf2 consumer that does not support the dwarf3
19410 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19411 expressions. */
19413 static dw_loc_list_ref
19414 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19416 int ix;
19417 dw_fde_ref fde;
19418 dw_loc_list_ref list, *list_tail;
19419 dw_cfi_ref cfi;
19420 dw_cfa_location last_cfa, next_cfa;
19421 const char *start_label, *last_label, *section;
19422 dw_cfa_location remember;
19424 fde = cfun->fde;
19425 gcc_assert (fde != NULL);
19427 section = secname_for_decl (current_function_decl);
19428 list_tail = &list;
19429 list = NULL;
19431 memset (&next_cfa, 0, sizeof (next_cfa));
19432 next_cfa.reg = INVALID_REGNUM;
19433 remember = next_cfa;
19435 start_label = fde->dw_fde_begin;
19437 /* ??? Bald assumption that the CIE opcode list does not contain
19438 advance opcodes. */
19439 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19440 lookup_cfa_1 (cfi, &next_cfa, &remember);
19442 last_cfa = next_cfa;
19443 last_label = start_label;
19445 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19447 /* If the first partition contained no CFI adjustments, the
19448 CIE opcodes apply to the whole first partition. */
19449 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19450 fde->dw_fde_begin, fde->dw_fde_end, section);
19451 list_tail =&(*list_tail)->dw_loc_next;
19452 start_label = last_label = fde->dw_fde_second_begin;
19455 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19457 switch (cfi->dw_cfi_opc)
19459 case DW_CFA_set_loc:
19460 case DW_CFA_advance_loc1:
19461 case DW_CFA_advance_loc2:
19462 case DW_CFA_advance_loc4:
19463 if (!cfa_equal_p (&last_cfa, &next_cfa))
19465 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19466 start_label, last_label, section);
19468 list_tail = &(*list_tail)->dw_loc_next;
19469 last_cfa = next_cfa;
19470 start_label = last_label;
19472 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19473 break;
19475 case DW_CFA_advance_loc:
19476 /* The encoding is complex enough that we should never emit this. */
19477 gcc_unreachable ();
19479 default:
19480 lookup_cfa_1 (cfi, &next_cfa, &remember);
19481 break;
19483 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19485 if (!cfa_equal_p (&last_cfa, &next_cfa))
19487 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19488 start_label, last_label, section);
19490 list_tail = &(*list_tail)->dw_loc_next;
19491 last_cfa = next_cfa;
19492 start_label = last_label;
19494 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19495 start_label, fde->dw_fde_end, section);
19496 list_tail = &(*list_tail)->dw_loc_next;
19497 start_label = last_label = fde->dw_fde_second_begin;
19501 if (!cfa_equal_p (&last_cfa, &next_cfa))
19503 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19504 start_label, last_label, section);
19505 list_tail = &(*list_tail)->dw_loc_next;
19506 start_label = last_label;
19509 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19510 start_label,
19511 fde->dw_fde_second_begin
19512 ? fde->dw_fde_second_end : fde->dw_fde_end,
19513 section);
19515 if (list && list->dw_loc_next)
19516 gen_llsym (list);
19518 return list;
19521 /* Compute a displacement from the "steady-state frame pointer" to the
19522 frame base (often the same as the CFA), and store it in
19523 frame_pointer_fb_offset. OFFSET is added to the displacement
19524 before the latter is negated. */
19526 static void
19527 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19529 rtx reg, elim;
19531 #ifdef FRAME_POINTER_CFA_OFFSET
19532 reg = frame_pointer_rtx;
19533 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19534 #else
19535 reg = arg_pointer_rtx;
19536 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19537 #endif
19539 elim = (ira_use_lra_p
19540 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19541 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19542 if (GET_CODE (elim) == PLUS)
19544 offset += INTVAL (XEXP (elim, 1));
19545 elim = XEXP (elim, 0);
19548 frame_pointer_fb_offset = -offset;
19550 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19551 in which to eliminate. This is because it's stack pointer isn't
19552 directly accessible as a register within the ISA. To work around
19553 this, assume that while we cannot provide a proper value for
19554 frame_pointer_fb_offset, we won't need one either. */
19555 frame_pointer_fb_offset_valid
19556 = ((SUPPORTS_STACK_ALIGNMENT
19557 && (elim == hard_frame_pointer_rtx
19558 || elim == stack_pointer_rtx))
19559 || elim == (frame_pointer_needed
19560 ? hard_frame_pointer_rtx
19561 : stack_pointer_rtx));
19564 /* Generate a DW_AT_name attribute given some string value to be included as
19565 the value of the attribute. */
19567 static void
19568 add_name_attribute (dw_die_ref die, const char *name_string)
19570 if (name_string != NULL && *name_string != 0)
19572 if (demangle_name_func)
19573 name_string = (*demangle_name_func) (name_string);
19575 add_AT_string (die, DW_AT_name, name_string);
19579 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19580 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19581 of TYPE accordingly.
19583 ??? This is a temporary measure until after we're able to generate
19584 regular DWARF for the complex Ada type system. */
19586 static void
19587 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19588 dw_die_ref context_die)
19590 tree dtype;
19591 dw_die_ref dtype_die;
19593 if (!lang_hooks.types.descriptive_type)
19594 return;
19596 dtype = lang_hooks.types.descriptive_type (type);
19597 if (!dtype)
19598 return;
19600 dtype_die = lookup_type_die (dtype);
19601 if (!dtype_die)
19603 gen_type_die (dtype, context_die);
19604 dtype_die = lookup_type_die (dtype);
19605 gcc_assert (dtype_die);
19608 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19611 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19613 static const char *
19614 comp_dir_string (void)
19616 const char *wd;
19617 char *wd1;
19618 static const char *cached_wd = NULL;
19620 if (cached_wd != NULL)
19621 return cached_wd;
19623 wd = get_src_pwd ();
19624 if (wd == NULL)
19625 return NULL;
19627 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19629 int wdlen;
19631 wdlen = strlen (wd);
19632 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19633 strcpy (wd1, wd);
19634 wd1 [wdlen] = DIR_SEPARATOR;
19635 wd1 [wdlen + 1] = 0;
19636 wd = wd1;
19639 cached_wd = remap_debug_filename (wd);
19640 return cached_wd;
19643 /* Generate a DW_AT_comp_dir attribute for DIE. */
19645 static void
19646 add_comp_dir_attribute (dw_die_ref die)
19648 const char * wd = comp_dir_string ();
19649 if (wd != NULL)
19650 add_AT_string (die, DW_AT_comp_dir, wd);
19653 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19654 pointer computation, ...), output a representation for that bound according
19655 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19656 loc_list_from_tree for the meaning of CONTEXT. */
19658 static void
19659 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19660 int forms, struct loc_descr_context *context)
19662 dw_die_ref context_die, decl_die;
19663 dw_loc_list_ref list;
19664 bool strip_conversions = true;
19665 bool placeholder_seen = false;
19667 while (strip_conversions)
19668 switch (TREE_CODE (value))
19670 case ERROR_MARK:
19671 case SAVE_EXPR:
19672 return;
19674 CASE_CONVERT:
19675 case VIEW_CONVERT_EXPR:
19676 value = TREE_OPERAND (value, 0);
19677 break;
19679 default:
19680 strip_conversions = false;
19681 break;
19684 /* If possible and permitted, output the attribute as a constant. */
19685 if ((forms & dw_scalar_form_constant) != 0
19686 && TREE_CODE (value) == INTEGER_CST)
19688 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19690 /* If HOST_WIDE_INT is big enough then represent the bound as
19691 a constant value. We need to choose a form based on
19692 whether the type is signed or unsigned. We cannot just
19693 call add_AT_unsigned if the value itself is positive
19694 (add_AT_unsigned might add the unsigned value encoded as
19695 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19696 bounds type and then sign extend any unsigned values found
19697 for signed types. This is needed only for
19698 DW_AT_{lower,upper}_bound, since for most other attributes,
19699 consumers will treat DW_FORM_data[1248] as unsigned values,
19700 regardless of the underlying type. */
19701 if (prec <= HOST_BITS_PER_WIDE_INT
19702 || tree_fits_uhwi_p (value))
19704 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19705 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19706 else
19707 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19709 else
19710 /* Otherwise represent the bound as an unsigned value with
19711 the precision of its type. The precision and signedness
19712 of the type will be necessary to re-interpret it
19713 unambiguously. */
19714 add_AT_wide (die, attr, value);
19715 return;
19718 /* Otherwise, if it's possible and permitted too, output a reference to
19719 another DIE. */
19720 if ((forms & dw_scalar_form_reference) != 0)
19722 tree decl = NULL_TREE;
19724 /* Some type attributes reference an outer type. For instance, the upper
19725 bound of an array may reference an embedding record (this happens in
19726 Ada). */
19727 if (TREE_CODE (value) == COMPONENT_REF
19728 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19729 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19730 decl = TREE_OPERAND (value, 1);
19732 else if (VAR_P (value)
19733 || TREE_CODE (value) == PARM_DECL
19734 || TREE_CODE (value) == RESULT_DECL)
19735 decl = value;
19737 if (decl != NULL_TREE)
19739 dw_die_ref decl_die = lookup_decl_die (decl);
19741 /* ??? Can this happen, or should the variable have been bound
19742 first? Probably it can, since I imagine that we try to create
19743 the types of parameters in the order in which they exist in
19744 the list, and won't have created a forward reference to a
19745 later parameter. */
19746 if (decl_die != NULL)
19748 add_AT_die_ref (die, attr, decl_die);
19749 return;
19754 /* Last chance: try to create a stack operation procedure to evaluate the
19755 value. Do nothing if even that is not possible or permitted. */
19756 if ((forms & dw_scalar_form_exprloc) == 0)
19757 return;
19759 list = loc_list_from_tree (value, 2, context);
19760 if (context && context->placeholder_arg)
19762 placeholder_seen = context->placeholder_seen;
19763 context->placeholder_seen = false;
19765 if (list == NULL || single_element_loc_list_p (list))
19767 /* If this attribute is not a reference nor constant, it is
19768 a DWARF expression rather than location description. For that
19769 loc_list_from_tree (value, 0, &context) is needed. */
19770 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19771 if (list2 && single_element_loc_list_p (list2))
19773 if (placeholder_seen)
19775 struct dwarf_procedure_info dpi;
19776 dpi.fndecl = NULL_TREE;
19777 dpi.args_count = 1;
19778 if (!resolve_args_picking (list2->expr, 1, &dpi))
19779 return;
19781 add_AT_loc (die, attr, list2->expr);
19782 return;
19786 /* If that failed to give a single element location list, fall back to
19787 outputting this as a reference... still if permitted. */
19788 if (list == NULL
19789 || (forms & dw_scalar_form_reference) == 0
19790 || placeholder_seen)
19791 return;
19793 if (current_function_decl == 0)
19794 context_die = comp_unit_die ();
19795 else
19796 context_die = lookup_decl_die (current_function_decl);
19798 decl_die = new_die (DW_TAG_variable, context_die, value);
19799 add_AT_flag (decl_die, DW_AT_artificial, 1);
19800 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19801 context_die);
19802 add_AT_location_description (decl_die, DW_AT_location, list);
19803 add_AT_die_ref (die, attr, decl_die);
19806 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19807 default. */
19809 static int
19810 lower_bound_default (void)
19812 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19814 case DW_LANG_C:
19815 case DW_LANG_C89:
19816 case DW_LANG_C99:
19817 case DW_LANG_C11:
19818 case DW_LANG_C_plus_plus:
19819 case DW_LANG_C_plus_plus_11:
19820 case DW_LANG_C_plus_plus_14:
19821 case DW_LANG_ObjC:
19822 case DW_LANG_ObjC_plus_plus:
19823 return 0;
19824 case DW_LANG_Fortran77:
19825 case DW_LANG_Fortran90:
19826 case DW_LANG_Fortran95:
19827 case DW_LANG_Fortran03:
19828 case DW_LANG_Fortran08:
19829 return 1;
19830 case DW_LANG_UPC:
19831 case DW_LANG_D:
19832 case DW_LANG_Python:
19833 return dwarf_version >= 4 ? 0 : -1;
19834 case DW_LANG_Ada95:
19835 case DW_LANG_Ada83:
19836 case DW_LANG_Cobol74:
19837 case DW_LANG_Cobol85:
19838 case DW_LANG_Modula2:
19839 case DW_LANG_PLI:
19840 return dwarf_version >= 4 ? 1 : -1;
19841 default:
19842 return -1;
19846 /* Given a tree node describing an array bound (either lower or upper) output
19847 a representation for that bound. */
19849 static void
19850 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19851 tree bound, struct loc_descr_context *context)
19853 int dflt;
19855 while (1)
19856 switch (TREE_CODE (bound))
19858 /* Strip all conversions. */
19859 CASE_CONVERT:
19860 case VIEW_CONVERT_EXPR:
19861 bound = TREE_OPERAND (bound, 0);
19862 break;
19864 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19865 are even omitted when they are the default. */
19866 case INTEGER_CST:
19867 /* If the value for this bound is the default one, we can even omit the
19868 attribute. */
19869 if (bound_attr == DW_AT_lower_bound
19870 && tree_fits_shwi_p (bound)
19871 && (dflt = lower_bound_default ()) != -1
19872 && tree_to_shwi (bound) == dflt)
19873 return;
19875 /* FALLTHRU */
19877 default:
19878 /* Because of the complex interaction there can be with other GNAT
19879 encodings, GDB isn't ready yet to handle proper DWARF description
19880 for self-referencial subrange bounds: let GNAT encodings do the
19881 magic in such a case. */
19882 if (is_ada ()
19883 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19884 && contains_placeholder_p (bound))
19885 return;
19887 add_scalar_info (subrange_die, bound_attr, bound,
19888 dw_scalar_form_constant
19889 | dw_scalar_form_exprloc
19890 | dw_scalar_form_reference,
19891 context);
19892 return;
19896 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19897 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19898 Note that the block of subscript information for an array type also
19899 includes information about the element type of the given array type.
19901 This function reuses previously set type and bound information if
19902 available. */
19904 static void
19905 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19907 unsigned dimension_number;
19908 tree lower, upper;
19909 dw_die_ref child = type_die->die_child;
19911 for (dimension_number = 0;
19912 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19913 type = TREE_TYPE (type), dimension_number++)
19915 tree domain = TYPE_DOMAIN (type);
19917 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19918 break;
19920 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19921 and (in GNU C only) variable bounds. Handle all three forms
19922 here. */
19924 /* Find and reuse a previously generated DW_TAG_subrange_type if
19925 available.
19927 For multi-dimensional arrays, as we iterate through the
19928 various dimensions in the enclosing for loop above, we also
19929 iterate through the DIE children and pick at each
19930 DW_TAG_subrange_type previously generated (if available).
19931 Each child DW_TAG_subrange_type DIE describes the range of
19932 the current dimension. At this point we should have as many
19933 DW_TAG_subrange_type's as we have dimensions in the
19934 array. */
19935 dw_die_ref subrange_die = NULL;
19936 if (child)
19937 while (1)
19939 child = child->die_sib;
19940 if (child->die_tag == DW_TAG_subrange_type)
19941 subrange_die = child;
19942 if (child == type_die->die_child)
19944 /* If we wrapped around, stop looking next time. */
19945 child = NULL;
19946 break;
19948 if (child->die_tag == DW_TAG_subrange_type)
19949 break;
19951 if (!subrange_die)
19952 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19954 if (domain)
19956 /* We have an array type with specified bounds. */
19957 lower = TYPE_MIN_VALUE (domain);
19958 upper = TYPE_MAX_VALUE (domain);
19960 /* Define the index type. */
19961 if (TREE_TYPE (domain)
19962 && !get_AT (subrange_die, DW_AT_type))
19964 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19965 TREE_TYPE field. We can't emit debug info for this
19966 because it is an unnamed integral type. */
19967 if (TREE_CODE (domain) == INTEGER_TYPE
19968 && TYPE_NAME (domain) == NULL_TREE
19969 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19970 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
19972 else
19973 add_type_attribute (subrange_die, TREE_TYPE (domain),
19974 TYPE_UNQUALIFIED, false, type_die);
19977 /* ??? If upper is NULL, the array has unspecified length,
19978 but it does have a lower bound. This happens with Fortran
19979 dimension arr(N:*)
19980 Since the debugger is definitely going to need to know N
19981 to produce useful results, go ahead and output the lower
19982 bound solo, and hope the debugger can cope. */
19984 if (!get_AT (subrange_die, DW_AT_lower_bound))
19985 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
19986 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
19987 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
19990 /* Otherwise we have an array type with an unspecified length. The
19991 DWARF-2 spec does not say how to handle this; let's just leave out the
19992 bounds. */
19996 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
19998 static void
19999 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20001 dw_die_ref decl_die;
20002 HOST_WIDE_INT size;
20003 dw_loc_descr_ref size_expr = NULL;
20005 switch (TREE_CODE (tree_node))
20007 case ERROR_MARK:
20008 size = 0;
20009 break;
20010 case ENUMERAL_TYPE:
20011 case RECORD_TYPE:
20012 case UNION_TYPE:
20013 case QUAL_UNION_TYPE:
20014 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20015 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20017 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20018 return;
20020 size_expr = type_byte_size (tree_node, &size);
20021 break;
20022 case FIELD_DECL:
20023 /* For a data member of a struct or union, the DW_AT_byte_size is
20024 generally given as the number of bytes normally allocated for an
20025 object of the *declared* type of the member itself. This is true
20026 even for bit-fields. */
20027 size = int_size_in_bytes (field_type (tree_node));
20028 break;
20029 default:
20030 gcc_unreachable ();
20033 /* Support for dynamically-sized objects was introduced by DWARFv3.
20034 At the moment, GDB does not handle variable byte sizes very well,
20035 though. */
20036 if ((dwarf_version >= 3 || !dwarf_strict)
20037 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20038 && size_expr != NULL)
20039 add_AT_loc (die, DW_AT_byte_size, size_expr);
20041 /* Note that `size' might be -1 when we get to this point. If it is, that
20042 indicates that the byte size of the entity in question is variable and
20043 that we could not generate a DWARF expression that computes it. */
20044 if (size >= 0)
20045 add_AT_unsigned (die, DW_AT_byte_size, size);
20048 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20049 alignment. */
20051 static void
20052 add_alignment_attribute (dw_die_ref die, tree tree_node)
20054 if (dwarf_version < 5 && dwarf_strict)
20055 return;
20057 unsigned align;
20059 if (DECL_P (tree_node))
20061 if (!DECL_USER_ALIGN (tree_node))
20062 return;
20064 align = DECL_ALIGN_UNIT (tree_node);
20066 else if (TYPE_P (tree_node))
20068 if (!TYPE_USER_ALIGN (tree_node))
20069 return;
20071 align = TYPE_ALIGN_UNIT (tree_node);
20073 else
20074 gcc_unreachable ();
20076 add_AT_unsigned (die, DW_AT_alignment, align);
20079 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20080 which specifies the distance in bits from the highest order bit of the
20081 "containing object" for the bit-field to the highest order bit of the
20082 bit-field itself.
20084 For any given bit-field, the "containing object" is a hypothetical object
20085 (of some integral or enum type) within which the given bit-field lives. The
20086 type of this hypothetical "containing object" is always the same as the
20087 declared type of the individual bit-field itself. The determination of the
20088 exact location of the "containing object" for a bit-field is rather
20089 complicated. It's handled by the `field_byte_offset' function (above).
20091 CTX is required: see the comment for VLR_CONTEXT.
20093 Note that it is the size (in bytes) of the hypothetical "containing object"
20094 which will be given in the DW_AT_byte_size attribute for this bit-field.
20095 (See `byte_size_attribute' above). */
20097 static inline void
20098 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20100 HOST_WIDE_INT object_offset_in_bytes;
20101 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20102 HOST_WIDE_INT bitpos_int;
20103 HOST_WIDE_INT highest_order_object_bit_offset;
20104 HOST_WIDE_INT highest_order_field_bit_offset;
20105 HOST_WIDE_INT bit_offset;
20107 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20109 /* Must be a field and a bit field. */
20110 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20112 /* We can't yet handle bit-fields whose offsets are variable, so if we
20113 encounter such things, just return without generating any attribute
20114 whatsoever. Likewise for variable or too large size. */
20115 if (! tree_fits_shwi_p (bit_position (decl))
20116 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20117 return;
20119 bitpos_int = int_bit_position (decl);
20121 /* Note that the bit offset is always the distance (in bits) from the
20122 highest-order bit of the "containing object" to the highest-order bit of
20123 the bit-field itself. Since the "high-order end" of any object or field
20124 is different on big-endian and little-endian machines, the computation
20125 below must take account of these differences. */
20126 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20127 highest_order_field_bit_offset = bitpos_int;
20129 if (! BYTES_BIG_ENDIAN)
20131 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20132 highest_order_object_bit_offset +=
20133 simple_type_size_in_bits (original_type);
20136 bit_offset
20137 = (! BYTES_BIG_ENDIAN
20138 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20139 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20141 if (bit_offset < 0)
20142 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20143 else
20144 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20147 /* For a FIELD_DECL node which represents a bit field, output an attribute
20148 which specifies the length in bits of the given field. */
20150 static inline void
20151 add_bit_size_attribute (dw_die_ref die, tree decl)
20153 /* Must be a field and a bit field. */
20154 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20155 && DECL_BIT_FIELD_TYPE (decl));
20157 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20158 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20161 /* If the compiled language is ANSI C, then add a 'prototyped'
20162 attribute, if arg types are given for the parameters of a function. */
20164 static inline void
20165 add_prototyped_attribute (dw_die_ref die, tree func_type)
20167 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20169 case DW_LANG_C:
20170 case DW_LANG_C89:
20171 case DW_LANG_C99:
20172 case DW_LANG_C11:
20173 case DW_LANG_ObjC:
20174 if (prototype_p (func_type))
20175 add_AT_flag (die, DW_AT_prototyped, 1);
20176 break;
20177 default:
20178 break;
20182 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20183 by looking in the type declaration, the object declaration equate table or
20184 the block mapping. */
20186 static inline dw_die_ref
20187 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20189 dw_die_ref origin_die = NULL;
20191 if (TREE_CODE (origin) != FUNCTION_DECL
20192 && TREE_CODE (origin) != BLOCK)
20194 /* We may have gotten separated from the block for the inlined
20195 function, if we're in an exception handler or some such; make
20196 sure that the abstract function has been written out.
20198 Doing this for nested functions is wrong, however; functions are
20199 distinct units, and our context might not even be inline. */
20200 tree fn = origin;
20202 if (TYPE_P (fn))
20203 fn = TYPE_STUB_DECL (fn);
20205 fn = decl_function_context (fn);
20206 if (fn)
20207 dwarf2out_abstract_function (fn);
20210 if (DECL_P (origin))
20211 origin_die = lookup_decl_die (origin);
20212 else if (TYPE_P (origin))
20213 origin_die = lookup_type_die (origin);
20214 else if (TREE_CODE (origin) == BLOCK)
20215 origin_die = BLOCK_DIE (origin);
20217 /* XXX: Functions that are never lowered don't always have correct block
20218 trees (in the case of java, they simply have no block tree, in some other
20219 languages). For these functions, there is nothing we can really do to
20220 output correct debug info for inlined functions in all cases. Rather
20221 than die, we'll just produce deficient debug info now, in that we will
20222 have variables without a proper abstract origin. In the future, when all
20223 functions are lowered, we should re-add a gcc_assert (origin_die)
20224 here. */
20226 if (origin_die)
20227 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20228 return origin_die;
20231 /* We do not currently support the pure_virtual attribute. */
20233 static inline void
20234 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20236 if (DECL_VINDEX (func_decl))
20238 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20240 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20241 add_AT_loc (die, DW_AT_vtable_elem_location,
20242 new_loc_descr (DW_OP_constu,
20243 tree_to_shwi (DECL_VINDEX (func_decl)),
20244 0));
20246 /* GNU extension: Record what type this method came from originally. */
20247 if (debug_info_level > DINFO_LEVEL_TERSE
20248 && DECL_CONTEXT (func_decl))
20249 add_AT_die_ref (die, DW_AT_containing_type,
20250 lookup_type_die (DECL_CONTEXT (func_decl)));
20254 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20255 given decl. This used to be a vendor extension until after DWARF 4
20256 standardized it. */
20258 static void
20259 add_linkage_attr (dw_die_ref die, tree decl)
20261 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20263 /* Mimic what assemble_name_raw does with a leading '*'. */
20264 if (name[0] == '*')
20265 name = &name[1];
20267 if (dwarf_version >= 4)
20268 add_AT_string (die, DW_AT_linkage_name, name);
20269 else
20270 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20273 /* Add source coordinate attributes for the given decl. */
20275 static void
20276 add_src_coords_attributes (dw_die_ref die, tree decl)
20278 expanded_location s;
20280 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20281 return;
20282 s = expand_location (DECL_SOURCE_LOCATION (decl));
20283 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20284 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20285 if (debug_column_info && s.column)
20286 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20289 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20291 static void
20292 add_linkage_name_raw (dw_die_ref die, tree decl)
20294 /* Defer until we have an assembler name set. */
20295 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20297 limbo_die_node *asm_name;
20299 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20300 asm_name->die = die;
20301 asm_name->created_for = decl;
20302 asm_name->next = deferred_asm_name;
20303 deferred_asm_name = asm_name;
20305 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20306 add_linkage_attr (die, decl);
20309 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20311 static void
20312 add_linkage_name (dw_die_ref die, tree decl)
20314 if (debug_info_level > DINFO_LEVEL_NONE
20315 && VAR_OR_FUNCTION_DECL_P (decl)
20316 && TREE_PUBLIC (decl)
20317 && !(VAR_P (decl) && DECL_REGISTER (decl))
20318 && die->die_tag != DW_TAG_member)
20319 add_linkage_name_raw (die, decl);
20322 /* Add a DW_AT_name attribute and source coordinate attribute for the
20323 given decl, but only if it actually has a name. */
20325 static void
20326 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20327 bool no_linkage_name)
20329 tree decl_name;
20331 decl_name = DECL_NAME (decl);
20332 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20334 const char *name = dwarf2_name (decl, 0);
20335 if (name)
20336 add_name_attribute (die, name);
20337 if (! DECL_ARTIFICIAL (decl))
20338 add_src_coords_attributes (die, decl);
20340 if (!no_linkage_name)
20341 add_linkage_name (die, decl);
20344 #ifdef VMS_DEBUGGING_INFO
20345 /* Get the function's name, as described by its RTL. This may be different
20346 from the DECL_NAME name used in the source file. */
20347 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20349 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20350 XEXP (DECL_RTL (decl), 0), false);
20351 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20353 #endif /* VMS_DEBUGGING_INFO */
20356 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20358 static void
20359 add_discr_value (dw_die_ref die, dw_discr_value *value)
20361 dw_attr_node attr;
20363 attr.dw_attr = DW_AT_discr_value;
20364 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20365 attr.dw_attr_val.val_entry = NULL;
20366 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20367 if (value->pos)
20368 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20369 else
20370 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20371 add_dwarf_attr (die, &attr);
20374 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20376 static void
20377 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20379 dw_attr_node attr;
20381 attr.dw_attr = DW_AT_discr_list;
20382 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20383 attr.dw_attr_val.val_entry = NULL;
20384 attr.dw_attr_val.v.val_discr_list = discr_list;
20385 add_dwarf_attr (die, &attr);
20388 static inline dw_discr_list_ref
20389 AT_discr_list (dw_attr_node *attr)
20391 return attr->dw_attr_val.v.val_discr_list;
20394 #ifdef VMS_DEBUGGING_INFO
20395 /* Output the debug main pointer die for VMS */
20397 void
20398 dwarf2out_vms_debug_main_pointer (void)
20400 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20401 dw_die_ref die;
20403 /* Allocate the VMS debug main subprogram die. */
20404 die = ggc_cleared_alloc<die_node> ();
20405 die->die_tag = DW_TAG_subprogram;
20406 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20407 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20408 current_function_funcdef_no);
20409 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20411 /* Make it the first child of comp_unit_die (). */
20412 die->die_parent = comp_unit_die ();
20413 if (comp_unit_die ()->die_child)
20415 die->die_sib = comp_unit_die ()->die_child->die_sib;
20416 comp_unit_die ()->die_child->die_sib = die;
20418 else
20420 die->die_sib = die;
20421 comp_unit_die ()->die_child = die;
20424 #endif /* VMS_DEBUGGING_INFO */
20426 /* Push a new declaration scope. */
20428 static void
20429 push_decl_scope (tree scope)
20431 vec_safe_push (decl_scope_table, scope);
20434 /* Pop a declaration scope. */
20436 static inline void
20437 pop_decl_scope (void)
20439 decl_scope_table->pop ();
20442 /* walk_tree helper function for uses_local_type, below. */
20444 static tree
20445 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20447 if (!TYPE_P (*tp))
20448 *walk_subtrees = 0;
20449 else
20451 tree name = TYPE_NAME (*tp);
20452 if (name && DECL_P (name) && decl_function_context (name))
20453 return *tp;
20455 return NULL_TREE;
20458 /* If TYPE involves a function-local type (including a local typedef to a
20459 non-local type), returns that type; otherwise returns NULL_TREE. */
20461 static tree
20462 uses_local_type (tree type)
20464 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20465 return used;
20468 /* Return the DIE for the scope that immediately contains this type.
20469 Non-named types that do not involve a function-local type get global
20470 scope. Named types nested in namespaces or other types get their
20471 containing scope. All other types (i.e. function-local named types) get
20472 the current active scope. */
20474 static dw_die_ref
20475 scope_die_for (tree t, dw_die_ref context_die)
20477 dw_die_ref scope_die = NULL;
20478 tree containing_scope;
20480 /* Non-types always go in the current scope. */
20481 gcc_assert (TYPE_P (t));
20483 /* Use the scope of the typedef, rather than the scope of the type
20484 it refers to. */
20485 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20486 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20487 else
20488 containing_scope = TYPE_CONTEXT (t);
20490 /* Use the containing namespace if there is one. */
20491 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20493 if (context_die == lookup_decl_die (containing_scope))
20494 /* OK */;
20495 else if (debug_info_level > DINFO_LEVEL_TERSE)
20496 context_die = get_context_die (containing_scope);
20497 else
20498 containing_scope = NULL_TREE;
20501 /* Ignore function type "scopes" from the C frontend. They mean that
20502 a tagged type is local to a parmlist of a function declarator, but
20503 that isn't useful to DWARF. */
20504 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20505 containing_scope = NULL_TREE;
20507 if (SCOPE_FILE_SCOPE_P (containing_scope))
20509 /* If T uses a local type keep it local as well, to avoid references
20510 to function-local DIEs from outside the function. */
20511 if (current_function_decl && uses_local_type (t))
20512 scope_die = context_die;
20513 else
20514 scope_die = comp_unit_die ();
20516 else if (TYPE_P (containing_scope))
20518 /* For types, we can just look up the appropriate DIE. */
20519 if (debug_info_level > DINFO_LEVEL_TERSE)
20520 scope_die = get_context_die (containing_scope);
20521 else
20523 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20524 if (scope_die == NULL)
20525 scope_die = comp_unit_die ();
20528 else
20529 scope_die = context_die;
20531 return scope_die;
20534 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20536 static inline int
20537 local_scope_p (dw_die_ref context_die)
20539 for (; context_die; context_die = context_die->die_parent)
20540 if (context_die->die_tag == DW_TAG_inlined_subroutine
20541 || context_die->die_tag == DW_TAG_subprogram)
20542 return 1;
20544 return 0;
20547 /* Returns nonzero if CONTEXT_DIE is a class. */
20549 static inline int
20550 class_scope_p (dw_die_ref context_die)
20552 return (context_die
20553 && (context_die->die_tag == DW_TAG_structure_type
20554 || context_die->die_tag == DW_TAG_class_type
20555 || context_die->die_tag == DW_TAG_interface_type
20556 || context_die->die_tag == DW_TAG_union_type));
20559 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20560 whether or not to treat a DIE in this context as a declaration. */
20562 static inline int
20563 class_or_namespace_scope_p (dw_die_ref context_die)
20565 return (class_scope_p (context_die)
20566 || (context_die && context_die->die_tag == DW_TAG_namespace));
20569 /* Many forms of DIEs require a "type description" attribute. This
20570 routine locates the proper "type descriptor" die for the type given
20571 by 'type' plus any additional qualifiers given by 'cv_quals', and
20572 adds a DW_AT_type attribute below the given die. */
20574 static void
20575 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20576 bool reverse, dw_die_ref context_die)
20578 enum tree_code code = TREE_CODE (type);
20579 dw_die_ref type_die = NULL;
20581 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20582 or fixed-point type, use the inner type. This is because we have no
20583 support for unnamed types in base_type_die. This can happen if this is
20584 an Ada subrange type. Correct solution is emit a subrange type die. */
20585 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20586 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20587 type = TREE_TYPE (type), code = TREE_CODE (type);
20589 if (code == ERROR_MARK
20590 /* Handle a special case. For functions whose return type is void, we
20591 generate *no* type attribute. (Note that no object may have type
20592 `void', so this only applies to function return types). */
20593 || code == VOID_TYPE)
20594 return;
20596 type_die = modified_type_die (type,
20597 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20598 reverse,
20599 context_die);
20601 if (type_die != NULL)
20602 add_AT_die_ref (object_die, DW_AT_type, type_die);
20605 /* Given an object die, add the calling convention attribute for the
20606 function call type. */
20607 static void
20608 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20610 enum dwarf_calling_convention value = DW_CC_normal;
20612 value = ((enum dwarf_calling_convention)
20613 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20615 if (is_fortran ()
20616 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20618 /* DWARF 2 doesn't provide a way to identify a program's source-level
20619 entry point. DW_AT_calling_convention attributes are only meant
20620 to describe functions' calling conventions. However, lacking a
20621 better way to signal the Fortran main program, we used this for
20622 a long time, following existing custom. Now, DWARF 4 has
20623 DW_AT_main_subprogram, which we add below, but some tools still
20624 rely on the old way, which we thus keep. */
20625 value = DW_CC_program;
20627 if (dwarf_version >= 4 || !dwarf_strict)
20628 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20631 /* Only add the attribute if the backend requests it, and
20632 is not DW_CC_normal. */
20633 if (value && (value != DW_CC_normal))
20634 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20637 /* Given a tree pointer to a struct, class, union, or enum type node, return
20638 a pointer to the (string) tag name for the given type, or zero if the type
20639 was declared without a tag. */
20641 static const char *
20642 type_tag (const_tree type)
20644 const char *name = 0;
20646 if (TYPE_NAME (type) != 0)
20648 tree t = 0;
20650 /* Find the IDENTIFIER_NODE for the type name. */
20651 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20652 && !TYPE_NAMELESS (type))
20653 t = TYPE_NAME (type);
20655 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20656 a TYPE_DECL node, regardless of whether or not a `typedef' was
20657 involved. */
20658 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20659 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20661 /* We want to be extra verbose. Don't call dwarf_name if
20662 DECL_NAME isn't set. The default hook for decl_printable_name
20663 doesn't like that, and in this context it's correct to return
20664 0, instead of "<anonymous>" or the like. */
20665 if (DECL_NAME (TYPE_NAME (type))
20666 && !DECL_NAMELESS (TYPE_NAME (type)))
20667 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20670 /* Now get the name as a string, or invent one. */
20671 if (!name && t != 0)
20672 name = IDENTIFIER_POINTER (t);
20675 return (name == 0 || *name == '\0') ? 0 : name;
20678 /* Return the type associated with a data member, make a special check
20679 for bit field types. */
20681 static inline tree
20682 member_declared_type (const_tree member)
20684 return (DECL_BIT_FIELD_TYPE (member)
20685 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20688 /* Get the decl's label, as described by its RTL. This may be different
20689 from the DECL_NAME name used in the source file. */
20691 #if 0
20692 static const char *
20693 decl_start_label (tree decl)
20695 rtx x;
20696 const char *fnname;
20698 x = DECL_RTL (decl);
20699 gcc_assert (MEM_P (x));
20701 x = XEXP (x, 0);
20702 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20704 fnname = XSTR (x, 0);
20705 return fnname;
20707 #endif
20709 /* For variable-length arrays that have been previously generated, but
20710 may be incomplete due to missing subscript info, fill the subscript
20711 info. Return TRUE if this is one of those cases. */
20712 static bool
20713 fill_variable_array_bounds (tree type)
20715 if (TREE_ASM_WRITTEN (type)
20716 && TREE_CODE (type) == ARRAY_TYPE
20717 && variably_modified_type_p (type, NULL))
20719 dw_die_ref array_die = lookup_type_die (type);
20720 if (!array_die)
20721 return false;
20722 add_subscript_info (array_die, type, !is_ada ());
20723 return true;
20725 return false;
20728 /* These routines generate the internal representation of the DIE's for
20729 the compilation unit. Debugging information is collected by walking
20730 the declaration trees passed in from dwarf2out_decl(). */
20732 static void
20733 gen_array_type_die (tree type, dw_die_ref context_die)
20735 dw_die_ref array_die;
20737 /* GNU compilers represent multidimensional array types as sequences of one
20738 dimensional array types whose element types are themselves array types.
20739 We sometimes squish that down to a single array_type DIE with multiple
20740 subscripts in the Dwarf debugging info. The draft Dwarf specification
20741 say that we are allowed to do this kind of compression in C, because
20742 there is no difference between an array of arrays and a multidimensional
20743 array. We don't do this for Ada to remain as close as possible to the
20744 actual representation, which is especially important against the language
20745 flexibilty wrt arrays of variable size. */
20747 bool collapse_nested_arrays = !is_ada ();
20749 if (fill_variable_array_bounds (type))
20750 return;
20752 dw_die_ref scope_die = scope_die_for (type, context_die);
20753 tree element_type;
20755 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20756 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20757 if (TYPE_STRING_FLAG (type)
20758 && TREE_CODE (type) == ARRAY_TYPE
20759 && is_fortran ()
20760 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20762 HOST_WIDE_INT size;
20764 array_die = new_die (DW_TAG_string_type, scope_die, type);
20765 add_name_attribute (array_die, type_tag (type));
20766 equate_type_number_to_die (type, array_die);
20767 size = int_size_in_bytes (type);
20768 if (size >= 0)
20769 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20770 else if (TYPE_DOMAIN (type) != NULL_TREE
20771 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20773 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20774 tree rszdecl = szdecl;
20776 size = int_size_in_bytes (TREE_TYPE (szdecl));
20777 if (!DECL_P (szdecl))
20779 if (TREE_CODE (szdecl) == INDIRECT_REF
20780 && DECL_P (TREE_OPERAND (szdecl, 0)))
20782 rszdecl = TREE_OPERAND (szdecl, 0);
20783 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20784 != DWARF2_ADDR_SIZE)
20785 size = 0;
20787 else
20788 size = 0;
20790 if (size > 0)
20792 dw_loc_list_ref loc
20793 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20794 NULL);
20795 if (loc)
20797 add_AT_location_description (array_die, DW_AT_string_length,
20798 loc);
20799 if (size != DWARF2_ADDR_SIZE)
20800 add_AT_unsigned (array_die, dwarf_version >= 5
20801 ? DW_AT_string_length_byte_size
20802 : DW_AT_byte_size, size);
20806 return;
20809 array_die = new_die (DW_TAG_array_type, scope_die, type);
20810 add_name_attribute (array_die, type_tag (type));
20811 equate_type_number_to_die (type, array_die);
20813 if (TREE_CODE (type) == VECTOR_TYPE)
20814 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20816 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20817 if (is_fortran ()
20818 && TREE_CODE (type) == ARRAY_TYPE
20819 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20820 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20821 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20823 #if 0
20824 /* We default the array ordering. SDB will probably do
20825 the right things even if DW_AT_ordering is not present. It's not even
20826 an issue until we start to get into multidimensional arrays anyway. If
20827 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20828 then we'll have to put the DW_AT_ordering attribute back in. (But if
20829 and when we find out that we need to put these in, we will only do so
20830 for multidimensional arrays. */
20831 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20832 #endif
20834 if (TREE_CODE (type) == VECTOR_TYPE)
20836 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20837 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20838 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20839 add_bound_info (subrange_die, DW_AT_upper_bound,
20840 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20842 else
20843 add_subscript_info (array_die, type, collapse_nested_arrays);
20845 /* Add representation of the type of the elements of this array type and
20846 emit the corresponding DIE if we haven't done it already. */
20847 element_type = TREE_TYPE (type);
20848 if (collapse_nested_arrays)
20849 while (TREE_CODE (element_type) == ARRAY_TYPE)
20851 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20852 break;
20853 element_type = TREE_TYPE (element_type);
20856 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20857 TREE_CODE (type) == ARRAY_TYPE
20858 && TYPE_REVERSE_STORAGE_ORDER (type),
20859 context_die);
20861 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20862 if (TYPE_ARTIFICIAL (type))
20863 add_AT_flag (array_die, DW_AT_artificial, 1);
20865 if (get_AT (array_die, DW_AT_name))
20866 add_pubtype (type, array_die);
20868 add_alignment_attribute (array_die, type);
20871 /* This routine generates DIE for array with hidden descriptor, details
20872 are filled into *info by a langhook. */
20874 static void
20875 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20876 dw_die_ref context_die)
20878 const dw_die_ref scope_die = scope_die_for (type, context_die);
20879 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20880 struct loc_descr_context context = { type, info->base_decl, NULL,
20881 false, false };
20882 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20883 int dim;
20885 add_name_attribute (array_die, type_tag (type));
20886 equate_type_number_to_die (type, array_die);
20888 if (info->ndimensions > 1)
20889 switch (info->ordering)
20891 case array_descr_ordering_row_major:
20892 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20893 break;
20894 case array_descr_ordering_column_major:
20895 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20896 break;
20897 default:
20898 break;
20901 if (dwarf_version >= 3 || !dwarf_strict)
20903 if (info->data_location)
20904 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20905 dw_scalar_form_exprloc, &context);
20906 if (info->associated)
20907 add_scalar_info (array_die, DW_AT_associated, info->associated,
20908 dw_scalar_form_constant
20909 | dw_scalar_form_exprloc
20910 | dw_scalar_form_reference, &context);
20911 if (info->allocated)
20912 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20913 dw_scalar_form_constant
20914 | dw_scalar_form_exprloc
20915 | dw_scalar_form_reference, &context);
20916 if (info->stride)
20918 const enum dwarf_attribute attr
20919 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20920 const int forms
20921 = (info->stride_in_bits)
20922 ? dw_scalar_form_constant
20923 : (dw_scalar_form_constant
20924 | dw_scalar_form_exprloc
20925 | dw_scalar_form_reference);
20927 add_scalar_info (array_die, attr, info->stride, forms, &context);
20930 if (dwarf_version >= 5)
20932 if (info->rank)
20934 add_scalar_info (array_die, DW_AT_rank, info->rank,
20935 dw_scalar_form_constant
20936 | dw_scalar_form_exprloc, &context);
20937 subrange_tag = DW_TAG_generic_subrange;
20938 context.placeholder_arg = true;
20942 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20944 for (dim = 0; dim < info->ndimensions; dim++)
20946 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20948 if (info->dimen[dim].bounds_type)
20949 add_type_attribute (subrange_die,
20950 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20951 false, context_die);
20952 if (info->dimen[dim].lower_bound)
20953 add_bound_info (subrange_die, DW_AT_lower_bound,
20954 info->dimen[dim].lower_bound, &context);
20955 if (info->dimen[dim].upper_bound)
20956 add_bound_info (subrange_die, DW_AT_upper_bound,
20957 info->dimen[dim].upper_bound, &context);
20958 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20959 add_scalar_info (subrange_die, DW_AT_byte_stride,
20960 info->dimen[dim].stride,
20961 dw_scalar_form_constant
20962 | dw_scalar_form_exprloc
20963 | dw_scalar_form_reference,
20964 &context);
20967 gen_type_die (info->element_type, context_die);
20968 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20969 TREE_CODE (type) == ARRAY_TYPE
20970 && TYPE_REVERSE_STORAGE_ORDER (type),
20971 context_die);
20973 if (get_AT (array_die, DW_AT_name))
20974 add_pubtype (type, array_die);
20976 add_alignment_attribute (array_die, type);
20979 #if 0
20980 static void
20981 gen_entry_point_die (tree decl, dw_die_ref context_die)
20983 tree origin = decl_ultimate_origin (decl);
20984 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
20986 if (origin != NULL)
20987 add_abstract_origin_attribute (decl_die, origin);
20988 else
20990 add_name_and_src_coords_attributes (decl_die, decl);
20991 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
20992 TYPE_UNQUALIFIED, false, context_die);
20995 if (DECL_ABSTRACT_P (decl))
20996 equate_decl_number_to_die (decl, decl_die);
20997 else
20998 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21000 #endif
21002 /* Walk through the list of incomplete types again, trying once more to
21003 emit full debugging info for them. */
21005 static void
21006 retry_incomplete_types (void)
21008 set_early_dwarf s;
21009 int i;
21011 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21012 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21013 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21014 vec_safe_truncate (incomplete_types, 0);
21017 /* Determine what tag to use for a record type. */
21019 static enum dwarf_tag
21020 record_type_tag (tree type)
21022 if (! lang_hooks.types.classify_record)
21023 return DW_TAG_structure_type;
21025 switch (lang_hooks.types.classify_record (type))
21027 case RECORD_IS_STRUCT:
21028 return DW_TAG_structure_type;
21030 case RECORD_IS_CLASS:
21031 return DW_TAG_class_type;
21033 case RECORD_IS_INTERFACE:
21034 if (dwarf_version >= 3 || !dwarf_strict)
21035 return DW_TAG_interface_type;
21036 return DW_TAG_structure_type;
21038 default:
21039 gcc_unreachable ();
21043 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21044 include all of the information about the enumeration values also. Each
21045 enumerated type name/value is listed as a child of the enumerated type
21046 DIE. */
21048 static dw_die_ref
21049 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21051 dw_die_ref type_die = lookup_type_die (type);
21053 if (type_die == NULL)
21055 type_die = new_die (DW_TAG_enumeration_type,
21056 scope_die_for (type, context_die), type);
21057 equate_type_number_to_die (type, type_die);
21058 add_name_attribute (type_die, type_tag (type));
21059 if (dwarf_version >= 4 || !dwarf_strict)
21061 if (ENUM_IS_SCOPED (type))
21062 add_AT_flag (type_die, DW_AT_enum_class, 1);
21063 if (ENUM_IS_OPAQUE (type))
21064 add_AT_flag (type_die, DW_AT_declaration, 1);
21066 if (!dwarf_strict)
21067 add_AT_unsigned (type_die, DW_AT_encoding,
21068 TYPE_UNSIGNED (type)
21069 ? DW_ATE_unsigned
21070 : DW_ATE_signed);
21072 else if (! TYPE_SIZE (type))
21073 return type_die;
21074 else
21075 remove_AT (type_die, DW_AT_declaration);
21077 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21078 given enum type is incomplete, do not generate the DW_AT_byte_size
21079 attribute or the DW_AT_element_list attribute. */
21080 if (TYPE_SIZE (type))
21082 tree link;
21084 TREE_ASM_WRITTEN (type) = 1;
21085 add_byte_size_attribute (type_die, type);
21086 add_alignment_attribute (type_die, type);
21087 if (dwarf_version >= 3 || !dwarf_strict)
21089 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21090 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21091 context_die);
21093 if (TYPE_STUB_DECL (type) != NULL_TREE)
21095 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21096 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21099 /* If the first reference to this type was as the return type of an
21100 inline function, then it may not have a parent. Fix this now. */
21101 if (type_die->die_parent == NULL)
21102 add_child_die (scope_die_for (type, context_die), type_die);
21104 for (link = TYPE_VALUES (type);
21105 link != NULL; link = TREE_CHAIN (link))
21107 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21108 tree value = TREE_VALUE (link);
21110 add_name_attribute (enum_die,
21111 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21113 if (TREE_CODE (value) == CONST_DECL)
21114 value = DECL_INITIAL (value);
21116 if (simple_type_size_in_bits (TREE_TYPE (value))
21117 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21119 /* For constant forms created by add_AT_unsigned DWARF
21120 consumers (GDB, elfutils, etc.) always zero extend
21121 the value. Only when the actual value is negative
21122 do we need to use add_AT_int to generate a constant
21123 form that can represent negative values. */
21124 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21125 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21126 add_AT_unsigned (enum_die, DW_AT_const_value,
21127 (unsigned HOST_WIDE_INT) val);
21128 else
21129 add_AT_int (enum_die, DW_AT_const_value, val);
21131 else
21132 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21133 that here. TODO: This should be re-worked to use correct
21134 signed/unsigned double tags for all cases. */
21135 add_AT_wide (enum_die, DW_AT_const_value, value);
21138 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21139 if (TYPE_ARTIFICIAL (type))
21140 add_AT_flag (type_die, DW_AT_artificial, 1);
21142 else
21143 add_AT_flag (type_die, DW_AT_declaration, 1);
21145 add_alignment_attribute (type_die, type);
21147 add_pubtype (type, type_die);
21149 return type_die;
21152 /* Generate a DIE to represent either a real live formal parameter decl or to
21153 represent just the type of some formal parameter position in some function
21154 type.
21156 Note that this routine is a bit unusual because its argument may be a
21157 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21158 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21159 node. If it's the former then this function is being called to output a
21160 DIE to represent a formal parameter object (or some inlining thereof). If
21161 it's the latter, then this function is only being called to output a
21162 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21163 argument type of some subprogram type.
21164 If EMIT_NAME_P is true, name and source coordinate attributes
21165 are emitted. */
21167 static dw_die_ref
21168 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21169 dw_die_ref context_die)
21171 tree node_or_origin = node ? node : origin;
21172 tree ultimate_origin;
21173 dw_die_ref parm_die = NULL;
21175 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21177 parm_die = lookup_decl_die (node);
21179 /* If the contexts differ, we may not be talking about the same
21180 thing. */
21181 if (parm_die && parm_die->die_parent != context_die)
21183 if (!DECL_ABSTRACT_P (node))
21185 /* This can happen when creating an inlined instance, in
21186 which case we need to create a new DIE that will get
21187 annotated with DW_AT_abstract_origin. */
21188 parm_die = NULL;
21190 else
21192 /* FIXME: Reuse DIE even with a differing context.
21194 This can happen when calling
21195 dwarf2out_abstract_function to build debug info for
21196 the abstract instance of a function for which we have
21197 already generated a DIE in
21198 dwarf2out_early_global_decl.
21200 Once we remove dwarf2out_abstract_function, we should
21201 have a call to gcc_unreachable here. */
21205 if (parm_die && parm_die->die_parent == NULL)
21207 /* Check that parm_die already has the right attributes that
21208 we would have added below. If any attributes are
21209 missing, fall through to add them. */
21210 if (! DECL_ABSTRACT_P (node_or_origin)
21211 && !get_AT (parm_die, DW_AT_location)
21212 && !get_AT (parm_die, DW_AT_const_value))
21213 /* We are missing location info, and are about to add it. */
21215 else
21217 add_child_die (context_die, parm_die);
21218 return parm_die;
21223 /* If we have a previously generated DIE, use it, unless this is an
21224 concrete instance (origin != NULL), in which case we need a new
21225 DIE with a corresponding DW_AT_abstract_origin. */
21226 bool reusing_die;
21227 if (parm_die && origin == NULL)
21228 reusing_die = true;
21229 else
21231 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21232 reusing_die = false;
21235 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21237 case tcc_declaration:
21238 ultimate_origin = decl_ultimate_origin (node_or_origin);
21239 if (node || ultimate_origin)
21240 origin = ultimate_origin;
21242 if (reusing_die)
21243 goto add_location;
21245 if (origin != NULL)
21246 add_abstract_origin_attribute (parm_die, origin);
21247 else if (emit_name_p)
21248 add_name_and_src_coords_attributes (parm_die, node);
21249 if (origin == NULL
21250 || (! DECL_ABSTRACT_P (node_or_origin)
21251 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21252 decl_function_context
21253 (node_or_origin))))
21255 tree type = TREE_TYPE (node_or_origin);
21256 if (decl_by_reference_p (node_or_origin))
21257 add_type_attribute (parm_die, TREE_TYPE (type),
21258 TYPE_UNQUALIFIED,
21259 false, context_die);
21260 else
21261 add_type_attribute (parm_die, type,
21262 decl_quals (node_or_origin),
21263 false, context_die);
21265 if (origin == NULL && DECL_ARTIFICIAL (node))
21266 add_AT_flag (parm_die, DW_AT_artificial, 1);
21267 add_location:
21268 if (node && node != origin)
21269 equate_decl_number_to_die (node, parm_die);
21270 if (! DECL_ABSTRACT_P (node_or_origin))
21271 add_location_or_const_value_attribute (parm_die, node_or_origin,
21272 node == NULL);
21274 break;
21276 case tcc_type:
21277 /* We were called with some kind of a ..._TYPE node. */
21278 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21279 context_die);
21280 break;
21282 default:
21283 gcc_unreachable ();
21286 return parm_die;
21289 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21290 children DW_TAG_formal_parameter DIEs representing the arguments of the
21291 parameter pack.
21293 PARM_PACK must be a function parameter pack.
21294 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21295 must point to the subsequent arguments of the function PACK_ARG belongs to.
21296 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21297 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21298 following the last one for which a DIE was generated. */
21300 static dw_die_ref
21301 gen_formal_parameter_pack_die (tree parm_pack,
21302 tree pack_arg,
21303 dw_die_ref subr_die,
21304 tree *next_arg)
21306 tree arg;
21307 dw_die_ref parm_pack_die;
21309 gcc_assert (parm_pack
21310 && lang_hooks.function_parameter_pack_p (parm_pack)
21311 && subr_die);
21313 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21314 add_src_coords_attributes (parm_pack_die, parm_pack);
21316 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21318 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21319 parm_pack))
21320 break;
21321 gen_formal_parameter_die (arg, NULL,
21322 false /* Don't emit name attribute. */,
21323 parm_pack_die);
21325 if (next_arg)
21326 *next_arg = arg;
21327 return parm_pack_die;
21330 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21331 at the end of an (ANSI prototyped) formal parameters list. */
21333 static void
21334 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21336 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21339 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21340 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21341 parameters as specified in some function type specification (except for
21342 those which appear as part of a function *definition*). */
21344 static void
21345 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21347 tree link;
21348 tree formal_type = NULL;
21349 tree first_parm_type;
21350 tree arg;
21352 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21354 arg = DECL_ARGUMENTS (function_or_method_type);
21355 function_or_method_type = TREE_TYPE (function_or_method_type);
21357 else
21358 arg = NULL_TREE;
21360 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21362 /* Make our first pass over the list of formal parameter types and output a
21363 DW_TAG_formal_parameter DIE for each one. */
21364 for (link = first_parm_type; link; )
21366 dw_die_ref parm_die;
21368 formal_type = TREE_VALUE (link);
21369 if (formal_type == void_type_node)
21370 break;
21372 /* Output a (nameless) DIE to represent the formal parameter itself. */
21373 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21375 parm_die = gen_formal_parameter_die (formal_type, NULL,
21376 true /* Emit name attribute. */,
21377 context_die);
21378 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21379 && link == first_parm_type)
21381 add_AT_flag (parm_die, DW_AT_artificial, 1);
21382 if (dwarf_version >= 3 || !dwarf_strict)
21383 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21385 else if (arg && DECL_ARTIFICIAL (arg))
21386 add_AT_flag (parm_die, DW_AT_artificial, 1);
21389 link = TREE_CHAIN (link);
21390 if (arg)
21391 arg = DECL_CHAIN (arg);
21394 /* If this function type has an ellipsis, add a
21395 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21396 if (formal_type != void_type_node)
21397 gen_unspecified_parameters_die (function_or_method_type, context_die);
21399 /* Make our second (and final) pass over the list of formal parameter types
21400 and output DIEs to represent those types (as necessary). */
21401 for (link = TYPE_ARG_TYPES (function_or_method_type);
21402 link && TREE_VALUE (link);
21403 link = TREE_CHAIN (link))
21404 gen_type_die (TREE_VALUE (link), context_die);
21407 /* We want to generate the DIE for TYPE so that we can generate the
21408 die for MEMBER, which has been defined; we will need to refer back
21409 to the member declaration nested within TYPE. If we're trying to
21410 generate minimal debug info for TYPE, processing TYPE won't do the
21411 trick; we need to attach the member declaration by hand. */
21413 static void
21414 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21416 gen_type_die (type, context_die);
21418 /* If we're trying to avoid duplicate debug info, we may not have
21419 emitted the member decl for this function. Emit it now. */
21420 if (TYPE_STUB_DECL (type)
21421 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21422 && ! lookup_decl_die (member))
21424 dw_die_ref type_die;
21425 gcc_assert (!decl_ultimate_origin (member));
21427 push_decl_scope (type);
21428 type_die = lookup_type_die_strip_naming_typedef (type);
21429 if (TREE_CODE (member) == FUNCTION_DECL)
21430 gen_subprogram_die (member, type_die);
21431 else if (TREE_CODE (member) == FIELD_DECL)
21433 /* Ignore the nameless fields that are used to skip bits but handle
21434 C++ anonymous unions and structs. */
21435 if (DECL_NAME (member) != NULL_TREE
21436 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21437 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21439 struct vlr_context vlr_ctx = {
21440 DECL_CONTEXT (member), /* struct_type */
21441 NULL_TREE /* variant_part_offset */
21443 gen_type_die (member_declared_type (member), type_die);
21444 gen_field_die (member, &vlr_ctx, type_die);
21447 else
21448 gen_variable_die (member, NULL_TREE, type_die);
21450 pop_decl_scope ();
21454 /* Forward declare these functions, because they are mutually recursive
21455 with their set_block_* pairing functions. */
21456 static void set_decl_origin_self (tree);
21457 static void set_decl_abstract_flags (tree, vec<tree> &);
21459 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21460 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21461 that it points to the node itself, thus indicating that the node is its
21462 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21463 the given node is NULL, recursively descend the decl/block tree which
21464 it is the root of, and for each other ..._DECL or BLOCK node contained
21465 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21466 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21467 values to point to themselves. */
21469 static void
21470 set_block_origin_self (tree stmt)
21472 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21474 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21477 tree local_decl;
21479 for (local_decl = BLOCK_VARS (stmt);
21480 local_decl != NULL_TREE;
21481 local_decl = DECL_CHAIN (local_decl))
21482 /* Do not recurse on nested functions since the inlining status
21483 of parent and child can be different as per the DWARF spec. */
21484 if (TREE_CODE (local_decl) != FUNCTION_DECL
21485 && !DECL_EXTERNAL (local_decl))
21486 set_decl_origin_self (local_decl);
21490 tree subblock;
21492 for (subblock = BLOCK_SUBBLOCKS (stmt);
21493 subblock != NULL_TREE;
21494 subblock = BLOCK_CHAIN (subblock))
21495 set_block_origin_self (subblock); /* Recurse. */
21500 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21501 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21502 node to so that it points to the node itself, thus indicating that the
21503 node represents its own (abstract) origin. Additionally, if the
21504 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21505 the decl/block tree of which the given node is the root of, and for
21506 each other ..._DECL or BLOCK node contained therein whose
21507 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21508 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21509 point to themselves. */
21511 static void
21512 set_decl_origin_self (tree decl)
21514 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21516 DECL_ABSTRACT_ORIGIN (decl) = decl;
21517 if (TREE_CODE (decl) == FUNCTION_DECL)
21519 tree arg;
21521 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21522 DECL_ABSTRACT_ORIGIN (arg) = arg;
21523 if (DECL_INITIAL (decl) != NULL_TREE
21524 && DECL_INITIAL (decl) != error_mark_node)
21525 set_block_origin_self (DECL_INITIAL (decl));
21530 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21531 and if it wasn't 1 before, push it to abstract_vec vector.
21532 For all local decls and all local sub-blocks (recursively) do it
21533 too. */
21535 static void
21536 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21538 tree local_decl;
21539 tree subblock;
21540 unsigned int i;
21542 if (!BLOCK_ABSTRACT (stmt))
21544 abstract_vec.safe_push (stmt);
21545 BLOCK_ABSTRACT (stmt) = 1;
21548 for (local_decl = BLOCK_VARS (stmt);
21549 local_decl != NULL_TREE;
21550 local_decl = DECL_CHAIN (local_decl))
21551 if (! DECL_EXTERNAL (local_decl))
21552 set_decl_abstract_flags (local_decl, abstract_vec);
21554 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21556 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21557 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21558 || TREE_CODE (local_decl) == PARM_DECL)
21559 set_decl_abstract_flags (local_decl, abstract_vec);
21562 for (subblock = BLOCK_SUBBLOCKS (stmt);
21563 subblock != NULL_TREE;
21564 subblock = BLOCK_CHAIN (subblock))
21565 set_block_abstract_flags (subblock, abstract_vec);
21568 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21569 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21570 In the case where the decl is a FUNCTION_DECL also set the abstract
21571 flags for all of the parameters, local vars, local
21572 blocks and sub-blocks (recursively). */
21574 static void
21575 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21577 if (!DECL_ABSTRACT_P (decl))
21579 abstract_vec.safe_push (decl);
21580 DECL_ABSTRACT_P (decl) = 1;
21583 if (TREE_CODE (decl) == FUNCTION_DECL)
21585 tree arg;
21587 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21588 if (!DECL_ABSTRACT_P (arg))
21590 abstract_vec.safe_push (arg);
21591 DECL_ABSTRACT_P (arg) = 1;
21593 if (DECL_INITIAL (decl) != NULL_TREE
21594 && DECL_INITIAL (decl) != error_mark_node)
21595 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21599 /* Generate the DWARF2 info for the "abstract" instance of a function which we
21600 may later generate inlined and/or out-of-line instances of.
21602 FIXME: In the early-dwarf world, this function, and most of the
21603 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21604 the abstract instance. All we would need to do is annotate
21605 the early DIE with the appropriate DW_AT_inline in late
21606 dwarf (perhaps in gen_inlined_subroutine_die).
21608 However, we can't do this yet, because LTO streaming of DIEs
21609 has not been implemented yet. */
21611 static void
21612 dwarf2out_abstract_function (tree decl)
21614 dw_die_ref old_die;
21615 tree save_fn;
21616 tree context;
21617 hash_table<decl_loc_hasher> *old_decl_loc_table;
21618 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21619 int old_call_site_count, old_tail_call_site_count;
21620 struct call_arg_loc_node *old_call_arg_locations;
21622 /* Make sure we have the actual abstract inline, not a clone. */
21623 decl = DECL_ORIGIN (decl);
21625 old_die = lookup_decl_die (decl);
21626 if (old_die && get_AT (old_die, DW_AT_inline))
21627 /* We've already generated the abstract instance. */
21628 return;
21630 /* We can be called while recursively when seeing block defining inlined subroutine
21631 DIE. Be sure to not clobber the outer location table nor use it or we would
21632 get locations in abstract instantces. */
21633 old_decl_loc_table = decl_loc_table;
21634 decl_loc_table = NULL;
21635 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21636 cached_dw_loc_list_table = NULL;
21637 old_call_arg_locations = call_arg_locations;
21638 call_arg_locations = NULL;
21639 old_call_site_count = call_site_count;
21640 call_site_count = -1;
21641 old_tail_call_site_count = tail_call_site_count;
21642 tail_call_site_count = -1;
21644 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21645 we don't get confused by DECL_ABSTRACT_P. */
21646 if (debug_info_level > DINFO_LEVEL_TERSE)
21648 context = decl_class_context (decl);
21649 if (context)
21650 gen_type_die_for_member
21651 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21654 /* Pretend we've just finished compiling this function. */
21655 save_fn = current_function_decl;
21656 current_function_decl = decl;
21658 auto_vec<tree, 64> abstract_vec;
21659 set_decl_abstract_flags (decl, abstract_vec);
21660 dwarf2out_decl (decl);
21661 unsigned int i;
21662 tree t;
21663 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21664 if (TREE_CODE (t) == BLOCK)
21665 BLOCK_ABSTRACT (t) = 0;
21666 else
21667 DECL_ABSTRACT_P (t) = 0;
21669 current_function_decl = save_fn;
21670 decl_loc_table = old_decl_loc_table;
21671 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21672 call_arg_locations = old_call_arg_locations;
21673 call_site_count = old_call_site_count;
21674 tail_call_site_count = old_tail_call_site_count;
21677 /* Helper function of premark_used_types() which gets called through
21678 htab_traverse.
21680 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21681 marked as unused by prune_unused_types. */
21683 bool
21684 premark_used_types_helper (tree const &type, void *)
21686 dw_die_ref die;
21688 die = lookup_type_die (type);
21689 if (die != NULL)
21690 die->die_perennial_p = 1;
21691 return true;
21694 /* Helper function of premark_types_used_by_global_vars which gets called
21695 through htab_traverse.
21697 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21698 marked as unused by prune_unused_types. The DIE of the type is marked
21699 only if the global variable using the type will actually be emitted. */
21702 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21703 void *)
21705 struct types_used_by_vars_entry *entry;
21706 dw_die_ref die;
21708 entry = (struct types_used_by_vars_entry *) *slot;
21709 gcc_assert (entry->type != NULL
21710 && entry->var_decl != NULL);
21711 die = lookup_type_die (entry->type);
21712 if (die)
21714 /* Ask cgraph if the global variable really is to be emitted.
21715 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21716 varpool_node *node = varpool_node::get (entry->var_decl);
21717 if (node && node->definition)
21719 die->die_perennial_p = 1;
21720 /* Keep the parent DIEs as well. */
21721 while ((die = die->die_parent) && die->die_perennial_p == 0)
21722 die->die_perennial_p = 1;
21725 return 1;
21728 /* Mark all members of used_types_hash as perennial. */
21730 static void
21731 premark_used_types (struct function *fun)
21733 if (fun && fun->used_types_hash)
21734 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21737 /* Mark all members of types_used_by_vars_entry as perennial. */
21739 static void
21740 premark_types_used_by_global_vars (void)
21742 if (types_used_by_vars_hash)
21743 types_used_by_vars_hash
21744 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21747 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21748 for CA_LOC call arg loc node. */
21750 static dw_die_ref
21751 gen_call_site_die (tree decl, dw_die_ref subr_die,
21752 struct call_arg_loc_node *ca_loc)
21754 dw_die_ref stmt_die = NULL, die;
21755 tree block = ca_loc->block;
21757 while (block
21758 && block != DECL_INITIAL (decl)
21759 && TREE_CODE (block) == BLOCK)
21761 stmt_die = BLOCK_DIE (block);
21762 if (stmt_die)
21763 break;
21764 block = BLOCK_SUPERCONTEXT (block);
21766 if (stmt_die == NULL)
21767 stmt_die = subr_die;
21768 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21769 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21770 if (ca_loc->tail_call_p)
21771 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21772 if (ca_loc->symbol_ref)
21774 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21775 if (tdie)
21776 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21777 else
21778 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21779 false);
21781 return die;
21784 /* Generate a DIE to represent a declared function (either file-scope or
21785 block-local). */
21787 static void
21788 gen_subprogram_die (tree decl, dw_die_ref context_die)
21790 tree origin = decl_ultimate_origin (decl);
21791 dw_die_ref subr_die;
21792 dw_die_ref old_die = lookup_decl_die (decl);
21794 /* This function gets called multiple times for different stages of
21795 the debug process. For example, for func() in this code:
21797 namespace S
21799 void func() { ... }
21802 ...we get called 4 times. Twice in early debug and twice in
21803 late debug:
21805 Early debug
21806 -----------
21808 1. Once while generating func() within the namespace. This is
21809 the declaration. The declaration bit below is set, as the
21810 context is the namespace.
21812 A new DIE will be generated with DW_AT_declaration set.
21814 2. Once for func() itself. This is the specification. The
21815 declaration bit below is clear as the context is the CU.
21817 We will use the cached DIE from (1) to create a new DIE with
21818 DW_AT_specification pointing to the declaration in (1).
21820 Late debug via rest_of_handle_final()
21821 -------------------------------------
21823 3. Once generating func() within the namespace. This is also the
21824 declaration, as in (1), but this time we will early exit below
21825 as we have a cached DIE and a declaration needs no additional
21826 annotations (no locations), as the source declaration line
21827 info is enough.
21829 4. Once for func() itself. As in (2), this is the specification,
21830 but this time we will re-use the cached DIE, and just annotate
21831 it with the location information that should now be available.
21833 For something without namespaces, but with abstract instances, we
21834 are also called a multiple times:
21836 class Base
21838 public:
21839 Base (); // constructor declaration (1)
21842 Base::Base () { } // constructor specification (2)
21844 Early debug
21845 -----------
21847 1. Once for the Base() constructor by virtue of it being a
21848 member of the Base class. This is done via
21849 rest_of_type_compilation.
21851 This is a declaration, so a new DIE will be created with
21852 DW_AT_declaration.
21854 2. Once for the Base() constructor definition, but this time
21855 while generating the abstract instance of the base
21856 constructor (__base_ctor) which is being generated via early
21857 debug of reachable functions.
21859 Even though we have a cached version of the declaration (1),
21860 we will create a DW_AT_specification of the declaration DIE
21861 in (1).
21863 3. Once for the __base_ctor itself, but this time, we generate
21864 an DW_AT_abstract_origin version of the DW_AT_specification in
21865 (2).
21867 Late debug via rest_of_handle_final
21868 -----------------------------------
21870 4. One final time for the __base_ctor (which will have a cached
21871 DIE with DW_AT_abstract_origin created in (3). This time,
21872 we will just annotate the location information now
21873 available.
21875 int declaration = (current_function_decl != decl
21876 || class_or_namespace_scope_p (context_die));
21878 /* Now that the C++ front end lazily declares artificial member fns, we
21879 might need to retrofit the declaration into its class. */
21880 if (!declaration && !origin && !old_die
21881 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21882 && !class_or_namespace_scope_p (context_die)
21883 && debug_info_level > DINFO_LEVEL_TERSE)
21884 old_die = force_decl_die (decl);
21886 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21887 if (origin != NULL)
21889 gcc_assert (!declaration || local_scope_p (context_die));
21891 /* Fixup die_parent for the abstract instance of a nested
21892 inline function. */
21893 if (old_die && old_die->die_parent == NULL)
21894 add_child_die (context_die, old_die);
21896 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21898 /* If we have a DW_AT_abstract_origin we have a working
21899 cached version. */
21900 subr_die = old_die;
21902 else
21904 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21905 add_abstract_origin_attribute (subr_die, origin);
21906 /* This is where the actual code for a cloned function is.
21907 Let's emit linkage name attribute for it. This helps
21908 debuggers to e.g, set breakpoints into
21909 constructors/destructors when the user asks "break
21910 K::K". */
21911 add_linkage_name (subr_die, decl);
21914 /* A cached copy, possibly from early dwarf generation. Reuse as
21915 much as possible. */
21916 else if (old_die)
21918 /* A declaration that has been previously dumped needs no
21919 additional information. */
21920 if (declaration)
21921 return;
21923 if (!get_AT_flag (old_die, DW_AT_declaration)
21924 /* We can have a normal definition following an inline one in the
21925 case of redefinition of GNU C extern inlines.
21926 It seems reasonable to use AT_specification in this case. */
21927 && !get_AT (old_die, DW_AT_inline))
21929 /* Detect and ignore this case, where we are trying to output
21930 something we have already output. */
21931 if (get_AT (old_die, DW_AT_low_pc)
21932 || get_AT (old_die, DW_AT_ranges))
21933 return;
21935 /* If we have no location information, this must be a
21936 partially generated DIE from early dwarf generation.
21937 Fall through and generate it. */
21940 /* If the definition comes from the same place as the declaration,
21941 maybe use the old DIE. We always want the DIE for this function
21942 that has the *_pc attributes to be under comp_unit_die so the
21943 debugger can find it. We also need to do this for abstract
21944 instances of inlines, since the spec requires the out-of-line copy
21945 to have the same parent. For local class methods, this doesn't
21946 apply; we just use the old DIE. */
21947 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21948 struct dwarf_file_data * file_index = lookup_filename (s.file);
21949 if ((is_cu_die (old_die->die_parent)
21950 /* This condition fixes the inconsistency/ICE with the
21951 following Fortran test (or some derivative thereof) while
21952 building libgfortran:
21954 module some_m
21955 contains
21956 logical function funky (FLAG)
21957 funky = .true.
21958 end function
21959 end module
21961 || (old_die->die_parent
21962 && old_die->die_parent->die_tag == DW_TAG_module)
21963 || context_die == NULL)
21964 && (DECL_ARTIFICIAL (decl)
21965 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21966 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21967 == (unsigned) s.line)
21968 && (!debug_column_info
21969 || s.column == 0
21970 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21971 == (unsigned) s.column)))))
21973 subr_die = old_die;
21975 /* Clear out the declaration attribute, but leave the
21976 parameters so they can be augmented with location
21977 information later. Unless this was a declaration, in
21978 which case, wipe out the nameless parameters and recreate
21979 them further down. */
21980 if (remove_AT (subr_die, DW_AT_declaration))
21983 remove_AT (subr_die, DW_AT_object_pointer);
21984 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21987 /* Make a specification pointing to the previously built
21988 declaration. */
21989 else
21991 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21992 add_AT_specification (subr_die, old_die);
21993 add_pubname (decl, subr_die);
21994 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21995 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21996 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21997 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21998 if (debug_column_info
21999 && s.column
22000 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22001 != (unsigned) s.column))
22002 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22004 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22005 emit the real type on the definition die. */
22006 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22008 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22009 if (die == auto_die || die == decltype_auto_die)
22010 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22011 TYPE_UNQUALIFIED, false, context_die);
22014 /* When we process the method declaration, we haven't seen
22015 the out-of-class defaulted definition yet, so we have to
22016 recheck now. */
22017 if ((dwarf_version >= 5 || ! dwarf_strict)
22018 && !get_AT (subr_die, DW_AT_defaulted))
22020 int defaulted
22021 = lang_hooks.decls.decl_dwarf_attribute (decl,
22022 DW_AT_defaulted);
22023 if (defaulted != -1)
22025 /* Other values must have been handled before. */
22026 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22027 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22032 /* Create a fresh DIE for anything else. */
22033 else
22035 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22037 if (TREE_PUBLIC (decl))
22038 add_AT_flag (subr_die, DW_AT_external, 1);
22040 add_name_and_src_coords_attributes (subr_die, decl);
22041 add_pubname (decl, subr_die);
22042 if (debug_info_level > DINFO_LEVEL_TERSE)
22044 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22045 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22046 TYPE_UNQUALIFIED, false, context_die);
22049 add_pure_or_virtual_attribute (subr_die, decl);
22050 if (DECL_ARTIFICIAL (decl))
22051 add_AT_flag (subr_die, DW_AT_artificial, 1);
22053 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22054 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22056 add_alignment_attribute (subr_die, decl);
22058 add_accessibility_attribute (subr_die, decl);
22061 /* Unless we have an existing non-declaration DIE, equate the new
22062 DIE. */
22063 if (!old_die || is_declaration_die (old_die))
22064 equate_decl_number_to_die (decl, subr_die);
22066 if (declaration)
22068 if (!old_die || !get_AT (old_die, DW_AT_inline))
22070 add_AT_flag (subr_die, DW_AT_declaration, 1);
22072 /* If this is an explicit function declaration then generate
22073 a DW_AT_explicit attribute. */
22074 if ((dwarf_version >= 3 || !dwarf_strict)
22075 && lang_hooks.decls.decl_dwarf_attribute (decl,
22076 DW_AT_explicit) == 1)
22077 add_AT_flag (subr_die, DW_AT_explicit, 1);
22079 /* If this is a C++11 deleted special function member then generate
22080 a DW_AT_deleted attribute. */
22081 if ((dwarf_version >= 5 || !dwarf_strict)
22082 && lang_hooks.decls.decl_dwarf_attribute (decl,
22083 DW_AT_deleted) == 1)
22084 add_AT_flag (subr_die, DW_AT_deleted, 1);
22086 /* If this is a C++11 defaulted special function member then
22087 generate a DW_AT_defaulted attribute. */
22088 if (dwarf_version >= 5 || !dwarf_strict)
22090 int defaulted
22091 = lang_hooks.decls.decl_dwarf_attribute (decl,
22092 DW_AT_defaulted);
22093 if (defaulted != -1)
22094 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22097 /* If this is a C++11 non-static member function with & ref-qualifier
22098 then generate a DW_AT_reference attribute. */
22099 if ((dwarf_version >= 5 || !dwarf_strict)
22100 && lang_hooks.decls.decl_dwarf_attribute (decl,
22101 DW_AT_reference) == 1)
22102 add_AT_flag (subr_die, DW_AT_reference, 1);
22104 /* If this is a C++11 non-static member function with &&
22105 ref-qualifier then generate a DW_AT_reference attribute. */
22106 if ((dwarf_version >= 5 || !dwarf_strict)
22107 && lang_hooks.decls.decl_dwarf_attribute (decl,
22108 DW_AT_rvalue_reference)
22109 == 1)
22110 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22113 /* Tag abstract instances with DW_AT_inline. */
22114 else if (DECL_ABSTRACT_P (decl))
22116 if (DECL_DECLARED_INLINE_P (decl))
22118 if (cgraph_function_possibly_inlined_p (decl))
22119 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22120 else
22121 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22123 else
22125 if (cgraph_function_possibly_inlined_p (decl))
22126 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22127 else
22128 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22131 if (DECL_DECLARED_INLINE_P (decl)
22132 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22133 add_AT_flag (subr_die, DW_AT_artificial, 1);
22135 /* For non DECL_EXTERNALs, if range information is available, fill
22136 the DIE with it. */
22137 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22139 HOST_WIDE_INT cfa_fb_offset;
22141 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22143 if (!crtl->has_bb_partition)
22145 dw_fde_ref fde = fun->fde;
22146 if (fde->dw_fde_begin)
22148 /* We have already generated the labels. */
22149 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22150 fde->dw_fde_end, false);
22152 else
22154 /* Create start/end labels and add the range. */
22155 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22156 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22157 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22158 current_function_funcdef_no);
22159 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22160 current_function_funcdef_no);
22161 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22162 false);
22165 #if VMS_DEBUGGING_INFO
22166 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22167 Section 2.3 Prologue and Epilogue Attributes:
22168 When a breakpoint is set on entry to a function, it is generally
22169 desirable for execution to be suspended, not on the very first
22170 instruction of the function, but rather at a point after the
22171 function's frame has been set up, after any language defined local
22172 declaration processing has been completed, and before execution of
22173 the first statement of the function begins. Debuggers generally
22174 cannot properly determine where this point is. Similarly for a
22175 breakpoint set on exit from a function. The prologue and epilogue
22176 attributes allow a compiler to communicate the location(s) to use. */
22179 if (fde->dw_fde_vms_end_prologue)
22180 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22181 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22183 if (fde->dw_fde_vms_begin_epilogue)
22184 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22185 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22187 #endif
22190 else
22192 /* Generate pubnames entries for the split function code ranges. */
22193 dw_fde_ref fde = fun->fde;
22195 if (fde->dw_fde_second_begin)
22197 if (dwarf_version >= 3 || !dwarf_strict)
22199 /* We should use ranges for non-contiguous code section
22200 addresses. Use the actual code range for the initial
22201 section, since the HOT/COLD labels might precede an
22202 alignment offset. */
22203 bool range_list_added = false;
22204 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22205 fde->dw_fde_end, &range_list_added,
22206 false);
22207 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22208 fde->dw_fde_second_end,
22209 &range_list_added, false);
22210 if (range_list_added)
22211 add_ranges (NULL);
22213 else
22215 /* There is no real support in DW2 for this .. so we make
22216 a work-around. First, emit the pub name for the segment
22217 containing the function label. Then make and emit a
22218 simplified subprogram DIE for the second segment with the
22219 name pre-fixed by __hot/cold_sect_of_. We use the same
22220 linkage name for the second die so that gdb will find both
22221 sections when given "b foo". */
22222 const char *name = NULL;
22223 tree decl_name = DECL_NAME (decl);
22224 dw_die_ref seg_die;
22226 /* Do the 'primary' section. */
22227 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22228 fde->dw_fde_end, false);
22230 /* Build a minimal DIE for the secondary section. */
22231 seg_die = new_die (DW_TAG_subprogram,
22232 subr_die->die_parent, decl);
22234 if (TREE_PUBLIC (decl))
22235 add_AT_flag (seg_die, DW_AT_external, 1);
22237 if (decl_name != NULL
22238 && IDENTIFIER_POINTER (decl_name) != NULL)
22240 name = dwarf2_name (decl, 1);
22241 if (! DECL_ARTIFICIAL (decl))
22242 add_src_coords_attributes (seg_die, decl);
22244 add_linkage_name (seg_die, decl);
22246 gcc_assert (name != NULL);
22247 add_pure_or_virtual_attribute (seg_die, decl);
22248 if (DECL_ARTIFICIAL (decl))
22249 add_AT_flag (seg_die, DW_AT_artificial, 1);
22251 name = concat ("__second_sect_of_", name, NULL);
22252 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22253 fde->dw_fde_second_end, false);
22254 add_name_attribute (seg_die, name);
22255 if (want_pubnames ())
22256 add_pubname_string (name, seg_die);
22259 else
22260 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22261 false);
22264 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22266 /* We define the "frame base" as the function's CFA. This is more
22267 convenient for several reasons: (1) It's stable across the prologue
22268 and epilogue, which makes it better than just a frame pointer,
22269 (2) With dwarf3, there exists a one-byte encoding that allows us
22270 to reference the .debug_frame data by proxy, but failing that,
22271 (3) We can at least reuse the code inspection and interpretation
22272 code that determines the CFA position at various points in the
22273 function. */
22274 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22276 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22277 add_AT_loc (subr_die, DW_AT_frame_base, op);
22279 else
22281 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22282 if (list->dw_loc_next)
22283 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22284 else
22285 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22288 /* Compute a displacement from the "steady-state frame pointer" to
22289 the CFA. The former is what all stack slots and argument slots
22290 will reference in the rtl; the latter is what we've told the
22291 debugger about. We'll need to adjust all frame_base references
22292 by this displacement. */
22293 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22295 if (fun->static_chain_decl)
22297 /* DWARF requires here a location expression that computes the
22298 address of the enclosing subprogram's frame base. The machinery
22299 in tree-nested.c is supposed to store this specific address in the
22300 last field of the FRAME record. */
22301 const tree frame_type
22302 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22303 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22305 tree fb_expr
22306 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22307 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22308 fb_expr, fb_decl, NULL_TREE);
22310 add_AT_location_description (subr_die, DW_AT_static_link,
22311 loc_list_from_tree (fb_expr, 0, NULL));
22314 resolve_variable_values ();
22317 /* Generate child dies for template paramaters. */
22318 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22319 gen_generic_params_dies (decl);
22321 /* Now output descriptions of the arguments for this function. This gets
22322 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22323 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22324 `...' at the end of the formal parameter list. In order to find out if
22325 there was a trailing ellipsis or not, we must instead look at the type
22326 associated with the FUNCTION_DECL. This will be a node of type
22327 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22328 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22329 an ellipsis at the end. */
22331 /* In the case where we are describing a mere function declaration, all we
22332 need to do here (and all we *can* do here) is to describe the *types* of
22333 its formal parameters. */
22334 if (debug_info_level <= DINFO_LEVEL_TERSE)
22336 else if (declaration)
22337 gen_formal_types_die (decl, subr_die);
22338 else
22340 /* Generate DIEs to represent all known formal parameters. */
22341 tree parm = DECL_ARGUMENTS (decl);
22342 tree generic_decl = early_dwarf
22343 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22344 tree generic_decl_parm = generic_decl
22345 ? DECL_ARGUMENTS (generic_decl)
22346 : NULL;
22348 /* Now we want to walk the list of parameters of the function and
22349 emit their relevant DIEs.
22351 We consider the case of DECL being an instance of a generic function
22352 as well as it being a normal function.
22354 If DECL is an instance of a generic function we walk the
22355 parameters of the generic function declaration _and_ the parameters of
22356 DECL itself. This is useful because we want to emit specific DIEs for
22357 function parameter packs and those are declared as part of the
22358 generic function declaration. In that particular case,
22359 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22360 That DIE has children DIEs representing the set of arguments
22361 of the pack. Note that the set of pack arguments can be empty.
22362 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22363 children DIE.
22365 Otherwise, we just consider the parameters of DECL. */
22366 while (generic_decl_parm || parm)
22368 if (generic_decl_parm
22369 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22370 gen_formal_parameter_pack_die (generic_decl_parm,
22371 parm, subr_die,
22372 &parm);
22373 else if (parm && !POINTER_BOUNDS_P (parm))
22375 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22377 if (parm == DECL_ARGUMENTS (decl)
22378 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22379 && parm_die
22380 && (dwarf_version >= 3 || !dwarf_strict))
22381 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22383 parm = DECL_CHAIN (parm);
22385 else if (parm)
22386 parm = DECL_CHAIN (parm);
22388 if (generic_decl_parm)
22389 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22392 /* Decide whether we need an unspecified_parameters DIE at the end.
22393 There are 2 more cases to do this for: 1) the ansi ... declaration -
22394 this is detectable when the end of the arg list is not a
22395 void_type_node 2) an unprototyped function declaration (not a
22396 definition). This just means that we have no info about the
22397 parameters at all. */
22398 if (early_dwarf)
22400 if (prototype_p (TREE_TYPE (decl)))
22402 /* This is the prototyped case, check for.... */
22403 if (stdarg_p (TREE_TYPE (decl)))
22404 gen_unspecified_parameters_die (decl, subr_die);
22406 else if (DECL_INITIAL (decl) == NULL_TREE)
22407 gen_unspecified_parameters_die (decl, subr_die);
22411 if (subr_die != old_die)
22412 /* Add the calling convention attribute if requested. */
22413 add_calling_convention_attribute (subr_die, decl);
22415 /* Output Dwarf info for all of the stuff within the body of the function
22416 (if it has one - it may be just a declaration).
22418 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22419 a function. This BLOCK actually represents the outermost binding contour
22420 for the function, i.e. the contour in which the function's formal
22421 parameters and labels get declared. Curiously, it appears that the front
22422 end doesn't actually put the PARM_DECL nodes for the current function onto
22423 the BLOCK_VARS list for this outer scope, but are strung off of the
22424 DECL_ARGUMENTS list for the function instead.
22426 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22427 the LABEL_DECL nodes for the function however, and we output DWARF info
22428 for those in decls_for_scope. Just within the `outer_scope' there will be
22429 a BLOCK node representing the function's outermost pair of curly braces,
22430 and any blocks used for the base and member initializers of a C++
22431 constructor function. */
22432 tree outer_scope = DECL_INITIAL (decl);
22433 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22435 int call_site_note_count = 0;
22436 int tail_call_site_note_count = 0;
22438 /* Emit a DW_TAG_variable DIE for a named return value. */
22439 if (DECL_NAME (DECL_RESULT (decl)))
22440 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22442 /* The first time through decls_for_scope we will generate the
22443 DIEs for the locals. The second time, we fill in the
22444 location info. */
22445 decls_for_scope (outer_scope, subr_die);
22447 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22449 struct call_arg_loc_node *ca_loc;
22450 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22452 dw_die_ref die = NULL;
22453 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22454 rtx arg, next_arg;
22456 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22457 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22458 : NULL_RTX);
22459 arg; arg = next_arg)
22461 dw_loc_descr_ref reg, val;
22462 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22463 dw_die_ref cdie, tdie = NULL;
22465 next_arg = XEXP (arg, 1);
22466 if (REG_P (XEXP (XEXP (arg, 0), 0))
22467 && next_arg
22468 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22469 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22470 && REGNO (XEXP (XEXP (arg, 0), 0))
22471 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22472 next_arg = XEXP (next_arg, 1);
22473 if (mode == VOIDmode)
22475 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22476 if (mode == VOIDmode)
22477 mode = GET_MODE (XEXP (arg, 0));
22479 if (mode == VOIDmode || mode == BLKmode)
22480 continue;
22481 /* Get dynamic information about call target only if we
22482 have no static information: we cannot generate both
22483 DW_AT_call_origin and DW_AT_call_target
22484 attributes. */
22485 if (ca_loc->symbol_ref == NULL_RTX)
22487 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22489 tloc = XEXP (XEXP (arg, 0), 1);
22490 continue;
22492 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22493 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22495 tlocc = XEXP (XEXP (arg, 0), 1);
22496 continue;
22499 reg = NULL;
22500 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22501 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22502 VAR_INIT_STATUS_INITIALIZED);
22503 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22505 rtx mem = XEXP (XEXP (arg, 0), 0);
22506 reg = mem_loc_descriptor (XEXP (mem, 0),
22507 get_address_mode (mem),
22508 GET_MODE (mem),
22509 VAR_INIT_STATUS_INITIALIZED);
22511 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22512 == DEBUG_PARAMETER_REF)
22514 tree tdecl
22515 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22516 tdie = lookup_decl_die (tdecl);
22517 if (tdie == NULL)
22518 continue;
22520 else
22521 continue;
22522 if (reg == NULL
22523 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22524 != DEBUG_PARAMETER_REF)
22525 continue;
22526 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22527 VOIDmode,
22528 VAR_INIT_STATUS_INITIALIZED);
22529 if (val == NULL)
22530 continue;
22531 if (die == NULL)
22532 die = gen_call_site_die (decl, subr_die, ca_loc);
22533 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22534 NULL_TREE);
22535 if (reg != NULL)
22536 add_AT_loc (cdie, DW_AT_location, reg);
22537 else if (tdie != NULL)
22538 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22539 tdie);
22540 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22541 if (next_arg != XEXP (arg, 1))
22543 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22544 if (mode == VOIDmode)
22545 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22546 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22547 0), 1),
22548 mode, VOIDmode,
22549 VAR_INIT_STATUS_INITIALIZED);
22550 if (val != NULL)
22551 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22552 val);
22555 if (die == NULL
22556 && (ca_loc->symbol_ref || tloc))
22557 die = gen_call_site_die (decl, subr_die, ca_loc);
22558 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22560 dw_loc_descr_ref tval = NULL;
22562 if (tloc != NULL_RTX)
22563 tval = mem_loc_descriptor (tloc,
22564 GET_MODE (tloc) == VOIDmode
22565 ? Pmode : GET_MODE (tloc),
22566 VOIDmode,
22567 VAR_INIT_STATUS_INITIALIZED);
22568 if (tval)
22569 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22570 else if (tlocc != NULL_RTX)
22572 tval = mem_loc_descriptor (tlocc,
22573 GET_MODE (tlocc) == VOIDmode
22574 ? Pmode : GET_MODE (tlocc),
22575 VOIDmode,
22576 VAR_INIT_STATUS_INITIALIZED);
22577 if (tval)
22578 add_AT_loc (die,
22579 dwarf_AT (DW_AT_call_target_clobbered),
22580 tval);
22583 if (die != NULL)
22585 call_site_note_count++;
22586 if (ca_loc->tail_call_p)
22587 tail_call_site_note_count++;
22591 call_arg_locations = NULL;
22592 call_arg_loc_last = NULL;
22593 if (tail_call_site_count >= 0
22594 && tail_call_site_count == tail_call_site_note_count
22595 && (!dwarf_strict || dwarf_version >= 5))
22597 if (call_site_count >= 0
22598 && call_site_count == call_site_note_count)
22599 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22600 else
22601 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22603 call_site_count = -1;
22604 tail_call_site_count = -1;
22607 /* Mark used types after we have created DIEs for the functions scopes. */
22608 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22611 /* Returns a hash value for X (which really is a die_struct). */
22613 hashval_t
22614 block_die_hasher::hash (die_struct *d)
22616 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22619 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22620 as decl_id and die_parent of die_struct Y. */
22622 bool
22623 block_die_hasher::equal (die_struct *x, die_struct *y)
22625 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22628 /* Return TRUE if DECL, which may have been previously generated as
22629 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22630 true if decl (or its origin) is either an extern declaration or a
22631 class/namespace scoped declaration.
22633 The declare_in_namespace support causes us to get two DIEs for one
22634 variable, both of which are declarations. We want to avoid
22635 considering one to be a specification, so we must test for
22636 DECLARATION and DW_AT_declaration. */
22637 static inline bool
22638 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22640 return (old_die && TREE_STATIC (decl) && !declaration
22641 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22644 /* Return true if DECL is a local static. */
22646 static inline bool
22647 local_function_static (tree decl)
22649 gcc_assert (VAR_P (decl));
22650 return TREE_STATIC (decl)
22651 && DECL_CONTEXT (decl)
22652 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22655 /* Generate a DIE to represent a declared data object.
22656 Either DECL or ORIGIN must be non-null. */
22658 static void
22659 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22661 HOST_WIDE_INT off = 0;
22662 tree com_decl;
22663 tree decl_or_origin = decl ? decl : origin;
22664 tree ultimate_origin;
22665 dw_die_ref var_die;
22666 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22667 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22668 || class_or_namespace_scope_p (context_die));
22669 bool specialization_p = false;
22670 bool no_linkage_name = false;
22672 /* While C++ inline static data members have definitions inside of the
22673 class, force the first DIE to be a declaration, then let gen_member_die
22674 reparent it to the class context and call gen_variable_die again
22675 to create the outside of the class DIE for the definition. */
22676 if (!declaration
22677 && old_die == NULL
22678 && decl
22679 && DECL_CONTEXT (decl)
22680 && TYPE_P (DECL_CONTEXT (decl))
22681 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22683 declaration = true;
22684 if (dwarf_version < 5)
22685 no_linkage_name = true;
22688 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22689 if (decl || ultimate_origin)
22690 origin = ultimate_origin;
22691 com_decl = fortran_common (decl_or_origin, &off);
22693 /* Symbol in common gets emitted as a child of the common block, in the form
22694 of a data member. */
22695 if (com_decl)
22697 dw_die_ref com_die;
22698 dw_loc_list_ref loc = NULL;
22699 die_node com_die_arg;
22701 var_die = lookup_decl_die (decl_or_origin);
22702 if (var_die)
22704 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22706 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22707 if (loc)
22709 if (off)
22711 /* Optimize the common case. */
22712 if (single_element_loc_list_p (loc)
22713 && loc->expr->dw_loc_opc == DW_OP_addr
22714 && loc->expr->dw_loc_next == NULL
22715 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22716 == SYMBOL_REF)
22718 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22719 loc->expr->dw_loc_oprnd1.v.val_addr
22720 = plus_constant (GET_MODE (x), x , off);
22722 else
22723 loc_list_plus_const (loc, off);
22725 add_AT_location_description (var_die, DW_AT_location, loc);
22726 remove_AT (var_die, DW_AT_declaration);
22729 return;
22732 if (common_block_die_table == NULL)
22733 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22735 com_die_arg.decl_id = DECL_UID (com_decl);
22736 com_die_arg.die_parent = context_die;
22737 com_die = common_block_die_table->find (&com_die_arg);
22738 if (! early_dwarf)
22739 loc = loc_list_from_tree (com_decl, 2, NULL);
22740 if (com_die == NULL)
22742 const char *cnam
22743 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22744 die_node **slot;
22746 com_die = new_die (DW_TAG_common_block, context_die, decl);
22747 add_name_and_src_coords_attributes (com_die, com_decl);
22748 if (loc)
22750 add_AT_location_description (com_die, DW_AT_location, loc);
22751 /* Avoid sharing the same loc descriptor between
22752 DW_TAG_common_block and DW_TAG_variable. */
22753 loc = loc_list_from_tree (com_decl, 2, NULL);
22755 else if (DECL_EXTERNAL (decl_or_origin))
22756 add_AT_flag (com_die, DW_AT_declaration, 1);
22757 if (want_pubnames ())
22758 add_pubname_string (cnam, com_die); /* ??? needed? */
22759 com_die->decl_id = DECL_UID (com_decl);
22760 slot = common_block_die_table->find_slot (com_die, INSERT);
22761 *slot = com_die;
22763 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22765 add_AT_location_description (com_die, DW_AT_location, loc);
22766 loc = loc_list_from_tree (com_decl, 2, NULL);
22767 remove_AT (com_die, DW_AT_declaration);
22769 var_die = new_die (DW_TAG_variable, com_die, decl);
22770 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22771 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22772 decl_quals (decl_or_origin), false,
22773 context_die);
22774 add_alignment_attribute (var_die, decl);
22775 add_AT_flag (var_die, DW_AT_external, 1);
22776 if (loc)
22778 if (off)
22780 /* Optimize the common case. */
22781 if (single_element_loc_list_p (loc)
22782 && loc->expr->dw_loc_opc == DW_OP_addr
22783 && loc->expr->dw_loc_next == NULL
22784 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22786 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22787 loc->expr->dw_loc_oprnd1.v.val_addr
22788 = plus_constant (GET_MODE (x), x, off);
22790 else
22791 loc_list_plus_const (loc, off);
22793 add_AT_location_description (var_die, DW_AT_location, loc);
22795 else if (DECL_EXTERNAL (decl_or_origin))
22796 add_AT_flag (var_die, DW_AT_declaration, 1);
22797 if (decl)
22798 equate_decl_number_to_die (decl, var_die);
22799 return;
22802 if (old_die)
22804 if (declaration)
22806 /* A declaration that has been previously dumped, needs no
22807 further annotations, since it doesn't need location on
22808 the second pass. */
22809 return;
22811 else if (decl_will_get_specification_p (old_die, decl, declaration)
22812 && !get_AT (old_die, DW_AT_specification))
22814 /* Fall-thru so we can make a new variable die along with a
22815 DW_AT_specification. */
22817 else if (origin && old_die->die_parent != context_die)
22819 /* If we will be creating an inlined instance, we need a
22820 new DIE that will get annotated with
22821 DW_AT_abstract_origin. Clear things so we can get a
22822 new DIE. */
22823 gcc_assert (!DECL_ABSTRACT_P (decl));
22824 old_die = NULL;
22826 else
22828 /* If a DIE was dumped early, it still needs location info.
22829 Skip to where we fill the location bits. */
22830 var_die = old_die;
22831 goto gen_variable_die_location;
22835 /* For static data members, the declaration in the class is supposed
22836 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22837 also in DWARF2; the specification should still be DW_TAG_variable
22838 referencing the DW_TAG_member DIE. */
22839 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22840 var_die = new_die (DW_TAG_member, context_die, decl);
22841 else
22842 var_die = new_die (DW_TAG_variable, context_die, decl);
22844 if (origin != NULL)
22845 add_abstract_origin_attribute (var_die, origin);
22847 /* Loop unrolling can create multiple blocks that refer to the same
22848 static variable, so we must test for the DW_AT_declaration flag.
22850 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22851 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22852 sharing them.
22854 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22855 else if (decl_will_get_specification_p (old_die, decl, declaration))
22857 /* This is a definition of a C++ class level static. */
22858 add_AT_specification (var_die, old_die);
22859 specialization_p = true;
22860 if (DECL_NAME (decl))
22862 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22863 struct dwarf_file_data * file_index = lookup_filename (s.file);
22865 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22866 add_AT_file (var_die, DW_AT_decl_file, file_index);
22868 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22869 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22871 if (debug_column_info
22872 && s.column
22873 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22874 != (unsigned) s.column))
22875 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22877 if (old_die->die_tag == DW_TAG_member)
22878 add_linkage_name (var_die, decl);
22881 else
22882 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22884 if ((origin == NULL && !specialization_p)
22885 || (origin != NULL
22886 && !DECL_ABSTRACT_P (decl_or_origin)
22887 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22888 decl_function_context
22889 (decl_or_origin))))
22891 tree type = TREE_TYPE (decl_or_origin);
22893 if (decl_by_reference_p (decl_or_origin))
22894 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22895 context_die);
22896 else
22897 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22898 context_die);
22901 if (origin == NULL && !specialization_p)
22903 if (TREE_PUBLIC (decl))
22904 add_AT_flag (var_die, DW_AT_external, 1);
22906 if (DECL_ARTIFICIAL (decl))
22907 add_AT_flag (var_die, DW_AT_artificial, 1);
22909 add_alignment_attribute (var_die, decl);
22911 add_accessibility_attribute (var_die, decl);
22914 if (declaration)
22915 add_AT_flag (var_die, DW_AT_declaration, 1);
22917 if (decl && (DECL_ABSTRACT_P (decl)
22918 || !old_die || is_declaration_die (old_die)))
22919 equate_decl_number_to_die (decl, var_die);
22921 gen_variable_die_location:
22922 if (! declaration
22923 && (! DECL_ABSTRACT_P (decl_or_origin)
22924 /* Local static vars are shared between all clones/inlines,
22925 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22926 already set. */
22927 || (VAR_P (decl_or_origin)
22928 && TREE_STATIC (decl_or_origin)
22929 && DECL_RTL_SET_P (decl_or_origin))))
22931 if (early_dwarf)
22932 add_pubname (decl_or_origin, var_die);
22933 else
22934 add_location_or_const_value_attribute (var_die, decl_or_origin,
22935 decl == NULL);
22937 else
22938 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22940 if ((dwarf_version >= 4 || !dwarf_strict)
22941 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22942 DW_AT_const_expr) == 1
22943 && !get_AT (var_die, DW_AT_const_expr)
22944 && !specialization_p)
22945 add_AT_flag (var_die, DW_AT_const_expr, 1);
22947 if (!dwarf_strict)
22949 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22950 DW_AT_inline);
22951 if (inl != -1
22952 && !get_AT (var_die, DW_AT_inline)
22953 && !specialization_p)
22954 add_AT_unsigned (var_die, DW_AT_inline, inl);
22958 /* Generate a DIE to represent a named constant. */
22960 static void
22961 gen_const_die (tree decl, dw_die_ref context_die)
22963 dw_die_ref const_die;
22964 tree type = TREE_TYPE (decl);
22966 const_die = lookup_decl_die (decl);
22967 if (const_die)
22968 return;
22970 const_die = new_die (DW_TAG_constant, context_die, decl);
22971 equate_decl_number_to_die (decl, const_die);
22972 add_name_and_src_coords_attributes (const_die, decl);
22973 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22974 if (TREE_PUBLIC (decl))
22975 add_AT_flag (const_die, DW_AT_external, 1);
22976 if (DECL_ARTIFICIAL (decl))
22977 add_AT_flag (const_die, DW_AT_artificial, 1);
22978 tree_add_const_value_attribute_for_decl (const_die, decl);
22981 /* Generate a DIE to represent a label identifier. */
22983 static void
22984 gen_label_die (tree decl, dw_die_ref context_die)
22986 tree origin = decl_ultimate_origin (decl);
22987 dw_die_ref lbl_die = lookup_decl_die (decl);
22988 rtx insn;
22989 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22991 if (!lbl_die)
22993 lbl_die = new_die (DW_TAG_label, context_die, decl);
22994 equate_decl_number_to_die (decl, lbl_die);
22996 if (origin != NULL)
22997 add_abstract_origin_attribute (lbl_die, origin);
22998 else
22999 add_name_and_src_coords_attributes (lbl_die, decl);
23002 if (DECL_ABSTRACT_P (decl))
23003 equate_decl_number_to_die (decl, lbl_die);
23004 else if (! early_dwarf)
23006 insn = DECL_RTL_IF_SET (decl);
23008 /* Deleted labels are programmer specified labels which have been
23009 eliminated because of various optimizations. We still emit them
23010 here so that it is possible to put breakpoints on them. */
23011 if (insn
23012 && (LABEL_P (insn)
23013 || ((NOTE_P (insn)
23014 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23016 /* When optimization is enabled (via -O) some parts of the compiler
23017 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23018 represent source-level labels which were explicitly declared by
23019 the user. This really shouldn't be happening though, so catch
23020 it if it ever does happen. */
23021 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23023 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23024 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23026 else if (insn
23027 && NOTE_P (insn)
23028 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23029 && CODE_LABEL_NUMBER (insn) != -1)
23031 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23032 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23037 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23038 attributes to the DIE for a block STMT, to describe where the inlined
23039 function was called from. This is similar to add_src_coords_attributes. */
23041 static inline void
23042 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23044 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23046 if (dwarf_version >= 3 || !dwarf_strict)
23048 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23049 add_AT_unsigned (die, DW_AT_call_line, s.line);
23050 if (debug_column_info && s.column)
23051 add_AT_unsigned (die, DW_AT_call_column, s.column);
23056 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23057 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23059 static inline void
23060 add_high_low_attributes (tree stmt, dw_die_ref die)
23062 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23064 if (BLOCK_FRAGMENT_CHAIN (stmt)
23065 && (dwarf_version >= 3 || !dwarf_strict))
23067 tree chain, superblock = NULL_TREE;
23068 dw_die_ref pdie;
23069 dw_attr_node *attr = NULL;
23071 if (inlined_function_outer_scope_p (stmt))
23073 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23074 BLOCK_NUMBER (stmt));
23075 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23078 /* Optimize duplicate .debug_ranges lists or even tails of
23079 lists. If this BLOCK has same ranges as its supercontext,
23080 lookup DW_AT_ranges attribute in the supercontext (and
23081 recursively so), verify that the ranges_table contains the
23082 right values and use it instead of adding a new .debug_range. */
23083 for (chain = stmt, pdie = die;
23084 BLOCK_SAME_RANGE (chain);
23085 chain = BLOCK_SUPERCONTEXT (chain))
23087 dw_attr_node *new_attr;
23089 pdie = pdie->die_parent;
23090 if (pdie == NULL)
23091 break;
23092 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23093 break;
23094 new_attr = get_AT (pdie, DW_AT_ranges);
23095 if (new_attr == NULL
23096 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23097 break;
23098 attr = new_attr;
23099 superblock = BLOCK_SUPERCONTEXT (chain);
23101 if (attr != NULL
23102 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23103 == BLOCK_NUMBER (superblock))
23104 && BLOCK_FRAGMENT_CHAIN (superblock))
23106 unsigned long off = attr->dw_attr_val.v.val_offset;
23107 unsigned long supercnt = 0, thiscnt = 0;
23108 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23109 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23111 ++supercnt;
23112 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23113 == BLOCK_NUMBER (chain));
23115 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23116 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23117 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23118 ++thiscnt;
23119 gcc_assert (supercnt >= thiscnt);
23120 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23121 false);
23122 note_rnglist_head (off + supercnt - thiscnt);
23123 return;
23126 unsigned int offset = add_ranges (stmt, true);
23127 add_AT_range_list (die, DW_AT_ranges, offset, false);
23128 note_rnglist_head (offset);
23130 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23131 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23134 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23135 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23136 chain = BLOCK_FRAGMENT_CHAIN (chain);
23138 while (chain);
23139 add_ranges (NULL);
23141 else
23143 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23144 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23145 BLOCK_NUMBER (stmt));
23146 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23147 BLOCK_NUMBER (stmt));
23148 add_AT_low_high_pc (die, label, label_high, false);
23152 /* Generate a DIE for a lexical block. */
23154 static void
23155 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23157 dw_die_ref old_die = BLOCK_DIE (stmt);
23158 dw_die_ref stmt_die = NULL;
23159 if (!old_die)
23161 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23162 BLOCK_DIE (stmt) = stmt_die;
23165 if (BLOCK_ABSTRACT (stmt))
23167 if (old_die)
23169 /* This must have been generated early and it won't even
23170 need location information since it's a DW_AT_inline
23171 function. */
23172 if (flag_checking)
23173 for (dw_die_ref c = context_die; c; c = c->die_parent)
23174 if (c->die_tag == DW_TAG_inlined_subroutine
23175 || c->die_tag == DW_TAG_subprogram)
23177 gcc_assert (get_AT (c, DW_AT_inline));
23178 break;
23180 return;
23183 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23185 /* If this is an inlined instance, create a new lexical die for
23186 anything below to attach DW_AT_abstract_origin to. */
23187 if (old_die)
23189 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23190 BLOCK_DIE (stmt) = stmt_die;
23191 old_die = NULL;
23194 tree origin = block_ultimate_origin (stmt);
23195 if (origin != NULL_TREE && origin != stmt)
23196 add_abstract_origin_attribute (stmt_die, origin);
23199 if (old_die)
23200 stmt_die = old_die;
23202 /* A non abstract block whose blocks have already been reordered
23203 should have the instruction range for this block. If so, set the
23204 high/low attributes. */
23205 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23207 gcc_assert (stmt_die);
23208 add_high_low_attributes (stmt, stmt_die);
23211 decls_for_scope (stmt, stmt_die);
23214 /* Generate a DIE for an inlined subprogram. */
23216 static void
23217 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23219 tree decl;
23221 /* The instance of function that is effectively being inlined shall not
23222 be abstract. */
23223 gcc_assert (! BLOCK_ABSTRACT (stmt));
23225 decl = block_ultimate_origin (stmt);
23227 /* Make sure any inlined functions are known to be inlineable. */
23228 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23229 || cgraph_function_possibly_inlined_p (decl));
23231 /* Emit info for the abstract instance first, if we haven't yet. We
23232 must emit this even if the block is abstract, otherwise when we
23233 emit the block below (or elsewhere), we may end up trying to emit
23234 a die whose origin die hasn't been emitted, and crashing. */
23235 dwarf2out_abstract_function (decl);
23237 if (! BLOCK_ABSTRACT (stmt))
23239 dw_die_ref subr_die
23240 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23242 if (call_arg_locations)
23243 BLOCK_DIE (stmt) = subr_die;
23244 add_abstract_origin_attribute (subr_die, decl);
23245 if (TREE_ASM_WRITTEN (stmt))
23246 add_high_low_attributes (stmt, subr_die);
23247 add_call_src_coords_attributes (stmt, subr_die);
23249 decls_for_scope (stmt, subr_die);
23253 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23254 the comment for VLR_CONTEXT. */
23256 static void
23257 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23259 dw_die_ref decl_die;
23261 if (TREE_TYPE (decl) == error_mark_node)
23262 return;
23264 decl_die = new_die (DW_TAG_member, context_die, decl);
23265 add_name_and_src_coords_attributes (decl_die, decl);
23266 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23267 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23268 context_die);
23270 if (DECL_BIT_FIELD_TYPE (decl))
23272 add_byte_size_attribute (decl_die, decl);
23273 add_bit_size_attribute (decl_die, decl);
23274 add_bit_offset_attribute (decl_die, decl, ctx);
23277 add_alignment_attribute (decl_die, decl);
23279 /* If we have a variant part offset, then we are supposed to process a member
23280 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23281 trees. */
23282 gcc_assert (ctx->variant_part_offset == NULL_TREE
23283 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23284 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23285 add_data_member_location_attribute (decl_die, decl, ctx);
23287 if (DECL_ARTIFICIAL (decl))
23288 add_AT_flag (decl_die, DW_AT_artificial, 1);
23290 add_accessibility_attribute (decl_die, decl);
23292 /* Equate decl number to die, so that we can look up this decl later on. */
23293 equate_decl_number_to_die (decl, decl_die);
23296 /* Generate a DIE for a pointer to a member type. TYPE can be an
23297 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23298 pointer to member function. */
23300 static void
23301 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23303 if (lookup_type_die (type))
23304 return;
23306 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23307 scope_die_for (type, context_die), type);
23309 equate_type_number_to_die (type, ptr_die);
23310 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23311 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23312 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23313 context_die);
23314 add_alignment_attribute (ptr_die, type);
23316 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23317 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23319 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23320 add_AT_loc (ptr_die, DW_AT_use_location, op);
23324 static char *producer_string;
23326 /* Return a heap allocated producer string including command line options
23327 if -grecord-gcc-switches. */
23329 static char *
23330 gen_producer_string (void)
23332 size_t j;
23333 auto_vec<const char *> switches;
23334 const char *language_string = lang_hooks.name;
23335 char *producer, *tail;
23336 const char *p;
23337 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23338 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23340 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23341 switch (save_decoded_options[j].opt_index)
23343 case OPT_o:
23344 case OPT_d:
23345 case OPT_dumpbase:
23346 case OPT_dumpdir:
23347 case OPT_auxbase:
23348 case OPT_auxbase_strip:
23349 case OPT_quiet:
23350 case OPT_version:
23351 case OPT_v:
23352 case OPT_w:
23353 case OPT_L:
23354 case OPT_D:
23355 case OPT_I:
23356 case OPT_U:
23357 case OPT_SPECIAL_unknown:
23358 case OPT_SPECIAL_ignore:
23359 case OPT_SPECIAL_program_name:
23360 case OPT_SPECIAL_input_file:
23361 case OPT_grecord_gcc_switches:
23362 case OPT_gno_record_gcc_switches:
23363 case OPT__output_pch_:
23364 case OPT_fdiagnostics_show_location_:
23365 case OPT_fdiagnostics_show_option:
23366 case OPT_fdiagnostics_show_caret:
23367 case OPT_fdiagnostics_color_:
23368 case OPT_fverbose_asm:
23369 case OPT____:
23370 case OPT__sysroot_:
23371 case OPT_nostdinc:
23372 case OPT_nostdinc__:
23373 case OPT_fpreprocessed:
23374 case OPT_fltrans_output_list_:
23375 case OPT_fresolution_:
23376 case OPT_fdebug_prefix_map_:
23377 /* Ignore these. */
23378 continue;
23379 default:
23380 if (cl_options[save_decoded_options[j].opt_index].flags
23381 & CL_NO_DWARF_RECORD)
23382 continue;
23383 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23384 == '-');
23385 switch (save_decoded_options[j].canonical_option[0][1])
23387 case 'M':
23388 case 'i':
23389 case 'W':
23390 continue;
23391 case 'f':
23392 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23393 "dump", 4) == 0)
23394 continue;
23395 break;
23396 default:
23397 break;
23399 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23400 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23401 break;
23404 producer = XNEWVEC (char, plen + 1 + len + 1);
23405 tail = producer;
23406 sprintf (tail, "%s %s", language_string, version_string);
23407 tail += plen;
23409 FOR_EACH_VEC_ELT (switches, j, p)
23411 len = strlen (p);
23412 *tail = ' ';
23413 memcpy (tail + 1, p, len);
23414 tail += len + 1;
23417 *tail = '\0';
23418 return producer;
23421 /* Given a C and/or C++ language/version string return the "highest".
23422 C++ is assumed to be "higher" than C in this case. Used for merging
23423 LTO translation unit languages. */
23424 static const char *
23425 highest_c_language (const char *lang1, const char *lang2)
23427 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23428 return "GNU C++14";
23429 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23430 return "GNU C++11";
23431 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23432 return "GNU C++98";
23434 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23435 return "GNU C11";
23436 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23437 return "GNU C99";
23438 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23439 return "GNU C89";
23441 gcc_unreachable ();
23445 /* Generate the DIE for the compilation unit. */
23447 static dw_die_ref
23448 gen_compile_unit_die (const char *filename)
23450 dw_die_ref die;
23451 const char *language_string = lang_hooks.name;
23452 int language;
23454 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23456 if (filename)
23458 add_name_attribute (die, filename);
23459 /* Don't add cwd for <built-in>. */
23460 if (filename[0] != '<')
23461 add_comp_dir_attribute (die);
23464 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23466 /* If our producer is LTO try to figure out a common language to use
23467 from the global list of translation units. */
23468 if (strcmp (language_string, "GNU GIMPLE") == 0)
23470 unsigned i;
23471 tree t;
23472 const char *common_lang = NULL;
23474 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23476 if (!TRANSLATION_UNIT_LANGUAGE (t))
23477 continue;
23478 if (!common_lang)
23479 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23480 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23482 else if (strncmp (common_lang, "GNU C", 5) == 0
23483 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23484 /* Mixing C and C++ is ok, use C++ in that case. */
23485 common_lang = highest_c_language (common_lang,
23486 TRANSLATION_UNIT_LANGUAGE (t));
23487 else
23489 /* Fall back to C. */
23490 common_lang = NULL;
23491 break;
23495 if (common_lang)
23496 language_string = common_lang;
23499 language = DW_LANG_C;
23500 if (strncmp (language_string, "GNU C", 5) == 0
23501 && ISDIGIT (language_string[5]))
23503 language = DW_LANG_C89;
23504 if (dwarf_version >= 3 || !dwarf_strict)
23506 if (strcmp (language_string, "GNU C89") != 0)
23507 language = DW_LANG_C99;
23509 if (dwarf_version >= 5 /* || !dwarf_strict */)
23510 if (strcmp (language_string, "GNU C11") == 0)
23511 language = DW_LANG_C11;
23514 else if (strncmp (language_string, "GNU C++", 7) == 0)
23516 language = DW_LANG_C_plus_plus;
23517 if (dwarf_version >= 5 /* || !dwarf_strict */)
23519 if (strcmp (language_string, "GNU C++11") == 0)
23520 language = DW_LANG_C_plus_plus_11;
23521 else if (strcmp (language_string, "GNU C++14") == 0)
23522 language = DW_LANG_C_plus_plus_14;
23525 else if (strcmp (language_string, "GNU F77") == 0)
23526 language = DW_LANG_Fortran77;
23527 else if (dwarf_version >= 3 || !dwarf_strict)
23529 if (strcmp (language_string, "GNU Ada") == 0)
23530 language = DW_LANG_Ada95;
23531 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23533 language = DW_LANG_Fortran95;
23534 if (dwarf_version >= 5 /* || !dwarf_strict */)
23536 if (strcmp (language_string, "GNU Fortran2003") == 0)
23537 language = DW_LANG_Fortran03;
23538 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23539 language = DW_LANG_Fortran08;
23542 else if (strcmp (language_string, "GNU Objective-C") == 0)
23543 language = DW_LANG_ObjC;
23544 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23545 language = DW_LANG_ObjC_plus_plus;
23546 else if (dwarf_version >= 5 || !dwarf_strict)
23548 if (strcmp (language_string, "GNU Go") == 0)
23549 language = DW_LANG_Go;
23552 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23553 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23554 language = DW_LANG_Fortran90;
23556 add_AT_unsigned (die, DW_AT_language, language);
23558 switch (language)
23560 case DW_LANG_Fortran77:
23561 case DW_LANG_Fortran90:
23562 case DW_LANG_Fortran95:
23563 case DW_LANG_Fortran03:
23564 case DW_LANG_Fortran08:
23565 /* Fortran has case insensitive identifiers and the front-end
23566 lowercases everything. */
23567 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23568 break;
23569 default:
23570 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23571 break;
23573 return die;
23576 /* Generate the DIE for a base class. */
23578 static void
23579 gen_inheritance_die (tree binfo, tree access, tree type,
23580 dw_die_ref context_die)
23582 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23583 struct vlr_context ctx = { type, NULL };
23585 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23586 context_die);
23587 add_data_member_location_attribute (die, binfo, &ctx);
23589 if (BINFO_VIRTUAL_P (binfo))
23590 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23592 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23593 children, otherwise the default is DW_ACCESS_public. In DWARF2
23594 the default has always been DW_ACCESS_private. */
23595 if (access == access_public_node)
23597 if (dwarf_version == 2
23598 || context_die->die_tag == DW_TAG_class_type)
23599 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23601 else if (access == access_protected_node)
23602 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23603 else if (dwarf_version > 2
23604 && context_die->die_tag != DW_TAG_class_type)
23605 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23608 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23609 structure. */
23610 static bool
23611 is_variant_part (tree decl)
23613 return (TREE_CODE (decl) == FIELD_DECL
23614 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23617 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23618 return the FIELD_DECL. Return NULL_TREE otherwise. */
23620 static tree
23621 analyze_discr_in_predicate (tree operand, tree struct_type)
23623 bool continue_stripping = true;
23624 while (continue_stripping)
23625 switch (TREE_CODE (operand))
23627 CASE_CONVERT:
23628 operand = TREE_OPERAND (operand, 0);
23629 break;
23630 default:
23631 continue_stripping = false;
23632 break;
23635 /* Match field access to members of struct_type only. */
23636 if (TREE_CODE (operand) == COMPONENT_REF
23637 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23638 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23639 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23640 return TREE_OPERAND (operand, 1);
23641 else
23642 return NULL_TREE;
23645 /* Check that SRC is a constant integer that can be represented as a native
23646 integer constant (either signed or unsigned). If so, store it into DEST and
23647 return true. Return false otherwise. */
23649 static bool
23650 get_discr_value (tree src, dw_discr_value *dest)
23652 tree discr_type = TREE_TYPE (src);
23654 if (lang_hooks.types.get_debug_type)
23656 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23657 if (debug_type != NULL)
23658 discr_type = debug_type;
23661 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23662 return false;
23664 /* Signedness can vary between the original type and the debug type. This
23665 can happen for character types in Ada for instance: the character type
23666 used for code generation can be signed, to be compatible with the C one,
23667 but from a debugger point of view, it must be unsigned. */
23668 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23669 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23671 if (is_orig_unsigned != is_debug_unsigned)
23672 src = fold_convert (discr_type, src);
23674 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23675 return false;
23677 dest->pos = is_debug_unsigned;
23678 if (is_debug_unsigned)
23679 dest->v.uval = tree_to_uhwi (src);
23680 else
23681 dest->v.sval = tree_to_shwi (src);
23683 return true;
23686 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23687 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23688 store NULL_TREE in DISCR_DECL. Otherwise:
23690 - store the discriminant field in STRUCT_TYPE that controls the variant
23691 part to *DISCR_DECL
23693 - put in *DISCR_LISTS_P an array where for each variant, the item
23694 represents the corresponding matching list of discriminant values.
23696 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23697 the above array.
23699 Note that when the array is allocated (i.e. when the analysis is
23700 successful), it is up to the caller to free the array. */
23702 static void
23703 analyze_variants_discr (tree variant_part_decl,
23704 tree struct_type,
23705 tree *discr_decl,
23706 dw_discr_list_ref **discr_lists_p,
23707 unsigned *discr_lists_length)
23709 tree variant_part_type = TREE_TYPE (variant_part_decl);
23710 tree variant;
23711 dw_discr_list_ref *discr_lists;
23712 unsigned i;
23714 /* Compute how many variants there are in this variant part. */
23715 *discr_lists_length = 0;
23716 for (variant = TYPE_FIELDS (variant_part_type);
23717 variant != NULL_TREE;
23718 variant = DECL_CHAIN (variant))
23719 ++*discr_lists_length;
23721 *discr_decl = NULL_TREE;
23722 *discr_lists_p
23723 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23724 sizeof (**discr_lists_p));
23725 discr_lists = *discr_lists_p;
23727 /* And then analyze all variants to extract discriminant information for all
23728 of them. This analysis is conservative: as soon as we detect something we
23729 do not support, abort everything and pretend we found nothing. */
23730 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23731 variant != NULL_TREE;
23732 variant = DECL_CHAIN (variant), ++i)
23734 tree match_expr = DECL_QUALIFIER (variant);
23736 /* Now, try to analyze the predicate and deduce a discriminant for
23737 it. */
23738 if (match_expr == boolean_true_node)
23739 /* Typically happens for the default variant: it matches all cases that
23740 previous variants rejected. Don't output any matching value for
23741 this one. */
23742 continue;
23744 /* The following loop tries to iterate over each discriminant
23745 possibility: single values or ranges. */
23746 while (match_expr != NULL_TREE)
23748 tree next_round_match_expr;
23749 tree candidate_discr = NULL_TREE;
23750 dw_discr_list_ref new_node = NULL;
23752 /* Possibilities are matched one after the other by nested
23753 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23754 continue with the rest at next iteration. */
23755 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23757 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23758 match_expr = TREE_OPERAND (match_expr, 1);
23760 else
23761 next_round_match_expr = NULL_TREE;
23763 if (match_expr == boolean_false_node)
23764 /* This sub-expression matches nothing: just wait for the next
23765 one. */
23768 else if (TREE_CODE (match_expr) == EQ_EXPR)
23770 /* We are matching: <discr_field> == <integer_cst>
23771 This sub-expression matches a single value. */
23772 tree integer_cst = TREE_OPERAND (match_expr, 1);
23774 candidate_discr
23775 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23776 struct_type);
23778 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23779 if (!get_discr_value (integer_cst,
23780 &new_node->dw_discr_lower_bound))
23781 goto abort;
23782 new_node->dw_discr_range = false;
23785 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23787 /* We are matching:
23788 <discr_field> > <integer_cst>
23789 && <discr_field> < <integer_cst>.
23790 This sub-expression matches the range of values between the
23791 two matched integer constants. Note that comparisons can be
23792 inclusive or exclusive. */
23793 tree candidate_discr_1, candidate_discr_2;
23794 tree lower_cst, upper_cst;
23795 bool lower_cst_included, upper_cst_included;
23796 tree lower_op = TREE_OPERAND (match_expr, 0);
23797 tree upper_op = TREE_OPERAND (match_expr, 1);
23799 /* When the comparison is exclusive, the integer constant is not
23800 the discriminant range bound we are looking for: we will have
23801 to increment or decrement it. */
23802 if (TREE_CODE (lower_op) == GE_EXPR)
23803 lower_cst_included = true;
23804 else if (TREE_CODE (lower_op) == GT_EXPR)
23805 lower_cst_included = false;
23806 else
23807 goto abort;
23809 if (TREE_CODE (upper_op) == LE_EXPR)
23810 upper_cst_included = true;
23811 else if (TREE_CODE (upper_op) == LT_EXPR)
23812 upper_cst_included = false;
23813 else
23814 goto abort;
23816 /* Extract the discriminant from the first operand and check it
23817 is consistant with the same analysis in the second
23818 operand. */
23819 candidate_discr_1
23820 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23821 struct_type);
23822 candidate_discr_2
23823 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23824 struct_type);
23825 if (candidate_discr_1 == candidate_discr_2)
23826 candidate_discr = candidate_discr_1;
23827 else
23828 goto abort;
23830 /* Extract bounds from both. */
23831 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23832 lower_cst = TREE_OPERAND (lower_op, 1);
23833 upper_cst = TREE_OPERAND (upper_op, 1);
23835 if (!lower_cst_included)
23836 lower_cst
23837 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23838 build_int_cst (TREE_TYPE (lower_cst), 1));
23839 if (!upper_cst_included)
23840 upper_cst
23841 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23842 build_int_cst (TREE_TYPE (upper_cst), 1));
23844 if (!get_discr_value (lower_cst,
23845 &new_node->dw_discr_lower_bound)
23846 || !get_discr_value (upper_cst,
23847 &new_node->dw_discr_upper_bound))
23848 goto abort;
23850 new_node->dw_discr_range = true;
23853 else
23854 /* Unsupported sub-expression: we cannot determine the set of
23855 matching discriminant values. Abort everything. */
23856 goto abort;
23858 /* If the discriminant info is not consistant with what we saw so
23859 far, consider the analysis failed and abort everything. */
23860 if (candidate_discr == NULL_TREE
23861 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23862 goto abort;
23863 else
23864 *discr_decl = candidate_discr;
23866 if (new_node != NULL)
23868 new_node->dw_discr_next = discr_lists[i];
23869 discr_lists[i] = new_node;
23871 match_expr = next_round_match_expr;
23875 /* If we reach this point, we could match everything we were interested
23876 in. */
23877 return;
23879 abort:
23880 /* Clean all data structure and return no result. */
23881 free (*discr_lists_p);
23882 *discr_lists_p = NULL;
23883 *discr_decl = NULL_TREE;
23886 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23887 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23888 under CONTEXT_DIE.
23890 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23891 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23892 this type, which are record types, represent the available variants and each
23893 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23894 values are inferred from these attributes.
23896 In trees, the offsets for the fields inside these sub-records are relative
23897 to the variant part itself, whereas the corresponding DIEs should have
23898 offset attributes that are relative to the embedding record base address.
23899 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23900 must be an expression that computes the offset of the variant part to
23901 describe in DWARF. */
23903 static void
23904 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23905 dw_die_ref context_die)
23907 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23908 tree variant_part_offset = vlr_ctx->variant_part_offset;
23909 struct loc_descr_context ctx = {
23910 vlr_ctx->struct_type, /* context_type */
23911 NULL_TREE, /* base_decl */
23912 NULL, /* dpi */
23913 false, /* placeholder_arg */
23914 false /* placeholder_seen */
23917 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23918 NULL_TREE if there is no such field. */
23919 tree discr_decl = NULL_TREE;
23920 dw_discr_list_ref *discr_lists;
23921 unsigned discr_lists_length = 0;
23922 unsigned i;
23924 dw_die_ref dwarf_proc_die = NULL;
23925 dw_die_ref variant_part_die
23926 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23928 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23930 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23931 &discr_decl, &discr_lists, &discr_lists_length);
23933 if (discr_decl != NULL_TREE)
23935 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23937 if (discr_die)
23938 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23939 else
23940 /* We have no DIE for the discriminant, so just discard all
23941 discrimimant information in the output. */
23942 discr_decl = NULL_TREE;
23945 /* If the offset for this variant part is more complex than a constant,
23946 create a DWARF procedure for it so that we will not have to generate DWARF
23947 expressions for it for each member. */
23948 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23949 && (dwarf_version >= 3 || !dwarf_strict))
23951 const tree dwarf_proc_fndecl
23952 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23953 build_function_type (TREE_TYPE (variant_part_offset),
23954 NULL_TREE));
23955 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23956 const dw_loc_descr_ref dwarf_proc_body
23957 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23959 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23960 dwarf_proc_fndecl, context_die);
23961 if (dwarf_proc_die != NULL)
23962 variant_part_offset = dwarf_proc_call;
23965 /* Output DIEs for all variants. */
23966 i = 0;
23967 for (tree variant = TYPE_FIELDS (variant_part_type);
23968 variant != NULL_TREE;
23969 variant = DECL_CHAIN (variant), ++i)
23971 tree variant_type = TREE_TYPE (variant);
23972 dw_die_ref variant_die;
23974 /* All variants (i.e. members of a variant part) are supposed to be
23975 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23976 under these records. */
23977 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23979 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23980 equate_decl_number_to_die (variant, variant_die);
23982 /* Output discriminant values this variant matches, if any. */
23983 if (discr_decl == NULL || discr_lists[i] == NULL)
23984 /* In the case we have discriminant information at all, this is
23985 probably the default variant: as the standard says, don't
23986 output any discriminant value/list attribute. */
23988 else if (discr_lists[i]->dw_discr_next == NULL
23989 && !discr_lists[i]->dw_discr_range)
23990 /* If there is only one accepted value, don't bother outputting a
23991 list. */
23992 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23993 else
23994 add_discr_list (variant_die, discr_lists[i]);
23996 for (tree member = TYPE_FIELDS (variant_type);
23997 member != NULL_TREE;
23998 member = DECL_CHAIN (member))
24000 struct vlr_context vlr_sub_ctx = {
24001 vlr_ctx->struct_type, /* struct_type */
24002 NULL /* variant_part_offset */
24004 if (is_variant_part (member))
24006 /* All offsets for fields inside variant parts are relative to
24007 the top-level embedding RECORD_TYPE's base address. On the
24008 other hand, offsets in GCC's types are relative to the
24009 nested-most variant part. So we have to sum offsets each time
24010 we recurse. */
24012 vlr_sub_ctx.variant_part_offset
24013 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24014 variant_part_offset, byte_position (member));
24015 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24017 else
24019 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24020 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24025 free (discr_lists);
24028 /* Generate a DIE for a class member. */
24030 static void
24031 gen_member_die (tree type, dw_die_ref context_die)
24033 tree member;
24034 tree binfo = TYPE_BINFO (type);
24035 dw_die_ref child;
24037 /* If this is not an incomplete type, output descriptions of each of its
24038 members. Note that as we output the DIEs necessary to represent the
24039 members of this record or union type, we will also be trying to output
24040 DIEs to represent the *types* of those members. However the `type'
24041 function (above) will specifically avoid generating type DIEs for member
24042 types *within* the list of member DIEs for this (containing) type except
24043 for those types (of members) which are explicitly marked as also being
24044 members of this (containing) type themselves. The g++ front- end can
24045 force any given type to be treated as a member of some other (containing)
24046 type by setting the TYPE_CONTEXT of the given (member) type to point to
24047 the TREE node representing the appropriate (containing) type. */
24049 /* First output info about the base classes. */
24050 if (binfo)
24052 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24053 int i;
24054 tree base;
24056 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24057 gen_inheritance_die (base,
24058 (accesses ? (*accesses)[i] : access_public_node),
24059 type,
24060 context_die);
24063 /* Now output info about the data members and type members. */
24064 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24066 struct vlr_context vlr_ctx = { type, NULL_TREE };
24067 bool static_inline_p
24068 = (TREE_STATIC (member)
24069 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24070 != -1));
24072 /* If we thought we were generating minimal debug info for TYPE
24073 and then changed our minds, some of the member declarations
24074 may have already been defined. Don't define them again, but
24075 do put them in the right order. */
24077 child = lookup_decl_die (member);
24078 if (child)
24080 /* Handle inline static data members, which only have in-class
24081 declarations. */
24082 dw_die_ref ref = NULL;
24083 if (child->die_tag == DW_TAG_variable
24084 && child->die_parent == comp_unit_die ())
24086 ref = get_AT_ref (child, DW_AT_specification);
24087 /* For C++17 inline static data members followed by redundant
24088 out of class redeclaration, we might get here with
24089 child being the DIE created for the out of class
24090 redeclaration and with its DW_AT_specification being
24091 the DIE created for in-class definition. We want to
24092 reparent the latter, and don't want to create another
24093 DIE with DW_AT_specification in that case, because
24094 we already have one. */
24095 if (ref
24096 && static_inline_p
24097 && ref->die_tag == DW_TAG_variable
24098 && ref->die_parent == comp_unit_die ()
24099 && get_AT (ref, DW_AT_specification) == NULL)
24101 child = ref;
24102 ref = NULL;
24103 static_inline_p = false;
24106 if (child->die_tag == DW_TAG_variable
24107 && child->die_parent == comp_unit_die ()
24108 && ref == NULL)
24110 reparent_child (child, context_die);
24111 if (dwarf_version < 5)
24112 child->die_tag = DW_TAG_member;
24114 else
24115 splice_child_die (context_die, child);
24118 /* Do not generate standard DWARF for variant parts if we are generating
24119 the corresponding GNAT encodings: DIEs generated for both would
24120 conflict in our mappings. */
24121 else if (is_variant_part (member)
24122 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24124 vlr_ctx.variant_part_offset = byte_position (member);
24125 gen_variant_part (member, &vlr_ctx, context_die);
24127 else
24129 vlr_ctx.variant_part_offset = NULL_TREE;
24130 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24133 /* For C++ inline static data members emit immediately a DW_TAG_variable
24134 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24135 DW_AT_specification. */
24136 if (static_inline_p)
24138 int old_extern = DECL_EXTERNAL (member);
24139 DECL_EXTERNAL (member) = 0;
24140 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24141 DECL_EXTERNAL (member) = old_extern;
24145 /* We do not keep type methods in type variants. */
24146 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24147 /* Now output info about the function members (if any). */
24148 if (TYPE_METHODS (type) != error_mark_node)
24149 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24151 /* Don't include clones in the member list. */
24152 if (DECL_ABSTRACT_ORIGIN (member))
24153 continue;
24155 child = lookup_decl_die (member);
24156 if (child)
24157 splice_child_die (context_die, child);
24158 else
24159 gen_decl_die (member, NULL, NULL, context_die);
24163 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24164 is set, we pretend that the type was never defined, so we only get the
24165 member DIEs needed by later specification DIEs. */
24167 static void
24168 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24169 enum debug_info_usage usage)
24171 if (TREE_ASM_WRITTEN (type))
24173 /* Fill in the bound of variable-length fields in late dwarf if
24174 still incomplete. */
24175 if (!early_dwarf && variably_modified_type_p (type, NULL))
24176 for (tree member = TYPE_FIELDS (type);
24177 member;
24178 member = DECL_CHAIN (member))
24179 fill_variable_array_bounds (TREE_TYPE (member));
24180 return;
24183 dw_die_ref type_die = lookup_type_die (type);
24184 dw_die_ref scope_die = 0;
24185 int nested = 0;
24186 int complete = (TYPE_SIZE (type)
24187 && (! TYPE_STUB_DECL (type)
24188 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24189 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24190 complete = complete && should_emit_struct_debug (type, usage);
24192 if (type_die && ! complete)
24193 return;
24195 if (TYPE_CONTEXT (type) != NULL_TREE
24196 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24197 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24198 nested = 1;
24200 scope_die = scope_die_for (type, context_die);
24202 /* Generate child dies for template paramaters. */
24203 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24204 schedule_generic_params_dies_gen (type);
24206 if (! type_die || (nested && is_cu_die (scope_die)))
24207 /* First occurrence of type or toplevel definition of nested class. */
24209 dw_die_ref old_die = type_die;
24211 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24212 ? record_type_tag (type) : DW_TAG_union_type,
24213 scope_die, type);
24214 equate_type_number_to_die (type, type_die);
24215 if (old_die)
24216 add_AT_specification (type_die, old_die);
24217 else
24218 add_name_attribute (type_die, type_tag (type));
24220 else
24221 remove_AT (type_die, DW_AT_declaration);
24223 /* If this type has been completed, then give it a byte_size attribute and
24224 then give a list of members. */
24225 if (complete && !ns_decl)
24227 /* Prevent infinite recursion in cases where the type of some member of
24228 this type is expressed in terms of this type itself. */
24229 TREE_ASM_WRITTEN (type) = 1;
24230 add_byte_size_attribute (type_die, type);
24231 add_alignment_attribute (type_die, type);
24232 if (TYPE_STUB_DECL (type) != NULL_TREE)
24234 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24235 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24238 /* If the first reference to this type was as the return type of an
24239 inline function, then it may not have a parent. Fix this now. */
24240 if (type_die->die_parent == NULL)
24241 add_child_die (scope_die, type_die);
24243 push_decl_scope (type);
24244 gen_member_die (type, type_die);
24245 pop_decl_scope ();
24247 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24248 if (TYPE_ARTIFICIAL (type))
24249 add_AT_flag (type_die, DW_AT_artificial, 1);
24251 /* GNU extension: Record what type our vtable lives in. */
24252 if (TYPE_VFIELD (type))
24254 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24256 gen_type_die (vtype, context_die);
24257 add_AT_die_ref (type_die, DW_AT_containing_type,
24258 lookup_type_die (vtype));
24261 else
24263 add_AT_flag (type_die, DW_AT_declaration, 1);
24265 /* We don't need to do this for function-local types. */
24266 if (TYPE_STUB_DECL (type)
24267 && ! decl_function_context (TYPE_STUB_DECL (type)))
24268 vec_safe_push (incomplete_types, type);
24271 if (get_AT (type_die, DW_AT_name))
24272 add_pubtype (type, type_die);
24275 /* Generate a DIE for a subroutine _type_. */
24277 static void
24278 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24280 tree return_type = TREE_TYPE (type);
24281 dw_die_ref subr_die
24282 = new_die (DW_TAG_subroutine_type,
24283 scope_die_for (type, context_die), type);
24285 equate_type_number_to_die (type, subr_die);
24286 add_prototyped_attribute (subr_die, type);
24287 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24288 context_die);
24289 add_alignment_attribute (subr_die, type);
24290 gen_formal_types_die (type, subr_die);
24292 if (get_AT (subr_die, DW_AT_name))
24293 add_pubtype (type, subr_die);
24294 if ((dwarf_version >= 5 || !dwarf_strict)
24295 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24296 add_AT_flag (subr_die, DW_AT_reference, 1);
24297 if ((dwarf_version >= 5 || !dwarf_strict)
24298 && lang_hooks.types.type_dwarf_attribute (type,
24299 DW_AT_rvalue_reference) != -1)
24300 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24303 /* Generate a DIE for a type definition. */
24305 static void
24306 gen_typedef_die (tree decl, dw_die_ref context_die)
24308 dw_die_ref type_die;
24309 tree origin;
24311 if (TREE_ASM_WRITTEN (decl))
24313 if (DECL_ORIGINAL_TYPE (decl))
24314 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24315 return;
24318 TREE_ASM_WRITTEN (decl) = 1;
24319 type_die = new_die (DW_TAG_typedef, context_die, decl);
24320 origin = decl_ultimate_origin (decl);
24321 if (origin != NULL)
24322 add_abstract_origin_attribute (type_die, origin);
24323 else
24325 tree type = TREE_TYPE (decl);
24327 if (type == error_mark_node)
24328 return;
24330 add_name_and_src_coords_attributes (type_die, decl);
24331 if (DECL_ORIGINAL_TYPE (decl))
24333 type = DECL_ORIGINAL_TYPE (decl);
24335 if (type == error_mark_node)
24336 return;
24338 gcc_assert (type != TREE_TYPE (decl));
24339 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24341 else
24343 if (is_naming_typedef_decl (TYPE_NAME (type)))
24345 /* Here, we are in the case of decl being a typedef naming
24346 an anonymous type, e.g:
24347 typedef struct {...} foo;
24348 In that case TREE_TYPE (decl) is not a typedef variant
24349 type and TYPE_NAME of the anonymous type is set to the
24350 TYPE_DECL of the typedef. This construct is emitted by
24351 the C++ FE.
24353 TYPE is the anonymous struct named by the typedef
24354 DECL. As we need the DW_AT_type attribute of the
24355 DW_TAG_typedef to point to the DIE of TYPE, let's
24356 generate that DIE right away. add_type_attribute
24357 called below will then pick (via lookup_type_die) that
24358 anonymous struct DIE. */
24359 if (!TREE_ASM_WRITTEN (type))
24360 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24362 /* This is a GNU Extension. We are adding a
24363 DW_AT_linkage_name attribute to the DIE of the
24364 anonymous struct TYPE. The value of that attribute
24365 is the name of the typedef decl naming the anonymous
24366 struct. This greatly eases the work of consumers of
24367 this debug info. */
24368 add_linkage_name_raw (lookup_type_die (type), decl);
24372 add_type_attribute (type_die, type, decl_quals (decl), false,
24373 context_die);
24375 if (is_naming_typedef_decl (decl))
24376 /* We want that all subsequent calls to lookup_type_die with
24377 TYPE in argument yield the DW_TAG_typedef we have just
24378 created. */
24379 equate_type_number_to_die (type, type_die);
24381 type = TREE_TYPE (decl);
24383 add_alignment_attribute (type_die, type);
24385 add_accessibility_attribute (type_die, decl);
24388 if (DECL_ABSTRACT_P (decl))
24389 equate_decl_number_to_die (decl, type_die);
24391 if (get_AT (type_die, DW_AT_name))
24392 add_pubtype (decl, type_die);
24395 /* Generate a DIE for a struct, class, enum or union type. */
24397 static void
24398 gen_tagged_type_die (tree type,
24399 dw_die_ref context_die,
24400 enum debug_info_usage usage)
24402 int need_pop;
24404 if (type == NULL_TREE
24405 || !is_tagged_type (type))
24406 return;
24408 if (TREE_ASM_WRITTEN (type))
24409 need_pop = 0;
24410 /* If this is a nested type whose containing class hasn't been written
24411 out yet, writing it out will cover this one, too. This does not apply
24412 to instantiations of member class templates; they need to be added to
24413 the containing class as they are generated. FIXME: This hurts the
24414 idea of combining type decls from multiple TUs, since we can't predict
24415 what set of template instantiations we'll get. */
24416 else if (TYPE_CONTEXT (type)
24417 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24418 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24420 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24422 if (TREE_ASM_WRITTEN (type))
24423 return;
24425 /* If that failed, attach ourselves to the stub. */
24426 push_decl_scope (TYPE_CONTEXT (type));
24427 context_die = lookup_type_die (TYPE_CONTEXT (type));
24428 need_pop = 1;
24430 else if (TYPE_CONTEXT (type) != NULL_TREE
24431 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24433 /* If this type is local to a function that hasn't been written
24434 out yet, use a NULL context for now; it will be fixed up in
24435 decls_for_scope. */
24436 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24437 /* A declaration DIE doesn't count; nested types need to go in the
24438 specification. */
24439 if (context_die && is_declaration_die (context_die))
24440 context_die = NULL;
24441 need_pop = 0;
24443 else
24445 context_die = declare_in_namespace (type, context_die);
24446 need_pop = 0;
24449 if (TREE_CODE (type) == ENUMERAL_TYPE)
24451 /* This might have been written out by the call to
24452 declare_in_namespace. */
24453 if (!TREE_ASM_WRITTEN (type))
24454 gen_enumeration_type_die (type, context_die);
24456 else
24457 gen_struct_or_union_type_die (type, context_die, usage);
24459 if (need_pop)
24460 pop_decl_scope ();
24462 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24463 it up if it is ever completed. gen_*_type_die will set it for us
24464 when appropriate. */
24467 /* Generate a type description DIE. */
24469 static void
24470 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24471 enum debug_info_usage usage)
24473 struct array_descr_info info;
24475 if (type == NULL_TREE || type == error_mark_node)
24476 return;
24478 if (flag_checking && type)
24479 verify_type (type);
24481 if (TYPE_NAME (type) != NULL_TREE
24482 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24483 && is_redundant_typedef (TYPE_NAME (type))
24484 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24485 /* The DECL of this type is a typedef we don't want to emit debug
24486 info for but we want debug info for its underlying typedef.
24487 This can happen for e.g, the injected-class-name of a C++
24488 type. */
24489 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24491 /* If TYPE is a typedef type variant, let's generate debug info
24492 for the parent typedef which TYPE is a type of. */
24493 if (typedef_variant_p (type))
24495 if (TREE_ASM_WRITTEN (type))
24496 return;
24498 /* Prevent broken recursion; we can't hand off to the same type. */
24499 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
24501 /* Give typedefs the right scope. */
24502 context_die = scope_die_for (type, context_die);
24504 TREE_ASM_WRITTEN (type) = 1;
24506 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24507 return;
24510 /* If type is an anonymous tagged type named by a typedef, let's
24511 generate debug info for the typedef. */
24512 if (is_naming_typedef_decl (TYPE_NAME (type)))
24514 /* Use the DIE of the containing namespace as the parent DIE of
24515 the type description DIE we want to generate. */
24516 if (DECL_CONTEXT (TYPE_NAME (type))
24517 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24518 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24520 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24521 return;
24524 if (lang_hooks.types.get_debug_type)
24526 tree debug_type = lang_hooks.types.get_debug_type (type);
24528 if (debug_type != NULL_TREE && debug_type != type)
24530 gen_type_die_with_usage (debug_type, context_die, usage);
24531 return;
24535 /* We are going to output a DIE to represent the unqualified version
24536 of this type (i.e. without any const or volatile qualifiers) so
24537 get the main variant (i.e. the unqualified version) of this type
24538 now. (Vectors and arrays are special because the debugging info is in the
24539 cloned type itself. Similarly function/method types can contain extra
24540 ref-qualification). */
24541 if (TREE_CODE (type) == FUNCTION_TYPE
24542 || TREE_CODE (type) == METHOD_TYPE)
24544 /* For function/method types, can't use type_main_variant here,
24545 because that can have different ref-qualifiers for C++,
24546 but try to canonicalize. */
24547 tree main = TYPE_MAIN_VARIANT (type);
24548 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24549 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24550 && check_base_type (t, main)
24551 && check_lang_type (t, type))
24553 type = t;
24554 break;
24557 else if (TREE_CODE (type) != VECTOR_TYPE
24558 && TREE_CODE (type) != ARRAY_TYPE)
24559 type = type_main_variant (type);
24561 /* If this is an array type with hidden descriptor, handle it first. */
24562 if (!TREE_ASM_WRITTEN (type)
24563 && lang_hooks.types.get_array_descr_info)
24565 memset (&info, 0, sizeof (info));
24566 if (lang_hooks.types.get_array_descr_info (type, &info))
24568 /* Fortran sometimes emits array types with no dimension. */
24569 gcc_assert (info.ndimensions >= 0
24570 && (info.ndimensions
24571 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24572 gen_descr_array_type_die (type, &info, context_die);
24573 TREE_ASM_WRITTEN (type) = 1;
24574 return;
24578 if (TREE_ASM_WRITTEN (type))
24580 /* Variable-length types may be incomplete even if
24581 TREE_ASM_WRITTEN. For such types, fall through to
24582 gen_array_type_die() and possibly fill in
24583 DW_AT_{upper,lower}_bound attributes. */
24584 if ((TREE_CODE (type) != ARRAY_TYPE
24585 && TREE_CODE (type) != RECORD_TYPE
24586 && TREE_CODE (type) != UNION_TYPE
24587 && TREE_CODE (type) != QUAL_UNION_TYPE)
24588 || !variably_modified_type_p (type, NULL))
24589 return;
24592 switch (TREE_CODE (type))
24594 case ERROR_MARK:
24595 break;
24597 case POINTER_TYPE:
24598 case REFERENCE_TYPE:
24599 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24600 ensures that the gen_type_die recursion will terminate even if the
24601 type is recursive. Recursive types are possible in Ada. */
24602 /* ??? We could perhaps do this for all types before the switch
24603 statement. */
24604 TREE_ASM_WRITTEN (type) = 1;
24606 /* For these types, all that is required is that we output a DIE (or a
24607 set of DIEs) to represent the "basis" type. */
24608 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24609 DINFO_USAGE_IND_USE);
24610 break;
24612 case OFFSET_TYPE:
24613 /* This code is used for C++ pointer-to-data-member types.
24614 Output a description of the relevant class type. */
24615 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24616 DINFO_USAGE_IND_USE);
24618 /* Output a description of the type of the object pointed to. */
24619 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24620 DINFO_USAGE_IND_USE);
24622 /* Now output a DIE to represent this pointer-to-data-member type
24623 itself. */
24624 gen_ptr_to_mbr_type_die (type, context_die);
24625 break;
24627 case FUNCTION_TYPE:
24628 /* Force out return type (in case it wasn't forced out already). */
24629 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24630 DINFO_USAGE_DIR_USE);
24631 gen_subroutine_type_die (type, context_die);
24632 break;
24634 case METHOD_TYPE:
24635 /* Force out return type (in case it wasn't forced out already). */
24636 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24637 DINFO_USAGE_DIR_USE);
24638 gen_subroutine_type_die (type, context_die);
24639 break;
24641 case ARRAY_TYPE:
24642 case VECTOR_TYPE:
24643 gen_array_type_die (type, context_die);
24644 break;
24646 case ENUMERAL_TYPE:
24647 case RECORD_TYPE:
24648 case UNION_TYPE:
24649 case QUAL_UNION_TYPE:
24650 gen_tagged_type_die (type, context_die, usage);
24651 return;
24653 case VOID_TYPE:
24654 case INTEGER_TYPE:
24655 case REAL_TYPE:
24656 case FIXED_POINT_TYPE:
24657 case COMPLEX_TYPE:
24658 case BOOLEAN_TYPE:
24659 case POINTER_BOUNDS_TYPE:
24660 /* No DIEs needed for fundamental types. */
24661 break;
24663 case NULLPTR_TYPE:
24664 case LANG_TYPE:
24665 /* Just use DW_TAG_unspecified_type. */
24667 dw_die_ref type_die = lookup_type_die (type);
24668 if (type_die == NULL)
24670 tree name = TYPE_IDENTIFIER (type);
24671 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24672 type);
24673 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24674 equate_type_number_to_die (type, type_die);
24677 break;
24679 default:
24680 if (is_cxx_auto (type))
24682 tree name = TYPE_IDENTIFIER (type);
24683 dw_die_ref *die = (name == get_identifier ("auto")
24684 ? &auto_die : &decltype_auto_die);
24685 if (!*die)
24687 *die = new_die (DW_TAG_unspecified_type,
24688 comp_unit_die (), NULL_TREE);
24689 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24691 equate_type_number_to_die (type, *die);
24692 break;
24694 gcc_unreachable ();
24697 TREE_ASM_WRITTEN (type) = 1;
24700 static void
24701 gen_type_die (tree type, dw_die_ref context_die)
24703 if (type != error_mark_node)
24705 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24706 if (flag_checking)
24708 dw_die_ref die = lookup_type_die (type);
24709 if (die)
24710 check_die (die);
24715 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24716 things which are local to the given block. */
24718 static void
24719 gen_block_die (tree stmt, dw_die_ref context_die)
24721 int must_output_die = 0;
24722 bool inlined_func;
24724 /* Ignore blocks that are NULL. */
24725 if (stmt == NULL_TREE)
24726 return;
24728 inlined_func = inlined_function_outer_scope_p (stmt);
24730 /* If the block is one fragment of a non-contiguous block, do not
24731 process the variables, since they will have been done by the
24732 origin block. Do process subblocks. */
24733 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24735 tree sub;
24737 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24738 gen_block_die (sub, context_die);
24740 return;
24743 /* Determine if we need to output any Dwarf DIEs at all to represent this
24744 block. */
24745 if (inlined_func)
24746 /* The outer scopes for inlinings *must* always be represented. We
24747 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24748 must_output_die = 1;
24749 else
24751 /* Determine if this block directly contains any "significant"
24752 local declarations which we will need to output DIEs for. */
24753 if (debug_info_level > DINFO_LEVEL_TERSE)
24754 /* We are not in terse mode so *any* local declaration counts
24755 as being a "significant" one. */
24756 must_output_die = ((BLOCK_VARS (stmt) != NULL
24757 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24758 && (TREE_USED (stmt)
24759 || TREE_ASM_WRITTEN (stmt)
24760 || BLOCK_ABSTRACT (stmt)));
24761 else if ((TREE_USED (stmt)
24762 || TREE_ASM_WRITTEN (stmt)
24763 || BLOCK_ABSTRACT (stmt))
24764 && !dwarf2out_ignore_block (stmt))
24765 must_output_die = 1;
24768 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24769 DIE for any block which contains no significant local declarations at
24770 all. Rather, in such cases we just call `decls_for_scope' so that any
24771 needed Dwarf info for any sub-blocks will get properly generated. Note
24772 that in terse mode, our definition of what constitutes a "significant"
24773 local declaration gets restricted to include only inlined function
24774 instances and local (nested) function definitions. */
24775 if (must_output_die)
24777 if (inlined_func)
24779 /* If STMT block is abstract, that means we have been called
24780 indirectly from dwarf2out_abstract_function.
24781 That function rightfully marks the descendent blocks (of
24782 the abstract function it is dealing with) as being abstract,
24783 precisely to prevent us from emitting any
24784 DW_TAG_inlined_subroutine DIE as a descendent
24785 of an abstract function instance. So in that case, we should
24786 not call gen_inlined_subroutine_die.
24788 Later though, when cgraph asks dwarf2out to emit info
24789 for the concrete instance of the function decl into which
24790 the concrete instance of STMT got inlined, the later will lead
24791 to the generation of a DW_TAG_inlined_subroutine DIE. */
24792 if (! BLOCK_ABSTRACT (stmt))
24793 gen_inlined_subroutine_die (stmt, context_die);
24795 else
24796 gen_lexical_block_die (stmt, context_die);
24798 else
24799 decls_for_scope (stmt, context_die);
24802 /* Process variable DECL (or variable with origin ORIGIN) within
24803 block STMT and add it to CONTEXT_DIE. */
24804 static void
24805 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24807 dw_die_ref die;
24808 tree decl_or_origin = decl ? decl : origin;
24810 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24811 die = lookup_decl_die (decl_or_origin);
24812 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24814 if (TYPE_DECL_IS_STUB (decl_or_origin))
24815 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24816 else
24817 die = lookup_decl_die (decl_or_origin);
24818 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24819 if (! die && ! early_dwarf)
24820 return;
24822 else
24823 die = NULL;
24825 if (die != NULL && die->die_parent == NULL)
24826 add_child_die (context_die, die);
24827 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24829 if (early_dwarf)
24830 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24831 stmt, context_die);
24833 else
24834 gen_decl_die (decl, origin, NULL, context_die);
24837 /* Generate all of the decls declared within a given scope and (recursively)
24838 all of its sub-blocks. */
24840 static void
24841 decls_for_scope (tree stmt, dw_die_ref context_die)
24843 tree decl;
24844 unsigned int i;
24845 tree subblocks;
24847 /* Ignore NULL blocks. */
24848 if (stmt == NULL_TREE)
24849 return;
24851 /* Output the DIEs to represent all of the data objects and typedefs
24852 declared directly within this block but not within any nested
24853 sub-blocks. Also, nested function and tag DIEs have been
24854 generated with a parent of NULL; fix that up now. We don't
24855 have to do this if we're at -g1. */
24856 if (debug_info_level > DINFO_LEVEL_TERSE)
24858 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24859 process_scope_var (stmt, decl, NULL_TREE, context_die);
24860 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24861 origin - avoid doing this twice as we have no good way to see
24862 if we've done it once already. */
24863 if (! early_dwarf)
24864 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24866 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24867 if (decl == current_function_decl)
24868 /* Ignore declarations of the current function, while they
24869 are declarations, gen_subprogram_die would treat them
24870 as definitions again, because they are equal to
24871 current_function_decl and endlessly recurse. */;
24872 else if (TREE_CODE (decl) == FUNCTION_DECL)
24873 process_scope_var (stmt, decl, NULL_TREE, context_die);
24874 else
24875 process_scope_var (stmt, NULL_TREE, decl, context_die);
24879 /* Even if we're at -g1, we need to process the subblocks in order to get
24880 inlined call information. */
24882 /* Output the DIEs to represent all sub-blocks (and the items declared
24883 therein) of this block. */
24884 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24885 subblocks != NULL;
24886 subblocks = BLOCK_CHAIN (subblocks))
24887 gen_block_die (subblocks, context_die);
24890 /* Is this a typedef we can avoid emitting? */
24892 bool
24893 is_redundant_typedef (const_tree decl)
24895 if (TYPE_DECL_IS_STUB (decl))
24896 return true;
24898 if (DECL_ARTIFICIAL (decl)
24899 && DECL_CONTEXT (decl)
24900 && is_tagged_type (DECL_CONTEXT (decl))
24901 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24902 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24903 /* Also ignore the artificial member typedef for the class name. */
24904 return true;
24906 return false;
24909 /* Return TRUE if TYPE is a typedef that names a type for linkage
24910 purposes. This kind of typedefs is produced by the C++ FE for
24911 constructs like:
24913 typedef struct {...} foo;
24915 In that case, there is no typedef variant type produced for foo.
24916 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24917 struct type. */
24919 static bool
24920 is_naming_typedef_decl (const_tree decl)
24922 if (decl == NULL_TREE
24923 || TREE_CODE (decl) != TYPE_DECL
24924 || DECL_NAMELESS (decl)
24925 || !is_tagged_type (TREE_TYPE (decl))
24926 || DECL_IS_BUILTIN (decl)
24927 || is_redundant_typedef (decl)
24928 /* It looks like Ada produces TYPE_DECLs that are very similar
24929 to C++ naming typedefs but that have different
24930 semantics. Let's be specific to c++ for now. */
24931 || !is_cxx (decl))
24932 return FALSE;
24934 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24935 && TYPE_NAME (TREE_TYPE (decl)) == decl
24936 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24937 != TYPE_NAME (TREE_TYPE (decl))));
24940 /* Looks up the DIE for a context. */
24942 static inline dw_die_ref
24943 lookup_context_die (tree context)
24945 if (context)
24947 /* Find die that represents this context. */
24948 if (TYPE_P (context))
24950 context = TYPE_MAIN_VARIANT (context);
24951 dw_die_ref ctx = lookup_type_die (context);
24952 if (!ctx)
24953 return NULL;
24954 return strip_naming_typedef (context, ctx);
24956 else
24957 return lookup_decl_die (context);
24959 return comp_unit_die ();
24962 /* Returns the DIE for a context. */
24964 static inline dw_die_ref
24965 get_context_die (tree context)
24967 if (context)
24969 /* Find die that represents this context. */
24970 if (TYPE_P (context))
24972 context = TYPE_MAIN_VARIANT (context);
24973 return strip_naming_typedef (context, force_type_die (context));
24975 else
24976 return force_decl_die (context);
24978 return comp_unit_die ();
24981 /* Returns the DIE for decl. A DIE will always be returned. */
24983 static dw_die_ref
24984 force_decl_die (tree decl)
24986 dw_die_ref decl_die;
24987 unsigned saved_external_flag;
24988 tree save_fn = NULL_TREE;
24989 decl_die = lookup_decl_die (decl);
24990 if (!decl_die)
24992 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
24994 decl_die = lookup_decl_die (decl);
24995 if (decl_die)
24996 return decl_die;
24998 switch (TREE_CODE (decl))
25000 case FUNCTION_DECL:
25001 /* Clear current_function_decl, so that gen_subprogram_die thinks
25002 that this is a declaration. At this point, we just want to force
25003 declaration die. */
25004 save_fn = current_function_decl;
25005 current_function_decl = NULL_TREE;
25006 gen_subprogram_die (decl, context_die);
25007 current_function_decl = save_fn;
25008 break;
25010 case VAR_DECL:
25011 /* Set external flag to force declaration die. Restore it after
25012 gen_decl_die() call. */
25013 saved_external_flag = DECL_EXTERNAL (decl);
25014 DECL_EXTERNAL (decl) = 1;
25015 gen_decl_die (decl, NULL, NULL, context_die);
25016 DECL_EXTERNAL (decl) = saved_external_flag;
25017 break;
25019 case NAMESPACE_DECL:
25020 if (dwarf_version >= 3 || !dwarf_strict)
25021 dwarf2out_decl (decl);
25022 else
25023 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25024 decl_die = comp_unit_die ();
25025 break;
25027 case TRANSLATION_UNIT_DECL:
25028 decl_die = comp_unit_die ();
25029 break;
25031 default:
25032 gcc_unreachable ();
25035 /* We should be able to find the DIE now. */
25036 if (!decl_die)
25037 decl_die = lookup_decl_die (decl);
25038 gcc_assert (decl_die);
25041 return decl_die;
25044 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25045 always returned. */
25047 static dw_die_ref
25048 force_type_die (tree type)
25050 dw_die_ref type_die;
25052 type_die = lookup_type_die (type);
25053 if (!type_die)
25055 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25057 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25058 false, context_die);
25059 gcc_assert (type_die);
25061 return type_die;
25064 /* Force out any required namespaces to be able to output DECL,
25065 and return the new context_die for it, if it's changed. */
25067 static dw_die_ref
25068 setup_namespace_context (tree thing, dw_die_ref context_die)
25070 tree context = (DECL_P (thing)
25071 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25072 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25073 /* Force out the namespace. */
25074 context_die = force_decl_die (context);
25076 return context_die;
25079 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25080 type) within its namespace, if appropriate.
25082 For compatibility with older debuggers, namespace DIEs only contain
25083 declarations; all definitions are emitted at CU scope, with
25084 DW_AT_specification pointing to the declaration (like with class
25085 members). */
25087 static dw_die_ref
25088 declare_in_namespace (tree thing, dw_die_ref context_die)
25090 dw_die_ref ns_context;
25092 if (debug_info_level <= DINFO_LEVEL_TERSE)
25093 return context_die;
25095 /* External declarations in the local scope only need to be emitted
25096 once, not once in the namespace and once in the scope.
25098 This avoids declaring the `extern' below in the
25099 namespace DIE as well as in the innermost scope:
25101 namespace S
25103 int i=5;
25104 int foo()
25106 int i=8;
25107 extern int i;
25108 return i;
25112 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25113 return context_die;
25115 /* If this decl is from an inlined function, then don't try to emit it in its
25116 namespace, as we will get confused. It would have already been emitted
25117 when the abstract instance of the inline function was emitted anyways. */
25118 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25119 return context_die;
25121 ns_context = setup_namespace_context (thing, context_die);
25123 if (ns_context != context_die)
25125 if (is_fortran ())
25126 return ns_context;
25127 if (DECL_P (thing))
25128 gen_decl_die (thing, NULL, NULL, ns_context);
25129 else
25130 gen_type_die (thing, ns_context);
25132 return context_die;
25135 /* Generate a DIE for a namespace or namespace alias. */
25137 static void
25138 gen_namespace_die (tree decl, dw_die_ref context_die)
25140 dw_die_ref namespace_die;
25142 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25143 they are an alias of. */
25144 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25146 /* Output a real namespace or module. */
25147 context_die = setup_namespace_context (decl, comp_unit_die ());
25148 namespace_die = new_die (is_fortran ()
25149 ? DW_TAG_module : DW_TAG_namespace,
25150 context_die, decl);
25151 /* For Fortran modules defined in different CU don't add src coords. */
25152 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25154 const char *name = dwarf2_name (decl, 0);
25155 if (name)
25156 add_name_attribute (namespace_die, name);
25158 else
25159 add_name_and_src_coords_attributes (namespace_die, decl);
25160 if (DECL_EXTERNAL (decl))
25161 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25162 equate_decl_number_to_die (decl, namespace_die);
25164 else
25166 /* Output a namespace alias. */
25168 /* Force out the namespace we are an alias of, if necessary. */
25169 dw_die_ref origin_die
25170 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25172 if (DECL_FILE_SCOPE_P (decl)
25173 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25174 context_die = setup_namespace_context (decl, comp_unit_die ());
25175 /* Now create the namespace alias DIE. */
25176 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25177 add_name_and_src_coords_attributes (namespace_die, decl);
25178 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25179 equate_decl_number_to_die (decl, namespace_die);
25181 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25182 if (want_pubnames ())
25183 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25186 /* Generate Dwarf debug information for a decl described by DECL.
25187 The return value is currently only meaningful for PARM_DECLs,
25188 for all other decls it returns NULL.
25190 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25191 It can be NULL otherwise. */
25193 static dw_die_ref
25194 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25195 dw_die_ref context_die)
25197 tree decl_or_origin = decl ? decl : origin;
25198 tree class_origin = NULL, ultimate_origin;
25200 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25201 return NULL;
25203 /* Ignore pointer bounds decls. */
25204 if (DECL_P (decl_or_origin)
25205 && TREE_TYPE (decl_or_origin)
25206 && POINTER_BOUNDS_P (decl_or_origin))
25207 return NULL;
25209 switch (TREE_CODE (decl_or_origin))
25211 case ERROR_MARK:
25212 break;
25214 case CONST_DECL:
25215 if (!is_fortran () && !is_ada ())
25217 /* The individual enumerators of an enum type get output when we output
25218 the Dwarf representation of the relevant enum type itself. */
25219 break;
25222 /* Emit its type. */
25223 gen_type_die (TREE_TYPE (decl), context_die);
25225 /* And its containing namespace. */
25226 context_die = declare_in_namespace (decl, context_die);
25228 gen_const_die (decl, context_die);
25229 break;
25231 case FUNCTION_DECL:
25232 #if 0
25233 /* FIXME */
25234 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25235 on local redeclarations of global functions. That seems broken. */
25236 if (current_function_decl != decl)
25237 /* This is only a declaration. */;
25238 #endif
25240 /* If we're emitting a clone, emit info for the abstract instance. */
25241 if (origin || DECL_ORIGIN (decl) != decl)
25242 dwarf2out_abstract_function (origin
25243 ? DECL_ORIGIN (origin)
25244 : DECL_ABSTRACT_ORIGIN (decl));
25246 /* If we're emitting an out-of-line copy of an inline function,
25247 emit info for the abstract instance and set up to refer to it. */
25248 else if (cgraph_function_possibly_inlined_p (decl)
25249 && ! DECL_ABSTRACT_P (decl)
25250 && ! class_or_namespace_scope_p (context_die)
25251 /* dwarf2out_abstract_function won't emit a die if this is just
25252 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25253 that case, because that works only if we have a die. */
25254 && DECL_INITIAL (decl) != NULL_TREE)
25256 dwarf2out_abstract_function (decl);
25257 set_decl_origin_self (decl);
25260 /* Otherwise we're emitting the primary DIE for this decl. */
25261 else if (debug_info_level > DINFO_LEVEL_TERSE)
25263 /* Before we describe the FUNCTION_DECL itself, make sure that we
25264 have its containing type. */
25265 if (!origin)
25266 origin = decl_class_context (decl);
25267 if (origin != NULL_TREE)
25268 gen_type_die (origin, context_die);
25270 /* And its return type. */
25271 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25273 /* And its virtual context. */
25274 if (DECL_VINDEX (decl) != NULL_TREE)
25275 gen_type_die (DECL_CONTEXT (decl), context_die);
25277 /* Make sure we have a member DIE for decl. */
25278 if (origin != NULL_TREE)
25279 gen_type_die_for_member (origin, decl, context_die);
25281 /* And its containing namespace. */
25282 context_die = declare_in_namespace (decl, context_die);
25285 /* Now output a DIE to represent the function itself. */
25286 if (decl)
25287 gen_subprogram_die (decl, context_die);
25288 break;
25290 case TYPE_DECL:
25291 /* If we are in terse mode, don't generate any DIEs to represent any
25292 actual typedefs. */
25293 if (debug_info_level <= DINFO_LEVEL_TERSE)
25294 break;
25296 /* In the special case of a TYPE_DECL node representing the declaration
25297 of some type tag, if the given TYPE_DECL is marked as having been
25298 instantiated from some other (original) TYPE_DECL node (e.g. one which
25299 was generated within the original definition of an inline function) we
25300 used to generate a special (abbreviated) DW_TAG_structure_type,
25301 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25302 should be actually referencing those DIEs, as variable DIEs with that
25303 type would be emitted already in the abstract origin, so it was always
25304 removed during unused type prunning. Don't add anything in this
25305 case. */
25306 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25307 break;
25309 if (is_redundant_typedef (decl))
25310 gen_type_die (TREE_TYPE (decl), context_die);
25311 else
25312 /* Output a DIE to represent the typedef itself. */
25313 gen_typedef_die (decl, context_die);
25314 break;
25316 case LABEL_DECL:
25317 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25318 gen_label_die (decl, context_die);
25319 break;
25321 case VAR_DECL:
25322 case RESULT_DECL:
25323 /* If we are in terse mode, don't generate any DIEs to represent any
25324 variable declarations or definitions. */
25325 if (debug_info_level <= DINFO_LEVEL_TERSE)
25326 break;
25328 /* Output any DIEs that are needed to specify the type of this data
25329 object. */
25330 if (decl_by_reference_p (decl_or_origin))
25331 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25332 else
25333 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25335 /* And its containing type. */
25336 class_origin = decl_class_context (decl_or_origin);
25337 if (class_origin != NULL_TREE)
25338 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25340 /* And its containing namespace. */
25341 context_die = declare_in_namespace (decl_or_origin, context_die);
25343 /* Now output the DIE to represent the data object itself. This gets
25344 complicated because of the possibility that the VAR_DECL really
25345 represents an inlined instance of a formal parameter for an inline
25346 function. */
25347 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25348 if (ultimate_origin != NULL_TREE
25349 && TREE_CODE (ultimate_origin) == PARM_DECL)
25350 gen_formal_parameter_die (decl, origin,
25351 true /* Emit name attribute. */,
25352 context_die);
25353 else
25354 gen_variable_die (decl, origin, context_die);
25355 break;
25357 case FIELD_DECL:
25358 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25359 /* Ignore the nameless fields that are used to skip bits but handle C++
25360 anonymous unions and structs. */
25361 if (DECL_NAME (decl) != NULL_TREE
25362 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25363 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25365 gen_type_die (member_declared_type (decl), context_die);
25366 gen_field_die (decl, ctx, context_die);
25368 break;
25370 case PARM_DECL:
25371 if (DECL_BY_REFERENCE (decl_or_origin))
25372 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25373 else
25374 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25375 return gen_formal_parameter_die (decl, origin,
25376 true /* Emit name attribute. */,
25377 context_die);
25379 case NAMESPACE_DECL:
25380 if (dwarf_version >= 3 || !dwarf_strict)
25381 gen_namespace_die (decl, context_die);
25382 break;
25384 case IMPORTED_DECL:
25385 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25386 DECL_CONTEXT (decl), context_die);
25387 break;
25389 case NAMELIST_DECL:
25390 gen_namelist_decl (DECL_NAME (decl), context_die,
25391 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25392 break;
25394 default:
25395 /* Probably some frontend-internal decl. Assume we don't care. */
25396 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25397 break;
25400 return NULL;
25403 /* Output initial debug information for global DECL. Called at the
25404 end of the parsing process.
25406 This is the initial debug generation process. As such, the DIEs
25407 generated may be incomplete. A later debug generation pass
25408 (dwarf2out_late_global_decl) will augment the information generated
25409 in this pass (e.g., with complete location info). */
25411 static void
25412 dwarf2out_early_global_decl (tree decl)
25414 set_early_dwarf s;
25416 /* gen_decl_die() will set DECL_ABSTRACT because
25417 cgraph_function_possibly_inlined_p() returns true. This is in
25418 turn will cause DW_AT_inline attributes to be set.
25420 This happens because at early dwarf generation, there is no
25421 cgraph information, causing cgraph_function_possibly_inlined_p()
25422 to return true. Trick cgraph_function_possibly_inlined_p()
25423 while we generate dwarf early. */
25424 bool save = symtab->global_info_ready;
25425 symtab->global_info_ready = true;
25427 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25428 other DECLs and they can point to template types or other things
25429 that dwarf2out can't handle when done via dwarf2out_decl. */
25430 if (TREE_CODE (decl) != TYPE_DECL
25431 && TREE_CODE (decl) != PARM_DECL)
25433 tree save_fndecl = current_function_decl;
25434 if (TREE_CODE (decl) == FUNCTION_DECL)
25436 /* For nested functions, make sure we have DIEs for the parents first
25437 so that all nested DIEs are generated at the proper scope in the
25438 first shot. */
25439 tree context = decl_function_context (decl);
25440 if (context != NULL && lookup_decl_die (context) == NULL)
25442 current_function_decl = context;
25443 dwarf2out_decl (context);
25446 current_function_decl = decl;
25448 dwarf2out_decl (decl);
25449 if (TREE_CODE (decl) == FUNCTION_DECL)
25450 current_function_decl = save_fndecl;
25452 symtab->global_info_ready = save;
25455 /* Output debug information for global decl DECL. Called from
25456 toplev.c after compilation proper has finished. */
25458 static void
25459 dwarf2out_late_global_decl (tree decl)
25461 /* Fill-in any location information we were unable to determine
25462 on the first pass. */
25463 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25465 dw_die_ref die = lookup_decl_die (decl);
25467 /* We have to generate early debug late for LTO. */
25468 if (! die && in_lto_p)
25470 dwarf2out_decl (decl);
25471 die = lookup_decl_die (decl);
25474 if (die)
25476 /* We get called via the symtab code invoking late_global_decl
25477 for symbols that are optimized out. Do not add locations
25478 for those, except if they have a DECL_VALUE_EXPR, in which case
25479 they are relevant for debuggers. */
25480 varpool_node *node = varpool_node::get (decl);
25481 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25482 tree_add_const_value_attribute_for_decl (die, decl);
25483 else
25484 add_location_or_const_value_attribute (die, decl, false);
25489 /* Output debug information for type decl DECL. Called from toplev.c
25490 and from language front ends (to record built-in types). */
25491 static void
25492 dwarf2out_type_decl (tree decl, int local)
25494 if (!local)
25496 set_early_dwarf s;
25497 dwarf2out_decl (decl);
25501 /* Output debug information for imported module or decl DECL.
25502 NAME is non-NULL name in the lexical block if the decl has been renamed.
25503 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25504 that DECL belongs to.
25505 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25506 static void
25507 dwarf2out_imported_module_or_decl_1 (tree decl,
25508 tree name,
25509 tree lexical_block,
25510 dw_die_ref lexical_block_die)
25512 expanded_location xloc;
25513 dw_die_ref imported_die = NULL;
25514 dw_die_ref at_import_die;
25516 if (TREE_CODE (decl) == IMPORTED_DECL)
25518 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25519 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25520 gcc_assert (decl);
25522 else
25523 xloc = expand_location (input_location);
25525 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25527 at_import_die = force_type_die (TREE_TYPE (decl));
25528 /* For namespace N { typedef void T; } using N::T; base_type_die
25529 returns NULL, but DW_TAG_imported_declaration requires
25530 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25531 if (!at_import_die)
25533 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25534 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25535 at_import_die = lookup_type_die (TREE_TYPE (decl));
25536 gcc_assert (at_import_die);
25539 else
25541 at_import_die = lookup_decl_die (decl);
25542 if (!at_import_die)
25544 /* If we're trying to avoid duplicate debug info, we may not have
25545 emitted the member decl for this field. Emit it now. */
25546 if (TREE_CODE (decl) == FIELD_DECL)
25548 tree type = DECL_CONTEXT (decl);
25550 if (TYPE_CONTEXT (type)
25551 && TYPE_P (TYPE_CONTEXT (type))
25552 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25553 DINFO_USAGE_DIR_USE))
25554 return;
25555 gen_type_die_for_member (type, decl,
25556 get_context_die (TYPE_CONTEXT (type)));
25558 if (TREE_CODE (decl) == NAMELIST_DECL)
25559 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25560 get_context_die (DECL_CONTEXT (decl)),
25561 NULL_TREE);
25562 else
25563 at_import_die = force_decl_die (decl);
25567 if (TREE_CODE (decl) == NAMESPACE_DECL)
25569 if (dwarf_version >= 3 || !dwarf_strict)
25570 imported_die = new_die (DW_TAG_imported_module,
25571 lexical_block_die,
25572 lexical_block);
25573 else
25574 return;
25576 else
25577 imported_die = new_die (DW_TAG_imported_declaration,
25578 lexical_block_die,
25579 lexical_block);
25581 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25582 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25583 if (debug_column_info && xloc.column)
25584 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25585 if (name)
25586 add_AT_string (imported_die, DW_AT_name,
25587 IDENTIFIER_POINTER (name));
25588 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25591 /* Output debug information for imported module or decl DECL.
25592 NAME is non-NULL name in context if the decl has been renamed.
25593 CHILD is true if decl is one of the renamed decls as part of
25594 importing whole module. */
25596 static void
25597 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25598 bool child)
25600 /* dw_die_ref at_import_die; */
25601 dw_die_ref scope_die;
25603 if (debug_info_level <= DINFO_LEVEL_TERSE)
25604 return;
25606 gcc_assert (decl);
25608 set_early_dwarf s;
25610 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25611 We need decl DIE for reference and scope die. First, get DIE for the decl
25612 itself. */
25614 /* Get the scope die for decl context. Use comp_unit_die for global module
25615 or decl. If die is not found for non globals, force new die. */
25616 if (context
25617 && TYPE_P (context)
25618 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25619 return;
25621 scope_die = get_context_die (context);
25623 if (child)
25625 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25626 there is nothing we can do, here. */
25627 if (dwarf_version < 3 && dwarf_strict)
25628 return;
25630 gcc_assert (scope_die->die_child);
25631 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25632 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25633 scope_die = scope_die->die_child;
25636 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25637 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25640 /* Output debug information for namelists. */
25642 static dw_die_ref
25643 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25645 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25646 tree value;
25647 unsigned i;
25649 if (debug_info_level <= DINFO_LEVEL_TERSE)
25650 return NULL;
25652 gcc_assert (scope_die != NULL);
25653 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25654 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25656 /* If there are no item_decls, we have a nondefining namelist, e.g.
25657 with USE association; hence, set DW_AT_declaration. */
25658 if (item_decls == NULL_TREE)
25660 add_AT_flag (nml_die, DW_AT_declaration, 1);
25661 return nml_die;
25664 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25666 nml_item_ref_die = lookup_decl_die (value);
25667 if (!nml_item_ref_die)
25668 nml_item_ref_die = force_decl_die (value);
25670 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25671 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25673 return nml_die;
25677 /* Write the debugging output for DECL and return the DIE. */
25679 static void
25680 dwarf2out_decl (tree decl)
25682 dw_die_ref context_die = comp_unit_die ();
25684 switch (TREE_CODE (decl))
25686 case ERROR_MARK:
25687 return;
25689 case FUNCTION_DECL:
25690 /* If we're a nested function, initially use a parent of NULL; if we're
25691 a plain function, this will be fixed up in decls_for_scope. If
25692 we're a method, it will be ignored, since we already have a DIE. */
25693 if (decl_function_context (decl)
25694 /* But if we're in terse mode, we don't care about scope. */
25695 && debug_info_level > DINFO_LEVEL_TERSE)
25696 context_die = NULL;
25697 break;
25699 case VAR_DECL:
25700 /* For local statics lookup proper context die. */
25701 if (local_function_static (decl))
25702 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25704 /* If we are in terse mode, don't generate any DIEs to represent any
25705 variable declarations or definitions. */
25706 if (debug_info_level <= DINFO_LEVEL_TERSE)
25707 return;
25708 break;
25710 case CONST_DECL:
25711 if (debug_info_level <= DINFO_LEVEL_TERSE)
25712 return;
25713 if (!is_fortran () && !is_ada ())
25714 return;
25715 if (TREE_STATIC (decl) && decl_function_context (decl))
25716 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25717 break;
25719 case NAMESPACE_DECL:
25720 case IMPORTED_DECL:
25721 if (debug_info_level <= DINFO_LEVEL_TERSE)
25722 return;
25723 if (lookup_decl_die (decl) != NULL)
25724 return;
25725 break;
25727 case TYPE_DECL:
25728 /* Don't emit stubs for types unless they are needed by other DIEs. */
25729 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25730 return;
25732 /* Don't bother trying to generate any DIEs to represent any of the
25733 normal built-in types for the language we are compiling. */
25734 if (DECL_IS_BUILTIN (decl))
25735 return;
25737 /* If we are in terse mode, don't generate any DIEs for types. */
25738 if (debug_info_level <= DINFO_LEVEL_TERSE)
25739 return;
25741 /* If we're a function-scope tag, initially use a parent of NULL;
25742 this will be fixed up in decls_for_scope. */
25743 if (decl_function_context (decl))
25744 context_die = NULL;
25746 break;
25748 case NAMELIST_DECL:
25749 break;
25751 default:
25752 return;
25755 gen_decl_die (decl, NULL, NULL, context_die);
25757 if (flag_checking)
25759 dw_die_ref die = lookup_decl_die (decl);
25760 if (die)
25761 check_die (die);
25765 /* Write the debugging output for DECL. */
25767 static void
25768 dwarf2out_function_decl (tree decl)
25770 dwarf2out_decl (decl);
25771 call_arg_locations = NULL;
25772 call_arg_loc_last = NULL;
25773 call_site_count = -1;
25774 tail_call_site_count = -1;
25775 decl_loc_table->empty ();
25776 cached_dw_loc_list_table->empty ();
25779 /* Output a marker (i.e. a label) for the beginning of the generated code for
25780 a lexical block. */
25782 static void
25783 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25784 unsigned int blocknum)
25786 switch_to_section (current_function_section ());
25787 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25790 /* Output a marker (i.e. a label) for the end of the generated code for a
25791 lexical block. */
25793 static void
25794 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25796 switch_to_section (current_function_section ());
25797 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25800 /* Returns nonzero if it is appropriate not to emit any debugging
25801 information for BLOCK, because it doesn't contain any instructions.
25803 Don't allow this for blocks with nested functions or local classes
25804 as we would end up with orphans, and in the presence of scheduling
25805 we may end up calling them anyway. */
25807 static bool
25808 dwarf2out_ignore_block (const_tree block)
25810 tree decl;
25811 unsigned int i;
25813 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25814 if (TREE_CODE (decl) == FUNCTION_DECL
25815 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25816 return 0;
25817 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25819 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25820 if (TREE_CODE (decl) == FUNCTION_DECL
25821 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25822 return 0;
25825 return 1;
25828 /* Hash table routines for file_hash. */
25830 bool
25831 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25833 return filename_cmp (p1->filename, p2) == 0;
25836 hashval_t
25837 dwarf_file_hasher::hash (dwarf_file_data *p)
25839 return htab_hash_string (p->filename);
25842 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25843 dwarf2out.c) and return its "index". The index of each (known) filename is
25844 just a unique number which is associated with only that one filename. We
25845 need such numbers for the sake of generating labels (in the .debug_sfnames
25846 section) and references to those files numbers (in the .debug_srcinfo
25847 and .debug_macinfo sections). If the filename given as an argument is not
25848 found in our current list, add it to the list and assign it the next
25849 available unique index number. */
25851 static struct dwarf_file_data *
25852 lookup_filename (const char *file_name)
25854 struct dwarf_file_data * created;
25856 if (!file_name)
25857 return NULL;
25859 dwarf_file_data **slot
25860 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25861 INSERT);
25862 if (*slot)
25863 return *slot;
25865 created = ggc_alloc<dwarf_file_data> ();
25866 created->filename = file_name;
25867 created->emitted_number = 0;
25868 *slot = created;
25869 return created;
25872 /* If the assembler will construct the file table, then translate the compiler
25873 internal file table number into the assembler file table number, and emit
25874 a .file directive if we haven't already emitted one yet. The file table
25875 numbers are different because we prune debug info for unused variables and
25876 types, which may include filenames. */
25878 static int
25879 maybe_emit_file (struct dwarf_file_data * fd)
25881 if (! fd->emitted_number)
25883 if (last_emitted_file)
25884 fd->emitted_number = last_emitted_file->emitted_number + 1;
25885 else
25886 fd->emitted_number = 1;
25887 last_emitted_file = fd;
25889 if (DWARF2_ASM_LINE_DEBUG_INFO)
25891 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25892 output_quoted_string (asm_out_file,
25893 remap_debug_filename (fd->filename));
25894 fputc ('\n', asm_out_file);
25898 return fd->emitted_number;
25901 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25902 That generation should happen after function debug info has been
25903 generated. The value of the attribute is the constant value of ARG. */
25905 static void
25906 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25908 die_arg_entry entry;
25910 if (!die || !arg)
25911 return;
25913 gcc_assert (early_dwarf);
25915 if (!tmpl_value_parm_die_table)
25916 vec_alloc (tmpl_value_parm_die_table, 32);
25918 entry.die = die;
25919 entry.arg = arg;
25920 vec_safe_push (tmpl_value_parm_die_table, entry);
25923 /* Return TRUE if T is an instance of generic type, FALSE
25924 otherwise. */
25926 static bool
25927 generic_type_p (tree t)
25929 if (t == NULL_TREE || !TYPE_P (t))
25930 return false;
25931 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
25934 /* Schedule the generation of the generic parameter dies for the
25935 instance of generic type T. The proper generation itself is later
25936 done by gen_scheduled_generic_parms_dies. */
25938 static void
25939 schedule_generic_params_dies_gen (tree t)
25941 if (!generic_type_p (t))
25942 return;
25944 gcc_assert (early_dwarf);
25946 if (!generic_type_instances)
25947 vec_alloc (generic_type_instances, 256);
25949 vec_safe_push (generic_type_instances, t);
25952 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
25953 by append_entry_to_tmpl_value_parm_die_table. This function must
25954 be called after function DIEs have been generated. */
25956 static void
25957 gen_remaining_tmpl_value_param_die_attribute (void)
25959 if (tmpl_value_parm_die_table)
25961 unsigned i, j;
25962 die_arg_entry *e;
25964 /* We do this in two phases - first get the cases we can
25965 handle during early-finish, preserving those we cannot
25966 (containing symbolic constants where we don't yet know
25967 whether we are going to output the referenced symbols).
25968 For those we try again at late-finish. */
25969 j = 0;
25970 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
25972 if (!tree_add_const_value_attribute (e->die, e->arg))
25974 dw_loc_descr_ref loc = NULL;
25975 if (! early_dwarf
25976 && (dwarf_version >= 5 || !dwarf_strict))
25977 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
25978 if (loc)
25979 add_AT_loc (e->die, DW_AT_location, loc);
25980 else
25981 (*tmpl_value_parm_die_table)[j++] = *e;
25984 tmpl_value_parm_die_table->truncate (j);
25988 /* Generate generic parameters DIEs for instances of generic types
25989 that have been previously scheduled by
25990 schedule_generic_params_dies_gen. This function must be called
25991 after all the types of the CU have been laid out. */
25993 static void
25994 gen_scheduled_generic_parms_dies (void)
25996 unsigned i;
25997 tree t;
25999 if (!generic_type_instances)
26000 return;
26002 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26003 if (COMPLETE_TYPE_P (t))
26004 gen_generic_params_dies (t);
26006 generic_type_instances = NULL;
26010 /* Replace DW_AT_name for the decl with name. */
26012 static void
26013 dwarf2out_set_name (tree decl, tree name)
26015 dw_die_ref die;
26016 dw_attr_node *attr;
26017 const char *dname;
26019 die = TYPE_SYMTAB_DIE (decl);
26020 if (!die)
26021 return;
26023 dname = dwarf2_name (name, 0);
26024 if (!dname)
26025 return;
26027 attr = get_AT (die, DW_AT_name);
26028 if (attr)
26030 struct indirect_string_node *node;
26032 node = find_AT_string (dname);
26033 /* replace the string. */
26034 attr->dw_attr_val.v.val_str = node;
26037 else
26038 add_name_attribute (die, dname);
26041 /* True if before or during processing of the first function being emitted. */
26042 static bool in_first_function_p = true;
26043 /* True if loc_note during dwarf2out_var_location call might still be
26044 before first real instruction at address equal to .Ltext0. */
26045 static bool maybe_at_text_label_p = true;
26046 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26047 static unsigned int first_loclabel_num_not_at_text_label;
26049 /* Called by the final INSN scan whenever we see a var location. We
26050 use it to drop labels in the right places, and throw the location in
26051 our lookup table. */
26053 static void
26054 dwarf2out_var_location (rtx_insn *loc_note)
26056 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26057 struct var_loc_node *newloc;
26058 rtx_insn *next_real, *next_note;
26059 rtx_insn *call_insn = NULL;
26060 static const char *last_label;
26061 static const char *last_postcall_label;
26062 static bool last_in_cold_section_p;
26063 static rtx_insn *expected_next_loc_note;
26064 tree decl;
26065 bool var_loc_p;
26067 if (!NOTE_P (loc_note))
26069 if (CALL_P (loc_note))
26071 call_site_count++;
26072 if (SIBLING_CALL_P (loc_note))
26073 tail_call_site_count++;
26074 if (optimize == 0 && !flag_var_tracking)
26076 /* When the var-tracking pass is not running, there is no note
26077 for indirect calls whose target is compile-time known. In this
26078 case, process such calls specifically so that we generate call
26079 sites for them anyway. */
26080 rtx x = PATTERN (loc_note);
26081 if (GET_CODE (x) == PARALLEL)
26082 x = XVECEXP (x, 0, 0);
26083 if (GET_CODE (x) == SET)
26084 x = SET_SRC (x);
26085 if (GET_CODE (x) == CALL)
26086 x = XEXP (x, 0);
26087 if (!MEM_P (x)
26088 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26089 || !SYMBOL_REF_DECL (XEXP (x, 0))
26090 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26091 != FUNCTION_DECL))
26093 call_insn = loc_note;
26094 loc_note = NULL;
26095 var_loc_p = false;
26097 next_real = next_real_insn (call_insn);
26098 next_note = NULL;
26099 cached_next_real_insn = NULL;
26100 goto create_label;
26104 return;
26107 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26108 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26109 return;
26111 /* Optimize processing a large consecutive sequence of location
26112 notes so we don't spend too much time in next_real_insn. If the
26113 next insn is another location note, remember the next_real_insn
26114 calculation for next time. */
26115 next_real = cached_next_real_insn;
26116 if (next_real)
26118 if (expected_next_loc_note != loc_note)
26119 next_real = NULL;
26122 next_note = NEXT_INSN (loc_note);
26123 if (! next_note
26124 || next_note->deleted ()
26125 || ! NOTE_P (next_note)
26126 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26127 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26128 next_note = NULL;
26130 if (! next_real)
26131 next_real = next_real_insn (loc_note);
26133 if (next_note)
26135 expected_next_loc_note = next_note;
26136 cached_next_real_insn = next_real;
26138 else
26139 cached_next_real_insn = NULL;
26141 /* If there are no instructions which would be affected by this note,
26142 don't do anything. */
26143 if (var_loc_p
26144 && next_real == NULL_RTX
26145 && !NOTE_DURING_CALL_P (loc_note))
26146 return;
26148 create_label:
26150 if (next_real == NULL_RTX)
26151 next_real = get_last_insn ();
26153 /* If there were any real insns between note we processed last time
26154 and this note (or if it is the first note), clear
26155 last_{,postcall_}label so that they are not reused this time. */
26156 if (last_var_location_insn == NULL_RTX
26157 || last_var_location_insn != next_real
26158 || last_in_cold_section_p != in_cold_section_p)
26160 last_label = NULL;
26161 last_postcall_label = NULL;
26164 if (var_loc_p)
26166 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26167 newloc = add_var_loc_to_decl (decl, loc_note,
26168 NOTE_DURING_CALL_P (loc_note)
26169 ? last_postcall_label : last_label);
26170 if (newloc == NULL)
26171 return;
26173 else
26175 decl = NULL_TREE;
26176 newloc = NULL;
26179 /* If there were no real insns between note we processed last time
26180 and this note, use the label we emitted last time. Otherwise
26181 create a new label and emit it. */
26182 if (last_label == NULL)
26184 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26185 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26186 loclabel_num++;
26187 last_label = ggc_strdup (loclabel);
26188 /* See if loclabel might be equal to .Ltext0. If yes,
26189 bump first_loclabel_num_not_at_text_label. */
26190 if (!have_multiple_function_sections
26191 && in_first_function_p
26192 && maybe_at_text_label_p)
26194 static rtx_insn *last_start;
26195 rtx_insn *insn;
26196 for (insn = loc_note; insn; insn = previous_insn (insn))
26197 if (insn == last_start)
26198 break;
26199 else if (!NONDEBUG_INSN_P (insn))
26200 continue;
26201 else
26203 rtx body = PATTERN (insn);
26204 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26205 continue;
26206 /* Inline asm could occupy zero bytes. */
26207 else if (GET_CODE (body) == ASM_INPUT
26208 || asm_noperands (body) >= 0)
26209 continue;
26210 #ifdef HAVE_attr_length
26211 else if (get_attr_min_length (insn) == 0)
26212 continue;
26213 #endif
26214 else
26216 /* Assume insn has non-zero length. */
26217 maybe_at_text_label_p = false;
26218 break;
26221 if (maybe_at_text_label_p)
26223 last_start = loc_note;
26224 first_loclabel_num_not_at_text_label = loclabel_num;
26229 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26230 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26232 if (!var_loc_p)
26234 struct call_arg_loc_node *ca_loc
26235 = ggc_cleared_alloc<call_arg_loc_node> ();
26236 rtx_insn *prev
26237 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26239 ca_loc->call_arg_loc_note = loc_note;
26240 ca_loc->next = NULL;
26241 ca_loc->label = last_label;
26242 gcc_assert (prev
26243 && (CALL_P (prev)
26244 || (NONJUMP_INSN_P (prev)
26245 && GET_CODE (PATTERN (prev)) == SEQUENCE
26246 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26247 if (!CALL_P (prev))
26248 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26249 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26251 /* Look for a SYMBOL_REF in the "prev" instruction. */
26252 rtx x = get_call_rtx_from (PATTERN (prev));
26253 if (x)
26255 /* Try to get the call symbol, if any. */
26256 if (MEM_P (XEXP (x, 0)))
26257 x = XEXP (x, 0);
26258 /* First, look for a memory access to a symbol_ref. */
26259 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26260 && SYMBOL_REF_DECL (XEXP (x, 0))
26261 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26262 ca_loc->symbol_ref = XEXP (x, 0);
26263 /* Otherwise, look at a compile-time known user-level function
26264 declaration. */
26265 else if (MEM_P (x)
26266 && MEM_EXPR (x)
26267 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26268 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26271 ca_loc->block = insn_scope (prev);
26272 if (call_arg_locations)
26273 call_arg_loc_last->next = ca_loc;
26274 else
26275 call_arg_locations = ca_loc;
26276 call_arg_loc_last = ca_loc;
26278 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26279 newloc->label = last_label;
26280 else
26282 if (!last_postcall_label)
26284 sprintf (loclabel, "%s-1", last_label);
26285 last_postcall_label = ggc_strdup (loclabel);
26287 newloc->label = last_postcall_label;
26290 last_var_location_insn = next_real;
26291 last_in_cold_section_p = in_cold_section_p;
26294 /* Called from finalize_size_functions for size functions so that their body
26295 can be encoded in the debug info to describe the layout of variable-length
26296 structures. */
26298 static void
26299 dwarf2out_size_function (tree decl)
26301 function_to_dwarf_procedure (decl);
26304 /* Note in one location list that text section has changed. */
26307 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26309 var_loc_list *list = *slot;
26310 if (list->first)
26311 list->last_before_switch
26312 = list->last->next ? list->last->next : list->last;
26313 return 1;
26316 /* Note in all location lists that text section has changed. */
26318 static void
26319 var_location_switch_text_section (void)
26321 if (decl_loc_table == NULL)
26322 return;
26324 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26327 /* Create a new line number table. */
26329 static dw_line_info_table *
26330 new_line_info_table (void)
26332 dw_line_info_table *table;
26334 table = ggc_cleared_alloc<dw_line_info_table> ();
26335 table->file_num = 1;
26336 table->line_num = 1;
26337 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26339 return table;
26342 /* Lookup the "current" table into which we emit line info, so
26343 that we don't have to do it for every source line. */
26345 static void
26346 set_cur_line_info_table (section *sec)
26348 dw_line_info_table *table;
26350 if (sec == text_section)
26351 table = text_section_line_info;
26352 else if (sec == cold_text_section)
26354 table = cold_text_section_line_info;
26355 if (!table)
26357 cold_text_section_line_info = table = new_line_info_table ();
26358 table->end_label = cold_end_label;
26361 else
26363 const char *end_label;
26365 if (crtl->has_bb_partition)
26367 if (in_cold_section_p)
26368 end_label = crtl->subsections.cold_section_end_label;
26369 else
26370 end_label = crtl->subsections.hot_section_end_label;
26372 else
26374 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26375 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26376 current_function_funcdef_no);
26377 end_label = ggc_strdup (label);
26380 table = new_line_info_table ();
26381 table->end_label = end_label;
26383 vec_safe_push (separate_line_info, table);
26386 if (DWARF2_ASM_LINE_DEBUG_INFO)
26387 table->is_stmt = (cur_line_info_table
26388 ? cur_line_info_table->is_stmt
26389 : DWARF_LINE_DEFAULT_IS_STMT_START);
26390 cur_line_info_table = table;
26394 /* We need to reset the locations at the beginning of each
26395 function. We can't do this in the end_function hook, because the
26396 declarations that use the locations won't have been output when
26397 that hook is called. Also compute have_multiple_function_sections here. */
26399 static void
26400 dwarf2out_begin_function (tree fun)
26402 section *sec = function_section (fun);
26404 if (sec != text_section)
26405 have_multiple_function_sections = true;
26407 if (crtl->has_bb_partition && !cold_text_section)
26409 gcc_assert (current_function_decl == fun);
26410 cold_text_section = unlikely_text_section ();
26411 switch_to_section (cold_text_section);
26412 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26413 switch_to_section (sec);
26416 dwarf2out_note_section_used ();
26417 call_site_count = 0;
26418 tail_call_site_count = 0;
26420 set_cur_line_info_table (sec);
26423 /* Helper function of dwarf2out_end_function, called only after emitting
26424 the very first function into assembly. Check if some .debug_loc range
26425 might end with a .LVL* label that could be equal to .Ltext0.
26426 In that case we must force using absolute addresses in .debug_loc ranges,
26427 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26428 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26429 list terminator.
26430 Set have_multiple_function_sections to true in that case and
26431 terminate htab traversal. */
26434 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26436 var_loc_list *entry = *slot;
26437 struct var_loc_node *node;
26439 node = entry->first;
26440 if (node && node->next && node->next->label)
26442 unsigned int i;
26443 const char *label = node->next->label;
26444 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26446 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26448 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26449 if (strcmp (label, loclabel) == 0)
26451 have_multiple_function_sections = true;
26452 return 0;
26456 return 1;
26459 /* Hook called after emitting a function into assembly.
26460 This does something only for the very first function emitted. */
26462 static void
26463 dwarf2out_end_function (unsigned int)
26465 if (in_first_function_p
26466 && !have_multiple_function_sections
26467 && first_loclabel_num_not_at_text_label
26468 && decl_loc_table)
26469 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26470 in_first_function_p = false;
26471 maybe_at_text_label_p = false;
26474 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26475 front-ends register a translation unit even before dwarf2out_init is
26476 called. */
26477 static tree main_translation_unit = NULL_TREE;
26479 /* Hook called by front-ends after they built their main translation unit.
26480 Associate comp_unit_die to UNIT. */
26482 static void
26483 dwarf2out_register_main_translation_unit (tree unit)
26485 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26486 && main_translation_unit == NULL_TREE);
26487 main_translation_unit = unit;
26488 /* If dwarf2out_init has not been called yet, it will perform the association
26489 itself looking at main_translation_unit. */
26490 if (decl_die_table != NULL)
26491 equate_decl_number_to_die (unit, comp_unit_die ());
26494 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26496 static void
26497 push_dw_line_info_entry (dw_line_info_table *table,
26498 enum dw_line_info_opcode opcode, unsigned int val)
26500 dw_line_info_entry e;
26501 e.opcode = opcode;
26502 e.val = val;
26503 vec_safe_push (table->entries, e);
26506 /* Output a label to mark the beginning of a source code line entry
26507 and record information relating to this source line, in
26508 'line_info_table' for later output of the .debug_line section. */
26509 /* ??? The discriminator parameter ought to be unsigned. */
26511 static void
26512 dwarf2out_source_line (unsigned int line, unsigned int column,
26513 const char *filename,
26514 int discriminator, bool is_stmt)
26516 unsigned int file_num;
26517 dw_line_info_table *table;
26519 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26520 return;
26522 /* The discriminator column was added in dwarf4. Simplify the below
26523 by simply removing it if we're not supposed to output it. */
26524 if (dwarf_version < 4 && dwarf_strict)
26525 discriminator = 0;
26527 if (!debug_column_info)
26528 column = 0;
26530 table = cur_line_info_table;
26531 file_num = maybe_emit_file (lookup_filename (filename));
26533 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26534 the debugger has used the second (possibly duplicate) line number
26535 at the beginning of the function to mark the end of the prologue.
26536 We could eliminate any other duplicates within the function. For
26537 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26538 that second line number entry. */
26539 /* Recall that this end-of-prologue indication is *not* the same thing
26540 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26541 to which the hook corresponds, follows the last insn that was
26542 emitted by gen_prologue. What we need is to precede the first insn
26543 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26544 insn that corresponds to something the user wrote. These may be
26545 very different locations once scheduling is enabled. */
26547 if (0 && file_num == table->file_num
26548 && line == table->line_num
26549 && column == table->column_num
26550 && discriminator == table->discrim_num
26551 && is_stmt == table->is_stmt)
26552 return;
26554 switch_to_section (current_function_section ());
26556 /* If requested, emit something human-readable. */
26557 if (flag_debug_asm)
26559 if (debug_column_info)
26560 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26561 filename, line, column);
26562 else
26563 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26564 filename, line);
26567 if (DWARF2_ASM_LINE_DEBUG_INFO)
26569 /* Emit the .loc directive understood by GNU as. */
26570 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26571 file_num, line, is_stmt, discriminator */
26572 fputs ("\t.loc ", asm_out_file);
26573 fprint_ul (asm_out_file, file_num);
26574 putc (' ', asm_out_file);
26575 fprint_ul (asm_out_file, line);
26576 putc (' ', asm_out_file);
26577 if (debug_column_info)
26578 fprint_ul (asm_out_file, column);
26579 else
26580 putc ('0', asm_out_file);
26582 if (is_stmt != table->is_stmt)
26584 fputs (" is_stmt ", asm_out_file);
26585 putc (is_stmt ? '1' : '0', asm_out_file);
26587 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26589 gcc_assert (discriminator > 0);
26590 fputs (" discriminator ", asm_out_file);
26591 fprint_ul (asm_out_file, (unsigned long) discriminator);
26593 putc ('\n', asm_out_file);
26595 else
26597 unsigned int label_num = ++line_info_label_num;
26599 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26601 push_dw_line_info_entry (table, LI_set_address, label_num);
26602 if (file_num != table->file_num)
26603 push_dw_line_info_entry (table, LI_set_file, file_num);
26604 if (discriminator != table->discrim_num)
26605 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26606 if (is_stmt != table->is_stmt)
26607 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26608 push_dw_line_info_entry (table, LI_set_line, line);
26609 if (debug_column_info)
26610 push_dw_line_info_entry (table, LI_set_column, column);
26613 table->file_num = file_num;
26614 table->line_num = line;
26615 table->column_num = column;
26616 table->discrim_num = discriminator;
26617 table->is_stmt = is_stmt;
26618 table->in_use = true;
26621 /* Record the beginning of a new source file. */
26623 static void
26624 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26626 if (flag_eliminate_dwarf2_dups)
26628 /* Record the beginning of the file for break_out_includes. */
26629 dw_die_ref bincl_die;
26631 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26632 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26635 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26637 macinfo_entry e;
26638 e.code = DW_MACINFO_start_file;
26639 e.lineno = lineno;
26640 e.info = ggc_strdup (filename);
26641 vec_safe_push (macinfo_table, e);
26645 /* Record the end of a source file. */
26647 static void
26648 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26650 if (flag_eliminate_dwarf2_dups)
26651 /* Record the end of the file for break_out_includes. */
26652 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26654 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26656 macinfo_entry e;
26657 e.code = DW_MACINFO_end_file;
26658 e.lineno = lineno;
26659 e.info = NULL;
26660 vec_safe_push (macinfo_table, e);
26664 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26665 the tail part of the directive line, i.e. the part which is past the
26666 initial whitespace, #, whitespace, directive-name, whitespace part. */
26668 static void
26669 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26670 const char *buffer ATTRIBUTE_UNUSED)
26672 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26674 macinfo_entry e;
26675 /* Insert a dummy first entry to be able to optimize the whole
26676 predefined macro block using DW_MACRO_import. */
26677 if (macinfo_table->is_empty () && lineno <= 1)
26679 e.code = 0;
26680 e.lineno = 0;
26681 e.info = NULL;
26682 vec_safe_push (macinfo_table, e);
26684 e.code = DW_MACINFO_define;
26685 e.lineno = lineno;
26686 e.info = ggc_strdup (buffer);
26687 vec_safe_push (macinfo_table, e);
26691 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26692 the tail part of the directive line, i.e. the part which is past the
26693 initial whitespace, #, whitespace, directive-name, whitespace part. */
26695 static void
26696 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26697 const char *buffer ATTRIBUTE_UNUSED)
26699 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26701 macinfo_entry e;
26702 /* Insert a dummy first entry to be able to optimize the whole
26703 predefined macro block using DW_MACRO_import. */
26704 if (macinfo_table->is_empty () && lineno <= 1)
26706 e.code = 0;
26707 e.lineno = 0;
26708 e.info = NULL;
26709 vec_safe_push (macinfo_table, e);
26711 e.code = DW_MACINFO_undef;
26712 e.lineno = lineno;
26713 e.info = ggc_strdup (buffer);
26714 vec_safe_push (macinfo_table, e);
26718 /* Helpers to manipulate hash table of CUs. */
26720 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26722 static inline hashval_t hash (const macinfo_entry *);
26723 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26726 inline hashval_t
26727 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26729 return htab_hash_string (entry->info);
26732 inline bool
26733 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26734 const macinfo_entry *entry2)
26736 return !strcmp (entry1->info, entry2->info);
26739 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26741 /* Output a single .debug_macinfo entry. */
26743 static void
26744 output_macinfo_op (macinfo_entry *ref)
26746 int file_num;
26747 size_t len;
26748 struct indirect_string_node *node;
26749 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26750 struct dwarf_file_data *fd;
26752 switch (ref->code)
26754 case DW_MACINFO_start_file:
26755 fd = lookup_filename (ref->info);
26756 file_num = maybe_emit_file (fd);
26757 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26758 dw2_asm_output_data_uleb128 (ref->lineno,
26759 "Included from line number %lu",
26760 (unsigned long) ref->lineno);
26761 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26762 break;
26763 case DW_MACINFO_end_file:
26764 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26765 break;
26766 case DW_MACINFO_define:
26767 case DW_MACINFO_undef:
26768 len = strlen (ref->info) + 1;
26769 if (!dwarf_strict
26770 && len > DWARF_OFFSET_SIZE
26771 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26772 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26774 ref->code = ref->code == DW_MACINFO_define
26775 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26776 output_macinfo_op (ref);
26777 return;
26779 dw2_asm_output_data (1, ref->code,
26780 ref->code == DW_MACINFO_define
26781 ? "Define macro" : "Undefine macro");
26782 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26783 (unsigned long) ref->lineno);
26784 dw2_asm_output_nstring (ref->info, -1, "The macro");
26785 break;
26786 case DW_MACRO_define_strp:
26787 case DW_MACRO_undef_strp:
26788 node = find_AT_string (ref->info);
26789 gcc_assert (node
26790 && (node->form == DW_FORM_strp
26791 || node->form == DW_FORM_GNU_str_index));
26792 dw2_asm_output_data (1, ref->code,
26793 ref->code == DW_MACRO_define_strp
26794 ? "Define macro strp"
26795 : "Undefine macro strp");
26796 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26797 (unsigned long) ref->lineno);
26798 if (node->form == DW_FORM_strp)
26799 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26800 debug_str_section, "The macro: \"%s\"",
26801 ref->info);
26802 else
26803 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26804 ref->info);
26805 break;
26806 case DW_MACRO_import:
26807 dw2_asm_output_data (1, ref->code, "Import");
26808 ASM_GENERATE_INTERNAL_LABEL (label,
26809 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26810 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26811 break;
26812 default:
26813 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26814 ASM_COMMENT_START, (unsigned long) ref->code);
26815 break;
26819 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26820 other compilation unit .debug_macinfo sections. IDX is the first
26821 index of a define/undef, return the number of ops that should be
26822 emitted in a comdat .debug_macinfo section and emit
26823 a DW_MACRO_import entry referencing it.
26824 If the define/undef entry should be emitted normally, return 0. */
26826 static unsigned
26827 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26828 macinfo_hash_type **macinfo_htab)
26830 macinfo_entry *first, *second, *cur, *inc;
26831 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26832 unsigned char checksum[16];
26833 struct md5_ctx ctx;
26834 char *grp_name, *tail;
26835 const char *base;
26836 unsigned int i, count, encoded_filename_len, linebuf_len;
26837 macinfo_entry **slot;
26839 first = &(*macinfo_table)[idx];
26840 second = &(*macinfo_table)[idx + 1];
26842 /* Optimize only if there are at least two consecutive define/undef ops,
26843 and either all of them are before first DW_MACINFO_start_file
26844 with lineno {0,1} (i.e. predefined macro block), or all of them are
26845 in some included header file. */
26846 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26847 return 0;
26848 if (vec_safe_is_empty (files))
26850 if (first->lineno > 1 || second->lineno > 1)
26851 return 0;
26853 else if (first->lineno == 0)
26854 return 0;
26856 /* Find the last define/undef entry that can be grouped together
26857 with first and at the same time compute md5 checksum of their
26858 codes, linenumbers and strings. */
26859 md5_init_ctx (&ctx);
26860 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26861 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26862 break;
26863 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26864 break;
26865 else
26867 unsigned char code = cur->code;
26868 md5_process_bytes (&code, 1, &ctx);
26869 checksum_uleb128 (cur->lineno, &ctx);
26870 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26872 md5_finish_ctx (&ctx, checksum);
26873 count = i - idx;
26875 /* From the containing include filename (if any) pick up just
26876 usable characters from its basename. */
26877 if (vec_safe_is_empty (files))
26878 base = "";
26879 else
26880 base = lbasename (files->last ().info);
26881 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26882 if (ISIDNUM (base[i]) || base[i] == '.')
26883 encoded_filename_len++;
26884 /* Count . at the end. */
26885 if (encoded_filename_len)
26886 encoded_filename_len++;
26888 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26889 linebuf_len = strlen (linebuf);
26891 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26892 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26893 + 16 * 2 + 1);
26894 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26895 tail = grp_name + 4;
26896 if (encoded_filename_len)
26898 for (i = 0; base[i]; i++)
26899 if (ISIDNUM (base[i]) || base[i] == '.')
26900 *tail++ = base[i];
26901 *tail++ = '.';
26903 memcpy (tail, linebuf, linebuf_len);
26904 tail += linebuf_len;
26905 *tail++ = '.';
26906 for (i = 0; i < 16; i++)
26907 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26909 /* Construct a macinfo_entry for DW_MACRO_import
26910 in the empty vector entry before the first define/undef. */
26911 inc = &(*macinfo_table)[idx - 1];
26912 inc->code = DW_MACRO_import;
26913 inc->lineno = 0;
26914 inc->info = ggc_strdup (grp_name);
26915 if (!*macinfo_htab)
26916 *macinfo_htab = new macinfo_hash_type (10);
26917 /* Avoid emitting duplicates. */
26918 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26919 if (*slot != NULL)
26921 inc->code = 0;
26922 inc->info = NULL;
26923 /* If such an entry has been used before, just emit
26924 a DW_MACRO_import op. */
26925 inc = *slot;
26926 output_macinfo_op (inc);
26927 /* And clear all macinfo_entry in the range to avoid emitting them
26928 in the second pass. */
26929 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26931 cur->code = 0;
26932 cur->info = NULL;
26935 else
26937 *slot = inc;
26938 inc->lineno = (*macinfo_htab)->elements ();
26939 output_macinfo_op (inc);
26941 return count;
26944 /* Save any strings needed by the macinfo table in the debug str
26945 table. All strings must be collected into the table by the time
26946 index_string is called. */
26948 static void
26949 save_macinfo_strings (void)
26951 unsigned len;
26952 unsigned i;
26953 macinfo_entry *ref;
26955 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
26957 switch (ref->code)
26959 /* Match the logic in output_macinfo_op to decide on
26960 indirect strings. */
26961 case DW_MACINFO_define:
26962 case DW_MACINFO_undef:
26963 len = strlen (ref->info) + 1;
26964 if (!dwarf_strict
26965 && len > DWARF_OFFSET_SIZE
26966 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26967 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26968 set_indirect_string (find_AT_string (ref->info));
26969 break;
26970 case DW_MACRO_define_strp:
26971 case DW_MACRO_undef_strp:
26972 set_indirect_string (find_AT_string (ref->info));
26973 break;
26974 default:
26975 break;
26980 /* Output macinfo section(s). */
26982 static void
26983 output_macinfo (void)
26985 unsigned i;
26986 unsigned long length = vec_safe_length (macinfo_table);
26987 macinfo_entry *ref;
26988 vec<macinfo_entry, va_gc> *files = NULL;
26989 macinfo_hash_type *macinfo_htab = NULL;
26991 if (! length)
26992 return;
26994 /* output_macinfo* uses these interchangeably. */
26995 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
26996 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
26997 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
26998 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27000 /* For .debug_macro emit the section header. */
27001 if (!dwarf_strict || dwarf_version >= 5)
27003 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27004 "DWARF macro version number");
27005 if (DWARF_OFFSET_SIZE == 8)
27006 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27007 else
27008 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27009 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27010 (!dwarf_split_debug_info ? debug_line_section_label
27011 : debug_skeleton_line_section_label),
27012 debug_line_section, NULL);
27015 /* In the first loop, it emits the primary .debug_macinfo section
27016 and after each emitted op the macinfo_entry is cleared.
27017 If a longer range of define/undef ops can be optimized using
27018 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27019 the vector before the first define/undef in the range and the
27020 whole range of define/undef ops is not emitted and kept. */
27021 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27023 switch (ref->code)
27025 case DW_MACINFO_start_file:
27026 vec_safe_push (files, *ref);
27027 break;
27028 case DW_MACINFO_end_file:
27029 if (!vec_safe_is_empty (files))
27030 files->pop ();
27031 break;
27032 case DW_MACINFO_define:
27033 case DW_MACINFO_undef:
27034 if ((!dwarf_strict || dwarf_version >= 5)
27035 && HAVE_COMDAT_GROUP
27036 && vec_safe_length (files) != 1
27037 && i > 0
27038 && i + 1 < length
27039 && (*macinfo_table)[i - 1].code == 0)
27041 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27042 if (count)
27044 i += count - 1;
27045 continue;
27048 break;
27049 case 0:
27050 /* A dummy entry may be inserted at the beginning to be able
27051 to optimize the whole block of predefined macros. */
27052 if (i == 0)
27053 continue;
27054 default:
27055 break;
27057 output_macinfo_op (ref);
27058 ref->info = NULL;
27059 ref->code = 0;
27062 if (!macinfo_htab)
27063 return;
27065 delete macinfo_htab;
27066 macinfo_htab = NULL;
27068 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27069 terminate the current chain and switch to a new comdat .debug_macinfo
27070 section and emit the define/undef entries within it. */
27071 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27072 switch (ref->code)
27074 case 0:
27075 continue;
27076 case DW_MACRO_import:
27078 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27079 tree comdat_key = get_identifier (ref->info);
27080 /* Terminate the previous .debug_macinfo section. */
27081 dw2_asm_output_data (1, 0, "End compilation unit");
27082 targetm.asm_out.named_section (debug_macinfo_section_name,
27083 SECTION_DEBUG
27084 | SECTION_LINKONCE,
27085 comdat_key);
27086 ASM_GENERATE_INTERNAL_LABEL (label,
27087 DEBUG_MACRO_SECTION_LABEL,
27088 ref->lineno);
27089 ASM_OUTPUT_LABEL (asm_out_file, label);
27090 ref->code = 0;
27091 ref->info = NULL;
27092 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27093 "DWARF macro version number");
27094 if (DWARF_OFFSET_SIZE == 8)
27095 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27096 else
27097 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27099 break;
27100 case DW_MACINFO_define:
27101 case DW_MACINFO_undef:
27102 output_macinfo_op (ref);
27103 ref->code = 0;
27104 ref->info = NULL;
27105 break;
27106 default:
27107 gcc_unreachable ();
27111 /* Initialize the various sections and labels for dwarf output. */
27113 static void
27114 init_sections_and_labels (void)
27116 if (!dwarf_split_debug_info)
27118 debug_info_section = get_section (DEBUG_INFO_SECTION,
27119 SECTION_DEBUG, NULL);
27120 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27121 SECTION_DEBUG, NULL);
27122 debug_loc_section = get_section (dwarf_version >= 5
27123 ? DEBUG_LOCLISTS_SECTION
27124 : DEBUG_LOC_SECTION,
27125 SECTION_DEBUG, NULL);
27126 debug_macinfo_section_name
27127 = (dwarf_strict && dwarf_version < 5)
27128 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27129 debug_macinfo_section = get_section (debug_macinfo_section_name,
27130 SECTION_DEBUG, NULL);
27132 else
27134 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27135 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27136 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27137 SECTION_DEBUG | SECTION_EXCLUDE,
27138 NULL);
27139 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27140 SECTION_DEBUG, NULL);
27141 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27142 SECTION_DEBUG, NULL);
27143 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27144 SECTION_DEBUG, NULL);
27145 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27146 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27148 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27149 the main .o, but the skeleton_line goes into the split off dwo. */
27150 debug_skeleton_line_section
27151 = get_section (DEBUG_DWO_LINE_SECTION,
27152 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27153 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27154 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27155 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27156 SECTION_DEBUG | SECTION_EXCLUDE,
27157 NULL);
27158 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27159 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27160 debug_loc_section = get_section (dwarf_version >= 5
27161 ? DEBUG_DWO_LOCLISTS_SECTION
27162 : DEBUG_DWO_LOC_SECTION,
27163 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27164 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27165 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27166 debug_macinfo_section_name
27167 = (dwarf_strict && dwarf_version < 5)
27168 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27169 debug_macinfo_section = get_section (debug_macinfo_section_name,
27170 SECTION_DEBUG | SECTION_EXCLUDE,
27171 NULL);
27173 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27174 SECTION_DEBUG, NULL);
27175 debug_line_section = get_section (DEBUG_LINE_SECTION,
27176 SECTION_DEBUG, NULL);
27177 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27178 SECTION_DEBUG, NULL);
27179 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27180 SECTION_DEBUG, NULL);
27181 debug_str_section = get_section (DEBUG_STR_SECTION,
27182 DEBUG_STR_SECTION_FLAGS, NULL);
27183 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27184 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27185 DEBUG_STR_SECTION_FLAGS, NULL);
27187 debug_ranges_section = get_section (dwarf_version >= 5
27188 ? DEBUG_RNGLISTS_SECTION
27189 : DEBUG_RANGES_SECTION,
27190 SECTION_DEBUG, NULL);
27191 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27192 SECTION_DEBUG, NULL);
27194 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27195 DEBUG_ABBREV_SECTION_LABEL, 0);
27196 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27197 DEBUG_INFO_SECTION_LABEL, 0);
27198 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27199 DEBUG_LINE_SECTION_LABEL, 0);
27200 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27201 DEBUG_RANGES_SECTION_LABEL, 0);
27202 if (dwarf_version >= 5 && dwarf_split_debug_info)
27203 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27204 DEBUG_RANGES_SECTION_LABEL, 1);
27205 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27206 DEBUG_ADDR_SECTION_LABEL, 0);
27207 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27208 (dwarf_strict && dwarf_version < 5)
27209 ? DEBUG_MACINFO_SECTION_LABEL
27210 : DEBUG_MACRO_SECTION_LABEL, 0);
27211 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27214 /* Set up for Dwarf output at the start of compilation. */
27216 static void
27217 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27219 /* This option is currently broken, see (PR53118 and PR46102). */
27220 if (flag_eliminate_dwarf2_dups
27221 && strstr (lang_hooks.name, "C++"))
27223 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27224 flag_eliminate_dwarf2_dups = 0;
27227 /* Allocate the file_table. */
27228 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27230 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27231 /* Allocate the decl_die_table. */
27232 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27234 /* Allocate the decl_loc_table. */
27235 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27237 /* Allocate the cached_dw_loc_list_table. */
27238 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27240 /* Allocate the initial hunk of the decl_scope_table. */
27241 vec_alloc (decl_scope_table, 256);
27243 /* Allocate the initial hunk of the abbrev_die_table. */
27244 vec_alloc (abbrev_die_table, 256);
27245 /* Zero-th entry is allocated, but unused. */
27246 abbrev_die_table->quick_push (NULL);
27248 /* Allocate the dwarf_proc_stack_usage_map. */
27249 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27251 /* Allocate the pubtypes and pubnames vectors. */
27252 vec_alloc (pubname_table, 32);
27253 vec_alloc (pubtype_table, 32);
27255 vec_alloc (incomplete_types, 64);
27257 vec_alloc (used_rtx_array, 32);
27259 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27260 vec_alloc (macinfo_table, 64);
27261 #endif
27263 /* If front-ends already registered a main translation unit but we were not
27264 ready to perform the association, do this now. */
27265 if (main_translation_unit != NULL_TREE)
27266 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27269 /* Called before compile () starts outputtting functions, variables
27270 and toplevel asms into assembly. */
27272 static void
27273 dwarf2out_assembly_start (void)
27275 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27276 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27277 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27278 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27279 COLD_TEXT_SECTION_LABEL, 0);
27280 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27282 switch_to_section (text_section);
27283 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27284 #endif
27286 /* Make sure the line number table for .text always exists. */
27287 text_section_line_info = new_line_info_table ();
27288 text_section_line_info->end_label = text_end_label;
27290 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27291 cur_line_info_table = text_section_line_info;
27292 #endif
27294 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27295 && dwarf2out_do_cfi_asm ()
27296 && (!(flag_unwind_tables || flag_exceptions)
27297 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27298 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27301 /* A helper function for dwarf2out_finish called through
27302 htab_traverse. Assign a string its index. All strings must be
27303 collected into the table by the time index_string is called,
27304 because the indexing code relies on htab_traverse to traverse nodes
27305 in the same order for each run. */
27308 index_string (indirect_string_node **h, unsigned int *index)
27310 indirect_string_node *node = *h;
27312 find_string_form (node);
27313 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27315 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27316 node->index = *index;
27317 *index += 1;
27319 return 1;
27322 /* A helper function for output_indirect_strings called through
27323 htab_traverse. Output the offset to a string and update the
27324 current offset. */
27327 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27329 indirect_string_node *node = *h;
27331 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27333 /* Assert that this node has been assigned an index. */
27334 gcc_assert (node->index != NO_INDEX_ASSIGNED
27335 && node->index != NOT_INDEXED);
27336 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27337 "indexed string 0x%x: %s", node->index, node->str);
27338 *offset += strlen (node->str) + 1;
27340 return 1;
27343 /* A helper function for dwarf2out_finish called through
27344 htab_traverse. Output the indexed string. */
27347 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27349 struct indirect_string_node *node = *h;
27351 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27353 /* Assert that the strings are output in the same order as their
27354 indexes were assigned. */
27355 gcc_assert (*cur_idx == node->index);
27356 assemble_string (node->str, strlen (node->str) + 1);
27357 *cur_idx += 1;
27359 return 1;
27362 /* A helper function for dwarf2out_finish called through
27363 htab_traverse. Emit one queued .debug_str string. */
27366 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27368 struct indirect_string_node *node = *h;
27370 node->form = find_string_form (node);
27371 if (node->form == form && node->refcount > 0)
27373 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27374 assemble_string (node->str, strlen (node->str) + 1);
27377 return 1;
27380 /* Output the indexed string table. */
27382 static void
27383 output_indirect_strings (void)
27385 switch_to_section (debug_str_section);
27386 if (!dwarf_split_debug_info)
27387 debug_str_hash->traverse<enum dwarf_form,
27388 output_indirect_string> (DW_FORM_strp);
27389 else
27391 unsigned int offset = 0;
27392 unsigned int cur_idx = 0;
27394 skeleton_debug_str_hash->traverse<enum dwarf_form,
27395 output_indirect_string> (DW_FORM_strp);
27397 switch_to_section (debug_str_offsets_section);
27398 debug_str_hash->traverse_noresize
27399 <unsigned int *, output_index_string_offset> (&offset);
27400 switch_to_section (debug_str_dwo_section);
27401 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27402 (&cur_idx);
27406 /* Callback for htab_traverse to assign an index to an entry in the
27407 table, and to write that entry to the .debug_addr section. */
27410 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27412 addr_table_entry *entry = *slot;
27414 if (entry->refcount == 0)
27416 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27417 || entry->index == NOT_INDEXED);
27418 return 1;
27421 gcc_assert (entry->index == *cur_index);
27422 (*cur_index)++;
27424 switch (entry->kind)
27426 case ate_kind_rtx:
27427 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27428 "0x%x", entry->index);
27429 break;
27430 case ate_kind_rtx_dtprel:
27431 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27432 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27433 DWARF2_ADDR_SIZE,
27434 entry->addr.rtl);
27435 fputc ('\n', asm_out_file);
27436 break;
27437 case ate_kind_label:
27438 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27439 "0x%x", entry->index);
27440 break;
27441 default:
27442 gcc_unreachable ();
27444 return 1;
27447 /* Produce the .debug_addr section. */
27449 static void
27450 output_addr_table (void)
27452 unsigned int index = 0;
27453 if (addr_index_table == NULL || addr_index_table->size () == 0)
27454 return;
27456 switch_to_section (debug_addr_section);
27457 addr_index_table
27458 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27461 #if ENABLE_ASSERT_CHECKING
27462 /* Verify that all marks are clear. */
27464 static void
27465 verify_marks_clear (dw_die_ref die)
27467 dw_die_ref c;
27469 gcc_assert (! die->die_mark);
27470 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27472 #endif /* ENABLE_ASSERT_CHECKING */
27474 /* Clear the marks for a die and its children.
27475 Be cool if the mark isn't set. */
27477 static void
27478 prune_unmark_dies (dw_die_ref die)
27480 dw_die_ref c;
27482 if (die->die_mark)
27483 die->die_mark = 0;
27484 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27487 /* Given LOC that is referenced by a DIE we're marking as used, find all
27488 referenced DWARF procedures it references and mark them as used. */
27490 static void
27491 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27493 for (; loc != NULL; loc = loc->dw_loc_next)
27494 switch (loc->dw_loc_opc)
27496 case DW_OP_implicit_pointer:
27497 case DW_OP_convert:
27498 case DW_OP_reinterpret:
27499 case DW_OP_GNU_implicit_pointer:
27500 case DW_OP_GNU_convert:
27501 case DW_OP_GNU_reinterpret:
27502 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27503 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27504 break;
27505 case DW_OP_GNU_variable_value:
27506 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27508 dw_die_ref ref
27509 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27510 if (ref == NULL)
27511 break;
27512 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27513 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27514 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27516 /* FALLTHRU */
27517 case DW_OP_call2:
27518 case DW_OP_call4:
27519 case DW_OP_call_ref:
27520 case DW_OP_const_type:
27521 case DW_OP_GNU_const_type:
27522 case DW_OP_GNU_parameter_ref:
27523 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27524 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27525 break;
27526 case DW_OP_regval_type:
27527 case DW_OP_deref_type:
27528 case DW_OP_GNU_regval_type:
27529 case DW_OP_GNU_deref_type:
27530 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27531 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27532 break;
27533 case DW_OP_entry_value:
27534 case DW_OP_GNU_entry_value:
27535 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27536 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27537 break;
27538 default:
27539 break;
27543 /* Given DIE that we're marking as used, find any other dies
27544 it references as attributes and mark them as used. */
27546 static void
27547 prune_unused_types_walk_attribs (dw_die_ref die)
27549 dw_attr_node *a;
27550 unsigned ix;
27552 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27554 switch (AT_class (a))
27556 /* Make sure DWARF procedures referenced by location descriptions will
27557 get emitted. */
27558 case dw_val_class_loc:
27559 prune_unused_types_walk_loc_descr (AT_loc (a));
27560 break;
27561 case dw_val_class_loc_list:
27562 for (dw_loc_list_ref list = AT_loc_list (a);
27563 list != NULL;
27564 list = list->dw_loc_next)
27565 prune_unused_types_walk_loc_descr (list->expr);
27566 break;
27568 case dw_val_class_die_ref:
27569 /* A reference to another DIE.
27570 Make sure that it will get emitted.
27571 If it was broken out into a comdat group, don't follow it. */
27572 if (! AT_ref (a)->comdat_type_p
27573 || a->dw_attr == DW_AT_specification)
27574 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27575 break;
27577 case dw_val_class_str:
27578 /* Set the string's refcount to 0 so that prune_unused_types_mark
27579 accounts properly for it. */
27580 a->dw_attr_val.v.val_str->refcount = 0;
27581 break;
27583 default:
27584 break;
27589 /* Mark the generic parameters and arguments children DIEs of DIE. */
27591 static void
27592 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27594 dw_die_ref c;
27596 if (die == NULL || die->die_child == NULL)
27597 return;
27598 c = die->die_child;
27601 if (is_template_parameter (c))
27602 prune_unused_types_mark (c, 1);
27603 c = c->die_sib;
27604 } while (c && c != die->die_child);
27607 /* Mark DIE as being used. If DOKIDS is true, then walk down
27608 to DIE's children. */
27610 static void
27611 prune_unused_types_mark (dw_die_ref die, int dokids)
27613 dw_die_ref c;
27615 if (die->die_mark == 0)
27617 /* We haven't done this node yet. Mark it as used. */
27618 die->die_mark = 1;
27619 /* If this is the DIE of a generic type instantiation,
27620 mark the children DIEs that describe its generic parms and
27621 args. */
27622 prune_unused_types_mark_generic_parms_dies (die);
27624 /* We also have to mark its parents as used.
27625 (But we don't want to mark our parent's kids due to this,
27626 unless it is a class.) */
27627 if (die->die_parent)
27628 prune_unused_types_mark (die->die_parent,
27629 class_scope_p (die->die_parent));
27631 /* Mark any referenced nodes. */
27632 prune_unused_types_walk_attribs (die);
27634 /* If this node is a specification,
27635 also mark the definition, if it exists. */
27636 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27637 prune_unused_types_mark (die->die_definition, 1);
27640 if (dokids && die->die_mark != 2)
27642 /* We need to walk the children, but haven't done so yet.
27643 Remember that we've walked the kids. */
27644 die->die_mark = 2;
27646 /* If this is an array type, we need to make sure our
27647 kids get marked, even if they're types. If we're
27648 breaking out types into comdat sections, do this
27649 for all type definitions. */
27650 if (die->die_tag == DW_TAG_array_type
27651 || (use_debug_types
27652 && is_type_die (die) && ! is_declaration_die (die)))
27653 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27654 else
27655 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27659 /* For local classes, look if any static member functions were emitted
27660 and if so, mark them. */
27662 static void
27663 prune_unused_types_walk_local_classes (dw_die_ref die)
27665 dw_die_ref c;
27667 if (die->die_mark == 2)
27668 return;
27670 switch (die->die_tag)
27672 case DW_TAG_structure_type:
27673 case DW_TAG_union_type:
27674 case DW_TAG_class_type:
27675 break;
27677 case DW_TAG_subprogram:
27678 if (!get_AT_flag (die, DW_AT_declaration)
27679 || die->die_definition != NULL)
27680 prune_unused_types_mark (die, 1);
27681 return;
27683 default:
27684 return;
27687 /* Mark children. */
27688 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27691 /* Walk the tree DIE and mark types that we actually use. */
27693 static void
27694 prune_unused_types_walk (dw_die_ref die)
27696 dw_die_ref c;
27698 /* Don't do anything if this node is already marked and
27699 children have been marked as well. */
27700 if (die->die_mark == 2)
27701 return;
27703 switch (die->die_tag)
27705 case DW_TAG_structure_type:
27706 case DW_TAG_union_type:
27707 case DW_TAG_class_type:
27708 if (die->die_perennial_p)
27709 break;
27711 for (c = die->die_parent; c; c = c->die_parent)
27712 if (c->die_tag == DW_TAG_subprogram)
27713 break;
27715 /* Finding used static member functions inside of classes
27716 is needed just for local classes, because for other classes
27717 static member function DIEs with DW_AT_specification
27718 are emitted outside of the DW_TAG_*_type. If we ever change
27719 it, we'd need to call this even for non-local classes. */
27720 if (c)
27721 prune_unused_types_walk_local_classes (die);
27723 /* It's a type node --- don't mark it. */
27724 return;
27726 case DW_TAG_const_type:
27727 case DW_TAG_packed_type:
27728 case DW_TAG_pointer_type:
27729 case DW_TAG_reference_type:
27730 case DW_TAG_rvalue_reference_type:
27731 case DW_TAG_volatile_type:
27732 case DW_TAG_typedef:
27733 case DW_TAG_array_type:
27734 case DW_TAG_interface_type:
27735 case DW_TAG_friend:
27736 case DW_TAG_enumeration_type:
27737 case DW_TAG_subroutine_type:
27738 case DW_TAG_string_type:
27739 case DW_TAG_set_type:
27740 case DW_TAG_subrange_type:
27741 case DW_TAG_ptr_to_member_type:
27742 case DW_TAG_file_type:
27743 /* Type nodes are useful only when other DIEs reference them --- don't
27744 mark them. */
27745 /* FALLTHROUGH */
27747 case DW_TAG_dwarf_procedure:
27748 /* Likewise for DWARF procedures. */
27750 if (die->die_perennial_p)
27751 break;
27753 return;
27755 default:
27756 /* Mark everything else. */
27757 break;
27760 if (die->die_mark == 0)
27762 die->die_mark = 1;
27764 /* Now, mark any dies referenced from here. */
27765 prune_unused_types_walk_attribs (die);
27768 die->die_mark = 2;
27770 /* Mark children. */
27771 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27774 /* Increment the string counts on strings referred to from DIE's
27775 attributes. */
27777 static void
27778 prune_unused_types_update_strings (dw_die_ref die)
27780 dw_attr_node *a;
27781 unsigned ix;
27783 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27784 if (AT_class (a) == dw_val_class_str)
27786 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27787 s->refcount++;
27788 /* Avoid unnecessarily putting strings that are used less than
27789 twice in the hash table. */
27790 if (s->refcount
27791 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27793 indirect_string_node **slot
27794 = debug_str_hash->find_slot_with_hash (s->str,
27795 htab_hash_string (s->str),
27796 INSERT);
27797 gcc_assert (*slot == NULL);
27798 *slot = s;
27803 /* Mark DIE and its children as removed. */
27805 static void
27806 mark_removed (dw_die_ref die)
27808 dw_die_ref c;
27809 die->removed = true;
27810 FOR_EACH_CHILD (die, c, mark_removed (c));
27813 /* Remove from the tree DIE any dies that aren't marked. */
27815 static void
27816 prune_unused_types_prune (dw_die_ref die)
27818 dw_die_ref c;
27820 gcc_assert (die->die_mark);
27821 prune_unused_types_update_strings (die);
27823 if (! die->die_child)
27824 return;
27826 c = die->die_child;
27827 do {
27828 dw_die_ref prev = c, next;
27829 for (c = c->die_sib; ! c->die_mark; c = next)
27830 if (c == die->die_child)
27832 /* No marked children between 'prev' and the end of the list. */
27833 if (prev == c)
27834 /* No marked children at all. */
27835 die->die_child = NULL;
27836 else
27838 prev->die_sib = c->die_sib;
27839 die->die_child = prev;
27841 c->die_sib = NULL;
27842 mark_removed (c);
27843 return;
27845 else
27847 next = c->die_sib;
27848 c->die_sib = NULL;
27849 mark_removed (c);
27852 if (c != prev->die_sib)
27853 prev->die_sib = c;
27854 prune_unused_types_prune (c);
27855 } while (c != die->die_child);
27858 /* Remove dies representing declarations that we never use. */
27860 static void
27861 prune_unused_types (void)
27863 unsigned int i;
27864 limbo_die_node *node;
27865 comdat_type_node *ctnode;
27866 pubname_entry *pub;
27867 dw_die_ref base_type;
27869 #if ENABLE_ASSERT_CHECKING
27870 /* All the marks should already be clear. */
27871 verify_marks_clear (comp_unit_die ());
27872 for (node = limbo_die_list; node; node = node->next)
27873 verify_marks_clear (node->die);
27874 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27875 verify_marks_clear (ctnode->root_die);
27876 #endif /* ENABLE_ASSERT_CHECKING */
27878 /* Mark types that are used in global variables. */
27879 premark_types_used_by_global_vars ();
27881 /* Set the mark on nodes that are actually used. */
27882 prune_unused_types_walk (comp_unit_die ());
27883 for (node = limbo_die_list; node; node = node->next)
27884 prune_unused_types_walk (node->die);
27885 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27887 prune_unused_types_walk (ctnode->root_die);
27888 prune_unused_types_mark (ctnode->type_die, 1);
27891 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
27892 are unusual in that they are pubnames that are the children of pubtypes.
27893 They should only be marked via their parent DW_TAG_enumeration_type die,
27894 not as roots in themselves. */
27895 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
27896 if (pub->die->die_tag != DW_TAG_enumerator)
27897 prune_unused_types_mark (pub->die, 1);
27898 for (i = 0; base_types.iterate (i, &base_type); i++)
27899 prune_unused_types_mark (base_type, 1);
27901 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
27902 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
27903 callees). */
27904 cgraph_node *cnode;
27905 FOR_EACH_FUNCTION (cnode)
27906 if (cnode->referred_to_p (false))
27908 dw_die_ref die = lookup_decl_die (cnode->decl);
27909 if (die == NULL || die->die_mark)
27910 continue;
27911 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
27912 if (e->caller != cnode
27913 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
27915 prune_unused_types_mark (die, 1);
27916 break;
27920 if (debug_str_hash)
27921 debug_str_hash->empty ();
27922 if (skeleton_debug_str_hash)
27923 skeleton_debug_str_hash->empty ();
27924 prune_unused_types_prune (comp_unit_die ());
27925 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
27927 node = *pnode;
27928 if (!node->die->die_mark)
27929 *pnode = node->next;
27930 else
27932 prune_unused_types_prune (node->die);
27933 pnode = &node->next;
27936 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27937 prune_unused_types_prune (ctnode->root_die);
27939 /* Leave the marks clear. */
27940 prune_unmark_dies (comp_unit_die ());
27941 for (node = limbo_die_list; node; node = node->next)
27942 prune_unmark_dies (node->die);
27943 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
27944 prune_unmark_dies (ctnode->root_die);
27947 /* Helpers to manipulate hash table of comdat type units. */
27949 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
27951 static inline hashval_t hash (const comdat_type_node *);
27952 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
27955 inline hashval_t
27956 comdat_type_hasher::hash (const comdat_type_node *type_node)
27958 hashval_t h;
27959 memcpy (&h, type_node->signature, sizeof (h));
27960 return h;
27963 inline bool
27964 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
27965 const comdat_type_node *type_node_2)
27967 return (! memcmp (type_node_1->signature, type_node_2->signature,
27968 DWARF_TYPE_SIGNATURE_SIZE));
27971 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
27972 to the location it would have been added, should we know its
27973 DECL_ASSEMBLER_NAME when we added other attributes. This will
27974 probably improve compactness of debug info, removing equivalent
27975 abbrevs, and hide any differences caused by deferring the
27976 computation of the assembler name, triggered by e.g. PCH. */
27978 static inline void
27979 move_linkage_attr (dw_die_ref die)
27981 unsigned ix = vec_safe_length (die->die_attr);
27982 dw_attr_node linkage = (*die->die_attr)[ix - 1];
27984 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
27985 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
27987 while (--ix > 0)
27989 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
27991 if (prev->dw_attr == DW_AT_decl_line
27992 || prev->dw_attr == DW_AT_decl_column
27993 || prev->dw_attr == DW_AT_name)
27994 break;
27997 if (ix != vec_safe_length (die->die_attr) - 1)
27999 die->die_attr->pop ();
28000 die->die_attr->quick_insert (ix, linkage);
28004 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28005 referenced from typed stack ops and count how often they are used. */
28007 static void
28008 mark_base_types (dw_loc_descr_ref loc)
28010 dw_die_ref base_type = NULL;
28012 for (; loc; loc = loc->dw_loc_next)
28014 switch (loc->dw_loc_opc)
28016 case DW_OP_regval_type:
28017 case DW_OP_deref_type:
28018 case DW_OP_GNU_regval_type:
28019 case DW_OP_GNU_deref_type:
28020 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28021 break;
28022 case DW_OP_convert:
28023 case DW_OP_reinterpret:
28024 case DW_OP_GNU_convert:
28025 case DW_OP_GNU_reinterpret:
28026 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28027 continue;
28028 /* FALLTHRU */
28029 case DW_OP_const_type:
28030 case DW_OP_GNU_const_type:
28031 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28032 break;
28033 case DW_OP_entry_value:
28034 case DW_OP_GNU_entry_value:
28035 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28036 continue;
28037 default:
28038 continue;
28040 gcc_assert (base_type->die_parent == comp_unit_die ());
28041 if (base_type->die_mark)
28042 base_type->die_mark++;
28043 else
28045 base_types.safe_push (base_type);
28046 base_type->die_mark = 1;
28051 /* Comparison function for sorting marked base types. */
28053 static int
28054 base_type_cmp (const void *x, const void *y)
28056 dw_die_ref dx = *(const dw_die_ref *) x;
28057 dw_die_ref dy = *(const dw_die_ref *) y;
28058 unsigned int byte_size1, byte_size2;
28059 unsigned int encoding1, encoding2;
28060 unsigned int align1, align2;
28061 if (dx->die_mark > dy->die_mark)
28062 return -1;
28063 if (dx->die_mark < dy->die_mark)
28064 return 1;
28065 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28066 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28067 if (byte_size1 < byte_size2)
28068 return 1;
28069 if (byte_size1 > byte_size2)
28070 return -1;
28071 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28072 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28073 if (encoding1 < encoding2)
28074 return 1;
28075 if (encoding1 > encoding2)
28076 return -1;
28077 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28078 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28079 if (align1 < align2)
28080 return 1;
28081 if (align1 > align2)
28082 return -1;
28083 return 0;
28086 /* Move base types marked by mark_base_types as early as possible
28087 in the CU, sorted by decreasing usage count both to make the
28088 uleb128 references as small as possible and to make sure they
28089 will have die_offset already computed by calc_die_sizes when
28090 sizes of typed stack loc ops is computed. */
28092 static void
28093 move_marked_base_types (void)
28095 unsigned int i;
28096 dw_die_ref base_type, die, c;
28098 if (base_types.is_empty ())
28099 return;
28101 /* Sort by decreasing usage count, they will be added again in that
28102 order later on. */
28103 base_types.qsort (base_type_cmp);
28104 die = comp_unit_die ();
28105 c = die->die_child;
28108 dw_die_ref prev = c;
28109 c = c->die_sib;
28110 while (c->die_mark)
28112 remove_child_with_prev (c, prev);
28113 /* As base types got marked, there must be at least
28114 one node other than DW_TAG_base_type. */
28115 gcc_assert (die->die_child != NULL);
28116 c = prev->die_sib;
28119 while (c != die->die_child);
28120 gcc_assert (die->die_child);
28121 c = die->die_child;
28122 for (i = 0; base_types.iterate (i, &base_type); i++)
28124 base_type->die_mark = 0;
28125 base_type->die_sib = c->die_sib;
28126 c->die_sib = base_type;
28127 c = base_type;
28131 /* Helper function for resolve_addr, attempt to resolve
28132 one CONST_STRING, return true if successful. Similarly verify that
28133 SYMBOL_REFs refer to variables emitted in the current CU. */
28135 static bool
28136 resolve_one_addr (rtx *addr)
28138 rtx rtl = *addr;
28140 if (GET_CODE (rtl) == CONST_STRING)
28142 size_t len = strlen (XSTR (rtl, 0)) + 1;
28143 tree t = build_string (len, XSTR (rtl, 0));
28144 tree tlen = size_int (len - 1);
28145 TREE_TYPE (t)
28146 = build_array_type (char_type_node, build_index_type (tlen));
28147 rtl = lookup_constant_def (t);
28148 if (!rtl || !MEM_P (rtl))
28149 return false;
28150 rtl = XEXP (rtl, 0);
28151 if (GET_CODE (rtl) == SYMBOL_REF
28152 && SYMBOL_REF_DECL (rtl)
28153 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28154 return false;
28155 vec_safe_push (used_rtx_array, rtl);
28156 *addr = rtl;
28157 return true;
28160 if (GET_CODE (rtl) == SYMBOL_REF
28161 && SYMBOL_REF_DECL (rtl))
28163 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28165 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28166 return false;
28168 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28169 return false;
28172 if (GET_CODE (rtl) == CONST)
28174 subrtx_ptr_iterator::array_type array;
28175 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28176 if (!resolve_one_addr (*iter))
28177 return false;
28180 return true;
28183 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28184 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28185 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28187 static rtx
28188 string_cst_pool_decl (tree t)
28190 rtx rtl = output_constant_def (t, 1);
28191 unsigned char *array;
28192 dw_loc_descr_ref l;
28193 tree decl;
28194 size_t len;
28195 dw_die_ref ref;
28197 if (!rtl || !MEM_P (rtl))
28198 return NULL_RTX;
28199 rtl = XEXP (rtl, 0);
28200 if (GET_CODE (rtl) != SYMBOL_REF
28201 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28202 return NULL_RTX;
28204 decl = SYMBOL_REF_DECL (rtl);
28205 if (!lookup_decl_die (decl))
28207 len = TREE_STRING_LENGTH (t);
28208 vec_safe_push (used_rtx_array, rtl);
28209 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28210 array = ggc_vec_alloc<unsigned char> (len);
28211 memcpy (array, TREE_STRING_POINTER (t), len);
28212 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28213 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28214 l->dw_loc_oprnd2.v.val_vec.length = len;
28215 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28216 l->dw_loc_oprnd2.v.val_vec.array = array;
28217 add_AT_loc (ref, DW_AT_location, l);
28218 equate_decl_number_to_die (decl, ref);
28220 return rtl;
28223 /* Helper function of resolve_addr_in_expr. LOC is
28224 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28225 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28226 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28227 with DW_OP_implicit_pointer if possible
28228 and return true, if unsuccessful, return false. */
28230 static bool
28231 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28233 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28234 HOST_WIDE_INT offset = 0;
28235 dw_die_ref ref = NULL;
28236 tree decl;
28238 if (GET_CODE (rtl) == CONST
28239 && GET_CODE (XEXP (rtl, 0)) == PLUS
28240 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28242 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28243 rtl = XEXP (XEXP (rtl, 0), 0);
28245 if (GET_CODE (rtl) == CONST_STRING)
28247 size_t len = strlen (XSTR (rtl, 0)) + 1;
28248 tree t = build_string (len, XSTR (rtl, 0));
28249 tree tlen = size_int (len - 1);
28251 TREE_TYPE (t)
28252 = build_array_type (char_type_node, build_index_type (tlen));
28253 rtl = string_cst_pool_decl (t);
28254 if (!rtl)
28255 return false;
28257 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28259 decl = SYMBOL_REF_DECL (rtl);
28260 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28262 ref = lookup_decl_die (decl);
28263 if (ref && (get_AT (ref, DW_AT_location)
28264 || get_AT (ref, DW_AT_const_value)))
28266 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28267 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28268 loc->dw_loc_oprnd1.val_entry = NULL;
28269 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28270 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28271 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28272 loc->dw_loc_oprnd2.v.val_int = offset;
28273 return true;
28277 return false;
28280 /* Helper function for resolve_addr, handle one location
28281 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28282 the location list couldn't be resolved. */
28284 static bool
28285 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28287 dw_loc_descr_ref keep = NULL;
28288 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28289 switch (loc->dw_loc_opc)
28291 case DW_OP_addr:
28292 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28294 if ((prev == NULL
28295 || prev->dw_loc_opc == DW_OP_piece
28296 || prev->dw_loc_opc == DW_OP_bit_piece)
28297 && loc->dw_loc_next
28298 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28299 && (!dwarf_strict || dwarf_version >= 5)
28300 && optimize_one_addr_into_implicit_ptr (loc))
28301 break;
28302 return false;
28304 break;
28305 case DW_OP_GNU_addr_index:
28306 case DW_OP_GNU_const_index:
28307 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28308 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28310 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28311 if (!resolve_one_addr (&rtl))
28312 return false;
28313 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28314 loc->dw_loc_oprnd1.val_entry
28315 = add_addr_table_entry (rtl, ate_kind_rtx);
28317 break;
28318 case DW_OP_const4u:
28319 case DW_OP_const8u:
28320 if (loc->dtprel
28321 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28322 return false;
28323 break;
28324 case DW_OP_plus_uconst:
28325 if (size_of_loc_descr (loc)
28326 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28328 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28330 dw_loc_descr_ref repl
28331 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28332 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28333 add_loc_descr (&repl, loc->dw_loc_next);
28334 *loc = *repl;
28336 break;
28337 case DW_OP_implicit_value:
28338 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28339 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28340 return false;
28341 break;
28342 case DW_OP_implicit_pointer:
28343 case DW_OP_GNU_implicit_pointer:
28344 case DW_OP_GNU_parameter_ref:
28345 case DW_OP_GNU_variable_value:
28346 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28348 dw_die_ref ref
28349 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28350 if (ref == NULL)
28351 return false;
28352 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28353 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28354 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28356 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28358 if (prev == NULL
28359 && loc->dw_loc_next == NULL
28360 && AT_class (a) == dw_val_class_loc)
28361 switch (a->dw_attr)
28363 /* Following attributes allow both exprloc and reference,
28364 so if the whole expression is DW_OP_GNU_variable_value
28365 alone we could transform it into reference. */
28366 case DW_AT_byte_size:
28367 case DW_AT_bit_size:
28368 case DW_AT_lower_bound:
28369 case DW_AT_upper_bound:
28370 case DW_AT_bit_stride:
28371 case DW_AT_count:
28372 case DW_AT_allocated:
28373 case DW_AT_associated:
28374 case DW_AT_byte_stride:
28375 a->dw_attr_val.val_class = dw_val_class_die_ref;
28376 a->dw_attr_val.val_entry = NULL;
28377 a->dw_attr_val.v.val_die_ref.die
28378 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28379 a->dw_attr_val.v.val_die_ref.external = 0;
28380 return true;
28381 default:
28382 break;
28384 if (dwarf_strict)
28385 return false;
28387 break;
28388 case DW_OP_const_type:
28389 case DW_OP_regval_type:
28390 case DW_OP_deref_type:
28391 case DW_OP_convert:
28392 case DW_OP_reinterpret:
28393 case DW_OP_GNU_const_type:
28394 case DW_OP_GNU_regval_type:
28395 case DW_OP_GNU_deref_type:
28396 case DW_OP_GNU_convert:
28397 case DW_OP_GNU_reinterpret:
28398 while (loc->dw_loc_next
28399 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28400 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28402 dw_die_ref base1, base2;
28403 unsigned enc1, enc2, size1, size2;
28404 if (loc->dw_loc_opc == DW_OP_regval_type
28405 || loc->dw_loc_opc == DW_OP_deref_type
28406 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28407 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28408 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28409 else if (loc->dw_loc_oprnd1.val_class
28410 == dw_val_class_unsigned_const)
28411 break;
28412 else
28413 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28414 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28415 == dw_val_class_unsigned_const)
28416 break;
28417 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28418 gcc_assert (base1->die_tag == DW_TAG_base_type
28419 && base2->die_tag == DW_TAG_base_type);
28420 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28421 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28422 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28423 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28424 if (size1 == size2
28425 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28426 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28427 && loc != keep)
28428 || enc1 == enc2))
28430 /* Optimize away next DW_OP_convert after
28431 adjusting LOC's base type die reference. */
28432 if (loc->dw_loc_opc == DW_OP_regval_type
28433 || loc->dw_loc_opc == DW_OP_deref_type
28434 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28435 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28436 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28437 else
28438 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28439 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28440 continue;
28442 /* Don't change integer DW_OP_convert after e.g. floating
28443 point typed stack entry. */
28444 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28445 keep = loc->dw_loc_next;
28446 break;
28448 break;
28449 default:
28450 break;
28452 return true;
28455 /* Helper function of resolve_addr. DIE had DW_AT_location of
28456 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28457 and DW_OP_addr couldn't be resolved. resolve_addr has already
28458 removed the DW_AT_location attribute. This function attempts to
28459 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28460 to it or DW_AT_const_value attribute, if possible. */
28462 static void
28463 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28465 if (!VAR_P (decl)
28466 || lookup_decl_die (decl) != die
28467 || DECL_EXTERNAL (decl)
28468 || !TREE_STATIC (decl)
28469 || DECL_INITIAL (decl) == NULL_TREE
28470 || DECL_P (DECL_INITIAL (decl))
28471 || get_AT (die, DW_AT_const_value))
28472 return;
28474 tree init = DECL_INITIAL (decl);
28475 HOST_WIDE_INT offset = 0;
28476 /* For variables that have been optimized away and thus
28477 don't have a memory location, see if we can emit
28478 DW_AT_const_value instead. */
28479 if (tree_add_const_value_attribute (die, init))
28480 return;
28481 if (dwarf_strict && dwarf_version < 5)
28482 return;
28483 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28484 and ADDR_EXPR refers to a decl that has DW_AT_location or
28485 DW_AT_const_value (but isn't addressable, otherwise
28486 resolving the original DW_OP_addr wouldn't fail), see if
28487 we can add DW_OP_implicit_pointer. */
28488 STRIP_NOPS (init);
28489 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28490 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28492 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28493 init = TREE_OPERAND (init, 0);
28494 STRIP_NOPS (init);
28496 if (TREE_CODE (init) != ADDR_EXPR)
28497 return;
28498 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28499 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28500 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28501 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28502 && TREE_OPERAND (init, 0) != decl))
28504 dw_die_ref ref;
28505 dw_loc_descr_ref l;
28507 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28509 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28510 if (!rtl)
28511 return;
28512 decl = SYMBOL_REF_DECL (rtl);
28514 else
28515 decl = TREE_OPERAND (init, 0);
28516 ref = lookup_decl_die (decl);
28517 if (ref == NULL
28518 || (!get_AT (ref, DW_AT_location)
28519 && !get_AT (ref, DW_AT_const_value)))
28520 return;
28521 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28522 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28523 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28524 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28525 add_AT_loc (die, DW_AT_location, l);
28529 /* Return NULL if l is a DWARF expression, or first op that is not
28530 valid DWARF expression. */
28532 static dw_loc_descr_ref
28533 non_dwarf_expression (dw_loc_descr_ref l)
28535 while (l)
28537 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28538 return l;
28539 switch (l->dw_loc_opc)
28541 case DW_OP_regx:
28542 case DW_OP_implicit_value:
28543 case DW_OP_stack_value:
28544 case DW_OP_implicit_pointer:
28545 case DW_OP_GNU_implicit_pointer:
28546 case DW_OP_GNU_parameter_ref:
28547 case DW_OP_piece:
28548 case DW_OP_bit_piece:
28549 return l;
28550 default:
28551 break;
28553 l = l->dw_loc_next;
28555 return NULL;
28558 /* Return adjusted copy of EXPR:
28559 If it is empty DWARF expression, return it.
28560 If it is valid non-empty DWARF expression,
28561 return copy of EXPR with DW_OP_deref appended to it.
28562 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28563 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28564 If it is DWARF expression followed by DW_OP_stack_value, return
28565 copy of the DWARF expression without anything appended.
28566 Otherwise, return NULL. */
28568 static dw_loc_descr_ref
28569 copy_deref_exprloc (dw_loc_descr_ref expr)
28571 dw_loc_descr_ref tail = NULL;
28573 if (expr == NULL)
28574 return NULL;
28576 dw_loc_descr_ref l = non_dwarf_expression (expr);
28577 if (l && l->dw_loc_next)
28578 return NULL;
28580 if (l)
28582 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28583 tail = new_loc_descr ((enum dwarf_location_atom)
28584 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28585 0, 0);
28586 else
28587 switch (l->dw_loc_opc)
28589 case DW_OP_regx:
28590 tail = new_loc_descr (DW_OP_bregx,
28591 l->dw_loc_oprnd1.v.val_unsigned, 0);
28592 break;
28593 case DW_OP_stack_value:
28594 break;
28595 default:
28596 return NULL;
28599 else
28600 tail = new_loc_descr (DW_OP_deref, 0, 0);
28602 dw_loc_descr_ref ret = NULL, *p = &ret;
28603 while (expr != l)
28605 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28606 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28607 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28608 p = &(*p)->dw_loc_next;
28609 expr = expr->dw_loc_next;
28611 *p = tail;
28612 return ret;
28615 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28616 reference to a variable or argument, adjust it if needed and return:
28617 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28618 attribute if present should be removed
28619 0 keep the attribute perhaps with minor modifications, no need to rescan
28620 1 if the attribute has been successfully adjusted. */
28622 static int
28623 optimize_string_length (dw_attr_node *a)
28625 dw_loc_descr_ref l = AT_loc (a), lv;
28626 dw_die_ref die;
28627 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28629 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28630 die = lookup_decl_die (decl);
28631 if (die)
28633 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28634 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28635 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28637 else
28638 return -1;
28640 else
28641 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28643 /* DWARF5 allows reference class, so we can then reference the DIE.
28644 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28645 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28647 a->dw_attr_val.val_class = dw_val_class_die_ref;
28648 a->dw_attr_val.val_entry = NULL;
28649 a->dw_attr_val.v.val_die_ref.die = die;
28650 a->dw_attr_val.v.val_die_ref.external = 0;
28651 return 0;
28654 dw_attr_node *av = get_AT (die, DW_AT_location);
28655 dw_loc_list_ref d;
28656 bool non_dwarf_expr = false;
28658 if (av == NULL)
28659 return dwarf_strict ? -1 : 0;
28660 switch (AT_class (av))
28662 case dw_val_class_loc_list:
28663 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28664 if (d->expr && non_dwarf_expression (d->expr))
28665 non_dwarf_expr = true;
28666 break;
28667 case dw_val_class_loc:
28668 lv = AT_loc (av);
28669 if (lv == NULL)
28670 return dwarf_strict ? -1 : 0;
28671 if (non_dwarf_expression (lv))
28672 non_dwarf_expr = true;
28673 break;
28674 default:
28675 return dwarf_strict ? -1 : 0;
28678 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28679 into DW_OP_call4 or DW_OP_GNU_variable_value into
28680 DW_OP_call4 DW_OP_deref, do so. */
28681 if (!non_dwarf_expr
28682 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28684 l->dw_loc_opc = DW_OP_call4;
28685 if (l->dw_loc_next)
28686 l->dw_loc_next = NULL;
28687 else
28688 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28689 return 0;
28692 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28693 copy over the DW_AT_location attribute from die to a. */
28694 if (l->dw_loc_next != NULL)
28696 a->dw_attr_val = av->dw_attr_val;
28697 return 1;
28700 dw_loc_list_ref list, *p;
28701 switch (AT_class (av))
28703 case dw_val_class_loc_list:
28704 p = &list;
28705 list = NULL;
28706 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28708 lv = copy_deref_exprloc (d->expr);
28709 if (lv)
28711 *p = new_loc_list (lv, d->begin, d->end, d->section);
28712 p = &(*p)->dw_loc_next;
28714 else if (!dwarf_strict && d->expr)
28715 return 0;
28717 if (list == NULL)
28718 return dwarf_strict ? -1 : 0;
28719 a->dw_attr_val.val_class = dw_val_class_loc_list;
28720 gen_llsym (list);
28721 *AT_loc_list_ptr (a) = list;
28722 return 1;
28723 case dw_val_class_loc:
28724 lv = copy_deref_exprloc (AT_loc (av));
28725 if (lv == NULL)
28726 return dwarf_strict ? -1 : 0;
28727 a->dw_attr_val.v.val_loc = lv;
28728 return 1;
28729 default:
28730 gcc_unreachable ();
28734 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28735 an address in .rodata section if the string literal is emitted there,
28736 or remove the containing location list or replace DW_AT_const_value
28737 with DW_AT_location and empty location expression, if it isn't found
28738 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28739 to something that has been emitted in the current CU. */
28741 static void
28742 resolve_addr (dw_die_ref die)
28744 dw_die_ref c;
28745 dw_attr_node *a;
28746 dw_loc_list_ref *curr, *start, loc;
28747 unsigned ix;
28748 bool remove_AT_byte_size = false;
28750 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28751 switch (AT_class (a))
28753 case dw_val_class_loc_list:
28754 start = curr = AT_loc_list_ptr (a);
28755 loc = *curr;
28756 gcc_assert (loc);
28757 /* The same list can be referenced more than once. See if we have
28758 already recorded the result from a previous pass. */
28759 if (loc->replaced)
28760 *curr = loc->dw_loc_next;
28761 else if (!loc->resolved_addr)
28763 /* As things stand, we do not expect or allow one die to
28764 reference a suffix of another die's location list chain.
28765 References must be identical or completely separate.
28766 There is therefore no need to cache the result of this
28767 pass on any list other than the first; doing so
28768 would lead to unnecessary writes. */
28769 while (*curr)
28771 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28772 if (!resolve_addr_in_expr (a, (*curr)->expr))
28774 dw_loc_list_ref next = (*curr)->dw_loc_next;
28775 dw_loc_descr_ref l = (*curr)->expr;
28777 if (next && (*curr)->ll_symbol)
28779 gcc_assert (!next->ll_symbol);
28780 next->ll_symbol = (*curr)->ll_symbol;
28782 if (dwarf_split_debug_info)
28783 remove_loc_list_addr_table_entries (l);
28784 *curr = next;
28786 else
28788 mark_base_types ((*curr)->expr);
28789 curr = &(*curr)->dw_loc_next;
28792 if (loc == *start)
28793 loc->resolved_addr = 1;
28794 else
28796 loc->replaced = 1;
28797 loc->dw_loc_next = *start;
28800 if (!*start)
28802 remove_AT (die, a->dw_attr);
28803 ix--;
28805 break;
28806 case dw_val_class_loc:
28808 dw_loc_descr_ref l = AT_loc (a);
28809 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28810 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28811 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28812 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28813 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28814 with DW_FORM_ref referencing the same DIE as
28815 DW_OP_GNU_variable_value used to reference. */
28816 if (a->dw_attr == DW_AT_string_length
28817 && l
28818 && l->dw_loc_opc == DW_OP_GNU_variable_value
28819 && (l->dw_loc_next == NULL
28820 || (l->dw_loc_next->dw_loc_next == NULL
28821 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28823 switch (optimize_string_length (a))
28825 case -1:
28826 remove_AT (die, a->dw_attr);
28827 ix--;
28828 /* If we drop DW_AT_string_length, we need to drop also
28829 DW_AT_{string_length_,}byte_size. */
28830 remove_AT_byte_size = true;
28831 continue;
28832 default:
28833 break;
28834 case 1:
28835 /* Even if we keep the optimized DW_AT_string_length,
28836 it might have changed AT_class, so process it again. */
28837 ix--;
28838 continue;
28841 /* For -gdwarf-2 don't attempt to optimize
28842 DW_AT_data_member_location containing
28843 DW_OP_plus_uconst - older consumers might
28844 rely on it being that op instead of a more complex,
28845 but shorter, location description. */
28846 if ((dwarf_version > 2
28847 || a->dw_attr != DW_AT_data_member_location
28848 || l == NULL
28849 || l->dw_loc_opc != DW_OP_plus_uconst
28850 || l->dw_loc_next != NULL)
28851 && !resolve_addr_in_expr (a, l))
28853 if (dwarf_split_debug_info)
28854 remove_loc_list_addr_table_entries (l);
28855 if (l != NULL
28856 && l->dw_loc_next == NULL
28857 && l->dw_loc_opc == DW_OP_addr
28858 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
28859 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
28860 && a->dw_attr == DW_AT_location)
28862 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
28863 remove_AT (die, a->dw_attr);
28864 ix--;
28865 optimize_location_into_implicit_ptr (die, decl);
28866 break;
28868 if (a->dw_attr == DW_AT_string_length)
28869 /* If we drop DW_AT_string_length, we need to drop also
28870 DW_AT_{string_length_,}byte_size. */
28871 remove_AT_byte_size = true;
28872 remove_AT (die, a->dw_attr);
28873 ix--;
28875 else
28876 mark_base_types (l);
28878 break;
28879 case dw_val_class_addr:
28880 if (a->dw_attr == DW_AT_const_value
28881 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
28883 if (AT_index (a) != NOT_INDEXED)
28884 remove_addr_table_entry (a->dw_attr_val.val_entry);
28885 remove_AT (die, a->dw_attr);
28886 ix--;
28888 if ((die->die_tag == DW_TAG_call_site
28889 && a->dw_attr == DW_AT_call_origin)
28890 || (die->die_tag == DW_TAG_GNU_call_site
28891 && a->dw_attr == DW_AT_abstract_origin))
28893 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
28894 dw_die_ref tdie = lookup_decl_die (tdecl);
28895 dw_die_ref cdie;
28896 if (tdie == NULL
28897 && DECL_EXTERNAL (tdecl)
28898 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
28899 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
28901 dw_die_ref pdie = cdie;
28902 /* Make sure we don't add these DIEs into type units.
28903 We could emit skeleton DIEs for context (namespaces,
28904 outer structs/classes) and a skeleton DIE for the
28905 innermost context with DW_AT_signature pointing to the
28906 type unit. See PR78835. */
28907 while (pdie && pdie->die_tag != DW_TAG_type_unit)
28908 pdie = pdie->die_parent;
28909 if (pdie == NULL)
28911 /* Creating a full DIE for tdecl is overly expensive and
28912 at this point even wrong when in the LTO phase
28913 as it can end up generating new type DIEs we didn't
28914 output and thus optimize_external_refs will crash. */
28915 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
28916 add_AT_flag (tdie, DW_AT_external, 1);
28917 add_AT_flag (tdie, DW_AT_declaration, 1);
28918 add_linkage_attr (tdie, tdecl);
28919 add_name_and_src_coords_attributes (tdie, tdecl);
28920 equate_decl_number_to_die (tdecl, tdie);
28923 if (tdie)
28925 a->dw_attr_val.val_class = dw_val_class_die_ref;
28926 a->dw_attr_val.v.val_die_ref.die = tdie;
28927 a->dw_attr_val.v.val_die_ref.external = 0;
28929 else
28931 if (AT_index (a) != NOT_INDEXED)
28932 remove_addr_table_entry (a->dw_attr_val.val_entry);
28933 remove_AT (die, a->dw_attr);
28934 ix--;
28937 break;
28938 default:
28939 break;
28942 if (remove_AT_byte_size)
28943 remove_AT (die, dwarf_version >= 5
28944 ? DW_AT_string_length_byte_size
28945 : DW_AT_byte_size);
28947 FOR_EACH_CHILD (die, c, resolve_addr (c));
28950 /* Helper routines for optimize_location_lists.
28951 This pass tries to share identical local lists in .debug_loc
28952 section. */
28954 /* Iteratively hash operands of LOC opcode into HSTATE. */
28956 static void
28957 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
28959 dw_val_ref val1 = &loc->dw_loc_oprnd1;
28960 dw_val_ref val2 = &loc->dw_loc_oprnd2;
28962 switch (loc->dw_loc_opc)
28964 case DW_OP_const4u:
28965 case DW_OP_const8u:
28966 if (loc->dtprel)
28967 goto hash_addr;
28968 /* FALLTHRU */
28969 case DW_OP_const1u:
28970 case DW_OP_const1s:
28971 case DW_OP_const2u:
28972 case DW_OP_const2s:
28973 case DW_OP_const4s:
28974 case DW_OP_const8s:
28975 case DW_OP_constu:
28976 case DW_OP_consts:
28977 case DW_OP_pick:
28978 case DW_OP_plus_uconst:
28979 case DW_OP_breg0:
28980 case DW_OP_breg1:
28981 case DW_OP_breg2:
28982 case DW_OP_breg3:
28983 case DW_OP_breg4:
28984 case DW_OP_breg5:
28985 case DW_OP_breg6:
28986 case DW_OP_breg7:
28987 case DW_OP_breg8:
28988 case DW_OP_breg9:
28989 case DW_OP_breg10:
28990 case DW_OP_breg11:
28991 case DW_OP_breg12:
28992 case DW_OP_breg13:
28993 case DW_OP_breg14:
28994 case DW_OP_breg15:
28995 case DW_OP_breg16:
28996 case DW_OP_breg17:
28997 case DW_OP_breg18:
28998 case DW_OP_breg19:
28999 case DW_OP_breg20:
29000 case DW_OP_breg21:
29001 case DW_OP_breg22:
29002 case DW_OP_breg23:
29003 case DW_OP_breg24:
29004 case DW_OP_breg25:
29005 case DW_OP_breg26:
29006 case DW_OP_breg27:
29007 case DW_OP_breg28:
29008 case DW_OP_breg29:
29009 case DW_OP_breg30:
29010 case DW_OP_breg31:
29011 case DW_OP_regx:
29012 case DW_OP_fbreg:
29013 case DW_OP_piece:
29014 case DW_OP_deref_size:
29015 case DW_OP_xderef_size:
29016 hstate.add_object (val1->v.val_int);
29017 break;
29018 case DW_OP_skip:
29019 case DW_OP_bra:
29021 int offset;
29023 gcc_assert (val1->val_class == dw_val_class_loc);
29024 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29025 hstate.add_object (offset);
29027 break;
29028 case DW_OP_implicit_value:
29029 hstate.add_object (val1->v.val_unsigned);
29030 switch (val2->val_class)
29032 case dw_val_class_const:
29033 hstate.add_object (val2->v.val_int);
29034 break;
29035 case dw_val_class_vec:
29037 unsigned int elt_size = val2->v.val_vec.elt_size;
29038 unsigned int len = val2->v.val_vec.length;
29040 hstate.add_int (elt_size);
29041 hstate.add_int (len);
29042 hstate.add (val2->v.val_vec.array, len * elt_size);
29044 break;
29045 case dw_val_class_const_double:
29046 hstate.add_object (val2->v.val_double.low);
29047 hstate.add_object (val2->v.val_double.high);
29048 break;
29049 case dw_val_class_wide_int:
29050 hstate.add (val2->v.val_wide->get_val (),
29051 get_full_len (*val2->v.val_wide)
29052 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29053 break;
29054 case dw_val_class_addr:
29055 inchash::add_rtx (val2->v.val_addr, hstate);
29056 break;
29057 default:
29058 gcc_unreachable ();
29060 break;
29061 case DW_OP_bregx:
29062 case DW_OP_bit_piece:
29063 hstate.add_object (val1->v.val_int);
29064 hstate.add_object (val2->v.val_int);
29065 break;
29066 case DW_OP_addr:
29067 hash_addr:
29068 if (loc->dtprel)
29070 unsigned char dtprel = 0xd1;
29071 hstate.add_object (dtprel);
29073 inchash::add_rtx (val1->v.val_addr, hstate);
29074 break;
29075 case DW_OP_GNU_addr_index:
29076 case DW_OP_GNU_const_index:
29078 if (loc->dtprel)
29080 unsigned char dtprel = 0xd1;
29081 hstate.add_object (dtprel);
29083 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29085 break;
29086 case DW_OP_implicit_pointer:
29087 case DW_OP_GNU_implicit_pointer:
29088 hstate.add_int (val2->v.val_int);
29089 break;
29090 case DW_OP_entry_value:
29091 case DW_OP_GNU_entry_value:
29092 hstate.add_object (val1->v.val_loc);
29093 break;
29094 case DW_OP_regval_type:
29095 case DW_OP_deref_type:
29096 case DW_OP_GNU_regval_type:
29097 case DW_OP_GNU_deref_type:
29099 unsigned int byte_size
29100 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29101 unsigned int encoding
29102 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29103 hstate.add_object (val1->v.val_int);
29104 hstate.add_object (byte_size);
29105 hstate.add_object (encoding);
29107 break;
29108 case DW_OP_convert:
29109 case DW_OP_reinterpret:
29110 case DW_OP_GNU_convert:
29111 case DW_OP_GNU_reinterpret:
29112 if (val1->val_class == dw_val_class_unsigned_const)
29114 hstate.add_object (val1->v.val_unsigned);
29115 break;
29117 /* FALLTHRU */
29118 case DW_OP_const_type:
29119 case DW_OP_GNU_const_type:
29121 unsigned int byte_size
29122 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29123 unsigned int encoding
29124 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29125 hstate.add_object (byte_size);
29126 hstate.add_object (encoding);
29127 if (loc->dw_loc_opc != DW_OP_const_type
29128 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29129 break;
29130 hstate.add_object (val2->val_class);
29131 switch (val2->val_class)
29133 case dw_val_class_const:
29134 hstate.add_object (val2->v.val_int);
29135 break;
29136 case dw_val_class_vec:
29138 unsigned int elt_size = val2->v.val_vec.elt_size;
29139 unsigned int len = val2->v.val_vec.length;
29141 hstate.add_object (elt_size);
29142 hstate.add_object (len);
29143 hstate.add (val2->v.val_vec.array, len * elt_size);
29145 break;
29146 case dw_val_class_const_double:
29147 hstate.add_object (val2->v.val_double.low);
29148 hstate.add_object (val2->v.val_double.high);
29149 break;
29150 case dw_val_class_wide_int:
29151 hstate.add (val2->v.val_wide->get_val (),
29152 get_full_len (*val2->v.val_wide)
29153 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29154 break;
29155 default:
29156 gcc_unreachable ();
29159 break;
29161 default:
29162 /* Other codes have no operands. */
29163 break;
29167 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29169 static inline void
29170 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29172 dw_loc_descr_ref l;
29173 bool sizes_computed = false;
29174 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29175 size_of_locs (loc);
29177 for (l = loc; l != NULL; l = l->dw_loc_next)
29179 enum dwarf_location_atom opc = l->dw_loc_opc;
29180 hstate.add_object (opc);
29181 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29183 size_of_locs (loc);
29184 sizes_computed = true;
29186 hash_loc_operands (l, hstate);
29190 /* Compute hash of the whole location list LIST_HEAD. */
29192 static inline void
29193 hash_loc_list (dw_loc_list_ref list_head)
29195 dw_loc_list_ref curr = list_head;
29196 inchash::hash hstate;
29198 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29200 hstate.add (curr->begin, strlen (curr->begin) + 1);
29201 hstate.add (curr->end, strlen (curr->end) + 1);
29202 if (curr->section)
29203 hstate.add (curr->section, strlen (curr->section) + 1);
29204 hash_locs (curr->expr, hstate);
29206 list_head->hash = hstate.end ();
29209 /* Return true if X and Y opcodes have the same operands. */
29211 static inline bool
29212 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29214 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29215 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29216 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29217 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29219 switch (x->dw_loc_opc)
29221 case DW_OP_const4u:
29222 case DW_OP_const8u:
29223 if (x->dtprel)
29224 goto hash_addr;
29225 /* FALLTHRU */
29226 case DW_OP_const1u:
29227 case DW_OP_const1s:
29228 case DW_OP_const2u:
29229 case DW_OP_const2s:
29230 case DW_OP_const4s:
29231 case DW_OP_const8s:
29232 case DW_OP_constu:
29233 case DW_OP_consts:
29234 case DW_OP_pick:
29235 case DW_OP_plus_uconst:
29236 case DW_OP_breg0:
29237 case DW_OP_breg1:
29238 case DW_OP_breg2:
29239 case DW_OP_breg3:
29240 case DW_OP_breg4:
29241 case DW_OP_breg5:
29242 case DW_OP_breg6:
29243 case DW_OP_breg7:
29244 case DW_OP_breg8:
29245 case DW_OP_breg9:
29246 case DW_OP_breg10:
29247 case DW_OP_breg11:
29248 case DW_OP_breg12:
29249 case DW_OP_breg13:
29250 case DW_OP_breg14:
29251 case DW_OP_breg15:
29252 case DW_OP_breg16:
29253 case DW_OP_breg17:
29254 case DW_OP_breg18:
29255 case DW_OP_breg19:
29256 case DW_OP_breg20:
29257 case DW_OP_breg21:
29258 case DW_OP_breg22:
29259 case DW_OP_breg23:
29260 case DW_OP_breg24:
29261 case DW_OP_breg25:
29262 case DW_OP_breg26:
29263 case DW_OP_breg27:
29264 case DW_OP_breg28:
29265 case DW_OP_breg29:
29266 case DW_OP_breg30:
29267 case DW_OP_breg31:
29268 case DW_OP_regx:
29269 case DW_OP_fbreg:
29270 case DW_OP_piece:
29271 case DW_OP_deref_size:
29272 case DW_OP_xderef_size:
29273 return valx1->v.val_int == valy1->v.val_int;
29274 case DW_OP_skip:
29275 case DW_OP_bra:
29276 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29277 can cause irrelevant differences in dw_loc_addr. */
29278 gcc_assert (valx1->val_class == dw_val_class_loc
29279 && valy1->val_class == dw_val_class_loc
29280 && (dwarf_split_debug_info
29281 || x->dw_loc_addr == y->dw_loc_addr));
29282 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29283 case DW_OP_implicit_value:
29284 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29285 || valx2->val_class != valy2->val_class)
29286 return false;
29287 switch (valx2->val_class)
29289 case dw_val_class_const:
29290 return valx2->v.val_int == valy2->v.val_int;
29291 case dw_val_class_vec:
29292 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29293 && valx2->v.val_vec.length == valy2->v.val_vec.length
29294 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29295 valx2->v.val_vec.elt_size
29296 * valx2->v.val_vec.length) == 0;
29297 case dw_val_class_const_double:
29298 return valx2->v.val_double.low == valy2->v.val_double.low
29299 && valx2->v.val_double.high == valy2->v.val_double.high;
29300 case dw_val_class_wide_int:
29301 return *valx2->v.val_wide == *valy2->v.val_wide;
29302 case dw_val_class_addr:
29303 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29304 default:
29305 gcc_unreachable ();
29307 case DW_OP_bregx:
29308 case DW_OP_bit_piece:
29309 return valx1->v.val_int == valy1->v.val_int
29310 && valx2->v.val_int == valy2->v.val_int;
29311 case DW_OP_addr:
29312 hash_addr:
29313 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29314 case DW_OP_GNU_addr_index:
29315 case DW_OP_GNU_const_index:
29317 rtx ax1 = valx1->val_entry->addr.rtl;
29318 rtx ay1 = valy1->val_entry->addr.rtl;
29319 return rtx_equal_p (ax1, ay1);
29321 case DW_OP_implicit_pointer:
29322 case DW_OP_GNU_implicit_pointer:
29323 return valx1->val_class == dw_val_class_die_ref
29324 && valx1->val_class == valy1->val_class
29325 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29326 && valx2->v.val_int == valy2->v.val_int;
29327 case DW_OP_entry_value:
29328 case DW_OP_GNU_entry_value:
29329 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29330 case DW_OP_const_type:
29331 case DW_OP_GNU_const_type:
29332 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29333 || valx2->val_class != valy2->val_class)
29334 return false;
29335 switch (valx2->val_class)
29337 case dw_val_class_const:
29338 return valx2->v.val_int == valy2->v.val_int;
29339 case dw_val_class_vec:
29340 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29341 && valx2->v.val_vec.length == valy2->v.val_vec.length
29342 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29343 valx2->v.val_vec.elt_size
29344 * valx2->v.val_vec.length) == 0;
29345 case dw_val_class_const_double:
29346 return valx2->v.val_double.low == valy2->v.val_double.low
29347 && valx2->v.val_double.high == valy2->v.val_double.high;
29348 case dw_val_class_wide_int:
29349 return *valx2->v.val_wide == *valy2->v.val_wide;
29350 default:
29351 gcc_unreachable ();
29353 case DW_OP_regval_type:
29354 case DW_OP_deref_type:
29355 case DW_OP_GNU_regval_type:
29356 case DW_OP_GNU_deref_type:
29357 return valx1->v.val_int == valy1->v.val_int
29358 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29359 case DW_OP_convert:
29360 case DW_OP_reinterpret:
29361 case DW_OP_GNU_convert:
29362 case DW_OP_GNU_reinterpret:
29363 if (valx1->val_class != valy1->val_class)
29364 return false;
29365 if (valx1->val_class == dw_val_class_unsigned_const)
29366 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29367 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29368 case DW_OP_GNU_parameter_ref:
29369 return valx1->val_class == dw_val_class_die_ref
29370 && valx1->val_class == valy1->val_class
29371 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29372 default:
29373 /* Other codes have no operands. */
29374 return true;
29378 /* Return true if DWARF location expressions X and Y are the same. */
29380 static inline bool
29381 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29383 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29384 if (x->dw_loc_opc != y->dw_loc_opc
29385 || x->dtprel != y->dtprel
29386 || !compare_loc_operands (x, y))
29387 break;
29388 return x == NULL && y == NULL;
29391 /* Hashtable helpers. */
29393 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29395 static inline hashval_t hash (const dw_loc_list_struct *);
29396 static inline bool equal (const dw_loc_list_struct *,
29397 const dw_loc_list_struct *);
29400 /* Return precomputed hash of location list X. */
29402 inline hashval_t
29403 loc_list_hasher::hash (const dw_loc_list_struct *x)
29405 return x->hash;
29408 /* Return true if location lists A and B are the same. */
29410 inline bool
29411 loc_list_hasher::equal (const dw_loc_list_struct *a,
29412 const dw_loc_list_struct *b)
29414 if (a == b)
29415 return 1;
29416 if (a->hash != b->hash)
29417 return 0;
29418 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29419 if (strcmp (a->begin, b->begin) != 0
29420 || strcmp (a->end, b->end) != 0
29421 || (a->section == NULL) != (b->section == NULL)
29422 || (a->section && strcmp (a->section, b->section) != 0)
29423 || !compare_locs (a->expr, b->expr))
29424 break;
29425 return a == NULL && b == NULL;
29428 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29431 /* Recursively optimize location lists referenced from DIE
29432 children and share them whenever possible. */
29434 static void
29435 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29437 dw_die_ref c;
29438 dw_attr_node *a;
29439 unsigned ix;
29440 dw_loc_list_struct **slot;
29442 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29443 if (AT_class (a) == dw_val_class_loc_list)
29445 dw_loc_list_ref list = AT_loc_list (a);
29446 /* TODO: perform some optimizations here, before hashing
29447 it and storing into the hash table. */
29448 hash_loc_list (list);
29449 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29450 if (*slot == NULL)
29451 *slot = list;
29452 else
29453 a->dw_attr_val.v.val_loc_list = *slot;
29456 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29460 /* Recursively assign each location list a unique index into the debug_addr
29461 section. */
29463 static void
29464 index_location_lists (dw_die_ref die)
29466 dw_die_ref c;
29467 dw_attr_node *a;
29468 unsigned ix;
29470 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29471 if (AT_class (a) == dw_val_class_loc_list)
29473 dw_loc_list_ref list = AT_loc_list (a);
29474 dw_loc_list_ref curr;
29475 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29477 /* Don't index an entry that has already been indexed
29478 or won't be output. */
29479 if (curr->begin_entry != NULL
29480 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29481 continue;
29483 curr->begin_entry
29484 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29488 FOR_EACH_CHILD (die, c, index_location_lists (c));
29491 /* Optimize location lists referenced from DIE
29492 children and share them whenever possible. */
29494 static void
29495 optimize_location_lists (dw_die_ref die)
29497 loc_list_hash_type htab (500);
29498 optimize_location_lists_1 (die, &htab);
29501 /* Traverse the limbo die list, and add parent/child links. The only
29502 dies without parents that should be here are concrete instances of
29503 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29504 For concrete instances, we can get the parent die from the abstract
29505 instance. */
29507 static void
29508 flush_limbo_die_list (void)
29510 limbo_die_node *node;
29512 /* get_context_die calls force_decl_die, which can put new DIEs on the
29513 limbo list in LTO mode when nested functions are put in a different
29514 partition than that of their parent function. */
29515 while ((node = limbo_die_list))
29517 dw_die_ref die = node->die;
29518 limbo_die_list = node->next;
29520 if (die->die_parent == NULL)
29522 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29524 if (origin && origin->die_parent)
29525 add_child_die (origin->die_parent, die);
29526 else if (is_cu_die (die))
29528 else if (seen_error ())
29529 /* It's OK to be confused by errors in the input. */
29530 add_child_die (comp_unit_die (), die);
29531 else
29533 /* In certain situations, the lexical block containing a
29534 nested function can be optimized away, which results
29535 in the nested function die being orphaned. Likewise
29536 with the return type of that nested function. Force
29537 this to be a child of the containing function.
29539 It may happen that even the containing function got fully
29540 inlined and optimized out. In that case we are lost and
29541 assign the empty child. This should not be big issue as
29542 the function is likely unreachable too. */
29543 gcc_assert (node->created_for);
29545 if (DECL_P (node->created_for))
29546 origin = get_context_die (DECL_CONTEXT (node->created_for));
29547 else if (TYPE_P (node->created_for))
29548 origin = scope_die_for (node->created_for, comp_unit_die ());
29549 else
29550 origin = comp_unit_die ();
29552 add_child_die (origin, die);
29558 /* Output stuff that dwarf requires at the end of every file,
29559 and generate the DWARF-2 debugging info. */
29561 static void
29562 dwarf2out_finish (const char *)
29564 comdat_type_node *ctnode;
29565 dw_die_ref main_comp_unit_die;
29566 unsigned char checksum[16];
29567 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29569 /* Flush out any latecomers to the limbo party. */
29570 flush_limbo_die_list ();
29572 if (flag_checking)
29574 verify_die (comp_unit_die ());
29575 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29576 verify_die (node->die);
29579 /* We shouldn't have any symbols with delayed asm names for
29580 DIEs generated after early finish. */
29581 gcc_assert (deferred_asm_name == NULL);
29583 gen_remaining_tmpl_value_param_die_attribute ();
29585 #if ENABLE_ASSERT_CHECKING
29587 dw_die_ref die = comp_unit_die (), c;
29588 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29590 #endif
29591 resolve_addr (comp_unit_die ());
29592 move_marked_base_types ();
29594 /* Initialize sections and labels used for actual assembler output. */
29595 init_sections_and_labels ();
29597 /* Traverse the DIE's and add sibling attributes to those DIE's that
29598 have children. */
29599 add_sibling_attributes (comp_unit_die ());
29600 limbo_die_node *node;
29601 for (node = cu_die_list; node; node = node->next)
29602 add_sibling_attributes (node->die);
29603 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29604 add_sibling_attributes (ctnode->root_die);
29606 /* When splitting DWARF info, we put some attributes in the
29607 skeleton compile_unit DIE that remains in the .o, while
29608 most attributes go in the DWO compile_unit_die. */
29609 if (dwarf_split_debug_info)
29611 limbo_die_node *cu;
29612 main_comp_unit_die = gen_compile_unit_die (NULL);
29613 if (dwarf_version >= 5)
29614 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29615 cu = limbo_die_list;
29616 gcc_assert (cu->die == main_comp_unit_die);
29617 limbo_die_list = limbo_die_list->next;
29618 cu->next = cu_die_list;
29619 cu_die_list = cu;
29621 else
29622 main_comp_unit_die = comp_unit_die ();
29624 /* Output a terminator label for the .text section. */
29625 switch_to_section (text_section);
29626 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29627 if (cold_text_section)
29629 switch_to_section (cold_text_section);
29630 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29633 /* We can only use the low/high_pc attributes if all of the code was
29634 in .text. */
29635 if (!have_multiple_function_sections
29636 || (dwarf_version < 3 && dwarf_strict))
29638 /* Don't add if the CU has no associated code. */
29639 if (text_section_used)
29640 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29641 text_end_label, true);
29643 else
29645 unsigned fde_idx;
29646 dw_fde_ref fde;
29647 bool range_list_added = false;
29649 if (text_section_used)
29650 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29651 text_end_label, &range_list_added, true);
29652 if (cold_text_section_used)
29653 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29654 cold_end_label, &range_list_added, true);
29656 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29658 if (DECL_IGNORED_P (fde->decl))
29659 continue;
29660 if (!fde->in_std_section)
29661 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29662 fde->dw_fde_end, &range_list_added,
29663 true);
29664 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29665 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29666 fde->dw_fde_second_end, &range_list_added,
29667 true);
29670 if (range_list_added)
29672 /* We need to give .debug_loc and .debug_ranges an appropriate
29673 "base address". Use zero so that these addresses become
29674 absolute. Historically, we've emitted the unexpected
29675 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29676 Emit both to give time for other tools to adapt. */
29677 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29678 if (! dwarf_strict && dwarf_version < 4)
29679 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29681 add_ranges (NULL);
29685 /* AIX Assembler inserts the length, so adjust the reference to match the
29686 offset expected by debuggers. */
29687 strcpy (dl_section_ref, debug_line_section_label);
29688 if (XCOFF_DEBUGGING_INFO)
29689 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29691 if (debug_info_level >= DINFO_LEVEL_TERSE)
29692 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29693 dl_section_ref);
29695 if (have_macinfo)
29696 add_AT_macptr (comp_unit_die (),
29697 dwarf_version >= 5 ? DW_AT_macros
29698 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29699 macinfo_section_label);
29701 if (dwarf_split_debug_info)
29703 if (have_location_lists)
29705 if (dwarf_version >= 5)
29706 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29707 loc_section_label);
29708 /* optimize_location_lists calculates the size of the lists,
29709 so index them first, and assign indices to the entries.
29710 Although optimize_location_lists will remove entries from
29711 the table, it only does so for duplicates, and therefore
29712 only reduces ref_counts to 1. */
29713 index_location_lists (comp_unit_die ());
29716 if (addr_index_table != NULL)
29718 unsigned int index = 0;
29719 addr_index_table
29720 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29721 (&index);
29725 loc_list_idx = 0;
29726 if (have_location_lists)
29728 optimize_location_lists (comp_unit_die ());
29729 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29730 if (dwarf_version >= 5 && dwarf_split_debug_info)
29731 assign_location_list_indexes (comp_unit_die ());
29734 save_macinfo_strings ();
29736 if (dwarf_split_debug_info)
29738 unsigned int index = 0;
29740 /* Add attributes common to skeleton compile_units and
29741 type_units. Because these attributes include strings, it
29742 must be done before freezing the string table. Top-level
29743 skeleton die attrs are added when the skeleton type unit is
29744 created, so ensure it is created by this point. */
29745 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29746 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29749 /* Output all of the compilation units. We put the main one last so that
29750 the offsets are available to output_pubnames. */
29751 for (node = cu_die_list; node; node = node->next)
29752 output_comp_unit (node->die, 0, NULL);
29754 hash_table<comdat_type_hasher> comdat_type_table (100);
29755 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29757 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29759 /* Don't output duplicate types. */
29760 if (*slot != HTAB_EMPTY_ENTRY)
29761 continue;
29763 /* Add a pointer to the line table for the main compilation unit
29764 so that the debugger can make sense of DW_AT_decl_file
29765 attributes. */
29766 if (debug_info_level >= DINFO_LEVEL_TERSE)
29767 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29768 (!dwarf_split_debug_info
29769 ? dl_section_ref
29770 : debug_skeleton_line_section_label));
29772 output_comdat_type_unit (ctnode);
29773 *slot = ctnode;
29776 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29777 both the main_cu and all skeleton TUs. Making this call unconditional
29778 would end up either adding a second copy of the AT_pubnames attribute, or
29779 requiring a special case in add_top_level_skeleton_die_attrs. */
29780 if (!dwarf_split_debug_info)
29781 add_AT_pubnames (comp_unit_die ());
29783 if (dwarf_split_debug_info)
29785 int mark;
29786 struct md5_ctx ctx;
29788 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29789 index_rnglists ();
29791 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29792 md5_init_ctx (&ctx);
29793 mark = 0;
29794 die_checksum (comp_unit_die (), &ctx, &mark);
29795 unmark_all_dies (comp_unit_die ());
29796 md5_finish_ctx (&ctx, checksum);
29798 if (dwarf_version < 5)
29800 /* Use the first 8 bytes of the checksum as the dwo_id,
29801 and add it to both comp-unit DIEs. */
29802 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29803 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29806 /* Add the base offset of the ranges table to the skeleton
29807 comp-unit DIE. */
29808 if (!vec_safe_is_empty (ranges_table))
29810 if (dwarf_version >= 5)
29811 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29812 ranges_base_label);
29813 else
29814 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29815 ranges_section_label);
29818 switch_to_section (debug_addr_section);
29819 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29820 output_addr_table ();
29823 /* Output the main compilation unit if non-empty or if .debug_macinfo
29824 or .debug_macro will be emitted. */
29825 output_comp_unit (comp_unit_die (), have_macinfo,
29826 dwarf_split_debug_info ? checksum : NULL);
29828 if (dwarf_split_debug_info && info_section_emitted)
29829 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29831 /* Output the abbreviation table. */
29832 if (vec_safe_length (abbrev_die_table) != 1)
29834 switch_to_section (debug_abbrev_section);
29835 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29836 output_abbrev_section ();
29839 /* Output location list section if necessary. */
29840 if (have_location_lists)
29842 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29843 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29844 /* Output the location lists info. */
29845 switch_to_section (debug_loc_section);
29846 if (dwarf_version >= 5)
29848 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29849 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29850 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29851 dw2_asm_output_data (4, 0xffffffff,
29852 "Initial length escape value indicating "
29853 "64-bit DWARF extension");
29854 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29855 "Length of Location Lists");
29856 ASM_OUTPUT_LABEL (asm_out_file, l1);
29857 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
29858 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
29859 dw2_asm_output_data (1, 0, "Segment Size");
29860 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
29861 "Offset Entry Count");
29863 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
29864 if (dwarf_version >= 5 && dwarf_split_debug_info)
29866 unsigned int save_loc_list_idx = loc_list_idx;
29867 loc_list_idx = 0;
29868 output_loclists_offsets (comp_unit_die ());
29869 gcc_assert (save_loc_list_idx == loc_list_idx);
29871 output_location_lists (comp_unit_die ());
29872 if (dwarf_version >= 5)
29873 ASM_OUTPUT_LABEL (asm_out_file, l2);
29876 output_pubtables ();
29878 /* Output the address range information if a CU (.debug_info section)
29879 was emitted. We output an empty table even if we had no functions
29880 to put in it. This because the consumer has no way to tell the
29881 difference between an empty table that we omitted and failure to
29882 generate a table that would have contained data. */
29883 if (info_section_emitted)
29885 switch_to_section (debug_aranges_section);
29886 output_aranges ();
29889 /* Output ranges section if necessary. */
29890 if (!vec_safe_is_empty (ranges_table))
29892 if (dwarf_version >= 5)
29893 output_rnglists ();
29894 else
29895 output_ranges ();
29898 /* Have to end the macro section. */
29899 if (have_macinfo)
29901 switch_to_section (debug_macinfo_section);
29902 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
29903 output_macinfo ();
29904 dw2_asm_output_data (1, 0, "End compilation unit");
29907 /* Output the source line correspondence table. We must do this
29908 even if there is no line information. Otherwise, on an empty
29909 translation unit, we will generate a present, but empty,
29910 .debug_info section. IRIX 6.5 `nm' will then complain when
29911 examining the file. This is done late so that any filenames
29912 used by the debug_info section are marked as 'used'. */
29913 switch_to_section (debug_line_section);
29914 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
29915 if (! DWARF2_ASM_LINE_DEBUG_INFO)
29916 output_line_info (false);
29918 if (dwarf_split_debug_info && info_section_emitted)
29920 switch_to_section (debug_skeleton_line_section);
29921 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
29922 output_line_info (true);
29925 /* If we emitted any indirect strings, output the string table too. */
29926 if (debug_str_hash || skeleton_debug_str_hash)
29927 output_indirect_strings ();
29928 if (debug_line_str_hash)
29930 switch_to_section (debug_line_str_section);
29931 const enum dwarf_form form = DW_FORM_line_strp;
29932 debug_line_str_hash->traverse<enum dwarf_form,
29933 output_indirect_string> (form);
29937 /* Returns a hash value for X (which really is a variable_value_struct). */
29939 inline hashval_t
29940 variable_value_hasher::hash (variable_value_struct *x)
29942 return (hashval_t) x->decl_id;
29945 /* Return nonzero if decl_id of variable_value_struct X is the same as
29946 UID of decl Y. */
29948 inline bool
29949 variable_value_hasher::equal (variable_value_struct *x, tree y)
29951 return x->decl_id == DECL_UID (y);
29954 /* Helper function for resolve_variable_value, handle
29955 DW_OP_GNU_variable_value in one location expression.
29956 Return true if exprloc has been changed into loclist. */
29958 static bool
29959 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29961 dw_loc_descr_ref next;
29962 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
29964 next = loc->dw_loc_next;
29965 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
29966 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
29967 continue;
29969 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
29970 if (DECL_CONTEXT (decl) != current_function_decl)
29971 continue;
29973 dw_die_ref ref = lookup_decl_die (decl);
29974 if (ref)
29976 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29977 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29978 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29979 continue;
29981 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
29982 if (l == NULL)
29983 continue;
29984 if (l->dw_loc_next)
29986 if (AT_class (a) != dw_val_class_loc)
29987 continue;
29988 switch (a->dw_attr)
29990 /* Following attributes allow both exprloc and loclist
29991 classes, so we can change them into a loclist. */
29992 case DW_AT_location:
29993 case DW_AT_string_length:
29994 case DW_AT_return_addr:
29995 case DW_AT_data_member_location:
29996 case DW_AT_frame_base:
29997 case DW_AT_segment:
29998 case DW_AT_static_link:
29999 case DW_AT_use_location:
30000 case DW_AT_vtable_elem_location:
30001 if (prev)
30003 prev->dw_loc_next = NULL;
30004 prepend_loc_descr_to_each (l, AT_loc (a));
30006 if (next)
30007 add_loc_descr_to_each (l, next);
30008 a->dw_attr_val.val_class = dw_val_class_loc_list;
30009 a->dw_attr_val.val_entry = NULL;
30010 a->dw_attr_val.v.val_loc_list = l;
30011 have_location_lists = true;
30012 return true;
30013 /* Following attributes allow both exprloc and reference,
30014 so if the whole expression is DW_OP_GNU_variable_value alone
30015 we could transform it into reference. */
30016 case DW_AT_byte_size:
30017 case DW_AT_bit_size:
30018 case DW_AT_lower_bound:
30019 case DW_AT_upper_bound:
30020 case DW_AT_bit_stride:
30021 case DW_AT_count:
30022 case DW_AT_allocated:
30023 case DW_AT_associated:
30024 case DW_AT_byte_stride:
30025 if (prev == NULL && next == NULL)
30026 break;
30027 /* FALLTHRU */
30028 default:
30029 if (dwarf_strict)
30030 continue;
30031 break;
30033 /* Create DW_TAG_variable that we can refer to. */
30034 gen_decl_die (decl, NULL_TREE, NULL,
30035 lookup_decl_die (current_function_decl));
30036 ref = lookup_decl_die (decl);
30037 if (ref)
30039 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30040 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30041 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30043 continue;
30045 if (prev)
30047 prev->dw_loc_next = l->expr;
30048 add_loc_descr (&prev->dw_loc_next, next);
30049 free_loc_descr (loc, NULL);
30050 next = prev->dw_loc_next;
30052 else
30054 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30055 add_loc_descr (&loc, next);
30056 next = loc;
30058 loc = prev;
30060 return false;
30063 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30065 static void
30066 resolve_variable_value (dw_die_ref die)
30068 dw_attr_node *a;
30069 dw_loc_list_ref loc;
30070 unsigned ix;
30072 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30073 switch (AT_class (a))
30075 case dw_val_class_loc:
30076 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30077 break;
30078 /* FALLTHRU */
30079 case dw_val_class_loc_list:
30080 loc = AT_loc_list (a);
30081 gcc_assert (loc);
30082 for (; loc; loc = loc->dw_loc_next)
30083 resolve_variable_value_in_expr (a, loc->expr);
30084 break;
30085 default:
30086 break;
30090 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30091 temporaries in the current function. */
30093 static void
30094 resolve_variable_values (void)
30096 if (!variable_value_hash || !current_function_decl)
30097 return;
30099 struct variable_value_struct *node
30100 = variable_value_hash->find_with_hash (current_function_decl,
30101 DECL_UID (current_function_decl));
30103 if (node == NULL)
30104 return;
30106 unsigned int i;
30107 dw_die_ref die;
30108 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30109 resolve_variable_value (die);
30112 /* Helper function for note_variable_value, handle one location
30113 expression. */
30115 static void
30116 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30118 for (; loc; loc = loc->dw_loc_next)
30119 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30120 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30122 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30123 dw_die_ref ref = lookup_decl_die (decl);
30124 if (ref)
30126 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30127 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30128 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30129 continue;
30131 if (VAR_P (decl)
30132 && DECL_CONTEXT (decl)
30133 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30134 && lookup_decl_die (DECL_CONTEXT (decl)))
30136 if (!variable_value_hash)
30137 variable_value_hash
30138 = hash_table<variable_value_hasher>::create_ggc (10);
30140 tree fndecl = DECL_CONTEXT (decl);
30141 struct variable_value_struct *node;
30142 struct variable_value_struct **slot
30143 = variable_value_hash->find_slot_with_hash (fndecl,
30144 DECL_UID (fndecl),
30145 INSERT);
30146 if (*slot == NULL)
30148 node = ggc_cleared_alloc<variable_value_struct> ();
30149 node->decl_id = DECL_UID (fndecl);
30150 *slot = node;
30152 else
30153 node = *slot;
30155 vec_safe_push (node->dies, die);
30160 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30161 with dw_val_class_decl_ref operand. */
30163 static void
30164 note_variable_value (dw_die_ref die)
30166 dw_die_ref c;
30167 dw_attr_node *a;
30168 dw_loc_list_ref loc;
30169 unsigned ix;
30171 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30172 switch (AT_class (a))
30174 case dw_val_class_loc_list:
30175 loc = AT_loc_list (a);
30176 gcc_assert (loc);
30177 if (!loc->noted_variable_value)
30179 loc->noted_variable_value = 1;
30180 for (; loc; loc = loc->dw_loc_next)
30181 note_variable_value_in_expr (die, loc->expr);
30183 break;
30184 case dw_val_class_loc:
30185 note_variable_value_in_expr (die, AT_loc (a));
30186 break;
30187 default:
30188 break;
30191 /* Mark children. */
30192 FOR_EACH_CHILD (die, c, note_variable_value (c));
30195 /* Perform any cleanups needed after the early debug generation pass
30196 has run. */
30198 static void
30199 dwarf2out_early_finish (const char *filename)
30201 set_early_dwarf s;
30203 /* PCH might result in DW_AT_producer string being restored from the
30204 header compilation, so always fill it with empty string initially
30205 and overwrite only here. */
30206 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30207 producer_string = gen_producer_string ();
30208 producer->dw_attr_val.v.val_str->refcount--;
30209 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30211 /* Add the name for the main input file now. We delayed this from
30212 dwarf2out_init to avoid complications with PCH. */
30213 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30214 add_comp_dir_attribute (comp_unit_die ());
30216 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30217 DW_AT_comp_dir into .debug_line_str section. */
30218 if (!DWARF2_ASM_LINE_DEBUG_INFO
30219 && dwarf_version >= 5
30220 && DWARF5_USE_DEBUG_LINE_STR)
30222 for (int i = 0; i < 2; i++)
30224 dw_attr_node *a = get_AT (comp_unit_die (),
30225 i ? DW_AT_comp_dir : DW_AT_name);
30226 if (a == NULL
30227 || AT_class (a) != dw_val_class_str
30228 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30229 continue;
30231 if (! debug_line_str_hash)
30232 debug_line_str_hash
30233 = hash_table<indirect_string_hasher>::create_ggc (10);
30235 struct indirect_string_node *node
30236 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30237 set_indirect_string (node);
30238 node->form = DW_FORM_line_strp;
30239 a->dw_attr_val.v.val_str->refcount--;
30240 a->dw_attr_val.v.val_str = node;
30244 /* With LTO early dwarf was really finished at compile-time, so make
30245 sure to adjust the phase after annotating the LTRANS CU DIE. */
30246 if (in_lto_p)
30248 early_dwarf_finished = true;
30249 return;
30252 /* Walk through the list of incomplete types again, trying once more to
30253 emit full debugging info for them. */
30254 retry_incomplete_types ();
30256 /* The point here is to flush out the limbo list so that it is empty
30257 and we don't need to stream it for LTO. */
30258 flush_limbo_die_list ();
30260 gen_scheduled_generic_parms_dies ();
30261 gen_remaining_tmpl_value_param_die_attribute ();
30263 /* Add DW_AT_linkage_name for all deferred DIEs. */
30264 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30266 tree decl = node->created_for;
30267 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30268 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30269 ended up in deferred_asm_name before we knew it was
30270 constant and never written to disk. */
30271 && DECL_ASSEMBLER_NAME (decl))
30273 add_linkage_attr (node->die, decl);
30274 move_linkage_attr (node->die);
30277 deferred_asm_name = NULL;
30279 if (flag_eliminate_unused_debug_types)
30280 prune_unused_types ();
30282 /* Generate separate COMDAT sections for type DIEs. */
30283 if (use_debug_types)
30285 break_out_comdat_types (comp_unit_die ());
30287 /* Each new type_unit DIE was added to the limbo die list when created.
30288 Since these have all been added to comdat_type_list, clear the
30289 limbo die list. */
30290 limbo_die_list = NULL;
30292 /* For each new comdat type unit, copy declarations for incomplete
30293 types to make the new unit self-contained (i.e., no direct
30294 references to the main compile unit). */
30295 for (comdat_type_node *ctnode = comdat_type_list;
30296 ctnode != NULL; ctnode = ctnode->next)
30297 copy_decls_for_unworthy_types (ctnode->root_die);
30298 copy_decls_for_unworthy_types (comp_unit_die ());
30300 /* In the process of copying declarations from one unit to another,
30301 we may have left some declarations behind that are no longer
30302 referenced. Prune them. */
30303 prune_unused_types ();
30306 /* Generate separate CUs for each of the include files we've seen.
30307 They will go into limbo_die_list and from there to cu_die_list. */
30308 if (flag_eliminate_dwarf2_dups)
30310 gcc_assert (limbo_die_list == NULL);
30311 break_out_includes (comp_unit_die ());
30312 limbo_die_node *cu;
30313 while ((cu = limbo_die_list))
30315 limbo_die_list = cu->next;
30316 cu->next = cu_die_list;
30317 cu_die_list = cu;
30321 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30322 with dw_val_class_decl_ref operand. */
30323 note_variable_value (comp_unit_die ());
30324 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30325 note_variable_value (node->die);
30326 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30327 ctnode = ctnode->next)
30328 note_variable_value (ctnode->root_die);
30329 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30330 note_variable_value (node->die);
30332 /* The early debug phase is now finished. */
30333 early_dwarf_finished = true;
30336 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30337 within the same process. For use by toplev::finalize. */
30339 void
30340 dwarf2out_c_finalize (void)
30342 last_var_location_insn = NULL;
30343 cached_next_real_insn = NULL;
30344 used_rtx_array = NULL;
30345 incomplete_types = NULL;
30346 decl_scope_table = NULL;
30347 debug_info_section = NULL;
30348 debug_skeleton_info_section = NULL;
30349 debug_abbrev_section = NULL;
30350 debug_skeleton_abbrev_section = NULL;
30351 debug_aranges_section = NULL;
30352 debug_addr_section = NULL;
30353 debug_macinfo_section = NULL;
30354 debug_line_section = NULL;
30355 debug_skeleton_line_section = NULL;
30356 debug_loc_section = NULL;
30357 debug_pubnames_section = NULL;
30358 debug_pubtypes_section = NULL;
30359 debug_str_section = NULL;
30360 debug_line_str_section = NULL;
30361 debug_str_dwo_section = NULL;
30362 debug_str_offsets_section = NULL;
30363 debug_ranges_section = NULL;
30364 debug_frame_section = NULL;
30365 fde_vec = NULL;
30366 debug_str_hash = NULL;
30367 debug_line_str_hash = NULL;
30368 skeleton_debug_str_hash = NULL;
30369 dw2_string_counter = 0;
30370 have_multiple_function_sections = false;
30371 text_section_used = false;
30372 cold_text_section_used = false;
30373 cold_text_section = NULL;
30374 current_unit_personality = NULL;
30376 early_dwarf = false;
30377 early_dwarf_finished = false;
30379 next_die_offset = 0;
30380 single_comp_unit_die = NULL;
30381 comdat_type_list = NULL;
30382 limbo_die_list = NULL;
30383 file_table = NULL;
30384 decl_die_table = NULL;
30385 common_block_die_table = NULL;
30386 decl_loc_table = NULL;
30387 call_arg_locations = NULL;
30388 call_arg_loc_last = NULL;
30389 call_site_count = -1;
30390 tail_call_site_count = -1;
30391 cached_dw_loc_list_table = NULL;
30392 abbrev_die_table = NULL;
30393 delete dwarf_proc_stack_usage_map;
30394 dwarf_proc_stack_usage_map = NULL;
30395 line_info_label_num = 0;
30396 cur_line_info_table = NULL;
30397 text_section_line_info = NULL;
30398 cold_text_section_line_info = NULL;
30399 separate_line_info = NULL;
30400 info_section_emitted = false;
30401 pubname_table = NULL;
30402 pubtype_table = NULL;
30403 macinfo_table = NULL;
30404 ranges_table = NULL;
30405 ranges_by_label = NULL;
30406 rnglist_idx = 0;
30407 have_location_lists = false;
30408 loclabel_num = 0;
30409 poc_label_num = 0;
30410 last_emitted_file = NULL;
30411 label_num = 0;
30412 tmpl_value_parm_die_table = NULL;
30413 generic_type_instances = NULL;
30414 frame_pointer_fb_offset = 0;
30415 frame_pointer_fb_offset_valid = false;
30416 base_types.release ();
30417 XDELETEVEC (producer_string);
30418 producer_string = NULL;
30421 #include "gt-dwarf2out.h"