2016-09-26 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / dwarf2out.c
blobed74190131b2bc77951778167b20161018230c0e
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "tm_p.h"
66 #include "stringpool.h"
67 #include "insn-config.h"
68 #include "ira.h"
69 #include "cgraph.h"
70 #include "diagnostic.h"
71 #include "fold-const.h"
72 #include "stor-layout.h"
73 #include "varasm.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "rtlhash.h"
77 #include "reload.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "dwarf2out.h"
81 #include "dwarf2asm.h"
82 #include "toplev.h"
83 #include "md5.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "common/common-target.h"
87 #include "langhooks.h"
88 #include "lra.h"
89 #include "dumpfile.h"
90 #include "opts.h"
91 #include "tree-dfa.h"
92 #include "gdb/gdb-index.h"
93 #include "rtl-iter.h"
95 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
96 static rtx_insn *last_var_location_insn;
97 static rtx_insn *cached_next_real_insn;
98 static void dwarf2out_decl (tree);
100 #ifndef XCOFF_DEBUGGING_INFO
101 #define XCOFF_DEBUGGING_INFO 0
102 #endif
104 #ifndef HAVE_XCOFF_DWARF_EXTRAS
105 #define HAVE_XCOFF_DWARF_EXTRAS 0
106 #endif
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
111 /* Define this macro to be a nonzero value if the directory specifications
112 which are output in the debug info should end with a separator. */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115 from generating indirect strings in DWARF2 debug information, for instance
116 if your target is stuck with an old version of GDB that is unable to
117 process them properly or uses VMS Debug. */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
124 /* ??? Poison these here until it can be done generically. They've been
125 totally replaced in this file; make sure it stays that way. */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
132 /* The size of the target's pointer type. */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
137 /* Array of RTXes referenced by the debugging information, which therefore
138 must be kept around forever. */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
141 /* A pointer to the base of a list of incomplete types which might be
142 completed at some later time. incomplete_types_list needs to be a
143 vec<tree, va_gc> *because we want to tell the garbage collector about
144 it. */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
147 /* A pointer to the base of a table of references to declaration
148 scopes. This table is a display which tracks the nesting
149 of declaration scopes at the current scope and containing
150 scopes. This table is used to find the proper place to
151 define type declaration DIE's. */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static const char *debug_macinfo_section_name;
163 static GTY(()) section *debug_line_section;
164 static GTY(()) section *debug_skeleton_line_section;
165 static GTY(()) section *debug_loc_section;
166 static GTY(()) section *debug_pubnames_section;
167 static GTY(()) section *debug_pubtypes_section;
168 static GTY(()) section *debug_str_section;
169 static GTY(()) section *debug_str_dwo_section;
170 static GTY(()) section *debug_str_offsets_section;
171 static GTY(()) section *debug_ranges_section;
172 static GTY(()) section *debug_frame_section;
174 /* Maximum size (in bytes) of an artificially generated label. */
175 #define MAX_ARTIFICIAL_LABEL_BYTES 30
177 /* According to the (draft) DWARF 3 specification, the initial length
178 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
179 bytes are 0xffffffff, followed by the length stored in the next 8
180 bytes.
182 However, the SGI/MIPS ABI uses an initial length which is equal to
183 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
185 #ifndef DWARF_INITIAL_LENGTH_SIZE
186 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
187 #endif
189 /* Round SIZE up to the nearest BOUNDARY. */
190 #define DWARF_ROUND(SIZE,BOUNDARY) \
191 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
193 /* CIE identifier. */
194 #if HOST_BITS_PER_WIDE_INT >= 64
195 #define DWARF_CIE_ID \
196 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
197 #else
198 #define DWARF_CIE_ID DW_CIE_ID
199 #endif
202 /* A vector for a table that contains frame description
203 information for each routine. */
204 #define NOT_INDEXED (-1U)
205 #define NO_INDEX_ASSIGNED (-2U)
207 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
209 struct GTY((for_user)) indirect_string_node {
210 const char *str;
211 unsigned int refcount;
212 enum dwarf_form form;
213 char *label;
214 unsigned int index;
217 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
219 typedef const char *compare_type;
221 static hashval_t hash (indirect_string_node *);
222 static bool equal (indirect_string_node *, const char *);
225 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
227 /* With split_debug_info, both the comp_dir and dwo_name go in the
228 main object file, rather than the dwo, similar to the force_direct
229 parameter elsewhere but with additional complications:
231 1) The string is needed in both the main object file and the dwo.
232 That is, the comp_dir and dwo_name will appear in both places.
234 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
235 DW_FORM_GNU_str_index.
237 3) GCC chooses the form to use late, depending on the size and
238 reference count.
240 Rather than forcing the all debug string handling functions and
241 callers to deal with these complications, simply use a separate,
242 special-cased string table for any attribute that should go in the
243 main object file. This limits the complexity to just the places
244 that need it. */
246 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
248 static GTY(()) int dw2_string_counter;
250 /* True if the compilation unit places functions in more than one section. */
251 static GTY(()) bool have_multiple_function_sections = false;
253 /* Whether the default text and cold text sections have been used at all. */
255 static GTY(()) bool text_section_used = false;
256 static GTY(()) bool cold_text_section_used = false;
258 /* The default cold text section. */
259 static GTY(()) section *cold_text_section;
261 /* The DIE for C++14 'auto' in a function return type. */
262 static GTY(()) dw_die_ref auto_die;
264 /* The DIE for C++14 'decltype(auto)' in a function return type. */
265 static GTY(()) dw_die_ref decltype_auto_die;
267 /* Forward declarations for functions defined in this file. */
269 static void output_call_frame_info (int);
270 static void dwarf2out_note_section_used (void);
272 /* Personality decl of current unit. Used only when assembler does not support
273 personality CFI. */
274 static GTY(()) rtx current_unit_personality;
276 /* Data and reference forms for relocatable data. */
277 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
278 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
280 #ifndef DEBUG_FRAME_SECTION
281 #define DEBUG_FRAME_SECTION ".debug_frame"
282 #endif
284 #ifndef FUNC_BEGIN_LABEL
285 #define FUNC_BEGIN_LABEL "LFB"
286 #endif
288 #ifndef FUNC_END_LABEL
289 #define FUNC_END_LABEL "LFE"
290 #endif
292 #ifndef PROLOGUE_END_LABEL
293 #define PROLOGUE_END_LABEL "LPE"
294 #endif
296 #ifndef EPILOGUE_BEGIN_LABEL
297 #define EPILOGUE_BEGIN_LABEL "LEB"
298 #endif
300 #ifndef FRAME_BEGIN_LABEL
301 #define FRAME_BEGIN_LABEL "Lframe"
302 #endif
303 #define CIE_AFTER_SIZE_LABEL "LSCIE"
304 #define CIE_END_LABEL "LECIE"
305 #define FDE_LABEL "LSFDE"
306 #define FDE_AFTER_SIZE_LABEL "LASFDE"
307 #define FDE_END_LABEL "LEFDE"
308 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
309 #define LINE_NUMBER_END_LABEL "LELT"
310 #define LN_PROLOG_AS_LABEL "LASLTP"
311 #define LN_PROLOG_END_LABEL "LELTP"
312 #define DIE_LABEL_PREFIX "DW"
314 /* Match the base name of a file to the base name of a compilation unit. */
316 static int
317 matches_main_base (const char *path)
319 /* Cache the last query. */
320 static const char *last_path = NULL;
321 static int last_match = 0;
322 if (path != last_path)
324 const char *base;
325 int length = base_of_path (path, &base);
326 last_path = path;
327 last_match = (length == main_input_baselength
328 && memcmp (base, main_input_basename, length) == 0);
330 return last_match;
333 #ifdef DEBUG_DEBUG_STRUCT
335 static int
336 dump_struct_debug (tree type, enum debug_info_usage usage,
337 enum debug_struct_file criterion, int generic,
338 int matches, int result)
340 /* Find the type name. */
341 tree type_decl = TYPE_STUB_DECL (type);
342 tree t = type_decl;
343 const char *name = 0;
344 if (TREE_CODE (t) == TYPE_DECL)
345 t = DECL_NAME (t);
346 if (t)
347 name = IDENTIFIER_POINTER (t);
349 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
350 criterion,
351 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
352 matches ? "bas" : "hdr",
353 generic ? "gen" : "ord",
354 usage == DINFO_USAGE_DFN ? ";" :
355 usage == DINFO_USAGE_DIR_USE ? "." : "*",
356 result,
357 (void*) type_decl, name);
358 return result;
360 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
361 dump_struct_debug (type, usage, criterion, generic, matches, result)
363 #else
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366 (result)
368 #endif
370 /* Get the number of HOST_WIDE_INTs needed to represent the precision
371 of the number. Some constants have a large uniform precision, so
372 we get the precision needed for the actual value of the number. */
374 static unsigned int
375 get_full_len (const wide_int &op)
377 int prec = wi::min_precision (op, UNSIGNED);
378 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
379 / HOST_BITS_PER_WIDE_INT);
382 static bool
383 should_emit_struct_debug (tree type, enum debug_info_usage usage)
385 enum debug_struct_file criterion;
386 tree type_decl;
387 bool generic = lang_hooks.types.generic_p (type);
389 if (generic)
390 criterion = debug_struct_generic[usage];
391 else
392 criterion = debug_struct_ordinary[usage];
394 if (criterion == DINFO_STRUCT_FILE_NONE)
395 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
396 if (criterion == DINFO_STRUCT_FILE_ANY)
397 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
399 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
401 if (type_decl != NULL)
403 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
407 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
410 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
413 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
414 switch to the data section instead, and write out a synthetic start label
415 for collect2 the first time around. */
417 static void
418 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
420 if (eh_frame_section == 0)
422 int flags;
424 if (EH_TABLES_CAN_BE_READ_ONLY)
426 int fde_encoding;
427 int per_encoding;
428 int lsda_encoding;
430 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
431 /*global=*/0);
432 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
433 /*global=*/1);
434 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
435 /*global=*/0);
436 flags = ((! flag_pic
437 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
438 && (fde_encoding & 0x70) != DW_EH_PE_aligned
439 && (per_encoding & 0x70) != DW_EH_PE_absptr
440 && (per_encoding & 0x70) != DW_EH_PE_aligned
441 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
442 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
443 ? 0 : SECTION_WRITE);
445 else
446 flags = SECTION_WRITE;
448 #ifdef EH_FRAME_SECTION_NAME
449 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
450 #else
451 eh_frame_section = ((flags == SECTION_WRITE)
452 ? data_section : readonly_data_section);
453 #endif /* EH_FRAME_SECTION_NAME */
456 switch_to_section (eh_frame_section);
458 #ifdef EH_FRAME_THROUGH_COLLECT2
459 /* We have no special eh_frame section. Emit special labels to guide
460 collect2. */
461 if (!back)
463 tree label = get_file_function_name ("F");
464 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
465 targetm.asm_out.globalize_label (asm_out_file,
466 IDENTIFIER_POINTER (label));
467 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
469 #endif
472 /* Switch [BACK] to the eh or debug frame table section, depending on
473 FOR_EH. */
475 static void
476 switch_to_frame_table_section (int for_eh, bool back)
478 if (for_eh)
479 switch_to_eh_frame_section (back);
480 else
482 if (!debug_frame_section)
483 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
484 SECTION_DEBUG, NULL);
485 switch_to_section (debug_frame_section);
489 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
491 enum dw_cfi_oprnd_type
492 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
494 switch (cfi)
496 case DW_CFA_nop:
497 case DW_CFA_GNU_window_save:
498 case DW_CFA_remember_state:
499 case DW_CFA_restore_state:
500 return dw_cfi_oprnd_unused;
502 case DW_CFA_set_loc:
503 case DW_CFA_advance_loc1:
504 case DW_CFA_advance_loc2:
505 case DW_CFA_advance_loc4:
506 case DW_CFA_MIPS_advance_loc8:
507 return dw_cfi_oprnd_addr;
509 case DW_CFA_offset:
510 case DW_CFA_offset_extended:
511 case DW_CFA_def_cfa:
512 case DW_CFA_offset_extended_sf:
513 case DW_CFA_def_cfa_sf:
514 case DW_CFA_restore:
515 case DW_CFA_restore_extended:
516 case DW_CFA_undefined:
517 case DW_CFA_same_value:
518 case DW_CFA_def_cfa_register:
519 case DW_CFA_register:
520 case DW_CFA_expression:
521 return dw_cfi_oprnd_reg_num;
523 case DW_CFA_def_cfa_offset:
524 case DW_CFA_GNU_args_size:
525 case DW_CFA_def_cfa_offset_sf:
526 return dw_cfi_oprnd_offset;
528 case DW_CFA_def_cfa_expression:
529 return dw_cfi_oprnd_loc;
531 default:
532 gcc_unreachable ();
536 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
538 enum dw_cfi_oprnd_type
539 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
541 switch (cfi)
543 case DW_CFA_def_cfa:
544 case DW_CFA_def_cfa_sf:
545 case DW_CFA_offset:
546 case DW_CFA_offset_extended_sf:
547 case DW_CFA_offset_extended:
548 return dw_cfi_oprnd_offset;
550 case DW_CFA_register:
551 return dw_cfi_oprnd_reg_num;
553 case DW_CFA_expression:
554 return dw_cfi_oprnd_loc;
556 default:
557 return dw_cfi_oprnd_unused;
561 /* Output one FDE. */
563 static void
564 output_fde (dw_fde_ref fde, bool for_eh, bool second,
565 char *section_start_label, int fde_encoding, char *augmentation,
566 bool any_lsda_needed, int lsda_encoding)
568 const char *begin, *end;
569 static unsigned int j;
570 char l1[20], l2[20];
572 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
573 /* empty */ 0);
574 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
575 for_eh + j);
576 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
577 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
578 if (!XCOFF_DEBUGGING_INFO || for_eh)
580 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
581 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
582 " indicating 64-bit DWARF extension");
583 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
584 "FDE Length");
586 ASM_OUTPUT_LABEL (asm_out_file, l1);
588 if (for_eh)
589 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
590 else
591 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
592 debug_frame_section, "FDE CIE offset");
594 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
595 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
597 if (for_eh)
599 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
600 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
601 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
602 "FDE initial location");
603 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
604 end, begin, "FDE address range");
606 else
608 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
609 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
612 if (augmentation[0])
614 if (any_lsda_needed)
616 int size = size_of_encoded_value (lsda_encoding);
618 if (lsda_encoding == DW_EH_PE_aligned)
620 int offset = ( 4 /* Length */
621 + 4 /* CIE offset */
622 + 2 * size_of_encoded_value (fde_encoding)
623 + 1 /* Augmentation size */ );
624 int pad = -offset & (PTR_SIZE - 1);
626 size += pad;
627 gcc_assert (size_of_uleb128 (size) == 1);
630 dw2_asm_output_data_uleb128 (size, "Augmentation size");
632 if (fde->uses_eh_lsda)
634 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
635 fde->funcdef_number);
636 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
637 gen_rtx_SYMBOL_REF (Pmode, l1),
638 false,
639 "Language Specific Data Area");
641 else
643 if (lsda_encoding == DW_EH_PE_aligned)
644 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
645 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
646 "Language Specific Data Area (none)");
649 else
650 dw2_asm_output_data_uleb128 (0, "Augmentation size");
653 /* Loop through the Call Frame Instructions associated with this FDE. */
654 fde->dw_fde_current_label = begin;
656 size_t from, until, i;
658 from = 0;
659 until = vec_safe_length (fde->dw_fde_cfi);
661 if (fde->dw_fde_second_begin == NULL)
663 else if (!second)
664 until = fde->dw_fde_switch_cfi_index;
665 else
666 from = fde->dw_fde_switch_cfi_index;
668 for (i = from; i < until; i++)
669 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
672 /* If we are to emit a ref/link from function bodies to their frame tables,
673 do it now. This is typically performed to make sure that tables
674 associated with functions are dragged with them and not discarded in
675 garbage collecting links. We need to do this on a per function basis to
676 cope with -ffunction-sections. */
678 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
679 /* Switch to the function section, emit the ref to the tables, and
680 switch *back* into the table section. */
681 switch_to_section (function_section (fde->decl));
682 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
683 switch_to_frame_table_section (for_eh, true);
684 #endif
686 /* Pad the FDE out to an address sized boundary. */
687 ASM_OUTPUT_ALIGN (asm_out_file,
688 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
689 ASM_OUTPUT_LABEL (asm_out_file, l2);
691 j += 2;
694 /* Return true if frame description entry FDE is needed for EH. */
696 static bool
697 fde_needed_for_eh_p (dw_fde_ref fde)
699 if (flag_asynchronous_unwind_tables)
700 return true;
702 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
703 return true;
705 if (fde->uses_eh_lsda)
706 return true;
708 /* If exceptions are enabled, we have collected nothrow info. */
709 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
710 return false;
712 return true;
715 /* Output the call frame information used to record information
716 that relates to calculating the frame pointer, and records the
717 location of saved registers. */
719 static void
720 output_call_frame_info (int for_eh)
722 unsigned int i;
723 dw_fde_ref fde;
724 dw_cfi_ref cfi;
725 char l1[20], l2[20], section_start_label[20];
726 bool any_lsda_needed = false;
727 char augmentation[6];
728 int augmentation_size;
729 int fde_encoding = DW_EH_PE_absptr;
730 int per_encoding = DW_EH_PE_absptr;
731 int lsda_encoding = DW_EH_PE_absptr;
732 int return_reg;
733 rtx personality = NULL;
734 int dw_cie_version;
736 /* Don't emit a CIE if there won't be any FDEs. */
737 if (!fde_vec)
738 return;
740 /* Nothing to do if the assembler's doing it all. */
741 if (dwarf2out_do_cfi_asm ())
742 return;
744 /* If we don't have any functions we'll want to unwind out of, don't emit
745 any EH unwind information. If we make FDEs linkonce, we may have to
746 emit an empty label for an FDE that wouldn't otherwise be emitted. We
747 want to avoid having an FDE kept around when the function it refers to
748 is discarded. Example where this matters: a primary function template
749 in C++ requires EH information, an explicit specialization doesn't. */
750 if (for_eh)
752 bool any_eh_needed = false;
754 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
756 if (fde->uses_eh_lsda)
757 any_eh_needed = any_lsda_needed = true;
758 else if (fde_needed_for_eh_p (fde))
759 any_eh_needed = true;
760 else if (TARGET_USES_WEAK_UNWIND_INFO)
761 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
764 if (!any_eh_needed)
765 return;
768 /* We're going to be generating comments, so turn on app. */
769 if (flag_debug_asm)
770 app_enable ();
772 /* Switch to the proper frame section, first time. */
773 switch_to_frame_table_section (for_eh, false);
775 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
776 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
778 /* Output the CIE. */
779 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
780 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
781 if (!XCOFF_DEBUGGING_INFO || for_eh)
783 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
784 dw2_asm_output_data (4, 0xffffffff,
785 "Initial length escape value indicating 64-bit DWARF extension");
786 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
787 "Length of Common Information Entry");
789 ASM_OUTPUT_LABEL (asm_out_file, l1);
791 /* Now that the CIE pointer is PC-relative for EH,
792 use 0 to identify the CIE. */
793 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
794 (for_eh ? 0 : DWARF_CIE_ID),
795 "CIE Identifier Tag");
797 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
798 use CIE version 1, unless that would produce incorrect results
799 due to overflowing the return register column. */
800 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
801 dw_cie_version = 1;
802 if (return_reg >= 256 || dwarf_version > 2)
803 dw_cie_version = 3;
804 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
806 augmentation[0] = 0;
807 augmentation_size = 0;
809 personality = current_unit_personality;
810 if (for_eh)
812 char *p;
814 /* Augmentation:
815 z Indicates that a uleb128 is present to size the
816 augmentation section.
817 L Indicates the encoding (and thus presence) of
818 an LSDA pointer in the FDE augmentation.
819 R Indicates a non-default pointer encoding for
820 FDE code pointers.
821 P Indicates the presence of an encoding + language
822 personality routine in the CIE augmentation. */
824 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
825 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
826 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
828 p = augmentation + 1;
829 if (personality)
831 *p++ = 'P';
832 augmentation_size += 1 + size_of_encoded_value (per_encoding);
833 assemble_external_libcall (personality);
835 if (any_lsda_needed)
837 *p++ = 'L';
838 augmentation_size += 1;
840 if (fde_encoding != DW_EH_PE_absptr)
842 *p++ = 'R';
843 augmentation_size += 1;
845 if (p > augmentation + 1)
847 augmentation[0] = 'z';
848 *p = '\0';
851 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
852 if (personality && per_encoding == DW_EH_PE_aligned)
854 int offset = ( 4 /* Length */
855 + 4 /* CIE Id */
856 + 1 /* CIE version */
857 + strlen (augmentation) + 1 /* Augmentation */
858 + size_of_uleb128 (1) /* Code alignment */
859 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
860 + 1 /* RA column */
861 + 1 /* Augmentation size */
862 + 1 /* Personality encoding */ );
863 int pad = -offset & (PTR_SIZE - 1);
865 augmentation_size += pad;
867 /* Augmentations should be small, so there's scarce need to
868 iterate for a solution. Die if we exceed one uleb128 byte. */
869 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
873 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
874 if (dw_cie_version >= 4)
876 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
877 dw2_asm_output_data (1, 0, "CIE Segment Size");
879 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
880 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
881 "CIE Data Alignment Factor");
883 if (dw_cie_version == 1)
884 dw2_asm_output_data (1, return_reg, "CIE RA Column");
885 else
886 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
888 if (augmentation[0])
890 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
891 if (personality)
893 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
894 eh_data_format_name (per_encoding));
895 dw2_asm_output_encoded_addr_rtx (per_encoding,
896 personality,
897 true, NULL);
900 if (any_lsda_needed)
901 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
902 eh_data_format_name (lsda_encoding));
904 if (fde_encoding != DW_EH_PE_absptr)
905 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
906 eh_data_format_name (fde_encoding));
909 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
910 output_cfi (cfi, NULL, for_eh);
912 /* Pad the CIE out to an address sized boundary. */
913 ASM_OUTPUT_ALIGN (asm_out_file,
914 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
915 ASM_OUTPUT_LABEL (asm_out_file, l2);
917 /* Loop through all of the FDE's. */
918 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
920 unsigned int k;
922 /* Don't emit EH unwind info for leaf functions that don't need it. */
923 if (for_eh && !fde_needed_for_eh_p (fde))
924 continue;
926 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
927 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
928 augmentation, any_lsda_needed, lsda_encoding);
931 if (for_eh && targetm.terminate_dw2_eh_frame_info)
932 dw2_asm_output_data (4, 0, "End of Table");
934 /* Turn off app to make assembly quicker. */
935 if (flag_debug_asm)
936 app_disable ();
939 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
941 static void
942 dwarf2out_do_cfi_startproc (bool second)
944 int enc;
945 rtx ref;
946 rtx personality = get_personality_function (current_function_decl);
948 fprintf (asm_out_file, "\t.cfi_startproc\n");
950 if (personality)
952 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
953 ref = personality;
955 /* ??? The GAS support isn't entirely consistent. We have to
956 handle indirect support ourselves, but PC-relative is done
957 in the assembler. Further, the assembler can't handle any
958 of the weirder relocation types. */
959 if (enc & DW_EH_PE_indirect)
960 ref = dw2_force_const_mem (ref, true);
962 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
963 output_addr_const (asm_out_file, ref);
964 fputc ('\n', asm_out_file);
967 if (crtl->uses_eh_lsda)
969 char lab[20];
971 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
972 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
973 current_function_funcdef_no);
974 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
975 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
986 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
987 this allocation may be done before pass_final. */
989 dw_fde_ref
990 dwarf2out_alloc_current_fde (void)
992 dw_fde_ref fde;
994 fde = ggc_cleared_alloc<dw_fde_node> ();
995 fde->decl = current_function_decl;
996 fde->funcdef_number = current_function_funcdef_no;
997 fde->fde_index = vec_safe_length (fde_vec);
998 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
999 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1000 fde->nothrow = crtl->nothrow;
1001 fde->drap_reg = INVALID_REGNUM;
1002 fde->vdrap_reg = INVALID_REGNUM;
1004 /* Record the FDE associated with this function. */
1005 cfun->fde = fde;
1006 vec_safe_push (fde_vec, fde);
1008 return fde;
1011 /* Output a marker (i.e. a label) for the beginning of a function, before
1012 the prologue. */
1014 void
1015 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1016 const char *file ATTRIBUTE_UNUSED)
1018 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1019 char * dup_label;
1020 dw_fde_ref fde;
1021 section *fnsec;
1022 bool do_frame;
1024 current_function_func_begin_label = NULL;
1026 do_frame = dwarf2out_do_frame ();
1028 /* ??? current_function_func_begin_label is also used by except.c for
1029 call-site information. We must emit this label if it might be used. */
1030 if (!do_frame
1031 && (!flag_exceptions
1032 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1033 return;
1035 fnsec = function_section (current_function_decl);
1036 switch_to_section (fnsec);
1037 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1038 current_function_funcdef_no);
1039 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1040 current_function_funcdef_no);
1041 dup_label = xstrdup (label);
1042 current_function_func_begin_label = dup_label;
1044 /* We can elide the fde allocation if we're not emitting debug info. */
1045 if (!do_frame)
1046 return;
1048 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1049 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1050 would include pass_dwarf2_frame. If we've not created the FDE yet,
1051 do so now. */
1052 fde = cfun->fde;
1053 if (fde == NULL)
1054 fde = dwarf2out_alloc_current_fde ();
1056 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1057 fde->dw_fde_begin = dup_label;
1058 fde->dw_fde_current_label = dup_label;
1059 fde->in_std_section = (fnsec == text_section
1060 || (cold_text_section && fnsec == cold_text_section));
1062 /* We only want to output line number information for the genuine dwarf2
1063 prologue case, not the eh frame case. */
1064 #ifdef DWARF2_DEBUGGING_INFO
1065 if (file)
1066 dwarf2out_source_line (line, file, 0, true);
1067 #endif
1069 if (dwarf2out_do_cfi_asm ())
1070 dwarf2out_do_cfi_startproc (false);
1071 else
1073 rtx personality = get_personality_function (current_function_decl);
1074 if (!current_unit_personality)
1075 current_unit_personality = personality;
1077 /* We cannot keep a current personality per function as without CFI
1078 asm, at the point where we emit the CFI data, there is no current
1079 function anymore. */
1080 if (personality && current_unit_personality != personality)
1081 sorry ("multiple EH personalities are supported only with assemblers "
1082 "supporting .cfi_personality directive");
1086 /* Output a marker (i.e. a label) for the end of the generated code
1087 for a function prologue. This gets called *after* the prologue code has
1088 been generated. */
1090 void
1091 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1092 const char *file ATTRIBUTE_UNUSED)
1094 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1096 /* Output a label to mark the endpoint of the code generated for this
1097 function. */
1098 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1099 current_function_funcdef_no);
1100 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1101 current_function_funcdef_no);
1102 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1105 /* Output a marker (i.e. a label) for the beginning of the generated code
1106 for a function epilogue. This gets called *before* the prologue code has
1107 been generated. */
1109 void
1110 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1111 const char *file ATTRIBUTE_UNUSED)
1113 dw_fde_ref fde = cfun->fde;
1114 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1116 if (fde->dw_fde_vms_begin_epilogue)
1117 return;
1119 /* Output a label to mark the endpoint of the code generated for this
1120 function. */
1121 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1122 current_function_funcdef_no);
1123 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1124 current_function_funcdef_no);
1125 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1128 /* Output a marker (i.e. a label) for the absolute end of the generated code
1129 for a function definition. This gets called *after* the epilogue code has
1130 been generated. */
1132 void
1133 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1134 const char *file ATTRIBUTE_UNUSED)
1136 dw_fde_ref fde;
1137 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1139 last_var_location_insn = NULL;
1140 cached_next_real_insn = NULL;
1142 if (dwarf2out_do_cfi_asm ())
1143 fprintf (asm_out_file, "\t.cfi_endproc\n");
1145 /* Output a label to mark the endpoint of the code generated for this
1146 function. */
1147 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1148 current_function_funcdef_no);
1149 ASM_OUTPUT_LABEL (asm_out_file, label);
1150 fde = cfun->fde;
1151 gcc_assert (fde != NULL);
1152 if (fde->dw_fde_second_begin == NULL)
1153 fde->dw_fde_end = xstrdup (label);
1156 void
1157 dwarf2out_frame_finish (void)
1159 /* Output call frame information. */
1160 if (targetm.debug_unwind_info () == UI_DWARF2)
1161 output_call_frame_info (0);
1163 /* Output another copy for the unwinder. */
1164 if ((flag_unwind_tables || flag_exceptions)
1165 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1166 output_call_frame_info (1);
1169 /* Note that the current function section is being used for code. */
1171 static void
1172 dwarf2out_note_section_used (void)
1174 section *sec = current_function_section ();
1175 if (sec == text_section)
1176 text_section_used = true;
1177 else if (sec == cold_text_section)
1178 cold_text_section_used = true;
1181 static void var_location_switch_text_section (void);
1182 static void set_cur_line_info_table (section *);
1184 void
1185 dwarf2out_switch_text_section (void)
1187 section *sect;
1188 dw_fde_ref fde = cfun->fde;
1190 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1192 if (!in_cold_section_p)
1194 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1195 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1196 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1198 else
1200 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1201 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1202 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1204 have_multiple_function_sections = true;
1206 /* There is no need to mark used sections when not debugging. */
1207 if (cold_text_section != NULL)
1208 dwarf2out_note_section_used ();
1210 if (dwarf2out_do_cfi_asm ())
1211 fprintf (asm_out_file, "\t.cfi_endproc\n");
1213 /* Now do the real section switch. */
1214 sect = current_function_section ();
1215 switch_to_section (sect);
1217 fde->second_in_std_section
1218 = (sect == text_section
1219 || (cold_text_section && sect == cold_text_section));
1221 if (dwarf2out_do_cfi_asm ())
1222 dwarf2out_do_cfi_startproc (true);
1224 var_location_switch_text_section ();
1226 if (cold_text_section != NULL)
1227 set_cur_line_info_table (sect);
1230 /* And now, the subset of the debugging information support code necessary
1231 for emitting location expressions. */
1233 /* Data about a single source file. */
1234 struct GTY((for_user)) dwarf_file_data {
1235 const char * filename;
1236 int emitted_number;
1239 /* Describe an entry into the .debug_addr section. */
1241 enum ate_kind {
1242 ate_kind_rtx,
1243 ate_kind_rtx_dtprel,
1244 ate_kind_label
1247 struct GTY((for_user)) addr_table_entry {
1248 enum ate_kind kind;
1249 unsigned int refcount;
1250 unsigned int index;
1251 union addr_table_entry_struct_union
1253 rtx GTY ((tag ("0"))) rtl;
1254 char * GTY ((tag ("1"))) label;
1256 GTY ((desc ("%1.kind"))) addr;
1259 /* Location lists are ranges + location descriptions for that range,
1260 so you can track variables that are in different places over
1261 their entire life. */
1262 typedef struct GTY(()) dw_loc_list_struct {
1263 dw_loc_list_ref dw_loc_next;
1264 const char *begin; /* Label and addr_entry for start of range */
1265 addr_table_entry *begin_entry;
1266 const char *end; /* Label for end of range */
1267 char *ll_symbol; /* Label for beginning of location list.
1268 Only on head of list */
1269 const char *section; /* Section this loclist is relative to */
1270 dw_loc_descr_ref expr;
1271 hashval_t hash;
1272 /* True if all addresses in this and subsequent lists are known to be
1273 resolved. */
1274 bool resolved_addr;
1275 /* True if this list has been replaced by dw_loc_next. */
1276 bool replaced;
1277 bool emitted;
1278 /* True if the range should be emitted even if begin and end
1279 are the same. */
1280 bool force;
1281 } dw_loc_list_node;
1283 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1284 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1286 /* Convert a DWARF stack opcode into its string name. */
1288 static const char *
1289 dwarf_stack_op_name (unsigned int op)
1291 const char *name = get_DW_OP_name (op);
1293 if (name != NULL)
1294 return name;
1296 return "OP_<unknown>";
1299 /* Return a pointer to a newly allocated location description. Location
1300 descriptions are simple expression terms that can be strung
1301 together to form more complicated location (address) descriptions. */
1303 static inline dw_loc_descr_ref
1304 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1305 unsigned HOST_WIDE_INT oprnd2)
1307 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1309 descr->dw_loc_opc = op;
1310 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1311 descr->dw_loc_oprnd1.val_entry = NULL;
1312 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1313 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1314 descr->dw_loc_oprnd2.val_entry = NULL;
1315 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1317 return descr;
1320 /* Return a pointer to a newly allocated location description for
1321 REG and OFFSET. */
1323 static inline dw_loc_descr_ref
1324 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1326 if (reg <= 31)
1327 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1328 offset, 0);
1329 else
1330 return new_loc_descr (DW_OP_bregx, reg, offset);
1333 /* Add a location description term to a location description expression. */
1335 static inline void
1336 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1338 dw_loc_descr_ref *d;
1340 /* Find the end of the chain. */
1341 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1344 *d = descr;
1347 /* Compare two location operands for exact equality. */
1349 static bool
1350 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1352 if (a->val_class != b->val_class)
1353 return false;
1354 switch (a->val_class)
1356 case dw_val_class_none:
1357 return true;
1358 case dw_val_class_addr:
1359 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1361 case dw_val_class_offset:
1362 case dw_val_class_unsigned_const:
1363 case dw_val_class_const:
1364 case dw_val_class_range_list:
1365 case dw_val_class_lineptr:
1366 case dw_val_class_macptr:
1367 /* These are all HOST_WIDE_INT, signed or unsigned. */
1368 return a->v.val_unsigned == b->v.val_unsigned;
1370 case dw_val_class_loc:
1371 return a->v.val_loc == b->v.val_loc;
1372 case dw_val_class_loc_list:
1373 return a->v.val_loc_list == b->v.val_loc_list;
1374 case dw_val_class_die_ref:
1375 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1376 case dw_val_class_fde_ref:
1377 return a->v.val_fde_index == b->v.val_fde_index;
1378 case dw_val_class_lbl_id:
1379 case dw_val_class_high_pc:
1380 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1381 case dw_val_class_str:
1382 return a->v.val_str == b->v.val_str;
1383 case dw_val_class_flag:
1384 return a->v.val_flag == b->v.val_flag;
1385 case dw_val_class_file:
1386 return a->v.val_file == b->v.val_file;
1387 case dw_val_class_decl_ref:
1388 return a->v.val_decl_ref == b->v.val_decl_ref;
1390 case dw_val_class_const_double:
1391 return (a->v.val_double.high == b->v.val_double.high
1392 && a->v.val_double.low == b->v.val_double.low);
1394 case dw_val_class_wide_int:
1395 return *a->v.val_wide == *b->v.val_wide;
1397 case dw_val_class_vec:
1399 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1400 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1402 return (a_len == b_len
1403 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1406 case dw_val_class_data8:
1407 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1409 case dw_val_class_vms_delta:
1410 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1411 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1413 case dw_val_class_discr_value:
1414 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1415 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1416 case dw_val_class_discr_list:
1417 /* It makes no sense comparing two discriminant value lists. */
1418 return false;
1420 gcc_unreachable ();
1423 /* Compare two location atoms for exact equality. */
1425 static bool
1426 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1428 if (a->dw_loc_opc != b->dw_loc_opc)
1429 return false;
1431 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1432 address size, but since we always allocate cleared storage it
1433 should be zero for other types of locations. */
1434 if (a->dtprel != b->dtprel)
1435 return false;
1437 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1438 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1441 /* Compare two complete location expressions for exact equality. */
1443 bool
1444 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1446 while (1)
1448 if (a == b)
1449 return true;
1450 if (a == NULL || b == NULL)
1451 return false;
1452 if (!loc_descr_equal_p_1 (a, b))
1453 return false;
1455 a = a->dw_loc_next;
1456 b = b->dw_loc_next;
1461 /* Add a constant OFFSET to a location expression. */
1463 static void
1464 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1466 dw_loc_descr_ref loc;
1467 HOST_WIDE_INT *p;
1469 gcc_assert (*list_head != NULL);
1471 if (!offset)
1472 return;
1474 /* Find the end of the chain. */
1475 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1478 p = NULL;
1479 if (loc->dw_loc_opc == DW_OP_fbreg
1480 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1481 p = &loc->dw_loc_oprnd1.v.val_int;
1482 else if (loc->dw_loc_opc == DW_OP_bregx)
1483 p = &loc->dw_loc_oprnd2.v.val_int;
1485 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1486 offset. Don't optimize if an signed integer overflow would happen. */
1487 if (p != NULL
1488 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1489 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1490 *p += offset;
1492 else if (offset > 0)
1493 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1495 else
1497 loc->dw_loc_next = int_loc_descriptor (-offset);
1498 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1502 /* Add a constant OFFSET to a location list. */
1504 static void
1505 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1507 dw_loc_list_ref d;
1508 for (d = list_head; d != NULL; d = d->dw_loc_next)
1509 loc_descr_plus_const (&d->expr, offset);
1512 #define DWARF_REF_SIZE \
1513 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1515 static unsigned long int get_base_type_offset (dw_die_ref);
1517 /* Return the size of a location descriptor. */
1519 static unsigned long
1520 size_of_loc_descr (dw_loc_descr_ref loc)
1522 unsigned long size = 1;
1524 switch (loc->dw_loc_opc)
1526 case DW_OP_addr:
1527 size += DWARF2_ADDR_SIZE;
1528 break;
1529 case DW_OP_GNU_addr_index:
1530 case DW_OP_GNU_const_index:
1531 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1532 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1533 break;
1534 case DW_OP_const1u:
1535 case DW_OP_const1s:
1536 size += 1;
1537 break;
1538 case DW_OP_const2u:
1539 case DW_OP_const2s:
1540 size += 2;
1541 break;
1542 case DW_OP_const4u:
1543 case DW_OP_const4s:
1544 size += 4;
1545 break;
1546 case DW_OP_const8u:
1547 case DW_OP_const8s:
1548 size += 8;
1549 break;
1550 case DW_OP_constu:
1551 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1552 break;
1553 case DW_OP_consts:
1554 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1555 break;
1556 case DW_OP_pick:
1557 size += 1;
1558 break;
1559 case DW_OP_plus_uconst:
1560 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1561 break;
1562 case DW_OP_skip:
1563 case DW_OP_bra:
1564 size += 2;
1565 break;
1566 case DW_OP_breg0:
1567 case DW_OP_breg1:
1568 case DW_OP_breg2:
1569 case DW_OP_breg3:
1570 case DW_OP_breg4:
1571 case DW_OP_breg5:
1572 case DW_OP_breg6:
1573 case DW_OP_breg7:
1574 case DW_OP_breg8:
1575 case DW_OP_breg9:
1576 case DW_OP_breg10:
1577 case DW_OP_breg11:
1578 case DW_OP_breg12:
1579 case DW_OP_breg13:
1580 case DW_OP_breg14:
1581 case DW_OP_breg15:
1582 case DW_OP_breg16:
1583 case DW_OP_breg17:
1584 case DW_OP_breg18:
1585 case DW_OP_breg19:
1586 case DW_OP_breg20:
1587 case DW_OP_breg21:
1588 case DW_OP_breg22:
1589 case DW_OP_breg23:
1590 case DW_OP_breg24:
1591 case DW_OP_breg25:
1592 case DW_OP_breg26:
1593 case DW_OP_breg27:
1594 case DW_OP_breg28:
1595 case DW_OP_breg29:
1596 case DW_OP_breg30:
1597 case DW_OP_breg31:
1598 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1599 break;
1600 case DW_OP_regx:
1601 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1602 break;
1603 case DW_OP_fbreg:
1604 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1605 break;
1606 case DW_OP_bregx:
1607 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1608 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1609 break;
1610 case DW_OP_piece:
1611 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1612 break;
1613 case DW_OP_bit_piece:
1614 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1615 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1616 break;
1617 case DW_OP_deref_size:
1618 case DW_OP_xderef_size:
1619 size += 1;
1620 break;
1621 case DW_OP_call2:
1622 size += 2;
1623 break;
1624 case DW_OP_call4:
1625 size += 4;
1626 break;
1627 case DW_OP_call_ref:
1628 size += DWARF_REF_SIZE;
1629 break;
1630 case DW_OP_implicit_value:
1631 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1632 + loc->dw_loc_oprnd1.v.val_unsigned;
1633 break;
1634 case DW_OP_GNU_implicit_pointer:
1635 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1636 break;
1637 case DW_OP_GNU_entry_value:
1639 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1640 size += size_of_uleb128 (op_size) + op_size;
1641 break;
1643 case DW_OP_GNU_const_type:
1645 unsigned long o
1646 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1647 size += size_of_uleb128 (o) + 1;
1648 switch (loc->dw_loc_oprnd2.val_class)
1650 case dw_val_class_vec:
1651 size += loc->dw_loc_oprnd2.v.val_vec.length
1652 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1653 break;
1654 case dw_val_class_const:
1655 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1656 break;
1657 case dw_val_class_const_double:
1658 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1659 break;
1660 case dw_val_class_wide_int:
1661 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1662 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1663 break;
1664 default:
1665 gcc_unreachable ();
1667 break;
1669 case DW_OP_GNU_regval_type:
1671 unsigned long o
1672 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1673 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1674 + size_of_uleb128 (o);
1676 break;
1677 case DW_OP_GNU_deref_type:
1679 unsigned long o
1680 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1681 size += 1 + size_of_uleb128 (o);
1683 break;
1684 case DW_OP_GNU_convert:
1685 case DW_OP_GNU_reinterpret:
1686 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1687 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1688 else
1690 unsigned long o
1691 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1692 size += size_of_uleb128 (o);
1694 break;
1695 case DW_OP_GNU_parameter_ref:
1696 size += 4;
1697 break;
1698 default:
1699 break;
1702 return size;
1705 /* Return the size of a series of location descriptors. */
1707 unsigned long
1708 size_of_locs (dw_loc_descr_ref loc)
1710 dw_loc_descr_ref l;
1711 unsigned long size;
1713 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1714 field, to avoid writing to a PCH file. */
1715 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1717 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1718 break;
1719 size += size_of_loc_descr (l);
1721 if (! l)
1722 return size;
1724 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1726 l->dw_loc_addr = size;
1727 size += size_of_loc_descr (l);
1730 return size;
1733 /* Return the size of the value in a DW_AT_discr_value attribute. */
1735 static int
1736 size_of_discr_value (dw_discr_value *discr_value)
1738 if (discr_value->pos)
1739 return size_of_uleb128 (discr_value->v.uval);
1740 else
1741 return size_of_sleb128 (discr_value->v.sval);
1744 /* Return the size of the value in a DW_discr_list attribute. */
1746 static int
1747 size_of_discr_list (dw_discr_list_ref discr_list)
1749 int size = 0;
1751 for (dw_discr_list_ref list = discr_list;
1752 list != NULL;
1753 list = list->dw_discr_next)
1755 /* One byte for the discriminant value descriptor, and then one or two
1756 LEB128 numbers, depending on whether it's a single case label or a
1757 range label. */
1758 size += 1;
1759 size += size_of_discr_value (&list->dw_discr_lower_bound);
1760 if (list->dw_discr_range != 0)
1761 size += size_of_discr_value (&list->dw_discr_upper_bound);
1763 return size;
1766 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1767 static void get_ref_die_offset_label (char *, dw_die_ref);
1768 static unsigned long int get_ref_die_offset (dw_die_ref);
1770 /* Output location description stack opcode's operands (if any).
1771 The for_eh_or_skip parameter controls whether register numbers are
1772 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1773 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1774 info). This should be suppressed for the cases that have not been converted
1775 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1777 static void
1778 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1780 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1781 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1783 switch (loc->dw_loc_opc)
1785 #ifdef DWARF2_DEBUGGING_INFO
1786 case DW_OP_const2u:
1787 case DW_OP_const2s:
1788 dw2_asm_output_data (2, val1->v.val_int, NULL);
1789 break;
1790 case DW_OP_const4u:
1791 if (loc->dtprel)
1793 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1794 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1795 val1->v.val_addr);
1796 fputc ('\n', asm_out_file);
1797 break;
1799 /* FALLTHRU */
1800 case DW_OP_const4s:
1801 dw2_asm_output_data (4, val1->v.val_int, NULL);
1802 break;
1803 case DW_OP_const8u:
1804 if (loc->dtprel)
1806 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1807 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1808 val1->v.val_addr);
1809 fputc ('\n', asm_out_file);
1810 break;
1812 /* FALLTHRU */
1813 case DW_OP_const8s:
1814 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1815 dw2_asm_output_data (8, val1->v.val_int, NULL);
1816 break;
1817 case DW_OP_skip:
1818 case DW_OP_bra:
1820 int offset;
1822 gcc_assert (val1->val_class == dw_val_class_loc);
1823 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1825 dw2_asm_output_data (2, offset, NULL);
1827 break;
1828 case DW_OP_implicit_value:
1829 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1830 switch (val2->val_class)
1832 case dw_val_class_const:
1833 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1834 break;
1835 case dw_val_class_vec:
1837 unsigned int elt_size = val2->v.val_vec.elt_size;
1838 unsigned int len = val2->v.val_vec.length;
1839 unsigned int i;
1840 unsigned char *p;
1842 if (elt_size > sizeof (HOST_WIDE_INT))
1844 elt_size /= 2;
1845 len *= 2;
1847 for (i = 0, p = val2->v.val_vec.array;
1848 i < len;
1849 i++, p += elt_size)
1850 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1851 "fp or vector constant word %u", i);
1853 break;
1854 case dw_val_class_const_double:
1856 unsigned HOST_WIDE_INT first, second;
1858 if (WORDS_BIG_ENDIAN)
1860 first = val2->v.val_double.high;
1861 second = val2->v.val_double.low;
1863 else
1865 first = val2->v.val_double.low;
1866 second = val2->v.val_double.high;
1868 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1869 first, NULL);
1870 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1871 second, NULL);
1873 break;
1874 case dw_val_class_wide_int:
1876 int i;
1877 int len = get_full_len (*val2->v.val_wide);
1878 if (WORDS_BIG_ENDIAN)
1879 for (i = len - 1; i >= 0; --i)
1880 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1881 val2->v.val_wide->elt (i), NULL);
1882 else
1883 for (i = 0; i < len; ++i)
1884 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1885 val2->v.val_wide->elt (i), NULL);
1887 break;
1888 case dw_val_class_addr:
1889 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1890 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1891 break;
1892 default:
1893 gcc_unreachable ();
1895 break;
1896 #else
1897 case DW_OP_const2u:
1898 case DW_OP_const2s:
1899 case DW_OP_const4u:
1900 case DW_OP_const4s:
1901 case DW_OP_const8u:
1902 case DW_OP_const8s:
1903 case DW_OP_skip:
1904 case DW_OP_bra:
1905 case DW_OP_implicit_value:
1906 /* We currently don't make any attempt to make sure these are
1907 aligned properly like we do for the main unwind info, so
1908 don't support emitting things larger than a byte if we're
1909 only doing unwinding. */
1910 gcc_unreachable ();
1911 #endif
1912 case DW_OP_const1u:
1913 case DW_OP_const1s:
1914 dw2_asm_output_data (1, val1->v.val_int, NULL);
1915 break;
1916 case DW_OP_constu:
1917 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1918 break;
1919 case DW_OP_consts:
1920 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1921 break;
1922 case DW_OP_pick:
1923 dw2_asm_output_data (1, val1->v.val_int, NULL);
1924 break;
1925 case DW_OP_plus_uconst:
1926 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1927 break;
1928 case DW_OP_breg0:
1929 case DW_OP_breg1:
1930 case DW_OP_breg2:
1931 case DW_OP_breg3:
1932 case DW_OP_breg4:
1933 case DW_OP_breg5:
1934 case DW_OP_breg6:
1935 case DW_OP_breg7:
1936 case DW_OP_breg8:
1937 case DW_OP_breg9:
1938 case DW_OP_breg10:
1939 case DW_OP_breg11:
1940 case DW_OP_breg12:
1941 case DW_OP_breg13:
1942 case DW_OP_breg14:
1943 case DW_OP_breg15:
1944 case DW_OP_breg16:
1945 case DW_OP_breg17:
1946 case DW_OP_breg18:
1947 case DW_OP_breg19:
1948 case DW_OP_breg20:
1949 case DW_OP_breg21:
1950 case DW_OP_breg22:
1951 case DW_OP_breg23:
1952 case DW_OP_breg24:
1953 case DW_OP_breg25:
1954 case DW_OP_breg26:
1955 case DW_OP_breg27:
1956 case DW_OP_breg28:
1957 case DW_OP_breg29:
1958 case DW_OP_breg30:
1959 case DW_OP_breg31:
1960 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1961 break;
1962 case DW_OP_regx:
1964 unsigned r = val1->v.val_unsigned;
1965 if (for_eh_or_skip >= 0)
1966 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1967 gcc_assert (size_of_uleb128 (r)
1968 == size_of_uleb128 (val1->v.val_unsigned));
1969 dw2_asm_output_data_uleb128 (r, NULL);
1971 break;
1972 case DW_OP_fbreg:
1973 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1974 break;
1975 case DW_OP_bregx:
1977 unsigned r = val1->v.val_unsigned;
1978 if (for_eh_or_skip >= 0)
1979 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1980 gcc_assert (size_of_uleb128 (r)
1981 == size_of_uleb128 (val1->v.val_unsigned));
1982 dw2_asm_output_data_uleb128 (r, NULL);
1983 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1985 break;
1986 case DW_OP_piece:
1987 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1988 break;
1989 case DW_OP_bit_piece:
1990 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1991 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1992 break;
1993 case DW_OP_deref_size:
1994 case DW_OP_xderef_size:
1995 dw2_asm_output_data (1, val1->v.val_int, NULL);
1996 break;
1998 case DW_OP_addr:
1999 if (loc->dtprel)
2001 if (targetm.asm_out.output_dwarf_dtprel)
2003 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2004 DWARF2_ADDR_SIZE,
2005 val1->v.val_addr);
2006 fputc ('\n', asm_out_file);
2008 else
2009 gcc_unreachable ();
2011 else
2013 #ifdef DWARF2_DEBUGGING_INFO
2014 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2015 #else
2016 gcc_unreachable ();
2017 #endif
2019 break;
2021 case DW_OP_GNU_addr_index:
2022 case DW_OP_GNU_const_index:
2023 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2024 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2025 "(index into .debug_addr)");
2026 break;
2028 case DW_OP_call2:
2029 case DW_OP_call4:
2031 unsigned long die_offset
2032 = get_ref_die_offset (val1->v.val_die_ref.die);
2033 /* Make sure the offset has been computed and that we can encode it as
2034 an operand. */
2035 gcc_assert (die_offset > 0
2036 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2037 ? 0xffff
2038 : 0xffffffff));
2039 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2040 die_offset, NULL);
2042 break;
2044 case DW_OP_GNU_implicit_pointer:
2046 char label[MAX_ARTIFICIAL_LABEL_BYTES
2047 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2048 gcc_assert (val1->val_class == dw_val_class_die_ref);
2049 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2050 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2051 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2053 break;
2055 case DW_OP_GNU_entry_value:
2056 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2057 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2058 break;
2060 case DW_OP_GNU_const_type:
2062 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2063 gcc_assert (o);
2064 dw2_asm_output_data_uleb128 (o, NULL);
2065 switch (val2->val_class)
2067 case dw_val_class_const:
2068 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2069 dw2_asm_output_data (1, l, NULL);
2070 dw2_asm_output_data (l, val2->v.val_int, NULL);
2071 break;
2072 case dw_val_class_vec:
2074 unsigned int elt_size = val2->v.val_vec.elt_size;
2075 unsigned int len = val2->v.val_vec.length;
2076 unsigned int i;
2077 unsigned char *p;
2079 l = len * elt_size;
2080 dw2_asm_output_data (1, l, NULL);
2081 if (elt_size > sizeof (HOST_WIDE_INT))
2083 elt_size /= 2;
2084 len *= 2;
2086 for (i = 0, p = val2->v.val_vec.array;
2087 i < len;
2088 i++, p += elt_size)
2089 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2090 "fp or vector constant word %u", i);
2092 break;
2093 case dw_val_class_const_double:
2095 unsigned HOST_WIDE_INT first, second;
2096 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2098 dw2_asm_output_data (1, 2 * l, NULL);
2099 if (WORDS_BIG_ENDIAN)
2101 first = val2->v.val_double.high;
2102 second = val2->v.val_double.low;
2104 else
2106 first = val2->v.val_double.low;
2107 second = val2->v.val_double.high;
2109 dw2_asm_output_data (l, first, NULL);
2110 dw2_asm_output_data (l, second, NULL);
2112 break;
2113 case dw_val_class_wide_int:
2115 int i;
2116 int len = get_full_len (*val2->v.val_wide);
2117 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2119 dw2_asm_output_data (1, len * l, NULL);
2120 if (WORDS_BIG_ENDIAN)
2121 for (i = len - 1; i >= 0; --i)
2122 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2123 else
2124 for (i = 0; i < len; ++i)
2125 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2127 break;
2128 default:
2129 gcc_unreachable ();
2132 break;
2133 case DW_OP_GNU_regval_type:
2135 unsigned r = val1->v.val_unsigned;
2136 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2137 gcc_assert (o);
2138 if (for_eh_or_skip >= 0)
2140 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2141 gcc_assert (size_of_uleb128 (r)
2142 == size_of_uleb128 (val1->v.val_unsigned));
2144 dw2_asm_output_data_uleb128 (r, NULL);
2145 dw2_asm_output_data_uleb128 (o, NULL);
2147 break;
2148 case DW_OP_GNU_deref_type:
2150 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2151 gcc_assert (o);
2152 dw2_asm_output_data (1, val1->v.val_int, NULL);
2153 dw2_asm_output_data_uleb128 (o, NULL);
2155 break;
2156 case DW_OP_GNU_convert:
2157 case DW_OP_GNU_reinterpret:
2158 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2159 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2160 else
2162 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2163 gcc_assert (o);
2164 dw2_asm_output_data_uleb128 (o, NULL);
2166 break;
2168 case DW_OP_GNU_parameter_ref:
2170 unsigned long o;
2171 gcc_assert (val1->val_class == dw_val_class_die_ref);
2172 o = get_ref_die_offset (val1->v.val_die_ref.die);
2173 dw2_asm_output_data (4, o, NULL);
2175 break;
2177 default:
2178 /* Other codes have no operands. */
2179 break;
2183 /* Output a sequence of location operations.
2184 The for_eh_or_skip parameter controls whether register numbers are
2185 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2186 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2187 info). This should be suppressed for the cases that have not been converted
2188 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2190 void
2191 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2193 for (; loc != NULL; loc = loc->dw_loc_next)
2195 enum dwarf_location_atom opc = loc->dw_loc_opc;
2196 /* Output the opcode. */
2197 if (for_eh_or_skip >= 0
2198 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2200 unsigned r = (opc - DW_OP_breg0);
2201 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2202 gcc_assert (r <= 31);
2203 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2205 else if (for_eh_or_skip >= 0
2206 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2208 unsigned r = (opc - DW_OP_reg0);
2209 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2210 gcc_assert (r <= 31);
2211 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2214 dw2_asm_output_data (1, opc,
2215 "%s", dwarf_stack_op_name (opc));
2217 /* Output the operand(s) (if any). */
2218 output_loc_operands (loc, for_eh_or_skip);
2222 /* Output location description stack opcode's operands (if any).
2223 The output is single bytes on a line, suitable for .cfi_escape. */
2225 static void
2226 output_loc_operands_raw (dw_loc_descr_ref loc)
2228 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2229 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2231 switch (loc->dw_loc_opc)
2233 case DW_OP_addr:
2234 case DW_OP_GNU_addr_index:
2235 case DW_OP_GNU_const_index:
2236 case DW_OP_implicit_value:
2237 /* We cannot output addresses in .cfi_escape, only bytes. */
2238 gcc_unreachable ();
2240 case DW_OP_const1u:
2241 case DW_OP_const1s:
2242 case DW_OP_pick:
2243 case DW_OP_deref_size:
2244 case DW_OP_xderef_size:
2245 fputc (',', asm_out_file);
2246 dw2_asm_output_data_raw (1, val1->v.val_int);
2247 break;
2249 case DW_OP_const2u:
2250 case DW_OP_const2s:
2251 fputc (',', asm_out_file);
2252 dw2_asm_output_data_raw (2, val1->v.val_int);
2253 break;
2255 case DW_OP_const4u:
2256 case DW_OP_const4s:
2257 fputc (',', asm_out_file);
2258 dw2_asm_output_data_raw (4, val1->v.val_int);
2259 break;
2261 case DW_OP_const8u:
2262 case DW_OP_const8s:
2263 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2264 fputc (',', asm_out_file);
2265 dw2_asm_output_data_raw (8, val1->v.val_int);
2266 break;
2268 case DW_OP_skip:
2269 case DW_OP_bra:
2271 int offset;
2273 gcc_assert (val1->val_class == dw_val_class_loc);
2274 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2276 fputc (',', asm_out_file);
2277 dw2_asm_output_data_raw (2, offset);
2279 break;
2281 case DW_OP_regx:
2283 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2284 gcc_assert (size_of_uleb128 (r)
2285 == size_of_uleb128 (val1->v.val_unsigned));
2286 fputc (',', asm_out_file);
2287 dw2_asm_output_data_uleb128_raw (r);
2289 break;
2291 case DW_OP_constu:
2292 case DW_OP_plus_uconst:
2293 case DW_OP_piece:
2294 fputc (',', asm_out_file);
2295 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2296 break;
2298 case DW_OP_bit_piece:
2299 fputc (',', asm_out_file);
2300 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2301 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2302 break;
2304 case DW_OP_consts:
2305 case DW_OP_breg0:
2306 case DW_OP_breg1:
2307 case DW_OP_breg2:
2308 case DW_OP_breg3:
2309 case DW_OP_breg4:
2310 case DW_OP_breg5:
2311 case DW_OP_breg6:
2312 case DW_OP_breg7:
2313 case DW_OP_breg8:
2314 case DW_OP_breg9:
2315 case DW_OP_breg10:
2316 case DW_OP_breg11:
2317 case DW_OP_breg12:
2318 case DW_OP_breg13:
2319 case DW_OP_breg14:
2320 case DW_OP_breg15:
2321 case DW_OP_breg16:
2322 case DW_OP_breg17:
2323 case DW_OP_breg18:
2324 case DW_OP_breg19:
2325 case DW_OP_breg20:
2326 case DW_OP_breg21:
2327 case DW_OP_breg22:
2328 case DW_OP_breg23:
2329 case DW_OP_breg24:
2330 case DW_OP_breg25:
2331 case DW_OP_breg26:
2332 case DW_OP_breg27:
2333 case DW_OP_breg28:
2334 case DW_OP_breg29:
2335 case DW_OP_breg30:
2336 case DW_OP_breg31:
2337 case DW_OP_fbreg:
2338 fputc (',', asm_out_file);
2339 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2340 break;
2342 case DW_OP_bregx:
2344 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2345 gcc_assert (size_of_uleb128 (r)
2346 == size_of_uleb128 (val1->v.val_unsigned));
2347 fputc (',', asm_out_file);
2348 dw2_asm_output_data_uleb128_raw (r);
2349 fputc (',', asm_out_file);
2350 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2352 break;
2354 case DW_OP_GNU_implicit_pointer:
2355 case DW_OP_GNU_entry_value:
2356 case DW_OP_GNU_const_type:
2357 case DW_OP_GNU_regval_type:
2358 case DW_OP_GNU_deref_type:
2359 case DW_OP_GNU_convert:
2360 case DW_OP_GNU_reinterpret:
2361 case DW_OP_GNU_parameter_ref:
2362 gcc_unreachable ();
2363 break;
2365 default:
2366 /* Other codes have no operands. */
2367 break;
2371 void
2372 output_loc_sequence_raw (dw_loc_descr_ref loc)
2374 while (1)
2376 enum dwarf_location_atom opc = loc->dw_loc_opc;
2377 /* Output the opcode. */
2378 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2380 unsigned r = (opc - DW_OP_breg0);
2381 r = DWARF2_FRAME_REG_OUT (r, 1);
2382 gcc_assert (r <= 31);
2383 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2385 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2387 unsigned r = (opc - DW_OP_reg0);
2388 r = DWARF2_FRAME_REG_OUT (r, 1);
2389 gcc_assert (r <= 31);
2390 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2392 /* Output the opcode. */
2393 fprintf (asm_out_file, "%#x", opc);
2394 output_loc_operands_raw (loc);
2396 if (!loc->dw_loc_next)
2397 break;
2398 loc = loc->dw_loc_next;
2400 fputc (',', asm_out_file);
2404 /* This function builds a dwarf location descriptor sequence from a
2405 dw_cfa_location, adding the given OFFSET to the result of the
2406 expression. */
2408 struct dw_loc_descr_node *
2409 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2411 struct dw_loc_descr_node *head, *tmp;
2413 offset += cfa->offset;
2415 if (cfa->indirect)
2417 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2418 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2419 head->dw_loc_oprnd1.val_entry = NULL;
2420 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2421 add_loc_descr (&head, tmp);
2422 if (offset != 0)
2424 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2425 add_loc_descr (&head, tmp);
2428 else
2429 head = new_reg_loc_descr (cfa->reg, offset);
2431 return head;
2434 /* This function builds a dwarf location descriptor sequence for
2435 the address at OFFSET from the CFA when stack is aligned to
2436 ALIGNMENT byte. */
2438 struct dw_loc_descr_node *
2439 build_cfa_aligned_loc (dw_cfa_location *cfa,
2440 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2442 struct dw_loc_descr_node *head;
2443 unsigned int dwarf_fp
2444 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2446 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2447 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2449 head = new_reg_loc_descr (dwarf_fp, 0);
2450 add_loc_descr (&head, int_loc_descriptor (alignment));
2451 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2452 loc_descr_plus_const (&head, offset);
2454 else
2455 head = new_reg_loc_descr (dwarf_fp, offset);
2456 return head;
2459 /* And now, the support for symbolic debugging information. */
2461 /* .debug_str support. */
2463 static void dwarf2out_init (const char *);
2464 static void dwarf2out_finish (const char *);
2465 static void dwarf2out_early_finish (const char *);
2466 static void dwarf2out_assembly_start (void);
2467 static void dwarf2out_define (unsigned int, const char *);
2468 static void dwarf2out_undef (unsigned int, const char *);
2469 static void dwarf2out_start_source_file (unsigned, const char *);
2470 static void dwarf2out_end_source_file (unsigned);
2471 static void dwarf2out_function_decl (tree);
2472 static void dwarf2out_begin_block (unsigned, unsigned);
2473 static void dwarf2out_end_block (unsigned, unsigned);
2474 static bool dwarf2out_ignore_block (const_tree);
2475 static void dwarf2out_early_global_decl (tree);
2476 static void dwarf2out_late_global_decl (tree);
2477 static void dwarf2out_type_decl (tree, int);
2478 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2479 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2480 dw_die_ref);
2481 static void dwarf2out_abstract_function (tree);
2482 static void dwarf2out_var_location (rtx_insn *);
2483 static void dwarf2out_size_function (tree);
2484 static void dwarf2out_begin_function (tree);
2485 static void dwarf2out_end_function (unsigned int);
2486 static void dwarf2out_register_main_translation_unit (tree unit);
2487 static void dwarf2out_set_name (tree, tree);
2489 /* The debug hooks structure. */
2491 const struct gcc_debug_hooks dwarf2_debug_hooks =
2493 dwarf2out_init,
2494 dwarf2out_finish,
2495 dwarf2out_early_finish,
2496 dwarf2out_assembly_start,
2497 dwarf2out_define,
2498 dwarf2out_undef,
2499 dwarf2out_start_source_file,
2500 dwarf2out_end_source_file,
2501 dwarf2out_begin_block,
2502 dwarf2out_end_block,
2503 dwarf2out_ignore_block,
2504 dwarf2out_source_line,
2505 dwarf2out_begin_prologue,
2506 #if VMS_DEBUGGING_INFO
2507 dwarf2out_vms_end_prologue,
2508 dwarf2out_vms_begin_epilogue,
2509 #else
2510 debug_nothing_int_charstar,
2511 debug_nothing_int_charstar,
2512 #endif
2513 dwarf2out_end_epilogue,
2514 dwarf2out_begin_function,
2515 dwarf2out_end_function, /* end_function */
2516 dwarf2out_register_main_translation_unit,
2517 dwarf2out_function_decl, /* function_decl */
2518 dwarf2out_early_global_decl,
2519 dwarf2out_late_global_decl,
2520 dwarf2out_type_decl, /* type_decl */
2521 dwarf2out_imported_module_or_decl,
2522 debug_nothing_tree, /* deferred_inline_function */
2523 /* The DWARF 2 backend tries to reduce debugging bloat by not
2524 emitting the abstract description of inline functions until
2525 something tries to reference them. */
2526 dwarf2out_abstract_function, /* outlining_inline_function */
2527 debug_nothing_rtx_code_label, /* label */
2528 debug_nothing_int, /* handle_pch */
2529 dwarf2out_var_location,
2530 dwarf2out_size_function, /* size_function */
2531 dwarf2out_switch_text_section,
2532 dwarf2out_set_name,
2533 1, /* start_end_main_source_file */
2534 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2537 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2539 dwarf2out_init,
2540 debug_nothing_charstar,
2541 debug_nothing_charstar,
2542 debug_nothing_void,
2543 debug_nothing_int_charstar,
2544 debug_nothing_int_charstar,
2545 debug_nothing_int_charstar,
2546 debug_nothing_int,
2547 debug_nothing_int_int, /* begin_block */
2548 debug_nothing_int_int, /* end_block */
2549 debug_true_const_tree, /* ignore_block */
2550 dwarf2out_source_line, /* source_line */
2551 debug_nothing_int_charstar, /* begin_prologue */
2552 debug_nothing_int_charstar, /* end_prologue */
2553 debug_nothing_int_charstar, /* begin_epilogue */
2554 debug_nothing_int_charstar, /* end_epilogue */
2555 debug_nothing_tree, /* begin_function */
2556 debug_nothing_int, /* end_function */
2557 debug_nothing_tree, /* register_main_translation_unit */
2558 debug_nothing_tree, /* function_decl */
2559 debug_nothing_tree, /* early_global_decl */
2560 debug_nothing_tree, /* late_global_decl */
2561 debug_nothing_tree_int, /* type_decl */
2562 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2563 debug_nothing_tree, /* deferred_inline_function */
2564 debug_nothing_tree, /* outlining_inline_function */
2565 debug_nothing_rtx_code_label, /* label */
2566 debug_nothing_int, /* handle_pch */
2567 debug_nothing_rtx_insn, /* var_location */
2568 debug_nothing_tree, /* size_function */
2569 debug_nothing_void, /* switch_text_section */
2570 debug_nothing_tree_tree, /* set_name */
2571 0, /* start_end_main_source_file */
2572 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2575 /* NOTE: In the comments in this file, many references are made to
2576 "Debugging Information Entries". This term is abbreviated as `DIE'
2577 throughout the remainder of this file. */
2579 /* An internal representation of the DWARF output is built, and then
2580 walked to generate the DWARF debugging info. The walk of the internal
2581 representation is done after the entire program has been compiled.
2582 The types below are used to describe the internal representation. */
2584 /* Whether to put type DIEs into their own section .debug_types instead
2585 of making them part of the .debug_info section. Only supported for
2586 Dwarf V4 or higher and the user didn't disable them through
2587 -fno-debug-types-section. It is more efficient to put them in a
2588 separate comdat sections since the linker will then be able to
2589 remove duplicates. But not all tools support .debug_types sections
2590 yet. */
2592 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2594 /* Various DIE's use offsets relative to the beginning of the
2595 .debug_info section to refer to each other. */
2597 typedef long int dw_offset;
2599 struct comdat_type_node;
2601 /* The entries in the line_info table more-or-less mirror the opcodes
2602 that are used in the real dwarf line table. Arrays of these entries
2603 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2604 supported. */
2606 enum dw_line_info_opcode {
2607 /* Emit DW_LNE_set_address; the operand is the label index. */
2608 LI_set_address,
2610 /* Emit a row to the matrix with the given line. This may be done
2611 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2612 special opcodes. */
2613 LI_set_line,
2615 /* Emit a DW_LNS_set_file. */
2616 LI_set_file,
2618 /* Emit a DW_LNS_set_column. */
2619 LI_set_column,
2621 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2622 LI_negate_stmt,
2624 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2625 LI_set_prologue_end,
2626 LI_set_epilogue_begin,
2628 /* Emit a DW_LNE_set_discriminator. */
2629 LI_set_discriminator
2632 typedef struct GTY(()) dw_line_info_struct {
2633 enum dw_line_info_opcode opcode;
2634 unsigned int val;
2635 } dw_line_info_entry;
2638 struct GTY(()) dw_line_info_table {
2639 /* The label that marks the end of this section. */
2640 const char *end_label;
2642 /* The values for the last row of the matrix, as collected in the table.
2643 These are used to minimize the changes to the next row. */
2644 unsigned int file_num;
2645 unsigned int line_num;
2646 unsigned int column_num;
2647 int discrim_num;
2648 bool is_stmt;
2649 bool in_use;
2651 vec<dw_line_info_entry, va_gc> *entries;
2655 /* Each DIE attribute has a field specifying the attribute kind,
2656 a link to the next attribute in the chain, and an attribute value.
2657 Attributes are typically linked below the DIE they modify. */
2659 typedef struct GTY(()) dw_attr_struct {
2660 enum dwarf_attribute dw_attr;
2661 dw_val_node dw_attr_val;
2663 dw_attr_node;
2666 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2667 The children of each node form a circular list linked by
2668 die_sib. die_child points to the node *before* the "first" child node. */
2670 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2671 union die_symbol_or_type_node
2673 const char * GTY ((tag ("0"))) die_symbol;
2674 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2676 GTY ((desc ("%0.comdat_type_p"))) die_id;
2677 vec<dw_attr_node, va_gc> *die_attr;
2678 dw_die_ref die_parent;
2679 dw_die_ref die_child;
2680 dw_die_ref die_sib;
2681 dw_die_ref die_definition; /* ref from a specification to its definition */
2682 dw_offset die_offset;
2683 unsigned long die_abbrev;
2684 int die_mark;
2685 unsigned int decl_id;
2686 enum dwarf_tag die_tag;
2687 /* Die is used and must not be pruned as unused. */
2688 BOOL_BITFIELD die_perennial_p : 1;
2689 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2690 /* Lots of spare bits. */
2692 die_node;
2694 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2695 static bool early_dwarf;
2696 static bool early_dwarf_finished;
2697 struct set_early_dwarf {
2698 bool saved;
2699 set_early_dwarf () : saved(early_dwarf)
2701 gcc_assert (! early_dwarf_finished);
2702 early_dwarf = true;
2704 ~set_early_dwarf () { early_dwarf = saved; }
2707 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2708 #define FOR_EACH_CHILD(die, c, expr) do { \
2709 c = die->die_child; \
2710 if (c) do { \
2711 c = c->die_sib; \
2712 expr; \
2713 } while (c != die->die_child); \
2714 } while (0)
2716 /* The pubname structure */
2718 typedef struct GTY(()) pubname_struct {
2719 dw_die_ref die;
2720 const char *name;
2722 pubname_entry;
2725 struct GTY(()) dw_ranges {
2726 /* If this is positive, it's a block number, otherwise it's a
2727 bitwise-negated index into dw_ranges_by_label. */
2728 int num;
2731 /* A structure to hold a macinfo entry. */
2733 typedef struct GTY(()) macinfo_struct {
2734 unsigned char code;
2735 unsigned HOST_WIDE_INT lineno;
2736 const char *info;
2738 macinfo_entry;
2741 struct GTY(()) dw_ranges_by_label {
2742 const char *begin;
2743 const char *end;
2746 /* The comdat type node structure. */
2747 struct GTY(()) comdat_type_node
2749 dw_die_ref root_die;
2750 dw_die_ref type_die;
2751 dw_die_ref skeleton_die;
2752 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2753 comdat_type_node *next;
2756 /* A list of DIEs for which we can't determine ancestry (parent_die
2757 field) just yet. Later in dwarf2out_finish we will fill in the
2758 missing bits. */
2759 typedef struct GTY(()) limbo_die_struct {
2760 dw_die_ref die;
2761 /* The tree for which this DIE was created. We use this to
2762 determine ancestry later. */
2763 tree created_for;
2764 struct limbo_die_struct *next;
2766 limbo_die_node;
2768 typedef struct skeleton_chain_struct
2770 dw_die_ref old_die;
2771 dw_die_ref new_die;
2772 struct skeleton_chain_struct *parent;
2774 skeleton_chain_node;
2776 /* Define a macro which returns nonzero for a TYPE_DECL which was
2777 implicitly generated for a type.
2779 Note that, unlike the C front-end (which generates a NULL named
2780 TYPE_DECL node for each complete tagged type, each array type,
2781 and each function type node created) the C++ front-end generates
2782 a _named_ TYPE_DECL node for each tagged type node created.
2783 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2784 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2785 front-end, but for each type, tagged or not. */
2787 #define TYPE_DECL_IS_STUB(decl) \
2788 (DECL_NAME (decl) == NULL_TREE \
2789 || (DECL_ARTIFICIAL (decl) \
2790 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2791 /* This is necessary for stub decls that \
2792 appear in nested inline functions. */ \
2793 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2794 && (decl_ultimate_origin (decl) \
2795 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2797 /* Information concerning the compilation unit's programming
2798 language, and compiler version. */
2800 /* Fixed size portion of the DWARF compilation unit header. */
2801 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2802 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2804 /* Fixed size portion of the DWARF comdat type unit header. */
2805 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2806 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2807 + DWARF_OFFSET_SIZE)
2809 /* Fixed size portion of public names info. */
2810 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2812 /* Fixed size portion of the address range info. */
2813 #define DWARF_ARANGES_HEADER_SIZE \
2814 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2815 DWARF2_ADDR_SIZE * 2) \
2816 - DWARF_INITIAL_LENGTH_SIZE)
2818 /* Size of padding portion in the address range info. It must be
2819 aligned to twice the pointer size. */
2820 #define DWARF_ARANGES_PAD_SIZE \
2821 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2822 DWARF2_ADDR_SIZE * 2) \
2823 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2825 /* Use assembler line directives if available. */
2826 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2827 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2828 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2829 #else
2830 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2831 #endif
2832 #endif
2834 /* Minimum line offset in a special line info. opcode.
2835 This value was chosen to give a reasonable range of values. */
2836 #define DWARF_LINE_BASE -10
2838 /* First special line opcode - leave room for the standard opcodes. */
2839 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2841 /* Range of line offsets in a special line info. opcode. */
2842 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2844 /* Flag that indicates the initial value of the is_stmt_start flag.
2845 In the present implementation, we do not mark any lines as
2846 the beginning of a source statement, because that information
2847 is not made available by the GCC front-end. */
2848 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2850 /* Maximum number of operations per instruction bundle. */
2851 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2852 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2853 #endif
2855 /* This location is used by calc_die_sizes() to keep track
2856 the offset of each DIE within the .debug_info section. */
2857 static unsigned long next_die_offset;
2859 /* Record the root of the DIE's built for the current compilation unit. */
2860 static GTY(()) dw_die_ref single_comp_unit_die;
2862 /* A list of type DIEs that have been separated into comdat sections. */
2863 static GTY(()) comdat_type_node *comdat_type_list;
2865 /* A list of DIEs with a NULL parent waiting to be relocated. */
2866 static GTY(()) limbo_die_node *limbo_die_list;
2868 /* A list of DIEs for which we may have to generate
2869 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2870 static GTY(()) limbo_die_node *deferred_asm_name;
2872 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2874 typedef const char *compare_type;
2876 static hashval_t hash (dwarf_file_data *);
2877 static bool equal (dwarf_file_data *, const char *);
2880 /* Filenames referenced by this compilation unit. */
2881 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2883 struct decl_die_hasher : ggc_ptr_hash<die_node>
2885 typedef tree compare_type;
2887 static hashval_t hash (die_node *);
2888 static bool equal (die_node *, tree);
2890 /* A hash table of references to DIE's that describe declarations.
2891 The key is a DECL_UID() which is a unique number identifying each decl. */
2892 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2894 struct block_die_hasher : ggc_ptr_hash<die_struct>
2896 static hashval_t hash (die_struct *);
2897 static bool equal (die_struct *, die_struct *);
2900 /* A hash table of references to DIE's that describe COMMON blocks.
2901 The key is DECL_UID() ^ die_parent. */
2902 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2904 typedef struct GTY(()) die_arg_entry_struct {
2905 dw_die_ref die;
2906 tree arg;
2907 } die_arg_entry;
2910 /* Node of the variable location list. */
2911 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2912 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2913 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2914 in mode of the EXPR_LIST node and first EXPR_LIST operand
2915 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2916 location or NULL for padding. For larger bitsizes,
2917 mode is 0 and first operand is a CONCAT with bitsize
2918 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2919 NULL as second operand. */
2920 rtx GTY (()) loc;
2921 const char * GTY (()) label;
2922 struct var_loc_node * GTY (()) next;
2925 /* Variable location list. */
2926 struct GTY ((for_user)) var_loc_list_def {
2927 struct var_loc_node * GTY (()) first;
2929 /* Pointer to the last but one or last element of the
2930 chained list. If the list is empty, both first and
2931 last are NULL, if the list contains just one node
2932 or the last node certainly is not redundant, it points
2933 to the last node, otherwise points to the last but one.
2934 Do not mark it for GC because it is marked through the chain. */
2935 struct var_loc_node * GTY ((skip ("%h"))) last;
2937 /* Pointer to the last element before section switch,
2938 if NULL, either sections weren't switched or first
2939 is after section switch. */
2940 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2942 /* DECL_UID of the variable decl. */
2943 unsigned int decl_id;
2945 typedef struct var_loc_list_def var_loc_list;
2947 /* Call argument location list. */
2948 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2949 rtx GTY (()) call_arg_loc_note;
2950 const char * GTY (()) label;
2951 tree GTY (()) block;
2952 bool tail_call_p;
2953 rtx GTY (()) symbol_ref;
2954 struct call_arg_loc_node * GTY (()) next;
2958 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2960 typedef const_tree compare_type;
2962 static hashval_t hash (var_loc_list *);
2963 static bool equal (var_loc_list *, const_tree);
2966 /* Table of decl location linked lists. */
2967 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2969 /* Head and tail of call_arg_loc chain. */
2970 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2971 static struct call_arg_loc_node *call_arg_loc_last;
2973 /* Number of call sites in the current function. */
2974 static int call_site_count = -1;
2975 /* Number of tail call sites in the current function. */
2976 static int tail_call_site_count = -1;
2978 /* A cached location list. */
2979 struct GTY ((for_user)) cached_dw_loc_list_def {
2980 /* The DECL_UID of the decl that this entry describes. */
2981 unsigned int decl_id;
2983 /* The cached location list. */
2984 dw_loc_list_ref loc_list;
2986 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2988 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2991 typedef const_tree compare_type;
2993 static hashval_t hash (cached_dw_loc_list *);
2994 static bool equal (cached_dw_loc_list *, const_tree);
2997 /* Table of cached location lists. */
2998 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3000 /* A pointer to the base of a list of references to DIE's that
3001 are uniquely identified by their tag, presence/absence of
3002 children DIE's, and list of attribute/value pairs. */
3003 static GTY((length ("abbrev_die_table_allocated")))
3004 dw_die_ref *abbrev_die_table;
3006 /* Number of elements currently allocated for abbrev_die_table. */
3007 static GTY(()) unsigned abbrev_die_table_allocated;
3009 /* Number of elements in abbrev_die_table currently in use. */
3010 static GTY(()) unsigned abbrev_die_table_in_use;
3012 /* A hash map to remember the stack usage for DWARF procedures. The value
3013 stored is the stack size difference between before the DWARF procedure
3014 invokation and after it returned. In other words, for a DWARF procedure
3015 that consumes N stack slots and that pushes M ones, this stores M - N. */
3016 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3018 /* Size (in elements) of increments by which we may expand the
3019 abbrev_die_table. */
3020 #define ABBREV_DIE_TABLE_INCREMENT 256
3022 /* A global counter for generating labels for line number data. */
3023 static unsigned int line_info_label_num;
3025 /* The current table to which we should emit line number information
3026 for the current function. This will be set up at the beginning of
3027 assembly for the function. */
3028 static GTY(()) dw_line_info_table *cur_line_info_table;
3030 /* The two default tables of line number info. */
3031 static GTY(()) dw_line_info_table *text_section_line_info;
3032 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3034 /* The set of all non-default tables of line number info. */
3035 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3037 /* A flag to tell pubnames/types export if there is an info section to
3038 refer to. */
3039 static bool info_section_emitted;
3041 /* A pointer to the base of a table that contains a list of publicly
3042 accessible names. */
3043 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3045 /* A pointer to the base of a table that contains a list of publicly
3046 accessible types. */
3047 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3049 /* A pointer to the base of a table that contains a list of macro
3050 defines/undefines (and file start/end markers). */
3051 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3053 /* True if .debug_macinfo or .debug_macros section is going to be
3054 emitted. */
3055 #define have_macinfo \
3056 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3057 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3058 && !macinfo_table->is_empty ())
3060 /* Array of dies for which we should generate .debug_ranges info. */
3061 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3063 /* Number of elements currently allocated for ranges_table. */
3064 static GTY(()) unsigned ranges_table_allocated;
3066 /* Number of elements in ranges_table currently in use. */
3067 static GTY(()) unsigned ranges_table_in_use;
3069 /* Array of pairs of labels referenced in ranges_table. */
3070 static GTY ((length ("ranges_by_label_allocated")))
3071 dw_ranges_by_label *ranges_by_label;
3073 /* Number of elements currently allocated for ranges_by_label. */
3074 static GTY(()) unsigned ranges_by_label_allocated;
3076 /* Number of elements in ranges_by_label currently in use. */
3077 static GTY(()) unsigned ranges_by_label_in_use;
3079 /* Size (in elements) of increments by which we may expand the
3080 ranges_table. */
3081 #define RANGES_TABLE_INCREMENT 64
3083 /* Whether we have location lists that need outputting */
3084 static GTY(()) bool have_location_lists;
3086 /* Unique label counter. */
3087 static GTY(()) unsigned int loclabel_num;
3089 /* Unique label counter for point-of-call tables. */
3090 static GTY(()) unsigned int poc_label_num;
3092 /* The last file entry emitted by maybe_emit_file(). */
3093 static GTY(()) struct dwarf_file_data * last_emitted_file;
3095 /* Number of internal labels generated by gen_internal_sym(). */
3096 static GTY(()) int label_num;
3098 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3100 /* Instances of generic types for which we need to generate debug
3101 info that describe their generic parameters and arguments. That
3102 generation needs to happen once all types are properly laid out so
3103 we do it at the end of compilation. */
3104 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3106 /* Offset from the "steady-state frame pointer" to the frame base,
3107 within the current function. */
3108 static HOST_WIDE_INT frame_pointer_fb_offset;
3109 static bool frame_pointer_fb_offset_valid;
3111 static vec<dw_die_ref> base_types;
3113 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3114 once all arguments are parsed. */
3115 static vec<dw_die_ref> *string_types;
3117 /* Flags to represent a set of attribute classes for attributes that represent
3118 a scalar value (bounds, pointers, ...). */
3119 enum dw_scalar_form
3121 dw_scalar_form_constant = 0x01,
3122 dw_scalar_form_exprloc = 0x02,
3123 dw_scalar_form_reference = 0x04
3126 /* Forward declarations for functions defined in this file. */
3128 static int is_pseudo_reg (const_rtx);
3129 static tree type_main_variant (tree);
3130 static int is_tagged_type (const_tree);
3131 static const char *dwarf_tag_name (unsigned);
3132 static const char *dwarf_attr_name (unsigned);
3133 static const char *dwarf_form_name (unsigned);
3134 static tree decl_ultimate_origin (const_tree);
3135 static tree decl_class_context (tree);
3136 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3137 static inline enum dw_val_class AT_class (dw_attr_node *);
3138 static inline unsigned int AT_index (dw_attr_node *);
3139 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3140 static inline unsigned AT_flag (dw_attr_node *);
3141 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3142 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3143 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3144 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3145 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3146 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3147 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3148 unsigned int, unsigned char *);
3149 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3150 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3151 static inline const char *AT_string (dw_attr_node *);
3152 static enum dwarf_form AT_string_form (dw_attr_node *);
3153 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3154 static void add_AT_specification (dw_die_ref, dw_die_ref);
3155 static inline dw_die_ref AT_ref (dw_attr_node *);
3156 static inline int AT_ref_external (dw_attr_node *);
3157 static inline void set_AT_ref_external (dw_attr_node *, int);
3158 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3159 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3160 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3161 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3162 dw_loc_list_ref);
3163 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3164 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3165 static void remove_addr_table_entry (addr_table_entry *);
3166 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3167 static inline rtx AT_addr (dw_attr_node *);
3168 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3169 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3170 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3171 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3172 unsigned HOST_WIDE_INT);
3173 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3174 unsigned long, bool);
3175 static inline const char *AT_lbl (dw_attr_node *);
3176 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3177 static const char *get_AT_low_pc (dw_die_ref);
3178 static const char *get_AT_hi_pc (dw_die_ref);
3179 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3180 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3181 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3182 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3183 static bool is_cxx (void);
3184 static bool is_fortran (void);
3185 static bool is_ada (void);
3186 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3187 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3188 static void add_child_die (dw_die_ref, dw_die_ref);
3189 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3190 static dw_die_ref lookup_type_die (tree);
3191 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3192 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3193 static void equate_type_number_to_die (tree, dw_die_ref);
3194 static dw_die_ref lookup_decl_die (tree);
3195 static var_loc_list *lookup_decl_loc (const_tree);
3196 static void equate_decl_number_to_die (tree, dw_die_ref);
3197 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3198 static void print_spaces (FILE *);
3199 static void print_die (dw_die_ref, FILE *);
3200 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3201 static dw_die_ref pop_compile_unit (dw_die_ref);
3202 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3203 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3204 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3205 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3206 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3207 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3208 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3209 struct md5_ctx *, int *);
3210 struct checksum_attributes;
3211 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3212 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3213 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3214 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3215 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3216 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3217 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3218 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3219 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3220 static void compute_section_prefix (dw_die_ref);
3221 static int is_type_die (dw_die_ref);
3222 static int is_comdat_die (dw_die_ref);
3223 static int is_symbol_die (dw_die_ref);
3224 static inline bool is_template_instantiation (dw_die_ref);
3225 static void assign_symbol_names (dw_die_ref);
3226 static void break_out_includes (dw_die_ref);
3227 static int is_declaration_die (dw_die_ref);
3228 static int should_move_die_to_comdat (dw_die_ref);
3229 static dw_die_ref clone_as_declaration (dw_die_ref);
3230 static dw_die_ref clone_die (dw_die_ref);
3231 static dw_die_ref clone_tree (dw_die_ref);
3232 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3233 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3234 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3235 static dw_die_ref generate_skeleton (dw_die_ref);
3236 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3237 dw_die_ref,
3238 dw_die_ref);
3239 static void break_out_comdat_types (dw_die_ref);
3240 static void copy_decls_for_unworthy_types (dw_die_ref);
3242 static void add_sibling_attributes (dw_die_ref);
3243 static void output_location_lists (dw_die_ref);
3244 static int constant_size (unsigned HOST_WIDE_INT);
3245 static unsigned long size_of_die (dw_die_ref);
3246 static void calc_die_sizes (dw_die_ref);
3247 static void calc_base_type_die_sizes (void);
3248 static void mark_dies (dw_die_ref);
3249 static void unmark_dies (dw_die_ref);
3250 static void unmark_all_dies (dw_die_ref);
3251 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3252 static unsigned long size_of_aranges (void);
3253 static enum dwarf_form value_format (dw_attr_node *);
3254 static void output_value_format (dw_attr_node *);
3255 static void output_abbrev_section (void);
3256 static void output_die_abbrevs (unsigned long, dw_die_ref);
3257 static void output_die_symbol (dw_die_ref);
3258 static void output_die (dw_die_ref);
3259 static void output_compilation_unit_header (void);
3260 static void output_comp_unit (dw_die_ref, int);
3261 static void output_comdat_type_unit (comdat_type_node *);
3262 static const char *dwarf2_name (tree, int);
3263 static void add_pubname (tree, dw_die_ref);
3264 static void add_enumerator_pubname (const char *, dw_die_ref);
3265 static void add_pubname_string (const char *, dw_die_ref);
3266 static void add_pubtype (tree, dw_die_ref);
3267 static void output_pubnames (vec<pubname_entry, va_gc> *);
3268 static void output_aranges (void);
3269 static unsigned int add_ranges_num (int);
3270 static unsigned int add_ranges (const_tree);
3271 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3272 bool *, bool);
3273 static void output_ranges (void);
3274 static dw_line_info_table *new_line_info_table (void);
3275 static void output_line_info (bool);
3276 static void output_file_names (void);
3277 static dw_die_ref base_type_die (tree, bool);
3278 static int is_base_type (tree);
3279 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3280 static int decl_quals (const_tree);
3281 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3282 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3283 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3284 static int type_is_enum (const_tree);
3285 static unsigned int dbx_reg_number (const_rtx);
3286 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3287 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3288 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3289 enum var_init_status);
3290 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3291 enum var_init_status);
3292 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3293 enum var_init_status);
3294 static int is_based_loc (const_rtx);
3295 static bool resolve_one_addr (rtx *);
3296 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3297 enum var_init_status);
3298 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3299 enum var_init_status);
3300 struct loc_descr_context;
3301 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3302 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3303 static dw_loc_list_ref loc_list_from_tree (tree, int,
3304 const struct loc_descr_context *);
3305 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3306 const struct loc_descr_context *);
3307 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3308 static tree field_type (const_tree);
3309 static unsigned int simple_type_align_in_bits (const_tree);
3310 static unsigned int simple_decl_align_in_bits (const_tree);
3311 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3312 struct vlr_context;
3313 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3314 HOST_WIDE_INT *);
3315 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3316 dw_loc_list_ref);
3317 static void add_data_member_location_attribute (dw_die_ref, tree,
3318 struct vlr_context *);
3319 static bool add_const_value_attribute (dw_die_ref, rtx);
3320 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3321 static void insert_wide_int (const wide_int &, unsigned char *, int);
3322 static void insert_float (const_rtx, unsigned char *);
3323 static rtx rtl_for_decl_location (tree);
3324 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3325 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3326 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3327 static void add_name_attribute (dw_die_ref, const char *);
3328 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3329 static void add_comp_dir_attribute (dw_die_ref);
3330 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3331 const struct loc_descr_context *);
3332 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3333 const struct loc_descr_context *);
3334 static void add_subscript_info (dw_die_ref, tree, bool);
3335 static void add_byte_size_attribute (dw_die_ref, tree);
3336 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3337 struct vlr_context *);
3338 static void add_bit_size_attribute (dw_die_ref, tree);
3339 static void add_prototyped_attribute (dw_die_ref, tree);
3340 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3341 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3342 static void add_src_coords_attributes (dw_die_ref, tree);
3343 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3344 static void add_discr_value (dw_die_ref, dw_discr_value *);
3345 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3346 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3347 static void push_decl_scope (tree);
3348 static void pop_decl_scope (void);
3349 static dw_die_ref scope_die_for (tree, dw_die_ref);
3350 static inline int local_scope_p (dw_die_ref);
3351 static inline int class_scope_p (dw_die_ref);
3352 static inline int class_or_namespace_scope_p (dw_die_ref);
3353 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3354 static void add_calling_convention_attribute (dw_die_ref, tree);
3355 static const char *type_tag (const_tree);
3356 static tree member_declared_type (const_tree);
3357 #if 0
3358 static const char *decl_start_label (tree);
3359 #endif
3360 static void gen_array_type_die (tree, dw_die_ref);
3361 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3362 #if 0
3363 static void gen_entry_point_die (tree, dw_die_ref);
3364 #endif
3365 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3366 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3367 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3368 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3369 static void gen_formal_types_die (tree, dw_die_ref);
3370 static void gen_subprogram_die (tree, dw_die_ref);
3371 static void gen_variable_die (tree, tree, dw_die_ref);
3372 static void gen_const_die (tree, dw_die_ref);
3373 static void gen_label_die (tree, dw_die_ref);
3374 static void gen_lexical_block_die (tree, dw_die_ref);
3375 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3376 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3377 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3378 static dw_die_ref gen_compile_unit_die (const char *);
3379 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3380 static void gen_member_die (tree, dw_die_ref);
3381 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3382 enum debug_info_usage);
3383 static void gen_subroutine_type_die (tree, dw_die_ref);
3384 static void gen_typedef_die (tree, dw_die_ref);
3385 static void gen_type_die (tree, dw_die_ref);
3386 static void gen_block_die (tree, dw_die_ref);
3387 static void decls_for_scope (tree, dw_die_ref);
3388 static bool is_naming_typedef_decl (const_tree);
3389 static inline dw_die_ref get_context_die (tree);
3390 static void gen_namespace_die (tree, dw_die_ref);
3391 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3392 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3393 static dw_die_ref force_decl_die (tree);
3394 static dw_die_ref force_type_die (tree);
3395 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3396 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3397 static struct dwarf_file_data * lookup_filename (const char *);
3398 static void retry_incomplete_types (void);
3399 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3400 static void gen_generic_params_dies (tree);
3401 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3402 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3403 static void splice_child_die (dw_die_ref, dw_die_ref);
3404 static int file_info_cmp (const void *, const void *);
3405 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3406 const char *, const char *);
3407 static void output_loc_list (dw_loc_list_ref);
3408 static char *gen_internal_sym (const char *);
3409 static bool want_pubnames (void);
3411 static void prune_unmark_dies (dw_die_ref);
3412 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3413 static void prune_unused_types_mark (dw_die_ref, int);
3414 static void prune_unused_types_walk (dw_die_ref);
3415 static void prune_unused_types_walk_attribs (dw_die_ref);
3416 static void prune_unused_types_prune (dw_die_ref);
3417 static void prune_unused_types (void);
3418 static int maybe_emit_file (struct dwarf_file_data *fd);
3419 static inline const char *AT_vms_delta1 (dw_attr_node *);
3420 static inline const char *AT_vms_delta2 (dw_attr_node *);
3421 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3422 const char *, const char *);
3423 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3424 static void gen_remaining_tmpl_value_param_die_attribute (void);
3425 static bool generic_type_p (tree);
3426 static void schedule_generic_params_dies_gen (tree t);
3427 static void gen_scheduled_generic_parms_dies (void);
3429 static const char *comp_dir_string (void);
3431 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3433 /* enum for tracking thread-local variables whose address is really an offset
3434 relative to the TLS pointer, which will need link-time relocation, but will
3435 not need relocation by the DWARF consumer. */
3437 enum dtprel_bool
3439 dtprel_false = 0,
3440 dtprel_true = 1
3443 /* Return the operator to use for an address of a variable. For dtprel_true, we
3444 use DW_OP_const*. For regular variables, which need both link-time
3445 relocation and consumer-level relocation (e.g., to account for shared objects
3446 loaded at a random address), we use DW_OP_addr*. */
3448 static inline enum dwarf_location_atom
3449 dw_addr_op (enum dtprel_bool dtprel)
3451 if (dtprel == dtprel_true)
3452 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3453 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3454 else
3455 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3458 /* Return a pointer to a newly allocated address location description. If
3459 dwarf_split_debug_info is true, then record the address with the appropriate
3460 relocation. */
3461 static inline dw_loc_descr_ref
3462 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3464 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3466 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3467 ref->dw_loc_oprnd1.v.val_addr = addr;
3468 ref->dtprel = dtprel;
3469 if (dwarf_split_debug_info)
3470 ref->dw_loc_oprnd1.val_entry
3471 = add_addr_table_entry (addr,
3472 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3473 else
3474 ref->dw_loc_oprnd1.val_entry = NULL;
3476 return ref;
3479 /* Section names used to hold DWARF debugging information. */
3481 #ifndef DEBUG_INFO_SECTION
3482 #define DEBUG_INFO_SECTION ".debug_info"
3483 #endif
3484 #ifndef DEBUG_DWO_INFO_SECTION
3485 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3486 #endif
3487 #ifndef DEBUG_ABBREV_SECTION
3488 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3489 #endif
3490 #ifndef DEBUG_DWO_ABBREV_SECTION
3491 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3492 #endif
3493 #ifndef DEBUG_ARANGES_SECTION
3494 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3495 #endif
3496 #ifndef DEBUG_ADDR_SECTION
3497 #define DEBUG_ADDR_SECTION ".debug_addr"
3498 #endif
3499 #ifndef DEBUG_MACINFO_SECTION
3500 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3501 #endif
3502 #ifndef DEBUG_DWO_MACINFO_SECTION
3503 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3504 #endif
3505 #ifndef DEBUG_DWO_MACRO_SECTION
3506 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3507 #endif
3508 #ifndef DEBUG_MACRO_SECTION
3509 #define DEBUG_MACRO_SECTION ".debug_macro"
3510 #endif
3511 #ifndef DEBUG_LINE_SECTION
3512 #define DEBUG_LINE_SECTION ".debug_line"
3513 #endif
3514 #ifndef DEBUG_DWO_LINE_SECTION
3515 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3516 #endif
3517 #ifndef DEBUG_LOC_SECTION
3518 #define DEBUG_LOC_SECTION ".debug_loc"
3519 #endif
3520 #ifndef DEBUG_DWO_LOC_SECTION
3521 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3522 #endif
3523 #ifndef DEBUG_PUBNAMES_SECTION
3524 #define DEBUG_PUBNAMES_SECTION \
3525 ((debug_generate_pub_sections == 2) \
3526 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3527 #endif
3528 #ifndef DEBUG_PUBTYPES_SECTION
3529 #define DEBUG_PUBTYPES_SECTION \
3530 ((debug_generate_pub_sections == 2) \
3531 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3532 #endif
3533 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3534 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3535 #ifndef DEBUG_STR_OFFSETS_SECTION
3536 #define DEBUG_STR_OFFSETS_SECTION \
3537 (!dwarf_split_debug_info \
3538 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3539 #endif
3540 #ifndef DEBUG_STR_DWO_SECTION
3541 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3542 #endif
3543 #ifndef DEBUG_STR_SECTION
3544 #define DEBUG_STR_SECTION ".debug_str"
3545 #endif
3546 #ifndef DEBUG_RANGES_SECTION
3547 #define DEBUG_RANGES_SECTION ".debug_ranges"
3548 #endif
3550 /* Standard ELF section names for compiled code and data. */
3551 #ifndef TEXT_SECTION_NAME
3552 #define TEXT_SECTION_NAME ".text"
3553 #endif
3555 /* Section flags for .debug_str section. */
3556 #define DEBUG_STR_SECTION_FLAGS \
3557 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3558 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3559 : SECTION_DEBUG)
3561 /* Section flags for .debug_str.dwo section. */
3562 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3564 /* Labels we insert at beginning sections we can reference instead of
3565 the section names themselves. */
3567 #ifndef TEXT_SECTION_LABEL
3568 #define TEXT_SECTION_LABEL "Ltext"
3569 #endif
3570 #ifndef COLD_TEXT_SECTION_LABEL
3571 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3572 #endif
3573 #ifndef DEBUG_LINE_SECTION_LABEL
3574 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3575 #endif
3576 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3577 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3578 #endif
3579 #ifndef DEBUG_INFO_SECTION_LABEL
3580 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3581 #endif
3582 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3583 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3584 #endif
3585 #ifndef DEBUG_ABBREV_SECTION_LABEL
3586 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3587 #endif
3588 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3589 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3590 #endif
3591 #ifndef DEBUG_ADDR_SECTION_LABEL
3592 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3593 #endif
3594 #ifndef DEBUG_LOC_SECTION_LABEL
3595 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3596 #endif
3597 #ifndef DEBUG_RANGES_SECTION_LABEL
3598 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3599 #endif
3600 #ifndef DEBUG_MACINFO_SECTION_LABEL
3601 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3602 #endif
3603 #ifndef DEBUG_MACRO_SECTION_LABEL
3604 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3605 #endif
3606 #define SKELETON_COMP_DIE_ABBREV 1
3607 #define SKELETON_TYPE_DIE_ABBREV 2
3609 /* Definitions of defaults for formats and names of various special
3610 (artificial) labels which may be generated within this file (when the -g
3611 options is used and DWARF2_DEBUGGING_INFO is in effect.
3612 If necessary, these may be overridden from within the tm.h file, but
3613 typically, overriding these defaults is unnecessary. */
3615 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3616 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3617 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3618 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3619 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3620 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3621 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3622 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3623 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3624 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3625 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3626 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3627 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3628 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3630 #ifndef TEXT_END_LABEL
3631 #define TEXT_END_LABEL "Letext"
3632 #endif
3633 #ifndef COLD_END_LABEL
3634 #define COLD_END_LABEL "Letext_cold"
3635 #endif
3636 #ifndef BLOCK_BEGIN_LABEL
3637 #define BLOCK_BEGIN_LABEL "LBB"
3638 #endif
3639 #ifndef BLOCK_END_LABEL
3640 #define BLOCK_END_LABEL "LBE"
3641 #endif
3642 #ifndef LINE_CODE_LABEL
3643 #define LINE_CODE_LABEL "LM"
3644 #endif
3647 /* Return the root of the DIE's built for the current compilation unit. */
3648 static dw_die_ref
3649 comp_unit_die (void)
3651 if (!single_comp_unit_die)
3652 single_comp_unit_die = gen_compile_unit_die (NULL);
3653 return single_comp_unit_die;
3656 /* We allow a language front-end to designate a function that is to be
3657 called to "demangle" any name before it is put into a DIE. */
3659 static const char *(*demangle_name_func) (const char *);
3661 void
3662 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3664 demangle_name_func = func;
3667 /* Test if rtl node points to a pseudo register. */
3669 static inline int
3670 is_pseudo_reg (const_rtx rtl)
3672 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3673 || (GET_CODE (rtl) == SUBREG
3674 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3677 /* Return a reference to a type, with its const and volatile qualifiers
3678 removed. */
3680 static inline tree
3681 type_main_variant (tree type)
3683 type = TYPE_MAIN_VARIANT (type);
3685 /* ??? There really should be only one main variant among any group of
3686 variants of a given type (and all of the MAIN_VARIANT values for all
3687 members of the group should point to that one type) but sometimes the C
3688 front-end messes this up for array types, so we work around that bug
3689 here. */
3690 if (TREE_CODE (type) == ARRAY_TYPE)
3691 while (type != TYPE_MAIN_VARIANT (type))
3692 type = TYPE_MAIN_VARIANT (type);
3694 return type;
3697 /* Return nonzero if the given type node represents a tagged type. */
3699 static inline int
3700 is_tagged_type (const_tree type)
3702 enum tree_code code = TREE_CODE (type);
3704 return (code == RECORD_TYPE || code == UNION_TYPE
3705 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3708 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3710 static void
3711 get_ref_die_offset_label (char *label, dw_die_ref ref)
3713 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3716 /* Return die_offset of a DIE reference to a base type. */
3718 static unsigned long int
3719 get_base_type_offset (dw_die_ref ref)
3721 if (ref->die_offset)
3722 return ref->die_offset;
3723 if (comp_unit_die ()->die_abbrev)
3725 calc_base_type_die_sizes ();
3726 gcc_assert (ref->die_offset);
3728 return ref->die_offset;
3731 /* Return die_offset of a DIE reference other than base type. */
3733 static unsigned long int
3734 get_ref_die_offset (dw_die_ref ref)
3736 gcc_assert (ref->die_offset);
3737 return ref->die_offset;
3740 /* Convert a DIE tag into its string name. */
3742 static const char *
3743 dwarf_tag_name (unsigned int tag)
3745 const char *name = get_DW_TAG_name (tag);
3747 if (name != NULL)
3748 return name;
3750 return "DW_TAG_<unknown>";
3753 /* Convert a DWARF attribute code into its string name. */
3755 static const char *
3756 dwarf_attr_name (unsigned int attr)
3758 const char *name;
3760 switch (attr)
3762 #if VMS_DEBUGGING_INFO
3763 case DW_AT_HP_prologue:
3764 return "DW_AT_HP_prologue";
3765 #else
3766 case DW_AT_MIPS_loop_unroll_factor:
3767 return "DW_AT_MIPS_loop_unroll_factor";
3768 #endif
3770 #if VMS_DEBUGGING_INFO
3771 case DW_AT_HP_epilogue:
3772 return "DW_AT_HP_epilogue";
3773 #else
3774 case DW_AT_MIPS_stride:
3775 return "DW_AT_MIPS_stride";
3776 #endif
3779 name = get_DW_AT_name (attr);
3781 if (name != NULL)
3782 return name;
3784 return "DW_AT_<unknown>";
3787 /* Convert a DWARF value form code into its string name. */
3789 static const char *
3790 dwarf_form_name (unsigned int form)
3792 const char *name = get_DW_FORM_name (form);
3794 if (name != NULL)
3795 return name;
3797 return "DW_FORM_<unknown>";
3800 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3801 instance of an inlined instance of a decl which is local to an inline
3802 function, so we have to trace all of the way back through the origin chain
3803 to find out what sort of node actually served as the original seed for the
3804 given block. */
3806 static tree
3807 decl_ultimate_origin (const_tree decl)
3809 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3810 return NULL_TREE;
3812 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3813 we're trying to output the abstract instance of this function. */
3814 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3815 return NULL_TREE;
3817 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3818 most distant ancestor, this should never happen. */
3819 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3821 return DECL_ABSTRACT_ORIGIN (decl);
3824 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3825 of a virtual function may refer to a base class, so we check the 'this'
3826 parameter. */
3828 static tree
3829 decl_class_context (tree decl)
3831 tree context = NULL_TREE;
3833 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3834 context = DECL_CONTEXT (decl);
3835 else
3836 context = TYPE_MAIN_VARIANT
3837 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3839 if (context && !TYPE_P (context))
3840 context = NULL_TREE;
3842 return context;
3845 /* Add an attribute/value pair to a DIE. */
3847 static inline void
3848 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3850 /* Maybe this should be an assert? */
3851 if (die == NULL)
3852 return;
3854 vec_safe_reserve (die->die_attr, 1);
3855 vec_safe_push (die->die_attr, *attr);
3858 static inline enum dw_val_class
3859 AT_class (dw_attr_node *a)
3861 return a->dw_attr_val.val_class;
3864 /* Return the index for any attribute that will be referenced with a
3865 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3866 are stored in dw_attr_val.v.val_str for reference counting
3867 pruning. */
3869 static inline unsigned int
3870 AT_index (dw_attr_node *a)
3872 if (AT_class (a) == dw_val_class_str)
3873 return a->dw_attr_val.v.val_str->index;
3874 else if (a->dw_attr_val.val_entry != NULL)
3875 return a->dw_attr_val.val_entry->index;
3876 return NOT_INDEXED;
3879 /* Add a flag value attribute to a DIE. */
3881 static inline void
3882 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3884 dw_attr_node attr;
3886 attr.dw_attr = attr_kind;
3887 attr.dw_attr_val.val_class = dw_val_class_flag;
3888 attr.dw_attr_val.val_entry = NULL;
3889 attr.dw_attr_val.v.val_flag = flag;
3890 add_dwarf_attr (die, &attr);
3893 static inline unsigned
3894 AT_flag (dw_attr_node *a)
3896 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3897 return a->dw_attr_val.v.val_flag;
3900 /* Add a signed integer attribute value to a DIE. */
3902 static inline void
3903 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3905 dw_attr_node attr;
3907 attr.dw_attr = attr_kind;
3908 attr.dw_attr_val.val_class = dw_val_class_const;
3909 attr.dw_attr_val.val_entry = NULL;
3910 attr.dw_attr_val.v.val_int = int_val;
3911 add_dwarf_attr (die, &attr);
3914 static inline HOST_WIDE_INT
3915 AT_int (dw_attr_node *a)
3917 gcc_assert (a && AT_class (a) == dw_val_class_const);
3918 return a->dw_attr_val.v.val_int;
3921 /* Add an unsigned integer attribute value to a DIE. */
3923 static inline void
3924 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3925 unsigned HOST_WIDE_INT unsigned_val)
3927 dw_attr_node attr;
3929 attr.dw_attr = attr_kind;
3930 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3931 attr.dw_attr_val.val_entry = NULL;
3932 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3933 add_dwarf_attr (die, &attr);
3936 static inline unsigned HOST_WIDE_INT
3937 AT_unsigned (dw_attr_node *a)
3939 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3940 return a->dw_attr_val.v.val_unsigned;
3943 /* Add an unsigned wide integer attribute value to a DIE. */
3945 static inline void
3946 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3947 const wide_int& w)
3949 dw_attr_node attr;
3951 attr.dw_attr = attr_kind;
3952 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3953 attr.dw_attr_val.val_entry = NULL;
3954 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3955 *attr.dw_attr_val.v.val_wide = w;
3956 add_dwarf_attr (die, &attr);
3959 /* Add an unsigned double integer attribute value to a DIE. */
3961 static inline void
3962 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3963 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3965 dw_attr_node attr;
3967 attr.dw_attr = attr_kind;
3968 attr.dw_attr_val.val_class = dw_val_class_const_double;
3969 attr.dw_attr_val.val_entry = NULL;
3970 attr.dw_attr_val.v.val_double.high = high;
3971 attr.dw_attr_val.v.val_double.low = low;
3972 add_dwarf_attr (die, &attr);
3975 /* Add a floating point attribute value to a DIE and return it. */
3977 static inline void
3978 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3979 unsigned int length, unsigned int elt_size, unsigned char *array)
3981 dw_attr_node attr;
3983 attr.dw_attr = attr_kind;
3984 attr.dw_attr_val.val_class = dw_val_class_vec;
3985 attr.dw_attr_val.val_entry = NULL;
3986 attr.dw_attr_val.v.val_vec.length = length;
3987 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3988 attr.dw_attr_val.v.val_vec.array = array;
3989 add_dwarf_attr (die, &attr);
3992 /* Add an 8-byte data attribute value to a DIE. */
3994 static inline void
3995 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3996 unsigned char data8[8])
3998 dw_attr_node attr;
4000 attr.dw_attr = attr_kind;
4001 attr.dw_attr_val.val_class = dw_val_class_data8;
4002 attr.dw_attr_val.val_entry = NULL;
4003 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4004 add_dwarf_attr (die, &attr);
4007 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4008 dwarf_split_debug_info, address attributes in dies destined for the
4009 final executable have force_direct set to avoid using indexed
4010 references. */
4012 static inline void
4013 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4014 bool force_direct)
4016 dw_attr_node attr;
4017 char * lbl_id;
4019 lbl_id = xstrdup (lbl_low);
4020 attr.dw_attr = DW_AT_low_pc;
4021 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4022 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4023 if (dwarf_split_debug_info && !force_direct)
4024 attr.dw_attr_val.val_entry
4025 = add_addr_table_entry (lbl_id, ate_kind_label);
4026 else
4027 attr.dw_attr_val.val_entry = NULL;
4028 add_dwarf_attr (die, &attr);
4030 attr.dw_attr = DW_AT_high_pc;
4031 if (dwarf_version < 4)
4032 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4033 else
4034 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4035 lbl_id = xstrdup (lbl_high);
4036 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4037 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4038 && dwarf_split_debug_info && !force_direct)
4039 attr.dw_attr_val.val_entry
4040 = add_addr_table_entry (lbl_id, ate_kind_label);
4041 else
4042 attr.dw_attr_val.val_entry = NULL;
4043 add_dwarf_attr (die, &attr);
4046 /* Hash and equality functions for debug_str_hash. */
4048 hashval_t
4049 indirect_string_hasher::hash (indirect_string_node *x)
4051 return htab_hash_string (x->str);
4054 bool
4055 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4057 return strcmp (x1->str, x2) == 0;
4060 /* Add STR to the given string hash table. */
4062 static struct indirect_string_node *
4063 find_AT_string_in_table (const char *str,
4064 hash_table<indirect_string_hasher> *table)
4066 struct indirect_string_node *node;
4068 indirect_string_node **slot
4069 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4070 if (*slot == NULL)
4072 node = ggc_cleared_alloc<indirect_string_node> ();
4073 node->str = ggc_strdup (str);
4074 *slot = node;
4076 else
4077 node = *slot;
4079 node->refcount++;
4080 return node;
4083 /* Add STR to the indirect string hash table. */
4085 static struct indirect_string_node *
4086 find_AT_string (const char *str)
4088 if (! debug_str_hash)
4089 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4091 return find_AT_string_in_table (str, debug_str_hash);
4094 /* Add a string attribute value to a DIE. */
4096 static inline void
4097 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4099 dw_attr_node attr;
4100 struct indirect_string_node *node;
4102 node = find_AT_string (str);
4104 attr.dw_attr = attr_kind;
4105 attr.dw_attr_val.val_class = dw_val_class_str;
4106 attr.dw_attr_val.val_entry = NULL;
4107 attr.dw_attr_val.v.val_str = node;
4108 add_dwarf_attr (die, &attr);
4111 static inline const char *
4112 AT_string (dw_attr_node *a)
4114 gcc_assert (a && AT_class (a) == dw_val_class_str);
4115 return a->dw_attr_val.v.val_str->str;
4118 /* Call this function directly to bypass AT_string_form's logic to put
4119 the string inline in the die. */
4121 static void
4122 set_indirect_string (struct indirect_string_node *node)
4124 char label[32];
4125 /* Already indirect is a no op. */
4126 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4128 gcc_assert (node->label);
4129 return;
4131 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4132 ++dw2_string_counter;
4133 node->label = xstrdup (label);
4135 if (!dwarf_split_debug_info)
4137 node->form = DW_FORM_strp;
4138 node->index = NOT_INDEXED;
4140 else
4142 node->form = DW_FORM_GNU_str_index;
4143 node->index = NO_INDEX_ASSIGNED;
4147 /* Find out whether a string should be output inline in DIE
4148 or out-of-line in .debug_str section. */
4150 static enum dwarf_form
4151 find_string_form (struct indirect_string_node *node)
4153 unsigned int len;
4155 if (node->form)
4156 return node->form;
4158 len = strlen (node->str) + 1;
4160 /* If the string is shorter or equal to the size of the reference, it is
4161 always better to put it inline. */
4162 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4163 return node->form = DW_FORM_string;
4165 /* If we cannot expect the linker to merge strings in .debug_str
4166 section, only put it into .debug_str if it is worth even in this
4167 single module. */
4168 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4169 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4170 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4171 return node->form = DW_FORM_string;
4173 set_indirect_string (node);
4175 return node->form;
4178 /* Find out whether the string referenced from the attribute should be
4179 output inline in DIE or out-of-line in .debug_str section. */
4181 static enum dwarf_form
4182 AT_string_form (dw_attr_node *a)
4184 gcc_assert (a && AT_class (a) == dw_val_class_str);
4185 return find_string_form (a->dw_attr_val.v.val_str);
4188 /* Add a DIE reference attribute value to a DIE. */
4190 static inline void
4191 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4193 dw_attr_node attr;
4194 gcc_checking_assert (targ_die != NULL);
4196 /* With LTO we can end up trying to reference something we didn't create
4197 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4198 if (targ_die == NULL)
4199 return;
4201 attr.dw_attr = attr_kind;
4202 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4203 attr.dw_attr_val.val_entry = NULL;
4204 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4205 attr.dw_attr_val.v.val_die_ref.external = 0;
4206 add_dwarf_attr (die, &attr);
4209 /* Change DIE reference REF to point to NEW_DIE instead. */
4211 static inline void
4212 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4214 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4215 ref->dw_attr_val.v.val_die_ref.die = new_die;
4216 ref->dw_attr_val.v.val_die_ref.external = 0;
4219 /* Add an AT_specification attribute to a DIE, and also make the back
4220 pointer from the specification to the definition. */
4222 static inline void
4223 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4225 add_AT_die_ref (die, DW_AT_specification, targ_die);
4226 gcc_assert (!targ_die->die_definition);
4227 targ_die->die_definition = die;
4230 static inline dw_die_ref
4231 AT_ref (dw_attr_node *a)
4233 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4234 return a->dw_attr_val.v.val_die_ref.die;
4237 static inline int
4238 AT_ref_external (dw_attr_node *a)
4240 if (a && AT_class (a) == dw_val_class_die_ref)
4241 return a->dw_attr_val.v.val_die_ref.external;
4243 return 0;
4246 static inline void
4247 set_AT_ref_external (dw_attr_node *a, int i)
4249 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4250 a->dw_attr_val.v.val_die_ref.external = i;
4253 /* Add an FDE reference attribute value to a DIE. */
4255 static inline void
4256 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4258 dw_attr_node attr;
4260 attr.dw_attr = attr_kind;
4261 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4262 attr.dw_attr_val.val_entry = NULL;
4263 attr.dw_attr_val.v.val_fde_index = targ_fde;
4264 add_dwarf_attr (die, &attr);
4267 /* Add a location description attribute value to a DIE. */
4269 static inline void
4270 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4272 dw_attr_node attr;
4274 attr.dw_attr = attr_kind;
4275 attr.dw_attr_val.val_class = dw_val_class_loc;
4276 attr.dw_attr_val.val_entry = NULL;
4277 attr.dw_attr_val.v.val_loc = loc;
4278 add_dwarf_attr (die, &attr);
4281 static inline dw_loc_descr_ref
4282 AT_loc (dw_attr_node *a)
4284 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4285 return a->dw_attr_val.v.val_loc;
4288 static inline void
4289 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4291 dw_attr_node attr;
4293 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4294 return;
4296 attr.dw_attr = attr_kind;
4297 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4298 attr.dw_attr_val.val_entry = NULL;
4299 attr.dw_attr_val.v.val_loc_list = loc_list;
4300 add_dwarf_attr (die, &attr);
4301 have_location_lists = true;
4304 static inline dw_loc_list_ref
4305 AT_loc_list (dw_attr_node *a)
4307 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4308 return a->dw_attr_val.v.val_loc_list;
4311 static inline dw_loc_list_ref *
4312 AT_loc_list_ptr (dw_attr_node *a)
4314 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4315 return &a->dw_attr_val.v.val_loc_list;
4318 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4320 static hashval_t hash (addr_table_entry *);
4321 static bool equal (addr_table_entry *, addr_table_entry *);
4324 /* Table of entries into the .debug_addr section. */
4326 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4328 /* Hash an address_table_entry. */
4330 hashval_t
4331 addr_hasher::hash (addr_table_entry *a)
4333 inchash::hash hstate;
4334 switch (a->kind)
4336 case ate_kind_rtx:
4337 hstate.add_int (0);
4338 break;
4339 case ate_kind_rtx_dtprel:
4340 hstate.add_int (1);
4341 break;
4342 case ate_kind_label:
4343 return htab_hash_string (a->addr.label);
4344 default:
4345 gcc_unreachable ();
4347 inchash::add_rtx (a->addr.rtl, hstate);
4348 return hstate.end ();
4351 /* Determine equality for two address_table_entries. */
4353 bool
4354 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4356 if (a1->kind != a2->kind)
4357 return 0;
4358 switch (a1->kind)
4360 case ate_kind_rtx:
4361 case ate_kind_rtx_dtprel:
4362 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4363 case ate_kind_label:
4364 return strcmp (a1->addr.label, a2->addr.label) == 0;
4365 default:
4366 gcc_unreachable ();
4370 /* Initialize an addr_table_entry. */
4372 void
4373 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4375 e->kind = kind;
4376 switch (kind)
4378 case ate_kind_rtx:
4379 case ate_kind_rtx_dtprel:
4380 e->addr.rtl = (rtx) addr;
4381 break;
4382 case ate_kind_label:
4383 e->addr.label = (char *) addr;
4384 break;
4386 e->refcount = 0;
4387 e->index = NO_INDEX_ASSIGNED;
4390 /* Add attr to the address table entry to the table. Defer setting an
4391 index until output time. */
4393 static addr_table_entry *
4394 add_addr_table_entry (void *addr, enum ate_kind kind)
4396 addr_table_entry *node;
4397 addr_table_entry finder;
4399 gcc_assert (dwarf_split_debug_info);
4400 if (! addr_index_table)
4401 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4402 init_addr_table_entry (&finder, kind, addr);
4403 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4405 if (*slot == HTAB_EMPTY_ENTRY)
4407 node = ggc_cleared_alloc<addr_table_entry> ();
4408 init_addr_table_entry (node, kind, addr);
4409 *slot = node;
4411 else
4412 node = *slot;
4414 node->refcount++;
4415 return node;
4418 /* Remove an entry from the addr table by decrementing its refcount.
4419 Strictly, decrementing the refcount would be enough, but the
4420 assertion that the entry is actually in the table has found
4421 bugs. */
4423 static void
4424 remove_addr_table_entry (addr_table_entry *entry)
4426 gcc_assert (dwarf_split_debug_info && addr_index_table);
4427 /* After an index is assigned, the table is frozen. */
4428 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4429 entry->refcount--;
4432 /* Given a location list, remove all addresses it refers to from the
4433 address_table. */
4435 static void
4436 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4438 for (; descr; descr = descr->dw_loc_next)
4439 if (descr->dw_loc_oprnd1.val_entry != NULL)
4441 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4442 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4446 /* A helper function for dwarf2out_finish called through
4447 htab_traverse. Assign an addr_table_entry its index. All entries
4448 must be collected into the table when this function is called,
4449 because the indexing code relies on htab_traverse to traverse nodes
4450 in the same order for each run. */
4453 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4455 addr_table_entry *node = *h;
4457 /* Don't index unreferenced nodes. */
4458 if (node->refcount == 0)
4459 return 1;
4461 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4462 node->index = *index;
4463 *index += 1;
4465 return 1;
4468 /* Add an address constant attribute value to a DIE. When using
4469 dwarf_split_debug_info, address attributes in dies destined for the
4470 final executable should be direct references--setting the parameter
4471 force_direct ensures this behavior. */
4473 static inline void
4474 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4475 bool force_direct)
4477 dw_attr_node attr;
4479 attr.dw_attr = attr_kind;
4480 attr.dw_attr_val.val_class = dw_val_class_addr;
4481 attr.dw_attr_val.v.val_addr = addr;
4482 if (dwarf_split_debug_info && !force_direct)
4483 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4484 else
4485 attr.dw_attr_val.val_entry = NULL;
4486 add_dwarf_attr (die, &attr);
4489 /* Get the RTX from to an address DIE attribute. */
4491 static inline rtx
4492 AT_addr (dw_attr_node *a)
4494 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4495 return a->dw_attr_val.v.val_addr;
4498 /* Add a file attribute value to a DIE. */
4500 static inline void
4501 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4502 struct dwarf_file_data *fd)
4504 dw_attr_node attr;
4506 attr.dw_attr = attr_kind;
4507 attr.dw_attr_val.val_class = dw_val_class_file;
4508 attr.dw_attr_val.val_entry = NULL;
4509 attr.dw_attr_val.v.val_file = fd;
4510 add_dwarf_attr (die, &attr);
4513 /* Get the dwarf_file_data from a file DIE attribute. */
4515 static inline struct dwarf_file_data *
4516 AT_file (dw_attr_node *a)
4518 gcc_assert (a && AT_class (a) == dw_val_class_file);
4519 return a->dw_attr_val.v.val_file;
4522 /* Add a vms delta attribute value to a DIE. */
4524 static inline void
4525 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4526 const char *lbl1, const char *lbl2)
4528 dw_attr_node attr;
4530 attr.dw_attr = attr_kind;
4531 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4532 attr.dw_attr_val.val_entry = NULL;
4533 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4534 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4535 add_dwarf_attr (die, &attr);
4538 /* Add a label identifier attribute value to a DIE. */
4540 static inline void
4541 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4542 const char *lbl_id)
4544 dw_attr_node attr;
4546 attr.dw_attr = attr_kind;
4547 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4548 attr.dw_attr_val.val_entry = NULL;
4549 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4550 if (dwarf_split_debug_info)
4551 attr.dw_attr_val.val_entry
4552 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4553 ate_kind_label);
4554 add_dwarf_attr (die, &attr);
4557 /* Add a section offset attribute value to a DIE, an offset into the
4558 debug_line section. */
4560 static inline void
4561 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4562 const char *label)
4564 dw_attr_node attr;
4566 attr.dw_attr = attr_kind;
4567 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4568 attr.dw_attr_val.val_entry = NULL;
4569 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4570 add_dwarf_attr (die, &attr);
4573 /* Add a section offset attribute value to a DIE, an offset into the
4574 debug_macinfo section. */
4576 static inline void
4577 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4578 const char *label)
4580 dw_attr_node attr;
4582 attr.dw_attr = attr_kind;
4583 attr.dw_attr_val.val_class = dw_val_class_macptr;
4584 attr.dw_attr_val.val_entry = NULL;
4585 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4586 add_dwarf_attr (die, &attr);
4589 /* Add an offset attribute value to a DIE. */
4591 static inline void
4592 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4593 unsigned HOST_WIDE_INT offset)
4595 dw_attr_node attr;
4597 attr.dw_attr = attr_kind;
4598 attr.dw_attr_val.val_class = dw_val_class_offset;
4599 attr.dw_attr_val.val_entry = NULL;
4600 attr.dw_attr_val.v.val_offset = offset;
4601 add_dwarf_attr (die, &attr);
4604 /* Add a range_list attribute value to a DIE. When using
4605 dwarf_split_debug_info, address attributes in dies destined for the
4606 final executable should be direct references--setting the parameter
4607 force_direct ensures this behavior. */
4609 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4610 #define RELOCATED_OFFSET (NULL)
4612 static void
4613 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4614 long unsigned int offset, bool force_direct)
4616 dw_attr_node attr;
4618 attr.dw_attr = attr_kind;
4619 attr.dw_attr_val.val_class = dw_val_class_range_list;
4620 /* For the range_list attribute, use val_entry to store whether the
4621 offset should follow split-debug-info or normal semantics. This
4622 value is read in output_range_list_offset. */
4623 if (dwarf_split_debug_info && !force_direct)
4624 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4625 else
4626 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4627 attr.dw_attr_val.v.val_offset = offset;
4628 add_dwarf_attr (die, &attr);
4631 /* Return the start label of a delta attribute. */
4633 static inline const char *
4634 AT_vms_delta1 (dw_attr_node *a)
4636 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4637 return a->dw_attr_val.v.val_vms_delta.lbl1;
4640 /* Return the end label of a delta attribute. */
4642 static inline const char *
4643 AT_vms_delta2 (dw_attr_node *a)
4645 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4646 return a->dw_attr_val.v.val_vms_delta.lbl2;
4649 static inline const char *
4650 AT_lbl (dw_attr_node *a)
4652 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4653 || AT_class (a) == dw_val_class_lineptr
4654 || AT_class (a) == dw_val_class_macptr
4655 || AT_class (a) == dw_val_class_high_pc));
4656 return a->dw_attr_val.v.val_lbl_id;
4659 /* Get the attribute of type attr_kind. */
4661 static dw_attr_node *
4662 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4664 dw_attr_node *a;
4665 unsigned ix;
4666 dw_die_ref spec = NULL;
4668 if (! die)
4669 return NULL;
4671 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4672 if (a->dw_attr == attr_kind)
4673 return a;
4674 else if (a->dw_attr == DW_AT_specification
4675 || a->dw_attr == DW_AT_abstract_origin)
4676 spec = AT_ref (a);
4678 if (spec)
4679 return get_AT (spec, attr_kind);
4681 return NULL;
4684 /* Returns the parent of the declaration of DIE. */
4686 static dw_die_ref
4687 get_die_parent (dw_die_ref die)
4689 dw_die_ref t;
4691 if (!die)
4692 return NULL;
4694 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4695 || (t = get_AT_ref (die, DW_AT_specification)))
4696 die = t;
4698 return die->die_parent;
4701 /* Return the "low pc" attribute value, typically associated with a subprogram
4702 DIE. Return null if the "low pc" attribute is either not present, or if it
4703 cannot be represented as an assembler label identifier. */
4705 static inline const char *
4706 get_AT_low_pc (dw_die_ref die)
4708 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4710 return a ? AT_lbl (a) : NULL;
4713 /* Return the "high pc" attribute value, typically associated with a subprogram
4714 DIE. Return null if the "high pc" attribute is either not present, or if it
4715 cannot be represented as an assembler label identifier. */
4717 static inline const char *
4718 get_AT_hi_pc (dw_die_ref die)
4720 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4722 return a ? AT_lbl (a) : NULL;
4725 /* Return the value of the string attribute designated by ATTR_KIND, or
4726 NULL if it is not present. */
4728 static inline const char *
4729 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4731 dw_attr_node *a = get_AT (die, attr_kind);
4733 return a ? AT_string (a) : NULL;
4736 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4737 if it is not present. */
4739 static inline int
4740 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4742 dw_attr_node *a = get_AT (die, attr_kind);
4744 return a ? AT_flag (a) : 0;
4747 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4748 if it is not present. */
4750 static inline unsigned
4751 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4753 dw_attr_node *a = get_AT (die, attr_kind);
4755 return a ? AT_unsigned (a) : 0;
4758 static inline dw_die_ref
4759 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4761 dw_attr_node *a = get_AT (die, attr_kind);
4763 return a ? AT_ref (a) : NULL;
4766 static inline struct dwarf_file_data *
4767 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4769 dw_attr_node *a = get_AT (die, attr_kind);
4771 return a ? AT_file (a) : NULL;
4774 /* Return TRUE if the language is C++. */
4776 static inline bool
4777 is_cxx (void)
4779 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4781 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4782 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4785 /* Return TRUE if the language is Java. */
4787 static inline bool
4788 is_java (void)
4790 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4792 return lang == DW_LANG_Java;
4795 /* Return TRUE if the language is Fortran. */
4797 static inline bool
4798 is_fortran (void)
4800 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4802 return (lang == DW_LANG_Fortran77
4803 || lang == DW_LANG_Fortran90
4804 || lang == DW_LANG_Fortran95
4805 || lang == DW_LANG_Fortran03
4806 || lang == DW_LANG_Fortran08);
4809 /* Return TRUE if the language is Ada. */
4811 static inline bool
4812 is_ada (void)
4814 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4816 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4819 /* Remove the specified attribute if present. Return TRUE if removal
4820 was successful. */
4822 static bool
4823 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4825 dw_attr_node *a;
4826 unsigned ix;
4828 if (! die)
4829 return false;
4831 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4832 if (a->dw_attr == attr_kind)
4834 if (AT_class (a) == dw_val_class_str)
4835 if (a->dw_attr_val.v.val_str->refcount)
4836 a->dw_attr_val.v.val_str->refcount--;
4838 /* vec::ordered_remove should help reduce the number of abbrevs
4839 that are needed. */
4840 die->die_attr->ordered_remove (ix);
4841 return true;
4843 return false;
4846 /* Remove CHILD from its parent. PREV must have the property that
4847 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4849 static void
4850 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4852 gcc_assert (child->die_parent == prev->die_parent);
4853 gcc_assert (prev->die_sib == child);
4854 if (prev == child)
4856 gcc_assert (child->die_parent->die_child == child);
4857 prev = NULL;
4859 else
4860 prev->die_sib = child->die_sib;
4861 if (child->die_parent->die_child == child)
4862 child->die_parent->die_child = prev;
4863 child->die_sib = NULL;
4866 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4867 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4869 static void
4870 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4872 dw_die_ref parent = old_child->die_parent;
4874 gcc_assert (parent == prev->die_parent);
4875 gcc_assert (prev->die_sib == old_child);
4877 new_child->die_parent = parent;
4878 if (prev == old_child)
4880 gcc_assert (parent->die_child == old_child);
4881 new_child->die_sib = new_child;
4883 else
4885 prev->die_sib = new_child;
4886 new_child->die_sib = old_child->die_sib;
4888 if (old_child->die_parent->die_child == old_child)
4889 old_child->die_parent->die_child = new_child;
4890 old_child->die_sib = NULL;
4893 /* Move all children from OLD_PARENT to NEW_PARENT. */
4895 static void
4896 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4898 dw_die_ref c;
4899 new_parent->die_child = old_parent->die_child;
4900 old_parent->die_child = NULL;
4901 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4904 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4905 matches TAG. */
4907 static void
4908 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4910 dw_die_ref c;
4912 c = die->die_child;
4913 if (c) do {
4914 dw_die_ref prev = c;
4915 c = c->die_sib;
4916 while (c->die_tag == tag)
4918 remove_child_with_prev (c, prev);
4919 c->die_parent = NULL;
4920 /* Might have removed every child. */
4921 if (die->die_child == NULL)
4922 return;
4923 c = prev->die_sib;
4925 } while (c != die->die_child);
4928 /* Add a CHILD_DIE as the last child of DIE. */
4930 static void
4931 add_child_die (dw_die_ref die, dw_die_ref child_die)
4933 /* FIXME this should probably be an assert. */
4934 if (! die || ! child_die)
4935 return;
4936 gcc_assert (die != child_die);
4938 child_die->die_parent = die;
4939 if (die->die_child)
4941 child_die->die_sib = die->die_child->die_sib;
4942 die->die_child->die_sib = child_die;
4944 else
4945 child_die->die_sib = child_die;
4946 die->die_child = child_die;
4949 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
4951 static void
4952 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4953 dw_die_ref after_die)
4955 gcc_assert (die
4956 && child_die
4957 && after_die
4958 && die->die_child
4959 && die != child_die);
4961 child_die->die_parent = die;
4962 child_die->die_sib = after_die->die_sib;
4963 after_die->die_sib = child_die;
4964 if (die->die_child == after_die)
4965 die->die_child = child_die;
4968 /* Unassociate CHILD from its parent, and make its parent be
4969 NEW_PARENT. */
4971 static void
4972 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4974 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4975 if (p->die_sib == child)
4977 remove_child_with_prev (child, p);
4978 break;
4980 add_child_die (new_parent, child);
4983 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4984 is the specification, to the end of PARENT's list of children.
4985 This is done by removing and re-adding it. */
4987 static void
4988 splice_child_die (dw_die_ref parent, dw_die_ref child)
4990 /* We want the declaration DIE from inside the class, not the
4991 specification DIE at toplevel. */
4992 if (child->die_parent != parent)
4994 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4996 if (tmp)
4997 child = tmp;
5000 gcc_assert (child->die_parent == parent
5001 || (child->die_parent
5002 == get_AT_ref (parent, DW_AT_specification)));
5004 reparent_child (child, parent);
5007 /* Create and return a new die with a parent of PARENT_DIE. If
5008 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5009 associated tree T must be supplied to determine parenthood
5010 later. */
5012 static inline dw_die_ref
5013 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5015 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5017 die->die_tag = tag_value;
5019 if (parent_die != NULL)
5020 add_child_die (parent_die, die);
5021 else
5023 limbo_die_node *limbo_node;
5025 /* No DIEs created after early dwarf should end up in limbo,
5026 because the limbo list should not persist past LTO
5027 streaming. */
5028 if (tag_value != DW_TAG_compile_unit
5029 /* These are allowed because they're generated while
5030 breaking out COMDAT units late. */
5031 && tag_value != DW_TAG_type_unit
5032 && !early_dwarf
5033 /* Allow nested functions to live in limbo because they will
5034 only temporarily live there, as decls_for_scope will fix
5035 them up. */
5036 && (TREE_CODE (t) != FUNCTION_DECL
5037 || !decl_function_context (t))
5038 /* Same as nested functions above but for types. Types that
5039 are local to a function will be fixed in
5040 decls_for_scope. */
5041 && (!RECORD_OR_UNION_TYPE_P (t)
5042 || !TYPE_CONTEXT (t)
5043 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5044 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5045 especially in the ltrans stage, but once we implement LTO
5046 dwarf streaming, we should remove this exception. */
5047 && !in_lto_p)
5049 fprintf (stderr, "symbol ended up in limbo too late:");
5050 debug_generic_stmt (t);
5051 gcc_unreachable ();
5054 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5055 limbo_node->die = die;
5056 limbo_node->created_for = t;
5057 limbo_node->next = limbo_die_list;
5058 limbo_die_list = limbo_node;
5061 return die;
5064 /* Return the DIE associated with the given type specifier. */
5066 static inline dw_die_ref
5067 lookup_type_die (tree type)
5069 return TYPE_SYMTAB_DIE (type);
5072 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5073 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5074 anonymous type instead the one of the naming typedef. */
5076 static inline dw_die_ref
5077 strip_naming_typedef (tree type, dw_die_ref type_die)
5079 if (type
5080 && TREE_CODE (type) == RECORD_TYPE
5081 && type_die
5082 && type_die->die_tag == DW_TAG_typedef
5083 && is_naming_typedef_decl (TYPE_NAME (type)))
5084 type_die = get_AT_ref (type_die, DW_AT_type);
5085 return type_die;
5088 /* Like lookup_type_die, but if type is an anonymous type named by a
5089 typedef[1], return the DIE of the anonymous type instead the one of
5090 the naming typedef. This is because in gen_typedef_die, we did
5091 equate the anonymous struct named by the typedef with the DIE of
5092 the naming typedef. So by default, lookup_type_die on an anonymous
5093 struct yields the DIE of the naming typedef.
5095 [1]: Read the comment of is_naming_typedef_decl to learn about what
5096 a naming typedef is. */
5098 static inline dw_die_ref
5099 lookup_type_die_strip_naming_typedef (tree type)
5101 dw_die_ref die = lookup_type_die (type);
5102 return strip_naming_typedef (type, die);
5105 /* Equate a DIE to a given type specifier. */
5107 static inline void
5108 equate_type_number_to_die (tree type, dw_die_ref type_die)
5110 TYPE_SYMTAB_DIE (type) = type_die;
5113 /* Returns a hash value for X (which really is a die_struct). */
5115 inline hashval_t
5116 decl_die_hasher::hash (die_node *x)
5118 return (hashval_t) x->decl_id;
5121 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5123 inline bool
5124 decl_die_hasher::equal (die_node *x, tree y)
5126 return (x->decl_id == DECL_UID (y));
5129 /* Return the DIE associated with a given declaration. */
5131 static inline dw_die_ref
5132 lookup_decl_die (tree decl)
5134 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5137 /* Returns a hash value for X (which really is a var_loc_list). */
5139 inline hashval_t
5140 decl_loc_hasher::hash (var_loc_list *x)
5142 return (hashval_t) x->decl_id;
5145 /* Return nonzero if decl_id of var_loc_list X is the same as
5146 UID of decl *Y. */
5148 inline bool
5149 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5151 return (x->decl_id == DECL_UID (y));
5154 /* Return the var_loc list associated with a given declaration. */
5156 static inline var_loc_list *
5157 lookup_decl_loc (const_tree decl)
5159 if (!decl_loc_table)
5160 return NULL;
5161 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5164 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5166 inline hashval_t
5167 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5169 return (hashval_t) x->decl_id;
5172 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5173 UID of decl *Y. */
5175 inline bool
5176 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5178 return (x->decl_id == DECL_UID (y));
5181 /* Equate a DIE to a particular declaration. */
5183 static void
5184 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5186 unsigned int decl_id = DECL_UID (decl);
5188 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5189 decl_die->decl_id = decl_id;
5192 /* Return how many bits covers PIECE EXPR_LIST. */
5194 static HOST_WIDE_INT
5195 decl_piece_bitsize (rtx piece)
5197 int ret = (int) GET_MODE (piece);
5198 if (ret)
5199 return ret;
5200 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5201 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5202 return INTVAL (XEXP (XEXP (piece, 0), 0));
5205 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5207 static rtx *
5208 decl_piece_varloc_ptr (rtx piece)
5210 if ((int) GET_MODE (piece))
5211 return &XEXP (piece, 0);
5212 else
5213 return &XEXP (XEXP (piece, 0), 1);
5216 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5217 Next is the chain of following piece nodes. */
5219 static rtx_expr_list *
5220 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5222 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5223 return alloc_EXPR_LIST (bitsize, loc_note, next);
5224 else
5225 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5226 GEN_INT (bitsize),
5227 loc_note), next);
5230 /* Return rtx that should be stored into loc field for
5231 LOC_NOTE and BITPOS/BITSIZE. */
5233 static rtx
5234 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5235 HOST_WIDE_INT bitsize)
5237 if (bitsize != -1)
5239 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5240 if (bitpos != 0)
5241 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5243 return loc_note;
5246 /* This function either modifies location piece list *DEST in
5247 place (if SRC and INNER is NULL), or copies location piece list
5248 *SRC to *DEST while modifying it. Location BITPOS is modified
5249 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5250 not copied and if needed some padding around it is added.
5251 When modifying in place, DEST should point to EXPR_LIST where
5252 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5253 to the start of the whole list and INNER points to the EXPR_LIST
5254 where earlier pieces cover PIECE_BITPOS bits. */
5256 static void
5257 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5258 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5259 HOST_WIDE_INT bitsize, rtx loc_note)
5261 HOST_WIDE_INT diff;
5262 bool copy = inner != NULL;
5264 if (copy)
5266 /* First copy all nodes preceding the current bitpos. */
5267 while (src != inner)
5269 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5270 decl_piece_bitsize (*src), NULL_RTX);
5271 dest = &XEXP (*dest, 1);
5272 src = &XEXP (*src, 1);
5275 /* Add padding if needed. */
5276 if (bitpos != piece_bitpos)
5278 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5279 copy ? NULL_RTX : *dest);
5280 dest = &XEXP (*dest, 1);
5282 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5284 gcc_assert (!copy);
5285 /* A piece with correct bitpos and bitsize already exist,
5286 just update the location for it and return. */
5287 *decl_piece_varloc_ptr (*dest) = loc_note;
5288 return;
5290 /* Add the piece that changed. */
5291 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5292 dest = &XEXP (*dest, 1);
5293 /* Skip over pieces that overlap it. */
5294 diff = bitpos - piece_bitpos + bitsize;
5295 if (!copy)
5296 src = dest;
5297 while (diff > 0 && *src)
5299 rtx piece = *src;
5300 diff -= decl_piece_bitsize (piece);
5301 if (copy)
5302 src = &XEXP (piece, 1);
5303 else
5305 *src = XEXP (piece, 1);
5306 free_EXPR_LIST_node (piece);
5309 /* Add padding if needed. */
5310 if (diff < 0 && *src)
5312 if (!copy)
5313 dest = src;
5314 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5315 dest = &XEXP (*dest, 1);
5317 if (!copy)
5318 return;
5319 /* Finally copy all nodes following it. */
5320 while (*src)
5322 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5323 decl_piece_bitsize (*src), NULL_RTX);
5324 dest = &XEXP (*dest, 1);
5325 src = &XEXP (*src, 1);
5329 /* Add a variable location node to the linked list for DECL. */
5331 static struct var_loc_node *
5332 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5334 unsigned int decl_id;
5335 var_loc_list *temp;
5336 struct var_loc_node *loc = NULL;
5337 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5339 if (TREE_CODE (decl) == VAR_DECL
5340 && DECL_HAS_DEBUG_EXPR_P (decl))
5342 tree realdecl = DECL_DEBUG_EXPR (decl);
5343 if (handled_component_p (realdecl)
5344 || (TREE_CODE (realdecl) == MEM_REF
5345 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5347 HOST_WIDE_INT maxsize;
5348 bool reverse;
5349 tree innerdecl
5350 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5351 &reverse);
5352 if (!DECL_P (innerdecl)
5353 || DECL_IGNORED_P (innerdecl)
5354 || TREE_STATIC (innerdecl)
5355 || bitsize <= 0
5356 || bitpos + bitsize > 256
5357 || bitsize != maxsize)
5358 return NULL;
5359 decl = innerdecl;
5363 decl_id = DECL_UID (decl);
5364 var_loc_list **slot
5365 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5366 if (*slot == NULL)
5368 temp = ggc_cleared_alloc<var_loc_list> ();
5369 temp->decl_id = decl_id;
5370 *slot = temp;
5372 else
5373 temp = *slot;
5375 /* For PARM_DECLs try to keep around the original incoming value,
5376 even if that means we'll emit a zero-range .debug_loc entry. */
5377 if (temp->last
5378 && temp->first == temp->last
5379 && TREE_CODE (decl) == PARM_DECL
5380 && NOTE_P (temp->first->loc)
5381 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5382 && DECL_INCOMING_RTL (decl)
5383 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5384 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5385 == GET_CODE (DECL_INCOMING_RTL (decl))
5386 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5387 && (bitsize != -1
5388 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5389 NOTE_VAR_LOCATION_LOC (loc_note))
5390 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5391 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5393 loc = ggc_cleared_alloc<var_loc_node> ();
5394 temp->first->next = loc;
5395 temp->last = loc;
5396 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5398 else if (temp->last)
5400 struct var_loc_node *last = temp->last, *unused = NULL;
5401 rtx *piece_loc = NULL, last_loc_note;
5402 HOST_WIDE_INT piece_bitpos = 0;
5403 if (last->next)
5405 last = last->next;
5406 gcc_assert (last->next == NULL);
5408 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5410 piece_loc = &last->loc;
5413 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5414 if (piece_bitpos + cur_bitsize > bitpos)
5415 break;
5416 piece_bitpos += cur_bitsize;
5417 piece_loc = &XEXP (*piece_loc, 1);
5419 while (*piece_loc);
5421 /* TEMP->LAST here is either pointer to the last but one or
5422 last element in the chained list, LAST is pointer to the
5423 last element. */
5424 if (label && strcmp (last->label, label) == 0)
5426 /* For SRA optimized variables if there weren't any real
5427 insns since last note, just modify the last node. */
5428 if (piece_loc != NULL)
5430 adjust_piece_list (piece_loc, NULL, NULL,
5431 bitpos, piece_bitpos, bitsize, loc_note);
5432 return NULL;
5434 /* If the last note doesn't cover any instructions, remove it. */
5435 if (temp->last != last)
5437 temp->last->next = NULL;
5438 unused = last;
5439 last = temp->last;
5440 gcc_assert (strcmp (last->label, label) != 0);
5442 else
5444 gcc_assert (temp->first == temp->last
5445 || (temp->first->next == temp->last
5446 && TREE_CODE (decl) == PARM_DECL));
5447 memset (temp->last, '\0', sizeof (*temp->last));
5448 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5449 return temp->last;
5452 if (bitsize == -1 && NOTE_P (last->loc))
5453 last_loc_note = last->loc;
5454 else if (piece_loc != NULL
5455 && *piece_loc != NULL_RTX
5456 && piece_bitpos == bitpos
5457 && decl_piece_bitsize (*piece_loc) == bitsize)
5458 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5459 else
5460 last_loc_note = NULL_RTX;
5461 /* If the current location is the same as the end of the list,
5462 and either both or neither of the locations is uninitialized,
5463 we have nothing to do. */
5464 if (last_loc_note == NULL_RTX
5465 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5466 NOTE_VAR_LOCATION_LOC (loc_note)))
5467 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5468 != NOTE_VAR_LOCATION_STATUS (loc_note))
5469 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5470 == VAR_INIT_STATUS_UNINITIALIZED)
5471 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5472 == VAR_INIT_STATUS_UNINITIALIZED))))
5474 /* Add LOC to the end of list and update LAST. If the last
5475 element of the list has been removed above, reuse its
5476 memory for the new node, otherwise allocate a new one. */
5477 if (unused)
5479 loc = unused;
5480 memset (loc, '\0', sizeof (*loc));
5482 else
5483 loc = ggc_cleared_alloc<var_loc_node> ();
5484 if (bitsize == -1 || piece_loc == NULL)
5485 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5486 else
5487 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5488 bitpos, piece_bitpos, bitsize, loc_note);
5489 last->next = loc;
5490 /* Ensure TEMP->LAST will point either to the new last but one
5491 element of the chain, or to the last element in it. */
5492 if (last != temp->last)
5493 temp->last = last;
5495 else if (unused)
5496 ggc_free (unused);
5498 else
5500 loc = ggc_cleared_alloc<var_loc_node> ();
5501 temp->first = loc;
5502 temp->last = loc;
5503 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5505 return loc;
5508 /* Keep track of the number of spaces used to indent the
5509 output of the debugging routines that print the structure of
5510 the DIE internal representation. */
5511 static int print_indent;
5513 /* Indent the line the number of spaces given by print_indent. */
5515 static inline void
5516 print_spaces (FILE *outfile)
5518 fprintf (outfile, "%*s", print_indent, "");
5521 /* Print a type signature in hex. */
5523 static inline void
5524 print_signature (FILE *outfile, char *sig)
5526 int i;
5528 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5529 fprintf (outfile, "%02x", sig[i] & 0xff);
5532 static inline void
5533 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5535 if (discr_value->pos)
5536 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5537 else
5538 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5541 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5543 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5544 RECURSE, output location descriptor operations. */
5546 static void
5547 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5549 switch (val->val_class)
5551 case dw_val_class_addr:
5552 fprintf (outfile, "address");
5553 break;
5554 case dw_val_class_offset:
5555 fprintf (outfile, "offset");
5556 break;
5557 case dw_val_class_loc:
5558 fprintf (outfile, "location descriptor");
5559 if (val->v.val_loc == NULL)
5560 fprintf (outfile, " -> <null>\n");
5561 else if (recurse)
5563 fprintf (outfile, ":\n");
5564 print_indent += 4;
5565 print_loc_descr (val->v.val_loc, outfile);
5566 print_indent -= 4;
5568 else
5569 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5570 break;
5571 case dw_val_class_loc_list:
5572 fprintf (outfile, "location list -> label:%s",
5573 val->v.val_loc_list->ll_symbol);
5574 break;
5575 case dw_val_class_range_list:
5576 fprintf (outfile, "range list");
5577 break;
5578 case dw_val_class_const:
5579 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5580 break;
5581 case dw_val_class_unsigned_const:
5582 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5583 break;
5584 case dw_val_class_const_double:
5585 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5586 HOST_WIDE_INT_PRINT_UNSIGNED")",
5587 val->v.val_double.high,
5588 val->v.val_double.low);
5589 break;
5590 case dw_val_class_wide_int:
5592 int i = val->v.val_wide->get_len ();
5593 fprintf (outfile, "constant (");
5594 gcc_assert (i > 0);
5595 if (val->v.val_wide->elt (i - 1) == 0)
5596 fprintf (outfile, "0x");
5597 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5598 val->v.val_wide->elt (--i));
5599 while (--i >= 0)
5600 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5601 val->v.val_wide->elt (i));
5602 fprintf (outfile, ")");
5603 break;
5605 case dw_val_class_vec:
5606 fprintf (outfile, "floating-point or vector constant");
5607 break;
5608 case dw_val_class_flag:
5609 fprintf (outfile, "%u", val->v.val_flag);
5610 break;
5611 case dw_val_class_die_ref:
5612 if (val->v.val_die_ref.die != NULL)
5614 dw_die_ref die = val->v.val_die_ref.die;
5616 if (die->comdat_type_p)
5618 fprintf (outfile, "die -> signature: ");
5619 print_signature (outfile,
5620 die->die_id.die_type_node->signature);
5622 else if (die->die_id.die_symbol)
5623 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5624 else
5625 fprintf (outfile, "die -> %ld", die->die_offset);
5626 fprintf (outfile, " (%p)", (void *) die);
5628 else
5629 fprintf (outfile, "die -> <null>");
5630 break;
5631 case dw_val_class_vms_delta:
5632 fprintf (outfile, "delta: @slotcount(%s-%s)",
5633 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5634 break;
5635 case dw_val_class_lbl_id:
5636 case dw_val_class_lineptr:
5637 case dw_val_class_macptr:
5638 case dw_val_class_high_pc:
5639 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5640 break;
5641 case dw_val_class_str:
5642 if (val->v.val_str->str != NULL)
5643 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5644 else
5645 fprintf (outfile, "<null>");
5646 break;
5647 case dw_val_class_file:
5648 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5649 val->v.val_file->emitted_number);
5650 break;
5651 case dw_val_class_data8:
5653 int i;
5655 for (i = 0; i < 8; i++)
5656 fprintf (outfile, "%02x", val->v.val_data8[i]);
5657 break;
5659 case dw_val_class_discr_value:
5660 print_discr_value (outfile, &val->v.val_discr_value);
5661 break;
5662 case dw_val_class_discr_list:
5663 for (dw_discr_list_ref node = val->v.val_discr_list;
5664 node != NULL;
5665 node = node->dw_discr_next)
5667 if (node->dw_discr_range)
5669 fprintf (outfile, " .. ");
5670 print_discr_value (outfile, &node->dw_discr_lower_bound);
5671 print_discr_value (outfile, &node->dw_discr_upper_bound);
5673 else
5674 print_discr_value (outfile, &node->dw_discr_lower_bound);
5676 if (node->dw_discr_next != NULL)
5677 fprintf (outfile, " | ");
5679 default:
5680 break;
5684 /* Likewise, for a DIE attribute. */
5686 static void
5687 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5689 print_dw_val (&a->dw_attr_val, recurse, outfile);
5693 /* Print the list of operands in the LOC location description to OUTFILE. This
5694 routine is a debugging aid only. */
5696 static void
5697 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5699 dw_loc_descr_ref l = loc;
5701 if (loc == NULL)
5703 print_spaces (outfile);
5704 fprintf (outfile, "<null>\n");
5705 return;
5708 for (l = loc; l != NULL; l = l->dw_loc_next)
5710 print_spaces (outfile);
5711 fprintf (outfile, "(%p) %s",
5712 (void *) l,
5713 dwarf_stack_op_name (l->dw_loc_opc));
5714 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5716 fprintf (outfile, " ");
5717 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5719 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5721 fprintf (outfile, ", ");
5722 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5724 fprintf (outfile, "\n");
5728 /* Print the information associated with a given DIE, and its children.
5729 This routine is a debugging aid only. */
5731 static void
5732 print_die (dw_die_ref die, FILE *outfile)
5734 dw_attr_node *a;
5735 dw_die_ref c;
5736 unsigned ix;
5738 print_spaces (outfile);
5739 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5740 die->die_offset, dwarf_tag_name (die->die_tag),
5741 (void*) die);
5742 print_spaces (outfile);
5743 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5744 fprintf (outfile, " offset: %ld", die->die_offset);
5745 fprintf (outfile, " mark: %d\n", die->die_mark);
5747 if (die->comdat_type_p)
5749 print_spaces (outfile);
5750 fprintf (outfile, " signature: ");
5751 print_signature (outfile, die->die_id.die_type_node->signature);
5752 fprintf (outfile, "\n");
5755 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5757 print_spaces (outfile);
5758 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5760 print_attribute (a, true, outfile);
5761 fprintf (outfile, "\n");
5764 if (die->die_child != NULL)
5766 print_indent += 4;
5767 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5768 print_indent -= 4;
5770 if (print_indent == 0)
5771 fprintf (outfile, "\n");
5774 /* Print the list of operations in the LOC location description. */
5776 DEBUG_FUNCTION void
5777 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5779 print_loc_descr (loc, stderr);
5782 /* Print the information collected for a given DIE. */
5784 DEBUG_FUNCTION void
5785 debug_dwarf_die (dw_die_ref die)
5787 print_die (die, stderr);
5790 DEBUG_FUNCTION void
5791 debug (die_struct &ref)
5793 print_die (&ref, stderr);
5796 DEBUG_FUNCTION void
5797 debug (die_struct *ptr)
5799 if (ptr)
5800 debug (*ptr);
5801 else
5802 fprintf (stderr, "<nil>\n");
5806 /* Print all DWARF information collected for the compilation unit.
5807 This routine is a debugging aid only. */
5809 DEBUG_FUNCTION void
5810 debug_dwarf (void)
5812 print_indent = 0;
5813 print_die (comp_unit_die (), stderr);
5816 /* Sanity checks on DIEs. */
5818 static void
5819 check_die (dw_die_ref die)
5821 unsigned ix;
5822 dw_attr_node *a;
5823 bool inline_found = false;
5824 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5825 int n_decl_line = 0, n_decl_file = 0;
5826 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5828 switch (a->dw_attr)
5830 case DW_AT_inline:
5831 if (a->dw_attr_val.v.val_unsigned)
5832 inline_found = true;
5833 break;
5834 case DW_AT_location:
5835 ++n_location;
5836 break;
5837 case DW_AT_low_pc:
5838 ++n_low_pc;
5839 break;
5840 case DW_AT_high_pc:
5841 ++n_high_pc;
5842 break;
5843 case DW_AT_artificial:
5844 ++n_artificial;
5845 break;
5846 case DW_AT_decl_line:
5847 ++n_decl_line;
5848 break;
5849 case DW_AT_decl_file:
5850 ++n_decl_file;
5851 break;
5852 default:
5853 break;
5856 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5857 || n_decl_line > 1 || n_decl_file > 1)
5859 fprintf (stderr, "Duplicate attributes in DIE:\n");
5860 debug_dwarf_die (die);
5861 gcc_unreachable ();
5863 if (inline_found)
5865 /* A debugging information entry that is a member of an abstract
5866 instance tree [that has DW_AT_inline] should not contain any
5867 attributes which describe aspects of the subroutine which vary
5868 between distinct inlined expansions or distinct out-of-line
5869 expansions. */
5870 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5871 gcc_assert (a->dw_attr != DW_AT_low_pc
5872 && a->dw_attr != DW_AT_high_pc
5873 && a->dw_attr != DW_AT_location
5874 && a->dw_attr != DW_AT_frame_base
5875 && a->dw_attr != DW_AT_GNU_all_call_sites);
5879 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5880 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5881 DIE that marks the start of the DIEs for this include file. */
5883 static dw_die_ref
5884 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5886 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5887 dw_die_ref new_unit = gen_compile_unit_die (filename);
5889 new_unit->die_sib = old_unit;
5890 return new_unit;
5893 /* Close an include-file CU and reopen the enclosing one. */
5895 static dw_die_ref
5896 pop_compile_unit (dw_die_ref old_unit)
5898 dw_die_ref new_unit = old_unit->die_sib;
5900 old_unit->die_sib = NULL;
5901 return new_unit;
5904 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5905 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5906 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5908 /* Calculate the checksum of a location expression. */
5910 static inline void
5911 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5913 int tem;
5914 inchash::hash hstate;
5915 hashval_t hash;
5917 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5918 CHECKSUM (tem);
5919 hash_loc_operands (loc, hstate);
5920 hash = hstate.end();
5921 CHECKSUM (hash);
5924 /* Calculate the checksum of an attribute. */
5926 static void
5927 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5929 dw_loc_descr_ref loc;
5930 rtx r;
5932 CHECKSUM (at->dw_attr);
5934 /* We don't care that this was compiled with a different compiler
5935 snapshot; if the output is the same, that's what matters. */
5936 if (at->dw_attr == DW_AT_producer)
5937 return;
5939 switch (AT_class (at))
5941 case dw_val_class_const:
5942 CHECKSUM (at->dw_attr_val.v.val_int);
5943 break;
5944 case dw_val_class_unsigned_const:
5945 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5946 break;
5947 case dw_val_class_const_double:
5948 CHECKSUM (at->dw_attr_val.v.val_double);
5949 break;
5950 case dw_val_class_wide_int:
5951 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5952 get_full_len (*at->dw_attr_val.v.val_wide)
5953 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5954 break;
5955 case dw_val_class_vec:
5956 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5957 (at->dw_attr_val.v.val_vec.length
5958 * at->dw_attr_val.v.val_vec.elt_size));
5959 break;
5960 case dw_val_class_flag:
5961 CHECKSUM (at->dw_attr_val.v.val_flag);
5962 break;
5963 case dw_val_class_str:
5964 CHECKSUM_STRING (AT_string (at));
5965 break;
5967 case dw_val_class_addr:
5968 r = AT_addr (at);
5969 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5970 CHECKSUM_STRING (XSTR (r, 0));
5971 break;
5973 case dw_val_class_offset:
5974 CHECKSUM (at->dw_attr_val.v.val_offset);
5975 break;
5977 case dw_val_class_loc:
5978 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5979 loc_checksum (loc, ctx);
5980 break;
5982 case dw_val_class_die_ref:
5983 die_checksum (AT_ref (at), ctx, mark);
5984 break;
5986 case dw_val_class_fde_ref:
5987 case dw_val_class_vms_delta:
5988 case dw_val_class_lbl_id:
5989 case dw_val_class_lineptr:
5990 case dw_val_class_macptr:
5991 case dw_val_class_high_pc:
5992 break;
5994 case dw_val_class_file:
5995 CHECKSUM_STRING (AT_file (at)->filename);
5996 break;
5998 case dw_val_class_data8:
5999 CHECKSUM (at->dw_attr_val.v.val_data8);
6000 break;
6002 default:
6003 break;
6007 /* Calculate the checksum of a DIE. */
6009 static void
6010 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6012 dw_die_ref c;
6013 dw_attr_node *a;
6014 unsigned ix;
6016 /* To avoid infinite recursion. */
6017 if (die->die_mark)
6019 CHECKSUM (die->die_mark);
6020 return;
6022 die->die_mark = ++(*mark);
6024 CHECKSUM (die->die_tag);
6026 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6027 attr_checksum (a, ctx, mark);
6029 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6032 #undef CHECKSUM
6033 #undef CHECKSUM_BLOCK
6034 #undef CHECKSUM_STRING
6036 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6037 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6038 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6039 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6040 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6041 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6042 #define CHECKSUM_ATTR(FOO) \
6043 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6045 /* Calculate the checksum of a number in signed LEB128 format. */
6047 static void
6048 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6050 unsigned char byte;
6051 bool more;
6053 while (1)
6055 byte = (value & 0x7f);
6056 value >>= 7;
6057 more = !((value == 0 && (byte & 0x40) == 0)
6058 || (value == -1 && (byte & 0x40) != 0));
6059 if (more)
6060 byte |= 0x80;
6061 CHECKSUM (byte);
6062 if (!more)
6063 break;
6067 /* Calculate the checksum of a number in unsigned LEB128 format. */
6069 static void
6070 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6072 while (1)
6074 unsigned char byte = (value & 0x7f);
6075 value >>= 7;
6076 if (value != 0)
6077 /* More bytes to follow. */
6078 byte |= 0x80;
6079 CHECKSUM (byte);
6080 if (value == 0)
6081 break;
6085 /* Checksum the context of the DIE. This adds the names of any
6086 surrounding namespaces or structures to the checksum. */
6088 static void
6089 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6091 const char *name;
6092 dw_die_ref spec;
6093 int tag = die->die_tag;
6095 if (tag != DW_TAG_namespace
6096 && tag != DW_TAG_structure_type
6097 && tag != DW_TAG_class_type)
6098 return;
6100 name = get_AT_string (die, DW_AT_name);
6102 spec = get_AT_ref (die, DW_AT_specification);
6103 if (spec != NULL)
6104 die = spec;
6106 if (die->die_parent != NULL)
6107 checksum_die_context (die->die_parent, ctx);
6109 CHECKSUM_ULEB128 ('C');
6110 CHECKSUM_ULEB128 (tag);
6111 if (name != NULL)
6112 CHECKSUM_STRING (name);
6115 /* Calculate the checksum of a location expression. */
6117 static inline void
6118 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6120 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6121 were emitted as a DW_FORM_sdata instead of a location expression. */
6122 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6124 CHECKSUM_ULEB128 (DW_FORM_sdata);
6125 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6126 return;
6129 /* Otherwise, just checksum the raw location expression. */
6130 while (loc != NULL)
6132 inchash::hash hstate;
6133 hashval_t hash;
6135 CHECKSUM_ULEB128 (loc->dtprel);
6136 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6137 hash_loc_operands (loc, hstate);
6138 hash = hstate.end ();
6139 CHECKSUM (hash);
6140 loc = loc->dw_loc_next;
6144 /* Calculate the checksum of an attribute. */
6146 static void
6147 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6148 struct md5_ctx *ctx, int *mark)
6150 dw_loc_descr_ref loc;
6151 rtx r;
6153 if (AT_class (at) == dw_val_class_die_ref)
6155 dw_die_ref target_die = AT_ref (at);
6157 /* For pointer and reference types, we checksum only the (qualified)
6158 name of the target type (if there is a name). For friend entries,
6159 we checksum only the (qualified) name of the target type or function.
6160 This allows the checksum to remain the same whether the target type
6161 is complete or not. */
6162 if ((at->dw_attr == DW_AT_type
6163 && (tag == DW_TAG_pointer_type
6164 || tag == DW_TAG_reference_type
6165 || tag == DW_TAG_rvalue_reference_type
6166 || tag == DW_TAG_ptr_to_member_type))
6167 || (at->dw_attr == DW_AT_friend
6168 && tag == DW_TAG_friend))
6170 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6172 if (name_attr != NULL)
6174 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6176 if (decl == NULL)
6177 decl = target_die;
6178 CHECKSUM_ULEB128 ('N');
6179 CHECKSUM_ULEB128 (at->dw_attr);
6180 if (decl->die_parent != NULL)
6181 checksum_die_context (decl->die_parent, ctx);
6182 CHECKSUM_ULEB128 ('E');
6183 CHECKSUM_STRING (AT_string (name_attr));
6184 return;
6188 /* For all other references to another DIE, we check to see if the
6189 target DIE has already been visited. If it has, we emit a
6190 backward reference; if not, we descend recursively. */
6191 if (target_die->die_mark > 0)
6193 CHECKSUM_ULEB128 ('R');
6194 CHECKSUM_ULEB128 (at->dw_attr);
6195 CHECKSUM_ULEB128 (target_die->die_mark);
6197 else
6199 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6201 if (decl == NULL)
6202 decl = target_die;
6203 target_die->die_mark = ++(*mark);
6204 CHECKSUM_ULEB128 ('T');
6205 CHECKSUM_ULEB128 (at->dw_attr);
6206 if (decl->die_parent != NULL)
6207 checksum_die_context (decl->die_parent, ctx);
6208 die_checksum_ordered (target_die, ctx, mark);
6210 return;
6213 CHECKSUM_ULEB128 ('A');
6214 CHECKSUM_ULEB128 (at->dw_attr);
6216 switch (AT_class (at))
6218 case dw_val_class_const:
6219 CHECKSUM_ULEB128 (DW_FORM_sdata);
6220 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6221 break;
6223 case dw_val_class_unsigned_const:
6224 CHECKSUM_ULEB128 (DW_FORM_sdata);
6225 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6226 break;
6228 case dw_val_class_const_double:
6229 CHECKSUM_ULEB128 (DW_FORM_block);
6230 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6231 CHECKSUM (at->dw_attr_val.v.val_double);
6232 break;
6234 case dw_val_class_wide_int:
6235 CHECKSUM_ULEB128 (DW_FORM_block);
6236 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6237 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6238 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6239 get_full_len (*at->dw_attr_val.v.val_wide)
6240 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6241 break;
6243 case dw_val_class_vec:
6244 CHECKSUM_ULEB128 (DW_FORM_block);
6245 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6246 * at->dw_attr_val.v.val_vec.elt_size);
6247 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6248 (at->dw_attr_val.v.val_vec.length
6249 * at->dw_attr_val.v.val_vec.elt_size));
6250 break;
6252 case dw_val_class_flag:
6253 CHECKSUM_ULEB128 (DW_FORM_flag);
6254 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6255 break;
6257 case dw_val_class_str:
6258 CHECKSUM_ULEB128 (DW_FORM_string);
6259 CHECKSUM_STRING (AT_string (at));
6260 break;
6262 case dw_val_class_addr:
6263 r = AT_addr (at);
6264 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6265 CHECKSUM_ULEB128 (DW_FORM_string);
6266 CHECKSUM_STRING (XSTR (r, 0));
6267 break;
6269 case dw_val_class_offset:
6270 CHECKSUM_ULEB128 (DW_FORM_sdata);
6271 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6272 break;
6274 case dw_val_class_loc:
6275 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6276 loc_checksum_ordered (loc, ctx);
6277 break;
6279 case dw_val_class_fde_ref:
6280 case dw_val_class_lbl_id:
6281 case dw_val_class_lineptr:
6282 case dw_val_class_macptr:
6283 case dw_val_class_high_pc:
6284 break;
6286 case dw_val_class_file:
6287 CHECKSUM_ULEB128 (DW_FORM_string);
6288 CHECKSUM_STRING (AT_file (at)->filename);
6289 break;
6291 case dw_val_class_data8:
6292 CHECKSUM (at->dw_attr_val.v.val_data8);
6293 break;
6295 default:
6296 break;
6300 struct checksum_attributes
6302 dw_attr_node *at_name;
6303 dw_attr_node *at_type;
6304 dw_attr_node *at_friend;
6305 dw_attr_node *at_accessibility;
6306 dw_attr_node *at_address_class;
6307 dw_attr_node *at_allocated;
6308 dw_attr_node *at_artificial;
6309 dw_attr_node *at_associated;
6310 dw_attr_node *at_binary_scale;
6311 dw_attr_node *at_bit_offset;
6312 dw_attr_node *at_bit_size;
6313 dw_attr_node *at_bit_stride;
6314 dw_attr_node *at_byte_size;
6315 dw_attr_node *at_byte_stride;
6316 dw_attr_node *at_const_value;
6317 dw_attr_node *at_containing_type;
6318 dw_attr_node *at_count;
6319 dw_attr_node *at_data_location;
6320 dw_attr_node *at_data_member_location;
6321 dw_attr_node *at_decimal_scale;
6322 dw_attr_node *at_decimal_sign;
6323 dw_attr_node *at_default_value;
6324 dw_attr_node *at_digit_count;
6325 dw_attr_node *at_discr;
6326 dw_attr_node *at_discr_list;
6327 dw_attr_node *at_discr_value;
6328 dw_attr_node *at_encoding;
6329 dw_attr_node *at_endianity;
6330 dw_attr_node *at_explicit;
6331 dw_attr_node *at_is_optional;
6332 dw_attr_node *at_location;
6333 dw_attr_node *at_lower_bound;
6334 dw_attr_node *at_mutable;
6335 dw_attr_node *at_ordering;
6336 dw_attr_node *at_picture_string;
6337 dw_attr_node *at_prototyped;
6338 dw_attr_node *at_small;
6339 dw_attr_node *at_segment;
6340 dw_attr_node *at_string_length;
6341 dw_attr_node *at_string_length_bit_size;
6342 dw_attr_node *at_string_length_byte_size;
6343 dw_attr_node *at_threads_scaled;
6344 dw_attr_node *at_upper_bound;
6345 dw_attr_node *at_use_location;
6346 dw_attr_node *at_use_UTF8;
6347 dw_attr_node *at_variable_parameter;
6348 dw_attr_node *at_virtuality;
6349 dw_attr_node *at_visibility;
6350 dw_attr_node *at_vtable_elem_location;
6353 /* Collect the attributes that we will want to use for the checksum. */
6355 static void
6356 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6358 dw_attr_node *a;
6359 unsigned ix;
6361 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6363 switch (a->dw_attr)
6365 case DW_AT_name:
6366 attrs->at_name = a;
6367 break;
6368 case DW_AT_type:
6369 attrs->at_type = a;
6370 break;
6371 case DW_AT_friend:
6372 attrs->at_friend = a;
6373 break;
6374 case DW_AT_accessibility:
6375 attrs->at_accessibility = a;
6376 break;
6377 case DW_AT_address_class:
6378 attrs->at_address_class = a;
6379 break;
6380 case DW_AT_allocated:
6381 attrs->at_allocated = a;
6382 break;
6383 case DW_AT_artificial:
6384 attrs->at_artificial = a;
6385 break;
6386 case DW_AT_associated:
6387 attrs->at_associated = a;
6388 break;
6389 case DW_AT_binary_scale:
6390 attrs->at_binary_scale = a;
6391 break;
6392 case DW_AT_bit_offset:
6393 attrs->at_bit_offset = a;
6394 break;
6395 case DW_AT_bit_size:
6396 attrs->at_bit_size = a;
6397 break;
6398 case DW_AT_bit_stride:
6399 attrs->at_bit_stride = a;
6400 break;
6401 case DW_AT_byte_size:
6402 attrs->at_byte_size = a;
6403 break;
6404 case DW_AT_byte_stride:
6405 attrs->at_byte_stride = a;
6406 break;
6407 case DW_AT_const_value:
6408 attrs->at_const_value = a;
6409 break;
6410 case DW_AT_containing_type:
6411 attrs->at_containing_type = a;
6412 break;
6413 case DW_AT_count:
6414 attrs->at_count = a;
6415 break;
6416 case DW_AT_data_location:
6417 attrs->at_data_location = a;
6418 break;
6419 case DW_AT_data_member_location:
6420 attrs->at_data_member_location = a;
6421 break;
6422 case DW_AT_decimal_scale:
6423 attrs->at_decimal_scale = a;
6424 break;
6425 case DW_AT_decimal_sign:
6426 attrs->at_decimal_sign = a;
6427 break;
6428 case DW_AT_default_value:
6429 attrs->at_default_value = a;
6430 break;
6431 case DW_AT_digit_count:
6432 attrs->at_digit_count = a;
6433 break;
6434 case DW_AT_discr:
6435 attrs->at_discr = a;
6436 break;
6437 case DW_AT_discr_list:
6438 attrs->at_discr_list = a;
6439 break;
6440 case DW_AT_discr_value:
6441 attrs->at_discr_value = a;
6442 break;
6443 case DW_AT_encoding:
6444 attrs->at_encoding = a;
6445 break;
6446 case DW_AT_endianity:
6447 attrs->at_endianity = a;
6448 break;
6449 case DW_AT_explicit:
6450 attrs->at_explicit = a;
6451 break;
6452 case DW_AT_is_optional:
6453 attrs->at_is_optional = a;
6454 break;
6455 case DW_AT_location:
6456 attrs->at_location = a;
6457 break;
6458 case DW_AT_lower_bound:
6459 attrs->at_lower_bound = a;
6460 break;
6461 case DW_AT_mutable:
6462 attrs->at_mutable = a;
6463 break;
6464 case DW_AT_ordering:
6465 attrs->at_ordering = a;
6466 break;
6467 case DW_AT_picture_string:
6468 attrs->at_picture_string = a;
6469 break;
6470 case DW_AT_prototyped:
6471 attrs->at_prototyped = a;
6472 break;
6473 case DW_AT_small:
6474 attrs->at_small = a;
6475 break;
6476 case DW_AT_segment:
6477 attrs->at_segment = a;
6478 break;
6479 case DW_AT_string_length:
6480 attrs->at_string_length = a;
6481 break;
6482 case DW_AT_string_length_bit_size:
6483 attrs->at_string_length_bit_size = a;
6484 break;
6485 case DW_AT_string_length_byte_size:
6486 attrs->at_string_length_byte_size = a;
6487 break;
6488 case DW_AT_threads_scaled:
6489 attrs->at_threads_scaled = a;
6490 break;
6491 case DW_AT_upper_bound:
6492 attrs->at_upper_bound = a;
6493 break;
6494 case DW_AT_use_location:
6495 attrs->at_use_location = a;
6496 break;
6497 case DW_AT_use_UTF8:
6498 attrs->at_use_UTF8 = a;
6499 break;
6500 case DW_AT_variable_parameter:
6501 attrs->at_variable_parameter = a;
6502 break;
6503 case DW_AT_virtuality:
6504 attrs->at_virtuality = a;
6505 break;
6506 case DW_AT_visibility:
6507 attrs->at_visibility = a;
6508 break;
6509 case DW_AT_vtable_elem_location:
6510 attrs->at_vtable_elem_location = a;
6511 break;
6512 default:
6513 break;
6518 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6520 static void
6521 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6523 dw_die_ref c;
6524 dw_die_ref decl;
6525 struct checksum_attributes attrs;
6527 CHECKSUM_ULEB128 ('D');
6528 CHECKSUM_ULEB128 (die->die_tag);
6530 memset (&attrs, 0, sizeof (attrs));
6532 decl = get_AT_ref (die, DW_AT_specification);
6533 if (decl != NULL)
6534 collect_checksum_attributes (&attrs, decl);
6535 collect_checksum_attributes (&attrs, die);
6537 CHECKSUM_ATTR (attrs.at_name);
6538 CHECKSUM_ATTR (attrs.at_accessibility);
6539 CHECKSUM_ATTR (attrs.at_address_class);
6540 CHECKSUM_ATTR (attrs.at_allocated);
6541 CHECKSUM_ATTR (attrs.at_artificial);
6542 CHECKSUM_ATTR (attrs.at_associated);
6543 CHECKSUM_ATTR (attrs.at_binary_scale);
6544 CHECKSUM_ATTR (attrs.at_bit_offset);
6545 CHECKSUM_ATTR (attrs.at_bit_size);
6546 CHECKSUM_ATTR (attrs.at_bit_stride);
6547 CHECKSUM_ATTR (attrs.at_byte_size);
6548 CHECKSUM_ATTR (attrs.at_byte_stride);
6549 CHECKSUM_ATTR (attrs.at_const_value);
6550 CHECKSUM_ATTR (attrs.at_containing_type);
6551 CHECKSUM_ATTR (attrs.at_count);
6552 CHECKSUM_ATTR (attrs.at_data_location);
6553 CHECKSUM_ATTR (attrs.at_data_member_location);
6554 CHECKSUM_ATTR (attrs.at_decimal_scale);
6555 CHECKSUM_ATTR (attrs.at_decimal_sign);
6556 CHECKSUM_ATTR (attrs.at_default_value);
6557 CHECKSUM_ATTR (attrs.at_digit_count);
6558 CHECKSUM_ATTR (attrs.at_discr);
6559 CHECKSUM_ATTR (attrs.at_discr_list);
6560 CHECKSUM_ATTR (attrs.at_discr_value);
6561 CHECKSUM_ATTR (attrs.at_encoding);
6562 CHECKSUM_ATTR (attrs.at_endianity);
6563 CHECKSUM_ATTR (attrs.at_explicit);
6564 CHECKSUM_ATTR (attrs.at_is_optional);
6565 CHECKSUM_ATTR (attrs.at_location);
6566 CHECKSUM_ATTR (attrs.at_lower_bound);
6567 CHECKSUM_ATTR (attrs.at_mutable);
6568 CHECKSUM_ATTR (attrs.at_ordering);
6569 CHECKSUM_ATTR (attrs.at_picture_string);
6570 CHECKSUM_ATTR (attrs.at_prototyped);
6571 CHECKSUM_ATTR (attrs.at_small);
6572 CHECKSUM_ATTR (attrs.at_segment);
6573 CHECKSUM_ATTR (attrs.at_string_length);
6574 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6575 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6576 CHECKSUM_ATTR (attrs.at_threads_scaled);
6577 CHECKSUM_ATTR (attrs.at_upper_bound);
6578 CHECKSUM_ATTR (attrs.at_use_location);
6579 CHECKSUM_ATTR (attrs.at_use_UTF8);
6580 CHECKSUM_ATTR (attrs.at_variable_parameter);
6581 CHECKSUM_ATTR (attrs.at_virtuality);
6582 CHECKSUM_ATTR (attrs.at_visibility);
6583 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6584 CHECKSUM_ATTR (attrs.at_type);
6585 CHECKSUM_ATTR (attrs.at_friend);
6587 /* Checksum the child DIEs. */
6588 c = die->die_child;
6589 if (c) do {
6590 dw_attr_node *name_attr;
6592 c = c->die_sib;
6593 name_attr = get_AT (c, DW_AT_name);
6594 if (is_template_instantiation (c))
6596 /* Ignore instantiations of member type and function templates. */
6598 else if (name_attr != NULL
6599 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6601 /* Use a shallow checksum for named nested types and member
6602 functions. */
6603 CHECKSUM_ULEB128 ('S');
6604 CHECKSUM_ULEB128 (c->die_tag);
6605 CHECKSUM_STRING (AT_string (name_attr));
6607 else
6609 /* Use a deep checksum for other children. */
6610 /* Mark this DIE so it gets processed when unmarking. */
6611 if (c->die_mark == 0)
6612 c->die_mark = -1;
6613 die_checksum_ordered (c, ctx, mark);
6615 } while (c != die->die_child);
6617 CHECKSUM_ULEB128 (0);
6620 /* Add a type name and tag to a hash. */
6621 static void
6622 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6624 CHECKSUM_ULEB128 (tag);
6625 CHECKSUM_STRING (name);
6628 #undef CHECKSUM
6629 #undef CHECKSUM_STRING
6630 #undef CHECKSUM_ATTR
6631 #undef CHECKSUM_LEB128
6632 #undef CHECKSUM_ULEB128
6634 /* Generate the type signature for DIE. This is computed by generating an
6635 MD5 checksum over the DIE's tag, its relevant attributes, and its
6636 children. Attributes that are references to other DIEs are processed
6637 by recursion, using the MARK field to prevent infinite recursion.
6638 If the DIE is nested inside a namespace or another type, we also
6639 need to include that context in the signature. The lower 64 bits
6640 of the resulting MD5 checksum comprise the signature. */
6642 static void
6643 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6645 int mark;
6646 const char *name;
6647 unsigned char checksum[16];
6648 struct md5_ctx ctx;
6649 dw_die_ref decl;
6650 dw_die_ref parent;
6652 name = get_AT_string (die, DW_AT_name);
6653 decl = get_AT_ref (die, DW_AT_specification);
6654 parent = get_die_parent (die);
6656 /* First, compute a signature for just the type name (and its surrounding
6657 context, if any. This is stored in the type unit DIE for link-time
6658 ODR (one-definition rule) checking. */
6660 if (is_cxx () && name != NULL)
6662 md5_init_ctx (&ctx);
6664 /* Checksum the names of surrounding namespaces and structures. */
6665 if (parent != NULL)
6666 checksum_die_context (parent, &ctx);
6668 /* Checksum the current DIE. */
6669 die_odr_checksum (die->die_tag, name, &ctx);
6670 md5_finish_ctx (&ctx, checksum);
6672 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6675 /* Next, compute the complete type signature. */
6677 md5_init_ctx (&ctx);
6678 mark = 1;
6679 die->die_mark = mark;
6681 /* Checksum the names of surrounding namespaces and structures. */
6682 if (parent != NULL)
6683 checksum_die_context (parent, &ctx);
6685 /* Checksum the DIE and its children. */
6686 die_checksum_ordered (die, &ctx, &mark);
6687 unmark_all_dies (die);
6688 md5_finish_ctx (&ctx, checksum);
6690 /* Store the signature in the type node and link the type DIE and the
6691 type node together. */
6692 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6693 DWARF_TYPE_SIGNATURE_SIZE);
6694 die->comdat_type_p = true;
6695 die->die_id.die_type_node = type_node;
6696 type_node->type_die = die;
6698 /* If the DIE is a specification, link its declaration to the type node
6699 as well. */
6700 if (decl != NULL)
6702 decl->comdat_type_p = true;
6703 decl->die_id.die_type_node = type_node;
6707 /* Do the location expressions look same? */
6708 static inline int
6709 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6711 return loc1->dw_loc_opc == loc2->dw_loc_opc
6712 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6713 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6716 /* Do the values look the same? */
6717 static int
6718 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6720 dw_loc_descr_ref loc1, loc2;
6721 rtx r1, r2;
6723 if (v1->val_class != v2->val_class)
6724 return 0;
6726 switch (v1->val_class)
6728 case dw_val_class_const:
6729 return v1->v.val_int == v2->v.val_int;
6730 case dw_val_class_unsigned_const:
6731 return v1->v.val_unsigned == v2->v.val_unsigned;
6732 case dw_val_class_const_double:
6733 return v1->v.val_double.high == v2->v.val_double.high
6734 && v1->v.val_double.low == v2->v.val_double.low;
6735 case dw_val_class_wide_int:
6736 return *v1->v.val_wide == *v2->v.val_wide;
6737 case dw_val_class_vec:
6738 if (v1->v.val_vec.length != v2->v.val_vec.length
6739 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6740 return 0;
6741 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6742 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6743 return 0;
6744 return 1;
6745 case dw_val_class_flag:
6746 return v1->v.val_flag == v2->v.val_flag;
6747 case dw_val_class_str:
6748 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6750 case dw_val_class_addr:
6751 r1 = v1->v.val_addr;
6752 r2 = v2->v.val_addr;
6753 if (GET_CODE (r1) != GET_CODE (r2))
6754 return 0;
6755 return !rtx_equal_p (r1, r2);
6757 case dw_val_class_offset:
6758 return v1->v.val_offset == v2->v.val_offset;
6760 case dw_val_class_loc:
6761 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6762 loc1 && loc2;
6763 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6764 if (!same_loc_p (loc1, loc2, mark))
6765 return 0;
6766 return !loc1 && !loc2;
6768 case dw_val_class_die_ref:
6769 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6771 case dw_val_class_fde_ref:
6772 case dw_val_class_vms_delta:
6773 case dw_val_class_lbl_id:
6774 case dw_val_class_lineptr:
6775 case dw_val_class_macptr:
6776 case dw_val_class_high_pc:
6777 return 1;
6779 case dw_val_class_file:
6780 return v1->v.val_file == v2->v.val_file;
6782 case dw_val_class_data8:
6783 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6785 default:
6786 return 1;
6790 /* Do the attributes look the same? */
6792 static int
6793 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6795 if (at1->dw_attr != at2->dw_attr)
6796 return 0;
6798 /* We don't care that this was compiled with a different compiler
6799 snapshot; if the output is the same, that's what matters. */
6800 if (at1->dw_attr == DW_AT_producer)
6801 return 1;
6803 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6806 /* Do the dies look the same? */
6808 static int
6809 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6811 dw_die_ref c1, c2;
6812 dw_attr_node *a1;
6813 unsigned ix;
6815 /* To avoid infinite recursion. */
6816 if (die1->die_mark)
6817 return die1->die_mark == die2->die_mark;
6818 die1->die_mark = die2->die_mark = ++(*mark);
6820 if (die1->die_tag != die2->die_tag)
6821 return 0;
6823 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6824 return 0;
6826 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6827 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6828 return 0;
6830 c1 = die1->die_child;
6831 c2 = die2->die_child;
6832 if (! c1)
6834 if (c2)
6835 return 0;
6837 else
6838 for (;;)
6840 if (!same_die_p (c1, c2, mark))
6841 return 0;
6842 c1 = c1->die_sib;
6843 c2 = c2->die_sib;
6844 if (c1 == die1->die_child)
6846 if (c2 == die2->die_child)
6847 break;
6848 else
6849 return 0;
6853 return 1;
6856 /* Do the dies look the same? Wrapper around same_die_p. */
6858 static int
6859 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6861 int mark = 0;
6862 int ret = same_die_p (die1, die2, &mark);
6864 unmark_all_dies (die1);
6865 unmark_all_dies (die2);
6867 return ret;
6870 /* The prefix to attach to symbols on DIEs in the current comdat debug
6871 info section. */
6872 static const char *comdat_symbol_id;
6874 /* The index of the current symbol within the current comdat CU. */
6875 static unsigned int comdat_symbol_number;
6877 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6878 children, and set comdat_symbol_id accordingly. */
6880 static void
6881 compute_section_prefix (dw_die_ref unit_die)
6883 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6884 const char *base = die_name ? lbasename (die_name) : "anonymous";
6885 char *name = XALLOCAVEC (char, strlen (base) + 64);
6886 char *p;
6887 int i, mark;
6888 unsigned char checksum[16];
6889 struct md5_ctx ctx;
6891 /* Compute the checksum of the DIE, then append part of it as hex digits to
6892 the name filename of the unit. */
6894 md5_init_ctx (&ctx);
6895 mark = 0;
6896 die_checksum (unit_die, &ctx, &mark);
6897 unmark_all_dies (unit_die);
6898 md5_finish_ctx (&ctx, checksum);
6900 sprintf (name, "%s.", base);
6901 clean_symbol_name (name);
6903 p = name + strlen (name);
6904 for (i = 0; i < 4; i++)
6906 sprintf (p, "%.2x", checksum[i]);
6907 p += 2;
6910 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6911 comdat_symbol_number = 0;
6914 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6916 static int
6917 is_type_die (dw_die_ref die)
6919 switch (die->die_tag)
6921 case DW_TAG_array_type:
6922 case DW_TAG_class_type:
6923 case DW_TAG_interface_type:
6924 case DW_TAG_enumeration_type:
6925 case DW_TAG_pointer_type:
6926 case DW_TAG_reference_type:
6927 case DW_TAG_rvalue_reference_type:
6928 case DW_TAG_string_type:
6929 case DW_TAG_structure_type:
6930 case DW_TAG_subroutine_type:
6931 case DW_TAG_union_type:
6932 case DW_TAG_ptr_to_member_type:
6933 case DW_TAG_set_type:
6934 case DW_TAG_subrange_type:
6935 case DW_TAG_base_type:
6936 case DW_TAG_const_type:
6937 case DW_TAG_file_type:
6938 case DW_TAG_packed_type:
6939 case DW_TAG_volatile_type:
6940 case DW_TAG_typedef:
6941 return 1;
6942 default:
6943 return 0;
6947 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6948 Basically, we want to choose the bits that are likely to be shared between
6949 compilations (types) and leave out the bits that are specific to individual
6950 compilations (functions). */
6952 static int
6953 is_comdat_die (dw_die_ref c)
6955 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6956 we do for stabs. The advantage is a greater likelihood of sharing between
6957 objects that don't include headers in the same order (and therefore would
6958 put the base types in a different comdat). jason 8/28/00 */
6960 if (c->die_tag == DW_TAG_base_type)
6961 return 0;
6963 if (c->die_tag == DW_TAG_pointer_type
6964 || c->die_tag == DW_TAG_reference_type
6965 || c->die_tag == DW_TAG_rvalue_reference_type
6966 || c->die_tag == DW_TAG_const_type
6967 || c->die_tag == DW_TAG_volatile_type)
6969 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6971 return t ? is_comdat_die (t) : 0;
6974 return is_type_die (c);
6977 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6978 compilation unit. */
6980 static int
6981 is_symbol_die (dw_die_ref c)
6983 return (is_type_die (c)
6984 || is_declaration_die (c)
6985 || c->die_tag == DW_TAG_namespace
6986 || c->die_tag == DW_TAG_module);
6989 /* Returns true iff C is a compile-unit DIE. */
6991 static inline bool
6992 is_cu_die (dw_die_ref c)
6994 return c && c->die_tag == DW_TAG_compile_unit;
6997 /* Returns true iff C is a unit DIE of some sort. */
6999 static inline bool
7000 is_unit_die (dw_die_ref c)
7002 return c && (c->die_tag == DW_TAG_compile_unit
7003 || c->die_tag == DW_TAG_partial_unit
7004 || c->die_tag == DW_TAG_type_unit);
7007 /* Returns true iff C is a namespace DIE. */
7009 static inline bool
7010 is_namespace_die (dw_die_ref c)
7012 return c && c->die_tag == DW_TAG_namespace;
7015 /* Returns true iff C is a class or structure DIE. */
7017 static inline bool
7018 is_class_die (dw_die_ref c)
7020 return c && (c->die_tag == DW_TAG_class_type
7021 || c->die_tag == DW_TAG_structure_type);
7024 /* Return non-zero if this DIE is a template parameter. */
7026 static inline bool
7027 is_template_parameter (dw_die_ref die)
7029 switch (die->die_tag)
7031 case DW_TAG_template_type_param:
7032 case DW_TAG_template_value_param:
7033 case DW_TAG_GNU_template_template_param:
7034 case DW_TAG_GNU_template_parameter_pack:
7035 return true;
7036 default:
7037 return false;
7041 /* Return non-zero if this DIE represents a template instantiation. */
7043 static inline bool
7044 is_template_instantiation (dw_die_ref die)
7046 dw_die_ref c;
7048 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7049 return false;
7050 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7051 return false;
7054 static char *
7055 gen_internal_sym (const char *prefix)
7057 char buf[256];
7059 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7060 return xstrdup (buf);
7063 /* Assign symbols to all worthy DIEs under DIE. */
7065 static void
7066 assign_symbol_names (dw_die_ref die)
7068 dw_die_ref c;
7070 if (is_symbol_die (die) && !die->comdat_type_p)
7072 if (comdat_symbol_id)
7074 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7076 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7077 comdat_symbol_id, comdat_symbol_number++);
7078 die->die_id.die_symbol = xstrdup (p);
7080 else
7081 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7084 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7087 struct cu_hash_table_entry
7089 dw_die_ref cu;
7090 unsigned min_comdat_num, max_comdat_num;
7091 struct cu_hash_table_entry *next;
7094 /* Helpers to manipulate hash table of CUs. */
7096 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7098 typedef die_struct *compare_type;
7099 static inline hashval_t hash (const cu_hash_table_entry *);
7100 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7101 static inline void remove (cu_hash_table_entry *);
7104 inline hashval_t
7105 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7107 return htab_hash_string (entry->cu->die_id.die_symbol);
7110 inline bool
7111 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7112 const die_struct *entry2)
7114 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7117 inline void
7118 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7120 struct cu_hash_table_entry *next;
7122 while (entry)
7124 next = entry->next;
7125 free (entry);
7126 entry = next;
7130 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7132 /* Check whether we have already seen this CU and set up SYM_NUM
7133 accordingly. */
7134 static int
7135 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7137 struct cu_hash_table_entry dummy;
7138 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7140 dummy.max_comdat_num = 0;
7142 slot = htable->find_slot_with_hash (cu,
7143 htab_hash_string (cu->die_id.die_symbol),
7144 INSERT);
7145 entry = *slot;
7147 for (; entry; last = entry, entry = entry->next)
7149 if (same_die_p_wrap (cu, entry->cu))
7150 break;
7153 if (entry)
7155 *sym_num = entry->min_comdat_num;
7156 return 1;
7159 entry = XCNEW (struct cu_hash_table_entry);
7160 entry->cu = cu;
7161 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7162 entry->next = *slot;
7163 *slot = entry;
7165 return 0;
7168 /* Record SYM_NUM to record of CU in HTABLE. */
7169 static void
7170 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7171 unsigned int sym_num)
7173 struct cu_hash_table_entry **slot, *entry;
7175 slot = htable->find_slot_with_hash (cu,
7176 htab_hash_string (cu->die_id.die_symbol),
7177 NO_INSERT);
7178 entry = *slot;
7180 entry->max_comdat_num = sym_num;
7183 /* Traverse the DIE (which is always comp_unit_die), and set up
7184 additional compilation units for each of the include files we see
7185 bracketed by BINCL/EINCL. */
7187 static void
7188 break_out_includes (dw_die_ref die)
7190 dw_die_ref c;
7191 dw_die_ref unit = NULL;
7192 limbo_die_node *node, **pnode;
7194 c = die->die_child;
7195 if (c) do {
7196 dw_die_ref prev = c;
7197 c = c->die_sib;
7198 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7199 || (unit && is_comdat_die (c)))
7201 dw_die_ref next = c->die_sib;
7203 /* This DIE is for a secondary CU; remove it from the main one. */
7204 remove_child_with_prev (c, prev);
7206 if (c->die_tag == DW_TAG_GNU_BINCL)
7207 unit = push_new_compile_unit (unit, c);
7208 else if (c->die_tag == DW_TAG_GNU_EINCL)
7209 unit = pop_compile_unit (unit);
7210 else
7211 add_child_die (unit, c);
7212 c = next;
7213 if (c == die->die_child)
7214 break;
7216 } while (c != die->die_child);
7218 #if 0
7219 /* We can only use this in debugging, since the frontend doesn't check
7220 to make sure that we leave every include file we enter. */
7221 gcc_assert (!unit);
7222 #endif
7224 assign_symbol_names (die);
7225 cu_hash_type cu_hash_table (10);
7226 for (node = limbo_die_list, pnode = &limbo_die_list;
7227 node;
7228 node = node->next)
7230 int is_dupl;
7232 compute_section_prefix (node->die);
7233 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7234 &comdat_symbol_number);
7235 assign_symbol_names (node->die);
7236 if (is_dupl)
7237 *pnode = node->next;
7238 else
7240 pnode = &node->next;
7241 record_comdat_symbol_number (node->die, &cu_hash_table,
7242 comdat_symbol_number);
7247 /* Return non-zero if this DIE is a declaration. */
7249 static int
7250 is_declaration_die (dw_die_ref die)
7252 dw_attr_node *a;
7253 unsigned ix;
7255 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7256 if (a->dw_attr == DW_AT_declaration)
7257 return 1;
7259 return 0;
7262 /* Return non-zero if this DIE is nested inside a subprogram. */
7264 static int
7265 is_nested_in_subprogram (dw_die_ref die)
7267 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7269 if (decl == NULL)
7270 decl = die;
7271 return local_scope_p (decl);
7274 /* Return non-zero if this DIE contains a defining declaration of a
7275 subprogram. */
7277 static int
7278 contains_subprogram_definition (dw_die_ref die)
7280 dw_die_ref c;
7282 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7283 return 1;
7284 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7285 return 0;
7288 /* Return non-zero if this is a type DIE that should be moved to a
7289 COMDAT .debug_types section. */
7291 static int
7292 should_move_die_to_comdat (dw_die_ref die)
7294 switch (die->die_tag)
7296 case DW_TAG_class_type:
7297 case DW_TAG_structure_type:
7298 case DW_TAG_enumeration_type:
7299 case DW_TAG_union_type:
7300 /* Don't move declarations, inlined instances, types nested in a
7301 subprogram, or types that contain subprogram definitions. */
7302 if (is_declaration_die (die)
7303 || get_AT (die, DW_AT_abstract_origin)
7304 || is_nested_in_subprogram (die)
7305 || contains_subprogram_definition (die))
7306 return 0;
7307 return 1;
7308 case DW_TAG_array_type:
7309 case DW_TAG_interface_type:
7310 case DW_TAG_pointer_type:
7311 case DW_TAG_reference_type:
7312 case DW_TAG_rvalue_reference_type:
7313 case DW_TAG_string_type:
7314 case DW_TAG_subroutine_type:
7315 case DW_TAG_ptr_to_member_type:
7316 case DW_TAG_set_type:
7317 case DW_TAG_subrange_type:
7318 case DW_TAG_base_type:
7319 case DW_TAG_const_type:
7320 case DW_TAG_file_type:
7321 case DW_TAG_packed_type:
7322 case DW_TAG_volatile_type:
7323 case DW_TAG_typedef:
7324 default:
7325 return 0;
7329 /* Make a clone of DIE. */
7331 static dw_die_ref
7332 clone_die (dw_die_ref die)
7334 dw_die_ref clone;
7335 dw_attr_node *a;
7336 unsigned ix;
7338 clone = ggc_cleared_alloc<die_node> ();
7339 clone->die_tag = die->die_tag;
7341 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7342 add_dwarf_attr (clone, a);
7344 return clone;
7347 /* Make a clone of the tree rooted at DIE. */
7349 static dw_die_ref
7350 clone_tree (dw_die_ref die)
7352 dw_die_ref c;
7353 dw_die_ref clone = clone_die (die);
7355 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7357 return clone;
7360 /* Make a clone of DIE as a declaration. */
7362 static dw_die_ref
7363 clone_as_declaration (dw_die_ref die)
7365 dw_die_ref clone;
7366 dw_die_ref decl;
7367 dw_attr_node *a;
7368 unsigned ix;
7370 /* If the DIE is already a declaration, just clone it. */
7371 if (is_declaration_die (die))
7372 return clone_die (die);
7374 /* If the DIE is a specification, just clone its declaration DIE. */
7375 decl = get_AT_ref (die, DW_AT_specification);
7376 if (decl != NULL)
7378 clone = clone_die (decl);
7379 if (die->comdat_type_p)
7380 add_AT_die_ref (clone, DW_AT_signature, die);
7381 return clone;
7384 clone = ggc_cleared_alloc<die_node> ();
7385 clone->die_tag = die->die_tag;
7387 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7389 /* We don't want to copy over all attributes.
7390 For example we don't want DW_AT_byte_size because otherwise we will no
7391 longer have a declaration and GDB will treat it as a definition. */
7393 switch (a->dw_attr)
7395 case DW_AT_abstract_origin:
7396 case DW_AT_artificial:
7397 case DW_AT_containing_type:
7398 case DW_AT_external:
7399 case DW_AT_name:
7400 case DW_AT_type:
7401 case DW_AT_virtuality:
7402 case DW_AT_linkage_name:
7403 case DW_AT_MIPS_linkage_name:
7404 add_dwarf_attr (clone, a);
7405 break;
7406 case DW_AT_byte_size:
7407 default:
7408 break;
7412 if (die->comdat_type_p)
7413 add_AT_die_ref (clone, DW_AT_signature, die);
7415 add_AT_flag (clone, DW_AT_declaration, 1);
7416 return clone;
7420 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7422 struct decl_table_entry
7424 dw_die_ref orig;
7425 dw_die_ref copy;
7428 /* Helpers to manipulate hash table of copied declarations. */
7430 /* Hashtable helpers. */
7432 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7434 typedef die_struct *compare_type;
7435 static inline hashval_t hash (const decl_table_entry *);
7436 static inline bool equal (const decl_table_entry *, const die_struct *);
7439 inline hashval_t
7440 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7442 return htab_hash_pointer (entry->orig);
7445 inline bool
7446 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7447 const die_struct *entry2)
7449 return entry1->orig == entry2;
7452 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7454 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7455 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7456 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7457 to check if the ancestor has already been copied into UNIT. */
7459 static dw_die_ref
7460 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7461 decl_hash_type *decl_table)
7463 dw_die_ref parent = die->die_parent;
7464 dw_die_ref new_parent = unit;
7465 dw_die_ref copy;
7466 decl_table_entry **slot = NULL;
7467 struct decl_table_entry *entry = NULL;
7469 if (decl_table)
7471 /* Check if the entry has already been copied to UNIT. */
7472 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7473 INSERT);
7474 if (*slot != HTAB_EMPTY_ENTRY)
7476 entry = *slot;
7477 return entry->copy;
7480 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7481 entry = XCNEW (struct decl_table_entry);
7482 entry->orig = die;
7483 entry->copy = NULL;
7484 *slot = entry;
7487 if (parent != NULL)
7489 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7490 if (spec != NULL)
7491 parent = spec;
7492 if (!is_unit_die (parent))
7493 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7496 copy = clone_as_declaration (die);
7497 add_child_die (new_parent, copy);
7499 if (decl_table)
7501 /* Record the pointer to the copy. */
7502 entry->copy = copy;
7505 return copy;
7507 /* Copy the declaration context to the new type unit DIE. This includes
7508 any surrounding namespace or type declarations. If the DIE has an
7509 AT_specification attribute, it also includes attributes and children
7510 attached to the specification, and returns a pointer to the original
7511 parent of the declaration DIE. Returns NULL otherwise. */
7513 static dw_die_ref
7514 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7516 dw_die_ref decl;
7517 dw_die_ref new_decl;
7518 dw_die_ref orig_parent = NULL;
7520 decl = get_AT_ref (die, DW_AT_specification);
7521 if (decl == NULL)
7522 decl = die;
7523 else
7525 unsigned ix;
7526 dw_die_ref c;
7527 dw_attr_node *a;
7529 /* The original DIE will be changed to a declaration, and must
7530 be moved to be a child of the original declaration DIE. */
7531 orig_parent = decl->die_parent;
7533 /* Copy the type node pointer from the new DIE to the original
7534 declaration DIE so we can forward references later. */
7535 decl->comdat_type_p = true;
7536 decl->die_id.die_type_node = die->die_id.die_type_node;
7538 remove_AT (die, DW_AT_specification);
7540 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7542 if (a->dw_attr != DW_AT_name
7543 && a->dw_attr != DW_AT_declaration
7544 && a->dw_attr != DW_AT_external)
7545 add_dwarf_attr (die, a);
7548 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7551 if (decl->die_parent != NULL
7552 && !is_unit_die (decl->die_parent))
7554 new_decl = copy_ancestor_tree (unit, decl, NULL);
7555 if (new_decl != NULL)
7557 remove_AT (new_decl, DW_AT_signature);
7558 add_AT_specification (die, new_decl);
7562 return orig_parent;
7565 /* Generate the skeleton ancestor tree for the given NODE, then clone
7566 the DIE and add the clone into the tree. */
7568 static void
7569 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7571 if (node->new_die != NULL)
7572 return;
7574 node->new_die = clone_as_declaration (node->old_die);
7576 if (node->parent != NULL)
7578 generate_skeleton_ancestor_tree (node->parent);
7579 add_child_die (node->parent->new_die, node->new_die);
7583 /* Generate a skeleton tree of DIEs containing any declarations that are
7584 found in the original tree. We traverse the tree looking for declaration
7585 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7587 static void
7588 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7590 skeleton_chain_node node;
7591 dw_die_ref c;
7592 dw_die_ref first;
7593 dw_die_ref prev = NULL;
7594 dw_die_ref next = NULL;
7596 node.parent = parent;
7598 first = c = parent->old_die->die_child;
7599 if (c)
7600 next = c->die_sib;
7601 if (c) do {
7602 if (prev == NULL || prev->die_sib == c)
7603 prev = c;
7604 c = next;
7605 next = (c == first ? NULL : c->die_sib);
7606 node.old_die = c;
7607 node.new_die = NULL;
7608 if (is_declaration_die (c))
7610 if (is_template_instantiation (c))
7612 /* Instantiated templates do not need to be cloned into the
7613 type unit. Just move the DIE and its children back to
7614 the skeleton tree (in the main CU). */
7615 remove_child_with_prev (c, prev);
7616 add_child_die (parent->new_die, c);
7617 c = prev;
7619 else
7621 /* Clone the existing DIE, move the original to the skeleton
7622 tree (which is in the main CU), and put the clone, with
7623 all the original's children, where the original came from
7624 (which is about to be moved to the type unit). */
7625 dw_die_ref clone = clone_die (c);
7626 move_all_children (c, clone);
7628 /* If the original has a DW_AT_object_pointer attribute,
7629 it would now point to a child DIE just moved to the
7630 cloned tree, so we need to remove that attribute from
7631 the original. */
7632 remove_AT (c, DW_AT_object_pointer);
7634 replace_child (c, clone, prev);
7635 generate_skeleton_ancestor_tree (parent);
7636 add_child_die (parent->new_die, c);
7637 node.new_die = c;
7638 c = clone;
7641 generate_skeleton_bottom_up (&node);
7642 } while (next != NULL);
7645 /* Wrapper function for generate_skeleton_bottom_up. */
7647 static dw_die_ref
7648 generate_skeleton (dw_die_ref die)
7650 skeleton_chain_node node;
7652 node.old_die = die;
7653 node.new_die = NULL;
7654 node.parent = NULL;
7656 /* If this type definition is nested inside another type,
7657 and is not an instantiation of a template, always leave
7658 at least a declaration in its place. */
7659 if (die->die_parent != NULL
7660 && is_type_die (die->die_parent)
7661 && !is_template_instantiation (die))
7662 node.new_die = clone_as_declaration (die);
7664 generate_skeleton_bottom_up (&node);
7665 return node.new_die;
7668 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7669 declaration. The original DIE is moved to a new compile unit so that
7670 existing references to it follow it to the new location. If any of the
7671 original DIE's descendants is a declaration, we need to replace the
7672 original DIE with a skeleton tree and move the declarations back into the
7673 skeleton tree. */
7675 static dw_die_ref
7676 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7677 dw_die_ref prev)
7679 dw_die_ref skeleton, orig_parent;
7681 /* Copy the declaration context to the type unit DIE. If the returned
7682 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7683 that DIE. */
7684 orig_parent = copy_declaration_context (unit, child);
7686 skeleton = generate_skeleton (child);
7687 if (skeleton == NULL)
7688 remove_child_with_prev (child, prev);
7689 else
7691 skeleton->comdat_type_p = true;
7692 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7694 /* If the original DIE was a specification, we need to put
7695 the skeleton under the parent DIE of the declaration.
7696 This leaves the original declaration in the tree, but
7697 it will be pruned later since there are no longer any
7698 references to it. */
7699 if (orig_parent != NULL)
7701 remove_child_with_prev (child, prev);
7702 add_child_die (orig_parent, skeleton);
7704 else
7705 replace_child (child, skeleton, prev);
7708 return skeleton;
7711 static void
7712 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7713 comdat_type_node *type_node,
7714 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7716 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7717 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7718 DWARF procedure references in the DW_AT_location attribute. */
7720 static dw_die_ref
7721 copy_dwarf_procedure (dw_die_ref die,
7722 comdat_type_node *type_node,
7723 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7725 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7727 /* DWARF procedures are not supposed to have children... */
7728 gcc_assert (die->die_child == NULL);
7730 /* ... and they are supposed to have only one attribute: DW_AT_location. */
7731 gcc_assert (vec_safe_length (die->die_attr) == 1
7732 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7734 /* Do not copy more than once DWARF procedures. */
7735 bool existed;
7736 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7737 if (existed)
7738 return die_copy;
7740 die_copy = clone_die (die);
7741 add_child_die (type_node->root_die, die_copy);
7742 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7743 return die_copy;
7746 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
7747 procedures in DIE's attributes. */
7749 static void
7750 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7751 comdat_type_node *type_node,
7752 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7754 dw_attr_node *a;
7755 unsigned i;
7757 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7759 dw_loc_descr_ref loc;
7761 if (a->dw_attr_val.val_class != dw_val_class_loc)
7762 continue;
7764 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7766 switch (loc->dw_loc_opc)
7768 case DW_OP_call2:
7769 case DW_OP_call4:
7770 case DW_OP_call_ref:
7771 gcc_assert (loc->dw_loc_oprnd1.val_class
7772 == dw_val_class_die_ref);
7773 loc->dw_loc_oprnd1.v.val_die_ref.die
7774 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7775 type_node,
7776 copied_dwarf_procs);
7778 default:
7779 break;
7785 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7786 rewrite references to point to the copies.
7788 References are looked for in DIE's attributes and recursively in all its
7789 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7790 mapping from old DWARF procedures to their copy. It is used not to copy
7791 twice the same DWARF procedure under TYPE_NODE. */
7793 static void
7794 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7795 comdat_type_node *type_node,
7796 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7798 dw_die_ref c;
7800 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7801 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7802 type_node,
7803 copied_dwarf_procs));
7806 /* Traverse the DIE and set up additional .debug_types sections for each
7807 type worthy of being placed in a COMDAT section. */
7809 static void
7810 break_out_comdat_types (dw_die_ref die)
7812 dw_die_ref c;
7813 dw_die_ref first;
7814 dw_die_ref prev = NULL;
7815 dw_die_ref next = NULL;
7816 dw_die_ref unit = NULL;
7818 first = c = die->die_child;
7819 if (c)
7820 next = c->die_sib;
7821 if (c) do {
7822 if (prev == NULL || prev->die_sib == c)
7823 prev = c;
7824 c = next;
7825 next = (c == first ? NULL : c->die_sib);
7826 if (should_move_die_to_comdat (c))
7828 dw_die_ref replacement;
7829 comdat_type_node *type_node;
7831 /* Break out nested types into their own type units. */
7832 break_out_comdat_types (c);
7834 /* Create a new type unit DIE as the root for the new tree, and
7835 add it to the list of comdat types. */
7836 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7837 add_AT_unsigned (unit, DW_AT_language,
7838 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7839 type_node = ggc_cleared_alloc<comdat_type_node> ();
7840 type_node->root_die = unit;
7841 type_node->next = comdat_type_list;
7842 comdat_type_list = type_node;
7844 /* Generate the type signature. */
7845 generate_type_signature (c, type_node);
7847 /* Copy the declaration context, attributes, and children of the
7848 declaration into the new type unit DIE, then remove this DIE
7849 from the main CU (or replace it with a skeleton if necessary). */
7850 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7851 type_node->skeleton_die = replacement;
7853 /* Add the DIE to the new compunit. */
7854 add_child_die (unit, c);
7856 /* Types can reference DWARF procedures for type size or data location
7857 expressions. Calls in DWARF expressions cannot target procedures
7858 that are not in the same section. So we must copy DWARF procedures
7859 along with this type and then rewrite references to them. */
7860 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7861 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7863 if (replacement != NULL)
7864 c = replacement;
7866 else if (c->die_tag == DW_TAG_namespace
7867 || c->die_tag == DW_TAG_class_type
7868 || c->die_tag == DW_TAG_structure_type
7869 || c->die_tag == DW_TAG_union_type)
7871 /* Look for nested types that can be broken out. */
7872 break_out_comdat_types (c);
7874 } while (next != NULL);
7877 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7878 Enter all the cloned children into the hash table decl_table. */
7880 static dw_die_ref
7881 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7883 dw_die_ref c;
7884 dw_die_ref clone;
7885 struct decl_table_entry *entry;
7886 decl_table_entry **slot;
7888 if (die->die_tag == DW_TAG_subprogram)
7889 clone = clone_as_declaration (die);
7890 else
7891 clone = clone_die (die);
7893 slot = decl_table->find_slot_with_hash (die,
7894 htab_hash_pointer (die), INSERT);
7896 /* Assert that DIE isn't in the hash table yet. If it would be there
7897 before, the ancestors would be necessarily there as well, therefore
7898 clone_tree_partial wouldn't be called. */
7899 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7901 entry = XCNEW (struct decl_table_entry);
7902 entry->orig = die;
7903 entry->copy = clone;
7904 *slot = entry;
7906 if (die->die_tag != DW_TAG_subprogram)
7907 FOR_EACH_CHILD (die, c,
7908 add_child_die (clone, clone_tree_partial (c, decl_table)));
7910 return clone;
7913 /* Walk the DIE and its children, looking for references to incomplete
7914 or trivial types that are unmarked (i.e., that are not in the current
7915 type_unit). */
7917 static void
7918 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7920 dw_die_ref c;
7921 dw_attr_node *a;
7922 unsigned ix;
7924 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7926 if (AT_class (a) == dw_val_class_die_ref)
7928 dw_die_ref targ = AT_ref (a);
7929 decl_table_entry **slot;
7930 struct decl_table_entry *entry;
7932 if (targ->die_mark != 0 || targ->comdat_type_p)
7933 continue;
7935 slot = decl_table->find_slot_with_hash (targ,
7936 htab_hash_pointer (targ),
7937 INSERT);
7939 if (*slot != HTAB_EMPTY_ENTRY)
7941 /* TARG has already been copied, so we just need to
7942 modify the reference to point to the copy. */
7943 entry = *slot;
7944 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7946 else
7948 dw_die_ref parent = unit;
7949 dw_die_ref copy = clone_die (targ);
7951 /* Record in DECL_TABLE that TARG has been copied.
7952 Need to do this now, before the recursive call,
7953 because DECL_TABLE may be expanded and SLOT
7954 would no longer be a valid pointer. */
7955 entry = XCNEW (struct decl_table_entry);
7956 entry->orig = targ;
7957 entry->copy = copy;
7958 *slot = entry;
7960 /* If TARG is not a declaration DIE, we need to copy its
7961 children. */
7962 if (!is_declaration_die (targ))
7964 FOR_EACH_CHILD (
7965 targ, c,
7966 add_child_die (copy,
7967 clone_tree_partial (c, decl_table)));
7970 /* Make sure the cloned tree is marked as part of the
7971 type unit. */
7972 mark_dies (copy);
7974 /* If TARG has surrounding context, copy its ancestor tree
7975 into the new type unit. */
7976 if (targ->die_parent != NULL
7977 && !is_unit_die (targ->die_parent))
7978 parent = copy_ancestor_tree (unit, targ->die_parent,
7979 decl_table);
7981 add_child_die (parent, copy);
7982 a->dw_attr_val.v.val_die_ref.die = copy;
7984 /* Make sure the newly-copied DIE is walked. If it was
7985 installed in a previously-added context, it won't
7986 get visited otherwise. */
7987 if (parent != unit)
7989 /* Find the highest point of the newly-added tree,
7990 mark each node along the way, and walk from there. */
7991 parent->die_mark = 1;
7992 while (parent->die_parent
7993 && parent->die_parent->die_mark == 0)
7995 parent = parent->die_parent;
7996 parent->die_mark = 1;
7998 copy_decls_walk (unit, parent, decl_table);
8004 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8007 /* Copy declarations for "unworthy" types into the new comdat section.
8008 Incomplete types, modified types, and certain other types aren't broken
8009 out into comdat sections of their own, so they don't have a signature,
8010 and we need to copy the declaration into the same section so that we
8011 don't have an external reference. */
8013 static void
8014 copy_decls_for_unworthy_types (dw_die_ref unit)
8016 mark_dies (unit);
8017 decl_hash_type decl_table (10);
8018 copy_decls_walk (unit, unit, &decl_table);
8019 unmark_dies (unit);
8022 /* Traverse the DIE and add a sibling attribute if it may have the
8023 effect of speeding up access to siblings. To save some space,
8024 avoid generating sibling attributes for DIE's without children. */
8026 static void
8027 add_sibling_attributes (dw_die_ref die)
8029 dw_die_ref c;
8031 if (! die->die_child)
8032 return;
8034 if (die->die_parent && die != die->die_parent->die_child)
8035 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8037 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8040 /* Output all location lists for the DIE and its children. */
8042 static void
8043 output_location_lists (dw_die_ref die)
8045 dw_die_ref c;
8046 dw_attr_node *a;
8047 unsigned ix;
8049 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8050 if (AT_class (a) == dw_val_class_loc_list)
8051 output_loc_list (AT_loc_list (a));
8053 FOR_EACH_CHILD (die, c, output_location_lists (c));
8056 /* We want to limit the number of external references, because they are
8057 larger than local references: a relocation takes multiple words, and
8058 even a sig8 reference is always eight bytes, whereas a local reference
8059 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8060 So if we encounter multiple external references to the same type DIE, we
8061 make a local typedef stub for it and redirect all references there.
8063 This is the element of the hash table for keeping track of these
8064 references. */
8066 struct external_ref
8068 dw_die_ref type;
8069 dw_die_ref stub;
8070 unsigned n_refs;
8073 /* Hashtable helpers. */
8075 struct external_ref_hasher : free_ptr_hash <external_ref>
8077 static inline hashval_t hash (const external_ref *);
8078 static inline bool equal (const external_ref *, const external_ref *);
8081 inline hashval_t
8082 external_ref_hasher::hash (const external_ref *r)
8084 dw_die_ref die = r->type;
8085 hashval_t h = 0;
8087 /* We can't use the address of the DIE for hashing, because
8088 that will make the order of the stub DIEs non-deterministic. */
8089 if (! die->comdat_type_p)
8090 /* We have a symbol; use it to compute a hash. */
8091 h = htab_hash_string (die->die_id.die_symbol);
8092 else
8094 /* We have a type signature; use a subset of the bits as the hash.
8095 The 8-byte signature is at least as large as hashval_t. */
8096 comdat_type_node *type_node = die->die_id.die_type_node;
8097 memcpy (&h, type_node->signature, sizeof (h));
8099 return h;
8102 inline bool
8103 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8105 return r1->type == r2->type;
8108 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8110 /* Return a pointer to the external_ref for references to DIE. */
8112 static struct external_ref *
8113 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8115 struct external_ref ref, *ref_p;
8116 external_ref **slot;
8118 ref.type = die;
8119 slot = map->find_slot (&ref, INSERT);
8120 if (*slot != HTAB_EMPTY_ENTRY)
8121 return *slot;
8123 ref_p = XCNEW (struct external_ref);
8124 ref_p->type = die;
8125 *slot = ref_p;
8126 return ref_p;
8129 /* Subroutine of optimize_external_refs, below.
8131 If we see a type skeleton, record it as our stub. If we see external
8132 references, remember how many we've seen. */
8134 static void
8135 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8137 dw_die_ref c;
8138 dw_attr_node *a;
8139 unsigned ix;
8140 struct external_ref *ref_p;
8142 if (is_type_die (die)
8143 && (c = get_AT_ref (die, DW_AT_signature)))
8145 /* This is a local skeleton; use it for local references. */
8146 ref_p = lookup_external_ref (map, c);
8147 ref_p->stub = die;
8150 /* Scan the DIE references, and remember any that refer to DIEs from
8151 other CUs (i.e. those which are not marked). */
8152 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8153 if (AT_class (a) == dw_val_class_die_ref
8154 && (c = AT_ref (a))->die_mark == 0
8155 && is_type_die (c))
8157 ref_p = lookup_external_ref (map, c);
8158 ref_p->n_refs++;
8161 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8164 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8165 points to an external_ref, DATA is the CU we're processing. If we don't
8166 already have a local stub, and we have multiple refs, build a stub. */
8169 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8171 struct external_ref *ref_p = *slot;
8173 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8175 /* We have multiple references to this type, so build a small stub.
8176 Both of these forms are a bit dodgy from the perspective of the
8177 DWARF standard, since technically they should have names. */
8178 dw_die_ref cu = data;
8179 dw_die_ref type = ref_p->type;
8180 dw_die_ref stub = NULL;
8182 if (type->comdat_type_p)
8184 /* If we refer to this type via sig8, use AT_signature. */
8185 stub = new_die (type->die_tag, cu, NULL_TREE);
8186 add_AT_die_ref (stub, DW_AT_signature, type);
8188 else
8190 /* Otherwise, use a typedef with no name. */
8191 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8192 add_AT_die_ref (stub, DW_AT_type, type);
8195 stub->die_mark++;
8196 ref_p->stub = stub;
8198 return 1;
8201 /* DIE is a unit; look through all the DIE references to see if there are
8202 any external references to types, and if so, create local stubs for
8203 them which will be applied in build_abbrev_table. This is useful because
8204 references to local DIEs are smaller. */
8206 static external_ref_hash_type *
8207 optimize_external_refs (dw_die_ref die)
8209 external_ref_hash_type *map = new external_ref_hash_type (10);
8210 optimize_external_refs_1 (die, map);
8211 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8212 return map;
8215 /* The format of each DIE (and its attribute value pairs) is encoded in an
8216 abbreviation table. This routine builds the abbreviation table and assigns
8217 a unique abbreviation id for each abbreviation entry. The children of each
8218 die are visited recursively. */
8220 static void
8221 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8223 unsigned long abbrev_id;
8224 unsigned int n_alloc;
8225 dw_die_ref c;
8226 dw_attr_node *a;
8227 unsigned ix;
8229 /* Scan the DIE references, and replace any that refer to
8230 DIEs from other CUs (i.e. those which are not marked) with
8231 the local stubs we built in optimize_external_refs. */
8232 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8233 if (AT_class (a) == dw_val_class_die_ref
8234 && (c = AT_ref (a))->die_mark == 0)
8236 struct external_ref *ref_p;
8237 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8239 ref_p = lookup_external_ref (extern_map, c);
8240 if (ref_p->stub && ref_p->stub != die)
8241 change_AT_die_ref (a, ref_p->stub);
8242 else
8243 /* We aren't changing this reference, so mark it external. */
8244 set_AT_ref_external (a, 1);
8247 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8249 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8250 dw_attr_node *die_a, *abbrev_a;
8251 unsigned ix;
8252 bool ok = true;
8254 if (abbrev->die_tag != die->die_tag)
8255 continue;
8256 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8257 continue;
8259 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8260 continue;
8262 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8264 abbrev_a = &(*abbrev->die_attr)[ix];
8265 if ((abbrev_a->dw_attr != die_a->dw_attr)
8266 || (value_format (abbrev_a) != value_format (die_a)))
8268 ok = false;
8269 break;
8272 if (ok)
8273 break;
8276 if (abbrev_id >= abbrev_die_table_in_use)
8278 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8280 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8281 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8282 n_alloc);
8284 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8285 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8286 abbrev_die_table_allocated = n_alloc;
8289 ++abbrev_die_table_in_use;
8290 abbrev_die_table[abbrev_id] = die;
8293 die->die_abbrev = abbrev_id;
8294 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8297 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8299 static int
8300 constant_size (unsigned HOST_WIDE_INT value)
8302 int log;
8304 if (value == 0)
8305 log = 0;
8306 else
8307 log = floor_log2 (value);
8309 log = log / 8;
8310 log = 1 << (floor_log2 (log) + 1);
8312 return log;
8315 /* Return the size of a DIE as it is represented in the
8316 .debug_info section. */
8318 static unsigned long
8319 size_of_die (dw_die_ref die)
8321 unsigned long size = 0;
8322 dw_attr_node *a;
8323 unsigned ix;
8324 enum dwarf_form form;
8326 size += size_of_uleb128 (die->die_abbrev);
8327 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8329 switch (AT_class (a))
8331 case dw_val_class_addr:
8332 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8334 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8335 size += size_of_uleb128 (AT_index (a));
8337 else
8338 size += DWARF2_ADDR_SIZE;
8339 break;
8340 case dw_val_class_offset:
8341 size += DWARF_OFFSET_SIZE;
8342 break;
8343 case dw_val_class_loc:
8345 unsigned long lsize = size_of_locs (AT_loc (a));
8347 /* Block length. */
8348 if (dwarf_version >= 4)
8349 size += size_of_uleb128 (lsize);
8350 else
8351 size += constant_size (lsize);
8352 size += lsize;
8354 break;
8355 case dw_val_class_loc_list:
8356 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8358 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8359 size += size_of_uleb128 (AT_index (a));
8361 else
8362 size += DWARF_OFFSET_SIZE;
8363 break;
8364 case dw_val_class_range_list:
8365 size += DWARF_OFFSET_SIZE;
8366 break;
8367 case dw_val_class_const:
8368 size += size_of_sleb128 (AT_int (a));
8369 break;
8370 case dw_val_class_unsigned_const:
8372 int csize = constant_size (AT_unsigned (a));
8373 if (dwarf_version == 3
8374 && a->dw_attr == DW_AT_data_member_location
8375 && csize >= 4)
8376 size += size_of_uleb128 (AT_unsigned (a));
8377 else
8378 size += csize;
8380 break;
8381 case dw_val_class_const_double:
8382 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8383 if (HOST_BITS_PER_WIDE_INT >= 64)
8384 size++; /* block */
8385 break;
8386 case dw_val_class_wide_int:
8387 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8388 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8389 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8390 > 64)
8391 size++; /* block */
8392 break;
8393 case dw_val_class_vec:
8394 size += constant_size (a->dw_attr_val.v.val_vec.length
8395 * a->dw_attr_val.v.val_vec.elt_size)
8396 + a->dw_attr_val.v.val_vec.length
8397 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8398 break;
8399 case dw_val_class_flag:
8400 if (dwarf_version >= 4)
8401 /* Currently all add_AT_flag calls pass in 1 as last argument,
8402 so DW_FORM_flag_present can be used. If that ever changes,
8403 we'll need to use DW_FORM_flag and have some optimization
8404 in build_abbrev_table that will change those to
8405 DW_FORM_flag_present if it is set to 1 in all DIEs using
8406 the same abbrev entry. */
8407 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8408 else
8409 size += 1;
8410 break;
8411 case dw_val_class_die_ref:
8412 if (AT_ref_external (a))
8414 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8415 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8416 is sized by target address length, whereas in DWARF3
8417 it's always sized as an offset. */
8418 if (use_debug_types)
8419 size += DWARF_TYPE_SIGNATURE_SIZE;
8420 else if (dwarf_version == 2)
8421 size += DWARF2_ADDR_SIZE;
8422 else
8423 size += DWARF_OFFSET_SIZE;
8425 else
8426 size += DWARF_OFFSET_SIZE;
8427 break;
8428 case dw_val_class_fde_ref:
8429 size += DWARF_OFFSET_SIZE;
8430 break;
8431 case dw_val_class_lbl_id:
8432 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8434 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8435 size += size_of_uleb128 (AT_index (a));
8437 else
8438 size += DWARF2_ADDR_SIZE;
8439 break;
8440 case dw_val_class_lineptr:
8441 case dw_val_class_macptr:
8442 size += DWARF_OFFSET_SIZE;
8443 break;
8444 case dw_val_class_str:
8445 form = AT_string_form (a);
8446 if (form == DW_FORM_strp)
8447 size += DWARF_OFFSET_SIZE;
8448 else if (form == DW_FORM_GNU_str_index)
8449 size += size_of_uleb128 (AT_index (a));
8450 else
8451 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8452 break;
8453 case dw_val_class_file:
8454 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8455 break;
8456 case dw_val_class_data8:
8457 size += 8;
8458 break;
8459 case dw_val_class_vms_delta:
8460 size += DWARF_OFFSET_SIZE;
8461 break;
8462 case dw_val_class_high_pc:
8463 size += DWARF2_ADDR_SIZE;
8464 break;
8465 case dw_val_class_discr_value:
8466 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8467 break;
8468 case dw_val_class_discr_list:
8470 unsigned block_size = size_of_discr_list (AT_discr_list (a));
8472 /* This is a block, so we have the block length and then its
8473 data. */
8474 size += constant_size (block_size) + block_size;
8476 break;
8477 default:
8478 gcc_unreachable ();
8482 return size;
8485 /* Size the debugging information associated with a given DIE. Visits the
8486 DIE's children recursively. Updates the global variable next_die_offset, on
8487 each time through. Uses the current value of next_die_offset to update the
8488 die_offset field in each DIE. */
8490 static void
8491 calc_die_sizes (dw_die_ref die)
8493 dw_die_ref c;
8495 gcc_assert (die->die_offset == 0
8496 || (unsigned long int) die->die_offset == next_die_offset);
8497 die->die_offset = next_die_offset;
8498 next_die_offset += size_of_die (die);
8500 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8502 if (die->die_child != NULL)
8503 /* Count the null byte used to terminate sibling lists. */
8504 next_die_offset += 1;
8507 /* Size just the base type children at the start of the CU.
8508 This is needed because build_abbrev needs to size locs
8509 and sizing of type based stack ops needs to know die_offset
8510 values for the base types. */
8512 static void
8513 calc_base_type_die_sizes (void)
8515 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8516 unsigned int i;
8517 dw_die_ref base_type;
8518 #if ENABLE_ASSERT_CHECKING
8519 dw_die_ref prev = comp_unit_die ()->die_child;
8520 #endif
8522 die_offset += size_of_die (comp_unit_die ());
8523 for (i = 0; base_types.iterate (i, &base_type); i++)
8525 #if ENABLE_ASSERT_CHECKING
8526 gcc_assert (base_type->die_offset == 0
8527 && prev->die_sib == base_type
8528 && base_type->die_child == NULL
8529 && base_type->die_abbrev);
8530 prev = base_type;
8531 #endif
8532 base_type->die_offset = die_offset;
8533 die_offset += size_of_die (base_type);
8537 /* Set the marks for a die and its children. We do this so
8538 that we know whether or not a reference needs to use FORM_ref_addr; only
8539 DIEs in the same CU will be marked. We used to clear out the offset
8540 and use that as the flag, but ran into ordering problems. */
8542 static void
8543 mark_dies (dw_die_ref die)
8545 dw_die_ref c;
8547 gcc_assert (!die->die_mark);
8549 die->die_mark = 1;
8550 FOR_EACH_CHILD (die, c, mark_dies (c));
8553 /* Clear the marks for a die and its children. */
8555 static void
8556 unmark_dies (dw_die_ref die)
8558 dw_die_ref c;
8560 if (! use_debug_types)
8561 gcc_assert (die->die_mark);
8563 die->die_mark = 0;
8564 FOR_EACH_CHILD (die, c, unmark_dies (c));
8567 /* Clear the marks for a die, its children and referred dies. */
8569 static void
8570 unmark_all_dies (dw_die_ref die)
8572 dw_die_ref c;
8573 dw_attr_node *a;
8574 unsigned ix;
8576 if (!die->die_mark)
8577 return;
8578 die->die_mark = 0;
8580 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8582 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8583 if (AT_class (a) == dw_val_class_die_ref)
8584 unmark_all_dies (AT_ref (a));
8587 /* Calculate if the entry should appear in the final output file. It may be
8588 from a pruned a type. */
8590 static bool
8591 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8593 /* By limiting gnu pubnames to definitions only, gold can generate a
8594 gdb index without entries for declarations, which don't include
8595 enough information to be useful. */
8596 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8597 return false;
8599 if (table == pubname_table)
8601 /* Enumerator names are part of the pubname table, but the
8602 parent DW_TAG_enumeration_type die may have been pruned.
8603 Don't output them if that is the case. */
8604 if (p->die->die_tag == DW_TAG_enumerator &&
8605 (p->die->die_parent == NULL
8606 || !p->die->die_parent->die_perennial_p))
8607 return false;
8609 /* Everything else in the pubname table is included. */
8610 return true;
8613 /* The pubtypes table shouldn't include types that have been
8614 pruned. */
8615 return (p->die->die_offset != 0
8616 || !flag_eliminate_unused_debug_types);
8619 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8620 generated for the compilation unit. */
8622 static unsigned long
8623 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8625 unsigned long size;
8626 unsigned i;
8627 pubname_entry *p;
8628 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8630 size = DWARF_PUBNAMES_HEADER_SIZE;
8631 FOR_EACH_VEC_ELT (*names, i, p)
8632 if (include_pubname_in_output (names, p))
8633 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8635 size += DWARF_OFFSET_SIZE;
8636 return size;
8639 /* Return the size of the information in the .debug_aranges section. */
8641 static unsigned long
8642 size_of_aranges (void)
8644 unsigned long size;
8646 size = DWARF_ARANGES_HEADER_SIZE;
8648 /* Count the address/length pair for this compilation unit. */
8649 if (text_section_used)
8650 size += 2 * DWARF2_ADDR_SIZE;
8651 if (cold_text_section_used)
8652 size += 2 * DWARF2_ADDR_SIZE;
8653 if (have_multiple_function_sections)
8655 unsigned fde_idx;
8656 dw_fde_ref fde;
8658 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8660 if (DECL_IGNORED_P (fde->decl))
8661 continue;
8662 if (!fde->in_std_section)
8663 size += 2 * DWARF2_ADDR_SIZE;
8664 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8665 size += 2 * DWARF2_ADDR_SIZE;
8669 /* Count the two zero words used to terminated the address range table. */
8670 size += 2 * DWARF2_ADDR_SIZE;
8671 return size;
8674 /* Select the encoding of an attribute value. */
8676 static enum dwarf_form
8677 value_format (dw_attr_node *a)
8679 switch (AT_class (a))
8681 case dw_val_class_addr:
8682 /* Only very few attributes allow DW_FORM_addr. */
8683 switch (a->dw_attr)
8685 case DW_AT_low_pc:
8686 case DW_AT_high_pc:
8687 case DW_AT_entry_pc:
8688 case DW_AT_trampoline:
8689 return (AT_index (a) == NOT_INDEXED
8690 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8691 default:
8692 break;
8694 switch (DWARF2_ADDR_SIZE)
8696 case 1:
8697 return DW_FORM_data1;
8698 case 2:
8699 return DW_FORM_data2;
8700 case 4:
8701 return DW_FORM_data4;
8702 case 8:
8703 return DW_FORM_data8;
8704 default:
8705 gcc_unreachable ();
8707 case dw_val_class_range_list:
8708 case dw_val_class_loc_list:
8709 if (dwarf_version >= 4)
8710 return DW_FORM_sec_offset;
8711 /* FALLTHRU */
8712 case dw_val_class_vms_delta:
8713 case dw_val_class_offset:
8714 switch (DWARF_OFFSET_SIZE)
8716 case 4:
8717 return DW_FORM_data4;
8718 case 8:
8719 return DW_FORM_data8;
8720 default:
8721 gcc_unreachable ();
8723 case dw_val_class_loc:
8724 if (dwarf_version >= 4)
8725 return DW_FORM_exprloc;
8726 switch (constant_size (size_of_locs (AT_loc (a))))
8728 case 1:
8729 return DW_FORM_block1;
8730 case 2:
8731 return DW_FORM_block2;
8732 case 4:
8733 return DW_FORM_block4;
8734 default:
8735 gcc_unreachable ();
8737 case dw_val_class_const:
8738 return DW_FORM_sdata;
8739 case dw_val_class_unsigned_const:
8740 switch (constant_size (AT_unsigned (a)))
8742 case 1:
8743 return DW_FORM_data1;
8744 case 2:
8745 return DW_FORM_data2;
8746 case 4:
8747 /* In DWARF3 DW_AT_data_member_location with
8748 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8749 constant, so we need to use DW_FORM_udata if we need
8750 a large constant. */
8751 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8752 return DW_FORM_udata;
8753 return DW_FORM_data4;
8754 case 8:
8755 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8756 return DW_FORM_udata;
8757 return DW_FORM_data8;
8758 default:
8759 gcc_unreachable ();
8761 case dw_val_class_const_double:
8762 switch (HOST_BITS_PER_WIDE_INT)
8764 case 8:
8765 return DW_FORM_data2;
8766 case 16:
8767 return DW_FORM_data4;
8768 case 32:
8769 return DW_FORM_data8;
8770 case 64:
8771 default:
8772 return DW_FORM_block1;
8774 case dw_val_class_wide_int:
8775 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8777 case 8:
8778 return DW_FORM_data1;
8779 case 16:
8780 return DW_FORM_data2;
8781 case 32:
8782 return DW_FORM_data4;
8783 case 64:
8784 return DW_FORM_data8;
8785 default:
8786 return DW_FORM_block1;
8788 case dw_val_class_vec:
8789 switch (constant_size (a->dw_attr_val.v.val_vec.length
8790 * a->dw_attr_val.v.val_vec.elt_size))
8792 case 1:
8793 return DW_FORM_block1;
8794 case 2:
8795 return DW_FORM_block2;
8796 case 4:
8797 return DW_FORM_block4;
8798 default:
8799 gcc_unreachable ();
8801 case dw_val_class_flag:
8802 if (dwarf_version >= 4)
8804 /* Currently all add_AT_flag calls pass in 1 as last argument,
8805 so DW_FORM_flag_present can be used. If that ever changes,
8806 we'll need to use DW_FORM_flag and have some optimization
8807 in build_abbrev_table that will change those to
8808 DW_FORM_flag_present if it is set to 1 in all DIEs using
8809 the same abbrev entry. */
8810 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8811 return DW_FORM_flag_present;
8813 return DW_FORM_flag;
8814 case dw_val_class_die_ref:
8815 if (AT_ref_external (a))
8816 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8817 else
8818 return DW_FORM_ref;
8819 case dw_val_class_fde_ref:
8820 return DW_FORM_data;
8821 case dw_val_class_lbl_id:
8822 return (AT_index (a) == NOT_INDEXED
8823 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8824 case dw_val_class_lineptr:
8825 case dw_val_class_macptr:
8826 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8827 case dw_val_class_str:
8828 return AT_string_form (a);
8829 case dw_val_class_file:
8830 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8832 case 1:
8833 return DW_FORM_data1;
8834 case 2:
8835 return DW_FORM_data2;
8836 case 4:
8837 return DW_FORM_data4;
8838 default:
8839 gcc_unreachable ();
8842 case dw_val_class_data8:
8843 return DW_FORM_data8;
8845 case dw_val_class_high_pc:
8846 switch (DWARF2_ADDR_SIZE)
8848 case 1:
8849 return DW_FORM_data1;
8850 case 2:
8851 return DW_FORM_data2;
8852 case 4:
8853 return DW_FORM_data4;
8854 case 8:
8855 return DW_FORM_data8;
8856 default:
8857 gcc_unreachable ();
8860 case dw_val_class_discr_value:
8861 return (a->dw_attr_val.v.val_discr_value.pos
8862 ? DW_FORM_udata
8863 : DW_FORM_sdata);
8864 case dw_val_class_discr_list:
8865 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8867 case 1:
8868 return DW_FORM_block1;
8869 case 2:
8870 return DW_FORM_block2;
8871 case 4:
8872 return DW_FORM_block4;
8873 default:
8874 gcc_unreachable ();
8877 default:
8878 gcc_unreachable ();
8882 /* Output the encoding of an attribute value. */
8884 static void
8885 output_value_format (dw_attr_node *a)
8887 enum dwarf_form form = value_format (a);
8889 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8892 /* Given a die and id, produce the appropriate abbreviations. */
8894 static void
8895 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8897 unsigned ix;
8898 dw_attr_node *a_attr;
8900 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8901 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8902 dwarf_tag_name (abbrev->die_tag));
8904 if (abbrev->die_child != NULL)
8905 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8906 else
8907 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8909 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8911 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8912 dwarf_attr_name (a_attr->dw_attr));
8913 output_value_format (a_attr);
8916 dw2_asm_output_data (1, 0, NULL);
8917 dw2_asm_output_data (1, 0, NULL);
8921 /* Output the .debug_abbrev section which defines the DIE abbreviation
8922 table. */
8924 static void
8925 output_abbrev_section (void)
8927 unsigned long abbrev_id;
8929 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8930 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8932 /* Terminate the table. */
8933 dw2_asm_output_data (1, 0, NULL);
8936 /* Output a symbol we can use to refer to this DIE from another CU. */
8938 static inline void
8939 output_die_symbol (dw_die_ref die)
8941 const char *sym = die->die_id.die_symbol;
8943 gcc_assert (!die->comdat_type_p);
8945 if (sym == 0)
8946 return;
8948 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8949 /* We make these global, not weak; if the target doesn't support
8950 .linkonce, it doesn't support combining the sections, so debugging
8951 will break. */
8952 targetm.asm_out.globalize_label (asm_out_file, sym);
8954 ASM_OUTPUT_LABEL (asm_out_file, sym);
8957 /* Return a new location list, given the begin and end range, and the
8958 expression. */
8960 static inline dw_loc_list_ref
8961 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8962 const char *section)
8964 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8966 retlist->begin = begin;
8967 retlist->begin_entry = NULL;
8968 retlist->end = end;
8969 retlist->expr = expr;
8970 retlist->section = section;
8972 return retlist;
8975 /* Generate a new internal symbol for this location list node, if it
8976 hasn't got one yet. */
8978 static inline void
8979 gen_llsym (dw_loc_list_ref list)
8981 gcc_assert (!list->ll_symbol);
8982 list->ll_symbol = gen_internal_sym ("LLST");
8985 /* Output the location list given to us. */
8987 static void
8988 output_loc_list (dw_loc_list_ref list_head)
8990 dw_loc_list_ref curr = list_head;
8992 if (list_head->emitted)
8993 return;
8994 list_head->emitted = true;
8996 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8998 /* Walk the location list, and output each range + expression. */
8999 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9001 unsigned long size;
9002 /* Don't output an entry that starts and ends at the same address. */
9003 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9004 continue;
9005 size = size_of_locs (curr->expr);
9006 /* If the expression is too large, drop it on the floor. We could
9007 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9008 in the expression, but >= 64KB expressions for a single value
9009 in a single range are unlikely very useful. */
9010 if (size > 0xffff)
9011 continue;
9012 if (dwarf_split_debug_info)
9014 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9015 "Location list start/length entry (%s)",
9016 list_head->ll_symbol);
9017 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9018 "Location list range start index (%s)",
9019 curr->begin);
9020 /* The length field is 4 bytes. If we ever need to support
9021 an 8-byte length, we can add a new DW_LLE code or fall back
9022 to DW_LLE_GNU_start_end_entry. */
9023 dw2_asm_output_delta (4, curr->end, curr->begin,
9024 "Location list range length (%s)",
9025 list_head->ll_symbol);
9027 else if (!have_multiple_function_sections)
9029 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9030 "Location list begin address (%s)",
9031 list_head->ll_symbol);
9032 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9033 "Location list end address (%s)",
9034 list_head->ll_symbol);
9036 else
9038 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9039 "Location list begin address (%s)",
9040 list_head->ll_symbol);
9041 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9042 "Location list end address (%s)",
9043 list_head->ll_symbol);
9046 /* Output the block length for this list of location operations. */
9047 gcc_assert (size <= 0xffff);
9048 dw2_asm_output_data (2, size, "%s", "Location expression size");
9050 output_loc_sequence (curr->expr, -1);
9053 if (dwarf_split_debug_info)
9054 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9055 "Location list terminator (%s)",
9056 list_head->ll_symbol);
9057 else
9059 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9060 "Location list terminator begin (%s)",
9061 list_head->ll_symbol);
9062 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9063 "Location list terminator end (%s)",
9064 list_head->ll_symbol);
9068 /* Output a range_list offset into the debug_range section. Emit a
9069 relocated reference if val_entry is NULL, otherwise, emit an
9070 indirect reference. */
9072 static void
9073 output_range_list_offset (dw_attr_node *a)
9075 const char *name = dwarf_attr_name (a->dw_attr);
9077 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9079 char *p = strchr (ranges_section_label, '\0');
9080 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9081 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9082 debug_ranges_section, "%s", name);
9083 *p = '\0';
9085 else
9086 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9087 "%s (offset from %s)", name, ranges_section_label);
9090 /* Output the offset into the debug_loc section. */
9092 static void
9093 output_loc_list_offset (dw_attr_node *a)
9095 char *sym = AT_loc_list (a)->ll_symbol;
9097 gcc_assert (sym);
9098 if (dwarf_split_debug_info)
9099 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9100 "%s", dwarf_attr_name (a->dw_attr));
9101 else
9102 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9103 "%s", dwarf_attr_name (a->dw_attr));
9106 /* Output an attribute's index or value appropriately. */
9108 static void
9109 output_attr_index_or_value (dw_attr_node *a)
9111 const char *name = dwarf_attr_name (a->dw_attr);
9113 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9115 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9116 return;
9118 switch (AT_class (a))
9120 case dw_val_class_addr:
9121 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9122 break;
9123 case dw_val_class_high_pc:
9124 case dw_val_class_lbl_id:
9125 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9126 break;
9127 case dw_val_class_loc_list:
9128 output_loc_list_offset (a);
9129 break;
9130 default:
9131 gcc_unreachable ();
9135 /* Output a type signature. */
9137 static inline void
9138 output_signature (const char *sig, const char *name)
9140 int i;
9142 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9143 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9146 /* Output a discriminant value. */
9148 static inline void
9149 output_discr_value (dw_discr_value *discr_value, const char *name)
9151 if (discr_value->pos)
9152 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9153 else
9154 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9157 /* Output the DIE and its attributes. Called recursively to generate
9158 the definitions of each child DIE. */
9160 static void
9161 output_die (dw_die_ref die)
9163 dw_attr_node *a;
9164 dw_die_ref c;
9165 unsigned long size;
9166 unsigned ix;
9168 /* If someone in another CU might refer to us, set up a symbol for
9169 them to point to. */
9170 if (! die->comdat_type_p && die->die_id.die_symbol)
9171 output_die_symbol (die);
9173 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9174 (unsigned long)die->die_offset,
9175 dwarf_tag_name (die->die_tag));
9177 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9179 const char *name = dwarf_attr_name (a->dw_attr);
9181 switch (AT_class (a))
9183 case dw_val_class_addr:
9184 output_attr_index_or_value (a);
9185 break;
9187 case dw_val_class_offset:
9188 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9189 "%s", name);
9190 break;
9192 case dw_val_class_range_list:
9193 output_range_list_offset (a);
9194 break;
9196 case dw_val_class_loc:
9197 size = size_of_locs (AT_loc (a));
9199 /* Output the block length for this list of location operations. */
9200 if (dwarf_version >= 4)
9201 dw2_asm_output_data_uleb128 (size, "%s", name);
9202 else
9203 dw2_asm_output_data (constant_size (size), size, "%s", name);
9205 output_loc_sequence (AT_loc (a), -1);
9206 break;
9208 case dw_val_class_const:
9209 /* ??? It would be slightly more efficient to use a scheme like is
9210 used for unsigned constants below, but gdb 4.x does not sign
9211 extend. Gdb 5.x does sign extend. */
9212 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9213 break;
9215 case dw_val_class_unsigned_const:
9217 int csize = constant_size (AT_unsigned (a));
9218 if (dwarf_version == 3
9219 && a->dw_attr == DW_AT_data_member_location
9220 && csize >= 4)
9221 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9222 else
9223 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9225 break;
9227 case dw_val_class_const_double:
9229 unsigned HOST_WIDE_INT first, second;
9231 if (HOST_BITS_PER_WIDE_INT >= 64)
9232 dw2_asm_output_data (1,
9233 HOST_BITS_PER_DOUBLE_INT
9234 / HOST_BITS_PER_CHAR,
9235 NULL);
9237 if (WORDS_BIG_ENDIAN)
9239 first = a->dw_attr_val.v.val_double.high;
9240 second = a->dw_attr_val.v.val_double.low;
9242 else
9244 first = a->dw_attr_val.v.val_double.low;
9245 second = a->dw_attr_val.v.val_double.high;
9248 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9249 first, "%s", name);
9250 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9251 second, NULL);
9253 break;
9255 case dw_val_class_wide_int:
9257 int i;
9258 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9259 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9260 if (len * HOST_BITS_PER_WIDE_INT > 64)
9261 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9262 NULL);
9264 if (WORDS_BIG_ENDIAN)
9265 for (i = len - 1; i >= 0; --i)
9267 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9268 "%s", name);
9269 name = "";
9271 else
9272 for (i = 0; i < len; ++i)
9274 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9275 "%s", name);
9276 name = "";
9279 break;
9281 case dw_val_class_vec:
9283 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9284 unsigned int len = a->dw_attr_val.v.val_vec.length;
9285 unsigned int i;
9286 unsigned char *p;
9288 dw2_asm_output_data (constant_size (len * elt_size),
9289 len * elt_size, "%s", name);
9290 if (elt_size > sizeof (HOST_WIDE_INT))
9292 elt_size /= 2;
9293 len *= 2;
9295 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9296 i < len;
9297 i++, p += elt_size)
9298 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9299 "fp or vector constant word %u", i);
9300 break;
9303 case dw_val_class_flag:
9304 if (dwarf_version >= 4)
9306 /* Currently all add_AT_flag calls pass in 1 as last argument,
9307 so DW_FORM_flag_present can be used. If that ever changes,
9308 we'll need to use DW_FORM_flag and have some optimization
9309 in build_abbrev_table that will change those to
9310 DW_FORM_flag_present if it is set to 1 in all DIEs using
9311 the same abbrev entry. */
9312 gcc_assert (AT_flag (a) == 1);
9313 if (flag_debug_asm)
9314 fprintf (asm_out_file, "\t\t\t%s %s\n",
9315 ASM_COMMENT_START, name);
9316 break;
9318 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9319 break;
9321 case dw_val_class_loc_list:
9322 output_attr_index_or_value (a);
9323 break;
9325 case dw_val_class_die_ref:
9326 if (AT_ref_external (a))
9328 if (AT_ref (a)->comdat_type_p)
9330 comdat_type_node *type_node =
9331 AT_ref (a)->die_id.die_type_node;
9333 gcc_assert (type_node);
9334 output_signature (type_node->signature, name);
9336 else
9338 const char *sym = AT_ref (a)->die_id.die_symbol;
9339 int size;
9341 gcc_assert (sym);
9342 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9343 length, whereas in DWARF3 it's always sized as an
9344 offset. */
9345 if (dwarf_version == 2)
9346 size = DWARF2_ADDR_SIZE;
9347 else
9348 size = DWARF_OFFSET_SIZE;
9349 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9350 name);
9353 else
9355 gcc_assert (AT_ref (a)->die_offset);
9356 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9357 "%s", name);
9359 break;
9361 case dw_val_class_fde_ref:
9363 char l1[20];
9365 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9366 a->dw_attr_val.v.val_fde_index * 2);
9367 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9368 "%s", name);
9370 break;
9372 case dw_val_class_vms_delta:
9373 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9374 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9375 AT_vms_delta2 (a), AT_vms_delta1 (a),
9376 "%s", name);
9377 #else
9378 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9379 AT_vms_delta2 (a), AT_vms_delta1 (a),
9380 "%s", name);
9381 #endif
9382 break;
9384 case dw_val_class_lbl_id:
9385 output_attr_index_or_value (a);
9386 break;
9388 case dw_val_class_lineptr:
9389 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9390 debug_line_section, "%s", name);
9391 break;
9393 case dw_val_class_macptr:
9394 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9395 debug_macinfo_section, "%s", name);
9396 break;
9398 case dw_val_class_str:
9399 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9400 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9401 a->dw_attr_val.v.val_str->label,
9402 debug_str_section,
9403 "%s: \"%s\"", name, AT_string (a));
9404 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9405 dw2_asm_output_data_uleb128 (AT_index (a),
9406 "%s: \"%s\"", name, AT_string (a));
9407 else
9408 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9409 break;
9411 case dw_val_class_file:
9413 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9415 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9416 a->dw_attr_val.v.val_file->filename);
9417 break;
9420 case dw_val_class_data8:
9422 int i;
9424 for (i = 0; i < 8; i++)
9425 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9426 i == 0 ? "%s" : NULL, name);
9427 break;
9430 case dw_val_class_high_pc:
9431 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9432 get_AT_low_pc (die), "DW_AT_high_pc");
9433 break;
9435 case dw_val_class_discr_value:
9436 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9437 break;
9439 case dw_val_class_discr_list:
9441 dw_discr_list_ref list = AT_discr_list (a);
9442 const int size = size_of_discr_list (list);
9444 /* This is a block, so output its length first. */
9445 dw2_asm_output_data (constant_size (size), size,
9446 "%s: block size", name);
9448 for (; list != NULL; list = list->dw_discr_next)
9450 /* One byte for the discriminant value descriptor, and then as
9451 many LEB128 numbers as required. */
9452 if (list->dw_discr_range)
9453 dw2_asm_output_data (1, DW_DSC_range,
9454 "%s: DW_DSC_range", name);
9455 else
9456 dw2_asm_output_data (1, DW_DSC_label,
9457 "%s: DW_DSC_label", name);
9459 output_discr_value (&list->dw_discr_lower_bound, name);
9460 if (list->dw_discr_range)
9461 output_discr_value (&list->dw_discr_upper_bound, name);
9463 break;
9466 default:
9467 gcc_unreachable ();
9471 FOR_EACH_CHILD (die, c, output_die (c));
9473 /* Add null byte to terminate sibling list. */
9474 if (die->die_child != NULL)
9475 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9476 (unsigned long) die->die_offset);
9479 /* Output the compilation unit that appears at the beginning of the
9480 .debug_info section, and precedes the DIE descriptions. */
9482 static void
9483 output_compilation_unit_header (void)
9485 /* We don't support actual DWARFv5 units yet, we just use some
9486 DWARFv5 draft DIE tags in DWARFv4 format. */
9487 int ver = dwarf_version < 5 ? dwarf_version : 4;
9489 if (!XCOFF_DEBUGGING_INFO)
9491 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9492 dw2_asm_output_data (4, 0xffffffff,
9493 "Initial length escape value indicating 64-bit DWARF extension");
9494 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9495 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9496 "Length of Compilation Unit Info");
9499 dw2_asm_output_data (2, ver, "DWARF version number");
9500 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9501 debug_abbrev_section,
9502 "Offset Into Abbrev. Section");
9503 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9506 /* Output the compilation unit DIE and its children. */
9508 static void
9509 output_comp_unit (dw_die_ref die, int output_if_empty)
9511 const char *secname, *oldsym;
9512 char *tmp;
9514 /* Unless we are outputting main CU, we may throw away empty ones. */
9515 if (!output_if_empty && die->die_child == NULL)
9516 return;
9518 /* Even if there are no children of this DIE, we must output the information
9519 about the compilation unit. Otherwise, on an empty translation unit, we
9520 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9521 will then complain when examining the file. First mark all the DIEs in
9522 this CU so we know which get local refs. */
9523 mark_dies (die);
9525 external_ref_hash_type *extern_map = optimize_external_refs (die);
9527 build_abbrev_table (die, extern_map);
9529 delete extern_map;
9531 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9532 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9533 calc_die_sizes (die);
9535 oldsym = die->die_id.die_symbol;
9536 if (oldsym)
9538 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9540 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9541 secname = tmp;
9542 die->die_id.die_symbol = NULL;
9543 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9545 else
9547 switch_to_section (debug_info_section);
9548 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9549 info_section_emitted = true;
9552 /* Output debugging information. */
9553 output_compilation_unit_header ();
9554 output_die (die);
9556 /* Leave the marks on the main CU, so we can check them in
9557 output_pubnames. */
9558 if (oldsym)
9560 unmark_dies (die);
9561 die->die_id.die_symbol = oldsym;
9565 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9566 and .debug_pubtypes. This is configured per-target, but can be
9567 overridden by the -gpubnames or -gno-pubnames options. */
9569 static inline bool
9570 want_pubnames (void)
9572 if (debug_info_level <= DINFO_LEVEL_TERSE)
9573 return false;
9574 if (debug_generate_pub_sections != -1)
9575 return debug_generate_pub_sections;
9576 return targetm.want_debug_pub_sections;
9579 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9581 static void
9582 add_AT_pubnames (dw_die_ref die)
9584 if (want_pubnames ())
9585 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9588 /* Add a string attribute value to a skeleton DIE. */
9590 static inline void
9591 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9592 const char *str)
9594 dw_attr_node attr;
9595 struct indirect_string_node *node;
9597 if (! skeleton_debug_str_hash)
9598 skeleton_debug_str_hash
9599 = hash_table<indirect_string_hasher>::create_ggc (10);
9601 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9602 find_string_form (node);
9603 if (node->form == DW_FORM_GNU_str_index)
9604 node->form = DW_FORM_strp;
9606 attr.dw_attr = attr_kind;
9607 attr.dw_attr_val.val_class = dw_val_class_str;
9608 attr.dw_attr_val.val_entry = NULL;
9609 attr.dw_attr_val.v.val_str = node;
9610 add_dwarf_attr (die, &attr);
9613 /* Helper function to generate top-level dies for skeleton debug_info and
9614 debug_types. */
9616 static void
9617 add_top_level_skeleton_die_attrs (dw_die_ref die)
9619 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9620 const char *comp_dir = comp_dir_string ();
9622 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9623 if (comp_dir != NULL)
9624 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9625 add_AT_pubnames (die);
9626 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9629 /* Output skeleton debug sections that point to the dwo file. */
9631 static void
9632 output_skeleton_debug_sections (dw_die_ref comp_unit)
9634 /* We don't support actual DWARFv5 units yet, we just use some
9635 DWARFv5 draft DIE tags in DWARFv4 format. */
9636 int ver = dwarf_version < 5 ? dwarf_version : 4;
9638 /* These attributes will be found in the full debug_info section. */
9639 remove_AT (comp_unit, DW_AT_producer);
9640 remove_AT (comp_unit, DW_AT_language);
9642 switch_to_section (debug_skeleton_info_section);
9643 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9645 /* Produce the skeleton compilation-unit header. This one differs enough from
9646 a normal CU header that it's better not to call output_compilation_unit
9647 header. */
9648 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9649 dw2_asm_output_data (4, 0xffffffff,
9650 "Initial length escape value indicating 64-bit DWARF extension");
9652 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9653 DWARF_COMPILE_UNIT_HEADER_SIZE
9654 - DWARF_INITIAL_LENGTH_SIZE
9655 + size_of_die (comp_unit),
9656 "Length of Compilation Unit Info");
9657 dw2_asm_output_data (2, ver, "DWARF version number");
9658 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9659 debug_abbrev_section,
9660 "Offset Into Abbrev. Section");
9661 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9663 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9664 output_die (comp_unit);
9666 /* Build the skeleton debug_abbrev section. */
9667 switch_to_section (debug_skeleton_abbrev_section);
9668 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9670 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9672 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9675 /* Output a comdat type unit DIE and its children. */
9677 static void
9678 output_comdat_type_unit (comdat_type_node *node)
9680 const char *secname;
9681 char *tmp;
9682 int i;
9683 #if defined (OBJECT_FORMAT_ELF)
9684 tree comdat_key;
9685 #endif
9687 /* First mark all the DIEs in this CU so we know which get local refs. */
9688 mark_dies (node->root_die);
9690 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9692 build_abbrev_table (node->root_die, extern_map);
9694 delete extern_map;
9695 extern_map = NULL;
9697 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9698 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9699 calc_die_sizes (node->root_die);
9701 #if defined (OBJECT_FORMAT_ELF)
9702 if (!dwarf_split_debug_info)
9703 secname = ".debug_types";
9704 else
9705 secname = ".debug_types.dwo";
9707 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9708 sprintf (tmp, "wt.");
9709 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9710 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9711 comdat_key = get_identifier (tmp);
9712 targetm.asm_out.named_section (secname,
9713 SECTION_DEBUG | SECTION_LINKONCE,
9714 comdat_key);
9715 #else
9716 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9717 sprintf (tmp, ".gnu.linkonce.wt.");
9718 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9719 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9720 secname = tmp;
9721 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9722 #endif
9724 /* Output debugging information. */
9725 output_compilation_unit_header ();
9726 output_signature (node->signature, "Type Signature");
9727 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9728 "Offset to Type DIE");
9729 output_die (node->root_die);
9731 unmark_dies (node->root_die);
9734 /* Return the DWARF2/3 pubname associated with a decl. */
9736 static const char *
9737 dwarf2_name (tree decl, int scope)
9739 if (DECL_NAMELESS (decl))
9740 return NULL;
9741 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9744 /* Add a new entry to .debug_pubnames if appropriate. */
9746 static void
9747 add_pubname_string (const char *str, dw_die_ref die)
9749 pubname_entry e;
9751 e.die = die;
9752 e.name = xstrdup (str);
9753 vec_safe_push (pubname_table, e);
9756 static void
9757 add_pubname (tree decl, dw_die_ref die)
9759 if (!want_pubnames ())
9760 return;
9762 /* Don't add items to the table when we expect that the consumer will have
9763 just read the enclosing die. For example, if the consumer is looking at a
9764 class_member, it will either be inside the class already, or will have just
9765 looked up the class to find the member. Either way, searching the class is
9766 faster than searching the index. */
9767 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9768 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9770 const char *name = dwarf2_name (decl, 1);
9772 if (name)
9773 add_pubname_string (name, die);
9777 /* Add an enumerator to the pubnames section. */
9779 static void
9780 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9782 pubname_entry e;
9784 gcc_assert (scope_name);
9785 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9786 e.die = die;
9787 vec_safe_push (pubname_table, e);
9790 /* Add a new entry to .debug_pubtypes if appropriate. */
9792 static void
9793 add_pubtype (tree decl, dw_die_ref die)
9795 pubname_entry e;
9797 if (!want_pubnames ())
9798 return;
9800 if ((TREE_PUBLIC (decl)
9801 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9802 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9804 tree scope = NULL;
9805 const char *scope_name = "";
9806 const char *sep = is_cxx () ? "::" : ".";
9807 const char *name;
9809 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9810 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9812 scope_name = lang_hooks.dwarf_name (scope, 1);
9813 if (scope_name != NULL && scope_name[0] != '\0')
9814 scope_name = concat (scope_name, sep, NULL);
9815 else
9816 scope_name = "";
9819 if (TYPE_P (decl))
9820 name = type_tag (decl);
9821 else
9822 name = lang_hooks.dwarf_name (decl, 1);
9824 /* If we don't have a name for the type, there's no point in adding
9825 it to the table. */
9826 if (name != NULL && name[0] != '\0')
9828 e.die = die;
9829 e.name = concat (scope_name, name, NULL);
9830 vec_safe_push (pubtype_table, e);
9833 /* Although it might be more consistent to add the pubinfo for the
9834 enumerators as their dies are created, they should only be added if the
9835 enum type meets the criteria above. So rather than re-check the parent
9836 enum type whenever an enumerator die is created, just output them all
9837 here. This isn't protected by the name conditional because anonymous
9838 enums don't have names. */
9839 if (die->die_tag == DW_TAG_enumeration_type)
9841 dw_die_ref c;
9843 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9848 /* Output a single entry in the pubnames table. */
9850 static void
9851 output_pubname (dw_offset die_offset, pubname_entry *entry)
9853 dw_die_ref die = entry->die;
9854 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9856 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9858 if (debug_generate_pub_sections == 2)
9860 /* This logic follows gdb's method for determining the value of the flag
9861 byte. */
9862 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9863 switch (die->die_tag)
9865 case DW_TAG_typedef:
9866 case DW_TAG_base_type:
9867 case DW_TAG_subrange_type:
9868 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9869 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9870 break;
9871 case DW_TAG_enumerator:
9872 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9873 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9874 if (!is_cxx () && !is_java ())
9875 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9876 break;
9877 case DW_TAG_subprogram:
9878 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9879 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9880 if (!is_ada ())
9881 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9882 break;
9883 case DW_TAG_constant:
9884 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9885 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9886 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9887 break;
9888 case DW_TAG_variable:
9889 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9890 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9891 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9892 break;
9893 case DW_TAG_namespace:
9894 case DW_TAG_imported_declaration:
9895 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9896 break;
9897 case DW_TAG_class_type:
9898 case DW_TAG_interface_type:
9899 case DW_TAG_structure_type:
9900 case DW_TAG_union_type:
9901 case DW_TAG_enumeration_type:
9902 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9903 if (!is_cxx () && !is_java ())
9904 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9905 break;
9906 default:
9907 /* An unusual tag. Leave the flag-byte empty. */
9908 break;
9910 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9911 "GDB-index flags");
9914 dw2_asm_output_nstring (entry->name, -1, "external name");
9918 /* Output the public names table used to speed up access to externally
9919 visible names; or the public types table used to find type definitions. */
9921 static void
9922 output_pubnames (vec<pubname_entry, va_gc> *names)
9924 unsigned i;
9925 unsigned long pubnames_length = size_of_pubnames (names);
9926 pubname_entry *pub;
9928 if (!XCOFF_DEBUGGING_INFO)
9930 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9931 dw2_asm_output_data (4, 0xffffffff,
9932 "Initial length escape value indicating 64-bit DWARF extension");
9933 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9934 "Pub Info Length");
9937 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9938 dw2_asm_output_data (2, 2, "DWARF Version");
9940 if (dwarf_split_debug_info)
9941 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9942 debug_skeleton_info_section,
9943 "Offset of Compilation Unit Info");
9944 else
9945 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9946 debug_info_section,
9947 "Offset of Compilation Unit Info");
9948 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9949 "Compilation Unit Length");
9951 FOR_EACH_VEC_ELT (*names, i, pub)
9953 if (include_pubname_in_output (names, pub))
9955 dw_offset die_offset = pub->die->die_offset;
9957 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9958 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9959 gcc_assert (pub->die->die_mark);
9961 /* If we're putting types in their own .debug_types sections,
9962 the .debug_pubtypes table will still point to the compile
9963 unit (not the type unit), so we want to use the offset of
9964 the skeleton DIE (if there is one). */
9965 if (pub->die->comdat_type_p && names == pubtype_table)
9967 comdat_type_node *type_node = pub->die->die_id.die_type_node;
9969 if (type_node != NULL)
9970 die_offset = (type_node->skeleton_die != NULL
9971 ? type_node->skeleton_die->die_offset
9972 : comp_unit_die ()->die_offset);
9975 output_pubname (die_offset, pub);
9979 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9982 /* Output public names and types tables if necessary. */
9984 static void
9985 output_pubtables (void)
9987 if (!want_pubnames () || !info_section_emitted)
9988 return;
9990 switch_to_section (debug_pubnames_section);
9991 output_pubnames (pubname_table);
9992 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9993 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9994 simply won't look for the section. */
9995 switch_to_section (debug_pubtypes_section);
9996 output_pubnames (pubtype_table);
10000 /* Output the information that goes into the .debug_aranges table.
10001 Namely, define the beginning and ending address range of the
10002 text section generated for this compilation unit. */
10004 static void
10005 output_aranges (void)
10007 unsigned i;
10008 unsigned long aranges_length = size_of_aranges ();
10010 if (!XCOFF_DEBUGGING_INFO)
10012 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10013 dw2_asm_output_data (4, 0xffffffff,
10014 "Initial length escape value indicating 64-bit DWARF extension");
10015 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10016 "Length of Address Ranges Info");
10019 /* Version number for aranges is still 2, even up to DWARF5. */
10020 dw2_asm_output_data (2, 2, "DWARF Version");
10021 if (dwarf_split_debug_info)
10022 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10023 debug_skeleton_info_section,
10024 "Offset of Compilation Unit Info");
10025 else
10026 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10027 debug_info_section,
10028 "Offset of Compilation Unit Info");
10029 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10030 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10032 /* We need to align to twice the pointer size here. */
10033 if (DWARF_ARANGES_PAD_SIZE)
10035 /* Pad using a 2 byte words so that padding is correct for any
10036 pointer size. */
10037 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10038 2 * DWARF2_ADDR_SIZE);
10039 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10040 dw2_asm_output_data (2, 0, NULL);
10043 /* It is necessary not to output these entries if the sections were
10044 not used; if the sections were not used, the length will be 0 and
10045 the address may end up as 0 if the section is discarded by ld
10046 --gc-sections, leaving an invalid (0, 0) entry that can be
10047 confused with the terminator. */
10048 if (text_section_used)
10050 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10051 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10052 text_section_label, "Length");
10054 if (cold_text_section_used)
10056 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10057 "Address");
10058 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10059 cold_text_section_label, "Length");
10062 if (have_multiple_function_sections)
10064 unsigned fde_idx;
10065 dw_fde_ref fde;
10067 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10069 if (DECL_IGNORED_P (fde->decl))
10070 continue;
10071 if (!fde->in_std_section)
10073 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10074 "Address");
10075 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10076 fde->dw_fde_begin, "Length");
10078 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10080 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10081 "Address");
10082 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10083 fde->dw_fde_second_begin, "Length");
10088 /* Output the terminator words. */
10089 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10090 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10093 /* Add a new entry to .debug_ranges. Return the offset at which it
10094 was placed. */
10096 static unsigned int
10097 add_ranges_num (int num)
10099 unsigned int in_use = ranges_table_in_use;
10101 if (in_use == ranges_table_allocated)
10103 ranges_table_allocated += RANGES_TABLE_INCREMENT;
10104 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10105 ranges_table_allocated);
10106 memset (ranges_table + ranges_table_in_use, 0,
10107 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10110 ranges_table[in_use].num = num;
10111 ranges_table_in_use = in_use + 1;
10113 return in_use * 2 * DWARF2_ADDR_SIZE;
10116 /* Add a new entry to .debug_ranges corresponding to a block, or a
10117 range terminator if BLOCK is NULL. */
10119 static unsigned int
10120 add_ranges (const_tree block)
10122 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10125 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10126 When using dwarf_split_debug_info, address attributes in dies destined
10127 for the final executable should be direct references--setting the
10128 parameter force_direct ensures this behavior. */
10130 static void
10131 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10132 bool *added, bool force_direct)
10134 unsigned int in_use = ranges_by_label_in_use;
10135 unsigned int offset;
10137 if (in_use == ranges_by_label_allocated)
10139 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10140 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10141 ranges_by_label_allocated);
10142 memset (ranges_by_label + ranges_by_label_in_use, 0,
10143 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10146 ranges_by_label[in_use].begin = begin;
10147 ranges_by_label[in_use].end = end;
10148 ranges_by_label_in_use = in_use + 1;
10150 offset = add_ranges_num (-(int)in_use - 1);
10151 if (!*added)
10153 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10154 *added = true;
10158 static void
10159 output_ranges (void)
10161 unsigned i;
10162 static const char *const start_fmt = "Offset %#x";
10163 const char *fmt = start_fmt;
10165 for (i = 0; i < ranges_table_in_use; i++)
10167 int block_num = ranges_table[i].num;
10169 if (block_num > 0)
10171 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10172 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10174 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10175 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10177 /* If all code is in the text section, then the compilation
10178 unit base address defaults to DW_AT_low_pc, which is the
10179 base of the text section. */
10180 if (!have_multiple_function_sections)
10182 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10183 text_section_label,
10184 fmt, i * 2 * DWARF2_ADDR_SIZE);
10185 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10186 text_section_label, NULL);
10189 /* Otherwise, the compilation unit base address is zero,
10190 which allows us to use absolute addresses, and not worry
10191 about whether the target supports cross-section
10192 arithmetic. */
10193 else
10195 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10196 fmt, i * 2 * DWARF2_ADDR_SIZE);
10197 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10200 fmt = NULL;
10203 /* Negative block_num stands for an index into ranges_by_label. */
10204 else if (block_num < 0)
10206 int lab_idx = - block_num - 1;
10208 if (!have_multiple_function_sections)
10210 gcc_unreachable ();
10211 #if 0
10212 /* If we ever use add_ranges_by_labels () for a single
10213 function section, all we have to do is to take out
10214 the #if 0 above. */
10215 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10216 ranges_by_label[lab_idx].begin,
10217 text_section_label,
10218 fmt, i * 2 * DWARF2_ADDR_SIZE);
10219 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10220 ranges_by_label[lab_idx].end,
10221 text_section_label, NULL);
10222 #endif
10224 else
10226 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10227 ranges_by_label[lab_idx].begin,
10228 fmt, i * 2 * DWARF2_ADDR_SIZE);
10229 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10230 ranges_by_label[lab_idx].end,
10231 NULL);
10234 else
10236 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10237 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10238 fmt = start_fmt;
10243 /* Data structure containing information about input files. */
10244 struct file_info
10246 const char *path; /* Complete file name. */
10247 const char *fname; /* File name part. */
10248 int length; /* Length of entire string. */
10249 struct dwarf_file_data * file_idx; /* Index in input file table. */
10250 int dir_idx; /* Index in directory table. */
10253 /* Data structure containing information about directories with source
10254 files. */
10255 struct dir_info
10257 const char *path; /* Path including directory name. */
10258 int length; /* Path length. */
10259 int prefix; /* Index of directory entry which is a prefix. */
10260 int count; /* Number of files in this directory. */
10261 int dir_idx; /* Index of directory used as base. */
10264 /* Callback function for file_info comparison. We sort by looking at
10265 the directories in the path. */
10267 static int
10268 file_info_cmp (const void *p1, const void *p2)
10270 const struct file_info *const s1 = (const struct file_info *) p1;
10271 const struct file_info *const s2 = (const struct file_info *) p2;
10272 const unsigned char *cp1;
10273 const unsigned char *cp2;
10275 /* Take care of file names without directories. We need to make sure that
10276 we return consistent values to qsort since some will get confused if
10277 we return the same value when identical operands are passed in opposite
10278 orders. So if neither has a directory, return 0 and otherwise return
10279 1 or -1 depending on which one has the directory. */
10280 if ((s1->path == s1->fname || s2->path == s2->fname))
10281 return (s2->path == s2->fname) - (s1->path == s1->fname);
10283 cp1 = (const unsigned char *) s1->path;
10284 cp2 = (const unsigned char *) s2->path;
10286 while (1)
10288 ++cp1;
10289 ++cp2;
10290 /* Reached the end of the first path? If so, handle like above. */
10291 if ((cp1 == (const unsigned char *) s1->fname)
10292 || (cp2 == (const unsigned char *) s2->fname))
10293 return ((cp2 == (const unsigned char *) s2->fname)
10294 - (cp1 == (const unsigned char *) s1->fname));
10296 /* Character of current path component the same? */
10297 else if (*cp1 != *cp2)
10298 return *cp1 - *cp2;
10302 struct file_name_acquire_data
10304 struct file_info *files;
10305 int used_files;
10306 int max_files;
10309 /* Traversal function for the hash table. */
10312 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10314 struct dwarf_file_data *d = *slot;
10315 struct file_info *fi;
10316 const char *f;
10318 gcc_assert (fnad->max_files >= d->emitted_number);
10320 if (! d->emitted_number)
10321 return 1;
10323 gcc_assert (fnad->max_files != fnad->used_files);
10325 fi = fnad->files + fnad->used_files++;
10327 /* Skip all leading "./". */
10328 f = d->filename;
10329 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10330 f += 2;
10332 /* Create a new array entry. */
10333 fi->path = f;
10334 fi->length = strlen (f);
10335 fi->file_idx = d;
10337 /* Search for the file name part. */
10338 f = strrchr (f, DIR_SEPARATOR);
10339 #if defined (DIR_SEPARATOR_2)
10341 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10343 if (g != NULL)
10345 if (f == NULL || f < g)
10346 f = g;
10349 #endif
10351 fi->fname = f == NULL ? fi->path : f + 1;
10352 return 1;
10355 /* Output the directory table and the file name table. We try to minimize
10356 the total amount of memory needed. A heuristic is used to avoid large
10357 slowdowns with many input files. */
10359 static void
10360 output_file_names (void)
10362 struct file_name_acquire_data fnad;
10363 int numfiles;
10364 struct file_info *files;
10365 struct dir_info *dirs;
10366 int *saved;
10367 int *savehere;
10368 int *backmap;
10369 int ndirs;
10370 int idx_offset;
10371 int i;
10373 if (!last_emitted_file)
10375 dw2_asm_output_data (1, 0, "End directory table");
10376 dw2_asm_output_data (1, 0, "End file name table");
10377 return;
10380 numfiles = last_emitted_file->emitted_number;
10382 /* Allocate the various arrays we need. */
10383 files = XALLOCAVEC (struct file_info, numfiles);
10384 dirs = XALLOCAVEC (struct dir_info, numfiles);
10386 fnad.files = files;
10387 fnad.used_files = 0;
10388 fnad.max_files = numfiles;
10389 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10390 gcc_assert (fnad.used_files == fnad.max_files);
10392 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10394 /* Find all the different directories used. */
10395 dirs[0].path = files[0].path;
10396 dirs[0].length = files[0].fname - files[0].path;
10397 dirs[0].prefix = -1;
10398 dirs[0].count = 1;
10399 dirs[0].dir_idx = 0;
10400 files[0].dir_idx = 0;
10401 ndirs = 1;
10403 for (i = 1; i < numfiles; i++)
10404 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10405 && memcmp (dirs[ndirs - 1].path, files[i].path,
10406 dirs[ndirs - 1].length) == 0)
10408 /* Same directory as last entry. */
10409 files[i].dir_idx = ndirs - 1;
10410 ++dirs[ndirs - 1].count;
10412 else
10414 int j;
10416 /* This is a new directory. */
10417 dirs[ndirs].path = files[i].path;
10418 dirs[ndirs].length = files[i].fname - files[i].path;
10419 dirs[ndirs].count = 1;
10420 dirs[ndirs].dir_idx = ndirs;
10421 files[i].dir_idx = ndirs;
10423 /* Search for a prefix. */
10424 dirs[ndirs].prefix = -1;
10425 for (j = 0; j < ndirs; j++)
10426 if (dirs[j].length < dirs[ndirs].length
10427 && dirs[j].length > 1
10428 && (dirs[ndirs].prefix == -1
10429 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10430 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10431 dirs[ndirs].prefix = j;
10433 ++ndirs;
10436 /* Now to the actual work. We have to find a subset of the directories which
10437 allow expressing the file name using references to the directory table
10438 with the least amount of characters. We do not do an exhaustive search
10439 where we would have to check out every combination of every single
10440 possible prefix. Instead we use a heuristic which provides nearly optimal
10441 results in most cases and never is much off. */
10442 saved = XALLOCAVEC (int, ndirs);
10443 savehere = XALLOCAVEC (int, ndirs);
10445 memset (saved, '\0', ndirs * sizeof (saved[0]));
10446 for (i = 0; i < ndirs; i++)
10448 int j;
10449 int total;
10451 /* We can always save some space for the current directory. But this
10452 does not mean it will be enough to justify adding the directory. */
10453 savehere[i] = dirs[i].length;
10454 total = (savehere[i] - saved[i]) * dirs[i].count;
10456 for (j = i + 1; j < ndirs; j++)
10458 savehere[j] = 0;
10459 if (saved[j] < dirs[i].length)
10461 /* Determine whether the dirs[i] path is a prefix of the
10462 dirs[j] path. */
10463 int k;
10465 k = dirs[j].prefix;
10466 while (k != -1 && k != (int) i)
10467 k = dirs[k].prefix;
10469 if (k == (int) i)
10471 /* Yes it is. We can possibly save some memory by
10472 writing the filenames in dirs[j] relative to
10473 dirs[i]. */
10474 savehere[j] = dirs[i].length;
10475 total += (savehere[j] - saved[j]) * dirs[j].count;
10480 /* Check whether we can save enough to justify adding the dirs[i]
10481 directory. */
10482 if (total > dirs[i].length + 1)
10484 /* It's worthwhile adding. */
10485 for (j = i; j < ndirs; j++)
10486 if (savehere[j] > 0)
10488 /* Remember how much we saved for this directory so far. */
10489 saved[j] = savehere[j];
10491 /* Remember the prefix directory. */
10492 dirs[j].dir_idx = i;
10497 /* Emit the directory name table. */
10498 idx_offset = dirs[0].length > 0 ? 1 : 0;
10499 for (i = 1 - idx_offset; i < ndirs; i++)
10500 dw2_asm_output_nstring (dirs[i].path,
10501 dirs[i].length
10502 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10503 "Directory Entry: %#x", i + idx_offset);
10505 dw2_asm_output_data (1, 0, "End directory table");
10507 /* We have to emit them in the order of emitted_number since that's
10508 used in the debug info generation. To do this efficiently we
10509 generate a back-mapping of the indices first. */
10510 backmap = XALLOCAVEC (int, numfiles);
10511 for (i = 0; i < numfiles; i++)
10512 backmap[files[i].file_idx->emitted_number - 1] = i;
10514 /* Now write all the file names. */
10515 for (i = 0; i < numfiles; i++)
10517 int file_idx = backmap[i];
10518 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10520 #ifdef VMS_DEBUGGING_INFO
10521 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10523 /* Setting these fields can lead to debugger miscomparisons,
10524 but VMS Debug requires them to be set correctly. */
10526 int ver;
10527 long long cdt;
10528 long siz;
10529 int maxfilelen = strlen (files[file_idx].path)
10530 + dirs[dir_idx].length
10531 + MAX_VMS_VERSION_LEN + 1;
10532 char *filebuf = XALLOCAVEC (char, maxfilelen);
10534 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10535 snprintf (filebuf, maxfilelen, "%s;%d",
10536 files[file_idx].path + dirs[dir_idx].length, ver);
10538 dw2_asm_output_nstring
10539 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10541 /* Include directory index. */
10542 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10544 /* Modification time. */
10545 dw2_asm_output_data_uleb128
10546 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10547 ? cdt : 0,
10548 NULL);
10550 /* File length in bytes. */
10551 dw2_asm_output_data_uleb128
10552 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10553 ? siz : 0,
10554 NULL);
10555 #else
10556 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10557 "File Entry: %#x", (unsigned) i + 1);
10559 /* Include directory index. */
10560 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10562 /* Modification time. */
10563 dw2_asm_output_data_uleb128 (0, NULL);
10565 /* File length in bytes. */
10566 dw2_asm_output_data_uleb128 (0, NULL);
10567 #endif /* VMS_DEBUGGING_INFO */
10570 dw2_asm_output_data (1, 0, "End file name table");
10574 /* Output one line number table into the .debug_line section. */
10576 static void
10577 output_one_line_info_table (dw_line_info_table *table)
10579 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10580 unsigned int current_line = 1;
10581 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10582 dw_line_info_entry *ent;
10583 size_t i;
10585 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10587 switch (ent->opcode)
10589 case LI_set_address:
10590 /* ??? Unfortunately, we have little choice here currently, and
10591 must always use the most general form. GCC does not know the
10592 address delta itself, so we can't use DW_LNS_advance_pc. Many
10593 ports do have length attributes which will give an upper bound
10594 on the address range. We could perhaps use length attributes
10595 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10596 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10598 /* This can handle any delta. This takes
10599 4+DWARF2_ADDR_SIZE bytes. */
10600 dw2_asm_output_data (1, 0, "set address %s", line_label);
10601 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10602 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10603 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10604 break;
10606 case LI_set_line:
10607 if (ent->val == current_line)
10609 /* We still need to start a new row, so output a copy insn. */
10610 dw2_asm_output_data (1, DW_LNS_copy,
10611 "copy line %u", current_line);
10613 else
10615 int line_offset = ent->val - current_line;
10616 int line_delta = line_offset - DWARF_LINE_BASE;
10618 current_line = ent->val;
10619 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10621 /* This can handle deltas from -10 to 234, using the current
10622 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10623 This takes 1 byte. */
10624 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10625 "line %u", current_line);
10627 else
10629 /* This can handle any delta. This takes at least 4 bytes,
10630 depending on the value being encoded. */
10631 dw2_asm_output_data (1, DW_LNS_advance_line,
10632 "advance to line %u", current_line);
10633 dw2_asm_output_data_sleb128 (line_offset, NULL);
10634 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10637 break;
10639 case LI_set_file:
10640 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10641 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10642 break;
10644 case LI_set_column:
10645 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10646 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10647 break;
10649 case LI_negate_stmt:
10650 current_is_stmt = !current_is_stmt;
10651 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10652 "is_stmt %d", current_is_stmt);
10653 break;
10655 case LI_set_prologue_end:
10656 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10657 "set prologue end");
10658 break;
10660 case LI_set_epilogue_begin:
10661 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10662 "set epilogue begin");
10663 break;
10665 case LI_set_discriminator:
10666 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10667 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10668 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10669 dw2_asm_output_data_uleb128 (ent->val, NULL);
10670 break;
10674 /* Emit debug info for the address of the end of the table. */
10675 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10676 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10677 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10678 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10680 dw2_asm_output_data (1, 0, "end sequence");
10681 dw2_asm_output_data_uleb128 (1, NULL);
10682 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10685 /* Output the source line number correspondence information. This
10686 information goes into the .debug_line section. */
10688 static void
10689 output_line_info (bool prologue_only)
10691 char l1[20], l2[20], p1[20], p2[20];
10692 /* We don't support DWARFv5 line tables yet. */
10693 int ver = dwarf_version < 5 ? dwarf_version : 4;
10694 bool saw_one = false;
10695 int opc;
10697 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10698 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10699 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10700 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10702 if (!XCOFF_DEBUGGING_INFO)
10704 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10705 dw2_asm_output_data (4, 0xffffffff,
10706 "Initial length escape value indicating 64-bit DWARF extension");
10707 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10708 "Length of Source Line Info");
10711 ASM_OUTPUT_LABEL (asm_out_file, l1);
10713 dw2_asm_output_data (2, ver, "DWARF Version");
10714 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10715 ASM_OUTPUT_LABEL (asm_out_file, p1);
10717 /* Define the architecture-dependent minimum instruction length (in bytes).
10718 In this implementation of DWARF, this field is used for information
10719 purposes only. Since GCC generates assembly language, we have no
10720 a priori knowledge of how many instruction bytes are generated for each
10721 source line, and therefore can use only the DW_LNE_set_address and
10722 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10723 this as '1', which is "correct enough" for all architectures,
10724 and don't let the target override. */
10725 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10727 if (ver >= 4)
10728 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10729 "Maximum Operations Per Instruction");
10730 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10731 "Default is_stmt_start flag");
10732 dw2_asm_output_data (1, DWARF_LINE_BASE,
10733 "Line Base Value (Special Opcodes)");
10734 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10735 "Line Range Value (Special Opcodes)");
10736 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10737 "Special Opcode Base");
10739 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10741 int n_op_args;
10742 switch (opc)
10744 case DW_LNS_advance_pc:
10745 case DW_LNS_advance_line:
10746 case DW_LNS_set_file:
10747 case DW_LNS_set_column:
10748 case DW_LNS_fixed_advance_pc:
10749 case DW_LNS_set_isa:
10750 n_op_args = 1;
10751 break;
10752 default:
10753 n_op_args = 0;
10754 break;
10757 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10758 opc, n_op_args);
10761 /* Write out the information about the files we use. */
10762 output_file_names ();
10763 ASM_OUTPUT_LABEL (asm_out_file, p2);
10764 if (prologue_only)
10766 /* Output the marker for the end of the line number info. */
10767 ASM_OUTPUT_LABEL (asm_out_file, l2);
10768 return;
10771 if (separate_line_info)
10773 dw_line_info_table *table;
10774 size_t i;
10776 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10777 if (table->in_use)
10779 output_one_line_info_table (table);
10780 saw_one = true;
10783 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10785 output_one_line_info_table (cold_text_section_line_info);
10786 saw_one = true;
10789 /* ??? Some Darwin linkers crash on a .debug_line section with no
10790 sequences. Further, merely a DW_LNE_end_sequence entry is not
10791 sufficient -- the address column must also be initialized.
10792 Make sure to output at least one set_address/end_sequence pair,
10793 choosing .text since that section is always present. */
10794 if (text_section_line_info->in_use || !saw_one)
10795 output_one_line_info_table (text_section_line_info);
10797 /* Output the marker for the end of the line number info. */
10798 ASM_OUTPUT_LABEL (asm_out_file, l2);
10801 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
10803 static inline bool
10804 need_endianity_attribute_p (bool reverse)
10806 return reverse && (dwarf_version >= 3 || !dwarf_strict);
10809 /* Given a pointer to a tree node for some base type, return a pointer to
10810 a DIE that describes the given type. REVERSE is true if the type is
10811 to be interpreted in the reverse storage order wrt the target order.
10813 This routine must only be called for GCC type nodes that correspond to
10814 Dwarf base (fundamental) types. */
10816 static dw_die_ref
10817 base_type_die (tree type, bool reverse)
10819 dw_die_ref base_type_result;
10820 enum dwarf_type encoding;
10821 bool fpt_used = false;
10822 struct fixed_point_type_info fpt_info;
10823 tree type_bias = NULL_TREE;
10825 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10826 return 0;
10828 /* If this is a subtype that should not be emitted as a subrange type,
10829 use the base type. See subrange_type_for_debug_p. */
10830 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10831 type = TREE_TYPE (type);
10833 switch (TREE_CODE (type))
10835 case INTEGER_TYPE:
10836 if ((dwarf_version >= 4 || !dwarf_strict)
10837 && TYPE_NAME (type)
10838 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10839 && DECL_IS_BUILTIN (TYPE_NAME (type))
10840 && DECL_NAME (TYPE_NAME (type)))
10842 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10843 if (strcmp (name, "char16_t") == 0
10844 || strcmp (name, "char32_t") == 0)
10846 encoding = DW_ATE_UTF;
10847 break;
10850 if ((dwarf_version >= 3 || !dwarf_strict)
10851 && lang_hooks.types.get_fixed_point_type_info)
10853 memset (&fpt_info, 0, sizeof (fpt_info));
10854 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10856 fpt_used = true;
10857 encoding = ((TYPE_UNSIGNED (type))
10858 ? DW_ATE_unsigned_fixed
10859 : DW_ATE_signed_fixed);
10860 break;
10863 if (TYPE_STRING_FLAG (type))
10865 if (TYPE_UNSIGNED (type))
10866 encoding = DW_ATE_unsigned_char;
10867 else
10868 encoding = DW_ATE_signed_char;
10870 else if (TYPE_UNSIGNED (type))
10871 encoding = DW_ATE_unsigned;
10872 else
10873 encoding = DW_ATE_signed;
10875 if (!dwarf_strict
10876 && lang_hooks.types.get_type_bias)
10877 type_bias = lang_hooks.types.get_type_bias (type);
10878 break;
10880 case REAL_TYPE:
10881 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10883 if (dwarf_version >= 3 || !dwarf_strict)
10884 encoding = DW_ATE_decimal_float;
10885 else
10886 encoding = DW_ATE_lo_user;
10888 else
10889 encoding = DW_ATE_float;
10890 break;
10892 case FIXED_POINT_TYPE:
10893 if (!(dwarf_version >= 3 || !dwarf_strict))
10894 encoding = DW_ATE_lo_user;
10895 else if (TYPE_UNSIGNED (type))
10896 encoding = DW_ATE_unsigned_fixed;
10897 else
10898 encoding = DW_ATE_signed_fixed;
10899 break;
10901 /* Dwarf2 doesn't know anything about complex ints, so use
10902 a user defined type for it. */
10903 case COMPLEX_TYPE:
10904 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10905 encoding = DW_ATE_complex_float;
10906 else
10907 encoding = DW_ATE_lo_user;
10908 break;
10910 case BOOLEAN_TYPE:
10911 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10912 encoding = DW_ATE_boolean;
10913 break;
10915 default:
10916 /* No other TREE_CODEs are Dwarf fundamental types. */
10917 gcc_unreachable ();
10920 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10922 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10923 int_size_in_bytes (type));
10924 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10926 if (need_endianity_attribute_p (reverse))
10927 add_AT_unsigned (base_type_result, DW_AT_endianity,
10928 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10930 if (fpt_used)
10932 switch (fpt_info.scale_factor_kind)
10934 case fixed_point_scale_factor_binary:
10935 add_AT_int (base_type_result, DW_AT_binary_scale,
10936 fpt_info.scale_factor.binary);
10937 break;
10939 case fixed_point_scale_factor_decimal:
10940 add_AT_int (base_type_result, DW_AT_decimal_scale,
10941 fpt_info.scale_factor.decimal);
10942 break;
10944 case fixed_point_scale_factor_arbitrary:
10945 /* Arbitrary scale factors cannot be described in standard DWARF,
10946 yet. */
10947 if (!dwarf_strict)
10949 /* Describe the scale factor as a rational constant. */
10950 const dw_die_ref scale_factor
10951 = new_die (DW_TAG_constant, comp_unit_die (), type);
10953 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
10954 fpt_info.scale_factor.arbitrary.numerator);
10955 add_AT_int (scale_factor, DW_AT_GNU_denominator,
10956 fpt_info.scale_factor.arbitrary.denominator);
10958 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
10960 break;
10962 default:
10963 gcc_unreachable ();
10967 if (type_bias)
10968 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
10969 dw_scalar_form_constant
10970 | dw_scalar_form_exprloc
10971 | dw_scalar_form_reference,
10972 NULL);
10974 add_pubtype (type, base_type_result);
10976 return base_type_result;
10979 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10980 named 'auto' in its type: return true for it, false otherwise. */
10982 static inline bool
10983 is_cxx_auto (tree type)
10985 if (is_cxx ())
10987 tree name = TYPE_IDENTIFIER (type);
10988 if (name == get_identifier ("auto")
10989 || name == get_identifier ("decltype(auto)"))
10990 return true;
10992 return false;
10995 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10996 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10998 static inline int
10999 is_base_type (tree type)
11001 switch (TREE_CODE (type))
11003 case ERROR_MARK:
11004 case VOID_TYPE:
11005 case INTEGER_TYPE:
11006 case REAL_TYPE:
11007 case FIXED_POINT_TYPE:
11008 case COMPLEX_TYPE:
11009 case BOOLEAN_TYPE:
11010 case POINTER_BOUNDS_TYPE:
11011 return 1;
11013 case ARRAY_TYPE:
11014 case RECORD_TYPE:
11015 case UNION_TYPE:
11016 case QUAL_UNION_TYPE:
11017 case ENUMERAL_TYPE:
11018 case FUNCTION_TYPE:
11019 case METHOD_TYPE:
11020 case POINTER_TYPE:
11021 case REFERENCE_TYPE:
11022 case NULLPTR_TYPE:
11023 case OFFSET_TYPE:
11024 case LANG_TYPE:
11025 case VECTOR_TYPE:
11026 return 0;
11028 default:
11029 if (is_cxx_auto (type))
11030 return 0;
11031 gcc_unreachable ();
11034 return 0;
11037 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11038 node, return the size in bits for the type if it is a constant, or else
11039 return the alignment for the type if the type's size is not constant, or
11040 else return BITS_PER_WORD if the type actually turns out to be an
11041 ERROR_MARK node. */
11043 static inline unsigned HOST_WIDE_INT
11044 simple_type_size_in_bits (const_tree type)
11046 if (TREE_CODE (type) == ERROR_MARK)
11047 return BITS_PER_WORD;
11048 else if (TYPE_SIZE (type) == NULL_TREE)
11049 return 0;
11050 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11051 return tree_to_uhwi (TYPE_SIZE (type));
11052 else
11053 return TYPE_ALIGN (type);
11056 /* Similarly, but return an offset_int instead of UHWI. */
11058 static inline offset_int
11059 offset_int_type_size_in_bits (const_tree type)
11061 if (TREE_CODE (type) == ERROR_MARK)
11062 return BITS_PER_WORD;
11063 else if (TYPE_SIZE (type) == NULL_TREE)
11064 return 0;
11065 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11066 return wi::to_offset (TYPE_SIZE (type));
11067 else
11068 return TYPE_ALIGN (type);
11071 /* Given a pointer to a tree node for a subrange type, return a pointer
11072 to a DIE that describes the given type. */
11074 static dw_die_ref
11075 subrange_type_die (tree type, tree low, tree high, tree bias,
11076 dw_die_ref context_die)
11078 dw_die_ref subrange_die;
11079 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11081 if (context_die == NULL)
11082 context_die = comp_unit_die ();
11084 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11086 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11088 /* The size of the subrange type and its base type do not match,
11089 so we need to generate a size attribute for the subrange type. */
11090 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11093 if (low)
11094 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11095 if (high)
11096 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11097 if (bias && !dwarf_strict)
11098 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11099 dw_scalar_form_constant
11100 | dw_scalar_form_exprloc
11101 | dw_scalar_form_reference,
11102 NULL);
11104 return subrange_die;
11107 /* Returns the (const and/or volatile) cv_qualifiers associated with
11108 the decl node. This will normally be augmented with the
11109 cv_qualifiers of the underlying type in add_type_attribute. */
11111 static int
11112 decl_quals (const_tree decl)
11114 return ((TREE_READONLY (decl)
11115 /* The C++ front-end correctly marks reference-typed
11116 variables as readonly, but from a language (and debug
11117 info) standpoint they are not const-qualified. */
11118 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
11119 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11120 | (TREE_THIS_VOLATILE (decl)
11121 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11124 /* Determine the TYPE whose qualifiers match the largest strict subset
11125 of the given TYPE_QUALS, and return its qualifiers. Ignore all
11126 qualifiers outside QUAL_MASK. */
11128 static int
11129 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11131 tree t;
11132 int best_rank = 0, best_qual = 0, max_rank;
11134 type_quals &= qual_mask;
11135 max_rank = popcount_hwi (type_quals) - 1;
11137 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11138 t = TYPE_NEXT_VARIANT (t))
11140 int q = TYPE_QUALS (t) & qual_mask;
11142 if ((q & type_quals) == q && q != type_quals
11143 && check_base_type (t, type))
11145 int rank = popcount_hwi (q);
11147 if (rank > best_rank)
11149 best_rank = rank;
11150 best_qual = q;
11155 return best_qual;
11158 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11159 static const dwarf_qual_info_t dwarf_qual_info[] =
11161 { TYPE_QUAL_CONST, DW_TAG_const_type },
11162 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11163 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11164 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11166 static const unsigned int dwarf_qual_info_size
11167 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11169 /* If DIE is a qualified DIE of some base DIE with the same parent,
11170 return the base DIE, otherwise return NULL. Set MASK to the
11171 qualifiers added compared to the returned DIE. */
11173 static dw_die_ref
11174 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11176 unsigned int i;
11177 for (i = 0; i < dwarf_qual_info_size; i++)
11178 if (die->die_tag == dwarf_qual_info[i].t)
11179 break;
11180 if (i == dwarf_qual_info_size)
11181 return NULL;
11182 if (vec_safe_length (die->die_attr) != 1)
11183 return NULL;
11184 dw_die_ref type = get_AT_ref (die, DW_AT_type);
11185 if (type == NULL || type->die_parent != die->die_parent)
11186 return NULL;
11187 *mask |= dwarf_qual_info[i].q;
11188 if (depth)
11190 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11191 if (ret)
11192 return ret;
11194 return type;
11197 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11198 entry that chains the modifiers specified by CV_QUALS in front of the
11199 given type. REVERSE is true if the type is to be interpreted in the
11200 reverse storage order wrt the target order. */
11202 static dw_die_ref
11203 modified_type_die (tree type, int cv_quals, bool reverse,
11204 dw_die_ref context_die)
11206 enum tree_code code = TREE_CODE (type);
11207 dw_die_ref mod_type_die;
11208 dw_die_ref sub_die = NULL;
11209 tree item_type = NULL;
11210 tree qualified_type;
11211 tree name, low, high;
11212 dw_die_ref mod_scope;
11213 /* Only these cv-qualifiers are currently handled. */
11214 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11215 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11217 if (code == ERROR_MARK)
11218 return NULL;
11220 if (lang_hooks.types.get_debug_type)
11222 tree debug_type = lang_hooks.types.get_debug_type (type);
11224 if (debug_type != NULL_TREE && debug_type != type)
11225 return modified_type_die (debug_type, cv_quals, reverse, context_die);
11228 cv_quals &= cv_qual_mask;
11230 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11231 tag modifier (and not an attribute) old consumers won't be able
11232 to handle it. */
11233 if (dwarf_version < 3)
11234 cv_quals &= ~TYPE_QUAL_RESTRICT;
11236 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
11237 if (dwarf_version < 5)
11238 cv_quals &= ~TYPE_QUAL_ATOMIC;
11240 /* See if we already have the appropriately qualified variant of
11241 this type. */
11242 qualified_type = get_qualified_type (type, cv_quals);
11244 if (qualified_type == sizetype
11245 && TYPE_NAME (qualified_type)
11246 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11248 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11250 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11251 && TYPE_PRECISION (t)
11252 == TYPE_PRECISION (qualified_type)
11253 && TYPE_UNSIGNED (t)
11254 == TYPE_UNSIGNED (qualified_type));
11255 qualified_type = t;
11258 /* If we do, then we can just use its DIE, if it exists. */
11259 if (qualified_type)
11261 mod_type_die = lookup_type_die (qualified_type);
11263 /* DW_AT_endianity doesn't come from a qualifier on the type. */
11264 if (mod_type_die
11265 && (!need_endianity_attribute_p (reverse)
11266 || !is_base_type (type)
11267 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11268 return mod_type_die;
11271 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11273 /* Handle C typedef types. */
11274 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11275 && !DECL_ARTIFICIAL (name))
11277 tree dtype = TREE_TYPE (name);
11279 if (qualified_type == dtype)
11281 /* For a named type, use the typedef. */
11282 gen_type_die (qualified_type, context_die);
11283 return lookup_type_die (qualified_type);
11285 else
11287 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11288 dquals &= cv_qual_mask;
11289 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11290 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11291 /* cv-unqualified version of named type. Just use
11292 the unnamed type to which it refers. */
11293 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11294 reverse, context_die);
11295 /* Else cv-qualified version of named type; fall through. */
11299 mod_scope = scope_die_for (type, context_die);
11301 if (cv_quals)
11303 int sub_quals = 0, first_quals = 0;
11304 unsigned i;
11305 dw_die_ref first = NULL, last = NULL;
11307 /* Determine a lesser qualified type that most closely matches
11308 this one. Then generate DW_TAG_* entries for the remaining
11309 qualifiers. */
11310 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11311 cv_qual_mask);
11312 if (sub_quals && use_debug_types)
11314 bool needed = false;
11315 /* If emitting type units, make sure the order of qualifiers
11316 is canonical. Thus, start from unqualified type if
11317 an earlier qualifier is missing in sub_quals, but some later
11318 one is present there. */
11319 for (i = 0; i < dwarf_qual_info_size; i++)
11320 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11321 needed = true;
11322 else if (needed && (dwarf_qual_info[i].q & cv_quals))
11324 sub_quals = 0;
11325 break;
11328 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11329 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11331 /* As not all intermediate qualified DIEs have corresponding
11332 tree types, ensure that qualified DIEs in the same scope
11333 as their DW_AT_type are emitted after their DW_AT_type,
11334 only with other qualified DIEs for the same type possibly
11335 in between them. Determine the range of such qualified
11336 DIEs now (first being the base type, last being corresponding
11337 last qualified DIE for it). */
11338 unsigned int count = 0;
11339 first = qualified_die_p (mod_type_die, &first_quals,
11340 dwarf_qual_info_size);
11341 if (first == NULL)
11342 first = mod_type_die;
11343 gcc_assert ((first_quals & ~sub_quals) == 0);
11344 for (count = 0, last = first;
11345 count < (1U << dwarf_qual_info_size);
11346 count++, last = last->die_sib)
11348 int quals = 0;
11349 if (last == mod_scope->die_child)
11350 break;
11351 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11352 != first)
11353 break;
11357 for (i = 0; i < dwarf_qual_info_size; i++)
11358 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11360 dw_die_ref d;
11361 if (first && first != last)
11363 for (d = first->die_sib; ; d = d->die_sib)
11365 int quals = 0;
11366 qualified_die_p (d, &quals, dwarf_qual_info_size);
11367 if (quals == (first_quals | dwarf_qual_info[i].q))
11368 break;
11369 if (d == last)
11371 d = NULL;
11372 break;
11375 if (d)
11377 mod_type_die = d;
11378 continue;
11381 if (first)
11383 d = ggc_cleared_alloc<die_node> ();
11384 d->die_tag = dwarf_qual_info[i].t;
11385 add_child_die_after (mod_scope, d, last);
11386 last = d;
11388 else
11389 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11390 if (mod_type_die)
11391 add_AT_die_ref (d, DW_AT_type, mod_type_die);
11392 mod_type_die = d;
11393 first_quals |= dwarf_qual_info[i].q;
11396 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11398 dwarf_tag tag = DW_TAG_pointer_type;
11399 if (code == REFERENCE_TYPE)
11401 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11402 tag = DW_TAG_rvalue_reference_type;
11403 else
11404 tag = DW_TAG_reference_type;
11406 mod_type_die = new_die (tag, mod_scope, type);
11408 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11409 simple_type_size_in_bits (type) / BITS_PER_UNIT);
11410 item_type = TREE_TYPE (type);
11412 addr_space_t as = TYPE_ADDR_SPACE (item_type);
11413 if (!ADDR_SPACE_GENERIC_P (as))
11415 int action = targetm.addr_space.debug (as);
11416 if (action >= 0)
11418 /* Positive values indicate an address_class. */
11419 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11421 else
11423 /* Negative values indicate an (inverted) segment base reg. */
11424 dw_loc_descr_ref d
11425 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11426 add_AT_loc (mod_type_die, DW_AT_segment, d);
11430 else if (code == INTEGER_TYPE
11431 && TREE_TYPE (type) != NULL_TREE
11432 && subrange_type_for_debug_p (type, &low, &high))
11434 tree bias = NULL_TREE;
11435 if (lang_hooks.types.get_type_bias)
11436 bias = lang_hooks.types.get_type_bias (type);
11437 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11438 item_type = TREE_TYPE (type);
11440 else if (is_base_type (type))
11441 mod_type_die = base_type_die (type, reverse);
11442 else
11444 gen_type_die (type, context_die);
11446 /* We have to get the type_main_variant here (and pass that to the
11447 `lookup_type_die' routine) because the ..._TYPE node we have
11448 might simply be a *copy* of some original type node (where the
11449 copy was created to help us keep track of typedef names) and
11450 that copy might have a different TYPE_UID from the original
11451 ..._TYPE node. */
11452 if (TREE_CODE (type) != VECTOR_TYPE
11453 && TREE_CODE (type) != ARRAY_TYPE)
11454 return lookup_type_die (type_main_variant (type));
11455 else
11456 /* Vectors have the debugging information in the type,
11457 not the main variant. */
11458 return lookup_type_die (type);
11461 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
11462 don't output a DW_TAG_typedef, since there isn't one in the
11463 user's program; just attach a DW_AT_name to the type.
11464 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11465 if the base type already has the same name. */
11466 if (name
11467 && ((TREE_CODE (name) != TYPE_DECL
11468 && (qualified_type == TYPE_MAIN_VARIANT (type)
11469 || (cv_quals == TYPE_UNQUALIFIED)))
11470 || (TREE_CODE (name) == TYPE_DECL
11471 && TREE_TYPE (name) == qualified_type
11472 && DECL_NAME (name))))
11474 if (TREE_CODE (name) == TYPE_DECL)
11475 /* Could just call add_name_and_src_coords_attributes here,
11476 but since this is a builtin type it doesn't have any
11477 useful source coordinates anyway. */
11478 name = DECL_NAME (name);
11479 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11481 /* This probably indicates a bug. */
11482 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11484 name = TYPE_IDENTIFIER (type);
11485 add_name_attribute (mod_type_die,
11486 name ? IDENTIFIER_POINTER (name) : "__unknown__");
11489 if (qualified_type)
11490 equate_type_number_to_die (qualified_type, mod_type_die);
11492 if (item_type)
11493 /* We must do this after the equate_type_number_to_die call, in case
11494 this is a recursive type. This ensures that the modified_type_die
11495 recursion will terminate even if the type is recursive. Recursive
11496 types are possible in Ada. */
11497 sub_die = modified_type_die (item_type,
11498 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11499 reverse,
11500 context_die);
11502 if (sub_die != NULL)
11503 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11505 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11506 if (TYPE_ARTIFICIAL (type))
11507 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11509 return mod_type_die;
11512 /* Generate DIEs for the generic parameters of T.
11513 T must be either a generic type or a generic function.
11514 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11516 static void
11517 gen_generic_params_dies (tree t)
11519 tree parms, args;
11520 int parms_num, i;
11521 dw_die_ref die = NULL;
11522 int non_default;
11524 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11525 return;
11527 if (TYPE_P (t))
11528 die = lookup_type_die (t);
11529 else if (DECL_P (t))
11530 die = lookup_decl_die (t);
11532 gcc_assert (die);
11534 parms = lang_hooks.get_innermost_generic_parms (t);
11535 if (!parms)
11536 /* T has no generic parameter. It means T is neither a generic type
11537 or function. End of story. */
11538 return;
11540 parms_num = TREE_VEC_LENGTH (parms);
11541 args = lang_hooks.get_innermost_generic_args (t);
11542 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11543 non_default = int_cst_value (TREE_CHAIN (args));
11544 else
11545 non_default = TREE_VEC_LENGTH (args);
11546 for (i = 0; i < parms_num; i++)
11548 tree parm, arg, arg_pack_elems;
11549 dw_die_ref parm_die;
11551 parm = TREE_VEC_ELT (parms, i);
11552 arg = TREE_VEC_ELT (args, i);
11553 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11554 gcc_assert (parm && TREE_VALUE (parm) && arg);
11556 if (parm && TREE_VALUE (parm) && arg)
11558 /* If PARM represents a template parameter pack,
11559 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11560 by DW_TAG_template_*_parameter DIEs for the argument
11561 pack elements of ARG. Note that ARG would then be
11562 an argument pack. */
11563 if (arg_pack_elems)
11564 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11565 arg_pack_elems,
11566 die);
11567 else
11568 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11569 true /* emit name */, die);
11570 if (i >= non_default)
11571 add_AT_flag (parm_die, DW_AT_default_value, 1);
11576 /* Create and return a DIE for PARM which should be
11577 the representation of a generic type parameter.
11578 For instance, in the C++ front end, PARM would be a template parameter.
11579 ARG is the argument to PARM.
11580 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11581 name of the PARM.
11582 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11583 as a child node. */
11585 static dw_die_ref
11586 generic_parameter_die (tree parm, tree arg,
11587 bool emit_name_p,
11588 dw_die_ref parent_die)
11590 dw_die_ref tmpl_die = NULL;
11591 const char *name = NULL;
11593 if (!parm || !DECL_NAME (parm) || !arg)
11594 return NULL;
11596 /* We support non-type generic parameters and arguments,
11597 type generic parameters and arguments, as well as
11598 generic generic parameters (a.k.a. template template parameters in C++)
11599 and arguments. */
11600 if (TREE_CODE (parm) == PARM_DECL)
11601 /* PARM is a nontype generic parameter */
11602 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11603 else if (TREE_CODE (parm) == TYPE_DECL)
11604 /* PARM is a type generic parameter. */
11605 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11606 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11607 /* PARM is a generic generic parameter.
11608 Its DIE is a GNU extension. It shall have a
11609 DW_AT_name attribute to represent the name of the template template
11610 parameter, and a DW_AT_GNU_template_name attribute to represent the
11611 name of the template template argument. */
11612 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11613 parent_die, parm);
11614 else
11615 gcc_unreachable ();
11617 if (tmpl_die)
11619 tree tmpl_type;
11621 /* If PARM is a generic parameter pack, it means we are
11622 emitting debug info for a template argument pack element.
11623 In other terms, ARG is a template argument pack element.
11624 In that case, we don't emit any DW_AT_name attribute for
11625 the die. */
11626 if (emit_name_p)
11628 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11629 gcc_assert (name);
11630 add_AT_string (tmpl_die, DW_AT_name, name);
11633 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11635 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11636 TMPL_DIE should have a child DW_AT_type attribute that is set
11637 to the type of the argument to PARM, which is ARG.
11638 If PARM is a type generic parameter, TMPL_DIE should have a
11639 child DW_AT_type that is set to ARG. */
11640 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11641 add_type_attribute (tmpl_die, tmpl_type,
11642 (TREE_THIS_VOLATILE (tmpl_type)
11643 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11644 false, parent_die);
11646 else
11648 /* So TMPL_DIE is a DIE representing a
11649 a generic generic template parameter, a.k.a template template
11650 parameter in C++ and arg is a template. */
11652 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11653 to the name of the argument. */
11654 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11655 if (name)
11656 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11659 if (TREE_CODE (parm) == PARM_DECL)
11660 /* So PARM is a non-type generic parameter.
11661 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11662 attribute of TMPL_DIE which value represents the value
11663 of ARG.
11664 We must be careful here:
11665 The value of ARG might reference some function decls.
11666 We might currently be emitting debug info for a generic
11667 type and types are emitted before function decls, we don't
11668 know if the function decls referenced by ARG will actually be
11669 emitted after cgraph computations.
11670 So must defer the generation of the DW_AT_const_value to
11671 after cgraph is ready. */
11672 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11675 return tmpl_die;
11678 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11679 PARM_PACK must be a template parameter pack. The returned DIE
11680 will be child DIE of PARENT_DIE. */
11682 static dw_die_ref
11683 template_parameter_pack_die (tree parm_pack,
11684 tree parm_pack_args,
11685 dw_die_ref parent_die)
11687 dw_die_ref die;
11688 int j;
11690 gcc_assert (parent_die && parm_pack);
11692 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11693 add_name_and_src_coords_attributes (die, parm_pack);
11694 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11695 generic_parameter_die (parm_pack,
11696 TREE_VEC_ELT (parm_pack_args, j),
11697 false /* Don't emit DW_AT_name */,
11698 die);
11699 return die;
11702 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11703 an enumerated type. */
11705 static inline int
11706 type_is_enum (const_tree type)
11708 return TREE_CODE (type) == ENUMERAL_TYPE;
11711 /* Return the DBX register number described by a given RTL node. */
11713 static unsigned int
11714 dbx_reg_number (const_rtx rtl)
11716 unsigned regno = REGNO (rtl);
11718 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11720 #ifdef LEAF_REG_REMAP
11721 if (crtl->uses_only_leaf_regs)
11723 int leaf_reg = LEAF_REG_REMAP (regno);
11724 if (leaf_reg != -1)
11725 regno = (unsigned) leaf_reg;
11727 #endif
11729 regno = DBX_REGISTER_NUMBER (regno);
11730 gcc_assert (regno != INVALID_REGNUM);
11731 return regno;
11734 /* Optionally add a DW_OP_piece term to a location description expression.
11735 DW_OP_piece is only added if the location description expression already
11736 doesn't end with DW_OP_piece. */
11738 static void
11739 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11741 dw_loc_descr_ref loc;
11743 if (*list_head != NULL)
11745 /* Find the end of the chain. */
11746 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11749 if (loc->dw_loc_opc != DW_OP_piece)
11750 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11754 /* Return a location descriptor that designates a machine register or
11755 zero if there is none. */
11757 static dw_loc_descr_ref
11758 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11760 rtx regs;
11762 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11763 return 0;
11765 /* We only use "frame base" when we're sure we're talking about the
11766 post-prologue local stack frame. We do this by *not* running
11767 register elimination until this point, and recognizing the special
11768 argument pointer and soft frame pointer rtx's.
11769 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11770 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11771 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11773 dw_loc_descr_ref result = NULL;
11775 if (dwarf_version >= 4 || !dwarf_strict)
11777 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11778 initialized);
11779 if (result)
11780 add_loc_descr (&result,
11781 new_loc_descr (DW_OP_stack_value, 0, 0));
11783 return result;
11786 regs = targetm.dwarf_register_span (rtl);
11788 if (REG_NREGS (rtl) > 1 || regs)
11789 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11790 else
11792 unsigned int dbx_regnum = dbx_reg_number (rtl);
11793 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11794 return 0;
11795 return one_reg_loc_descriptor (dbx_regnum, initialized);
11799 /* Return a location descriptor that designates a machine register for
11800 a given hard register number. */
11802 static dw_loc_descr_ref
11803 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11805 dw_loc_descr_ref reg_loc_descr;
11807 if (regno <= 31)
11808 reg_loc_descr
11809 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11810 else
11811 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11813 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11814 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11816 return reg_loc_descr;
11819 /* Given an RTL of a register, return a location descriptor that
11820 designates a value that spans more than one register. */
11822 static dw_loc_descr_ref
11823 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11824 enum var_init_status initialized)
11826 int size, i;
11827 dw_loc_descr_ref loc_result = NULL;
11829 /* Simple, contiguous registers. */
11830 if (regs == NULL_RTX)
11832 unsigned reg = REGNO (rtl);
11833 int nregs;
11835 #ifdef LEAF_REG_REMAP
11836 if (crtl->uses_only_leaf_regs)
11838 int leaf_reg = LEAF_REG_REMAP (reg);
11839 if (leaf_reg != -1)
11840 reg = (unsigned) leaf_reg;
11842 #endif
11844 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11845 nregs = REG_NREGS (rtl);
11847 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11849 loc_result = NULL;
11850 while (nregs--)
11852 dw_loc_descr_ref t;
11854 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11855 VAR_INIT_STATUS_INITIALIZED);
11856 add_loc_descr (&loc_result, t);
11857 add_loc_descr_op_piece (&loc_result, size);
11858 ++reg;
11860 return loc_result;
11863 /* Now onto stupid register sets in non contiguous locations. */
11865 gcc_assert (GET_CODE (regs) == PARALLEL);
11867 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11868 loc_result = NULL;
11870 for (i = 0; i < XVECLEN (regs, 0); ++i)
11872 dw_loc_descr_ref t;
11874 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11875 VAR_INIT_STATUS_INITIALIZED);
11876 add_loc_descr (&loc_result, t);
11877 add_loc_descr_op_piece (&loc_result, size);
11880 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11881 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11882 return loc_result;
11885 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11887 /* Return a location descriptor that designates a constant i,
11888 as a compound operation from constant (i >> shift), constant shift
11889 and DW_OP_shl. */
11891 static dw_loc_descr_ref
11892 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11894 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11895 add_loc_descr (&ret, int_loc_descriptor (shift));
11896 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11897 return ret;
11900 /* Return a location descriptor that designates a constant. */
11902 static dw_loc_descr_ref
11903 int_loc_descriptor (HOST_WIDE_INT i)
11905 enum dwarf_location_atom op;
11907 /* Pick the smallest representation of a constant, rather than just
11908 defaulting to the LEB encoding. */
11909 if (i >= 0)
11911 int clz = clz_hwi (i);
11912 int ctz = ctz_hwi (i);
11913 if (i <= 31)
11914 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11915 else if (i <= 0xff)
11916 op = DW_OP_const1u;
11917 else if (i <= 0xffff)
11918 op = DW_OP_const2u;
11919 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11920 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11921 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11922 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11923 while DW_OP_const4u is 5 bytes. */
11924 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11925 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11926 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11927 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11928 while DW_OP_const4u is 5 bytes. */
11929 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11930 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11931 op = DW_OP_const4u;
11932 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11933 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11934 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11935 while DW_OP_constu of constant >= 0x100000000 takes at least
11936 6 bytes. */
11937 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11938 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11939 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11940 >= HOST_BITS_PER_WIDE_INT)
11941 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11942 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11943 while DW_OP_constu takes in this case at least 6 bytes. */
11944 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11945 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11946 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11947 && size_of_uleb128 (i) > 6)
11948 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11949 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11950 else
11951 op = DW_OP_constu;
11953 else
11955 if (i >= -0x80)
11956 op = DW_OP_const1s;
11957 else if (i >= -0x8000)
11958 op = DW_OP_const2s;
11959 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11961 if (size_of_int_loc_descriptor (i) < 5)
11963 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11964 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11965 return ret;
11967 op = DW_OP_const4s;
11969 else
11971 if (size_of_int_loc_descriptor (i)
11972 < (unsigned long) 1 + size_of_sleb128 (i))
11974 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11975 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11976 return ret;
11978 op = DW_OP_consts;
11982 return new_loc_descr (op, i, 0);
11985 /* Likewise, for unsigned constants. */
11987 static dw_loc_descr_ref
11988 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
11990 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
11991 const unsigned HOST_WIDE_INT max_uint
11992 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
11994 /* If possible, use the clever signed constants handling. */
11995 if (i <= max_int)
11996 return int_loc_descriptor ((HOST_WIDE_INT) i);
11998 /* Here, we are left with positive numbers that cannot be represented as
11999 HOST_WIDE_INT, i.e.:
12000 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12002 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12003 whereas may be better to output a negative integer: thanks to integer
12004 wrapping, we know that:
12005 x = x - 2 ** DWARF2_ADDR_SIZE
12006 = x - 2 * (max (HOST_WIDE_INT) + 1)
12007 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12008 small negative integers. Let's try that in cases it will clearly improve
12009 the encoding: there is no gain turning DW_OP_const4u into
12010 DW_OP_const4s. */
12011 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12012 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12013 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12015 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12017 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
12018 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
12019 const HOST_WIDE_INT second_shift
12020 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12022 /* So we finally have:
12023 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12024 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
12025 return int_loc_descriptor (second_shift);
12028 /* Last chance: fallback to a simple constant operation. */
12029 return new_loc_descr
12030 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12031 ? DW_OP_const4u
12032 : DW_OP_const8u,
12033 i, 0);
12036 /* Generate and return a location description that computes the unsigned
12037 comparison of the two stack top entries (a OP b where b is the top-most
12038 entry and a is the second one). The KIND of comparison can be LT_EXPR,
12039 LE_EXPR, GT_EXPR or GE_EXPR. */
12041 static dw_loc_descr_ref
12042 uint_comparison_loc_list (enum tree_code kind)
12044 enum dwarf_location_atom op, flip_op;
12045 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12047 switch (kind)
12049 case LT_EXPR:
12050 op = DW_OP_lt;
12051 break;
12052 case LE_EXPR:
12053 op = DW_OP_le;
12054 break;
12055 case GT_EXPR:
12056 op = DW_OP_gt;
12057 break;
12058 case GE_EXPR:
12059 op = DW_OP_ge;
12060 break;
12061 default:
12062 gcc_unreachable ();
12065 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12066 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12068 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
12069 possible to perform unsigned comparisons: we just have to distinguish
12070 three cases:
12072 1. when a and b have the same sign (as signed integers); then we should
12073 return: a OP(signed) b;
12075 2. when a is a negative signed integer while b is a positive one, then a
12076 is a greater unsigned integer than b; likewise when a and b's roles
12077 are flipped.
12079 So first, compare the sign of the two operands. */
12080 ret = new_loc_descr (DW_OP_over, 0, 0);
12081 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12082 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12083 /* If they have different signs (i.e. they have different sign bits), then
12084 the stack top value has now the sign bit set and thus it's smaller than
12085 zero. */
12086 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12087 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12088 add_loc_descr (&ret, bra_node);
12090 /* We are in case 1. At this point, we know both operands have the same
12091 sign, to it's safe to use the built-in signed comparison. */
12092 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12093 add_loc_descr (&ret, jmp_node);
12095 /* We are in case 2. Here, we know both operands do not have the same sign,
12096 so we have to flip the signed comparison. */
12097 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12098 tmp = new_loc_descr (flip_op, 0, 0);
12099 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12100 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12101 add_loc_descr (&ret, tmp);
12103 /* This dummy operation is necessary to make the two branches join. */
12104 tmp = new_loc_descr (DW_OP_nop, 0, 0);
12105 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12106 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12107 add_loc_descr (&ret, tmp);
12109 return ret;
12112 /* Likewise, but takes the location description lists (might be destructive on
12113 them). Return NULL if either is NULL or if concatenation fails. */
12115 static dw_loc_list_ref
12116 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12117 enum tree_code kind)
12119 if (left == NULL || right == NULL)
12120 return NULL;
12122 add_loc_list (&left, right);
12123 if (left == NULL)
12124 return NULL;
12126 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12127 return left;
12130 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12131 without actually allocating it. */
12133 static unsigned long
12134 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12136 return size_of_int_loc_descriptor (i >> shift)
12137 + size_of_int_loc_descriptor (shift)
12138 + 1;
12141 /* Return size_of_locs (int_loc_descriptor (i)) without
12142 actually allocating it. */
12144 static unsigned long
12145 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12147 unsigned long s;
12149 if (i >= 0)
12151 int clz, ctz;
12152 if (i <= 31)
12153 return 1;
12154 else if (i <= 0xff)
12155 return 2;
12156 else if (i <= 0xffff)
12157 return 3;
12158 clz = clz_hwi (i);
12159 ctz = ctz_hwi (i);
12160 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12161 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12162 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12163 - clz - 5);
12164 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12165 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12166 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12167 - clz - 8);
12168 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12169 return 5;
12170 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12171 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12172 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12173 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12174 - clz - 8);
12175 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12176 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12177 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12178 - clz - 16);
12179 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12180 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12181 && s > 6)
12182 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12183 - clz - 32);
12184 else
12185 return 1 + s;
12187 else
12189 if (i >= -0x80)
12190 return 2;
12191 else if (i >= -0x8000)
12192 return 3;
12193 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12195 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12197 s = size_of_int_loc_descriptor (-i) + 1;
12198 if (s < 5)
12199 return s;
12201 return 5;
12203 else
12205 unsigned long r = 1 + size_of_sleb128 (i);
12206 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12208 s = size_of_int_loc_descriptor (-i) + 1;
12209 if (s < r)
12210 return s;
12212 return r;
12217 /* Return loc description representing "address" of integer value.
12218 This can appear only as toplevel expression. */
12220 static dw_loc_descr_ref
12221 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12223 int litsize;
12224 dw_loc_descr_ref loc_result = NULL;
12226 if (!(dwarf_version >= 4 || !dwarf_strict))
12227 return NULL;
12229 litsize = size_of_int_loc_descriptor (i);
12230 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12231 is more compact. For DW_OP_stack_value we need:
12232 litsize + 1 (DW_OP_stack_value)
12233 and for DW_OP_implicit_value:
12234 1 (DW_OP_implicit_value) + 1 (length) + size. */
12235 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12237 loc_result = int_loc_descriptor (i);
12238 add_loc_descr (&loc_result,
12239 new_loc_descr (DW_OP_stack_value, 0, 0));
12240 return loc_result;
12243 loc_result = new_loc_descr (DW_OP_implicit_value,
12244 size, 0);
12245 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12246 loc_result->dw_loc_oprnd2.v.val_int = i;
12247 return loc_result;
12250 /* Return a location descriptor that designates a base+offset location. */
12252 static dw_loc_descr_ref
12253 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12254 enum var_init_status initialized)
12256 unsigned int regno;
12257 dw_loc_descr_ref result;
12258 dw_fde_ref fde = cfun->fde;
12260 /* We only use "frame base" when we're sure we're talking about the
12261 post-prologue local stack frame. We do this by *not* running
12262 register elimination until this point, and recognizing the special
12263 argument pointer and soft frame pointer rtx's. */
12264 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12266 rtx elim = (ira_use_lra_p
12267 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12268 : eliminate_regs (reg, VOIDmode, NULL_RTX));
12270 if (elim != reg)
12272 if (GET_CODE (elim) == PLUS)
12274 offset += INTVAL (XEXP (elim, 1));
12275 elim = XEXP (elim, 0);
12277 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12278 && (elim == hard_frame_pointer_rtx
12279 || elim == stack_pointer_rtx))
12280 || elim == (frame_pointer_needed
12281 ? hard_frame_pointer_rtx
12282 : stack_pointer_rtx));
12284 /* If drap register is used to align stack, use frame
12285 pointer + offset to access stack variables. If stack
12286 is aligned without drap, use stack pointer + offset to
12287 access stack variables. */
12288 if (crtl->stack_realign_tried
12289 && reg == frame_pointer_rtx)
12291 int base_reg
12292 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12293 ? HARD_FRAME_POINTER_REGNUM
12294 : REGNO (elim));
12295 return new_reg_loc_descr (base_reg, offset);
12298 gcc_assert (frame_pointer_fb_offset_valid);
12299 offset += frame_pointer_fb_offset;
12300 return new_loc_descr (DW_OP_fbreg, offset, 0);
12304 regno = REGNO (reg);
12305 #ifdef LEAF_REG_REMAP
12306 if (crtl->uses_only_leaf_regs)
12308 int leaf_reg = LEAF_REG_REMAP (regno);
12309 if (leaf_reg != -1)
12310 regno = (unsigned) leaf_reg;
12312 #endif
12313 regno = DWARF_FRAME_REGNUM (regno);
12315 if (!optimize && fde
12316 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12318 /* Use cfa+offset to represent the location of arguments passed
12319 on the stack when drap is used to align stack.
12320 Only do this when not optimizing, for optimized code var-tracking
12321 is supposed to track where the arguments live and the register
12322 used as vdrap or drap in some spot might be used for something
12323 else in other part of the routine. */
12324 return new_loc_descr (DW_OP_fbreg, offset, 0);
12327 if (regno <= 31)
12328 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12329 offset, 0);
12330 else
12331 result = new_loc_descr (DW_OP_bregx, regno, offset);
12333 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12334 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12336 return result;
12339 /* Return true if this RTL expression describes a base+offset calculation. */
12341 static inline int
12342 is_based_loc (const_rtx rtl)
12344 return (GET_CODE (rtl) == PLUS
12345 && ((REG_P (XEXP (rtl, 0))
12346 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12347 && CONST_INT_P (XEXP (rtl, 1)))));
12350 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12351 failed. */
12353 static dw_loc_descr_ref
12354 tls_mem_loc_descriptor (rtx mem)
12356 tree base;
12357 dw_loc_descr_ref loc_result;
12359 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12360 return NULL;
12362 base = get_base_address (MEM_EXPR (mem));
12363 if (base == NULL
12364 || TREE_CODE (base) != VAR_DECL
12365 || !DECL_THREAD_LOCAL_P (base))
12366 return NULL;
12368 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12369 if (loc_result == NULL)
12370 return NULL;
12372 if (MEM_OFFSET (mem))
12373 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12375 return loc_result;
12378 /* Output debug info about reason why we failed to expand expression as dwarf
12379 expression. */
12381 static void
12382 expansion_failed (tree expr, rtx rtl, char const *reason)
12384 if (dump_file && (dump_flags & TDF_DETAILS))
12386 fprintf (dump_file, "Failed to expand as dwarf: ");
12387 if (expr)
12388 print_generic_expr (dump_file, expr, dump_flags);
12389 if (rtl)
12391 fprintf (dump_file, "\n");
12392 print_rtl (dump_file, rtl);
12394 fprintf (dump_file, "\nReason: %s\n", reason);
12398 /* Helper function for const_ok_for_output. */
12400 static bool
12401 const_ok_for_output_1 (rtx rtl)
12403 if (GET_CODE (rtl) == UNSPEC)
12405 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12406 we can't express it in the debug info. */
12407 /* Don't complain about TLS UNSPECs, those are just too hard to
12408 delegitimize. Note this could be a non-decl SYMBOL_REF such as
12409 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12410 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
12411 if (flag_checking
12412 && (XVECLEN (rtl, 0) == 0
12413 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12414 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12415 inform (current_function_decl
12416 ? DECL_SOURCE_LOCATION (current_function_decl)
12417 : UNKNOWN_LOCATION,
12418 #if NUM_UNSPEC_VALUES > 0
12419 "non-delegitimized UNSPEC %s (%d) found in variable location",
12420 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12421 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12422 XINT (rtl, 1));
12423 #else
12424 "non-delegitimized UNSPEC %d found in variable location",
12425 XINT (rtl, 1));
12426 #endif
12427 expansion_failed (NULL_TREE, rtl,
12428 "UNSPEC hasn't been delegitimized.\n");
12429 return false;
12432 if (targetm.const_not_ok_for_debug_p (rtl))
12434 expansion_failed (NULL_TREE, rtl,
12435 "Expression rejected for debug by the backend.\n");
12436 return false;
12439 /* FIXME: Refer to PR60655. It is possible for simplification
12440 of rtl expressions in var tracking to produce such expressions.
12441 We should really identify / validate expressions
12442 enclosed in CONST that can be handled by assemblers on various
12443 targets and only handle legitimate cases here. */
12444 if (GET_CODE (rtl) != SYMBOL_REF)
12446 if (GET_CODE (rtl) == NOT)
12447 return false;
12448 return true;
12451 if (CONSTANT_POOL_ADDRESS_P (rtl))
12453 bool marked;
12454 get_pool_constant_mark (rtl, &marked);
12455 /* If all references to this pool constant were optimized away,
12456 it was not output and thus we can't represent it. */
12457 if (!marked)
12459 expansion_failed (NULL_TREE, rtl,
12460 "Constant was removed from constant pool.\n");
12461 return false;
12465 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12466 return false;
12468 /* Avoid references to external symbols in debug info, on several targets
12469 the linker might even refuse to link when linking a shared library,
12470 and in many other cases the relocations for .debug_info/.debug_loc are
12471 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
12472 to be defined within the same shared library or executable are fine. */
12473 if (SYMBOL_REF_EXTERNAL_P (rtl))
12475 tree decl = SYMBOL_REF_DECL (rtl);
12477 if (decl == NULL || !targetm.binds_local_p (decl))
12479 expansion_failed (NULL_TREE, rtl,
12480 "Symbol not defined in current TU.\n");
12481 return false;
12485 return true;
12488 /* Return true if constant RTL can be emitted in DW_OP_addr or
12489 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
12490 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
12492 static bool
12493 const_ok_for_output (rtx rtl)
12495 if (GET_CODE (rtl) == SYMBOL_REF)
12496 return const_ok_for_output_1 (rtl);
12498 if (GET_CODE (rtl) == CONST)
12500 subrtx_var_iterator::array_type array;
12501 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12502 if (!const_ok_for_output_1 (*iter))
12503 return false;
12504 return true;
12507 return true;
12510 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12511 if possible, NULL otherwise. */
12513 static dw_die_ref
12514 base_type_for_mode (machine_mode mode, bool unsignedp)
12516 dw_die_ref type_die;
12517 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12519 if (type == NULL)
12520 return NULL;
12521 switch (TREE_CODE (type))
12523 case INTEGER_TYPE:
12524 case REAL_TYPE:
12525 break;
12526 default:
12527 return NULL;
12529 type_die = lookup_type_die (type);
12530 if (!type_die)
12531 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12532 comp_unit_die ());
12533 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12534 return NULL;
12535 return type_die;
12538 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12539 type matching MODE, or, if MODE is narrower than or as wide as
12540 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
12541 possible. */
12543 static dw_loc_descr_ref
12544 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12546 machine_mode outer_mode = mode;
12547 dw_die_ref type_die;
12548 dw_loc_descr_ref cvt;
12550 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12552 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12553 return op;
12555 type_die = base_type_for_mode (outer_mode, 1);
12556 if (type_die == NULL)
12557 return NULL;
12558 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12559 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12560 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12561 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12562 add_loc_descr (&op, cvt);
12563 return op;
12566 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
12568 static dw_loc_descr_ref
12569 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12570 dw_loc_descr_ref op1)
12572 dw_loc_descr_ref ret = op0;
12573 add_loc_descr (&ret, op1);
12574 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12575 if (STORE_FLAG_VALUE != 1)
12577 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12578 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12580 return ret;
12583 /* Return location descriptor for signed comparison OP RTL. */
12585 static dw_loc_descr_ref
12586 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12587 machine_mode mem_mode)
12589 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12590 dw_loc_descr_ref op0, op1;
12591 int shift;
12593 if (op_mode == VOIDmode)
12594 op_mode = GET_MODE (XEXP (rtl, 1));
12595 if (op_mode == VOIDmode)
12596 return NULL;
12598 if (dwarf_strict
12599 && (!SCALAR_INT_MODE_P (op_mode)
12600 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12601 return NULL;
12603 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12604 VAR_INIT_STATUS_INITIALIZED);
12605 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12606 VAR_INIT_STATUS_INITIALIZED);
12608 if (op0 == NULL || op1 == NULL)
12609 return NULL;
12611 if (!SCALAR_INT_MODE_P (op_mode)
12612 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12613 return compare_loc_descriptor (op, op0, op1);
12615 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12617 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12618 dw_loc_descr_ref cvt;
12620 if (type_die == NULL)
12621 return NULL;
12622 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12623 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12624 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12625 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12626 add_loc_descr (&op0, cvt);
12627 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12628 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12629 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12630 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12631 add_loc_descr (&op1, cvt);
12632 return compare_loc_descriptor (op, op0, op1);
12635 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12636 /* For eq/ne, if the operands are known to be zero-extended,
12637 there is no need to do the fancy shifting up. */
12638 if (op == DW_OP_eq || op == DW_OP_ne)
12640 dw_loc_descr_ref last0, last1;
12641 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12643 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12645 /* deref_size zero extends, and for constants we can check
12646 whether they are zero extended or not. */
12647 if (((last0->dw_loc_opc == DW_OP_deref_size
12648 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12649 || (CONST_INT_P (XEXP (rtl, 0))
12650 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12651 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12652 && ((last1->dw_loc_opc == DW_OP_deref_size
12653 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12654 || (CONST_INT_P (XEXP (rtl, 1))
12655 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12656 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12657 return compare_loc_descriptor (op, op0, op1);
12659 /* EQ/NE comparison against constant in narrower type than
12660 DWARF2_ADDR_SIZE can be performed either as
12661 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12662 DW_OP_{eq,ne}
12664 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12665 DW_OP_{eq,ne}. Pick whatever is shorter. */
12666 if (CONST_INT_P (XEXP (rtl, 1))
12667 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12668 && (size_of_int_loc_descriptor (shift) + 1
12669 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12670 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12671 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12672 & GET_MODE_MASK (op_mode))))
12674 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12675 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12676 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12677 & GET_MODE_MASK (op_mode));
12678 return compare_loc_descriptor (op, op0, op1);
12681 add_loc_descr (&op0, int_loc_descriptor (shift));
12682 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12683 if (CONST_INT_P (XEXP (rtl, 1)))
12684 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12685 else
12687 add_loc_descr (&op1, int_loc_descriptor (shift));
12688 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12690 return compare_loc_descriptor (op, op0, op1);
12693 /* Return location descriptor for unsigned comparison OP RTL. */
12695 static dw_loc_descr_ref
12696 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12697 machine_mode mem_mode)
12699 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12700 dw_loc_descr_ref op0, op1;
12702 if (op_mode == VOIDmode)
12703 op_mode = GET_MODE (XEXP (rtl, 1));
12704 if (op_mode == VOIDmode)
12705 return NULL;
12706 if (!SCALAR_INT_MODE_P (op_mode))
12707 return NULL;
12709 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12710 return NULL;
12712 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12713 VAR_INIT_STATUS_INITIALIZED);
12714 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12715 VAR_INIT_STATUS_INITIALIZED);
12717 if (op0 == NULL || op1 == NULL)
12718 return NULL;
12720 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12722 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12723 dw_loc_descr_ref last0, last1;
12724 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12726 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12728 if (CONST_INT_P (XEXP (rtl, 0)))
12729 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12730 /* deref_size zero extends, so no need to mask it again. */
12731 else if (last0->dw_loc_opc != DW_OP_deref_size
12732 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12734 add_loc_descr (&op0, int_loc_descriptor (mask));
12735 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12737 if (CONST_INT_P (XEXP (rtl, 1)))
12738 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12739 /* deref_size zero extends, so no need to mask it again. */
12740 else if (last1->dw_loc_opc != DW_OP_deref_size
12741 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12743 add_loc_descr (&op1, int_loc_descriptor (mask));
12744 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12747 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12749 HOST_WIDE_INT bias = 1;
12750 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12751 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12752 if (CONST_INT_P (XEXP (rtl, 1)))
12753 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12754 + INTVAL (XEXP (rtl, 1)));
12755 else
12756 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12757 bias, 0));
12759 return compare_loc_descriptor (op, op0, op1);
12762 /* Return location descriptor for {U,S}{MIN,MAX}. */
12764 static dw_loc_descr_ref
12765 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12766 machine_mode mem_mode)
12768 enum dwarf_location_atom op;
12769 dw_loc_descr_ref op0, op1, ret;
12770 dw_loc_descr_ref bra_node, drop_node;
12772 if (dwarf_strict
12773 && (!SCALAR_INT_MODE_P (mode)
12774 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12775 return NULL;
12777 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12778 VAR_INIT_STATUS_INITIALIZED);
12779 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12780 VAR_INIT_STATUS_INITIALIZED);
12782 if (op0 == NULL || op1 == NULL)
12783 return NULL;
12785 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12786 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12787 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12788 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12790 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12792 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12793 add_loc_descr (&op0, int_loc_descriptor (mask));
12794 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12795 add_loc_descr (&op1, int_loc_descriptor (mask));
12796 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12798 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12800 HOST_WIDE_INT bias = 1;
12801 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12802 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12803 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12806 else if (!SCALAR_INT_MODE_P (mode)
12807 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12809 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12810 add_loc_descr (&op0, int_loc_descriptor (shift));
12811 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12812 add_loc_descr (&op1, int_loc_descriptor (shift));
12813 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12815 else if (SCALAR_INT_MODE_P (mode)
12816 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12818 dw_die_ref type_die = base_type_for_mode (mode, 0);
12819 dw_loc_descr_ref cvt;
12820 if (type_die == NULL)
12821 return NULL;
12822 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12823 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12824 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12825 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12826 add_loc_descr (&op0, cvt);
12827 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12828 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12829 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12830 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12831 add_loc_descr (&op1, cvt);
12834 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12835 op = DW_OP_lt;
12836 else
12837 op = DW_OP_gt;
12838 ret = op0;
12839 add_loc_descr (&ret, op1);
12840 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12841 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12842 add_loc_descr (&ret, bra_node);
12843 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12844 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12845 add_loc_descr (&ret, drop_node);
12846 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12847 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12848 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12849 && SCALAR_INT_MODE_P (mode)
12850 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12851 ret = convert_descriptor_to_mode (mode, ret);
12852 return ret;
12855 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12856 but after converting arguments to type_die, afterwards
12857 convert back to unsigned. */
12859 static dw_loc_descr_ref
12860 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12861 machine_mode mode, machine_mode mem_mode)
12863 dw_loc_descr_ref cvt, op0, op1;
12865 if (type_die == NULL)
12866 return NULL;
12867 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12868 VAR_INIT_STATUS_INITIALIZED);
12869 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12870 VAR_INIT_STATUS_INITIALIZED);
12871 if (op0 == NULL || op1 == NULL)
12872 return NULL;
12873 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12874 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12875 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12876 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12877 add_loc_descr (&op0, cvt);
12878 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12879 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12880 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12881 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12882 add_loc_descr (&op1, cvt);
12883 add_loc_descr (&op0, op1);
12884 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12885 return convert_descriptor_to_mode (mode, op0);
12888 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12889 const0 is DW_OP_lit0 or corresponding typed constant,
12890 const1 is DW_OP_lit1 or corresponding typed constant
12891 and constMSB is constant with just the MSB bit set
12892 for the mode):
12893 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12894 L1: const0 DW_OP_swap
12895 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12896 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12897 L3: DW_OP_drop
12898 L4: DW_OP_nop
12900 CTZ is similar:
12901 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12902 L1: const0 DW_OP_swap
12903 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12904 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12905 L3: DW_OP_drop
12906 L4: DW_OP_nop
12908 FFS is similar:
12909 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12910 L1: const1 DW_OP_swap
12911 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12912 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12913 L3: DW_OP_drop
12914 L4: DW_OP_nop */
12916 static dw_loc_descr_ref
12917 clz_loc_descriptor (rtx rtl, machine_mode mode,
12918 machine_mode mem_mode)
12920 dw_loc_descr_ref op0, ret, tmp;
12921 HOST_WIDE_INT valv;
12922 dw_loc_descr_ref l1jump, l1label;
12923 dw_loc_descr_ref l2jump, l2label;
12924 dw_loc_descr_ref l3jump, l3label;
12925 dw_loc_descr_ref l4jump, l4label;
12926 rtx msb;
12928 if (!SCALAR_INT_MODE_P (mode)
12929 || GET_MODE (XEXP (rtl, 0)) != mode)
12930 return NULL;
12932 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12933 VAR_INIT_STATUS_INITIALIZED);
12934 if (op0 == NULL)
12935 return NULL;
12936 ret = op0;
12937 if (GET_CODE (rtl) == CLZ)
12939 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12940 valv = GET_MODE_BITSIZE (mode);
12942 else if (GET_CODE (rtl) == FFS)
12943 valv = 0;
12944 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12945 valv = GET_MODE_BITSIZE (mode);
12946 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12947 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12948 add_loc_descr (&ret, l1jump);
12949 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12950 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12951 VAR_INIT_STATUS_INITIALIZED);
12952 if (tmp == NULL)
12953 return NULL;
12954 add_loc_descr (&ret, tmp);
12955 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12956 add_loc_descr (&ret, l4jump);
12957 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12958 ? const1_rtx : const0_rtx,
12959 mode, mem_mode,
12960 VAR_INIT_STATUS_INITIALIZED);
12961 if (l1label == NULL)
12962 return NULL;
12963 add_loc_descr (&ret, l1label);
12964 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12965 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12966 add_loc_descr (&ret, l2label);
12967 if (GET_CODE (rtl) != CLZ)
12968 msb = const1_rtx;
12969 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12970 msb = GEN_INT (HOST_WIDE_INT_1U
12971 << (GET_MODE_BITSIZE (mode) - 1));
12972 else
12973 msb = immed_wide_int_const
12974 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12975 GET_MODE_PRECISION (mode)), mode);
12976 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12977 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12978 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12979 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12980 else
12981 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12982 VAR_INIT_STATUS_INITIALIZED);
12983 if (tmp == NULL)
12984 return NULL;
12985 add_loc_descr (&ret, tmp);
12986 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12987 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12988 add_loc_descr (&ret, l3jump);
12989 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12990 VAR_INIT_STATUS_INITIALIZED);
12991 if (tmp == NULL)
12992 return NULL;
12993 add_loc_descr (&ret, tmp);
12994 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12995 ? DW_OP_shl : DW_OP_shr, 0, 0));
12996 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12997 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12998 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12999 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13000 add_loc_descr (&ret, l2jump);
13001 l3label = new_loc_descr (DW_OP_drop, 0, 0);
13002 add_loc_descr (&ret, l3label);
13003 l4label = new_loc_descr (DW_OP_nop, 0, 0);
13004 add_loc_descr (&ret, l4label);
13005 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13006 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13007 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13008 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13009 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13010 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13011 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13012 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13013 return ret;
13016 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13017 const1 is DW_OP_lit1 or corresponding typed constant):
13018 const0 DW_OP_swap
13019 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13020 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13021 L2: DW_OP_drop
13023 PARITY is similar:
13024 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13025 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13026 L2: DW_OP_drop */
13028 static dw_loc_descr_ref
13029 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13030 machine_mode mem_mode)
13032 dw_loc_descr_ref op0, ret, tmp;
13033 dw_loc_descr_ref l1jump, l1label;
13034 dw_loc_descr_ref l2jump, l2label;
13036 if (!SCALAR_INT_MODE_P (mode)
13037 || GET_MODE (XEXP (rtl, 0)) != mode)
13038 return NULL;
13040 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13041 VAR_INIT_STATUS_INITIALIZED);
13042 if (op0 == NULL)
13043 return NULL;
13044 ret = op0;
13045 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13046 VAR_INIT_STATUS_INITIALIZED);
13047 if (tmp == NULL)
13048 return NULL;
13049 add_loc_descr (&ret, tmp);
13050 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13051 l1label = new_loc_descr (DW_OP_dup, 0, 0);
13052 add_loc_descr (&ret, l1label);
13053 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13054 add_loc_descr (&ret, l2jump);
13055 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13056 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13057 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13058 VAR_INIT_STATUS_INITIALIZED);
13059 if (tmp == NULL)
13060 return NULL;
13061 add_loc_descr (&ret, tmp);
13062 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13063 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13064 ? DW_OP_plus : DW_OP_xor, 0, 0));
13065 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13066 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13067 VAR_INIT_STATUS_INITIALIZED);
13068 add_loc_descr (&ret, tmp);
13069 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13070 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13071 add_loc_descr (&ret, l1jump);
13072 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13073 add_loc_descr (&ret, l2label);
13074 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13075 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13076 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13077 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13078 return ret;
13081 /* BSWAP (constS is initial shift count, either 56 or 24):
13082 constS const0
13083 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13084 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13085 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13086 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13087 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
13089 static dw_loc_descr_ref
13090 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13091 machine_mode mem_mode)
13093 dw_loc_descr_ref op0, ret, tmp;
13094 dw_loc_descr_ref l1jump, l1label;
13095 dw_loc_descr_ref l2jump, l2label;
13097 if (!SCALAR_INT_MODE_P (mode)
13098 || BITS_PER_UNIT != 8
13099 || (GET_MODE_BITSIZE (mode) != 32
13100 && GET_MODE_BITSIZE (mode) != 64))
13101 return NULL;
13103 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13104 VAR_INIT_STATUS_INITIALIZED);
13105 if (op0 == NULL)
13106 return NULL;
13108 ret = op0;
13109 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13110 mode, mem_mode,
13111 VAR_INIT_STATUS_INITIALIZED);
13112 if (tmp == NULL)
13113 return NULL;
13114 add_loc_descr (&ret, tmp);
13115 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13116 VAR_INIT_STATUS_INITIALIZED);
13117 if (tmp == NULL)
13118 return NULL;
13119 add_loc_descr (&ret, tmp);
13120 l1label = new_loc_descr (DW_OP_pick, 2, 0);
13121 add_loc_descr (&ret, l1label);
13122 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13123 mode, mem_mode,
13124 VAR_INIT_STATUS_INITIALIZED);
13125 add_loc_descr (&ret, tmp);
13126 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13127 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13128 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13129 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13130 VAR_INIT_STATUS_INITIALIZED);
13131 if (tmp == NULL)
13132 return NULL;
13133 add_loc_descr (&ret, tmp);
13134 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13135 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13136 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13137 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13138 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13139 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13140 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13141 VAR_INIT_STATUS_INITIALIZED);
13142 add_loc_descr (&ret, tmp);
13143 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13144 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13145 add_loc_descr (&ret, l2jump);
13146 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13147 VAR_INIT_STATUS_INITIALIZED);
13148 add_loc_descr (&ret, tmp);
13149 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13150 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13151 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13152 add_loc_descr (&ret, l1jump);
13153 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13154 add_loc_descr (&ret, l2label);
13155 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13156 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13157 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13158 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13159 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13160 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13161 return ret;
13164 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13165 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13166 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13167 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13169 ROTATERT is similar:
13170 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13171 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13172 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
13174 static dw_loc_descr_ref
13175 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13176 machine_mode mem_mode)
13178 rtx rtlop1 = XEXP (rtl, 1);
13179 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13180 int i;
13182 if (!SCALAR_INT_MODE_P (mode))
13183 return NULL;
13185 if (GET_MODE (rtlop1) != VOIDmode
13186 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13187 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13188 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13189 VAR_INIT_STATUS_INITIALIZED);
13190 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13191 VAR_INIT_STATUS_INITIALIZED);
13192 if (op0 == NULL || op1 == NULL)
13193 return NULL;
13194 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13195 for (i = 0; i < 2; i++)
13197 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13198 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13199 mode, mem_mode,
13200 VAR_INIT_STATUS_INITIALIZED);
13201 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13202 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13203 ? DW_OP_const4u
13204 : HOST_BITS_PER_WIDE_INT == 64
13205 ? DW_OP_const8u : DW_OP_constu,
13206 GET_MODE_MASK (mode), 0);
13207 else
13208 mask[i] = NULL;
13209 if (mask[i] == NULL)
13210 return NULL;
13211 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13213 ret = op0;
13214 add_loc_descr (&ret, op1);
13215 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13216 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13217 if (GET_CODE (rtl) == ROTATERT)
13219 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13220 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13221 GET_MODE_BITSIZE (mode), 0));
13223 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13224 if (mask[0] != NULL)
13225 add_loc_descr (&ret, mask[0]);
13226 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13227 if (mask[1] != NULL)
13229 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13230 add_loc_descr (&ret, mask[1]);
13231 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13233 if (GET_CODE (rtl) == ROTATE)
13235 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13236 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13237 GET_MODE_BITSIZE (mode), 0));
13239 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13240 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13241 return ret;
13244 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
13245 for DEBUG_PARAMETER_REF RTL. */
13247 static dw_loc_descr_ref
13248 parameter_ref_descriptor (rtx rtl)
13250 dw_loc_descr_ref ret;
13251 dw_die_ref ref;
13253 if (dwarf_strict)
13254 return NULL;
13255 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13256 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13257 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13258 if (ref)
13260 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13261 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13262 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13264 else
13266 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13267 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13269 return ret;
13272 /* The following routine converts the RTL for a variable or parameter
13273 (resident in memory) into an equivalent Dwarf representation of a
13274 mechanism for getting the address of that same variable onto the top of a
13275 hypothetical "address evaluation" stack.
13277 When creating memory location descriptors, we are effectively transforming
13278 the RTL for a memory-resident object into its Dwarf postfix expression
13279 equivalent. This routine recursively descends an RTL tree, turning
13280 it into Dwarf postfix code as it goes.
13282 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13284 MEM_MODE is the mode of the memory reference, needed to handle some
13285 autoincrement addressing modes.
13287 Return 0 if we can't represent the location. */
13289 dw_loc_descr_ref
13290 mem_loc_descriptor (rtx rtl, machine_mode mode,
13291 machine_mode mem_mode,
13292 enum var_init_status initialized)
13294 dw_loc_descr_ref mem_loc_result = NULL;
13295 enum dwarf_location_atom op;
13296 dw_loc_descr_ref op0, op1;
13297 rtx inner = NULL_RTX;
13299 if (mode == VOIDmode)
13300 mode = GET_MODE (rtl);
13302 /* Note that for a dynamically sized array, the location we will generate a
13303 description of here will be the lowest numbered location which is
13304 actually within the array. That's *not* necessarily the same as the
13305 zeroth element of the array. */
13307 rtl = targetm.delegitimize_address (rtl);
13309 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13310 return NULL;
13312 switch (GET_CODE (rtl))
13314 case POST_INC:
13315 case POST_DEC:
13316 case POST_MODIFY:
13317 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13319 case SUBREG:
13320 /* The case of a subreg may arise when we have a local (register)
13321 variable or a formal (register) parameter which doesn't quite fill
13322 up an entire register. For now, just assume that it is
13323 legitimate to make the Dwarf info refer to the whole register which
13324 contains the given subreg. */
13325 if (!subreg_lowpart_p (rtl))
13326 break;
13327 inner = SUBREG_REG (rtl);
13328 /* FALLTHRU */
13329 case TRUNCATE:
13330 if (inner == NULL_RTX)
13331 inner = XEXP (rtl, 0);
13332 if (SCALAR_INT_MODE_P (mode)
13333 && SCALAR_INT_MODE_P (GET_MODE (inner))
13334 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13335 #ifdef POINTERS_EXTEND_UNSIGNED
13336 || (mode == Pmode && mem_mode != VOIDmode)
13337 #endif
13339 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13341 mem_loc_result = mem_loc_descriptor (inner,
13342 GET_MODE (inner),
13343 mem_mode, initialized);
13344 break;
13346 if (dwarf_strict)
13347 break;
13348 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13349 break;
13350 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13351 && (!SCALAR_INT_MODE_P (mode)
13352 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
13353 break;
13354 else
13356 dw_die_ref type_die;
13357 dw_loc_descr_ref cvt;
13359 mem_loc_result = mem_loc_descriptor (inner,
13360 GET_MODE (inner),
13361 mem_mode, initialized);
13362 if (mem_loc_result == NULL)
13363 break;
13364 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13365 if (type_die == NULL)
13367 mem_loc_result = NULL;
13368 break;
13370 if (GET_MODE_SIZE (mode)
13371 != GET_MODE_SIZE (GET_MODE (inner)))
13372 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13373 else
13374 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13375 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13376 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13377 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13378 add_loc_descr (&mem_loc_result, cvt);
13379 if (SCALAR_INT_MODE_P (mode)
13380 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13382 /* Convert it to untyped afterwards. */
13383 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13384 add_loc_descr (&mem_loc_result, cvt);
13387 break;
13389 case REG:
13390 if (! SCALAR_INT_MODE_P (mode)
13391 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13392 && rtl != arg_pointer_rtx
13393 && rtl != frame_pointer_rtx
13394 #ifdef POINTERS_EXTEND_UNSIGNED
13395 && (mode != Pmode || mem_mode == VOIDmode)
13396 #endif
13399 dw_die_ref type_die;
13400 unsigned int dbx_regnum;
13402 if (dwarf_strict)
13403 break;
13404 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13405 break;
13406 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13407 if (type_die == NULL)
13408 break;
13410 dbx_regnum = dbx_reg_number (rtl);
13411 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13412 break;
13413 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13414 dbx_regnum, 0);
13415 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13416 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13417 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13418 break;
13420 /* Whenever a register number forms a part of the description of the
13421 method for calculating the (dynamic) address of a memory resident
13422 object, DWARF rules require the register number be referred to as
13423 a "base register". This distinction is not based in any way upon
13424 what category of register the hardware believes the given register
13425 belongs to. This is strictly DWARF terminology we're dealing with
13426 here. Note that in cases where the location of a memory-resident
13427 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13428 OP_CONST (0)) the actual DWARF location descriptor that we generate
13429 may just be OP_BASEREG (basereg). This may look deceptively like
13430 the object in question was allocated to a register (rather than in
13431 memory) so DWARF consumers need to be aware of the subtle
13432 distinction between OP_REG and OP_BASEREG. */
13433 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13434 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13435 else if (stack_realign_drap
13436 && crtl->drap_reg
13437 && crtl->args.internal_arg_pointer == rtl
13438 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13440 /* If RTL is internal_arg_pointer, which has been optimized
13441 out, use DRAP instead. */
13442 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13443 VAR_INIT_STATUS_INITIALIZED);
13445 break;
13447 case SIGN_EXTEND:
13448 case ZERO_EXTEND:
13449 if (!SCALAR_INT_MODE_P (mode))
13450 break;
13451 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13452 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13453 if (op0 == 0)
13454 break;
13455 else if (GET_CODE (rtl) == ZERO_EXTEND
13456 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13457 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13458 < HOST_BITS_PER_WIDE_INT
13459 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13460 to expand zero extend as two shifts instead of
13461 masking. */
13462 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13464 machine_mode imode = GET_MODE (XEXP (rtl, 0));
13465 mem_loc_result = op0;
13466 add_loc_descr (&mem_loc_result,
13467 int_loc_descriptor (GET_MODE_MASK (imode)));
13468 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13470 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13472 int shift = DWARF2_ADDR_SIZE
13473 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13474 shift *= BITS_PER_UNIT;
13475 if (GET_CODE (rtl) == SIGN_EXTEND)
13476 op = DW_OP_shra;
13477 else
13478 op = DW_OP_shr;
13479 mem_loc_result = op0;
13480 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13481 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13482 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13483 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13485 else if (!dwarf_strict)
13487 dw_die_ref type_die1, type_die2;
13488 dw_loc_descr_ref cvt;
13490 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13491 GET_CODE (rtl) == ZERO_EXTEND);
13492 if (type_die1 == NULL)
13493 break;
13494 type_die2 = base_type_for_mode (mode, 1);
13495 if (type_die2 == NULL)
13496 break;
13497 mem_loc_result = op0;
13498 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13499 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13500 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13501 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13502 add_loc_descr (&mem_loc_result, cvt);
13503 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13504 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13505 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13506 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13507 add_loc_descr (&mem_loc_result, cvt);
13509 break;
13511 case MEM:
13513 rtx new_rtl = avoid_constant_pool_reference (rtl);
13514 if (new_rtl != rtl)
13516 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13517 initialized);
13518 if (mem_loc_result != NULL)
13519 return mem_loc_result;
13522 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13523 get_address_mode (rtl), mode,
13524 VAR_INIT_STATUS_INITIALIZED);
13525 if (mem_loc_result == NULL)
13526 mem_loc_result = tls_mem_loc_descriptor (rtl);
13527 if (mem_loc_result != NULL)
13529 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13530 || !SCALAR_INT_MODE_P(mode))
13532 dw_die_ref type_die;
13533 dw_loc_descr_ref deref;
13535 if (dwarf_strict)
13536 return NULL;
13537 type_die
13538 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13539 if (type_die == NULL)
13540 return NULL;
13541 deref = new_loc_descr (DW_OP_GNU_deref_type,
13542 GET_MODE_SIZE (mode), 0);
13543 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13544 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13545 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13546 add_loc_descr (&mem_loc_result, deref);
13548 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13549 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13550 else
13551 add_loc_descr (&mem_loc_result,
13552 new_loc_descr (DW_OP_deref_size,
13553 GET_MODE_SIZE (mode), 0));
13555 break;
13557 case LO_SUM:
13558 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13560 case LABEL_REF:
13561 /* Some ports can transform a symbol ref into a label ref, because
13562 the symbol ref is too far away and has to be dumped into a constant
13563 pool. */
13564 case CONST:
13565 case SYMBOL_REF:
13566 if (!SCALAR_INT_MODE_P (mode)
13567 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13568 #ifdef POINTERS_EXTEND_UNSIGNED
13569 && (mode != Pmode || mem_mode == VOIDmode)
13570 #endif
13572 break;
13573 if (GET_CODE (rtl) == SYMBOL_REF
13574 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13576 dw_loc_descr_ref temp;
13578 /* If this is not defined, we have no way to emit the data. */
13579 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13580 break;
13582 temp = new_addr_loc_descr (rtl, dtprel_true);
13584 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13585 add_loc_descr (&mem_loc_result, temp);
13587 break;
13590 if (!const_ok_for_output (rtl))
13592 if (GET_CODE (rtl) == CONST)
13593 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13594 initialized);
13595 break;
13598 symref:
13599 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13600 vec_safe_push (used_rtx_array, rtl);
13601 break;
13603 case CONCAT:
13604 case CONCATN:
13605 case VAR_LOCATION:
13606 case DEBUG_IMPLICIT_PTR:
13607 expansion_failed (NULL_TREE, rtl,
13608 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13609 return 0;
13611 case ENTRY_VALUE:
13612 if (dwarf_strict)
13613 return NULL;
13614 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13616 if (!SCALAR_INT_MODE_P (mode)
13617 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13618 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13619 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13620 else
13622 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13623 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13624 return NULL;
13625 op0 = one_reg_loc_descriptor (dbx_regnum,
13626 VAR_INIT_STATUS_INITIALIZED);
13629 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13630 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13632 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13633 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13634 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13635 return NULL;
13637 else
13638 gcc_unreachable ();
13639 if (op0 == NULL)
13640 return NULL;
13641 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13642 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13643 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13644 break;
13646 case DEBUG_PARAMETER_REF:
13647 mem_loc_result = parameter_ref_descriptor (rtl);
13648 break;
13650 case PRE_MODIFY:
13651 /* Extract the PLUS expression nested inside and fall into
13652 PLUS code below. */
13653 rtl = XEXP (rtl, 1);
13654 goto plus;
13656 case PRE_INC:
13657 case PRE_DEC:
13658 /* Turn these into a PLUS expression and fall into the PLUS code
13659 below. */
13660 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13661 gen_int_mode (GET_CODE (rtl) == PRE_INC
13662 ? GET_MODE_UNIT_SIZE (mem_mode)
13663 : -GET_MODE_UNIT_SIZE (mem_mode),
13664 mode));
13666 /* fall through */
13668 case PLUS:
13669 plus:
13670 if (is_based_loc (rtl)
13671 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13672 || XEXP (rtl, 0) == arg_pointer_rtx
13673 || XEXP (rtl, 0) == frame_pointer_rtx)
13674 && SCALAR_INT_MODE_P (mode))
13675 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13676 INTVAL (XEXP (rtl, 1)),
13677 VAR_INIT_STATUS_INITIALIZED);
13678 else
13680 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13681 VAR_INIT_STATUS_INITIALIZED);
13682 if (mem_loc_result == 0)
13683 break;
13685 if (CONST_INT_P (XEXP (rtl, 1))
13686 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13687 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13688 else
13690 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13691 VAR_INIT_STATUS_INITIALIZED);
13692 if (op1 == 0)
13693 return NULL;
13694 add_loc_descr (&mem_loc_result, op1);
13695 add_loc_descr (&mem_loc_result,
13696 new_loc_descr (DW_OP_plus, 0, 0));
13699 break;
13701 /* If a pseudo-reg is optimized away, it is possible for it to
13702 be replaced with a MEM containing a multiply or shift. */
13703 case MINUS:
13704 op = DW_OP_minus;
13705 goto do_binop;
13707 case MULT:
13708 op = DW_OP_mul;
13709 goto do_binop;
13711 case DIV:
13712 if (!dwarf_strict
13713 && SCALAR_INT_MODE_P (mode)
13714 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13716 mem_loc_result = typed_binop (DW_OP_div, rtl,
13717 base_type_for_mode (mode, 0),
13718 mode, mem_mode);
13719 break;
13721 op = DW_OP_div;
13722 goto do_binop;
13724 case UMOD:
13725 op = DW_OP_mod;
13726 goto do_binop;
13728 case ASHIFT:
13729 op = DW_OP_shl;
13730 goto do_shift;
13732 case ASHIFTRT:
13733 op = DW_OP_shra;
13734 goto do_shift;
13736 case LSHIFTRT:
13737 op = DW_OP_shr;
13738 goto do_shift;
13740 do_shift:
13741 if (!SCALAR_INT_MODE_P (mode))
13742 break;
13743 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13744 VAR_INIT_STATUS_INITIALIZED);
13746 rtx rtlop1 = XEXP (rtl, 1);
13747 if (GET_MODE (rtlop1) != VOIDmode
13748 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13749 < GET_MODE_BITSIZE (mode))
13750 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13751 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13752 VAR_INIT_STATUS_INITIALIZED);
13755 if (op0 == 0 || op1 == 0)
13756 break;
13758 mem_loc_result = op0;
13759 add_loc_descr (&mem_loc_result, op1);
13760 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13761 break;
13763 case AND:
13764 op = DW_OP_and;
13765 goto do_binop;
13767 case IOR:
13768 op = DW_OP_or;
13769 goto do_binop;
13771 case XOR:
13772 op = DW_OP_xor;
13773 goto do_binop;
13775 do_binop:
13776 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13777 VAR_INIT_STATUS_INITIALIZED);
13778 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13779 VAR_INIT_STATUS_INITIALIZED);
13781 if (op0 == 0 || op1 == 0)
13782 break;
13784 mem_loc_result = op0;
13785 add_loc_descr (&mem_loc_result, op1);
13786 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13787 break;
13789 case MOD:
13790 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13792 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13793 base_type_for_mode (mode, 0),
13794 mode, mem_mode);
13795 break;
13798 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13799 VAR_INIT_STATUS_INITIALIZED);
13800 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13801 VAR_INIT_STATUS_INITIALIZED);
13803 if (op0 == 0 || op1 == 0)
13804 break;
13806 mem_loc_result = op0;
13807 add_loc_descr (&mem_loc_result, op1);
13808 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13809 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13810 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13811 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13812 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13813 break;
13815 case UDIV:
13816 if (!dwarf_strict && SCALAR_INT_MODE_P (mode))
13818 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13820 op = DW_OP_div;
13821 goto do_binop;
13823 mem_loc_result = typed_binop (DW_OP_div, rtl,
13824 base_type_for_mode (mode, 1),
13825 mode, mem_mode);
13827 break;
13829 case NOT:
13830 op = DW_OP_not;
13831 goto do_unop;
13833 case ABS:
13834 op = DW_OP_abs;
13835 goto do_unop;
13837 case NEG:
13838 op = DW_OP_neg;
13839 goto do_unop;
13841 do_unop:
13842 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13843 VAR_INIT_STATUS_INITIALIZED);
13845 if (op0 == 0)
13846 break;
13848 mem_loc_result = op0;
13849 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13850 break;
13852 case CONST_INT:
13853 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13854 #ifdef POINTERS_EXTEND_UNSIGNED
13855 || (mode == Pmode
13856 && mem_mode != VOIDmode
13857 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13858 #endif
13861 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13862 break;
13864 if (!dwarf_strict
13865 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13866 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13868 dw_die_ref type_die = base_type_for_mode (mode, 1);
13869 machine_mode amode;
13870 if (type_die == NULL)
13871 return NULL;
13872 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13873 MODE_INT, 0);
13874 if (INTVAL (rtl) >= 0
13875 && amode != BLKmode
13876 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13877 /* const DW_OP_GNU_convert <XXX> vs.
13878 DW_OP_GNU_const_type <XXX, 1, const>. */
13879 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13880 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13882 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13883 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13884 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13885 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13886 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13887 add_loc_descr (&mem_loc_result, op0);
13888 return mem_loc_result;
13890 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13891 INTVAL (rtl));
13892 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13893 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13894 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13895 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13896 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13897 else
13899 mem_loc_result->dw_loc_oprnd2.val_class
13900 = dw_val_class_const_double;
13901 mem_loc_result->dw_loc_oprnd2.v.val_double
13902 = double_int::from_shwi (INTVAL (rtl));
13905 break;
13907 case CONST_DOUBLE:
13908 if (!dwarf_strict)
13910 dw_die_ref type_die;
13912 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13913 CONST_DOUBLE rtx could represent either a large integer
13914 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13915 the value is always a floating point constant.
13917 When it is an integer, a CONST_DOUBLE is used whenever
13918 the constant requires 2 HWIs to be adequately represented.
13919 We output CONST_DOUBLEs as blocks. */
13920 if (mode == VOIDmode
13921 || (GET_MODE (rtl) == VOIDmode
13922 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13923 break;
13924 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13925 if (type_die == NULL)
13926 return NULL;
13927 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13928 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13929 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13930 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13931 #if TARGET_SUPPORTS_WIDE_INT == 0
13932 if (!SCALAR_FLOAT_MODE_P (mode))
13934 mem_loc_result->dw_loc_oprnd2.val_class
13935 = dw_val_class_const_double;
13936 mem_loc_result->dw_loc_oprnd2.v.val_double
13937 = rtx_to_double_int (rtl);
13939 else
13940 #endif
13942 unsigned int length = GET_MODE_SIZE (mode);
13943 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13945 insert_float (rtl, array);
13946 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13947 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13948 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13949 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13952 break;
13954 case CONST_WIDE_INT:
13955 if (!dwarf_strict)
13957 dw_die_ref type_die;
13959 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13960 if (type_die == NULL)
13961 return NULL;
13962 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13963 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13964 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13965 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13966 mem_loc_result->dw_loc_oprnd2.val_class
13967 = dw_val_class_wide_int;
13968 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13969 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13971 break;
13973 case EQ:
13974 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13975 break;
13977 case GE:
13978 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13979 break;
13981 case GT:
13982 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13983 break;
13985 case LE:
13986 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13987 break;
13989 case LT:
13990 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13991 break;
13993 case NE:
13994 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13995 break;
13997 case GEU:
13998 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13999 break;
14001 case GTU:
14002 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14003 break;
14005 case LEU:
14006 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14007 break;
14009 case LTU:
14010 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14011 break;
14013 case UMIN:
14014 case UMAX:
14015 if (!SCALAR_INT_MODE_P (mode))
14016 break;
14017 /* FALLTHRU */
14018 case SMIN:
14019 case SMAX:
14020 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14021 break;
14023 case ZERO_EXTRACT:
14024 case SIGN_EXTRACT:
14025 if (CONST_INT_P (XEXP (rtl, 1))
14026 && CONST_INT_P (XEXP (rtl, 2))
14027 && ((unsigned) INTVAL (XEXP (rtl, 1))
14028 + (unsigned) INTVAL (XEXP (rtl, 2))
14029 <= GET_MODE_BITSIZE (mode))
14030 && SCALAR_INT_MODE_P (mode)
14031 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14032 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14034 int shift, size;
14035 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14036 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14037 if (op0 == 0)
14038 break;
14039 if (GET_CODE (rtl) == SIGN_EXTRACT)
14040 op = DW_OP_shra;
14041 else
14042 op = DW_OP_shr;
14043 mem_loc_result = op0;
14044 size = INTVAL (XEXP (rtl, 1));
14045 shift = INTVAL (XEXP (rtl, 2));
14046 if (BITS_BIG_ENDIAN)
14047 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14048 - shift - size;
14049 if (shift + size != (int) DWARF2_ADDR_SIZE)
14051 add_loc_descr (&mem_loc_result,
14052 int_loc_descriptor (DWARF2_ADDR_SIZE
14053 - shift - size));
14054 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14056 if (size != (int) DWARF2_ADDR_SIZE)
14058 add_loc_descr (&mem_loc_result,
14059 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14060 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14063 break;
14065 case IF_THEN_ELSE:
14067 dw_loc_descr_ref op2, bra_node, drop_node;
14068 op0 = mem_loc_descriptor (XEXP (rtl, 0),
14069 GET_MODE (XEXP (rtl, 0)) == VOIDmode
14070 ? word_mode : GET_MODE (XEXP (rtl, 0)),
14071 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14072 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14073 VAR_INIT_STATUS_INITIALIZED);
14074 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14075 VAR_INIT_STATUS_INITIALIZED);
14076 if (op0 == NULL || op1 == NULL || op2 == NULL)
14077 break;
14079 mem_loc_result = op1;
14080 add_loc_descr (&mem_loc_result, op2);
14081 add_loc_descr (&mem_loc_result, op0);
14082 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14083 add_loc_descr (&mem_loc_result, bra_node);
14084 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14085 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14086 add_loc_descr (&mem_loc_result, drop_node);
14087 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14088 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14090 break;
14092 case FLOAT_EXTEND:
14093 case FLOAT_TRUNCATE:
14094 case FLOAT:
14095 case UNSIGNED_FLOAT:
14096 case FIX:
14097 case UNSIGNED_FIX:
14098 if (!dwarf_strict)
14100 dw_die_ref type_die;
14101 dw_loc_descr_ref cvt;
14103 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14104 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14105 if (op0 == NULL)
14106 break;
14107 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
14108 && (GET_CODE (rtl) == FLOAT
14109 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14110 <= DWARF2_ADDR_SIZE))
14112 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14113 GET_CODE (rtl) == UNSIGNED_FLOAT);
14114 if (type_die == NULL)
14115 break;
14116 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14117 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14118 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14119 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14120 add_loc_descr (&op0, cvt);
14122 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14123 if (type_die == NULL)
14124 break;
14125 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14126 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14127 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14128 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14129 add_loc_descr (&op0, cvt);
14130 if (SCALAR_INT_MODE_P (mode)
14131 && (GET_CODE (rtl) == FIX
14132 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14134 op0 = convert_descriptor_to_mode (mode, op0);
14135 if (op0 == NULL)
14136 break;
14138 mem_loc_result = op0;
14140 break;
14142 case CLZ:
14143 case CTZ:
14144 case FFS:
14145 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14146 break;
14148 case POPCOUNT:
14149 case PARITY:
14150 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14151 break;
14153 case BSWAP:
14154 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14155 break;
14157 case ROTATE:
14158 case ROTATERT:
14159 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14160 break;
14162 case COMPARE:
14163 /* In theory, we could implement the above. */
14164 /* DWARF cannot represent the unsigned compare operations
14165 natively. */
14166 case SS_MULT:
14167 case US_MULT:
14168 case SS_DIV:
14169 case US_DIV:
14170 case SS_PLUS:
14171 case US_PLUS:
14172 case SS_MINUS:
14173 case US_MINUS:
14174 case SS_NEG:
14175 case US_NEG:
14176 case SS_ABS:
14177 case SS_ASHIFT:
14178 case US_ASHIFT:
14179 case SS_TRUNCATE:
14180 case US_TRUNCATE:
14181 case UNORDERED:
14182 case ORDERED:
14183 case UNEQ:
14184 case UNGE:
14185 case UNGT:
14186 case UNLE:
14187 case UNLT:
14188 case LTGT:
14189 case FRACT_CONVERT:
14190 case UNSIGNED_FRACT_CONVERT:
14191 case SAT_FRACT:
14192 case UNSIGNED_SAT_FRACT:
14193 case SQRT:
14194 case ASM_OPERANDS:
14195 case VEC_MERGE:
14196 case VEC_SELECT:
14197 case VEC_CONCAT:
14198 case VEC_DUPLICATE:
14199 case UNSPEC:
14200 case HIGH:
14201 case FMA:
14202 case STRICT_LOW_PART:
14203 case CONST_VECTOR:
14204 case CONST_FIXED:
14205 case CLRSB:
14206 case CLOBBER:
14207 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14208 can't express it in the debug info. This can happen e.g. with some
14209 TLS UNSPECs. */
14210 break;
14212 case CONST_STRING:
14213 resolve_one_addr (&rtl);
14214 goto symref;
14216 default:
14217 if (flag_checking)
14219 print_rtl (stderr, rtl);
14220 gcc_unreachable ();
14222 break;
14225 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14226 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14228 return mem_loc_result;
14231 /* Return a descriptor that describes the concatenation of two locations.
14232 This is typically a complex variable. */
14234 static dw_loc_descr_ref
14235 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14237 dw_loc_descr_ref cc_loc_result = NULL;
14238 dw_loc_descr_ref x0_ref
14239 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14240 dw_loc_descr_ref x1_ref
14241 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14243 if (x0_ref == 0 || x1_ref == 0)
14244 return 0;
14246 cc_loc_result = x0_ref;
14247 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14249 add_loc_descr (&cc_loc_result, x1_ref);
14250 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14252 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14253 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14255 return cc_loc_result;
14258 /* Return a descriptor that describes the concatenation of N
14259 locations. */
14261 static dw_loc_descr_ref
14262 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14264 unsigned int i;
14265 dw_loc_descr_ref cc_loc_result = NULL;
14266 unsigned int n = XVECLEN (concatn, 0);
14268 for (i = 0; i < n; ++i)
14270 dw_loc_descr_ref ref;
14271 rtx x = XVECEXP (concatn, 0, i);
14273 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14274 if (ref == NULL)
14275 return NULL;
14277 add_loc_descr (&cc_loc_result, ref);
14278 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14281 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14282 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14284 return cc_loc_result;
14287 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14288 for DEBUG_IMPLICIT_PTR RTL. */
14290 static dw_loc_descr_ref
14291 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14293 dw_loc_descr_ref ret;
14294 dw_die_ref ref;
14296 if (dwarf_strict)
14297 return NULL;
14298 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14299 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14300 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14301 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14302 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14303 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14304 if (ref)
14306 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14307 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14308 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14310 else
14312 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14313 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14315 return ret;
14318 /* Output a proper Dwarf location descriptor for a variable or parameter
14319 which is either allocated in a register or in a memory location. For a
14320 register, we just generate an OP_REG and the register number. For a
14321 memory location we provide a Dwarf postfix expression describing how to
14322 generate the (dynamic) address of the object onto the address stack.
14324 MODE is mode of the decl if this loc_descriptor is going to be used in
14325 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14326 allowed, VOIDmode otherwise.
14328 If we don't know how to describe it, return 0. */
14330 static dw_loc_descr_ref
14331 loc_descriptor (rtx rtl, machine_mode mode,
14332 enum var_init_status initialized)
14334 dw_loc_descr_ref loc_result = NULL;
14336 switch (GET_CODE (rtl))
14338 case SUBREG:
14339 /* The case of a subreg may arise when we have a local (register)
14340 variable or a formal (register) parameter which doesn't quite fill
14341 up an entire register. For now, just assume that it is
14342 legitimate to make the Dwarf info refer to the whole register which
14343 contains the given subreg. */
14344 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14345 loc_result = loc_descriptor (SUBREG_REG (rtl),
14346 GET_MODE (SUBREG_REG (rtl)), initialized);
14347 else
14348 goto do_default;
14349 break;
14351 case REG:
14352 loc_result = reg_loc_descriptor (rtl, initialized);
14353 break;
14355 case MEM:
14356 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14357 GET_MODE (rtl), initialized);
14358 if (loc_result == NULL)
14359 loc_result = tls_mem_loc_descriptor (rtl);
14360 if (loc_result == NULL)
14362 rtx new_rtl = avoid_constant_pool_reference (rtl);
14363 if (new_rtl != rtl)
14364 loc_result = loc_descriptor (new_rtl, mode, initialized);
14366 break;
14368 case CONCAT:
14369 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14370 initialized);
14371 break;
14373 case CONCATN:
14374 loc_result = concatn_loc_descriptor (rtl, initialized);
14375 break;
14377 case VAR_LOCATION:
14378 /* Single part. */
14379 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14381 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14382 if (GET_CODE (loc) == EXPR_LIST)
14383 loc = XEXP (loc, 0);
14384 loc_result = loc_descriptor (loc, mode, initialized);
14385 break;
14388 rtl = XEXP (rtl, 1);
14389 /* FALLTHRU */
14391 case PARALLEL:
14393 rtvec par_elems = XVEC (rtl, 0);
14394 int num_elem = GET_NUM_ELEM (par_elems);
14395 machine_mode mode;
14396 int i;
14398 /* Create the first one, so we have something to add to. */
14399 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14400 VOIDmode, initialized);
14401 if (loc_result == NULL)
14402 return NULL;
14403 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14404 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14405 for (i = 1; i < num_elem; i++)
14407 dw_loc_descr_ref temp;
14409 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14410 VOIDmode, initialized);
14411 if (temp == NULL)
14412 return NULL;
14413 add_loc_descr (&loc_result, temp);
14414 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14415 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14418 break;
14420 case CONST_INT:
14421 if (mode != VOIDmode && mode != BLKmode)
14422 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14423 INTVAL (rtl));
14424 break;
14426 case CONST_DOUBLE:
14427 if (mode == VOIDmode)
14428 mode = GET_MODE (rtl);
14430 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14432 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14434 /* Note that a CONST_DOUBLE rtx could represent either an integer
14435 or a floating-point constant. A CONST_DOUBLE is used whenever
14436 the constant requires more than one word in order to be
14437 adequately represented. We output CONST_DOUBLEs as blocks. */
14438 loc_result = new_loc_descr (DW_OP_implicit_value,
14439 GET_MODE_SIZE (mode), 0);
14440 #if TARGET_SUPPORTS_WIDE_INT == 0
14441 if (!SCALAR_FLOAT_MODE_P (mode))
14443 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14444 loc_result->dw_loc_oprnd2.v.val_double
14445 = rtx_to_double_int (rtl);
14447 else
14448 #endif
14450 unsigned int length = GET_MODE_SIZE (mode);
14451 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14453 insert_float (rtl, array);
14454 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14455 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14456 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14457 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14460 break;
14462 case CONST_WIDE_INT:
14463 if (mode == VOIDmode)
14464 mode = GET_MODE (rtl);
14466 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14468 loc_result = new_loc_descr (DW_OP_implicit_value,
14469 GET_MODE_SIZE (mode), 0);
14470 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14471 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14472 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14474 break;
14476 case CONST_VECTOR:
14477 if (mode == VOIDmode)
14478 mode = GET_MODE (rtl);
14480 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14482 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14483 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14484 unsigned char *array
14485 = ggc_vec_alloc<unsigned char> (length * elt_size);
14486 unsigned int i;
14487 unsigned char *p;
14488 machine_mode imode = GET_MODE_INNER (mode);
14490 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14491 switch (GET_MODE_CLASS (mode))
14493 case MODE_VECTOR_INT:
14494 for (i = 0, p = array; i < length; i++, p += elt_size)
14496 rtx elt = CONST_VECTOR_ELT (rtl, i);
14497 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14499 break;
14501 case MODE_VECTOR_FLOAT:
14502 for (i = 0, p = array; i < length; i++, p += elt_size)
14504 rtx elt = CONST_VECTOR_ELT (rtl, i);
14505 insert_float (elt, p);
14507 break;
14509 default:
14510 gcc_unreachable ();
14513 loc_result = new_loc_descr (DW_OP_implicit_value,
14514 length * elt_size, 0);
14515 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14516 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14517 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14518 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14520 break;
14522 case CONST:
14523 if (mode == VOIDmode
14524 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14525 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14526 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14528 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14529 break;
14531 /* FALLTHROUGH */
14532 case SYMBOL_REF:
14533 if (!const_ok_for_output (rtl))
14534 break;
14535 case LABEL_REF:
14536 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14537 && (dwarf_version >= 4 || !dwarf_strict))
14539 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14540 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14541 vec_safe_push (used_rtx_array, rtl);
14543 break;
14545 case DEBUG_IMPLICIT_PTR:
14546 loc_result = implicit_ptr_descriptor (rtl, 0);
14547 break;
14549 case PLUS:
14550 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14551 && CONST_INT_P (XEXP (rtl, 1)))
14553 loc_result
14554 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14555 break;
14557 /* FALLTHRU */
14558 do_default:
14559 default:
14560 if ((SCALAR_INT_MODE_P (mode)
14561 && GET_MODE (rtl) == mode
14562 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14563 && dwarf_version >= 4)
14564 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14566 /* Value expression. */
14567 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14568 if (loc_result)
14569 add_loc_descr (&loc_result,
14570 new_loc_descr (DW_OP_stack_value, 0, 0));
14572 break;
14575 return loc_result;
14578 /* We need to figure out what section we should use as the base for the
14579 address ranges where a given location is valid.
14580 1. If this particular DECL has a section associated with it, use that.
14581 2. If this function has a section associated with it, use that.
14582 3. Otherwise, use the text section.
14583 XXX: If you split a variable across multiple sections, we won't notice. */
14585 static const char *
14586 secname_for_decl (const_tree decl)
14588 const char *secname;
14590 if (VAR_OR_FUNCTION_DECL_P (decl)
14591 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14592 && DECL_SECTION_NAME (decl))
14593 secname = DECL_SECTION_NAME (decl);
14594 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14595 secname = DECL_SECTION_NAME (current_function_decl);
14596 else if (cfun && in_cold_section_p)
14597 secname = crtl->subsections.cold_section_label;
14598 else
14599 secname = text_section_label;
14601 return secname;
14604 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14606 static bool
14607 decl_by_reference_p (tree decl)
14609 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14610 || TREE_CODE (decl) == VAR_DECL)
14611 && DECL_BY_REFERENCE (decl));
14614 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14615 for VARLOC. */
14617 static dw_loc_descr_ref
14618 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14619 enum var_init_status initialized)
14621 int have_address = 0;
14622 dw_loc_descr_ref descr;
14623 machine_mode mode;
14625 if (want_address != 2)
14627 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14628 /* Single part. */
14629 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14631 varloc = PAT_VAR_LOCATION_LOC (varloc);
14632 if (GET_CODE (varloc) == EXPR_LIST)
14633 varloc = XEXP (varloc, 0);
14634 mode = GET_MODE (varloc);
14635 if (MEM_P (varloc))
14637 rtx addr = XEXP (varloc, 0);
14638 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14639 mode, initialized);
14640 if (descr)
14641 have_address = 1;
14642 else
14644 rtx x = avoid_constant_pool_reference (varloc);
14645 if (x != varloc)
14646 descr = mem_loc_descriptor (x, mode, VOIDmode,
14647 initialized);
14650 else
14651 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14653 else
14654 return 0;
14656 else
14658 if (GET_CODE (varloc) == VAR_LOCATION)
14659 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14660 else
14661 mode = DECL_MODE (loc);
14662 descr = loc_descriptor (varloc, mode, initialized);
14663 have_address = 1;
14666 if (!descr)
14667 return 0;
14669 if (want_address == 2 && !have_address
14670 && (dwarf_version >= 4 || !dwarf_strict))
14672 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14674 expansion_failed (loc, NULL_RTX,
14675 "DWARF address size mismatch");
14676 return 0;
14678 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14679 have_address = 1;
14681 /* Show if we can't fill the request for an address. */
14682 if (want_address && !have_address)
14684 expansion_failed (loc, NULL_RTX,
14685 "Want address and only have value");
14686 return 0;
14689 /* If we've got an address and don't want one, dereference. */
14690 if (!want_address && have_address)
14692 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14693 enum dwarf_location_atom op;
14695 if (size > DWARF2_ADDR_SIZE || size == -1)
14697 expansion_failed (loc, NULL_RTX,
14698 "DWARF address size mismatch");
14699 return 0;
14701 else if (size == DWARF2_ADDR_SIZE)
14702 op = DW_OP_deref;
14703 else
14704 op = DW_OP_deref_size;
14706 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14709 return descr;
14712 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14713 if it is not possible. */
14715 static dw_loc_descr_ref
14716 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14718 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14719 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14720 else if (dwarf_version >= 3 || !dwarf_strict)
14721 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14722 else
14723 return NULL;
14726 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14727 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14729 static dw_loc_descr_ref
14730 dw_sra_loc_expr (tree decl, rtx loc)
14732 rtx p;
14733 unsigned HOST_WIDE_INT padsize = 0;
14734 dw_loc_descr_ref descr, *descr_tail;
14735 unsigned HOST_WIDE_INT decl_size;
14736 rtx varloc;
14737 enum var_init_status initialized;
14739 if (DECL_SIZE (decl) == NULL
14740 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14741 return NULL;
14743 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14744 descr = NULL;
14745 descr_tail = &descr;
14747 for (p = loc; p; p = XEXP (p, 1))
14749 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14750 rtx loc_note = *decl_piece_varloc_ptr (p);
14751 dw_loc_descr_ref cur_descr;
14752 dw_loc_descr_ref *tail, last = NULL;
14753 unsigned HOST_WIDE_INT opsize = 0;
14755 if (loc_note == NULL_RTX
14756 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14758 padsize += bitsize;
14759 continue;
14761 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14762 varloc = NOTE_VAR_LOCATION (loc_note);
14763 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14764 if (cur_descr == NULL)
14766 padsize += bitsize;
14767 continue;
14770 /* Check that cur_descr either doesn't use
14771 DW_OP_*piece operations, or their sum is equal
14772 to bitsize. Otherwise we can't embed it. */
14773 for (tail = &cur_descr; *tail != NULL;
14774 tail = &(*tail)->dw_loc_next)
14775 if ((*tail)->dw_loc_opc == DW_OP_piece)
14777 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14778 * BITS_PER_UNIT;
14779 last = *tail;
14781 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14783 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14784 last = *tail;
14787 if (last != NULL && opsize != bitsize)
14789 padsize += bitsize;
14790 /* Discard the current piece of the descriptor and release any
14791 addr_table entries it uses. */
14792 remove_loc_list_addr_table_entries (cur_descr);
14793 continue;
14796 /* If there is a hole, add DW_OP_*piece after empty DWARF
14797 expression, which means that those bits are optimized out. */
14798 if (padsize)
14800 if (padsize > decl_size)
14802 remove_loc_list_addr_table_entries (cur_descr);
14803 goto discard_descr;
14805 decl_size -= padsize;
14806 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14807 if (*descr_tail == NULL)
14809 remove_loc_list_addr_table_entries (cur_descr);
14810 goto discard_descr;
14812 descr_tail = &(*descr_tail)->dw_loc_next;
14813 padsize = 0;
14815 *descr_tail = cur_descr;
14816 descr_tail = tail;
14817 if (bitsize > decl_size)
14818 goto discard_descr;
14819 decl_size -= bitsize;
14820 if (last == NULL)
14822 HOST_WIDE_INT offset = 0;
14823 if (GET_CODE (varloc) == VAR_LOCATION
14824 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14826 varloc = PAT_VAR_LOCATION_LOC (varloc);
14827 if (GET_CODE (varloc) == EXPR_LIST)
14828 varloc = XEXP (varloc, 0);
14832 if (GET_CODE (varloc) == CONST
14833 || GET_CODE (varloc) == SIGN_EXTEND
14834 || GET_CODE (varloc) == ZERO_EXTEND)
14835 varloc = XEXP (varloc, 0);
14836 else if (GET_CODE (varloc) == SUBREG)
14837 varloc = SUBREG_REG (varloc);
14838 else
14839 break;
14841 while (1);
14842 /* DW_OP_bit_size offset should be zero for register
14843 or implicit location descriptions and empty location
14844 descriptions, but for memory addresses needs big endian
14845 adjustment. */
14846 if (MEM_P (varloc))
14848 unsigned HOST_WIDE_INT memsize
14849 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14850 if (memsize != bitsize)
14852 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14853 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14854 goto discard_descr;
14855 if (memsize < bitsize)
14856 goto discard_descr;
14857 if (BITS_BIG_ENDIAN)
14858 offset = memsize - bitsize;
14862 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14863 if (*descr_tail == NULL)
14864 goto discard_descr;
14865 descr_tail = &(*descr_tail)->dw_loc_next;
14869 /* If there were any non-empty expressions, add padding till the end of
14870 the decl. */
14871 if (descr != NULL && decl_size != 0)
14873 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14874 if (*descr_tail == NULL)
14875 goto discard_descr;
14877 return descr;
14879 discard_descr:
14880 /* Discard the descriptor and release any addr_table entries it uses. */
14881 remove_loc_list_addr_table_entries (descr);
14882 return NULL;
14885 /* Return the dwarf representation of the location list LOC_LIST of
14886 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14887 function. */
14889 static dw_loc_list_ref
14890 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14892 const char *endname, *secname;
14893 rtx varloc;
14894 enum var_init_status initialized;
14895 struct var_loc_node *node;
14896 dw_loc_descr_ref descr;
14897 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14898 dw_loc_list_ref list = NULL;
14899 dw_loc_list_ref *listp = &list;
14901 /* Now that we know what section we are using for a base,
14902 actually construct the list of locations.
14903 The first location information is what is passed to the
14904 function that creates the location list, and the remaining
14905 locations just get added on to that list.
14906 Note that we only know the start address for a location
14907 (IE location changes), so to build the range, we use
14908 the range [current location start, next location start].
14909 This means we have to special case the last node, and generate
14910 a range of [last location start, end of function label]. */
14912 secname = secname_for_decl (decl);
14914 for (node = loc_list->first; node; node = node->next)
14915 if (GET_CODE (node->loc) == EXPR_LIST
14916 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14918 if (GET_CODE (node->loc) == EXPR_LIST)
14920 /* This requires DW_OP_{,bit_}piece, which is not usable
14921 inside DWARF expressions. */
14922 if (want_address != 2)
14923 continue;
14924 descr = dw_sra_loc_expr (decl, node->loc);
14925 if (descr == NULL)
14926 continue;
14928 else
14930 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14931 varloc = NOTE_VAR_LOCATION (node->loc);
14932 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14934 if (descr)
14936 bool range_across_switch = false;
14937 /* If section switch happens in between node->label
14938 and node->next->label (or end of function) and
14939 we can't emit it as a single entry list,
14940 emit two ranges, first one ending at the end
14941 of first partition and second one starting at the
14942 beginning of second partition. */
14943 if (node == loc_list->last_before_switch
14944 && (node != loc_list->first || loc_list->first->next)
14945 && current_function_decl)
14947 endname = cfun->fde->dw_fde_end;
14948 range_across_switch = true;
14950 /* The variable has a location between NODE->LABEL and
14951 NODE->NEXT->LABEL. */
14952 else if (node->next)
14953 endname = node->next->label;
14954 /* If the variable has a location at the last label
14955 it keeps its location until the end of function. */
14956 else if (!current_function_decl)
14957 endname = text_end_label;
14958 else
14960 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14961 current_function_funcdef_no);
14962 endname = ggc_strdup (label_id);
14965 *listp = new_loc_list (descr, node->label, endname, secname);
14966 if (TREE_CODE (decl) == PARM_DECL
14967 && node == loc_list->first
14968 && NOTE_P (node->loc)
14969 && strcmp (node->label, endname) == 0)
14970 (*listp)->force = true;
14971 listp = &(*listp)->dw_loc_next;
14973 if (range_across_switch)
14975 if (GET_CODE (node->loc) == EXPR_LIST)
14976 descr = dw_sra_loc_expr (decl, node->loc);
14977 else
14979 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14980 varloc = NOTE_VAR_LOCATION (node->loc);
14981 descr = dw_loc_list_1 (decl, varloc, want_address,
14982 initialized);
14984 gcc_assert (descr);
14985 /* The variable has a location between NODE->LABEL and
14986 NODE->NEXT->LABEL. */
14987 if (node->next)
14988 endname = node->next->label;
14989 else
14990 endname = cfun->fde->dw_fde_second_end;
14991 *listp = new_loc_list (descr,
14992 cfun->fde->dw_fde_second_begin,
14993 endname, secname);
14994 listp = &(*listp)->dw_loc_next;
14999 /* Try to avoid the overhead of a location list emitting a location
15000 expression instead, but only if we didn't have more than one
15001 location entry in the first place. If some entries were not
15002 representable, we don't want to pretend a single entry that was
15003 applies to the entire scope in which the variable is
15004 available. */
15005 if (list && loc_list->first->next)
15006 gen_llsym (list);
15008 return list;
15011 /* Return if the loc_list has only single element and thus can be represented
15012 as location description. */
15014 static bool
15015 single_element_loc_list_p (dw_loc_list_ref list)
15017 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15018 return !list->ll_symbol;
15021 /* To each location in list LIST add loc descr REF. */
15023 static void
15024 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15026 dw_loc_descr_ref copy;
15027 add_loc_descr (&list->expr, ref);
15028 list = list->dw_loc_next;
15029 while (list)
15031 copy = ggc_alloc<dw_loc_descr_node> ();
15032 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15033 add_loc_descr (&list->expr, copy);
15034 while (copy->dw_loc_next)
15036 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15037 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15038 copy->dw_loc_next = new_copy;
15039 copy = new_copy;
15041 list = list->dw_loc_next;
15045 /* Given two lists RET and LIST
15046 produce location list that is result of adding expression in LIST
15047 to expression in RET on each position in program.
15048 Might be destructive on both RET and LIST.
15050 TODO: We handle only simple cases of RET or LIST having at most one
15051 element. General case would inolve sorting the lists in program order
15052 and merging them that will need some additional work.
15053 Adding that will improve quality of debug info especially for SRA-ed
15054 structures. */
15056 static void
15057 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15059 if (!list)
15060 return;
15061 if (!*ret)
15063 *ret = list;
15064 return;
15066 if (!list->dw_loc_next)
15068 add_loc_descr_to_each (*ret, list->expr);
15069 return;
15071 if (!(*ret)->dw_loc_next)
15073 add_loc_descr_to_each (list, (*ret)->expr);
15074 *ret = list;
15075 return;
15077 expansion_failed (NULL_TREE, NULL_RTX,
15078 "Don't know how to merge two non-trivial"
15079 " location lists.\n");
15080 *ret = NULL;
15081 return;
15084 /* LOC is constant expression. Try a luck, look it up in constant
15085 pool and return its loc_descr of its address. */
15087 static dw_loc_descr_ref
15088 cst_pool_loc_descr (tree loc)
15090 /* Get an RTL for this, if something has been emitted. */
15091 rtx rtl = lookup_constant_def (loc);
15093 if (!rtl || !MEM_P (rtl))
15095 gcc_assert (!rtl);
15096 return 0;
15098 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15100 /* TODO: We might get more coverage if we was actually delaying expansion
15101 of all expressions till end of compilation when constant pools are fully
15102 populated. */
15103 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15105 expansion_failed (loc, NULL_RTX,
15106 "CST value in contant pool but not marked.");
15107 return 0;
15109 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15110 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15113 /* Return dw_loc_list representing address of addr_expr LOC
15114 by looking for inner INDIRECT_REF expression and turning
15115 it into simple arithmetics.
15117 See loc_list_from_tree for the meaning of CONTEXT. */
15119 static dw_loc_list_ref
15120 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15121 const loc_descr_context *context)
15123 tree obj, offset;
15124 HOST_WIDE_INT bitsize, bitpos, bytepos;
15125 machine_mode mode;
15126 int unsignedp, reversep, volatilep = 0;
15127 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15129 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15130 &bitsize, &bitpos, &offset, &mode,
15131 &unsignedp, &reversep, &volatilep);
15132 STRIP_NOPS (obj);
15133 if (bitpos % BITS_PER_UNIT)
15135 expansion_failed (loc, NULL_RTX, "bitfield access");
15136 return 0;
15138 if (!INDIRECT_REF_P (obj))
15140 expansion_failed (obj,
15141 NULL_RTX, "no indirect ref in inner refrence");
15142 return 0;
15144 if (!offset && !bitpos)
15145 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15146 context);
15147 else if (toplev
15148 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15149 && (dwarf_version >= 4 || !dwarf_strict))
15151 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15152 if (!list_ret)
15153 return 0;
15154 if (offset)
15156 /* Variable offset. */
15157 list_ret1 = loc_list_from_tree (offset, 0, context);
15158 if (list_ret1 == 0)
15159 return 0;
15160 add_loc_list (&list_ret, list_ret1);
15161 if (!list_ret)
15162 return 0;
15163 add_loc_descr_to_each (list_ret,
15164 new_loc_descr (DW_OP_plus, 0, 0));
15166 bytepos = bitpos / BITS_PER_UNIT;
15167 if (bytepos > 0)
15168 add_loc_descr_to_each (list_ret,
15169 new_loc_descr (DW_OP_plus_uconst,
15170 bytepos, 0));
15171 else if (bytepos < 0)
15172 loc_list_plus_const (list_ret, bytepos);
15173 add_loc_descr_to_each (list_ret,
15174 new_loc_descr (DW_OP_stack_value, 0, 0));
15176 return list_ret;
15179 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15180 all operations from LOC are nops, move to the last one. Insert in NOPS all
15181 operations that are skipped. */
15183 static void
15184 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15185 hash_set<dw_loc_descr_ref> &nops)
15187 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15189 nops.add (loc);
15190 loc = loc->dw_loc_next;
15194 /* Helper for loc_descr_without_nops: free the location description operation
15195 P. */
15197 bool
15198 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15200 ggc_free (loc);
15201 return true;
15204 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15205 finishes LOC. */
15207 static void
15208 loc_descr_without_nops (dw_loc_descr_ref &loc)
15210 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15211 return;
15213 /* Set of all DW_OP_nop operations we remove. */
15214 hash_set<dw_loc_descr_ref> nops;
15216 /* First, strip all prefix NOP operations in order to keep the head of the
15217 operations list. */
15218 loc_descr_to_next_no_nop (loc, nops);
15220 for (dw_loc_descr_ref cur = loc; cur != NULL;)
15222 /* For control flow operations: strip "prefix" nops in destination
15223 labels. */
15224 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15225 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15226 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15227 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15229 /* Do the same for the operations that follow, then move to the next
15230 iteration. */
15231 if (cur->dw_loc_next != NULL)
15232 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15233 cur = cur->dw_loc_next;
15236 nops.traverse<void *, free_loc_descr> (NULL);
15240 struct dwarf_procedure_info;
15242 /* Helper structure for location descriptions generation. */
15243 struct loc_descr_context
15245 /* The type that is implicitly referenced by DW_OP_push_object_address, or
15246 NULL_TREE if DW_OP_push_object_address in invalid for this location
15247 description. This is used when processing PLACEHOLDER_EXPR nodes. */
15248 tree context_type;
15249 /* The ..._DECL node that should be translated as a
15250 DW_OP_push_object_address operation. */
15251 tree base_decl;
15252 /* Information about the DWARF procedure we are currently generating. NULL if
15253 we are not generating a DWARF procedure. */
15254 struct dwarf_procedure_info *dpi;
15257 /* DWARF procedures generation
15259 DWARF expressions (aka. location descriptions) are used to encode variable
15260 things such as sizes or offsets. Such computations can have redundant parts
15261 that can be factorized in order to reduce the size of the output debug
15262 information. This is the whole point of DWARF procedures.
15264 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15265 already factorized into functions ("size functions") in order to handle very
15266 big and complex types. Such functions are quite simple: they have integral
15267 arguments, they return an integral result and their body contains only a
15268 return statement with arithmetic expressions. This is the only kind of
15269 function we are interested in translating into DWARF procedures, here.
15271 DWARF expressions and DWARF procedure are executed using a stack, so we have
15272 to define some calling convention for them to interact. Let's say that:
15274 - Before calling a DWARF procedure, DWARF expressions must push on the stack
15275 all arguments in reverse order (right-to-left) so that when the DWARF
15276 procedure execution starts, the first argument is the top of the stack.
15278 - Then, when returning, the DWARF procedure must have consumed all arguments
15279 on the stack, must have pushed the result and touched nothing else.
15281 - Each integral argument and the result are integral types can be hold in a
15282 single stack slot.
15284 - We call "frame offset" the number of stack slots that are "under DWARF
15285 procedure control": it includes the arguments slots, the temporaries and
15286 the result slot. Thus, it is equal to the number of arguments when the
15287 procedure execution starts and must be equal to one (the result) when it
15288 returns. */
15290 /* Helper structure used when generating operations for a DWARF procedure. */
15291 struct dwarf_procedure_info
15293 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15294 currently translated. */
15295 tree fndecl;
15296 /* The number of arguments FNDECL takes. */
15297 unsigned args_count;
15300 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
15301 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
15302 equate it to this DIE. */
15304 static dw_die_ref
15305 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15306 dw_die_ref parent_die)
15308 dw_die_ref dwarf_proc_die;
15310 if ((dwarf_version < 3 && dwarf_strict)
15311 || location == NULL)
15312 return NULL;
15314 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
15315 if (fndecl)
15316 equate_decl_number_to_die (fndecl, dwarf_proc_die);
15317 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15318 return dwarf_proc_die;
15321 /* Return whether TYPE is a supported type as a DWARF procedure argument
15322 type or return type (we handle only scalar types and pointer types that
15323 aren't wider than the DWARF expression evaluation stack. */
15325 static bool
15326 is_handled_procedure_type (tree type)
15328 return ((INTEGRAL_TYPE_P (type)
15329 || TREE_CODE (type) == OFFSET_TYPE
15330 || TREE_CODE (type) == POINTER_TYPE)
15331 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15334 /* Helper for resolve_args_picking: do the same but stop when coming across
15335 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
15336 offset *before* evaluating the corresponding operation. */
15338 static bool
15339 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15340 struct dwarf_procedure_info *dpi,
15341 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
15343 /* The "frame_offset" identifier is already used to name a macro... */
15344 unsigned frame_offset_ = initial_frame_offset;
15345 dw_loc_descr_ref l;
15347 for (l = loc; l != NULL;)
15349 bool existed;
15350 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
15352 /* If we already met this node, there is nothing to compute anymore. */
15353 if (existed)
15355 /* Make sure that the stack size is consistent wherever the execution
15356 flow comes from. */
15357 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
15358 break;
15360 l_frame_offset = frame_offset_;
15362 /* If needed, relocate the picking offset with respect to the frame
15363 offset. */
15364 if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15366 /* frame_offset_ is the size of the current stack frame, including
15367 incoming arguments. Besides, the arguments are pushed
15368 right-to-left. Thus, in order to access the Nth argument from
15369 this operation node, the picking has to skip temporaries *plus*
15370 one stack slot per argument (0 for the first one, 1 for the second
15371 one, etc.).
15373 The targetted argument number (N) is already set as the operand,
15374 and the number of temporaries can be computed with:
15375 frame_offsets_ - dpi->args_count */
15376 l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15378 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
15379 if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15380 return false;
15383 /* Update frame_offset according to the effect the current operation has
15384 on the stack. */
15385 switch (l->dw_loc_opc)
15387 case DW_OP_deref:
15388 case DW_OP_swap:
15389 case DW_OP_rot:
15390 case DW_OP_abs:
15391 case DW_OP_neg:
15392 case DW_OP_not:
15393 case DW_OP_plus_uconst:
15394 case DW_OP_skip:
15395 case DW_OP_reg0:
15396 case DW_OP_reg1:
15397 case DW_OP_reg2:
15398 case DW_OP_reg3:
15399 case DW_OP_reg4:
15400 case DW_OP_reg5:
15401 case DW_OP_reg6:
15402 case DW_OP_reg7:
15403 case DW_OP_reg8:
15404 case DW_OP_reg9:
15405 case DW_OP_reg10:
15406 case DW_OP_reg11:
15407 case DW_OP_reg12:
15408 case DW_OP_reg13:
15409 case DW_OP_reg14:
15410 case DW_OP_reg15:
15411 case DW_OP_reg16:
15412 case DW_OP_reg17:
15413 case DW_OP_reg18:
15414 case DW_OP_reg19:
15415 case DW_OP_reg20:
15416 case DW_OP_reg21:
15417 case DW_OP_reg22:
15418 case DW_OP_reg23:
15419 case DW_OP_reg24:
15420 case DW_OP_reg25:
15421 case DW_OP_reg26:
15422 case DW_OP_reg27:
15423 case DW_OP_reg28:
15424 case DW_OP_reg29:
15425 case DW_OP_reg30:
15426 case DW_OP_reg31:
15427 case DW_OP_bregx:
15428 case DW_OP_piece:
15429 case DW_OP_deref_size:
15430 case DW_OP_nop:
15431 case DW_OP_form_tls_address:
15432 case DW_OP_bit_piece:
15433 case DW_OP_implicit_value:
15434 case DW_OP_stack_value:
15435 break;
15437 case DW_OP_addr:
15438 case DW_OP_const1u:
15439 case DW_OP_const1s:
15440 case DW_OP_const2u:
15441 case DW_OP_const2s:
15442 case DW_OP_const4u:
15443 case DW_OP_const4s:
15444 case DW_OP_const8u:
15445 case DW_OP_const8s:
15446 case DW_OP_constu:
15447 case DW_OP_consts:
15448 case DW_OP_dup:
15449 case DW_OP_over:
15450 case DW_OP_pick:
15451 case DW_OP_lit0:
15452 case DW_OP_lit1:
15453 case DW_OP_lit2:
15454 case DW_OP_lit3:
15455 case DW_OP_lit4:
15456 case DW_OP_lit5:
15457 case DW_OP_lit6:
15458 case DW_OP_lit7:
15459 case DW_OP_lit8:
15460 case DW_OP_lit9:
15461 case DW_OP_lit10:
15462 case DW_OP_lit11:
15463 case DW_OP_lit12:
15464 case DW_OP_lit13:
15465 case DW_OP_lit14:
15466 case DW_OP_lit15:
15467 case DW_OP_lit16:
15468 case DW_OP_lit17:
15469 case DW_OP_lit18:
15470 case DW_OP_lit19:
15471 case DW_OP_lit20:
15472 case DW_OP_lit21:
15473 case DW_OP_lit22:
15474 case DW_OP_lit23:
15475 case DW_OP_lit24:
15476 case DW_OP_lit25:
15477 case DW_OP_lit26:
15478 case DW_OP_lit27:
15479 case DW_OP_lit28:
15480 case DW_OP_lit29:
15481 case DW_OP_lit30:
15482 case DW_OP_lit31:
15483 case DW_OP_breg0:
15484 case DW_OP_breg1:
15485 case DW_OP_breg2:
15486 case DW_OP_breg3:
15487 case DW_OP_breg4:
15488 case DW_OP_breg5:
15489 case DW_OP_breg6:
15490 case DW_OP_breg7:
15491 case DW_OP_breg8:
15492 case DW_OP_breg9:
15493 case DW_OP_breg10:
15494 case DW_OP_breg11:
15495 case DW_OP_breg12:
15496 case DW_OP_breg13:
15497 case DW_OP_breg14:
15498 case DW_OP_breg15:
15499 case DW_OP_breg16:
15500 case DW_OP_breg17:
15501 case DW_OP_breg18:
15502 case DW_OP_breg19:
15503 case DW_OP_breg20:
15504 case DW_OP_breg21:
15505 case DW_OP_breg22:
15506 case DW_OP_breg23:
15507 case DW_OP_breg24:
15508 case DW_OP_breg25:
15509 case DW_OP_breg26:
15510 case DW_OP_breg27:
15511 case DW_OP_breg28:
15512 case DW_OP_breg29:
15513 case DW_OP_breg30:
15514 case DW_OP_breg31:
15515 case DW_OP_fbreg:
15516 case DW_OP_push_object_address:
15517 case DW_OP_call_frame_cfa:
15518 ++frame_offset_;
15519 break;
15521 case DW_OP_drop:
15522 case DW_OP_xderef:
15523 case DW_OP_and:
15524 case DW_OP_div:
15525 case DW_OP_minus:
15526 case DW_OP_mod:
15527 case DW_OP_mul:
15528 case DW_OP_or:
15529 case DW_OP_plus:
15530 case DW_OP_shl:
15531 case DW_OP_shr:
15532 case DW_OP_shra:
15533 case DW_OP_xor:
15534 case DW_OP_bra:
15535 case DW_OP_eq:
15536 case DW_OP_ge:
15537 case DW_OP_gt:
15538 case DW_OP_le:
15539 case DW_OP_lt:
15540 case DW_OP_ne:
15541 case DW_OP_regx:
15542 case DW_OP_xderef_size:
15543 --frame_offset_;
15544 break;
15546 case DW_OP_call2:
15547 case DW_OP_call4:
15548 case DW_OP_call_ref:
15550 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15551 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15553 if (stack_usage == NULL)
15554 return false;
15555 frame_offset_ += *stack_usage;
15556 break;
15559 case DW_OP_GNU_push_tls_address:
15560 case DW_OP_GNU_uninit:
15561 case DW_OP_GNU_encoded_addr:
15562 case DW_OP_GNU_implicit_pointer:
15563 case DW_OP_GNU_entry_value:
15564 case DW_OP_GNU_const_type:
15565 case DW_OP_GNU_regval_type:
15566 case DW_OP_GNU_deref_type:
15567 case DW_OP_GNU_convert:
15568 case DW_OP_GNU_reinterpret:
15569 case DW_OP_GNU_parameter_ref:
15570 /* loc_list_from_tree will probably not output these operations for
15571 size functions, so assume they will not appear here. */
15572 /* Fall through... */
15574 default:
15575 gcc_unreachable ();
15578 /* Now, follow the control flow (except subroutine calls). */
15579 switch (l->dw_loc_opc)
15581 case DW_OP_bra:
15582 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15583 frame_offsets))
15584 return false;
15585 /* Fall through. */
15587 case DW_OP_skip:
15588 l = l->dw_loc_oprnd1.v.val_loc;
15589 break;
15591 case DW_OP_stack_value:
15592 return true;
15594 default:
15595 l = l->dw_loc_next;
15596 break;
15600 return true;
15603 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15604 operations) in order to resolve the operand of DW_OP_pick operations that
15605 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
15606 offset *before* LOC is executed. Return if all relocations were
15607 successful. */
15609 static bool
15610 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15611 struct dwarf_procedure_info *dpi)
15613 /* Associate to all visited operations the frame offset *before* evaluating
15614 this operation. */
15615 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
15617 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
15618 frame_offsets);
15621 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15622 Return NULL if it is not possible. */
15624 static dw_die_ref
15625 function_to_dwarf_procedure (tree fndecl)
15627 struct loc_descr_context ctx;
15628 struct dwarf_procedure_info dpi;
15629 dw_die_ref dwarf_proc_die;
15630 tree tree_body = DECL_SAVED_TREE (fndecl);
15631 dw_loc_descr_ref loc_body, epilogue;
15633 tree cursor;
15634 unsigned i;
15636 /* Do not generate multiple DWARF procedures for the same function
15637 declaration. */
15638 dwarf_proc_die = lookup_decl_die (fndecl);
15639 if (dwarf_proc_die != NULL)
15640 return dwarf_proc_die;
15642 /* DWARF procedures are available starting with the DWARFv3 standard. */
15643 if (dwarf_version < 3 && dwarf_strict)
15644 return NULL;
15646 /* We handle only functions for which we still have a body, that return a
15647 supported type and that takes arguments with supported types. Note that
15648 there is no point translating functions that return nothing. */
15649 if (tree_body == NULL_TREE
15650 || DECL_RESULT (fndecl) == NULL_TREE
15651 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15652 return NULL;
15654 for (cursor = DECL_ARGUMENTS (fndecl);
15655 cursor != NULL_TREE;
15656 cursor = TREE_CHAIN (cursor))
15657 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15658 return NULL;
15660 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
15661 if (TREE_CODE (tree_body) != RETURN_EXPR)
15662 return NULL;
15663 tree_body = TREE_OPERAND (tree_body, 0);
15664 if (TREE_CODE (tree_body) != MODIFY_EXPR
15665 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15666 return NULL;
15667 tree_body = TREE_OPERAND (tree_body, 1);
15669 /* Try to translate the body expression itself. Note that this will probably
15670 cause an infinite recursion if its call graph has a cycle. This is very
15671 unlikely for size functions, however, so don't bother with such things at
15672 the moment. */
15673 ctx.context_type = NULL_TREE;
15674 ctx.base_decl = NULL_TREE;
15675 ctx.dpi = &dpi;
15676 dpi.fndecl = fndecl;
15677 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15678 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15679 if (!loc_body)
15680 return NULL;
15682 /* After evaluating all operands in "loc_body", we should still have on the
15683 stack all arguments plus the desired function result (top of the stack).
15684 Generate code in order to keep only the result in our stack frame. */
15685 epilogue = NULL;
15686 for (i = 0; i < dpi.args_count; ++i)
15688 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15689 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15690 op_couple->dw_loc_next->dw_loc_next = epilogue;
15691 epilogue = op_couple;
15693 add_loc_descr (&loc_body, epilogue);
15694 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15695 return NULL;
15697 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15698 because they are considered useful. Now there is an epilogue, they are
15699 not anymore, so give it another try. */
15700 loc_descr_without_nops (loc_body);
15702 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15703 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
15704 though, given that size functions do not come from source, so they should
15705 not have a dedicated DW_TAG_subprogram DIE. */
15706 dwarf_proc_die
15707 = new_dwarf_proc_die (loc_body, fndecl,
15708 get_context_die (DECL_CONTEXT (fndecl)));
15710 /* The called DWARF procedure consumes one stack slot per argument and
15711 returns one stack slot. */
15712 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15714 return dwarf_proc_die;
15718 /* Generate Dwarf location list representing LOC.
15719 If WANT_ADDRESS is false, expression computing LOC will be computed
15720 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15721 if WANT_ADDRESS is 2, expression computing address useable in location
15722 will be returned (i.e. DW_OP_reg can be used
15723 to refer to register values).
15725 CONTEXT provides information to customize the location descriptions
15726 generation. Its context_type field specifies what type is implicitly
15727 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
15728 will not be generated.
15730 Its DPI field determines whether we are generating a DWARF expression for a
15731 DWARF procedure, so PARM_DECL references are processed specifically.
15733 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15734 and dpi fields were null. */
15736 static dw_loc_list_ref
15737 loc_list_from_tree_1 (tree loc, int want_address,
15738 const struct loc_descr_context *context)
15740 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15741 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15742 int have_address = 0;
15743 enum dwarf_location_atom op;
15745 /* ??? Most of the time we do not take proper care for sign/zero
15746 extending the values properly. Hopefully this won't be a real
15747 problem... */
15749 if (context != NULL
15750 && context->base_decl == loc
15751 && want_address == 0)
15753 if (dwarf_version >= 3 || !dwarf_strict)
15754 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15755 NULL, NULL, NULL);
15756 else
15757 return NULL;
15760 switch (TREE_CODE (loc))
15762 case ERROR_MARK:
15763 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15764 return 0;
15766 case PLACEHOLDER_EXPR:
15767 /* This case involves extracting fields from an object to determine the
15768 position of other fields. It is supposed to appear only as the first
15769 operand of COMPONENT_REF nodes and to reference precisely the type
15770 that the context allows. */
15771 if (context != NULL
15772 && TREE_TYPE (loc) == context->context_type
15773 && want_address >= 1)
15775 if (dwarf_version >= 3 || !dwarf_strict)
15777 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15778 have_address = 1;
15779 break;
15781 else
15782 return NULL;
15784 else
15785 expansion_failed (loc, NULL_RTX,
15786 "PLACEHOLDER_EXPR for an unexpected type");
15787 break;
15789 case CALL_EXPR:
15791 const int nargs = call_expr_nargs (loc);
15792 tree callee = get_callee_fndecl (loc);
15793 int i;
15794 dw_die_ref dwarf_proc;
15796 if (callee == NULL_TREE)
15797 goto call_expansion_failed;
15799 /* We handle only functions that return an integer. */
15800 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15801 goto call_expansion_failed;
15803 dwarf_proc = function_to_dwarf_procedure (callee);
15804 if (dwarf_proc == NULL)
15805 goto call_expansion_failed;
15807 /* Evaluate arguments right-to-left so that the first argument will
15808 be the top-most one on the stack. */
15809 for (i = nargs - 1; i >= 0; --i)
15811 dw_loc_descr_ref loc_descr
15812 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15813 context);
15815 if (loc_descr == NULL)
15816 goto call_expansion_failed;
15818 add_loc_descr (&ret, loc_descr);
15821 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15822 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15823 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15824 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15825 add_loc_descr (&ret, ret1);
15826 break;
15828 call_expansion_failed:
15829 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15830 /* There are no opcodes for these operations. */
15831 return 0;
15834 case PREINCREMENT_EXPR:
15835 case PREDECREMENT_EXPR:
15836 case POSTINCREMENT_EXPR:
15837 case POSTDECREMENT_EXPR:
15838 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15839 /* There are no opcodes for these operations. */
15840 return 0;
15842 case ADDR_EXPR:
15843 /* If we already want an address, see if there is INDIRECT_REF inside
15844 e.g. for &this->field. */
15845 if (want_address)
15847 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15848 (loc, want_address == 2, context);
15849 if (list_ret)
15850 have_address = 1;
15851 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15852 && (ret = cst_pool_loc_descr (loc)))
15853 have_address = 1;
15855 /* Otherwise, process the argument and look for the address. */
15856 if (!list_ret && !ret)
15857 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15858 else
15860 if (want_address)
15861 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15862 return NULL;
15864 break;
15866 case VAR_DECL:
15867 if (DECL_THREAD_LOCAL_P (loc))
15869 rtx rtl;
15870 enum dwarf_location_atom tls_op;
15871 enum dtprel_bool dtprel = dtprel_false;
15873 if (targetm.have_tls)
15875 /* If this is not defined, we have no way to emit the
15876 data. */
15877 if (!targetm.asm_out.output_dwarf_dtprel)
15878 return 0;
15880 /* The way DW_OP_GNU_push_tls_address is specified, we
15881 can only look up addresses of objects in the current
15882 module. We used DW_OP_addr as first op, but that's
15883 wrong, because DW_OP_addr is relocated by the debug
15884 info consumer, while DW_OP_GNU_push_tls_address
15885 operand shouldn't be. */
15886 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15887 return 0;
15888 dtprel = dtprel_true;
15889 tls_op = DW_OP_GNU_push_tls_address;
15891 else
15893 if (!targetm.emutls.debug_form_tls_address
15894 || !(dwarf_version >= 3 || !dwarf_strict))
15895 return 0;
15896 /* We stuffed the control variable into the DECL_VALUE_EXPR
15897 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15898 no longer appear in gimple code. We used the control
15899 variable in specific so that we could pick it up here. */
15900 loc = DECL_VALUE_EXPR (loc);
15901 tls_op = DW_OP_form_tls_address;
15904 rtl = rtl_for_decl_location (loc);
15905 if (rtl == NULL_RTX)
15906 return 0;
15908 if (!MEM_P (rtl))
15909 return 0;
15910 rtl = XEXP (rtl, 0);
15911 if (! CONSTANT_P (rtl))
15912 return 0;
15914 ret = new_addr_loc_descr (rtl, dtprel);
15915 ret1 = new_loc_descr (tls_op, 0, 0);
15916 add_loc_descr (&ret, ret1);
15918 have_address = 1;
15919 break;
15921 /* FALLTHRU */
15923 case PARM_DECL:
15924 if (context != NULL && context->dpi != NULL
15925 && DECL_CONTEXT (loc) == context->dpi->fndecl)
15927 /* We are generating code for a DWARF procedure and we want to access
15928 one of its arguments: find the appropriate argument offset and let
15929 the resolve_args_picking pass compute the offset that complies
15930 with the stack frame size. */
15931 unsigned i = 0;
15932 tree cursor;
15934 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15935 cursor != NULL_TREE && cursor != loc;
15936 cursor = TREE_CHAIN (cursor), ++i)
15938 /* If we are translating a DWARF procedure, all referenced parameters
15939 must belong to the current function. */
15940 gcc_assert (cursor != NULL_TREE);
15942 ret = new_loc_descr (DW_OP_pick, i, 0);
15943 ret->frame_offset_rel = 1;
15944 break;
15946 /* FALLTHRU */
15948 case RESULT_DECL:
15949 if (DECL_HAS_VALUE_EXPR_P (loc))
15950 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15951 want_address, context);
15952 /* FALLTHRU */
15954 case FUNCTION_DECL:
15956 rtx rtl;
15957 var_loc_list *loc_list = lookup_decl_loc (loc);
15959 if (loc_list && loc_list->first)
15961 list_ret = dw_loc_list (loc_list, loc, want_address);
15962 have_address = want_address != 0;
15963 break;
15965 rtl = rtl_for_decl_location (loc);
15966 if (rtl == NULL_RTX)
15968 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15969 return 0;
15971 else if (CONST_INT_P (rtl))
15973 HOST_WIDE_INT val = INTVAL (rtl);
15974 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15975 val &= GET_MODE_MASK (DECL_MODE (loc));
15976 ret = int_loc_descriptor (val);
15978 else if (GET_CODE (rtl) == CONST_STRING)
15980 expansion_failed (loc, NULL_RTX, "CONST_STRING");
15981 return 0;
15983 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15984 ret = new_addr_loc_descr (rtl, dtprel_false);
15985 else
15987 machine_mode mode, mem_mode;
15989 /* Certain constructs can only be represented at top-level. */
15990 if (want_address == 2)
15992 ret = loc_descriptor (rtl, VOIDmode,
15993 VAR_INIT_STATUS_INITIALIZED);
15994 have_address = 1;
15996 else
15998 mode = GET_MODE (rtl);
15999 mem_mode = VOIDmode;
16000 if (MEM_P (rtl))
16002 mem_mode = mode;
16003 mode = get_address_mode (rtl);
16004 rtl = XEXP (rtl, 0);
16005 have_address = 1;
16007 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16008 VAR_INIT_STATUS_INITIALIZED);
16010 if (!ret)
16011 expansion_failed (loc, rtl,
16012 "failed to produce loc descriptor for rtl");
16015 break;
16017 case MEM_REF:
16018 if (!integer_zerop (TREE_OPERAND (loc, 1)))
16020 have_address = 1;
16021 goto do_plus;
16023 /* Fallthru. */
16024 case INDIRECT_REF:
16025 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16026 have_address = 1;
16027 break;
16029 case TARGET_MEM_REF:
16030 case SSA_NAME:
16031 case DEBUG_EXPR_DECL:
16032 return NULL;
16034 case COMPOUND_EXPR:
16035 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16036 context);
16038 CASE_CONVERT:
16039 case VIEW_CONVERT_EXPR:
16040 case SAVE_EXPR:
16041 case MODIFY_EXPR:
16042 case NON_LVALUE_EXPR:
16043 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16044 context);
16046 case COMPONENT_REF:
16047 case BIT_FIELD_REF:
16048 case ARRAY_REF:
16049 case ARRAY_RANGE_REF:
16050 case REALPART_EXPR:
16051 case IMAGPART_EXPR:
16053 tree obj, offset;
16054 HOST_WIDE_INT bitsize, bitpos, bytepos;
16055 machine_mode mode;
16056 int unsignedp, reversep, volatilep = 0;
16058 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16059 &unsignedp, &reversep, &volatilep);
16061 gcc_assert (obj != loc);
16063 list_ret = loc_list_from_tree_1 (obj,
16064 want_address == 2
16065 && !bitpos && !offset ? 2 : 1,
16066 context);
16067 /* TODO: We can extract value of the small expression via shifting even
16068 for nonzero bitpos. */
16069 if (list_ret == 0)
16070 return 0;
16071 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16073 expansion_failed (loc, NULL_RTX,
16074 "bitfield access");
16075 return 0;
16078 if (offset != NULL_TREE)
16080 /* Variable offset. */
16081 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16082 if (list_ret1 == 0)
16083 return 0;
16084 add_loc_list (&list_ret, list_ret1);
16085 if (!list_ret)
16086 return 0;
16087 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16090 bytepos = bitpos / BITS_PER_UNIT;
16091 if (bytepos > 0)
16092 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16093 else if (bytepos < 0)
16094 loc_list_plus_const (list_ret, bytepos);
16096 have_address = 1;
16097 break;
16100 case INTEGER_CST:
16101 if ((want_address || !tree_fits_shwi_p (loc))
16102 && (ret = cst_pool_loc_descr (loc)))
16103 have_address = 1;
16104 else if (want_address == 2
16105 && tree_fits_shwi_p (loc)
16106 && (ret = address_of_int_loc_descriptor
16107 (int_size_in_bytes (TREE_TYPE (loc)),
16108 tree_to_shwi (loc))))
16109 have_address = 1;
16110 else if (tree_fits_shwi_p (loc))
16111 ret = int_loc_descriptor (tree_to_shwi (loc));
16112 else if (tree_fits_uhwi_p (loc))
16113 ret = uint_loc_descriptor (tree_to_uhwi (loc));
16114 else
16116 expansion_failed (loc, NULL_RTX,
16117 "Integer operand is not host integer");
16118 return 0;
16120 break;
16122 case CONSTRUCTOR:
16123 case REAL_CST:
16124 case STRING_CST:
16125 case COMPLEX_CST:
16126 if ((ret = cst_pool_loc_descr (loc)))
16127 have_address = 1;
16128 else if (TREE_CODE (loc) == CONSTRUCTOR)
16130 tree type = TREE_TYPE (loc);
16131 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
16132 unsigned HOST_WIDE_INT offset = 0;
16133 unsigned HOST_WIDE_INT cnt;
16134 constructor_elt *ce;
16136 if (TREE_CODE (type) == RECORD_TYPE)
16138 /* This is very limited, but it's enough to output
16139 pointers to member functions, as long as the
16140 referenced function is defined in the current
16141 translation unit. */
16142 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
16144 tree val = ce->value;
16146 tree field = ce->index;
16148 if (val)
16149 STRIP_NOPS (val);
16151 if (!field || DECL_BIT_FIELD (field))
16153 expansion_failed (loc, NULL_RTX,
16154 "bitfield in record type constructor");
16155 size = offset = (unsigned HOST_WIDE_INT)-1;
16156 ret = NULL;
16157 break;
16160 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16161 unsigned HOST_WIDE_INT pos = int_byte_position (field);
16162 gcc_assert (pos + fieldsize <= size);
16163 if (pos < offset)
16165 expansion_failed (loc, NULL_RTX,
16166 "out-of-order fields in record constructor");
16167 size = offset = (unsigned HOST_WIDE_INT)-1;
16168 ret = NULL;
16169 break;
16171 if (pos > offset)
16173 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
16174 add_loc_descr (&ret, ret1);
16175 offset = pos;
16177 if (val && fieldsize != 0)
16179 ret1 = loc_descriptor_from_tree (val, want_address, context);
16180 if (!ret1)
16182 expansion_failed (loc, NULL_RTX,
16183 "unsupported expression in field");
16184 size = offset = (unsigned HOST_WIDE_INT)-1;
16185 ret = NULL;
16186 break;
16188 add_loc_descr (&ret, ret1);
16190 if (fieldsize)
16192 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
16193 add_loc_descr (&ret, ret1);
16194 offset = pos + fieldsize;
16198 if (offset != size)
16200 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
16201 add_loc_descr (&ret, ret1);
16202 offset = size;
16205 have_address = !!want_address;
16207 else
16208 expansion_failed (loc, NULL_RTX,
16209 "constructor of non-record type");
16211 else
16212 /* We can construct small constants here using int_loc_descriptor. */
16213 expansion_failed (loc, NULL_RTX,
16214 "constructor or constant not in constant pool");
16215 break;
16217 case TRUTH_AND_EXPR:
16218 case TRUTH_ANDIF_EXPR:
16219 case BIT_AND_EXPR:
16220 op = DW_OP_and;
16221 goto do_binop;
16223 case TRUTH_XOR_EXPR:
16224 case BIT_XOR_EXPR:
16225 op = DW_OP_xor;
16226 goto do_binop;
16228 case TRUTH_OR_EXPR:
16229 case TRUTH_ORIF_EXPR:
16230 case BIT_IOR_EXPR:
16231 op = DW_OP_or;
16232 goto do_binop;
16234 case FLOOR_DIV_EXPR:
16235 case CEIL_DIV_EXPR:
16236 case ROUND_DIV_EXPR:
16237 case TRUNC_DIV_EXPR:
16238 case EXACT_DIV_EXPR:
16239 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16240 return 0;
16241 op = DW_OP_div;
16242 goto do_binop;
16244 case MINUS_EXPR:
16245 op = DW_OP_minus;
16246 goto do_binop;
16248 case FLOOR_MOD_EXPR:
16249 case CEIL_MOD_EXPR:
16250 case ROUND_MOD_EXPR:
16251 case TRUNC_MOD_EXPR:
16252 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16254 op = DW_OP_mod;
16255 goto do_binop;
16257 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16258 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16259 if (list_ret == 0 || list_ret1 == 0)
16260 return 0;
16262 add_loc_list (&list_ret, list_ret1);
16263 if (list_ret == 0)
16264 return 0;
16265 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16266 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16267 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16268 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16269 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16270 break;
16272 case MULT_EXPR:
16273 op = DW_OP_mul;
16274 goto do_binop;
16276 case LSHIFT_EXPR:
16277 op = DW_OP_shl;
16278 goto do_binop;
16280 case RSHIFT_EXPR:
16281 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16282 goto do_binop;
16284 case POINTER_PLUS_EXPR:
16285 case PLUS_EXPR:
16286 do_plus:
16287 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16289 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16290 smarter to encode their opposite. The DW_OP_plus_uconst operation
16291 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
16292 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16293 bytes, Y being the size of the operation that pushes the opposite
16294 of the addend. So let's choose the smallest representation. */
16295 const tree tree_addend = TREE_OPERAND (loc, 1);
16296 offset_int wi_addend;
16297 HOST_WIDE_INT shwi_addend;
16298 dw_loc_descr_ref loc_naddend;
16300 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16301 if (list_ret == 0)
16302 return 0;
16304 /* Try to get the literal to push. It is the opposite of the addend,
16305 so as we rely on wrapping during DWARF evaluation, first decode
16306 the literal as a "DWARF-sized" signed number. */
16307 wi_addend = wi::to_offset (tree_addend);
16308 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16309 shwi_addend = wi_addend.to_shwi ();
16310 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16311 ? int_loc_descriptor (-shwi_addend)
16312 : NULL;
16314 if (loc_naddend != NULL
16315 && ((unsigned) size_of_uleb128 (shwi_addend)
16316 > size_of_loc_descr (loc_naddend)))
16318 add_loc_descr_to_each (list_ret, loc_naddend);
16319 add_loc_descr_to_each (list_ret,
16320 new_loc_descr (DW_OP_minus, 0, 0));
16322 else
16324 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16326 loc_naddend = loc_cur;
16327 loc_cur = loc_cur->dw_loc_next;
16328 ggc_free (loc_naddend);
16330 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16332 break;
16335 op = DW_OP_plus;
16336 goto do_binop;
16338 case LE_EXPR:
16339 op = DW_OP_le;
16340 goto do_comp_binop;
16342 case GE_EXPR:
16343 op = DW_OP_ge;
16344 goto do_comp_binop;
16346 case LT_EXPR:
16347 op = DW_OP_lt;
16348 goto do_comp_binop;
16350 case GT_EXPR:
16351 op = DW_OP_gt;
16352 goto do_comp_binop;
16354 do_comp_binop:
16355 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16357 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16358 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16359 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16360 TREE_CODE (loc));
16361 break;
16363 else
16364 goto do_binop;
16366 case EQ_EXPR:
16367 op = DW_OP_eq;
16368 goto do_binop;
16370 case NE_EXPR:
16371 op = DW_OP_ne;
16372 goto do_binop;
16374 do_binop:
16375 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16376 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16377 if (list_ret == 0 || list_ret1 == 0)
16378 return 0;
16380 add_loc_list (&list_ret, list_ret1);
16381 if (list_ret == 0)
16382 return 0;
16383 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16384 break;
16386 case TRUTH_NOT_EXPR:
16387 case BIT_NOT_EXPR:
16388 op = DW_OP_not;
16389 goto do_unop;
16391 case ABS_EXPR:
16392 op = DW_OP_abs;
16393 goto do_unop;
16395 case NEGATE_EXPR:
16396 op = DW_OP_neg;
16397 goto do_unop;
16399 do_unop:
16400 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16401 if (list_ret == 0)
16402 return 0;
16404 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16405 break;
16407 case MIN_EXPR:
16408 case MAX_EXPR:
16410 const enum tree_code code =
16411 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16413 loc = build3 (COND_EXPR, TREE_TYPE (loc),
16414 build2 (code, integer_type_node,
16415 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16416 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16419 /* fall through */
16421 case COND_EXPR:
16423 dw_loc_descr_ref lhs
16424 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16425 dw_loc_list_ref rhs
16426 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16427 dw_loc_descr_ref bra_node, jump_node, tmp;
16429 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16430 if (list_ret == 0 || lhs == 0 || rhs == 0)
16431 return 0;
16433 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16434 add_loc_descr_to_each (list_ret, bra_node);
16436 add_loc_list (&list_ret, rhs);
16437 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16438 add_loc_descr_to_each (list_ret, jump_node);
16440 add_loc_descr_to_each (list_ret, lhs);
16441 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16442 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16444 /* ??? Need a node to point the skip at. Use a nop. */
16445 tmp = new_loc_descr (DW_OP_nop, 0, 0);
16446 add_loc_descr_to_each (list_ret, tmp);
16447 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16448 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16450 break;
16452 case FIX_TRUNC_EXPR:
16453 return 0;
16455 default:
16456 /* Leave front-end specific codes as simply unknown. This comes
16457 up, for instance, with the C STMT_EXPR. */
16458 if ((unsigned int) TREE_CODE (loc)
16459 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16461 expansion_failed (loc, NULL_RTX,
16462 "language specific tree node");
16463 return 0;
16466 /* Otherwise this is a generic code; we should just lists all of
16467 these explicitly. We forgot one. */
16468 if (flag_checking)
16469 gcc_unreachable ();
16471 /* In a release build, we want to degrade gracefully: better to
16472 generate incomplete debugging information than to crash. */
16473 return NULL;
16476 if (!ret && !list_ret)
16477 return 0;
16479 if (want_address == 2 && !have_address
16480 && (dwarf_version >= 4 || !dwarf_strict))
16482 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16484 expansion_failed (loc, NULL_RTX,
16485 "DWARF address size mismatch");
16486 return 0;
16488 if (ret)
16489 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16490 else
16491 add_loc_descr_to_each (list_ret,
16492 new_loc_descr (DW_OP_stack_value, 0, 0));
16493 have_address = 1;
16495 /* Show if we can't fill the request for an address. */
16496 if (want_address && !have_address)
16498 expansion_failed (loc, NULL_RTX,
16499 "Want address and only have value");
16500 return 0;
16503 gcc_assert (!ret || !list_ret);
16505 /* If we've got an address and don't want one, dereference. */
16506 if (!want_address && have_address)
16508 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16510 if (size > DWARF2_ADDR_SIZE || size == -1)
16512 expansion_failed (loc, NULL_RTX,
16513 "DWARF address size mismatch");
16514 return 0;
16516 else if (size == DWARF2_ADDR_SIZE)
16517 op = DW_OP_deref;
16518 else
16519 op = DW_OP_deref_size;
16521 if (ret)
16522 add_loc_descr (&ret, new_loc_descr (op, size, 0));
16523 else
16524 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16526 if (ret)
16527 list_ret = new_loc_list (ret, NULL, NULL, NULL);
16529 return list_ret;
16532 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16533 expressions. */
16535 static dw_loc_list_ref
16536 loc_list_from_tree (tree loc, int want_address,
16537 const struct loc_descr_context *context)
16539 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16541 for (dw_loc_list_ref loc_cur = result;
16542 loc_cur != NULL; loc_cur =
16543 loc_cur->dw_loc_next)
16544 loc_descr_without_nops (loc_cur->expr);
16545 return result;
16548 /* Same as above but return only single location expression. */
16549 static dw_loc_descr_ref
16550 loc_descriptor_from_tree (tree loc, int want_address,
16551 const struct loc_descr_context *context)
16553 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16554 if (!ret)
16555 return NULL;
16556 if (ret->dw_loc_next)
16558 expansion_failed (loc, NULL_RTX,
16559 "Location list where only loc descriptor needed");
16560 return NULL;
16562 return ret->expr;
16565 /* Given a value, round it up to the lowest multiple of `boundary'
16566 which is not less than the value itself. */
16568 static inline HOST_WIDE_INT
16569 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16571 return (((value + boundary - 1) / boundary) * boundary);
16574 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16575 pointer to the declared type for the relevant field variable, or return
16576 `integer_type_node' if the given node turns out to be an
16577 ERROR_MARK node. */
16579 static inline tree
16580 field_type (const_tree decl)
16582 tree type;
16584 if (TREE_CODE (decl) == ERROR_MARK)
16585 return integer_type_node;
16587 type = DECL_BIT_FIELD_TYPE (decl);
16588 if (type == NULL_TREE)
16589 type = TREE_TYPE (decl);
16591 return type;
16594 /* Given a pointer to a tree node, return the alignment in bits for
16595 it, or else return BITS_PER_WORD if the node actually turns out to
16596 be an ERROR_MARK node. */
16598 static inline unsigned
16599 simple_type_align_in_bits (const_tree type)
16601 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16604 static inline unsigned
16605 simple_decl_align_in_bits (const_tree decl)
16607 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16610 /* Return the result of rounding T up to ALIGN. */
16612 static inline offset_int
16613 round_up_to_align (const offset_int &t, unsigned int align)
16615 return wi::udiv_trunc (t + align - 1, align) * align;
16618 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
16619 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
16620 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
16621 if we fail to return the size in one of these two forms. */
16623 static dw_loc_descr_ref
16624 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16626 tree tree_size;
16627 struct loc_descr_context ctx;
16629 /* Return a constant integer in priority, if possible. */
16630 *cst_size = int_size_in_bytes (type);
16631 if (*cst_size != -1)
16632 return NULL;
16634 ctx.context_type = const_cast<tree> (type);
16635 ctx.base_decl = NULL_TREE;
16636 ctx.dpi = NULL;
16638 type = TYPE_MAIN_VARIANT (type);
16639 tree_size = TYPE_SIZE_UNIT (type);
16640 return ((tree_size != NULL_TREE)
16641 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16642 : NULL);
16645 /* Helper structure for RECORD_TYPE processing. */
16646 struct vlr_context
16648 /* Root RECORD_TYPE. It is needed to generate data member location
16649 descriptions in variable-length records (VLR), but also to cope with
16650 variants, which are composed of nested structures multiplexed with
16651 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
16652 function processing a FIELD_DECL, it is required to be non null. */
16653 tree struct_type;
16654 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16655 QUAL_UNION_TYPE), this holds an expression that computes the offset for
16656 this variant part as part of the root record (in storage units). For
16657 regular records, it must be NULL_TREE. */
16658 tree variant_part_offset;
16661 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16662 addressed byte of the "containing object" for the given FIELD_DECL. If
16663 possible, return a native constant through CST_OFFSET (in which case NULL is
16664 returned); otherwise return a DWARF expression that computes the offset.
16666 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16667 that offset is, either because the argument turns out to be a pointer to an
16668 ERROR_MARK node, or because the offset expression is too complex for us.
16670 CTX is required: see the comment for VLR_CONTEXT. */
16672 static dw_loc_descr_ref
16673 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16674 HOST_WIDE_INT *cst_offset)
16676 offset_int object_offset_in_bits;
16677 offset_int object_offset_in_bytes;
16678 offset_int bitpos_int;
16679 bool is_byte_offset_cst, is_bit_offset_cst;
16680 tree tree_result;
16681 dw_loc_list_ref loc_result;
16683 *cst_offset = 0;
16685 if (TREE_CODE (decl) == ERROR_MARK)
16686 return NULL;
16687 else
16688 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16690 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16691 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16693 /* We cannot handle variable bit offsets at the moment, so abort if it's the
16694 case. */
16695 if (is_bit_offset_cst)
16696 return NULL;
16698 #ifdef PCC_BITFIELD_TYPE_MATTERS
16699 /* We used to handle only constant offsets in all cases. Now, we handle
16700 properly dynamic byte offsets only when PCC bitfield type doesn't
16701 matter. */
16702 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16704 tree type;
16705 tree field_size_tree;
16706 offset_int deepest_bitpos;
16707 offset_int field_size_in_bits;
16708 unsigned int type_align_in_bits;
16709 unsigned int decl_align_in_bits;
16710 offset_int type_size_in_bits;
16712 bitpos_int = wi::to_offset (bit_position (decl));
16713 type = field_type (decl);
16714 type_size_in_bits = offset_int_type_size_in_bits (type);
16715 type_align_in_bits = simple_type_align_in_bits (type);
16717 field_size_tree = DECL_SIZE (decl);
16719 /* The size could be unspecified if there was an error, or for
16720 a flexible array member. */
16721 if (!field_size_tree)
16722 field_size_tree = bitsize_zero_node;
16724 /* If the size of the field is not constant, use the type size. */
16725 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16726 field_size_in_bits = wi::to_offset (field_size_tree);
16727 else
16728 field_size_in_bits = type_size_in_bits;
16730 decl_align_in_bits = simple_decl_align_in_bits (decl);
16732 /* The GCC front-end doesn't make any attempt to keep track of the
16733 starting bit offset (relative to the start of the containing
16734 structure type) of the hypothetical "containing object" for a
16735 bit-field. Thus, when computing the byte offset value for the
16736 start of the "containing object" of a bit-field, we must deduce
16737 this information on our own. This can be rather tricky to do in
16738 some cases. For example, handling the following structure type
16739 definition when compiling for an i386/i486 target (which only
16740 aligns long long's to 32-bit boundaries) can be very tricky:
16742 struct S { int field1; long long field2:31; };
16744 Fortunately, there is a simple rule-of-thumb which can be used
16745 in such cases. When compiling for an i386/i486, GCC will
16746 allocate 8 bytes for the structure shown above. It decides to
16747 do this based upon one simple rule for bit-field allocation.
16748 GCC allocates each "containing object" for each bit-field at
16749 the first (i.e. lowest addressed) legitimate alignment boundary
16750 (based upon the required minimum alignment for the declared
16751 type of the field) which it can possibly use, subject to the
16752 condition that there is still enough available space remaining
16753 in the containing object (when allocated at the selected point)
16754 to fully accommodate all of the bits of the bit-field itself.
16756 This simple rule makes it obvious why GCC allocates 8 bytes for
16757 each object of the structure type shown above. When looking
16758 for a place to allocate the "containing object" for `field2',
16759 the compiler simply tries to allocate a 64-bit "containing
16760 object" at each successive 32-bit boundary (starting at zero)
16761 until it finds a place to allocate that 64- bit field such that
16762 at least 31 contiguous (and previously unallocated) bits remain
16763 within that selected 64 bit field. (As it turns out, for the
16764 example above, the compiler finds it is OK to allocate the
16765 "containing object" 64-bit field at bit-offset zero within the
16766 structure type.)
16768 Here we attempt to work backwards from the limited set of facts
16769 we're given, and we try to deduce from those facts, where GCC
16770 must have believed that the containing object started (within
16771 the structure type). The value we deduce is then used (by the
16772 callers of this routine) to generate DW_AT_location and
16773 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16774 the case of DW_AT_location, regular fields as well). */
16776 /* Figure out the bit-distance from the start of the structure to
16777 the "deepest" bit of the bit-field. */
16778 deepest_bitpos = bitpos_int + field_size_in_bits;
16780 /* This is the tricky part. Use some fancy footwork to deduce
16781 where the lowest addressed bit of the containing object must
16782 be. */
16783 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16785 /* Round up to type_align by default. This works best for
16786 bitfields. */
16787 object_offset_in_bits
16788 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16790 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16792 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16794 /* Round up to decl_align instead. */
16795 object_offset_in_bits
16796 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16799 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16801 tree_result = byte_position (decl);
16802 if (ctx->variant_part_offset != NULL_TREE)
16803 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16804 ctx->variant_part_offset, tree_result));
16806 /* If the byte offset is a constant, it's simplier to handle a native
16807 constant rather than a DWARF expression. */
16808 if (TREE_CODE (tree_result) == INTEGER_CST)
16810 *cst_offset = wi::to_offset (tree_result).to_shwi ();
16811 return NULL;
16813 struct loc_descr_context loc_ctx = {
16814 ctx->struct_type, /* context_type */
16815 NULL_TREE, /* base_decl */
16816 NULL /* dpi */
16818 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16820 /* We want a DWARF expression: abort if we only have a location list with
16821 multiple elements. */
16822 if (!loc_result || !single_element_loc_list_p (loc_result))
16823 return NULL;
16824 else
16825 return loc_result->expr;
16828 /* The following routines define various Dwarf attributes and any data
16829 associated with them. */
16831 /* Add a location description attribute value to a DIE.
16833 This emits location attributes suitable for whole variables and
16834 whole parameters. Note that the location attributes for struct fields are
16835 generated by the routine `data_member_location_attribute' below. */
16837 static inline void
16838 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16839 dw_loc_list_ref descr)
16841 if (descr == 0)
16842 return;
16843 if (single_element_loc_list_p (descr))
16844 add_AT_loc (die, attr_kind, descr->expr);
16845 else
16846 add_AT_loc_list (die, attr_kind, descr);
16849 /* Add DW_AT_accessibility attribute to DIE if needed. */
16851 static void
16852 add_accessibility_attribute (dw_die_ref die, tree decl)
16854 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16855 children, otherwise the default is DW_ACCESS_public. In DWARF2
16856 the default has always been DW_ACCESS_public. */
16857 if (TREE_PROTECTED (decl))
16858 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16859 else if (TREE_PRIVATE (decl))
16861 if (dwarf_version == 2
16862 || die->die_parent == NULL
16863 || die->die_parent->die_tag != DW_TAG_class_type)
16864 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16866 else if (dwarf_version > 2
16867 && die->die_parent
16868 && die->die_parent->die_tag == DW_TAG_class_type)
16869 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16872 /* Attach the specialized form of location attribute used for data members of
16873 struct and union types. In the special case of a FIELD_DECL node which
16874 represents a bit-field, the "offset" part of this special location
16875 descriptor must indicate the distance in bytes from the lowest-addressed
16876 byte of the containing struct or union type to the lowest-addressed byte of
16877 the "containing object" for the bit-field. (See the `field_byte_offset'
16878 function above).
16880 For any given bit-field, the "containing object" is a hypothetical object
16881 (of some integral or enum type) within which the given bit-field lives. The
16882 type of this hypothetical "containing object" is always the same as the
16883 declared type of the individual bit-field itself (for GCC anyway... the
16884 DWARF spec doesn't actually mandate this). Note that it is the size (in
16885 bytes) of the hypothetical "containing object" which will be given in the
16886 DW_AT_byte_size attribute for this bit-field. (See the
16887 `byte_size_attribute' function below.) It is also used when calculating the
16888 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16889 function below.)
16891 CTX is required: see the comment for VLR_CONTEXT. */
16893 static void
16894 add_data_member_location_attribute (dw_die_ref die,
16895 tree decl,
16896 struct vlr_context *ctx)
16898 HOST_WIDE_INT offset;
16899 dw_loc_descr_ref loc_descr = 0;
16901 if (TREE_CODE (decl) == TREE_BINFO)
16903 /* We're working on the TAG_inheritance for a base class. */
16904 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16906 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16907 aren't at a fixed offset from all (sub)objects of the same
16908 type. We need to extract the appropriate offset from our
16909 vtable. The following dwarf expression means
16911 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16913 This is specific to the V3 ABI, of course. */
16915 dw_loc_descr_ref tmp;
16917 /* Make a copy of the object address. */
16918 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16919 add_loc_descr (&loc_descr, tmp);
16921 /* Extract the vtable address. */
16922 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16923 add_loc_descr (&loc_descr, tmp);
16925 /* Calculate the address of the offset. */
16926 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16927 gcc_assert (offset < 0);
16929 tmp = int_loc_descriptor (-offset);
16930 add_loc_descr (&loc_descr, tmp);
16931 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16932 add_loc_descr (&loc_descr, tmp);
16934 /* Extract the offset. */
16935 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16936 add_loc_descr (&loc_descr, tmp);
16938 /* Add it to the object address. */
16939 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16940 add_loc_descr (&loc_descr, tmp);
16942 else
16943 offset = tree_to_shwi (BINFO_OFFSET (decl));
16945 else
16947 loc_descr = field_byte_offset (decl, ctx, &offset);
16949 /* If loc_descr is available then we know the field offset is dynamic.
16950 However, GDB does not handle dynamic field offsets very well at the
16951 moment. */
16952 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16954 loc_descr = NULL;
16955 offset = 0;
16958 /* Data member location evalutation starts with the base address on the
16959 stack. Compute the field offset and add it to this base address. */
16960 else if (loc_descr != NULL)
16961 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
16964 if (! loc_descr)
16966 if (dwarf_version > 2)
16968 /* Don't need to output a location expression, just the constant. */
16969 if (offset < 0)
16970 add_AT_int (die, DW_AT_data_member_location, offset);
16971 else
16972 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16973 return;
16975 else
16977 enum dwarf_location_atom op;
16979 /* The DWARF2 standard says that we should assume that the structure
16980 address is already on the stack, so we can specify a structure
16981 field address by using DW_OP_plus_uconst. */
16982 op = DW_OP_plus_uconst;
16983 loc_descr = new_loc_descr (op, offset, 0);
16987 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16990 /* Writes integer values to dw_vec_const array. */
16992 static void
16993 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16995 while (size != 0)
16997 *dest++ = val & 0xff;
16998 val >>= 8;
16999 --size;
17003 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
17005 static HOST_WIDE_INT
17006 extract_int (const unsigned char *src, unsigned int size)
17008 HOST_WIDE_INT val = 0;
17010 src += size;
17011 while (size != 0)
17013 val <<= 8;
17014 val |= *--src & 0xff;
17015 --size;
17017 return val;
17020 /* Writes wide_int values to dw_vec_const array. */
17022 static void
17023 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
17025 int i;
17027 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
17029 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
17030 return;
17033 /* We'd have to extend this code to support odd sizes. */
17034 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
17036 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
17038 if (WORDS_BIG_ENDIAN)
17039 for (i = n - 1; i >= 0; i--)
17041 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17042 dest += sizeof (HOST_WIDE_INT);
17044 else
17045 for (i = 0; i < n; i++)
17047 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17048 dest += sizeof (HOST_WIDE_INT);
17052 /* Writes floating point values to dw_vec_const array. */
17054 static void
17055 insert_float (const_rtx rtl, unsigned char *array)
17057 long val[4];
17058 int i;
17060 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
17062 /* real_to_target puts 32-bit pieces in each long. Pack them. */
17063 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17065 insert_int (val[i], 4, array);
17066 array += 4;
17070 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17071 does not have a "location" either in memory or in a register. These
17072 things can arise in GNU C when a constant is passed as an actual parameter
17073 to an inlined function. They can also arise in C++ where declared
17074 constants do not necessarily get memory "homes". */
17076 static bool
17077 add_const_value_attribute (dw_die_ref die, rtx rtl)
17079 switch (GET_CODE (rtl))
17081 case CONST_INT:
17083 HOST_WIDE_INT val = INTVAL (rtl);
17085 if (val < 0)
17086 add_AT_int (die, DW_AT_const_value, val);
17087 else
17088 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17090 return true;
17092 case CONST_WIDE_INT:
17094 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17095 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17096 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17097 wide_int w = wi::zext (w1, prec);
17098 add_AT_wide (die, DW_AT_const_value, w);
17100 return true;
17102 case CONST_DOUBLE:
17103 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17104 floating-point constant. A CONST_DOUBLE is used whenever the
17105 constant requires more than one word in order to be adequately
17106 represented. */
17108 machine_mode mode = GET_MODE (rtl);
17110 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17111 add_AT_double (die, DW_AT_const_value,
17112 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17113 else
17115 unsigned int length = GET_MODE_SIZE (mode);
17116 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17118 insert_float (rtl, array);
17119 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17122 return true;
17124 case CONST_VECTOR:
17126 machine_mode mode = GET_MODE (rtl);
17127 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17128 unsigned int length = CONST_VECTOR_NUNITS (rtl);
17129 unsigned char *array
17130 = ggc_vec_alloc<unsigned char> (length * elt_size);
17131 unsigned int i;
17132 unsigned char *p;
17133 machine_mode imode = GET_MODE_INNER (mode);
17135 switch (GET_MODE_CLASS (mode))
17137 case MODE_VECTOR_INT:
17138 for (i = 0, p = array; i < length; i++, p += elt_size)
17140 rtx elt = CONST_VECTOR_ELT (rtl, i);
17141 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17143 break;
17145 case MODE_VECTOR_FLOAT:
17146 for (i = 0, p = array; i < length; i++, p += elt_size)
17148 rtx elt = CONST_VECTOR_ELT (rtl, i);
17149 insert_float (elt, p);
17151 break;
17153 default:
17154 gcc_unreachable ();
17157 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17159 return true;
17161 case CONST_STRING:
17162 if (dwarf_version >= 4 || !dwarf_strict)
17164 dw_loc_descr_ref loc_result;
17165 resolve_one_addr (&rtl);
17166 rtl_addr:
17167 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17168 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17169 add_AT_loc (die, DW_AT_location, loc_result);
17170 vec_safe_push (used_rtx_array, rtl);
17171 return true;
17173 return false;
17175 case CONST:
17176 if (CONSTANT_P (XEXP (rtl, 0)))
17177 return add_const_value_attribute (die, XEXP (rtl, 0));
17178 /* FALLTHROUGH */
17179 case SYMBOL_REF:
17180 if (!const_ok_for_output (rtl))
17181 return false;
17182 case LABEL_REF:
17183 if (dwarf_version >= 4 || !dwarf_strict)
17184 goto rtl_addr;
17185 return false;
17187 case PLUS:
17188 /* In cases where an inlined instance of an inline function is passed
17189 the address of an `auto' variable (which is local to the caller) we
17190 can get a situation where the DECL_RTL of the artificial local
17191 variable (for the inlining) which acts as a stand-in for the
17192 corresponding formal parameter (of the inline function) will look
17193 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
17194 exactly a compile-time constant expression, but it isn't the address
17195 of the (artificial) local variable either. Rather, it represents the
17196 *value* which the artificial local variable always has during its
17197 lifetime. We currently have no way to represent such quasi-constant
17198 values in Dwarf, so for now we just punt and generate nothing. */
17199 return false;
17201 case HIGH:
17202 case CONST_FIXED:
17203 return false;
17205 case MEM:
17206 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17207 && MEM_READONLY_P (rtl)
17208 && GET_MODE (rtl) == BLKmode)
17210 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17211 return true;
17213 return false;
17215 default:
17216 /* No other kinds of rtx should be possible here. */
17217 gcc_unreachable ();
17219 return false;
17222 /* Determine whether the evaluation of EXPR references any variables
17223 or functions which aren't otherwise used (and therefore may not be
17224 output). */
17225 static tree
17226 reference_to_unused (tree * tp, int * walk_subtrees,
17227 void * data ATTRIBUTE_UNUSED)
17229 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17230 *walk_subtrees = 0;
17232 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17233 && ! TREE_ASM_WRITTEN (*tp))
17234 return *tp;
17235 /* ??? The C++ FE emits debug information for using decls, so
17236 putting gcc_unreachable here falls over. See PR31899. For now
17237 be conservative. */
17238 else if (!symtab->global_info_ready
17239 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17240 return *tp;
17241 else if (TREE_CODE (*tp) == VAR_DECL)
17243 varpool_node *node = varpool_node::get (*tp);
17244 if (!node || !node->definition)
17245 return *tp;
17247 else if (TREE_CODE (*tp) == FUNCTION_DECL
17248 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17250 /* The call graph machinery must have finished analyzing,
17251 optimizing and gimplifying the CU by now.
17252 So if *TP has no call graph node associated
17253 to it, it means *TP will not be emitted. */
17254 if (!cgraph_node::get (*tp))
17255 return *tp;
17257 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17258 return *tp;
17260 return NULL_TREE;
17263 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17264 for use in a later add_const_value_attribute call. */
17266 static rtx
17267 rtl_for_decl_init (tree init, tree type)
17269 rtx rtl = NULL_RTX;
17271 STRIP_NOPS (init);
17273 /* If a variable is initialized with a string constant without embedded
17274 zeros, build CONST_STRING. */
17275 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17277 tree enttype = TREE_TYPE (type);
17278 tree domain = TYPE_DOMAIN (type);
17279 machine_mode mode = TYPE_MODE (enttype);
17281 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17282 && domain
17283 && integer_zerop (TYPE_MIN_VALUE (domain))
17284 && compare_tree_int (TYPE_MAX_VALUE (domain),
17285 TREE_STRING_LENGTH (init) - 1) == 0
17286 && ((size_t) TREE_STRING_LENGTH (init)
17287 == strlen (TREE_STRING_POINTER (init)) + 1))
17289 rtl = gen_rtx_CONST_STRING (VOIDmode,
17290 ggc_strdup (TREE_STRING_POINTER (init)));
17291 rtl = gen_rtx_MEM (BLKmode, rtl);
17292 MEM_READONLY_P (rtl) = 1;
17295 /* Other aggregates, and complex values, could be represented using
17296 CONCAT: FIXME! */
17297 else if (AGGREGATE_TYPE_P (type)
17298 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17299 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17300 || TREE_CODE (type) == COMPLEX_TYPE)
17302 /* Vectors only work if their mode is supported by the target.
17303 FIXME: generic vectors ought to work too. */
17304 else if (TREE_CODE (type) == VECTOR_TYPE
17305 && !VECTOR_MODE_P (TYPE_MODE (type)))
17307 /* If the initializer is something that we know will expand into an
17308 immediate RTL constant, expand it now. We must be careful not to
17309 reference variables which won't be output. */
17310 else if (initializer_constant_valid_p (init, type)
17311 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17313 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17314 possible. */
17315 if (TREE_CODE (type) == VECTOR_TYPE)
17316 switch (TREE_CODE (init))
17318 case VECTOR_CST:
17319 break;
17320 case CONSTRUCTOR:
17321 if (TREE_CONSTANT (init))
17323 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17324 bool constant_p = true;
17325 tree value;
17326 unsigned HOST_WIDE_INT ix;
17328 /* Even when ctor is constant, it might contain non-*_CST
17329 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17330 belong into VECTOR_CST nodes. */
17331 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17332 if (!CONSTANT_CLASS_P (value))
17334 constant_p = false;
17335 break;
17338 if (constant_p)
17340 init = build_vector_from_ctor (type, elts);
17341 break;
17344 /* FALLTHRU */
17346 default:
17347 return NULL;
17350 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17352 /* If expand_expr returns a MEM, it wasn't immediate. */
17353 gcc_assert (!rtl || !MEM_P (rtl));
17356 return rtl;
17359 /* Generate RTL for the variable DECL to represent its location. */
17361 static rtx
17362 rtl_for_decl_location (tree decl)
17364 rtx rtl;
17366 /* Here we have to decide where we are going to say the parameter "lives"
17367 (as far as the debugger is concerned). We only have a couple of
17368 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17370 DECL_RTL normally indicates where the parameter lives during most of the
17371 activation of the function. If optimization is enabled however, this
17372 could be either NULL or else a pseudo-reg. Both of those cases indicate
17373 that the parameter doesn't really live anywhere (as far as the code
17374 generation parts of GCC are concerned) during most of the function's
17375 activation. That will happen (for example) if the parameter is never
17376 referenced within the function.
17378 We could just generate a location descriptor here for all non-NULL
17379 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17380 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17381 where DECL_RTL is NULL or is a pseudo-reg.
17383 Note however that we can only get away with using DECL_INCOMING_RTL as
17384 a backup substitute for DECL_RTL in certain limited cases. In cases
17385 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17386 we can be sure that the parameter was passed using the same type as it is
17387 declared to have within the function, and that its DECL_INCOMING_RTL
17388 points us to a place where a value of that type is passed.
17390 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17391 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17392 because in these cases DECL_INCOMING_RTL points us to a value of some
17393 type which is *different* from the type of the parameter itself. Thus,
17394 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17395 such cases, the debugger would end up (for example) trying to fetch a
17396 `float' from a place which actually contains the first part of a
17397 `double'. That would lead to really incorrect and confusing
17398 output at debug-time.
17400 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17401 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
17402 are a couple of exceptions however. On little-endian machines we can
17403 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17404 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17405 an integral type that is smaller than TREE_TYPE (decl). These cases arise
17406 when (on a little-endian machine) a non-prototyped function has a
17407 parameter declared to be of type `short' or `char'. In such cases,
17408 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17409 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17410 passed `int' value. If the debugger then uses that address to fetch
17411 a `short' or a `char' (on a little-endian machine) the result will be
17412 the correct data, so we allow for such exceptional cases below.
17414 Note that our goal here is to describe the place where the given formal
17415 parameter lives during most of the function's activation (i.e. between the
17416 end of the prologue and the start of the epilogue). We'll do that as best
17417 as we can. Note however that if the given formal parameter is modified
17418 sometime during the execution of the function, then a stack backtrace (at
17419 debug-time) will show the function as having been called with the *new*
17420 value rather than the value which was originally passed in. This happens
17421 rarely enough that it is not a major problem, but it *is* a problem, and
17422 I'd like to fix it.
17424 A future version of dwarf2out.c may generate two additional attributes for
17425 any given DW_TAG_formal_parameter DIE which will describe the "passed
17426 type" and the "passed location" for the given formal parameter in addition
17427 to the attributes we now generate to indicate the "declared type" and the
17428 "active location" for each parameter. This additional set of attributes
17429 could be used by debuggers for stack backtraces. Separately, note that
17430 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17431 This happens (for example) for inlined-instances of inline function formal
17432 parameters which are never referenced. This really shouldn't be
17433 happening. All PARM_DECL nodes should get valid non-NULL
17434 DECL_INCOMING_RTL values. FIXME. */
17436 /* Use DECL_RTL as the "location" unless we find something better. */
17437 rtl = DECL_RTL_IF_SET (decl);
17439 /* When generating abstract instances, ignore everything except
17440 constants, symbols living in memory, and symbols living in
17441 fixed registers. */
17442 if (! reload_completed)
17444 if (rtl
17445 && (CONSTANT_P (rtl)
17446 || (MEM_P (rtl)
17447 && CONSTANT_P (XEXP (rtl, 0)))
17448 || (REG_P (rtl)
17449 && TREE_CODE (decl) == VAR_DECL
17450 && TREE_STATIC (decl))))
17452 rtl = targetm.delegitimize_address (rtl);
17453 return rtl;
17455 rtl = NULL_RTX;
17457 else if (TREE_CODE (decl) == PARM_DECL)
17459 if (rtl == NULL_RTX
17460 || is_pseudo_reg (rtl)
17461 || (MEM_P (rtl)
17462 && is_pseudo_reg (XEXP (rtl, 0))
17463 && DECL_INCOMING_RTL (decl)
17464 && MEM_P (DECL_INCOMING_RTL (decl))
17465 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17467 tree declared_type = TREE_TYPE (decl);
17468 tree passed_type = DECL_ARG_TYPE (decl);
17469 machine_mode dmode = TYPE_MODE (declared_type);
17470 machine_mode pmode = TYPE_MODE (passed_type);
17472 /* This decl represents a formal parameter which was optimized out.
17473 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17474 all cases where (rtl == NULL_RTX) just below. */
17475 if (dmode == pmode)
17476 rtl = DECL_INCOMING_RTL (decl);
17477 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17478 && SCALAR_INT_MODE_P (dmode)
17479 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17480 && DECL_INCOMING_RTL (decl))
17482 rtx inc = DECL_INCOMING_RTL (decl);
17483 if (REG_P (inc))
17484 rtl = inc;
17485 else if (MEM_P (inc))
17487 if (BYTES_BIG_ENDIAN)
17488 rtl = adjust_address_nv (inc, dmode,
17489 GET_MODE_SIZE (pmode)
17490 - GET_MODE_SIZE (dmode));
17491 else
17492 rtl = inc;
17497 /* If the parm was passed in registers, but lives on the stack, then
17498 make a big endian correction if the mode of the type of the
17499 parameter is not the same as the mode of the rtl. */
17500 /* ??? This is the same series of checks that are made in dbxout.c before
17501 we reach the big endian correction code there. It isn't clear if all
17502 of these checks are necessary here, but keeping them all is the safe
17503 thing to do. */
17504 else if (MEM_P (rtl)
17505 && XEXP (rtl, 0) != const0_rtx
17506 && ! CONSTANT_P (XEXP (rtl, 0))
17507 /* Not passed in memory. */
17508 && !MEM_P (DECL_INCOMING_RTL (decl))
17509 /* Not passed by invisible reference. */
17510 && (!REG_P (XEXP (rtl, 0))
17511 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17512 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17513 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17514 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17515 #endif
17517 /* Big endian correction check. */
17518 && BYTES_BIG_ENDIAN
17519 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17520 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17521 < UNITS_PER_WORD))
17523 machine_mode addr_mode = get_address_mode (rtl);
17524 int offset = (UNITS_PER_WORD
17525 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17527 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17528 plus_constant (addr_mode, XEXP (rtl, 0), offset));
17531 else if (TREE_CODE (decl) == VAR_DECL
17532 && rtl
17533 && MEM_P (rtl)
17534 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17535 && BYTES_BIG_ENDIAN)
17537 machine_mode addr_mode = get_address_mode (rtl);
17538 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17539 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17541 /* If a variable is declared "register" yet is smaller than
17542 a register, then if we store the variable to memory, it
17543 looks like we're storing a register-sized value, when in
17544 fact we are not. We need to adjust the offset of the
17545 storage location to reflect the actual value's bytes,
17546 else gdb will not be able to display it. */
17547 if (rsize > dsize)
17548 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17549 plus_constant (addr_mode, XEXP (rtl, 0),
17550 rsize - dsize));
17553 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17554 and will have been substituted directly into all expressions that use it.
17555 C does not have such a concept, but C++ and other languages do. */
17556 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17557 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17559 if (rtl)
17560 rtl = targetm.delegitimize_address (rtl);
17562 /* If we don't look past the constant pool, we risk emitting a
17563 reference to a constant pool entry that isn't referenced from
17564 code, and thus is not emitted. */
17565 if (rtl)
17566 rtl = avoid_constant_pool_reference (rtl);
17568 /* Try harder to get a rtl. If this symbol ends up not being emitted
17569 in the current CU, resolve_addr will remove the expression referencing
17570 it. */
17571 if (rtl == NULL_RTX
17572 && TREE_CODE (decl) == VAR_DECL
17573 && !DECL_EXTERNAL (decl)
17574 && TREE_STATIC (decl)
17575 && DECL_NAME (decl)
17576 && !DECL_HARD_REGISTER (decl)
17577 && DECL_MODE (decl) != VOIDmode)
17579 rtl = make_decl_rtl_for_debug (decl);
17580 if (!MEM_P (rtl)
17581 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17582 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17583 rtl = NULL_RTX;
17586 return rtl;
17589 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
17590 returned. If so, the decl for the COMMON block is returned, and the
17591 value is the offset into the common block for the symbol. */
17593 static tree
17594 fortran_common (tree decl, HOST_WIDE_INT *value)
17596 tree val_expr, cvar;
17597 machine_mode mode;
17598 HOST_WIDE_INT bitsize, bitpos;
17599 tree offset;
17600 int unsignedp, reversep, volatilep = 0;
17602 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17603 it does not have a value (the offset into the common area), or if it
17604 is thread local (as opposed to global) then it isn't common, and shouldn't
17605 be handled as such. */
17606 if (TREE_CODE (decl) != VAR_DECL
17607 || !TREE_STATIC (decl)
17608 || !DECL_HAS_VALUE_EXPR_P (decl)
17609 || !is_fortran ())
17610 return NULL_TREE;
17612 val_expr = DECL_VALUE_EXPR (decl);
17613 if (TREE_CODE (val_expr) != COMPONENT_REF)
17614 return NULL_TREE;
17616 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17617 &unsignedp, &reversep, &volatilep);
17619 if (cvar == NULL_TREE
17620 || TREE_CODE (cvar) != VAR_DECL
17621 || DECL_ARTIFICIAL (cvar)
17622 || !TREE_PUBLIC (cvar))
17623 return NULL_TREE;
17625 *value = 0;
17626 if (offset != NULL)
17628 if (!tree_fits_shwi_p (offset))
17629 return NULL_TREE;
17630 *value = tree_to_shwi (offset);
17632 if (bitpos != 0)
17633 *value += bitpos / BITS_PER_UNIT;
17635 return cvar;
17638 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17639 data attribute for a variable or a parameter. We generate the
17640 DW_AT_const_value attribute only in those cases where the given variable
17641 or parameter does not have a true "location" either in memory or in a
17642 register. This can happen (for example) when a constant is passed as an
17643 actual argument in a call to an inline function. (It's possible that
17644 these things can crop up in other ways also.) Note that one type of
17645 constant value which can be passed into an inlined function is a constant
17646 pointer. This can happen for example if an actual argument in an inlined
17647 function call evaluates to a compile-time constant address.
17649 CACHE_P is true if it is worth caching the location list for DECL,
17650 so that future calls can reuse it rather than regenerate it from scratch.
17651 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17652 since we will need to refer to them each time the function is inlined. */
17654 static bool
17655 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17657 rtx rtl;
17658 dw_loc_list_ref list;
17659 var_loc_list *loc_list;
17660 cached_dw_loc_list *cache;
17662 if (early_dwarf)
17663 return false;
17665 if (TREE_CODE (decl) == ERROR_MARK)
17666 return false;
17668 if (get_AT (die, DW_AT_location)
17669 || get_AT (die, DW_AT_const_value))
17670 return true;
17672 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17673 || TREE_CODE (decl) == RESULT_DECL);
17675 /* Try to get some constant RTL for this decl, and use that as the value of
17676 the location. */
17678 rtl = rtl_for_decl_location (decl);
17679 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17680 && add_const_value_attribute (die, rtl))
17681 return true;
17683 /* See if we have single element location list that is equivalent to
17684 a constant value. That way we are better to use add_const_value_attribute
17685 rather than expanding constant value equivalent. */
17686 loc_list = lookup_decl_loc (decl);
17687 if (loc_list
17688 && loc_list->first
17689 && loc_list->first->next == NULL
17690 && NOTE_P (loc_list->first->loc)
17691 && NOTE_VAR_LOCATION (loc_list->first->loc)
17692 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17694 struct var_loc_node *node;
17696 node = loc_list->first;
17697 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17698 if (GET_CODE (rtl) == EXPR_LIST)
17699 rtl = XEXP (rtl, 0);
17700 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17701 && add_const_value_attribute (die, rtl))
17702 return true;
17704 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17705 list several times. See if we've already cached the contents. */
17706 list = NULL;
17707 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17708 cache_p = false;
17709 if (cache_p)
17711 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17712 if (cache)
17713 list = cache->loc_list;
17715 if (list == NULL)
17717 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17718 NULL);
17719 /* It is usually worth caching this result if the decl is from
17720 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17721 if (cache_p && list && list->dw_loc_next)
17723 cached_dw_loc_list **slot
17724 = cached_dw_loc_list_table->find_slot_with_hash (decl,
17725 DECL_UID (decl),
17726 INSERT);
17727 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17728 cache->decl_id = DECL_UID (decl);
17729 cache->loc_list = list;
17730 *slot = cache;
17733 if (list)
17735 add_AT_location_description (die, DW_AT_location, list);
17736 return true;
17738 /* None of that worked, so it must not really have a location;
17739 try adding a constant value attribute from the DECL_INITIAL. */
17740 return tree_add_const_value_attribute_for_decl (die, decl);
17743 /* Helper function for tree_add_const_value_attribute. Natively encode
17744 initializer INIT into an array. Return true if successful. */
17746 static bool
17747 native_encode_initializer (tree init, unsigned char *array, int size)
17749 tree type;
17751 if (init == NULL_TREE)
17752 return false;
17754 STRIP_NOPS (init);
17755 switch (TREE_CODE (init))
17757 case STRING_CST:
17758 type = TREE_TYPE (init);
17759 if (TREE_CODE (type) == ARRAY_TYPE)
17761 tree enttype = TREE_TYPE (type);
17762 machine_mode mode = TYPE_MODE (enttype);
17764 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17765 return false;
17766 if (int_size_in_bytes (type) != size)
17767 return false;
17768 if (size > TREE_STRING_LENGTH (init))
17770 memcpy (array, TREE_STRING_POINTER (init),
17771 TREE_STRING_LENGTH (init));
17772 memset (array + TREE_STRING_LENGTH (init),
17773 '\0', size - TREE_STRING_LENGTH (init));
17775 else
17776 memcpy (array, TREE_STRING_POINTER (init), size);
17777 return true;
17779 return false;
17780 case CONSTRUCTOR:
17781 type = TREE_TYPE (init);
17782 if (int_size_in_bytes (type) != size)
17783 return false;
17784 if (TREE_CODE (type) == ARRAY_TYPE)
17786 HOST_WIDE_INT min_index;
17787 unsigned HOST_WIDE_INT cnt;
17788 int curpos = 0, fieldsize;
17789 constructor_elt *ce;
17791 if (TYPE_DOMAIN (type) == NULL_TREE
17792 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17793 return false;
17795 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17796 if (fieldsize <= 0)
17797 return false;
17799 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17800 memset (array, '\0', size);
17801 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17803 tree val = ce->value;
17804 tree index = ce->index;
17805 int pos = curpos;
17806 if (index && TREE_CODE (index) == RANGE_EXPR)
17807 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17808 * fieldsize;
17809 else if (index)
17810 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17812 if (val)
17814 STRIP_NOPS (val);
17815 if (!native_encode_initializer (val, array + pos, fieldsize))
17816 return false;
17818 curpos = pos + fieldsize;
17819 if (index && TREE_CODE (index) == RANGE_EXPR)
17821 int count = tree_to_shwi (TREE_OPERAND (index, 1))
17822 - tree_to_shwi (TREE_OPERAND (index, 0));
17823 while (count-- > 0)
17825 if (val)
17826 memcpy (array + curpos, array + pos, fieldsize);
17827 curpos += fieldsize;
17830 gcc_assert (curpos <= size);
17832 return true;
17834 else if (TREE_CODE (type) == RECORD_TYPE
17835 || TREE_CODE (type) == UNION_TYPE)
17837 tree field = NULL_TREE;
17838 unsigned HOST_WIDE_INT cnt;
17839 constructor_elt *ce;
17841 if (int_size_in_bytes (type) != size)
17842 return false;
17844 if (TREE_CODE (type) == RECORD_TYPE)
17845 field = TYPE_FIELDS (type);
17847 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17849 tree val = ce->value;
17850 int pos, fieldsize;
17852 if (ce->index != 0)
17853 field = ce->index;
17855 if (val)
17856 STRIP_NOPS (val);
17858 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17859 return false;
17861 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17862 && TYPE_DOMAIN (TREE_TYPE (field))
17863 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17864 return false;
17865 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17866 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17867 return false;
17868 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17869 pos = int_byte_position (field);
17870 gcc_assert (pos + fieldsize <= size);
17871 if (val && fieldsize != 0
17872 && !native_encode_initializer (val, array + pos, fieldsize))
17873 return false;
17875 return true;
17877 return false;
17878 case VIEW_CONVERT_EXPR:
17879 case NON_LVALUE_EXPR:
17880 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17881 default:
17882 return native_encode_expr (init, array, size) == size;
17886 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17887 attribute is the const value T. */
17889 static bool
17890 tree_add_const_value_attribute (dw_die_ref die, tree t)
17892 tree init;
17893 tree type = TREE_TYPE (t);
17894 rtx rtl;
17896 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17897 return false;
17899 init = t;
17900 gcc_assert (!DECL_P (init));
17902 rtl = rtl_for_decl_init (init, type);
17903 if (rtl)
17904 return add_const_value_attribute (die, rtl);
17905 /* If the host and target are sane, try harder. */
17906 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17907 && initializer_constant_valid_p (init, type))
17909 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17910 if (size > 0 && (int) size == size)
17912 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17914 if (native_encode_initializer (init, array, size))
17916 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17917 return true;
17919 ggc_free (array);
17922 return false;
17925 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17926 attribute is the const value of T, where T is an integral constant
17927 variable with static storage duration
17928 (so it can't be a PARM_DECL or a RESULT_DECL). */
17930 static bool
17931 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17934 if (!decl
17935 || (TREE_CODE (decl) != VAR_DECL
17936 && TREE_CODE (decl) != CONST_DECL)
17937 || (TREE_CODE (decl) == VAR_DECL
17938 && !TREE_STATIC (decl)))
17939 return false;
17941 if (TREE_READONLY (decl)
17942 && ! TREE_THIS_VOLATILE (decl)
17943 && DECL_INITIAL (decl))
17944 /* OK */;
17945 else
17946 return false;
17948 /* Don't add DW_AT_const_value if abstract origin already has one. */
17949 if (get_AT (var_die, DW_AT_const_value))
17950 return false;
17952 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17955 /* Convert the CFI instructions for the current function into a
17956 location list. This is used for DW_AT_frame_base when we targeting
17957 a dwarf2 consumer that does not support the dwarf3
17958 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17959 expressions. */
17961 static dw_loc_list_ref
17962 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17964 int ix;
17965 dw_fde_ref fde;
17966 dw_loc_list_ref list, *list_tail;
17967 dw_cfi_ref cfi;
17968 dw_cfa_location last_cfa, next_cfa;
17969 const char *start_label, *last_label, *section;
17970 dw_cfa_location remember;
17972 fde = cfun->fde;
17973 gcc_assert (fde != NULL);
17975 section = secname_for_decl (current_function_decl);
17976 list_tail = &list;
17977 list = NULL;
17979 memset (&next_cfa, 0, sizeof (next_cfa));
17980 next_cfa.reg = INVALID_REGNUM;
17981 remember = next_cfa;
17983 start_label = fde->dw_fde_begin;
17985 /* ??? Bald assumption that the CIE opcode list does not contain
17986 advance opcodes. */
17987 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
17988 lookup_cfa_1 (cfi, &next_cfa, &remember);
17990 last_cfa = next_cfa;
17991 last_label = start_label;
17993 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
17995 /* If the first partition contained no CFI adjustments, the
17996 CIE opcodes apply to the whole first partition. */
17997 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17998 fde->dw_fde_begin, fde->dw_fde_end, section);
17999 list_tail =&(*list_tail)->dw_loc_next;
18000 start_label = last_label = fde->dw_fde_second_begin;
18003 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
18005 switch (cfi->dw_cfi_opc)
18007 case DW_CFA_set_loc:
18008 case DW_CFA_advance_loc1:
18009 case DW_CFA_advance_loc2:
18010 case DW_CFA_advance_loc4:
18011 if (!cfa_equal_p (&last_cfa, &next_cfa))
18013 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18014 start_label, last_label, section);
18016 list_tail = &(*list_tail)->dw_loc_next;
18017 last_cfa = next_cfa;
18018 start_label = last_label;
18020 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18021 break;
18023 case DW_CFA_advance_loc:
18024 /* The encoding is complex enough that we should never emit this. */
18025 gcc_unreachable ();
18027 default:
18028 lookup_cfa_1 (cfi, &next_cfa, &remember);
18029 break;
18031 if (ix + 1 == fde->dw_fde_switch_cfi_index)
18033 if (!cfa_equal_p (&last_cfa, &next_cfa))
18035 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18036 start_label, last_label, section);
18038 list_tail = &(*list_tail)->dw_loc_next;
18039 last_cfa = next_cfa;
18040 start_label = last_label;
18042 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18043 start_label, fde->dw_fde_end, section);
18044 list_tail = &(*list_tail)->dw_loc_next;
18045 start_label = last_label = fde->dw_fde_second_begin;
18049 if (!cfa_equal_p (&last_cfa, &next_cfa))
18051 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18052 start_label, last_label, section);
18053 list_tail = &(*list_tail)->dw_loc_next;
18054 start_label = last_label;
18057 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18058 start_label,
18059 fde->dw_fde_second_begin
18060 ? fde->dw_fde_second_end : fde->dw_fde_end,
18061 section);
18063 if (list && list->dw_loc_next)
18064 gen_llsym (list);
18066 return list;
18069 /* Compute a displacement from the "steady-state frame pointer" to the
18070 frame base (often the same as the CFA), and store it in
18071 frame_pointer_fb_offset. OFFSET is added to the displacement
18072 before the latter is negated. */
18074 static void
18075 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18077 rtx reg, elim;
18079 #ifdef FRAME_POINTER_CFA_OFFSET
18080 reg = frame_pointer_rtx;
18081 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18082 #else
18083 reg = arg_pointer_rtx;
18084 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18085 #endif
18087 elim = (ira_use_lra_p
18088 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18089 : eliminate_regs (reg, VOIDmode, NULL_RTX));
18090 if (GET_CODE (elim) == PLUS)
18092 offset += INTVAL (XEXP (elim, 1));
18093 elim = XEXP (elim, 0);
18096 frame_pointer_fb_offset = -offset;
18098 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18099 in which to eliminate. This is because it's stack pointer isn't
18100 directly accessible as a register within the ISA. To work around
18101 this, assume that while we cannot provide a proper value for
18102 frame_pointer_fb_offset, we won't need one either. */
18103 frame_pointer_fb_offset_valid
18104 = ((SUPPORTS_STACK_ALIGNMENT
18105 && (elim == hard_frame_pointer_rtx
18106 || elim == stack_pointer_rtx))
18107 || elim == (frame_pointer_needed
18108 ? hard_frame_pointer_rtx
18109 : stack_pointer_rtx));
18112 /* Generate a DW_AT_name attribute given some string value to be included as
18113 the value of the attribute. */
18115 static void
18116 add_name_attribute (dw_die_ref die, const char *name_string)
18118 if (name_string != NULL && *name_string != 0)
18120 if (demangle_name_func)
18121 name_string = (*demangle_name_func) (name_string);
18123 add_AT_string (die, DW_AT_name, name_string);
18127 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18128 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18129 of TYPE accordingly.
18131 ??? This is a temporary measure until after we're able to generate
18132 regular DWARF for the complex Ada type system. */
18134 static void
18135 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18136 dw_die_ref context_die)
18138 tree dtype;
18139 dw_die_ref dtype_die;
18141 if (!lang_hooks.types.descriptive_type)
18142 return;
18144 dtype = lang_hooks.types.descriptive_type (type);
18145 if (!dtype)
18146 return;
18148 dtype_die = lookup_type_die (dtype);
18149 if (!dtype_die)
18151 gen_type_die (dtype, context_die);
18152 dtype_die = lookup_type_die (dtype);
18153 gcc_assert (dtype_die);
18156 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18159 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
18161 static const char *
18162 comp_dir_string (void)
18164 const char *wd;
18165 char *wd1;
18166 static const char *cached_wd = NULL;
18168 if (cached_wd != NULL)
18169 return cached_wd;
18171 wd = get_src_pwd ();
18172 if (wd == NULL)
18173 return NULL;
18175 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18177 int wdlen;
18179 wdlen = strlen (wd);
18180 wd1 = ggc_vec_alloc<char> (wdlen + 2);
18181 strcpy (wd1, wd);
18182 wd1 [wdlen] = DIR_SEPARATOR;
18183 wd1 [wdlen + 1] = 0;
18184 wd = wd1;
18187 cached_wd = remap_debug_filename (wd);
18188 return cached_wd;
18191 /* Generate a DW_AT_comp_dir attribute for DIE. */
18193 static void
18194 add_comp_dir_attribute (dw_die_ref die)
18196 const char * wd = comp_dir_string ();
18197 if (wd != NULL)
18198 add_AT_string (die, DW_AT_comp_dir, wd);
18201 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18202 pointer computation, ...), output a representation for that bound according
18203 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
18204 loc_list_from_tree for the meaning of CONTEXT. */
18206 static void
18207 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18208 int forms, const struct loc_descr_context *context)
18210 dw_die_ref context_die, decl_die;
18211 dw_loc_list_ref list;
18213 bool strip_conversions = true;
18215 while (strip_conversions)
18216 switch (TREE_CODE (value))
18218 case ERROR_MARK:
18219 case SAVE_EXPR:
18220 return;
18222 CASE_CONVERT:
18223 case VIEW_CONVERT_EXPR:
18224 value = TREE_OPERAND (value, 0);
18225 break;
18227 default:
18228 strip_conversions = false;
18229 break;
18232 /* If possible and permitted, output the attribute as a constant. */
18233 if ((forms & dw_scalar_form_constant) != 0
18234 && TREE_CODE (value) == INTEGER_CST)
18236 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18238 /* If HOST_WIDE_INT is big enough then represent the bound as
18239 a constant value. We need to choose a form based on
18240 whether the type is signed or unsigned. We cannot just
18241 call add_AT_unsigned if the value itself is positive
18242 (add_AT_unsigned might add the unsigned value encoded as
18243 DW_FORM_data[1248]). Some DWARF consumers will lookup the
18244 bounds type and then sign extend any unsigned values found
18245 for signed types. This is needed only for
18246 DW_AT_{lower,upper}_bound, since for most other attributes,
18247 consumers will treat DW_FORM_data[1248] as unsigned values,
18248 regardless of the underlying type. */
18249 if (prec <= HOST_BITS_PER_WIDE_INT
18250 || tree_fits_uhwi_p (value))
18252 if (TYPE_UNSIGNED (TREE_TYPE (value)))
18253 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18254 else
18255 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18257 else
18258 /* Otherwise represent the bound as an unsigned value with
18259 the precision of its type. The precision and signedness
18260 of the type will be necessary to re-interpret it
18261 unambiguously. */
18262 add_AT_wide (die, attr, value);
18263 return;
18266 /* Otherwise, if it's possible and permitted too, output a reference to
18267 another DIE. */
18268 if ((forms & dw_scalar_form_reference) != 0)
18270 tree decl = NULL_TREE;
18272 /* Some type attributes reference an outer type. For instance, the upper
18273 bound of an array may reference an embedding record (this happens in
18274 Ada). */
18275 if (TREE_CODE (value) == COMPONENT_REF
18276 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18277 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18278 decl = TREE_OPERAND (value, 1);
18280 else if (TREE_CODE (value) == VAR_DECL
18281 || TREE_CODE (value) == PARM_DECL
18282 || TREE_CODE (value) == RESULT_DECL)
18283 decl = value;
18285 if (decl != NULL_TREE)
18287 dw_die_ref decl_die = lookup_decl_die (decl);
18289 /* ??? Can this happen, or should the variable have been bound
18290 first? Probably it can, since I imagine that we try to create
18291 the types of parameters in the order in which they exist in
18292 the list, and won't have created a forward reference to a
18293 later parameter. */
18294 if (decl_die != NULL)
18296 add_AT_die_ref (die, attr, decl_die);
18297 return;
18302 /* Last chance: try to create a stack operation procedure to evaluate the
18303 value. Do nothing if even that is not possible or permitted. */
18304 if ((forms & dw_scalar_form_exprloc) == 0)
18305 return;
18307 list = loc_list_from_tree (value, 2, context);
18308 if (list == NULL || single_element_loc_list_p (list))
18310 /* If this attribute is not a reference nor constant, it is
18311 a DWARF expression rather than location description. For that
18312 loc_list_from_tree (value, 0, &context) is needed. */
18313 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18314 if (list2 && single_element_loc_list_p (list2))
18316 add_AT_loc (die, attr, list2->expr);
18317 return;
18321 /* If that failed to give a single element location list, fall back to
18322 outputting this as a reference... still if permitted. */
18323 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18324 return;
18326 if (current_function_decl == 0)
18327 context_die = comp_unit_die ();
18328 else
18329 context_die = lookup_decl_die (current_function_decl);
18331 decl_die = new_die (DW_TAG_variable, context_die, value);
18332 add_AT_flag (decl_die, DW_AT_artificial, 1);
18333 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18334 context_die);
18335 add_AT_location_description (decl_die, DW_AT_location, list);
18336 add_AT_die_ref (die, attr, decl_die);
18339 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18340 default. */
18342 static int
18343 lower_bound_default (void)
18345 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18347 case DW_LANG_C:
18348 case DW_LANG_C89:
18349 case DW_LANG_C99:
18350 case DW_LANG_C11:
18351 case DW_LANG_C_plus_plus:
18352 case DW_LANG_C_plus_plus_11:
18353 case DW_LANG_C_plus_plus_14:
18354 case DW_LANG_ObjC:
18355 case DW_LANG_ObjC_plus_plus:
18356 case DW_LANG_Java:
18357 return 0;
18358 case DW_LANG_Fortran77:
18359 case DW_LANG_Fortran90:
18360 case DW_LANG_Fortran95:
18361 case DW_LANG_Fortran03:
18362 case DW_LANG_Fortran08:
18363 return 1;
18364 case DW_LANG_UPC:
18365 case DW_LANG_D:
18366 case DW_LANG_Python:
18367 return dwarf_version >= 4 ? 0 : -1;
18368 case DW_LANG_Ada95:
18369 case DW_LANG_Ada83:
18370 case DW_LANG_Cobol74:
18371 case DW_LANG_Cobol85:
18372 case DW_LANG_Pascal83:
18373 case DW_LANG_Modula2:
18374 case DW_LANG_PLI:
18375 return dwarf_version >= 4 ? 1 : -1;
18376 default:
18377 return -1;
18381 /* Given a tree node describing an array bound (either lower or upper) output
18382 a representation for that bound. */
18384 static void
18385 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18386 tree bound, const struct loc_descr_context *context)
18388 int dflt;
18390 while (1)
18391 switch (TREE_CODE (bound))
18393 /* Strip all conversions. */
18394 CASE_CONVERT:
18395 case VIEW_CONVERT_EXPR:
18396 bound = TREE_OPERAND (bound, 0);
18397 break;
18399 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
18400 are even omitted when they are the default. */
18401 case INTEGER_CST:
18402 /* If the value for this bound is the default one, we can even omit the
18403 attribute. */
18404 if (bound_attr == DW_AT_lower_bound
18405 && tree_fits_shwi_p (bound)
18406 && (dflt = lower_bound_default ()) != -1
18407 && tree_to_shwi (bound) == dflt)
18408 return;
18410 /* FALLTHRU */
18412 default:
18413 /* Because of the complex interaction there can be with other GNAT
18414 encodings, GDB isn't ready yet to handle proper DWARF description
18415 for self-referencial subrange bounds: let GNAT encodings do the
18416 magic in such a case. */
18417 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18418 && contains_placeholder_p (bound))
18419 return;
18421 add_scalar_info (subrange_die, bound_attr, bound,
18422 dw_scalar_form_constant
18423 | dw_scalar_form_exprloc
18424 | dw_scalar_form_reference,
18425 context);
18426 return;
18430 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18431 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18432 Note that the block of subscript information for an array type also
18433 includes information about the element type of the given array type.
18435 This function reuses previously set type and bound information if
18436 available. */
18438 static void
18439 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18441 unsigned dimension_number;
18442 tree lower, upper;
18443 dw_die_ref child = type_die->die_child;
18445 for (dimension_number = 0;
18446 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18447 type = TREE_TYPE (type), dimension_number++)
18449 tree domain = TYPE_DOMAIN (type);
18451 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18452 break;
18454 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18455 and (in GNU C only) variable bounds. Handle all three forms
18456 here. */
18458 /* Find and reuse a previously generated DW_TAG_subrange_type if
18459 available.
18461 For multi-dimensional arrays, as we iterate through the
18462 various dimensions in the enclosing for loop above, we also
18463 iterate through the DIE children and pick at each
18464 DW_TAG_subrange_type previously generated (if available).
18465 Each child DW_TAG_subrange_type DIE describes the range of
18466 the current dimension. At this point we should have as many
18467 DW_TAG_subrange_type's as we have dimensions in the
18468 array. */
18469 dw_die_ref subrange_die = NULL;
18470 if (child)
18471 while (1)
18473 child = child->die_sib;
18474 if (child->die_tag == DW_TAG_subrange_type)
18475 subrange_die = child;
18476 if (child == type_die->die_child)
18478 /* If we wrapped around, stop looking next time. */
18479 child = NULL;
18480 break;
18482 if (child->die_tag == DW_TAG_subrange_type)
18483 break;
18485 if (!subrange_die)
18486 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18488 if (domain)
18490 /* We have an array type with specified bounds. */
18491 lower = TYPE_MIN_VALUE (domain);
18492 upper = TYPE_MAX_VALUE (domain);
18494 /* Define the index type. */
18495 if (TREE_TYPE (domain)
18496 && !get_AT (subrange_die, DW_AT_type))
18498 /* ??? This is probably an Ada unnamed subrange type. Ignore the
18499 TREE_TYPE field. We can't emit debug info for this
18500 because it is an unnamed integral type. */
18501 if (TREE_CODE (domain) == INTEGER_TYPE
18502 && TYPE_NAME (domain) == NULL_TREE
18503 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18504 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18506 else
18507 add_type_attribute (subrange_die, TREE_TYPE (domain),
18508 TYPE_UNQUALIFIED, false, type_die);
18511 /* ??? If upper is NULL, the array has unspecified length,
18512 but it does have a lower bound. This happens with Fortran
18513 dimension arr(N:*)
18514 Since the debugger is definitely going to need to know N
18515 to produce useful results, go ahead and output the lower
18516 bound solo, and hope the debugger can cope. */
18518 if (!get_AT (subrange_die, DW_AT_lower_bound))
18519 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18520 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18521 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18524 /* Otherwise we have an array type with an unspecified length. The
18525 DWARF-2 spec does not say how to handle this; let's just leave out the
18526 bounds. */
18530 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
18532 static void
18533 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18535 dw_die_ref decl_die;
18536 HOST_WIDE_INT size;
18537 dw_loc_descr_ref size_expr = NULL;
18539 switch (TREE_CODE (tree_node))
18541 case ERROR_MARK:
18542 size = 0;
18543 break;
18544 case ENUMERAL_TYPE:
18545 case RECORD_TYPE:
18546 case UNION_TYPE:
18547 case QUAL_UNION_TYPE:
18548 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18549 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18551 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18552 return;
18554 size_expr = type_byte_size (tree_node, &size);
18555 break;
18556 case FIELD_DECL:
18557 /* For a data member of a struct or union, the DW_AT_byte_size is
18558 generally given as the number of bytes normally allocated for an
18559 object of the *declared* type of the member itself. This is true
18560 even for bit-fields. */
18561 size = int_size_in_bytes (field_type (tree_node));
18562 break;
18563 default:
18564 gcc_unreachable ();
18567 /* Support for dynamically-sized objects was introduced by DWARFv3.
18568 At the moment, GDB does not handle variable byte sizes very well,
18569 though. */
18570 if ((dwarf_version >= 3 || !dwarf_strict)
18571 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18572 && size_expr != NULL)
18573 add_AT_loc (die, DW_AT_byte_size, size_expr);
18575 /* Note that `size' might be -1 when we get to this point. If it is, that
18576 indicates that the byte size of the entity in question is variable and
18577 that we could not generate a DWARF expression that computes it. */
18578 if (size >= 0)
18579 add_AT_unsigned (die, DW_AT_byte_size, size);
18582 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18583 which specifies the distance in bits from the highest order bit of the
18584 "containing object" for the bit-field to the highest order bit of the
18585 bit-field itself.
18587 For any given bit-field, the "containing object" is a hypothetical object
18588 (of some integral or enum type) within which the given bit-field lives. The
18589 type of this hypothetical "containing object" is always the same as the
18590 declared type of the individual bit-field itself. The determination of the
18591 exact location of the "containing object" for a bit-field is rather
18592 complicated. It's handled by the `field_byte_offset' function (above).
18594 CTX is required: see the comment for VLR_CONTEXT.
18596 Note that it is the size (in bytes) of the hypothetical "containing object"
18597 which will be given in the DW_AT_byte_size attribute for this bit-field.
18598 (See `byte_size_attribute' above). */
18600 static inline void
18601 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18603 HOST_WIDE_INT object_offset_in_bytes;
18604 tree original_type = DECL_BIT_FIELD_TYPE (decl);
18605 HOST_WIDE_INT bitpos_int;
18606 HOST_WIDE_INT highest_order_object_bit_offset;
18607 HOST_WIDE_INT highest_order_field_bit_offset;
18608 HOST_WIDE_INT bit_offset;
18610 field_byte_offset (decl, ctx, &object_offset_in_bytes);
18612 /* Must be a field and a bit field. */
18613 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18615 /* We can't yet handle bit-fields whose offsets are variable, so if we
18616 encounter such things, just return without generating any attribute
18617 whatsoever. Likewise for variable or too large size. */
18618 if (! tree_fits_shwi_p (bit_position (decl))
18619 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18620 return;
18622 bitpos_int = int_bit_position (decl);
18624 /* Note that the bit offset is always the distance (in bits) from the
18625 highest-order bit of the "containing object" to the highest-order bit of
18626 the bit-field itself. Since the "high-order end" of any object or field
18627 is different on big-endian and little-endian machines, the computation
18628 below must take account of these differences. */
18629 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18630 highest_order_field_bit_offset = bitpos_int;
18632 if (! BYTES_BIG_ENDIAN)
18634 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18635 highest_order_object_bit_offset +=
18636 simple_type_size_in_bits (original_type);
18639 bit_offset
18640 = (! BYTES_BIG_ENDIAN
18641 ? highest_order_object_bit_offset - highest_order_field_bit_offset
18642 : highest_order_field_bit_offset - highest_order_object_bit_offset);
18644 if (bit_offset < 0)
18645 add_AT_int (die, DW_AT_bit_offset, bit_offset);
18646 else
18647 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18650 /* For a FIELD_DECL node which represents a bit field, output an attribute
18651 which specifies the length in bits of the given field. */
18653 static inline void
18654 add_bit_size_attribute (dw_die_ref die, tree decl)
18656 /* Must be a field and a bit field. */
18657 gcc_assert (TREE_CODE (decl) == FIELD_DECL
18658 && DECL_BIT_FIELD_TYPE (decl));
18660 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18661 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18664 /* If the compiled language is ANSI C, then add a 'prototyped'
18665 attribute, if arg types are given for the parameters of a function. */
18667 static inline void
18668 add_prototyped_attribute (dw_die_ref die, tree func_type)
18670 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18672 case DW_LANG_C:
18673 case DW_LANG_C89:
18674 case DW_LANG_C99:
18675 case DW_LANG_C11:
18676 case DW_LANG_ObjC:
18677 if (prototype_p (func_type))
18678 add_AT_flag (die, DW_AT_prototyped, 1);
18679 break;
18680 default:
18681 break;
18685 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
18686 by looking in the type declaration, the object declaration equate table or
18687 the block mapping. */
18689 static inline dw_die_ref
18690 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18692 dw_die_ref origin_die = NULL;
18694 if (TREE_CODE (origin) != FUNCTION_DECL
18695 && TREE_CODE (origin) != BLOCK)
18697 /* We may have gotten separated from the block for the inlined
18698 function, if we're in an exception handler or some such; make
18699 sure that the abstract function has been written out.
18701 Doing this for nested functions is wrong, however; functions are
18702 distinct units, and our context might not even be inline. */
18703 tree fn = origin;
18705 if (TYPE_P (fn))
18706 fn = TYPE_STUB_DECL (fn);
18708 fn = decl_function_context (fn);
18709 if (fn)
18710 dwarf2out_abstract_function (fn);
18713 if (DECL_P (origin))
18714 origin_die = lookup_decl_die (origin);
18715 else if (TYPE_P (origin))
18716 origin_die = lookup_type_die (origin);
18717 else if (TREE_CODE (origin) == BLOCK)
18718 origin_die = BLOCK_DIE (origin);
18720 /* XXX: Functions that are never lowered don't always have correct block
18721 trees (in the case of java, they simply have no block tree, in some other
18722 languages). For these functions, there is nothing we can really do to
18723 output correct debug info for inlined functions in all cases. Rather
18724 than die, we'll just produce deficient debug info now, in that we will
18725 have variables without a proper abstract origin. In the future, when all
18726 functions are lowered, we should re-add a gcc_assert (origin_die)
18727 here. */
18729 if (origin_die)
18730 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18731 return origin_die;
18734 /* We do not currently support the pure_virtual attribute. */
18736 static inline void
18737 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18739 if (DECL_VINDEX (func_decl))
18741 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18743 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18744 add_AT_loc (die, DW_AT_vtable_elem_location,
18745 new_loc_descr (DW_OP_constu,
18746 tree_to_shwi (DECL_VINDEX (func_decl)),
18747 0));
18749 /* GNU extension: Record what type this method came from originally. */
18750 if (debug_info_level > DINFO_LEVEL_TERSE
18751 && DECL_CONTEXT (func_decl))
18752 add_AT_die_ref (die, DW_AT_containing_type,
18753 lookup_type_die (DECL_CONTEXT (func_decl)));
18757 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18758 given decl. This used to be a vendor extension until after DWARF 4
18759 standardized it. */
18761 static void
18762 add_linkage_attr (dw_die_ref die, tree decl)
18764 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18766 /* Mimic what assemble_name_raw does with a leading '*'. */
18767 if (name[0] == '*')
18768 name = &name[1];
18770 if (dwarf_version >= 4)
18771 add_AT_string (die, DW_AT_linkage_name, name);
18772 else
18773 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18776 /* Add source coordinate attributes for the given decl. */
18778 static void
18779 add_src_coords_attributes (dw_die_ref die, tree decl)
18781 expanded_location s;
18783 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18784 return;
18785 s = expand_location (DECL_SOURCE_LOCATION (decl));
18786 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18787 add_AT_unsigned (die, DW_AT_decl_line, s.line);
18790 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
18792 static void
18793 add_linkage_name_raw (dw_die_ref die, tree decl)
18795 /* Defer until we have an assembler name set. */
18796 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18798 limbo_die_node *asm_name;
18800 asm_name = ggc_cleared_alloc<limbo_die_node> ();
18801 asm_name->die = die;
18802 asm_name->created_for = decl;
18803 asm_name->next = deferred_asm_name;
18804 deferred_asm_name = asm_name;
18806 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18807 add_linkage_attr (die, decl);
18810 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
18812 static void
18813 add_linkage_name (dw_die_ref die, tree decl)
18815 if (debug_info_level > DINFO_LEVEL_NONE
18816 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18817 && TREE_PUBLIC (decl)
18818 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18819 && die->die_tag != DW_TAG_member)
18820 add_linkage_name_raw (die, decl);
18823 /* Add a DW_AT_name attribute and source coordinate attribute for the
18824 given decl, but only if it actually has a name. */
18826 static void
18827 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18829 tree decl_name;
18831 decl_name = DECL_NAME (decl);
18832 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18834 const char *name = dwarf2_name (decl, 0);
18835 if (name)
18836 add_name_attribute (die, name);
18837 if (! DECL_ARTIFICIAL (decl))
18838 add_src_coords_attributes (die, decl);
18840 add_linkage_name (die, decl);
18843 #ifdef VMS_DEBUGGING_INFO
18844 /* Get the function's name, as described by its RTL. This may be different
18845 from the DECL_NAME name used in the source file. */
18846 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18848 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18849 XEXP (DECL_RTL (decl), 0), false);
18850 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18852 #endif /* VMS_DEBUGGING_INFO */
18855 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
18857 static void
18858 add_discr_value (dw_die_ref die, dw_discr_value *value)
18860 dw_attr_node attr;
18862 attr.dw_attr = DW_AT_discr_value;
18863 attr.dw_attr_val.val_class = dw_val_class_discr_value;
18864 attr.dw_attr_val.val_entry = NULL;
18865 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18866 if (value->pos)
18867 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18868 else
18869 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18870 add_dwarf_attr (die, &attr);
18873 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
18875 static void
18876 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18878 dw_attr_node attr;
18880 attr.dw_attr = DW_AT_discr_list;
18881 attr.dw_attr_val.val_class = dw_val_class_discr_list;
18882 attr.dw_attr_val.val_entry = NULL;
18883 attr.dw_attr_val.v.val_discr_list = discr_list;
18884 add_dwarf_attr (die, &attr);
18887 static inline dw_discr_list_ref
18888 AT_discr_list (dw_attr_node *attr)
18890 return attr->dw_attr_val.v.val_discr_list;
18893 #ifdef VMS_DEBUGGING_INFO
18894 /* Output the debug main pointer die for VMS */
18896 void
18897 dwarf2out_vms_debug_main_pointer (void)
18899 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18900 dw_die_ref die;
18902 /* Allocate the VMS debug main subprogram die. */
18903 die = ggc_cleared_alloc<die_node> ();
18904 die->die_tag = DW_TAG_subprogram;
18905 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18906 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18907 current_function_funcdef_no);
18908 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18910 /* Make it the first child of comp_unit_die (). */
18911 die->die_parent = comp_unit_die ();
18912 if (comp_unit_die ()->die_child)
18914 die->die_sib = comp_unit_die ()->die_child->die_sib;
18915 comp_unit_die ()->die_child->die_sib = die;
18917 else
18919 die->die_sib = die;
18920 comp_unit_die ()->die_child = die;
18923 #endif /* VMS_DEBUGGING_INFO */
18925 /* Push a new declaration scope. */
18927 static void
18928 push_decl_scope (tree scope)
18930 vec_safe_push (decl_scope_table, scope);
18933 /* Pop a declaration scope. */
18935 static inline void
18936 pop_decl_scope (void)
18938 decl_scope_table->pop ();
18941 /* walk_tree helper function for uses_local_type, below. */
18943 static tree
18944 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18946 if (!TYPE_P (*tp))
18947 *walk_subtrees = 0;
18948 else
18950 tree name = TYPE_NAME (*tp);
18951 if (name && DECL_P (name) && decl_function_context (name))
18952 return *tp;
18954 return NULL_TREE;
18957 /* If TYPE involves a function-local type (including a local typedef to a
18958 non-local type), returns that type; otherwise returns NULL_TREE. */
18960 static tree
18961 uses_local_type (tree type)
18963 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
18964 return used;
18967 /* Return the DIE for the scope that immediately contains this type.
18968 Non-named types that do not involve a function-local type get global
18969 scope. Named types nested in namespaces or other types get their
18970 containing scope. All other types (i.e. function-local named types) get
18971 the current active scope. */
18973 static dw_die_ref
18974 scope_die_for (tree t, dw_die_ref context_die)
18976 dw_die_ref scope_die = NULL;
18977 tree containing_scope;
18979 /* Non-types always go in the current scope. */
18980 gcc_assert (TYPE_P (t));
18982 /* Use the scope of the typedef, rather than the scope of the type
18983 it refers to. */
18984 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
18985 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
18986 else
18987 containing_scope = TYPE_CONTEXT (t);
18989 /* Use the containing namespace if there is one. */
18990 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18992 if (context_die == lookup_decl_die (containing_scope))
18993 /* OK */;
18994 else if (debug_info_level > DINFO_LEVEL_TERSE)
18995 context_die = get_context_die (containing_scope);
18996 else
18997 containing_scope = NULL_TREE;
19000 /* Ignore function type "scopes" from the C frontend. They mean that
19001 a tagged type is local to a parmlist of a function declarator, but
19002 that isn't useful to DWARF. */
19003 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19004 containing_scope = NULL_TREE;
19006 if (SCOPE_FILE_SCOPE_P (containing_scope))
19008 /* If T uses a local type keep it local as well, to avoid references
19009 to function-local DIEs from outside the function. */
19010 if (current_function_decl && uses_local_type (t))
19011 scope_die = context_die;
19012 else
19013 scope_die = comp_unit_die ();
19015 else if (TYPE_P (containing_scope))
19017 /* For types, we can just look up the appropriate DIE. */
19018 if (debug_info_level > DINFO_LEVEL_TERSE)
19019 scope_die = get_context_die (containing_scope);
19020 else
19022 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19023 if (scope_die == NULL)
19024 scope_die = comp_unit_die ();
19027 else
19028 scope_die = context_die;
19030 return scope_die;
19033 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
19035 static inline int
19036 local_scope_p (dw_die_ref context_die)
19038 for (; context_die; context_die = context_die->die_parent)
19039 if (context_die->die_tag == DW_TAG_inlined_subroutine
19040 || context_die->die_tag == DW_TAG_subprogram)
19041 return 1;
19043 return 0;
19046 /* Returns nonzero if CONTEXT_DIE is a class. */
19048 static inline int
19049 class_scope_p (dw_die_ref context_die)
19051 return (context_die
19052 && (context_die->die_tag == DW_TAG_structure_type
19053 || context_die->die_tag == DW_TAG_class_type
19054 || context_die->die_tag == DW_TAG_interface_type
19055 || context_die->die_tag == DW_TAG_union_type));
19058 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19059 whether or not to treat a DIE in this context as a declaration. */
19061 static inline int
19062 class_or_namespace_scope_p (dw_die_ref context_die)
19064 return (class_scope_p (context_die)
19065 || (context_die && context_die->die_tag == DW_TAG_namespace));
19068 /* Many forms of DIEs require a "type description" attribute. This
19069 routine locates the proper "type descriptor" die for the type given
19070 by 'type' plus any additional qualifiers given by 'cv_quals', and
19071 adds a DW_AT_type attribute below the given die. */
19073 static void
19074 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19075 bool reverse, dw_die_ref context_die)
19077 enum tree_code code = TREE_CODE (type);
19078 dw_die_ref type_die = NULL;
19080 /* ??? If this type is an unnamed subrange type of an integral, floating-point
19081 or fixed-point type, use the inner type. This is because we have no
19082 support for unnamed types in base_type_die. This can happen if this is
19083 an Ada subrange type. Correct solution is emit a subrange type die. */
19084 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19085 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19086 type = TREE_TYPE (type), code = TREE_CODE (type);
19088 if (code == ERROR_MARK
19089 /* Handle a special case. For functions whose return type is void, we
19090 generate *no* type attribute. (Note that no object may have type
19091 `void', so this only applies to function return types). */
19092 || code == VOID_TYPE)
19093 return;
19095 type_die = modified_type_die (type,
19096 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19097 reverse,
19098 context_die);
19100 if (type_die != NULL)
19101 add_AT_die_ref (object_die, DW_AT_type, type_die);
19104 /* Given an object die, add the calling convention attribute for the
19105 function call type. */
19106 static void
19107 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19109 enum dwarf_calling_convention value = DW_CC_normal;
19111 value = ((enum dwarf_calling_convention)
19112 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19114 if (is_fortran ()
19115 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19117 /* DWARF 2 doesn't provide a way to identify a program's source-level
19118 entry point. DW_AT_calling_convention attributes are only meant
19119 to describe functions' calling conventions. However, lacking a
19120 better way to signal the Fortran main program, we used this for
19121 a long time, following existing custom. Now, DWARF 4 has
19122 DW_AT_main_subprogram, which we add below, but some tools still
19123 rely on the old way, which we thus keep. */
19124 value = DW_CC_program;
19126 if (dwarf_version >= 4 || !dwarf_strict)
19127 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19130 /* Only add the attribute if the backend requests it, and
19131 is not DW_CC_normal. */
19132 if (value && (value != DW_CC_normal))
19133 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19136 /* Given a tree pointer to a struct, class, union, or enum type node, return
19137 a pointer to the (string) tag name for the given type, or zero if the type
19138 was declared without a tag. */
19140 static const char *
19141 type_tag (const_tree type)
19143 const char *name = 0;
19145 if (TYPE_NAME (type) != 0)
19147 tree t = 0;
19149 /* Find the IDENTIFIER_NODE for the type name. */
19150 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19151 && !TYPE_NAMELESS (type))
19152 t = TYPE_NAME (type);
19154 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19155 a TYPE_DECL node, regardless of whether or not a `typedef' was
19156 involved. */
19157 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19158 && ! DECL_IGNORED_P (TYPE_NAME (type)))
19160 /* We want to be extra verbose. Don't call dwarf_name if
19161 DECL_NAME isn't set. The default hook for decl_printable_name
19162 doesn't like that, and in this context it's correct to return
19163 0, instead of "<anonymous>" or the like. */
19164 if (DECL_NAME (TYPE_NAME (type))
19165 && !DECL_NAMELESS (TYPE_NAME (type)))
19166 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19169 /* Now get the name as a string, or invent one. */
19170 if (!name && t != 0)
19171 name = IDENTIFIER_POINTER (t);
19174 return (name == 0 || *name == '\0') ? 0 : name;
19177 /* Return the type associated with a data member, make a special check
19178 for bit field types. */
19180 static inline tree
19181 member_declared_type (const_tree member)
19183 return (DECL_BIT_FIELD_TYPE (member)
19184 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19187 /* Get the decl's label, as described by its RTL. This may be different
19188 from the DECL_NAME name used in the source file. */
19190 #if 0
19191 static const char *
19192 decl_start_label (tree decl)
19194 rtx x;
19195 const char *fnname;
19197 x = DECL_RTL (decl);
19198 gcc_assert (MEM_P (x));
19200 x = XEXP (x, 0);
19201 gcc_assert (GET_CODE (x) == SYMBOL_REF);
19203 fnname = XSTR (x, 0);
19204 return fnname;
19206 #endif
19208 /* For variable-length arrays that have been previously generated, but
19209 may be incomplete due to missing subscript info, fill the subscript
19210 info. Return TRUE if this is one of those cases. */
19211 static bool
19212 fill_variable_array_bounds (tree type)
19214 if (TREE_ASM_WRITTEN (type)
19215 && TREE_CODE (type) == ARRAY_TYPE
19216 && variably_modified_type_p (type, NULL))
19218 dw_die_ref array_die = lookup_type_die (type);
19219 if (!array_die)
19220 return false;
19221 add_subscript_info (array_die, type, !is_ada ());
19222 return true;
19224 return false;
19227 /* These routines generate the internal representation of the DIE's for
19228 the compilation unit. Debugging information is collected by walking
19229 the declaration trees passed in from dwarf2out_decl(). */
19231 static void
19232 gen_array_type_die (tree type, dw_die_ref context_die)
19234 dw_die_ref array_die;
19236 /* GNU compilers represent multidimensional array types as sequences of one
19237 dimensional array types whose element types are themselves array types.
19238 We sometimes squish that down to a single array_type DIE with multiple
19239 subscripts in the Dwarf debugging info. The draft Dwarf specification
19240 say that we are allowed to do this kind of compression in C, because
19241 there is no difference between an array of arrays and a multidimensional
19242 array. We don't do this for Ada to remain as close as possible to the
19243 actual representation, which is especially important against the language
19244 flexibilty wrt arrays of variable size. */
19246 bool collapse_nested_arrays = !is_ada ();
19248 if (fill_variable_array_bounds (type))
19249 return;
19251 dw_die_ref scope_die = scope_die_for (type, context_die);
19252 tree element_type;
19254 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19255 DW_TAG_string_type doesn't have DW_AT_type attribute). */
19256 if (TYPE_STRING_FLAG (type)
19257 && TREE_CODE (type) == ARRAY_TYPE
19258 && is_fortran ()
19259 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19261 HOST_WIDE_INT size;
19263 array_die = new_die (DW_TAG_string_type, scope_die, type);
19264 add_name_attribute (array_die, type_tag (type));
19265 equate_type_number_to_die (type, array_die);
19266 size = int_size_in_bytes (type);
19267 if (size >= 0)
19268 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19269 else if (TYPE_DOMAIN (type) != NULL_TREE
19270 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
19272 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19273 tree rszdecl = szdecl;
19274 HOST_WIDE_INT rsize = 0;
19276 size = int_size_in_bytes (TREE_TYPE (szdecl));
19277 if (!DECL_P (szdecl))
19279 if (TREE_CODE (szdecl) == INDIRECT_REF
19280 && DECL_P (TREE_OPERAND (szdecl, 0)))
19282 rszdecl = TREE_OPERAND (szdecl, 0);
19283 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
19284 if (rsize <= 0)
19285 size = 0;
19287 else
19288 size = 0;
19290 if (size > 0)
19292 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19293 if (loc == NULL
19294 && early_dwarf
19295 && current_function_decl
19296 && DECL_CONTEXT (rszdecl) == current_function_decl)
19298 dw_die_ref ref = lookup_decl_die (rszdecl);
19299 dw_loc_descr_ref l = NULL;
19300 if (ref)
19302 l = new_loc_descr (DW_OP_call4, 0, 0);
19303 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19304 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
19305 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
19307 else if (TREE_CODE (rszdecl) == PARM_DECL
19308 && string_types)
19310 l = new_loc_descr (DW_OP_call4, 0, 0);
19311 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19312 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
19313 string_types->safe_push (array_die);
19315 if (l && rszdecl != szdecl)
19317 if (rsize == DWARF2_ADDR_SIZE)
19318 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
19319 0, 0));
19320 else
19321 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
19322 rsize, 0));
19324 if (l)
19325 loc = new_loc_list (l, NULL, NULL, NULL);
19327 if (loc)
19329 add_AT_location_description (array_die, DW_AT_string_length,
19330 loc);
19331 if (size != DWARF2_ADDR_SIZE)
19332 add_AT_unsigned (array_die, dwarf_version >= 5
19333 ? DW_AT_string_length_byte_size
19334 : DW_AT_byte_size, size);
19338 return;
19341 array_die = new_die (DW_TAG_array_type, scope_die, type);
19342 add_name_attribute (array_die, type_tag (type));
19343 equate_type_number_to_die (type, array_die);
19345 if (TREE_CODE (type) == VECTOR_TYPE)
19346 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19348 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
19349 if (is_fortran ()
19350 && TREE_CODE (type) == ARRAY_TYPE
19351 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19352 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19353 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19355 #if 0
19356 /* We default the array ordering. SDB will probably do
19357 the right things even if DW_AT_ordering is not present. It's not even
19358 an issue until we start to get into multidimensional arrays anyway. If
19359 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19360 then we'll have to put the DW_AT_ordering attribute back in. (But if
19361 and when we find out that we need to put these in, we will only do so
19362 for multidimensional arrays. */
19363 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19364 #endif
19366 if (TREE_CODE (type) == VECTOR_TYPE)
19368 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
19369 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19370 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19371 add_bound_info (subrange_die, DW_AT_upper_bound,
19372 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19374 else
19375 add_subscript_info (array_die, type, collapse_nested_arrays);
19377 /* Add representation of the type of the elements of this array type and
19378 emit the corresponding DIE if we haven't done it already. */
19379 element_type = TREE_TYPE (type);
19380 if (collapse_nested_arrays)
19381 while (TREE_CODE (element_type) == ARRAY_TYPE)
19383 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19384 break;
19385 element_type = TREE_TYPE (element_type);
19388 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19389 TREE_CODE (type) == ARRAY_TYPE
19390 && TYPE_REVERSE_STORAGE_ORDER (type),
19391 context_die);
19393 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19394 if (TYPE_ARTIFICIAL (type))
19395 add_AT_flag (array_die, DW_AT_artificial, 1);
19397 if (get_AT (array_die, DW_AT_name))
19398 add_pubtype (type, array_die);
19401 /* After all arguments are created, adjust any DW_TAG_string_type
19402 DIEs DW_AT_string_length attributes. */
19404 static void
19405 adjust_string_types (void)
19407 dw_die_ref array_die;
19408 unsigned int i;
19409 FOR_EACH_VEC_ELT (*string_types, i, array_die)
19411 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
19412 if (a == NULL)
19413 continue;
19414 dw_loc_descr_ref loc = AT_loc (a);
19415 gcc_assert (loc->dw_loc_opc == DW_OP_call4
19416 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
19417 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
19418 if (ref)
19420 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19421 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
19422 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
19424 else
19426 remove_AT (array_die, DW_AT_string_length);
19427 remove_AT (array_die, dwarf_version >= 5
19428 ? DW_AT_string_length_byte_size
19429 : DW_AT_byte_size);
19434 /* This routine generates DIE for array with hidden descriptor, details
19435 are filled into *info by a langhook. */
19437 static void
19438 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19439 dw_die_ref context_die)
19441 const dw_die_ref scope_die = scope_die_for (type, context_die);
19442 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19443 const struct loc_descr_context context = { type, info->base_decl, NULL };
19444 int dim;
19446 add_name_attribute (array_die, type_tag (type));
19447 equate_type_number_to_die (type, array_die);
19449 if (info->ndimensions > 1)
19450 switch (info->ordering)
19452 case array_descr_ordering_row_major:
19453 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19454 break;
19455 case array_descr_ordering_column_major:
19456 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19457 break;
19458 default:
19459 break;
19462 if (dwarf_version >= 3 || !dwarf_strict)
19464 if (info->data_location)
19465 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19466 dw_scalar_form_exprloc, &context);
19467 if (info->associated)
19468 add_scalar_info (array_die, DW_AT_associated, info->associated,
19469 dw_scalar_form_constant
19470 | dw_scalar_form_exprloc
19471 | dw_scalar_form_reference, &context);
19472 if (info->allocated)
19473 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19474 dw_scalar_form_constant
19475 | dw_scalar_form_exprloc
19476 | dw_scalar_form_reference, &context);
19477 if (info->stride)
19479 const enum dwarf_attribute attr
19480 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19481 const int forms
19482 = (info->stride_in_bits)
19483 ? dw_scalar_form_constant
19484 : (dw_scalar_form_constant
19485 | dw_scalar_form_exprloc
19486 | dw_scalar_form_reference);
19488 add_scalar_info (array_die, attr, info->stride, forms, &context);
19492 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19494 for (dim = 0; dim < info->ndimensions; dim++)
19496 dw_die_ref subrange_die
19497 = new_die (DW_TAG_subrange_type, array_die, NULL);
19499 if (info->dimen[dim].bounds_type)
19500 add_type_attribute (subrange_die,
19501 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19502 false, context_die);
19503 if (info->dimen[dim].lower_bound)
19504 add_bound_info (subrange_die, DW_AT_lower_bound,
19505 info->dimen[dim].lower_bound, &context);
19506 if (info->dimen[dim].upper_bound)
19507 add_bound_info (subrange_die, DW_AT_upper_bound,
19508 info->dimen[dim].upper_bound, &context);
19509 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19510 add_scalar_info (subrange_die, DW_AT_byte_stride,
19511 info->dimen[dim].stride,
19512 dw_scalar_form_constant
19513 | dw_scalar_form_exprloc
19514 | dw_scalar_form_reference,
19515 &context);
19518 gen_type_die (info->element_type, context_die);
19519 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19520 TREE_CODE (type) == ARRAY_TYPE
19521 && TYPE_REVERSE_STORAGE_ORDER (type),
19522 context_die);
19524 if (get_AT (array_die, DW_AT_name))
19525 add_pubtype (type, array_die);
19528 #if 0
19529 static void
19530 gen_entry_point_die (tree decl, dw_die_ref context_die)
19532 tree origin = decl_ultimate_origin (decl);
19533 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19535 if (origin != NULL)
19536 add_abstract_origin_attribute (decl_die, origin);
19537 else
19539 add_name_and_src_coords_attributes (decl_die, decl);
19540 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19541 TYPE_UNQUALIFIED, false, context_die);
19544 if (DECL_ABSTRACT_P (decl))
19545 equate_decl_number_to_die (decl, decl_die);
19546 else
19547 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19549 #endif
19551 /* Walk through the list of incomplete types again, trying once more to
19552 emit full debugging info for them. */
19554 static void
19555 retry_incomplete_types (void)
19557 set_early_dwarf s;
19558 int i;
19560 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19561 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19562 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19563 vec_safe_truncate (incomplete_types, 0);
19566 /* Determine what tag to use for a record type. */
19568 static enum dwarf_tag
19569 record_type_tag (tree type)
19571 if (! lang_hooks.types.classify_record)
19572 return DW_TAG_structure_type;
19574 switch (lang_hooks.types.classify_record (type))
19576 case RECORD_IS_STRUCT:
19577 return DW_TAG_structure_type;
19579 case RECORD_IS_CLASS:
19580 return DW_TAG_class_type;
19582 case RECORD_IS_INTERFACE:
19583 if (dwarf_version >= 3 || !dwarf_strict)
19584 return DW_TAG_interface_type;
19585 return DW_TAG_structure_type;
19587 default:
19588 gcc_unreachable ();
19592 /* Generate a DIE to represent an enumeration type. Note that these DIEs
19593 include all of the information about the enumeration values also. Each
19594 enumerated type name/value is listed as a child of the enumerated type
19595 DIE. */
19597 static dw_die_ref
19598 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19600 dw_die_ref type_die = lookup_type_die (type);
19602 if (type_die == NULL)
19604 type_die = new_die (DW_TAG_enumeration_type,
19605 scope_die_for (type, context_die), type);
19606 equate_type_number_to_die (type, type_die);
19607 add_name_attribute (type_die, type_tag (type));
19608 if (dwarf_version >= 4 || !dwarf_strict)
19610 if (ENUM_IS_SCOPED (type))
19611 add_AT_flag (type_die, DW_AT_enum_class, 1);
19612 if (ENUM_IS_OPAQUE (type))
19613 add_AT_flag (type_die, DW_AT_declaration, 1);
19616 else if (! TYPE_SIZE (type))
19617 return type_die;
19618 else
19619 remove_AT (type_die, DW_AT_declaration);
19621 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
19622 given enum type is incomplete, do not generate the DW_AT_byte_size
19623 attribute or the DW_AT_element_list attribute. */
19624 if (TYPE_SIZE (type))
19626 tree link;
19628 TREE_ASM_WRITTEN (type) = 1;
19629 add_byte_size_attribute (type_die, type);
19630 if (dwarf_version >= 3 || !dwarf_strict)
19632 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19633 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19634 context_die);
19636 if (TYPE_STUB_DECL (type) != NULL_TREE)
19638 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19639 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19642 /* If the first reference to this type was as the return type of an
19643 inline function, then it may not have a parent. Fix this now. */
19644 if (type_die->die_parent == NULL)
19645 add_child_die (scope_die_for (type, context_die), type_die);
19647 for (link = TYPE_VALUES (type);
19648 link != NULL; link = TREE_CHAIN (link))
19650 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19651 tree value = TREE_VALUE (link);
19653 add_name_attribute (enum_die,
19654 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19656 if (TREE_CODE (value) == CONST_DECL)
19657 value = DECL_INITIAL (value);
19659 if (simple_type_size_in_bits (TREE_TYPE (value))
19660 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19662 /* For constant forms created by add_AT_unsigned DWARF
19663 consumers (GDB, elfutils, etc.) always zero extend
19664 the value. Only when the actual value is negative
19665 do we need to use add_AT_int to generate a constant
19666 form that can represent negative values. */
19667 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19668 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19669 add_AT_unsigned (enum_die, DW_AT_const_value,
19670 (unsigned HOST_WIDE_INT) val);
19671 else
19672 add_AT_int (enum_die, DW_AT_const_value, val);
19674 else
19675 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
19676 that here. TODO: This should be re-worked to use correct
19677 signed/unsigned double tags for all cases. */
19678 add_AT_wide (enum_die, DW_AT_const_value, value);
19681 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19682 if (TYPE_ARTIFICIAL (type))
19683 add_AT_flag (type_die, DW_AT_artificial, 1);
19685 else
19686 add_AT_flag (type_die, DW_AT_declaration, 1);
19688 add_pubtype (type, type_die);
19690 return type_die;
19693 /* Generate a DIE to represent either a real live formal parameter decl or to
19694 represent just the type of some formal parameter position in some function
19695 type.
19697 Note that this routine is a bit unusual because its argument may be a
19698 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19699 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19700 node. If it's the former then this function is being called to output a
19701 DIE to represent a formal parameter object (or some inlining thereof). If
19702 it's the latter, then this function is only being called to output a
19703 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19704 argument type of some subprogram type.
19705 If EMIT_NAME_P is true, name and source coordinate attributes
19706 are emitted. */
19708 static dw_die_ref
19709 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19710 dw_die_ref context_die)
19712 tree node_or_origin = node ? node : origin;
19713 tree ultimate_origin;
19714 dw_die_ref parm_die = NULL;
19716 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19718 parm_die = lookup_decl_die (node);
19720 /* If the contexts differ, we may not be talking about the same
19721 thing. */
19722 if (parm_die && parm_die->die_parent != context_die)
19724 if (!DECL_ABSTRACT_P (node))
19726 /* This can happen when creating an inlined instance, in
19727 which case we need to create a new DIE that will get
19728 annotated with DW_AT_abstract_origin. */
19729 parm_die = NULL;
19731 else
19733 /* FIXME: Reuse DIE even with a differing context.
19735 This can happen when calling
19736 dwarf2out_abstract_function to build debug info for
19737 the abstract instance of a function for which we have
19738 already generated a DIE in
19739 dwarf2out_early_global_decl.
19741 Once we remove dwarf2out_abstract_function, we should
19742 have a call to gcc_unreachable here. */
19746 if (parm_die && parm_die->die_parent == NULL)
19748 /* Check that parm_die already has the right attributes that
19749 we would have added below. If any attributes are
19750 missing, fall through to add them. */
19751 if (! DECL_ABSTRACT_P (node_or_origin)
19752 && !get_AT (parm_die, DW_AT_location)
19753 && !get_AT (parm_die, DW_AT_const_value))
19754 /* We are missing location info, and are about to add it. */
19756 else
19758 add_child_die (context_die, parm_die);
19759 return parm_die;
19764 /* If we have a previously generated DIE, use it, unless this is an
19765 concrete instance (origin != NULL), in which case we need a new
19766 DIE with a corresponding DW_AT_abstract_origin. */
19767 bool reusing_die;
19768 if (parm_die && origin == NULL)
19769 reusing_die = true;
19770 else
19772 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19773 reusing_die = false;
19776 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19778 case tcc_declaration:
19779 ultimate_origin = decl_ultimate_origin (node_or_origin);
19780 if (node || ultimate_origin)
19781 origin = ultimate_origin;
19783 if (reusing_die)
19784 goto add_location;
19786 if (origin != NULL)
19787 add_abstract_origin_attribute (parm_die, origin);
19788 else if (emit_name_p)
19789 add_name_and_src_coords_attributes (parm_die, node);
19790 if (origin == NULL
19791 || (! DECL_ABSTRACT_P (node_or_origin)
19792 && variably_modified_type_p (TREE_TYPE (node_or_origin),
19793 decl_function_context
19794 (node_or_origin))))
19796 tree type = TREE_TYPE (node_or_origin);
19797 if (decl_by_reference_p (node_or_origin))
19798 add_type_attribute (parm_die, TREE_TYPE (type),
19799 TYPE_UNQUALIFIED,
19800 false, context_die);
19801 else
19802 add_type_attribute (parm_die, type,
19803 decl_quals (node_or_origin),
19804 false, context_die);
19806 if (origin == NULL && DECL_ARTIFICIAL (node))
19807 add_AT_flag (parm_die, DW_AT_artificial, 1);
19808 add_location:
19809 if (node && node != origin)
19810 equate_decl_number_to_die (node, parm_die);
19811 if (! DECL_ABSTRACT_P (node_or_origin))
19812 add_location_or_const_value_attribute (parm_die, node_or_origin,
19813 node == NULL);
19815 break;
19817 case tcc_type:
19818 /* We were called with some kind of a ..._TYPE node. */
19819 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19820 context_die);
19821 break;
19823 default:
19824 gcc_unreachable ();
19827 return parm_die;
19830 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19831 children DW_TAG_formal_parameter DIEs representing the arguments of the
19832 parameter pack.
19834 PARM_PACK must be a function parameter pack.
19835 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19836 must point to the subsequent arguments of the function PACK_ARG belongs to.
19837 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19838 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19839 following the last one for which a DIE was generated. */
19841 static dw_die_ref
19842 gen_formal_parameter_pack_die (tree parm_pack,
19843 tree pack_arg,
19844 dw_die_ref subr_die,
19845 tree *next_arg)
19847 tree arg;
19848 dw_die_ref parm_pack_die;
19850 gcc_assert (parm_pack
19851 && lang_hooks.function_parameter_pack_p (parm_pack)
19852 && subr_die);
19854 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19855 add_src_coords_attributes (parm_pack_die, parm_pack);
19857 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19859 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19860 parm_pack))
19861 break;
19862 gen_formal_parameter_die (arg, NULL,
19863 false /* Don't emit name attribute. */,
19864 parm_pack_die);
19866 if (next_arg)
19867 *next_arg = arg;
19868 return parm_pack_die;
19871 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19872 at the end of an (ANSI prototyped) formal parameters list. */
19874 static void
19875 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19877 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19880 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19881 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19882 parameters as specified in some function type specification (except for
19883 those which appear as part of a function *definition*). */
19885 static void
19886 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19888 tree link;
19889 tree formal_type = NULL;
19890 tree first_parm_type;
19891 tree arg;
19893 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19895 arg = DECL_ARGUMENTS (function_or_method_type);
19896 function_or_method_type = TREE_TYPE (function_or_method_type);
19898 else
19899 arg = NULL_TREE;
19901 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19903 /* Make our first pass over the list of formal parameter types and output a
19904 DW_TAG_formal_parameter DIE for each one. */
19905 for (link = first_parm_type; link; )
19907 dw_die_ref parm_die;
19909 formal_type = TREE_VALUE (link);
19910 if (formal_type == void_type_node)
19911 break;
19913 /* Output a (nameless) DIE to represent the formal parameter itself. */
19914 if (!POINTER_BOUNDS_TYPE_P (formal_type))
19916 parm_die = gen_formal_parameter_die (formal_type, NULL,
19917 true /* Emit name attribute. */,
19918 context_die);
19919 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19920 && link == first_parm_type)
19922 add_AT_flag (parm_die, DW_AT_artificial, 1);
19923 if (dwarf_version >= 3 || !dwarf_strict)
19924 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19926 else if (arg && DECL_ARTIFICIAL (arg))
19927 add_AT_flag (parm_die, DW_AT_artificial, 1);
19930 link = TREE_CHAIN (link);
19931 if (arg)
19932 arg = DECL_CHAIN (arg);
19935 /* If this function type has an ellipsis, add a
19936 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
19937 if (formal_type != void_type_node)
19938 gen_unspecified_parameters_die (function_or_method_type, context_die);
19940 /* Make our second (and final) pass over the list of formal parameter types
19941 and output DIEs to represent those types (as necessary). */
19942 for (link = TYPE_ARG_TYPES (function_or_method_type);
19943 link && TREE_VALUE (link);
19944 link = TREE_CHAIN (link))
19945 gen_type_die (TREE_VALUE (link), context_die);
19948 /* We want to generate the DIE for TYPE so that we can generate the
19949 die for MEMBER, which has been defined; we will need to refer back
19950 to the member declaration nested within TYPE. If we're trying to
19951 generate minimal debug info for TYPE, processing TYPE won't do the
19952 trick; we need to attach the member declaration by hand. */
19954 static void
19955 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19957 gen_type_die (type, context_die);
19959 /* If we're trying to avoid duplicate debug info, we may not have
19960 emitted the member decl for this function. Emit it now. */
19961 if (TYPE_STUB_DECL (type)
19962 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19963 && ! lookup_decl_die (member))
19965 dw_die_ref type_die;
19966 gcc_assert (!decl_ultimate_origin (member));
19968 push_decl_scope (type);
19969 type_die = lookup_type_die_strip_naming_typedef (type);
19970 if (TREE_CODE (member) == FUNCTION_DECL)
19971 gen_subprogram_die (member, type_die);
19972 else if (TREE_CODE (member) == FIELD_DECL)
19974 /* Ignore the nameless fields that are used to skip bits but handle
19975 C++ anonymous unions and structs. */
19976 if (DECL_NAME (member) != NULL_TREE
19977 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19978 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19980 struct vlr_context vlr_ctx = {
19981 DECL_CONTEXT (member), /* struct_type */
19982 NULL_TREE /* variant_part_offset */
19984 gen_type_die (member_declared_type (member), type_die);
19985 gen_field_die (member, &vlr_ctx, type_die);
19988 else
19989 gen_variable_die (member, NULL_TREE, type_die);
19991 pop_decl_scope ();
19995 /* Forward declare these functions, because they are mutually recursive
19996 with their set_block_* pairing functions. */
19997 static void set_decl_origin_self (tree);
19998 static void set_decl_abstract_flags (tree, vec<tree> &);
20000 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
20001 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
20002 that it points to the node itself, thus indicating that the node is its
20003 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
20004 the given node is NULL, recursively descend the decl/block tree which
20005 it is the root of, and for each other ..._DECL or BLOCK node contained
20006 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
20007 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
20008 values to point to themselves. */
20010 static void
20011 set_block_origin_self (tree stmt)
20013 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
20015 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
20018 tree local_decl;
20020 for (local_decl = BLOCK_VARS (stmt);
20021 local_decl != NULL_TREE;
20022 local_decl = DECL_CHAIN (local_decl))
20023 /* Do not recurse on nested functions since the inlining status
20024 of parent and child can be different as per the DWARF spec. */
20025 if (TREE_CODE (local_decl) != FUNCTION_DECL
20026 && !DECL_EXTERNAL (local_decl))
20027 set_decl_origin_self (local_decl);
20031 tree subblock;
20033 for (subblock = BLOCK_SUBBLOCKS (stmt);
20034 subblock != NULL_TREE;
20035 subblock = BLOCK_CHAIN (subblock))
20036 set_block_origin_self (subblock); /* Recurse. */
20041 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
20042 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
20043 node to so that it points to the node itself, thus indicating that the
20044 node represents its own (abstract) origin. Additionally, if the
20045 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
20046 the decl/block tree of which the given node is the root of, and for
20047 each other ..._DECL or BLOCK node contained therein whose
20048 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
20049 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
20050 point to themselves. */
20052 static void
20053 set_decl_origin_self (tree decl)
20055 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
20057 DECL_ABSTRACT_ORIGIN (decl) = decl;
20058 if (TREE_CODE (decl) == FUNCTION_DECL)
20060 tree arg;
20062 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20063 DECL_ABSTRACT_ORIGIN (arg) = arg;
20064 if (DECL_INITIAL (decl) != NULL_TREE
20065 && DECL_INITIAL (decl) != error_mark_node)
20066 set_block_origin_self (DECL_INITIAL (decl));
20071 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
20072 and if it wasn't 1 before, push it to abstract_vec vector.
20073 For all local decls and all local sub-blocks (recursively) do it
20074 too. */
20076 static void
20077 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
20079 tree local_decl;
20080 tree subblock;
20081 unsigned int i;
20083 if (!BLOCK_ABSTRACT (stmt))
20085 abstract_vec.safe_push (stmt);
20086 BLOCK_ABSTRACT (stmt) = 1;
20089 for (local_decl = BLOCK_VARS (stmt);
20090 local_decl != NULL_TREE;
20091 local_decl = DECL_CHAIN (local_decl))
20092 if (! DECL_EXTERNAL (local_decl))
20093 set_decl_abstract_flags (local_decl, abstract_vec);
20095 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20097 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
20098 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
20099 || TREE_CODE (local_decl) == PARM_DECL)
20100 set_decl_abstract_flags (local_decl, abstract_vec);
20103 for (subblock = BLOCK_SUBBLOCKS (stmt);
20104 subblock != NULL_TREE;
20105 subblock = BLOCK_CHAIN (subblock))
20106 set_block_abstract_flags (subblock, abstract_vec);
20109 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
20110 to 1 and if it wasn't 1 before, push to abstract_vec vector.
20111 In the case where the decl is a FUNCTION_DECL also set the abstract
20112 flags for all of the parameters, local vars, local
20113 blocks and sub-blocks (recursively). */
20115 static void
20116 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
20118 if (!DECL_ABSTRACT_P (decl))
20120 abstract_vec.safe_push (decl);
20121 DECL_ABSTRACT_P (decl) = 1;
20124 if (TREE_CODE (decl) == FUNCTION_DECL)
20126 tree arg;
20128 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20129 if (!DECL_ABSTRACT_P (arg))
20131 abstract_vec.safe_push (arg);
20132 DECL_ABSTRACT_P (arg) = 1;
20134 if (DECL_INITIAL (decl) != NULL_TREE
20135 && DECL_INITIAL (decl) != error_mark_node)
20136 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
20140 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20141 may later generate inlined and/or out-of-line instances of.
20143 FIXME: In the early-dwarf world, this function, and most of the
20144 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
20145 the abstract instance. All we would need to do is annotate
20146 the early DIE with the appropriate DW_AT_inline in late
20147 dwarf (perhaps in gen_inlined_subroutine_die).
20149 However, we can't do this yet, because LTO streaming of DIEs
20150 has not been implemented yet. */
20152 static void
20153 dwarf2out_abstract_function (tree decl)
20155 dw_die_ref old_die;
20156 tree save_fn;
20157 tree context;
20158 hash_table<decl_loc_hasher> *old_decl_loc_table;
20159 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20160 int old_call_site_count, old_tail_call_site_count;
20161 struct call_arg_loc_node *old_call_arg_locations;
20163 /* Make sure we have the actual abstract inline, not a clone. */
20164 decl = DECL_ORIGIN (decl);
20166 old_die = lookup_decl_die (decl);
20167 if (old_die && get_AT (old_die, DW_AT_inline))
20168 /* We've already generated the abstract instance. */
20169 return;
20171 /* We can be called while recursively when seeing block defining inlined subroutine
20172 DIE. Be sure to not clobber the outer location table nor use it or we would
20173 get locations in abstract instantces. */
20174 old_decl_loc_table = decl_loc_table;
20175 decl_loc_table = NULL;
20176 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20177 cached_dw_loc_list_table = NULL;
20178 old_call_arg_locations = call_arg_locations;
20179 call_arg_locations = NULL;
20180 old_call_site_count = call_site_count;
20181 call_site_count = -1;
20182 old_tail_call_site_count = tail_call_site_count;
20183 tail_call_site_count = -1;
20185 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20186 we don't get confused by DECL_ABSTRACT_P. */
20187 if (debug_info_level > DINFO_LEVEL_TERSE)
20189 context = decl_class_context (decl);
20190 if (context)
20191 gen_type_die_for_member
20192 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20195 /* Pretend we've just finished compiling this function. */
20196 save_fn = current_function_decl;
20197 current_function_decl = decl;
20199 auto_vec<tree, 64> abstract_vec;
20200 set_decl_abstract_flags (decl, abstract_vec);
20201 dwarf2out_decl (decl);
20202 unsigned int i;
20203 tree t;
20204 FOR_EACH_VEC_ELT (abstract_vec, i, t)
20205 if (TREE_CODE (t) == BLOCK)
20206 BLOCK_ABSTRACT (t) = 0;
20207 else
20208 DECL_ABSTRACT_P (t) = 0;
20210 current_function_decl = save_fn;
20211 decl_loc_table = old_decl_loc_table;
20212 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20213 call_arg_locations = old_call_arg_locations;
20214 call_site_count = old_call_site_count;
20215 tail_call_site_count = old_tail_call_site_count;
20218 /* Helper function of premark_used_types() which gets called through
20219 htab_traverse.
20221 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20222 marked as unused by prune_unused_types. */
20224 bool
20225 premark_used_types_helper (tree const &type, void *)
20227 dw_die_ref die;
20229 die = lookup_type_die (type);
20230 if (die != NULL)
20231 die->die_perennial_p = 1;
20232 return true;
20235 /* Helper function of premark_types_used_by_global_vars which gets called
20236 through htab_traverse.
20238 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20239 marked as unused by prune_unused_types. The DIE of the type is marked
20240 only if the global variable using the type will actually be emitted. */
20243 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20244 void *)
20246 struct types_used_by_vars_entry *entry;
20247 dw_die_ref die;
20249 entry = (struct types_used_by_vars_entry *) *slot;
20250 gcc_assert (entry->type != NULL
20251 && entry->var_decl != NULL);
20252 die = lookup_type_die (entry->type);
20253 if (die)
20255 /* Ask cgraph if the global variable really is to be emitted.
20256 If yes, then we'll keep the DIE of ENTRY->TYPE. */
20257 varpool_node *node = varpool_node::get (entry->var_decl);
20258 if (node && node->definition)
20260 die->die_perennial_p = 1;
20261 /* Keep the parent DIEs as well. */
20262 while ((die = die->die_parent) && die->die_perennial_p == 0)
20263 die->die_perennial_p = 1;
20266 return 1;
20269 /* Mark all members of used_types_hash as perennial. */
20271 static void
20272 premark_used_types (struct function *fun)
20274 if (fun && fun->used_types_hash)
20275 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20278 /* Mark all members of types_used_by_vars_entry as perennial. */
20280 static void
20281 premark_types_used_by_global_vars (void)
20283 if (types_used_by_vars_hash)
20284 types_used_by_vars_hash
20285 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20288 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20289 for CA_LOC call arg loc node. */
20291 static dw_die_ref
20292 gen_call_site_die (tree decl, dw_die_ref subr_die,
20293 struct call_arg_loc_node *ca_loc)
20295 dw_die_ref stmt_die = NULL, die;
20296 tree block = ca_loc->block;
20298 while (block
20299 && block != DECL_INITIAL (decl)
20300 && TREE_CODE (block) == BLOCK)
20302 stmt_die = BLOCK_DIE (block);
20303 if (stmt_die)
20304 break;
20305 block = BLOCK_SUPERCONTEXT (block);
20307 if (stmt_die == NULL)
20308 stmt_die = subr_die;
20309 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20310 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20311 if (ca_loc->tail_call_p)
20312 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20313 if (ca_loc->symbol_ref)
20315 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20316 if (tdie)
20317 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20318 else
20319 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20321 return die;
20324 /* Generate a DIE to represent a declared function (either file-scope or
20325 block-local). */
20327 static void
20328 gen_subprogram_die (tree decl, dw_die_ref context_die)
20330 tree origin = decl_ultimate_origin (decl);
20331 dw_die_ref subr_die;
20332 dw_die_ref old_die = lookup_decl_die (decl);
20334 /* This function gets called multiple times for different stages of
20335 the debug process. For example, for func() in this code:
20337 namespace S
20339 void func() { ... }
20342 ...we get called 4 times. Twice in early debug and twice in
20343 late debug:
20345 Early debug
20346 -----------
20348 1. Once while generating func() within the namespace. This is
20349 the declaration. The declaration bit below is set, as the
20350 context is the namespace.
20352 A new DIE will be generated with DW_AT_declaration set.
20354 2. Once for func() itself. This is the specification. The
20355 declaration bit below is clear as the context is the CU.
20357 We will use the cached DIE from (1) to create a new DIE with
20358 DW_AT_specification pointing to the declaration in (1).
20360 Late debug via rest_of_handle_final()
20361 -------------------------------------
20363 3. Once generating func() within the namespace. This is also the
20364 declaration, as in (1), but this time we will early exit below
20365 as we have a cached DIE and a declaration needs no additional
20366 annotations (no locations), as the source declaration line
20367 info is enough.
20369 4. Once for func() itself. As in (2), this is the specification,
20370 but this time we will re-use the cached DIE, and just annotate
20371 it with the location information that should now be available.
20373 For something without namespaces, but with abstract instances, we
20374 are also called a multiple times:
20376 class Base
20378 public:
20379 Base (); // constructor declaration (1)
20382 Base::Base () { } // constructor specification (2)
20384 Early debug
20385 -----------
20387 1. Once for the Base() constructor by virtue of it being a
20388 member of the Base class. This is done via
20389 rest_of_type_compilation.
20391 This is a declaration, so a new DIE will be created with
20392 DW_AT_declaration.
20394 2. Once for the Base() constructor definition, but this time
20395 while generating the abstract instance of the base
20396 constructor (__base_ctor) which is being generated via early
20397 debug of reachable functions.
20399 Even though we have a cached version of the declaration (1),
20400 we will create a DW_AT_specification of the declaration DIE
20401 in (1).
20403 3. Once for the __base_ctor itself, but this time, we generate
20404 an DW_AT_abstract_origin version of the DW_AT_specification in
20405 (2).
20407 Late debug via rest_of_handle_final
20408 -----------------------------------
20410 4. One final time for the __base_ctor (which will have a cached
20411 DIE with DW_AT_abstract_origin created in (3). This time,
20412 we will just annotate the location information now
20413 available.
20415 int declaration = (current_function_decl != decl
20416 || class_or_namespace_scope_p (context_die));
20418 premark_used_types (DECL_STRUCT_FUNCTION (decl));
20420 /* Now that the C++ front end lazily declares artificial member fns, we
20421 might need to retrofit the declaration into its class. */
20422 if (!declaration && !origin && !old_die
20423 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20424 && !class_or_namespace_scope_p (context_die)
20425 && debug_info_level > DINFO_LEVEL_TERSE)
20426 old_die = force_decl_die (decl);
20428 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
20429 if (origin != NULL)
20431 gcc_assert (!declaration || local_scope_p (context_die));
20433 /* Fixup die_parent for the abstract instance of a nested
20434 inline function. */
20435 if (old_die && old_die->die_parent == NULL)
20436 add_child_die (context_die, old_die);
20438 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20440 /* If we have a DW_AT_abstract_origin we have a working
20441 cached version. */
20442 subr_die = old_die;
20444 else
20446 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20447 add_abstract_origin_attribute (subr_die, origin);
20448 /* This is where the actual code for a cloned function is.
20449 Let's emit linkage name attribute for it. This helps
20450 debuggers to e.g, set breakpoints into
20451 constructors/destructors when the user asks "break
20452 K::K". */
20453 add_linkage_name (subr_die, decl);
20456 /* A cached copy, possibly from early dwarf generation. Reuse as
20457 much as possible. */
20458 else if (old_die)
20460 /* A declaration that has been previously dumped needs no
20461 additional information. */
20462 if (declaration)
20463 return;
20465 if (!get_AT_flag (old_die, DW_AT_declaration)
20466 /* We can have a normal definition following an inline one in the
20467 case of redefinition of GNU C extern inlines.
20468 It seems reasonable to use AT_specification in this case. */
20469 && !get_AT (old_die, DW_AT_inline))
20471 /* Detect and ignore this case, where we are trying to output
20472 something we have already output. */
20473 if (get_AT (old_die, DW_AT_low_pc)
20474 || get_AT (old_die, DW_AT_ranges))
20475 return;
20477 /* If we have no location information, this must be a
20478 partially generated DIE from early dwarf generation.
20479 Fall through and generate it. */
20482 /* If the definition comes from the same place as the declaration,
20483 maybe use the old DIE. We always want the DIE for this function
20484 that has the *_pc attributes to be under comp_unit_die so the
20485 debugger can find it. We also need to do this for abstract
20486 instances of inlines, since the spec requires the out-of-line copy
20487 to have the same parent. For local class methods, this doesn't
20488 apply; we just use the old DIE. */
20489 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20490 struct dwarf_file_data * file_index = lookup_filename (s.file);
20491 if ((is_cu_die (old_die->die_parent)
20492 /* This condition fixes the inconsistency/ICE with the
20493 following Fortran test (or some derivative thereof) while
20494 building libgfortran:
20496 module some_m
20497 contains
20498 logical function funky (FLAG)
20499 funky = .true.
20500 end function
20501 end module
20503 || (old_die->die_parent
20504 && old_die->die_parent->die_tag == DW_TAG_module)
20505 || context_die == NULL)
20506 && (DECL_ARTIFICIAL (decl)
20507 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20508 && (get_AT_unsigned (old_die, DW_AT_decl_line)
20509 == (unsigned) s.line))))
20511 subr_die = old_die;
20513 /* Clear out the declaration attribute, but leave the
20514 parameters so they can be augmented with location
20515 information later. Unless this was a declaration, in
20516 which case, wipe out the nameless parameters and recreate
20517 them further down. */
20518 if (remove_AT (subr_die, DW_AT_declaration))
20521 remove_AT (subr_die, DW_AT_object_pointer);
20522 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20525 /* Make a specification pointing to the previously built
20526 declaration. */
20527 else
20529 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20530 add_AT_specification (subr_die, old_die);
20531 add_pubname (decl, subr_die);
20532 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20533 add_AT_file (subr_die, DW_AT_decl_file, file_index);
20534 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20535 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20537 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20538 emit the real type on the definition die. */
20539 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20541 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20542 if (die == auto_die || die == decltype_auto_die)
20543 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20544 TYPE_UNQUALIFIED, false, context_die);
20547 /* When we process the method declaration, we haven't seen
20548 the out-of-class defaulted definition yet, so we have to
20549 recheck now. */
20550 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20551 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict)
20552 && !get_AT (subr_die, DW_AT_defaulted))
20553 switch (defaulted)
20555 case 2:
20556 add_AT_unsigned (subr_die, DW_AT_defaulted,
20557 DW_DEFAULTED_out_of_class);
20558 break;
20560 case 1: /* This must have been handled before. */
20561 default:
20562 gcc_unreachable ();
20566 /* Create a fresh DIE for anything else. */
20567 else
20569 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20571 if (TREE_PUBLIC (decl))
20572 add_AT_flag (subr_die, DW_AT_external, 1);
20574 add_name_and_src_coords_attributes (subr_die, decl);
20575 add_pubname (decl, subr_die);
20576 if (debug_info_level > DINFO_LEVEL_TERSE)
20578 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20579 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20580 TYPE_UNQUALIFIED, false, context_die);
20583 add_pure_or_virtual_attribute (subr_die, decl);
20584 if (DECL_ARTIFICIAL (decl))
20585 add_AT_flag (subr_die, DW_AT_artificial, 1);
20587 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20588 add_AT_flag (subr_die, DW_AT_noreturn, 1);
20590 add_accessibility_attribute (subr_die, decl);
20593 /* Unless we have an existing non-declaration DIE, equate the new
20594 DIE. */
20595 if (!old_die || is_declaration_die (old_die))
20596 equate_decl_number_to_die (decl, subr_die);
20598 if (declaration)
20600 if (!old_die || !get_AT (old_die, DW_AT_inline))
20602 add_AT_flag (subr_die, DW_AT_declaration, 1);
20604 /* If this is an explicit function declaration then generate
20605 a DW_AT_explicit attribute. */
20606 if (lang_hooks.decls.function_decl_explicit_p (decl)
20607 && (dwarf_version >= 3 || !dwarf_strict))
20608 add_AT_flag (subr_die, DW_AT_explicit, 1);
20610 /* If this is a C++11 deleted special function member then generate
20611 a DW_AT_deleted attribute. */
20612 if (lang_hooks.decls.function_decl_deleted_p (decl)
20613 && (dwarf_version >= 5 || ! dwarf_strict))
20614 add_AT_flag (subr_die, DW_AT_deleted, 1);
20616 /* If this is a C++11 defaulted special function member then
20617 generate a DW_AT_GNU_defaulted attribute. */
20618 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20619 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict))
20620 switch (defaulted)
20622 case 1:
20623 add_AT_unsigned (subr_die, DW_AT_defaulted,
20624 DW_DEFAULTED_in_class);
20625 break;
20627 /* It is likely that this will never hit, since we
20628 don't have the out-of-class definition yet when we
20629 process the class definition and the method
20630 declaration. We recheck elsewhere, but leave it
20631 here just in case. */
20632 case 2:
20633 add_AT_unsigned (subr_die, DW_AT_defaulted,
20634 DW_DEFAULTED_out_of_class);
20635 break;
20637 default:
20638 gcc_unreachable ();
20642 /* Tag abstract instances with DW_AT_inline. */
20643 else if (DECL_ABSTRACT_P (decl))
20645 if (DECL_DECLARED_INLINE_P (decl))
20647 if (cgraph_function_possibly_inlined_p (decl))
20648 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20649 else
20650 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20652 else
20654 if (cgraph_function_possibly_inlined_p (decl))
20655 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20656 else
20657 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20660 if (DECL_DECLARED_INLINE_P (decl)
20661 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20662 add_AT_flag (subr_die, DW_AT_artificial, 1);
20664 /* For non DECL_EXTERNALs, if range information is available, fill
20665 the DIE with it. */
20666 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20668 HOST_WIDE_INT cfa_fb_offset;
20670 struct function *fun = DECL_STRUCT_FUNCTION (decl);
20672 if (!flag_reorder_blocks_and_partition)
20674 dw_fde_ref fde = fun->fde;
20675 if (fde->dw_fde_begin)
20677 /* We have already generated the labels. */
20678 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20679 fde->dw_fde_end, false);
20681 else
20683 /* Create start/end labels and add the range. */
20684 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20685 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20686 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20687 current_function_funcdef_no);
20688 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20689 current_function_funcdef_no);
20690 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20691 false);
20694 #if VMS_DEBUGGING_INFO
20695 /* HP OpenVMS Industry Standard 64: DWARF Extensions
20696 Section 2.3 Prologue and Epilogue Attributes:
20697 When a breakpoint is set on entry to a function, it is generally
20698 desirable for execution to be suspended, not on the very first
20699 instruction of the function, but rather at a point after the
20700 function's frame has been set up, after any language defined local
20701 declaration processing has been completed, and before execution of
20702 the first statement of the function begins. Debuggers generally
20703 cannot properly determine where this point is. Similarly for a
20704 breakpoint set on exit from a function. The prologue and epilogue
20705 attributes allow a compiler to communicate the location(s) to use. */
20708 if (fde->dw_fde_vms_end_prologue)
20709 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20710 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20712 if (fde->dw_fde_vms_begin_epilogue)
20713 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20714 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20716 #endif
20719 else
20721 /* Generate pubnames entries for the split function code ranges. */
20722 dw_fde_ref fde = fun->fde;
20724 if (fde->dw_fde_second_begin)
20726 if (dwarf_version >= 3 || !dwarf_strict)
20728 /* We should use ranges for non-contiguous code section
20729 addresses. Use the actual code range for the initial
20730 section, since the HOT/COLD labels might precede an
20731 alignment offset. */
20732 bool range_list_added = false;
20733 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20734 fde->dw_fde_end, &range_list_added,
20735 false);
20736 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20737 fde->dw_fde_second_end,
20738 &range_list_added, false);
20739 if (range_list_added)
20740 add_ranges (NULL);
20742 else
20744 /* There is no real support in DW2 for this .. so we make
20745 a work-around. First, emit the pub name for the segment
20746 containing the function label. Then make and emit a
20747 simplified subprogram DIE for the second segment with the
20748 name pre-fixed by __hot/cold_sect_of_. We use the same
20749 linkage name for the second die so that gdb will find both
20750 sections when given "b foo". */
20751 const char *name = NULL;
20752 tree decl_name = DECL_NAME (decl);
20753 dw_die_ref seg_die;
20755 /* Do the 'primary' section. */
20756 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20757 fde->dw_fde_end, false);
20759 /* Build a minimal DIE for the secondary section. */
20760 seg_die = new_die (DW_TAG_subprogram,
20761 subr_die->die_parent, decl);
20763 if (TREE_PUBLIC (decl))
20764 add_AT_flag (seg_die, DW_AT_external, 1);
20766 if (decl_name != NULL
20767 && IDENTIFIER_POINTER (decl_name) != NULL)
20769 name = dwarf2_name (decl, 1);
20770 if (! DECL_ARTIFICIAL (decl))
20771 add_src_coords_attributes (seg_die, decl);
20773 add_linkage_name (seg_die, decl);
20775 gcc_assert (name != NULL);
20776 add_pure_or_virtual_attribute (seg_die, decl);
20777 if (DECL_ARTIFICIAL (decl))
20778 add_AT_flag (seg_die, DW_AT_artificial, 1);
20780 name = concat ("__second_sect_of_", name, NULL);
20781 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20782 fde->dw_fde_second_end, false);
20783 add_name_attribute (seg_die, name);
20784 if (want_pubnames ())
20785 add_pubname_string (name, seg_die);
20788 else
20789 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20790 false);
20793 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20795 /* We define the "frame base" as the function's CFA. This is more
20796 convenient for several reasons: (1) It's stable across the prologue
20797 and epilogue, which makes it better than just a frame pointer,
20798 (2) With dwarf3, there exists a one-byte encoding that allows us
20799 to reference the .debug_frame data by proxy, but failing that,
20800 (3) We can at least reuse the code inspection and interpretation
20801 code that determines the CFA position at various points in the
20802 function. */
20803 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20805 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20806 add_AT_loc (subr_die, DW_AT_frame_base, op);
20808 else
20810 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20811 if (list->dw_loc_next)
20812 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20813 else
20814 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20817 /* Compute a displacement from the "steady-state frame pointer" to
20818 the CFA. The former is what all stack slots and argument slots
20819 will reference in the rtl; the latter is what we've told the
20820 debugger about. We'll need to adjust all frame_base references
20821 by this displacement. */
20822 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20824 if (fun->static_chain_decl)
20826 /* DWARF requires here a location expression that computes the
20827 address of the enclosing subprogram's frame base. The machinery
20828 in tree-nested.c is supposed to store this specific address in the
20829 last field of the FRAME record. */
20830 const tree frame_type
20831 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20832 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20834 tree fb_expr
20835 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20836 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20837 fb_expr, fb_decl, NULL_TREE);
20839 add_AT_location_description (subr_die, DW_AT_static_link,
20840 loc_list_from_tree (fb_expr, 0, NULL));
20844 /* Generate child dies for template paramaters. */
20845 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20846 gen_generic_params_dies (decl);
20848 /* Now output descriptions of the arguments for this function. This gets
20849 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20850 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20851 `...' at the end of the formal parameter list. In order to find out if
20852 there was a trailing ellipsis or not, we must instead look at the type
20853 associated with the FUNCTION_DECL. This will be a node of type
20854 FUNCTION_TYPE. If the chain of type nodes hanging off of this
20855 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20856 an ellipsis at the end. */
20858 /* In the case where we are describing a mere function declaration, all we
20859 need to do here (and all we *can* do here) is to describe the *types* of
20860 its formal parameters. */
20861 if (debug_info_level <= DINFO_LEVEL_TERSE)
20863 else if (declaration)
20864 gen_formal_types_die (decl, subr_die);
20865 else
20867 /* Generate DIEs to represent all known formal parameters. */
20868 tree parm = DECL_ARGUMENTS (decl);
20869 tree generic_decl = early_dwarf
20870 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20871 tree generic_decl_parm = generic_decl
20872 ? DECL_ARGUMENTS (generic_decl)
20873 : NULL;
20874 auto_vec<dw_die_ref> string_types_vec;
20875 if (string_types == NULL)
20876 string_types = &string_types_vec;
20878 /* Now we want to walk the list of parameters of the function and
20879 emit their relevant DIEs.
20881 We consider the case of DECL being an instance of a generic function
20882 as well as it being a normal function.
20884 If DECL is an instance of a generic function we walk the
20885 parameters of the generic function declaration _and_ the parameters of
20886 DECL itself. This is useful because we want to emit specific DIEs for
20887 function parameter packs and those are declared as part of the
20888 generic function declaration. In that particular case,
20889 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20890 That DIE has children DIEs representing the set of arguments
20891 of the pack. Note that the set of pack arguments can be empty.
20892 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20893 children DIE.
20895 Otherwise, we just consider the parameters of DECL. */
20896 while (generic_decl_parm || parm)
20898 if (generic_decl_parm
20899 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20900 gen_formal_parameter_pack_die (generic_decl_parm,
20901 parm, subr_die,
20902 &parm);
20903 else if (parm && !POINTER_BOUNDS_P (parm))
20905 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20907 if (parm == DECL_ARGUMENTS (decl)
20908 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20909 && parm_die
20910 && (dwarf_version >= 3 || !dwarf_strict))
20911 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20913 parm = DECL_CHAIN (parm);
20915 else if (parm)
20916 parm = DECL_CHAIN (parm);
20918 if (generic_decl_parm)
20919 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20922 /* Decide whether we need an unspecified_parameters DIE at the end.
20923 There are 2 more cases to do this for: 1) the ansi ... declaration -
20924 this is detectable when the end of the arg list is not a
20925 void_type_node 2) an unprototyped function declaration (not a
20926 definition). This just means that we have no info about the
20927 parameters at all. */
20928 if (early_dwarf)
20930 if (prototype_p (TREE_TYPE (decl)))
20932 /* This is the prototyped case, check for.... */
20933 if (stdarg_p (TREE_TYPE (decl)))
20934 gen_unspecified_parameters_die (decl, subr_die);
20936 else if (DECL_INITIAL (decl) == NULL_TREE)
20937 gen_unspecified_parameters_die (decl, subr_die);
20940 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
20941 have DIEs. */
20942 if (string_types == &string_types_vec)
20944 adjust_string_types ();
20945 string_types = NULL;
20949 if (subr_die != old_die)
20950 /* Add the calling convention attribute if requested. */
20951 add_calling_convention_attribute (subr_die, decl);
20953 /* Output Dwarf info for all of the stuff within the body of the function
20954 (if it has one - it may be just a declaration).
20956 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20957 a function. This BLOCK actually represents the outermost binding contour
20958 for the function, i.e. the contour in which the function's formal
20959 parameters and labels get declared. Curiously, it appears that the front
20960 end doesn't actually put the PARM_DECL nodes for the current function onto
20961 the BLOCK_VARS list for this outer scope, but are strung off of the
20962 DECL_ARGUMENTS list for the function instead.
20964 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20965 the LABEL_DECL nodes for the function however, and we output DWARF info
20966 for those in decls_for_scope. Just within the `outer_scope' there will be
20967 a BLOCK node representing the function's outermost pair of curly braces,
20968 and any blocks used for the base and member initializers of a C++
20969 constructor function. */
20970 tree outer_scope = DECL_INITIAL (decl);
20971 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
20973 int call_site_note_count = 0;
20974 int tail_call_site_note_count = 0;
20976 /* Emit a DW_TAG_variable DIE for a named return value. */
20977 if (DECL_NAME (DECL_RESULT (decl)))
20978 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
20980 /* The first time through decls_for_scope we will generate the
20981 DIEs for the locals. The second time, we fill in the
20982 location info. */
20983 decls_for_scope (outer_scope, subr_die);
20985 if (call_arg_locations && !dwarf_strict)
20987 struct call_arg_loc_node *ca_loc;
20988 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20990 dw_die_ref die = NULL;
20991 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20992 rtx arg, next_arg;
20994 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
20995 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
20996 : NULL_RTX);
20997 arg; arg = next_arg)
20999 dw_loc_descr_ref reg, val;
21000 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
21001 dw_die_ref cdie, tdie = NULL;
21003 next_arg = XEXP (arg, 1);
21004 if (REG_P (XEXP (XEXP (arg, 0), 0))
21005 && next_arg
21006 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
21007 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
21008 && REGNO (XEXP (XEXP (arg, 0), 0))
21009 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
21010 next_arg = XEXP (next_arg, 1);
21011 if (mode == VOIDmode)
21013 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
21014 if (mode == VOIDmode)
21015 mode = GET_MODE (XEXP (arg, 0));
21017 if (mode == VOIDmode || mode == BLKmode)
21018 continue;
21019 /* Get dynamic information about call target only if we
21020 have no static information: we cannot generate both
21021 DW_AT_abstract_origin and DW_AT_GNU_call_site_target
21022 attributes. */
21023 if (ca_loc->symbol_ref == NULL_RTX)
21025 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
21027 tloc = XEXP (XEXP (arg, 0), 1);
21028 continue;
21030 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
21031 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
21033 tlocc = XEXP (XEXP (arg, 0), 1);
21034 continue;
21037 reg = NULL;
21038 if (REG_P (XEXP (XEXP (arg, 0), 0)))
21039 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
21040 VAR_INIT_STATUS_INITIALIZED);
21041 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
21043 rtx mem = XEXP (XEXP (arg, 0), 0);
21044 reg = mem_loc_descriptor (XEXP (mem, 0),
21045 get_address_mode (mem),
21046 GET_MODE (mem),
21047 VAR_INIT_STATUS_INITIALIZED);
21049 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
21050 == DEBUG_PARAMETER_REF)
21052 tree tdecl
21053 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
21054 tdie = lookup_decl_die (tdecl);
21055 if (tdie == NULL)
21056 continue;
21058 else
21059 continue;
21060 if (reg == NULL
21061 && GET_CODE (XEXP (XEXP (arg, 0), 0))
21062 != DEBUG_PARAMETER_REF)
21063 continue;
21064 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
21065 VOIDmode,
21066 VAR_INIT_STATUS_INITIALIZED);
21067 if (val == NULL)
21068 continue;
21069 if (die == NULL)
21070 die = gen_call_site_die (decl, subr_die, ca_loc);
21071 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
21072 NULL_TREE);
21073 if (reg != NULL)
21074 add_AT_loc (cdie, DW_AT_location, reg);
21075 else if (tdie != NULL)
21076 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
21077 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
21078 if (next_arg != XEXP (arg, 1))
21080 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
21081 if (mode == VOIDmode)
21082 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
21083 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
21084 0), 1),
21085 mode, VOIDmode,
21086 VAR_INIT_STATUS_INITIALIZED);
21087 if (val != NULL)
21088 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
21091 if (die == NULL
21092 && (ca_loc->symbol_ref || tloc))
21093 die = gen_call_site_die (decl, subr_die, ca_loc);
21094 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
21096 dw_loc_descr_ref tval = NULL;
21098 if (tloc != NULL_RTX)
21099 tval = mem_loc_descriptor (tloc,
21100 GET_MODE (tloc) == VOIDmode
21101 ? Pmode : GET_MODE (tloc),
21102 VOIDmode,
21103 VAR_INIT_STATUS_INITIALIZED);
21104 if (tval)
21105 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
21106 else if (tlocc != NULL_RTX)
21108 tval = mem_loc_descriptor (tlocc,
21109 GET_MODE (tlocc) == VOIDmode
21110 ? Pmode : GET_MODE (tlocc),
21111 VOIDmode,
21112 VAR_INIT_STATUS_INITIALIZED);
21113 if (tval)
21114 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
21115 tval);
21118 if (die != NULL)
21120 call_site_note_count++;
21121 if (ca_loc->tail_call_p)
21122 tail_call_site_note_count++;
21126 call_arg_locations = NULL;
21127 call_arg_loc_last = NULL;
21128 if (tail_call_site_count >= 0
21129 && tail_call_site_count == tail_call_site_note_count
21130 && !dwarf_strict)
21132 if (call_site_count >= 0
21133 && call_site_count == call_site_note_count)
21134 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
21135 else
21136 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
21138 call_site_count = -1;
21139 tail_call_site_count = -1;
21143 /* Returns a hash value for X (which really is a die_struct). */
21145 hashval_t
21146 block_die_hasher::hash (die_struct *d)
21148 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
21151 /* Return nonzero if decl_id and die_parent of die_struct X is the same
21152 as decl_id and die_parent of die_struct Y. */
21154 bool
21155 block_die_hasher::equal (die_struct *x, die_struct *y)
21157 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
21160 /* Return TRUE if DECL, which may have been previously generated as
21161 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
21162 true if decl (or its origin) is either an extern declaration or a
21163 class/namespace scoped declaration.
21165 The declare_in_namespace support causes us to get two DIEs for one
21166 variable, both of which are declarations. We want to avoid
21167 considering one to be a specification, so we must test for
21168 DECLARATION and DW_AT_declaration. */
21169 static inline bool
21170 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
21172 return (old_die && TREE_STATIC (decl) && !declaration
21173 && get_AT_flag (old_die, DW_AT_declaration) == 1);
21176 /* Return true if DECL is a local static. */
21178 static inline bool
21179 local_function_static (tree decl)
21181 gcc_assert (TREE_CODE (decl) == VAR_DECL);
21182 return TREE_STATIC (decl)
21183 && DECL_CONTEXT (decl)
21184 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
21187 /* Generate a DIE to represent a declared data object.
21188 Either DECL or ORIGIN must be non-null. */
21190 static void
21191 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
21193 HOST_WIDE_INT off = 0;
21194 tree com_decl;
21195 tree decl_or_origin = decl ? decl : origin;
21196 tree ultimate_origin;
21197 dw_die_ref var_die;
21198 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
21199 dw_die_ref origin_die = NULL;
21200 bool declaration = (DECL_EXTERNAL (decl_or_origin)
21201 || class_or_namespace_scope_p (context_die));
21202 bool specialization_p = false;
21204 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21205 if (decl || ultimate_origin)
21206 origin = ultimate_origin;
21207 com_decl = fortran_common (decl_or_origin, &off);
21209 /* Symbol in common gets emitted as a child of the common block, in the form
21210 of a data member. */
21211 if (com_decl)
21213 dw_die_ref com_die;
21214 dw_loc_list_ref loc;
21215 die_node com_die_arg;
21217 var_die = lookup_decl_die (decl_or_origin);
21218 if (var_die)
21220 if (get_AT (var_die, DW_AT_location) == NULL)
21222 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21223 if (loc)
21225 if (off)
21227 /* Optimize the common case. */
21228 if (single_element_loc_list_p (loc)
21229 && loc->expr->dw_loc_opc == DW_OP_addr
21230 && loc->expr->dw_loc_next == NULL
21231 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21232 == SYMBOL_REF)
21234 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21235 loc->expr->dw_loc_oprnd1.v.val_addr
21236 = plus_constant (GET_MODE (x), x , off);
21238 else
21239 loc_list_plus_const (loc, off);
21241 add_AT_location_description (var_die, DW_AT_location, loc);
21242 remove_AT (var_die, DW_AT_declaration);
21245 return;
21248 if (common_block_die_table == NULL)
21249 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21251 com_die_arg.decl_id = DECL_UID (com_decl);
21252 com_die_arg.die_parent = context_die;
21253 com_die = common_block_die_table->find (&com_die_arg);
21254 loc = loc_list_from_tree (com_decl, 2, NULL);
21255 if (com_die == NULL)
21257 const char *cnam
21258 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21259 die_node **slot;
21261 com_die = new_die (DW_TAG_common_block, context_die, decl);
21262 add_name_and_src_coords_attributes (com_die, com_decl);
21263 if (loc)
21265 add_AT_location_description (com_die, DW_AT_location, loc);
21266 /* Avoid sharing the same loc descriptor between
21267 DW_TAG_common_block and DW_TAG_variable. */
21268 loc = loc_list_from_tree (com_decl, 2, NULL);
21270 else if (DECL_EXTERNAL (decl_or_origin))
21271 add_AT_flag (com_die, DW_AT_declaration, 1);
21272 if (want_pubnames ())
21273 add_pubname_string (cnam, com_die); /* ??? needed? */
21274 com_die->decl_id = DECL_UID (com_decl);
21275 slot = common_block_die_table->find_slot (com_die, INSERT);
21276 *slot = com_die;
21278 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21280 add_AT_location_description (com_die, DW_AT_location, loc);
21281 loc = loc_list_from_tree (com_decl, 2, NULL);
21282 remove_AT (com_die, DW_AT_declaration);
21284 var_die = new_die (DW_TAG_variable, com_die, decl);
21285 add_name_and_src_coords_attributes (var_die, decl_or_origin);
21286 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21287 decl_quals (decl_or_origin), false,
21288 context_die);
21289 add_AT_flag (var_die, DW_AT_external, 1);
21290 if (loc)
21292 if (off)
21294 /* Optimize the common case. */
21295 if (single_element_loc_list_p (loc)
21296 && loc->expr->dw_loc_opc == DW_OP_addr
21297 && loc->expr->dw_loc_next == NULL
21298 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21300 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21301 loc->expr->dw_loc_oprnd1.v.val_addr
21302 = plus_constant (GET_MODE (x), x, off);
21304 else
21305 loc_list_plus_const (loc, off);
21307 add_AT_location_description (var_die, DW_AT_location, loc);
21309 else if (DECL_EXTERNAL (decl_or_origin))
21310 add_AT_flag (var_die, DW_AT_declaration, 1);
21311 if (decl)
21312 equate_decl_number_to_die (decl, var_die);
21313 return;
21316 if (old_die)
21318 if (declaration)
21320 /* A declaration that has been previously dumped, needs no
21321 further annotations, since it doesn't need location on
21322 the second pass. */
21323 return;
21325 else if (decl_will_get_specification_p (old_die, decl, declaration)
21326 && !get_AT (old_die, DW_AT_specification))
21328 /* Fall-thru so we can make a new variable die along with a
21329 DW_AT_specification. */
21331 else if (origin && old_die->die_parent != context_die)
21333 /* If we will be creating an inlined instance, we need a
21334 new DIE that will get annotated with
21335 DW_AT_abstract_origin. Clear things so we can get a
21336 new DIE. */
21337 gcc_assert (!DECL_ABSTRACT_P (decl));
21338 old_die = NULL;
21340 else
21342 /* If a DIE was dumped early, it still needs location info.
21343 Skip to where we fill the location bits. */
21344 var_die = old_die;
21345 goto gen_variable_die_location;
21349 /* For static data members, the declaration in the class is supposed
21350 to have DW_TAG_member tag; the specification should still be
21351 DW_TAG_variable referencing the DW_TAG_member DIE. */
21352 if (declaration && class_scope_p (context_die))
21353 var_die = new_die (DW_TAG_member, context_die, decl);
21354 else
21355 var_die = new_die (DW_TAG_variable, context_die, decl);
21357 if (origin != NULL)
21358 origin_die = add_abstract_origin_attribute (var_die, origin);
21360 /* Loop unrolling can create multiple blocks that refer to the same
21361 static variable, so we must test for the DW_AT_declaration flag.
21363 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21364 copy decls and set the DECL_ABSTRACT_P flag on them instead of
21365 sharing them.
21367 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
21368 else if (decl_will_get_specification_p (old_die, decl, declaration))
21370 /* This is a definition of a C++ class level static. */
21371 add_AT_specification (var_die, old_die);
21372 specialization_p = true;
21373 if (DECL_NAME (decl))
21375 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21376 struct dwarf_file_data * file_index = lookup_filename (s.file);
21378 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21379 add_AT_file (var_die, DW_AT_decl_file, file_index);
21381 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21382 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21384 if (old_die->die_tag == DW_TAG_member)
21385 add_linkage_name (var_die, decl);
21388 else
21389 add_name_and_src_coords_attributes (var_die, decl);
21391 if ((origin == NULL && !specialization_p)
21392 || (origin != NULL
21393 && !DECL_ABSTRACT_P (decl_or_origin)
21394 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21395 decl_function_context
21396 (decl_or_origin))))
21398 tree type = TREE_TYPE (decl_or_origin);
21400 if (decl_by_reference_p (decl_or_origin))
21401 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21402 context_die);
21403 else
21404 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21405 context_die);
21408 if (origin == NULL && !specialization_p)
21410 if (TREE_PUBLIC (decl))
21411 add_AT_flag (var_die, DW_AT_external, 1);
21413 if (DECL_ARTIFICIAL (decl))
21414 add_AT_flag (var_die, DW_AT_artificial, 1);
21416 add_accessibility_attribute (var_die, decl);
21419 if (declaration)
21420 add_AT_flag (var_die, DW_AT_declaration, 1);
21422 if (decl && (DECL_ABSTRACT_P (decl)
21423 || !old_die || is_declaration_die (old_die)))
21424 equate_decl_number_to_die (decl, var_die);
21426 gen_variable_die_location:
21427 if (! declaration
21428 && (! DECL_ABSTRACT_P (decl_or_origin)
21429 /* Local static vars are shared between all clones/inlines,
21430 so emit DW_AT_location on the abstract DIE if DECL_RTL is
21431 already set. */
21432 || (TREE_CODE (decl_or_origin) == VAR_DECL
21433 && TREE_STATIC (decl_or_origin)
21434 && DECL_RTL_SET_P (decl_or_origin)))
21435 /* When abstract origin already has DW_AT_location attribute, no need
21436 to add it again. */
21437 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21439 if (early_dwarf)
21440 add_pubname (decl_or_origin, var_die);
21441 else
21442 add_location_or_const_value_attribute (var_die, decl_or_origin,
21443 decl == NULL);
21445 else
21446 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21449 /* Generate a DIE to represent a named constant. */
21451 static void
21452 gen_const_die (tree decl, dw_die_ref context_die)
21454 dw_die_ref const_die;
21455 tree type = TREE_TYPE (decl);
21457 const_die = lookup_decl_die (decl);
21458 if (const_die)
21459 return;
21461 const_die = new_die (DW_TAG_constant, context_die, decl);
21462 equate_decl_number_to_die (decl, const_die);
21463 add_name_and_src_coords_attributes (const_die, decl);
21464 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21465 if (TREE_PUBLIC (decl))
21466 add_AT_flag (const_die, DW_AT_external, 1);
21467 if (DECL_ARTIFICIAL (decl))
21468 add_AT_flag (const_die, DW_AT_artificial, 1);
21469 tree_add_const_value_attribute_for_decl (const_die, decl);
21472 /* Generate a DIE to represent a label identifier. */
21474 static void
21475 gen_label_die (tree decl, dw_die_ref context_die)
21477 tree origin = decl_ultimate_origin (decl);
21478 dw_die_ref lbl_die = lookup_decl_die (decl);
21479 rtx insn;
21480 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21482 if (!lbl_die)
21484 lbl_die = new_die (DW_TAG_label, context_die, decl);
21485 equate_decl_number_to_die (decl, lbl_die);
21487 if (origin != NULL)
21488 add_abstract_origin_attribute (lbl_die, origin);
21489 else
21490 add_name_and_src_coords_attributes (lbl_die, decl);
21493 if (DECL_ABSTRACT_P (decl))
21494 equate_decl_number_to_die (decl, lbl_die);
21495 else
21497 insn = DECL_RTL_IF_SET (decl);
21499 /* Deleted labels are programmer specified labels which have been
21500 eliminated because of various optimizations. We still emit them
21501 here so that it is possible to put breakpoints on them. */
21502 if (insn
21503 && (LABEL_P (insn)
21504 || ((NOTE_P (insn)
21505 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21507 /* When optimization is enabled (via -O) some parts of the compiler
21508 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21509 represent source-level labels which were explicitly declared by
21510 the user. This really shouldn't be happening though, so catch
21511 it if it ever does happen. */
21512 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21514 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21515 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21517 else if (insn
21518 && NOTE_P (insn)
21519 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21520 && CODE_LABEL_NUMBER (insn) != -1)
21522 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21523 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21528 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
21529 attributes to the DIE for a block STMT, to describe where the inlined
21530 function was called from. This is similar to add_src_coords_attributes. */
21532 static inline void
21533 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21535 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21537 if (dwarf_version >= 3 || !dwarf_strict)
21539 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21540 add_AT_unsigned (die, DW_AT_call_line, s.line);
21545 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21546 Add low_pc and high_pc attributes to the DIE for a block STMT. */
21548 static inline void
21549 add_high_low_attributes (tree stmt, dw_die_ref die)
21551 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21553 if (BLOCK_FRAGMENT_CHAIN (stmt)
21554 && (dwarf_version >= 3 || !dwarf_strict))
21556 tree chain, superblock = NULL_TREE;
21557 dw_die_ref pdie;
21558 dw_attr_node *attr = NULL;
21560 if (inlined_function_outer_scope_p (stmt))
21562 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21563 BLOCK_NUMBER (stmt));
21564 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21567 /* Optimize duplicate .debug_ranges lists or even tails of
21568 lists. If this BLOCK has same ranges as its supercontext,
21569 lookup DW_AT_ranges attribute in the supercontext (and
21570 recursively so), verify that the ranges_table contains the
21571 right values and use it instead of adding a new .debug_range. */
21572 for (chain = stmt, pdie = die;
21573 BLOCK_SAME_RANGE (chain);
21574 chain = BLOCK_SUPERCONTEXT (chain))
21576 dw_attr_node *new_attr;
21578 pdie = pdie->die_parent;
21579 if (pdie == NULL)
21580 break;
21581 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21582 break;
21583 new_attr = get_AT (pdie, DW_AT_ranges);
21584 if (new_attr == NULL
21585 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21586 break;
21587 attr = new_attr;
21588 superblock = BLOCK_SUPERCONTEXT (chain);
21590 if (attr != NULL
21591 && (ranges_table[attr->dw_attr_val.v.val_offset
21592 / 2 / DWARF2_ADDR_SIZE].num
21593 == BLOCK_NUMBER (superblock))
21594 && BLOCK_FRAGMENT_CHAIN (superblock))
21596 unsigned long off = attr->dw_attr_val.v.val_offset
21597 / 2 / DWARF2_ADDR_SIZE;
21598 unsigned long supercnt = 0, thiscnt = 0;
21599 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21600 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21602 ++supercnt;
21603 gcc_checking_assert (ranges_table[off + supercnt].num
21604 == BLOCK_NUMBER (chain));
21606 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21607 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21608 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21609 ++thiscnt;
21610 gcc_assert (supercnt >= thiscnt);
21611 add_AT_range_list (die, DW_AT_ranges,
21612 ((off + supercnt - thiscnt)
21613 * 2 * DWARF2_ADDR_SIZE),
21614 false);
21615 return;
21618 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21620 chain = BLOCK_FRAGMENT_CHAIN (stmt);
21623 add_ranges (chain);
21624 chain = BLOCK_FRAGMENT_CHAIN (chain);
21626 while (chain);
21627 add_ranges (NULL);
21629 else
21631 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21632 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21633 BLOCK_NUMBER (stmt));
21634 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21635 BLOCK_NUMBER (stmt));
21636 add_AT_low_high_pc (die, label, label_high, false);
21640 /* Generate a DIE for a lexical block. */
21642 static void
21643 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21645 dw_die_ref old_die = BLOCK_DIE (stmt);
21646 dw_die_ref stmt_die = NULL;
21647 if (!old_die)
21649 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21650 BLOCK_DIE (stmt) = stmt_die;
21653 if (BLOCK_ABSTRACT (stmt))
21655 if (old_die)
21657 /* This must have been generated early and it won't even
21658 need location information since it's a DW_AT_inline
21659 function. */
21660 if (flag_checking)
21661 for (dw_die_ref c = context_die; c; c = c->die_parent)
21662 if (c->die_tag == DW_TAG_inlined_subroutine
21663 || c->die_tag == DW_TAG_subprogram)
21665 gcc_assert (get_AT (c, DW_AT_inline));
21666 break;
21668 return;
21671 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21673 /* If this is an inlined instance, create a new lexical die for
21674 anything below to attach DW_AT_abstract_origin to. */
21675 if (old_die)
21677 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21678 BLOCK_DIE (stmt) = stmt_die;
21679 old_die = NULL;
21682 tree origin = block_ultimate_origin (stmt);
21683 if (origin != NULL_TREE && origin != stmt)
21684 add_abstract_origin_attribute (stmt_die, origin);
21687 if (old_die)
21688 stmt_die = old_die;
21690 /* A non abstract block whose blocks have already been reordered
21691 should have the instruction range for this block. If so, set the
21692 high/low attributes. */
21693 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21695 gcc_assert (stmt_die);
21696 add_high_low_attributes (stmt, stmt_die);
21699 decls_for_scope (stmt, stmt_die);
21702 /* Generate a DIE for an inlined subprogram. */
21704 static void
21705 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21707 tree decl;
21709 /* The instance of function that is effectively being inlined shall not
21710 be abstract. */
21711 gcc_assert (! BLOCK_ABSTRACT (stmt));
21713 decl = block_ultimate_origin (stmt);
21715 /* Make sure any inlined functions are known to be inlineable. */
21716 gcc_checking_assert (DECL_ABSTRACT_P (decl)
21717 || cgraph_function_possibly_inlined_p (decl));
21719 /* Emit info for the abstract instance first, if we haven't yet. We
21720 must emit this even if the block is abstract, otherwise when we
21721 emit the block below (or elsewhere), we may end up trying to emit
21722 a die whose origin die hasn't been emitted, and crashing. */
21723 dwarf2out_abstract_function (decl);
21725 if (! BLOCK_ABSTRACT (stmt))
21727 dw_die_ref subr_die
21728 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21730 if (call_arg_locations)
21731 BLOCK_DIE (stmt) = subr_die;
21732 add_abstract_origin_attribute (subr_die, decl);
21733 if (TREE_ASM_WRITTEN (stmt))
21734 add_high_low_attributes (stmt, subr_die);
21735 add_call_src_coords_attributes (stmt, subr_die);
21737 decls_for_scope (stmt, subr_die);
21741 /* Generate a DIE for a field in a record, or structure. CTX is required: see
21742 the comment for VLR_CONTEXT. */
21744 static void
21745 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21747 dw_die_ref decl_die;
21749 if (TREE_TYPE (decl) == error_mark_node)
21750 return;
21752 decl_die = new_die (DW_TAG_member, context_die, decl);
21753 add_name_and_src_coords_attributes (decl_die, decl);
21754 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21755 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21756 context_die);
21758 if (DECL_BIT_FIELD_TYPE (decl))
21760 add_byte_size_attribute (decl_die, decl);
21761 add_bit_size_attribute (decl_die, decl);
21762 add_bit_offset_attribute (decl_die, decl, ctx);
21765 /* If we have a variant part offset, then we are supposed to process a member
21766 of a QUAL_UNION_TYPE, which is how we represent variant parts in
21767 trees. */
21768 gcc_assert (ctx->variant_part_offset == NULL_TREE
21769 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21770 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21771 add_data_member_location_attribute (decl_die, decl, ctx);
21773 if (DECL_ARTIFICIAL (decl))
21774 add_AT_flag (decl_die, DW_AT_artificial, 1);
21776 add_accessibility_attribute (decl_die, decl);
21778 /* Equate decl number to die, so that we can look up this decl later on. */
21779 equate_decl_number_to_die (decl, decl_die);
21782 #if 0
21783 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21784 Use modified_type_die instead.
21785 We keep this code here just in case these types of DIEs may be needed to
21786 represent certain things in other languages (e.g. Pascal) someday. */
21788 static void
21789 gen_pointer_type_die (tree type, dw_die_ref context_die)
21791 dw_die_ref ptr_die
21792 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21794 equate_type_number_to_die (type, ptr_die);
21795 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21796 context_die);
21797 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21800 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21801 Use modified_type_die instead.
21802 We keep this code here just in case these types of DIEs may be needed to
21803 represent certain things in other languages (e.g. Pascal) someday. */
21805 static void
21806 gen_reference_type_die (tree type, dw_die_ref context_die)
21808 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21810 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21811 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21812 else
21813 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21815 equate_type_number_to_die (type, ref_die);
21816 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21817 context_die);
21818 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21820 #endif
21822 /* Generate a DIE for a pointer to a member type. */
21824 static void
21825 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21827 dw_die_ref ptr_die
21828 = new_die (DW_TAG_ptr_to_member_type,
21829 scope_die_for (type, context_die), type);
21831 equate_type_number_to_die (type, ptr_die);
21832 add_AT_die_ref (ptr_die, DW_AT_containing_type,
21833 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21834 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21835 context_die);
21838 static char *producer_string;
21840 /* Return a heap allocated producer string including command line options
21841 if -grecord-gcc-switches. */
21843 static char *
21844 gen_producer_string (void)
21846 size_t j;
21847 auto_vec<const char *> switches;
21848 const char *language_string = lang_hooks.name;
21849 char *producer, *tail;
21850 const char *p;
21851 size_t len = dwarf_record_gcc_switches ? 0 : 3;
21852 size_t plen = strlen (language_string) + 1 + strlen (version_string);
21854 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21855 switch (save_decoded_options[j].opt_index)
21857 case OPT_o:
21858 case OPT_d:
21859 case OPT_dumpbase:
21860 case OPT_dumpdir:
21861 case OPT_auxbase:
21862 case OPT_auxbase_strip:
21863 case OPT_quiet:
21864 case OPT_version:
21865 case OPT_v:
21866 case OPT_w:
21867 case OPT_L:
21868 case OPT_D:
21869 case OPT_I:
21870 case OPT_U:
21871 case OPT_SPECIAL_unknown:
21872 case OPT_SPECIAL_ignore:
21873 case OPT_SPECIAL_program_name:
21874 case OPT_SPECIAL_input_file:
21875 case OPT_grecord_gcc_switches:
21876 case OPT_gno_record_gcc_switches:
21877 case OPT__output_pch_:
21878 case OPT_fdiagnostics_show_location_:
21879 case OPT_fdiagnostics_show_option:
21880 case OPT_fdiagnostics_show_caret:
21881 case OPT_fdiagnostics_color_:
21882 case OPT_fverbose_asm:
21883 case OPT____:
21884 case OPT__sysroot_:
21885 case OPT_nostdinc:
21886 case OPT_nostdinc__:
21887 case OPT_fpreprocessed:
21888 case OPT_fltrans_output_list_:
21889 case OPT_fresolution_:
21890 case OPT_fdebug_prefix_map_:
21891 /* Ignore these. */
21892 continue;
21893 default:
21894 if (cl_options[save_decoded_options[j].opt_index].flags
21895 & CL_NO_DWARF_RECORD)
21896 continue;
21897 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21898 == '-');
21899 switch (save_decoded_options[j].canonical_option[0][1])
21901 case 'M':
21902 case 'i':
21903 case 'W':
21904 continue;
21905 case 'f':
21906 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21907 "dump", 4) == 0)
21908 continue;
21909 break;
21910 default:
21911 break;
21913 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21914 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21915 break;
21918 producer = XNEWVEC (char, plen + 1 + len + 1);
21919 tail = producer;
21920 sprintf (tail, "%s %s", language_string, version_string);
21921 tail += plen;
21923 FOR_EACH_VEC_ELT (switches, j, p)
21925 len = strlen (p);
21926 *tail = ' ';
21927 memcpy (tail + 1, p, len);
21928 tail += len + 1;
21931 *tail = '\0';
21932 return producer;
21935 /* Given a C and/or C++ language/version string return the "highest".
21936 C++ is assumed to be "higher" than C in this case. Used for merging
21937 LTO translation unit languages. */
21938 static const char *
21939 highest_c_language (const char *lang1, const char *lang2)
21941 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21942 return "GNU C++14";
21943 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21944 return "GNU C++11";
21945 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21946 return "GNU C++98";
21948 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21949 return "GNU C11";
21950 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
21951 return "GNU C99";
21952 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
21953 return "GNU C89";
21955 gcc_unreachable ();
21959 /* Generate the DIE for the compilation unit. */
21961 static dw_die_ref
21962 gen_compile_unit_die (const char *filename)
21964 dw_die_ref die;
21965 const char *language_string = lang_hooks.name;
21966 int language;
21968 die = new_die (DW_TAG_compile_unit, NULL, NULL);
21970 if (filename)
21972 add_name_attribute (die, filename);
21973 /* Don't add cwd for <built-in>. */
21974 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21975 add_comp_dir_attribute (die);
21978 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
21980 /* If our producer is LTO try to figure out a common language to use
21981 from the global list of translation units. */
21982 if (strcmp (language_string, "GNU GIMPLE") == 0)
21984 unsigned i;
21985 tree t;
21986 const char *common_lang = NULL;
21988 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
21990 if (!TRANSLATION_UNIT_LANGUAGE (t))
21991 continue;
21992 if (!common_lang)
21993 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
21994 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
21996 else if (strncmp (common_lang, "GNU C", 5) == 0
21997 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
21998 /* Mixing C and C++ is ok, use C++ in that case. */
21999 common_lang = highest_c_language (common_lang,
22000 TRANSLATION_UNIT_LANGUAGE (t));
22001 else
22003 /* Fall back to C. */
22004 common_lang = NULL;
22005 break;
22009 if (common_lang)
22010 language_string = common_lang;
22013 language = DW_LANG_C;
22014 if (strncmp (language_string, "GNU C", 5) == 0
22015 && ISDIGIT (language_string[5]))
22017 language = DW_LANG_C89;
22018 if (dwarf_version >= 3 || !dwarf_strict)
22020 if (strcmp (language_string, "GNU C89") != 0)
22021 language = DW_LANG_C99;
22023 if (dwarf_version >= 5 /* || !dwarf_strict */)
22024 if (strcmp (language_string, "GNU C11") == 0)
22025 language = DW_LANG_C11;
22028 else if (strncmp (language_string, "GNU C++", 7) == 0)
22030 language = DW_LANG_C_plus_plus;
22031 if (dwarf_version >= 5 /* || !dwarf_strict */)
22033 if (strcmp (language_string, "GNU C++11") == 0)
22034 language = DW_LANG_C_plus_plus_11;
22035 else if (strcmp (language_string, "GNU C++14") == 0)
22036 language = DW_LANG_C_plus_plus_14;
22039 else if (strcmp (language_string, "GNU F77") == 0)
22040 language = DW_LANG_Fortran77;
22041 else if (strcmp (language_string, "GNU Pascal") == 0)
22042 language = DW_LANG_Pascal83;
22043 else if (dwarf_version >= 3 || !dwarf_strict)
22045 if (strcmp (language_string, "GNU Ada") == 0)
22046 language = DW_LANG_Ada95;
22047 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22049 language = DW_LANG_Fortran95;
22050 if (dwarf_version >= 5 /* || !dwarf_strict */)
22052 if (strcmp (language_string, "GNU Fortran2003") == 0)
22053 language = DW_LANG_Fortran03;
22054 else if (strcmp (language_string, "GNU Fortran2008") == 0)
22055 language = DW_LANG_Fortran08;
22058 else if (strcmp (language_string, "GNU Java") == 0)
22059 language = DW_LANG_Java;
22060 else if (strcmp (language_string, "GNU Objective-C") == 0)
22061 language = DW_LANG_ObjC;
22062 else if (strcmp (language_string, "GNU Objective-C++") == 0)
22063 language = DW_LANG_ObjC_plus_plus;
22064 else if (dwarf_version >= 5 || !dwarf_strict)
22066 if (strcmp (language_string, "GNU Go") == 0)
22067 language = DW_LANG_Go;
22070 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
22071 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22072 language = DW_LANG_Fortran90;
22074 add_AT_unsigned (die, DW_AT_language, language);
22076 switch (language)
22078 case DW_LANG_Fortran77:
22079 case DW_LANG_Fortran90:
22080 case DW_LANG_Fortran95:
22081 case DW_LANG_Fortran03:
22082 case DW_LANG_Fortran08:
22083 /* Fortran has case insensitive identifiers and the front-end
22084 lowercases everything. */
22085 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
22086 break;
22087 default:
22088 /* The default DW_ID_case_sensitive doesn't need to be specified. */
22089 break;
22091 return die;
22094 /* Generate the DIE for a base class. */
22096 static void
22097 gen_inheritance_die (tree binfo, tree access, tree type,
22098 dw_die_ref context_die)
22100 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
22101 struct vlr_context ctx = { type, NULL };
22103 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
22104 context_die);
22105 add_data_member_location_attribute (die, binfo, &ctx);
22107 if (BINFO_VIRTUAL_P (binfo))
22108 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22110 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
22111 children, otherwise the default is DW_ACCESS_public. In DWARF2
22112 the default has always been DW_ACCESS_private. */
22113 if (access == access_public_node)
22115 if (dwarf_version == 2
22116 || context_die->die_tag == DW_TAG_class_type)
22117 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
22119 else if (access == access_protected_node)
22120 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
22121 else if (dwarf_version > 2
22122 && context_die->die_tag != DW_TAG_class_type)
22123 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
22126 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
22127 structure. */
22128 static bool
22129 is_variant_part (tree decl)
22131 return (TREE_CODE (decl) == FIELD_DECL
22132 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
22135 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
22136 return the FIELD_DECL. Return NULL_TREE otherwise. */
22138 static tree
22139 analyze_discr_in_predicate (tree operand, tree struct_type)
22141 bool continue_stripping = true;
22142 while (continue_stripping)
22143 switch (TREE_CODE (operand))
22145 CASE_CONVERT:
22146 operand = TREE_OPERAND (operand, 0);
22147 break;
22148 default:
22149 continue_stripping = false;
22150 break;
22153 /* Match field access to members of struct_type only. */
22154 if (TREE_CODE (operand) == COMPONENT_REF
22155 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
22156 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
22157 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
22158 return TREE_OPERAND (operand, 1);
22159 else
22160 return NULL_TREE;
22163 /* Check that SRC is a constant integer that can be represented as a native
22164 integer constant (either signed or unsigned). If so, store it into DEST and
22165 return true. Return false otherwise. */
22167 static bool
22168 get_discr_value (tree src, dw_discr_value *dest)
22170 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
22172 if (TREE_CODE (src) != INTEGER_CST
22173 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
22174 return false;
22176 dest->pos = is_unsigned;
22177 if (is_unsigned)
22178 dest->v.uval = tree_to_uhwi (src);
22179 else
22180 dest->v.sval = tree_to_shwi (src);
22182 return true;
22185 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
22186 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
22187 store NULL_TREE in DISCR_DECL. Otherwise:
22189 - store the discriminant field in STRUCT_TYPE that controls the variant
22190 part to *DISCR_DECL
22192 - put in *DISCR_LISTS_P an array where for each variant, the item
22193 represents the corresponding matching list of discriminant values.
22195 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
22196 the above array.
22198 Note that when the array is allocated (i.e. when the analysis is
22199 successful), it is up to the caller to free the array. */
22201 static void
22202 analyze_variants_discr (tree variant_part_decl,
22203 tree struct_type,
22204 tree *discr_decl,
22205 dw_discr_list_ref **discr_lists_p,
22206 unsigned *discr_lists_length)
22208 tree variant_part_type = TREE_TYPE (variant_part_decl);
22209 tree variant;
22210 dw_discr_list_ref *discr_lists;
22211 unsigned i;
22213 /* Compute how many variants there are in this variant part. */
22214 *discr_lists_length = 0;
22215 for (variant = TYPE_FIELDS (variant_part_type);
22216 variant != NULL_TREE;
22217 variant = DECL_CHAIN (variant))
22218 ++*discr_lists_length;
22220 *discr_decl = NULL_TREE;
22221 *discr_lists_p
22222 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22223 sizeof (**discr_lists_p));
22224 discr_lists = *discr_lists_p;
22226 /* And then analyze all variants to extract discriminant information for all
22227 of them. This analysis is conservative: as soon as we detect something we
22228 do not support, abort everything and pretend we found nothing. */
22229 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22230 variant != NULL_TREE;
22231 variant = DECL_CHAIN (variant), ++i)
22233 tree match_expr = DECL_QUALIFIER (variant);
22235 /* Now, try to analyze the predicate and deduce a discriminant for
22236 it. */
22237 if (match_expr == boolean_true_node)
22238 /* Typically happens for the default variant: it matches all cases that
22239 previous variants rejected. Don't output any matching value for
22240 this one. */
22241 continue;
22243 /* The following loop tries to iterate over each discriminant
22244 possibility: single values or ranges. */
22245 while (match_expr != NULL_TREE)
22247 tree next_round_match_expr;
22248 tree candidate_discr = NULL_TREE;
22249 dw_discr_list_ref new_node = NULL;
22251 /* Possibilities are matched one after the other by nested
22252 TRUTH_ORIF_EXPR expressions. Process the current possibility and
22253 continue with the rest at next iteration. */
22254 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22256 next_round_match_expr = TREE_OPERAND (match_expr, 0);
22257 match_expr = TREE_OPERAND (match_expr, 1);
22259 else
22260 next_round_match_expr = NULL_TREE;
22262 if (match_expr == boolean_false_node)
22263 /* This sub-expression matches nothing: just wait for the next
22264 one. */
22267 else if (TREE_CODE (match_expr) == EQ_EXPR)
22269 /* We are matching: <discr_field> == <integer_cst>
22270 This sub-expression matches a single value. */
22271 tree integer_cst = TREE_OPERAND (match_expr, 1);
22273 candidate_discr
22274 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22275 struct_type);
22277 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22278 if (!get_discr_value (integer_cst,
22279 &new_node->dw_discr_lower_bound))
22280 goto abort;
22281 new_node->dw_discr_range = false;
22284 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22286 /* We are matching:
22287 <discr_field> > <integer_cst>
22288 && <discr_field> < <integer_cst>.
22289 This sub-expression matches the range of values between the
22290 two matched integer constants. Note that comparisons can be
22291 inclusive or exclusive. */
22292 tree candidate_discr_1, candidate_discr_2;
22293 tree lower_cst, upper_cst;
22294 bool lower_cst_included, upper_cst_included;
22295 tree lower_op = TREE_OPERAND (match_expr, 0);
22296 tree upper_op = TREE_OPERAND (match_expr, 1);
22298 /* When the comparison is exclusive, the integer constant is not
22299 the discriminant range bound we are looking for: we will have
22300 to increment or decrement it. */
22301 if (TREE_CODE (lower_op) == GE_EXPR)
22302 lower_cst_included = true;
22303 else if (TREE_CODE (lower_op) == GT_EXPR)
22304 lower_cst_included = false;
22305 else
22306 goto abort;
22308 if (TREE_CODE (upper_op) == LE_EXPR)
22309 upper_cst_included = true;
22310 else if (TREE_CODE (upper_op) == LT_EXPR)
22311 upper_cst_included = false;
22312 else
22313 goto abort;
22315 /* Extract the discriminant from the first operand and check it
22316 is consistant with the same analysis in the second
22317 operand. */
22318 candidate_discr_1
22319 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22320 struct_type);
22321 candidate_discr_2
22322 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22323 struct_type);
22324 if (candidate_discr_1 == candidate_discr_2)
22325 candidate_discr = candidate_discr_1;
22326 else
22327 goto abort;
22329 /* Extract bounds from both. */
22330 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22331 lower_cst = TREE_OPERAND (lower_op, 1);
22332 upper_cst = TREE_OPERAND (upper_op, 1);
22334 if (!lower_cst_included)
22335 lower_cst
22336 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22337 lower_cst,
22338 build_int_cst (TREE_TYPE (lower_cst), 1)));
22339 if (!upper_cst_included)
22340 upper_cst
22341 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22342 upper_cst,
22343 build_int_cst (TREE_TYPE (upper_cst), 1)));
22345 if (!get_discr_value (lower_cst,
22346 &new_node->dw_discr_lower_bound)
22347 || !get_discr_value (upper_cst,
22348 &new_node->dw_discr_upper_bound))
22349 goto abort;
22351 new_node->dw_discr_range = true;
22354 else
22355 /* Unsupported sub-expression: we cannot determine the set of
22356 matching discriminant values. Abort everything. */
22357 goto abort;
22359 /* If the discriminant info is not consistant with what we saw so
22360 far, consider the analysis failed and abort everything. */
22361 if (candidate_discr == NULL_TREE
22362 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22363 goto abort;
22364 else
22365 *discr_decl = candidate_discr;
22367 if (new_node != NULL)
22369 new_node->dw_discr_next = discr_lists[i];
22370 discr_lists[i] = new_node;
22372 match_expr = next_round_match_expr;
22376 /* If we reach this point, we could match everything we were interested
22377 in. */
22378 return;
22380 abort:
22381 /* Clean all data structure and return no result. */
22382 free (*discr_lists_p);
22383 *discr_lists_p = NULL;
22384 *discr_decl = NULL_TREE;
22387 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22388 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
22389 under CONTEXT_DIE.
22391 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22392 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
22393 this type, which are record types, represent the available variants and each
22394 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
22395 values are inferred from these attributes.
22397 In trees, the offsets for the fields inside these sub-records are relative
22398 to the variant part itself, whereas the corresponding DIEs should have
22399 offset attributes that are relative to the embedding record base address.
22400 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22401 must be an expression that computes the offset of the variant part to
22402 describe in DWARF. */
22404 static void
22405 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22406 dw_die_ref context_die)
22408 const tree variant_part_type = TREE_TYPE (variant_part_decl);
22409 tree variant_part_offset = vlr_ctx->variant_part_offset;
22410 struct loc_descr_context ctx = {
22411 vlr_ctx->struct_type, /* context_type */
22412 NULL_TREE, /* base_decl */
22413 NULL /* dpi */
22416 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22417 NULL_TREE if there is no such field. */
22418 tree discr_decl = NULL_TREE;
22419 dw_discr_list_ref *discr_lists;
22420 unsigned discr_lists_length = 0;
22421 unsigned i;
22423 dw_die_ref dwarf_proc_die = NULL;
22424 dw_die_ref variant_part_die
22425 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22427 equate_decl_number_to_die (variant_part_decl, variant_part_die);
22429 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22430 &discr_decl, &discr_lists, &discr_lists_length);
22432 if (discr_decl != NULL_TREE)
22434 dw_die_ref discr_die = lookup_decl_die (discr_decl);
22436 if (discr_die)
22437 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22438 else
22439 /* We have no DIE for the discriminant, so just discard all
22440 discrimimant information in the output. */
22441 discr_decl = NULL_TREE;
22444 /* If the offset for this variant part is more complex than a constant,
22445 create a DWARF procedure for it so that we will not have to generate DWARF
22446 expressions for it for each member. */
22447 if (TREE_CODE (variant_part_offset) != INTEGER_CST
22448 && (dwarf_version >= 3 || !dwarf_strict))
22450 const tree dwarf_proc_fndecl
22451 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22452 build_function_type (TREE_TYPE (variant_part_offset),
22453 NULL_TREE));
22454 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22455 const dw_loc_descr_ref dwarf_proc_body
22456 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22458 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22459 dwarf_proc_fndecl, context_die);
22460 if (dwarf_proc_die != NULL)
22461 variant_part_offset = dwarf_proc_call;
22464 /* Output DIEs for all variants. */
22465 i = 0;
22466 for (tree variant = TYPE_FIELDS (variant_part_type);
22467 variant != NULL_TREE;
22468 variant = DECL_CHAIN (variant), ++i)
22470 tree variant_type = TREE_TYPE (variant);
22471 dw_die_ref variant_die;
22473 /* All variants (i.e. members of a variant part) are supposed to be
22474 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
22475 under these records. */
22476 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22478 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22479 equate_decl_number_to_die (variant, variant_die);
22481 /* Output discriminant values this variant matches, if any. */
22482 if (discr_decl == NULL || discr_lists[i] == NULL)
22483 /* In the case we have discriminant information at all, this is
22484 probably the default variant: as the standard says, don't
22485 output any discriminant value/list attribute. */
22487 else if (discr_lists[i]->dw_discr_next == NULL
22488 && !discr_lists[i]->dw_discr_range)
22489 /* If there is only one accepted value, don't bother outputting a
22490 list. */
22491 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22492 else
22493 add_discr_list (variant_die, discr_lists[i]);
22495 for (tree member = TYPE_FIELDS (variant_type);
22496 member != NULL_TREE;
22497 member = DECL_CHAIN (member))
22499 struct vlr_context vlr_sub_ctx = {
22500 vlr_ctx->struct_type, /* struct_type */
22501 NULL /* variant_part_offset */
22503 if (is_variant_part (member))
22505 /* All offsets for fields inside variant parts are relative to
22506 the top-level embedding RECORD_TYPE's base address. On the
22507 other hand, offsets in GCC's types are relative to the
22508 nested-most variant part. So we have to sum offsets each time
22509 we recurse. */
22511 vlr_sub_ctx.variant_part_offset
22512 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22513 variant_part_offset, byte_position (member)));
22514 gen_variant_part (member, &vlr_sub_ctx, variant_die);
22516 else
22518 vlr_sub_ctx.variant_part_offset = variant_part_offset;
22519 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22524 free (discr_lists);
22527 /* Generate a DIE for a class member. */
22529 static void
22530 gen_member_die (tree type, dw_die_ref context_die)
22532 tree member;
22533 tree binfo = TYPE_BINFO (type);
22534 dw_die_ref child;
22536 /* If this is not an incomplete type, output descriptions of each of its
22537 members. Note that as we output the DIEs necessary to represent the
22538 members of this record or union type, we will also be trying to output
22539 DIEs to represent the *types* of those members. However the `type'
22540 function (above) will specifically avoid generating type DIEs for member
22541 types *within* the list of member DIEs for this (containing) type except
22542 for those types (of members) which are explicitly marked as also being
22543 members of this (containing) type themselves. The g++ front- end can
22544 force any given type to be treated as a member of some other (containing)
22545 type by setting the TYPE_CONTEXT of the given (member) type to point to
22546 the TREE node representing the appropriate (containing) type. */
22548 /* First output info about the base classes. */
22549 if (binfo)
22551 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22552 int i;
22553 tree base;
22555 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22556 gen_inheritance_die (base,
22557 (accesses ? (*accesses)[i] : access_public_node),
22558 type,
22559 context_die);
22562 /* Now output info about the data members and type members. */
22563 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22565 struct vlr_context vlr_ctx = { type, NULL_TREE };
22567 /* If we thought we were generating minimal debug info for TYPE
22568 and then changed our minds, some of the member declarations
22569 may have already been defined. Don't define them again, but
22570 do put them in the right order. */
22572 child = lookup_decl_die (member);
22573 if (child)
22574 splice_child_die (context_die, child);
22576 /* Do not generate standard DWARF for variant parts if we are generating
22577 the corresponding GNAT encodings: DIEs generated for both would
22578 conflict in our mappings. */
22579 else if (is_variant_part (member)
22580 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22582 vlr_ctx.variant_part_offset = byte_position (member);
22583 gen_variant_part (member, &vlr_ctx, context_die);
22585 else
22587 vlr_ctx.variant_part_offset = NULL_TREE;
22588 gen_decl_die (member, NULL, &vlr_ctx, context_die);
22592 /* We do not keep type methods in type variants. */
22593 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22594 /* Now output info about the function members (if any). */
22595 if (TYPE_METHODS (type) != error_mark_node)
22596 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22598 /* Don't include clones in the member list. */
22599 if (DECL_ABSTRACT_ORIGIN (member))
22600 continue;
22601 /* Nor constructors for anonymous classes. */
22602 if (DECL_ARTIFICIAL (member)
22603 && dwarf2_name (member, 0) == NULL)
22604 continue;
22606 child = lookup_decl_die (member);
22607 if (child)
22608 splice_child_die (context_die, child);
22609 else
22610 gen_decl_die (member, NULL, NULL, context_die);
22614 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
22615 is set, we pretend that the type was never defined, so we only get the
22616 member DIEs needed by later specification DIEs. */
22618 static void
22619 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22620 enum debug_info_usage usage)
22622 if (TREE_ASM_WRITTEN (type))
22624 /* Fill in the bound of variable-length fields in late dwarf if
22625 still incomplete. */
22626 if (!early_dwarf && variably_modified_type_p (type, NULL))
22627 for (tree member = TYPE_FIELDS (type);
22628 member;
22629 member = DECL_CHAIN (member))
22630 fill_variable_array_bounds (TREE_TYPE (member));
22631 return;
22634 dw_die_ref type_die = lookup_type_die (type);
22635 dw_die_ref scope_die = 0;
22636 int nested = 0;
22637 int complete = (TYPE_SIZE (type)
22638 && (! TYPE_STUB_DECL (type)
22639 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22640 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22641 complete = complete && should_emit_struct_debug (type, usage);
22643 if (type_die && ! complete)
22644 return;
22646 if (TYPE_CONTEXT (type) != NULL_TREE
22647 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22648 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22649 nested = 1;
22651 scope_die = scope_die_for (type, context_die);
22653 /* Generate child dies for template paramaters. */
22654 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22655 schedule_generic_params_dies_gen (type);
22657 if (! type_die || (nested && is_cu_die (scope_die)))
22658 /* First occurrence of type or toplevel definition of nested class. */
22660 dw_die_ref old_die = type_die;
22662 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22663 ? record_type_tag (type) : DW_TAG_union_type,
22664 scope_die, type);
22665 equate_type_number_to_die (type, type_die);
22666 if (old_die)
22667 add_AT_specification (type_die, old_die);
22668 else
22669 add_name_attribute (type_die, type_tag (type));
22671 else
22672 remove_AT (type_die, DW_AT_declaration);
22674 /* If this type has been completed, then give it a byte_size attribute and
22675 then give a list of members. */
22676 if (complete && !ns_decl)
22678 /* Prevent infinite recursion in cases where the type of some member of
22679 this type is expressed in terms of this type itself. */
22680 TREE_ASM_WRITTEN (type) = 1;
22681 add_byte_size_attribute (type_die, type);
22682 if (TYPE_STUB_DECL (type) != NULL_TREE)
22684 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22685 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22688 /* If the first reference to this type was as the return type of an
22689 inline function, then it may not have a parent. Fix this now. */
22690 if (type_die->die_parent == NULL)
22691 add_child_die (scope_die, type_die);
22693 push_decl_scope (type);
22694 gen_member_die (type, type_die);
22695 pop_decl_scope ();
22697 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22698 if (TYPE_ARTIFICIAL (type))
22699 add_AT_flag (type_die, DW_AT_artificial, 1);
22701 /* GNU extension: Record what type our vtable lives in. */
22702 if (TYPE_VFIELD (type))
22704 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22706 gen_type_die (vtype, context_die);
22707 add_AT_die_ref (type_die, DW_AT_containing_type,
22708 lookup_type_die (vtype));
22711 else
22713 add_AT_flag (type_die, DW_AT_declaration, 1);
22715 /* We don't need to do this for function-local types. */
22716 if (TYPE_STUB_DECL (type)
22717 && ! decl_function_context (TYPE_STUB_DECL (type)))
22718 vec_safe_push (incomplete_types, type);
22721 if (get_AT (type_die, DW_AT_name))
22722 add_pubtype (type, type_die);
22725 /* Generate a DIE for a subroutine _type_. */
22727 static void
22728 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22730 tree return_type = TREE_TYPE (type);
22731 dw_die_ref subr_die
22732 = new_die (DW_TAG_subroutine_type,
22733 scope_die_for (type, context_die), type);
22735 equate_type_number_to_die (type, subr_die);
22736 add_prototyped_attribute (subr_die, type);
22737 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22738 context_die);
22739 gen_formal_types_die (type, subr_die);
22741 if (get_AT (subr_die, DW_AT_name))
22742 add_pubtype (type, subr_die);
22745 /* Generate a DIE for a type definition. */
22747 static void
22748 gen_typedef_die (tree decl, dw_die_ref context_die)
22750 dw_die_ref type_die;
22751 tree origin;
22753 if (TREE_ASM_WRITTEN (decl))
22755 if (DECL_ORIGINAL_TYPE (decl))
22756 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22757 return;
22760 TREE_ASM_WRITTEN (decl) = 1;
22761 type_die = new_die (DW_TAG_typedef, context_die, decl);
22762 origin = decl_ultimate_origin (decl);
22763 if (origin != NULL)
22764 add_abstract_origin_attribute (type_die, origin);
22765 else
22767 tree type;
22769 add_name_and_src_coords_attributes (type_die, decl);
22770 if (DECL_ORIGINAL_TYPE (decl))
22772 type = DECL_ORIGINAL_TYPE (decl);
22774 if (type == error_mark_node)
22775 return;
22777 gcc_assert (type != TREE_TYPE (decl));
22778 equate_type_number_to_die (TREE_TYPE (decl), type_die);
22780 else
22782 type = TREE_TYPE (decl);
22784 if (type == error_mark_node)
22785 return;
22787 if (is_naming_typedef_decl (TYPE_NAME (type)))
22789 /* Here, we are in the case of decl being a typedef naming
22790 an anonymous type, e.g:
22791 typedef struct {...} foo;
22792 In that case TREE_TYPE (decl) is not a typedef variant
22793 type and TYPE_NAME of the anonymous type is set to the
22794 TYPE_DECL of the typedef. This construct is emitted by
22795 the C++ FE.
22797 TYPE is the anonymous struct named by the typedef
22798 DECL. As we need the DW_AT_type attribute of the
22799 DW_TAG_typedef to point to the DIE of TYPE, let's
22800 generate that DIE right away. add_type_attribute
22801 called below will then pick (via lookup_type_die) that
22802 anonymous struct DIE. */
22803 if (!TREE_ASM_WRITTEN (type))
22804 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22806 /* This is a GNU Extension. We are adding a
22807 DW_AT_linkage_name attribute to the DIE of the
22808 anonymous struct TYPE. The value of that attribute
22809 is the name of the typedef decl naming the anonymous
22810 struct. This greatly eases the work of consumers of
22811 this debug info. */
22812 add_linkage_name_raw (lookup_type_die (type), decl);
22816 add_type_attribute (type_die, type, decl_quals (decl), false,
22817 context_die);
22819 if (is_naming_typedef_decl (decl))
22820 /* We want that all subsequent calls to lookup_type_die with
22821 TYPE in argument yield the DW_TAG_typedef we have just
22822 created. */
22823 equate_type_number_to_die (type, type_die);
22825 add_accessibility_attribute (type_die, decl);
22828 if (DECL_ABSTRACT_P (decl))
22829 equate_decl_number_to_die (decl, type_die);
22831 if (get_AT (type_die, DW_AT_name))
22832 add_pubtype (decl, type_die);
22835 /* Generate a DIE for a struct, class, enum or union type. */
22837 static void
22838 gen_tagged_type_die (tree type,
22839 dw_die_ref context_die,
22840 enum debug_info_usage usage)
22842 int need_pop;
22844 if (type == NULL_TREE
22845 || !is_tagged_type (type))
22846 return;
22848 if (TREE_ASM_WRITTEN (type))
22849 need_pop = 0;
22850 /* If this is a nested type whose containing class hasn't been written
22851 out yet, writing it out will cover this one, too. This does not apply
22852 to instantiations of member class templates; they need to be added to
22853 the containing class as they are generated. FIXME: This hurts the
22854 idea of combining type decls from multiple TUs, since we can't predict
22855 what set of template instantiations we'll get. */
22856 else if (TYPE_CONTEXT (type)
22857 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22858 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22860 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22862 if (TREE_ASM_WRITTEN (type))
22863 return;
22865 /* If that failed, attach ourselves to the stub. */
22866 push_decl_scope (TYPE_CONTEXT (type));
22867 context_die = lookup_type_die (TYPE_CONTEXT (type));
22868 need_pop = 1;
22870 else if (TYPE_CONTEXT (type) != NULL_TREE
22871 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22873 /* If this type is local to a function that hasn't been written
22874 out yet, use a NULL context for now; it will be fixed up in
22875 decls_for_scope. */
22876 context_die = lookup_decl_die (TYPE_CONTEXT (type));
22877 /* A declaration DIE doesn't count; nested types need to go in the
22878 specification. */
22879 if (context_die && is_declaration_die (context_die))
22880 context_die = NULL;
22881 need_pop = 0;
22883 else
22885 context_die = declare_in_namespace (type, context_die);
22886 need_pop = 0;
22889 if (TREE_CODE (type) == ENUMERAL_TYPE)
22891 /* This might have been written out by the call to
22892 declare_in_namespace. */
22893 if (!TREE_ASM_WRITTEN (type))
22894 gen_enumeration_type_die (type, context_die);
22896 else
22897 gen_struct_or_union_type_die (type, context_die, usage);
22899 if (need_pop)
22900 pop_decl_scope ();
22902 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22903 it up if it is ever completed. gen_*_type_die will set it for us
22904 when appropriate. */
22907 /* Generate a type description DIE. */
22909 static void
22910 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22911 enum debug_info_usage usage)
22913 struct array_descr_info info;
22915 if (type == NULL_TREE || type == error_mark_node)
22916 return;
22918 if (flag_checking && type)
22919 verify_type (type);
22921 if (TYPE_NAME (type) != NULL_TREE
22922 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22923 && is_redundant_typedef (TYPE_NAME (type))
22924 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22925 /* The DECL of this type is a typedef we don't want to emit debug
22926 info for but we want debug info for its underlying typedef.
22927 This can happen for e.g, the injected-class-name of a C++
22928 type. */
22929 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22931 /* If TYPE is a typedef type variant, let's generate debug info
22932 for the parent typedef which TYPE is a type of. */
22933 if (typedef_variant_p (type))
22935 if (TREE_ASM_WRITTEN (type))
22936 return;
22938 /* Prevent broken recursion; we can't hand off to the same type. */
22939 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22941 /* Give typedefs the right scope. */
22942 context_die = scope_die_for (type, context_die);
22944 TREE_ASM_WRITTEN (type) = 1;
22946 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22947 return;
22950 /* If type is an anonymous tagged type named by a typedef, let's
22951 generate debug info for the typedef. */
22952 if (is_naming_typedef_decl (TYPE_NAME (type)))
22954 /* Use the DIE of the containing namespace as the parent DIE of
22955 the type description DIE we want to generate. */
22956 if (DECL_CONTEXT (TYPE_NAME (type))
22957 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
22958 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
22960 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22961 return;
22964 /* We are going to output a DIE to represent the unqualified version
22965 of this type (i.e. without any const or volatile qualifiers) so
22966 get the main variant (i.e. the unqualified version) of this type
22967 now. (Vectors and arrays are special because the debugging info is in the
22968 cloned type itself). */
22969 if (TREE_CODE (type) != VECTOR_TYPE
22970 && TREE_CODE (type) != ARRAY_TYPE)
22971 type = type_main_variant (type);
22973 /* If this is an array type with hidden descriptor, handle it first. */
22974 if (!TREE_ASM_WRITTEN (type)
22975 && lang_hooks.types.get_array_descr_info)
22977 memset (&info, 0, sizeof (info));
22978 if (lang_hooks.types.get_array_descr_info (type, &info))
22980 /* Fortran sometimes emits array types with no dimension. */
22981 gcc_assert (info.ndimensions >= 0
22982 && (info.ndimensions
22983 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
22984 gen_descr_array_type_die (type, &info, context_die);
22985 TREE_ASM_WRITTEN (type) = 1;
22986 return;
22990 if (TREE_ASM_WRITTEN (type))
22992 /* Variable-length types may be incomplete even if
22993 TREE_ASM_WRITTEN. For such types, fall through to
22994 gen_array_type_die() and possibly fill in
22995 DW_AT_{upper,lower}_bound attributes. */
22996 if ((TREE_CODE (type) != ARRAY_TYPE
22997 && TREE_CODE (type) != RECORD_TYPE
22998 && TREE_CODE (type) != UNION_TYPE
22999 && TREE_CODE (type) != QUAL_UNION_TYPE)
23000 || !variably_modified_type_p (type, NULL))
23001 return;
23004 switch (TREE_CODE (type))
23006 case ERROR_MARK:
23007 break;
23009 case POINTER_TYPE:
23010 case REFERENCE_TYPE:
23011 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
23012 ensures that the gen_type_die recursion will terminate even if the
23013 type is recursive. Recursive types are possible in Ada. */
23014 /* ??? We could perhaps do this for all types before the switch
23015 statement. */
23016 TREE_ASM_WRITTEN (type) = 1;
23018 /* For these types, all that is required is that we output a DIE (or a
23019 set of DIEs) to represent the "basis" type. */
23020 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23021 DINFO_USAGE_IND_USE);
23022 break;
23024 case OFFSET_TYPE:
23025 /* This code is used for C++ pointer-to-data-member types.
23026 Output a description of the relevant class type. */
23027 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
23028 DINFO_USAGE_IND_USE);
23030 /* Output a description of the type of the object pointed to. */
23031 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23032 DINFO_USAGE_IND_USE);
23034 /* Now output a DIE to represent this pointer-to-data-member type
23035 itself. */
23036 gen_ptr_to_mbr_type_die (type, context_die);
23037 break;
23039 case FUNCTION_TYPE:
23040 /* Force out return type (in case it wasn't forced out already). */
23041 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23042 DINFO_USAGE_DIR_USE);
23043 gen_subroutine_type_die (type, context_die);
23044 break;
23046 case METHOD_TYPE:
23047 /* Force out return type (in case it wasn't forced out already). */
23048 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23049 DINFO_USAGE_DIR_USE);
23050 gen_subroutine_type_die (type, context_die);
23051 break;
23053 case ARRAY_TYPE:
23054 case VECTOR_TYPE:
23055 gen_array_type_die (type, context_die);
23056 break;
23058 case ENUMERAL_TYPE:
23059 case RECORD_TYPE:
23060 case UNION_TYPE:
23061 case QUAL_UNION_TYPE:
23062 gen_tagged_type_die (type, context_die, usage);
23063 return;
23065 case VOID_TYPE:
23066 case INTEGER_TYPE:
23067 case REAL_TYPE:
23068 case FIXED_POINT_TYPE:
23069 case COMPLEX_TYPE:
23070 case BOOLEAN_TYPE:
23071 case POINTER_BOUNDS_TYPE:
23072 /* No DIEs needed for fundamental types. */
23073 break;
23075 case NULLPTR_TYPE:
23076 case LANG_TYPE:
23077 /* Just use DW_TAG_unspecified_type. */
23079 dw_die_ref type_die = lookup_type_die (type);
23080 if (type_die == NULL)
23082 tree name = TYPE_IDENTIFIER (type);
23083 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
23084 type);
23085 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
23086 equate_type_number_to_die (type, type_die);
23089 break;
23091 default:
23092 if (is_cxx_auto (type))
23094 tree name = TYPE_IDENTIFIER (type);
23095 dw_die_ref *die = (name == get_identifier ("auto")
23096 ? &auto_die : &decltype_auto_die);
23097 if (!*die)
23099 *die = new_die (DW_TAG_unspecified_type,
23100 comp_unit_die (), NULL_TREE);
23101 add_name_attribute (*die, IDENTIFIER_POINTER (name));
23103 equate_type_number_to_die (type, *die);
23104 break;
23106 gcc_unreachable ();
23109 TREE_ASM_WRITTEN (type) = 1;
23112 static void
23113 gen_type_die (tree type, dw_die_ref context_die)
23115 if (type != error_mark_node)
23117 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
23118 if (flag_checking)
23120 dw_die_ref die = lookup_type_die (type);
23121 if (die)
23122 check_die (die);
23127 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
23128 things which are local to the given block. */
23130 static void
23131 gen_block_die (tree stmt, dw_die_ref context_die)
23133 int must_output_die = 0;
23134 bool inlined_func;
23136 /* Ignore blocks that are NULL. */
23137 if (stmt == NULL_TREE)
23138 return;
23140 inlined_func = inlined_function_outer_scope_p (stmt);
23142 /* If the block is one fragment of a non-contiguous block, do not
23143 process the variables, since they will have been done by the
23144 origin block. Do process subblocks. */
23145 if (BLOCK_FRAGMENT_ORIGIN (stmt))
23147 tree sub;
23149 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
23150 gen_block_die (sub, context_die);
23152 return;
23155 /* Determine if we need to output any Dwarf DIEs at all to represent this
23156 block. */
23157 if (inlined_func)
23158 /* The outer scopes for inlinings *must* always be represented. We
23159 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
23160 must_output_die = 1;
23161 else
23163 /* Determine if this block directly contains any "significant"
23164 local declarations which we will need to output DIEs for. */
23165 if (debug_info_level > DINFO_LEVEL_TERSE)
23166 /* We are not in terse mode so *any* local declaration counts
23167 as being a "significant" one. */
23168 must_output_die = ((BLOCK_VARS (stmt) != NULL
23169 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
23170 && (TREE_USED (stmt)
23171 || TREE_ASM_WRITTEN (stmt)
23172 || BLOCK_ABSTRACT (stmt)));
23173 else if ((TREE_USED (stmt)
23174 || TREE_ASM_WRITTEN (stmt)
23175 || BLOCK_ABSTRACT (stmt))
23176 && !dwarf2out_ignore_block (stmt))
23177 must_output_die = 1;
23180 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
23181 DIE for any block which contains no significant local declarations at
23182 all. Rather, in such cases we just call `decls_for_scope' so that any
23183 needed Dwarf info for any sub-blocks will get properly generated. Note
23184 that in terse mode, our definition of what constitutes a "significant"
23185 local declaration gets restricted to include only inlined function
23186 instances and local (nested) function definitions. */
23187 if (must_output_die)
23189 if (inlined_func)
23191 /* If STMT block is abstract, that means we have been called
23192 indirectly from dwarf2out_abstract_function.
23193 That function rightfully marks the descendent blocks (of
23194 the abstract function it is dealing with) as being abstract,
23195 precisely to prevent us from emitting any
23196 DW_TAG_inlined_subroutine DIE as a descendent
23197 of an abstract function instance. So in that case, we should
23198 not call gen_inlined_subroutine_die.
23200 Later though, when cgraph asks dwarf2out to emit info
23201 for the concrete instance of the function decl into which
23202 the concrete instance of STMT got inlined, the later will lead
23203 to the generation of a DW_TAG_inlined_subroutine DIE. */
23204 if (! BLOCK_ABSTRACT (stmt))
23205 gen_inlined_subroutine_die (stmt, context_die);
23207 else
23208 gen_lexical_block_die (stmt, context_die);
23210 else
23211 decls_for_scope (stmt, context_die);
23214 /* Process variable DECL (or variable with origin ORIGIN) within
23215 block STMT and add it to CONTEXT_DIE. */
23216 static void
23217 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23219 dw_die_ref die;
23220 tree decl_or_origin = decl ? decl : origin;
23222 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23223 die = lookup_decl_die (decl_or_origin);
23224 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
23225 && TYPE_DECL_IS_STUB (decl_or_origin))
23226 die = lookup_type_die (TREE_TYPE (decl_or_origin));
23227 else
23228 die = NULL;
23230 if (die != NULL && die->die_parent == NULL)
23231 add_child_die (context_die, die);
23232 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23234 if (early_dwarf)
23235 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23236 stmt, context_die);
23238 else
23239 gen_decl_die (decl, origin, NULL, context_die);
23242 /* Generate all of the decls declared within a given scope and (recursively)
23243 all of its sub-blocks. */
23245 static void
23246 decls_for_scope (tree stmt, dw_die_ref context_die)
23248 tree decl;
23249 unsigned int i;
23250 tree subblocks;
23252 /* Ignore NULL blocks. */
23253 if (stmt == NULL_TREE)
23254 return;
23256 /* Output the DIEs to represent all of the data objects and typedefs
23257 declared directly within this block but not within any nested
23258 sub-blocks. Also, nested function and tag DIEs have been
23259 generated with a parent of NULL; fix that up now. We don't
23260 have to do this if we're at -g1. */
23261 if (debug_info_level > DINFO_LEVEL_TERSE)
23263 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23264 process_scope_var (stmt, decl, NULL_TREE, context_die);
23265 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23266 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23267 context_die);
23270 /* Even if we're at -g1, we need to process the subblocks in order to get
23271 inlined call information. */
23273 /* Output the DIEs to represent all sub-blocks (and the items declared
23274 therein) of this block. */
23275 for (subblocks = BLOCK_SUBBLOCKS (stmt);
23276 subblocks != NULL;
23277 subblocks = BLOCK_CHAIN (subblocks))
23278 gen_block_die (subblocks, context_die);
23281 /* Is this a typedef we can avoid emitting? */
23283 bool
23284 is_redundant_typedef (const_tree decl)
23286 if (TYPE_DECL_IS_STUB (decl))
23287 return true;
23289 if (DECL_ARTIFICIAL (decl)
23290 && DECL_CONTEXT (decl)
23291 && is_tagged_type (DECL_CONTEXT (decl))
23292 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23293 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23294 /* Also ignore the artificial member typedef for the class name. */
23295 return true;
23297 return false;
23300 /* Return TRUE if TYPE is a typedef that names a type for linkage
23301 purposes. This kind of typedefs is produced by the C++ FE for
23302 constructs like:
23304 typedef struct {...} foo;
23306 In that case, there is no typedef variant type produced for foo.
23307 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23308 struct type. */
23310 static bool
23311 is_naming_typedef_decl (const_tree decl)
23313 if (decl == NULL_TREE
23314 || TREE_CODE (decl) != TYPE_DECL
23315 || DECL_NAMELESS (decl)
23316 || !is_tagged_type (TREE_TYPE (decl))
23317 || DECL_IS_BUILTIN (decl)
23318 || is_redundant_typedef (decl)
23319 /* It looks like Ada produces TYPE_DECLs that are very similar
23320 to C++ naming typedefs but that have different
23321 semantics. Let's be specific to c++ for now. */
23322 || !is_cxx ())
23323 return FALSE;
23325 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23326 && TYPE_NAME (TREE_TYPE (decl)) == decl
23327 && (TYPE_STUB_DECL (TREE_TYPE (decl))
23328 != TYPE_NAME (TREE_TYPE (decl))));
23331 /* Looks up the DIE for a context. */
23333 static inline dw_die_ref
23334 lookup_context_die (tree context)
23336 if (context)
23338 /* Find die that represents this context. */
23339 if (TYPE_P (context))
23341 context = TYPE_MAIN_VARIANT (context);
23342 dw_die_ref ctx = lookup_type_die (context);
23343 if (!ctx)
23344 return NULL;
23345 return strip_naming_typedef (context, ctx);
23347 else
23348 return lookup_decl_die (context);
23350 return comp_unit_die ();
23353 /* Returns the DIE for a context. */
23355 static inline dw_die_ref
23356 get_context_die (tree context)
23358 if (context)
23360 /* Find die that represents this context. */
23361 if (TYPE_P (context))
23363 context = TYPE_MAIN_VARIANT (context);
23364 return strip_naming_typedef (context, force_type_die (context));
23366 else
23367 return force_decl_die (context);
23369 return comp_unit_die ();
23372 /* Returns the DIE for decl. A DIE will always be returned. */
23374 static dw_die_ref
23375 force_decl_die (tree decl)
23377 dw_die_ref decl_die;
23378 unsigned saved_external_flag;
23379 tree save_fn = NULL_TREE;
23380 decl_die = lookup_decl_die (decl);
23381 if (!decl_die)
23383 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23385 decl_die = lookup_decl_die (decl);
23386 if (decl_die)
23387 return decl_die;
23389 switch (TREE_CODE (decl))
23391 case FUNCTION_DECL:
23392 /* Clear current_function_decl, so that gen_subprogram_die thinks
23393 that this is a declaration. At this point, we just want to force
23394 declaration die. */
23395 save_fn = current_function_decl;
23396 current_function_decl = NULL_TREE;
23397 gen_subprogram_die (decl, context_die);
23398 current_function_decl = save_fn;
23399 break;
23401 case VAR_DECL:
23402 /* Set external flag to force declaration die. Restore it after
23403 gen_decl_die() call. */
23404 saved_external_flag = DECL_EXTERNAL (decl);
23405 DECL_EXTERNAL (decl) = 1;
23406 gen_decl_die (decl, NULL, NULL, context_die);
23407 DECL_EXTERNAL (decl) = saved_external_flag;
23408 break;
23410 case NAMESPACE_DECL:
23411 if (dwarf_version >= 3 || !dwarf_strict)
23412 dwarf2out_decl (decl);
23413 else
23414 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
23415 decl_die = comp_unit_die ();
23416 break;
23418 case TRANSLATION_UNIT_DECL:
23419 decl_die = comp_unit_die ();
23420 break;
23422 default:
23423 gcc_unreachable ();
23426 /* We should be able to find the DIE now. */
23427 if (!decl_die)
23428 decl_die = lookup_decl_die (decl);
23429 gcc_assert (decl_die);
23432 return decl_die;
23435 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
23436 always returned. */
23438 static dw_die_ref
23439 force_type_die (tree type)
23441 dw_die_ref type_die;
23443 type_die = lookup_type_die (type);
23444 if (!type_die)
23446 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23448 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23449 false, context_die);
23450 gcc_assert (type_die);
23452 return type_die;
23455 /* Force out any required namespaces to be able to output DECL,
23456 and return the new context_die for it, if it's changed. */
23458 static dw_die_ref
23459 setup_namespace_context (tree thing, dw_die_ref context_die)
23461 tree context = (DECL_P (thing)
23462 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23463 if (context && TREE_CODE (context) == NAMESPACE_DECL)
23464 /* Force out the namespace. */
23465 context_die = force_decl_die (context);
23467 return context_die;
23470 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23471 type) within its namespace, if appropriate.
23473 For compatibility with older debuggers, namespace DIEs only contain
23474 declarations; all definitions are emitted at CU scope, with
23475 DW_AT_specification pointing to the declaration (like with class
23476 members). */
23478 static dw_die_ref
23479 declare_in_namespace (tree thing, dw_die_ref context_die)
23481 dw_die_ref ns_context;
23483 if (debug_info_level <= DINFO_LEVEL_TERSE)
23484 return context_die;
23486 /* External declarations in the local scope only need to be emitted
23487 once, not once in the namespace and once in the scope.
23489 This avoids declaring the `extern' below in the
23490 namespace DIE as well as in the innermost scope:
23492 namespace S
23494 int i=5;
23495 int foo()
23497 int i=8;
23498 extern int i;
23499 return i;
23503 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23504 return context_die;
23506 /* If this decl is from an inlined function, then don't try to emit it in its
23507 namespace, as we will get confused. It would have already been emitted
23508 when the abstract instance of the inline function was emitted anyways. */
23509 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23510 return context_die;
23512 ns_context = setup_namespace_context (thing, context_die);
23514 if (ns_context != context_die)
23516 if (is_fortran ())
23517 return ns_context;
23518 if (DECL_P (thing))
23519 gen_decl_die (thing, NULL, NULL, ns_context);
23520 else
23521 gen_type_die (thing, ns_context);
23523 return context_die;
23526 /* Generate a DIE for a namespace or namespace alias. */
23528 static void
23529 gen_namespace_die (tree decl, dw_die_ref context_die)
23531 dw_die_ref namespace_die;
23533 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23534 they are an alias of. */
23535 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23537 /* Output a real namespace or module. */
23538 context_die = setup_namespace_context (decl, comp_unit_die ());
23539 namespace_die = new_die (is_fortran ()
23540 ? DW_TAG_module : DW_TAG_namespace,
23541 context_die, decl);
23542 /* For Fortran modules defined in different CU don't add src coords. */
23543 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23545 const char *name = dwarf2_name (decl, 0);
23546 if (name)
23547 add_name_attribute (namespace_die, name);
23549 else
23550 add_name_and_src_coords_attributes (namespace_die, decl);
23551 if (DECL_EXTERNAL (decl))
23552 add_AT_flag (namespace_die, DW_AT_declaration, 1);
23553 equate_decl_number_to_die (decl, namespace_die);
23555 else
23557 /* Output a namespace alias. */
23559 /* Force out the namespace we are an alias of, if necessary. */
23560 dw_die_ref origin_die
23561 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23563 if (DECL_FILE_SCOPE_P (decl)
23564 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23565 context_die = setup_namespace_context (decl, comp_unit_die ());
23566 /* Now create the namespace alias DIE. */
23567 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23568 add_name_and_src_coords_attributes (namespace_die, decl);
23569 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23570 equate_decl_number_to_die (decl, namespace_die);
23572 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
23573 if (want_pubnames ())
23574 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23577 /* Generate Dwarf debug information for a decl described by DECL.
23578 The return value is currently only meaningful for PARM_DECLs,
23579 for all other decls it returns NULL.
23581 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23582 It can be NULL otherwise. */
23584 static dw_die_ref
23585 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23586 dw_die_ref context_die)
23588 tree decl_or_origin = decl ? decl : origin;
23589 tree class_origin = NULL, ultimate_origin;
23591 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23592 return NULL;
23594 /* Ignore pointer bounds decls. */
23595 if (DECL_P (decl_or_origin)
23596 && TREE_TYPE (decl_or_origin)
23597 && POINTER_BOUNDS_P (decl_or_origin))
23598 return NULL;
23600 switch (TREE_CODE (decl_or_origin))
23602 case ERROR_MARK:
23603 break;
23605 case CONST_DECL:
23606 if (!is_fortran () && !is_ada ())
23608 /* The individual enumerators of an enum type get output when we output
23609 the Dwarf representation of the relevant enum type itself. */
23610 break;
23613 /* Emit its type. */
23614 gen_type_die (TREE_TYPE (decl), context_die);
23616 /* And its containing namespace. */
23617 context_die = declare_in_namespace (decl, context_die);
23619 gen_const_die (decl, context_die);
23620 break;
23622 case FUNCTION_DECL:
23623 /* Don't output any DIEs to represent mere function declarations,
23624 unless they are class members or explicit block externs. */
23625 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23626 && DECL_FILE_SCOPE_P (decl_or_origin)
23627 && (current_function_decl == NULL_TREE
23628 || DECL_ARTIFICIAL (decl_or_origin)))
23629 break;
23631 #if 0
23632 /* FIXME */
23633 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23634 on local redeclarations of global functions. That seems broken. */
23635 if (current_function_decl != decl)
23636 /* This is only a declaration. */;
23637 #endif
23639 /* If we're emitting a clone, emit info for the abstract instance. */
23640 if (origin || DECL_ORIGIN (decl) != decl)
23641 dwarf2out_abstract_function (origin
23642 ? DECL_ORIGIN (origin)
23643 : DECL_ABSTRACT_ORIGIN (decl));
23645 /* If we're emitting an out-of-line copy of an inline function,
23646 emit info for the abstract instance and set up to refer to it. */
23647 else if (cgraph_function_possibly_inlined_p (decl)
23648 && ! DECL_ABSTRACT_P (decl)
23649 && ! class_or_namespace_scope_p (context_die)
23650 /* dwarf2out_abstract_function won't emit a die if this is just
23651 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
23652 that case, because that works only if we have a die. */
23653 && DECL_INITIAL (decl) != NULL_TREE)
23655 dwarf2out_abstract_function (decl);
23656 set_decl_origin_self (decl);
23659 /* Otherwise we're emitting the primary DIE for this decl. */
23660 else if (debug_info_level > DINFO_LEVEL_TERSE)
23662 /* Before we describe the FUNCTION_DECL itself, make sure that we
23663 have its containing type. */
23664 if (!origin)
23665 origin = decl_class_context (decl);
23666 if (origin != NULL_TREE)
23667 gen_type_die (origin, context_die);
23669 /* And its return type. */
23670 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23672 /* And its virtual context. */
23673 if (DECL_VINDEX (decl) != NULL_TREE)
23674 gen_type_die (DECL_CONTEXT (decl), context_die);
23676 /* Make sure we have a member DIE for decl. */
23677 if (origin != NULL_TREE)
23678 gen_type_die_for_member (origin, decl, context_die);
23680 /* And its containing namespace. */
23681 context_die = declare_in_namespace (decl, context_die);
23684 /* Now output a DIE to represent the function itself. */
23685 if (decl)
23686 gen_subprogram_die (decl, context_die);
23687 break;
23689 case TYPE_DECL:
23690 /* If we are in terse mode, don't generate any DIEs to represent any
23691 actual typedefs. */
23692 if (debug_info_level <= DINFO_LEVEL_TERSE)
23693 break;
23695 /* In the special case of a TYPE_DECL node representing the declaration
23696 of some type tag, if the given TYPE_DECL is marked as having been
23697 instantiated from some other (original) TYPE_DECL node (e.g. one which
23698 was generated within the original definition of an inline function) we
23699 used to generate a special (abbreviated) DW_TAG_structure_type,
23700 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
23701 should be actually referencing those DIEs, as variable DIEs with that
23702 type would be emitted already in the abstract origin, so it was always
23703 removed during unused type prunning. Don't add anything in this
23704 case. */
23705 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23706 break;
23708 if (is_redundant_typedef (decl))
23709 gen_type_die (TREE_TYPE (decl), context_die);
23710 else
23711 /* Output a DIE to represent the typedef itself. */
23712 gen_typedef_die (decl, context_die);
23713 break;
23715 case LABEL_DECL:
23716 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23717 gen_label_die (decl, context_die);
23718 break;
23720 case VAR_DECL:
23721 case RESULT_DECL:
23722 /* If we are in terse mode, don't generate any DIEs to represent any
23723 variable declarations or definitions. */
23724 if (debug_info_level <= DINFO_LEVEL_TERSE)
23725 break;
23727 /* Output any DIEs that are needed to specify the type of this data
23728 object. */
23729 if (decl_by_reference_p (decl_or_origin))
23730 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23731 else
23732 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23734 /* And its containing type. */
23735 class_origin = decl_class_context (decl_or_origin);
23736 if (class_origin != NULL_TREE)
23737 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23739 /* And its containing namespace. */
23740 context_die = declare_in_namespace (decl_or_origin, context_die);
23742 /* Now output the DIE to represent the data object itself. This gets
23743 complicated because of the possibility that the VAR_DECL really
23744 represents an inlined instance of a formal parameter for an inline
23745 function. */
23746 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23747 if (ultimate_origin != NULL_TREE
23748 && TREE_CODE (ultimate_origin) == PARM_DECL)
23749 gen_formal_parameter_die (decl, origin,
23750 true /* Emit name attribute. */,
23751 context_die);
23752 else
23753 gen_variable_die (decl, origin, context_die);
23754 break;
23756 case FIELD_DECL:
23757 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23758 /* Ignore the nameless fields that are used to skip bits but handle C++
23759 anonymous unions and structs. */
23760 if (DECL_NAME (decl) != NULL_TREE
23761 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23762 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23764 gen_type_die (member_declared_type (decl), context_die);
23765 gen_field_die (decl, ctx, context_die);
23767 break;
23769 case PARM_DECL:
23770 if (DECL_BY_REFERENCE (decl_or_origin))
23771 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23772 else
23773 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23774 return gen_formal_parameter_die (decl, origin,
23775 true /* Emit name attribute. */,
23776 context_die);
23778 case NAMESPACE_DECL:
23779 if (dwarf_version >= 3 || !dwarf_strict)
23780 gen_namespace_die (decl, context_die);
23781 break;
23783 case IMPORTED_DECL:
23784 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23785 DECL_CONTEXT (decl), context_die);
23786 break;
23788 case NAMELIST_DECL:
23789 gen_namelist_decl (DECL_NAME (decl), context_die,
23790 NAMELIST_DECL_ASSOCIATED_DECL (decl));
23791 break;
23793 default:
23794 /* Probably some frontend-internal decl. Assume we don't care. */
23795 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23796 break;
23799 return NULL;
23802 /* Output initial debug information for global DECL. Called at the
23803 end of the parsing process.
23805 This is the initial debug generation process. As such, the DIEs
23806 generated may be incomplete. A later debug generation pass
23807 (dwarf2out_late_global_decl) will augment the information generated
23808 in this pass (e.g., with complete location info). */
23810 static void
23811 dwarf2out_early_global_decl (tree decl)
23813 set_early_dwarf s;
23815 /* gen_decl_die() will set DECL_ABSTRACT because
23816 cgraph_function_possibly_inlined_p() returns true. This is in
23817 turn will cause DW_AT_inline attributes to be set.
23819 This happens because at early dwarf generation, there is no
23820 cgraph information, causing cgraph_function_possibly_inlined_p()
23821 to return true. Trick cgraph_function_possibly_inlined_p()
23822 while we generate dwarf early. */
23823 bool save = symtab->global_info_ready;
23824 symtab->global_info_ready = true;
23826 /* We don't handle TYPE_DECLs. If required, they'll be reached via
23827 other DECLs and they can point to template types or other things
23828 that dwarf2out can't handle when done via dwarf2out_decl. */
23829 if (TREE_CODE (decl) != TYPE_DECL
23830 && TREE_CODE (decl) != PARM_DECL)
23832 tree save_fndecl = current_function_decl;
23833 if (TREE_CODE (decl) == FUNCTION_DECL)
23835 /* No cfun means the symbol has no body, so there's nothing
23836 to emit. */
23837 if (!DECL_STRUCT_FUNCTION (decl))
23838 goto early_decl_exit;
23840 current_function_decl = decl;
23842 dwarf2out_decl (decl);
23843 if (TREE_CODE (decl) == FUNCTION_DECL)
23844 current_function_decl = save_fndecl;
23846 early_decl_exit:
23847 symtab->global_info_ready = save;
23850 /* Output debug information for global decl DECL. Called from
23851 toplev.c after compilation proper has finished. */
23853 static void
23854 dwarf2out_late_global_decl (tree decl)
23856 /* Fill-in any location information we were unable to determine
23857 on the first pass. */
23858 if (TREE_CODE (decl) == VAR_DECL
23859 && !POINTER_BOUNDS_P (decl))
23861 dw_die_ref die = lookup_decl_die (decl);
23863 /* We have to generate early debug late for LTO. */
23864 if (! die && in_lto_p)
23866 dwarf2out_decl (decl);
23867 die = lookup_decl_die (decl);
23870 if (die)
23872 /* We get called during the early debug phase via the symtab
23873 code invoking late_global_decl for symbols that are optimized
23874 out. When the early phase is not finished, do not add
23875 locations. */
23876 if (! early_dwarf_finished)
23877 tree_add_const_value_attribute_for_decl (die, decl);
23878 else
23879 add_location_or_const_value_attribute (die, decl, false);
23884 /* Output debug information for type decl DECL. Called from toplev.c
23885 and from language front ends (to record built-in types). */
23886 static void
23887 dwarf2out_type_decl (tree decl, int local)
23889 if (!local)
23891 set_early_dwarf s;
23892 dwarf2out_decl (decl);
23896 /* Output debug information for imported module or decl DECL.
23897 NAME is non-NULL name in the lexical block if the decl has been renamed.
23898 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23899 that DECL belongs to.
23900 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
23901 static void
23902 dwarf2out_imported_module_or_decl_1 (tree decl,
23903 tree name,
23904 tree lexical_block,
23905 dw_die_ref lexical_block_die)
23907 expanded_location xloc;
23908 dw_die_ref imported_die = NULL;
23909 dw_die_ref at_import_die;
23911 if (TREE_CODE (decl) == IMPORTED_DECL)
23913 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23914 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23915 gcc_assert (decl);
23917 else
23918 xloc = expand_location (input_location);
23920 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23922 at_import_die = force_type_die (TREE_TYPE (decl));
23923 /* For namespace N { typedef void T; } using N::T; base_type_die
23924 returns NULL, but DW_TAG_imported_declaration requires
23925 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
23926 if (!at_import_die)
23928 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23929 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23930 at_import_die = lookup_type_die (TREE_TYPE (decl));
23931 gcc_assert (at_import_die);
23934 else
23936 at_import_die = lookup_decl_die (decl);
23937 if (!at_import_die)
23939 /* If we're trying to avoid duplicate debug info, we may not have
23940 emitted the member decl for this field. Emit it now. */
23941 if (TREE_CODE (decl) == FIELD_DECL)
23943 tree type = DECL_CONTEXT (decl);
23945 if (TYPE_CONTEXT (type)
23946 && TYPE_P (TYPE_CONTEXT (type))
23947 && !should_emit_struct_debug (TYPE_CONTEXT (type),
23948 DINFO_USAGE_DIR_USE))
23949 return;
23950 gen_type_die_for_member (type, decl,
23951 get_context_die (TYPE_CONTEXT (type)));
23953 if (TREE_CODE (decl) == NAMELIST_DECL)
23954 at_import_die = gen_namelist_decl (DECL_NAME (decl),
23955 get_context_die (DECL_CONTEXT (decl)),
23956 NULL_TREE);
23957 else
23958 at_import_die = force_decl_die (decl);
23962 if (TREE_CODE (decl) == NAMESPACE_DECL)
23964 if (dwarf_version >= 3 || !dwarf_strict)
23965 imported_die = new_die (DW_TAG_imported_module,
23966 lexical_block_die,
23967 lexical_block);
23968 else
23969 return;
23971 else
23972 imported_die = new_die (DW_TAG_imported_declaration,
23973 lexical_block_die,
23974 lexical_block);
23976 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
23977 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
23978 if (name)
23979 add_AT_string (imported_die, DW_AT_name,
23980 IDENTIFIER_POINTER (name));
23981 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
23984 /* Output debug information for imported module or decl DECL.
23985 NAME is non-NULL name in context if the decl has been renamed.
23986 CHILD is true if decl is one of the renamed decls as part of
23987 importing whole module. */
23989 static void
23990 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
23991 bool child)
23993 /* dw_die_ref at_import_die; */
23994 dw_die_ref scope_die;
23996 if (debug_info_level <= DINFO_LEVEL_TERSE)
23997 return;
23999 gcc_assert (decl);
24001 set_early_dwarf s;
24003 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
24004 We need decl DIE for reference and scope die. First, get DIE for the decl
24005 itself. */
24007 /* Get the scope die for decl context. Use comp_unit_die for global module
24008 or decl. If die is not found for non globals, force new die. */
24009 if (context
24010 && TYPE_P (context)
24011 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
24012 return;
24014 if (!(dwarf_version >= 3 || !dwarf_strict))
24015 return;
24017 scope_die = get_context_die (context);
24019 if (child)
24021 gcc_assert (scope_die->die_child);
24022 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
24023 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
24024 scope_die = scope_die->die_child;
24027 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
24028 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
24031 /* Output debug information for namelists. */
24033 static dw_die_ref
24034 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
24036 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
24037 tree value;
24038 unsigned i;
24040 if (debug_info_level <= DINFO_LEVEL_TERSE)
24041 return NULL;
24043 gcc_assert (scope_die != NULL);
24044 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
24045 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
24047 /* If there are no item_decls, we have a nondefining namelist, e.g.
24048 with USE association; hence, set DW_AT_declaration. */
24049 if (item_decls == NULL_TREE)
24051 add_AT_flag (nml_die, DW_AT_declaration, 1);
24052 return nml_die;
24055 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
24057 nml_item_ref_die = lookup_decl_die (value);
24058 if (!nml_item_ref_die)
24059 nml_item_ref_die = force_decl_die (value);
24061 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
24062 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
24064 return nml_die;
24068 /* Write the debugging output for DECL and return the DIE. */
24070 static void
24071 dwarf2out_decl (tree decl)
24073 dw_die_ref context_die = comp_unit_die ();
24075 switch (TREE_CODE (decl))
24077 case ERROR_MARK:
24078 return;
24080 case FUNCTION_DECL:
24081 /* What we would really like to do here is to filter out all mere
24082 file-scope declarations of file-scope functions which are never
24083 referenced later within this translation unit (and keep all of ones
24084 that *are* referenced later on) but we aren't clairvoyant, so we have
24085 no idea which functions will be referenced in the future (i.e. later
24086 on within the current translation unit). So here we just ignore all
24087 file-scope function declarations which are not also definitions. If
24088 and when the debugger needs to know something about these functions,
24089 it will have to hunt around and find the DWARF information associated
24090 with the definition of the function.
24092 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
24093 nodes represent definitions and which ones represent mere
24094 declarations. We have to check DECL_INITIAL instead. That's because
24095 the C front-end supports some weird semantics for "extern inline"
24096 function definitions. These can get inlined within the current
24097 translation unit (and thus, we need to generate Dwarf info for their
24098 abstract instances so that the Dwarf info for the concrete inlined
24099 instances can have something to refer to) but the compiler never
24100 generates any out-of-lines instances of such things (despite the fact
24101 that they *are* definitions).
24103 The important point is that the C front-end marks these "extern
24104 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
24105 them anyway. Note that the C++ front-end also plays some similar games
24106 for inline function definitions appearing within include files which
24107 also contain `#pragma interface' pragmas.
24109 If we are called from dwarf2out_abstract_function output a DIE
24110 anyway. We can end up here this way with early inlining and LTO
24111 where the inlined function is output in a different LTRANS unit
24112 or not at all. */
24113 if (DECL_INITIAL (decl) == NULL_TREE
24114 && ! DECL_ABSTRACT_P (decl))
24115 return;
24117 /* If we're a nested function, initially use a parent of NULL; if we're
24118 a plain function, this will be fixed up in decls_for_scope. If
24119 we're a method, it will be ignored, since we already have a DIE. */
24120 if (decl_function_context (decl)
24121 /* But if we're in terse mode, we don't care about scope. */
24122 && debug_info_level > DINFO_LEVEL_TERSE)
24123 context_die = NULL;
24124 break;
24126 case VAR_DECL:
24127 /* For local statics lookup proper context die. */
24128 if (local_function_static (decl))
24129 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24131 /* If we are in terse mode, don't generate any DIEs to represent any
24132 variable declarations or definitions. */
24133 if (debug_info_level <= DINFO_LEVEL_TERSE)
24134 return;
24135 break;
24137 case CONST_DECL:
24138 if (debug_info_level <= DINFO_LEVEL_TERSE)
24139 return;
24140 if (!is_fortran () && !is_ada ())
24141 return;
24142 if (TREE_STATIC (decl) && decl_function_context (decl))
24143 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24144 break;
24146 case NAMESPACE_DECL:
24147 case IMPORTED_DECL:
24148 if (debug_info_level <= DINFO_LEVEL_TERSE)
24149 return;
24150 if (lookup_decl_die (decl) != NULL)
24151 return;
24152 break;
24154 case TYPE_DECL:
24155 /* Don't emit stubs for types unless they are needed by other DIEs. */
24156 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
24157 return;
24159 /* Don't bother trying to generate any DIEs to represent any of the
24160 normal built-in types for the language we are compiling. */
24161 if (DECL_IS_BUILTIN (decl))
24162 return;
24164 /* If we are in terse mode, don't generate any DIEs for types. */
24165 if (debug_info_level <= DINFO_LEVEL_TERSE)
24166 return;
24168 /* If we're a function-scope tag, initially use a parent of NULL;
24169 this will be fixed up in decls_for_scope. */
24170 if (decl_function_context (decl))
24171 context_die = NULL;
24173 break;
24175 case NAMELIST_DECL:
24176 break;
24178 default:
24179 return;
24182 gen_decl_die (decl, NULL, NULL, context_die);
24184 if (flag_checking)
24186 dw_die_ref die = lookup_decl_die (decl);
24187 if (die)
24188 check_die (die);
24192 /* Write the debugging output for DECL. */
24194 static void
24195 dwarf2out_function_decl (tree decl)
24197 dwarf2out_decl (decl);
24198 call_arg_locations = NULL;
24199 call_arg_loc_last = NULL;
24200 call_site_count = -1;
24201 tail_call_site_count = -1;
24202 decl_loc_table->empty ();
24203 cached_dw_loc_list_table->empty ();
24206 /* Output a marker (i.e. a label) for the beginning of the generated code for
24207 a lexical block. */
24209 static void
24210 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
24211 unsigned int blocknum)
24213 switch_to_section (current_function_section ());
24214 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
24217 /* Output a marker (i.e. a label) for the end of the generated code for a
24218 lexical block. */
24220 static void
24221 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
24223 switch_to_section (current_function_section ());
24224 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
24227 /* Returns nonzero if it is appropriate not to emit any debugging
24228 information for BLOCK, because it doesn't contain any instructions.
24230 Don't allow this for blocks with nested functions or local classes
24231 as we would end up with orphans, and in the presence of scheduling
24232 we may end up calling them anyway. */
24234 static bool
24235 dwarf2out_ignore_block (const_tree block)
24237 tree decl;
24238 unsigned int i;
24240 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24241 if (TREE_CODE (decl) == FUNCTION_DECL
24242 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24243 return 0;
24244 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24246 decl = BLOCK_NONLOCALIZED_VAR (block, i);
24247 if (TREE_CODE (decl) == FUNCTION_DECL
24248 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24249 return 0;
24252 return 1;
24255 /* Hash table routines for file_hash. */
24257 bool
24258 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24260 return filename_cmp (p1->filename, p2) == 0;
24263 hashval_t
24264 dwarf_file_hasher::hash (dwarf_file_data *p)
24266 return htab_hash_string (p->filename);
24269 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24270 dwarf2out.c) and return its "index". The index of each (known) filename is
24271 just a unique number which is associated with only that one filename. We
24272 need such numbers for the sake of generating labels (in the .debug_sfnames
24273 section) and references to those files numbers (in the .debug_srcinfo
24274 and .debug_macinfo sections). If the filename given as an argument is not
24275 found in our current list, add it to the list and assign it the next
24276 available unique index number. */
24278 static struct dwarf_file_data *
24279 lookup_filename (const char *file_name)
24281 struct dwarf_file_data * created;
24283 if (!file_name)
24284 return NULL;
24286 dwarf_file_data **slot
24287 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24288 INSERT);
24289 if (*slot)
24290 return *slot;
24292 created = ggc_alloc<dwarf_file_data> ();
24293 created->filename = file_name;
24294 created->emitted_number = 0;
24295 *slot = created;
24296 return created;
24299 /* If the assembler will construct the file table, then translate the compiler
24300 internal file table number into the assembler file table number, and emit
24301 a .file directive if we haven't already emitted one yet. The file table
24302 numbers are different because we prune debug info for unused variables and
24303 types, which may include filenames. */
24305 static int
24306 maybe_emit_file (struct dwarf_file_data * fd)
24308 if (! fd->emitted_number)
24310 if (last_emitted_file)
24311 fd->emitted_number = last_emitted_file->emitted_number + 1;
24312 else
24313 fd->emitted_number = 1;
24314 last_emitted_file = fd;
24316 if (DWARF2_ASM_LINE_DEBUG_INFO)
24318 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24319 output_quoted_string (asm_out_file,
24320 remap_debug_filename (fd->filename));
24321 fputc ('\n', asm_out_file);
24325 return fd->emitted_number;
24328 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24329 That generation should happen after function debug info has been
24330 generated. The value of the attribute is the constant value of ARG. */
24332 static void
24333 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24335 die_arg_entry entry;
24337 if (!die || !arg)
24338 return;
24340 gcc_assert (early_dwarf);
24342 if (!tmpl_value_parm_die_table)
24343 vec_alloc (tmpl_value_parm_die_table, 32);
24345 entry.die = die;
24346 entry.arg = arg;
24347 vec_safe_push (tmpl_value_parm_die_table, entry);
24350 /* Return TRUE if T is an instance of generic type, FALSE
24351 otherwise. */
24353 static bool
24354 generic_type_p (tree t)
24356 if (t == NULL_TREE || !TYPE_P (t))
24357 return false;
24358 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24361 /* Schedule the generation of the generic parameter dies for the
24362 instance of generic type T. The proper generation itself is later
24363 done by gen_scheduled_generic_parms_dies. */
24365 static void
24366 schedule_generic_params_dies_gen (tree t)
24368 if (!generic_type_p (t))
24369 return;
24371 gcc_assert (early_dwarf);
24373 if (!generic_type_instances)
24374 vec_alloc (generic_type_instances, 256);
24376 vec_safe_push (generic_type_instances, t);
24379 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24380 by append_entry_to_tmpl_value_parm_die_table. This function must
24381 be called after function DIEs have been generated. */
24383 static void
24384 gen_remaining_tmpl_value_param_die_attribute (void)
24386 if (tmpl_value_parm_die_table)
24388 unsigned i, j;
24389 die_arg_entry *e;
24391 /* We do this in two phases - first get the cases we can
24392 handle during early-finish, preserving those we cannot
24393 (containing symbolic constants where we don't yet know
24394 whether we are going to output the referenced symbols).
24395 For those we try again at late-finish. */
24396 j = 0;
24397 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24399 if (!tree_add_const_value_attribute (e->die, e->arg))
24401 dw_loc_descr_ref loc = NULL;
24402 if (! early_dwarf
24403 && (dwarf_version >= 5 || !dwarf_strict))
24404 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
24405 if (loc)
24406 add_AT_loc (e->die, DW_AT_location, loc);
24407 else
24408 (*tmpl_value_parm_die_table)[j++] = *e;
24411 tmpl_value_parm_die_table->truncate (j);
24415 /* Generate generic parameters DIEs for instances of generic types
24416 that have been previously scheduled by
24417 schedule_generic_params_dies_gen. This function must be called
24418 after all the types of the CU have been laid out. */
24420 static void
24421 gen_scheduled_generic_parms_dies (void)
24423 unsigned i;
24424 tree t;
24426 if (!generic_type_instances)
24427 return;
24429 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24430 if (COMPLETE_TYPE_P (t))
24431 gen_generic_params_dies (t);
24433 generic_type_instances = NULL;
24437 /* Replace DW_AT_name for the decl with name. */
24439 static void
24440 dwarf2out_set_name (tree decl, tree name)
24442 dw_die_ref die;
24443 dw_attr_node *attr;
24444 const char *dname;
24446 die = TYPE_SYMTAB_DIE (decl);
24447 if (!die)
24448 return;
24450 dname = dwarf2_name (name, 0);
24451 if (!dname)
24452 return;
24454 attr = get_AT (die, DW_AT_name);
24455 if (attr)
24457 struct indirect_string_node *node;
24459 node = find_AT_string (dname);
24460 /* replace the string. */
24461 attr->dw_attr_val.v.val_str = node;
24464 else
24465 add_name_attribute (die, dname);
24468 /* True if before or during processing of the first function being emitted. */
24469 static bool in_first_function_p = true;
24470 /* True if loc_note during dwarf2out_var_location call might still be
24471 before first real instruction at address equal to .Ltext0. */
24472 static bool maybe_at_text_label_p = true;
24473 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
24474 static unsigned int first_loclabel_num_not_at_text_label;
24476 /* Called by the final INSN scan whenever we see a var location. We
24477 use it to drop labels in the right places, and throw the location in
24478 our lookup table. */
24480 static void
24481 dwarf2out_var_location (rtx_insn *loc_note)
24483 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24484 struct var_loc_node *newloc;
24485 rtx_insn *next_real, *next_note;
24486 rtx_insn *call_insn = NULL;
24487 static const char *last_label;
24488 static const char *last_postcall_label;
24489 static bool last_in_cold_section_p;
24490 static rtx_insn *expected_next_loc_note;
24491 tree decl;
24492 bool var_loc_p;
24494 if (!NOTE_P (loc_note))
24496 if (CALL_P (loc_note))
24498 call_site_count++;
24499 if (SIBLING_CALL_P (loc_note))
24500 tail_call_site_count++;
24501 if (optimize == 0 && !flag_var_tracking)
24503 /* When the var-tracking pass is not running, there is no note
24504 for indirect calls whose target is compile-time known. In this
24505 case, process such calls specifically so that we generate call
24506 sites for them anyway. */
24507 rtx x = PATTERN (loc_note);
24508 if (GET_CODE (x) == PARALLEL)
24509 x = XVECEXP (x, 0, 0);
24510 if (GET_CODE (x) == SET)
24511 x = SET_SRC (x);
24512 if (GET_CODE (x) == CALL)
24513 x = XEXP (x, 0);
24514 if (!MEM_P (x)
24515 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24516 || !SYMBOL_REF_DECL (XEXP (x, 0))
24517 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24518 != FUNCTION_DECL))
24520 call_insn = loc_note;
24521 loc_note = NULL;
24522 var_loc_p = false;
24524 next_real = next_real_insn (call_insn);
24525 next_note = NULL;
24526 cached_next_real_insn = NULL;
24527 goto create_label;
24531 return;
24534 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24535 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24536 return;
24538 /* Optimize processing a large consecutive sequence of location
24539 notes so we don't spend too much time in next_real_insn. If the
24540 next insn is another location note, remember the next_real_insn
24541 calculation for next time. */
24542 next_real = cached_next_real_insn;
24543 if (next_real)
24545 if (expected_next_loc_note != loc_note)
24546 next_real = NULL;
24549 next_note = NEXT_INSN (loc_note);
24550 if (! next_note
24551 || next_note->deleted ()
24552 || ! NOTE_P (next_note)
24553 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24554 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24555 next_note = NULL;
24557 if (! next_real)
24558 next_real = next_real_insn (loc_note);
24560 if (next_note)
24562 expected_next_loc_note = next_note;
24563 cached_next_real_insn = next_real;
24565 else
24566 cached_next_real_insn = NULL;
24568 /* If there are no instructions which would be affected by this note,
24569 don't do anything. */
24570 if (var_loc_p
24571 && next_real == NULL_RTX
24572 && !NOTE_DURING_CALL_P (loc_note))
24573 return;
24575 create_label:
24577 if (next_real == NULL_RTX)
24578 next_real = get_last_insn ();
24580 /* If there were any real insns between note we processed last time
24581 and this note (or if it is the first note), clear
24582 last_{,postcall_}label so that they are not reused this time. */
24583 if (last_var_location_insn == NULL_RTX
24584 || last_var_location_insn != next_real
24585 || last_in_cold_section_p != in_cold_section_p)
24587 last_label = NULL;
24588 last_postcall_label = NULL;
24591 if (var_loc_p)
24593 decl = NOTE_VAR_LOCATION_DECL (loc_note);
24594 newloc = add_var_loc_to_decl (decl, loc_note,
24595 NOTE_DURING_CALL_P (loc_note)
24596 ? last_postcall_label : last_label);
24597 if (newloc == NULL)
24598 return;
24600 else
24602 decl = NULL_TREE;
24603 newloc = NULL;
24606 /* If there were no real insns between note we processed last time
24607 and this note, use the label we emitted last time. Otherwise
24608 create a new label and emit it. */
24609 if (last_label == NULL)
24611 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24612 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24613 loclabel_num++;
24614 last_label = ggc_strdup (loclabel);
24615 /* See if loclabel might be equal to .Ltext0. If yes,
24616 bump first_loclabel_num_not_at_text_label. */
24617 if (!have_multiple_function_sections
24618 && in_first_function_p
24619 && maybe_at_text_label_p)
24621 static rtx_insn *last_start;
24622 rtx_insn *insn;
24623 for (insn = loc_note; insn; insn = previous_insn (insn))
24624 if (insn == last_start)
24625 break;
24626 else if (!NONDEBUG_INSN_P (insn))
24627 continue;
24628 else
24630 rtx body = PATTERN (insn);
24631 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24632 continue;
24633 /* Inline asm could occupy zero bytes. */
24634 else if (GET_CODE (body) == ASM_INPUT
24635 || asm_noperands (body) >= 0)
24636 continue;
24637 #ifdef HAVE_attr_length
24638 else if (get_attr_min_length (insn) == 0)
24639 continue;
24640 #endif
24641 else
24643 /* Assume insn has non-zero length. */
24644 maybe_at_text_label_p = false;
24645 break;
24648 if (maybe_at_text_label_p)
24650 last_start = loc_note;
24651 first_loclabel_num_not_at_text_label = loclabel_num;
24656 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24657 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24659 if (!var_loc_p)
24661 struct call_arg_loc_node *ca_loc
24662 = ggc_cleared_alloc<call_arg_loc_node> ();
24663 rtx_insn *prev
24664 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24666 ca_loc->call_arg_loc_note = loc_note;
24667 ca_loc->next = NULL;
24668 ca_loc->label = last_label;
24669 gcc_assert (prev
24670 && (CALL_P (prev)
24671 || (NONJUMP_INSN_P (prev)
24672 && GET_CODE (PATTERN (prev)) == SEQUENCE
24673 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24674 if (!CALL_P (prev))
24675 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24676 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24678 /* Look for a SYMBOL_REF in the "prev" instruction. */
24679 rtx x = get_call_rtx_from (PATTERN (prev));
24680 if (x)
24682 /* Try to get the call symbol, if any. */
24683 if (MEM_P (XEXP (x, 0)))
24684 x = XEXP (x, 0);
24685 /* First, look for a memory access to a symbol_ref. */
24686 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24687 && SYMBOL_REF_DECL (XEXP (x, 0))
24688 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24689 ca_loc->symbol_ref = XEXP (x, 0);
24690 /* Otherwise, look at a compile-time known user-level function
24691 declaration. */
24692 else if (MEM_P (x)
24693 && MEM_EXPR (x)
24694 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24695 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24698 ca_loc->block = insn_scope (prev);
24699 if (call_arg_locations)
24700 call_arg_loc_last->next = ca_loc;
24701 else
24702 call_arg_locations = ca_loc;
24703 call_arg_loc_last = ca_loc;
24705 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24706 newloc->label = last_label;
24707 else
24709 if (!last_postcall_label)
24711 sprintf (loclabel, "%s-1", last_label);
24712 last_postcall_label = ggc_strdup (loclabel);
24714 newloc->label = last_postcall_label;
24717 last_var_location_insn = next_real;
24718 last_in_cold_section_p = in_cold_section_p;
24721 /* Called from finalize_size_functions for size functions so that their body
24722 can be encoded in the debug info to describe the layout of variable-length
24723 structures. */
24725 static void
24726 dwarf2out_size_function (tree decl)
24728 function_to_dwarf_procedure (decl);
24731 /* Note in one location list that text section has changed. */
24734 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24736 var_loc_list *list = *slot;
24737 if (list->first)
24738 list->last_before_switch
24739 = list->last->next ? list->last->next : list->last;
24740 return 1;
24743 /* Note in all location lists that text section has changed. */
24745 static void
24746 var_location_switch_text_section (void)
24748 if (decl_loc_table == NULL)
24749 return;
24751 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24754 /* Create a new line number table. */
24756 static dw_line_info_table *
24757 new_line_info_table (void)
24759 dw_line_info_table *table;
24761 table = ggc_cleared_alloc<dw_line_info_table> ();
24762 table->file_num = 1;
24763 table->line_num = 1;
24764 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24766 return table;
24769 /* Lookup the "current" table into which we emit line info, so
24770 that we don't have to do it for every source line. */
24772 static void
24773 set_cur_line_info_table (section *sec)
24775 dw_line_info_table *table;
24777 if (sec == text_section)
24778 table = text_section_line_info;
24779 else if (sec == cold_text_section)
24781 table = cold_text_section_line_info;
24782 if (!table)
24784 cold_text_section_line_info = table = new_line_info_table ();
24785 table->end_label = cold_end_label;
24788 else
24790 const char *end_label;
24792 if (flag_reorder_blocks_and_partition)
24794 if (in_cold_section_p)
24795 end_label = crtl->subsections.cold_section_end_label;
24796 else
24797 end_label = crtl->subsections.hot_section_end_label;
24799 else
24801 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24802 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24803 current_function_funcdef_no);
24804 end_label = ggc_strdup (label);
24807 table = new_line_info_table ();
24808 table->end_label = end_label;
24810 vec_safe_push (separate_line_info, table);
24813 if (DWARF2_ASM_LINE_DEBUG_INFO)
24814 table->is_stmt = (cur_line_info_table
24815 ? cur_line_info_table->is_stmt
24816 : DWARF_LINE_DEFAULT_IS_STMT_START);
24817 cur_line_info_table = table;
24821 /* We need to reset the locations at the beginning of each
24822 function. We can't do this in the end_function hook, because the
24823 declarations that use the locations won't have been output when
24824 that hook is called. Also compute have_multiple_function_sections here. */
24826 static void
24827 dwarf2out_begin_function (tree fun)
24829 section *sec = function_section (fun);
24831 if (sec != text_section)
24832 have_multiple_function_sections = true;
24834 if (flag_reorder_blocks_and_partition && !cold_text_section)
24836 gcc_assert (current_function_decl == fun);
24837 cold_text_section = unlikely_text_section ();
24838 switch_to_section (cold_text_section);
24839 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24840 switch_to_section (sec);
24843 dwarf2out_note_section_used ();
24844 call_site_count = 0;
24845 tail_call_site_count = 0;
24847 set_cur_line_info_table (sec);
24850 /* Helper function of dwarf2out_end_function, called only after emitting
24851 the very first function into assembly. Check if some .debug_loc range
24852 might end with a .LVL* label that could be equal to .Ltext0.
24853 In that case we must force using absolute addresses in .debug_loc ranges,
24854 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24855 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24856 list terminator.
24857 Set have_multiple_function_sections to true in that case and
24858 terminate htab traversal. */
24861 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24863 var_loc_list *entry = *slot;
24864 struct var_loc_node *node;
24866 node = entry->first;
24867 if (node && node->next && node->next->label)
24869 unsigned int i;
24870 const char *label = node->next->label;
24871 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24873 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24875 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24876 if (strcmp (label, loclabel) == 0)
24878 have_multiple_function_sections = true;
24879 return 0;
24883 return 1;
24886 /* Hook called after emitting a function into assembly.
24887 This does something only for the very first function emitted. */
24889 static void
24890 dwarf2out_end_function (unsigned int)
24892 if (in_first_function_p
24893 && !have_multiple_function_sections
24894 && first_loclabel_num_not_at_text_label
24895 && decl_loc_table)
24896 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24897 in_first_function_p = false;
24898 maybe_at_text_label_p = false;
24901 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
24902 front-ends register a translation unit even before dwarf2out_init is
24903 called. */
24904 static tree main_translation_unit = NULL_TREE;
24906 /* Hook called by front-ends after they built their main translation unit.
24907 Associate comp_unit_die to UNIT. */
24909 static void
24910 dwarf2out_register_main_translation_unit (tree unit)
24912 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24913 && main_translation_unit == NULL_TREE);
24914 main_translation_unit = unit;
24915 /* If dwarf2out_init has not been called yet, it will perform the association
24916 itself looking at main_translation_unit. */
24917 if (decl_die_table != NULL)
24918 equate_decl_number_to_die (unit, comp_unit_die ());
24921 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
24923 static void
24924 push_dw_line_info_entry (dw_line_info_table *table,
24925 enum dw_line_info_opcode opcode, unsigned int val)
24927 dw_line_info_entry e;
24928 e.opcode = opcode;
24929 e.val = val;
24930 vec_safe_push (table->entries, e);
24933 /* Output a label to mark the beginning of a source code line entry
24934 and record information relating to this source line, in
24935 'line_info_table' for later output of the .debug_line section. */
24936 /* ??? The discriminator parameter ought to be unsigned. */
24938 static void
24939 dwarf2out_source_line (unsigned int line, const char *filename,
24940 int discriminator, bool is_stmt)
24942 unsigned int file_num;
24943 dw_line_info_table *table;
24945 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
24946 return;
24948 /* The discriminator column was added in dwarf4. Simplify the below
24949 by simply removing it if we're not supposed to output it. */
24950 if (dwarf_version < 4 && dwarf_strict)
24951 discriminator = 0;
24953 table = cur_line_info_table;
24954 file_num = maybe_emit_file (lookup_filename (filename));
24956 /* ??? TODO: Elide duplicate line number entries. Traditionally,
24957 the debugger has used the second (possibly duplicate) line number
24958 at the beginning of the function to mark the end of the prologue.
24959 We could eliminate any other duplicates within the function. For
24960 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
24961 that second line number entry. */
24962 /* Recall that this end-of-prologue indication is *not* the same thing
24963 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
24964 to which the hook corresponds, follows the last insn that was
24965 emitted by gen_prologue. What we need is to precede the first insn
24966 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
24967 insn that corresponds to something the user wrote. These may be
24968 very different locations once scheduling is enabled. */
24970 if (0 && file_num == table->file_num
24971 && line == table->line_num
24972 && discriminator == table->discrim_num
24973 && is_stmt == table->is_stmt)
24974 return;
24976 switch_to_section (current_function_section ());
24978 /* If requested, emit something human-readable. */
24979 if (flag_debug_asm)
24980 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
24982 if (DWARF2_ASM_LINE_DEBUG_INFO)
24984 /* Emit the .loc directive understood by GNU as. */
24985 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
24986 file_num, line, is_stmt, discriminator */
24987 fputs ("\t.loc ", asm_out_file);
24988 fprint_ul (asm_out_file, file_num);
24989 putc (' ', asm_out_file);
24990 fprint_ul (asm_out_file, line);
24991 putc (' ', asm_out_file);
24992 putc ('0', asm_out_file);
24994 if (is_stmt != table->is_stmt)
24996 fputs (" is_stmt ", asm_out_file);
24997 putc (is_stmt ? '1' : '0', asm_out_file);
24999 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
25001 gcc_assert (discriminator > 0);
25002 fputs (" discriminator ", asm_out_file);
25003 fprint_ul (asm_out_file, (unsigned long) discriminator);
25005 putc ('\n', asm_out_file);
25007 else
25009 unsigned int label_num = ++line_info_label_num;
25011 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
25013 push_dw_line_info_entry (table, LI_set_address, label_num);
25014 if (file_num != table->file_num)
25015 push_dw_line_info_entry (table, LI_set_file, file_num);
25016 if (discriminator != table->discrim_num)
25017 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
25018 if (is_stmt != table->is_stmt)
25019 push_dw_line_info_entry (table, LI_negate_stmt, 0);
25020 push_dw_line_info_entry (table, LI_set_line, line);
25023 table->file_num = file_num;
25024 table->line_num = line;
25025 table->discrim_num = discriminator;
25026 table->is_stmt = is_stmt;
25027 table->in_use = true;
25030 /* Record the beginning of a new source file. */
25032 static void
25033 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
25035 if (flag_eliminate_dwarf2_dups)
25037 /* Record the beginning of the file for break_out_includes. */
25038 dw_die_ref bincl_die;
25040 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
25041 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
25044 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25046 macinfo_entry e;
25047 e.code = DW_MACINFO_start_file;
25048 e.lineno = lineno;
25049 e.info = ggc_strdup (filename);
25050 vec_safe_push (macinfo_table, e);
25054 /* Record the end of a source file. */
25056 static void
25057 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
25059 if (flag_eliminate_dwarf2_dups)
25060 /* Record the end of the file for break_out_includes. */
25061 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
25063 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25065 macinfo_entry e;
25066 e.code = DW_MACINFO_end_file;
25067 e.lineno = lineno;
25068 e.info = NULL;
25069 vec_safe_push (macinfo_table, e);
25073 /* Called from debug_define in toplev.c. The `buffer' parameter contains
25074 the tail part of the directive line, i.e. the part which is past the
25075 initial whitespace, #, whitespace, directive-name, whitespace part. */
25077 static void
25078 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
25079 const char *buffer ATTRIBUTE_UNUSED)
25081 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25083 macinfo_entry e;
25084 /* Insert a dummy first entry to be able to optimize the whole
25085 predefined macro block using DW_MACRO_GNU_transparent_include. */
25086 if (macinfo_table->is_empty () && lineno <= 1)
25088 e.code = 0;
25089 e.lineno = 0;
25090 e.info = NULL;
25091 vec_safe_push (macinfo_table, e);
25093 e.code = DW_MACINFO_define;
25094 e.lineno = lineno;
25095 e.info = ggc_strdup (buffer);
25096 vec_safe_push (macinfo_table, e);
25100 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
25101 the tail part of the directive line, i.e. the part which is past the
25102 initial whitespace, #, whitespace, directive-name, whitespace part. */
25104 static void
25105 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
25106 const char *buffer ATTRIBUTE_UNUSED)
25108 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25110 macinfo_entry e;
25111 /* Insert a dummy first entry to be able to optimize the whole
25112 predefined macro block using DW_MACRO_GNU_transparent_include. */
25113 if (macinfo_table->is_empty () && lineno <= 1)
25115 e.code = 0;
25116 e.lineno = 0;
25117 e.info = NULL;
25118 vec_safe_push (macinfo_table, e);
25120 e.code = DW_MACINFO_undef;
25121 e.lineno = lineno;
25122 e.info = ggc_strdup (buffer);
25123 vec_safe_push (macinfo_table, e);
25127 /* Helpers to manipulate hash table of CUs. */
25129 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
25131 static inline hashval_t hash (const macinfo_entry *);
25132 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
25135 inline hashval_t
25136 macinfo_entry_hasher::hash (const macinfo_entry *entry)
25138 return htab_hash_string (entry->info);
25141 inline bool
25142 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
25143 const macinfo_entry *entry2)
25145 return !strcmp (entry1->info, entry2->info);
25148 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
25150 /* Output a single .debug_macinfo entry. */
25152 static void
25153 output_macinfo_op (macinfo_entry *ref)
25155 int file_num;
25156 size_t len;
25157 struct indirect_string_node *node;
25158 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25159 struct dwarf_file_data *fd;
25161 switch (ref->code)
25163 case DW_MACINFO_start_file:
25164 fd = lookup_filename (ref->info);
25165 file_num = maybe_emit_file (fd);
25166 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
25167 dw2_asm_output_data_uleb128 (ref->lineno,
25168 "Included from line number %lu",
25169 (unsigned long) ref->lineno);
25170 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
25171 break;
25172 case DW_MACINFO_end_file:
25173 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
25174 break;
25175 case DW_MACINFO_define:
25176 case DW_MACINFO_undef:
25177 len = strlen (ref->info) + 1;
25178 if (!dwarf_strict
25179 && len > DWARF_OFFSET_SIZE
25180 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25181 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25183 ref->code = ref->code == DW_MACINFO_define
25184 ? DW_MACRO_GNU_define_indirect
25185 : DW_MACRO_GNU_undef_indirect;
25186 output_macinfo_op (ref);
25187 return;
25189 dw2_asm_output_data (1, ref->code,
25190 ref->code == DW_MACINFO_define
25191 ? "Define macro" : "Undefine macro");
25192 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25193 (unsigned long) ref->lineno);
25194 dw2_asm_output_nstring (ref->info, -1, "The macro");
25195 break;
25196 case DW_MACRO_GNU_define_indirect:
25197 case DW_MACRO_GNU_undef_indirect:
25198 node = find_AT_string (ref->info);
25199 gcc_assert (node
25200 && ((node->form == DW_FORM_strp)
25201 || (node->form == DW_FORM_GNU_str_index)));
25202 dw2_asm_output_data (1, ref->code,
25203 ref->code == DW_MACRO_GNU_define_indirect
25204 ? "Define macro indirect"
25205 : "Undefine macro indirect");
25206 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25207 (unsigned long) ref->lineno);
25208 if (node->form == DW_FORM_strp)
25209 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
25210 debug_str_section, "The macro: \"%s\"",
25211 ref->info);
25212 else
25213 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
25214 ref->info);
25215 break;
25216 case DW_MACRO_GNU_transparent_include:
25217 dw2_asm_output_data (1, ref->code, "Transparent include");
25218 ASM_GENERATE_INTERNAL_LABEL (label,
25219 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
25220 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
25221 break;
25222 default:
25223 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
25224 ASM_COMMENT_START, (unsigned long) ref->code);
25225 break;
25229 /* Attempt to make a sequence of define/undef macinfo ops shareable with
25230 other compilation unit .debug_macinfo sections. IDX is the first
25231 index of a define/undef, return the number of ops that should be
25232 emitted in a comdat .debug_macinfo section and emit
25233 a DW_MACRO_GNU_transparent_include entry referencing it.
25234 If the define/undef entry should be emitted normally, return 0. */
25236 static unsigned
25237 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25238 macinfo_hash_type **macinfo_htab)
25240 macinfo_entry *first, *second, *cur, *inc;
25241 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25242 unsigned char checksum[16];
25243 struct md5_ctx ctx;
25244 char *grp_name, *tail;
25245 const char *base;
25246 unsigned int i, count, encoded_filename_len, linebuf_len;
25247 macinfo_entry **slot;
25249 first = &(*macinfo_table)[idx];
25250 second = &(*macinfo_table)[idx + 1];
25252 /* Optimize only if there are at least two consecutive define/undef ops,
25253 and either all of them are before first DW_MACINFO_start_file
25254 with lineno {0,1} (i.e. predefined macro block), or all of them are
25255 in some included header file. */
25256 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25257 return 0;
25258 if (vec_safe_is_empty (files))
25260 if (first->lineno > 1 || second->lineno > 1)
25261 return 0;
25263 else if (first->lineno == 0)
25264 return 0;
25266 /* Find the last define/undef entry that can be grouped together
25267 with first and at the same time compute md5 checksum of their
25268 codes, linenumbers and strings. */
25269 md5_init_ctx (&ctx);
25270 for (i = idx; macinfo_table->iterate (i, &cur); i++)
25271 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25272 break;
25273 else if (vec_safe_is_empty (files) && cur->lineno > 1)
25274 break;
25275 else
25277 unsigned char code = cur->code;
25278 md5_process_bytes (&code, 1, &ctx);
25279 checksum_uleb128 (cur->lineno, &ctx);
25280 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25282 md5_finish_ctx (&ctx, checksum);
25283 count = i - idx;
25285 /* From the containing include filename (if any) pick up just
25286 usable characters from its basename. */
25287 if (vec_safe_is_empty (files))
25288 base = "";
25289 else
25290 base = lbasename (files->last ().info);
25291 for (encoded_filename_len = 0, i = 0; base[i]; i++)
25292 if (ISIDNUM (base[i]) || base[i] == '.')
25293 encoded_filename_len++;
25294 /* Count . at the end. */
25295 if (encoded_filename_len)
25296 encoded_filename_len++;
25298 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25299 linebuf_len = strlen (linebuf);
25301 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
25302 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25303 + 16 * 2 + 1);
25304 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25305 tail = grp_name + 4;
25306 if (encoded_filename_len)
25308 for (i = 0; base[i]; i++)
25309 if (ISIDNUM (base[i]) || base[i] == '.')
25310 *tail++ = base[i];
25311 *tail++ = '.';
25313 memcpy (tail, linebuf, linebuf_len);
25314 tail += linebuf_len;
25315 *tail++ = '.';
25316 for (i = 0; i < 16; i++)
25317 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25319 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25320 in the empty vector entry before the first define/undef. */
25321 inc = &(*macinfo_table)[idx - 1];
25322 inc->code = DW_MACRO_GNU_transparent_include;
25323 inc->lineno = 0;
25324 inc->info = ggc_strdup (grp_name);
25325 if (!*macinfo_htab)
25326 *macinfo_htab = new macinfo_hash_type (10);
25327 /* Avoid emitting duplicates. */
25328 slot = (*macinfo_htab)->find_slot (inc, INSERT);
25329 if (*slot != NULL)
25331 inc->code = 0;
25332 inc->info = NULL;
25333 /* If such an entry has been used before, just emit
25334 a DW_MACRO_GNU_transparent_include op. */
25335 inc = *slot;
25336 output_macinfo_op (inc);
25337 /* And clear all macinfo_entry in the range to avoid emitting them
25338 in the second pass. */
25339 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25341 cur->code = 0;
25342 cur->info = NULL;
25345 else
25347 *slot = inc;
25348 inc->lineno = (*macinfo_htab)->elements ();
25349 output_macinfo_op (inc);
25351 return count;
25354 /* Save any strings needed by the macinfo table in the debug str
25355 table. All strings must be collected into the table by the time
25356 index_string is called. */
25358 static void
25359 save_macinfo_strings (void)
25361 unsigned len;
25362 unsigned i;
25363 macinfo_entry *ref;
25365 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25367 switch (ref->code)
25369 /* Match the logic in output_macinfo_op to decide on
25370 indirect strings. */
25371 case DW_MACINFO_define:
25372 case DW_MACINFO_undef:
25373 len = strlen (ref->info) + 1;
25374 if (!dwarf_strict
25375 && len > DWARF_OFFSET_SIZE
25376 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25377 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25378 set_indirect_string (find_AT_string (ref->info));
25379 break;
25380 case DW_MACRO_GNU_define_indirect:
25381 case DW_MACRO_GNU_undef_indirect:
25382 set_indirect_string (find_AT_string (ref->info));
25383 break;
25384 default:
25385 break;
25390 /* Output macinfo section(s). */
25392 static void
25393 output_macinfo (void)
25395 unsigned i;
25396 unsigned long length = vec_safe_length (macinfo_table);
25397 macinfo_entry *ref;
25398 vec<macinfo_entry, va_gc> *files = NULL;
25399 macinfo_hash_type *macinfo_htab = NULL;
25401 if (! length)
25402 return;
25404 /* output_macinfo* uses these interchangeably. */
25405 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25406 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25407 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25408 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25410 /* For .debug_macro emit the section header. */
25411 if (!dwarf_strict)
25413 dw2_asm_output_data (2, 4, "DWARF macro version number");
25414 if (DWARF_OFFSET_SIZE == 8)
25415 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25416 else
25417 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25418 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25419 (!dwarf_split_debug_info ? debug_line_section_label
25420 : debug_skeleton_line_section_label),
25421 debug_line_section, NULL);
25424 /* In the first loop, it emits the primary .debug_macinfo section
25425 and after each emitted op the macinfo_entry is cleared.
25426 If a longer range of define/undef ops can be optimized using
25427 DW_MACRO_GNU_transparent_include, the
25428 DW_MACRO_GNU_transparent_include op is emitted and kept in
25429 the vector before the first define/undef in the range and the
25430 whole range of define/undef ops is not emitted and kept. */
25431 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25433 switch (ref->code)
25435 case DW_MACINFO_start_file:
25436 vec_safe_push (files, *ref);
25437 break;
25438 case DW_MACINFO_end_file:
25439 if (!vec_safe_is_empty (files))
25440 files->pop ();
25441 break;
25442 case DW_MACINFO_define:
25443 case DW_MACINFO_undef:
25444 if (!dwarf_strict
25445 && HAVE_COMDAT_GROUP
25446 && vec_safe_length (files) != 1
25447 && i > 0
25448 && i + 1 < length
25449 && (*macinfo_table)[i - 1].code == 0)
25451 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25452 if (count)
25454 i += count - 1;
25455 continue;
25458 break;
25459 case 0:
25460 /* A dummy entry may be inserted at the beginning to be able
25461 to optimize the whole block of predefined macros. */
25462 if (i == 0)
25463 continue;
25464 default:
25465 break;
25467 output_macinfo_op (ref);
25468 ref->info = NULL;
25469 ref->code = 0;
25472 if (!macinfo_htab)
25473 return;
25475 delete macinfo_htab;
25476 macinfo_htab = NULL;
25478 /* If any DW_MACRO_GNU_transparent_include were used, on those
25479 DW_MACRO_GNU_transparent_include entries terminate the
25480 current chain and switch to a new comdat .debug_macinfo
25481 section and emit the define/undef entries within it. */
25482 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25483 switch (ref->code)
25485 case 0:
25486 continue;
25487 case DW_MACRO_GNU_transparent_include:
25489 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25490 tree comdat_key = get_identifier (ref->info);
25491 /* Terminate the previous .debug_macinfo section. */
25492 dw2_asm_output_data (1, 0, "End compilation unit");
25493 targetm.asm_out.named_section (debug_macinfo_section_name,
25494 SECTION_DEBUG
25495 | SECTION_LINKONCE,
25496 comdat_key);
25497 ASM_GENERATE_INTERNAL_LABEL (label,
25498 DEBUG_MACRO_SECTION_LABEL,
25499 ref->lineno);
25500 ASM_OUTPUT_LABEL (asm_out_file, label);
25501 ref->code = 0;
25502 ref->info = NULL;
25503 dw2_asm_output_data (2, 4, "DWARF macro version number");
25504 if (DWARF_OFFSET_SIZE == 8)
25505 dw2_asm_output_data (1, 1, "Flags: 64-bit");
25506 else
25507 dw2_asm_output_data (1, 0, "Flags: 32-bit");
25509 break;
25510 case DW_MACINFO_define:
25511 case DW_MACINFO_undef:
25512 output_macinfo_op (ref);
25513 ref->code = 0;
25514 ref->info = NULL;
25515 break;
25516 default:
25517 gcc_unreachable ();
25521 /* Initialize the various sections and labels for dwarf output. */
25523 static void
25524 init_sections_and_labels (void)
25526 if (!dwarf_split_debug_info)
25528 debug_info_section = get_section (DEBUG_INFO_SECTION,
25529 SECTION_DEBUG, NULL);
25530 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25531 SECTION_DEBUG, NULL);
25532 debug_loc_section = get_section (DEBUG_LOC_SECTION,
25533 SECTION_DEBUG, NULL);
25534 debug_macinfo_section_name
25535 = dwarf_strict ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
25536 debug_macinfo_section = get_section (debug_macinfo_section_name,
25537 SECTION_DEBUG, NULL);
25539 else
25541 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25542 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25543 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25544 SECTION_DEBUG | SECTION_EXCLUDE,
25545 NULL);
25546 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25547 SECTION_DEBUG, NULL);
25548 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25549 SECTION_DEBUG, NULL);
25550 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25551 SECTION_DEBUG, NULL);
25552 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25553 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25555 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25556 the main .o, but the skeleton_line goes into the split off dwo. */
25557 debug_skeleton_line_section
25558 = get_section (DEBUG_DWO_LINE_SECTION,
25559 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25560 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25561 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25562 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
25563 SECTION_DEBUG | SECTION_EXCLUDE,
25564 NULL);
25565 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25566 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25567 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25568 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25569 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25570 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25571 debug_macinfo_section_name
25572 = dwarf_strict ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
25573 debug_macinfo_section = get_section (debug_macinfo_section_name,
25574 SECTION_DEBUG | SECTION_EXCLUDE,
25575 NULL);
25577 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25578 SECTION_DEBUG, NULL);
25579 debug_line_section = get_section (DEBUG_LINE_SECTION,
25580 SECTION_DEBUG, NULL);
25581 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25582 SECTION_DEBUG, NULL);
25583 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25584 SECTION_DEBUG, NULL);
25585 debug_str_section = get_section (DEBUG_STR_SECTION,
25586 DEBUG_STR_SECTION_FLAGS, NULL);
25587 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25588 SECTION_DEBUG, NULL);
25589 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25590 SECTION_DEBUG, NULL);
25592 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25593 DEBUG_ABBREV_SECTION_LABEL, 0);
25594 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25595 DEBUG_INFO_SECTION_LABEL, 0);
25596 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25597 DEBUG_LINE_SECTION_LABEL, 0);
25598 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25599 DEBUG_RANGES_SECTION_LABEL, 0);
25600 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25601 DEBUG_ADDR_SECTION_LABEL, 0);
25602 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25603 dwarf_strict
25604 ? DEBUG_MACINFO_SECTION_LABEL
25605 : DEBUG_MACRO_SECTION_LABEL, 0);
25606 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25609 /* Set up for Dwarf output at the start of compilation. */
25611 static void
25612 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25614 /* This option is currently broken, see (PR53118 and PR46102). */
25615 if (flag_eliminate_dwarf2_dups
25616 && strstr (lang_hooks.name, "C++"))
25618 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25619 flag_eliminate_dwarf2_dups = 0;
25622 /* Allocate the file_table. */
25623 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25625 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25626 /* Allocate the decl_die_table. */
25627 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25629 /* Allocate the decl_loc_table. */
25630 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25632 /* Allocate the cached_dw_loc_list_table. */
25633 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25635 /* Allocate the initial hunk of the decl_scope_table. */
25636 vec_alloc (decl_scope_table, 256);
25638 /* Allocate the initial hunk of the abbrev_die_table. */
25639 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25640 (ABBREV_DIE_TABLE_INCREMENT);
25641 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25642 /* Zero-th entry is allocated, but unused. */
25643 abbrev_die_table_in_use = 1;
25645 /* Allocate the dwarf_proc_stack_usage_map. */
25646 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25648 /* Allocate the pubtypes and pubnames vectors. */
25649 vec_alloc (pubname_table, 32);
25650 vec_alloc (pubtype_table, 32);
25652 vec_alloc (incomplete_types, 64);
25654 vec_alloc (used_rtx_array, 32);
25656 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25657 vec_alloc (macinfo_table, 64);
25658 #endif
25660 /* Make sure the line number table for .text always exists. */
25661 text_section_line_info = new_line_info_table ();
25662 text_section_line_info->end_label = text_end_label;
25664 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25665 cur_line_info_table = text_section_line_info;
25666 #endif
25668 /* If front-ends already registered a main translation unit but we were not
25669 ready to perform the association, do this now. */
25670 if (main_translation_unit != NULL_TREE)
25671 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25674 /* Called before compile () starts outputtting functions, variables
25675 and toplevel asms into assembly. */
25677 static void
25678 dwarf2out_assembly_start (void)
25680 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25681 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25682 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25683 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25684 COLD_TEXT_SECTION_LABEL, 0);
25685 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25687 switch_to_section (text_section);
25688 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25689 #endif
25691 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25692 && dwarf2out_do_cfi_asm ()
25693 && (!(flag_unwind_tables || flag_exceptions)
25694 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25695 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25698 /* A helper function for dwarf2out_finish called through
25699 htab_traverse. Assign a string its index. All strings must be
25700 collected into the table by the time index_string is called,
25701 because the indexing code relies on htab_traverse to traverse nodes
25702 in the same order for each run. */
25705 index_string (indirect_string_node **h, unsigned int *index)
25707 indirect_string_node *node = *h;
25709 find_string_form (node);
25710 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25712 gcc_assert (node->index == NO_INDEX_ASSIGNED);
25713 node->index = *index;
25714 *index += 1;
25716 return 1;
25719 /* A helper function for output_indirect_strings called through
25720 htab_traverse. Output the offset to a string and update the
25721 current offset. */
25724 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25726 indirect_string_node *node = *h;
25728 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25730 /* Assert that this node has been assigned an index. */
25731 gcc_assert (node->index != NO_INDEX_ASSIGNED
25732 && node->index != NOT_INDEXED);
25733 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25734 "indexed string 0x%x: %s", node->index, node->str);
25735 *offset += strlen (node->str) + 1;
25737 return 1;
25740 /* A helper function for dwarf2out_finish called through
25741 htab_traverse. Output the indexed string. */
25744 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25746 struct indirect_string_node *node = *h;
25748 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25750 /* Assert that the strings are output in the same order as their
25751 indexes were assigned. */
25752 gcc_assert (*cur_idx == node->index);
25753 assemble_string (node->str, strlen (node->str) + 1);
25754 *cur_idx += 1;
25756 return 1;
25759 /* A helper function for dwarf2out_finish called through
25760 htab_traverse. Emit one queued .debug_str string. */
25763 output_indirect_string (indirect_string_node **h, void *)
25765 struct indirect_string_node *node = *h;
25767 node->form = find_string_form (node);
25768 if (node->form == DW_FORM_strp && node->refcount > 0)
25770 ASM_OUTPUT_LABEL (asm_out_file, node->label);
25771 assemble_string (node->str, strlen (node->str) + 1);
25774 return 1;
25777 /* Output the indexed string table. */
25779 static void
25780 output_indirect_strings (void)
25782 switch_to_section (debug_str_section);
25783 if (!dwarf_split_debug_info)
25784 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25785 else
25787 unsigned int offset = 0;
25788 unsigned int cur_idx = 0;
25790 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25792 switch_to_section (debug_str_offsets_section);
25793 debug_str_hash->traverse_noresize
25794 <unsigned int *, output_index_string_offset> (&offset);
25795 switch_to_section (debug_str_dwo_section);
25796 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25797 (&cur_idx);
25801 /* Callback for htab_traverse to assign an index to an entry in the
25802 table, and to write that entry to the .debug_addr section. */
25805 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25807 addr_table_entry *entry = *slot;
25809 if (entry->refcount == 0)
25811 gcc_assert (entry->index == NO_INDEX_ASSIGNED
25812 || entry->index == NOT_INDEXED);
25813 return 1;
25816 gcc_assert (entry->index == *cur_index);
25817 (*cur_index)++;
25819 switch (entry->kind)
25821 case ate_kind_rtx:
25822 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25823 "0x%x", entry->index);
25824 break;
25825 case ate_kind_rtx_dtprel:
25826 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25827 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25828 DWARF2_ADDR_SIZE,
25829 entry->addr.rtl);
25830 fputc ('\n', asm_out_file);
25831 break;
25832 case ate_kind_label:
25833 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25834 "0x%x", entry->index);
25835 break;
25836 default:
25837 gcc_unreachable ();
25839 return 1;
25842 /* Produce the .debug_addr section. */
25844 static void
25845 output_addr_table (void)
25847 unsigned int index = 0;
25848 if (addr_index_table == NULL || addr_index_table->size () == 0)
25849 return;
25851 switch_to_section (debug_addr_section);
25852 addr_index_table
25853 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25856 #if ENABLE_ASSERT_CHECKING
25857 /* Verify that all marks are clear. */
25859 static void
25860 verify_marks_clear (dw_die_ref die)
25862 dw_die_ref c;
25864 gcc_assert (! die->die_mark);
25865 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25867 #endif /* ENABLE_ASSERT_CHECKING */
25869 /* Clear the marks for a die and its children.
25870 Be cool if the mark isn't set. */
25872 static void
25873 prune_unmark_dies (dw_die_ref die)
25875 dw_die_ref c;
25877 if (die->die_mark)
25878 die->die_mark = 0;
25879 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25882 /* Given LOC that is referenced by a DIE we're marking as used, find all
25883 referenced DWARF procedures it references and mark them as used. */
25885 static void
25886 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25888 for (; loc != NULL; loc = loc->dw_loc_next)
25889 switch (loc->dw_loc_opc)
25891 case DW_OP_GNU_implicit_pointer:
25892 case DW_OP_GNU_convert:
25893 case DW_OP_GNU_reinterpret:
25894 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25895 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25896 break;
25897 case DW_OP_call2:
25898 case DW_OP_call4:
25899 case DW_OP_call_ref:
25900 case DW_OP_GNU_const_type:
25901 case DW_OP_GNU_parameter_ref:
25902 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
25903 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25904 break;
25905 case DW_OP_GNU_regval_type:
25906 case DW_OP_GNU_deref_type:
25907 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
25908 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
25909 break;
25910 case DW_OP_GNU_entry_value:
25911 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
25912 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
25913 break;
25914 default:
25915 break;
25919 /* Given DIE that we're marking as used, find any other dies
25920 it references as attributes and mark them as used. */
25922 static void
25923 prune_unused_types_walk_attribs (dw_die_ref die)
25925 dw_attr_node *a;
25926 unsigned ix;
25928 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25930 switch (AT_class (a))
25932 /* Make sure DWARF procedures referenced by location descriptions will
25933 get emitted. */
25934 case dw_val_class_loc:
25935 prune_unused_types_walk_loc_descr (AT_loc (a));
25936 break;
25937 case dw_val_class_loc_list:
25938 for (dw_loc_list_ref list = AT_loc_list (a);
25939 list != NULL;
25940 list = list->dw_loc_next)
25941 prune_unused_types_walk_loc_descr (list->expr);
25942 break;
25944 case dw_val_class_die_ref:
25945 /* A reference to another DIE.
25946 Make sure that it will get emitted.
25947 If it was broken out into a comdat group, don't follow it. */
25948 if (! AT_ref (a)->comdat_type_p
25949 || a->dw_attr == DW_AT_specification)
25950 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
25951 break;
25953 case dw_val_class_str:
25954 /* Set the string's refcount to 0 so that prune_unused_types_mark
25955 accounts properly for it. */
25956 a->dw_attr_val.v.val_str->refcount = 0;
25957 break;
25959 default:
25960 break;
25965 /* Mark the generic parameters and arguments children DIEs of DIE. */
25967 static void
25968 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
25970 dw_die_ref c;
25972 if (die == NULL || die->die_child == NULL)
25973 return;
25974 c = die->die_child;
25977 if (is_template_parameter (c))
25978 prune_unused_types_mark (c, 1);
25979 c = c->die_sib;
25980 } while (c && c != die->die_child);
25983 /* Mark DIE as being used. If DOKIDS is true, then walk down
25984 to DIE's children. */
25986 static void
25987 prune_unused_types_mark (dw_die_ref die, int dokids)
25989 dw_die_ref c;
25991 if (die->die_mark == 0)
25993 /* We haven't done this node yet. Mark it as used. */
25994 die->die_mark = 1;
25995 /* If this is the DIE of a generic type instantiation,
25996 mark the children DIEs that describe its generic parms and
25997 args. */
25998 prune_unused_types_mark_generic_parms_dies (die);
26000 /* We also have to mark its parents as used.
26001 (But we don't want to mark our parent's kids due to this,
26002 unless it is a class.) */
26003 if (die->die_parent)
26004 prune_unused_types_mark (die->die_parent,
26005 class_scope_p (die->die_parent));
26007 /* Mark any referenced nodes. */
26008 prune_unused_types_walk_attribs (die);
26010 /* If this node is a specification,
26011 also mark the definition, if it exists. */
26012 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
26013 prune_unused_types_mark (die->die_definition, 1);
26016 if (dokids && die->die_mark != 2)
26018 /* We need to walk the children, but haven't done so yet.
26019 Remember that we've walked the kids. */
26020 die->die_mark = 2;
26022 /* If this is an array type, we need to make sure our
26023 kids get marked, even if they're types. If we're
26024 breaking out types into comdat sections, do this
26025 for all type definitions. */
26026 if (die->die_tag == DW_TAG_array_type
26027 || (use_debug_types
26028 && is_type_die (die) && ! is_declaration_die (die)))
26029 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
26030 else
26031 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26035 /* For local classes, look if any static member functions were emitted
26036 and if so, mark them. */
26038 static void
26039 prune_unused_types_walk_local_classes (dw_die_ref die)
26041 dw_die_ref c;
26043 if (die->die_mark == 2)
26044 return;
26046 switch (die->die_tag)
26048 case DW_TAG_structure_type:
26049 case DW_TAG_union_type:
26050 case DW_TAG_class_type:
26051 break;
26053 case DW_TAG_subprogram:
26054 if (!get_AT_flag (die, DW_AT_declaration)
26055 || die->die_definition != NULL)
26056 prune_unused_types_mark (die, 1);
26057 return;
26059 default:
26060 return;
26063 /* Mark children. */
26064 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
26067 /* Walk the tree DIE and mark types that we actually use. */
26069 static void
26070 prune_unused_types_walk (dw_die_ref die)
26072 dw_die_ref c;
26074 /* Don't do anything if this node is already marked and
26075 children have been marked as well. */
26076 if (die->die_mark == 2)
26077 return;
26079 switch (die->die_tag)
26081 case DW_TAG_structure_type:
26082 case DW_TAG_union_type:
26083 case DW_TAG_class_type:
26084 if (die->die_perennial_p)
26085 break;
26087 for (c = die->die_parent; c; c = c->die_parent)
26088 if (c->die_tag == DW_TAG_subprogram)
26089 break;
26091 /* Finding used static member functions inside of classes
26092 is needed just for local classes, because for other classes
26093 static member function DIEs with DW_AT_specification
26094 are emitted outside of the DW_TAG_*_type. If we ever change
26095 it, we'd need to call this even for non-local classes. */
26096 if (c)
26097 prune_unused_types_walk_local_classes (die);
26099 /* It's a type node --- don't mark it. */
26100 return;
26102 case DW_TAG_const_type:
26103 case DW_TAG_packed_type:
26104 case DW_TAG_pointer_type:
26105 case DW_TAG_reference_type:
26106 case DW_TAG_rvalue_reference_type:
26107 case DW_TAG_volatile_type:
26108 case DW_TAG_typedef:
26109 case DW_TAG_array_type:
26110 case DW_TAG_interface_type:
26111 case DW_TAG_friend:
26112 case DW_TAG_enumeration_type:
26113 case DW_TAG_subroutine_type:
26114 case DW_TAG_string_type:
26115 case DW_TAG_set_type:
26116 case DW_TAG_subrange_type:
26117 case DW_TAG_ptr_to_member_type:
26118 case DW_TAG_file_type:
26119 /* Type nodes are useful only when other DIEs reference them --- don't
26120 mark them. */
26121 /* FALLTHROUGH */
26123 case DW_TAG_dwarf_procedure:
26124 /* Likewise for DWARF procedures. */
26126 if (die->die_perennial_p)
26127 break;
26129 return;
26131 default:
26132 /* Mark everything else. */
26133 break;
26136 if (die->die_mark == 0)
26138 die->die_mark = 1;
26140 /* Now, mark any dies referenced from here. */
26141 prune_unused_types_walk_attribs (die);
26144 die->die_mark = 2;
26146 /* Mark children. */
26147 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26150 /* Increment the string counts on strings referred to from DIE's
26151 attributes. */
26153 static void
26154 prune_unused_types_update_strings (dw_die_ref die)
26156 dw_attr_node *a;
26157 unsigned ix;
26159 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26160 if (AT_class (a) == dw_val_class_str)
26162 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
26163 s->refcount++;
26164 /* Avoid unnecessarily putting strings that are used less than
26165 twice in the hash table. */
26166 if (s->refcount
26167 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
26169 indirect_string_node **slot
26170 = debug_str_hash->find_slot_with_hash (s->str,
26171 htab_hash_string (s->str),
26172 INSERT);
26173 gcc_assert (*slot == NULL);
26174 *slot = s;
26179 /* Remove from the tree DIE any dies that aren't marked. */
26181 static void
26182 prune_unused_types_prune (dw_die_ref die)
26184 dw_die_ref c;
26186 gcc_assert (die->die_mark);
26187 prune_unused_types_update_strings (die);
26189 if (! die->die_child)
26190 return;
26192 c = die->die_child;
26193 do {
26194 dw_die_ref prev = c, next;
26195 for (c = c->die_sib; ! c->die_mark; c = next)
26196 if (c == die->die_child)
26198 /* No marked children between 'prev' and the end of the list. */
26199 if (prev == c)
26200 /* No marked children at all. */
26201 die->die_child = NULL;
26202 else
26204 prev->die_sib = c->die_sib;
26205 die->die_child = prev;
26207 c->die_sib = NULL;
26208 return;
26210 else
26212 next = c->die_sib;
26213 c->die_sib = NULL;
26216 if (c != prev->die_sib)
26217 prev->die_sib = c;
26218 prune_unused_types_prune (c);
26219 } while (c != die->die_child);
26222 /* Remove dies representing declarations that we never use. */
26224 static void
26225 prune_unused_types (void)
26227 unsigned int i;
26228 limbo_die_node *node;
26229 comdat_type_node *ctnode;
26230 pubname_entry *pub;
26231 dw_die_ref base_type;
26233 #if ENABLE_ASSERT_CHECKING
26234 /* All the marks should already be clear. */
26235 verify_marks_clear (comp_unit_die ());
26236 for (node = limbo_die_list; node; node = node->next)
26237 verify_marks_clear (node->die);
26238 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26239 verify_marks_clear (ctnode->root_die);
26240 #endif /* ENABLE_ASSERT_CHECKING */
26242 /* Mark types that are used in global variables. */
26243 premark_types_used_by_global_vars ();
26245 /* Set the mark on nodes that are actually used. */
26246 prune_unused_types_walk (comp_unit_die ());
26247 for (node = limbo_die_list; node; node = node->next)
26248 prune_unused_types_walk (node->die);
26249 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26251 prune_unused_types_walk (ctnode->root_die);
26252 prune_unused_types_mark (ctnode->type_die, 1);
26255 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
26256 are unusual in that they are pubnames that are the children of pubtypes.
26257 They should only be marked via their parent DW_TAG_enumeration_type die,
26258 not as roots in themselves. */
26259 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26260 if (pub->die->die_tag != DW_TAG_enumerator)
26261 prune_unused_types_mark (pub->die, 1);
26262 for (i = 0; base_types.iterate (i, &base_type); i++)
26263 prune_unused_types_mark (base_type, 1);
26265 if (debug_str_hash)
26266 debug_str_hash->empty ();
26267 if (skeleton_debug_str_hash)
26268 skeleton_debug_str_hash->empty ();
26269 prune_unused_types_prune (comp_unit_die ());
26270 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26272 node = *pnode;
26273 if (!node->die->die_mark)
26274 *pnode = node->next;
26275 else
26277 prune_unused_types_prune (node->die);
26278 pnode = &node->next;
26281 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26282 prune_unused_types_prune (ctnode->root_die);
26284 /* Leave the marks clear. */
26285 prune_unmark_dies (comp_unit_die ());
26286 for (node = limbo_die_list; node; node = node->next)
26287 prune_unmark_dies (node->die);
26288 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26289 prune_unmark_dies (ctnode->root_die);
26292 /* Set the parameter to true if there are any relative pathnames in
26293 the file table. */
26295 file_table_relative_p (dwarf_file_data **slot, bool *p)
26297 struct dwarf_file_data *d = *slot;
26298 if (!IS_ABSOLUTE_PATH (d->filename))
26300 *p = true;
26301 return 0;
26303 return 1;
26306 /* Helpers to manipulate hash table of comdat type units. */
26308 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26310 static inline hashval_t hash (const comdat_type_node *);
26311 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26314 inline hashval_t
26315 comdat_type_hasher::hash (const comdat_type_node *type_node)
26317 hashval_t h;
26318 memcpy (&h, type_node->signature, sizeof (h));
26319 return h;
26322 inline bool
26323 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26324 const comdat_type_node *type_node_2)
26326 return (! memcmp (type_node_1->signature, type_node_2->signature,
26327 DWARF_TYPE_SIGNATURE_SIZE));
26330 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26331 to the location it would have been added, should we know its
26332 DECL_ASSEMBLER_NAME when we added other attributes. This will
26333 probably improve compactness of debug info, removing equivalent
26334 abbrevs, and hide any differences caused by deferring the
26335 computation of the assembler name, triggered by e.g. PCH. */
26337 static inline void
26338 move_linkage_attr (dw_die_ref die)
26340 unsigned ix = vec_safe_length (die->die_attr);
26341 dw_attr_node linkage = (*die->die_attr)[ix - 1];
26343 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26344 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26346 while (--ix > 0)
26348 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26350 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26351 break;
26354 if (ix != vec_safe_length (die->die_attr) - 1)
26356 die->die_attr->pop ();
26357 die->die_attr->quick_insert (ix, linkage);
26361 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26362 referenced from typed stack ops and count how often they are used. */
26364 static void
26365 mark_base_types (dw_loc_descr_ref loc)
26367 dw_die_ref base_type = NULL;
26369 for (; loc; loc = loc->dw_loc_next)
26371 switch (loc->dw_loc_opc)
26373 case DW_OP_GNU_regval_type:
26374 case DW_OP_GNU_deref_type:
26375 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26376 break;
26377 case DW_OP_GNU_convert:
26378 case DW_OP_GNU_reinterpret:
26379 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26380 continue;
26381 /* FALLTHRU */
26382 case DW_OP_GNU_const_type:
26383 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26384 break;
26385 case DW_OP_GNU_entry_value:
26386 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26387 continue;
26388 default:
26389 continue;
26391 gcc_assert (base_type->die_parent == comp_unit_die ());
26392 if (base_type->die_mark)
26393 base_type->die_mark++;
26394 else
26396 base_types.safe_push (base_type);
26397 base_type->die_mark = 1;
26402 /* Comparison function for sorting marked base types. */
26404 static int
26405 base_type_cmp (const void *x, const void *y)
26407 dw_die_ref dx = *(const dw_die_ref *) x;
26408 dw_die_ref dy = *(const dw_die_ref *) y;
26409 unsigned int byte_size1, byte_size2;
26410 unsigned int encoding1, encoding2;
26411 if (dx->die_mark > dy->die_mark)
26412 return -1;
26413 if (dx->die_mark < dy->die_mark)
26414 return 1;
26415 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26416 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26417 if (byte_size1 < byte_size2)
26418 return 1;
26419 if (byte_size1 > byte_size2)
26420 return -1;
26421 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26422 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26423 if (encoding1 < encoding2)
26424 return 1;
26425 if (encoding1 > encoding2)
26426 return -1;
26427 return 0;
26430 /* Move base types marked by mark_base_types as early as possible
26431 in the CU, sorted by decreasing usage count both to make the
26432 uleb128 references as small as possible and to make sure they
26433 will have die_offset already computed by calc_die_sizes when
26434 sizes of typed stack loc ops is computed. */
26436 static void
26437 move_marked_base_types (void)
26439 unsigned int i;
26440 dw_die_ref base_type, die, c;
26442 if (base_types.is_empty ())
26443 return;
26445 /* Sort by decreasing usage count, they will be added again in that
26446 order later on. */
26447 base_types.qsort (base_type_cmp);
26448 die = comp_unit_die ();
26449 c = die->die_child;
26452 dw_die_ref prev = c;
26453 c = c->die_sib;
26454 while (c->die_mark)
26456 remove_child_with_prev (c, prev);
26457 /* As base types got marked, there must be at least
26458 one node other than DW_TAG_base_type. */
26459 gcc_assert (die->die_child != NULL);
26460 c = prev->die_sib;
26463 while (c != die->die_child);
26464 gcc_assert (die->die_child);
26465 c = die->die_child;
26466 for (i = 0; base_types.iterate (i, &base_type); i++)
26468 base_type->die_mark = 0;
26469 base_type->die_sib = c->die_sib;
26470 c->die_sib = base_type;
26471 c = base_type;
26475 /* Helper function for resolve_addr, attempt to resolve
26476 one CONST_STRING, return true if successful. Similarly verify that
26477 SYMBOL_REFs refer to variables emitted in the current CU. */
26479 static bool
26480 resolve_one_addr (rtx *addr)
26482 rtx rtl = *addr;
26484 if (GET_CODE (rtl) == CONST_STRING)
26486 size_t len = strlen (XSTR (rtl, 0)) + 1;
26487 tree t = build_string (len, XSTR (rtl, 0));
26488 tree tlen = size_int (len - 1);
26489 TREE_TYPE (t)
26490 = build_array_type (char_type_node, build_index_type (tlen));
26491 rtl = lookup_constant_def (t);
26492 if (!rtl || !MEM_P (rtl))
26493 return false;
26494 rtl = XEXP (rtl, 0);
26495 if (GET_CODE (rtl) == SYMBOL_REF
26496 && SYMBOL_REF_DECL (rtl)
26497 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26498 return false;
26499 vec_safe_push (used_rtx_array, rtl);
26500 *addr = rtl;
26501 return true;
26504 if (GET_CODE (rtl) == SYMBOL_REF
26505 && SYMBOL_REF_DECL (rtl))
26507 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26509 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26510 return false;
26512 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26513 return false;
26516 if (GET_CODE (rtl) == CONST)
26518 subrtx_ptr_iterator::array_type array;
26519 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26520 if (!resolve_one_addr (*iter))
26521 return false;
26524 return true;
26527 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26528 if possible, and create DW_TAG_dwarf_procedure that can be referenced
26529 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
26531 static rtx
26532 string_cst_pool_decl (tree t)
26534 rtx rtl = output_constant_def (t, 1);
26535 unsigned char *array;
26536 dw_loc_descr_ref l;
26537 tree decl;
26538 size_t len;
26539 dw_die_ref ref;
26541 if (!rtl || !MEM_P (rtl))
26542 return NULL_RTX;
26543 rtl = XEXP (rtl, 0);
26544 if (GET_CODE (rtl) != SYMBOL_REF
26545 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26546 return NULL_RTX;
26548 decl = SYMBOL_REF_DECL (rtl);
26549 if (!lookup_decl_die (decl))
26551 len = TREE_STRING_LENGTH (t);
26552 vec_safe_push (used_rtx_array, rtl);
26553 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26554 array = ggc_vec_alloc<unsigned char> (len);
26555 memcpy (array, TREE_STRING_POINTER (t), len);
26556 l = new_loc_descr (DW_OP_implicit_value, len, 0);
26557 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26558 l->dw_loc_oprnd2.v.val_vec.length = len;
26559 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26560 l->dw_loc_oprnd2.v.val_vec.array = array;
26561 add_AT_loc (ref, DW_AT_location, l);
26562 equate_decl_number_to_die (decl, ref);
26564 return rtl;
26567 /* Helper function of resolve_addr_in_expr. LOC is
26568 a DW_OP_addr followed by DW_OP_stack_value, either at the start
26569 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26570 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
26571 with DW_OP_GNU_implicit_pointer if possible
26572 and return true, if unsuccessful, return false. */
26574 static bool
26575 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26577 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26578 HOST_WIDE_INT offset = 0;
26579 dw_die_ref ref = NULL;
26580 tree decl;
26582 if (GET_CODE (rtl) == CONST
26583 && GET_CODE (XEXP (rtl, 0)) == PLUS
26584 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26586 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26587 rtl = XEXP (XEXP (rtl, 0), 0);
26589 if (GET_CODE (rtl) == CONST_STRING)
26591 size_t len = strlen (XSTR (rtl, 0)) + 1;
26592 tree t = build_string (len, XSTR (rtl, 0));
26593 tree tlen = size_int (len - 1);
26595 TREE_TYPE (t)
26596 = build_array_type (char_type_node, build_index_type (tlen));
26597 rtl = string_cst_pool_decl (t);
26598 if (!rtl)
26599 return false;
26601 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26603 decl = SYMBOL_REF_DECL (rtl);
26604 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
26606 ref = lookup_decl_die (decl);
26607 if (ref && (get_AT (ref, DW_AT_location)
26608 || get_AT (ref, DW_AT_const_value)))
26610 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26611 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26612 loc->dw_loc_oprnd1.val_entry = NULL;
26613 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26614 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26615 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26616 loc->dw_loc_oprnd2.v.val_int = offset;
26617 return true;
26621 return false;
26624 /* Helper function for resolve_addr, handle one location
26625 expression, return false if at least one CONST_STRING or SYMBOL_REF in
26626 the location list couldn't be resolved. */
26628 static bool
26629 resolve_addr_in_expr (dw_loc_descr_ref loc)
26631 dw_loc_descr_ref keep = NULL;
26632 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26633 switch (loc->dw_loc_opc)
26635 case DW_OP_addr:
26636 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26638 if ((prev == NULL
26639 || prev->dw_loc_opc == DW_OP_piece
26640 || prev->dw_loc_opc == DW_OP_bit_piece)
26641 && loc->dw_loc_next
26642 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26643 && !dwarf_strict
26644 && optimize_one_addr_into_implicit_ptr (loc))
26645 break;
26646 return false;
26648 break;
26649 case DW_OP_GNU_addr_index:
26650 case DW_OP_GNU_const_index:
26651 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26652 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26654 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26655 if (!resolve_one_addr (&rtl))
26656 return false;
26657 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26658 loc->dw_loc_oprnd1.val_entry =
26659 add_addr_table_entry (rtl, ate_kind_rtx);
26661 break;
26662 case DW_OP_const4u:
26663 case DW_OP_const8u:
26664 if (loc->dtprel
26665 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26666 return false;
26667 break;
26668 case DW_OP_plus_uconst:
26669 if (size_of_loc_descr (loc)
26670 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26672 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26674 dw_loc_descr_ref repl
26675 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26676 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26677 add_loc_descr (&repl, loc->dw_loc_next);
26678 *loc = *repl;
26680 break;
26681 case DW_OP_implicit_value:
26682 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26683 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26684 return false;
26685 break;
26686 case DW_OP_GNU_implicit_pointer:
26687 case DW_OP_GNU_parameter_ref:
26688 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26690 dw_die_ref ref
26691 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26692 if (ref == NULL)
26693 return false;
26694 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26695 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26696 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26698 break;
26699 case DW_OP_GNU_const_type:
26700 case DW_OP_GNU_regval_type:
26701 case DW_OP_GNU_deref_type:
26702 case DW_OP_GNU_convert:
26703 case DW_OP_GNU_reinterpret:
26704 while (loc->dw_loc_next
26705 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26707 dw_die_ref base1, base2;
26708 unsigned enc1, enc2, size1, size2;
26709 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26710 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26711 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26712 else if (loc->dw_loc_oprnd1.val_class
26713 == dw_val_class_unsigned_const)
26714 break;
26715 else
26716 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26717 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26718 == dw_val_class_unsigned_const)
26719 break;
26720 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26721 gcc_assert (base1->die_tag == DW_TAG_base_type
26722 && base2->die_tag == DW_TAG_base_type);
26723 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26724 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26725 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26726 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26727 if (size1 == size2
26728 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26729 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26730 && loc != keep)
26731 || enc1 == enc2))
26733 /* Optimize away next DW_OP_GNU_convert after
26734 adjusting LOC's base type die reference. */
26735 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26736 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26737 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26738 else
26739 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26740 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26741 continue;
26743 /* Don't change integer DW_OP_GNU_convert after e.g. floating
26744 point typed stack entry. */
26745 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26746 keep = loc->dw_loc_next;
26747 break;
26749 break;
26750 default:
26751 break;
26753 return true;
26756 /* Helper function of resolve_addr. DIE had DW_AT_location of
26757 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26758 and DW_OP_addr couldn't be resolved. resolve_addr has already
26759 removed the DW_AT_location attribute. This function attempts to
26760 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26761 to it or DW_AT_const_value attribute, if possible. */
26763 static void
26764 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26766 if (TREE_CODE (decl) != VAR_DECL
26767 || lookup_decl_die (decl) != die
26768 || DECL_EXTERNAL (decl)
26769 || !TREE_STATIC (decl)
26770 || DECL_INITIAL (decl) == NULL_TREE
26771 || DECL_P (DECL_INITIAL (decl))
26772 || get_AT (die, DW_AT_const_value))
26773 return;
26775 tree init = DECL_INITIAL (decl);
26776 HOST_WIDE_INT offset = 0;
26777 /* For variables that have been optimized away and thus
26778 don't have a memory location, see if we can emit
26779 DW_AT_const_value instead. */
26780 if (tree_add_const_value_attribute (die, init))
26781 return;
26782 if (dwarf_strict)
26783 return;
26784 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26785 and ADDR_EXPR refers to a decl that has DW_AT_location or
26786 DW_AT_const_value (but isn't addressable, otherwise
26787 resolving the original DW_OP_addr wouldn't fail), see if
26788 we can add DW_OP_GNU_implicit_pointer. */
26789 STRIP_NOPS (init);
26790 if (TREE_CODE (init) == POINTER_PLUS_EXPR
26791 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26793 offset = tree_to_shwi (TREE_OPERAND (init, 1));
26794 init = TREE_OPERAND (init, 0);
26795 STRIP_NOPS (init);
26797 if (TREE_CODE (init) != ADDR_EXPR)
26798 return;
26799 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26800 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26801 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26802 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26803 && TREE_OPERAND (init, 0) != decl))
26805 dw_die_ref ref;
26806 dw_loc_descr_ref l;
26808 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26810 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26811 if (!rtl)
26812 return;
26813 decl = SYMBOL_REF_DECL (rtl);
26815 else
26816 decl = TREE_OPERAND (init, 0);
26817 ref = lookup_decl_die (decl);
26818 if (ref == NULL
26819 || (!get_AT (ref, DW_AT_location)
26820 && !get_AT (ref, DW_AT_const_value)))
26821 return;
26822 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26823 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26824 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26825 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26826 add_AT_loc (die, DW_AT_location, l);
26830 /* Return NULL if l is a DWARF expression, or first op that is not
26831 valid DWARF expression. */
26833 static dw_loc_descr_ref
26834 non_dwarf_expression (dw_loc_descr_ref l)
26836 while (l)
26838 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26839 return l;
26840 switch (l->dw_loc_opc)
26842 case DW_OP_regx:
26843 case DW_OP_implicit_value:
26844 case DW_OP_stack_value:
26845 case DW_OP_GNU_implicit_pointer:
26846 case DW_OP_GNU_parameter_ref:
26847 case DW_OP_piece:
26848 case DW_OP_bit_piece:
26849 return l;
26850 default:
26851 break;
26853 l = l->dw_loc_next;
26855 return NULL;
26858 /* Return adjusted copy of EXPR:
26859 If it is empty DWARF expression, return it.
26860 If it is valid non-empty DWARF expression,
26861 return copy of EXPR with copy of DEREF appended to it.
26862 If it is DWARF expression followed by DW_OP_reg{N,x}, return
26863 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
26864 and no DEREF.
26865 If it is DWARF expression followed by DW_OP_stack_value, return
26866 copy of the DWARF expression without anything appended.
26867 Otherwise, return NULL. */
26869 static dw_loc_descr_ref
26870 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
26873 if (expr == NULL)
26874 return NULL;
26876 dw_loc_descr_ref l = non_dwarf_expression (expr);
26877 if (l && l->dw_loc_next)
26878 return NULL;
26880 if (l)
26882 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26883 deref = new_loc_descr ((enum dwarf_location_atom)
26884 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
26885 0, 0);
26886 else
26887 switch (l->dw_loc_opc)
26889 case DW_OP_regx:
26890 deref = new_loc_descr (DW_OP_bregx,
26891 l->dw_loc_oprnd1.v.val_unsigned, 0);
26892 break;
26893 case DW_OP_stack_value:
26894 deref = NULL;
26895 break;
26896 default:
26897 return NULL;
26900 else
26901 deref = new_loc_descr (deref->dw_loc_opc,
26902 deref->dw_loc_oprnd1.v.val_int, 0);
26904 dw_loc_descr_ref ret = NULL, *p = &ret;
26905 while (expr != l)
26907 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
26908 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
26909 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
26910 p = &(*p)->dw_loc_next;
26911 expr = expr->dw_loc_next;
26913 *p = deref;
26914 return ret;
26917 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
26918 or argument, adjust it if needed and return:
26919 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
26920 attribute if present should be removed
26921 0 keep the attribute as is if the referenced var or argument has
26922 only DWARF expression that covers all ranges
26923 1 if the attribute has been successfully adjusted. */
26925 static int
26926 optimize_string_length (dw_attr_node *a)
26928 dw_loc_descr_ref l = AT_loc (a), lv;
26929 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
26930 dw_attr_node *av = get_AT (die, DW_AT_location);
26931 dw_loc_list_ref d;
26932 bool non_dwarf_expr = false;
26934 if (av == NULL)
26935 return -1;
26936 switch (AT_class (av))
26938 case dw_val_class_loc_list:
26939 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26940 if (d->expr && non_dwarf_expression (d->expr))
26941 non_dwarf_expr = true;
26942 break;
26943 case dw_val_class_loc:
26944 lv = AT_loc (av);
26945 if (lv == NULL)
26946 return -1;
26947 if (non_dwarf_expression (lv))
26948 non_dwarf_expr = true;
26949 break;
26950 default:
26951 return -1;
26954 /* If it is safe to keep DW_OP_call4 in, keep it. */
26955 if (!non_dwarf_expr
26956 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
26957 return 0;
26959 /* If not dereferencing the DW_OP_call4 afterwards, we can just
26960 copy over the DW_AT_location attribute from die to a. */
26961 if (l->dw_loc_next == NULL)
26963 a->dw_attr_val = av->dw_attr_val;
26964 return 1;
26967 dw_loc_list_ref list, *p;
26968 switch (AT_class (av))
26970 case dw_val_class_loc_list:
26971 p = &list;
26972 list = NULL;
26973 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26975 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
26976 if (lv)
26978 *p = new_loc_list (lv, d->begin, d->end, d->section);
26979 p = &(*p)->dw_loc_next;
26982 if (list == NULL)
26983 return -1;
26984 a->dw_attr_val.val_class = dw_val_class_loc_list;
26985 gen_llsym (list);
26986 *AT_loc_list_ptr (a) = list;
26987 return 1;
26988 case dw_val_class_loc:
26989 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
26990 if (lv == NULL)
26991 return -1;
26992 a->dw_attr_val.v.val_loc = lv;
26993 return 1;
26994 default:
26995 gcc_unreachable ();
26999 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
27000 an address in .rodata section if the string literal is emitted there,
27001 or remove the containing location list or replace DW_AT_const_value
27002 with DW_AT_location and empty location expression, if it isn't found
27003 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
27004 to something that has been emitted in the current CU. */
27006 static void
27007 resolve_addr (dw_die_ref die)
27009 dw_die_ref c;
27010 dw_attr_node *a;
27011 dw_loc_list_ref *curr, *start, loc;
27012 unsigned ix;
27013 bool remove_AT_byte_size = false;
27015 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27016 switch (AT_class (a))
27018 case dw_val_class_loc_list:
27019 start = curr = AT_loc_list_ptr (a);
27020 loc = *curr;
27021 gcc_assert (loc);
27022 /* The same list can be referenced more than once. See if we have
27023 already recorded the result from a previous pass. */
27024 if (loc->replaced)
27025 *curr = loc->dw_loc_next;
27026 else if (!loc->resolved_addr)
27028 /* As things stand, we do not expect or allow one die to
27029 reference a suffix of another die's location list chain.
27030 References must be identical or completely separate.
27031 There is therefore no need to cache the result of this
27032 pass on any list other than the first; doing so
27033 would lead to unnecessary writes. */
27034 while (*curr)
27036 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
27037 if (!resolve_addr_in_expr ((*curr)->expr))
27039 dw_loc_list_ref next = (*curr)->dw_loc_next;
27040 dw_loc_descr_ref l = (*curr)->expr;
27042 if (next && (*curr)->ll_symbol)
27044 gcc_assert (!next->ll_symbol);
27045 next->ll_symbol = (*curr)->ll_symbol;
27047 if (dwarf_split_debug_info)
27048 remove_loc_list_addr_table_entries (l);
27049 *curr = next;
27051 else
27053 mark_base_types ((*curr)->expr);
27054 curr = &(*curr)->dw_loc_next;
27057 if (loc == *start)
27058 loc->resolved_addr = 1;
27059 else
27061 loc->replaced = 1;
27062 loc->dw_loc_next = *start;
27065 if (!*start)
27067 remove_AT (die, a->dw_attr);
27068 ix--;
27070 break;
27071 case dw_val_class_loc:
27073 dw_loc_descr_ref l = AT_loc (a);
27074 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
27075 DW_AT_string_length is only a rough approximation; unfortunately
27076 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
27077 needs a DWARF expression, while DW_AT_location of the referenced
27078 variable or argument might be any location description. */
27079 if (a->dw_attr == DW_AT_string_length
27080 && l
27081 && l->dw_loc_opc == DW_OP_call4
27082 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
27083 && (l->dw_loc_next == NULL
27084 || (l->dw_loc_next->dw_loc_next == NULL
27085 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
27086 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
27088 switch (optimize_string_length (a))
27090 case -1:
27091 remove_AT (die, a->dw_attr);
27092 ix--;
27093 /* If we drop DW_AT_string_length, we need to drop also
27094 DW_AT_{string_length_,}byte_size. */
27095 remove_AT_byte_size = true;
27096 continue;
27097 default:
27098 break;
27099 case 1:
27100 /* Even if we keep the optimized DW_AT_string_length,
27101 it might have changed AT_class, so process it again. */
27102 ix--;
27103 continue;
27106 /* For -gdwarf-2 don't attempt to optimize
27107 DW_AT_data_member_location containing
27108 DW_OP_plus_uconst - older consumers might
27109 rely on it being that op instead of a more complex,
27110 but shorter, location description. */
27111 if ((dwarf_version > 2
27112 || a->dw_attr != DW_AT_data_member_location
27113 || l == NULL
27114 || l->dw_loc_opc != DW_OP_plus_uconst
27115 || l->dw_loc_next != NULL)
27116 && !resolve_addr_in_expr (l))
27118 if (dwarf_split_debug_info)
27119 remove_loc_list_addr_table_entries (l);
27120 if (l != NULL
27121 && l->dw_loc_next == NULL
27122 && l->dw_loc_opc == DW_OP_addr
27123 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
27124 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
27125 && a->dw_attr == DW_AT_location)
27127 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
27128 remove_AT (die, a->dw_attr);
27129 ix--;
27130 optimize_location_into_implicit_ptr (die, decl);
27131 break;
27133 remove_AT (die, a->dw_attr);
27134 ix--;
27136 else
27137 mark_base_types (l);
27139 break;
27140 case dw_val_class_addr:
27141 if (a->dw_attr == DW_AT_const_value
27142 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
27144 if (AT_index (a) != NOT_INDEXED)
27145 remove_addr_table_entry (a->dw_attr_val.val_entry);
27146 remove_AT (die, a->dw_attr);
27147 ix--;
27149 if (die->die_tag == DW_TAG_GNU_call_site
27150 && a->dw_attr == DW_AT_abstract_origin)
27152 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
27153 dw_die_ref tdie = lookup_decl_die (tdecl);
27154 dw_die_ref cdie;
27155 if (tdie == NULL
27156 && DECL_EXTERNAL (tdecl)
27157 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
27158 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
27160 /* Creating a full DIE for tdecl is overly expensive and
27161 at this point even wrong when in the LTO phase
27162 as it can end up generating new type DIEs we didn't
27163 output and thus optimize_external_refs will crash. */
27164 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
27165 add_AT_flag (tdie, DW_AT_external, 1);
27166 add_AT_flag (tdie, DW_AT_declaration, 1);
27167 add_linkage_attr (tdie, tdecl);
27168 add_name_and_src_coords_attributes (tdie, tdecl);
27169 equate_decl_number_to_die (tdecl, tdie);
27171 if (tdie)
27173 a->dw_attr_val.val_class = dw_val_class_die_ref;
27174 a->dw_attr_val.v.val_die_ref.die = tdie;
27175 a->dw_attr_val.v.val_die_ref.external = 0;
27177 else
27179 if (AT_index (a) != NOT_INDEXED)
27180 remove_addr_table_entry (a->dw_attr_val.val_entry);
27181 remove_AT (die, a->dw_attr);
27182 ix--;
27185 break;
27186 default:
27187 break;
27190 if (remove_AT_byte_size)
27191 remove_AT (die, dwarf_version >= 5
27192 ? DW_AT_string_length_byte_size
27193 : DW_AT_byte_size);
27195 FOR_EACH_CHILD (die, c, resolve_addr (c));
27198 /* Helper routines for optimize_location_lists.
27199 This pass tries to share identical local lists in .debug_loc
27200 section. */
27202 /* Iteratively hash operands of LOC opcode into HSTATE. */
27204 static void
27205 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
27207 dw_val_ref val1 = &loc->dw_loc_oprnd1;
27208 dw_val_ref val2 = &loc->dw_loc_oprnd2;
27210 switch (loc->dw_loc_opc)
27212 case DW_OP_const4u:
27213 case DW_OP_const8u:
27214 if (loc->dtprel)
27215 goto hash_addr;
27216 /* FALLTHRU */
27217 case DW_OP_const1u:
27218 case DW_OP_const1s:
27219 case DW_OP_const2u:
27220 case DW_OP_const2s:
27221 case DW_OP_const4s:
27222 case DW_OP_const8s:
27223 case DW_OP_constu:
27224 case DW_OP_consts:
27225 case DW_OP_pick:
27226 case DW_OP_plus_uconst:
27227 case DW_OP_breg0:
27228 case DW_OP_breg1:
27229 case DW_OP_breg2:
27230 case DW_OP_breg3:
27231 case DW_OP_breg4:
27232 case DW_OP_breg5:
27233 case DW_OP_breg6:
27234 case DW_OP_breg7:
27235 case DW_OP_breg8:
27236 case DW_OP_breg9:
27237 case DW_OP_breg10:
27238 case DW_OP_breg11:
27239 case DW_OP_breg12:
27240 case DW_OP_breg13:
27241 case DW_OP_breg14:
27242 case DW_OP_breg15:
27243 case DW_OP_breg16:
27244 case DW_OP_breg17:
27245 case DW_OP_breg18:
27246 case DW_OP_breg19:
27247 case DW_OP_breg20:
27248 case DW_OP_breg21:
27249 case DW_OP_breg22:
27250 case DW_OP_breg23:
27251 case DW_OP_breg24:
27252 case DW_OP_breg25:
27253 case DW_OP_breg26:
27254 case DW_OP_breg27:
27255 case DW_OP_breg28:
27256 case DW_OP_breg29:
27257 case DW_OP_breg30:
27258 case DW_OP_breg31:
27259 case DW_OP_regx:
27260 case DW_OP_fbreg:
27261 case DW_OP_piece:
27262 case DW_OP_deref_size:
27263 case DW_OP_xderef_size:
27264 hstate.add_object (val1->v.val_int);
27265 break;
27266 case DW_OP_skip:
27267 case DW_OP_bra:
27269 int offset;
27271 gcc_assert (val1->val_class == dw_val_class_loc);
27272 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
27273 hstate.add_object (offset);
27275 break;
27276 case DW_OP_implicit_value:
27277 hstate.add_object (val1->v.val_unsigned);
27278 switch (val2->val_class)
27280 case dw_val_class_const:
27281 hstate.add_object (val2->v.val_int);
27282 break;
27283 case dw_val_class_vec:
27285 unsigned int elt_size = val2->v.val_vec.elt_size;
27286 unsigned int len = val2->v.val_vec.length;
27288 hstate.add_int (elt_size);
27289 hstate.add_int (len);
27290 hstate.add (val2->v.val_vec.array, len * elt_size);
27292 break;
27293 case dw_val_class_const_double:
27294 hstate.add_object (val2->v.val_double.low);
27295 hstate.add_object (val2->v.val_double.high);
27296 break;
27297 case dw_val_class_wide_int:
27298 hstate.add (val2->v.val_wide->get_val (),
27299 get_full_len (*val2->v.val_wide)
27300 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27301 break;
27302 case dw_val_class_addr:
27303 inchash::add_rtx (val2->v.val_addr, hstate);
27304 break;
27305 default:
27306 gcc_unreachable ();
27308 break;
27309 case DW_OP_bregx:
27310 case DW_OP_bit_piece:
27311 hstate.add_object (val1->v.val_int);
27312 hstate.add_object (val2->v.val_int);
27313 break;
27314 case DW_OP_addr:
27315 hash_addr:
27316 if (loc->dtprel)
27318 unsigned char dtprel = 0xd1;
27319 hstate.add_object (dtprel);
27321 inchash::add_rtx (val1->v.val_addr, hstate);
27322 break;
27323 case DW_OP_GNU_addr_index:
27324 case DW_OP_GNU_const_index:
27326 if (loc->dtprel)
27328 unsigned char dtprel = 0xd1;
27329 hstate.add_object (dtprel);
27331 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
27333 break;
27334 case DW_OP_GNU_implicit_pointer:
27335 hstate.add_int (val2->v.val_int);
27336 break;
27337 case DW_OP_GNU_entry_value:
27338 hstate.add_object (val1->v.val_loc);
27339 break;
27340 case DW_OP_GNU_regval_type:
27341 case DW_OP_GNU_deref_type:
27343 unsigned int byte_size
27344 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
27345 unsigned int encoding
27346 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
27347 hstate.add_object (val1->v.val_int);
27348 hstate.add_object (byte_size);
27349 hstate.add_object (encoding);
27351 break;
27352 case DW_OP_GNU_convert:
27353 case DW_OP_GNU_reinterpret:
27354 if (val1->val_class == dw_val_class_unsigned_const)
27356 hstate.add_object (val1->v.val_unsigned);
27357 break;
27359 /* FALLTHRU */
27360 case DW_OP_GNU_const_type:
27362 unsigned int byte_size
27363 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
27364 unsigned int encoding
27365 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
27366 hstate.add_object (byte_size);
27367 hstate.add_object (encoding);
27368 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
27369 break;
27370 hstate.add_object (val2->val_class);
27371 switch (val2->val_class)
27373 case dw_val_class_const:
27374 hstate.add_object (val2->v.val_int);
27375 break;
27376 case dw_val_class_vec:
27378 unsigned int elt_size = val2->v.val_vec.elt_size;
27379 unsigned int len = val2->v.val_vec.length;
27381 hstate.add_object (elt_size);
27382 hstate.add_object (len);
27383 hstate.add (val2->v.val_vec.array, len * elt_size);
27385 break;
27386 case dw_val_class_const_double:
27387 hstate.add_object (val2->v.val_double.low);
27388 hstate.add_object (val2->v.val_double.high);
27389 break;
27390 case dw_val_class_wide_int:
27391 hstate.add (val2->v.val_wide->get_val (),
27392 get_full_len (*val2->v.val_wide)
27393 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27394 break;
27395 default:
27396 gcc_unreachable ();
27399 break;
27401 default:
27402 /* Other codes have no operands. */
27403 break;
27407 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
27409 static inline void
27410 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
27412 dw_loc_descr_ref l;
27413 bool sizes_computed = false;
27414 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
27415 size_of_locs (loc);
27417 for (l = loc; l != NULL; l = l->dw_loc_next)
27419 enum dwarf_location_atom opc = l->dw_loc_opc;
27420 hstate.add_object (opc);
27421 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
27423 size_of_locs (loc);
27424 sizes_computed = true;
27426 hash_loc_operands (l, hstate);
27430 /* Compute hash of the whole location list LIST_HEAD. */
27432 static inline void
27433 hash_loc_list (dw_loc_list_ref list_head)
27435 dw_loc_list_ref curr = list_head;
27436 inchash::hash hstate;
27438 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
27440 hstate.add (curr->begin, strlen (curr->begin) + 1);
27441 hstate.add (curr->end, strlen (curr->end) + 1);
27442 if (curr->section)
27443 hstate.add (curr->section, strlen (curr->section) + 1);
27444 hash_locs (curr->expr, hstate);
27446 list_head->hash = hstate.end ();
27449 /* Return true if X and Y opcodes have the same operands. */
27451 static inline bool
27452 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
27454 dw_val_ref valx1 = &x->dw_loc_oprnd1;
27455 dw_val_ref valx2 = &x->dw_loc_oprnd2;
27456 dw_val_ref valy1 = &y->dw_loc_oprnd1;
27457 dw_val_ref valy2 = &y->dw_loc_oprnd2;
27459 switch (x->dw_loc_opc)
27461 case DW_OP_const4u:
27462 case DW_OP_const8u:
27463 if (x->dtprel)
27464 goto hash_addr;
27465 /* FALLTHRU */
27466 case DW_OP_const1u:
27467 case DW_OP_const1s:
27468 case DW_OP_const2u:
27469 case DW_OP_const2s:
27470 case DW_OP_const4s:
27471 case DW_OP_const8s:
27472 case DW_OP_constu:
27473 case DW_OP_consts:
27474 case DW_OP_pick:
27475 case DW_OP_plus_uconst:
27476 case DW_OP_breg0:
27477 case DW_OP_breg1:
27478 case DW_OP_breg2:
27479 case DW_OP_breg3:
27480 case DW_OP_breg4:
27481 case DW_OP_breg5:
27482 case DW_OP_breg6:
27483 case DW_OP_breg7:
27484 case DW_OP_breg8:
27485 case DW_OP_breg9:
27486 case DW_OP_breg10:
27487 case DW_OP_breg11:
27488 case DW_OP_breg12:
27489 case DW_OP_breg13:
27490 case DW_OP_breg14:
27491 case DW_OP_breg15:
27492 case DW_OP_breg16:
27493 case DW_OP_breg17:
27494 case DW_OP_breg18:
27495 case DW_OP_breg19:
27496 case DW_OP_breg20:
27497 case DW_OP_breg21:
27498 case DW_OP_breg22:
27499 case DW_OP_breg23:
27500 case DW_OP_breg24:
27501 case DW_OP_breg25:
27502 case DW_OP_breg26:
27503 case DW_OP_breg27:
27504 case DW_OP_breg28:
27505 case DW_OP_breg29:
27506 case DW_OP_breg30:
27507 case DW_OP_breg31:
27508 case DW_OP_regx:
27509 case DW_OP_fbreg:
27510 case DW_OP_piece:
27511 case DW_OP_deref_size:
27512 case DW_OP_xderef_size:
27513 return valx1->v.val_int == valy1->v.val_int;
27514 case DW_OP_skip:
27515 case DW_OP_bra:
27516 /* If splitting debug info, the use of DW_OP_GNU_addr_index
27517 can cause irrelevant differences in dw_loc_addr. */
27518 gcc_assert (valx1->val_class == dw_val_class_loc
27519 && valy1->val_class == dw_val_class_loc
27520 && (dwarf_split_debug_info
27521 || x->dw_loc_addr == y->dw_loc_addr));
27522 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27523 case DW_OP_implicit_value:
27524 if (valx1->v.val_unsigned != valy1->v.val_unsigned
27525 || valx2->val_class != valy2->val_class)
27526 return false;
27527 switch (valx2->val_class)
27529 case dw_val_class_const:
27530 return valx2->v.val_int == valy2->v.val_int;
27531 case dw_val_class_vec:
27532 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27533 && valx2->v.val_vec.length == valy2->v.val_vec.length
27534 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27535 valx2->v.val_vec.elt_size
27536 * valx2->v.val_vec.length) == 0;
27537 case dw_val_class_const_double:
27538 return valx2->v.val_double.low == valy2->v.val_double.low
27539 && valx2->v.val_double.high == valy2->v.val_double.high;
27540 case dw_val_class_wide_int:
27541 return *valx2->v.val_wide == *valy2->v.val_wide;
27542 case dw_val_class_addr:
27543 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27544 default:
27545 gcc_unreachable ();
27547 case DW_OP_bregx:
27548 case DW_OP_bit_piece:
27549 return valx1->v.val_int == valy1->v.val_int
27550 && valx2->v.val_int == valy2->v.val_int;
27551 case DW_OP_addr:
27552 hash_addr:
27553 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27554 case DW_OP_GNU_addr_index:
27555 case DW_OP_GNU_const_index:
27557 rtx ax1 = valx1->val_entry->addr.rtl;
27558 rtx ay1 = valy1->val_entry->addr.rtl;
27559 return rtx_equal_p (ax1, ay1);
27561 case DW_OP_GNU_implicit_pointer:
27562 return valx1->val_class == dw_val_class_die_ref
27563 && valx1->val_class == valy1->val_class
27564 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27565 && valx2->v.val_int == valy2->v.val_int;
27566 case DW_OP_GNU_entry_value:
27567 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27568 case DW_OP_GNU_const_type:
27569 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27570 || valx2->val_class != valy2->val_class)
27571 return false;
27572 switch (valx2->val_class)
27574 case dw_val_class_const:
27575 return valx2->v.val_int == valy2->v.val_int;
27576 case dw_val_class_vec:
27577 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27578 && valx2->v.val_vec.length == valy2->v.val_vec.length
27579 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27580 valx2->v.val_vec.elt_size
27581 * valx2->v.val_vec.length) == 0;
27582 case dw_val_class_const_double:
27583 return valx2->v.val_double.low == valy2->v.val_double.low
27584 && valx2->v.val_double.high == valy2->v.val_double.high;
27585 case dw_val_class_wide_int:
27586 return *valx2->v.val_wide == *valy2->v.val_wide;
27587 default:
27588 gcc_unreachable ();
27590 case DW_OP_GNU_regval_type:
27591 case DW_OP_GNU_deref_type:
27592 return valx1->v.val_int == valy1->v.val_int
27593 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27594 case DW_OP_GNU_convert:
27595 case DW_OP_GNU_reinterpret:
27596 if (valx1->val_class != valy1->val_class)
27597 return false;
27598 if (valx1->val_class == dw_val_class_unsigned_const)
27599 return valx1->v.val_unsigned == valy1->v.val_unsigned;
27600 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27601 case DW_OP_GNU_parameter_ref:
27602 return valx1->val_class == dw_val_class_die_ref
27603 && valx1->val_class == valy1->val_class
27604 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27605 default:
27606 /* Other codes have no operands. */
27607 return true;
27611 /* Return true if DWARF location expressions X and Y are the same. */
27613 static inline bool
27614 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27616 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27617 if (x->dw_loc_opc != y->dw_loc_opc
27618 || x->dtprel != y->dtprel
27619 || !compare_loc_operands (x, y))
27620 break;
27621 return x == NULL && y == NULL;
27624 /* Hashtable helpers. */
27626 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27628 static inline hashval_t hash (const dw_loc_list_struct *);
27629 static inline bool equal (const dw_loc_list_struct *,
27630 const dw_loc_list_struct *);
27633 /* Return precomputed hash of location list X. */
27635 inline hashval_t
27636 loc_list_hasher::hash (const dw_loc_list_struct *x)
27638 return x->hash;
27641 /* Return true if location lists A and B are the same. */
27643 inline bool
27644 loc_list_hasher::equal (const dw_loc_list_struct *a,
27645 const dw_loc_list_struct *b)
27647 if (a == b)
27648 return 1;
27649 if (a->hash != b->hash)
27650 return 0;
27651 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27652 if (strcmp (a->begin, b->begin) != 0
27653 || strcmp (a->end, b->end) != 0
27654 || (a->section == NULL) != (b->section == NULL)
27655 || (a->section && strcmp (a->section, b->section) != 0)
27656 || !compare_locs (a->expr, b->expr))
27657 break;
27658 return a == NULL && b == NULL;
27661 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27664 /* Recursively optimize location lists referenced from DIE
27665 children and share them whenever possible. */
27667 static void
27668 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27670 dw_die_ref c;
27671 dw_attr_node *a;
27672 unsigned ix;
27673 dw_loc_list_struct **slot;
27675 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27676 if (AT_class (a) == dw_val_class_loc_list)
27678 dw_loc_list_ref list = AT_loc_list (a);
27679 /* TODO: perform some optimizations here, before hashing
27680 it and storing into the hash table. */
27681 hash_loc_list (list);
27682 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27683 if (*slot == NULL)
27684 *slot = list;
27685 else
27686 a->dw_attr_val.v.val_loc_list = *slot;
27689 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27693 /* Recursively assign each location list a unique index into the debug_addr
27694 section. */
27696 static void
27697 index_location_lists (dw_die_ref die)
27699 dw_die_ref c;
27700 dw_attr_node *a;
27701 unsigned ix;
27703 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27704 if (AT_class (a) == dw_val_class_loc_list)
27706 dw_loc_list_ref list = AT_loc_list (a);
27707 dw_loc_list_ref curr;
27708 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27710 /* Don't index an entry that has already been indexed
27711 or won't be output. */
27712 if (curr->begin_entry != NULL
27713 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27714 continue;
27716 curr->begin_entry
27717 = add_addr_table_entry (xstrdup (curr->begin),
27718 ate_kind_label);
27722 FOR_EACH_CHILD (die, c, index_location_lists (c));
27725 /* Optimize location lists referenced from DIE
27726 children and share them whenever possible. */
27728 static void
27729 optimize_location_lists (dw_die_ref die)
27731 loc_list_hash_type htab (500);
27732 optimize_location_lists_1 (die, &htab);
27735 /* Traverse the limbo die list, and add parent/child links. The only
27736 dies without parents that should be here are concrete instances of
27737 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
27738 For concrete instances, we can get the parent die from the abstract
27739 instance. */
27741 static void
27742 flush_limbo_die_list (void)
27744 limbo_die_node *node;
27746 /* get_context_die calls force_decl_die, which can put new DIEs on the
27747 limbo list in LTO mode when nested functions are put in a different
27748 partition than that of their parent function. */
27749 while ((node = limbo_die_list))
27751 dw_die_ref die = node->die;
27752 limbo_die_list = node->next;
27754 if (die->die_parent == NULL)
27756 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27758 if (origin && origin->die_parent)
27759 add_child_die (origin->die_parent, die);
27760 else if (is_cu_die (die))
27762 else if (seen_error ())
27763 /* It's OK to be confused by errors in the input. */
27764 add_child_die (comp_unit_die (), die);
27765 else
27767 /* In certain situations, the lexical block containing a
27768 nested function can be optimized away, which results
27769 in the nested function die being orphaned. Likewise
27770 with the return type of that nested function. Force
27771 this to be a child of the containing function.
27773 It may happen that even the containing function got fully
27774 inlined and optimized out. In that case we are lost and
27775 assign the empty child. This should not be big issue as
27776 the function is likely unreachable too. */
27777 gcc_assert (node->created_for);
27779 if (DECL_P (node->created_for))
27780 origin = get_context_die (DECL_CONTEXT (node->created_for));
27781 else if (TYPE_P (node->created_for))
27782 origin = scope_die_for (node->created_for, comp_unit_die ());
27783 else
27784 origin = comp_unit_die ();
27786 add_child_die (origin, die);
27792 /* Output stuff that dwarf requires at the end of every file,
27793 and generate the DWARF-2 debugging info. */
27795 static void
27796 dwarf2out_finish (const char *)
27798 comdat_type_node *ctnode;
27799 dw_die_ref main_comp_unit_die;
27801 /* Flush out any latecomers to the limbo party. */
27802 flush_limbo_die_list ();
27804 /* We shouldn't have any symbols with delayed asm names for
27805 DIEs generated after early finish. */
27806 gcc_assert (deferred_asm_name == NULL);
27808 gen_remaining_tmpl_value_param_die_attribute ();
27810 #if ENABLE_ASSERT_CHECKING
27812 dw_die_ref die = comp_unit_die (), c;
27813 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27815 #endif
27816 resolve_addr (comp_unit_die ());
27817 move_marked_base_types ();
27819 if (flag_eliminate_unused_debug_types)
27820 prune_unused_types ();
27822 /* Generate separate COMDAT sections for type DIEs. */
27823 if (use_debug_types)
27825 break_out_comdat_types (comp_unit_die ());
27827 /* Each new type_unit DIE was added to the limbo die list when created.
27828 Since these have all been added to comdat_type_list, clear the
27829 limbo die list. */
27830 limbo_die_list = NULL;
27832 /* For each new comdat type unit, copy declarations for incomplete
27833 types to make the new unit self-contained (i.e., no direct
27834 references to the main compile unit). */
27835 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27836 copy_decls_for_unworthy_types (ctnode->root_die);
27837 copy_decls_for_unworthy_types (comp_unit_die ());
27839 /* In the process of copying declarations from one unit to another,
27840 we may have left some declarations behind that are no longer
27841 referenced. Prune them. */
27842 prune_unused_types ();
27845 /* Generate separate CUs for each of the include files we've seen.
27846 They will go into limbo_die_list. */
27847 if (flag_eliminate_dwarf2_dups)
27848 break_out_includes (comp_unit_die ());
27850 /* Initialize sections and labels used for actual assembler output. */
27851 init_sections_and_labels ();
27853 /* Traverse the DIE's and add sibling attributes to those DIE's that
27854 have children. */
27855 add_sibling_attributes (comp_unit_die ());
27856 limbo_die_node *node;
27857 for (node = limbo_die_list; node; node = node->next)
27858 add_sibling_attributes (node->die);
27859 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27860 add_sibling_attributes (ctnode->root_die);
27862 /* When splitting DWARF info, we put some attributes in the
27863 skeleton compile_unit DIE that remains in the .o, while
27864 most attributes go in the DWO compile_unit_die. */
27865 if (dwarf_split_debug_info)
27866 main_comp_unit_die = gen_compile_unit_die (NULL);
27867 else
27868 main_comp_unit_die = comp_unit_die ();
27870 /* Output a terminator label for the .text section. */
27871 switch_to_section (text_section);
27872 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27873 if (cold_text_section)
27875 switch_to_section (cold_text_section);
27876 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27879 /* We can only use the low/high_pc attributes if all of the code was
27880 in .text. */
27881 if (!have_multiple_function_sections
27882 || (dwarf_version < 3 && dwarf_strict))
27884 /* Don't add if the CU has no associated code. */
27885 if (text_section_used)
27886 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27887 text_end_label, true);
27889 else
27891 unsigned fde_idx;
27892 dw_fde_ref fde;
27893 bool range_list_added = false;
27895 if (text_section_used)
27896 add_ranges_by_labels (main_comp_unit_die, text_section_label,
27897 text_end_label, &range_list_added, true);
27898 if (cold_text_section_used)
27899 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27900 cold_end_label, &range_list_added, true);
27902 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27904 if (DECL_IGNORED_P (fde->decl))
27905 continue;
27906 if (!fde->in_std_section)
27907 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27908 fde->dw_fde_end, &range_list_added,
27909 true);
27910 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27911 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27912 fde->dw_fde_second_end, &range_list_added,
27913 true);
27916 if (range_list_added)
27918 /* We need to give .debug_loc and .debug_ranges an appropriate
27919 "base address". Use zero so that these addresses become
27920 absolute. Historically, we've emitted the unexpected
27921 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27922 Emit both to give time for other tools to adapt. */
27923 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27924 if (! dwarf_strict && dwarf_version < 4)
27925 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27927 add_ranges (NULL);
27931 if (debug_info_level >= DINFO_LEVEL_TERSE)
27932 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27933 debug_line_section_label);
27935 if (have_macinfo)
27936 add_AT_macptr (comp_unit_die (),
27937 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
27938 macinfo_section_label);
27940 if (dwarf_split_debug_info)
27942 /* optimize_location_lists calculates the size of the lists,
27943 so index them first, and assign indices to the entries.
27944 Although optimize_location_lists will remove entries from
27945 the table, it only does so for duplicates, and therefore
27946 only reduces ref_counts to 1. */
27947 index_location_lists (comp_unit_die ());
27949 if (addr_index_table != NULL)
27951 unsigned int index = 0;
27952 addr_index_table
27953 ->traverse_noresize<unsigned int *, index_addr_table_entry>
27954 (&index);
27958 if (have_location_lists)
27959 optimize_location_lists (comp_unit_die ());
27961 save_macinfo_strings ();
27963 if (dwarf_split_debug_info)
27965 unsigned int index = 0;
27967 /* Add attributes common to skeleton compile_units and
27968 type_units. Because these attributes include strings, it
27969 must be done before freezing the string table. Top-level
27970 skeleton die attrs are added when the skeleton type unit is
27971 created, so ensure it is created by this point. */
27972 add_top_level_skeleton_die_attrs (main_comp_unit_die);
27973 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
27976 /* Output all of the compilation units. We put the main one last so that
27977 the offsets are available to output_pubnames. */
27978 for (node = limbo_die_list; node; node = node->next)
27979 output_comp_unit (node->die, 0);
27981 hash_table<comdat_type_hasher> comdat_type_table (100);
27982 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27984 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
27986 /* Don't output duplicate types. */
27987 if (*slot != HTAB_EMPTY_ENTRY)
27988 continue;
27990 /* Add a pointer to the line table for the main compilation unit
27991 so that the debugger can make sense of DW_AT_decl_file
27992 attributes. */
27993 if (debug_info_level >= DINFO_LEVEL_TERSE)
27994 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
27995 (!dwarf_split_debug_info
27996 ? debug_line_section_label
27997 : debug_skeleton_line_section_label));
27999 output_comdat_type_unit (ctnode);
28000 *slot = ctnode;
28003 /* The AT_pubnames attribute needs to go in all skeleton dies, including
28004 both the main_cu and all skeleton TUs. Making this call unconditional
28005 would end up either adding a second copy of the AT_pubnames attribute, or
28006 requiring a special case in add_top_level_skeleton_die_attrs. */
28007 if (!dwarf_split_debug_info)
28008 add_AT_pubnames (comp_unit_die ());
28010 if (dwarf_split_debug_info)
28012 int mark;
28013 unsigned char checksum[16];
28014 struct md5_ctx ctx;
28016 /* Compute a checksum of the comp_unit to use as the dwo_id. */
28017 md5_init_ctx (&ctx);
28018 mark = 0;
28019 die_checksum (comp_unit_die (), &ctx, &mark);
28020 unmark_all_dies (comp_unit_die ());
28021 md5_finish_ctx (&ctx, checksum);
28023 /* Use the first 8 bytes of the checksum as the dwo_id,
28024 and add it to both comp-unit DIEs. */
28025 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
28026 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
28028 /* Add the base offset of the ranges table to the skeleton
28029 comp-unit DIE. */
28030 if (ranges_table_in_use)
28031 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
28032 ranges_section_label);
28034 switch_to_section (debug_addr_section);
28035 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
28036 output_addr_table ();
28039 /* Output the main compilation unit if non-empty or if .debug_macinfo
28040 or .debug_macro will be emitted. */
28041 output_comp_unit (comp_unit_die (), have_macinfo);
28043 if (dwarf_split_debug_info && info_section_emitted)
28044 output_skeleton_debug_sections (main_comp_unit_die);
28046 /* Output the abbreviation table. */
28047 if (abbrev_die_table_in_use != 1)
28049 switch_to_section (debug_abbrev_section);
28050 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
28051 output_abbrev_section ();
28054 /* Output location list section if necessary. */
28055 if (have_location_lists)
28057 /* Output the location lists info. */
28058 switch_to_section (debug_loc_section);
28059 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
28060 output_location_lists (comp_unit_die ());
28063 output_pubtables ();
28065 /* Output the address range information if a CU (.debug_info section)
28066 was emitted. We output an empty table even if we had no functions
28067 to put in it. This because the consumer has no way to tell the
28068 difference between an empty table that we omitted and failure to
28069 generate a table that would have contained data. */
28070 if (info_section_emitted)
28072 switch_to_section (debug_aranges_section);
28073 output_aranges ();
28076 /* Output ranges section if necessary. */
28077 if (ranges_table_in_use)
28079 switch_to_section (debug_ranges_section);
28080 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
28081 output_ranges ();
28084 /* Have to end the macro section. */
28085 if (have_macinfo)
28087 switch_to_section (debug_macinfo_section);
28088 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
28089 output_macinfo ();
28090 dw2_asm_output_data (1, 0, "End compilation unit");
28093 /* Output the source line correspondence table. We must do this
28094 even if there is no line information. Otherwise, on an empty
28095 translation unit, we will generate a present, but empty,
28096 .debug_info section. IRIX 6.5 `nm' will then complain when
28097 examining the file. This is done late so that any filenames
28098 used by the debug_info section are marked as 'used'. */
28099 switch_to_section (debug_line_section);
28100 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
28101 if (! DWARF2_ASM_LINE_DEBUG_INFO)
28102 output_line_info (false);
28104 if (dwarf_split_debug_info && info_section_emitted)
28106 switch_to_section (debug_skeleton_line_section);
28107 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
28108 output_line_info (true);
28111 /* If we emitted any indirect strings, output the string table too. */
28112 if (debug_str_hash || skeleton_debug_str_hash)
28113 output_indirect_strings ();
28116 /* Perform any cleanups needed after the early debug generation pass
28117 has run. */
28119 static void
28120 dwarf2out_early_finish (const char *filename)
28122 set_early_dwarf s;
28124 /* PCH might result in DW_AT_producer string being restored from the
28125 header compilation, so always fill it with empty string initially
28126 and overwrite only here. */
28127 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
28128 producer_string = gen_producer_string ();
28129 producer->dw_attr_val.v.val_str->refcount--;
28130 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
28132 /* Add the name for the main input file now. We delayed this from
28133 dwarf2out_init to avoid complications with PCH. */
28134 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
28135 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
28136 add_comp_dir_attribute (comp_unit_die ());
28137 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
28139 bool p = false;
28140 file_table->traverse<bool *, file_table_relative_p> (&p);
28141 if (p)
28142 add_comp_dir_attribute (comp_unit_die ());
28145 /* With LTO early dwarf was really finished at compile-time, so make
28146 sure to adjust the phase after annotating the LTRANS CU DIE. */
28147 if (in_lto_p)
28149 early_dwarf_finished = true;
28150 return;
28153 /* Walk through the list of incomplete types again, trying once more to
28154 emit full debugging info for them. */
28155 retry_incomplete_types ();
28157 /* The point here is to flush out the limbo list so that it is empty
28158 and we don't need to stream it for LTO. */
28159 flush_limbo_die_list ();
28161 gen_scheduled_generic_parms_dies ();
28162 gen_remaining_tmpl_value_param_die_attribute ();
28164 /* Add DW_AT_linkage_name for all deferred DIEs. */
28165 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
28167 tree decl = node->created_for;
28168 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
28169 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
28170 ended up in deferred_asm_name before we knew it was
28171 constant and never written to disk. */
28172 && DECL_ASSEMBLER_NAME (decl))
28174 add_linkage_attr (node->die, decl);
28175 move_linkage_attr (node->die);
28178 deferred_asm_name = NULL;
28180 /* The early debug phase is now finished. */
28181 early_dwarf_finished = true;
28184 /* Reset all state within dwarf2out.c so that we can rerun the compiler
28185 within the same process. For use by toplev::finalize. */
28187 void
28188 dwarf2out_c_finalize (void)
28190 last_var_location_insn = NULL;
28191 cached_next_real_insn = NULL;
28192 used_rtx_array = NULL;
28193 incomplete_types = NULL;
28194 decl_scope_table = NULL;
28195 debug_info_section = NULL;
28196 debug_skeleton_info_section = NULL;
28197 debug_abbrev_section = NULL;
28198 debug_skeleton_abbrev_section = NULL;
28199 debug_aranges_section = NULL;
28200 debug_addr_section = NULL;
28201 debug_macinfo_section = NULL;
28202 debug_line_section = NULL;
28203 debug_skeleton_line_section = NULL;
28204 debug_loc_section = NULL;
28205 debug_pubnames_section = NULL;
28206 debug_pubtypes_section = NULL;
28207 debug_str_section = NULL;
28208 debug_str_dwo_section = NULL;
28209 debug_str_offsets_section = NULL;
28210 debug_ranges_section = NULL;
28211 debug_frame_section = NULL;
28212 fde_vec = NULL;
28213 debug_str_hash = NULL;
28214 skeleton_debug_str_hash = NULL;
28215 dw2_string_counter = 0;
28216 have_multiple_function_sections = false;
28217 text_section_used = false;
28218 cold_text_section_used = false;
28219 cold_text_section = NULL;
28220 current_unit_personality = NULL;
28222 next_die_offset = 0;
28223 single_comp_unit_die = NULL;
28224 comdat_type_list = NULL;
28225 limbo_die_list = NULL;
28226 file_table = NULL;
28227 decl_die_table = NULL;
28228 common_block_die_table = NULL;
28229 decl_loc_table = NULL;
28230 call_arg_locations = NULL;
28231 call_arg_loc_last = NULL;
28232 call_site_count = -1;
28233 tail_call_site_count = -1;
28234 cached_dw_loc_list_table = NULL;
28235 abbrev_die_table = NULL;
28236 abbrev_die_table_allocated = 0;
28237 abbrev_die_table_in_use = 0;
28238 delete dwarf_proc_stack_usage_map;
28239 dwarf_proc_stack_usage_map = NULL;
28240 line_info_label_num = 0;
28241 cur_line_info_table = NULL;
28242 text_section_line_info = NULL;
28243 cold_text_section_line_info = NULL;
28244 separate_line_info = NULL;
28245 info_section_emitted = false;
28246 pubname_table = NULL;
28247 pubtype_table = NULL;
28248 macinfo_table = NULL;
28249 ranges_table = NULL;
28250 ranges_table_allocated = 0;
28251 ranges_table_in_use = 0;
28252 ranges_by_label = 0;
28253 ranges_by_label_allocated = 0;
28254 ranges_by_label_in_use = 0;
28255 have_location_lists = false;
28256 loclabel_num = 0;
28257 poc_label_num = 0;
28258 last_emitted_file = NULL;
28259 label_num = 0;
28260 tmpl_value_parm_die_table = NULL;
28261 generic_type_instances = NULL;
28262 frame_pointer_fb_offset = 0;
28263 frame_pointer_fb_offset_valid = false;
28264 base_types.release ();
28265 XDELETEVEC (producer_string);
28266 producer_string = NULL;
28269 #include "gt-dwarf2out.h"