runtime/internal/atomic: new package, API copied from Go 1.7
[official-gcc.git] / gcc / dwarf2out.c
blob11e011399afb686cbf6d2f6846ca9988376cb510
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 GTY(()) section *debug_line_section;
163 static GTY(()) section *debug_skeleton_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_str_dwo_section;
169 static GTY(()) section *debug_str_offsets_section;
170 static GTY(()) section *debug_ranges_section;
171 static GTY(()) section *debug_frame_section;
173 /* Maximum size (in bytes) of an artificially generated label. */
174 #define MAX_ARTIFICIAL_LABEL_BYTES 30
176 /* According to the (draft) DWARF 3 specification, the initial length
177 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
178 bytes are 0xffffffff, followed by the length stored in the next 8
179 bytes.
181 However, the SGI/MIPS ABI uses an initial length which is equal to
182 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
184 #ifndef DWARF_INITIAL_LENGTH_SIZE
185 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
186 #endif
188 /* Round SIZE up to the nearest BOUNDARY. */
189 #define DWARF_ROUND(SIZE,BOUNDARY) \
190 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
192 /* CIE identifier. */
193 #if HOST_BITS_PER_WIDE_INT >= 64
194 #define DWARF_CIE_ID \
195 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
196 #else
197 #define DWARF_CIE_ID DW_CIE_ID
198 #endif
201 /* A vector for a table that contains frame description
202 information for each routine. */
203 #define NOT_INDEXED (-1U)
204 #define NO_INDEX_ASSIGNED (-2U)
206 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
208 struct GTY((for_user)) indirect_string_node {
209 const char *str;
210 unsigned int refcount;
211 enum dwarf_form form;
212 char *label;
213 unsigned int index;
216 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
218 typedef const char *compare_type;
220 static hashval_t hash (indirect_string_node *);
221 static bool equal (indirect_string_node *, const char *);
224 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
226 /* With split_debug_info, both the comp_dir and dwo_name go in the
227 main object file, rather than the dwo, similar to the force_direct
228 parameter elsewhere but with additional complications:
230 1) The string is needed in both the main object file and the dwo.
231 That is, the comp_dir and dwo_name will appear in both places.
233 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
234 DW_FORM_GNU_str_index.
236 3) GCC chooses the form to use late, depending on the size and
237 reference count.
239 Rather than forcing the all debug string handling functions and
240 callers to deal with these complications, simply use a separate,
241 special-cased string table for any attribute that should go in the
242 main object file. This limits the complexity to just the places
243 that need it. */
245 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
247 static GTY(()) int dw2_string_counter;
249 /* True if the compilation unit places functions in more than one section. */
250 static GTY(()) bool have_multiple_function_sections = false;
252 /* Whether the default text and cold text sections have been used at all. */
254 static GTY(()) bool text_section_used = false;
255 static GTY(()) bool cold_text_section_used = false;
257 /* The default cold text section. */
258 static GTY(()) section *cold_text_section;
260 /* The DIE for C++14 'auto' in a function return type. */
261 static GTY(()) dw_die_ref auto_die;
263 /* The DIE for C++14 'decltype(auto)' in a function return type. */
264 static GTY(()) dw_die_ref decltype_auto_die;
266 /* Forward declarations for functions defined in this file. */
268 static char *stripattributes (const char *);
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 /* Return a pointer to a copy of the section string name S with all
414 attributes stripped off, and an asterisk prepended (for assemble_name). */
416 static inline char *
417 stripattributes (const char *s)
419 char *stripped = XNEWVEC (char, strlen (s) + 2);
420 char *p = stripped;
422 *p++ = '*';
424 while (*s && *s != ',')
425 *p++ = *s++;
427 *p = '\0';
428 return stripped;
431 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
432 switch to the data section instead, and write out a synthetic start label
433 for collect2 the first time around. */
435 static void
436 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
438 if (eh_frame_section == 0)
440 int flags;
442 if (EH_TABLES_CAN_BE_READ_ONLY)
444 int fde_encoding;
445 int per_encoding;
446 int lsda_encoding;
448 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
449 /*global=*/0);
450 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
451 /*global=*/1);
452 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
453 /*global=*/0);
454 flags = ((! flag_pic
455 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
456 && (fde_encoding & 0x70) != DW_EH_PE_aligned
457 && (per_encoding & 0x70) != DW_EH_PE_absptr
458 && (per_encoding & 0x70) != DW_EH_PE_aligned
459 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
460 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
461 ? 0 : SECTION_WRITE);
463 else
464 flags = SECTION_WRITE;
466 #ifdef EH_FRAME_SECTION_NAME
467 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
468 #else
469 eh_frame_section = ((flags == SECTION_WRITE)
470 ? data_section : readonly_data_section);
471 #endif /* EH_FRAME_SECTION_NAME */
474 switch_to_section (eh_frame_section);
476 #ifdef EH_FRAME_THROUGH_COLLECT2
477 /* We have no special eh_frame section. Emit special labels to guide
478 collect2. */
479 if (!back)
481 tree label = get_file_function_name ("F");
482 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
483 targetm.asm_out.globalize_label (asm_out_file,
484 IDENTIFIER_POINTER (label));
485 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
487 #endif
490 /* Switch [BACK] to the eh or debug frame table section, depending on
491 FOR_EH. */
493 static void
494 switch_to_frame_table_section (int for_eh, bool back)
496 if (for_eh)
497 switch_to_eh_frame_section (back);
498 else
500 if (!debug_frame_section)
501 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
502 SECTION_DEBUG, NULL);
503 switch_to_section (debug_frame_section);
507 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
509 enum dw_cfi_oprnd_type
510 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
512 switch (cfi)
514 case DW_CFA_nop:
515 case DW_CFA_GNU_window_save:
516 case DW_CFA_remember_state:
517 case DW_CFA_restore_state:
518 return dw_cfi_oprnd_unused;
520 case DW_CFA_set_loc:
521 case DW_CFA_advance_loc1:
522 case DW_CFA_advance_loc2:
523 case DW_CFA_advance_loc4:
524 case DW_CFA_MIPS_advance_loc8:
525 return dw_cfi_oprnd_addr;
527 case DW_CFA_offset:
528 case DW_CFA_offset_extended:
529 case DW_CFA_def_cfa:
530 case DW_CFA_offset_extended_sf:
531 case DW_CFA_def_cfa_sf:
532 case DW_CFA_restore:
533 case DW_CFA_restore_extended:
534 case DW_CFA_undefined:
535 case DW_CFA_same_value:
536 case DW_CFA_def_cfa_register:
537 case DW_CFA_register:
538 case DW_CFA_expression:
539 return dw_cfi_oprnd_reg_num;
541 case DW_CFA_def_cfa_offset:
542 case DW_CFA_GNU_args_size:
543 case DW_CFA_def_cfa_offset_sf:
544 return dw_cfi_oprnd_offset;
546 case DW_CFA_def_cfa_expression:
547 return dw_cfi_oprnd_loc;
549 default:
550 gcc_unreachable ();
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
559 switch (cfi)
561 case DW_CFA_def_cfa:
562 case DW_CFA_def_cfa_sf:
563 case DW_CFA_offset:
564 case DW_CFA_offset_extended_sf:
565 case DW_CFA_offset_extended:
566 return dw_cfi_oprnd_offset;
568 case DW_CFA_register:
569 return dw_cfi_oprnd_reg_num;
571 case DW_CFA_expression:
572 return dw_cfi_oprnd_loc;
574 default:
575 return dw_cfi_oprnd_unused;
579 /* Output one FDE. */
581 static void
582 output_fde (dw_fde_ref fde, bool for_eh, bool second,
583 char *section_start_label, int fde_encoding, char *augmentation,
584 bool any_lsda_needed, int lsda_encoding)
586 const char *begin, *end;
587 static unsigned int j;
588 char l1[20], l2[20];
590 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
591 /* empty */ 0);
592 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
593 for_eh + j);
594 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
595 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
596 if (!XCOFF_DEBUGGING_INFO || for_eh)
598 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600 " indicating 64-bit DWARF extension");
601 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602 "FDE Length");
604 ASM_OUTPUT_LABEL (asm_out_file, l1);
606 if (for_eh)
607 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
608 else
609 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
610 debug_frame_section, "FDE CIE offset");
612 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
613 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
615 if (for_eh)
617 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
618 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
619 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
620 "FDE initial location");
621 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
622 end, begin, "FDE address range");
624 else
626 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
627 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
630 if (augmentation[0])
632 if (any_lsda_needed)
634 int size = size_of_encoded_value (lsda_encoding);
636 if (lsda_encoding == DW_EH_PE_aligned)
638 int offset = ( 4 /* Length */
639 + 4 /* CIE offset */
640 + 2 * size_of_encoded_value (fde_encoding)
641 + 1 /* Augmentation size */ );
642 int pad = -offset & (PTR_SIZE - 1);
644 size += pad;
645 gcc_assert (size_of_uleb128 (size) == 1);
648 dw2_asm_output_data_uleb128 (size, "Augmentation size");
650 if (fde->uses_eh_lsda)
652 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
653 fde->funcdef_number);
654 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
655 gen_rtx_SYMBOL_REF (Pmode, l1),
656 false,
657 "Language Specific Data Area");
659 else
661 if (lsda_encoding == DW_EH_PE_aligned)
662 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
663 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
664 "Language Specific Data Area (none)");
667 else
668 dw2_asm_output_data_uleb128 (0, "Augmentation size");
671 /* Loop through the Call Frame Instructions associated with this FDE. */
672 fde->dw_fde_current_label = begin;
674 size_t from, until, i;
676 from = 0;
677 until = vec_safe_length (fde->dw_fde_cfi);
679 if (fde->dw_fde_second_begin == NULL)
681 else if (!second)
682 until = fde->dw_fde_switch_cfi_index;
683 else
684 from = fde->dw_fde_switch_cfi_index;
686 for (i = from; i < until; i++)
687 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
690 /* If we are to emit a ref/link from function bodies to their frame tables,
691 do it now. This is typically performed to make sure that tables
692 associated with functions are dragged with them and not discarded in
693 garbage collecting links. We need to do this on a per function basis to
694 cope with -ffunction-sections. */
696 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
697 /* Switch to the function section, emit the ref to the tables, and
698 switch *back* into the table section. */
699 switch_to_section (function_section (fde->decl));
700 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
701 switch_to_frame_table_section (for_eh, true);
702 #endif
704 /* Pad the FDE out to an address sized boundary. */
705 ASM_OUTPUT_ALIGN (asm_out_file,
706 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
707 ASM_OUTPUT_LABEL (asm_out_file, l2);
709 j += 2;
712 /* Return true if frame description entry FDE is needed for EH. */
714 static bool
715 fde_needed_for_eh_p (dw_fde_ref fde)
717 if (flag_asynchronous_unwind_tables)
718 return true;
720 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
721 return true;
723 if (fde->uses_eh_lsda)
724 return true;
726 /* If exceptions are enabled, we have collected nothrow info. */
727 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
728 return false;
730 return true;
733 /* Output the call frame information used to record information
734 that relates to calculating the frame pointer, and records the
735 location of saved registers. */
737 static void
738 output_call_frame_info (int for_eh)
740 unsigned int i;
741 dw_fde_ref fde;
742 dw_cfi_ref cfi;
743 char l1[20], l2[20], section_start_label[20];
744 bool any_lsda_needed = false;
745 char augmentation[6];
746 int augmentation_size;
747 int fde_encoding = DW_EH_PE_absptr;
748 int per_encoding = DW_EH_PE_absptr;
749 int lsda_encoding = DW_EH_PE_absptr;
750 int return_reg;
751 rtx personality = NULL;
752 int dw_cie_version;
754 /* Don't emit a CIE if there won't be any FDEs. */
755 if (!fde_vec)
756 return;
758 /* Nothing to do if the assembler's doing it all. */
759 if (dwarf2out_do_cfi_asm ())
760 return;
762 /* If we don't have any functions we'll want to unwind out of, don't emit
763 any EH unwind information. If we make FDEs linkonce, we may have to
764 emit an empty label for an FDE that wouldn't otherwise be emitted. We
765 want to avoid having an FDE kept around when the function it refers to
766 is discarded. Example where this matters: a primary function template
767 in C++ requires EH information, an explicit specialization doesn't. */
768 if (for_eh)
770 bool any_eh_needed = false;
772 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
774 if (fde->uses_eh_lsda)
775 any_eh_needed = any_lsda_needed = true;
776 else if (fde_needed_for_eh_p (fde))
777 any_eh_needed = true;
778 else if (TARGET_USES_WEAK_UNWIND_INFO)
779 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
782 if (!any_eh_needed)
783 return;
786 /* We're going to be generating comments, so turn on app. */
787 if (flag_debug_asm)
788 app_enable ();
790 /* Switch to the proper frame section, first time. */
791 switch_to_frame_table_section (for_eh, false);
793 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
796 /* Output the CIE. */
797 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799 if (!XCOFF_DEBUGGING_INFO || for_eh)
801 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802 dw2_asm_output_data (4, 0xffffffff,
803 "Initial length escape value indicating 64-bit DWARF extension");
804 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[20];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1042 current_function_func_begin_label = NULL;
1044 do_frame = dwarf2out_do_frame ();
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, file, 0, true);
1085 #endif
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1174 void
1175 dwarf2out_frame_finish (void)
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1187 /* Note that the current function section is being used for code. */
1189 static void
1190 dwarf2out_note_section_used (void)
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1202 void
1203 dwarf2out_switch_text_section (void)
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1210 if (!in_cold_section_p)
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1216 else
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1222 have_multiple_function_sections = true;
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1242 var_location_switch_text_section ();
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1257 /* Describe an entry into the .debug_addr section. */
1259 enum ate_kind {
1260 ate_kind_rtx,
1261 ate_kind_rtx_dtprel,
1262 ate_kind_label
1265 struct GTY((for_user)) addr_table_entry {
1266 enum ate_kind kind;
1267 unsigned int refcount;
1268 unsigned int index;
1269 union addr_table_entry_struct_union
1271 rtx GTY ((tag ("0"))) rtl;
1272 char * GTY ((tag ("1"))) label;
1274 GTY ((desc ("%1.kind"))) addr;
1277 /* Location lists are ranges + location descriptions for that range,
1278 so you can track variables that are in different places over
1279 their entire life. */
1280 typedef struct GTY(()) dw_loc_list_struct {
1281 dw_loc_list_ref dw_loc_next;
1282 const char *begin; /* Label and addr_entry for start of range */
1283 addr_table_entry *begin_entry;
1284 const char *end; /* Label for end of range */
1285 char *ll_symbol; /* Label for beginning of location list.
1286 Only on head of list */
1287 const char *section; /* Section this loclist is relative to */
1288 dw_loc_descr_ref expr;
1289 hashval_t hash;
1290 /* True if all addresses in this and subsequent lists are known to be
1291 resolved. */
1292 bool resolved_addr;
1293 /* True if this list has been replaced by dw_loc_next. */
1294 bool replaced;
1295 bool emitted;
1296 /* True if the range should be emitted even if begin and end
1297 are the same. */
1298 bool force;
1299 } dw_loc_list_node;
1301 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1302 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1304 /* Convert a DWARF stack opcode into its string name. */
1306 static const char *
1307 dwarf_stack_op_name (unsigned int op)
1309 const char *name = get_DW_OP_name (op);
1311 if (name != NULL)
1312 return name;
1314 return "OP_<unknown>";
1317 /* Return a pointer to a newly allocated location description. Location
1318 descriptions are simple expression terms that can be strung
1319 together to form more complicated location (address) descriptions. */
1321 static inline dw_loc_descr_ref
1322 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1323 unsigned HOST_WIDE_INT oprnd2)
1325 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1327 descr->dw_loc_opc = op;
1328 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1329 descr->dw_loc_oprnd1.val_entry = NULL;
1330 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1331 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1332 descr->dw_loc_oprnd2.val_entry = NULL;
1333 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1335 return descr;
1338 /* Return a pointer to a newly allocated location description for
1339 REG and OFFSET. */
1341 static inline dw_loc_descr_ref
1342 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1344 if (reg <= 31)
1345 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1346 offset, 0);
1347 else
1348 return new_loc_descr (DW_OP_bregx, reg, offset);
1351 /* Add a location description term to a location description expression. */
1353 static inline void
1354 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1356 dw_loc_descr_ref *d;
1358 /* Find the end of the chain. */
1359 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1362 *d = descr;
1365 /* Compare two location operands for exact equality. */
1367 static bool
1368 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1370 if (a->val_class != b->val_class)
1371 return false;
1372 switch (a->val_class)
1374 case dw_val_class_none:
1375 return true;
1376 case dw_val_class_addr:
1377 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1379 case dw_val_class_offset:
1380 case dw_val_class_unsigned_const:
1381 case dw_val_class_const:
1382 case dw_val_class_range_list:
1383 case dw_val_class_lineptr:
1384 case dw_val_class_macptr:
1385 /* These are all HOST_WIDE_INT, signed or unsigned. */
1386 return a->v.val_unsigned == b->v.val_unsigned;
1388 case dw_val_class_loc:
1389 return a->v.val_loc == b->v.val_loc;
1390 case dw_val_class_loc_list:
1391 return a->v.val_loc_list == b->v.val_loc_list;
1392 case dw_val_class_die_ref:
1393 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1394 case dw_val_class_fde_ref:
1395 return a->v.val_fde_index == b->v.val_fde_index;
1396 case dw_val_class_lbl_id:
1397 case dw_val_class_high_pc:
1398 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1399 case dw_val_class_str:
1400 return a->v.val_str == b->v.val_str;
1401 case dw_val_class_flag:
1402 return a->v.val_flag == b->v.val_flag;
1403 case dw_val_class_file:
1404 return a->v.val_file == b->v.val_file;
1405 case dw_val_class_decl_ref:
1406 return a->v.val_decl_ref == b->v.val_decl_ref;
1408 case dw_val_class_const_double:
1409 return (a->v.val_double.high == b->v.val_double.high
1410 && a->v.val_double.low == b->v.val_double.low);
1412 case dw_val_class_wide_int:
1413 return *a->v.val_wide == *b->v.val_wide;
1415 case dw_val_class_vec:
1417 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1418 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1420 return (a_len == b_len
1421 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1424 case dw_val_class_data8:
1425 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1427 case dw_val_class_vms_delta:
1428 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1429 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1431 case dw_val_class_discr_value:
1432 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1433 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1434 case dw_val_class_discr_list:
1435 /* It makes no sense comparing two discriminant value lists. */
1436 return false;
1438 gcc_unreachable ();
1441 /* Compare two location atoms for exact equality. */
1443 static bool
1444 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1446 if (a->dw_loc_opc != b->dw_loc_opc)
1447 return false;
1449 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1450 address size, but since we always allocate cleared storage it
1451 should be zero for other types of locations. */
1452 if (a->dtprel != b->dtprel)
1453 return false;
1455 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1456 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1459 /* Compare two complete location expressions for exact equality. */
1461 bool
1462 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1464 while (1)
1466 if (a == b)
1467 return true;
1468 if (a == NULL || b == NULL)
1469 return false;
1470 if (!loc_descr_equal_p_1 (a, b))
1471 return false;
1473 a = a->dw_loc_next;
1474 b = b->dw_loc_next;
1479 /* Add a constant OFFSET to a location expression. */
1481 static void
1482 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1484 dw_loc_descr_ref loc;
1485 HOST_WIDE_INT *p;
1487 gcc_assert (*list_head != NULL);
1489 if (!offset)
1490 return;
1492 /* Find the end of the chain. */
1493 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1496 p = NULL;
1497 if (loc->dw_loc_opc == DW_OP_fbreg
1498 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1499 p = &loc->dw_loc_oprnd1.v.val_int;
1500 else if (loc->dw_loc_opc == DW_OP_bregx)
1501 p = &loc->dw_loc_oprnd2.v.val_int;
1503 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1504 offset. Don't optimize if an signed integer overflow would happen. */
1505 if (p != NULL
1506 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1507 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1508 *p += offset;
1510 else if (offset > 0)
1511 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1513 else
1515 loc->dw_loc_next = int_loc_descriptor (-offset);
1516 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1520 /* Add a constant OFFSET to a location list. */
1522 static void
1523 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1525 dw_loc_list_ref d;
1526 for (d = list_head; d != NULL; d = d->dw_loc_next)
1527 loc_descr_plus_const (&d->expr, offset);
1530 #define DWARF_REF_SIZE \
1531 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1533 static unsigned long int get_base_type_offset (dw_die_ref);
1535 /* Return the size of a location descriptor. */
1537 static unsigned long
1538 size_of_loc_descr (dw_loc_descr_ref loc)
1540 unsigned long size = 1;
1542 switch (loc->dw_loc_opc)
1544 case DW_OP_addr:
1545 size += DWARF2_ADDR_SIZE;
1546 break;
1547 case DW_OP_GNU_addr_index:
1548 case DW_OP_GNU_const_index:
1549 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1550 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1551 break;
1552 case DW_OP_const1u:
1553 case DW_OP_const1s:
1554 size += 1;
1555 break;
1556 case DW_OP_const2u:
1557 case DW_OP_const2s:
1558 size += 2;
1559 break;
1560 case DW_OP_const4u:
1561 case DW_OP_const4s:
1562 size += 4;
1563 break;
1564 case DW_OP_const8u:
1565 case DW_OP_const8s:
1566 size += 8;
1567 break;
1568 case DW_OP_constu:
1569 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1570 break;
1571 case DW_OP_consts:
1572 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1573 break;
1574 case DW_OP_pick:
1575 size += 1;
1576 break;
1577 case DW_OP_plus_uconst:
1578 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1579 break;
1580 case DW_OP_skip:
1581 case DW_OP_bra:
1582 size += 2;
1583 break;
1584 case DW_OP_breg0:
1585 case DW_OP_breg1:
1586 case DW_OP_breg2:
1587 case DW_OP_breg3:
1588 case DW_OP_breg4:
1589 case DW_OP_breg5:
1590 case DW_OP_breg6:
1591 case DW_OP_breg7:
1592 case DW_OP_breg8:
1593 case DW_OP_breg9:
1594 case DW_OP_breg10:
1595 case DW_OP_breg11:
1596 case DW_OP_breg12:
1597 case DW_OP_breg13:
1598 case DW_OP_breg14:
1599 case DW_OP_breg15:
1600 case DW_OP_breg16:
1601 case DW_OP_breg17:
1602 case DW_OP_breg18:
1603 case DW_OP_breg19:
1604 case DW_OP_breg20:
1605 case DW_OP_breg21:
1606 case DW_OP_breg22:
1607 case DW_OP_breg23:
1608 case DW_OP_breg24:
1609 case DW_OP_breg25:
1610 case DW_OP_breg26:
1611 case DW_OP_breg27:
1612 case DW_OP_breg28:
1613 case DW_OP_breg29:
1614 case DW_OP_breg30:
1615 case DW_OP_breg31:
1616 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1617 break;
1618 case DW_OP_regx:
1619 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1620 break;
1621 case DW_OP_fbreg:
1622 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1623 break;
1624 case DW_OP_bregx:
1625 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1626 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1627 break;
1628 case DW_OP_piece:
1629 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1630 break;
1631 case DW_OP_bit_piece:
1632 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1633 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1634 break;
1635 case DW_OP_deref_size:
1636 case DW_OP_xderef_size:
1637 size += 1;
1638 break;
1639 case DW_OP_call2:
1640 size += 2;
1641 break;
1642 case DW_OP_call4:
1643 size += 4;
1644 break;
1645 case DW_OP_call_ref:
1646 size += DWARF_REF_SIZE;
1647 break;
1648 case DW_OP_implicit_value:
1649 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1650 + loc->dw_loc_oprnd1.v.val_unsigned;
1651 break;
1652 case DW_OP_GNU_implicit_pointer:
1653 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1654 break;
1655 case DW_OP_GNU_entry_value:
1657 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1658 size += size_of_uleb128 (op_size) + op_size;
1659 break;
1661 case DW_OP_GNU_const_type:
1663 unsigned long o
1664 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1665 size += size_of_uleb128 (o) + 1;
1666 switch (loc->dw_loc_oprnd2.val_class)
1668 case dw_val_class_vec:
1669 size += loc->dw_loc_oprnd2.v.val_vec.length
1670 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1671 break;
1672 case dw_val_class_const:
1673 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1674 break;
1675 case dw_val_class_const_double:
1676 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1677 break;
1678 case dw_val_class_wide_int:
1679 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1680 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1681 break;
1682 default:
1683 gcc_unreachable ();
1685 break;
1687 case DW_OP_GNU_regval_type:
1689 unsigned long o
1690 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1691 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1692 + size_of_uleb128 (o);
1694 break;
1695 case DW_OP_GNU_deref_type:
1697 unsigned long o
1698 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1699 size += 1 + size_of_uleb128 (o);
1701 break;
1702 case DW_OP_GNU_convert:
1703 case DW_OP_GNU_reinterpret:
1704 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1705 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1706 else
1708 unsigned long o
1709 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1710 size += size_of_uleb128 (o);
1712 break;
1713 case DW_OP_GNU_parameter_ref:
1714 size += 4;
1715 break;
1716 default:
1717 break;
1720 return size;
1723 /* Return the size of a series of location descriptors. */
1725 unsigned long
1726 size_of_locs (dw_loc_descr_ref loc)
1728 dw_loc_descr_ref l;
1729 unsigned long size;
1731 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1732 field, to avoid writing to a PCH file. */
1733 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1735 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1736 break;
1737 size += size_of_loc_descr (l);
1739 if (! l)
1740 return size;
1742 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1744 l->dw_loc_addr = size;
1745 size += size_of_loc_descr (l);
1748 return size;
1751 /* Return the size of the value in a DW_AT_discr_value attribute. */
1753 static int
1754 size_of_discr_value (dw_discr_value *discr_value)
1756 if (discr_value->pos)
1757 return size_of_uleb128 (discr_value->v.uval);
1758 else
1759 return size_of_sleb128 (discr_value->v.sval);
1762 /* Return the size of the value in a DW_discr_list attribute. */
1764 static int
1765 size_of_discr_list (dw_discr_list_ref discr_list)
1767 int size = 0;
1769 for (dw_discr_list_ref list = discr_list;
1770 list != NULL;
1771 list = list->dw_discr_next)
1773 /* One byte for the discriminant value descriptor, and then one or two
1774 LEB128 numbers, depending on whether it's a single case label or a
1775 range label. */
1776 size += 1;
1777 size += size_of_discr_value (&list->dw_discr_lower_bound);
1778 if (list->dw_discr_range != 0)
1779 size += size_of_discr_value (&list->dw_discr_upper_bound);
1781 return size;
1784 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1785 static void get_ref_die_offset_label (char *, dw_die_ref);
1786 static unsigned long int get_ref_die_offset (dw_die_ref);
1788 /* Output location description stack opcode's operands (if any).
1789 The for_eh_or_skip parameter controls whether register numbers are
1790 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1791 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1792 info). This should be suppressed for the cases that have not been converted
1793 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1795 static void
1796 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1798 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1799 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1801 switch (loc->dw_loc_opc)
1803 #ifdef DWARF2_DEBUGGING_INFO
1804 case DW_OP_const2u:
1805 case DW_OP_const2s:
1806 dw2_asm_output_data (2, val1->v.val_int, NULL);
1807 break;
1808 case DW_OP_const4u:
1809 if (loc->dtprel)
1811 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1812 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1813 val1->v.val_addr);
1814 fputc ('\n', asm_out_file);
1815 break;
1817 /* FALLTHRU */
1818 case DW_OP_const4s:
1819 dw2_asm_output_data (4, val1->v.val_int, NULL);
1820 break;
1821 case DW_OP_const8u:
1822 if (loc->dtprel)
1824 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1825 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1826 val1->v.val_addr);
1827 fputc ('\n', asm_out_file);
1828 break;
1830 /* FALLTHRU */
1831 case DW_OP_const8s:
1832 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1833 dw2_asm_output_data (8, val1->v.val_int, NULL);
1834 break;
1835 case DW_OP_skip:
1836 case DW_OP_bra:
1838 int offset;
1840 gcc_assert (val1->val_class == dw_val_class_loc);
1841 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1843 dw2_asm_output_data (2, offset, NULL);
1845 break;
1846 case DW_OP_implicit_value:
1847 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1848 switch (val2->val_class)
1850 case dw_val_class_const:
1851 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1852 break;
1853 case dw_val_class_vec:
1855 unsigned int elt_size = val2->v.val_vec.elt_size;
1856 unsigned int len = val2->v.val_vec.length;
1857 unsigned int i;
1858 unsigned char *p;
1860 if (elt_size > sizeof (HOST_WIDE_INT))
1862 elt_size /= 2;
1863 len *= 2;
1865 for (i = 0, p = val2->v.val_vec.array;
1866 i < len;
1867 i++, p += elt_size)
1868 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1869 "fp or vector constant word %u", i);
1871 break;
1872 case dw_val_class_const_double:
1874 unsigned HOST_WIDE_INT first, second;
1876 if (WORDS_BIG_ENDIAN)
1878 first = val2->v.val_double.high;
1879 second = val2->v.val_double.low;
1881 else
1883 first = val2->v.val_double.low;
1884 second = val2->v.val_double.high;
1886 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1887 first, NULL);
1888 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1889 second, NULL);
1891 break;
1892 case dw_val_class_wide_int:
1894 int i;
1895 int len = get_full_len (*val2->v.val_wide);
1896 if (WORDS_BIG_ENDIAN)
1897 for (i = len - 1; i >= 0; --i)
1898 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1899 val2->v.val_wide->elt (i), NULL);
1900 else
1901 for (i = 0; i < len; ++i)
1902 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1903 val2->v.val_wide->elt (i), NULL);
1905 break;
1906 case dw_val_class_addr:
1907 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1908 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1909 break;
1910 default:
1911 gcc_unreachable ();
1913 break;
1914 #else
1915 case DW_OP_const2u:
1916 case DW_OP_const2s:
1917 case DW_OP_const4u:
1918 case DW_OP_const4s:
1919 case DW_OP_const8u:
1920 case DW_OP_const8s:
1921 case DW_OP_skip:
1922 case DW_OP_bra:
1923 case DW_OP_implicit_value:
1924 /* We currently don't make any attempt to make sure these are
1925 aligned properly like we do for the main unwind info, so
1926 don't support emitting things larger than a byte if we're
1927 only doing unwinding. */
1928 gcc_unreachable ();
1929 #endif
1930 case DW_OP_const1u:
1931 case DW_OP_const1s:
1932 dw2_asm_output_data (1, val1->v.val_int, NULL);
1933 break;
1934 case DW_OP_constu:
1935 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1936 break;
1937 case DW_OP_consts:
1938 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1939 break;
1940 case DW_OP_pick:
1941 dw2_asm_output_data (1, val1->v.val_int, NULL);
1942 break;
1943 case DW_OP_plus_uconst:
1944 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1945 break;
1946 case DW_OP_breg0:
1947 case DW_OP_breg1:
1948 case DW_OP_breg2:
1949 case DW_OP_breg3:
1950 case DW_OP_breg4:
1951 case DW_OP_breg5:
1952 case DW_OP_breg6:
1953 case DW_OP_breg7:
1954 case DW_OP_breg8:
1955 case DW_OP_breg9:
1956 case DW_OP_breg10:
1957 case DW_OP_breg11:
1958 case DW_OP_breg12:
1959 case DW_OP_breg13:
1960 case DW_OP_breg14:
1961 case DW_OP_breg15:
1962 case DW_OP_breg16:
1963 case DW_OP_breg17:
1964 case DW_OP_breg18:
1965 case DW_OP_breg19:
1966 case DW_OP_breg20:
1967 case DW_OP_breg21:
1968 case DW_OP_breg22:
1969 case DW_OP_breg23:
1970 case DW_OP_breg24:
1971 case DW_OP_breg25:
1972 case DW_OP_breg26:
1973 case DW_OP_breg27:
1974 case DW_OP_breg28:
1975 case DW_OP_breg29:
1976 case DW_OP_breg30:
1977 case DW_OP_breg31:
1978 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1979 break;
1980 case DW_OP_regx:
1982 unsigned r = val1->v.val_unsigned;
1983 if (for_eh_or_skip >= 0)
1984 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1985 gcc_assert (size_of_uleb128 (r)
1986 == size_of_uleb128 (val1->v.val_unsigned));
1987 dw2_asm_output_data_uleb128 (r, NULL);
1989 break;
1990 case DW_OP_fbreg:
1991 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1992 break;
1993 case DW_OP_bregx:
1995 unsigned r = val1->v.val_unsigned;
1996 if (for_eh_or_skip >= 0)
1997 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1998 gcc_assert (size_of_uleb128 (r)
1999 == size_of_uleb128 (val1->v.val_unsigned));
2000 dw2_asm_output_data_uleb128 (r, NULL);
2001 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2003 break;
2004 case DW_OP_piece:
2005 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2006 break;
2007 case DW_OP_bit_piece:
2008 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2009 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2010 break;
2011 case DW_OP_deref_size:
2012 case DW_OP_xderef_size:
2013 dw2_asm_output_data (1, val1->v.val_int, NULL);
2014 break;
2016 case DW_OP_addr:
2017 if (loc->dtprel)
2019 if (targetm.asm_out.output_dwarf_dtprel)
2021 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2022 DWARF2_ADDR_SIZE,
2023 val1->v.val_addr);
2024 fputc ('\n', asm_out_file);
2026 else
2027 gcc_unreachable ();
2029 else
2031 #ifdef DWARF2_DEBUGGING_INFO
2032 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2033 #else
2034 gcc_unreachable ();
2035 #endif
2037 break;
2039 case DW_OP_GNU_addr_index:
2040 case DW_OP_GNU_const_index:
2041 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2042 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2043 "(index into .debug_addr)");
2044 break;
2046 case DW_OP_call2:
2047 case DW_OP_call4:
2049 unsigned long die_offset
2050 = get_ref_die_offset (val1->v.val_die_ref.die);
2051 /* Make sure the offset has been computed and that we can encode it as
2052 an operand. */
2053 gcc_assert (die_offset > 0
2054 && die_offset <= (loc->dw_loc_opc == DW_OP_call2)
2055 ? 0xffff
2056 : 0xffffffff);
2057 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2058 die_offset, NULL);
2060 break;
2062 case DW_OP_GNU_implicit_pointer:
2064 char label[MAX_ARTIFICIAL_LABEL_BYTES
2065 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2066 gcc_assert (val1->val_class == dw_val_class_die_ref);
2067 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2068 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2069 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2071 break;
2073 case DW_OP_GNU_entry_value:
2074 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2075 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2076 break;
2078 case DW_OP_GNU_const_type:
2080 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2081 gcc_assert (o);
2082 dw2_asm_output_data_uleb128 (o, NULL);
2083 switch (val2->val_class)
2085 case dw_val_class_const:
2086 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2087 dw2_asm_output_data (1, l, NULL);
2088 dw2_asm_output_data (l, val2->v.val_int, NULL);
2089 break;
2090 case dw_val_class_vec:
2092 unsigned int elt_size = val2->v.val_vec.elt_size;
2093 unsigned int len = val2->v.val_vec.length;
2094 unsigned int i;
2095 unsigned char *p;
2097 l = len * elt_size;
2098 dw2_asm_output_data (1, l, NULL);
2099 if (elt_size > sizeof (HOST_WIDE_INT))
2101 elt_size /= 2;
2102 len *= 2;
2104 for (i = 0, p = val2->v.val_vec.array;
2105 i < len;
2106 i++, p += elt_size)
2107 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2108 "fp or vector constant word %u", i);
2110 break;
2111 case dw_val_class_const_double:
2113 unsigned HOST_WIDE_INT first, second;
2114 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2116 dw2_asm_output_data (1, 2 * l, NULL);
2117 if (WORDS_BIG_ENDIAN)
2119 first = val2->v.val_double.high;
2120 second = val2->v.val_double.low;
2122 else
2124 first = val2->v.val_double.low;
2125 second = val2->v.val_double.high;
2127 dw2_asm_output_data (l, first, NULL);
2128 dw2_asm_output_data (l, second, NULL);
2130 break;
2131 case dw_val_class_wide_int:
2133 int i;
2134 int len = get_full_len (*val2->v.val_wide);
2135 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2137 dw2_asm_output_data (1, len * l, NULL);
2138 if (WORDS_BIG_ENDIAN)
2139 for (i = len - 1; i >= 0; --i)
2140 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2141 else
2142 for (i = 0; i < len; ++i)
2143 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2145 break;
2146 default:
2147 gcc_unreachable ();
2150 break;
2151 case DW_OP_GNU_regval_type:
2153 unsigned r = val1->v.val_unsigned;
2154 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2155 gcc_assert (o);
2156 if (for_eh_or_skip >= 0)
2158 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2159 gcc_assert (size_of_uleb128 (r)
2160 == size_of_uleb128 (val1->v.val_unsigned));
2162 dw2_asm_output_data_uleb128 (r, NULL);
2163 dw2_asm_output_data_uleb128 (o, NULL);
2165 break;
2166 case DW_OP_GNU_deref_type:
2168 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2169 gcc_assert (o);
2170 dw2_asm_output_data (1, val1->v.val_int, NULL);
2171 dw2_asm_output_data_uleb128 (o, NULL);
2173 break;
2174 case DW_OP_GNU_convert:
2175 case DW_OP_GNU_reinterpret:
2176 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2177 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2178 else
2180 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2181 gcc_assert (o);
2182 dw2_asm_output_data_uleb128 (o, NULL);
2184 break;
2186 case DW_OP_GNU_parameter_ref:
2188 unsigned long o;
2189 gcc_assert (val1->val_class == dw_val_class_die_ref);
2190 o = get_ref_die_offset (val1->v.val_die_ref.die);
2191 dw2_asm_output_data (4, o, NULL);
2193 break;
2195 default:
2196 /* Other codes have no operands. */
2197 break;
2201 /* Output a sequence of location operations.
2202 The for_eh_or_skip parameter controls whether register numbers are
2203 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2204 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2205 info). This should be suppressed for the cases that have not been converted
2206 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2208 void
2209 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2211 for (; loc != NULL; loc = loc->dw_loc_next)
2213 enum dwarf_location_atom opc = loc->dw_loc_opc;
2214 /* Output the opcode. */
2215 if (for_eh_or_skip >= 0
2216 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2218 unsigned r = (opc - DW_OP_breg0);
2219 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2220 gcc_assert (r <= 31);
2221 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2223 else if (for_eh_or_skip >= 0
2224 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2226 unsigned r = (opc - DW_OP_reg0);
2227 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2228 gcc_assert (r <= 31);
2229 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2232 dw2_asm_output_data (1, opc,
2233 "%s", dwarf_stack_op_name (opc));
2235 /* Output the operand(s) (if any). */
2236 output_loc_operands (loc, for_eh_or_skip);
2240 /* Output location description stack opcode's operands (if any).
2241 The output is single bytes on a line, suitable for .cfi_escape. */
2243 static void
2244 output_loc_operands_raw (dw_loc_descr_ref loc)
2246 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2247 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2249 switch (loc->dw_loc_opc)
2251 case DW_OP_addr:
2252 case DW_OP_GNU_addr_index:
2253 case DW_OP_GNU_const_index:
2254 case DW_OP_implicit_value:
2255 /* We cannot output addresses in .cfi_escape, only bytes. */
2256 gcc_unreachable ();
2258 case DW_OP_const1u:
2259 case DW_OP_const1s:
2260 case DW_OP_pick:
2261 case DW_OP_deref_size:
2262 case DW_OP_xderef_size:
2263 fputc (',', asm_out_file);
2264 dw2_asm_output_data_raw (1, val1->v.val_int);
2265 break;
2267 case DW_OP_const2u:
2268 case DW_OP_const2s:
2269 fputc (',', asm_out_file);
2270 dw2_asm_output_data_raw (2, val1->v.val_int);
2271 break;
2273 case DW_OP_const4u:
2274 case DW_OP_const4s:
2275 fputc (',', asm_out_file);
2276 dw2_asm_output_data_raw (4, val1->v.val_int);
2277 break;
2279 case DW_OP_const8u:
2280 case DW_OP_const8s:
2281 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2282 fputc (',', asm_out_file);
2283 dw2_asm_output_data_raw (8, val1->v.val_int);
2284 break;
2286 case DW_OP_skip:
2287 case DW_OP_bra:
2289 int offset;
2291 gcc_assert (val1->val_class == dw_val_class_loc);
2292 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2294 fputc (',', asm_out_file);
2295 dw2_asm_output_data_raw (2, offset);
2297 break;
2299 case DW_OP_regx:
2301 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2302 gcc_assert (size_of_uleb128 (r)
2303 == size_of_uleb128 (val1->v.val_unsigned));
2304 fputc (',', asm_out_file);
2305 dw2_asm_output_data_uleb128_raw (r);
2307 break;
2309 case DW_OP_constu:
2310 case DW_OP_plus_uconst:
2311 case DW_OP_piece:
2312 fputc (',', asm_out_file);
2313 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2314 break;
2316 case DW_OP_bit_piece:
2317 fputc (',', asm_out_file);
2318 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2319 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2320 break;
2322 case DW_OP_consts:
2323 case DW_OP_breg0:
2324 case DW_OP_breg1:
2325 case DW_OP_breg2:
2326 case DW_OP_breg3:
2327 case DW_OP_breg4:
2328 case DW_OP_breg5:
2329 case DW_OP_breg6:
2330 case DW_OP_breg7:
2331 case DW_OP_breg8:
2332 case DW_OP_breg9:
2333 case DW_OP_breg10:
2334 case DW_OP_breg11:
2335 case DW_OP_breg12:
2336 case DW_OP_breg13:
2337 case DW_OP_breg14:
2338 case DW_OP_breg15:
2339 case DW_OP_breg16:
2340 case DW_OP_breg17:
2341 case DW_OP_breg18:
2342 case DW_OP_breg19:
2343 case DW_OP_breg20:
2344 case DW_OP_breg21:
2345 case DW_OP_breg22:
2346 case DW_OP_breg23:
2347 case DW_OP_breg24:
2348 case DW_OP_breg25:
2349 case DW_OP_breg26:
2350 case DW_OP_breg27:
2351 case DW_OP_breg28:
2352 case DW_OP_breg29:
2353 case DW_OP_breg30:
2354 case DW_OP_breg31:
2355 case DW_OP_fbreg:
2356 fputc (',', asm_out_file);
2357 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2358 break;
2360 case DW_OP_bregx:
2362 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2363 gcc_assert (size_of_uleb128 (r)
2364 == size_of_uleb128 (val1->v.val_unsigned));
2365 fputc (',', asm_out_file);
2366 dw2_asm_output_data_uleb128_raw (r);
2367 fputc (',', asm_out_file);
2368 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2370 break;
2372 case DW_OP_GNU_implicit_pointer:
2373 case DW_OP_GNU_entry_value:
2374 case DW_OP_GNU_const_type:
2375 case DW_OP_GNU_regval_type:
2376 case DW_OP_GNU_deref_type:
2377 case DW_OP_GNU_convert:
2378 case DW_OP_GNU_reinterpret:
2379 case DW_OP_GNU_parameter_ref:
2380 gcc_unreachable ();
2381 break;
2383 default:
2384 /* Other codes have no operands. */
2385 break;
2389 void
2390 output_loc_sequence_raw (dw_loc_descr_ref loc)
2392 while (1)
2394 enum dwarf_location_atom opc = loc->dw_loc_opc;
2395 /* Output the opcode. */
2396 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2398 unsigned r = (opc - DW_OP_breg0);
2399 r = DWARF2_FRAME_REG_OUT (r, 1);
2400 gcc_assert (r <= 31);
2401 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2403 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2405 unsigned r = (opc - DW_OP_reg0);
2406 r = DWARF2_FRAME_REG_OUT (r, 1);
2407 gcc_assert (r <= 31);
2408 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2410 /* Output the opcode. */
2411 fprintf (asm_out_file, "%#x", opc);
2412 output_loc_operands_raw (loc);
2414 if (!loc->dw_loc_next)
2415 break;
2416 loc = loc->dw_loc_next;
2418 fputc (',', asm_out_file);
2422 /* This function builds a dwarf location descriptor sequence from a
2423 dw_cfa_location, adding the given OFFSET to the result of the
2424 expression. */
2426 struct dw_loc_descr_node *
2427 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2429 struct dw_loc_descr_node *head, *tmp;
2431 offset += cfa->offset;
2433 if (cfa->indirect)
2435 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2436 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2437 head->dw_loc_oprnd1.val_entry = NULL;
2438 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2439 add_loc_descr (&head, tmp);
2440 if (offset != 0)
2442 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2443 add_loc_descr (&head, tmp);
2446 else
2447 head = new_reg_loc_descr (cfa->reg, offset);
2449 return head;
2452 /* This function builds a dwarf location descriptor sequence for
2453 the address at OFFSET from the CFA when stack is aligned to
2454 ALIGNMENT byte. */
2456 struct dw_loc_descr_node *
2457 build_cfa_aligned_loc (dw_cfa_location *cfa,
2458 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2460 struct dw_loc_descr_node *head;
2461 unsigned int dwarf_fp
2462 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2464 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2465 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2467 head = new_reg_loc_descr (dwarf_fp, 0);
2468 add_loc_descr (&head, int_loc_descriptor (alignment));
2469 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2470 loc_descr_plus_const (&head, offset);
2472 else
2473 head = new_reg_loc_descr (dwarf_fp, offset);
2474 return head;
2477 /* And now, the support for symbolic debugging information. */
2479 /* .debug_str support. */
2481 static void dwarf2out_init (const char *);
2482 static void dwarf2out_finish (const char *);
2483 static void dwarf2out_early_finish (void);
2484 static void dwarf2out_assembly_start (void);
2485 static void dwarf2out_define (unsigned int, const char *);
2486 static void dwarf2out_undef (unsigned int, const char *);
2487 static void dwarf2out_start_source_file (unsigned, const char *);
2488 static void dwarf2out_end_source_file (unsigned);
2489 static void dwarf2out_function_decl (tree);
2490 static void dwarf2out_begin_block (unsigned, unsigned);
2491 static void dwarf2out_end_block (unsigned, unsigned);
2492 static bool dwarf2out_ignore_block (const_tree);
2493 static void dwarf2out_early_global_decl (tree);
2494 static void dwarf2out_late_global_decl (tree);
2495 static void dwarf2out_type_decl (tree, int);
2496 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2497 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2498 dw_die_ref);
2499 static void dwarf2out_abstract_function (tree);
2500 static void dwarf2out_var_location (rtx_insn *);
2501 static void dwarf2out_size_function (tree);
2502 static void dwarf2out_begin_function (tree);
2503 static void dwarf2out_end_function (unsigned int);
2504 static void dwarf2out_register_main_translation_unit (tree unit);
2505 static void dwarf2out_set_name (tree, tree);
2507 /* The debug hooks structure. */
2509 const struct gcc_debug_hooks dwarf2_debug_hooks =
2511 dwarf2out_init,
2512 dwarf2out_finish,
2513 dwarf2out_early_finish,
2514 dwarf2out_assembly_start,
2515 dwarf2out_define,
2516 dwarf2out_undef,
2517 dwarf2out_start_source_file,
2518 dwarf2out_end_source_file,
2519 dwarf2out_begin_block,
2520 dwarf2out_end_block,
2521 dwarf2out_ignore_block,
2522 dwarf2out_source_line,
2523 dwarf2out_begin_prologue,
2524 #if VMS_DEBUGGING_INFO
2525 dwarf2out_vms_end_prologue,
2526 dwarf2out_vms_begin_epilogue,
2527 #else
2528 debug_nothing_int_charstar,
2529 debug_nothing_int_charstar,
2530 #endif
2531 dwarf2out_end_epilogue,
2532 dwarf2out_begin_function,
2533 dwarf2out_end_function, /* end_function */
2534 dwarf2out_register_main_translation_unit,
2535 dwarf2out_function_decl, /* function_decl */
2536 dwarf2out_early_global_decl,
2537 dwarf2out_late_global_decl,
2538 dwarf2out_type_decl, /* type_decl */
2539 dwarf2out_imported_module_or_decl,
2540 debug_nothing_tree, /* deferred_inline_function */
2541 /* The DWARF 2 backend tries to reduce debugging bloat by not
2542 emitting the abstract description of inline functions until
2543 something tries to reference them. */
2544 dwarf2out_abstract_function, /* outlining_inline_function */
2545 debug_nothing_rtx_code_label, /* label */
2546 debug_nothing_int, /* handle_pch */
2547 dwarf2out_var_location,
2548 dwarf2out_size_function, /* size_function */
2549 dwarf2out_switch_text_section,
2550 dwarf2out_set_name,
2551 1, /* start_end_main_source_file */
2552 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2555 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2557 dwarf2out_init,
2558 debug_nothing_charstar,
2559 debug_nothing_void,
2560 debug_nothing_void,
2561 debug_nothing_int_charstar,
2562 debug_nothing_int_charstar,
2563 debug_nothing_int_charstar,
2564 debug_nothing_int,
2565 debug_nothing_int_int, /* begin_block */
2566 debug_nothing_int_int, /* end_block */
2567 debug_true_const_tree, /* ignore_block */
2568 dwarf2out_source_line, /* source_line */
2569 debug_nothing_int_charstar, /* begin_prologue */
2570 debug_nothing_int_charstar, /* end_prologue */
2571 debug_nothing_int_charstar, /* begin_epilogue */
2572 debug_nothing_int_charstar, /* end_epilogue */
2573 debug_nothing_tree, /* begin_function */
2574 debug_nothing_int, /* end_function */
2575 debug_nothing_tree, /* register_main_translation_unit */
2576 debug_nothing_tree, /* function_decl */
2577 debug_nothing_tree, /* early_global_decl */
2578 debug_nothing_tree, /* late_global_decl */
2579 debug_nothing_tree_int, /* type_decl */
2580 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2581 debug_nothing_tree, /* deferred_inline_function */
2582 debug_nothing_tree, /* outlining_inline_function */
2583 debug_nothing_rtx_code_label, /* label */
2584 debug_nothing_int, /* handle_pch */
2585 debug_nothing_rtx_insn, /* var_location */
2586 debug_nothing_tree, /* size_function */
2587 debug_nothing_void, /* switch_text_section */
2588 debug_nothing_tree_tree, /* set_name */
2589 0, /* start_end_main_source_file */
2590 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2593 /* NOTE: In the comments in this file, many references are made to
2594 "Debugging Information Entries". This term is abbreviated as `DIE'
2595 throughout the remainder of this file. */
2597 /* An internal representation of the DWARF output is built, and then
2598 walked to generate the DWARF debugging info. The walk of the internal
2599 representation is done after the entire program has been compiled.
2600 The types below are used to describe the internal representation. */
2602 /* Whether to put type DIEs into their own section .debug_types instead
2603 of making them part of the .debug_info section. Only supported for
2604 Dwarf V4 or higher and the user didn't disable them through
2605 -fno-debug-types-section. It is more efficient to put them in a
2606 separate comdat sections since the linker will then be able to
2607 remove duplicates. But not all tools support .debug_types sections
2608 yet. */
2610 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2612 /* Various DIE's use offsets relative to the beginning of the
2613 .debug_info section to refer to each other. */
2615 typedef long int dw_offset;
2617 struct comdat_type_node;
2619 /* The entries in the line_info table more-or-less mirror the opcodes
2620 that are used in the real dwarf line table. Arrays of these entries
2621 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2622 supported. */
2624 enum dw_line_info_opcode {
2625 /* Emit DW_LNE_set_address; the operand is the label index. */
2626 LI_set_address,
2628 /* Emit a row to the matrix with the given line. This may be done
2629 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2630 special opcodes. */
2631 LI_set_line,
2633 /* Emit a DW_LNS_set_file. */
2634 LI_set_file,
2636 /* Emit a DW_LNS_set_column. */
2637 LI_set_column,
2639 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2640 LI_negate_stmt,
2642 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2643 LI_set_prologue_end,
2644 LI_set_epilogue_begin,
2646 /* Emit a DW_LNE_set_discriminator. */
2647 LI_set_discriminator
2650 typedef struct GTY(()) dw_line_info_struct {
2651 enum dw_line_info_opcode opcode;
2652 unsigned int val;
2653 } dw_line_info_entry;
2656 struct GTY(()) dw_line_info_table {
2657 /* The label that marks the end of this section. */
2658 const char *end_label;
2660 /* The values for the last row of the matrix, as collected in the table.
2661 These are used to minimize the changes to the next row. */
2662 unsigned int file_num;
2663 unsigned int line_num;
2664 unsigned int column_num;
2665 int discrim_num;
2666 bool is_stmt;
2667 bool in_use;
2669 vec<dw_line_info_entry, va_gc> *entries;
2673 /* Each DIE attribute has a field specifying the attribute kind,
2674 a link to the next attribute in the chain, and an attribute value.
2675 Attributes are typically linked below the DIE they modify. */
2677 typedef struct GTY(()) dw_attr_struct {
2678 enum dwarf_attribute dw_attr;
2679 dw_val_node dw_attr_val;
2681 dw_attr_node;
2684 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2685 The children of each node form a circular list linked by
2686 die_sib. die_child points to the node *before* the "first" child node. */
2688 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2689 union die_symbol_or_type_node
2691 const char * GTY ((tag ("0"))) die_symbol;
2692 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2694 GTY ((desc ("%0.comdat_type_p"))) die_id;
2695 vec<dw_attr_node, va_gc> *die_attr;
2696 dw_die_ref die_parent;
2697 dw_die_ref die_child;
2698 dw_die_ref die_sib;
2699 dw_die_ref die_definition; /* ref from a specification to its definition */
2700 dw_offset die_offset;
2701 unsigned long die_abbrev;
2702 int die_mark;
2703 unsigned int decl_id;
2704 enum dwarf_tag die_tag;
2705 /* Die is used and must not be pruned as unused. */
2706 BOOL_BITFIELD die_perennial_p : 1;
2707 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2708 /* Lots of spare bits. */
2710 die_node;
2712 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2713 static bool early_dwarf;
2714 struct set_early_dwarf {
2715 bool saved;
2716 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2717 ~set_early_dwarf () { early_dwarf = saved; }
2720 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2721 #define FOR_EACH_CHILD(die, c, expr) do { \
2722 c = die->die_child; \
2723 if (c) do { \
2724 c = c->die_sib; \
2725 expr; \
2726 } while (c != die->die_child); \
2727 } while (0)
2729 /* The pubname structure */
2731 typedef struct GTY(()) pubname_struct {
2732 dw_die_ref die;
2733 const char *name;
2735 pubname_entry;
2738 struct GTY(()) dw_ranges {
2739 /* If this is positive, it's a block number, otherwise it's a
2740 bitwise-negated index into dw_ranges_by_label. */
2741 int num;
2744 /* A structure to hold a macinfo entry. */
2746 typedef struct GTY(()) macinfo_struct {
2747 unsigned char code;
2748 unsigned HOST_WIDE_INT lineno;
2749 const char *info;
2751 macinfo_entry;
2754 struct GTY(()) dw_ranges_by_label {
2755 const char *begin;
2756 const char *end;
2759 /* The comdat type node structure. */
2760 struct GTY(()) comdat_type_node
2762 dw_die_ref root_die;
2763 dw_die_ref type_die;
2764 dw_die_ref skeleton_die;
2765 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2766 comdat_type_node *next;
2769 /* A list of DIEs for which we can't determine ancestry (parent_die
2770 field) just yet. Later in dwarf2out_finish we will fill in the
2771 missing bits. */
2772 typedef struct GTY(()) limbo_die_struct {
2773 dw_die_ref die;
2774 /* The tree for which this DIE was created. We use this to
2775 determine ancestry later. */
2776 tree created_for;
2777 struct limbo_die_struct *next;
2779 limbo_die_node;
2781 typedef struct skeleton_chain_struct
2783 dw_die_ref old_die;
2784 dw_die_ref new_die;
2785 struct skeleton_chain_struct *parent;
2787 skeleton_chain_node;
2789 /* Define a macro which returns nonzero for a TYPE_DECL which was
2790 implicitly generated for a type.
2792 Note that, unlike the C front-end (which generates a NULL named
2793 TYPE_DECL node for each complete tagged type, each array type,
2794 and each function type node created) the C++ front-end generates
2795 a _named_ TYPE_DECL node for each tagged type node created.
2796 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2797 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2798 front-end, but for each type, tagged or not. */
2800 #define TYPE_DECL_IS_STUB(decl) \
2801 (DECL_NAME (decl) == NULL_TREE \
2802 || (DECL_ARTIFICIAL (decl) \
2803 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2804 /* This is necessary for stub decls that \
2805 appear in nested inline functions. */ \
2806 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2807 && (decl_ultimate_origin (decl) \
2808 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2810 /* Information concerning the compilation unit's programming
2811 language, and compiler version. */
2813 /* Fixed size portion of the DWARF compilation unit header. */
2814 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2815 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2817 /* Fixed size portion of the DWARF comdat type unit header. */
2818 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2819 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2820 + DWARF_OFFSET_SIZE)
2822 /* Fixed size portion of public names info. */
2823 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2825 /* Fixed size portion of the address range info. */
2826 #define DWARF_ARANGES_HEADER_SIZE \
2827 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2828 DWARF2_ADDR_SIZE * 2) \
2829 - DWARF_INITIAL_LENGTH_SIZE)
2831 /* Size of padding portion in the address range info. It must be
2832 aligned to twice the pointer size. */
2833 #define DWARF_ARANGES_PAD_SIZE \
2834 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2835 DWARF2_ADDR_SIZE * 2) \
2836 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2838 /* Use assembler line directives if available. */
2839 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2840 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2841 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2842 #else
2843 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2844 #endif
2845 #endif
2847 /* Minimum line offset in a special line info. opcode.
2848 This value was chosen to give a reasonable range of values. */
2849 #define DWARF_LINE_BASE -10
2851 /* First special line opcode - leave room for the standard opcodes. */
2852 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2854 /* Range of line offsets in a special line info. opcode. */
2855 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2857 /* Flag that indicates the initial value of the is_stmt_start flag.
2858 In the present implementation, we do not mark any lines as
2859 the beginning of a source statement, because that information
2860 is not made available by the GCC front-end. */
2861 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2863 /* Maximum number of operations per instruction bundle. */
2864 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2865 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2866 #endif
2868 /* This location is used by calc_die_sizes() to keep track
2869 the offset of each DIE within the .debug_info section. */
2870 static unsigned long next_die_offset;
2872 /* Record the root of the DIE's built for the current compilation unit. */
2873 static GTY(()) dw_die_ref single_comp_unit_die;
2875 /* A list of type DIEs that have been separated into comdat sections. */
2876 static GTY(()) comdat_type_node *comdat_type_list;
2878 /* A list of DIEs with a NULL parent waiting to be relocated. */
2879 static GTY(()) limbo_die_node *limbo_die_list;
2881 /* A list of DIEs for which we may have to generate
2882 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2883 static GTY(()) limbo_die_node *deferred_asm_name;
2885 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2887 typedef const char *compare_type;
2889 static hashval_t hash (dwarf_file_data *);
2890 static bool equal (dwarf_file_data *, const char *);
2893 /* Filenames referenced by this compilation unit. */
2894 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2896 struct decl_die_hasher : ggc_ptr_hash<die_node>
2898 typedef tree compare_type;
2900 static hashval_t hash (die_node *);
2901 static bool equal (die_node *, tree);
2903 /* A hash table of references to DIE's that describe declarations.
2904 The key is a DECL_UID() which is a unique number identifying each decl. */
2905 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2907 struct block_die_hasher : ggc_ptr_hash<die_struct>
2909 static hashval_t hash (die_struct *);
2910 static bool equal (die_struct *, die_struct *);
2913 /* A hash table of references to DIE's that describe COMMON blocks.
2914 The key is DECL_UID() ^ die_parent. */
2915 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2917 typedef struct GTY(()) die_arg_entry_struct {
2918 dw_die_ref die;
2919 tree arg;
2920 } die_arg_entry;
2923 /* Node of the variable location list. */
2924 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2925 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2926 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2927 in mode of the EXPR_LIST node and first EXPR_LIST operand
2928 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2929 location or NULL for padding. For larger bitsizes,
2930 mode is 0 and first operand is a CONCAT with bitsize
2931 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2932 NULL as second operand. */
2933 rtx GTY (()) loc;
2934 const char * GTY (()) label;
2935 struct var_loc_node * GTY (()) next;
2938 /* Variable location list. */
2939 struct GTY ((for_user)) var_loc_list_def {
2940 struct var_loc_node * GTY (()) first;
2942 /* Pointer to the last but one or last element of the
2943 chained list. If the list is empty, both first and
2944 last are NULL, if the list contains just one node
2945 or the last node certainly is not redundant, it points
2946 to the last node, otherwise points to the last but one.
2947 Do not mark it for GC because it is marked through the chain. */
2948 struct var_loc_node * GTY ((skip ("%h"))) last;
2950 /* Pointer to the last element before section switch,
2951 if NULL, either sections weren't switched or first
2952 is after section switch. */
2953 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2955 /* DECL_UID of the variable decl. */
2956 unsigned int decl_id;
2958 typedef struct var_loc_list_def var_loc_list;
2960 /* Call argument location list. */
2961 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2962 rtx GTY (()) call_arg_loc_note;
2963 const char * GTY (()) label;
2964 tree GTY (()) block;
2965 bool tail_call_p;
2966 rtx GTY (()) symbol_ref;
2967 struct call_arg_loc_node * GTY (()) next;
2971 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2973 typedef const_tree compare_type;
2975 static hashval_t hash (var_loc_list *);
2976 static bool equal (var_loc_list *, const_tree);
2979 /* Table of decl location linked lists. */
2980 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2982 /* Head and tail of call_arg_loc chain. */
2983 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2984 static struct call_arg_loc_node *call_arg_loc_last;
2986 /* Number of call sites in the current function. */
2987 static int call_site_count = -1;
2988 /* Number of tail call sites in the current function. */
2989 static int tail_call_site_count = -1;
2991 /* A cached location list. */
2992 struct GTY ((for_user)) cached_dw_loc_list_def {
2993 /* The DECL_UID of the decl that this entry describes. */
2994 unsigned int decl_id;
2996 /* The cached location list. */
2997 dw_loc_list_ref loc_list;
2999 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3001 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3004 typedef const_tree compare_type;
3006 static hashval_t hash (cached_dw_loc_list *);
3007 static bool equal (cached_dw_loc_list *, const_tree);
3010 /* Table of cached location lists. */
3011 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3013 /* A pointer to the base of a list of references to DIE's that
3014 are uniquely identified by their tag, presence/absence of
3015 children DIE's, and list of attribute/value pairs. */
3016 static GTY((length ("abbrev_die_table_allocated")))
3017 dw_die_ref *abbrev_die_table;
3019 /* Number of elements currently allocated for abbrev_die_table. */
3020 static GTY(()) unsigned abbrev_die_table_allocated;
3022 /* Number of elements in abbrev_die_table currently in use. */
3023 static GTY(()) unsigned abbrev_die_table_in_use;
3025 /* A hash map to remember the stack usage for DWARF procedures. The value
3026 stored is the stack size difference between before the DWARF procedure
3027 invokation and after it returned. In other words, for a DWARF procedure
3028 that consumes N stack slots and that pushes M ones, this stores M - N. */
3029 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3031 /* Size (in elements) of increments by which we may expand the
3032 abbrev_die_table. */
3033 #define ABBREV_DIE_TABLE_INCREMENT 256
3035 /* A global counter for generating labels for line number data. */
3036 static unsigned int line_info_label_num;
3038 /* The current table to which we should emit line number information
3039 for the current function. This will be set up at the beginning of
3040 assembly for the function. */
3041 static GTY(()) dw_line_info_table *cur_line_info_table;
3043 /* The two default tables of line number info. */
3044 static GTY(()) dw_line_info_table *text_section_line_info;
3045 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3047 /* The set of all non-default tables of line number info. */
3048 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3050 /* A flag to tell pubnames/types export if there is an info section to
3051 refer to. */
3052 static bool info_section_emitted;
3054 /* A pointer to the base of a table that contains a list of publicly
3055 accessible names. */
3056 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3058 /* A pointer to the base of a table that contains a list of publicly
3059 accessible types. */
3060 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3062 /* A pointer to the base of a table that contains a list of macro
3063 defines/undefines (and file start/end markers). */
3064 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3066 /* True if .debug_macinfo or .debug_macros section is going to be
3067 emitted. */
3068 #define have_macinfo \
3069 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3070 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3071 && !macinfo_table->is_empty ())
3073 /* Array of dies for which we should generate .debug_ranges info. */
3074 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3076 /* Number of elements currently allocated for ranges_table. */
3077 static GTY(()) unsigned ranges_table_allocated;
3079 /* Number of elements in ranges_table currently in use. */
3080 static GTY(()) unsigned ranges_table_in_use;
3082 /* Array of pairs of labels referenced in ranges_table. */
3083 static GTY ((length ("ranges_by_label_allocated")))
3084 dw_ranges_by_label *ranges_by_label;
3086 /* Number of elements currently allocated for ranges_by_label. */
3087 static GTY(()) unsigned ranges_by_label_allocated;
3089 /* Number of elements in ranges_by_label currently in use. */
3090 static GTY(()) unsigned ranges_by_label_in_use;
3092 /* Size (in elements) of increments by which we may expand the
3093 ranges_table. */
3094 #define RANGES_TABLE_INCREMENT 64
3096 /* Whether we have location lists that need outputting */
3097 static GTY(()) bool have_location_lists;
3099 /* Unique label counter. */
3100 static GTY(()) unsigned int loclabel_num;
3102 /* Unique label counter for point-of-call tables. */
3103 static GTY(()) unsigned int poc_label_num;
3105 /* The last file entry emitted by maybe_emit_file(). */
3106 static GTY(()) struct dwarf_file_data * last_emitted_file;
3108 /* Number of internal labels generated by gen_internal_sym(). */
3109 static GTY(()) int label_num;
3111 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3113 /* Instances of generic types for which we need to generate debug
3114 info that describe their generic parameters and arguments. That
3115 generation needs to happen once all types are properly laid out so
3116 we do it at the end of compilation. */
3117 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3119 /* Offset from the "steady-state frame pointer" to the frame base,
3120 within the current function. */
3121 static HOST_WIDE_INT frame_pointer_fb_offset;
3122 static bool frame_pointer_fb_offset_valid;
3124 static vec<dw_die_ref> base_types;
3126 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3127 once all arguments are parsed. */
3128 static vec<dw_die_ref> *string_types;
3130 /* Flags to represent a set of attribute classes for attributes that represent
3131 a scalar value (bounds, pointers, ...). */
3132 enum dw_scalar_form
3134 dw_scalar_form_constant = 0x01,
3135 dw_scalar_form_exprloc = 0x02,
3136 dw_scalar_form_reference = 0x04
3139 /* Forward declarations for functions defined in this file. */
3141 static int is_pseudo_reg (const_rtx);
3142 static tree type_main_variant (tree);
3143 static int is_tagged_type (const_tree);
3144 static const char *dwarf_tag_name (unsigned);
3145 static const char *dwarf_attr_name (unsigned);
3146 static const char *dwarf_form_name (unsigned);
3147 static tree decl_ultimate_origin (const_tree);
3148 static tree decl_class_context (tree);
3149 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3150 static inline enum dw_val_class AT_class (dw_attr_node *);
3151 static inline unsigned int AT_index (dw_attr_node *);
3152 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3153 static inline unsigned AT_flag (dw_attr_node *);
3154 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3155 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3156 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3157 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3158 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3159 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3160 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3161 unsigned int, unsigned char *);
3162 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3163 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3164 static inline const char *AT_string (dw_attr_node *);
3165 static enum dwarf_form AT_string_form (dw_attr_node *);
3166 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3167 static void add_AT_specification (dw_die_ref, dw_die_ref);
3168 static inline dw_die_ref AT_ref (dw_attr_node *);
3169 static inline int AT_ref_external (dw_attr_node *);
3170 static inline void set_AT_ref_external (dw_attr_node *, int);
3171 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3172 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3173 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3174 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3175 dw_loc_list_ref);
3176 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3177 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3178 static void remove_addr_table_entry (addr_table_entry *);
3179 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3180 static inline rtx AT_addr (dw_attr_node *);
3181 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3182 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3183 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3184 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3185 unsigned HOST_WIDE_INT);
3186 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3187 unsigned long, bool);
3188 static inline const char *AT_lbl (dw_attr_node *);
3189 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3190 static const char *get_AT_low_pc (dw_die_ref);
3191 static const char *get_AT_hi_pc (dw_die_ref);
3192 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3193 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3194 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3195 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3196 static bool is_cxx (void);
3197 static bool is_fortran (void);
3198 static bool is_ada (void);
3199 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3200 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3201 static void add_child_die (dw_die_ref, dw_die_ref);
3202 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3203 static dw_die_ref lookup_type_die (tree);
3204 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3205 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3206 static void equate_type_number_to_die (tree, dw_die_ref);
3207 static dw_die_ref lookup_decl_die (tree);
3208 static var_loc_list *lookup_decl_loc (const_tree);
3209 static void equate_decl_number_to_die (tree, dw_die_ref);
3210 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3211 static void print_spaces (FILE *);
3212 static void print_die (dw_die_ref, FILE *);
3213 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3214 static dw_die_ref pop_compile_unit (dw_die_ref);
3215 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3216 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3217 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3218 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3219 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3220 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3221 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3222 struct md5_ctx *, int *);
3223 struct checksum_attributes;
3224 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3225 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3226 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3227 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3228 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3229 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3230 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3231 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3232 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3233 static void compute_section_prefix (dw_die_ref);
3234 static int is_type_die (dw_die_ref);
3235 static int is_comdat_die (dw_die_ref);
3236 static int is_symbol_die (dw_die_ref);
3237 static inline bool is_template_instantiation (dw_die_ref);
3238 static void assign_symbol_names (dw_die_ref);
3239 static void break_out_includes (dw_die_ref);
3240 static int is_declaration_die (dw_die_ref);
3241 static int should_move_die_to_comdat (dw_die_ref);
3242 static dw_die_ref clone_as_declaration (dw_die_ref);
3243 static dw_die_ref clone_die (dw_die_ref);
3244 static dw_die_ref clone_tree (dw_die_ref);
3245 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3246 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3247 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3248 static dw_die_ref generate_skeleton (dw_die_ref);
3249 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3250 dw_die_ref,
3251 dw_die_ref);
3252 static void break_out_comdat_types (dw_die_ref);
3253 static void copy_decls_for_unworthy_types (dw_die_ref);
3255 static void add_sibling_attributes (dw_die_ref);
3256 static void output_location_lists (dw_die_ref);
3257 static int constant_size (unsigned HOST_WIDE_INT);
3258 static unsigned long size_of_die (dw_die_ref);
3259 static void calc_die_sizes (dw_die_ref);
3260 static void calc_base_type_die_sizes (void);
3261 static void mark_dies (dw_die_ref);
3262 static void unmark_dies (dw_die_ref);
3263 static void unmark_all_dies (dw_die_ref);
3264 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3265 static unsigned long size_of_aranges (void);
3266 static enum dwarf_form value_format (dw_attr_node *);
3267 static void output_value_format (dw_attr_node *);
3268 static void output_abbrev_section (void);
3269 static void output_die_abbrevs (unsigned long, dw_die_ref);
3270 static void output_die_symbol (dw_die_ref);
3271 static void output_die (dw_die_ref);
3272 static void output_compilation_unit_header (void);
3273 static void output_comp_unit (dw_die_ref, int);
3274 static void output_comdat_type_unit (comdat_type_node *);
3275 static const char *dwarf2_name (tree, int);
3276 static void add_pubname (tree, dw_die_ref);
3277 static void add_enumerator_pubname (const char *, dw_die_ref);
3278 static void add_pubname_string (const char *, dw_die_ref);
3279 static void add_pubtype (tree, dw_die_ref);
3280 static void output_pubnames (vec<pubname_entry, va_gc> *);
3281 static void output_aranges (void);
3282 static unsigned int add_ranges_num (int);
3283 static unsigned int add_ranges (const_tree);
3284 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3285 bool *, bool);
3286 static void output_ranges (void);
3287 static dw_line_info_table *new_line_info_table (void);
3288 static void output_line_info (bool);
3289 static void output_file_names (void);
3290 static dw_die_ref base_type_die (tree, bool);
3291 static int is_base_type (tree);
3292 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3293 static int decl_quals (const_tree);
3294 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3295 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3296 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3297 static int type_is_enum (const_tree);
3298 static unsigned int dbx_reg_number (const_rtx);
3299 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3300 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3301 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3302 enum var_init_status);
3303 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3304 enum var_init_status);
3305 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3306 enum var_init_status);
3307 static int is_based_loc (const_rtx);
3308 static bool resolve_one_addr (rtx *);
3309 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3310 enum var_init_status);
3311 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3312 enum var_init_status);
3313 struct loc_descr_context;
3314 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3315 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3316 static dw_loc_list_ref loc_list_from_tree (tree, int,
3317 const struct loc_descr_context *);
3318 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3319 const struct loc_descr_context *);
3320 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3321 static tree field_type (const_tree);
3322 static unsigned int simple_type_align_in_bits (const_tree);
3323 static unsigned int simple_decl_align_in_bits (const_tree);
3324 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3325 struct vlr_context;
3326 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3327 HOST_WIDE_INT *);
3328 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3329 dw_loc_list_ref);
3330 static void add_data_member_location_attribute (dw_die_ref, tree,
3331 struct vlr_context *);
3332 static bool add_const_value_attribute (dw_die_ref, rtx);
3333 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3334 static void insert_wide_int (const wide_int &, unsigned char *, int);
3335 static void insert_float (const_rtx, unsigned char *);
3336 static rtx rtl_for_decl_location (tree);
3337 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3338 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3339 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3340 static void add_name_attribute (dw_die_ref, const char *);
3341 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3342 static void add_comp_dir_attribute (dw_die_ref);
3343 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3344 const struct loc_descr_context *);
3345 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3346 const struct loc_descr_context *);
3347 static void add_subscript_info (dw_die_ref, tree, bool);
3348 static void add_byte_size_attribute (dw_die_ref, tree);
3349 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3350 struct vlr_context *);
3351 static void add_bit_size_attribute (dw_die_ref, tree);
3352 static void add_prototyped_attribute (dw_die_ref, tree);
3353 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3354 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3355 static void add_src_coords_attributes (dw_die_ref, tree);
3356 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3357 static void add_discr_value (dw_die_ref, dw_discr_value *);
3358 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3359 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3360 static void push_decl_scope (tree);
3361 static void pop_decl_scope (void);
3362 static dw_die_ref scope_die_for (tree, dw_die_ref);
3363 static inline int local_scope_p (dw_die_ref);
3364 static inline int class_scope_p (dw_die_ref);
3365 static inline int class_or_namespace_scope_p (dw_die_ref);
3366 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3367 static void add_calling_convention_attribute (dw_die_ref, tree);
3368 static const char *type_tag (const_tree);
3369 static tree member_declared_type (const_tree);
3370 #if 0
3371 static const char *decl_start_label (tree);
3372 #endif
3373 static void gen_array_type_die (tree, dw_die_ref);
3374 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3375 #if 0
3376 static void gen_entry_point_die (tree, dw_die_ref);
3377 #endif
3378 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3379 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3380 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3381 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3382 static void gen_formal_types_die (tree, dw_die_ref);
3383 static void gen_subprogram_die (tree, dw_die_ref);
3384 static void gen_variable_die (tree, tree, dw_die_ref);
3385 static void gen_const_die (tree, dw_die_ref);
3386 static void gen_label_die (tree, dw_die_ref);
3387 static void gen_lexical_block_die (tree, dw_die_ref);
3388 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3389 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3390 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3391 static dw_die_ref gen_compile_unit_die (const char *);
3392 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3393 static void gen_member_die (tree, dw_die_ref);
3394 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3395 enum debug_info_usage);
3396 static void gen_subroutine_type_die (tree, dw_die_ref);
3397 static void gen_typedef_die (tree, dw_die_ref);
3398 static void gen_type_die (tree, dw_die_ref);
3399 static void gen_block_die (tree, dw_die_ref);
3400 static void decls_for_scope (tree, dw_die_ref);
3401 static bool is_naming_typedef_decl (const_tree);
3402 static inline dw_die_ref get_context_die (tree);
3403 static void gen_namespace_die (tree, dw_die_ref);
3404 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3405 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3406 static dw_die_ref force_decl_die (tree);
3407 static dw_die_ref force_type_die (tree);
3408 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3409 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3410 static struct dwarf_file_data * lookup_filename (const char *);
3411 static void retry_incomplete_types (void);
3412 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3413 static void gen_generic_params_dies (tree);
3414 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3415 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3416 static void splice_child_die (dw_die_ref, dw_die_ref);
3417 static int file_info_cmp (const void *, const void *);
3418 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3419 const char *, const char *);
3420 static void output_loc_list (dw_loc_list_ref);
3421 static char *gen_internal_sym (const char *);
3422 static bool want_pubnames (void);
3424 static void prune_unmark_dies (dw_die_ref);
3425 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3426 static void prune_unused_types_mark (dw_die_ref, int);
3427 static void prune_unused_types_walk (dw_die_ref);
3428 static void prune_unused_types_walk_attribs (dw_die_ref);
3429 static void prune_unused_types_prune (dw_die_ref);
3430 static void prune_unused_types (void);
3431 static int maybe_emit_file (struct dwarf_file_data *fd);
3432 static inline const char *AT_vms_delta1 (dw_attr_node *);
3433 static inline const char *AT_vms_delta2 (dw_attr_node *);
3434 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3435 const char *, const char *);
3436 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3437 static void gen_remaining_tmpl_value_param_die_attribute (void);
3438 static bool generic_type_p (tree);
3439 static void schedule_generic_params_dies_gen (tree t);
3440 static void gen_scheduled_generic_parms_dies (void);
3442 static const char *comp_dir_string (void);
3444 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3446 /* enum for tracking thread-local variables whose address is really an offset
3447 relative to the TLS pointer, which will need link-time relocation, but will
3448 not need relocation by the DWARF consumer. */
3450 enum dtprel_bool
3452 dtprel_false = 0,
3453 dtprel_true = 1
3456 /* Return the operator to use for an address of a variable. For dtprel_true, we
3457 use DW_OP_const*. For regular variables, which need both link-time
3458 relocation and consumer-level relocation (e.g., to account for shared objects
3459 loaded at a random address), we use DW_OP_addr*. */
3461 static inline enum dwarf_location_atom
3462 dw_addr_op (enum dtprel_bool dtprel)
3464 if (dtprel == dtprel_true)
3465 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3466 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3467 else
3468 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3471 /* Return a pointer to a newly allocated address location description. If
3472 dwarf_split_debug_info is true, then record the address with the appropriate
3473 relocation. */
3474 static inline dw_loc_descr_ref
3475 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3477 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3479 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3480 ref->dw_loc_oprnd1.v.val_addr = addr;
3481 ref->dtprel = dtprel;
3482 if (dwarf_split_debug_info)
3483 ref->dw_loc_oprnd1.val_entry
3484 = add_addr_table_entry (addr,
3485 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3486 else
3487 ref->dw_loc_oprnd1.val_entry = NULL;
3489 return ref;
3492 /* Section names used to hold DWARF debugging information. */
3494 #ifndef DEBUG_INFO_SECTION
3495 #define DEBUG_INFO_SECTION ".debug_info"
3496 #endif
3497 #ifndef DEBUG_DWO_INFO_SECTION
3498 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3499 #endif
3500 #ifndef DEBUG_ABBREV_SECTION
3501 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3502 #endif
3503 #ifndef DEBUG_DWO_ABBREV_SECTION
3504 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3505 #endif
3506 #ifndef DEBUG_ARANGES_SECTION
3507 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3508 #endif
3509 #ifndef DEBUG_ADDR_SECTION
3510 #define DEBUG_ADDR_SECTION ".debug_addr"
3511 #endif
3512 #ifndef DEBUG_NORM_MACINFO_SECTION
3513 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3514 #endif
3515 #ifndef DEBUG_DWO_MACINFO_SECTION
3516 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3517 #endif
3518 #ifndef DEBUG_MACINFO_SECTION
3519 #define DEBUG_MACINFO_SECTION \
3520 (!dwarf_split_debug_info \
3521 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3522 #endif
3523 #ifndef DEBUG_NORM_MACRO_SECTION
3524 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3525 #endif
3526 #ifndef DEBUG_DWO_MACRO_SECTION
3527 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3528 #endif
3529 #ifndef DEBUG_MACRO_SECTION
3530 #define DEBUG_MACRO_SECTION \
3531 (!dwarf_split_debug_info \
3532 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3533 #endif
3534 #ifndef DEBUG_LINE_SECTION
3535 #define DEBUG_LINE_SECTION ".debug_line"
3536 #endif
3537 #ifndef DEBUG_DWO_LINE_SECTION
3538 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3539 #endif
3540 #ifndef DEBUG_LOC_SECTION
3541 #define DEBUG_LOC_SECTION ".debug_loc"
3542 #endif
3543 #ifndef DEBUG_DWO_LOC_SECTION
3544 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3545 #endif
3546 #ifndef DEBUG_PUBNAMES_SECTION
3547 #define DEBUG_PUBNAMES_SECTION \
3548 ((debug_generate_pub_sections == 2) \
3549 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3550 #endif
3551 #ifndef DEBUG_PUBTYPES_SECTION
3552 #define DEBUG_PUBTYPES_SECTION \
3553 ((debug_generate_pub_sections == 2) \
3554 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3555 #endif
3556 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3557 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3558 #ifndef DEBUG_STR_OFFSETS_SECTION
3559 #define DEBUG_STR_OFFSETS_SECTION \
3560 (!dwarf_split_debug_info \
3561 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3562 #endif
3563 #ifndef DEBUG_STR_DWO_SECTION
3564 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3565 #endif
3566 #ifndef DEBUG_STR_SECTION
3567 #define DEBUG_STR_SECTION ".debug_str"
3568 #endif
3569 #ifndef DEBUG_RANGES_SECTION
3570 #define DEBUG_RANGES_SECTION ".debug_ranges"
3571 #endif
3573 /* Standard ELF section names for compiled code and data. */
3574 #ifndef TEXT_SECTION_NAME
3575 #define TEXT_SECTION_NAME ".text"
3576 #endif
3578 /* Section flags for .debug_macinfo/.debug_macro section. */
3579 #define DEBUG_MACRO_SECTION_FLAGS \
3580 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3582 /* Section flags for .debug_str section. */
3583 #define DEBUG_STR_SECTION_FLAGS \
3584 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3585 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3586 : SECTION_DEBUG)
3588 /* Section flags for .debug_str.dwo section. */
3589 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3591 /* Labels we insert at beginning sections we can reference instead of
3592 the section names themselves. */
3594 #ifndef TEXT_SECTION_LABEL
3595 #define TEXT_SECTION_LABEL "Ltext"
3596 #endif
3597 #ifndef COLD_TEXT_SECTION_LABEL
3598 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3599 #endif
3600 #ifndef DEBUG_LINE_SECTION_LABEL
3601 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3602 #endif
3603 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3604 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3605 #endif
3606 #ifndef DEBUG_INFO_SECTION_LABEL
3607 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3608 #endif
3609 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3610 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3611 #endif
3612 #ifndef DEBUG_ABBREV_SECTION_LABEL
3613 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3614 #endif
3615 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3616 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3617 #endif
3618 #ifndef DEBUG_ADDR_SECTION_LABEL
3619 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3620 #endif
3621 #ifndef DEBUG_LOC_SECTION_LABEL
3622 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3623 #endif
3624 #ifndef DEBUG_RANGES_SECTION_LABEL
3625 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3626 #endif
3627 #ifndef DEBUG_MACINFO_SECTION_LABEL
3628 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3629 #endif
3630 #ifndef DEBUG_MACRO_SECTION_LABEL
3631 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3632 #endif
3633 #define SKELETON_COMP_DIE_ABBREV 1
3634 #define SKELETON_TYPE_DIE_ABBREV 2
3636 /* Definitions of defaults for formats and names of various special
3637 (artificial) labels which may be generated within this file (when the -g
3638 options is used and DWARF2_DEBUGGING_INFO is in effect.
3639 If necessary, these may be overridden from within the tm.h file, but
3640 typically, overriding these defaults is unnecessary. */
3642 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3643 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3644 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3645 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3646 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3647 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3648 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3649 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3650 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3651 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3652 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3653 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3654 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3655 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3657 #ifndef TEXT_END_LABEL
3658 #define TEXT_END_LABEL "Letext"
3659 #endif
3660 #ifndef COLD_END_LABEL
3661 #define COLD_END_LABEL "Letext_cold"
3662 #endif
3663 #ifndef BLOCK_BEGIN_LABEL
3664 #define BLOCK_BEGIN_LABEL "LBB"
3665 #endif
3666 #ifndef BLOCK_END_LABEL
3667 #define BLOCK_END_LABEL "LBE"
3668 #endif
3669 #ifndef LINE_CODE_LABEL
3670 #define LINE_CODE_LABEL "LM"
3671 #endif
3674 /* Return the root of the DIE's built for the current compilation unit. */
3675 static dw_die_ref
3676 comp_unit_die (void)
3678 if (!single_comp_unit_die)
3679 single_comp_unit_die = gen_compile_unit_die (NULL);
3680 return single_comp_unit_die;
3683 /* We allow a language front-end to designate a function that is to be
3684 called to "demangle" any name before it is put into a DIE. */
3686 static const char *(*demangle_name_func) (const char *);
3688 void
3689 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3691 demangle_name_func = func;
3694 /* Test if rtl node points to a pseudo register. */
3696 static inline int
3697 is_pseudo_reg (const_rtx rtl)
3699 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3700 || (GET_CODE (rtl) == SUBREG
3701 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3704 /* Return a reference to a type, with its const and volatile qualifiers
3705 removed. */
3707 static inline tree
3708 type_main_variant (tree type)
3710 type = TYPE_MAIN_VARIANT (type);
3712 /* ??? There really should be only one main variant among any group of
3713 variants of a given type (and all of the MAIN_VARIANT values for all
3714 members of the group should point to that one type) but sometimes the C
3715 front-end messes this up for array types, so we work around that bug
3716 here. */
3717 if (TREE_CODE (type) == ARRAY_TYPE)
3718 while (type != TYPE_MAIN_VARIANT (type))
3719 type = TYPE_MAIN_VARIANT (type);
3721 return type;
3724 /* Return nonzero if the given type node represents a tagged type. */
3726 static inline int
3727 is_tagged_type (const_tree type)
3729 enum tree_code code = TREE_CODE (type);
3731 return (code == RECORD_TYPE || code == UNION_TYPE
3732 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3735 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3737 static void
3738 get_ref_die_offset_label (char *label, dw_die_ref ref)
3740 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3743 /* Return die_offset of a DIE reference to a base type. */
3745 static unsigned long int
3746 get_base_type_offset (dw_die_ref ref)
3748 if (ref->die_offset)
3749 return ref->die_offset;
3750 if (comp_unit_die ()->die_abbrev)
3752 calc_base_type_die_sizes ();
3753 gcc_assert (ref->die_offset);
3755 return ref->die_offset;
3758 /* Return die_offset of a DIE reference other than base type. */
3760 static unsigned long int
3761 get_ref_die_offset (dw_die_ref ref)
3763 gcc_assert (ref->die_offset);
3764 return ref->die_offset;
3767 /* Convert a DIE tag into its string name. */
3769 static const char *
3770 dwarf_tag_name (unsigned int tag)
3772 const char *name = get_DW_TAG_name (tag);
3774 if (name != NULL)
3775 return name;
3777 return "DW_TAG_<unknown>";
3780 /* Convert a DWARF attribute code into its string name. */
3782 static const char *
3783 dwarf_attr_name (unsigned int attr)
3785 const char *name;
3787 switch (attr)
3789 #if VMS_DEBUGGING_INFO
3790 case DW_AT_HP_prologue:
3791 return "DW_AT_HP_prologue";
3792 #else
3793 case DW_AT_MIPS_loop_unroll_factor:
3794 return "DW_AT_MIPS_loop_unroll_factor";
3795 #endif
3797 #if VMS_DEBUGGING_INFO
3798 case DW_AT_HP_epilogue:
3799 return "DW_AT_HP_epilogue";
3800 #else
3801 case DW_AT_MIPS_stride:
3802 return "DW_AT_MIPS_stride";
3803 #endif
3806 name = get_DW_AT_name (attr);
3808 if (name != NULL)
3809 return name;
3811 return "DW_AT_<unknown>";
3814 /* Convert a DWARF value form code into its string name. */
3816 static const char *
3817 dwarf_form_name (unsigned int form)
3819 const char *name = get_DW_FORM_name (form);
3821 if (name != NULL)
3822 return name;
3824 return "DW_FORM_<unknown>";
3827 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3828 instance of an inlined instance of a decl which is local to an inline
3829 function, so we have to trace all of the way back through the origin chain
3830 to find out what sort of node actually served as the original seed for the
3831 given block. */
3833 static tree
3834 decl_ultimate_origin (const_tree decl)
3836 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3837 return NULL_TREE;
3839 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3840 we're trying to output the abstract instance of this function. */
3841 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3842 return NULL_TREE;
3844 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3845 most distant ancestor, this should never happen. */
3846 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3848 return DECL_ABSTRACT_ORIGIN (decl);
3851 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3852 of a virtual function may refer to a base class, so we check the 'this'
3853 parameter. */
3855 static tree
3856 decl_class_context (tree decl)
3858 tree context = NULL_TREE;
3860 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3861 context = DECL_CONTEXT (decl);
3862 else
3863 context = TYPE_MAIN_VARIANT
3864 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3866 if (context && !TYPE_P (context))
3867 context = NULL_TREE;
3869 return context;
3872 /* Add an attribute/value pair to a DIE. */
3874 static inline void
3875 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3877 /* Maybe this should be an assert? */
3878 if (die == NULL)
3879 return;
3881 vec_safe_reserve (die->die_attr, 1);
3882 vec_safe_push (die->die_attr, *attr);
3885 static inline enum dw_val_class
3886 AT_class (dw_attr_node *a)
3888 return a->dw_attr_val.val_class;
3891 /* Return the index for any attribute that will be referenced with a
3892 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3893 are stored in dw_attr_val.v.val_str for reference counting
3894 pruning. */
3896 static inline unsigned int
3897 AT_index (dw_attr_node *a)
3899 if (AT_class (a) == dw_val_class_str)
3900 return a->dw_attr_val.v.val_str->index;
3901 else if (a->dw_attr_val.val_entry != NULL)
3902 return a->dw_attr_val.val_entry->index;
3903 return NOT_INDEXED;
3906 /* Add a flag value attribute to a DIE. */
3908 static inline void
3909 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3911 dw_attr_node attr;
3913 attr.dw_attr = attr_kind;
3914 attr.dw_attr_val.val_class = dw_val_class_flag;
3915 attr.dw_attr_val.val_entry = NULL;
3916 attr.dw_attr_val.v.val_flag = flag;
3917 add_dwarf_attr (die, &attr);
3920 static inline unsigned
3921 AT_flag (dw_attr_node *a)
3923 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3924 return a->dw_attr_val.v.val_flag;
3927 /* Add a signed integer attribute value to a DIE. */
3929 static inline void
3930 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3932 dw_attr_node attr;
3934 attr.dw_attr = attr_kind;
3935 attr.dw_attr_val.val_class = dw_val_class_const;
3936 attr.dw_attr_val.val_entry = NULL;
3937 attr.dw_attr_val.v.val_int = int_val;
3938 add_dwarf_attr (die, &attr);
3941 static inline HOST_WIDE_INT
3942 AT_int (dw_attr_node *a)
3944 gcc_assert (a && AT_class (a) == dw_val_class_const);
3945 return a->dw_attr_val.v.val_int;
3948 /* Add an unsigned integer attribute value to a DIE. */
3950 static inline void
3951 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3952 unsigned HOST_WIDE_INT unsigned_val)
3954 dw_attr_node attr;
3956 attr.dw_attr = attr_kind;
3957 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3958 attr.dw_attr_val.val_entry = NULL;
3959 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3960 add_dwarf_attr (die, &attr);
3963 static inline unsigned HOST_WIDE_INT
3964 AT_unsigned (dw_attr_node *a)
3966 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3967 return a->dw_attr_val.v.val_unsigned;
3970 /* Add an unsigned wide integer attribute value to a DIE. */
3972 static inline void
3973 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3974 const wide_int& w)
3976 dw_attr_node attr;
3978 attr.dw_attr = attr_kind;
3979 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3980 attr.dw_attr_val.val_entry = NULL;
3981 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3982 *attr.dw_attr_val.v.val_wide = w;
3983 add_dwarf_attr (die, &attr);
3986 /* Add an unsigned double integer attribute value to a DIE. */
3988 static inline void
3989 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3990 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3992 dw_attr_node attr;
3994 attr.dw_attr = attr_kind;
3995 attr.dw_attr_val.val_class = dw_val_class_const_double;
3996 attr.dw_attr_val.val_entry = NULL;
3997 attr.dw_attr_val.v.val_double.high = high;
3998 attr.dw_attr_val.v.val_double.low = low;
3999 add_dwarf_attr (die, &attr);
4002 /* Add a floating point attribute value to a DIE and return it. */
4004 static inline void
4005 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4006 unsigned int length, unsigned int elt_size, unsigned char *array)
4008 dw_attr_node attr;
4010 attr.dw_attr = attr_kind;
4011 attr.dw_attr_val.val_class = dw_val_class_vec;
4012 attr.dw_attr_val.val_entry = NULL;
4013 attr.dw_attr_val.v.val_vec.length = length;
4014 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4015 attr.dw_attr_val.v.val_vec.array = array;
4016 add_dwarf_attr (die, &attr);
4019 /* Add an 8-byte data attribute value to a DIE. */
4021 static inline void
4022 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4023 unsigned char data8[8])
4025 dw_attr_node attr;
4027 attr.dw_attr = attr_kind;
4028 attr.dw_attr_val.val_class = dw_val_class_data8;
4029 attr.dw_attr_val.val_entry = NULL;
4030 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4031 add_dwarf_attr (die, &attr);
4034 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4035 dwarf_split_debug_info, address attributes in dies destined for the
4036 final executable have force_direct set to avoid using indexed
4037 references. */
4039 static inline void
4040 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4041 bool force_direct)
4043 dw_attr_node attr;
4044 char * lbl_id;
4046 lbl_id = xstrdup (lbl_low);
4047 attr.dw_attr = DW_AT_low_pc;
4048 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4049 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4050 if (dwarf_split_debug_info && !force_direct)
4051 attr.dw_attr_val.val_entry
4052 = add_addr_table_entry (lbl_id, ate_kind_label);
4053 else
4054 attr.dw_attr_val.val_entry = NULL;
4055 add_dwarf_attr (die, &attr);
4057 attr.dw_attr = DW_AT_high_pc;
4058 if (dwarf_version < 4)
4059 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4060 else
4061 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4062 lbl_id = xstrdup (lbl_high);
4063 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4064 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4065 && dwarf_split_debug_info && !force_direct)
4066 attr.dw_attr_val.val_entry
4067 = add_addr_table_entry (lbl_id, ate_kind_label);
4068 else
4069 attr.dw_attr_val.val_entry = NULL;
4070 add_dwarf_attr (die, &attr);
4073 /* Hash and equality functions for debug_str_hash. */
4075 hashval_t
4076 indirect_string_hasher::hash (indirect_string_node *x)
4078 return htab_hash_string (x->str);
4081 bool
4082 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4084 return strcmp (x1->str, x2) == 0;
4087 /* Add STR to the given string hash table. */
4089 static struct indirect_string_node *
4090 find_AT_string_in_table (const char *str,
4091 hash_table<indirect_string_hasher> *table)
4093 struct indirect_string_node *node;
4095 indirect_string_node **slot
4096 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4097 if (*slot == NULL)
4099 node = ggc_cleared_alloc<indirect_string_node> ();
4100 node->str = ggc_strdup (str);
4101 *slot = node;
4103 else
4104 node = *slot;
4106 node->refcount++;
4107 return node;
4110 /* Add STR to the indirect string hash table. */
4112 static struct indirect_string_node *
4113 find_AT_string (const char *str)
4115 if (! debug_str_hash)
4116 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4118 return find_AT_string_in_table (str, debug_str_hash);
4121 /* Add a string attribute value to a DIE. */
4123 static inline void
4124 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4126 dw_attr_node attr;
4127 struct indirect_string_node *node;
4129 node = find_AT_string (str);
4131 attr.dw_attr = attr_kind;
4132 attr.dw_attr_val.val_class = dw_val_class_str;
4133 attr.dw_attr_val.val_entry = NULL;
4134 attr.dw_attr_val.v.val_str = node;
4135 add_dwarf_attr (die, &attr);
4138 static inline const char *
4139 AT_string (dw_attr_node *a)
4141 gcc_assert (a && AT_class (a) == dw_val_class_str);
4142 return a->dw_attr_val.v.val_str->str;
4145 /* Call this function directly to bypass AT_string_form's logic to put
4146 the string inline in the die. */
4148 static void
4149 set_indirect_string (struct indirect_string_node *node)
4151 char label[32];
4152 /* Already indirect is a no op. */
4153 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4155 gcc_assert (node->label);
4156 return;
4158 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4159 ++dw2_string_counter;
4160 node->label = xstrdup (label);
4162 if (!dwarf_split_debug_info)
4164 node->form = DW_FORM_strp;
4165 node->index = NOT_INDEXED;
4167 else
4169 node->form = DW_FORM_GNU_str_index;
4170 node->index = NO_INDEX_ASSIGNED;
4174 /* Find out whether a string should be output inline in DIE
4175 or out-of-line in .debug_str section. */
4177 static enum dwarf_form
4178 find_string_form (struct indirect_string_node *node)
4180 unsigned int len;
4182 if (node->form)
4183 return node->form;
4185 len = strlen (node->str) + 1;
4187 /* If the string is shorter or equal to the size of the reference, it is
4188 always better to put it inline. */
4189 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4190 return node->form = DW_FORM_string;
4192 /* If we cannot expect the linker to merge strings in .debug_str
4193 section, only put it into .debug_str if it is worth even in this
4194 single module. */
4195 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4196 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4197 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4198 return node->form = DW_FORM_string;
4200 set_indirect_string (node);
4202 return node->form;
4205 /* Find out whether the string referenced from the attribute should be
4206 output inline in DIE or out-of-line in .debug_str section. */
4208 static enum dwarf_form
4209 AT_string_form (dw_attr_node *a)
4211 gcc_assert (a && AT_class (a) == dw_val_class_str);
4212 return find_string_form (a->dw_attr_val.v.val_str);
4215 /* Add a DIE reference attribute value to a DIE. */
4217 static inline void
4218 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4220 dw_attr_node attr;
4221 gcc_checking_assert (targ_die != NULL);
4223 /* With LTO we can end up trying to reference something we didn't create
4224 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4225 if (targ_die == NULL)
4226 return;
4228 attr.dw_attr = attr_kind;
4229 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4230 attr.dw_attr_val.val_entry = NULL;
4231 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4232 attr.dw_attr_val.v.val_die_ref.external = 0;
4233 add_dwarf_attr (die, &attr);
4236 /* Change DIE reference REF to point to NEW_DIE instead. */
4238 static inline void
4239 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4241 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4242 ref->dw_attr_val.v.val_die_ref.die = new_die;
4243 ref->dw_attr_val.v.val_die_ref.external = 0;
4246 /* Add an AT_specification attribute to a DIE, and also make the back
4247 pointer from the specification to the definition. */
4249 static inline void
4250 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4252 add_AT_die_ref (die, DW_AT_specification, targ_die);
4253 gcc_assert (!targ_die->die_definition);
4254 targ_die->die_definition = die;
4257 static inline dw_die_ref
4258 AT_ref (dw_attr_node *a)
4260 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4261 return a->dw_attr_val.v.val_die_ref.die;
4264 static inline int
4265 AT_ref_external (dw_attr_node *a)
4267 if (a && AT_class (a) == dw_val_class_die_ref)
4268 return a->dw_attr_val.v.val_die_ref.external;
4270 return 0;
4273 static inline void
4274 set_AT_ref_external (dw_attr_node *a, int i)
4276 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4277 a->dw_attr_val.v.val_die_ref.external = i;
4280 /* Add an FDE reference attribute value to a DIE. */
4282 static inline void
4283 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4285 dw_attr_node attr;
4287 attr.dw_attr = attr_kind;
4288 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4289 attr.dw_attr_val.val_entry = NULL;
4290 attr.dw_attr_val.v.val_fde_index = targ_fde;
4291 add_dwarf_attr (die, &attr);
4294 /* Add a location description attribute value to a DIE. */
4296 static inline void
4297 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4299 dw_attr_node attr;
4301 attr.dw_attr = attr_kind;
4302 attr.dw_attr_val.val_class = dw_val_class_loc;
4303 attr.dw_attr_val.val_entry = NULL;
4304 attr.dw_attr_val.v.val_loc = loc;
4305 add_dwarf_attr (die, &attr);
4308 static inline dw_loc_descr_ref
4309 AT_loc (dw_attr_node *a)
4311 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4312 return a->dw_attr_val.v.val_loc;
4315 static inline void
4316 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4318 dw_attr_node attr;
4320 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4321 return;
4323 attr.dw_attr = attr_kind;
4324 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4325 attr.dw_attr_val.val_entry = NULL;
4326 attr.dw_attr_val.v.val_loc_list = loc_list;
4327 add_dwarf_attr (die, &attr);
4328 have_location_lists = true;
4331 static inline dw_loc_list_ref
4332 AT_loc_list (dw_attr_node *a)
4334 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4335 return a->dw_attr_val.v.val_loc_list;
4338 static inline dw_loc_list_ref *
4339 AT_loc_list_ptr (dw_attr_node *a)
4341 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4342 return &a->dw_attr_val.v.val_loc_list;
4345 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4347 static hashval_t hash (addr_table_entry *);
4348 static bool equal (addr_table_entry *, addr_table_entry *);
4351 /* Table of entries into the .debug_addr section. */
4353 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4355 /* Hash an address_table_entry. */
4357 hashval_t
4358 addr_hasher::hash (addr_table_entry *a)
4360 inchash::hash hstate;
4361 switch (a->kind)
4363 case ate_kind_rtx:
4364 hstate.add_int (0);
4365 break;
4366 case ate_kind_rtx_dtprel:
4367 hstate.add_int (1);
4368 break;
4369 case ate_kind_label:
4370 return htab_hash_string (a->addr.label);
4371 default:
4372 gcc_unreachable ();
4374 inchash::add_rtx (a->addr.rtl, hstate);
4375 return hstate.end ();
4378 /* Determine equality for two address_table_entries. */
4380 bool
4381 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4383 if (a1->kind != a2->kind)
4384 return 0;
4385 switch (a1->kind)
4387 case ate_kind_rtx:
4388 case ate_kind_rtx_dtprel:
4389 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4390 case ate_kind_label:
4391 return strcmp (a1->addr.label, a2->addr.label) == 0;
4392 default:
4393 gcc_unreachable ();
4397 /* Initialize an addr_table_entry. */
4399 void
4400 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4402 e->kind = kind;
4403 switch (kind)
4405 case ate_kind_rtx:
4406 case ate_kind_rtx_dtprel:
4407 e->addr.rtl = (rtx) addr;
4408 break;
4409 case ate_kind_label:
4410 e->addr.label = (char *) addr;
4411 break;
4413 e->refcount = 0;
4414 e->index = NO_INDEX_ASSIGNED;
4417 /* Add attr to the address table entry to the table. Defer setting an
4418 index until output time. */
4420 static addr_table_entry *
4421 add_addr_table_entry (void *addr, enum ate_kind kind)
4423 addr_table_entry *node;
4424 addr_table_entry finder;
4426 gcc_assert (dwarf_split_debug_info);
4427 if (! addr_index_table)
4428 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4429 init_addr_table_entry (&finder, kind, addr);
4430 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4432 if (*slot == HTAB_EMPTY_ENTRY)
4434 node = ggc_cleared_alloc<addr_table_entry> ();
4435 init_addr_table_entry (node, kind, addr);
4436 *slot = node;
4438 else
4439 node = *slot;
4441 node->refcount++;
4442 return node;
4445 /* Remove an entry from the addr table by decrementing its refcount.
4446 Strictly, decrementing the refcount would be enough, but the
4447 assertion that the entry is actually in the table has found
4448 bugs. */
4450 static void
4451 remove_addr_table_entry (addr_table_entry *entry)
4453 gcc_assert (dwarf_split_debug_info && addr_index_table);
4454 /* After an index is assigned, the table is frozen. */
4455 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4456 entry->refcount--;
4459 /* Given a location list, remove all addresses it refers to from the
4460 address_table. */
4462 static void
4463 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4465 for (; descr; descr = descr->dw_loc_next)
4466 if (descr->dw_loc_oprnd1.val_entry != NULL)
4468 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4469 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4473 /* A helper function for dwarf2out_finish called through
4474 htab_traverse. Assign an addr_table_entry its index. All entries
4475 must be collected into the table when this function is called,
4476 because the indexing code relies on htab_traverse to traverse nodes
4477 in the same order for each run. */
4480 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4482 addr_table_entry *node = *h;
4484 /* Don't index unreferenced nodes. */
4485 if (node->refcount == 0)
4486 return 1;
4488 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4489 node->index = *index;
4490 *index += 1;
4492 return 1;
4495 /* Add an address constant attribute value to a DIE. When using
4496 dwarf_split_debug_info, address attributes in dies destined for the
4497 final executable should be direct references--setting the parameter
4498 force_direct ensures this behavior. */
4500 static inline void
4501 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4502 bool force_direct)
4504 dw_attr_node attr;
4506 attr.dw_attr = attr_kind;
4507 attr.dw_attr_val.val_class = dw_val_class_addr;
4508 attr.dw_attr_val.v.val_addr = addr;
4509 if (dwarf_split_debug_info && !force_direct)
4510 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4511 else
4512 attr.dw_attr_val.val_entry = NULL;
4513 add_dwarf_attr (die, &attr);
4516 /* Get the RTX from to an address DIE attribute. */
4518 static inline rtx
4519 AT_addr (dw_attr_node *a)
4521 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4522 return a->dw_attr_val.v.val_addr;
4525 /* Add a file attribute value to a DIE. */
4527 static inline void
4528 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4529 struct dwarf_file_data *fd)
4531 dw_attr_node attr;
4533 attr.dw_attr = attr_kind;
4534 attr.dw_attr_val.val_class = dw_val_class_file;
4535 attr.dw_attr_val.val_entry = NULL;
4536 attr.dw_attr_val.v.val_file = fd;
4537 add_dwarf_attr (die, &attr);
4540 /* Get the dwarf_file_data from a file DIE attribute. */
4542 static inline struct dwarf_file_data *
4543 AT_file (dw_attr_node *a)
4545 gcc_assert (a && AT_class (a) == dw_val_class_file);
4546 return a->dw_attr_val.v.val_file;
4549 /* Add a vms delta attribute value to a DIE. */
4551 static inline void
4552 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4553 const char *lbl1, const char *lbl2)
4555 dw_attr_node attr;
4557 attr.dw_attr = attr_kind;
4558 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4559 attr.dw_attr_val.val_entry = NULL;
4560 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4561 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4562 add_dwarf_attr (die, &attr);
4565 /* Add a label identifier attribute value to a DIE. */
4567 static inline void
4568 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4569 const char *lbl_id)
4571 dw_attr_node attr;
4573 attr.dw_attr = attr_kind;
4574 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4575 attr.dw_attr_val.val_entry = NULL;
4576 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4577 if (dwarf_split_debug_info)
4578 attr.dw_attr_val.val_entry
4579 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4580 ate_kind_label);
4581 add_dwarf_attr (die, &attr);
4584 /* Add a section offset attribute value to a DIE, an offset into the
4585 debug_line section. */
4587 static inline void
4588 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4589 const char *label)
4591 dw_attr_node attr;
4593 attr.dw_attr = attr_kind;
4594 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4595 attr.dw_attr_val.val_entry = NULL;
4596 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4597 add_dwarf_attr (die, &attr);
4600 /* Add a section offset attribute value to a DIE, an offset into the
4601 debug_macinfo section. */
4603 static inline void
4604 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4605 const char *label)
4607 dw_attr_node attr;
4609 attr.dw_attr = attr_kind;
4610 attr.dw_attr_val.val_class = dw_val_class_macptr;
4611 attr.dw_attr_val.val_entry = NULL;
4612 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4613 add_dwarf_attr (die, &attr);
4616 /* Add an offset attribute value to a DIE. */
4618 static inline void
4619 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4620 unsigned HOST_WIDE_INT offset)
4622 dw_attr_node attr;
4624 attr.dw_attr = attr_kind;
4625 attr.dw_attr_val.val_class = dw_val_class_offset;
4626 attr.dw_attr_val.val_entry = NULL;
4627 attr.dw_attr_val.v.val_offset = offset;
4628 add_dwarf_attr (die, &attr);
4631 /* Add a range_list attribute value to a DIE. When using
4632 dwarf_split_debug_info, address attributes in dies destined for the
4633 final executable should be direct references--setting the parameter
4634 force_direct ensures this behavior. */
4636 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4637 #define RELOCATED_OFFSET (NULL)
4639 static void
4640 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4641 long unsigned int offset, bool force_direct)
4643 dw_attr_node attr;
4645 attr.dw_attr = attr_kind;
4646 attr.dw_attr_val.val_class = dw_val_class_range_list;
4647 /* For the range_list attribute, use val_entry to store whether the
4648 offset should follow split-debug-info or normal semantics. This
4649 value is read in output_range_list_offset. */
4650 if (dwarf_split_debug_info && !force_direct)
4651 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4652 else
4653 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4654 attr.dw_attr_val.v.val_offset = offset;
4655 add_dwarf_attr (die, &attr);
4658 /* Return the start label of a delta attribute. */
4660 static inline const char *
4661 AT_vms_delta1 (dw_attr_node *a)
4663 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4664 return a->dw_attr_val.v.val_vms_delta.lbl1;
4667 /* Return the end label of a delta attribute. */
4669 static inline const char *
4670 AT_vms_delta2 (dw_attr_node *a)
4672 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4673 return a->dw_attr_val.v.val_vms_delta.lbl2;
4676 static inline const char *
4677 AT_lbl (dw_attr_node *a)
4679 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4680 || AT_class (a) == dw_val_class_lineptr
4681 || AT_class (a) == dw_val_class_macptr
4682 || AT_class (a) == dw_val_class_high_pc));
4683 return a->dw_attr_val.v.val_lbl_id;
4686 /* Get the attribute of type attr_kind. */
4688 static dw_attr_node *
4689 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4691 dw_attr_node *a;
4692 unsigned ix;
4693 dw_die_ref spec = NULL;
4695 if (! die)
4696 return NULL;
4698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4699 if (a->dw_attr == attr_kind)
4700 return a;
4701 else if (a->dw_attr == DW_AT_specification
4702 || a->dw_attr == DW_AT_abstract_origin)
4703 spec = AT_ref (a);
4705 if (spec)
4706 return get_AT (spec, attr_kind);
4708 return NULL;
4711 /* Returns the parent of the declaration of DIE. */
4713 static dw_die_ref
4714 get_die_parent (dw_die_ref die)
4716 dw_die_ref t;
4718 if (!die)
4719 return NULL;
4721 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4722 || (t = get_AT_ref (die, DW_AT_specification)))
4723 die = t;
4725 return die->die_parent;
4728 /* Return the "low pc" attribute value, typically associated with a subprogram
4729 DIE. Return null if the "low pc" attribute is either not present, or if it
4730 cannot be represented as an assembler label identifier. */
4732 static inline const char *
4733 get_AT_low_pc (dw_die_ref die)
4735 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4737 return a ? AT_lbl (a) : NULL;
4740 /* Return the "high pc" attribute value, typically associated with a subprogram
4741 DIE. Return null if the "high pc" attribute is either not present, or if it
4742 cannot be represented as an assembler label identifier. */
4744 static inline const char *
4745 get_AT_hi_pc (dw_die_ref die)
4747 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4749 return a ? AT_lbl (a) : NULL;
4752 /* Return the value of the string attribute designated by ATTR_KIND, or
4753 NULL if it is not present. */
4755 static inline const char *
4756 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4758 dw_attr_node *a = get_AT (die, attr_kind);
4760 return a ? AT_string (a) : NULL;
4763 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4764 if it is not present. */
4766 static inline int
4767 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4769 dw_attr_node *a = get_AT (die, attr_kind);
4771 return a ? AT_flag (a) : 0;
4774 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4775 if it is not present. */
4777 static inline unsigned
4778 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4780 dw_attr_node *a = get_AT (die, attr_kind);
4782 return a ? AT_unsigned (a) : 0;
4785 static inline dw_die_ref
4786 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4788 dw_attr_node *a = get_AT (die, attr_kind);
4790 return a ? AT_ref (a) : NULL;
4793 static inline struct dwarf_file_data *
4794 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4796 dw_attr_node *a = get_AT (die, attr_kind);
4798 return a ? AT_file (a) : NULL;
4801 /* Return TRUE if the language is C++. */
4803 static inline bool
4804 is_cxx (void)
4806 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4808 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4809 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4812 /* Return TRUE if the language is Java. */
4814 static inline bool
4815 is_java (void)
4817 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4819 return lang == DW_LANG_Java;
4822 /* Return TRUE if the language is Fortran. */
4824 static inline bool
4825 is_fortran (void)
4827 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4829 return (lang == DW_LANG_Fortran77
4830 || lang == DW_LANG_Fortran90
4831 || lang == DW_LANG_Fortran95
4832 || lang == DW_LANG_Fortran03
4833 || lang == DW_LANG_Fortran08);
4836 /* Return TRUE if the language is Ada. */
4838 static inline bool
4839 is_ada (void)
4841 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4843 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4846 /* Remove the specified attribute if present. Return TRUE if removal
4847 was successful. */
4849 static bool
4850 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4852 dw_attr_node *a;
4853 unsigned ix;
4855 if (! die)
4856 return false;
4858 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4859 if (a->dw_attr == attr_kind)
4861 if (AT_class (a) == dw_val_class_str)
4862 if (a->dw_attr_val.v.val_str->refcount)
4863 a->dw_attr_val.v.val_str->refcount--;
4865 /* vec::ordered_remove should help reduce the number of abbrevs
4866 that are needed. */
4867 die->die_attr->ordered_remove (ix);
4868 return true;
4870 return false;
4873 /* Remove CHILD from its parent. PREV must have the property that
4874 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4876 static void
4877 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4879 gcc_assert (child->die_parent == prev->die_parent);
4880 gcc_assert (prev->die_sib == child);
4881 if (prev == child)
4883 gcc_assert (child->die_parent->die_child == child);
4884 prev = NULL;
4886 else
4887 prev->die_sib = child->die_sib;
4888 if (child->die_parent->die_child == child)
4889 child->die_parent->die_child = prev;
4892 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4893 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4895 static void
4896 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4898 dw_die_ref parent = old_child->die_parent;
4900 gcc_assert (parent == prev->die_parent);
4901 gcc_assert (prev->die_sib == old_child);
4903 new_child->die_parent = parent;
4904 if (prev == old_child)
4906 gcc_assert (parent->die_child == old_child);
4907 new_child->die_sib = new_child;
4909 else
4911 prev->die_sib = new_child;
4912 new_child->die_sib = old_child->die_sib;
4914 if (old_child->die_parent->die_child == old_child)
4915 old_child->die_parent->die_child = new_child;
4918 /* Move all children from OLD_PARENT to NEW_PARENT. */
4920 static void
4921 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4923 dw_die_ref c;
4924 new_parent->die_child = old_parent->die_child;
4925 old_parent->die_child = NULL;
4926 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4929 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4930 matches TAG. */
4932 static void
4933 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4935 dw_die_ref c;
4937 c = die->die_child;
4938 if (c) do {
4939 dw_die_ref prev = c;
4940 c = c->die_sib;
4941 while (c->die_tag == tag)
4943 remove_child_with_prev (c, prev);
4944 c->die_parent = NULL;
4945 /* Might have removed every child. */
4946 if (c == c->die_sib)
4947 return;
4948 c = c->die_sib;
4950 } while (c != die->die_child);
4953 /* Add a CHILD_DIE as the last child of DIE. */
4955 static void
4956 add_child_die (dw_die_ref die, dw_die_ref child_die)
4958 /* FIXME this should probably be an assert. */
4959 if (! die || ! child_die)
4960 return;
4961 gcc_assert (die != child_die);
4963 child_die->die_parent = die;
4964 if (die->die_child)
4966 child_die->die_sib = die->die_child->die_sib;
4967 die->die_child->die_sib = child_die;
4969 else
4970 child_die->die_sib = child_die;
4971 die->die_child = child_die;
4974 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
4976 static void
4977 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4978 dw_die_ref after_die)
4980 gcc_assert (die
4981 && child_die
4982 && after_die
4983 && die->die_child
4984 && die != child_die);
4986 child_die->die_parent = die;
4987 child_die->die_sib = after_die->die_sib;
4988 after_die->die_sib = child_die;
4989 if (die->die_child == after_die)
4990 die->die_child = child_die;
4993 /* Unassociate CHILD from its parent, and make its parent be
4994 NEW_PARENT. */
4996 static void
4997 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4999 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5000 if (p->die_sib == child)
5002 remove_child_with_prev (child, p);
5003 break;
5005 add_child_die (new_parent, child);
5008 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5009 is the specification, to the end of PARENT's list of children.
5010 This is done by removing and re-adding it. */
5012 static void
5013 splice_child_die (dw_die_ref parent, dw_die_ref child)
5015 /* We want the declaration DIE from inside the class, not the
5016 specification DIE at toplevel. */
5017 if (child->die_parent != parent)
5019 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5021 if (tmp)
5022 child = tmp;
5025 gcc_assert (child->die_parent == parent
5026 || (child->die_parent
5027 == get_AT_ref (parent, DW_AT_specification)));
5029 reparent_child (child, parent);
5032 /* Create and return a new die with a parent of PARENT_DIE. If
5033 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5034 associated tree T must be supplied to determine parenthood
5035 later. */
5037 static inline dw_die_ref
5038 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5040 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5042 die->die_tag = tag_value;
5044 if (parent_die != NULL)
5045 add_child_die (parent_die, die);
5046 else
5048 limbo_die_node *limbo_node;
5050 /* No DIEs created after early dwarf should end up in limbo,
5051 because the limbo list should not persist past LTO
5052 streaming. */
5053 if (tag_value != DW_TAG_compile_unit
5054 /* These are allowed because they're generated while
5055 breaking out COMDAT units late. */
5056 && tag_value != DW_TAG_type_unit
5057 && !early_dwarf
5058 /* Allow nested functions to live in limbo because they will
5059 only temporarily live there, as decls_for_scope will fix
5060 them up. */
5061 && (TREE_CODE (t) != FUNCTION_DECL
5062 || !decl_function_context (t))
5063 /* Same as nested functions above but for types. Types that
5064 are local to a function will be fixed in
5065 decls_for_scope. */
5066 && (!RECORD_OR_UNION_TYPE_P (t)
5067 || !TYPE_CONTEXT (t)
5068 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5069 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5070 especially in the ltrans stage, but once we implement LTO
5071 dwarf streaming, we should remove this exception. */
5072 && !in_lto_p)
5074 fprintf (stderr, "symbol ended up in limbo too late:");
5075 debug_generic_stmt (t);
5076 gcc_unreachable ();
5079 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5080 limbo_node->die = die;
5081 limbo_node->created_for = t;
5082 limbo_node->next = limbo_die_list;
5083 limbo_die_list = limbo_node;
5086 return die;
5089 /* Return the DIE associated with the given type specifier. */
5091 static inline dw_die_ref
5092 lookup_type_die (tree type)
5094 return TYPE_SYMTAB_DIE (type);
5097 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5098 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5099 anonymous type instead the one of the naming typedef. */
5101 static inline dw_die_ref
5102 strip_naming_typedef (tree type, dw_die_ref type_die)
5104 if (type
5105 && TREE_CODE (type) == RECORD_TYPE
5106 && type_die
5107 && type_die->die_tag == DW_TAG_typedef
5108 && is_naming_typedef_decl (TYPE_NAME (type)))
5109 type_die = get_AT_ref (type_die, DW_AT_type);
5110 return type_die;
5113 /* Like lookup_type_die, but if type is an anonymous type named by a
5114 typedef[1], return the DIE of the anonymous type instead the one of
5115 the naming typedef. This is because in gen_typedef_die, we did
5116 equate the anonymous struct named by the typedef with the DIE of
5117 the naming typedef. So by default, lookup_type_die on an anonymous
5118 struct yields the DIE of the naming typedef.
5120 [1]: Read the comment of is_naming_typedef_decl to learn about what
5121 a naming typedef is. */
5123 static inline dw_die_ref
5124 lookup_type_die_strip_naming_typedef (tree type)
5126 dw_die_ref die = lookup_type_die (type);
5127 return strip_naming_typedef (type, die);
5130 /* Equate a DIE to a given type specifier. */
5132 static inline void
5133 equate_type_number_to_die (tree type, dw_die_ref type_die)
5135 TYPE_SYMTAB_DIE (type) = type_die;
5138 /* Returns a hash value for X (which really is a die_struct). */
5140 inline hashval_t
5141 decl_die_hasher::hash (die_node *x)
5143 return (hashval_t) x->decl_id;
5146 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5148 inline bool
5149 decl_die_hasher::equal (die_node *x, tree y)
5151 return (x->decl_id == DECL_UID (y));
5154 /* Return the DIE associated with a given declaration. */
5156 static inline dw_die_ref
5157 lookup_decl_die (tree decl)
5159 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5162 /* Returns a hash value for X (which really is a var_loc_list). */
5164 inline hashval_t
5165 decl_loc_hasher::hash (var_loc_list *x)
5167 return (hashval_t) x->decl_id;
5170 /* Return nonzero if decl_id of var_loc_list X is the same as
5171 UID of decl *Y. */
5173 inline bool
5174 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5176 return (x->decl_id == DECL_UID (y));
5179 /* Return the var_loc list associated with a given declaration. */
5181 static inline var_loc_list *
5182 lookup_decl_loc (const_tree decl)
5184 if (!decl_loc_table)
5185 return NULL;
5186 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5189 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5191 inline hashval_t
5192 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5194 return (hashval_t) x->decl_id;
5197 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5198 UID of decl *Y. */
5200 inline bool
5201 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5203 return (x->decl_id == DECL_UID (y));
5206 /* Equate a DIE to a particular declaration. */
5208 static void
5209 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5211 unsigned int decl_id = DECL_UID (decl);
5213 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5214 decl_die->decl_id = decl_id;
5217 /* Return how many bits covers PIECE EXPR_LIST. */
5219 static HOST_WIDE_INT
5220 decl_piece_bitsize (rtx piece)
5222 int ret = (int) GET_MODE (piece);
5223 if (ret)
5224 return ret;
5225 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5226 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5227 return INTVAL (XEXP (XEXP (piece, 0), 0));
5230 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5232 static rtx *
5233 decl_piece_varloc_ptr (rtx piece)
5235 if ((int) GET_MODE (piece))
5236 return &XEXP (piece, 0);
5237 else
5238 return &XEXP (XEXP (piece, 0), 1);
5241 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5242 Next is the chain of following piece nodes. */
5244 static rtx_expr_list *
5245 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5247 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5248 return alloc_EXPR_LIST (bitsize, loc_note, next);
5249 else
5250 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5251 GEN_INT (bitsize),
5252 loc_note), next);
5255 /* Return rtx that should be stored into loc field for
5256 LOC_NOTE and BITPOS/BITSIZE. */
5258 static rtx
5259 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5260 HOST_WIDE_INT bitsize)
5262 if (bitsize != -1)
5264 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5265 if (bitpos != 0)
5266 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5268 return loc_note;
5271 /* This function either modifies location piece list *DEST in
5272 place (if SRC and INNER is NULL), or copies location piece list
5273 *SRC to *DEST while modifying it. Location BITPOS is modified
5274 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5275 not copied and if needed some padding around it is added.
5276 When modifying in place, DEST should point to EXPR_LIST where
5277 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5278 to the start of the whole list and INNER points to the EXPR_LIST
5279 where earlier pieces cover PIECE_BITPOS bits. */
5281 static void
5282 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5283 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5284 HOST_WIDE_INT bitsize, rtx loc_note)
5286 HOST_WIDE_INT diff;
5287 bool copy = inner != NULL;
5289 if (copy)
5291 /* First copy all nodes preceding the current bitpos. */
5292 while (src != inner)
5294 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5295 decl_piece_bitsize (*src), NULL_RTX);
5296 dest = &XEXP (*dest, 1);
5297 src = &XEXP (*src, 1);
5300 /* Add padding if needed. */
5301 if (bitpos != piece_bitpos)
5303 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5304 copy ? NULL_RTX : *dest);
5305 dest = &XEXP (*dest, 1);
5307 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5309 gcc_assert (!copy);
5310 /* A piece with correct bitpos and bitsize already exist,
5311 just update the location for it and return. */
5312 *decl_piece_varloc_ptr (*dest) = loc_note;
5313 return;
5315 /* Add the piece that changed. */
5316 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5317 dest = &XEXP (*dest, 1);
5318 /* Skip over pieces that overlap it. */
5319 diff = bitpos - piece_bitpos + bitsize;
5320 if (!copy)
5321 src = dest;
5322 while (diff > 0 && *src)
5324 rtx piece = *src;
5325 diff -= decl_piece_bitsize (piece);
5326 if (copy)
5327 src = &XEXP (piece, 1);
5328 else
5330 *src = XEXP (piece, 1);
5331 free_EXPR_LIST_node (piece);
5334 /* Add padding if needed. */
5335 if (diff < 0 && *src)
5337 if (!copy)
5338 dest = src;
5339 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5340 dest = &XEXP (*dest, 1);
5342 if (!copy)
5343 return;
5344 /* Finally copy all nodes following it. */
5345 while (*src)
5347 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5348 decl_piece_bitsize (*src), NULL_RTX);
5349 dest = &XEXP (*dest, 1);
5350 src = &XEXP (*src, 1);
5354 /* Add a variable location node to the linked list for DECL. */
5356 static struct var_loc_node *
5357 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5359 unsigned int decl_id;
5360 var_loc_list *temp;
5361 struct var_loc_node *loc = NULL;
5362 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5364 if (TREE_CODE (decl) == VAR_DECL
5365 && DECL_HAS_DEBUG_EXPR_P (decl))
5367 tree realdecl = DECL_DEBUG_EXPR (decl);
5368 if (handled_component_p (realdecl)
5369 || (TREE_CODE (realdecl) == MEM_REF
5370 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5372 HOST_WIDE_INT maxsize;
5373 bool reverse;
5374 tree innerdecl
5375 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5376 &reverse);
5377 if (!DECL_P (innerdecl)
5378 || DECL_IGNORED_P (innerdecl)
5379 || TREE_STATIC (innerdecl)
5380 || bitsize <= 0
5381 || bitpos + bitsize > 256
5382 || bitsize != maxsize)
5383 return NULL;
5384 decl = innerdecl;
5388 decl_id = DECL_UID (decl);
5389 var_loc_list **slot
5390 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5391 if (*slot == NULL)
5393 temp = ggc_cleared_alloc<var_loc_list> ();
5394 temp->decl_id = decl_id;
5395 *slot = temp;
5397 else
5398 temp = *slot;
5400 /* For PARM_DECLs try to keep around the original incoming value,
5401 even if that means we'll emit a zero-range .debug_loc entry. */
5402 if (temp->last
5403 && temp->first == temp->last
5404 && TREE_CODE (decl) == PARM_DECL
5405 && NOTE_P (temp->first->loc)
5406 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5407 && DECL_INCOMING_RTL (decl)
5408 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5409 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5410 == GET_CODE (DECL_INCOMING_RTL (decl))
5411 && prev_real_insn (temp->first->loc) == NULL_RTX
5412 && (bitsize != -1
5413 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5414 NOTE_VAR_LOCATION_LOC (loc_note))
5415 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5416 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5418 loc = ggc_cleared_alloc<var_loc_node> ();
5419 temp->first->next = loc;
5420 temp->last = loc;
5421 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5423 else if (temp->last)
5425 struct var_loc_node *last = temp->last, *unused = NULL;
5426 rtx *piece_loc = NULL, last_loc_note;
5427 HOST_WIDE_INT piece_bitpos = 0;
5428 if (last->next)
5430 last = last->next;
5431 gcc_assert (last->next == NULL);
5433 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5435 piece_loc = &last->loc;
5438 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5439 if (piece_bitpos + cur_bitsize > bitpos)
5440 break;
5441 piece_bitpos += cur_bitsize;
5442 piece_loc = &XEXP (*piece_loc, 1);
5444 while (*piece_loc);
5446 /* TEMP->LAST here is either pointer to the last but one or
5447 last element in the chained list, LAST is pointer to the
5448 last element. */
5449 if (label && strcmp (last->label, label) == 0)
5451 /* For SRA optimized variables if there weren't any real
5452 insns since last note, just modify the last node. */
5453 if (piece_loc != NULL)
5455 adjust_piece_list (piece_loc, NULL, NULL,
5456 bitpos, piece_bitpos, bitsize, loc_note);
5457 return NULL;
5459 /* If the last note doesn't cover any instructions, remove it. */
5460 if (temp->last != last)
5462 temp->last->next = NULL;
5463 unused = last;
5464 last = temp->last;
5465 gcc_assert (strcmp (last->label, label) != 0);
5467 else
5469 gcc_assert (temp->first == temp->last
5470 || (temp->first->next == temp->last
5471 && TREE_CODE (decl) == PARM_DECL));
5472 memset (temp->last, '\0', sizeof (*temp->last));
5473 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5474 return temp->last;
5477 if (bitsize == -1 && NOTE_P (last->loc))
5478 last_loc_note = last->loc;
5479 else if (piece_loc != NULL
5480 && *piece_loc != NULL_RTX
5481 && piece_bitpos == bitpos
5482 && decl_piece_bitsize (*piece_loc) == bitsize)
5483 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5484 else
5485 last_loc_note = NULL_RTX;
5486 /* If the current location is the same as the end of the list,
5487 and either both or neither of the locations is uninitialized,
5488 we have nothing to do. */
5489 if (last_loc_note == NULL_RTX
5490 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5491 NOTE_VAR_LOCATION_LOC (loc_note)))
5492 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5493 != NOTE_VAR_LOCATION_STATUS (loc_note))
5494 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5495 == VAR_INIT_STATUS_UNINITIALIZED)
5496 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5497 == VAR_INIT_STATUS_UNINITIALIZED))))
5499 /* Add LOC to the end of list and update LAST. If the last
5500 element of the list has been removed above, reuse its
5501 memory for the new node, otherwise allocate a new one. */
5502 if (unused)
5504 loc = unused;
5505 memset (loc, '\0', sizeof (*loc));
5507 else
5508 loc = ggc_cleared_alloc<var_loc_node> ();
5509 if (bitsize == -1 || piece_loc == NULL)
5510 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5511 else
5512 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5513 bitpos, piece_bitpos, bitsize, loc_note);
5514 last->next = loc;
5515 /* Ensure TEMP->LAST will point either to the new last but one
5516 element of the chain, or to the last element in it. */
5517 if (last != temp->last)
5518 temp->last = last;
5520 else if (unused)
5521 ggc_free (unused);
5523 else
5525 loc = ggc_cleared_alloc<var_loc_node> ();
5526 temp->first = loc;
5527 temp->last = loc;
5528 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5530 return loc;
5533 /* Keep track of the number of spaces used to indent the
5534 output of the debugging routines that print the structure of
5535 the DIE internal representation. */
5536 static int print_indent;
5538 /* Indent the line the number of spaces given by print_indent. */
5540 static inline void
5541 print_spaces (FILE *outfile)
5543 fprintf (outfile, "%*s", print_indent, "");
5546 /* Print a type signature in hex. */
5548 static inline void
5549 print_signature (FILE *outfile, char *sig)
5551 int i;
5553 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5554 fprintf (outfile, "%02x", sig[i] & 0xff);
5557 static inline void
5558 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5560 if (discr_value->pos)
5561 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5562 else
5563 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5566 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5568 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5569 RECURSE, output location descriptor operations. */
5571 static void
5572 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5574 switch (val->val_class)
5576 case dw_val_class_addr:
5577 fprintf (outfile, "address");
5578 break;
5579 case dw_val_class_offset:
5580 fprintf (outfile, "offset");
5581 break;
5582 case dw_val_class_loc:
5583 fprintf (outfile, "location descriptor");
5584 if (val->v.val_loc == NULL)
5585 fprintf (outfile, " -> <null>\n");
5586 else if (recurse)
5588 fprintf (outfile, ":\n");
5589 print_indent += 4;
5590 print_loc_descr (val->v.val_loc, outfile);
5591 print_indent -= 4;
5593 else
5594 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5595 break;
5596 case dw_val_class_loc_list:
5597 fprintf (outfile, "location list -> label:%s",
5598 val->v.val_loc_list->ll_symbol);
5599 break;
5600 case dw_val_class_range_list:
5601 fprintf (outfile, "range list");
5602 break;
5603 case dw_val_class_const:
5604 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5605 break;
5606 case dw_val_class_unsigned_const:
5607 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5608 break;
5609 case dw_val_class_const_double:
5610 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5611 HOST_WIDE_INT_PRINT_UNSIGNED")",
5612 val->v.val_double.high,
5613 val->v.val_double.low);
5614 break;
5615 case dw_val_class_wide_int:
5617 int i = val->v.val_wide->get_len ();
5618 fprintf (outfile, "constant (");
5619 gcc_assert (i > 0);
5620 if (val->v.val_wide->elt (i - 1) == 0)
5621 fprintf (outfile, "0x");
5622 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5623 val->v.val_wide->elt (--i));
5624 while (--i >= 0)
5625 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5626 val->v.val_wide->elt (i));
5627 fprintf (outfile, ")");
5628 break;
5630 case dw_val_class_vec:
5631 fprintf (outfile, "floating-point or vector constant");
5632 break;
5633 case dw_val_class_flag:
5634 fprintf (outfile, "%u", val->v.val_flag);
5635 break;
5636 case dw_val_class_die_ref:
5637 if (val->v.val_die_ref.die != NULL)
5639 dw_die_ref die = val->v.val_die_ref.die;
5641 if (die->comdat_type_p)
5643 fprintf (outfile, "die -> signature: ");
5644 print_signature (outfile,
5645 die->die_id.die_type_node->signature);
5647 else if (die->die_id.die_symbol)
5648 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5649 else
5650 fprintf (outfile, "die -> %ld", die->die_offset);
5651 fprintf (outfile, " (%p)", (void *) die);
5653 else
5654 fprintf (outfile, "die -> <null>");
5655 break;
5656 case dw_val_class_vms_delta:
5657 fprintf (outfile, "delta: @slotcount(%s-%s)",
5658 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5659 break;
5660 case dw_val_class_lbl_id:
5661 case dw_val_class_lineptr:
5662 case dw_val_class_macptr:
5663 case dw_val_class_high_pc:
5664 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5665 break;
5666 case dw_val_class_str:
5667 if (val->v.val_str->str != NULL)
5668 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5669 else
5670 fprintf (outfile, "<null>");
5671 break;
5672 case dw_val_class_file:
5673 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5674 val->v.val_file->emitted_number);
5675 break;
5676 case dw_val_class_data8:
5678 int i;
5680 for (i = 0; i < 8; i++)
5681 fprintf (outfile, "%02x", val->v.val_data8[i]);
5682 break;
5684 case dw_val_class_discr_value:
5685 print_discr_value (outfile, &val->v.val_discr_value);
5686 break;
5687 case dw_val_class_discr_list:
5688 for (dw_discr_list_ref node = val->v.val_discr_list;
5689 node != NULL;
5690 node = node->dw_discr_next)
5692 if (node->dw_discr_range)
5694 fprintf (outfile, " .. ");
5695 print_discr_value (outfile, &node->dw_discr_lower_bound);
5696 print_discr_value (outfile, &node->dw_discr_upper_bound);
5698 else
5699 print_discr_value (outfile, &node->dw_discr_lower_bound);
5701 if (node->dw_discr_next != NULL)
5702 fprintf (outfile, " | ");
5704 default:
5705 break;
5709 /* Likewise, for a DIE attribute. */
5711 static void
5712 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5714 print_dw_val (&a->dw_attr_val, recurse, outfile);
5718 /* Print the list of operands in the LOC location description to OUTFILE. This
5719 routine is a debugging aid only. */
5721 static void
5722 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5724 dw_loc_descr_ref l = loc;
5726 if (loc == NULL)
5728 print_spaces (outfile);
5729 fprintf (outfile, "<null>\n");
5730 return;
5733 for (l = loc; l != NULL; l = l->dw_loc_next)
5735 print_spaces (outfile);
5736 fprintf (outfile, "(%p) %s",
5737 (void *) l,
5738 dwarf_stack_op_name (l->dw_loc_opc));
5739 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5741 fprintf (outfile, " ");
5742 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5744 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5746 fprintf (outfile, ", ");
5747 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5749 fprintf (outfile, "\n");
5753 /* Print the information associated with a given DIE, and its children.
5754 This routine is a debugging aid only. */
5756 static void
5757 print_die (dw_die_ref die, FILE *outfile)
5759 dw_attr_node *a;
5760 dw_die_ref c;
5761 unsigned ix;
5763 print_spaces (outfile);
5764 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5765 die->die_offset, dwarf_tag_name (die->die_tag),
5766 (void*) die);
5767 print_spaces (outfile);
5768 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5769 fprintf (outfile, " offset: %ld", die->die_offset);
5770 fprintf (outfile, " mark: %d\n", die->die_mark);
5772 if (die->comdat_type_p)
5774 print_spaces (outfile);
5775 fprintf (outfile, " signature: ");
5776 print_signature (outfile, die->die_id.die_type_node->signature);
5777 fprintf (outfile, "\n");
5780 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5782 print_spaces (outfile);
5783 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5785 print_attribute (a, true, outfile);
5786 fprintf (outfile, "\n");
5789 if (die->die_child != NULL)
5791 print_indent += 4;
5792 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5793 print_indent -= 4;
5795 if (print_indent == 0)
5796 fprintf (outfile, "\n");
5799 /* Print the list of operations in the LOC location description. */
5801 DEBUG_FUNCTION void
5802 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5804 print_loc_descr (loc, stderr);
5807 /* Print the information collected for a given DIE. */
5809 DEBUG_FUNCTION void
5810 debug_dwarf_die (dw_die_ref die)
5812 print_die (die, stderr);
5815 DEBUG_FUNCTION void
5816 debug (die_struct &ref)
5818 print_die (&ref, stderr);
5821 DEBUG_FUNCTION void
5822 debug (die_struct *ptr)
5824 if (ptr)
5825 debug (*ptr);
5826 else
5827 fprintf (stderr, "<nil>\n");
5831 /* Print all DWARF information collected for the compilation unit.
5832 This routine is a debugging aid only. */
5834 DEBUG_FUNCTION void
5835 debug_dwarf (void)
5837 print_indent = 0;
5838 print_die (comp_unit_die (), stderr);
5841 /* Sanity checks on DIEs. */
5843 static void
5844 check_die (dw_die_ref die)
5846 unsigned ix;
5847 dw_attr_node *a;
5848 bool inline_found = false;
5849 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5850 int n_decl_line = 0, n_decl_file = 0;
5851 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5853 switch (a->dw_attr)
5855 case DW_AT_inline:
5856 if (a->dw_attr_val.v.val_unsigned)
5857 inline_found = true;
5858 break;
5859 case DW_AT_location:
5860 ++n_location;
5861 break;
5862 case DW_AT_low_pc:
5863 ++n_low_pc;
5864 break;
5865 case DW_AT_high_pc:
5866 ++n_high_pc;
5867 break;
5868 case DW_AT_artificial:
5869 ++n_artificial;
5870 break;
5871 case DW_AT_decl_line:
5872 ++n_decl_line;
5873 break;
5874 case DW_AT_decl_file:
5875 ++n_decl_file;
5876 break;
5877 default:
5878 break;
5881 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5882 || n_decl_line > 1 || n_decl_file > 1)
5884 fprintf (stderr, "Duplicate attributes in DIE:\n");
5885 debug_dwarf_die (die);
5886 gcc_unreachable ();
5888 if (inline_found)
5890 /* A debugging information entry that is a member of an abstract
5891 instance tree [that has DW_AT_inline] should not contain any
5892 attributes which describe aspects of the subroutine which vary
5893 between distinct inlined expansions or distinct out-of-line
5894 expansions. */
5895 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5896 gcc_assert (a->dw_attr != DW_AT_low_pc
5897 && a->dw_attr != DW_AT_high_pc
5898 && a->dw_attr != DW_AT_location
5899 && a->dw_attr != DW_AT_frame_base
5900 && a->dw_attr != DW_AT_GNU_all_call_sites);
5904 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5905 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5906 DIE that marks the start of the DIEs for this include file. */
5908 static dw_die_ref
5909 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5911 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5912 dw_die_ref new_unit = gen_compile_unit_die (filename);
5914 new_unit->die_sib = old_unit;
5915 return new_unit;
5918 /* Close an include-file CU and reopen the enclosing one. */
5920 static dw_die_ref
5921 pop_compile_unit (dw_die_ref old_unit)
5923 dw_die_ref new_unit = old_unit->die_sib;
5925 old_unit->die_sib = NULL;
5926 return new_unit;
5929 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5930 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5931 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5933 /* Calculate the checksum of a location expression. */
5935 static inline void
5936 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5938 int tem;
5939 inchash::hash hstate;
5940 hashval_t hash;
5942 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5943 CHECKSUM (tem);
5944 hash_loc_operands (loc, hstate);
5945 hash = hstate.end();
5946 CHECKSUM (hash);
5949 /* Calculate the checksum of an attribute. */
5951 static void
5952 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5954 dw_loc_descr_ref loc;
5955 rtx r;
5957 CHECKSUM (at->dw_attr);
5959 /* We don't care that this was compiled with a different compiler
5960 snapshot; if the output is the same, that's what matters. */
5961 if (at->dw_attr == DW_AT_producer)
5962 return;
5964 switch (AT_class (at))
5966 case dw_val_class_const:
5967 CHECKSUM (at->dw_attr_val.v.val_int);
5968 break;
5969 case dw_val_class_unsigned_const:
5970 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5971 break;
5972 case dw_val_class_const_double:
5973 CHECKSUM (at->dw_attr_val.v.val_double);
5974 break;
5975 case dw_val_class_wide_int:
5976 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5977 get_full_len (*at->dw_attr_val.v.val_wide)
5978 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5979 break;
5980 case dw_val_class_vec:
5981 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5982 (at->dw_attr_val.v.val_vec.length
5983 * at->dw_attr_val.v.val_vec.elt_size));
5984 break;
5985 case dw_val_class_flag:
5986 CHECKSUM (at->dw_attr_val.v.val_flag);
5987 break;
5988 case dw_val_class_str:
5989 CHECKSUM_STRING (AT_string (at));
5990 break;
5992 case dw_val_class_addr:
5993 r = AT_addr (at);
5994 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5995 CHECKSUM_STRING (XSTR (r, 0));
5996 break;
5998 case dw_val_class_offset:
5999 CHECKSUM (at->dw_attr_val.v.val_offset);
6000 break;
6002 case dw_val_class_loc:
6003 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6004 loc_checksum (loc, ctx);
6005 break;
6007 case dw_val_class_die_ref:
6008 die_checksum (AT_ref (at), ctx, mark);
6009 break;
6011 case dw_val_class_fde_ref:
6012 case dw_val_class_vms_delta:
6013 case dw_val_class_lbl_id:
6014 case dw_val_class_lineptr:
6015 case dw_val_class_macptr:
6016 case dw_val_class_high_pc:
6017 break;
6019 case dw_val_class_file:
6020 CHECKSUM_STRING (AT_file (at)->filename);
6021 break;
6023 case dw_val_class_data8:
6024 CHECKSUM (at->dw_attr_val.v.val_data8);
6025 break;
6027 default:
6028 break;
6032 /* Calculate the checksum of a DIE. */
6034 static void
6035 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6037 dw_die_ref c;
6038 dw_attr_node *a;
6039 unsigned ix;
6041 /* To avoid infinite recursion. */
6042 if (die->die_mark)
6044 CHECKSUM (die->die_mark);
6045 return;
6047 die->die_mark = ++(*mark);
6049 CHECKSUM (die->die_tag);
6051 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6052 attr_checksum (a, ctx, mark);
6054 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6057 #undef CHECKSUM
6058 #undef CHECKSUM_BLOCK
6059 #undef CHECKSUM_STRING
6061 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6062 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6063 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6064 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6065 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6066 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6067 #define CHECKSUM_ATTR(FOO) \
6068 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6070 /* Calculate the checksum of a number in signed LEB128 format. */
6072 static void
6073 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6075 unsigned char byte;
6076 bool more;
6078 while (1)
6080 byte = (value & 0x7f);
6081 value >>= 7;
6082 more = !((value == 0 && (byte & 0x40) == 0)
6083 || (value == -1 && (byte & 0x40) != 0));
6084 if (more)
6085 byte |= 0x80;
6086 CHECKSUM (byte);
6087 if (!more)
6088 break;
6092 /* Calculate the checksum of a number in unsigned LEB128 format. */
6094 static void
6095 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6097 while (1)
6099 unsigned char byte = (value & 0x7f);
6100 value >>= 7;
6101 if (value != 0)
6102 /* More bytes to follow. */
6103 byte |= 0x80;
6104 CHECKSUM (byte);
6105 if (value == 0)
6106 break;
6110 /* Checksum the context of the DIE. This adds the names of any
6111 surrounding namespaces or structures to the checksum. */
6113 static void
6114 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6116 const char *name;
6117 dw_die_ref spec;
6118 int tag = die->die_tag;
6120 if (tag != DW_TAG_namespace
6121 && tag != DW_TAG_structure_type
6122 && tag != DW_TAG_class_type)
6123 return;
6125 name = get_AT_string (die, DW_AT_name);
6127 spec = get_AT_ref (die, DW_AT_specification);
6128 if (spec != NULL)
6129 die = spec;
6131 if (die->die_parent != NULL)
6132 checksum_die_context (die->die_parent, ctx);
6134 CHECKSUM_ULEB128 ('C');
6135 CHECKSUM_ULEB128 (tag);
6136 if (name != NULL)
6137 CHECKSUM_STRING (name);
6140 /* Calculate the checksum of a location expression. */
6142 static inline void
6143 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6145 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6146 were emitted as a DW_FORM_sdata instead of a location expression. */
6147 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6149 CHECKSUM_ULEB128 (DW_FORM_sdata);
6150 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6151 return;
6154 /* Otherwise, just checksum the raw location expression. */
6155 while (loc != NULL)
6157 inchash::hash hstate;
6158 hashval_t hash;
6160 CHECKSUM_ULEB128 (loc->dtprel);
6161 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6162 hash_loc_operands (loc, hstate);
6163 hash = hstate.end ();
6164 CHECKSUM (hash);
6165 loc = loc->dw_loc_next;
6169 /* Calculate the checksum of an attribute. */
6171 static void
6172 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6173 struct md5_ctx *ctx, int *mark)
6175 dw_loc_descr_ref loc;
6176 rtx r;
6178 if (AT_class (at) == dw_val_class_die_ref)
6180 dw_die_ref target_die = AT_ref (at);
6182 /* For pointer and reference types, we checksum only the (qualified)
6183 name of the target type (if there is a name). For friend entries,
6184 we checksum only the (qualified) name of the target type or function.
6185 This allows the checksum to remain the same whether the target type
6186 is complete or not. */
6187 if ((at->dw_attr == DW_AT_type
6188 && (tag == DW_TAG_pointer_type
6189 || tag == DW_TAG_reference_type
6190 || tag == DW_TAG_rvalue_reference_type
6191 || tag == DW_TAG_ptr_to_member_type))
6192 || (at->dw_attr == DW_AT_friend
6193 && tag == DW_TAG_friend))
6195 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6197 if (name_attr != NULL)
6199 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6201 if (decl == NULL)
6202 decl = target_die;
6203 CHECKSUM_ULEB128 ('N');
6204 CHECKSUM_ULEB128 (at->dw_attr);
6205 if (decl->die_parent != NULL)
6206 checksum_die_context (decl->die_parent, ctx);
6207 CHECKSUM_ULEB128 ('E');
6208 CHECKSUM_STRING (AT_string (name_attr));
6209 return;
6213 /* For all other references to another DIE, we check to see if the
6214 target DIE has already been visited. If it has, we emit a
6215 backward reference; if not, we descend recursively. */
6216 if (target_die->die_mark > 0)
6218 CHECKSUM_ULEB128 ('R');
6219 CHECKSUM_ULEB128 (at->dw_attr);
6220 CHECKSUM_ULEB128 (target_die->die_mark);
6222 else
6224 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6226 if (decl == NULL)
6227 decl = target_die;
6228 target_die->die_mark = ++(*mark);
6229 CHECKSUM_ULEB128 ('T');
6230 CHECKSUM_ULEB128 (at->dw_attr);
6231 if (decl->die_parent != NULL)
6232 checksum_die_context (decl->die_parent, ctx);
6233 die_checksum_ordered (target_die, ctx, mark);
6235 return;
6238 CHECKSUM_ULEB128 ('A');
6239 CHECKSUM_ULEB128 (at->dw_attr);
6241 switch (AT_class (at))
6243 case dw_val_class_const:
6244 CHECKSUM_ULEB128 (DW_FORM_sdata);
6245 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6246 break;
6248 case dw_val_class_unsigned_const:
6249 CHECKSUM_ULEB128 (DW_FORM_sdata);
6250 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6251 break;
6253 case dw_val_class_const_double:
6254 CHECKSUM_ULEB128 (DW_FORM_block);
6255 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6256 CHECKSUM (at->dw_attr_val.v.val_double);
6257 break;
6259 case dw_val_class_wide_int:
6260 CHECKSUM_ULEB128 (DW_FORM_block);
6261 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6262 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6263 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6264 get_full_len (*at->dw_attr_val.v.val_wide)
6265 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6266 break;
6268 case dw_val_class_vec:
6269 CHECKSUM_ULEB128 (DW_FORM_block);
6270 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6271 * at->dw_attr_val.v.val_vec.elt_size);
6272 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6273 (at->dw_attr_val.v.val_vec.length
6274 * at->dw_attr_val.v.val_vec.elt_size));
6275 break;
6277 case dw_val_class_flag:
6278 CHECKSUM_ULEB128 (DW_FORM_flag);
6279 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6280 break;
6282 case dw_val_class_str:
6283 CHECKSUM_ULEB128 (DW_FORM_string);
6284 CHECKSUM_STRING (AT_string (at));
6285 break;
6287 case dw_val_class_addr:
6288 r = AT_addr (at);
6289 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6290 CHECKSUM_ULEB128 (DW_FORM_string);
6291 CHECKSUM_STRING (XSTR (r, 0));
6292 break;
6294 case dw_val_class_offset:
6295 CHECKSUM_ULEB128 (DW_FORM_sdata);
6296 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6297 break;
6299 case dw_val_class_loc:
6300 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6301 loc_checksum_ordered (loc, ctx);
6302 break;
6304 case dw_val_class_fde_ref:
6305 case dw_val_class_lbl_id:
6306 case dw_val_class_lineptr:
6307 case dw_val_class_macptr:
6308 case dw_val_class_high_pc:
6309 break;
6311 case dw_val_class_file:
6312 CHECKSUM_ULEB128 (DW_FORM_string);
6313 CHECKSUM_STRING (AT_file (at)->filename);
6314 break;
6316 case dw_val_class_data8:
6317 CHECKSUM (at->dw_attr_val.v.val_data8);
6318 break;
6320 default:
6321 break;
6325 struct checksum_attributes
6327 dw_attr_node *at_name;
6328 dw_attr_node *at_type;
6329 dw_attr_node *at_friend;
6330 dw_attr_node *at_accessibility;
6331 dw_attr_node *at_address_class;
6332 dw_attr_node *at_allocated;
6333 dw_attr_node *at_artificial;
6334 dw_attr_node *at_associated;
6335 dw_attr_node *at_binary_scale;
6336 dw_attr_node *at_bit_offset;
6337 dw_attr_node *at_bit_size;
6338 dw_attr_node *at_bit_stride;
6339 dw_attr_node *at_byte_size;
6340 dw_attr_node *at_byte_stride;
6341 dw_attr_node *at_const_value;
6342 dw_attr_node *at_containing_type;
6343 dw_attr_node *at_count;
6344 dw_attr_node *at_data_location;
6345 dw_attr_node *at_data_member_location;
6346 dw_attr_node *at_decimal_scale;
6347 dw_attr_node *at_decimal_sign;
6348 dw_attr_node *at_default_value;
6349 dw_attr_node *at_digit_count;
6350 dw_attr_node *at_discr;
6351 dw_attr_node *at_discr_list;
6352 dw_attr_node *at_discr_value;
6353 dw_attr_node *at_encoding;
6354 dw_attr_node *at_endianity;
6355 dw_attr_node *at_explicit;
6356 dw_attr_node *at_is_optional;
6357 dw_attr_node *at_location;
6358 dw_attr_node *at_lower_bound;
6359 dw_attr_node *at_mutable;
6360 dw_attr_node *at_ordering;
6361 dw_attr_node *at_picture_string;
6362 dw_attr_node *at_prototyped;
6363 dw_attr_node *at_small;
6364 dw_attr_node *at_segment;
6365 dw_attr_node *at_string_length;
6366 dw_attr_node *at_string_length_bit_size;
6367 dw_attr_node *at_string_length_byte_size;
6368 dw_attr_node *at_threads_scaled;
6369 dw_attr_node *at_upper_bound;
6370 dw_attr_node *at_use_location;
6371 dw_attr_node *at_use_UTF8;
6372 dw_attr_node *at_variable_parameter;
6373 dw_attr_node *at_virtuality;
6374 dw_attr_node *at_visibility;
6375 dw_attr_node *at_vtable_elem_location;
6378 /* Collect the attributes that we will want to use for the checksum. */
6380 static void
6381 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6383 dw_attr_node *a;
6384 unsigned ix;
6386 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6388 switch (a->dw_attr)
6390 case DW_AT_name:
6391 attrs->at_name = a;
6392 break;
6393 case DW_AT_type:
6394 attrs->at_type = a;
6395 break;
6396 case DW_AT_friend:
6397 attrs->at_friend = a;
6398 break;
6399 case DW_AT_accessibility:
6400 attrs->at_accessibility = a;
6401 break;
6402 case DW_AT_address_class:
6403 attrs->at_address_class = a;
6404 break;
6405 case DW_AT_allocated:
6406 attrs->at_allocated = a;
6407 break;
6408 case DW_AT_artificial:
6409 attrs->at_artificial = a;
6410 break;
6411 case DW_AT_associated:
6412 attrs->at_associated = a;
6413 break;
6414 case DW_AT_binary_scale:
6415 attrs->at_binary_scale = a;
6416 break;
6417 case DW_AT_bit_offset:
6418 attrs->at_bit_offset = a;
6419 break;
6420 case DW_AT_bit_size:
6421 attrs->at_bit_size = a;
6422 break;
6423 case DW_AT_bit_stride:
6424 attrs->at_bit_stride = a;
6425 break;
6426 case DW_AT_byte_size:
6427 attrs->at_byte_size = a;
6428 break;
6429 case DW_AT_byte_stride:
6430 attrs->at_byte_stride = a;
6431 break;
6432 case DW_AT_const_value:
6433 attrs->at_const_value = a;
6434 break;
6435 case DW_AT_containing_type:
6436 attrs->at_containing_type = a;
6437 break;
6438 case DW_AT_count:
6439 attrs->at_count = a;
6440 break;
6441 case DW_AT_data_location:
6442 attrs->at_data_location = a;
6443 break;
6444 case DW_AT_data_member_location:
6445 attrs->at_data_member_location = a;
6446 break;
6447 case DW_AT_decimal_scale:
6448 attrs->at_decimal_scale = a;
6449 break;
6450 case DW_AT_decimal_sign:
6451 attrs->at_decimal_sign = a;
6452 break;
6453 case DW_AT_default_value:
6454 attrs->at_default_value = a;
6455 break;
6456 case DW_AT_digit_count:
6457 attrs->at_digit_count = a;
6458 break;
6459 case DW_AT_discr:
6460 attrs->at_discr = a;
6461 break;
6462 case DW_AT_discr_list:
6463 attrs->at_discr_list = a;
6464 break;
6465 case DW_AT_discr_value:
6466 attrs->at_discr_value = a;
6467 break;
6468 case DW_AT_encoding:
6469 attrs->at_encoding = a;
6470 break;
6471 case DW_AT_endianity:
6472 attrs->at_endianity = a;
6473 break;
6474 case DW_AT_explicit:
6475 attrs->at_explicit = a;
6476 break;
6477 case DW_AT_is_optional:
6478 attrs->at_is_optional = a;
6479 break;
6480 case DW_AT_location:
6481 attrs->at_location = a;
6482 break;
6483 case DW_AT_lower_bound:
6484 attrs->at_lower_bound = a;
6485 break;
6486 case DW_AT_mutable:
6487 attrs->at_mutable = a;
6488 break;
6489 case DW_AT_ordering:
6490 attrs->at_ordering = a;
6491 break;
6492 case DW_AT_picture_string:
6493 attrs->at_picture_string = a;
6494 break;
6495 case DW_AT_prototyped:
6496 attrs->at_prototyped = a;
6497 break;
6498 case DW_AT_small:
6499 attrs->at_small = a;
6500 break;
6501 case DW_AT_segment:
6502 attrs->at_segment = a;
6503 break;
6504 case DW_AT_string_length:
6505 attrs->at_string_length = a;
6506 break;
6507 case DW_AT_string_length_bit_size:
6508 attrs->at_string_length_bit_size = a;
6509 break;
6510 case DW_AT_string_length_byte_size:
6511 attrs->at_string_length_byte_size = a;
6512 break;
6513 case DW_AT_threads_scaled:
6514 attrs->at_threads_scaled = a;
6515 break;
6516 case DW_AT_upper_bound:
6517 attrs->at_upper_bound = a;
6518 break;
6519 case DW_AT_use_location:
6520 attrs->at_use_location = a;
6521 break;
6522 case DW_AT_use_UTF8:
6523 attrs->at_use_UTF8 = a;
6524 break;
6525 case DW_AT_variable_parameter:
6526 attrs->at_variable_parameter = a;
6527 break;
6528 case DW_AT_virtuality:
6529 attrs->at_virtuality = a;
6530 break;
6531 case DW_AT_visibility:
6532 attrs->at_visibility = a;
6533 break;
6534 case DW_AT_vtable_elem_location:
6535 attrs->at_vtable_elem_location = a;
6536 break;
6537 default:
6538 break;
6543 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6545 static void
6546 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6548 dw_die_ref c;
6549 dw_die_ref decl;
6550 struct checksum_attributes attrs;
6552 CHECKSUM_ULEB128 ('D');
6553 CHECKSUM_ULEB128 (die->die_tag);
6555 memset (&attrs, 0, sizeof (attrs));
6557 decl = get_AT_ref (die, DW_AT_specification);
6558 if (decl != NULL)
6559 collect_checksum_attributes (&attrs, decl);
6560 collect_checksum_attributes (&attrs, die);
6562 CHECKSUM_ATTR (attrs.at_name);
6563 CHECKSUM_ATTR (attrs.at_accessibility);
6564 CHECKSUM_ATTR (attrs.at_address_class);
6565 CHECKSUM_ATTR (attrs.at_allocated);
6566 CHECKSUM_ATTR (attrs.at_artificial);
6567 CHECKSUM_ATTR (attrs.at_associated);
6568 CHECKSUM_ATTR (attrs.at_binary_scale);
6569 CHECKSUM_ATTR (attrs.at_bit_offset);
6570 CHECKSUM_ATTR (attrs.at_bit_size);
6571 CHECKSUM_ATTR (attrs.at_bit_stride);
6572 CHECKSUM_ATTR (attrs.at_byte_size);
6573 CHECKSUM_ATTR (attrs.at_byte_stride);
6574 CHECKSUM_ATTR (attrs.at_const_value);
6575 CHECKSUM_ATTR (attrs.at_containing_type);
6576 CHECKSUM_ATTR (attrs.at_count);
6577 CHECKSUM_ATTR (attrs.at_data_location);
6578 CHECKSUM_ATTR (attrs.at_data_member_location);
6579 CHECKSUM_ATTR (attrs.at_decimal_scale);
6580 CHECKSUM_ATTR (attrs.at_decimal_sign);
6581 CHECKSUM_ATTR (attrs.at_default_value);
6582 CHECKSUM_ATTR (attrs.at_digit_count);
6583 CHECKSUM_ATTR (attrs.at_discr);
6584 CHECKSUM_ATTR (attrs.at_discr_list);
6585 CHECKSUM_ATTR (attrs.at_discr_value);
6586 CHECKSUM_ATTR (attrs.at_encoding);
6587 CHECKSUM_ATTR (attrs.at_endianity);
6588 CHECKSUM_ATTR (attrs.at_explicit);
6589 CHECKSUM_ATTR (attrs.at_is_optional);
6590 CHECKSUM_ATTR (attrs.at_location);
6591 CHECKSUM_ATTR (attrs.at_lower_bound);
6592 CHECKSUM_ATTR (attrs.at_mutable);
6593 CHECKSUM_ATTR (attrs.at_ordering);
6594 CHECKSUM_ATTR (attrs.at_picture_string);
6595 CHECKSUM_ATTR (attrs.at_prototyped);
6596 CHECKSUM_ATTR (attrs.at_small);
6597 CHECKSUM_ATTR (attrs.at_segment);
6598 CHECKSUM_ATTR (attrs.at_string_length);
6599 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6600 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6601 CHECKSUM_ATTR (attrs.at_threads_scaled);
6602 CHECKSUM_ATTR (attrs.at_upper_bound);
6603 CHECKSUM_ATTR (attrs.at_use_location);
6604 CHECKSUM_ATTR (attrs.at_use_UTF8);
6605 CHECKSUM_ATTR (attrs.at_variable_parameter);
6606 CHECKSUM_ATTR (attrs.at_virtuality);
6607 CHECKSUM_ATTR (attrs.at_visibility);
6608 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6609 CHECKSUM_ATTR (attrs.at_type);
6610 CHECKSUM_ATTR (attrs.at_friend);
6612 /* Checksum the child DIEs. */
6613 c = die->die_child;
6614 if (c) do {
6615 dw_attr_node *name_attr;
6617 c = c->die_sib;
6618 name_attr = get_AT (c, DW_AT_name);
6619 if (is_template_instantiation (c))
6621 /* Ignore instantiations of member type and function templates. */
6623 else if (name_attr != NULL
6624 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6626 /* Use a shallow checksum for named nested types and member
6627 functions. */
6628 CHECKSUM_ULEB128 ('S');
6629 CHECKSUM_ULEB128 (c->die_tag);
6630 CHECKSUM_STRING (AT_string (name_attr));
6632 else
6634 /* Use a deep checksum for other children. */
6635 /* Mark this DIE so it gets processed when unmarking. */
6636 if (c->die_mark == 0)
6637 c->die_mark = -1;
6638 die_checksum_ordered (c, ctx, mark);
6640 } while (c != die->die_child);
6642 CHECKSUM_ULEB128 (0);
6645 /* Add a type name and tag to a hash. */
6646 static void
6647 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6649 CHECKSUM_ULEB128 (tag);
6650 CHECKSUM_STRING (name);
6653 #undef CHECKSUM
6654 #undef CHECKSUM_STRING
6655 #undef CHECKSUM_ATTR
6656 #undef CHECKSUM_LEB128
6657 #undef CHECKSUM_ULEB128
6659 /* Generate the type signature for DIE. This is computed by generating an
6660 MD5 checksum over the DIE's tag, its relevant attributes, and its
6661 children. Attributes that are references to other DIEs are processed
6662 by recursion, using the MARK field to prevent infinite recursion.
6663 If the DIE is nested inside a namespace or another type, we also
6664 need to include that context in the signature. The lower 64 bits
6665 of the resulting MD5 checksum comprise the signature. */
6667 static void
6668 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6670 int mark;
6671 const char *name;
6672 unsigned char checksum[16];
6673 struct md5_ctx ctx;
6674 dw_die_ref decl;
6675 dw_die_ref parent;
6677 name = get_AT_string (die, DW_AT_name);
6678 decl = get_AT_ref (die, DW_AT_specification);
6679 parent = get_die_parent (die);
6681 /* First, compute a signature for just the type name (and its surrounding
6682 context, if any. This is stored in the type unit DIE for link-time
6683 ODR (one-definition rule) checking. */
6685 if (is_cxx () && name != NULL)
6687 md5_init_ctx (&ctx);
6689 /* Checksum the names of surrounding namespaces and structures. */
6690 if (parent != NULL)
6691 checksum_die_context (parent, &ctx);
6693 /* Checksum the current DIE. */
6694 die_odr_checksum (die->die_tag, name, &ctx);
6695 md5_finish_ctx (&ctx, checksum);
6697 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6700 /* Next, compute the complete type signature. */
6702 md5_init_ctx (&ctx);
6703 mark = 1;
6704 die->die_mark = mark;
6706 /* Checksum the names of surrounding namespaces and structures. */
6707 if (parent != NULL)
6708 checksum_die_context (parent, &ctx);
6710 /* Checksum the DIE and its children. */
6711 die_checksum_ordered (die, &ctx, &mark);
6712 unmark_all_dies (die);
6713 md5_finish_ctx (&ctx, checksum);
6715 /* Store the signature in the type node and link the type DIE and the
6716 type node together. */
6717 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6718 DWARF_TYPE_SIGNATURE_SIZE);
6719 die->comdat_type_p = true;
6720 die->die_id.die_type_node = type_node;
6721 type_node->type_die = die;
6723 /* If the DIE is a specification, link its declaration to the type node
6724 as well. */
6725 if (decl != NULL)
6727 decl->comdat_type_p = true;
6728 decl->die_id.die_type_node = type_node;
6732 /* Do the location expressions look same? */
6733 static inline int
6734 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6736 return loc1->dw_loc_opc == loc2->dw_loc_opc
6737 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6738 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6741 /* Do the values look the same? */
6742 static int
6743 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6745 dw_loc_descr_ref loc1, loc2;
6746 rtx r1, r2;
6748 if (v1->val_class != v2->val_class)
6749 return 0;
6751 switch (v1->val_class)
6753 case dw_val_class_const:
6754 return v1->v.val_int == v2->v.val_int;
6755 case dw_val_class_unsigned_const:
6756 return v1->v.val_unsigned == v2->v.val_unsigned;
6757 case dw_val_class_const_double:
6758 return v1->v.val_double.high == v2->v.val_double.high
6759 && v1->v.val_double.low == v2->v.val_double.low;
6760 case dw_val_class_wide_int:
6761 return *v1->v.val_wide == *v2->v.val_wide;
6762 case dw_val_class_vec:
6763 if (v1->v.val_vec.length != v2->v.val_vec.length
6764 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6765 return 0;
6766 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6767 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6768 return 0;
6769 return 1;
6770 case dw_val_class_flag:
6771 return v1->v.val_flag == v2->v.val_flag;
6772 case dw_val_class_str:
6773 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6775 case dw_val_class_addr:
6776 r1 = v1->v.val_addr;
6777 r2 = v2->v.val_addr;
6778 if (GET_CODE (r1) != GET_CODE (r2))
6779 return 0;
6780 return !rtx_equal_p (r1, r2);
6782 case dw_val_class_offset:
6783 return v1->v.val_offset == v2->v.val_offset;
6785 case dw_val_class_loc:
6786 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6787 loc1 && loc2;
6788 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6789 if (!same_loc_p (loc1, loc2, mark))
6790 return 0;
6791 return !loc1 && !loc2;
6793 case dw_val_class_die_ref:
6794 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6796 case dw_val_class_fde_ref:
6797 case dw_val_class_vms_delta:
6798 case dw_val_class_lbl_id:
6799 case dw_val_class_lineptr:
6800 case dw_val_class_macptr:
6801 case dw_val_class_high_pc:
6802 return 1;
6804 case dw_val_class_file:
6805 return v1->v.val_file == v2->v.val_file;
6807 case dw_val_class_data8:
6808 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6810 default:
6811 return 1;
6815 /* Do the attributes look the same? */
6817 static int
6818 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6820 if (at1->dw_attr != at2->dw_attr)
6821 return 0;
6823 /* We don't care that this was compiled with a different compiler
6824 snapshot; if the output is the same, that's what matters. */
6825 if (at1->dw_attr == DW_AT_producer)
6826 return 1;
6828 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6831 /* Do the dies look the same? */
6833 static int
6834 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6836 dw_die_ref c1, c2;
6837 dw_attr_node *a1;
6838 unsigned ix;
6840 /* To avoid infinite recursion. */
6841 if (die1->die_mark)
6842 return die1->die_mark == die2->die_mark;
6843 die1->die_mark = die2->die_mark = ++(*mark);
6845 if (die1->die_tag != die2->die_tag)
6846 return 0;
6848 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6849 return 0;
6851 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6852 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6853 return 0;
6855 c1 = die1->die_child;
6856 c2 = die2->die_child;
6857 if (! c1)
6859 if (c2)
6860 return 0;
6862 else
6863 for (;;)
6865 if (!same_die_p (c1, c2, mark))
6866 return 0;
6867 c1 = c1->die_sib;
6868 c2 = c2->die_sib;
6869 if (c1 == die1->die_child)
6871 if (c2 == die2->die_child)
6872 break;
6873 else
6874 return 0;
6878 return 1;
6881 /* Do the dies look the same? Wrapper around same_die_p. */
6883 static int
6884 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6886 int mark = 0;
6887 int ret = same_die_p (die1, die2, &mark);
6889 unmark_all_dies (die1);
6890 unmark_all_dies (die2);
6892 return ret;
6895 /* The prefix to attach to symbols on DIEs in the current comdat debug
6896 info section. */
6897 static const char *comdat_symbol_id;
6899 /* The index of the current symbol within the current comdat CU. */
6900 static unsigned int comdat_symbol_number;
6902 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6903 children, and set comdat_symbol_id accordingly. */
6905 static void
6906 compute_section_prefix (dw_die_ref unit_die)
6908 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6909 const char *base = die_name ? lbasename (die_name) : "anonymous";
6910 char *name = XALLOCAVEC (char, strlen (base) + 64);
6911 char *p;
6912 int i, mark;
6913 unsigned char checksum[16];
6914 struct md5_ctx ctx;
6916 /* Compute the checksum of the DIE, then append part of it as hex digits to
6917 the name filename of the unit. */
6919 md5_init_ctx (&ctx);
6920 mark = 0;
6921 die_checksum (unit_die, &ctx, &mark);
6922 unmark_all_dies (unit_die);
6923 md5_finish_ctx (&ctx, checksum);
6925 sprintf (name, "%s.", base);
6926 clean_symbol_name (name);
6928 p = name + strlen (name);
6929 for (i = 0; i < 4; i++)
6931 sprintf (p, "%.2x", checksum[i]);
6932 p += 2;
6935 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6936 comdat_symbol_number = 0;
6939 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6941 static int
6942 is_type_die (dw_die_ref die)
6944 switch (die->die_tag)
6946 case DW_TAG_array_type:
6947 case DW_TAG_class_type:
6948 case DW_TAG_interface_type:
6949 case DW_TAG_enumeration_type:
6950 case DW_TAG_pointer_type:
6951 case DW_TAG_reference_type:
6952 case DW_TAG_rvalue_reference_type:
6953 case DW_TAG_string_type:
6954 case DW_TAG_structure_type:
6955 case DW_TAG_subroutine_type:
6956 case DW_TAG_union_type:
6957 case DW_TAG_ptr_to_member_type:
6958 case DW_TAG_set_type:
6959 case DW_TAG_subrange_type:
6960 case DW_TAG_base_type:
6961 case DW_TAG_const_type:
6962 case DW_TAG_file_type:
6963 case DW_TAG_packed_type:
6964 case DW_TAG_volatile_type:
6965 case DW_TAG_typedef:
6966 return 1;
6967 default:
6968 return 0;
6972 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6973 Basically, we want to choose the bits that are likely to be shared between
6974 compilations (types) and leave out the bits that are specific to individual
6975 compilations (functions). */
6977 static int
6978 is_comdat_die (dw_die_ref c)
6980 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6981 we do for stabs. The advantage is a greater likelihood of sharing between
6982 objects that don't include headers in the same order (and therefore would
6983 put the base types in a different comdat). jason 8/28/00 */
6985 if (c->die_tag == DW_TAG_base_type)
6986 return 0;
6988 if (c->die_tag == DW_TAG_pointer_type
6989 || c->die_tag == DW_TAG_reference_type
6990 || c->die_tag == DW_TAG_rvalue_reference_type
6991 || c->die_tag == DW_TAG_const_type
6992 || c->die_tag == DW_TAG_volatile_type)
6994 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6996 return t ? is_comdat_die (t) : 0;
6999 return is_type_die (c);
7002 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7003 compilation unit. */
7005 static int
7006 is_symbol_die (dw_die_ref c)
7008 return (is_type_die (c)
7009 || is_declaration_die (c)
7010 || c->die_tag == DW_TAG_namespace
7011 || c->die_tag == DW_TAG_module);
7014 /* Returns true iff C is a compile-unit DIE. */
7016 static inline bool
7017 is_cu_die (dw_die_ref c)
7019 return c && c->die_tag == DW_TAG_compile_unit;
7022 /* Returns true iff C is a unit DIE of some sort. */
7024 static inline bool
7025 is_unit_die (dw_die_ref c)
7027 return c && (c->die_tag == DW_TAG_compile_unit
7028 || c->die_tag == DW_TAG_partial_unit
7029 || c->die_tag == DW_TAG_type_unit);
7032 /* Returns true iff C is a namespace DIE. */
7034 static inline bool
7035 is_namespace_die (dw_die_ref c)
7037 return c && c->die_tag == DW_TAG_namespace;
7040 /* Returns true iff C is a class or structure DIE. */
7042 static inline bool
7043 is_class_die (dw_die_ref c)
7045 return c && (c->die_tag == DW_TAG_class_type
7046 || c->die_tag == DW_TAG_structure_type);
7049 /* Return non-zero if this DIE is a template parameter. */
7051 static inline bool
7052 is_template_parameter (dw_die_ref die)
7054 switch (die->die_tag)
7056 case DW_TAG_template_type_param:
7057 case DW_TAG_template_value_param:
7058 case DW_TAG_GNU_template_template_param:
7059 case DW_TAG_GNU_template_parameter_pack:
7060 return true;
7061 default:
7062 return false;
7066 /* Return non-zero if this DIE represents a template instantiation. */
7068 static inline bool
7069 is_template_instantiation (dw_die_ref die)
7071 dw_die_ref c;
7073 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7074 return false;
7075 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7076 return false;
7079 static char *
7080 gen_internal_sym (const char *prefix)
7082 char buf[256];
7084 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7085 return xstrdup (buf);
7088 /* Assign symbols to all worthy DIEs under DIE. */
7090 static void
7091 assign_symbol_names (dw_die_ref die)
7093 dw_die_ref c;
7095 if (is_symbol_die (die) && !die->comdat_type_p)
7097 if (comdat_symbol_id)
7099 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7101 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7102 comdat_symbol_id, comdat_symbol_number++);
7103 die->die_id.die_symbol = xstrdup (p);
7105 else
7106 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7109 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7112 struct cu_hash_table_entry
7114 dw_die_ref cu;
7115 unsigned min_comdat_num, max_comdat_num;
7116 struct cu_hash_table_entry *next;
7119 /* Helpers to manipulate hash table of CUs. */
7121 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7123 typedef die_struct *compare_type;
7124 static inline hashval_t hash (const cu_hash_table_entry *);
7125 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7126 static inline void remove (cu_hash_table_entry *);
7129 inline hashval_t
7130 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7132 return htab_hash_string (entry->cu->die_id.die_symbol);
7135 inline bool
7136 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7137 const die_struct *entry2)
7139 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7142 inline void
7143 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7145 struct cu_hash_table_entry *next;
7147 while (entry)
7149 next = entry->next;
7150 free (entry);
7151 entry = next;
7155 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7157 /* Check whether we have already seen this CU and set up SYM_NUM
7158 accordingly. */
7159 static int
7160 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7162 struct cu_hash_table_entry dummy;
7163 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7165 dummy.max_comdat_num = 0;
7167 slot = htable->find_slot_with_hash (cu,
7168 htab_hash_string (cu->die_id.die_symbol),
7169 INSERT);
7170 entry = *slot;
7172 for (; entry; last = entry, entry = entry->next)
7174 if (same_die_p_wrap (cu, entry->cu))
7175 break;
7178 if (entry)
7180 *sym_num = entry->min_comdat_num;
7181 return 1;
7184 entry = XCNEW (struct cu_hash_table_entry);
7185 entry->cu = cu;
7186 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7187 entry->next = *slot;
7188 *slot = entry;
7190 return 0;
7193 /* Record SYM_NUM to record of CU in HTABLE. */
7194 static void
7195 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7196 unsigned int sym_num)
7198 struct cu_hash_table_entry **slot, *entry;
7200 slot = htable->find_slot_with_hash (cu,
7201 htab_hash_string (cu->die_id.die_symbol),
7202 NO_INSERT);
7203 entry = *slot;
7205 entry->max_comdat_num = sym_num;
7208 /* Traverse the DIE (which is always comp_unit_die), and set up
7209 additional compilation units for each of the include files we see
7210 bracketed by BINCL/EINCL. */
7212 static void
7213 break_out_includes (dw_die_ref die)
7215 dw_die_ref c;
7216 dw_die_ref unit = NULL;
7217 limbo_die_node *node, **pnode;
7219 c = die->die_child;
7220 if (c) do {
7221 dw_die_ref prev = c;
7222 c = c->die_sib;
7223 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7224 || (unit && is_comdat_die (c)))
7226 dw_die_ref next = c->die_sib;
7228 /* This DIE is for a secondary CU; remove it from the main one. */
7229 remove_child_with_prev (c, prev);
7231 if (c->die_tag == DW_TAG_GNU_BINCL)
7232 unit = push_new_compile_unit (unit, c);
7233 else if (c->die_tag == DW_TAG_GNU_EINCL)
7234 unit = pop_compile_unit (unit);
7235 else
7236 add_child_die (unit, c);
7237 c = next;
7238 if (c == die->die_child)
7239 break;
7241 } while (c != die->die_child);
7243 #if 0
7244 /* We can only use this in debugging, since the frontend doesn't check
7245 to make sure that we leave every include file we enter. */
7246 gcc_assert (!unit);
7247 #endif
7249 assign_symbol_names (die);
7250 cu_hash_type cu_hash_table (10);
7251 for (node = limbo_die_list, pnode = &limbo_die_list;
7252 node;
7253 node = node->next)
7255 int is_dupl;
7257 compute_section_prefix (node->die);
7258 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7259 &comdat_symbol_number);
7260 assign_symbol_names (node->die);
7261 if (is_dupl)
7262 *pnode = node->next;
7263 else
7265 pnode = &node->next;
7266 record_comdat_symbol_number (node->die, &cu_hash_table,
7267 comdat_symbol_number);
7272 /* Return non-zero if this DIE is a declaration. */
7274 static int
7275 is_declaration_die (dw_die_ref die)
7277 dw_attr_node *a;
7278 unsigned ix;
7280 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7281 if (a->dw_attr == DW_AT_declaration)
7282 return 1;
7284 return 0;
7287 /* Return non-zero if this DIE is nested inside a subprogram. */
7289 static int
7290 is_nested_in_subprogram (dw_die_ref die)
7292 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7294 if (decl == NULL)
7295 decl = die;
7296 return local_scope_p (decl);
7299 /* Return non-zero if this DIE contains a defining declaration of a
7300 subprogram. */
7302 static int
7303 contains_subprogram_definition (dw_die_ref die)
7305 dw_die_ref c;
7307 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7308 return 1;
7309 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7310 return 0;
7313 /* Return non-zero if this is a type DIE that should be moved to a
7314 COMDAT .debug_types section. */
7316 static int
7317 should_move_die_to_comdat (dw_die_ref die)
7319 switch (die->die_tag)
7321 case DW_TAG_class_type:
7322 case DW_TAG_structure_type:
7323 case DW_TAG_enumeration_type:
7324 case DW_TAG_union_type:
7325 /* Don't move declarations, inlined instances, types nested in a
7326 subprogram, or types that contain subprogram definitions. */
7327 if (is_declaration_die (die)
7328 || get_AT (die, DW_AT_abstract_origin)
7329 || is_nested_in_subprogram (die)
7330 || contains_subprogram_definition (die))
7331 return 0;
7332 return 1;
7333 case DW_TAG_array_type:
7334 case DW_TAG_interface_type:
7335 case DW_TAG_pointer_type:
7336 case DW_TAG_reference_type:
7337 case DW_TAG_rvalue_reference_type:
7338 case DW_TAG_string_type:
7339 case DW_TAG_subroutine_type:
7340 case DW_TAG_ptr_to_member_type:
7341 case DW_TAG_set_type:
7342 case DW_TAG_subrange_type:
7343 case DW_TAG_base_type:
7344 case DW_TAG_const_type:
7345 case DW_TAG_file_type:
7346 case DW_TAG_packed_type:
7347 case DW_TAG_volatile_type:
7348 case DW_TAG_typedef:
7349 default:
7350 return 0;
7354 /* Make a clone of DIE. */
7356 static dw_die_ref
7357 clone_die (dw_die_ref die)
7359 dw_die_ref clone;
7360 dw_attr_node *a;
7361 unsigned ix;
7363 clone = ggc_cleared_alloc<die_node> ();
7364 clone->die_tag = die->die_tag;
7366 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7367 add_dwarf_attr (clone, a);
7369 return clone;
7372 /* Make a clone of the tree rooted at DIE. */
7374 static dw_die_ref
7375 clone_tree (dw_die_ref die)
7377 dw_die_ref c;
7378 dw_die_ref clone = clone_die (die);
7380 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7382 return clone;
7385 /* Make a clone of DIE as a declaration. */
7387 static dw_die_ref
7388 clone_as_declaration (dw_die_ref die)
7390 dw_die_ref clone;
7391 dw_die_ref decl;
7392 dw_attr_node *a;
7393 unsigned ix;
7395 /* If the DIE is already a declaration, just clone it. */
7396 if (is_declaration_die (die))
7397 return clone_die (die);
7399 /* If the DIE is a specification, just clone its declaration DIE. */
7400 decl = get_AT_ref (die, DW_AT_specification);
7401 if (decl != NULL)
7403 clone = clone_die (decl);
7404 if (die->comdat_type_p)
7405 add_AT_die_ref (clone, DW_AT_signature, die);
7406 return clone;
7409 clone = ggc_cleared_alloc<die_node> ();
7410 clone->die_tag = die->die_tag;
7412 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7414 /* We don't want to copy over all attributes.
7415 For example we don't want DW_AT_byte_size because otherwise we will no
7416 longer have a declaration and GDB will treat it as a definition. */
7418 switch (a->dw_attr)
7420 case DW_AT_abstract_origin:
7421 case DW_AT_artificial:
7422 case DW_AT_containing_type:
7423 case DW_AT_external:
7424 case DW_AT_name:
7425 case DW_AT_type:
7426 case DW_AT_virtuality:
7427 case DW_AT_linkage_name:
7428 case DW_AT_MIPS_linkage_name:
7429 add_dwarf_attr (clone, a);
7430 break;
7431 case DW_AT_byte_size:
7432 default:
7433 break;
7437 if (die->comdat_type_p)
7438 add_AT_die_ref (clone, DW_AT_signature, die);
7440 add_AT_flag (clone, DW_AT_declaration, 1);
7441 return clone;
7445 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7447 struct decl_table_entry
7449 dw_die_ref orig;
7450 dw_die_ref copy;
7453 /* Helpers to manipulate hash table of copied declarations. */
7455 /* Hashtable helpers. */
7457 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7459 typedef die_struct *compare_type;
7460 static inline hashval_t hash (const decl_table_entry *);
7461 static inline bool equal (const decl_table_entry *, const die_struct *);
7464 inline hashval_t
7465 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7467 return htab_hash_pointer (entry->orig);
7470 inline bool
7471 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7472 const die_struct *entry2)
7474 return entry1->orig == entry2;
7477 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7479 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7480 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7481 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7482 to check if the ancestor has already been copied into UNIT. */
7484 static dw_die_ref
7485 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7486 decl_hash_type *decl_table)
7488 dw_die_ref parent = die->die_parent;
7489 dw_die_ref new_parent = unit;
7490 dw_die_ref copy;
7491 decl_table_entry **slot = NULL;
7492 struct decl_table_entry *entry = NULL;
7494 if (decl_table)
7496 /* Check if the entry has already been copied to UNIT. */
7497 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7498 INSERT);
7499 if (*slot != HTAB_EMPTY_ENTRY)
7501 entry = *slot;
7502 return entry->copy;
7505 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7506 entry = XCNEW (struct decl_table_entry);
7507 entry->orig = die;
7508 entry->copy = NULL;
7509 *slot = entry;
7512 if (parent != NULL)
7514 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7515 if (spec != NULL)
7516 parent = spec;
7517 if (!is_unit_die (parent))
7518 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7521 copy = clone_as_declaration (die);
7522 add_child_die (new_parent, copy);
7524 if (decl_table)
7526 /* Record the pointer to the copy. */
7527 entry->copy = copy;
7530 return copy;
7532 /* Copy the declaration context to the new type unit DIE. This includes
7533 any surrounding namespace or type declarations. If the DIE has an
7534 AT_specification attribute, it also includes attributes and children
7535 attached to the specification, and returns a pointer to the original
7536 parent of the declaration DIE. Returns NULL otherwise. */
7538 static dw_die_ref
7539 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7541 dw_die_ref decl;
7542 dw_die_ref new_decl;
7543 dw_die_ref orig_parent = NULL;
7545 decl = get_AT_ref (die, DW_AT_specification);
7546 if (decl == NULL)
7547 decl = die;
7548 else
7550 unsigned ix;
7551 dw_die_ref c;
7552 dw_attr_node *a;
7554 /* The original DIE will be changed to a declaration, and must
7555 be moved to be a child of the original declaration DIE. */
7556 orig_parent = decl->die_parent;
7558 /* Copy the type node pointer from the new DIE to the original
7559 declaration DIE so we can forward references later. */
7560 decl->comdat_type_p = true;
7561 decl->die_id.die_type_node = die->die_id.die_type_node;
7563 remove_AT (die, DW_AT_specification);
7565 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7567 if (a->dw_attr != DW_AT_name
7568 && a->dw_attr != DW_AT_declaration
7569 && a->dw_attr != DW_AT_external)
7570 add_dwarf_attr (die, a);
7573 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7576 if (decl->die_parent != NULL
7577 && !is_unit_die (decl->die_parent))
7579 new_decl = copy_ancestor_tree (unit, decl, NULL);
7580 if (new_decl != NULL)
7582 remove_AT (new_decl, DW_AT_signature);
7583 add_AT_specification (die, new_decl);
7587 return orig_parent;
7590 /* Generate the skeleton ancestor tree for the given NODE, then clone
7591 the DIE and add the clone into the tree. */
7593 static void
7594 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7596 if (node->new_die != NULL)
7597 return;
7599 node->new_die = clone_as_declaration (node->old_die);
7601 if (node->parent != NULL)
7603 generate_skeleton_ancestor_tree (node->parent);
7604 add_child_die (node->parent->new_die, node->new_die);
7608 /* Generate a skeleton tree of DIEs containing any declarations that are
7609 found in the original tree. We traverse the tree looking for declaration
7610 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7612 static void
7613 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7615 skeleton_chain_node node;
7616 dw_die_ref c;
7617 dw_die_ref first;
7618 dw_die_ref prev = NULL;
7619 dw_die_ref next = NULL;
7621 node.parent = parent;
7623 first = c = parent->old_die->die_child;
7624 if (c)
7625 next = c->die_sib;
7626 if (c) do {
7627 if (prev == NULL || prev->die_sib == c)
7628 prev = c;
7629 c = next;
7630 next = (c == first ? NULL : c->die_sib);
7631 node.old_die = c;
7632 node.new_die = NULL;
7633 if (is_declaration_die (c))
7635 if (is_template_instantiation (c))
7637 /* Instantiated templates do not need to be cloned into the
7638 type unit. Just move the DIE and its children back to
7639 the skeleton tree (in the main CU). */
7640 remove_child_with_prev (c, prev);
7641 add_child_die (parent->new_die, c);
7642 c = prev;
7644 else
7646 /* Clone the existing DIE, move the original to the skeleton
7647 tree (which is in the main CU), and put the clone, with
7648 all the original's children, where the original came from
7649 (which is about to be moved to the type unit). */
7650 dw_die_ref clone = clone_die (c);
7651 move_all_children (c, clone);
7653 /* If the original has a DW_AT_object_pointer attribute,
7654 it would now point to a child DIE just moved to the
7655 cloned tree, so we need to remove that attribute from
7656 the original. */
7657 remove_AT (c, DW_AT_object_pointer);
7659 replace_child (c, clone, prev);
7660 generate_skeleton_ancestor_tree (parent);
7661 add_child_die (parent->new_die, c);
7662 node.new_die = c;
7663 c = clone;
7666 generate_skeleton_bottom_up (&node);
7667 } while (next != NULL);
7670 /* Wrapper function for generate_skeleton_bottom_up. */
7672 static dw_die_ref
7673 generate_skeleton (dw_die_ref die)
7675 skeleton_chain_node node;
7677 node.old_die = die;
7678 node.new_die = NULL;
7679 node.parent = NULL;
7681 /* If this type definition is nested inside another type,
7682 and is not an instantiation of a template, always leave
7683 at least a declaration in its place. */
7684 if (die->die_parent != NULL
7685 && is_type_die (die->die_parent)
7686 && !is_template_instantiation (die))
7687 node.new_die = clone_as_declaration (die);
7689 generate_skeleton_bottom_up (&node);
7690 return node.new_die;
7693 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7694 declaration. The original DIE is moved to a new compile unit so that
7695 existing references to it follow it to the new location. If any of the
7696 original DIE's descendants is a declaration, we need to replace the
7697 original DIE with a skeleton tree and move the declarations back into the
7698 skeleton tree. */
7700 static dw_die_ref
7701 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7702 dw_die_ref prev)
7704 dw_die_ref skeleton, orig_parent;
7706 /* Copy the declaration context to the type unit DIE. If the returned
7707 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7708 that DIE. */
7709 orig_parent = copy_declaration_context (unit, child);
7711 skeleton = generate_skeleton (child);
7712 if (skeleton == NULL)
7713 remove_child_with_prev (child, prev);
7714 else
7716 skeleton->comdat_type_p = true;
7717 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7719 /* If the original DIE was a specification, we need to put
7720 the skeleton under the parent DIE of the declaration.
7721 This leaves the original declaration in the tree, but
7722 it will be pruned later since there are no longer any
7723 references to it. */
7724 if (orig_parent != NULL)
7726 remove_child_with_prev (child, prev);
7727 add_child_die (orig_parent, skeleton);
7729 else
7730 replace_child (child, skeleton, prev);
7733 return skeleton;
7736 static void
7737 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7738 comdat_type_node *type_node,
7739 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7741 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7742 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7743 DWARF procedure references in the DW_AT_location attribute. */
7745 static dw_die_ref
7746 copy_dwarf_procedure (dw_die_ref die,
7747 comdat_type_node *type_node,
7748 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7750 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7752 /* DWARF procedures are not supposed to have children... */
7753 gcc_assert (die->die_child == NULL);
7755 /* ... and they are supposed to have only one attribute: DW_AT_location. */
7756 gcc_assert (vec_safe_length (die->die_attr) == 1
7757 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7759 /* Do not copy more than once DWARF procedures. */
7760 bool existed;
7761 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7762 if (existed)
7763 return die_copy;
7765 die_copy = clone_die (die);
7766 add_child_die (type_node->root_die, die_copy);
7767 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7768 return die_copy;
7771 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
7772 procedures in DIE's attributes. */
7774 static void
7775 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7776 comdat_type_node *type_node,
7777 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7779 dw_attr_node *a;
7780 unsigned i;
7782 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7784 dw_loc_descr_ref loc;
7786 if (a->dw_attr_val.val_class != dw_val_class_loc)
7787 continue;
7789 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7791 switch (loc->dw_loc_opc)
7793 case DW_OP_call2:
7794 case DW_OP_call4:
7795 case DW_OP_call_ref:
7796 gcc_assert (loc->dw_loc_oprnd1.val_class
7797 == dw_val_class_die_ref);
7798 loc->dw_loc_oprnd1.v.val_die_ref.die
7799 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7800 type_node,
7801 copied_dwarf_procs);
7803 default:
7804 break;
7810 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7811 rewrite references to point to the copies.
7813 References are looked for in DIE's attributes and recursively in all its
7814 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7815 mapping from old DWARF procedures to their copy. It is used not to copy
7816 twice the same DWARF procedure under TYPE_NODE. */
7818 static void
7819 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7820 comdat_type_node *type_node,
7821 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7823 dw_die_ref c;
7825 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7826 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7827 type_node,
7828 copied_dwarf_procs));
7831 /* Traverse the DIE and set up additional .debug_types sections for each
7832 type worthy of being placed in a COMDAT section. */
7834 static void
7835 break_out_comdat_types (dw_die_ref die)
7837 dw_die_ref c;
7838 dw_die_ref first;
7839 dw_die_ref prev = NULL;
7840 dw_die_ref next = NULL;
7841 dw_die_ref unit = NULL;
7843 first = c = die->die_child;
7844 if (c)
7845 next = c->die_sib;
7846 if (c) do {
7847 if (prev == NULL || prev->die_sib == c)
7848 prev = c;
7849 c = next;
7850 next = (c == first ? NULL : c->die_sib);
7851 if (should_move_die_to_comdat (c))
7853 dw_die_ref replacement;
7854 comdat_type_node *type_node;
7856 /* Break out nested types into their own type units. */
7857 break_out_comdat_types (c);
7859 /* Create a new type unit DIE as the root for the new tree, and
7860 add it to the list of comdat types. */
7861 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7862 add_AT_unsigned (unit, DW_AT_language,
7863 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7864 type_node = ggc_cleared_alloc<comdat_type_node> ();
7865 type_node->root_die = unit;
7866 type_node->next = comdat_type_list;
7867 comdat_type_list = type_node;
7869 /* Generate the type signature. */
7870 generate_type_signature (c, type_node);
7872 /* Copy the declaration context, attributes, and children of the
7873 declaration into the new type unit DIE, then remove this DIE
7874 from the main CU (or replace it with a skeleton if necessary). */
7875 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7876 type_node->skeleton_die = replacement;
7878 /* Add the DIE to the new compunit. */
7879 add_child_die (unit, c);
7881 /* Types can reference DWARF procedures for type size or data location
7882 expressions. Calls in DWARF expressions cannot target procedures
7883 that are not in the same section. So we must copy DWARF procedures
7884 along with this type and then rewrite references to them. */
7885 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7886 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7888 if (replacement != NULL)
7889 c = replacement;
7891 else if (c->die_tag == DW_TAG_namespace
7892 || c->die_tag == DW_TAG_class_type
7893 || c->die_tag == DW_TAG_structure_type
7894 || c->die_tag == DW_TAG_union_type)
7896 /* Look for nested types that can be broken out. */
7897 break_out_comdat_types (c);
7899 } while (next != NULL);
7902 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7903 Enter all the cloned children into the hash table decl_table. */
7905 static dw_die_ref
7906 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7908 dw_die_ref c;
7909 dw_die_ref clone;
7910 struct decl_table_entry *entry;
7911 decl_table_entry **slot;
7913 if (die->die_tag == DW_TAG_subprogram)
7914 clone = clone_as_declaration (die);
7915 else
7916 clone = clone_die (die);
7918 slot = decl_table->find_slot_with_hash (die,
7919 htab_hash_pointer (die), INSERT);
7921 /* Assert that DIE isn't in the hash table yet. If it would be there
7922 before, the ancestors would be necessarily there as well, therefore
7923 clone_tree_partial wouldn't be called. */
7924 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7926 entry = XCNEW (struct decl_table_entry);
7927 entry->orig = die;
7928 entry->copy = clone;
7929 *slot = entry;
7931 if (die->die_tag != DW_TAG_subprogram)
7932 FOR_EACH_CHILD (die, c,
7933 add_child_die (clone, clone_tree_partial (c, decl_table)));
7935 return clone;
7938 /* Walk the DIE and its children, looking for references to incomplete
7939 or trivial types that are unmarked (i.e., that are not in the current
7940 type_unit). */
7942 static void
7943 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7945 dw_die_ref c;
7946 dw_attr_node *a;
7947 unsigned ix;
7949 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7951 if (AT_class (a) == dw_val_class_die_ref)
7953 dw_die_ref targ = AT_ref (a);
7954 decl_table_entry **slot;
7955 struct decl_table_entry *entry;
7957 if (targ->die_mark != 0 || targ->comdat_type_p)
7958 continue;
7960 slot = decl_table->find_slot_with_hash (targ,
7961 htab_hash_pointer (targ),
7962 INSERT);
7964 if (*slot != HTAB_EMPTY_ENTRY)
7966 /* TARG has already been copied, so we just need to
7967 modify the reference to point to the copy. */
7968 entry = *slot;
7969 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7971 else
7973 dw_die_ref parent = unit;
7974 dw_die_ref copy = clone_die (targ);
7976 /* Record in DECL_TABLE that TARG has been copied.
7977 Need to do this now, before the recursive call,
7978 because DECL_TABLE may be expanded and SLOT
7979 would no longer be a valid pointer. */
7980 entry = XCNEW (struct decl_table_entry);
7981 entry->orig = targ;
7982 entry->copy = copy;
7983 *slot = entry;
7985 /* If TARG is not a declaration DIE, we need to copy its
7986 children. */
7987 if (!is_declaration_die (targ))
7989 FOR_EACH_CHILD (
7990 targ, c,
7991 add_child_die (copy,
7992 clone_tree_partial (c, decl_table)));
7995 /* Make sure the cloned tree is marked as part of the
7996 type unit. */
7997 mark_dies (copy);
7999 /* If TARG has surrounding context, copy its ancestor tree
8000 into the new type unit. */
8001 if (targ->die_parent != NULL
8002 && !is_unit_die (targ->die_parent))
8003 parent = copy_ancestor_tree (unit, targ->die_parent,
8004 decl_table);
8006 add_child_die (parent, copy);
8007 a->dw_attr_val.v.val_die_ref.die = copy;
8009 /* Make sure the newly-copied DIE is walked. If it was
8010 installed in a previously-added context, it won't
8011 get visited otherwise. */
8012 if (parent != unit)
8014 /* Find the highest point of the newly-added tree,
8015 mark each node along the way, and walk from there. */
8016 parent->die_mark = 1;
8017 while (parent->die_parent
8018 && parent->die_parent->die_mark == 0)
8020 parent = parent->die_parent;
8021 parent->die_mark = 1;
8023 copy_decls_walk (unit, parent, decl_table);
8029 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8032 /* Copy declarations for "unworthy" types into the new comdat section.
8033 Incomplete types, modified types, and certain other types aren't broken
8034 out into comdat sections of their own, so they don't have a signature,
8035 and we need to copy the declaration into the same section so that we
8036 don't have an external reference. */
8038 static void
8039 copy_decls_for_unworthy_types (dw_die_ref unit)
8041 mark_dies (unit);
8042 decl_hash_type decl_table (10);
8043 copy_decls_walk (unit, unit, &decl_table);
8044 unmark_dies (unit);
8047 /* Traverse the DIE and add a sibling attribute if it may have the
8048 effect of speeding up access to siblings. To save some space,
8049 avoid generating sibling attributes for DIE's without children. */
8051 static void
8052 add_sibling_attributes (dw_die_ref die)
8054 dw_die_ref c;
8056 if (! die->die_child)
8057 return;
8059 if (die->die_parent && die != die->die_parent->die_child)
8060 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8062 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8065 /* Output all location lists for the DIE and its children. */
8067 static void
8068 output_location_lists (dw_die_ref die)
8070 dw_die_ref c;
8071 dw_attr_node *a;
8072 unsigned ix;
8074 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8075 if (AT_class (a) == dw_val_class_loc_list)
8076 output_loc_list (AT_loc_list (a));
8078 FOR_EACH_CHILD (die, c, output_location_lists (c));
8081 /* We want to limit the number of external references, because they are
8082 larger than local references: a relocation takes multiple words, and
8083 even a sig8 reference is always eight bytes, whereas a local reference
8084 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8085 So if we encounter multiple external references to the same type DIE, we
8086 make a local typedef stub for it and redirect all references there.
8088 This is the element of the hash table for keeping track of these
8089 references. */
8091 struct external_ref
8093 dw_die_ref type;
8094 dw_die_ref stub;
8095 unsigned n_refs;
8098 /* Hashtable helpers. */
8100 struct external_ref_hasher : free_ptr_hash <external_ref>
8102 static inline hashval_t hash (const external_ref *);
8103 static inline bool equal (const external_ref *, const external_ref *);
8106 inline hashval_t
8107 external_ref_hasher::hash (const external_ref *r)
8109 dw_die_ref die = r->type;
8110 hashval_t h = 0;
8112 /* We can't use the address of the DIE for hashing, because
8113 that will make the order of the stub DIEs non-deterministic. */
8114 if (! die->comdat_type_p)
8115 /* We have a symbol; use it to compute a hash. */
8116 h = htab_hash_string (die->die_id.die_symbol);
8117 else
8119 /* We have a type signature; use a subset of the bits as the hash.
8120 The 8-byte signature is at least as large as hashval_t. */
8121 comdat_type_node *type_node = die->die_id.die_type_node;
8122 memcpy (&h, type_node->signature, sizeof (h));
8124 return h;
8127 inline bool
8128 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8130 return r1->type == r2->type;
8133 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8135 /* Return a pointer to the external_ref for references to DIE. */
8137 static struct external_ref *
8138 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8140 struct external_ref ref, *ref_p;
8141 external_ref **slot;
8143 ref.type = die;
8144 slot = map->find_slot (&ref, INSERT);
8145 if (*slot != HTAB_EMPTY_ENTRY)
8146 return *slot;
8148 ref_p = XCNEW (struct external_ref);
8149 ref_p->type = die;
8150 *slot = ref_p;
8151 return ref_p;
8154 /* Subroutine of optimize_external_refs, below.
8156 If we see a type skeleton, record it as our stub. If we see external
8157 references, remember how many we've seen. */
8159 static void
8160 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8162 dw_die_ref c;
8163 dw_attr_node *a;
8164 unsigned ix;
8165 struct external_ref *ref_p;
8167 if (is_type_die (die)
8168 && (c = get_AT_ref (die, DW_AT_signature)))
8170 /* This is a local skeleton; use it for local references. */
8171 ref_p = lookup_external_ref (map, c);
8172 ref_p->stub = die;
8175 /* Scan the DIE references, and remember any that refer to DIEs from
8176 other CUs (i.e. those which are not marked). */
8177 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8178 if (AT_class (a) == dw_val_class_die_ref
8179 && (c = AT_ref (a))->die_mark == 0
8180 && is_type_die (c))
8182 ref_p = lookup_external_ref (map, c);
8183 ref_p->n_refs++;
8186 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8189 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8190 points to an external_ref, DATA is the CU we're processing. If we don't
8191 already have a local stub, and we have multiple refs, build a stub. */
8194 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8196 struct external_ref *ref_p = *slot;
8198 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8200 /* We have multiple references to this type, so build a small stub.
8201 Both of these forms are a bit dodgy from the perspective of the
8202 DWARF standard, since technically they should have names. */
8203 dw_die_ref cu = data;
8204 dw_die_ref type = ref_p->type;
8205 dw_die_ref stub = NULL;
8207 if (type->comdat_type_p)
8209 /* If we refer to this type via sig8, use AT_signature. */
8210 stub = new_die (type->die_tag, cu, NULL_TREE);
8211 add_AT_die_ref (stub, DW_AT_signature, type);
8213 else
8215 /* Otherwise, use a typedef with no name. */
8216 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8217 add_AT_die_ref (stub, DW_AT_type, type);
8220 stub->die_mark++;
8221 ref_p->stub = stub;
8223 return 1;
8226 /* DIE is a unit; look through all the DIE references to see if there are
8227 any external references to types, and if so, create local stubs for
8228 them which will be applied in build_abbrev_table. This is useful because
8229 references to local DIEs are smaller. */
8231 static external_ref_hash_type *
8232 optimize_external_refs (dw_die_ref die)
8234 external_ref_hash_type *map = new external_ref_hash_type (10);
8235 optimize_external_refs_1 (die, map);
8236 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8237 return map;
8240 /* The format of each DIE (and its attribute value pairs) is encoded in an
8241 abbreviation table. This routine builds the abbreviation table and assigns
8242 a unique abbreviation id for each abbreviation entry. The children of each
8243 die are visited recursively. */
8245 static void
8246 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8248 unsigned long abbrev_id;
8249 unsigned int n_alloc;
8250 dw_die_ref c;
8251 dw_attr_node *a;
8252 unsigned ix;
8254 /* Scan the DIE references, and replace any that refer to
8255 DIEs from other CUs (i.e. those which are not marked) with
8256 the local stubs we built in optimize_external_refs. */
8257 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8258 if (AT_class (a) == dw_val_class_die_ref
8259 && (c = AT_ref (a))->die_mark == 0)
8261 struct external_ref *ref_p;
8262 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8264 ref_p = lookup_external_ref (extern_map, c);
8265 if (ref_p->stub && ref_p->stub != die)
8266 change_AT_die_ref (a, ref_p->stub);
8267 else
8268 /* We aren't changing this reference, so mark it external. */
8269 set_AT_ref_external (a, 1);
8272 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8274 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8275 dw_attr_node *die_a, *abbrev_a;
8276 unsigned ix;
8277 bool ok = true;
8279 if (abbrev->die_tag != die->die_tag)
8280 continue;
8281 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8282 continue;
8284 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8285 continue;
8287 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8289 abbrev_a = &(*abbrev->die_attr)[ix];
8290 if ((abbrev_a->dw_attr != die_a->dw_attr)
8291 || (value_format (abbrev_a) != value_format (die_a)))
8293 ok = false;
8294 break;
8297 if (ok)
8298 break;
8301 if (abbrev_id >= abbrev_die_table_in_use)
8303 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8305 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8306 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8307 n_alloc);
8309 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8310 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8311 abbrev_die_table_allocated = n_alloc;
8314 ++abbrev_die_table_in_use;
8315 abbrev_die_table[abbrev_id] = die;
8318 die->die_abbrev = abbrev_id;
8319 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8322 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8324 static int
8325 constant_size (unsigned HOST_WIDE_INT value)
8327 int log;
8329 if (value == 0)
8330 log = 0;
8331 else
8332 log = floor_log2 (value);
8334 log = log / 8;
8335 log = 1 << (floor_log2 (log) + 1);
8337 return log;
8340 /* Return the size of a DIE as it is represented in the
8341 .debug_info section. */
8343 static unsigned long
8344 size_of_die (dw_die_ref die)
8346 unsigned long size = 0;
8347 dw_attr_node *a;
8348 unsigned ix;
8349 enum dwarf_form form;
8351 size += size_of_uleb128 (die->die_abbrev);
8352 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8354 switch (AT_class (a))
8356 case dw_val_class_addr:
8357 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8359 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8360 size += size_of_uleb128 (AT_index (a));
8362 else
8363 size += DWARF2_ADDR_SIZE;
8364 break;
8365 case dw_val_class_offset:
8366 size += DWARF_OFFSET_SIZE;
8367 break;
8368 case dw_val_class_loc:
8370 unsigned long lsize = size_of_locs (AT_loc (a));
8372 /* Block length. */
8373 if (dwarf_version >= 4)
8374 size += size_of_uleb128 (lsize);
8375 else
8376 size += constant_size (lsize);
8377 size += lsize;
8379 break;
8380 case dw_val_class_loc_list:
8381 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8383 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8384 size += size_of_uleb128 (AT_index (a));
8386 else
8387 size += DWARF_OFFSET_SIZE;
8388 break;
8389 case dw_val_class_range_list:
8390 size += DWARF_OFFSET_SIZE;
8391 break;
8392 case dw_val_class_const:
8393 size += size_of_sleb128 (AT_int (a));
8394 break;
8395 case dw_val_class_unsigned_const:
8397 int csize = constant_size (AT_unsigned (a));
8398 if (dwarf_version == 3
8399 && a->dw_attr == DW_AT_data_member_location
8400 && csize >= 4)
8401 size += size_of_uleb128 (AT_unsigned (a));
8402 else
8403 size += csize;
8405 break;
8406 case dw_val_class_const_double:
8407 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8408 if (HOST_BITS_PER_WIDE_INT >= 64)
8409 size++; /* block */
8410 break;
8411 case dw_val_class_wide_int:
8412 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8413 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8414 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8415 > 64)
8416 size++; /* block */
8417 break;
8418 case dw_val_class_vec:
8419 size += constant_size (a->dw_attr_val.v.val_vec.length
8420 * a->dw_attr_val.v.val_vec.elt_size)
8421 + a->dw_attr_val.v.val_vec.length
8422 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8423 break;
8424 case dw_val_class_flag:
8425 if (dwarf_version >= 4)
8426 /* Currently all add_AT_flag calls pass in 1 as last argument,
8427 so DW_FORM_flag_present can be used. If that ever changes,
8428 we'll need to use DW_FORM_flag and have some optimization
8429 in build_abbrev_table that will change those to
8430 DW_FORM_flag_present if it is set to 1 in all DIEs using
8431 the same abbrev entry. */
8432 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8433 else
8434 size += 1;
8435 break;
8436 case dw_val_class_die_ref:
8437 if (AT_ref_external (a))
8439 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8440 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8441 is sized by target address length, whereas in DWARF3
8442 it's always sized as an offset. */
8443 if (use_debug_types)
8444 size += DWARF_TYPE_SIGNATURE_SIZE;
8445 else if (dwarf_version == 2)
8446 size += DWARF2_ADDR_SIZE;
8447 else
8448 size += DWARF_OFFSET_SIZE;
8450 else
8451 size += DWARF_OFFSET_SIZE;
8452 break;
8453 case dw_val_class_fde_ref:
8454 size += DWARF_OFFSET_SIZE;
8455 break;
8456 case dw_val_class_lbl_id:
8457 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8459 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8460 size += size_of_uleb128 (AT_index (a));
8462 else
8463 size += DWARF2_ADDR_SIZE;
8464 break;
8465 case dw_val_class_lineptr:
8466 case dw_val_class_macptr:
8467 size += DWARF_OFFSET_SIZE;
8468 break;
8469 case dw_val_class_str:
8470 form = AT_string_form (a);
8471 if (form == DW_FORM_strp)
8472 size += DWARF_OFFSET_SIZE;
8473 else if (form == DW_FORM_GNU_str_index)
8474 size += size_of_uleb128 (AT_index (a));
8475 else
8476 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8477 break;
8478 case dw_val_class_file:
8479 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8480 break;
8481 case dw_val_class_data8:
8482 size += 8;
8483 break;
8484 case dw_val_class_vms_delta:
8485 size += DWARF_OFFSET_SIZE;
8486 break;
8487 case dw_val_class_high_pc:
8488 size += DWARF2_ADDR_SIZE;
8489 break;
8490 case dw_val_class_discr_value:
8491 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8492 break;
8493 case dw_val_class_discr_list:
8495 unsigned block_size = size_of_discr_list (AT_discr_list (a));
8497 /* This is a block, so we have the block length and then its
8498 data. */
8499 size += constant_size (block_size) + block_size;
8501 break;
8502 default:
8503 gcc_unreachable ();
8507 return size;
8510 /* Size the debugging information associated with a given DIE. Visits the
8511 DIE's children recursively. Updates the global variable next_die_offset, on
8512 each time through. Uses the current value of next_die_offset to update the
8513 die_offset field in each DIE. */
8515 static void
8516 calc_die_sizes (dw_die_ref die)
8518 dw_die_ref c;
8520 gcc_assert (die->die_offset == 0
8521 || (unsigned long int) die->die_offset == next_die_offset);
8522 die->die_offset = next_die_offset;
8523 next_die_offset += size_of_die (die);
8525 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8527 if (die->die_child != NULL)
8528 /* Count the null byte used to terminate sibling lists. */
8529 next_die_offset += 1;
8532 /* Size just the base type children at the start of the CU.
8533 This is needed because build_abbrev needs to size locs
8534 and sizing of type based stack ops needs to know die_offset
8535 values for the base types. */
8537 static void
8538 calc_base_type_die_sizes (void)
8540 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8541 unsigned int i;
8542 dw_die_ref base_type;
8543 #if ENABLE_ASSERT_CHECKING
8544 dw_die_ref prev = comp_unit_die ()->die_child;
8545 #endif
8547 die_offset += size_of_die (comp_unit_die ());
8548 for (i = 0; base_types.iterate (i, &base_type); i++)
8550 #if ENABLE_ASSERT_CHECKING
8551 gcc_assert (base_type->die_offset == 0
8552 && prev->die_sib == base_type
8553 && base_type->die_child == NULL
8554 && base_type->die_abbrev);
8555 prev = base_type;
8556 #endif
8557 base_type->die_offset = die_offset;
8558 die_offset += size_of_die (base_type);
8562 /* Set the marks for a die and its children. We do this so
8563 that we know whether or not a reference needs to use FORM_ref_addr; only
8564 DIEs in the same CU will be marked. We used to clear out the offset
8565 and use that as the flag, but ran into ordering problems. */
8567 static void
8568 mark_dies (dw_die_ref die)
8570 dw_die_ref c;
8572 gcc_assert (!die->die_mark);
8574 die->die_mark = 1;
8575 FOR_EACH_CHILD (die, c, mark_dies (c));
8578 /* Clear the marks for a die and its children. */
8580 static void
8581 unmark_dies (dw_die_ref die)
8583 dw_die_ref c;
8585 if (! use_debug_types)
8586 gcc_assert (die->die_mark);
8588 die->die_mark = 0;
8589 FOR_EACH_CHILD (die, c, unmark_dies (c));
8592 /* Clear the marks for a die, its children and referred dies. */
8594 static void
8595 unmark_all_dies (dw_die_ref die)
8597 dw_die_ref c;
8598 dw_attr_node *a;
8599 unsigned ix;
8601 if (!die->die_mark)
8602 return;
8603 die->die_mark = 0;
8605 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8607 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8608 if (AT_class (a) == dw_val_class_die_ref)
8609 unmark_all_dies (AT_ref (a));
8612 /* Calculate if the entry should appear in the final output file. It may be
8613 from a pruned a type. */
8615 static bool
8616 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8618 /* By limiting gnu pubnames to definitions only, gold can generate a
8619 gdb index without entries for declarations, which don't include
8620 enough information to be useful. */
8621 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8622 return false;
8624 if (table == pubname_table)
8626 /* Enumerator names are part of the pubname table, but the
8627 parent DW_TAG_enumeration_type die may have been pruned.
8628 Don't output them if that is the case. */
8629 if (p->die->die_tag == DW_TAG_enumerator &&
8630 (p->die->die_parent == NULL
8631 || !p->die->die_parent->die_perennial_p))
8632 return false;
8634 /* Everything else in the pubname table is included. */
8635 return true;
8638 /* The pubtypes table shouldn't include types that have been
8639 pruned. */
8640 return (p->die->die_offset != 0
8641 || !flag_eliminate_unused_debug_types);
8644 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8645 generated for the compilation unit. */
8647 static unsigned long
8648 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8650 unsigned long size;
8651 unsigned i;
8652 pubname_entry *p;
8653 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8655 size = DWARF_PUBNAMES_HEADER_SIZE;
8656 FOR_EACH_VEC_ELT (*names, i, p)
8657 if (include_pubname_in_output (names, p))
8658 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8660 size += DWARF_OFFSET_SIZE;
8661 return size;
8664 /* Return the size of the information in the .debug_aranges section. */
8666 static unsigned long
8667 size_of_aranges (void)
8669 unsigned long size;
8671 size = DWARF_ARANGES_HEADER_SIZE;
8673 /* Count the address/length pair for this compilation unit. */
8674 if (text_section_used)
8675 size += 2 * DWARF2_ADDR_SIZE;
8676 if (cold_text_section_used)
8677 size += 2 * DWARF2_ADDR_SIZE;
8678 if (have_multiple_function_sections)
8680 unsigned fde_idx;
8681 dw_fde_ref fde;
8683 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8685 if (DECL_IGNORED_P (fde->decl))
8686 continue;
8687 if (!fde->in_std_section)
8688 size += 2 * DWARF2_ADDR_SIZE;
8689 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8690 size += 2 * DWARF2_ADDR_SIZE;
8694 /* Count the two zero words used to terminated the address range table. */
8695 size += 2 * DWARF2_ADDR_SIZE;
8696 return size;
8699 /* Select the encoding of an attribute value. */
8701 static enum dwarf_form
8702 value_format (dw_attr_node *a)
8704 switch (AT_class (a))
8706 case dw_val_class_addr:
8707 /* Only very few attributes allow DW_FORM_addr. */
8708 switch (a->dw_attr)
8710 case DW_AT_low_pc:
8711 case DW_AT_high_pc:
8712 case DW_AT_entry_pc:
8713 case DW_AT_trampoline:
8714 return (AT_index (a) == NOT_INDEXED
8715 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8716 default:
8717 break;
8719 switch (DWARF2_ADDR_SIZE)
8721 case 1:
8722 return DW_FORM_data1;
8723 case 2:
8724 return DW_FORM_data2;
8725 case 4:
8726 return DW_FORM_data4;
8727 case 8:
8728 return DW_FORM_data8;
8729 default:
8730 gcc_unreachable ();
8732 case dw_val_class_range_list:
8733 case dw_val_class_loc_list:
8734 if (dwarf_version >= 4)
8735 return DW_FORM_sec_offset;
8736 /* FALLTHRU */
8737 case dw_val_class_vms_delta:
8738 case dw_val_class_offset:
8739 switch (DWARF_OFFSET_SIZE)
8741 case 4:
8742 return DW_FORM_data4;
8743 case 8:
8744 return DW_FORM_data8;
8745 default:
8746 gcc_unreachable ();
8748 case dw_val_class_loc:
8749 if (dwarf_version >= 4)
8750 return DW_FORM_exprloc;
8751 switch (constant_size (size_of_locs (AT_loc (a))))
8753 case 1:
8754 return DW_FORM_block1;
8755 case 2:
8756 return DW_FORM_block2;
8757 case 4:
8758 return DW_FORM_block4;
8759 default:
8760 gcc_unreachable ();
8762 case dw_val_class_const:
8763 return DW_FORM_sdata;
8764 case dw_val_class_unsigned_const:
8765 switch (constant_size (AT_unsigned (a)))
8767 case 1:
8768 return DW_FORM_data1;
8769 case 2:
8770 return DW_FORM_data2;
8771 case 4:
8772 /* In DWARF3 DW_AT_data_member_location with
8773 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8774 constant, so we need to use DW_FORM_udata if we need
8775 a large constant. */
8776 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8777 return DW_FORM_udata;
8778 return DW_FORM_data4;
8779 case 8:
8780 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8781 return DW_FORM_udata;
8782 return DW_FORM_data8;
8783 default:
8784 gcc_unreachable ();
8786 case dw_val_class_const_double:
8787 switch (HOST_BITS_PER_WIDE_INT)
8789 case 8:
8790 return DW_FORM_data2;
8791 case 16:
8792 return DW_FORM_data4;
8793 case 32:
8794 return DW_FORM_data8;
8795 case 64:
8796 default:
8797 return DW_FORM_block1;
8799 case dw_val_class_wide_int:
8800 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8802 case 8:
8803 return DW_FORM_data1;
8804 case 16:
8805 return DW_FORM_data2;
8806 case 32:
8807 return DW_FORM_data4;
8808 case 64:
8809 return DW_FORM_data8;
8810 default:
8811 return DW_FORM_block1;
8813 case dw_val_class_vec:
8814 switch (constant_size (a->dw_attr_val.v.val_vec.length
8815 * a->dw_attr_val.v.val_vec.elt_size))
8817 case 1:
8818 return DW_FORM_block1;
8819 case 2:
8820 return DW_FORM_block2;
8821 case 4:
8822 return DW_FORM_block4;
8823 default:
8824 gcc_unreachable ();
8826 case dw_val_class_flag:
8827 if (dwarf_version >= 4)
8829 /* Currently all add_AT_flag calls pass in 1 as last argument,
8830 so DW_FORM_flag_present can be used. If that ever changes,
8831 we'll need to use DW_FORM_flag and have some optimization
8832 in build_abbrev_table that will change those to
8833 DW_FORM_flag_present if it is set to 1 in all DIEs using
8834 the same abbrev entry. */
8835 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8836 return DW_FORM_flag_present;
8838 return DW_FORM_flag;
8839 case dw_val_class_die_ref:
8840 if (AT_ref_external (a))
8841 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8842 else
8843 return DW_FORM_ref;
8844 case dw_val_class_fde_ref:
8845 return DW_FORM_data;
8846 case dw_val_class_lbl_id:
8847 return (AT_index (a) == NOT_INDEXED
8848 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8849 case dw_val_class_lineptr:
8850 case dw_val_class_macptr:
8851 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8852 case dw_val_class_str:
8853 return AT_string_form (a);
8854 case dw_val_class_file:
8855 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8857 case 1:
8858 return DW_FORM_data1;
8859 case 2:
8860 return DW_FORM_data2;
8861 case 4:
8862 return DW_FORM_data4;
8863 default:
8864 gcc_unreachable ();
8867 case dw_val_class_data8:
8868 return DW_FORM_data8;
8870 case dw_val_class_high_pc:
8871 switch (DWARF2_ADDR_SIZE)
8873 case 1:
8874 return DW_FORM_data1;
8875 case 2:
8876 return DW_FORM_data2;
8877 case 4:
8878 return DW_FORM_data4;
8879 case 8:
8880 return DW_FORM_data8;
8881 default:
8882 gcc_unreachable ();
8885 case dw_val_class_discr_value:
8886 return (a->dw_attr_val.v.val_discr_value.pos
8887 ? DW_FORM_udata
8888 : DW_FORM_sdata);
8889 case dw_val_class_discr_list:
8890 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8892 case 1:
8893 return DW_FORM_block1;
8894 case 2:
8895 return DW_FORM_block2;
8896 case 4:
8897 return DW_FORM_block4;
8898 default:
8899 gcc_unreachable ();
8902 default:
8903 gcc_unreachable ();
8907 /* Output the encoding of an attribute value. */
8909 static void
8910 output_value_format (dw_attr_node *a)
8912 enum dwarf_form form = value_format (a);
8914 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8917 /* Given a die and id, produce the appropriate abbreviations. */
8919 static void
8920 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8922 unsigned ix;
8923 dw_attr_node *a_attr;
8925 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8926 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8927 dwarf_tag_name (abbrev->die_tag));
8929 if (abbrev->die_child != NULL)
8930 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8931 else
8932 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8934 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8936 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8937 dwarf_attr_name (a_attr->dw_attr));
8938 output_value_format (a_attr);
8941 dw2_asm_output_data (1, 0, NULL);
8942 dw2_asm_output_data (1, 0, NULL);
8946 /* Output the .debug_abbrev section which defines the DIE abbreviation
8947 table. */
8949 static void
8950 output_abbrev_section (void)
8952 unsigned long abbrev_id;
8954 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8955 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8957 /* Terminate the table. */
8958 dw2_asm_output_data (1, 0, NULL);
8961 /* Output a symbol we can use to refer to this DIE from another CU. */
8963 static inline void
8964 output_die_symbol (dw_die_ref die)
8966 const char *sym = die->die_id.die_symbol;
8968 gcc_assert (!die->comdat_type_p);
8970 if (sym == 0)
8971 return;
8973 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8974 /* We make these global, not weak; if the target doesn't support
8975 .linkonce, it doesn't support combining the sections, so debugging
8976 will break. */
8977 targetm.asm_out.globalize_label (asm_out_file, sym);
8979 ASM_OUTPUT_LABEL (asm_out_file, sym);
8982 /* Return a new location list, given the begin and end range, and the
8983 expression. */
8985 static inline dw_loc_list_ref
8986 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8987 const char *section)
8989 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8991 retlist->begin = begin;
8992 retlist->begin_entry = NULL;
8993 retlist->end = end;
8994 retlist->expr = expr;
8995 retlist->section = section;
8997 return retlist;
9000 /* Generate a new internal symbol for this location list node, if it
9001 hasn't got one yet. */
9003 static inline void
9004 gen_llsym (dw_loc_list_ref list)
9006 gcc_assert (!list->ll_symbol);
9007 list->ll_symbol = gen_internal_sym ("LLST");
9010 /* Output the location list given to us. */
9012 static void
9013 output_loc_list (dw_loc_list_ref list_head)
9015 dw_loc_list_ref curr = list_head;
9017 if (list_head->emitted)
9018 return;
9019 list_head->emitted = true;
9021 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9023 /* Walk the location list, and output each range + expression. */
9024 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9026 unsigned long size;
9027 /* Don't output an entry that starts and ends at the same address. */
9028 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9029 continue;
9030 size = size_of_locs (curr->expr);
9031 /* If the expression is too large, drop it on the floor. We could
9032 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9033 in the expression, but >= 64KB expressions for a single value
9034 in a single range are unlikely very useful. */
9035 if (size > 0xffff)
9036 continue;
9037 if (dwarf_split_debug_info)
9039 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9040 "Location list start/length entry (%s)",
9041 list_head->ll_symbol);
9042 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9043 "Location list range start index (%s)",
9044 curr->begin);
9045 /* The length field is 4 bytes. If we ever need to support
9046 an 8-byte length, we can add a new DW_LLE code or fall back
9047 to DW_LLE_GNU_start_end_entry. */
9048 dw2_asm_output_delta (4, curr->end, curr->begin,
9049 "Location list range length (%s)",
9050 list_head->ll_symbol);
9052 else if (!have_multiple_function_sections)
9054 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9055 "Location list begin address (%s)",
9056 list_head->ll_symbol);
9057 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9058 "Location list end address (%s)",
9059 list_head->ll_symbol);
9061 else
9063 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9064 "Location list begin address (%s)",
9065 list_head->ll_symbol);
9066 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9067 "Location list end address (%s)",
9068 list_head->ll_symbol);
9071 /* Output the block length for this list of location operations. */
9072 gcc_assert (size <= 0xffff);
9073 dw2_asm_output_data (2, size, "%s", "Location expression size");
9075 output_loc_sequence (curr->expr, -1);
9078 if (dwarf_split_debug_info)
9079 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9080 "Location list terminator (%s)",
9081 list_head->ll_symbol);
9082 else
9084 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9085 "Location list terminator begin (%s)",
9086 list_head->ll_symbol);
9087 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9088 "Location list terminator end (%s)",
9089 list_head->ll_symbol);
9093 /* Output a range_list offset into the debug_range section. Emit a
9094 relocated reference if val_entry is NULL, otherwise, emit an
9095 indirect reference. */
9097 static void
9098 output_range_list_offset (dw_attr_node *a)
9100 const char *name = dwarf_attr_name (a->dw_attr);
9102 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9104 char *p = strchr (ranges_section_label, '\0');
9105 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9106 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9107 debug_ranges_section, "%s", name);
9108 *p = '\0';
9110 else
9111 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9112 "%s (offset from %s)", name, ranges_section_label);
9115 /* Output the offset into the debug_loc section. */
9117 static void
9118 output_loc_list_offset (dw_attr_node *a)
9120 char *sym = AT_loc_list (a)->ll_symbol;
9122 gcc_assert (sym);
9123 if (dwarf_split_debug_info)
9124 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9125 "%s", dwarf_attr_name (a->dw_attr));
9126 else
9127 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9128 "%s", dwarf_attr_name (a->dw_attr));
9131 /* Output an attribute's index or value appropriately. */
9133 static void
9134 output_attr_index_or_value (dw_attr_node *a)
9136 const char *name = dwarf_attr_name (a->dw_attr);
9138 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9140 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9141 return;
9143 switch (AT_class (a))
9145 case dw_val_class_addr:
9146 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9147 break;
9148 case dw_val_class_high_pc:
9149 case dw_val_class_lbl_id:
9150 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9151 break;
9152 case dw_val_class_loc_list:
9153 output_loc_list_offset (a);
9154 break;
9155 default:
9156 gcc_unreachable ();
9160 /* Output a type signature. */
9162 static inline void
9163 output_signature (const char *sig, const char *name)
9165 int i;
9167 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9168 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9171 /* Output a discriminant value. */
9173 static inline void
9174 output_discr_value (dw_discr_value *discr_value, const char *name)
9176 if (discr_value->pos)
9177 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9178 else
9179 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9182 /* Output the DIE and its attributes. Called recursively to generate
9183 the definitions of each child DIE. */
9185 static void
9186 output_die (dw_die_ref die)
9188 dw_attr_node *a;
9189 dw_die_ref c;
9190 unsigned long size;
9191 unsigned ix;
9193 /* If someone in another CU might refer to us, set up a symbol for
9194 them to point to. */
9195 if (! die->comdat_type_p && die->die_id.die_symbol)
9196 output_die_symbol (die);
9198 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9199 (unsigned long)die->die_offset,
9200 dwarf_tag_name (die->die_tag));
9202 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9204 const char *name = dwarf_attr_name (a->dw_attr);
9206 switch (AT_class (a))
9208 case dw_val_class_addr:
9209 output_attr_index_or_value (a);
9210 break;
9212 case dw_val_class_offset:
9213 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9214 "%s", name);
9215 break;
9217 case dw_val_class_range_list:
9218 output_range_list_offset (a);
9219 break;
9221 case dw_val_class_loc:
9222 size = size_of_locs (AT_loc (a));
9224 /* Output the block length for this list of location operations. */
9225 if (dwarf_version >= 4)
9226 dw2_asm_output_data_uleb128 (size, "%s", name);
9227 else
9228 dw2_asm_output_data (constant_size (size), size, "%s", name);
9230 output_loc_sequence (AT_loc (a), -1);
9231 break;
9233 case dw_val_class_const:
9234 /* ??? It would be slightly more efficient to use a scheme like is
9235 used for unsigned constants below, but gdb 4.x does not sign
9236 extend. Gdb 5.x does sign extend. */
9237 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9238 break;
9240 case dw_val_class_unsigned_const:
9242 int csize = constant_size (AT_unsigned (a));
9243 if (dwarf_version == 3
9244 && a->dw_attr == DW_AT_data_member_location
9245 && csize >= 4)
9246 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9247 else
9248 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9250 break;
9252 case dw_val_class_const_double:
9254 unsigned HOST_WIDE_INT first, second;
9256 if (HOST_BITS_PER_WIDE_INT >= 64)
9257 dw2_asm_output_data (1,
9258 HOST_BITS_PER_DOUBLE_INT
9259 / HOST_BITS_PER_CHAR,
9260 NULL);
9262 if (WORDS_BIG_ENDIAN)
9264 first = a->dw_attr_val.v.val_double.high;
9265 second = a->dw_attr_val.v.val_double.low;
9267 else
9269 first = a->dw_attr_val.v.val_double.low;
9270 second = a->dw_attr_val.v.val_double.high;
9273 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9274 first, "%s", name);
9275 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9276 second, NULL);
9278 break;
9280 case dw_val_class_wide_int:
9282 int i;
9283 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9284 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9285 if (len * HOST_BITS_PER_WIDE_INT > 64)
9286 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9287 NULL);
9289 if (WORDS_BIG_ENDIAN)
9290 for (i = len - 1; i >= 0; --i)
9292 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9293 "%s", name);
9294 name = "";
9296 else
9297 for (i = 0; i < len; ++i)
9299 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9300 "%s", name);
9301 name = "";
9304 break;
9306 case dw_val_class_vec:
9308 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9309 unsigned int len = a->dw_attr_val.v.val_vec.length;
9310 unsigned int i;
9311 unsigned char *p;
9313 dw2_asm_output_data (constant_size (len * elt_size),
9314 len * elt_size, "%s", name);
9315 if (elt_size > sizeof (HOST_WIDE_INT))
9317 elt_size /= 2;
9318 len *= 2;
9320 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9321 i < len;
9322 i++, p += elt_size)
9323 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9324 "fp or vector constant word %u", i);
9325 break;
9328 case dw_val_class_flag:
9329 if (dwarf_version >= 4)
9331 /* Currently all add_AT_flag calls pass in 1 as last argument,
9332 so DW_FORM_flag_present can be used. If that ever changes,
9333 we'll need to use DW_FORM_flag and have some optimization
9334 in build_abbrev_table that will change those to
9335 DW_FORM_flag_present if it is set to 1 in all DIEs using
9336 the same abbrev entry. */
9337 gcc_assert (AT_flag (a) == 1);
9338 if (flag_debug_asm)
9339 fprintf (asm_out_file, "\t\t\t%s %s\n",
9340 ASM_COMMENT_START, name);
9341 break;
9343 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9344 break;
9346 case dw_val_class_loc_list:
9347 output_attr_index_or_value (a);
9348 break;
9350 case dw_val_class_die_ref:
9351 if (AT_ref_external (a))
9353 if (AT_ref (a)->comdat_type_p)
9355 comdat_type_node *type_node =
9356 AT_ref (a)->die_id.die_type_node;
9358 gcc_assert (type_node);
9359 output_signature (type_node->signature, name);
9361 else
9363 const char *sym = AT_ref (a)->die_id.die_symbol;
9364 int size;
9366 gcc_assert (sym);
9367 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9368 length, whereas in DWARF3 it's always sized as an
9369 offset. */
9370 if (dwarf_version == 2)
9371 size = DWARF2_ADDR_SIZE;
9372 else
9373 size = DWARF_OFFSET_SIZE;
9374 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9375 name);
9378 else
9380 gcc_assert (AT_ref (a)->die_offset);
9381 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9382 "%s", name);
9384 break;
9386 case dw_val_class_fde_ref:
9388 char l1[20];
9390 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9391 a->dw_attr_val.v.val_fde_index * 2);
9392 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9393 "%s", name);
9395 break;
9397 case dw_val_class_vms_delta:
9398 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9399 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9400 AT_vms_delta2 (a), AT_vms_delta1 (a),
9401 "%s", name);
9402 #else
9403 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9404 AT_vms_delta2 (a), AT_vms_delta1 (a),
9405 "%s", name);
9406 #endif
9407 break;
9409 case dw_val_class_lbl_id:
9410 output_attr_index_or_value (a);
9411 break;
9413 case dw_val_class_lineptr:
9414 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9415 debug_line_section, "%s", name);
9416 break;
9418 case dw_val_class_macptr:
9419 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9420 debug_macinfo_section, "%s", name);
9421 break;
9423 case dw_val_class_str:
9424 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9425 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9426 a->dw_attr_val.v.val_str->label,
9427 debug_str_section,
9428 "%s: \"%s\"", name, AT_string (a));
9429 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9430 dw2_asm_output_data_uleb128 (AT_index (a),
9431 "%s: \"%s\"", name, AT_string (a));
9432 else
9433 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9434 break;
9436 case dw_val_class_file:
9438 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9440 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9441 a->dw_attr_val.v.val_file->filename);
9442 break;
9445 case dw_val_class_data8:
9447 int i;
9449 for (i = 0; i < 8; i++)
9450 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9451 i == 0 ? "%s" : NULL, name);
9452 break;
9455 case dw_val_class_high_pc:
9456 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9457 get_AT_low_pc (die), "DW_AT_high_pc");
9458 break;
9460 case dw_val_class_discr_value:
9461 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9462 break;
9464 case dw_val_class_discr_list:
9466 dw_discr_list_ref list = AT_discr_list (a);
9467 const int size = size_of_discr_list (list);
9469 /* This is a block, so output its length first. */
9470 dw2_asm_output_data (constant_size (size), size,
9471 "%s: block size", name);
9473 for (; list != NULL; list = list->dw_discr_next)
9475 /* One byte for the discriminant value descriptor, and then as
9476 many LEB128 numbers as required. */
9477 if (list->dw_discr_range)
9478 dw2_asm_output_data (1, DW_DSC_range,
9479 "%s: DW_DSC_range", name);
9480 else
9481 dw2_asm_output_data (1, DW_DSC_label,
9482 "%s: DW_DSC_label", name);
9484 output_discr_value (&list->dw_discr_lower_bound, name);
9485 if (list->dw_discr_range)
9486 output_discr_value (&list->dw_discr_upper_bound, name);
9488 break;
9491 default:
9492 gcc_unreachable ();
9496 FOR_EACH_CHILD (die, c, output_die (c));
9498 /* Add null byte to terminate sibling list. */
9499 if (die->die_child != NULL)
9500 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9501 (unsigned long) die->die_offset);
9504 /* Output the compilation unit that appears at the beginning of the
9505 .debug_info section, and precedes the DIE descriptions. */
9507 static void
9508 output_compilation_unit_header (void)
9510 /* We don't support actual DWARFv5 units yet, we just use some
9511 DWARFv5 draft DIE tags in DWARFv4 format. */
9512 int ver = dwarf_version < 5 ? dwarf_version : 4;
9514 if (!XCOFF_DEBUGGING_INFO)
9516 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9517 dw2_asm_output_data (4, 0xffffffff,
9518 "Initial length escape value indicating 64-bit DWARF extension");
9519 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9520 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9521 "Length of Compilation Unit Info");
9524 dw2_asm_output_data (2, ver, "DWARF version number");
9525 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9526 debug_abbrev_section,
9527 "Offset Into Abbrev. Section");
9528 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9531 /* Output the compilation unit DIE and its children. */
9533 static void
9534 output_comp_unit (dw_die_ref die, int output_if_empty)
9536 const char *secname, *oldsym;
9537 char *tmp;
9539 /* Unless we are outputting main CU, we may throw away empty ones. */
9540 if (!output_if_empty && die->die_child == NULL)
9541 return;
9543 /* Even if there are no children of this DIE, we must output the information
9544 about the compilation unit. Otherwise, on an empty translation unit, we
9545 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9546 will then complain when examining the file. First mark all the DIEs in
9547 this CU so we know which get local refs. */
9548 mark_dies (die);
9550 external_ref_hash_type *extern_map = optimize_external_refs (die);
9552 build_abbrev_table (die, extern_map);
9554 delete extern_map;
9556 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9557 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9558 calc_die_sizes (die);
9560 oldsym = die->die_id.die_symbol;
9561 if (oldsym)
9563 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9565 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9566 secname = tmp;
9567 die->die_id.die_symbol = NULL;
9568 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9570 else
9572 switch_to_section (debug_info_section);
9573 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9574 info_section_emitted = true;
9577 /* Output debugging information. */
9578 output_compilation_unit_header ();
9579 output_die (die);
9581 /* Leave the marks on the main CU, so we can check them in
9582 output_pubnames. */
9583 if (oldsym)
9585 unmark_dies (die);
9586 die->die_id.die_symbol = oldsym;
9590 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9591 and .debug_pubtypes. This is configured per-target, but can be
9592 overridden by the -gpubnames or -gno-pubnames options. */
9594 static inline bool
9595 want_pubnames (void)
9597 if (debug_info_level <= DINFO_LEVEL_TERSE)
9598 return false;
9599 if (debug_generate_pub_sections != -1)
9600 return debug_generate_pub_sections;
9601 return targetm.want_debug_pub_sections;
9604 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9606 static void
9607 add_AT_pubnames (dw_die_ref die)
9609 if (want_pubnames ())
9610 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9613 /* Add a string attribute value to a skeleton DIE. */
9615 static inline void
9616 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9617 const char *str)
9619 dw_attr_node attr;
9620 struct indirect_string_node *node;
9622 if (! skeleton_debug_str_hash)
9623 skeleton_debug_str_hash
9624 = hash_table<indirect_string_hasher>::create_ggc (10);
9626 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9627 find_string_form (node);
9628 if (node->form == DW_FORM_GNU_str_index)
9629 node->form = DW_FORM_strp;
9631 attr.dw_attr = attr_kind;
9632 attr.dw_attr_val.val_class = dw_val_class_str;
9633 attr.dw_attr_val.val_entry = NULL;
9634 attr.dw_attr_val.v.val_str = node;
9635 add_dwarf_attr (die, &attr);
9638 /* Helper function to generate top-level dies for skeleton debug_info and
9639 debug_types. */
9641 static void
9642 add_top_level_skeleton_die_attrs (dw_die_ref die)
9644 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9645 const char *comp_dir = comp_dir_string ();
9647 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9648 if (comp_dir != NULL)
9649 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9650 add_AT_pubnames (die);
9651 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9654 /* Output skeleton debug sections that point to the dwo file. */
9656 static void
9657 output_skeleton_debug_sections (dw_die_ref comp_unit)
9659 /* We don't support actual DWARFv5 units yet, we just use some
9660 DWARFv5 draft DIE tags in DWARFv4 format. */
9661 int ver = dwarf_version < 5 ? dwarf_version : 4;
9663 /* These attributes will be found in the full debug_info section. */
9664 remove_AT (comp_unit, DW_AT_producer);
9665 remove_AT (comp_unit, DW_AT_language);
9667 switch_to_section (debug_skeleton_info_section);
9668 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9670 /* Produce the skeleton compilation-unit header. This one differs enough from
9671 a normal CU header that it's better not to call output_compilation_unit
9672 header. */
9673 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9674 dw2_asm_output_data (4, 0xffffffff,
9675 "Initial length escape value indicating 64-bit DWARF extension");
9677 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9678 DWARF_COMPILE_UNIT_HEADER_SIZE
9679 - DWARF_INITIAL_LENGTH_SIZE
9680 + size_of_die (comp_unit),
9681 "Length of Compilation Unit Info");
9682 dw2_asm_output_data (2, ver, "DWARF version number");
9683 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9684 debug_abbrev_section,
9685 "Offset Into Abbrev. Section");
9686 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9688 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9689 output_die (comp_unit);
9691 /* Build the skeleton debug_abbrev section. */
9692 switch_to_section (debug_skeleton_abbrev_section);
9693 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9695 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9697 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9700 /* Output a comdat type unit DIE and its children. */
9702 static void
9703 output_comdat_type_unit (comdat_type_node *node)
9705 const char *secname;
9706 char *tmp;
9707 int i;
9708 #if defined (OBJECT_FORMAT_ELF)
9709 tree comdat_key;
9710 #endif
9712 /* First mark all the DIEs in this CU so we know which get local refs. */
9713 mark_dies (node->root_die);
9715 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9717 build_abbrev_table (node->root_die, extern_map);
9719 delete extern_map;
9720 extern_map = NULL;
9722 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9723 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9724 calc_die_sizes (node->root_die);
9726 #if defined (OBJECT_FORMAT_ELF)
9727 if (!dwarf_split_debug_info)
9728 secname = ".debug_types";
9729 else
9730 secname = ".debug_types.dwo";
9732 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9733 sprintf (tmp, "wt.");
9734 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9735 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9736 comdat_key = get_identifier (tmp);
9737 targetm.asm_out.named_section (secname,
9738 SECTION_DEBUG | SECTION_LINKONCE,
9739 comdat_key);
9740 #else
9741 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9742 sprintf (tmp, ".gnu.linkonce.wt.");
9743 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9744 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9745 secname = tmp;
9746 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9747 #endif
9749 /* Output debugging information. */
9750 output_compilation_unit_header ();
9751 output_signature (node->signature, "Type Signature");
9752 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9753 "Offset to Type DIE");
9754 output_die (node->root_die);
9756 unmark_dies (node->root_die);
9759 /* Return the DWARF2/3 pubname associated with a decl. */
9761 static const char *
9762 dwarf2_name (tree decl, int scope)
9764 if (DECL_NAMELESS (decl))
9765 return NULL;
9766 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9769 /* Add a new entry to .debug_pubnames if appropriate. */
9771 static void
9772 add_pubname_string (const char *str, dw_die_ref die)
9774 pubname_entry e;
9776 e.die = die;
9777 e.name = xstrdup (str);
9778 vec_safe_push (pubname_table, e);
9781 static void
9782 add_pubname (tree decl, dw_die_ref die)
9784 if (!want_pubnames ())
9785 return;
9787 /* Don't add items to the table when we expect that the consumer will have
9788 just read the enclosing die. For example, if the consumer is looking at a
9789 class_member, it will either be inside the class already, or will have just
9790 looked up the class to find the member. Either way, searching the class is
9791 faster than searching the index. */
9792 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9793 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9795 const char *name = dwarf2_name (decl, 1);
9797 if (name)
9798 add_pubname_string (name, die);
9802 /* Add an enumerator to the pubnames section. */
9804 static void
9805 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9807 pubname_entry e;
9809 gcc_assert (scope_name);
9810 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9811 e.die = die;
9812 vec_safe_push (pubname_table, e);
9815 /* Add a new entry to .debug_pubtypes if appropriate. */
9817 static void
9818 add_pubtype (tree decl, dw_die_ref die)
9820 pubname_entry e;
9822 if (!want_pubnames ())
9823 return;
9825 if ((TREE_PUBLIC (decl)
9826 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9827 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9829 tree scope = NULL;
9830 const char *scope_name = "";
9831 const char *sep = is_cxx () ? "::" : ".";
9832 const char *name;
9834 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9835 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9837 scope_name = lang_hooks.dwarf_name (scope, 1);
9838 if (scope_name != NULL && scope_name[0] != '\0')
9839 scope_name = concat (scope_name, sep, NULL);
9840 else
9841 scope_name = "";
9844 if (TYPE_P (decl))
9845 name = type_tag (decl);
9846 else
9847 name = lang_hooks.dwarf_name (decl, 1);
9849 /* If we don't have a name for the type, there's no point in adding
9850 it to the table. */
9851 if (name != NULL && name[0] != '\0')
9853 e.die = die;
9854 e.name = concat (scope_name, name, NULL);
9855 vec_safe_push (pubtype_table, e);
9858 /* Although it might be more consistent to add the pubinfo for the
9859 enumerators as their dies are created, they should only be added if the
9860 enum type meets the criteria above. So rather than re-check the parent
9861 enum type whenever an enumerator die is created, just output them all
9862 here. This isn't protected by the name conditional because anonymous
9863 enums don't have names. */
9864 if (die->die_tag == DW_TAG_enumeration_type)
9866 dw_die_ref c;
9868 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9873 /* Output a single entry in the pubnames table. */
9875 static void
9876 output_pubname (dw_offset die_offset, pubname_entry *entry)
9878 dw_die_ref die = entry->die;
9879 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9881 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9883 if (debug_generate_pub_sections == 2)
9885 /* This logic follows gdb's method for determining the value of the flag
9886 byte. */
9887 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9888 switch (die->die_tag)
9890 case DW_TAG_typedef:
9891 case DW_TAG_base_type:
9892 case DW_TAG_subrange_type:
9893 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9894 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9895 break;
9896 case DW_TAG_enumerator:
9897 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9898 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9899 if (!is_cxx () && !is_java ())
9900 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9901 break;
9902 case DW_TAG_subprogram:
9903 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9904 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9905 if (!is_ada ())
9906 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9907 break;
9908 case DW_TAG_constant:
9909 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9910 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9911 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9912 break;
9913 case DW_TAG_variable:
9914 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9915 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9916 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9917 break;
9918 case DW_TAG_namespace:
9919 case DW_TAG_imported_declaration:
9920 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9921 break;
9922 case DW_TAG_class_type:
9923 case DW_TAG_interface_type:
9924 case DW_TAG_structure_type:
9925 case DW_TAG_union_type:
9926 case DW_TAG_enumeration_type:
9927 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9928 if (!is_cxx () && !is_java ())
9929 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9930 break;
9931 default:
9932 /* An unusual tag. Leave the flag-byte empty. */
9933 break;
9935 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9936 "GDB-index flags");
9939 dw2_asm_output_nstring (entry->name, -1, "external name");
9943 /* Output the public names table used to speed up access to externally
9944 visible names; or the public types table used to find type definitions. */
9946 static void
9947 output_pubnames (vec<pubname_entry, va_gc> *names)
9949 unsigned i;
9950 unsigned long pubnames_length = size_of_pubnames (names);
9951 pubname_entry *pub;
9953 if (!XCOFF_DEBUGGING_INFO)
9955 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9956 dw2_asm_output_data (4, 0xffffffff,
9957 "Initial length escape value indicating 64-bit DWARF extension");
9958 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9959 "Pub Info Length");
9962 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9963 dw2_asm_output_data (2, 2, "DWARF Version");
9965 if (dwarf_split_debug_info)
9966 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9967 debug_skeleton_info_section,
9968 "Offset of Compilation Unit Info");
9969 else
9970 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9971 debug_info_section,
9972 "Offset of Compilation Unit Info");
9973 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9974 "Compilation Unit Length");
9976 FOR_EACH_VEC_ELT (*names, i, pub)
9978 if (include_pubname_in_output (names, pub))
9980 dw_offset die_offset = pub->die->die_offset;
9982 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9983 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9984 gcc_assert (pub->die->die_mark);
9986 /* If we're putting types in their own .debug_types sections,
9987 the .debug_pubtypes table will still point to the compile
9988 unit (not the type unit), so we want to use the offset of
9989 the skeleton DIE (if there is one). */
9990 if (pub->die->comdat_type_p && names == pubtype_table)
9992 comdat_type_node *type_node = pub->die->die_id.die_type_node;
9994 if (type_node != NULL)
9995 die_offset = (type_node->skeleton_die != NULL
9996 ? type_node->skeleton_die->die_offset
9997 : comp_unit_die ()->die_offset);
10000 output_pubname (die_offset, pub);
10004 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10007 /* Output public names and types tables if necessary. */
10009 static void
10010 output_pubtables (void)
10012 if (!want_pubnames () || !info_section_emitted)
10013 return;
10015 switch_to_section (debug_pubnames_section);
10016 output_pubnames (pubname_table);
10017 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10018 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10019 simply won't look for the section. */
10020 switch_to_section (debug_pubtypes_section);
10021 output_pubnames (pubtype_table);
10025 /* Output the information that goes into the .debug_aranges table.
10026 Namely, define the beginning and ending address range of the
10027 text section generated for this compilation unit. */
10029 static void
10030 output_aranges (void)
10032 unsigned i;
10033 unsigned long aranges_length = size_of_aranges ();
10035 if (!XCOFF_DEBUGGING_INFO)
10037 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10038 dw2_asm_output_data (4, 0xffffffff,
10039 "Initial length escape value indicating 64-bit DWARF extension");
10040 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10041 "Length of Address Ranges Info");
10044 /* Version number for aranges is still 2, even up to DWARF5. */
10045 dw2_asm_output_data (2, 2, "DWARF Version");
10046 if (dwarf_split_debug_info)
10047 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10048 debug_skeleton_info_section,
10049 "Offset of Compilation Unit Info");
10050 else
10051 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10052 debug_info_section,
10053 "Offset of Compilation Unit Info");
10054 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10055 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10057 /* We need to align to twice the pointer size here. */
10058 if (DWARF_ARANGES_PAD_SIZE)
10060 /* Pad using a 2 byte words so that padding is correct for any
10061 pointer size. */
10062 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10063 2 * DWARF2_ADDR_SIZE);
10064 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10065 dw2_asm_output_data (2, 0, NULL);
10068 /* It is necessary not to output these entries if the sections were
10069 not used; if the sections were not used, the length will be 0 and
10070 the address may end up as 0 if the section is discarded by ld
10071 --gc-sections, leaving an invalid (0, 0) entry that can be
10072 confused with the terminator. */
10073 if (text_section_used)
10075 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10076 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10077 text_section_label, "Length");
10079 if (cold_text_section_used)
10081 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10082 "Address");
10083 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10084 cold_text_section_label, "Length");
10087 if (have_multiple_function_sections)
10089 unsigned fde_idx;
10090 dw_fde_ref fde;
10092 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10094 if (DECL_IGNORED_P (fde->decl))
10095 continue;
10096 if (!fde->in_std_section)
10098 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10099 "Address");
10100 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10101 fde->dw_fde_begin, "Length");
10103 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10105 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10106 "Address");
10107 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10108 fde->dw_fde_second_begin, "Length");
10113 /* Output the terminator words. */
10114 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10115 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10118 /* Add a new entry to .debug_ranges. Return the offset at which it
10119 was placed. */
10121 static unsigned int
10122 add_ranges_num (int num)
10124 unsigned int in_use = ranges_table_in_use;
10126 if (in_use == ranges_table_allocated)
10128 ranges_table_allocated += RANGES_TABLE_INCREMENT;
10129 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10130 ranges_table_allocated);
10131 memset (ranges_table + ranges_table_in_use, 0,
10132 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10135 ranges_table[in_use].num = num;
10136 ranges_table_in_use = in_use + 1;
10138 return in_use * 2 * DWARF2_ADDR_SIZE;
10141 /* Add a new entry to .debug_ranges corresponding to a block, or a
10142 range terminator if BLOCK is NULL. */
10144 static unsigned int
10145 add_ranges (const_tree block)
10147 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10150 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10151 When using dwarf_split_debug_info, address attributes in dies destined
10152 for the final executable should be direct references--setting the
10153 parameter force_direct ensures this behavior. */
10155 static void
10156 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10157 bool *added, bool force_direct)
10159 unsigned int in_use = ranges_by_label_in_use;
10160 unsigned int offset;
10162 if (in_use == ranges_by_label_allocated)
10164 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10165 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10166 ranges_by_label_allocated);
10167 memset (ranges_by_label + ranges_by_label_in_use, 0,
10168 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10171 ranges_by_label[in_use].begin = begin;
10172 ranges_by_label[in_use].end = end;
10173 ranges_by_label_in_use = in_use + 1;
10175 offset = add_ranges_num (-(int)in_use - 1);
10176 if (!*added)
10178 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10179 *added = true;
10183 static void
10184 output_ranges (void)
10186 unsigned i;
10187 static const char *const start_fmt = "Offset %#x";
10188 const char *fmt = start_fmt;
10190 for (i = 0; i < ranges_table_in_use; i++)
10192 int block_num = ranges_table[i].num;
10194 if (block_num > 0)
10196 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10197 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10199 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10200 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10202 /* If all code is in the text section, then the compilation
10203 unit base address defaults to DW_AT_low_pc, which is the
10204 base of the text section. */
10205 if (!have_multiple_function_sections)
10207 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10208 text_section_label,
10209 fmt, i * 2 * DWARF2_ADDR_SIZE);
10210 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10211 text_section_label, NULL);
10214 /* Otherwise, the compilation unit base address is zero,
10215 which allows us to use absolute addresses, and not worry
10216 about whether the target supports cross-section
10217 arithmetic. */
10218 else
10220 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10221 fmt, i * 2 * DWARF2_ADDR_SIZE);
10222 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10225 fmt = NULL;
10228 /* Negative block_num stands for an index into ranges_by_label. */
10229 else if (block_num < 0)
10231 int lab_idx = - block_num - 1;
10233 if (!have_multiple_function_sections)
10235 gcc_unreachable ();
10236 #if 0
10237 /* If we ever use add_ranges_by_labels () for a single
10238 function section, all we have to do is to take out
10239 the #if 0 above. */
10240 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10241 ranges_by_label[lab_idx].begin,
10242 text_section_label,
10243 fmt, i * 2 * DWARF2_ADDR_SIZE);
10244 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10245 ranges_by_label[lab_idx].end,
10246 text_section_label, NULL);
10247 #endif
10249 else
10251 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10252 ranges_by_label[lab_idx].begin,
10253 fmt, i * 2 * DWARF2_ADDR_SIZE);
10254 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10255 ranges_by_label[lab_idx].end,
10256 NULL);
10259 else
10261 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10262 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10263 fmt = start_fmt;
10268 /* Data structure containing information about input files. */
10269 struct file_info
10271 const char *path; /* Complete file name. */
10272 const char *fname; /* File name part. */
10273 int length; /* Length of entire string. */
10274 struct dwarf_file_data * file_idx; /* Index in input file table. */
10275 int dir_idx; /* Index in directory table. */
10278 /* Data structure containing information about directories with source
10279 files. */
10280 struct dir_info
10282 const char *path; /* Path including directory name. */
10283 int length; /* Path length. */
10284 int prefix; /* Index of directory entry which is a prefix. */
10285 int count; /* Number of files in this directory. */
10286 int dir_idx; /* Index of directory used as base. */
10289 /* Callback function for file_info comparison. We sort by looking at
10290 the directories in the path. */
10292 static int
10293 file_info_cmp (const void *p1, const void *p2)
10295 const struct file_info *const s1 = (const struct file_info *) p1;
10296 const struct file_info *const s2 = (const struct file_info *) p2;
10297 const unsigned char *cp1;
10298 const unsigned char *cp2;
10300 /* Take care of file names without directories. We need to make sure that
10301 we return consistent values to qsort since some will get confused if
10302 we return the same value when identical operands are passed in opposite
10303 orders. So if neither has a directory, return 0 and otherwise return
10304 1 or -1 depending on which one has the directory. */
10305 if ((s1->path == s1->fname || s2->path == s2->fname))
10306 return (s2->path == s2->fname) - (s1->path == s1->fname);
10308 cp1 = (const unsigned char *) s1->path;
10309 cp2 = (const unsigned char *) s2->path;
10311 while (1)
10313 ++cp1;
10314 ++cp2;
10315 /* Reached the end of the first path? If so, handle like above. */
10316 if ((cp1 == (const unsigned char *) s1->fname)
10317 || (cp2 == (const unsigned char *) s2->fname))
10318 return ((cp2 == (const unsigned char *) s2->fname)
10319 - (cp1 == (const unsigned char *) s1->fname));
10321 /* Character of current path component the same? */
10322 else if (*cp1 != *cp2)
10323 return *cp1 - *cp2;
10327 struct file_name_acquire_data
10329 struct file_info *files;
10330 int used_files;
10331 int max_files;
10334 /* Traversal function for the hash table. */
10337 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10339 struct dwarf_file_data *d = *slot;
10340 struct file_info *fi;
10341 const char *f;
10343 gcc_assert (fnad->max_files >= d->emitted_number);
10345 if (! d->emitted_number)
10346 return 1;
10348 gcc_assert (fnad->max_files != fnad->used_files);
10350 fi = fnad->files + fnad->used_files++;
10352 /* Skip all leading "./". */
10353 f = d->filename;
10354 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10355 f += 2;
10357 /* Create a new array entry. */
10358 fi->path = f;
10359 fi->length = strlen (f);
10360 fi->file_idx = d;
10362 /* Search for the file name part. */
10363 f = strrchr (f, DIR_SEPARATOR);
10364 #if defined (DIR_SEPARATOR_2)
10366 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10368 if (g != NULL)
10370 if (f == NULL || f < g)
10371 f = g;
10374 #endif
10376 fi->fname = f == NULL ? fi->path : f + 1;
10377 return 1;
10380 /* Output the directory table and the file name table. We try to minimize
10381 the total amount of memory needed. A heuristic is used to avoid large
10382 slowdowns with many input files. */
10384 static void
10385 output_file_names (void)
10387 struct file_name_acquire_data fnad;
10388 int numfiles;
10389 struct file_info *files;
10390 struct dir_info *dirs;
10391 int *saved;
10392 int *savehere;
10393 int *backmap;
10394 int ndirs;
10395 int idx_offset;
10396 int i;
10398 if (!last_emitted_file)
10400 dw2_asm_output_data (1, 0, "End directory table");
10401 dw2_asm_output_data (1, 0, "End file name table");
10402 return;
10405 numfiles = last_emitted_file->emitted_number;
10407 /* Allocate the various arrays we need. */
10408 files = XALLOCAVEC (struct file_info, numfiles);
10409 dirs = XALLOCAVEC (struct dir_info, numfiles);
10411 fnad.files = files;
10412 fnad.used_files = 0;
10413 fnad.max_files = numfiles;
10414 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10415 gcc_assert (fnad.used_files == fnad.max_files);
10417 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10419 /* Find all the different directories used. */
10420 dirs[0].path = files[0].path;
10421 dirs[0].length = files[0].fname - files[0].path;
10422 dirs[0].prefix = -1;
10423 dirs[0].count = 1;
10424 dirs[0].dir_idx = 0;
10425 files[0].dir_idx = 0;
10426 ndirs = 1;
10428 for (i = 1; i < numfiles; i++)
10429 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10430 && memcmp (dirs[ndirs - 1].path, files[i].path,
10431 dirs[ndirs - 1].length) == 0)
10433 /* Same directory as last entry. */
10434 files[i].dir_idx = ndirs - 1;
10435 ++dirs[ndirs - 1].count;
10437 else
10439 int j;
10441 /* This is a new directory. */
10442 dirs[ndirs].path = files[i].path;
10443 dirs[ndirs].length = files[i].fname - files[i].path;
10444 dirs[ndirs].count = 1;
10445 dirs[ndirs].dir_idx = ndirs;
10446 files[i].dir_idx = ndirs;
10448 /* Search for a prefix. */
10449 dirs[ndirs].prefix = -1;
10450 for (j = 0; j < ndirs; j++)
10451 if (dirs[j].length < dirs[ndirs].length
10452 && dirs[j].length > 1
10453 && (dirs[ndirs].prefix == -1
10454 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10455 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10456 dirs[ndirs].prefix = j;
10458 ++ndirs;
10461 /* Now to the actual work. We have to find a subset of the directories which
10462 allow expressing the file name using references to the directory table
10463 with the least amount of characters. We do not do an exhaustive search
10464 where we would have to check out every combination of every single
10465 possible prefix. Instead we use a heuristic which provides nearly optimal
10466 results in most cases and never is much off. */
10467 saved = XALLOCAVEC (int, ndirs);
10468 savehere = XALLOCAVEC (int, ndirs);
10470 memset (saved, '\0', ndirs * sizeof (saved[0]));
10471 for (i = 0; i < ndirs; i++)
10473 int j;
10474 int total;
10476 /* We can always save some space for the current directory. But this
10477 does not mean it will be enough to justify adding the directory. */
10478 savehere[i] = dirs[i].length;
10479 total = (savehere[i] - saved[i]) * dirs[i].count;
10481 for (j = i + 1; j < ndirs; j++)
10483 savehere[j] = 0;
10484 if (saved[j] < dirs[i].length)
10486 /* Determine whether the dirs[i] path is a prefix of the
10487 dirs[j] path. */
10488 int k;
10490 k = dirs[j].prefix;
10491 while (k != -1 && k != (int) i)
10492 k = dirs[k].prefix;
10494 if (k == (int) i)
10496 /* Yes it is. We can possibly save some memory by
10497 writing the filenames in dirs[j] relative to
10498 dirs[i]. */
10499 savehere[j] = dirs[i].length;
10500 total += (savehere[j] - saved[j]) * dirs[j].count;
10505 /* Check whether we can save enough to justify adding the dirs[i]
10506 directory. */
10507 if (total > dirs[i].length + 1)
10509 /* It's worthwhile adding. */
10510 for (j = i; j < ndirs; j++)
10511 if (savehere[j] > 0)
10513 /* Remember how much we saved for this directory so far. */
10514 saved[j] = savehere[j];
10516 /* Remember the prefix directory. */
10517 dirs[j].dir_idx = i;
10522 /* Emit the directory name table. */
10523 idx_offset = dirs[0].length > 0 ? 1 : 0;
10524 for (i = 1 - idx_offset; i < ndirs; i++)
10525 dw2_asm_output_nstring (dirs[i].path,
10526 dirs[i].length
10527 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10528 "Directory Entry: %#x", i + idx_offset);
10530 dw2_asm_output_data (1, 0, "End directory table");
10532 /* We have to emit them in the order of emitted_number since that's
10533 used in the debug info generation. To do this efficiently we
10534 generate a back-mapping of the indices first. */
10535 backmap = XALLOCAVEC (int, numfiles);
10536 for (i = 0; i < numfiles; i++)
10537 backmap[files[i].file_idx->emitted_number - 1] = i;
10539 /* Now write all the file names. */
10540 for (i = 0; i < numfiles; i++)
10542 int file_idx = backmap[i];
10543 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10545 #ifdef VMS_DEBUGGING_INFO
10546 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10548 /* Setting these fields can lead to debugger miscomparisons,
10549 but VMS Debug requires them to be set correctly. */
10551 int ver;
10552 long long cdt;
10553 long siz;
10554 int maxfilelen = strlen (files[file_idx].path)
10555 + dirs[dir_idx].length
10556 + MAX_VMS_VERSION_LEN + 1;
10557 char *filebuf = XALLOCAVEC (char, maxfilelen);
10559 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10560 snprintf (filebuf, maxfilelen, "%s;%d",
10561 files[file_idx].path + dirs[dir_idx].length, ver);
10563 dw2_asm_output_nstring
10564 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10566 /* Include directory index. */
10567 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10569 /* Modification time. */
10570 dw2_asm_output_data_uleb128
10571 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10572 ? cdt : 0,
10573 NULL);
10575 /* File length in bytes. */
10576 dw2_asm_output_data_uleb128
10577 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10578 ? siz : 0,
10579 NULL);
10580 #else
10581 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10582 "File Entry: %#x", (unsigned) i + 1);
10584 /* Include directory index. */
10585 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10587 /* Modification time. */
10588 dw2_asm_output_data_uleb128 (0, NULL);
10590 /* File length in bytes. */
10591 dw2_asm_output_data_uleb128 (0, NULL);
10592 #endif /* VMS_DEBUGGING_INFO */
10595 dw2_asm_output_data (1, 0, "End file name table");
10599 /* Output one line number table into the .debug_line section. */
10601 static void
10602 output_one_line_info_table (dw_line_info_table *table)
10604 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10605 unsigned int current_line = 1;
10606 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10607 dw_line_info_entry *ent;
10608 size_t i;
10610 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10612 switch (ent->opcode)
10614 case LI_set_address:
10615 /* ??? Unfortunately, we have little choice here currently, and
10616 must always use the most general form. GCC does not know the
10617 address delta itself, so we can't use DW_LNS_advance_pc. Many
10618 ports do have length attributes which will give an upper bound
10619 on the address range. We could perhaps use length attributes
10620 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10621 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10623 /* This can handle any delta. This takes
10624 4+DWARF2_ADDR_SIZE bytes. */
10625 dw2_asm_output_data (1, 0, "set address %s", line_label);
10626 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10627 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10628 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10629 break;
10631 case LI_set_line:
10632 if (ent->val == current_line)
10634 /* We still need to start a new row, so output a copy insn. */
10635 dw2_asm_output_data (1, DW_LNS_copy,
10636 "copy line %u", current_line);
10638 else
10640 int line_offset = ent->val - current_line;
10641 int line_delta = line_offset - DWARF_LINE_BASE;
10643 current_line = ent->val;
10644 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10646 /* This can handle deltas from -10 to 234, using the current
10647 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10648 This takes 1 byte. */
10649 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10650 "line %u", current_line);
10652 else
10654 /* This can handle any delta. This takes at least 4 bytes,
10655 depending on the value being encoded. */
10656 dw2_asm_output_data (1, DW_LNS_advance_line,
10657 "advance to line %u", current_line);
10658 dw2_asm_output_data_sleb128 (line_offset, NULL);
10659 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10662 break;
10664 case LI_set_file:
10665 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10666 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10667 break;
10669 case LI_set_column:
10670 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10671 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10672 break;
10674 case LI_negate_stmt:
10675 current_is_stmt = !current_is_stmt;
10676 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10677 "is_stmt %d", current_is_stmt);
10678 break;
10680 case LI_set_prologue_end:
10681 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10682 "set prologue end");
10683 break;
10685 case LI_set_epilogue_begin:
10686 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10687 "set epilogue begin");
10688 break;
10690 case LI_set_discriminator:
10691 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10692 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10693 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10694 dw2_asm_output_data_uleb128 (ent->val, NULL);
10695 break;
10699 /* Emit debug info for the address of the end of the table. */
10700 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10701 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10702 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10703 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10705 dw2_asm_output_data (1, 0, "end sequence");
10706 dw2_asm_output_data_uleb128 (1, NULL);
10707 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10710 /* Output the source line number correspondence information. This
10711 information goes into the .debug_line section. */
10713 static void
10714 output_line_info (bool prologue_only)
10716 char l1[20], l2[20], p1[20], p2[20];
10717 /* We don't support DWARFv5 line tables yet. */
10718 int ver = dwarf_version < 5 ? dwarf_version : 4;
10719 bool saw_one = false;
10720 int opc;
10722 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10723 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10724 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10725 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10727 if (!XCOFF_DEBUGGING_INFO)
10729 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10730 dw2_asm_output_data (4, 0xffffffff,
10731 "Initial length escape value indicating 64-bit DWARF extension");
10732 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10733 "Length of Source Line Info");
10736 ASM_OUTPUT_LABEL (asm_out_file, l1);
10738 dw2_asm_output_data (2, ver, "DWARF Version");
10739 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10740 ASM_OUTPUT_LABEL (asm_out_file, p1);
10742 /* Define the architecture-dependent minimum instruction length (in bytes).
10743 In this implementation of DWARF, this field is used for information
10744 purposes only. Since GCC generates assembly language, we have no
10745 a priori knowledge of how many instruction bytes are generated for each
10746 source line, and therefore can use only the DW_LNE_set_address and
10747 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10748 this as '1', which is "correct enough" for all architectures,
10749 and don't let the target override. */
10750 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10752 if (ver >= 4)
10753 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10754 "Maximum Operations Per Instruction");
10755 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10756 "Default is_stmt_start flag");
10757 dw2_asm_output_data (1, DWARF_LINE_BASE,
10758 "Line Base Value (Special Opcodes)");
10759 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10760 "Line Range Value (Special Opcodes)");
10761 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10762 "Special Opcode Base");
10764 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10766 int n_op_args;
10767 switch (opc)
10769 case DW_LNS_advance_pc:
10770 case DW_LNS_advance_line:
10771 case DW_LNS_set_file:
10772 case DW_LNS_set_column:
10773 case DW_LNS_fixed_advance_pc:
10774 case DW_LNS_set_isa:
10775 n_op_args = 1;
10776 break;
10777 default:
10778 n_op_args = 0;
10779 break;
10782 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10783 opc, n_op_args);
10786 /* Write out the information about the files we use. */
10787 output_file_names ();
10788 ASM_OUTPUT_LABEL (asm_out_file, p2);
10789 if (prologue_only)
10791 /* Output the marker for the end of the line number info. */
10792 ASM_OUTPUT_LABEL (asm_out_file, l2);
10793 return;
10796 if (separate_line_info)
10798 dw_line_info_table *table;
10799 size_t i;
10801 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10802 if (table->in_use)
10804 output_one_line_info_table (table);
10805 saw_one = true;
10808 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10810 output_one_line_info_table (cold_text_section_line_info);
10811 saw_one = true;
10814 /* ??? Some Darwin linkers crash on a .debug_line section with no
10815 sequences. Further, merely a DW_LNE_end_sequence entry is not
10816 sufficient -- the address column must also be initialized.
10817 Make sure to output at least one set_address/end_sequence pair,
10818 choosing .text since that section is always present. */
10819 if (text_section_line_info->in_use || !saw_one)
10820 output_one_line_info_table (text_section_line_info);
10822 /* Output the marker for the end of the line number info. */
10823 ASM_OUTPUT_LABEL (asm_out_file, l2);
10826 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
10828 static inline bool
10829 need_endianity_attribute_p (bool reverse)
10831 return reverse && (dwarf_version >= 3 || !dwarf_strict);
10834 /* Given a pointer to a tree node for some base type, return a pointer to
10835 a DIE that describes the given type. REVERSE is true if the type is
10836 to be interpreted in the reverse storage order wrt the target order.
10838 This routine must only be called for GCC type nodes that correspond to
10839 Dwarf base (fundamental) types. */
10841 static dw_die_ref
10842 base_type_die (tree type, bool reverse)
10844 dw_die_ref base_type_result;
10845 enum dwarf_type encoding;
10846 bool fpt_used = false;
10847 struct fixed_point_type_info fpt_info;
10848 tree type_bias = NULL_TREE;
10850 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10851 return 0;
10853 /* If this is a subtype that should not be emitted as a subrange type,
10854 use the base type. See subrange_type_for_debug_p. */
10855 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10856 type = TREE_TYPE (type);
10858 switch (TREE_CODE (type))
10860 case INTEGER_TYPE:
10861 if ((dwarf_version >= 4 || !dwarf_strict)
10862 && TYPE_NAME (type)
10863 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10864 && DECL_IS_BUILTIN (TYPE_NAME (type))
10865 && DECL_NAME (TYPE_NAME (type)))
10867 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10868 if (strcmp (name, "char16_t") == 0
10869 || strcmp (name, "char32_t") == 0)
10871 encoding = DW_ATE_UTF;
10872 break;
10875 if ((dwarf_version >= 3 || !dwarf_strict)
10876 && lang_hooks.types.get_fixed_point_type_info)
10878 memset (&fpt_info, 0, sizeof (fpt_info));
10879 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10881 fpt_used = true;
10882 encoding = ((TYPE_UNSIGNED (type))
10883 ? DW_ATE_unsigned_fixed
10884 : DW_ATE_signed_fixed);
10885 break;
10888 if (TYPE_STRING_FLAG (type))
10890 if (TYPE_UNSIGNED (type))
10891 encoding = DW_ATE_unsigned_char;
10892 else
10893 encoding = DW_ATE_signed_char;
10895 else if (TYPE_UNSIGNED (type))
10896 encoding = DW_ATE_unsigned;
10897 else
10898 encoding = DW_ATE_signed;
10900 if (!dwarf_strict
10901 && lang_hooks.types.get_type_bias)
10902 type_bias = lang_hooks.types.get_type_bias (type);
10903 break;
10905 case REAL_TYPE:
10906 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10908 if (dwarf_version >= 3 || !dwarf_strict)
10909 encoding = DW_ATE_decimal_float;
10910 else
10911 encoding = DW_ATE_lo_user;
10913 else
10914 encoding = DW_ATE_float;
10915 break;
10917 case FIXED_POINT_TYPE:
10918 if (!(dwarf_version >= 3 || !dwarf_strict))
10919 encoding = DW_ATE_lo_user;
10920 else if (TYPE_UNSIGNED (type))
10921 encoding = DW_ATE_unsigned_fixed;
10922 else
10923 encoding = DW_ATE_signed_fixed;
10924 break;
10926 /* Dwarf2 doesn't know anything about complex ints, so use
10927 a user defined type for it. */
10928 case COMPLEX_TYPE:
10929 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10930 encoding = DW_ATE_complex_float;
10931 else
10932 encoding = DW_ATE_lo_user;
10933 break;
10935 case BOOLEAN_TYPE:
10936 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10937 encoding = DW_ATE_boolean;
10938 break;
10940 default:
10941 /* No other TREE_CODEs are Dwarf fundamental types. */
10942 gcc_unreachable ();
10945 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10947 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10948 int_size_in_bytes (type));
10949 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10951 if (need_endianity_attribute_p (reverse))
10952 add_AT_unsigned (base_type_result, DW_AT_endianity,
10953 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10955 if (fpt_used)
10957 switch (fpt_info.scale_factor_kind)
10959 case fixed_point_scale_factor_binary:
10960 add_AT_int (base_type_result, DW_AT_binary_scale,
10961 fpt_info.scale_factor.binary);
10962 break;
10964 case fixed_point_scale_factor_decimal:
10965 add_AT_int (base_type_result, DW_AT_decimal_scale,
10966 fpt_info.scale_factor.decimal);
10967 break;
10969 case fixed_point_scale_factor_arbitrary:
10970 /* Arbitrary scale factors cannot be described in standard DWARF,
10971 yet. */
10972 if (!dwarf_strict)
10974 /* Describe the scale factor as a rational constant. */
10975 const dw_die_ref scale_factor
10976 = new_die (DW_TAG_constant, comp_unit_die (), type);
10978 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
10979 fpt_info.scale_factor.arbitrary.numerator);
10980 add_AT_int (scale_factor, DW_AT_GNU_denominator,
10981 fpt_info.scale_factor.arbitrary.denominator);
10983 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
10985 break;
10987 default:
10988 gcc_unreachable ();
10992 if (type_bias)
10993 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
10994 dw_scalar_form_constant
10995 | dw_scalar_form_exprloc
10996 | dw_scalar_form_reference,
10997 NULL);
10999 add_pubtype (type, base_type_result);
11001 return base_type_result;
11004 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
11005 named 'auto' in its type: return true for it, false otherwise. */
11007 static inline bool
11008 is_cxx_auto (tree type)
11010 if (is_cxx ())
11012 tree name = TYPE_IDENTIFIER (type);
11013 if (name == get_identifier ("auto")
11014 || name == get_identifier ("decltype(auto)"))
11015 return true;
11017 return false;
11020 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11021 given input type is a Dwarf "fundamental" type. Otherwise return null. */
11023 static inline int
11024 is_base_type (tree type)
11026 switch (TREE_CODE (type))
11028 case ERROR_MARK:
11029 case VOID_TYPE:
11030 case INTEGER_TYPE:
11031 case REAL_TYPE:
11032 case FIXED_POINT_TYPE:
11033 case COMPLEX_TYPE:
11034 case BOOLEAN_TYPE:
11035 case POINTER_BOUNDS_TYPE:
11036 return 1;
11038 case ARRAY_TYPE:
11039 case RECORD_TYPE:
11040 case UNION_TYPE:
11041 case QUAL_UNION_TYPE:
11042 case ENUMERAL_TYPE:
11043 case FUNCTION_TYPE:
11044 case METHOD_TYPE:
11045 case POINTER_TYPE:
11046 case REFERENCE_TYPE:
11047 case NULLPTR_TYPE:
11048 case OFFSET_TYPE:
11049 case LANG_TYPE:
11050 case VECTOR_TYPE:
11051 return 0;
11053 default:
11054 if (is_cxx_auto (type))
11055 return 0;
11056 gcc_unreachable ();
11059 return 0;
11062 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11063 node, return the size in bits for the type if it is a constant, or else
11064 return the alignment for the type if the type's size is not constant, or
11065 else return BITS_PER_WORD if the type actually turns out to be an
11066 ERROR_MARK node. */
11068 static inline unsigned HOST_WIDE_INT
11069 simple_type_size_in_bits (const_tree type)
11071 if (TREE_CODE (type) == ERROR_MARK)
11072 return BITS_PER_WORD;
11073 else if (TYPE_SIZE (type) == NULL_TREE)
11074 return 0;
11075 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11076 return tree_to_uhwi (TYPE_SIZE (type));
11077 else
11078 return TYPE_ALIGN (type);
11081 /* Similarly, but return an offset_int instead of UHWI. */
11083 static inline offset_int
11084 offset_int_type_size_in_bits (const_tree type)
11086 if (TREE_CODE (type) == ERROR_MARK)
11087 return BITS_PER_WORD;
11088 else if (TYPE_SIZE (type) == NULL_TREE)
11089 return 0;
11090 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11091 return wi::to_offset (TYPE_SIZE (type));
11092 else
11093 return TYPE_ALIGN (type);
11096 /* Given a pointer to a tree node for a subrange type, return a pointer
11097 to a DIE that describes the given type. */
11099 static dw_die_ref
11100 subrange_type_die (tree type, tree low, tree high, tree bias,
11101 dw_die_ref context_die)
11103 dw_die_ref subrange_die;
11104 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11106 if (context_die == NULL)
11107 context_die = comp_unit_die ();
11109 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11111 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11113 /* The size of the subrange type and its base type do not match,
11114 so we need to generate a size attribute for the subrange type. */
11115 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11118 if (low)
11119 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11120 if (high)
11121 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11122 if (bias && !dwarf_strict)
11123 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11124 dw_scalar_form_constant
11125 | dw_scalar_form_exprloc
11126 | dw_scalar_form_reference,
11127 NULL);
11129 return subrange_die;
11132 /* Returns the (const and/or volatile) cv_qualifiers associated with
11133 the decl node. This will normally be augmented with the
11134 cv_qualifiers of the underlying type in add_type_attribute. */
11136 static int
11137 decl_quals (const_tree decl)
11139 return ((TREE_READONLY (decl)
11140 /* The C++ front-end correctly marks reference-typed
11141 variables as readonly, but from a language (and debug
11142 info) standpoint they are not const-qualified. */
11143 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
11144 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11145 | (TREE_THIS_VOLATILE (decl)
11146 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11149 /* Determine the TYPE whose qualifiers match the largest strict subset
11150 of the given TYPE_QUALS, and return its qualifiers. Ignore all
11151 qualifiers outside QUAL_MASK. */
11153 static int
11154 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11156 tree t;
11157 int best_rank = 0, best_qual = 0, max_rank;
11159 type_quals &= qual_mask;
11160 max_rank = popcount_hwi (type_quals) - 1;
11162 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11163 t = TYPE_NEXT_VARIANT (t))
11165 int q = TYPE_QUALS (t) & qual_mask;
11167 if ((q & type_quals) == q && q != type_quals
11168 && check_base_type (t, type))
11170 int rank = popcount_hwi (q);
11172 if (rank > best_rank)
11174 best_rank = rank;
11175 best_qual = q;
11180 return best_qual;
11183 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11184 static const dwarf_qual_info_t dwarf_qual_info[] =
11186 { TYPE_QUAL_CONST, DW_TAG_const_type },
11187 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11188 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11189 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11191 static const unsigned int dwarf_qual_info_size
11192 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11194 /* If DIE is a qualified DIE of some base DIE with the same parent,
11195 return the base DIE, otherwise return NULL. Set MASK to the
11196 qualifiers added compared to the returned DIE. */
11198 static dw_die_ref
11199 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11201 unsigned int i;
11202 for (i = 0; i < dwarf_qual_info_size; i++)
11203 if (die->die_tag == dwarf_qual_info[i].t)
11204 break;
11205 if (i == dwarf_qual_info_size)
11206 return NULL;
11207 if (vec_safe_length (die->die_attr) != 1)
11208 return NULL;
11209 dw_die_ref type = get_AT_ref (die, DW_AT_type);
11210 if (type == NULL || type->die_parent != die->die_parent)
11211 return NULL;
11212 *mask |= dwarf_qual_info[i].q;
11213 if (depth)
11215 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11216 if (ret)
11217 return ret;
11219 return type;
11222 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11223 entry that chains the modifiers specified by CV_QUALS in front of the
11224 given type. REVERSE is true if the type is to be interpreted in the
11225 reverse storage order wrt the target order. */
11227 static dw_die_ref
11228 modified_type_die (tree type, int cv_quals, bool reverse,
11229 dw_die_ref context_die)
11231 enum tree_code code = TREE_CODE (type);
11232 dw_die_ref mod_type_die;
11233 dw_die_ref sub_die = NULL;
11234 tree item_type = NULL;
11235 tree qualified_type;
11236 tree name, low, high;
11237 dw_die_ref mod_scope;
11238 /* Only these cv-qualifiers are currently handled. */
11239 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11240 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11242 if (code == ERROR_MARK)
11243 return NULL;
11245 if (lang_hooks.types.get_debug_type)
11247 tree debug_type = lang_hooks.types.get_debug_type (type);
11249 if (debug_type != NULL_TREE && debug_type != type)
11250 return modified_type_die (debug_type, cv_quals, reverse, context_die);
11253 cv_quals &= cv_qual_mask;
11255 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11256 tag modifier (and not an attribute) old consumers won't be able
11257 to handle it. */
11258 if (dwarf_version < 3)
11259 cv_quals &= ~TYPE_QUAL_RESTRICT;
11261 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
11262 if (dwarf_version < 5)
11263 cv_quals &= ~TYPE_QUAL_ATOMIC;
11265 /* See if we already have the appropriately qualified variant of
11266 this type. */
11267 qualified_type = get_qualified_type (type, cv_quals);
11269 if (qualified_type == sizetype
11270 && TYPE_NAME (qualified_type)
11271 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11273 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11275 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11276 && TYPE_PRECISION (t)
11277 == TYPE_PRECISION (qualified_type)
11278 && TYPE_UNSIGNED (t)
11279 == TYPE_UNSIGNED (qualified_type));
11280 qualified_type = t;
11283 /* If we do, then we can just use its DIE, if it exists. */
11284 if (qualified_type)
11286 mod_type_die = lookup_type_die (qualified_type);
11288 /* DW_AT_endianity doesn't come from a qualifier on the type. */
11289 if (mod_type_die
11290 && (!need_endianity_attribute_p (reverse)
11291 || !is_base_type (type)
11292 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11293 return mod_type_die;
11296 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11298 /* Handle C typedef types. */
11299 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11300 && !DECL_ARTIFICIAL (name))
11302 tree dtype = TREE_TYPE (name);
11304 if (qualified_type == dtype)
11306 /* For a named type, use the typedef. */
11307 gen_type_die (qualified_type, context_die);
11308 return lookup_type_die (qualified_type);
11310 else
11312 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11313 dquals &= cv_qual_mask;
11314 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11315 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11316 /* cv-unqualified version of named type. Just use
11317 the unnamed type to which it refers. */
11318 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11319 reverse, context_die);
11320 /* Else cv-qualified version of named type; fall through. */
11324 mod_scope = scope_die_for (type, context_die);
11326 if (cv_quals)
11328 int sub_quals = 0, first_quals = 0;
11329 unsigned i;
11330 dw_die_ref first = NULL, last = NULL;
11332 /* Determine a lesser qualified type that most closely matches
11333 this one. Then generate DW_TAG_* entries for the remaining
11334 qualifiers. */
11335 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11336 cv_qual_mask);
11337 if (sub_quals && use_debug_types)
11339 bool needed = false;
11340 /* If emitting type units, make sure the order of qualifiers
11341 is canonical. Thus, start from unqualified type if
11342 an earlier qualifier is missing in sub_quals, but some later
11343 one is present there. */
11344 for (i = 0; i < dwarf_qual_info_size; i++)
11345 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11346 needed = true;
11347 else if (needed && (dwarf_qual_info[i].q & cv_quals))
11349 sub_quals = 0;
11350 break;
11353 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11354 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11356 /* As not all intermediate qualified DIEs have corresponding
11357 tree types, ensure that qualified DIEs in the same scope
11358 as their DW_AT_type are emitted after their DW_AT_type,
11359 only with other qualified DIEs for the same type possibly
11360 in between them. Determine the range of such qualified
11361 DIEs now (first being the base type, last being corresponding
11362 last qualified DIE for it). */
11363 unsigned int count = 0;
11364 first = qualified_die_p (mod_type_die, &first_quals,
11365 dwarf_qual_info_size);
11366 if (first == NULL)
11367 first = mod_type_die;
11368 gcc_assert ((first_quals & ~sub_quals) == 0);
11369 for (count = 0, last = first;
11370 count < (1U << dwarf_qual_info_size);
11371 count++, last = last->die_sib)
11373 int quals = 0;
11374 if (last == mod_scope->die_child)
11375 break;
11376 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11377 != first)
11378 break;
11382 for (i = 0; i < dwarf_qual_info_size; i++)
11383 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11385 dw_die_ref d;
11386 if (first && first != last)
11388 for (d = first->die_sib; ; d = d->die_sib)
11390 int quals = 0;
11391 qualified_die_p (d, &quals, dwarf_qual_info_size);
11392 if (quals == (first_quals | dwarf_qual_info[i].q))
11393 break;
11394 if (d == last)
11396 d = NULL;
11397 break;
11400 if (d)
11402 mod_type_die = d;
11403 continue;
11406 if (first)
11408 d = ggc_cleared_alloc<die_node> ();
11409 d->die_tag = dwarf_qual_info[i].t;
11410 add_child_die_after (mod_scope, d, last);
11411 last = d;
11413 else
11414 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11415 if (mod_type_die)
11416 add_AT_die_ref (d, DW_AT_type, mod_type_die);
11417 mod_type_die = d;
11418 first_quals |= dwarf_qual_info[i].q;
11421 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11423 dwarf_tag tag = DW_TAG_pointer_type;
11424 if (code == REFERENCE_TYPE)
11426 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11427 tag = DW_TAG_rvalue_reference_type;
11428 else
11429 tag = DW_TAG_reference_type;
11431 mod_type_die = new_die (tag, mod_scope, type);
11433 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11434 simple_type_size_in_bits (type) / BITS_PER_UNIT);
11435 item_type = TREE_TYPE (type);
11437 addr_space_t as = TYPE_ADDR_SPACE (item_type);
11438 if (!ADDR_SPACE_GENERIC_P (as))
11440 int action = targetm.addr_space.debug (as);
11441 if (action >= 0)
11443 /* Positive values indicate an address_class. */
11444 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11446 else
11448 /* Negative values indicate an (inverted) segment base reg. */
11449 dw_loc_descr_ref d
11450 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11451 add_AT_loc (mod_type_die, DW_AT_segment, d);
11455 else if (code == INTEGER_TYPE
11456 && TREE_TYPE (type) != NULL_TREE
11457 && subrange_type_for_debug_p (type, &low, &high))
11459 tree bias = NULL_TREE;
11460 if (lang_hooks.types.get_type_bias)
11461 bias = lang_hooks.types.get_type_bias (type);
11462 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11463 item_type = TREE_TYPE (type);
11465 else if (is_base_type (type))
11466 mod_type_die = base_type_die (type, reverse);
11467 else
11469 gen_type_die (type, context_die);
11471 /* We have to get the type_main_variant here (and pass that to the
11472 `lookup_type_die' routine) because the ..._TYPE node we have
11473 might simply be a *copy* of some original type node (where the
11474 copy was created to help us keep track of typedef names) and
11475 that copy might have a different TYPE_UID from the original
11476 ..._TYPE node. */
11477 if (TREE_CODE (type) != VECTOR_TYPE
11478 && TREE_CODE (type) != ARRAY_TYPE)
11479 return lookup_type_die (type_main_variant (type));
11480 else
11481 /* Vectors have the debugging information in the type,
11482 not the main variant. */
11483 return lookup_type_die (type);
11486 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
11487 don't output a DW_TAG_typedef, since there isn't one in the
11488 user's program; just attach a DW_AT_name to the type.
11489 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11490 if the base type already has the same name. */
11491 if (name
11492 && ((TREE_CODE (name) != TYPE_DECL
11493 && (qualified_type == TYPE_MAIN_VARIANT (type)
11494 || (cv_quals == TYPE_UNQUALIFIED)))
11495 || (TREE_CODE (name) == TYPE_DECL
11496 && TREE_TYPE (name) == qualified_type
11497 && DECL_NAME (name))))
11499 if (TREE_CODE (name) == TYPE_DECL)
11500 /* Could just call add_name_and_src_coords_attributes here,
11501 but since this is a builtin type it doesn't have any
11502 useful source coordinates anyway. */
11503 name = DECL_NAME (name);
11504 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11506 /* This probably indicates a bug. */
11507 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11509 name = TYPE_IDENTIFIER (type);
11510 add_name_attribute (mod_type_die,
11511 name ? IDENTIFIER_POINTER (name) : "__unknown__");
11514 if (qualified_type)
11515 equate_type_number_to_die (qualified_type, mod_type_die);
11517 if (item_type)
11518 /* We must do this after the equate_type_number_to_die call, in case
11519 this is a recursive type. This ensures that the modified_type_die
11520 recursion will terminate even if the type is recursive. Recursive
11521 types are possible in Ada. */
11522 sub_die = modified_type_die (item_type,
11523 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11524 reverse,
11525 context_die);
11527 if (sub_die != NULL)
11528 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11530 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11531 if (TYPE_ARTIFICIAL (type))
11532 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11534 return mod_type_die;
11537 /* Generate DIEs for the generic parameters of T.
11538 T must be either a generic type or a generic function.
11539 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11541 static void
11542 gen_generic_params_dies (tree t)
11544 tree parms, args;
11545 int parms_num, i;
11546 dw_die_ref die = NULL;
11547 int non_default;
11549 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11550 return;
11552 if (TYPE_P (t))
11553 die = lookup_type_die (t);
11554 else if (DECL_P (t))
11555 die = lookup_decl_die (t);
11557 gcc_assert (die);
11559 parms = lang_hooks.get_innermost_generic_parms (t);
11560 if (!parms)
11561 /* T has no generic parameter. It means T is neither a generic type
11562 or function. End of story. */
11563 return;
11565 parms_num = TREE_VEC_LENGTH (parms);
11566 args = lang_hooks.get_innermost_generic_args (t);
11567 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11568 non_default = int_cst_value (TREE_CHAIN (args));
11569 else
11570 non_default = TREE_VEC_LENGTH (args);
11571 for (i = 0; i < parms_num; i++)
11573 tree parm, arg, arg_pack_elems;
11574 dw_die_ref parm_die;
11576 parm = TREE_VEC_ELT (parms, i);
11577 arg = TREE_VEC_ELT (args, i);
11578 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11579 gcc_assert (parm && TREE_VALUE (parm) && arg);
11581 if (parm && TREE_VALUE (parm) && arg)
11583 /* If PARM represents a template parameter pack,
11584 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11585 by DW_TAG_template_*_parameter DIEs for the argument
11586 pack elements of ARG. Note that ARG would then be
11587 an argument pack. */
11588 if (arg_pack_elems)
11589 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11590 arg_pack_elems,
11591 die);
11592 else
11593 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11594 true /* emit name */, die);
11595 if (i >= non_default)
11596 add_AT_flag (parm_die, DW_AT_default_value, 1);
11601 /* Create and return a DIE for PARM which should be
11602 the representation of a generic type parameter.
11603 For instance, in the C++ front end, PARM would be a template parameter.
11604 ARG is the argument to PARM.
11605 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11606 name of the PARM.
11607 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11608 as a child node. */
11610 static dw_die_ref
11611 generic_parameter_die (tree parm, tree arg,
11612 bool emit_name_p,
11613 dw_die_ref parent_die)
11615 dw_die_ref tmpl_die = NULL;
11616 const char *name = NULL;
11618 if (!parm || !DECL_NAME (parm) || !arg)
11619 return NULL;
11621 /* We support non-type generic parameters and arguments,
11622 type generic parameters and arguments, as well as
11623 generic generic parameters (a.k.a. template template parameters in C++)
11624 and arguments. */
11625 if (TREE_CODE (parm) == PARM_DECL)
11626 /* PARM is a nontype generic parameter */
11627 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11628 else if (TREE_CODE (parm) == TYPE_DECL)
11629 /* PARM is a type generic parameter. */
11630 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11631 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11632 /* PARM is a generic generic parameter.
11633 Its DIE is a GNU extension. It shall have a
11634 DW_AT_name attribute to represent the name of the template template
11635 parameter, and a DW_AT_GNU_template_name attribute to represent the
11636 name of the template template argument. */
11637 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11638 parent_die, parm);
11639 else
11640 gcc_unreachable ();
11642 if (tmpl_die)
11644 tree tmpl_type;
11646 /* If PARM is a generic parameter pack, it means we are
11647 emitting debug info for a template argument pack element.
11648 In other terms, ARG is a template argument pack element.
11649 In that case, we don't emit any DW_AT_name attribute for
11650 the die. */
11651 if (emit_name_p)
11653 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11654 gcc_assert (name);
11655 add_AT_string (tmpl_die, DW_AT_name, name);
11658 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11660 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11661 TMPL_DIE should have a child DW_AT_type attribute that is set
11662 to the type of the argument to PARM, which is ARG.
11663 If PARM is a type generic parameter, TMPL_DIE should have a
11664 child DW_AT_type that is set to ARG. */
11665 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11666 add_type_attribute (tmpl_die, tmpl_type,
11667 (TREE_THIS_VOLATILE (tmpl_type)
11668 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11669 false, parent_die);
11671 else
11673 /* So TMPL_DIE is a DIE representing a
11674 a generic generic template parameter, a.k.a template template
11675 parameter in C++ and arg is a template. */
11677 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11678 to the name of the argument. */
11679 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11680 if (name)
11681 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11684 if (TREE_CODE (parm) == PARM_DECL)
11685 /* So PARM is a non-type generic parameter.
11686 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11687 attribute of TMPL_DIE which value represents the value
11688 of ARG.
11689 We must be careful here:
11690 The value of ARG might reference some function decls.
11691 We might currently be emitting debug info for a generic
11692 type and types are emitted before function decls, we don't
11693 know if the function decls referenced by ARG will actually be
11694 emitted after cgraph computations.
11695 So must defer the generation of the DW_AT_const_value to
11696 after cgraph is ready. */
11697 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11700 return tmpl_die;
11703 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11704 PARM_PACK must be a template parameter pack. The returned DIE
11705 will be child DIE of PARENT_DIE. */
11707 static dw_die_ref
11708 template_parameter_pack_die (tree parm_pack,
11709 tree parm_pack_args,
11710 dw_die_ref parent_die)
11712 dw_die_ref die;
11713 int j;
11715 gcc_assert (parent_die && parm_pack);
11717 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11718 add_name_and_src_coords_attributes (die, parm_pack);
11719 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11720 generic_parameter_die (parm_pack,
11721 TREE_VEC_ELT (parm_pack_args, j),
11722 false /* Don't emit DW_AT_name */,
11723 die);
11724 return die;
11727 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11728 an enumerated type. */
11730 static inline int
11731 type_is_enum (const_tree type)
11733 return TREE_CODE (type) == ENUMERAL_TYPE;
11736 /* Return the DBX register number described by a given RTL node. */
11738 static unsigned int
11739 dbx_reg_number (const_rtx rtl)
11741 unsigned regno = REGNO (rtl);
11743 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11745 #ifdef LEAF_REG_REMAP
11746 if (crtl->uses_only_leaf_regs)
11748 int leaf_reg = LEAF_REG_REMAP (regno);
11749 if (leaf_reg != -1)
11750 regno = (unsigned) leaf_reg;
11752 #endif
11754 regno = DBX_REGISTER_NUMBER (regno);
11755 gcc_assert (regno != INVALID_REGNUM);
11756 return regno;
11759 /* Optionally add a DW_OP_piece term to a location description expression.
11760 DW_OP_piece is only added if the location description expression already
11761 doesn't end with DW_OP_piece. */
11763 static void
11764 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11766 dw_loc_descr_ref loc;
11768 if (*list_head != NULL)
11770 /* Find the end of the chain. */
11771 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11774 if (loc->dw_loc_opc != DW_OP_piece)
11775 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11779 /* Return a location descriptor that designates a machine register or
11780 zero if there is none. */
11782 static dw_loc_descr_ref
11783 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11785 rtx regs;
11787 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11788 return 0;
11790 /* We only use "frame base" when we're sure we're talking about the
11791 post-prologue local stack frame. We do this by *not* running
11792 register elimination until this point, and recognizing the special
11793 argument pointer and soft frame pointer rtx's.
11794 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11795 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11796 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11798 dw_loc_descr_ref result = NULL;
11800 if (dwarf_version >= 4 || !dwarf_strict)
11802 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11803 initialized);
11804 if (result)
11805 add_loc_descr (&result,
11806 new_loc_descr (DW_OP_stack_value, 0, 0));
11808 return result;
11811 regs = targetm.dwarf_register_span (rtl);
11813 if (REG_NREGS (rtl) > 1 || regs)
11814 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11815 else
11817 unsigned int dbx_regnum = dbx_reg_number (rtl);
11818 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11819 return 0;
11820 return one_reg_loc_descriptor (dbx_regnum, initialized);
11824 /* Return a location descriptor that designates a machine register for
11825 a given hard register number. */
11827 static dw_loc_descr_ref
11828 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11830 dw_loc_descr_ref reg_loc_descr;
11832 if (regno <= 31)
11833 reg_loc_descr
11834 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11835 else
11836 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11838 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11839 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11841 return reg_loc_descr;
11844 /* Given an RTL of a register, return a location descriptor that
11845 designates a value that spans more than one register. */
11847 static dw_loc_descr_ref
11848 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11849 enum var_init_status initialized)
11851 int size, i;
11852 dw_loc_descr_ref loc_result = NULL;
11854 /* Simple, contiguous registers. */
11855 if (regs == NULL_RTX)
11857 unsigned reg = REGNO (rtl);
11858 int nregs;
11860 #ifdef LEAF_REG_REMAP
11861 if (crtl->uses_only_leaf_regs)
11863 int leaf_reg = LEAF_REG_REMAP (reg);
11864 if (leaf_reg != -1)
11865 reg = (unsigned) leaf_reg;
11867 #endif
11869 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11870 nregs = REG_NREGS (rtl);
11872 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11874 loc_result = NULL;
11875 while (nregs--)
11877 dw_loc_descr_ref t;
11879 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11880 VAR_INIT_STATUS_INITIALIZED);
11881 add_loc_descr (&loc_result, t);
11882 add_loc_descr_op_piece (&loc_result, size);
11883 ++reg;
11885 return loc_result;
11888 /* Now onto stupid register sets in non contiguous locations. */
11890 gcc_assert (GET_CODE (regs) == PARALLEL);
11892 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11893 loc_result = NULL;
11895 for (i = 0; i < XVECLEN (regs, 0); ++i)
11897 dw_loc_descr_ref t;
11899 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11900 VAR_INIT_STATUS_INITIALIZED);
11901 add_loc_descr (&loc_result, t);
11902 add_loc_descr_op_piece (&loc_result, size);
11905 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11906 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11907 return loc_result;
11910 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11912 /* Return a location descriptor that designates a constant i,
11913 as a compound operation from constant (i >> shift), constant shift
11914 and DW_OP_shl. */
11916 static dw_loc_descr_ref
11917 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11919 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11920 add_loc_descr (&ret, int_loc_descriptor (shift));
11921 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11922 return ret;
11925 /* Return a location descriptor that designates a constant. */
11927 static dw_loc_descr_ref
11928 int_loc_descriptor (HOST_WIDE_INT i)
11930 enum dwarf_location_atom op;
11932 /* Pick the smallest representation of a constant, rather than just
11933 defaulting to the LEB encoding. */
11934 if (i >= 0)
11936 int clz = clz_hwi (i);
11937 int ctz = ctz_hwi (i);
11938 if (i <= 31)
11939 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11940 else if (i <= 0xff)
11941 op = DW_OP_const1u;
11942 else if (i <= 0xffff)
11943 op = DW_OP_const2u;
11944 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11945 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11946 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11947 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11948 while DW_OP_const4u is 5 bytes. */
11949 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11950 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11951 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11952 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11953 while DW_OP_const4u is 5 bytes. */
11954 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11955 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11956 op = DW_OP_const4u;
11957 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11958 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11959 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11960 while DW_OP_constu of constant >= 0x100000000 takes at least
11961 6 bytes. */
11962 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11963 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11964 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11965 >= HOST_BITS_PER_WIDE_INT)
11966 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11967 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11968 while DW_OP_constu takes in this case at least 6 bytes. */
11969 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11970 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11971 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11972 && size_of_uleb128 (i) > 6)
11973 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11974 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11975 else
11976 op = DW_OP_constu;
11978 else
11980 if (i >= -0x80)
11981 op = DW_OP_const1s;
11982 else if (i >= -0x8000)
11983 op = DW_OP_const2s;
11984 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11986 if (size_of_int_loc_descriptor (i) < 5)
11988 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11989 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11990 return ret;
11992 op = DW_OP_const4s;
11994 else
11996 if (size_of_int_loc_descriptor (i)
11997 < (unsigned long) 1 + size_of_sleb128 (i))
11999 dw_loc_descr_ref ret = int_loc_descriptor (-i);
12000 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12001 return ret;
12003 op = DW_OP_consts;
12007 return new_loc_descr (op, i, 0);
12010 /* Likewise, for unsigned constants. */
12012 static dw_loc_descr_ref
12013 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12015 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12016 const unsigned HOST_WIDE_INT max_uint
12017 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12019 /* If possible, use the clever signed constants handling. */
12020 if (i <= max_int)
12021 return int_loc_descriptor ((HOST_WIDE_INT) i);
12023 /* Here, we are left with positive numbers that cannot be represented as
12024 HOST_WIDE_INT, i.e.:
12025 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12027 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12028 whereas may be better to output a negative integer: thanks to integer
12029 wrapping, we know that:
12030 x = x - 2 ** DWARF2_ADDR_SIZE
12031 = x - 2 * (max (HOST_WIDE_INT) + 1)
12032 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12033 small negative integers. Let's try that in cases it will clearly improve
12034 the encoding: there is no gain turning DW_OP_const4u into
12035 DW_OP_const4s. */
12036 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12037 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12038 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12040 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12042 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
12043 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
12044 const HOST_WIDE_INT second_shift
12045 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12047 /* So we finally have:
12048 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12049 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
12050 return int_loc_descriptor (second_shift);
12053 /* Last chance: fallback to a simple constant operation. */
12054 return new_loc_descr
12055 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12056 ? DW_OP_const4u
12057 : DW_OP_const8u,
12058 i, 0);
12061 /* Generate and return a location description that computes the unsigned
12062 comparison of the two stack top entries (a OP b where b is the top-most
12063 entry and a is the second one). The KIND of comparison can be LT_EXPR,
12064 LE_EXPR, GT_EXPR or GE_EXPR. */
12066 static dw_loc_descr_ref
12067 uint_comparison_loc_list (enum tree_code kind)
12069 enum dwarf_location_atom op, flip_op;
12070 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12072 switch (kind)
12074 case LT_EXPR:
12075 op = DW_OP_lt;
12076 break;
12077 case LE_EXPR:
12078 op = DW_OP_le;
12079 break;
12080 case GT_EXPR:
12081 op = DW_OP_gt;
12082 break;
12083 case GE_EXPR:
12084 op = DW_OP_ge;
12085 break;
12086 default:
12087 gcc_unreachable ();
12090 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12091 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12093 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
12094 possible to perform unsigned comparisons: we just have to distinguish
12095 three cases:
12097 1. when a and b have the same sign (as signed integers); then we should
12098 return: a OP(signed) b;
12100 2. when a is a negative signed integer while b is a positive one, then a
12101 is a greater unsigned integer than b; likewise when a and b's roles
12102 are flipped.
12104 So first, compare the sign of the two operands. */
12105 ret = new_loc_descr (DW_OP_over, 0, 0);
12106 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12107 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12108 /* If they have different signs (i.e. they have different sign bits), then
12109 the stack top value has now the sign bit set and thus it's smaller than
12110 zero. */
12111 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12112 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12113 add_loc_descr (&ret, bra_node);
12115 /* We are in case 1. At this point, we know both operands have the same
12116 sign, to it's safe to use the built-in signed comparison. */
12117 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12118 add_loc_descr (&ret, jmp_node);
12120 /* We are in case 2. Here, we know both operands do not have the same sign,
12121 so we have to flip the signed comparison. */
12122 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12123 tmp = new_loc_descr (flip_op, 0, 0);
12124 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12125 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12126 add_loc_descr (&ret, tmp);
12128 /* This dummy operation is necessary to make the two branches join. */
12129 tmp = new_loc_descr (DW_OP_nop, 0, 0);
12130 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12131 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12132 add_loc_descr (&ret, tmp);
12134 return ret;
12137 /* Likewise, but takes the location description lists (might be destructive on
12138 them). Return NULL if either is NULL or if concatenation fails. */
12140 static dw_loc_list_ref
12141 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12142 enum tree_code kind)
12144 if (left == NULL || right == NULL)
12145 return NULL;
12147 add_loc_list (&left, right);
12148 if (left == NULL)
12149 return NULL;
12151 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12152 return left;
12155 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12156 without actually allocating it. */
12158 static unsigned long
12159 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12161 return size_of_int_loc_descriptor (i >> shift)
12162 + size_of_int_loc_descriptor (shift)
12163 + 1;
12166 /* Return size_of_locs (int_loc_descriptor (i)) without
12167 actually allocating it. */
12169 static unsigned long
12170 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12172 unsigned long s;
12174 if (i >= 0)
12176 int clz, ctz;
12177 if (i <= 31)
12178 return 1;
12179 else if (i <= 0xff)
12180 return 2;
12181 else if (i <= 0xffff)
12182 return 3;
12183 clz = clz_hwi (i);
12184 ctz = ctz_hwi (i);
12185 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12186 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12187 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12188 - clz - 5);
12189 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12190 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12191 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12192 - clz - 8);
12193 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12194 return 5;
12195 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12196 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12197 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12198 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12199 - clz - 8);
12200 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12201 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12202 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12203 - clz - 16);
12204 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12205 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12206 && s > 6)
12207 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12208 - clz - 32);
12209 else
12210 return 1 + s;
12212 else
12214 if (i >= -0x80)
12215 return 2;
12216 else if (i >= -0x8000)
12217 return 3;
12218 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12220 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12222 s = size_of_int_loc_descriptor (-i) + 1;
12223 if (s < 5)
12224 return s;
12226 return 5;
12228 else
12230 unsigned long r = 1 + size_of_sleb128 (i);
12231 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12233 s = size_of_int_loc_descriptor (-i) + 1;
12234 if (s < r)
12235 return s;
12237 return r;
12242 /* Return loc description representing "address" of integer value.
12243 This can appear only as toplevel expression. */
12245 static dw_loc_descr_ref
12246 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12248 int litsize;
12249 dw_loc_descr_ref loc_result = NULL;
12251 if (!(dwarf_version >= 4 || !dwarf_strict))
12252 return NULL;
12254 litsize = size_of_int_loc_descriptor (i);
12255 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12256 is more compact. For DW_OP_stack_value we need:
12257 litsize + 1 (DW_OP_stack_value)
12258 and for DW_OP_implicit_value:
12259 1 (DW_OP_implicit_value) + 1 (length) + size. */
12260 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12262 loc_result = int_loc_descriptor (i);
12263 add_loc_descr (&loc_result,
12264 new_loc_descr (DW_OP_stack_value, 0, 0));
12265 return loc_result;
12268 loc_result = new_loc_descr (DW_OP_implicit_value,
12269 size, 0);
12270 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12271 loc_result->dw_loc_oprnd2.v.val_int = i;
12272 return loc_result;
12275 /* Return a location descriptor that designates a base+offset location. */
12277 static dw_loc_descr_ref
12278 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12279 enum var_init_status initialized)
12281 unsigned int regno;
12282 dw_loc_descr_ref result;
12283 dw_fde_ref fde = cfun->fde;
12285 /* We only use "frame base" when we're sure we're talking about the
12286 post-prologue local stack frame. We do this by *not* running
12287 register elimination until this point, and recognizing the special
12288 argument pointer and soft frame pointer rtx's. */
12289 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12291 rtx elim = (ira_use_lra_p
12292 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12293 : eliminate_regs (reg, VOIDmode, NULL_RTX));
12295 if (elim != reg)
12297 if (GET_CODE (elim) == PLUS)
12299 offset += INTVAL (XEXP (elim, 1));
12300 elim = XEXP (elim, 0);
12302 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12303 && (elim == hard_frame_pointer_rtx
12304 || elim == stack_pointer_rtx))
12305 || elim == (frame_pointer_needed
12306 ? hard_frame_pointer_rtx
12307 : stack_pointer_rtx));
12309 /* If drap register is used to align stack, use frame
12310 pointer + offset to access stack variables. If stack
12311 is aligned without drap, use stack pointer + offset to
12312 access stack variables. */
12313 if (crtl->stack_realign_tried
12314 && reg == frame_pointer_rtx)
12316 int base_reg
12317 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12318 ? HARD_FRAME_POINTER_REGNUM
12319 : REGNO (elim));
12320 return new_reg_loc_descr (base_reg, offset);
12323 gcc_assert (frame_pointer_fb_offset_valid);
12324 offset += frame_pointer_fb_offset;
12325 return new_loc_descr (DW_OP_fbreg, offset, 0);
12329 regno = REGNO (reg);
12330 #ifdef LEAF_REG_REMAP
12331 if (crtl->uses_only_leaf_regs)
12333 int leaf_reg = LEAF_REG_REMAP (regno);
12334 if (leaf_reg != -1)
12335 regno = (unsigned) leaf_reg;
12337 #endif
12338 regno = DWARF_FRAME_REGNUM (regno);
12340 if (!optimize && fde
12341 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12343 /* Use cfa+offset to represent the location of arguments passed
12344 on the stack when drap is used to align stack.
12345 Only do this when not optimizing, for optimized code var-tracking
12346 is supposed to track where the arguments live and the register
12347 used as vdrap or drap in some spot might be used for something
12348 else in other part of the routine. */
12349 return new_loc_descr (DW_OP_fbreg, offset, 0);
12352 if (regno <= 31)
12353 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12354 offset, 0);
12355 else
12356 result = new_loc_descr (DW_OP_bregx, regno, offset);
12358 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12359 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12361 return result;
12364 /* Return true if this RTL expression describes a base+offset calculation. */
12366 static inline int
12367 is_based_loc (const_rtx rtl)
12369 return (GET_CODE (rtl) == PLUS
12370 && ((REG_P (XEXP (rtl, 0))
12371 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12372 && CONST_INT_P (XEXP (rtl, 1)))));
12375 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12376 failed. */
12378 static dw_loc_descr_ref
12379 tls_mem_loc_descriptor (rtx mem)
12381 tree base;
12382 dw_loc_descr_ref loc_result;
12384 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12385 return NULL;
12387 base = get_base_address (MEM_EXPR (mem));
12388 if (base == NULL
12389 || TREE_CODE (base) != VAR_DECL
12390 || !DECL_THREAD_LOCAL_P (base))
12391 return NULL;
12393 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12394 if (loc_result == NULL)
12395 return NULL;
12397 if (MEM_OFFSET (mem))
12398 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12400 return loc_result;
12403 /* Output debug info about reason why we failed to expand expression as dwarf
12404 expression. */
12406 static void
12407 expansion_failed (tree expr, rtx rtl, char const *reason)
12409 if (dump_file && (dump_flags & TDF_DETAILS))
12411 fprintf (dump_file, "Failed to expand as dwarf: ");
12412 if (expr)
12413 print_generic_expr (dump_file, expr, dump_flags);
12414 if (rtl)
12416 fprintf (dump_file, "\n");
12417 print_rtl (dump_file, rtl);
12419 fprintf (dump_file, "\nReason: %s\n", reason);
12423 /* Helper function for const_ok_for_output. */
12425 static bool
12426 const_ok_for_output_1 (rtx rtl)
12428 if (GET_CODE (rtl) == UNSPEC)
12430 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12431 we can't express it in the debug info. */
12432 /* Don't complain about TLS UNSPECs, those are just too hard to
12433 delegitimize. Note this could be a non-decl SYMBOL_REF such as
12434 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12435 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
12436 if (flag_checking
12437 && (XVECLEN (rtl, 0) == 0
12438 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12439 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12440 inform (current_function_decl
12441 ? DECL_SOURCE_LOCATION (current_function_decl)
12442 : UNKNOWN_LOCATION,
12443 #if NUM_UNSPEC_VALUES > 0
12444 "non-delegitimized UNSPEC %s (%d) found in variable location",
12445 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12446 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12447 XINT (rtl, 1));
12448 #else
12449 "non-delegitimized UNSPEC %d found in variable location",
12450 XINT (rtl, 1));
12451 #endif
12452 expansion_failed (NULL_TREE, rtl,
12453 "UNSPEC hasn't been delegitimized.\n");
12454 return false;
12457 if (targetm.const_not_ok_for_debug_p (rtl))
12459 expansion_failed (NULL_TREE, rtl,
12460 "Expression rejected for debug by the backend.\n");
12461 return false;
12464 /* FIXME: Refer to PR60655. It is possible for simplification
12465 of rtl expressions in var tracking to produce such expressions.
12466 We should really identify / validate expressions
12467 enclosed in CONST that can be handled by assemblers on various
12468 targets and only handle legitimate cases here. */
12469 if (GET_CODE (rtl) != SYMBOL_REF)
12471 if (GET_CODE (rtl) == NOT)
12472 return false;
12473 return true;
12476 if (CONSTANT_POOL_ADDRESS_P (rtl))
12478 bool marked;
12479 get_pool_constant_mark (rtl, &marked);
12480 /* If all references to this pool constant were optimized away,
12481 it was not output and thus we can't represent it. */
12482 if (!marked)
12484 expansion_failed (NULL_TREE, rtl,
12485 "Constant was removed from constant pool.\n");
12486 return false;
12490 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12491 return false;
12493 /* Avoid references to external symbols in debug info, on several targets
12494 the linker might even refuse to link when linking a shared library,
12495 and in many other cases the relocations for .debug_info/.debug_loc are
12496 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
12497 to be defined within the same shared library or executable are fine. */
12498 if (SYMBOL_REF_EXTERNAL_P (rtl))
12500 tree decl = SYMBOL_REF_DECL (rtl);
12502 if (decl == NULL || !targetm.binds_local_p (decl))
12504 expansion_failed (NULL_TREE, rtl,
12505 "Symbol not defined in current TU.\n");
12506 return false;
12510 return true;
12513 /* Return true if constant RTL can be emitted in DW_OP_addr or
12514 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
12515 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
12517 static bool
12518 const_ok_for_output (rtx rtl)
12520 if (GET_CODE (rtl) == SYMBOL_REF)
12521 return const_ok_for_output_1 (rtl);
12523 if (GET_CODE (rtl) == CONST)
12525 subrtx_var_iterator::array_type array;
12526 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12527 if (!const_ok_for_output_1 (*iter))
12528 return false;
12529 return true;
12532 return true;
12535 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12536 if possible, NULL otherwise. */
12538 static dw_die_ref
12539 base_type_for_mode (machine_mode mode, bool unsignedp)
12541 dw_die_ref type_die;
12542 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12544 if (type == NULL)
12545 return NULL;
12546 switch (TREE_CODE (type))
12548 case INTEGER_TYPE:
12549 case REAL_TYPE:
12550 break;
12551 default:
12552 return NULL;
12554 type_die = lookup_type_die (type);
12555 if (!type_die)
12556 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12557 comp_unit_die ());
12558 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12559 return NULL;
12560 return type_die;
12563 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12564 type matching MODE, or, if MODE is narrower than or as wide as
12565 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
12566 possible. */
12568 static dw_loc_descr_ref
12569 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12571 machine_mode outer_mode = mode;
12572 dw_die_ref type_die;
12573 dw_loc_descr_ref cvt;
12575 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12577 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12578 return op;
12580 type_die = base_type_for_mode (outer_mode, 1);
12581 if (type_die == NULL)
12582 return NULL;
12583 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12584 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12585 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12586 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12587 add_loc_descr (&op, cvt);
12588 return op;
12591 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
12593 static dw_loc_descr_ref
12594 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12595 dw_loc_descr_ref op1)
12597 dw_loc_descr_ref ret = op0;
12598 add_loc_descr (&ret, op1);
12599 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12600 if (STORE_FLAG_VALUE != 1)
12602 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12603 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12605 return ret;
12608 /* Return location descriptor for signed comparison OP RTL. */
12610 static dw_loc_descr_ref
12611 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12612 machine_mode mem_mode)
12614 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12615 dw_loc_descr_ref op0, op1;
12616 int shift;
12618 if (op_mode == VOIDmode)
12619 op_mode = GET_MODE (XEXP (rtl, 1));
12620 if (op_mode == VOIDmode)
12621 return NULL;
12623 if (dwarf_strict
12624 && (!SCALAR_INT_MODE_P (op_mode)
12625 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12626 return NULL;
12628 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12629 VAR_INIT_STATUS_INITIALIZED);
12630 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12631 VAR_INIT_STATUS_INITIALIZED);
12633 if (op0 == NULL || op1 == NULL)
12634 return NULL;
12636 if (!SCALAR_INT_MODE_P (op_mode)
12637 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12638 return compare_loc_descriptor (op, op0, op1);
12640 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12642 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12643 dw_loc_descr_ref cvt;
12645 if (type_die == NULL)
12646 return NULL;
12647 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12648 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12649 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12650 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12651 add_loc_descr (&op0, cvt);
12652 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12653 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12654 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12655 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12656 add_loc_descr (&op1, cvt);
12657 return compare_loc_descriptor (op, op0, op1);
12660 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12661 /* For eq/ne, if the operands are known to be zero-extended,
12662 there is no need to do the fancy shifting up. */
12663 if (op == DW_OP_eq || op == DW_OP_ne)
12665 dw_loc_descr_ref last0, last1;
12666 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12668 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12670 /* deref_size zero extends, and for constants we can check
12671 whether they are zero extended or not. */
12672 if (((last0->dw_loc_opc == DW_OP_deref_size
12673 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12674 || (CONST_INT_P (XEXP (rtl, 0))
12675 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12676 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12677 && ((last1->dw_loc_opc == DW_OP_deref_size
12678 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12679 || (CONST_INT_P (XEXP (rtl, 1))
12680 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12681 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12682 return compare_loc_descriptor (op, op0, op1);
12684 /* EQ/NE comparison against constant in narrower type than
12685 DWARF2_ADDR_SIZE can be performed either as
12686 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12687 DW_OP_{eq,ne}
12689 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12690 DW_OP_{eq,ne}. Pick whatever is shorter. */
12691 if (CONST_INT_P (XEXP (rtl, 1))
12692 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12693 && (size_of_int_loc_descriptor (shift) + 1
12694 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12695 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12696 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12697 & GET_MODE_MASK (op_mode))))
12699 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12700 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12701 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12702 & GET_MODE_MASK (op_mode));
12703 return compare_loc_descriptor (op, op0, op1);
12706 add_loc_descr (&op0, int_loc_descriptor (shift));
12707 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12708 if (CONST_INT_P (XEXP (rtl, 1)))
12709 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12710 else
12712 add_loc_descr (&op1, int_loc_descriptor (shift));
12713 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12715 return compare_loc_descriptor (op, op0, op1);
12718 /* Return location descriptor for unsigned comparison OP RTL. */
12720 static dw_loc_descr_ref
12721 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12722 machine_mode mem_mode)
12724 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12725 dw_loc_descr_ref op0, op1;
12727 if (op_mode == VOIDmode)
12728 op_mode = GET_MODE (XEXP (rtl, 1));
12729 if (op_mode == VOIDmode)
12730 return NULL;
12731 if (!SCALAR_INT_MODE_P (op_mode))
12732 return NULL;
12734 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12735 return NULL;
12737 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12738 VAR_INIT_STATUS_INITIALIZED);
12739 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12740 VAR_INIT_STATUS_INITIALIZED);
12742 if (op0 == NULL || op1 == NULL)
12743 return NULL;
12745 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12747 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12748 dw_loc_descr_ref last0, last1;
12749 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12751 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12753 if (CONST_INT_P (XEXP (rtl, 0)))
12754 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12755 /* deref_size zero extends, so no need to mask it again. */
12756 else if (last0->dw_loc_opc != DW_OP_deref_size
12757 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12759 add_loc_descr (&op0, int_loc_descriptor (mask));
12760 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12762 if (CONST_INT_P (XEXP (rtl, 1)))
12763 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12764 /* deref_size zero extends, so no need to mask it again. */
12765 else if (last1->dw_loc_opc != DW_OP_deref_size
12766 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12768 add_loc_descr (&op1, int_loc_descriptor (mask));
12769 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12772 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12774 HOST_WIDE_INT bias = 1;
12775 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12776 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12777 if (CONST_INT_P (XEXP (rtl, 1)))
12778 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12779 + INTVAL (XEXP (rtl, 1)));
12780 else
12781 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12782 bias, 0));
12784 return compare_loc_descriptor (op, op0, op1);
12787 /* Return location descriptor for {U,S}{MIN,MAX}. */
12789 static dw_loc_descr_ref
12790 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12791 machine_mode mem_mode)
12793 enum dwarf_location_atom op;
12794 dw_loc_descr_ref op0, op1, ret;
12795 dw_loc_descr_ref bra_node, drop_node;
12797 if (dwarf_strict
12798 && (!SCALAR_INT_MODE_P (mode)
12799 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12800 return NULL;
12802 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12803 VAR_INIT_STATUS_INITIALIZED);
12804 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12805 VAR_INIT_STATUS_INITIALIZED);
12807 if (op0 == NULL || op1 == NULL)
12808 return NULL;
12810 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12811 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12812 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12813 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12815 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12817 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12818 add_loc_descr (&op0, int_loc_descriptor (mask));
12819 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12820 add_loc_descr (&op1, int_loc_descriptor (mask));
12821 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12823 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12825 HOST_WIDE_INT bias = 1;
12826 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12827 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12828 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12831 else if (!SCALAR_INT_MODE_P (mode)
12832 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12834 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12835 add_loc_descr (&op0, int_loc_descriptor (shift));
12836 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12837 add_loc_descr (&op1, int_loc_descriptor (shift));
12838 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12840 else if (SCALAR_INT_MODE_P (mode)
12841 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12843 dw_die_ref type_die = base_type_for_mode (mode, 0);
12844 dw_loc_descr_ref cvt;
12845 if (type_die == NULL)
12846 return NULL;
12847 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12848 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12849 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12850 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12851 add_loc_descr (&op0, cvt);
12852 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12853 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12854 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12855 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12856 add_loc_descr (&op1, cvt);
12859 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12860 op = DW_OP_lt;
12861 else
12862 op = DW_OP_gt;
12863 ret = op0;
12864 add_loc_descr (&ret, op1);
12865 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12866 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12867 add_loc_descr (&ret, bra_node);
12868 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12869 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12870 add_loc_descr (&ret, drop_node);
12871 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12872 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12873 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12874 && SCALAR_INT_MODE_P (mode)
12875 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12876 ret = convert_descriptor_to_mode (mode, ret);
12877 return ret;
12880 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12881 but after converting arguments to type_die, afterwards
12882 convert back to unsigned. */
12884 static dw_loc_descr_ref
12885 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12886 machine_mode mode, machine_mode mem_mode)
12888 dw_loc_descr_ref cvt, op0, op1;
12890 if (type_die == NULL)
12891 return NULL;
12892 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12893 VAR_INIT_STATUS_INITIALIZED);
12894 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12895 VAR_INIT_STATUS_INITIALIZED);
12896 if (op0 == NULL || op1 == NULL)
12897 return NULL;
12898 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12899 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12900 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12901 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12902 add_loc_descr (&op0, cvt);
12903 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12904 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12905 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12906 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12907 add_loc_descr (&op1, cvt);
12908 add_loc_descr (&op0, op1);
12909 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12910 return convert_descriptor_to_mode (mode, op0);
12913 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12914 const0 is DW_OP_lit0 or corresponding typed constant,
12915 const1 is DW_OP_lit1 or corresponding typed constant
12916 and constMSB is constant with just the MSB bit set
12917 for the mode):
12918 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12919 L1: const0 DW_OP_swap
12920 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12921 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12922 L3: DW_OP_drop
12923 L4: DW_OP_nop
12925 CTZ is similar:
12926 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12927 L1: const0 DW_OP_swap
12928 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12929 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12930 L3: DW_OP_drop
12931 L4: DW_OP_nop
12933 FFS is similar:
12934 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12935 L1: const1 DW_OP_swap
12936 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12937 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12938 L3: DW_OP_drop
12939 L4: DW_OP_nop */
12941 static dw_loc_descr_ref
12942 clz_loc_descriptor (rtx rtl, machine_mode mode,
12943 machine_mode mem_mode)
12945 dw_loc_descr_ref op0, ret, tmp;
12946 HOST_WIDE_INT valv;
12947 dw_loc_descr_ref l1jump, l1label;
12948 dw_loc_descr_ref l2jump, l2label;
12949 dw_loc_descr_ref l3jump, l3label;
12950 dw_loc_descr_ref l4jump, l4label;
12951 rtx msb;
12953 if (!SCALAR_INT_MODE_P (mode)
12954 || GET_MODE (XEXP (rtl, 0)) != mode)
12955 return NULL;
12957 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12958 VAR_INIT_STATUS_INITIALIZED);
12959 if (op0 == NULL)
12960 return NULL;
12961 ret = op0;
12962 if (GET_CODE (rtl) == CLZ)
12964 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12965 valv = GET_MODE_BITSIZE (mode);
12967 else if (GET_CODE (rtl) == FFS)
12968 valv = 0;
12969 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12970 valv = GET_MODE_BITSIZE (mode);
12971 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12972 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12973 add_loc_descr (&ret, l1jump);
12974 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12975 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12976 VAR_INIT_STATUS_INITIALIZED);
12977 if (tmp == NULL)
12978 return NULL;
12979 add_loc_descr (&ret, tmp);
12980 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12981 add_loc_descr (&ret, l4jump);
12982 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12983 ? const1_rtx : const0_rtx,
12984 mode, mem_mode,
12985 VAR_INIT_STATUS_INITIALIZED);
12986 if (l1label == NULL)
12987 return NULL;
12988 add_loc_descr (&ret, l1label);
12989 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12990 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12991 add_loc_descr (&ret, l2label);
12992 if (GET_CODE (rtl) != CLZ)
12993 msb = const1_rtx;
12994 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12995 msb = GEN_INT (HOST_WIDE_INT_1U
12996 << (GET_MODE_BITSIZE (mode) - 1));
12997 else
12998 msb = immed_wide_int_const
12999 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
13000 GET_MODE_PRECISION (mode)), mode);
13001 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
13002 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13003 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
13004 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
13005 else
13006 tmp = mem_loc_descriptor (msb, mode, mem_mode,
13007 VAR_INIT_STATUS_INITIALIZED);
13008 if (tmp == NULL)
13009 return NULL;
13010 add_loc_descr (&ret, tmp);
13011 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13012 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13013 add_loc_descr (&ret, l3jump);
13014 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13015 VAR_INIT_STATUS_INITIALIZED);
13016 if (tmp == NULL)
13017 return NULL;
13018 add_loc_descr (&ret, tmp);
13019 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13020 ? DW_OP_shl : DW_OP_shr, 0, 0));
13021 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13022 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13023 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13024 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13025 add_loc_descr (&ret, l2jump);
13026 l3label = new_loc_descr (DW_OP_drop, 0, 0);
13027 add_loc_descr (&ret, l3label);
13028 l4label = new_loc_descr (DW_OP_nop, 0, 0);
13029 add_loc_descr (&ret, l4label);
13030 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13031 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13032 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13033 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13034 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13035 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13036 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13037 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13038 return ret;
13041 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13042 const1 is DW_OP_lit1 or corresponding typed constant):
13043 const0 DW_OP_swap
13044 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13045 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13046 L2: DW_OP_drop
13048 PARITY is similar:
13049 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13050 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13051 L2: DW_OP_drop */
13053 static dw_loc_descr_ref
13054 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13055 machine_mode mem_mode)
13057 dw_loc_descr_ref op0, ret, tmp;
13058 dw_loc_descr_ref l1jump, l1label;
13059 dw_loc_descr_ref l2jump, l2label;
13061 if (!SCALAR_INT_MODE_P (mode)
13062 || GET_MODE (XEXP (rtl, 0)) != mode)
13063 return NULL;
13065 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13066 VAR_INIT_STATUS_INITIALIZED);
13067 if (op0 == NULL)
13068 return NULL;
13069 ret = op0;
13070 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13071 VAR_INIT_STATUS_INITIALIZED);
13072 if (tmp == NULL)
13073 return NULL;
13074 add_loc_descr (&ret, tmp);
13075 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13076 l1label = new_loc_descr (DW_OP_dup, 0, 0);
13077 add_loc_descr (&ret, l1label);
13078 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13079 add_loc_descr (&ret, l2jump);
13080 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13081 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13082 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13083 VAR_INIT_STATUS_INITIALIZED);
13084 if (tmp == NULL)
13085 return NULL;
13086 add_loc_descr (&ret, tmp);
13087 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13088 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13089 ? DW_OP_plus : DW_OP_xor, 0, 0));
13090 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13091 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13092 VAR_INIT_STATUS_INITIALIZED);
13093 add_loc_descr (&ret, tmp);
13094 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13095 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13096 add_loc_descr (&ret, l1jump);
13097 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13098 add_loc_descr (&ret, l2label);
13099 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13100 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13101 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13102 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13103 return ret;
13106 /* BSWAP (constS is initial shift count, either 56 or 24):
13107 constS const0
13108 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13109 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13110 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13111 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13112 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
13114 static dw_loc_descr_ref
13115 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13116 machine_mode mem_mode)
13118 dw_loc_descr_ref op0, ret, tmp;
13119 dw_loc_descr_ref l1jump, l1label;
13120 dw_loc_descr_ref l2jump, l2label;
13122 if (!SCALAR_INT_MODE_P (mode)
13123 || BITS_PER_UNIT != 8
13124 || (GET_MODE_BITSIZE (mode) != 32
13125 && GET_MODE_BITSIZE (mode) != 64))
13126 return NULL;
13128 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13129 VAR_INIT_STATUS_INITIALIZED);
13130 if (op0 == NULL)
13131 return NULL;
13133 ret = op0;
13134 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13135 mode, mem_mode,
13136 VAR_INIT_STATUS_INITIALIZED);
13137 if (tmp == NULL)
13138 return NULL;
13139 add_loc_descr (&ret, tmp);
13140 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13141 VAR_INIT_STATUS_INITIALIZED);
13142 if (tmp == NULL)
13143 return NULL;
13144 add_loc_descr (&ret, tmp);
13145 l1label = new_loc_descr (DW_OP_pick, 2, 0);
13146 add_loc_descr (&ret, l1label);
13147 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13148 mode, mem_mode,
13149 VAR_INIT_STATUS_INITIALIZED);
13150 add_loc_descr (&ret, tmp);
13151 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13152 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13153 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13154 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13155 VAR_INIT_STATUS_INITIALIZED);
13156 if (tmp == NULL)
13157 return NULL;
13158 add_loc_descr (&ret, tmp);
13159 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13160 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13161 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13162 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13163 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13164 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13165 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13166 VAR_INIT_STATUS_INITIALIZED);
13167 add_loc_descr (&ret, tmp);
13168 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13169 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13170 add_loc_descr (&ret, l2jump);
13171 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13172 VAR_INIT_STATUS_INITIALIZED);
13173 add_loc_descr (&ret, tmp);
13174 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13175 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13176 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13177 add_loc_descr (&ret, l1jump);
13178 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13179 add_loc_descr (&ret, l2label);
13180 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13181 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13182 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13183 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13184 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13185 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13186 return ret;
13189 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13190 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13191 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13192 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13194 ROTATERT is similar:
13195 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13196 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13197 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
13199 static dw_loc_descr_ref
13200 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13201 machine_mode mem_mode)
13203 rtx rtlop1 = XEXP (rtl, 1);
13204 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13205 int i;
13207 if (!SCALAR_INT_MODE_P (mode))
13208 return NULL;
13210 if (GET_MODE (rtlop1) != VOIDmode
13211 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13212 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13213 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13214 VAR_INIT_STATUS_INITIALIZED);
13215 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13216 VAR_INIT_STATUS_INITIALIZED);
13217 if (op0 == NULL || op1 == NULL)
13218 return NULL;
13219 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13220 for (i = 0; i < 2; i++)
13222 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13223 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13224 mode, mem_mode,
13225 VAR_INIT_STATUS_INITIALIZED);
13226 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13227 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13228 ? DW_OP_const4u
13229 : HOST_BITS_PER_WIDE_INT == 64
13230 ? DW_OP_const8u : DW_OP_constu,
13231 GET_MODE_MASK (mode), 0);
13232 else
13233 mask[i] = NULL;
13234 if (mask[i] == NULL)
13235 return NULL;
13236 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13238 ret = op0;
13239 add_loc_descr (&ret, op1);
13240 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13241 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13242 if (GET_CODE (rtl) == ROTATERT)
13244 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13245 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13246 GET_MODE_BITSIZE (mode), 0));
13248 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13249 if (mask[0] != NULL)
13250 add_loc_descr (&ret, mask[0]);
13251 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13252 if (mask[1] != NULL)
13254 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13255 add_loc_descr (&ret, mask[1]);
13256 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13258 if (GET_CODE (rtl) == ROTATE)
13260 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13261 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13262 GET_MODE_BITSIZE (mode), 0));
13264 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13265 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13266 return ret;
13269 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
13270 for DEBUG_PARAMETER_REF RTL. */
13272 static dw_loc_descr_ref
13273 parameter_ref_descriptor (rtx rtl)
13275 dw_loc_descr_ref ret;
13276 dw_die_ref ref;
13278 if (dwarf_strict)
13279 return NULL;
13280 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13281 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13282 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13283 if (ref)
13285 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13286 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13287 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13289 else
13291 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13292 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13294 return ret;
13297 /* The following routine converts the RTL for a variable or parameter
13298 (resident in memory) into an equivalent Dwarf representation of a
13299 mechanism for getting the address of that same variable onto the top of a
13300 hypothetical "address evaluation" stack.
13302 When creating memory location descriptors, we are effectively transforming
13303 the RTL for a memory-resident object into its Dwarf postfix expression
13304 equivalent. This routine recursively descends an RTL tree, turning
13305 it into Dwarf postfix code as it goes.
13307 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13309 MEM_MODE is the mode of the memory reference, needed to handle some
13310 autoincrement addressing modes.
13312 Return 0 if we can't represent the location. */
13314 dw_loc_descr_ref
13315 mem_loc_descriptor (rtx rtl, machine_mode mode,
13316 machine_mode mem_mode,
13317 enum var_init_status initialized)
13319 dw_loc_descr_ref mem_loc_result = NULL;
13320 enum dwarf_location_atom op;
13321 dw_loc_descr_ref op0, op1;
13322 rtx inner = NULL_RTX;
13324 if (mode == VOIDmode)
13325 mode = GET_MODE (rtl);
13327 /* Note that for a dynamically sized array, the location we will generate a
13328 description of here will be the lowest numbered location which is
13329 actually within the array. That's *not* necessarily the same as the
13330 zeroth element of the array. */
13332 rtl = targetm.delegitimize_address (rtl);
13334 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13335 return NULL;
13337 switch (GET_CODE (rtl))
13339 case POST_INC:
13340 case POST_DEC:
13341 case POST_MODIFY:
13342 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13344 case SUBREG:
13345 /* The case of a subreg may arise when we have a local (register)
13346 variable or a formal (register) parameter which doesn't quite fill
13347 up an entire register. For now, just assume that it is
13348 legitimate to make the Dwarf info refer to the whole register which
13349 contains the given subreg. */
13350 if (!subreg_lowpart_p (rtl))
13351 break;
13352 inner = SUBREG_REG (rtl);
13353 /* FALLTHRU */
13354 case TRUNCATE:
13355 if (inner == NULL_RTX)
13356 inner = XEXP (rtl, 0);
13357 if (SCALAR_INT_MODE_P (mode)
13358 && SCALAR_INT_MODE_P (GET_MODE (inner))
13359 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13360 #ifdef POINTERS_EXTEND_UNSIGNED
13361 || (mode == Pmode && mem_mode != VOIDmode)
13362 #endif
13364 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13366 mem_loc_result = mem_loc_descriptor (inner,
13367 GET_MODE (inner),
13368 mem_mode, initialized);
13369 break;
13371 if (dwarf_strict)
13372 break;
13373 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13374 break;
13375 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13376 && (!SCALAR_INT_MODE_P (mode)
13377 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
13378 break;
13379 else
13381 dw_die_ref type_die;
13382 dw_loc_descr_ref cvt;
13384 mem_loc_result = mem_loc_descriptor (inner,
13385 GET_MODE (inner),
13386 mem_mode, initialized);
13387 if (mem_loc_result == NULL)
13388 break;
13389 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13390 if (type_die == NULL)
13392 mem_loc_result = NULL;
13393 break;
13395 if (GET_MODE_SIZE (mode)
13396 != GET_MODE_SIZE (GET_MODE (inner)))
13397 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13398 else
13399 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13400 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13401 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13402 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13403 add_loc_descr (&mem_loc_result, cvt);
13404 if (SCALAR_INT_MODE_P (mode)
13405 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13407 /* Convert it to untyped afterwards. */
13408 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13409 add_loc_descr (&mem_loc_result, cvt);
13412 break;
13414 case REG:
13415 if (! SCALAR_INT_MODE_P (mode)
13416 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13417 && rtl != arg_pointer_rtx
13418 && rtl != frame_pointer_rtx
13419 #ifdef POINTERS_EXTEND_UNSIGNED
13420 && (mode != Pmode || mem_mode == VOIDmode)
13421 #endif
13424 dw_die_ref type_die;
13425 unsigned int dbx_regnum;
13427 if (dwarf_strict)
13428 break;
13429 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13430 break;
13431 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13432 if (type_die == NULL)
13433 break;
13435 dbx_regnum = dbx_reg_number (rtl);
13436 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13437 break;
13438 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13439 dbx_regnum, 0);
13440 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13441 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13442 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13443 break;
13445 /* Whenever a register number forms a part of the description of the
13446 method for calculating the (dynamic) address of a memory resident
13447 object, DWARF rules require the register number be referred to as
13448 a "base register". This distinction is not based in any way upon
13449 what category of register the hardware believes the given register
13450 belongs to. This is strictly DWARF terminology we're dealing with
13451 here. Note that in cases where the location of a memory-resident
13452 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13453 OP_CONST (0)) the actual DWARF location descriptor that we generate
13454 may just be OP_BASEREG (basereg). This may look deceptively like
13455 the object in question was allocated to a register (rather than in
13456 memory) so DWARF consumers need to be aware of the subtle
13457 distinction between OP_REG and OP_BASEREG. */
13458 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13459 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13460 else if (stack_realign_drap
13461 && crtl->drap_reg
13462 && crtl->args.internal_arg_pointer == rtl
13463 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13465 /* If RTL is internal_arg_pointer, which has been optimized
13466 out, use DRAP instead. */
13467 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13468 VAR_INIT_STATUS_INITIALIZED);
13470 break;
13472 case SIGN_EXTEND:
13473 case ZERO_EXTEND:
13474 if (!SCALAR_INT_MODE_P (mode))
13475 break;
13476 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13477 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13478 if (op0 == 0)
13479 break;
13480 else if (GET_CODE (rtl) == ZERO_EXTEND
13481 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13482 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13483 < HOST_BITS_PER_WIDE_INT
13484 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13485 to expand zero extend as two shifts instead of
13486 masking. */
13487 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13489 machine_mode imode = GET_MODE (XEXP (rtl, 0));
13490 mem_loc_result = op0;
13491 add_loc_descr (&mem_loc_result,
13492 int_loc_descriptor (GET_MODE_MASK (imode)));
13493 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13495 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13497 int shift = DWARF2_ADDR_SIZE
13498 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13499 shift *= BITS_PER_UNIT;
13500 if (GET_CODE (rtl) == SIGN_EXTEND)
13501 op = DW_OP_shra;
13502 else
13503 op = DW_OP_shr;
13504 mem_loc_result = op0;
13505 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13506 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13507 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13508 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13510 else if (!dwarf_strict)
13512 dw_die_ref type_die1, type_die2;
13513 dw_loc_descr_ref cvt;
13515 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13516 GET_CODE (rtl) == ZERO_EXTEND);
13517 if (type_die1 == NULL)
13518 break;
13519 type_die2 = base_type_for_mode (mode, 1);
13520 if (type_die2 == NULL)
13521 break;
13522 mem_loc_result = op0;
13523 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13524 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13525 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13526 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13527 add_loc_descr (&mem_loc_result, cvt);
13528 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13529 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13530 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13531 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13532 add_loc_descr (&mem_loc_result, cvt);
13534 break;
13536 case MEM:
13538 rtx new_rtl = avoid_constant_pool_reference (rtl);
13539 if (new_rtl != rtl)
13541 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13542 initialized);
13543 if (mem_loc_result != NULL)
13544 return mem_loc_result;
13547 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13548 get_address_mode (rtl), mode,
13549 VAR_INIT_STATUS_INITIALIZED);
13550 if (mem_loc_result == NULL)
13551 mem_loc_result = tls_mem_loc_descriptor (rtl);
13552 if (mem_loc_result != NULL)
13554 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13555 || !SCALAR_INT_MODE_P(mode))
13557 dw_die_ref type_die;
13558 dw_loc_descr_ref deref;
13560 if (dwarf_strict)
13561 return NULL;
13562 type_die
13563 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13564 if (type_die == NULL)
13565 return NULL;
13566 deref = new_loc_descr (DW_OP_GNU_deref_type,
13567 GET_MODE_SIZE (mode), 0);
13568 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13569 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13570 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13571 add_loc_descr (&mem_loc_result, deref);
13573 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13574 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13575 else
13576 add_loc_descr (&mem_loc_result,
13577 new_loc_descr (DW_OP_deref_size,
13578 GET_MODE_SIZE (mode), 0));
13580 break;
13582 case LO_SUM:
13583 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13585 case LABEL_REF:
13586 /* Some ports can transform a symbol ref into a label ref, because
13587 the symbol ref is too far away and has to be dumped into a constant
13588 pool. */
13589 case CONST:
13590 case SYMBOL_REF:
13591 if (!SCALAR_INT_MODE_P (mode)
13592 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13593 #ifdef POINTERS_EXTEND_UNSIGNED
13594 && (mode != Pmode || mem_mode == VOIDmode)
13595 #endif
13597 break;
13598 if (GET_CODE (rtl) == SYMBOL_REF
13599 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13601 dw_loc_descr_ref temp;
13603 /* If this is not defined, we have no way to emit the data. */
13604 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13605 break;
13607 temp = new_addr_loc_descr (rtl, dtprel_true);
13609 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13610 add_loc_descr (&mem_loc_result, temp);
13612 break;
13615 if (!const_ok_for_output (rtl))
13617 if (GET_CODE (rtl) == CONST)
13618 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13619 initialized);
13620 break;
13623 symref:
13624 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13625 vec_safe_push (used_rtx_array, rtl);
13626 break;
13628 case CONCAT:
13629 case CONCATN:
13630 case VAR_LOCATION:
13631 case DEBUG_IMPLICIT_PTR:
13632 expansion_failed (NULL_TREE, rtl,
13633 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13634 return 0;
13636 case ENTRY_VALUE:
13637 if (dwarf_strict)
13638 return NULL;
13639 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13641 if (!SCALAR_INT_MODE_P (mode)
13642 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13643 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13644 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13645 else
13647 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13648 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13649 return NULL;
13650 op0 = one_reg_loc_descriptor (dbx_regnum,
13651 VAR_INIT_STATUS_INITIALIZED);
13654 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13655 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13657 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13658 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13659 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13660 return NULL;
13662 else
13663 gcc_unreachable ();
13664 if (op0 == NULL)
13665 return NULL;
13666 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13667 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13668 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13669 break;
13671 case DEBUG_PARAMETER_REF:
13672 mem_loc_result = parameter_ref_descriptor (rtl);
13673 break;
13675 case PRE_MODIFY:
13676 /* Extract the PLUS expression nested inside and fall into
13677 PLUS code below. */
13678 rtl = XEXP (rtl, 1);
13679 goto plus;
13681 case PRE_INC:
13682 case PRE_DEC:
13683 /* Turn these into a PLUS expression and fall into the PLUS code
13684 below. */
13685 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13686 gen_int_mode (GET_CODE (rtl) == PRE_INC
13687 ? GET_MODE_UNIT_SIZE (mem_mode)
13688 : -GET_MODE_UNIT_SIZE (mem_mode),
13689 mode));
13691 /* fall through */
13693 case PLUS:
13694 plus:
13695 if (is_based_loc (rtl)
13696 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13697 || XEXP (rtl, 0) == arg_pointer_rtx
13698 || XEXP (rtl, 0) == frame_pointer_rtx)
13699 && SCALAR_INT_MODE_P (mode))
13700 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13701 INTVAL (XEXP (rtl, 1)),
13702 VAR_INIT_STATUS_INITIALIZED);
13703 else
13705 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13706 VAR_INIT_STATUS_INITIALIZED);
13707 if (mem_loc_result == 0)
13708 break;
13710 if (CONST_INT_P (XEXP (rtl, 1))
13711 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13712 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13713 else
13715 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13716 VAR_INIT_STATUS_INITIALIZED);
13717 if (op1 == 0)
13718 return NULL;
13719 add_loc_descr (&mem_loc_result, op1);
13720 add_loc_descr (&mem_loc_result,
13721 new_loc_descr (DW_OP_plus, 0, 0));
13724 break;
13726 /* If a pseudo-reg is optimized away, it is possible for it to
13727 be replaced with a MEM containing a multiply or shift. */
13728 case MINUS:
13729 op = DW_OP_minus;
13730 goto do_binop;
13732 case MULT:
13733 op = DW_OP_mul;
13734 goto do_binop;
13736 case DIV:
13737 if (!dwarf_strict
13738 && SCALAR_INT_MODE_P (mode)
13739 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13741 mem_loc_result = typed_binop (DW_OP_div, rtl,
13742 base_type_for_mode (mode, 0),
13743 mode, mem_mode);
13744 break;
13746 op = DW_OP_div;
13747 goto do_binop;
13749 case UMOD:
13750 op = DW_OP_mod;
13751 goto do_binop;
13753 case ASHIFT:
13754 op = DW_OP_shl;
13755 goto do_shift;
13757 case ASHIFTRT:
13758 op = DW_OP_shra;
13759 goto do_shift;
13761 case LSHIFTRT:
13762 op = DW_OP_shr;
13763 goto do_shift;
13765 do_shift:
13766 if (!SCALAR_INT_MODE_P (mode))
13767 break;
13768 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13769 VAR_INIT_STATUS_INITIALIZED);
13771 rtx rtlop1 = XEXP (rtl, 1);
13772 if (GET_MODE (rtlop1) != VOIDmode
13773 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13774 < GET_MODE_BITSIZE (mode))
13775 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13776 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13777 VAR_INIT_STATUS_INITIALIZED);
13780 if (op0 == 0 || op1 == 0)
13781 break;
13783 mem_loc_result = op0;
13784 add_loc_descr (&mem_loc_result, op1);
13785 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13786 break;
13788 case AND:
13789 op = DW_OP_and;
13790 goto do_binop;
13792 case IOR:
13793 op = DW_OP_or;
13794 goto do_binop;
13796 case XOR:
13797 op = DW_OP_xor;
13798 goto do_binop;
13800 do_binop:
13801 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13802 VAR_INIT_STATUS_INITIALIZED);
13803 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13804 VAR_INIT_STATUS_INITIALIZED);
13806 if (op0 == 0 || op1 == 0)
13807 break;
13809 mem_loc_result = op0;
13810 add_loc_descr (&mem_loc_result, op1);
13811 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13812 break;
13814 case MOD:
13815 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13817 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13818 base_type_for_mode (mode, 0),
13819 mode, mem_mode);
13820 break;
13823 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13824 VAR_INIT_STATUS_INITIALIZED);
13825 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13826 VAR_INIT_STATUS_INITIALIZED);
13828 if (op0 == 0 || op1 == 0)
13829 break;
13831 mem_loc_result = op0;
13832 add_loc_descr (&mem_loc_result, op1);
13833 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13834 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13835 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13836 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13837 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13838 break;
13840 case UDIV:
13841 if (!dwarf_strict && SCALAR_INT_MODE_P (mode))
13843 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13845 op = DW_OP_div;
13846 goto do_binop;
13848 mem_loc_result = typed_binop (DW_OP_div, rtl,
13849 base_type_for_mode (mode, 1),
13850 mode, mem_mode);
13852 break;
13854 case NOT:
13855 op = DW_OP_not;
13856 goto do_unop;
13858 case ABS:
13859 op = DW_OP_abs;
13860 goto do_unop;
13862 case NEG:
13863 op = DW_OP_neg;
13864 goto do_unop;
13866 do_unop:
13867 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13868 VAR_INIT_STATUS_INITIALIZED);
13870 if (op0 == 0)
13871 break;
13873 mem_loc_result = op0;
13874 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13875 break;
13877 case CONST_INT:
13878 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13879 #ifdef POINTERS_EXTEND_UNSIGNED
13880 || (mode == Pmode
13881 && mem_mode != VOIDmode
13882 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13883 #endif
13886 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13887 break;
13889 if (!dwarf_strict
13890 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13891 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13893 dw_die_ref type_die = base_type_for_mode (mode, 1);
13894 machine_mode amode;
13895 if (type_die == NULL)
13896 return NULL;
13897 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13898 MODE_INT, 0);
13899 if (INTVAL (rtl) >= 0
13900 && amode != BLKmode
13901 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13902 /* const DW_OP_GNU_convert <XXX> vs.
13903 DW_OP_GNU_const_type <XXX, 1, const>. */
13904 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13905 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13907 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13908 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13909 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13910 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13911 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13912 add_loc_descr (&mem_loc_result, op0);
13913 return mem_loc_result;
13915 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13916 INTVAL (rtl));
13917 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13918 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13919 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13920 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13921 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13922 else
13924 mem_loc_result->dw_loc_oprnd2.val_class
13925 = dw_val_class_const_double;
13926 mem_loc_result->dw_loc_oprnd2.v.val_double
13927 = double_int::from_shwi (INTVAL (rtl));
13930 break;
13932 case CONST_DOUBLE:
13933 if (!dwarf_strict)
13935 dw_die_ref type_die;
13937 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13938 CONST_DOUBLE rtx could represent either a large integer
13939 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13940 the value is always a floating point constant.
13942 When it is an integer, a CONST_DOUBLE is used whenever
13943 the constant requires 2 HWIs to be adequately represented.
13944 We output CONST_DOUBLEs as blocks. */
13945 if (mode == VOIDmode
13946 || (GET_MODE (rtl) == VOIDmode
13947 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13948 break;
13949 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13950 if (type_die == NULL)
13951 return NULL;
13952 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13953 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13954 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13955 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13956 #if TARGET_SUPPORTS_WIDE_INT == 0
13957 if (!SCALAR_FLOAT_MODE_P (mode))
13959 mem_loc_result->dw_loc_oprnd2.val_class
13960 = dw_val_class_const_double;
13961 mem_loc_result->dw_loc_oprnd2.v.val_double
13962 = rtx_to_double_int (rtl);
13964 else
13965 #endif
13967 unsigned int length = GET_MODE_SIZE (mode);
13968 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13970 insert_float (rtl, array);
13971 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13972 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13973 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13974 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13977 break;
13979 case CONST_WIDE_INT:
13980 if (!dwarf_strict)
13982 dw_die_ref type_die;
13984 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13985 if (type_die == NULL)
13986 return NULL;
13987 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13988 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13989 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13990 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13991 mem_loc_result->dw_loc_oprnd2.val_class
13992 = dw_val_class_wide_int;
13993 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13994 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13996 break;
13998 case EQ:
13999 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
14000 break;
14002 case GE:
14003 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14004 break;
14006 case GT:
14007 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14008 break;
14010 case LE:
14011 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14012 break;
14014 case LT:
14015 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14016 break;
14018 case NE:
14019 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14020 break;
14022 case GEU:
14023 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14024 break;
14026 case GTU:
14027 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14028 break;
14030 case LEU:
14031 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14032 break;
14034 case LTU:
14035 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14036 break;
14038 case UMIN:
14039 case UMAX:
14040 if (!SCALAR_INT_MODE_P (mode))
14041 break;
14042 /* FALLTHRU */
14043 case SMIN:
14044 case SMAX:
14045 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14046 break;
14048 case ZERO_EXTRACT:
14049 case SIGN_EXTRACT:
14050 if (CONST_INT_P (XEXP (rtl, 1))
14051 && CONST_INT_P (XEXP (rtl, 2))
14052 && ((unsigned) INTVAL (XEXP (rtl, 1))
14053 + (unsigned) INTVAL (XEXP (rtl, 2))
14054 <= GET_MODE_BITSIZE (mode))
14055 && SCALAR_INT_MODE_P (mode)
14056 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14057 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14059 int shift, size;
14060 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14061 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14062 if (op0 == 0)
14063 break;
14064 if (GET_CODE (rtl) == SIGN_EXTRACT)
14065 op = DW_OP_shra;
14066 else
14067 op = DW_OP_shr;
14068 mem_loc_result = op0;
14069 size = INTVAL (XEXP (rtl, 1));
14070 shift = INTVAL (XEXP (rtl, 2));
14071 if (BITS_BIG_ENDIAN)
14072 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14073 - shift - size;
14074 if (shift + size != (int) DWARF2_ADDR_SIZE)
14076 add_loc_descr (&mem_loc_result,
14077 int_loc_descriptor (DWARF2_ADDR_SIZE
14078 - shift - size));
14079 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14081 if (size != (int) DWARF2_ADDR_SIZE)
14083 add_loc_descr (&mem_loc_result,
14084 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14085 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14088 break;
14090 case IF_THEN_ELSE:
14092 dw_loc_descr_ref op2, bra_node, drop_node;
14093 op0 = mem_loc_descriptor (XEXP (rtl, 0),
14094 GET_MODE (XEXP (rtl, 0)) == VOIDmode
14095 ? word_mode : GET_MODE (XEXP (rtl, 0)),
14096 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14097 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14098 VAR_INIT_STATUS_INITIALIZED);
14099 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14100 VAR_INIT_STATUS_INITIALIZED);
14101 if (op0 == NULL || op1 == NULL || op2 == NULL)
14102 break;
14104 mem_loc_result = op1;
14105 add_loc_descr (&mem_loc_result, op2);
14106 add_loc_descr (&mem_loc_result, op0);
14107 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14108 add_loc_descr (&mem_loc_result, bra_node);
14109 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14110 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14111 add_loc_descr (&mem_loc_result, drop_node);
14112 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14113 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14115 break;
14117 case FLOAT_EXTEND:
14118 case FLOAT_TRUNCATE:
14119 case FLOAT:
14120 case UNSIGNED_FLOAT:
14121 case FIX:
14122 case UNSIGNED_FIX:
14123 if (!dwarf_strict)
14125 dw_die_ref type_die;
14126 dw_loc_descr_ref cvt;
14128 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14129 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14130 if (op0 == NULL)
14131 break;
14132 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
14133 && (GET_CODE (rtl) == FLOAT
14134 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14135 <= DWARF2_ADDR_SIZE))
14137 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14138 GET_CODE (rtl) == UNSIGNED_FLOAT);
14139 if (type_die == NULL)
14140 break;
14141 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14142 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14143 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14144 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14145 add_loc_descr (&op0, cvt);
14147 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14148 if (type_die == NULL)
14149 break;
14150 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14151 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14152 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14153 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14154 add_loc_descr (&op0, cvt);
14155 if (SCALAR_INT_MODE_P (mode)
14156 && (GET_CODE (rtl) == FIX
14157 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14159 op0 = convert_descriptor_to_mode (mode, op0);
14160 if (op0 == NULL)
14161 break;
14163 mem_loc_result = op0;
14165 break;
14167 case CLZ:
14168 case CTZ:
14169 case FFS:
14170 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14171 break;
14173 case POPCOUNT:
14174 case PARITY:
14175 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14176 break;
14178 case BSWAP:
14179 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14180 break;
14182 case ROTATE:
14183 case ROTATERT:
14184 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14185 break;
14187 case COMPARE:
14188 /* In theory, we could implement the above. */
14189 /* DWARF cannot represent the unsigned compare operations
14190 natively. */
14191 case SS_MULT:
14192 case US_MULT:
14193 case SS_DIV:
14194 case US_DIV:
14195 case SS_PLUS:
14196 case US_PLUS:
14197 case SS_MINUS:
14198 case US_MINUS:
14199 case SS_NEG:
14200 case US_NEG:
14201 case SS_ABS:
14202 case SS_ASHIFT:
14203 case US_ASHIFT:
14204 case SS_TRUNCATE:
14205 case US_TRUNCATE:
14206 case UNORDERED:
14207 case ORDERED:
14208 case UNEQ:
14209 case UNGE:
14210 case UNGT:
14211 case UNLE:
14212 case UNLT:
14213 case LTGT:
14214 case FRACT_CONVERT:
14215 case UNSIGNED_FRACT_CONVERT:
14216 case SAT_FRACT:
14217 case UNSIGNED_SAT_FRACT:
14218 case SQRT:
14219 case ASM_OPERANDS:
14220 case VEC_MERGE:
14221 case VEC_SELECT:
14222 case VEC_CONCAT:
14223 case VEC_DUPLICATE:
14224 case UNSPEC:
14225 case HIGH:
14226 case FMA:
14227 case STRICT_LOW_PART:
14228 case CONST_VECTOR:
14229 case CONST_FIXED:
14230 case CLRSB:
14231 case CLOBBER:
14232 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14233 can't express it in the debug info. This can happen e.g. with some
14234 TLS UNSPECs. */
14235 break;
14237 case CONST_STRING:
14238 resolve_one_addr (&rtl);
14239 goto symref;
14241 default:
14242 if (flag_checking)
14244 print_rtl (stderr, rtl);
14245 gcc_unreachable ();
14247 break;
14250 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14251 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14253 return mem_loc_result;
14256 /* Return a descriptor that describes the concatenation of two locations.
14257 This is typically a complex variable. */
14259 static dw_loc_descr_ref
14260 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14262 dw_loc_descr_ref cc_loc_result = NULL;
14263 dw_loc_descr_ref x0_ref
14264 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14265 dw_loc_descr_ref x1_ref
14266 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14268 if (x0_ref == 0 || x1_ref == 0)
14269 return 0;
14271 cc_loc_result = x0_ref;
14272 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14274 add_loc_descr (&cc_loc_result, x1_ref);
14275 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14277 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14278 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14280 return cc_loc_result;
14283 /* Return a descriptor that describes the concatenation of N
14284 locations. */
14286 static dw_loc_descr_ref
14287 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14289 unsigned int i;
14290 dw_loc_descr_ref cc_loc_result = NULL;
14291 unsigned int n = XVECLEN (concatn, 0);
14293 for (i = 0; i < n; ++i)
14295 dw_loc_descr_ref ref;
14296 rtx x = XVECEXP (concatn, 0, i);
14298 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14299 if (ref == NULL)
14300 return NULL;
14302 add_loc_descr (&cc_loc_result, ref);
14303 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14306 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14307 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14309 return cc_loc_result;
14312 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14313 for DEBUG_IMPLICIT_PTR RTL. */
14315 static dw_loc_descr_ref
14316 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14318 dw_loc_descr_ref ret;
14319 dw_die_ref ref;
14321 if (dwarf_strict)
14322 return NULL;
14323 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14324 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14325 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14326 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14327 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14328 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14329 if (ref)
14331 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14332 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14333 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14335 else
14337 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14338 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14340 return ret;
14343 /* Output a proper Dwarf location descriptor for a variable or parameter
14344 which is either allocated in a register or in a memory location. For a
14345 register, we just generate an OP_REG and the register number. For a
14346 memory location we provide a Dwarf postfix expression describing how to
14347 generate the (dynamic) address of the object onto the address stack.
14349 MODE is mode of the decl if this loc_descriptor is going to be used in
14350 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14351 allowed, VOIDmode otherwise.
14353 If we don't know how to describe it, return 0. */
14355 static dw_loc_descr_ref
14356 loc_descriptor (rtx rtl, machine_mode mode,
14357 enum var_init_status initialized)
14359 dw_loc_descr_ref loc_result = NULL;
14361 switch (GET_CODE (rtl))
14363 case SUBREG:
14364 /* The case of a subreg may arise when we have a local (register)
14365 variable or a formal (register) parameter which doesn't quite fill
14366 up an entire register. For now, just assume that it is
14367 legitimate to make the Dwarf info refer to the whole register which
14368 contains the given subreg. */
14369 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14370 loc_result = loc_descriptor (SUBREG_REG (rtl),
14371 GET_MODE (SUBREG_REG (rtl)), initialized);
14372 else
14373 goto do_default;
14374 break;
14376 case REG:
14377 loc_result = reg_loc_descriptor (rtl, initialized);
14378 break;
14380 case MEM:
14381 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14382 GET_MODE (rtl), initialized);
14383 if (loc_result == NULL)
14384 loc_result = tls_mem_loc_descriptor (rtl);
14385 if (loc_result == NULL)
14387 rtx new_rtl = avoid_constant_pool_reference (rtl);
14388 if (new_rtl != rtl)
14389 loc_result = loc_descriptor (new_rtl, mode, initialized);
14391 break;
14393 case CONCAT:
14394 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14395 initialized);
14396 break;
14398 case CONCATN:
14399 loc_result = concatn_loc_descriptor (rtl, initialized);
14400 break;
14402 case VAR_LOCATION:
14403 /* Single part. */
14404 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14406 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14407 if (GET_CODE (loc) == EXPR_LIST)
14408 loc = XEXP (loc, 0);
14409 loc_result = loc_descriptor (loc, mode, initialized);
14410 break;
14413 rtl = XEXP (rtl, 1);
14414 /* FALLTHRU */
14416 case PARALLEL:
14418 rtvec par_elems = XVEC (rtl, 0);
14419 int num_elem = GET_NUM_ELEM (par_elems);
14420 machine_mode mode;
14421 int i;
14423 /* Create the first one, so we have something to add to. */
14424 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14425 VOIDmode, initialized);
14426 if (loc_result == NULL)
14427 return NULL;
14428 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14429 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14430 for (i = 1; i < num_elem; i++)
14432 dw_loc_descr_ref temp;
14434 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14435 VOIDmode, initialized);
14436 if (temp == NULL)
14437 return NULL;
14438 add_loc_descr (&loc_result, temp);
14439 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14440 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14443 break;
14445 case CONST_INT:
14446 if (mode != VOIDmode && mode != BLKmode)
14447 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14448 INTVAL (rtl));
14449 break;
14451 case CONST_DOUBLE:
14452 if (mode == VOIDmode)
14453 mode = GET_MODE (rtl);
14455 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14457 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14459 /* Note that a CONST_DOUBLE rtx could represent either an integer
14460 or a floating-point constant. A CONST_DOUBLE is used whenever
14461 the constant requires more than one word in order to be
14462 adequately represented. We output CONST_DOUBLEs as blocks. */
14463 loc_result = new_loc_descr (DW_OP_implicit_value,
14464 GET_MODE_SIZE (mode), 0);
14465 #if TARGET_SUPPORTS_WIDE_INT == 0
14466 if (!SCALAR_FLOAT_MODE_P (mode))
14468 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14469 loc_result->dw_loc_oprnd2.v.val_double
14470 = rtx_to_double_int (rtl);
14472 else
14473 #endif
14475 unsigned int length = GET_MODE_SIZE (mode);
14476 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14478 insert_float (rtl, array);
14479 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14480 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14481 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14482 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14485 break;
14487 case CONST_WIDE_INT:
14488 if (mode == VOIDmode)
14489 mode = GET_MODE (rtl);
14491 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14493 loc_result = new_loc_descr (DW_OP_implicit_value,
14494 GET_MODE_SIZE (mode), 0);
14495 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14496 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14497 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14499 break;
14501 case CONST_VECTOR:
14502 if (mode == VOIDmode)
14503 mode = GET_MODE (rtl);
14505 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14507 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14508 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14509 unsigned char *array
14510 = ggc_vec_alloc<unsigned char> (length * elt_size);
14511 unsigned int i;
14512 unsigned char *p;
14513 machine_mode imode = GET_MODE_INNER (mode);
14515 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14516 switch (GET_MODE_CLASS (mode))
14518 case MODE_VECTOR_INT:
14519 for (i = 0, p = array; i < length; i++, p += elt_size)
14521 rtx elt = CONST_VECTOR_ELT (rtl, i);
14522 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14524 break;
14526 case MODE_VECTOR_FLOAT:
14527 for (i = 0, p = array; i < length; i++, p += elt_size)
14529 rtx elt = CONST_VECTOR_ELT (rtl, i);
14530 insert_float (elt, p);
14532 break;
14534 default:
14535 gcc_unreachable ();
14538 loc_result = new_loc_descr (DW_OP_implicit_value,
14539 length * elt_size, 0);
14540 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14541 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14542 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14543 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14545 break;
14547 case CONST:
14548 if (mode == VOIDmode
14549 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14550 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14551 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14553 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14554 break;
14556 /* FALLTHROUGH */
14557 case SYMBOL_REF:
14558 if (!const_ok_for_output (rtl))
14559 break;
14560 case LABEL_REF:
14561 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14562 && (dwarf_version >= 4 || !dwarf_strict))
14564 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14565 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14566 vec_safe_push (used_rtx_array, rtl);
14568 break;
14570 case DEBUG_IMPLICIT_PTR:
14571 loc_result = implicit_ptr_descriptor (rtl, 0);
14572 break;
14574 case PLUS:
14575 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14576 && CONST_INT_P (XEXP (rtl, 1)))
14578 loc_result
14579 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14580 break;
14582 /* FALLTHRU */
14583 do_default:
14584 default:
14585 if ((SCALAR_INT_MODE_P (mode)
14586 && GET_MODE (rtl) == mode
14587 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14588 && dwarf_version >= 4)
14589 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14591 /* Value expression. */
14592 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14593 if (loc_result)
14594 add_loc_descr (&loc_result,
14595 new_loc_descr (DW_OP_stack_value, 0, 0));
14597 break;
14600 return loc_result;
14603 /* We need to figure out what section we should use as the base for the
14604 address ranges where a given location is valid.
14605 1. If this particular DECL has a section associated with it, use that.
14606 2. If this function has a section associated with it, use that.
14607 3. Otherwise, use the text section.
14608 XXX: If you split a variable across multiple sections, we won't notice. */
14610 static const char *
14611 secname_for_decl (const_tree decl)
14613 const char *secname;
14615 if (VAR_OR_FUNCTION_DECL_P (decl)
14616 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14617 && DECL_SECTION_NAME (decl))
14618 secname = DECL_SECTION_NAME (decl);
14619 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14620 secname = DECL_SECTION_NAME (current_function_decl);
14621 else if (cfun && in_cold_section_p)
14622 secname = crtl->subsections.cold_section_label;
14623 else
14624 secname = text_section_label;
14626 return secname;
14629 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14631 static bool
14632 decl_by_reference_p (tree decl)
14634 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14635 || TREE_CODE (decl) == VAR_DECL)
14636 && DECL_BY_REFERENCE (decl));
14639 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14640 for VARLOC. */
14642 static dw_loc_descr_ref
14643 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14644 enum var_init_status initialized)
14646 int have_address = 0;
14647 dw_loc_descr_ref descr;
14648 machine_mode mode;
14650 if (want_address != 2)
14652 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14653 /* Single part. */
14654 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14656 varloc = PAT_VAR_LOCATION_LOC (varloc);
14657 if (GET_CODE (varloc) == EXPR_LIST)
14658 varloc = XEXP (varloc, 0);
14659 mode = GET_MODE (varloc);
14660 if (MEM_P (varloc))
14662 rtx addr = XEXP (varloc, 0);
14663 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14664 mode, initialized);
14665 if (descr)
14666 have_address = 1;
14667 else
14669 rtx x = avoid_constant_pool_reference (varloc);
14670 if (x != varloc)
14671 descr = mem_loc_descriptor (x, mode, VOIDmode,
14672 initialized);
14675 else
14676 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14678 else
14679 return 0;
14681 else
14683 if (GET_CODE (varloc) == VAR_LOCATION)
14684 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14685 else
14686 mode = DECL_MODE (loc);
14687 descr = loc_descriptor (varloc, mode, initialized);
14688 have_address = 1;
14691 if (!descr)
14692 return 0;
14694 if (want_address == 2 && !have_address
14695 && (dwarf_version >= 4 || !dwarf_strict))
14697 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14699 expansion_failed (loc, NULL_RTX,
14700 "DWARF address size mismatch");
14701 return 0;
14703 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14704 have_address = 1;
14706 /* Show if we can't fill the request for an address. */
14707 if (want_address && !have_address)
14709 expansion_failed (loc, NULL_RTX,
14710 "Want address and only have value");
14711 return 0;
14714 /* If we've got an address and don't want one, dereference. */
14715 if (!want_address && have_address)
14717 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14718 enum dwarf_location_atom op;
14720 if (size > DWARF2_ADDR_SIZE || size == -1)
14722 expansion_failed (loc, NULL_RTX,
14723 "DWARF address size mismatch");
14724 return 0;
14726 else if (size == DWARF2_ADDR_SIZE)
14727 op = DW_OP_deref;
14728 else
14729 op = DW_OP_deref_size;
14731 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14734 return descr;
14737 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14738 if it is not possible. */
14740 static dw_loc_descr_ref
14741 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14743 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14744 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14745 else if (dwarf_version >= 3 || !dwarf_strict)
14746 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14747 else
14748 return NULL;
14751 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14752 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14754 static dw_loc_descr_ref
14755 dw_sra_loc_expr (tree decl, rtx loc)
14757 rtx p;
14758 unsigned HOST_WIDE_INT padsize = 0;
14759 dw_loc_descr_ref descr, *descr_tail;
14760 unsigned HOST_WIDE_INT decl_size;
14761 rtx varloc;
14762 enum var_init_status initialized;
14764 if (DECL_SIZE (decl) == NULL
14765 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14766 return NULL;
14768 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14769 descr = NULL;
14770 descr_tail = &descr;
14772 for (p = loc; p; p = XEXP (p, 1))
14774 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14775 rtx loc_note = *decl_piece_varloc_ptr (p);
14776 dw_loc_descr_ref cur_descr;
14777 dw_loc_descr_ref *tail, last = NULL;
14778 unsigned HOST_WIDE_INT opsize = 0;
14780 if (loc_note == NULL_RTX
14781 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14783 padsize += bitsize;
14784 continue;
14786 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14787 varloc = NOTE_VAR_LOCATION (loc_note);
14788 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14789 if (cur_descr == NULL)
14791 padsize += bitsize;
14792 continue;
14795 /* Check that cur_descr either doesn't use
14796 DW_OP_*piece operations, or their sum is equal
14797 to bitsize. Otherwise we can't embed it. */
14798 for (tail = &cur_descr; *tail != NULL;
14799 tail = &(*tail)->dw_loc_next)
14800 if ((*tail)->dw_loc_opc == DW_OP_piece)
14802 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14803 * BITS_PER_UNIT;
14804 last = *tail;
14806 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14808 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14809 last = *tail;
14812 if (last != NULL && opsize != bitsize)
14814 padsize += bitsize;
14815 /* Discard the current piece of the descriptor and release any
14816 addr_table entries it uses. */
14817 remove_loc_list_addr_table_entries (cur_descr);
14818 continue;
14821 /* If there is a hole, add DW_OP_*piece after empty DWARF
14822 expression, which means that those bits are optimized out. */
14823 if (padsize)
14825 if (padsize > decl_size)
14827 remove_loc_list_addr_table_entries (cur_descr);
14828 goto discard_descr;
14830 decl_size -= padsize;
14831 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14832 if (*descr_tail == NULL)
14834 remove_loc_list_addr_table_entries (cur_descr);
14835 goto discard_descr;
14837 descr_tail = &(*descr_tail)->dw_loc_next;
14838 padsize = 0;
14840 *descr_tail = cur_descr;
14841 descr_tail = tail;
14842 if (bitsize > decl_size)
14843 goto discard_descr;
14844 decl_size -= bitsize;
14845 if (last == NULL)
14847 HOST_WIDE_INT offset = 0;
14848 if (GET_CODE (varloc) == VAR_LOCATION
14849 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14851 varloc = PAT_VAR_LOCATION_LOC (varloc);
14852 if (GET_CODE (varloc) == EXPR_LIST)
14853 varloc = XEXP (varloc, 0);
14857 if (GET_CODE (varloc) == CONST
14858 || GET_CODE (varloc) == SIGN_EXTEND
14859 || GET_CODE (varloc) == ZERO_EXTEND)
14860 varloc = XEXP (varloc, 0);
14861 else if (GET_CODE (varloc) == SUBREG)
14862 varloc = SUBREG_REG (varloc);
14863 else
14864 break;
14866 while (1);
14867 /* DW_OP_bit_size offset should be zero for register
14868 or implicit location descriptions and empty location
14869 descriptions, but for memory addresses needs big endian
14870 adjustment. */
14871 if (MEM_P (varloc))
14873 unsigned HOST_WIDE_INT memsize
14874 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14875 if (memsize != bitsize)
14877 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14878 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14879 goto discard_descr;
14880 if (memsize < bitsize)
14881 goto discard_descr;
14882 if (BITS_BIG_ENDIAN)
14883 offset = memsize - bitsize;
14887 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14888 if (*descr_tail == NULL)
14889 goto discard_descr;
14890 descr_tail = &(*descr_tail)->dw_loc_next;
14894 /* If there were any non-empty expressions, add padding till the end of
14895 the decl. */
14896 if (descr != NULL && decl_size != 0)
14898 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14899 if (*descr_tail == NULL)
14900 goto discard_descr;
14902 return descr;
14904 discard_descr:
14905 /* Discard the descriptor and release any addr_table entries it uses. */
14906 remove_loc_list_addr_table_entries (descr);
14907 return NULL;
14910 /* Return the dwarf representation of the location list LOC_LIST of
14911 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14912 function. */
14914 static dw_loc_list_ref
14915 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14917 const char *endname, *secname;
14918 rtx varloc;
14919 enum var_init_status initialized;
14920 struct var_loc_node *node;
14921 dw_loc_descr_ref descr;
14922 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14923 dw_loc_list_ref list = NULL;
14924 dw_loc_list_ref *listp = &list;
14926 /* Now that we know what section we are using for a base,
14927 actually construct the list of locations.
14928 The first location information is what is passed to the
14929 function that creates the location list, and the remaining
14930 locations just get added on to that list.
14931 Note that we only know the start address for a location
14932 (IE location changes), so to build the range, we use
14933 the range [current location start, next location start].
14934 This means we have to special case the last node, and generate
14935 a range of [last location start, end of function label]. */
14937 secname = secname_for_decl (decl);
14939 for (node = loc_list->first; node; node = node->next)
14940 if (GET_CODE (node->loc) == EXPR_LIST
14941 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14943 if (GET_CODE (node->loc) == EXPR_LIST)
14945 /* This requires DW_OP_{,bit_}piece, which is not usable
14946 inside DWARF expressions. */
14947 if (want_address != 2)
14948 continue;
14949 descr = dw_sra_loc_expr (decl, node->loc);
14950 if (descr == NULL)
14951 continue;
14953 else
14955 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14956 varloc = NOTE_VAR_LOCATION (node->loc);
14957 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14959 if (descr)
14961 bool range_across_switch = false;
14962 /* If section switch happens in between node->label
14963 and node->next->label (or end of function) and
14964 we can't emit it as a single entry list,
14965 emit two ranges, first one ending at the end
14966 of first partition and second one starting at the
14967 beginning of second partition. */
14968 if (node == loc_list->last_before_switch
14969 && (node != loc_list->first || loc_list->first->next)
14970 && current_function_decl)
14972 endname = cfun->fde->dw_fde_end;
14973 range_across_switch = true;
14975 /* The variable has a location between NODE->LABEL and
14976 NODE->NEXT->LABEL. */
14977 else if (node->next)
14978 endname = node->next->label;
14979 /* If the variable has a location at the last label
14980 it keeps its location until the end of function. */
14981 else if (!current_function_decl)
14982 endname = text_end_label;
14983 else
14985 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14986 current_function_funcdef_no);
14987 endname = ggc_strdup (label_id);
14990 *listp = new_loc_list (descr, node->label, endname, secname);
14991 if (TREE_CODE (decl) == PARM_DECL
14992 && node == loc_list->first
14993 && NOTE_P (node->loc)
14994 && strcmp (node->label, endname) == 0)
14995 (*listp)->force = true;
14996 listp = &(*listp)->dw_loc_next;
14998 if (range_across_switch)
15000 if (GET_CODE (node->loc) == EXPR_LIST)
15001 descr = dw_sra_loc_expr (decl, node->loc);
15002 else
15004 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15005 varloc = NOTE_VAR_LOCATION (node->loc);
15006 descr = dw_loc_list_1 (decl, varloc, want_address,
15007 initialized);
15009 gcc_assert (descr);
15010 /* The variable has a location between NODE->LABEL and
15011 NODE->NEXT->LABEL. */
15012 if (node->next)
15013 endname = node->next->label;
15014 else
15015 endname = cfun->fde->dw_fde_second_end;
15016 *listp = new_loc_list (descr,
15017 cfun->fde->dw_fde_second_begin,
15018 endname, secname);
15019 listp = &(*listp)->dw_loc_next;
15024 /* Try to avoid the overhead of a location list emitting a location
15025 expression instead, but only if we didn't have more than one
15026 location entry in the first place. If some entries were not
15027 representable, we don't want to pretend a single entry that was
15028 applies to the entire scope in which the variable is
15029 available. */
15030 if (list && loc_list->first->next)
15031 gen_llsym (list);
15033 return list;
15036 /* Return if the loc_list has only single element and thus can be represented
15037 as location description. */
15039 static bool
15040 single_element_loc_list_p (dw_loc_list_ref list)
15042 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15043 return !list->ll_symbol;
15046 /* To each location in list LIST add loc descr REF. */
15048 static void
15049 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15051 dw_loc_descr_ref copy;
15052 add_loc_descr (&list->expr, ref);
15053 list = list->dw_loc_next;
15054 while (list)
15056 copy = ggc_alloc<dw_loc_descr_node> ();
15057 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15058 add_loc_descr (&list->expr, copy);
15059 while (copy->dw_loc_next)
15061 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15062 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15063 copy->dw_loc_next = new_copy;
15064 copy = new_copy;
15066 list = list->dw_loc_next;
15070 /* Given two lists RET and LIST
15071 produce location list that is result of adding expression in LIST
15072 to expression in RET on each position in program.
15073 Might be destructive on both RET and LIST.
15075 TODO: We handle only simple cases of RET or LIST having at most one
15076 element. General case would inolve sorting the lists in program order
15077 and merging them that will need some additional work.
15078 Adding that will improve quality of debug info especially for SRA-ed
15079 structures. */
15081 static void
15082 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15084 if (!list)
15085 return;
15086 if (!*ret)
15088 *ret = list;
15089 return;
15091 if (!list->dw_loc_next)
15093 add_loc_descr_to_each (*ret, list->expr);
15094 return;
15096 if (!(*ret)->dw_loc_next)
15098 add_loc_descr_to_each (list, (*ret)->expr);
15099 *ret = list;
15100 return;
15102 expansion_failed (NULL_TREE, NULL_RTX,
15103 "Don't know how to merge two non-trivial"
15104 " location lists.\n");
15105 *ret = NULL;
15106 return;
15109 /* LOC is constant expression. Try a luck, look it up in constant
15110 pool and return its loc_descr of its address. */
15112 static dw_loc_descr_ref
15113 cst_pool_loc_descr (tree loc)
15115 /* Get an RTL for this, if something has been emitted. */
15116 rtx rtl = lookup_constant_def (loc);
15118 if (!rtl || !MEM_P (rtl))
15120 gcc_assert (!rtl);
15121 return 0;
15123 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15125 /* TODO: We might get more coverage if we was actually delaying expansion
15126 of all expressions till end of compilation when constant pools are fully
15127 populated. */
15128 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15130 expansion_failed (loc, NULL_RTX,
15131 "CST value in contant pool but not marked.");
15132 return 0;
15134 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15135 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15138 /* Return dw_loc_list representing address of addr_expr LOC
15139 by looking for inner INDIRECT_REF expression and turning
15140 it into simple arithmetics.
15142 See loc_list_from_tree for the meaning of CONTEXT. */
15144 static dw_loc_list_ref
15145 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15146 const loc_descr_context *context)
15148 tree obj, offset;
15149 HOST_WIDE_INT bitsize, bitpos, bytepos;
15150 machine_mode mode;
15151 int unsignedp, reversep, volatilep = 0;
15152 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15154 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15155 &bitsize, &bitpos, &offset, &mode,
15156 &unsignedp, &reversep, &volatilep);
15157 STRIP_NOPS (obj);
15158 if (bitpos % BITS_PER_UNIT)
15160 expansion_failed (loc, NULL_RTX, "bitfield access");
15161 return 0;
15163 if (!INDIRECT_REF_P (obj))
15165 expansion_failed (obj,
15166 NULL_RTX, "no indirect ref in inner refrence");
15167 return 0;
15169 if (!offset && !bitpos)
15170 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15171 context);
15172 else if (toplev
15173 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15174 && (dwarf_version >= 4 || !dwarf_strict))
15176 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15177 if (!list_ret)
15178 return 0;
15179 if (offset)
15181 /* Variable offset. */
15182 list_ret1 = loc_list_from_tree (offset, 0, context);
15183 if (list_ret1 == 0)
15184 return 0;
15185 add_loc_list (&list_ret, list_ret1);
15186 if (!list_ret)
15187 return 0;
15188 add_loc_descr_to_each (list_ret,
15189 new_loc_descr (DW_OP_plus, 0, 0));
15191 bytepos = bitpos / BITS_PER_UNIT;
15192 if (bytepos > 0)
15193 add_loc_descr_to_each (list_ret,
15194 new_loc_descr (DW_OP_plus_uconst,
15195 bytepos, 0));
15196 else if (bytepos < 0)
15197 loc_list_plus_const (list_ret, bytepos);
15198 add_loc_descr_to_each (list_ret,
15199 new_loc_descr (DW_OP_stack_value, 0, 0));
15201 return list_ret;
15204 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15205 all operations from LOC are nops, move to the last one. Insert in NOPS all
15206 operations that are skipped. */
15208 static void
15209 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15210 hash_set<dw_loc_descr_ref> &nops)
15212 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15214 nops.add (loc);
15215 loc = loc->dw_loc_next;
15219 /* Helper for loc_descr_without_nops: free the location description operation
15220 P. */
15222 bool
15223 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15225 ggc_free (loc);
15226 return true;
15229 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15230 finishes LOC. */
15232 static void
15233 loc_descr_without_nops (dw_loc_descr_ref &loc)
15235 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15236 return;
15238 /* Set of all DW_OP_nop operations we remove. */
15239 hash_set<dw_loc_descr_ref> nops;
15241 /* First, strip all prefix NOP operations in order to keep the head of the
15242 operations list. */
15243 loc_descr_to_next_no_nop (loc, nops);
15245 for (dw_loc_descr_ref cur = loc; cur != NULL;)
15247 /* For control flow operations: strip "prefix" nops in destination
15248 labels. */
15249 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15250 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15251 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15252 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15254 /* Do the same for the operations that follow, then move to the next
15255 iteration. */
15256 if (cur->dw_loc_next != NULL)
15257 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15258 cur = cur->dw_loc_next;
15261 nops.traverse<void *, free_loc_descr> (NULL);
15265 struct dwarf_procedure_info;
15267 /* Helper structure for location descriptions generation. */
15268 struct loc_descr_context
15270 /* The type that is implicitly referenced by DW_OP_push_object_address, or
15271 NULL_TREE if DW_OP_push_object_address in invalid for this location
15272 description. This is used when processing PLACEHOLDER_EXPR nodes. */
15273 tree context_type;
15274 /* The ..._DECL node that should be translated as a
15275 DW_OP_push_object_address operation. */
15276 tree base_decl;
15277 /* Information about the DWARF procedure we are currently generating. NULL if
15278 we are not generating a DWARF procedure. */
15279 struct dwarf_procedure_info *dpi;
15282 /* DWARF procedures generation
15284 DWARF expressions (aka. location descriptions) are used to encode variable
15285 things such as sizes or offsets. Such computations can have redundant parts
15286 that can be factorized in order to reduce the size of the output debug
15287 information. This is the whole point of DWARF procedures.
15289 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15290 already factorized into functions ("size functions") in order to handle very
15291 big and complex types. Such functions are quite simple: they have integral
15292 arguments, they return an integral result and their body contains only a
15293 return statement with arithmetic expressions. This is the only kind of
15294 function we are interested in translating into DWARF procedures, here.
15296 DWARF expressions and DWARF procedure are executed using a stack, so we have
15297 to define some calling convention for them to interact. Let's say that:
15299 - Before calling a DWARF procedure, DWARF expressions must push on the stack
15300 all arguments in reverse order (right-to-left) so that when the DWARF
15301 procedure execution starts, the first argument is the top of the stack.
15303 - Then, when returning, the DWARF procedure must have consumed all arguments
15304 on the stack, must have pushed the result and touched nothing else.
15306 - Each integral argument and the result are integral types can be hold in a
15307 single stack slot.
15309 - We call "frame offset" the number of stack slots that are "under DWARF
15310 procedure control": it includes the arguments slots, the temporaries and
15311 the result slot. Thus, it is equal to the number of arguments when the
15312 procedure execution starts and must be equal to one (the result) when it
15313 returns. */
15315 /* Helper structure used when generating operations for a DWARF procedure. */
15316 struct dwarf_procedure_info
15318 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15319 currently translated. */
15320 tree fndecl;
15321 /* The number of arguments FNDECL takes. */
15322 unsigned args_count;
15325 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
15326 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
15327 equate it to this DIE. */
15329 static dw_die_ref
15330 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15331 dw_die_ref parent_die)
15333 dw_die_ref dwarf_proc_die;
15335 if ((dwarf_version < 3 && dwarf_strict)
15336 || location == NULL)
15337 return NULL;
15339 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
15340 if (fndecl)
15341 equate_decl_number_to_die (fndecl, dwarf_proc_die);
15342 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15343 return dwarf_proc_die;
15346 /* Return whether TYPE is a supported type as a DWARF procedure argument
15347 type or return type (we handle only scalar types and pointer types that
15348 aren't wider than the DWARF expression evaluation stack. */
15350 static bool
15351 is_handled_procedure_type (tree type)
15353 return ((INTEGRAL_TYPE_P (type)
15354 || TREE_CODE (type) == OFFSET_TYPE
15355 || TREE_CODE (type) == POINTER_TYPE)
15356 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15359 /* Helper for resolve_args_picking: do the same but stop when coming across
15360 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
15361 offset *before* evaluating the corresponding operation. */
15363 static bool
15364 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15365 struct dwarf_procedure_info *dpi,
15366 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
15368 /* The "frame_offset" identifier is already used to name a macro... */
15369 unsigned frame_offset_ = initial_frame_offset;
15370 dw_loc_descr_ref l;
15372 for (l = loc; l != NULL;)
15374 bool existed;
15375 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
15377 /* If we already met this node, there is nothing to compute anymore. */
15378 if (existed)
15380 /* Make sure that the stack size is consistent wherever the execution
15381 flow comes from. */
15382 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
15383 break;
15385 l_frame_offset = frame_offset_;
15387 /* If needed, relocate the picking offset with respect to the frame
15388 offset. */
15389 if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15391 /* frame_offset_ is the size of the current stack frame, including
15392 incoming arguments. Besides, the arguments are pushed
15393 right-to-left. Thus, in order to access the Nth argument from
15394 this operation node, the picking has to skip temporaries *plus*
15395 one stack slot per argument (0 for the first one, 1 for the second
15396 one, etc.).
15398 The targetted argument number (N) is already set as the operand,
15399 and the number of temporaries can be computed with:
15400 frame_offsets_ - dpi->args_count */
15401 l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15403 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
15404 if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15405 return false;
15408 /* Update frame_offset according to the effect the current operation has
15409 on the stack. */
15410 switch (l->dw_loc_opc)
15412 case DW_OP_deref:
15413 case DW_OP_swap:
15414 case DW_OP_rot:
15415 case DW_OP_abs:
15416 case DW_OP_neg:
15417 case DW_OP_not:
15418 case DW_OP_plus_uconst:
15419 case DW_OP_skip:
15420 case DW_OP_reg0:
15421 case DW_OP_reg1:
15422 case DW_OP_reg2:
15423 case DW_OP_reg3:
15424 case DW_OP_reg4:
15425 case DW_OP_reg5:
15426 case DW_OP_reg6:
15427 case DW_OP_reg7:
15428 case DW_OP_reg8:
15429 case DW_OP_reg9:
15430 case DW_OP_reg10:
15431 case DW_OP_reg11:
15432 case DW_OP_reg12:
15433 case DW_OP_reg13:
15434 case DW_OP_reg14:
15435 case DW_OP_reg15:
15436 case DW_OP_reg16:
15437 case DW_OP_reg17:
15438 case DW_OP_reg18:
15439 case DW_OP_reg19:
15440 case DW_OP_reg20:
15441 case DW_OP_reg21:
15442 case DW_OP_reg22:
15443 case DW_OP_reg23:
15444 case DW_OP_reg24:
15445 case DW_OP_reg25:
15446 case DW_OP_reg26:
15447 case DW_OP_reg27:
15448 case DW_OP_reg28:
15449 case DW_OP_reg29:
15450 case DW_OP_reg30:
15451 case DW_OP_reg31:
15452 case DW_OP_bregx:
15453 case DW_OP_piece:
15454 case DW_OP_deref_size:
15455 case DW_OP_nop:
15456 case DW_OP_form_tls_address:
15457 case DW_OP_bit_piece:
15458 case DW_OP_implicit_value:
15459 case DW_OP_stack_value:
15460 break;
15462 case DW_OP_addr:
15463 case DW_OP_const1u:
15464 case DW_OP_const1s:
15465 case DW_OP_const2u:
15466 case DW_OP_const2s:
15467 case DW_OP_const4u:
15468 case DW_OP_const4s:
15469 case DW_OP_const8u:
15470 case DW_OP_const8s:
15471 case DW_OP_constu:
15472 case DW_OP_consts:
15473 case DW_OP_dup:
15474 case DW_OP_over:
15475 case DW_OP_pick:
15476 case DW_OP_lit0:
15477 case DW_OP_lit1:
15478 case DW_OP_lit2:
15479 case DW_OP_lit3:
15480 case DW_OP_lit4:
15481 case DW_OP_lit5:
15482 case DW_OP_lit6:
15483 case DW_OP_lit7:
15484 case DW_OP_lit8:
15485 case DW_OP_lit9:
15486 case DW_OP_lit10:
15487 case DW_OP_lit11:
15488 case DW_OP_lit12:
15489 case DW_OP_lit13:
15490 case DW_OP_lit14:
15491 case DW_OP_lit15:
15492 case DW_OP_lit16:
15493 case DW_OP_lit17:
15494 case DW_OP_lit18:
15495 case DW_OP_lit19:
15496 case DW_OP_lit20:
15497 case DW_OP_lit21:
15498 case DW_OP_lit22:
15499 case DW_OP_lit23:
15500 case DW_OP_lit24:
15501 case DW_OP_lit25:
15502 case DW_OP_lit26:
15503 case DW_OP_lit27:
15504 case DW_OP_lit28:
15505 case DW_OP_lit29:
15506 case DW_OP_lit30:
15507 case DW_OP_lit31:
15508 case DW_OP_breg0:
15509 case DW_OP_breg1:
15510 case DW_OP_breg2:
15511 case DW_OP_breg3:
15512 case DW_OP_breg4:
15513 case DW_OP_breg5:
15514 case DW_OP_breg6:
15515 case DW_OP_breg7:
15516 case DW_OP_breg8:
15517 case DW_OP_breg9:
15518 case DW_OP_breg10:
15519 case DW_OP_breg11:
15520 case DW_OP_breg12:
15521 case DW_OP_breg13:
15522 case DW_OP_breg14:
15523 case DW_OP_breg15:
15524 case DW_OP_breg16:
15525 case DW_OP_breg17:
15526 case DW_OP_breg18:
15527 case DW_OP_breg19:
15528 case DW_OP_breg20:
15529 case DW_OP_breg21:
15530 case DW_OP_breg22:
15531 case DW_OP_breg23:
15532 case DW_OP_breg24:
15533 case DW_OP_breg25:
15534 case DW_OP_breg26:
15535 case DW_OP_breg27:
15536 case DW_OP_breg28:
15537 case DW_OP_breg29:
15538 case DW_OP_breg30:
15539 case DW_OP_breg31:
15540 case DW_OP_fbreg:
15541 case DW_OP_push_object_address:
15542 case DW_OP_call_frame_cfa:
15543 ++frame_offset_;
15544 break;
15546 case DW_OP_drop:
15547 case DW_OP_xderef:
15548 case DW_OP_and:
15549 case DW_OP_div:
15550 case DW_OP_minus:
15551 case DW_OP_mod:
15552 case DW_OP_mul:
15553 case DW_OP_or:
15554 case DW_OP_plus:
15555 case DW_OP_shl:
15556 case DW_OP_shr:
15557 case DW_OP_shra:
15558 case DW_OP_xor:
15559 case DW_OP_bra:
15560 case DW_OP_eq:
15561 case DW_OP_ge:
15562 case DW_OP_gt:
15563 case DW_OP_le:
15564 case DW_OP_lt:
15565 case DW_OP_ne:
15566 case DW_OP_regx:
15567 case DW_OP_xderef_size:
15568 --frame_offset_;
15569 break;
15571 case DW_OP_call2:
15572 case DW_OP_call4:
15573 case DW_OP_call_ref:
15575 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15576 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15578 if (stack_usage == NULL)
15579 return false;
15580 frame_offset_ += *stack_usage;
15581 break;
15584 case DW_OP_GNU_push_tls_address:
15585 case DW_OP_GNU_uninit:
15586 case DW_OP_GNU_encoded_addr:
15587 case DW_OP_GNU_implicit_pointer:
15588 case DW_OP_GNU_entry_value:
15589 case DW_OP_GNU_const_type:
15590 case DW_OP_GNU_regval_type:
15591 case DW_OP_GNU_deref_type:
15592 case DW_OP_GNU_convert:
15593 case DW_OP_GNU_reinterpret:
15594 case DW_OP_GNU_parameter_ref:
15595 /* loc_list_from_tree will probably not output these operations for
15596 size functions, so assume they will not appear here. */
15597 /* Fall through... */
15599 default:
15600 gcc_unreachable ();
15603 /* Now, follow the control flow (except subroutine calls). */
15604 switch (l->dw_loc_opc)
15606 case DW_OP_bra:
15607 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15608 frame_offsets))
15609 return false;
15610 /* Fall through. */
15612 case DW_OP_skip:
15613 l = l->dw_loc_oprnd1.v.val_loc;
15614 break;
15616 case DW_OP_stack_value:
15617 return true;
15619 default:
15620 l = l->dw_loc_next;
15621 break;
15625 return true;
15628 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15629 operations) in order to resolve the operand of DW_OP_pick operations that
15630 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
15631 offset *before* LOC is executed. Return if all relocations were
15632 successful. */
15634 static bool
15635 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15636 struct dwarf_procedure_info *dpi)
15638 /* Associate to all visited operations the frame offset *before* evaluating
15639 this operation. */
15640 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
15642 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
15643 frame_offsets);
15646 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15647 Return NULL if it is not possible. */
15649 static dw_die_ref
15650 function_to_dwarf_procedure (tree fndecl)
15652 struct loc_descr_context ctx;
15653 struct dwarf_procedure_info dpi;
15654 dw_die_ref dwarf_proc_die;
15655 tree tree_body = DECL_SAVED_TREE (fndecl);
15656 dw_loc_descr_ref loc_body, epilogue;
15658 tree cursor;
15659 unsigned i;
15661 /* Do not generate multiple DWARF procedures for the same function
15662 declaration. */
15663 dwarf_proc_die = lookup_decl_die (fndecl);
15664 if (dwarf_proc_die != NULL)
15665 return dwarf_proc_die;
15667 /* DWARF procedures are available starting with the DWARFv3 standard. */
15668 if (dwarf_version < 3 && dwarf_strict)
15669 return NULL;
15671 /* We handle only functions for which we still have a body, that return a
15672 supported type and that takes arguments with supported types. Note that
15673 there is no point translating functions that return nothing. */
15674 if (tree_body == NULL_TREE
15675 || DECL_RESULT (fndecl) == NULL_TREE
15676 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15677 return NULL;
15679 for (cursor = DECL_ARGUMENTS (fndecl);
15680 cursor != NULL_TREE;
15681 cursor = TREE_CHAIN (cursor))
15682 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15683 return NULL;
15685 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
15686 if (TREE_CODE (tree_body) != RETURN_EXPR)
15687 return NULL;
15688 tree_body = TREE_OPERAND (tree_body, 0);
15689 if (TREE_CODE (tree_body) != MODIFY_EXPR
15690 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15691 return NULL;
15692 tree_body = TREE_OPERAND (tree_body, 1);
15694 /* Try to translate the body expression itself. Note that this will probably
15695 cause an infinite recursion if its call graph has a cycle. This is very
15696 unlikely for size functions, however, so don't bother with such things at
15697 the moment. */
15698 ctx.context_type = NULL_TREE;
15699 ctx.base_decl = NULL_TREE;
15700 ctx.dpi = &dpi;
15701 dpi.fndecl = fndecl;
15702 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15703 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15704 if (!loc_body)
15705 return NULL;
15707 /* After evaluating all operands in "loc_body", we should still have on the
15708 stack all arguments plus the desired function result (top of the stack).
15709 Generate code in order to keep only the result in our stack frame. */
15710 epilogue = NULL;
15711 for (i = 0; i < dpi.args_count; ++i)
15713 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15714 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15715 op_couple->dw_loc_next->dw_loc_next = epilogue;
15716 epilogue = op_couple;
15718 add_loc_descr (&loc_body, epilogue);
15719 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15720 return NULL;
15722 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15723 because they are considered useful. Now there is an epilogue, they are
15724 not anymore, so give it another try. */
15725 loc_descr_without_nops (loc_body);
15727 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15728 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
15729 though, given that size functions do not come from source, so they should
15730 not have a dedicated DW_TAG_subprogram DIE. */
15731 dwarf_proc_die
15732 = new_dwarf_proc_die (loc_body, fndecl,
15733 get_context_die (DECL_CONTEXT (fndecl)));
15735 /* The called DWARF procedure consumes one stack slot per argument and
15736 returns one stack slot. */
15737 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15739 return dwarf_proc_die;
15743 /* Generate Dwarf location list representing LOC.
15744 If WANT_ADDRESS is false, expression computing LOC will be computed
15745 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15746 if WANT_ADDRESS is 2, expression computing address useable in location
15747 will be returned (i.e. DW_OP_reg can be used
15748 to refer to register values).
15750 CONTEXT provides information to customize the location descriptions
15751 generation. Its context_type field specifies what type is implicitly
15752 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
15753 will not be generated.
15755 Its DPI field determines whether we are generating a DWARF expression for a
15756 DWARF procedure, so PARM_DECL references are processed specifically.
15758 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15759 and dpi fields were null. */
15761 static dw_loc_list_ref
15762 loc_list_from_tree_1 (tree loc, int want_address,
15763 const struct loc_descr_context *context)
15765 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15766 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15767 int have_address = 0;
15768 enum dwarf_location_atom op;
15770 /* ??? Most of the time we do not take proper care for sign/zero
15771 extending the values properly. Hopefully this won't be a real
15772 problem... */
15774 if (context != NULL
15775 && context->base_decl == loc
15776 && want_address == 0)
15778 if (dwarf_version >= 3 || !dwarf_strict)
15779 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15780 NULL, NULL, NULL);
15781 else
15782 return NULL;
15785 switch (TREE_CODE (loc))
15787 case ERROR_MARK:
15788 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15789 return 0;
15791 case PLACEHOLDER_EXPR:
15792 /* This case involves extracting fields from an object to determine the
15793 position of other fields. It is supposed to appear only as the first
15794 operand of COMPONENT_REF nodes and to reference precisely the type
15795 that the context allows. */
15796 if (context != NULL
15797 && TREE_TYPE (loc) == context->context_type
15798 && want_address >= 1)
15800 if (dwarf_version >= 3 || !dwarf_strict)
15802 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15803 have_address = 1;
15804 break;
15806 else
15807 return NULL;
15809 else
15810 expansion_failed (loc, NULL_RTX,
15811 "PLACEHOLDER_EXPR for an unexpected type");
15812 break;
15814 case CALL_EXPR:
15816 const int nargs = call_expr_nargs (loc);
15817 tree callee = get_callee_fndecl (loc);
15818 int i;
15819 dw_die_ref dwarf_proc;
15821 if (callee == NULL_TREE)
15822 goto call_expansion_failed;
15824 /* We handle only functions that return an integer. */
15825 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15826 goto call_expansion_failed;
15828 dwarf_proc = function_to_dwarf_procedure (callee);
15829 if (dwarf_proc == NULL)
15830 goto call_expansion_failed;
15832 /* Evaluate arguments right-to-left so that the first argument will
15833 be the top-most one on the stack. */
15834 for (i = nargs - 1; i >= 0; --i)
15836 dw_loc_descr_ref loc_descr
15837 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15838 context);
15840 if (loc_descr == NULL)
15841 goto call_expansion_failed;
15843 add_loc_descr (&ret, loc_descr);
15846 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15847 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15848 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15849 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15850 add_loc_descr (&ret, ret1);
15851 break;
15853 call_expansion_failed:
15854 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15855 /* There are no opcodes for these operations. */
15856 return 0;
15859 case PREINCREMENT_EXPR:
15860 case PREDECREMENT_EXPR:
15861 case POSTINCREMENT_EXPR:
15862 case POSTDECREMENT_EXPR:
15863 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15864 /* There are no opcodes for these operations. */
15865 return 0;
15867 case ADDR_EXPR:
15868 /* If we already want an address, see if there is INDIRECT_REF inside
15869 e.g. for &this->field. */
15870 if (want_address)
15872 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15873 (loc, want_address == 2, context);
15874 if (list_ret)
15875 have_address = 1;
15876 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15877 && (ret = cst_pool_loc_descr (loc)))
15878 have_address = 1;
15880 /* Otherwise, process the argument and look for the address. */
15881 if (!list_ret && !ret)
15882 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15883 else
15885 if (want_address)
15886 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15887 return NULL;
15889 break;
15891 case VAR_DECL:
15892 if (DECL_THREAD_LOCAL_P (loc))
15894 rtx rtl;
15895 enum dwarf_location_atom tls_op;
15896 enum dtprel_bool dtprel = dtprel_false;
15898 if (targetm.have_tls)
15900 /* If this is not defined, we have no way to emit the
15901 data. */
15902 if (!targetm.asm_out.output_dwarf_dtprel)
15903 return 0;
15905 /* The way DW_OP_GNU_push_tls_address is specified, we
15906 can only look up addresses of objects in the current
15907 module. We used DW_OP_addr as first op, but that's
15908 wrong, because DW_OP_addr is relocated by the debug
15909 info consumer, while DW_OP_GNU_push_tls_address
15910 operand shouldn't be. */
15911 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15912 return 0;
15913 dtprel = dtprel_true;
15914 tls_op = DW_OP_GNU_push_tls_address;
15916 else
15918 if (!targetm.emutls.debug_form_tls_address
15919 || !(dwarf_version >= 3 || !dwarf_strict))
15920 return 0;
15921 /* We stuffed the control variable into the DECL_VALUE_EXPR
15922 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15923 no longer appear in gimple code. We used the control
15924 variable in specific so that we could pick it up here. */
15925 loc = DECL_VALUE_EXPR (loc);
15926 tls_op = DW_OP_form_tls_address;
15929 rtl = rtl_for_decl_location (loc);
15930 if (rtl == NULL_RTX)
15931 return 0;
15933 if (!MEM_P (rtl))
15934 return 0;
15935 rtl = XEXP (rtl, 0);
15936 if (! CONSTANT_P (rtl))
15937 return 0;
15939 ret = new_addr_loc_descr (rtl, dtprel);
15940 ret1 = new_loc_descr (tls_op, 0, 0);
15941 add_loc_descr (&ret, ret1);
15943 have_address = 1;
15944 break;
15946 /* FALLTHRU */
15948 case PARM_DECL:
15949 if (context != NULL && context->dpi != NULL
15950 && DECL_CONTEXT (loc) == context->dpi->fndecl)
15952 /* We are generating code for a DWARF procedure and we want to access
15953 one of its arguments: find the appropriate argument offset and let
15954 the resolve_args_picking pass compute the offset that complies
15955 with the stack frame size. */
15956 unsigned i = 0;
15957 tree cursor;
15959 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15960 cursor != NULL_TREE && cursor != loc;
15961 cursor = TREE_CHAIN (cursor), ++i)
15963 /* If we are translating a DWARF procedure, all referenced parameters
15964 must belong to the current function. */
15965 gcc_assert (cursor != NULL_TREE);
15967 ret = new_loc_descr (DW_OP_pick, i, 0);
15968 ret->frame_offset_rel = 1;
15969 break;
15971 /* FALLTHRU */
15973 case RESULT_DECL:
15974 if (DECL_HAS_VALUE_EXPR_P (loc))
15975 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15976 want_address, context);
15977 /* FALLTHRU */
15979 case FUNCTION_DECL:
15981 rtx rtl;
15982 var_loc_list *loc_list = lookup_decl_loc (loc);
15984 if (loc_list && loc_list->first)
15986 list_ret = dw_loc_list (loc_list, loc, want_address);
15987 have_address = want_address != 0;
15988 break;
15990 rtl = rtl_for_decl_location (loc);
15991 if (rtl == NULL_RTX)
15993 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15994 return 0;
15996 else if (CONST_INT_P (rtl))
15998 HOST_WIDE_INT val = INTVAL (rtl);
15999 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16000 val &= GET_MODE_MASK (DECL_MODE (loc));
16001 ret = int_loc_descriptor (val);
16003 else if (GET_CODE (rtl) == CONST_STRING)
16005 expansion_failed (loc, NULL_RTX, "CONST_STRING");
16006 return 0;
16008 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16009 ret = new_addr_loc_descr (rtl, dtprel_false);
16010 else
16012 machine_mode mode, mem_mode;
16014 /* Certain constructs can only be represented at top-level. */
16015 if (want_address == 2)
16017 ret = loc_descriptor (rtl, VOIDmode,
16018 VAR_INIT_STATUS_INITIALIZED);
16019 have_address = 1;
16021 else
16023 mode = GET_MODE (rtl);
16024 mem_mode = VOIDmode;
16025 if (MEM_P (rtl))
16027 mem_mode = mode;
16028 mode = get_address_mode (rtl);
16029 rtl = XEXP (rtl, 0);
16030 have_address = 1;
16032 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16033 VAR_INIT_STATUS_INITIALIZED);
16035 if (!ret)
16036 expansion_failed (loc, rtl,
16037 "failed to produce loc descriptor for rtl");
16040 break;
16042 case MEM_REF:
16043 if (!integer_zerop (TREE_OPERAND (loc, 1)))
16045 have_address = 1;
16046 goto do_plus;
16048 /* Fallthru. */
16049 case INDIRECT_REF:
16050 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16051 have_address = 1;
16052 break;
16054 case TARGET_MEM_REF:
16055 case SSA_NAME:
16056 case DEBUG_EXPR_DECL:
16057 return NULL;
16059 case COMPOUND_EXPR:
16060 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16061 context);
16063 CASE_CONVERT:
16064 case VIEW_CONVERT_EXPR:
16065 case SAVE_EXPR:
16066 case MODIFY_EXPR:
16067 case NON_LVALUE_EXPR:
16068 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16069 context);
16071 case COMPONENT_REF:
16072 case BIT_FIELD_REF:
16073 case ARRAY_REF:
16074 case ARRAY_RANGE_REF:
16075 case REALPART_EXPR:
16076 case IMAGPART_EXPR:
16078 tree obj, offset;
16079 HOST_WIDE_INT bitsize, bitpos, bytepos;
16080 machine_mode mode;
16081 int unsignedp, reversep, volatilep = 0;
16083 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16084 &unsignedp, &reversep, &volatilep);
16086 gcc_assert (obj != loc);
16088 list_ret = loc_list_from_tree_1 (obj,
16089 want_address == 2
16090 && !bitpos && !offset ? 2 : 1,
16091 context);
16092 /* TODO: We can extract value of the small expression via shifting even
16093 for nonzero bitpos. */
16094 if (list_ret == 0)
16095 return 0;
16096 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16098 expansion_failed (loc, NULL_RTX,
16099 "bitfield access");
16100 return 0;
16103 if (offset != NULL_TREE)
16105 /* Variable offset. */
16106 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16107 if (list_ret1 == 0)
16108 return 0;
16109 add_loc_list (&list_ret, list_ret1);
16110 if (!list_ret)
16111 return 0;
16112 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16115 bytepos = bitpos / BITS_PER_UNIT;
16116 if (bytepos > 0)
16117 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16118 else if (bytepos < 0)
16119 loc_list_plus_const (list_ret, bytepos);
16121 have_address = 1;
16122 break;
16125 case INTEGER_CST:
16126 if ((want_address || !tree_fits_shwi_p (loc))
16127 && (ret = cst_pool_loc_descr (loc)))
16128 have_address = 1;
16129 else if (want_address == 2
16130 && tree_fits_shwi_p (loc)
16131 && (ret = address_of_int_loc_descriptor
16132 (int_size_in_bytes (TREE_TYPE (loc)),
16133 tree_to_shwi (loc))))
16134 have_address = 1;
16135 else if (tree_fits_shwi_p (loc))
16136 ret = int_loc_descriptor (tree_to_shwi (loc));
16137 else if (tree_fits_uhwi_p (loc))
16138 ret = uint_loc_descriptor (tree_to_uhwi (loc));
16139 else
16141 expansion_failed (loc, NULL_RTX,
16142 "Integer operand is not host integer");
16143 return 0;
16145 break;
16147 case CONSTRUCTOR:
16148 case REAL_CST:
16149 case STRING_CST:
16150 case COMPLEX_CST:
16151 if ((ret = cst_pool_loc_descr (loc)))
16152 have_address = 1;
16153 else if (TREE_CODE (loc) == CONSTRUCTOR)
16155 tree type = TREE_TYPE (loc);
16156 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
16157 unsigned HOST_WIDE_INT offset = 0;
16158 unsigned HOST_WIDE_INT cnt;
16159 constructor_elt *ce;
16161 if (TREE_CODE (type) == RECORD_TYPE)
16163 /* This is very limited, but it's enough to output
16164 pointers to member functions, as long as the
16165 referenced function is defined in the current
16166 translation unit. */
16167 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
16169 tree val = ce->value;
16171 tree field = ce->index;
16173 if (val)
16174 STRIP_NOPS (val);
16176 if (!field || DECL_BIT_FIELD (field))
16178 expansion_failed (loc, NULL_RTX,
16179 "bitfield in record type constructor");
16180 size = offset = (unsigned HOST_WIDE_INT)-1;
16181 ret = NULL;
16182 break;
16185 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16186 unsigned HOST_WIDE_INT pos = int_byte_position (field);
16187 gcc_assert (pos + fieldsize <= size);
16188 if (pos < offset)
16190 expansion_failed (loc, NULL_RTX,
16191 "out-of-order fields in record constructor");
16192 size = offset = (unsigned HOST_WIDE_INT)-1;
16193 ret = NULL;
16194 break;
16196 if (pos > offset)
16198 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
16199 add_loc_descr (&ret, ret1);
16200 offset = pos;
16202 if (val && fieldsize != 0)
16204 ret1 = loc_descriptor_from_tree (val, want_address, context);
16205 if (!ret1)
16207 expansion_failed (loc, NULL_RTX,
16208 "unsupported expression in field");
16209 size = offset = (unsigned HOST_WIDE_INT)-1;
16210 ret = NULL;
16211 break;
16213 add_loc_descr (&ret, ret1);
16215 if (fieldsize)
16217 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
16218 add_loc_descr (&ret, ret1);
16219 offset = pos + fieldsize;
16223 if (offset != size)
16225 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
16226 add_loc_descr (&ret, ret1);
16227 offset = size;
16230 have_address = !!want_address;
16232 else
16233 expansion_failed (loc, NULL_RTX,
16234 "constructor of non-record type");
16236 else
16237 /* We can construct small constants here using int_loc_descriptor. */
16238 expansion_failed (loc, NULL_RTX,
16239 "constructor or constant not in constant pool");
16240 break;
16242 case TRUTH_AND_EXPR:
16243 case TRUTH_ANDIF_EXPR:
16244 case BIT_AND_EXPR:
16245 op = DW_OP_and;
16246 goto do_binop;
16248 case TRUTH_XOR_EXPR:
16249 case BIT_XOR_EXPR:
16250 op = DW_OP_xor;
16251 goto do_binop;
16253 case TRUTH_OR_EXPR:
16254 case TRUTH_ORIF_EXPR:
16255 case BIT_IOR_EXPR:
16256 op = DW_OP_or;
16257 goto do_binop;
16259 case FLOOR_DIV_EXPR:
16260 case CEIL_DIV_EXPR:
16261 case ROUND_DIV_EXPR:
16262 case TRUNC_DIV_EXPR:
16263 case EXACT_DIV_EXPR:
16264 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16265 return 0;
16266 op = DW_OP_div;
16267 goto do_binop;
16269 case MINUS_EXPR:
16270 op = DW_OP_minus;
16271 goto do_binop;
16273 case FLOOR_MOD_EXPR:
16274 case CEIL_MOD_EXPR:
16275 case ROUND_MOD_EXPR:
16276 case TRUNC_MOD_EXPR:
16277 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16279 op = DW_OP_mod;
16280 goto do_binop;
16282 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16283 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16284 if (list_ret == 0 || list_ret1 == 0)
16285 return 0;
16287 add_loc_list (&list_ret, list_ret1);
16288 if (list_ret == 0)
16289 return 0;
16290 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16291 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16292 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16293 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16294 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16295 break;
16297 case MULT_EXPR:
16298 op = DW_OP_mul;
16299 goto do_binop;
16301 case LSHIFT_EXPR:
16302 op = DW_OP_shl;
16303 goto do_binop;
16305 case RSHIFT_EXPR:
16306 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16307 goto do_binop;
16309 case POINTER_PLUS_EXPR:
16310 case PLUS_EXPR:
16311 do_plus:
16312 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16314 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16315 smarter to encode their opposite. The DW_OP_plus_uconst operation
16316 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
16317 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16318 bytes, Y being the size of the operation that pushes the opposite
16319 of the addend. So let's choose the smallest representation. */
16320 const tree tree_addend = TREE_OPERAND (loc, 1);
16321 offset_int wi_addend;
16322 HOST_WIDE_INT shwi_addend;
16323 dw_loc_descr_ref loc_naddend;
16325 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16326 if (list_ret == 0)
16327 return 0;
16329 /* Try to get the literal to push. It is the opposite of the addend,
16330 so as we rely on wrapping during DWARF evaluation, first decode
16331 the literal as a "DWARF-sized" signed number. */
16332 wi_addend = wi::to_offset (tree_addend);
16333 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16334 shwi_addend = wi_addend.to_shwi ();
16335 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16336 ? int_loc_descriptor (-shwi_addend)
16337 : NULL;
16339 if (loc_naddend != NULL
16340 && ((unsigned) size_of_uleb128 (shwi_addend)
16341 > size_of_loc_descr (loc_naddend)))
16343 add_loc_descr_to_each (list_ret, loc_naddend);
16344 add_loc_descr_to_each (list_ret,
16345 new_loc_descr (DW_OP_minus, 0, 0));
16347 else
16349 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16351 loc_naddend = loc_cur;
16352 loc_cur = loc_cur->dw_loc_next;
16353 ggc_free (loc_naddend);
16355 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16357 break;
16360 op = DW_OP_plus;
16361 goto do_binop;
16363 case LE_EXPR:
16364 op = DW_OP_le;
16365 goto do_comp_binop;
16367 case GE_EXPR:
16368 op = DW_OP_ge;
16369 goto do_comp_binop;
16371 case LT_EXPR:
16372 op = DW_OP_lt;
16373 goto do_comp_binop;
16375 case GT_EXPR:
16376 op = DW_OP_gt;
16377 goto do_comp_binop;
16379 do_comp_binop:
16380 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16382 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16383 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16384 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16385 TREE_CODE (loc));
16386 break;
16388 else
16389 goto do_binop;
16391 case EQ_EXPR:
16392 op = DW_OP_eq;
16393 goto do_binop;
16395 case NE_EXPR:
16396 op = DW_OP_ne;
16397 goto do_binop;
16399 do_binop:
16400 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16401 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16402 if (list_ret == 0 || list_ret1 == 0)
16403 return 0;
16405 add_loc_list (&list_ret, list_ret1);
16406 if (list_ret == 0)
16407 return 0;
16408 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16409 break;
16411 case TRUTH_NOT_EXPR:
16412 case BIT_NOT_EXPR:
16413 op = DW_OP_not;
16414 goto do_unop;
16416 case ABS_EXPR:
16417 op = DW_OP_abs;
16418 goto do_unop;
16420 case NEGATE_EXPR:
16421 op = DW_OP_neg;
16422 goto do_unop;
16424 do_unop:
16425 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16426 if (list_ret == 0)
16427 return 0;
16429 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16430 break;
16432 case MIN_EXPR:
16433 case MAX_EXPR:
16435 const enum tree_code code =
16436 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16438 loc = build3 (COND_EXPR, TREE_TYPE (loc),
16439 build2 (code, integer_type_node,
16440 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16441 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16444 /* fall through */
16446 case COND_EXPR:
16448 dw_loc_descr_ref lhs
16449 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16450 dw_loc_list_ref rhs
16451 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16452 dw_loc_descr_ref bra_node, jump_node, tmp;
16454 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16455 if (list_ret == 0 || lhs == 0 || rhs == 0)
16456 return 0;
16458 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16459 add_loc_descr_to_each (list_ret, bra_node);
16461 add_loc_list (&list_ret, rhs);
16462 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16463 add_loc_descr_to_each (list_ret, jump_node);
16465 add_loc_descr_to_each (list_ret, lhs);
16466 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16467 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16469 /* ??? Need a node to point the skip at. Use a nop. */
16470 tmp = new_loc_descr (DW_OP_nop, 0, 0);
16471 add_loc_descr_to_each (list_ret, tmp);
16472 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16473 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16475 break;
16477 case FIX_TRUNC_EXPR:
16478 return 0;
16480 default:
16481 /* Leave front-end specific codes as simply unknown. This comes
16482 up, for instance, with the C STMT_EXPR. */
16483 if ((unsigned int) TREE_CODE (loc)
16484 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16486 expansion_failed (loc, NULL_RTX,
16487 "language specific tree node");
16488 return 0;
16491 /* Otherwise this is a generic code; we should just lists all of
16492 these explicitly. We forgot one. */
16493 if (flag_checking)
16494 gcc_unreachable ();
16496 /* In a release build, we want to degrade gracefully: better to
16497 generate incomplete debugging information than to crash. */
16498 return NULL;
16501 if (!ret && !list_ret)
16502 return 0;
16504 if (want_address == 2 && !have_address
16505 && (dwarf_version >= 4 || !dwarf_strict))
16507 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16509 expansion_failed (loc, NULL_RTX,
16510 "DWARF address size mismatch");
16511 return 0;
16513 if (ret)
16514 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16515 else
16516 add_loc_descr_to_each (list_ret,
16517 new_loc_descr (DW_OP_stack_value, 0, 0));
16518 have_address = 1;
16520 /* Show if we can't fill the request for an address. */
16521 if (want_address && !have_address)
16523 expansion_failed (loc, NULL_RTX,
16524 "Want address and only have value");
16525 return 0;
16528 gcc_assert (!ret || !list_ret);
16530 /* If we've got an address and don't want one, dereference. */
16531 if (!want_address && have_address)
16533 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16535 if (size > DWARF2_ADDR_SIZE || size == -1)
16537 expansion_failed (loc, NULL_RTX,
16538 "DWARF address size mismatch");
16539 return 0;
16541 else if (size == DWARF2_ADDR_SIZE)
16542 op = DW_OP_deref;
16543 else
16544 op = DW_OP_deref_size;
16546 if (ret)
16547 add_loc_descr (&ret, new_loc_descr (op, size, 0));
16548 else
16549 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16551 if (ret)
16552 list_ret = new_loc_list (ret, NULL, NULL, NULL);
16554 return list_ret;
16557 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16558 expressions. */
16560 static dw_loc_list_ref
16561 loc_list_from_tree (tree loc, int want_address,
16562 const struct loc_descr_context *context)
16564 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16566 for (dw_loc_list_ref loc_cur = result;
16567 loc_cur != NULL; loc_cur =
16568 loc_cur->dw_loc_next)
16569 loc_descr_without_nops (loc_cur->expr);
16570 return result;
16573 /* Same as above but return only single location expression. */
16574 static dw_loc_descr_ref
16575 loc_descriptor_from_tree (tree loc, int want_address,
16576 const struct loc_descr_context *context)
16578 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16579 if (!ret)
16580 return NULL;
16581 if (ret->dw_loc_next)
16583 expansion_failed (loc, NULL_RTX,
16584 "Location list where only loc descriptor needed");
16585 return NULL;
16587 return ret->expr;
16590 /* Given a value, round it up to the lowest multiple of `boundary'
16591 which is not less than the value itself. */
16593 static inline HOST_WIDE_INT
16594 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16596 return (((value + boundary - 1) / boundary) * boundary);
16599 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16600 pointer to the declared type for the relevant field variable, or return
16601 `integer_type_node' if the given node turns out to be an
16602 ERROR_MARK node. */
16604 static inline tree
16605 field_type (const_tree decl)
16607 tree type;
16609 if (TREE_CODE (decl) == ERROR_MARK)
16610 return integer_type_node;
16612 type = DECL_BIT_FIELD_TYPE (decl);
16613 if (type == NULL_TREE)
16614 type = TREE_TYPE (decl);
16616 return type;
16619 /* Given a pointer to a tree node, return the alignment in bits for
16620 it, or else return BITS_PER_WORD if the node actually turns out to
16621 be an ERROR_MARK node. */
16623 static inline unsigned
16624 simple_type_align_in_bits (const_tree type)
16626 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16629 static inline unsigned
16630 simple_decl_align_in_bits (const_tree decl)
16632 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16635 /* Return the result of rounding T up to ALIGN. */
16637 static inline offset_int
16638 round_up_to_align (const offset_int &t, unsigned int align)
16640 return wi::udiv_trunc (t + align - 1, align) * align;
16643 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
16644 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
16645 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
16646 if we fail to return the size in one of these two forms. */
16648 static dw_loc_descr_ref
16649 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16651 tree tree_size;
16652 struct loc_descr_context ctx;
16654 /* Return a constant integer in priority, if possible. */
16655 *cst_size = int_size_in_bytes (type);
16656 if (*cst_size != -1)
16657 return NULL;
16659 ctx.context_type = const_cast<tree> (type);
16660 ctx.base_decl = NULL_TREE;
16661 ctx.dpi = NULL;
16663 type = TYPE_MAIN_VARIANT (type);
16664 tree_size = TYPE_SIZE_UNIT (type);
16665 return ((tree_size != NULL_TREE)
16666 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16667 : NULL);
16670 /* Helper structure for RECORD_TYPE processing. */
16671 struct vlr_context
16673 /* Root RECORD_TYPE. It is needed to generate data member location
16674 descriptions in variable-length records (VLR), but also to cope with
16675 variants, which are composed of nested structures multiplexed with
16676 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
16677 function processing a FIELD_DECL, it is required to be non null. */
16678 tree struct_type;
16679 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16680 QUAL_UNION_TYPE), this holds an expression that computes the offset for
16681 this variant part as part of the root record (in storage units). For
16682 regular records, it must be NULL_TREE. */
16683 tree variant_part_offset;
16686 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16687 addressed byte of the "containing object" for the given FIELD_DECL. If
16688 possible, return a native constant through CST_OFFSET (in which case NULL is
16689 returned); otherwise return a DWARF expression that computes the offset.
16691 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16692 that offset is, either because the argument turns out to be a pointer to an
16693 ERROR_MARK node, or because the offset expression is too complex for us.
16695 CTX is required: see the comment for VLR_CONTEXT. */
16697 static dw_loc_descr_ref
16698 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16699 HOST_WIDE_INT *cst_offset)
16701 offset_int object_offset_in_bits;
16702 offset_int object_offset_in_bytes;
16703 offset_int bitpos_int;
16704 bool is_byte_offset_cst, is_bit_offset_cst;
16705 tree tree_result;
16706 dw_loc_list_ref loc_result;
16708 *cst_offset = 0;
16710 if (TREE_CODE (decl) == ERROR_MARK)
16711 return NULL;
16712 else
16713 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16715 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16716 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16718 /* We cannot handle variable bit offsets at the moment, so abort if it's the
16719 case. */
16720 if (is_bit_offset_cst)
16721 return NULL;
16723 #ifdef PCC_BITFIELD_TYPE_MATTERS
16724 /* We used to handle only constant offsets in all cases. Now, we handle
16725 properly dynamic byte offsets only when PCC bitfield type doesn't
16726 matter. */
16727 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16729 tree type;
16730 tree field_size_tree;
16731 offset_int deepest_bitpos;
16732 offset_int field_size_in_bits;
16733 unsigned int type_align_in_bits;
16734 unsigned int decl_align_in_bits;
16735 offset_int type_size_in_bits;
16737 bitpos_int = wi::to_offset (bit_position (decl));
16738 type = field_type (decl);
16739 type_size_in_bits = offset_int_type_size_in_bits (type);
16740 type_align_in_bits = simple_type_align_in_bits (type);
16742 field_size_tree = DECL_SIZE (decl);
16744 /* The size could be unspecified if there was an error, or for
16745 a flexible array member. */
16746 if (!field_size_tree)
16747 field_size_tree = bitsize_zero_node;
16749 /* If the size of the field is not constant, use the type size. */
16750 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16751 field_size_in_bits = wi::to_offset (field_size_tree);
16752 else
16753 field_size_in_bits = type_size_in_bits;
16755 decl_align_in_bits = simple_decl_align_in_bits (decl);
16757 /* The GCC front-end doesn't make any attempt to keep track of the
16758 starting bit offset (relative to the start of the containing
16759 structure type) of the hypothetical "containing object" for a
16760 bit-field. Thus, when computing the byte offset value for the
16761 start of the "containing object" of a bit-field, we must deduce
16762 this information on our own. This can be rather tricky to do in
16763 some cases. For example, handling the following structure type
16764 definition when compiling for an i386/i486 target (which only
16765 aligns long long's to 32-bit boundaries) can be very tricky:
16767 struct S { int field1; long long field2:31; };
16769 Fortunately, there is a simple rule-of-thumb which can be used
16770 in such cases. When compiling for an i386/i486, GCC will
16771 allocate 8 bytes for the structure shown above. It decides to
16772 do this based upon one simple rule for bit-field allocation.
16773 GCC allocates each "containing object" for each bit-field at
16774 the first (i.e. lowest addressed) legitimate alignment boundary
16775 (based upon the required minimum alignment for the declared
16776 type of the field) which it can possibly use, subject to the
16777 condition that there is still enough available space remaining
16778 in the containing object (when allocated at the selected point)
16779 to fully accommodate all of the bits of the bit-field itself.
16781 This simple rule makes it obvious why GCC allocates 8 bytes for
16782 each object of the structure type shown above. When looking
16783 for a place to allocate the "containing object" for `field2',
16784 the compiler simply tries to allocate a 64-bit "containing
16785 object" at each successive 32-bit boundary (starting at zero)
16786 until it finds a place to allocate that 64- bit field such that
16787 at least 31 contiguous (and previously unallocated) bits remain
16788 within that selected 64 bit field. (As it turns out, for the
16789 example above, the compiler finds it is OK to allocate the
16790 "containing object" 64-bit field at bit-offset zero within the
16791 structure type.)
16793 Here we attempt to work backwards from the limited set of facts
16794 we're given, and we try to deduce from those facts, where GCC
16795 must have believed that the containing object started (within
16796 the structure type). The value we deduce is then used (by the
16797 callers of this routine) to generate DW_AT_location and
16798 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16799 the case of DW_AT_location, regular fields as well). */
16801 /* Figure out the bit-distance from the start of the structure to
16802 the "deepest" bit of the bit-field. */
16803 deepest_bitpos = bitpos_int + field_size_in_bits;
16805 /* This is the tricky part. Use some fancy footwork to deduce
16806 where the lowest addressed bit of the containing object must
16807 be. */
16808 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16810 /* Round up to type_align by default. This works best for
16811 bitfields. */
16812 object_offset_in_bits
16813 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16815 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16817 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16819 /* Round up to decl_align instead. */
16820 object_offset_in_bits
16821 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16824 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16826 tree_result = byte_position (decl);
16827 if (ctx->variant_part_offset != NULL_TREE)
16828 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16829 ctx->variant_part_offset, tree_result));
16831 /* If the byte offset is a constant, it's simplier to handle a native
16832 constant rather than a DWARF expression. */
16833 if (TREE_CODE (tree_result) == INTEGER_CST)
16835 *cst_offset = wi::to_offset (tree_result).to_shwi ();
16836 return NULL;
16838 struct loc_descr_context loc_ctx = {
16839 ctx->struct_type, /* context_type */
16840 NULL_TREE, /* base_decl */
16841 NULL /* dpi */
16843 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16845 /* We want a DWARF expression: abort if we only have a location list with
16846 multiple elements. */
16847 if (!loc_result || !single_element_loc_list_p (loc_result))
16848 return NULL;
16849 else
16850 return loc_result->expr;
16853 /* The following routines define various Dwarf attributes and any data
16854 associated with them. */
16856 /* Add a location description attribute value to a DIE.
16858 This emits location attributes suitable for whole variables and
16859 whole parameters. Note that the location attributes for struct fields are
16860 generated by the routine `data_member_location_attribute' below. */
16862 static inline void
16863 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16864 dw_loc_list_ref descr)
16866 if (descr == 0)
16867 return;
16868 if (single_element_loc_list_p (descr))
16869 add_AT_loc (die, attr_kind, descr->expr);
16870 else
16871 add_AT_loc_list (die, attr_kind, descr);
16874 /* Add DW_AT_accessibility attribute to DIE if needed. */
16876 static void
16877 add_accessibility_attribute (dw_die_ref die, tree decl)
16879 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16880 children, otherwise the default is DW_ACCESS_public. In DWARF2
16881 the default has always been DW_ACCESS_public. */
16882 if (TREE_PROTECTED (decl))
16883 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16884 else if (TREE_PRIVATE (decl))
16886 if (dwarf_version == 2
16887 || die->die_parent == NULL
16888 || die->die_parent->die_tag != DW_TAG_class_type)
16889 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16891 else if (dwarf_version > 2
16892 && die->die_parent
16893 && die->die_parent->die_tag == DW_TAG_class_type)
16894 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16897 /* Attach the specialized form of location attribute used for data members of
16898 struct and union types. In the special case of a FIELD_DECL node which
16899 represents a bit-field, the "offset" part of this special location
16900 descriptor must indicate the distance in bytes from the lowest-addressed
16901 byte of the containing struct or union type to the lowest-addressed byte of
16902 the "containing object" for the bit-field. (See the `field_byte_offset'
16903 function above).
16905 For any given bit-field, the "containing object" is a hypothetical object
16906 (of some integral or enum type) within which the given bit-field lives. The
16907 type of this hypothetical "containing object" is always the same as the
16908 declared type of the individual bit-field itself (for GCC anyway... the
16909 DWARF spec doesn't actually mandate this). Note that it is the size (in
16910 bytes) of the hypothetical "containing object" which will be given in the
16911 DW_AT_byte_size attribute for this bit-field. (See the
16912 `byte_size_attribute' function below.) It is also used when calculating the
16913 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16914 function below.)
16916 CTX is required: see the comment for VLR_CONTEXT. */
16918 static void
16919 add_data_member_location_attribute (dw_die_ref die,
16920 tree decl,
16921 struct vlr_context *ctx)
16923 HOST_WIDE_INT offset;
16924 dw_loc_descr_ref loc_descr = 0;
16926 if (TREE_CODE (decl) == TREE_BINFO)
16928 /* We're working on the TAG_inheritance for a base class. */
16929 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16931 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16932 aren't at a fixed offset from all (sub)objects of the same
16933 type. We need to extract the appropriate offset from our
16934 vtable. The following dwarf expression means
16936 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16938 This is specific to the V3 ABI, of course. */
16940 dw_loc_descr_ref tmp;
16942 /* Make a copy of the object address. */
16943 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16944 add_loc_descr (&loc_descr, tmp);
16946 /* Extract the vtable address. */
16947 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16948 add_loc_descr (&loc_descr, tmp);
16950 /* Calculate the address of the offset. */
16951 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16952 gcc_assert (offset < 0);
16954 tmp = int_loc_descriptor (-offset);
16955 add_loc_descr (&loc_descr, tmp);
16956 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16957 add_loc_descr (&loc_descr, tmp);
16959 /* Extract the offset. */
16960 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16961 add_loc_descr (&loc_descr, tmp);
16963 /* Add it to the object address. */
16964 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16965 add_loc_descr (&loc_descr, tmp);
16967 else
16968 offset = tree_to_shwi (BINFO_OFFSET (decl));
16970 else
16972 loc_descr = field_byte_offset (decl, ctx, &offset);
16974 /* If loc_descr is available then we know the field offset is dynamic.
16975 However, GDB does not handle dynamic field offsets very well at the
16976 moment. */
16977 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16979 loc_descr = NULL;
16980 offset = 0;
16983 /* Data member location evalutation starts with the base address on the
16984 stack. Compute the field offset and add it to this base address. */
16985 else if (loc_descr != NULL)
16986 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
16989 if (! loc_descr)
16991 if (dwarf_version > 2)
16993 /* Don't need to output a location expression, just the constant. */
16994 if (offset < 0)
16995 add_AT_int (die, DW_AT_data_member_location, offset);
16996 else
16997 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16998 return;
17000 else
17002 enum dwarf_location_atom op;
17004 /* The DWARF2 standard says that we should assume that the structure
17005 address is already on the stack, so we can specify a structure
17006 field address by using DW_OP_plus_uconst. */
17007 op = DW_OP_plus_uconst;
17008 loc_descr = new_loc_descr (op, offset, 0);
17012 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17015 /* Writes integer values to dw_vec_const array. */
17017 static void
17018 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17020 while (size != 0)
17022 *dest++ = val & 0xff;
17023 val >>= 8;
17024 --size;
17028 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
17030 static HOST_WIDE_INT
17031 extract_int (const unsigned char *src, unsigned int size)
17033 HOST_WIDE_INT val = 0;
17035 src += size;
17036 while (size != 0)
17038 val <<= 8;
17039 val |= *--src & 0xff;
17040 --size;
17042 return val;
17045 /* Writes wide_int values to dw_vec_const array. */
17047 static void
17048 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
17050 int i;
17052 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
17054 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
17055 return;
17058 /* We'd have to extend this code to support odd sizes. */
17059 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
17061 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
17063 if (WORDS_BIG_ENDIAN)
17064 for (i = n - 1; i >= 0; i--)
17066 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17067 dest += sizeof (HOST_WIDE_INT);
17069 else
17070 for (i = 0; i < n; i++)
17072 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17073 dest += sizeof (HOST_WIDE_INT);
17077 /* Writes floating point values to dw_vec_const array. */
17079 static void
17080 insert_float (const_rtx rtl, unsigned char *array)
17082 long val[4];
17083 int i;
17085 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
17087 /* real_to_target puts 32-bit pieces in each long. Pack them. */
17088 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17090 insert_int (val[i], 4, array);
17091 array += 4;
17095 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17096 does not have a "location" either in memory or in a register. These
17097 things can arise in GNU C when a constant is passed as an actual parameter
17098 to an inlined function. They can also arise in C++ where declared
17099 constants do not necessarily get memory "homes". */
17101 static bool
17102 add_const_value_attribute (dw_die_ref die, rtx rtl)
17104 switch (GET_CODE (rtl))
17106 case CONST_INT:
17108 HOST_WIDE_INT val = INTVAL (rtl);
17110 if (val < 0)
17111 add_AT_int (die, DW_AT_const_value, val);
17112 else
17113 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17115 return true;
17117 case CONST_WIDE_INT:
17119 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17120 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17121 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17122 wide_int w = wi::zext (w1, prec);
17123 add_AT_wide (die, DW_AT_const_value, w);
17125 return true;
17127 case CONST_DOUBLE:
17128 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17129 floating-point constant. A CONST_DOUBLE is used whenever the
17130 constant requires more than one word in order to be adequately
17131 represented. */
17133 machine_mode mode = GET_MODE (rtl);
17135 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17136 add_AT_double (die, DW_AT_const_value,
17137 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17138 else
17140 unsigned int length = GET_MODE_SIZE (mode);
17141 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17143 insert_float (rtl, array);
17144 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17147 return true;
17149 case CONST_VECTOR:
17151 machine_mode mode = GET_MODE (rtl);
17152 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17153 unsigned int length = CONST_VECTOR_NUNITS (rtl);
17154 unsigned char *array
17155 = ggc_vec_alloc<unsigned char> (length * elt_size);
17156 unsigned int i;
17157 unsigned char *p;
17158 machine_mode imode = GET_MODE_INNER (mode);
17160 switch (GET_MODE_CLASS (mode))
17162 case MODE_VECTOR_INT:
17163 for (i = 0, p = array; i < length; i++, p += elt_size)
17165 rtx elt = CONST_VECTOR_ELT (rtl, i);
17166 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17168 break;
17170 case MODE_VECTOR_FLOAT:
17171 for (i = 0, p = array; i < length; i++, p += elt_size)
17173 rtx elt = CONST_VECTOR_ELT (rtl, i);
17174 insert_float (elt, p);
17176 break;
17178 default:
17179 gcc_unreachable ();
17182 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17184 return true;
17186 case CONST_STRING:
17187 if (dwarf_version >= 4 || !dwarf_strict)
17189 dw_loc_descr_ref loc_result;
17190 resolve_one_addr (&rtl);
17191 rtl_addr:
17192 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17193 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17194 add_AT_loc (die, DW_AT_location, loc_result);
17195 vec_safe_push (used_rtx_array, rtl);
17196 return true;
17198 return false;
17200 case CONST:
17201 if (CONSTANT_P (XEXP (rtl, 0)))
17202 return add_const_value_attribute (die, XEXP (rtl, 0));
17203 /* FALLTHROUGH */
17204 case SYMBOL_REF:
17205 if (!const_ok_for_output (rtl))
17206 return false;
17207 case LABEL_REF:
17208 if (dwarf_version >= 4 || !dwarf_strict)
17209 goto rtl_addr;
17210 return false;
17212 case PLUS:
17213 /* In cases where an inlined instance of an inline function is passed
17214 the address of an `auto' variable (which is local to the caller) we
17215 can get a situation where the DECL_RTL of the artificial local
17216 variable (for the inlining) which acts as a stand-in for the
17217 corresponding formal parameter (of the inline function) will look
17218 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
17219 exactly a compile-time constant expression, but it isn't the address
17220 of the (artificial) local variable either. Rather, it represents the
17221 *value* which the artificial local variable always has during its
17222 lifetime. We currently have no way to represent such quasi-constant
17223 values in Dwarf, so for now we just punt and generate nothing. */
17224 return false;
17226 case HIGH:
17227 case CONST_FIXED:
17228 return false;
17230 case MEM:
17231 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17232 && MEM_READONLY_P (rtl)
17233 && GET_MODE (rtl) == BLKmode)
17235 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17236 return true;
17238 return false;
17240 default:
17241 /* No other kinds of rtx should be possible here. */
17242 gcc_unreachable ();
17244 return false;
17247 /* Determine whether the evaluation of EXPR references any variables
17248 or functions which aren't otherwise used (and therefore may not be
17249 output). */
17250 static tree
17251 reference_to_unused (tree * tp, int * walk_subtrees,
17252 void * data ATTRIBUTE_UNUSED)
17254 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17255 *walk_subtrees = 0;
17257 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17258 && ! TREE_ASM_WRITTEN (*tp))
17259 return *tp;
17260 /* ??? The C++ FE emits debug information for using decls, so
17261 putting gcc_unreachable here falls over. See PR31899. For now
17262 be conservative. */
17263 else if (!symtab->global_info_ready
17264 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17265 return *tp;
17266 else if (TREE_CODE (*tp) == VAR_DECL)
17268 varpool_node *node = varpool_node::get (*tp);
17269 if (!node || !node->definition)
17270 return *tp;
17272 else if (TREE_CODE (*tp) == FUNCTION_DECL
17273 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17275 /* The call graph machinery must have finished analyzing,
17276 optimizing and gimplifying the CU by now.
17277 So if *TP has no call graph node associated
17278 to it, it means *TP will not be emitted. */
17279 if (!cgraph_node::get (*tp))
17280 return *tp;
17282 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17283 return *tp;
17285 return NULL_TREE;
17288 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17289 for use in a later add_const_value_attribute call. */
17291 static rtx
17292 rtl_for_decl_init (tree init, tree type)
17294 rtx rtl = NULL_RTX;
17296 STRIP_NOPS (init);
17298 /* If a variable is initialized with a string constant without embedded
17299 zeros, build CONST_STRING. */
17300 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17302 tree enttype = TREE_TYPE (type);
17303 tree domain = TYPE_DOMAIN (type);
17304 machine_mode mode = TYPE_MODE (enttype);
17306 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17307 && domain
17308 && integer_zerop (TYPE_MIN_VALUE (domain))
17309 && compare_tree_int (TYPE_MAX_VALUE (domain),
17310 TREE_STRING_LENGTH (init) - 1) == 0
17311 && ((size_t) TREE_STRING_LENGTH (init)
17312 == strlen (TREE_STRING_POINTER (init)) + 1))
17314 rtl = gen_rtx_CONST_STRING (VOIDmode,
17315 ggc_strdup (TREE_STRING_POINTER (init)));
17316 rtl = gen_rtx_MEM (BLKmode, rtl);
17317 MEM_READONLY_P (rtl) = 1;
17320 /* Other aggregates, and complex values, could be represented using
17321 CONCAT: FIXME! */
17322 else if (AGGREGATE_TYPE_P (type)
17323 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17324 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17325 || TREE_CODE (type) == COMPLEX_TYPE)
17327 /* Vectors only work if their mode is supported by the target.
17328 FIXME: generic vectors ought to work too. */
17329 else if (TREE_CODE (type) == VECTOR_TYPE
17330 && !VECTOR_MODE_P (TYPE_MODE (type)))
17332 /* If the initializer is something that we know will expand into an
17333 immediate RTL constant, expand it now. We must be careful not to
17334 reference variables which won't be output. */
17335 else if (initializer_constant_valid_p (init, type)
17336 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17338 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17339 possible. */
17340 if (TREE_CODE (type) == VECTOR_TYPE)
17341 switch (TREE_CODE (init))
17343 case VECTOR_CST:
17344 break;
17345 case CONSTRUCTOR:
17346 if (TREE_CONSTANT (init))
17348 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17349 bool constant_p = true;
17350 tree value;
17351 unsigned HOST_WIDE_INT ix;
17353 /* Even when ctor is constant, it might contain non-*_CST
17354 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17355 belong into VECTOR_CST nodes. */
17356 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17357 if (!CONSTANT_CLASS_P (value))
17359 constant_p = false;
17360 break;
17363 if (constant_p)
17365 init = build_vector_from_ctor (type, elts);
17366 break;
17369 /* FALLTHRU */
17371 default:
17372 return NULL;
17375 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17377 /* If expand_expr returns a MEM, it wasn't immediate. */
17378 gcc_assert (!rtl || !MEM_P (rtl));
17381 return rtl;
17384 /* Generate RTL for the variable DECL to represent its location. */
17386 static rtx
17387 rtl_for_decl_location (tree decl)
17389 rtx rtl;
17391 /* Here we have to decide where we are going to say the parameter "lives"
17392 (as far as the debugger is concerned). We only have a couple of
17393 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17395 DECL_RTL normally indicates where the parameter lives during most of the
17396 activation of the function. If optimization is enabled however, this
17397 could be either NULL or else a pseudo-reg. Both of those cases indicate
17398 that the parameter doesn't really live anywhere (as far as the code
17399 generation parts of GCC are concerned) during most of the function's
17400 activation. That will happen (for example) if the parameter is never
17401 referenced within the function.
17403 We could just generate a location descriptor here for all non-NULL
17404 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17405 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17406 where DECL_RTL is NULL or is a pseudo-reg.
17408 Note however that we can only get away with using DECL_INCOMING_RTL as
17409 a backup substitute for DECL_RTL in certain limited cases. In cases
17410 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17411 we can be sure that the parameter was passed using the same type as it is
17412 declared to have within the function, and that its DECL_INCOMING_RTL
17413 points us to a place where a value of that type is passed.
17415 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17416 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17417 because in these cases DECL_INCOMING_RTL points us to a value of some
17418 type which is *different* from the type of the parameter itself. Thus,
17419 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17420 such cases, the debugger would end up (for example) trying to fetch a
17421 `float' from a place which actually contains the first part of a
17422 `double'. That would lead to really incorrect and confusing
17423 output at debug-time.
17425 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17426 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
17427 are a couple of exceptions however. On little-endian machines we can
17428 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17429 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17430 an integral type that is smaller than TREE_TYPE (decl). These cases arise
17431 when (on a little-endian machine) a non-prototyped function has a
17432 parameter declared to be of type `short' or `char'. In such cases,
17433 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17434 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17435 passed `int' value. If the debugger then uses that address to fetch
17436 a `short' or a `char' (on a little-endian machine) the result will be
17437 the correct data, so we allow for such exceptional cases below.
17439 Note that our goal here is to describe the place where the given formal
17440 parameter lives during most of the function's activation (i.e. between the
17441 end of the prologue and the start of the epilogue). We'll do that as best
17442 as we can. Note however that if the given formal parameter is modified
17443 sometime during the execution of the function, then a stack backtrace (at
17444 debug-time) will show the function as having been called with the *new*
17445 value rather than the value which was originally passed in. This happens
17446 rarely enough that it is not a major problem, but it *is* a problem, and
17447 I'd like to fix it.
17449 A future version of dwarf2out.c may generate two additional attributes for
17450 any given DW_TAG_formal_parameter DIE which will describe the "passed
17451 type" and the "passed location" for the given formal parameter in addition
17452 to the attributes we now generate to indicate the "declared type" and the
17453 "active location" for each parameter. This additional set of attributes
17454 could be used by debuggers for stack backtraces. Separately, note that
17455 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17456 This happens (for example) for inlined-instances of inline function formal
17457 parameters which are never referenced. This really shouldn't be
17458 happening. All PARM_DECL nodes should get valid non-NULL
17459 DECL_INCOMING_RTL values. FIXME. */
17461 /* Use DECL_RTL as the "location" unless we find something better. */
17462 rtl = DECL_RTL_IF_SET (decl);
17464 /* When generating abstract instances, ignore everything except
17465 constants, symbols living in memory, and symbols living in
17466 fixed registers. */
17467 if (! reload_completed)
17469 if (rtl
17470 && (CONSTANT_P (rtl)
17471 || (MEM_P (rtl)
17472 && CONSTANT_P (XEXP (rtl, 0)))
17473 || (REG_P (rtl)
17474 && TREE_CODE (decl) == VAR_DECL
17475 && TREE_STATIC (decl))))
17477 rtl = targetm.delegitimize_address (rtl);
17478 return rtl;
17480 rtl = NULL_RTX;
17482 else if (TREE_CODE (decl) == PARM_DECL)
17484 if (rtl == NULL_RTX
17485 || is_pseudo_reg (rtl)
17486 || (MEM_P (rtl)
17487 && is_pseudo_reg (XEXP (rtl, 0))
17488 && DECL_INCOMING_RTL (decl)
17489 && MEM_P (DECL_INCOMING_RTL (decl))
17490 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17492 tree declared_type = TREE_TYPE (decl);
17493 tree passed_type = DECL_ARG_TYPE (decl);
17494 machine_mode dmode = TYPE_MODE (declared_type);
17495 machine_mode pmode = TYPE_MODE (passed_type);
17497 /* This decl represents a formal parameter which was optimized out.
17498 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17499 all cases where (rtl == NULL_RTX) just below. */
17500 if (dmode == pmode)
17501 rtl = DECL_INCOMING_RTL (decl);
17502 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17503 && SCALAR_INT_MODE_P (dmode)
17504 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17505 && DECL_INCOMING_RTL (decl))
17507 rtx inc = DECL_INCOMING_RTL (decl);
17508 if (REG_P (inc))
17509 rtl = inc;
17510 else if (MEM_P (inc))
17512 if (BYTES_BIG_ENDIAN)
17513 rtl = adjust_address_nv (inc, dmode,
17514 GET_MODE_SIZE (pmode)
17515 - GET_MODE_SIZE (dmode));
17516 else
17517 rtl = inc;
17522 /* If the parm was passed in registers, but lives on the stack, then
17523 make a big endian correction if the mode of the type of the
17524 parameter is not the same as the mode of the rtl. */
17525 /* ??? This is the same series of checks that are made in dbxout.c before
17526 we reach the big endian correction code there. It isn't clear if all
17527 of these checks are necessary here, but keeping them all is the safe
17528 thing to do. */
17529 else if (MEM_P (rtl)
17530 && XEXP (rtl, 0) != const0_rtx
17531 && ! CONSTANT_P (XEXP (rtl, 0))
17532 /* Not passed in memory. */
17533 && !MEM_P (DECL_INCOMING_RTL (decl))
17534 /* Not passed by invisible reference. */
17535 && (!REG_P (XEXP (rtl, 0))
17536 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17537 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17538 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17539 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17540 #endif
17542 /* Big endian correction check. */
17543 && BYTES_BIG_ENDIAN
17544 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17545 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17546 < UNITS_PER_WORD))
17548 machine_mode addr_mode = get_address_mode (rtl);
17549 int offset = (UNITS_PER_WORD
17550 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17552 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17553 plus_constant (addr_mode, XEXP (rtl, 0), offset));
17556 else if (TREE_CODE (decl) == VAR_DECL
17557 && rtl
17558 && MEM_P (rtl)
17559 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17560 && BYTES_BIG_ENDIAN)
17562 machine_mode addr_mode = get_address_mode (rtl);
17563 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17564 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17566 /* If a variable is declared "register" yet is smaller than
17567 a register, then if we store the variable to memory, it
17568 looks like we're storing a register-sized value, when in
17569 fact we are not. We need to adjust the offset of the
17570 storage location to reflect the actual value's bytes,
17571 else gdb will not be able to display it. */
17572 if (rsize > dsize)
17573 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17574 plus_constant (addr_mode, XEXP (rtl, 0),
17575 rsize - dsize));
17578 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17579 and will have been substituted directly into all expressions that use it.
17580 C does not have such a concept, but C++ and other languages do. */
17581 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17582 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17584 if (rtl)
17585 rtl = targetm.delegitimize_address (rtl);
17587 /* If we don't look past the constant pool, we risk emitting a
17588 reference to a constant pool entry that isn't referenced from
17589 code, and thus is not emitted. */
17590 if (rtl)
17591 rtl = avoid_constant_pool_reference (rtl);
17593 /* Try harder to get a rtl. If this symbol ends up not being emitted
17594 in the current CU, resolve_addr will remove the expression referencing
17595 it. */
17596 if (rtl == NULL_RTX
17597 && TREE_CODE (decl) == VAR_DECL
17598 && !DECL_EXTERNAL (decl)
17599 && TREE_STATIC (decl)
17600 && DECL_NAME (decl)
17601 && !DECL_HARD_REGISTER (decl)
17602 && DECL_MODE (decl) != VOIDmode)
17604 rtl = make_decl_rtl_for_debug (decl);
17605 if (!MEM_P (rtl)
17606 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17607 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17608 rtl = NULL_RTX;
17611 return rtl;
17614 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
17615 returned. If so, the decl for the COMMON block is returned, and the
17616 value is the offset into the common block for the symbol. */
17618 static tree
17619 fortran_common (tree decl, HOST_WIDE_INT *value)
17621 tree val_expr, cvar;
17622 machine_mode mode;
17623 HOST_WIDE_INT bitsize, bitpos;
17624 tree offset;
17625 int unsignedp, reversep, volatilep = 0;
17627 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17628 it does not have a value (the offset into the common area), or if it
17629 is thread local (as opposed to global) then it isn't common, and shouldn't
17630 be handled as such. */
17631 if (TREE_CODE (decl) != VAR_DECL
17632 || !TREE_STATIC (decl)
17633 || !DECL_HAS_VALUE_EXPR_P (decl)
17634 || !is_fortran ())
17635 return NULL_TREE;
17637 val_expr = DECL_VALUE_EXPR (decl);
17638 if (TREE_CODE (val_expr) != COMPONENT_REF)
17639 return NULL_TREE;
17641 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17642 &unsignedp, &reversep, &volatilep);
17644 if (cvar == NULL_TREE
17645 || TREE_CODE (cvar) != VAR_DECL
17646 || DECL_ARTIFICIAL (cvar)
17647 || !TREE_PUBLIC (cvar))
17648 return NULL_TREE;
17650 *value = 0;
17651 if (offset != NULL)
17653 if (!tree_fits_shwi_p (offset))
17654 return NULL_TREE;
17655 *value = tree_to_shwi (offset);
17657 if (bitpos != 0)
17658 *value += bitpos / BITS_PER_UNIT;
17660 return cvar;
17663 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17664 data attribute for a variable or a parameter. We generate the
17665 DW_AT_const_value attribute only in those cases where the given variable
17666 or parameter does not have a true "location" either in memory or in a
17667 register. This can happen (for example) when a constant is passed as an
17668 actual argument in a call to an inline function. (It's possible that
17669 these things can crop up in other ways also.) Note that one type of
17670 constant value which can be passed into an inlined function is a constant
17671 pointer. This can happen for example if an actual argument in an inlined
17672 function call evaluates to a compile-time constant address.
17674 CACHE_P is true if it is worth caching the location list for DECL,
17675 so that future calls can reuse it rather than regenerate it from scratch.
17676 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17677 since we will need to refer to them each time the function is inlined. */
17679 static bool
17680 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17682 rtx rtl;
17683 dw_loc_list_ref list;
17684 var_loc_list *loc_list;
17685 cached_dw_loc_list *cache;
17687 if (early_dwarf)
17688 return false;
17690 if (TREE_CODE (decl) == ERROR_MARK)
17691 return false;
17693 if (get_AT (die, DW_AT_location)
17694 || get_AT (die, DW_AT_const_value))
17695 return true;
17697 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17698 || TREE_CODE (decl) == RESULT_DECL);
17700 /* Try to get some constant RTL for this decl, and use that as the value of
17701 the location. */
17703 rtl = rtl_for_decl_location (decl);
17704 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17705 && add_const_value_attribute (die, rtl))
17706 return true;
17708 /* See if we have single element location list that is equivalent to
17709 a constant value. That way we are better to use add_const_value_attribute
17710 rather than expanding constant value equivalent. */
17711 loc_list = lookup_decl_loc (decl);
17712 if (loc_list
17713 && loc_list->first
17714 && loc_list->first->next == NULL
17715 && NOTE_P (loc_list->first->loc)
17716 && NOTE_VAR_LOCATION (loc_list->first->loc)
17717 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17719 struct var_loc_node *node;
17721 node = loc_list->first;
17722 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17723 if (GET_CODE (rtl) == EXPR_LIST)
17724 rtl = XEXP (rtl, 0);
17725 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17726 && add_const_value_attribute (die, rtl))
17727 return true;
17729 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17730 list several times. See if we've already cached the contents. */
17731 list = NULL;
17732 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17733 cache_p = false;
17734 if (cache_p)
17736 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17737 if (cache)
17738 list = cache->loc_list;
17740 if (list == NULL)
17742 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17743 NULL);
17744 /* It is usually worth caching this result if the decl is from
17745 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17746 if (cache_p && list && list->dw_loc_next)
17748 cached_dw_loc_list **slot
17749 = cached_dw_loc_list_table->find_slot_with_hash (decl,
17750 DECL_UID (decl),
17751 INSERT);
17752 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17753 cache->decl_id = DECL_UID (decl);
17754 cache->loc_list = list;
17755 *slot = cache;
17758 if (list)
17760 add_AT_location_description (die, DW_AT_location, list);
17761 return true;
17763 /* None of that worked, so it must not really have a location;
17764 try adding a constant value attribute from the DECL_INITIAL. */
17765 return tree_add_const_value_attribute_for_decl (die, decl);
17768 /* Helper function for tree_add_const_value_attribute. Natively encode
17769 initializer INIT into an array. Return true if successful. */
17771 static bool
17772 native_encode_initializer (tree init, unsigned char *array, int size)
17774 tree type;
17776 if (init == NULL_TREE)
17777 return false;
17779 STRIP_NOPS (init);
17780 switch (TREE_CODE (init))
17782 case STRING_CST:
17783 type = TREE_TYPE (init);
17784 if (TREE_CODE (type) == ARRAY_TYPE)
17786 tree enttype = TREE_TYPE (type);
17787 machine_mode mode = TYPE_MODE (enttype);
17789 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17790 return false;
17791 if (int_size_in_bytes (type) != size)
17792 return false;
17793 if (size > TREE_STRING_LENGTH (init))
17795 memcpy (array, TREE_STRING_POINTER (init),
17796 TREE_STRING_LENGTH (init));
17797 memset (array + TREE_STRING_LENGTH (init),
17798 '\0', size - TREE_STRING_LENGTH (init));
17800 else
17801 memcpy (array, TREE_STRING_POINTER (init), size);
17802 return true;
17804 return false;
17805 case CONSTRUCTOR:
17806 type = TREE_TYPE (init);
17807 if (int_size_in_bytes (type) != size)
17808 return false;
17809 if (TREE_CODE (type) == ARRAY_TYPE)
17811 HOST_WIDE_INT min_index;
17812 unsigned HOST_WIDE_INT cnt;
17813 int curpos = 0, fieldsize;
17814 constructor_elt *ce;
17816 if (TYPE_DOMAIN (type) == NULL_TREE
17817 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17818 return false;
17820 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17821 if (fieldsize <= 0)
17822 return false;
17824 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17825 memset (array, '\0', size);
17826 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17828 tree val = ce->value;
17829 tree index = ce->index;
17830 int pos = curpos;
17831 if (index && TREE_CODE (index) == RANGE_EXPR)
17832 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17833 * fieldsize;
17834 else if (index)
17835 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17837 if (val)
17839 STRIP_NOPS (val);
17840 if (!native_encode_initializer (val, array + pos, fieldsize))
17841 return false;
17843 curpos = pos + fieldsize;
17844 if (index && TREE_CODE (index) == RANGE_EXPR)
17846 int count = tree_to_shwi (TREE_OPERAND (index, 1))
17847 - tree_to_shwi (TREE_OPERAND (index, 0));
17848 while (count-- > 0)
17850 if (val)
17851 memcpy (array + curpos, array + pos, fieldsize);
17852 curpos += fieldsize;
17855 gcc_assert (curpos <= size);
17857 return true;
17859 else if (TREE_CODE (type) == RECORD_TYPE
17860 || TREE_CODE (type) == UNION_TYPE)
17862 tree field = NULL_TREE;
17863 unsigned HOST_WIDE_INT cnt;
17864 constructor_elt *ce;
17866 if (int_size_in_bytes (type) != size)
17867 return false;
17869 if (TREE_CODE (type) == RECORD_TYPE)
17870 field = TYPE_FIELDS (type);
17872 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17874 tree val = ce->value;
17875 int pos, fieldsize;
17877 if (ce->index != 0)
17878 field = ce->index;
17880 if (val)
17881 STRIP_NOPS (val);
17883 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17884 return false;
17886 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17887 && TYPE_DOMAIN (TREE_TYPE (field))
17888 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17889 return false;
17890 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17891 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17892 return false;
17893 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17894 pos = int_byte_position (field);
17895 gcc_assert (pos + fieldsize <= size);
17896 if (val && fieldsize != 0
17897 && !native_encode_initializer (val, array + pos, fieldsize))
17898 return false;
17900 return true;
17902 return false;
17903 case VIEW_CONVERT_EXPR:
17904 case NON_LVALUE_EXPR:
17905 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17906 default:
17907 return native_encode_expr (init, array, size) == size;
17911 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17912 attribute is the const value T. */
17914 static bool
17915 tree_add_const_value_attribute (dw_die_ref die, tree t)
17917 tree init;
17918 tree type = TREE_TYPE (t);
17919 rtx rtl;
17921 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17922 return false;
17924 init = t;
17925 gcc_assert (!DECL_P (init));
17927 rtl = rtl_for_decl_init (init, type);
17928 if (rtl)
17929 return add_const_value_attribute (die, rtl);
17930 /* If the host and target are sane, try harder. */
17931 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17932 && initializer_constant_valid_p (init, type))
17934 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17935 if (size > 0 && (int) size == size)
17937 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17939 if (native_encode_initializer (init, array, size))
17941 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17942 return true;
17944 ggc_free (array);
17947 return false;
17950 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17951 attribute is the const value of T, where T is an integral constant
17952 variable with static storage duration
17953 (so it can't be a PARM_DECL or a RESULT_DECL). */
17955 static bool
17956 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17959 if (!decl
17960 || (TREE_CODE (decl) != VAR_DECL
17961 && TREE_CODE (decl) != CONST_DECL)
17962 || (TREE_CODE (decl) == VAR_DECL
17963 && !TREE_STATIC (decl)))
17964 return false;
17966 if (TREE_READONLY (decl)
17967 && ! TREE_THIS_VOLATILE (decl)
17968 && DECL_INITIAL (decl))
17969 /* OK */;
17970 else
17971 return false;
17973 /* Don't add DW_AT_const_value if abstract origin already has one. */
17974 if (get_AT (var_die, DW_AT_const_value))
17975 return false;
17977 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17980 /* Convert the CFI instructions for the current function into a
17981 location list. This is used for DW_AT_frame_base when we targeting
17982 a dwarf2 consumer that does not support the dwarf3
17983 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17984 expressions. */
17986 static dw_loc_list_ref
17987 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17989 int ix;
17990 dw_fde_ref fde;
17991 dw_loc_list_ref list, *list_tail;
17992 dw_cfi_ref cfi;
17993 dw_cfa_location last_cfa, next_cfa;
17994 const char *start_label, *last_label, *section;
17995 dw_cfa_location remember;
17997 fde = cfun->fde;
17998 gcc_assert (fde != NULL);
18000 section = secname_for_decl (current_function_decl);
18001 list_tail = &list;
18002 list = NULL;
18004 memset (&next_cfa, 0, sizeof (next_cfa));
18005 next_cfa.reg = INVALID_REGNUM;
18006 remember = next_cfa;
18008 start_label = fde->dw_fde_begin;
18010 /* ??? Bald assumption that the CIE opcode list does not contain
18011 advance opcodes. */
18012 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
18013 lookup_cfa_1 (cfi, &next_cfa, &remember);
18015 last_cfa = next_cfa;
18016 last_label = start_label;
18018 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18020 /* If the first partition contained no CFI adjustments, the
18021 CIE opcodes apply to the whole first partition. */
18022 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18023 fde->dw_fde_begin, fde->dw_fde_end, section);
18024 list_tail =&(*list_tail)->dw_loc_next;
18025 start_label = last_label = fde->dw_fde_second_begin;
18028 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
18030 switch (cfi->dw_cfi_opc)
18032 case DW_CFA_set_loc:
18033 case DW_CFA_advance_loc1:
18034 case DW_CFA_advance_loc2:
18035 case DW_CFA_advance_loc4:
18036 if (!cfa_equal_p (&last_cfa, &next_cfa))
18038 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18039 start_label, last_label, section);
18041 list_tail = &(*list_tail)->dw_loc_next;
18042 last_cfa = next_cfa;
18043 start_label = last_label;
18045 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18046 break;
18048 case DW_CFA_advance_loc:
18049 /* The encoding is complex enough that we should never emit this. */
18050 gcc_unreachable ();
18052 default:
18053 lookup_cfa_1 (cfi, &next_cfa, &remember);
18054 break;
18056 if (ix + 1 == fde->dw_fde_switch_cfi_index)
18058 if (!cfa_equal_p (&last_cfa, &next_cfa))
18060 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18061 start_label, last_label, section);
18063 list_tail = &(*list_tail)->dw_loc_next;
18064 last_cfa = next_cfa;
18065 start_label = last_label;
18067 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18068 start_label, fde->dw_fde_end, section);
18069 list_tail = &(*list_tail)->dw_loc_next;
18070 start_label = last_label = fde->dw_fde_second_begin;
18074 if (!cfa_equal_p (&last_cfa, &next_cfa))
18076 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18077 start_label, last_label, section);
18078 list_tail = &(*list_tail)->dw_loc_next;
18079 start_label = last_label;
18082 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18083 start_label,
18084 fde->dw_fde_second_begin
18085 ? fde->dw_fde_second_end : fde->dw_fde_end,
18086 section);
18088 if (list && list->dw_loc_next)
18089 gen_llsym (list);
18091 return list;
18094 /* Compute a displacement from the "steady-state frame pointer" to the
18095 frame base (often the same as the CFA), and store it in
18096 frame_pointer_fb_offset. OFFSET is added to the displacement
18097 before the latter is negated. */
18099 static void
18100 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18102 rtx reg, elim;
18104 #ifdef FRAME_POINTER_CFA_OFFSET
18105 reg = frame_pointer_rtx;
18106 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18107 #else
18108 reg = arg_pointer_rtx;
18109 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18110 #endif
18112 elim = (ira_use_lra_p
18113 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18114 : eliminate_regs (reg, VOIDmode, NULL_RTX));
18115 if (GET_CODE (elim) == PLUS)
18117 offset += INTVAL (XEXP (elim, 1));
18118 elim = XEXP (elim, 0);
18121 frame_pointer_fb_offset = -offset;
18123 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18124 in which to eliminate. This is because it's stack pointer isn't
18125 directly accessible as a register within the ISA. To work around
18126 this, assume that while we cannot provide a proper value for
18127 frame_pointer_fb_offset, we won't need one either. */
18128 frame_pointer_fb_offset_valid
18129 = ((SUPPORTS_STACK_ALIGNMENT
18130 && (elim == hard_frame_pointer_rtx
18131 || elim == stack_pointer_rtx))
18132 || elim == (frame_pointer_needed
18133 ? hard_frame_pointer_rtx
18134 : stack_pointer_rtx));
18137 /* Generate a DW_AT_name attribute given some string value to be included as
18138 the value of the attribute. */
18140 static void
18141 add_name_attribute (dw_die_ref die, const char *name_string)
18143 if (name_string != NULL && *name_string != 0)
18145 if (demangle_name_func)
18146 name_string = (*demangle_name_func) (name_string);
18148 add_AT_string (die, DW_AT_name, name_string);
18152 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18153 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18154 of TYPE accordingly.
18156 ??? This is a temporary measure until after we're able to generate
18157 regular DWARF for the complex Ada type system. */
18159 static void
18160 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18161 dw_die_ref context_die)
18163 tree dtype;
18164 dw_die_ref dtype_die;
18166 if (!lang_hooks.types.descriptive_type)
18167 return;
18169 dtype = lang_hooks.types.descriptive_type (type);
18170 if (!dtype)
18171 return;
18173 dtype_die = lookup_type_die (dtype);
18174 if (!dtype_die)
18176 gen_type_die (dtype, context_die);
18177 dtype_die = lookup_type_die (dtype);
18178 gcc_assert (dtype_die);
18181 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18184 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
18186 static const char *
18187 comp_dir_string (void)
18189 const char *wd;
18190 char *wd1;
18191 static const char *cached_wd = NULL;
18193 if (cached_wd != NULL)
18194 return cached_wd;
18196 wd = get_src_pwd ();
18197 if (wd == NULL)
18198 return NULL;
18200 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18202 int wdlen;
18204 wdlen = strlen (wd);
18205 wd1 = ggc_vec_alloc<char> (wdlen + 2);
18206 strcpy (wd1, wd);
18207 wd1 [wdlen] = DIR_SEPARATOR;
18208 wd1 [wdlen + 1] = 0;
18209 wd = wd1;
18212 cached_wd = remap_debug_filename (wd);
18213 return cached_wd;
18216 /* Generate a DW_AT_comp_dir attribute for DIE. */
18218 static void
18219 add_comp_dir_attribute (dw_die_ref die)
18221 const char * wd = comp_dir_string ();
18222 if (wd != NULL)
18223 add_AT_string (die, DW_AT_comp_dir, wd);
18226 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18227 pointer computation, ...), output a representation for that bound according
18228 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
18229 loc_list_from_tree for the meaning of CONTEXT. */
18231 static void
18232 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18233 int forms, const struct loc_descr_context *context)
18235 dw_die_ref context_die, decl_die;
18236 dw_loc_list_ref list;
18238 bool strip_conversions = true;
18240 while (strip_conversions)
18241 switch (TREE_CODE (value))
18243 case ERROR_MARK:
18244 case SAVE_EXPR:
18245 return;
18247 CASE_CONVERT:
18248 case VIEW_CONVERT_EXPR:
18249 value = TREE_OPERAND (value, 0);
18250 break;
18252 default:
18253 strip_conversions = false;
18254 break;
18257 /* If possible and permitted, output the attribute as a constant. */
18258 if ((forms & dw_scalar_form_constant) != 0
18259 && TREE_CODE (value) == INTEGER_CST)
18261 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18263 /* If HOST_WIDE_INT is big enough then represent the bound as
18264 a constant value. We need to choose a form based on
18265 whether the type is signed or unsigned. We cannot just
18266 call add_AT_unsigned if the value itself is positive
18267 (add_AT_unsigned might add the unsigned value encoded as
18268 DW_FORM_data[1248]). Some DWARF consumers will lookup the
18269 bounds type and then sign extend any unsigned values found
18270 for signed types. This is needed only for
18271 DW_AT_{lower,upper}_bound, since for most other attributes,
18272 consumers will treat DW_FORM_data[1248] as unsigned values,
18273 regardless of the underlying type. */
18274 if (prec <= HOST_BITS_PER_WIDE_INT
18275 || tree_fits_uhwi_p (value))
18277 if (TYPE_UNSIGNED (TREE_TYPE (value)))
18278 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18279 else
18280 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18282 else
18283 /* Otherwise represent the bound as an unsigned value with
18284 the precision of its type. The precision and signedness
18285 of the type will be necessary to re-interpret it
18286 unambiguously. */
18287 add_AT_wide (die, attr, value);
18288 return;
18291 /* Otherwise, if it's possible and permitted too, output a reference to
18292 another DIE. */
18293 if ((forms & dw_scalar_form_reference) != 0)
18295 tree decl = NULL_TREE;
18297 /* Some type attributes reference an outer type. For instance, the upper
18298 bound of an array may reference an embedding record (this happens in
18299 Ada). */
18300 if (TREE_CODE (value) == COMPONENT_REF
18301 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18302 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18303 decl = TREE_OPERAND (value, 1);
18305 else if (TREE_CODE (value) == VAR_DECL
18306 || TREE_CODE (value) == PARM_DECL
18307 || TREE_CODE (value) == RESULT_DECL)
18308 decl = value;
18310 if (decl != NULL_TREE)
18312 dw_die_ref decl_die = lookup_decl_die (decl);
18314 /* ??? Can this happen, or should the variable have been bound
18315 first? Probably it can, since I imagine that we try to create
18316 the types of parameters in the order in which they exist in
18317 the list, and won't have created a forward reference to a
18318 later parameter. */
18319 if (decl_die != NULL)
18321 add_AT_die_ref (die, attr, decl_die);
18322 return;
18327 /* Last chance: try to create a stack operation procedure to evaluate the
18328 value. Do nothing if even that is not possible or permitted. */
18329 if ((forms & dw_scalar_form_exprloc) == 0)
18330 return;
18332 list = loc_list_from_tree (value, 2, context);
18333 if (list == NULL || single_element_loc_list_p (list))
18335 /* If this attribute is not a reference nor constant, it is
18336 a DWARF expression rather than location description. For that
18337 loc_list_from_tree (value, 0, &context) is needed. */
18338 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18339 if (list2 && single_element_loc_list_p (list2))
18341 add_AT_loc (die, attr, list2->expr);
18342 return;
18346 /* If that failed to give a single element location list, fall back to
18347 outputting this as a reference... still if permitted. */
18348 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18349 return;
18351 if (current_function_decl == 0)
18352 context_die = comp_unit_die ();
18353 else
18354 context_die = lookup_decl_die (current_function_decl);
18356 decl_die = new_die (DW_TAG_variable, context_die, value);
18357 add_AT_flag (decl_die, DW_AT_artificial, 1);
18358 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18359 context_die);
18360 add_AT_location_description (decl_die, DW_AT_location, list);
18361 add_AT_die_ref (die, attr, decl_die);
18364 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18365 default. */
18367 static int
18368 lower_bound_default (void)
18370 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18372 case DW_LANG_C:
18373 case DW_LANG_C89:
18374 case DW_LANG_C99:
18375 case DW_LANG_C11:
18376 case DW_LANG_C_plus_plus:
18377 case DW_LANG_C_plus_plus_11:
18378 case DW_LANG_C_plus_plus_14:
18379 case DW_LANG_ObjC:
18380 case DW_LANG_ObjC_plus_plus:
18381 case DW_LANG_Java:
18382 return 0;
18383 case DW_LANG_Fortran77:
18384 case DW_LANG_Fortran90:
18385 case DW_LANG_Fortran95:
18386 case DW_LANG_Fortran03:
18387 case DW_LANG_Fortran08:
18388 return 1;
18389 case DW_LANG_UPC:
18390 case DW_LANG_D:
18391 case DW_LANG_Python:
18392 return dwarf_version >= 4 ? 0 : -1;
18393 case DW_LANG_Ada95:
18394 case DW_LANG_Ada83:
18395 case DW_LANG_Cobol74:
18396 case DW_LANG_Cobol85:
18397 case DW_LANG_Pascal83:
18398 case DW_LANG_Modula2:
18399 case DW_LANG_PLI:
18400 return dwarf_version >= 4 ? 1 : -1;
18401 default:
18402 return -1;
18406 /* Given a tree node describing an array bound (either lower or upper) output
18407 a representation for that bound. */
18409 static void
18410 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18411 tree bound, const struct loc_descr_context *context)
18413 int dflt;
18415 while (1)
18416 switch (TREE_CODE (bound))
18418 /* Strip all conversions. */
18419 CASE_CONVERT:
18420 case VIEW_CONVERT_EXPR:
18421 bound = TREE_OPERAND (bound, 0);
18422 break;
18424 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
18425 are even omitted when they are the default. */
18426 case INTEGER_CST:
18427 /* If the value for this bound is the default one, we can even omit the
18428 attribute. */
18429 if (bound_attr == DW_AT_lower_bound
18430 && tree_fits_shwi_p (bound)
18431 && (dflt = lower_bound_default ()) != -1
18432 && tree_to_shwi (bound) == dflt)
18433 return;
18435 /* FALLTHRU */
18437 default:
18438 /* Because of the complex interaction there can be with other GNAT
18439 encodings, GDB isn't ready yet to handle proper DWARF description
18440 for self-referencial subrange bounds: let GNAT encodings do the
18441 magic in such a case. */
18442 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18443 && contains_placeholder_p (bound))
18444 return;
18446 add_scalar_info (subrange_die, bound_attr, bound,
18447 dw_scalar_form_constant
18448 | dw_scalar_form_exprloc
18449 | dw_scalar_form_reference,
18450 context);
18451 return;
18455 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18456 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18457 Note that the block of subscript information for an array type also
18458 includes information about the element type of the given array type.
18460 This function reuses previously set type and bound information if
18461 available. */
18463 static void
18464 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18466 unsigned dimension_number;
18467 tree lower, upper;
18468 dw_die_ref child = type_die->die_child;
18470 for (dimension_number = 0;
18471 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18472 type = TREE_TYPE (type), dimension_number++)
18474 tree domain = TYPE_DOMAIN (type);
18476 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18477 break;
18479 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18480 and (in GNU C only) variable bounds. Handle all three forms
18481 here. */
18483 /* Find and reuse a previously generated DW_TAG_subrange_type if
18484 available.
18486 For multi-dimensional arrays, as we iterate through the
18487 various dimensions in the enclosing for loop above, we also
18488 iterate through the DIE children and pick at each
18489 DW_TAG_subrange_type previously generated (if available).
18490 Each child DW_TAG_subrange_type DIE describes the range of
18491 the current dimension. At this point we should have as many
18492 DW_TAG_subrange_type's as we have dimensions in the
18493 array. */
18494 dw_die_ref subrange_die = NULL;
18495 if (child)
18496 while (1)
18498 child = child->die_sib;
18499 if (child->die_tag == DW_TAG_subrange_type)
18500 subrange_die = child;
18501 if (child == type_die->die_child)
18503 /* If we wrapped around, stop looking next time. */
18504 child = NULL;
18505 break;
18507 if (child->die_tag == DW_TAG_subrange_type)
18508 break;
18510 if (!subrange_die)
18511 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18513 if (domain)
18515 /* We have an array type with specified bounds. */
18516 lower = TYPE_MIN_VALUE (domain);
18517 upper = TYPE_MAX_VALUE (domain);
18519 /* Define the index type. */
18520 if (TREE_TYPE (domain)
18521 && !get_AT (subrange_die, DW_AT_type))
18523 /* ??? This is probably an Ada unnamed subrange type. Ignore the
18524 TREE_TYPE field. We can't emit debug info for this
18525 because it is an unnamed integral type. */
18526 if (TREE_CODE (domain) == INTEGER_TYPE
18527 && TYPE_NAME (domain) == NULL_TREE
18528 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18529 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18531 else
18532 add_type_attribute (subrange_die, TREE_TYPE (domain),
18533 TYPE_UNQUALIFIED, false, type_die);
18536 /* ??? If upper is NULL, the array has unspecified length,
18537 but it does have a lower bound. This happens with Fortran
18538 dimension arr(N:*)
18539 Since the debugger is definitely going to need to know N
18540 to produce useful results, go ahead and output the lower
18541 bound solo, and hope the debugger can cope. */
18543 if (!get_AT (subrange_die, DW_AT_lower_bound))
18544 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18545 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18546 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18549 /* Otherwise we have an array type with an unspecified length. The
18550 DWARF-2 spec does not say how to handle this; let's just leave out the
18551 bounds. */
18555 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
18557 static void
18558 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18560 dw_die_ref decl_die;
18561 HOST_WIDE_INT size;
18562 dw_loc_descr_ref size_expr = NULL;
18564 switch (TREE_CODE (tree_node))
18566 case ERROR_MARK:
18567 size = 0;
18568 break;
18569 case ENUMERAL_TYPE:
18570 case RECORD_TYPE:
18571 case UNION_TYPE:
18572 case QUAL_UNION_TYPE:
18573 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18574 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18576 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18577 return;
18579 size_expr = type_byte_size (tree_node, &size);
18580 break;
18581 case FIELD_DECL:
18582 /* For a data member of a struct or union, the DW_AT_byte_size is
18583 generally given as the number of bytes normally allocated for an
18584 object of the *declared* type of the member itself. This is true
18585 even for bit-fields. */
18586 size = int_size_in_bytes (field_type (tree_node));
18587 break;
18588 default:
18589 gcc_unreachable ();
18592 /* Support for dynamically-sized objects was introduced by DWARFv3.
18593 At the moment, GDB does not handle variable byte sizes very well,
18594 though. */
18595 if ((dwarf_version >= 3 || !dwarf_strict)
18596 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18597 && size_expr != NULL)
18598 add_AT_loc (die, DW_AT_byte_size, size_expr);
18600 /* Note that `size' might be -1 when we get to this point. If it is, that
18601 indicates that the byte size of the entity in question is variable and
18602 that we could not generate a DWARF expression that computes it. */
18603 if (size >= 0)
18604 add_AT_unsigned (die, DW_AT_byte_size, size);
18607 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18608 which specifies the distance in bits from the highest order bit of the
18609 "containing object" for the bit-field to the highest order bit of the
18610 bit-field itself.
18612 For any given bit-field, the "containing object" is a hypothetical object
18613 (of some integral or enum type) within which the given bit-field lives. The
18614 type of this hypothetical "containing object" is always the same as the
18615 declared type of the individual bit-field itself. The determination of the
18616 exact location of the "containing object" for a bit-field is rather
18617 complicated. It's handled by the `field_byte_offset' function (above).
18619 CTX is required: see the comment for VLR_CONTEXT.
18621 Note that it is the size (in bytes) of the hypothetical "containing object"
18622 which will be given in the DW_AT_byte_size attribute for this bit-field.
18623 (See `byte_size_attribute' above). */
18625 static inline void
18626 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18628 HOST_WIDE_INT object_offset_in_bytes;
18629 tree original_type = DECL_BIT_FIELD_TYPE (decl);
18630 HOST_WIDE_INT bitpos_int;
18631 HOST_WIDE_INT highest_order_object_bit_offset;
18632 HOST_WIDE_INT highest_order_field_bit_offset;
18633 HOST_WIDE_INT bit_offset;
18635 field_byte_offset (decl, ctx, &object_offset_in_bytes);
18637 /* Must be a field and a bit field. */
18638 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18640 /* We can't yet handle bit-fields whose offsets are variable, so if we
18641 encounter such things, just return without generating any attribute
18642 whatsoever. Likewise for variable or too large size. */
18643 if (! tree_fits_shwi_p (bit_position (decl))
18644 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18645 return;
18647 bitpos_int = int_bit_position (decl);
18649 /* Note that the bit offset is always the distance (in bits) from the
18650 highest-order bit of the "containing object" to the highest-order bit of
18651 the bit-field itself. Since the "high-order end" of any object or field
18652 is different on big-endian and little-endian machines, the computation
18653 below must take account of these differences. */
18654 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18655 highest_order_field_bit_offset = bitpos_int;
18657 if (! BYTES_BIG_ENDIAN)
18659 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18660 highest_order_object_bit_offset +=
18661 simple_type_size_in_bits (original_type);
18664 bit_offset
18665 = (! BYTES_BIG_ENDIAN
18666 ? highest_order_object_bit_offset - highest_order_field_bit_offset
18667 : highest_order_field_bit_offset - highest_order_object_bit_offset);
18669 if (bit_offset < 0)
18670 add_AT_int (die, DW_AT_bit_offset, bit_offset);
18671 else
18672 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18675 /* For a FIELD_DECL node which represents a bit field, output an attribute
18676 which specifies the length in bits of the given field. */
18678 static inline void
18679 add_bit_size_attribute (dw_die_ref die, tree decl)
18681 /* Must be a field and a bit field. */
18682 gcc_assert (TREE_CODE (decl) == FIELD_DECL
18683 && DECL_BIT_FIELD_TYPE (decl));
18685 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18686 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18689 /* If the compiled language is ANSI C, then add a 'prototyped'
18690 attribute, if arg types are given for the parameters of a function. */
18692 static inline void
18693 add_prototyped_attribute (dw_die_ref die, tree func_type)
18695 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18697 case DW_LANG_C:
18698 case DW_LANG_C89:
18699 case DW_LANG_C99:
18700 case DW_LANG_C11:
18701 case DW_LANG_ObjC:
18702 if (prototype_p (func_type))
18703 add_AT_flag (die, DW_AT_prototyped, 1);
18704 break;
18705 default:
18706 break;
18710 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
18711 by looking in the type declaration, the object declaration equate table or
18712 the block mapping. */
18714 static inline dw_die_ref
18715 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18717 dw_die_ref origin_die = NULL;
18719 if (TREE_CODE (origin) != FUNCTION_DECL
18720 && TREE_CODE (origin) != BLOCK)
18722 /* We may have gotten separated from the block for the inlined
18723 function, if we're in an exception handler or some such; make
18724 sure that the abstract function has been written out.
18726 Doing this for nested functions is wrong, however; functions are
18727 distinct units, and our context might not even be inline. */
18728 tree fn = origin;
18730 if (TYPE_P (fn))
18731 fn = TYPE_STUB_DECL (fn);
18733 fn = decl_function_context (fn);
18734 if (fn)
18735 dwarf2out_abstract_function (fn);
18738 if (DECL_P (origin))
18739 origin_die = lookup_decl_die (origin);
18740 else if (TYPE_P (origin))
18741 origin_die = lookup_type_die (origin);
18742 else if (TREE_CODE (origin) == BLOCK)
18743 origin_die = BLOCK_DIE (origin);
18745 /* XXX: Functions that are never lowered don't always have correct block
18746 trees (in the case of java, they simply have no block tree, in some other
18747 languages). For these functions, there is nothing we can really do to
18748 output correct debug info for inlined functions in all cases. Rather
18749 than die, we'll just produce deficient debug info now, in that we will
18750 have variables without a proper abstract origin. In the future, when all
18751 functions are lowered, we should re-add a gcc_assert (origin_die)
18752 here. */
18754 if (origin_die)
18755 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18756 return origin_die;
18759 /* We do not currently support the pure_virtual attribute. */
18761 static inline void
18762 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18764 if (DECL_VINDEX (func_decl))
18766 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18768 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18769 add_AT_loc (die, DW_AT_vtable_elem_location,
18770 new_loc_descr (DW_OP_constu,
18771 tree_to_shwi (DECL_VINDEX (func_decl)),
18772 0));
18774 /* GNU extension: Record what type this method came from originally. */
18775 if (debug_info_level > DINFO_LEVEL_TERSE
18776 && DECL_CONTEXT (func_decl))
18777 add_AT_die_ref (die, DW_AT_containing_type,
18778 lookup_type_die (DECL_CONTEXT (func_decl)));
18782 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18783 given decl. This used to be a vendor extension until after DWARF 4
18784 standardized it. */
18786 static void
18787 add_linkage_attr (dw_die_ref die, tree decl)
18789 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18791 /* Mimic what assemble_name_raw does with a leading '*'. */
18792 if (name[0] == '*')
18793 name = &name[1];
18795 if (dwarf_version >= 4)
18796 add_AT_string (die, DW_AT_linkage_name, name);
18797 else
18798 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18801 /* Add source coordinate attributes for the given decl. */
18803 static void
18804 add_src_coords_attributes (dw_die_ref die, tree decl)
18806 expanded_location s;
18808 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18809 return;
18810 s = expand_location (DECL_SOURCE_LOCATION (decl));
18811 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18812 add_AT_unsigned (die, DW_AT_decl_line, s.line);
18815 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
18817 static void
18818 add_linkage_name_raw (dw_die_ref die, tree decl)
18820 /* Defer until we have an assembler name set. */
18821 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18823 limbo_die_node *asm_name;
18825 asm_name = ggc_cleared_alloc<limbo_die_node> ();
18826 asm_name->die = die;
18827 asm_name->created_for = decl;
18828 asm_name->next = deferred_asm_name;
18829 deferred_asm_name = asm_name;
18831 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18832 add_linkage_attr (die, decl);
18835 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
18837 static void
18838 add_linkage_name (dw_die_ref die, tree decl)
18840 if (debug_info_level > DINFO_LEVEL_NONE
18841 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18842 && TREE_PUBLIC (decl)
18843 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18844 && die->die_tag != DW_TAG_member)
18845 add_linkage_name_raw (die, decl);
18848 /* Add a DW_AT_name attribute and source coordinate attribute for the
18849 given decl, but only if it actually has a name. */
18851 static void
18852 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18854 tree decl_name;
18856 decl_name = DECL_NAME (decl);
18857 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18859 const char *name = dwarf2_name (decl, 0);
18860 if (name)
18861 add_name_attribute (die, name);
18862 if (! DECL_ARTIFICIAL (decl))
18863 add_src_coords_attributes (die, decl);
18865 add_linkage_name (die, decl);
18868 #ifdef VMS_DEBUGGING_INFO
18869 /* Get the function's name, as described by its RTL. This may be different
18870 from the DECL_NAME name used in the source file. */
18871 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18873 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18874 XEXP (DECL_RTL (decl), 0), false);
18875 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18877 #endif /* VMS_DEBUGGING_INFO */
18880 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
18882 static void
18883 add_discr_value (dw_die_ref die, dw_discr_value *value)
18885 dw_attr_node attr;
18887 attr.dw_attr = DW_AT_discr_value;
18888 attr.dw_attr_val.val_class = dw_val_class_discr_value;
18889 attr.dw_attr_val.val_entry = NULL;
18890 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18891 if (value->pos)
18892 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18893 else
18894 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18895 add_dwarf_attr (die, &attr);
18898 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
18900 static void
18901 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18903 dw_attr_node attr;
18905 attr.dw_attr = DW_AT_discr_list;
18906 attr.dw_attr_val.val_class = dw_val_class_discr_list;
18907 attr.dw_attr_val.val_entry = NULL;
18908 attr.dw_attr_val.v.val_discr_list = discr_list;
18909 add_dwarf_attr (die, &attr);
18912 static inline dw_discr_list_ref
18913 AT_discr_list (dw_attr_node *attr)
18915 return attr->dw_attr_val.v.val_discr_list;
18918 #ifdef VMS_DEBUGGING_INFO
18919 /* Output the debug main pointer die for VMS */
18921 void
18922 dwarf2out_vms_debug_main_pointer (void)
18924 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18925 dw_die_ref die;
18927 /* Allocate the VMS debug main subprogram die. */
18928 die = ggc_cleared_alloc<die_node> ();
18929 die->die_tag = DW_TAG_subprogram;
18930 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18931 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18932 current_function_funcdef_no);
18933 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18935 /* Make it the first child of comp_unit_die (). */
18936 die->die_parent = comp_unit_die ();
18937 if (comp_unit_die ()->die_child)
18939 die->die_sib = comp_unit_die ()->die_child->die_sib;
18940 comp_unit_die ()->die_child->die_sib = die;
18942 else
18944 die->die_sib = die;
18945 comp_unit_die ()->die_child = die;
18948 #endif /* VMS_DEBUGGING_INFO */
18950 /* Push a new declaration scope. */
18952 static void
18953 push_decl_scope (tree scope)
18955 vec_safe_push (decl_scope_table, scope);
18958 /* Pop a declaration scope. */
18960 static inline void
18961 pop_decl_scope (void)
18963 decl_scope_table->pop ();
18966 /* walk_tree helper function for uses_local_type, below. */
18968 static tree
18969 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18971 if (!TYPE_P (*tp))
18972 *walk_subtrees = 0;
18973 else
18975 tree name = TYPE_NAME (*tp);
18976 if (name && DECL_P (name) && decl_function_context (name))
18977 return *tp;
18979 return NULL_TREE;
18982 /* If TYPE involves a function-local type (including a local typedef to a
18983 non-local type), returns that type; otherwise returns NULL_TREE. */
18985 static tree
18986 uses_local_type (tree type)
18988 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
18989 return used;
18992 /* Return the DIE for the scope that immediately contains this type.
18993 Non-named types that do not involve a function-local type get global
18994 scope. Named types nested in namespaces or other types get their
18995 containing scope. All other types (i.e. function-local named types) get
18996 the current active scope. */
18998 static dw_die_ref
18999 scope_die_for (tree t, dw_die_ref context_die)
19001 dw_die_ref scope_die = NULL;
19002 tree containing_scope;
19004 /* Non-types always go in the current scope. */
19005 gcc_assert (TYPE_P (t));
19007 /* Use the scope of the typedef, rather than the scope of the type
19008 it refers to. */
19009 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
19010 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
19011 else
19012 containing_scope = TYPE_CONTEXT (t);
19014 /* Use the containing namespace if there is one. */
19015 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
19017 if (context_die == lookup_decl_die (containing_scope))
19018 /* OK */;
19019 else if (debug_info_level > DINFO_LEVEL_TERSE)
19020 context_die = get_context_die (containing_scope);
19021 else
19022 containing_scope = NULL_TREE;
19025 /* Ignore function type "scopes" from the C frontend. They mean that
19026 a tagged type is local to a parmlist of a function declarator, but
19027 that isn't useful to DWARF. */
19028 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19029 containing_scope = NULL_TREE;
19031 if (SCOPE_FILE_SCOPE_P (containing_scope))
19033 /* If T uses a local type keep it local as well, to avoid references
19034 to function-local DIEs from outside the function. */
19035 if (current_function_decl && uses_local_type (t))
19036 scope_die = context_die;
19037 else
19038 scope_die = comp_unit_die ();
19040 else if (TYPE_P (containing_scope))
19042 /* For types, we can just look up the appropriate DIE. */
19043 if (debug_info_level > DINFO_LEVEL_TERSE)
19044 scope_die = get_context_die (containing_scope);
19045 else
19047 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19048 if (scope_die == NULL)
19049 scope_die = comp_unit_die ();
19052 else
19053 scope_die = context_die;
19055 return scope_die;
19058 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
19060 static inline int
19061 local_scope_p (dw_die_ref context_die)
19063 for (; context_die; context_die = context_die->die_parent)
19064 if (context_die->die_tag == DW_TAG_inlined_subroutine
19065 || context_die->die_tag == DW_TAG_subprogram)
19066 return 1;
19068 return 0;
19071 /* Returns nonzero if CONTEXT_DIE is a class. */
19073 static inline int
19074 class_scope_p (dw_die_ref context_die)
19076 return (context_die
19077 && (context_die->die_tag == DW_TAG_structure_type
19078 || context_die->die_tag == DW_TAG_class_type
19079 || context_die->die_tag == DW_TAG_interface_type
19080 || context_die->die_tag == DW_TAG_union_type));
19083 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19084 whether or not to treat a DIE in this context as a declaration. */
19086 static inline int
19087 class_or_namespace_scope_p (dw_die_ref context_die)
19089 return (class_scope_p (context_die)
19090 || (context_die && context_die->die_tag == DW_TAG_namespace));
19093 /* Many forms of DIEs require a "type description" attribute. This
19094 routine locates the proper "type descriptor" die for the type given
19095 by 'type' plus any additional qualifiers given by 'cv_quals', and
19096 adds a DW_AT_type attribute below the given die. */
19098 static void
19099 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19100 bool reverse, dw_die_ref context_die)
19102 enum tree_code code = TREE_CODE (type);
19103 dw_die_ref type_die = NULL;
19105 /* ??? If this type is an unnamed subrange type of an integral, floating-point
19106 or fixed-point type, use the inner type. This is because we have no
19107 support for unnamed types in base_type_die. This can happen if this is
19108 an Ada subrange type. Correct solution is emit a subrange type die. */
19109 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19110 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19111 type = TREE_TYPE (type), code = TREE_CODE (type);
19113 if (code == ERROR_MARK
19114 /* Handle a special case. For functions whose return type is void, we
19115 generate *no* type attribute. (Note that no object may have type
19116 `void', so this only applies to function return types). */
19117 || code == VOID_TYPE)
19118 return;
19120 type_die = modified_type_die (type,
19121 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19122 reverse,
19123 context_die);
19125 if (type_die != NULL)
19126 add_AT_die_ref (object_die, DW_AT_type, type_die);
19129 /* Given an object die, add the calling convention attribute for the
19130 function call type. */
19131 static void
19132 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19134 enum dwarf_calling_convention value = DW_CC_normal;
19136 value = ((enum dwarf_calling_convention)
19137 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19139 if (is_fortran ()
19140 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19142 /* DWARF 2 doesn't provide a way to identify a program's source-level
19143 entry point. DW_AT_calling_convention attributes are only meant
19144 to describe functions' calling conventions. However, lacking a
19145 better way to signal the Fortran main program, we used this for
19146 a long time, following existing custom. Now, DWARF 4 has
19147 DW_AT_main_subprogram, which we add below, but some tools still
19148 rely on the old way, which we thus keep. */
19149 value = DW_CC_program;
19151 if (dwarf_version >= 4 || !dwarf_strict)
19152 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19155 /* Only add the attribute if the backend requests it, and
19156 is not DW_CC_normal. */
19157 if (value && (value != DW_CC_normal))
19158 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19161 /* Given a tree pointer to a struct, class, union, or enum type node, return
19162 a pointer to the (string) tag name for the given type, or zero if the type
19163 was declared without a tag. */
19165 static const char *
19166 type_tag (const_tree type)
19168 const char *name = 0;
19170 if (TYPE_NAME (type) != 0)
19172 tree t = 0;
19174 /* Find the IDENTIFIER_NODE for the type name. */
19175 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19176 && !TYPE_NAMELESS (type))
19177 t = TYPE_NAME (type);
19179 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19180 a TYPE_DECL node, regardless of whether or not a `typedef' was
19181 involved. */
19182 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19183 && ! DECL_IGNORED_P (TYPE_NAME (type)))
19185 /* We want to be extra verbose. Don't call dwarf_name if
19186 DECL_NAME isn't set. The default hook for decl_printable_name
19187 doesn't like that, and in this context it's correct to return
19188 0, instead of "<anonymous>" or the like. */
19189 if (DECL_NAME (TYPE_NAME (type))
19190 && !DECL_NAMELESS (TYPE_NAME (type)))
19191 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19194 /* Now get the name as a string, or invent one. */
19195 if (!name && t != 0)
19196 name = IDENTIFIER_POINTER (t);
19199 return (name == 0 || *name == '\0') ? 0 : name;
19202 /* Return the type associated with a data member, make a special check
19203 for bit field types. */
19205 static inline tree
19206 member_declared_type (const_tree member)
19208 return (DECL_BIT_FIELD_TYPE (member)
19209 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19212 /* Get the decl's label, as described by its RTL. This may be different
19213 from the DECL_NAME name used in the source file. */
19215 #if 0
19216 static const char *
19217 decl_start_label (tree decl)
19219 rtx x;
19220 const char *fnname;
19222 x = DECL_RTL (decl);
19223 gcc_assert (MEM_P (x));
19225 x = XEXP (x, 0);
19226 gcc_assert (GET_CODE (x) == SYMBOL_REF);
19228 fnname = XSTR (x, 0);
19229 return fnname;
19231 #endif
19233 /* For variable-length arrays that have been previously generated, but
19234 may be incomplete due to missing subscript info, fill the subscript
19235 info. Return TRUE if this is one of those cases. */
19236 static bool
19237 fill_variable_array_bounds (tree type)
19239 if (TREE_ASM_WRITTEN (type)
19240 && TREE_CODE (type) == ARRAY_TYPE
19241 && variably_modified_type_p (type, NULL))
19243 dw_die_ref array_die = lookup_type_die (type);
19244 if (!array_die)
19245 return false;
19246 add_subscript_info (array_die, type, !is_ada ());
19247 return true;
19249 return false;
19252 /* These routines generate the internal representation of the DIE's for
19253 the compilation unit. Debugging information is collected by walking
19254 the declaration trees passed in from dwarf2out_decl(). */
19256 static void
19257 gen_array_type_die (tree type, dw_die_ref context_die)
19259 dw_die_ref array_die;
19261 /* GNU compilers represent multidimensional array types as sequences of one
19262 dimensional array types whose element types are themselves array types.
19263 We sometimes squish that down to a single array_type DIE with multiple
19264 subscripts in the Dwarf debugging info. The draft Dwarf specification
19265 say that we are allowed to do this kind of compression in C, because
19266 there is no difference between an array of arrays and a multidimensional
19267 array. We don't do this for Ada to remain as close as possible to the
19268 actual representation, which is especially important against the language
19269 flexibilty wrt arrays of variable size. */
19271 bool collapse_nested_arrays = !is_ada ();
19273 if (fill_variable_array_bounds (type))
19274 return;
19276 dw_die_ref scope_die = scope_die_for (type, context_die);
19277 tree element_type;
19279 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19280 DW_TAG_string_type doesn't have DW_AT_type attribute). */
19281 if (TYPE_STRING_FLAG (type)
19282 && TREE_CODE (type) == ARRAY_TYPE
19283 && is_fortran ()
19284 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19286 HOST_WIDE_INT size;
19288 array_die = new_die (DW_TAG_string_type, scope_die, type);
19289 add_name_attribute (array_die, type_tag (type));
19290 equate_type_number_to_die (type, array_die);
19291 size = int_size_in_bytes (type);
19292 if (size >= 0)
19293 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19294 else if (TYPE_DOMAIN (type) != NULL_TREE
19295 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
19297 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19298 tree rszdecl = szdecl;
19299 HOST_WIDE_INT rsize = 0;
19301 size = int_size_in_bytes (TREE_TYPE (szdecl));
19302 if (!DECL_P (szdecl))
19304 if (TREE_CODE (szdecl) == INDIRECT_REF
19305 && DECL_P (TREE_OPERAND (szdecl, 0)))
19307 rszdecl = TREE_OPERAND (szdecl, 0);
19308 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
19309 if (rsize <= 0)
19310 size = 0;
19312 else
19313 size = 0;
19315 if (size > 0)
19317 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19318 if (loc == NULL
19319 && early_dwarf
19320 && current_function_decl
19321 && DECL_CONTEXT (rszdecl) == current_function_decl)
19323 dw_die_ref ref = lookup_decl_die (rszdecl);
19324 dw_loc_descr_ref l = NULL;
19325 if (ref)
19327 l = new_loc_descr (DW_OP_call4, 0, 0);
19328 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19329 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
19330 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
19332 else if (TREE_CODE (rszdecl) == PARM_DECL
19333 && string_types)
19335 l = new_loc_descr (DW_OP_call4, 0, 0);
19336 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19337 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
19338 string_types->safe_push (array_die);
19340 if (l && rszdecl != szdecl)
19342 if (rsize == DWARF2_ADDR_SIZE)
19343 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
19344 0, 0));
19345 else
19346 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
19347 rsize, 0));
19349 if (l)
19350 loc = new_loc_list (l, NULL, NULL, NULL);
19352 if (loc)
19354 add_AT_location_description (array_die, DW_AT_string_length,
19355 loc);
19356 if (size != DWARF2_ADDR_SIZE)
19357 add_AT_unsigned (array_die, dwarf_version >= 5
19358 ? DW_AT_string_length_byte_size
19359 : DW_AT_byte_size, size);
19363 return;
19366 array_die = new_die (DW_TAG_array_type, scope_die, type);
19367 add_name_attribute (array_die, type_tag (type));
19368 equate_type_number_to_die (type, array_die);
19370 if (TREE_CODE (type) == VECTOR_TYPE)
19371 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19373 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
19374 if (is_fortran ()
19375 && TREE_CODE (type) == ARRAY_TYPE
19376 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19377 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19378 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19380 #if 0
19381 /* We default the array ordering. SDB will probably do
19382 the right things even if DW_AT_ordering is not present. It's not even
19383 an issue until we start to get into multidimensional arrays anyway. If
19384 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19385 then we'll have to put the DW_AT_ordering attribute back in. (But if
19386 and when we find out that we need to put these in, we will only do so
19387 for multidimensional arrays. */
19388 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19389 #endif
19391 if (TREE_CODE (type) == VECTOR_TYPE)
19393 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
19394 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19395 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19396 add_bound_info (subrange_die, DW_AT_upper_bound,
19397 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19399 else
19400 add_subscript_info (array_die, type, collapse_nested_arrays);
19402 /* Add representation of the type of the elements of this array type and
19403 emit the corresponding DIE if we haven't done it already. */
19404 element_type = TREE_TYPE (type);
19405 if (collapse_nested_arrays)
19406 while (TREE_CODE (element_type) == ARRAY_TYPE)
19408 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19409 break;
19410 element_type = TREE_TYPE (element_type);
19413 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19414 TREE_CODE (type) == ARRAY_TYPE
19415 && TYPE_REVERSE_STORAGE_ORDER (type),
19416 context_die);
19418 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19419 if (TYPE_ARTIFICIAL (type))
19420 add_AT_flag (array_die, DW_AT_artificial, 1);
19422 if (get_AT (array_die, DW_AT_name))
19423 add_pubtype (type, array_die);
19426 /* After all arguments are created, adjust any DW_TAG_string_type
19427 DIEs DW_AT_string_length attributes. */
19429 static void
19430 adjust_string_types (void)
19432 dw_die_ref array_die;
19433 unsigned int i;
19434 FOR_EACH_VEC_ELT (*string_types, i, array_die)
19436 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
19437 if (a == NULL)
19438 continue;
19439 dw_loc_descr_ref loc = AT_loc (a);
19440 gcc_assert (loc->dw_loc_opc == DW_OP_call4
19441 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
19442 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
19443 if (ref)
19445 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19446 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
19447 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
19449 else
19451 remove_AT (array_die, DW_AT_string_length);
19452 remove_AT (array_die, dwarf_version >= 5
19453 ? DW_AT_string_length_byte_size
19454 : DW_AT_byte_size);
19459 /* This routine generates DIE for array with hidden descriptor, details
19460 are filled into *info by a langhook. */
19462 static void
19463 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19464 dw_die_ref context_die)
19466 const dw_die_ref scope_die = scope_die_for (type, context_die);
19467 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19468 const struct loc_descr_context context = { type, info->base_decl, NULL };
19469 int dim;
19471 add_name_attribute (array_die, type_tag (type));
19472 equate_type_number_to_die (type, array_die);
19474 if (info->ndimensions > 1)
19475 switch (info->ordering)
19477 case array_descr_ordering_row_major:
19478 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19479 break;
19480 case array_descr_ordering_column_major:
19481 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19482 break;
19483 default:
19484 break;
19487 if (dwarf_version >= 3 || !dwarf_strict)
19489 if (info->data_location)
19490 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19491 dw_scalar_form_exprloc, &context);
19492 if (info->associated)
19493 add_scalar_info (array_die, DW_AT_associated, info->associated,
19494 dw_scalar_form_constant
19495 | dw_scalar_form_exprloc
19496 | dw_scalar_form_reference, &context);
19497 if (info->allocated)
19498 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19499 dw_scalar_form_constant
19500 | dw_scalar_form_exprloc
19501 | dw_scalar_form_reference, &context);
19502 if (info->stride)
19504 const enum dwarf_attribute attr
19505 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19506 const int forms
19507 = (info->stride_in_bits)
19508 ? dw_scalar_form_constant
19509 : (dw_scalar_form_constant
19510 | dw_scalar_form_exprloc
19511 | dw_scalar_form_reference);
19513 add_scalar_info (array_die, attr, info->stride, forms, &context);
19517 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19519 for (dim = 0; dim < info->ndimensions; dim++)
19521 dw_die_ref subrange_die
19522 = new_die (DW_TAG_subrange_type, array_die, NULL);
19524 if (info->dimen[dim].bounds_type)
19525 add_type_attribute (subrange_die,
19526 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19527 false, context_die);
19528 if (info->dimen[dim].lower_bound)
19529 add_bound_info (subrange_die, DW_AT_lower_bound,
19530 info->dimen[dim].lower_bound, &context);
19531 if (info->dimen[dim].upper_bound)
19532 add_bound_info (subrange_die, DW_AT_upper_bound,
19533 info->dimen[dim].upper_bound, &context);
19534 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19535 add_scalar_info (subrange_die, DW_AT_byte_stride,
19536 info->dimen[dim].stride,
19537 dw_scalar_form_constant
19538 | dw_scalar_form_exprloc
19539 | dw_scalar_form_reference,
19540 &context);
19543 gen_type_die (info->element_type, context_die);
19544 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19545 TREE_CODE (type) == ARRAY_TYPE
19546 && TYPE_REVERSE_STORAGE_ORDER (type),
19547 context_die);
19549 if (get_AT (array_die, DW_AT_name))
19550 add_pubtype (type, array_die);
19553 #if 0
19554 static void
19555 gen_entry_point_die (tree decl, dw_die_ref context_die)
19557 tree origin = decl_ultimate_origin (decl);
19558 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19560 if (origin != NULL)
19561 add_abstract_origin_attribute (decl_die, origin);
19562 else
19564 add_name_and_src_coords_attributes (decl_die, decl);
19565 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19566 TYPE_UNQUALIFIED, false, context_die);
19569 if (DECL_ABSTRACT_P (decl))
19570 equate_decl_number_to_die (decl, decl_die);
19571 else
19572 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19574 #endif
19576 /* Walk through the list of incomplete types again, trying once more to
19577 emit full debugging info for them. */
19579 static void
19580 retry_incomplete_types (void)
19582 set_early_dwarf s;
19583 int i;
19585 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19586 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19587 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19588 vec_safe_truncate (incomplete_types, 0);
19591 /* Determine what tag to use for a record type. */
19593 static enum dwarf_tag
19594 record_type_tag (tree type)
19596 if (! lang_hooks.types.classify_record)
19597 return DW_TAG_structure_type;
19599 switch (lang_hooks.types.classify_record (type))
19601 case RECORD_IS_STRUCT:
19602 return DW_TAG_structure_type;
19604 case RECORD_IS_CLASS:
19605 return DW_TAG_class_type;
19607 case RECORD_IS_INTERFACE:
19608 if (dwarf_version >= 3 || !dwarf_strict)
19609 return DW_TAG_interface_type;
19610 return DW_TAG_structure_type;
19612 default:
19613 gcc_unreachable ();
19617 /* Generate a DIE to represent an enumeration type. Note that these DIEs
19618 include all of the information about the enumeration values also. Each
19619 enumerated type name/value is listed as a child of the enumerated type
19620 DIE. */
19622 static dw_die_ref
19623 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19625 dw_die_ref type_die = lookup_type_die (type);
19627 if (type_die == NULL)
19629 type_die = new_die (DW_TAG_enumeration_type,
19630 scope_die_for (type, context_die), type);
19631 equate_type_number_to_die (type, type_die);
19632 add_name_attribute (type_die, type_tag (type));
19633 if (dwarf_version >= 4 || !dwarf_strict)
19635 if (ENUM_IS_SCOPED (type))
19636 add_AT_flag (type_die, DW_AT_enum_class, 1);
19637 if (ENUM_IS_OPAQUE (type))
19638 add_AT_flag (type_die, DW_AT_declaration, 1);
19641 else if (! TYPE_SIZE (type))
19642 return type_die;
19643 else
19644 remove_AT (type_die, DW_AT_declaration);
19646 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
19647 given enum type is incomplete, do not generate the DW_AT_byte_size
19648 attribute or the DW_AT_element_list attribute. */
19649 if (TYPE_SIZE (type))
19651 tree link;
19653 TREE_ASM_WRITTEN (type) = 1;
19654 add_byte_size_attribute (type_die, type);
19655 if (dwarf_version >= 3 || !dwarf_strict)
19657 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19658 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19659 context_die);
19661 if (TYPE_STUB_DECL (type) != NULL_TREE)
19663 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19664 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19667 /* If the first reference to this type was as the return type of an
19668 inline function, then it may not have a parent. Fix this now. */
19669 if (type_die->die_parent == NULL)
19670 add_child_die (scope_die_for (type, context_die), type_die);
19672 for (link = TYPE_VALUES (type);
19673 link != NULL; link = TREE_CHAIN (link))
19675 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19676 tree value = TREE_VALUE (link);
19678 add_name_attribute (enum_die,
19679 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19681 if (TREE_CODE (value) == CONST_DECL)
19682 value = DECL_INITIAL (value);
19684 if (simple_type_size_in_bits (TREE_TYPE (value))
19685 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19687 /* For constant forms created by add_AT_unsigned DWARF
19688 consumers (GDB, elfutils, etc.) always zero extend
19689 the value. Only when the actual value is negative
19690 do we need to use add_AT_int to generate a constant
19691 form that can represent negative values. */
19692 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19693 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19694 add_AT_unsigned (enum_die, DW_AT_const_value,
19695 (unsigned HOST_WIDE_INT) val);
19696 else
19697 add_AT_int (enum_die, DW_AT_const_value, val);
19699 else
19700 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
19701 that here. TODO: This should be re-worked to use correct
19702 signed/unsigned double tags for all cases. */
19703 add_AT_wide (enum_die, DW_AT_const_value, value);
19706 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19707 if (TYPE_ARTIFICIAL (type))
19708 add_AT_flag (type_die, DW_AT_artificial, 1);
19710 else
19711 add_AT_flag (type_die, DW_AT_declaration, 1);
19713 add_pubtype (type, type_die);
19715 return type_die;
19718 /* Generate a DIE to represent either a real live formal parameter decl or to
19719 represent just the type of some formal parameter position in some function
19720 type.
19722 Note that this routine is a bit unusual because its argument may be a
19723 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19724 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19725 node. If it's the former then this function is being called to output a
19726 DIE to represent a formal parameter object (or some inlining thereof). If
19727 it's the latter, then this function is only being called to output a
19728 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19729 argument type of some subprogram type.
19730 If EMIT_NAME_P is true, name and source coordinate attributes
19731 are emitted. */
19733 static dw_die_ref
19734 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19735 dw_die_ref context_die)
19737 tree node_or_origin = node ? node : origin;
19738 tree ultimate_origin;
19739 dw_die_ref parm_die = NULL;
19741 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19743 parm_die = lookup_decl_die (node);
19745 /* If the contexts differ, we may not be talking about the same
19746 thing. */
19747 if (parm_die && parm_die->die_parent != context_die)
19749 if (!DECL_ABSTRACT_P (node))
19751 /* This can happen when creating an inlined instance, in
19752 which case we need to create a new DIE that will get
19753 annotated with DW_AT_abstract_origin. */
19754 parm_die = NULL;
19756 else
19758 /* FIXME: Reuse DIE even with a differing context.
19760 This can happen when calling
19761 dwarf2out_abstract_function to build debug info for
19762 the abstract instance of a function for which we have
19763 already generated a DIE in
19764 dwarf2out_early_global_decl.
19766 Once we remove dwarf2out_abstract_function, we should
19767 have a call to gcc_unreachable here. */
19771 if (parm_die && parm_die->die_parent == NULL)
19773 /* Check that parm_die already has the right attributes that
19774 we would have added below. If any attributes are
19775 missing, fall through to add them. */
19776 if (! DECL_ABSTRACT_P (node_or_origin)
19777 && !get_AT (parm_die, DW_AT_location)
19778 && !get_AT (parm_die, DW_AT_const_value))
19779 /* We are missing location info, and are about to add it. */
19781 else
19783 add_child_die (context_die, parm_die);
19784 return parm_die;
19789 /* If we have a previously generated DIE, use it, unless this is an
19790 concrete instance (origin != NULL), in which case we need a new
19791 DIE with a corresponding DW_AT_abstract_origin. */
19792 bool reusing_die;
19793 if (parm_die && origin == NULL)
19794 reusing_die = true;
19795 else
19797 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19798 reusing_die = false;
19801 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19803 case tcc_declaration:
19804 ultimate_origin = decl_ultimate_origin (node_or_origin);
19805 if (node || ultimate_origin)
19806 origin = ultimate_origin;
19808 if (reusing_die)
19809 goto add_location;
19811 if (origin != NULL)
19812 add_abstract_origin_attribute (parm_die, origin);
19813 else if (emit_name_p)
19814 add_name_and_src_coords_attributes (parm_die, node);
19815 if (origin == NULL
19816 || (! DECL_ABSTRACT_P (node_or_origin)
19817 && variably_modified_type_p (TREE_TYPE (node_or_origin),
19818 decl_function_context
19819 (node_or_origin))))
19821 tree type = TREE_TYPE (node_or_origin);
19822 if (decl_by_reference_p (node_or_origin))
19823 add_type_attribute (parm_die, TREE_TYPE (type),
19824 TYPE_UNQUALIFIED,
19825 false, context_die);
19826 else
19827 add_type_attribute (parm_die, type,
19828 decl_quals (node_or_origin),
19829 false, context_die);
19831 if (origin == NULL && DECL_ARTIFICIAL (node))
19832 add_AT_flag (parm_die, DW_AT_artificial, 1);
19833 add_location:
19834 if (node && node != origin)
19835 equate_decl_number_to_die (node, parm_die);
19836 if (! DECL_ABSTRACT_P (node_or_origin))
19837 add_location_or_const_value_attribute (parm_die, node_or_origin,
19838 node == NULL);
19840 break;
19842 case tcc_type:
19843 /* We were called with some kind of a ..._TYPE node. */
19844 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19845 context_die);
19846 break;
19848 default:
19849 gcc_unreachable ();
19852 return parm_die;
19855 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19856 children DW_TAG_formal_parameter DIEs representing the arguments of the
19857 parameter pack.
19859 PARM_PACK must be a function parameter pack.
19860 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19861 must point to the subsequent arguments of the function PACK_ARG belongs to.
19862 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19863 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19864 following the last one for which a DIE was generated. */
19866 static dw_die_ref
19867 gen_formal_parameter_pack_die (tree parm_pack,
19868 tree pack_arg,
19869 dw_die_ref subr_die,
19870 tree *next_arg)
19872 tree arg;
19873 dw_die_ref parm_pack_die;
19875 gcc_assert (parm_pack
19876 && lang_hooks.function_parameter_pack_p (parm_pack)
19877 && subr_die);
19879 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19880 add_src_coords_attributes (parm_pack_die, parm_pack);
19882 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19884 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19885 parm_pack))
19886 break;
19887 gen_formal_parameter_die (arg, NULL,
19888 false /* Don't emit name attribute. */,
19889 parm_pack_die);
19891 if (next_arg)
19892 *next_arg = arg;
19893 return parm_pack_die;
19896 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19897 at the end of an (ANSI prototyped) formal parameters list. */
19899 static void
19900 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19902 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19905 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19906 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19907 parameters as specified in some function type specification (except for
19908 those which appear as part of a function *definition*). */
19910 static void
19911 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19913 tree link;
19914 tree formal_type = NULL;
19915 tree first_parm_type;
19916 tree arg;
19918 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19920 arg = DECL_ARGUMENTS (function_or_method_type);
19921 function_or_method_type = TREE_TYPE (function_or_method_type);
19923 else
19924 arg = NULL_TREE;
19926 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19928 /* Make our first pass over the list of formal parameter types and output a
19929 DW_TAG_formal_parameter DIE for each one. */
19930 for (link = first_parm_type; link; )
19932 dw_die_ref parm_die;
19934 formal_type = TREE_VALUE (link);
19935 if (formal_type == void_type_node)
19936 break;
19938 /* Output a (nameless) DIE to represent the formal parameter itself. */
19939 if (!POINTER_BOUNDS_TYPE_P (formal_type))
19941 parm_die = gen_formal_parameter_die (formal_type, NULL,
19942 true /* Emit name attribute. */,
19943 context_die);
19944 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19945 && link == first_parm_type)
19947 add_AT_flag (parm_die, DW_AT_artificial, 1);
19948 if (dwarf_version >= 3 || !dwarf_strict)
19949 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19951 else if (arg && DECL_ARTIFICIAL (arg))
19952 add_AT_flag (parm_die, DW_AT_artificial, 1);
19955 link = TREE_CHAIN (link);
19956 if (arg)
19957 arg = DECL_CHAIN (arg);
19960 /* If this function type has an ellipsis, add a
19961 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
19962 if (formal_type != void_type_node)
19963 gen_unspecified_parameters_die (function_or_method_type, context_die);
19965 /* Make our second (and final) pass over the list of formal parameter types
19966 and output DIEs to represent those types (as necessary). */
19967 for (link = TYPE_ARG_TYPES (function_or_method_type);
19968 link && TREE_VALUE (link);
19969 link = TREE_CHAIN (link))
19970 gen_type_die (TREE_VALUE (link), context_die);
19973 /* We want to generate the DIE for TYPE so that we can generate the
19974 die for MEMBER, which has been defined; we will need to refer back
19975 to the member declaration nested within TYPE. If we're trying to
19976 generate minimal debug info for TYPE, processing TYPE won't do the
19977 trick; we need to attach the member declaration by hand. */
19979 static void
19980 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19982 gen_type_die (type, context_die);
19984 /* If we're trying to avoid duplicate debug info, we may not have
19985 emitted the member decl for this function. Emit it now. */
19986 if (TYPE_STUB_DECL (type)
19987 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19988 && ! lookup_decl_die (member))
19990 dw_die_ref type_die;
19991 gcc_assert (!decl_ultimate_origin (member));
19993 push_decl_scope (type);
19994 type_die = lookup_type_die_strip_naming_typedef (type);
19995 if (TREE_CODE (member) == FUNCTION_DECL)
19996 gen_subprogram_die (member, type_die);
19997 else if (TREE_CODE (member) == FIELD_DECL)
19999 /* Ignore the nameless fields that are used to skip bits but handle
20000 C++ anonymous unions and structs. */
20001 if (DECL_NAME (member) != NULL_TREE
20002 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
20003 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
20005 struct vlr_context vlr_ctx = {
20006 DECL_CONTEXT (member), /* struct_type */
20007 NULL_TREE /* variant_part_offset */
20009 gen_type_die (member_declared_type (member), type_die);
20010 gen_field_die (member, &vlr_ctx, type_die);
20013 else
20014 gen_variable_die (member, NULL_TREE, type_die);
20016 pop_decl_scope ();
20020 /* Forward declare these functions, because they are mutually recursive
20021 with their set_block_* pairing functions. */
20022 static void set_decl_origin_self (tree);
20023 static void set_decl_abstract_flags (tree, vec<tree> &);
20025 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
20026 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
20027 that it points to the node itself, thus indicating that the node is its
20028 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
20029 the given node is NULL, recursively descend the decl/block tree which
20030 it is the root of, and for each other ..._DECL or BLOCK node contained
20031 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
20032 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
20033 values to point to themselves. */
20035 static void
20036 set_block_origin_self (tree stmt)
20038 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
20040 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
20043 tree local_decl;
20045 for (local_decl = BLOCK_VARS (stmt);
20046 local_decl != NULL_TREE;
20047 local_decl = DECL_CHAIN (local_decl))
20048 /* Do not recurse on nested functions since the inlining status
20049 of parent and child can be different as per the DWARF spec. */
20050 if (TREE_CODE (local_decl) != FUNCTION_DECL
20051 && !DECL_EXTERNAL (local_decl))
20052 set_decl_origin_self (local_decl);
20056 tree subblock;
20058 for (subblock = BLOCK_SUBBLOCKS (stmt);
20059 subblock != NULL_TREE;
20060 subblock = BLOCK_CHAIN (subblock))
20061 set_block_origin_self (subblock); /* Recurse. */
20066 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
20067 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
20068 node to so that it points to the node itself, thus indicating that the
20069 node represents its own (abstract) origin. Additionally, if the
20070 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
20071 the decl/block tree of which the given node is the root of, and for
20072 each other ..._DECL or BLOCK node contained therein whose
20073 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
20074 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
20075 point to themselves. */
20077 static void
20078 set_decl_origin_self (tree decl)
20080 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
20082 DECL_ABSTRACT_ORIGIN (decl) = decl;
20083 if (TREE_CODE (decl) == FUNCTION_DECL)
20085 tree arg;
20087 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20088 DECL_ABSTRACT_ORIGIN (arg) = arg;
20089 if (DECL_INITIAL (decl) != NULL_TREE
20090 && DECL_INITIAL (decl) != error_mark_node)
20091 set_block_origin_self (DECL_INITIAL (decl));
20096 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
20097 and if it wasn't 1 before, push it to abstract_vec vector.
20098 For all local decls and all local sub-blocks (recursively) do it
20099 too. */
20101 static void
20102 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
20104 tree local_decl;
20105 tree subblock;
20106 unsigned int i;
20108 if (!BLOCK_ABSTRACT (stmt))
20110 abstract_vec.safe_push (stmt);
20111 BLOCK_ABSTRACT (stmt) = 1;
20114 for (local_decl = BLOCK_VARS (stmt);
20115 local_decl != NULL_TREE;
20116 local_decl = DECL_CHAIN (local_decl))
20117 if (! DECL_EXTERNAL (local_decl))
20118 set_decl_abstract_flags (local_decl, abstract_vec);
20120 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20122 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
20123 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
20124 || TREE_CODE (local_decl) == PARM_DECL)
20125 set_decl_abstract_flags (local_decl, abstract_vec);
20128 for (subblock = BLOCK_SUBBLOCKS (stmt);
20129 subblock != NULL_TREE;
20130 subblock = BLOCK_CHAIN (subblock))
20131 set_block_abstract_flags (subblock, abstract_vec);
20134 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
20135 to 1 and if it wasn't 1 before, push to abstract_vec vector.
20136 In the case where the decl is a FUNCTION_DECL also set the abstract
20137 flags for all of the parameters, local vars, local
20138 blocks and sub-blocks (recursively). */
20140 static void
20141 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
20143 if (!DECL_ABSTRACT_P (decl))
20145 abstract_vec.safe_push (decl);
20146 DECL_ABSTRACT_P (decl) = 1;
20149 if (TREE_CODE (decl) == FUNCTION_DECL)
20151 tree arg;
20153 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20154 if (!DECL_ABSTRACT_P (arg))
20156 abstract_vec.safe_push (arg);
20157 DECL_ABSTRACT_P (arg) = 1;
20159 if (DECL_INITIAL (decl) != NULL_TREE
20160 && DECL_INITIAL (decl) != error_mark_node)
20161 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
20165 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20166 may later generate inlined and/or out-of-line instances of.
20168 FIXME: In the early-dwarf world, this function, and most of the
20169 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
20170 the abstract instance. All we would need to do is annotate
20171 the early DIE with the appropriate DW_AT_inline in late
20172 dwarf (perhaps in gen_inlined_subroutine_die).
20174 However, we can't do this yet, because LTO streaming of DIEs
20175 has not been implemented yet. */
20177 static void
20178 dwarf2out_abstract_function (tree decl)
20180 dw_die_ref old_die;
20181 tree save_fn;
20182 tree context;
20183 hash_table<decl_loc_hasher> *old_decl_loc_table;
20184 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20185 int old_call_site_count, old_tail_call_site_count;
20186 struct call_arg_loc_node *old_call_arg_locations;
20188 /* Make sure we have the actual abstract inline, not a clone. */
20189 decl = DECL_ORIGIN (decl);
20191 old_die = lookup_decl_die (decl);
20192 if (old_die && get_AT (old_die, DW_AT_inline))
20193 /* We've already generated the abstract instance. */
20194 return;
20196 /* We can be called while recursively when seeing block defining inlined subroutine
20197 DIE. Be sure to not clobber the outer location table nor use it or we would
20198 get locations in abstract instantces. */
20199 old_decl_loc_table = decl_loc_table;
20200 decl_loc_table = NULL;
20201 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20202 cached_dw_loc_list_table = NULL;
20203 old_call_arg_locations = call_arg_locations;
20204 call_arg_locations = NULL;
20205 old_call_site_count = call_site_count;
20206 call_site_count = -1;
20207 old_tail_call_site_count = tail_call_site_count;
20208 tail_call_site_count = -1;
20210 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20211 we don't get confused by DECL_ABSTRACT_P. */
20212 if (debug_info_level > DINFO_LEVEL_TERSE)
20214 context = decl_class_context (decl);
20215 if (context)
20216 gen_type_die_for_member
20217 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20220 /* Pretend we've just finished compiling this function. */
20221 save_fn = current_function_decl;
20222 current_function_decl = decl;
20224 auto_vec<tree, 64> abstract_vec;
20225 set_decl_abstract_flags (decl, abstract_vec);
20226 dwarf2out_decl (decl);
20227 unsigned int i;
20228 tree t;
20229 FOR_EACH_VEC_ELT (abstract_vec, i, t)
20230 if (TREE_CODE (t) == BLOCK)
20231 BLOCK_ABSTRACT (t) = 0;
20232 else
20233 DECL_ABSTRACT_P (t) = 0;
20235 current_function_decl = save_fn;
20236 decl_loc_table = old_decl_loc_table;
20237 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20238 call_arg_locations = old_call_arg_locations;
20239 call_site_count = old_call_site_count;
20240 tail_call_site_count = old_tail_call_site_count;
20243 /* Helper function of premark_used_types() which gets called through
20244 htab_traverse.
20246 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20247 marked as unused by prune_unused_types. */
20249 bool
20250 premark_used_types_helper (tree const &type, void *)
20252 dw_die_ref die;
20254 die = lookup_type_die (type);
20255 if (die != NULL)
20256 die->die_perennial_p = 1;
20257 return true;
20260 /* Helper function of premark_types_used_by_global_vars which gets called
20261 through htab_traverse.
20263 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20264 marked as unused by prune_unused_types. The DIE of the type is marked
20265 only if the global variable using the type will actually be emitted. */
20268 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20269 void *)
20271 struct types_used_by_vars_entry *entry;
20272 dw_die_ref die;
20274 entry = (struct types_used_by_vars_entry *) *slot;
20275 gcc_assert (entry->type != NULL
20276 && entry->var_decl != NULL);
20277 die = lookup_type_die (entry->type);
20278 if (die)
20280 /* Ask cgraph if the global variable really is to be emitted.
20281 If yes, then we'll keep the DIE of ENTRY->TYPE. */
20282 varpool_node *node = varpool_node::get (entry->var_decl);
20283 if (node && node->definition)
20285 die->die_perennial_p = 1;
20286 /* Keep the parent DIEs as well. */
20287 while ((die = die->die_parent) && die->die_perennial_p == 0)
20288 die->die_perennial_p = 1;
20291 return 1;
20294 /* Mark all members of used_types_hash as perennial. */
20296 static void
20297 premark_used_types (struct function *fun)
20299 if (fun && fun->used_types_hash)
20300 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20303 /* Mark all members of types_used_by_vars_entry as perennial. */
20305 static void
20306 premark_types_used_by_global_vars (void)
20308 if (types_used_by_vars_hash)
20309 types_used_by_vars_hash
20310 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20313 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20314 for CA_LOC call arg loc node. */
20316 static dw_die_ref
20317 gen_call_site_die (tree decl, dw_die_ref subr_die,
20318 struct call_arg_loc_node *ca_loc)
20320 dw_die_ref stmt_die = NULL, die;
20321 tree block = ca_loc->block;
20323 while (block
20324 && block != DECL_INITIAL (decl)
20325 && TREE_CODE (block) == BLOCK)
20327 stmt_die = BLOCK_DIE (block);
20328 if (stmt_die)
20329 break;
20330 block = BLOCK_SUPERCONTEXT (block);
20332 if (stmt_die == NULL)
20333 stmt_die = subr_die;
20334 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20335 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20336 if (ca_loc->tail_call_p)
20337 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20338 if (ca_loc->symbol_ref)
20340 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20341 if (tdie)
20342 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20343 else
20344 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20346 return die;
20349 /* Generate a DIE to represent a declared function (either file-scope or
20350 block-local). */
20352 static void
20353 gen_subprogram_die (tree decl, dw_die_ref context_die)
20355 tree origin = decl_ultimate_origin (decl);
20356 dw_die_ref subr_die;
20357 dw_die_ref old_die = lookup_decl_die (decl);
20359 /* This function gets called multiple times for different stages of
20360 the debug process. For example, for func() in this code:
20362 namespace S
20364 void func() { ... }
20367 ...we get called 4 times. Twice in early debug and twice in
20368 late debug:
20370 Early debug
20371 -----------
20373 1. Once while generating func() within the namespace. This is
20374 the declaration. The declaration bit below is set, as the
20375 context is the namespace.
20377 A new DIE will be generated with DW_AT_declaration set.
20379 2. Once for func() itself. This is the specification. The
20380 declaration bit below is clear as the context is the CU.
20382 We will use the cached DIE from (1) to create a new DIE with
20383 DW_AT_specification pointing to the declaration in (1).
20385 Late debug via rest_of_handle_final()
20386 -------------------------------------
20388 3. Once generating func() within the namespace. This is also the
20389 declaration, as in (1), but this time we will early exit below
20390 as we have a cached DIE and a declaration needs no additional
20391 annotations (no locations), as the source declaration line
20392 info is enough.
20394 4. Once for func() itself. As in (2), this is the specification,
20395 but this time we will re-use the cached DIE, and just annotate
20396 it with the location information that should now be available.
20398 For something without namespaces, but with abstract instances, we
20399 are also called a multiple times:
20401 class Base
20403 public:
20404 Base (); // constructor declaration (1)
20407 Base::Base () { } // constructor specification (2)
20409 Early debug
20410 -----------
20412 1. Once for the Base() constructor by virtue of it being a
20413 member of the Base class. This is done via
20414 rest_of_type_compilation.
20416 This is a declaration, so a new DIE will be created with
20417 DW_AT_declaration.
20419 2. Once for the Base() constructor definition, but this time
20420 while generating the abstract instance of the base
20421 constructor (__base_ctor) which is being generated via early
20422 debug of reachable functions.
20424 Even though we have a cached version of the declaration (1),
20425 we will create a DW_AT_specification of the declaration DIE
20426 in (1).
20428 3. Once for the __base_ctor itself, but this time, we generate
20429 an DW_AT_abstract_origin version of the DW_AT_specification in
20430 (2).
20432 Late debug via rest_of_handle_final
20433 -----------------------------------
20435 4. One final time for the __base_ctor (which will have a cached
20436 DIE with DW_AT_abstract_origin created in (3). This time,
20437 we will just annotate the location information now
20438 available.
20440 int declaration = (current_function_decl != decl
20441 || class_or_namespace_scope_p (context_die));
20443 premark_used_types (DECL_STRUCT_FUNCTION (decl));
20445 /* Now that the C++ front end lazily declares artificial member fns, we
20446 might need to retrofit the declaration into its class. */
20447 if (!declaration && !origin && !old_die
20448 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20449 && !class_or_namespace_scope_p (context_die)
20450 && debug_info_level > DINFO_LEVEL_TERSE)
20451 old_die = force_decl_die (decl);
20453 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
20454 if (origin != NULL)
20456 gcc_assert (!declaration || local_scope_p (context_die));
20458 /* Fixup die_parent for the abstract instance of a nested
20459 inline function. */
20460 if (old_die && old_die->die_parent == NULL)
20461 add_child_die (context_die, old_die);
20463 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20465 /* If we have a DW_AT_abstract_origin we have a working
20466 cached version. */
20467 subr_die = old_die;
20469 else
20471 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20472 add_abstract_origin_attribute (subr_die, origin);
20473 /* This is where the actual code for a cloned function is.
20474 Let's emit linkage name attribute for it. This helps
20475 debuggers to e.g, set breakpoints into
20476 constructors/destructors when the user asks "break
20477 K::K". */
20478 add_linkage_name (subr_die, decl);
20481 /* A cached copy, possibly from early dwarf generation. Reuse as
20482 much as possible. */
20483 else if (old_die)
20485 /* A declaration that has been previously dumped needs no
20486 additional information. */
20487 if (declaration)
20488 return;
20490 if (!get_AT_flag (old_die, DW_AT_declaration)
20491 /* We can have a normal definition following an inline one in the
20492 case of redefinition of GNU C extern inlines.
20493 It seems reasonable to use AT_specification in this case. */
20494 && !get_AT (old_die, DW_AT_inline))
20496 /* Detect and ignore this case, where we are trying to output
20497 something we have already output. */
20498 if (get_AT (old_die, DW_AT_low_pc)
20499 || get_AT (old_die, DW_AT_ranges))
20500 return;
20502 /* If we have no location information, this must be a
20503 partially generated DIE from early dwarf generation.
20504 Fall through and generate it. */
20507 /* If the definition comes from the same place as the declaration,
20508 maybe use the old DIE. We always want the DIE for this function
20509 that has the *_pc attributes to be under comp_unit_die so the
20510 debugger can find it. We also need to do this for abstract
20511 instances of inlines, since the spec requires the out-of-line copy
20512 to have the same parent. For local class methods, this doesn't
20513 apply; we just use the old DIE. */
20514 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20515 struct dwarf_file_data * file_index = lookup_filename (s.file);
20516 if ((is_cu_die (old_die->die_parent)
20517 /* This condition fixes the inconsistency/ICE with the
20518 following Fortran test (or some derivative thereof) while
20519 building libgfortran:
20521 module some_m
20522 contains
20523 logical function funky (FLAG)
20524 funky = .true.
20525 end function
20526 end module
20528 || (old_die->die_parent
20529 && old_die->die_parent->die_tag == DW_TAG_module)
20530 || context_die == NULL)
20531 && (DECL_ARTIFICIAL (decl)
20532 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20533 && (get_AT_unsigned (old_die, DW_AT_decl_line)
20534 == (unsigned) s.line))))
20536 subr_die = old_die;
20538 /* Clear out the declaration attribute, but leave the
20539 parameters so they can be augmented with location
20540 information later. Unless this was a declaration, in
20541 which case, wipe out the nameless parameters and recreate
20542 them further down. */
20543 if (remove_AT (subr_die, DW_AT_declaration))
20546 remove_AT (subr_die, DW_AT_object_pointer);
20547 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20550 /* Make a specification pointing to the previously built
20551 declaration. */
20552 else
20554 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20555 add_AT_specification (subr_die, old_die);
20556 add_pubname (decl, subr_die);
20557 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20558 add_AT_file (subr_die, DW_AT_decl_file, file_index);
20559 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20560 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20562 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20563 emit the real type on the definition die. */
20564 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20566 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20567 if (die == auto_die || die == decltype_auto_die)
20568 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20569 TYPE_UNQUALIFIED, false, context_die);
20572 /* When we process the method declaration, we haven't seen
20573 the out-of-class defaulted definition yet, so we have to
20574 recheck now. */
20575 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20576 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict)
20577 && !get_AT (subr_die, DW_AT_defaulted))
20578 switch (defaulted)
20580 case 2:
20581 add_AT_unsigned (subr_die, DW_AT_defaulted,
20582 DW_DEFAULTED_out_of_class);
20583 break;
20585 case 1: /* This must have been handled before. */
20586 default:
20587 gcc_unreachable ();
20591 /* Create a fresh DIE for anything else. */
20592 else
20594 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20596 if (TREE_PUBLIC (decl))
20597 add_AT_flag (subr_die, DW_AT_external, 1);
20599 add_name_and_src_coords_attributes (subr_die, decl);
20600 add_pubname (decl, subr_die);
20601 if (debug_info_level > DINFO_LEVEL_TERSE)
20603 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20604 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20605 TYPE_UNQUALIFIED, false, context_die);
20608 add_pure_or_virtual_attribute (subr_die, decl);
20609 if (DECL_ARTIFICIAL (decl))
20610 add_AT_flag (subr_die, DW_AT_artificial, 1);
20612 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20613 add_AT_flag (subr_die, DW_AT_noreturn, 1);
20615 add_accessibility_attribute (subr_die, decl);
20618 /* Unless we have an existing non-declaration DIE, equate the new
20619 DIE. */
20620 if (!old_die || is_declaration_die (old_die))
20621 equate_decl_number_to_die (decl, subr_die);
20623 if (declaration)
20625 if (!old_die || !get_AT (old_die, DW_AT_inline))
20627 add_AT_flag (subr_die, DW_AT_declaration, 1);
20629 /* If this is an explicit function declaration then generate
20630 a DW_AT_explicit attribute. */
20631 if (lang_hooks.decls.function_decl_explicit_p (decl)
20632 && (dwarf_version >= 3 || !dwarf_strict))
20633 add_AT_flag (subr_die, DW_AT_explicit, 1);
20635 /* If this is a C++11 deleted special function member then generate
20636 a DW_AT_deleted attribute. */
20637 if (lang_hooks.decls.function_decl_deleted_p (decl)
20638 && (dwarf_version >= 5 || ! dwarf_strict))
20639 add_AT_flag (subr_die, DW_AT_deleted, 1);
20641 /* If this is a C++11 defaulted special function member then
20642 generate a DW_AT_GNU_defaulted attribute. */
20643 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20644 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict))
20645 switch (defaulted)
20647 case 1:
20648 add_AT_unsigned (subr_die, DW_AT_defaulted,
20649 DW_DEFAULTED_in_class);
20650 break;
20652 /* It is likely that this will never hit, since we
20653 don't have the out-of-class definition yet when we
20654 process the class definition and the method
20655 declaration. We recheck elsewhere, but leave it
20656 here just in case. */
20657 case 2:
20658 add_AT_unsigned (subr_die, DW_AT_defaulted,
20659 DW_DEFAULTED_out_of_class);
20660 break;
20662 default:
20663 gcc_unreachable ();
20667 /* Tag abstract instances with DW_AT_inline. */
20668 else if (DECL_ABSTRACT_P (decl))
20670 if (DECL_DECLARED_INLINE_P (decl))
20672 if (cgraph_function_possibly_inlined_p (decl))
20673 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20674 else
20675 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20677 else
20679 if (cgraph_function_possibly_inlined_p (decl))
20680 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20681 else
20682 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20685 if (DECL_DECLARED_INLINE_P (decl)
20686 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20687 add_AT_flag (subr_die, DW_AT_artificial, 1);
20689 /* For non DECL_EXTERNALs, if range information is available, fill
20690 the DIE with it. */
20691 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20693 HOST_WIDE_INT cfa_fb_offset;
20695 struct function *fun = DECL_STRUCT_FUNCTION (decl);
20697 if (!flag_reorder_blocks_and_partition)
20699 dw_fde_ref fde = fun->fde;
20700 if (fde->dw_fde_begin)
20702 /* We have already generated the labels. */
20703 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20704 fde->dw_fde_end, false);
20706 else
20708 /* Create start/end labels and add the range. */
20709 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20710 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20711 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20712 current_function_funcdef_no);
20713 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20714 current_function_funcdef_no);
20715 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20716 false);
20719 #if VMS_DEBUGGING_INFO
20720 /* HP OpenVMS Industry Standard 64: DWARF Extensions
20721 Section 2.3 Prologue and Epilogue Attributes:
20722 When a breakpoint is set on entry to a function, it is generally
20723 desirable for execution to be suspended, not on the very first
20724 instruction of the function, but rather at a point after the
20725 function's frame has been set up, after any language defined local
20726 declaration processing has been completed, and before execution of
20727 the first statement of the function begins. Debuggers generally
20728 cannot properly determine where this point is. Similarly for a
20729 breakpoint set on exit from a function. The prologue and epilogue
20730 attributes allow a compiler to communicate the location(s) to use. */
20733 if (fde->dw_fde_vms_end_prologue)
20734 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20735 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20737 if (fde->dw_fde_vms_begin_epilogue)
20738 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20739 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20741 #endif
20744 else
20746 /* Generate pubnames entries for the split function code ranges. */
20747 dw_fde_ref fde = fun->fde;
20749 if (fde->dw_fde_second_begin)
20751 if (dwarf_version >= 3 || !dwarf_strict)
20753 /* We should use ranges for non-contiguous code section
20754 addresses. Use the actual code range for the initial
20755 section, since the HOT/COLD labels might precede an
20756 alignment offset. */
20757 bool range_list_added = false;
20758 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20759 fde->dw_fde_end, &range_list_added,
20760 false);
20761 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20762 fde->dw_fde_second_end,
20763 &range_list_added, false);
20764 if (range_list_added)
20765 add_ranges (NULL);
20767 else
20769 /* There is no real support in DW2 for this .. so we make
20770 a work-around. First, emit the pub name for the segment
20771 containing the function label. Then make and emit a
20772 simplified subprogram DIE for the second segment with the
20773 name pre-fixed by __hot/cold_sect_of_. We use the same
20774 linkage name for the second die so that gdb will find both
20775 sections when given "b foo". */
20776 const char *name = NULL;
20777 tree decl_name = DECL_NAME (decl);
20778 dw_die_ref seg_die;
20780 /* Do the 'primary' section. */
20781 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20782 fde->dw_fde_end, false);
20784 /* Build a minimal DIE for the secondary section. */
20785 seg_die = new_die (DW_TAG_subprogram,
20786 subr_die->die_parent, decl);
20788 if (TREE_PUBLIC (decl))
20789 add_AT_flag (seg_die, DW_AT_external, 1);
20791 if (decl_name != NULL
20792 && IDENTIFIER_POINTER (decl_name) != NULL)
20794 name = dwarf2_name (decl, 1);
20795 if (! DECL_ARTIFICIAL (decl))
20796 add_src_coords_attributes (seg_die, decl);
20798 add_linkage_name (seg_die, decl);
20800 gcc_assert (name != NULL);
20801 add_pure_or_virtual_attribute (seg_die, decl);
20802 if (DECL_ARTIFICIAL (decl))
20803 add_AT_flag (seg_die, DW_AT_artificial, 1);
20805 name = concat ("__second_sect_of_", name, NULL);
20806 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20807 fde->dw_fde_second_end, false);
20808 add_name_attribute (seg_die, name);
20809 if (want_pubnames ())
20810 add_pubname_string (name, seg_die);
20813 else
20814 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20815 false);
20818 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20820 /* We define the "frame base" as the function's CFA. This is more
20821 convenient for several reasons: (1) It's stable across the prologue
20822 and epilogue, which makes it better than just a frame pointer,
20823 (2) With dwarf3, there exists a one-byte encoding that allows us
20824 to reference the .debug_frame data by proxy, but failing that,
20825 (3) We can at least reuse the code inspection and interpretation
20826 code that determines the CFA position at various points in the
20827 function. */
20828 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20830 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20831 add_AT_loc (subr_die, DW_AT_frame_base, op);
20833 else
20835 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20836 if (list->dw_loc_next)
20837 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20838 else
20839 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20842 /* Compute a displacement from the "steady-state frame pointer" to
20843 the CFA. The former is what all stack slots and argument slots
20844 will reference in the rtl; the latter is what we've told the
20845 debugger about. We'll need to adjust all frame_base references
20846 by this displacement. */
20847 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20849 if (fun->static_chain_decl)
20851 /* DWARF requires here a location expression that computes the
20852 address of the enclosing subprogram's frame base. The machinery
20853 in tree-nested.c is supposed to store this specific address in the
20854 last field of the FRAME record. */
20855 const tree frame_type
20856 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20857 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20859 tree fb_expr
20860 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20861 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20862 fb_expr, fb_decl, NULL_TREE);
20864 add_AT_location_description (subr_die, DW_AT_static_link,
20865 loc_list_from_tree (fb_expr, 0, NULL));
20869 /* Generate child dies for template paramaters. */
20870 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20871 gen_generic_params_dies (decl);
20873 /* Now output descriptions of the arguments for this function. This gets
20874 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20875 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20876 `...' at the end of the formal parameter list. In order to find out if
20877 there was a trailing ellipsis or not, we must instead look at the type
20878 associated with the FUNCTION_DECL. This will be a node of type
20879 FUNCTION_TYPE. If the chain of type nodes hanging off of this
20880 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20881 an ellipsis at the end. */
20883 /* In the case where we are describing a mere function declaration, all we
20884 need to do here (and all we *can* do here) is to describe the *types* of
20885 its formal parameters. */
20886 if (debug_info_level <= DINFO_LEVEL_TERSE)
20888 else if (declaration)
20889 gen_formal_types_die (decl, subr_die);
20890 else
20892 /* Generate DIEs to represent all known formal parameters. */
20893 tree parm = DECL_ARGUMENTS (decl);
20894 tree generic_decl = early_dwarf
20895 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20896 tree generic_decl_parm = generic_decl
20897 ? DECL_ARGUMENTS (generic_decl)
20898 : NULL;
20899 auto_vec<dw_die_ref> string_types_vec;
20900 if (string_types == NULL)
20901 string_types = &string_types_vec;
20903 /* Now we want to walk the list of parameters of the function and
20904 emit their relevant DIEs.
20906 We consider the case of DECL being an instance of a generic function
20907 as well as it being a normal function.
20909 If DECL is an instance of a generic function we walk the
20910 parameters of the generic function declaration _and_ the parameters of
20911 DECL itself. This is useful because we want to emit specific DIEs for
20912 function parameter packs and those are declared as part of the
20913 generic function declaration. In that particular case,
20914 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20915 That DIE has children DIEs representing the set of arguments
20916 of the pack. Note that the set of pack arguments can be empty.
20917 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20918 children DIE.
20920 Otherwise, we just consider the parameters of DECL. */
20921 while (generic_decl_parm || parm)
20923 if (generic_decl_parm
20924 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20925 gen_formal_parameter_pack_die (generic_decl_parm,
20926 parm, subr_die,
20927 &parm);
20928 else if (parm && !POINTER_BOUNDS_P (parm))
20930 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20932 if (parm == DECL_ARGUMENTS (decl)
20933 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20934 && parm_die
20935 && (dwarf_version >= 3 || !dwarf_strict))
20936 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20938 parm = DECL_CHAIN (parm);
20940 else if (parm)
20941 parm = DECL_CHAIN (parm);
20943 if (generic_decl_parm)
20944 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20947 /* Decide whether we need an unspecified_parameters DIE at the end.
20948 There are 2 more cases to do this for: 1) the ansi ... declaration -
20949 this is detectable when the end of the arg list is not a
20950 void_type_node 2) an unprototyped function declaration (not a
20951 definition). This just means that we have no info about the
20952 parameters at all. */
20953 if (early_dwarf)
20955 if (prototype_p (TREE_TYPE (decl)))
20957 /* This is the prototyped case, check for.... */
20958 if (stdarg_p (TREE_TYPE (decl)))
20959 gen_unspecified_parameters_die (decl, subr_die);
20961 else if (DECL_INITIAL (decl) == NULL_TREE)
20962 gen_unspecified_parameters_die (decl, subr_die);
20965 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
20966 have DIEs. */
20967 if (string_types == &string_types_vec)
20969 adjust_string_types ();
20970 string_types = NULL;
20974 if (subr_die != old_die)
20975 /* Add the calling convention attribute if requested. */
20976 add_calling_convention_attribute (subr_die, decl);
20978 /* Output Dwarf info for all of the stuff within the body of the function
20979 (if it has one - it may be just a declaration).
20981 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20982 a function. This BLOCK actually represents the outermost binding contour
20983 for the function, i.e. the contour in which the function's formal
20984 parameters and labels get declared. Curiously, it appears that the front
20985 end doesn't actually put the PARM_DECL nodes for the current function onto
20986 the BLOCK_VARS list for this outer scope, but are strung off of the
20987 DECL_ARGUMENTS list for the function instead.
20989 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20990 the LABEL_DECL nodes for the function however, and we output DWARF info
20991 for those in decls_for_scope. Just within the `outer_scope' there will be
20992 a BLOCK node representing the function's outermost pair of curly braces,
20993 and any blocks used for the base and member initializers of a C++
20994 constructor function. */
20995 tree outer_scope = DECL_INITIAL (decl);
20996 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
20998 int call_site_note_count = 0;
20999 int tail_call_site_note_count = 0;
21001 /* Emit a DW_TAG_variable DIE for a named return value. */
21002 if (DECL_NAME (DECL_RESULT (decl)))
21003 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
21005 /* The first time through decls_for_scope we will generate the
21006 DIEs for the locals. The second time, we fill in the
21007 location info. */
21008 decls_for_scope (outer_scope, subr_die);
21010 if (call_arg_locations && !dwarf_strict)
21012 struct call_arg_loc_node *ca_loc;
21013 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
21015 dw_die_ref die = NULL;
21016 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
21017 rtx arg, next_arg;
21019 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
21020 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
21021 : NULL_RTX);
21022 arg; arg = next_arg)
21024 dw_loc_descr_ref reg, val;
21025 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
21026 dw_die_ref cdie, tdie = NULL;
21028 next_arg = XEXP (arg, 1);
21029 if (REG_P (XEXP (XEXP (arg, 0), 0))
21030 && next_arg
21031 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
21032 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
21033 && REGNO (XEXP (XEXP (arg, 0), 0))
21034 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
21035 next_arg = XEXP (next_arg, 1);
21036 if (mode == VOIDmode)
21038 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
21039 if (mode == VOIDmode)
21040 mode = GET_MODE (XEXP (arg, 0));
21042 if (mode == VOIDmode || mode == BLKmode)
21043 continue;
21044 /* Get dynamic information about call target only if we
21045 have no static information: we cannot generate both
21046 DW_AT_abstract_origin and DW_AT_GNU_call_site_target
21047 attributes. */
21048 if (ca_loc->symbol_ref == NULL_RTX)
21050 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
21052 tloc = XEXP (XEXP (arg, 0), 1);
21053 continue;
21055 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
21056 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
21058 tlocc = XEXP (XEXP (arg, 0), 1);
21059 continue;
21062 reg = NULL;
21063 if (REG_P (XEXP (XEXP (arg, 0), 0)))
21064 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
21065 VAR_INIT_STATUS_INITIALIZED);
21066 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
21068 rtx mem = XEXP (XEXP (arg, 0), 0);
21069 reg = mem_loc_descriptor (XEXP (mem, 0),
21070 get_address_mode (mem),
21071 GET_MODE (mem),
21072 VAR_INIT_STATUS_INITIALIZED);
21074 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
21075 == DEBUG_PARAMETER_REF)
21077 tree tdecl
21078 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
21079 tdie = lookup_decl_die (tdecl);
21080 if (tdie == NULL)
21081 continue;
21083 else
21084 continue;
21085 if (reg == NULL
21086 && GET_CODE (XEXP (XEXP (arg, 0), 0))
21087 != DEBUG_PARAMETER_REF)
21088 continue;
21089 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
21090 VOIDmode,
21091 VAR_INIT_STATUS_INITIALIZED);
21092 if (val == NULL)
21093 continue;
21094 if (die == NULL)
21095 die = gen_call_site_die (decl, subr_die, ca_loc);
21096 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
21097 NULL_TREE);
21098 if (reg != NULL)
21099 add_AT_loc (cdie, DW_AT_location, reg);
21100 else if (tdie != NULL)
21101 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
21102 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
21103 if (next_arg != XEXP (arg, 1))
21105 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
21106 if (mode == VOIDmode)
21107 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
21108 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
21109 0), 1),
21110 mode, VOIDmode,
21111 VAR_INIT_STATUS_INITIALIZED);
21112 if (val != NULL)
21113 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
21116 if (die == NULL
21117 && (ca_loc->symbol_ref || tloc))
21118 die = gen_call_site_die (decl, subr_die, ca_loc);
21119 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
21121 dw_loc_descr_ref tval = NULL;
21123 if (tloc != NULL_RTX)
21124 tval = mem_loc_descriptor (tloc,
21125 GET_MODE (tloc) == VOIDmode
21126 ? Pmode : GET_MODE (tloc),
21127 VOIDmode,
21128 VAR_INIT_STATUS_INITIALIZED);
21129 if (tval)
21130 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
21131 else if (tlocc != NULL_RTX)
21133 tval = mem_loc_descriptor (tlocc,
21134 GET_MODE (tlocc) == VOIDmode
21135 ? Pmode : GET_MODE (tlocc),
21136 VOIDmode,
21137 VAR_INIT_STATUS_INITIALIZED);
21138 if (tval)
21139 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
21140 tval);
21143 if (die != NULL)
21145 call_site_note_count++;
21146 if (ca_loc->tail_call_p)
21147 tail_call_site_note_count++;
21151 call_arg_locations = NULL;
21152 call_arg_loc_last = NULL;
21153 if (tail_call_site_count >= 0
21154 && tail_call_site_count == tail_call_site_note_count
21155 && !dwarf_strict)
21157 if (call_site_count >= 0
21158 && call_site_count == call_site_note_count)
21159 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
21160 else
21161 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
21163 call_site_count = -1;
21164 tail_call_site_count = -1;
21168 /* Returns a hash value for X (which really is a die_struct). */
21170 hashval_t
21171 block_die_hasher::hash (die_struct *d)
21173 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
21176 /* Return nonzero if decl_id and die_parent of die_struct X is the same
21177 as decl_id and die_parent of die_struct Y. */
21179 bool
21180 block_die_hasher::equal (die_struct *x, die_struct *y)
21182 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
21185 /* Return TRUE if DECL, which may have been previously generated as
21186 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
21187 true if decl (or its origin) is either an extern declaration or a
21188 class/namespace scoped declaration.
21190 The declare_in_namespace support causes us to get two DIEs for one
21191 variable, both of which are declarations. We want to avoid
21192 considering one to be a specification, so we must test for
21193 DECLARATION and DW_AT_declaration. */
21194 static inline bool
21195 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
21197 return (old_die && TREE_STATIC (decl) && !declaration
21198 && get_AT_flag (old_die, DW_AT_declaration) == 1);
21201 /* Return true if DECL is a local static. */
21203 static inline bool
21204 local_function_static (tree decl)
21206 gcc_assert (TREE_CODE (decl) == VAR_DECL);
21207 return TREE_STATIC (decl)
21208 && DECL_CONTEXT (decl)
21209 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
21212 /* Generate a DIE to represent a declared data object.
21213 Either DECL or ORIGIN must be non-null. */
21215 static void
21216 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
21218 HOST_WIDE_INT off = 0;
21219 tree com_decl;
21220 tree decl_or_origin = decl ? decl : origin;
21221 tree ultimate_origin;
21222 dw_die_ref var_die;
21223 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
21224 dw_die_ref origin_die = NULL;
21225 bool declaration = (DECL_EXTERNAL (decl_or_origin)
21226 || class_or_namespace_scope_p (context_die));
21227 bool specialization_p = false;
21229 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21230 if (decl || ultimate_origin)
21231 origin = ultimate_origin;
21232 com_decl = fortran_common (decl_or_origin, &off);
21234 /* Symbol in common gets emitted as a child of the common block, in the form
21235 of a data member. */
21236 if (com_decl)
21238 dw_die_ref com_die;
21239 dw_loc_list_ref loc;
21240 die_node com_die_arg;
21242 var_die = lookup_decl_die (decl_or_origin);
21243 if (var_die)
21245 if (get_AT (var_die, DW_AT_location) == NULL)
21247 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21248 if (loc)
21250 if (off)
21252 /* Optimize the common case. */
21253 if (single_element_loc_list_p (loc)
21254 && loc->expr->dw_loc_opc == DW_OP_addr
21255 && loc->expr->dw_loc_next == NULL
21256 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21257 == SYMBOL_REF)
21259 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21260 loc->expr->dw_loc_oprnd1.v.val_addr
21261 = plus_constant (GET_MODE (x), x , off);
21263 else
21264 loc_list_plus_const (loc, off);
21266 add_AT_location_description (var_die, DW_AT_location, loc);
21267 remove_AT (var_die, DW_AT_declaration);
21270 return;
21273 if (common_block_die_table == NULL)
21274 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21276 com_die_arg.decl_id = DECL_UID (com_decl);
21277 com_die_arg.die_parent = context_die;
21278 com_die = common_block_die_table->find (&com_die_arg);
21279 loc = loc_list_from_tree (com_decl, 2, NULL);
21280 if (com_die == NULL)
21282 const char *cnam
21283 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21284 die_node **slot;
21286 com_die = new_die (DW_TAG_common_block, context_die, decl);
21287 add_name_and_src_coords_attributes (com_die, com_decl);
21288 if (loc)
21290 add_AT_location_description (com_die, DW_AT_location, loc);
21291 /* Avoid sharing the same loc descriptor between
21292 DW_TAG_common_block and DW_TAG_variable. */
21293 loc = loc_list_from_tree (com_decl, 2, NULL);
21295 else if (DECL_EXTERNAL (decl_or_origin))
21296 add_AT_flag (com_die, DW_AT_declaration, 1);
21297 if (want_pubnames ())
21298 add_pubname_string (cnam, com_die); /* ??? needed? */
21299 com_die->decl_id = DECL_UID (com_decl);
21300 slot = common_block_die_table->find_slot (com_die, INSERT);
21301 *slot = com_die;
21303 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21305 add_AT_location_description (com_die, DW_AT_location, loc);
21306 loc = loc_list_from_tree (com_decl, 2, NULL);
21307 remove_AT (com_die, DW_AT_declaration);
21309 var_die = new_die (DW_TAG_variable, com_die, decl);
21310 add_name_and_src_coords_attributes (var_die, decl_or_origin);
21311 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21312 decl_quals (decl_or_origin), false,
21313 context_die);
21314 add_AT_flag (var_die, DW_AT_external, 1);
21315 if (loc)
21317 if (off)
21319 /* Optimize the common case. */
21320 if (single_element_loc_list_p (loc)
21321 && loc->expr->dw_loc_opc == DW_OP_addr
21322 && loc->expr->dw_loc_next == NULL
21323 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21325 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21326 loc->expr->dw_loc_oprnd1.v.val_addr
21327 = plus_constant (GET_MODE (x), x, off);
21329 else
21330 loc_list_plus_const (loc, off);
21332 add_AT_location_description (var_die, DW_AT_location, loc);
21334 else if (DECL_EXTERNAL (decl_or_origin))
21335 add_AT_flag (var_die, DW_AT_declaration, 1);
21336 if (decl)
21337 equate_decl_number_to_die (decl, var_die);
21338 return;
21341 if (old_die)
21343 if (declaration)
21345 /* A declaration that has been previously dumped, needs no
21346 further annotations, since it doesn't need location on
21347 the second pass. */
21348 return;
21350 else if (decl_will_get_specification_p (old_die, decl, declaration)
21351 && !get_AT (old_die, DW_AT_specification))
21353 /* Fall-thru so we can make a new variable die along with a
21354 DW_AT_specification. */
21356 else if (origin && old_die->die_parent != context_die)
21358 /* If we will be creating an inlined instance, we need a
21359 new DIE that will get annotated with
21360 DW_AT_abstract_origin. Clear things so we can get a
21361 new DIE. */
21362 gcc_assert (!DECL_ABSTRACT_P (decl));
21363 old_die = NULL;
21365 else
21367 /* If a DIE was dumped early, it still needs location info.
21368 Skip to where we fill the location bits. */
21369 var_die = old_die;
21370 goto gen_variable_die_location;
21374 /* For static data members, the declaration in the class is supposed
21375 to have DW_TAG_member tag; the specification should still be
21376 DW_TAG_variable referencing the DW_TAG_member DIE. */
21377 if (declaration && class_scope_p (context_die))
21378 var_die = new_die (DW_TAG_member, context_die, decl);
21379 else
21380 var_die = new_die (DW_TAG_variable, context_die, decl);
21382 if (origin != NULL)
21383 origin_die = add_abstract_origin_attribute (var_die, origin);
21385 /* Loop unrolling can create multiple blocks that refer to the same
21386 static variable, so we must test for the DW_AT_declaration flag.
21388 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21389 copy decls and set the DECL_ABSTRACT_P flag on them instead of
21390 sharing them.
21392 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
21393 else if (decl_will_get_specification_p (old_die, decl, declaration))
21395 /* This is a definition of a C++ class level static. */
21396 add_AT_specification (var_die, old_die);
21397 specialization_p = true;
21398 if (DECL_NAME (decl))
21400 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21401 struct dwarf_file_data * file_index = lookup_filename (s.file);
21403 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21404 add_AT_file (var_die, DW_AT_decl_file, file_index);
21406 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21407 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21409 if (old_die->die_tag == DW_TAG_member)
21410 add_linkage_name (var_die, decl);
21413 else
21414 add_name_and_src_coords_attributes (var_die, decl);
21416 if ((origin == NULL && !specialization_p)
21417 || (origin != NULL
21418 && !DECL_ABSTRACT_P (decl_or_origin)
21419 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21420 decl_function_context
21421 (decl_or_origin))))
21423 tree type = TREE_TYPE (decl_or_origin);
21425 if (decl_by_reference_p (decl_or_origin))
21426 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21427 context_die);
21428 else
21429 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21430 context_die);
21433 if (origin == NULL && !specialization_p)
21435 if (TREE_PUBLIC (decl))
21436 add_AT_flag (var_die, DW_AT_external, 1);
21438 if (DECL_ARTIFICIAL (decl))
21439 add_AT_flag (var_die, DW_AT_artificial, 1);
21441 add_accessibility_attribute (var_die, decl);
21444 if (declaration)
21445 add_AT_flag (var_die, DW_AT_declaration, 1);
21447 if (decl && (DECL_ABSTRACT_P (decl)
21448 || !old_die || is_declaration_die (old_die)))
21449 equate_decl_number_to_die (decl, var_die);
21451 gen_variable_die_location:
21452 if (! declaration
21453 && (! DECL_ABSTRACT_P (decl_or_origin)
21454 /* Local static vars are shared between all clones/inlines,
21455 so emit DW_AT_location on the abstract DIE if DECL_RTL is
21456 already set. */
21457 || (TREE_CODE (decl_or_origin) == VAR_DECL
21458 && TREE_STATIC (decl_or_origin)
21459 && DECL_RTL_SET_P (decl_or_origin)))
21460 /* When abstract origin already has DW_AT_location attribute, no need
21461 to add it again. */
21462 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21464 if (early_dwarf)
21465 add_pubname (decl_or_origin, var_die);
21466 else
21467 add_location_or_const_value_attribute (var_die, decl_or_origin,
21468 decl == NULL);
21470 else
21471 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21474 /* Generate a DIE to represent a named constant. */
21476 static void
21477 gen_const_die (tree decl, dw_die_ref context_die)
21479 dw_die_ref const_die;
21480 tree type = TREE_TYPE (decl);
21482 const_die = lookup_decl_die (decl);
21483 if (const_die)
21484 return;
21486 const_die = new_die (DW_TAG_constant, context_die, decl);
21487 equate_decl_number_to_die (decl, const_die);
21488 add_name_and_src_coords_attributes (const_die, decl);
21489 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21490 if (TREE_PUBLIC (decl))
21491 add_AT_flag (const_die, DW_AT_external, 1);
21492 if (DECL_ARTIFICIAL (decl))
21493 add_AT_flag (const_die, DW_AT_artificial, 1);
21494 tree_add_const_value_attribute_for_decl (const_die, decl);
21497 /* Generate a DIE to represent a label identifier. */
21499 static void
21500 gen_label_die (tree decl, dw_die_ref context_die)
21502 tree origin = decl_ultimate_origin (decl);
21503 dw_die_ref lbl_die = lookup_decl_die (decl);
21504 rtx insn;
21505 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21507 if (!lbl_die)
21509 lbl_die = new_die (DW_TAG_label, context_die, decl);
21510 equate_decl_number_to_die (decl, lbl_die);
21512 if (origin != NULL)
21513 add_abstract_origin_attribute (lbl_die, origin);
21514 else
21515 add_name_and_src_coords_attributes (lbl_die, decl);
21518 if (DECL_ABSTRACT_P (decl))
21519 equate_decl_number_to_die (decl, lbl_die);
21520 else
21522 insn = DECL_RTL_IF_SET (decl);
21524 /* Deleted labels are programmer specified labels which have been
21525 eliminated because of various optimizations. We still emit them
21526 here so that it is possible to put breakpoints on them. */
21527 if (insn
21528 && (LABEL_P (insn)
21529 || ((NOTE_P (insn)
21530 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21532 /* When optimization is enabled (via -O) some parts of the compiler
21533 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21534 represent source-level labels which were explicitly declared by
21535 the user. This really shouldn't be happening though, so catch
21536 it if it ever does happen. */
21537 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21539 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21540 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21542 else if (insn
21543 && NOTE_P (insn)
21544 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21545 && CODE_LABEL_NUMBER (insn) != -1)
21547 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21548 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21553 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
21554 attributes to the DIE for a block STMT, to describe where the inlined
21555 function was called from. This is similar to add_src_coords_attributes. */
21557 static inline void
21558 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21560 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21562 if (dwarf_version >= 3 || !dwarf_strict)
21564 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21565 add_AT_unsigned (die, DW_AT_call_line, s.line);
21570 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21571 Add low_pc and high_pc attributes to the DIE for a block STMT. */
21573 static inline void
21574 add_high_low_attributes (tree stmt, dw_die_ref die)
21576 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21578 if (BLOCK_FRAGMENT_CHAIN (stmt)
21579 && (dwarf_version >= 3 || !dwarf_strict))
21581 tree chain, superblock = NULL_TREE;
21582 dw_die_ref pdie;
21583 dw_attr_node *attr = NULL;
21585 if (inlined_function_outer_scope_p (stmt))
21587 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21588 BLOCK_NUMBER (stmt));
21589 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21592 /* Optimize duplicate .debug_ranges lists or even tails of
21593 lists. If this BLOCK has same ranges as its supercontext,
21594 lookup DW_AT_ranges attribute in the supercontext (and
21595 recursively so), verify that the ranges_table contains the
21596 right values and use it instead of adding a new .debug_range. */
21597 for (chain = stmt, pdie = die;
21598 BLOCK_SAME_RANGE (chain);
21599 chain = BLOCK_SUPERCONTEXT (chain))
21601 dw_attr_node *new_attr;
21603 pdie = pdie->die_parent;
21604 if (pdie == NULL)
21605 break;
21606 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21607 break;
21608 new_attr = get_AT (pdie, DW_AT_ranges);
21609 if (new_attr == NULL
21610 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21611 break;
21612 attr = new_attr;
21613 superblock = BLOCK_SUPERCONTEXT (chain);
21615 if (attr != NULL
21616 && (ranges_table[attr->dw_attr_val.v.val_offset
21617 / 2 / DWARF2_ADDR_SIZE].num
21618 == BLOCK_NUMBER (superblock))
21619 && BLOCK_FRAGMENT_CHAIN (superblock))
21621 unsigned long off = attr->dw_attr_val.v.val_offset
21622 / 2 / DWARF2_ADDR_SIZE;
21623 unsigned long supercnt = 0, thiscnt = 0;
21624 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21625 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21627 ++supercnt;
21628 gcc_checking_assert (ranges_table[off + supercnt].num
21629 == BLOCK_NUMBER (chain));
21631 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21632 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21633 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21634 ++thiscnt;
21635 gcc_assert (supercnt >= thiscnt);
21636 add_AT_range_list (die, DW_AT_ranges,
21637 ((off + supercnt - thiscnt)
21638 * 2 * DWARF2_ADDR_SIZE),
21639 false);
21640 return;
21643 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21645 chain = BLOCK_FRAGMENT_CHAIN (stmt);
21648 add_ranges (chain);
21649 chain = BLOCK_FRAGMENT_CHAIN (chain);
21651 while (chain);
21652 add_ranges (NULL);
21654 else
21656 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21657 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21658 BLOCK_NUMBER (stmt));
21659 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21660 BLOCK_NUMBER (stmt));
21661 add_AT_low_high_pc (die, label, label_high, false);
21665 /* Generate a DIE for a lexical block. */
21667 static void
21668 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21670 dw_die_ref old_die = BLOCK_DIE (stmt);
21671 dw_die_ref stmt_die = NULL;
21672 if (!old_die)
21674 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21675 BLOCK_DIE (stmt) = stmt_die;
21678 if (BLOCK_ABSTRACT (stmt))
21680 if (old_die)
21682 /* This must have been generated early and it won't even
21683 need location information since it's a DW_AT_inline
21684 function. */
21685 if (flag_checking)
21686 for (dw_die_ref c = context_die; c; c = c->die_parent)
21687 if (c->die_tag == DW_TAG_inlined_subroutine
21688 || c->die_tag == DW_TAG_subprogram)
21690 gcc_assert (get_AT (c, DW_AT_inline));
21691 break;
21693 return;
21696 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21698 /* If this is an inlined instance, create a new lexical die for
21699 anything below to attach DW_AT_abstract_origin to. */
21700 if (old_die)
21702 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21703 BLOCK_DIE (stmt) = stmt_die;
21704 old_die = NULL;
21707 tree origin = block_ultimate_origin (stmt);
21708 if (origin != NULL_TREE && origin != stmt)
21709 add_abstract_origin_attribute (stmt_die, origin);
21712 if (old_die)
21713 stmt_die = old_die;
21715 /* A non abstract block whose blocks have already been reordered
21716 should have the instruction range for this block. If so, set the
21717 high/low attributes. */
21718 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21720 gcc_assert (stmt_die);
21721 add_high_low_attributes (stmt, stmt_die);
21724 decls_for_scope (stmt, stmt_die);
21727 /* Generate a DIE for an inlined subprogram. */
21729 static void
21730 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21732 tree decl;
21734 /* The instance of function that is effectively being inlined shall not
21735 be abstract. */
21736 gcc_assert (! BLOCK_ABSTRACT (stmt));
21738 decl = block_ultimate_origin (stmt);
21740 /* Make sure any inlined functions are known to be inlineable. */
21741 gcc_checking_assert (DECL_ABSTRACT_P (decl)
21742 || cgraph_function_possibly_inlined_p (decl));
21744 /* Emit info for the abstract instance first, if we haven't yet. We
21745 must emit this even if the block is abstract, otherwise when we
21746 emit the block below (or elsewhere), we may end up trying to emit
21747 a die whose origin die hasn't been emitted, and crashing. */
21748 dwarf2out_abstract_function (decl);
21750 if (! BLOCK_ABSTRACT (stmt))
21752 dw_die_ref subr_die
21753 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21755 if (call_arg_locations)
21756 BLOCK_DIE (stmt) = subr_die;
21757 add_abstract_origin_attribute (subr_die, decl);
21758 if (TREE_ASM_WRITTEN (stmt))
21759 add_high_low_attributes (stmt, subr_die);
21760 add_call_src_coords_attributes (stmt, subr_die);
21762 decls_for_scope (stmt, subr_die);
21766 /* Generate a DIE for a field in a record, or structure. CTX is required: see
21767 the comment for VLR_CONTEXT. */
21769 static void
21770 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21772 dw_die_ref decl_die;
21774 if (TREE_TYPE (decl) == error_mark_node)
21775 return;
21777 decl_die = new_die (DW_TAG_member, context_die, decl);
21778 add_name_and_src_coords_attributes (decl_die, decl);
21779 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21780 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21781 context_die);
21783 if (DECL_BIT_FIELD_TYPE (decl))
21785 add_byte_size_attribute (decl_die, decl);
21786 add_bit_size_attribute (decl_die, decl);
21787 add_bit_offset_attribute (decl_die, decl, ctx);
21790 /* If we have a variant part offset, then we are supposed to process a member
21791 of a QUAL_UNION_TYPE, which is how we represent variant parts in
21792 trees. */
21793 gcc_assert (ctx->variant_part_offset == NULL_TREE
21794 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21795 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21796 add_data_member_location_attribute (decl_die, decl, ctx);
21798 if (DECL_ARTIFICIAL (decl))
21799 add_AT_flag (decl_die, DW_AT_artificial, 1);
21801 add_accessibility_attribute (decl_die, decl);
21803 /* Equate decl number to die, so that we can look up this decl later on. */
21804 equate_decl_number_to_die (decl, decl_die);
21807 #if 0
21808 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21809 Use modified_type_die instead.
21810 We keep this code here just in case these types of DIEs may be needed to
21811 represent certain things in other languages (e.g. Pascal) someday. */
21813 static void
21814 gen_pointer_type_die (tree type, dw_die_ref context_die)
21816 dw_die_ref ptr_die
21817 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21819 equate_type_number_to_die (type, ptr_die);
21820 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21821 context_die);
21822 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21825 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21826 Use modified_type_die instead.
21827 We keep this code here just in case these types of DIEs may be needed to
21828 represent certain things in other languages (e.g. Pascal) someday. */
21830 static void
21831 gen_reference_type_die (tree type, dw_die_ref context_die)
21833 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21835 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21836 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21837 else
21838 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21840 equate_type_number_to_die (type, ref_die);
21841 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21842 context_die);
21843 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21845 #endif
21847 /* Generate a DIE for a pointer to a member type. */
21849 static void
21850 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21852 dw_die_ref ptr_die
21853 = new_die (DW_TAG_ptr_to_member_type,
21854 scope_die_for (type, context_die), type);
21856 equate_type_number_to_die (type, ptr_die);
21857 add_AT_die_ref (ptr_die, DW_AT_containing_type,
21858 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21859 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21860 context_die);
21863 static char *producer_string;
21865 /* Return a heap allocated producer string including command line options
21866 if -grecord-gcc-switches. */
21868 static char *
21869 gen_producer_string (void)
21871 size_t j;
21872 auto_vec<const char *> switches;
21873 const char *language_string = lang_hooks.name;
21874 char *producer, *tail;
21875 const char *p;
21876 size_t len = dwarf_record_gcc_switches ? 0 : 3;
21877 size_t plen = strlen (language_string) + 1 + strlen (version_string);
21879 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21880 switch (save_decoded_options[j].opt_index)
21882 case OPT_o:
21883 case OPT_d:
21884 case OPT_dumpbase:
21885 case OPT_dumpdir:
21886 case OPT_auxbase:
21887 case OPT_auxbase_strip:
21888 case OPT_quiet:
21889 case OPT_version:
21890 case OPT_v:
21891 case OPT_w:
21892 case OPT_L:
21893 case OPT_D:
21894 case OPT_I:
21895 case OPT_U:
21896 case OPT_SPECIAL_unknown:
21897 case OPT_SPECIAL_ignore:
21898 case OPT_SPECIAL_program_name:
21899 case OPT_SPECIAL_input_file:
21900 case OPT_grecord_gcc_switches:
21901 case OPT_gno_record_gcc_switches:
21902 case OPT__output_pch_:
21903 case OPT_fdiagnostics_show_location_:
21904 case OPT_fdiagnostics_show_option:
21905 case OPT_fdiagnostics_show_caret:
21906 case OPT_fdiagnostics_color_:
21907 case OPT_fverbose_asm:
21908 case OPT____:
21909 case OPT__sysroot_:
21910 case OPT_nostdinc:
21911 case OPT_nostdinc__:
21912 case OPT_fpreprocessed:
21913 case OPT_fltrans_output_list_:
21914 case OPT_fresolution_:
21915 case OPT_fdebug_prefix_map_:
21916 /* Ignore these. */
21917 continue;
21918 default:
21919 if (cl_options[save_decoded_options[j].opt_index].flags
21920 & CL_NO_DWARF_RECORD)
21921 continue;
21922 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21923 == '-');
21924 switch (save_decoded_options[j].canonical_option[0][1])
21926 case 'M':
21927 case 'i':
21928 case 'W':
21929 continue;
21930 case 'f':
21931 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21932 "dump", 4) == 0)
21933 continue;
21934 break;
21935 default:
21936 break;
21938 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21939 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21940 break;
21943 producer = XNEWVEC (char, plen + 1 + len + 1);
21944 tail = producer;
21945 sprintf (tail, "%s %s", language_string, version_string);
21946 tail += plen;
21948 FOR_EACH_VEC_ELT (switches, j, p)
21950 len = strlen (p);
21951 *tail = ' ';
21952 memcpy (tail + 1, p, len);
21953 tail += len + 1;
21956 *tail = '\0';
21957 return producer;
21960 /* Given a C and/or C++ language/version string return the "highest".
21961 C++ is assumed to be "higher" than C in this case. Used for merging
21962 LTO translation unit languages. */
21963 static const char *
21964 highest_c_language (const char *lang1, const char *lang2)
21966 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21967 return "GNU C++14";
21968 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21969 return "GNU C++11";
21970 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21971 return "GNU C++98";
21973 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21974 return "GNU C11";
21975 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
21976 return "GNU C99";
21977 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
21978 return "GNU C89";
21980 gcc_unreachable ();
21984 /* Generate the DIE for the compilation unit. */
21986 static dw_die_ref
21987 gen_compile_unit_die (const char *filename)
21989 dw_die_ref die;
21990 const char *language_string = lang_hooks.name;
21991 int language;
21993 die = new_die (DW_TAG_compile_unit, NULL, NULL);
21995 if (filename)
21997 add_name_attribute (die, filename);
21998 /* Don't add cwd for <built-in>. */
21999 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
22000 add_comp_dir_attribute (die);
22003 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
22005 /* If our producer is LTO try to figure out a common language to use
22006 from the global list of translation units. */
22007 if (strcmp (language_string, "GNU GIMPLE") == 0)
22009 unsigned i;
22010 tree t;
22011 const char *common_lang = NULL;
22013 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
22015 if (!TRANSLATION_UNIT_LANGUAGE (t))
22016 continue;
22017 if (!common_lang)
22018 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
22019 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
22021 else if (strncmp (common_lang, "GNU C", 5) == 0
22022 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
22023 /* Mixing C and C++ is ok, use C++ in that case. */
22024 common_lang = highest_c_language (common_lang,
22025 TRANSLATION_UNIT_LANGUAGE (t));
22026 else
22028 /* Fall back to C. */
22029 common_lang = NULL;
22030 break;
22034 if (common_lang)
22035 language_string = common_lang;
22038 language = DW_LANG_C;
22039 if (strncmp (language_string, "GNU C", 5) == 0
22040 && ISDIGIT (language_string[5]))
22042 language = DW_LANG_C89;
22043 if (dwarf_version >= 3 || !dwarf_strict)
22045 if (strcmp (language_string, "GNU C89") != 0)
22046 language = DW_LANG_C99;
22048 if (dwarf_version >= 5 /* || !dwarf_strict */)
22049 if (strcmp (language_string, "GNU C11") == 0)
22050 language = DW_LANG_C11;
22053 else if (strncmp (language_string, "GNU C++", 7) == 0)
22055 language = DW_LANG_C_plus_plus;
22056 if (dwarf_version >= 5 /* || !dwarf_strict */)
22058 if (strcmp (language_string, "GNU C++11") == 0)
22059 language = DW_LANG_C_plus_plus_11;
22060 else if (strcmp (language_string, "GNU C++14") == 0)
22061 language = DW_LANG_C_plus_plus_14;
22064 else if (strcmp (language_string, "GNU F77") == 0)
22065 language = DW_LANG_Fortran77;
22066 else if (strcmp (language_string, "GNU Pascal") == 0)
22067 language = DW_LANG_Pascal83;
22068 else if (dwarf_version >= 3 || !dwarf_strict)
22070 if (strcmp (language_string, "GNU Ada") == 0)
22071 language = DW_LANG_Ada95;
22072 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22074 language = DW_LANG_Fortran95;
22075 if (dwarf_version >= 5 /* || !dwarf_strict */)
22077 if (strcmp (language_string, "GNU Fortran2003") == 0)
22078 language = DW_LANG_Fortran03;
22079 else if (strcmp (language_string, "GNU Fortran2008") == 0)
22080 language = DW_LANG_Fortran08;
22083 else if (strcmp (language_string, "GNU Java") == 0)
22084 language = DW_LANG_Java;
22085 else if (strcmp (language_string, "GNU Objective-C") == 0)
22086 language = DW_LANG_ObjC;
22087 else if (strcmp (language_string, "GNU Objective-C++") == 0)
22088 language = DW_LANG_ObjC_plus_plus;
22089 else if (dwarf_version >= 5 || !dwarf_strict)
22091 if (strcmp (language_string, "GNU Go") == 0)
22092 language = DW_LANG_Go;
22095 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
22096 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22097 language = DW_LANG_Fortran90;
22099 add_AT_unsigned (die, DW_AT_language, language);
22101 switch (language)
22103 case DW_LANG_Fortran77:
22104 case DW_LANG_Fortran90:
22105 case DW_LANG_Fortran95:
22106 case DW_LANG_Fortran03:
22107 case DW_LANG_Fortran08:
22108 /* Fortran has case insensitive identifiers and the front-end
22109 lowercases everything. */
22110 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
22111 break;
22112 default:
22113 /* The default DW_ID_case_sensitive doesn't need to be specified. */
22114 break;
22116 return die;
22119 /* Generate the DIE for a base class. */
22121 static void
22122 gen_inheritance_die (tree binfo, tree access, tree type,
22123 dw_die_ref context_die)
22125 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
22126 struct vlr_context ctx = { type, NULL };
22128 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
22129 context_die);
22130 add_data_member_location_attribute (die, binfo, &ctx);
22132 if (BINFO_VIRTUAL_P (binfo))
22133 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22135 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
22136 children, otherwise the default is DW_ACCESS_public. In DWARF2
22137 the default has always been DW_ACCESS_private. */
22138 if (access == access_public_node)
22140 if (dwarf_version == 2
22141 || context_die->die_tag == DW_TAG_class_type)
22142 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
22144 else if (access == access_protected_node)
22145 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
22146 else if (dwarf_version > 2
22147 && context_die->die_tag != DW_TAG_class_type)
22148 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
22151 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
22152 structure. */
22153 static bool
22154 is_variant_part (tree decl)
22156 return (TREE_CODE (decl) == FIELD_DECL
22157 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
22160 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
22161 return the FIELD_DECL. Return NULL_TREE otherwise. */
22163 static tree
22164 analyze_discr_in_predicate (tree operand, tree struct_type)
22166 bool continue_stripping = true;
22167 while (continue_stripping)
22168 switch (TREE_CODE (operand))
22170 CASE_CONVERT:
22171 operand = TREE_OPERAND (operand, 0);
22172 break;
22173 default:
22174 continue_stripping = false;
22175 break;
22178 /* Match field access to members of struct_type only. */
22179 if (TREE_CODE (operand) == COMPONENT_REF
22180 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
22181 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
22182 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
22183 return TREE_OPERAND (operand, 1);
22184 else
22185 return NULL_TREE;
22188 /* Check that SRC is a constant integer that can be represented as a native
22189 integer constant (either signed or unsigned). If so, store it into DEST and
22190 return true. Return false otherwise. */
22192 static bool
22193 get_discr_value (tree src, dw_discr_value *dest)
22195 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
22197 if (TREE_CODE (src) != INTEGER_CST
22198 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
22199 return false;
22201 dest->pos = is_unsigned;
22202 if (is_unsigned)
22203 dest->v.uval = tree_to_uhwi (src);
22204 else
22205 dest->v.sval = tree_to_shwi (src);
22207 return true;
22210 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
22211 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
22212 store NULL_TREE in DISCR_DECL. Otherwise:
22214 - store the discriminant field in STRUCT_TYPE that controls the variant
22215 part to *DISCR_DECL
22217 - put in *DISCR_LISTS_P an array where for each variant, the item
22218 represents the corresponding matching list of discriminant values.
22220 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
22221 the above array.
22223 Note that when the array is allocated (i.e. when the analysis is
22224 successful), it is up to the caller to free the array. */
22226 static void
22227 analyze_variants_discr (tree variant_part_decl,
22228 tree struct_type,
22229 tree *discr_decl,
22230 dw_discr_list_ref **discr_lists_p,
22231 unsigned *discr_lists_length)
22233 tree variant_part_type = TREE_TYPE (variant_part_decl);
22234 tree variant;
22235 dw_discr_list_ref *discr_lists;
22236 unsigned i;
22238 /* Compute how many variants there are in this variant part. */
22239 *discr_lists_length = 0;
22240 for (variant = TYPE_FIELDS (variant_part_type);
22241 variant != NULL_TREE;
22242 variant = DECL_CHAIN (variant))
22243 ++*discr_lists_length;
22245 *discr_decl = NULL_TREE;
22246 *discr_lists_p
22247 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22248 sizeof (**discr_lists_p));
22249 discr_lists = *discr_lists_p;
22251 /* And then analyze all variants to extract discriminant information for all
22252 of them. This analysis is conservative: as soon as we detect something we
22253 do not support, abort everything and pretend we found nothing. */
22254 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22255 variant != NULL_TREE;
22256 variant = DECL_CHAIN (variant), ++i)
22258 tree match_expr = DECL_QUALIFIER (variant);
22260 /* Now, try to analyze the predicate and deduce a discriminant for
22261 it. */
22262 if (match_expr == boolean_true_node)
22263 /* Typically happens for the default variant: it matches all cases that
22264 previous variants rejected. Don't output any matching value for
22265 this one. */
22266 continue;
22268 /* The following loop tries to iterate over each discriminant
22269 possibility: single values or ranges. */
22270 while (match_expr != NULL_TREE)
22272 tree next_round_match_expr;
22273 tree candidate_discr = NULL_TREE;
22274 dw_discr_list_ref new_node = NULL;
22276 /* Possibilities are matched one after the other by nested
22277 TRUTH_ORIF_EXPR expressions. Process the current possibility and
22278 continue with the rest at next iteration. */
22279 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22281 next_round_match_expr = TREE_OPERAND (match_expr, 0);
22282 match_expr = TREE_OPERAND (match_expr, 1);
22284 else
22285 next_round_match_expr = NULL_TREE;
22287 if (match_expr == boolean_false_node)
22288 /* This sub-expression matches nothing: just wait for the next
22289 one. */
22292 else if (TREE_CODE (match_expr) == EQ_EXPR)
22294 /* We are matching: <discr_field> == <integer_cst>
22295 This sub-expression matches a single value. */
22296 tree integer_cst = TREE_OPERAND (match_expr, 1);
22298 candidate_discr
22299 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22300 struct_type);
22302 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22303 if (!get_discr_value (integer_cst,
22304 &new_node->dw_discr_lower_bound))
22305 goto abort;
22306 new_node->dw_discr_range = false;
22309 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22311 /* We are matching:
22312 <discr_field> > <integer_cst>
22313 && <discr_field> < <integer_cst>.
22314 This sub-expression matches the range of values between the
22315 two matched integer constants. Note that comparisons can be
22316 inclusive or exclusive. */
22317 tree candidate_discr_1, candidate_discr_2;
22318 tree lower_cst, upper_cst;
22319 bool lower_cst_included, upper_cst_included;
22320 tree lower_op = TREE_OPERAND (match_expr, 0);
22321 tree upper_op = TREE_OPERAND (match_expr, 1);
22323 /* When the comparison is exclusive, the integer constant is not
22324 the discriminant range bound we are looking for: we will have
22325 to increment or decrement it. */
22326 if (TREE_CODE (lower_op) == GE_EXPR)
22327 lower_cst_included = true;
22328 else if (TREE_CODE (lower_op) == GT_EXPR)
22329 lower_cst_included = false;
22330 else
22331 goto abort;
22333 if (TREE_CODE (upper_op) == LE_EXPR)
22334 upper_cst_included = true;
22335 else if (TREE_CODE (upper_op) == LT_EXPR)
22336 upper_cst_included = false;
22337 else
22338 goto abort;
22340 /* Extract the discriminant from the first operand and check it
22341 is consistant with the same analysis in the second
22342 operand. */
22343 candidate_discr_1
22344 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22345 struct_type);
22346 candidate_discr_2
22347 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22348 struct_type);
22349 if (candidate_discr_1 == candidate_discr_2)
22350 candidate_discr = candidate_discr_1;
22351 else
22352 goto abort;
22354 /* Extract bounds from both. */
22355 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22356 lower_cst = TREE_OPERAND (lower_op, 1);
22357 upper_cst = TREE_OPERAND (upper_op, 1);
22359 if (!lower_cst_included)
22360 lower_cst
22361 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22362 lower_cst,
22363 build_int_cst (TREE_TYPE (lower_cst), 1)));
22364 if (!upper_cst_included)
22365 upper_cst
22366 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22367 upper_cst,
22368 build_int_cst (TREE_TYPE (upper_cst), 1)));
22370 if (!get_discr_value (lower_cst,
22371 &new_node->dw_discr_lower_bound)
22372 || !get_discr_value (upper_cst,
22373 &new_node->dw_discr_upper_bound))
22374 goto abort;
22376 new_node->dw_discr_range = true;
22379 else
22380 /* Unsupported sub-expression: we cannot determine the set of
22381 matching discriminant values. Abort everything. */
22382 goto abort;
22384 /* If the discriminant info is not consistant with what we saw so
22385 far, consider the analysis failed and abort everything. */
22386 if (candidate_discr == NULL_TREE
22387 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22388 goto abort;
22389 else
22390 *discr_decl = candidate_discr;
22392 if (new_node != NULL)
22394 new_node->dw_discr_next = discr_lists[i];
22395 discr_lists[i] = new_node;
22397 match_expr = next_round_match_expr;
22401 /* If we reach this point, we could match everything we were interested
22402 in. */
22403 return;
22405 abort:
22406 /* Clean all data structure and return no result. */
22407 free (*discr_lists_p);
22408 *discr_lists_p = NULL;
22409 *discr_decl = NULL_TREE;
22412 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22413 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
22414 under CONTEXT_DIE.
22416 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22417 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
22418 this type, which are record types, represent the available variants and each
22419 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
22420 values are inferred from these attributes.
22422 In trees, the offsets for the fields inside these sub-records are relative
22423 to the variant part itself, whereas the corresponding DIEs should have
22424 offset attributes that are relative to the embedding record base address.
22425 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22426 must be an expression that computes the offset of the variant part to
22427 describe in DWARF. */
22429 static void
22430 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22431 dw_die_ref context_die)
22433 const tree variant_part_type = TREE_TYPE (variant_part_decl);
22434 tree variant_part_offset = vlr_ctx->variant_part_offset;
22435 struct loc_descr_context ctx = {
22436 vlr_ctx->struct_type, /* context_type */
22437 NULL_TREE, /* base_decl */
22438 NULL /* dpi */
22441 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22442 NULL_TREE if there is no such field. */
22443 tree discr_decl = NULL_TREE;
22444 dw_discr_list_ref *discr_lists;
22445 unsigned discr_lists_length = 0;
22446 unsigned i;
22448 dw_die_ref dwarf_proc_die = NULL;
22449 dw_die_ref variant_part_die
22450 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22452 equate_decl_number_to_die (variant_part_decl, variant_part_die);
22454 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22455 &discr_decl, &discr_lists, &discr_lists_length);
22457 if (discr_decl != NULL_TREE)
22459 dw_die_ref discr_die = lookup_decl_die (discr_decl);
22461 if (discr_die)
22462 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22463 else
22464 /* We have no DIE for the discriminant, so just discard all
22465 discrimimant information in the output. */
22466 discr_decl = NULL_TREE;
22469 /* If the offset for this variant part is more complex than a constant,
22470 create a DWARF procedure for it so that we will not have to generate DWARF
22471 expressions for it for each member. */
22472 if (TREE_CODE (variant_part_offset) != INTEGER_CST
22473 && (dwarf_version >= 3 || !dwarf_strict))
22475 const tree dwarf_proc_fndecl
22476 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22477 build_function_type (TREE_TYPE (variant_part_offset),
22478 NULL_TREE));
22479 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22480 const dw_loc_descr_ref dwarf_proc_body
22481 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22483 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22484 dwarf_proc_fndecl, context_die);
22485 if (dwarf_proc_die != NULL)
22486 variant_part_offset = dwarf_proc_call;
22489 /* Output DIEs for all variants. */
22490 i = 0;
22491 for (tree variant = TYPE_FIELDS (variant_part_type);
22492 variant != NULL_TREE;
22493 variant = DECL_CHAIN (variant), ++i)
22495 tree variant_type = TREE_TYPE (variant);
22496 dw_die_ref variant_die;
22498 /* All variants (i.e. members of a variant part) are supposed to be
22499 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
22500 under these records. */
22501 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22503 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22504 equate_decl_number_to_die (variant, variant_die);
22506 /* Output discriminant values this variant matches, if any. */
22507 if (discr_decl == NULL || discr_lists[i] == NULL)
22508 /* In the case we have discriminant information at all, this is
22509 probably the default variant: as the standard says, don't
22510 output any discriminant value/list attribute. */
22512 else if (discr_lists[i]->dw_discr_next == NULL
22513 && !discr_lists[i]->dw_discr_range)
22514 /* If there is only one accepted value, don't bother outputting a
22515 list. */
22516 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22517 else
22518 add_discr_list (variant_die, discr_lists[i]);
22520 for (tree member = TYPE_FIELDS (variant_type);
22521 member != NULL_TREE;
22522 member = DECL_CHAIN (member))
22524 struct vlr_context vlr_sub_ctx = {
22525 vlr_ctx->struct_type, /* struct_type */
22526 NULL /* variant_part_offset */
22528 if (is_variant_part (member))
22530 /* All offsets for fields inside variant parts are relative to
22531 the top-level embedding RECORD_TYPE's base address. On the
22532 other hand, offsets in GCC's types are relative to the
22533 nested-most variant part. So we have to sum offsets each time
22534 we recurse. */
22536 vlr_sub_ctx.variant_part_offset
22537 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22538 variant_part_offset, byte_position (member)));
22539 gen_variant_part (member, &vlr_sub_ctx, variant_die);
22541 else
22543 vlr_sub_ctx.variant_part_offset = variant_part_offset;
22544 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22549 free (discr_lists);
22552 /* Generate a DIE for a class member. */
22554 static void
22555 gen_member_die (tree type, dw_die_ref context_die)
22557 tree member;
22558 tree binfo = TYPE_BINFO (type);
22559 dw_die_ref child;
22561 /* If this is not an incomplete type, output descriptions of each of its
22562 members. Note that as we output the DIEs necessary to represent the
22563 members of this record or union type, we will also be trying to output
22564 DIEs to represent the *types* of those members. However the `type'
22565 function (above) will specifically avoid generating type DIEs for member
22566 types *within* the list of member DIEs for this (containing) type except
22567 for those types (of members) which are explicitly marked as also being
22568 members of this (containing) type themselves. The g++ front- end can
22569 force any given type to be treated as a member of some other (containing)
22570 type by setting the TYPE_CONTEXT of the given (member) type to point to
22571 the TREE node representing the appropriate (containing) type. */
22573 /* First output info about the base classes. */
22574 if (binfo)
22576 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22577 int i;
22578 tree base;
22580 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22581 gen_inheritance_die (base,
22582 (accesses ? (*accesses)[i] : access_public_node),
22583 type,
22584 context_die);
22587 /* Now output info about the data members and type members. */
22588 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22590 struct vlr_context vlr_ctx = { type, NULL_TREE };
22592 /* If we thought we were generating minimal debug info for TYPE
22593 and then changed our minds, some of the member declarations
22594 may have already been defined. Don't define them again, but
22595 do put them in the right order. */
22597 child = lookup_decl_die (member);
22598 if (child)
22599 splice_child_die (context_die, child);
22601 /* Do not generate standard DWARF for variant parts if we are generating
22602 the corresponding GNAT encodings: DIEs generated for both would
22603 conflict in our mappings. */
22604 else if (is_variant_part (member)
22605 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22607 vlr_ctx.variant_part_offset = byte_position (member);
22608 gen_variant_part (member, &vlr_ctx, context_die);
22610 else
22612 vlr_ctx.variant_part_offset = NULL_TREE;
22613 gen_decl_die (member, NULL, &vlr_ctx, context_die);
22617 /* We do not keep type methods in type variants. */
22618 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22619 /* Now output info about the function members (if any). */
22620 if (TYPE_METHODS (type) != error_mark_node)
22621 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22623 /* Don't include clones in the member list. */
22624 if (DECL_ABSTRACT_ORIGIN (member))
22625 continue;
22626 /* Nor constructors for anonymous classes. */
22627 if (DECL_ARTIFICIAL (member)
22628 && dwarf2_name (member, 0) == NULL)
22629 continue;
22631 child = lookup_decl_die (member);
22632 if (child)
22633 splice_child_die (context_die, child);
22634 else
22635 gen_decl_die (member, NULL, NULL, context_die);
22639 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
22640 is set, we pretend that the type was never defined, so we only get the
22641 member DIEs needed by later specification DIEs. */
22643 static void
22644 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22645 enum debug_info_usage usage)
22647 if (TREE_ASM_WRITTEN (type))
22649 /* Fill in the bound of variable-length fields in late dwarf if
22650 still incomplete. */
22651 if (!early_dwarf && variably_modified_type_p (type, NULL))
22652 for (tree member = TYPE_FIELDS (type);
22653 member;
22654 member = DECL_CHAIN (member))
22655 fill_variable_array_bounds (TREE_TYPE (member));
22656 return;
22659 dw_die_ref type_die = lookup_type_die (type);
22660 dw_die_ref scope_die = 0;
22661 int nested = 0;
22662 int complete = (TYPE_SIZE (type)
22663 && (! TYPE_STUB_DECL (type)
22664 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22665 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22666 complete = complete && should_emit_struct_debug (type, usage);
22668 if (type_die && ! complete)
22669 return;
22671 if (TYPE_CONTEXT (type) != NULL_TREE
22672 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22673 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22674 nested = 1;
22676 scope_die = scope_die_for (type, context_die);
22678 /* Generate child dies for template paramaters. */
22679 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22680 schedule_generic_params_dies_gen (type);
22682 if (! type_die || (nested && is_cu_die (scope_die)))
22683 /* First occurrence of type or toplevel definition of nested class. */
22685 dw_die_ref old_die = type_die;
22687 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22688 ? record_type_tag (type) : DW_TAG_union_type,
22689 scope_die, type);
22690 equate_type_number_to_die (type, type_die);
22691 if (old_die)
22692 add_AT_specification (type_die, old_die);
22693 else
22694 add_name_attribute (type_die, type_tag (type));
22696 else
22697 remove_AT (type_die, DW_AT_declaration);
22699 /* If this type has been completed, then give it a byte_size attribute and
22700 then give a list of members. */
22701 if (complete && !ns_decl)
22703 /* Prevent infinite recursion in cases where the type of some member of
22704 this type is expressed in terms of this type itself. */
22705 TREE_ASM_WRITTEN (type) = 1;
22706 add_byte_size_attribute (type_die, type);
22707 if (TYPE_STUB_DECL (type) != NULL_TREE)
22709 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22710 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22713 /* If the first reference to this type was as the return type of an
22714 inline function, then it may not have a parent. Fix this now. */
22715 if (type_die->die_parent == NULL)
22716 add_child_die (scope_die, type_die);
22718 push_decl_scope (type);
22719 gen_member_die (type, type_die);
22720 pop_decl_scope ();
22722 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22723 if (TYPE_ARTIFICIAL (type))
22724 add_AT_flag (type_die, DW_AT_artificial, 1);
22726 /* GNU extension: Record what type our vtable lives in. */
22727 if (TYPE_VFIELD (type))
22729 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22731 gen_type_die (vtype, context_die);
22732 add_AT_die_ref (type_die, DW_AT_containing_type,
22733 lookup_type_die (vtype));
22736 else
22738 add_AT_flag (type_die, DW_AT_declaration, 1);
22740 /* We don't need to do this for function-local types. */
22741 if (TYPE_STUB_DECL (type)
22742 && ! decl_function_context (TYPE_STUB_DECL (type)))
22743 vec_safe_push (incomplete_types, type);
22746 if (get_AT (type_die, DW_AT_name))
22747 add_pubtype (type, type_die);
22750 /* Generate a DIE for a subroutine _type_. */
22752 static void
22753 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22755 tree return_type = TREE_TYPE (type);
22756 dw_die_ref subr_die
22757 = new_die (DW_TAG_subroutine_type,
22758 scope_die_for (type, context_die), type);
22760 equate_type_number_to_die (type, subr_die);
22761 add_prototyped_attribute (subr_die, type);
22762 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22763 context_die);
22764 gen_formal_types_die (type, subr_die);
22766 if (get_AT (subr_die, DW_AT_name))
22767 add_pubtype (type, subr_die);
22770 /* Generate a DIE for a type definition. */
22772 static void
22773 gen_typedef_die (tree decl, dw_die_ref context_die)
22775 dw_die_ref type_die;
22776 tree origin;
22778 if (TREE_ASM_WRITTEN (decl))
22780 if (DECL_ORIGINAL_TYPE (decl))
22781 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22782 return;
22785 TREE_ASM_WRITTEN (decl) = 1;
22786 type_die = new_die (DW_TAG_typedef, context_die, decl);
22787 origin = decl_ultimate_origin (decl);
22788 if (origin != NULL)
22789 add_abstract_origin_attribute (type_die, origin);
22790 else
22792 tree type;
22794 add_name_and_src_coords_attributes (type_die, decl);
22795 if (DECL_ORIGINAL_TYPE (decl))
22797 type = DECL_ORIGINAL_TYPE (decl);
22799 if (type == error_mark_node)
22800 return;
22802 gcc_assert (type != TREE_TYPE (decl));
22803 equate_type_number_to_die (TREE_TYPE (decl), type_die);
22805 else
22807 type = TREE_TYPE (decl);
22809 if (type == error_mark_node)
22810 return;
22812 if (is_naming_typedef_decl (TYPE_NAME (type)))
22814 /* Here, we are in the case of decl being a typedef naming
22815 an anonymous type, e.g:
22816 typedef struct {...} foo;
22817 In that case TREE_TYPE (decl) is not a typedef variant
22818 type and TYPE_NAME of the anonymous type is set to the
22819 TYPE_DECL of the typedef. This construct is emitted by
22820 the C++ FE.
22822 TYPE is the anonymous struct named by the typedef
22823 DECL. As we need the DW_AT_type attribute of the
22824 DW_TAG_typedef to point to the DIE of TYPE, let's
22825 generate that DIE right away. add_type_attribute
22826 called below will then pick (via lookup_type_die) that
22827 anonymous struct DIE. */
22828 if (!TREE_ASM_WRITTEN (type))
22829 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22831 /* This is a GNU Extension. We are adding a
22832 DW_AT_linkage_name attribute to the DIE of the
22833 anonymous struct TYPE. The value of that attribute
22834 is the name of the typedef decl naming the anonymous
22835 struct. This greatly eases the work of consumers of
22836 this debug info. */
22837 add_linkage_name_raw (lookup_type_die (type), decl);
22841 add_type_attribute (type_die, type, decl_quals (decl), false,
22842 context_die);
22844 if (is_naming_typedef_decl (decl))
22845 /* We want that all subsequent calls to lookup_type_die with
22846 TYPE in argument yield the DW_TAG_typedef we have just
22847 created. */
22848 equate_type_number_to_die (type, type_die);
22850 add_accessibility_attribute (type_die, decl);
22853 if (DECL_ABSTRACT_P (decl))
22854 equate_decl_number_to_die (decl, type_die);
22856 if (get_AT (type_die, DW_AT_name))
22857 add_pubtype (decl, type_die);
22860 /* Generate a DIE for a struct, class, enum or union type. */
22862 static void
22863 gen_tagged_type_die (tree type,
22864 dw_die_ref context_die,
22865 enum debug_info_usage usage)
22867 int need_pop;
22869 if (type == NULL_TREE
22870 || !is_tagged_type (type))
22871 return;
22873 if (TREE_ASM_WRITTEN (type))
22874 need_pop = 0;
22875 /* If this is a nested type whose containing class hasn't been written
22876 out yet, writing it out will cover this one, too. This does not apply
22877 to instantiations of member class templates; they need to be added to
22878 the containing class as they are generated. FIXME: This hurts the
22879 idea of combining type decls from multiple TUs, since we can't predict
22880 what set of template instantiations we'll get. */
22881 else if (TYPE_CONTEXT (type)
22882 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22883 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22885 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22887 if (TREE_ASM_WRITTEN (type))
22888 return;
22890 /* If that failed, attach ourselves to the stub. */
22891 push_decl_scope (TYPE_CONTEXT (type));
22892 context_die = lookup_type_die (TYPE_CONTEXT (type));
22893 need_pop = 1;
22895 else if (TYPE_CONTEXT (type) != NULL_TREE
22896 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22898 /* If this type is local to a function that hasn't been written
22899 out yet, use a NULL context for now; it will be fixed up in
22900 decls_for_scope. */
22901 context_die = lookup_decl_die (TYPE_CONTEXT (type));
22902 /* A declaration DIE doesn't count; nested types need to go in the
22903 specification. */
22904 if (context_die && is_declaration_die (context_die))
22905 context_die = NULL;
22906 need_pop = 0;
22908 else
22910 context_die = declare_in_namespace (type, context_die);
22911 need_pop = 0;
22914 if (TREE_CODE (type) == ENUMERAL_TYPE)
22916 /* This might have been written out by the call to
22917 declare_in_namespace. */
22918 if (!TREE_ASM_WRITTEN (type))
22919 gen_enumeration_type_die (type, context_die);
22921 else
22922 gen_struct_or_union_type_die (type, context_die, usage);
22924 if (need_pop)
22925 pop_decl_scope ();
22927 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22928 it up if it is ever completed. gen_*_type_die will set it for us
22929 when appropriate. */
22932 /* Generate a type description DIE. */
22934 static void
22935 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22936 enum debug_info_usage usage)
22938 struct array_descr_info info;
22940 if (type == NULL_TREE || type == error_mark_node)
22941 return;
22943 if (flag_checking && type)
22944 verify_type (type);
22946 if (TYPE_NAME (type) != NULL_TREE
22947 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22948 && is_redundant_typedef (TYPE_NAME (type))
22949 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22950 /* The DECL of this type is a typedef we don't want to emit debug
22951 info for but we want debug info for its underlying typedef.
22952 This can happen for e.g, the injected-class-name of a C++
22953 type. */
22954 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22956 /* If TYPE is a typedef type variant, let's generate debug info
22957 for the parent typedef which TYPE is a type of. */
22958 if (typedef_variant_p (type))
22960 if (TREE_ASM_WRITTEN (type))
22961 return;
22963 /* Prevent broken recursion; we can't hand off to the same type. */
22964 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22966 /* Give typedefs the right scope. */
22967 context_die = scope_die_for (type, context_die);
22969 TREE_ASM_WRITTEN (type) = 1;
22971 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22972 return;
22975 /* If type is an anonymous tagged type named by a typedef, let's
22976 generate debug info for the typedef. */
22977 if (is_naming_typedef_decl (TYPE_NAME (type)))
22979 /* Use the DIE of the containing namespace as the parent DIE of
22980 the type description DIE we want to generate. */
22981 if (DECL_CONTEXT (TYPE_NAME (type))
22982 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
22983 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
22985 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22986 return;
22989 /* We are going to output a DIE to represent the unqualified version
22990 of this type (i.e. without any const or volatile qualifiers) so
22991 get the main variant (i.e. the unqualified version) of this type
22992 now. (Vectors and arrays are special because the debugging info is in the
22993 cloned type itself). */
22994 if (TREE_CODE (type) != VECTOR_TYPE
22995 && TREE_CODE (type) != ARRAY_TYPE)
22996 type = type_main_variant (type);
22998 /* If this is an array type with hidden descriptor, handle it first. */
22999 if (!TREE_ASM_WRITTEN (type)
23000 && lang_hooks.types.get_array_descr_info)
23002 memset (&info, 0, sizeof (info));
23003 if (lang_hooks.types.get_array_descr_info (type, &info))
23005 /* Fortran sometimes emits array types with no dimension. */
23006 gcc_assert (info.ndimensions >= 0
23007 && (info.ndimensions
23008 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
23009 gen_descr_array_type_die (type, &info, context_die);
23010 TREE_ASM_WRITTEN (type) = 1;
23011 return;
23015 if (TREE_ASM_WRITTEN (type))
23017 /* Variable-length types may be incomplete even if
23018 TREE_ASM_WRITTEN. For such types, fall through to
23019 gen_array_type_die() and possibly fill in
23020 DW_AT_{upper,lower}_bound attributes. */
23021 if ((TREE_CODE (type) != ARRAY_TYPE
23022 && TREE_CODE (type) != RECORD_TYPE
23023 && TREE_CODE (type) != UNION_TYPE
23024 && TREE_CODE (type) != QUAL_UNION_TYPE)
23025 || !variably_modified_type_p (type, NULL))
23026 return;
23029 switch (TREE_CODE (type))
23031 case ERROR_MARK:
23032 break;
23034 case POINTER_TYPE:
23035 case REFERENCE_TYPE:
23036 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
23037 ensures that the gen_type_die recursion will terminate even if the
23038 type is recursive. Recursive types are possible in Ada. */
23039 /* ??? We could perhaps do this for all types before the switch
23040 statement. */
23041 TREE_ASM_WRITTEN (type) = 1;
23043 /* For these types, all that is required is that we output a DIE (or a
23044 set of DIEs) to represent the "basis" type. */
23045 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23046 DINFO_USAGE_IND_USE);
23047 break;
23049 case OFFSET_TYPE:
23050 /* This code is used for C++ pointer-to-data-member types.
23051 Output a description of the relevant class type. */
23052 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
23053 DINFO_USAGE_IND_USE);
23055 /* Output a description of the type of the object pointed to. */
23056 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23057 DINFO_USAGE_IND_USE);
23059 /* Now output a DIE to represent this pointer-to-data-member type
23060 itself. */
23061 gen_ptr_to_mbr_type_die (type, context_die);
23062 break;
23064 case FUNCTION_TYPE:
23065 /* Force out return type (in case it wasn't forced out already). */
23066 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23067 DINFO_USAGE_DIR_USE);
23068 gen_subroutine_type_die (type, context_die);
23069 break;
23071 case METHOD_TYPE:
23072 /* Force out return type (in case it wasn't forced out already). */
23073 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23074 DINFO_USAGE_DIR_USE);
23075 gen_subroutine_type_die (type, context_die);
23076 break;
23078 case ARRAY_TYPE:
23079 case VECTOR_TYPE:
23080 gen_array_type_die (type, context_die);
23081 break;
23083 case ENUMERAL_TYPE:
23084 case RECORD_TYPE:
23085 case UNION_TYPE:
23086 case QUAL_UNION_TYPE:
23087 gen_tagged_type_die (type, context_die, usage);
23088 return;
23090 case VOID_TYPE:
23091 case INTEGER_TYPE:
23092 case REAL_TYPE:
23093 case FIXED_POINT_TYPE:
23094 case COMPLEX_TYPE:
23095 case BOOLEAN_TYPE:
23096 case POINTER_BOUNDS_TYPE:
23097 /* No DIEs needed for fundamental types. */
23098 break;
23100 case NULLPTR_TYPE:
23101 case LANG_TYPE:
23102 /* Just use DW_TAG_unspecified_type. */
23104 dw_die_ref type_die = lookup_type_die (type);
23105 if (type_die == NULL)
23107 tree name = TYPE_IDENTIFIER (type);
23108 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
23109 type);
23110 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
23111 equate_type_number_to_die (type, type_die);
23114 break;
23116 default:
23117 if (is_cxx_auto (type))
23119 tree name = TYPE_IDENTIFIER (type);
23120 dw_die_ref *die = (name == get_identifier ("auto")
23121 ? &auto_die : &decltype_auto_die);
23122 if (!*die)
23124 *die = new_die (DW_TAG_unspecified_type,
23125 comp_unit_die (), NULL_TREE);
23126 add_name_attribute (*die, IDENTIFIER_POINTER (name));
23128 equate_type_number_to_die (type, *die);
23129 break;
23131 gcc_unreachable ();
23134 TREE_ASM_WRITTEN (type) = 1;
23137 static void
23138 gen_type_die (tree type, dw_die_ref context_die)
23140 if (type != error_mark_node)
23142 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
23143 if (flag_checking)
23145 dw_die_ref die = lookup_type_die (type);
23146 if (die)
23147 check_die (die);
23152 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
23153 things which are local to the given block. */
23155 static void
23156 gen_block_die (tree stmt, dw_die_ref context_die)
23158 int must_output_die = 0;
23159 bool inlined_func;
23161 /* Ignore blocks that are NULL. */
23162 if (stmt == NULL_TREE)
23163 return;
23165 inlined_func = inlined_function_outer_scope_p (stmt);
23167 /* If the block is one fragment of a non-contiguous block, do not
23168 process the variables, since they will have been done by the
23169 origin block. Do process subblocks. */
23170 if (BLOCK_FRAGMENT_ORIGIN (stmt))
23172 tree sub;
23174 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
23175 gen_block_die (sub, context_die);
23177 return;
23180 /* Determine if we need to output any Dwarf DIEs at all to represent this
23181 block. */
23182 if (inlined_func)
23183 /* The outer scopes for inlinings *must* always be represented. We
23184 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
23185 must_output_die = 1;
23186 else
23188 /* Determine if this block directly contains any "significant"
23189 local declarations which we will need to output DIEs for. */
23190 if (debug_info_level > DINFO_LEVEL_TERSE)
23191 /* We are not in terse mode so *any* local declaration counts
23192 as being a "significant" one. */
23193 must_output_die = ((BLOCK_VARS (stmt) != NULL
23194 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
23195 && (TREE_USED (stmt)
23196 || TREE_ASM_WRITTEN (stmt)
23197 || BLOCK_ABSTRACT (stmt)));
23198 else if ((TREE_USED (stmt)
23199 || TREE_ASM_WRITTEN (stmt)
23200 || BLOCK_ABSTRACT (stmt))
23201 && !dwarf2out_ignore_block (stmt))
23202 must_output_die = 1;
23205 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
23206 DIE for any block which contains no significant local declarations at
23207 all. Rather, in such cases we just call `decls_for_scope' so that any
23208 needed Dwarf info for any sub-blocks will get properly generated. Note
23209 that in terse mode, our definition of what constitutes a "significant"
23210 local declaration gets restricted to include only inlined function
23211 instances and local (nested) function definitions. */
23212 if (must_output_die)
23214 if (inlined_func)
23216 /* If STMT block is abstract, that means we have been called
23217 indirectly from dwarf2out_abstract_function.
23218 That function rightfully marks the descendent blocks (of
23219 the abstract function it is dealing with) as being abstract,
23220 precisely to prevent us from emitting any
23221 DW_TAG_inlined_subroutine DIE as a descendent
23222 of an abstract function instance. So in that case, we should
23223 not call gen_inlined_subroutine_die.
23225 Later though, when cgraph asks dwarf2out to emit info
23226 for the concrete instance of the function decl into which
23227 the concrete instance of STMT got inlined, the later will lead
23228 to the generation of a DW_TAG_inlined_subroutine DIE. */
23229 if (! BLOCK_ABSTRACT (stmt))
23230 gen_inlined_subroutine_die (stmt, context_die);
23232 else
23233 gen_lexical_block_die (stmt, context_die);
23235 else
23236 decls_for_scope (stmt, context_die);
23239 /* Process variable DECL (or variable with origin ORIGIN) within
23240 block STMT and add it to CONTEXT_DIE. */
23241 static void
23242 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23244 dw_die_ref die;
23245 tree decl_or_origin = decl ? decl : origin;
23247 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23248 die = lookup_decl_die (decl_or_origin);
23249 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
23250 && TYPE_DECL_IS_STUB (decl_or_origin))
23251 die = lookup_type_die (TREE_TYPE (decl_or_origin));
23252 else
23253 die = NULL;
23255 if (die != NULL && die->die_parent == NULL)
23256 add_child_die (context_die, die);
23257 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23259 if (early_dwarf)
23260 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23261 stmt, context_die);
23263 else
23264 gen_decl_die (decl, origin, NULL, context_die);
23267 /* Generate all of the decls declared within a given scope and (recursively)
23268 all of its sub-blocks. */
23270 static void
23271 decls_for_scope (tree stmt, dw_die_ref context_die)
23273 tree decl;
23274 unsigned int i;
23275 tree subblocks;
23277 /* Ignore NULL blocks. */
23278 if (stmt == NULL_TREE)
23279 return;
23281 /* Output the DIEs to represent all of the data objects and typedefs
23282 declared directly within this block but not within any nested
23283 sub-blocks. Also, nested function and tag DIEs have been
23284 generated with a parent of NULL; fix that up now. We don't
23285 have to do this if we're at -g1. */
23286 if (debug_info_level > DINFO_LEVEL_TERSE)
23288 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23289 process_scope_var (stmt, decl, NULL_TREE, context_die);
23290 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23291 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23292 context_die);
23295 /* Even if we're at -g1, we need to process the subblocks in order to get
23296 inlined call information. */
23298 /* Output the DIEs to represent all sub-blocks (and the items declared
23299 therein) of this block. */
23300 for (subblocks = BLOCK_SUBBLOCKS (stmt);
23301 subblocks != NULL;
23302 subblocks = BLOCK_CHAIN (subblocks))
23303 gen_block_die (subblocks, context_die);
23306 /* Is this a typedef we can avoid emitting? */
23308 bool
23309 is_redundant_typedef (const_tree decl)
23311 if (TYPE_DECL_IS_STUB (decl))
23312 return true;
23314 if (DECL_ARTIFICIAL (decl)
23315 && DECL_CONTEXT (decl)
23316 && is_tagged_type (DECL_CONTEXT (decl))
23317 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23318 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23319 /* Also ignore the artificial member typedef for the class name. */
23320 return true;
23322 return false;
23325 /* Return TRUE if TYPE is a typedef that names a type for linkage
23326 purposes. This kind of typedefs is produced by the C++ FE for
23327 constructs like:
23329 typedef struct {...} foo;
23331 In that case, there is no typedef variant type produced for foo.
23332 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23333 struct type. */
23335 static bool
23336 is_naming_typedef_decl (const_tree decl)
23338 if (decl == NULL_TREE
23339 || TREE_CODE (decl) != TYPE_DECL
23340 || DECL_NAMELESS (decl)
23341 || !is_tagged_type (TREE_TYPE (decl))
23342 || DECL_IS_BUILTIN (decl)
23343 || is_redundant_typedef (decl)
23344 /* It looks like Ada produces TYPE_DECLs that are very similar
23345 to C++ naming typedefs but that have different
23346 semantics. Let's be specific to c++ for now. */
23347 || !is_cxx ())
23348 return FALSE;
23350 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23351 && TYPE_NAME (TREE_TYPE (decl)) == decl
23352 && (TYPE_STUB_DECL (TREE_TYPE (decl))
23353 != TYPE_NAME (TREE_TYPE (decl))));
23356 /* Looks up the DIE for a context. */
23358 static inline dw_die_ref
23359 lookup_context_die (tree context)
23361 if (context)
23363 /* Find die that represents this context. */
23364 if (TYPE_P (context))
23366 context = TYPE_MAIN_VARIANT (context);
23367 dw_die_ref ctx = lookup_type_die (context);
23368 if (!ctx)
23369 return NULL;
23370 return strip_naming_typedef (context, ctx);
23372 else
23373 return lookup_decl_die (context);
23375 return comp_unit_die ();
23378 /* Returns the DIE for a context. */
23380 static inline dw_die_ref
23381 get_context_die (tree context)
23383 if (context)
23385 /* Find die that represents this context. */
23386 if (TYPE_P (context))
23388 context = TYPE_MAIN_VARIANT (context);
23389 return strip_naming_typedef (context, force_type_die (context));
23391 else
23392 return force_decl_die (context);
23394 return comp_unit_die ();
23397 /* Returns the DIE for decl. A DIE will always be returned. */
23399 static dw_die_ref
23400 force_decl_die (tree decl)
23402 dw_die_ref decl_die;
23403 unsigned saved_external_flag;
23404 tree save_fn = NULL_TREE;
23405 decl_die = lookup_decl_die (decl);
23406 if (!decl_die)
23408 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23410 decl_die = lookup_decl_die (decl);
23411 if (decl_die)
23412 return decl_die;
23414 switch (TREE_CODE (decl))
23416 case FUNCTION_DECL:
23417 /* Clear current_function_decl, so that gen_subprogram_die thinks
23418 that this is a declaration. At this point, we just want to force
23419 declaration die. */
23420 save_fn = current_function_decl;
23421 current_function_decl = NULL_TREE;
23422 gen_subprogram_die (decl, context_die);
23423 current_function_decl = save_fn;
23424 break;
23426 case VAR_DECL:
23427 /* Set external flag to force declaration die. Restore it after
23428 gen_decl_die() call. */
23429 saved_external_flag = DECL_EXTERNAL (decl);
23430 DECL_EXTERNAL (decl) = 1;
23431 gen_decl_die (decl, NULL, NULL, context_die);
23432 DECL_EXTERNAL (decl) = saved_external_flag;
23433 break;
23435 case NAMESPACE_DECL:
23436 if (dwarf_version >= 3 || !dwarf_strict)
23437 dwarf2out_decl (decl);
23438 else
23439 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
23440 decl_die = comp_unit_die ();
23441 break;
23443 case TRANSLATION_UNIT_DECL:
23444 decl_die = comp_unit_die ();
23445 break;
23447 default:
23448 gcc_unreachable ();
23451 /* We should be able to find the DIE now. */
23452 if (!decl_die)
23453 decl_die = lookup_decl_die (decl);
23454 gcc_assert (decl_die);
23457 return decl_die;
23460 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
23461 always returned. */
23463 static dw_die_ref
23464 force_type_die (tree type)
23466 dw_die_ref type_die;
23468 type_die = lookup_type_die (type);
23469 if (!type_die)
23471 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23473 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23474 false, context_die);
23475 gcc_assert (type_die);
23477 return type_die;
23480 /* Force out any required namespaces to be able to output DECL,
23481 and return the new context_die for it, if it's changed. */
23483 static dw_die_ref
23484 setup_namespace_context (tree thing, dw_die_ref context_die)
23486 tree context = (DECL_P (thing)
23487 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23488 if (context && TREE_CODE (context) == NAMESPACE_DECL)
23489 /* Force out the namespace. */
23490 context_die = force_decl_die (context);
23492 return context_die;
23495 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23496 type) within its namespace, if appropriate.
23498 For compatibility with older debuggers, namespace DIEs only contain
23499 declarations; all definitions are emitted at CU scope, with
23500 DW_AT_specification pointing to the declaration (like with class
23501 members). */
23503 static dw_die_ref
23504 declare_in_namespace (tree thing, dw_die_ref context_die)
23506 dw_die_ref ns_context;
23508 if (debug_info_level <= DINFO_LEVEL_TERSE)
23509 return context_die;
23511 /* External declarations in the local scope only need to be emitted
23512 once, not once in the namespace and once in the scope.
23514 This avoids declaring the `extern' below in the
23515 namespace DIE as well as in the innermost scope:
23517 namespace S
23519 int i=5;
23520 int foo()
23522 int i=8;
23523 extern int i;
23524 return i;
23528 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23529 return context_die;
23531 /* If this decl is from an inlined function, then don't try to emit it in its
23532 namespace, as we will get confused. It would have already been emitted
23533 when the abstract instance of the inline function was emitted anyways. */
23534 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23535 return context_die;
23537 ns_context = setup_namespace_context (thing, context_die);
23539 if (ns_context != context_die)
23541 if (is_fortran ())
23542 return ns_context;
23543 if (DECL_P (thing))
23544 gen_decl_die (thing, NULL, NULL, ns_context);
23545 else
23546 gen_type_die (thing, ns_context);
23548 return context_die;
23551 /* Generate a DIE for a namespace or namespace alias. */
23553 static void
23554 gen_namespace_die (tree decl, dw_die_ref context_die)
23556 dw_die_ref namespace_die;
23558 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23559 they are an alias of. */
23560 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23562 /* Output a real namespace or module. */
23563 context_die = setup_namespace_context (decl, comp_unit_die ());
23564 namespace_die = new_die (is_fortran ()
23565 ? DW_TAG_module : DW_TAG_namespace,
23566 context_die, decl);
23567 /* For Fortran modules defined in different CU don't add src coords. */
23568 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23570 const char *name = dwarf2_name (decl, 0);
23571 if (name)
23572 add_name_attribute (namespace_die, name);
23574 else
23575 add_name_and_src_coords_attributes (namespace_die, decl);
23576 if (DECL_EXTERNAL (decl))
23577 add_AT_flag (namespace_die, DW_AT_declaration, 1);
23578 equate_decl_number_to_die (decl, namespace_die);
23580 else
23582 /* Output a namespace alias. */
23584 /* Force out the namespace we are an alias of, if necessary. */
23585 dw_die_ref origin_die
23586 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23588 if (DECL_FILE_SCOPE_P (decl)
23589 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23590 context_die = setup_namespace_context (decl, comp_unit_die ());
23591 /* Now create the namespace alias DIE. */
23592 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23593 add_name_and_src_coords_attributes (namespace_die, decl);
23594 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23595 equate_decl_number_to_die (decl, namespace_die);
23597 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
23598 if (want_pubnames ())
23599 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23602 /* Generate Dwarf debug information for a decl described by DECL.
23603 The return value is currently only meaningful for PARM_DECLs,
23604 for all other decls it returns NULL.
23606 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23607 It can be NULL otherwise. */
23609 static dw_die_ref
23610 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23611 dw_die_ref context_die)
23613 tree decl_or_origin = decl ? decl : origin;
23614 tree class_origin = NULL, ultimate_origin;
23616 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23617 return NULL;
23619 /* Ignore pointer bounds decls. */
23620 if (DECL_P (decl_or_origin)
23621 && TREE_TYPE (decl_or_origin)
23622 && POINTER_BOUNDS_P (decl_or_origin))
23623 return NULL;
23625 switch (TREE_CODE (decl_or_origin))
23627 case ERROR_MARK:
23628 break;
23630 case CONST_DECL:
23631 if (!is_fortran () && !is_ada ())
23633 /* The individual enumerators of an enum type get output when we output
23634 the Dwarf representation of the relevant enum type itself. */
23635 break;
23638 /* Emit its type. */
23639 gen_type_die (TREE_TYPE (decl), context_die);
23641 /* And its containing namespace. */
23642 context_die = declare_in_namespace (decl, context_die);
23644 gen_const_die (decl, context_die);
23645 break;
23647 case FUNCTION_DECL:
23648 /* Don't output any DIEs to represent mere function declarations,
23649 unless they are class members or explicit block externs. */
23650 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23651 && DECL_FILE_SCOPE_P (decl_or_origin)
23652 && (current_function_decl == NULL_TREE
23653 || DECL_ARTIFICIAL (decl_or_origin)))
23654 break;
23656 #if 0
23657 /* FIXME */
23658 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23659 on local redeclarations of global functions. That seems broken. */
23660 if (current_function_decl != decl)
23661 /* This is only a declaration. */;
23662 #endif
23664 /* If we're emitting a clone, emit info for the abstract instance. */
23665 if (origin || DECL_ORIGIN (decl) != decl)
23666 dwarf2out_abstract_function (origin
23667 ? DECL_ORIGIN (origin)
23668 : DECL_ABSTRACT_ORIGIN (decl));
23670 /* If we're emitting an out-of-line copy of an inline function,
23671 emit info for the abstract instance and set up to refer to it. */
23672 else if (cgraph_function_possibly_inlined_p (decl)
23673 && ! DECL_ABSTRACT_P (decl)
23674 && ! class_or_namespace_scope_p (context_die)
23675 /* dwarf2out_abstract_function won't emit a die if this is just
23676 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
23677 that case, because that works only if we have a die. */
23678 && DECL_INITIAL (decl) != NULL_TREE)
23680 dwarf2out_abstract_function (decl);
23681 set_decl_origin_self (decl);
23684 /* Otherwise we're emitting the primary DIE for this decl. */
23685 else if (debug_info_level > DINFO_LEVEL_TERSE)
23687 /* Before we describe the FUNCTION_DECL itself, make sure that we
23688 have its containing type. */
23689 if (!origin)
23690 origin = decl_class_context (decl);
23691 if (origin != NULL_TREE)
23692 gen_type_die (origin, context_die);
23694 /* And its return type. */
23695 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23697 /* And its virtual context. */
23698 if (DECL_VINDEX (decl) != NULL_TREE)
23699 gen_type_die (DECL_CONTEXT (decl), context_die);
23701 /* Make sure we have a member DIE for decl. */
23702 if (origin != NULL_TREE)
23703 gen_type_die_for_member (origin, decl, context_die);
23705 /* And its containing namespace. */
23706 context_die = declare_in_namespace (decl, context_die);
23709 /* Now output a DIE to represent the function itself. */
23710 if (decl)
23711 gen_subprogram_die (decl, context_die);
23712 break;
23714 case TYPE_DECL:
23715 /* If we are in terse mode, don't generate any DIEs to represent any
23716 actual typedefs. */
23717 if (debug_info_level <= DINFO_LEVEL_TERSE)
23718 break;
23720 /* In the special case of a TYPE_DECL node representing the declaration
23721 of some type tag, if the given TYPE_DECL is marked as having been
23722 instantiated from some other (original) TYPE_DECL node (e.g. one which
23723 was generated within the original definition of an inline function) we
23724 used to generate a special (abbreviated) DW_TAG_structure_type,
23725 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
23726 should be actually referencing those DIEs, as variable DIEs with that
23727 type would be emitted already in the abstract origin, so it was always
23728 removed during unused type prunning. Don't add anything in this
23729 case. */
23730 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23731 break;
23733 if (is_redundant_typedef (decl))
23734 gen_type_die (TREE_TYPE (decl), context_die);
23735 else
23736 /* Output a DIE to represent the typedef itself. */
23737 gen_typedef_die (decl, context_die);
23738 break;
23740 case LABEL_DECL:
23741 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23742 gen_label_die (decl, context_die);
23743 break;
23745 case VAR_DECL:
23746 case RESULT_DECL:
23747 /* If we are in terse mode, don't generate any DIEs to represent any
23748 variable declarations or definitions. */
23749 if (debug_info_level <= DINFO_LEVEL_TERSE)
23750 break;
23752 /* Output any DIEs that are needed to specify the type of this data
23753 object. */
23754 if (decl_by_reference_p (decl_or_origin))
23755 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23756 else
23757 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23759 /* And its containing type. */
23760 class_origin = decl_class_context (decl_or_origin);
23761 if (class_origin != NULL_TREE)
23762 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23764 /* And its containing namespace. */
23765 context_die = declare_in_namespace (decl_or_origin, context_die);
23767 /* Now output the DIE to represent the data object itself. This gets
23768 complicated because of the possibility that the VAR_DECL really
23769 represents an inlined instance of a formal parameter for an inline
23770 function. */
23771 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23772 if (ultimate_origin != NULL_TREE
23773 && TREE_CODE (ultimate_origin) == PARM_DECL)
23774 gen_formal_parameter_die (decl, origin,
23775 true /* Emit name attribute. */,
23776 context_die);
23777 else
23778 gen_variable_die (decl, origin, context_die);
23779 break;
23781 case FIELD_DECL:
23782 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23783 /* Ignore the nameless fields that are used to skip bits but handle C++
23784 anonymous unions and structs. */
23785 if (DECL_NAME (decl) != NULL_TREE
23786 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23787 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23789 gen_type_die (member_declared_type (decl), context_die);
23790 gen_field_die (decl, ctx, context_die);
23792 break;
23794 case PARM_DECL:
23795 if (DECL_BY_REFERENCE (decl_or_origin))
23796 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23797 else
23798 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23799 return gen_formal_parameter_die (decl, origin,
23800 true /* Emit name attribute. */,
23801 context_die);
23803 case NAMESPACE_DECL:
23804 if (dwarf_version >= 3 || !dwarf_strict)
23805 gen_namespace_die (decl, context_die);
23806 break;
23808 case IMPORTED_DECL:
23809 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23810 DECL_CONTEXT (decl), context_die);
23811 break;
23813 case NAMELIST_DECL:
23814 gen_namelist_decl (DECL_NAME (decl), context_die,
23815 NAMELIST_DECL_ASSOCIATED_DECL (decl));
23816 break;
23818 default:
23819 /* Probably some frontend-internal decl. Assume we don't care. */
23820 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23821 break;
23824 return NULL;
23827 /* Output initial debug information for global DECL. Called at the
23828 end of the parsing process.
23830 This is the initial debug generation process. As such, the DIEs
23831 generated may be incomplete. A later debug generation pass
23832 (dwarf2out_late_global_decl) will augment the information generated
23833 in this pass (e.g., with complete location info). */
23835 static void
23836 dwarf2out_early_global_decl (tree decl)
23838 set_early_dwarf s;
23840 /* gen_decl_die() will set DECL_ABSTRACT because
23841 cgraph_function_possibly_inlined_p() returns true. This is in
23842 turn will cause DW_AT_inline attributes to be set.
23844 This happens because at early dwarf generation, there is no
23845 cgraph information, causing cgraph_function_possibly_inlined_p()
23846 to return true. Trick cgraph_function_possibly_inlined_p()
23847 while we generate dwarf early. */
23848 bool save = symtab->global_info_ready;
23849 symtab->global_info_ready = true;
23851 /* We don't handle TYPE_DECLs. If required, they'll be reached via
23852 other DECLs and they can point to template types or other things
23853 that dwarf2out can't handle when done via dwarf2out_decl. */
23854 if (TREE_CODE (decl) != TYPE_DECL
23855 && TREE_CODE (decl) != PARM_DECL)
23857 tree save_fndecl = current_function_decl;
23858 if (TREE_CODE (decl) == FUNCTION_DECL)
23860 /* No cfun means the symbol has no body, so there's nothing
23861 to emit. */
23862 if (!DECL_STRUCT_FUNCTION (decl))
23863 goto early_decl_exit;
23865 current_function_decl = decl;
23867 dwarf2out_decl (decl);
23868 if (TREE_CODE (decl) == FUNCTION_DECL)
23869 current_function_decl = save_fndecl;
23871 early_decl_exit:
23872 symtab->global_info_ready = save;
23875 /* Output debug information for global decl DECL. Called from
23876 toplev.c after compilation proper has finished. */
23878 static void
23879 dwarf2out_late_global_decl (tree decl)
23881 /* We have to generate early debug late for LTO. */
23882 if (in_lto_p)
23883 dwarf2out_early_global_decl (decl);
23885 /* Fill-in any location information we were unable to determine
23886 on the first pass. */
23887 if (TREE_CODE (decl) == VAR_DECL
23888 && !POINTER_BOUNDS_P (decl))
23890 dw_die_ref die = lookup_decl_die (decl);
23891 if (die)
23892 add_location_or_const_value_attribute (die, decl, false);
23896 /* Output debug information for type decl DECL. Called from toplev.c
23897 and from language front ends (to record built-in types). */
23898 static void
23899 dwarf2out_type_decl (tree decl, int local)
23901 if (!local)
23903 set_early_dwarf s;
23904 dwarf2out_decl (decl);
23908 /* Output debug information for imported module or decl DECL.
23909 NAME is non-NULL name in the lexical block if the decl has been renamed.
23910 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23911 that DECL belongs to.
23912 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
23913 static void
23914 dwarf2out_imported_module_or_decl_1 (tree decl,
23915 tree name,
23916 tree lexical_block,
23917 dw_die_ref lexical_block_die)
23919 expanded_location xloc;
23920 dw_die_ref imported_die = NULL;
23921 dw_die_ref at_import_die;
23923 if (TREE_CODE (decl) == IMPORTED_DECL)
23925 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23926 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23927 gcc_assert (decl);
23929 else
23930 xloc = expand_location (input_location);
23932 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23934 at_import_die = force_type_die (TREE_TYPE (decl));
23935 /* For namespace N { typedef void T; } using N::T; base_type_die
23936 returns NULL, but DW_TAG_imported_declaration requires
23937 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
23938 if (!at_import_die)
23940 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23941 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23942 at_import_die = lookup_type_die (TREE_TYPE (decl));
23943 gcc_assert (at_import_die);
23946 else
23948 at_import_die = lookup_decl_die (decl);
23949 if (!at_import_die)
23951 /* If we're trying to avoid duplicate debug info, we may not have
23952 emitted the member decl for this field. Emit it now. */
23953 if (TREE_CODE (decl) == FIELD_DECL)
23955 tree type = DECL_CONTEXT (decl);
23957 if (TYPE_CONTEXT (type)
23958 && TYPE_P (TYPE_CONTEXT (type))
23959 && !should_emit_struct_debug (TYPE_CONTEXT (type),
23960 DINFO_USAGE_DIR_USE))
23961 return;
23962 gen_type_die_for_member (type, decl,
23963 get_context_die (TYPE_CONTEXT (type)));
23965 if (TREE_CODE (decl) == NAMELIST_DECL)
23966 at_import_die = gen_namelist_decl (DECL_NAME (decl),
23967 get_context_die (DECL_CONTEXT (decl)),
23968 NULL_TREE);
23969 else
23970 at_import_die = force_decl_die (decl);
23974 if (TREE_CODE (decl) == NAMESPACE_DECL)
23976 if (dwarf_version >= 3 || !dwarf_strict)
23977 imported_die = new_die (DW_TAG_imported_module,
23978 lexical_block_die,
23979 lexical_block);
23980 else
23981 return;
23983 else
23984 imported_die = new_die (DW_TAG_imported_declaration,
23985 lexical_block_die,
23986 lexical_block);
23988 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
23989 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
23990 if (name)
23991 add_AT_string (imported_die, DW_AT_name,
23992 IDENTIFIER_POINTER (name));
23993 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
23996 /* Output debug information for imported module or decl DECL.
23997 NAME is non-NULL name in context if the decl has been renamed.
23998 CHILD is true if decl is one of the renamed decls as part of
23999 importing whole module. */
24001 static void
24002 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
24003 bool child)
24005 /* dw_die_ref at_import_die; */
24006 dw_die_ref scope_die;
24008 if (debug_info_level <= DINFO_LEVEL_TERSE)
24009 return;
24011 gcc_assert (decl);
24013 set_early_dwarf s;
24015 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
24016 We need decl DIE for reference and scope die. First, get DIE for the decl
24017 itself. */
24019 /* Get the scope die for decl context. Use comp_unit_die for global module
24020 or decl. If die is not found for non globals, force new die. */
24021 if (context
24022 && TYPE_P (context)
24023 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
24024 return;
24026 if (!(dwarf_version >= 3 || !dwarf_strict))
24027 return;
24029 scope_die = get_context_die (context);
24031 if (child)
24033 gcc_assert (scope_die->die_child);
24034 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
24035 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
24036 scope_die = scope_die->die_child;
24039 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
24040 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
24043 /* Output debug information for namelists. */
24045 static dw_die_ref
24046 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
24048 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
24049 tree value;
24050 unsigned i;
24052 if (debug_info_level <= DINFO_LEVEL_TERSE)
24053 return NULL;
24055 gcc_assert (scope_die != NULL);
24056 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
24057 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
24059 /* If there are no item_decls, we have a nondefining namelist, e.g.
24060 with USE association; hence, set DW_AT_declaration. */
24061 if (item_decls == NULL_TREE)
24063 add_AT_flag (nml_die, DW_AT_declaration, 1);
24064 return nml_die;
24067 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
24069 nml_item_ref_die = lookup_decl_die (value);
24070 if (!nml_item_ref_die)
24071 nml_item_ref_die = force_decl_die (value);
24073 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
24074 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
24076 return nml_die;
24080 /* Write the debugging output for DECL and return the DIE. */
24082 static void
24083 dwarf2out_decl (tree decl)
24085 dw_die_ref context_die = comp_unit_die ();
24087 switch (TREE_CODE (decl))
24089 case ERROR_MARK:
24090 return;
24092 case FUNCTION_DECL:
24093 /* What we would really like to do here is to filter out all mere
24094 file-scope declarations of file-scope functions which are never
24095 referenced later within this translation unit (and keep all of ones
24096 that *are* referenced later on) but we aren't clairvoyant, so we have
24097 no idea which functions will be referenced in the future (i.e. later
24098 on within the current translation unit). So here we just ignore all
24099 file-scope function declarations which are not also definitions. If
24100 and when the debugger needs to know something about these functions,
24101 it will have to hunt around and find the DWARF information associated
24102 with the definition of the function.
24104 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
24105 nodes represent definitions and which ones represent mere
24106 declarations. We have to check DECL_INITIAL instead. That's because
24107 the C front-end supports some weird semantics for "extern inline"
24108 function definitions. These can get inlined within the current
24109 translation unit (and thus, we need to generate Dwarf info for their
24110 abstract instances so that the Dwarf info for the concrete inlined
24111 instances can have something to refer to) but the compiler never
24112 generates any out-of-lines instances of such things (despite the fact
24113 that they *are* definitions).
24115 The important point is that the C front-end marks these "extern
24116 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
24117 them anyway. Note that the C++ front-end also plays some similar games
24118 for inline function definitions appearing within include files which
24119 also contain `#pragma interface' pragmas.
24121 If we are called from dwarf2out_abstract_function output a DIE
24122 anyway. We can end up here this way with early inlining and LTO
24123 where the inlined function is output in a different LTRANS unit
24124 or not at all. */
24125 if (DECL_INITIAL (decl) == NULL_TREE
24126 && ! DECL_ABSTRACT_P (decl))
24127 return;
24129 /* If we're a nested function, initially use a parent of NULL; if we're
24130 a plain function, this will be fixed up in decls_for_scope. If
24131 we're a method, it will be ignored, since we already have a DIE. */
24132 if (decl_function_context (decl)
24133 /* But if we're in terse mode, we don't care about scope. */
24134 && debug_info_level > DINFO_LEVEL_TERSE)
24135 context_die = NULL;
24136 break;
24138 case VAR_DECL:
24139 /* For local statics lookup proper context die. */
24140 if (local_function_static (decl))
24141 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24143 /* If we are in terse mode, don't generate any DIEs to represent any
24144 variable declarations or definitions. */
24145 if (debug_info_level <= DINFO_LEVEL_TERSE)
24146 return;
24147 break;
24149 case CONST_DECL:
24150 if (debug_info_level <= DINFO_LEVEL_TERSE)
24151 return;
24152 if (!is_fortran () && !is_ada ())
24153 return;
24154 if (TREE_STATIC (decl) && decl_function_context (decl))
24155 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24156 break;
24158 case NAMESPACE_DECL:
24159 case IMPORTED_DECL:
24160 if (debug_info_level <= DINFO_LEVEL_TERSE)
24161 return;
24162 if (lookup_decl_die (decl) != NULL)
24163 return;
24164 break;
24166 case TYPE_DECL:
24167 /* Don't emit stubs for types unless they are needed by other DIEs. */
24168 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
24169 return;
24171 /* Don't bother trying to generate any DIEs to represent any of the
24172 normal built-in types for the language we are compiling. */
24173 if (DECL_IS_BUILTIN (decl))
24174 return;
24176 /* If we are in terse mode, don't generate any DIEs for types. */
24177 if (debug_info_level <= DINFO_LEVEL_TERSE)
24178 return;
24180 /* If we're a function-scope tag, initially use a parent of NULL;
24181 this will be fixed up in decls_for_scope. */
24182 if (decl_function_context (decl))
24183 context_die = NULL;
24185 break;
24187 case NAMELIST_DECL:
24188 break;
24190 default:
24191 return;
24194 gen_decl_die (decl, NULL, NULL, context_die);
24196 if (flag_checking)
24198 dw_die_ref die = lookup_decl_die (decl);
24199 if (die)
24200 check_die (die);
24204 /* Write the debugging output for DECL. */
24206 static void
24207 dwarf2out_function_decl (tree decl)
24209 dwarf2out_decl (decl);
24210 call_arg_locations = NULL;
24211 call_arg_loc_last = NULL;
24212 call_site_count = -1;
24213 tail_call_site_count = -1;
24214 decl_loc_table->empty ();
24215 cached_dw_loc_list_table->empty ();
24218 /* Output a marker (i.e. a label) for the beginning of the generated code for
24219 a lexical block. */
24221 static void
24222 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
24223 unsigned int blocknum)
24225 switch_to_section (current_function_section ());
24226 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
24229 /* Output a marker (i.e. a label) for the end of the generated code for a
24230 lexical block. */
24232 static void
24233 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
24235 switch_to_section (current_function_section ());
24236 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
24239 /* Returns nonzero if it is appropriate not to emit any debugging
24240 information for BLOCK, because it doesn't contain any instructions.
24242 Don't allow this for blocks with nested functions or local classes
24243 as we would end up with orphans, and in the presence of scheduling
24244 we may end up calling them anyway. */
24246 static bool
24247 dwarf2out_ignore_block (const_tree block)
24249 tree decl;
24250 unsigned int i;
24252 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24253 if (TREE_CODE (decl) == FUNCTION_DECL
24254 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24255 return 0;
24256 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24258 decl = BLOCK_NONLOCALIZED_VAR (block, i);
24259 if (TREE_CODE (decl) == FUNCTION_DECL
24260 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24261 return 0;
24264 return 1;
24267 /* Hash table routines for file_hash. */
24269 bool
24270 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24272 return filename_cmp (p1->filename, p2) == 0;
24275 hashval_t
24276 dwarf_file_hasher::hash (dwarf_file_data *p)
24278 return htab_hash_string (p->filename);
24281 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24282 dwarf2out.c) and return its "index". The index of each (known) filename is
24283 just a unique number which is associated with only that one filename. We
24284 need such numbers for the sake of generating labels (in the .debug_sfnames
24285 section) and references to those files numbers (in the .debug_srcinfo
24286 and .debug_macinfo sections). If the filename given as an argument is not
24287 found in our current list, add it to the list and assign it the next
24288 available unique index number. */
24290 static struct dwarf_file_data *
24291 lookup_filename (const char *file_name)
24293 struct dwarf_file_data * created;
24295 if (!file_name)
24296 return NULL;
24298 dwarf_file_data **slot
24299 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24300 INSERT);
24301 if (*slot)
24302 return *slot;
24304 created = ggc_alloc<dwarf_file_data> ();
24305 created->filename = file_name;
24306 created->emitted_number = 0;
24307 *slot = created;
24308 return created;
24311 /* If the assembler will construct the file table, then translate the compiler
24312 internal file table number into the assembler file table number, and emit
24313 a .file directive if we haven't already emitted one yet. The file table
24314 numbers are different because we prune debug info for unused variables and
24315 types, which may include filenames. */
24317 static int
24318 maybe_emit_file (struct dwarf_file_data * fd)
24320 if (! fd->emitted_number)
24322 if (last_emitted_file)
24323 fd->emitted_number = last_emitted_file->emitted_number + 1;
24324 else
24325 fd->emitted_number = 1;
24326 last_emitted_file = fd;
24328 if (DWARF2_ASM_LINE_DEBUG_INFO)
24330 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24331 output_quoted_string (asm_out_file,
24332 remap_debug_filename (fd->filename));
24333 fputc ('\n', asm_out_file);
24337 return fd->emitted_number;
24340 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24341 That generation should happen after function debug info has been
24342 generated. The value of the attribute is the constant value of ARG. */
24344 static void
24345 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24347 die_arg_entry entry;
24349 if (!die || !arg)
24350 return;
24352 gcc_assert (early_dwarf);
24354 if (!tmpl_value_parm_die_table)
24355 vec_alloc (tmpl_value_parm_die_table, 32);
24357 entry.die = die;
24358 entry.arg = arg;
24359 vec_safe_push (tmpl_value_parm_die_table, entry);
24362 /* Return TRUE if T is an instance of generic type, FALSE
24363 otherwise. */
24365 static bool
24366 generic_type_p (tree t)
24368 if (t == NULL_TREE || !TYPE_P (t))
24369 return false;
24370 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24373 /* Schedule the generation of the generic parameter dies for the
24374 instance of generic type T. The proper generation itself is later
24375 done by gen_scheduled_generic_parms_dies. */
24377 static void
24378 schedule_generic_params_dies_gen (tree t)
24380 if (!generic_type_p (t))
24381 return;
24383 gcc_assert (early_dwarf);
24385 if (!generic_type_instances)
24386 vec_alloc (generic_type_instances, 256);
24388 vec_safe_push (generic_type_instances, t);
24391 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24392 by append_entry_to_tmpl_value_parm_die_table. This function must
24393 be called after function DIEs have been generated. */
24395 static void
24396 gen_remaining_tmpl_value_param_die_attribute (void)
24398 if (tmpl_value_parm_die_table)
24400 unsigned i, j;
24401 die_arg_entry *e;
24403 /* We do this in two phases - first get the cases we can
24404 handle during early-finish, preserving those we cannot
24405 (containing symbolic constants where we don't yet know
24406 whether we are going to output the referenced symbols).
24407 For those we try again at late-finish. */
24408 j = 0;
24409 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24411 if (!tree_add_const_value_attribute (e->die, e->arg))
24413 dw_loc_descr_ref loc = NULL;
24414 if (! early_dwarf
24415 && (dwarf_version >= 5 || !dwarf_strict))
24416 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
24417 if (loc)
24418 add_AT_loc (e->die, DW_AT_location, loc);
24419 else
24420 (*tmpl_value_parm_die_table)[j++] = *e;
24423 tmpl_value_parm_die_table->truncate (j);
24427 /* Generate generic parameters DIEs for instances of generic types
24428 that have been previously scheduled by
24429 schedule_generic_params_dies_gen. This function must be called
24430 after all the types of the CU have been laid out. */
24432 static void
24433 gen_scheduled_generic_parms_dies (void)
24435 unsigned i;
24436 tree t;
24438 if (!generic_type_instances)
24439 return;
24441 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24442 if (COMPLETE_TYPE_P (t))
24443 gen_generic_params_dies (t);
24445 generic_type_instances = NULL;
24449 /* Replace DW_AT_name for the decl with name. */
24451 static void
24452 dwarf2out_set_name (tree decl, tree name)
24454 dw_die_ref die;
24455 dw_attr_node *attr;
24456 const char *dname;
24458 die = TYPE_SYMTAB_DIE (decl);
24459 if (!die)
24460 return;
24462 dname = dwarf2_name (name, 0);
24463 if (!dname)
24464 return;
24466 attr = get_AT (die, DW_AT_name);
24467 if (attr)
24469 struct indirect_string_node *node;
24471 node = find_AT_string (dname);
24472 /* replace the string. */
24473 attr->dw_attr_val.v.val_str = node;
24476 else
24477 add_name_attribute (die, dname);
24480 /* True if before or during processing of the first function being emitted. */
24481 static bool in_first_function_p = true;
24482 /* True if loc_note during dwarf2out_var_location call might still be
24483 before first real instruction at address equal to .Ltext0. */
24484 static bool maybe_at_text_label_p = true;
24485 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
24486 static unsigned int first_loclabel_num_not_at_text_label;
24488 /* Called by the final INSN scan whenever we see a var location. We
24489 use it to drop labels in the right places, and throw the location in
24490 our lookup table. */
24492 static void
24493 dwarf2out_var_location (rtx_insn *loc_note)
24495 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24496 struct var_loc_node *newloc;
24497 rtx_insn *next_real, *next_note;
24498 rtx_insn *call_insn = NULL;
24499 static const char *last_label;
24500 static const char *last_postcall_label;
24501 static bool last_in_cold_section_p;
24502 static rtx_insn *expected_next_loc_note;
24503 tree decl;
24504 bool var_loc_p;
24506 if (!NOTE_P (loc_note))
24508 if (CALL_P (loc_note))
24510 call_site_count++;
24511 if (SIBLING_CALL_P (loc_note))
24512 tail_call_site_count++;
24513 if (optimize == 0 && !flag_var_tracking)
24515 /* When the var-tracking pass is not running, there is no note
24516 for indirect calls whose target is compile-time known. In this
24517 case, process such calls specifically so that we generate call
24518 sites for them anyway. */
24519 rtx x = PATTERN (loc_note);
24520 if (GET_CODE (x) == PARALLEL)
24521 x = XVECEXP (x, 0, 0);
24522 if (GET_CODE (x) == SET)
24523 x = SET_SRC (x);
24524 if (GET_CODE (x) == CALL)
24525 x = XEXP (x, 0);
24526 if (!MEM_P (x)
24527 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24528 || !SYMBOL_REF_DECL (XEXP (x, 0))
24529 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24530 != FUNCTION_DECL))
24532 call_insn = loc_note;
24533 loc_note = NULL;
24534 var_loc_p = false;
24536 next_real = next_real_insn (call_insn);
24537 next_note = NULL;
24538 cached_next_real_insn = NULL;
24539 goto create_label;
24543 return;
24546 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24547 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24548 return;
24550 /* Optimize processing a large consecutive sequence of location
24551 notes so we don't spend too much time in next_real_insn. If the
24552 next insn is another location note, remember the next_real_insn
24553 calculation for next time. */
24554 next_real = cached_next_real_insn;
24555 if (next_real)
24557 if (expected_next_loc_note != loc_note)
24558 next_real = NULL;
24561 next_note = NEXT_INSN (loc_note);
24562 if (! next_note
24563 || next_note->deleted ()
24564 || ! NOTE_P (next_note)
24565 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24566 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24567 next_note = NULL;
24569 if (! next_real)
24570 next_real = next_real_insn (loc_note);
24572 if (next_note)
24574 expected_next_loc_note = next_note;
24575 cached_next_real_insn = next_real;
24577 else
24578 cached_next_real_insn = NULL;
24580 /* If there are no instructions which would be affected by this note,
24581 don't do anything. */
24582 if (var_loc_p
24583 && next_real == NULL_RTX
24584 && !NOTE_DURING_CALL_P (loc_note))
24585 return;
24587 create_label:
24589 if (next_real == NULL_RTX)
24590 next_real = get_last_insn ();
24592 /* If there were any real insns between note we processed last time
24593 and this note (or if it is the first note), clear
24594 last_{,postcall_}label so that they are not reused this time. */
24595 if (last_var_location_insn == NULL_RTX
24596 || last_var_location_insn != next_real
24597 || last_in_cold_section_p != in_cold_section_p)
24599 last_label = NULL;
24600 last_postcall_label = NULL;
24603 if (var_loc_p)
24605 decl = NOTE_VAR_LOCATION_DECL (loc_note);
24606 newloc = add_var_loc_to_decl (decl, loc_note,
24607 NOTE_DURING_CALL_P (loc_note)
24608 ? last_postcall_label : last_label);
24609 if (newloc == NULL)
24610 return;
24612 else
24614 decl = NULL_TREE;
24615 newloc = NULL;
24618 /* If there were no real insns between note we processed last time
24619 and this note, use the label we emitted last time. Otherwise
24620 create a new label and emit it. */
24621 if (last_label == NULL)
24623 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24624 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24625 loclabel_num++;
24626 last_label = ggc_strdup (loclabel);
24627 /* See if loclabel might be equal to .Ltext0. If yes,
24628 bump first_loclabel_num_not_at_text_label. */
24629 if (!have_multiple_function_sections
24630 && in_first_function_p
24631 && maybe_at_text_label_p)
24633 static rtx_insn *last_start;
24634 rtx_insn *insn;
24635 for (insn = loc_note; insn; insn = previous_insn (insn))
24636 if (insn == last_start)
24637 break;
24638 else if (!NONDEBUG_INSN_P (insn))
24639 continue;
24640 else
24642 rtx body = PATTERN (insn);
24643 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24644 continue;
24645 /* Inline asm could occupy zero bytes. */
24646 else if (GET_CODE (body) == ASM_INPUT
24647 || asm_noperands (body) >= 0)
24648 continue;
24649 #ifdef HAVE_attr_length
24650 else if (get_attr_min_length (insn) == 0)
24651 continue;
24652 #endif
24653 else
24655 /* Assume insn has non-zero length. */
24656 maybe_at_text_label_p = false;
24657 break;
24660 if (maybe_at_text_label_p)
24662 last_start = loc_note;
24663 first_loclabel_num_not_at_text_label = loclabel_num;
24668 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24669 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24671 if (!var_loc_p)
24673 struct call_arg_loc_node *ca_loc
24674 = ggc_cleared_alloc<call_arg_loc_node> ();
24675 rtx_insn *prev
24676 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24678 ca_loc->call_arg_loc_note = loc_note;
24679 ca_loc->next = NULL;
24680 ca_loc->label = last_label;
24681 gcc_assert (prev
24682 && (CALL_P (prev)
24683 || (NONJUMP_INSN_P (prev)
24684 && GET_CODE (PATTERN (prev)) == SEQUENCE
24685 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24686 if (!CALL_P (prev))
24687 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24688 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24690 /* Look for a SYMBOL_REF in the "prev" instruction. */
24691 rtx x = get_call_rtx_from (PATTERN (prev));
24692 if (x)
24694 /* Try to get the call symbol, if any. */
24695 if (MEM_P (XEXP (x, 0)))
24696 x = XEXP (x, 0);
24697 /* First, look for a memory access to a symbol_ref. */
24698 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24699 && SYMBOL_REF_DECL (XEXP (x, 0))
24700 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24701 ca_loc->symbol_ref = XEXP (x, 0);
24702 /* Otherwise, look at a compile-time known user-level function
24703 declaration. */
24704 else if (MEM_P (x)
24705 && MEM_EXPR (x)
24706 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24707 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24710 ca_loc->block = insn_scope (prev);
24711 if (call_arg_locations)
24712 call_arg_loc_last->next = ca_loc;
24713 else
24714 call_arg_locations = ca_loc;
24715 call_arg_loc_last = ca_loc;
24717 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24718 newloc->label = last_label;
24719 else
24721 if (!last_postcall_label)
24723 sprintf (loclabel, "%s-1", last_label);
24724 last_postcall_label = ggc_strdup (loclabel);
24726 newloc->label = last_postcall_label;
24729 last_var_location_insn = next_real;
24730 last_in_cold_section_p = in_cold_section_p;
24733 /* Called from finalize_size_functions for size functions so that their body
24734 can be encoded in the debug info to describe the layout of variable-length
24735 structures. */
24737 static void
24738 dwarf2out_size_function (tree decl)
24740 function_to_dwarf_procedure (decl);
24743 /* Note in one location list that text section has changed. */
24746 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24748 var_loc_list *list = *slot;
24749 if (list->first)
24750 list->last_before_switch
24751 = list->last->next ? list->last->next : list->last;
24752 return 1;
24755 /* Note in all location lists that text section has changed. */
24757 static void
24758 var_location_switch_text_section (void)
24760 if (decl_loc_table == NULL)
24761 return;
24763 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24766 /* Create a new line number table. */
24768 static dw_line_info_table *
24769 new_line_info_table (void)
24771 dw_line_info_table *table;
24773 table = ggc_cleared_alloc<dw_line_info_table> ();
24774 table->file_num = 1;
24775 table->line_num = 1;
24776 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24778 return table;
24781 /* Lookup the "current" table into which we emit line info, so
24782 that we don't have to do it for every source line. */
24784 static void
24785 set_cur_line_info_table (section *sec)
24787 dw_line_info_table *table;
24789 if (sec == text_section)
24790 table = text_section_line_info;
24791 else if (sec == cold_text_section)
24793 table = cold_text_section_line_info;
24794 if (!table)
24796 cold_text_section_line_info = table = new_line_info_table ();
24797 table->end_label = cold_end_label;
24800 else
24802 const char *end_label;
24804 if (flag_reorder_blocks_and_partition)
24806 if (in_cold_section_p)
24807 end_label = crtl->subsections.cold_section_end_label;
24808 else
24809 end_label = crtl->subsections.hot_section_end_label;
24811 else
24813 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24814 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24815 current_function_funcdef_no);
24816 end_label = ggc_strdup (label);
24819 table = new_line_info_table ();
24820 table->end_label = end_label;
24822 vec_safe_push (separate_line_info, table);
24825 if (DWARF2_ASM_LINE_DEBUG_INFO)
24826 table->is_stmt = (cur_line_info_table
24827 ? cur_line_info_table->is_stmt
24828 : DWARF_LINE_DEFAULT_IS_STMT_START);
24829 cur_line_info_table = table;
24833 /* We need to reset the locations at the beginning of each
24834 function. We can't do this in the end_function hook, because the
24835 declarations that use the locations won't have been output when
24836 that hook is called. Also compute have_multiple_function_sections here. */
24838 static void
24839 dwarf2out_begin_function (tree fun)
24841 section *sec = function_section (fun);
24843 if (sec != text_section)
24844 have_multiple_function_sections = true;
24846 if (flag_reorder_blocks_and_partition && !cold_text_section)
24848 gcc_assert (current_function_decl == fun);
24849 cold_text_section = unlikely_text_section ();
24850 switch_to_section (cold_text_section);
24851 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24852 switch_to_section (sec);
24855 dwarf2out_note_section_used ();
24856 call_site_count = 0;
24857 tail_call_site_count = 0;
24859 set_cur_line_info_table (sec);
24862 /* Helper function of dwarf2out_end_function, called only after emitting
24863 the very first function into assembly. Check if some .debug_loc range
24864 might end with a .LVL* label that could be equal to .Ltext0.
24865 In that case we must force using absolute addresses in .debug_loc ranges,
24866 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24867 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24868 list terminator.
24869 Set have_multiple_function_sections to true in that case and
24870 terminate htab traversal. */
24873 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24875 var_loc_list *entry = *slot;
24876 struct var_loc_node *node;
24878 node = entry->first;
24879 if (node && node->next && node->next->label)
24881 unsigned int i;
24882 const char *label = node->next->label;
24883 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24885 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24887 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24888 if (strcmp (label, loclabel) == 0)
24890 have_multiple_function_sections = true;
24891 return 0;
24895 return 1;
24898 /* Hook called after emitting a function into assembly.
24899 This does something only for the very first function emitted. */
24901 static void
24902 dwarf2out_end_function (unsigned int)
24904 if (in_first_function_p
24905 && !have_multiple_function_sections
24906 && first_loclabel_num_not_at_text_label
24907 && decl_loc_table)
24908 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24909 in_first_function_p = false;
24910 maybe_at_text_label_p = false;
24913 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
24914 front-ends register a translation unit even before dwarf2out_init is
24915 called. */
24916 static tree main_translation_unit = NULL_TREE;
24918 /* Hook called by front-ends after they built their main translation unit.
24919 Associate comp_unit_die to UNIT. */
24921 static void
24922 dwarf2out_register_main_translation_unit (tree unit)
24924 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24925 && main_translation_unit == NULL_TREE);
24926 main_translation_unit = unit;
24927 /* If dwarf2out_init has not been called yet, it will perform the association
24928 itself looking at main_translation_unit. */
24929 if (decl_die_table != NULL)
24930 equate_decl_number_to_die (unit, comp_unit_die ());
24933 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
24935 static void
24936 push_dw_line_info_entry (dw_line_info_table *table,
24937 enum dw_line_info_opcode opcode, unsigned int val)
24939 dw_line_info_entry e;
24940 e.opcode = opcode;
24941 e.val = val;
24942 vec_safe_push (table->entries, e);
24945 /* Output a label to mark the beginning of a source code line entry
24946 and record information relating to this source line, in
24947 'line_info_table' for later output of the .debug_line section. */
24948 /* ??? The discriminator parameter ought to be unsigned. */
24950 static void
24951 dwarf2out_source_line (unsigned int line, const char *filename,
24952 int discriminator, bool is_stmt)
24954 unsigned int file_num;
24955 dw_line_info_table *table;
24957 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
24958 return;
24960 /* The discriminator column was added in dwarf4. Simplify the below
24961 by simply removing it if we're not supposed to output it. */
24962 if (dwarf_version < 4 && dwarf_strict)
24963 discriminator = 0;
24965 table = cur_line_info_table;
24966 file_num = maybe_emit_file (lookup_filename (filename));
24968 /* ??? TODO: Elide duplicate line number entries. Traditionally,
24969 the debugger has used the second (possibly duplicate) line number
24970 at the beginning of the function to mark the end of the prologue.
24971 We could eliminate any other duplicates within the function. For
24972 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
24973 that second line number entry. */
24974 /* Recall that this end-of-prologue indication is *not* the same thing
24975 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
24976 to which the hook corresponds, follows the last insn that was
24977 emitted by gen_prologue. What we need is to precede the first insn
24978 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
24979 insn that corresponds to something the user wrote. These may be
24980 very different locations once scheduling is enabled. */
24982 if (0 && file_num == table->file_num
24983 && line == table->line_num
24984 && discriminator == table->discrim_num
24985 && is_stmt == table->is_stmt)
24986 return;
24988 switch_to_section (current_function_section ());
24990 /* If requested, emit something human-readable. */
24991 if (flag_debug_asm)
24992 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
24994 if (DWARF2_ASM_LINE_DEBUG_INFO)
24996 /* Emit the .loc directive understood by GNU as. */
24997 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
24998 file_num, line, is_stmt, discriminator */
24999 fputs ("\t.loc ", asm_out_file);
25000 fprint_ul (asm_out_file, file_num);
25001 putc (' ', asm_out_file);
25002 fprint_ul (asm_out_file, line);
25003 putc (' ', asm_out_file);
25004 putc ('0', asm_out_file);
25006 if (is_stmt != table->is_stmt)
25008 fputs (" is_stmt ", asm_out_file);
25009 putc (is_stmt ? '1' : '0', asm_out_file);
25011 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
25013 gcc_assert (discriminator > 0);
25014 fputs (" discriminator ", asm_out_file);
25015 fprint_ul (asm_out_file, (unsigned long) discriminator);
25017 putc ('\n', asm_out_file);
25019 else
25021 unsigned int label_num = ++line_info_label_num;
25023 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
25025 push_dw_line_info_entry (table, LI_set_address, label_num);
25026 if (file_num != table->file_num)
25027 push_dw_line_info_entry (table, LI_set_file, file_num);
25028 if (discriminator != table->discrim_num)
25029 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
25030 if (is_stmt != table->is_stmt)
25031 push_dw_line_info_entry (table, LI_negate_stmt, 0);
25032 push_dw_line_info_entry (table, LI_set_line, line);
25035 table->file_num = file_num;
25036 table->line_num = line;
25037 table->discrim_num = discriminator;
25038 table->is_stmt = is_stmt;
25039 table->in_use = true;
25042 /* Record the beginning of a new source file. */
25044 static void
25045 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
25047 if (flag_eliminate_dwarf2_dups)
25049 /* Record the beginning of the file for break_out_includes. */
25050 dw_die_ref bincl_die;
25052 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
25053 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
25056 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25058 macinfo_entry e;
25059 e.code = DW_MACINFO_start_file;
25060 e.lineno = lineno;
25061 e.info = ggc_strdup (filename);
25062 vec_safe_push (macinfo_table, e);
25066 /* Record the end of a source file. */
25068 static void
25069 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
25071 if (flag_eliminate_dwarf2_dups)
25072 /* Record the end of the file for break_out_includes. */
25073 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
25075 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25077 macinfo_entry e;
25078 e.code = DW_MACINFO_end_file;
25079 e.lineno = lineno;
25080 e.info = NULL;
25081 vec_safe_push (macinfo_table, e);
25085 /* Called from debug_define in toplev.c. The `buffer' parameter contains
25086 the tail part of the directive line, i.e. the part which is past the
25087 initial whitespace, #, whitespace, directive-name, whitespace part. */
25089 static void
25090 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
25091 const char *buffer ATTRIBUTE_UNUSED)
25093 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25095 macinfo_entry e;
25096 /* Insert a dummy first entry to be able to optimize the whole
25097 predefined macro block using DW_MACRO_GNU_transparent_include. */
25098 if (macinfo_table->is_empty () && lineno <= 1)
25100 e.code = 0;
25101 e.lineno = 0;
25102 e.info = NULL;
25103 vec_safe_push (macinfo_table, e);
25105 e.code = DW_MACINFO_define;
25106 e.lineno = lineno;
25107 e.info = ggc_strdup (buffer);
25108 vec_safe_push (macinfo_table, e);
25112 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
25113 the tail part of the directive line, i.e. the part which is past the
25114 initial whitespace, #, whitespace, directive-name, whitespace part. */
25116 static void
25117 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
25118 const char *buffer ATTRIBUTE_UNUSED)
25120 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25122 macinfo_entry e;
25123 /* Insert a dummy first entry to be able to optimize the whole
25124 predefined macro block using DW_MACRO_GNU_transparent_include. */
25125 if (macinfo_table->is_empty () && lineno <= 1)
25127 e.code = 0;
25128 e.lineno = 0;
25129 e.info = NULL;
25130 vec_safe_push (macinfo_table, e);
25132 e.code = DW_MACINFO_undef;
25133 e.lineno = lineno;
25134 e.info = ggc_strdup (buffer);
25135 vec_safe_push (macinfo_table, e);
25139 /* Helpers to manipulate hash table of CUs. */
25141 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
25143 static inline hashval_t hash (const macinfo_entry *);
25144 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
25147 inline hashval_t
25148 macinfo_entry_hasher::hash (const macinfo_entry *entry)
25150 return htab_hash_string (entry->info);
25153 inline bool
25154 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
25155 const macinfo_entry *entry2)
25157 return !strcmp (entry1->info, entry2->info);
25160 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
25162 /* Output a single .debug_macinfo entry. */
25164 static void
25165 output_macinfo_op (macinfo_entry *ref)
25167 int file_num;
25168 size_t len;
25169 struct indirect_string_node *node;
25170 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25171 struct dwarf_file_data *fd;
25173 switch (ref->code)
25175 case DW_MACINFO_start_file:
25176 fd = lookup_filename (ref->info);
25177 file_num = maybe_emit_file (fd);
25178 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
25179 dw2_asm_output_data_uleb128 (ref->lineno,
25180 "Included from line number %lu",
25181 (unsigned long) ref->lineno);
25182 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
25183 break;
25184 case DW_MACINFO_end_file:
25185 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
25186 break;
25187 case DW_MACINFO_define:
25188 case DW_MACINFO_undef:
25189 len = strlen (ref->info) + 1;
25190 if (!dwarf_strict
25191 && len > DWARF_OFFSET_SIZE
25192 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25193 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25195 ref->code = ref->code == DW_MACINFO_define
25196 ? DW_MACRO_GNU_define_indirect
25197 : DW_MACRO_GNU_undef_indirect;
25198 output_macinfo_op (ref);
25199 return;
25201 dw2_asm_output_data (1, ref->code,
25202 ref->code == DW_MACINFO_define
25203 ? "Define macro" : "Undefine macro");
25204 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25205 (unsigned long) ref->lineno);
25206 dw2_asm_output_nstring (ref->info, -1, "The macro");
25207 break;
25208 case DW_MACRO_GNU_define_indirect:
25209 case DW_MACRO_GNU_undef_indirect:
25210 node = find_AT_string (ref->info);
25211 gcc_assert (node
25212 && ((node->form == DW_FORM_strp)
25213 || (node->form == DW_FORM_GNU_str_index)));
25214 dw2_asm_output_data (1, ref->code,
25215 ref->code == DW_MACRO_GNU_define_indirect
25216 ? "Define macro indirect"
25217 : "Undefine macro indirect");
25218 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25219 (unsigned long) ref->lineno);
25220 if (node->form == DW_FORM_strp)
25221 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
25222 debug_str_section, "The macro: \"%s\"",
25223 ref->info);
25224 else
25225 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
25226 ref->info);
25227 break;
25228 case DW_MACRO_GNU_transparent_include:
25229 dw2_asm_output_data (1, ref->code, "Transparent include");
25230 ASM_GENERATE_INTERNAL_LABEL (label,
25231 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
25232 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
25233 break;
25234 default:
25235 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
25236 ASM_COMMENT_START, (unsigned long) ref->code);
25237 break;
25241 /* Attempt to make a sequence of define/undef macinfo ops shareable with
25242 other compilation unit .debug_macinfo sections. IDX is the first
25243 index of a define/undef, return the number of ops that should be
25244 emitted in a comdat .debug_macinfo section and emit
25245 a DW_MACRO_GNU_transparent_include entry referencing it.
25246 If the define/undef entry should be emitted normally, return 0. */
25248 static unsigned
25249 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25250 macinfo_hash_type **macinfo_htab)
25252 macinfo_entry *first, *second, *cur, *inc;
25253 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25254 unsigned char checksum[16];
25255 struct md5_ctx ctx;
25256 char *grp_name, *tail;
25257 const char *base;
25258 unsigned int i, count, encoded_filename_len, linebuf_len;
25259 macinfo_entry **slot;
25261 first = &(*macinfo_table)[idx];
25262 second = &(*macinfo_table)[idx + 1];
25264 /* Optimize only if there are at least two consecutive define/undef ops,
25265 and either all of them are before first DW_MACINFO_start_file
25266 with lineno {0,1} (i.e. predefined macro block), or all of them are
25267 in some included header file. */
25268 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25269 return 0;
25270 if (vec_safe_is_empty (files))
25272 if (first->lineno > 1 || second->lineno > 1)
25273 return 0;
25275 else if (first->lineno == 0)
25276 return 0;
25278 /* Find the last define/undef entry that can be grouped together
25279 with first and at the same time compute md5 checksum of their
25280 codes, linenumbers and strings. */
25281 md5_init_ctx (&ctx);
25282 for (i = idx; macinfo_table->iterate (i, &cur); i++)
25283 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25284 break;
25285 else if (vec_safe_is_empty (files) && cur->lineno > 1)
25286 break;
25287 else
25289 unsigned char code = cur->code;
25290 md5_process_bytes (&code, 1, &ctx);
25291 checksum_uleb128 (cur->lineno, &ctx);
25292 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25294 md5_finish_ctx (&ctx, checksum);
25295 count = i - idx;
25297 /* From the containing include filename (if any) pick up just
25298 usable characters from its basename. */
25299 if (vec_safe_is_empty (files))
25300 base = "";
25301 else
25302 base = lbasename (files->last ().info);
25303 for (encoded_filename_len = 0, i = 0; base[i]; i++)
25304 if (ISIDNUM (base[i]) || base[i] == '.')
25305 encoded_filename_len++;
25306 /* Count . at the end. */
25307 if (encoded_filename_len)
25308 encoded_filename_len++;
25310 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25311 linebuf_len = strlen (linebuf);
25313 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
25314 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25315 + 16 * 2 + 1);
25316 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25317 tail = grp_name + 4;
25318 if (encoded_filename_len)
25320 for (i = 0; base[i]; i++)
25321 if (ISIDNUM (base[i]) || base[i] == '.')
25322 *tail++ = base[i];
25323 *tail++ = '.';
25325 memcpy (tail, linebuf, linebuf_len);
25326 tail += linebuf_len;
25327 *tail++ = '.';
25328 for (i = 0; i < 16; i++)
25329 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25331 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25332 in the empty vector entry before the first define/undef. */
25333 inc = &(*macinfo_table)[idx - 1];
25334 inc->code = DW_MACRO_GNU_transparent_include;
25335 inc->lineno = 0;
25336 inc->info = ggc_strdup (grp_name);
25337 if (!*macinfo_htab)
25338 *macinfo_htab = new macinfo_hash_type (10);
25339 /* Avoid emitting duplicates. */
25340 slot = (*macinfo_htab)->find_slot (inc, INSERT);
25341 if (*slot != NULL)
25343 inc->code = 0;
25344 inc->info = NULL;
25345 /* If such an entry has been used before, just emit
25346 a DW_MACRO_GNU_transparent_include op. */
25347 inc = *slot;
25348 output_macinfo_op (inc);
25349 /* And clear all macinfo_entry in the range to avoid emitting them
25350 in the second pass. */
25351 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25353 cur->code = 0;
25354 cur->info = NULL;
25357 else
25359 *slot = inc;
25360 inc->lineno = (*macinfo_htab)->elements ();
25361 output_macinfo_op (inc);
25363 return count;
25366 /* Save any strings needed by the macinfo table in the debug str
25367 table. All strings must be collected into the table by the time
25368 index_string is called. */
25370 static void
25371 save_macinfo_strings (void)
25373 unsigned len;
25374 unsigned i;
25375 macinfo_entry *ref;
25377 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25379 switch (ref->code)
25381 /* Match the logic in output_macinfo_op to decide on
25382 indirect strings. */
25383 case DW_MACINFO_define:
25384 case DW_MACINFO_undef:
25385 len = strlen (ref->info) + 1;
25386 if (!dwarf_strict
25387 && len > DWARF_OFFSET_SIZE
25388 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25389 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25390 set_indirect_string (find_AT_string (ref->info));
25391 break;
25392 case DW_MACRO_GNU_define_indirect:
25393 case DW_MACRO_GNU_undef_indirect:
25394 set_indirect_string (find_AT_string (ref->info));
25395 break;
25396 default:
25397 break;
25402 /* Output macinfo section(s). */
25404 static void
25405 output_macinfo (void)
25407 unsigned i;
25408 unsigned long length = vec_safe_length (macinfo_table);
25409 macinfo_entry *ref;
25410 vec<macinfo_entry, va_gc> *files = NULL;
25411 macinfo_hash_type *macinfo_htab = NULL;
25413 if (! length)
25414 return;
25416 /* output_macinfo* uses these interchangeably. */
25417 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25418 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25419 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25420 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25422 /* For .debug_macro emit the section header. */
25423 if (!dwarf_strict)
25425 dw2_asm_output_data (2, 4, "DWARF macro version number");
25426 if (DWARF_OFFSET_SIZE == 8)
25427 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25428 else
25429 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25430 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25431 (!dwarf_split_debug_info ? debug_line_section_label
25432 : debug_skeleton_line_section_label),
25433 debug_line_section, NULL);
25436 /* In the first loop, it emits the primary .debug_macinfo section
25437 and after each emitted op the macinfo_entry is cleared.
25438 If a longer range of define/undef ops can be optimized using
25439 DW_MACRO_GNU_transparent_include, the
25440 DW_MACRO_GNU_transparent_include op is emitted and kept in
25441 the vector before the first define/undef in the range and the
25442 whole range of define/undef ops is not emitted and kept. */
25443 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25445 switch (ref->code)
25447 case DW_MACINFO_start_file:
25448 vec_safe_push (files, *ref);
25449 break;
25450 case DW_MACINFO_end_file:
25451 if (!vec_safe_is_empty (files))
25452 files->pop ();
25453 break;
25454 case DW_MACINFO_define:
25455 case DW_MACINFO_undef:
25456 if (!dwarf_strict
25457 && HAVE_COMDAT_GROUP
25458 && vec_safe_length (files) != 1
25459 && i > 0
25460 && i + 1 < length
25461 && (*macinfo_table)[i - 1].code == 0)
25463 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25464 if (count)
25466 i += count - 1;
25467 continue;
25470 break;
25471 case 0:
25472 /* A dummy entry may be inserted at the beginning to be able
25473 to optimize the whole block of predefined macros. */
25474 if (i == 0)
25475 continue;
25476 default:
25477 break;
25479 output_macinfo_op (ref);
25480 ref->info = NULL;
25481 ref->code = 0;
25484 if (!macinfo_htab)
25485 return;
25487 delete macinfo_htab;
25488 macinfo_htab = NULL;
25490 /* If any DW_MACRO_GNU_transparent_include were used, on those
25491 DW_MACRO_GNU_transparent_include entries terminate the
25492 current chain and switch to a new comdat .debug_macinfo
25493 section and emit the define/undef entries within it. */
25494 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25495 switch (ref->code)
25497 case 0:
25498 continue;
25499 case DW_MACRO_GNU_transparent_include:
25501 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25502 tree comdat_key = get_identifier (ref->info);
25503 /* Terminate the previous .debug_macinfo section. */
25504 dw2_asm_output_data (1, 0, "End compilation unit");
25505 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
25506 SECTION_DEBUG
25507 | SECTION_LINKONCE,
25508 comdat_key);
25509 ASM_GENERATE_INTERNAL_LABEL (label,
25510 DEBUG_MACRO_SECTION_LABEL,
25511 ref->lineno);
25512 ASM_OUTPUT_LABEL (asm_out_file, label);
25513 ref->code = 0;
25514 ref->info = NULL;
25515 dw2_asm_output_data (2, 4, "DWARF macro version number");
25516 if (DWARF_OFFSET_SIZE == 8)
25517 dw2_asm_output_data (1, 1, "Flags: 64-bit");
25518 else
25519 dw2_asm_output_data (1, 0, "Flags: 32-bit");
25521 break;
25522 case DW_MACINFO_define:
25523 case DW_MACINFO_undef:
25524 output_macinfo_op (ref);
25525 ref->code = 0;
25526 ref->info = NULL;
25527 break;
25528 default:
25529 gcc_unreachable ();
25533 /* Set up for Dwarf output at the start of compilation. */
25535 static void
25536 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25538 /* This option is currently broken, see (PR53118 and PR46102). */
25539 if (flag_eliminate_dwarf2_dups
25540 && strstr (lang_hooks.name, "C++"))
25542 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25543 flag_eliminate_dwarf2_dups = 0;
25546 /* Allocate the file_table. */
25547 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25549 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25550 /* Allocate the decl_die_table. */
25551 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25553 /* Allocate the decl_loc_table. */
25554 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25556 /* Allocate the cached_dw_loc_list_table. */
25557 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25559 /* Allocate the initial hunk of the decl_scope_table. */
25560 vec_alloc (decl_scope_table, 256);
25562 /* Allocate the initial hunk of the abbrev_die_table. */
25563 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25564 (ABBREV_DIE_TABLE_INCREMENT);
25565 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25566 /* Zero-th entry is allocated, but unused. */
25567 abbrev_die_table_in_use = 1;
25569 /* Allocate the dwarf_proc_stack_usage_map. */
25570 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25572 /* Allocate the pubtypes and pubnames vectors. */
25573 vec_alloc (pubname_table, 32);
25574 vec_alloc (pubtype_table, 32);
25576 vec_alloc (incomplete_types, 64);
25578 vec_alloc (used_rtx_array, 32);
25580 if (!dwarf_split_debug_info)
25582 debug_info_section = get_section (DEBUG_INFO_SECTION,
25583 SECTION_DEBUG, NULL);
25584 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25585 SECTION_DEBUG, NULL);
25586 debug_loc_section = get_section (DEBUG_LOC_SECTION,
25587 SECTION_DEBUG, NULL);
25589 else
25591 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25592 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25593 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25594 SECTION_DEBUG | SECTION_EXCLUDE,
25595 NULL);
25596 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25597 SECTION_DEBUG, NULL);
25598 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25599 SECTION_DEBUG, NULL);
25600 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25601 SECTION_DEBUG, NULL);
25602 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25603 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25605 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25606 the main .o, but the skeleton_line goes into the split off dwo. */
25607 debug_skeleton_line_section
25608 = get_section (DEBUG_DWO_LINE_SECTION,
25609 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25610 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25611 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25612 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
25613 SECTION_DEBUG | SECTION_EXCLUDE,
25614 NULL);
25615 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25616 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25617 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25618 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25619 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25620 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25622 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25623 SECTION_DEBUG, NULL);
25624 debug_macinfo_section = get_section (dwarf_strict
25625 ? DEBUG_MACINFO_SECTION
25626 : DEBUG_MACRO_SECTION,
25627 DEBUG_MACRO_SECTION_FLAGS, NULL);
25628 debug_line_section = get_section (DEBUG_LINE_SECTION,
25629 SECTION_DEBUG, NULL);
25630 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25631 SECTION_DEBUG, NULL);
25632 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25633 SECTION_DEBUG, NULL);
25634 debug_str_section = get_section (DEBUG_STR_SECTION,
25635 DEBUG_STR_SECTION_FLAGS, NULL);
25636 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25637 SECTION_DEBUG, NULL);
25638 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25639 SECTION_DEBUG, NULL);
25641 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25642 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25643 DEBUG_ABBREV_SECTION_LABEL, 0);
25644 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25645 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25646 COLD_TEXT_SECTION_LABEL, 0);
25647 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25649 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25650 DEBUG_INFO_SECTION_LABEL, 0);
25651 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25652 DEBUG_LINE_SECTION_LABEL, 0);
25653 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25654 DEBUG_RANGES_SECTION_LABEL, 0);
25655 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25656 DEBUG_ADDR_SECTION_LABEL, 0);
25657 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25658 dwarf_strict
25659 ? DEBUG_MACINFO_SECTION_LABEL
25660 : DEBUG_MACRO_SECTION_LABEL, 0);
25661 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25663 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25664 vec_alloc (macinfo_table, 64);
25666 switch_to_section (text_section);
25667 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25668 #endif
25670 /* Make sure the line number table for .text always exists. */
25671 text_section_line_info = new_line_info_table ();
25672 text_section_line_info->end_label = text_end_label;
25674 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25675 cur_line_info_table = text_section_line_info;
25676 #endif
25678 /* If front-ends already registered a main translation unit but we were not
25679 ready to perform the association, do this now. */
25680 if (main_translation_unit != NULL_TREE)
25681 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25684 /* Called before compile () starts outputtting functions, variables
25685 and toplevel asms into assembly. */
25687 static void
25688 dwarf2out_assembly_start (void)
25690 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25691 && dwarf2out_do_cfi_asm ()
25692 && (!(flag_unwind_tables || flag_exceptions)
25693 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25694 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25697 /* A helper function for dwarf2out_finish called through
25698 htab_traverse. Assign a string its index. All strings must be
25699 collected into the table by the time index_string is called,
25700 because the indexing code relies on htab_traverse to traverse nodes
25701 in the same order for each run. */
25704 index_string (indirect_string_node **h, unsigned int *index)
25706 indirect_string_node *node = *h;
25708 find_string_form (node);
25709 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25711 gcc_assert (node->index == NO_INDEX_ASSIGNED);
25712 node->index = *index;
25713 *index += 1;
25715 return 1;
25718 /* A helper function for output_indirect_strings called through
25719 htab_traverse. Output the offset to a string and update the
25720 current offset. */
25723 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25725 indirect_string_node *node = *h;
25727 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25729 /* Assert that this node has been assigned an index. */
25730 gcc_assert (node->index != NO_INDEX_ASSIGNED
25731 && node->index != NOT_INDEXED);
25732 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25733 "indexed string 0x%x: %s", node->index, node->str);
25734 *offset += strlen (node->str) + 1;
25736 return 1;
25739 /* A helper function for dwarf2out_finish called through
25740 htab_traverse. Output the indexed string. */
25743 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25745 struct indirect_string_node *node = *h;
25747 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25749 /* Assert that the strings are output in the same order as their
25750 indexes were assigned. */
25751 gcc_assert (*cur_idx == node->index);
25752 assemble_string (node->str, strlen (node->str) + 1);
25753 *cur_idx += 1;
25755 return 1;
25758 /* A helper function for dwarf2out_finish called through
25759 htab_traverse. Emit one queued .debug_str string. */
25762 output_indirect_string (indirect_string_node **h, void *)
25764 struct indirect_string_node *node = *h;
25766 node->form = find_string_form (node);
25767 if (node->form == DW_FORM_strp && node->refcount > 0)
25769 ASM_OUTPUT_LABEL (asm_out_file, node->label);
25770 assemble_string (node->str, strlen (node->str) + 1);
25773 return 1;
25776 /* Output the indexed string table. */
25778 static void
25779 output_indirect_strings (void)
25781 switch_to_section (debug_str_section);
25782 if (!dwarf_split_debug_info)
25783 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25784 else
25786 unsigned int offset = 0;
25787 unsigned int cur_idx = 0;
25789 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25791 switch_to_section (debug_str_offsets_section);
25792 debug_str_hash->traverse_noresize
25793 <unsigned int *, output_index_string_offset> (&offset);
25794 switch_to_section (debug_str_dwo_section);
25795 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25796 (&cur_idx);
25800 /* Callback for htab_traverse to assign an index to an entry in the
25801 table, and to write that entry to the .debug_addr section. */
25804 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25806 addr_table_entry *entry = *slot;
25808 if (entry->refcount == 0)
25810 gcc_assert (entry->index == NO_INDEX_ASSIGNED
25811 || entry->index == NOT_INDEXED);
25812 return 1;
25815 gcc_assert (entry->index == *cur_index);
25816 (*cur_index)++;
25818 switch (entry->kind)
25820 case ate_kind_rtx:
25821 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25822 "0x%x", entry->index);
25823 break;
25824 case ate_kind_rtx_dtprel:
25825 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25826 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25827 DWARF2_ADDR_SIZE,
25828 entry->addr.rtl);
25829 fputc ('\n', asm_out_file);
25830 break;
25831 case ate_kind_label:
25832 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25833 "0x%x", entry->index);
25834 break;
25835 default:
25836 gcc_unreachable ();
25838 return 1;
25841 /* Produce the .debug_addr section. */
25843 static void
25844 output_addr_table (void)
25846 unsigned int index = 0;
25847 if (addr_index_table == NULL || addr_index_table->size () == 0)
25848 return;
25850 switch_to_section (debug_addr_section);
25851 addr_index_table
25852 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25855 #if ENABLE_ASSERT_CHECKING
25856 /* Verify that all marks are clear. */
25858 static void
25859 verify_marks_clear (dw_die_ref die)
25861 dw_die_ref c;
25863 gcc_assert (! die->die_mark);
25864 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25866 #endif /* ENABLE_ASSERT_CHECKING */
25868 /* Clear the marks for a die and its children.
25869 Be cool if the mark isn't set. */
25871 static void
25872 prune_unmark_dies (dw_die_ref die)
25874 dw_die_ref c;
25876 if (die->die_mark)
25877 die->die_mark = 0;
25878 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25881 /* Given LOC that is referenced by a DIE we're marking as used, find all
25882 referenced DWARF procedures it references and mark them as used. */
25884 static void
25885 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25887 for (; loc != NULL; loc = loc->dw_loc_next)
25888 switch (loc->dw_loc_opc)
25890 case DW_OP_GNU_implicit_pointer:
25891 case DW_OP_GNU_convert:
25892 case DW_OP_GNU_reinterpret:
25893 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25894 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25895 break;
25896 case DW_OP_call2:
25897 case DW_OP_call4:
25898 case DW_OP_call_ref:
25899 case DW_OP_GNU_const_type:
25900 case DW_OP_GNU_parameter_ref:
25901 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
25902 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25903 break;
25904 case DW_OP_GNU_regval_type:
25905 case DW_OP_GNU_deref_type:
25906 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
25907 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
25908 break;
25909 case DW_OP_GNU_entry_value:
25910 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
25911 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
25912 break;
25913 default:
25914 break;
25918 /* Given DIE that we're marking as used, find any other dies
25919 it references as attributes and mark them as used. */
25921 static void
25922 prune_unused_types_walk_attribs (dw_die_ref die)
25924 dw_attr_node *a;
25925 unsigned ix;
25927 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25929 switch (AT_class (a))
25931 /* Make sure DWARF procedures referenced by location descriptions will
25932 get emitted. */
25933 case dw_val_class_loc:
25934 prune_unused_types_walk_loc_descr (AT_loc (a));
25935 break;
25936 case dw_val_class_loc_list:
25937 for (dw_loc_list_ref list = AT_loc_list (a);
25938 list != NULL;
25939 list = list->dw_loc_next)
25940 prune_unused_types_walk_loc_descr (list->expr);
25941 break;
25943 case dw_val_class_die_ref:
25944 /* A reference to another DIE.
25945 Make sure that it will get emitted.
25946 If it was broken out into a comdat group, don't follow it. */
25947 if (! AT_ref (a)->comdat_type_p
25948 || a->dw_attr == DW_AT_specification)
25949 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
25950 break;
25952 case dw_val_class_str:
25953 /* Set the string's refcount to 0 so that prune_unused_types_mark
25954 accounts properly for it. */
25955 a->dw_attr_val.v.val_str->refcount = 0;
25956 break;
25958 default:
25959 break;
25964 /* Mark the generic parameters and arguments children DIEs of DIE. */
25966 static void
25967 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
25969 dw_die_ref c;
25971 if (die == NULL || die->die_child == NULL)
25972 return;
25973 c = die->die_child;
25976 if (is_template_parameter (c))
25977 prune_unused_types_mark (c, 1);
25978 c = c->die_sib;
25979 } while (c && c != die->die_child);
25982 /* Mark DIE as being used. If DOKIDS is true, then walk down
25983 to DIE's children. */
25985 static void
25986 prune_unused_types_mark (dw_die_ref die, int dokids)
25988 dw_die_ref c;
25990 if (die->die_mark == 0)
25992 /* We haven't done this node yet. Mark it as used. */
25993 die->die_mark = 1;
25994 /* If this is the DIE of a generic type instantiation,
25995 mark the children DIEs that describe its generic parms and
25996 args. */
25997 prune_unused_types_mark_generic_parms_dies (die);
25999 /* We also have to mark its parents as used.
26000 (But we don't want to mark our parent's kids due to this,
26001 unless it is a class.) */
26002 if (die->die_parent)
26003 prune_unused_types_mark (die->die_parent,
26004 class_scope_p (die->die_parent));
26006 /* Mark any referenced nodes. */
26007 prune_unused_types_walk_attribs (die);
26009 /* If this node is a specification,
26010 also mark the definition, if it exists. */
26011 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
26012 prune_unused_types_mark (die->die_definition, 1);
26015 if (dokids && die->die_mark != 2)
26017 /* We need to walk the children, but haven't done so yet.
26018 Remember that we've walked the kids. */
26019 die->die_mark = 2;
26021 /* If this is an array type, we need to make sure our
26022 kids get marked, even if they're types. If we're
26023 breaking out types into comdat sections, do this
26024 for all type definitions. */
26025 if (die->die_tag == DW_TAG_array_type
26026 || (use_debug_types
26027 && is_type_die (die) && ! is_declaration_die (die)))
26028 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
26029 else
26030 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26034 /* For local classes, look if any static member functions were emitted
26035 and if so, mark them. */
26037 static void
26038 prune_unused_types_walk_local_classes (dw_die_ref die)
26040 dw_die_ref c;
26042 if (die->die_mark == 2)
26043 return;
26045 switch (die->die_tag)
26047 case DW_TAG_structure_type:
26048 case DW_TAG_union_type:
26049 case DW_TAG_class_type:
26050 break;
26052 case DW_TAG_subprogram:
26053 if (!get_AT_flag (die, DW_AT_declaration)
26054 || die->die_definition != NULL)
26055 prune_unused_types_mark (die, 1);
26056 return;
26058 default:
26059 return;
26062 /* Mark children. */
26063 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
26066 /* Walk the tree DIE and mark types that we actually use. */
26068 static void
26069 prune_unused_types_walk (dw_die_ref die)
26071 dw_die_ref c;
26073 /* Don't do anything if this node is already marked and
26074 children have been marked as well. */
26075 if (die->die_mark == 2)
26076 return;
26078 switch (die->die_tag)
26080 case DW_TAG_structure_type:
26081 case DW_TAG_union_type:
26082 case DW_TAG_class_type:
26083 if (die->die_perennial_p)
26084 break;
26086 for (c = die->die_parent; c; c = c->die_parent)
26087 if (c->die_tag == DW_TAG_subprogram)
26088 break;
26090 /* Finding used static member functions inside of classes
26091 is needed just for local classes, because for other classes
26092 static member function DIEs with DW_AT_specification
26093 are emitted outside of the DW_TAG_*_type. If we ever change
26094 it, we'd need to call this even for non-local classes. */
26095 if (c)
26096 prune_unused_types_walk_local_classes (die);
26098 /* It's a type node --- don't mark it. */
26099 return;
26101 case DW_TAG_const_type:
26102 case DW_TAG_packed_type:
26103 case DW_TAG_pointer_type:
26104 case DW_TAG_reference_type:
26105 case DW_TAG_rvalue_reference_type:
26106 case DW_TAG_volatile_type:
26107 case DW_TAG_typedef:
26108 case DW_TAG_array_type:
26109 case DW_TAG_interface_type:
26110 case DW_TAG_friend:
26111 case DW_TAG_enumeration_type:
26112 case DW_TAG_subroutine_type:
26113 case DW_TAG_string_type:
26114 case DW_TAG_set_type:
26115 case DW_TAG_subrange_type:
26116 case DW_TAG_ptr_to_member_type:
26117 case DW_TAG_file_type:
26118 /* Type nodes are useful only when other DIEs reference them --- don't
26119 mark them. */
26120 /* FALLTHROUGH */
26122 case DW_TAG_dwarf_procedure:
26123 /* Likewise for DWARF procedures. */
26125 if (die->die_perennial_p)
26126 break;
26128 return;
26130 default:
26131 /* Mark everything else. */
26132 break;
26135 if (die->die_mark == 0)
26137 die->die_mark = 1;
26139 /* Now, mark any dies referenced from here. */
26140 prune_unused_types_walk_attribs (die);
26143 die->die_mark = 2;
26145 /* Mark children. */
26146 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26149 /* Increment the string counts on strings referred to from DIE's
26150 attributes. */
26152 static void
26153 prune_unused_types_update_strings (dw_die_ref die)
26155 dw_attr_node *a;
26156 unsigned ix;
26158 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26159 if (AT_class (a) == dw_val_class_str)
26161 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
26162 s->refcount++;
26163 /* Avoid unnecessarily putting strings that are used less than
26164 twice in the hash table. */
26165 if (s->refcount
26166 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
26168 indirect_string_node **slot
26169 = debug_str_hash->find_slot_with_hash (s->str,
26170 htab_hash_string (s->str),
26171 INSERT);
26172 gcc_assert (*slot == NULL);
26173 *slot = s;
26178 /* Remove from the tree DIE any dies that aren't marked. */
26180 static void
26181 prune_unused_types_prune (dw_die_ref die)
26183 dw_die_ref c;
26185 gcc_assert (die->die_mark);
26186 prune_unused_types_update_strings (die);
26188 if (! die->die_child)
26189 return;
26191 c = die->die_child;
26192 do {
26193 dw_die_ref prev = c;
26194 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
26195 if (c == die->die_child)
26197 /* No marked children between 'prev' and the end of the list. */
26198 if (prev == c)
26199 /* No marked children at all. */
26200 die->die_child = NULL;
26201 else
26203 prev->die_sib = c->die_sib;
26204 die->die_child = prev;
26206 return;
26209 if (c != prev->die_sib)
26210 prev->die_sib = c;
26211 prune_unused_types_prune (c);
26212 } while (c != die->die_child);
26215 /* Remove dies representing declarations that we never use. */
26217 static void
26218 prune_unused_types (void)
26220 unsigned int i;
26221 limbo_die_node *node;
26222 comdat_type_node *ctnode;
26223 pubname_entry *pub;
26224 dw_die_ref base_type;
26226 #if ENABLE_ASSERT_CHECKING
26227 /* All the marks should already be clear. */
26228 verify_marks_clear (comp_unit_die ());
26229 for (node = limbo_die_list; node; node = node->next)
26230 verify_marks_clear (node->die);
26231 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26232 verify_marks_clear (ctnode->root_die);
26233 #endif /* ENABLE_ASSERT_CHECKING */
26235 /* Mark types that are used in global variables. */
26236 premark_types_used_by_global_vars ();
26238 /* Set the mark on nodes that are actually used. */
26239 prune_unused_types_walk (comp_unit_die ());
26240 for (node = limbo_die_list; node; node = node->next)
26241 prune_unused_types_walk (node->die);
26242 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26244 prune_unused_types_walk (ctnode->root_die);
26245 prune_unused_types_mark (ctnode->type_die, 1);
26248 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
26249 are unusual in that they are pubnames that are the children of pubtypes.
26250 They should only be marked via their parent DW_TAG_enumeration_type die,
26251 not as roots in themselves. */
26252 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26253 if (pub->die->die_tag != DW_TAG_enumerator)
26254 prune_unused_types_mark (pub->die, 1);
26255 for (i = 0; base_types.iterate (i, &base_type); i++)
26256 prune_unused_types_mark (base_type, 1);
26258 if (debug_str_hash)
26259 debug_str_hash->empty ();
26260 if (skeleton_debug_str_hash)
26261 skeleton_debug_str_hash->empty ();
26262 prune_unused_types_prune (comp_unit_die ());
26263 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26265 node = *pnode;
26266 if (!node->die->die_mark)
26267 *pnode = node->next;
26268 else
26270 prune_unused_types_prune (node->die);
26271 pnode = &node->next;
26274 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26275 prune_unused_types_prune (ctnode->root_die);
26277 /* Leave the marks clear. */
26278 prune_unmark_dies (comp_unit_die ());
26279 for (node = limbo_die_list; node; node = node->next)
26280 prune_unmark_dies (node->die);
26281 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26282 prune_unmark_dies (ctnode->root_die);
26285 /* Set the parameter to true if there are any relative pathnames in
26286 the file table. */
26288 file_table_relative_p (dwarf_file_data **slot, bool *p)
26290 struct dwarf_file_data *d = *slot;
26291 if (!IS_ABSOLUTE_PATH (d->filename))
26293 *p = true;
26294 return 0;
26296 return 1;
26299 /* Helpers to manipulate hash table of comdat type units. */
26301 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26303 static inline hashval_t hash (const comdat_type_node *);
26304 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26307 inline hashval_t
26308 comdat_type_hasher::hash (const comdat_type_node *type_node)
26310 hashval_t h;
26311 memcpy (&h, type_node->signature, sizeof (h));
26312 return h;
26315 inline bool
26316 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26317 const comdat_type_node *type_node_2)
26319 return (! memcmp (type_node_1->signature, type_node_2->signature,
26320 DWARF_TYPE_SIGNATURE_SIZE));
26323 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26324 to the location it would have been added, should we know its
26325 DECL_ASSEMBLER_NAME when we added other attributes. This will
26326 probably improve compactness of debug info, removing equivalent
26327 abbrevs, and hide any differences caused by deferring the
26328 computation of the assembler name, triggered by e.g. PCH. */
26330 static inline void
26331 move_linkage_attr (dw_die_ref die)
26333 unsigned ix = vec_safe_length (die->die_attr);
26334 dw_attr_node linkage = (*die->die_attr)[ix - 1];
26336 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26337 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26339 while (--ix > 0)
26341 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26343 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26344 break;
26347 if (ix != vec_safe_length (die->die_attr) - 1)
26349 die->die_attr->pop ();
26350 die->die_attr->quick_insert (ix, linkage);
26354 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26355 referenced from typed stack ops and count how often they are used. */
26357 static void
26358 mark_base_types (dw_loc_descr_ref loc)
26360 dw_die_ref base_type = NULL;
26362 for (; loc; loc = loc->dw_loc_next)
26364 switch (loc->dw_loc_opc)
26366 case DW_OP_GNU_regval_type:
26367 case DW_OP_GNU_deref_type:
26368 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26369 break;
26370 case DW_OP_GNU_convert:
26371 case DW_OP_GNU_reinterpret:
26372 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26373 continue;
26374 /* FALLTHRU */
26375 case DW_OP_GNU_const_type:
26376 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26377 break;
26378 case DW_OP_GNU_entry_value:
26379 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26380 continue;
26381 default:
26382 continue;
26384 gcc_assert (base_type->die_parent == comp_unit_die ());
26385 if (base_type->die_mark)
26386 base_type->die_mark++;
26387 else
26389 base_types.safe_push (base_type);
26390 base_type->die_mark = 1;
26395 /* Comparison function for sorting marked base types. */
26397 static int
26398 base_type_cmp (const void *x, const void *y)
26400 dw_die_ref dx = *(const dw_die_ref *) x;
26401 dw_die_ref dy = *(const dw_die_ref *) y;
26402 unsigned int byte_size1, byte_size2;
26403 unsigned int encoding1, encoding2;
26404 if (dx->die_mark > dy->die_mark)
26405 return -1;
26406 if (dx->die_mark < dy->die_mark)
26407 return 1;
26408 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26409 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26410 if (byte_size1 < byte_size2)
26411 return 1;
26412 if (byte_size1 > byte_size2)
26413 return -1;
26414 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26415 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26416 if (encoding1 < encoding2)
26417 return 1;
26418 if (encoding1 > encoding2)
26419 return -1;
26420 return 0;
26423 /* Move base types marked by mark_base_types as early as possible
26424 in the CU, sorted by decreasing usage count both to make the
26425 uleb128 references as small as possible and to make sure they
26426 will have die_offset already computed by calc_die_sizes when
26427 sizes of typed stack loc ops is computed. */
26429 static void
26430 move_marked_base_types (void)
26432 unsigned int i;
26433 dw_die_ref base_type, die, c;
26435 if (base_types.is_empty ())
26436 return;
26438 /* Sort by decreasing usage count, they will be added again in that
26439 order later on. */
26440 base_types.qsort (base_type_cmp);
26441 die = comp_unit_die ();
26442 c = die->die_child;
26445 dw_die_ref prev = c;
26446 c = c->die_sib;
26447 while (c->die_mark)
26449 remove_child_with_prev (c, prev);
26450 /* As base types got marked, there must be at least
26451 one node other than DW_TAG_base_type. */
26452 gcc_assert (c != c->die_sib);
26453 c = c->die_sib;
26456 while (c != die->die_child);
26457 gcc_assert (die->die_child);
26458 c = die->die_child;
26459 for (i = 0; base_types.iterate (i, &base_type); i++)
26461 base_type->die_mark = 0;
26462 base_type->die_sib = c->die_sib;
26463 c->die_sib = base_type;
26464 c = base_type;
26468 /* Helper function for resolve_addr, attempt to resolve
26469 one CONST_STRING, return true if successful. Similarly verify that
26470 SYMBOL_REFs refer to variables emitted in the current CU. */
26472 static bool
26473 resolve_one_addr (rtx *addr)
26475 rtx rtl = *addr;
26477 if (GET_CODE (rtl) == CONST_STRING)
26479 size_t len = strlen (XSTR (rtl, 0)) + 1;
26480 tree t = build_string (len, XSTR (rtl, 0));
26481 tree tlen = size_int (len - 1);
26482 TREE_TYPE (t)
26483 = build_array_type (char_type_node, build_index_type (tlen));
26484 rtl = lookup_constant_def (t);
26485 if (!rtl || !MEM_P (rtl))
26486 return false;
26487 rtl = XEXP (rtl, 0);
26488 if (GET_CODE (rtl) == SYMBOL_REF
26489 && SYMBOL_REF_DECL (rtl)
26490 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26491 return false;
26492 vec_safe_push (used_rtx_array, rtl);
26493 *addr = rtl;
26494 return true;
26497 if (GET_CODE (rtl) == SYMBOL_REF
26498 && SYMBOL_REF_DECL (rtl))
26500 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26502 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26503 return false;
26505 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26506 return false;
26509 if (GET_CODE (rtl) == CONST)
26511 subrtx_ptr_iterator::array_type array;
26512 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26513 if (!resolve_one_addr (*iter))
26514 return false;
26517 return true;
26520 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26521 if possible, and create DW_TAG_dwarf_procedure that can be referenced
26522 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
26524 static rtx
26525 string_cst_pool_decl (tree t)
26527 rtx rtl = output_constant_def (t, 1);
26528 unsigned char *array;
26529 dw_loc_descr_ref l;
26530 tree decl;
26531 size_t len;
26532 dw_die_ref ref;
26534 if (!rtl || !MEM_P (rtl))
26535 return NULL_RTX;
26536 rtl = XEXP (rtl, 0);
26537 if (GET_CODE (rtl) != SYMBOL_REF
26538 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26539 return NULL_RTX;
26541 decl = SYMBOL_REF_DECL (rtl);
26542 if (!lookup_decl_die (decl))
26544 len = TREE_STRING_LENGTH (t);
26545 vec_safe_push (used_rtx_array, rtl);
26546 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26547 array = ggc_vec_alloc<unsigned char> (len);
26548 memcpy (array, TREE_STRING_POINTER (t), len);
26549 l = new_loc_descr (DW_OP_implicit_value, len, 0);
26550 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26551 l->dw_loc_oprnd2.v.val_vec.length = len;
26552 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26553 l->dw_loc_oprnd2.v.val_vec.array = array;
26554 add_AT_loc (ref, DW_AT_location, l);
26555 equate_decl_number_to_die (decl, ref);
26557 return rtl;
26560 /* Helper function of resolve_addr_in_expr. LOC is
26561 a DW_OP_addr followed by DW_OP_stack_value, either at the start
26562 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26563 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
26564 with DW_OP_GNU_implicit_pointer if possible
26565 and return true, if unsuccessful, return false. */
26567 static bool
26568 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26570 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26571 HOST_WIDE_INT offset = 0;
26572 dw_die_ref ref = NULL;
26573 tree decl;
26575 if (GET_CODE (rtl) == CONST
26576 && GET_CODE (XEXP (rtl, 0)) == PLUS
26577 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26579 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26580 rtl = XEXP (XEXP (rtl, 0), 0);
26582 if (GET_CODE (rtl) == CONST_STRING)
26584 size_t len = strlen (XSTR (rtl, 0)) + 1;
26585 tree t = build_string (len, XSTR (rtl, 0));
26586 tree tlen = size_int (len - 1);
26588 TREE_TYPE (t)
26589 = build_array_type (char_type_node, build_index_type (tlen));
26590 rtl = string_cst_pool_decl (t);
26591 if (!rtl)
26592 return false;
26594 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26596 decl = SYMBOL_REF_DECL (rtl);
26597 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
26599 ref = lookup_decl_die (decl);
26600 if (ref && (get_AT (ref, DW_AT_location)
26601 || get_AT (ref, DW_AT_const_value)))
26603 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26604 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26605 loc->dw_loc_oprnd1.val_entry = NULL;
26606 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26607 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26608 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26609 loc->dw_loc_oprnd2.v.val_int = offset;
26610 return true;
26614 return false;
26617 /* Helper function for resolve_addr, handle one location
26618 expression, return false if at least one CONST_STRING or SYMBOL_REF in
26619 the location list couldn't be resolved. */
26621 static bool
26622 resolve_addr_in_expr (dw_loc_descr_ref loc)
26624 dw_loc_descr_ref keep = NULL;
26625 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26626 switch (loc->dw_loc_opc)
26628 case DW_OP_addr:
26629 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26631 if ((prev == NULL
26632 || prev->dw_loc_opc == DW_OP_piece
26633 || prev->dw_loc_opc == DW_OP_bit_piece)
26634 && loc->dw_loc_next
26635 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26636 && !dwarf_strict
26637 && optimize_one_addr_into_implicit_ptr (loc))
26638 break;
26639 return false;
26641 break;
26642 case DW_OP_GNU_addr_index:
26643 case DW_OP_GNU_const_index:
26644 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26645 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26647 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26648 if (!resolve_one_addr (&rtl))
26649 return false;
26650 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26651 loc->dw_loc_oprnd1.val_entry =
26652 add_addr_table_entry (rtl, ate_kind_rtx);
26654 break;
26655 case DW_OP_const4u:
26656 case DW_OP_const8u:
26657 if (loc->dtprel
26658 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26659 return false;
26660 break;
26661 case DW_OP_plus_uconst:
26662 if (size_of_loc_descr (loc)
26663 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26665 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26667 dw_loc_descr_ref repl
26668 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26669 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26670 add_loc_descr (&repl, loc->dw_loc_next);
26671 *loc = *repl;
26673 break;
26674 case DW_OP_implicit_value:
26675 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26676 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26677 return false;
26678 break;
26679 case DW_OP_GNU_implicit_pointer:
26680 case DW_OP_GNU_parameter_ref:
26681 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26683 dw_die_ref ref
26684 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26685 if (ref == NULL)
26686 return false;
26687 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26688 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26689 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26691 break;
26692 case DW_OP_GNU_const_type:
26693 case DW_OP_GNU_regval_type:
26694 case DW_OP_GNU_deref_type:
26695 case DW_OP_GNU_convert:
26696 case DW_OP_GNU_reinterpret:
26697 while (loc->dw_loc_next
26698 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26700 dw_die_ref base1, base2;
26701 unsigned enc1, enc2, size1, size2;
26702 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26703 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26704 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26705 else if (loc->dw_loc_oprnd1.val_class
26706 == dw_val_class_unsigned_const)
26707 break;
26708 else
26709 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26710 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26711 == dw_val_class_unsigned_const)
26712 break;
26713 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26714 gcc_assert (base1->die_tag == DW_TAG_base_type
26715 && base2->die_tag == DW_TAG_base_type);
26716 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26717 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26718 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26719 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26720 if (size1 == size2
26721 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26722 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26723 && loc != keep)
26724 || enc1 == enc2))
26726 /* Optimize away next DW_OP_GNU_convert after
26727 adjusting LOC's base type die reference. */
26728 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26729 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26730 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26731 else
26732 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26733 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26734 continue;
26736 /* Don't change integer DW_OP_GNU_convert after e.g. floating
26737 point typed stack entry. */
26738 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26739 keep = loc->dw_loc_next;
26740 break;
26742 break;
26743 default:
26744 break;
26746 return true;
26749 /* Helper function of resolve_addr. DIE had DW_AT_location of
26750 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26751 and DW_OP_addr couldn't be resolved. resolve_addr has already
26752 removed the DW_AT_location attribute. This function attempts to
26753 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26754 to it or DW_AT_const_value attribute, if possible. */
26756 static void
26757 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26759 if (TREE_CODE (decl) != VAR_DECL
26760 || lookup_decl_die (decl) != die
26761 || DECL_EXTERNAL (decl)
26762 || !TREE_STATIC (decl)
26763 || DECL_INITIAL (decl) == NULL_TREE
26764 || DECL_P (DECL_INITIAL (decl))
26765 || get_AT (die, DW_AT_const_value))
26766 return;
26768 tree init = DECL_INITIAL (decl);
26769 HOST_WIDE_INT offset = 0;
26770 /* For variables that have been optimized away and thus
26771 don't have a memory location, see if we can emit
26772 DW_AT_const_value instead. */
26773 if (tree_add_const_value_attribute (die, init))
26774 return;
26775 if (dwarf_strict)
26776 return;
26777 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26778 and ADDR_EXPR refers to a decl that has DW_AT_location or
26779 DW_AT_const_value (but isn't addressable, otherwise
26780 resolving the original DW_OP_addr wouldn't fail), see if
26781 we can add DW_OP_GNU_implicit_pointer. */
26782 STRIP_NOPS (init);
26783 if (TREE_CODE (init) == POINTER_PLUS_EXPR
26784 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26786 offset = tree_to_shwi (TREE_OPERAND (init, 1));
26787 init = TREE_OPERAND (init, 0);
26788 STRIP_NOPS (init);
26790 if (TREE_CODE (init) != ADDR_EXPR)
26791 return;
26792 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26793 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26794 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26795 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26796 && TREE_OPERAND (init, 0) != decl))
26798 dw_die_ref ref;
26799 dw_loc_descr_ref l;
26801 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26803 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26804 if (!rtl)
26805 return;
26806 decl = SYMBOL_REF_DECL (rtl);
26808 else
26809 decl = TREE_OPERAND (init, 0);
26810 ref = lookup_decl_die (decl);
26811 if (ref == NULL
26812 || (!get_AT (ref, DW_AT_location)
26813 && !get_AT (ref, DW_AT_const_value)))
26814 return;
26815 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26816 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26817 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26818 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26819 add_AT_loc (die, DW_AT_location, l);
26823 /* Return NULL if l is a DWARF expression, or first op that is not
26824 valid DWARF expression. */
26826 static dw_loc_descr_ref
26827 non_dwarf_expression (dw_loc_descr_ref l)
26829 while (l)
26831 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26832 return l;
26833 switch (l->dw_loc_opc)
26835 case DW_OP_regx:
26836 case DW_OP_implicit_value:
26837 case DW_OP_stack_value:
26838 case DW_OP_GNU_implicit_pointer:
26839 case DW_OP_GNU_parameter_ref:
26840 case DW_OP_piece:
26841 case DW_OP_bit_piece:
26842 return l;
26843 default:
26844 break;
26846 l = l->dw_loc_next;
26848 return NULL;
26851 /* Return adjusted copy of EXPR:
26852 If it is empty DWARF expression, return it.
26853 If it is valid non-empty DWARF expression,
26854 return copy of EXPR with copy of DEREF appended to it.
26855 If it is DWARF expression followed by DW_OP_reg{N,x}, return
26856 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
26857 and no DEREF.
26858 If it is DWARF expression followed by DW_OP_stack_value, return
26859 copy of the DWARF expression without anything appended.
26860 Otherwise, return NULL. */
26862 static dw_loc_descr_ref
26863 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
26866 if (expr == NULL)
26867 return NULL;
26869 dw_loc_descr_ref l = non_dwarf_expression (expr);
26870 if (l && l->dw_loc_next)
26871 return NULL;
26873 if (l)
26875 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26876 deref = new_loc_descr ((enum dwarf_location_atom)
26877 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
26878 0, 0);
26879 else
26880 switch (l->dw_loc_opc)
26882 case DW_OP_regx:
26883 deref = new_loc_descr (DW_OP_bregx,
26884 l->dw_loc_oprnd1.v.val_unsigned, 0);
26885 break;
26886 case DW_OP_stack_value:
26887 deref = NULL;
26888 break;
26889 default:
26890 return NULL;
26893 else
26894 deref = new_loc_descr (deref->dw_loc_opc,
26895 deref->dw_loc_oprnd1.v.val_int, 0);
26897 dw_loc_descr_ref ret = NULL, *p = &ret;
26898 while (expr != l)
26900 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
26901 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
26902 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
26903 p = &(*p)->dw_loc_next;
26904 expr = expr->dw_loc_next;
26906 *p = deref;
26907 return ret;
26910 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
26911 or argument, adjust it if needed and return:
26912 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
26913 attribute if present should be removed
26914 0 keep the attribute as is if the referenced var or argument has
26915 only DWARF expression that covers all ranges
26916 1 if the attribute has been successfully adjusted. */
26918 static int
26919 optimize_string_length (dw_attr_node *a)
26921 dw_loc_descr_ref l = AT_loc (a), lv;
26922 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
26923 dw_attr_node *av = get_AT (die, DW_AT_location);
26924 dw_loc_list_ref d;
26925 bool non_dwarf_expr = false;
26927 if (av == NULL)
26928 return -1;
26929 switch (AT_class (av))
26931 case dw_val_class_loc_list:
26932 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26933 if (d->expr && non_dwarf_expression (d->expr))
26934 non_dwarf_expr = true;
26935 break;
26936 case dw_val_class_loc:
26937 lv = AT_loc (av);
26938 if (lv == NULL)
26939 return -1;
26940 if (non_dwarf_expression (lv))
26941 non_dwarf_expr = true;
26942 break;
26943 default:
26944 return -1;
26947 /* If it is safe to keep DW_OP_call4 in, keep it. */
26948 if (!non_dwarf_expr
26949 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
26950 return 0;
26952 /* If not dereferencing the DW_OP_call4 afterwards, we can just
26953 copy over the DW_AT_location attribute from die to a. */
26954 if (l->dw_loc_next == NULL)
26956 a->dw_attr_val = av->dw_attr_val;
26957 return 1;
26960 dw_loc_list_ref list, *p;
26961 switch (AT_class (av))
26963 case dw_val_class_loc_list:
26964 p = &list;
26965 list = NULL;
26966 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26968 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
26969 if (lv)
26971 *p = new_loc_list (lv, d->begin, d->end, d->section);
26972 p = &(*p)->dw_loc_next;
26975 if (list == NULL)
26976 return -1;
26977 a->dw_attr_val.val_class = dw_val_class_loc_list;
26978 gen_llsym (list);
26979 *AT_loc_list_ptr (a) = list;
26980 return 1;
26981 case dw_val_class_loc:
26982 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
26983 if (lv == NULL)
26984 return -1;
26985 a->dw_attr_val.v.val_loc = lv;
26986 return 1;
26987 default:
26988 gcc_unreachable ();
26992 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
26993 an address in .rodata section if the string literal is emitted there,
26994 or remove the containing location list or replace DW_AT_const_value
26995 with DW_AT_location and empty location expression, if it isn't found
26996 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
26997 to something that has been emitted in the current CU. */
26999 static void
27000 resolve_addr (dw_die_ref die)
27002 dw_die_ref c;
27003 dw_attr_node *a;
27004 dw_loc_list_ref *curr, *start, loc;
27005 unsigned ix;
27006 bool remove_AT_byte_size = false;
27008 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27009 switch (AT_class (a))
27011 case dw_val_class_loc_list:
27012 start = curr = AT_loc_list_ptr (a);
27013 loc = *curr;
27014 gcc_assert (loc);
27015 /* The same list can be referenced more than once. See if we have
27016 already recorded the result from a previous pass. */
27017 if (loc->replaced)
27018 *curr = loc->dw_loc_next;
27019 else if (!loc->resolved_addr)
27021 /* As things stand, we do not expect or allow one die to
27022 reference a suffix of another die's location list chain.
27023 References must be identical or completely separate.
27024 There is therefore no need to cache the result of this
27025 pass on any list other than the first; doing so
27026 would lead to unnecessary writes. */
27027 while (*curr)
27029 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
27030 if (!resolve_addr_in_expr ((*curr)->expr))
27032 dw_loc_list_ref next = (*curr)->dw_loc_next;
27033 dw_loc_descr_ref l = (*curr)->expr;
27035 if (next && (*curr)->ll_symbol)
27037 gcc_assert (!next->ll_symbol);
27038 next->ll_symbol = (*curr)->ll_symbol;
27040 if (dwarf_split_debug_info)
27041 remove_loc_list_addr_table_entries (l);
27042 *curr = next;
27044 else
27046 mark_base_types ((*curr)->expr);
27047 curr = &(*curr)->dw_loc_next;
27050 if (loc == *start)
27051 loc->resolved_addr = 1;
27052 else
27054 loc->replaced = 1;
27055 loc->dw_loc_next = *start;
27058 if (!*start)
27060 remove_AT (die, a->dw_attr);
27061 ix--;
27063 break;
27064 case dw_val_class_loc:
27066 dw_loc_descr_ref l = AT_loc (a);
27067 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
27068 DW_AT_string_length is only a rough approximation; unfortunately
27069 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
27070 needs a DWARF expression, while DW_AT_location of the referenced
27071 variable or argument might be any location description. */
27072 if (a->dw_attr == DW_AT_string_length
27073 && l
27074 && l->dw_loc_opc == DW_OP_call4
27075 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
27076 && (l->dw_loc_next == NULL
27077 || (l->dw_loc_next->dw_loc_next == NULL
27078 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
27079 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
27081 switch (optimize_string_length (a))
27083 case -1:
27084 remove_AT (die, a->dw_attr);
27085 ix--;
27086 /* If we drop DW_AT_string_length, we need to drop also
27087 DW_AT_{string_length_,}byte_size. */
27088 remove_AT_byte_size = true;
27089 continue;
27090 default:
27091 break;
27092 case 1:
27093 /* Even if we keep the optimized DW_AT_string_length,
27094 it might have changed AT_class, so process it again. */
27095 ix--;
27096 continue;
27099 /* For -gdwarf-2 don't attempt to optimize
27100 DW_AT_data_member_location containing
27101 DW_OP_plus_uconst - older consumers might
27102 rely on it being that op instead of a more complex,
27103 but shorter, location description. */
27104 if ((dwarf_version > 2
27105 || a->dw_attr != DW_AT_data_member_location
27106 || l == NULL
27107 || l->dw_loc_opc != DW_OP_plus_uconst
27108 || l->dw_loc_next != NULL)
27109 && !resolve_addr_in_expr (l))
27111 if (dwarf_split_debug_info)
27112 remove_loc_list_addr_table_entries (l);
27113 if (l != NULL
27114 && l->dw_loc_next == NULL
27115 && l->dw_loc_opc == DW_OP_addr
27116 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
27117 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
27118 && a->dw_attr == DW_AT_location)
27120 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
27121 remove_AT (die, a->dw_attr);
27122 ix--;
27123 optimize_location_into_implicit_ptr (die, decl);
27124 break;
27126 remove_AT (die, a->dw_attr);
27127 ix--;
27129 else
27130 mark_base_types (l);
27132 break;
27133 case dw_val_class_addr:
27134 if (a->dw_attr == DW_AT_const_value
27135 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
27137 if (AT_index (a) != NOT_INDEXED)
27138 remove_addr_table_entry (a->dw_attr_val.val_entry);
27139 remove_AT (die, a->dw_attr);
27140 ix--;
27142 if (die->die_tag == DW_TAG_GNU_call_site
27143 && a->dw_attr == DW_AT_abstract_origin)
27145 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
27146 dw_die_ref tdie = lookup_decl_die (tdecl);
27147 dw_die_ref cdie;
27148 if (tdie == NULL
27149 && DECL_EXTERNAL (tdecl)
27150 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
27151 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
27153 /* Creating a full DIE for tdecl is overly expensive and
27154 at this point even wrong when in the LTO phase
27155 as it can end up generating new type DIEs we didn't
27156 output and thus optimize_external_refs will crash. */
27157 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
27158 add_AT_flag (tdie, DW_AT_external, 1);
27159 add_AT_flag (tdie, DW_AT_declaration, 1);
27160 add_linkage_attr (tdie, tdecl);
27161 add_name_and_src_coords_attributes (tdie, tdecl);
27162 equate_decl_number_to_die (tdecl, tdie);
27164 if (tdie)
27166 a->dw_attr_val.val_class = dw_val_class_die_ref;
27167 a->dw_attr_val.v.val_die_ref.die = tdie;
27168 a->dw_attr_val.v.val_die_ref.external = 0;
27170 else
27172 if (AT_index (a) != NOT_INDEXED)
27173 remove_addr_table_entry (a->dw_attr_val.val_entry);
27174 remove_AT (die, a->dw_attr);
27175 ix--;
27178 break;
27179 default:
27180 break;
27183 if (remove_AT_byte_size)
27184 remove_AT (die, dwarf_version >= 5
27185 ? DW_AT_string_length_byte_size
27186 : DW_AT_byte_size);
27188 FOR_EACH_CHILD (die, c, resolve_addr (c));
27191 /* Helper routines for optimize_location_lists.
27192 This pass tries to share identical local lists in .debug_loc
27193 section. */
27195 /* Iteratively hash operands of LOC opcode into HSTATE. */
27197 static void
27198 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
27200 dw_val_ref val1 = &loc->dw_loc_oprnd1;
27201 dw_val_ref val2 = &loc->dw_loc_oprnd2;
27203 switch (loc->dw_loc_opc)
27205 case DW_OP_const4u:
27206 case DW_OP_const8u:
27207 if (loc->dtprel)
27208 goto hash_addr;
27209 /* FALLTHRU */
27210 case DW_OP_const1u:
27211 case DW_OP_const1s:
27212 case DW_OP_const2u:
27213 case DW_OP_const2s:
27214 case DW_OP_const4s:
27215 case DW_OP_const8s:
27216 case DW_OP_constu:
27217 case DW_OP_consts:
27218 case DW_OP_pick:
27219 case DW_OP_plus_uconst:
27220 case DW_OP_breg0:
27221 case DW_OP_breg1:
27222 case DW_OP_breg2:
27223 case DW_OP_breg3:
27224 case DW_OP_breg4:
27225 case DW_OP_breg5:
27226 case DW_OP_breg6:
27227 case DW_OP_breg7:
27228 case DW_OP_breg8:
27229 case DW_OP_breg9:
27230 case DW_OP_breg10:
27231 case DW_OP_breg11:
27232 case DW_OP_breg12:
27233 case DW_OP_breg13:
27234 case DW_OP_breg14:
27235 case DW_OP_breg15:
27236 case DW_OP_breg16:
27237 case DW_OP_breg17:
27238 case DW_OP_breg18:
27239 case DW_OP_breg19:
27240 case DW_OP_breg20:
27241 case DW_OP_breg21:
27242 case DW_OP_breg22:
27243 case DW_OP_breg23:
27244 case DW_OP_breg24:
27245 case DW_OP_breg25:
27246 case DW_OP_breg26:
27247 case DW_OP_breg27:
27248 case DW_OP_breg28:
27249 case DW_OP_breg29:
27250 case DW_OP_breg30:
27251 case DW_OP_breg31:
27252 case DW_OP_regx:
27253 case DW_OP_fbreg:
27254 case DW_OP_piece:
27255 case DW_OP_deref_size:
27256 case DW_OP_xderef_size:
27257 hstate.add_object (val1->v.val_int);
27258 break;
27259 case DW_OP_skip:
27260 case DW_OP_bra:
27262 int offset;
27264 gcc_assert (val1->val_class == dw_val_class_loc);
27265 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
27266 hstate.add_object (offset);
27268 break;
27269 case DW_OP_implicit_value:
27270 hstate.add_object (val1->v.val_unsigned);
27271 switch (val2->val_class)
27273 case dw_val_class_const:
27274 hstate.add_object (val2->v.val_int);
27275 break;
27276 case dw_val_class_vec:
27278 unsigned int elt_size = val2->v.val_vec.elt_size;
27279 unsigned int len = val2->v.val_vec.length;
27281 hstate.add_int (elt_size);
27282 hstate.add_int (len);
27283 hstate.add (val2->v.val_vec.array, len * elt_size);
27285 break;
27286 case dw_val_class_const_double:
27287 hstate.add_object (val2->v.val_double.low);
27288 hstate.add_object (val2->v.val_double.high);
27289 break;
27290 case dw_val_class_wide_int:
27291 hstate.add (val2->v.val_wide->get_val (),
27292 get_full_len (*val2->v.val_wide)
27293 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27294 break;
27295 case dw_val_class_addr:
27296 inchash::add_rtx (val2->v.val_addr, hstate);
27297 break;
27298 default:
27299 gcc_unreachable ();
27301 break;
27302 case DW_OP_bregx:
27303 case DW_OP_bit_piece:
27304 hstate.add_object (val1->v.val_int);
27305 hstate.add_object (val2->v.val_int);
27306 break;
27307 case DW_OP_addr:
27308 hash_addr:
27309 if (loc->dtprel)
27311 unsigned char dtprel = 0xd1;
27312 hstate.add_object (dtprel);
27314 inchash::add_rtx (val1->v.val_addr, hstate);
27315 break;
27316 case DW_OP_GNU_addr_index:
27317 case DW_OP_GNU_const_index:
27319 if (loc->dtprel)
27321 unsigned char dtprel = 0xd1;
27322 hstate.add_object (dtprel);
27324 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
27326 break;
27327 case DW_OP_GNU_implicit_pointer:
27328 hstate.add_int (val2->v.val_int);
27329 break;
27330 case DW_OP_GNU_entry_value:
27331 hstate.add_object (val1->v.val_loc);
27332 break;
27333 case DW_OP_GNU_regval_type:
27334 case DW_OP_GNU_deref_type:
27336 unsigned int byte_size
27337 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
27338 unsigned int encoding
27339 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
27340 hstate.add_object (val1->v.val_int);
27341 hstate.add_object (byte_size);
27342 hstate.add_object (encoding);
27344 break;
27345 case DW_OP_GNU_convert:
27346 case DW_OP_GNU_reinterpret:
27347 if (val1->val_class == dw_val_class_unsigned_const)
27349 hstate.add_object (val1->v.val_unsigned);
27350 break;
27352 /* FALLTHRU */
27353 case DW_OP_GNU_const_type:
27355 unsigned int byte_size
27356 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
27357 unsigned int encoding
27358 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
27359 hstate.add_object (byte_size);
27360 hstate.add_object (encoding);
27361 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
27362 break;
27363 hstate.add_object (val2->val_class);
27364 switch (val2->val_class)
27366 case dw_val_class_const:
27367 hstate.add_object (val2->v.val_int);
27368 break;
27369 case dw_val_class_vec:
27371 unsigned int elt_size = val2->v.val_vec.elt_size;
27372 unsigned int len = val2->v.val_vec.length;
27374 hstate.add_object (elt_size);
27375 hstate.add_object (len);
27376 hstate.add (val2->v.val_vec.array, len * elt_size);
27378 break;
27379 case dw_val_class_const_double:
27380 hstate.add_object (val2->v.val_double.low);
27381 hstate.add_object (val2->v.val_double.high);
27382 break;
27383 case dw_val_class_wide_int:
27384 hstate.add (val2->v.val_wide->get_val (),
27385 get_full_len (*val2->v.val_wide)
27386 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27387 break;
27388 default:
27389 gcc_unreachable ();
27392 break;
27394 default:
27395 /* Other codes have no operands. */
27396 break;
27400 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
27402 static inline void
27403 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
27405 dw_loc_descr_ref l;
27406 bool sizes_computed = false;
27407 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
27408 size_of_locs (loc);
27410 for (l = loc; l != NULL; l = l->dw_loc_next)
27412 enum dwarf_location_atom opc = l->dw_loc_opc;
27413 hstate.add_object (opc);
27414 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
27416 size_of_locs (loc);
27417 sizes_computed = true;
27419 hash_loc_operands (l, hstate);
27423 /* Compute hash of the whole location list LIST_HEAD. */
27425 static inline void
27426 hash_loc_list (dw_loc_list_ref list_head)
27428 dw_loc_list_ref curr = list_head;
27429 inchash::hash hstate;
27431 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
27433 hstate.add (curr->begin, strlen (curr->begin) + 1);
27434 hstate.add (curr->end, strlen (curr->end) + 1);
27435 if (curr->section)
27436 hstate.add (curr->section, strlen (curr->section) + 1);
27437 hash_locs (curr->expr, hstate);
27439 list_head->hash = hstate.end ();
27442 /* Return true if X and Y opcodes have the same operands. */
27444 static inline bool
27445 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
27447 dw_val_ref valx1 = &x->dw_loc_oprnd1;
27448 dw_val_ref valx2 = &x->dw_loc_oprnd2;
27449 dw_val_ref valy1 = &y->dw_loc_oprnd1;
27450 dw_val_ref valy2 = &y->dw_loc_oprnd2;
27452 switch (x->dw_loc_opc)
27454 case DW_OP_const4u:
27455 case DW_OP_const8u:
27456 if (x->dtprel)
27457 goto hash_addr;
27458 /* FALLTHRU */
27459 case DW_OP_const1u:
27460 case DW_OP_const1s:
27461 case DW_OP_const2u:
27462 case DW_OP_const2s:
27463 case DW_OP_const4s:
27464 case DW_OP_const8s:
27465 case DW_OP_constu:
27466 case DW_OP_consts:
27467 case DW_OP_pick:
27468 case DW_OP_plus_uconst:
27469 case DW_OP_breg0:
27470 case DW_OP_breg1:
27471 case DW_OP_breg2:
27472 case DW_OP_breg3:
27473 case DW_OP_breg4:
27474 case DW_OP_breg5:
27475 case DW_OP_breg6:
27476 case DW_OP_breg7:
27477 case DW_OP_breg8:
27478 case DW_OP_breg9:
27479 case DW_OP_breg10:
27480 case DW_OP_breg11:
27481 case DW_OP_breg12:
27482 case DW_OP_breg13:
27483 case DW_OP_breg14:
27484 case DW_OP_breg15:
27485 case DW_OP_breg16:
27486 case DW_OP_breg17:
27487 case DW_OP_breg18:
27488 case DW_OP_breg19:
27489 case DW_OP_breg20:
27490 case DW_OP_breg21:
27491 case DW_OP_breg22:
27492 case DW_OP_breg23:
27493 case DW_OP_breg24:
27494 case DW_OP_breg25:
27495 case DW_OP_breg26:
27496 case DW_OP_breg27:
27497 case DW_OP_breg28:
27498 case DW_OP_breg29:
27499 case DW_OP_breg30:
27500 case DW_OP_breg31:
27501 case DW_OP_regx:
27502 case DW_OP_fbreg:
27503 case DW_OP_piece:
27504 case DW_OP_deref_size:
27505 case DW_OP_xderef_size:
27506 return valx1->v.val_int == valy1->v.val_int;
27507 case DW_OP_skip:
27508 case DW_OP_bra:
27509 /* If splitting debug info, the use of DW_OP_GNU_addr_index
27510 can cause irrelevant differences in dw_loc_addr. */
27511 gcc_assert (valx1->val_class == dw_val_class_loc
27512 && valy1->val_class == dw_val_class_loc
27513 && (dwarf_split_debug_info
27514 || x->dw_loc_addr == y->dw_loc_addr));
27515 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27516 case DW_OP_implicit_value:
27517 if (valx1->v.val_unsigned != valy1->v.val_unsigned
27518 || valx2->val_class != valy2->val_class)
27519 return false;
27520 switch (valx2->val_class)
27522 case dw_val_class_const:
27523 return valx2->v.val_int == valy2->v.val_int;
27524 case dw_val_class_vec:
27525 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27526 && valx2->v.val_vec.length == valy2->v.val_vec.length
27527 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27528 valx2->v.val_vec.elt_size
27529 * valx2->v.val_vec.length) == 0;
27530 case dw_val_class_const_double:
27531 return valx2->v.val_double.low == valy2->v.val_double.low
27532 && valx2->v.val_double.high == valy2->v.val_double.high;
27533 case dw_val_class_wide_int:
27534 return *valx2->v.val_wide == *valy2->v.val_wide;
27535 case dw_val_class_addr:
27536 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27537 default:
27538 gcc_unreachable ();
27540 case DW_OP_bregx:
27541 case DW_OP_bit_piece:
27542 return valx1->v.val_int == valy1->v.val_int
27543 && valx2->v.val_int == valy2->v.val_int;
27544 case DW_OP_addr:
27545 hash_addr:
27546 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27547 case DW_OP_GNU_addr_index:
27548 case DW_OP_GNU_const_index:
27550 rtx ax1 = valx1->val_entry->addr.rtl;
27551 rtx ay1 = valy1->val_entry->addr.rtl;
27552 return rtx_equal_p (ax1, ay1);
27554 case DW_OP_GNU_implicit_pointer:
27555 return valx1->val_class == dw_val_class_die_ref
27556 && valx1->val_class == valy1->val_class
27557 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27558 && valx2->v.val_int == valy2->v.val_int;
27559 case DW_OP_GNU_entry_value:
27560 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27561 case DW_OP_GNU_const_type:
27562 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27563 || valx2->val_class != valy2->val_class)
27564 return false;
27565 switch (valx2->val_class)
27567 case dw_val_class_const:
27568 return valx2->v.val_int == valy2->v.val_int;
27569 case dw_val_class_vec:
27570 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27571 && valx2->v.val_vec.length == valy2->v.val_vec.length
27572 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27573 valx2->v.val_vec.elt_size
27574 * valx2->v.val_vec.length) == 0;
27575 case dw_val_class_const_double:
27576 return valx2->v.val_double.low == valy2->v.val_double.low
27577 && valx2->v.val_double.high == valy2->v.val_double.high;
27578 case dw_val_class_wide_int:
27579 return *valx2->v.val_wide == *valy2->v.val_wide;
27580 default:
27581 gcc_unreachable ();
27583 case DW_OP_GNU_regval_type:
27584 case DW_OP_GNU_deref_type:
27585 return valx1->v.val_int == valy1->v.val_int
27586 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27587 case DW_OP_GNU_convert:
27588 case DW_OP_GNU_reinterpret:
27589 if (valx1->val_class != valy1->val_class)
27590 return false;
27591 if (valx1->val_class == dw_val_class_unsigned_const)
27592 return valx1->v.val_unsigned == valy1->v.val_unsigned;
27593 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27594 case DW_OP_GNU_parameter_ref:
27595 return valx1->val_class == dw_val_class_die_ref
27596 && valx1->val_class == valy1->val_class
27597 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27598 default:
27599 /* Other codes have no operands. */
27600 return true;
27604 /* Return true if DWARF location expressions X and Y are the same. */
27606 static inline bool
27607 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27609 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27610 if (x->dw_loc_opc != y->dw_loc_opc
27611 || x->dtprel != y->dtprel
27612 || !compare_loc_operands (x, y))
27613 break;
27614 return x == NULL && y == NULL;
27617 /* Hashtable helpers. */
27619 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27621 static inline hashval_t hash (const dw_loc_list_struct *);
27622 static inline bool equal (const dw_loc_list_struct *,
27623 const dw_loc_list_struct *);
27626 /* Return precomputed hash of location list X. */
27628 inline hashval_t
27629 loc_list_hasher::hash (const dw_loc_list_struct *x)
27631 return x->hash;
27634 /* Return true if location lists A and B are the same. */
27636 inline bool
27637 loc_list_hasher::equal (const dw_loc_list_struct *a,
27638 const dw_loc_list_struct *b)
27640 if (a == b)
27641 return 1;
27642 if (a->hash != b->hash)
27643 return 0;
27644 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27645 if (strcmp (a->begin, b->begin) != 0
27646 || strcmp (a->end, b->end) != 0
27647 || (a->section == NULL) != (b->section == NULL)
27648 || (a->section && strcmp (a->section, b->section) != 0)
27649 || !compare_locs (a->expr, b->expr))
27650 break;
27651 return a == NULL && b == NULL;
27654 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27657 /* Recursively optimize location lists referenced from DIE
27658 children and share them whenever possible. */
27660 static void
27661 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27663 dw_die_ref c;
27664 dw_attr_node *a;
27665 unsigned ix;
27666 dw_loc_list_struct **slot;
27668 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27669 if (AT_class (a) == dw_val_class_loc_list)
27671 dw_loc_list_ref list = AT_loc_list (a);
27672 /* TODO: perform some optimizations here, before hashing
27673 it and storing into the hash table. */
27674 hash_loc_list (list);
27675 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27676 if (*slot == NULL)
27677 *slot = list;
27678 else
27679 a->dw_attr_val.v.val_loc_list = *slot;
27682 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27686 /* Recursively assign each location list a unique index into the debug_addr
27687 section. */
27689 static void
27690 index_location_lists (dw_die_ref die)
27692 dw_die_ref c;
27693 dw_attr_node *a;
27694 unsigned ix;
27696 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27697 if (AT_class (a) == dw_val_class_loc_list)
27699 dw_loc_list_ref list = AT_loc_list (a);
27700 dw_loc_list_ref curr;
27701 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27703 /* Don't index an entry that has already been indexed
27704 or won't be output. */
27705 if (curr->begin_entry != NULL
27706 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27707 continue;
27709 curr->begin_entry
27710 = add_addr_table_entry (xstrdup (curr->begin),
27711 ate_kind_label);
27715 FOR_EACH_CHILD (die, c, index_location_lists (c));
27718 /* Optimize location lists referenced from DIE
27719 children and share them whenever possible. */
27721 static void
27722 optimize_location_lists (dw_die_ref die)
27724 loc_list_hash_type htab (500);
27725 optimize_location_lists_1 (die, &htab);
27728 /* Traverse the limbo die list, and add parent/child links. The only
27729 dies without parents that should be here are concrete instances of
27730 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
27731 For concrete instances, we can get the parent die from the abstract
27732 instance. */
27734 static void
27735 flush_limbo_die_list (void)
27737 limbo_die_node *node;
27739 /* get_context_die calls force_decl_die, which can put new DIEs on the
27740 limbo list in LTO mode when nested functions are put in a different
27741 partition than that of their parent function. */
27742 while ((node = limbo_die_list))
27744 dw_die_ref die = node->die;
27745 limbo_die_list = node->next;
27747 if (die->die_parent == NULL)
27749 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27751 if (origin && origin->die_parent)
27752 add_child_die (origin->die_parent, die);
27753 else if (is_cu_die (die))
27755 else if (seen_error ())
27756 /* It's OK to be confused by errors in the input. */
27757 add_child_die (comp_unit_die (), die);
27758 else
27760 /* In certain situations, the lexical block containing a
27761 nested function can be optimized away, which results
27762 in the nested function die being orphaned. Likewise
27763 with the return type of that nested function. Force
27764 this to be a child of the containing function.
27766 It may happen that even the containing function got fully
27767 inlined and optimized out. In that case we are lost and
27768 assign the empty child. This should not be big issue as
27769 the function is likely unreachable too. */
27770 gcc_assert (node->created_for);
27772 if (DECL_P (node->created_for))
27773 origin = get_context_die (DECL_CONTEXT (node->created_for));
27774 else if (TYPE_P (node->created_for))
27775 origin = scope_die_for (node->created_for, comp_unit_die ());
27776 else
27777 origin = comp_unit_die ();
27779 add_child_die (origin, die);
27785 /* Output stuff that dwarf requires at the end of every file,
27786 and generate the DWARF-2 debugging info. */
27788 static void
27789 dwarf2out_finish (const char *filename)
27791 comdat_type_node *ctnode;
27792 dw_die_ref main_comp_unit_die;
27794 /* Flush out any latecomers to the limbo party. */
27795 flush_limbo_die_list ();
27797 /* We shouldn't have any symbols with delayed asm names for
27798 DIEs generated after early finish. */
27799 gcc_assert (deferred_asm_name == NULL);
27801 /* PCH might result in DW_AT_producer string being restored from the
27802 header compilation, so always fill it with empty string initially
27803 and overwrite only here. */
27804 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
27805 producer_string = gen_producer_string ();
27806 producer->dw_attr_val.v.val_str->refcount--;
27807 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
27809 gen_remaining_tmpl_value_param_die_attribute ();
27811 /* Add the name for the main input file now. We delayed this from
27812 dwarf2out_init to avoid complications with PCH.
27813 For LTO produced units use a fixed artificial name to avoid
27814 leaking tempfile names into the dwarf. */
27815 if (!in_lto_p)
27816 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
27817 else
27818 add_name_attribute (comp_unit_die (), "<artificial>");
27819 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
27820 add_comp_dir_attribute (comp_unit_die ());
27821 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
27823 bool p = false;
27824 file_table->traverse<bool *, file_table_relative_p> (&p);
27825 if (p)
27826 add_comp_dir_attribute (comp_unit_die ());
27829 #if ENABLE_ASSERT_CHECKING
27831 dw_die_ref die = comp_unit_die (), c;
27832 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27834 #endif
27835 resolve_addr (comp_unit_die ());
27836 move_marked_base_types ();
27838 if (flag_eliminate_unused_debug_types)
27839 prune_unused_types ();
27841 /* Generate separate COMDAT sections for type DIEs. */
27842 if (use_debug_types)
27844 break_out_comdat_types (comp_unit_die ());
27846 /* Each new type_unit DIE was added to the limbo die list when created.
27847 Since these have all been added to comdat_type_list, clear the
27848 limbo die list. */
27849 limbo_die_list = NULL;
27851 /* For each new comdat type unit, copy declarations for incomplete
27852 types to make the new unit self-contained (i.e., no direct
27853 references to the main compile unit). */
27854 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27855 copy_decls_for_unworthy_types (ctnode->root_die);
27856 copy_decls_for_unworthy_types (comp_unit_die ());
27858 /* In the process of copying declarations from one unit to another,
27859 we may have left some declarations behind that are no longer
27860 referenced. Prune them. */
27861 prune_unused_types ();
27864 /* Generate separate CUs for each of the include files we've seen.
27865 They will go into limbo_die_list. */
27866 if (flag_eliminate_dwarf2_dups)
27867 break_out_includes (comp_unit_die ());
27869 /* Traverse the DIE's and add sibling attributes to those DIE's that
27870 have children. */
27871 add_sibling_attributes (comp_unit_die ());
27872 limbo_die_node *node;
27873 for (node = limbo_die_list; node; node = node->next)
27874 add_sibling_attributes (node->die);
27875 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27876 add_sibling_attributes (ctnode->root_die);
27878 /* When splitting DWARF info, we put some attributes in the
27879 skeleton compile_unit DIE that remains in the .o, while
27880 most attributes go in the DWO compile_unit_die. */
27881 if (dwarf_split_debug_info)
27882 main_comp_unit_die = gen_compile_unit_die (NULL);
27883 else
27884 main_comp_unit_die = comp_unit_die ();
27886 /* Output a terminator label for the .text section. */
27887 switch_to_section (text_section);
27888 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27889 if (cold_text_section)
27891 switch_to_section (cold_text_section);
27892 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27895 /* We can only use the low/high_pc attributes if all of the code was
27896 in .text. */
27897 if (!have_multiple_function_sections
27898 || (dwarf_version < 3 && dwarf_strict))
27900 /* Don't add if the CU has no associated code. */
27901 if (text_section_used)
27902 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27903 text_end_label, true);
27905 else
27907 unsigned fde_idx;
27908 dw_fde_ref fde;
27909 bool range_list_added = false;
27911 if (text_section_used)
27912 add_ranges_by_labels (main_comp_unit_die, text_section_label,
27913 text_end_label, &range_list_added, true);
27914 if (cold_text_section_used)
27915 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27916 cold_end_label, &range_list_added, true);
27918 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27920 if (DECL_IGNORED_P (fde->decl))
27921 continue;
27922 if (!fde->in_std_section)
27923 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27924 fde->dw_fde_end, &range_list_added,
27925 true);
27926 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27927 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27928 fde->dw_fde_second_end, &range_list_added,
27929 true);
27932 if (range_list_added)
27934 /* We need to give .debug_loc and .debug_ranges an appropriate
27935 "base address". Use zero so that these addresses become
27936 absolute. Historically, we've emitted the unexpected
27937 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27938 Emit both to give time for other tools to adapt. */
27939 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27940 if (! dwarf_strict && dwarf_version < 4)
27941 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27943 add_ranges (NULL);
27947 if (debug_info_level >= DINFO_LEVEL_TERSE)
27948 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27949 debug_line_section_label);
27951 if (have_macinfo)
27952 add_AT_macptr (comp_unit_die (),
27953 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
27954 macinfo_section_label);
27956 if (dwarf_split_debug_info)
27958 /* optimize_location_lists calculates the size of the lists,
27959 so index them first, and assign indices to the entries.
27960 Although optimize_location_lists will remove entries from
27961 the table, it only does so for duplicates, and therefore
27962 only reduces ref_counts to 1. */
27963 index_location_lists (comp_unit_die ());
27965 if (addr_index_table != NULL)
27967 unsigned int index = 0;
27968 addr_index_table
27969 ->traverse_noresize<unsigned int *, index_addr_table_entry>
27970 (&index);
27974 if (have_location_lists)
27975 optimize_location_lists (comp_unit_die ());
27977 save_macinfo_strings ();
27979 if (dwarf_split_debug_info)
27981 unsigned int index = 0;
27983 /* Add attributes common to skeleton compile_units and
27984 type_units. Because these attributes include strings, it
27985 must be done before freezing the string table. Top-level
27986 skeleton die attrs are added when the skeleton type unit is
27987 created, so ensure it is created by this point. */
27988 add_top_level_skeleton_die_attrs (main_comp_unit_die);
27989 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
27992 /* Output all of the compilation units. We put the main one last so that
27993 the offsets are available to output_pubnames. */
27994 for (node = limbo_die_list; node; node = node->next)
27995 output_comp_unit (node->die, 0);
27997 hash_table<comdat_type_hasher> comdat_type_table (100);
27998 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
28000 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
28002 /* Don't output duplicate types. */
28003 if (*slot != HTAB_EMPTY_ENTRY)
28004 continue;
28006 /* Add a pointer to the line table for the main compilation unit
28007 so that the debugger can make sense of DW_AT_decl_file
28008 attributes. */
28009 if (debug_info_level >= DINFO_LEVEL_TERSE)
28010 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
28011 (!dwarf_split_debug_info
28012 ? debug_line_section_label
28013 : debug_skeleton_line_section_label));
28015 output_comdat_type_unit (ctnode);
28016 *slot = ctnode;
28019 /* The AT_pubnames attribute needs to go in all skeleton dies, including
28020 both the main_cu and all skeleton TUs. Making this call unconditional
28021 would end up either adding a second copy of the AT_pubnames attribute, or
28022 requiring a special case in add_top_level_skeleton_die_attrs. */
28023 if (!dwarf_split_debug_info)
28024 add_AT_pubnames (comp_unit_die ());
28026 if (dwarf_split_debug_info)
28028 int mark;
28029 unsigned char checksum[16];
28030 struct md5_ctx ctx;
28032 /* Compute a checksum of the comp_unit to use as the dwo_id. */
28033 md5_init_ctx (&ctx);
28034 mark = 0;
28035 die_checksum (comp_unit_die (), &ctx, &mark);
28036 unmark_all_dies (comp_unit_die ());
28037 md5_finish_ctx (&ctx, checksum);
28039 /* Use the first 8 bytes of the checksum as the dwo_id,
28040 and add it to both comp-unit DIEs. */
28041 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
28042 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
28044 /* Add the base offset of the ranges table to the skeleton
28045 comp-unit DIE. */
28046 if (ranges_table_in_use)
28047 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
28048 ranges_section_label);
28050 switch_to_section (debug_addr_section);
28051 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
28052 output_addr_table ();
28055 /* Output the main compilation unit if non-empty or if .debug_macinfo
28056 or .debug_macro will be emitted. */
28057 output_comp_unit (comp_unit_die (), have_macinfo);
28059 if (dwarf_split_debug_info && info_section_emitted)
28060 output_skeleton_debug_sections (main_comp_unit_die);
28062 /* Output the abbreviation table. */
28063 if (abbrev_die_table_in_use != 1)
28065 switch_to_section (debug_abbrev_section);
28066 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
28067 output_abbrev_section ();
28070 /* Output location list section if necessary. */
28071 if (have_location_lists)
28073 /* Output the location lists info. */
28074 switch_to_section (debug_loc_section);
28075 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
28076 output_location_lists (comp_unit_die ());
28079 output_pubtables ();
28081 /* Output the address range information if a CU (.debug_info section)
28082 was emitted. We output an empty table even if we had no functions
28083 to put in it. This because the consumer has no way to tell the
28084 difference between an empty table that we omitted and failure to
28085 generate a table that would have contained data. */
28086 if (info_section_emitted)
28088 switch_to_section (debug_aranges_section);
28089 output_aranges ();
28092 /* Output ranges section if necessary. */
28093 if (ranges_table_in_use)
28095 switch_to_section (debug_ranges_section);
28096 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
28097 output_ranges ();
28100 /* Have to end the macro section. */
28101 if (have_macinfo)
28103 switch_to_section (debug_macinfo_section);
28104 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
28105 output_macinfo ();
28106 dw2_asm_output_data (1, 0, "End compilation unit");
28109 /* Output the source line correspondence table. We must do this
28110 even if there is no line information. Otherwise, on an empty
28111 translation unit, we will generate a present, but empty,
28112 .debug_info section. IRIX 6.5 `nm' will then complain when
28113 examining the file. This is done late so that any filenames
28114 used by the debug_info section are marked as 'used'. */
28115 switch_to_section (debug_line_section);
28116 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
28117 if (! DWARF2_ASM_LINE_DEBUG_INFO)
28118 output_line_info (false);
28120 if (dwarf_split_debug_info && info_section_emitted)
28122 switch_to_section (debug_skeleton_line_section);
28123 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
28124 output_line_info (true);
28127 /* If we emitted any indirect strings, output the string table too. */
28128 if (debug_str_hash || skeleton_debug_str_hash)
28129 output_indirect_strings ();
28132 /* Perform any cleanups needed after the early debug generation pass
28133 has run. */
28135 static void
28136 dwarf2out_early_finish (void)
28138 set_early_dwarf s;
28140 /* Walk through the list of incomplete types again, trying once more to
28141 emit full debugging info for them. */
28142 retry_incomplete_types ();
28144 /* The point here is to flush out the limbo list so that it is empty
28145 and we don't need to stream it for LTO. */
28146 flush_limbo_die_list ();
28148 gen_scheduled_generic_parms_dies ();
28149 gen_remaining_tmpl_value_param_die_attribute ();
28151 /* Add DW_AT_linkage_name for all deferred DIEs. */
28152 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
28154 tree decl = node->created_for;
28155 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
28156 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
28157 ended up in deferred_asm_name before we knew it was
28158 constant and never written to disk. */
28159 && DECL_ASSEMBLER_NAME (decl))
28161 add_linkage_attr (node->die, decl);
28162 move_linkage_attr (node->die);
28165 deferred_asm_name = NULL;
28168 /* Reset all state within dwarf2out.c so that we can rerun the compiler
28169 within the same process. For use by toplev::finalize. */
28171 void
28172 dwarf2out_c_finalize (void)
28174 last_var_location_insn = NULL;
28175 cached_next_real_insn = NULL;
28176 used_rtx_array = NULL;
28177 incomplete_types = NULL;
28178 decl_scope_table = NULL;
28179 debug_info_section = NULL;
28180 debug_skeleton_info_section = NULL;
28181 debug_abbrev_section = NULL;
28182 debug_skeleton_abbrev_section = NULL;
28183 debug_aranges_section = NULL;
28184 debug_addr_section = NULL;
28185 debug_macinfo_section = NULL;
28186 debug_line_section = NULL;
28187 debug_skeleton_line_section = NULL;
28188 debug_loc_section = NULL;
28189 debug_pubnames_section = NULL;
28190 debug_pubtypes_section = NULL;
28191 debug_str_section = NULL;
28192 debug_str_dwo_section = NULL;
28193 debug_str_offsets_section = NULL;
28194 debug_ranges_section = NULL;
28195 debug_frame_section = NULL;
28196 fde_vec = NULL;
28197 debug_str_hash = NULL;
28198 skeleton_debug_str_hash = NULL;
28199 dw2_string_counter = 0;
28200 have_multiple_function_sections = false;
28201 text_section_used = false;
28202 cold_text_section_used = false;
28203 cold_text_section = NULL;
28204 current_unit_personality = NULL;
28206 next_die_offset = 0;
28207 single_comp_unit_die = NULL;
28208 comdat_type_list = NULL;
28209 limbo_die_list = NULL;
28210 file_table = NULL;
28211 decl_die_table = NULL;
28212 common_block_die_table = NULL;
28213 decl_loc_table = NULL;
28214 call_arg_locations = NULL;
28215 call_arg_loc_last = NULL;
28216 call_site_count = -1;
28217 tail_call_site_count = -1;
28218 cached_dw_loc_list_table = NULL;
28219 abbrev_die_table = NULL;
28220 abbrev_die_table_allocated = 0;
28221 abbrev_die_table_in_use = 0;
28222 delete dwarf_proc_stack_usage_map;
28223 dwarf_proc_stack_usage_map = NULL;
28224 line_info_label_num = 0;
28225 cur_line_info_table = NULL;
28226 text_section_line_info = NULL;
28227 cold_text_section_line_info = NULL;
28228 separate_line_info = NULL;
28229 info_section_emitted = false;
28230 pubname_table = NULL;
28231 pubtype_table = NULL;
28232 macinfo_table = NULL;
28233 ranges_table = NULL;
28234 ranges_table_allocated = 0;
28235 ranges_table_in_use = 0;
28236 ranges_by_label = 0;
28237 ranges_by_label_allocated = 0;
28238 ranges_by_label_in_use = 0;
28239 have_location_lists = false;
28240 loclabel_num = 0;
28241 poc_label_num = 0;
28242 last_emitted_file = NULL;
28243 label_num = 0;
28244 tmpl_value_parm_die_table = NULL;
28245 generic_type_instances = NULL;
28246 frame_pointer_fb_offset = 0;
28247 frame_pointer_fb_offset_valid = false;
28248 base_types.release ();
28249 XDELETEVEC (producer_string);
28250 producer_string = NULL;
28253 #include "gt-dwarf2out.h"