Split print_rtx into subroutines
[official-gcc.git] / gcc / dwarf2out.c
blob3f7833ff9cd3c0dd981e93b82379aa94c743a580
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[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
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[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
726 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
727 bool any_lsda_needed = false;
728 char augmentation[6];
729 int augmentation_size;
730 int fde_encoding = DW_EH_PE_absptr;
731 int per_encoding = DW_EH_PE_absptr;
732 int lsda_encoding = DW_EH_PE_absptr;
733 int return_reg;
734 rtx personality = NULL;
735 int dw_cie_version;
737 /* Don't emit a CIE if there won't be any FDEs. */
738 if (!fde_vec)
739 return;
741 /* Nothing to do if the assembler's doing it all. */
742 if (dwarf2out_do_cfi_asm ())
743 return;
745 /* If we don't have any functions we'll want to unwind out of, don't emit
746 any EH unwind information. If we make FDEs linkonce, we may have to
747 emit an empty label for an FDE that wouldn't otherwise be emitted. We
748 want to avoid having an FDE kept around when the function it refers to
749 is discarded. Example where this matters: a primary function template
750 in C++ requires EH information, an explicit specialization doesn't. */
751 if (for_eh)
753 bool any_eh_needed = false;
755 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
757 if (fde->uses_eh_lsda)
758 any_eh_needed = any_lsda_needed = true;
759 else if (fde_needed_for_eh_p (fde))
760 any_eh_needed = true;
761 else if (TARGET_USES_WEAK_UNWIND_INFO)
762 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
765 if (!any_eh_needed)
766 return;
769 /* We're going to be generating comments, so turn on app. */
770 if (flag_debug_asm)
771 app_enable ();
773 /* Switch to the proper frame section, first time. */
774 switch_to_frame_table_section (for_eh, false);
776 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
777 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
779 /* Output the CIE. */
780 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
781 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
782 if (!XCOFF_DEBUGGING_INFO || for_eh)
784 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
785 dw2_asm_output_data (4, 0xffffffff,
786 "Initial length escape value indicating 64-bit DWARF extension");
787 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
788 "Length of Common Information Entry");
790 ASM_OUTPUT_LABEL (asm_out_file, l1);
792 /* Now that the CIE pointer is PC-relative for EH,
793 use 0 to identify the CIE. */
794 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
795 (for_eh ? 0 : DWARF_CIE_ID),
796 "CIE Identifier Tag");
798 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
799 use CIE version 1, unless that would produce incorrect results
800 due to overflowing the return register column. */
801 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
802 dw_cie_version = 1;
803 if (return_reg >= 256 || dwarf_version > 2)
804 dw_cie_version = 3;
805 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
807 augmentation[0] = 0;
808 augmentation_size = 0;
810 personality = current_unit_personality;
811 if (for_eh)
813 char *p;
815 /* Augmentation:
816 z Indicates that a uleb128 is present to size the
817 augmentation section.
818 L Indicates the encoding (and thus presence) of
819 an LSDA pointer in the FDE augmentation.
820 R Indicates a non-default pointer encoding for
821 FDE code pointers.
822 P Indicates the presence of an encoding + language
823 personality routine in the CIE augmentation. */
825 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
826 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
827 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
829 p = augmentation + 1;
830 if (personality)
832 *p++ = 'P';
833 augmentation_size += 1 + size_of_encoded_value (per_encoding);
834 assemble_external_libcall (personality);
836 if (any_lsda_needed)
838 *p++ = 'L';
839 augmentation_size += 1;
841 if (fde_encoding != DW_EH_PE_absptr)
843 *p++ = 'R';
844 augmentation_size += 1;
846 if (p > augmentation + 1)
848 augmentation[0] = 'z';
849 *p = '\0';
852 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
853 if (personality && per_encoding == DW_EH_PE_aligned)
855 int offset = ( 4 /* Length */
856 + 4 /* CIE Id */
857 + 1 /* CIE version */
858 + strlen (augmentation) + 1 /* Augmentation */
859 + size_of_uleb128 (1) /* Code alignment */
860 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
861 + 1 /* RA column */
862 + 1 /* Augmentation size */
863 + 1 /* Personality encoding */ );
864 int pad = -offset & (PTR_SIZE - 1);
866 augmentation_size += pad;
868 /* Augmentations should be small, so there's scarce need to
869 iterate for a solution. Die if we exceed one uleb128 byte. */
870 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
874 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
875 if (dw_cie_version >= 4)
877 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
878 dw2_asm_output_data (1, 0, "CIE Segment Size");
880 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
881 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
882 "CIE Data Alignment Factor");
884 if (dw_cie_version == 1)
885 dw2_asm_output_data (1, return_reg, "CIE RA Column");
886 else
887 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
889 if (augmentation[0])
891 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
892 if (personality)
894 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
895 eh_data_format_name (per_encoding));
896 dw2_asm_output_encoded_addr_rtx (per_encoding,
897 personality,
898 true, NULL);
901 if (any_lsda_needed)
902 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
903 eh_data_format_name (lsda_encoding));
905 if (fde_encoding != DW_EH_PE_absptr)
906 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
907 eh_data_format_name (fde_encoding));
910 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
911 output_cfi (cfi, NULL, for_eh);
913 /* Pad the CIE out to an address sized boundary. */
914 ASM_OUTPUT_ALIGN (asm_out_file,
915 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
916 ASM_OUTPUT_LABEL (asm_out_file, l2);
918 /* Loop through all of the FDE's. */
919 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
921 unsigned int k;
923 /* Don't emit EH unwind info for leaf functions that don't need it. */
924 if (for_eh && !fde_needed_for_eh_p (fde))
925 continue;
927 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
928 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
929 augmentation, any_lsda_needed, lsda_encoding);
932 if (for_eh && targetm.terminate_dw2_eh_frame_info)
933 dw2_asm_output_data (4, 0, "End of Table");
935 /* Turn off app to make assembly quicker. */
936 if (flag_debug_asm)
937 app_disable ();
940 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
942 static void
943 dwarf2out_do_cfi_startproc (bool second)
945 int enc;
946 rtx ref;
947 rtx personality = get_personality_function (current_function_decl);
949 fprintf (asm_out_file, "\t.cfi_startproc\n");
951 if (personality)
953 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
954 ref = personality;
956 /* ??? The GAS support isn't entirely consistent. We have to
957 handle indirect support ourselves, but PC-relative is done
958 in the assembler. Further, the assembler can't handle any
959 of the weirder relocation types. */
960 if (enc & DW_EH_PE_indirect)
961 ref = dw2_force_const_mem (ref, true);
963 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
964 output_addr_const (asm_out_file, ref);
965 fputc ('\n', asm_out_file);
968 if (crtl->uses_eh_lsda)
970 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
972 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
973 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
974 current_function_funcdef_no);
975 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
976 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
978 if (enc & DW_EH_PE_indirect)
979 ref = dw2_force_const_mem (ref, true);
981 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
982 output_addr_const (asm_out_file, ref);
983 fputc ('\n', asm_out_file);
987 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
988 this allocation may be done before pass_final. */
990 dw_fde_ref
991 dwarf2out_alloc_current_fde (void)
993 dw_fde_ref fde;
995 fde = ggc_cleared_alloc<dw_fde_node> ();
996 fde->decl = current_function_decl;
997 fde->funcdef_number = current_function_funcdef_no;
998 fde->fde_index = vec_safe_length (fde_vec);
999 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1000 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1001 fde->nothrow = crtl->nothrow;
1002 fde->drap_reg = INVALID_REGNUM;
1003 fde->vdrap_reg = INVALID_REGNUM;
1005 /* Record the FDE associated with this function. */
1006 cfun->fde = fde;
1007 vec_safe_push (fde_vec, fde);
1009 return fde;
1012 /* Output a marker (i.e. a label) for the beginning of a function, before
1013 the prologue. */
1015 void
1016 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1017 const char *file ATTRIBUTE_UNUSED)
1019 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1020 char * dup_label;
1021 dw_fde_ref fde;
1022 section *fnsec;
1023 bool do_frame;
1025 current_function_func_begin_label = NULL;
1027 do_frame = dwarf2out_do_frame ();
1029 /* ??? current_function_func_begin_label is also used by except.c for
1030 call-site information. We must emit this label if it might be used. */
1031 if (!do_frame
1032 && (!flag_exceptions
1033 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1034 return;
1036 fnsec = function_section (current_function_decl);
1037 switch_to_section (fnsec);
1038 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1039 current_function_funcdef_no);
1040 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1041 current_function_funcdef_no);
1042 dup_label = xstrdup (label);
1043 current_function_func_begin_label = dup_label;
1045 /* We can elide the fde allocation if we're not emitting debug info. */
1046 if (!do_frame)
1047 return;
1049 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1050 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1051 would include pass_dwarf2_frame. If we've not created the FDE yet,
1052 do so now. */
1053 fde = cfun->fde;
1054 if (fde == NULL)
1055 fde = dwarf2out_alloc_current_fde ();
1057 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1058 fde->dw_fde_begin = dup_label;
1059 fde->dw_fde_current_label = dup_label;
1060 fde->in_std_section = (fnsec == text_section
1061 || (cold_text_section && fnsec == cold_text_section));
1063 /* We only want to output line number information for the genuine dwarf2
1064 prologue case, not the eh frame case. */
1065 #ifdef DWARF2_DEBUGGING_INFO
1066 if (file)
1067 dwarf2out_source_line (line, file, 0, true);
1068 #endif
1070 if (dwarf2out_do_cfi_asm ())
1071 dwarf2out_do_cfi_startproc (false);
1072 else
1074 rtx personality = get_personality_function (current_function_decl);
1075 if (!current_unit_personality)
1076 current_unit_personality = personality;
1078 /* We cannot keep a current personality per function as without CFI
1079 asm, at the point where we emit the CFI data, there is no current
1080 function anymore. */
1081 if (personality && current_unit_personality != personality)
1082 sorry ("multiple EH personalities are supported only with assemblers "
1083 "supporting .cfi_personality directive");
1087 /* Output a marker (i.e. a label) for the end of the generated code
1088 for a function prologue. This gets called *after* the prologue code has
1089 been generated. */
1091 void
1092 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1093 const char *file ATTRIBUTE_UNUSED)
1095 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1097 /* Output a label to mark the endpoint of the code generated for this
1098 function. */
1099 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1100 current_function_funcdef_no);
1101 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1102 current_function_funcdef_no);
1103 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1106 /* Output a marker (i.e. a label) for the beginning of the generated code
1107 for a function epilogue. This gets called *before* the prologue code has
1108 been generated. */
1110 void
1111 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1112 const char *file ATTRIBUTE_UNUSED)
1114 dw_fde_ref fde = cfun->fde;
1115 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1117 if (fde->dw_fde_vms_begin_epilogue)
1118 return;
1120 /* Output a label to mark the endpoint of the code generated for this
1121 function. */
1122 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1123 current_function_funcdef_no);
1124 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1125 current_function_funcdef_no);
1126 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1129 /* Output a marker (i.e. a label) for the absolute end of the generated code
1130 for a function definition. This gets called *after* the epilogue code has
1131 been generated. */
1133 void
1134 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1135 const char *file ATTRIBUTE_UNUSED)
1137 dw_fde_ref fde;
1138 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1140 last_var_location_insn = NULL;
1141 cached_next_real_insn = NULL;
1143 if (dwarf2out_do_cfi_asm ())
1144 fprintf (asm_out_file, "\t.cfi_endproc\n");
1146 /* Output a label to mark the endpoint of the code generated for this
1147 function. */
1148 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1149 current_function_funcdef_no);
1150 ASM_OUTPUT_LABEL (asm_out_file, label);
1151 fde = cfun->fde;
1152 gcc_assert (fde != NULL);
1153 if (fde->dw_fde_second_begin == NULL)
1154 fde->dw_fde_end = xstrdup (label);
1157 void
1158 dwarf2out_frame_finish (void)
1160 /* Output call frame information. */
1161 if (targetm.debug_unwind_info () == UI_DWARF2)
1162 output_call_frame_info (0);
1164 /* Output another copy for the unwinder. */
1165 if ((flag_unwind_tables || flag_exceptions)
1166 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1167 output_call_frame_info (1);
1170 /* Note that the current function section is being used for code. */
1172 static void
1173 dwarf2out_note_section_used (void)
1175 section *sec = current_function_section ();
1176 if (sec == text_section)
1177 text_section_used = true;
1178 else if (sec == cold_text_section)
1179 cold_text_section_used = true;
1182 static void var_location_switch_text_section (void);
1183 static void set_cur_line_info_table (section *);
1185 void
1186 dwarf2out_switch_text_section (void)
1188 section *sect;
1189 dw_fde_ref fde = cfun->fde;
1191 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1193 if (!in_cold_section_p)
1195 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1196 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1197 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1199 else
1201 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1202 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1203 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1205 have_multiple_function_sections = true;
1207 /* There is no need to mark used sections when not debugging. */
1208 if (cold_text_section != NULL)
1209 dwarf2out_note_section_used ();
1211 if (dwarf2out_do_cfi_asm ())
1212 fprintf (asm_out_file, "\t.cfi_endproc\n");
1214 /* Now do the real section switch. */
1215 sect = current_function_section ();
1216 switch_to_section (sect);
1218 fde->second_in_std_section
1219 = (sect == text_section
1220 || (cold_text_section && sect == cold_text_section));
1222 if (dwarf2out_do_cfi_asm ())
1223 dwarf2out_do_cfi_startproc (true);
1225 var_location_switch_text_section ();
1227 if (cold_text_section != NULL)
1228 set_cur_line_info_table (sect);
1231 /* And now, the subset of the debugging information support code necessary
1232 for emitting location expressions. */
1234 /* Data about a single source file. */
1235 struct GTY((for_user)) dwarf_file_data {
1236 const char * filename;
1237 int emitted_number;
1240 /* Describe an entry into the .debug_addr section. */
1242 enum ate_kind {
1243 ate_kind_rtx,
1244 ate_kind_rtx_dtprel,
1245 ate_kind_label
1248 struct GTY((for_user)) addr_table_entry {
1249 enum ate_kind kind;
1250 unsigned int refcount;
1251 unsigned int index;
1252 union addr_table_entry_struct_union
1254 rtx GTY ((tag ("0"))) rtl;
1255 char * GTY ((tag ("1"))) label;
1257 GTY ((desc ("%1.kind"))) addr;
1260 /* Location lists are ranges + location descriptions for that range,
1261 so you can track variables that are in different places over
1262 their entire life. */
1263 typedef struct GTY(()) dw_loc_list_struct {
1264 dw_loc_list_ref dw_loc_next;
1265 const char *begin; /* Label and addr_entry for start of range */
1266 addr_table_entry *begin_entry;
1267 const char *end; /* Label for end of range */
1268 char *ll_symbol; /* Label for beginning of location list.
1269 Only on head of list */
1270 const char *section; /* Section this loclist is relative to */
1271 dw_loc_descr_ref expr;
1272 hashval_t hash;
1273 /* True if all addresses in this and subsequent lists are known to be
1274 resolved. */
1275 bool resolved_addr;
1276 /* True if this list has been replaced by dw_loc_next. */
1277 bool replaced;
1278 bool emitted;
1279 /* True if the range should be emitted even if begin and end
1280 are the same. */
1281 bool force;
1282 } dw_loc_list_node;
1284 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1285 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1287 /* Convert a DWARF stack opcode into its string name. */
1289 static const char *
1290 dwarf_stack_op_name (unsigned int op)
1292 const char *name = get_DW_OP_name (op);
1294 if (name != NULL)
1295 return name;
1297 return "OP_<unknown>";
1300 /* Return a pointer to a newly allocated location description. Location
1301 descriptions are simple expression terms that can be strung
1302 together to form more complicated location (address) descriptions. */
1304 static inline dw_loc_descr_ref
1305 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1306 unsigned HOST_WIDE_INT oprnd2)
1308 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1310 descr->dw_loc_opc = op;
1311 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1312 descr->dw_loc_oprnd1.val_entry = NULL;
1313 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1314 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1315 descr->dw_loc_oprnd2.val_entry = NULL;
1316 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1318 return descr;
1321 /* Return a pointer to a newly allocated location description for
1322 REG and OFFSET. */
1324 static inline dw_loc_descr_ref
1325 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1327 if (reg <= 31)
1328 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1329 offset, 0);
1330 else
1331 return new_loc_descr (DW_OP_bregx, reg, offset);
1334 /* Add a location description term to a location description expression. */
1336 static inline void
1337 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1339 dw_loc_descr_ref *d;
1341 /* Find the end of the chain. */
1342 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1345 *d = descr;
1348 /* Compare two location operands for exact equality. */
1350 static bool
1351 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1353 if (a->val_class != b->val_class)
1354 return false;
1355 switch (a->val_class)
1357 case dw_val_class_none:
1358 return true;
1359 case dw_val_class_addr:
1360 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1362 case dw_val_class_offset:
1363 case dw_val_class_unsigned_const:
1364 case dw_val_class_const:
1365 case dw_val_class_range_list:
1366 case dw_val_class_lineptr:
1367 case dw_val_class_macptr:
1368 /* These are all HOST_WIDE_INT, signed or unsigned. */
1369 return a->v.val_unsigned == b->v.val_unsigned;
1371 case dw_val_class_loc:
1372 return a->v.val_loc == b->v.val_loc;
1373 case dw_val_class_loc_list:
1374 return a->v.val_loc_list == b->v.val_loc_list;
1375 case dw_val_class_die_ref:
1376 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1377 case dw_val_class_fde_ref:
1378 return a->v.val_fde_index == b->v.val_fde_index;
1379 case dw_val_class_lbl_id:
1380 case dw_val_class_high_pc:
1381 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1382 case dw_val_class_str:
1383 return a->v.val_str == b->v.val_str;
1384 case dw_val_class_flag:
1385 return a->v.val_flag == b->v.val_flag;
1386 case dw_val_class_file:
1387 return a->v.val_file == b->v.val_file;
1388 case dw_val_class_decl_ref:
1389 return a->v.val_decl_ref == b->v.val_decl_ref;
1391 case dw_val_class_const_double:
1392 return (a->v.val_double.high == b->v.val_double.high
1393 && a->v.val_double.low == b->v.val_double.low);
1395 case dw_val_class_wide_int:
1396 return *a->v.val_wide == *b->v.val_wide;
1398 case dw_val_class_vec:
1400 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1401 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1403 return (a_len == b_len
1404 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1407 case dw_val_class_data8:
1408 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1410 case dw_val_class_vms_delta:
1411 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1412 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1414 case dw_val_class_discr_value:
1415 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1416 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1417 case dw_val_class_discr_list:
1418 /* It makes no sense comparing two discriminant value lists. */
1419 return false;
1421 gcc_unreachable ();
1424 /* Compare two location atoms for exact equality. */
1426 static bool
1427 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1429 if (a->dw_loc_opc != b->dw_loc_opc)
1430 return false;
1432 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1433 address size, but since we always allocate cleared storage it
1434 should be zero for other types of locations. */
1435 if (a->dtprel != b->dtprel)
1436 return false;
1438 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1439 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1442 /* Compare two complete location expressions for exact equality. */
1444 bool
1445 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1447 while (1)
1449 if (a == b)
1450 return true;
1451 if (a == NULL || b == NULL)
1452 return false;
1453 if (!loc_descr_equal_p_1 (a, b))
1454 return false;
1456 a = a->dw_loc_next;
1457 b = b->dw_loc_next;
1462 /* Add a constant OFFSET to a location expression. */
1464 static void
1465 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1467 dw_loc_descr_ref loc;
1468 HOST_WIDE_INT *p;
1470 gcc_assert (*list_head != NULL);
1472 if (!offset)
1473 return;
1475 /* Find the end of the chain. */
1476 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1479 p = NULL;
1480 if (loc->dw_loc_opc == DW_OP_fbreg
1481 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1482 p = &loc->dw_loc_oprnd1.v.val_int;
1483 else if (loc->dw_loc_opc == DW_OP_bregx)
1484 p = &loc->dw_loc_oprnd2.v.val_int;
1486 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1487 offset. Don't optimize if an signed integer overflow would happen. */
1488 if (p != NULL
1489 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1490 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1491 *p += offset;
1493 else if (offset > 0)
1494 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1496 else
1498 loc->dw_loc_next = int_loc_descriptor (-offset);
1499 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1503 /* Add a constant OFFSET to a location list. */
1505 static void
1506 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1508 dw_loc_list_ref d;
1509 for (d = list_head; d != NULL; d = d->dw_loc_next)
1510 loc_descr_plus_const (&d->expr, offset);
1513 #define DWARF_REF_SIZE \
1514 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1516 static unsigned long int get_base_type_offset (dw_die_ref);
1518 /* Return the size of a location descriptor. */
1520 static unsigned long
1521 size_of_loc_descr (dw_loc_descr_ref loc)
1523 unsigned long size = 1;
1525 switch (loc->dw_loc_opc)
1527 case DW_OP_addr:
1528 size += DWARF2_ADDR_SIZE;
1529 break;
1530 case DW_OP_GNU_addr_index:
1531 case DW_OP_GNU_const_index:
1532 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1533 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1534 break;
1535 case DW_OP_const1u:
1536 case DW_OP_const1s:
1537 size += 1;
1538 break;
1539 case DW_OP_const2u:
1540 case DW_OP_const2s:
1541 size += 2;
1542 break;
1543 case DW_OP_const4u:
1544 case DW_OP_const4s:
1545 size += 4;
1546 break;
1547 case DW_OP_const8u:
1548 case DW_OP_const8s:
1549 size += 8;
1550 break;
1551 case DW_OP_constu:
1552 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1553 break;
1554 case DW_OP_consts:
1555 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1556 break;
1557 case DW_OP_pick:
1558 size += 1;
1559 break;
1560 case DW_OP_plus_uconst:
1561 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1562 break;
1563 case DW_OP_skip:
1564 case DW_OP_bra:
1565 size += 2;
1566 break;
1567 case DW_OP_breg0:
1568 case DW_OP_breg1:
1569 case DW_OP_breg2:
1570 case DW_OP_breg3:
1571 case DW_OP_breg4:
1572 case DW_OP_breg5:
1573 case DW_OP_breg6:
1574 case DW_OP_breg7:
1575 case DW_OP_breg8:
1576 case DW_OP_breg9:
1577 case DW_OP_breg10:
1578 case DW_OP_breg11:
1579 case DW_OP_breg12:
1580 case DW_OP_breg13:
1581 case DW_OP_breg14:
1582 case DW_OP_breg15:
1583 case DW_OP_breg16:
1584 case DW_OP_breg17:
1585 case DW_OP_breg18:
1586 case DW_OP_breg19:
1587 case DW_OP_breg20:
1588 case DW_OP_breg21:
1589 case DW_OP_breg22:
1590 case DW_OP_breg23:
1591 case DW_OP_breg24:
1592 case DW_OP_breg25:
1593 case DW_OP_breg26:
1594 case DW_OP_breg27:
1595 case DW_OP_breg28:
1596 case DW_OP_breg29:
1597 case DW_OP_breg30:
1598 case DW_OP_breg31:
1599 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1600 break;
1601 case DW_OP_regx:
1602 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1603 break;
1604 case DW_OP_fbreg:
1605 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1606 break;
1607 case DW_OP_bregx:
1608 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1609 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1610 break;
1611 case DW_OP_piece:
1612 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1613 break;
1614 case DW_OP_bit_piece:
1615 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1616 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1617 break;
1618 case DW_OP_deref_size:
1619 case DW_OP_xderef_size:
1620 size += 1;
1621 break;
1622 case DW_OP_call2:
1623 size += 2;
1624 break;
1625 case DW_OP_call4:
1626 size += 4;
1627 break;
1628 case DW_OP_call_ref:
1629 size += DWARF_REF_SIZE;
1630 break;
1631 case DW_OP_implicit_value:
1632 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1633 + loc->dw_loc_oprnd1.v.val_unsigned;
1634 break;
1635 case DW_OP_GNU_implicit_pointer:
1636 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1637 break;
1638 case DW_OP_GNU_entry_value:
1640 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1641 size += size_of_uleb128 (op_size) + op_size;
1642 break;
1644 case DW_OP_GNU_const_type:
1646 unsigned long o
1647 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1648 size += size_of_uleb128 (o) + 1;
1649 switch (loc->dw_loc_oprnd2.val_class)
1651 case dw_val_class_vec:
1652 size += loc->dw_loc_oprnd2.v.val_vec.length
1653 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1654 break;
1655 case dw_val_class_const:
1656 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1657 break;
1658 case dw_val_class_const_double:
1659 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1660 break;
1661 case dw_val_class_wide_int:
1662 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1663 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1664 break;
1665 default:
1666 gcc_unreachable ();
1668 break;
1670 case DW_OP_GNU_regval_type:
1672 unsigned long o
1673 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1674 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1675 + size_of_uleb128 (o);
1677 break;
1678 case DW_OP_GNU_deref_type:
1680 unsigned long o
1681 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1682 size += 1 + size_of_uleb128 (o);
1684 break;
1685 case DW_OP_GNU_convert:
1686 case DW_OP_GNU_reinterpret:
1687 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1688 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1689 else
1691 unsigned long o
1692 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1693 size += size_of_uleb128 (o);
1695 break;
1696 case DW_OP_GNU_parameter_ref:
1697 size += 4;
1698 break;
1699 default:
1700 break;
1703 return size;
1706 /* Return the size of a series of location descriptors. */
1708 unsigned long
1709 size_of_locs (dw_loc_descr_ref loc)
1711 dw_loc_descr_ref l;
1712 unsigned long size;
1714 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1715 field, to avoid writing to a PCH file. */
1716 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1718 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1719 break;
1720 size += size_of_loc_descr (l);
1722 if (! l)
1723 return size;
1725 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1727 l->dw_loc_addr = size;
1728 size += size_of_loc_descr (l);
1731 return size;
1734 /* Return the size of the value in a DW_AT_discr_value attribute. */
1736 static int
1737 size_of_discr_value (dw_discr_value *discr_value)
1739 if (discr_value->pos)
1740 return size_of_uleb128 (discr_value->v.uval);
1741 else
1742 return size_of_sleb128 (discr_value->v.sval);
1745 /* Return the size of the value in a DW_discr_list attribute. */
1747 static int
1748 size_of_discr_list (dw_discr_list_ref discr_list)
1750 int size = 0;
1752 for (dw_discr_list_ref list = discr_list;
1753 list != NULL;
1754 list = list->dw_discr_next)
1756 /* One byte for the discriminant value descriptor, and then one or two
1757 LEB128 numbers, depending on whether it's a single case label or a
1758 range label. */
1759 size += 1;
1760 size += size_of_discr_value (&list->dw_discr_lower_bound);
1761 if (list->dw_discr_range != 0)
1762 size += size_of_discr_value (&list->dw_discr_upper_bound);
1764 return size;
1767 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1768 static void get_ref_die_offset_label (char *, dw_die_ref);
1769 static unsigned long int get_ref_die_offset (dw_die_ref);
1771 /* Output location description stack opcode's operands (if any).
1772 The for_eh_or_skip parameter controls whether register numbers are
1773 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1774 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1775 info). This should be suppressed for the cases that have not been converted
1776 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1778 static void
1779 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1781 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1782 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1784 switch (loc->dw_loc_opc)
1786 #ifdef DWARF2_DEBUGGING_INFO
1787 case DW_OP_const2u:
1788 case DW_OP_const2s:
1789 dw2_asm_output_data (2, val1->v.val_int, NULL);
1790 break;
1791 case DW_OP_const4u:
1792 if (loc->dtprel)
1794 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1795 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1796 val1->v.val_addr);
1797 fputc ('\n', asm_out_file);
1798 break;
1800 /* FALLTHRU */
1801 case DW_OP_const4s:
1802 dw2_asm_output_data (4, val1->v.val_int, NULL);
1803 break;
1804 case DW_OP_const8u:
1805 if (loc->dtprel)
1807 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1808 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1809 val1->v.val_addr);
1810 fputc ('\n', asm_out_file);
1811 break;
1813 /* FALLTHRU */
1814 case DW_OP_const8s:
1815 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1816 dw2_asm_output_data (8, val1->v.val_int, NULL);
1817 break;
1818 case DW_OP_skip:
1819 case DW_OP_bra:
1821 int offset;
1823 gcc_assert (val1->val_class == dw_val_class_loc);
1824 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1826 dw2_asm_output_data (2, offset, NULL);
1828 break;
1829 case DW_OP_implicit_value:
1830 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1831 switch (val2->val_class)
1833 case dw_val_class_const:
1834 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1835 break;
1836 case dw_val_class_vec:
1838 unsigned int elt_size = val2->v.val_vec.elt_size;
1839 unsigned int len = val2->v.val_vec.length;
1840 unsigned int i;
1841 unsigned char *p;
1843 if (elt_size > sizeof (HOST_WIDE_INT))
1845 elt_size /= 2;
1846 len *= 2;
1848 for (i = 0, p = val2->v.val_vec.array;
1849 i < len;
1850 i++, p += elt_size)
1851 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1852 "fp or vector constant word %u", i);
1854 break;
1855 case dw_val_class_const_double:
1857 unsigned HOST_WIDE_INT first, second;
1859 if (WORDS_BIG_ENDIAN)
1861 first = val2->v.val_double.high;
1862 second = val2->v.val_double.low;
1864 else
1866 first = val2->v.val_double.low;
1867 second = val2->v.val_double.high;
1869 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1870 first, NULL);
1871 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1872 second, NULL);
1874 break;
1875 case dw_val_class_wide_int:
1877 int i;
1878 int len = get_full_len (*val2->v.val_wide);
1879 if (WORDS_BIG_ENDIAN)
1880 for (i = len - 1; i >= 0; --i)
1881 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1882 val2->v.val_wide->elt (i), NULL);
1883 else
1884 for (i = 0; i < len; ++i)
1885 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1886 val2->v.val_wide->elt (i), NULL);
1888 break;
1889 case dw_val_class_addr:
1890 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1891 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1892 break;
1893 default:
1894 gcc_unreachable ();
1896 break;
1897 #else
1898 case DW_OP_const2u:
1899 case DW_OP_const2s:
1900 case DW_OP_const4u:
1901 case DW_OP_const4s:
1902 case DW_OP_const8u:
1903 case DW_OP_const8s:
1904 case DW_OP_skip:
1905 case DW_OP_bra:
1906 case DW_OP_implicit_value:
1907 /* We currently don't make any attempt to make sure these are
1908 aligned properly like we do for the main unwind info, so
1909 don't support emitting things larger than a byte if we're
1910 only doing unwinding. */
1911 gcc_unreachable ();
1912 #endif
1913 case DW_OP_const1u:
1914 case DW_OP_const1s:
1915 dw2_asm_output_data (1, val1->v.val_int, NULL);
1916 break;
1917 case DW_OP_constu:
1918 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1919 break;
1920 case DW_OP_consts:
1921 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1922 break;
1923 case DW_OP_pick:
1924 dw2_asm_output_data (1, val1->v.val_int, NULL);
1925 break;
1926 case DW_OP_plus_uconst:
1927 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1928 break;
1929 case DW_OP_breg0:
1930 case DW_OP_breg1:
1931 case DW_OP_breg2:
1932 case DW_OP_breg3:
1933 case DW_OP_breg4:
1934 case DW_OP_breg5:
1935 case DW_OP_breg6:
1936 case DW_OP_breg7:
1937 case DW_OP_breg8:
1938 case DW_OP_breg9:
1939 case DW_OP_breg10:
1940 case DW_OP_breg11:
1941 case DW_OP_breg12:
1942 case DW_OP_breg13:
1943 case DW_OP_breg14:
1944 case DW_OP_breg15:
1945 case DW_OP_breg16:
1946 case DW_OP_breg17:
1947 case DW_OP_breg18:
1948 case DW_OP_breg19:
1949 case DW_OP_breg20:
1950 case DW_OP_breg21:
1951 case DW_OP_breg22:
1952 case DW_OP_breg23:
1953 case DW_OP_breg24:
1954 case DW_OP_breg25:
1955 case DW_OP_breg26:
1956 case DW_OP_breg27:
1957 case DW_OP_breg28:
1958 case DW_OP_breg29:
1959 case DW_OP_breg30:
1960 case DW_OP_breg31:
1961 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1962 break;
1963 case DW_OP_regx:
1965 unsigned r = val1->v.val_unsigned;
1966 if (for_eh_or_skip >= 0)
1967 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1968 gcc_assert (size_of_uleb128 (r)
1969 == size_of_uleb128 (val1->v.val_unsigned));
1970 dw2_asm_output_data_uleb128 (r, NULL);
1972 break;
1973 case DW_OP_fbreg:
1974 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1975 break;
1976 case DW_OP_bregx:
1978 unsigned r = val1->v.val_unsigned;
1979 if (for_eh_or_skip >= 0)
1980 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1981 gcc_assert (size_of_uleb128 (r)
1982 == size_of_uleb128 (val1->v.val_unsigned));
1983 dw2_asm_output_data_uleb128 (r, NULL);
1984 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1986 break;
1987 case DW_OP_piece:
1988 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1989 break;
1990 case DW_OP_bit_piece:
1991 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1992 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1993 break;
1994 case DW_OP_deref_size:
1995 case DW_OP_xderef_size:
1996 dw2_asm_output_data (1, val1->v.val_int, NULL);
1997 break;
1999 case DW_OP_addr:
2000 if (loc->dtprel)
2002 if (targetm.asm_out.output_dwarf_dtprel)
2004 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2005 DWARF2_ADDR_SIZE,
2006 val1->v.val_addr);
2007 fputc ('\n', asm_out_file);
2009 else
2010 gcc_unreachable ();
2012 else
2014 #ifdef DWARF2_DEBUGGING_INFO
2015 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2016 #else
2017 gcc_unreachable ();
2018 #endif
2020 break;
2022 case DW_OP_GNU_addr_index:
2023 case DW_OP_GNU_const_index:
2024 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2025 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2026 "(index into .debug_addr)");
2027 break;
2029 case DW_OP_call2:
2030 case DW_OP_call4:
2032 unsigned long die_offset
2033 = get_ref_die_offset (val1->v.val_die_ref.die);
2034 /* Make sure the offset has been computed and that we can encode it as
2035 an operand. */
2036 gcc_assert (die_offset > 0
2037 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2038 ? 0xffff
2039 : 0xffffffff));
2040 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2041 die_offset, NULL);
2043 break;
2045 case DW_OP_GNU_implicit_pointer:
2047 char label[MAX_ARTIFICIAL_LABEL_BYTES
2048 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2049 gcc_assert (val1->val_class == dw_val_class_die_ref);
2050 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2051 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2052 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2054 break;
2056 case DW_OP_GNU_entry_value:
2057 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2058 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2059 break;
2061 case DW_OP_GNU_const_type:
2063 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2064 gcc_assert (o);
2065 dw2_asm_output_data_uleb128 (o, NULL);
2066 switch (val2->val_class)
2068 case dw_val_class_const:
2069 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2070 dw2_asm_output_data (1, l, NULL);
2071 dw2_asm_output_data (l, val2->v.val_int, NULL);
2072 break;
2073 case dw_val_class_vec:
2075 unsigned int elt_size = val2->v.val_vec.elt_size;
2076 unsigned int len = val2->v.val_vec.length;
2077 unsigned int i;
2078 unsigned char *p;
2080 l = len * elt_size;
2081 dw2_asm_output_data (1, l, NULL);
2082 if (elt_size > sizeof (HOST_WIDE_INT))
2084 elt_size /= 2;
2085 len *= 2;
2087 for (i = 0, p = val2->v.val_vec.array;
2088 i < len;
2089 i++, p += elt_size)
2090 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2091 "fp or vector constant word %u", i);
2093 break;
2094 case dw_val_class_const_double:
2096 unsigned HOST_WIDE_INT first, second;
2097 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2099 dw2_asm_output_data (1, 2 * l, NULL);
2100 if (WORDS_BIG_ENDIAN)
2102 first = val2->v.val_double.high;
2103 second = val2->v.val_double.low;
2105 else
2107 first = val2->v.val_double.low;
2108 second = val2->v.val_double.high;
2110 dw2_asm_output_data (l, first, NULL);
2111 dw2_asm_output_data (l, second, NULL);
2113 break;
2114 case dw_val_class_wide_int:
2116 int i;
2117 int len = get_full_len (*val2->v.val_wide);
2118 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2120 dw2_asm_output_data (1, len * l, NULL);
2121 if (WORDS_BIG_ENDIAN)
2122 for (i = len - 1; i >= 0; --i)
2123 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2124 else
2125 for (i = 0; i < len; ++i)
2126 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2128 break;
2129 default:
2130 gcc_unreachable ();
2133 break;
2134 case DW_OP_GNU_regval_type:
2136 unsigned r = val1->v.val_unsigned;
2137 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2138 gcc_assert (o);
2139 if (for_eh_or_skip >= 0)
2141 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2142 gcc_assert (size_of_uleb128 (r)
2143 == size_of_uleb128 (val1->v.val_unsigned));
2145 dw2_asm_output_data_uleb128 (r, NULL);
2146 dw2_asm_output_data_uleb128 (o, NULL);
2148 break;
2149 case DW_OP_GNU_deref_type:
2151 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2152 gcc_assert (o);
2153 dw2_asm_output_data (1, val1->v.val_int, NULL);
2154 dw2_asm_output_data_uleb128 (o, NULL);
2156 break;
2157 case DW_OP_GNU_convert:
2158 case DW_OP_GNU_reinterpret:
2159 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2160 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2161 else
2163 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2164 gcc_assert (o);
2165 dw2_asm_output_data_uleb128 (o, NULL);
2167 break;
2169 case DW_OP_GNU_parameter_ref:
2171 unsigned long o;
2172 gcc_assert (val1->val_class == dw_val_class_die_ref);
2173 o = get_ref_die_offset (val1->v.val_die_ref.die);
2174 dw2_asm_output_data (4, o, NULL);
2176 break;
2178 default:
2179 /* Other codes have no operands. */
2180 break;
2184 /* Output a sequence of location operations.
2185 The for_eh_or_skip parameter controls whether register numbers are
2186 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2187 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2188 info). This should be suppressed for the cases that have not been converted
2189 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2191 void
2192 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2194 for (; loc != NULL; loc = loc->dw_loc_next)
2196 enum dwarf_location_atom opc = loc->dw_loc_opc;
2197 /* Output the opcode. */
2198 if (for_eh_or_skip >= 0
2199 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2201 unsigned r = (opc - DW_OP_breg0);
2202 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2203 gcc_assert (r <= 31);
2204 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2206 else if (for_eh_or_skip >= 0
2207 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2209 unsigned r = (opc - DW_OP_reg0);
2210 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2211 gcc_assert (r <= 31);
2212 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2215 dw2_asm_output_data (1, opc,
2216 "%s", dwarf_stack_op_name (opc));
2218 /* Output the operand(s) (if any). */
2219 output_loc_operands (loc, for_eh_or_skip);
2223 /* Output location description stack opcode's operands (if any).
2224 The output is single bytes on a line, suitable for .cfi_escape. */
2226 static void
2227 output_loc_operands_raw (dw_loc_descr_ref loc)
2229 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2230 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2232 switch (loc->dw_loc_opc)
2234 case DW_OP_addr:
2235 case DW_OP_GNU_addr_index:
2236 case DW_OP_GNU_const_index:
2237 case DW_OP_implicit_value:
2238 /* We cannot output addresses in .cfi_escape, only bytes. */
2239 gcc_unreachable ();
2241 case DW_OP_const1u:
2242 case DW_OP_const1s:
2243 case DW_OP_pick:
2244 case DW_OP_deref_size:
2245 case DW_OP_xderef_size:
2246 fputc (',', asm_out_file);
2247 dw2_asm_output_data_raw (1, val1->v.val_int);
2248 break;
2250 case DW_OP_const2u:
2251 case DW_OP_const2s:
2252 fputc (',', asm_out_file);
2253 dw2_asm_output_data_raw (2, val1->v.val_int);
2254 break;
2256 case DW_OP_const4u:
2257 case DW_OP_const4s:
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_raw (4, val1->v.val_int);
2260 break;
2262 case DW_OP_const8u:
2263 case DW_OP_const8s:
2264 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2265 fputc (',', asm_out_file);
2266 dw2_asm_output_data_raw (8, val1->v.val_int);
2267 break;
2269 case DW_OP_skip:
2270 case DW_OP_bra:
2272 int offset;
2274 gcc_assert (val1->val_class == dw_val_class_loc);
2275 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2277 fputc (',', asm_out_file);
2278 dw2_asm_output_data_raw (2, offset);
2280 break;
2282 case DW_OP_regx:
2284 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2285 gcc_assert (size_of_uleb128 (r)
2286 == size_of_uleb128 (val1->v.val_unsigned));
2287 fputc (',', asm_out_file);
2288 dw2_asm_output_data_uleb128_raw (r);
2290 break;
2292 case DW_OP_constu:
2293 case DW_OP_plus_uconst:
2294 case DW_OP_piece:
2295 fputc (',', asm_out_file);
2296 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2297 break;
2299 case DW_OP_bit_piece:
2300 fputc (',', asm_out_file);
2301 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2302 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2303 break;
2305 case DW_OP_consts:
2306 case DW_OP_breg0:
2307 case DW_OP_breg1:
2308 case DW_OP_breg2:
2309 case DW_OP_breg3:
2310 case DW_OP_breg4:
2311 case DW_OP_breg5:
2312 case DW_OP_breg6:
2313 case DW_OP_breg7:
2314 case DW_OP_breg8:
2315 case DW_OP_breg9:
2316 case DW_OP_breg10:
2317 case DW_OP_breg11:
2318 case DW_OP_breg12:
2319 case DW_OP_breg13:
2320 case DW_OP_breg14:
2321 case DW_OP_breg15:
2322 case DW_OP_breg16:
2323 case DW_OP_breg17:
2324 case DW_OP_breg18:
2325 case DW_OP_breg19:
2326 case DW_OP_breg20:
2327 case DW_OP_breg21:
2328 case DW_OP_breg22:
2329 case DW_OP_breg23:
2330 case DW_OP_breg24:
2331 case DW_OP_breg25:
2332 case DW_OP_breg26:
2333 case DW_OP_breg27:
2334 case DW_OP_breg28:
2335 case DW_OP_breg29:
2336 case DW_OP_breg30:
2337 case DW_OP_breg31:
2338 case DW_OP_fbreg:
2339 fputc (',', asm_out_file);
2340 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2341 break;
2343 case DW_OP_bregx:
2345 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2346 gcc_assert (size_of_uleb128 (r)
2347 == size_of_uleb128 (val1->v.val_unsigned));
2348 fputc (',', asm_out_file);
2349 dw2_asm_output_data_uleb128_raw (r);
2350 fputc (',', asm_out_file);
2351 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2353 break;
2355 case DW_OP_GNU_implicit_pointer:
2356 case DW_OP_GNU_entry_value:
2357 case DW_OP_GNU_const_type:
2358 case DW_OP_GNU_regval_type:
2359 case DW_OP_GNU_deref_type:
2360 case DW_OP_GNU_convert:
2361 case DW_OP_GNU_reinterpret:
2362 case DW_OP_GNU_parameter_ref:
2363 gcc_unreachable ();
2364 break;
2366 default:
2367 /* Other codes have no operands. */
2368 break;
2372 void
2373 output_loc_sequence_raw (dw_loc_descr_ref loc)
2375 while (1)
2377 enum dwarf_location_atom opc = loc->dw_loc_opc;
2378 /* Output the opcode. */
2379 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2381 unsigned r = (opc - DW_OP_breg0);
2382 r = DWARF2_FRAME_REG_OUT (r, 1);
2383 gcc_assert (r <= 31);
2384 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2386 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2388 unsigned r = (opc - DW_OP_reg0);
2389 r = DWARF2_FRAME_REG_OUT (r, 1);
2390 gcc_assert (r <= 31);
2391 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2393 /* Output the opcode. */
2394 fprintf (asm_out_file, "%#x", opc);
2395 output_loc_operands_raw (loc);
2397 if (!loc->dw_loc_next)
2398 break;
2399 loc = loc->dw_loc_next;
2401 fputc (',', asm_out_file);
2405 /* This function builds a dwarf location descriptor sequence from a
2406 dw_cfa_location, adding the given OFFSET to the result of the
2407 expression. */
2409 struct dw_loc_descr_node *
2410 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2412 struct dw_loc_descr_node *head, *tmp;
2414 offset += cfa->offset;
2416 if (cfa->indirect)
2418 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2419 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2420 head->dw_loc_oprnd1.val_entry = NULL;
2421 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2422 add_loc_descr (&head, tmp);
2423 if (offset != 0)
2425 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2426 add_loc_descr (&head, tmp);
2429 else
2430 head = new_reg_loc_descr (cfa->reg, offset);
2432 return head;
2435 /* This function builds a dwarf location descriptor sequence for
2436 the address at OFFSET from the CFA when stack is aligned to
2437 ALIGNMENT byte. */
2439 struct dw_loc_descr_node *
2440 build_cfa_aligned_loc (dw_cfa_location *cfa,
2441 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2443 struct dw_loc_descr_node *head;
2444 unsigned int dwarf_fp
2445 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2447 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2448 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2450 head = new_reg_loc_descr (dwarf_fp, 0);
2451 add_loc_descr (&head, int_loc_descriptor (alignment));
2452 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2453 loc_descr_plus_const (&head, offset);
2455 else
2456 head = new_reg_loc_descr (dwarf_fp, offset);
2457 return head;
2460 /* And now, the support for symbolic debugging information. */
2462 /* .debug_str support. */
2464 static void dwarf2out_init (const char *);
2465 static void dwarf2out_finish (const char *);
2466 static void dwarf2out_early_finish (const char *);
2467 static void dwarf2out_assembly_start (void);
2468 static void dwarf2out_define (unsigned int, const char *);
2469 static void dwarf2out_undef (unsigned int, const char *);
2470 static void dwarf2out_start_source_file (unsigned, const char *);
2471 static void dwarf2out_end_source_file (unsigned);
2472 static void dwarf2out_function_decl (tree);
2473 static void dwarf2out_begin_block (unsigned, unsigned);
2474 static void dwarf2out_end_block (unsigned, unsigned);
2475 static bool dwarf2out_ignore_block (const_tree);
2476 static void dwarf2out_early_global_decl (tree);
2477 static void dwarf2out_late_global_decl (tree);
2478 static void dwarf2out_type_decl (tree, int);
2479 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2480 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2481 dw_die_ref);
2482 static void dwarf2out_abstract_function (tree);
2483 static void dwarf2out_var_location (rtx_insn *);
2484 static void dwarf2out_size_function (tree);
2485 static void dwarf2out_begin_function (tree);
2486 static void dwarf2out_end_function (unsigned int);
2487 static void dwarf2out_register_main_translation_unit (tree unit);
2488 static void dwarf2out_set_name (tree, tree);
2490 /* The debug hooks structure. */
2492 const struct gcc_debug_hooks dwarf2_debug_hooks =
2494 dwarf2out_init,
2495 dwarf2out_finish,
2496 dwarf2out_early_finish,
2497 dwarf2out_assembly_start,
2498 dwarf2out_define,
2499 dwarf2out_undef,
2500 dwarf2out_start_source_file,
2501 dwarf2out_end_source_file,
2502 dwarf2out_begin_block,
2503 dwarf2out_end_block,
2504 dwarf2out_ignore_block,
2505 dwarf2out_source_line,
2506 dwarf2out_begin_prologue,
2507 #if VMS_DEBUGGING_INFO
2508 dwarf2out_vms_end_prologue,
2509 dwarf2out_vms_begin_epilogue,
2510 #else
2511 debug_nothing_int_charstar,
2512 debug_nothing_int_charstar,
2513 #endif
2514 dwarf2out_end_epilogue,
2515 dwarf2out_begin_function,
2516 dwarf2out_end_function, /* end_function */
2517 dwarf2out_register_main_translation_unit,
2518 dwarf2out_function_decl, /* function_decl */
2519 dwarf2out_early_global_decl,
2520 dwarf2out_late_global_decl,
2521 dwarf2out_type_decl, /* type_decl */
2522 dwarf2out_imported_module_or_decl,
2523 debug_nothing_tree, /* deferred_inline_function */
2524 /* The DWARF 2 backend tries to reduce debugging bloat by not
2525 emitting the abstract description of inline functions until
2526 something tries to reference them. */
2527 dwarf2out_abstract_function, /* outlining_inline_function */
2528 debug_nothing_rtx_code_label, /* label */
2529 debug_nothing_int, /* handle_pch */
2530 dwarf2out_var_location,
2531 dwarf2out_size_function, /* size_function */
2532 dwarf2out_switch_text_section,
2533 dwarf2out_set_name,
2534 1, /* start_end_main_source_file */
2535 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2538 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2540 dwarf2out_init,
2541 debug_nothing_charstar,
2542 debug_nothing_charstar,
2543 debug_nothing_void,
2544 debug_nothing_int_charstar,
2545 debug_nothing_int_charstar,
2546 debug_nothing_int_charstar,
2547 debug_nothing_int,
2548 debug_nothing_int_int, /* begin_block */
2549 debug_nothing_int_int, /* end_block */
2550 debug_true_const_tree, /* ignore_block */
2551 dwarf2out_source_line, /* source_line */
2552 debug_nothing_int_charstar, /* begin_prologue */
2553 debug_nothing_int_charstar, /* end_prologue */
2554 debug_nothing_int_charstar, /* begin_epilogue */
2555 debug_nothing_int_charstar, /* end_epilogue */
2556 debug_nothing_tree, /* begin_function */
2557 debug_nothing_int, /* end_function */
2558 debug_nothing_tree, /* register_main_translation_unit */
2559 debug_nothing_tree, /* function_decl */
2560 debug_nothing_tree, /* early_global_decl */
2561 debug_nothing_tree, /* late_global_decl */
2562 debug_nothing_tree_int, /* type_decl */
2563 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2564 debug_nothing_tree, /* deferred_inline_function */
2565 debug_nothing_tree, /* outlining_inline_function */
2566 debug_nothing_rtx_code_label, /* label */
2567 debug_nothing_int, /* handle_pch */
2568 debug_nothing_rtx_insn, /* var_location */
2569 debug_nothing_tree, /* size_function */
2570 debug_nothing_void, /* switch_text_section */
2571 debug_nothing_tree_tree, /* set_name */
2572 0, /* start_end_main_source_file */
2573 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2576 /* NOTE: In the comments in this file, many references are made to
2577 "Debugging Information Entries". This term is abbreviated as `DIE'
2578 throughout the remainder of this file. */
2580 /* An internal representation of the DWARF output is built, and then
2581 walked to generate the DWARF debugging info. The walk of the internal
2582 representation is done after the entire program has been compiled.
2583 The types below are used to describe the internal representation. */
2585 /* Whether to put type DIEs into their own section .debug_types instead
2586 of making them part of the .debug_info section. Only supported for
2587 Dwarf V4 or higher and the user didn't disable them through
2588 -fno-debug-types-section. It is more efficient to put them in a
2589 separate comdat sections since the linker will then be able to
2590 remove duplicates. But not all tools support .debug_types sections
2591 yet. */
2593 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2595 /* Various DIE's use offsets relative to the beginning of the
2596 .debug_info section to refer to each other. */
2598 typedef long int dw_offset;
2600 struct comdat_type_node;
2602 /* The entries in the line_info table more-or-less mirror the opcodes
2603 that are used in the real dwarf line table. Arrays of these entries
2604 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2605 supported. */
2607 enum dw_line_info_opcode {
2608 /* Emit DW_LNE_set_address; the operand is the label index. */
2609 LI_set_address,
2611 /* Emit a row to the matrix with the given line. This may be done
2612 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2613 special opcodes. */
2614 LI_set_line,
2616 /* Emit a DW_LNS_set_file. */
2617 LI_set_file,
2619 /* Emit a DW_LNS_set_column. */
2620 LI_set_column,
2622 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2623 LI_negate_stmt,
2625 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2626 LI_set_prologue_end,
2627 LI_set_epilogue_begin,
2629 /* Emit a DW_LNE_set_discriminator. */
2630 LI_set_discriminator
2633 typedef struct GTY(()) dw_line_info_struct {
2634 enum dw_line_info_opcode opcode;
2635 unsigned int val;
2636 } dw_line_info_entry;
2639 struct GTY(()) dw_line_info_table {
2640 /* The label that marks the end of this section. */
2641 const char *end_label;
2643 /* The values for the last row of the matrix, as collected in the table.
2644 These are used to minimize the changes to the next row. */
2645 unsigned int file_num;
2646 unsigned int line_num;
2647 unsigned int column_num;
2648 int discrim_num;
2649 bool is_stmt;
2650 bool in_use;
2652 vec<dw_line_info_entry, va_gc> *entries;
2656 /* Each DIE attribute has a field specifying the attribute kind,
2657 a link to the next attribute in the chain, and an attribute value.
2658 Attributes are typically linked below the DIE they modify. */
2660 typedef struct GTY(()) dw_attr_struct {
2661 enum dwarf_attribute dw_attr;
2662 dw_val_node dw_attr_val;
2664 dw_attr_node;
2667 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2668 The children of each node form a circular list linked by
2669 die_sib. die_child points to the node *before* the "first" child node. */
2671 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2672 union die_symbol_or_type_node
2674 const char * GTY ((tag ("0"))) die_symbol;
2675 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2677 GTY ((desc ("%0.comdat_type_p"))) die_id;
2678 vec<dw_attr_node, va_gc> *die_attr;
2679 dw_die_ref die_parent;
2680 dw_die_ref die_child;
2681 dw_die_ref die_sib;
2682 dw_die_ref die_definition; /* ref from a specification to its definition */
2683 dw_offset die_offset;
2684 unsigned long die_abbrev;
2685 int die_mark;
2686 unsigned int decl_id;
2687 enum dwarf_tag die_tag;
2688 /* Die is used and must not be pruned as unused. */
2689 BOOL_BITFIELD die_perennial_p : 1;
2690 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2691 /* Whether this DIE was removed from the DIE tree, for example via
2692 prune_unused_types. We don't consider those present from the
2693 DIE lookup routines. */
2694 BOOL_BITFIELD removed : 1;
2695 /* Lots of spare bits. */
2697 die_node;
2699 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2700 static bool early_dwarf;
2701 static bool early_dwarf_finished;
2702 struct set_early_dwarf {
2703 bool saved;
2704 set_early_dwarf () : saved(early_dwarf)
2706 gcc_assert (! early_dwarf_finished);
2707 early_dwarf = true;
2709 ~set_early_dwarf () { early_dwarf = saved; }
2712 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2713 #define FOR_EACH_CHILD(die, c, expr) do { \
2714 c = die->die_child; \
2715 if (c) do { \
2716 c = c->die_sib; \
2717 expr; \
2718 } while (c != die->die_child); \
2719 } while (0)
2721 /* The pubname structure */
2723 typedef struct GTY(()) pubname_struct {
2724 dw_die_ref die;
2725 const char *name;
2727 pubname_entry;
2730 struct GTY(()) dw_ranges {
2731 /* If this is positive, it's a block number, otherwise it's a
2732 bitwise-negated index into dw_ranges_by_label. */
2733 int num;
2736 /* A structure to hold a macinfo entry. */
2738 typedef struct GTY(()) macinfo_struct {
2739 unsigned char code;
2740 unsigned HOST_WIDE_INT lineno;
2741 const char *info;
2743 macinfo_entry;
2746 struct GTY(()) dw_ranges_by_label {
2747 const char *begin;
2748 const char *end;
2751 /* The comdat type node structure. */
2752 struct GTY(()) comdat_type_node
2754 dw_die_ref root_die;
2755 dw_die_ref type_die;
2756 dw_die_ref skeleton_die;
2757 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2758 comdat_type_node *next;
2761 /* A list of DIEs for which we can't determine ancestry (parent_die
2762 field) just yet. Later in dwarf2out_finish we will fill in the
2763 missing bits. */
2764 typedef struct GTY(()) limbo_die_struct {
2765 dw_die_ref die;
2766 /* The tree for which this DIE was created. We use this to
2767 determine ancestry later. */
2768 tree created_for;
2769 struct limbo_die_struct *next;
2771 limbo_die_node;
2773 typedef struct skeleton_chain_struct
2775 dw_die_ref old_die;
2776 dw_die_ref new_die;
2777 struct skeleton_chain_struct *parent;
2779 skeleton_chain_node;
2781 /* Define a macro which returns nonzero for a TYPE_DECL which was
2782 implicitly generated for a type.
2784 Note that, unlike the C front-end (which generates a NULL named
2785 TYPE_DECL node for each complete tagged type, each array type,
2786 and each function type node created) the C++ front-end generates
2787 a _named_ TYPE_DECL node for each tagged type node created.
2788 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2789 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2790 front-end, but for each type, tagged or not. */
2792 #define TYPE_DECL_IS_STUB(decl) \
2793 (DECL_NAME (decl) == NULL_TREE \
2794 || (DECL_ARTIFICIAL (decl) \
2795 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2796 /* This is necessary for stub decls that \
2797 appear in nested inline functions. */ \
2798 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2799 && (decl_ultimate_origin (decl) \
2800 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2802 /* Information concerning the compilation unit's programming
2803 language, and compiler version. */
2805 /* Fixed size portion of the DWARF compilation unit header. */
2806 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2807 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2809 /* Fixed size portion of the DWARF comdat type unit header. */
2810 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2811 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2812 + DWARF_OFFSET_SIZE)
2814 /* Fixed size portion of public names info. */
2815 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2817 /* Fixed size portion of the address range info. */
2818 #define DWARF_ARANGES_HEADER_SIZE \
2819 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2820 DWARF2_ADDR_SIZE * 2) \
2821 - DWARF_INITIAL_LENGTH_SIZE)
2823 /* Size of padding portion in the address range info. It must be
2824 aligned to twice the pointer size. */
2825 #define DWARF_ARANGES_PAD_SIZE \
2826 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2827 DWARF2_ADDR_SIZE * 2) \
2828 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2830 /* Use assembler line directives if available. */
2831 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2832 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2833 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2834 #else
2835 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2836 #endif
2837 #endif
2839 /* Minimum line offset in a special line info. opcode.
2840 This value was chosen to give a reasonable range of values. */
2841 #define DWARF_LINE_BASE -10
2843 /* First special line opcode - leave room for the standard opcodes. */
2844 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2846 /* Range of line offsets in a special line info. opcode. */
2847 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2849 /* Flag that indicates the initial value of the is_stmt_start flag.
2850 In the present implementation, we do not mark any lines as
2851 the beginning of a source statement, because that information
2852 is not made available by the GCC front-end. */
2853 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2855 /* Maximum number of operations per instruction bundle. */
2856 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2857 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2858 #endif
2860 /* This location is used by calc_die_sizes() to keep track
2861 the offset of each DIE within the .debug_info section. */
2862 static unsigned long next_die_offset;
2864 /* Record the root of the DIE's built for the current compilation unit. */
2865 static GTY(()) dw_die_ref single_comp_unit_die;
2867 /* A list of type DIEs that have been separated into comdat sections. */
2868 static GTY(()) comdat_type_node *comdat_type_list;
2870 /* A list of CU DIEs that have been separated. */
2871 static GTY(()) limbo_die_node *cu_die_list;
2873 /* A list of DIEs with a NULL parent waiting to be relocated. */
2874 static GTY(()) limbo_die_node *limbo_die_list;
2876 /* A list of DIEs for which we may have to generate
2877 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2878 static GTY(()) limbo_die_node *deferred_asm_name;
2880 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2882 typedef const char *compare_type;
2884 static hashval_t hash (dwarf_file_data *);
2885 static bool equal (dwarf_file_data *, const char *);
2888 /* Filenames referenced by this compilation unit. */
2889 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2891 struct decl_die_hasher : ggc_ptr_hash<die_node>
2893 typedef tree compare_type;
2895 static hashval_t hash (die_node *);
2896 static bool equal (die_node *, tree);
2898 /* A hash table of references to DIE's that describe declarations.
2899 The key is a DECL_UID() which is a unique number identifying each decl. */
2900 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2902 struct block_die_hasher : ggc_ptr_hash<die_struct>
2904 static hashval_t hash (die_struct *);
2905 static bool equal (die_struct *, die_struct *);
2908 /* A hash table of references to DIE's that describe COMMON blocks.
2909 The key is DECL_UID() ^ die_parent. */
2910 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2912 typedef struct GTY(()) die_arg_entry_struct {
2913 dw_die_ref die;
2914 tree arg;
2915 } die_arg_entry;
2918 /* Node of the variable location list. */
2919 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2920 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2921 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2922 in mode of the EXPR_LIST node and first EXPR_LIST operand
2923 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2924 location or NULL for padding. For larger bitsizes,
2925 mode is 0 and first operand is a CONCAT with bitsize
2926 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2927 NULL as second operand. */
2928 rtx GTY (()) loc;
2929 const char * GTY (()) label;
2930 struct var_loc_node * GTY (()) next;
2933 /* Variable location list. */
2934 struct GTY ((for_user)) var_loc_list_def {
2935 struct var_loc_node * GTY (()) first;
2937 /* Pointer to the last but one or last element of the
2938 chained list. If the list is empty, both first and
2939 last are NULL, if the list contains just one node
2940 or the last node certainly is not redundant, it points
2941 to the last node, otherwise points to the last but one.
2942 Do not mark it for GC because it is marked through the chain. */
2943 struct var_loc_node * GTY ((skip ("%h"))) last;
2945 /* Pointer to the last element before section switch,
2946 if NULL, either sections weren't switched or first
2947 is after section switch. */
2948 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2950 /* DECL_UID of the variable decl. */
2951 unsigned int decl_id;
2953 typedef struct var_loc_list_def var_loc_list;
2955 /* Call argument location list. */
2956 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2957 rtx GTY (()) call_arg_loc_note;
2958 const char * GTY (()) label;
2959 tree GTY (()) block;
2960 bool tail_call_p;
2961 rtx GTY (()) symbol_ref;
2962 struct call_arg_loc_node * GTY (()) next;
2966 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2968 typedef const_tree compare_type;
2970 static hashval_t hash (var_loc_list *);
2971 static bool equal (var_loc_list *, const_tree);
2974 /* Table of decl location linked lists. */
2975 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2977 /* Head and tail of call_arg_loc chain. */
2978 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2979 static struct call_arg_loc_node *call_arg_loc_last;
2981 /* Number of call sites in the current function. */
2982 static int call_site_count = -1;
2983 /* Number of tail call sites in the current function. */
2984 static int tail_call_site_count = -1;
2986 /* A cached location list. */
2987 struct GTY ((for_user)) cached_dw_loc_list_def {
2988 /* The DECL_UID of the decl that this entry describes. */
2989 unsigned int decl_id;
2991 /* The cached location list. */
2992 dw_loc_list_ref loc_list;
2994 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2996 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2999 typedef const_tree compare_type;
3001 static hashval_t hash (cached_dw_loc_list *);
3002 static bool equal (cached_dw_loc_list *, const_tree);
3005 /* Table of cached location lists. */
3006 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3008 /* A pointer to the base of a list of references to DIE's that
3009 are uniquely identified by their tag, presence/absence of
3010 children DIE's, and list of attribute/value pairs. */
3011 static GTY((length ("abbrev_die_table_allocated")))
3012 dw_die_ref *abbrev_die_table;
3014 /* Number of elements currently allocated for abbrev_die_table. */
3015 static GTY(()) unsigned abbrev_die_table_allocated;
3017 /* Number of elements in abbrev_die_table currently in use. */
3018 static GTY(()) unsigned abbrev_die_table_in_use;
3020 /* A hash map to remember the stack usage for DWARF procedures. The value
3021 stored is the stack size difference between before the DWARF procedure
3022 invokation and after it returned. In other words, for a DWARF procedure
3023 that consumes N stack slots and that pushes M ones, this stores M - N. */
3024 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3026 /* Size (in elements) of increments by which we may expand the
3027 abbrev_die_table. */
3028 #define ABBREV_DIE_TABLE_INCREMENT 256
3030 /* A global counter for generating labels for line number data. */
3031 static unsigned int line_info_label_num;
3033 /* The current table to which we should emit line number information
3034 for the current function. This will be set up at the beginning of
3035 assembly for the function. */
3036 static GTY(()) dw_line_info_table *cur_line_info_table;
3038 /* The two default tables of line number info. */
3039 static GTY(()) dw_line_info_table *text_section_line_info;
3040 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3042 /* The set of all non-default tables of line number info. */
3043 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3045 /* A flag to tell pubnames/types export if there is an info section to
3046 refer to. */
3047 static bool info_section_emitted;
3049 /* A pointer to the base of a table that contains a list of publicly
3050 accessible names. */
3051 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3053 /* A pointer to the base of a table that contains a list of publicly
3054 accessible types. */
3055 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3057 /* A pointer to the base of a table that contains a list of macro
3058 defines/undefines (and file start/end markers). */
3059 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3061 /* True if .debug_macinfo or .debug_macros section is going to be
3062 emitted. */
3063 #define have_macinfo \
3064 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3065 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3066 && !macinfo_table->is_empty ())
3068 /* Array of dies for which we should generate .debug_ranges info. */
3069 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3071 /* Number of elements currently allocated for ranges_table. */
3072 static GTY(()) unsigned ranges_table_allocated;
3074 /* Number of elements in ranges_table currently in use. */
3075 static GTY(()) unsigned ranges_table_in_use;
3077 /* Array of pairs of labels referenced in ranges_table. */
3078 static GTY ((length ("ranges_by_label_allocated")))
3079 dw_ranges_by_label *ranges_by_label;
3081 /* Number of elements currently allocated for ranges_by_label. */
3082 static GTY(()) unsigned ranges_by_label_allocated;
3084 /* Number of elements in ranges_by_label currently in use. */
3085 static GTY(()) unsigned ranges_by_label_in_use;
3087 /* Size (in elements) of increments by which we may expand the
3088 ranges_table. */
3089 #define RANGES_TABLE_INCREMENT 64
3091 /* Whether we have location lists that need outputting */
3092 static GTY(()) bool have_location_lists;
3094 /* Unique label counter. */
3095 static GTY(()) unsigned int loclabel_num;
3097 /* Unique label counter for point-of-call tables. */
3098 static GTY(()) unsigned int poc_label_num;
3100 /* The last file entry emitted by maybe_emit_file(). */
3101 static GTY(()) struct dwarf_file_data * last_emitted_file;
3103 /* Number of internal labels generated by gen_internal_sym(). */
3104 static GTY(()) int label_num;
3106 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3108 /* Instances of generic types for which we need to generate debug
3109 info that describe their generic parameters and arguments. That
3110 generation needs to happen once all types are properly laid out so
3111 we do it at the end of compilation. */
3112 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3114 /* Offset from the "steady-state frame pointer" to the frame base,
3115 within the current function. */
3116 static HOST_WIDE_INT frame_pointer_fb_offset;
3117 static bool frame_pointer_fb_offset_valid;
3119 static vec<dw_die_ref> base_types;
3121 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3122 once all arguments are parsed. */
3123 static vec<dw_die_ref> *string_types;
3125 /* Flags to represent a set of attribute classes for attributes that represent
3126 a scalar value (bounds, pointers, ...). */
3127 enum dw_scalar_form
3129 dw_scalar_form_constant = 0x01,
3130 dw_scalar_form_exprloc = 0x02,
3131 dw_scalar_form_reference = 0x04
3134 /* Forward declarations for functions defined in this file. */
3136 static int is_pseudo_reg (const_rtx);
3137 static tree type_main_variant (tree);
3138 static int is_tagged_type (const_tree);
3139 static const char *dwarf_tag_name (unsigned);
3140 static const char *dwarf_attr_name (unsigned);
3141 static const char *dwarf_form_name (unsigned);
3142 static tree decl_ultimate_origin (const_tree);
3143 static tree decl_class_context (tree);
3144 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3145 static inline enum dw_val_class AT_class (dw_attr_node *);
3146 static inline unsigned int AT_index (dw_attr_node *);
3147 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3148 static inline unsigned AT_flag (dw_attr_node *);
3149 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3150 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3151 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3152 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3153 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3154 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3155 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3156 unsigned int, unsigned char *);
3157 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3158 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3159 static inline const char *AT_string (dw_attr_node *);
3160 static enum dwarf_form AT_string_form (dw_attr_node *);
3161 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3162 static void add_AT_specification (dw_die_ref, dw_die_ref);
3163 static inline dw_die_ref AT_ref (dw_attr_node *);
3164 static inline int AT_ref_external (dw_attr_node *);
3165 static inline void set_AT_ref_external (dw_attr_node *, int);
3166 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3167 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3168 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3169 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3170 dw_loc_list_ref);
3171 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3172 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3173 static void remove_addr_table_entry (addr_table_entry *);
3174 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3175 static inline rtx AT_addr (dw_attr_node *);
3176 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3177 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3178 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3179 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3180 unsigned HOST_WIDE_INT);
3181 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3182 unsigned long, bool);
3183 static inline const char *AT_lbl (dw_attr_node *);
3184 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3185 static const char *get_AT_low_pc (dw_die_ref);
3186 static const char *get_AT_hi_pc (dw_die_ref);
3187 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3188 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3189 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3190 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3191 static bool is_cxx (void);
3192 static bool is_fortran (void);
3193 static bool is_ada (void);
3194 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3195 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3196 static void add_child_die (dw_die_ref, dw_die_ref);
3197 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3198 static dw_die_ref lookup_type_die (tree);
3199 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3200 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3201 static void equate_type_number_to_die (tree, dw_die_ref);
3202 static dw_die_ref lookup_decl_die (tree);
3203 static var_loc_list *lookup_decl_loc (const_tree);
3204 static void equate_decl_number_to_die (tree, dw_die_ref);
3205 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3206 static void print_spaces (FILE *);
3207 static void print_die (dw_die_ref, FILE *);
3208 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3209 static dw_die_ref pop_compile_unit (dw_die_ref);
3210 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3211 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3212 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3213 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3214 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3215 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3216 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3217 struct md5_ctx *, int *);
3218 struct checksum_attributes;
3219 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3220 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3221 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3222 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3223 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3224 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3225 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3226 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3227 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3228 static void compute_section_prefix (dw_die_ref);
3229 static int is_type_die (dw_die_ref);
3230 static int is_comdat_die (dw_die_ref);
3231 static int is_symbol_die (dw_die_ref);
3232 static inline bool is_template_instantiation (dw_die_ref);
3233 static void assign_symbol_names (dw_die_ref);
3234 static void break_out_includes (dw_die_ref);
3235 static int is_declaration_die (dw_die_ref);
3236 static int should_move_die_to_comdat (dw_die_ref);
3237 static dw_die_ref clone_as_declaration (dw_die_ref);
3238 static dw_die_ref clone_die (dw_die_ref);
3239 static dw_die_ref clone_tree (dw_die_ref);
3240 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3241 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3242 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3243 static dw_die_ref generate_skeleton (dw_die_ref);
3244 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3245 dw_die_ref,
3246 dw_die_ref);
3247 static void break_out_comdat_types (dw_die_ref);
3248 static void copy_decls_for_unworthy_types (dw_die_ref);
3250 static void add_sibling_attributes (dw_die_ref);
3251 static void output_location_lists (dw_die_ref);
3252 static int constant_size (unsigned HOST_WIDE_INT);
3253 static unsigned long size_of_die (dw_die_ref);
3254 static void calc_die_sizes (dw_die_ref);
3255 static void calc_base_type_die_sizes (void);
3256 static void mark_dies (dw_die_ref);
3257 static void unmark_dies (dw_die_ref);
3258 static void unmark_all_dies (dw_die_ref);
3259 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3260 static unsigned long size_of_aranges (void);
3261 static enum dwarf_form value_format (dw_attr_node *);
3262 static void output_value_format (dw_attr_node *);
3263 static void output_abbrev_section (void);
3264 static void output_die_abbrevs (unsigned long, dw_die_ref);
3265 static void output_die_symbol (dw_die_ref);
3266 static void output_die (dw_die_ref);
3267 static void output_compilation_unit_header (void);
3268 static void output_comp_unit (dw_die_ref, int);
3269 static void output_comdat_type_unit (comdat_type_node *);
3270 static const char *dwarf2_name (tree, int);
3271 static void add_pubname (tree, dw_die_ref);
3272 static void add_enumerator_pubname (const char *, dw_die_ref);
3273 static void add_pubname_string (const char *, dw_die_ref);
3274 static void add_pubtype (tree, dw_die_ref);
3275 static void output_pubnames (vec<pubname_entry, va_gc> *);
3276 static void output_aranges (void);
3277 static unsigned int add_ranges_num (int);
3278 static unsigned int add_ranges (const_tree);
3279 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3280 bool *, bool);
3281 static void output_ranges (void);
3282 static dw_line_info_table *new_line_info_table (void);
3283 static void output_line_info (bool);
3284 static void output_file_names (void);
3285 static dw_die_ref base_type_die (tree, bool);
3286 static int is_base_type (tree);
3287 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3288 static int decl_quals (const_tree);
3289 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3290 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3291 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3292 static int type_is_enum (const_tree);
3293 static unsigned int dbx_reg_number (const_rtx);
3294 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3295 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3296 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3297 enum var_init_status);
3298 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3299 enum var_init_status);
3300 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3301 enum var_init_status);
3302 static int is_based_loc (const_rtx);
3303 static bool resolve_one_addr (rtx *);
3304 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3305 enum var_init_status);
3306 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3307 enum var_init_status);
3308 struct loc_descr_context;
3309 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3310 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3311 static dw_loc_list_ref loc_list_from_tree (tree, int,
3312 const struct loc_descr_context *);
3313 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3314 const struct loc_descr_context *);
3315 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3316 static tree field_type (const_tree);
3317 static unsigned int simple_type_align_in_bits (const_tree);
3318 static unsigned int simple_decl_align_in_bits (const_tree);
3319 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3320 struct vlr_context;
3321 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3322 HOST_WIDE_INT *);
3323 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3324 dw_loc_list_ref);
3325 static void add_data_member_location_attribute (dw_die_ref, tree,
3326 struct vlr_context *);
3327 static bool add_const_value_attribute (dw_die_ref, rtx);
3328 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3329 static void insert_wide_int (const wide_int &, unsigned char *, int);
3330 static void insert_float (const_rtx, unsigned char *);
3331 static rtx rtl_for_decl_location (tree);
3332 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3333 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3334 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3335 static void add_name_attribute (dw_die_ref, const char *);
3336 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3337 static void add_comp_dir_attribute (dw_die_ref);
3338 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3339 const struct loc_descr_context *);
3340 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3341 const struct loc_descr_context *);
3342 static void add_subscript_info (dw_die_ref, tree, bool);
3343 static void add_byte_size_attribute (dw_die_ref, tree);
3344 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3345 struct vlr_context *);
3346 static void add_bit_size_attribute (dw_die_ref, tree);
3347 static void add_prototyped_attribute (dw_die_ref, tree);
3348 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3349 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3350 static void add_src_coords_attributes (dw_die_ref, tree);
3351 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3352 static void add_discr_value (dw_die_ref, dw_discr_value *);
3353 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3354 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3355 static void push_decl_scope (tree);
3356 static void pop_decl_scope (void);
3357 static dw_die_ref scope_die_for (tree, dw_die_ref);
3358 static inline int local_scope_p (dw_die_ref);
3359 static inline int class_scope_p (dw_die_ref);
3360 static inline int class_or_namespace_scope_p (dw_die_ref);
3361 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3362 static void add_calling_convention_attribute (dw_die_ref, tree);
3363 static const char *type_tag (const_tree);
3364 static tree member_declared_type (const_tree);
3365 #if 0
3366 static const char *decl_start_label (tree);
3367 #endif
3368 static void gen_array_type_die (tree, dw_die_ref);
3369 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3370 #if 0
3371 static void gen_entry_point_die (tree, dw_die_ref);
3372 #endif
3373 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3374 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3375 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3376 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3377 static void gen_formal_types_die (tree, dw_die_ref);
3378 static void gen_subprogram_die (tree, dw_die_ref);
3379 static void gen_variable_die (tree, tree, dw_die_ref);
3380 static void gen_const_die (tree, dw_die_ref);
3381 static void gen_label_die (tree, dw_die_ref);
3382 static void gen_lexical_block_die (tree, dw_die_ref);
3383 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3384 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3385 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3386 static dw_die_ref gen_compile_unit_die (const char *);
3387 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3388 static void gen_member_die (tree, dw_die_ref);
3389 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3390 enum debug_info_usage);
3391 static void gen_subroutine_type_die (tree, dw_die_ref);
3392 static void gen_typedef_die (tree, dw_die_ref);
3393 static void gen_type_die (tree, dw_die_ref);
3394 static void gen_block_die (tree, dw_die_ref);
3395 static void decls_for_scope (tree, dw_die_ref);
3396 static bool is_naming_typedef_decl (const_tree);
3397 static inline dw_die_ref get_context_die (tree);
3398 static void gen_namespace_die (tree, dw_die_ref);
3399 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3400 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3401 static dw_die_ref force_decl_die (tree);
3402 static dw_die_ref force_type_die (tree);
3403 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3404 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3405 static struct dwarf_file_data * lookup_filename (const char *);
3406 static void retry_incomplete_types (void);
3407 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3408 static void gen_generic_params_dies (tree);
3409 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3410 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3411 static void splice_child_die (dw_die_ref, dw_die_ref);
3412 static int file_info_cmp (const void *, const void *);
3413 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3414 const char *, const char *);
3415 static void output_loc_list (dw_loc_list_ref);
3416 static char *gen_internal_sym (const char *);
3417 static bool want_pubnames (void);
3419 static void prune_unmark_dies (dw_die_ref);
3420 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3421 static void prune_unused_types_mark (dw_die_ref, int);
3422 static void prune_unused_types_walk (dw_die_ref);
3423 static void prune_unused_types_walk_attribs (dw_die_ref);
3424 static void prune_unused_types_prune (dw_die_ref);
3425 static void prune_unused_types (void);
3426 static int maybe_emit_file (struct dwarf_file_data *fd);
3427 static inline const char *AT_vms_delta1 (dw_attr_node *);
3428 static inline const char *AT_vms_delta2 (dw_attr_node *);
3429 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3430 const char *, const char *);
3431 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3432 static void gen_remaining_tmpl_value_param_die_attribute (void);
3433 static bool generic_type_p (tree);
3434 static void schedule_generic_params_dies_gen (tree t);
3435 static void gen_scheduled_generic_parms_dies (void);
3437 static const char *comp_dir_string (void);
3439 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3441 /* enum for tracking thread-local variables whose address is really an offset
3442 relative to the TLS pointer, which will need link-time relocation, but will
3443 not need relocation by the DWARF consumer. */
3445 enum dtprel_bool
3447 dtprel_false = 0,
3448 dtprel_true = 1
3451 /* Return the operator to use for an address of a variable. For dtprel_true, we
3452 use DW_OP_const*. For regular variables, which need both link-time
3453 relocation and consumer-level relocation (e.g., to account for shared objects
3454 loaded at a random address), we use DW_OP_addr*. */
3456 static inline enum dwarf_location_atom
3457 dw_addr_op (enum dtprel_bool dtprel)
3459 if (dtprel == dtprel_true)
3460 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3461 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3462 else
3463 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3466 /* Return a pointer to a newly allocated address location description. If
3467 dwarf_split_debug_info is true, then record the address with the appropriate
3468 relocation. */
3469 static inline dw_loc_descr_ref
3470 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3472 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3474 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3475 ref->dw_loc_oprnd1.v.val_addr = addr;
3476 ref->dtprel = dtprel;
3477 if (dwarf_split_debug_info)
3478 ref->dw_loc_oprnd1.val_entry
3479 = add_addr_table_entry (addr,
3480 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3481 else
3482 ref->dw_loc_oprnd1.val_entry = NULL;
3484 return ref;
3487 /* Section names used to hold DWARF debugging information. */
3489 #ifndef DEBUG_INFO_SECTION
3490 #define DEBUG_INFO_SECTION ".debug_info"
3491 #endif
3492 #ifndef DEBUG_DWO_INFO_SECTION
3493 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3494 #endif
3495 #ifndef DEBUG_ABBREV_SECTION
3496 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3497 #endif
3498 #ifndef DEBUG_DWO_ABBREV_SECTION
3499 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3500 #endif
3501 #ifndef DEBUG_ARANGES_SECTION
3502 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3503 #endif
3504 #ifndef DEBUG_ADDR_SECTION
3505 #define DEBUG_ADDR_SECTION ".debug_addr"
3506 #endif
3507 #ifndef DEBUG_MACINFO_SECTION
3508 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3509 #endif
3510 #ifndef DEBUG_DWO_MACINFO_SECTION
3511 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3512 #endif
3513 #ifndef DEBUG_DWO_MACRO_SECTION
3514 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3515 #endif
3516 #ifndef DEBUG_MACRO_SECTION
3517 #define DEBUG_MACRO_SECTION ".debug_macro"
3518 #endif
3519 #ifndef DEBUG_LINE_SECTION
3520 #define DEBUG_LINE_SECTION ".debug_line"
3521 #endif
3522 #ifndef DEBUG_DWO_LINE_SECTION
3523 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3524 #endif
3525 #ifndef DEBUG_LOC_SECTION
3526 #define DEBUG_LOC_SECTION ".debug_loc"
3527 #endif
3528 #ifndef DEBUG_DWO_LOC_SECTION
3529 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3530 #endif
3531 #ifndef DEBUG_PUBNAMES_SECTION
3532 #define DEBUG_PUBNAMES_SECTION \
3533 ((debug_generate_pub_sections == 2) \
3534 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3535 #endif
3536 #ifndef DEBUG_PUBTYPES_SECTION
3537 #define DEBUG_PUBTYPES_SECTION \
3538 ((debug_generate_pub_sections == 2) \
3539 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3540 #endif
3541 #ifndef DEBUG_STR_OFFSETS_SECTION
3542 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3543 #endif
3544 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3545 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3546 #endif
3547 #ifndef DEBUG_STR_DWO_SECTION
3548 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3549 #endif
3550 #ifndef DEBUG_STR_SECTION
3551 #define DEBUG_STR_SECTION ".debug_str"
3552 #endif
3553 #ifndef DEBUG_RANGES_SECTION
3554 #define DEBUG_RANGES_SECTION ".debug_ranges"
3555 #endif
3557 /* Standard ELF section names for compiled code and data. */
3558 #ifndef TEXT_SECTION_NAME
3559 #define TEXT_SECTION_NAME ".text"
3560 #endif
3562 /* Section flags for .debug_str section. */
3563 #define DEBUG_STR_SECTION_FLAGS \
3564 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3565 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3566 : SECTION_DEBUG)
3568 /* Section flags for .debug_str.dwo section. */
3569 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3571 /* Labels we insert at beginning sections we can reference instead of
3572 the section names themselves. */
3574 #ifndef TEXT_SECTION_LABEL
3575 #define TEXT_SECTION_LABEL "Ltext"
3576 #endif
3577 #ifndef COLD_TEXT_SECTION_LABEL
3578 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3579 #endif
3580 #ifndef DEBUG_LINE_SECTION_LABEL
3581 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3582 #endif
3583 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3584 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3585 #endif
3586 #ifndef DEBUG_INFO_SECTION_LABEL
3587 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3588 #endif
3589 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3590 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3591 #endif
3592 #ifndef DEBUG_ABBREV_SECTION_LABEL
3593 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3594 #endif
3595 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3596 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3597 #endif
3598 #ifndef DEBUG_ADDR_SECTION_LABEL
3599 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3600 #endif
3601 #ifndef DEBUG_LOC_SECTION_LABEL
3602 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3603 #endif
3604 #ifndef DEBUG_RANGES_SECTION_LABEL
3605 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3606 #endif
3607 #ifndef DEBUG_MACINFO_SECTION_LABEL
3608 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3609 #endif
3610 #ifndef DEBUG_MACRO_SECTION_LABEL
3611 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3612 #endif
3613 #define SKELETON_COMP_DIE_ABBREV 1
3614 #define SKELETON_TYPE_DIE_ABBREV 2
3616 /* Definitions of defaults for formats and names of various special
3617 (artificial) labels which may be generated within this file (when the -g
3618 options is used and DWARF2_DEBUGGING_INFO is in effect.
3619 If necessary, these may be overridden from within the tm.h file, but
3620 typically, overriding these defaults is unnecessary. */
3622 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3623 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3624 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3625 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3626 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3627 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3628 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3629 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3630 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3631 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3632 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3633 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3634 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3635 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3637 #ifndef TEXT_END_LABEL
3638 #define TEXT_END_LABEL "Letext"
3639 #endif
3640 #ifndef COLD_END_LABEL
3641 #define COLD_END_LABEL "Letext_cold"
3642 #endif
3643 #ifndef BLOCK_BEGIN_LABEL
3644 #define BLOCK_BEGIN_LABEL "LBB"
3645 #endif
3646 #ifndef BLOCK_END_LABEL
3647 #define BLOCK_END_LABEL "LBE"
3648 #endif
3649 #ifndef LINE_CODE_LABEL
3650 #define LINE_CODE_LABEL "LM"
3651 #endif
3654 /* Return the root of the DIE's built for the current compilation unit. */
3655 static dw_die_ref
3656 comp_unit_die (void)
3658 if (!single_comp_unit_die)
3659 single_comp_unit_die = gen_compile_unit_die (NULL);
3660 return single_comp_unit_die;
3663 /* We allow a language front-end to designate a function that is to be
3664 called to "demangle" any name before it is put into a DIE. */
3666 static const char *(*demangle_name_func) (const char *);
3668 void
3669 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3671 demangle_name_func = func;
3674 /* Test if rtl node points to a pseudo register. */
3676 static inline int
3677 is_pseudo_reg (const_rtx rtl)
3679 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3680 || (GET_CODE (rtl) == SUBREG
3681 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3684 /* Return a reference to a type, with its const and volatile qualifiers
3685 removed. */
3687 static inline tree
3688 type_main_variant (tree type)
3690 type = TYPE_MAIN_VARIANT (type);
3692 /* ??? There really should be only one main variant among any group of
3693 variants of a given type (and all of the MAIN_VARIANT values for all
3694 members of the group should point to that one type) but sometimes the C
3695 front-end messes this up for array types, so we work around that bug
3696 here. */
3697 if (TREE_CODE (type) == ARRAY_TYPE)
3698 while (type != TYPE_MAIN_VARIANT (type))
3699 type = TYPE_MAIN_VARIANT (type);
3701 return type;
3704 /* Return nonzero if the given type node represents a tagged type. */
3706 static inline int
3707 is_tagged_type (const_tree type)
3709 enum tree_code code = TREE_CODE (type);
3711 return (code == RECORD_TYPE || code == UNION_TYPE
3712 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3715 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3717 static void
3718 get_ref_die_offset_label (char *label, dw_die_ref ref)
3720 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3723 /* Return die_offset of a DIE reference to a base type. */
3725 static unsigned long int
3726 get_base_type_offset (dw_die_ref ref)
3728 if (ref->die_offset)
3729 return ref->die_offset;
3730 if (comp_unit_die ()->die_abbrev)
3732 calc_base_type_die_sizes ();
3733 gcc_assert (ref->die_offset);
3735 return ref->die_offset;
3738 /* Return die_offset of a DIE reference other than base type. */
3740 static unsigned long int
3741 get_ref_die_offset (dw_die_ref ref)
3743 gcc_assert (ref->die_offset);
3744 return ref->die_offset;
3747 /* Convert a DIE tag into its string name. */
3749 static const char *
3750 dwarf_tag_name (unsigned int tag)
3752 const char *name = get_DW_TAG_name (tag);
3754 if (name != NULL)
3755 return name;
3757 return "DW_TAG_<unknown>";
3760 /* Convert a DWARF attribute code into its string name. */
3762 static const char *
3763 dwarf_attr_name (unsigned int attr)
3765 const char *name;
3767 switch (attr)
3769 #if VMS_DEBUGGING_INFO
3770 case DW_AT_HP_prologue:
3771 return "DW_AT_HP_prologue";
3772 #else
3773 case DW_AT_MIPS_loop_unroll_factor:
3774 return "DW_AT_MIPS_loop_unroll_factor";
3775 #endif
3777 #if VMS_DEBUGGING_INFO
3778 case DW_AT_HP_epilogue:
3779 return "DW_AT_HP_epilogue";
3780 #else
3781 case DW_AT_MIPS_stride:
3782 return "DW_AT_MIPS_stride";
3783 #endif
3786 name = get_DW_AT_name (attr);
3788 if (name != NULL)
3789 return name;
3791 return "DW_AT_<unknown>";
3794 /* Convert a DWARF value form code into its string name. */
3796 static const char *
3797 dwarf_form_name (unsigned int form)
3799 const char *name = get_DW_FORM_name (form);
3801 if (name != NULL)
3802 return name;
3804 return "DW_FORM_<unknown>";
3807 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3808 instance of an inlined instance of a decl which is local to an inline
3809 function, so we have to trace all of the way back through the origin chain
3810 to find out what sort of node actually served as the original seed for the
3811 given block. */
3813 static tree
3814 decl_ultimate_origin (const_tree decl)
3816 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3817 return NULL_TREE;
3819 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3820 we're trying to output the abstract instance of this function. */
3821 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3822 return NULL_TREE;
3824 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3825 most distant ancestor, this should never happen. */
3826 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3828 return DECL_ABSTRACT_ORIGIN (decl);
3831 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3832 of a virtual function may refer to a base class, so we check the 'this'
3833 parameter. */
3835 static tree
3836 decl_class_context (tree decl)
3838 tree context = NULL_TREE;
3840 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3841 context = DECL_CONTEXT (decl);
3842 else
3843 context = TYPE_MAIN_VARIANT
3844 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3846 if (context && !TYPE_P (context))
3847 context = NULL_TREE;
3849 return context;
3852 /* Add an attribute/value pair to a DIE. */
3854 static inline void
3855 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3857 /* Maybe this should be an assert? */
3858 if (die == NULL)
3859 return;
3861 vec_safe_reserve (die->die_attr, 1);
3862 vec_safe_push (die->die_attr, *attr);
3865 static inline enum dw_val_class
3866 AT_class (dw_attr_node *a)
3868 return a->dw_attr_val.val_class;
3871 /* Return the index for any attribute that will be referenced with a
3872 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3873 are stored in dw_attr_val.v.val_str for reference counting
3874 pruning. */
3876 static inline unsigned int
3877 AT_index (dw_attr_node *a)
3879 if (AT_class (a) == dw_val_class_str)
3880 return a->dw_attr_val.v.val_str->index;
3881 else if (a->dw_attr_val.val_entry != NULL)
3882 return a->dw_attr_val.val_entry->index;
3883 return NOT_INDEXED;
3886 /* Add a flag value attribute to a DIE. */
3888 static inline void
3889 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3891 dw_attr_node attr;
3893 attr.dw_attr = attr_kind;
3894 attr.dw_attr_val.val_class = dw_val_class_flag;
3895 attr.dw_attr_val.val_entry = NULL;
3896 attr.dw_attr_val.v.val_flag = flag;
3897 add_dwarf_attr (die, &attr);
3900 static inline unsigned
3901 AT_flag (dw_attr_node *a)
3903 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3904 return a->dw_attr_val.v.val_flag;
3907 /* Add a signed integer attribute value to a DIE. */
3909 static inline void
3910 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3912 dw_attr_node attr;
3914 attr.dw_attr = attr_kind;
3915 attr.dw_attr_val.val_class = dw_val_class_const;
3916 attr.dw_attr_val.val_entry = NULL;
3917 attr.dw_attr_val.v.val_int = int_val;
3918 add_dwarf_attr (die, &attr);
3921 static inline HOST_WIDE_INT
3922 AT_int (dw_attr_node *a)
3924 gcc_assert (a && AT_class (a) == dw_val_class_const);
3925 return a->dw_attr_val.v.val_int;
3928 /* Add an unsigned integer attribute value to a DIE. */
3930 static inline void
3931 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3932 unsigned HOST_WIDE_INT unsigned_val)
3934 dw_attr_node attr;
3936 attr.dw_attr = attr_kind;
3937 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3938 attr.dw_attr_val.val_entry = NULL;
3939 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3940 add_dwarf_attr (die, &attr);
3943 static inline unsigned HOST_WIDE_INT
3944 AT_unsigned (dw_attr_node *a)
3946 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3947 return a->dw_attr_val.v.val_unsigned;
3950 /* Add an unsigned wide integer attribute value to a DIE. */
3952 static inline void
3953 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3954 const wide_int& w)
3956 dw_attr_node attr;
3958 attr.dw_attr = attr_kind;
3959 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3960 attr.dw_attr_val.val_entry = NULL;
3961 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3962 *attr.dw_attr_val.v.val_wide = w;
3963 add_dwarf_attr (die, &attr);
3966 /* Add an unsigned double integer attribute value to a DIE. */
3968 static inline void
3969 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3970 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3972 dw_attr_node attr;
3974 attr.dw_attr = attr_kind;
3975 attr.dw_attr_val.val_class = dw_val_class_const_double;
3976 attr.dw_attr_val.val_entry = NULL;
3977 attr.dw_attr_val.v.val_double.high = high;
3978 attr.dw_attr_val.v.val_double.low = low;
3979 add_dwarf_attr (die, &attr);
3982 /* Add a floating point attribute value to a DIE and return it. */
3984 static inline void
3985 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3986 unsigned int length, unsigned int elt_size, unsigned char *array)
3988 dw_attr_node attr;
3990 attr.dw_attr = attr_kind;
3991 attr.dw_attr_val.val_class = dw_val_class_vec;
3992 attr.dw_attr_val.val_entry = NULL;
3993 attr.dw_attr_val.v.val_vec.length = length;
3994 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3995 attr.dw_attr_val.v.val_vec.array = array;
3996 add_dwarf_attr (die, &attr);
3999 /* Add an 8-byte data attribute value to a DIE. */
4001 static inline void
4002 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4003 unsigned char data8[8])
4005 dw_attr_node attr;
4007 attr.dw_attr = attr_kind;
4008 attr.dw_attr_val.val_class = dw_val_class_data8;
4009 attr.dw_attr_val.val_entry = NULL;
4010 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4011 add_dwarf_attr (die, &attr);
4014 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4015 dwarf_split_debug_info, address attributes in dies destined for the
4016 final executable have force_direct set to avoid using indexed
4017 references. */
4019 static inline void
4020 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4021 bool force_direct)
4023 dw_attr_node attr;
4024 char * lbl_id;
4026 lbl_id = xstrdup (lbl_low);
4027 attr.dw_attr = DW_AT_low_pc;
4028 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4029 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4030 if (dwarf_split_debug_info && !force_direct)
4031 attr.dw_attr_val.val_entry
4032 = add_addr_table_entry (lbl_id, ate_kind_label);
4033 else
4034 attr.dw_attr_val.val_entry = NULL;
4035 add_dwarf_attr (die, &attr);
4037 attr.dw_attr = DW_AT_high_pc;
4038 if (dwarf_version < 4)
4039 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4040 else
4041 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4042 lbl_id = xstrdup (lbl_high);
4043 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4044 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4045 && dwarf_split_debug_info && !force_direct)
4046 attr.dw_attr_val.val_entry
4047 = add_addr_table_entry (lbl_id, ate_kind_label);
4048 else
4049 attr.dw_attr_val.val_entry = NULL;
4050 add_dwarf_attr (die, &attr);
4053 /* Hash and equality functions for debug_str_hash. */
4055 hashval_t
4056 indirect_string_hasher::hash (indirect_string_node *x)
4058 return htab_hash_string (x->str);
4061 bool
4062 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4064 return strcmp (x1->str, x2) == 0;
4067 /* Add STR to the given string hash table. */
4069 static struct indirect_string_node *
4070 find_AT_string_in_table (const char *str,
4071 hash_table<indirect_string_hasher> *table)
4073 struct indirect_string_node *node;
4075 indirect_string_node **slot
4076 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4077 if (*slot == NULL)
4079 node = ggc_cleared_alloc<indirect_string_node> ();
4080 node->str = ggc_strdup (str);
4081 *slot = node;
4083 else
4084 node = *slot;
4086 node->refcount++;
4087 return node;
4090 /* Add STR to the indirect string hash table. */
4092 static struct indirect_string_node *
4093 find_AT_string (const char *str)
4095 if (! debug_str_hash)
4096 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4098 return find_AT_string_in_table (str, debug_str_hash);
4101 /* Add a string attribute value to a DIE. */
4103 static inline void
4104 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4106 dw_attr_node attr;
4107 struct indirect_string_node *node;
4109 node = find_AT_string (str);
4111 attr.dw_attr = attr_kind;
4112 attr.dw_attr_val.val_class = dw_val_class_str;
4113 attr.dw_attr_val.val_entry = NULL;
4114 attr.dw_attr_val.v.val_str = node;
4115 add_dwarf_attr (die, &attr);
4118 static inline const char *
4119 AT_string (dw_attr_node *a)
4121 gcc_assert (a && AT_class (a) == dw_val_class_str);
4122 return a->dw_attr_val.v.val_str->str;
4125 /* Call this function directly to bypass AT_string_form's logic to put
4126 the string inline in the die. */
4128 static void
4129 set_indirect_string (struct indirect_string_node *node)
4131 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4132 /* Already indirect is a no op. */
4133 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4135 gcc_assert (node->label);
4136 return;
4138 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4139 ++dw2_string_counter;
4140 node->label = xstrdup (label);
4142 if (!dwarf_split_debug_info)
4144 node->form = DW_FORM_strp;
4145 node->index = NOT_INDEXED;
4147 else
4149 node->form = DW_FORM_GNU_str_index;
4150 node->index = NO_INDEX_ASSIGNED;
4154 /* Find out whether a string should be output inline in DIE
4155 or out-of-line in .debug_str section. */
4157 static enum dwarf_form
4158 find_string_form (struct indirect_string_node *node)
4160 unsigned int len;
4162 if (node->form)
4163 return node->form;
4165 len = strlen (node->str) + 1;
4167 /* If the string is shorter or equal to the size of the reference, it is
4168 always better to put it inline. */
4169 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4170 return node->form = DW_FORM_string;
4172 /* If we cannot expect the linker to merge strings in .debug_str
4173 section, only put it into .debug_str if it is worth even in this
4174 single module. */
4175 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4176 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4177 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4178 return node->form = DW_FORM_string;
4180 set_indirect_string (node);
4182 return node->form;
4185 /* Find out whether the string referenced from the attribute should be
4186 output inline in DIE or out-of-line in .debug_str section. */
4188 static enum dwarf_form
4189 AT_string_form (dw_attr_node *a)
4191 gcc_assert (a && AT_class (a) == dw_val_class_str);
4192 return find_string_form (a->dw_attr_val.v.val_str);
4195 /* Add a DIE reference attribute value to a DIE. */
4197 static inline void
4198 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4200 dw_attr_node attr;
4201 gcc_checking_assert (targ_die != NULL);
4203 /* With LTO we can end up trying to reference something we didn't create
4204 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4205 if (targ_die == NULL)
4206 return;
4208 attr.dw_attr = attr_kind;
4209 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4210 attr.dw_attr_val.val_entry = NULL;
4211 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4212 attr.dw_attr_val.v.val_die_ref.external = 0;
4213 add_dwarf_attr (die, &attr);
4216 /* Change DIE reference REF to point to NEW_DIE instead. */
4218 static inline void
4219 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4221 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4222 ref->dw_attr_val.v.val_die_ref.die = new_die;
4223 ref->dw_attr_val.v.val_die_ref.external = 0;
4226 /* Add an AT_specification attribute to a DIE, and also make the back
4227 pointer from the specification to the definition. */
4229 static inline void
4230 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4232 add_AT_die_ref (die, DW_AT_specification, targ_die);
4233 gcc_assert (!targ_die->die_definition);
4234 targ_die->die_definition = die;
4237 static inline dw_die_ref
4238 AT_ref (dw_attr_node *a)
4240 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4241 return a->dw_attr_val.v.val_die_ref.die;
4244 static inline int
4245 AT_ref_external (dw_attr_node *a)
4247 if (a && AT_class (a) == dw_val_class_die_ref)
4248 return a->dw_attr_val.v.val_die_ref.external;
4250 return 0;
4253 static inline void
4254 set_AT_ref_external (dw_attr_node *a, int i)
4256 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4257 a->dw_attr_val.v.val_die_ref.external = i;
4260 /* Add an FDE reference attribute value to a DIE. */
4262 static inline void
4263 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4265 dw_attr_node attr;
4267 attr.dw_attr = attr_kind;
4268 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4269 attr.dw_attr_val.val_entry = NULL;
4270 attr.dw_attr_val.v.val_fde_index = targ_fde;
4271 add_dwarf_attr (die, &attr);
4274 /* Add a location description attribute value to a DIE. */
4276 static inline void
4277 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4279 dw_attr_node attr;
4281 attr.dw_attr = attr_kind;
4282 attr.dw_attr_val.val_class = dw_val_class_loc;
4283 attr.dw_attr_val.val_entry = NULL;
4284 attr.dw_attr_val.v.val_loc = loc;
4285 add_dwarf_attr (die, &attr);
4288 static inline dw_loc_descr_ref
4289 AT_loc (dw_attr_node *a)
4291 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4292 return a->dw_attr_val.v.val_loc;
4295 static inline void
4296 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4298 dw_attr_node attr;
4300 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4301 return;
4303 attr.dw_attr = attr_kind;
4304 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4305 attr.dw_attr_val.val_entry = NULL;
4306 attr.dw_attr_val.v.val_loc_list = loc_list;
4307 add_dwarf_attr (die, &attr);
4308 have_location_lists = true;
4311 static inline dw_loc_list_ref
4312 AT_loc_list (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 static inline dw_loc_list_ref *
4319 AT_loc_list_ptr (dw_attr_node *a)
4321 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4322 return &a->dw_attr_val.v.val_loc_list;
4325 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4327 static hashval_t hash (addr_table_entry *);
4328 static bool equal (addr_table_entry *, addr_table_entry *);
4331 /* Table of entries into the .debug_addr section. */
4333 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4335 /* Hash an address_table_entry. */
4337 hashval_t
4338 addr_hasher::hash (addr_table_entry *a)
4340 inchash::hash hstate;
4341 switch (a->kind)
4343 case ate_kind_rtx:
4344 hstate.add_int (0);
4345 break;
4346 case ate_kind_rtx_dtprel:
4347 hstate.add_int (1);
4348 break;
4349 case ate_kind_label:
4350 return htab_hash_string (a->addr.label);
4351 default:
4352 gcc_unreachable ();
4354 inchash::add_rtx (a->addr.rtl, hstate);
4355 return hstate.end ();
4358 /* Determine equality for two address_table_entries. */
4360 bool
4361 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4363 if (a1->kind != a2->kind)
4364 return 0;
4365 switch (a1->kind)
4367 case ate_kind_rtx:
4368 case ate_kind_rtx_dtprel:
4369 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4370 case ate_kind_label:
4371 return strcmp (a1->addr.label, a2->addr.label) == 0;
4372 default:
4373 gcc_unreachable ();
4377 /* Initialize an addr_table_entry. */
4379 void
4380 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4382 e->kind = kind;
4383 switch (kind)
4385 case ate_kind_rtx:
4386 case ate_kind_rtx_dtprel:
4387 e->addr.rtl = (rtx) addr;
4388 break;
4389 case ate_kind_label:
4390 e->addr.label = (char *) addr;
4391 break;
4393 e->refcount = 0;
4394 e->index = NO_INDEX_ASSIGNED;
4397 /* Add attr to the address table entry to the table. Defer setting an
4398 index until output time. */
4400 static addr_table_entry *
4401 add_addr_table_entry (void *addr, enum ate_kind kind)
4403 addr_table_entry *node;
4404 addr_table_entry finder;
4406 gcc_assert (dwarf_split_debug_info);
4407 if (! addr_index_table)
4408 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4409 init_addr_table_entry (&finder, kind, addr);
4410 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4412 if (*slot == HTAB_EMPTY_ENTRY)
4414 node = ggc_cleared_alloc<addr_table_entry> ();
4415 init_addr_table_entry (node, kind, addr);
4416 *slot = node;
4418 else
4419 node = *slot;
4421 node->refcount++;
4422 return node;
4425 /* Remove an entry from the addr table by decrementing its refcount.
4426 Strictly, decrementing the refcount would be enough, but the
4427 assertion that the entry is actually in the table has found
4428 bugs. */
4430 static void
4431 remove_addr_table_entry (addr_table_entry *entry)
4433 gcc_assert (dwarf_split_debug_info && addr_index_table);
4434 /* After an index is assigned, the table is frozen. */
4435 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4436 entry->refcount--;
4439 /* Given a location list, remove all addresses it refers to from the
4440 address_table. */
4442 static void
4443 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4445 for (; descr; descr = descr->dw_loc_next)
4446 if (descr->dw_loc_oprnd1.val_entry != NULL)
4448 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4449 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4453 /* A helper function for dwarf2out_finish called through
4454 htab_traverse. Assign an addr_table_entry its index. All entries
4455 must be collected into the table when this function is called,
4456 because the indexing code relies on htab_traverse to traverse nodes
4457 in the same order for each run. */
4460 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4462 addr_table_entry *node = *h;
4464 /* Don't index unreferenced nodes. */
4465 if (node->refcount == 0)
4466 return 1;
4468 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4469 node->index = *index;
4470 *index += 1;
4472 return 1;
4475 /* Add an address constant attribute value to a DIE. When using
4476 dwarf_split_debug_info, address attributes in dies destined for the
4477 final executable should be direct references--setting the parameter
4478 force_direct ensures this behavior. */
4480 static inline void
4481 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4482 bool force_direct)
4484 dw_attr_node attr;
4486 attr.dw_attr = attr_kind;
4487 attr.dw_attr_val.val_class = dw_val_class_addr;
4488 attr.dw_attr_val.v.val_addr = addr;
4489 if (dwarf_split_debug_info && !force_direct)
4490 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4491 else
4492 attr.dw_attr_val.val_entry = NULL;
4493 add_dwarf_attr (die, &attr);
4496 /* Get the RTX from to an address DIE attribute. */
4498 static inline rtx
4499 AT_addr (dw_attr_node *a)
4501 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4502 return a->dw_attr_val.v.val_addr;
4505 /* Add a file attribute value to a DIE. */
4507 static inline void
4508 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4509 struct dwarf_file_data *fd)
4511 dw_attr_node attr;
4513 attr.dw_attr = attr_kind;
4514 attr.dw_attr_val.val_class = dw_val_class_file;
4515 attr.dw_attr_val.val_entry = NULL;
4516 attr.dw_attr_val.v.val_file = fd;
4517 add_dwarf_attr (die, &attr);
4520 /* Get the dwarf_file_data from a file DIE attribute. */
4522 static inline struct dwarf_file_data *
4523 AT_file (dw_attr_node *a)
4525 gcc_assert (a && AT_class (a) == dw_val_class_file);
4526 return a->dw_attr_val.v.val_file;
4529 /* Add a vms delta attribute value to a DIE. */
4531 static inline void
4532 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4533 const char *lbl1, const char *lbl2)
4535 dw_attr_node attr;
4537 attr.dw_attr = attr_kind;
4538 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4539 attr.dw_attr_val.val_entry = NULL;
4540 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4541 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4542 add_dwarf_attr (die, &attr);
4545 /* Add a label identifier attribute value to a DIE. */
4547 static inline void
4548 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4549 const char *lbl_id)
4551 dw_attr_node attr;
4553 attr.dw_attr = attr_kind;
4554 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4555 attr.dw_attr_val.val_entry = NULL;
4556 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4557 if (dwarf_split_debug_info)
4558 attr.dw_attr_val.val_entry
4559 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4560 ate_kind_label);
4561 add_dwarf_attr (die, &attr);
4564 /* Add a section offset attribute value to a DIE, an offset into the
4565 debug_line section. */
4567 static inline void
4568 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4569 const char *label)
4571 dw_attr_node attr;
4573 attr.dw_attr = attr_kind;
4574 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4575 attr.dw_attr_val.val_entry = NULL;
4576 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4577 add_dwarf_attr (die, &attr);
4580 /* Add a section offset attribute value to a DIE, an offset into the
4581 debug_macinfo section. */
4583 static inline void
4584 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4585 const char *label)
4587 dw_attr_node attr;
4589 attr.dw_attr = attr_kind;
4590 attr.dw_attr_val.val_class = dw_val_class_macptr;
4591 attr.dw_attr_val.val_entry = NULL;
4592 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4593 add_dwarf_attr (die, &attr);
4596 /* Add an offset attribute value to a DIE. */
4598 static inline void
4599 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4600 unsigned HOST_WIDE_INT offset)
4602 dw_attr_node attr;
4604 attr.dw_attr = attr_kind;
4605 attr.dw_attr_val.val_class = dw_val_class_offset;
4606 attr.dw_attr_val.val_entry = NULL;
4607 attr.dw_attr_val.v.val_offset = offset;
4608 add_dwarf_attr (die, &attr);
4611 /* Add a range_list attribute value to a DIE. When using
4612 dwarf_split_debug_info, address attributes in dies destined for the
4613 final executable should be direct references--setting the parameter
4614 force_direct ensures this behavior. */
4616 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4617 #define RELOCATED_OFFSET (NULL)
4619 static void
4620 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4621 long unsigned int offset, bool force_direct)
4623 dw_attr_node attr;
4625 attr.dw_attr = attr_kind;
4626 attr.dw_attr_val.val_class = dw_val_class_range_list;
4627 /* For the range_list attribute, use val_entry to store whether the
4628 offset should follow split-debug-info or normal semantics. This
4629 value is read in output_range_list_offset. */
4630 if (dwarf_split_debug_info && !force_direct)
4631 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4632 else
4633 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4634 attr.dw_attr_val.v.val_offset = offset;
4635 add_dwarf_attr (die, &attr);
4638 /* Return the start label of a delta attribute. */
4640 static inline const char *
4641 AT_vms_delta1 (dw_attr_node *a)
4643 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4644 return a->dw_attr_val.v.val_vms_delta.lbl1;
4647 /* Return the end label of a delta attribute. */
4649 static inline const char *
4650 AT_vms_delta2 (dw_attr_node *a)
4652 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4653 return a->dw_attr_val.v.val_vms_delta.lbl2;
4656 static inline const char *
4657 AT_lbl (dw_attr_node *a)
4659 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4660 || AT_class (a) == dw_val_class_lineptr
4661 || AT_class (a) == dw_val_class_macptr
4662 || AT_class (a) == dw_val_class_high_pc));
4663 return a->dw_attr_val.v.val_lbl_id;
4666 /* Get the attribute of type attr_kind. */
4668 static dw_attr_node *
4669 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4671 dw_attr_node *a;
4672 unsigned ix;
4673 dw_die_ref spec = NULL;
4675 if (! die)
4676 return NULL;
4678 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4679 if (a->dw_attr == attr_kind)
4680 return a;
4681 else if (a->dw_attr == DW_AT_specification
4682 || a->dw_attr == DW_AT_abstract_origin)
4683 spec = AT_ref (a);
4685 if (spec)
4686 return get_AT (spec, attr_kind);
4688 return NULL;
4691 /* Returns the parent of the declaration of DIE. */
4693 static dw_die_ref
4694 get_die_parent (dw_die_ref die)
4696 dw_die_ref t;
4698 if (!die)
4699 return NULL;
4701 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4702 || (t = get_AT_ref (die, DW_AT_specification)))
4703 die = t;
4705 return die->die_parent;
4708 /* Return the "low pc" attribute value, typically associated with a subprogram
4709 DIE. Return null if the "low pc" attribute is either not present, or if it
4710 cannot be represented as an assembler label identifier. */
4712 static inline const char *
4713 get_AT_low_pc (dw_die_ref die)
4715 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4717 return a ? AT_lbl (a) : NULL;
4720 /* Return the "high pc" attribute value, typically associated with a subprogram
4721 DIE. Return null if the "high pc" attribute is either not present, or if it
4722 cannot be represented as an assembler label identifier. */
4724 static inline const char *
4725 get_AT_hi_pc (dw_die_ref die)
4727 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4729 return a ? AT_lbl (a) : NULL;
4732 /* Return the value of the string attribute designated by ATTR_KIND, or
4733 NULL if it is not present. */
4735 static inline const char *
4736 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4738 dw_attr_node *a = get_AT (die, attr_kind);
4740 return a ? AT_string (a) : NULL;
4743 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4744 if it is not present. */
4746 static inline int
4747 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4749 dw_attr_node *a = get_AT (die, attr_kind);
4751 return a ? AT_flag (a) : 0;
4754 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4755 if it is not present. */
4757 static inline unsigned
4758 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4760 dw_attr_node *a = get_AT (die, attr_kind);
4762 return a ? AT_unsigned (a) : 0;
4765 static inline dw_die_ref
4766 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4768 dw_attr_node *a = get_AT (die, attr_kind);
4770 return a ? AT_ref (a) : NULL;
4773 static inline struct dwarf_file_data *
4774 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4776 dw_attr_node *a = get_AT (die, attr_kind);
4778 return a ? AT_file (a) : NULL;
4781 /* Return TRUE if the language is C++. */
4783 static inline bool
4784 is_cxx (void)
4786 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4788 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4789 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4792 /* Return TRUE if the language is Java. */
4794 static inline bool
4795 is_java (void)
4797 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4799 return lang == DW_LANG_Java;
4802 /* Return TRUE if the language is Fortran. */
4804 static inline bool
4805 is_fortran (void)
4807 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4809 return (lang == DW_LANG_Fortran77
4810 || lang == DW_LANG_Fortran90
4811 || lang == DW_LANG_Fortran95
4812 || lang == DW_LANG_Fortran03
4813 || lang == DW_LANG_Fortran08);
4816 /* Return TRUE if the language is Ada. */
4818 static inline bool
4819 is_ada (void)
4821 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4823 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4826 /* Remove the specified attribute if present. Return TRUE if removal
4827 was successful. */
4829 static bool
4830 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4832 dw_attr_node *a;
4833 unsigned ix;
4835 if (! die)
4836 return false;
4838 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4839 if (a->dw_attr == attr_kind)
4841 if (AT_class (a) == dw_val_class_str)
4842 if (a->dw_attr_val.v.val_str->refcount)
4843 a->dw_attr_val.v.val_str->refcount--;
4845 /* vec::ordered_remove should help reduce the number of abbrevs
4846 that are needed. */
4847 die->die_attr->ordered_remove (ix);
4848 return true;
4850 return false;
4853 /* Remove CHILD from its parent. PREV must have the property that
4854 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4856 static void
4857 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4859 gcc_assert (child->die_parent == prev->die_parent);
4860 gcc_assert (prev->die_sib == child);
4861 if (prev == child)
4863 gcc_assert (child->die_parent->die_child == child);
4864 prev = NULL;
4866 else
4867 prev->die_sib = child->die_sib;
4868 if (child->die_parent->die_child == child)
4869 child->die_parent->die_child = prev;
4870 child->die_sib = NULL;
4873 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4874 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4876 static void
4877 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4879 dw_die_ref parent = old_child->die_parent;
4881 gcc_assert (parent == prev->die_parent);
4882 gcc_assert (prev->die_sib == old_child);
4884 new_child->die_parent = parent;
4885 if (prev == old_child)
4887 gcc_assert (parent->die_child == old_child);
4888 new_child->die_sib = new_child;
4890 else
4892 prev->die_sib = new_child;
4893 new_child->die_sib = old_child->die_sib;
4895 if (old_child->die_parent->die_child == old_child)
4896 old_child->die_parent->die_child = new_child;
4897 old_child->die_sib = NULL;
4900 /* Move all children from OLD_PARENT to NEW_PARENT. */
4902 static void
4903 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4905 dw_die_ref c;
4906 new_parent->die_child = old_parent->die_child;
4907 old_parent->die_child = NULL;
4908 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4911 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4912 matches TAG. */
4914 static void
4915 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4917 dw_die_ref c;
4919 c = die->die_child;
4920 if (c) do {
4921 dw_die_ref prev = c;
4922 c = c->die_sib;
4923 while (c->die_tag == tag)
4925 remove_child_with_prev (c, prev);
4926 c->die_parent = NULL;
4927 /* Might have removed every child. */
4928 if (die->die_child == NULL)
4929 return;
4930 c = prev->die_sib;
4932 } while (c != die->die_child);
4935 /* Add a CHILD_DIE as the last child of DIE. */
4937 static void
4938 add_child_die (dw_die_ref die, dw_die_ref child_die)
4940 /* FIXME this should probably be an assert. */
4941 if (! die || ! child_die)
4942 return;
4943 gcc_assert (die != child_die);
4945 child_die->die_parent = die;
4946 if (die->die_child)
4948 child_die->die_sib = die->die_child->die_sib;
4949 die->die_child->die_sib = child_die;
4951 else
4952 child_die->die_sib = child_die;
4953 die->die_child = child_die;
4956 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
4958 static void
4959 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4960 dw_die_ref after_die)
4962 gcc_assert (die
4963 && child_die
4964 && after_die
4965 && die->die_child
4966 && die != child_die);
4968 child_die->die_parent = die;
4969 child_die->die_sib = after_die->die_sib;
4970 after_die->die_sib = child_die;
4971 if (die->die_child == after_die)
4972 die->die_child = child_die;
4975 /* Unassociate CHILD from its parent, and make its parent be
4976 NEW_PARENT. */
4978 static void
4979 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4981 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4982 if (p->die_sib == child)
4984 remove_child_with_prev (child, p);
4985 break;
4987 add_child_die (new_parent, child);
4990 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4991 is the specification, to the end of PARENT's list of children.
4992 This is done by removing and re-adding it. */
4994 static void
4995 splice_child_die (dw_die_ref parent, dw_die_ref child)
4997 /* We want the declaration DIE from inside the class, not the
4998 specification DIE at toplevel. */
4999 if (child->die_parent != parent)
5001 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5003 if (tmp)
5004 child = tmp;
5007 gcc_assert (child->die_parent == parent
5008 || (child->die_parent
5009 == get_AT_ref (parent, DW_AT_specification)));
5011 reparent_child (child, parent);
5014 /* Create and return a new die with a parent of PARENT_DIE. If
5015 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5016 associated tree T must be supplied to determine parenthood
5017 later. */
5019 static inline dw_die_ref
5020 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5022 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5024 die->die_tag = tag_value;
5026 if (parent_die != NULL)
5027 add_child_die (parent_die, die);
5028 else
5030 limbo_die_node *limbo_node;
5032 /* No DIEs created after early dwarf should end up in limbo,
5033 because the limbo list should not persist past LTO
5034 streaming. */
5035 if (tag_value != DW_TAG_compile_unit
5036 /* These are allowed because they're generated while
5037 breaking out COMDAT units late. */
5038 && tag_value != DW_TAG_type_unit
5039 && !early_dwarf
5040 /* Allow nested functions to live in limbo because they will
5041 only temporarily live there, as decls_for_scope will fix
5042 them up. */
5043 && (TREE_CODE (t) != FUNCTION_DECL
5044 || !decl_function_context (t))
5045 /* Same as nested functions above but for types. Types that
5046 are local to a function will be fixed in
5047 decls_for_scope. */
5048 && (!RECORD_OR_UNION_TYPE_P (t)
5049 || !TYPE_CONTEXT (t)
5050 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5051 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5052 especially in the ltrans stage, but once we implement LTO
5053 dwarf streaming, we should remove this exception. */
5054 && !in_lto_p)
5056 fprintf (stderr, "symbol ended up in limbo too late:");
5057 debug_generic_stmt (t);
5058 gcc_unreachable ();
5061 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5062 limbo_node->die = die;
5063 limbo_node->created_for = t;
5064 limbo_node->next = limbo_die_list;
5065 limbo_die_list = limbo_node;
5068 return die;
5071 /* Return the DIE associated with the given type specifier. */
5073 static inline dw_die_ref
5074 lookup_type_die (tree type)
5076 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5077 if (die && die->removed)
5079 TYPE_SYMTAB_DIE (type) = NULL;
5080 return NULL;
5082 return die;
5085 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5086 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5087 anonymous type instead the one of the naming typedef. */
5089 static inline dw_die_ref
5090 strip_naming_typedef (tree type, dw_die_ref type_die)
5092 if (type
5093 && TREE_CODE (type) == RECORD_TYPE
5094 && type_die
5095 && type_die->die_tag == DW_TAG_typedef
5096 && is_naming_typedef_decl (TYPE_NAME (type)))
5097 type_die = get_AT_ref (type_die, DW_AT_type);
5098 return type_die;
5101 /* Like lookup_type_die, but if type is an anonymous type named by a
5102 typedef[1], return the DIE of the anonymous type instead the one of
5103 the naming typedef. This is because in gen_typedef_die, we did
5104 equate the anonymous struct named by the typedef with the DIE of
5105 the naming typedef. So by default, lookup_type_die on an anonymous
5106 struct yields the DIE of the naming typedef.
5108 [1]: Read the comment of is_naming_typedef_decl to learn about what
5109 a naming typedef is. */
5111 static inline dw_die_ref
5112 lookup_type_die_strip_naming_typedef (tree type)
5114 dw_die_ref die = lookup_type_die (type);
5115 return strip_naming_typedef (type, die);
5118 /* Equate a DIE to a given type specifier. */
5120 static inline void
5121 equate_type_number_to_die (tree type, dw_die_ref type_die)
5123 TYPE_SYMTAB_DIE (type) = type_die;
5126 /* Returns a hash value for X (which really is a die_struct). */
5128 inline hashval_t
5129 decl_die_hasher::hash (die_node *x)
5131 return (hashval_t) x->decl_id;
5134 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5136 inline bool
5137 decl_die_hasher::equal (die_node *x, tree y)
5139 return (x->decl_id == DECL_UID (y));
5142 /* Return the DIE associated with a given declaration. */
5144 static inline dw_die_ref
5145 lookup_decl_die (tree decl)
5147 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5148 NO_INSERT);
5149 if (!die)
5150 return NULL;
5151 if ((*die)->removed)
5153 decl_die_table->clear_slot (die);
5154 return NULL;
5156 return *die;
5159 /* Returns a hash value for X (which really is a var_loc_list). */
5161 inline hashval_t
5162 decl_loc_hasher::hash (var_loc_list *x)
5164 return (hashval_t) x->decl_id;
5167 /* Return nonzero if decl_id of var_loc_list X is the same as
5168 UID of decl *Y. */
5170 inline bool
5171 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5173 return (x->decl_id == DECL_UID (y));
5176 /* Return the var_loc list associated with a given declaration. */
5178 static inline var_loc_list *
5179 lookup_decl_loc (const_tree decl)
5181 if (!decl_loc_table)
5182 return NULL;
5183 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5186 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5188 inline hashval_t
5189 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5191 return (hashval_t) x->decl_id;
5194 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5195 UID of decl *Y. */
5197 inline bool
5198 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5200 return (x->decl_id == DECL_UID (y));
5203 /* Equate a DIE to a particular declaration. */
5205 static void
5206 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5208 unsigned int decl_id = DECL_UID (decl);
5210 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5211 decl_die->decl_id = decl_id;
5214 /* Return how many bits covers PIECE EXPR_LIST. */
5216 static HOST_WIDE_INT
5217 decl_piece_bitsize (rtx piece)
5219 int ret = (int) GET_MODE (piece);
5220 if (ret)
5221 return ret;
5222 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5223 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5224 return INTVAL (XEXP (XEXP (piece, 0), 0));
5227 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5229 static rtx *
5230 decl_piece_varloc_ptr (rtx piece)
5232 if ((int) GET_MODE (piece))
5233 return &XEXP (piece, 0);
5234 else
5235 return &XEXP (XEXP (piece, 0), 1);
5238 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5239 Next is the chain of following piece nodes. */
5241 static rtx_expr_list *
5242 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5244 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5245 return alloc_EXPR_LIST (bitsize, loc_note, next);
5246 else
5247 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5248 GEN_INT (bitsize),
5249 loc_note), next);
5252 /* Return rtx that should be stored into loc field for
5253 LOC_NOTE and BITPOS/BITSIZE. */
5255 static rtx
5256 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5257 HOST_WIDE_INT bitsize)
5259 if (bitsize != -1)
5261 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5262 if (bitpos != 0)
5263 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5265 return loc_note;
5268 /* This function either modifies location piece list *DEST in
5269 place (if SRC and INNER is NULL), or copies location piece list
5270 *SRC to *DEST while modifying it. Location BITPOS is modified
5271 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5272 not copied and if needed some padding around it is added.
5273 When modifying in place, DEST should point to EXPR_LIST where
5274 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5275 to the start of the whole list and INNER points to the EXPR_LIST
5276 where earlier pieces cover PIECE_BITPOS bits. */
5278 static void
5279 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5280 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5281 HOST_WIDE_INT bitsize, rtx loc_note)
5283 HOST_WIDE_INT diff;
5284 bool copy = inner != NULL;
5286 if (copy)
5288 /* First copy all nodes preceding the current bitpos. */
5289 while (src != inner)
5291 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5292 decl_piece_bitsize (*src), NULL_RTX);
5293 dest = &XEXP (*dest, 1);
5294 src = &XEXP (*src, 1);
5297 /* Add padding if needed. */
5298 if (bitpos != piece_bitpos)
5300 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5301 copy ? NULL_RTX : *dest);
5302 dest = &XEXP (*dest, 1);
5304 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5306 gcc_assert (!copy);
5307 /* A piece with correct bitpos and bitsize already exist,
5308 just update the location for it and return. */
5309 *decl_piece_varloc_ptr (*dest) = loc_note;
5310 return;
5312 /* Add the piece that changed. */
5313 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5314 dest = &XEXP (*dest, 1);
5315 /* Skip over pieces that overlap it. */
5316 diff = bitpos - piece_bitpos + bitsize;
5317 if (!copy)
5318 src = dest;
5319 while (diff > 0 && *src)
5321 rtx piece = *src;
5322 diff -= decl_piece_bitsize (piece);
5323 if (copy)
5324 src = &XEXP (piece, 1);
5325 else
5327 *src = XEXP (piece, 1);
5328 free_EXPR_LIST_node (piece);
5331 /* Add padding if needed. */
5332 if (diff < 0 && *src)
5334 if (!copy)
5335 dest = src;
5336 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5337 dest = &XEXP (*dest, 1);
5339 if (!copy)
5340 return;
5341 /* Finally copy all nodes following it. */
5342 while (*src)
5344 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5345 decl_piece_bitsize (*src), NULL_RTX);
5346 dest = &XEXP (*dest, 1);
5347 src = &XEXP (*src, 1);
5351 /* Add a variable location node to the linked list for DECL. */
5353 static struct var_loc_node *
5354 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5356 unsigned int decl_id;
5357 var_loc_list *temp;
5358 struct var_loc_node *loc = NULL;
5359 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5361 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5363 tree realdecl = DECL_DEBUG_EXPR (decl);
5364 if (handled_component_p (realdecl)
5365 || (TREE_CODE (realdecl) == MEM_REF
5366 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5368 HOST_WIDE_INT maxsize;
5369 bool reverse;
5370 tree innerdecl
5371 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5372 &reverse);
5373 if (!DECL_P (innerdecl)
5374 || DECL_IGNORED_P (innerdecl)
5375 || TREE_STATIC (innerdecl)
5376 || bitsize <= 0
5377 || bitpos + bitsize > 256
5378 || bitsize != maxsize)
5379 return NULL;
5380 decl = innerdecl;
5384 decl_id = DECL_UID (decl);
5385 var_loc_list **slot
5386 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5387 if (*slot == NULL)
5389 temp = ggc_cleared_alloc<var_loc_list> ();
5390 temp->decl_id = decl_id;
5391 *slot = temp;
5393 else
5394 temp = *slot;
5396 /* For PARM_DECLs try to keep around the original incoming value,
5397 even if that means we'll emit a zero-range .debug_loc entry. */
5398 if (temp->last
5399 && temp->first == temp->last
5400 && TREE_CODE (decl) == PARM_DECL
5401 && NOTE_P (temp->first->loc)
5402 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5403 && DECL_INCOMING_RTL (decl)
5404 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5405 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5406 == GET_CODE (DECL_INCOMING_RTL (decl))
5407 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5408 && (bitsize != -1
5409 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5410 NOTE_VAR_LOCATION_LOC (loc_note))
5411 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5412 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5414 loc = ggc_cleared_alloc<var_loc_node> ();
5415 temp->first->next = loc;
5416 temp->last = loc;
5417 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5419 else if (temp->last)
5421 struct var_loc_node *last = temp->last, *unused = NULL;
5422 rtx *piece_loc = NULL, last_loc_note;
5423 HOST_WIDE_INT piece_bitpos = 0;
5424 if (last->next)
5426 last = last->next;
5427 gcc_assert (last->next == NULL);
5429 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5431 piece_loc = &last->loc;
5434 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5435 if (piece_bitpos + cur_bitsize > bitpos)
5436 break;
5437 piece_bitpos += cur_bitsize;
5438 piece_loc = &XEXP (*piece_loc, 1);
5440 while (*piece_loc);
5442 /* TEMP->LAST here is either pointer to the last but one or
5443 last element in the chained list, LAST is pointer to the
5444 last element. */
5445 if (label && strcmp (last->label, label) == 0)
5447 /* For SRA optimized variables if there weren't any real
5448 insns since last note, just modify the last node. */
5449 if (piece_loc != NULL)
5451 adjust_piece_list (piece_loc, NULL, NULL,
5452 bitpos, piece_bitpos, bitsize, loc_note);
5453 return NULL;
5455 /* If the last note doesn't cover any instructions, remove it. */
5456 if (temp->last != last)
5458 temp->last->next = NULL;
5459 unused = last;
5460 last = temp->last;
5461 gcc_assert (strcmp (last->label, label) != 0);
5463 else
5465 gcc_assert (temp->first == temp->last
5466 || (temp->first->next == temp->last
5467 && TREE_CODE (decl) == PARM_DECL));
5468 memset (temp->last, '\0', sizeof (*temp->last));
5469 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5470 return temp->last;
5473 if (bitsize == -1 && NOTE_P (last->loc))
5474 last_loc_note = last->loc;
5475 else if (piece_loc != NULL
5476 && *piece_loc != NULL_RTX
5477 && piece_bitpos == bitpos
5478 && decl_piece_bitsize (*piece_loc) == bitsize)
5479 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5480 else
5481 last_loc_note = NULL_RTX;
5482 /* If the current location is the same as the end of the list,
5483 and either both or neither of the locations is uninitialized,
5484 we have nothing to do. */
5485 if (last_loc_note == NULL_RTX
5486 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5487 NOTE_VAR_LOCATION_LOC (loc_note)))
5488 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5489 != NOTE_VAR_LOCATION_STATUS (loc_note))
5490 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5491 == VAR_INIT_STATUS_UNINITIALIZED)
5492 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5493 == VAR_INIT_STATUS_UNINITIALIZED))))
5495 /* Add LOC to the end of list and update LAST. If the last
5496 element of the list has been removed above, reuse its
5497 memory for the new node, otherwise allocate a new one. */
5498 if (unused)
5500 loc = unused;
5501 memset (loc, '\0', sizeof (*loc));
5503 else
5504 loc = ggc_cleared_alloc<var_loc_node> ();
5505 if (bitsize == -1 || piece_loc == NULL)
5506 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5507 else
5508 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5509 bitpos, piece_bitpos, bitsize, loc_note);
5510 last->next = loc;
5511 /* Ensure TEMP->LAST will point either to the new last but one
5512 element of the chain, or to the last element in it. */
5513 if (last != temp->last)
5514 temp->last = last;
5516 else if (unused)
5517 ggc_free (unused);
5519 else
5521 loc = ggc_cleared_alloc<var_loc_node> ();
5522 temp->first = loc;
5523 temp->last = loc;
5524 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5526 return loc;
5529 /* Keep track of the number of spaces used to indent the
5530 output of the debugging routines that print the structure of
5531 the DIE internal representation. */
5532 static int print_indent;
5534 /* Indent the line the number of spaces given by print_indent. */
5536 static inline void
5537 print_spaces (FILE *outfile)
5539 fprintf (outfile, "%*s", print_indent, "");
5542 /* Print a type signature in hex. */
5544 static inline void
5545 print_signature (FILE *outfile, char *sig)
5547 int i;
5549 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5550 fprintf (outfile, "%02x", sig[i] & 0xff);
5553 static inline void
5554 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5556 if (discr_value->pos)
5557 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5558 else
5559 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5562 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5564 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5565 RECURSE, output location descriptor operations. */
5567 static void
5568 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5570 switch (val->val_class)
5572 case dw_val_class_addr:
5573 fprintf (outfile, "address");
5574 break;
5575 case dw_val_class_offset:
5576 fprintf (outfile, "offset");
5577 break;
5578 case dw_val_class_loc:
5579 fprintf (outfile, "location descriptor");
5580 if (val->v.val_loc == NULL)
5581 fprintf (outfile, " -> <null>\n");
5582 else if (recurse)
5584 fprintf (outfile, ":\n");
5585 print_indent += 4;
5586 print_loc_descr (val->v.val_loc, outfile);
5587 print_indent -= 4;
5589 else
5590 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5591 break;
5592 case dw_val_class_loc_list:
5593 fprintf (outfile, "location list -> label:%s",
5594 val->v.val_loc_list->ll_symbol);
5595 break;
5596 case dw_val_class_range_list:
5597 fprintf (outfile, "range list");
5598 break;
5599 case dw_val_class_const:
5600 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5601 break;
5602 case dw_val_class_unsigned_const:
5603 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5604 break;
5605 case dw_val_class_const_double:
5606 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5607 HOST_WIDE_INT_PRINT_UNSIGNED")",
5608 val->v.val_double.high,
5609 val->v.val_double.low);
5610 break;
5611 case dw_val_class_wide_int:
5613 int i = val->v.val_wide->get_len ();
5614 fprintf (outfile, "constant (");
5615 gcc_assert (i > 0);
5616 if (val->v.val_wide->elt (i - 1) == 0)
5617 fprintf (outfile, "0x");
5618 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5619 val->v.val_wide->elt (--i));
5620 while (--i >= 0)
5621 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5622 val->v.val_wide->elt (i));
5623 fprintf (outfile, ")");
5624 break;
5626 case dw_val_class_vec:
5627 fprintf (outfile, "floating-point or vector constant");
5628 break;
5629 case dw_val_class_flag:
5630 fprintf (outfile, "%u", val->v.val_flag);
5631 break;
5632 case dw_val_class_die_ref:
5633 if (val->v.val_die_ref.die != NULL)
5635 dw_die_ref die = val->v.val_die_ref.die;
5637 if (die->comdat_type_p)
5639 fprintf (outfile, "die -> signature: ");
5640 print_signature (outfile,
5641 die->die_id.die_type_node->signature);
5643 else if (die->die_id.die_symbol)
5644 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5645 else
5646 fprintf (outfile, "die -> %ld", die->die_offset);
5647 fprintf (outfile, " (%p)", (void *) die);
5649 else
5650 fprintf (outfile, "die -> <null>");
5651 break;
5652 case dw_val_class_vms_delta:
5653 fprintf (outfile, "delta: @slotcount(%s-%s)",
5654 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5655 break;
5656 case dw_val_class_lbl_id:
5657 case dw_val_class_lineptr:
5658 case dw_val_class_macptr:
5659 case dw_val_class_high_pc:
5660 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5661 break;
5662 case dw_val_class_str:
5663 if (val->v.val_str->str != NULL)
5664 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5665 else
5666 fprintf (outfile, "<null>");
5667 break;
5668 case dw_val_class_file:
5669 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5670 val->v.val_file->emitted_number);
5671 break;
5672 case dw_val_class_data8:
5674 int i;
5676 for (i = 0; i < 8; i++)
5677 fprintf (outfile, "%02x", val->v.val_data8[i]);
5678 break;
5680 case dw_val_class_discr_value:
5681 print_discr_value (outfile, &val->v.val_discr_value);
5682 break;
5683 case dw_val_class_discr_list:
5684 for (dw_discr_list_ref node = val->v.val_discr_list;
5685 node != NULL;
5686 node = node->dw_discr_next)
5688 if (node->dw_discr_range)
5690 fprintf (outfile, " .. ");
5691 print_discr_value (outfile, &node->dw_discr_lower_bound);
5692 print_discr_value (outfile, &node->dw_discr_upper_bound);
5694 else
5695 print_discr_value (outfile, &node->dw_discr_lower_bound);
5697 if (node->dw_discr_next != NULL)
5698 fprintf (outfile, " | ");
5700 default:
5701 break;
5705 /* Likewise, for a DIE attribute. */
5707 static void
5708 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5710 print_dw_val (&a->dw_attr_val, recurse, outfile);
5714 /* Print the list of operands in the LOC location description to OUTFILE. This
5715 routine is a debugging aid only. */
5717 static void
5718 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5720 dw_loc_descr_ref l = loc;
5722 if (loc == NULL)
5724 print_spaces (outfile);
5725 fprintf (outfile, "<null>\n");
5726 return;
5729 for (l = loc; l != NULL; l = l->dw_loc_next)
5731 print_spaces (outfile);
5732 fprintf (outfile, "(%p) %s",
5733 (void *) l,
5734 dwarf_stack_op_name (l->dw_loc_opc));
5735 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5737 fprintf (outfile, " ");
5738 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5740 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5742 fprintf (outfile, ", ");
5743 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5745 fprintf (outfile, "\n");
5749 /* Print the information associated with a given DIE, and its children.
5750 This routine is a debugging aid only. */
5752 static void
5753 print_die (dw_die_ref die, FILE *outfile)
5755 dw_attr_node *a;
5756 dw_die_ref c;
5757 unsigned ix;
5759 print_spaces (outfile);
5760 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5761 die->die_offset, dwarf_tag_name (die->die_tag),
5762 (void*) die);
5763 print_spaces (outfile);
5764 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5765 fprintf (outfile, " offset: %ld", die->die_offset);
5766 fprintf (outfile, " mark: %d\n", die->die_mark);
5768 if (die->comdat_type_p)
5770 print_spaces (outfile);
5771 fprintf (outfile, " signature: ");
5772 print_signature (outfile, die->die_id.die_type_node->signature);
5773 fprintf (outfile, "\n");
5776 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5778 print_spaces (outfile);
5779 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5781 print_attribute (a, true, outfile);
5782 fprintf (outfile, "\n");
5785 if (die->die_child != NULL)
5787 print_indent += 4;
5788 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5789 print_indent -= 4;
5791 if (print_indent == 0)
5792 fprintf (outfile, "\n");
5795 /* Print the list of operations in the LOC location description. */
5797 DEBUG_FUNCTION void
5798 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5800 print_loc_descr (loc, stderr);
5803 /* Print the information collected for a given DIE. */
5805 DEBUG_FUNCTION void
5806 debug_dwarf_die (dw_die_ref die)
5808 print_die (die, stderr);
5811 DEBUG_FUNCTION void
5812 debug (die_struct &ref)
5814 print_die (&ref, stderr);
5817 DEBUG_FUNCTION void
5818 debug (die_struct *ptr)
5820 if (ptr)
5821 debug (*ptr);
5822 else
5823 fprintf (stderr, "<nil>\n");
5827 /* Print all DWARF information collected for the compilation unit.
5828 This routine is a debugging aid only. */
5830 DEBUG_FUNCTION void
5831 debug_dwarf (void)
5833 print_indent = 0;
5834 print_die (comp_unit_die (), stderr);
5837 /* Verify the DIE tree structure. */
5839 DEBUG_FUNCTION void
5840 verify_die (dw_die_ref die)
5842 gcc_assert (!die->die_mark);
5843 if (die->die_parent == NULL
5844 && die->die_sib == NULL)
5845 return;
5846 /* Verify the die_sib list is cyclic. */
5847 dw_die_ref x = die;
5850 x->die_mark = 1;
5851 x = x->die_sib;
5853 while (x && !x->die_mark);
5854 gcc_assert (x == die);
5855 x = die;
5858 /* Verify all dies have the same parent. */
5859 gcc_assert (x->die_parent == die->die_parent);
5860 if (x->die_child)
5862 /* Verify the child has the proper parent and recurse. */
5863 gcc_assert (x->die_child->die_parent == x);
5864 verify_die (x->die_child);
5866 x->die_mark = 0;
5867 x = x->die_sib;
5869 while (x && x->die_mark);
5872 /* Sanity checks on DIEs. */
5874 static void
5875 check_die (dw_die_ref die)
5877 unsigned ix;
5878 dw_attr_node *a;
5879 bool inline_found = false;
5880 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5881 int n_decl_line = 0, n_decl_file = 0;
5882 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5884 switch (a->dw_attr)
5886 case DW_AT_inline:
5887 if (a->dw_attr_val.v.val_unsigned)
5888 inline_found = true;
5889 break;
5890 case DW_AT_location:
5891 ++n_location;
5892 break;
5893 case DW_AT_low_pc:
5894 ++n_low_pc;
5895 break;
5896 case DW_AT_high_pc:
5897 ++n_high_pc;
5898 break;
5899 case DW_AT_artificial:
5900 ++n_artificial;
5901 break;
5902 case DW_AT_decl_line:
5903 ++n_decl_line;
5904 break;
5905 case DW_AT_decl_file:
5906 ++n_decl_file;
5907 break;
5908 default:
5909 break;
5912 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5913 || n_decl_line > 1 || n_decl_file > 1)
5915 fprintf (stderr, "Duplicate attributes in DIE:\n");
5916 debug_dwarf_die (die);
5917 gcc_unreachable ();
5919 if (inline_found)
5921 /* A debugging information entry that is a member of an abstract
5922 instance tree [that has DW_AT_inline] should not contain any
5923 attributes which describe aspects of the subroutine which vary
5924 between distinct inlined expansions or distinct out-of-line
5925 expansions. */
5926 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5927 gcc_assert (a->dw_attr != DW_AT_low_pc
5928 && a->dw_attr != DW_AT_high_pc
5929 && a->dw_attr != DW_AT_location
5930 && a->dw_attr != DW_AT_frame_base
5931 && a->dw_attr != DW_AT_GNU_all_call_sites);
5935 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5936 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5937 DIE that marks the start of the DIEs for this include file. */
5939 static dw_die_ref
5940 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5942 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5943 dw_die_ref new_unit = gen_compile_unit_die (filename);
5945 new_unit->die_sib = old_unit;
5946 return new_unit;
5949 /* Close an include-file CU and reopen the enclosing one. */
5951 static dw_die_ref
5952 pop_compile_unit (dw_die_ref old_unit)
5954 dw_die_ref new_unit = old_unit->die_sib;
5956 old_unit->die_sib = NULL;
5957 return new_unit;
5960 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5961 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5962 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5964 /* Calculate the checksum of a location expression. */
5966 static inline void
5967 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5969 int tem;
5970 inchash::hash hstate;
5971 hashval_t hash;
5973 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5974 CHECKSUM (tem);
5975 hash_loc_operands (loc, hstate);
5976 hash = hstate.end();
5977 CHECKSUM (hash);
5980 /* Calculate the checksum of an attribute. */
5982 static void
5983 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5985 dw_loc_descr_ref loc;
5986 rtx r;
5988 CHECKSUM (at->dw_attr);
5990 /* We don't care that this was compiled with a different compiler
5991 snapshot; if the output is the same, that's what matters. */
5992 if (at->dw_attr == DW_AT_producer)
5993 return;
5995 switch (AT_class (at))
5997 case dw_val_class_const:
5998 CHECKSUM (at->dw_attr_val.v.val_int);
5999 break;
6000 case dw_val_class_unsigned_const:
6001 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6002 break;
6003 case dw_val_class_const_double:
6004 CHECKSUM (at->dw_attr_val.v.val_double);
6005 break;
6006 case dw_val_class_wide_int:
6007 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6008 get_full_len (*at->dw_attr_val.v.val_wide)
6009 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6010 break;
6011 case dw_val_class_vec:
6012 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6013 (at->dw_attr_val.v.val_vec.length
6014 * at->dw_attr_val.v.val_vec.elt_size));
6015 break;
6016 case dw_val_class_flag:
6017 CHECKSUM (at->dw_attr_val.v.val_flag);
6018 break;
6019 case dw_val_class_str:
6020 CHECKSUM_STRING (AT_string (at));
6021 break;
6023 case dw_val_class_addr:
6024 r = AT_addr (at);
6025 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6026 CHECKSUM_STRING (XSTR (r, 0));
6027 break;
6029 case dw_val_class_offset:
6030 CHECKSUM (at->dw_attr_val.v.val_offset);
6031 break;
6033 case dw_val_class_loc:
6034 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6035 loc_checksum (loc, ctx);
6036 break;
6038 case dw_val_class_die_ref:
6039 die_checksum (AT_ref (at), ctx, mark);
6040 break;
6042 case dw_val_class_fde_ref:
6043 case dw_val_class_vms_delta:
6044 case dw_val_class_lbl_id:
6045 case dw_val_class_lineptr:
6046 case dw_val_class_macptr:
6047 case dw_val_class_high_pc:
6048 break;
6050 case dw_val_class_file:
6051 CHECKSUM_STRING (AT_file (at)->filename);
6052 break;
6054 case dw_val_class_data8:
6055 CHECKSUM (at->dw_attr_val.v.val_data8);
6056 break;
6058 default:
6059 break;
6063 /* Calculate the checksum of a DIE. */
6065 static void
6066 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6068 dw_die_ref c;
6069 dw_attr_node *a;
6070 unsigned ix;
6072 /* To avoid infinite recursion. */
6073 if (die->die_mark)
6075 CHECKSUM (die->die_mark);
6076 return;
6078 die->die_mark = ++(*mark);
6080 CHECKSUM (die->die_tag);
6082 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6083 attr_checksum (a, ctx, mark);
6085 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6088 #undef CHECKSUM
6089 #undef CHECKSUM_BLOCK
6090 #undef CHECKSUM_STRING
6092 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6093 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6094 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6095 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6096 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6097 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6098 #define CHECKSUM_ATTR(FOO) \
6099 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6101 /* Calculate the checksum of a number in signed LEB128 format. */
6103 static void
6104 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6106 unsigned char byte;
6107 bool more;
6109 while (1)
6111 byte = (value & 0x7f);
6112 value >>= 7;
6113 more = !((value == 0 && (byte & 0x40) == 0)
6114 || (value == -1 && (byte & 0x40) != 0));
6115 if (more)
6116 byte |= 0x80;
6117 CHECKSUM (byte);
6118 if (!more)
6119 break;
6123 /* Calculate the checksum of a number in unsigned LEB128 format. */
6125 static void
6126 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6128 while (1)
6130 unsigned char byte = (value & 0x7f);
6131 value >>= 7;
6132 if (value != 0)
6133 /* More bytes to follow. */
6134 byte |= 0x80;
6135 CHECKSUM (byte);
6136 if (value == 0)
6137 break;
6141 /* Checksum the context of the DIE. This adds the names of any
6142 surrounding namespaces or structures to the checksum. */
6144 static void
6145 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6147 const char *name;
6148 dw_die_ref spec;
6149 int tag = die->die_tag;
6151 if (tag != DW_TAG_namespace
6152 && tag != DW_TAG_structure_type
6153 && tag != DW_TAG_class_type)
6154 return;
6156 name = get_AT_string (die, DW_AT_name);
6158 spec = get_AT_ref (die, DW_AT_specification);
6159 if (spec != NULL)
6160 die = spec;
6162 if (die->die_parent != NULL)
6163 checksum_die_context (die->die_parent, ctx);
6165 CHECKSUM_ULEB128 ('C');
6166 CHECKSUM_ULEB128 (tag);
6167 if (name != NULL)
6168 CHECKSUM_STRING (name);
6171 /* Calculate the checksum of a location expression. */
6173 static inline void
6174 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6176 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6177 were emitted as a DW_FORM_sdata instead of a location expression. */
6178 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6180 CHECKSUM_ULEB128 (DW_FORM_sdata);
6181 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6182 return;
6185 /* Otherwise, just checksum the raw location expression. */
6186 while (loc != NULL)
6188 inchash::hash hstate;
6189 hashval_t hash;
6191 CHECKSUM_ULEB128 (loc->dtprel);
6192 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6193 hash_loc_operands (loc, hstate);
6194 hash = hstate.end ();
6195 CHECKSUM (hash);
6196 loc = loc->dw_loc_next;
6200 /* Calculate the checksum of an attribute. */
6202 static void
6203 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6204 struct md5_ctx *ctx, int *mark)
6206 dw_loc_descr_ref loc;
6207 rtx r;
6209 if (AT_class (at) == dw_val_class_die_ref)
6211 dw_die_ref target_die = AT_ref (at);
6213 /* For pointer and reference types, we checksum only the (qualified)
6214 name of the target type (if there is a name). For friend entries,
6215 we checksum only the (qualified) name of the target type or function.
6216 This allows the checksum to remain the same whether the target type
6217 is complete or not. */
6218 if ((at->dw_attr == DW_AT_type
6219 && (tag == DW_TAG_pointer_type
6220 || tag == DW_TAG_reference_type
6221 || tag == DW_TAG_rvalue_reference_type
6222 || tag == DW_TAG_ptr_to_member_type))
6223 || (at->dw_attr == DW_AT_friend
6224 && tag == DW_TAG_friend))
6226 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6228 if (name_attr != NULL)
6230 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6232 if (decl == NULL)
6233 decl = target_die;
6234 CHECKSUM_ULEB128 ('N');
6235 CHECKSUM_ULEB128 (at->dw_attr);
6236 if (decl->die_parent != NULL)
6237 checksum_die_context (decl->die_parent, ctx);
6238 CHECKSUM_ULEB128 ('E');
6239 CHECKSUM_STRING (AT_string (name_attr));
6240 return;
6244 /* For all other references to another DIE, we check to see if the
6245 target DIE has already been visited. If it has, we emit a
6246 backward reference; if not, we descend recursively. */
6247 if (target_die->die_mark > 0)
6249 CHECKSUM_ULEB128 ('R');
6250 CHECKSUM_ULEB128 (at->dw_attr);
6251 CHECKSUM_ULEB128 (target_die->die_mark);
6253 else
6255 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6257 if (decl == NULL)
6258 decl = target_die;
6259 target_die->die_mark = ++(*mark);
6260 CHECKSUM_ULEB128 ('T');
6261 CHECKSUM_ULEB128 (at->dw_attr);
6262 if (decl->die_parent != NULL)
6263 checksum_die_context (decl->die_parent, ctx);
6264 die_checksum_ordered (target_die, ctx, mark);
6266 return;
6269 CHECKSUM_ULEB128 ('A');
6270 CHECKSUM_ULEB128 (at->dw_attr);
6272 switch (AT_class (at))
6274 case dw_val_class_const:
6275 CHECKSUM_ULEB128 (DW_FORM_sdata);
6276 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6277 break;
6279 case dw_val_class_unsigned_const:
6280 CHECKSUM_ULEB128 (DW_FORM_sdata);
6281 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6282 break;
6284 case dw_val_class_const_double:
6285 CHECKSUM_ULEB128 (DW_FORM_block);
6286 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6287 CHECKSUM (at->dw_attr_val.v.val_double);
6288 break;
6290 case dw_val_class_wide_int:
6291 CHECKSUM_ULEB128 (DW_FORM_block);
6292 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6293 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6294 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6295 get_full_len (*at->dw_attr_val.v.val_wide)
6296 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6297 break;
6299 case dw_val_class_vec:
6300 CHECKSUM_ULEB128 (DW_FORM_block);
6301 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6302 * at->dw_attr_val.v.val_vec.elt_size);
6303 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6304 (at->dw_attr_val.v.val_vec.length
6305 * at->dw_attr_val.v.val_vec.elt_size));
6306 break;
6308 case dw_val_class_flag:
6309 CHECKSUM_ULEB128 (DW_FORM_flag);
6310 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6311 break;
6313 case dw_val_class_str:
6314 CHECKSUM_ULEB128 (DW_FORM_string);
6315 CHECKSUM_STRING (AT_string (at));
6316 break;
6318 case dw_val_class_addr:
6319 r = AT_addr (at);
6320 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6321 CHECKSUM_ULEB128 (DW_FORM_string);
6322 CHECKSUM_STRING (XSTR (r, 0));
6323 break;
6325 case dw_val_class_offset:
6326 CHECKSUM_ULEB128 (DW_FORM_sdata);
6327 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6328 break;
6330 case dw_val_class_loc:
6331 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6332 loc_checksum_ordered (loc, ctx);
6333 break;
6335 case dw_val_class_fde_ref:
6336 case dw_val_class_lbl_id:
6337 case dw_val_class_lineptr:
6338 case dw_val_class_macptr:
6339 case dw_val_class_high_pc:
6340 break;
6342 case dw_val_class_file:
6343 CHECKSUM_ULEB128 (DW_FORM_string);
6344 CHECKSUM_STRING (AT_file (at)->filename);
6345 break;
6347 case dw_val_class_data8:
6348 CHECKSUM (at->dw_attr_val.v.val_data8);
6349 break;
6351 default:
6352 break;
6356 struct checksum_attributes
6358 dw_attr_node *at_name;
6359 dw_attr_node *at_type;
6360 dw_attr_node *at_friend;
6361 dw_attr_node *at_accessibility;
6362 dw_attr_node *at_address_class;
6363 dw_attr_node *at_allocated;
6364 dw_attr_node *at_artificial;
6365 dw_attr_node *at_associated;
6366 dw_attr_node *at_binary_scale;
6367 dw_attr_node *at_bit_offset;
6368 dw_attr_node *at_bit_size;
6369 dw_attr_node *at_bit_stride;
6370 dw_attr_node *at_byte_size;
6371 dw_attr_node *at_byte_stride;
6372 dw_attr_node *at_const_value;
6373 dw_attr_node *at_containing_type;
6374 dw_attr_node *at_count;
6375 dw_attr_node *at_data_location;
6376 dw_attr_node *at_data_member_location;
6377 dw_attr_node *at_decimal_scale;
6378 dw_attr_node *at_decimal_sign;
6379 dw_attr_node *at_default_value;
6380 dw_attr_node *at_digit_count;
6381 dw_attr_node *at_discr;
6382 dw_attr_node *at_discr_list;
6383 dw_attr_node *at_discr_value;
6384 dw_attr_node *at_encoding;
6385 dw_attr_node *at_endianity;
6386 dw_attr_node *at_explicit;
6387 dw_attr_node *at_is_optional;
6388 dw_attr_node *at_location;
6389 dw_attr_node *at_lower_bound;
6390 dw_attr_node *at_mutable;
6391 dw_attr_node *at_ordering;
6392 dw_attr_node *at_picture_string;
6393 dw_attr_node *at_prototyped;
6394 dw_attr_node *at_small;
6395 dw_attr_node *at_segment;
6396 dw_attr_node *at_string_length;
6397 dw_attr_node *at_string_length_bit_size;
6398 dw_attr_node *at_string_length_byte_size;
6399 dw_attr_node *at_threads_scaled;
6400 dw_attr_node *at_upper_bound;
6401 dw_attr_node *at_use_location;
6402 dw_attr_node *at_use_UTF8;
6403 dw_attr_node *at_variable_parameter;
6404 dw_attr_node *at_virtuality;
6405 dw_attr_node *at_visibility;
6406 dw_attr_node *at_vtable_elem_location;
6409 /* Collect the attributes that we will want to use for the checksum. */
6411 static void
6412 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6414 dw_attr_node *a;
6415 unsigned ix;
6417 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6419 switch (a->dw_attr)
6421 case DW_AT_name:
6422 attrs->at_name = a;
6423 break;
6424 case DW_AT_type:
6425 attrs->at_type = a;
6426 break;
6427 case DW_AT_friend:
6428 attrs->at_friend = a;
6429 break;
6430 case DW_AT_accessibility:
6431 attrs->at_accessibility = a;
6432 break;
6433 case DW_AT_address_class:
6434 attrs->at_address_class = a;
6435 break;
6436 case DW_AT_allocated:
6437 attrs->at_allocated = a;
6438 break;
6439 case DW_AT_artificial:
6440 attrs->at_artificial = a;
6441 break;
6442 case DW_AT_associated:
6443 attrs->at_associated = a;
6444 break;
6445 case DW_AT_binary_scale:
6446 attrs->at_binary_scale = a;
6447 break;
6448 case DW_AT_bit_offset:
6449 attrs->at_bit_offset = a;
6450 break;
6451 case DW_AT_bit_size:
6452 attrs->at_bit_size = a;
6453 break;
6454 case DW_AT_bit_stride:
6455 attrs->at_bit_stride = a;
6456 break;
6457 case DW_AT_byte_size:
6458 attrs->at_byte_size = a;
6459 break;
6460 case DW_AT_byte_stride:
6461 attrs->at_byte_stride = a;
6462 break;
6463 case DW_AT_const_value:
6464 attrs->at_const_value = a;
6465 break;
6466 case DW_AT_containing_type:
6467 attrs->at_containing_type = a;
6468 break;
6469 case DW_AT_count:
6470 attrs->at_count = a;
6471 break;
6472 case DW_AT_data_location:
6473 attrs->at_data_location = a;
6474 break;
6475 case DW_AT_data_member_location:
6476 attrs->at_data_member_location = a;
6477 break;
6478 case DW_AT_decimal_scale:
6479 attrs->at_decimal_scale = a;
6480 break;
6481 case DW_AT_decimal_sign:
6482 attrs->at_decimal_sign = a;
6483 break;
6484 case DW_AT_default_value:
6485 attrs->at_default_value = a;
6486 break;
6487 case DW_AT_digit_count:
6488 attrs->at_digit_count = a;
6489 break;
6490 case DW_AT_discr:
6491 attrs->at_discr = a;
6492 break;
6493 case DW_AT_discr_list:
6494 attrs->at_discr_list = a;
6495 break;
6496 case DW_AT_discr_value:
6497 attrs->at_discr_value = a;
6498 break;
6499 case DW_AT_encoding:
6500 attrs->at_encoding = a;
6501 break;
6502 case DW_AT_endianity:
6503 attrs->at_endianity = a;
6504 break;
6505 case DW_AT_explicit:
6506 attrs->at_explicit = a;
6507 break;
6508 case DW_AT_is_optional:
6509 attrs->at_is_optional = a;
6510 break;
6511 case DW_AT_location:
6512 attrs->at_location = a;
6513 break;
6514 case DW_AT_lower_bound:
6515 attrs->at_lower_bound = a;
6516 break;
6517 case DW_AT_mutable:
6518 attrs->at_mutable = a;
6519 break;
6520 case DW_AT_ordering:
6521 attrs->at_ordering = a;
6522 break;
6523 case DW_AT_picture_string:
6524 attrs->at_picture_string = a;
6525 break;
6526 case DW_AT_prototyped:
6527 attrs->at_prototyped = a;
6528 break;
6529 case DW_AT_small:
6530 attrs->at_small = a;
6531 break;
6532 case DW_AT_segment:
6533 attrs->at_segment = a;
6534 break;
6535 case DW_AT_string_length:
6536 attrs->at_string_length = a;
6537 break;
6538 case DW_AT_string_length_bit_size:
6539 attrs->at_string_length_bit_size = a;
6540 break;
6541 case DW_AT_string_length_byte_size:
6542 attrs->at_string_length_byte_size = a;
6543 break;
6544 case DW_AT_threads_scaled:
6545 attrs->at_threads_scaled = a;
6546 break;
6547 case DW_AT_upper_bound:
6548 attrs->at_upper_bound = a;
6549 break;
6550 case DW_AT_use_location:
6551 attrs->at_use_location = a;
6552 break;
6553 case DW_AT_use_UTF8:
6554 attrs->at_use_UTF8 = a;
6555 break;
6556 case DW_AT_variable_parameter:
6557 attrs->at_variable_parameter = a;
6558 break;
6559 case DW_AT_virtuality:
6560 attrs->at_virtuality = a;
6561 break;
6562 case DW_AT_visibility:
6563 attrs->at_visibility = a;
6564 break;
6565 case DW_AT_vtable_elem_location:
6566 attrs->at_vtable_elem_location = a;
6567 break;
6568 default:
6569 break;
6574 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6576 static void
6577 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6579 dw_die_ref c;
6580 dw_die_ref decl;
6581 struct checksum_attributes attrs;
6583 CHECKSUM_ULEB128 ('D');
6584 CHECKSUM_ULEB128 (die->die_tag);
6586 memset (&attrs, 0, sizeof (attrs));
6588 decl = get_AT_ref (die, DW_AT_specification);
6589 if (decl != NULL)
6590 collect_checksum_attributes (&attrs, decl);
6591 collect_checksum_attributes (&attrs, die);
6593 CHECKSUM_ATTR (attrs.at_name);
6594 CHECKSUM_ATTR (attrs.at_accessibility);
6595 CHECKSUM_ATTR (attrs.at_address_class);
6596 CHECKSUM_ATTR (attrs.at_allocated);
6597 CHECKSUM_ATTR (attrs.at_artificial);
6598 CHECKSUM_ATTR (attrs.at_associated);
6599 CHECKSUM_ATTR (attrs.at_binary_scale);
6600 CHECKSUM_ATTR (attrs.at_bit_offset);
6601 CHECKSUM_ATTR (attrs.at_bit_size);
6602 CHECKSUM_ATTR (attrs.at_bit_stride);
6603 CHECKSUM_ATTR (attrs.at_byte_size);
6604 CHECKSUM_ATTR (attrs.at_byte_stride);
6605 CHECKSUM_ATTR (attrs.at_const_value);
6606 CHECKSUM_ATTR (attrs.at_containing_type);
6607 CHECKSUM_ATTR (attrs.at_count);
6608 CHECKSUM_ATTR (attrs.at_data_location);
6609 CHECKSUM_ATTR (attrs.at_data_member_location);
6610 CHECKSUM_ATTR (attrs.at_decimal_scale);
6611 CHECKSUM_ATTR (attrs.at_decimal_sign);
6612 CHECKSUM_ATTR (attrs.at_default_value);
6613 CHECKSUM_ATTR (attrs.at_digit_count);
6614 CHECKSUM_ATTR (attrs.at_discr);
6615 CHECKSUM_ATTR (attrs.at_discr_list);
6616 CHECKSUM_ATTR (attrs.at_discr_value);
6617 CHECKSUM_ATTR (attrs.at_encoding);
6618 CHECKSUM_ATTR (attrs.at_endianity);
6619 CHECKSUM_ATTR (attrs.at_explicit);
6620 CHECKSUM_ATTR (attrs.at_is_optional);
6621 CHECKSUM_ATTR (attrs.at_location);
6622 CHECKSUM_ATTR (attrs.at_lower_bound);
6623 CHECKSUM_ATTR (attrs.at_mutable);
6624 CHECKSUM_ATTR (attrs.at_ordering);
6625 CHECKSUM_ATTR (attrs.at_picture_string);
6626 CHECKSUM_ATTR (attrs.at_prototyped);
6627 CHECKSUM_ATTR (attrs.at_small);
6628 CHECKSUM_ATTR (attrs.at_segment);
6629 CHECKSUM_ATTR (attrs.at_string_length);
6630 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6631 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6632 CHECKSUM_ATTR (attrs.at_threads_scaled);
6633 CHECKSUM_ATTR (attrs.at_upper_bound);
6634 CHECKSUM_ATTR (attrs.at_use_location);
6635 CHECKSUM_ATTR (attrs.at_use_UTF8);
6636 CHECKSUM_ATTR (attrs.at_variable_parameter);
6637 CHECKSUM_ATTR (attrs.at_virtuality);
6638 CHECKSUM_ATTR (attrs.at_visibility);
6639 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6640 CHECKSUM_ATTR (attrs.at_type);
6641 CHECKSUM_ATTR (attrs.at_friend);
6643 /* Checksum the child DIEs. */
6644 c = die->die_child;
6645 if (c) do {
6646 dw_attr_node *name_attr;
6648 c = c->die_sib;
6649 name_attr = get_AT (c, DW_AT_name);
6650 if (is_template_instantiation (c))
6652 /* Ignore instantiations of member type and function templates. */
6654 else if (name_attr != NULL
6655 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6657 /* Use a shallow checksum for named nested types and member
6658 functions. */
6659 CHECKSUM_ULEB128 ('S');
6660 CHECKSUM_ULEB128 (c->die_tag);
6661 CHECKSUM_STRING (AT_string (name_attr));
6663 else
6665 /* Use a deep checksum for other children. */
6666 /* Mark this DIE so it gets processed when unmarking. */
6667 if (c->die_mark == 0)
6668 c->die_mark = -1;
6669 die_checksum_ordered (c, ctx, mark);
6671 } while (c != die->die_child);
6673 CHECKSUM_ULEB128 (0);
6676 /* Add a type name and tag to a hash. */
6677 static void
6678 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6680 CHECKSUM_ULEB128 (tag);
6681 CHECKSUM_STRING (name);
6684 #undef CHECKSUM
6685 #undef CHECKSUM_STRING
6686 #undef CHECKSUM_ATTR
6687 #undef CHECKSUM_LEB128
6688 #undef CHECKSUM_ULEB128
6690 /* Generate the type signature for DIE. This is computed by generating an
6691 MD5 checksum over the DIE's tag, its relevant attributes, and its
6692 children. Attributes that are references to other DIEs are processed
6693 by recursion, using the MARK field to prevent infinite recursion.
6694 If the DIE is nested inside a namespace or another type, we also
6695 need to include that context in the signature. The lower 64 bits
6696 of the resulting MD5 checksum comprise the signature. */
6698 static void
6699 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6701 int mark;
6702 const char *name;
6703 unsigned char checksum[16];
6704 struct md5_ctx ctx;
6705 dw_die_ref decl;
6706 dw_die_ref parent;
6708 name = get_AT_string (die, DW_AT_name);
6709 decl = get_AT_ref (die, DW_AT_specification);
6710 parent = get_die_parent (die);
6712 /* First, compute a signature for just the type name (and its surrounding
6713 context, if any. This is stored in the type unit DIE for link-time
6714 ODR (one-definition rule) checking. */
6716 if (is_cxx () && name != NULL)
6718 md5_init_ctx (&ctx);
6720 /* Checksum the names of surrounding namespaces and structures. */
6721 if (parent != NULL)
6722 checksum_die_context (parent, &ctx);
6724 /* Checksum the current DIE. */
6725 die_odr_checksum (die->die_tag, name, &ctx);
6726 md5_finish_ctx (&ctx, checksum);
6728 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6731 /* Next, compute the complete type signature. */
6733 md5_init_ctx (&ctx);
6734 mark = 1;
6735 die->die_mark = mark;
6737 /* Checksum the names of surrounding namespaces and structures. */
6738 if (parent != NULL)
6739 checksum_die_context (parent, &ctx);
6741 /* Checksum the DIE and its children. */
6742 die_checksum_ordered (die, &ctx, &mark);
6743 unmark_all_dies (die);
6744 md5_finish_ctx (&ctx, checksum);
6746 /* Store the signature in the type node and link the type DIE and the
6747 type node together. */
6748 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6749 DWARF_TYPE_SIGNATURE_SIZE);
6750 die->comdat_type_p = true;
6751 die->die_id.die_type_node = type_node;
6752 type_node->type_die = die;
6754 /* If the DIE is a specification, link its declaration to the type node
6755 as well. */
6756 if (decl != NULL)
6758 decl->comdat_type_p = true;
6759 decl->die_id.die_type_node = type_node;
6763 /* Do the location expressions look same? */
6764 static inline int
6765 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6767 return loc1->dw_loc_opc == loc2->dw_loc_opc
6768 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6769 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6772 /* Do the values look the same? */
6773 static int
6774 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6776 dw_loc_descr_ref loc1, loc2;
6777 rtx r1, r2;
6779 if (v1->val_class != v2->val_class)
6780 return 0;
6782 switch (v1->val_class)
6784 case dw_val_class_const:
6785 return v1->v.val_int == v2->v.val_int;
6786 case dw_val_class_unsigned_const:
6787 return v1->v.val_unsigned == v2->v.val_unsigned;
6788 case dw_val_class_const_double:
6789 return v1->v.val_double.high == v2->v.val_double.high
6790 && v1->v.val_double.low == v2->v.val_double.low;
6791 case dw_val_class_wide_int:
6792 return *v1->v.val_wide == *v2->v.val_wide;
6793 case dw_val_class_vec:
6794 if (v1->v.val_vec.length != v2->v.val_vec.length
6795 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6796 return 0;
6797 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6798 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6799 return 0;
6800 return 1;
6801 case dw_val_class_flag:
6802 return v1->v.val_flag == v2->v.val_flag;
6803 case dw_val_class_str:
6804 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6806 case dw_val_class_addr:
6807 r1 = v1->v.val_addr;
6808 r2 = v2->v.val_addr;
6809 if (GET_CODE (r1) != GET_CODE (r2))
6810 return 0;
6811 return !rtx_equal_p (r1, r2);
6813 case dw_val_class_offset:
6814 return v1->v.val_offset == v2->v.val_offset;
6816 case dw_val_class_loc:
6817 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6818 loc1 && loc2;
6819 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6820 if (!same_loc_p (loc1, loc2, mark))
6821 return 0;
6822 return !loc1 && !loc2;
6824 case dw_val_class_die_ref:
6825 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6827 case dw_val_class_fde_ref:
6828 case dw_val_class_vms_delta:
6829 case dw_val_class_lbl_id:
6830 case dw_val_class_lineptr:
6831 case dw_val_class_macptr:
6832 case dw_val_class_high_pc:
6833 return 1;
6835 case dw_val_class_file:
6836 return v1->v.val_file == v2->v.val_file;
6838 case dw_val_class_data8:
6839 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6841 default:
6842 return 1;
6846 /* Do the attributes look the same? */
6848 static int
6849 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6851 if (at1->dw_attr != at2->dw_attr)
6852 return 0;
6854 /* We don't care that this was compiled with a different compiler
6855 snapshot; if the output is the same, that's what matters. */
6856 if (at1->dw_attr == DW_AT_producer)
6857 return 1;
6859 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6862 /* Do the dies look the same? */
6864 static int
6865 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6867 dw_die_ref c1, c2;
6868 dw_attr_node *a1;
6869 unsigned ix;
6871 /* To avoid infinite recursion. */
6872 if (die1->die_mark)
6873 return die1->die_mark == die2->die_mark;
6874 die1->die_mark = die2->die_mark = ++(*mark);
6876 if (die1->die_tag != die2->die_tag)
6877 return 0;
6879 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6880 return 0;
6882 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6883 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6884 return 0;
6886 c1 = die1->die_child;
6887 c2 = die2->die_child;
6888 if (! c1)
6890 if (c2)
6891 return 0;
6893 else
6894 for (;;)
6896 if (!same_die_p (c1, c2, mark))
6897 return 0;
6898 c1 = c1->die_sib;
6899 c2 = c2->die_sib;
6900 if (c1 == die1->die_child)
6902 if (c2 == die2->die_child)
6903 break;
6904 else
6905 return 0;
6909 return 1;
6912 /* Do the dies look the same? Wrapper around same_die_p. */
6914 static int
6915 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6917 int mark = 0;
6918 int ret = same_die_p (die1, die2, &mark);
6920 unmark_all_dies (die1);
6921 unmark_all_dies (die2);
6923 return ret;
6926 /* The prefix to attach to symbols on DIEs in the current comdat debug
6927 info section. */
6928 static const char *comdat_symbol_id;
6930 /* The index of the current symbol within the current comdat CU. */
6931 static unsigned int comdat_symbol_number;
6933 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6934 children, and set comdat_symbol_id accordingly. */
6936 static void
6937 compute_section_prefix (dw_die_ref unit_die)
6939 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6940 const char *base = die_name ? lbasename (die_name) : "anonymous";
6941 char *name = XALLOCAVEC (char, strlen (base) + 64);
6942 char *p;
6943 int i, mark;
6944 unsigned char checksum[16];
6945 struct md5_ctx ctx;
6947 /* Compute the checksum of the DIE, then append part of it as hex digits to
6948 the name filename of the unit. */
6950 md5_init_ctx (&ctx);
6951 mark = 0;
6952 die_checksum (unit_die, &ctx, &mark);
6953 unmark_all_dies (unit_die);
6954 md5_finish_ctx (&ctx, checksum);
6956 sprintf (name, "%s.", base);
6957 clean_symbol_name (name);
6959 p = name + strlen (name);
6960 for (i = 0; i < 4; i++)
6962 sprintf (p, "%.2x", checksum[i]);
6963 p += 2;
6966 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6967 comdat_symbol_number = 0;
6970 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6972 static int
6973 is_type_die (dw_die_ref die)
6975 switch (die->die_tag)
6977 case DW_TAG_array_type:
6978 case DW_TAG_class_type:
6979 case DW_TAG_interface_type:
6980 case DW_TAG_enumeration_type:
6981 case DW_TAG_pointer_type:
6982 case DW_TAG_reference_type:
6983 case DW_TAG_rvalue_reference_type:
6984 case DW_TAG_string_type:
6985 case DW_TAG_structure_type:
6986 case DW_TAG_subroutine_type:
6987 case DW_TAG_union_type:
6988 case DW_TAG_ptr_to_member_type:
6989 case DW_TAG_set_type:
6990 case DW_TAG_subrange_type:
6991 case DW_TAG_base_type:
6992 case DW_TAG_const_type:
6993 case DW_TAG_file_type:
6994 case DW_TAG_packed_type:
6995 case DW_TAG_volatile_type:
6996 case DW_TAG_typedef:
6997 return 1;
6998 default:
6999 return 0;
7003 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7004 Basically, we want to choose the bits that are likely to be shared between
7005 compilations (types) and leave out the bits that are specific to individual
7006 compilations (functions). */
7008 static int
7009 is_comdat_die (dw_die_ref c)
7011 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7012 we do for stabs. The advantage is a greater likelihood of sharing between
7013 objects that don't include headers in the same order (and therefore would
7014 put the base types in a different comdat). jason 8/28/00 */
7016 if (c->die_tag == DW_TAG_base_type)
7017 return 0;
7019 if (c->die_tag == DW_TAG_pointer_type
7020 || c->die_tag == DW_TAG_reference_type
7021 || c->die_tag == DW_TAG_rvalue_reference_type
7022 || c->die_tag == DW_TAG_const_type
7023 || c->die_tag == DW_TAG_volatile_type)
7025 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7027 return t ? is_comdat_die (t) : 0;
7030 return is_type_die (c);
7033 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7034 compilation unit. */
7036 static int
7037 is_symbol_die (dw_die_ref c)
7039 return (is_type_die (c)
7040 || is_declaration_die (c)
7041 || c->die_tag == DW_TAG_namespace
7042 || c->die_tag == DW_TAG_module);
7045 /* Returns true iff C is a compile-unit DIE. */
7047 static inline bool
7048 is_cu_die (dw_die_ref c)
7050 return c && c->die_tag == DW_TAG_compile_unit;
7053 /* Returns true iff C is a unit DIE of some sort. */
7055 static inline bool
7056 is_unit_die (dw_die_ref c)
7058 return c && (c->die_tag == DW_TAG_compile_unit
7059 || c->die_tag == DW_TAG_partial_unit
7060 || c->die_tag == DW_TAG_type_unit);
7063 /* Returns true iff C is a namespace DIE. */
7065 static inline bool
7066 is_namespace_die (dw_die_ref c)
7068 return c && c->die_tag == DW_TAG_namespace;
7071 /* Returns true iff C is a class or structure DIE. */
7073 static inline bool
7074 is_class_die (dw_die_ref c)
7076 return c && (c->die_tag == DW_TAG_class_type
7077 || c->die_tag == DW_TAG_structure_type);
7080 /* Return non-zero if this DIE is a template parameter. */
7082 static inline bool
7083 is_template_parameter (dw_die_ref die)
7085 switch (die->die_tag)
7087 case DW_TAG_template_type_param:
7088 case DW_TAG_template_value_param:
7089 case DW_TAG_GNU_template_template_param:
7090 case DW_TAG_GNU_template_parameter_pack:
7091 return true;
7092 default:
7093 return false;
7097 /* Return non-zero if this DIE represents a template instantiation. */
7099 static inline bool
7100 is_template_instantiation (dw_die_ref die)
7102 dw_die_ref c;
7104 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7105 return false;
7106 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7107 return false;
7110 static char *
7111 gen_internal_sym (const char *prefix)
7113 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7115 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7116 return xstrdup (buf);
7119 /* Assign symbols to all worthy DIEs under DIE. */
7121 static void
7122 assign_symbol_names (dw_die_ref die)
7124 dw_die_ref c;
7126 if (is_symbol_die (die) && !die->comdat_type_p)
7128 if (comdat_symbol_id)
7130 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7132 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7133 comdat_symbol_id, comdat_symbol_number++);
7134 die->die_id.die_symbol = xstrdup (p);
7136 else
7137 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7140 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7143 struct cu_hash_table_entry
7145 dw_die_ref cu;
7146 unsigned min_comdat_num, max_comdat_num;
7147 struct cu_hash_table_entry *next;
7150 /* Helpers to manipulate hash table of CUs. */
7152 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7154 typedef die_struct *compare_type;
7155 static inline hashval_t hash (const cu_hash_table_entry *);
7156 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7157 static inline void remove (cu_hash_table_entry *);
7160 inline hashval_t
7161 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7163 return htab_hash_string (entry->cu->die_id.die_symbol);
7166 inline bool
7167 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7168 const die_struct *entry2)
7170 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7173 inline void
7174 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7176 struct cu_hash_table_entry *next;
7178 while (entry)
7180 next = entry->next;
7181 free (entry);
7182 entry = next;
7186 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7188 /* Check whether we have already seen this CU and set up SYM_NUM
7189 accordingly. */
7190 static int
7191 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7193 struct cu_hash_table_entry dummy;
7194 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7196 dummy.max_comdat_num = 0;
7198 slot = htable->find_slot_with_hash (cu,
7199 htab_hash_string (cu->die_id.die_symbol),
7200 INSERT);
7201 entry = *slot;
7203 for (; entry; last = entry, entry = entry->next)
7205 if (same_die_p_wrap (cu, entry->cu))
7206 break;
7209 if (entry)
7211 *sym_num = entry->min_comdat_num;
7212 return 1;
7215 entry = XCNEW (struct cu_hash_table_entry);
7216 entry->cu = cu;
7217 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7218 entry->next = *slot;
7219 *slot = entry;
7221 return 0;
7224 /* Record SYM_NUM to record of CU in HTABLE. */
7225 static void
7226 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7227 unsigned int sym_num)
7229 struct cu_hash_table_entry **slot, *entry;
7231 slot = htable->find_slot_with_hash (cu,
7232 htab_hash_string (cu->die_id.die_symbol),
7233 NO_INSERT);
7234 entry = *slot;
7236 entry->max_comdat_num = sym_num;
7239 /* Traverse the DIE (which is always comp_unit_die), and set up
7240 additional compilation units for each of the include files we see
7241 bracketed by BINCL/EINCL. */
7243 static void
7244 break_out_includes (dw_die_ref die)
7246 dw_die_ref c;
7247 dw_die_ref unit = NULL;
7248 limbo_die_node *node, **pnode;
7250 c = die->die_child;
7251 if (c) do {
7252 dw_die_ref prev = c;
7253 c = c->die_sib;
7254 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7255 || (unit && is_comdat_die (c)))
7257 dw_die_ref next = c->die_sib;
7259 /* This DIE is for a secondary CU; remove it from the main one. */
7260 remove_child_with_prev (c, prev);
7262 if (c->die_tag == DW_TAG_GNU_BINCL)
7263 unit = push_new_compile_unit (unit, c);
7264 else if (c->die_tag == DW_TAG_GNU_EINCL)
7265 unit = pop_compile_unit (unit);
7266 else
7267 add_child_die (unit, c);
7268 c = next;
7269 if (c == die->die_child)
7270 break;
7272 } while (c != die->die_child);
7274 #if 0
7275 /* We can only use this in debugging, since the frontend doesn't check
7276 to make sure that we leave every include file we enter. */
7277 gcc_assert (!unit);
7278 #endif
7280 assign_symbol_names (die);
7281 cu_hash_type cu_hash_table (10);
7282 for (node = limbo_die_list, pnode = &limbo_die_list;
7283 node;
7284 node = node->next)
7286 int is_dupl;
7288 compute_section_prefix (node->die);
7289 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7290 &comdat_symbol_number);
7291 assign_symbol_names (node->die);
7292 if (is_dupl)
7293 *pnode = node->next;
7294 else
7296 pnode = &node->next;
7297 record_comdat_symbol_number (node->die, &cu_hash_table,
7298 comdat_symbol_number);
7303 /* Return non-zero if this DIE is a declaration. */
7305 static int
7306 is_declaration_die (dw_die_ref die)
7308 dw_attr_node *a;
7309 unsigned ix;
7311 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7312 if (a->dw_attr == DW_AT_declaration)
7313 return 1;
7315 return 0;
7318 /* Return non-zero if this DIE is nested inside a subprogram. */
7320 static int
7321 is_nested_in_subprogram (dw_die_ref die)
7323 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7325 if (decl == NULL)
7326 decl = die;
7327 return local_scope_p (decl);
7330 /* Return non-zero if this DIE contains a defining declaration of a
7331 subprogram. */
7333 static int
7334 contains_subprogram_definition (dw_die_ref die)
7336 dw_die_ref c;
7338 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7339 return 1;
7340 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7341 return 0;
7344 /* Return non-zero if this is a type DIE that should be moved to a
7345 COMDAT .debug_types section. */
7347 static int
7348 should_move_die_to_comdat (dw_die_ref die)
7350 switch (die->die_tag)
7352 case DW_TAG_class_type:
7353 case DW_TAG_structure_type:
7354 case DW_TAG_enumeration_type:
7355 case DW_TAG_union_type:
7356 /* Don't move declarations, inlined instances, types nested in a
7357 subprogram, or types that contain subprogram definitions. */
7358 if (is_declaration_die (die)
7359 || get_AT (die, DW_AT_abstract_origin)
7360 || is_nested_in_subprogram (die)
7361 || contains_subprogram_definition (die))
7362 return 0;
7363 return 1;
7364 case DW_TAG_array_type:
7365 case DW_TAG_interface_type:
7366 case DW_TAG_pointer_type:
7367 case DW_TAG_reference_type:
7368 case DW_TAG_rvalue_reference_type:
7369 case DW_TAG_string_type:
7370 case DW_TAG_subroutine_type:
7371 case DW_TAG_ptr_to_member_type:
7372 case DW_TAG_set_type:
7373 case DW_TAG_subrange_type:
7374 case DW_TAG_base_type:
7375 case DW_TAG_const_type:
7376 case DW_TAG_file_type:
7377 case DW_TAG_packed_type:
7378 case DW_TAG_volatile_type:
7379 case DW_TAG_typedef:
7380 default:
7381 return 0;
7385 /* Make a clone of DIE. */
7387 static dw_die_ref
7388 clone_die (dw_die_ref die)
7390 dw_die_ref clone;
7391 dw_attr_node *a;
7392 unsigned ix;
7394 clone = ggc_cleared_alloc<die_node> ();
7395 clone->die_tag = die->die_tag;
7397 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7398 add_dwarf_attr (clone, a);
7400 return clone;
7403 /* Make a clone of the tree rooted at DIE. */
7405 static dw_die_ref
7406 clone_tree (dw_die_ref die)
7408 dw_die_ref c;
7409 dw_die_ref clone = clone_die (die);
7411 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7413 return clone;
7416 /* Make a clone of DIE as a declaration. */
7418 static dw_die_ref
7419 clone_as_declaration (dw_die_ref die)
7421 dw_die_ref clone;
7422 dw_die_ref decl;
7423 dw_attr_node *a;
7424 unsigned ix;
7426 /* If the DIE is already a declaration, just clone it. */
7427 if (is_declaration_die (die))
7428 return clone_die (die);
7430 /* If the DIE is a specification, just clone its declaration DIE. */
7431 decl = get_AT_ref (die, DW_AT_specification);
7432 if (decl != NULL)
7434 clone = clone_die (decl);
7435 if (die->comdat_type_p)
7436 add_AT_die_ref (clone, DW_AT_signature, die);
7437 return clone;
7440 clone = ggc_cleared_alloc<die_node> ();
7441 clone->die_tag = die->die_tag;
7443 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7445 /* We don't want to copy over all attributes.
7446 For example we don't want DW_AT_byte_size because otherwise we will no
7447 longer have a declaration and GDB will treat it as a definition. */
7449 switch (a->dw_attr)
7451 case DW_AT_abstract_origin:
7452 case DW_AT_artificial:
7453 case DW_AT_containing_type:
7454 case DW_AT_external:
7455 case DW_AT_name:
7456 case DW_AT_type:
7457 case DW_AT_virtuality:
7458 case DW_AT_linkage_name:
7459 case DW_AT_MIPS_linkage_name:
7460 add_dwarf_attr (clone, a);
7461 break;
7462 case DW_AT_byte_size:
7463 default:
7464 break;
7468 if (die->comdat_type_p)
7469 add_AT_die_ref (clone, DW_AT_signature, die);
7471 add_AT_flag (clone, DW_AT_declaration, 1);
7472 return clone;
7476 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7478 struct decl_table_entry
7480 dw_die_ref orig;
7481 dw_die_ref copy;
7484 /* Helpers to manipulate hash table of copied declarations. */
7486 /* Hashtable helpers. */
7488 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7490 typedef die_struct *compare_type;
7491 static inline hashval_t hash (const decl_table_entry *);
7492 static inline bool equal (const decl_table_entry *, const die_struct *);
7495 inline hashval_t
7496 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7498 return htab_hash_pointer (entry->orig);
7501 inline bool
7502 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7503 const die_struct *entry2)
7505 return entry1->orig == entry2;
7508 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7510 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7511 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7512 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7513 to check if the ancestor has already been copied into UNIT. */
7515 static dw_die_ref
7516 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7517 decl_hash_type *decl_table)
7519 dw_die_ref parent = die->die_parent;
7520 dw_die_ref new_parent = unit;
7521 dw_die_ref copy;
7522 decl_table_entry **slot = NULL;
7523 struct decl_table_entry *entry = NULL;
7525 if (decl_table)
7527 /* Check if the entry has already been copied to UNIT. */
7528 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7529 INSERT);
7530 if (*slot != HTAB_EMPTY_ENTRY)
7532 entry = *slot;
7533 return entry->copy;
7536 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7537 entry = XCNEW (struct decl_table_entry);
7538 entry->orig = die;
7539 entry->copy = NULL;
7540 *slot = entry;
7543 if (parent != NULL)
7545 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7546 if (spec != NULL)
7547 parent = spec;
7548 if (!is_unit_die (parent))
7549 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7552 copy = clone_as_declaration (die);
7553 add_child_die (new_parent, copy);
7555 if (decl_table)
7557 /* Record the pointer to the copy. */
7558 entry->copy = copy;
7561 return copy;
7563 /* Copy the declaration context to the new type unit DIE. This includes
7564 any surrounding namespace or type declarations. If the DIE has an
7565 AT_specification attribute, it also includes attributes and children
7566 attached to the specification, and returns a pointer to the original
7567 parent of the declaration DIE. Returns NULL otherwise. */
7569 static dw_die_ref
7570 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7572 dw_die_ref decl;
7573 dw_die_ref new_decl;
7574 dw_die_ref orig_parent = NULL;
7576 decl = get_AT_ref (die, DW_AT_specification);
7577 if (decl == NULL)
7578 decl = die;
7579 else
7581 unsigned ix;
7582 dw_die_ref c;
7583 dw_attr_node *a;
7585 /* The original DIE will be changed to a declaration, and must
7586 be moved to be a child of the original declaration DIE. */
7587 orig_parent = decl->die_parent;
7589 /* Copy the type node pointer from the new DIE to the original
7590 declaration DIE so we can forward references later. */
7591 decl->comdat_type_p = true;
7592 decl->die_id.die_type_node = die->die_id.die_type_node;
7594 remove_AT (die, DW_AT_specification);
7596 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7598 if (a->dw_attr != DW_AT_name
7599 && a->dw_attr != DW_AT_declaration
7600 && a->dw_attr != DW_AT_external)
7601 add_dwarf_attr (die, a);
7604 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7607 if (decl->die_parent != NULL
7608 && !is_unit_die (decl->die_parent))
7610 new_decl = copy_ancestor_tree (unit, decl, NULL);
7611 if (new_decl != NULL)
7613 remove_AT (new_decl, DW_AT_signature);
7614 add_AT_specification (die, new_decl);
7618 return orig_parent;
7621 /* Generate the skeleton ancestor tree for the given NODE, then clone
7622 the DIE and add the clone into the tree. */
7624 static void
7625 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7627 if (node->new_die != NULL)
7628 return;
7630 node->new_die = clone_as_declaration (node->old_die);
7632 if (node->parent != NULL)
7634 generate_skeleton_ancestor_tree (node->parent);
7635 add_child_die (node->parent->new_die, node->new_die);
7639 /* Generate a skeleton tree of DIEs containing any declarations that are
7640 found in the original tree. We traverse the tree looking for declaration
7641 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7643 static void
7644 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7646 skeleton_chain_node node;
7647 dw_die_ref c;
7648 dw_die_ref first;
7649 dw_die_ref prev = NULL;
7650 dw_die_ref next = NULL;
7652 node.parent = parent;
7654 first = c = parent->old_die->die_child;
7655 if (c)
7656 next = c->die_sib;
7657 if (c) do {
7658 if (prev == NULL || prev->die_sib == c)
7659 prev = c;
7660 c = next;
7661 next = (c == first ? NULL : c->die_sib);
7662 node.old_die = c;
7663 node.new_die = NULL;
7664 if (is_declaration_die (c))
7666 if (is_template_instantiation (c))
7668 /* Instantiated templates do not need to be cloned into the
7669 type unit. Just move the DIE and its children back to
7670 the skeleton tree (in the main CU). */
7671 remove_child_with_prev (c, prev);
7672 add_child_die (parent->new_die, c);
7673 c = prev;
7675 else
7677 /* Clone the existing DIE, move the original to the skeleton
7678 tree (which is in the main CU), and put the clone, with
7679 all the original's children, where the original came from
7680 (which is about to be moved to the type unit). */
7681 dw_die_ref clone = clone_die (c);
7682 move_all_children (c, clone);
7684 /* If the original has a DW_AT_object_pointer attribute,
7685 it would now point to a child DIE just moved to the
7686 cloned tree, so we need to remove that attribute from
7687 the original. */
7688 remove_AT (c, DW_AT_object_pointer);
7690 replace_child (c, clone, prev);
7691 generate_skeleton_ancestor_tree (parent);
7692 add_child_die (parent->new_die, c);
7693 node.new_die = c;
7694 c = clone;
7697 generate_skeleton_bottom_up (&node);
7698 } while (next != NULL);
7701 /* Wrapper function for generate_skeleton_bottom_up. */
7703 static dw_die_ref
7704 generate_skeleton (dw_die_ref die)
7706 skeleton_chain_node node;
7708 node.old_die = die;
7709 node.new_die = NULL;
7710 node.parent = NULL;
7712 /* If this type definition is nested inside another type,
7713 and is not an instantiation of a template, always leave
7714 at least a declaration in its place. */
7715 if (die->die_parent != NULL
7716 && is_type_die (die->die_parent)
7717 && !is_template_instantiation (die))
7718 node.new_die = clone_as_declaration (die);
7720 generate_skeleton_bottom_up (&node);
7721 return node.new_die;
7724 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7725 declaration. The original DIE is moved to a new compile unit so that
7726 existing references to it follow it to the new location. If any of the
7727 original DIE's descendants is a declaration, we need to replace the
7728 original DIE with a skeleton tree and move the declarations back into the
7729 skeleton tree. */
7731 static dw_die_ref
7732 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7733 dw_die_ref prev)
7735 dw_die_ref skeleton, orig_parent;
7737 /* Copy the declaration context to the type unit DIE. If the returned
7738 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7739 that DIE. */
7740 orig_parent = copy_declaration_context (unit, child);
7742 skeleton = generate_skeleton (child);
7743 if (skeleton == NULL)
7744 remove_child_with_prev (child, prev);
7745 else
7747 skeleton->comdat_type_p = true;
7748 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7750 /* If the original DIE was a specification, we need to put
7751 the skeleton under the parent DIE of the declaration.
7752 This leaves the original declaration in the tree, but
7753 it will be pruned later since there are no longer any
7754 references to it. */
7755 if (orig_parent != NULL)
7757 remove_child_with_prev (child, prev);
7758 add_child_die (orig_parent, skeleton);
7760 else
7761 replace_child (child, skeleton, prev);
7764 return skeleton;
7767 static void
7768 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7769 comdat_type_node *type_node,
7770 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7772 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7773 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7774 DWARF procedure references in the DW_AT_location attribute. */
7776 static dw_die_ref
7777 copy_dwarf_procedure (dw_die_ref die,
7778 comdat_type_node *type_node,
7779 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7781 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7783 /* DWARF procedures are not supposed to have children... */
7784 gcc_assert (die->die_child == NULL);
7786 /* ... and they are supposed to have only one attribute: DW_AT_location. */
7787 gcc_assert (vec_safe_length (die->die_attr) == 1
7788 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7790 /* Do not copy more than once DWARF procedures. */
7791 bool existed;
7792 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7793 if (existed)
7794 return die_copy;
7796 die_copy = clone_die (die);
7797 add_child_die (type_node->root_die, die_copy);
7798 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7799 return die_copy;
7802 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
7803 procedures in DIE's attributes. */
7805 static void
7806 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7807 comdat_type_node *type_node,
7808 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7810 dw_attr_node *a;
7811 unsigned i;
7813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7815 dw_loc_descr_ref loc;
7817 if (a->dw_attr_val.val_class != dw_val_class_loc)
7818 continue;
7820 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7822 switch (loc->dw_loc_opc)
7824 case DW_OP_call2:
7825 case DW_OP_call4:
7826 case DW_OP_call_ref:
7827 gcc_assert (loc->dw_loc_oprnd1.val_class
7828 == dw_val_class_die_ref);
7829 loc->dw_loc_oprnd1.v.val_die_ref.die
7830 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7831 type_node,
7832 copied_dwarf_procs);
7834 default:
7835 break;
7841 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7842 rewrite references to point to the copies.
7844 References are looked for in DIE's attributes and recursively in all its
7845 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7846 mapping from old DWARF procedures to their copy. It is used not to copy
7847 twice the same DWARF procedure under TYPE_NODE. */
7849 static void
7850 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7851 comdat_type_node *type_node,
7852 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7854 dw_die_ref c;
7856 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7857 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7858 type_node,
7859 copied_dwarf_procs));
7862 /* Traverse the DIE and set up additional .debug_types sections for each
7863 type worthy of being placed in a COMDAT section. */
7865 static void
7866 break_out_comdat_types (dw_die_ref die)
7868 dw_die_ref c;
7869 dw_die_ref first;
7870 dw_die_ref prev = NULL;
7871 dw_die_ref next = NULL;
7872 dw_die_ref unit = NULL;
7874 first = c = die->die_child;
7875 if (c)
7876 next = c->die_sib;
7877 if (c) do {
7878 if (prev == NULL || prev->die_sib == c)
7879 prev = c;
7880 c = next;
7881 next = (c == first ? NULL : c->die_sib);
7882 if (should_move_die_to_comdat (c))
7884 dw_die_ref replacement;
7885 comdat_type_node *type_node;
7887 /* Break out nested types into their own type units. */
7888 break_out_comdat_types (c);
7890 /* Create a new type unit DIE as the root for the new tree, and
7891 add it to the list of comdat types. */
7892 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7893 add_AT_unsigned (unit, DW_AT_language,
7894 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7895 type_node = ggc_cleared_alloc<comdat_type_node> ();
7896 type_node->root_die = unit;
7897 type_node->next = comdat_type_list;
7898 comdat_type_list = type_node;
7900 /* Generate the type signature. */
7901 generate_type_signature (c, type_node);
7903 /* Copy the declaration context, attributes, and children of the
7904 declaration into the new type unit DIE, then remove this DIE
7905 from the main CU (or replace it with a skeleton if necessary). */
7906 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7907 type_node->skeleton_die = replacement;
7909 /* Add the DIE to the new compunit. */
7910 add_child_die (unit, c);
7912 /* Types can reference DWARF procedures for type size or data location
7913 expressions. Calls in DWARF expressions cannot target procedures
7914 that are not in the same section. So we must copy DWARF procedures
7915 along with this type and then rewrite references to them. */
7916 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7917 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7919 if (replacement != NULL)
7920 c = replacement;
7922 else if (c->die_tag == DW_TAG_namespace
7923 || c->die_tag == DW_TAG_class_type
7924 || c->die_tag == DW_TAG_structure_type
7925 || c->die_tag == DW_TAG_union_type)
7927 /* Look for nested types that can be broken out. */
7928 break_out_comdat_types (c);
7930 } while (next != NULL);
7933 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7934 Enter all the cloned children into the hash table decl_table. */
7936 static dw_die_ref
7937 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7939 dw_die_ref c;
7940 dw_die_ref clone;
7941 struct decl_table_entry *entry;
7942 decl_table_entry **slot;
7944 if (die->die_tag == DW_TAG_subprogram)
7945 clone = clone_as_declaration (die);
7946 else
7947 clone = clone_die (die);
7949 slot = decl_table->find_slot_with_hash (die,
7950 htab_hash_pointer (die), INSERT);
7952 /* Assert that DIE isn't in the hash table yet. If it would be there
7953 before, the ancestors would be necessarily there as well, therefore
7954 clone_tree_partial wouldn't be called. */
7955 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7957 entry = XCNEW (struct decl_table_entry);
7958 entry->orig = die;
7959 entry->copy = clone;
7960 *slot = entry;
7962 if (die->die_tag != DW_TAG_subprogram)
7963 FOR_EACH_CHILD (die, c,
7964 add_child_die (clone, clone_tree_partial (c, decl_table)));
7966 return clone;
7969 /* Walk the DIE and its children, looking for references to incomplete
7970 or trivial types that are unmarked (i.e., that are not in the current
7971 type_unit). */
7973 static void
7974 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7976 dw_die_ref c;
7977 dw_attr_node *a;
7978 unsigned ix;
7980 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7982 if (AT_class (a) == dw_val_class_die_ref)
7984 dw_die_ref targ = AT_ref (a);
7985 decl_table_entry **slot;
7986 struct decl_table_entry *entry;
7988 if (targ->die_mark != 0 || targ->comdat_type_p)
7989 continue;
7991 slot = decl_table->find_slot_with_hash (targ,
7992 htab_hash_pointer (targ),
7993 INSERT);
7995 if (*slot != HTAB_EMPTY_ENTRY)
7997 /* TARG has already been copied, so we just need to
7998 modify the reference to point to the copy. */
7999 entry = *slot;
8000 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8002 else
8004 dw_die_ref parent = unit;
8005 dw_die_ref copy = clone_die (targ);
8007 /* Record in DECL_TABLE that TARG has been copied.
8008 Need to do this now, before the recursive call,
8009 because DECL_TABLE may be expanded and SLOT
8010 would no longer be a valid pointer. */
8011 entry = XCNEW (struct decl_table_entry);
8012 entry->orig = targ;
8013 entry->copy = copy;
8014 *slot = entry;
8016 /* If TARG is not a declaration DIE, we need to copy its
8017 children. */
8018 if (!is_declaration_die (targ))
8020 FOR_EACH_CHILD (
8021 targ, c,
8022 add_child_die (copy,
8023 clone_tree_partial (c, decl_table)));
8026 /* Make sure the cloned tree is marked as part of the
8027 type unit. */
8028 mark_dies (copy);
8030 /* If TARG has surrounding context, copy its ancestor tree
8031 into the new type unit. */
8032 if (targ->die_parent != NULL
8033 && !is_unit_die (targ->die_parent))
8034 parent = copy_ancestor_tree (unit, targ->die_parent,
8035 decl_table);
8037 add_child_die (parent, copy);
8038 a->dw_attr_val.v.val_die_ref.die = copy;
8040 /* Make sure the newly-copied DIE is walked. If it was
8041 installed in a previously-added context, it won't
8042 get visited otherwise. */
8043 if (parent != unit)
8045 /* Find the highest point of the newly-added tree,
8046 mark each node along the way, and walk from there. */
8047 parent->die_mark = 1;
8048 while (parent->die_parent
8049 && parent->die_parent->die_mark == 0)
8051 parent = parent->die_parent;
8052 parent->die_mark = 1;
8054 copy_decls_walk (unit, parent, decl_table);
8060 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8063 /* Copy declarations for "unworthy" types into the new comdat section.
8064 Incomplete types, modified types, and certain other types aren't broken
8065 out into comdat sections of their own, so they don't have a signature,
8066 and we need to copy the declaration into the same section so that we
8067 don't have an external reference. */
8069 static void
8070 copy_decls_for_unworthy_types (dw_die_ref unit)
8072 mark_dies (unit);
8073 decl_hash_type decl_table (10);
8074 copy_decls_walk (unit, unit, &decl_table);
8075 unmark_dies (unit);
8078 /* Traverse the DIE and add a sibling attribute if it may have the
8079 effect of speeding up access to siblings. To save some space,
8080 avoid generating sibling attributes for DIE's without children. */
8082 static void
8083 add_sibling_attributes (dw_die_ref die)
8085 dw_die_ref c;
8087 if (! die->die_child)
8088 return;
8090 if (die->die_parent && die != die->die_parent->die_child)
8091 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8093 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8096 /* Output all location lists for the DIE and its children. */
8098 static void
8099 output_location_lists (dw_die_ref die)
8101 dw_die_ref c;
8102 dw_attr_node *a;
8103 unsigned ix;
8105 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8106 if (AT_class (a) == dw_val_class_loc_list)
8107 output_loc_list (AT_loc_list (a));
8109 FOR_EACH_CHILD (die, c, output_location_lists (c));
8112 /* We want to limit the number of external references, because they are
8113 larger than local references: a relocation takes multiple words, and
8114 even a sig8 reference is always eight bytes, whereas a local reference
8115 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8116 So if we encounter multiple external references to the same type DIE, we
8117 make a local typedef stub for it and redirect all references there.
8119 This is the element of the hash table for keeping track of these
8120 references. */
8122 struct external_ref
8124 dw_die_ref type;
8125 dw_die_ref stub;
8126 unsigned n_refs;
8129 /* Hashtable helpers. */
8131 struct external_ref_hasher : free_ptr_hash <external_ref>
8133 static inline hashval_t hash (const external_ref *);
8134 static inline bool equal (const external_ref *, const external_ref *);
8137 inline hashval_t
8138 external_ref_hasher::hash (const external_ref *r)
8140 dw_die_ref die = r->type;
8141 hashval_t h = 0;
8143 /* We can't use the address of the DIE for hashing, because
8144 that will make the order of the stub DIEs non-deterministic. */
8145 if (! die->comdat_type_p)
8146 /* We have a symbol; use it to compute a hash. */
8147 h = htab_hash_string (die->die_id.die_symbol);
8148 else
8150 /* We have a type signature; use a subset of the bits as the hash.
8151 The 8-byte signature is at least as large as hashval_t. */
8152 comdat_type_node *type_node = die->die_id.die_type_node;
8153 memcpy (&h, type_node->signature, sizeof (h));
8155 return h;
8158 inline bool
8159 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8161 return r1->type == r2->type;
8164 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8166 /* Return a pointer to the external_ref for references to DIE. */
8168 static struct external_ref *
8169 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8171 struct external_ref ref, *ref_p;
8172 external_ref **slot;
8174 ref.type = die;
8175 slot = map->find_slot (&ref, INSERT);
8176 if (*slot != HTAB_EMPTY_ENTRY)
8177 return *slot;
8179 ref_p = XCNEW (struct external_ref);
8180 ref_p->type = die;
8181 *slot = ref_p;
8182 return ref_p;
8185 /* Subroutine of optimize_external_refs, below.
8187 If we see a type skeleton, record it as our stub. If we see external
8188 references, remember how many we've seen. */
8190 static void
8191 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8193 dw_die_ref c;
8194 dw_attr_node *a;
8195 unsigned ix;
8196 struct external_ref *ref_p;
8198 if (is_type_die (die)
8199 && (c = get_AT_ref (die, DW_AT_signature)))
8201 /* This is a local skeleton; use it for local references. */
8202 ref_p = lookup_external_ref (map, c);
8203 ref_p->stub = die;
8206 /* Scan the DIE references, and remember any that refer to DIEs from
8207 other CUs (i.e. those which are not marked). */
8208 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8209 if (AT_class (a) == dw_val_class_die_ref
8210 && (c = AT_ref (a))->die_mark == 0
8211 && is_type_die (c))
8213 ref_p = lookup_external_ref (map, c);
8214 ref_p->n_refs++;
8217 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8220 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8221 points to an external_ref, DATA is the CU we're processing. If we don't
8222 already have a local stub, and we have multiple refs, build a stub. */
8225 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8227 struct external_ref *ref_p = *slot;
8229 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8231 /* We have multiple references to this type, so build a small stub.
8232 Both of these forms are a bit dodgy from the perspective of the
8233 DWARF standard, since technically they should have names. */
8234 dw_die_ref cu = data;
8235 dw_die_ref type = ref_p->type;
8236 dw_die_ref stub = NULL;
8238 if (type->comdat_type_p)
8240 /* If we refer to this type via sig8, use AT_signature. */
8241 stub = new_die (type->die_tag, cu, NULL_TREE);
8242 add_AT_die_ref (stub, DW_AT_signature, type);
8244 else
8246 /* Otherwise, use a typedef with no name. */
8247 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8248 add_AT_die_ref (stub, DW_AT_type, type);
8251 stub->die_mark++;
8252 ref_p->stub = stub;
8254 return 1;
8257 /* DIE is a unit; look through all the DIE references to see if there are
8258 any external references to types, and if so, create local stubs for
8259 them which will be applied in build_abbrev_table. This is useful because
8260 references to local DIEs are smaller. */
8262 static external_ref_hash_type *
8263 optimize_external_refs (dw_die_ref die)
8265 external_ref_hash_type *map = new external_ref_hash_type (10);
8266 optimize_external_refs_1 (die, map);
8267 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8268 return map;
8271 /* The format of each DIE (and its attribute value pairs) is encoded in an
8272 abbreviation table. This routine builds the abbreviation table and assigns
8273 a unique abbreviation id for each abbreviation entry. The children of each
8274 die are visited recursively. */
8276 static void
8277 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8279 unsigned long abbrev_id;
8280 unsigned int n_alloc;
8281 dw_die_ref c;
8282 dw_attr_node *a;
8283 unsigned ix;
8285 /* Scan the DIE references, and replace any that refer to
8286 DIEs from other CUs (i.e. those which are not marked) with
8287 the local stubs we built in optimize_external_refs. */
8288 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8289 if (AT_class (a) == dw_val_class_die_ref
8290 && (c = AT_ref (a))->die_mark == 0)
8292 struct external_ref *ref_p;
8293 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8295 ref_p = lookup_external_ref (extern_map, c);
8296 if (ref_p->stub && ref_p->stub != die)
8297 change_AT_die_ref (a, ref_p->stub);
8298 else
8299 /* We aren't changing this reference, so mark it external. */
8300 set_AT_ref_external (a, 1);
8303 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8305 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8306 dw_attr_node *die_a, *abbrev_a;
8307 unsigned ix;
8308 bool ok = true;
8310 if (abbrev->die_tag != die->die_tag)
8311 continue;
8312 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8313 continue;
8315 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8316 continue;
8318 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8320 abbrev_a = &(*abbrev->die_attr)[ix];
8321 if ((abbrev_a->dw_attr != die_a->dw_attr)
8322 || (value_format (abbrev_a) != value_format (die_a)))
8324 ok = false;
8325 break;
8328 if (ok)
8329 break;
8332 if (abbrev_id >= abbrev_die_table_in_use)
8334 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8336 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8337 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8338 n_alloc);
8340 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8341 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8342 abbrev_die_table_allocated = n_alloc;
8345 ++abbrev_die_table_in_use;
8346 abbrev_die_table[abbrev_id] = die;
8349 die->die_abbrev = abbrev_id;
8350 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8353 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8355 static int
8356 constant_size (unsigned HOST_WIDE_INT value)
8358 int log;
8360 if (value == 0)
8361 log = 0;
8362 else
8363 log = floor_log2 (value);
8365 log = log / 8;
8366 log = 1 << (floor_log2 (log) + 1);
8368 return log;
8371 /* Return the size of a DIE as it is represented in the
8372 .debug_info section. */
8374 static unsigned long
8375 size_of_die (dw_die_ref die)
8377 unsigned long size = 0;
8378 dw_attr_node *a;
8379 unsigned ix;
8380 enum dwarf_form form;
8382 size += size_of_uleb128 (die->die_abbrev);
8383 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8385 switch (AT_class (a))
8387 case dw_val_class_addr:
8388 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8390 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8391 size += size_of_uleb128 (AT_index (a));
8393 else
8394 size += DWARF2_ADDR_SIZE;
8395 break;
8396 case dw_val_class_offset:
8397 size += DWARF_OFFSET_SIZE;
8398 break;
8399 case dw_val_class_loc:
8401 unsigned long lsize = size_of_locs (AT_loc (a));
8403 /* Block length. */
8404 if (dwarf_version >= 4)
8405 size += size_of_uleb128 (lsize);
8406 else
8407 size += constant_size (lsize);
8408 size += lsize;
8410 break;
8411 case dw_val_class_loc_list:
8412 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8414 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8415 size += size_of_uleb128 (AT_index (a));
8417 else
8418 size += DWARF_OFFSET_SIZE;
8419 break;
8420 case dw_val_class_range_list:
8421 size += DWARF_OFFSET_SIZE;
8422 break;
8423 case dw_val_class_const:
8424 size += size_of_sleb128 (AT_int (a));
8425 break;
8426 case dw_val_class_unsigned_const:
8428 int csize = constant_size (AT_unsigned (a));
8429 if (dwarf_version == 3
8430 && a->dw_attr == DW_AT_data_member_location
8431 && csize >= 4)
8432 size += size_of_uleb128 (AT_unsigned (a));
8433 else
8434 size += csize;
8436 break;
8437 case dw_val_class_const_double:
8438 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8439 if (HOST_BITS_PER_WIDE_INT >= 64)
8440 size++; /* block */
8441 break;
8442 case dw_val_class_wide_int:
8443 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8444 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8445 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8446 > 64)
8447 size++; /* block */
8448 break;
8449 case dw_val_class_vec:
8450 size += constant_size (a->dw_attr_val.v.val_vec.length
8451 * a->dw_attr_val.v.val_vec.elt_size)
8452 + a->dw_attr_val.v.val_vec.length
8453 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8454 break;
8455 case dw_val_class_flag:
8456 if (dwarf_version >= 4)
8457 /* Currently all add_AT_flag calls pass in 1 as last argument,
8458 so DW_FORM_flag_present can be used. If that ever changes,
8459 we'll need to use DW_FORM_flag and have some optimization
8460 in build_abbrev_table that will change those to
8461 DW_FORM_flag_present if it is set to 1 in all DIEs using
8462 the same abbrev entry. */
8463 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8464 else
8465 size += 1;
8466 break;
8467 case dw_val_class_die_ref:
8468 if (AT_ref_external (a))
8470 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8471 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8472 is sized by target address length, whereas in DWARF3
8473 it's always sized as an offset. */
8474 if (use_debug_types)
8475 size += DWARF_TYPE_SIGNATURE_SIZE;
8476 else if (dwarf_version == 2)
8477 size += DWARF2_ADDR_SIZE;
8478 else
8479 size += DWARF_OFFSET_SIZE;
8481 else
8482 size += DWARF_OFFSET_SIZE;
8483 break;
8484 case dw_val_class_fde_ref:
8485 size += DWARF_OFFSET_SIZE;
8486 break;
8487 case dw_val_class_lbl_id:
8488 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8490 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8491 size += size_of_uleb128 (AT_index (a));
8493 else
8494 size += DWARF2_ADDR_SIZE;
8495 break;
8496 case dw_val_class_lineptr:
8497 case dw_val_class_macptr:
8498 size += DWARF_OFFSET_SIZE;
8499 break;
8500 case dw_val_class_str:
8501 form = AT_string_form (a);
8502 if (form == DW_FORM_strp)
8503 size += DWARF_OFFSET_SIZE;
8504 else if (form == DW_FORM_GNU_str_index)
8505 size += size_of_uleb128 (AT_index (a));
8506 else
8507 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8508 break;
8509 case dw_val_class_file:
8510 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8511 break;
8512 case dw_val_class_data8:
8513 size += 8;
8514 break;
8515 case dw_val_class_vms_delta:
8516 size += DWARF_OFFSET_SIZE;
8517 break;
8518 case dw_val_class_high_pc:
8519 size += DWARF2_ADDR_SIZE;
8520 break;
8521 case dw_val_class_discr_value:
8522 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8523 break;
8524 case dw_val_class_discr_list:
8526 unsigned block_size = size_of_discr_list (AT_discr_list (a));
8528 /* This is a block, so we have the block length and then its
8529 data. */
8530 size += constant_size (block_size) + block_size;
8532 break;
8533 default:
8534 gcc_unreachable ();
8538 return size;
8541 /* Size the debugging information associated with a given DIE. Visits the
8542 DIE's children recursively. Updates the global variable next_die_offset, on
8543 each time through. Uses the current value of next_die_offset to update the
8544 die_offset field in each DIE. */
8546 static void
8547 calc_die_sizes (dw_die_ref die)
8549 dw_die_ref c;
8551 gcc_assert (die->die_offset == 0
8552 || (unsigned long int) die->die_offset == next_die_offset);
8553 die->die_offset = next_die_offset;
8554 next_die_offset += size_of_die (die);
8556 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8558 if (die->die_child != NULL)
8559 /* Count the null byte used to terminate sibling lists. */
8560 next_die_offset += 1;
8563 /* Size just the base type children at the start of the CU.
8564 This is needed because build_abbrev needs to size locs
8565 and sizing of type based stack ops needs to know die_offset
8566 values for the base types. */
8568 static void
8569 calc_base_type_die_sizes (void)
8571 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8572 unsigned int i;
8573 dw_die_ref base_type;
8574 #if ENABLE_ASSERT_CHECKING
8575 dw_die_ref prev = comp_unit_die ()->die_child;
8576 #endif
8578 die_offset += size_of_die (comp_unit_die ());
8579 for (i = 0; base_types.iterate (i, &base_type); i++)
8581 #if ENABLE_ASSERT_CHECKING
8582 gcc_assert (base_type->die_offset == 0
8583 && prev->die_sib == base_type
8584 && base_type->die_child == NULL
8585 && base_type->die_abbrev);
8586 prev = base_type;
8587 #endif
8588 base_type->die_offset = die_offset;
8589 die_offset += size_of_die (base_type);
8593 /* Set the marks for a die and its children. We do this so
8594 that we know whether or not a reference needs to use FORM_ref_addr; only
8595 DIEs in the same CU will be marked. We used to clear out the offset
8596 and use that as the flag, but ran into ordering problems. */
8598 static void
8599 mark_dies (dw_die_ref die)
8601 dw_die_ref c;
8603 gcc_assert (!die->die_mark);
8605 die->die_mark = 1;
8606 FOR_EACH_CHILD (die, c, mark_dies (c));
8609 /* Clear the marks for a die and its children. */
8611 static void
8612 unmark_dies (dw_die_ref die)
8614 dw_die_ref c;
8616 if (! use_debug_types)
8617 gcc_assert (die->die_mark);
8619 die->die_mark = 0;
8620 FOR_EACH_CHILD (die, c, unmark_dies (c));
8623 /* Clear the marks for a die, its children and referred dies. */
8625 static void
8626 unmark_all_dies (dw_die_ref die)
8628 dw_die_ref c;
8629 dw_attr_node *a;
8630 unsigned ix;
8632 if (!die->die_mark)
8633 return;
8634 die->die_mark = 0;
8636 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8638 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8639 if (AT_class (a) == dw_val_class_die_ref)
8640 unmark_all_dies (AT_ref (a));
8643 /* Calculate if the entry should appear in the final output file. It may be
8644 from a pruned a type. */
8646 static bool
8647 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8649 /* By limiting gnu pubnames to definitions only, gold can generate a
8650 gdb index without entries for declarations, which don't include
8651 enough information to be useful. */
8652 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8653 return false;
8655 if (table == pubname_table)
8657 /* Enumerator names are part of the pubname table, but the
8658 parent DW_TAG_enumeration_type die may have been pruned.
8659 Don't output them if that is the case. */
8660 if (p->die->die_tag == DW_TAG_enumerator &&
8661 (p->die->die_parent == NULL
8662 || !p->die->die_parent->die_perennial_p))
8663 return false;
8665 /* Everything else in the pubname table is included. */
8666 return true;
8669 /* The pubtypes table shouldn't include types that have been
8670 pruned. */
8671 return (p->die->die_offset != 0
8672 || !flag_eliminate_unused_debug_types);
8675 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8676 generated for the compilation unit. */
8678 static unsigned long
8679 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8681 unsigned long size;
8682 unsigned i;
8683 pubname_entry *p;
8684 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8686 size = DWARF_PUBNAMES_HEADER_SIZE;
8687 FOR_EACH_VEC_ELT (*names, i, p)
8688 if (include_pubname_in_output (names, p))
8689 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8691 size += DWARF_OFFSET_SIZE;
8692 return size;
8695 /* Return the size of the information in the .debug_aranges section. */
8697 static unsigned long
8698 size_of_aranges (void)
8700 unsigned long size;
8702 size = DWARF_ARANGES_HEADER_SIZE;
8704 /* Count the address/length pair for this compilation unit. */
8705 if (text_section_used)
8706 size += 2 * DWARF2_ADDR_SIZE;
8707 if (cold_text_section_used)
8708 size += 2 * DWARF2_ADDR_SIZE;
8709 if (have_multiple_function_sections)
8711 unsigned fde_idx;
8712 dw_fde_ref fde;
8714 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8716 if (DECL_IGNORED_P (fde->decl))
8717 continue;
8718 if (!fde->in_std_section)
8719 size += 2 * DWARF2_ADDR_SIZE;
8720 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8721 size += 2 * DWARF2_ADDR_SIZE;
8725 /* Count the two zero words used to terminated the address range table. */
8726 size += 2 * DWARF2_ADDR_SIZE;
8727 return size;
8730 /* Select the encoding of an attribute value. */
8732 static enum dwarf_form
8733 value_format (dw_attr_node *a)
8735 switch (AT_class (a))
8737 case dw_val_class_addr:
8738 /* Only very few attributes allow DW_FORM_addr. */
8739 switch (a->dw_attr)
8741 case DW_AT_low_pc:
8742 case DW_AT_high_pc:
8743 case DW_AT_entry_pc:
8744 case DW_AT_trampoline:
8745 return (AT_index (a) == NOT_INDEXED
8746 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8747 default:
8748 break;
8750 switch (DWARF2_ADDR_SIZE)
8752 case 1:
8753 return DW_FORM_data1;
8754 case 2:
8755 return DW_FORM_data2;
8756 case 4:
8757 return DW_FORM_data4;
8758 case 8:
8759 return DW_FORM_data8;
8760 default:
8761 gcc_unreachable ();
8763 case dw_val_class_range_list:
8764 case dw_val_class_loc_list:
8765 if (dwarf_version >= 4)
8766 return DW_FORM_sec_offset;
8767 /* FALLTHRU */
8768 case dw_val_class_vms_delta:
8769 case dw_val_class_offset:
8770 switch (DWARF_OFFSET_SIZE)
8772 case 4:
8773 return DW_FORM_data4;
8774 case 8:
8775 return DW_FORM_data8;
8776 default:
8777 gcc_unreachable ();
8779 case dw_val_class_loc:
8780 if (dwarf_version >= 4)
8781 return DW_FORM_exprloc;
8782 switch (constant_size (size_of_locs (AT_loc (a))))
8784 case 1:
8785 return DW_FORM_block1;
8786 case 2:
8787 return DW_FORM_block2;
8788 case 4:
8789 return DW_FORM_block4;
8790 default:
8791 gcc_unreachable ();
8793 case dw_val_class_const:
8794 return DW_FORM_sdata;
8795 case dw_val_class_unsigned_const:
8796 switch (constant_size (AT_unsigned (a)))
8798 case 1:
8799 return DW_FORM_data1;
8800 case 2:
8801 return DW_FORM_data2;
8802 case 4:
8803 /* In DWARF3 DW_AT_data_member_location with
8804 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8805 constant, so we need to use DW_FORM_udata if we need
8806 a large constant. */
8807 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8808 return DW_FORM_udata;
8809 return DW_FORM_data4;
8810 case 8:
8811 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8812 return DW_FORM_udata;
8813 return DW_FORM_data8;
8814 default:
8815 gcc_unreachable ();
8817 case dw_val_class_const_double:
8818 switch (HOST_BITS_PER_WIDE_INT)
8820 case 8:
8821 return DW_FORM_data2;
8822 case 16:
8823 return DW_FORM_data4;
8824 case 32:
8825 return DW_FORM_data8;
8826 case 64:
8827 default:
8828 return DW_FORM_block1;
8830 case dw_val_class_wide_int:
8831 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8833 case 8:
8834 return DW_FORM_data1;
8835 case 16:
8836 return DW_FORM_data2;
8837 case 32:
8838 return DW_FORM_data4;
8839 case 64:
8840 return DW_FORM_data8;
8841 default:
8842 return DW_FORM_block1;
8844 case dw_val_class_vec:
8845 switch (constant_size (a->dw_attr_val.v.val_vec.length
8846 * a->dw_attr_val.v.val_vec.elt_size))
8848 case 1:
8849 return DW_FORM_block1;
8850 case 2:
8851 return DW_FORM_block2;
8852 case 4:
8853 return DW_FORM_block4;
8854 default:
8855 gcc_unreachable ();
8857 case dw_val_class_flag:
8858 if (dwarf_version >= 4)
8860 /* Currently all add_AT_flag calls pass in 1 as last argument,
8861 so DW_FORM_flag_present can be used. If that ever changes,
8862 we'll need to use DW_FORM_flag and have some optimization
8863 in build_abbrev_table that will change those to
8864 DW_FORM_flag_present if it is set to 1 in all DIEs using
8865 the same abbrev entry. */
8866 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8867 return DW_FORM_flag_present;
8869 return DW_FORM_flag;
8870 case dw_val_class_die_ref:
8871 if (AT_ref_external (a))
8872 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8873 else
8874 return DW_FORM_ref;
8875 case dw_val_class_fde_ref:
8876 return DW_FORM_data;
8877 case dw_val_class_lbl_id:
8878 return (AT_index (a) == NOT_INDEXED
8879 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8880 case dw_val_class_lineptr:
8881 case dw_val_class_macptr:
8882 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8883 case dw_val_class_str:
8884 return AT_string_form (a);
8885 case dw_val_class_file:
8886 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8888 case 1:
8889 return DW_FORM_data1;
8890 case 2:
8891 return DW_FORM_data2;
8892 case 4:
8893 return DW_FORM_data4;
8894 default:
8895 gcc_unreachable ();
8898 case dw_val_class_data8:
8899 return DW_FORM_data8;
8901 case dw_val_class_high_pc:
8902 switch (DWARF2_ADDR_SIZE)
8904 case 1:
8905 return DW_FORM_data1;
8906 case 2:
8907 return DW_FORM_data2;
8908 case 4:
8909 return DW_FORM_data4;
8910 case 8:
8911 return DW_FORM_data8;
8912 default:
8913 gcc_unreachable ();
8916 case dw_val_class_discr_value:
8917 return (a->dw_attr_val.v.val_discr_value.pos
8918 ? DW_FORM_udata
8919 : DW_FORM_sdata);
8920 case dw_val_class_discr_list:
8921 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8923 case 1:
8924 return DW_FORM_block1;
8925 case 2:
8926 return DW_FORM_block2;
8927 case 4:
8928 return DW_FORM_block4;
8929 default:
8930 gcc_unreachable ();
8933 default:
8934 gcc_unreachable ();
8938 /* Output the encoding of an attribute value. */
8940 static void
8941 output_value_format (dw_attr_node *a)
8943 enum dwarf_form form = value_format (a);
8945 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8948 /* Given a die and id, produce the appropriate abbreviations. */
8950 static void
8951 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8953 unsigned ix;
8954 dw_attr_node *a_attr;
8956 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8957 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8958 dwarf_tag_name (abbrev->die_tag));
8960 if (abbrev->die_child != NULL)
8961 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8962 else
8963 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8965 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8967 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8968 dwarf_attr_name (a_attr->dw_attr));
8969 output_value_format (a_attr);
8972 dw2_asm_output_data (1, 0, NULL);
8973 dw2_asm_output_data (1, 0, NULL);
8977 /* Output the .debug_abbrev section which defines the DIE abbreviation
8978 table. */
8980 static void
8981 output_abbrev_section (void)
8983 unsigned long abbrev_id;
8985 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8986 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8988 /* Terminate the table. */
8989 dw2_asm_output_data (1, 0, NULL);
8992 /* Output a symbol we can use to refer to this DIE from another CU. */
8994 static inline void
8995 output_die_symbol (dw_die_ref die)
8997 const char *sym = die->die_id.die_symbol;
8999 gcc_assert (!die->comdat_type_p);
9001 if (sym == 0)
9002 return;
9004 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9005 /* We make these global, not weak; if the target doesn't support
9006 .linkonce, it doesn't support combining the sections, so debugging
9007 will break. */
9008 targetm.asm_out.globalize_label (asm_out_file, sym);
9010 ASM_OUTPUT_LABEL (asm_out_file, sym);
9013 /* Return a new location list, given the begin and end range, and the
9014 expression. */
9016 static inline dw_loc_list_ref
9017 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9018 const char *section)
9020 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9022 retlist->begin = begin;
9023 retlist->begin_entry = NULL;
9024 retlist->end = end;
9025 retlist->expr = expr;
9026 retlist->section = section;
9028 return retlist;
9031 /* Generate a new internal symbol for this location list node, if it
9032 hasn't got one yet. */
9034 static inline void
9035 gen_llsym (dw_loc_list_ref list)
9037 gcc_assert (!list->ll_symbol);
9038 list->ll_symbol = gen_internal_sym ("LLST");
9041 /* Output the location list given to us. */
9043 static void
9044 output_loc_list (dw_loc_list_ref list_head)
9046 dw_loc_list_ref curr = list_head;
9048 if (list_head->emitted)
9049 return;
9050 list_head->emitted = true;
9052 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9054 /* Walk the location list, and output each range + expression. */
9055 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9057 unsigned long size;
9058 /* Don't output an entry that starts and ends at the same address. */
9059 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9060 continue;
9061 size = size_of_locs (curr->expr);
9062 /* If the expression is too large, drop it on the floor. We could
9063 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9064 in the expression, but >= 64KB expressions for a single value
9065 in a single range are unlikely very useful. */
9066 if (size > 0xffff)
9067 continue;
9068 if (dwarf_split_debug_info)
9070 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9071 "Location list start/length entry (%s)",
9072 list_head->ll_symbol);
9073 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9074 "Location list range start index (%s)",
9075 curr->begin);
9076 /* The length field is 4 bytes. If we ever need to support
9077 an 8-byte length, we can add a new DW_LLE code or fall back
9078 to DW_LLE_GNU_start_end_entry. */
9079 dw2_asm_output_delta (4, curr->end, curr->begin,
9080 "Location list range length (%s)",
9081 list_head->ll_symbol);
9083 else if (!have_multiple_function_sections)
9085 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9086 "Location list begin address (%s)",
9087 list_head->ll_symbol);
9088 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9089 "Location list end address (%s)",
9090 list_head->ll_symbol);
9092 else
9094 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9095 "Location list begin address (%s)",
9096 list_head->ll_symbol);
9097 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9098 "Location list end address (%s)",
9099 list_head->ll_symbol);
9102 /* Output the block length for this list of location operations. */
9103 gcc_assert (size <= 0xffff);
9104 dw2_asm_output_data (2, size, "%s", "Location expression size");
9106 output_loc_sequence (curr->expr, -1);
9109 if (dwarf_split_debug_info)
9110 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9111 "Location list terminator (%s)",
9112 list_head->ll_symbol);
9113 else
9115 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9116 "Location list terminator begin (%s)",
9117 list_head->ll_symbol);
9118 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9119 "Location list terminator end (%s)",
9120 list_head->ll_symbol);
9124 /* Output a range_list offset into the debug_range section. Emit a
9125 relocated reference if val_entry is NULL, otherwise, emit an
9126 indirect reference. */
9128 static void
9129 output_range_list_offset (dw_attr_node *a)
9131 const char *name = dwarf_attr_name (a->dw_attr);
9133 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9135 char *p = strchr (ranges_section_label, '\0');
9136 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9137 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9138 debug_ranges_section, "%s", name);
9139 *p = '\0';
9141 else
9142 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9143 "%s (offset from %s)", name, ranges_section_label);
9146 /* Output the offset into the debug_loc section. */
9148 static void
9149 output_loc_list_offset (dw_attr_node *a)
9151 char *sym = AT_loc_list (a)->ll_symbol;
9153 gcc_assert (sym);
9154 if (dwarf_split_debug_info)
9155 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9156 "%s", dwarf_attr_name (a->dw_attr));
9157 else
9158 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9159 "%s", dwarf_attr_name (a->dw_attr));
9162 /* Output an attribute's index or value appropriately. */
9164 static void
9165 output_attr_index_or_value (dw_attr_node *a)
9167 const char *name = dwarf_attr_name (a->dw_attr);
9169 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9171 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9172 return;
9174 switch (AT_class (a))
9176 case dw_val_class_addr:
9177 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9178 break;
9179 case dw_val_class_high_pc:
9180 case dw_val_class_lbl_id:
9181 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9182 break;
9183 case dw_val_class_loc_list:
9184 output_loc_list_offset (a);
9185 break;
9186 default:
9187 gcc_unreachable ();
9191 /* Output a type signature. */
9193 static inline void
9194 output_signature (const char *sig, const char *name)
9196 int i;
9198 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9199 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9202 /* Output a discriminant value. */
9204 static inline void
9205 output_discr_value (dw_discr_value *discr_value, const char *name)
9207 if (discr_value->pos)
9208 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9209 else
9210 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9213 /* Output the DIE and its attributes. Called recursively to generate
9214 the definitions of each child DIE. */
9216 static void
9217 output_die (dw_die_ref die)
9219 dw_attr_node *a;
9220 dw_die_ref c;
9221 unsigned long size;
9222 unsigned ix;
9224 /* If someone in another CU might refer to us, set up a symbol for
9225 them to point to. */
9226 if (! die->comdat_type_p && die->die_id.die_symbol)
9227 output_die_symbol (die);
9229 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9230 (unsigned long)die->die_offset,
9231 dwarf_tag_name (die->die_tag));
9233 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9235 const char *name = dwarf_attr_name (a->dw_attr);
9237 switch (AT_class (a))
9239 case dw_val_class_addr:
9240 output_attr_index_or_value (a);
9241 break;
9243 case dw_val_class_offset:
9244 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9245 "%s", name);
9246 break;
9248 case dw_val_class_range_list:
9249 output_range_list_offset (a);
9250 break;
9252 case dw_val_class_loc:
9253 size = size_of_locs (AT_loc (a));
9255 /* Output the block length for this list of location operations. */
9256 if (dwarf_version >= 4)
9257 dw2_asm_output_data_uleb128 (size, "%s", name);
9258 else
9259 dw2_asm_output_data (constant_size (size), size, "%s", name);
9261 output_loc_sequence (AT_loc (a), -1);
9262 break;
9264 case dw_val_class_const:
9265 /* ??? It would be slightly more efficient to use a scheme like is
9266 used for unsigned constants below, but gdb 4.x does not sign
9267 extend. Gdb 5.x does sign extend. */
9268 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9269 break;
9271 case dw_val_class_unsigned_const:
9273 int csize = constant_size (AT_unsigned (a));
9274 if (dwarf_version == 3
9275 && a->dw_attr == DW_AT_data_member_location
9276 && csize >= 4)
9277 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9278 else
9279 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9281 break;
9283 case dw_val_class_const_double:
9285 unsigned HOST_WIDE_INT first, second;
9287 if (HOST_BITS_PER_WIDE_INT >= 64)
9288 dw2_asm_output_data (1,
9289 HOST_BITS_PER_DOUBLE_INT
9290 / HOST_BITS_PER_CHAR,
9291 NULL);
9293 if (WORDS_BIG_ENDIAN)
9295 first = a->dw_attr_val.v.val_double.high;
9296 second = a->dw_attr_val.v.val_double.low;
9298 else
9300 first = a->dw_attr_val.v.val_double.low;
9301 second = a->dw_attr_val.v.val_double.high;
9304 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9305 first, "%s", name);
9306 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9307 second, NULL);
9309 break;
9311 case dw_val_class_wide_int:
9313 int i;
9314 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9315 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9316 if (len * HOST_BITS_PER_WIDE_INT > 64)
9317 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9318 NULL);
9320 if (WORDS_BIG_ENDIAN)
9321 for (i = len - 1; i >= 0; --i)
9323 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9324 "%s", name);
9325 name = "";
9327 else
9328 for (i = 0; i < len; ++i)
9330 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9331 "%s", name);
9332 name = "";
9335 break;
9337 case dw_val_class_vec:
9339 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9340 unsigned int len = a->dw_attr_val.v.val_vec.length;
9341 unsigned int i;
9342 unsigned char *p;
9344 dw2_asm_output_data (constant_size (len * elt_size),
9345 len * elt_size, "%s", name);
9346 if (elt_size > sizeof (HOST_WIDE_INT))
9348 elt_size /= 2;
9349 len *= 2;
9351 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9352 i < len;
9353 i++, p += elt_size)
9354 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9355 "fp or vector constant word %u", i);
9356 break;
9359 case dw_val_class_flag:
9360 if (dwarf_version >= 4)
9362 /* Currently all add_AT_flag calls pass in 1 as last argument,
9363 so DW_FORM_flag_present can be used. If that ever changes,
9364 we'll need to use DW_FORM_flag and have some optimization
9365 in build_abbrev_table that will change those to
9366 DW_FORM_flag_present if it is set to 1 in all DIEs using
9367 the same abbrev entry. */
9368 gcc_assert (AT_flag (a) == 1);
9369 if (flag_debug_asm)
9370 fprintf (asm_out_file, "\t\t\t%s %s\n",
9371 ASM_COMMENT_START, name);
9372 break;
9374 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9375 break;
9377 case dw_val_class_loc_list:
9378 output_attr_index_or_value (a);
9379 break;
9381 case dw_val_class_die_ref:
9382 if (AT_ref_external (a))
9384 if (AT_ref (a)->comdat_type_p)
9386 comdat_type_node *type_node =
9387 AT_ref (a)->die_id.die_type_node;
9389 gcc_assert (type_node);
9390 output_signature (type_node->signature, name);
9392 else
9394 const char *sym = AT_ref (a)->die_id.die_symbol;
9395 int size;
9397 gcc_assert (sym);
9398 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9399 length, whereas in DWARF3 it's always sized as an
9400 offset. */
9401 if (dwarf_version == 2)
9402 size = DWARF2_ADDR_SIZE;
9403 else
9404 size = DWARF_OFFSET_SIZE;
9405 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9406 name);
9409 else
9411 gcc_assert (AT_ref (a)->die_offset);
9412 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9413 "%s", name);
9415 break;
9417 case dw_val_class_fde_ref:
9419 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
9421 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9422 a->dw_attr_val.v.val_fde_index * 2);
9423 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9424 "%s", name);
9426 break;
9428 case dw_val_class_vms_delta:
9429 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9430 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9431 AT_vms_delta2 (a), AT_vms_delta1 (a),
9432 "%s", name);
9433 #else
9434 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9435 AT_vms_delta2 (a), AT_vms_delta1 (a),
9436 "%s", name);
9437 #endif
9438 break;
9440 case dw_val_class_lbl_id:
9441 output_attr_index_or_value (a);
9442 break;
9444 case dw_val_class_lineptr:
9445 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9446 debug_line_section, "%s", name);
9447 break;
9449 case dw_val_class_macptr:
9450 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9451 debug_macinfo_section, "%s", name);
9452 break;
9454 case dw_val_class_str:
9455 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9456 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9457 a->dw_attr_val.v.val_str->label,
9458 debug_str_section,
9459 "%s: \"%s\"", name, AT_string (a));
9460 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9461 dw2_asm_output_data_uleb128 (AT_index (a),
9462 "%s: \"%s\"", name, AT_string (a));
9463 else
9464 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9465 break;
9467 case dw_val_class_file:
9469 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9471 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9472 a->dw_attr_val.v.val_file->filename);
9473 break;
9476 case dw_val_class_data8:
9478 int i;
9480 for (i = 0; i < 8; i++)
9481 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9482 i == 0 ? "%s" : NULL, name);
9483 break;
9486 case dw_val_class_high_pc:
9487 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9488 get_AT_low_pc (die), "DW_AT_high_pc");
9489 break;
9491 case dw_val_class_discr_value:
9492 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9493 break;
9495 case dw_val_class_discr_list:
9497 dw_discr_list_ref list = AT_discr_list (a);
9498 const int size = size_of_discr_list (list);
9500 /* This is a block, so output its length first. */
9501 dw2_asm_output_data (constant_size (size), size,
9502 "%s: block size", name);
9504 for (; list != NULL; list = list->dw_discr_next)
9506 /* One byte for the discriminant value descriptor, and then as
9507 many LEB128 numbers as required. */
9508 if (list->dw_discr_range)
9509 dw2_asm_output_data (1, DW_DSC_range,
9510 "%s: DW_DSC_range", name);
9511 else
9512 dw2_asm_output_data (1, DW_DSC_label,
9513 "%s: DW_DSC_label", name);
9515 output_discr_value (&list->dw_discr_lower_bound, name);
9516 if (list->dw_discr_range)
9517 output_discr_value (&list->dw_discr_upper_bound, name);
9519 break;
9522 default:
9523 gcc_unreachable ();
9527 FOR_EACH_CHILD (die, c, output_die (c));
9529 /* Add null byte to terminate sibling list. */
9530 if (die->die_child != NULL)
9531 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9532 (unsigned long) die->die_offset);
9535 /* Output the compilation unit that appears at the beginning of the
9536 .debug_info section, and precedes the DIE descriptions. */
9538 static void
9539 output_compilation_unit_header (void)
9541 /* We don't support actual DWARFv5 units yet, we just use some
9542 DWARFv5 draft DIE tags in DWARFv4 format. */
9543 int ver = dwarf_version < 5 ? dwarf_version : 4;
9545 if (!XCOFF_DEBUGGING_INFO)
9547 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9548 dw2_asm_output_data (4, 0xffffffff,
9549 "Initial length escape value indicating 64-bit DWARF extension");
9550 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9551 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9552 "Length of Compilation Unit Info");
9555 dw2_asm_output_data (2, ver, "DWARF version number");
9556 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9557 debug_abbrev_section,
9558 "Offset Into Abbrev. Section");
9559 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9562 /* Output the compilation unit DIE and its children. */
9564 static void
9565 output_comp_unit (dw_die_ref die, int output_if_empty)
9567 const char *secname, *oldsym;
9568 char *tmp;
9570 /* Unless we are outputting main CU, we may throw away empty ones. */
9571 if (!output_if_empty && die->die_child == NULL)
9572 return;
9574 /* Even if there are no children of this DIE, we must output the information
9575 about the compilation unit. Otherwise, on an empty translation unit, we
9576 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9577 will then complain when examining the file. First mark all the DIEs in
9578 this CU so we know which get local refs. */
9579 mark_dies (die);
9581 external_ref_hash_type *extern_map = optimize_external_refs (die);
9583 build_abbrev_table (die, extern_map);
9585 delete extern_map;
9587 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9588 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9589 calc_die_sizes (die);
9591 oldsym = die->die_id.die_symbol;
9592 if (oldsym)
9594 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9596 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9597 secname = tmp;
9598 die->die_id.die_symbol = NULL;
9599 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9601 else
9603 switch_to_section (debug_info_section);
9604 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9605 info_section_emitted = true;
9608 /* Output debugging information. */
9609 output_compilation_unit_header ();
9610 output_die (die);
9612 /* Leave the marks on the main CU, so we can check them in
9613 output_pubnames. */
9614 if (oldsym)
9616 unmark_dies (die);
9617 die->die_id.die_symbol = oldsym;
9621 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9622 and .debug_pubtypes. This is configured per-target, but can be
9623 overridden by the -gpubnames or -gno-pubnames options. */
9625 static inline bool
9626 want_pubnames (void)
9628 if (debug_info_level <= DINFO_LEVEL_TERSE)
9629 return false;
9630 if (debug_generate_pub_sections != -1)
9631 return debug_generate_pub_sections;
9632 return targetm.want_debug_pub_sections;
9635 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9637 static void
9638 add_AT_pubnames (dw_die_ref die)
9640 if (want_pubnames ())
9641 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9644 /* Add a string attribute value to a skeleton DIE. */
9646 static inline void
9647 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9648 const char *str)
9650 dw_attr_node attr;
9651 struct indirect_string_node *node;
9653 if (! skeleton_debug_str_hash)
9654 skeleton_debug_str_hash
9655 = hash_table<indirect_string_hasher>::create_ggc (10);
9657 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9658 find_string_form (node);
9659 if (node->form == DW_FORM_GNU_str_index)
9660 node->form = DW_FORM_strp;
9662 attr.dw_attr = attr_kind;
9663 attr.dw_attr_val.val_class = dw_val_class_str;
9664 attr.dw_attr_val.val_entry = NULL;
9665 attr.dw_attr_val.v.val_str = node;
9666 add_dwarf_attr (die, &attr);
9669 /* Helper function to generate top-level dies for skeleton debug_info and
9670 debug_types. */
9672 static void
9673 add_top_level_skeleton_die_attrs (dw_die_ref die)
9675 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9676 const char *comp_dir = comp_dir_string ();
9678 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9679 if (comp_dir != NULL)
9680 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9681 add_AT_pubnames (die);
9682 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9685 /* Output skeleton debug sections that point to the dwo file. */
9687 static void
9688 output_skeleton_debug_sections (dw_die_ref comp_unit)
9690 /* We don't support actual DWARFv5 units yet, we just use some
9691 DWARFv5 draft DIE tags in DWARFv4 format. */
9692 int ver = dwarf_version < 5 ? dwarf_version : 4;
9694 /* These attributes will be found in the full debug_info section. */
9695 remove_AT (comp_unit, DW_AT_producer);
9696 remove_AT (comp_unit, DW_AT_language);
9698 switch_to_section (debug_skeleton_info_section);
9699 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9701 /* Produce the skeleton compilation-unit header. This one differs enough from
9702 a normal CU header that it's better not to call output_compilation_unit
9703 header. */
9704 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9705 dw2_asm_output_data (4, 0xffffffff,
9706 "Initial length escape value indicating 64-bit DWARF extension");
9708 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9709 DWARF_COMPILE_UNIT_HEADER_SIZE
9710 - DWARF_INITIAL_LENGTH_SIZE
9711 + size_of_die (comp_unit),
9712 "Length of Compilation Unit Info");
9713 dw2_asm_output_data (2, ver, "DWARF version number");
9714 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9715 debug_abbrev_section,
9716 "Offset Into Abbrev. Section");
9717 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9719 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9720 output_die (comp_unit);
9722 /* Build the skeleton debug_abbrev section. */
9723 switch_to_section (debug_skeleton_abbrev_section);
9724 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9726 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9728 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9731 /* Output a comdat type unit DIE and its children. */
9733 static void
9734 output_comdat_type_unit (comdat_type_node *node)
9736 const char *secname;
9737 char *tmp;
9738 int i;
9739 #if defined (OBJECT_FORMAT_ELF)
9740 tree comdat_key;
9741 #endif
9743 /* First mark all the DIEs in this CU so we know which get local refs. */
9744 mark_dies (node->root_die);
9746 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9748 build_abbrev_table (node->root_die, extern_map);
9750 delete extern_map;
9751 extern_map = NULL;
9753 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9754 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9755 calc_die_sizes (node->root_die);
9757 #if defined (OBJECT_FORMAT_ELF)
9758 if (!dwarf_split_debug_info)
9759 secname = ".debug_types";
9760 else
9761 secname = ".debug_types.dwo";
9763 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9764 sprintf (tmp, "wt.");
9765 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9766 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9767 comdat_key = get_identifier (tmp);
9768 targetm.asm_out.named_section (secname,
9769 SECTION_DEBUG | SECTION_LINKONCE,
9770 comdat_key);
9771 #else
9772 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9773 sprintf (tmp, ".gnu.linkonce.wt.");
9774 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9775 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9776 secname = tmp;
9777 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9778 #endif
9780 /* Output debugging information. */
9781 output_compilation_unit_header ();
9782 output_signature (node->signature, "Type Signature");
9783 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9784 "Offset to Type DIE");
9785 output_die (node->root_die);
9787 unmark_dies (node->root_die);
9790 /* Return the DWARF2/3 pubname associated with a decl. */
9792 static const char *
9793 dwarf2_name (tree decl, int scope)
9795 if (DECL_NAMELESS (decl))
9796 return NULL;
9797 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9800 /* Add a new entry to .debug_pubnames if appropriate. */
9802 static void
9803 add_pubname_string (const char *str, dw_die_ref die)
9805 pubname_entry e;
9807 e.die = die;
9808 e.name = xstrdup (str);
9809 vec_safe_push (pubname_table, e);
9812 static void
9813 add_pubname (tree decl, dw_die_ref die)
9815 if (!want_pubnames ())
9816 return;
9818 /* Don't add items to the table when we expect that the consumer will have
9819 just read the enclosing die. For example, if the consumer is looking at a
9820 class_member, it will either be inside the class already, or will have just
9821 looked up the class to find the member. Either way, searching the class is
9822 faster than searching the index. */
9823 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9824 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9826 const char *name = dwarf2_name (decl, 1);
9828 if (name)
9829 add_pubname_string (name, die);
9833 /* Add an enumerator to the pubnames section. */
9835 static void
9836 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9838 pubname_entry e;
9840 gcc_assert (scope_name);
9841 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9842 e.die = die;
9843 vec_safe_push (pubname_table, e);
9846 /* Add a new entry to .debug_pubtypes if appropriate. */
9848 static void
9849 add_pubtype (tree decl, dw_die_ref die)
9851 pubname_entry e;
9853 if (!want_pubnames ())
9854 return;
9856 if ((TREE_PUBLIC (decl)
9857 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9858 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9860 tree scope = NULL;
9861 const char *scope_name = "";
9862 const char *sep = is_cxx () ? "::" : ".";
9863 const char *name;
9865 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9866 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9868 scope_name = lang_hooks.dwarf_name (scope, 1);
9869 if (scope_name != NULL && scope_name[0] != '\0')
9870 scope_name = concat (scope_name, sep, NULL);
9871 else
9872 scope_name = "";
9875 if (TYPE_P (decl))
9876 name = type_tag (decl);
9877 else
9878 name = lang_hooks.dwarf_name (decl, 1);
9880 /* If we don't have a name for the type, there's no point in adding
9881 it to the table. */
9882 if (name != NULL && name[0] != '\0')
9884 e.die = die;
9885 e.name = concat (scope_name, name, NULL);
9886 vec_safe_push (pubtype_table, e);
9889 /* Although it might be more consistent to add the pubinfo for the
9890 enumerators as their dies are created, they should only be added if the
9891 enum type meets the criteria above. So rather than re-check the parent
9892 enum type whenever an enumerator die is created, just output them all
9893 here. This isn't protected by the name conditional because anonymous
9894 enums don't have names. */
9895 if (die->die_tag == DW_TAG_enumeration_type)
9897 dw_die_ref c;
9899 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9904 /* Output a single entry in the pubnames table. */
9906 static void
9907 output_pubname (dw_offset die_offset, pubname_entry *entry)
9909 dw_die_ref die = entry->die;
9910 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9912 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9914 if (debug_generate_pub_sections == 2)
9916 /* This logic follows gdb's method for determining the value of the flag
9917 byte. */
9918 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9919 switch (die->die_tag)
9921 case DW_TAG_typedef:
9922 case DW_TAG_base_type:
9923 case DW_TAG_subrange_type:
9924 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9925 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9926 break;
9927 case DW_TAG_enumerator:
9928 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9929 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9930 if (!is_cxx () && !is_java ())
9931 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9932 break;
9933 case DW_TAG_subprogram:
9934 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9935 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9936 if (!is_ada ())
9937 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9938 break;
9939 case DW_TAG_constant:
9940 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9941 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9942 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9943 break;
9944 case DW_TAG_variable:
9945 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9946 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9947 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9948 break;
9949 case DW_TAG_namespace:
9950 case DW_TAG_imported_declaration:
9951 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9952 break;
9953 case DW_TAG_class_type:
9954 case DW_TAG_interface_type:
9955 case DW_TAG_structure_type:
9956 case DW_TAG_union_type:
9957 case DW_TAG_enumeration_type:
9958 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9959 if (!is_cxx () && !is_java ())
9960 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9961 break;
9962 default:
9963 /* An unusual tag. Leave the flag-byte empty. */
9964 break;
9966 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9967 "GDB-index flags");
9970 dw2_asm_output_nstring (entry->name, -1, "external name");
9974 /* Output the public names table used to speed up access to externally
9975 visible names; or the public types table used to find type definitions. */
9977 static void
9978 output_pubnames (vec<pubname_entry, va_gc> *names)
9980 unsigned i;
9981 unsigned long pubnames_length = size_of_pubnames (names);
9982 pubname_entry *pub;
9984 if (!XCOFF_DEBUGGING_INFO)
9986 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9987 dw2_asm_output_data (4, 0xffffffff,
9988 "Initial length escape value indicating 64-bit DWARF extension");
9989 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9990 "Pub Info Length");
9993 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9994 dw2_asm_output_data (2, 2, "DWARF Version");
9996 if (dwarf_split_debug_info)
9997 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9998 debug_skeleton_info_section,
9999 "Offset of Compilation Unit Info");
10000 else
10001 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10002 debug_info_section,
10003 "Offset of Compilation Unit Info");
10004 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10005 "Compilation Unit Length");
10007 FOR_EACH_VEC_ELT (*names, i, pub)
10009 if (include_pubname_in_output (names, pub))
10011 dw_offset die_offset = pub->die->die_offset;
10013 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10014 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10015 gcc_assert (pub->die->die_mark);
10017 /* If we're putting types in their own .debug_types sections,
10018 the .debug_pubtypes table will still point to the compile
10019 unit (not the type unit), so we want to use the offset of
10020 the skeleton DIE (if there is one). */
10021 if (pub->die->comdat_type_p && names == pubtype_table)
10023 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10025 if (type_node != NULL)
10026 die_offset = (type_node->skeleton_die != NULL
10027 ? type_node->skeleton_die->die_offset
10028 : comp_unit_die ()->die_offset);
10031 output_pubname (die_offset, pub);
10035 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10038 /* Output public names and types tables if necessary. */
10040 static void
10041 output_pubtables (void)
10043 if (!want_pubnames () || !info_section_emitted)
10044 return;
10046 switch_to_section (debug_pubnames_section);
10047 output_pubnames (pubname_table);
10048 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10049 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10050 simply won't look for the section. */
10051 switch_to_section (debug_pubtypes_section);
10052 output_pubnames (pubtype_table);
10056 /* Output the information that goes into the .debug_aranges table.
10057 Namely, define the beginning and ending address range of the
10058 text section generated for this compilation unit. */
10060 static void
10061 output_aranges (void)
10063 unsigned i;
10064 unsigned long aranges_length = size_of_aranges ();
10066 if (!XCOFF_DEBUGGING_INFO)
10068 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10069 dw2_asm_output_data (4, 0xffffffff,
10070 "Initial length escape value indicating 64-bit DWARF extension");
10071 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10072 "Length of Address Ranges Info");
10075 /* Version number for aranges is still 2, even up to DWARF5. */
10076 dw2_asm_output_data (2, 2, "DWARF Version");
10077 if (dwarf_split_debug_info)
10078 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10079 debug_skeleton_info_section,
10080 "Offset of Compilation Unit Info");
10081 else
10082 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10083 debug_info_section,
10084 "Offset of Compilation Unit Info");
10085 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10086 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10088 /* We need to align to twice the pointer size here. */
10089 if (DWARF_ARANGES_PAD_SIZE)
10091 /* Pad using a 2 byte words so that padding is correct for any
10092 pointer size. */
10093 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10094 2 * DWARF2_ADDR_SIZE);
10095 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10096 dw2_asm_output_data (2, 0, NULL);
10099 /* It is necessary not to output these entries if the sections were
10100 not used; if the sections were not used, the length will be 0 and
10101 the address may end up as 0 if the section is discarded by ld
10102 --gc-sections, leaving an invalid (0, 0) entry that can be
10103 confused with the terminator. */
10104 if (text_section_used)
10106 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10107 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10108 text_section_label, "Length");
10110 if (cold_text_section_used)
10112 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10113 "Address");
10114 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10115 cold_text_section_label, "Length");
10118 if (have_multiple_function_sections)
10120 unsigned fde_idx;
10121 dw_fde_ref fde;
10123 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10125 if (DECL_IGNORED_P (fde->decl))
10126 continue;
10127 if (!fde->in_std_section)
10129 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10130 "Address");
10131 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10132 fde->dw_fde_begin, "Length");
10134 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10136 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10137 "Address");
10138 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10139 fde->dw_fde_second_begin, "Length");
10144 /* Output the terminator words. */
10145 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10146 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10149 /* Add a new entry to .debug_ranges. Return the offset at which it
10150 was placed. */
10152 static unsigned int
10153 add_ranges_num (int num)
10155 unsigned int in_use = ranges_table_in_use;
10157 if (in_use == ranges_table_allocated)
10159 ranges_table_allocated += RANGES_TABLE_INCREMENT;
10160 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10161 ranges_table_allocated);
10162 memset (ranges_table + ranges_table_in_use, 0,
10163 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10166 ranges_table[in_use].num = num;
10167 ranges_table_in_use = in_use + 1;
10169 return in_use * 2 * DWARF2_ADDR_SIZE;
10172 /* Add a new entry to .debug_ranges corresponding to a block, or a
10173 range terminator if BLOCK is NULL. */
10175 static unsigned int
10176 add_ranges (const_tree block)
10178 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10181 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10182 When using dwarf_split_debug_info, address attributes in dies destined
10183 for the final executable should be direct references--setting the
10184 parameter force_direct ensures this behavior. */
10186 static void
10187 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10188 bool *added, bool force_direct)
10190 unsigned int in_use = ranges_by_label_in_use;
10191 unsigned int offset;
10193 if (in_use == ranges_by_label_allocated)
10195 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10196 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10197 ranges_by_label_allocated);
10198 memset (ranges_by_label + ranges_by_label_in_use, 0,
10199 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10202 ranges_by_label[in_use].begin = begin;
10203 ranges_by_label[in_use].end = end;
10204 ranges_by_label_in_use = in_use + 1;
10206 offset = add_ranges_num (-(int)in_use - 1);
10207 if (!*added)
10209 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10210 *added = true;
10214 static void
10215 output_ranges (void)
10217 unsigned i;
10218 static const char *const start_fmt = "Offset %#x";
10219 const char *fmt = start_fmt;
10221 for (i = 0; i < ranges_table_in_use; i++)
10223 int block_num = ranges_table[i].num;
10225 if (block_num > 0)
10227 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10228 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10230 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10231 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10233 /* If all code is in the text section, then the compilation
10234 unit base address defaults to DW_AT_low_pc, which is the
10235 base of the text section. */
10236 if (!have_multiple_function_sections)
10238 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10239 text_section_label,
10240 fmt, i * 2 * DWARF2_ADDR_SIZE);
10241 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10242 text_section_label, NULL);
10245 /* Otherwise, the compilation unit base address is zero,
10246 which allows us to use absolute addresses, and not worry
10247 about whether the target supports cross-section
10248 arithmetic. */
10249 else
10251 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10252 fmt, i * 2 * DWARF2_ADDR_SIZE);
10253 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10256 fmt = NULL;
10259 /* Negative block_num stands for an index into ranges_by_label. */
10260 else if (block_num < 0)
10262 int lab_idx = - block_num - 1;
10264 if (!have_multiple_function_sections)
10266 gcc_unreachable ();
10267 #if 0
10268 /* If we ever use add_ranges_by_labels () for a single
10269 function section, all we have to do is to take out
10270 the #if 0 above. */
10271 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10272 ranges_by_label[lab_idx].begin,
10273 text_section_label,
10274 fmt, i * 2 * DWARF2_ADDR_SIZE);
10275 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10276 ranges_by_label[lab_idx].end,
10277 text_section_label, NULL);
10278 #endif
10280 else
10282 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10283 ranges_by_label[lab_idx].begin,
10284 fmt, i * 2 * DWARF2_ADDR_SIZE);
10285 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10286 ranges_by_label[lab_idx].end,
10287 NULL);
10290 else
10292 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10293 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10294 fmt = start_fmt;
10299 /* Data structure containing information about input files. */
10300 struct file_info
10302 const char *path; /* Complete file name. */
10303 const char *fname; /* File name part. */
10304 int length; /* Length of entire string. */
10305 struct dwarf_file_data * file_idx; /* Index in input file table. */
10306 int dir_idx; /* Index in directory table. */
10309 /* Data structure containing information about directories with source
10310 files. */
10311 struct dir_info
10313 const char *path; /* Path including directory name. */
10314 int length; /* Path length. */
10315 int prefix; /* Index of directory entry which is a prefix. */
10316 int count; /* Number of files in this directory. */
10317 int dir_idx; /* Index of directory used as base. */
10320 /* Callback function for file_info comparison. We sort by looking at
10321 the directories in the path. */
10323 static int
10324 file_info_cmp (const void *p1, const void *p2)
10326 const struct file_info *const s1 = (const struct file_info *) p1;
10327 const struct file_info *const s2 = (const struct file_info *) p2;
10328 const unsigned char *cp1;
10329 const unsigned char *cp2;
10331 /* Take care of file names without directories. We need to make sure that
10332 we return consistent values to qsort since some will get confused if
10333 we return the same value when identical operands are passed in opposite
10334 orders. So if neither has a directory, return 0 and otherwise return
10335 1 or -1 depending on which one has the directory. */
10336 if ((s1->path == s1->fname || s2->path == s2->fname))
10337 return (s2->path == s2->fname) - (s1->path == s1->fname);
10339 cp1 = (const unsigned char *) s1->path;
10340 cp2 = (const unsigned char *) s2->path;
10342 while (1)
10344 ++cp1;
10345 ++cp2;
10346 /* Reached the end of the first path? If so, handle like above. */
10347 if ((cp1 == (const unsigned char *) s1->fname)
10348 || (cp2 == (const unsigned char *) s2->fname))
10349 return ((cp2 == (const unsigned char *) s2->fname)
10350 - (cp1 == (const unsigned char *) s1->fname));
10352 /* Character of current path component the same? */
10353 else if (*cp1 != *cp2)
10354 return *cp1 - *cp2;
10358 struct file_name_acquire_data
10360 struct file_info *files;
10361 int used_files;
10362 int max_files;
10365 /* Traversal function for the hash table. */
10368 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10370 struct dwarf_file_data *d = *slot;
10371 struct file_info *fi;
10372 const char *f;
10374 gcc_assert (fnad->max_files >= d->emitted_number);
10376 if (! d->emitted_number)
10377 return 1;
10379 gcc_assert (fnad->max_files != fnad->used_files);
10381 fi = fnad->files + fnad->used_files++;
10383 /* Skip all leading "./". */
10384 f = d->filename;
10385 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10386 f += 2;
10388 /* Create a new array entry. */
10389 fi->path = f;
10390 fi->length = strlen (f);
10391 fi->file_idx = d;
10393 /* Search for the file name part. */
10394 f = strrchr (f, DIR_SEPARATOR);
10395 #if defined (DIR_SEPARATOR_2)
10397 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10399 if (g != NULL)
10401 if (f == NULL || f < g)
10402 f = g;
10405 #endif
10407 fi->fname = f == NULL ? fi->path : f + 1;
10408 return 1;
10411 /* Output the directory table and the file name table. We try to minimize
10412 the total amount of memory needed. A heuristic is used to avoid large
10413 slowdowns with many input files. */
10415 static void
10416 output_file_names (void)
10418 struct file_name_acquire_data fnad;
10419 int numfiles;
10420 struct file_info *files;
10421 struct dir_info *dirs;
10422 int *saved;
10423 int *savehere;
10424 int *backmap;
10425 int ndirs;
10426 int idx_offset;
10427 int i;
10429 if (!last_emitted_file)
10431 dw2_asm_output_data (1, 0, "End directory table");
10432 dw2_asm_output_data (1, 0, "End file name table");
10433 return;
10436 numfiles = last_emitted_file->emitted_number;
10438 /* Allocate the various arrays we need. */
10439 files = XALLOCAVEC (struct file_info, numfiles);
10440 dirs = XALLOCAVEC (struct dir_info, numfiles);
10442 fnad.files = files;
10443 fnad.used_files = 0;
10444 fnad.max_files = numfiles;
10445 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10446 gcc_assert (fnad.used_files == fnad.max_files);
10448 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10450 /* Find all the different directories used. */
10451 dirs[0].path = files[0].path;
10452 dirs[0].length = files[0].fname - files[0].path;
10453 dirs[0].prefix = -1;
10454 dirs[0].count = 1;
10455 dirs[0].dir_idx = 0;
10456 files[0].dir_idx = 0;
10457 ndirs = 1;
10459 for (i = 1; i < numfiles; i++)
10460 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10461 && memcmp (dirs[ndirs - 1].path, files[i].path,
10462 dirs[ndirs - 1].length) == 0)
10464 /* Same directory as last entry. */
10465 files[i].dir_idx = ndirs - 1;
10466 ++dirs[ndirs - 1].count;
10468 else
10470 int j;
10472 /* This is a new directory. */
10473 dirs[ndirs].path = files[i].path;
10474 dirs[ndirs].length = files[i].fname - files[i].path;
10475 dirs[ndirs].count = 1;
10476 dirs[ndirs].dir_idx = ndirs;
10477 files[i].dir_idx = ndirs;
10479 /* Search for a prefix. */
10480 dirs[ndirs].prefix = -1;
10481 for (j = 0; j < ndirs; j++)
10482 if (dirs[j].length < dirs[ndirs].length
10483 && dirs[j].length > 1
10484 && (dirs[ndirs].prefix == -1
10485 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10486 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10487 dirs[ndirs].prefix = j;
10489 ++ndirs;
10492 /* Now to the actual work. We have to find a subset of the directories which
10493 allow expressing the file name using references to the directory table
10494 with the least amount of characters. We do not do an exhaustive search
10495 where we would have to check out every combination of every single
10496 possible prefix. Instead we use a heuristic which provides nearly optimal
10497 results in most cases and never is much off. */
10498 saved = XALLOCAVEC (int, ndirs);
10499 savehere = XALLOCAVEC (int, ndirs);
10501 memset (saved, '\0', ndirs * sizeof (saved[0]));
10502 for (i = 0; i < ndirs; i++)
10504 int j;
10505 int total;
10507 /* We can always save some space for the current directory. But this
10508 does not mean it will be enough to justify adding the directory. */
10509 savehere[i] = dirs[i].length;
10510 total = (savehere[i] - saved[i]) * dirs[i].count;
10512 for (j = i + 1; j < ndirs; j++)
10514 savehere[j] = 0;
10515 if (saved[j] < dirs[i].length)
10517 /* Determine whether the dirs[i] path is a prefix of the
10518 dirs[j] path. */
10519 int k;
10521 k = dirs[j].prefix;
10522 while (k != -1 && k != (int) i)
10523 k = dirs[k].prefix;
10525 if (k == (int) i)
10527 /* Yes it is. We can possibly save some memory by
10528 writing the filenames in dirs[j] relative to
10529 dirs[i]. */
10530 savehere[j] = dirs[i].length;
10531 total += (savehere[j] - saved[j]) * dirs[j].count;
10536 /* Check whether we can save enough to justify adding the dirs[i]
10537 directory. */
10538 if (total > dirs[i].length + 1)
10540 /* It's worthwhile adding. */
10541 for (j = i; j < ndirs; j++)
10542 if (savehere[j] > 0)
10544 /* Remember how much we saved for this directory so far. */
10545 saved[j] = savehere[j];
10547 /* Remember the prefix directory. */
10548 dirs[j].dir_idx = i;
10553 /* Emit the directory name table. */
10554 idx_offset = dirs[0].length > 0 ? 1 : 0;
10555 for (i = 1 - idx_offset; i < ndirs; i++)
10556 dw2_asm_output_nstring (dirs[i].path,
10557 dirs[i].length
10558 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10559 "Directory Entry: %#x", i + idx_offset);
10561 dw2_asm_output_data (1, 0, "End directory table");
10563 /* We have to emit them in the order of emitted_number since that's
10564 used in the debug info generation. To do this efficiently we
10565 generate a back-mapping of the indices first. */
10566 backmap = XALLOCAVEC (int, numfiles);
10567 for (i = 0; i < numfiles; i++)
10568 backmap[files[i].file_idx->emitted_number - 1] = i;
10570 /* Now write all the file names. */
10571 for (i = 0; i < numfiles; i++)
10573 int file_idx = backmap[i];
10574 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10576 #ifdef VMS_DEBUGGING_INFO
10577 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10579 /* Setting these fields can lead to debugger miscomparisons,
10580 but VMS Debug requires them to be set correctly. */
10582 int ver;
10583 long long cdt;
10584 long siz;
10585 int maxfilelen = strlen (files[file_idx].path)
10586 + dirs[dir_idx].length
10587 + MAX_VMS_VERSION_LEN + 1;
10588 char *filebuf = XALLOCAVEC (char, maxfilelen);
10590 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10591 snprintf (filebuf, maxfilelen, "%s;%d",
10592 files[file_idx].path + dirs[dir_idx].length, ver);
10594 dw2_asm_output_nstring
10595 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10597 /* Include directory index. */
10598 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10600 /* Modification time. */
10601 dw2_asm_output_data_uleb128
10602 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10603 ? cdt : 0,
10604 NULL);
10606 /* File length in bytes. */
10607 dw2_asm_output_data_uleb128
10608 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10609 ? siz : 0,
10610 NULL);
10611 #else
10612 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10613 "File Entry: %#x", (unsigned) i + 1);
10615 /* Include directory index. */
10616 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10618 /* Modification time. */
10619 dw2_asm_output_data_uleb128 (0, NULL);
10621 /* File length in bytes. */
10622 dw2_asm_output_data_uleb128 (0, NULL);
10623 #endif /* VMS_DEBUGGING_INFO */
10626 dw2_asm_output_data (1, 0, "End file name table");
10630 /* Output one line number table into the .debug_line section. */
10632 static void
10633 output_one_line_info_table (dw_line_info_table *table)
10635 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10636 unsigned int current_line = 1;
10637 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10638 dw_line_info_entry *ent;
10639 size_t i;
10641 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10643 switch (ent->opcode)
10645 case LI_set_address:
10646 /* ??? Unfortunately, we have little choice here currently, and
10647 must always use the most general form. GCC does not know the
10648 address delta itself, so we can't use DW_LNS_advance_pc. Many
10649 ports do have length attributes which will give an upper bound
10650 on the address range. We could perhaps use length attributes
10651 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10652 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10654 /* This can handle any delta. This takes
10655 4+DWARF2_ADDR_SIZE bytes. */
10656 dw2_asm_output_data (1, 0, "set address %s", line_label);
10657 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10658 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10659 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10660 break;
10662 case LI_set_line:
10663 if (ent->val == current_line)
10665 /* We still need to start a new row, so output a copy insn. */
10666 dw2_asm_output_data (1, DW_LNS_copy,
10667 "copy line %u", current_line);
10669 else
10671 int line_offset = ent->val - current_line;
10672 int line_delta = line_offset - DWARF_LINE_BASE;
10674 current_line = ent->val;
10675 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10677 /* This can handle deltas from -10 to 234, using the current
10678 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10679 This takes 1 byte. */
10680 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10681 "line %u", current_line);
10683 else
10685 /* This can handle any delta. This takes at least 4 bytes,
10686 depending on the value being encoded. */
10687 dw2_asm_output_data (1, DW_LNS_advance_line,
10688 "advance to line %u", current_line);
10689 dw2_asm_output_data_sleb128 (line_offset, NULL);
10690 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10693 break;
10695 case LI_set_file:
10696 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10697 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10698 break;
10700 case LI_set_column:
10701 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10702 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10703 break;
10705 case LI_negate_stmt:
10706 current_is_stmt = !current_is_stmt;
10707 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10708 "is_stmt %d", current_is_stmt);
10709 break;
10711 case LI_set_prologue_end:
10712 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10713 "set prologue end");
10714 break;
10716 case LI_set_epilogue_begin:
10717 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10718 "set epilogue begin");
10719 break;
10721 case LI_set_discriminator:
10722 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10723 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10724 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10725 dw2_asm_output_data_uleb128 (ent->val, NULL);
10726 break;
10730 /* Emit debug info for the address of the end of the table. */
10731 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10732 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10733 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10734 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10736 dw2_asm_output_data (1, 0, "end sequence");
10737 dw2_asm_output_data_uleb128 (1, NULL);
10738 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10741 /* Output the source line number correspondence information. This
10742 information goes into the .debug_line section. */
10744 static void
10745 output_line_info (bool prologue_only)
10747 static unsigned int generation;
10748 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
10749 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
10750 /* We don't support DWARFv5 line tables yet. */
10751 int ver = dwarf_version < 5 ? dwarf_version : 4;
10752 bool saw_one = false;
10753 int opc;
10755 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
10756 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
10757 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
10758 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
10760 if (!XCOFF_DEBUGGING_INFO)
10762 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10763 dw2_asm_output_data (4, 0xffffffff,
10764 "Initial length escape value indicating 64-bit DWARF extension");
10765 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10766 "Length of Source Line Info");
10769 ASM_OUTPUT_LABEL (asm_out_file, l1);
10771 dw2_asm_output_data (2, ver, "DWARF Version");
10772 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10773 ASM_OUTPUT_LABEL (asm_out_file, p1);
10775 /* Define the architecture-dependent minimum instruction length (in bytes).
10776 In this implementation of DWARF, this field is used for information
10777 purposes only. Since GCC generates assembly language, we have no
10778 a priori knowledge of how many instruction bytes are generated for each
10779 source line, and therefore can use only the DW_LNE_set_address and
10780 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10781 this as '1', which is "correct enough" for all architectures,
10782 and don't let the target override. */
10783 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10785 if (ver >= 4)
10786 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10787 "Maximum Operations Per Instruction");
10788 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10789 "Default is_stmt_start flag");
10790 dw2_asm_output_data (1, DWARF_LINE_BASE,
10791 "Line Base Value (Special Opcodes)");
10792 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10793 "Line Range Value (Special Opcodes)");
10794 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10795 "Special Opcode Base");
10797 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10799 int n_op_args;
10800 switch (opc)
10802 case DW_LNS_advance_pc:
10803 case DW_LNS_advance_line:
10804 case DW_LNS_set_file:
10805 case DW_LNS_set_column:
10806 case DW_LNS_fixed_advance_pc:
10807 case DW_LNS_set_isa:
10808 n_op_args = 1;
10809 break;
10810 default:
10811 n_op_args = 0;
10812 break;
10815 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10816 opc, n_op_args);
10819 /* Write out the information about the files we use. */
10820 output_file_names ();
10821 ASM_OUTPUT_LABEL (asm_out_file, p2);
10822 if (prologue_only)
10824 /* Output the marker for the end of the line number info. */
10825 ASM_OUTPUT_LABEL (asm_out_file, l2);
10826 return;
10829 if (separate_line_info)
10831 dw_line_info_table *table;
10832 size_t i;
10834 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10835 if (table->in_use)
10837 output_one_line_info_table (table);
10838 saw_one = true;
10841 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10843 output_one_line_info_table (cold_text_section_line_info);
10844 saw_one = true;
10847 /* ??? Some Darwin linkers crash on a .debug_line section with no
10848 sequences. Further, merely a DW_LNE_end_sequence entry is not
10849 sufficient -- the address column must also be initialized.
10850 Make sure to output at least one set_address/end_sequence pair,
10851 choosing .text since that section is always present. */
10852 if (text_section_line_info->in_use || !saw_one)
10853 output_one_line_info_table (text_section_line_info);
10855 /* Output the marker for the end of the line number info. */
10856 ASM_OUTPUT_LABEL (asm_out_file, l2);
10859 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
10861 static inline bool
10862 need_endianity_attribute_p (bool reverse)
10864 return reverse && (dwarf_version >= 3 || !dwarf_strict);
10867 /* Given a pointer to a tree node for some base type, return a pointer to
10868 a DIE that describes the given type. REVERSE is true if the type is
10869 to be interpreted in the reverse storage order wrt the target order.
10871 This routine must only be called for GCC type nodes that correspond to
10872 Dwarf base (fundamental) types. */
10874 static dw_die_ref
10875 base_type_die (tree type, bool reverse)
10877 dw_die_ref base_type_result;
10878 enum dwarf_type encoding;
10879 bool fpt_used = false;
10880 struct fixed_point_type_info fpt_info;
10881 tree type_bias = NULL_TREE;
10883 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10884 return 0;
10886 /* If this is a subtype that should not be emitted as a subrange type,
10887 use the base type. See subrange_type_for_debug_p. */
10888 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10889 type = TREE_TYPE (type);
10891 switch (TREE_CODE (type))
10893 case INTEGER_TYPE:
10894 if ((dwarf_version >= 4 || !dwarf_strict)
10895 && TYPE_NAME (type)
10896 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10897 && DECL_IS_BUILTIN (TYPE_NAME (type))
10898 && DECL_NAME (TYPE_NAME (type)))
10900 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10901 if (strcmp (name, "char16_t") == 0
10902 || strcmp (name, "char32_t") == 0)
10904 encoding = DW_ATE_UTF;
10905 break;
10908 if ((dwarf_version >= 3 || !dwarf_strict)
10909 && lang_hooks.types.get_fixed_point_type_info)
10911 memset (&fpt_info, 0, sizeof (fpt_info));
10912 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10914 fpt_used = true;
10915 encoding = ((TYPE_UNSIGNED (type))
10916 ? DW_ATE_unsigned_fixed
10917 : DW_ATE_signed_fixed);
10918 break;
10921 if (TYPE_STRING_FLAG (type))
10923 if (TYPE_UNSIGNED (type))
10924 encoding = DW_ATE_unsigned_char;
10925 else
10926 encoding = DW_ATE_signed_char;
10928 else if (TYPE_UNSIGNED (type))
10929 encoding = DW_ATE_unsigned;
10930 else
10931 encoding = DW_ATE_signed;
10933 if (!dwarf_strict
10934 && lang_hooks.types.get_type_bias)
10935 type_bias = lang_hooks.types.get_type_bias (type);
10936 break;
10938 case REAL_TYPE:
10939 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10941 if (dwarf_version >= 3 || !dwarf_strict)
10942 encoding = DW_ATE_decimal_float;
10943 else
10944 encoding = DW_ATE_lo_user;
10946 else
10947 encoding = DW_ATE_float;
10948 break;
10950 case FIXED_POINT_TYPE:
10951 if (!(dwarf_version >= 3 || !dwarf_strict))
10952 encoding = DW_ATE_lo_user;
10953 else if (TYPE_UNSIGNED (type))
10954 encoding = DW_ATE_unsigned_fixed;
10955 else
10956 encoding = DW_ATE_signed_fixed;
10957 break;
10959 /* Dwarf2 doesn't know anything about complex ints, so use
10960 a user defined type for it. */
10961 case COMPLEX_TYPE:
10962 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10963 encoding = DW_ATE_complex_float;
10964 else
10965 encoding = DW_ATE_lo_user;
10966 break;
10968 case BOOLEAN_TYPE:
10969 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10970 encoding = DW_ATE_boolean;
10971 break;
10973 default:
10974 /* No other TREE_CODEs are Dwarf fundamental types. */
10975 gcc_unreachable ();
10978 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10980 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10981 int_size_in_bytes (type));
10982 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10984 if (need_endianity_attribute_p (reverse))
10985 add_AT_unsigned (base_type_result, DW_AT_endianity,
10986 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10988 if (fpt_used)
10990 switch (fpt_info.scale_factor_kind)
10992 case fixed_point_scale_factor_binary:
10993 add_AT_int (base_type_result, DW_AT_binary_scale,
10994 fpt_info.scale_factor.binary);
10995 break;
10997 case fixed_point_scale_factor_decimal:
10998 add_AT_int (base_type_result, DW_AT_decimal_scale,
10999 fpt_info.scale_factor.decimal);
11000 break;
11002 case fixed_point_scale_factor_arbitrary:
11003 /* Arbitrary scale factors cannot be described in standard DWARF,
11004 yet. */
11005 if (!dwarf_strict)
11007 /* Describe the scale factor as a rational constant. */
11008 const dw_die_ref scale_factor
11009 = new_die (DW_TAG_constant, comp_unit_die (), type);
11011 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
11012 fpt_info.scale_factor.arbitrary.numerator);
11013 add_AT_int (scale_factor, DW_AT_GNU_denominator,
11014 fpt_info.scale_factor.arbitrary.denominator);
11016 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
11018 break;
11020 default:
11021 gcc_unreachable ();
11025 if (type_bias)
11026 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
11027 dw_scalar_form_constant
11028 | dw_scalar_form_exprloc
11029 | dw_scalar_form_reference,
11030 NULL);
11032 add_pubtype (type, base_type_result);
11034 return base_type_result;
11037 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
11038 named 'auto' in its type: return true for it, false otherwise. */
11040 static inline bool
11041 is_cxx_auto (tree type)
11043 if (is_cxx ())
11045 tree name = TYPE_IDENTIFIER (type);
11046 if (name == get_identifier ("auto")
11047 || name == get_identifier ("decltype(auto)"))
11048 return true;
11050 return false;
11053 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11054 given input type is a Dwarf "fundamental" type. Otherwise return null. */
11056 static inline int
11057 is_base_type (tree type)
11059 switch (TREE_CODE (type))
11061 case ERROR_MARK:
11062 case VOID_TYPE:
11063 case INTEGER_TYPE:
11064 case REAL_TYPE:
11065 case FIXED_POINT_TYPE:
11066 case COMPLEX_TYPE:
11067 case BOOLEAN_TYPE:
11068 case POINTER_BOUNDS_TYPE:
11069 return 1;
11071 case ARRAY_TYPE:
11072 case RECORD_TYPE:
11073 case UNION_TYPE:
11074 case QUAL_UNION_TYPE:
11075 case ENUMERAL_TYPE:
11076 case FUNCTION_TYPE:
11077 case METHOD_TYPE:
11078 case POINTER_TYPE:
11079 case REFERENCE_TYPE:
11080 case NULLPTR_TYPE:
11081 case OFFSET_TYPE:
11082 case LANG_TYPE:
11083 case VECTOR_TYPE:
11084 return 0;
11086 default:
11087 if (is_cxx_auto (type))
11088 return 0;
11089 gcc_unreachable ();
11092 return 0;
11095 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11096 node, return the size in bits for the type if it is a constant, or else
11097 return the alignment for the type if the type's size is not constant, or
11098 else return BITS_PER_WORD if the type actually turns out to be an
11099 ERROR_MARK node. */
11101 static inline unsigned HOST_WIDE_INT
11102 simple_type_size_in_bits (const_tree type)
11104 if (TREE_CODE (type) == ERROR_MARK)
11105 return BITS_PER_WORD;
11106 else if (TYPE_SIZE (type) == NULL_TREE)
11107 return 0;
11108 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11109 return tree_to_uhwi (TYPE_SIZE (type));
11110 else
11111 return TYPE_ALIGN (type);
11114 /* Similarly, but return an offset_int instead of UHWI. */
11116 static inline offset_int
11117 offset_int_type_size_in_bits (const_tree type)
11119 if (TREE_CODE (type) == ERROR_MARK)
11120 return BITS_PER_WORD;
11121 else if (TYPE_SIZE (type) == NULL_TREE)
11122 return 0;
11123 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11124 return wi::to_offset (TYPE_SIZE (type));
11125 else
11126 return TYPE_ALIGN (type);
11129 /* Given a pointer to a tree node for a subrange type, return a pointer
11130 to a DIE that describes the given type. */
11132 static dw_die_ref
11133 subrange_type_die (tree type, tree low, tree high, tree bias,
11134 dw_die_ref context_die)
11136 dw_die_ref subrange_die;
11137 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11139 if (context_die == NULL)
11140 context_die = comp_unit_die ();
11142 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11144 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11146 /* The size of the subrange type and its base type do not match,
11147 so we need to generate a size attribute for the subrange type. */
11148 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11151 if (low)
11152 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11153 if (high)
11154 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11155 if (bias && !dwarf_strict)
11156 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11157 dw_scalar_form_constant
11158 | dw_scalar_form_exprloc
11159 | dw_scalar_form_reference,
11160 NULL);
11162 return subrange_die;
11165 /* Returns the (const and/or volatile) cv_qualifiers associated with
11166 the decl node. This will normally be augmented with the
11167 cv_qualifiers of the underlying type in add_type_attribute. */
11169 static int
11170 decl_quals (const_tree decl)
11172 return ((TREE_READONLY (decl)
11173 /* The C++ front-end correctly marks reference-typed
11174 variables as readonly, but from a language (and debug
11175 info) standpoint they are not const-qualified. */
11176 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
11177 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11178 | (TREE_THIS_VOLATILE (decl)
11179 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11182 /* Determine the TYPE whose qualifiers match the largest strict subset
11183 of the given TYPE_QUALS, and return its qualifiers. Ignore all
11184 qualifiers outside QUAL_MASK. */
11186 static int
11187 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11189 tree t;
11190 int best_rank = 0, best_qual = 0, max_rank;
11192 type_quals &= qual_mask;
11193 max_rank = popcount_hwi (type_quals) - 1;
11195 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11196 t = TYPE_NEXT_VARIANT (t))
11198 int q = TYPE_QUALS (t) & qual_mask;
11200 if ((q & type_quals) == q && q != type_quals
11201 && check_base_type (t, type))
11203 int rank = popcount_hwi (q);
11205 if (rank > best_rank)
11207 best_rank = rank;
11208 best_qual = q;
11213 return best_qual;
11216 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11217 static const dwarf_qual_info_t dwarf_qual_info[] =
11219 { TYPE_QUAL_CONST, DW_TAG_const_type },
11220 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11221 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11222 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11224 static const unsigned int dwarf_qual_info_size
11225 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11227 /* If DIE is a qualified DIE of some base DIE with the same parent,
11228 return the base DIE, otherwise return NULL. Set MASK to the
11229 qualifiers added compared to the returned DIE. */
11231 static dw_die_ref
11232 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11234 unsigned int i;
11235 for (i = 0; i < dwarf_qual_info_size; i++)
11236 if (die->die_tag == dwarf_qual_info[i].t)
11237 break;
11238 if (i == dwarf_qual_info_size)
11239 return NULL;
11240 if (vec_safe_length (die->die_attr) != 1)
11241 return NULL;
11242 dw_die_ref type = get_AT_ref (die, DW_AT_type);
11243 if (type == NULL || type->die_parent != die->die_parent)
11244 return NULL;
11245 *mask |= dwarf_qual_info[i].q;
11246 if (depth)
11248 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11249 if (ret)
11250 return ret;
11252 return type;
11255 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11256 entry that chains the modifiers specified by CV_QUALS in front of the
11257 given type. REVERSE is true if the type is to be interpreted in the
11258 reverse storage order wrt the target order. */
11260 static dw_die_ref
11261 modified_type_die (tree type, int cv_quals, bool reverse,
11262 dw_die_ref context_die)
11264 enum tree_code code = TREE_CODE (type);
11265 dw_die_ref mod_type_die;
11266 dw_die_ref sub_die = NULL;
11267 tree item_type = NULL;
11268 tree qualified_type;
11269 tree name, low, high;
11270 dw_die_ref mod_scope;
11271 /* Only these cv-qualifiers are currently handled. */
11272 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11273 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11275 if (code == ERROR_MARK)
11276 return NULL;
11278 if (lang_hooks.types.get_debug_type)
11280 tree debug_type = lang_hooks.types.get_debug_type (type);
11282 if (debug_type != NULL_TREE && debug_type != type)
11283 return modified_type_die (debug_type, cv_quals, reverse, context_die);
11286 cv_quals &= cv_qual_mask;
11288 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11289 tag modifier (and not an attribute) old consumers won't be able
11290 to handle it. */
11291 if (dwarf_version < 3)
11292 cv_quals &= ~TYPE_QUAL_RESTRICT;
11294 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
11295 if (dwarf_version < 5)
11296 cv_quals &= ~TYPE_QUAL_ATOMIC;
11298 /* See if we already have the appropriately qualified variant of
11299 this type. */
11300 qualified_type = get_qualified_type (type, cv_quals);
11302 if (qualified_type == sizetype
11303 && TYPE_NAME (qualified_type)
11304 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11306 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11308 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11309 && TYPE_PRECISION (t)
11310 == TYPE_PRECISION (qualified_type)
11311 && TYPE_UNSIGNED (t)
11312 == TYPE_UNSIGNED (qualified_type));
11313 qualified_type = t;
11316 /* If we do, then we can just use its DIE, if it exists. */
11317 if (qualified_type)
11319 mod_type_die = lookup_type_die (qualified_type);
11321 /* DW_AT_endianity doesn't come from a qualifier on the type. */
11322 if (mod_type_die
11323 && (!need_endianity_attribute_p (reverse)
11324 || !is_base_type (type)
11325 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11326 return mod_type_die;
11329 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11331 /* Handle C typedef types. */
11332 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11333 && !DECL_ARTIFICIAL (name))
11335 tree dtype = TREE_TYPE (name);
11337 if (qualified_type == dtype)
11339 /* For a named type, use the typedef. */
11340 gen_type_die (qualified_type, context_die);
11341 return lookup_type_die (qualified_type);
11343 else
11345 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11346 dquals &= cv_qual_mask;
11347 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11348 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11349 /* cv-unqualified version of named type. Just use
11350 the unnamed type to which it refers. */
11351 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11352 reverse, context_die);
11353 /* Else cv-qualified version of named type; fall through. */
11357 mod_scope = scope_die_for (type, context_die);
11359 if (cv_quals)
11361 int sub_quals = 0, first_quals = 0;
11362 unsigned i;
11363 dw_die_ref first = NULL, last = NULL;
11365 /* Determine a lesser qualified type that most closely matches
11366 this one. Then generate DW_TAG_* entries for the remaining
11367 qualifiers. */
11368 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11369 cv_qual_mask);
11370 if (sub_quals && use_debug_types)
11372 bool needed = false;
11373 /* If emitting type units, make sure the order of qualifiers
11374 is canonical. Thus, start from unqualified type if
11375 an earlier qualifier is missing in sub_quals, but some later
11376 one is present there. */
11377 for (i = 0; i < dwarf_qual_info_size; i++)
11378 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11379 needed = true;
11380 else if (needed && (dwarf_qual_info[i].q & cv_quals))
11382 sub_quals = 0;
11383 break;
11386 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11387 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11389 /* As not all intermediate qualified DIEs have corresponding
11390 tree types, ensure that qualified DIEs in the same scope
11391 as their DW_AT_type are emitted after their DW_AT_type,
11392 only with other qualified DIEs for the same type possibly
11393 in between them. Determine the range of such qualified
11394 DIEs now (first being the base type, last being corresponding
11395 last qualified DIE for it). */
11396 unsigned int count = 0;
11397 first = qualified_die_p (mod_type_die, &first_quals,
11398 dwarf_qual_info_size);
11399 if (first == NULL)
11400 first = mod_type_die;
11401 gcc_assert ((first_quals & ~sub_quals) == 0);
11402 for (count = 0, last = first;
11403 count < (1U << dwarf_qual_info_size);
11404 count++, last = last->die_sib)
11406 int quals = 0;
11407 if (last == mod_scope->die_child)
11408 break;
11409 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11410 != first)
11411 break;
11415 for (i = 0; i < dwarf_qual_info_size; i++)
11416 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11418 dw_die_ref d;
11419 if (first && first != last)
11421 for (d = first->die_sib; ; d = d->die_sib)
11423 int quals = 0;
11424 qualified_die_p (d, &quals, dwarf_qual_info_size);
11425 if (quals == (first_quals | dwarf_qual_info[i].q))
11426 break;
11427 if (d == last)
11429 d = NULL;
11430 break;
11433 if (d)
11435 mod_type_die = d;
11436 continue;
11439 if (first)
11441 d = ggc_cleared_alloc<die_node> ();
11442 d->die_tag = dwarf_qual_info[i].t;
11443 add_child_die_after (mod_scope, d, last);
11444 last = d;
11446 else
11447 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11448 if (mod_type_die)
11449 add_AT_die_ref (d, DW_AT_type, mod_type_die);
11450 mod_type_die = d;
11451 first_quals |= dwarf_qual_info[i].q;
11454 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11456 dwarf_tag tag = DW_TAG_pointer_type;
11457 if (code == REFERENCE_TYPE)
11459 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11460 tag = DW_TAG_rvalue_reference_type;
11461 else
11462 tag = DW_TAG_reference_type;
11464 mod_type_die = new_die (tag, mod_scope, type);
11466 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11467 simple_type_size_in_bits (type) / BITS_PER_UNIT);
11468 item_type = TREE_TYPE (type);
11470 addr_space_t as = TYPE_ADDR_SPACE (item_type);
11471 if (!ADDR_SPACE_GENERIC_P (as))
11473 int action = targetm.addr_space.debug (as);
11474 if (action >= 0)
11476 /* Positive values indicate an address_class. */
11477 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11479 else
11481 /* Negative values indicate an (inverted) segment base reg. */
11482 dw_loc_descr_ref d
11483 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11484 add_AT_loc (mod_type_die, DW_AT_segment, d);
11488 else if (code == INTEGER_TYPE
11489 && TREE_TYPE (type) != NULL_TREE
11490 && subrange_type_for_debug_p (type, &low, &high))
11492 tree bias = NULL_TREE;
11493 if (lang_hooks.types.get_type_bias)
11494 bias = lang_hooks.types.get_type_bias (type);
11495 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11496 item_type = TREE_TYPE (type);
11498 else if (is_base_type (type))
11499 mod_type_die = base_type_die (type, reverse);
11500 else
11502 gen_type_die (type, context_die);
11504 /* We have to get the type_main_variant here (and pass that to the
11505 `lookup_type_die' routine) because the ..._TYPE node we have
11506 might simply be a *copy* of some original type node (where the
11507 copy was created to help us keep track of typedef names) and
11508 that copy might have a different TYPE_UID from the original
11509 ..._TYPE node. */
11510 if (TREE_CODE (type) != VECTOR_TYPE
11511 && TREE_CODE (type) != ARRAY_TYPE)
11512 return lookup_type_die (type_main_variant (type));
11513 else
11514 /* Vectors have the debugging information in the type,
11515 not the main variant. */
11516 return lookup_type_die (type);
11519 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
11520 don't output a DW_TAG_typedef, since there isn't one in the
11521 user's program; just attach a DW_AT_name to the type.
11522 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11523 if the base type already has the same name. */
11524 if (name
11525 && ((TREE_CODE (name) != TYPE_DECL
11526 && (qualified_type == TYPE_MAIN_VARIANT (type)
11527 || (cv_quals == TYPE_UNQUALIFIED)))
11528 || (TREE_CODE (name) == TYPE_DECL
11529 && TREE_TYPE (name) == qualified_type
11530 && DECL_NAME (name))))
11532 if (TREE_CODE (name) == TYPE_DECL)
11533 /* Could just call add_name_and_src_coords_attributes here,
11534 but since this is a builtin type it doesn't have any
11535 useful source coordinates anyway. */
11536 name = DECL_NAME (name);
11537 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11539 /* This probably indicates a bug. */
11540 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11542 name = TYPE_IDENTIFIER (type);
11543 add_name_attribute (mod_type_die,
11544 name ? IDENTIFIER_POINTER (name) : "__unknown__");
11547 if (qualified_type)
11548 equate_type_number_to_die (qualified_type, mod_type_die);
11550 if (item_type)
11551 /* We must do this after the equate_type_number_to_die call, in case
11552 this is a recursive type. This ensures that the modified_type_die
11553 recursion will terminate even if the type is recursive. Recursive
11554 types are possible in Ada. */
11555 sub_die = modified_type_die (item_type,
11556 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11557 reverse,
11558 context_die);
11560 if (sub_die != NULL)
11561 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11563 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11564 if (TYPE_ARTIFICIAL (type))
11565 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11567 return mod_type_die;
11570 /* Generate DIEs for the generic parameters of T.
11571 T must be either a generic type or a generic function.
11572 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11574 static void
11575 gen_generic_params_dies (tree t)
11577 tree parms, args;
11578 int parms_num, i;
11579 dw_die_ref die = NULL;
11580 int non_default;
11582 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11583 return;
11585 if (TYPE_P (t))
11586 die = lookup_type_die (t);
11587 else if (DECL_P (t))
11588 die = lookup_decl_die (t);
11590 gcc_assert (die);
11592 parms = lang_hooks.get_innermost_generic_parms (t);
11593 if (!parms)
11594 /* T has no generic parameter. It means T is neither a generic type
11595 or function. End of story. */
11596 return;
11598 parms_num = TREE_VEC_LENGTH (parms);
11599 args = lang_hooks.get_innermost_generic_args (t);
11600 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11601 non_default = int_cst_value (TREE_CHAIN (args));
11602 else
11603 non_default = TREE_VEC_LENGTH (args);
11604 for (i = 0; i < parms_num; i++)
11606 tree parm, arg, arg_pack_elems;
11607 dw_die_ref parm_die;
11609 parm = TREE_VEC_ELT (parms, i);
11610 arg = TREE_VEC_ELT (args, i);
11611 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11612 gcc_assert (parm && TREE_VALUE (parm) && arg);
11614 if (parm && TREE_VALUE (parm) && arg)
11616 /* If PARM represents a template parameter pack,
11617 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11618 by DW_TAG_template_*_parameter DIEs for the argument
11619 pack elements of ARG. Note that ARG would then be
11620 an argument pack. */
11621 if (arg_pack_elems)
11622 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11623 arg_pack_elems,
11624 die);
11625 else
11626 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11627 true /* emit name */, die);
11628 if (i >= non_default)
11629 add_AT_flag (parm_die, DW_AT_default_value, 1);
11634 /* Create and return a DIE for PARM which should be
11635 the representation of a generic type parameter.
11636 For instance, in the C++ front end, PARM would be a template parameter.
11637 ARG is the argument to PARM.
11638 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11639 name of the PARM.
11640 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11641 as a child node. */
11643 static dw_die_ref
11644 generic_parameter_die (tree parm, tree arg,
11645 bool emit_name_p,
11646 dw_die_ref parent_die)
11648 dw_die_ref tmpl_die = NULL;
11649 const char *name = NULL;
11651 if (!parm || !DECL_NAME (parm) || !arg)
11652 return NULL;
11654 /* We support non-type generic parameters and arguments,
11655 type generic parameters and arguments, as well as
11656 generic generic parameters (a.k.a. template template parameters in C++)
11657 and arguments. */
11658 if (TREE_CODE (parm) == PARM_DECL)
11659 /* PARM is a nontype generic parameter */
11660 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11661 else if (TREE_CODE (parm) == TYPE_DECL)
11662 /* PARM is a type generic parameter. */
11663 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11664 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11665 /* PARM is a generic generic parameter.
11666 Its DIE is a GNU extension. It shall have a
11667 DW_AT_name attribute to represent the name of the template template
11668 parameter, and a DW_AT_GNU_template_name attribute to represent the
11669 name of the template template argument. */
11670 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11671 parent_die, parm);
11672 else
11673 gcc_unreachable ();
11675 if (tmpl_die)
11677 tree tmpl_type;
11679 /* If PARM is a generic parameter pack, it means we are
11680 emitting debug info for a template argument pack element.
11681 In other terms, ARG is a template argument pack element.
11682 In that case, we don't emit any DW_AT_name attribute for
11683 the die. */
11684 if (emit_name_p)
11686 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11687 gcc_assert (name);
11688 add_AT_string (tmpl_die, DW_AT_name, name);
11691 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11693 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11694 TMPL_DIE should have a child DW_AT_type attribute that is set
11695 to the type of the argument to PARM, which is ARG.
11696 If PARM is a type generic parameter, TMPL_DIE should have a
11697 child DW_AT_type that is set to ARG. */
11698 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11699 add_type_attribute (tmpl_die, tmpl_type,
11700 (TREE_THIS_VOLATILE (tmpl_type)
11701 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11702 false, parent_die);
11704 else
11706 /* So TMPL_DIE is a DIE representing a
11707 a generic generic template parameter, a.k.a template template
11708 parameter in C++ and arg is a template. */
11710 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11711 to the name of the argument. */
11712 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11713 if (name)
11714 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11717 if (TREE_CODE (parm) == PARM_DECL)
11718 /* So PARM is a non-type generic parameter.
11719 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11720 attribute of TMPL_DIE which value represents the value
11721 of ARG.
11722 We must be careful here:
11723 The value of ARG might reference some function decls.
11724 We might currently be emitting debug info for a generic
11725 type and types are emitted before function decls, we don't
11726 know if the function decls referenced by ARG will actually be
11727 emitted after cgraph computations.
11728 So must defer the generation of the DW_AT_const_value to
11729 after cgraph is ready. */
11730 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11733 return tmpl_die;
11736 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11737 PARM_PACK must be a template parameter pack. The returned DIE
11738 will be child DIE of PARENT_DIE. */
11740 static dw_die_ref
11741 template_parameter_pack_die (tree parm_pack,
11742 tree parm_pack_args,
11743 dw_die_ref parent_die)
11745 dw_die_ref die;
11746 int j;
11748 gcc_assert (parent_die && parm_pack);
11750 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11751 add_name_and_src_coords_attributes (die, parm_pack);
11752 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11753 generic_parameter_die (parm_pack,
11754 TREE_VEC_ELT (parm_pack_args, j),
11755 false /* Don't emit DW_AT_name */,
11756 die);
11757 return die;
11760 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11761 an enumerated type. */
11763 static inline int
11764 type_is_enum (const_tree type)
11766 return TREE_CODE (type) == ENUMERAL_TYPE;
11769 /* Return the DBX register number described by a given RTL node. */
11771 static unsigned int
11772 dbx_reg_number (const_rtx rtl)
11774 unsigned regno = REGNO (rtl);
11776 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11778 #ifdef LEAF_REG_REMAP
11779 if (crtl->uses_only_leaf_regs)
11781 int leaf_reg = LEAF_REG_REMAP (regno);
11782 if (leaf_reg != -1)
11783 regno = (unsigned) leaf_reg;
11785 #endif
11787 regno = DBX_REGISTER_NUMBER (regno);
11788 gcc_assert (regno != INVALID_REGNUM);
11789 return regno;
11792 /* Optionally add a DW_OP_piece term to a location description expression.
11793 DW_OP_piece is only added if the location description expression already
11794 doesn't end with DW_OP_piece. */
11796 static void
11797 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11799 dw_loc_descr_ref loc;
11801 if (*list_head != NULL)
11803 /* Find the end of the chain. */
11804 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11807 if (loc->dw_loc_opc != DW_OP_piece)
11808 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11812 /* Return a location descriptor that designates a machine register or
11813 zero if there is none. */
11815 static dw_loc_descr_ref
11816 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11818 rtx regs;
11820 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11821 return 0;
11823 /* We only use "frame base" when we're sure we're talking about the
11824 post-prologue local stack frame. We do this by *not* running
11825 register elimination until this point, and recognizing the special
11826 argument pointer and soft frame pointer rtx's.
11827 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11828 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11829 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11831 dw_loc_descr_ref result = NULL;
11833 if (dwarf_version >= 4 || !dwarf_strict)
11835 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11836 initialized);
11837 if (result)
11838 add_loc_descr (&result,
11839 new_loc_descr (DW_OP_stack_value, 0, 0));
11841 return result;
11844 regs = targetm.dwarf_register_span (rtl);
11846 if (REG_NREGS (rtl) > 1 || regs)
11847 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11848 else
11850 unsigned int dbx_regnum = dbx_reg_number (rtl);
11851 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11852 return 0;
11853 return one_reg_loc_descriptor (dbx_regnum, initialized);
11857 /* Return a location descriptor that designates a machine register for
11858 a given hard register number. */
11860 static dw_loc_descr_ref
11861 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11863 dw_loc_descr_ref reg_loc_descr;
11865 if (regno <= 31)
11866 reg_loc_descr
11867 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11868 else
11869 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11871 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11872 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11874 return reg_loc_descr;
11877 /* Given an RTL of a register, return a location descriptor that
11878 designates a value that spans more than one register. */
11880 static dw_loc_descr_ref
11881 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11882 enum var_init_status initialized)
11884 int size, i;
11885 dw_loc_descr_ref loc_result = NULL;
11887 /* Simple, contiguous registers. */
11888 if (regs == NULL_RTX)
11890 unsigned reg = REGNO (rtl);
11891 int nregs;
11893 #ifdef LEAF_REG_REMAP
11894 if (crtl->uses_only_leaf_regs)
11896 int leaf_reg = LEAF_REG_REMAP (reg);
11897 if (leaf_reg != -1)
11898 reg = (unsigned) leaf_reg;
11900 #endif
11902 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11903 nregs = REG_NREGS (rtl);
11905 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11907 loc_result = NULL;
11908 while (nregs--)
11910 dw_loc_descr_ref t;
11912 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11913 VAR_INIT_STATUS_INITIALIZED);
11914 add_loc_descr (&loc_result, t);
11915 add_loc_descr_op_piece (&loc_result, size);
11916 ++reg;
11918 return loc_result;
11921 /* Now onto stupid register sets in non contiguous locations. */
11923 gcc_assert (GET_CODE (regs) == PARALLEL);
11925 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11926 loc_result = NULL;
11928 for (i = 0; i < XVECLEN (regs, 0); ++i)
11930 dw_loc_descr_ref t;
11932 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11933 VAR_INIT_STATUS_INITIALIZED);
11934 add_loc_descr (&loc_result, t);
11935 add_loc_descr_op_piece (&loc_result, size);
11938 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11939 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11940 return loc_result;
11943 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11945 /* Return a location descriptor that designates a constant i,
11946 as a compound operation from constant (i >> shift), constant shift
11947 and DW_OP_shl. */
11949 static dw_loc_descr_ref
11950 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11952 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11953 add_loc_descr (&ret, int_loc_descriptor (shift));
11954 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11955 return ret;
11958 /* Return a location descriptor that designates a constant. */
11960 static dw_loc_descr_ref
11961 int_loc_descriptor (HOST_WIDE_INT i)
11963 enum dwarf_location_atom op;
11965 /* Pick the smallest representation of a constant, rather than just
11966 defaulting to the LEB encoding. */
11967 if (i >= 0)
11969 int clz = clz_hwi (i);
11970 int ctz = ctz_hwi (i);
11971 if (i <= 31)
11972 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11973 else if (i <= 0xff)
11974 op = DW_OP_const1u;
11975 else if (i <= 0xffff)
11976 op = DW_OP_const2u;
11977 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11978 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11979 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11980 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11981 while DW_OP_const4u is 5 bytes. */
11982 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11983 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11984 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11985 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11986 while DW_OP_const4u is 5 bytes. */
11987 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11988 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11989 op = DW_OP_const4u;
11990 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11991 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11992 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11993 while DW_OP_constu of constant >= 0x100000000 takes at least
11994 6 bytes. */
11995 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11996 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11997 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11998 >= HOST_BITS_PER_WIDE_INT)
11999 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
12000 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
12001 while DW_OP_constu takes in this case at least 6 bytes. */
12002 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
12003 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12004 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12005 && size_of_uleb128 (i) > 6)
12006 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
12007 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
12008 else
12009 op = DW_OP_constu;
12011 else
12013 if (i >= -0x80)
12014 op = DW_OP_const1s;
12015 else if (i >= -0x8000)
12016 op = DW_OP_const2s;
12017 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12019 if (size_of_int_loc_descriptor (i) < 5)
12021 dw_loc_descr_ref ret = int_loc_descriptor (-i);
12022 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12023 return ret;
12025 op = DW_OP_const4s;
12027 else
12029 if (size_of_int_loc_descriptor (i)
12030 < (unsigned long) 1 + size_of_sleb128 (i))
12032 dw_loc_descr_ref ret = int_loc_descriptor (-i);
12033 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12034 return ret;
12036 op = DW_OP_consts;
12040 return new_loc_descr (op, i, 0);
12043 /* Likewise, for unsigned constants. */
12045 static dw_loc_descr_ref
12046 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12048 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12049 const unsigned HOST_WIDE_INT max_uint
12050 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12052 /* If possible, use the clever signed constants handling. */
12053 if (i <= max_int)
12054 return int_loc_descriptor ((HOST_WIDE_INT) i);
12056 /* Here, we are left with positive numbers that cannot be represented as
12057 HOST_WIDE_INT, i.e.:
12058 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12060 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12061 whereas may be better to output a negative integer: thanks to integer
12062 wrapping, we know that:
12063 x = x - 2 ** DWARF2_ADDR_SIZE
12064 = x - 2 * (max (HOST_WIDE_INT) + 1)
12065 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12066 small negative integers. Let's try that in cases it will clearly improve
12067 the encoding: there is no gain turning DW_OP_const4u into
12068 DW_OP_const4s. */
12069 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12070 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12071 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12073 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12075 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
12076 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
12077 const HOST_WIDE_INT second_shift
12078 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12080 /* So we finally have:
12081 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12082 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
12083 return int_loc_descriptor (second_shift);
12086 /* Last chance: fallback to a simple constant operation. */
12087 return new_loc_descr
12088 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12089 ? DW_OP_const4u
12090 : DW_OP_const8u,
12091 i, 0);
12094 /* Generate and return a location description that computes the unsigned
12095 comparison of the two stack top entries (a OP b where b is the top-most
12096 entry and a is the second one). The KIND of comparison can be LT_EXPR,
12097 LE_EXPR, GT_EXPR or GE_EXPR. */
12099 static dw_loc_descr_ref
12100 uint_comparison_loc_list (enum tree_code kind)
12102 enum dwarf_location_atom op, flip_op;
12103 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12105 switch (kind)
12107 case LT_EXPR:
12108 op = DW_OP_lt;
12109 break;
12110 case LE_EXPR:
12111 op = DW_OP_le;
12112 break;
12113 case GT_EXPR:
12114 op = DW_OP_gt;
12115 break;
12116 case GE_EXPR:
12117 op = DW_OP_ge;
12118 break;
12119 default:
12120 gcc_unreachable ();
12123 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12124 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12126 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
12127 possible to perform unsigned comparisons: we just have to distinguish
12128 three cases:
12130 1. when a and b have the same sign (as signed integers); then we should
12131 return: a OP(signed) b;
12133 2. when a is a negative signed integer while b is a positive one, then a
12134 is a greater unsigned integer than b; likewise when a and b's roles
12135 are flipped.
12137 So first, compare the sign of the two operands. */
12138 ret = new_loc_descr (DW_OP_over, 0, 0);
12139 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12140 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12141 /* If they have different signs (i.e. they have different sign bits), then
12142 the stack top value has now the sign bit set and thus it's smaller than
12143 zero. */
12144 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12145 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12146 add_loc_descr (&ret, bra_node);
12148 /* We are in case 1. At this point, we know both operands have the same
12149 sign, to it's safe to use the built-in signed comparison. */
12150 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12151 add_loc_descr (&ret, jmp_node);
12153 /* We are in case 2. Here, we know both operands do not have the same sign,
12154 so we have to flip the signed comparison. */
12155 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12156 tmp = new_loc_descr (flip_op, 0, 0);
12157 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12158 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12159 add_loc_descr (&ret, tmp);
12161 /* This dummy operation is necessary to make the two branches join. */
12162 tmp = new_loc_descr (DW_OP_nop, 0, 0);
12163 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12164 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12165 add_loc_descr (&ret, tmp);
12167 return ret;
12170 /* Likewise, but takes the location description lists (might be destructive on
12171 them). Return NULL if either is NULL or if concatenation fails. */
12173 static dw_loc_list_ref
12174 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12175 enum tree_code kind)
12177 if (left == NULL || right == NULL)
12178 return NULL;
12180 add_loc_list (&left, right);
12181 if (left == NULL)
12182 return NULL;
12184 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12185 return left;
12188 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12189 without actually allocating it. */
12191 static unsigned long
12192 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12194 return size_of_int_loc_descriptor (i >> shift)
12195 + size_of_int_loc_descriptor (shift)
12196 + 1;
12199 /* Return size_of_locs (int_loc_descriptor (i)) without
12200 actually allocating it. */
12202 static unsigned long
12203 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12205 unsigned long s;
12207 if (i >= 0)
12209 int clz, ctz;
12210 if (i <= 31)
12211 return 1;
12212 else if (i <= 0xff)
12213 return 2;
12214 else if (i <= 0xffff)
12215 return 3;
12216 clz = clz_hwi (i);
12217 ctz = ctz_hwi (i);
12218 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12219 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12220 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12221 - clz - 5);
12222 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12223 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12224 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12225 - clz - 8);
12226 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12227 return 5;
12228 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12229 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12230 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12231 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12232 - clz - 8);
12233 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12234 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12235 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12236 - clz - 16);
12237 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12238 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12239 && s > 6)
12240 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12241 - clz - 32);
12242 else
12243 return 1 + s;
12245 else
12247 if (i >= -0x80)
12248 return 2;
12249 else if (i >= -0x8000)
12250 return 3;
12251 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12253 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12255 s = size_of_int_loc_descriptor (-i) + 1;
12256 if (s < 5)
12257 return s;
12259 return 5;
12261 else
12263 unsigned long r = 1 + size_of_sleb128 (i);
12264 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12266 s = size_of_int_loc_descriptor (-i) + 1;
12267 if (s < r)
12268 return s;
12270 return r;
12275 /* Return loc description representing "address" of integer value.
12276 This can appear only as toplevel expression. */
12278 static dw_loc_descr_ref
12279 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12281 int litsize;
12282 dw_loc_descr_ref loc_result = NULL;
12284 if (!(dwarf_version >= 4 || !dwarf_strict))
12285 return NULL;
12287 litsize = size_of_int_loc_descriptor (i);
12288 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12289 is more compact. For DW_OP_stack_value we need:
12290 litsize + 1 (DW_OP_stack_value)
12291 and for DW_OP_implicit_value:
12292 1 (DW_OP_implicit_value) + 1 (length) + size. */
12293 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12295 loc_result = int_loc_descriptor (i);
12296 add_loc_descr (&loc_result,
12297 new_loc_descr (DW_OP_stack_value, 0, 0));
12298 return loc_result;
12301 loc_result = new_loc_descr (DW_OP_implicit_value,
12302 size, 0);
12303 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12304 loc_result->dw_loc_oprnd2.v.val_int = i;
12305 return loc_result;
12308 /* Return a location descriptor that designates a base+offset location. */
12310 static dw_loc_descr_ref
12311 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12312 enum var_init_status initialized)
12314 unsigned int regno;
12315 dw_loc_descr_ref result;
12316 dw_fde_ref fde = cfun->fde;
12318 /* We only use "frame base" when we're sure we're talking about the
12319 post-prologue local stack frame. We do this by *not* running
12320 register elimination until this point, and recognizing the special
12321 argument pointer and soft frame pointer rtx's. */
12322 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12324 rtx elim = (ira_use_lra_p
12325 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12326 : eliminate_regs (reg, VOIDmode, NULL_RTX));
12328 if (elim != reg)
12330 if (GET_CODE (elim) == PLUS)
12332 offset += INTVAL (XEXP (elim, 1));
12333 elim = XEXP (elim, 0);
12335 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12336 && (elim == hard_frame_pointer_rtx
12337 || elim == stack_pointer_rtx))
12338 || elim == (frame_pointer_needed
12339 ? hard_frame_pointer_rtx
12340 : stack_pointer_rtx));
12342 /* If drap register is used to align stack, use frame
12343 pointer + offset to access stack variables. If stack
12344 is aligned without drap, use stack pointer + offset to
12345 access stack variables. */
12346 if (crtl->stack_realign_tried
12347 && reg == frame_pointer_rtx)
12349 int base_reg
12350 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12351 ? HARD_FRAME_POINTER_REGNUM
12352 : REGNO (elim));
12353 return new_reg_loc_descr (base_reg, offset);
12356 gcc_assert (frame_pointer_fb_offset_valid);
12357 offset += frame_pointer_fb_offset;
12358 return new_loc_descr (DW_OP_fbreg, offset, 0);
12362 regno = REGNO (reg);
12363 #ifdef LEAF_REG_REMAP
12364 if (crtl->uses_only_leaf_regs)
12366 int leaf_reg = LEAF_REG_REMAP (regno);
12367 if (leaf_reg != -1)
12368 regno = (unsigned) leaf_reg;
12370 #endif
12371 regno = DWARF_FRAME_REGNUM (regno);
12373 if (!optimize && fde
12374 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12376 /* Use cfa+offset to represent the location of arguments passed
12377 on the stack when drap is used to align stack.
12378 Only do this when not optimizing, for optimized code var-tracking
12379 is supposed to track where the arguments live and the register
12380 used as vdrap or drap in some spot might be used for something
12381 else in other part of the routine. */
12382 return new_loc_descr (DW_OP_fbreg, offset, 0);
12385 if (regno <= 31)
12386 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12387 offset, 0);
12388 else
12389 result = new_loc_descr (DW_OP_bregx, regno, offset);
12391 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12392 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12394 return result;
12397 /* Return true if this RTL expression describes a base+offset calculation. */
12399 static inline int
12400 is_based_loc (const_rtx rtl)
12402 return (GET_CODE (rtl) == PLUS
12403 && ((REG_P (XEXP (rtl, 0))
12404 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12405 && CONST_INT_P (XEXP (rtl, 1)))));
12408 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12409 failed. */
12411 static dw_loc_descr_ref
12412 tls_mem_loc_descriptor (rtx mem)
12414 tree base;
12415 dw_loc_descr_ref loc_result;
12417 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12418 return NULL;
12420 base = get_base_address (MEM_EXPR (mem));
12421 if (base == NULL
12422 || !VAR_P (base)
12423 || !DECL_THREAD_LOCAL_P (base))
12424 return NULL;
12426 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12427 if (loc_result == NULL)
12428 return NULL;
12430 if (MEM_OFFSET (mem))
12431 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12433 return loc_result;
12436 /* Output debug info about reason why we failed to expand expression as dwarf
12437 expression. */
12439 static void
12440 expansion_failed (tree expr, rtx rtl, char const *reason)
12442 if (dump_file && (dump_flags & TDF_DETAILS))
12444 fprintf (dump_file, "Failed to expand as dwarf: ");
12445 if (expr)
12446 print_generic_expr (dump_file, expr, dump_flags);
12447 if (rtl)
12449 fprintf (dump_file, "\n");
12450 print_rtl (dump_file, rtl);
12452 fprintf (dump_file, "\nReason: %s\n", reason);
12456 /* Helper function for const_ok_for_output. */
12458 static bool
12459 const_ok_for_output_1 (rtx rtl)
12461 if (GET_CODE (rtl) == UNSPEC)
12463 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12464 we can't express it in the debug info. */
12465 /* Don't complain about TLS UNSPECs, those are just too hard to
12466 delegitimize. Note this could be a non-decl SYMBOL_REF such as
12467 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12468 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
12469 if (flag_checking
12470 && (XVECLEN (rtl, 0) == 0
12471 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12472 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12473 inform (current_function_decl
12474 ? DECL_SOURCE_LOCATION (current_function_decl)
12475 : UNKNOWN_LOCATION,
12476 #if NUM_UNSPEC_VALUES > 0
12477 "non-delegitimized UNSPEC %s (%d) found in variable location",
12478 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12479 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12480 XINT (rtl, 1));
12481 #else
12482 "non-delegitimized UNSPEC %d found in variable location",
12483 XINT (rtl, 1));
12484 #endif
12485 expansion_failed (NULL_TREE, rtl,
12486 "UNSPEC hasn't been delegitimized.\n");
12487 return false;
12490 if (targetm.const_not_ok_for_debug_p (rtl))
12492 expansion_failed (NULL_TREE, rtl,
12493 "Expression rejected for debug by the backend.\n");
12494 return false;
12497 /* FIXME: Refer to PR60655. It is possible for simplification
12498 of rtl expressions in var tracking to produce such expressions.
12499 We should really identify / validate expressions
12500 enclosed in CONST that can be handled by assemblers on various
12501 targets and only handle legitimate cases here. */
12502 if (GET_CODE (rtl) != SYMBOL_REF)
12504 if (GET_CODE (rtl) == NOT)
12505 return false;
12506 return true;
12509 if (CONSTANT_POOL_ADDRESS_P (rtl))
12511 bool marked;
12512 get_pool_constant_mark (rtl, &marked);
12513 /* If all references to this pool constant were optimized away,
12514 it was not output and thus we can't represent it. */
12515 if (!marked)
12517 expansion_failed (NULL_TREE, rtl,
12518 "Constant was removed from constant pool.\n");
12519 return false;
12523 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12524 return false;
12526 /* Avoid references to external symbols in debug info, on several targets
12527 the linker might even refuse to link when linking a shared library,
12528 and in many other cases the relocations for .debug_info/.debug_loc are
12529 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
12530 to be defined within the same shared library or executable are fine. */
12531 if (SYMBOL_REF_EXTERNAL_P (rtl))
12533 tree decl = SYMBOL_REF_DECL (rtl);
12535 if (decl == NULL || !targetm.binds_local_p (decl))
12537 expansion_failed (NULL_TREE, rtl,
12538 "Symbol not defined in current TU.\n");
12539 return false;
12543 return true;
12546 /* Return true if constant RTL can be emitted in DW_OP_addr or
12547 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
12548 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
12550 static bool
12551 const_ok_for_output (rtx rtl)
12553 if (GET_CODE (rtl) == SYMBOL_REF)
12554 return const_ok_for_output_1 (rtl);
12556 if (GET_CODE (rtl) == CONST)
12558 subrtx_var_iterator::array_type array;
12559 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12560 if (!const_ok_for_output_1 (*iter))
12561 return false;
12562 return true;
12565 return true;
12568 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12569 if possible, NULL otherwise. */
12571 static dw_die_ref
12572 base_type_for_mode (machine_mode mode, bool unsignedp)
12574 dw_die_ref type_die;
12575 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12577 if (type == NULL)
12578 return NULL;
12579 switch (TREE_CODE (type))
12581 case INTEGER_TYPE:
12582 case REAL_TYPE:
12583 break;
12584 default:
12585 return NULL;
12587 type_die = lookup_type_die (type);
12588 if (!type_die)
12589 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12590 comp_unit_die ());
12591 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12592 return NULL;
12593 return type_die;
12596 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12597 type matching MODE, or, if MODE is narrower than or as wide as
12598 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
12599 possible. */
12601 static dw_loc_descr_ref
12602 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12604 machine_mode outer_mode = mode;
12605 dw_die_ref type_die;
12606 dw_loc_descr_ref cvt;
12608 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12610 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12611 return op;
12613 type_die = base_type_for_mode (outer_mode, 1);
12614 if (type_die == NULL)
12615 return NULL;
12616 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12617 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12618 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12619 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12620 add_loc_descr (&op, cvt);
12621 return op;
12624 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
12626 static dw_loc_descr_ref
12627 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12628 dw_loc_descr_ref op1)
12630 dw_loc_descr_ref ret = op0;
12631 add_loc_descr (&ret, op1);
12632 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12633 if (STORE_FLAG_VALUE != 1)
12635 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12636 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12638 return ret;
12641 /* Return location descriptor for signed comparison OP RTL. */
12643 static dw_loc_descr_ref
12644 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12645 machine_mode mem_mode)
12647 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12648 dw_loc_descr_ref op0, op1;
12649 int shift;
12651 if (op_mode == VOIDmode)
12652 op_mode = GET_MODE (XEXP (rtl, 1));
12653 if (op_mode == VOIDmode)
12654 return NULL;
12656 if (dwarf_strict
12657 && (!SCALAR_INT_MODE_P (op_mode)
12658 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12659 return NULL;
12661 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12662 VAR_INIT_STATUS_INITIALIZED);
12663 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12664 VAR_INIT_STATUS_INITIALIZED);
12666 if (op0 == NULL || op1 == NULL)
12667 return NULL;
12669 if (!SCALAR_INT_MODE_P (op_mode)
12670 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12671 return compare_loc_descriptor (op, op0, op1);
12673 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12675 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12676 dw_loc_descr_ref cvt;
12678 if (type_die == NULL)
12679 return NULL;
12680 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12681 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12682 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12683 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12684 add_loc_descr (&op0, cvt);
12685 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12686 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12687 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12688 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12689 add_loc_descr (&op1, cvt);
12690 return compare_loc_descriptor (op, op0, op1);
12693 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12694 /* For eq/ne, if the operands are known to be zero-extended,
12695 there is no need to do the fancy shifting up. */
12696 if (op == DW_OP_eq || op == DW_OP_ne)
12698 dw_loc_descr_ref last0, last1;
12699 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12701 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12703 /* deref_size zero extends, and for constants we can check
12704 whether they are zero extended or not. */
12705 if (((last0->dw_loc_opc == DW_OP_deref_size
12706 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12707 || (CONST_INT_P (XEXP (rtl, 0))
12708 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12709 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12710 && ((last1->dw_loc_opc == DW_OP_deref_size
12711 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12712 || (CONST_INT_P (XEXP (rtl, 1))
12713 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12714 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12715 return compare_loc_descriptor (op, op0, op1);
12717 /* EQ/NE comparison against constant in narrower type than
12718 DWARF2_ADDR_SIZE can be performed either as
12719 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12720 DW_OP_{eq,ne}
12722 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12723 DW_OP_{eq,ne}. Pick whatever is shorter. */
12724 if (CONST_INT_P (XEXP (rtl, 1))
12725 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12726 && (size_of_int_loc_descriptor (shift) + 1
12727 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12728 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12729 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12730 & GET_MODE_MASK (op_mode))))
12732 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12733 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12734 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12735 & GET_MODE_MASK (op_mode));
12736 return compare_loc_descriptor (op, op0, op1);
12739 add_loc_descr (&op0, int_loc_descriptor (shift));
12740 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12741 if (CONST_INT_P (XEXP (rtl, 1)))
12742 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12743 else
12745 add_loc_descr (&op1, int_loc_descriptor (shift));
12746 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12748 return compare_loc_descriptor (op, op0, op1);
12751 /* Return location descriptor for unsigned comparison OP RTL. */
12753 static dw_loc_descr_ref
12754 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12755 machine_mode mem_mode)
12757 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12758 dw_loc_descr_ref op0, op1;
12760 if (op_mode == VOIDmode)
12761 op_mode = GET_MODE (XEXP (rtl, 1));
12762 if (op_mode == VOIDmode)
12763 return NULL;
12764 if (!SCALAR_INT_MODE_P (op_mode))
12765 return NULL;
12767 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12768 return NULL;
12770 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12771 VAR_INIT_STATUS_INITIALIZED);
12772 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12773 VAR_INIT_STATUS_INITIALIZED);
12775 if (op0 == NULL || op1 == NULL)
12776 return NULL;
12778 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12780 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12781 dw_loc_descr_ref last0, last1;
12782 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12784 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12786 if (CONST_INT_P (XEXP (rtl, 0)))
12787 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12788 /* deref_size zero extends, so no need to mask it again. */
12789 else if (last0->dw_loc_opc != DW_OP_deref_size
12790 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12792 add_loc_descr (&op0, int_loc_descriptor (mask));
12793 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12795 if (CONST_INT_P (XEXP (rtl, 1)))
12796 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12797 /* deref_size zero extends, so no need to mask it again. */
12798 else if (last1->dw_loc_opc != DW_OP_deref_size
12799 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12801 add_loc_descr (&op1, int_loc_descriptor (mask));
12802 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12805 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12807 HOST_WIDE_INT bias = 1;
12808 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12809 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12810 if (CONST_INT_P (XEXP (rtl, 1)))
12811 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12812 + INTVAL (XEXP (rtl, 1)));
12813 else
12814 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12815 bias, 0));
12817 return compare_loc_descriptor (op, op0, op1);
12820 /* Return location descriptor for {U,S}{MIN,MAX}. */
12822 static dw_loc_descr_ref
12823 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12824 machine_mode mem_mode)
12826 enum dwarf_location_atom op;
12827 dw_loc_descr_ref op0, op1, ret;
12828 dw_loc_descr_ref bra_node, drop_node;
12830 if (dwarf_strict
12831 && (!SCALAR_INT_MODE_P (mode)
12832 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12833 return NULL;
12835 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12836 VAR_INIT_STATUS_INITIALIZED);
12837 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12838 VAR_INIT_STATUS_INITIALIZED);
12840 if (op0 == NULL || op1 == NULL)
12841 return NULL;
12843 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12844 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12845 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12846 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12848 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12850 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12851 add_loc_descr (&op0, int_loc_descriptor (mask));
12852 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12853 add_loc_descr (&op1, int_loc_descriptor (mask));
12854 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12856 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12858 HOST_WIDE_INT bias = 1;
12859 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12860 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12861 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12864 else if (!SCALAR_INT_MODE_P (mode)
12865 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12867 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12868 add_loc_descr (&op0, int_loc_descriptor (shift));
12869 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12870 add_loc_descr (&op1, int_loc_descriptor (shift));
12871 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12873 else if (SCALAR_INT_MODE_P (mode)
12874 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12876 dw_die_ref type_die = base_type_for_mode (mode, 0);
12877 dw_loc_descr_ref cvt;
12878 if (type_die == NULL)
12879 return NULL;
12880 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12881 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12882 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12883 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12884 add_loc_descr (&op0, cvt);
12885 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12886 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12887 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12888 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12889 add_loc_descr (&op1, cvt);
12892 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12893 op = DW_OP_lt;
12894 else
12895 op = DW_OP_gt;
12896 ret = op0;
12897 add_loc_descr (&ret, op1);
12898 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12899 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12900 add_loc_descr (&ret, bra_node);
12901 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12902 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12903 add_loc_descr (&ret, drop_node);
12904 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12905 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12906 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12907 && SCALAR_INT_MODE_P (mode)
12908 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12909 ret = convert_descriptor_to_mode (mode, ret);
12910 return ret;
12913 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12914 but after converting arguments to type_die, afterwards
12915 convert back to unsigned. */
12917 static dw_loc_descr_ref
12918 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12919 machine_mode mode, machine_mode mem_mode)
12921 dw_loc_descr_ref cvt, op0, op1;
12923 if (type_die == NULL)
12924 return NULL;
12925 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12926 VAR_INIT_STATUS_INITIALIZED);
12927 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12928 VAR_INIT_STATUS_INITIALIZED);
12929 if (op0 == NULL || op1 == NULL)
12930 return NULL;
12931 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12932 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12933 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12934 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12935 add_loc_descr (&op0, cvt);
12936 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12937 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12938 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12939 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12940 add_loc_descr (&op1, cvt);
12941 add_loc_descr (&op0, op1);
12942 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12943 return convert_descriptor_to_mode (mode, op0);
12946 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12947 const0 is DW_OP_lit0 or corresponding typed constant,
12948 const1 is DW_OP_lit1 or corresponding typed constant
12949 and constMSB is constant with just the MSB bit set
12950 for the mode):
12951 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12952 L1: const0 DW_OP_swap
12953 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12954 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12955 L3: DW_OP_drop
12956 L4: DW_OP_nop
12958 CTZ is similar:
12959 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12960 L1: const0 DW_OP_swap
12961 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12962 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12963 L3: DW_OP_drop
12964 L4: DW_OP_nop
12966 FFS is similar:
12967 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12968 L1: const1 DW_OP_swap
12969 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12970 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12971 L3: DW_OP_drop
12972 L4: DW_OP_nop */
12974 static dw_loc_descr_ref
12975 clz_loc_descriptor (rtx rtl, machine_mode mode,
12976 machine_mode mem_mode)
12978 dw_loc_descr_ref op0, ret, tmp;
12979 HOST_WIDE_INT valv;
12980 dw_loc_descr_ref l1jump, l1label;
12981 dw_loc_descr_ref l2jump, l2label;
12982 dw_loc_descr_ref l3jump, l3label;
12983 dw_loc_descr_ref l4jump, l4label;
12984 rtx msb;
12986 if (!SCALAR_INT_MODE_P (mode)
12987 || GET_MODE (XEXP (rtl, 0)) != mode)
12988 return NULL;
12990 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12991 VAR_INIT_STATUS_INITIALIZED);
12992 if (op0 == NULL)
12993 return NULL;
12994 ret = op0;
12995 if (GET_CODE (rtl) == CLZ)
12997 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12998 valv = GET_MODE_BITSIZE (mode);
13000 else if (GET_CODE (rtl) == FFS)
13001 valv = 0;
13002 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
13003 valv = GET_MODE_BITSIZE (mode);
13004 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13005 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
13006 add_loc_descr (&ret, l1jump);
13007 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13008 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
13009 VAR_INIT_STATUS_INITIALIZED);
13010 if (tmp == NULL)
13011 return NULL;
13012 add_loc_descr (&ret, tmp);
13013 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
13014 add_loc_descr (&ret, l4jump);
13015 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
13016 ? const1_rtx : const0_rtx,
13017 mode, mem_mode,
13018 VAR_INIT_STATUS_INITIALIZED);
13019 if (l1label == NULL)
13020 return NULL;
13021 add_loc_descr (&ret, l1label);
13022 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13023 l2label = new_loc_descr (DW_OP_dup, 0, 0);
13024 add_loc_descr (&ret, l2label);
13025 if (GET_CODE (rtl) != CLZ)
13026 msb = const1_rtx;
13027 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
13028 msb = GEN_INT (HOST_WIDE_INT_1U
13029 << (GET_MODE_BITSIZE (mode) - 1));
13030 else
13031 msb = immed_wide_int_const
13032 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
13033 GET_MODE_PRECISION (mode)), mode);
13034 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
13035 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13036 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
13037 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
13038 else
13039 tmp = mem_loc_descriptor (msb, mode, mem_mode,
13040 VAR_INIT_STATUS_INITIALIZED);
13041 if (tmp == NULL)
13042 return NULL;
13043 add_loc_descr (&ret, tmp);
13044 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13045 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13046 add_loc_descr (&ret, l3jump);
13047 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13048 VAR_INIT_STATUS_INITIALIZED);
13049 if (tmp == NULL)
13050 return NULL;
13051 add_loc_descr (&ret, tmp);
13052 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13053 ? DW_OP_shl : DW_OP_shr, 0, 0));
13054 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13055 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13056 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13057 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13058 add_loc_descr (&ret, l2jump);
13059 l3label = new_loc_descr (DW_OP_drop, 0, 0);
13060 add_loc_descr (&ret, l3label);
13061 l4label = new_loc_descr (DW_OP_nop, 0, 0);
13062 add_loc_descr (&ret, l4label);
13063 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13064 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13065 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13066 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13067 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13068 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13069 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13070 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13071 return ret;
13074 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13075 const1 is DW_OP_lit1 or corresponding typed constant):
13076 const0 DW_OP_swap
13077 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13078 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13079 L2: DW_OP_drop
13081 PARITY is similar:
13082 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13083 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13084 L2: DW_OP_drop */
13086 static dw_loc_descr_ref
13087 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13088 machine_mode mem_mode)
13090 dw_loc_descr_ref op0, ret, tmp;
13091 dw_loc_descr_ref l1jump, l1label;
13092 dw_loc_descr_ref l2jump, l2label;
13094 if (!SCALAR_INT_MODE_P (mode)
13095 || GET_MODE (XEXP (rtl, 0)) != mode)
13096 return NULL;
13098 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13099 VAR_INIT_STATUS_INITIALIZED);
13100 if (op0 == NULL)
13101 return NULL;
13102 ret = op0;
13103 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13104 VAR_INIT_STATUS_INITIALIZED);
13105 if (tmp == NULL)
13106 return NULL;
13107 add_loc_descr (&ret, tmp);
13108 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13109 l1label = new_loc_descr (DW_OP_dup, 0, 0);
13110 add_loc_descr (&ret, l1label);
13111 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13112 add_loc_descr (&ret, l2jump);
13113 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13114 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13115 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13116 VAR_INIT_STATUS_INITIALIZED);
13117 if (tmp == NULL)
13118 return NULL;
13119 add_loc_descr (&ret, tmp);
13120 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13121 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13122 ? DW_OP_plus : DW_OP_xor, 0, 0));
13123 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13124 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13125 VAR_INIT_STATUS_INITIALIZED);
13126 add_loc_descr (&ret, tmp);
13127 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13128 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13129 add_loc_descr (&ret, l1jump);
13130 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13131 add_loc_descr (&ret, l2label);
13132 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13133 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13134 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13135 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13136 return ret;
13139 /* BSWAP (constS is initial shift count, either 56 or 24):
13140 constS const0
13141 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13142 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13143 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13144 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13145 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
13147 static dw_loc_descr_ref
13148 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13149 machine_mode mem_mode)
13151 dw_loc_descr_ref op0, ret, tmp;
13152 dw_loc_descr_ref l1jump, l1label;
13153 dw_loc_descr_ref l2jump, l2label;
13155 if (!SCALAR_INT_MODE_P (mode)
13156 || BITS_PER_UNIT != 8
13157 || (GET_MODE_BITSIZE (mode) != 32
13158 && GET_MODE_BITSIZE (mode) != 64))
13159 return NULL;
13161 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13162 VAR_INIT_STATUS_INITIALIZED);
13163 if (op0 == NULL)
13164 return NULL;
13166 ret = op0;
13167 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13168 mode, mem_mode,
13169 VAR_INIT_STATUS_INITIALIZED);
13170 if (tmp == NULL)
13171 return NULL;
13172 add_loc_descr (&ret, tmp);
13173 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13174 VAR_INIT_STATUS_INITIALIZED);
13175 if (tmp == NULL)
13176 return NULL;
13177 add_loc_descr (&ret, tmp);
13178 l1label = new_loc_descr (DW_OP_pick, 2, 0);
13179 add_loc_descr (&ret, l1label);
13180 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13181 mode, mem_mode,
13182 VAR_INIT_STATUS_INITIALIZED);
13183 add_loc_descr (&ret, tmp);
13184 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13185 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13186 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13187 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13188 VAR_INIT_STATUS_INITIALIZED);
13189 if (tmp == NULL)
13190 return NULL;
13191 add_loc_descr (&ret, tmp);
13192 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13193 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13194 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13195 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13196 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13197 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13198 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13199 VAR_INIT_STATUS_INITIALIZED);
13200 add_loc_descr (&ret, tmp);
13201 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13202 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13203 add_loc_descr (&ret, l2jump);
13204 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13205 VAR_INIT_STATUS_INITIALIZED);
13206 add_loc_descr (&ret, tmp);
13207 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13208 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13209 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13210 add_loc_descr (&ret, l1jump);
13211 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13212 add_loc_descr (&ret, l2label);
13213 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13214 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13215 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13216 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13217 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13218 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13219 return ret;
13222 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13223 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13224 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13225 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13227 ROTATERT is similar:
13228 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13229 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13230 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
13232 static dw_loc_descr_ref
13233 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13234 machine_mode mem_mode)
13236 rtx rtlop1 = XEXP (rtl, 1);
13237 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13238 int i;
13240 if (!SCALAR_INT_MODE_P (mode))
13241 return NULL;
13243 if (GET_MODE (rtlop1) != VOIDmode
13244 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13245 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13246 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13247 VAR_INIT_STATUS_INITIALIZED);
13248 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13249 VAR_INIT_STATUS_INITIALIZED);
13250 if (op0 == NULL || op1 == NULL)
13251 return NULL;
13252 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13253 for (i = 0; i < 2; i++)
13255 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13256 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13257 mode, mem_mode,
13258 VAR_INIT_STATUS_INITIALIZED);
13259 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13260 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13261 ? DW_OP_const4u
13262 : HOST_BITS_PER_WIDE_INT == 64
13263 ? DW_OP_const8u : DW_OP_constu,
13264 GET_MODE_MASK (mode), 0);
13265 else
13266 mask[i] = NULL;
13267 if (mask[i] == NULL)
13268 return NULL;
13269 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13271 ret = op0;
13272 add_loc_descr (&ret, op1);
13273 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13274 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13275 if (GET_CODE (rtl) == ROTATERT)
13277 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13278 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13279 GET_MODE_BITSIZE (mode), 0));
13281 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13282 if (mask[0] != NULL)
13283 add_loc_descr (&ret, mask[0]);
13284 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13285 if (mask[1] != NULL)
13287 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13288 add_loc_descr (&ret, mask[1]);
13289 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13291 if (GET_CODE (rtl) == ROTATE)
13293 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13294 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13295 GET_MODE_BITSIZE (mode), 0));
13297 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13298 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13299 return ret;
13302 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
13303 for DEBUG_PARAMETER_REF RTL. */
13305 static dw_loc_descr_ref
13306 parameter_ref_descriptor (rtx rtl)
13308 dw_loc_descr_ref ret;
13309 dw_die_ref ref;
13311 if (dwarf_strict)
13312 return NULL;
13313 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13314 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13315 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13316 if (ref)
13318 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13319 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13320 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13322 else
13324 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13325 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13327 return ret;
13330 /* The following routine converts the RTL for a variable or parameter
13331 (resident in memory) into an equivalent Dwarf representation of a
13332 mechanism for getting the address of that same variable onto the top of a
13333 hypothetical "address evaluation" stack.
13335 When creating memory location descriptors, we are effectively transforming
13336 the RTL for a memory-resident object into its Dwarf postfix expression
13337 equivalent. This routine recursively descends an RTL tree, turning
13338 it into Dwarf postfix code as it goes.
13340 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13342 MEM_MODE is the mode of the memory reference, needed to handle some
13343 autoincrement addressing modes.
13345 Return 0 if we can't represent the location. */
13347 dw_loc_descr_ref
13348 mem_loc_descriptor (rtx rtl, machine_mode mode,
13349 machine_mode mem_mode,
13350 enum var_init_status initialized)
13352 dw_loc_descr_ref mem_loc_result = NULL;
13353 enum dwarf_location_atom op;
13354 dw_loc_descr_ref op0, op1;
13355 rtx inner = NULL_RTX;
13357 if (mode == VOIDmode)
13358 mode = GET_MODE (rtl);
13360 /* Note that for a dynamically sized array, the location we will generate a
13361 description of here will be the lowest numbered location which is
13362 actually within the array. That's *not* necessarily the same as the
13363 zeroth element of the array. */
13365 rtl = targetm.delegitimize_address (rtl);
13367 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13368 return NULL;
13370 switch (GET_CODE (rtl))
13372 case POST_INC:
13373 case POST_DEC:
13374 case POST_MODIFY:
13375 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13377 case SUBREG:
13378 /* The case of a subreg may arise when we have a local (register)
13379 variable or a formal (register) parameter which doesn't quite fill
13380 up an entire register. For now, just assume that it is
13381 legitimate to make the Dwarf info refer to the whole register which
13382 contains the given subreg. */
13383 if (!subreg_lowpart_p (rtl))
13384 break;
13385 inner = SUBREG_REG (rtl);
13386 /* FALLTHRU */
13387 case TRUNCATE:
13388 if (inner == NULL_RTX)
13389 inner = XEXP (rtl, 0);
13390 if (SCALAR_INT_MODE_P (mode)
13391 && SCALAR_INT_MODE_P (GET_MODE (inner))
13392 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13393 #ifdef POINTERS_EXTEND_UNSIGNED
13394 || (mode == Pmode && mem_mode != VOIDmode)
13395 #endif
13397 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13399 mem_loc_result = mem_loc_descriptor (inner,
13400 GET_MODE (inner),
13401 mem_mode, initialized);
13402 break;
13404 if (dwarf_strict)
13405 break;
13406 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13407 break;
13408 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13409 && (!SCALAR_INT_MODE_P (mode)
13410 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
13411 break;
13412 else
13414 dw_die_ref type_die;
13415 dw_loc_descr_ref cvt;
13417 mem_loc_result = mem_loc_descriptor (inner,
13418 GET_MODE (inner),
13419 mem_mode, initialized);
13420 if (mem_loc_result == NULL)
13421 break;
13422 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13423 if (type_die == NULL)
13425 mem_loc_result = NULL;
13426 break;
13428 if (GET_MODE_SIZE (mode)
13429 != GET_MODE_SIZE (GET_MODE (inner)))
13430 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13431 else
13432 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13433 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13434 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13435 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13436 add_loc_descr (&mem_loc_result, cvt);
13437 if (SCALAR_INT_MODE_P (mode)
13438 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13440 /* Convert it to untyped afterwards. */
13441 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13442 add_loc_descr (&mem_loc_result, cvt);
13445 break;
13447 case REG:
13448 if (! SCALAR_INT_MODE_P (mode)
13449 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13450 && rtl != arg_pointer_rtx
13451 && rtl != frame_pointer_rtx
13452 #ifdef POINTERS_EXTEND_UNSIGNED
13453 && (mode != Pmode || mem_mode == VOIDmode)
13454 #endif
13457 dw_die_ref type_die;
13458 unsigned int dbx_regnum;
13460 if (dwarf_strict)
13461 break;
13462 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13463 break;
13464 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13465 if (type_die == NULL)
13466 break;
13468 dbx_regnum = dbx_reg_number (rtl);
13469 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13470 break;
13471 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13472 dbx_regnum, 0);
13473 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13474 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13475 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13476 break;
13478 /* Whenever a register number forms a part of the description of the
13479 method for calculating the (dynamic) address of a memory resident
13480 object, DWARF rules require the register number be referred to as
13481 a "base register". This distinction is not based in any way upon
13482 what category of register the hardware believes the given register
13483 belongs to. This is strictly DWARF terminology we're dealing with
13484 here. Note that in cases where the location of a memory-resident
13485 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13486 OP_CONST (0)) the actual DWARF location descriptor that we generate
13487 may just be OP_BASEREG (basereg). This may look deceptively like
13488 the object in question was allocated to a register (rather than in
13489 memory) so DWARF consumers need to be aware of the subtle
13490 distinction between OP_REG and OP_BASEREG. */
13491 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13492 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13493 else if (stack_realign_drap
13494 && crtl->drap_reg
13495 && crtl->args.internal_arg_pointer == rtl
13496 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13498 /* If RTL is internal_arg_pointer, which has been optimized
13499 out, use DRAP instead. */
13500 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13501 VAR_INIT_STATUS_INITIALIZED);
13503 break;
13505 case SIGN_EXTEND:
13506 case ZERO_EXTEND:
13507 if (!SCALAR_INT_MODE_P (mode))
13508 break;
13509 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13510 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13511 if (op0 == 0)
13512 break;
13513 else if (GET_CODE (rtl) == ZERO_EXTEND
13514 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13515 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13516 < HOST_BITS_PER_WIDE_INT
13517 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13518 to expand zero extend as two shifts instead of
13519 masking. */
13520 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13522 machine_mode imode = GET_MODE (XEXP (rtl, 0));
13523 mem_loc_result = op0;
13524 add_loc_descr (&mem_loc_result,
13525 int_loc_descriptor (GET_MODE_MASK (imode)));
13526 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13528 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13530 int shift = DWARF2_ADDR_SIZE
13531 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13532 shift *= BITS_PER_UNIT;
13533 if (GET_CODE (rtl) == SIGN_EXTEND)
13534 op = DW_OP_shra;
13535 else
13536 op = DW_OP_shr;
13537 mem_loc_result = op0;
13538 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13539 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13540 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13541 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13543 else if (!dwarf_strict)
13545 dw_die_ref type_die1, type_die2;
13546 dw_loc_descr_ref cvt;
13548 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13549 GET_CODE (rtl) == ZERO_EXTEND);
13550 if (type_die1 == NULL)
13551 break;
13552 type_die2 = base_type_for_mode (mode, 1);
13553 if (type_die2 == NULL)
13554 break;
13555 mem_loc_result = op0;
13556 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13557 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13558 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13559 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13560 add_loc_descr (&mem_loc_result, cvt);
13561 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13562 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13563 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13564 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13565 add_loc_descr (&mem_loc_result, cvt);
13567 break;
13569 case MEM:
13571 rtx new_rtl = avoid_constant_pool_reference (rtl);
13572 if (new_rtl != rtl)
13574 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13575 initialized);
13576 if (mem_loc_result != NULL)
13577 return mem_loc_result;
13580 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13581 get_address_mode (rtl), mode,
13582 VAR_INIT_STATUS_INITIALIZED);
13583 if (mem_loc_result == NULL)
13584 mem_loc_result = tls_mem_loc_descriptor (rtl);
13585 if (mem_loc_result != NULL)
13587 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13588 || !SCALAR_INT_MODE_P(mode))
13590 dw_die_ref type_die;
13591 dw_loc_descr_ref deref;
13593 if (dwarf_strict)
13594 return NULL;
13595 type_die
13596 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13597 if (type_die == NULL)
13598 return NULL;
13599 deref = new_loc_descr (DW_OP_GNU_deref_type,
13600 GET_MODE_SIZE (mode), 0);
13601 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13602 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13603 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13604 add_loc_descr (&mem_loc_result, deref);
13606 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13607 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13608 else
13609 add_loc_descr (&mem_loc_result,
13610 new_loc_descr (DW_OP_deref_size,
13611 GET_MODE_SIZE (mode), 0));
13613 break;
13615 case LO_SUM:
13616 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13618 case LABEL_REF:
13619 /* Some ports can transform a symbol ref into a label ref, because
13620 the symbol ref is too far away and has to be dumped into a constant
13621 pool. */
13622 case CONST:
13623 case SYMBOL_REF:
13624 if (!SCALAR_INT_MODE_P (mode)
13625 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13626 #ifdef POINTERS_EXTEND_UNSIGNED
13627 && (mode != Pmode || mem_mode == VOIDmode)
13628 #endif
13630 break;
13631 if (GET_CODE (rtl) == SYMBOL_REF
13632 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13634 dw_loc_descr_ref temp;
13636 /* If this is not defined, we have no way to emit the data. */
13637 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13638 break;
13640 temp = new_addr_loc_descr (rtl, dtprel_true);
13642 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13643 add_loc_descr (&mem_loc_result, temp);
13645 break;
13648 if (!const_ok_for_output (rtl))
13650 if (GET_CODE (rtl) == CONST)
13651 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13652 initialized);
13653 break;
13656 symref:
13657 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13658 vec_safe_push (used_rtx_array, rtl);
13659 break;
13661 case CONCAT:
13662 case CONCATN:
13663 case VAR_LOCATION:
13664 case DEBUG_IMPLICIT_PTR:
13665 expansion_failed (NULL_TREE, rtl,
13666 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13667 return 0;
13669 case ENTRY_VALUE:
13670 if (dwarf_strict)
13671 return NULL;
13672 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13674 if (!SCALAR_INT_MODE_P (mode)
13675 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13676 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13677 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13678 else
13680 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13681 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13682 return NULL;
13683 op0 = one_reg_loc_descriptor (dbx_regnum,
13684 VAR_INIT_STATUS_INITIALIZED);
13687 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13688 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13690 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13691 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13692 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13693 return NULL;
13695 else
13696 gcc_unreachable ();
13697 if (op0 == NULL)
13698 return NULL;
13699 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13700 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13701 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13702 break;
13704 case DEBUG_PARAMETER_REF:
13705 mem_loc_result = parameter_ref_descriptor (rtl);
13706 break;
13708 case PRE_MODIFY:
13709 /* Extract the PLUS expression nested inside and fall into
13710 PLUS code below. */
13711 rtl = XEXP (rtl, 1);
13712 goto plus;
13714 case PRE_INC:
13715 case PRE_DEC:
13716 /* Turn these into a PLUS expression and fall into the PLUS code
13717 below. */
13718 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13719 gen_int_mode (GET_CODE (rtl) == PRE_INC
13720 ? GET_MODE_UNIT_SIZE (mem_mode)
13721 : -GET_MODE_UNIT_SIZE (mem_mode),
13722 mode));
13724 /* fall through */
13726 case PLUS:
13727 plus:
13728 if (is_based_loc (rtl)
13729 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13730 || XEXP (rtl, 0) == arg_pointer_rtx
13731 || XEXP (rtl, 0) == frame_pointer_rtx)
13732 && SCALAR_INT_MODE_P (mode))
13733 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13734 INTVAL (XEXP (rtl, 1)),
13735 VAR_INIT_STATUS_INITIALIZED);
13736 else
13738 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13739 VAR_INIT_STATUS_INITIALIZED);
13740 if (mem_loc_result == 0)
13741 break;
13743 if (CONST_INT_P (XEXP (rtl, 1))
13744 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13745 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13746 else
13748 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13749 VAR_INIT_STATUS_INITIALIZED);
13750 if (op1 == 0)
13751 return NULL;
13752 add_loc_descr (&mem_loc_result, op1);
13753 add_loc_descr (&mem_loc_result,
13754 new_loc_descr (DW_OP_plus, 0, 0));
13757 break;
13759 /* If a pseudo-reg is optimized away, it is possible for it to
13760 be replaced with a MEM containing a multiply or shift. */
13761 case MINUS:
13762 op = DW_OP_minus;
13763 goto do_binop;
13765 case MULT:
13766 op = DW_OP_mul;
13767 goto do_binop;
13769 case DIV:
13770 if (!dwarf_strict
13771 && SCALAR_INT_MODE_P (mode)
13772 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13774 mem_loc_result = typed_binop (DW_OP_div, rtl,
13775 base_type_for_mode (mode, 0),
13776 mode, mem_mode);
13777 break;
13779 op = DW_OP_div;
13780 goto do_binop;
13782 case UMOD:
13783 op = DW_OP_mod;
13784 goto do_binop;
13786 case ASHIFT:
13787 op = DW_OP_shl;
13788 goto do_shift;
13790 case ASHIFTRT:
13791 op = DW_OP_shra;
13792 goto do_shift;
13794 case LSHIFTRT:
13795 op = DW_OP_shr;
13796 goto do_shift;
13798 do_shift:
13799 if (!SCALAR_INT_MODE_P (mode))
13800 break;
13801 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13802 VAR_INIT_STATUS_INITIALIZED);
13804 rtx rtlop1 = XEXP (rtl, 1);
13805 if (GET_MODE (rtlop1) != VOIDmode
13806 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13807 < GET_MODE_BITSIZE (mode))
13808 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13809 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13810 VAR_INIT_STATUS_INITIALIZED);
13813 if (op0 == 0 || op1 == 0)
13814 break;
13816 mem_loc_result = op0;
13817 add_loc_descr (&mem_loc_result, op1);
13818 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13819 break;
13821 case AND:
13822 op = DW_OP_and;
13823 goto do_binop;
13825 case IOR:
13826 op = DW_OP_or;
13827 goto do_binop;
13829 case XOR:
13830 op = DW_OP_xor;
13831 goto do_binop;
13833 do_binop:
13834 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13835 VAR_INIT_STATUS_INITIALIZED);
13836 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13837 VAR_INIT_STATUS_INITIALIZED);
13839 if (op0 == 0 || op1 == 0)
13840 break;
13842 mem_loc_result = op0;
13843 add_loc_descr (&mem_loc_result, op1);
13844 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13845 break;
13847 case MOD:
13848 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13850 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13851 base_type_for_mode (mode, 0),
13852 mode, mem_mode);
13853 break;
13856 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13857 VAR_INIT_STATUS_INITIALIZED);
13858 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13859 VAR_INIT_STATUS_INITIALIZED);
13861 if (op0 == 0 || op1 == 0)
13862 break;
13864 mem_loc_result = op0;
13865 add_loc_descr (&mem_loc_result, op1);
13866 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13867 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13868 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13869 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13870 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13871 break;
13873 case UDIV:
13874 if (!dwarf_strict && SCALAR_INT_MODE_P (mode))
13876 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13878 op = DW_OP_div;
13879 goto do_binop;
13881 mem_loc_result = typed_binop (DW_OP_div, rtl,
13882 base_type_for_mode (mode, 1),
13883 mode, mem_mode);
13885 break;
13887 case NOT:
13888 op = DW_OP_not;
13889 goto do_unop;
13891 case ABS:
13892 op = DW_OP_abs;
13893 goto do_unop;
13895 case NEG:
13896 op = DW_OP_neg;
13897 goto do_unop;
13899 do_unop:
13900 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13901 VAR_INIT_STATUS_INITIALIZED);
13903 if (op0 == 0)
13904 break;
13906 mem_loc_result = op0;
13907 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13908 break;
13910 case CONST_INT:
13911 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13912 #ifdef POINTERS_EXTEND_UNSIGNED
13913 || (mode == Pmode
13914 && mem_mode != VOIDmode
13915 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13916 #endif
13919 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13920 break;
13922 if (!dwarf_strict
13923 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13924 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13926 dw_die_ref type_die = base_type_for_mode (mode, 1);
13927 machine_mode amode;
13928 if (type_die == NULL)
13929 return NULL;
13930 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13931 MODE_INT, 0);
13932 if (INTVAL (rtl) >= 0
13933 && amode != BLKmode
13934 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13935 /* const DW_OP_GNU_convert <XXX> vs.
13936 DW_OP_GNU_const_type <XXX, 1, const>. */
13937 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13938 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13940 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13941 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13942 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13943 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13944 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13945 add_loc_descr (&mem_loc_result, op0);
13946 return mem_loc_result;
13948 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13949 INTVAL (rtl));
13950 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13951 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13952 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13953 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13954 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13955 else
13957 mem_loc_result->dw_loc_oprnd2.val_class
13958 = dw_val_class_const_double;
13959 mem_loc_result->dw_loc_oprnd2.v.val_double
13960 = double_int::from_shwi (INTVAL (rtl));
13963 break;
13965 case CONST_DOUBLE:
13966 if (!dwarf_strict)
13968 dw_die_ref type_die;
13970 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13971 CONST_DOUBLE rtx could represent either a large integer
13972 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13973 the value is always a floating point constant.
13975 When it is an integer, a CONST_DOUBLE is used whenever
13976 the constant requires 2 HWIs to be adequately represented.
13977 We output CONST_DOUBLEs as blocks. */
13978 if (mode == VOIDmode
13979 || (GET_MODE (rtl) == VOIDmode
13980 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13981 break;
13982 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13983 if (type_die == NULL)
13984 return NULL;
13985 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13986 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13987 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13988 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13989 #if TARGET_SUPPORTS_WIDE_INT == 0
13990 if (!SCALAR_FLOAT_MODE_P (mode))
13992 mem_loc_result->dw_loc_oprnd2.val_class
13993 = dw_val_class_const_double;
13994 mem_loc_result->dw_loc_oprnd2.v.val_double
13995 = rtx_to_double_int (rtl);
13997 else
13998 #endif
14000 unsigned int length = GET_MODE_SIZE (mode);
14001 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14003 insert_float (rtl, array);
14004 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14005 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14006 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14007 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14010 break;
14012 case CONST_WIDE_INT:
14013 if (!dwarf_strict)
14015 dw_die_ref type_die;
14017 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14018 if (type_die == NULL)
14019 return NULL;
14020 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
14021 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14022 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14023 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14024 mem_loc_result->dw_loc_oprnd2.val_class
14025 = dw_val_class_wide_int;
14026 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14027 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14029 break;
14031 case EQ:
14032 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
14033 break;
14035 case GE:
14036 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14037 break;
14039 case GT:
14040 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14041 break;
14043 case LE:
14044 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14045 break;
14047 case LT:
14048 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14049 break;
14051 case NE:
14052 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14053 break;
14055 case GEU:
14056 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14057 break;
14059 case GTU:
14060 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14061 break;
14063 case LEU:
14064 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14065 break;
14067 case LTU:
14068 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14069 break;
14071 case UMIN:
14072 case UMAX:
14073 if (!SCALAR_INT_MODE_P (mode))
14074 break;
14075 /* FALLTHRU */
14076 case SMIN:
14077 case SMAX:
14078 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14079 break;
14081 case ZERO_EXTRACT:
14082 case SIGN_EXTRACT:
14083 if (CONST_INT_P (XEXP (rtl, 1))
14084 && CONST_INT_P (XEXP (rtl, 2))
14085 && ((unsigned) INTVAL (XEXP (rtl, 1))
14086 + (unsigned) INTVAL (XEXP (rtl, 2))
14087 <= GET_MODE_BITSIZE (mode))
14088 && SCALAR_INT_MODE_P (mode)
14089 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14090 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14092 int shift, size;
14093 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14094 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14095 if (op0 == 0)
14096 break;
14097 if (GET_CODE (rtl) == SIGN_EXTRACT)
14098 op = DW_OP_shra;
14099 else
14100 op = DW_OP_shr;
14101 mem_loc_result = op0;
14102 size = INTVAL (XEXP (rtl, 1));
14103 shift = INTVAL (XEXP (rtl, 2));
14104 if (BITS_BIG_ENDIAN)
14105 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14106 - shift - size;
14107 if (shift + size != (int) DWARF2_ADDR_SIZE)
14109 add_loc_descr (&mem_loc_result,
14110 int_loc_descriptor (DWARF2_ADDR_SIZE
14111 - shift - size));
14112 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14114 if (size != (int) DWARF2_ADDR_SIZE)
14116 add_loc_descr (&mem_loc_result,
14117 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14118 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14121 break;
14123 case IF_THEN_ELSE:
14125 dw_loc_descr_ref op2, bra_node, drop_node;
14126 op0 = mem_loc_descriptor (XEXP (rtl, 0),
14127 GET_MODE (XEXP (rtl, 0)) == VOIDmode
14128 ? word_mode : GET_MODE (XEXP (rtl, 0)),
14129 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14130 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14131 VAR_INIT_STATUS_INITIALIZED);
14132 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14133 VAR_INIT_STATUS_INITIALIZED);
14134 if (op0 == NULL || op1 == NULL || op2 == NULL)
14135 break;
14137 mem_loc_result = op1;
14138 add_loc_descr (&mem_loc_result, op2);
14139 add_loc_descr (&mem_loc_result, op0);
14140 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14141 add_loc_descr (&mem_loc_result, bra_node);
14142 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14143 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14144 add_loc_descr (&mem_loc_result, drop_node);
14145 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14146 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14148 break;
14150 case FLOAT_EXTEND:
14151 case FLOAT_TRUNCATE:
14152 case FLOAT:
14153 case UNSIGNED_FLOAT:
14154 case FIX:
14155 case UNSIGNED_FIX:
14156 if (!dwarf_strict)
14158 dw_die_ref type_die;
14159 dw_loc_descr_ref cvt;
14161 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14162 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14163 if (op0 == NULL)
14164 break;
14165 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
14166 && (GET_CODE (rtl) == FLOAT
14167 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14168 <= DWARF2_ADDR_SIZE))
14170 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14171 GET_CODE (rtl) == UNSIGNED_FLOAT);
14172 if (type_die == NULL)
14173 break;
14174 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14175 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14176 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14177 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14178 add_loc_descr (&op0, cvt);
14180 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14181 if (type_die == NULL)
14182 break;
14183 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14184 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14185 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14186 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14187 add_loc_descr (&op0, cvt);
14188 if (SCALAR_INT_MODE_P (mode)
14189 && (GET_CODE (rtl) == FIX
14190 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14192 op0 = convert_descriptor_to_mode (mode, op0);
14193 if (op0 == NULL)
14194 break;
14196 mem_loc_result = op0;
14198 break;
14200 case CLZ:
14201 case CTZ:
14202 case FFS:
14203 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14204 break;
14206 case POPCOUNT:
14207 case PARITY:
14208 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14209 break;
14211 case BSWAP:
14212 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14213 break;
14215 case ROTATE:
14216 case ROTATERT:
14217 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14218 break;
14220 case COMPARE:
14221 /* In theory, we could implement the above. */
14222 /* DWARF cannot represent the unsigned compare operations
14223 natively. */
14224 case SS_MULT:
14225 case US_MULT:
14226 case SS_DIV:
14227 case US_DIV:
14228 case SS_PLUS:
14229 case US_PLUS:
14230 case SS_MINUS:
14231 case US_MINUS:
14232 case SS_NEG:
14233 case US_NEG:
14234 case SS_ABS:
14235 case SS_ASHIFT:
14236 case US_ASHIFT:
14237 case SS_TRUNCATE:
14238 case US_TRUNCATE:
14239 case UNORDERED:
14240 case ORDERED:
14241 case UNEQ:
14242 case UNGE:
14243 case UNGT:
14244 case UNLE:
14245 case UNLT:
14246 case LTGT:
14247 case FRACT_CONVERT:
14248 case UNSIGNED_FRACT_CONVERT:
14249 case SAT_FRACT:
14250 case UNSIGNED_SAT_FRACT:
14251 case SQRT:
14252 case ASM_OPERANDS:
14253 case VEC_MERGE:
14254 case VEC_SELECT:
14255 case VEC_CONCAT:
14256 case VEC_DUPLICATE:
14257 case UNSPEC:
14258 case HIGH:
14259 case FMA:
14260 case STRICT_LOW_PART:
14261 case CONST_VECTOR:
14262 case CONST_FIXED:
14263 case CLRSB:
14264 case CLOBBER:
14265 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14266 can't express it in the debug info. This can happen e.g. with some
14267 TLS UNSPECs. */
14268 break;
14270 case CONST_STRING:
14271 resolve_one_addr (&rtl);
14272 goto symref;
14274 default:
14275 if (flag_checking)
14277 print_rtl (stderr, rtl);
14278 gcc_unreachable ();
14280 break;
14283 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14284 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14286 return mem_loc_result;
14289 /* Return a descriptor that describes the concatenation of two locations.
14290 This is typically a complex variable. */
14292 static dw_loc_descr_ref
14293 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14295 dw_loc_descr_ref cc_loc_result = NULL;
14296 dw_loc_descr_ref x0_ref
14297 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14298 dw_loc_descr_ref x1_ref
14299 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14301 if (x0_ref == 0 || x1_ref == 0)
14302 return 0;
14304 cc_loc_result = x0_ref;
14305 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14307 add_loc_descr (&cc_loc_result, x1_ref);
14308 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14310 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14311 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14313 return cc_loc_result;
14316 /* Return a descriptor that describes the concatenation of N
14317 locations. */
14319 static dw_loc_descr_ref
14320 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14322 unsigned int i;
14323 dw_loc_descr_ref cc_loc_result = NULL;
14324 unsigned int n = XVECLEN (concatn, 0);
14326 for (i = 0; i < n; ++i)
14328 dw_loc_descr_ref ref;
14329 rtx x = XVECEXP (concatn, 0, i);
14331 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14332 if (ref == NULL)
14333 return NULL;
14335 add_loc_descr (&cc_loc_result, ref);
14336 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14339 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14340 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14342 return cc_loc_result;
14345 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14346 for DEBUG_IMPLICIT_PTR RTL. */
14348 static dw_loc_descr_ref
14349 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14351 dw_loc_descr_ref ret;
14352 dw_die_ref ref;
14354 if (dwarf_strict)
14355 return NULL;
14356 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14357 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14358 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14359 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14360 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14361 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14362 if (ref)
14364 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14365 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14366 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14368 else
14370 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14371 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14373 return ret;
14376 /* Output a proper Dwarf location descriptor for a variable or parameter
14377 which is either allocated in a register or in a memory location. For a
14378 register, we just generate an OP_REG and the register number. For a
14379 memory location we provide a Dwarf postfix expression describing how to
14380 generate the (dynamic) address of the object onto the address stack.
14382 MODE is mode of the decl if this loc_descriptor is going to be used in
14383 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14384 allowed, VOIDmode otherwise.
14386 If we don't know how to describe it, return 0. */
14388 static dw_loc_descr_ref
14389 loc_descriptor (rtx rtl, machine_mode mode,
14390 enum var_init_status initialized)
14392 dw_loc_descr_ref loc_result = NULL;
14394 switch (GET_CODE (rtl))
14396 case SUBREG:
14397 /* The case of a subreg may arise when we have a local (register)
14398 variable or a formal (register) parameter which doesn't quite fill
14399 up an entire register. For now, just assume that it is
14400 legitimate to make the Dwarf info refer to the whole register which
14401 contains the given subreg. */
14402 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14403 loc_result = loc_descriptor (SUBREG_REG (rtl),
14404 GET_MODE (SUBREG_REG (rtl)), initialized);
14405 else
14406 goto do_default;
14407 break;
14409 case REG:
14410 loc_result = reg_loc_descriptor (rtl, initialized);
14411 break;
14413 case MEM:
14414 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14415 GET_MODE (rtl), initialized);
14416 if (loc_result == NULL)
14417 loc_result = tls_mem_loc_descriptor (rtl);
14418 if (loc_result == NULL)
14420 rtx new_rtl = avoid_constant_pool_reference (rtl);
14421 if (new_rtl != rtl)
14422 loc_result = loc_descriptor (new_rtl, mode, initialized);
14424 break;
14426 case CONCAT:
14427 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14428 initialized);
14429 break;
14431 case CONCATN:
14432 loc_result = concatn_loc_descriptor (rtl, initialized);
14433 break;
14435 case VAR_LOCATION:
14436 /* Single part. */
14437 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14439 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14440 if (GET_CODE (loc) == EXPR_LIST)
14441 loc = XEXP (loc, 0);
14442 loc_result = loc_descriptor (loc, mode, initialized);
14443 break;
14446 rtl = XEXP (rtl, 1);
14447 /* FALLTHRU */
14449 case PARALLEL:
14451 rtvec par_elems = XVEC (rtl, 0);
14452 int num_elem = GET_NUM_ELEM (par_elems);
14453 machine_mode mode;
14454 int i;
14456 /* Create the first one, so we have something to add to. */
14457 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14458 VOIDmode, initialized);
14459 if (loc_result == NULL)
14460 return NULL;
14461 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14462 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14463 for (i = 1; i < num_elem; i++)
14465 dw_loc_descr_ref temp;
14467 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14468 VOIDmode, initialized);
14469 if (temp == NULL)
14470 return NULL;
14471 add_loc_descr (&loc_result, temp);
14472 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14473 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14476 break;
14478 case CONST_INT:
14479 if (mode != VOIDmode && mode != BLKmode)
14480 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14481 INTVAL (rtl));
14482 break;
14484 case CONST_DOUBLE:
14485 if (mode == VOIDmode)
14486 mode = GET_MODE (rtl);
14488 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14490 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14492 /* Note that a CONST_DOUBLE rtx could represent either an integer
14493 or a floating-point constant. A CONST_DOUBLE is used whenever
14494 the constant requires more than one word in order to be
14495 adequately represented. We output CONST_DOUBLEs as blocks. */
14496 loc_result = new_loc_descr (DW_OP_implicit_value,
14497 GET_MODE_SIZE (mode), 0);
14498 #if TARGET_SUPPORTS_WIDE_INT == 0
14499 if (!SCALAR_FLOAT_MODE_P (mode))
14501 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14502 loc_result->dw_loc_oprnd2.v.val_double
14503 = rtx_to_double_int (rtl);
14505 else
14506 #endif
14508 unsigned int length = GET_MODE_SIZE (mode);
14509 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14511 insert_float (rtl, array);
14512 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14513 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14514 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14515 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14518 break;
14520 case CONST_WIDE_INT:
14521 if (mode == VOIDmode)
14522 mode = GET_MODE (rtl);
14524 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14526 loc_result = new_loc_descr (DW_OP_implicit_value,
14527 GET_MODE_SIZE (mode), 0);
14528 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14529 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14530 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14532 break;
14534 case CONST_VECTOR:
14535 if (mode == VOIDmode)
14536 mode = GET_MODE (rtl);
14538 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14540 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14541 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14542 unsigned char *array
14543 = ggc_vec_alloc<unsigned char> (length * elt_size);
14544 unsigned int i;
14545 unsigned char *p;
14546 machine_mode imode = GET_MODE_INNER (mode);
14548 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14549 switch (GET_MODE_CLASS (mode))
14551 case MODE_VECTOR_INT:
14552 for (i = 0, p = array; i < length; i++, p += elt_size)
14554 rtx elt = CONST_VECTOR_ELT (rtl, i);
14555 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14557 break;
14559 case MODE_VECTOR_FLOAT:
14560 for (i = 0, p = array; i < length; i++, p += elt_size)
14562 rtx elt = CONST_VECTOR_ELT (rtl, i);
14563 insert_float (elt, p);
14565 break;
14567 default:
14568 gcc_unreachable ();
14571 loc_result = new_loc_descr (DW_OP_implicit_value,
14572 length * elt_size, 0);
14573 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14574 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14575 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14576 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14578 break;
14580 case CONST:
14581 if (mode == VOIDmode
14582 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14583 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14584 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14586 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14587 break;
14589 /* FALLTHROUGH */
14590 case SYMBOL_REF:
14591 if (!const_ok_for_output (rtl))
14592 break;
14593 /* FALLTHROUGH */
14594 case LABEL_REF:
14595 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14596 && (dwarf_version >= 4 || !dwarf_strict))
14598 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14599 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14600 vec_safe_push (used_rtx_array, rtl);
14602 break;
14604 case DEBUG_IMPLICIT_PTR:
14605 loc_result = implicit_ptr_descriptor (rtl, 0);
14606 break;
14608 case PLUS:
14609 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14610 && CONST_INT_P (XEXP (rtl, 1)))
14612 loc_result
14613 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14614 break;
14616 /* FALLTHRU */
14617 do_default:
14618 default:
14619 if ((SCALAR_INT_MODE_P (mode)
14620 && GET_MODE (rtl) == mode
14621 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14622 && dwarf_version >= 4)
14623 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14625 /* Value expression. */
14626 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14627 if (loc_result)
14628 add_loc_descr (&loc_result,
14629 new_loc_descr (DW_OP_stack_value, 0, 0));
14631 break;
14634 return loc_result;
14637 /* We need to figure out what section we should use as the base for the
14638 address ranges where a given location is valid.
14639 1. If this particular DECL has a section associated with it, use that.
14640 2. If this function has a section associated with it, use that.
14641 3. Otherwise, use the text section.
14642 XXX: If you split a variable across multiple sections, we won't notice. */
14644 static const char *
14645 secname_for_decl (const_tree decl)
14647 const char *secname;
14649 if (VAR_OR_FUNCTION_DECL_P (decl)
14650 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14651 && DECL_SECTION_NAME (decl))
14652 secname = DECL_SECTION_NAME (decl);
14653 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14654 secname = DECL_SECTION_NAME (current_function_decl);
14655 else if (cfun && in_cold_section_p)
14656 secname = crtl->subsections.cold_section_label;
14657 else
14658 secname = text_section_label;
14660 return secname;
14663 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14665 static bool
14666 decl_by_reference_p (tree decl)
14668 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14669 || VAR_P (decl))
14670 && DECL_BY_REFERENCE (decl));
14673 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14674 for VARLOC. */
14676 static dw_loc_descr_ref
14677 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14678 enum var_init_status initialized)
14680 int have_address = 0;
14681 dw_loc_descr_ref descr;
14682 machine_mode mode;
14684 if (want_address != 2)
14686 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14687 /* Single part. */
14688 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14690 varloc = PAT_VAR_LOCATION_LOC (varloc);
14691 if (GET_CODE (varloc) == EXPR_LIST)
14692 varloc = XEXP (varloc, 0);
14693 mode = GET_MODE (varloc);
14694 if (MEM_P (varloc))
14696 rtx addr = XEXP (varloc, 0);
14697 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14698 mode, initialized);
14699 if (descr)
14700 have_address = 1;
14701 else
14703 rtx x = avoid_constant_pool_reference (varloc);
14704 if (x != varloc)
14705 descr = mem_loc_descriptor (x, mode, VOIDmode,
14706 initialized);
14709 else
14710 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14712 else
14713 return 0;
14715 else
14717 if (GET_CODE (varloc) == VAR_LOCATION)
14718 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14719 else
14720 mode = DECL_MODE (loc);
14721 descr = loc_descriptor (varloc, mode, initialized);
14722 have_address = 1;
14725 if (!descr)
14726 return 0;
14728 if (want_address == 2 && !have_address
14729 && (dwarf_version >= 4 || !dwarf_strict))
14731 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14733 expansion_failed (loc, NULL_RTX,
14734 "DWARF address size mismatch");
14735 return 0;
14737 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14738 have_address = 1;
14740 /* Show if we can't fill the request for an address. */
14741 if (want_address && !have_address)
14743 expansion_failed (loc, NULL_RTX,
14744 "Want address and only have value");
14745 return 0;
14748 /* If we've got an address and don't want one, dereference. */
14749 if (!want_address && have_address)
14751 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14752 enum dwarf_location_atom op;
14754 if (size > DWARF2_ADDR_SIZE || size == -1)
14756 expansion_failed (loc, NULL_RTX,
14757 "DWARF address size mismatch");
14758 return 0;
14760 else if (size == DWARF2_ADDR_SIZE)
14761 op = DW_OP_deref;
14762 else
14763 op = DW_OP_deref_size;
14765 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14768 return descr;
14771 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14772 if it is not possible. */
14774 static dw_loc_descr_ref
14775 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14777 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14778 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14779 else if (dwarf_version >= 3 || !dwarf_strict)
14780 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14781 else
14782 return NULL;
14785 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14786 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14788 static dw_loc_descr_ref
14789 dw_sra_loc_expr (tree decl, rtx loc)
14791 rtx p;
14792 unsigned HOST_WIDE_INT padsize = 0;
14793 dw_loc_descr_ref descr, *descr_tail;
14794 unsigned HOST_WIDE_INT decl_size;
14795 rtx varloc;
14796 enum var_init_status initialized;
14798 if (DECL_SIZE (decl) == NULL
14799 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14800 return NULL;
14802 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14803 descr = NULL;
14804 descr_tail = &descr;
14806 for (p = loc; p; p = XEXP (p, 1))
14808 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14809 rtx loc_note = *decl_piece_varloc_ptr (p);
14810 dw_loc_descr_ref cur_descr;
14811 dw_loc_descr_ref *tail, last = NULL;
14812 unsigned HOST_WIDE_INT opsize = 0;
14814 if (loc_note == NULL_RTX
14815 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14817 padsize += bitsize;
14818 continue;
14820 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14821 varloc = NOTE_VAR_LOCATION (loc_note);
14822 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14823 if (cur_descr == NULL)
14825 padsize += bitsize;
14826 continue;
14829 /* Check that cur_descr either doesn't use
14830 DW_OP_*piece operations, or their sum is equal
14831 to bitsize. Otherwise we can't embed it. */
14832 for (tail = &cur_descr; *tail != NULL;
14833 tail = &(*tail)->dw_loc_next)
14834 if ((*tail)->dw_loc_opc == DW_OP_piece)
14836 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14837 * BITS_PER_UNIT;
14838 last = *tail;
14840 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14842 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14843 last = *tail;
14846 if (last != NULL && opsize != bitsize)
14848 padsize += bitsize;
14849 /* Discard the current piece of the descriptor and release any
14850 addr_table entries it uses. */
14851 remove_loc_list_addr_table_entries (cur_descr);
14852 continue;
14855 /* If there is a hole, add DW_OP_*piece after empty DWARF
14856 expression, which means that those bits are optimized out. */
14857 if (padsize)
14859 if (padsize > decl_size)
14861 remove_loc_list_addr_table_entries (cur_descr);
14862 goto discard_descr;
14864 decl_size -= padsize;
14865 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14866 if (*descr_tail == NULL)
14868 remove_loc_list_addr_table_entries (cur_descr);
14869 goto discard_descr;
14871 descr_tail = &(*descr_tail)->dw_loc_next;
14872 padsize = 0;
14874 *descr_tail = cur_descr;
14875 descr_tail = tail;
14876 if (bitsize > decl_size)
14877 goto discard_descr;
14878 decl_size -= bitsize;
14879 if (last == NULL)
14881 HOST_WIDE_INT offset = 0;
14882 if (GET_CODE (varloc) == VAR_LOCATION
14883 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14885 varloc = PAT_VAR_LOCATION_LOC (varloc);
14886 if (GET_CODE (varloc) == EXPR_LIST)
14887 varloc = XEXP (varloc, 0);
14891 if (GET_CODE (varloc) == CONST
14892 || GET_CODE (varloc) == SIGN_EXTEND
14893 || GET_CODE (varloc) == ZERO_EXTEND)
14894 varloc = XEXP (varloc, 0);
14895 else if (GET_CODE (varloc) == SUBREG)
14896 varloc = SUBREG_REG (varloc);
14897 else
14898 break;
14900 while (1);
14901 /* DW_OP_bit_size offset should be zero for register
14902 or implicit location descriptions and empty location
14903 descriptions, but for memory addresses needs big endian
14904 adjustment. */
14905 if (MEM_P (varloc))
14907 unsigned HOST_WIDE_INT memsize
14908 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14909 if (memsize != bitsize)
14911 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14912 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14913 goto discard_descr;
14914 if (memsize < bitsize)
14915 goto discard_descr;
14916 if (BITS_BIG_ENDIAN)
14917 offset = memsize - bitsize;
14921 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14922 if (*descr_tail == NULL)
14923 goto discard_descr;
14924 descr_tail = &(*descr_tail)->dw_loc_next;
14928 /* If there were any non-empty expressions, add padding till the end of
14929 the decl. */
14930 if (descr != NULL && decl_size != 0)
14932 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14933 if (*descr_tail == NULL)
14934 goto discard_descr;
14936 return descr;
14938 discard_descr:
14939 /* Discard the descriptor and release any addr_table entries it uses. */
14940 remove_loc_list_addr_table_entries (descr);
14941 return NULL;
14944 /* Return the dwarf representation of the location list LOC_LIST of
14945 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14946 function. */
14948 static dw_loc_list_ref
14949 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14951 const char *endname, *secname;
14952 rtx varloc;
14953 enum var_init_status initialized;
14954 struct var_loc_node *node;
14955 dw_loc_descr_ref descr;
14956 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14957 dw_loc_list_ref list = NULL;
14958 dw_loc_list_ref *listp = &list;
14960 /* Now that we know what section we are using for a base,
14961 actually construct the list of locations.
14962 The first location information is what is passed to the
14963 function that creates the location list, and the remaining
14964 locations just get added on to that list.
14965 Note that we only know the start address for a location
14966 (IE location changes), so to build the range, we use
14967 the range [current location start, next location start].
14968 This means we have to special case the last node, and generate
14969 a range of [last location start, end of function label]. */
14971 secname = secname_for_decl (decl);
14973 for (node = loc_list->first; node; node = node->next)
14974 if (GET_CODE (node->loc) == EXPR_LIST
14975 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14977 if (GET_CODE (node->loc) == EXPR_LIST)
14979 /* This requires DW_OP_{,bit_}piece, which is not usable
14980 inside DWARF expressions. */
14981 if (want_address != 2)
14982 continue;
14983 descr = dw_sra_loc_expr (decl, node->loc);
14984 if (descr == NULL)
14985 continue;
14987 else
14989 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14990 varloc = NOTE_VAR_LOCATION (node->loc);
14991 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14993 if (descr)
14995 bool range_across_switch = false;
14996 /* If section switch happens in between node->label
14997 and node->next->label (or end of function) and
14998 we can't emit it as a single entry list,
14999 emit two ranges, first one ending at the end
15000 of first partition and second one starting at the
15001 beginning of second partition. */
15002 if (node == loc_list->last_before_switch
15003 && (node != loc_list->first || loc_list->first->next)
15004 && current_function_decl)
15006 endname = cfun->fde->dw_fde_end;
15007 range_across_switch = true;
15009 /* The variable has a location between NODE->LABEL and
15010 NODE->NEXT->LABEL. */
15011 else if (node->next)
15012 endname = node->next->label;
15013 /* If the variable has a location at the last label
15014 it keeps its location until the end of function. */
15015 else if (!current_function_decl)
15016 endname = text_end_label;
15017 else
15019 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15020 current_function_funcdef_no);
15021 endname = ggc_strdup (label_id);
15024 *listp = new_loc_list (descr, node->label, endname, secname);
15025 if (TREE_CODE (decl) == PARM_DECL
15026 && node == loc_list->first
15027 && NOTE_P (node->loc)
15028 && strcmp (node->label, endname) == 0)
15029 (*listp)->force = true;
15030 listp = &(*listp)->dw_loc_next;
15032 if (range_across_switch)
15034 if (GET_CODE (node->loc) == EXPR_LIST)
15035 descr = dw_sra_loc_expr (decl, node->loc);
15036 else
15038 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15039 varloc = NOTE_VAR_LOCATION (node->loc);
15040 descr = dw_loc_list_1 (decl, varloc, want_address,
15041 initialized);
15043 gcc_assert (descr);
15044 /* The variable has a location between NODE->LABEL and
15045 NODE->NEXT->LABEL. */
15046 if (node->next)
15047 endname = node->next->label;
15048 else
15049 endname = cfun->fde->dw_fde_second_end;
15050 *listp = new_loc_list (descr,
15051 cfun->fde->dw_fde_second_begin,
15052 endname, secname);
15053 listp = &(*listp)->dw_loc_next;
15058 /* Try to avoid the overhead of a location list emitting a location
15059 expression instead, but only if we didn't have more than one
15060 location entry in the first place. If some entries were not
15061 representable, we don't want to pretend a single entry that was
15062 applies to the entire scope in which the variable is
15063 available. */
15064 if (list && loc_list->first->next)
15065 gen_llsym (list);
15067 return list;
15070 /* Return if the loc_list has only single element and thus can be represented
15071 as location description. */
15073 static bool
15074 single_element_loc_list_p (dw_loc_list_ref list)
15076 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15077 return !list->ll_symbol;
15080 /* To each location in list LIST add loc descr REF. */
15082 static void
15083 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15085 dw_loc_descr_ref copy;
15086 add_loc_descr (&list->expr, ref);
15087 list = list->dw_loc_next;
15088 while (list)
15090 copy = ggc_alloc<dw_loc_descr_node> ();
15091 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15092 add_loc_descr (&list->expr, copy);
15093 while (copy->dw_loc_next)
15095 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15096 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15097 copy->dw_loc_next = new_copy;
15098 copy = new_copy;
15100 list = list->dw_loc_next;
15104 /* Given two lists RET and LIST
15105 produce location list that is result of adding expression in LIST
15106 to expression in RET on each position in program.
15107 Might be destructive on both RET and LIST.
15109 TODO: We handle only simple cases of RET or LIST having at most one
15110 element. General case would inolve sorting the lists in program order
15111 and merging them that will need some additional work.
15112 Adding that will improve quality of debug info especially for SRA-ed
15113 structures. */
15115 static void
15116 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15118 if (!list)
15119 return;
15120 if (!*ret)
15122 *ret = list;
15123 return;
15125 if (!list->dw_loc_next)
15127 add_loc_descr_to_each (*ret, list->expr);
15128 return;
15130 if (!(*ret)->dw_loc_next)
15132 add_loc_descr_to_each (list, (*ret)->expr);
15133 *ret = list;
15134 return;
15136 expansion_failed (NULL_TREE, NULL_RTX,
15137 "Don't know how to merge two non-trivial"
15138 " location lists.\n");
15139 *ret = NULL;
15140 return;
15143 /* LOC is constant expression. Try a luck, look it up in constant
15144 pool and return its loc_descr of its address. */
15146 static dw_loc_descr_ref
15147 cst_pool_loc_descr (tree loc)
15149 /* Get an RTL for this, if something has been emitted. */
15150 rtx rtl = lookup_constant_def (loc);
15152 if (!rtl || !MEM_P (rtl))
15154 gcc_assert (!rtl);
15155 return 0;
15157 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15159 /* TODO: We might get more coverage if we was actually delaying expansion
15160 of all expressions till end of compilation when constant pools are fully
15161 populated. */
15162 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15164 expansion_failed (loc, NULL_RTX,
15165 "CST value in contant pool but not marked.");
15166 return 0;
15168 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15169 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15172 /* Return dw_loc_list representing address of addr_expr LOC
15173 by looking for inner INDIRECT_REF expression and turning
15174 it into simple arithmetics.
15176 See loc_list_from_tree for the meaning of CONTEXT. */
15178 static dw_loc_list_ref
15179 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15180 const loc_descr_context *context)
15182 tree obj, offset;
15183 HOST_WIDE_INT bitsize, bitpos, bytepos;
15184 machine_mode mode;
15185 int unsignedp, reversep, volatilep = 0;
15186 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15188 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15189 &bitsize, &bitpos, &offset, &mode,
15190 &unsignedp, &reversep, &volatilep);
15191 STRIP_NOPS (obj);
15192 if (bitpos % BITS_PER_UNIT)
15194 expansion_failed (loc, NULL_RTX, "bitfield access");
15195 return 0;
15197 if (!INDIRECT_REF_P (obj))
15199 expansion_failed (obj,
15200 NULL_RTX, "no indirect ref in inner refrence");
15201 return 0;
15203 if (!offset && !bitpos)
15204 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15205 context);
15206 else if (toplev
15207 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15208 && (dwarf_version >= 4 || !dwarf_strict))
15210 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15211 if (!list_ret)
15212 return 0;
15213 if (offset)
15215 /* Variable offset. */
15216 list_ret1 = loc_list_from_tree (offset, 0, context);
15217 if (list_ret1 == 0)
15218 return 0;
15219 add_loc_list (&list_ret, list_ret1);
15220 if (!list_ret)
15221 return 0;
15222 add_loc_descr_to_each (list_ret,
15223 new_loc_descr (DW_OP_plus, 0, 0));
15225 bytepos = bitpos / BITS_PER_UNIT;
15226 if (bytepos > 0)
15227 add_loc_descr_to_each (list_ret,
15228 new_loc_descr (DW_OP_plus_uconst,
15229 bytepos, 0));
15230 else if (bytepos < 0)
15231 loc_list_plus_const (list_ret, bytepos);
15232 add_loc_descr_to_each (list_ret,
15233 new_loc_descr (DW_OP_stack_value, 0, 0));
15235 return list_ret;
15238 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15239 all operations from LOC are nops, move to the last one. Insert in NOPS all
15240 operations that are skipped. */
15242 static void
15243 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15244 hash_set<dw_loc_descr_ref> &nops)
15246 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15248 nops.add (loc);
15249 loc = loc->dw_loc_next;
15253 /* Helper for loc_descr_without_nops: free the location description operation
15254 P. */
15256 bool
15257 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15259 ggc_free (loc);
15260 return true;
15263 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15264 finishes LOC. */
15266 static void
15267 loc_descr_without_nops (dw_loc_descr_ref &loc)
15269 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15270 return;
15272 /* Set of all DW_OP_nop operations we remove. */
15273 hash_set<dw_loc_descr_ref> nops;
15275 /* First, strip all prefix NOP operations in order to keep the head of the
15276 operations list. */
15277 loc_descr_to_next_no_nop (loc, nops);
15279 for (dw_loc_descr_ref cur = loc; cur != NULL;)
15281 /* For control flow operations: strip "prefix" nops in destination
15282 labels. */
15283 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15284 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15285 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15286 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15288 /* Do the same for the operations that follow, then move to the next
15289 iteration. */
15290 if (cur->dw_loc_next != NULL)
15291 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15292 cur = cur->dw_loc_next;
15295 nops.traverse<void *, free_loc_descr> (NULL);
15299 struct dwarf_procedure_info;
15301 /* Helper structure for location descriptions generation. */
15302 struct loc_descr_context
15304 /* The type that is implicitly referenced by DW_OP_push_object_address, or
15305 NULL_TREE if DW_OP_push_object_address in invalid for this location
15306 description. This is used when processing PLACEHOLDER_EXPR nodes. */
15307 tree context_type;
15308 /* The ..._DECL node that should be translated as a
15309 DW_OP_push_object_address operation. */
15310 tree base_decl;
15311 /* Information about the DWARF procedure we are currently generating. NULL if
15312 we are not generating a DWARF procedure. */
15313 struct dwarf_procedure_info *dpi;
15316 /* DWARF procedures generation
15318 DWARF expressions (aka. location descriptions) are used to encode variable
15319 things such as sizes or offsets. Such computations can have redundant parts
15320 that can be factorized in order to reduce the size of the output debug
15321 information. This is the whole point of DWARF procedures.
15323 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15324 already factorized into functions ("size functions") in order to handle very
15325 big and complex types. Such functions are quite simple: they have integral
15326 arguments, they return an integral result and their body contains only a
15327 return statement with arithmetic expressions. This is the only kind of
15328 function we are interested in translating into DWARF procedures, here.
15330 DWARF expressions and DWARF procedure are executed using a stack, so we have
15331 to define some calling convention for them to interact. Let's say that:
15333 - Before calling a DWARF procedure, DWARF expressions must push on the stack
15334 all arguments in reverse order (right-to-left) so that when the DWARF
15335 procedure execution starts, the first argument is the top of the stack.
15337 - Then, when returning, the DWARF procedure must have consumed all arguments
15338 on the stack, must have pushed the result and touched nothing else.
15340 - Each integral argument and the result are integral types can be hold in a
15341 single stack slot.
15343 - We call "frame offset" the number of stack slots that are "under DWARF
15344 procedure control": it includes the arguments slots, the temporaries and
15345 the result slot. Thus, it is equal to the number of arguments when the
15346 procedure execution starts and must be equal to one (the result) when it
15347 returns. */
15349 /* Helper structure used when generating operations for a DWARF procedure. */
15350 struct dwarf_procedure_info
15352 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15353 currently translated. */
15354 tree fndecl;
15355 /* The number of arguments FNDECL takes. */
15356 unsigned args_count;
15359 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
15360 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
15361 equate it to this DIE. */
15363 static dw_die_ref
15364 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15365 dw_die_ref parent_die)
15367 dw_die_ref dwarf_proc_die;
15369 if ((dwarf_version < 3 && dwarf_strict)
15370 || location == NULL)
15371 return NULL;
15373 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
15374 if (fndecl)
15375 equate_decl_number_to_die (fndecl, dwarf_proc_die);
15376 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15377 return dwarf_proc_die;
15380 /* Return whether TYPE is a supported type as a DWARF procedure argument
15381 type or return type (we handle only scalar types and pointer types that
15382 aren't wider than the DWARF expression evaluation stack. */
15384 static bool
15385 is_handled_procedure_type (tree type)
15387 return ((INTEGRAL_TYPE_P (type)
15388 || TREE_CODE (type) == OFFSET_TYPE
15389 || TREE_CODE (type) == POINTER_TYPE)
15390 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15393 /* Helper for resolve_args_picking: do the same but stop when coming across
15394 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
15395 offset *before* evaluating the corresponding operation. */
15397 static bool
15398 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15399 struct dwarf_procedure_info *dpi,
15400 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
15402 /* The "frame_offset" identifier is already used to name a macro... */
15403 unsigned frame_offset_ = initial_frame_offset;
15404 dw_loc_descr_ref l;
15406 for (l = loc; l != NULL;)
15408 bool existed;
15409 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
15411 /* If we already met this node, there is nothing to compute anymore. */
15412 if (existed)
15414 /* Make sure that the stack size is consistent wherever the execution
15415 flow comes from. */
15416 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
15417 break;
15419 l_frame_offset = frame_offset_;
15421 /* If needed, relocate the picking offset with respect to the frame
15422 offset. */
15423 if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15425 /* frame_offset_ is the size of the current stack frame, including
15426 incoming arguments. Besides, the arguments are pushed
15427 right-to-left. Thus, in order to access the Nth argument from
15428 this operation node, the picking has to skip temporaries *plus*
15429 one stack slot per argument (0 for the first one, 1 for the second
15430 one, etc.).
15432 The targetted argument number (N) is already set as the operand,
15433 and the number of temporaries can be computed with:
15434 frame_offsets_ - dpi->args_count */
15435 l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15437 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
15438 if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15439 return false;
15442 /* Update frame_offset according to the effect the current operation has
15443 on the stack. */
15444 switch (l->dw_loc_opc)
15446 case DW_OP_deref:
15447 case DW_OP_swap:
15448 case DW_OP_rot:
15449 case DW_OP_abs:
15450 case DW_OP_neg:
15451 case DW_OP_not:
15452 case DW_OP_plus_uconst:
15453 case DW_OP_skip:
15454 case DW_OP_reg0:
15455 case DW_OP_reg1:
15456 case DW_OP_reg2:
15457 case DW_OP_reg3:
15458 case DW_OP_reg4:
15459 case DW_OP_reg5:
15460 case DW_OP_reg6:
15461 case DW_OP_reg7:
15462 case DW_OP_reg8:
15463 case DW_OP_reg9:
15464 case DW_OP_reg10:
15465 case DW_OP_reg11:
15466 case DW_OP_reg12:
15467 case DW_OP_reg13:
15468 case DW_OP_reg14:
15469 case DW_OP_reg15:
15470 case DW_OP_reg16:
15471 case DW_OP_reg17:
15472 case DW_OP_reg18:
15473 case DW_OP_reg19:
15474 case DW_OP_reg20:
15475 case DW_OP_reg21:
15476 case DW_OP_reg22:
15477 case DW_OP_reg23:
15478 case DW_OP_reg24:
15479 case DW_OP_reg25:
15480 case DW_OP_reg26:
15481 case DW_OP_reg27:
15482 case DW_OP_reg28:
15483 case DW_OP_reg29:
15484 case DW_OP_reg30:
15485 case DW_OP_reg31:
15486 case DW_OP_bregx:
15487 case DW_OP_piece:
15488 case DW_OP_deref_size:
15489 case DW_OP_nop:
15490 case DW_OP_form_tls_address:
15491 case DW_OP_bit_piece:
15492 case DW_OP_implicit_value:
15493 case DW_OP_stack_value:
15494 break;
15496 case DW_OP_addr:
15497 case DW_OP_const1u:
15498 case DW_OP_const1s:
15499 case DW_OP_const2u:
15500 case DW_OP_const2s:
15501 case DW_OP_const4u:
15502 case DW_OP_const4s:
15503 case DW_OP_const8u:
15504 case DW_OP_const8s:
15505 case DW_OP_constu:
15506 case DW_OP_consts:
15507 case DW_OP_dup:
15508 case DW_OP_over:
15509 case DW_OP_pick:
15510 case DW_OP_lit0:
15511 case DW_OP_lit1:
15512 case DW_OP_lit2:
15513 case DW_OP_lit3:
15514 case DW_OP_lit4:
15515 case DW_OP_lit5:
15516 case DW_OP_lit6:
15517 case DW_OP_lit7:
15518 case DW_OP_lit8:
15519 case DW_OP_lit9:
15520 case DW_OP_lit10:
15521 case DW_OP_lit11:
15522 case DW_OP_lit12:
15523 case DW_OP_lit13:
15524 case DW_OP_lit14:
15525 case DW_OP_lit15:
15526 case DW_OP_lit16:
15527 case DW_OP_lit17:
15528 case DW_OP_lit18:
15529 case DW_OP_lit19:
15530 case DW_OP_lit20:
15531 case DW_OP_lit21:
15532 case DW_OP_lit22:
15533 case DW_OP_lit23:
15534 case DW_OP_lit24:
15535 case DW_OP_lit25:
15536 case DW_OP_lit26:
15537 case DW_OP_lit27:
15538 case DW_OP_lit28:
15539 case DW_OP_lit29:
15540 case DW_OP_lit30:
15541 case DW_OP_lit31:
15542 case DW_OP_breg0:
15543 case DW_OP_breg1:
15544 case DW_OP_breg2:
15545 case DW_OP_breg3:
15546 case DW_OP_breg4:
15547 case DW_OP_breg5:
15548 case DW_OP_breg6:
15549 case DW_OP_breg7:
15550 case DW_OP_breg8:
15551 case DW_OP_breg9:
15552 case DW_OP_breg10:
15553 case DW_OP_breg11:
15554 case DW_OP_breg12:
15555 case DW_OP_breg13:
15556 case DW_OP_breg14:
15557 case DW_OP_breg15:
15558 case DW_OP_breg16:
15559 case DW_OP_breg17:
15560 case DW_OP_breg18:
15561 case DW_OP_breg19:
15562 case DW_OP_breg20:
15563 case DW_OP_breg21:
15564 case DW_OP_breg22:
15565 case DW_OP_breg23:
15566 case DW_OP_breg24:
15567 case DW_OP_breg25:
15568 case DW_OP_breg26:
15569 case DW_OP_breg27:
15570 case DW_OP_breg28:
15571 case DW_OP_breg29:
15572 case DW_OP_breg30:
15573 case DW_OP_breg31:
15574 case DW_OP_fbreg:
15575 case DW_OP_push_object_address:
15576 case DW_OP_call_frame_cfa:
15577 ++frame_offset_;
15578 break;
15580 case DW_OP_drop:
15581 case DW_OP_xderef:
15582 case DW_OP_and:
15583 case DW_OP_div:
15584 case DW_OP_minus:
15585 case DW_OP_mod:
15586 case DW_OP_mul:
15587 case DW_OP_or:
15588 case DW_OP_plus:
15589 case DW_OP_shl:
15590 case DW_OP_shr:
15591 case DW_OP_shra:
15592 case DW_OP_xor:
15593 case DW_OP_bra:
15594 case DW_OP_eq:
15595 case DW_OP_ge:
15596 case DW_OP_gt:
15597 case DW_OP_le:
15598 case DW_OP_lt:
15599 case DW_OP_ne:
15600 case DW_OP_regx:
15601 case DW_OP_xderef_size:
15602 --frame_offset_;
15603 break;
15605 case DW_OP_call2:
15606 case DW_OP_call4:
15607 case DW_OP_call_ref:
15609 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15610 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15612 if (stack_usage == NULL)
15613 return false;
15614 frame_offset_ += *stack_usage;
15615 break;
15618 case DW_OP_GNU_push_tls_address:
15619 case DW_OP_GNU_uninit:
15620 case DW_OP_GNU_encoded_addr:
15621 case DW_OP_GNU_implicit_pointer:
15622 case DW_OP_GNU_entry_value:
15623 case DW_OP_GNU_const_type:
15624 case DW_OP_GNU_regval_type:
15625 case DW_OP_GNU_deref_type:
15626 case DW_OP_GNU_convert:
15627 case DW_OP_GNU_reinterpret:
15628 case DW_OP_GNU_parameter_ref:
15629 /* loc_list_from_tree will probably not output these operations for
15630 size functions, so assume they will not appear here. */
15631 /* Fall through... */
15633 default:
15634 gcc_unreachable ();
15637 /* Now, follow the control flow (except subroutine calls). */
15638 switch (l->dw_loc_opc)
15640 case DW_OP_bra:
15641 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15642 frame_offsets))
15643 return false;
15644 /* Fall through. */
15646 case DW_OP_skip:
15647 l = l->dw_loc_oprnd1.v.val_loc;
15648 break;
15650 case DW_OP_stack_value:
15651 return true;
15653 default:
15654 l = l->dw_loc_next;
15655 break;
15659 return true;
15662 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15663 operations) in order to resolve the operand of DW_OP_pick operations that
15664 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
15665 offset *before* LOC is executed. Return if all relocations were
15666 successful. */
15668 static bool
15669 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15670 struct dwarf_procedure_info *dpi)
15672 /* Associate to all visited operations the frame offset *before* evaluating
15673 this operation. */
15674 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
15676 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
15677 frame_offsets);
15680 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15681 Return NULL if it is not possible. */
15683 static dw_die_ref
15684 function_to_dwarf_procedure (tree fndecl)
15686 struct loc_descr_context ctx;
15687 struct dwarf_procedure_info dpi;
15688 dw_die_ref dwarf_proc_die;
15689 tree tree_body = DECL_SAVED_TREE (fndecl);
15690 dw_loc_descr_ref loc_body, epilogue;
15692 tree cursor;
15693 unsigned i;
15695 /* Do not generate multiple DWARF procedures for the same function
15696 declaration. */
15697 dwarf_proc_die = lookup_decl_die (fndecl);
15698 if (dwarf_proc_die != NULL)
15699 return dwarf_proc_die;
15701 /* DWARF procedures are available starting with the DWARFv3 standard. */
15702 if (dwarf_version < 3 && dwarf_strict)
15703 return NULL;
15705 /* We handle only functions for which we still have a body, that return a
15706 supported type and that takes arguments with supported types. Note that
15707 there is no point translating functions that return nothing. */
15708 if (tree_body == NULL_TREE
15709 || DECL_RESULT (fndecl) == NULL_TREE
15710 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15711 return NULL;
15713 for (cursor = DECL_ARGUMENTS (fndecl);
15714 cursor != NULL_TREE;
15715 cursor = TREE_CHAIN (cursor))
15716 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15717 return NULL;
15719 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
15720 if (TREE_CODE (tree_body) != RETURN_EXPR)
15721 return NULL;
15722 tree_body = TREE_OPERAND (tree_body, 0);
15723 if (TREE_CODE (tree_body) != MODIFY_EXPR
15724 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15725 return NULL;
15726 tree_body = TREE_OPERAND (tree_body, 1);
15728 /* Try to translate the body expression itself. Note that this will probably
15729 cause an infinite recursion if its call graph has a cycle. This is very
15730 unlikely for size functions, however, so don't bother with such things at
15731 the moment. */
15732 ctx.context_type = NULL_TREE;
15733 ctx.base_decl = NULL_TREE;
15734 ctx.dpi = &dpi;
15735 dpi.fndecl = fndecl;
15736 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15737 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15738 if (!loc_body)
15739 return NULL;
15741 /* After evaluating all operands in "loc_body", we should still have on the
15742 stack all arguments plus the desired function result (top of the stack).
15743 Generate code in order to keep only the result in our stack frame. */
15744 epilogue = NULL;
15745 for (i = 0; i < dpi.args_count; ++i)
15747 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15748 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15749 op_couple->dw_loc_next->dw_loc_next = epilogue;
15750 epilogue = op_couple;
15752 add_loc_descr (&loc_body, epilogue);
15753 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15754 return NULL;
15756 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15757 because they are considered useful. Now there is an epilogue, they are
15758 not anymore, so give it another try. */
15759 loc_descr_without_nops (loc_body);
15761 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15762 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
15763 though, given that size functions do not come from source, so they should
15764 not have a dedicated DW_TAG_subprogram DIE. */
15765 dwarf_proc_die
15766 = new_dwarf_proc_die (loc_body, fndecl,
15767 get_context_die (DECL_CONTEXT (fndecl)));
15769 /* The called DWARF procedure consumes one stack slot per argument and
15770 returns one stack slot. */
15771 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15773 return dwarf_proc_die;
15777 /* Generate Dwarf location list representing LOC.
15778 If WANT_ADDRESS is false, expression computing LOC will be computed
15779 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15780 if WANT_ADDRESS is 2, expression computing address useable in location
15781 will be returned (i.e. DW_OP_reg can be used
15782 to refer to register values).
15784 CONTEXT provides information to customize the location descriptions
15785 generation. Its context_type field specifies what type is implicitly
15786 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
15787 will not be generated.
15789 Its DPI field determines whether we are generating a DWARF expression for a
15790 DWARF procedure, so PARM_DECL references are processed specifically.
15792 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15793 and dpi fields were null. */
15795 static dw_loc_list_ref
15796 loc_list_from_tree_1 (tree loc, int want_address,
15797 const struct loc_descr_context *context)
15799 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15800 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15801 int have_address = 0;
15802 enum dwarf_location_atom op;
15804 /* ??? Most of the time we do not take proper care for sign/zero
15805 extending the values properly. Hopefully this won't be a real
15806 problem... */
15808 if (context != NULL
15809 && context->base_decl == loc
15810 && want_address == 0)
15812 if (dwarf_version >= 3 || !dwarf_strict)
15813 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15814 NULL, NULL, NULL);
15815 else
15816 return NULL;
15819 switch (TREE_CODE (loc))
15821 case ERROR_MARK:
15822 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15823 return 0;
15825 case PLACEHOLDER_EXPR:
15826 /* This case involves extracting fields from an object to determine the
15827 position of other fields. It is supposed to appear only as the first
15828 operand of COMPONENT_REF nodes and to reference precisely the type
15829 that the context allows. */
15830 if (context != NULL
15831 && TREE_TYPE (loc) == context->context_type
15832 && want_address >= 1)
15834 if (dwarf_version >= 3 || !dwarf_strict)
15836 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15837 have_address = 1;
15838 break;
15840 else
15841 return NULL;
15843 else
15844 expansion_failed (loc, NULL_RTX,
15845 "PLACEHOLDER_EXPR for an unexpected type");
15846 break;
15848 case CALL_EXPR:
15850 const int nargs = call_expr_nargs (loc);
15851 tree callee = get_callee_fndecl (loc);
15852 int i;
15853 dw_die_ref dwarf_proc;
15855 if (callee == NULL_TREE)
15856 goto call_expansion_failed;
15858 /* We handle only functions that return an integer. */
15859 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15860 goto call_expansion_failed;
15862 dwarf_proc = function_to_dwarf_procedure (callee);
15863 if (dwarf_proc == NULL)
15864 goto call_expansion_failed;
15866 /* Evaluate arguments right-to-left so that the first argument will
15867 be the top-most one on the stack. */
15868 for (i = nargs - 1; i >= 0; --i)
15870 dw_loc_descr_ref loc_descr
15871 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15872 context);
15874 if (loc_descr == NULL)
15875 goto call_expansion_failed;
15877 add_loc_descr (&ret, loc_descr);
15880 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15881 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15882 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15883 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15884 add_loc_descr (&ret, ret1);
15885 break;
15887 call_expansion_failed:
15888 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15889 /* There are no opcodes for these operations. */
15890 return 0;
15893 case PREINCREMENT_EXPR:
15894 case PREDECREMENT_EXPR:
15895 case POSTINCREMENT_EXPR:
15896 case POSTDECREMENT_EXPR:
15897 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15898 /* There are no opcodes for these operations. */
15899 return 0;
15901 case ADDR_EXPR:
15902 /* If we already want an address, see if there is INDIRECT_REF inside
15903 e.g. for &this->field. */
15904 if (want_address)
15906 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15907 (loc, want_address == 2, context);
15908 if (list_ret)
15909 have_address = 1;
15910 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15911 && (ret = cst_pool_loc_descr (loc)))
15912 have_address = 1;
15914 /* Otherwise, process the argument and look for the address. */
15915 if (!list_ret && !ret)
15916 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15917 else
15919 if (want_address)
15920 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15921 return NULL;
15923 break;
15925 case VAR_DECL:
15926 if (DECL_THREAD_LOCAL_P (loc))
15928 rtx rtl;
15929 enum dwarf_location_atom tls_op;
15930 enum dtprel_bool dtprel = dtprel_false;
15932 if (targetm.have_tls)
15934 /* If this is not defined, we have no way to emit the
15935 data. */
15936 if (!targetm.asm_out.output_dwarf_dtprel)
15937 return 0;
15939 /* The way DW_OP_GNU_push_tls_address is specified, we
15940 can only look up addresses of objects in the current
15941 module. We used DW_OP_addr as first op, but that's
15942 wrong, because DW_OP_addr is relocated by the debug
15943 info consumer, while DW_OP_GNU_push_tls_address
15944 operand shouldn't be. */
15945 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15946 return 0;
15947 dtprel = dtprel_true;
15948 tls_op = DW_OP_GNU_push_tls_address;
15950 else
15952 if (!targetm.emutls.debug_form_tls_address
15953 || !(dwarf_version >= 3 || !dwarf_strict))
15954 return 0;
15955 /* We stuffed the control variable into the DECL_VALUE_EXPR
15956 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15957 no longer appear in gimple code. We used the control
15958 variable in specific so that we could pick it up here. */
15959 loc = DECL_VALUE_EXPR (loc);
15960 tls_op = DW_OP_form_tls_address;
15963 rtl = rtl_for_decl_location (loc);
15964 if (rtl == NULL_RTX)
15965 return 0;
15967 if (!MEM_P (rtl))
15968 return 0;
15969 rtl = XEXP (rtl, 0);
15970 if (! CONSTANT_P (rtl))
15971 return 0;
15973 ret = new_addr_loc_descr (rtl, dtprel);
15974 ret1 = new_loc_descr (tls_op, 0, 0);
15975 add_loc_descr (&ret, ret1);
15977 have_address = 1;
15978 break;
15980 /* FALLTHRU */
15982 case PARM_DECL:
15983 if (context != NULL && context->dpi != NULL
15984 && DECL_CONTEXT (loc) == context->dpi->fndecl)
15986 /* We are generating code for a DWARF procedure and we want to access
15987 one of its arguments: find the appropriate argument offset and let
15988 the resolve_args_picking pass compute the offset that complies
15989 with the stack frame size. */
15990 unsigned i = 0;
15991 tree cursor;
15993 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15994 cursor != NULL_TREE && cursor != loc;
15995 cursor = TREE_CHAIN (cursor), ++i)
15997 /* If we are translating a DWARF procedure, all referenced parameters
15998 must belong to the current function. */
15999 gcc_assert (cursor != NULL_TREE);
16001 ret = new_loc_descr (DW_OP_pick, i, 0);
16002 ret->frame_offset_rel = 1;
16003 break;
16005 /* FALLTHRU */
16007 case RESULT_DECL:
16008 if (DECL_HAS_VALUE_EXPR_P (loc))
16009 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
16010 want_address, context);
16011 /* FALLTHRU */
16013 case FUNCTION_DECL:
16015 rtx rtl;
16016 var_loc_list *loc_list = lookup_decl_loc (loc);
16018 if (loc_list && loc_list->first)
16020 list_ret = dw_loc_list (loc_list, loc, want_address);
16021 have_address = want_address != 0;
16022 break;
16024 rtl = rtl_for_decl_location (loc);
16025 if (rtl == NULL_RTX)
16027 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
16028 return 0;
16030 else if (CONST_INT_P (rtl))
16032 HOST_WIDE_INT val = INTVAL (rtl);
16033 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16034 val &= GET_MODE_MASK (DECL_MODE (loc));
16035 ret = int_loc_descriptor (val);
16037 else if (GET_CODE (rtl) == CONST_STRING)
16039 expansion_failed (loc, NULL_RTX, "CONST_STRING");
16040 return 0;
16042 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16043 ret = new_addr_loc_descr (rtl, dtprel_false);
16044 else
16046 machine_mode mode, mem_mode;
16048 /* Certain constructs can only be represented at top-level. */
16049 if (want_address == 2)
16051 ret = loc_descriptor (rtl, VOIDmode,
16052 VAR_INIT_STATUS_INITIALIZED);
16053 have_address = 1;
16055 else
16057 mode = GET_MODE (rtl);
16058 mem_mode = VOIDmode;
16059 if (MEM_P (rtl))
16061 mem_mode = mode;
16062 mode = get_address_mode (rtl);
16063 rtl = XEXP (rtl, 0);
16064 have_address = 1;
16066 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16067 VAR_INIT_STATUS_INITIALIZED);
16069 if (!ret)
16070 expansion_failed (loc, rtl,
16071 "failed to produce loc descriptor for rtl");
16074 break;
16076 case MEM_REF:
16077 if (!integer_zerop (TREE_OPERAND (loc, 1)))
16079 have_address = 1;
16080 goto do_plus;
16082 /* Fallthru. */
16083 case INDIRECT_REF:
16084 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16085 have_address = 1;
16086 break;
16088 case TARGET_MEM_REF:
16089 case SSA_NAME:
16090 case DEBUG_EXPR_DECL:
16091 return NULL;
16093 case COMPOUND_EXPR:
16094 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16095 context);
16097 CASE_CONVERT:
16098 case VIEW_CONVERT_EXPR:
16099 case SAVE_EXPR:
16100 case MODIFY_EXPR:
16101 case NON_LVALUE_EXPR:
16102 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16103 context);
16105 case COMPONENT_REF:
16106 case BIT_FIELD_REF:
16107 case ARRAY_REF:
16108 case ARRAY_RANGE_REF:
16109 case REALPART_EXPR:
16110 case IMAGPART_EXPR:
16112 tree obj, offset;
16113 HOST_WIDE_INT bitsize, bitpos, bytepos;
16114 machine_mode mode;
16115 int unsignedp, reversep, volatilep = 0;
16117 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16118 &unsignedp, &reversep, &volatilep);
16120 gcc_assert (obj != loc);
16122 list_ret = loc_list_from_tree_1 (obj,
16123 want_address == 2
16124 && !bitpos && !offset ? 2 : 1,
16125 context);
16126 /* TODO: We can extract value of the small expression via shifting even
16127 for nonzero bitpos. */
16128 if (list_ret == 0)
16129 return 0;
16130 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16132 expansion_failed (loc, NULL_RTX,
16133 "bitfield access");
16134 return 0;
16137 if (offset != NULL_TREE)
16139 /* Variable offset. */
16140 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16141 if (list_ret1 == 0)
16142 return 0;
16143 add_loc_list (&list_ret, list_ret1);
16144 if (!list_ret)
16145 return 0;
16146 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16149 bytepos = bitpos / BITS_PER_UNIT;
16150 if (bytepos > 0)
16151 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16152 else if (bytepos < 0)
16153 loc_list_plus_const (list_ret, bytepos);
16155 have_address = 1;
16156 break;
16159 case INTEGER_CST:
16160 if ((want_address || !tree_fits_shwi_p (loc))
16161 && (ret = cst_pool_loc_descr (loc)))
16162 have_address = 1;
16163 else if (want_address == 2
16164 && tree_fits_shwi_p (loc)
16165 && (ret = address_of_int_loc_descriptor
16166 (int_size_in_bytes (TREE_TYPE (loc)),
16167 tree_to_shwi (loc))))
16168 have_address = 1;
16169 else if (tree_fits_shwi_p (loc))
16170 ret = int_loc_descriptor (tree_to_shwi (loc));
16171 else if (tree_fits_uhwi_p (loc))
16172 ret = uint_loc_descriptor (tree_to_uhwi (loc));
16173 else
16175 expansion_failed (loc, NULL_RTX,
16176 "Integer operand is not host integer");
16177 return 0;
16179 break;
16181 case CONSTRUCTOR:
16182 case REAL_CST:
16183 case STRING_CST:
16184 case COMPLEX_CST:
16185 if ((ret = cst_pool_loc_descr (loc)))
16186 have_address = 1;
16187 else if (TREE_CODE (loc) == CONSTRUCTOR)
16189 tree type = TREE_TYPE (loc);
16190 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
16191 unsigned HOST_WIDE_INT offset = 0;
16192 unsigned HOST_WIDE_INT cnt;
16193 constructor_elt *ce;
16195 if (TREE_CODE (type) == RECORD_TYPE)
16197 /* This is very limited, but it's enough to output
16198 pointers to member functions, as long as the
16199 referenced function is defined in the current
16200 translation unit. */
16201 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
16203 tree val = ce->value;
16205 tree field = ce->index;
16207 if (val)
16208 STRIP_NOPS (val);
16210 if (!field || DECL_BIT_FIELD (field))
16212 expansion_failed (loc, NULL_RTX,
16213 "bitfield in record type constructor");
16214 size = offset = (unsigned HOST_WIDE_INT)-1;
16215 ret = NULL;
16216 break;
16219 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16220 unsigned HOST_WIDE_INT pos = int_byte_position (field);
16221 gcc_assert (pos + fieldsize <= size);
16222 if (pos < offset)
16224 expansion_failed (loc, NULL_RTX,
16225 "out-of-order fields in record constructor");
16226 size = offset = (unsigned HOST_WIDE_INT)-1;
16227 ret = NULL;
16228 break;
16230 if (pos > offset)
16232 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
16233 add_loc_descr (&ret, ret1);
16234 offset = pos;
16236 if (val && fieldsize != 0)
16238 ret1 = loc_descriptor_from_tree (val, want_address, context);
16239 if (!ret1)
16241 expansion_failed (loc, NULL_RTX,
16242 "unsupported expression in field");
16243 size = offset = (unsigned HOST_WIDE_INT)-1;
16244 ret = NULL;
16245 break;
16247 add_loc_descr (&ret, ret1);
16249 if (fieldsize)
16251 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
16252 add_loc_descr (&ret, ret1);
16253 offset = pos + fieldsize;
16257 if (offset != size)
16259 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
16260 add_loc_descr (&ret, ret1);
16261 offset = size;
16264 have_address = !!want_address;
16266 else
16267 expansion_failed (loc, NULL_RTX,
16268 "constructor of non-record type");
16270 else
16271 /* We can construct small constants here using int_loc_descriptor. */
16272 expansion_failed (loc, NULL_RTX,
16273 "constructor or constant not in constant pool");
16274 break;
16276 case TRUTH_AND_EXPR:
16277 case TRUTH_ANDIF_EXPR:
16278 case BIT_AND_EXPR:
16279 op = DW_OP_and;
16280 goto do_binop;
16282 case TRUTH_XOR_EXPR:
16283 case BIT_XOR_EXPR:
16284 op = DW_OP_xor;
16285 goto do_binop;
16287 case TRUTH_OR_EXPR:
16288 case TRUTH_ORIF_EXPR:
16289 case BIT_IOR_EXPR:
16290 op = DW_OP_or;
16291 goto do_binop;
16293 case FLOOR_DIV_EXPR:
16294 case CEIL_DIV_EXPR:
16295 case ROUND_DIV_EXPR:
16296 case TRUNC_DIV_EXPR:
16297 case EXACT_DIV_EXPR:
16298 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16299 return 0;
16300 op = DW_OP_div;
16301 goto do_binop;
16303 case MINUS_EXPR:
16304 op = DW_OP_minus;
16305 goto do_binop;
16307 case FLOOR_MOD_EXPR:
16308 case CEIL_MOD_EXPR:
16309 case ROUND_MOD_EXPR:
16310 case TRUNC_MOD_EXPR:
16311 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16313 op = DW_OP_mod;
16314 goto do_binop;
16316 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16317 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16318 if (list_ret == 0 || list_ret1 == 0)
16319 return 0;
16321 add_loc_list (&list_ret, list_ret1);
16322 if (list_ret == 0)
16323 return 0;
16324 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16325 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16326 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16327 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16328 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16329 break;
16331 case MULT_EXPR:
16332 op = DW_OP_mul;
16333 goto do_binop;
16335 case LSHIFT_EXPR:
16336 op = DW_OP_shl;
16337 goto do_binop;
16339 case RSHIFT_EXPR:
16340 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16341 goto do_binop;
16343 case POINTER_PLUS_EXPR:
16344 case PLUS_EXPR:
16345 do_plus:
16346 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16348 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16349 smarter to encode their opposite. The DW_OP_plus_uconst operation
16350 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
16351 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16352 bytes, Y being the size of the operation that pushes the opposite
16353 of the addend. So let's choose the smallest representation. */
16354 const tree tree_addend = TREE_OPERAND (loc, 1);
16355 offset_int wi_addend;
16356 HOST_WIDE_INT shwi_addend;
16357 dw_loc_descr_ref loc_naddend;
16359 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16360 if (list_ret == 0)
16361 return 0;
16363 /* Try to get the literal to push. It is the opposite of the addend,
16364 so as we rely on wrapping during DWARF evaluation, first decode
16365 the literal as a "DWARF-sized" signed number. */
16366 wi_addend = wi::to_offset (tree_addend);
16367 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16368 shwi_addend = wi_addend.to_shwi ();
16369 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16370 ? int_loc_descriptor (-shwi_addend)
16371 : NULL;
16373 if (loc_naddend != NULL
16374 && ((unsigned) size_of_uleb128 (shwi_addend)
16375 > size_of_loc_descr (loc_naddend)))
16377 add_loc_descr_to_each (list_ret, loc_naddend);
16378 add_loc_descr_to_each (list_ret,
16379 new_loc_descr (DW_OP_minus, 0, 0));
16381 else
16383 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16385 loc_naddend = loc_cur;
16386 loc_cur = loc_cur->dw_loc_next;
16387 ggc_free (loc_naddend);
16389 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16391 break;
16394 op = DW_OP_plus;
16395 goto do_binop;
16397 case LE_EXPR:
16398 op = DW_OP_le;
16399 goto do_comp_binop;
16401 case GE_EXPR:
16402 op = DW_OP_ge;
16403 goto do_comp_binop;
16405 case LT_EXPR:
16406 op = DW_OP_lt;
16407 goto do_comp_binop;
16409 case GT_EXPR:
16410 op = DW_OP_gt;
16411 goto do_comp_binop;
16413 do_comp_binop:
16414 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16416 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16417 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16418 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16419 TREE_CODE (loc));
16420 break;
16422 else
16423 goto do_binop;
16425 case EQ_EXPR:
16426 op = DW_OP_eq;
16427 goto do_binop;
16429 case NE_EXPR:
16430 op = DW_OP_ne;
16431 goto do_binop;
16433 do_binop:
16434 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16435 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16436 if (list_ret == 0 || list_ret1 == 0)
16437 return 0;
16439 add_loc_list (&list_ret, list_ret1);
16440 if (list_ret == 0)
16441 return 0;
16442 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16443 break;
16445 case TRUTH_NOT_EXPR:
16446 case BIT_NOT_EXPR:
16447 op = DW_OP_not;
16448 goto do_unop;
16450 case ABS_EXPR:
16451 op = DW_OP_abs;
16452 goto do_unop;
16454 case NEGATE_EXPR:
16455 op = DW_OP_neg;
16456 goto do_unop;
16458 do_unop:
16459 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16460 if (list_ret == 0)
16461 return 0;
16463 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16464 break;
16466 case MIN_EXPR:
16467 case MAX_EXPR:
16469 const enum tree_code code =
16470 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16472 loc = build3 (COND_EXPR, TREE_TYPE (loc),
16473 build2 (code, integer_type_node,
16474 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16475 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16478 /* fall through */
16480 case COND_EXPR:
16482 dw_loc_descr_ref lhs
16483 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16484 dw_loc_list_ref rhs
16485 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16486 dw_loc_descr_ref bra_node, jump_node, tmp;
16488 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16489 if (list_ret == 0 || lhs == 0 || rhs == 0)
16490 return 0;
16492 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16493 add_loc_descr_to_each (list_ret, bra_node);
16495 add_loc_list (&list_ret, rhs);
16496 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16497 add_loc_descr_to_each (list_ret, jump_node);
16499 add_loc_descr_to_each (list_ret, lhs);
16500 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16501 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16503 /* ??? Need a node to point the skip at. Use a nop. */
16504 tmp = new_loc_descr (DW_OP_nop, 0, 0);
16505 add_loc_descr_to_each (list_ret, tmp);
16506 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16507 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16509 break;
16511 case FIX_TRUNC_EXPR:
16512 return 0;
16514 default:
16515 /* Leave front-end specific codes as simply unknown. This comes
16516 up, for instance, with the C STMT_EXPR. */
16517 if ((unsigned int) TREE_CODE (loc)
16518 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16520 expansion_failed (loc, NULL_RTX,
16521 "language specific tree node");
16522 return 0;
16525 /* Otherwise this is a generic code; we should just lists all of
16526 these explicitly. We forgot one. */
16527 if (flag_checking)
16528 gcc_unreachable ();
16530 /* In a release build, we want to degrade gracefully: better to
16531 generate incomplete debugging information than to crash. */
16532 return NULL;
16535 if (!ret && !list_ret)
16536 return 0;
16538 if (want_address == 2 && !have_address
16539 && (dwarf_version >= 4 || !dwarf_strict))
16541 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16543 expansion_failed (loc, NULL_RTX,
16544 "DWARF address size mismatch");
16545 return 0;
16547 if (ret)
16548 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16549 else
16550 add_loc_descr_to_each (list_ret,
16551 new_loc_descr (DW_OP_stack_value, 0, 0));
16552 have_address = 1;
16554 /* Show if we can't fill the request for an address. */
16555 if (want_address && !have_address)
16557 expansion_failed (loc, NULL_RTX,
16558 "Want address and only have value");
16559 return 0;
16562 gcc_assert (!ret || !list_ret);
16564 /* If we've got an address and don't want one, dereference. */
16565 if (!want_address && have_address)
16567 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16569 if (size > DWARF2_ADDR_SIZE || size == -1)
16571 expansion_failed (loc, NULL_RTX,
16572 "DWARF address size mismatch");
16573 return 0;
16575 else if (size == DWARF2_ADDR_SIZE)
16576 op = DW_OP_deref;
16577 else
16578 op = DW_OP_deref_size;
16580 if (ret)
16581 add_loc_descr (&ret, new_loc_descr (op, size, 0));
16582 else
16583 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16585 if (ret)
16586 list_ret = new_loc_list (ret, NULL, NULL, NULL);
16588 return list_ret;
16591 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16592 expressions. */
16594 static dw_loc_list_ref
16595 loc_list_from_tree (tree loc, int want_address,
16596 const struct loc_descr_context *context)
16598 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16600 for (dw_loc_list_ref loc_cur = result;
16601 loc_cur != NULL; loc_cur =
16602 loc_cur->dw_loc_next)
16603 loc_descr_without_nops (loc_cur->expr);
16604 return result;
16607 /* Same as above but return only single location expression. */
16608 static dw_loc_descr_ref
16609 loc_descriptor_from_tree (tree loc, int want_address,
16610 const struct loc_descr_context *context)
16612 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16613 if (!ret)
16614 return NULL;
16615 if (ret->dw_loc_next)
16617 expansion_failed (loc, NULL_RTX,
16618 "Location list where only loc descriptor needed");
16619 return NULL;
16621 return ret->expr;
16624 /* Given a value, round it up to the lowest multiple of `boundary'
16625 which is not less than the value itself. */
16627 static inline HOST_WIDE_INT
16628 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16630 return (((value + boundary - 1) / boundary) * boundary);
16633 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16634 pointer to the declared type for the relevant field variable, or return
16635 `integer_type_node' if the given node turns out to be an
16636 ERROR_MARK node. */
16638 static inline tree
16639 field_type (const_tree decl)
16641 tree type;
16643 if (TREE_CODE (decl) == ERROR_MARK)
16644 return integer_type_node;
16646 type = DECL_BIT_FIELD_TYPE (decl);
16647 if (type == NULL_TREE)
16648 type = TREE_TYPE (decl);
16650 return type;
16653 /* Given a pointer to a tree node, return the alignment in bits for
16654 it, or else return BITS_PER_WORD if the node actually turns out to
16655 be an ERROR_MARK node. */
16657 static inline unsigned
16658 simple_type_align_in_bits (const_tree type)
16660 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16663 static inline unsigned
16664 simple_decl_align_in_bits (const_tree decl)
16666 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16669 /* Return the result of rounding T up to ALIGN. */
16671 static inline offset_int
16672 round_up_to_align (const offset_int &t, unsigned int align)
16674 return wi::udiv_trunc (t + align - 1, align) * align;
16677 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
16678 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
16679 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
16680 if we fail to return the size in one of these two forms. */
16682 static dw_loc_descr_ref
16683 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16685 tree tree_size;
16686 struct loc_descr_context ctx;
16688 /* Return a constant integer in priority, if possible. */
16689 *cst_size = int_size_in_bytes (type);
16690 if (*cst_size != -1)
16691 return NULL;
16693 ctx.context_type = const_cast<tree> (type);
16694 ctx.base_decl = NULL_TREE;
16695 ctx.dpi = NULL;
16697 type = TYPE_MAIN_VARIANT (type);
16698 tree_size = TYPE_SIZE_UNIT (type);
16699 return ((tree_size != NULL_TREE)
16700 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16701 : NULL);
16704 /* Helper structure for RECORD_TYPE processing. */
16705 struct vlr_context
16707 /* Root RECORD_TYPE. It is needed to generate data member location
16708 descriptions in variable-length records (VLR), but also to cope with
16709 variants, which are composed of nested structures multiplexed with
16710 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
16711 function processing a FIELD_DECL, it is required to be non null. */
16712 tree struct_type;
16713 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16714 QUAL_UNION_TYPE), this holds an expression that computes the offset for
16715 this variant part as part of the root record (in storage units). For
16716 regular records, it must be NULL_TREE. */
16717 tree variant_part_offset;
16720 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16721 addressed byte of the "containing object" for the given FIELD_DECL. If
16722 possible, return a native constant through CST_OFFSET (in which case NULL is
16723 returned); otherwise return a DWARF expression that computes the offset.
16725 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16726 that offset is, either because the argument turns out to be a pointer to an
16727 ERROR_MARK node, or because the offset expression is too complex for us.
16729 CTX is required: see the comment for VLR_CONTEXT. */
16731 static dw_loc_descr_ref
16732 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16733 HOST_WIDE_INT *cst_offset)
16735 offset_int object_offset_in_bits;
16736 offset_int object_offset_in_bytes;
16737 offset_int bitpos_int;
16738 bool is_byte_offset_cst, is_bit_offset_cst;
16739 tree tree_result;
16740 dw_loc_list_ref loc_result;
16742 *cst_offset = 0;
16744 if (TREE_CODE (decl) == ERROR_MARK)
16745 return NULL;
16746 else
16747 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16749 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16750 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16752 /* We cannot handle variable bit offsets at the moment, so abort if it's the
16753 case. */
16754 if (is_bit_offset_cst)
16755 return NULL;
16757 #ifdef PCC_BITFIELD_TYPE_MATTERS
16758 /* We used to handle only constant offsets in all cases. Now, we handle
16759 properly dynamic byte offsets only when PCC bitfield type doesn't
16760 matter. */
16761 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16763 tree type;
16764 tree field_size_tree;
16765 offset_int deepest_bitpos;
16766 offset_int field_size_in_bits;
16767 unsigned int type_align_in_bits;
16768 unsigned int decl_align_in_bits;
16769 offset_int type_size_in_bits;
16771 bitpos_int = wi::to_offset (bit_position (decl));
16772 type = field_type (decl);
16773 type_size_in_bits = offset_int_type_size_in_bits (type);
16774 type_align_in_bits = simple_type_align_in_bits (type);
16776 field_size_tree = DECL_SIZE (decl);
16778 /* The size could be unspecified if there was an error, or for
16779 a flexible array member. */
16780 if (!field_size_tree)
16781 field_size_tree = bitsize_zero_node;
16783 /* If the size of the field is not constant, use the type size. */
16784 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16785 field_size_in_bits = wi::to_offset (field_size_tree);
16786 else
16787 field_size_in_bits = type_size_in_bits;
16789 decl_align_in_bits = simple_decl_align_in_bits (decl);
16791 /* The GCC front-end doesn't make any attempt to keep track of the
16792 starting bit offset (relative to the start of the containing
16793 structure type) of the hypothetical "containing object" for a
16794 bit-field. Thus, when computing the byte offset value for the
16795 start of the "containing object" of a bit-field, we must deduce
16796 this information on our own. This can be rather tricky to do in
16797 some cases. For example, handling the following structure type
16798 definition when compiling for an i386/i486 target (which only
16799 aligns long long's to 32-bit boundaries) can be very tricky:
16801 struct S { int field1; long long field2:31; };
16803 Fortunately, there is a simple rule-of-thumb which can be used
16804 in such cases. When compiling for an i386/i486, GCC will
16805 allocate 8 bytes for the structure shown above. It decides to
16806 do this based upon one simple rule for bit-field allocation.
16807 GCC allocates each "containing object" for each bit-field at
16808 the first (i.e. lowest addressed) legitimate alignment boundary
16809 (based upon the required minimum alignment for the declared
16810 type of the field) which it can possibly use, subject to the
16811 condition that there is still enough available space remaining
16812 in the containing object (when allocated at the selected point)
16813 to fully accommodate all of the bits of the bit-field itself.
16815 This simple rule makes it obvious why GCC allocates 8 bytes for
16816 each object of the structure type shown above. When looking
16817 for a place to allocate the "containing object" for `field2',
16818 the compiler simply tries to allocate a 64-bit "containing
16819 object" at each successive 32-bit boundary (starting at zero)
16820 until it finds a place to allocate that 64- bit field such that
16821 at least 31 contiguous (and previously unallocated) bits remain
16822 within that selected 64 bit field. (As it turns out, for the
16823 example above, the compiler finds it is OK to allocate the
16824 "containing object" 64-bit field at bit-offset zero within the
16825 structure type.)
16827 Here we attempt to work backwards from the limited set of facts
16828 we're given, and we try to deduce from those facts, where GCC
16829 must have believed that the containing object started (within
16830 the structure type). The value we deduce is then used (by the
16831 callers of this routine) to generate DW_AT_location and
16832 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16833 the case of DW_AT_location, regular fields as well). */
16835 /* Figure out the bit-distance from the start of the structure to
16836 the "deepest" bit of the bit-field. */
16837 deepest_bitpos = bitpos_int + field_size_in_bits;
16839 /* This is the tricky part. Use some fancy footwork to deduce
16840 where the lowest addressed bit of the containing object must
16841 be. */
16842 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16844 /* Round up to type_align by default. This works best for
16845 bitfields. */
16846 object_offset_in_bits
16847 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16849 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16851 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16853 /* Round up to decl_align instead. */
16854 object_offset_in_bits
16855 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16858 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16860 tree_result = byte_position (decl);
16861 if (ctx->variant_part_offset != NULL_TREE)
16862 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16863 ctx->variant_part_offset, tree_result));
16865 /* If the byte offset is a constant, it's simplier to handle a native
16866 constant rather than a DWARF expression. */
16867 if (TREE_CODE (tree_result) == INTEGER_CST)
16869 *cst_offset = wi::to_offset (tree_result).to_shwi ();
16870 return NULL;
16872 struct loc_descr_context loc_ctx = {
16873 ctx->struct_type, /* context_type */
16874 NULL_TREE, /* base_decl */
16875 NULL /* dpi */
16877 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16879 /* We want a DWARF expression: abort if we only have a location list with
16880 multiple elements. */
16881 if (!loc_result || !single_element_loc_list_p (loc_result))
16882 return NULL;
16883 else
16884 return loc_result->expr;
16887 /* The following routines define various Dwarf attributes and any data
16888 associated with them. */
16890 /* Add a location description attribute value to a DIE.
16892 This emits location attributes suitable for whole variables and
16893 whole parameters. Note that the location attributes for struct fields are
16894 generated by the routine `data_member_location_attribute' below. */
16896 static inline void
16897 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16898 dw_loc_list_ref descr)
16900 if (descr == 0)
16901 return;
16902 if (single_element_loc_list_p (descr))
16903 add_AT_loc (die, attr_kind, descr->expr);
16904 else
16905 add_AT_loc_list (die, attr_kind, descr);
16908 /* Add DW_AT_accessibility attribute to DIE if needed. */
16910 static void
16911 add_accessibility_attribute (dw_die_ref die, tree decl)
16913 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16914 children, otherwise the default is DW_ACCESS_public. In DWARF2
16915 the default has always been DW_ACCESS_public. */
16916 if (TREE_PROTECTED (decl))
16917 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16918 else if (TREE_PRIVATE (decl))
16920 if (dwarf_version == 2
16921 || die->die_parent == NULL
16922 || die->die_parent->die_tag != DW_TAG_class_type)
16923 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16925 else if (dwarf_version > 2
16926 && die->die_parent
16927 && die->die_parent->die_tag == DW_TAG_class_type)
16928 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16931 /* Attach the specialized form of location attribute used for data members of
16932 struct and union types. In the special case of a FIELD_DECL node which
16933 represents a bit-field, the "offset" part of this special location
16934 descriptor must indicate the distance in bytes from the lowest-addressed
16935 byte of the containing struct or union type to the lowest-addressed byte of
16936 the "containing object" for the bit-field. (See the `field_byte_offset'
16937 function above).
16939 For any given bit-field, the "containing object" is a hypothetical object
16940 (of some integral or enum type) within which the given bit-field lives. The
16941 type of this hypothetical "containing object" is always the same as the
16942 declared type of the individual bit-field itself (for GCC anyway... the
16943 DWARF spec doesn't actually mandate this). Note that it is the size (in
16944 bytes) of the hypothetical "containing object" which will be given in the
16945 DW_AT_byte_size attribute for this bit-field. (See the
16946 `byte_size_attribute' function below.) It is also used when calculating the
16947 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16948 function below.)
16950 CTX is required: see the comment for VLR_CONTEXT. */
16952 static void
16953 add_data_member_location_attribute (dw_die_ref die,
16954 tree decl,
16955 struct vlr_context *ctx)
16957 HOST_WIDE_INT offset;
16958 dw_loc_descr_ref loc_descr = 0;
16960 if (TREE_CODE (decl) == TREE_BINFO)
16962 /* We're working on the TAG_inheritance for a base class. */
16963 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16965 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16966 aren't at a fixed offset from all (sub)objects of the same
16967 type. We need to extract the appropriate offset from our
16968 vtable. The following dwarf expression means
16970 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16972 This is specific to the V3 ABI, of course. */
16974 dw_loc_descr_ref tmp;
16976 /* Make a copy of the object address. */
16977 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16978 add_loc_descr (&loc_descr, tmp);
16980 /* Extract the vtable address. */
16981 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16982 add_loc_descr (&loc_descr, tmp);
16984 /* Calculate the address of the offset. */
16985 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16986 gcc_assert (offset < 0);
16988 tmp = int_loc_descriptor (-offset);
16989 add_loc_descr (&loc_descr, tmp);
16990 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16991 add_loc_descr (&loc_descr, tmp);
16993 /* Extract the offset. */
16994 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16995 add_loc_descr (&loc_descr, tmp);
16997 /* Add it to the object address. */
16998 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16999 add_loc_descr (&loc_descr, tmp);
17001 else
17002 offset = tree_to_shwi (BINFO_OFFSET (decl));
17004 else
17006 loc_descr = field_byte_offset (decl, ctx, &offset);
17008 /* If loc_descr is available then we know the field offset is dynamic.
17009 However, GDB does not handle dynamic field offsets very well at the
17010 moment. */
17011 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
17013 loc_descr = NULL;
17014 offset = 0;
17017 /* Data member location evalutation starts with the base address on the
17018 stack. Compute the field offset and add it to this base address. */
17019 else if (loc_descr != NULL)
17020 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
17023 if (! loc_descr)
17025 if (dwarf_version > 2)
17027 /* Don't need to output a location expression, just the constant. */
17028 if (offset < 0)
17029 add_AT_int (die, DW_AT_data_member_location, offset);
17030 else
17031 add_AT_unsigned (die, DW_AT_data_member_location, offset);
17032 return;
17034 else
17036 enum dwarf_location_atom op;
17038 /* The DWARF2 standard says that we should assume that the structure
17039 address is already on the stack, so we can specify a structure
17040 field address by using DW_OP_plus_uconst. */
17041 op = DW_OP_plus_uconst;
17042 loc_descr = new_loc_descr (op, offset, 0);
17046 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17049 /* Writes integer values to dw_vec_const array. */
17051 static void
17052 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17054 while (size != 0)
17056 *dest++ = val & 0xff;
17057 val >>= 8;
17058 --size;
17062 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
17064 static HOST_WIDE_INT
17065 extract_int (const unsigned char *src, unsigned int size)
17067 HOST_WIDE_INT val = 0;
17069 src += size;
17070 while (size != 0)
17072 val <<= 8;
17073 val |= *--src & 0xff;
17074 --size;
17076 return val;
17079 /* Writes wide_int values to dw_vec_const array. */
17081 static void
17082 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
17084 int i;
17086 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
17088 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
17089 return;
17092 /* We'd have to extend this code to support odd sizes. */
17093 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
17095 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
17097 if (WORDS_BIG_ENDIAN)
17098 for (i = n - 1; i >= 0; i--)
17100 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17101 dest += sizeof (HOST_WIDE_INT);
17103 else
17104 for (i = 0; i < n; i++)
17106 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17107 dest += sizeof (HOST_WIDE_INT);
17111 /* Writes floating point values to dw_vec_const array. */
17113 static void
17114 insert_float (const_rtx rtl, unsigned char *array)
17116 long val[4];
17117 int i;
17119 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
17121 /* real_to_target puts 32-bit pieces in each long. Pack them. */
17122 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17124 insert_int (val[i], 4, array);
17125 array += 4;
17129 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17130 does not have a "location" either in memory or in a register. These
17131 things can arise in GNU C when a constant is passed as an actual parameter
17132 to an inlined function. They can also arise in C++ where declared
17133 constants do not necessarily get memory "homes". */
17135 static bool
17136 add_const_value_attribute (dw_die_ref die, rtx rtl)
17138 switch (GET_CODE (rtl))
17140 case CONST_INT:
17142 HOST_WIDE_INT val = INTVAL (rtl);
17144 if (val < 0)
17145 add_AT_int (die, DW_AT_const_value, val);
17146 else
17147 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17149 return true;
17151 case CONST_WIDE_INT:
17153 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17154 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17155 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17156 wide_int w = wi::zext (w1, prec);
17157 add_AT_wide (die, DW_AT_const_value, w);
17159 return true;
17161 case CONST_DOUBLE:
17162 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17163 floating-point constant. A CONST_DOUBLE is used whenever the
17164 constant requires more than one word in order to be adequately
17165 represented. */
17167 machine_mode mode = GET_MODE (rtl);
17169 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17170 add_AT_double (die, DW_AT_const_value,
17171 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17172 else
17174 unsigned int length = GET_MODE_SIZE (mode);
17175 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17177 insert_float (rtl, array);
17178 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17181 return true;
17183 case CONST_VECTOR:
17185 machine_mode mode = GET_MODE (rtl);
17186 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17187 unsigned int length = CONST_VECTOR_NUNITS (rtl);
17188 unsigned char *array
17189 = ggc_vec_alloc<unsigned char> (length * elt_size);
17190 unsigned int i;
17191 unsigned char *p;
17192 machine_mode imode = GET_MODE_INNER (mode);
17194 switch (GET_MODE_CLASS (mode))
17196 case MODE_VECTOR_INT:
17197 for (i = 0, p = array; i < length; i++, p += elt_size)
17199 rtx elt = CONST_VECTOR_ELT (rtl, i);
17200 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17202 break;
17204 case MODE_VECTOR_FLOAT:
17205 for (i = 0, p = array; i < length; i++, p += elt_size)
17207 rtx elt = CONST_VECTOR_ELT (rtl, i);
17208 insert_float (elt, p);
17210 break;
17212 default:
17213 gcc_unreachable ();
17216 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17218 return true;
17220 case CONST_STRING:
17221 if (dwarf_version >= 4 || !dwarf_strict)
17223 dw_loc_descr_ref loc_result;
17224 resolve_one_addr (&rtl);
17225 rtl_addr:
17226 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17227 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17228 add_AT_loc (die, DW_AT_location, loc_result);
17229 vec_safe_push (used_rtx_array, rtl);
17230 return true;
17232 return false;
17234 case CONST:
17235 if (CONSTANT_P (XEXP (rtl, 0)))
17236 return add_const_value_attribute (die, XEXP (rtl, 0));
17237 /* FALLTHROUGH */
17238 case SYMBOL_REF:
17239 if (!const_ok_for_output (rtl))
17240 return false;
17241 /* FALLTHROUGH */
17242 case LABEL_REF:
17243 if (dwarf_version >= 4 || !dwarf_strict)
17244 goto rtl_addr;
17245 return false;
17247 case PLUS:
17248 /* In cases where an inlined instance of an inline function is passed
17249 the address of an `auto' variable (which is local to the caller) we
17250 can get a situation where the DECL_RTL of the artificial local
17251 variable (for the inlining) which acts as a stand-in for the
17252 corresponding formal parameter (of the inline function) will look
17253 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
17254 exactly a compile-time constant expression, but it isn't the address
17255 of the (artificial) local variable either. Rather, it represents the
17256 *value* which the artificial local variable always has during its
17257 lifetime. We currently have no way to represent such quasi-constant
17258 values in Dwarf, so for now we just punt and generate nothing. */
17259 return false;
17261 case HIGH:
17262 case CONST_FIXED:
17263 return false;
17265 case MEM:
17266 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17267 && MEM_READONLY_P (rtl)
17268 && GET_MODE (rtl) == BLKmode)
17270 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17271 return true;
17273 return false;
17275 default:
17276 /* No other kinds of rtx should be possible here. */
17277 gcc_unreachable ();
17279 return false;
17282 /* Determine whether the evaluation of EXPR references any variables
17283 or functions which aren't otherwise used (and therefore may not be
17284 output). */
17285 static tree
17286 reference_to_unused (tree * tp, int * walk_subtrees,
17287 void * data ATTRIBUTE_UNUSED)
17289 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17290 *walk_subtrees = 0;
17292 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17293 && ! TREE_ASM_WRITTEN (*tp))
17294 return *tp;
17295 /* ??? The C++ FE emits debug information for using decls, so
17296 putting gcc_unreachable here falls over. See PR31899. For now
17297 be conservative. */
17298 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
17299 return *tp;
17300 else if (VAR_P (*tp))
17302 varpool_node *node = varpool_node::get (*tp);
17303 if (!node || !node->definition)
17304 return *tp;
17306 else if (TREE_CODE (*tp) == FUNCTION_DECL
17307 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17309 /* The call graph machinery must have finished analyzing,
17310 optimizing and gimplifying the CU by now.
17311 So if *TP has no call graph node associated
17312 to it, it means *TP will not be emitted. */
17313 if (!cgraph_node::get (*tp))
17314 return *tp;
17316 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17317 return *tp;
17319 return NULL_TREE;
17322 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17323 for use in a later add_const_value_attribute call. */
17325 static rtx
17326 rtl_for_decl_init (tree init, tree type)
17328 rtx rtl = NULL_RTX;
17330 STRIP_NOPS (init);
17332 /* If a variable is initialized with a string constant without embedded
17333 zeros, build CONST_STRING. */
17334 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17336 tree enttype = TREE_TYPE (type);
17337 tree domain = TYPE_DOMAIN (type);
17338 machine_mode mode = TYPE_MODE (enttype);
17340 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17341 && domain
17342 && integer_zerop (TYPE_MIN_VALUE (domain))
17343 && compare_tree_int (TYPE_MAX_VALUE (domain),
17344 TREE_STRING_LENGTH (init) - 1) == 0
17345 && ((size_t) TREE_STRING_LENGTH (init)
17346 == strlen (TREE_STRING_POINTER (init)) + 1))
17348 rtl = gen_rtx_CONST_STRING (VOIDmode,
17349 ggc_strdup (TREE_STRING_POINTER (init)));
17350 rtl = gen_rtx_MEM (BLKmode, rtl);
17351 MEM_READONLY_P (rtl) = 1;
17354 /* Other aggregates, and complex values, could be represented using
17355 CONCAT: FIXME! */
17356 else if (AGGREGATE_TYPE_P (type)
17357 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17358 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17359 || TREE_CODE (type) == COMPLEX_TYPE)
17361 /* Vectors only work if their mode is supported by the target.
17362 FIXME: generic vectors ought to work too. */
17363 else if (TREE_CODE (type) == VECTOR_TYPE
17364 && !VECTOR_MODE_P (TYPE_MODE (type)))
17366 /* If the initializer is something that we know will expand into an
17367 immediate RTL constant, expand it now. We must be careful not to
17368 reference variables which won't be output. */
17369 else if (initializer_constant_valid_p (init, type)
17370 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17372 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17373 possible. */
17374 if (TREE_CODE (type) == VECTOR_TYPE)
17375 switch (TREE_CODE (init))
17377 case VECTOR_CST:
17378 break;
17379 case CONSTRUCTOR:
17380 if (TREE_CONSTANT (init))
17382 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17383 bool constant_p = true;
17384 tree value;
17385 unsigned HOST_WIDE_INT ix;
17387 /* Even when ctor is constant, it might contain non-*_CST
17388 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17389 belong into VECTOR_CST nodes. */
17390 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17391 if (!CONSTANT_CLASS_P (value))
17393 constant_p = false;
17394 break;
17397 if (constant_p)
17399 init = build_vector_from_ctor (type, elts);
17400 break;
17403 /* FALLTHRU */
17405 default:
17406 return NULL;
17409 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17411 /* If expand_expr returns a MEM, it wasn't immediate. */
17412 gcc_assert (!rtl || !MEM_P (rtl));
17415 return rtl;
17418 /* Generate RTL for the variable DECL to represent its location. */
17420 static rtx
17421 rtl_for_decl_location (tree decl)
17423 rtx rtl;
17425 /* Here we have to decide where we are going to say the parameter "lives"
17426 (as far as the debugger is concerned). We only have a couple of
17427 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17429 DECL_RTL normally indicates where the parameter lives during most of the
17430 activation of the function. If optimization is enabled however, this
17431 could be either NULL or else a pseudo-reg. Both of those cases indicate
17432 that the parameter doesn't really live anywhere (as far as the code
17433 generation parts of GCC are concerned) during most of the function's
17434 activation. That will happen (for example) if the parameter is never
17435 referenced within the function.
17437 We could just generate a location descriptor here for all non-NULL
17438 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17439 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17440 where DECL_RTL is NULL or is a pseudo-reg.
17442 Note however that we can only get away with using DECL_INCOMING_RTL as
17443 a backup substitute for DECL_RTL in certain limited cases. In cases
17444 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17445 we can be sure that the parameter was passed using the same type as it is
17446 declared to have within the function, and that its DECL_INCOMING_RTL
17447 points us to a place where a value of that type is passed.
17449 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17450 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17451 because in these cases DECL_INCOMING_RTL points us to a value of some
17452 type which is *different* from the type of the parameter itself. Thus,
17453 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17454 such cases, the debugger would end up (for example) trying to fetch a
17455 `float' from a place which actually contains the first part of a
17456 `double'. That would lead to really incorrect and confusing
17457 output at debug-time.
17459 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17460 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
17461 are a couple of exceptions however. On little-endian machines we can
17462 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17463 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17464 an integral type that is smaller than TREE_TYPE (decl). These cases arise
17465 when (on a little-endian machine) a non-prototyped function has a
17466 parameter declared to be of type `short' or `char'. In such cases,
17467 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17468 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17469 passed `int' value. If the debugger then uses that address to fetch
17470 a `short' or a `char' (on a little-endian machine) the result will be
17471 the correct data, so we allow for such exceptional cases below.
17473 Note that our goal here is to describe the place where the given formal
17474 parameter lives during most of the function's activation (i.e. between the
17475 end of the prologue and the start of the epilogue). We'll do that as best
17476 as we can. Note however that if the given formal parameter is modified
17477 sometime during the execution of the function, then a stack backtrace (at
17478 debug-time) will show the function as having been called with the *new*
17479 value rather than the value which was originally passed in. This happens
17480 rarely enough that it is not a major problem, but it *is* a problem, and
17481 I'd like to fix it.
17483 A future version of dwarf2out.c may generate two additional attributes for
17484 any given DW_TAG_formal_parameter DIE which will describe the "passed
17485 type" and the "passed location" for the given formal parameter in addition
17486 to the attributes we now generate to indicate the "declared type" and the
17487 "active location" for each parameter. This additional set of attributes
17488 could be used by debuggers for stack backtraces. Separately, note that
17489 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17490 This happens (for example) for inlined-instances of inline function formal
17491 parameters which are never referenced. This really shouldn't be
17492 happening. All PARM_DECL nodes should get valid non-NULL
17493 DECL_INCOMING_RTL values. FIXME. */
17495 /* Use DECL_RTL as the "location" unless we find something better. */
17496 rtl = DECL_RTL_IF_SET (decl);
17498 /* When generating abstract instances, ignore everything except
17499 constants, symbols living in memory, and symbols living in
17500 fixed registers. */
17501 if (! reload_completed)
17503 if (rtl
17504 && (CONSTANT_P (rtl)
17505 || (MEM_P (rtl)
17506 && CONSTANT_P (XEXP (rtl, 0)))
17507 || (REG_P (rtl)
17508 && VAR_P (decl)
17509 && TREE_STATIC (decl))))
17511 rtl = targetm.delegitimize_address (rtl);
17512 return rtl;
17514 rtl = NULL_RTX;
17516 else if (TREE_CODE (decl) == PARM_DECL)
17518 if (rtl == NULL_RTX
17519 || is_pseudo_reg (rtl)
17520 || (MEM_P (rtl)
17521 && is_pseudo_reg (XEXP (rtl, 0))
17522 && DECL_INCOMING_RTL (decl)
17523 && MEM_P (DECL_INCOMING_RTL (decl))
17524 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17526 tree declared_type = TREE_TYPE (decl);
17527 tree passed_type = DECL_ARG_TYPE (decl);
17528 machine_mode dmode = TYPE_MODE (declared_type);
17529 machine_mode pmode = TYPE_MODE (passed_type);
17531 /* This decl represents a formal parameter which was optimized out.
17532 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17533 all cases where (rtl == NULL_RTX) just below. */
17534 if (dmode == pmode)
17535 rtl = DECL_INCOMING_RTL (decl);
17536 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17537 && SCALAR_INT_MODE_P (dmode)
17538 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17539 && DECL_INCOMING_RTL (decl))
17541 rtx inc = DECL_INCOMING_RTL (decl);
17542 if (REG_P (inc))
17543 rtl = inc;
17544 else if (MEM_P (inc))
17546 if (BYTES_BIG_ENDIAN)
17547 rtl = adjust_address_nv (inc, dmode,
17548 GET_MODE_SIZE (pmode)
17549 - GET_MODE_SIZE (dmode));
17550 else
17551 rtl = inc;
17556 /* If the parm was passed in registers, but lives on the stack, then
17557 make a big endian correction if the mode of the type of the
17558 parameter is not the same as the mode of the rtl. */
17559 /* ??? This is the same series of checks that are made in dbxout.c before
17560 we reach the big endian correction code there. It isn't clear if all
17561 of these checks are necessary here, but keeping them all is the safe
17562 thing to do. */
17563 else if (MEM_P (rtl)
17564 && XEXP (rtl, 0) != const0_rtx
17565 && ! CONSTANT_P (XEXP (rtl, 0))
17566 /* Not passed in memory. */
17567 && !MEM_P (DECL_INCOMING_RTL (decl))
17568 /* Not passed by invisible reference. */
17569 && (!REG_P (XEXP (rtl, 0))
17570 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17571 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17572 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17573 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17574 #endif
17576 /* Big endian correction check. */
17577 && BYTES_BIG_ENDIAN
17578 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17579 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17580 < UNITS_PER_WORD))
17582 machine_mode addr_mode = get_address_mode (rtl);
17583 int offset = (UNITS_PER_WORD
17584 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17586 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17587 plus_constant (addr_mode, XEXP (rtl, 0), offset));
17590 else if (VAR_P (decl)
17591 && rtl
17592 && MEM_P (rtl)
17593 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17594 && BYTES_BIG_ENDIAN)
17596 machine_mode addr_mode = get_address_mode (rtl);
17597 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17598 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17600 /* If a variable is declared "register" yet is smaller than
17601 a register, then if we store the variable to memory, it
17602 looks like we're storing a register-sized value, when in
17603 fact we are not. We need to adjust the offset of the
17604 storage location to reflect the actual value's bytes,
17605 else gdb will not be able to display it. */
17606 if (rsize > dsize)
17607 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17608 plus_constant (addr_mode, XEXP (rtl, 0),
17609 rsize - dsize));
17612 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17613 and will have been substituted directly into all expressions that use it.
17614 C does not have such a concept, but C++ and other languages do. */
17615 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
17616 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17618 if (rtl)
17619 rtl = targetm.delegitimize_address (rtl);
17621 /* If we don't look past the constant pool, we risk emitting a
17622 reference to a constant pool entry that isn't referenced from
17623 code, and thus is not emitted. */
17624 if (rtl)
17625 rtl = avoid_constant_pool_reference (rtl);
17627 /* Try harder to get a rtl. If this symbol ends up not being emitted
17628 in the current CU, resolve_addr will remove the expression referencing
17629 it. */
17630 if (rtl == NULL_RTX
17631 && VAR_P (decl)
17632 && !DECL_EXTERNAL (decl)
17633 && TREE_STATIC (decl)
17634 && DECL_NAME (decl)
17635 && !DECL_HARD_REGISTER (decl)
17636 && DECL_MODE (decl) != VOIDmode)
17638 rtl = make_decl_rtl_for_debug (decl);
17639 if (!MEM_P (rtl)
17640 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17641 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17642 rtl = NULL_RTX;
17645 return rtl;
17648 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
17649 returned. If so, the decl for the COMMON block is returned, and the
17650 value is the offset into the common block for the symbol. */
17652 static tree
17653 fortran_common (tree decl, HOST_WIDE_INT *value)
17655 tree val_expr, cvar;
17656 machine_mode mode;
17657 HOST_WIDE_INT bitsize, bitpos;
17658 tree offset;
17659 int unsignedp, reversep, volatilep = 0;
17661 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17662 it does not have a value (the offset into the common area), or if it
17663 is thread local (as opposed to global) then it isn't common, and shouldn't
17664 be handled as such. */
17665 if (!VAR_P (decl)
17666 || !TREE_STATIC (decl)
17667 || !DECL_HAS_VALUE_EXPR_P (decl)
17668 || !is_fortran ())
17669 return NULL_TREE;
17671 val_expr = DECL_VALUE_EXPR (decl);
17672 if (TREE_CODE (val_expr) != COMPONENT_REF)
17673 return NULL_TREE;
17675 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17676 &unsignedp, &reversep, &volatilep);
17678 if (cvar == NULL_TREE
17679 || !VAR_P (cvar)
17680 || DECL_ARTIFICIAL (cvar)
17681 || !TREE_PUBLIC (cvar))
17682 return NULL_TREE;
17684 *value = 0;
17685 if (offset != NULL)
17687 if (!tree_fits_shwi_p (offset))
17688 return NULL_TREE;
17689 *value = tree_to_shwi (offset);
17691 if (bitpos != 0)
17692 *value += bitpos / BITS_PER_UNIT;
17694 return cvar;
17697 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17698 data attribute for a variable or a parameter. We generate the
17699 DW_AT_const_value attribute only in those cases where the given variable
17700 or parameter does not have a true "location" either in memory or in a
17701 register. This can happen (for example) when a constant is passed as an
17702 actual argument in a call to an inline function. (It's possible that
17703 these things can crop up in other ways also.) Note that one type of
17704 constant value which can be passed into an inlined function is a constant
17705 pointer. This can happen for example if an actual argument in an inlined
17706 function call evaluates to a compile-time constant address.
17708 CACHE_P is true if it is worth caching the location list for DECL,
17709 so that future calls can reuse it rather than regenerate it from scratch.
17710 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17711 since we will need to refer to them each time the function is inlined. */
17713 static bool
17714 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17716 rtx rtl;
17717 dw_loc_list_ref list;
17718 var_loc_list *loc_list;
17719 cached_dw_loc_list *cache;
17721 if (early_dwarf)
17722 return false;
17724 if (TREE_CODE (decl) == ERROR_MARK)
17725 return false;
17727 if (get_AT (die, DW_AT_location)
17728 || get_AT (die, DW_AT_const_value))
17729 return true;
17731 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
17732 || TREE_CODE (decl) == RESULT_DECL);
17734 /* Try to get some constant RTL for this decl, and use that as the value of
17735 the location. */
17737 rtl = rtl_for_decl_location (decl);
17738 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17739 && add_const_value_attribute (die, rtl))
17740 return true;
17742 /* See if we have single element location list that is equivalent to
17743 a constant value. That way we are better to use add_const_value_attribute
17744 rather than expanding constant value equivalent. */
17745 loc_list = lookup_decl_loc (decl);
17746 if (loc_list
17747 && loc_list->first
17748 && loc_list->first->next == NULL
17749 && NOTE_P (loc_list->first->loc)
17750 && NOTE_VAR_LOCATION (loc_list->first->loc)
17751 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17753 struct var_loc_node *node;
17755 node = loc_list->first;
17756 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17757 if (GET_CODE (rtl) == EXPR_LIST)
17758 rtl = XEXP (rtl, 0);
17759 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17760 && add_const_value_attribute (die, rtl))
17761 return true;
17763 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17764 list several times. See if we've already cached the contents. */
17765 list = NULL;
17766 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17767 cache_p = false;
17768 if (cache_p)
17770 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17771 if (cache)
17772 list = cache->loc_list;
17774 if (list == NULL)
17776 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17777 NULL);
17778 /* It is usually worth caching this result if the decl is from
17779 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17780 if (cache_p && list && list->dw_loc_next)
17782 cached_dw_loc_list **slot
17783 = cached_dw_loc_list_table->find_slot_with_hash (decl,
17784 DECL_UID (decl),
17785 INSERT);
17786 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17787 cache->decl_id = DECL_UID (decl);
17788 cache->loc_list = list;
17789 *slot = cache;
17792 if (list)
17794 add_AT_location_description (die, DW_AT_location, list);
17795 return true;
17797 /* None of that worked, so it must not really have a location;
17798 try adding a constant value attribute from the DECL_INITIAL. */
17799 return tree_add_const_value_attribute_for_decl (die, decl);
17802 /* Helper function for tree_add_const_value_attribute. Natively encode
17803 initializer INIT into an array. Return true if successful. */
17805 static bool
17806 native_encode_initializer (tree init, unsigned char *array, int size)
17808 tree type;
17810 if (init == NULL_TREE)
17811 return false;
17813 STRIP_NOPS (init);
17814 switch (TREE_CODE (init))
17816 case STRING_CST:
17817 type = TREE_TYPE (init);
17818 if (TREE_CODE (type) == ARRAY_TYPE)
17820 tree enttype = TREE_TYPE (type);
17821 machine_mode mode = TYPE_MODE (enttype);
17823 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17824 return false;
17825 if (int_size_in_bytes (type) != size)
17826 return false;
17827 if (size > TREE_STRING_LENGTH (init))
17829 memcpy (array, TREE_STRING_POINTER (init),
17830 TREE_STRING_LENGTH (init));
17831 memset (array + TREE_STRING_LENGTH (init),
17832 '\0', size - TREE_STRING_LENGTH (init));
17834 else
17835 memcpy (array, TREE_STRING_POINTER (init), size);
17836 return true;
17838 return false;
17839 case CONSTRUCTOR:
17840 type = TREE_TYPE (init);
17841 if (int_size_in_bytes (type) != size)
17842 return false;
17843 if (TREE_CODE (type) == ARRAY_TYPE)
17845 HOST_WIDE_INT min_index;
17846 unsigned HOST_WIDE_INT cnt;
17847 int curpos = 0, fieldsize;
17848 constructor_elt *ce;
17850 if (TYPE_DOMAIN (type) == NULL_TREE
17851 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17852 return false;
17854 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17855 if (fieldsize <= 0)
17856 return false;
17858 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17859 memset (array, '\0', size);
17860 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17862 tree val = ce->value;
17863 tree index = ce->index;
17864 int pos = curpos;
17865 if (index && TREE_CODE (index) == RANGE_EXPR)
17866 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17867 * fieldsize;
17868 else if (index)
17869 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17871 if (val)
17873 STRIP_NOPS (val);
17874 if (!native_encode_initializer (val, array + pos, fieldsize))
17875 return false;
17877 curpos = pos + fieldsize;
17878 if (index && TREE_CODE (index) == RANGE_EXPR)
17880 int count = tree_to_shwi (TREE_OPERAND (index, 1))
17881 - tree_to_shwi (TREE_OPERAND (index, 0));
17882 while (count-- > 0)
17884 if (val)
17885 memcpy (array + curpos, array + pos, fieldsize);
17886 curpos += fieldsize;
17889 gcc_assert (curpos <= size);
17891 return true;
17893 else if (TREE_CODE (type) == RECORD_TYPE
17894 || TREE_CODE (type) == UNION_TYPE)
17896 tree field = NULL_TREE;
17897 unsigned HOST_WIDE_INT cnt;
17898 constructor_elt *ce;
17900 if (int_size_in_bytes (type) != size)
17901 return false;
17903 if (TREE_CODE (type) == RECORD_TYPE)
17904 field = TYPE_FIELDS (type);
17906 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17908 tree val = ce->value;
17909 int pos, fieldsize;
17911 if (ce->index != 0)
17912 field = ce->index;
17914 if (val)
17915 STRIP_NOPS (val);
17917 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17918 return false;
17920 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17921 && TYPE_DOMAIN (TREE_TYPE (field))
17922 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17923 return false;
17924 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17925 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17926 return false;
17927 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17928 pos = int_byte_position (field);
17929 gcc_assert (pos + fieldsize <= size);
17930 if (val && fieldsize != 0
17931 && !native_encode_initializer (val, array + pos, fieldsize))
17932 return false;
17934 return true;
17936 return false;
17937 case VIEW_CONVERT_EXPR:
17938 case NON_LVALUE_EXPR:
17939 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17940 default:
17941 return native_encode_expr (init, array, size) == size;
17945 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17946 attribute is the const value T. */
17948 static bool
17949 tree_add_const_value_attribute (dw_die_ref die, tree t)
17951 tree init;
17952 tree type = TREE_TYPE (t);
17953 rtx rtl;
17955 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17956 return false;
17958 init = t;
17959 gcc_assert (!DECL_P (init));
17961 rtl = rtl_for_decl_init (init, type);
17962 if (rtl)
17963 return add_const_value_attribute (die, rtl);
17964 /* If the host and target are sane, try harder. */
17965 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17966 && initializer_constant_valid_p (init, type))
17968 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17969 if (size > 0 && (int) size == size)
17971 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17973 if (native_encode_initializer (init, array, size))
17975 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17976 return true;
17978 ggc_free (array);
17981 return false;
17984 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17985 attribute is the const value of T, where T is an integral constant
17986 variable with static storage duration
17987 (so it can't be a PARM_DECL or a RESULT_DECL). */
17989 static bool
17990 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17993 if (!decl
17994 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
17995 || (VAR_P (decl) && !TREE_STATIC (decl)))
17996 return false;
17998 if (TREE_READONLY (decl)
17999 && ! TREE_THIS_VOLATILE (decl)
18000 && DECL_INITIAL (decl))
18001 /* OK */;
18002 else
18003 return false;
18005 /* Don't add DW_AT_const_value if abstract origin already has one. */
18006 if (get_AT (var_die, DW_AT_const_value))
18007 return false;
18009 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
18012 /* Convert the CFI instructions for the current function into a
18013 location list. This is used for DW_AT_frame_base when we targeting
18014 a dwarf2 consumer that does not support the dwarf3
18015 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
18016 expressions. */
18018 static dw_loc_list_ref
18019 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
18021 int ix;
18022 dw_fde_ref fde;
18023 dw_loc_list_ref list, *list_tail;
18024 dw_cfi_ref cfi;
18025 dw_cfa_location last_cfa, next_cfa;
18026 const char *start_label, *last_label, *section;
18027 dw_cfa_location remember;
18029 fde = cfun->fde;
18030 gcc_assert (fde != NULL);
18032 section = secname_for_decl (current_function_decl);
18033 list_tail = &list;
18034 list = NULL;
18036 memset (&next_cfa, 0, sizeof (next_cfa));
18037 next_cfa.reg = INVALID_REGNUM;
18038 remember = next_cfa;
18040 start_label = fde->dw_fde_begin;
18042 /* ??? Bald assumption that the CIE opcode list does not contain
18043 advance opcodes. */
18044 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
18045 lookup_cfa_1 (cfi, &next_cfa, &remember);
18047 last_cfa = next_cfa;
18048 last_label = start_label;
18050 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18052 /* If the first partition contained no CFI adjustments, the
18053 CIE opcodes apply to the whole first partition. */
18054 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18055 fde->dw_fde_begin, fde->dw_fde_end, section);
18056 list_tail =&(*list_tail)->dw_loc_next;
18057 start_label = last_label = fde->dw_fde_second_begin;
18060 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
18062 switch (cfi->dw_cfi_opc)
18064 case DW_CFA_set_loc:
18065 case DW_CFA_advance_loc1:
18066 case DW_CFA_advance_loc2:
18067 case DW_CFA_advance_loc4:
18068 if (!cfa_equal_p (&last_cfa, &next_cfa))
18070 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18071 start_label, last_label, section);
18073 list_tail = &(*list_tail)->dw_loc_next;
18074 last_cfa = next_cfa;
18075 start_label = last_label;
18077 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18078 break;
18080 case DW_CFA_advance_loc:
18081 /* The encoding is complex enough that we should never emit this. */
18082 gcc_unreachable ();
18084 default:
18085 lookup_cfa_1 (cfi, &next_cfa, &remember);
18086 break;
18088 if (ix + 1 == fde->dw_fde_switch_cfi_index)
18090 if (!cfa_equal_p (&last_cfa, &next_cfa))
18092 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18093 start_label, last_label, section);
18095 list_tail = &(*list_tail)->dw_loc_next;
18096 last_cfa = next_cfa;
18097 start_label = last_label;
18099 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18100 start_label, fde->dw_fde_end, section);
18101 list_tail = &(*list_tail)->dw_loc_next;
18102 start_label = last_label = fde->dw_fde_second_begin;
18106 if (!cfa_equal_p (&last_cfa, &next_cfa))
18108 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18109 start_label, last_label, section);
18110 list_tail = &(*list_tail)->dw_loc_next;
18111 start_label = last_label;
18114 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18115 start_label,
18116 fde->dw_fde_second_begin
18117 ? fde->dw_fde_second_end : fde->dw_fde_end,
18118 section);
18120 if (list && list->dw_loc_next)
18121 gen_llsym (list);
18123 return list;
18126 /* Compute a displacement from the "steady-state frame pointer" to the
18127 frame base (often the same as the CFA), and store it in
18128 frame_pointer_fb_offset. OFFSET is added to the displacement
18129 before the latter is negated. */
18131 static void
18132 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18134 rtx reg, elim;
18136 #ifdef FRAME_POINTER_CFA_OFFSET
18137 reg = frame_pointer_rtx;
18138 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18139 #else
18140 reg = arg_pointer_rtx;
18141 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18142 #endif
18144 elim = (ira_use_lra_p
18145 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18146 : eliminate_regs (reg, VOIDmode, NULL_RTX));
18147 if (GET_CODE (elim) == PLUS)
18149 offset += INTVAL (XEXP (elim, 1));
18150 elim = XEXP (elim, 0);
18153 frame_pointer_fb_offset = -offset;
18155 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18156 in which to eliminate. This is because it's stack pointer isn't
18157 directly accessible as a register within the ISA. To work around
18158 this, assume that while we cannot provide a proper value for
18159 frame_pointer_fb_offset, we won't need one either. */
18160 frame_pointer_fb_offset_valid
18161 = ((SUPPORTS_STACK_ALIGNMENT
18162 && (elim == hard_frame_pointer_rtx
18163 || elim == stack_pointer_rtx))
18164 || elim == (frame_pointer_needed
18165 ? hard_frame_pointer_rtx
18166 : stack_pointer_rtx));
18169 /* Generate a DW_AT_name attribute given some string value to be included as
18170 the value of the attribute. */
18172 static void
18173 add_name_attribute (dw_die_ref die, const char *name_string)
18175 if (name_string != NULL && *name_string != 0)
18177 if (demangle_name_func)
18178 name_string = (*demangle_name_func) (name_string);
18180 add_AT_string (die, DW_AT_name, name_string);
18184 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18185 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18186 of TYPE accordingly.
18188 ??? This is a temporary measure until after we're able to generate
18189 regular DWARF for the complex Ada type system. */
18191 static void
18192 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18193 dw_die_ref context_die)
18195 tree dtype;
18196 dw_die_ref dtype_die;
18198 if (!lang_hooks.types.descriptive_type)
18199 return;
18201 dtype = lang_hooks.types.descriptive_type (type);
18202 if (!dtype)
18203 return;
18205 dtype_die = lookup_type_die (dtype);
18206 if (!dtype_die)
18208 gen_type_die (dtype, context_die);
18209 dtype_die = lookup_type_die (dtype);
18210 gcc_assert (dtype_die);
18213 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18216 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
18218 static const char *
18219 comp_dir_string (void)
18221 const char *wd;
18222 char *wd1;
18223 static const char *cached_wd = NULL;
18225 if (cached_wd != NULL)
18226 return cached_wd;
18228 wd = get_src_pwd ();
18229 if (wd == NULL)
18230 return NULL;
18232 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18234 int wdlen;
18236 wdlen = strlen (wd);
18237 wd1 = ggc_vec_alloc<char> (wdlen + 2);
18238 strcpy (wd1, wd);
18239 wd1 [wdlen] = DIR_SEPARATOR;
18240 wd1 [wdlen + 1] = 0;
18241 wd = wd1;
18244 cached_wd = remap_debug_filename (wd);
18245 return cached_wd;
18248 /* Generate a DW_AT_comp_dir attribute for DIE. */
18250 static void
18251 add_comp_dir_attribute (dw_die_ref die)
18253 const char * wd = comp_dir_string ();
18254 if (wd != NULL)
18255 add_AT_string (die, DW_AT_comp_dir, wd);
18258 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18259 pointer computation, ...), output a representation for that bound according
18260 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
18261 loc_list_from_tree for the meaning of CONTEXT. */
18263 static void
18264 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18265 int forms, const struct loc_descr_context *context)
18267 dw_die_ref context_die, decl_die;
18268 dw_loc_list_ref list;
18270 bool strip_conversions = true;
18272 while (strip_conversions)
18273 switch (TREE_CODE (value))
18275 case ERROR_MARK:
18276 case SAVE_EXPR:
18277 return;
18279 CASE_CONVERT:
18280 case VIEW_CONVERT_EXPR:
18281 value = TREE_OPERAND (value, 0);
18282 break;
18284 default:
18285 strip_conversions = false;
18286 break;
18289 /* If possible and permitted, output the attribute as a constant. */
18290 if ((forms & dw_scalar_form_constant) != 0
18291 && TREE_CODE (value) == INTEGER_CST)
18293 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18295 /* If HOST_WIDE_INT is big enough then represent the bound as
18296 a constant value. We need to choose a form based on
18297 whether the type is signed or unsigned. We cannot just
18298 call add_AT_unsigned if the value itself is positive
18299 (add_AT_unsigned might add the unsigned value encoded as
18300 DW_FORM_data[1248]). Some DWARF consumers will lookup the
18301 bounds type and then sign extend any unsigned values found
18302 for signed types. This is needed only for
18303 DW_AT_{lower,upper}_bound, since for most other attributes,
18304 consumers will treat DW_FORM_data[1248] as unsigned values,
18305 regardless of the underlying type. */
18306 if (prec <= HOST_BITS_PER_WIDE_INT
18307 || tree_fits_uhwi_p (value))
18309 if (TYPE_UNSIGNED (TREE_TYPE (value)))
18310 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18311 else
18312 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18314 else
18315 /* Otherwise represent the bound as an unsigned value with
18316 the precision of its type. The precision and signedness
18317 of the type will be necessary to re-interpret it
18318 unambiguously. */
18319 add_AT_wide (die, attr, value);
18320 return;
18323 /* Otherwise, if it's possible and permitted too, output a reference to
18324 another DIE. */
18325 if ((forms & dw_scalar_form_reference) != 0)
18327 tree decl = NULL_TREE;
18329 /* Some type attributes reference an outer type. For instance, the upper
18330 bound of an array may reference an embedding record (this happens in
18331 Ada). */
18332 if (TREE_CODE (value) == COMPONENT_REF
18333 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18334 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18335 decl = TREE_OPERAND (value, 1);
18337 else if (VAR_P (value)
18338 || TREE_CODE (value) == PARM_DECL
18339 || TREE_CODE (value) == RESULT_DECL)
18340 decl = value;
18342 if (decl != NULL_TREE)
18344 dw_die_ref decl_die = lookup_decl_die (decl);
18346 /* ??? Can this happen, or should the variable have been bound
18347 first? Probably it can, since I imagine that we try to create
18348 the types of parameters in the order in which they exist in
18349 the list, and won't have created a forward reference to a
18350 later parameter. */
18351 if (decl_die != NULL)
18353 add_AT_die_ref (die, attr, decl_die);
18354 return;
18359 /* Last chance: try to create a stack operation procedure to evaluate the
18360 value. Do nothing if even that is not possible or permitted. */
18361 if ((forms & dw_scalar_form_exprloc) == 0)
18362 return;
18364 list = loc_list_from_tree (value, 2, context);
18365 if (list == NULL || single_element_loc_list_p (list))
18367 /* If this attribute is not a reference nor constant, it is
18368 a DWARF expression rather than location description. For that
18369 loc_list_from_tree (value, 0, &context) is needed. */
18370 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18371 if (list2 && single_element_loc_list_p (list2))
18373 add_AT_loc (die, attr, list2->expr);
18374 return;
18378 /* If that failed to give a single element location list, fall back to
18379 outputting this as a reference... still if permitted. */
18380 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18381 return;
18383 if (current_function_decl == 0)
18384 context_die = comp_unit_die ();
18385 else
18386 context_die = lookup_decl_die (current_function_decl);
18388 decl_die = new_die (DW_TAG_variable, context_die, value);
18389 add_AT_flag (decl_die, DW_AT_artificial, 1);
18390 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18391 context_die);
18392 add_AT_location_description (decl_die, DW_AT_location, list);
18393 add_AT_die_ref (die, attr, decl_die);
18396 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18397 default. */
18399 static int
18400 lower_bound_default (void)
18402 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18404 case DW_LANG_C:
18405 case DW_LANG_C89:
18406 case DW_LANG_C99:
18407 case DW_LANG_C11:
18408 case DW_LANG_C_plus_plus:
18409 case DW_LANG_C_plus_plus_11:
18410 case DW_LANG_C_plus_plus_14:
18411 case DW_LANG_ObjC:
18412 case DW_LANG_ObjC_plus_plus:
18413 case DW_LANG_Java:
18414 return 0;
18415 case DW_LANG_Fortran77:
18416 case DW_LANG_Fortran90:
18417 case DW_LANG_Fortran95:
18418 case DW_LANG_Fortran03:
18419 case DW_LANG_Fortran08:
18420 return 1;
18421 case DW_LANG_UPC:
18422 case DW_LANG_D:
18423 case DW_LANG_Python:
18424 return dwarf_version >= 4 ? 0 : -1;
18425 case DW_LANG_Ada95:
18426 case DW_LANG_Ada83:
18427 case DW_LANG_Cobol74:
18428 case DW_LANG_Cobol85:
18429 case DW_LANG_Pascal83:
18430 case DW_LANG_Modula2:
18431 case DW_LANG_PLI:
18432 return dwarf_version >= 4 ? 1 : -1;
18433 default:
18434 return -1;
18438 /* Given a tree node describing an array bound (either lower or upper) output
18439 a representation for that bound. */
18441 static void
18442 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18443 tree bound, const struct loc_descr_context *context)
18445 int dflt;
18447 while (1)
18448 switch (TREE_CODE (bound))
18450 /* Strip all conversions. */
18451 CASE_CONVERT:
18452 case VIEW_CONVERT_EXPR:
18453 bound = TREE_OPERAND (bound, 0);
18454 break;
18456 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
18457 are even omitted when they are the default. */
18458 case INTEGER_CST:
18459 /* If the value for this bound is the default one, we can even omit the
18460 attribute. */
18461 if (bound_attr == DW_AT_lower_bound
18462 && tree_fits_shwi_p (bound)
18463 && (dflt = lower_bound_default ()) != -1
18464 && tree_to_shwi (bound) == dflt)
18465 return;
18467 /* FALLTHRU */
18469 default:
18470 /* Because of the complex interaction there can be with other GNAT
18471 encodings, GDB isn't ready yet to handle proper DWARF description
18472 for self-referencial subrange bounds: let GNAT encodings do the
18473 magic in such a case. */
18474 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18475 && contains_placeholder_p (bound))
18476 return;
18478 add_scalar_info (subrange_die, bound_attr, bound,
18479 dw_scalar_form_constant
18480 | dw_scalar_form_exprloc
18481 | dw_scalar_form_reference,
18482 context);
18483 return;
18487 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18488 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18489 Note that the block of subscript information for an array type also
18490 includes information about the element type of the given array type.
18492 This function reuses previously set type and bound information if
18493 available. */
18495 static void
18496 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18498 unsigned dimension_number;
18499 tree lower, upper;
18500 dw_die_ref child = type_die->die_child;
18502 for (dimension_number = 0;
18503 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18504 type = TREE_TYPE (type), dimension_number++)
18506 tree domain = TYPE_DOMAIN (type);
18508 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18509 break;
18511 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18512 and (in GNU C only) variable bounds. Handle all three forms
18513 here. */
18515 /* Find and reuse a previously generated DW_TAG_subrange_type if
18516 available.
18518 For multi-dimensional arrays, as we iterate through the
18519 various dimensions in the enclosing for loop above, we also
18520 iterate through the DIE children and pick at each
18521 DW_TAG_subrange_type previously generated (if available).
18522 Each child DW_TAG_subrange_type DIE describes the range of
18523 the current dimension. At this point we should have as many
18524 DW_TAG_subrange_type's as we have dimensions in the
18525 array. */
18526 dw_die_ref subrange_die = NULL;
18527 if (child)
18528 while (1)
18530 child = child->die_sib;
18531 if (child->die_tag == DW_TAG_subrange_type)
18532 subrange_die = child;
18533 if (child == type_die->die_child)
18535 /* If we wrapped around, stop looking next time. */
18536 child = NULL;
18537 break;
18539 if (child->die_tag == DW_TAG_subrange_type)
18540 break;
18542 if (!subrange_die)
18543 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18545 if (domain)
18547 /* We have an array type with specified bounds. */
18548 lower = TYPE_MIN_VALUE (domain);
18549 upper = TYPE_MAX_VALUE (domain);
18551 /* Define the index type. */
18552 if (TREE_TYPE (domain)
18553 && !get_AT (subrange_die, DW_AT_type))
18555 /* ??? This is probably an Ada unnamed subrange type. Ignore the
18556 TREE_TYPE field. We can't emit debug info for this
18557 because it is an unnamed integral type. */
18558 if (TREE_CODE (domain) == INTEGER_TYPE
18559 && TYPE_NAME (domain) == NULL_TREE
18560 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18561 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18563 else
18564 add_type_attribute (subrange_die, TREE_TYPE (domain),
18565 TYPE_UNQUALIFIED, false, type_die);
18568 /* ??? If upper is NULL, the array has unspecified length,
18569 but it does have a lower bound. This happens with Fortran
18570 dimension arr(N:*)
18571 Since the debugger is definitely going to need to know N
18572 to produce useful results, go ahead and output the lower
18573 bound solo, and hope the debugger can cope. */
18575 if (!get_AT (subrange_die, DW_AT_lower_bound))
18576 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18577 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18578 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18581 /* Otherwise we have an array type with an unspecified length. The
18582 DWARF-2 spec does not say how to handle this; let's just leave out the
18583 bounds. */
18587 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
18589 static void
18590 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18592 dw_die_ref decl_die;
18593 HOST_WIDE_INT size;
18594 dw_loc_descr_ref size_expr = NULL;
18596 switch (TREE_CODE (tree_node))
18598 case ERROR_MARK:
18599 size = 0;
18600 break;
18601 case ENUMERAL_TYPE:
18602 case RECORD_TYPE:
18603 case UNION_TYPE:
18604 case QUAL_UNION_TYPE:
18605 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18606 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18608 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18609 return;
18611 size_expr = type_byte_size (tree_node, &size);
18612 break;
18613 case FIELD_DECL:
18614 /* For a data member of a struct or union, the DW_AT_byte_size is
18615 generally given as the number of bytes normally allocated for an
18616 object of the *declared* type of the member itself. This is true
18617 even for bit-fields. */
18618 size = int_size_in_bytes (field_type (tree_node));
18619 break;
18620 default:
18621 gcc_unreachable ();
18624 /* Support for dynamically-sized objects was introduced by DWARFv3.
18625 At the moment, GDB does not handle variable byte sizes very well,
18626 though. */
18627 if ((dwarf_version >= 3 || !dwarf_strict)
18628 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18629 && size_expr != NULL)
18630 add_AT_loc (die, DW_AT_byte_size, size_expr);
18632 /* Note that `size' might be -1 when we get to this point. If it is, that
18633 indicates that the byte size of the entity in question is variable and
18634 that we could not generate a DWARF expression that computes it. */
18635 if (size >= 0)
18636 add_AT_unsigned (die, DW_AT_byte_size, size);
18639 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18640 which specifies the distance in bits from the highest order bit of the
18641 "containing object" for the bit-field to the highest order bit of the
18642 bit-field itself.
18644 For any given bit-field, the "containing object" is a hypothetical object
18645 (of some integral or enum type) within which the given bit-field lives. The
18646 type of this hypothetical "containing object" is always the same as the
18647 declared type of the individual bit-field itself. The determination of the
18648 exact location of the "containing object" for a bit-field is rather
18649 complicated. It's handled by the `field_byte_offset' function (above).
18651 CTX is required: see the comment for VLR_CONTEXT.
18653 Note that it is the size (in bytes) of the hypothetical "containing object"
18654 which will be given in the DW_AT_byte_size attribute for this bit-field.
18655 (See `byte_size_attribute' above). */
18657 static inline void
18658 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18660 HOST_WIDE_INT object_offset_in_bytes;
18661 tree original_type = DECL_BIT_FIELD_TYPE (decl);
18662 HOST_WIDE_INT bitpos_int;
18663 HOST_WIDE_INT highest_order_object_bit_offset;
18664 HOST_WIDE_INT highest_order_field_bit_offset;
18665 HOST_WIDE_INT bit_offset;
18667 field_byte_offset (decl, ctx, &object_offset_in_bytes);
18669 /* Must be a field and a bit field. */
18670 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18672 /* We can't yet handle bit-fields whose offsets are variable, so if we
18673 encounter such things, just return without generating any attribute
18674 whatsoever. Likewise for variable or too large size. */
18675 if (! tree_fits_shwi_p (bit_position (decl))
18676 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18677 return;
18679 bitpos_int = int_bit_position (decl);
18681 /* Note that the bit offset is always the distance (in bits) from the
18682 highest-order bit of the "containing object" to the highest-order bit of
18683 the bit-field itself. Since the "high-order end" of any object or field
18684 is different on big-endian and little-endian machines, the computation
18685 below must take account of these differences. */
18686 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18687 highest_order_field_bit_offset = bitpos_int;
18689 if (! BYTES_BIG_ENDIAN)
18691 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18692 highest_order_object_bit_offset +=
18693 simple_type_size_in_bits (original_type);
18696 bit_offset
18697 = (! BYTES_BIG_ENDIAN
18698 ? highest_order_object_bit_offset - highest_order_field_bit_offset
18699 : highest_order_field_bit_offset - highest_order_object_bit_offset);
18701 if (bit_offset < 0)
18702 add_AT_int (die, DW_AT_bit_offset, bit_offset);
18703 else
18704 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18707 /* For a FIELD_DECL node which represents a bit field, output an attribute
18708 which specifies the length in bits of the given field. */
18710 static inline void
18711 add_bit_size_attribute (dw_die_ref die, tree decl)
18713 /* Must be a field and a bit field. */
18714 gcc_assert (TREE_CODE (decl) == FIELD_DECL
18715 && DECL_BIT_FIELD_TYPE (decl));
18717 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18718 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18721 /* If the compiled language is ANSI C, then add a 'prototyped'
18722 attribute, if arg types are given for the parameters of a function. */
18724 static inline void
18725 add_prototyped_attribute (dw_die_ref die, tree func_type)
18727 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18729 case DW_LANG_C:
18730 case DW_LANG_C89:
18731 case DW_LANG_C99:
18732 case DW_LANG_C11:
18733 case DW_LANG_ObjC:
18734 if (prototype_p (func_type))
18735 add_AT_flag (die, DW_AT_prototyped, 1);
18736 break;
18737 default:
18738 break;
18742 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
18743 by looking in the type declaration, the object declaration equate table or
18744 the block mapping. */
18746 static inline dw_die_ref
18747 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18749 dw_die_ref origin_die = NULL;
18751 if (TREE_CODE (origin) != FUNCTION_DECL
18752 && TREE_CODE (origin) != BLOCK)
18754 /* We may have gotten separated from the block for the inlined
18755 function, if we're in an exception handler or some such; make
18756 sure that the abstract function has been written out.
18758 Doing this for nested functions is wrong, however; functions are
18759 distinct units, and our context might not even be inline. */
18760 tree fn = origin;
18762 if (TYPE_P (fn))
18763 fn = TYPE_STUB_DECL (fn);
18765 fn = decl_function_context (fn);
18766 if (fn)
18767 dwarf2out_abstract_function (fn);
18770 if (DECL_P (origin))
18771 origin_die = lookup_decl_die (origin);
18772 else if (TYPE_P (origin))
18773 origin_die = lookup_type_die (origin);
18774 else if (TREE_CODE (origin) == BLOCK)
18775 origin_die = BLOCK_DIE (origin);
18777 /* XXX: Functions that are never lowered don't always have correct block
18778 trees (in the case of java, they simply have no block tree, in some other
18779 languages). For these functions, there is nothing we can really do to
18780 output correct debug info for inlined functions in all cases. Rather
18781 than die, we'll just produce deficient debug info now, in that we will
18782 have variables without a proper abstract origin. In the future, when all
18783 functions are lowered, we should re-add a gcc_assert (origin_die)
18784 here. */
18786 if (origin_die)
18787 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18788 return origin_die;
18791 /* We do not currently support the pure_virtual attribute. */
18793 static inline void
18794 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18796 if (DECL_VINDEX (func_decl))
18798 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18800 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18801 add_AT_loc (die, DW_AT_vtable_elem_location,
18802 new_loc_descr (DW_OP_constu,
18803 tree_to_shwi (DECL_VINDEX (func_decl)),
18804 0));
18806 /* GNU extension: Record what type this method came from originally. */
18807 if (debug_info_level > DINFO_LEVEL_TERSE
18808 && DECL_CONTEXT (func_decl))
18809 add_AT_die_ref (die, DW_AT_containing_type,
18810 lookup_type_die (DECL_CONTEXT (func_decl)));
18814 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18815 given decl. This used to be a vendor extension until after DWARF 4
18816 standardized it. */
18818 static void
18819 add_linkage_attr (dw_die_ref die, tree decl)
18821 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18823 /* Mimic what assemble_name_raw does with a leading '*'. */
18824 if (name[0] == '*')
18825 name = &name[1];
18827 if (dwarf_version >= 4)
18828 add_AT_string (die, DW_AT_linkage_name, name);
18829 else
18830 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18833 /* Add source coordinate attributes for the given decl. */
18835 static void
18836 add_src_coords_attributes (dw_die_ref die, tree decl)
18838 expanded_location s;
18840 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18841 return;
18842 s = expand_location (DECL_SOURCE_LOCATION (decl));
18843 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18844 add_AT_unsigned (die, DW_AT_decl_line, s.line);
18847 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
18849 static void
18850 add_linkage_name_raw (dw_die_ref die, tree decl)
18852 /* Defer until we have an assembler name set. */
18853 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18855 limbo_die_node *asm_name;
18857 asm_name = ggc_cleared_alloc<limbo_die_node> ();
18858 asm_name->die = die;
18859 asm_name->created_for = decl;
18860 asm_name->next = deferred_asm_name;
18861 deferred_asm_name = asm_name;
18863 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18864 add_linkage_attr (die, decl);
18867 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
18869 static void
18870 add_linkage_name (dw_die_ref die, tree decl)
18872 if (debug_info_level > DINFO_LEVEL_NONE
18873 && VAR_OR_FUNCTION_DECL_P (decl)
18874 && TREE_PUBLIC (decl)
18875 && !(VAR_P (decl) && DECL_REGISTER (decl))
18876 && die->die_tag != DW_TAG_member)
18877 add_linkage_name_raw (die, decl);
18880 /* Add a DW_AT_name attribute and source coordinate attribute for the
18881 given decl, but only if it actually has a name. */
18883 static void
18884 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18886 tree decl_name;
18888 decl_name = DECL_NAME (decl);
18889 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18891 const char *name = dwarf2_name (decl, 0);
18892 if (name)
18893 add_name_attribute (die, name);
18894 if (! DECL_ARTIFICIAL (decl))
18895 add_src_coords_attributes (die, decl);
18897 add_linkage_name (die, decl);
18900 #ifdef VMS_DEBUGGING_INFO
18901 /* Get the function's name, as described by its RTL. This may be different
18902 from the DECL_NAME name used in the source file. */
18903 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18905 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18906 XEXP (DECL_RTL (decl), 0), false);
18907 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18909 #endif /* VMS_DEBUGGING_INFO */
18912 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
18914 static void
18915 add_discr_value (dw_die_ref die, dw_discr_value *value)
18917 dw_attr_node attr;
18919 attr.dw_attr = DW_AT_discr_value;
18920 attr.dw_attr_val.val_class = dw_val_class_discr_value;
18921 attr.dw_attr_val.val_entry = NULL;
18922 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18923 if (value->pos)
18924 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18925 else
18926 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18927 add_dwarf_attr (die, &attr);
18930 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
18932 static void
18933 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18935 dw_attr_node attr;
18937 attr.dw_attr = DW_AT_discr_list;
18938 attr.dw_attr_val.val_class = dw_val_class_discr_list;
18939 attr.dw_attr_val.val_entry = NULL;
18940 attr.dw_attr_val.v.val_discr_list = discr_list;
18941 add_dwarf_attr (die, &attr);
18944 static inline dw_discr_list_ref
18945 AT_discr_list (dw_attr_node *attr)
18947 return attr->dw_attr_val.v.val_discr_list;
18950 #ifdef VMS_DEBUGGING_INFO
18951 /* Output the debug main pointer die for VMS */
18953 void
18954 dwarf2out_vms_debug_main_pointer (void)
18956 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18957 dw_die_ref die;
18959 /* Allocate the VMS debug main subprogram die. */
18960 die = ggc_cleared_alloc<die_node> ();
18961 die->die_tag = DW_TAG_subprogram;
18962 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18963 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18964 current_function_funcdef_no);
18965 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18967 /* Make it the first child of comp_unit_die (). */
18968 die->die_parent = comp_unit_die ();
18969 if (comp_unit_die ()->die_child)
18971 die->die_sib = comp_unit_die ()->die_child->die_sib;
18972 comp_unit_die ()->die_child->die_sib = die;
18974 else
18976 die->die_sib = die;
18977 comp_unit_die ()->die_child = die;
18980 #endif /* VMS_DEBUGGING_INFO */
18982 /* Push a new declaration scope. */
18984 static void
18985 push_decl_scope (tree scope)
18987 vec_safe_push (decl_scope_table, scope);
18990 /* Pop a declaration scope. */
18992 static inline void
18993 pop_decl_scope (void)
18995 decl_scope_table->pop ();
18998 /* walk_tree helper function for uses_local_type, below. */
19000 static tree
19001 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
19003 if (!TYPE_P (*tp))
19004 *walk_subtrees = 0;
19005 else
19007 tree name = TYPE_NAME (*tp);
19008 if (name && DECL_P (name) && decl_function_context (name))
19009 return *tp;
19011 return NULL_TREE;
19014 /* If TYPE involves a function-local type (including a local typedef to a
19015 non-local type), returns that type; otherwise returns NULL_TREE. */
19017 static tree
19018 uses_local_type (tree type)
19020 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
19021 return used;
19024 /* Return the DIE for the scope that immediately contains this type.
19025 Non-named types that do not involve a function-local type get global
19026 scope. Named types nested in namespaces or other types get their
19027 containing scope. All other types (i.e. function-local named types) get
19028 the current active scope. */
19030 static dw_die_ref
19031 scope_die_for (tree t, dw_die_ref context_die)
19033 dw_die_ref scope_die = NULL;
19034 tree containing_scope;
19036 /* Non-types always go in the current scope. */
19037 gcc_assert (TYPE_P (t));
19039 /* Use the scope of the typedef, rather than the scope of the type
19040 it refers to. */
19041 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
19042 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
19043 else
19044 containing_scope = TYPE_CONTEXT (t);
19046 /* Use the containing namespace if there is one. */
19047 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
19049 if (context_die == lookup_decl_die (containing_scope))
19050 /* OK */;
19051 else if (debug_info_level > DINFO_LEVEL_TERSE)
19052 context_die = get_context_die (containing_scope);
19053 else
19054 containing_scope = NULL_TREE;
19057 /* Ignore function type "scopes" from the C frontend. They mean that
19058 a tagged type is local to a parmlist of a function declarator, but
19059 that isn't useful to DWARF. */
19060 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19061 containing_scope = NULL_TREE;
19063 if (SCOPE_FILE_SCOPE_P (containing_scope))
19065 /* If T uses a local type keep it local as well, to avoid references
19066 to function-local DIEs from outside the function. */
19067 if (current_function_decl && uses_local_type (t))
19068 scope_die = context_die;
19069 else
19070 scope_die = comp_unit_die ();
19072 else if (TYPE_P (containing_scope))
19074 /* For types, we can just look up the appropriate DIE. */
19075 if (debug_info_level > DINFO_LEVEL_TERSE)
19076 scope_die = get_context_die (containing_scope);
19077 else
19079 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19080 if (scope_die == NULL)
19081 scope_die = comp_unit_die ();
19084 else
19085 scope_die = context_die;
19087 return scope_die;
19090 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
19092 static inline int
19093 local_scope_p (dw_die_ref context_die)
19095 for (; context_die; context_die = context_die->die_parent)
19096 if (context_die->die_tag == DW_TAG_inlined_subroutine
19097 || context_die->die_tag == DW_TAG_subprogram)
19098 return 1;
19100 return 0;
19103 /* Returns nonzero if CONTEXT_DIE is a class. */
19105 static inline int
19106 class_scope_p (dw_die_ref context_die)
19108 return (context_die
19109 && (context_die->die_tag == DW_TAG_structure_type
19110 || context_die->die_tag == DW_TAG_class_type
19111 || context_die->die_tag == DW_TAG_interface_type
19112 || context_die->die_tag == DW_TAG_union_type));
19115 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19116 whether or not to treat a DIE in this context as a declaration. */
19118 static inline int
19119 class_or_namespace_scope_p (dw_die_ref context_die)
19121 return (class_scope_p (context_die)
19122 || (context_die && context_die->die_tag == DW_TAG_namespace));
19125 /* Many forms of DIEs require a "type description" attribute. This
19126 routine locates the proper "type descriptor" die for the type given
19127 by 'type' plus any additional qualifiers given by 'cv_quals', and
19128 adds a DW_AT_type attribute below the given die. */
19130 static void
19131 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19132 bool reverse, dw_die_ref context_die)
19134 enum tree_code code = TREE_CODE (type);
19135 dw_die_ref type_die = NULL;
19137 /* ??? If this type is an unnamed subrange type of an integral, floating-point
19138 or fixed-point type, use the inner type. This is because we have no
19139 support for unnamed types in base_type_die. This can happen if this is
19140 an Ada subrange type. Correct solution is emit a subrange type die. */
19141 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19142 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19143 type = TREE_TYPE (type), code = TREE_CODE (type);
19145 if (code == ERROR_MARK
19146 /* Handle a special case. For functions whose return type is void, we
19147 generate *no* type attribute. (Note that no object may have type
19148 `void', so this only applies to function return types). */
19149 || code == VOID_TYPE)
19150 return;
19152 type_die = modified_type_die (type,
19153 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19154 reverse,
19155 context_die);
19157 if (type_die != NULL)
19158 add_AT_die_ref (object_die, DW_AT_type, type_die);
19161 /* Given an object die, add the calling convention attribute for the
19162 function call type. */
19163 static void
19164 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19166 enum dwarf_calling_convention value = DW_CC_normal;
19168 value = ((enum dwarf_calling_convention)
19169 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19171 if (is_fortran ()
19172 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19174 /* DWARF 2 doesn't provide a way to identify a program's source-level
19175 entry point. DW_AT_calling_convention attributes are only meant
19176 to describe functions' calling conventions. However, lacking a
19177 better way to signal the Fortran main program, we used this for
19178 a long time, following existing custom. Now, DWARF 4 has
19179 DW_AT_main_subprogram, which we add below, but some tools still
19180 rely on the old way, which we thus keep. */
19181 value = DW_CC_program;
19183 if (dwarf_version >= 4 || !dwarf_strict)
19184 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19187 /* Only add the attribute if the backend requests it, and
19188 is not DW_CC_normal. */
19189 if (value && (value != DW_CC_normal))
19190 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19193 /* Given a tree pointer to a struct, class, union, or enum type node, return
19194 a pointer to the (string) tag name for the given type, or zero if the type
19195 was declared without a tag. */
19197 static const char *
19198 type_tag (const_tree type)
19200 const char *name = 0;
19202 if (TYPE_NAME (type) != 0)
19204 tree t = 0;
19206 /* Find the IDENTIFIER_NODE for the type name. */
19207 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19208 && !TYPE_NAMELESS (type))
19209 t = TYPE_NAME (type);
19211 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19212 a TYPE_DECL node, regardless of whether or not a `typedef' was
19213 involved. */
19214 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19215 && ! DECL_IGNORED_P (TYPE_NAME (type)))
19217 /* We want to be extra verbose. Don't call dwarf_name if
19218 DECL_NAME isn't set. The default hook for decl_printable_name
19219 doesn't like that, and in this context it's correct to return
19220 0, instead of "<anonymous>" or the like. */
19221 if (DECL_NAME (TYPE_NAME (type))
19222 && !DECL_NAMELESS (TYPE_NAME (type)))
19223 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19226 /* Now get the name as a string, or invent one. */
19227 if (!name && t != 0)
19228 name = IDENTIFIER_POINTER (t);
19231 return (name == 0 || *name == '\0') ? 0 : name;
19234 /* Return the type associated with a data member, make a special check
19235 for bit field types. */
19237 static inline tree
19238 member_declared_type (const_tree member)
19240 return (DECL_BIT_FIELD_TYPE (member)
19241 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19244 /* Get the decl's label, as described by its RTL. This may be different
19245 from the DECL_NAME name used in the source file. */
19247 #if 0
19248 static const char *
19249 decl_start_label (tree decl)
19251 rtx x;
19252 const char *fnname;
19254 x = DECL_RTL (decl);
19255 gcc_assert (MEM_P (x));
19257 x = XEXP (x, 0);
19258 gcc_assert (GET_CODE (x) == SYMBOL_REF);
19260 fnname = XSTR (x, 0);
19261 return fnname;
19263 #endif
19265 /* For variable-length arrays that have been previously generated, but
19266 may be incomplete due to missing subscript info, fill the subscript
19267 info. Return TRUE if this is one of those cases. */
19268 static bool
19269 fill_variable_array_bounds (tree type)
19271 if (TREE_ASM_WRITTEN (type)
19272 && TREE_CODE (type) == ARRAY_TYPE
19273 && variably_modified_type_p (type, NULL))
19275 dw_die_ref array_die = lookup_type_die (type);
19276 if (!array_die)
19277 return false;
19278 add_subscript_info (array_die, type, !is_ada ());
19279 return true;
19281 return false;
19284 /* These routines generate the internal representation of the DIE's for
19285 the compilation unit. Debugging information is collected by walking
19286 the declaration trees passed in from dwarf2out_decl(). */
19288 static void
19289 gen_array_type_die (tree type, dw_die_ref context_die)
19291 dw_die_ref array_die;
19293 /* GNU compilers represent multidimensional array types as sequences of one
19294 dimensional array types whose element types are themselves array types.
19295 We sometimes squish that down to a single array_type DIE with multiple
19296 subscripts in the Dwarf debugging info. The draft Dwarf specification
19297 say that we are allowed to do this kind of compression in C, because
19298 there is no difference between an array of arrays and a multidimensional
19299 array. We don't do this for Ada to remain as close as possible to the
19300 actual representation, which is especially important against the language
19301 flexibilty wrt arrays of variable size. */
19303 bool collapse_nested_arrays = !is_ada ();
19305 if (fill_variable_array_bounds (type))
19306 return;
19308 dw_die_ref scope_die = scope_die_for (type, context_die);
19309 tree element_type;
19311 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19312 DW_TAG_string_type doesn't have DW_AT_type attribute). */
19313 if (TYPE_STRING_FLAG (type)
19314 && TREE_CODE (type) == ARRAY_TYPE
19315 && is_fortran ()
19316 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19318 HOST_WIDE_INT size;
19320 array_die = new_die (DW_TAG_string_type, scope_die, type);
19321 add_name_attribute (array_die, type_tag (type));
19322 equate_type_number_to_die (type, array_die);
19323 size = int_size_in_bytes (type);
19324 if (size >= 0)
19325 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19326 else if (TYPE_DOMAIN (type) != NULL_TREE
19327 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
19329 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19330 tree rszdecl = szdecl;
19331 HOST_WIDE_INT rsize = 0;
19333 size = int_size_in_bytes (TREE_TYPE (szdecl));
19334 if (!DECL_P (szdecl))
19336 if (TREE_CODE (szdecl) == INDIRECT_REF
19337 && DECL_P (TREE_OPERAND (szdecl, 0)))
19339 rszdecl = TREE_OPERAND (szdecl, 0);
19340 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
19341 if (rsize <= 0)
19342 size = 0;
19344 else
19345 size = 0;
19347 if (size > 0)
19349 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19350 if (loc == NULL
19351 && early_dwarf
19352 && current_function_decl
19353 && DECL_CONTEXT (rszdecl) == current_function_decl)
19355 dw_die_ref ref = lookup_decl_die (rszdecl);
19356 dw_loc_descr_ref l = NULL;
19357 if (ref)
19359 l = new_loc_descr (DW_OP_call4, 0, 0);
19360 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19361 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
19362 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
19364 else if (TREE_CODE (rszdecl) == PARM_DECL
19365 && string_types)
19367 l = new_loc_descr (DW_OP_call4, 0, 0);
19368 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19369 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
19370 string_types->safe_push (array_die);
19372 if (l && rszdecl != szdecl)
19374 if (rsize == DWARF2_ADDR_SIZE)
19375 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
19376 0, 0));
19377 else
19378 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
19379 rsize, 0));
19381 if (l)
19382 loc = new_loc_list (l, NULL, NULL, NULL);
19384 if (loc)
19386 add_AT_location_description (array_die, DW_AT_string_length,
19387 loc);
19388 if (size != DWARF2_ADDR_SIZE)
19389 add_AT_unsigned (array_die, dwarf_version >= 5
19390 ? DW_AT_string_length_byte_size
19391 : DW_AT_byte_size, size);
19395 return;
19398 array_die = new_die (DW_TAG_array_type, scope_die, type);
19399 add_name_attribute (array_die, type_tag (type));
19400 equate_type_number_to_die (type, array_die);
19402 if (TREE_CODE (type) == VECTOR_TYPE)
19403 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19405 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
19406 if (is_fortran ()
19407 && TREE_CODE (type) == ARRAY_TYPE
19408 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19409 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19410 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19412 #if 0
19413 /* We default the array ordering. SDB will probably do
19414 the right things even if DW_AT_ordering is not present. It's not even
19415 an issue until we start to get into multidimensional arrays anyway. If
19416 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19417 then we'll have to put the DW_AT_ordering attribute back in. (But if
19418 and when we find out that we need to put these in, we will only do so
19419 for multidimensional arrays. */
19420 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19421 #endif
19423 if (TREE_CODE (type) == VECTOR_TYPE)
19425 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
19426 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19427 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19428 add_bound_info (subrange_die, DW_AT_upper_bound,
19429 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19431 else
19432 add_subscript_info (array_die, type, collapse_nested_arrays);
19434 /* Add representation of the type of the elements of this array type and
19435 emit the corresponding DIE if we haven't done it already. */
19436 element_type = TREE_TYPE (type);
19437 if (collapse_nested_arrays)
19438 while (TREE_CODE (element_type) == ARRAY_TYPE)
19440 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19441 break;
19442 element_type = TREE_TYPE (element_type);
19445 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19446 TREE_CODE (type) == ARRAY_TYPE
19447 && TYPE_REVERSE_STORAGE_ORDER (type),
19448 context_die);
19450 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19451 if (TYPE_ARTIFICIAL (type))
19452 add_AT_flag (array_die, DW_AT_artificial, 1);
19454 if (get_AT (array_die, DW_AT_name))
19455 add_pubtype (type, array_die);
19458 /* After all arguments are created, adjust any DW_TAG_string_type
19459 DIEs DW_AT_string_length attributes. */
19461 static void
19462 adjust_string_types (void)
19464 dw_die_ref array_die;
19465 unsigned int i;
19466 FOR_EACH_VEC_ELT (*string_types, i, array_die)
19468 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
19469 if (a == NULL)
19470 continue;
19471 dw_loc_descr_ref loc = AT_loc (a);
19472 gcc_assert (loc->dw_loc_opc == DW_OP_call4
19473 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
19474 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
19475 if (ref)
19477 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19478 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
19479 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
19481 else
19483 remove_AT (array_die, DW_AT_string_length);
19484 remove_AT (array_die, dwarf_version >= 5
19485 ? DW_AT_string_length_byte_size
19486 : DW_AT_byte_size);
19491 /* This routine generates DIE for array with hidden descriptor, details
19492 are filled into *info by a langhook. */
19494 static void
19495 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19496 dw_die_ref context_die)
19498 const dw_die_ref scope_die = scope_die_for (type, context_die);
19499 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19500 const struct loc_descr_context context = { type, info->base_decl, NULL };
19501 int dim;
19503 add_name_attribute (array_die, type_tag (type));
19504 equate_type_number_to_die (type, array_die);
19506 if (info->ndimensions > 1)
19507 switch (info->ordering)
19509 case array_descr_ordering_row_major:
19510 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19511 break;
19512 case array_descr_ordering_column_major:
19513 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19514 break;
19515 default:
19516 break;
19519 if (dwarf_version >= 3 || !dwarf_strict)
19521 if (info->data_location)
19522 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19523 dw_scalar_form_exprloc, &context);
19524 if (info->associated)
19525 add_scalar_info (array_die, DW_AT_associated, info->associated,
19526 dw_scalar_form_constant
19527 | dw_scalar_form_exprloc
19528 | dw_scalar_form_reference, &context);
19529 if (info->allocated)
19530 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19531 dw_scalar_form_constant
19532 | dw_scalar_form_exprloc
19533 | dw_scalar_form_reference, &context);
19534 if (info->stride)
19536 const enum dwarf_attribute attr
19537 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19538 const int forms
19539 = (info->stride_in_bits)
19540 ? dw_scalar_form_constant
19541 : (dw_scalar_form_constant
19542 | dw_scalar_form_exprloc
19543 | dw_scalar_form_reference);
19545 add_scalar_info (array_die, attr, info->stride, forms, &context);
19549 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19551 for (dim = 0; dim < info->ndimensions; dim++)
19553 dw_die_ref subrange_die
19554 = new_die (DW_TAG_subrange_type, array_die, NULL);
19556 if (info->dimen[dim].bounds_type)
19557 add_type_attribute (subrange_die,
19558 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19559 false, context_die);
19560 if (info->dimen[dim].lower_bound)
19561 add_bound_info (subrange_die, DW_AT_lower_bound,
19562 info->dimen[dim].lower_bound, &context);
19563 if (info->dimen[dim].upper_bound)
19564 add_bound_info (subrange_die, DW_AT_upper_bound,
19565 info->dimen[dim].upper_bound, &context);
19566 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19567 add_scalar_info (subrange_die, DW_AT_byte_stride,
19568 info->dimen[dim].stride,
19569 dw_scalar_form_constant
19570 | dw_scalar_form_exprloc
19571 | dw_scalar_form_reference,
19572 &context);
19575 gen_type_die (info->element_type, context_die);
19576 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19577 TREE_CODE (type) == ARRAY_TYPE
19578 && TYPE_REVERSE_STORAGE_ORDER (type),
19579 context_die);
19581 if (get_AT (array_die, DW_AT_name))
19582 add_pubtype (type, array_die);
19585 #if 0
19586 static void
19587 gen_entry_point_die (tree decl, dw_die_ref context_die)
19589 tree origin = decl_ultimate_origin (decl);
19590 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19592 if (origin != NULL)
19593 add_abstract_origin_attribute (decl_die, origin);
19594 else
19596 add_name_and_src_coords_attributes (decl_die, decl);
19597 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19598 TYPE_UNQUALIFIED, false, context_die);
19601 if (DECL_ABSTRACT_P (decl))
19602 equate_decl_number_to_die (decl, decl_die);
19603 else
19604 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19606 #endif
19608 /* Walk through the list of incomplete types again, trying once more to
19609 emit full debugging info for them. */
19611 static void
19612 retry_incomplete_types (void)
19614 set_early_dwarf s;
19615 int i;
19617 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19618 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19619 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19620 vec_safe_truncate (incomplete_types, 0);
19623 /* Determine what tag to use for a record type. */
19625 static enum dwarf_tag
19626 record_type_tag (tree type)
19628 if (! lang_hooks.types.classify_record)
19629 return DW_TAG_structure_type;
19631 switch (lang_hooks.types.classify_record (type))
19633 case RECORD_IS_STRUCT:
19634 return DW_TAG_structure_type;
19636 case RECORD_IS_CLASS:
19637 return DW_TAG_class_type;
19639 case RECORD_IS_INTERFACE:
19640 if (dwarf_version >= 3 || !dwarf_strict)
19641 return DW_TAG_interface_type;
19642 return DW_TAG_structure_type;
19644 default:
19645 gcc_unreachable ();
19649 /* Generate a DIE to represent an enumeration type. Note that these DIEs
19650 include all of the information about the enumeration values also. Each
19651 enumerated type name/value is listed as a child of the enumerated type
19652 DIE. */
19654 static dw_die_ref
19655 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19657 dw_die_ref type_die = lookup_type_die (type);
19659 if (type_die == NULL)
19661 type_die = new_die (DW_TAG_enumeration_type,
19662 scope_die_for (type, context_die), type);
19663 equate_type_number_to_die (type, type_die);
19664 add_name_attribute (type_die, type_tag (type));
19665 if (dwarf_version >= 4 || !dwarf_strict)
19667 if (ENUM_IS_SCOPED (type))
19668 add_AT_flag (type_die, DW_AT_enum_class, 1);
19669 if (ENUM_IS_OPAQUE (type))
19670 add_AT_flag (type_die, DW_AT_declaration, 1);
19673 else if (! TYPE_SIZE (type))
19674 return type_die;
19675 else
19676 remove_AT (type_die, DW_AT_declaration);
19678 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
19679 given enum type is incomplete, do not generate the DW_AT_byte_size
19680 attribute or the DW_AT_element_list attribute. */
19681 if (TYPE_SIZE (type))
19683 tree link;
19685 TREE_ASM_WRITTEN (type) = 1;
19686 add_byte_size_attribute (type_die, type);
19687 if (dwarf_version >= 3 || !dwarf_strict)
19689 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19690 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19691 context_die);
19693 if (TYPE_STUB_DECL (type) != NULL_TREE)
19695 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19696 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19699 /* If the first reference to this type was as the return type of an
19700 inline function, then it may not have a parent. Fix this now. */
19701 if (type_die->die_parent == NULL)
19702 add_child_die (scope_die_for (type, context_die), type_die);
19704 for (link = TYPE_VALUES (type);
19705 link != NULL; link = TREE_CHAIN (link))
19707 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19708 tree value = TREE_VALUE (link);
19710 add_name_attribute (enum_die,
19711 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19713 if (TREE_CODE (value) == CONST_DECL)
19714 value = DECL_INITIAL (value);
19716 if (simple_type_size_in_bits (TREE_TYPE (value))
19717 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19719 /* For constant forms created by add_AT_unsigned DWARF
19720 consumers (GDB, elfutils, etc.) always zero extend
19721 the value. Only when the actual value is negative
19722 do we need to use add_AT_int to generate a constant
19723 form that can represent negative values. */
19724 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19725 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19726 add_AT_unsigned (enum_die, DW_AT_const_value,
19727 (unsigned HOST_WIDE_INT) val);
19728 else
19729 add_AT_int (enum_die, DW_AT_const_value, val);
19731 else
19732 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
19733 that here. TODO: This should be re-worked to use correct
19734 signed/unsigned double tags for all cases. */
19735 add_AT_wide (enum_die, DW_AT_const_value, value);
19738 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19739 if (TYPE_ARTIFICIAL (type))
19740 add_AT_flag (type_die, DW_AT_artificial, 1);
19742 else
19743 add_AT_flag (type_die, DW_AT_declaration, 1);
19745 add_pubtype (type, type_die);
19747 return type_die;
19750 /* Generate a DIE to represent either a real live formal parameter decl or to
19751 represent just the type of some formal parameter position in some function
19752 type.
19754 Note that this routine is a bit unusual because its argument may be a
19755 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19756 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19757 node. If it's the former then this function is being called to output a
19758 DIE to represent a formal parameter object (or some inlining thereof). If
19759 it's the latter, then this function is only being called to output a
19760 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19761 argument type of some subprogram type.
19762 If EMIT_NAME_P is true, name and source coordinate attributes
19763 are emitted. */
19765 static dw_die_ref
19766 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19767 dw_die_ref context_die)
19769 tree node_or_origin = node ? node : origin;
19770 tree ultimate_origin;
19771 dw_die_ref parm_die = NULL;
19773 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19775 parm_die = lookup_decl_die (node);
19777 /* If the contexts differ, we may not be talking about the same
19778 thing. */
19779 if (parm_die && parm_die->die_parent != context_die)
19781 if (!DECL_ABSTRACT_P (node))
19783 /* This can happen when creating an inlined instance, in
19784 which case we need to create a new DIE that will get
19785 annotated with DW_AT_abstract_origin. */
19786 parm_die = NULL;
19788 else
19790 /* FIXME: Reuse DIE even with a differing context.
19792 This can happen when calling
19793 dwarf2out_abstract_function to build debug info for
19794 the abstract instance of a function for which we have
19795 already generated a DIE in
19796 dwarf2out_early_global_decl.
19798 Once we remove dwarf2out_abstract_function, we should
19799 have a call to gcc_unreachable here. */
19803 if (parm_die && parm_die->die_parent == NULL)
19805 /* Check that parm_die already has the right attributes that
19806 we would have added below. If any attributes are
19807 missing, fall through to add them. */
19808 if (! DECL_ABSTRACT_P (node_or_origin)
19809 && !get_AT (parm_die, DW_AT_location)
19810 && !get_AT (parm_die, DW_AT_const_value))
19811 /* We are missing location info, and are about to add it. */
19813 else
19815 add_child_die (context_die, parm_die);
19816 return parm_die;
19821 /* If we have a previously generated DIE, use it, unless this is an
19822 concrete instance (origin != NULL), in which case we need a new
19823 DIE with a corresponding DW_AT_abstract_origin. */
19824 bool reusing_die;
19825 if (parm_die && origin == NULL)
19826 reusing_die = true;
19827 else
19829 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19830 reusing_die = false;
19833 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19835 case tcc_declaration:
19836 ultimate_origin = decl_ultimate_origin (node_or_origin);
19837 if (node || ultimate_origin)
19838 origin = ultimate_origin;
19840 if (reusing_die)
19841 goto add_location;
19843 if (origin != NULL)
19844 add_abstract_origin_attribute (parm_die, origin);
19845 else if (emit_name_p)
19846 add_name_and_src_coords_attributes (parm_die, node);
19847 if (origin == NULL
19848 || (! DECL_ABSTRACT_P (node_or_origin)
19849 && variably_modified_type_p (TREE_TYPE (node_or_origin),
19850 decl_function_context
19851 (node_or_origin))))
19853 tree type = TREE_TYPE (node_or_origin);
19854 if (decl_by_reference_p (node_or_origin))
19855 add_type_attribute (parm_die, TREE_TYPE (type),
19856 TYPE_UNQUALIFIED,
19857 false, context_die);
19858 else
19859 add_type_attribute (parm_die, type,
19860 decl_quals (node_or_origin),
19861 false, context_die);
19863 if (origin == NULL && DECL_ARTIFICIAL (node))
19864 add_AT_flag (parm_die, DW_AT_artificial, 1);
19865 add_location:
19866 if (node && node != origin)
19867 equate_decl_number_to_die (node, parm_die);
19868 if (! DECL_ABSTRACT_P (node_or_origin))
19869 add_location_or_const_value_attribute (parm_die, node_or_origin,
19870 node == NULL);
19872 break;
19874 case tcc_type:
19875 /* We were called with some kind of a ..._TYPE node. */
19876 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19877 context_die);
19878 break;
19880 default:
19881 gcc_unreachable ();
19884 return parm_die;
19887 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19888 children DW_TAG_formal_parameter DIEs representing the arguments of the
19889 parameter pack.
19891 PARM_PACK must be a function parameter pack.
19892 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19893 must point to the subsequent arguments of the function PACK_ARG belongs to.
19894 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19895 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19896 following the last one for which a DIE was generated. */
19898 static dw_die_ref
19899 gen_formal_parameter_pack_die (tree parm_pack,
19900 tree pack_arg,
19901 dw_die_ref subr_die,
19902 tree *next_arg)
19904 tree arg;
19905 dw_die_ref parm_pack_die;
19907 gcc_assert (parm_pack
19908 && lang_hooks.function_parameter_pack_p (parm_pack)
19909 && subr_die);
19911 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19912 add_src_coords_attributes (parm_pack_die, parm_pack);
19914 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19916 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19917 parm_pack))
19918 break;
19919 gen_formal_parameter_die (arg, NULL,
19920 false /* Don't emit name attribute. */,
19921 parm_pack_die);
19923 if (next_arg)
19924 *next_arg = arg;
19925 return parm_pack_die;
19928 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19929 at the end of an (ANSI prototyped) formal parameters list. */
19931 static void
19932 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19934 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19937 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19938 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19939 parameters as specified in some function type specification (except for
19940 those which appear as part of a function *definition*). */
19942 static void
19943 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19945 tree link;
19946 tree formal_type = NULL;
19947 tree first_parm_type;
19948 tree arg;
19950 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19952 arg = DECL_ARGUMENTS (function_or_method_type);
19953 function_or_method_type = TREE_TYPE (function_or_method_type);
19955 else
19956 arg = NULL_TREE;
19958 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19960 /* Make our first pass over the list of formal parameter types and output a
19961 DW_TAG_formal_parameter DIE for each one. */
19962 for (link = first_parm_type; link; )
19964 dw_die_ref parm_die;
19966 formal_type = TREE_VALUE (link);
19967 if (formal_type == void_type_node)
19968 break;
19970 /* Output a (nameless) DIE to represent the formal parameter itself. */
19971 if (!POINTER_BOUNDS_TYPE_P (formal_type))
19973 parm_die = gen_formal_parameter_die (formal_type, NULL,
19974 true /* Emit name attribute. */,
19975 context_die);
19976 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19977 && link == first_parm_type)
19979 add_AT_flag (parm_die, DW_AT_artificial, 1);
19980 if (dwarf_version >= 3 || !dwarf_strict)
19981 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19983 else if (arg && DECL_ARTIFICIAL (arg))
19984 add_AT_flag (parm_die, DW_AT_artificial, 1);
19987 link = TREE_CHAIN (link);
19988 if (arg)
19989 arg = DECL_CHAIN (arg);
19992 /* If this function type has an ellipsis, add a
19993 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
19994 if (formal_type != void_type_node)
19995 gen_unspecified_parameters_die (function_or_method_type, context_die);
19997 /* Make our second (and final) pass over the list of formal parameter types
19998 and output DIEs to represent those types (as necessary). */
19999 for (link = TYPE_ARG_TYPES (function_or_method_type);
20000 link && TREE_VALUE (link);
20001 link = TREE_CHAIN (link))
20002 gen_type_die (TREE_VALUE (link), context_die);
20005 /* We want to generate the DIE for TYPE so that we can generate the
20006 die for MEMBER, which has been defined; we will need to refer back
20007 to the member declaration nested within TYPE. If we're trying to
20008 generate minimal debug info for TYPE, processing TYPE won't do the
20009 trick; we need to attach the member declaration by hand. */
20011 static void
20012 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
20014 gen_type_die (type, context_die);
20016 /* If we're trying to avoid duplicate debug info, we may not have
20017 emitted the member decl for this function. Emit it now. */
20018 if (TYPE_STUB_DECL (type)
20019 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
20020 && ! lookup_decl_die (member))
20022 dw_die_ref type_die;
20023 gcc_assert (!decl_ultimate_origin (member));
20025 push_decl_scope (type);
20026 type_die = lookup_type_die_strip_naming_typedef (type);
20027 if (TREE_CODE (member) == FUNCTION_DECL)
20028 gen_subprogram_die (member, type_die);
20029 else if (TREE_CODE (member) == FIELD_DECL)
20031 /* Ignore the nameless fields that are used to skip bits but handle
20032 C++ anonymous unions and structs. */
20033 if (DECL_NAME (member) != NULL_TREE
20034 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
20035 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
20037 struct vlr_context vlr_ctx = {
20038 DECL_CONTEXT (member), /* struct_type */
20039 NULL_TREE /* variant_part_offset */
20041 gen_type_die (member_declared_type (member), type_die);
20042 gen_field_die (member, &vlr_ctx, type_die);
20045 else
20046 gen_variable_die (member, NULL_TREE, type_die);
20048 pop_decl_scope ();
20052 /* Forward declare these functions, because they are mutually recursive
20053 with their set_block_* pairing functions. */
20054 static void set_decl_origin_self (tree);
20055 static void set_decl_abstract_flags (tree, vec<tree> &);
20057 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
20058 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
20059 that it points to the node itself, thus indicating that the node is its
20060 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
20061 the given node is NULL, recursively descend the decl/block tree which
20062 it is the root of, and for each other ..._DECL or BLOCK node contained
20063 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
20064 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
20065 values to point to themselves. */
20067 static void
20068 set_block_origin_self (tree stmt)
20070 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
20072 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
20075 tree local_decl;
20077 for (local_decl = BLOCK_VARS (stmt);
20078 local_decl != NULL_TREE;
20079 local_decl = DECL_CHAIN (local_decl))
20080 /* Do not recurse on nested functions since the inlining status
20081 of parent and child can be different as per the DWARF spec. */
20082 if (TREE_CODE (local_decl) != FUNCTION_DECL
20083 && !DECL_EXTERNAL (local_decl))
20084 set_decl_origin_self (local_decl);
20088 tree subblock;
20090 for (subblock = BLOCK_SUBBLOCKS (stmt);
20091 subblock != NULL_TREE;
20092 subblock = BLOCK_CHAIN (subblock))
20093 set_block_origin_self (subblock); /* Recurse. */
20098 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
20099 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
20100 node to so that it points to the node itself, thus indicating that the
20101 node represents its own (abstract) origin. Additionally, if the
20102 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
20103 the decl/block tree of which the given node is the root of, and for
20104 each other ..._DECL or BLOCK node contained therein whose
20105 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
20106 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
20107 point to themselves. */
20109 static void
20110 set_decl_origin_self (tree decl)
20112 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
20114 DECL_ABSTRACT_ORIGIN (decl) = decl;
20115 if (TREE_CODE (decl) == FUNCTION_DECL)
20117 tree arg;
20119 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20120 DECL_ABSTRACT_ORIGIN (arg) = arg;
20121 if (DECL_INITIAL (decl) != NULL_TREE
20122 && DECL_INITIAL (decl) != error_mark_node)
20123 set_block_origin_self (DECL_INITIAL (decl));
20128 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
20129 and if it wasn't 1 before, push it to abstract_vec vector.
20130 For all local decls and all local sub-blocks (recursively) do it
20131 too. */
20133 static void
20134 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
20136 tree local_decl;
20137 tree subblock;
20138 unsigned int i;
20140 if (!BLOCK_ABSTRACT (stmt))
20142 abstract_vec.safe_push (stmt);
20143 BLOCK_ABSTRACT (stmt) = 1;
20146 for (local_decl = BLOCK_VARS (stmt);
20147 local_decl != NULL_TREE;
20148 local_decl = DECL_CHAIN (local_decl))
20149 if (! DECL_EXTERNAL (local_decl))
20150 set_decl_abstract_flags (local_decl, abstract_vec);
20152 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20154 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
20155 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
20156 || TREE_CODE (local_decl) == PARM_DECL)
20157 set_decl_abstract_flags (local_decl, abstract_vec);
20160 for (subblock = BLOCK_SUBBLOCKS (stmt);
20161 subblock != NULL_TREE;
20162 subblock = BLOCK_CHAIN (subblock))
20163 set_block_abstract_flags (subblock, abstract_vec);
20166 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
20167 to 1 and if it wasn't 1 before, push to abstract_vec vector.
20168 In the case where the decl is a FUNCTION_DECL also set the abstract
20169 flags for all of the parameters, local vars, local
20170 blocks and sub-blocks (recursively). */
20172 static void
20173 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
20175 if (!DECL_ABSTRACT_P (decl))
20177 abstract_vec.safe_push (decl);
20178 DECL_ABSTRACT_P (decl) = 1;
20181 if (TREE_CODE (decl) == FUNCTION_DECL)
20183 tree arg;
20185 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20186 if (!DECL_ABSTRACT_P (arg))
20188 abstract_vec.safe_push (arg);
20189 DECL_ABSTRACT_P (arg) = 1;
20191 if (DECL_INITIAL (decl) != NULL_TREE
20192 && DECL_INITIAL (decl) != error_mark_node)
20193 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
20197 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20198 may later generate inlined and/or out-of-line instances of.
20200 FIXME: In the early-dwarf world, this function, and most of the
20201 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
20202 the abstract instance. All we would need to do is annotate
20203 the early DIE with the appropriate DW_AT_inline in late
20204 dwarf (perhaps in gen_inlined_subroutine_die).
20206 However, we can't do this yet, because LTO streaming of DIEs
20207 has not been implemented yet. */
20209 static void
20210 dwarf2out_abstract_function (tree decl)
20212 dw_die_ref old_die;
20213 tree save_fn;
20214 tree context;
20215 hash_table<decl_loc_hasher> *old_decl_loc_table;
20216 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20217 int old_call_site_count, old_tail_call_site_count;
20218 struct call_arg_loc_node *old_call_arg_locations;
20220 /* Make sure we have the actual abstract inline, not a clone. */
20221 decl = DECL_ORIGIN (decl);
20223 old_die = lookup_decl_die (decl);
20224 if (old_die && get_AT (old_die, DW_AT_inline))
20225 /* We've already generated the abstract instance. */
20226 return;
20228 /* We can be called while recursively when seeing block defining inlined subroutine
20229 DIE. Be sure to not clobber the outer location table nor use it or we would
20230 get locations in abstract instantces. */
20231 old_decl_loc_table = decl_loc_table;
20232 decl_loc_table = NULL;
20233 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20234 cached_dw_loc_list_table = NULL;
20235 old_call_arg_locations = call_arg_locations;
20236 call_arg_locations = NULL;
20237 old_call_site_count = call_site_count;
20238 call_site_count = -1;
20239 old_tail_call_site_count = tail_call_site_count;
20240 tail_call_site_count = -1;
20242 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20243 we don't get confused by DECL_ABSTRACT_P. */
20244 if (debug_info_level > DINFO_LEVEL_TERSE)
20246 context = decl_class_context (decl);
20247 if (context)
20248 gen_type_die_for_member
20249 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20252 /* Pretend we've just finished compiling this function. */
20253 save_fn = current_function_decl;
20254 current_function_decl = decl;
20256 auto_vec<tree, 64> abstract_vec;
20257 set_decl_abstract_flags (decl, abstract_vec);
20258 dwarf2out_decl (decl);
20259 unsigned int i;
20260 tree t;
20261 FOR_EACH_VEC_ELT (abstract_vec, i, t)
20262 if (TREE_CODE (t) == BLOCK)
20263 BLOCK_ABSTRACT (t) = 0;
20264 else
20265 DECL_ABSTRACT_P (t) = 0;
20267 current_function_decl = save_fn;
20268 decl_loc_table = old_decl_loc_table;
20269 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20270 call_arg_locations = old_call_arg_locations;
20271 call_site_count = old_call_site_count;
20272 tail_call_site_count = old_tail_call_site_count;
20275 /* Helper function of premark_used_types() which gets called through
20276 htab_traverse.
20278 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20279 marked as unused by prune_unused_types. */
20281 bool
20282 premark_used_types_helper (tree const &type, void *)
20284 dw_die_ref die;
20286 die = lookup_type_die (type);
20287 if (die != NULL)
20288 die->die_perennial_p = 1;
20289 return true;
20292 /* Helper function of premark_types_used_by_global_vars which gets called
20293 through htab_traverse.
20295 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20296 marked as unused by prune_unused_types. The DIE of the type is marked
20297 only if the global variable using the type will actually be emitted. */
20300 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20301 void *)
20303 struct types_used_by_vars_entry *entry;
20304 dw_die_ref die;
20306 entry = (struct types_used_by_vars_entry *) *slot;
20307 gcc_assert (entry->type != NULL
20308 && entry->var_decl != NULL);
20309 die = lookup_type_die (entry->type);
20310 if (die)
20312 /* Ask cgraph if the global variable really is to be emitted.
20313 If yes, then we'll keep the DIE of ENTRY->TYPE. */
20314 varpool_node *node = varpool_node::get (entry->var_decl);
20315 if (node && node->definition)
20317 die->die_perennial_p = 1;
20318 /* Keep the parent DIEs as well. */
20319 while ((die = die->die_parent) && die->die_perennial_p == 0)
20320 die->die_perennial_p = 1;
20323 return 1;
20326 /* Mark all members of used_types_hash as perennial. */
20328 static void
20329 premark_used_types (struct function *fun)
20331 if (fun && fun->used_types_hash)
20332 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20335 /* Mark all members of types_used_by_vars_entry as perennial. */
20337 static void
20338 premark_types_used_by_global_vars (void)
20340 if (types_used_by_vars_hash)
20341 types_used_by_vars_hash
20342 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20345 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20346 for CA_LOC call arg loc node. */
20348 static dw_die_ref
20349 gen_call_site_die (tree decl, dw_die_ref subr_die,
20350 struct call_arg_loc_node *ca_loc)
20352 dw_die_ref stmt_die = NULL, die;
20353 tree block = ca_loc->block;
20355 while (block
20356 && block != DECL_INITIAL (decl)
20357 && TREE_CODE (block) == BLOCK)
20359 stmt_die = BLOCK_DIE (block);
20360 if (stmt_die)
20361 break;
20362 block = BLOCK_SUPERCONTEXT (block);
20364 if (stmt_die == NULL)
20365 stmt_die = subr_die;
20366 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20367 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20368 if (ca_loc->tail_call_p)
20369 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20370 if (ca_loc->symbol_ref)
20372 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20373 if (tdie)
20374 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20375 else
20376 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20378 return die;
20381 /* Generate a DIE to represent a declared function (either file-scope or
20382 block-local). */
20384 static void
20385 gen_subprogram_die (tree decl, dw_die_ref context_die)
20387 tree origin = decl_ultimate_origin (decl);
20388 dw_die_ref subr_die;
20389 dw_die_ref old_die = lookup_decl_die (decl);
20391 /* This function gets called multiple times for different stages of
20392 the debug process. For example, for func() in this code:
20394 namespace S
20396 void func() { ... }
20399 ...we get called 4 times. Twice in early debug and twice in
20400 late debug:
20402 Early debug
20403 -----------
20405 1. Once while generating func() within the namespace. This is
20406 the declaration. The declaration bit below is set, as the
20407 context is the namespace.
20409 A new DIE will be generated with DW_AT_declaration set.
20411 2. Once for func() itself. This is the specification. The
20412 declaration bit below is clear as the context is the CU.
20414 We will use the cached DIE from (1) to create a new DIE with
20415 DW_AT_specification pointing to the declaration in (1).
20417 Late debug via rest_of_handle_final()
20418 -------------------------------------
20420 3. Once generating func() within the namespace. This is also the
20421 declaration, as in (1), but this time we will early exit below
20422 as we have a cached DIE and a declaration needs no additional
20423 annotations (no locations), as the source declaration line
20424 info is enough.
20426 4. Once for func() itself. As in (2), this is the specification,
20427 but this time we will re-use the cached DIE, and just annotate
20428 it with the location information that should now be available.
20430 For something without namespaces, but with abstract instances, we
20431 are also called a multiple times:
20433 class Base
20435 public:
20436 Base (); // constructor declaration (1)
20439 Base::Base () { } // constructor specification (2)
20441 Early debug
20442 -----------
20444 1. Once for the Base() constructor by virtue of it being a
20445 member of the Base class. This is done via
20446 rest_of_type_compilation.
20448 This is a declaration, so a new DIE will be created with
20449 DW_AT_declaration.
20451 2. Once for the Base() constructor definition, but this time
20452 while generating the abstract instance of the base
20453 constructor (__base_ctor) which is being generated via early
20454 debug of reachable functions.
20456 Even though we have a cached version of the declaration (1),
20457 we will create a DW_AT_specification of the declaration DIE
20458 in (1).
20460 3. Once for the __base_ctor itself, but this time, we generate
20461 an DW_AT_abstract_origin version of the DW_AT_specification in
20462 (2).
20464 Late debug via rest_of_handle_final
20465 -----------------------------------
20467 4. One final time for the __base_ctor (which will have a cached
20468 DIE with DW_AT_abstract_origin created in (3). This time,
20469 we will just annotate the location information now
20470 available.
20472 int declaration = (current_function_decl != decl
20473 || class_or_namespace_scope_p (context_die));
20475 /* Now that the C++ front end lazily declares artificial member fns, we
20476 might need to retrofit the declaration into its class. */
20477 if (!declaration && !origin && !old_die
20478 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20479 && !class_or_namespace_scope_p (context_die)
20480 && debug_info_level > DINFO_LEVEL_TERSE)
20481 old_die = force_decl_die (decl);
20483 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
20484 if (origin != NULL)
20486 gcc_assert (!declaration || local_scope_p (context_die));
20488 /* Fixup die_parent for the abstract instance of a nested
20489 inline function. */
20490 if (old_die && old_die->die_parent == NULL)
20491 add_child_die (context_die, old_die);
20493 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20495 /* If we have a DW_AT_abstract_origin we have a working
20496 cached version. */
20497 subr_die = old_die;
20499 else
20501 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20502 add_abstract_origin_attribute (subr_die, origin);
20503 /* This is where the actual code for a cloned function is.
20504 Let's emit linkage name attribute for it. This helps
20505 debuggers to e.g, set breakpoints into
20506 constructors/destructors when the user asks "break
20507 K::K". */
20508 add_linkage_name (subr_die, decl);
20511 /* A cached copy, possibly from early dwarf generation. Reuse as
20512 much as possible. */
20513 else if (old_die)
20515 /* A declaration that has been previously dumped needs no
20516 additional information. */
20517 if (declaration)
20518 return;
20520 if (!get_AT_flag (old_die, DW_AT_declaration)
20521 /* We can have a normal definition following an inline one in the
20522 case of redefinition of GNU C extern inlines.
20523 It seems reasonable to use AT_specification in this case. */
20524 && !get_AT (old_die, DW_AT_inline))
20526 /* Detect and ignore this case, where we are trying to output
20527 something we have already output. */
20528 if (get_AT (old_die, DW_AT_low_pc)
20529 || get_AT (old_die, DW_AT_ranges))
20530 return;
20532 /* If we have no location information, this must be a
20533 partially generated DIE from early dwarf generation.
20534 Fall through and generate it. */
20537 /* If the definition comes from the same place as the declaration,
20538 maybe use the old DIE. We always want the DIE for this function
20539 that has the *_pc attributes to be under comp_unit_die so the
20540 debugger can find it. We also need to do this for abstract
20541 instances of inlines, since the spec requires the out-of-line copy
20542 to have the same parent. For local class methods, this doesn't
20543 apply; we just use the old DIE. */
20544 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20545 struct dwarf_file_data * file_index = lookup_filename (s.file);
20546 if ((is_cu_die (old_die->die_parent)
20547 /* This condition fixes the inconsistency/ICE with the
20548 following Fortran test (or some derivative thereof) while
20549 building libgfortran:
20551 module some_m
20552 contains
20553 logical function funky (FLAG)
20554 funky = .true.
20555 end function
20556 end module
20558 || (old_die->die_parent
20559 && old_die->die_parent->die_tag == DW_TAG_module)
20560 || context_die == NULL)
20561 && (DECL_ARTIFICIAL (decl)
20562 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20563 && (get_AT_unsigned (old_die, DW_AT_decl_line)
20564 == (unsigned) s.line))))
20566 subr_die = old_die;
20568 /* Clear out the declaration attribute, but leave the
20569 parameters so they can be augmented with location
20570 information later. Unless this was a declaration, in
20571 which case, wipe out the nameless parameters and recreate
20572 them further down. */
20573 if (remove_AT (subr_die, DW_AT_declaration))
20576 remove_AT (subr_die, DW_AT_object_pointer);
20577 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20580 /* Make a specification pointing to the previously built
20581 declaration. */
20582 else
20584 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20585 add_AT_specification (subr_die, old_die);
20586 add_pubname (decl, subr_die);
20587 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20588 add_AT_file (subr_die, DW_AT_decl_file, file_index);
20589 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20590 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20592 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20593 emit the real type on the definition die. */
20594 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20596 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20597 if (die == auto_die || die == decltype_auto_die)
20598 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20599 TYPE_UNQUALIFIED, false, context_die);
20602 /* When we process the method declaration, we haven't seen
20603 the out-of-class defaulted definition yet, so we have to
20604 recheck now. */
20605 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20606 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict)
20607 && !get_AT (subr_die, DW_AT_defaulted))
20608 switch (defaulted)
20610 case 2:
20611 add_AT_unsigned (subr_die, DW_AT_defaulted,
20612 DW_DEFAULTED_out_of_class);
20613 break;
20615 case 1: /* This must have been handled before. */
20616 default:
20617 gcc_unreachable ();
20621 /* Create a fresh DIE for anything else. */
20622 else
20624 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20626 if (TREE_PUBLIC (decl))
20627 add_AT_flag (subr_die, DW_AT_external, 1);
20629 add_name_and_src_coords_attributes (subr_die, decl);
20630 add_pubname (decl, subr_die);
20631 if (debug_info_level > DINFO_LEVEL_TERSE)
20633 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20634 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20635 TYPE_UNQUALIFIED, false, context_die);
20638 add_pure_or_virtual_attribute (subr_die, decl);
20639 if (DECL_ARTIFICIAL (decl))
20640 add_AT_flag (subr_die, DW_AT_artificial, 1);
20642 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20643 add_AT_flag (subr_die, DW_AT_noreturn, 1);
20645 add_accessibility_attribute (subr_die, decl);
20648 /* Unless we have an existing non-declaration DIE, equate the new
20649 DIE. */
20650 if (!old_die || is_declaration_die (old_die))
20651 equate_decl_number_to_die (decl, subr_die);
20653 if (declaration)
20655 if (!old_die || !get_AT (old_die, DW_AT_inline))
20657 add_AT_flag (subr_die, DW_AT_declaration, 1);
20659 /* If this is an explicit function declaration then generate
20660 a DW_AT_explicit attribute. */
20661 if (lang_hooks.decls.function_decl_explicit_p (decl)
20662 && (dwarf_version >= 3 || !dwarf_strict))
20663 add_AT_flag (subr_die, DW_AT_explicit, 1);
20665 /* If this is a C++11 deleted special function member then generate
20666 a DW_AT_deleted attribute. */
20667 if (lang_hooks.decls.function_decl_deleted_p (decl)
20668 && (dwarf_version >= 5 || ! dwarf_strict))
20669 add_AT_flag (subr_die, DW_AT_deleted, 1);
20671 /* If this is a C++11 defaulted special function member then
20672 generate a DW_AT_GNU_defaulted attribute. */
20673 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20674 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict))
20675 switch (defaulted)
20677 case 1:
20678 add_AT_unsigned (subr_die, DW_AT_defaulted,
20679 DW_DEFAULTED_in_class);
20680 break;
20682 /* It is likely that this will never hit, since we
20683 don't have the out-of-class definition yet when we
20684 process the class definition and the method
20685 declaration. We recheck elsewhere, but leave it
20686 here just in case. */
20687 case 2:
20688 add_AT_unsigned (subr_die, DW_AT_defaulted,
20689 DW_DEFAULTED_out_of_class);
20690 break;
20692 default:
20693 gcc_unreachable ();
20697 /* Tag abstract instances with DW_AT_inline. */
20698 else if (DECL_ABSTRACT_P (decl))
20700 if (DECL_DECLARED_INLINE_P (decl))
20702 if (cgraph_function_possibly_inlined_p (decl))
20703 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20704 else
20705 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20707 else
20709 if (cgraph_function_possibly_inlined_p (decl))
20710 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20711 else
20712 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20715 if (DECL_DECLARED_INLINE_P (decl)
20716 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20717 add_AT_flag (subr_die, DW_AT_artificial, 1);
20719 /* For non DECL_EXTERNALs, if range information is available, fill
20720 the DIE with it. */
20721 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20723 HOST_WIDE_INT cfa_fb_offset;
20725 struct function *fun = DECL_STRUCT_FUNCTION (decl);
20727 if (!flag_reorder_blocks_and_partition)
20729 dw_fde_ref fde = fun->fde;
20730 if (fde->dw_fde_begin)
20732 /* We have already generated the labels. */
20733 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20734 fde->dw_fde_end, false);
20736 else
20738 /* Create start/end labels and add the range. */
20739 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20740 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20741 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20742 current_function_funcdef_no);
20743 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20744 current_function_funcdef_no);
20745 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20746 false);
20749 #if VMS_DEBUGGING_INFO
20750 /* HP OpenVMS Industry Standard 64: DWARF Extensions
20751 Section 2.3 Prologue and Epilogue Attributes:
20752 When a breakpoint is set on entry to a function, it is generally
20753 desirable for execution to be suspended, not on the very first
20754 instruction of the function, but rather at a point after the
20755 function's frame has been set up, after any language defined local
20756 declaration processing has been completed, and before execution of
20757 the first statement of the function begins. Debuggers generally
20758 cannot properly determine where this point is. Similarly for a
20759 breakpoint set on exit from a function. The prologue and epilogue
20760 attributes allow a compiler to communicate the location(s) to use. */
20763 if (fde->dw_fde_vms_end_prologue)
20764 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20765 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20767 if (fde->dw_fde_vms_begin_epilogue)
20768 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20769 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20771 #endif
20774 else
20776 /* Generate pubnames entries for the split function code ranges. */
20777 dw_fde_ref fde = fun->fde;
20779 if (fde->dw_fde_second_begin)
20781 if (dwarf_version >= 3 || !dwarf_strict)
20783 /* We should use ranges for non-contiguous code section
20784 addresses. Use the actual code range for the initial
20785 section, since the HOT/COLD labels might precede an
20786 alignment offset. */
20787 bool range_list_added = false;
20788 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20789 fde->dw_fde_end, &range_list_added,
20790 false);
20791 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20792 fde->dw_fde_second_end,
20793 &range_list_added, false);
20794 if (range_list_added)
20795 add_ranges (NULL);
20797 else
20799 /* There is no real support in DW2 for this .. so we make
20800 a work-around. First, emit the pub name for the segment
20801 containing the function label. Then make and emit a
20802 simplified subprogram DIE for the second segment with the
20803 name pre-fixed by __hot/cold_sect_of_. We use the same
20804 linkage name for the second die so that gdb will find both
20805 sections when given "b foo". */
20806 const char *name = NULL;
20807 tree decl_name = DECL_NAME (decl);
20808 dw_die_ref seg_die;
20810 /* Do the 'primary' section. */
20811 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20812 fde->dw_fde_end, false);
20814 /* Build a minimal DIE for the secondary section. */
20815 seg_die = new_die (DW_TAG_subprogram,
20816 subr_die->die_parent, decl);
20818 if (TREE_PUBLIC (decl))
20819 add_AT_flag (seg_die, DW_AT_external, 1);
20821 if (decl_name != NULL
20822 && IDENTIFIER_POINTER (decl_name) != NULL)
20824 name = dwarf2_name (decl, 1);
20825 if (! DECL_ARTIFICIAL (decl))
20826 add_src_coords_attributes (seg_die, decl);
20828 add_linkage_name (seg_die, decl);
20830 gcc_assert (name != NULL);
20831 add_pure_or_virtual_attribute (seg_die, decl);
20832 if (DECL_ARTIFICIAL (decl))
20833 add_AT_flag (seg_die, DW_AT_artificial, 1);
20835 name = concat ("__second_sect_of_", name, NULL);
20836 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20837 fde->dw_fde_second_end, false);
20838 add_name_attribute (seg_die, name);
20839 if (want_pubnames ())
20840 add_pubname_string (name, seg_die);
20843 else
20844 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20845 false);
20848 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20850 /* We define the "frame base" as the function's CFA. This is more
20851 convenient for several reasons: (1) It's stable across the prologue
20852 and epilogue, which makes it better than just a frame pointer,
20853 (2) With dwarf3, there exists a one-byte encoding that allows us
20854 to reference the .debug_frame data by proxy, but failing that,
20855 (3) We can at least reuse the code inspection and interpretation
20856 code that determines the CFA position at various points in the
20857 function. */
20858 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20860 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20861 add_AT_loc (subr_die, DW_AT_frame_base, op);
20863 else
20865 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20866 if (list->dw_loc_next)
20867 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20868 else
20869 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20872 /* Compute a displacement from the "steady-state frame pointer" to
20873 the CFA. The former is what all stack slots and argument slots
20874 will reference in the rtl; the latter is what we've told the
20875 debugger about. We'll need to adjust all frame_base references
20876 by this displacement. */
20877 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20879 if (fun->static_chain_decl)
20881 /* DWARF requires here a location expression that computes the
20882 address of the enclosing subprogram's frame base. The machinery
20883 in tree-nested.c is supposed to store this specific address in the
20884 last field of the FRAME record. */
20885 const tree frame_type
20886 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20887 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20889 tree fb_expr
20890 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20891 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20892 fb_expr, fb_decl, NULL_TREE);
20894 add_AT_location_description (subr_die, DW_AT_static_link,
20895 loc_list_from_tree (fb_expr, 0, NULL));
20899 /* Generate child dies for template paramaters. */
20900 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20901 gen_generic_params_dies (decl);
20903 /* Now output descriptions of the arguments for this function. This gets
20904 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20905 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20906 `...' at the end of the formal parameter list. In order to find out if
20907 there was a trailing ellipsis or not, we must instead look at the type
20908 associated with the FUNCTION_DECL. This will be a node of type
20909 FUNCTION_TYPE. If the chain of type nodes hanging off of this
20910 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20911 an ellipsis at the end. */
20913 /* In the case where we are describing a mere function declaration, all we
20914 need to do here (and all we *can* do here) is to describe the *types* of
20915 its formal parameters. */
20916 if (debug_info_level <= DINFO_LEVEL_TERSE)
20918 else if (declaration)
20919 gen_formal_types_die (decl, subr_die);
20920 else
20922 /* Generate DIEs to represent all known formal parameters. */
20923 tree parm = DECL_ARGUMENTS (decl);
20924 tree generic_decl = early_dwarf
20925 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20926 tree generic_decl_parm = generic_decl
20927 ? DECL_ARGUMENTS (generic_decl)
20928 : NULL;
20929 auto_vec<dw_die_ref> string_types_vec;
20930 if (string_types == NULL)
20931 string_types = &string_types_vec;
20933 /* Now we want to walk the list of parameters of the function and
20934 emit their relevant DIEs.
20936 We consider the case of DECL being an instance of a generic function
20937 as well as it being a normal function.
20939 If DECL is an instance of a generic function we walk the
20940 parameters of the generic function declaration _and_ the parameters of
20941 DECL itself. This is useful because we want to emit specific DIEs for
20942 function parameter packs and those are declared as part of the
20943 generic function declaration. In that particular case,
20944 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20945 That DIE has children DIEs representing the set of arguments
20946 of the pack. Note that the set of pack arguments can be empty.
20947 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20948 children DIE.
20950 Otherwise, we just consider the parameters of DECL. */
20951 while (generic_decl_parm || parm)
20953 if (generic_decl_parm
20954 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20955 gen_formal_parameter_pack_die (generic_decl_parm,
20956 parm, subr_die,
20957 &parm);
20958 else if (parm && !POINTER_BOUNDS_P (parm))
20960 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20962 if (parm == DECL_ARGUMENTS (decl)
20963 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20964 && parm_die
20965 && (dwarf_version >= 3 || !dwarf_strict))
20966 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20968 parm = DECL_CHAIN (parm);
20970 else if (parm)
20971 parm = DECL_CHAIN (parm);
20973 if (generic_decl_parm)
20974 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20977 /* Decide whether we need an unspecified_parameters DIE at the end.
20978 There are 2 more cases to do this for: 1) the ansi ... declaration -
20979 this is detectable when the end of the arg list is not a
20980 void_type_node 2) an unprototyped function declaration (not a
20981 definition). This just means that we have no info about the
20982 parameters at all. */
20983 if (early_dwarf)
20985 if (prototype_p (TREE_TYPE (decl)))
20987 /* This is the prototyped case, check for.... */
20988 if (stdarg_p (TREE_TYPE (decl)))
20989 gen_unspecified_parameters_die (decl, subr_die);
20991 else if (DECL_INITIAL (decl) == NULL_TREE)
20992 gen_unspecified_parameters_die (decl, subr_die);
20995 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
20996 have DIEs. */
20997 if (string_types == &string_types_vec)
20999 adjust_string_types ();
21000 string_types = NULL;
21004 if (subr_die != old_die)
21005 /* Add the calling convention attribute if requested. */
21006 add_calling_convention_attribute (subr_die, decl);
21008 /* Output Dwarf info for all of the stuff within the body of the function
21009 (if it has one - it may be just a declaration).
21011 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
21012 a function. This BLOCK actually represents the outermost binding contour
21013 for the function, i.e. the contour in which the function's formal
21014 parameters and labels get declared. Curiously, it appears that the front
21015 end doesn't actually put the PARM_DECL nodes for the current function onto
21016 the BLOCK_VARS list for this outer scope, but are strung off of the
21017 DECL_ARGUMENTS list for the function instead.
21019 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
21020 the LABEL_DECL nodes for the function however, and we output DWARF info
21021 for those in decls_for_scope. Just within the `outer_scope' there will be
21022 a BLOCK node representing the function's outermost pair of curly braces,
21023 and any blocks used for the base and member initializers of a C++
21024 constructor function. */
21025 tree outer_scope = DECL_INITIAL (decl);
21026 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
21028 int call_site_note_count = 0;
21029 int tail_call_site_note_count = 0;
21031 /* Emit a DW_TAG_variable DIE for a named return value. */
21032 if (DECL_NAME (DECL_RESULT (decl)))
21033 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
21035 /* The first time through decls_for_scope we will generate the
21036 DIEs for the locals. The second time, we fill in the
21037 location info. */
21038 decls_for_scope (outer_scope, subr_die);
21040 if (call_arg_locations && !dwarf_strict)
21042 struct call_arg_loc_node *ca_loc;
21043 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
21045 dw_die_ref die = NULL;
21046 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
21047 rtx arg, next_arg;
21049 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
21050 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
21051 : NULL_RTX);
21052 arg; arg = next_arg)
21054 dw_loc_descr_ref reg, val;
21055 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
21056 dw_die_ref cdie, tdie = NULL;
21058 next_arg = XEXP (arg, 1);
21059 if (REG_P (XEXP (XEXP (arg, 0), 0))
21060 && next_arg
21061 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
21062 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
21063 && REGNO (XEXP (XEXP (arg, 0), 0))
21064 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
21065 next_arg = XEXP (next_arg, 1);
21066 if (mode == VOIDmode)
21068 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
21069 if (mode == VOIDmode)
21070 mode = GET_MODE (XEXP (arg, 0));
21072 if (mode == VOIDmode || mode == BLKmode)
21073 continue;
21074 /* Get dynamic information about call target only if we
21075 have no static information: we cannot generate both
21076 DW_AT_abstract_origin and DW_AT_GNU_call_site_target
21077 attributes. */
21078 if (ca_loc->symbol_ref == NULL_RTX)
21080 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
21082 tloc = XEXP (XEXP (arg, 0), 1);
21083 continue;
21085 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
21086 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
21088 tlocc = XEXP (XEXP (arg, 0), 1);
21089 continue;
21092 reg = NULL;
21093 if (REG_P (XEXP (XEXP (arg, 0), 0)))
21094 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
21095 VAR_INIT_STATUS_INITIALIZED);
21096 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
21098 rtx mem = XEXP (XEXP (arg, 0), 0);
21099 reg = mem_loc_descriptor (XEXP (mem, 0),
21100 get_address_mode (mem),
21101 GET_MODE (mem),
21102 VAR_INIT_STATUS_INITIALIZED);
21104 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
21105 == DEBUG_PARAMETER_REF)
21107 tree tdecl
21108 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
21109 tdie = lookup_decl_die (tdecl);
21110 if (tdie == NULL)
21111 continue;
21113 else
21114 continue;
21115 if (reg == NULL
21116 && GET_CODE (XEXP (XEXP (arg, 0), 0))
21117 != DEBUG_PARAMETER_REF)
21118 continue;
21119 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
21120 VOIDmode,
21121 VAR_INIT_STATUS_INITIALIZED);
21122 if (val == NULL)
21123 continue;
21124 if (die == NULL)
21125 die = gen_call_site_die (decl, subr_die, ca_loc);
21126 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
21127 NULL_TREE);
21128 if (reg != NULL)
21129 add_AT_loc (cdie, DW_AT_location, reg);
21130 else if (tdie != NULL)
21131 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
21132 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
21133 if (next_arg != XEXP (arg, 1))
21135 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
21136 if (mode == VOIDmode)
21137 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
21138 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
21139 0), 1),
21140 mode, VOIDmode,
21141 VAR_INIT_STATUS_INITIALIZED);
21142 if (val != NULL)
21143 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
21146 if (die == NULL
21147 && (ca_loc->symbol_ref || tloc))
21148 die = gen_call_site_die (decl, subr_die, ca_loc);
21149 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
21151 dw_loc_descr_ref tval = NULL;
21153 if (tloc != NULL_RTX)
21154 tval = mem_loc_descriptor (tloc,
21155 GET_MODE (tloc) == VOIDmode
21156 ? Pmode : GET_MODE (tloc),
21157 VOIDmode,
21158 VAR_INIT_STATUS_INITIALIZED);
21159 if (tval)
21160 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
21161 else if (tlocc != NULL_RTX)
21163 tval = mem_loc_descriptor (tlocc,
21164 GET_MODE (tlocc) == VOIDmode
21165 ? Pmode : GET_MODE (tlocc),
21166 VOIDmode,
21167 VAR_INIT_STATUS_INITIALIZED);
21168 if (tval)
21169 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
21170 tval);
21173 if (die != NULL)
21175 call_site_note_count++;
21176 if (ca_loc->tail_call_p)
21177 tail_call_site_note_count++;
21181 call_arg_locations = NULL;
21182 call_arg_loc_last = NULL;
21183 if (tail_call_site_count >= 0
21184 && tail_call_site_count == tail_call_site_note_count
21185 && !dwarf_strict)
21187 if (call_site_count >= 0
21188 && call_site_count == call_site_note_count)
21189 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
21190 else
21191 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
21193 call_site_count = -1;
21194 tail_call_site_count = -1;
21197 /* Mark used types after we have created DIEs for the functions scopes. */
21198 premark_used_types (DECL_STRUCT_FUNCTION (decl));
21201 /* Returns a hash value for X (which really is a die_struct). */
21203 hashval_t
21204 block_die_hasher::hash (die_struct *d)
21206 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
21209 /* Return nonzero if decl_id and die_parent of die_struct X is the same
21210 as decl_id and die_parent of die_struct Y. */
21212 bool
21213 block_die_hasher::equal (die_struct *x, die_struct *y)
21215 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
21218 /* Return TRUE if DECL, which may have been previously generated as
21219 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
21220 true if decl (or its origin) is either an extern declaration or a
21221 class/namespace scoped declaration.
21223 The declare_in_namespace support causes us to get two DIEs for one
21224 variable, both of which are declarations. We want to avoid
21225 considering one to be a specification, so we must test for
21226 DECLARATION and DW_AT_declaration. */
21227 static inline bool
21228 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
21230 return (old_die && TREE_STATIC (decl) && !declaration
21231 && get_AT_flag (old_die, DW_AT_declaration) == 1);
21234 /* Return true if DECL is a local static. */
21236 static inline bool
21237 local_function_static (tree decl)
21239 gcc_assert (VAR_P (decl));
21240 return TREE_STATIC (decl)
21241 && DECL_CONTEXT (decl)
21242 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
21245 /* Generate a DIE to represent a declared data object.
21246 Either DECL or ORIGIN must be non-null. */
21248 static void
21249 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
21251 HOST_WIDE_INT off = 0;
21252 tree com_decl;
21253 tree decl_or_origin = decl ? decl : origin;
21254 tree ultimate_origin;
21255 dw_die_ref var_die;
21256 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
21257 dw_die_ref origin_die = NULL;
21258 bool declaration = (DECL_EXTERNAL (decl_or_origin)
21259 || class_or_namespace_scope_p (context_die));
21260 bool specialization_p = false;
21262 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21263 if (decl || ultimate_origin)
21264 origin = ultimate_origin;
21265 com_decl = fortran_common (decl_or_origin, &off);
21267 /* Symbol in common gets emitted as a child of the common block, in the form
21268 of a data member. */
21269 if (com_decl)
21271 dw_die_ref com_die;
21272 dw_loc_list_ref loc;
21273 die_node com_die_arg;
21275 var_die = lookup_decl_die (decl_or_origin);
21276 if (var_die)
21278 if (get_AT (var_die, DW_AT_location) == NULL)
21280 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21281 if (loc)
21283 if (off)
21285 /* Optimize the common case. */
21286 if (single_element_loc_list_p (loc)
21287 && loc->expr->dw_loc_opc == DW_OP_addr
21288 && loc->expr->dw_loc_next == NULL
21289 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21290 == SYMBOL_REF)
21292 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21293 loc->expr->dw_loc_oprnd1.v.val_addr
21294 = plus_constant (GET_MODE (x), x , off);
21296 else
21297 loc_list_plus_const (loc, off);
21299 add_AT_location_description (var_die, DW_AT_location, loc);
21300 remove_AT (var_die, DW_AT_declaration);
21303 return;
21306 if (common_block_die_table == NULL)
21307 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21309 com_die_arg.decl_id = DECL_UID (com_decl);
21310 com_die_arg.die_parent = context_die;
21311 com_die = common_block_die_table->find (&com_die_arg);
21312 loc = loc_list_from_tree (com_decl, 2, NULL);
21313 if (com_die == NULL)
21315 const char *cnam
21316 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21317 die_node **slot;
21319 com_die = new_die (DW_TAG_common_block, context_die, decl);
21320 add_name_and_src_coords_attributes (com_die, com_decl);
21321 if (loc)
21323 add_AT_location_description (com_die, DW_AT_location, loc);
21324 /* Avoid sharing the same loc descriptor between
21325 DW_TAG_common_block and DW_TAG_variable. */
21326 loc = loc_list_from_tree (com_decl, 2, NULL);
21328 else if (DECL_EXTERNAL (decl_or_origin))
21329 add_AT_flag (com_die, DW_AT_declaration, 1);
21330 if (want_pubnames ())
21331 add_pubname_string (cnam, com_die); /* ??? needed? */
21332 com_die->decl_id = DECL_UID (com_decl);
21333 slot = common_block_die_table->find_slot (com_die, INSERT);
21334 *slot = com_die;
21336 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21338 add_AT_location_description (com_die, DW_AT_location, loc);
21339 loc = loc_list_from_tree (com_decl, 2, NULL);
21340 remove_AT (com_die, DW_AT_declaration);
21342 var_die = new_die (DW_TAG_variable, com_die, decl);
21343 add_name_and_src_coords_attributes (var_die, decl_or_origin);
21344 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21345 decl_quals (decl_or_origin), false,
21346 context_die);
21347 add_AT_flag (var_die, DW_AT_external, 1);
21348 if (loc)
21350 if (off)
21352 /* Optimize the common case. */
21353 if (single_element_loc_list_p (loc)
21354 && loc->expr->dw_loc_opc == DW_OP_addr
21355 && loc->expr->dw_loc_next == NULL
21356 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21358 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21359 loc->expr->dw_loc_oprnd1.v.val_addr
21360 = plus_constant (GET_MODE (x), x, off);
21362 else
21363 loc_list_plus_const (loc, off);
21365 add_AT_location_description (var_die, DW_AT_location, loc);
21367 else if (DECL_EXTERNAL (decl_or_origin))
21368 add_AT_flag (var_die, DW_AT_declaration, 1);
21369 if (decl)
21370 equate_decl_number_to_die (decl, var_die);
21371 return;
21374 if (old_die)
21376 if (declaration)
21378 /* A declaration that has been previously dumped, needs no
21379 further annotations, since it doesn't need location on
21380 the second pass. */
21381 return;
21383 else if (decl_will_get_specification_p (old_die, decl, declaration)
21384 && !get_AT (old_die, DW_AT_specification))
21386 /* Fall-thru so we can make a new variable die along with a
21387 DW_AT_specification. */
21389 else if (origin && old_die->die_parent != context_die)
21391 /* If we will be creating an inlined instance, we need a
21392 new DIE that will get annotated with
21393 DW_AT_abstract_origin. Clear things so we can get a
21394 new DIE. */
21395 gcc_assert (!DECL_ABSTRACT_P (decl));
21396 old_die = NULL;
21398 else
21400 /* If a DIE was dumped early, it still needs location info.
21401 Skip to where we fill the location bits. */
21402 var_die = old_die;
21403 goto gen_variable_die_location;
21407 /* For static data members, the declaration in the class is supposed
21408 to have DW_TAG_member tag; the specification should still be
21409 DW_TAG_variable referencing the DW_TAG_member DIE. */
21410 if (declaration && class_scope_p (context_die))
21411 var_die = new_die (DW_TAG_member, context_die, decl);
21412 else
21413 var_die = new_die (DW_TAG_variable, context_die, decl);
21415 if (origin != NULL)
21416 origin_die = add_abstract_origin_attribute (var_die, origin);
21418 /* Loop unrolling can create multiple blocks that refer to the same
21419 static variable, so we must test for the DW_AT_declaration flag.
21421 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21422 copy decls and set the DECL_ABSTRACT_P flag on them instead of
21423 sharing them.
21425 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
21426 else if (decl_will_get_specification_p (old_die, decl, declaration))
21428 /* This is a definition of a C++ class level static. */
21429 add_AT_specification (var_die, old_die);
21430 specialization_p = true;
21431 if (DECL_NAME (decl))
21433 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21434 struct dwarf_file_data * file_index = lookup_filename (s.file);
21436 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21437 add_AT_file (var_die, DW_AT_decl_file, file_index);
21439 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21440 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21442 if (old_die->die_tag == DW_TAG_member)
21443 add_linkage_name (var_die, decl);
21446 else
21447 add_name_and_src_coords_attributes (var_die, decl);
21449 if ((origin == NULL && !specialization_p)
21450 || (origin != NULL
21451 && !DECL_ABSTRACT_P (decl_or_origin)
21452 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21453 decl_function_context
21454 (decl_or_origin))))
21456 tree type = TREE_TYPE (decl_or_origin);
21458 if (decl_by_reference_p (decl_or_origin))
21459 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21460 context_die);
21461 else
21462 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21463 context_die);
21466 if (origin == NULL && !specialization_p)
21468 if (TREE_PUBLIC (decl))
21469 add_AT_flag (var_die, DW_AT_external, 1);
21471 if (DECL_ARTIFICIAL (decl))
21472 add_AT_flag (var_die, DW_AT_artificial, 1);
21474 add_accessibility_attribute (var_die, decl);
21477 if (declaration)
21478 add_AT_flag (var_die, DW_AT_declaration, 1);
21480 if (decl && (DECL_ABSTRACT_P (decl)
21481 || !old_die || is_declaration_die (old_die)))
21482 equate_decl_number_to_die (decl, var_die);
21484 gen_variable_die_location:
21485 if (! declaration
21486 && (! DECL_ABSTRACT_P (decl_or_origin)
21487 /* Local static vars are shared between all clones/inlines,
21488 so emit DW_AT_location on the abstract DIE if DECL_RTL is
21489 already set. */
21490 || (VAR_P (decl_or_origin)
21491 && TREE_STATIC (decl_or_origin)
21492 && DECL_RTL_SET_P (decl_or_origin)))
21493 /* When abstract origin already has DW_AT_location attribute, no need
21494 to add it again. */
21495 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21497 if (early_dwarf)
21498 add_pubname (decl_or_origin, var_die);
21499 else
21500 add_location_or_const_value_attribute (var_die, decl_or_origin,
21501 decl == NULL);
21503 else
21504 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21507 /* Generate a DIE to represent a named constant. */
21509 static void
21510 gen_const_die (tree decl, dw_die_ref context_die)
21512 dw_die_ref const_die;
21513 tree type = TREE_TYPE (decl);
21515 const_die = lookup_decl_die (decl);
21516 if (const_die)
21517 return;
21519 const_die = new_die (DW_TAG_constant, context_die, decl);
21520 equate_decl_number_to_die (decl, const_die);
21521 add_name_and_src_coords_attributes (const_die, decl);
21522 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21523 if (TREE_PUBLIC (decl))
21524 add_AT_flag (const_die, DW_AT_external, 1);
21525 if (DECL_ARTIFICIAL (decl))
21526 add_AT_flag (const_die, DW_AT_artificial, 1);
21527 tree_add_const_value_attribute_for_decl (const_die, decl);
21530 /* Generate a DIE to represent a label identifier. */
21532 static void
21533 gen_label_die (tree decl, dw_die_ref context_die)
21535 tree origin = decl_ultimate_origin (decl);
21536 dw_die_ref lbl_die = lookup_decl_die (decl);
21537 rtx insn;
21538 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21540 if (!lbl_die)
21542 lbl_die = new_die (DW_TAG_label, context_die, decl);
21543 equate_decl_number_to_die (decl, lbl_die);
21545 if (origin != NULL)
21546 add_abstract_origin_attribute (lbl_die, origin);
21547 else
21548 add_name_and_src_coords_attributes (lbl_die, decl);
21551 if (DECL_ABSTRACT_P (decl))
21552 equate_decl_number_to_die (decl, lbl_die);
21553 else
21555 insn = DECL_RTL_IF_SET (decl);
21557 /* Deleted labels are programmer specified labels which have been
21558 eliminated because of various optimizations. We still emit them
21559 here so that it is possible to put breakpoints on them. */
21560 if (insn
21561 && (LABEL_P (insn)
21562 || ((NOTE_P (insn)
21563 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21565 /* When optimization is enabled (via -O) some parts of the compiler
21566 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21567 represent source-level labels which were explicitly declared by
21568 the user. This really shouldn't be happening though, so catch
21569 it if it ever does happen. */
21570 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21572 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21573 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21575 else if (insn
21576 && NOTE_P (insn)
21577 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21578 && CODE_LABEL_NUMBER (insn) != -1)
21580 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21581 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21586 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
21587 attributes to the DIE for a block STMT, to describe where the inlined
21588 function was called from. This is similar to add_src_coords_attributes. */
21590 static inline void
21591 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21593 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21595 if (dwarf_version >= 3 || !dwarf_strict)
21597 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21598 add_AT_unsigned (die, DW_AT_call_line, s.line);
21603 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21604 Add low_pc and high_pc attributes to the DIE for a block STMT. */
21606 static inline void
21607 add_high_low_attributes (tree stmt, dw_die_ref die)
21609 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21611 if (BLOCK_FRAGMENT_CHAIN (stmt)
21612 && (dwarf_version >= 3 || !dwarf_strict))
21614 tree chain, superblock = NULL_TREE;
21615 dw_die_ref pdie;
21616 dw_attr_node *attr = NULL;
21618 if (inlined_function_outer_scope_p (stmt))
21620 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21621 BLOCK_NUMBER (stmt));
21622 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21625 /* Optimize duplicate .debug_ranges lists or even tails of
21626 lists. If this BLOCK has same ranges as its supercontext,
21627 lookup DW_AT_ranges attribute in the supercontext (and
21628 recursively so), verify that the ranges_table contains the
21629 right values and use it instead of adding a new .debug_range. */
21630 for (chain = stmt, pdie = die;
21631 BLOCK_SAME_RANGE (chain);
21632 chain = BLOCK_SUPERCONTEXT (chain))
21634 dw_attr_node *new_attr;
21636 pdie = pdie->die_parent;
21637 if (pdie == NULL)
21638 break;
21639 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21640 break;
21641 new_attr = get_AT (pdie, DW_AT_ranges);
21642 if (new_attr == NULL
21643 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21644 break;
21645 attr = new_attr;
21646 superblock = BLOCK_SUPERCONTEXT (chain);
21648 if (attr != NULL
21649 && (ranges_table[attr->dw_attr_val.v.val_offset
21650 / 2 / DWARF2_ADDR_SIZE].num
21651 == BLOCK_NUMBER (superblock))
21652 && BLOCK_FRAGMENT_CHAIN (superblock))
21654 unsigned long off = attr->dw_attr_val.v.val_offset
21655 / 2 / DWARF2_ADDR_SIZE;
21656 unsigned long supercnt = 0, thiscnt = 0;
21657 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21658 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21660 ++supercnt;
21661 gcc_checking_assert (ranges_table[off + supercnt].num
21662 == BLOCK_NUMBER (chain));
21664 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21665 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21666 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21667 ++thiscnt;
21668 gcc_assert (supercnt >= thiscnt);
21669 add_AT_range_list (die, DW_AT_ranges,
21670 ((off + supercnt - thiscnt)
21671 * 2 * DWARF2_ADDR_SIZE),
21672 false);
21673 return;
21676 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21678 chain = BLOCK_FRAGMENT_CHAIN (stmt);
21681 add_ranges (chain);
21682 chain = BLOCK_FRAGMENT_CHAIN (chain);
21684 while (chain);
21685 add_ranges (NULL);
21687 else
21689 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21690 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21691 BLOCK_NUMBER (stmt));
21692 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21693 BLOCK_NUMBER (stmt));
21694 add_AT_low_high_pc (die, label, label_high, false);
21698 /* Generate a DIE for a lexical block. */
21700 static void
21701 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21703 dw_die_ref old_die = BLOCK_DIE (stmt);
21704 dw_die_ref stmt_die = NULL;
21705 if (!old_die)
21707 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21708 BLOCK_DIE (stmt) = stmt_die;
21711 if (BLOCK_ABSTRACT (stmt))
21713 if (old_die)
21715 /* This must have been generated early and it won't even
21716 need location information since it's a DW_AT_inline
21717 function. */
21718 if (flag_checking)
21719 for (dw_die_ref c = context_die; c; c = c->die_parent)
21720 if (c->die_tag == DW_TAG_inlined_subroutine
21721 || c->die_tag == DW_TAG_subprogram)
21723 gcc_assert (get_AT (c, DW_AT_inline));
21724 break;
21726 return;
21729 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21731 /* If this is an inlined instance, create a new lexical die for
21732 anything below to attach DW_AT_abstract_origin to. */
21733 if (old_die)
21735 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21736 BLOCK_DIE (stmt) = stmt_die;
21737 old_die = NULL;
21740 tree origin = block_ultimate_origin (stmt);
21741 if (origin != NULL_TREE && origin != stmt)
21742 add_abstract_origin_attribute (stmt_die, origin);
21745 if (old_die)
21746 stmt_die = old_die;
21748 /* A non abstract block whose blocks have already been reordered
21749 should have the instruction range for this block. If so, set the
21750 high/low attributes. */
21751 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21753 gcc_assert (stmt_die);
21754 add_high_low_attributes (stmt, stmt_die);
21757 decls_for_scope (stmt, stmt_die);
21760 /* Generate a DIE for an inlined subprogram. */
21762 static void
21763 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21765 tree decl;
21767 /* The instance of function that is effectively being inlined shall not
21768 be abstract. */
21769 gcc_assert (! BLOCK_ABSTRACT (stmt));
21771 decl = block_ultimate_origin (stmt);
21773 /* Make sure any inlined functions are known to be inlineable. */
21774 gcc_checking_assert (DECL_ABSTRACT_P (decl)
21775 || cgraph_function_possibly_inlined_p (decl));
21777 /* Emit info for the abstract instance first, if we haven't yet. We
21778 must emit this even if the block is abstract, otherwise when we
21779 emit the block below (or elsewhere), we may end up trying to emit
21780 a die whose origin die hasn't been emitted, and crashing. */
21781 dwarf2out_abstract_function (decl);
21783 if (! BLOCK_ABSTRACT (stmt))
21785 dw_die_ref subr_die
21786 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21788 if (call_arg_locations)
21789 BLOCK_DIE (stmt) = subr_die;
21790 add_abstract_origin_attribute (subr_die, decl);
21791 if (TREE_ASM_WRITTEN (stmt))
21792 add_high_low_attributes (stmt, subr_die);
21793 add_call_src_coords_attributes (stmt, subr_die);
21795 decls_for_scope (stmt, subr_die);
21799 /* Generate a DIE for a field in a record, or structure. CTX is required: see
21800 the comment for VLR_CONTEXT. */
21802 static void
21803 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21805 dw_die_ref decl_die;
21807 if (TREE_TYPE (decl) == error_mark_node)
21808 return;
21810 decl_die = new_die (DW_TAG_member, context_die, decl);
21811 add_name_and_src_coords_attributes (decl_die, decl);
21812 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21813 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21814 context_die);
21816 if (DECL_BIT_FIELD_TYPE (decl))
21818 add_byte_size_attribute (decl_die, decl);
21819 add_bit_size_attribute (decl_die, decl);
21820 add_bit_offset_attribute (decl_die, decl, ctx);
21823 /* If we have a variant part offset, then we are supposed to process a member
21824 of a QUAL_UNION_TYPE, which is how we represent variant parts in
21825 trees. */
21826 gcc_assert (ctx->variant_part_offset == NULL_TREE
21827 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21828 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21829 add_data_member_location_attribute (decl_die, decl, ctx);
21831 if (DECL_ARTIFICIAL (decl))
21832 add_AT_flag (decl_die, DW_AT_artificial, 1);
21834 add_accessibility_attribute (decl_die, decl);
21836 /* Equate decl number to die, so that we can look up this decl later on. */
21837 equate_decl_number_to_die (decl, decl_die);
21840 #if 0
21841 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21842 Use modified_type_die instead.
21843 We keep this code here just in case these types of DIEs may be needed to
21844 represent certain things in other languages (e.g. Pascal) someday. */
21846 static void
21847 gen_pointer_type_die (tree type, dw_die_ref context_die)
21849 dw_die_ref ptr_die
21850 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21852 equate_type_number_to_die (type, ptr_die);
21853 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21854 context_die);
21855 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21858 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21859 Use modified_type_die instead.
21860 We keep this code here just in case these types of DIEs may be needed to
21861 represent certain things in other languages (e.g. Pascal) someday. */
21863 static void
21864 gen_reference_type_die (tree type, dw_die_ref context_die)
21866 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21868 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21869 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21870 else
21871 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21873 equate_type_number_to_die (type, ref_die);
21874 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21875 context_die);
21876 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21878 #endif
21880 /* Generate a DIE for a pointer to a member type. */
21882 static void
21883 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21885 dw_die_ref ptr_die
21886 = new_die (DW_TAG_ptr_to_member_type,
21887 scope_die_for (type, context_die), type);
21889 equate_type_number_to_die (type, ptr_die);
21890 add_AT_die_ref (ptr_die, DW_AT_containing_type,
21891 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21892 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21893 context_die);
21896 static char *producer_string;
21898 /* Return a heap allocated producer string including command line options
21899 if -grecord-gcc-switches. */
21901 static char *
21902 gen_producer_string (void)
21904 size_t j;
21905 auto_vec<const char *> switches;
21906 const char *language_string = lang_hooks.name;
21907 char *producer, *tail;
21908 const char *p;
21909 size_t len = dwarf_record_gcc_switches ? 0 : 3;
21910 size_t plen = strlen (language_string) + 1 + strlen (version_string);
21912 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21913 switch (save_decoded_options[j].opt_index)
21915 case OPT_o:
21916 case OPT_d:
21917 case OPT_dumpbase:
21918 case OPT_dumpdir:
21919 case OPT_auxbase:
21920 case OPT_auxbase_strip:
21921 case OPT_quiet:
21922 case OPT_version:
21923 case OPT_v:
21924 case OPT_w:
21925 case OPT_L:
21926 case OPT_D:
21927 case OPT_I:
21928 case OPT_U:
21929 case OPT_SPECIAL_unknown:
21930 case OPT_SPECIAL_ignore:
21931 case OPT_SPECIAL_program_name:
21932 case OPT_SPECIAL_input_file:
21933 case OPT_grecord_gcc_switches:
21934 case OPT_gno_record_gcc_switches:
21935 case OPT__output_pch_:
21936 case OPT_fdiagnostics_show_location_:
21937 case OPT_fdiagnostics_show_option:
21938 case OPT_fdiagnostics_show_caret:
21939 case OPT_fdiagnostics_color_:
21940 case OPT_fverbose_asm:
21941 case OPT____:
21942 case OPT__sysroot_:
21943 case OPT_nostdinc:
21944 case OPT_nostdinc__:
21945 case OPT_fpreprocessed:
21946 case OPT_fltrans_output_list_:
21947 case OPT_fresolution_:
21948 case OPT_fdebug_prefix_map_:
21949 /* Ignore these. */
21950 continue;
21951 default:
21952 if (cl_options[save_decoded_options[j].opt_index].flags
21953 & CL_NO_DWARF_RECORD)
21954 continue;
21955 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21956 == '-');
21957 switch (save_decoded_options[j].canonical_option[0][1])
21959 case 'M':
21960 case 'i':
21961 case 'W':
21962 continue;
21963 case 'f':
21964 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21965 "dump", 4) == 0)
21966 continue;
21967 break;
21968 default:
21969 break;
21971 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21972 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21973 break;
21976 producer = XNEWVEC (char, plen + 1 + len + 1);
21977 tail = producer;
21978 sprintf (tail, "%s %s", language_string, version_string);
21979 tail += plen;
21981 FOR_EACH_VEC_ELT (switches, j, p)
21983 len = strlen (p);
21984 *tail = ' ';
21985 memcpy (tail + 1, p, len);
21986 tail += len + 1;
21989 *tail = '\0';
21990 return producer;
21993 /* Given a C and/or C++ language/version string return the "highest".
21994 C++ is assumed to be "higher" than C in this case. Used for merging
21995 LTO translation unit languages. */
21996 static const char *
21997 highest_c_language (const char *lang1, const char *lang2)
21999 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
22000 return "GNU C++14";
22001 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
22002 return "GNU C++11";
22003 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
22004 return "GNU C++98";
22006 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
22007 return "GNU C11";
22008 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
22009 return "GNU C99";
22010 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
22011 return "GNU C89";
22013 gcc_unreachable ();
22017 /* Generate the DIE for the compilation unit. */
22019 static dw_die_ref
22020 gen_compile_unit_die (const char *filename)
22022 dw_die_ref die;
22023 const char *language_string = lang_hooks.name;
22024 int language;
22026 die = new_die (DW_TAG_compile_unit, NULL, NULL);
22028 if (filename)
22030 add_name_attribute (die, filename);
22031 /* Don't add cwd for <built-in>. */
22032 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
22033 add_comp_dir_attribute (die);
22036 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
22038 /* If our producer is LTO try to figure out a common language to use
22039 from the global list of translation units. */
22040 if (strcmp (language_string, "GNU GIMPLE") == 0)
22042 unsigned i;
22043 tree t;
22044 const char *common_lang = NULL;
22046 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
22048 if (!TRANSLATION_UNIT_LANGUAGE (t))
22049 continue;
22050 if (!common_lang)
22051 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
22052 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
22054 else if (strncmp (common_lang, "GNU C", 5) == 0
22055 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
22056 /* Mixing C and C++ is ok, use C++ in that case. */
22057 common_lang = highest_c_language (common_lang,
22058 TRANSLATION_UNIT_LANGUAGE (t));
22059 else
22061 /* Fall back to C. */
22062 common_lang = NULL;
22063 break;
22067 if (common_lang)
22068 language_string = common_lang;
22071 language = DW_LANG_C;
22072 if (strncmp (language_string, "GNU C", 5) == 0
22073 && ISDIGIT (language_string[5]))
22075 language = DW_LANG_C89;
22076 if (dwarf_version >= 3 || !dwarf_strict)
22078 if (strcmp (language_string, "GNU C89") != 0)
22079 language = DW_LANG_C99;
22081 if (dwarf_version >= 5 /* || !dwarf_strict */)
22082 if (strcmp (language_string, "GNU C11") == 0)
22083 language = DW_LANG_C11;
22086 else if (strncmp (language_string, "GNU C++", 7) == 0)
22088 language = DW_LANG_C_plus_plus;
22089 if (dwarf_version >= 5 /* || !dwarf_strict */)
22091 if (strcmp (language_string, "GNU C++11") == 0)
22092 language = DW_LANG_C_plus_plus_11;
22093 else if (strcmp (language_string, "GNU C++14") == 0)
22094 language = DW_LANG_C_plus_plus_14;
22097 else if (strcmp (language_string, "GNU F77") == 0)
22098 language = DW_LANG_Fortran77;
22099 else if (strcmp (language_string, "GNU Pascal") == 0)
22100 language = DW_LANG_Pascal83;
22101 else if (dwarf_version >= 3 || !dwarf_strict)
22103 if (strcmp (language_string, "GNU Ada") == 0)
22104 language = DW_LANG_Ada95;
22105 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22107 language = DW_LANG_Fortran95;
22108 if (dwarf_version >= 5 /* || !dwarf_strict */)
22110 if (strcmp (language_string, "GNU Fortran2003") == 0)
22111 language = DW_LANG_Fortran03;
22112 else if (strcmp (language_string, "GNU Fortran2008") == 0)
22113 language = DW_LANG_Fortran08;
22116 else if (strcmp (language_string, "GNU Java") == 0)
22117 language = DW_LANG_Java;
22118 else if (strcmp (language_string, "GNU Objective-C") == 0)
22119 language = DW_LANG_ObjC;
22120 else if (strcmp (language_string, "GNU Objective-C++") == 0)
22121 language = DW_LANG_ObjC_plus_plus;
22122 else if (dwarf_version >= 5 || !dwarf_strict)
22124 if (strcmp (language_string, "GNU Go") == 0)
22125 language = DW_LANG_Go;
22128 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
22129 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22130 language = DW_LANG_Fortran90;
22132 add_AT_unsigned (die, DW_AT_language, language);
22134 switch (language)
22136 case DW_LANG_Fortran77:
22137 case DW_LANG_Fortran90:
22138 case DW_LANG_Fortran95:
22139 case DW_LANG_Fortran03:
22140 case DW_LANG_Fortran08:
22141 /* Fortran has case insensitive identifiers and the front-end
22142 lowercases everything. */
22143 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
22144 break;
22145 default:
22146 /* The default DW_ID_case_sensitive doesn't need to be specified. */
22147 break;
22149 return die;
22152 /* Generate the DIE for a base class. */
22154 static void
22155 gen_inheritance_die (tree binfo, tree access, tree type,
22156 dw_die_ref context_die)
22158 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
22159 struct vlr_context ctx = { type, NULL };
22161 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
22162 context_die);
22163 add_data_member_location_attribute (die, binfo, &ctx);
22165 if (BINFO_VIRTUAL_P (binfo))
22166 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22168 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
22169 children, otherwise the default is DW_ACCESS_public. In DWARF2
22170 the default has always been DW_ACCESS_private. */
22171 if (access == access_public_node)
22173 if (dwarf_version == 2
22174 || context_die->die_tag == DW_TAG_class_type)
22175 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
22177 else if (access == access_protected_node)
22178 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
22179 else if (dwarf_version > 2
22180 && context_die->die_tag != DW_TAG_class_type)
22181 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
22184 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
22185 structure. */
22186 static bool
22187 is_variant_part (tree decl)
22189 return (TREE_CODE (decl) == FIELD_DECL
22190 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
22193 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
22194 return the FIELD_DECL. Return NULL_TREE otherwise. */
22196 static tree
22197 analyze_discr_in_predicate (tree operand, tree struct_type)
22199 bool continue_stripping = true;
22200 while (continue_stripping)
22201 switch (TREE_CODE (operand))
22203 CASE_CONVERT:
22204 operand = TREE_OPERAND (operand, 0);
22205 break;
22206 default:
22207 continue_stripping = false;
22208 break;
22211 /* Match field access to members of struct_type only. */
22212 if (TREE_CODE (operand) == COMPONENT_REF
22213 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
22214 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
22215 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
22216 return TREE_OPERAND (operand, 1);
22217 else
22218 return NULL_TREE;
22221 /* Check that SRC is a constant integer that can be represented as a native
22222 integer constant (either signed or unsigned). If so, store it into DEST and
22223 return true. Return false otherwise. */
22225 static bool
22226 get_discr_value (tree src, dw_discr_value *dest)
22228 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
22230 if (TREE_CODE (src) != INTEGER_CST
22231 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
22232 return false;
22234 dest->pos = is_unsigned;
22235 if (is_unsigned)
22236 dest->v.uval = tree_to_uhwi (src);
22237 else
22238 dest->v.sval = tree_to_shwi (src);
22240 return true;
22243 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
22244 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
22245 store NULL_TREE in DISCR_DECL. Otherwise:
22247 - store the discriminant field in STRUCT_TYPE that controls the variant
22248 part to *DISCR_DECL
22250 - put in *DISCR_LISTS_P an array where for each variant, the item
22251 represents the corresponding matching list of discriminant values.
22253 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
22254 the above array.
22256 Note that when the array is allocated (i.e. when the analysis is
22257 successful), it is up to the caller to free the array. */
22259 static void
22260 analyze_variants_discr (tree variant_part_decl,
22261 tree struct_type,
22262 tree *discr_decl,
22263 dw_discr_list_ref **discr_lists_p,
22264 unsigned *discr_lists_length)
22266 tree variant_part_type = TREE_TYPE (variant_part_decl);
22267 tree variant;
22268 dw_discr_list_ref *discr_lists;
22269 unsigned i;
22271 /* Compute how many variants there are in this variant part. */
22272 *discr_lists_length = 0;
22273 for (variant = TYPE_FIELDS (variant_part_type);
22274 variant != NULL_TREE;
22275 variant = DECL_CHAIN (variant))
22276 ++*discr_lists_length;
22278 *discr_decl = NULL_TREE;
22279 *discr_lists_p
22280 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22281 sizeof (**discr_lists_p));
22282 discr_lists = *discr_lists_p;
22284 /* And then analyze all variants to extract discriminant information for all
22285 of them. This analysis is conservative: as soon as we detect something we
22286 do not support, abort everything and pretend we found nothing. */
22287 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22288 variant != NULL_TREE;
22289 variant = DECL_CHAIN (variant), ++i)
22291 tree match_expr = DECL_QUALIFIER (variant);
22293 /* Now, try to analyze the predicate and deduce a discriminant for
22294 it. */
22295 if (match_expr == boolean_true_node)
22296 /* Typically happens for the default variant: it matches all cases that
22297 previous variants rejected. Don't output any matching value for
22298 this one. */
22299 continue;
22301 /* The following loop tries to iterate over each discriminant
22302 possibility: single values or ranges. */
22303 while (match_expr != NULL_TREE)
22305 tree next_round_match_expr;
22306 tree candidate_discr = NULL_TREE;
22307 dw_discr_list_ref new_node = NULL;
22309 /* Possibilities are matched one after the other by nested
22310 TRUTH_ORIF_EXPR expressions. Process the current possibility and
22311 continue with the rest at next iteration. */
22312 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22314 next_round_match_expr = TREE_OPERAND (match_expr, 0);
22315 match_expr = TREE_OPERAND (match_expr, 1);
22317 else
22318 next_round_match_expr = NULL_TREE;
22320 if (match_expr == boolean_false_node)
22321 /* This sub-expression matches nothing: just wait for the next
22322 one. */
22325 else if (TREE_CODE (match_expr) == EQ_EXPR)
22327 /* We are matching: <discr_field> == <integer_cst>
22328 This sub-expression matches a single value. */
22329 tree integer_cst = TREE_OPERAND (match_expr, 1);
22331 candidate_discr
22332 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22333 struct_type);
22335 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22336 if (!get_discr_value (integer_cst,
22337 &new_node->dw_discr_lower_bound))
22338 goto abort;
22339 new_node->dw_discr_range = false;
22342 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22344 /* We are matching:
22345 <discr_field> > <integer_cst>
22346 && <discr_field> < <integer_cst>.
22347 This sub-expression matches the range of values between the
22348 two matched integer constants. Note that comparisons can be
22349 inclusive or exclusive. */
22350 tree candidate_discr_1, candidate_discr_2;
22351 tree lower_cst, upper_cst;
22352 bool lower_cst_included, upper_cst_included;
22353 tree lower_op = TREE_OPERAND (match_expr, 0);
22354 tree upper_op = TREE_OPERAND (match_expr, 1);
22356 /* When the comparison is exclusive, the integer constant is not
22357 the discriminant range bound we are looking for: we will have
22358 to increment or decrement it. */
22359 if (TREE_CODE (lower_op) == GE_EXPR)
22360 lower_cst_included = true;
22361 else if (TREE_CODE (lower_op) == GT_EXPR)
22362 lower_cst_included = false;
22363 else
22364 goto abort;
22366 if (TREE_CODE (upper_op) == LE_EXPR)
22367 upper_cst_included = true;
22368 else if (TREE_CODE (upper_op) == LT_EXPR)
22369 upper_cst_included = false;
22370 else
22371 goto abort;
22373 /* Extract the discriminant from the first operand and check it
22374 is consistant with the same analysis in the second
22375 operand. */
22376 candidate_discr_1
22377 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22378 struct_type);
22379 candidate_discr_2
22380 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22381 struct_type);
22382 if (candidate_discr_1 == candidate_discr_2)
22383 candidate_discr = candidate_discr_1;
22384 else
22385 goto abort;
22387 /* Extract bounds from both. */
22388 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22389 lower_cst = TREE_OPERAND (lower_op, 1);
22390 upper_cst = TREE_OPERAND (upper_op, 1);
22392 if (!lower_cst_included)
22393 lower_cst
22394 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22395 lower_cst,
22396 build_int_cst (TREE_TYPE (lower_cst), 1)));
22397 if (!upper_cst_included)
22398 upper_cst
22399 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22400 upper_cst,
22401 build_int_cst (TREE_TYPE (upper_cst), 1)));
22403 if (!get_discr_value (lower_cst,
22404 &new_node->dw_discr_lower_bound)
22405 || !get_discr_value (upper_cst,
22406 &new_node->dw_discr_upper_bound))
22407 goto abort;
22409 new_node->dw_discr_range = true;
22412 else
22413 /* Unsupported sub-expression: we cannot determine the set of
22414 matching discriminant values. Abort everything. */
22415 goto abort;
22417 /* If the discriminant info is not consistant with what we saw so
22418 far, consider the analysis failed and abort everything. */
22419 if (candidate_discr == NULL_TREE
22420 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22421 goto abort;
22422 else
22423 *discr_decl = candidate_discr;
22425 if (new_node != NULL)
22427 new_node->dw_discr_next = discr_lists[i];
22428 discr_lists[i] = new_node;
22430 match_expr = next_round_match_expr;
22434 /* If we reach this point, we could match everything we were interested
22435 in. */
22436 return;
22438 abort:
22439 /* Clean all data structure and return no result. */
22440 free (*discr_lists_p);
22441 *discr_lists_p = NULL;
22442 *discr_decl = NULL_TREE;
22445 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22446 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
22447 under CONTEXT_DIE.
22449 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22450 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
22451 this type, which are record types, represent the available variants and each
22452 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
22453 values are inferred from these attributes.
22455 In trees, the offsets for the fields inside these sub-records are relative
22456 to the variant part itself, whereas the corresponding DIEs should have
22457 offset attributes that are relative to the embedding record base address.
22458 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22459 must be an expression that computes the offset of the variant part to
22460 describe in DWARF. */
22462 static void
22463 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22464 dw_die_ref context_die)
22466 const tree variant_part_type = TREE_TYPE (variant_part_decl);
22467 tree variant_part_offset = vlr_ctx->variant_part_offset;
22468 struct loc_descr_context ctx = {
22469 vlr_ctx->struct_type, /* context_type */
22470 NULL_TREE, /* base_decl */
22471 NULL /* dpi */
22474 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22475 NULL_TREE if there is no such field. */
22476 tree discr_decl = NULL_TREE;
22477 dw_discr_list_ref *discr_lists;
22478 unsigned discr_lists_length = 0;
22479 unsigned i;
22481 dw_die_ref dwarf_proc_die = NULL;
22482 dw_die_ref variant_part_die
22483 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22485 equate_decl_number_to_die (variant_part_decl, variant_part_die);
22487 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22488 &discr_decl, &discr_lists, &discr_lists_length);
22490 if (discr_decl != NULL_TREE)
22492 dw_die_ref discr_die = lookup_decl_die (discr_decl);
22494 if (discr_die)
22495 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22496 else
22497 /* We have no DIE for the discriminant, so just discard all
22498 discrimimant information in the output. */
22499 discr_decl = NULL_TREE;
22502 /* If the offset for this variant part is more complex than a constant,
22503 create a DWARF procedure for it so that we will not have to generate DWARF
22504 expressions for it for each member. */
22505 if (TREE_CODE (variant_part_offset) != INTEGER_CST
22506 && (dwarf_version >= 3 || !dwarf_strict))
22508 const tree dwarf_proc_fndecl
22509 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22510 build_function_type (TREE_TYPE (variant_part_offset),
22511 NULL_TREE));
22512 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22513 const dw_loc_descr_ref dwarf_proc_body
22514 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22516 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22517 dwarf_proc_fndecl, context_die);
22518 if (dwarf_proc_die != NULL)
22519 variant_part_offset = dwarf_proc_call;
22522 /* Output DIEs for all variants. */
22523 i = 0;
22524 for (tree variant = TYPE_FIELDS (variant_part_type);
22525 variant != NULL_TREE;
22526 variant = DECL_CHAIN (variant), ++i)
22528 tree variant_type = TREE_TYPE (variant);
22529 dw_die_ref variant_die;
22531 /* All variants (i.e. members of a variant part) are supposed to be
22532 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
22533 under these records. */
22534 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22536 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22537 equate_decl_number_to_die (variant, variant_die);
22539 /* Output discriminant values this variant matches, if any. */
22540 if (discr_decl == NULL || discr_lists[i] == NULL)
22541 /* In the case we have discriminant information at all, this is
22542 probably the default variant: as the standard says, don't
22543 output any discriminant value/list attribute. */
22545 else if (discr_lists[i]->dw_discr_next == NULL
22546 && !discr_lists[i]->dw_discr_range)
22547 /* If there is only one accepted value, don't bother outputting a
22548 list. */
22549 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22550 else
22551 add_discr_list (variant_die, discr_lists[i]);
22553 for (tree member = TYPE_FIELDS (variant_type);
22554 member != NULL_TREE;
22555 member = DECL_CHAIN (member))
22557 struct vlr_context vlr_sub_ctx = {
22558 vlr_ctx->struct_type, /* struct_type */
22559 NULL /* variant_part_offset */
22561 if (is_variant_part (member))
22563 /* All offsets for fields inside variant parts are relative to
22564 the top-level embedding RECORD_TYPE's base address. On the
22565 other hand, offsets in GCC's types are relative to the
22566 nested-most variant part. So we have to sum offsets each time
22567 we recurse. */
22569 vlr_sub_ctx.variant_part_offset
22570 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22571 variant_part_offset, byte_position (member)));
22572 gen_variant_part (member, &vlr_sub_ctx, variant_die);
22574 else
22576 vlr_sub_ctx.variant_part_offset = variant_part_offset;
22577 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22582 free (discr_lists);
22585 /* Generate a DIE for a class member. */
22587 static void
22588 gen_member_die (tree type, dw_die_ref context_die)
22590 tree member;
22591 tree binfo = TYPE_BINFO (type);
22592 dw_die_ref child;
22594 /* If this is not an incomplete type, output descriptions of each of its
22595 members. Note that as we output the DIEs necessary to represent the
22596 members of this record or union type, we will also be trying to output
22597 DIEs to represent the *types* of those members. However the `type'
22598 function (above) will specifically avoid generating type DIEs for member
22599 types *within* the list of member DIEs for this (containing) type except
22600 for those types (of members) which are explicitly marked as also being
22601 members of this (containing) type themselves. The g++ front- end can
22602 force any given type to be treated as a member of some other (containing)
22603 type by setting the TYPE_CONTEXT of the given (member) type to point to
22604 the TREE node representing the appropriate (containing) type. */
22606 /* First output info about the base classes. */
22607 if (binfo)
22609 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22610 int i;
22611 tree base;
22613 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22614 gen_inheritance_die (base,
22615 (accesses ? (*accesses)[i] : access_public_node),
22616 type,
22617 context_die);
22620 /* Now output info about the data members and type members. */
22621 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22623 struct vlr_context vlr_ctx = { type, NULL_TREE };
22625 /* If we thought we were generating minimal debug info for TYPE
22626 and then changed our minds, some of the member declarations
22627 may have already been defined. Don't define them again, but
22628 do put them in the right order. */
22630 child = lookup_decl_die (member);
22631 if (child)
22632 splice_child_die (context_die, child);
22634 /* Do not generate standard DWARF for variant parts if we are generating
22635 the corresponding GNAT encodings: DIEs generated for both would
22636 conflict in our mappings. */
22637 else if (is_variant_part (member)
22638 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22640 vlr_ctx.variant_part_offset = byte_position (member);
22641 gen_variant_part (member, &vlr_ctx, context_die);
22643 else
22645 vlr_ctx.variant_part_offset = NULL_TREE;
22646 gen_decl_die (member, NULL, &vlr_ctx, context_die);
22650 /* We do not keep type methods in type variants. */
22651 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22652 /* Now output info about the function members (if any). */
22653 if (TYPE_METHODS (type) != error_mark_node)
22654 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22656 /* Don't include clones in the member list. */
22657 if (DECL_ABSTRACT_ORIGIN (member))
22658 continue;
22659 /* Nor constructors for anonymous classes. */
22660 if (DECL_ARTIFICIAL (member)
22661 && dwarf2_name (member, 0) == NULL)
22662 continue;
22664 child = lookup_decl_die (member);
22665 if (child)
22666 splice_child_die (context_die, child);
22667 else
22668 gen_decl_die (member, NULL, NULL, context_die);
22672 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
22673 is set, we pretend that the type was never defined, so we only get the
22674 member DIEs needed by later specification DIEs. */
22676 static void
22677 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22678 enum debug_info_usage usage)
22680 if (TREE_ASM_WRITTEN (type))
22682 /* Fill in the bound of variable-length fields in late dwarf if
22683 still incomplete. */
22684 if (!early_dwarf && variably_modified_type_p (type, NULL))
22685 for (tree member = TYPE_FIELDS (type);
22686 member;
22687 member = DECL_CHAIN (member))
22688 fill_variable_array_bounds (TREE_TYPE (member));
22689 return;
22692 dw_die_ref type_die = lookup_type_die (type);
22693 dw_die_ref scope_die = 0;
22694 int nested = 0;
22695 int complete = (TYPE_SIZE (type)
22696 && (! TYPE_STUB_DECL (type)
22697 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22698 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22699 complete = complete && should_emit_struct_debug (type, usage);
22701 if (type_die && ! complete)
22702 return;
22704 if (TYPE_CONTEXT (type) != NULL_TREE
22705 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22706 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22707 nested = 1;
22709 scope_die = scope_die_for (type, context_die);
22711 /* Generate child dies for template paramaters. */
22712 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22713 schedule_generic_params_dies_gen (type);
22715 if (! type_die || (nested && is_cu_die (scope_die)))
22716 /* First occurrence of type or toplevel definition of nested class. */
22718 dw_die_ref old_die = type_die;
22720 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22721 ? record_type_tag (type) : DW_TAG_union_type,
22722 scope_die, type);
22723 equate_type_number_to_die (type, type_die);
22724 if (old_die)
22725 add_AT_specification (type_die, old_die);
22726 else
22727 add_name_attribute (type_die, type_tag (type));
22729 else
22730 remove_AT (type_die, DW_AT_declaration);
22732 /* If this type has been completed, then give it a byte_size attribute and
22733 then give a list of members. */
22734 if (complete && !ns_decl)
22736 /* Prevent infinite recursion in cases where the type of some member of
22737 this type is expressed in terms of this type itself. */
22738 TREE_ASM_WRITTEN (type) = 1;
22739 add_byte_size_attribute (type_die, type);
22740 if (TYPE_STUB_DECL (type) != NULL_TREE)
22742 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22743 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22746 /* If the first reference to this type was as the return type of an
22747 inline function, then it may not have a parent. Fix this now. */
22748 if (type_die->die_parent == NULL)
22749 add_child_die (scope_die, type_die);
22751 push_decl_scope (type);
22752 gen_member_die (type, type_die);
22753 pop_decl_scope ();
22755 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22756 if (TYPE_ARTIFICIAL (type))
22757 add_AT_flag (type_die, DW_AT_artificial, 1);
22759 /* GNU extension: Record what type our vtable lives in. */
22760 if (TYPE_VFIELD (type))
22762 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22764 gen_type_die (vtype, context_die);
22765 add_AT_die_ref (type_die, DW_AT_containing_type,
22766 lookup_type_die (vtype));
22769 else
22771 add_AT_flag (type_die, DW_AT_declaration, 1);
22773 /* We don't need to do this for function-local types. */
22774 if (TYPE_STUB_DECL (type)
22775 && ! decl_function_context (TYPE_STUB_DECL (type)))
22776 vec_safe_push (incomplete_types, type);
22779 if (get_AT (type_die, DW_AT_name))
22780 add_pubtype (type, type_die);
22783 /* Generate a DIE for a subroutine _type_. */
22785 static void
22786 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22788 tree return_type = TREE_TYPE (type);
22789 dw_die_ref subr_die
22790 = new_die (DW_TAG_subroutine_type,
22791 scope_die_for (type, context_die), type);
22793 equate_type_number_to_die (type, subr_die);
22794 add_prototyped_attribute (subr_die, type);
22795 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22796 context_die);
22797 gen_formal_types_die (type, subr_die);
22799 if (get_AT (subr_die, DW_AT_name))
22800 add_pubtype (type, subr_die);
22803 /* Generate a DIE for a type definition. */
22805 static void
22806 gen_typedef_die (tree decl, dw_die_ref context_die)
22808 dw_die_ref type_die;
22809 tree origin;
22811 if (TREE_ASM_WRITTEN (decl))
22813 if (DECL_ORIGINAL_TYPE (decl))
22814 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22815 return;
22818 TREE_ASM_WRITTEN (decl) = 1;
22819 type_die = new_die (DW_TAG_typedef, context_die, decl);
22820 origin = decl_ultimate_origin (decl);
22821 if (origin != NULL)
22822 add_abstract_origin_attribute (type_die, origin);
22823 else
22825 tree type;
22827 add_name_and_src_coords_attributes (type_die, decl);
22828 if (DECL_ORIGINAL_TYPE (decl))
22830 type = DECL_ORIGINAL_TYPE (decl);
22832 if (type == error_mark_node)
22833 return;
22835 gcc_assert (type != TREE_TYPE (decl));
22836 equate_type_number_to_die (TREE_TYPE (decl), type_die);
22838 else
22840 type = TREE_TYPE (decl);
22842 if (type == error_mark_node)
22843 return;
22845 if (is_naming_typedef_decl (TYPE_NAME (type)))
22847 /* Here, we are in the case of decl being a typedef naming
22848 an anonymous type, e.g:
22849 typedef struct {...} foo;
22850 In that case TREE_TYPE (decl) is not a typedef variant
22851 type and TYPE_NAME of the anonymous type is set to the
22852 TYPE_DECL of the typedef. This construct is emitted by
22853 the C++ FE.
22855 TYPE is the anonymous struct named by the typedef
22856 DECL. As we need the DW_AT_type attribute of the
22857 DW_TAG_typedef to point to the DIE of TYPE, let's
22858 generate that DIE right away. add_type_attribute
22859 called below will then pick (via lookup_type_die) that
22860 anonymous struct DIE. */
22861 if (!TREE_ASM_WRITTEN (type))
22862 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22864 /* This is a GNU Extension. We are adding a
22865 DW_AT_linkage_name attribute to the DIE of the
22866 anonymous struct TYPE. The value of that attribute
22867 is the name of the typedef decl naming the anonymous
22868 struct. This greatly eases the work of consumers of
22869 this debug info. */
22870 add_linkage_name_raw (lookup_type_die (type), decl);
22874 add_type_attribute (type_die, type, decl_quals (decl), false,
22875 context_die);
22877 if (is_naming_typedef_decl (decl))
22878 /* We want that all subsequent calls to lookup_type_die with
22879 TYPE in argument yield the DW_TAG_typedef we have just
22880 created. */
22881 equate_type_number_to_die (type, type_die);
22883 add_accessibility_attribute (type_die, decl);
22886 if (DECL_ABSTRACT_P (decl))
22887 equate_decl_number_to_die (decl, type_die);
22889 if (get_AT (type_die, DW_AT_name))
22890 add_pubtype (decl, type_die);
22893 /* Generate a DIE for a struct, class, enum or union type. */
22895 static void
22896 gen_tagged_type_die (tree type,
22897 dw_die_ref context_die,
22898 enum debug_info_usage usage)
22900 int need_pop;
22902 if (type == NULL_TREE
22903 || !is_tagged_type (type))
22904 return;
22906 if (TREE_ASM_WRITTEN (type))
22907 need_pop = 0;
22908 /* If this is a nested type whose containing class hasn't been written
22909 out yet, writing it out will cover this one, too. This does not apply
22910 to instantiations of member class templates; they need to be added to
22911 the containing class as they are generated. FIXME: This hurts the
22912 idea of combining type decls from multiple TUs, since we can't predict
22913 what set of template instantiations we'll get. */
22914 else if (TYPE_CONTEXT (type)
22915 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22916 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22918 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22920 if (TREE_ASM_WRITTEN (type))
22921 return;
22923 /* If that failed, attach ourselves to the stub. */
22924 push_decl_scope (TYPE_CONTEXT (type));
22925 context_die = lookup_type_die (TYPE_CONTEXT (type));
22926 need_pop = 1;
22928 else if (TYPE_CONTEXT (type) != NULL_TREE
22929 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22931 /* If this type is local to a function that hasn't been written
22932 out yet, use a NULL context for now; it will be fixed up in
22933 decls_for_scope. */
22934 context_die = lookup_decl_die (TYPE_CONTEXT (type));
22935 /* A declaration DIE doesn't count; nested types need to go in the
22936 specification. */
22937 if (context_die && is_declaration_die (context_die))
22938 context_die = NULL;
22939 need_pop = 0;
22941 else
22943 context_die = declare_in_namespace (type, context_die);
22944 need_pop = 0;
22947 if (TREE_CODE (type) == ENUMERAL_TYPE)
22949 /* This might have been written out by the call to
22950 declare_in_namespace. */
22951 if (!TREE_ASM_WRITTEN (type))
22952 gen_enumeration_type_die (type, context_die);
22954 else
22955 gen_struct_or_union_type_die (type, context_die, usage);
22957 if (need_pop)
22958 pop_decl_scope ();
22960 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22961 it up if it is ever completed. gen_*_type_die will set it for us
22962 when appropriate. */
22965 /* Generate a type description DIE. */
22967 static void
22968 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22969 enum debug_info_usage usage)
22971 struct array_descr_info info;
22973 if (type == NULL_TREE || type == error_mark_node)
22974 return;
22976 if (flag_checking && type)
22977 verify_type (type);
22979 if (TYPE_NAME (type) != NULL_TREE
22980 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22981 && is_redundant_typedef (TYPE_NAME (type))
22982 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22983 /* The DECL of this type is a typedef we don't want to emit debug
22984 info for but we want debug info for its underlying typedef.
22985 This can happen for e.g, the injected-class-name of a C++
22986 type. */
22987 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22989 /* If TYPE is a typedef type variant, let's generate debug info
22990 for the parent typedef which TYPE is a type of. */
22991 if (typedef_variant_p (type))
22993 if (TREE_ASM_WRITTEN (type))
22994 return;
22996 /* Prevent broken recursion; we can't hand off to the same type. */
22997 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22999 /* Give typedefs the right scope. */
23000 context_die = scope_die_for (type, context_die);
23002 TREE_ASM_WRITTEN (type) = 1;
23004 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
23005 return;
23008 /* If type is an anonymous tagged type named by a typedef, let's
23009 generate debug info for the typedef. */
23010 if (is_naming_typedef_decl (TYPE_NAME (type)))
23012 /* Use the DIE of the containing namespace as the parent DIE of
23013 the type description DIE we want to generate. */
23014 if (DECL_CONTEXT (TYPE_NAME (type))
23015 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
23016 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
23018 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
23019 return;
23022 /* We are going to output a DIE to represent the unqualified version
23023 of this type (i.e. without any const or volatile qualifiers) so
23024 get the main variant (i.e. the unqualified version) of this type
23025 now. (Vectors and arrays are special because the debugging info is in the
23026 cloned type itself). */
23027 if (TREE_CODE (type) != VECTOR_TYPE
23028 && TREE_CODE (type) != ARRAY_TYPE)
23029 type = type_main_variant (type);
23031 /* If this is an array type with hidden descriptor, handle it first. */
23032 if (!TREE_ASM_WRITTEN (type)
23033 && lang_hooks.types.get_array_descr_info)
23035 memset (&info, 0, sizeof (info));
23036 if (lang_hooks.types.get_array_descr_info (type, &info))
23038 /* Fortran sometimes emits array types with no dimension. */
23039 gcc_assert (info.ndimensions >= 0
23040 && (info.ndimensions
23041 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
23042 gen_descr_array_type_die (type, &info, context_die);
23043 TREE_ASM_WRITTEN (type) = 1;
23044 return;
23048 if (TREE_ASM_WRITTEN (type))
23050 /* Variable-length types may be incomplete even if
23051 TREE_ASM_WRITTEN. For such types, fall through to
23052 gen_array_type_die() and possibly fill in
23053 DW_AT_{upper,lower}_bound attributes. */
23054 if ((TREE_CODE (type) != ARRAY_TYPE
23055 && TREE_CODE (type) != RECORD_TYPE
23056 && TREE_CODE (type) != UNION_TYPE
23057 && TREE_CODE (type) != QUAL_UNION_TYPE)
23058 || !variably_modified_type_p (type, NULL))
23059 return;
23062 switch (TREE_CODE (type))
23064 case ERROR_MARK:
23065 break;
23067 case POINTER_TYPE:
23068 case REFERENCE_TYPE:
23069 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
23070 ensures that the gen_type_die recursion will terminate even if the
23071 type is recursive. Recursive types are possible in Ada. */
23072 /* ??? We could perhaps do this for all types before the switch
23073 statement. */
23074 TREE_ASM_WRITTEN (type) = 1;
23076 /* For these types, all that is required is that we output a DIE (or a
23077 set of DIEs) to represent the "basis" type. */
23078 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23079 DINFO_USAGE_IND_USE);
23080 break;
23082 case OFFSET_TYPE:
23083 /* This code is used for C++ pointer-to-data-member types.
23084 Output a description of the relevant class type. */
23085 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
23086 DINFO_USAGE_IND_USE);
23088 /* Output a description of the type of the object pointed to. */
23089 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23090 DINFO_USAGE_IND_USE);
23092 /* Now output a DIE to represent this pointer-to-data-member type
23093 itself. */
23094 gen_ptr_to_mbr_type_die (type, context_die);
23095 break;
23097 case FUNCTION_TYPE:
23098 /* Force out return type (in case it wasn't forced out already). */
23099 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23100 DINFO_USAGE_DIR_USE);
23101 gen_subroutine_type_die (type, context_die);
23102 break;
23104 case METHOD_TYPE:
23105 /* Force out return type (in case it wasn't forced out already). */
23106 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23107 DINFO_USAGE_DIR_USE);
23108 gen_subroutine_type_die (type, context_die);
23109 break;
23111 case ARRAY_TYPE:
23112 case VECTOR_TYPE:
23113 gen_array_type_die (type, context_die);
23114 break;
23116 case ENUMERAL_TYPE:
23117 case RECORD_TYPE:
23118 case UNION_TYPE:
23119 case QUAL_UNION_TYPE:
23120 gen_tagged_type_die (type, context_die, usage);
23121 return;
23123 case VOID_TYPE:
23124 case INTEGER_TYPE:
23125 case REAL_TYPE:
23126 case FIXED_POINT_TYPE:
23127 case COMPLEX_TYPE:
23128 case BOOLEAN_TYPE:
23129 case POINTER_BOUNDS_TYPE:
23130 /* No DIEs needed for fundamental types. */
23131 break;
23133 case NULLPTR_TYPE:
23134 case LANG_TYPE:
23135 /* Just use DW_TAG_unspecified_type. */
23137 dw_die_ref type_die = lookup_type_die (type);
23138 if (type_die == NULL)
23140 tree name = TYPE_IDENTIFIER (type);
23141 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
23142 type);
23143 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
23144 equate_type_number_to_die (type, type_die);
23147 break;
23149 default:
23150 if (is_cxx_auto (type))
23152 tree name = TYPE_IDENTIFIER (type);
23153 dw_die_ref *die = (name == get_identifier ("auto")
23154 ? &auto_die : &decltype_auto_die);
23155 if (!*die)
23157 *die = new_die (DW_TAG_unspecified_type,
23158 comp_unit_die (), NULL_TREE);
23159 add_name_attribute (*die, IDENTIFIER_POINTER (name));
23161 equate_type_number_to_die (type, *die);
23162 break;
23164 gcc_unreachable ();
23167 TREE_ASM_WRITTEN (type) = 1;
23170 static void
23171 gen_type_die (tree type, dw_die_ref context_die)
23173 if (type != error_mark_node)
23175 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
23176 if (flag_checking)
23178 dw_die_ref die = lookup_type_die (type);
23179 if (die)
23180 check_die (die);
23185 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
23186 things which are local to the given block. */
23188 static void
23189 gen_block_die (tree stmt, dw_die_ref context_die)
23191 int must_output_die = 0;
23192 bool inlined_func;
23194 /* Ignore blocks that are NULL. */
23195 if (stmt == NULL_TREE)
23196 return;
23198 inlined_func = inlined_function_outer_scope_p (stmt);
23200 /* If the block is one fragment of a non-contiguous block, do not
23201 process the variables, since they will have been done by the
23202 origin block. Do process subblocks. */
23203 if (BLOCK_FRAGMENT_ORIGIN (stmt))
23205 tree sub;
23207 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
23208 gen_block_die (sub, context_die);
23210 return;
23213 /* Determine if we need to output any Dwarf DIEs at all to represent this
23214 block. */
23215 if (inlined_func)
23216 /* The outer scopes for inlinings *must* always be represented. We
23217 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
23218 must_output_die = 1;
23219 else
23221 /* Determine if this block directly contains any "significant"
23222 local declarations which we will need to output DIEs for. */
23223 if (debug_info_level > DINFO_LEVEL_TERSE)
23224 /* We are not in terse mode so *any* local declaration counts
23225 as being a "significant" one. */
23226 must_output_die = ((BLOCK_VARS (stmt) != NULL
23227 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
23228 && (TREE_USED (stmt)
23229 || TREE_ASM_WRITTEN (stmt)
23230 || BLOCK_ABSTRACT (stmt)));
23231 else if ((TREE_USED (stmt)
23232 || TREE_ASM_WRITTEN (stmt)
23233 || BLOCK_ABSTRACT (stmt))
23234 && !dwarf2out_ignore_block (stmt))
23235 must_output_die = 1;
23238 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
23239 DIE for any block which contains no significant local declarations at
23240 all. Rather, in such cases we just call `decls_for_scope' so that any
23241 needed Dwarf info for any sub-blocks will get properly generated. Note
23242 that in terse mode, our definition of what constitutes a "significant"
23243 local declaration gets restricted to include only inlined function
23244 instances and local (nested) function definitions. */
23245 if (must_output_die)
23247 if (inlined_func)
23249 /* If STMT block is abstract, that means we have been called
23250 indirectly from dwarf2out_abstract_function.
23251 That function rightfully marks the descendent blocks (of
23252 the abstract function it is dealing with) as being abstract,
23253 precisely to prevent us from emitting any
23254 DW_TAG_inlined_subroutine DIE as a descendent
23255 of an abstract function instance. So in that case, we should
23256 not call gen_inlined_subroutine_die.
23258 Later though, when cgraph asks dwarf2out to emit info
23259 for the concrete instance of the function decl into which
23260 the concrete instance of STMT got inlined, the later will lead
23261 to the generation of a DW_TAG_inlined_subroutine DIE. */
23262 if (! BLOCK_ABSTRACT (stmt))
23263 gen_inlined_subroutine_die (stmt, context_die);
23265 else
23266 gen_lexical_block_die (stmt, context_die);
23268 else
23269 decls_for_scope (stmt, context_die);
23272 /* Process variable DECL (or variable with origin ORIGIN) within
23273 block STMT and add it to CONTEXT_DIE. */
23274 static void
23275 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23277 dw_die_ref die;
23278 tree decl_or_origin = decl ? decl : origin;
23280 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23281 die = lookup_decl_die (decl_or_origin);
23282 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
23284 if (TYPE_DECL_IS_STUB (decl_or_origin))
23285 die = lookup_type_die (TREE_TYPE (decl_or_origin));
23286 else
23287 die = lookup_decl_die (decl_or_origin);
23288 /* Avoid re-creating the DIE late if it was optimized as unused early. */
23289 if (! die && ! early_dwarf)
23290 return;
23292 else
23293 die = NULL;
23295 if (die != NULL && die->die_parent == NULL)
23296 add_child_die (context_die, die);
23297 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23299 if (early_dwarf)
23300 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23301 stmt, context_die);
23303 else
23304 gen_decl_die (decl, origin, NULL, context_die);
23307 /* Generate all of the decls declared within a given scope and (recursively)
23308 all of its sub-blocks. */
23310 static void
23311 decls_for_scope (tree stmt, dw_die_ref context_die)
23313 tree decl;
23314 unsigned int i;
23315 tree subblocks;
23317 /* Ignore NULL blocks. */
23318 if (stmt == NULL_TREE)
23319 return;
23321 /* Output the DIEs to represent all of the data objects and typedefs
23322 declared directly within this block but not within any nested
23323 sub-blocks. Also, nested function and tag DIEs have been
23324 generated with a parent of NULL; fix that up now. We don't
23325 have to do this if we're at -g1. */
23326 if (debug_info_level > DINFO_LEVEL_TERSE)
23328 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23329 process_scope_var (stmt, decl, NULL_TREE, context_die);
23330 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23331 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23332 context_die);
23335 /* Even if we're at -g1, we need to process the subblocks in order to get
23336 inlined call information. */
23338 /* Output the DIEs to represent all sub-blocks (and the items declared
23339 therein) of this block. */
23340 for (subblocks = BLOCK_SUBBLOCKS (stmt);
23341 subblocks != NULL;
23342 subblocks = BLOCK_CHAIN (subblocks))
23343 gen_block_die (subblocks, context_die);
23346 /* Is this a typedef we can avoid emitting? */
23348 bool
23349 is_redundant_typedef (const_tree decl)
23351 if (TYPE_DECL_IS_STUB (decl))
23352 return true;
23354 if (DECL_ARTIFICIAL (decl)
23355 && DECL_CONTEXT (decl)
23356 && is_tagged_type (DECL_CONTEXT (decl))
23357 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23358 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23359 /* Also ignore the artificial member typedef for the class name. */
23360 return true;
23362 return false;
23365 /* Return TRUE if TYPE is a typedef that names a type for linkage
23366 purposes. This kind of typedefs is produced by the C++ FE for
23367 constructs like:
23369 typedef struct {...} foo;
23371 In that case, there is no typedef variant type produced for foo.
23372 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23373 struct type. */
23375 static bool
23376 is_naming_typedef_decl (const_tree decl)
23378 if (decl == NULL_TREE
23379 || TREE_CODE (decl) != TYPE_DECL
23380 || DECL_NAMELESS (decl)
23381 || !is_tagged_type (TREE_TYPE (decl))
23382 || DECL_IS_BUILTIN (decl)
23383 || is_redundant_typedef (decl)
23384 /* It looks like Ada produces TYPE_DECLs that are very similar
23385 to C++ naming typedefs but that have different
23386 semantics. Let's be specific to c++ for now. */
23387 || !is_cxx ())
23388 return FALSE;
23390 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23391 && TYPE_NAME (TREE_TYPE (decl)) == decl
23392 && (TYPE_STUB_DECL (TREE_TYPE (decl))
23393 != TYPE_NAME (TREE_TYPE (decl))));
23396 /* Looks up the DIE for a context. */
23398 static inline dw_die_ref
23399 lookup_context_die (tree context)
23401 if (context)
23403 /* Find die that represents this context. */
23404 if (TYPE_P (context))
23406 context = TYPE_MAIN_VARIANT (context);
23407 dw_die_ref ctx = lookup_type_die (context);
23408 if (!ctx)
23409 return NULL;
23410 return strip_naming_typedef (context, ctx);
23412 else
23413 return lookup_decl_die (context);
23415 return comp_unit_die ();
23418 /* Returns the DIE for a context. */
23420 static inline dw_die_ref
23421 get_context_die (tree context)
23423 if (context)
23425 /* Find die that represents this context. */
23426 if (TYPE_P (context))
23428 context = TYPE_MAIN_VARIANT (context);
23429 return strip_naming_typedef (context, force_type_die (context));
23431 else
23432 return force_decl_die (context);
23434 return comp_unit_die ();
23437 /* Returns the DIE for decl. A DIE will always be returned. */
23439 static dw_die_ref
23440 force_decl_die (tree decl)
23442 dw_die_ref decl_die;
23443 unsigned saved_external_flag;
23444 tree save_fn = NULL_TREE;
23445 decl_die = lookup_decl_die (decl);
23446 if (!decl_die)
23448 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23450 decl_die = lookup_decl_die (decl);
23451 if (decl_die)
23452 return decl_die;
23454 switch (TREE_CODE (decl))
23456 case FUNCTION_DECL:
23457 /* Clear current_function_decl, so that gen_subprogram_die thinks
23458 that this is a declaration. At this point, we just want to force
23459 declaration die. */
23460 save_fn = current_function_decl;
23461 current_function_decl = NULL_TREE;
23462 gen_subprogram_die (decl, context_die);
23463 current_function_decl = save_fn;
23464 break;
23466 case VAR_DECL:
23467 /* Set external flag to force declaration die. Restore it after
23468 gen_decl_die() call. */
23469 saved_external_flag = DECL_EXTERNAL (decl);
23470 DECL_EXTERNAL (decl) = 1;
23471 gen_decl_die (decl, NULL, NULL, context_die);
23472 DECL_EXTERNAL (decl) = saved_external_flag;
23473 break;
23475 case NAMESPACE_DECL:
23476 if (dwarf_version >= 3 || !dwarf_strict)
23477 dwarf2out_decl (decl);
23478 else
23479 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
23480 decl_die = comp_unit_die ();
23481 break;
23483 case TRANSLATION_UNIT_DECL:
23484 decl_die = comp_unit_die ();
23485 break;
23487 default:
23488 gcc_unreachable ();
23491 /* We should be able to find the DIE now. */
23492 if (!decl_die)
23493 decl_die = lookup_decl_die (decl);
23494 gcc_assert (decl_die);
23497 return decl_die;
23500 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
23501 always returned. */
23503 static dw_die_ref
23504 force_type_die (tree type)
23506 dw_die_ref type_die;
23508 type_die = lookup_type_die (type);
23509 if (!type_die)
23511 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23513 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23514 false, context_die);
23515 gcc_assert (type_die);
23517 return type_die;
23520 /* Force out any required namespaces to be able to output DECL,
23521 and return the new context_die for it, if it's changed. */
23523 static dw_die_ref
23524 setup_namespace_context (tree thing, dw_die_ref context_die)
23526 tree context = (DECL_P (thing)
23527 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23528 if (context && TREE_CODE (context) == NAMESPACE_DECL)
23529 /* Force out the namespace. */
23530 context_die = force_decl_die (context);
23532 return context_die;
23535 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23536 type) within its namespace, if appropriate.
23538 For compatibility with older debuggers, namespace DIEs only contain
23539 declarations; all definitions are emitted at CU scope, with
23540 DW_AT_specification pointing to the declaration (like with class
23541 members). */
23543 static dw_die_ref
23544 declare_in_namespace (tree thing, dw_die_ref context_die)
23546 dw_die_ref ns_context;
23548 if (debug_info_level <= DINFO_LEVEL_TERSE)
23549 return context_die;
23551 /* External declarations in the local scope only need to be emitted
23552 once, not once in the namespace and once in the scope.
23554 This avoids declaring the `extern' below in the
23555 namespace DIE as well as in the innermost scope:
23557 namespace S
23559 int i=5;
23560 int foo()
23562 int i=8;
23563 extern int i;
23564 return i;
23568 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23569 return context_die;
23571 /* If this decl is from an inlined function, then don't try to emit it in its
23572 namespace, as we will get confused. It would have already been emitted
23573 when the abstract instance of the inline function was emitted anyways. */
23574 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23575 return context_die;
23577 ns_context = setup_namespace_context (thing, context_die);
23579 if (ns_context != context_die)
23581 if (is_fortran ())
23582 return ns_context;
23583 if (DECL_P (thing))
23584 gen_decl_die (thing, NULL, NULL, ns_context);
23585 else
23586 gen_type_die (thing, ns_context);
23588 return context_die;
23591 /* Generate a DIE for a namespace or namespace alias. */
23593 static void
23594 gen_namespace_die (tree decl, dw_die_ref context_die)
23596 dw_die_ref namespace_die;
23598 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23599 they are an alias of. */
23600 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23602 /* Output a real namespace or module. */
23603 context_die = setup_namespace_context (decl, comp_unit_die ());
23604 namespace_die = new_die (is_fortran ()
23605 ? DW_TAG_module : DW_TAG_namespace,
23606 context_die, decl);
23607 /* For Fortran modules defined in different CU don't add src coords. */
23608 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23610 const char *name = dwarf2_name (decl, 0);
23611 if (name)
23612 add_name_attribute (namespace_die, name);
23614 else
23615 add_name_and_src_coords_attributes (namespace_die, decl);
23616 if (DECL_EXTERNAL (decl))
23617 add_AT_flag (namespace_die, DW_AT_declaration, 1);
23618 equate_decl_number_to_die (decl, namespace_die);
23620 else
23622 /* Output a namespace alias. */
23624 /* Force out the namespace we are an alias of, if necessary. */
23625 dw_die_ref origin_die
23626 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23628 if (DECL_FILE_SCOPE_P (decl)
23629 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23630 context_die = setup_namespace_context (decl, comp_unit_die ());
23631 /* Now create the namespace alias DIE. */
23632 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23633 add_name_and_src_coords_attributes (namespace_die, decl);
23634 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23635 equate_decl_number_to_die (decl, namespace_die);
23637 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
23638 if (want_pubnames ())
23639 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23642 /* Generate Dwarf debug information for a decl described by DECL.
23643 The return value is currently only meaningful for PARM_DECLs,
23644 for all other decls it returns NULL.
23646 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23647 It can be NULL otherwise. */
23649 static dw_die_ref
23650 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23651 dw_die_ref context_die)
23653 tree decl_or_origin = decl ? decl : origin;
23654 tree class_origin = NULL, ultimate_origin;
23656 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23657 return NULL;
23659 /* Ignore pointer bounds decls. */
23660 if (DECL_P (decl_or_origin)
23661 && TREE_TYPE (decl_or_origin)
23662 && POINTER_BOUNDS_P (decl_or_origin))
23663 return NULL;
23665 switch (TREE_CODE (decl_or_origin))
23667 case ERROR_MARK:
23668 break;
23670 case CONST_DECL:
23671 if (!is_fortran () && !is_ada ())
23673 /* The individual enumerators of an enum type get output when we output
23674 the Dwarf representation of the relevant enum type itself. */
23675 break;
23678 /* Emit its type. */
23679 gen_type_die (TREE_TYPE (decl), context_die);
23681 /* And its containing namespace. */
23682 context_die = declare_in_namespace (decl, context_die);
23684 gen_const_die (decl, context_die);
23685 break;
23687 case FUNCTION_DECL:
23688 /* Don't output any DIEs to represent mere function declarations,
23689 unless they are class members or explicit block externs. */
23690 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23691 && DECL_FILE_SCOPE_P (decl_or_origin)
23692 && (current_function_decl == NULL_TREE
23693 || DECL_ARTIFICIAL (decl_or_origin)))
23694 break;
23696 #if 0
23697 /* FIXME */
23698 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23699 on local redeclarations of global functions. That seems broken. */
23700 if (current_function_decl != decl)
23701 /* This is only a declaration. */;
23702 #endif
23704 /* If we're emitting a clone, emit info for the abstract instance. */
23705 if (origin || DECL_ORIGIN (decl) != decl)
23706 dwarf2out_abstract_function (origin
23707 ? DECL_ORIGIN (origin)
23708 : DECL_ABSTRACT_ORIGIN (decl));
23710 /* If we're emitting an out-of-line copy of an inline function,
23711 emit info for the abstract instance and set up to refer to it. */
23712 else if (cgraph_function_possibly_inlined_p (decl)
23713 && ! DECL_ABSTRACT_P (decl)
23714 && ! class_or_namespace_scope_p (context_die)
23715 /* dwarf2out_abstract_function won't emit a die if this is just
23716 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
23717 that case, because that works only if we have a die. */
23718 && DECL_INITIAL (decl) != NULL_TREE)
23720 dwarf2out_abstract_function (decl);
23721 set_decl_origin_self (decl);
23724 /* Otherwise we're emitting the primary DIE for this decl. */
23725 else if (debug_info_level > DINFO_LEVEL_TERSE)
23727 /* Before we describe the FUNCTION_DECL itself, make sure that we
23728 have its containing type. */
23729 if (!origin)
23730 origin = decl_class_context (decl);
23731 if (origin != NULL_TREE)
23732 gen_type_die (origin, context_die);
23734 /* And its return type. */
23735 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23737 /* And its virtual context. */
23738 if (DECL_VINDEX (decl) != NULL_TREE)
23739 gen_type_die (DECL_CONTEXT (decl), context_die);
23741 /* Make sure we have a member DIE for decl. */
23742 if (origin != NULL_TREE)
23743 gen_type_die_for_member (origin, decl, context_die);
23745 /* And its containing namespace. */
23746 context_die = declare_in_namespace (decl, context_die);
23749 /* Now output a DIE to represent the function itself. */
23750 if (decl)
23751 gen_subprogram_die (decl, context_die);
23752 break;
23754 case TYPE_DECL:
23755 /* If we are in terse mode, don't generate any DIEs to represent any
23756 actual typedefs. */
23757 if (debug_info_level <= DINFO_LEVEL_TERSE)
23758 break;
23760 /* In the special case of a TYPE_DECL node representing the declaration
23761 of some type tag, if the given TYPE_DECL is marked as having been
23762 instantiated from some other (original) TYPE_DECL node (e.g. one which
23763 was generated within the original definition of an inline function) we
23764 used to generate a special (abbreviated) DW_TAG_structure_type,
23765 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
23766 should be actually referencing those DIEs, as variable DIEs with that
23767 type would be emitted already in the abstract origin, so it was always
23768 removed during unused type prunning. Don't add anything in this
23769 case. */
23770 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23771 break;
23773 if (is_redundant_typedef (decl))
23774 gen_type_die (TREE_TYPE (decl), context_die);
23775 else
23776 /* Output a DIE to represent the typedef itself. */
23777 gen_typedef_die (decl, context_die);
23778 break;
23780 case LABEL_DECL:
23781 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23782 gen_label_die (decl, context_die);
23783 break;
23785 case VAR_DECL:
23786 case RESULT_DECL:
23787 /* If we are in terse mode, don't generate any DIEs to represent any
23788 variable declarations or definitions. */
23789 if (debug_info_level <= DINFO_LEVEL_TERSE)
23790 break;
23792 /* Output any DIEs that are needed to specify the type of this data
23793 object. */
23794 if (decl_by_reference_p (decl_or_origin))
23795 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23796 else
23797 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23799 /* And its containing type. */
23800 class_origin = decl_class_context (decl_or_origin);
23801 if (class_origin != NULL_TREE)
23802 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23804 /* And its containing namespace. */
23805 context_die = declare_in_namespace (decl_or_origin, context_die);
23807 /* Now output the DIE to represent the data object itself. This gets
23808 complicated because of the possibility that the VAR_DECL really
23809 represents an inlined instance of a formal parameter for an inline
23810 function. */
23811 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23812 if (ultimate_origin != NULL_TREE
23813 && TREE_CODE (ultimate_origin) == PARM_DECL)
23814 gen_formal_parameter_die (decl, origin,
23815 true /* Emit name attribute. */,
23816 context_die);
23817 else
23818 gen_variable_die (decl, origin, context_die);
23819 break;
23821 case FIELD_DECL:
23822 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23823 /* Ignore the nameless fields that are used to skip bits but handle C++
23824 anonymous unions and structs. */
23825 if (DECL_NAME (decl) != NULL_TREE
23826 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23827 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23829 gen_type_die (member_declared_type (decl), context_die);
23830 gen_field_die (decl, ctx, context_die);
23832 break;
23834 case PARM_DECL:
23835 if (DECL_BY_REFERENCE (decl_or_origin))
23836 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23837 else
23838 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23839 return gen_formal_parameter_die (decl, origin,
23840 true /* Emit name attribute. */,
23841 context_die);
23843 case NAMESPACE_DECL:
23844 if (dwarf_version >= 3 || !dwarf_strict)
23845 gen_namespace_die (decl, context_die);
23846 break;
23848 case IMPORTED_DECL:
23849 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23850 DECL_CONTEXT (decl), context_die);
23851 break;
23853 case NAMELIST_DECL:
23854 gen_namelist_decl (DECL_NAME (decl), context_die,
23855 NAMELIST_DECL_ASSOCIATED_DECL (decl));
23856 break;
23858 default:
23859 /* Probably some frontend-internal decl. Assume we don't care. */
23860 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23861 break;
23864 return NULL;
23867 /* Output initial debug information for global DECL. Called at the
23868 end of the parsing process.
23870 This is the initial debug generation process. As such, the DIEs
23871 generated may be incomplete. A later debug generation pass
23872 (dwarf2out_late_global_decl) will augment the information generated
23873 in this pass (e.g., with complete location info). */
23875 static void
23876 dwarf2out_early_global_decl (tree decl)
23878 set_early_dwarf s;
23880 /* gen_decl_die() will set DECL_ABSTRACT because
23881 cgraph_function_possibly_inlined_p() returns true. This is in
23882 turn will cause DW_AT_inline attributes to be set.
23884 This happens because at early dwarf generation, there is no
23885 cgraph information, causing cgraph_function_possibly_inlined_p()
23886 to return true. Trick cgraph_function_possibly_inlined_p()
23887 while we generate dwarf early. */
23888 bool save = symtab->global_info_ready;
23889 symtab->global_info_ready = true;
23891 /* We don't handle TYPE_DECLs. If required, they'll be reached via
23892 other DECLs and they can point to template types or other things
23893 that dwarf2out can't handle when done via dwarf2out_decl. */
23894 if (TREE_CODE (decl) != TYPE_DECL
23895 && TREE_CODE (decl) != PARM_DECL)
23897 tree save_fndecl = current_function_decl;
23898 if (TREE_CODE (decl) == FUNCTION_DECL)
23900 /* No cfun means the symbol has no body, so there's nothing
23901 to emit. */
23902 if (!DECL_STRUCT_FUNCTION (decl))
23903 goto early_decl_exit;
23905 current_function_decl = decl;
23907 dwarf2out_decl (decl);
23908 if (TREE_CODE (decl) == FUNCTION_DECL)
23909 current_function_decl = save_fndecl;
23911 early_decl_exit:
23912 symtab->global_info_ready = save;
23915 /* Output debug information for global decl DECL. Called from
23916 toplev.c after compilation proper has finished. */
23918 static void
23919 dwarf2out_late_global_decl (tree decl)
23921 /* Fill-in any location information we were unable to determine
23922 on the first pass. */
23923 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
23925 dw_die_ref die = lookup_decl_die (decl);
23927 /* We have to generate early debug late for LTO. */
23928 if (! die && in_lto_p)
23930 dwarf2out_decl (decl);
23931 die = lookup_decl_die (decl);
23934 if (die)
23936 /* We get called during the early debug phase via the symtab
23937 code invoking late_global_decl for symbols that are optimized
23938 out. When the early phase is not finished, do not add
23939 locations. */
23940 if (! early_dwarf_finished)
23941 tree_add_const_value_attribute_for_decl (die, decl);
23942 else
23943 add_location_or_const_value_attribute (die, decl, false);
23948 /* Output debug information for type decl DECL. Called from toplev.c
23949 and from language front ends (to record built-in types). */
23950 static void
23951 dwarf2out_type_decl (tree decl, int local)
23953 if (!local)
23955 set_early_dwarf s;
23956 dwarf2out_decl (decl);
23960 /* Output debug information for imported module or decl DECL.
23961 NAME is non-NULL name in the lexical block if the decl has been renamed.
23962 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23963 that DECL belongs to.
23964 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
23965 static void
23966 dwarf2out_imported_module_or_decl_1 (tree decl,
23967 tree name,
23968 tree lexical_block,
23969 dw_die_ref lexical_block_die)
23971 expanded_location xloc;
23972 dw_die_ref imported_die = NULL;
23973 dw_die_ref at_import_die;
23975 if (TREE_CODE (decl) == IMPORTED_DECL)
23977 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23978 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23979 gcc_assert (decl);
23981 else
23982 xloc = expand_location (input_location);
23984 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23986 at_import_die = force_type_die (TREE_TYPE (decl));
23987 /* For namespace N { typedef void T; } using N::T; base_type_die
23988 returns NULL, but DW_TAG_imported_declaration requires
23989 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
23990 if (!at_import_die)
23992 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23993 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23994 at_import_die = lookup_type_die (TREE_TYPE (decl));
23995 gcc_assert (at_import_die);
23998 else
24000 at_import_die = lookup_decl_die (decl);
24001 if (!at_import_die)
24003 /* If we're trying to avoid duplicate debug info, we may not have
24004 emitted the member decl for this field. Emit it now. */
24005 if (TREE_CODE (decl) == FIELD_DECL)
24007 tree type = DECL_CONTEXT (decl);
24009 if (TYPE_CONTEXT (type)
24010 && TYPE_P (TYPE_CONTEXT (type))
24011 && !should_emit_struct_debug (TYPE_CONTEXT (type),
24012 DINFO_USAGE_DIR_USE))
24013 return;
24014 gen_type_die_for_member (type, decl,
24015 get_context_die (TYPE_CONTEXT (type)));
24017 if (TREE_CODE (decl) == NAMELIST_DECL)
24018 at_import_die = gen_namelist_decl (DECL_NAME (decl),
24019 get_context_die (DECL_CONTEXT (decl)),
24020 NULL_TREE);
24021 else
24022 at_import_die = force_decl_die (decl);
24026 if (TREE_CODE (decl) == NAMESPACE_DECL)
24028 if (dwarf_version >= 3 || !dwarf_strict)
24029 imported_die = new_die (DW_TAG_imported_module,
24030 lexical_block_die,
24031 lexical_block);
24032 else
24033 return;
24035 else
24036 imported_die = new_die (DW_TAG_imported_declaration,
24037 lexical_block_die,
24038 lexical_block);
24040 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
24041 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
24042 if (name)
24043 add_AT_string (imported_die, DW_AT_name,
24044 IDENTIFIER_POINTER (name));
24045 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
24048 /* Output debug information for imported module or decl DECL.
24049 NAME is non-NULL name in context if the decl has been renamed.
24050 CHILD is true if decl is one of the renamed decls as part of
24051 importing whole module. */
24053 static void
24054 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
24055 bool child)
24057 /* dw_die_ref at_import_die; */
24058 dw_die_ref scope_die;
24060 if (debug_info_level <= DINFO_LEVEL_TERSE)
24061 return;
24063 gcc_assert (decl);
24065 set_early_dwarf s;
24067 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
24068 We need decl DIE for reference and scope die. First, get DIE for the decl
24069 itself. */
24071 /* Get the scope die for decl context. Use comp_unit_die for global module
24072 or decl. If die is not found for non globals, force new die. */
24073 if (context
24074 && TYPE_P (context)
24075 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
24076 return;
24078 scope_die = get_context_die (context);
24080 if (child)
24082 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
24083 there is nothing we can do, here. */
24084 if (dwarf_version < 3 && dwarf_strict)
24085 return;
24087 gcc_assert (scope_die->die_child);
24088 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
24089 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
24090 scope_die = scope_die->die_child;
24093 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
24094 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
24097 /* Output debug information for namelists. */
24099 static dw_die_ref
24100 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
24102 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
24103 tree value;
24104 unsigned i;
24106 if (debug_info_level <= DINFO_LEVEL_TERSE)
24107 return NULL;
24109 gcc_assert (scope_die != NULL);
24110 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
24111 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
24113 /* If there are no item_decls, we have a nondefining namelist, e.g.
24114 with USE association; hence, set DW_AT_declaration. */
24115 if (item_decls == NULL_TREE)
24117 add_AT_flag (nml_die, DW_AT_declaration, 1);
24118 return nml_die;
24121 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
24123 nml_item_ref_die = lookup_decl_die (value);
24124 if (!nml_item_ref_die)
24125 nml_item_ref_die = force_decl_die (value);
24127 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
24128 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
24130 return nml_die;
24134 /* Write the debugging output for DECL and return the DIE. */
24136 static void
24137 dwarf2out_decl (tree decl)
24139 dw_die_ref context_die = comp_unit_die ();
24141 switch (TREE_CODE (decl))
24143 case ERROR_MARK:
24144 return;
24146 case FUNCTION_DECL:
24147 /* What we would really like to do here is to filter out all mere
24148 file-scope declarations of file-scope functions which are never
24149 referenced later within this translation unit (and keep all of ones
24150 that *are* referenced later on) but we aren't clairvoyant, so we have
24151 no idea which functions will be referenced in the future (i.e. later
24152 on within the current translation unit). So here we just ignore all
24153 file-scope function declarations which are not also definitions. If
24154 and when the debugger needs to know something about these functions,
24155 it will have to hunt around and find the DWARF information associated
24156 with the definition of the function.
24158 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
24159 nodes represent definitions and which ones represent mere
24160 declarations. We have to check DECL_INITIAL instead. That's because
24161 the C front-end supports some weird semantics for "extern inline"
24162 function definitions. These can get inlined within the current
24163 translation unit (and thus, we need to generate Dwarf info for their
24164 abstract instances so that the Dwarf info for the concrete inlined
24165 instances can have something to refer to) but the compiler never
24166 generates any out-of-lines instances of such things (despite the fact
24167 that they *are* definitions).
24169 The important point is that the C front-end marks these "extern
24170 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
24171 them anyway. Note that the C++ front-end also plays some similar games
24172 for inline function definitions appearing within include files which
24173 also contain `#pragma interface' pragmas.
24175 If we are called from dwarf2out_abstract_function output a DIE
24176 anyway. We can end up here this way with early inlining and LTO
24177 where the inlined function is output in a different LTRANS unit
24178 or not at all. */
24179 if (DECL_INITIAL (decl) == NULL_TREE
24180 && ! DECL_ABSTRACT_P (decl))
24181 return;
24183 /* If we're a nested function, initially use a parent of NULL; if we're
24184 a plain function, this will be fixed up in decls_for_scope. If
24185 we're a method, it will be ignored, since we already have a DIE. */
24186 if (decl_function_context (decl)
24187 /* But if we're in terse mode, we don't care about scope. */
24188 && debug_info_level > DINFO_LEVEL_TERSE)
24189 context_die = NULL;
24190 break;
24192 case VAR_DECL:
24193 /* For local statics lookup proper context die. */
24194 if (local_function_static (decl))
24195 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24197 /* If we are in terse mode, don't generate any DIEs to represent any
24198 variable declarations or definitions. */
24199 if (debug_info_level <= DINFO_LEVEL_TERSE)
24200 return;
24201 break;
24203 case CONST_DECL:
24204 if (debug_info_level <= DINFO_LEVEL_TERSE)
24205 return;
24206 if (!is_fortran () && !is_ada ())
24207 return;
24208 if (TREE_STATIC (decl) && decl_function_context (decl))
24209 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24210 break;
24212 case NAMESPACE_DECL:
24213 case IMPORTED_DECL:
24214 if (debug_info_level <= DINFO_LEVEL_TERSE)
24215 return;
24216 if (lookup_decl_die (decl) != NULL)
24217 return;
24218 break;
24220 case TYPE_DECL:
24221 /* Don't emit stubs for types unless they are needed by other DIEs. */
24222 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
24223 return;
24225 /* Don't bother trying to generate any DIEs to represent any of the
24226 normal built-in types for the language we are compiling. */
24227 if (DECL_IS_BUILTIN (decl))
24228 return;
24230 /* If we are in terse mode, don't generate any DIEs for types. */
24231 if (debug_info_level <= DINFO_LEVEL_TERSE)
24232 return;
24234 /* If we're a function-scope tag, initially use a parent of NULL;
24235 this will be fixed up in decls_for_scope. */
24236 if (decl_function_context (decl))
24237 context_die = NULL;
24239 break;
24241 case NAMELIST_DECL:
24242 break;
24244 default:
24245 return;
24248 gen_decl_die (decl, NULL, NULL, context_die);
24250 if (flag_checking)
24252 dw_die_ref die = lookup_decl_die (decl);
24253 if (die)
24254 check_die (die);
24258 /* Write the debugging output for DECL. */
24260 static void
24261 dwarf2out_function_decl (tree decl)
24263 dwarf2out_decl (decl);
24264 call_arg_locations = NULL;
24265 call_arg_loc_last = NULL;
24266 call_site_count = -1;
24267 tail_call_site_count = -1;
24268 decl_loc_table->empty ();
24269 cached_dw_loc_list_table->empty ();
24272 /* Output a marker (i.e. a label) for the beginning of the generated code for
24273 a lexical block. */
24275 static void
24276 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
24277 unsigned int blocknum)
24279 switch_to_section (current_function_section ());
24280 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
24283 /* Output a marker (i.e. a label) for the end of the generated code for a
24284 lexical block. */
24286 static void
24287 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
24289 switch_to_section (current_function_section ());
24290 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
24293 /* Returns nonzero if it is appropriate not to emit any debugging
24294 information for BLOCK, because it doesn't contain any instructions.
24296 Don't allow this for blocks with nested functions or local classes
24297 as we would end up with orphans, and in the presence of scheduling
24298 we may end up calling them anyway. */
24300 static bool
24301 dwarf2out_ignore_block (const_tree block)
24303 tree decl;
24304 unsigned int i;
24306 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24307 if (TREE_CODE (decl) == FUNCTION_DECL
24308 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24309 return 0;
24310 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24312 decl = BLOCK_NONLOCALIZED_VAR (block, i);
24313 if (TREE_CODE (decl) == FUNCTION_DECL
24314 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24315 return 0;
24318 return 1;
24321 /* Hash table routines for file_hash. */
24323 bool
24324 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24326 return filename_cmp (p1->filename, p2) == 0;
24329 hashval_t
24330 dwarf_file_hasher::hash (dwarf_file_data *p)
24332 return htab_hash_string (p->filename);
24335 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24336 dwarf2out.c) and return its "index". The index of each (known) filename is
24337 just a unique number which is associated with only that one filename. We
24338 need such numbers for the sake of generating labels (in the .debug_sfnames
24339 section) and references to those files numbers (in the .debug_srcinfo
24340 and .debug_macinfo sections). If the filename given as an argument is not
24341 found in our current list, add it to the list and assign it the next
24342 available unique index number. */
24344 static struct dwarf_file_data *
24345 lookup_filename (const char *file_name)
24347 struct dwarf_file_data * created;
24349 if (!file_name)
24350 return NULL;
24352 dwarf_file_data **slot
24353 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24354 INSERT);
24355 if (*slot)
24356 return *slot;
24358 created = ggc_alloc<dwarf_file_data> ();
24359 created->filename = file_name;
24360 created->emitted_number = 0;
24361 *slot = created;
24362 return created;
24365 /* If the assembler will construct the file table, then translate the compiler
24366 internal file table number into the assembler file table number, and emit
24367 a .file directive if we haven't already emitted one yet. The file table
24368 numbers are different because we prune debug info for unused variables and
24369 types, which may include filenames. */
24371 static int
24372 maybe_emit_file (struct dwarf_file_data * fd)
24374 if (! fd->emitted_number)
24376 if (last_emitted_file)
24377 fd->emitted_number = last_emitted_file->emitted_number + 1;
24378 else
24379 fd->emitted_number = 1;
24380 last_emitted_file = fd;
24382 if (DWARF2_ASM_LINE_DEBUG_INFO)
24384 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24385 output_quoted_string (asm_out_file,
24386 remap_debug_filename (fd->filename));
24387 fputc ('\n', asm_out_file);
24391 return fd->emitted_number;
24394 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24395 That generation should happen after function debug info has been
24396 generated. The value of the attribute is the constant value of ARG. */
24398 static void
24399 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24401 die_arg_entry entry;
24403 if (!die || !arg)
24404 return;
24406 gcc_assert (early_dwarf);
24408 if (!tmpl_value_parm_die_table)
24409 vec_alloc (tmpl_value_parm_die_table, 32);
24411 entry.die = die;
24412 entry.arg = arg;
24413 vec_safe_push (tmpl_value_parm_die_table, entry);
24416 /* Return TRUE if T is an instance of generic type, FALSE
24417 otherwise. */
24419 static bool
24420 generic_type_p (tree t)
24422 if (t == NULL_TREE || !TYPE_P (t))
24423 return false;
24424 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24427 /* Schedule the generation of the generic parameter dies for the
24428 instance of generic type T. The proper generation itself is later
24429 done by gen_scheduled_generic_parms_dies. */
24431 static void
24432 schedule_generic_params_dies_gen (tree t)
24434 if (!generic_type_p (t))
24435 return;
24437 gcc_assert (early_dwarf);
24439 if (!generic_type_instances)
24440 vec_alloc (generic_type_instances, 256);
24442 vec_safe_push (generic_type_instances, t);
24445 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24446 by append_entry_to_tmpl_value_parm_die_table. This function must
24447 be called after function DIEs have been generated. */
24449 static void
24450 gen_remaining_tmpl_value_param_die_attribute (void)
24452 if (tmpl_value_parm_die_table)
24454 unsigned i, j;
24455 die_arg_entry *e;
24457 /* We do this in two phases - first get the cases we can
24458 handle during early-finish, preserving those we cannot
24459 (containing symbolic constants where we don't yet know
24460 whether we are going to output the referenced symbols).
24461 For those we try again at late-finish. */
24462 j = 0;
24463 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24465 if (!tree_add_const_value_attribute (e->die, e->arg))
24467 dw_loc_descr_ref loc = NULL;
24468 if (! early_dwarf
24469 && (dwarf_version >= 5 || !dwarf_strict))
24470 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
24471 if (loc)
24472 add_AT_loc (e->die, DW_AT_location, loc);
24473 else
24474 (*tmpl_value_parm_die_table)[j++] = *e;
24477 tmpl_value_parm_die_table->truncate (j);
24481 /* Generate generic parameters DIEs for instances of generic types
24482 that have been previously scheduled by
24483 schedule_generic_params_dies_gen. This function must be called
24484 after all the types of the CU have been laid out. */
24486 static void
24487 gen_scheduled_generic_parms_dies (void)
24489 unsigned i;
24490 tree t;
24492 if (!generic_type_instances)
24493 return;
24495 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24496 if (COMPLETE_TYPE_P (t))
24497 gen_generic_params_dies (t);
24499 generic_type_instances = NULL;
24503 /* Replace DW_AT_name for the decl with name. */
24505 static void
24506 dwarf2out_set_name (tree decl, tree name)
24508 dw_die_ref die;
24509 dw_attr_node *attr;
24510 const char *dname;
24512 die = TYPE_SYMTAB_DIE (decl);
24513 if (!die)
24514 return;
24516 dname = dwarf2_name (name, 0);
24517 if (!dname)
24518 return;
24520 attr = get_AT (die, DW_AT_name);
24521 if (attr)
24523 struct indirect_string_node *node;
24525 node = find_AT_string (dname);
24526 /* replace the string. */
24527 attr->dw_attr_val.v.val_str = node;
24530 else
24531 add_name_attribute (die, dname);
24534 /* True if before or during processing of the first function being emitted. */
24535 static bool in_first_function_p = true;
24536 /* True if loc_note during dwarf2out_var_location call might still be
24537 before first real instruction at address equal to .Ltext0. */
24538 static bool maybe_at_text_label_p = true;
24539 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
24540 static unsigned int first_loclabel_num_not_at_text_label;
24542 /* Called by the final INSN scan whenever we see a var location. We
24543 use it to drop labels in the right places, and throw the location in
24544 our lookup table. */
24546 static void
24547 dwarf2out_var_location (rtx_insn *loc_note)
24549 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24550 struct var_loc_node *newloc;
24551 rtx_insn *next_real, *next_note;
24552 rtx_insn *call_insn = NULL;
24553 static const char *last_label;
24554 static const char *last_postcall_label;
24555 static bool last_in_cold_section_p;
24556 static rtx_insn *expected_next_loc_note;
24557 tree decl;
24558 bool var_loc_p;
24560 if (!NOTE_P (loc_note))
24562 if (CALL_P (loc_note))
24564 call_site_count++;
24565 if (SIBLING_CALL_P (loc_note))
24566 tail_call_site_count++;
24567 if (optimize == 0 && !flag_var_tracking)
24569 /* When the var-tracking pass is not running, there is no note
24570 for indirect calls whose target is compile-time known. In this
24571 case, process such calls specifically so that we generate call
24572 sites for them anyway. */
24573 rtx x = PATTERN (loc_note);
24574 if (GET_CODE (x) == PARALLEL)
24575 x = XVECEXP (x, 0, 0);
24576 if (GET_CODE (x) == SET)
24577 x = SET_SRC (x);
24578 if (GET_CODE (x) == CALL)
24579 x = XEXP (x, 0);
24580 if (!MEM_P (x)
24581 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24582 || !SYMBOL_REF_DECL (XEXP (x, 0))
24583 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24584 != FUNCTION_DECL))
24586 call_insn = loc_note;
24587 loc_note = NULL;
24588 var_loc_p = false;
24590 next_real = next_real_insn (call_insn);
24591 next_note = NULL;
24592 cached_next_real_insn = NULL;
24593 goto create_label;
24597 return;
24600 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24601 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24602 return;
24604 /* Optimize processing a large consecutive sequence of location
24605 notes so we don't spend too much time in next_real_insn. If the
24606 next insn is another location note, remember the next_real_insn
24607 calculation for next time. */
24608 next_real = cached_next_real_insn;
24609 if (next_real)
24611 if (expected_next_loc_note != loc_note)
24612 next_real = NULL;
24615 next_note = NEXT_INSN (loc_note);
24616 if (! next_note
24617 || next_note->deleted ()
24618 || ! NOTE_P (next_note)
24619 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24620 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24621 next_note = NULL;
24623 if (! next_real)
24624 next_real = next_real_insn (loc_note);
24626 if (next_note)
24628 expected_next_loc_note = next_note;
24629 cached_next_real_insn = next_real;
24631 else
24632 cached_next_real_insn = NULL;
24634 /* If there are no instructions which would be affected by this note,
24635 don't do anything. */
24636 if (var_loc_p
24637 && next_real == NULL_RTX
24638 && !NOTE_DURING_CALL_P (loc_note))
24639 return;
24641 create_label:
24643 if (next_real == NULL_RTX)
24644 next_real = get_last_insn ();
24646 /* If there were any real insns between note we processed last time
24647 and this note (or if it is the first note), clear
24648 last_{,postcall_}label so that they are not reused this time. */
24649 if (last_var_location_insn == NULL_RTX
24650 || last_var_location_insn != next_real
24651 || last_in_cold_section_p != in_cold_section_p)
24653 last_label = NULL;
24654 last_postcall_label = NULL;
24657 if (var_loc_p)
24659 decl = NOTE_VAR_LOCATION_DECL (loc_note);
24660 newloc = add_var_loc_to_decl (decl, loc_note,
24661 NOTE_DURING_CALL_P (loc_note)
24662 ? last_postcall_label : last_label);
24663 if (newloc == NULL)
24664 return;
24666 else
24668 decl = NULL_TREE;
24669 newloc = NULL;
24672 /* If there were no real insns between note we processed last time
24673 and this note, use the label we emitted last time. Otherwise
24674 create a new label and emit it. */
24675 if (last_label == NULL)
24677 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24678 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24679 loclabel_num++;
24680 last_label = ggc_strdup (loclabel);
24681 /* See if loclabel might be equal to .Ltext0. If yes,
24682 bump first_loclabel_num_not_at_text_label. */
24683 if (!have_multiple_function_sections
24684 && in_first_function_p
24685 && maybe_at_text_label_p)
24687 static rtx_insn *last_start;
24688 rtx_insn *insn;
24689 for (insn = loc_note; insn; insn = previous_insn (insn))
24690 if (insn == last_start)
24691 break;
24692 else if (!NONDEBUG_INSN_P (insn))
24693 continue;
24694 else
24696 rtx body = PATTERN (insn);
24697 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24698 continue;
24699 /* Inline asm could occupy zero bytes. */
24700 else if (GET_CODE (body) == ASM_INPUT
24701 || asm_noperands (body) >= 0)
24702 continue;
24703 #ifdef HAVE_attr_length
24704 else if (get_attr_min_length (insn) == 0)
24705 continue;
24706 #endif
24707 else
24709 /* Assume insn has non-zero length. */
24710 maybe_at_text_label_p = false;
24711 break;
24714 if (maybe_at_text_label_p)
24716 last_start = loc_note;
24717 first_loclabel_num_not_at_text_label = loclabel_num;
24722 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24723 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24725 if (!var_loc_p)
24727 struct call_arg_loc_node *ca_loc
24728 = ggc_cleared_alloc<call_arg_loc_node> ();
24729 rtx_insn *prev
24730 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24732 ca_loc->call_arg_loc_note = loc_note;
24733 ca_loc->next = NULL;
24734 ca_loc->label = last_label;
24735 gcc_assert (prev
24736 && (CALL_P (prev)
24737 || (NONJUMP_INSN_P (prev)
24738 && GET_CODE (PATTERN (prev)) == SEQUENCE
24739 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24740 if (!CALL_P (prev))
24741 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24742 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24744 /* Look for a SYMBOL_REF in the "prev" instruction. */
24745 rtx x = get_call_rtx_from (PATTERN (prev));
24746 if (x)
24748 /* Try to get the call symbol, if any. */
24749 if (MEM_P (XEXP (x, 0)))
24750 x = XEXP (x, 0);
24751 /* First, look for a memory access to a symbol_ref. */
24752 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24753 && SYMBOL_REF_DECL (XEXP (x, 0))
24754 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24755 ca_loc->symbol_ref = XEXP (x, 0);
24756 /* Otherwise, look at a compile-time known user-level function
24757 declaration. */
24758 else if (MEM_P (x)
24759 && MEM_EXPR (x)
24760 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24761 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24764 ca_loc->block = insn_scope (prev);
24765 if (call_arg_locations)
24766 call_arg_loc_last->next = ca_loc;
24767 else
24768 call_arg_locations = ca_loc;
24769 call_arg_loc_last = ca_loc;
24771 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24772 newloc->label = last_label;
24773 else
24775 if (!last_postcall_label)
24777 sprintf (loclabel, "%s-1", last_label);
24778 last_postcall_label = ggc_strdup (loclabel);
24780 newloc->label = last_postcall_label;
24783 last_var_location_insn = next_real;
24784 last_in_cold_section_p = in_cold_section_p;
24787 /* Called from finalize_size_functions for size functions so that their body
24788 can be encoded in the debug info to describe the layout of variable-length
24789 structures. */
24791 static void
24792 dwarf2out_size_function (tree decl)
24794 function_to_dwarf_procedure (decl);
24797 /* Note in one location list that text section has changed. */
24800 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24802 var_loc_list *list = *slot;
24803 if (list->first)
24804 list->last_before_switch
24805 = list->last->next ? list->last->next : list->last;
24806 return 1;
24809 /* Note in all location lists that text section has changed. */
24811 static void
24812 var_location_switch_text_section (void)
24814 if (decl_loc_table == NULL)
24815 return;
24817 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24820 /* Create a new line number table. */
24822 static dw_line_info_table *
24823 new_line_info_table (void)
24825 dw_line_info_table *table;
24827 table = ggc_cleared_alloc<dw_line_info_table> ();
24828 table->file_num = 1;
24829 table->line_num = 1;
24830 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24832 return table;
24835 /* Lookup the "current" table into which we emit line info, so
24836 that we don't have to do it for every source line. */
24838 static void
24839 set_cur_line_info_table (section *sec)
24841 dw_line_info_table *table;
24843 if (sec == text_section)
24844 table = text_section_line_info;
24845 else if (sec == cold_text_section)
24847 table = cold_text_section_line_info;
24848 if (!table)
24850 cold_text_section_line_info = table = new_line_info_table ();
24851 table->end_label = cold_end_label;
24854 else
24856 const char *end_label;
24858 if (flag_reorder_blocks_and_partition)
24860 if (in_cold_section_p)
24861 end_label = crtl->subsections.cold_section_end_label;
24862 else
24863 end_label = crtl->subsections.hot_section_end_label;
24865 else
24867 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24868 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24869 current_function_funcdef_no);
24870 end_label = ggc_strdup (label);
24873 table = new_line_info_table ();
24874 table->end_label = end_label;
24876 vec_safe_push (separate_line_info, table);
24879 if (DWARF2_ASM_LINE_DEBUG_INFO)
24880 table->is_stmt = (cur_line_info_table
24881 ? cur_line_info_table->is_stmt
24882 : DWARF_LINE_DEFAULT_IS_STMT_START);
24883 cur_line_info_table = table;
24887 /* We need to reset the locations at the beginning of each
24888 function. We can't do this in the end_function hook, because the
24889 declarations that use the locations won't have been output when
24890 that hook is called. Also compute have_multiple_function_sections here. */
24892 static void
24893 dwarf2out_begin_function (tree fun)
24895 section *sec = function_section (fun);
24897 if (sec != text_section)
24898 have_multiple_function_sections = true;
24900 if (flag_reorder_blocks_and_partition && !cold_text_section)
24902 gcc_assert (current_function_decl == fun);
24903 cold_text_section = unlikely_text_section ();
24904 switch_to_section (cold_text_section);
24905 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24906 switch_to_section (sec);
24909 dwarf2out_note_section_used ();
24910 call_site_count = 0;
24911 tail_call_site_count = 0;
24913 set_cur_line_info_table (sec);
24916 /* Helper function of dwarf2out_end_function, called only after emitting
24917 the very first function into assembly. Check if some .debug_loc range
24918 might end with a .LVL* label that could be equal to .Ltext0.
24919 In that case we must force using absolute addresses in .debug_loc ranges,
24920 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24921 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24922 list terminator.
24923 Set have_multiple_function_sections to true in that case and
24924 terminate htab traversal. */
24927 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24929 var_loc_list *entry = *slot;
24930 struct var_loc_node *node;
24932 node = entry->first;
24933 if (node && node->next && node->next->label)
24935 unsigned int i;
24936 const char *label = node->next->label;
24937 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24939 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24941 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24942 if (strcmp (label, loclabel) == 0)
24944 have_multiple_function_sections = true;
24945 return 0;
24949 return 1;
24952 /* Hook called after emitting a function into assembly.
24953 This does something only for the very first function emitted. */
24955 static void
24956 dwarf2out_end_function (unsigned int)
24958 if (in_first_function_p
24959 && !have_multiple_function_sections
24960 && first_loclabel_num_not_at_text_label
24961 && decl_loc_table)
24962 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24963 in_first_function_p = false;
24964 maybe_at_text_label_p = false;
24967 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
24968 front-ends register a translation unit even before dwarf2out_init is
24969 called. */
24970 static tree main_translation_unit = NULL_TREE;
24972 /* Hook called by front-ends after they built their main translation unit.
24973 Associate comp_unit_die to UNIT. */
24975 static void
24976 dwarf2out_register_main_translation_unit (tree unit)
24978 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24979 && main_translation_unit == NULL_TREE);
24980 main_translation_unit = unit;
24981 /* If dwarf2out_init has not been called yet, it will perform the association
24982 itself looking at main_translation_unit. */
24983 if (decl_die_table != NULL)
24984 equate_decl_number_to_die (unit, comp_unit_die ());
24987 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
24989 static void
24990 push_dw_line_info_entry (dw_line_info_table *table,
24991 enum dw_line_info_opcode opcode, unsigned int val)
24993 dw_line_info_entry e;
24994 e.opcode = opcode;
24995 e.val = val;
24996 vec_safe_push (table->entries, e);
24999 /* Output a label to mark the beginning of a source code line entry
25000 and record information relating to this source line, in
25001 'line_info_table' for later output of the .debug_line section. */
25002 /* ??? The discriminator parameter ought to be unsigned. */
25004 static void
25005 dwarf2out_source_line (unsigned int line, const char *filename,
25006 int discriminator, bool is_stmt)
25008 unsigned int file_num;
25009 dw_line_info_table *table;
25011 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
25012 return;
25014 /* The discriminator column was added in dwarf4. Simplify the below
25015 by simply removing it if we're not supposed to output it. */
25016 if (dwarf_version < 4 && dwarf_strict)
25017 discriminator = 0;
25019 table = cur_line_info_table;
25020 file_num = maybe_emit_file (lookup_filename (filename));
25022 /* ??? TODO: Elide duplicate line number entries. Traditionally,
25023 the debugger has used the second (possibly duplicate) line number
25024 at the beginning of the function to mark the end of the prologue.
25025 We could eliminate any other duplicates within the function. For
25026 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
25027 that second line number entry. */
25028 /* Recall that this end-of-prologue indication is *not* the same thing
25029 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
25030 to which the hook corresponds, follows the last insn that was
25031 emitted by gen_prologue. What we need is to precede the first insn
25032 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
25033 insn that corresponds to something the user wrote. These may be
25034 very different locations once scheduling is enabled. */
25036 if (0 && file_num == table->file_num
25037 && line == table->line_num
25038 && discriminator == table->discrim_num
25039 && is_stmt == table->is_stmt)
25040 return;
25042 switch_to_section (current_function_section ());
25044 /* If requested, emit something human-readable. */
25045 if (flag_debug_asm)
25046 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
25048 if (DWARF2_ASM_LINE_DEBUG_INFO)
25050 /* Emit the .loc directive understood by GNU as. */
25051 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
25052 file_num, line, is_stmt, discriminator */
25053 fputs ("\t.loc ", asm_out_file);
25054 fprint_ul (asm_out_file, file_num);
25055 putc (' ', asm_out_file);
25056 fprint_ul (asm_out_file, line);
25057 putc (' ', asm_out_file);
25058 putc ('0', asm_out_file);
25060 if (is_stmt != table->is_stmt)
25062 fputs (" is_stmt ", asm_out_file);
25063 putc (is_stmt ? '1' : '0', asm_out_file);
25065 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
25067 gcc_assert (discriminator > 0);
25068 fputs (" discriminator ", asm_out_file);
25069 fprint_ul (asm_out_file, (unsigned long) discriminator);
25071 putc ('\n', asm_out_file);
25073 else
25075 unsigned int label_num = ++line_info_label_num;
25077 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
25079 push_dw_line_info_entry (table, LI_set_address, label_num);
25080 if (file_num != table->file_num)
25081 push_dw_line_info_entry (table, LI_set_file, file_num);
25082 if (discriminator != table->discrim_num)
25083 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
25084 if (is_stmt != table->is_stmt)
25085 push_dw_line_info_entry (table, LI_negate_stmt, 0);
25086 push_dw_line_info_entry (table, LI_set_line, line);
25089 table->file_num = file_num;
25090 table->line_num = line;
25091 table->discrim_num = discriminator;
25092 table->is_stmt = is_stmt;
25093 table->in_use = true;
25096 /* Record the beginning of a new source file. */
25098 static void
25099 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
25101 if (flag_eliminate_dwarf2_dups)
25103 /* Record the beginning of the file for break_out_includes. */
25104 dw_die_ref bincl_die;
25106 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
25107 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
25110 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25112 macinfo_entry e;
25113 e.code = DW_MACINFO_start_file;
25114 e.lineno = lineno;
25115 e.info = ggc_strdup (filename);
25116 vec_safe_push (macinfo_table, e);
25120 /* Record the end of a source file. */
25122 static void
25123 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
25125 if (flag_eliminate_dwarf2_dups)
25126 /* Record the end of the file for break_out_includes. */
25127 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
25129 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25131 macinfo_entry e;
25132 e.code = DW_MACINFO_end_file;
25133 e.lineno = lineno;
25134 e.info = NULL;
25135 vec_safe_push (macinfo_table, e);
25139 /* Called from debug_define in toplev.c. The `buffer' parameter contains
25140 the tail part of the directive line, i.e. the part which is past the
25141 initial whitespace, #, whitespace, directive-name, whitespace part. */
25143 static void
25144 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
25145 const char *buffer ATTRIBUTE_UNUSED)
25147 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25149 macinfo_entry e;
25150 /* Insert a dummy first entry to be able to optimize the whole
25151 predefined macro block using DW_MACRO_GNU_transparent_include. */
25152 if (macinfo_table->is_empty () && lineno <= 1)
25154 e.code = 0;
25155 e.lineno = 0;
25156 e.info = NULL;
25157 vec_safe_push (macinfo_table, e);
25159 e.code = DW_MACINFO_define;
25160 e.lineno = lineno;
25161 e.info = ggc_strdup (buffer);
25162 vec_safe_push (macinfo_table, e);
25166 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
25167 the tail part of the directive line, i.e. the part which is past the
25168 initial whitespace, #, whitespace, directive-name, whitespace part. */
25170 static void
25171 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
25172 const char *buffer ATTRIBUTE_UNUSED)
25174 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25176 macinfo_entry e;
25177 /* Insert a dummy first entry to be able to optimize the whole
25178 predefined macro block using DW_MACRO_GNU_transparent_include. */
25179 if (macinfo_table->is_empty () && lineno <= 1)
25181 e.code = 0;
25182 e.lineno = 0;
25183 e.info = NULL;
25184 vec_safe_push (macinfo_table, e);
25186 e.code = DW_MACINFO_undef;
25187 e.lineno = lineno;
25188 e.info = ggc_strdup (buffer);
25189 vec_safe_push (macinfo_table, e);
25193 /* Helpers to manipulate hash table of CUs. */
25195 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
25197 static inline hashval_t hash (const macinfo_entry *);
25198 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
25201 inline hashval_t
25202 macinfo_entry_hasher::hash (const macinfo_entry *entry)
25204 return htab_hash_string (entry->info);
25207 inline bool
25208 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
25209 const macinfo_entry *entry2)
25211 return !strcmp (entry1->info, entry2->info);
25214 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
25216 /* Output a single .debug_macinfo entry. */
25218 static void
25219 output_macinfo_op (macinfo_entry *ref)
25221 int file_num;
25222 size_t len;
25223 struct indirect_string_node *node;
25224 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25225 struct dwarf_file_data *fd;
25227 switch (ref->code)
25229 case DW_MACINFO_start_file:
25230 fd = lookup_filename (ref->info);
25231 file_num = maybe_emit_file (fd);
25232 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
25233 dw2_asm_output_data_uleb128 (ref->lineno,
25234 "Included from line number %lu",
25235 (unsigned long) ref->lineno);
25236 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
25237 break;
25238 case DW_MACINFO_end_file:
25239 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
25240 break;
25241 case DW_MACINFO_define:
25242 case DW_MACINFO_undef:
25243 len = strlen (ref->info) + 1;
25244 if (!dwarf_strict
25245 && len > DWARF_OFFSET_SIZE
25246 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25247 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25249 ref->code = ref->code == DW_MACINFO_define
25250 ? DW_MACRO_GNU_define_indirect
25251 : DW_MACRO_GNU_undef_indirect;
25252 output_macinfo_op (ref);
25253 return;
25255 dw2_asm_output_data (1, ref->code,
25256 ref->code == DW_MACINFO_define
25257 ? "Define macro" : "Undefine macro");
25258 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25259 (unsigned long) ref->lineno);
25260 dw2_asm_output_nstring (ref->info, -1, "The macro");
25261 break;
25262 case DW_MACRO_GNU_define_indirect:
25263 case DW_MACRO_GNU_undef_indirect:
25264 node = find_AT_string (ref->info);
25265 gcc_assert (node
25266 && ((node->form == DW_FORM_strp)
25267 || (node->form == DW_FORM_GNU_str_index)));
25268 dw2_asm_output_data (1, ref->code,
25269 ref->code == DW_MACRO_GNU_define_indirect
25270 ? "Define macro indirect"
25271 : "Undefine macro indirect");
25272 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25273 (unsigned long) ref->lineno);
25274 if (node->form == DW_FORM_strp)
25275 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
25276 debug_str_section, "The macro: \"%s\"",
25277 ref->info);
25278 else
25279 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
25280 ref->info);
25281 break;
25282 case DW_MACRO_GNU_transparent_include:
25283 dw2_asm_output_data (1, ref->code, "Transparent include");
25284 ASM_GENERATE_INTERNAL_LABEL (label,
25285 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
25286 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
25287 break;
25288 default:
25289 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
25290 ASM_COMMENT_START, (unsigned long) ref->code);
25291 break;
25295 /* Attempt to make a sequence of define/undef macinfo ops shareable with
25296 other compilation unit .debug_macinfo sections. IDX is the first
25297 index of a define/undef, return the number of ops that should be
25298 emitted in a comdat .debug_macinfo section and emit
25299 a DW_MACRO_GNU_transparent_include entry referencing it.
25300 If the define/undef entry should be emitted normally, return 0. */
25302 static unsigned
25303 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25304 macinfo_hash_type **macinfo_htab)
25306 macinfo_entry *first, *second, *cur, *inc;
25307 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25308 unsigned char checksum[16];
25309 struct md5_ctx ctx;
25310 char *grp_name, *tail;
25311 const char *base;
25312 unsigned int i, count, encoded_filename_len, linebuf_len;
25313 macinfo_entry **slot;
25315 first = &(*macinfo_table)[idx];
25316 second = &(*macinfo_table)[idx + 1];
25318 /* Optimize only if there are at least two consecutive define/undef ops,
25319 and either all of them are before first DW_MACINFO_start_file
25320 with lineno {0,1} (i.e. predefined macro block), or all of them are
25321 in some included header file. */
25322 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25323 return 0;
25324 if (vec_safe_is_empty (files))
25326 if (first->lineno > 1 || second->lineno > 1)
25327 return 0;
25329 else if (first->lineno == 0)
25330 return 0;
25332 /* Find the last define/undef entry that can be grouped together
25333 with first and at the same time compute md5 checksum of their
25334 codes, linenumbers and strings. */
25335 md5_init_ctx (&ctx);
25336 for (i = idx; macinfo_table->iterate (i, &cur); i++)
25337 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25338 break;
25339 else if (vec_safe_is_empty (files) && cur->lineno > 1)
25340 break;
25341 else
25343 unsigned char code = cur->code;
25344 md5_process_bytes (&code, 1, &ctx);
25345 checksum_uleb128 (cur->lineno, &ctx);
25346 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25348 md5_finish_ctx (&ctx, checksum);
25349 count = i - idx;
25351 /* From the containing include filename (if any) pick up just
25352 usable characters from its basename. */
25353 if (vec_safe_is_empty (files))
25354 base = "";
25355 else
25356 base = lbasename (files->last ().info);
25357 for (encoded_filename_len = 0, i = 0; base[i]; i++)
25358 if (ISIDNUM (base[i]) || base[i] == '.')
25359 encoded_filename_len++;
25360 /* Count . at the end. */
25361 if (encoded_filename_len)
25362 encoded_filename_len++;
25364 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25365 linebuf_len = strlen (linebuf);
25367 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
25368 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25369 + 16 * 2 + 1);
25370 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25371 tail = grp_name + 4;
25372 if (encoded_filename_len)
25374 for (i = 0; base[i]; i++)
25375 if (ISIDNUM (base[i]) || base[i] == '.')
25376 *tail++ = base[i];
25377 *tail++ = '.';
25379 memcpy (tail, linebuf, linebuf_len);
25380 tail += linebuf_len;
25381 *tail++ = '.';
25382 for (i = 0; i < 16; i++)
25383 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25385 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25386 in the empty vector entry before the first define/undef. */
25387 inc = &(*macinfo_table)[idx - 1];
25388 inc->code = DW_MACRO_GNU_transparent_include;
25389 inc->lineno = 0;
25390 inc->info = ggc_strdup (grp_name);
25391 if (!*macinfo_htab)
25392 *macinfo_htab = new macinfo_hash_type (10);
25393 /* Avoid emitting duplicates. */
25394 slot = (*macinfo_htab)->find_slot (inc, INSERT);
25395 if (*slot != NULL)
25397 inc->code = 0;
25398 inc->info = NULL;
25399 /* If such an entry has been used before, just emit
25400 a DW_MACRO_GNU_transparent_include op. */
25401 inc = *slot;
25402 output_macinfo_op (inc);
25403 /* And clear all macinfo_entry in the range to avoid emitting them
25404 in the second pass. */
25405 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25407 cur->code = 0;
25408 cur->info = NULL;
25411 else
25413 *slot = inc;
25414 inc->lineno = (*macinfo_htab)->elements ();
25415 output_macinfo_op (inc);
25417 return count;
25420 /* Save any strings needed by the macinfo table in the debug str
25421 table. All strings must be collected into the table by the time
25422 index_string is called. */
25424 static void
25425 save_macinfo_strings (void)
25427 unsigned len;
25428 unsigned i;
25429 macinfo_entry *ref;
25431 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25433 switch (ref->code)
25435 /* Match the logic in output_macinfo_op to decide on
25436 indirect strings. */
25437 case DW_MACINFO_define:
25438 case DW_MACINFO_undef:
25439 len = strlen (ref->info) + 1;
25440 if (!dwarf_strict
25441 && len > DWARF_OFFSET_SIZE
25442 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25443 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25444 set_indirect_string (find_AT_string (ref->info));
25445 break;
25446 case DW_MACRO_GNU_define_indirect:
25447 case DW_MACRO_GNU_undef_indirect:
25448 set_indirect_string (find_AT_string (ref->info));
25449 break;
25450 default:
25451 break;
25456 /* Output macinfo section(s). */
25458 static void
25459 output_macinfo (void)
25461 unsigned i;
25462 unsigned long length = vec_safe_length (macinfo_table);
25463 macinfo_entry *ref;
25464 vec<macinfo_entry, va_gc> *files = NULL;
25465 macinfo_hash_type *macinfo_htab = NULL;
25467 if (! length)
25468 return;
25470 /* output_macinfo* uses these interchangeably. */
25471 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25472 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25473 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25474 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25476 /* For .debug_macro emit the section header. */
25477 if (!dwarf_strict)
25479 dw2_asm_output_data (2, 4, "DWARF macro version number");
25480 if (DWARF_OFFSET_SIZE == 8)
25481 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25482 else
25483 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25484 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25485 (!dwarf_split_debug_info ? debug_line_section_label
25486 : debug_skeleton_line_section_label),
25487 debug_line_section, NULL);
25490 /* In the first loop, it emits the primary .debug_macinfo section
25491 and after each emitted op the macinfo_entry is cleared.
25492 If a longer range of define/undef ops can be optimized using
25493 DW_MACRO_GNU_transparent_include, the
25494 DW_MACRO_GNU_transparent_include op is emitted and kept in
25495 the vector before the first define/undef in the range and the
25496 whole range of define/undef ops is not emitted and kept. */
25497 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25499 switch (ref->code)
25501 case DW_MACINFO_start_file:
25502 vec_safe_push (files, *ref);
25503 break;
25504 case DW_MACINFO_end_file:
25505 if (!vec_safe_is_empty (files))
25506 files->pop ();
25507 break;
25508 case DW_MACINFO_define:
25509 case DW_MACINFO_undef:
25510 if (!dwarf_strict
25511 && HAVE_COMDAT_GROUP
25512 && vec_safe_length (files) != 1
25513 && i > 0
25514 && i + 1 < length
25515 && (*macinfo_table)[i - 1].code == 0)
25517 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25518 if (count)
25520 i += count - 1;
25521 continue;
25524 break;
25525 case 0:
25526 /* A dummy entry may be inserted at the beginning to be able
25527 to optimize the whole block of predefined macros. */
25528 if (i == 0)
25529 continue;
25530 default:
25531 break;
25533 output_macinfo_op (ref);
25534 ref->info = NULL;
25535 ref->code = 0;
25538 if (!macinfo_htab)
25539 return;
25541 delete macinfo_htab;
25542 macinfo_htab = NULL;
25544 /* If any DW_MACRO_GNU_transparent_include were used, on those
25545 DW_MACRO_GNU_transparent_include entries terminate the
25546 current chain and switch to a new comdat .debug_macinfo
25547 section and emit the define/undef entries within it. */
25548 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25549 switch (ref->code)
25551 case 0:
25552 continue;
25553 case DW_MACRO_GNU_transparent_include:
25555 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25556 tree comdat_key = get_identifier (ref->info);
25557 /* Terminate the previous .debug_macinfo section. */
25558 dw2_asm_output_data (1, 0, "End compilation unit");
25559 targetm.asm_out.named_section (debug_macinfo_section_name,
25560 SECTION_DEBUG
25561 | SECTION_LINKONCE,
25562 comdat_key);
25563 ASM_GENERATE_INTERNAL_LABEL (label,
25564 DEBUG_MACRO_SECTION_LABEL,
25565 ref->lineno);
25566 ASM_OUTPUT_LABEL (asm_out_file, label);
25567 ref->code = 0;
25568 ref->info = NULL;
25569 dw2_asm_output_data (2, 4, "DWARF macro version number");
25570 if (DWARF_OFFSET_SIZE == 8)
25571 dw2_asm_output_data (1, 1, "Flags: 64-bit");
25572 else
25573 dw2_asm_output_data (1, 0, "Flags: 32-bit");
25575 break;
25576 case DW_MACINFO_define:
25577 case DW_MACINFO_undef:
25578 output_macinfo_op (ref);
25579 ref->code = 0;
25580 ref->info = NULL;
25581 break;
25582 default:
25583 gcc_unreachable ();
25587 /* Initialize the various sections and labels for dwarf output. */
25589 static void
25590 init_sections_and_labels (void)
25592 if (!dwarf_split_debug_info)
25594 debug_info_section = get_section (DEBUG_INFO_SECTION,
25595 SECTION_DEBUG, NULL);
25596 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25597 SECTION_DEBUG, NULL);
25598 debug_loc_section = get_section (DEBUG_LOC_SECTION,
25599 SECTION_DEBUG, NULL);
25600 debug_macinfo_section_name
25601 = dwarf_strict ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
25602 debug_macinfo_section = get_section (debug_macinfo_section_name,
25603 SECTION_DEBUG, NULL);
25605 else
25607 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25608 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25609 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25610 SECTION_DEBUG | SECTION_EXCLUDE,
25611 NULL);
25612 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25613 SECTION_DEBUG, NULL);
25614 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25615 SECTION_DEBUG, NULL);
25616 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25617 SECTION_DEBUG, NULL);
25618 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25619 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25621 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25622 the main .o, but the skeleton_line goes into the split off dwo. */
25623 debug_skeleton_line_section
25624 = get_section (DEBUG_DWO_LINE_SECTION,
25625 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25626 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25627 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25628 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
25629 SECTION_DEBUG | SECTION_EXCLUDE,
25630 NULL);
25631 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25632 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25633 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25634 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25635 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25636 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25637 debug_macinfo_section_name
25638 = dwarf_strict ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
25639 debug_macinfo_section = get_section (debug_macinfo_section_name,
25640 SECTION_DEBUG | SECTION_EXCLUDE,
25641 NULL);
25643 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25644 SECTION_DEBUG, NULL);
25645 debug_line_section = get_section (DEBUG_LINE_SECTION,
25646 SECTION_DEBUG, NULL);
25647 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25648 SECTION_DEBUG, NULL);
25649 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25650 SECTION_DEBUG, NULL);
25651 debug_str_section = get_section (DEBUG_STR_SECTION,
25652 DEBUG_STR_SECTION_FLAGS, NULL);
25653 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25654 SECTION_DEBUG, NULL);
25655 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25656 SECTION_DEBUG, NULL);
25658 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25659 DEBUG_ABBREV_SECTION_LABEL, 0);
25660 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25661 DEBUG_INFO_SECTION_LABEL, 0);
25662 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25663 DEBUG_LINE_SECTION_LABEL, 0);
25664 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25665 DEBUG_RANGES_SECTION_LABEL, 0);
25666 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25667 DEBUG_ADDR_SECTION_LABEL, 0);
25668 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25669 dwarf_strict
25670 ? DEBUG_MACINFO_SECTION_LABEL
25671 : DEBUG_MACRO_SECTION_LABEL, 0);
25672 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25675 /* Set up for Dwarf output at the start of compilation. */
25677 static void
25678 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25680 /* This option is currently broken, see (PR53118 and PR46102). */
25681 if (flag_eliminate_dwarf2_dups
25682 && strstr (lang_hooks.name, "C++"))
25684 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25685 flag_eliminate_dwarf2_dups = 0;
25688 /* Allocate the file_table. */
25689 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25691 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25692 /* Allocate the decl_die_table. */
25693 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25695 /* Allocate the decl_loc_table. */
25696 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25698 /* Allocate the cached_dw_loc_list_table. */
25699 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25701 /* Allocate the initial hunk of the decl_scope_table. */
25702 vec_alloc (decl_scope_table, 256);
25704 /* Allocate the initial hunk of the abbrev_die_table. */
25705 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25706 (ABBREV_DIE_TABLE_INCREMENT);
25707 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25708 /* Zero-th entry is allocated, but unused. */
25709 abbrev_die_table_in_use = 1;
25711 /* Allocate the dwarf_proc_stack_usage_map. */
25712 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25714 /* Allocate the pubtypes and pubnames vectors. */
25715 vec_alloc (pubname_table, 32);
25716 vec_alloc (pubtype_table, 32);
25718 vec_alloc (incomplete_types, 64);
25720 vec_alloc (used_rtx_array, 32);
25722 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25723 vec_alloc (macinfo_table, 64);
25724 #endif
25726 /* If front-ends already registered a main translation unit but we were not
25727 ready to perform the association, do this now. */
25728 if (main_translation_unit != NULL_TREE)
25729 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25732 /* Called before compile () starts outputtting functions, variables
25733 and toplevel asms into assembly. */
25735 static void
25736 dwarf2out_assembly_start (void)
25738 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25739 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25740 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25741 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25742 COLD_TEXT_SECTION_LABEL, 0);
25743 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25745 switch_to_section (text_section);
25746 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25747 #endif
25749 /* Make sure the line number table for .text always exists. */
25750 text_section_line_info = new_line_info_table ();
25751 text_section_line_info->end_label = text_end_label;
25753 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25754 cur_line_info_table = text_section_line_info;
25755 #endif
25757 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25758 && dwarf2out_do_cfi_asm ()
25759 && (!(flag_unwind_tables || flag_exceptions)
25760 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25761 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25764 /* A helper function for dwarf2out_finish called through
25765 htab_traverse. Assign a string its index. All strings must be
25766 collected into the table by the time index_string is called,
25767 because the indexing code relies on htab_traverse to traverse nodes
25768 in the same order for each run. */
25771 index_string (indirect_string_node **h, unsigned int *index)
25773 indirect_string_node *node = *h;
25775 find_string_form (node);
25776 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25778 gcc_assert (node->index == NO_INDEX_ASSIGNED);
25779 node->index = *index;
25780 *index += 1;
25782 return 1;
25785 /* A helper function for output_indirect_strings called through
25786 htab_traverse. Output the offset to a string and update the
25787 current offset. */
25790 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25792 indirect_string_node *node = *h;
25794 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25796 /* Assert that this node has been assigned an index. */
25797 gcc_assert (node->index != NO_INDEX_ASSIGNED
25798 && node->index != NOT_INDEXED);
25799 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25800 "indexed string 0x%x: %s", node->index, node->str);
25801 *offset += strlen (node->str) + 1;
25803 return 1;
25806 /* A helper function for dwarf2out_finish called through
25807 htab_traverse. Output the indexed string. */
25810 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25812 struct indirect_string_node *node = *h;
25814 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25816 /* Assert that the strings are output in the same order as their
25817 indexes were assigned. */
25818 gcc_assert (*cur_idx == node->index);
25819 assemble_string (node->str, strlen (node->str) + 1);
25820 *cur_idx += 1;
25822 return 1;
25825 /* A helper function for dwarf2out_finish called through
25826 htab_traverse. Emit one queued .debug_str string. */
25829 output_indirect_string (indirect_string_node **h, void *)
25831 struct indirect_string_node *node = *h;
25833 node->form = find_string_form (node);
25834 if (node->form == DW_FORM_strp && node->refcount > 0)
25836 ASM_OUTPUT_LABEL (asm_out_file, node->label);
25837 assemble_string (node->str, strlen (node->str) + 1);
25840 return 1;
25843 /* Output the indexed string table. */
25845 static void
25846 output_indirect_strings (void)
25848 switch_to_section (debug_str_section);
25849 if (!dwarf_split_debug_info)
25850 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25851 else
25853 unsigned int offset = 0;
25854 unsigned int cur_idx = 0;
25856 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25858 switch_to_section (debug_str_offsets_section);
25859 debug_str_hash->traverse_noresize
25860 <unsigned int *, output_index_string_offset> (&offset);
25861 switch_to_section (debug_str_dwo_section);
25862 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25863 (&cur_idx);
25867 /* Callback for htab_traverse to assign an index to an entry in the
25868 table, and to write that entry to the .debug_addr section. */
25871 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25873 addr_table_entry *entry = *slot;
25875 if (entry->refcount == 0)
25877 gcc_assert (entry->index == NO_INDEX_ASSIGNED
25878 || entry->index == NOT_INDEXED);
25879 return 1;
25882 gcc_assert (entry->index == *cur_index);
25883 (*cur_index)++;
25885 switch (entry->kind)
25887 case ate_kind_rtx:
25888 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25889 "0x%x", entry->index);
25890 break;
25891 case ate_kind_rtx_dtprel:
25892 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25893 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25894 DWARF2_ADDR_SIZE,
25895 entry->addr.rtl);
25896 fputc ('\n', asm_out_file);
25897 break;
25898 case ate_kind_label:
25899 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25900 "0x%x", entry->index);
25901 break;
25902 default:
25903 gcc_unreachable ();
25905 return 1;
25908 /* Produce the .debug_addr section. */
25910 static void
25911 output_addr_table (void)
25913 unsigned int index = 0;
25914 if (addr_index_table == NULL || addr_index_table->size () == 0)
25915 return;
25917 switch_to_section (debug_addr_section);
25918 addr_index_table
25919 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25922 #if ENABLE_ASSERT_CHECKING
25923 /* Verify that all marks are clear. */
25925 static void
25926 verify_marks_clear (dw_die_ref die)
25928 dw_die_ref c;
25930 gcc_assert (! die->die_mark);
25931 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25933 #endif /* ENABLE_ASSERT_CHECKING */
25935 /* Clear the marks for a die and its children.
25936 Be cool if the mark isn't set. */
25938 static void
25939 prune_unmark_dies (dw_die_ref die)
25941 dw_die_ref c;
25943 if (die->die_mark)
25944 die->die_mark = 0;
25945 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25948 /* Given LOC that is referenced by a DIE we're marking as used, find all
25949 referenced DWARF procedures it references and mark them as used. */
25951 static void
25952 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25954 for (; loc != NULL; loc = loc->dw_loc_next)
25955 switch (loc->dw_loc_opc)
25957 case DW_OP_GNU_implicit_pointer:
25958 case DW_OP_GNU_convert:
25959 case DW_OP_GNU_reinterpret:
25960 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25961 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25962 break;
25963 case DW_OP_call2:
25964 case DW_OP_call4:
25965 case DW_OP_call_ref:
25966 case DW_OP_GNU_const_type:
25967 case DW_OP_GNU_parameter_ref:
25968 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
25969 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25970 break;
25971 case DW_OP_GNU_regval_type:
25972 case DW_OP_GNU_deref_type:
25973 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
25974 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
25975 break;
25976 case DW_OP_GNU_entry_value:
25977 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
25978 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
25979 break;
25980 default:
25981 break;
25985 /* Given DIE that we're marking as used, find any other dies
25986 it references as attributes and mark them as used. */
25988 static void
25989 prune_unused_types_walk_attribs (dw_die_ref die)
25991 dw_attr_node *a;
25992 unsigned ix;
25994 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25996 switch (AT_class (a))
25998 /* Make sure DWARF procedures referenced by location descriptions will
25999 get emitted. */
26000 case dw_val_class_loc:
26001 prune_unused_types_walk_loc_descr (AT_loc (a));
26002 break;
26003 case dw_val_class_loc_list:
26004 for (dw_loc_list_ref list = AT_loc_list (a);
26005 list != NULL;
26006 list = list->dw_loc_next)
26007 prune_unused_types_walk_loc_descr (list->expr);
26008 break;
26010 case dw_val_class_die_ref:
26011 /* A reference to another DIE.
26012 Make sure that it will get emitted.
26013 If it was broken out into a comdat group, don't follow it. */
26014 if (! AT_ref (a)->comdat_type_p
26015 || a->dw_attr == DW_AT_specification)
26016 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
26017 break;
26019 case dw_val_class_str:
26020 /* Set the string's refcount to 0 so that prune_unused_types_mark
26021 accounts properly for it. */
26022 a->dw_attr_val.v.val_str->refcount = 0;
26023 break;
26025 default:
26026 break;
26031 /* Mark the generic parameters and arguments children DIEs of DIE. */
26033 static void
26034 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
26036 dw_die_ref c;
26038 if (die == NULL || die->die_child == NULL)
26039 return;
26040 c = die->die_child;
26043 if (is_template_parameter (c))
26044 prune_unused_types_mark (c, 1);
26045 c = c->die_sib;
26046 } while (c && c != die->die_child);
26049 /* Mark DIE as being used. If DOKIDS is true, then walk down
26050 to DIE's children. */
26052 static void
26053 prune_unused_types_mark (dw_die_ref die, int dokids)
26055 dw_die_ref c;
26057 if (die->die_mark == 0)
26059 /* We haven't done this node yet. Mark it as used. */
26060 die->die_mark = 1;
26061 /* If this is the DIE of a generic type instantiation,
26062 mark the children DIEs that describe its generic parms and
26063 args. */
26064 prune_unused_types_mark_generic_parms_dies (die);
26066 /* We also have to mark its parents as used.
26067 (But we don't want to mark our parent's kids due to this,
26068 unless it is a class.) */
26069 if (die->die_parent)
26070 prune_unused_types_mark (die->die_parent,
26071 class_scope_p (die->die_parent));
26073 /* Mark any referenced nodes. */
26074 prune_unused_types_walk_attribs (die);
26076 /* If this node is a specification,
26077 also mark the definition, if it exists. */
26078 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
26079 prune_unused_types_mark (die->die_definition, 1);
26082 if (dokids && die->die_mark != 2)
26084 /* We need to walk the children, but haven't done so yet.
26085 Remember that we've walked the kids. */
26086 die->die_mark = 2;
26088 /* If this is an array type, we need to make sure our
26089 kids get marked, even if they're types. If we're
26090 breaking out types into comdat sections, do this
26091 for all type definitions. */
26092 if (die->die_tag == DW_TAG_array_type
26093 || (use_debug_types
26094 && is_type_die (die) && ! is_declaration_die (die)))
26095 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
26096 else
26097 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26101 /* For local classes, look if any static member functions were emitted
26102 and if so, mark them. */
26104 static void
26105 prune_unused_types_walk_local_classes (dw_die_ref die)
26107 dw_die_ref c;
26109 if (die->die_mark == 2)
26110 return;
26112 switch (die->die_tag)
26114 case DW_TAG_structure_type:
26115 case DW_TAG_union_type:
26116 case DW_TAG_class_type:
26117 break;
26119 case DW_TAG_subprogram:
26120 if (!get_AT_flag (die, DW_AT_declaration)
26121 || die->die_definition != NULL)
26122 prune_unused_types_mark (die, 1);
26123 return;
26125 default:
26126 return;
26129 /* Mark children. */
26130 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
26133 /* Walk the tree DIE and mark types that we actually use. */
26135 static void
26136 prune_unused_types_walk (dw_die_ref die)
26138 dw_die_ref c;
26140 /* Don't do anything if this node is already marked and
26141 children have been marked as well. */
26142 if (die->die_mark == 2)
26143 return;
26145 switch (die->die_tag)
26147 case DW_TAG_structure_type:
26148 case DW_TAG_union_type:
26149 case DW_TAG_class_type:
26150 if (die->die_perennial_p)
26151 break;
26153 for (c = die->die_parent; c; c = c->die_parent)
26154 if (c->die_tag == DW_TAG_subprogram)
26155 break;
26157 /* Finding used static member functions inside of classes
26158 is needed just for local classes, because for other classes
26159 static member function DIEs with DW_AT_specification
26160 are emitted outside of the DW_TAG_*_type. If we ever change
26161 it, we'd need to call this even for non-local classes. */
26162 if (c)
26163 prune_unused_types_walk_local_classes (die);
26165 /* It's a type node --- don't mark it. */
26166 return;
26168 case DW_TAG_const_type:
26169 case DW_TAG_packed_type:
26170 case DW_TAG_pointer_type:
26171 case DW_TAG_reference_type:
26172 case DW_TAG_rvalue_reference_type:
26173 case DW_TAG_volatile_type:
26174 case DW_TAG_typedef:
26175 case DW_TAG_array_type:
26176 case DW_TAG_interface_type:
26177 case DW_TAG_friend:
26178 case DW_TAG_enumeration_type:
26179 case DW_TAG_subroutine_type:
26180 case DW_TAG_string_type:
26181 case DW_TAG_set_type:
26182 case DW_TAG_subrange_type:
26183 case DW_TAG_ptr_to_member_type:
26184 case DW_TAG_file_type:
26185 /* Type nodes are useful only when other DIEs reference them --- don't
26186 mark them. */
26187 /* FALLTHROUGH */
26189 case DW_TAG_dwarf_procedure:
26190 /* Likewise for DWARF procedures. */
26192 if (die->die_perennial_p)
26193 break;
26195 return;
26197 default:
26198 /* Mark everything else. */
26199 break;
26202 if (die->die_mark == 0)
26204 die->die_mark = 1;
26206 /* Now, mark any dies referenced from here. */
26207 prune_unused_types_walk_attribs (die);
26210 die->die_mark = 2;
26212 /* Mark children. */
26213 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26216 /* Increment the string counts on strings referred to from DIE's
26217 attributes. */
26219 static void
26220 prune_unused_types_update_strings (dw_die_ref die)
26222 dw_attr_node *a;
26223 unsigned ix;
26225 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26226 if (AT_class (a) == dw_val_class_str)
26228 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
26229 s->refcount++;
26230 /* Avoid unnecessarily putting strings that are used less than
26231 twice in the hash table. */
26232 if (s->refcount
26233 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
26235 indirect_string_node **slot
26236 = debug_str_hash->find_slot_with_hash (s->str,
26237 htab_hash_string (s->str),
26238 INSERT);
26239 gcc_assert (*slot == NULL);
26240 *slot = s;
26245 /* Mark DIE and its children as removed. */
26247 static void
26248 mark_removed (dw_die_ref die)
26250 dw_die_ref c;
26251 die->removed = true;
26252 FOR_EACH_CHILD (die, c, mark_removed (c));
26255 /* Remove from the tree DIE any dies that aren't marked. */
26257 static void
26258 prune_unused_types_prune (dw_die_ref die)
26260 dw_die_ref c;
26262 gcc_assert (die->die_mark);
26263 prune_unused_types_update_strings (die);
26265 if (! die->die_child)
26266 return;
26268 c = die->die_child;
26269 do {
26270 dw_die_ref prev = c, next;
26271 for (c = c->die_sib; ! c->die_mark; c = next)
26272 if (c == die->die_child)
26274 /* No marked children between 'prev' and the end of the list. */
26275 if (prev == c)
26276 /* No marked children at all. */
26277 die->die_child = NULL;
26278 else
26280 prev->die_sib = c->die_sib;
26281 die->die_child = prev;
26283 c->die_sib = NULL;
26284 mark_removed (c);
26285 return;
26287 else
26289 next = c->die_sib;
26290 c->die_sib = NULL;
26291 mark_removed (c);
26294 if (c != prev->die_sib)
26295 prev->die_sib = c;
26296 prune_unused_types_prune (c);
26297 } while (c != die->die_child);
26300 /* Remove dies representing declarations that we never use. */
26302 static void
26303 prune_unused_types (void)
26305 unsigned int i;
26306 limbo_die_node *node;
26307 comdat_type_node *ctnode;
26308 pubname_entry *pub;
26309 dw_die_ref base_type;
26311 #if ENABLE_ASSERT_CHECKING
26312 /* All the marks should already be clear. */
26313 verify_marks_clear (comp_unit_die ());
26314 for (node = limbo_die_list; node; node = node->next)
26315 verify_marks_clear (node->die);
26316 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26317 verify_marks_clear (ctnode->root_die);
26318 #endif /* ENABLE_ASSERT_CHECKING */
26320 /* Mark types that are used in global variables. */
26321 premark_types_used_by_global_vars ();
26323 /* Set the mark on nodes that are actually used. */
26324 prune_unused_types_walk (comp_unit_die ());
26325 for (node = limbo_die_list; node; node = node->next)
26326 prune_unused_types_walk (node->die);
26327 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26329 prune_unused_types_walk (ctnode->root_die);
26330 prune_unused_types_mark (ctnode->type_die, 1);
26333 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
26334 are unusual in that they are pubnames that are the children of pubtypes.
26335 They should only be marked via their parent DW_TAG_enumeration_type die,
26336 not as roots in themselves. */
26337 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26338 if (pub->die->die_tag != DW_TAG_enumerator)
26339 prune_unused_types_mark (pub->die, 1);
26340 for (i = 0; base_types.iterate (i, &base_type); i++)
26341 prune_unused_types_mark (base_type, 1);
26343 if (debug_str_hash)
26344 debug_str_hash->empty ();
26345 if (skeleton_debug_str_hash)
26346 skeleton_debug_str_hash->empty ();
26347 prune_unused_types_prune (comp_unit_die ());
26348 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26350 node = *pnode;
26351 if (!node->die->die_mark)
26352 *pnode = node->next;
26353 else
26355 prune_unused_types_prune (node->die);
26356 pnode = &node->next;
26359 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26360 prune_unused_types_prune (ctnode->root_die);
26362 /* Leave the marks clear. */
26363 prune_unmark_dies (comp_unit_die ());
26364 for (node = limbo_die_list; node; node = node->next)
26365 prune_unmark_dies (node->die);
26366 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26367 prune_unmark_dies (ctnode->root_die);
26370 /* Set the parameter to true if there are any relative pathnames in
26371 the file table. */
26373 file_table_relative_p (dwarf_file_data **slot, bool *p)
26375 struct dwarf_file_data *d = *slot;
26376 if (!IS_ABSOLUTE_PATH (d->filename))
26378 *p = true;
26379 return 0;
26381 return 1;
26384 /* Helpers to manipulate hash table of comdat type units. */
26386 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26388 static inline hashval_t hash (const comdat_type_node *);
26389 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26392 inline hashval_t
26393 comdat_type_hasher::hash (const comdat_type_node *type_node)
26395 hashval_t h;
26396 memcpy (&h, type_node->signature, sizeof (h));
26397 return h;
26400 inline bool
26401 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26402 const comdat_type_node *type_node_2)
26404 return (! memcmp (type_node_1->signature, type_node_2->signature,
26405 DWARF_TYPE_SIGNATURE_SIZE));
26408 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26409 to the location it would have been added, should we know its
26410 DECL_ASSEMBLER_NAME when we added other attributes. This will
26411 probably improve compactness of debug info, removing equivalent
26412 abbrevs, and hide any differences caused by deferring the
26413 computation of the assembler name, triggered by e.g. PCH. */
26415 static inline void
26416 move_linkage_attr (dw_die_ref die)
26418 unsigned ix = vec_safe_length (die->die_attr);
26419 dw_attr_node linkage = (*die->die_attr)[ix - 1];
26421 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26422 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26424 while (--ix > 0)
26426 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26428 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26429 break;
26432 if (ix != vec_safe_length (die->die_attr) - 1)
26434 die->die_attr->pop ();
26435 die->die_attr->quick_insert (ix, linkage);
26439 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26440 referenced from typed stack ops and count how often they are used. */
26442 static void
26443 mark_base_types (dw_loc_descr_ref loc)
26445 dw_die_ref base_type = NULL;
26447 for (; loc; loc = loc->dw_loc_next)
26449 switch (loc->dw_loc_opc)
26451 case DW_OP_GNU_regval_type:
26452 case DW_OP_GNU_deref_type:
26453 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26454 break;
26455 case DW_OP_GNU_convert:
26456 case DW_OP_GNU_reinterpret:
26457 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26458 continue;
26459 /* FALLTHRU */
26460 case DW_OP_GNU_const_type:
26461 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26462 break;
26463 case DW_OP_GNU_entry_value:
26464 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26465 continue;
26466 default:
26467 continue;
26469 gcc_assert (base_type->die_parent == comp_unit_die ());
26470 if (base_type->die_mark)
26471 base_type->die_mark++;
26472 else
26474 base_types.safe_push (base_type);
26475 base_type->die_mark = 1;
26480 /* Comparison function for sorting marked base types. */
26482 static int
26483 base_type_cmp (const void *x, const void *y)
26485 dw_die_ref dx = *(const dw_die_ref *) x;
26486 dw_die_ref dy = *(const dw_die_ref *) y;
26487 unsigned int byte_size1, byte_size2;
26488 unsigned int encoding1, encoding2;
26489 if (dx->die_mark > dy->die_mark)
26490 return -1;
26491 if (dx->die_mark < dy->die_mark)
26492 return 1;
26493 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26494 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26495 if (byte_size1 < byte_size2)
26496 return 1;
26497 if (byte_size1 > byte_size2)
26498 return -1;
26499 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26500 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26501 if (encoding1 < encoding2)
26502 return 1;
26503 if (encoding1 > encoding2)
26504 return -1;
26505 return 0;
26508 /* Move base types marked by mark_base_types as early as possible
26509 in the CU, sorted by decreasing usage count both to make the
26510 uleb128 references as small as possible and to make sure they
26511 will have die_offset already computed by calc_die_sizes when
26512 sizes of typed stack loc ops is computed. */
26514 static void
26515 move_marked_base_types (void)
26517 unsigned int i;
26518 dw_die_ref base_type, die, c;
26520 if (base_types.is_empty ())
26521 return;
26523 /* Sort by decreasing usage count, they will be added again in that
26524 order later on. */
26525 base_types.qsort (base_type_cmp);
26526 die = comp_unit_die ();
26527 c = die->die_child;
26530 dw_die_ref prev = c;
26531 c = c->die_sib;
26532 while (c->die_mark)
26534 remove_child_with_prev (c, prev);
26535 /* As base types got marked, there must be at least
26536 one node other than DW_TAG_base_type. */
26537 gcc_assert (die->die_child != NULL);
26538 c = prev->die_sib;
26541 while (c != die->die_child);
26542 gcc_assert (die->die_child);
26543 c = die->die_child;
26544 for (i = 0; base_types.iterate (i, &base_type); i++)
26546 base_type->die_mark = 0;
26547 base_type->die_sib = c->die_sib;
26548 c->die_sib = base_type;
26549 c = base_type;
26553 /* Helper function for resolve_addr, attempt to resolve
26554 one CONST_STRING, return true if successful. Similarly verify that
26555 SYMBOL_REFs refer to variables emitted in the current CU. */
26557 static bool
26558 resolve_one_addr (rtx *addr)
26560 rtx rtl = *addr;
26562 if (GET_CODE (rtl) == CONST_STRING)
26564 size_t len = strlen (XSTR (rtl, 0)) + 1;
26565 tree t = build_string (len, XSTR (rtl, 0));
26566 tree tlen = size_int (len - 1);
26567 TREE_TYPE (t)
26568 = build_array_type (char_type_node, build_index_type (tlen));
26569 rtl = lookup_constant_def (t);
26570 if (!rtl || !MEM_P (rtl))
26571 return false;
26572 rtl = XEXP (rtl, 0);
26573 if (GET_CODE (rtl) == SYMBOL_REF
26574 && SYMBOL_REF_DECL (rtl)
26575 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26576 return false;
26577 vec_safe_push (used_rtx_array, rtl);
26578 *addr = rtl;
26579 return true;
26582 if (GET_CODE (rtl) == SYMBOL_REF
26583 && SYMBOL_REF_DECL (rtl))
26585 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26587 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26588 return false;
26590 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26591 return false;
26594 if (GET_CODE (rtl) == CONST)
26596 subrtx_ptr_iterator::array_type array;
26597 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26598 if (!resolve_one_addr (*iter))
26599 return false;
26602 return true;
26605 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26606 if possible, and create DW_TAG_dwarf_procedure that can be referenced
26607 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
26609 static rtx
26610 string_cst_pool_decl (tree t)
26612 rtx rtl = output_constant_def (t, 1);
26613 unsigned char *array;
26614 dw_loc_descr_ref l;
26615 tree decl;
26616 size_t len;
26617 dw_die_ref ref;
26619 if (!rtl || !MEM_P (rtl))
26620 return NULL_RTX;
26621 rtl = XEXP (rtl, 0);
26622 if (GET_CODE (rtl) != SYMBOL_REF
26623 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26624 return NULL_RTX;
26626 decl = SYMBOL_REF_DECL (rtl);
26627 if (!lookup_decl_die (decl))
26629 len = TREE_STRING_LENGTH (t);
26630 vec_safe_push (used_rtx_array, rtl);
26631 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26632 array = ggc_vec_alloc<unsigned char> (len);
26633 memcpy (array, TREE_STRING_POINTER (t), len);
26634 l = new_loc_descr (DW_OP_implicit_value, len, 0);
26635 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26636 l->dw_loc_oprnd2.v.val_vec.length = len;
26637 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26638 l->dw_loc_oprnd2.v.val_vec.array = array;
26639 add_AT_loc (ref, DW_AT_location, l);
26640 equate_decl_number_to_die (decl, ref);
26642 return rtl;
26645 /* Helper function of resolve_addr_in_expr. LOC is
26646 a DW_OP_addr followed by DW_OP_stack_value, either at the start
26647 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26648 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
26649 with DW_OP_GNU_implicit_pointer if possible
26650 and return true, if unsuccessful, return false. */
26652 static bool
26653 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26655 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26656 HOST_WIDE_INT offset = 0;
26657 dw_die_ref ref = NULL;
26658 tree decl;
26660 if (GET_CODE (rtl) == CONST
26661 && GET_CODE (XEXP (rtl, 0)) == PLUS
26662 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26664 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26665 rtl = XEXP (XEXP (rtl, 0), 0);
26667 if (GET_CODE (rtl) == CONST_STRING)
26669 size_t len = strlen (XSTR (rtl, 0)) + 1;
26670 tree t = build_string (len, XSTR (rtl, 0));
26671 tree tlen = size_int (len - 1);
26673 TREE_TYPE (t)
26674 = build_array_type (char_type_node, build_index_type (tlen));
26675 rtl = string_cst_pool_decl (t);
26676 if (!rtl)
26677 return false;
26679 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26681 decl = SYMBOL_REF_DECL (rtl);
26682 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
26684 ref = lookup_decl_die (decl);
26685 if (ref && (get_AT (ref, DW_AT_location)
26686 || get_AT (ref, DW_AT_const_value)))
26688 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26689 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26690 loc->dw_loc_oprnd1.val_entry = NULL;
26691 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26692 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26693 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26694 loc->dw_loc_oprnd2.v.val_int = offset;
26695 return true;
26699 return false;
26702 /* Helper function for resolve_addr, handle one location
26703 expression, return false if at least one CONST_STRING or SYMBOL_REF in
26704 the location list couldn't be resolved. */
26706 static bool
26707 resolve_addr_in_expr (dw_loc_descr_ref loc)
26709 dw_loc_descr_ref keep = NULL;
26710 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26711 switch (loc->dw_loc_opc)
26713 case DW_OP_addr:
26714 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26716 if ((prev == NULL
26717 || prev->dw_loc_opc == DW_OP_piece
26718 || prev->dw_loc_opc == DW_OP_bit_piece)
26719 && loc->dw_loc_next
26720 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26721 && !dwarf_strict
26722 && optimize_one_addr_into_implicit_ptr (loc))
26723 break;
26724 return false;
26726 break;
26727 case DW_OP_GNU_addr_index:
26728 case DW_OP_GNU_const_index:
26729 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26730 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26732 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26733 if (!resolve_one_addr (&rtl))
26734 return false;
26735 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26736 loc->dw_loc_oprnd1.val_entry =
26737 add_addr_table_entry (rtl, ate_kind_rtx);
26739 break;
26740 case DW_OP_const4u:
26741 case DW_OP_const8u:
26742 if (loc->dtprel
26743 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26744 return false;
26745 break;
26746 case DW_OP_plus_uconst:
26747 if (size_of_loc_descr (loc)
26748 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26750 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26752 dw_loc_descr_ref repl
26753 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26754 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26755 add_loc_descr (&repl, loc->dw_loc_next);
26756 *loc = *repl;
26758 break;
26759 case DW_OP_implicit_value:
26760 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26761 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26762 return false;
26763 break;
26764 case DW_OP_GNU_implicit_pointer:
26765 case DW_OP_GNU_parameter_ref:
26766 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26768 dw_die_ref ref
26769 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26770 if (ref == NULL)
26771 return false;
26772 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26773 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26774 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26776 break;
26777 case DW_OP_GNU_const_type:
26778 case DW_OP_GNU_regval_type:
26779 case DW_OP_GNU_deref_type:
26780 case DW_OP_GNU_convert:
26781 case DW_OP_GNU_reinterpret:
26782 while (loc->dw_loc_next
26783 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26785 dw_die_ref base1, base2;
26786 unsigned enc1, enc2, size1, size2;
26787 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26788 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26789 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26790 else if (loc->dw_loc_oprnd1.val_class
26791 == dw_val_class_unsigned_const)
26792 break;
26793 else
26794 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26795 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26796 == dw_val_class_unsigned_const)
26797 break;
26798 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26799 gcc_assert (base1->die_tag == DW_TAG_base_type
26800 && base2->die_tag == DW_TAG_base_type);
26801 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26802 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26803 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26804 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26805 if (size1 == size2
26806 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26807 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26808 && loc != keep)
26809 || enc1 == enc2))
26811 /* Optimize away next DW_OP_GNU_convert after
26812 adjusting LOC's base type die reference. */
26813 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26814 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26815 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26816 else
26817 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26818 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26819 continue;
26821 /* Don't change integer DW_OP_GNU_convert after e.g. floating
26822 point typed stack entry. */
26823 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26824 keep = loc->dw_loc_next;
26825 break;
26827 break;
26828 default:
26829 break;
26831 return true;
26834 /* Helper function of resolve_addr. DIE had DW_AT_location of
26835 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26836 and DW_OP_addr couldn't be resolved. resolve_addr has already
26837 removed the DW_AT_location attribute. This function attempts to
26838 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26839 to it or DW_AT_const_value attribute, if possible. */
26841 static void
26842 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26844 if (!VAR_P (decl)
26845 || lookup_decl_die (decl) != die
26846 || DECL_EXTERNAL (decl)
26847 || !TREE_STATIC (decl)
26848 || DECL_INITIAL (decl) == NULL_TREE
26849 || DECL_P (DECL_INITIAL (decl))
26850 || get_AT (die, DW_AT_const_value))
26851 return;
26853 tree init = DECL_INITIAL (decl);
26854 HOST_WIDE_INT offset = 0;
26855 /* For variables that have been optimized away and thus
26856 don't have a memory location, see if we can emit
26857 DW_AT_const_value instead. */
26858 if (tree_add_const_value_attribute (die, init))
26859 return;
26860 if (dwarf_strict)
26861 return;
26862 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26863 and ADDR_EXPR refers to a decl that has DW_AT_location or
26864 DW_AT_const_value (but isn't addressable, otherwise
26865 resolving the original DW_OP_addr wouldn't fail), see if
26866 we can add DW_OP_GNU_implicit_pointer. */
26867 STRIP_NOPS (init);
26868 if (TREE_CODE (init) == POINTER_PLUS_EXPR
26869 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26871 offset = tree_to_shwi (TREE_OPERAND (init, 1));
26872 init = TREE_OPERAND (init, 0);
26873 STRIP_NOPS (init);
26875 if (TREE_CODE (init) != ADDR_EXPR)
26876 return;
26877 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26878 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26879 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26880 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26881 && TREE_OPERAND (init, 0) != decl))
26883 dw_die_ref ref;
26884 dw_loc_descr_ref l;
26886 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26888 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26889 if (!rtl)
26890 return;
26891 decl = SYMBOL_REF_DECL (rtl);
26893 else
26894 decl = TREE_OPERAND (init, 0);
26895 ref = lookup_decl_die (decl);
26896 if (ref == NULL
26897 || (!get_AT (ref, DW_AT_location)
26898 && !get_AT (ref, DW_AT_const_value)))
26899 return;
26900 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26901 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26902 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26903 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26904 add_AT_loc (die, DW_AT_location, l);
26908 /* Return NULL if l is a DWARF expression, or first op that is not
26909 valid DWARF expression. */
26911 static dw_loc_descr_ref
26912 non_dwarf_expression (dw_loc_descr_ref l)
26914 while (l)
26916 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26917 return l;
26918 switch (l->dw_loc_opc)
26920 case DW_OP_regx:
26921 case DW_OP_implicit_value:
26922 case DW_OP_stack_value:
26923 case DW_OP_GNU_implicit_pointer:
26924 case DW_OP_GNU_parameter_ref:
26925 case DW_OP_piece:
26926 case DW_OP_bit_piece:
26927 return l;
26928 default:
26929 break;
26931 l = l->dw_loc_next;
26933 return NULL;
26936 /* Return adjusted copy of EXPR:
26937 If it is empty DWARF expression, return it.
26938 If it is valid non-empty DWARF expression,
26939 return copy of EXPR with copy of DEREF appended to it.
26940 If it is DWARF expression followed by DW_OP_reg{N,x}, return
26941 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
26942 and no DEREF.
26943 If it is DWARF expression followed by DW_OP_stack_value, return
26944 copy of the DWARF expression without anything appended.
26945 Otherwise, return NULL. */
26947 static dw_loc_descr_ref
26948 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
26951 if (expr == NULL)
26952 return NULL;
26954 dw_loc_descr_ref l = non_dwarf_expression (expr);
26955 if (l && l->dw_loc_next)
26956 return NULL;
26958 if (l)
26960 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26961 deref = new_loc_descr ((enum dwarf_location_atom)
26962 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
26963 0, 0);
26964 else
26965 switch (l->dw_loc_opc)
26967 case DW_OP_regx:
26968 deref = new_loc_descr (DW_OP_bregx,
26969 l->dw_loc_oprnd1.v.val_unsigned, 0);
26970 break;
26971 case DW_OP_stack_value:
26972 deref = NULL;
26973 break;
26974 default:
26975 return NULL;
26978 else
26979 deref = new_loc_descr (deref->dw_loc_opc,
26980 deref->dw_loc_oprnd1.v.val_int, 0);
26982 dw_loc_descr_ref ret = NULL, *p = &ret;
26983 while (expr != l)
26985 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
26986 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
26987 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
26988 p = &(*p)->dw_loc_next;
26989 expr = expr->dw_loc_next;
26991 *p = deref;
26992 return ret;
26995 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
26996 or argument, adjust it if needed and return:
26997 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
26998 attribute if present should be removed
26999 0 keep the attribute as is if the referenced var or argument has
27000 only DWARF expression that covers all ranges
27001 1 if the attribute has been successfully adjusted. */
27003 static int
27004 optimize_string_length (dw_attr_node *a)
27006 dw_loc_descr_ref l = AT_loc (a), lv;
27007 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
27008 dw_attr_node *av = get_AT (die, DW_AT_location);
27009 dw_loc_list_ref d;
27010 bool non_dwarf_expr = false;
27012 if (av == NULL)
27013 return -1;
27014 switch (AT_class (av))
27016 case dw_val_class_loc_list:
27017 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
27018 if (d->expr && non_dwarf_expression (d->expr))
27019 non_dwarf_expr = true;
27020 break;
27021 case dw_val_class_loc:
27022 lv = AT_loc (av);
27023 if (lv == NULL)
27024 return -1;
27025 if (non_dwarf_expression (lv))
27026 non_dwarf_expr = true;
27027 break;
27028 default:
27029 return -1;
27032 /* If it is safe to keep DW_OP_call4 in, keep it. */
27033 if (!non_dwarf_expr
27034 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
27035 return 0;
27037 /* If not dereferencing the DW_OP_call4 afterwards, we can just
27038 copy over the DW_AT_location attribute from die to a. */
27039 if (l->dw_loc_next == NULL)
27041 a->dw_attr_val = av->dw_attr_val;
27042 return 1;
27045 dw_loc_list_ref list, *p;
27046 switch (AT_class (av))
27048 case dw_val_class_loc_list:
27049 p = &list;
27050 list = NULL;
27051 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
27053 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
27054 if (lv)
27056 *p = new_loc_list (lv, d->begin, d->end, d->section);
27057 p = &(*p)->dw_loc_next;
27060 if (list == NULL)
27061 return -1;
27062 a->dw_attr_val.val_class = dw_val_class_loc_list;
27063 gen_llsym (list);
27064 *AT_loc_list_ptr (a) = list;
27065 return 1;
27066 case dw_val_class_loc:
27067 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
27068 if (lv == NULL)
27069 return -1;
27070 a->dw_attr_val.v.val_loc = lv;
27071 return 1;
27072 default:
27073 gcc_unreachable ();
27077 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
27078 an address in .rodata section if the string literal is emitted there,
27079 or remove the containing location list or replace DW_AT_const_value
27080 with DW_AT_location and empty location expression, if it isn't found
27081 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
27082 to something that has been emitted in the current CU. */
27084 static void
27085 resolve_addr (dw_die_ref die)
27087 dw_die_ref c;
27088 dw_attr_node *a;
27089 dw_loc_list_ref *curr, *start, loc;
27090 unsigned ix;
27091 bool remove_AT_byte_size = false;
27093 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27094 switch (AT_class (a))
27096 case dw_val_class_loc_list:
27097 start = curr = AT_loc_list_ptr (a);
27098 loc = *curr;
27099 gcc_assert (loc);
27100 /* The same list can be referenced more than once. See if we have
27101 already recorded the result from a previous pass. */
27102 if (loc->replaced)
27103 *curr = loc->dw_loc_next;
27104 else if (!loc->resolved_addr)
27106 /* As things stand, we do not expect or allow one die to
27107 reference a suffix of another die's location list chain.
27108 References must be identical or completely separate.
27109 There is therefore no need to cache the result of this
27110 pass on any list other than the first; doing so
27111 would lead to unnecessary writes. */
27112 while (*curr)
27114 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
27115 if (!resolve_addr_in_expr ((*curr)->expr))
27117 dw_loc_list_ref next = (*curr)->dw_loc_next;
27118 dw_loc_descr_ref l = (*curr)->expr;
27120 if (next && (*curr)->ll_symbol)
27122 gcc_assert (!next->ll_symbol);
27123 next->ll_symbol = (*curr)->ll_symbol;
27125 if (dwarf_split_debug_info)
27126 remove_loc_list_addr_table_entries (l);
27127 *curr = next;
27129 else
27131 mark_base_types ((*curr)->expr);
27132 curr = &(*curr)->dw_loc_next;
27135 if (loc == *start)
27136 loc->resolved_addr = 1;
27137 else
27139 loc->replaced = 1;
27140 loc->dw_loc_next = *start;
27143 if (!*start)
27145 remove_AT (die, a->dw_attr);
27146 ix--;
27148 break;
27149 case dw_val_class_loc:
27151 dw_loc_descr_ref l = AT_loc (a);
27152 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
27153 DW_AT_string_length is only a rough approximation; unfortunately
27154 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
27155 needs a DWARF expression, while DW_AT_location of the referenced
27156 variable or argument might be any location description. */
27157 if (a->dw_attr == DW_AT_string_length
27158 && l
27159 && l->dw_loc_opc == DW_OP_call4
27160 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
27161 && (l->dw_loc_next == NULL
27162 || (l->dw_loc_next->dw_loc_next == NULL
27163 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
27164 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
27166 switch (optimize_string_length (a))
27168 case -1:
27169 remove_AT (die, a->dw_attr);
27170 ix--;
27171 /* If we drop DW_AT_string_length, we need to drop also
27172 DW_AT_{string_length_,}byte_size. */
27173 remove_AT_byte_size = true;
27174 continue;
27175 default:
27176 break;
27177 case 1:
27178 /* Even if we keep the optimized DW_AT_string_length,
27179 it might have changed AT_class, so process it again. */
27180 ix--;
27181 continue;
27184 /* For -gdwarf-2 don't attempt to optimize
27185 DW_AT_data_member_location containing
27186 DW_OP_plus_uconst - older consumers might
27187 rely on it being that op instead of a more complex,
27188 but shorter, location description. */
27189 if ((dwarf_version > 2
27190 || a->dw_attr != DW_AT_data_member_location
27191 || l == NULL
27192 || l->dw_loc_opc != DW_OP_plus_uconst
27193 || l->dw_loc_next != NULL)
27194 && !resolve_addr_in_expr (l))
27196 if (dwarf_split_debug_info)
27197 remove_loc_list_addr_table_entries (l);
27198 if (l != NULL
27199 && l->dw_loc_next == NULL
27200 && l->dw_loc_opc == DW_OP_addr
27201 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
27202 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
27203 && a->dw_attr == DW_AT_location)
27205 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
27206 remove_AT (die, a->dw_attr);
27207 ix--;
27208 optimize_location_into_implicit_ptr (die, decl);
27209 break;
27211 remove_AT (die, a->dw_attr);
27212 ix--;
27214 else
27215 mark_base_types (l);
27217 break;
27218 case dw_val_class_addr:
27219 if (a->dw_attr == DW_AT_const_value
27220 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
27222 if (AT_index (a) != NOT_INDEXED)
27223 remove_addr_table_entry (a->dw_attr_val.val_entry);
27224 remove_AT (die, a->dw_attr);
27225 ix--;
27227 if (die->die_tag == DW_TAG_GNU_call_site
27228 && a->dw_attr == DW_AT_abstract_origin)
27230 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
27231 dw_die_ref tdie = lookup_decl_die (tdecl);
27232 dw_die_ref cdie;
27233 if (tdie == NULL
27234 && DECL_EXTERNAL (tdecl)
27235 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
27236 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
27238 /* Creating a full DIE for tdecl is overly expensive and
27239 at this point even wrong when in the LTO phase
27240 as it can end up generating new type DIEs we didn't
27241 output and thus optimize_external_refs will crash. */
27242 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
27243 add_AT_flag (tdie, DW_AT_external, 1);
27244 add_AT_flag (tdie, DW_AT_declaration, 1);
27245 add_linkage_attr (tdie, tdecl);
27246 add_name_and_src_coords_attributes (tdie, tdecl);
27247 equate_decl_number_to_die (tdecl, tdie);
27249 if (tdie)
27251 a->dw_attr_val.val_class = dw_val_class_die_ref;
27252 a->dw_attr_val.v.val_die_ref.die = tdie;
27253 a->dw_attr_val.v.val_die_ref.external = 0;
27255 else
27257 if (AT_index (a) != NOT_INDEXED)
27258 remove_addr_table_entry (a->dw_attr_val.val_entry);
27259 remove_AT (die, a->dw_attr);
27260 ix--;
27263 break;
27264 default:
27265 break;
27268 if (remove_AT_byte_size)
27269 remove_AT (die, dwarf_version >= 5
27270 ? DW_AT_string_length_byte_size
27271 : DW_AT_byte_size);
27273 FOR_EACH_CHILD (die, c, resolve_addr (c));
27276 /* Helper routines for optimize_location_lists.
27277 This pass tries to share identical local lists in .debug_loc
27278 section. */
27280 /* Iteratively hash operands of LOC opcode into HSTATE. */
27282 static void
27283 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
27285 dw_val_ref val1 = &loc->dw_loc_oprnd1;
27286 dw_val_ref val2 = &loc->dw_loc_oprnd2;
27288 switch (loc->dw_loc_opc)
27290 case DW_OP_const4u:
27291 case DW_OP_const8u:
27292 if (loc->dtprel)
27293 goto hash_addr;
27294 /* FALLTHRU */
27295 case DW_OP_const1u:
27296 case DW_OP_const1s:
27297 case DW_OP_const2u:
27298 case DW_OP_const2s:
27299 case DW_OP_const4s:
27300 case DW_OP_const8s:
27301 case DW_OP_constu:
27302 case DW_OP_consts:
27303 case DW_OP_pick:
27304 case DW_OP_plus_uconst:
27305 case DW_OP_breg0:
27306 case DW_OP_breg1:
27307 case DW_OP_breg2:
27308 case DW_OP_breg3:
27309 case DW_OP_breg4:
27310 case DW_OP_breg5:
27311 case DW_OP_breg6:
27312 case DW_OP_breg7:
27313 case DW_OP_breg8:
27314 case DW_OP_breg9:
27315 case DW_OP_breg10:
27316 case DW_OP_breg11:
27317 case DW_OP_breg12:
27318 case DW_OP_breg13:
27319 case DW_OP_breg14:
27320 case DW_OP_breg15:
27321 case DW_OP_breg16:
27322 case DW_OP_breg17:
27323 case DW_OP_breg18:
27324 case DW_OP_breg19:
27325 case DW_OP_breg20:
27326 case DW_OP_breg21:
27327 case DW_OP_breg22:
27328 case DW_OP_breg23:
27329 case DW_OP_breg24:
27330 case DW_OP_breg25:
27331 case DW_OP_breg26:
27332 case DW_OP_breg27:
27333 case DW_OP_breg28:
27334 case DW_OP_breg29:
27335 case DW_OP_breg30:
27336 case DW_OP_breg31:
27337 case DW_OP_regx:
27338 case DW_OP_fbreg:
27339 case DW_OP_piece:
27340 case DW_OP_deref_size:
27341 case DW_OP_xderef_size:
27342 hstate.add_object (val1->v.val_int);
27343 break;
27344 case DW_OP_skip:
27345 case DW_OP_bra:
27347 int offset;
27349 gcc_assert (val1->val_class == dw_val_class_loc);
27350 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
27351 hstate.add_object (offset);
27353 break;
27354 case DW_OP_implicit_value:
27355 hstate.add_object (val1->v.val_unsigned);
27356 switch (val2->val_class)
27358 case dw_val_class_const:
27359 hstate.add_object (val2->v.val_int);
27360 break;
27361 case dw_val_class_vec:
27363 unsigned int elt_size = val2->v.val_vec.elt_size;
27364 unsigned int len = val2->v.val_vec.length;
27366 hstate.add_int (elt_size);
27367 hstate.add_int (len);
27368 hstate.add (val2->v.val_vec.array, len * elt_size);
27370 break;
27371 case dw_val_class_const_double:
27372 hstate.add_object (val2->v.val_double.low);
27373 hstate.add_object (val2->v.val_double.high);
27374 break;
27375 case dw_val_class_wide_int:
27376 hstate.add (val2->v.val_wide->get_val (),
27377 get_full_len (*val2->v.val_wide)
27378 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27379 break;
27380 case dw_val_class_addr:
27381 inchash::add_rtx (val2->v.val_addr, hstate);
27382 break;
27383 default:
27384 gcc_unreachable ();
27386 break;
27387 case DW_OP_bregx:
27388 case DW_OP_bit_piece:
27389 hstate.add_object (val1->v.val_int);
27390 hstate.add_object (val2->v.val_int);
27391 break;
27392 case DW_OP_addr:
27393 hash_addr:
27394 if (loc->dtprel)
27396 unsigned char dtprel = 0xd1;
27397 hstate.add_object (dtprel);
27399 inchash::add_rtx (val1->v.val_addr, hstate);
27400 break;
27401 case DW_OP_GNU_addr_index:
27402 case DW_OP_GNU_const_index:
27404 if (loc->dtprel)
27406 unsigned char dtprel = 0xd1;
27407 hstate.add_object (dtprel);
27409 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
27411 break;
27412 case DW_OP_GNU_implicit_pointer:
27413 hstate.add_int (val2->v.val_int);
27414 break;
27415 case DW_OP_GNU_entry_value:
27416 hstate.add_object (val1->v.val_loc);
27417 break;
27418 case DW_OP_GNU_regval_type:
27419 case DW_OP_GNU_deref_type:
27421 unsigned int byte_size
27422 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
27423 unsigned int encoding
27424 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
27425 hstate.add_object (val1->v.val_int);
27426 hstate.add_object (byte_size);
27427 hstate.add_object (encoding);
27429 break;
27430 case DW_OP_GNU_convert:
27431 case DW_OP_GNU_reinterpret:
27432 if (val1->val_class == dw_val_class_unsigned_const)
27434 hstate.add_object (val1->v.val_unsigned);
27435 break;
27437 /* FALLTHRU */
27438 case DW_OP_GNU_const_type:
27440 unsigned int byte_size
27441 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
27442 unsigned int encoding
27443 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
27444 hstate.add_object (byte_size);
27445 hstate.add_object (encoding);
27446 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
27447 break;
27448 hstate.add_object (val2->val_class);
27449 switch (val2->val_class)
27451 case dw_val_class_const:
27452 hstate.add_object (val2->v.val_int);
27453 break;
27454 case dw_val_class_vec:
27456 unsigned int elt_size = val2->v.val_vec.elt_size;
27457 unsigned int len = val2->v.val_vec.length;
27459 hstate.add_object (elt_size);
27460 hstate.add_object (len);
27461 hstate.add (val2->v.val_vec.array, len * elt_size);
27463 break;
27464 case dw_val_class_const_double:
27465 hstate.add_object (val2->v.val_double.low);
27466 hstate.add_object (val2->v.val_double.high);
27467 break;
27468 case dw_val_class_wide_int:
27469 hstate.add (val2->v.val_wide->get_val (),
27470 get_full_len (*val2->v.val_wide)
27471 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27472 break;
27473 default:
27474 gcc_unreachable ();
27477 break;
27479 default:
27480 /* Other codes have no operands. */
27481 break;
27485 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
27487 static inline void
27488 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
27490 dw_loc_descr_ref l;
27491 bool sizes_computed = false;
27492 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
27493 size_of_locs (loc);
27495 for (l = loc; l != NULL; l = l->dw_loc_next)
27497 enum dwarf_location_atom opc = l->dw_loc_opc;
27498 hstate.add_object (opc);
27499 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
27501 size_of_locs (loc);
27502 sizes_computed = true;
27504 hash_loc_operands (l, hstate);
27508 /* Compute hash of the whole location list LIST_HEAD. */
27510 static inline void
27511 hash_loc_list (dw_loc_list_ref list_head)
27513 dw_loc_list_ref curr = list_head;
27514 inchash::hash hstate;
27516 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
27518 hstate.add (curr->begin, strlen (curr->begin) + 1);
27519 hstate.add (curr->end, strlen (curr->end) + 1);
27520 if (curr->section)
27521 hstate.add (curr->section, strlen (curr->section) + 1);
27522 hash_locs (curr->expr, hstate);
27524 list_head->hash = hstate.end ();
27527 /* Return true if X and Y opcodes have the same operands. */
27529 static inline bool
27530 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
27532 dw_val_ref valx1 = &x->dw_loc_oprnd1;
27533 dw_val_ref valx2 = &x->dw_loc_oprnd2;
27534 dw_val_ref valy1 = &y->dw_loc_oprnd1;
27535 dw_val_ref valy2 = &y->dw_loc_oprnd2;
27537 switch (x->dw_loc_opc)
27539 case DW_OP_const4u:
27540 case DW_OP_const8u:
27541 if (x->dtprel)
27542 goto hash_addr;
27543 /* FALLTHRU */
27544 case DW_OP_const1u:
27545 case DW_OP_const1s:
27546 case DW_OP_const2u:
27547 case DW_OP_const2s:
27548 case DW_OP_const4s:
27549 case DW_OP_const8s:
27550 case DW_OP_constu:
27551 case DW_OP_consts:
27552 case DW_OP_pick:
27553 case DW_OP_plus_uconst:
27554 case DW_OP_breg0:
27555 case DW_OP_breg1:
27556 case DW_OP_breg2:
27557 case DW_OP_breg3:
27558 case DW_OP_breg4:
27559 case DW_OP_breg5:
27560 case DW_OP_breg6:
27561 case DW_OP_breg7:
27562 case DW_OP_breg8:
27563 case DW_OP_breg9:
27564 case DW_OP_breg10:
27565 case DW_OP_breg11:
27566 case DW_OP_breg12:
27567 case DW_OP_breg13:
27568 case DW_OP_breg14:
27569 case DW_OP_breg15:
27570 case DW_OP_breg16:
27571 case DW_OP_breg17:
27572 case DW_OP_breg18:
27573 case DW_OP_breg19:
27574 case DW_OP_breg20:
27575 case DW_OP_breg21:
27576 case DW_OP_breg22:
27577 case DW_OP_breg23:
27578 case DW_OP_breg24:
27579 case DW_OP_breg25:
27580 case DW_OP_breg26:
27581 case DW_OP_breg27:
27582 case DW_OP_breg28:
27583 case DW_OP_breg29:
27584 case DW_OP_breg30:
27585 case DW_OP_breg31:
27586 case DW_OP_regx:
27587 case DW_OP_fbreg:
27588 case DW_OP_piece:
27589 case DW_OP_deref_size:
27590 case DW_OP_xderef_size:
27591 return valx1->v.val_int == valy1->v.val_int;
27592 case DW_OP_skip:
27593 case DW_OP_bra:
27594 /* If splitting debug info, the use of DW_OP_GNU_addr_index
27595 can cause irrelevant differences in dw_loc_addr. */
27596 gcc_assert (valx1->val_class == dw_val_class_loc
27597 && valy1->val_class == dw_val_class_loc
27598 && (dwarf_split_debug_info
27599 || x->dw_loc_addr == y->dw_loc_addr));
27600 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27601 case DW_OP_implicit_value:
27602 if (valx1->v.val_unsigned != valy1->v.val_unsigned
27603 || valx2->val_class != valy2->val_class)
27604 return false;
27605 switch (valx2->val_class)
27607 case dw_val_class_const:
27608 return valx2->v.val_int == valy2->v.val_int;
27609 case dw_val_class_vec:
27610 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27611 && valx2->v.val_vec.length == valy2->v.val_vec.length
27612 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27613 valx2->v.val_vec.elt_size
27614 * valx2->v.val_vec.length) == 0;
27615 case dw_val_class_const_double:
27616 return valx2->v.val_double.low == valy2->v.val_double.low
27617 && valx2->v.val_double.high == valy2->v.val_double.high;
27618 case dw_val_class_wide_int:
27619 return *valx2->v.val_wide == *valy2->v.val_wide;
27620 case dw_val_class_addr:
27621 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27622 default:
27623 gcc_unreachable ();
27625 case DW_OP_bregx:
27626 case DW_OP_bit_piece:
27627 return valx1->v.val_int == valy1->v.val_int
27628 && valx2->v.val_int == valy2->v.val_int;
27629 case DW_OP_addr:
27630 hash_addr:
27631 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27632 case DW_OP_GNU_addr_index:
27633 case DW_OP_GNU_const_index:
27635 rtx ax1 = valx1->val_entry->addr.rtl;
27636 rtx ay1 = valy1->val_entry->addr.rtl;
27637 return rtx_equal_p (ax1, ay1);
27639 case DW_OP_GNU_implicit_pointer:
27640 return valx1->val_class == dw_val_class_die_ref
27641 && valx1->val_class == valy1->val_class
27642 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27643 && valx2->v.val_int == valy2->v.val_int;
27644 case DW_OP_GNU_entry_value:
27645 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27646 case DW_OP_GNU_const_type:
27647 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27648 || valx2->val_class != valy2->val_class)
27649 return false;
27650 switch (valx2->val_class)
27652 case dw_val_class_const:
27653 return valx2->v.val_int == valy2->v.val_int;
27654 case dw_val_class_vec:
27655 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27656 && valx2->v.val_vec.length == valy2->v.val_vec.length
27657 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27658 valx2->v.val_vec.elt_size
27659 * valx2->v.val_vec.length) == 0;
27660 case dw_val_class_const_double:
27661 return valx2->v.val_double.low == valy2->v.val_double.low
27662 && valx2->v.val_double.high == valy2->v.val_double.high;
27663 case dw_val_class_wide_int:
27664 return *valx2->v.val_wide == *valy2->v.val_wide;
27665 default:
27666 gcc_unreachable ();
27668 case DW_OP_GNU_regval_type:
27669 case DW_OP_GNU_deref_type:
27670 return valx1->v.val_int == valy1->v.val_int
27671 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27672 case DW_OP_GNU_convert:
27673 case DW_OP_GNU_reinterpret:
27674 if (valx1->val_class != valy1->val_class)
27675 return false;
27676 if (valx1->val_class == dw_val_class_unsigned_const)
27677 return valx1->v.val_unsigned == valy1->v.val_unsigned;
27678 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27679 case DW_OP_GNU_parameter_ref:
27680 return valx1->val_class == dw_val_class_die_ref
27681 && valx1->val_class == valy1->val_class
27682 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27683 default:
27684 /* Other codes have no operands. */
27685 return true;
27689 /* Return true if DWARF location expressions X and Y are the same. */
27691 static inline bool
27692 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27694 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27695 if (x->dw_loc_opc != y->dw_loc_opc
27696 || x->dtprel != y->dtprel
27697 || !compare_loc_operands (x, y))
27698 break;
27699 return x == NULL && y == NULL;
27702 /* Hashtable helpers. */
27704 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27706 static inline hashval_t hash (const dw_loc_list_struct *);
27707 static inline bool equal (const dw_loc_list_struct *,
27708 const dw_loc_list_struct *);
27711 /* Return precomputed hash of location list X. */
27713 inline hashval_t
27714 loc_list_hasher::hash (const dw_loc_list_struct *x)
27716 return x->hash;
27719 /* Return true if location lists A and B are the same. */
27721 inline bool
27722 loc_list_hasher::equal (const dw_loc_list_struct *a,
27723 const dw_loc_list_struct *b)
27725 if (a == b)
27726 return 1;
27727 if (a->hash != b->hash)
27728 return 0;
27729 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27730 if (strcmp (a->begin, b->begin) != 0
27731 || strcmp (a->end, b->end) != 0
27732 || (a->section == NULL) != (b->section == NULL)
27733 || (a->section && strcmp (a->section, b->section) != 0)
27734 || !compare_locs (a->expr, b->expr))
27735 break;
27736 return a == NULL && b == NULL;
27739 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27742 /* Recursively optimize location lists referenced from DIE
27743 children and share them whenever possible. */
27745 static void
27746 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27748 dw_die_ref c;
27749 dw_attr_node *a;
27750 unsigned ix;
27751 dw_loc_list_struct **slot;
27753 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27754 if (AT_class (a) == dw_val_class_loc_list)
27756 dw_loc_list_ref list = AT_loc_list (a);
27757 /* TODO: perform some optimizations here, before hashing
27758 it and storing into the hash table. */
27759 hash_loc_list (list);
27760 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27761 if (*slot == NULL)
27762 *slot = list;
27763 else
27764 a->dw_attr_val.v.val_loc_list = *slot;
27767 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27771 /* Recursively assign each location list a unique index into the debug_addr
27772 section. */
27774 static void
27775 index_location_lists (dw_die_ref die)
27777 dw_die_ref c;
27778 dw_attr_node *a;
27779 unsigned ix;
27781 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27782 if (AT_class (a) == dw_val_class_loc_list)
27784 dw_loc_list_ref list = AT_loc_list (a);
27785 dw_loc_list_ref curr;
27786 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27788 /* Don't index an entry that has already been indexed
27789 or won't be output. */
27790 if (curr->begin_entry != NULL
27791 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27792 continue;
27794 curr->begin_entry
27795 = add_addr_table_entry (xstrdup (curr->begin),
27796 ate_kind_label);
27800 FOR_EACH_CHILD (die, c, index_location_lists (c));
27803 /* Optimize location lists referenced from DIE
27804 children and share them whenever possible. */
27806 static void
27807 optimize_location_lists (dw_die_ref die)
27809 loc_list_hash_type htab (500);
27810 optimize_location_lists_1 (die, &htab);
27813 /* Traverse the limbo die list, and add parent/child links. The only
27814 dies without parents that should be here are concrete instances of
27815 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
27816 For concrete instances, we can get the parent die from the abstract
27817 instance. */
27819 static void
27820 flush_limbo_die_list (void)
27822 limbo_die_node *node;
27824 /* get_context_die calls force_decl_die, which can put new DIEs on the
27825 limbo list in LTO mode when nested functions are put in a different
27826 partition than that of their parent function. */
27827 while ((node = limbo_die_list))
27829 dw_die_ref die = node->die;
27830 limbo_die_list = node->next;
27832 if (die->die_parent == NULL)
27834 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27836 if (origin && origin->die_parent)
27837 add_child_die (origin->die_parent, die);
27838 else if (is_cu_die (die))
27840 else if (seen_error ())
27841 /* It's OK to be confused by errors in the input. */
27842 add_child_die (comp_unit_die (), die);
27843 else
27845 /* In certain situations, the lexical block containing a
27846 nested function can be optimized away, which results
27847 in the nested function die being orphaned. Likewise
27848 with the return type of that nested function. Force
27849 this to be a child of the containing function.
27851 It may happen that even the containing function got fully
27852 inlined and optimized out. In that case we are lost and
27853 assign the empty child. This should not be big issue as
27854 the function is likely unreachable too. */
27855 gcc_assert (node->created_for);
27857 if (DECL_P (node->created_for))
27858 origin = get_context_die (DECL_CONTEXT (node->created_for));
27859 else if (TYPE_P (node->created_for))
27860 origin = scope_die_for (node->created_for, comp_unit_die ());
27861 else
27862 origin = comp_unit_die ();
27864 add_child_die (origin, die);
27870 /* Output stuff that dwarf requires at the end of every file,
27871 and generate the DWARF-2 debugging info. */
27873 static void
27874 dwarf2out_finish (const char *)
27876 comdat_type_node *ctnode;
27877 dw_die_ref main_comp_unit_die;
27879 /* Flush out any latecomers to the limbo party. */
27880 flush_limbo_die_list ();
27882 if (flag_checking)
27884 verify_die (comp_unit_die ());
27885 for (limbo_die_node *node = cu_die_list; node; node = node->next)
27886 verify_die (node->die);
27889 /* We shouldn't have any symbols with delayed asm names for
27890 DIEs generated after early finish. */
27891 gcc_assert (deferred_asm_name == NULL);
27893 gen_remaining_tmpl_value_param_die_attribute ();
27895 #if ENABLE_ASSERT_CHECKING
27897 dw_die_ref die = comp_unit_die (), c;
27898 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27900 #endif
27901 resolve_addr (comp_unit_die ());
27902 move_marked_base_types ();
27904 /* Initialize sections and labels used for actual assembler output. */
27905 init_sections_and_labels ();
27907 /* Traverse the DIE's and add sibling attributes to those DIE's that
27908 have children. */
27909 add_sibling_attributes (comp_unit_die ());
27910 limbo_die_node *node;
27911 for (node = cu_die_list; node; node = node->next)
27912 add_sibling_attributes (node->die);
27913 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27914 add_sibling_attributes (ctnode->root_die);
27916 /* When splitting DWARF info, we put some attributes in the
27917 skeleton compile_unit DIE that remains in the .o, while
27918 most attributes go in the DWO compile_unit_die. */
27919 if (dwarf_split_debug_info)
27921 limbo_die_node *cu;
27922 main_comp_unit_die = gen_compile_unit_die (NULL);
27923 cu = limbo_die_list;
27924 gcc_assert (cu->die == main_comp_unit_die);
27925 limbo_die_list = limbo_die_list->next;
27926 cu->next = cu_die_list;
27927 cu_die_list = cu;
27929 else
27930 main_comp_unit_die = comp_unit_die ();
27932 /* Output a terminator label for the .text section. */
27933 switch_to_section (text_section);
27934 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27935 if (cold_text_section)
27937 switch_to_section (cold_text_section);
27938 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27941 /* We can only use the low/high_pc attributes if all of the code was
27942 in .text. */
27943 if (!have_multiple_function_sections
27944 || (dwarf_version < 3 && dwarf_strict))
27946 /* Don't add if the CU has no associated code. */
27947 if (text_section_used)
27948 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27949 text_end_label, true);
27951 else
27953 unsigned fde_idx;
27954 dw_fde_ref fde;
27955 bool range_list_added = false;
27957 if (text_section_used)
27958 add_ranges_by_labels (main_comp_unit_die, text_section_label,
27959 text_end_label, &range_list_added, true);
27960 if (cold_text_section_used)
27961 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27962 cold_end_label, &range_list_added, true);
27964 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27966 if (DECL_IGNORED_P (fde->decl))
27967 continue;
27968 if (!fde->in_std_section)
27969 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27970 fde->dw_fde_end, &range_list_added,
27971 true);
27972 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27973 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27974 fde->dw_fde_second_end, &range_list_added,
27975 true);
27978 if (range_list_added)
27980 /* We need to give .debug_loc and .debug_ranges an appropriate
27981 "base address". Use zero so that these addresses become
27982 absolute. Historically, we've emitted the unexpected
27983 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27984 Emit both to give time for other tools to adapt. */
27985 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27986 if (! dwarf_strict && dwarf_version < 4)
27987 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27989 add_ranges (NULL);
27993 if (debug_info_level >= DINFO_LEVEL_TERSE)
27994 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27995 debug_line_section_label);
27997 if (have_macinfo)
27998 add_AT_macptr (comp_unit_die (),
27999 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
28000 macinfo_section_label);
28002 if (dwarf_split_debug_info)
28004 /* optimize_location_lists calculates the size of the lists,
28005 so index them first, and assign indices to the entries.
28006 Although optimize_location_lists will remove entries from
28007 the table, it only does so for duplicates, and therefore
28008 only reduces ref_counts to 1. */
28009 index_location_lists (comp_unit_die ());
28011 if (addr_index_table != NULL)
28013 unsigned int index = 0;
28014 addr_index_table
28015 ->traverse_noresize<unsigned int *, index_addr_table_entry>
28016 (&index);
28020 if (have_location_lists)
28021 optimize_location_lists (comp_unit_die ());
28023 save_macinfo_strings ();
28025 if (dwarf_split_debug_info)
28027 unsigned int index = 0;
28029 /* Add attributes common to skeleton compile_units and
28030 type_units. Because these attributes include strings, it
28031 must be done before freezing the string table. Top-level
28032 skeleton die attrs are added when the skeleton type unit is
28033 created, so ensure it is created by this point. */
28034 add_top_level_skeleton_die_attrs (main_comp_unit_die);
28035 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
28038 /* Output all of the compilation units. We put the main one last so that
28039 the offsets are available to output_pubnames. */
28040 for (node = cu_die_list; node; node = node->next)
28041 output_comp_unit (node->die, 0);
28043 hash_table<comdat_type_hasher> comdat_type_table (100);
28044 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
28046 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
28048 /* Don't output duplicate types. */
28049 if (*slot != HTAB_EMPTY_ENTRY)
28050 continue;
28052 /* Add a pointer to the line table for the main compilation unit
28053 so that the debugger can make sense of DW_AT_decl_file
28054 attributes. */
28055 if (debug_info_level >= DINFO_LEVEL_TERSE)
28056 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
28057 (!dwarf_split_debug_info
28058 ? debug_line_section_label
28059 : debug_skeleton_line_section_label));
28061 output_comdat_type_unit (ctnode);
28062 *slot = ctnode;
28065 /* The AT_pubnames attribute needs to go in all skeleton dies, including
28066 both the main_cu and all skeleton TUs. Making this call unconditional
28067 would end up either adding a second copy of the AT_pubnames attribute, or
28068 requiring a special case in add_top_level_skeleton_die_attrs. */
28069 if (!dwarf_split_debug_info)
28070 add_AT_pubnames (comp_unit_die ());
28072 if (dwarf_split_debug_info)
28074 int mark;
28075 unsigned char checksum[16];
28076 struct md5_ctx ctx;
28078 /* Compute a checksum of the comp_unit to use as the dwo_id. */
28079 md5_init_ctx (&ctx);
28080 mark = 0;
28081 die_checksum (comp_unit_die (), &ctx, &mark);
28082 unmark_all_dies (comp_unit_die ());
28083 md5_finish_ctx (&ctx, checksum);
28085 /* Use the first 8 bytes of the checksum as the dwo_id,
28086 and add it to both comp-unit DIEs. */
28087 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
28088 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
28090 /* Add the base offset of the ranges table to the skeleton
28091 comp-unit DIE. */
28092 if (ranges_table_in_use)
28093 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
28094 ranges_section_label);
28096 switch_to_section (debug_addr_section);
28097 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
28098 output_addr_table ();
28101 /* Output the main compilation unit if non-empty or if .debug_macinfo
28102 or .debug_macro will be emitted. */
28103 output_comp_unit (comp_unit_die (), have_macinfo);
28105 if (dwarf_split_debug_info && info_section_emitted)
28106 output_skeleton_debug_sections (main_comp_unit_die);
28108 /* Output the abbreviation table. */
28109 if (abbrev_die_table_in_use != 1)
28111 switch_to_section (debug_abbrev_section);
28112 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
28113 output_abbrev_section ();
28116 /* Output location list section if necessary. */
28117 if (have_location_lists)
28119 /* Output the location lists info. */
28120 switch_to_section (debug_loc_section);
28121 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
28122 output_location_lists (comp_unit_die ());
28125 output_pubtables ();
28127 /* Output the address range information if a CU (.debug_info section)
28128 was emitted. We output an empty table even if we had no functions
28129 to put in it. This because the consumer has no way to tell the
28130 difference between an empty table that we omitted and failure to
28131 generate a table that would have contained data. */
28132 if (info_section_emitted)
28134 switch_to_section (debug_aranges_section);
28135 output_aranges ();
28138 /* Output ranges section if necessary. */
28139 if (ranges_table_in_use)
28141 switch_to_section (debug_ranges_section);
28142 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
28143 output_ranges ();
28146 /* Have to end the macro section. */
28147 if (have_macinfo)
28149 switch_to_section (debug_macinfo_section);
28150 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
28151 output_macinfo ();
28152 dw2_asm_output_data (1, 0, "End compilation unit");
28155 /* Output the source line correspondence table. We must do this
28156 even if there is no line information. Otherwise, on an empty
28157 translation unit, we will generate a present, but empty,
28158 .debug_info section. IRIX 6.5 `nm' will then complain when
28159 examining the file. This is done late so that any filenames
28160 used by the debug_info section are marked as 'used'. */
28161 switch_to_section (debug_line_section);
28162 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
28163 if (! DWARF2_ASM_LINE_DEBUG_INFO)
28164 output_line_info (false);
28166 if (dwarf_split_debug_info && info_section_emitted)
28168 switch_to_section (debug_skeleton_line_section);
28169 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
28170 output_line_info (true);
28173 /* If we emitted any indirect strings, output the string table too. */
28174 if (debug_str_hash || skeleton_debug_str_hash)
28175 output_indirect_strings ();
28178 /* Perform any cleanups needed after the early debug generation pass
28179 has run. */
28181 static void
28182 dwarf2out_early_finish (const char *filename)
28184 set_early_dwarf s;
28186 /* PCH might result in DW_AT_producer string being restored from the
28187 header compilation, so always fill it with empty string initially
28188 and overwrite only here. */
28189 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
28190 producer_string = gen_producer_string ();
28191 producer->dw_attr_val.v.val_str->refcount--;
28192 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
28194 /* Add the name for the main input file now. We delayed this from
28195 dwarf2out_init to avoid complications with PCH. */
28196 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
28197 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
28198 add_comp_dir_attribute (comp_unit_die ());
28199 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
28201 bool p = false;
28202 file_table->traverse<bool *, file_table_relative_p> (&p);
28203 if (p)
28204 add_comp_dir_attribute (comp_unit_die ());
28207 /* With LTO early dwarf was really finished at compile-time, so make
28208 sure to adjust the phase after annotating the LTRANS CU DIE. */
28209 if (in_lto_p)
28211 early_dwarf_finished = true;
28212 return;
28215 /* Walk through the list of incomplete types again, trying once more to
28216 emit full debugging info for them. */
28217 retry_incomplete_types ();
28219 /* The point here is to flush out the limbo list so that it is empty
28220 and we don't need to stream it for LTO. */
28221 flush_limbo_die_list ();
28223 gen_scheduled_generic_parms_dies ();
28224 gen_remaining_tmpl_value_param_die_attribute ();
28226 /* Add DW_AT_linkage_name for all deferred DIEs. */
28227 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
28229 tree decl = node->created_for;
28230 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
28231 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
28232 ended up in deferred_asm_name before we knew it was
28233 constant and never written to disk. */
28234 && DECL_ASSEMBLER_NAME (decl))
28236 add_linkage_attr (node->die, decl);
28237 move_linkage_attr (node->die);
28240 deferred_asm_name = NULL;
28242 if (flag_eliminate_unused_debug_types)
28243 prune_unused_types ();
28245 /* Generate separate COMDAT sections for type DIEs. */
28246 if (use_debug_types)
28248 break_out_comdat_types (comp_unit_die ());
28250 /* Each new type_unit DIE was added to the limbo die list when created.
28251 Since these have all been added to comdat_type_list, clear the
28252 limbo die list. */
28253 limbo_die_list = NULL;
28255 /* For each new comdat type unit, copy declarations for incomplete
28256 types to make the new unit self-contained (i.e., no direct
28257 references to the main compile unit). */
28258 for (comdat_type_node *ctnode = comdat_type_list;
28259 ctnode != NULL; ctnode = ctnode->next)
28260 copy_decls_for_unworthy_types (ctnode->root_die);
28261 copy_decls_for_unworthy_types (comp_unit_die ());
28263 /* In the process of copying declarations from one unit to another,
28264 we may have left some declarations behind that are no longer
28265 referenced. Prune them. */
28266 prune_unused_types ();
28269 /* Generate separate CUs for each of the include files we've seen.
28270 They will go into limbo_die_list and from there to cu_die_list. */
28271 if (flag_eliminate_dwarf2_dups)
28273 gcc_assert (limbo_die_list == NULL);
28274 break_out_includes (comp_unit_die ());
28275 limbo_die_node *cu;
28276 while ((cu = limbo_die_list))
28278 limbo_die_list = cu->next;
28279 cu->next = cu_die_list;
28280 cu_die_list = cu;
28284 /* The early debug phase is now finished. */
28285 early_dwarf_finished = true;
28288 /* Reset all state within dwarf2out.c so that we can rerun the compiler
28289 within the same process. For use by toplev::finalize. */
28291 void
28292 dwarf2out_c_finalize (void)
28294 last_var_location_insn = NULL;
28295 cached_next_real_insn = NULL;
28296 used_rtx_array = NULL;
28297 incomplete_types = NULL;
28298 decl_scope_table = NULL;
28299 debug_info_section = NULL;
28300 debug_skeleton_info_section = NULL;
28301 debug_abbrev_section = NULL;
28302 debug_skeleton_abbrev_section = NULL;
28303 debug_aranges_section = NULL;
28304 debug_addr_section = NULL;
28305 debug_macinfo_section = NULL;
28306 debug_line_section = NULL;
28307 debug_skeleton_line_section = NULL;
28308 debug_loc_section = NULL;
28309 debug_pubnames_section = NULL;
28310 debug_pubtypes_section = NULL;
28311 debug_str_section = NULL;
28312 debug_str_dwo_section = NULL;
28313 debug_str_offsets_section = NULL;
28314 debug_ranges_section = NULL;
28315 debug_frame_section = NULL;
28316 fde_vec = NULL;
28317 debug_str_hash = NULL;
28318 skeleton_debug_str_hash = NULL;
28319 dw2_string_counter = 0;
28320 have_multiple_function_sections = false;
28321 text_section_used = false;
28322 cold_text_section_used = false;
28323 cold_text_section = NULL;
28324 current_unit_personality = NULL;
28326 next_die_offset = 0;
28327 single_comp_unit_die = NULL;
28328 comdat_type_list = NULL;
28329 limbo_die_list = NULL;
28330 file_table = NULL;
28331 decl_die_table = NULL;
28332 common_block_die_table = NULL;
28333 decl_loc_table = NULL;
28334 call_arg_locations = NULL;
28335 call_arg_loc_last = NULL;
28336 call_site_count = -1;
28337 tail_call_site_count = -1;
28338 cached_dw_loc_list_table = NULL;
28339 abbrev_die_table = NULL;
28340 abbrev_die_table_allocated = 0;
28341 abbrev_die_table_in_use = 0;
28342 delete dwarf_proc_stack_usage_map;
28343 dwarf_proc_stack_usage_map = NULL;
28344 line_info_label_num = 0;
28345 cur_line_info_table = NULL;
28346 text_section_line_info = NULL;
28347 cold_text_section_line_info = NULL;
28348 separate_line_info = NULL;
28349 info_section_emitted = false;
28350 pubname_table = NULL;
28351 pubtype_table = NULL;
28352 macinfo_table = NULL;
28353 ranges_table = NULL;
28354 ranges_table_allocated = 0;
28355 ranges_table_in_use = 0;
28356 ranges_by_label = 0;
28357 ranges_by_label_allocated = 0;
28358 ranges_by_label_in_use = 0;
28359 have_location_lists = false;
28360 loclabel_num = 0;
28361 poc_label_num = 0;
28362 last_emitted_file = NULL;
28363 label_num = 0;
28364 tmpl_value_parm_die_table = NULL;
28365 generic_type_instances = NULL;
28366 frame_pointer_fb_offset = 0;
28367 frame_pointer_fb_offset_valid = false;
28368 base_types.release ();
28369 XDELETEVEC (producer_string);
28370 producer_string = NULL;
28373 #include "gt-dwarf2out.h"