Implement C _FloatN, _FloatNx types.
[official-gcc.git] / gcc / dwarf2out.c
blob98f08b7f65ba83a3ad34ab7e39cd0e7989b97f8e
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 return lookup_type_die (type_main_variant (type));
11479 else
11480 /* Vectors have the debugging information in the type,
11481 not the main variant. */
11482 return lookup_type_die (type);
11485 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
11486 don't output a DW_TAG_typedef, since there isn't one in the
11487 user's program; just attach a DW_AT_name to the type.
11488 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11489 if the base type already has the same name. */
11490 if (name
11491 && ((TREE_CODE (name) != TYPE_DECL
11492 && (qualified_type == TYPE_MAIN_VARIANT (type)
11493 || (cv_quals == TYPE_UNQUALIFIED)))
11494 || (TREE_CODE (name) == TYPE_DECL
11495 && TREE_TYPE (name) == qualified_type
11496 && DECL_NAME (name))))
11498 if (TREE_CODE (name) == TYPE_DECL)
11499 /* Could just call add_name_and_src_coords_attributes here,
11500 but since this is a builtin type it doesn't have any
11501 useful source coordinates anyway. */
11502 name = DECL_NAME (name);
11503 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11505 /* This probably indicates a bug. */
11506 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11508 name = TYPE_IDENTIFIER (type);
11509 add_name_attribute (mod_type_die,
11510 name ? IDENTIFIER_POINTER (name) : "__unknown__");
11513 if (qualified_type)
11514 equate_type_number_to_die (qualified_type, mod_type_die);
11516 if (item_type)
11517 /* We must do this after the equate_type_number_to_die call, in case
11518 this is a recursive type. This ensures that the modified_type_die
11519 recursion will terminate even if the type is recursive. Recursive
11520 types are possible in Ada. */
11521 sub_die = modified_type_die (item_type,
11522 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11523 reverse,
11524 context_die);
11526 if (sub_die != NULL)
11527 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11529 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11530 if (TYPE_ARTIFICIAL (type))
11531 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11533 return mod_type_die;
11536 /* Generate DIEs for the generic parameters of T.
11537 T must be either a generic type or a generic function.
11538 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11540 static void
11541 gen_generic_params_dies (tree t)
11543 tree parms, args;
11544 int parms_num, i;
11545 dw_die_ref die = NULL;
11546 int non_default;
11548 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11549 return;
11551 if (TYPE_P (t))
11552 die = lookup_type_die (t);
11553 else if (DECL_P (t))
11554 die = lookup_decl_die (t);
11556 gcc_assert (die);
11558 parms = lang_hooks.get_innermost_generic_parms (t);
11559 if (!parms)
11560 /* T has no generic parameter. It means T is neither a generic type
11561 or function. End of story. */
11562 return;
11564 parms_num = TREE_VEC_LENGTH (parms);
11565 args = lang_hooks.get_innermost_generic_args (t);
11566 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11567 non_default = int_cst_value (TREE_CHAIN (args));
11568 else
11569 non_default = TREE_VEC_LENGTH (args);
11570 for (i = 0; i < parms_num; i++)
11572 tree parm, arg, arg_pack_elems;
11573 dw_die_ref parm_die;
11575 parm = TREE_VEC_ELT (parms, i);
11576 arg = TREE_VEC_ELT (args, i);
11577 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11578 gcc_assert (parm && TREE_VALUE (parm) && arg);
11580 if (parm && TREE_VALUE (parm) && arg)
11582 /* If PARM represents a template parameter pack,
11583 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11584 by DW_TAG_template_*_parameter DIEs for the argument
11585 pack elements of ARG. Note that ARG would then be
11586 an argument pack. */
11587 if (arg_pack_elems)
11588 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11589 arg_pack_elems,
11590 die);
11591 else
11592 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11593 true /* emit name */, die);
11594 if (i >= non_default)
11595 add_AT_flag (parm_die, DW_AT_default_value, 1);
11600 /* Create and return a DIE for PARM which should be
11601 the representation of a generic type parameter.
11602 For instance, in the C++ front end, PARM would be a template parameter.
11603 ARG is the argument to PARM.
11604 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11605 name of the PARM.
11606 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11607 as a child node. */
11609 static dw_die_ref
11610 generic_parameter_die (tree parm, tree arg,
11611 bool emit_name_p,
11612 dw_die_ref parent_die)
11614 dw_die_ref tmpl_die = NULL;
11615 const char *name = NULL;
11617 if (!parm || !DECL_NAME (parm) || !arg)
11618 return NULL;
11620 /* We support non-type generic parameters and arguments,
11621 type generic parameters and arguments, as well as
11622 generic generic parameters (a.k.a. template template parameters in C++)
11623 and arguments. */
11624 if (TREE_CODE (parm) == PARM_DECL)
11625 /* PARM is a nontype generic parameter */
11626 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11627 else if (TREE_CODE (parm) == TYPE_DECL)
11628 /* PARM is a type generic parameter. */
11629 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11630 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11631 /* PARM is a generic generic parameter.
11632 Its DIE is a GNU extension. It shall have a
11633 DW_AT_name attribute to represent the name of the template template
11634 parameter, and a DW_AT_GNU_template_name attribute to represent the
11635 name of the template template argument. */
11636 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11637 parent_die, parm);
11638 else
11639 gcc_unreachable ();
11641 if (tmpl_die)
11643 tree tmpl_type;
11645 /* If PARM is a generic parameter pack, it means we are
11646 emitting debug info for a template argument pack element.
11647 In other terms, ARG is a template argument pack element.
11648 In that case, we don't emit any DW_AT_name attribute for
11649 the die. */
11650 if (emit_name_p)
11652 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11653 gcc_assert (name);
11654 add_AT_string (tmpl_die, DW_AT_name, name);
11657 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11659 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11660 TMPL_DIE should have a child DW_AT_type attribute that is set
11661 to the type of the argument to PARM, which is ARG.
11662 If PARM is a type generic parameter, TMPL_DIE should have a
11663 child DW_AT_type that is set to ARG. */
11664 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11665 add_type_attribute (tmpl_die, tmpl_type,
11666 (TREE_THIS_VOLATILE (tmpl_type)
11667 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11668 false, parent_die);
11670 else
11672 /* So TMPL_DIE is a DIE representing a
11673 a generic generic template parameter, a.k.a template template
11674 parameter in C++ and arg is a template. */
11676 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11677 to the name of the argument. */
11678 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11679 if (name)
11680 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11683 if (TREE_CODE (parm) == PARM_DECL)
11684 /* So PARM is a non-type generic parameter.
11685 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11686 attribute of TMPL_DIE which value represents the value
11687 of ARG.
11688 We must be careful here:
11689 The value of ARG might reference some function decls.
11690 We might currently be emitting debug info for a generic
11691 type and types are emitted before function decls, we don't
11692 know if the function decls referenced by ARG will actually be
11693 emitted after cgraph computations.
11694 So must defer the generation of the DW_AT_const_value to
11695 after cgraph is ready. */
11696 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11699 return tmpl_die;
11702 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11703 PARM_PACK must be a template parameter pack. The returned DIE
11704 will be child DIE of PARENT_DIE. */
11706 static dw_die_ref
11707 template_parameter_pack_die (tree parm_pack,
11708 tree parm_pack_args,
11709 dw_die_ref parent_die)
11711 dw_die_ref die;
11712 int j;
11714 gcc_assert (parent_die && parm_pack);
11716 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11717 add_name_and_src_coords_attributes (die, parm_pack);
11718 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11719 generic_parameter_die (parm_pack,
11720 TREE_VEC_ELT (parm_pack_args, j),
11721 false /* Don't emit DW_AT_name */,
11722 die);
11723 return die;
11726 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11727 an enumerated type. */
11729 static inline int
11730 type_is_enum (const_tree type)
11732 return TREE_CODE (type) == ENUMERAL_TYPE;
11735 /* Return the DBX register number described by a given RTL node. */
11737 static unsigned int
11738 dbx_reg_number (const_rtx rtl)
11740 unsigned regno = REGNO (rtl);
11742 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11744 #ifdef LEAF_REG_REMAP
11745 if (crtl->uses_only_leaf_regs)
11747 int leaf_reg = LEAF_REG_REMAP (regno);
11748 if (leaf_reg != -1)
11749 regno = (unsigned) leaf_reg;
11751 #endif
11753 regno = DBX_REGISTER_NUMBER (regno);
11754 gcc_assert (regno != INVALID_REGNUM);
11755 return regno;
11758 /* Optionally add a DW_OP_piece term to a location description expression.
11759 DW_OP_piece is only added if the location description expression already
11760 doesn't end with DW_OP_piece. */
11762 static void
11763 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11765 dw_loc_descr_ref loc;
11767 if (*list_head != NULL)
11769 /* Find the end of the chain. */
11770 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11773 if (loc->dw_loc_opc != DW_OP_piece)
11774 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11778 /* Return a location descriptor that designates a machine register or
11779 zero if there is none. */
11781 static dw_loc_descr_ref
11782 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11784 rtx regs;
11786 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11787 return 0;
11789 /* We only use "frame base" when we're sure we're talking about the
11790 post-prologue local stack frame. We do this by *not* running
11791 register elimination until this point, and recognizing the special
11792 argument pointer and soft frame pointer rtx's.
11793 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11794 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11795 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11797 dw_loc_descr_ref result = NULL;
11799 if (dwarf_version >= 4 || !dwarf_strict)
11801 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11802 initialized);
11803 if (result)
11804 add_loc_descr (&result,
11805 new_loc_descr (DW_OP_stack_value, 0, 0));
11807 return result;
11810 regs = targetm.dwarf_register_span (rtl);
11812 if (REG_NREGS (rtl) > 1 || regs)
11813 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11814 else
11816 unsigned int dbx_regnum = dbx_reg_number (rtl);
11817 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11818 return 0;
11819 return one_reg_loc_descriptor (dbx_regnum, initialized);
11823 /* Return a location descriptor that designates a machine register for
11824 a given hard register number. */
11826 static dw_loc_descr_ref
11827 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11829 dw_loc_descr_ref reg_loc_descr;
11831 if (regno <= 31)
11832 reg_loc_descr
11833 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11834 else
11835 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11837 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11838 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11840 return reg_loc_descr;
11843 /* Given an RTL of a register, return a location descriptor that
11844 designates a value that spans more than one register. */
11846 static dw_loc_descr_ref
11847 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11848 enum var_init_status initialized)
11850 int size, i;
11851 dw_loc_descr_ref loc_result = NULL;
11853 /* Simple, contiguous registers. */
11854 if (regs == NULL_RTX)
11856 unsigned reg = REGNO (rtl);
11857 int nregs;
11859 #ifdef LEAF_REG_REMAP
11860 if (crtl->uses_only_leaf_regs)
11862 int leaf_reg = LEAF_REG_REMAP (reg);
11863 if (leaf_reg != -1)
11864 reg = (unsigned) leaf_reg;
11866 #endif
11868 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11869 nregs = REG_NREGS (rtl);
11871 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11873 loc_result = NULL;
11874 while (nregs--)
11876 dw_loc_descr_ref t;
11878 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11879 VAR_INIT_STATUS_INITIALIZED);
11880 add_loc_descr (&loc_result, t);
11881 add_loc_descr_op_piece (&loc_result, size);
11882 ++reg;
11884 return loc_result;
11887 /* Now onto stupid register sets in non contiguous locations. */
11889 gcc_assert (GET_CODE (regs) == PARALLEL);
11891 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11892 loc_result = NULL;
11894 for (i = 0; i < XVECLEN (regs, 0); ++i)
11896 dw_loc_descr_ref t;
11898 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11899 VAR_INIT_STATUS_INITIALIZED);
11900 add_loc_descr (&loc_result, t);
11901 add_loc_descr_op_piece (&loc_result, size);
11904 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11905 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11906 return loc_result;
11909 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11911 /* Return a location descriptor that designates a constant i,
11912 as a compound operation from constant (i >> shift), constant shift
11913 and DW_OP_shl. */
11915 static dw_loc_descr_ref
11916 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11918 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11919 add_loc_descr (&ret, int_loc_descriptor (shift));
11920 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11921 return ret;
11924 /* Return a location descriptor that designates a constant. */
11926 static dw_loc_descr_ref
11927 int_loc_descriptor (HOST_WIDE_INT i)
11929 enum dwarf_location_atom op;
11931 /* Pick the smallest representation of a constant, rather than just
11932 defaulting to the LEB encoding. */
11933 if (i >= 0)
11935 int clz = clz_hwi (i);
11936 int ctz = ctz_hwi (i);
11937 if (i <= 31)
11938 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11939 else if (i <= 0xff)
11940 op = DW_OP_const1u;
11941 else if (i <= 0xffff)
11942 op = DW_OP_const2u;
11943 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11944 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11945 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11946 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11947 while DW_OP_const4u is 5 bytes. */
11948 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11949 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11950 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11951 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11952 while DW_OP_const4u is 5 bytes. */
11953 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11954 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11955 op = DW_OP_const4u;
11956 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11957 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11958 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11959 while DW_OP_constu of constant >= 0x100000000 takes at least
11960 6 bytes. */
11961 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11962 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11963 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11964 >= HOST_BITS_PER_WIDE_INT)
11965 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11966 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11967 while DW_OP_constu takes in this case at least 6 bytes. */
11968 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11969 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11970 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11971 && size_of_uleb128 (i) > 6)
11972 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11973 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11974 else
11975 op = DW_OP_constu;
11977 else
11979 if (i >= -0x80)
11980 op = DW_OP_const1s;
11981 else if (i >= -0x8000)
11982 op = DW_OP_const2s;
11983 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11985 if (size_of_int_loc_descriptor (i) < 5)
11987 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11988 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11989 return ret;
11991 op = DW_OP_const4s;
11993 else
11995 if (size_of_int_loc_descriptor (i)
11996 < (unsigned long) 1 + size_of_sleb128 (i))
11998 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11999 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12000 return ret;
12002 op = DW_OP_consts;
12006 return new_loc_descr (op, i, 0);
12009 /* Likewise, for unsigned constants. */
12011 static dw_loc_descr_ref
12012 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12014 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12015 const unsigned HOST_WIDE_INT max_uint
12016 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12018 /* If possible, use the clever signed constants handling. */
12019 if (i <= max_int)
12020 return int_loc_descriptor ((HOST_WIDE_INT) i);
12022 /* Here, we are left with positive numbers that cannot be represented as
12023 HOST_WIDE_INT, i.e.:
12024 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12026 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12027 whereas may be better to output a negative integer: thanks to integer
12028 wrapping, we know that:
12029 x = x - 2 ** DWARF2_ADDR_SIZE
12030 = x - 2 * (max (HOST_WIDE_INT) + 1)
12031 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12032 small negative integers. Let's try that in cases it will clearly improve
12033 the encoding: there is no gain turning DW_OP_const4u into
12034 DW_OP_const4s. */
12035 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12036 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12037 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12039 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12041 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
12042 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
12043 const HOST_WIDE_INT second_shift
12044 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12046 /* So we finally have:
12047 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12048 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
12049 return int_loc_descriptor (second_shift);
12052 /* Last chance: fallback to a simple constant operation. */
12053 return new_loc_descr
12054 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12055 ? DW_OP_const4u
12056 : DW_OP_const8u,
12057 i, 0);
12060 /* Generate and return a location description that computes the unsigned
12061 comparison of the two stack top entries (a OP b where b is the top-most
12062 entry and a is the second one). The KIND of comparison can be LT_EXPR,
12063 LE_EXPR, GT_EXPR or GE_EXPR. */
12065 static dw_loc_descr_ref
12066 uint_comparison_loc_list (enum tree_code kind)
12068 enum dwarf_location_atom op, flip_op;
12069 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12071 switch (kind)
12073 case LT_EXPR:
12074 op = DW_OP_lt;
12075 break;
12076 case LE_EXPR:
12077 op = DW_OP_le;
12078 break;
12079 case GT_EXPR:
12080 op = DW_OP_gt;
12081 break;
12082 case GE_EXPR:
12083 op = DW_OP_ge;
12084 break;
12085 default:
12086 gcc_unreachable ();
12089 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12090 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12092 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
12093 possible to perform unsigned comparisons: we just have to distinguish
12094 three cases:
12096 1. when a and b have the same sign (as signed integers); then we should
12097 return: a OP(signed) b;
12099 2. when a is a negative signed integer while b is a positive one, then a
12100 is a greater unsigned integer than b; likewise when a and b's roles
12101 are flipped.
12103 So first, compare the sign of the two operands. */
12104 ret = new_loc_descr (DW_OP_over, 0, 0);
12105 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12106 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12107 /* If they have different signs (i.e. they have different sign bits), then
12108 the stack top value has now the sign bit set and thus it's smaller than
12109 zero. */
12110 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12111 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12112 add_loc_descr (&ret, bra_node);
12114 /* We are in case 1. At this point, we know both operands have the same
12115 sign, to it's safe to use the built-in signed comparison. */
12116 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12117 add_loc_descr (&ret, jmp_node);
12119 /* We are in case 2. Here, we know both operands do not have the same sign,
12120 so we have to flip the signed comparison. */
12121 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12122 tmp = new_loc_descr (flip_op, 0, 0);
12123 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12124 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12125 add_loc_descr (&ret, tmp);
12127 /* This dummy operation is necessary to make the two branches join. */
12128 tmp = new_loc_descr (DW_OP_nop, 0, 0);
12129 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12130 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12131 add_loc_descr (&ret, tmp);
12133 return ret;
12136 /* Likewise, but takes the location description lists (might be destructive on
12137 them). Return NULL if either is NULL or if concatenation fails. */
12139 static dw_loc_list_ref
12140 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12141 enum tree_code kind)
12143 if (left == NULL || right == NULL)
12144 return NULL;
12146 add_loc_list (&left, right);
12147 if (left == NULL)
12148 return NULL;
12150 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12151 return left;
12154 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12155 without actually allocating it. */
12157 static unsigned long
12158 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12160 return size_of_int_loc_descriptor (i >> shift)
12161 + size_of_int_loc_descriptor (shift)
12162 + 1;
12165 /* Return size_of_locs (int_loc_descriptor (i)) without
12166 actually allocating it. */
12168 static unsigned long
12169 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12171 unsigned long s;
12173 if (i >= 0)
12175 int clz, ctz;
12176 if (i <= 31)
12177 return 1;
12178 else if (i <= 0xff)
12179 return 2;
12180 else if (i <= 0xffff)
12181 return 3;
12182 clz = clz_hwi (i);
12183 ctz = ctz_hwi (i);
12184 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12185 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12186 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12187 - clz - 5);
12188 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12189 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12190 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12191 - clz - 8);
12192 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12193 return 5;
12194 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12195 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12196 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12197 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12198 - clz - 8);
12199 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12200 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12201 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12202 - clz - 16);
12203 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12204 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12205 && s > 6)
12206 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12207 - clz - 32);
12208 else
12209 return 1 + s;
12211 else
12213 if (i >= -0x80)
12214 return 2;
12215 else if (i >= -0x8000)
12216 return 3;
12217 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12219 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12221 s = size_of_int_loc_descriptor (-i) + 1;
12222 if (s < 5)
12223 return s;
12225 return 5;
12227 else
12229 unsigned long r = 1 + size_of_sleb128 (i);
12230 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12232 s = size_of_int_loc_descriptor (-i) + 1;
12233 if (s < r)
12234 return s;
12236 return r;
12241 /* Return loc description representing "address" of integer value.
12242 This can appear only as toplevel expression. */
12244 static dw_loc_descr_ref
12245 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12247 int litsize;
12248 dw_loc_descr_ref loc_result = NULL;
12250 if (!(dwarf_version >= 4 || !dwarf_strict))
12251 return NULL;
12253 litsize = size_of_int_loc_descriptor (i);
12254 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12255 is more compact. For DW_OP_stack_value we need:
12256 litsize + 1 (DW_OP_stack_value)
12257 and for DW_OP_implicit_value:
12258 1 (DW_OP_implicit_value) + 1 (length) + size. */
12259 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12261 loc_result = int_loc_descriptor (i);
12262 add_loc_descr (&loc_result,
12263 new_loc_descr (DW_OP_stack_value, 0, 0));
12264 return loc_result;
12267 loc_result = new_loc_descr (DW_OP_implicit_value,
12268 size, 0);
12269 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12270 loc_result->dw_loc_oprnd2.v.val_int = i;
12271 return loc_result;
12274 /* Return a location descriptor that designates a base+offset location. */
12276 static dw_loc_descr_ref
12277 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12278 enum var_init_status initialized)
12280 unsigned int regno;
12281 dw_loc_descr_ref result;
12282 dw_fde_ref fde = cfun->fde;
12284 /* We only use "frame base" when we're sure we're talking about the
12285 post-prologue local stack frame. We do this by *not* running
12286 register elimination until this point, and recognizing the special
12287 argument pointer and soft frame pointer rtx's. */
12288 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12290 rtx elim = (ira_use_lra_p
12291 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12292 : eliminate_regs (reg, VOIDmode, NULL_RTX));
12294 if (elim != reg)
12296 if (GET_CODE (elim) == PLUS)
12298 offset += INTVAL (XEXP (elim, 1));
12299 elim = XEXP (elim, 0);
12301 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12302 && (elim == hard_frame_pointer_rtx
12303 || elim == stack_pointer_rtx))
12304 || elim == (frame_pointer_needed
12305 ? hard_frame_pointer_rtx
12306 : stack_pointer_rtx));
12308 /* If drap register is used to align stack, use frame
12309 pointer + offset to access stack variables. If stack
12310 is aligned without drap, use stack pointer + offset to
12311 access stack variables. */
12312 if (crtl->stack_realign_tried
12313 && reg == frame_pointer_rtx)
12315 int base_reg
12316 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12317 ? HARD_FRAME_POINTER_REGNUM
12318 : REGNO (elim));
12319 return new_reg_loc_descr (base_reg, offset);
12322 gcc_assert (frame_pointer_fb_offset_valid);
12323 offset += frame_pointer_fb_offset;
12324 return new_loc_descr (DW_OP_fbreg, offset, 0);
12328 regno = REGNO (reg);
12329 #ifdef LEAF_REG_REMAP
12330 if (crtl->uses_only_leaf_regs)
12332 int leaf_reg = LEAF_REG_REMAP (regno);
12333 if (leaf_reg != -1)
12334 regno = (unsigned) leaf_reg;
12336 #endif
12337 regno = DWARF_FRAME_REGNUM (regno);
12339 if (!optimize && fde
12340 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12342 /* Use cfa+offset to represent the location of arguments passed
12343 on the stack when drap is used to align stack.
12344 Only do this when not optimizing, for optimized code var-tracking
12345 is supposed to track where the arguments live and the register
12346 used as vdrap or drap in some spot might be used for something
12347 else in other part of the routine. */
12348 return new_loc_descr (DW_OP_fbreg, offset, 0);
12351 if (regno <= 31)
12352 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12353 offset, 0);
12354 else
12355 result = new_loc_descr (DW_OP_bregx, regno, offset);
12357 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12358 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12360 return result;
12363 /* Return true if this RTL expression describes a base+offset calculation. */
12365 static inline int
12366 is_based_loc (const_rtx rtl)
12368 return (GET_CODE (rtl) == PLUS
12369 && ((REG_P (XEXP (rtl, 0))
12370 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12371 && CONST_INT_P (XEXP (rtl, 1)))));
12374 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12375 failed. */
12377 static dw_loc_descr_ref
12378 tls_mem_loc_descriptor (rtx mem)
12380 tree base;
12381 dw_loc_descr_ref loc_result;
12383 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12384 return NULL;
12386 base = get_base_address (MEM_EXPR (mem));
12387 if (base == NULL
12388 || TREE_CODE (base) != VAR_DECL
12389 || !DECL_THREAD_LOCAL_P (base))
12390 return NULL;
12392 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12393 if (loc_result == NULL)
12394 return NULL;
12396 if (MEM_OFFSET (mem))
12397 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12399 return loc_result;
12402 /* Output debug info about reason why we failed to expand expression as dwarf
12403 expression. */
12405 static void
12406 expansion_failed (tree expr, rtx rtl, char const *reason)
12408 if (dump_file && (dump_flags & TDF_DETAILS))
12410 fprintf (dump_file, "Failed to expand as dwarf: ");
12411 if (expr)
12412 print_generic_expr (dump_file, expr, dump_flags);
12413 if (rtl)
12415 fprintf (dump_file, "\n");
12416 print_rtl (dump_file, rtl);
12418 fprintf (dump_file, "\nReason: %s\n", reason);
12422 /* Helper function for const_ok_for_output. */
12424 static bool
12425 const_ok_for_output_1 (rtx rtl)
12427 if (GET_CODE (rtl) == UNSPEC)
12429 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12430 we can't express it in the debug info. */
12431 /* Don't complain about TLS UNSPECs, those are just too hard to
12432 delegitimize. Note this could be a non-decl SYMBOL_REF such as
12433 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12434 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
12435 if (flag_checking
12436 && (XVECLEN (rtl, 0) == 0
12437 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12438 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12439 inform (current_function_decl
12440 ? DECL_SOURCE_LOCATION (current_function_decl)
12441 : UNKNOWN_LOCATION,
12442 #if NUM_UNSPEC_VALUES > 0
12443 "non-delegitimized UNSPEC %s (%d) found in variable location",
12444 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12445 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12446 XINT (rtl, 1));
12447 #else
12448 "non-delegitimized UNSPEC %d found in variable location",
12449 XINT (rtl, 1));
12450 #endif
12451 expansion_failed (NULL_TREE, rtl,
12452 "UNSPEC hasn't been delegitimized.\n");
12453 return false;
12456 if (targetm.const_not_ok_for_debug_p (rtl))
12458 expansion_failed (NULL_TREE, rtl,
12459 "Expression rejected for debug by the backend.\n");
12460 return false;
12463 /* FIXME: Refer to PR60655. It is possible for simplification
12464 of rtl expressions in var tracking to produce such expressions.
12465 We should really identify / validate expressions
12466 enclosed in CONST that can be handled by assemblers on various
12467 targets and only handle legitimate cases here. */
12468 if (GET_CODE (rtl) != SYMBOL_REF)
12470 if (GET_CODE (rtl) == NOT)
12471 return false;
12472 return true;
12475 if (CONSTANT_POOL_ADDRESS_P (rtl))
12477 bool marked;
12478 get_pool_constant_mark (rtl, &marked);
12479 /* If all references to this pool constant were optimized away,
12480 it was not output and thus we can't represent it. */
12481 if (!marked)
12483 expansion_failed (NULL_TREE, rtl,
12484 "Constant was removed from constant pool.\n");
12485 return false;
12489 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12490 return false;
12492 /* Avoid references to external symbols in debug info, on several targets
12493 the linker might even refuse to link when linking a shared library,
12494 and in many other cases the relocations for .debug_info/.debug_loc are
12495 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
12496 to be defined within the same shared library or executable are fine. */
12497 if (SYMBOL_REF_EXTERNAL_P (rtl))
12499 tree decl = SYMBOL_REF_DECL (rtl);
12501 if (decl == NULL || !targetm.binds_local_p (decl))
12503 expansion_failed (NULL_TREE, rtl,
12504 "Symbol not defined in current TU.\n");
12505 return false;
12509 return true;
12512 /* Return true if constant RTL can be emitted in DW_OP_addr or
12513 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
12514 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
12516 static bool
12517 const_ok_for_output (rtx rtl)
12519 if (GET_CODE (rtl) == SYMBOL_REF)
12520 return const_ok_for_output_1 (rtl);
12522 if (GET_CODE (rtl) == CONST)
12524 subrtx_var_iterator::array_type array;
12525 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12526 if (!const_ok_for_output_1 (*iter))
12527 return false;
12528 return true;
12531 return true;
12534 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12535 if possible, NULL otherwise. */
12537 static dw_die_ref
12538 base_type_for_mode (machine_mode mode, bool unsignedp)
12540 dw_die_ref type_die;
12541 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12543 if (type == NULL)
12544 return NULL;
12545 switch (TREE_CODE (type))
12547 case INTEGER_TYPE:
12548 case REAL_TYPE:
12549 break;
12550 default:
12551 return NULL;
12553 type_die = lookup_type_die (type);
12554 if (!type_die)
12555 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12556 comp_unit_die ());
12557 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12558 return NULL;
12559 return type_die;
12562 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12563 type matching MODE, or, if MODE is narrower than or as wide as
12564 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
12565 possible. */
12567 static dw_loc_descr_ref
12568 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12570 machine_mode outer_mode = mode;
12571 dw_die_ref type_die;
12572 dw_loc_descr_ref cvt;
12574 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12576 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12577 return op;
12579 type_die = base_type_for_mode (outer_mode, 1);
12580 if (type_die == NULL)
12581 return NULL;
12582 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12583 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12584 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12585 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12586 add_loc_descr (&op, cvt);
12587 return op;
12590 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
12592 static dw_loc_descr_ref
12593 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12594 dw_loc_descr_ref op1)
12596 dw_loc_descr_ref ret = op0;
12597 add_loc_descr (&ret, op1);
12598 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12599 if (STORE_FLAG_VALUE != 1)
12601 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12602 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12604 return ret;
12607 /* Return location descriptor for signed comparison OP RTL. */
12609 static dw_loc_descr_ref
12610 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12611 machine_mode mem_mode)
12613 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12614 dw_loc_descr_ref op0, op1;
12615 int shift;
12617 if (op_mode == VOIDmode)
12618 op_mode = GET_MODE (XEXP (rtl, 1));
12619 if (op_mode == VOIDmode)
12620 return NULL;
12622 if (dwarf_strict
12623 && (!SCALAR_INT_MODE_P (op_mode)
12624 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12625 return NULL;
12627 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12628 VAR_INIT_STATUS_INITIALIZED);
12629 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12630 VAR_INIT_STATUS_INITIALIZED);
12632 if (op0 == NULL || op1 == NULL)
12633 return NULL;
12635 if (!SCALAR_INT_MODE_P (op_mode)
12636 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12637 return compare_loc_descriptor (op, op0, op1);
12639 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12641 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12642 dw_loc_descr_ref cvt;
12644 if (type_die == NULL)
12645 return NULL;
12646 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12647 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12648 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12649 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12650 add_loc_descr (&op0, cvt);
12651 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12652 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12653 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12654 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12655 add_loc_descr (&op1, cvt);
12656 return compare_loc_descriptor (op, op0, op1);
12659 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12660 /* For eq/ne, if the operands are known to be zero-extended,
12661 there is no need to do the fancy shifting up. */
12662 if (op == DW_OP_eq || op == DW_OP_ne)
12664 dw_loc_descr_ref last0, last1;
12665 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12667 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12669 /* deref_size zero extends, and for constants we can check
12670 whether they are zero extended or not. */
12671 if (((last0->dw_loc_opc == DW_OP_deref_size
12672 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12673 || (CONST_INT_P (XEXP (rtl, 0))
12674 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12675 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12676 && ((last1->dw_loc_opc == DW_OP_deref_size
12677 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12678 || (CONST_INT_P (XEXP (rtl, 1))
12679 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12680 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12681 return compare_loc_descriptor (op, op0, op1);
12683 /* EQ/NE comparison against constant in narrower type than
12684 DWARF2_ADDR_SIZE can be performed either as
12685 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12686 DW_OP_{eq,ne}
12688 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12689 DW_OP_{eq,ne}. Pick whatever is shorter. */
12690 if (CONST_INT_P (XEXP (rtl, 1))
12691 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12692 && (size_of_int_loc_descriptor (shift) + 1
12693 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12694 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12695 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12696 & GET_MODE_MASK (op_mode))))
12698 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12699 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12700 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12701 & GET_MODE_MASK (op_mode));
12702 return compare_loc_descriptor (op, op0, op1);
12705 add_loc_descr (&op0, int_loc_descriptor (shift));
12706 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12707 if (CONST_INT_P (XEXP (rtl, 1)))
12708 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12709 else
12711 add_loc_descr (&op1, int_loc_descriptor (shift));
12712 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12714 return compare_loc_descriptor (op, op0, op1);
12717 /* Return location descriptor for unsigned comparison OP RTL. */
12719 static dw_loc_descr_ref
12720 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12721 machine_mode mem_mode)
12723 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12724 dw_loc_descr_ref op0, op1;
12726 if (op_mode == VOIDmode)
12727 op_mode = GET_MODE (XEXP (rtl, 1));
12728 if (op_mode == VOIDmode)
12729 return NULL;
12730 if (!SCALAR_INT_MODE_P (op_mode))
12731 return NULL;
12733 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12734 return NULL;
12736 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12737 VAR_INIT_STATUS_INITIALIZED);
12738 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12739 VAR_INIT_STATUS_INITIALIZED);
12741 if (op0 == NULL || op1 == NULL)
12742 return NULL;
12744 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12746 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12747 dw_loc_descr_ref last0, last1;
12748 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12750 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12752 if (CONST_INT_P (XEXP (rtl, 0)))
12753 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12754 /* deref_size zero extends, so no need to mask it again. */
12755 else if (last0->dw_loc_opc != DW_OP_deref_size
12756 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12758 add_loc_descr (&op0, int_loc_descriptor (mask));
12759 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12761 if (CONST_INT_P (XEXP (rtl, 1)))
12762 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12763 /* deref_size zero extends, so no need to mask it again. */
12764 else if (last1->dw_loc_opc != DW_OP_deref_size
12765 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12767 add_loc_descr (&op1, int_loc_descriptor (mask));
12768 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12771 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12773 HOST_WIDE_INT bias = 1;
12774 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12775 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12776 if (CONST_INT_P (XEXP (rtl, 1)))
12777 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12778 + INTVAL (XEXP (rtl, 1)));
12779 else
12780 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12781 bias, 0));
12783 return compare_loc_descriptor (op, op0, op1);
12786 /* Return location descriptor for {U,S}{MIN,MAX}. */
12788 static dw_loc_descr_ref
12789 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12790 machine_mode mem_mode)
12792 enum dwarf_location_atom op;
12793 dw_loc_descr_ref op0, op1, ret;
12794 dw_loc_descr_ref bra_node, drop_node;
12796 if (dwarf_strict
12797 && (!SCALAR_INT_MODE_P (mode)
12798 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12799 return NULL;
12801 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12802 VAR_INIT_STATUS_INITIALIZED);
12803 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12804 VAR_INIT_STATUS_INITIALIZED);
12806 if (op0 == NULL || op1 == NULL)
12807 return NULL;
12809 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12810 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12811 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12812 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12814 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12816 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12817 add_loc_descr (&op0, int_loc_descriptor (mask));
12818 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12819 add_loc_descr (&op1, int_loc_descriptor (mask));
12820 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12822 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12824 HOST_WIDE_INT bias = 1;
12825 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12826 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12827 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12830 else if (!SCALAR_INT_MODE_P (mode)
12831 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12833 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12834 add_loc_descr (&op0, int_loc_descriptor (shift));
12835 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12836 add_loc_descr (&op1, int_loc_descriptor (shift));
12837 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12839 else if (SCALAR_INT_MODE_P (mode)
12840 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12842 dw_die_ref type_die = base_type_for_mode (mode, 0);
12843 dw_loc_descr_ref cvt;
12844 if (type_die == NULL)
12845 return NULL;
12846 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12847 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12848 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12849 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12850 add_loc_descr (&op0, cvt);
12851 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12852 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12853 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12854 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12855 add_loc_descr (&op1, cvt);
12858 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12859 op = DW_OP_lt;
12860 else
12861 op = DW_OP_gt;
12862 ret = op0;
12863 add_loc_descr (&ret, op1);
12864 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12865 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12866 add_loc_descr (&ret, bra_node);
12867 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12868 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12869 add_loc_descr (&ret, drop_node);
12870 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12871 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12872 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12873 && SCALAR_INT_MODE_P (mode)
12874 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12875 ret = convert_descriptor_to_mode (mode, ret);
12876 return ret;
12879 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12880 but after converting arguments to type_die, afterwards
12881 convert back to unsigned. */
12883 static dw_loc_descr_ref
12884 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12885 machine_mode mode, machine_mode mem_mode)
12887 dw_loc_descr_ref cvt, op0, op1;
12889 if (type_die == NULL)
12890 return NULL;
12891 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12892 VAR_INIT_STATUS_INITIALIZED);
12893 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12894 VAR_INIT_STATUS_INITIALIZED);
12895 if (op0 == NULL || op1 == NULL)
12896 return NULL;
12897 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12898 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12899 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12900 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12901 add_loc_descr (&op0, cvt);
12902 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12903 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12904 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12905 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12906 add_loc_descr (&op1, cvt);
12907 add_loc_descr (&op0, op1);
12908 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12909 return convert_descriptor_to_mode (mode, op0);
12912 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12913 const0 is DW_OP_lit0 or corresponding typed constant,
12914 const1 is DW_OP_lit1 or corresponding typed constant
12915 and constMSB is constant with just the MSB bit set
12916 for the mode):
12917 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12918 L1: const0 DW_OP_swap
12919 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12920 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12921 L3: DW_OP_drop
12922 L4: DW_OP_nop
12924 CTZ is similar:
12925 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12926 L1: const0 DW_OP_swap
12927 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12928 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12929 L3: DW_OP_drop
12930 L4: DW_OP_nop
12932 FFS is similar:
12933 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12934 L1: const1 DW_OP_swap
12935 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12936 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12937 L3: DW_OP_drop
12938 L4: DW_OP_nop */
12940 static dw_loc_descr_ref
12941 clz_loc_descriptor (rtx rtl, machine_mode mode,
12942 machine_mode mem_mode)
12944 dw_loc_descr_ref op0, ret, tmp;
12945 HOST_WIDE_INT valv;
12946 dw_loc_descr_ref l1jump, l1label;
12947 dw_loc_descr_ref l2jump, l2label;
12948 dw_loc_descr_ref l3jump, l3label;
12949 dw_loc_descr_ref l4jump, l4label;
12950 rtx msb;
12952 if (!SCALAR_INT_MODE_P (mode)
12953 || GET_MODE (XEXP (rtl, 0)) != mode)
12954 return NULL;
12956 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12957 VAR_INIT_STATUS_INITIALIZED);
12958 if (op0 == NULL)
12959 return NULL;
12960 ret = op0;
12961 if (GET_CODE (rtl) == CLZ)
12963 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12964 valv = GET_MODE_BITSIZE (mode);
12966 else if (GET_CODE (rtl) == FFS)
12967 valv = 0;
12968 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12969 valv = GET_MODE_BITSIZE (mode);
12970 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12971 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12972 add_loc_descr (&ret, l1jump);
12973 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12974 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12975 VAR_INIT_STATUS_INITIALIZED);
12976 if (tmp == NULL)
12977 return NULL;
12978 add_loc_descr (&ret, tmp);
12979 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12980 add_loc_descr (&ret, l4jump);
12981 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12982 ? const1_rtx : const0_rtx,
12983 mode, mem_mode,
12984 VAR_INIT_STATUS_INITIALIZED);
12985 if (l1label == NULL)
12986 return NULL;
12987 add_loc_descr (&ret, l1label);
12988 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12989 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12990 add_loc_descr (&ret, l2label);
12991 if (GET_CODE (rtl) != CLZ)
12992 msb = const1_rtx;
12993 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12994 msb = GEN_INT (HOST_WIDE_INT_1U
12995 << (GET_MODE_BITSIZE (mode) - 1));
12996 else
12997 msb = immed_wide_int_const
12998 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12999 GET_MODE_PRECISION (mode)), mode);
13000 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
13001 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13002 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
13003 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
13004 else
13005 tmp = mem_loc_descriptor (msb, mode, mem_mode,
13006 VAR_INIT_STATUS_INITIALIZED);
13007 if (tmp == NULL)
13008 return NULL;
13009 add_loc_descr (&ret, tmp);
13010 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13011 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13012 add_loc_descr (&ret, l3jump);
13013 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13014 VAR_INIT_STATUS_INITIALIZED);
13015 if (tmp == NULL)
13016 return NULL;
13017 add_loc_descr (&ret, tmp);
13018 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13019 ? DW_OP_shl : DW_OP_shr, 0, 0));
13020 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13021 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13022 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13023 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13024 add_loc_descr (&ret, l2jump);
13025 l3label = new_loc_descr (DW_OP_drop, 0, 0);
13026 add_loc_descr (&ret, l3label);
13027 l4label = new_loc_descr (DW_OP_nop, 0, 0);
13028 add_loc_descr (&ret, l4label);
13029 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13030 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13031 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13032 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13033 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13034 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13035 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13036 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13037 return ret;
13040 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13041 const1 is DW_OP_lit1 or corresponding typed constant):
13042 const0 DW_OP_swap
13043 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13044 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13045 L2: DW_OP_drop
13047 PARITY is similar:
13048 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13049 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13050 L2: DW_OP_drop */
13052 static dw_loc_descr_ref
13053 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13054 machine_mode mem_mode)
13056 dw_loc_descr_ref op0, ret, tmp;
13057 dw_loc_descr_ref l1jump, l1label;
13058 dw_loc_descr_ref l2jump, l2label;
13060 if (!SCALAR_INT_MODE_P (mode)
13061 || GET_MODE (XEXP (rtl, 0)) != mode)
13062 return NULL;
13064 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13065 VAR_INIT_STATUS_INITIALIZED);
13066 if (op0 == NULL)
13067 return NULL;
13068 ret = op0;
13069 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13070 VAR_INIT_STATUS_INITIALIZED);
13071 if (tmp == NULL)
13072 return NULL;
13073 add_loc_descr (&ret, tmp);
13074 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13075 l1label = new_loc_descr (DW_OP_dup, 0, 0);
13076 add_loc_descr (&ret, l1label);
13077 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13078 add_loc_descr (&ret, l2jump);
13079 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13080 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13081 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13082 VAR_INIT_STATUS_INITIALIZED);
13083 if (tmp == NULL)
13084 return NULL;
13085 add_loc_descr (&ret, tmp);
13086 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13087 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13088 ? DW_OP_plus : DW_OP_xor, 0, 0));
13089 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13090 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13091 VAR_INIT_STATUS_INITIALIZED);
13092 add_loc_descr (&ret, tmp);
13093 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13094 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13095 add_loc_descr (&ret, l1jump);
13096 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13097 add_loc_descr (&ret, l2label);
13098 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13099 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13100 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13101 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13102 return ret;
13105 /* BSWAP (constS is initial shift count, either 56 or 24):
13106 constS const0
13107 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13108 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13109 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13110 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13111 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
13113 static dw_loc_descr_ref
13114 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13115 machine_mode mem_mode)
13117 dw_loc_descr_ref op0, ret, tmp;
13118 dw_loc_descr_ref l1jump, l1label;
13119 dw_loc_descr_ref l2jump, l2label;
13121 if (!SCALAR_INT_MODE_P (mode)
13122 || BITS_PER_UNIT != 8
13123 || (GET_MODE_BITSIZE (mode) != 32
13124 && GET_MODE_BITSIZE (mode) != 64))
13125 return NULL;
13127 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13128 VAR_INIT_STATUS_INITIALIZED);
13129 if (op0 == NULL)
13130 return NULL;
13132 ret = op0;
13133 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13134 mode, mem_mode,
13135 VAR_INIT_STATUS_INITIALIZED);
13136 if (tmp == NULL)
13137 return NULL;
13138 add_loc_descr (&ret, tmp);
13139 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13140 VAR_INIT_STATUS_INITIALIZED);
13141 if (tmp == NULL)
13142 return NULL;
13143 add_loc_descr (&ret, tmp);
13144 l1label = new_loc_descr (DW_OP_pick, 2, 0);
13145 add_loc_descr (&ret, l1label);
13146 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13147 mode, mem_mode,
13148 VAR_INIT_STATUS_INITIALIZED);
13149 add_loc_descr (&ret, tmp);
13150 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13151 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13152 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13153 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13154 VAR_INIT_STATUS_INITIALIZED);
13155 if (tmp == NULL)
13156 return NULL;
13157 add_loc_descr (&ret, tmp);
13158 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13159 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13160 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13161 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13162 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13163 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13164 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13165 VAR_INIT_STATUS_INITIALIZED);
13166 add_loc_descr (&ret, tmp);
13167 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13168 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13169 add_loc_descr (&ret, l2jump);
13170 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13171 VAR_INIT_STATUS_INITIALIZED);
13172 add_loc_descr (&ret, tmp);
13173 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13174 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13175 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13176 add_loc_descr (&ret, l1jump);
13177 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13178 add_loc_descr (&ret, l2label);
13179 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13180 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13181 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13182 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13183 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13184 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13185 return ret;
13188 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13189 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13190 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13191 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13193 ROTATERT is similar:
13194 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13195 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13196 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
13198 static dw_loc_descr_ref
13199 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13200 machine_mode mem_mode)
13202 rtx rtlop1 = XEXP (rtl, 1);
13203 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13204 int i;
13206 if (!SCALAR_INT_MODE_P (mode))
13207 return NULL;
13209 if (GET_MODE (rtlop1) != VOIDmode
13210 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13211 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13212 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13213 VAR_INIT_STATUS_INITIALIZED);
13214 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13215 VAR_INIT_STATUS_INITIALIZED);
13216 if (op0 == NULL || op1 == NULL)
13217 return NULL;
13218 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13219 for (i = 0; i < 2; i++)
13221 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13222 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13223 mode, mem_mode,
13224 VAR_INIT_STATUS_INITIALIZED);
13225 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13226 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13227 ? DW_OP_const4u
13228 : HOST_BITS_PER_WIDE_INT == 64
13229 ? DW_OP_const8u : DW_OP_constu,
13230 GET_MODE_MASK (mode), 0);
13231 else
13232 mask[i] = NULL;
13233 if (mask[i] == NULL)
13234 return NULL;
13235 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13237 ret = op0;
13238 add_loc_descr (&ret, op1);
13239 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13240 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13241 if (GET_CODE (rtl) == ROTATERT)
13243 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13244 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13245 GET_MODE_BITSIZE (mode), 0));
13247 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13248 if (mask[0] != NULL)
13249 add_loc_descr (&ret, mask[0]);
13250 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13251 if (mask[1] != NULL)
13253 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13254 add_loc_descr (&ret, mask[1]);
13255 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13257 if (GET_CODE (rtl) == ROTATE)
13259 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13260 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13261 GET_MODE_BITSIZE (mode), 0));
13263 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13264 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13265 return ret;
13268 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
13269 for DEBUG_PARAMETER_REF RTL. */
13271 static dw_loc_descr_ref
13272 parameter_ref_descriptor (rtx rtl)
13274 dw_loc_descr_ref ret;
13275 dw_die_ref ref;
13277 if (dwarf_strict)
13278 return NULL;
13279 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13280 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13281 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13282 if (ref)
13284 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13285 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13286 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13288 else
13290 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13291 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13293 return ret;
13296 /* The following routine converts the RTL for a variable or parameter
13297 (resident in memory) into an equivalent Dwarf representation of a
13298 mechanism for getting the address of that same variable onto the top of a
13299 hypothetical "address evaluation" stack.
13301 When creating memory location descriptors, we are effectively transforming
13302 the RTL for a memory-resident object into its Dwarf postfix expression
13303 equivalent. This routine recursively descends an RTL tree, turning
13304 it into Dwarf postfix code as it goes.
13306 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13308 MEM_MODE is the mode of the memory reference, needed to handle some
13309 autoincrement addressing modes.
13311 Return 0 if we can't represent the location. */
13313 dw_loc_descr_ref
13314 mem_loc_descriptor (rtx rtl, machine_mode mode,
13315 machine_mode mem_mode,
13316 enum var_init_status initialized)
13318 dw_loc_descr_ref mem_loc_result = NULL;
13319 enum dwarf_location_atom op;
13320 dw_loc_descr_ref op0, op1;
13321 rtx inner = NULL_RTX;
13323 if (mode == VOIDmode)
13324 mode = GET_MODE (rtl);
13326 /* Note that for a dynamically sized array, the location we will generate a
13327 description of here will be the lowest numbered location which is
13328 actually within the array. That's *not* necessarily the same as the
13329 zeroth element of the array. */
13331 rtl = targetm.delegitimize_address (rtl);
13333 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13334 return NULL;
13336 switch (GET_CODE (rtl))
13338 case POST_INC:
13339 case POST_DEC:
13340 case POST_MODIFY:
13341 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13343 case SUBREG:
13344 /* The case of a subreg may arise when we have a local (register)
13345 variable or a formal (register) parameter which doesn't quite fill
13346 up an entire register. For now, just assume that it is
13347 legitimate to make the Dwarf info refer to the whole register which
13348 contains the given subreg. */
13349 if (!subreg_lowpart_p (rtl))
13350 break;
13351 inner = SUBREG_REG (rtl);
13352 /* FALLTHRU */
13353 case TRUNCATE:
13354 if (inner == NULL_RTX)
13355 inner = XEXP (rtl, 0);
13356 if (SCALAR_INT_MODE_P (mode)
13357 && SCALAR_INT_MODE_P (GET_MODE (inner))
13358 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13359 #ifdef POINTERS_EXTEND_UNSIGNED
13360 || (mode == Pmode && mem_mode != VOIDmode)
13361 #endif
13363 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13365 mem_loc_result = mem_loc_descriptor (inner,
13366 GET_MODE (inner),
13367 mem_mode, initialized);
13368 break;
13370 if (dwarf_strict)
13371 break;
13372 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13373 break;
13374 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13375 && (!SCALAR_INT_MODE_P (mode)
13376 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
13377 break;
13378 else
13380 dw_die_ref type_die;
13381 dw_loc_descr_ref cvt;
13383 mem_loc_result = mem_loc_descriptor (inner,
13384 GET_MODE (inner),
13385 mem_mode, initialized);
13386 if (mem_loc_result == NULL)
13387 break;
13388 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13389 if (type_die == NULL)
13391 mem_loc_result = NULL;
13392 break;
13394 if (GET_MODE_SIZE (mode)
13395 != GET_MODE_SIZE (GET_MODE (inner)))
13396 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13397 else
13398 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13399 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13400 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13401 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13402 add_loc_descr (&mem_loc_result, cvt);
13403 if (SCALAR_INT_MODE_P (mode)
13404 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13406 /* Convert it to untyped afterwards. */
13407 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13408 add_loc_descr (&mem_loc_result, cvt);
13411 break;
13413 case REG:
13414 if (! SCALAR_INT_MODE_P (mode)
13415 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13416 && rtl != arg_pointer_rtx
13417 && rtl != frame_pointer_rtx
13418 #ifdef POINTERS_EXTEND_UNSIGNED
13419 && (mode != Pmode || mem_mode == VOIDmode)
13420 #endif
13423 dw_die_ref type_die;
13424 unsigned int dbx_regnum;
13426 if (dwarf_strict)
13427 break;
13428 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13429 break;
13430 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13431 if (type_die == NULL)
13432 break;
13434 dbx_regnum = dbx_reg_number (rtl);
13435 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13436 break;
13437 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13438 dbx_regnum, 0);
13439 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13440 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13441 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13442 break;
13444 /* Whenever a register number forms a part of the description of the
13445 method for calculating the (dynamic) address of a memory resident
13446 object, DWARF rules require the register number be referred to as
13447 a "base register". This distinction is not based in any way upon
13448 what category of register the hardware believes the given register
13449 belongs to. This is strictly DWARF terminology we're dealing with
13450 here. Note that in cases where the location of a memory-resident
13451 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13452 OP_CONST (0)) the actual DWARF location descriptor that we generate
13453 may just be OP_BASEREG (basereg). This may look deceptively like
13454 the object in question was allocated to a register (rather than in
13455 memory) so DWARF consumers need to be aware of the subtle
13456 distinction between OP_REG and OP_BASEREG. */
13457 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13458 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13459 else if (stack_realign_drap
13460 && crtl->drap_reg
13461 && crtl->args.internal_arg_pointer == rtl
13462 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13464 /* If RTL is internal_arg_pointer, which has been optimized
13465 out, use DRAP instead. */
13466 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13467 VAR_INIT_STATUS_INITIALIZED);
13469 break;
13471 case SIGN_EXTEND:
13472 case ZERO_EXTEND:
13473 if (!SCALAR_INT_MODE_P (mode))
13474 break;
13475 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13476 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13477 if (op0 == 0)
13478 break;
13479 else if (GET_CODE (rtl) == ZERO_EXTEND
13480 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13481 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13482 < HOST_BITS_PER_WIDE_INT
13483 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13484 to expand zero extend as two shifts instead of
13485 masking. */
13486 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13488 machine_mode imode = GET_MODE (XEXP (rtl, 0));
13489 mem_loc_result = op0;
13490 add_loc_descr (&mem_loc_result,
13491 int_loc_descriptor (GET_MODE_MASK (imode)));
13492 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13494 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13496 int shift = DWARF2_ADDR_SIZE
13497 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13498 shift *= BITS_PER_UNIT;
13499 if (GET_CODE (rtl) == SIGN_EXTEND)
13500 op = DW_OP_shra;
13501 else
13502 op = DW_OP_shr;
13503 mem_loc_result = op0;
13504 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13505 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13506 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13507 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13509 else if (!dwarf_strict)
13511 dw_die_ref type_die1, type_die2;
13512 dw_loc_descr_ref cvt;
13514 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13515 GET_CODE (rtl) == ZERO_EXTEND);
13516 if (type_die1 == NULL)
13517 break;
13518 type_die2 = base_type_for_mode (mode, 1);
13519 if (type_die2 == NULL)
13520 break;
13521 mem_loc_result = op0;
13522 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13523 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13524 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13525 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13526 add_loc_descr (&mem_loc_result, cvt);
13527 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13528 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13529 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13530 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13531 add_loc_descr (&mem_loc_result, cvt);
13533 break;
13535 case MEM:
13537 rtx new_rtl = avoid_constant_pool_reference (rtl);
13538 if (new_rtl != rtl)
13540 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13541 initialized);
13542 if (mem_loc_result != NULL)
13543 return mem_loc_result;
13546 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13547 get_address_mode (rtl), mode,
13548 VAR_INIT_STATUS_INITIALIZED);
13549 if (mem_loc_result == NULL)
13550 mem_loc_result = tls_mem_loc_descriptor (rtl);
13551 if (mem_loc_result != NULL)
13553 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13554 || !SCALAR_INT_MODE_P(mode))
13556 dw_die_ref type_die;
13557 dw_loc_descr_ref deref;
13559 if (dwarf_strict)
13560 return NULL;
13561 type_die
13562 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13563 if (type_die == NULL)
13564 return NULL;
13565 deref = new_loc_descr (DW_OP_GNU_deref_type,
13566 GET_MODE_SIZE (mode), 0);
13567 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13568 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13569 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13570 add_loc_descr (&mem_loc_result, deref);
13572 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13573 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13574 else
13575 add_loc_descr (&mem_loc_result,
13576 new_loc_descr (DW_OP_deref_size,
13577 GET_MODE_SIZE (mode), 0));
13579 break;
13581 case LO_SUM:
13582 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13584 case LABEL_REF:
13585 /* Some ports can transform a symbol ref into a label ref, because
13586 the symbol ref is too far away and has to be dumped into a constant
13587 pool. */
13588 case CONST:
13589 case SYMBOL_REF:
13590 if (!SCALAR_INT_MODE_P (mode)
13591 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13592 #ifdef POINTERS_EXTEND_UNSIGNED
13593 && (mode != Pmode || mem_mode == VOIDmode)
13594 #endif
13596 break;
13597 if (GET_CODE (rtl) == SYMBOL_REF
13598 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13600 dw_loc_descr_ref temp;
13602 /* If this is not defined, we have no way to emit the data. */
13603 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13604 break;
13606 temp = new_addr_loc_descr (rtl, dtprel_true);
13608 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13609 add_loc_descr (&mem_loc_result, temp);
13611 break;
13614 if (!const_ok_for_output (rtl))
13616 if (GET_CODE (rtl) == CONST)
13617 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13618 initialized);
13619 break;
13622 symref:
13623 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13624 vec_safe_push (used_rtx_array, rtl);
13625 break;
13627 case CONCAT:
13628 case CONCATN:
13629 case VAR_LOCATION:
13630 case DEBUG_IMPLICIT_PTR:
13631 expansion_failed (NULL_TREE, rtl,
13632 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13633 return 0;
13635 case ENTRY_VALUE:
13636 if (dwarf_strict)
13637 return NULL;
13638 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13640 if (!SCALAR_INT_MODE_P (mode)
13641 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13642 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13643 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13644 else
13646 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13647 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13648 return NULL;
13649 op0 = one_reg_loc_descriptor (dbx_regnum,
13650 VAR_INIT_STATUS_INITIALIZED);
13653 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13654 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13656 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13657 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13658 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13659 return NULL;
13661 else
13662 gcc_unreachable ();
13663 if (op0 == NULL)
13664 return NULL;
13665 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13666 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13667 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13668 break;
13670 case DEBUG_PARAMETER_REF:
13671 mem_loc_result = parameter_ref_descriptor (rtl);
13672 break;
13674 case PRE_MODIFY:
13675 /* Extract the PLUS expression nested inside and fall into
13676 PLUS code below. */
13677 rtl = XEXP (rtl, 1);
13678 goto plus;
13680 case PRE_INC:
13681 case PRE_DEC:
13682 /* Turn these into a PLUS expression and fall into the PLUS code
13683 below. */
13684 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13685 gen_int_mode (GET_CODE (rtl) == PRE_INC
13686 ? GET_MODE_UNIT_SIZE (mem_mode)
13687 : -GET_MODE_UNIT_SIZE (mem_mode),
13688 mode));
13690 /* fall through */
13692 case PLUS:
13693 plus:
13694 if (is_based_loc (rtl)
13695 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13696 || XEXP (rtl, 0) == arg_pointer_rtx
13697 || XEXP (rtl, 0) == frame_pointer_rtx)
13698 && SCALAR_INT_MODE_P (mode))
13699 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13700 INTVAL (XEXP (rtl, 1)),
13701 VAR_INIT_STATUS_INITIALIZED);
13702 else
13704 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13705 VAR_INIT_STATUS_INITIALIZED);
13706 if (mem_loc_result == 0)
13707 break;
13709 if (CONST_INT_P (XEXP (rtl, 1))
13710 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13711 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13712 else
13714 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13715 VAR_INIT_STATUS_INITIALIZED);
13716 if (op1 == 0)
13717 return NULL;
13718 add_loc_descr (&mem_loc_result, op1);
13719 add_loc_descr (&mem_loc_result,
13720 new_loc_descr (DW_OP_plus, 0, 0));
13723 break;
13725 /* If a pseudo-reg is optimized away, it is possible for it to
13726 be replaced with a MEM containing a multiply or shift. */
13727 case MINUS:
13728 op = DW_OP_minus;
13729 goto do_binop;
13731 case MULT:
13732 op = DW_OP_mul;
13733 goto do_binop;
13735 case DIV:
13736 if (!dwarf_strict
13737 && SCALAR_INT_MODE_P (mode)
13738 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13740 mem_loc_result = typed_binop (DW_OP_div, rtl,
13741 base_type_for_mode (mode, 0),
13742 mode, mem_mode);
13743 break;
13745 op = DW_OP_div;
13746 goto do_binop;
13748 case UMOD:
13749 op = DW_OP_mod;
13750 goto do_binop;
13752 case ASHIFT:
13753 op = DW_OP_shl;
13754 goto do_shift;
13756 case ASHIFTRT:
13757 op = DW_OP_shra;
13758 goto do_shift;
13760 case LSHIFTRT:
13761 op = DW_OP_shr;
13762 goto do_shift;
13764 do_shift:
13765 if (!SCALAR_INT_MODE_P (mode))
13766 break;
13767 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13768 VAR_INIT_STATUS_INITIALIZED);
13770 rtx rtlop1 = XEXP (rtl, 1);
13771 if (GET_MODE (rtlop1) != VOIDmode
13772 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13773 < GET_MODE_BITSIZE (mode))
13774 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13775 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13776 VAR_INIT_STATUS_INITIALIZED);
13779 if (op0 == 0 || op1 == 0)
13780 break;
13782 mem_loc_result = op0;
13783 add_loc_descr (&mem_loc_result, op1);
13784 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13785 break;
13787 case AND:
13788 op = DW_OP_and;
13789 goto do_binop;
13791 case IOR:
13792 op = DW_OP_or;
13793 goto do_binop;
13795 case XOR:
13796 op = DW_OP_xor;
13797 goto do_binop;
13799 do_binop:
13800 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13801 VAR_INIT_STATUS_INITIALIZED);
13802 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13803 VAR_INIT_STATUS_INITIALIZED);
13805 if (op0 == 0 || op1 == 0)
13806 break;
13808 mem_loc_result = op0;
13809 add_loc_descr (&mem_loc_result, op1);
13810 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13811 break;
13813 case MOD:
13814 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13816 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13817 base_type_for_mode (mode, 0),
13818 mode, mem_mode);
13819 break;
13822 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13823 VAR_INIT_STATUS_INITIALIZED);
13824 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13825 VAR_INIT_STATUS_INITIALIZED);
13827 if (op0 == 0 || op1 == 0)
13828 break;
13830 mem_loc_result = op0;
13831 add_loc_descr (&mem_loc_result, op1);
13832 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
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_div, 0, 0));
13835 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13836 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13837 break;
13839 case UDIV:
13840 if (!dwarf_strict && SCALAR_INT_MODE_P (mode))
13842 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13844 op = DW_OP_div;
13845 goto do_binop;
13847 mem_loc_result = typed_binop (DW_OP_div, rtl,
13848 base_type_for_mode (mode, 1),
13849 mode, mem_mode);
13851 break;
13853 case NOT:
13854 op = DW_OP_not;
13855 goto do_unop;
13857 case ABS:
13858 op = DW_OP_abs;
13859 goto do_unop;
13861 case NEG:
13862 op = DW_OP_neg;
13863 goto do_unop;
13865 do_unop:
13866 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13867 VAR_INIT_STATUS_INITIALIZED);
13869 if (op0 == 0)
13870 break;
13872 mem_loc_result = op0;
13873 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13874 break;
13876 case CONST_INT:
13877 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13878 #ifdef POINTERS_EXTEND_UNSIGNED
13879 || (mode == Pmode
13880 && mem_mode != VOIDmode
13881 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13882 #endif
13885 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13886 break;
13888 if (!dwarf_strict
13889 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13890 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13892 dw_die_ref type_die = base_type_for_mode (mode, 1);
13893 machine_mode amode;
13894 if (type_die == NULL)
13895 return NULL;
13896 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13897 MODE_INT, 0);
13898 if (INTVAL (rtl) >= 0
13899 && amode != BLKmode
13900 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13901 /* const DW_OP_GNU_convert <XXX> vs.
13902 DW_OP_GNU_const_type <XXX, 1, const>. */
13903 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13904 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13906 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13907 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13908 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13909 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13910 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13911 add_loc_descr (&mem_loc_result, op0);
13912 return mem_loc_result;
13914 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13915 INTVAL (rtl));
13916 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13917 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13918 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13919 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13920 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13921 else
13923 mem_loc_result->dw_loc_oprnd2.val_class
13924 = dw_val_class_const_double;
13925 mem_loc_result->dw_loc_oprnd2.v.val_double
13926 = double_int::from_shwi (INTVAL (rtl));
13929 break;
13931 case CONST_DOUBLE:
13932 if (!dwarf_strict)
13934 dw_die_ref type_die;
13936 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13937 CONST_DOUBLE rtx could represent either a large integer
13938 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13939 the value is always a floating point constant.
13941 When it is an integer, a CONST_DOUBLE is used whenever
13942 the constant requires 2 HWIs to be adequately represented.
13943 We output CONST_DOUBLEs as blocks. */
13944 if (mode == VOIDmode
13945 || (GET_MODE (rtl) == VOIDmode
13946 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13947 break;
13948 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13949 if (type_die == NULL)
13950 return NULL;
13951 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13952 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13953 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13954 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13955 #if TARGET_SUPPORTS_WIDE_INT == 0
13956 if (!SCALAR_FLOAT_MODE_P (mode))
13958 mem_loc_result->dw_loc_oprnd2.val_class
13959 = dw_val_class_const_double;
13960 mem_loc_result->dw_loc_oprnd2.v.val_double
13961 = rtx_to_double_int (rtl);
13963 else
13964 #endif
13966 unsigned int length = GET_MODE_SIZE (mode);
13967 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13969 insert_float (rtl, array);
13970 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13971 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13972 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13973 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13976 break;
13978 case CONST_WIDE_INT:
13979 if (!dwarf_strict)
13981 dw_die_ref type_die;
13983 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13984 if (type_die == NULL)
13985 return NULL;
13986 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13987 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13988 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13989 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13990 mem_loc_result->dw_loc_oprnd2.val_class
13991 = dw_val_class_wide_int;
13992 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13993 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13995 break;
13997 case EQ:
13998 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13999 break;
14001 case GE:
14002 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14003 break;
14005 case GT:
14006 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14007 break;
14009 case LE:
14010 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14011 break;
14013 case LT:
14014 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14015 break;
14017 case NE:
14018 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14019 break;
14021 case GEU:
14022 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14023 break;
14025 case GTU:
14026 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14027 break;
14029 case LEU:
14030 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14031 break;
14033 case LTU:
14034 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14035 break;
14037 case UMIN:
14038 case UMAX:
14039 if (!SCALAR_INT_MODE_P (mode))
14040 break;
14041 /* FALLTHRU */
14042 case SMIN:
14043 case SMAX:
14044 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14045 break;
14047 case ZERO_EXTRACT:
14048 case SIGN_EXTRACT:
14049 if (CONST_INT_P (XEXP (rtl, 1))
14050 && CONST_INT_P (XEXP (rtl, 2))
14051 && ((unsigned) INTVAL (XEXP (rtl, 1))
14052 + (unsigned) INTVAL (XEXP (rtl, 2))
14053 <= GET_MODE_BITSIZE (mode))
14054 && SCALAR_INT_MODE_P (mode)
14055 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14056 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14058 int shift, size;
14059 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14060 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14061 if (op0 == 0)
14062 break;
14063 if (GET_CODE (rtl) == SIGN_EXTRACT)
14064 op = DW_OP_shra;
14065 else
14066 op = DW_OP_shr;
14067 mem_loc_result = op0;
14068 size = INTVAL (XEXP (rtl, 1));
14069 shift = INTVAL (XEXP (rtl, 2));
14070 if (BITS_BIG_ENDIAN)
14071 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14072 - shift - size;
14073 if (shift + size != (int) DWARF2_ADDR_SIZE)
14075 add_loc_descr (&mem_loc_result,
14076 int_loc_descriptor (DWARF2_ADDR_SIZE
14077 - shift - size));
14078 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14080 if (size != (int) DWARF2_ADDR_SIZE)
14082 add_loc_descr (&mem_loc_result,
14083 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14084 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14087 break;
14089 case IF_THEN_ELSE:
14091 dw_loc_descr_ref op2, bra_node, drop_node;
14092 op0 = mem_loc_descriptor (XEXP (rtl, 0),
14093 GET_MODE (XEXP (rtl, 0)) == VOIDmode
14094 ? word_mode : GET_MODE (XEXP (rtl, 0)),
14095 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14096 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14097 VAR_INIT_STATUS_INITIALIZED);
14098 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14099 VAR_INIT_STATUS_INITIALIZED);
14100 if (op0 == NULL || op1 == NULL || op2 == NULL)
14101 break;
14103 mem_loc_result = op1;
14104 add_loc_descr (&mem_loc_result, op2);
14105 add_loc_descr (&mem_loc_result, op0);
14106 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14107 add_loc_descr (&mem_loc_result, bra_node);
14108 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14109 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14110 add_loc_descr (&mem_loc_result, drop_node);
14111 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14112 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14114 break;
14116 case FLOAT_EXTEND:
14117 case FLOAT_TRUNCATE:
14118 case FLOAT:
14119 case UNSIGNED_FLOAT:
14120 case FIX:
14121 case UNSIGNED_FIX:
14122 if (!dwarf_strict)
14124 dw_die_ref type_die;
14125 dw_loc_descr_ref cvt;
14127 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14128 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14129 if (op0 == NULL)
14130 break;
14131 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
14132 && (GET_CODE (rtl) == FLOAT
14133 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14134 <= DWARF2_ADDR_SIZE))
14136 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14137 GET_CODE (rtl) == UNSIGNED_FLOAT);
14138 if (type_die == NULL)
14139 break;
14140 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14141 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14142 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14143 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14144 add_loc_descr (&op0, cvt);
14146 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14147 if (type_die == NULL)
14148 break;
14149 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14150 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14151 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14152 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14153 add_loc_descr (&op0, cvt);
14154 if (SCALAR_INT_MODE_P (mode)
14155 && (GET_CODE (rtl) == FIX
14156 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14158 op0 = convert_descriptor_to_mode (mode, op0);
14159 if (op0 == NULL)
14160 break;
14162 mem_loc_result = op0;
14164 break;
14166 case CLZ:
14167 case CTZ:
14168 case FFS:
14169 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14170 break;
14172 case POPCOUNT:
14173 case PARITY:
14174 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14175 break;
14177 case BSWAP:
14178 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14179 break;
14181 case ROTATE:
14182 case ROTATERT:
14183 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14184 break;
14186 case COMPARE:
14187 /* In theory, we could implement the above. */
14188 /* DWARF cannot represent the unsigned compare operations
14189 natively. */
14190 case SS_MULT:
14191 case US_MULT:
14192 case SS_DIV:
14193 case US_DIV:
14194 case SS_PLUS:
14195 case US_PLUS:
14196 case SS_MINUS:
14197 case US_MINUS:
14198 case SS_NEG:
14199 case US_NEG:
14200 case SS_ABS:
14201 case SS_ASHIFT:
14202 case US_ASHIFT:
14203 case SS_TRUNCATE:
14204 case US_TRUNCATE:
14205 case UNORDERED:
14206 case ORDERED:
14207 case UNEQ:
14208 case UNGE:
14209 case UNGT:
14210 case UNLE:
14211 case UNLT:
14212 case LTGT:
14213 case FRACT_CONVERT:
14214 case UNSIGNED_FRACT_CONVERT:
14215 case SAT_FRACT:
14216 case UNSIGNED_SAT_FRACT:
14217 case SQRT:
14218 case ASM_OPERANDS:
14219 case VEC_MERGE:
14220 case VEC_SELECT:
14221 case VEC_CONCAT:
14222 case VEC_DUPLICATE:
14223 case UNSPEC:
14224 case HIGH:
14225 case FMA:
14226 case STRICT_LOW_PART:
14227 case CONST_VECTOR:
14228 case CONST_FIXED:
14229 case CLRSB:
14230 case CLOBBER:
14231 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14232 can't express it in the debug info. This can happen e.g. with some
14233 TLS UNSPECs. */
14234 break;
14236 case CONST_STRING:
14237 resolve_one_addr (&rtl);
14238 goto symref;
14240 default:
14241 if (flag_checking)
14243 print_rtl (stderr, rtl);
14244 gcc_unreachable ();
14246 break;
14249 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14250 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14252 return mem_loc_result;
14255 /* Return a descriptor that describes the concatenation of two locations.
14256 This is typically a complex variable. */
14258 static dw_loc_descr_ref
14259 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14261 dw_loc_descr_ref cc_loc_result = NULL;
14262 dw_loc_descr_ref x0_ref
14263 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14264 dw_loc_descr_ref x1_ref
14265 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14267 if (x0_ref == 0 || x1_ref == 0)
14268 return 0;
14270 cc_loc_result = x0_ref;
14271 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14273 add_loc_descr (&cc_loc_result, x1_ref);
14274 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14276 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14277 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14279 return cc_loc_result;
14282 /* Return a descriptor that describes the concatenation of N
14283 locations. */
14285 static dw_loc_descr_ref
14286 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14288 unsigned int i;
14289 dw_loc_descr_ref cc_loc_result = NULL;
14290 unsigned int n = XVECLEN (concatn, 0);
14292 for (i = 0; i < n; ++i)
14294 dw_loc_descr_ref ref;
14295 rtx x = XVECEXP (concatn, 0, i);
14297 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14298 if (ref == NULL)
14299 return NULL;
14301 add_loc_descr (&cc_loc_result, ref);
14302 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14305 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14306 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14308 return cc_loc_result;
14311 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14312 for DEBUG_IMPLICIT_PTR RTL. */
14314 static dw_loc_descr_ref
14315 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14317 dw_loc_descr_ref ret;
14318 dw_die_ref ref;
14320 if (dwarf_strict)
14321 return NULL;
14322 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14323 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14324 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14325 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14326 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14327 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14328 if (ref)
14330 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14331 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14332 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14334 else
14336 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14337 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14339 return ret;
14342 /* Output a proper Dwarf location descriptor for a variable or parameter
14343 which is either allocated in a register or in a memory location. For a
14344 register, we just generate an OP_REG and the register number. For a
14345 memory location we provide a Dwarf postfix expression describing how to
14346 generate the (dynamic) address of the object onto the address stack.
14348 MODE is mode of the decl if this loc_descriptor is going to be used in
14349 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14350 allowed, VOIDmode otherwise.
14352 If we don't know how to describe it, return 0. */
14354 static dw_loc_descr_ref
14355 loc_descriptor (rtx rtl, machine_mode mode,
14356 enum var_init_status initialized)
14358 dw_loc_descr_ref loc_result = NULL;
14360 switch (GET_CODE (rtl))
14362 case SUBREG:
14363 /* The case of a subreg may arise when we have a local (register)
14364 variable or a formal (register) parameter which doesn't quite fill
14365 up an entire register. For now, just assume that it is
14366 legitimate to make the Dwarf info refer to the whole register which
14367 contains the given subreg. */
14368 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14369 loc_result = loc_descriptor (SUBREG_REG (rtl),
14370 GET_MODE (SUBREG_REG (rtl)), initialized);
14371 else
14372 goto do_default;
14373 break;
14375 case REG:
14376 loc_result = reg_loc_descriptor (rtl, initialized);
14377 break;
14379 case MEM:
14380 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14381 GET_MODE (rtl), initialized);
14382 if (loc_result == NULL)
14383 loc_result = tls_mem_loc_descriptor (rtl);
14384 if (loc_result == NULL)
14386 rtx new_rtl = avoid_constant_pool_reference (rtl);
14387 if (new_rtl != rtl)
14388 loc_result = loc_descriptor (new_rtl, mode, initialized);
14390 break;
14392 case CONCAT:
14393 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14394 initialized);
14395 break;
14397 case CONCATN:
14398 loc_result = concatn_loc_descriptor (rtl, initialized);
14399 break;
14401 case VAR_LOCATION:
14402 /* Single part. */
14403 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14405 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14406 if (GET_CODE (loc) == EXPR_LIST)
14407 loc = XEXP (loc, 0);
14408 loc_result = loc_descriptor (loc, mode, initialized);
14409 break;
14412 rtl = XEXP (rtl, 1);
14413 /* FALLTHRU */
14415 case PARALLEL:
14417 rtvec par_elems = XVEC (rtl, 0);
14418 int num_elem = GET_NUM_ELEM (par_elems);
14419 machine_mode mode;
14420 int i;
14422 /* Create the first one, so we have something to add to. */
14423 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14424 VOIDmode, initialized);
14425 if (loc_result == NULL)
14426 return NULL;
14427 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14428 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14429 for (i = 1; i < num_elem; i++)
14431 dw_loc_descr_ref temp;
14433 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14434 VOIDmode, initialized);
14435 if (temp == NULL)
14436 return NULL;
14437 add_loc_descr (&loc_result, temp);
14438 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14439 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14442 break;
14444 case CONST_INT:
14445 if (mode != VOIDmode && mode != BLKmode)
14446 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14447 INTVAL (rtl));
14448 break;
14450 case CONST_DOUBLE:
14451 if (mode == VOIDmode)
14452 mode = GET_MODE (rtl);
14454 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14456 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14458 /* Note that a CONST_DOUBLE rtx could represent either an integer
14459 or a floating-point constant. A CONST_DOUBLE is used whenever
14460 the constant requires more than one word in order to be
14461 adequately represented. We output CONST_DOUBLEs as blocks. */
14462 loc_result = new_loc_descr (DW_OP_implicit_value,
14463 GET_MODE_SIZE (mode), 0);
14464 #if TARGET_SUPPORTS_WIDE_INT == 0
14465 if (!SCALAR_FLOAT_MODE_P (mode))
14467 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14468 loc_result->dw_loc_oprnd2.v.val_double
14469 = rtx_to_double_int (rtl);
14471 else
14472 #endif
14474 unsigned int length = GET_MODE_SIZE (mode);
14475 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14477 insert_float (rtl, array);
14478 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14479 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14480 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14481 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14484 break;
14486 case CONST_WIDE_INT:
14487 if (mode == VOIDmode)
14488 mode = GET_MODE (rtl);
14490 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14492 loc_result = new_loc_descr (DW_OP_implicit_value,
14493 GET_MODE_SIZE (mode), 0);
14494 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14495 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14496 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14498 break;
14500 case CONST_VECTOR:
14501 if (mode == VOIDmode)
14502 mode = GET_MODE (rtl);
14504 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14506 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14507 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14508 unsigned char *array
14509 = ggc_vec_alloc<unsigned char> (length * elt_size);
14510 unsigned int i;
14511 unsigned char *p;
14512 machine_mode imode = GET_MODE_INNER (mode);
14514 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14515 switch (GET_MODE_CLASS (mode))
14517 case MODE_VECTOR_INT:
14518 for (i = 0, p = array; i < length; i++, p += elt_size)
14520 rtx elt = CONST_VECTOR_ELT (rtl, i);
14521 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14523 break;
14525 case MODE_VECTOR_FLOAT:
14526 for (i = 0, p = array; i < length; i++, p += elt_size)
14528 rtx elt = CONST_VECTOR_ELT (rtl, i);
14529 insert_float (elt, p);
14531 break;
14533 default:
14534 gcc_unreachable ();
14537 loc_result = new_loc_descr (DW_OP_implicit_value,
14538 length * elt_size, 0);
14539 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14540 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14541 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14542 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14544 break;
14546 case CONST:
14547 if (mode == VOIDmode
14548 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14549 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14550 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14552 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14553 break;
14555 /* FALLTHROUGH */
14556 case SYMBOL_REF:
14557 if (!const_ok_for_output (rtl))
14558 break;
14559 case LABEL_REF:
14560 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14561 && (dwarf_version >= 4 || !dwarf_strict))
14563 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14564 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14565 vec_safe_push (used_rtx_array, rtl);
14567 break;
14569 case DEBUG_IMPLICIT_PTR:
14570 loc_result = implicit_ptr_descriptor (rtl, 0);
14571 break;
14573 case PLUS:
14574 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14575 && CONST_INT_P (XEXP (rtl, 1)))
14577 loc_result
14578 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14579 break;
14581 /* FALLTHRU */
14582 do_default:
14583 default:
14584 if ((SCALAR_INT_MODE_P (mode)
14585 && GET_MODE (rtl) == mode
14586 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14587 && dwarf_version >= 4)
14588 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14590 /* Value expression. */
14591 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14592 if (loc_result)
14593 add_loc_descr (&loc_result,
14594 new_loc_descr (DW_OP_stack_value, 0, 0));
14596 break;
14599 return loc_result;
14602 /* We need to figure out what section we should use as the base for the
14603 address ranges where a given location is valid.
14604 1. If this particular DECL has a section associated with it, use that.
14605 2. If this function has a section associated with it, use that.
14606 3. Otherwise, use the text section.
14607 XXX: If you split a variable across multiple sections, we won't notice. */
14609 static const char *
14610 secname_for_decl (const_tree decl)
14612 const char *secname;
14614 if (VAR_OR_FUNCTION_DECL_P (decl)
14615 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14616 && DECL_SECTION_NAME (decl))
14617 secname = DECL_SECTION_NAME (decl);
14618 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14619 secname = DECL_SECTION_NAME (current_function_decl);
14620 else if (cfun && in_cold_section_p)
14621 secname = crtl->subsections.cold_section_label;
14622 else
14623 secname = text_section_label;
14625 return secname;
14628 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14630 static bool
14631 decl_by_reference_p (tree decl)
14633 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14634 || TREE_CODE (decl) == VAR_DECL)
14635 && DECL_BY_REFERENCE (decl));
14638 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14639 for VARLOC. */
14641 static dw_loc_descr_ref
14642 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14643 enum var_init_status initialized)
14645 int have_address = 0;
14646 dw_loc_descr_ref descr;
14647 machine_mode mode;
14649 if (want_address != 2)
14651 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14652 /* Single part. */
14653 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14655 varloc = PAT_VAR_LOCATION_LOC (varloc);
14656 if (GET_CODE (varloc) == EXPR_LIST)
14657 varloc = XEXP (varloc, 0);
14658 mode = GET_MODE (varloc);
14659 if (MEM_P (varloc))
14661 rtx addr = XEXP (varloc, 0);
14662 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14663 mode, initialized);
14664 if (descr)
14665 have_address = 1;
14666 else
14668 rtx x = avoid_constant_pool_reference (varloc);
14669 if (x != varloc)
14670 descr = mem_loc_descriptor (x, mode, VOIDmode,
14671 initialized);
14674 else
14675 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14677 else
14678 return 0;
14680 else
14682 if (GET_CODE (varloc) == VAR_LOCATION)
14683 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14684 else
14685 mode = DECL_MODE (loc);
14686 descr = loc_descriptor (varloc, mode, initialized);
14687 have_address = 1;
14690 if (!descr)
14691 return 0;
14693 if (want_address == 2 && !have_address
14694 && (dwarf_version >= 4 || !dwarf_strict))
14696 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14698 expansion_failed (loc, NULL_RTX,
14699 "DWARF address size mismatch");
14700 return 0;
14702 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14703 have_address = 1;
14705 /* Show if we can't fill the request for an address. */
14706 if (want_address && !have_address)
14708 expansion_failed (loc, NULL_RTX,
14709 "Want address and only have value");
14710 return 0;
14713 /* If we've got an address and don't want one, dereference. */
14714 if (!want_address && have_address)
14716 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14717 enum dwarf_location_atom op;
14719 if (size > DWARF2_ADDR_SIZE || size == -1)
14721 expansion_failed (loc, NULL_RTX,
14722 "DWARF address size mismatch");
14723 return 0;
14725 else if (size == DWARF2_ADDR_SIZE)
14726 op = DW_OP_deref;
14727 else
14728 op = DW_OP_deref_size;
14730 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14733 return descr;
14736 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14737 if it is not possible. */
14739 static dw_loc_descr_ref
14740 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14742 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14743 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14744 else if (dwarf_version >= 3 || !dwarf_strict)
14745 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14746 else
14747 return NULL;
14750 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14751 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14753 static dw_loc_descr_ref
14754 dw_sra_loc_expr (tree decl, rtx loc)
14756 rtx p;
14757 unsigned HOST_WIDE_INT padsize = 0;
14758 dw_loc_descr_ref descr, *descr_tail;
14759 unsigned HOST_WIDE_INT decl_size;
14760 rtx varloc;
14761 enum var_init_status initialized;
14763 if (DECL_SIZE (decl) == NULL
14764 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14765 return NULL;
14767 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14768 descr = NULL;
14769 descr_tail = &descr;
14771 for (p = loc; p; p = XEXP (p, 1))
14773 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14774 rtx loc_note = *decl_piece_varloc_ptr (p);
14775 dw_loc_descr_ref cur_descr;
14776 dw_loc_descr_ref *tail, last = NULL;
14777 unsigned HOST_WIDE_INT opsize = 0;
14779 if (loc_note == NULL_RTX
14780 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14782 padsize += bitsize;
14783 continue;
14785 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14786 varloc = NOTE_VAR_LOCATION (loc_note);
14787 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14788 if (cur_descr == NULL)
14790 padsize += bitsize;
14791 continue;
14794 /* Check that cur_descr either doesn't use
14795 DW_OP_*piece operations, or their sum is equal
14796 to bitsize. Otherwise we can't embed it. */
14797 for (tail = &cur_descr; *tail != NULL;
14798 tail = &(*tail)->dw_loc_next)
14799 if ((*tail)->dw_loc_opc == DW_OP_piece)
14801 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14802 * BITS_PER_UNIT;
14803 last = *tail;
14805 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14807 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14808 last = *tail;
14811 if (last != NULL && opsize != bitsize)
14813 padsize += bitsize;
14814 /* Discard the current piece of the descriptor and release any
14815 addr_table entries it uses. */
14816 remove_loc_list_addr_table_entries (cur_descr);
14817 continue;
14820 /* If there is a hole, add DW_OP_*piece after empty DWARF
14821 expression, which means that those bits are optimized out. */
14822 if (padsize)
14824 if (padsize > decl_size)
14826 remove_loc_list_addr_table_entries (cur_descr);
14827 goto discard_descr;
14829 decl_size -= padsize;
14830 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14831 if (*descr_tail == NULL)
14833 remove_loc_list_addr_table_entries (cur_descr);
14834 goto discard_descr;
14836 descr_tail = &(*descr_tail)->dw_loc_next;
14837 padsize = 0;
14839 *descr_tail = cur_descr;
14840 descr_tail = tail;
14841 if (bitsize > decl_size)
14842 goto discard_descr;
14843 decl_size -= bitsize;
14844 if (last == NULL)
14846 HOST_WIDE_INT offset = 0;
14847 if (GET_CODE (varloc) == VAR_LOCATION
14848 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14850 varloc = PAT_VAR_LOCATION_LOC (varloc);
14851 if (GET_CODE (varloc) == EXPR_LIST)
14852 varloc = XEXP (varloc, 0);
14856 if (GET_CODE (varloc) == CONST
14857 || GET_CODE (varloc) == SIGN_EXTEND
14858 || GET_CODE (varloc) == ZERO_EXTEND)
14859 varloc = XEXP (varloc, 0);
14860 else if (GET_CODE (varloc) == SUBREG)
14861 varloc = SUBREG_REG (varloc);
14862 else
14863 break;
14865 while (1);
14866 /* DW_OP_bit_size offset should be zero for register
14867 or implicit location descriptions and empty location
14868 descriptions, but for memory addresses needs big endian
14869 adjustment. */
14870 if (MEM_P (varloc))
14872 unsigned HOST_WIDE_INT memsize
14873 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14874 if (memsize != bitsize)
14876 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14877 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14878 goto discard_descr;
14879 if (memsize < bitsize)
14880 goto discard_descr;
14881 if (BITS_BIG_ENDIAN)
14882 offset = memsize - bitsize;
14886 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14887 if (*descr_tail == NULL)
14888 goto discard_descr;
14889 descr_tail = &(*descr_tail)->dw_loc_next;
14893 /* If there were any non-empty expressions, add padding till the end of
14894 the decl. */
14895 if (descr != NULL && decl_size != 0)
14897 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14898 if (*descr_tail == NULL)
14899 goto discard_descr;
14901 return descr;
14903 discard_descr:
14904 /* Discard the descriptor and release any addr_table entries it uses. */
14905 remove_loc_list_addr_table_entries (descr);
14906 return NULL;
14909 /* Return the dwarf representation of the location list LOC_LIST of
14910 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14911 function. */
14913 static dw_loc_list_ref
14914 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14916 const char *endname, *secname;
14917 rtx varloc;
14918 enum var_init_status initialized;
14919 struct var_loc_node *node;
14920 dw_loc_descr_ref descr;
14921 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14922 dw_loc_list_ref list = NULL;
14923 dw_loc_list_ref *listp = &list;
14925 /* Now that we know what section we are using for a base,
14926 actually construct the list of locations.
14927 The first location information is what is passed to the
14928 function that creates the location list, and the remaining
14929 locations just get added on to that list.
14930 Note that we only know the start address for a location
14931 (IE location changes), so to build the range, we use
14932 the range [current location start, next location start].
14933 This means we have to special case the last node, and generate
14934 a range of [last location start, end of function label]. */
14936 secname = secname_for_decl (decl);
14938 for (node = loc_list->first; node; node = node->next)
14939 if (GET_CODE (node->loc) == EXPR_LIST
14940 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14942 if (GET_CODE (node->loc) == EXPR_LIST)
14944 /* This requires DW_OP_{,bit_}piece, which is not usable
14945 inside DWARF expressions. */
14946 if (want_address != 2)
14947 continue;
14948 descr = dw_sra_loc_expr (decl, node->loc);
14949 if (descr == NULL)
14950 continue;
14952 else
14954 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14955 varloc = NOTE_VAR_LOCATION (node->loc);
14956 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14958 if (descr)
14960 bool range_across_switch = false;
14961 /* If section switch happens in between node->label
14962 and node->next->label (or end of function) and
14963 we can't emit it as a single entry list,
14964 emit two ranges, first one ending at the end
14965 of first partition and second one starting at the
14966 beginning of second partition. */
14967 if (node == loc_list->last_before_switch
14968 && (node != loc_list->first || loc_list->first->next)
14969 && current_function_decl)
14971 endname = cfun->fde->dw_fde_end;
14972 range_across_switch = true;
14974 /* The variable has a location between NODE->LABEL and
14975 NODE->NEXT->LABEL. */
14976 else if (node->next)
14977 endname = node->next->label;
14978 /* If the variable has a location at the last label
14979 it keeps its location until the end of function. */
14980 else if (!current_function_decl)
14981 endname = text_end_label;
14982 else
14984 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14985 current_function_funcdef_no);
14986 endname = ggc_strdup (label_id);
14989 *listp = new_loc_list (descr, node->label, endname, secname);
14990 if (TREE_CODE (decl) == PARM_DECL
14991 && node == loc_list->first
14992 && NOTE_P (node->loc)
14993 && strcmp (node->label, endname) == 0)
14994 (*listp)->force = true;
14995 listp = &(*listp)->dw_loc_next;
14997 if (range_across_switch)
14999 if (GET_CODE (node->loc) == EXPR_LIST)
15000 descr = dw_sra_loc_expr (decl, node->loc);
15001 else
15003 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15004 varloc = NOTE_VAR_LOCATION (node->loc);
15005 descr = dw_loc_list_1 (decl, varloc, want_address,
15006 initialized);
15008 gcc_assert (descr);
15009 /* The variable has a location between NODE->LABEL and
15010 NODE->NEXT->LABEL. */
15011 if (node->next)
15012 endname = node->next->label;
15013 else
15014 endname = cfun->fde->dw_fde_second_end;
15015 *listp = new_loc_list (descr,
15016 cfun->fde->dw_fde_second_begin,
15017 endname, secname);
15018 listp = &(*listp)->dw_loc_next;
15023 /* Try to avoid the overhead of a location list emitting a location
15024 expression instead, but only if we didn't have more than one
15025 location entry in the first place. If some entries were not
15026 representable, we don't want to pretend a single entry that was
15027 applies to the entire scope in which the variable is
15028 available. */
15029 if (list && loc_list->first->next)
15030 gen_llsym (list);
15032 return list;
15035 /* Return if the loc_list has only single element and thus can be represented
15036 as location description. */
15038 static bool
15039 single_element_loc_list_p (dw_loc_list_ref list)
15041 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15042 return !list->ll_symbol;
15045 /* To each location in list LIST add loc descr REF. */
15047 static void
15048 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15050 dw_loc_descr_ref copy;
15051 add_loc_descr (&list->expr, ref);
15052 list = list->dw_loc_next;
15053 while (list)
15055 copy = ggc_alloc<dw_loc_descr_node> ();
15056 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15057 add_loc_descr (&list->expr, copy);
15058 while (copy->dw_loc_next)
15060 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15061 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15062 copy->dw_loc_next = new_copy;
15063 copy = new_copy;
15065 list = list->dw_loc_next;
15069 /* Given two lists RET and LIST
15070 produce location list that is result of adding expression in LIST
15071 to expression in RET on each position in program.
15072 Might be destructive on both RET and LIST.
15074 TODO: We handle only simple cases of RET or LIST having at most one
15075 element. General case would inolve sorting the lists in program order
15076 and merging them that will need some additional work.
15077 Adding that will improve quality of debug info especially for SRA-ed
15078 structures. */
15080 static void
15081 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15083 if (!list)
15084 return;
15085 if (!*ret)
15087 *ret = list;
15088 return;
15090 if (!list->dw_loc_next)
15092 add_loc_descr_to_each (*ret, list->expr);
15093 return;
15095 if (!(*ret)->dw_loc_next)
15097 add_loc_descr_to_each (list, (*ret)->expr);
15098 *ret = list;
15099 return;
15101 expansion_failed (NULL_TREE, NULL_RTX,
15102 "Don't know how to merge two non-trivial"
15103 " location lists.\n");
15104 *ret = NULL;
15105 return;
15108 /* LOC is constant expression. Try a luck, look it up in constant
15109 pool and return its loc_descr of its address. */
15111 static dw_loc_descr_ref
15112 cst_pool_loc_descr (tree loc)
15114 /* Get an RTL for this, if something has been emitted. */
15115 rtx rtl = lookup_constant_def (loc);
15117 if (!rtl || !MEM_P (rtl))
15119 gcc_assert (!rtl);
15120 return 0;
15122 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15124 /* TODO: We might get more coverage if we was actually delaying expansion
15125 of all expressions till end of compilation when constant pools are fully
15126 populated. */
15127 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15129 expansion_failed (loc, NULL_RTX,
15130 "CST value in contant pool but not marked.");
15131 return 0;
15133 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15134 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15137 /* Return dw_loc_list representing address of addr_expr LOC
15138 by looking for inner INDIRECT_REF expression and turning
15139 it into simple arithmetics.
15141 See loc_list_from_tree for the meaning of CONTEXT. */
15143 static dw_loc_list_ref
15144 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15145 const loc_descr_context *context)
15147 tree obj, offset;
15148 HOST_WIDE_INT bitsize, bitpos, bytepos;
15149 machine_mode mode;
15150 int unsignedp, reversep, volatilep = 0;
15151 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15153 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15154 &bitsize, &bitpos, &offset, &mode,
15155 &unsignedp, &reversep, &volatilep);
15156 STRIP_NOPS (obj);
15157 if (bitpos % BITS_PER_UNIT)
15159 expansion_failed (loc, NULL_RTX, "bitfield access");
15160 return 0;
15162 if (!INDIRECT_REF_P (obj))
15164 expansion_failed (obj,
15165 NULL_RTX, "no indirect ref in inner refrence");
15166 return 0;
15168 if (!offset && !bitpos)
15169 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15170 context);
15171 else if (toplev
15172 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15173 && (dwarf_version >= 4 || !dwarf_strict))
15175 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15176 if (!list_ret)
15177 return 0;
15178 if (offset)
15180 /* Variable offset. */
15181 list_ret1 = loc_list_from_tree (offset, 0, context);
15182 if (list_ret1 == 0)
15183 return 0;
15184 add_loc_list (&list_ret, list_ret1);
15185 if (!list_ret)
15186 return 0;
15187 add_loc_descr_to_each (list_ret,
15188 new_loc_descr (DW_OP_plus, 0, 0));
15190 bytepos = bitpos / BITS_PER_UNIT;
15191 if (bytepos > 0)
15192 add_loc_descr_to_each (list_ret,
15193 new_loc_descr (DW_OP_plus_uconst,
15194 bytepos, 0));
15195 else if (bytepos < 0)
15196 loc_list_plus_const (list_ret, bytepos);
15197 add_loc_descr_to_each (list_ret,
15198 new_loc_descr (DW_OP_stack_value, 0, 0));
15200 return list_ret;
15203 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15204 all operations from LOC are nops, move to the last one. Insert in NOPS all
15205 operations that are skipped. */
15207 static void
15208 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15209 hash_set<dw_loc_descr_ref> &nops)
15211 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15213 nops.add (loc);
15214 loc = loc->dw_loc_next;
15218 /* Helper for loc_descr_without_nops: free the location description operation
15219 P. */
15221 bool
15222 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15224 ggc_free (loc);
15225 return true;
15228 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15229 finishes LOC. */
15231 static void
15232 loc_descr_without_nops (dw_loc_descr_ref &loc)
15234 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15235 return;
15237 /* Set of all DW_OP_nop operations we remove. */
15238 hash_set<dw_loc_descr_ref> nops;
15240 /* First, strip all prefix NOP operations in order to keep the head of the
15241 operations list. */
15242 loc_descr_to_next_no_nop (loc, nops);
15244 for (dw_loc_descr_ref cur = loc; cur != NULL;)
15246 /* For control flow operations: strip "prefix" nops in destination
15247 labels. */
15248 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15249 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15250 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15251 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15253 /* Do the same for the operations that follow, then move to the next
15254 iteration. */
15255 if (cur->dw_loc_next != NULL)
15256 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15257 cur = cur->dw_loc_next;
15260 nops.traverse<void *, free_loc_descr> (NULL);
15264 struct dwarf_procedure_info;
15266 /* Helper structure for location descriptions generation. */
15267 struct loc_descr_context
15269 /* The type that is implicitly referenced by DW_OP_push_object_address, or
15270 NULL_TREE if DW_OP_push_object_address in invalid for this location
15271 description. This is used when processing PLACEHOLDER_EXPR nodes. */
15272 tree context_type;
15273 /* The ..._DECL node that should be translated as a
15274 DW_OP_push_object_address operation. */
15275 tree base_decl;
15276 /* Information about the DWARF procedure we are currently generating. NULL if
15277 we are not generating a DWARF procedure. */
15278 struct dwarf_procedure_info *dpi;
15281 /* DWARF procedures generation
15283 DWARF expressions (aka. location descriptions) are used to encode variable
15284 things such as sizes or offsets. Such computations can have redundant parts
15285 that can be factorized in order to reduce the size of the output debug
15286 information. This is the whole point of DWARF procedures.
15288 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15289 already factorized into functions ("size functions") in order to handle very
15290 big and complex types. Such functions are quite simple: they have integral
15291 arguments, they return an integral result and their body contains only a
15292 return statement with arithmetic expressions. This is the only kind of
15293 function we are interested in translating into DWARF procedures, here.
15295 DWARF expressions and DWARF procedure are executed using a stack, so we have
15296 to define some calling convention for them to interact. Let's say that:
15298 - Before calling a DWARF procedure, DWARF expressions must push on the stack
15299 all arguments in reverse order (right-to-left) so that when the DWARF
15300 procedure execution starts, the first argument is the top of the stack.
15302 - Then, when returning, the DWARF procedure must have consumed all arguments
15303 on the stack, must have pushed the result and touched nothing else.
15305 - Each integral argument and the result are integral types can be hold in a
15306 single stack slot.
15308 - We call "frame offset" the number of stack slots that are "under DWARF
15309 procedure control": it includes the arguments slots, the temporaries and
15310 the result slot. Thus, it is equal to the number of arguments when the
15311 procedure execution starts and must be equal to one (the result) when it
15312 returns. */
15314 /* Helper structure used when generating operations for a DWARF procedure. */
15315 struct dwarf_procedure_info
15317 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15318 currently translated. */
15319 tree fndecl;
15320 /* The number of arguments FNDECL takes. */
15321 unsigned args_count;
15324 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
15325 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
15326 equate it to this DIE. */
15328 static dw_die_ref
15329 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15330 dw_die_ref parent_die)
15332 dw_die_ref dwarf_proc_die;
15334 if ((dwarf_version < 3 && dwarf_strict)
15335 || location == NULL)
15336 return NULL;
15338 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
15339 if (fndecl)
15340 equate_decl_number_to_die (fndecl, dwarf_proc_die);
15341 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15342 return dwarf_proc_die;
15345 /* Return whether TYPE is a supported type as a DWARF procedure argument
15346 type or return type (we handle only scalar types and pointer types that
15347 aren't wider than the DWARF expression evaluation stack. */
15349 static bool
15350 is_handled_procedure_type (tree type)
15352 return ((INTEGRAL_TYPE_P (type)
15353 || TREE_CODE (type) == OFFSET_TYPE
15354 || TREE_CODE (type) == POINTER_TYPE)
15355 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15358 /* Helper for resolve_args_picking: do the same but stop when coming across
15359 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
15360 offset *before* evaluating the corresponding operation. */
15362 static bool
15363 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15364 struct dwarf_procedure_info *dpi,
15365 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
15367 /* The "frame_offset" identifier is already used to name a macro... */
15368 unsigned frame_offset_ = initial_frame_offset;
15369 dw_loc_descr_ref l;
15371 for (l = loc; l != NULL;)
15373 bool existed;
15374 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
15376 /* If we already met this node, there is nothing to compute anymore. */
15377 if (existed)
15379 /* Make sure that the stack size is consistent wherever the execution
15380 flow comes from. */
15381 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
15382 break;
15384 l_frame_offset = frame_offset_;
15386 /* If needed, relocate the picking offset with respect to the frame
15387 offset. */
15388 if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15390 /* frame_offset_ is the size of the current stack frame, including
15391 incoming arguments. Besides, the arguments are pushed
15392 right-to-left. Thus, in order to access the Nth argument from
15393 this operation node, the picking has to skip temporaries *plus*
15394 one stack slot per argument (0 for the first one, 1 for the second
15395 one, etc.).
15397 The targetted argument number (N) is already set as the operand,
15398 and the number of temporaries can be computed with:
15399 frame_offsets_ - dpi->args_count */
15400 l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15402 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
15403 if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15404 return false;
15407 /* Update frame_offset according to the effect the current operation has
15408 on the stack. */
15409 switch (l->dw_loc_opc)
15411 case DW_OP_deref:
15412 case DW_OP_swap:
15413 case DW_OP_rot:
15414 case DW_OP_abs:
15415 case DW_OP_neg:
15416 case DW_OP_not:
15417 case DW_OP_plus_uconst:
15418 case DW_OP_skip:
15419 case DW_OP_reg0:
15420 case DW_OP_reg1:
15421 case DW_OP_reg2:
15422 case DW_OP_reg3:
15423 case DW_OP_reg4:
15424 case DW_OP_reg5:
15425 case DW_OP_reg6:
15426 case DW_OP_reg7:
15427 case DW_OP_reg8:
15428 case DW_OP_reg9:
15429 case DW_OP_reg10:
15430 case DW_OP_reg11:
15431 case DW_OP_reg12:
15432 case DW_OP_reg13:
15433 case DW_OP_reg14:
15434 case DW_OP_reg15:
15435 case DW_OP_reg16:
15436 case DW_OP_reg17:
15437 case DW_OP_reg18:
15438 case DW_OP_reg19:
15439 case DW_OP_reg20:
15440 case DW_OP_reg21:
15441 case DW_OP_reg22:
15442 case DW_OP_reg23:
15443 case DW_OP_reg24:
15444 case DW_OP_reg25:
15445 case DW_OP_reg26:
15446 case DW_OP_reg27:
15447 case DW_OP_reg28:
15448 case DW_OP_reg29:
15449 case DW_OP_reg30:
15450 case DW_OP_reg31:
15451 case DW_OP_bregx:
15452 case DW_OP_piece:
15453 case DW_OP_deref_size:
15454 case DW_OP_nop:
15455 case DW_OP_form_tls_address:
15456 case DW_OP_bit_piece:
15457 case DW_OP_implicit_value:
15458 case DW_OP_stack_value:
15459 break;
15461 case DW_OP_addr:
15462 case DW_OP_const1u:
15463 case DW_OP_const1s:
15464 case DW_OP_const2u:
15465 case DW_OP_const2s:
15466 case DW_OP_const4u:
15467 case DW_OP_const4s:
15468 case DW_OP_const8u:
15469 case DW_OP_const8s:
15470 case DW_OP_constu:
15471 case DW_OP_consts:
15472 case DW_OP_dup:
15473 case DW_OP_over:
15474 case DW_OP_pick:
15475 case DW_OP_lit0:
15476 case DW_OP_lit1:
15477 case DW_OP_lit2:
15478 case DW_OP_lit3:
15479 case DW_OP_lit4:
15480 case DW_OP_lit5:
15481 case DW_OP_lit6:
15482 case DW_OP_lit7:
15483 case DW_OP_lit8:
15484 case DW_OP_lit9:
15485 case DW_OP_lit10:
15486 case DW_OP_lit11:
15487 case DW_OP_lit12:
15488 case DW_OP_lit13:
15489 case DW_OP_lit14:
15490 case DW_OP_lit15:
15491 case DW_OP_lit16:
15492 case DW_OP_lit17:
15493 case DW_OP_lit18:
15494 case DW_OP_lit19:
15495 case DW_OP_lit20:
15496 case DW_OP_lit21:
15497 case DW_OP_lit22:
15498 case DW_OP_lit23:
15499 case DW_OP_lit24:
15500 case DW_OP_lit25:
15501 case DW_OP_lit26:
15502 case DW_OP_lit27:
15503 case DW_OP_lit28:
15504 case DW_OP_lit29:
15505 case DW_OP_lit30:
15506 case DW_OP_lit31:
15507 case DW_OP_breg0:
15508 case DW_OP_breg1:
15509 case DW_OP_breg2:
15510 case DW_OP_breg3:
15511 case DW_OP_breg4:
15512 case DW_OP_breg5:
15513 case DW_OP_breg6:
15514 case DW_OP_breg7:
15515 case DW_OP_breg8:
15516 case DW_OP_breg9:
15517 case DW_OP_breg10:
15518 case DW_OP_breg11:
15519 case DW_OP_breg12:
15520 case DW_OP_breg13:
15521 case DW_OP_breg14:
15522 case DW_OP_breg15:
15523 case DW_OP_breg16:
15524 case DW_OP_breg17:
15525 case DW_OP_breg18:
15526 case DW_OP_breg19:
15527 case DW_OP_breg20:
15528 case DW_OP_breg21:
15529 case DW_OP_breg22:
15530 case DW_OP_breg23:
15531 case DW_OP_breg24:
15532 case DW_OP_breg25:
15533 case DW_OP_breg26:
15534 case DW_OP_breg27:
15535 case DW_OP_breg28:
15536 case DW_OP_breg29:
15537 case DW_OP_breg30:
15538 case DW_OP_breg31:
15539 case DW_OP_fbreg:
15540 case DW_OP_push_object_address:
15541 case DW_OP_call_frame_cfa:
15542 ++frame_offset_;
15543 break;
15545 case DW_OP_drop:
15546 case DW_OP_xderef:
15547 case DW_OP_and:
15548 case DW_OP_div:
15549 case DW_OP_minus:
15550 case DW_OP_mod:
15551 case DW_OP_mul:
15552 case DW_OP_or:
15553 case DW_OP_plus:
15554 case DW_OP_shl:
15555 case DW_OP_shr:
15556 case DW_OP_shra:
15557 case DW_OP_xor:
15558 case DW_OP_bra:
15559 case DW_OP_eq:
15560 case DW_OP_ge:
15561 case DW_OP_gt:
15562 case DW_OP_le:
15563 case DW_OP_lt:
15564 case DW_OP_ne:
15565 case DW_OP_regx:
15566 case DW_OP_xderef_size:
15567 --frame_offset_;
15568 break;
15570 case DW_OP_call2:
15571 case DW_OP_call4:
15572 case DW_OP_call_ref:
15574 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15575 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15577 if (stack_usage == NULL)
15578 return false;
15579 frame_offset_ += *stack_usage;
15580 break;
15583 case DW_OP_GNU_push_tls_address:
15584 case DW_OP_GNU_uninit:
15585 case DW_OP_GNU_encoded_addr:
15586 case DW_OP_GNU_implicit_pointer:
15587 case DW_OP_GNU_entry_value:
15588 case DW_OP_GNU_const_type:
15589 case DW_OP_GNU_regval_type:
15590 case DW_OP_GNU_deref_type:
15591 case DW_OP_GNU_convert:
15592 case DW_OP_GNU_reinterpret:
15593 case DW_OP_GNU_parameter_ref:
15594 /* loc_list_from_tree will probably not output these operations for
15595 size functions, so assume they will not appear here. */
15596 /* Fall through... */
15598 default:
15599 gcc_unreachable ();
15602 /* Now, follow the control flow (except subroutine calls). */
15603 switch (l->dw_loc_opc)
15605 case DW_OP_bra:
15606 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15607 frame_offsets))
15608 return false;
15609 /* Fall through. */
15611 case DW_OP_skip:
15612 l = l->dw_loc_oprnd1.v.val_loc;
15613 break;
15615 case DW_OP_stack_value:
15616 return true;
15618 default:
15619 l = l->dw_loc_next;
15620 break;
15624 return true;
15627 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15628 operations) in order to resolve the operand of DW_OP_pick operations that
15629 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
15630 offset *before* LOC is executed. Return if all relocations were
15631 successful. */
15633 static bool
15634 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15635 struct dwarf_procedure_info *dpi)
15637 /* Associate to all visited operations the frame offset *before* evaluating
15638 this operation. */
15639 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
15641 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
15642 frame_offsets);
15645 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15646 Return NULL if it is not possible. */
15648 static dw_die_ref
15649 function_to_dwarf_procedure (tree fndecl)
15651 struct loc_descr_context ctx;
15652 struct dwarf_procedure_info dpi;
15653 dw_die_ref dwarf_proc_die;
15654 tree tree_body = DECL_SAVED_TREE (fndecl);
15655 dw_loc_descr_ref loc_body, epilogue;
15657 tree cursor;
15658 unsigned i;
15660 /* Do not generate multiple DWARF procedures for the same function
15661 declaration. */
15662 dwarf_proc_die = lookup_decl_die (fndecl);
15663 if (dwarf_proc_die != NULL)
15664 return dwarf_proc_die;
15666 /* DWARF procedures are available starting with the DWARFv3 standard. */
15667 if (dwarf_version < 3 && dwarf_strict)
15668 return NULL;
15670 /* We handle only functions for which we still have a body, that return a
15671 supported type and that takes arguments with supported types. Note that
15672 there is no point translating functions that return nothing. */
15673 if (tree_body == NULL_TREE
15674 || DECL_RESULT (fndecl) == NULL_TREE
15675 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15676 return NULL;
15678 for (cursor = DECL_ARGUMENTS (fndecl);
15679 cursor != NULL_TREE;
15680 cursor = TREE_CHAIN (cursor))
15681 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15682 return NULL;
15684 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
15685 if (TREE_CODE (tree_body) != RETURN_EXPR)
15686 return NULL;
15687 tree_body = TREE_OPERAND (tree_body, 0);
15688 if (TREE_CODE (tree_body) != MODIFY_EXPR
15689 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15690 return NULL;
15691 tree_body = TREE_OPERAND (tree_body, 1);
15693 /* Try to translate the body expression itself. Note that this will probably
15694 cause an infinite recursion if its call graph has a cycle. This is very
15695 unlikely for size functions, however, so don't bother with such things at
15696 the moment. */
15697 ctx.context_type = NULL_TREE;
15698 ctx.base_decl = NULL_TREE;
15699 ctx.dpi = &dpi;
15700 dpi.fndecl = fndecl;
15701 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15702 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15703 if (!loc_body)
15704 return NULL;
15706 /* After evaluating all operands in "loc_body", we should still have on the
15707 stack all arguments plus the desired function result (top of the stack).
15708 Generate code in order to keep only the result in our stack frame. */
15709 epilogue = NULL;
15710 for (i = 0; i < dpi.args_count; ++i)
15712 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15713 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15714 op_couple->dw_loc_next->dw_loc_next = epilogue;
15715 epilogue = op_couple;
15717 add_loc_descr (&loc_body, epilogue);
15718 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15719 return NULL;
15721 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15722 because they are considered useful. Now there is an epilogue, they are
15723 not anymore, so give it another try. */
15724 loc_descr_without_nops (loc_body);
15726 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15727 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
15728 though, given that size functions do not come from source, so they should
15729 not have a dedicated DW_TAG_subprogram DIE. */
15730 dwarf_proc_die
15731 = new_dwarf_proc_die (loc_body, fndecl,
15732 get_context_die (DECL_CONTEXT (fndecl)));
15734 /* The called DWARF procedure consumes one stack slot per argument and
15735 returns one stack slot. */
15736 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15738 return dwarf_proc_die;
15742 /* Generate Dwarf location list representing LOC.
15743 If WANT_ADDRESS is false, expression computing LOC will be computed
15744 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15745 if WANT_ADDRESS is 2, expression computing address useable in location
15746 will be returned (i.e. DW_OP_reg can be used
15747 to refer to register values).
15749 CONTEXT provides information to customize the location descriptions
15750 generation. Its context_type field specifies what type is implicitly
15751 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
15752 will not be generated.
15754 Its DPI field determines whether we are generating a DWARF expression for a
15755 DWARF procedure, so PARM_DECL references are processed specifically.
15757 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15758 and dpi fields were null. */
15760 static dw_loc_list_ref
15761 loc_list_from_tree_1 (tree loc, int want_address,
15762 const struct loc_descr_context *context)
15764 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15765 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15766 int have_address = 0;
15767 enum dwarf_location_atom op;
15769 /* ??? Most of the time we do not take proper care for sign/zero
15770 extending the values properly. Hopefully this won't be a real
15771 problem... */
15773 if (context != NULL
15774 && context->base_decl == loc
15775 && want_address == 0)
15777 if (dwarf_version >= 3 || !dwarf_strict)
15778 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15779 NULL, NULL, NULL);
15780 else
15781 return NULL;
15784 switch (TREE_CODE (loc))
15786 case ERROR_MARK:
15787 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15788 return 0;
15790 case PLACEHOLDER_EXPR:
15791 /* This case involves extracting fields from an object to determine the
15792 position of other fields. It is supposed to appear only as the first
15793 operand of COMPONENT_REF nodes and to reference precisely the type
15794 that the context allows. */
15795 if (context != NULL
15796 && TREE_TYPE (loc) == context->context_type
15797 && want_address >= 1)
15799 if (dwarf_version >= 3 || !dwarf_strict)
15801 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15802 have_address = 1;
15803 break;
15805 else
15806 return NULL;
15808 else
15809 expansion_failed (loc, NULL_RTX,
15810 "PLACEHOLDER_EXPR for an unexpected type");
15811 break;
15813 case CALL_EXPR:
15815 const int nargs = call_expr_nargs (loc);
15816 tree callee = get_callee_fndecl (loc);
15817 int i;
15818 dw_die_ref dwarf_proc;
15820 if (callee == NULL_TREE)
15821 goto call_expansion_failed;
15823 /* We handle only functions that return an integer. */
15824 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15825 goto call_expansion_failed;
15827 dwarf_proc = function_to_dwarf_procedure (callee);
15828 if (dwarf_proc == NULL)
15829 goto call_expansion_failed;
15831 /* Evaluate arguments right-to-left so that the first argument will
15832 be the top-most one on the stack. */
15833 for (i = nargs - 1; i >= 0; --i)
15835 dw_loc_descr_ref loc_descr
15836 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15837 context);
15839 if (loc_descr == NULL)
15840 goto call_expansion_failed;
15842 add_loc_descr (&ret, loc_descr);
15845 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15846 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15847 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15848 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15849 add_loc_descr (&ret, ret1);
15850 break;
15852 call_expansion_failed:
15853 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15854 /* There are no opcodes for these operations. */
15855 return 0;
15858 case PREINCREMENT_EXPR:
15859 case PREDECREMENT_EXPR:
15860 case POSTINCREMENT_EXPR:
15861 case POSTDECREMENT_EXPR:
15862 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15863 /* There are no opcodes for these operations. */
15864 return 0;
15866 case ADDR_EXPR:
15867 /* If we already want an address, see if there is INDIRECT_REF inside
15868 e.g. for &this->field. */
15869 if (want_address)
15871 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15872 (loc, want_address == 2, context);
15873 if (list_ret)
15874 have_address = 1;
15875 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15876 && (ret = cst_pool_loc_descr (loc)))
15877 have_address = 1;
15879 /* Otherwise, process the argument and look for the address. */
15880 if (!list_ret && !ret)
15881 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15882 else
15884 if (want_address)
15885 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15886 return NULL;
15888 break;
15890 case VAR_DECL:
15891 if (DECL_THREAD_LOCAL_P (loc))
15893 rtx rtl;
15894 enum dwarf_location_atom tls_op;
15895 enum dtprel_bool dtprel = dtprel_false;
15897 if (targetm.have_tls)
15899 /* If this is not defined, we have no way to emit the
15900 data. */
15901 if (!targetm.asm_out.output_dwarf_dtprel)
15902 return 0;
15904 /* The way DW_OP_GNU_push_tls_address is specified, we
15905 can only look up addresses of objects in the current
15906 module. We used DW_OP_addr as first op, but that's
15907 wrong, because DW_OP_addr is relocated by the debug
15908 info consumer, while DW_OP_GNU_push_tls_address
15909 operand shouldn't be. */
15910 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15911 return 0;
15912 dtprel = dtprel_true;
15913 tls_op = DW_OP_GNU_push_tls_address;
15915 else
15917 if (!targetm.emutls.debug_form_tls_address
15918 || !(dwarf_version >= 3 || !dwarf_strict))
15919 return 0;
15920 /* We stuffed the control variable into the DECL_VALUE_EXPR
15921 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15922 no longer appear in gimple code. We used the control
15923 variable in specific so that we could pick it up here. */
15924 loc = DECL_VALUE_EXPR (loc);
15925 tls_op = DW_OP_form_tls_address;
15928 rtl = rtl_for_decl_location (loc);
15929 if (rtl == NULL_RTX)
15930 return 0;
15932 if (!MEM_P (rtl))
15933 return 0;
15934 rtl = XEXP (rtl, 0);
15935 if (! CONSTANT_P (rtl))
15936 return 0;
15938 ret = new_addr_loc_descr (rtl, dtprel);
15939 ret1 = new_loc_descr (tls_op, 0, 0);
15940 add_loc_descr (&ret, ret1);
15942 have_address = 1;
15943 break;
15945 /* FALLTHRU */
15947 case PARM_DECL:
15948 if (context != NULL && context->dpi != NULL
15949 && DECL_CONTEXT (loc) == context->dpi->fndecl)
15951 /* We are generating code for a DWARF procedure and we want to access
15952 one of its arguments: find the appropriate argument offset and let
15953 the resolve_args_picking pass compute the offset that complies
15954 with the stack frame size. */
15955 unsigned i = 0;
15956 tree cursor;
15958 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15959 cursor != NULL_TREE && cursor != loc;
15960 cursor = TREE_CHAIN (cursor), ++i)
15962 /* If we are translating a DWARF procedure, all referenced parameters
15963 must belong to the current function. */
15964 gcc_assert (cursor != NULL_TREE);
15966 ret = new_loc_descr (DW_OP_pick, i, 0);
15967 ret->frame_offset_rel = 1;
15968 break;
15970 /* FALLTHRU */
15972 case RESULT_DECL:
15973 if (DECL_HAS_VALUE_EXPR_P (loc))
15974 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15975 want_address, context);
15976 /* FALLTHRU */
15978 case FUNCTION_DECL:
15980 rtx rtl;
15981 var_loc_list *loc_list = lookup_decl_loc (loc);
15983 if (loc_list && loc_list->first)
15985 list_ret = dw_loc_list (loc_list, loc, want_address);
15986 have_address = want_address != 0;
15987 break;
15989 rtl = rtl_for_decl_location (loc);
15990 if (rtl == NULL_RTX)
15992 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15993 return 0;
15995 else if (CONST_INT_P (rtl))
15997 HOST_WIDE_INT val = INTVAL (rtl);
15998 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15999 val &= GET_MODE_MASK (DECL_MODE (loc));
16000 ret = int_loc_descriptor (val);
16002 else if (GET_CODE (rtl) == CONST_STRING)
16004 expansion_failed (loc, NULL_RTX, "CONST_STRING");
16005 return 0;
16007 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16008 ret = new_addr_loc_descr (rtl, dtprel_false);
16009 else
16011 machine_mode mode, mem_mode;
16013 /* Certain constructs can only be represented at top-level. */
16014 if (want_address == 2)
16016 ret = loc_descriptor (rtl, VOIDmode,
16017 VAR_INIT_STATUS_INITIALIZED);
16018 have_address = 1;
16020 else
16022 mode = GET_MODE (rtl);
16023 mem_mode = VOIDmode;
16024 if (MEM_P (rtl))
16026 mem_mode = mode;
16027 mode = get_address_mode (rtl);
16028 rtl = XEXP (rtl, 0);
16029 have_address = 1;
16031 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16032 VAR_INIT_STATUS_INITIALIZED);
16034 if (!ret)
16035 expansion_failed (loc, rtl,
16036 "failed to produce loc descriptor for rtl");
16039 break;
16041 case MEM_REF:
16042 if (!integer_zerop (TREE_OPERAND (loc, 1)))
16044 have_address = 1;
16045 goto do_plus;
16047 /* Fallthru. */
16048 case INDIRECT_REF:
16049 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16050 have_address = 1;
16051 break;
16053 case TARGET_MEM_REF:
16054 case SSA_NAME:
16055 case DEBUG_EXPR_DECL:
16056 return NULL;
16058 case COMPOUND_EXPR:
16059 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16060 context);
16062 CASE_CONVERT:
16063 case VIEW_CONVERT_EXPR:
16064 case SAVE_EXPR:
16065 case MODIFY_EXPR:
16066 case NON_LVALUE_EXPR:
16067 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16068 context);
16070 case COMPONENT_REF:
16071 case BIT_FIELD_REF:
16072 case ARRAY_REF:
16073 case ARRAY_RANGE_REF:
16074 case REALPART_EXPR:
16075 case IMAGPART_EXPR:
16077 tree obj, offset;
16078 HOST_WIDE_INT bitsize, bitpos, bytepos;
16079 machine_mode mode;
16080 int unsignedp, reversep, volatilep = 0;
16082 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16083 &unsignedp, &reversep, &volatilep);
16085 gcc_assert (obj != loc);
16087 list_ret = loc_list_from_tree_1 (obj,
16088 want_address == 2
16089 && !bitpos && !offset ? 2 : 1,
16090 context);
16091 /* TODO: We can extract value of the small expression via shifting even
16092 for nonzero bitpos. */
16093 if (list_ret == 0)
16094 return 0;
16095 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16097 expansion_failed (loc, NULL_RTX,
16098 "bitfield access");
16099 return 0;
16102 if (offset != NULL_TREE)
16104 /* Variable offset. */
16105 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16106 if (list_ret1 == 0)
16107 return 0;
16108 add_loc_list (&list_ret, list_ret1);
16109 if (!list_ret)
16110 return 0;
16111 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16114 bytepos = bitpos / BITS_PER_UNIT;
16115 if (bytepos > 0)
16116 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16117 else if (bytepos < 0)
16118 loc_list_plus_const (list_ret, bytepos);
16120 have_address = 1;
16121 break;
16124 case INTEGER_CST:
16125 if ((want_address || !tree_fits_shwi_p (loc))
16126 && (ret = cst_pool_loc_descr (loc)))
16127 have_address = 1;
16128 else if (want_address == 2
16129 && tree_fits_shwi_p (loc)
16130 && (ret = address_of_int_loc_descriptor
16131 (int_size_in_bytes (TREE_TYPE (loc)),
16132 tree_to_shwi (loc))))
16133 have_address = 1;
16134 else if (tree_fits_shwi_p (loc))
16135 ret = int_loc_descriptor (tree_to_shwi (loc));
16136 else if (tree_fits_uhwi_p (loc))
16137 ret = uint_loc_descriptor (tree_to_uhwi (loc));
16138 else
16140 expansion_failed (loc, NULL_RTX,
16141 "Integer operand is not host integer");
16142 return 0;
16144 break;
16146 case CONSTRUCTOR:
16147 case REAL_CST:
16148 case STRING_CST:
16149 case COMPLEX_CST:
16150 if ((ret = cst_pool_loc_descr (loc)))
16151 have_address = 1;
16152 else if (TREE_CODE (loc) == CONSTRUCTOR)
16154 tree type = TREE_TYPE (loc);
16155 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
16156 unsigned HOST_WIDE_INT offset = 0;
16157 unsigned HOST_WIDE_INT cnt;
16158 constructor_elt *ce;
16160 if (TREE_CODE (type) == RECORD_TYPE)
16162 /* This is very limited, but it's enough to output
16163 pointers to member functions, as long as the
16164 referenced function is defined in the current
16165 translation unit. */
16166 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
16168 tree val = ce->value;
16170 tree field = ce->index;
16172 if (val)
16173 STRIP_NOPS (val);
16175 if (!field || DECL_BIT_FIELD (field))
16177 expansion_failed (loc, NULL_RTX,
16178 "bitfield in record type constructor");
16179 size = offset = (unsigned HOST_WIDE_INT)-1;
16180 ret = NULL;
16181 break;
16184 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16185 unsigned HOST_WIDE_INT pos = int_byte_position (field);
16186 gcc_assert (pos + fieldsize <= size);
16187 if (pos < offset)
16189 expansion_failed (loc, NULL_RTX,
16190 "out-of-order fields in record constructor");
16191 size = offset = (unsigned HOST_WIDE_INT)-1;
16192 ret = NULL;
16193 break;
16195 if (pos > offset)
16197 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
16198 add_loc_descr (&ret, ret1);
16199 offset = pos;
16201 if (val && fieldsize != 0)
16203 ret1 = loc_descriptor_from_tree (val, want_address, context);
16204 if (!ret1)
16206 expansion_failed (loc, NULL_RTX,
16207 "unsupported expression in field");
16208 size = offset = (unsigned HOST_WIDE_INT)-1;
16209 ret = NULL;
16210 break;
16212 add_loc_descr (&ret, ret1);
16214 if (fieldsize)
16216 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
16217 add_loc_descr (&ret, ret1);
16218 offset = pos + fieldsize;
16222 if (offset != size)
16224 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
16225 add_loc_descr (&ret, ret1);
16226 offset = size;
16229 have_address = !!want_address;
16231 else
16232 expansion_failed (loc, NULL_RTX,
16233 "constructor of non-record type");
16235 else
16236 /* We can construct small constants here using int_loc_descriptor. */
16237 expansion_failed (loc, NULL_RTX,
16238 "constructor or constant not in constant pool");
16239 break;
16241 case TRUTH_AND_EXPR:
16242 case TRUTH_ANDIF_EXPR:
16243 case BIT_AND_EXPR:
16244 op = DW_OP_and;
16245 goto do_binop;
16247 case TRUTH_XOR_EXPR:
16248 case BIT_XOR_EXPR:
16249 op = DW_OP_xor;
16250 goto do_binop;
16252 case TRUTH_OR_EXPR:
16253 case TRUTH_ORIF_EXPR:
16254 case BIT_IOR_EXPR:
16255 op = DW_OP_or;
16256 goto do_binop;
16258 case FLOOR_DIV_EXPR:
16259 case CEIL_DIV_EXPR:
16260 case ROUND_DIV_EXPR:
16261 case TRUNC_DIV_EXPR:
16262 case EXACT_DIV_EXPR:
16263 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16264 return 0;
16265 op = DW_OP_div;
16266 goto do_binop;
16268 case MINUS_EXPR:
16269 op = DW_OP_minus;
16270 goto do_binop;
16272 case FLOOR_MOD_EXPR:
16273 case CEIL_MOD_EXPR:
16274 case ROUND_MOD_EXPR:
16275 case TRUNC_MOD_EXPR:
16276 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16278 op = DW_OP_mod;
16279 goto do_binop;
16281 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16282 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16283 if (list_ret == 0 || list_ret1 == 0)
16284 return 0;
16286 add_loc_list (&list_ret, list_ret1);
16287 if (list_ret == 0)
16288 return 0;
16289 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 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_div, 0, 0));
16292 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16293 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16294 break;
16296 case MULT_EXPR:
16297 op = DW_OP_mul;
16298 goto do_binop;
16300 case LSHIFT_EXPR:
16301 op = DW_OP_shl;
16302 goto do_binop;
16304 case RSHIFT_EXPR:
16305 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16306 goto do_binop;
16308 case POINTER_PLUS_EXPR:
16309 case PLUS_EXPR:
16310 do_plus:
16311 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16313 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16314 smarter to encode their opposite. The DW_OP_plus_uconst operation
16315 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
16316 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16317 bytes, Y being the size of the operation that pushes the opposite
16318 of the addend. So let's choose the smallest representation. */
16319 const tree tree_addend = TREE_OPERAND (loc, 1);
16320 offset_int wi_addend;
16321 HOST_WIDE_INT shwi_addend;
16322 dw_loc_descr_ref loc_naddend;
16324 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16325 if (list_ret == 0)
16326 return 0;
16328 /* Try to get the literal to push. It is the opposite of the addend,
16329 so as we rely on wrapping during DWARF evaluation, first decode
16330 the literal as a "DWARF-sized" signed number. */
16331 wi_addend = wi::to_offset (tree_addend);
16332 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16333 shwi_addend = wi_addend.to_shwi ();
16334 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16335 ? int_loc_descriptor (-shwi_addend)
16336 : NULL;
16338 if (loc_naddend != NULL
16339 && ((unsigned) size_of_uleb128 (shwi_addend)
16340 > size_of_loc_descr (loc_naddend)))
16342 add_loc_descr_to_each (list_ret, loc_naddend);
16343 add_loc_descr_to_each (list_ret,
16344 new_loc_descr (DW_OP_minus, 0, 0));
16346 else
16348 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16350 loc_naddend = loc_cur;
16351 loc_cur = loc_cur->dw_loc_next;
16352 ggc_free (loc_naddend);
16354 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16356 break;
16359 op = DW_OP_plus;
16360 goto do_binop;
16362 case LE_EXPR:
16363 op = DW_OP_le;
16364 goto do_comp_binop;
16366 case GE_EXPR:
16367 op = DW_OP_ge;
16368 goto do_comp_binop;
16370 case LT_EXPR:
16371 op = DW_OP_lt;
16372 goto do_comp_binop;
16374 case GT_EXPR:
16375 op = DW_OP_gt;
16376 goto do_comp_binop;
16378 do_comp_binop:
16379 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16381 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16382 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16383 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16384 TREE_CODE (loc));
16385 break;
16387 else
16388 goto do_binop;
16390 case EQ_EXPR:
16391 op = DW_OP_eq;
16392 goto do_binop;
16394 case NE_EXPR:
16395 op = DW_OP_ne;
16396 goto do_binop;
16398 do_binop:
16399 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16400 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16401 if (list_ret == 0 || list_ret1 == 0)
16402 return 0;
16404 add_loc_list (&list_ret, list_ret1);
16405 if (list_ret == 0)
16406 return 0;
16407 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16408 break;
16410 case TRUTH_NOT_EXPR:
16411 case BIT_NOT_EXPR:
16412 op = DW_OP_not;
16413 goto do_unop;
16415 case ABS_EXPR:
16416 op = DW_OP_abs;
16417 goto do_unop;
16419 case NEGATE_EXPR:
16420 op = DW_OP_neg;
16421 goto do_unop;
16423 do_unop:
16424 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16425 if (list_ret == 0)
16426 return 0;
16428 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16429 break;
16431 case MIN_EXPR:
16432 case MAX_EXPR:
16434 const enum tree_code code =
16435 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16437 loc = build3 (COND_EXPR, TREE_TYPE (loc),
16438 build2 (code, integer_type_node,
16439 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16440 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16443 /* fall through */
16445 case COND_EXPR:
16447 dw_loc_descr_ref lhs
16448 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16449 dw_loc_list_ref rhs
16450 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16451 dw_loc_descr_ref bra_node, jump_node, tmp;
16453 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16454 if (list_ret == 0 || lhs == 0 || rhs == 0)
16455 return 0;
16457 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16458 add_loc_descr_to_each (list_ret, bra_node);
16460 add_loc_list (&list_ret, rhs);
16461 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16462 add_loc_descr_to_each (list_ret, jump_node);
16464 add_loc_descr_to_each (list_ret, lhs);
16465 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16466 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16468 /* ??? Need a node to point the skip at. Use a nop. */
16469 tmp = new_loc_descr (DW_OP_nop, 0, 0);
16470 add_loc_descr_to_each (list_ret, tmp);
16471 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16472 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16474 break;
16476 case FIX_TRUNC_EXPR:
16477 return 0;
16479 default:
16480 /* Leave front-end specific codes as simply unknown. This comes
16481 up, for instance, with the C STMT_EXPR. */
16482 if ((unsigned int) TREE_CODE (loc)
16483 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16485 expansion_failed (loc, NULL_RTX,
16486 "language specific tree node");
16487 return 0;
16490 /* Otherwise this is a generic code; we should just lists all of
16491 these explicitly. We forgot one. */
16492 if (flag_checking)
16493 gcc_unreachable ();
16495 /* In a release build, we want to degrade gracefully: better to
16496 generate incomplete debugging information than to crash. */
16497 return NULL;
16500 if (!ret && !list_ret)
16501 return 0;
16503 if (want_address == 2 && !have_address
16504 && (dwarf_version >= 4 || !dwarf_strict))
16506 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16508 expansion_failed (loc, NULL_RTX,
16509 "DWARF address size mismatch");
16510 return 0;
16512 if (ret)
16513 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16514 else
16515 add_loc_descr_to_each (list_ret,
16516 new_loc_descr (DW_OP_stack_value, 0, 0));
16517 have_address = 1;
16519 /* Show if we can't fill the request for an address. */
16520 if (want_address && !have_address)
16522 expansion_failed (loc, NULL_RTX,
16523 "Want address and only have value");
16524 return 0;
16527 gcc_assert (!ret || !list_ret);
16529 /* If we've got an address and don't want one, dereference. */
16530 if (!want_address && have_address)
16532 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16534 if (size > DWARF2_ADDR_SIZE || size == -1)
16536 expansion_failed (loc, NULL_RTX,
16537 "DWARF address size mismatch");
16538 return 0;
16540 else if (size == DWARF2_ADDR_SIZE)
16541 op = DW_OP_deref;
16542 else
16543 op = DW_OP_deref_size;
16545 if (ret)
16546 add_loc_descr (&ret, new_loc_descr (op, size, 0));
16547 else
16548 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16550 if (ret)
16551 list_ret = new_loc_list (ret, NULL, NULL, NULL);
16553 return list_ret;
16556 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16557 expressions. */
16559 static dw_loc_list_ref
16560 loc_list_from_tree (tree loc, int want_address,
16561 const struct loc_descr_context *context)
16563 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16565 for (dw_loc_list_ref loc_cur = result;
16566 loc_cur != NULL; loc_cur =
16567 loc_cur->dw_loc_next)
16568 loc_descr_without_nops (loc_cur->expr);
16569 return result;
16572 /* Same as above but return only single location expression. */
16573 static dw_loc_descr_ref
16574 loc_descriptor_from_tree (tree loc, int want_address,
16575 const struct loc_descr_context *context)
16577 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16578 if (!ret)
16579 return NULL;
16580 if (ret->dw_loc_next)
16582 expansion_failed (loc, NULL_RTX,
16583 "Location list where only loc descriptor needed");
16584 return NULL;
16586 return ret->expr;
16589 /* Given a value, round it up to the lowest multiple of `boundary'
16590 which is not less than the value itself. */
16592 static inline HOST_WIDE_INT
16593 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16595 return (((value + boundary - 1) / boundary) * boundary);
16598 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16599 pointer to the declared type for the relevant field variable, or return
16600 `integer_type_node' if the given node turns out to be an
16601 ERROR_MARK node. */
16603 static inline tree
16604 field_type (const_tree decl)
16606 tree type;
16608 if (TREE_CODE (decl) == ERROR_MARK)
16609 return integer_type_node;
16611 type = DECL_BIT_FIELD_TYPE (decl);
16612 if (type == NULL_TREE)
16613 type = TREE_TYPE (decl);
16615 return type;
16618 /* Given a pointer to a tree node, return the alignment in bits for
16619 it, or else return BITS_PER_WORD if the node actually turns out to
16620 be an ERROR_MARK node. */
16622 static inline unsigned
16623 simple_type_align_in_bits (const_tree type)
16625 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16628 static inline unsigned
16629 simple_decl_align_in_bits (const_tree decl)
16631 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16634 /* Return the result of rounding T up to ALIGN. */
16636 static inline offset_int
16637 round_up_to_align (const offset_int &t, unsigned int align)
16639 return wi::udiv_trunc (t + align - 1, align) * align;
16642 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
16643 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
16644 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
16645 if we fail to return the size in one of these two forms. */
16647 static dw_loc_descr_ref
16648 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16650 tree tree_size;
16651 struct loc_descr_context ctx;
16653 /* Return a constant integer in priority, if possible. */
16654 *cst_size = int_size_in_bytes (type);
16655 if (*cst_size != -1)
16656 return NULL;
16658 ctx.context_type = const_cast<tree> (type);
16659 ctx.base_decl = NULL_TREE;
16660 ctx.dpi = NULL;
16662 type = TYPE_MAIN_VARIANT (type);
16663 tree_size = TYPE_SIZE_UNIT (type);
16664 return ((tree_size != NULL_TREE)
16665 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16666 : NULL);
16669 /* Helper structure for RECORD_TYPE processing. */
16670 struct vlr_context
16672 /* Root RECORD_TYPE. It is needed to generate data member location
16673 descriptions in variable-length records (VLR), but also to cope with
16674 variants, which are composed of nested structures multiplexed with
16675 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
16676 function processing a FIELD_DECL, it is required to be non null. */
16677 tree struct_type;
16678 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16679 QUAL_UNION_TYPE), this holds an expression that computes the offset for
16680 this variant part as part of the root record (in storage units). For
16681 regular records, it must be NULL_TREE. */
16682 tree variant_part_offset;
16685 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16686 addressed byte of the "containing object" for the given FIELD_DECL. If
16687 possible, return a native constant through CST_OFFSET (in which case NULL is
16688 returned); otherwise return a DWARF expression that computes the offset.
16690 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16691 that offset is, either because the argument turns out to be a pointer to an
16692 ERROR_MARK node, or because the offset expression is too complex for us.
16694 CTX is required: see the comment for VLR_CONTEXT. */
16696 static dw_loc_descr_ref
16697 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16698 HOST_WIDE_INT *cst_offset)
16700 offset_int object_offset_in_bits;
16701 offset_int object_offset_in_bytes;
16702 offset_int bitpos_int;
16703 bool is_byte_offset_cst, is_bit_offset_cst;
16704 tree tree_result;
16705 dw_loc_list_ref loc_result;
16707 *cst_offset = 0;
16709 if (TREE_CODE (decl) == ERROR_MARK)
16710 return NULL;
16711 else
16712 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16714 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16715 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16717 /* We cannot handle variable bit offsets at the moment, so abort if it's the
16718 case. */
16719 if (is_bit_offset_cst)
16720 return NULL;
16722 #ifdef PCC_BITFIELD_TYPE_MATTERS
16723 /* We used to handle only constant offsets in all cases. Now, we handle
16724 properly dynamic byte offsets only when PCC bitfield type doesn't
16725 matter. */
16726 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16728 tree type;
16729 tree field_size_tree;
16730 offset_int deepest_bitpos;
16731 offset_int field_size_in_bits;
16732 unsigned int type_align_in_bits;
16733 unsigned int decl_align_in_bits;
16734 offset_int type_size_in_bits;
16736 bitpos_int = wi::to_offset (bit_position (decl));
16737 type = field_type (decl);
16738 type_size_in_bits = offset_int_type_size_in_bits (type);
16739 type_align_in_bits = simple_type_align_in_bits (type);
16741 field_size_tree = DECL_SIZE (decl);
16743 /* The size could be unspecified if there was an error, or for
16744 a flexible array member. */
16745 if (!field_size_tree)
16746 field_size_tree = bitsize_zero_node;
16748 /* If the size of the field is not constant, use the type size. */
16749 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16750 field_size_in_bits = wi::to_offset (field_size_tree);
16751 else
16752 field_size_in_bits = type_size_in_bits;
16754 decl_align_in_bits = simple_decl_align_in_bits (decl);
16756 /* The GCC front-end doesn't make any attempt to keep track of the
16757 starting bit offset (relative to the start of the containing
16758 structure type) of the hypothetical "containing object" for a
16759 bit-field. Thus, when computing the byte offset value for the
16760 start of the "containing object" of a bit-field, we must deduce
16761 this information on our own. This can be rather tricky to do in
16762 some cases. For example, handling the following structure type
16763 definition when compiling for an i386/i486 target (which only
16764 aligns long long's to 32-bit boundaries) can be very tricky:
16766 struct S { int field1; long long field2:31; };
16768 Fortunately, there is a simple rule-of-thumb which can be used
16769 in such cases. When compiling for an i386/i486, GCC will
16770 allocate 8 bytes for the structure shown above. It decides to
16771 do this based upon one simple rule for bit-field allocation.
16772 GCC allocates each "containing object" for each bit-field at
16773 the first (i.e. lowest addressed) legitimate alignment boundary
16774 (based upon the required minimum alignment for the declared
16775 type of the field) which it can possibly use, subject to the
16776 condition that there is still enough available space remaining
16777 in the containing object (when allocated at the selected point)
16778 to fully accommodate all of the bits of the bit-field itself.
16780 This simple rule makes it obvious why GCC allocates 8 bytes for
16781 each object of the structure type shown above. When looking
16782 for a place to allocate the "containing object" for `field2',
16783 the compiler simply tries to allocate a 64-bit "containing
16784 object" at each successive 32-bit boundary (starting at zero)
16785 until it finds a place to allocate that 64- bit field such that
16786 at least 31 contiguous (and previously unallocated) bits remain
16787 within that selected 64 bit field. (As it turns out, for the
16788 example above, the compiler finds it is OK to allocate the
16789 "containing object" 64-bit field at bit-offset zero within the
16790 structure type.)
16792 Here we attempt to work backwards from the limited set of facts
16793 we're given, and we try to deduce from those facts, where GCC
16794 must have believed that the containing object started (within
16795 the structure type). The value we deduce is then used (by the
16796 callers of this routine) to generate DW_AT_location and
16797 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16798 the case of DW_AT_location, regular fields as well). */
16800 /* Figure out the bit-distance from the start of the structure to
16801 the "deepest" bit of the bit-field. */
16802 deepest_bitpos = bitpos_int + field_size_in_bits;
16804 /* This is the tricky part. Use some fancy footwork to deduce
16805 where the lowest addressed bit of the containing object must
16806 be. */
16807 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16809 /* Round up to type_align by default. This works best for
16810 bitfields. */
16811 object_offset_in_bits
16812 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16814 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16816 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16818 /* Round up to decl_align instead. */
16819 object_offset_in_bits
16820 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16823 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16825 tree_result = byte_position (decl);
16826 if (ctx->variant_part_offset != NULL_TREE)
16827 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16828 ctx->variant_part_offset, tree_result));
16830 /* If the byte offset is a constant, it's simplier to handle a native
16831 constant rather than a DWARF expression. */
16832 if (TREE_CODE (tree_result) == INTEGER_CST)
16834 *cst_offset = wi::to_offset (tree_result).to_shwi ();
16835 return NULL;
16837 struct loc_descr_context loc_ctx = {
16838 ctx->struct_type, /* context_type */
16839 NULL_TREE, /* base_decl */
16840 NULL /* dpi */
16842 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16844 /* We want a DWARF expression: abort if we only have a location list with
16845 multiple elements. */
16846 if (!loc_result || !single_element_loc_list_p (loc_result))
16847 return NULL;
16848 else
16849 return loc_result->expr;
16852 /* The following routines define various Dwarf attributes and any data
16853 associated with them. */
16855 /* Add a location description attribute value to a DIE.
16857 This emits location attributes suitable for whole variables and
16858 whole parameters. Note that the location attributes for struct fields are
16859 generated by the routine `data_member_location_attribute' below. */
16861 static inline void
16862 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16863 dw_loc_list_ref descr)
16865 if (descr == 0)
16866 return;
16867 if (single_element_loc_list_p (descr))
16868 add_AT_loc (die, attr_kind, descr->expr);
16869 else
16870 add_AT_loc_list (die, attr_kind, descr);
16873 /* Add DW_AT_accessibility attribute to DIE if needed. */
16875 static void
16876 add_accessibility_attribute (dw_die_ref die, tree decl)
16878 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16879 children, otherwise the default is DW_ACCESS_public. In DWARF2
16880 the default has always been DW_ACCESS_public. */
16881 if (TREE_PROTECTED (decl))
16882 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16883 else if (TREE_PRIVATE (decl))
16885 if (dwarf_version == 2
16886 || die->die_parent == NULL
16887 || die->die_parent->die_tag != DW_TAG_class_type)
16888 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16890 else if (dwarf_version > 2
16891 && die->die_parent
16892 && die->die_parent->die_tag == DW_TAG_class_type)
16893 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16896 /* Attach the specialized form of location attribute used for data members of
16897 struct and union types. In the special case of a FIELD_DECL node which
16898 represents a bit-field, the "offset" part of this special location
16899 descriptor must indicate the distance in bytes from the lowest-addressed
16900 byte of the containing struct or union type to the lowest-addressed byte of
16901 the "containing object" for the bit-field. (See the `field_byte_offset'
16902 function above).
16904 For any given bit-field, the "containing object" is a hypothetical object
16905 (of some integral or enum type) within which the given bit-field lives. The
16906 type of this hypothetical "containing object" is always the same as the
16907 declared type of the individual bit-field itself (for GCC anyway... the
16908 DWARF spec doesn't actually mandate this). Note that it is the size (in
16909 bytes) of the hypothetical "containing object" which will be given in the
16910 DW_AT_byte_size attribute for this bit-field. (See the
16911 `byte_size_attribute' function below.) It is also used when calculating the
16912 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16913 function below.)
16915 CTX is required: see the comment for VLR_CONTEXT. */
16917 static void
16918 add_data_member_location_attribute (dw_die_ref die,
16919 tree decl,
16920 struct vlr_context *ctx)
16922 HOST_WIDE_INT offset;
16923 dw_loc_descr_ref loc_descr = 0;
16925 if (TREE_CODE (decl) == TREE_BINFO)
16927 /* We're working on the TAG_inheritance for a base class. */
16928 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16930 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16931 aren't at a fixed offset from all (sub)objects of the same
16932 type. We need to extract the appropriate offset from our
16933 vtable. The following dwarf expression means
16935 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16937 This is specific to the V3 ABI, of course. */
16939 dw_loc_descr_ref tmp;
16941 /* Make a copy of the object address. */
16942 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16943 add_loc_descr (&loc_descr, tmp);
16945 /* Extract the vtable address. */
16946 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16947 add_loc_descr (&loc_descr, tmp);
16949 /* Calculate the address of the offset. */
16950 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16951 gcc_assert (offset < 0);
16953 tmp = int_loc_descriptor (-offset);
16954 add_loc_descr (&loc_descr, tmp);
16955 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16956 add_loc_descr (&loc_descr, tmp);
16958 /* Extract the offset. */
16959 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16960 add_loc_descr (&loc_descr, tmp);
16962 /* Add it to the object address. */
16963 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16964 add_loc_descr (&loc_descr, tmp);
16966 else
16967 offset = tree_to_shwi (BINFO_OFFSET (decl));
16969 else
16971 loc_descr = field_byte_offset (decl, ctx, &offset);
16973 /* If loc_descr is available then we know the field offset is dynamic.
16974 However, GDB does not handle dynamic field offsets very well at the
16975 moment. */
16976 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16978 loc_descr = NULL;
16979 offset = 0;
16982 /* Data member location evalutation starts with the base address on the
16983 stack. Compute the field offset and add it to this base address. */
16984 else if (loc_descr != NULL)
16985 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
16988 if (! loc_descr)
16990 if (dwarf_version > 2)
16992 /* Don't need to output a location expression, just the constant. */
16993 if (offset < 0)
16994 add_AT_int (die, DW_AT_data_member_location, offset);
16995 else
16996 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16997 return;
16999 else
17001 enum dwarf_location_atom op;
17003 /* The DWARF2 standard says that we should assume that the structure
17004 address is already on the stack, so we can specify a structure
17005 field address by using DW_OP_plus_uconst. */
17006 op = DW_OP_plus_uconst;
17007 loc_descr = new_loc_descr (op, offset, 0);
17011 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17014 /* Writes integer values to dw_vec_const array. */
17016 static void
17017 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17019 while (size != 0)
17021 *dest++ = val & 0xff;
17022 val >>= 8;
17023 --size;
17027 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
17029 static HOST_WIDE_INT
17030 extract_int (const unsigned char *src, unsigned int size)
17032 HOST_WIDE_INT val = 0;
17034 src += size;
17035 while (size != 0)
17037 val <<= 8;
17038 val |= *--src & 0xff;
17039 --size;
17041 return val;
17044 /* Writes wide_int values to dw_vec_const array. */
17046 static void
17047 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
17049 int i;
17051 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
17053 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
17054 return;
17057 /* We'd have to extend this code to support odd sizes. */
17058 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
17060 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
17062 if (WORDS_BIG_ENDIAN)
17063 for (i = n - 1; i >= 0; i--)
17065 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17066 dest += sizeof (HOST_WIDE_INT);
17068 else
17069 for (i = 0; i < n; i++)
17071 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17072 dest += sizeof (HOST_WIDE_INT);
17076 /* Writes floating point values to dw_vec_const array. */
17078 static void
17079 insert_float (const_rtx rtl, unsigned char *array)
17081 long val[4];
17082 int i;
17084 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
17086 /* real_to_target puts 32-bit pieces in each long. Pack them. */
17087 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17089 insert_int (val[i], 4, array);
17090 array += 4;
17094 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17095 does not have a "location" either in memory or in a register. These
17096 things can arise in GNU C when a constant is passed as an actual parameter
17097 to an inlined function. They can also arise in C++ where declared
17098 constants do not necessarily get memory "homes". */
17100 static bool
17101 add_const_value_attribute (dw_die_ref die, rtx rtl)
17103 switch (GET_CODE (rtl))
17105 case CONST_INT:
17107 HOST_WIDE_INT val = INTVAL (rtl);
17109 if (val < 0)
17110 add_AT_int (die, DW_AT_const_value, val);
17111 else
17112 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17114 return true;
17116 case CONST_WIDE_INT:
17118 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17119 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17120 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17121 wide_int w = wi::zext (w1, prec);
17122 add_AT_wide (die, DW_AT_const_value, w);
17124 return true;
17126 case CONST_DOUBLE:
17127 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17128 floating-point constant. A CONST_DOUBLE is used whenever the
17129 constant requires more than one word in order to be adequately
17130 represented. */
17132 machine_mode mode = GET_MODE (rtl);
17134 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17135 add_AT_double (die, DW_AT_const_value,
17136 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17137 else
17139 unsigned int length = GET_MODE_SIZE (mode);
17140 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17142 insert_float (rtl, array);
17143 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17146 return true;
17148 case CONST_VECTOR:
17150 machine_mode mode = GET_MODE (rtl);
17151 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17152 unsigned int length = CONST_VECTOR_NUNITS (rtl);
17153 unsigned char *array
17154 = ggc_vec_alloc<unsigned char> (length * elt_size);
17155 unsigned int i;
17156 unsigned char *p;
17157 machine_mode imode = GET_MODE_INNER (mode);
17159 switch (GET_MODE_CLASS (mode))
17161 case MODE_VECTOR_INT:
17162 for (i = 0, p = array; i < length; i++, p += elt_size)
17164 rtx elt = CONST_VECTOR_ELT (rtl, i);
17165 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17167 break;
17169 case MODE_VECTOR_FLOAT:
17170 for (i = 0, p = array; i < length; i++, p += elt_size)
17172 rtx elt = CONST_VECTOR_ELT (rtl, i);
17173 insert_float (elt, p);
17175 break;
17177 default:
17178 gcc_unreachable ();
17181 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17183 return true;
17185 case CONST_STRING:
17186 if (dwarf_version >= 4 || !dwarf_strict)
17188 dw_loc_descr_ref loc_result;
17189 resolve_one_addr (&rtl);
17190 rtl_addr:
17191 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17192 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17193 add_AT_loc (die, DW_AT_location, loc_result);
17194 vec_safe_push (used_rtx_array, rtl);
17195 return true;
17197 return false;
17199 case CONST:
17200 if (CONSTANT_P (XEXP (rtl, 0)))
17201 return add_const_value_attribute (die, XEXP (rtl, 0));
17202 /* FALLTHROUGH */
17203 case SYMBOL_REF:
17204 if (!const_ok_for_output (rtl))
17205 return false;
17206 case LABEL_REF:
17207 if (dwarf_version >= 4 || !dwarf_strict)
17208 goto rtl_addr;
17209 return false;
17211 case PLUS:
17212 /* In cases where an inlined instance of an inline function is passed
17213 the address of an `auto' variable (which is local to the caller) we
17214 can get a situation where the DECL_RTL of the artificial local
17215 variable (for the inlining) which acts as a stand-in for the
17216 corresponding formal parameter (of the inline function) will look
17217 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
17218 exactly a compile-time constant expression, but it isn't the address
17219 of the (artificial) local variable either. Rather, it represents the
17220 *value* which the artificial local variable always has during its
17221 lifetime. We currently have no way to represent such quasi-constant
17222 values in Dwarf, so for now we just punt and generate nothing. */
17223 return false;
17225 case HIGH:
17226 case CONST_FIXED:
17227 return false;
17229 case MEM:
17230 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17231 && MEM_READONLY_P (rtl)
17232 && GET_MODE (rtl) == BLKmode)
17234 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17235 return true;
17237 return false;
17239 default:
17240 /* No other kinds of rtx should be possible here. */
17241 gcc_unreachable ();
17243 return false;
17246 /* Determine whether the evaluation of EXPR references any variables
17247 or functions which aren't otherwise used (and therefore may not be
17248 output). */
17249 static tree
17250 reference_to_unused (tree * tp, int * walk_subtrees,
17251 void * data ATTRIBUTE_UNUSED)
17253 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17254 *walk_subtrees = 0;
17256 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17257 && ! TREE_ASM_WRITTEN (*tp))
17258 return *tp;
17259 /* ??? The C++ FE emits debug information for using decls, so
17260 putting gcc_unreachable here falls over. See PR31899. For now
17261 be conservative. */
17262 else if (!symtab->global_info_ready
17263 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17264 return *tp;
17265 else if (TREE_CODE (*tp) == VAR_DECL)
17267 varpool_node *node = varpool_node::get (*tp);
17268 if (!node || !node->definition)
17269 return *tp;
17271 else if (TREE_CODE (*tp) == FUNCTION_DECL
17272 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17274 /* The call graph machinery must have finished analyzing,
17275 optimizing and gimplifying the CU by now.
17276 So if *TP has no call graph node associated
17277 to it, it means *TP will not be emitted. */
17278 if (!cgraph_node::get (*tp))
17279 return *tp;
17281 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17282 return *tp;
17284 return NULL_TREE;
17287 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17288 for use in a later add_const_value_attribute call. */
17290 static rtx
17291 rtl_for_decl_init (tree init, tree type)
17293 rtx rtl = NULL_RTX;
17295 STRIP_NOPS (init);
17297 /* If a variable is initialized with a string constant without embedded
17298 zeros, build CONST_STRING. */
17299 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17301 tree enttype = TREE_TYPE (type);
17302 tree domain = TYPE_DOMAIN (type);
17303 machine_mode mode = TYPE_MODE (enttype);
17305 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17306 && domain
17307 && integer_zerop (TYPE_MIN_VALUE (domain))
17308 && compare_tree_int (TYPE_MAX_VALUE (domain),
17309 TREE_STRING_LENGTH (init) - 1) == 0
17310 && ((size_t) TREE_STRING_LENGTH (init)
17311 == strlen (TREE_STRING_POINTER (init)) + 1))
17313 rtl = gen_rtx_CONST_STRING (VOIDmode,
17314 ggc_strdup (TREE_STRING_POINTER (init)));
17315 rtl = gen_rtx_MEM (BLKmode, rtl);
17316 MEM_READONLY_P (rtl) = 1;
17319 /* Other aggregates, and complex values, could be represented using
17320 CONCAT: FIXME! */
17321 else if (AGGREGATE_TYPE_P (type)
17322 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17323 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17324 || TREE_CODE (type) == COMPLEX_TYPE)
17326 /* Vectors only work if their mode is supported by the target.
17327 FIXME: generic vectors ought to work too. */
17328 else if (TREE_CODE (type) == VECTOR_TYPE
17329 && !VECTOR_MODE_P (TYPE_MODE (type)))
17331 /* If the initializer is something that we know will expand into an
17332 immediate RTL constant, expand it now. We must be careful not to
17333 reference variables which won't be output. */
17334 else if (initializer_constant_valid_p (init, type)
17335 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17337 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17338 possible. */
17339 if (TREE_CODE (type) == VECTOR_TYPE)
17340 switch (TREE_CODE (init))
17342 case VECTOR_CST:
17343 break;
17344 case CONSTRUCTOR:
17345 if (TREE_CONSTANT (init))
17347 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17348 bool constant_p = true;
17349 tree value;
17350 unsigned HOST_WIDE_INT ix;
17352 /* Even when ctor is constant, it might contain non-*_CST
17353 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17354 belong into VECTOR_CST nodes. */
17355 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17356 if (!CONSTANT_CLASS_P (value))
17358 constant_p = false;
17359 break;
17362 if (constant_p)
17364 init = build_vector_from_ctor (type, elts);
17365 break;
17368 /* FALLTHRU */
17370 default:
17371 return NULL;
17374 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17376 /* If expand_expr returns a MEM, it wasn't immediate. */
17377 gcc_assert (!rtl || !MEM_P (rtl));
17380 return rtl;
17383 /* Generate RTL for the variable DECL to represent its location. */
17385 static rtx
17386 rtl_for_decl_location (tree decl)
17388 rtx rtl;
17390 /* Here we have to decide where we are going to say the parameter "lives"
17391 (as far as the debugger is concerned). We only have a couple of
17392 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17394 DECL_RTL normally indicates where the parameter lives during most of the
17395 activation of the function. If optimization is enabled however, this
17396 could be either NULL or else a pseudo-reg. Both of those cases indicate
17397 that the parameter doesn't really live anywhere (as far as the code
17398 generation parts of GCC are concerned) during most of the function's
17399 activation. That will happen (for example) if the parameter is never
17400 referenced within the function.
17402 We could just generate a location descriptor here for all non-NULL
17403 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17404 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17405 where DECL_RTL is NULL or is a pseudo-reg.
17407 Note however that we can only get away with using DECL_INCOMING_RTL as
17408 a backup substitute for DECL_RTL in certain limited cases. In cases
17409 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17410 we can be sure that the parameter was passed using the same type as it is
17411 declared to have within the function, and that its DECL_INCOMING_RTL
17412 points us to a place where a value of that type is passed.
17414 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17415 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17416 because in these cases DECL_INCOMING_RTL points us to a value of some
17417 type which is *different* from the type of the parameter itself. Thus,
17418 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17419 such cases, the debugger would end up (for example) trying to fetch a
17420 `float' from a place which actually contains the first part of a
17421 `double'. That would lead to really incorrect and confusing
17422 output at debug-time.
17424 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17425 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
17426 are a couple of exceptions however. On little-endian machines we can
17427 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17428 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17429 an integral type that is smaller than TREE_TYPE (decl). These cases arise
17430 when (on a little-endian machine) a non-prototyped function has a
17431 parameter declared to be of type `short' or `char'. In such cases,
17432 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17433 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17434 passed `int' value. If the debugger then uses that address to fetch
17435 a `short' or a `char' (on a little-endian machine) the result will be
17436 the correct data, so we allow for such exceptional cases below.
17438 Note that our goal here is to describe the place where the given formal
17439 parameter lives during most of the function's activation (i.e. between the
17440 end of the prologue and the start of the epilogue). We'll do that as best
17441 as we can. Note however that if the given formal parameter is modified
17442 sometime during the execution of the function, then a stack backtrace (at
17443 debug-time) will show the function as having been called with the *new*
17444 value rather than the value which was originally passed in. This happens
17445 rarely enough that it is not a major problem, but it *is* a problem, and
17446 I'd like to fix it.
17448 A future version of dwarf2out.c may generate two additional attributes for
17449 any given DW_TAG_formal_parameter DIE which will describe the "passed
17450 type" and the "passed location" for the given formal parameter in addition
17451 to the attributes we now generate to indicate the "declared type" and the
17452 "active location" for each parameter. This additional set of attributes
17453 could be used by debuggers for stack backtraces. Separately, note that
17454 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17455 This happens (for example) for inlined-instances of inline function formal
17456 parameters which are never referenced. This really shouldn't be
17457 happening. All PARM_DECL nodes should get valid non-NULL
17458 DECL_INCOMING_RTL values. FIXME. */
17460 /* Use DECL_RTL as the "location" unless we find something better. */
17461 rtl = DECL_RTL_IF_SET (decl);
17463 /* When generating abstract instances, ignore everything except
17464 constants, symbols living in memory, and symbols living in
17465 fixed registers. */
17466 if (! reload_completed)
17468 if (rtl
17469 && (CONSTANT_P (rtl)
17470 || (MEM_P (rtl)
17471 && CONSTANT_P (XEXP (rtl, 0)))
17472 || (REG_P (rtl)
17473 && TREE_CODE (decl) == VAR_DECL
17474 && TREE_STATIC (decl))))
17476 rtl = targetm.delegitimize_address (rtl);
17477 return rtl;
17479 rtl = NULL_RTX;
17481 else if (TREE_CODE (decl) == PARM_DECL)
17483 if (rtl == NULL_RTX
17484 || is_pseudo_reg (rtl)
17485 || (MEM_P (rtl)
17486 && is_pseudo_reg (XEXP (rtl, 0))
17487 && DECL_INCOMING_RTL (decl)
17488 && MEM_P (DECL_INCOMING_RTL (decl))
17489 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17491 tree declared_type = TREE_TYPE (decl);
17492 tree passed_type = DECL_ARG_TYPE (decl);
17493 machine_mode dmode = TYPE_MODE (declared_type);
17494 machine_mode pmode = TYPE_MODE (passed_type);
17496 /* This decl represents a formal parameter which was optimized out.
17497 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17498 all cases where (rtl == NULL_RTX) just below. */
17499 if (dmode == pmode)
17500 rtl = DECL_INCOMING_RTL (decl);
17501 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17502 && SCALAR_INT_MODE_P (dmode)
17503 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17504 && DECL_INCOMING_RTL (decl))
17506 rtx inc = DECL_INCOMING_RTL (decl);
17507 if (REG_P (inc))
17508 rtl = inc;
17509 else if (MEM_P (inc))
17511 if (BYTES_BIG_ENDIAN)
17512 rtl = adjust_address_nv (inc, dmode,
17513 GET_MODE_SIZE (pmode)
17514 - GET_MODE_SIZE (dmode));
17515 else
17516 rtl = inc;
17521 /* If the parm was passed in registers, but lives on the stack, then
17522 make a big endian correction if the mode of the type of the
17523 parameter is not the same as the mode of the rtl. */
17524 /* ??? This is the same series of checks that are made in dbxout.c before
17525 we reach the big endian correction code there. It isn't clear if all
17526 of these checks are necessary here, but keeping them all is the safe
17527 thing to do. */
17528 else if (MEM_P (rtl)
17529 && XEXP (rtl, 0) != const0_rtx
17530 && ! CONSTANT_P (XEXP (rtl, 0))
17531 /* Not passed in memory. */
17532 && !MEM_P (DECL_INCOMING_RTL (decl))
17533 /* Not passed by invisible reference. */
17534 && (!REG_P (XEXP (rtl, 0))
17535 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17536 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17537 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17538 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17539 #endif
17541 /* Big endian correction check. */
17542 && BYTES_BIG_ENDIAN
17543 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17544 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17545 < UNITS_PER_WORD))
17547 machine_mode addr_mode = get_address_mode (rtl);
17548 int offset = (UNITS_PER_WORD
17549 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17551 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17552 plus_constant (addr_mode, XEXP (rtl, 0), offset));
17555 else if (TREE_CODE (decl) == VAR_DECL
17556 && rtl
17557 && MEM_P (rtl)
17558 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17559 && BYTES_BIG_ENDIAN)
17561 machine_mode addr_mode = get_address_mode (rtl);
17562 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17563 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17565 /* If a variable is declared "register" yet is smaller than
17566 a register, then if we store the variable to memory, it
17567 looks like we're storing a register-sized value, when in
17568 fact we are not. We need to adjust the offset of the
17569 storage location to reflect the actual value's bytes,
17570 else gdb will not be able to display it. */
17571 if (rsize > dsize)
17572 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17573 plus_constant (addr_mode, XEXP (rtl, 0),
17574 rsize - dsize));
17577 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17578 and will have been substituted directly into all expressions that use it.
17579 C does not have such a concept, but C++ and other languages do. */
17580 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17581 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17583 if (rtl)
17584 rtl = targetm.delegitimize_address (rtl);
17586 /* If we don't look past the constant pool, we risk emitting a
17587 reference to a constant pool entry that isn't referenced from
17588 code, and thus is not emitted. */
17589 if (rtl)
17590 rtl = avoid_constant_pool_reference (rtl);
17592 /* Try harder to get a rtl. If this symbol ends up not being emitted
17593 in the current CU, resolve_addr will remove the expression referencing
17594 it. */
17595 if (rtl == NULL_RTX
17596 && TREE_CODE (decl) == VAR_DECL
17597 && !DECL_EXTERNAL (decl)
17598 && TREE_STATIC (decl)
17599 && DECL_NAME (decl)
17600 && !DECL_HARD_REGISTER (decl)
17601 && DECL_MODE (decl) != VOIDmode)
17603 rtl = make_decl_rtl_for_debug (decl);
17604 if (!MEM_P (rtl)
17605 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17606 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17607 rtl = NULL_RTX;
17610 return rtl;
17613 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
17614 returned. If so, the decl for the COMMON block is returned, and the
17615 value is the offset into the common block for the symbol. */
17617 static tree
17618 fortran_common (tree decl, HOST_WIDE_INT *value)
17620 tree val_expr, cvar;
17621 machine_mode mode;
17622 HOST_WIDE_INT bitsize, bitpos;
17623 tree offset;
17624 int unsignedp, reversep, volatilep = 0;
17626 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17627 it does not have a value (the offset into the common area), or if it
17628 is thread local (as opposed to global) then it isn't common, and shouldn't
17629 be handled as such. */
17630 if (TREE_CODE (decl) != VAR_DECL
17631 || !TREE_STATIC (decl)
17632 || !DECL_HAS_VALUE_EXPR_P (decl)
17633 || !is_fortran ())
17634 return NULL_TREE;
17636 val_expr = DECL_VALUE_EXPR (decl);
17637 if (TREE_CODE (val_expr) != COMPONENT_REF)
17638 return NULL_TREE;
17640 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17641 &unsignedp, &reversep, &volatilep);
17643 if (cvar == NULL_TREE
17644 || TREE_CODE (cvar) != VAR_DECL
17645 || DECL_ARTIFICIAL (cvar)
17646 || !TREE_PUBLIC (cvar))
17647 return NULL_TREE;
17649 *value = 0;
17650 if (offset != NULL)
17652 if (!tree_fits_shwi_p (offset))
17653 return NULL_TREE;
17654 *value = tree_to_shwi (offset);
17656 if (bitpos != 0)
17657 *value += bitpos / BITS_PER_UNIT;
17659 return cvar;
17662 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17663 data attribute for a variable or a parameter. We generate the
17664 DW_AT_const_value attribute only in those cases where the given variable
17665 or parameter does not have a true "location" either in memory or in a
17666 register. This can happen (for example) when a constant is passed as an
17667 actual argument in a call to an inline function. (It's possible that
17668 these things can crop up in other ways also.) Note that one type of
17669 constant value which can be passed into an inlined function is a constant
17670 pointer. This can happen for example if an actual argument in an inlined
17671 function call evaluates to a compile-time constant address.
17673 CACHE_P is true if it is worth caching the location list for DECL,
17674 so that future calls can reuse it rather than regenerate it from scratch.
17675 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17676 since we will need to refer to them each time the function is inlined. */
17678 static bool
17679 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17681 rtx rtl;
17682 dw_loc_list_ref list;
17683 var_loc_list *loc_list;
17684 cached_dw_loc_list *cache;
17686 if (early_dwarf)
17687 return false;
17689 if (TREE_CODE (decl) == ERROR_MARK)
17690 return false;
17692 if (get_AT (die, DW_AT_location)
17693 || get_AT (die, DW_AT_const_value))
17694 return true;
17696 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17697 || TREE_CODE (decl) == RESULT_DECL);
17699 /* Try to get some constant RTL for this decl, and use that as the value of
17700 the location. */
17702 rtl = rtl_for_decl_location (decl);
17703 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17704 && add_const_value_attribute (die, rtl))
17705 return true;
17707 /* See if we have single element location list that is equivalent to
17708 a constant value. That way we are better to use add_const_value_attribute
17709 rather than expanding constant value equivalent. */
17710 loc_list = lookup_decl_loc (decl);
17711 if (loc_list
17712 && loc_list->first
17713 && loc_list->first->next == NULL
17714 && NOTE_P (loc_list->first->loc)
17715 && NOTE_VAR_LOCATION (loc_list->first->loc)
17716 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17718 struct var_loc_node *node;
17720 node = loc_list->first;
17721 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17722 if (GET_CODE (rtl) == EXPR_LIST)
17723 rtl = XEXP (rtl, 0);
17724 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17725 && add_const_value_attribute (die, rtl))
17726 return true;
17728 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17729 list several times. See if we've already cached the contents. */
17730 list = NULL;
17731 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17732 cache_p = false;
17733 if (cache_p)
17735 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17736 if (cache)
17737 list = cache->loc_list;
17739 if (list == NULL)
17741 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17742 NULL);
17743 /* It is usually worth caching this result if the decl is from
17744 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17745 if (cache_p && list && list->dw_loc_next)
17747 cached_dw_loc_list **slot
17748 = cached_dw_loc_list_table->find_slot_with_hash (decl,
17749 DECL_UID (decl),
17750 INSERT);
17751 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17752 cache->decl_id = DECL_UID (decl);
17753 cache->loc_list = list;
17754 *slot = cache;
17757 if (list)
17759 add_AT_location_description (die, DW_AT_location, list);
17760 return true;
17762 /* None of that worked, so it must not really have a location;
17763 try adding a constant value attribute from the DECL_INITIAL. */
17764 return tree_add_const_value_attribute_for_decl (die, decl);
17767 /* Helper function for tree_add_const_value_attribute. Natively encode
17768 initializer INIT into an array. Return true if successful. */
17770 static bool
17771 native_encode_initializer (tree init, unsigned char *array, int size)
17773 tree type;
17775 if (init == NULL_TREE)
17776 return false;
17778 STRIP_NOPS (init);
17779 switch (TREE_CODE (init))
17781 case STRING_CST:
17782 type = TREE_TYPE (init);
17783 if (TREE_CODE (type) == ARRAY_TYPE)
17785 tree enttype = TREE_TYPE (type);
17786 machine_mode mode = TYPE_MODE (enttype);
17788 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17789 return false;
17790 if (int_size_in_bytes (type) != size)
17791 return false;
17792 if (size > TREE_STRING_LENGTH (init))
17794 memcpy (array, TREE_STRING_POINTER (init),
17795 TREE_STRING_LENGTH (init));
17796 memset (array + TREE_STRING_LENGTH (init),
17797 '\0', size - TREE_STRING_LENGTH (init));
17799 else
17800 memcpy (array, TREE_STRING_POINTER (init), size);
17801 return true;
17803 return false;
17804 case CONSTRUCTOR:
17805 type = TREE_TYPE (init);
17806 if (int_size_in_bytes (type) != size)
17807 return false;
17808 if (TREE_CODE (type) == ARRAY_TYPE)
17810 HOST_WIDE_INT min_index;
17811 unsigned HOST_WIDE_INT cnt;
17812 int curpos = 0, fieldsize;
17813 constructor_elt *ce;
17815 if (TYPE_DOMAIN (type) == NULL_TREE
17816 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17817 return false;
17819 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17820 if (fieldsize <= 0)
17821 return false;
17823 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17824 memset (array, '\0', size);
17825 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17827 tree val = ce->value;
17828 tree index = ce->index;
17829 int pos = curpos;
17830 if (index && TREE_CODE (index) == RANGE_EXPR)
17831 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17832 * fieldsize;
17833 else if (index)
17834 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17836 if (val)
17838 STRIP_NOPS (val);
17839 if (!native_encode_initializer (val, array + pos, fieldsize))
17840 return false;
17842 curpos = pos + fieldsize;
17843 if (index && TREE_CODE (index) == RANGE_EXPR)
17845 int count = tree_to_shwi (TREE_OPERAND (index, 1))
17846 - tree_to_shwi (TREE_OPERAND (index, 0));
17847 while (count-- > 0)
17849 if (val)
17850 memcpy (array + curpos, array + pos, fieldsize);
17851 curpos += fieldsize;
17854 gcc_assert (curpos <= size);
17856 return true;
17858 else if (TREE_CODE (type) == RECORD_TYPE
17859 || TREE_CODE (type) == UNION_TYPE)
17861 tree field = NULL_TREE;
17862 unsigned HOST_WIDE_INT cnt;
17863 constructor_elt *ce;
17865 if (int_size_in_bytes (type) != size)
17866 return false;
17868 if (TREE_CODE (type) == RECORD_TYPE)
17869 field = TYPE_FIELDS (type);
17871 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17873 tree val = ce->value;
17874 int pos, fieldsize;
17876 if (ce->index != 0)
17877 field = ce->index;
17879 if (val)
17880 STRIP_NOPS (val);
17882 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17883 return false;
17885 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17886 && TYPE_DOMAIN (TREE_TYPE (field))
17887 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17888 return false;
17889 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17890 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17891 return false;
17892 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17893 pos = int_byte_position (field);
17894 gcc_assert (pos + fieldsize <= size);
17895 if (val && fieldsize != 0
17896 && !native_encode_initializer (val, array + pos, fieldsize))
17897 return false;
17899 return true;
17901 return false;
17902 case VIEW_CONVERT_EXPR:
17903 case NON_LVALUE_EXPR:
17904 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17905 default:
17906 return native_encode_expr (init, array, size) == size;
17910 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17911 attribute is the const value T. */
17913 static bool
17914 tree_add_const_value_attribute (dw_die_ref die, tree t)
17916 tree init;
17917 tree type = TREE_TYPE (t);
17918 rtx rtl;
17920 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17921 return false;
17923 init = t;
17924 gcc_assert (!DECL_P (init));
17926 rtl = rtl_for_decl_init (init, type);
17927 if (rtl)
17928 return add_const_value_attribute (die, rtl);
17929 /* If the host and target are sane, try harder. */
17930 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17931 && initializer_constant_valid_p (init, type))
17933 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17934 if (size > 0 && (int) size == size)
17936 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17938 if (native_encode_initializer (init, array, size))
17940 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17941 return true;
17943 ggc_free (array);
17946 return false;
17949 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17950 attribute is the const value of T, where T is an integral constant
17951 variable with static storage duration
17952 (so it can't be a PARM_DECL or a RESULT_DECL). */
17954 static bool
17955 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17958 if (!decl
17959 || (TREE_CODE (decl) != VAR_DECL
17960 && TREE_CODE (decl) != CONST_DECL)
17961 || (TREE_CODE (decl) == VAR_DECL
17962 && !TREE_STATIC (decl)))
17963 return false;
17965 if (TREE_READONLY (decl)
17966 && ! TREE_THIS_VOLATILE (decl)
17967 && DECL_INITIAL (decl))
17968 /* OK */;
17969 else
17970 return false;
17972 /* Don't add DW_AT_const_value if abstract origin already has one. */
17973 if (get_AT (var_die, DW_AT_const_value))
17974 return false;
17976 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17979 /* Convert the CFI instructions for the current function into a
17980 location list. This is used for DW_AT_frame_base when we targeting
17981 a dwarf2 consumer that does not support the dwarf3
17982 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17983 expressions. */
17985 static dw_loc_list_ref
17986 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17988 int ix;
17989 dw_fde_ref fde;
17990 dw_loc_list_ref list, *list_tail;
17991 dw_cfi_ref cfi;
17992 dw_cfa_location last_cfa, next_cfa;
17993 const char *start_label, *last_label, *section;
17994 dw_cfa_location remember;
17996 fde = cfun->fde;
17997 gcc_assert (fde != NULL);
17999 section = secname_for_decl (current_function_decl);
18000 list_tail = &list;
18001 list = NULL;
18003 memset (&next_cfa, 0, sizeof (next_cfa));
18004 next_cfa.reg = INVALID_REGNUM;
18005 remember = next_cfa;
18007 start_label = fde->dw_fde_begin;
18009 /* ??? Bald assumption that the CIE opcode list does not contain
18010 advance opcodes. */
18011 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
18012 lookup_cfa_1 (cfi, &next_cfa, &remember);
18014 last_cfa = next_cfa;
18015 last_label = start_label;
18017 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18019 /* If the first partition contained no CFI adjustments, the
18020 CIE opcodes apply to the whole first partition. */
18021 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18022 fde->dw_fde_begin, fde->dw_fde_end, section);
18023 list_tail =&(*list_tail)->dw_loc_next;
18024 start_label = last_label = fde->dw_fde_second_begin;
18027 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
18029 switch (cfi->dw_cfi_opc)
18031 case DW_CFA_set_loc:
18032 case DW_CFA_advance_loc1:
18033 case DW_CFA_advance_loc2:
18034 case DW_CFA_advance_loc4:
18035 if (!cfa_equal_p (&last_cfa, &next_cfa))
18037 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18038 start_label, last_label, section);
18040 list_tail = &(*list_tail)->dw_loc_next;
18041 last_cfa = next_cfa;
18042 start_label = last_label;
18044 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18045 break;
18047 case DW_CFA_advance_loc:
18048 /* The encoding is complex enough that we should never emit this. */
18049 gcc_unreachable ();
18051 default:
18052 lookup_cfa_1 (cfi, &next_cfa, &remember);
18053 break;
18055 if (ix + 1 == fde->dw_fde_switch_cfi_index)
18057 if (!cfa_equal_p (&last_cfa, &next_cfa))
18059 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18060 start_label, last_label, section);
18062 list_tail = &(*list_tail)->dw_loc_next;
18063 last_cfa = next_cfa;
18064 start_label = last_label;
18066 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18067 start_label, fde->dw_fde_end, section);
18068 list_tail = &(*list_tail)->dw_loc_next;
18069 start_label = last_label = fde->dw_fde_second_begin;
18073 if (!cfa_equal_p (&last_cfa, &next_cfa))
18075 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18076 start_label, last_label, section);
18077 list_tail = &(*list_tail)->dw_loc_next;
18078 start_label = last_label;
18081 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18082 start_label,
18083 fde->dw_fde_second_begin
18084 ? fde->dw_fde_second_end : fde->dw_fde_end,
18085 section);
18087 if (list && list->dw_loc_next)
18088 gen_llsym (list);
18090 return list;
18093 /* Compute a displacement from the "steady-state frame pointer" to the
18094 frame base (often the same as the CFA), and store it in
18095 frame_pointer_fb_offset. OFFSET is added to the displacement
18096 before the latter is negated. */
18098 static void
18099 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18101 rtx reg, elim;
18103 #ifdef FRAME_POINTER_CFA_OFFSET
18104 reg = frame_pointer_rtx;
18105 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18106 #else
18107 reg = arg_pointer_rtx;
18108 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18109 #endif
18111 elim = (ira_use_lra_p
18112 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18113 : eliminate_regs (reg, VOIDmode, NULL_RTX));
18114 if (GET_CODE (elim) == PLUS)
18116 offset += INTVAL (XEXP (elim, 1));
18117 elim = XEXP (elim, 0);
18120 frame_pointer_fb_offset = -offset;
18122 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18123 in which to eliminate. This is because it's stack pointer isn't
18124 directly accessible as a register within the ISA. To work around
18125 this, assume that while we cannot provide a proper value for
18126 frame_pointer_fb_offset, we won't need one either. */
18127 frame_pointer_fb_offset_valid
18128 = ((SUPPORTS_STACK_ALIGNMENT
18129 && (elim == hard_frame_pointer_rtx
18130 || elim == stack_pointer_rtx))
18131 || elim == (frame_pointer_needed
18132 ? hard_frame_pointer_rtx
18133 : stack_pointer_rtx));
18136 /* Generate a DW_AT_name attribute given some string value to be included as
18137 the value of the attribute. */
18139 static void
18140 add_name_attribute (dw_die_ref die, const char *name_string)
18142 if (name_string != NULL && *name_string != 0)
18144 if (demangle_name_func)
18145 name_string = (*demangle_name_func) (name_string);
18147 add_AT_string (die, DW_AT_name, name_string);
18151 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18152 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18153 of TYPE accordingly.
18155 ??? This is a temporary measure until after we're able to generate
18156 regular DWARF for the complex Ada type system. */
18158 static void
18159 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18160 dw_die_ref context_die)
18162 tree dtype;
18163 dw_die_ref dtype_die;
18165 if (!lang_hooks.types.descriptive_type)
18166 return;
18168 dtype = lang_hooks.types.descriptive_type (type);
18169 if (!dtype)
18170 return;
18172 dtype_die = lookup_type_die (dtype);
18173 if (!dtype_die)
18175 gen_type_die (dtype, context_die);
18176 dtype_die = lookup_type_die (dtype);
18177 gcc_assert (dtype_die);
18180 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18183 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
18185 static const char *
18186 comp_dir_string (void)
18188 const char *wd;
18189 char *wd1;
18190 static const char *cached_wd = NULL;
18192 if (cached_wd != NULL)
18193 return cached_wd;
18195 wd = get_src_pwd ();
18196 if (wd == NULL)
18197 return NULL;
18199 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18201 int wdlen;
18203 wdlen = strlen (wd);
18204 wd1 = ggc_vec_alloc<char> (wdlen + 2);
18205 strcpy (wd1, wd);
18206 wd1 [wdlen] = DIR_SEPARATOR;
18207 wd1 [wdlen + 1] = 0;
18208 wd = wd1;
18211 cached_wd = remap_debug_filename (wd);
18212 return cached_wd;
18215 /* Generate a DW_AT_comp_dir attribute for DIE. */
18217 static void
18218 add_comp_dir_attribute (dw_die_ref die)
18220 const char * wd = comp_dir_string ();
18221 if (wd != NULL)
18222 add_AT_string (die, DW_AT_comp_dir, wd);
18225 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18226 pointer computation, ...), output a representation for that bound according
18227 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
18228 loc_list_from_tree for the meaning of CONTEXT. */
18230 static void
18231 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18232 int forms, const struct loc_descr_context *context)
18234 dw_die_ref context_die, decl_die;
18235 dw_loc_list_ref list;
18237 bool strip_conversions = true;
18239 while (strip_conversions)
18240 switch (TREE_CODE (value))
18242 case ERROR_MARK:
18243 case SAVE_EXPR:
18244 return;
18246 CASE_CONVERT:
18247 case VIEW_CONVERT_EXPR:
18248 value = TREE_OPERAND (value, 0);
18249 break;
18251 default:
18252 strip_conversions = false;
18253 break;
18256 /* If possible and permitted, output the attribute as a constant. */
18257 if ((forms & dw_scalar_form_constant) != 0
18258 && TREE_CODE (value) == INTEGER_CST)
18260 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18262 /* If HOST_WIDE_INT is big enough then represent the bound as
18263 a constant value. We need to choose a form based on
18264 whether the type is signed or unsigned. We cannot just
18265 call add_AT_unsigned if the value itself is positive
18266 (add_AT_unsigned might add the unsigned value encoded as
18267 DW_FORM_data[1248]). Some DWARF consumers will lookup the
18268 bounds type and then sign extend any unsigned values found
18269 for signed types. This is needed only for
18270 DW_AT_{lower,upper}_bound, since for most other attributes,
18271 consumers will treat DW_FORM_data[1248] as unsigned values,
18272 regardless of the underlying type. */
18273 if (prec <= HOST_BITS_PER_WIDE_INT
18274 || tree_fits_uhwi_p (value))
18276 if (TYPE_UNSIGNED (TREE_TYPE (value)))
18277 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18278 else
18279 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18281 else
18282 /* Otherwise represent the bound as an unsigned value with
18283 the precision of its type. The precision and signedness
18284 of the type will be necessary to re-interpret it
18285 unambiguously. */
18286 add_AT_wide (die, attr, value);
18287 return;
18290 /* Otherwise, if it's possible and permitted too, output a reference to
18291 another DIE. */
18292 if ((forms & dw_scalar_form_reference) != 0)
18294 tree decl = NULL_TREE;
18296 /* Some type attributes reference an outer type. For instance, the upper
18297 bound of an array may reference an embedding record (this happens in
18298 Ada). */
18299 if (TREE_CODE (value) == COMPONENT_REF
18300 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18301 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18302 decl = TREE_OPERAND (value, 1);
18304 else if (TREE_CODE (value) == VAR_DECL
18305 || TREE_CODE (value) == PARM_DECL
18306 || TREE_CODE (value) == RESULT_DECL)
18307 decl = value;
18309 if (decl != NULL_TREE)
18311 dw_die_ref decl_die = lookup_decl_die (decl);
18313 /* ??? Can this happen, or should the variable have been bound
18314 first? Probably it can, since I imagine that we try to create
18315 the types of parameters in the order in which they exist in
18316 the list, and won't have created a forward reference to a
18317 later parameter. */
18318 if (decl_die != NULL)
18320 add_AT_die_ref (die, attr, decl_die);
18321 return;
18326 /* Last chance: try to create a stack operation procedure to evaluate the
18327 value. Do nothing if even that is not possible or permitted. */
18328 if ((forms & dw_scalar_form_exprloc) == 0)
18329 return;
18331 list = loc_list_from_tree (value, 2, context);
18332 if (list == NULL || single_element_loc_list_p (list))
18334 /* If this attribute is not a reference nor constant, it is
18335 a DWARF expression rather than location description. For that
18336 loc_list_from_tree (value, 0, &context) is needed. */
18337 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18338 if (list2 && single_element_loc_list_p (list2))
18340 add_AT_loc (die, attr, list2->expr);
18341 return;
18345 /* If that failed to give a single element location list, fall back to
18346 outputting this as a reference... still if permitted. */
18347 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18348 return;
18350 if (current_function_decl == 0)
18351 context_die = comp_unit_die ();
18352 else
18353 context_die = lookup_decl_die (current_function_decl);
18355 decl_die = new_die (DW_TAG_variable, context_die, value);
18356 add_AT_flag (decl_die, DW_AT_artificial, 1);
18357 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18358 context_die);
18359 add_AT_location_description (decl_die, DW_AT_location, list);
18360 add_AT_die_ref (die, attr, decl_die);
18363 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18364 default. */
18366 static int
18367 lower_bound_default (void)
18369 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18371 case DW_LANG_C:
18372 case DW_LANG_C89:
18373 case DW_LANG_C99:
18374 case DW_LANG_C11:
18375 case DW_LANG_C_plus_plus:
18376 case DW_LANG_C_plus_plus_11:
18377 case DW_LANG_C_plus_plus_14:
18378 case DW_LANG_ObjC:
18379 case DW_LANG_ObjC_plus_plus:
18380 case DW_LANG_Java:
18381 return 0;
18382 case DW_LANG_Fortran77:
18383 case DW_LANG_Fortran90:
18384 case DW_LANG_Fortran95:
18385 case DW_LANG_Fortran03:
18386 case DW_LANG_Fortran08:
18387 return 1;
18388 case DW_LANG_UPC:
18389 case DW_LANG_D:
18390 case DW_LANG_Python:
18391 return dwarf_version >= 4 ? 0 : -1;
18392 case DW_LANG_Ada95:
18393 case DW_LANG_Ada83:
18394 case DW_LANG_Cobol74:
18395 case DW_LANG_Cobol85:
18396 case DW_LANG_Pascal83:
18397 case DW_LANG_Modula2:
18398 case DW_LANG_PLI:
18399 return dwarf_version >= 4 ? 1 : -1;
18400 default:
18401 return -1;
18405 /* Given a tree node describing an array bound (either lower or upper) output
18406 a representation for that bound. */
18408 static void
18409 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18410 tree bound, const struct loc_descr_context *context)
18412 int dflt;
18414 while (1)
18415 switch (TREE_CODE (bound))
18417 /* Strip all conversions. */
18418 CASE_CONVERT:
18419 case VIEW_CONVERT_EXPR:
18420 bound = TREE_OPERAND (bound, 0);
18421 break;
18423 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
18424 are even omitted when they are the default. */
18425 case INTEGER_CST:
18426 /* If the value for this bound is the default one, we can even omit the
18427 attribute. */
18428 if (bound_attr == DW_AT_lower_bound
18429 && tree_fits_shwi_p (bound)
18430 && (dflt = lower_bound_default ()) != -1
18431 && tree_to_shwi (bound) == dflt)
18432 return;
18434 /* FALLTHRU */
18436 default:
18437 /* Because of the complex interaction there can be with other GNAT
18438 encodings, GDB isn't ready yet to handle proper DWARF description
18439 for self-referencial subrange bounds: let GNAT encodings do the
18440 magic in such a case. */
18441 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18442 && contains_placeholder_p (bound))
18443 return;
18445 add_scalar_info (subrange_die, bound_attr, bound,
18446 dw_scalar_form_constant
18447 | dw_scalar_form_exprloc
18448 | dw_scalar_form_reference,
18449 context);
18450 return;
18454 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18455 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18456 Note that the block of subscript information for an array type also
18457 includes information about the element type of the given array type.
18459 This function reuses previously set type and bound information if
18460 available. */
18462 static void
18463 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18465 unsigned dimension_number;
18466 tree lower, upper;
18467 dw_die_ref child = type_die->die_child;
18469 for (dimension_number = 0;
18470 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18471 type = TREE_TYPE (type), dimension_number++)
18473 tree domain = TYPE_DOMAIN (type);
18475 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18476 break;
18478 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18479 and (in GNU C only) variable bounds. Handle all three forms
18480 here. */
18482 /* Find and reuse a previously generated DW_TAG_subrange_type if
18483 available.
18485 For multi-dimensional arrays, as we iterate through the
18486 various dimensions in the enclosing for loop above, we also
18487 iterate through the DIE children and pick at each
18488 DW_TAG_subrange_type previously generated (if available).
18489 Each child DW_TAG_subrange_type DIE describes the range of
18490 the current dimension. At this point we should have as many
18491 DW_TAG_subrange_type's as we have dimensions in the
18492 array. */
18493 dw_die_ref subrange_die = NULL;
18494 if (child)
18495 while (1)
18497 child = child->die_sib;
18498 if (child->die_tag == DW_TAG_subrange_type)
18499 subrange_die = child;
18500 if (child == type_die->die_child)
18502 /* If we wrapped around, stop looking next time. */
18503 child = NULL;
18504 break;
18506 if (child->die_tag == DW_TAG_subrange_type)
18507 break;
18509 if (!subrange_die)
18510 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18512 if (domain)
18514 /* We have an array type with specified bounds. */
18515 lower = TYPE_MIN_VALUE (domain);
18516 upper = TYPE_MAX_VALUE (domain);
18518 /* Define the index type. */
18519 if (TREE_TYPE (domain)
18520 && !get_AT (subrange_die, DW_AT_type))
18522 /* ??? This is probably an Ada unnamed subrange type. Ignore the
18523 TREE_TYPE field. We can't emit debug info for this
18524 because it is an unnamed integral type. */
18525 if (TREE_CODE (domain) == INTEGER_TYPE
18526 && TYPE_NAME (domain) == NULL_TREE
18527 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18528 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18530 else
18531 add_type_attribute (subrange_die, TREE_TYPE (domain),
18532 TYPE_UNQUALIFIED, false, type_die);
18535 /* ??? If upper is NULL, the array has unspecified length,
18536 but it does have a lower bound. This happens with Fortran
18537 dimension arr(N:*)
18538 Since the debugger is definitely going to need to know N
18539 to produce useful results, go ahead and output the lower
18540 bound solo, and hope the debugger can cope. */
18542 if (!get_AT (subrange_die, DW_AT_lower_bound))
18543 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18544 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18545 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18548 /* Otherwise we have an array type with an unspecified length. The
18549 DWARF-2 spec does not say how to handle this; let's just leave out the
18550 bounds. */
18554 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
18556 static void
18557 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18559 dw_die_ref decl_die;
18560 HOST_WIDE_INT size;
18561 dw_loc_descr_ref size_expr = NULL;
18563 switch (TREE_CODE (tree_node))
18565 case ERROR_MARK:
18566 size = 0;
18567 break;
18568 case ENUMERAL_TYPE:
18569 case RECORD_TYPE:
18570 case UNION_TYPE:
18571 case QUAL_UNION_TYPE:
18572 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18573 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18575 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18576 return;
18578 size_expr = type_byte_size (tree_node, &size);
18579 break;
18580 case FIELD_DECL:
18581 /* For a data member of a struct or union, the DW_AT_byte_size is
18582 generally given as the number of bytes normally allocated for an
18583 object of the *declared* type of the member itself. This is true
18584 even for bit-fields. */
18585 size = int_size_in_bytes (field_type (tree_node));
18586 break;
18587 default:
18588 gcc_unreachable ();
18591 /* Support for dynamically-sized objects was introduced by DWARFv3.
18592 At the moment, GDB does not handle variable byte sizes very well,
18593 though. */
18594 if ((dwarf_version >= 3 || !dwarf_strict)
18595 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18596 && size_expr != NULL)
18597 add_AT_loc (die, DW_AT_byte_size, size_expr);
18599 /* Note that `size' might be -1 when we get to this point. If it is, that
18600 indicates that the byte size of the entity in question is variable and
18601 that we could not generate a DWARF expression that computes it. */
18602 if (size >= 0)
18603 add_AT_unsigned (die, DW_AT_byte_size, size);
18606 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18607 which specifies the distance in bits from the highest order bit of the
18608 "containing object" for the bit-field to the highest order bit of the
18609 bit-field itself.
18611 For any given bit-field, the "containing object" is a hypothetical object
18612 (of some integral or enum type) within which the given bit-field lives. The
18613 type of this hypothetical "containing object" is always the same as the
18614 declared type of the individual bit-field itself. The determination of the
18615 exact location of the "containing object" for a bit-field is rather
18616 complicated. It's handled by the `field_byte_offset' function (above).
18618 CTX is required: see the comment for VLR_CONTEXT.
18620 Note that it is the size (in bytes) of the hypothetical "containing object"
18621 which will be given in the DW_AT_byte_size attribute for this bit-field.
18622 (See `byte_size_attribute' above). */
18624 static inline void
18625 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18627 HOST_WIDE_INT object_offset_in_bytes;
18628 tree original_type = DECL_BIT_FIELD_TYPE (decl);
18629 HOST_WIDE_INT bitpos_int;
18630 HOST_WIDE_INT highest_order_object_bit_offset;
18631 HOST_WIDE_INT highest_order_field_bit_offset;
18632 HOST_WIDE_INT bit_offset;
18634 field_byte_offset (decl, ctx, &object_offset_in_bytes);
18636 /* Must be a field and a bit field. */
18637 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18639 /* We can't yet handle bit-fields whose offsets are variable, so if we
18640 encounter such things, just return without generating any attribute
18641 whatsoever. Likewise for variable or too large size. */
18642 if (! tree_fits_shwi_p (bit_position (decl))
18643 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18644 return;
18646 bitpos_int = int_bit_position (decl);
18648 /* Note that the bit offset is always the distance (in bits) from the
18649 highest-order bit of the "containing object" to the highest-order bit of
18650 the bit-field itself. Since the "high-order end" of any object or field
18651 is different on big-endian and little-endian machines, the computation
18652 below must take account of these differences. */
18653 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18654 highest_order_field_bit_offset = bitpos_int;
18656 if (! BYTES_BIG_ENDIAN)
18658 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18659 highest_order_object_bit_offset +=
18660 simple_type_size_in_bits (original_type);
18663 bit_offset
18664 = (! BYTES_BIG_ENDIAN
18665 ? highest_order_object_bit_offset - highest_order_field_bit_offset
18666 : highest_order_field_bit_offset - highest_order_object_bit_offset);
18668 if (bit_offset < 0)
18669 add_AT_int (die, DW_AT_bit_offset, bit_offset);
18670 else
18671 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18674 /* For a FIELD_DECL node which represents a bit field, output an attribute
18675 which specifies the length in bits of the given field. */
18677 static inline void
18678 add_bit_size_attribute (dw_die_ref die, tree decl)
18680 /* Must be a field and a bit field. */
18681 gcc_assert (TREE_CODE (decl) == FIELD_DECL
18682 && DECL_BIT_FIELD_TYPE (decl));
18684 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18685 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18688 /* If the compiled language is ANSI C, then add a 'prototyped'
18689 attribute, if arg types are given for the parameters of a function. */
18691 static inline void
18692 add_prototyped_attribute (dw_die_ref die, tree func_type)
18694 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18696 case DW_LANG_C:
18697 case DW_LANG_C89:
18698 case DW_LANG_C99:
18699 case DW_LANG_C11:
18700 case DW_LANG_ObjC:
18701 if (prototype_p (func_type))
18702 add_AT_flag (die, DW_AT_prototyped, 1);
18703 break;
18704 default:
18705 break;
18709 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
18710 by looking in the type declaration, the object declaration equate table or
18711 the block mapping. */
18713 static inline dw_die_ref
18714 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18716 dw_die_ref origin_die = NULL;
18718 if (TREE_CODE (origin) != FUNCTION_DECL
18719 && TREE_CODE (origin) != BLOCK)
18721 /* We may have gotten separated from the block for the inlined
18722 function, if we're in an exception handler or some such; make
18723 sure that the abstract function has been written out.
18725 Doing this for nested functions is wrong, however; functions are
18726 distinct units, and our context might not even be inline. */
18727 tree fn = origin;
18729 if (TYPE_P (fn))
18730 fn = TYPE_STUB_DECL (fn);
18732 fn = decl_function_context (fn);
18733 if (fn)
18734 dwarf2out_abstract_function (fn);
18737 if (DECL_P (origin))
18738 origin_die = lookup_decl_die (origin);
18739 else if (TYPE_P (origin))
18740 origin_die = lookup_type_die (origin);
18741 else if (TREE_CODE (origin) == BLOCK)
18742 origin_die = BLOCK_DIE (origin);
18744 /* XXX: Functions that are never lowered don't always have correct block
18745 trees (in the case of java, they simply have no block tree, in some other
18746 languages). For these functions, there is nothing we can really do to
18747 output correct debug info for inlined functions in all cases. Rather
18748 than die, we'll just produce deficient debug info now, in that we will
18749 have variables without a proper abstract origin. In the future, when all
18750 functions are lowered, we should re-add a gcc_assert (origin_die)
18751 here. */
18753 if (origin_die)
18754 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18755 return origin_die;
18758 /* We do not currently support the pure_virtual attribute. */
18760 static inline void
18761 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18763 if (DECL_VINDEX (func_decl))
18765 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18767 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18768 add_AT_loc (die, DW_AT_vtable_elem_location,
18769 new_loc_descr (DW_OP_constu,
18770 tree_to_shwi (DECL_VINDEX (func_decl)),
18771 0));
18773 /* GNU extension: Record what type this method came from originally. */
18774 if (debug_info_level > DINFO_LEVEL_TERSE
18775 && DECL_CONTEXT (func_decl))
18776 add_AT_die_ref (die, DW_AT_containing_type,
18777 lookup_type_die (DECL_CONTEXT (func_decl)));
18781 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18782 given decl. This used to be a vendor extension until after DWARF 4
18783 standardized it. */
18785 static void
18786 add_linkage_attr (dw_die_ref die, tree decl)
18788 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18790 /* Mimic what assemble_name_raw does with a leading '*'. */
18791 if (name[0] == '*')
18792 name = &name[1];
18794 if (dwarf_version >= 4)
18795 add_AT_string (die, DW_AT_linkage_name, name);
18796 else
18797 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18800 /* Add source coordinate attributes for the given decl. */
18802 static void
18803 add_src_coords_attributes (dw_die_ref die, tree decl)
18805 expanded_location s;
18807 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18808 return;
18809 s = expand_location (DECL_SOURCE_LOCATION (decl));
18810 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18811 add_AT_unsigned (die, DW_AT_decl_line, s.line);
18814 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
18816 static void
18817 add_linkage_name_raw (dw_die_ref die, tree decl)
18819 /* Defer until we have an assembler name set. */
18820 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18822 limbo_die_node *asm_name;
18824 asm_name = ggc_cleared_alloc<limbo_die_node> ();
18825 asm_name->die = die;
18826 asm_name->created_for = decl;
18827 asm_name->next = deferred_asm_name;
18828 deferred_asm_name = asm_name;
18830 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18831 add_linkage_attr (die, decl);
18834 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
18836 static void
18837 add_linkage_name (dw_die_ref die, tree decl)
18839 if (debug_info_level > DINFO_LEVEL_NONE
18840 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18841 && TREE_PUBLIC (decl)
18842 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18843 && die->die_tag != DW_TAG_member)
18844 add_linkage_name_raw (die, decl);
18847 /* Add a DW_AT_name attribute and source coordinate attribute for the
18848 given decl, but only if it actually has a name. */
18850 static void
18851 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18853 tree decl_name;
18855 decl_name = DECL_NAME (decl);
18856 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18858 const char *name = dwarf2_name (decl, 0);
18859 if (name)
18860 add_name_attribute (die, name);
18861 if (! DECL_ARTIFICIAL (decl))
18862 add_src_coords_attributes (die, decl);
18864 add_linkage_name (die, decl);
18867 #ifdef VMS_DEBUGGING_INFO
18868 /* Get the function's name, as described by its RTL. This may be different
18869 from the DECL_NAME name used in the source file. */
18870 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18872 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18873 XEXP (DECL_RTL (decl), 0), false);
18874 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18876 #endif /* VMS_DEBUGGING_INFO */
18879 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
18881 static void
18882 add_discr_value (dw_die_ref die, dw_discr_value *value)
18884 dw_attr_node attr;
18886 attr.dw_attr = DW_AT_discr_value;
18887 attr.dw_attr_val.val_class = dw_val_class_discr_value;
18888 attr.dw_attr_val.val_entry = NULL;
18889 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18890 if (value->pos)
18891 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18892 else
18893 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18894 add_dwarf_attr (die, &attr);
18897 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
18899 static void
18900 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18902 dw_attr_node attr;
18904 attr.dw_attr = DW_AT_discr_list;
18905 attr.dw_attr_val.val_class = dw_val_class_discr_list;
18906 attr.dw_attr_val.val_entry = NULL;
18907 attr.dw_attr_val.v.val_discr_list = discr_list;
18908 add_dwarf_attr (die, &attr);
18911 static inline dw_discr_list_ref
18912 AT_discr_list (dw_attr_node *attr)
18914 return attr->dw_attr_val.v.val_discr_list;
18917 #ifdef VMS_DEBUGGING_INFO
18918 /* Output the debug main pointer die for VMS */
18920 void
18921 dwarf2out_vms_debug_main_pointer (void)
18923 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18924 dw_die_ref die;
18926 /* Allocate the VMS debug main subprogram die. */
18927 die = ggc_cleared_alloc<die_node> ();
18928 die->die_tag = DW_TAG_subprogram;
18929 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18930 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18931 current_function_funcdef_no);
18932 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18934 /* Make it the first child of comp_unit_die (). */
18935 die->die_parent = comp_unit_die ();
18936 if (comp_unit_die ()->die_child)
18938 die->die_sib = comp_unit_die ()->die_child->die_sib;
18939 comp_unit_die ()->die_child->die_sib = die;
18941 else
18943 die->die_sib = die;
18944 comp_unit_die ()->die_child = die;
18947 #endif /* VMS_DEBUGGING_INFO */
18949 /* Push a new declaration scope. */
18951 static void
18952 push_decl_scope (tree scope)
18954 vec_safe_push (decl_scope_table, scope);
18957 /* Pop a declaration scope. */
18959 static inline void
18960 pop_decl_scope (void)
18962 decl_scope_table->pop ();
18965 /* walk_tree helper function for uses_local_type, below. */
18967 static tree
18968 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18970 if (!TYPE_P (*tp))
18971 *walk_subtrees = 0;
18972 else
18974 tree name = TYPE_NAME (*tp);
18975 if (name && DECL_P (name) && decl_function_context (name))
18976 return *tp;
18978 return NULL_TREE;
18981 /* If TYPE involves a function-local type (including a local typedef to a
18982 non-local type), returns that type; otherwise returns NULL_TREE. */
18984 static tree
18985 uses_local_type (tree type)
18987 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
18988 return used;
18991 /* Return the DIE for the scope that immediately contains this type.
18992 Non-named types that do not involve a function-local type get global
18993 scope. Named types nested in namespaces or other types get their
18994 containing scope. All other types (i.e. function-local named types) get
18995 the current active scope. */
18997 static dw_die_ref
18998 scope_die_for (tree t, dw_die_ref context_die)
19000 dw_die_ref scope_die = NULL;
19001 tree containing_scope;
19003 /* Non-types always go in the current scope. */
19004 gcc_assert (TYPE_P (t));
19006 /* Use the scope of the typedef, rather than the scope of the type
19007 it refers to. */
19008 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
19009 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
19010 else
19011 containing_scope = TYPE_CONTEXT (t);
19013 /* Use the containing namespace if there is one. */
19014 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
19016 if (context_die == lookup_decl_die (containing_scope))
19017 /* OK */;
19018 else if (debug_info_level > DINFO_LEVEL_TERSE)
19019 context_die = get_context_die (containing_scope);
19020 else
19021 containing_scope = NULL_TREE;
19024 /* Ignore function type "scopes" from the C frontend. They mean that
19025 a tagged type is local to a parmlist of a function declarator, but
19026 that isn't useful to DWARF. */
19027 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19028 containing_scope = NULL_TREE;
19030 if (SCOPE_FILE_SCOPE_P (containing_scope))
19032 /* If T uses a local type keep it local as well, to avoid references
19033 to function-local DIEs from outside the function. */
19034 if (current_function_decl && uses_local_type (t))
19035 scope_die = context_die;
19036 else
19037 scope_die = comp_unit_die ();
19039 else if (TYPE_P (containing_scope))
19041 /* For types, we can just look up the appropriate DIE. */
19042 if (debug_info_level > DINFO_LEVEL_TERSE)
19043 scope_die = get_context_die (containing_scope);
19044 else
19046 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19047 if (scope_die == NULL)
19048 scope_die = comp_unit_die ();
19051 else
19052 scope_die = context_die;
19054 return scope_die;
19057 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
19059 static inline int
19060 local_scope_p (dw_die_ref context_die)
19062 for (; context_die; context_die = context_die->die_parent)
19063 if (context_die->die_tag == DW_TAG_inlined_subroutine
19064 || context_die->die_tag == DW_TAG_subprogram)
19065 return 1;
19067 return 0;
19070 /* Returns nonzero if CONTEXT_DIE is a class. */
19072 static inline int
19073 class_scope_p (dw_die_ref context_die)
19075 return (context_die
19076 && (context_die->die_tag == DW_TAG_structure_type
19077 || context_die->die_tag == DW_TAG_class_type
19078 || context_die->die_tag == DW_TAG_interface_type
19079 || context_die->die_tag == DW_TAG_union_type));
19082 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19083 whether or not to treat a DIE in this context as a declaration. */
19085 static inline int
19086 class_or_namespace_scope_p (dw_die_ref context_die)
19088 return (class_scope_p (context_die)
19089 || (context_die && context_die->die_tag == DW_TAG_namespace));
19092 /* Many forms of DIEs require a "type description" attribute. This
19093 routine locates the proper "type descriptor" die for the type given
19094 by 'type' plus any additional qualifiers given by 'cv_quals', and
19095 adds a DW_AT_type attribute below the given die. */
19097 static void
19098 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19099 bool reverse, dw_die_ref context_die)
19101 enum tree_code code = TREE_CODE (type);
19102 dw_die_ref type_die = NULL;
19104 /* ??? If this type is an unnamed subrange type of an integral, floating-point
19105 or fixed-point type, use the inner type. This is because we have no
19106 support for unnamed types in base_type_die. This can happen if this is
19107 an Ada subrange type. Correct solution is emit a subrange type die. */
19108 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19109 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19110 type = TREE_TYPE (type), code = TREE_CODE (type);
19112 if (code == ERROR_MARK
19113 /* Handle a special case. For functions whose return type is void, we
19114 generate *no* type attribute. (Note that no object may have type
19115 `void', so this only applies to function return types). */
19116 || code == VOID_TYPE)
19117 return;
19119 type_die = modified_type_die (type,
19120 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19121 reverse,
19122 context_die);
19124 if (type_die != NULL)
19125 add_AT_die_ref (object_die, DW_AT_type, type_die);
19128 /* Given an object die, add the calling convention attribute for the
19129 function call type. */
19130 static void
19131 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19133 enum dwarf_calling_convention value = DW_CC_normal;
19135 value = ((enum dwarf_calling_convention)
19136 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19138 if (is_fortran ()
19139 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19141 /* DWARF 2 doesn't provide a way to identify a program's source-level
19142 entry point. DW_AT_calling_convention attributes are only meant
19143 to describe functions' calling conventions. However, lacking a
19144 better way to signal the Fortran main program, we used this for
19145 a long time, following existing custom. Now, DWARF 4 has
19146 DW_AT_main_subprogram, which we add below, but some tools still
19147 rely on the old way, which we thus keep. */
19148 value = DW_CC_program;
19150 if (dwarf_version >= 4 || !dwarf_strict)
19151 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19154 /* Only add the attribute if the backend requests it, and
19155 is not DW_CC_normal. */
19156 if (value && (value != DW_CC_normal))
19157 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19160 /* Given a tree pointer to a struct, class, union, or enum type node, return
19161 a pointer to the (string) tag name for the given type, or zero if the type
19162 was declared without a tag. */
19164 static const char *
19165 type_tag (const_tree type)
19167 const char *name = 0;
19169 if (TYPE_NAME (type) != 0)
19171 tree t = 0;
19173 /* Find the IDENTIFIER_NODE for the type name. */
19174 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19175 && !TYPE_NAMELESS (type))
19176 t = TYPE_NAME (type);
19178 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19179 a TYPE_DECL node, regardless of whether or not a `typedef' was
19180 involved. */
19181 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19182 && ! DECL_IGNORED_P (TYPE_NAME (type)))
19184 /* We want to be extra verbose. Don't call dwarf_name if
19185 DECL_NAME isn't set. The default hook for decl_printable_name
19186 doesn't like that, and in this context it's correct to return
19187 0, instead of "<anonymous>" or the like. */
19188 if (DECL_NAME (TYPE_NAME (type))
19189 && !DECL_NAMELESS (TYPE_NAME (type)))
19190 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19193 /* Now get the name as a string, or invent one. */
19194 if (!name && t != 0)
19195 name = IDENTIFIER_POINTER (t);
19198 return (name == 0 || *name == '\0') ? 0 : name;
19201 /* Return the type associated with a data member, make a special check
19202 for bit field types. */
19204 static inline tree
19205 member_declared_type (const_tree member)
19207 return (DECL_BIT_FIELD_TYPE (member)
19208 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19211 /* Get the decl's label, as described by its RTL. This may be different
19212 from the DECL_NAME name used in the source file. */
19214 #if 0
19215 static const char *
19216 decl_start_label (tree decl)
19218 rtx x;
19219 const char *fnname;
19221 x = DECL_RTL (decl);
19222 gcc_assert (MEM_P (x));
19224 x = XEXP (x, 0);
19225 gcc_assert (GET_CODE (x) == SYMBOL_REF);
19227 fnname = XSTR (x, 0);
19228 return fnname;
19230 #endif
19232 /* For variable-length arrays that have been previously generated, but
19233 may be incomplete due to missing subscript info, fill the subscript
19234 info. Return TRUE if this is one of those cases. */
19235 static bool
19236 fill_variable_array_bounds (tree type)
19238 if (TREE_ASM_WRITTEN (type)
19239 && TREE_CODE (type) == ARRAY_TYPE
19240 && variably_modified_type_p (type, NULL))
19242 dw_die_ref array_die = lookup_type_die (type);
19243 if (!array_die)
19244 return false;
19245 add_subscript_info (array_die, type, !is_ada ());
19246 return true;
19248 return false;
19251 /* These routines generate the internal representation of the DIE's for
19252 the compilation unit. Debugging information is collected by walking
19253 the declaration trees passed in from dwarf2out_decl(). */
19255 static void
19256 gen_array_type_die (tree type, dw_die_ref context_die)
19258 dw_die_ref array_die;
19260 /* GNU compilers represent multidimensional array types as sequences of one
19261 dimensional array types whose element types are themselves array types.
19262 We sometimes squish that down to a single array_type DIE with multiple
19263 subscripts in the Dwarf debugging info. The draft Dwarf specification
19264 say that we are allowed to do this kind of compression in C, because
19265 there is no difference between an array of arrays and a multidimensional
19266 array. We don't do this for Ada to remain as close as possible to the
19267 actual representation, which is especially important against the language
19268 flexibilty wrt arrays of variable size. */
19270 bool collapse_nested_arrays = !is_ada ();
19272 if (fill_variable_array_bounds (type))
19273 return;
19275 dw_die_ref scope_die = scope_die_for (type, context_die);
19276 tree element_type;
19278 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19279 DW_TAG_string_type doesn't have DW_AT_type attribute). */
19280 if (TYPE_STRING_FLAG (type)
19281 && TREE_CODE (type) == ARRAY_TYPE
19282 && is_fortran ()
19283 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19285 HOST_WIDE_INT size;
19287 array_die = new_die (DW_TAG_string_type, scope_die, type);
19288 add_name_attribute (array_die, type_tag (type));
19289 equate_type_number_to_die (type, array_die);
19290 size = int_size_in_bytes (type);
19291 if (size >= 0)
19292 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19293 else if (TYPE_DOMAIN (type) != NULL_TREE
19294 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
19296 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19297 tree rszdecl = szdecl;
19298 HOST_WIDE_INT rsize = 0;
19300 size = int_size_in_bytes (TREE_TYPE (szdecl));
19301 if (!DECL_P (szdecl))
19303 if (TREE_CODE (szdecl) == INDIRECT_REF
19304 && DECL_P (TREE_OPERAND (szdecl, 0)))
19306 rszdecl = TREE_OPERAND (szdecl, 0);
19307 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
19308 if (rsize <= 0)
19309 size = 0;
19311 else
19312 size = 0;
19314 if (size > 0)
19316 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19317 if (loc == NULL
19318 && early_dwarf
19319 && current_function_decl
19320 && DECL_CONTEXT (rszdecl) == current_function_decl)
19322 dw_die_ref ref = lookup_decl_die (rszdecl);
19323 dw_loc_descr_ref l = NULL;
19324 if (ref)
19326 l = new_loc_descr (DW_OP_call4, 0, 0);
19327 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19328 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
19329 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
19331 else if (TREE_CODE (rszdecl) == PARM_DECL
19332 && string_types)
19334 l = new_loc_descr (DW_OP_call4, 0, 0);
19335 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19336 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
19337 string_types->safe_push (array_die);
19339 if (l && rszdecl != szdecl)
19341 if (rsize == DWARF2_ADDR_SIZE)
19342 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
19343 0, 0));
19344 else
19345 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
19346 rsize, 0));
19348 if (l)
19349 loc = new_loc_list (l, NULL, NULL, NULL);
19351 if (loc)
19353 add_AT_location_description (array_die, DW_AT_string_length,
19354 loc);
19355 if (size != DWARF2_ADDR_SIZE)
19356 add_AT_unsigned (array_die, dwarf_version >= 5
19357 ? DW_AT_string_length_byte_size
19358 : DW_AT_byte_size, size);
19362 return;
19365 array_die = new_die (DW_TAG_array_type, scope_die, type);
19366 add_name_attribute (array_die, type_tag (type));
19367 equate_type_number_to_die (type, array_die);
19369 if (TREE_CODE (type) == VECTOR_TYPE)
19370 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19372 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
19373 if (is_fortran ()
19374 && TREE_CODE (type) == ARRAY_TYPE
19375 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19376 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19377 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19379 #if 0
19380 /* We default the array ordering. SDB will probably do
19381 the right things even if DW_AT_ordering is not present. It's not even
19382 an issue until we start to get into multidimensional arrays anyway. If
19383 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19384 then we'll have to put the DW_AT_ordering attribute back in. (But if
19385 and when we find out that we need to put these in, we will only do so
19386 for multidimensional arrays. */
19387 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19388 #endif
19390 if (TREE_CODE (type) == VECTOR_TYPE)
19392 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
19393 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19394 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19395 add_bound_info (subrange_die, DW_AT_upper_bound,
19396 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19398 else
19399 add_subscript_info (array_die, type, collapse_nested_arrays);
19401 /* Add representation of the type of the elements of this array type and
19402 emit the corresponding DIE if we haven't done it already. */
19403 element_type = TREE_TYPE (type);
19404 if (collapse_nested_arrays)
19405 while (TREE_CODE (element_type) == ARRAY_TYPE)
19407 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19408 break;
19409 element_type = TREE_TYPE (element_type);
19412 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19413 TREE_CODE (type) == ARRAY_TYPE
19414 && TYPE_REVERSE_STORAGE_ORDER (type),
19415 context_die);
19417 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19418 if (TYPE_ARTIFICIAL (type))
19419 add_AT_flag (array_die, DW_AT_artificial, 1);
19421 if (get_AT (array_die, DW_AT_name))
19422 add_pubtype (type, array_die);
19425 /* After all arguments are created, adjust any DW_TAG_string_type
19426 DIEs DW_AT_string_length attributes. */
19428 static void
19429 adjust_string_types (void)
19431 dw_die_ref array_die;
19432 unsigned int i;
19433 FOR_EACH_VEC_ELT (*string_types, i, array_die)
19435 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
19436 if (a == NULL)
19437 continue;
19438 dw_loc_descr_ref loc = AT_loc (a);
19439 gcc_assert (loc->dw_loc_opc == DW_OP_call4
19440 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
19441 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
19442 if (ref)
19444 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19445 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
19446 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
19448 else
19450 remove_AT (array_die, DW_AT_string_length);
19451 remove_AT (array_die, dwarf_version >= 5
19452 ? DW_AT_string_length_byte_size
19453 : DW_AT_byte_size);
19458 /* This routine generates DIE for array with hidden descriptor, details
19459 are filled into *info by a langhook. */
19461 static void
19462 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19463 dw_die_ref context_die)
19465 const dw_die_ref scope_die = scope_die_for (type, context_die);
19466 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19467 const struct loc_descr_context context = { type, info->base_decl, NULL };
19468 int dim;
19470 add_name_attribute (array_die, type_tag (type));
19471 equate_type_number_to_die (type, array_die);
19473 if (info->ndimensions > 1)
19474 switch (info->ordering)
19476 case array_descr_ordering_row_major:
19477 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19478 break;
19479 case array_descr_ordering_column_major:
19480 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19481 break;
19482 default:
19483 break;
19486 if (dwarf_version >= 3 || !dwarf_strict)
19488 if (info->data_location)
19489 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19490 dw_scalar_form_exprloc, &context);
19491 if (info->associated)
19492 add_scalar_info (array_die, DW_AT_associated, info->associated,
19493 dw_scalar_form_constant
19494 | dw_scalar_form_exprloc
19495 | dw_scalar_form_reference, &context);
19496 if (info->allocated)
19497 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19498 dw_scalar_form_constant
19499 | dw_scalar_form_exprloc
19500 | dw_scalar_form_reference, &context);
19501 if (info->stride)
19503 const enum dwarf_attribute attr
19504 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19505 const int forms
19506 = (info->stride_in_bits)
19507 ? dw_scalar_form_constant
19508 : (dw_scalar_form_constant
19509 | dw_scalar_form_exprloc
19510 | dw_scalar_form_reference);
19512 add_scalar_info (array_die, attr, info->stride, forms, &context);
19516 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19518 for (dim = 0; dim < info->ndimensions; dim++)
19520 dw_die_ref subrange_die
19521 = new_die (DW_TAG_subrange_type, array_die, NULL);
19523 if (info->dimen[dim].bounds_type)
19524 add_type_attribute (subrange_die,
19525 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19526 false, context_die);
19527 if (info->dimen[dim].lower_bound)
19528 add_bound_info (subrange_die, DW_AT_lower_bound,
19529 info->dimen[dim].lower_bound, &context);
19530 if (info->dimen[dim].upper_bound)
19531 add_bound_info (subrange_die, DW_AT_upper_bound,
19532 info->dimen[dim].upper_bound, &context);
19533 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19534 add_scalar_info (subrange_die, DW_AT_byte_stride,
19535 info->dimen[dim].stride,
19536 dw_scalar_form_constant
19537 | dw_scalar_form_exprloc
19538 | dw_scalar_form_reference,
19539 &context);
19542 gen_type_die (info->element_type, context_die);
19543 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19544 TREE_CODE (type) == ARRAY_TYPE
19545 && TYPE_REVERSE_STORAGE_ORDER (type),
19546 context_die);
19548 if (get_AT (array_die, DW_AT_name))
19549 add_pubtype (type, array_die);
19552 #if 0
19553 static void
19554 gen_entry_point_die (tree decl, dw_die_ref context_die)
19556 tree origin = decl_ultimate_origin (decl);
19557 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19559 if (origin != NULL)
19560 add_abstract_origin_attribute (decl_die, origin);
19561 else
19563 add_name_and_src_coords_attributes (decl_die, decl);
19564 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19565 TYPE_UNQUALIFIED, false, context_die);
19568 if (DECL_ABSTRACT_P (decl))
19569 equate_decl_number_to_die (decl, decl_die);
19570 else
19571 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19573 #endif
19575 /* Walk through the list of incomplete types again, trying once more to
19576 emit full debugging info for them. */
19578 static void
19579 retry_incomplete_types (void)
19581 set_early_dwarf s;
19582 int i;
19584 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19585 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19586 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19587 vec_safe_truncate (incomplete_types, 0);
19590 /* Determine what tag to use for a record type. */
19592 static enum dwarf_tag
19593 record_type_tag (tree type)
19595 if (! lang_hooks.types.classify_record)
19596 return DW_TAG_structure_type;
19598 switch (lang_hooks.types.classify_record (type))
19600 case RECORD_IS_STRUCT:
19601 return DW_TAG_structure_type;
19603 case RECORD_IS_CLASS:
19604 return DW_TAG_class_type;
19606 case RECORD_IS_INTERFACE:
19607 if (dwarf_version >= 3 || !dwarf_strict)
19608 return DW_TAG_interface_type;
19609 return DW_TAG_structure_type;
19611 default:
19612 gcc_unreachable ();
19616 /* Generate a DIE to represent an enumeration type. Note that these DIEs
19617 include all of the information about the enumeration values also. Each
19618 enumerated type name/value is listed as a child of the enumerated type
19619 DIE. */
19621 static dw_die_ref
19622 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19624 dw_die_ref type_die = lookup_type_die (type);
19626 if (type_die == NULL)
19628 type_die = new_die (DW_TAG_enumeration_type,
19629 scope_die_for (type, context_die), type);
19630 equate_type_number_to_die (type, type_die);
19631 add_name_attribute (type_die, type_tag (type));
19632 if (dwarf_version >= 4 || !dwarf_strict)
19634 if (ENUM_IS_SCOPED (type))
19635 add_AT_flag (type_die, DW_AT_enum_class, 1);
19636 if (ENUM_IS_OPAQUE (type))
19637 add_AT_flag (type_die, DW_AT_declaration, 1);
19640 else if (! TYPE_SIZE (type))
19641 return type_die;
19642 else
19643 remove_AT (type_die, DW_AT_declaration);
19645 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
19646 given enum type is incomplete, do not generate the DW_AT_byte_size
19647 attribute or the DW_AT_element_list attribute. */
19648 if (TYPE_SIZE (type))
19650 tree link;
19652 TREE_ASM_WRITTEN (type) = 1;
19653 add_byte_size_attribute (type_die, type);
19654 if (dwarf_version >= 3 || !dwarf_strict)
19656 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19657 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19658 context_die);
19660 if (TYPE_STUB_DECL (type) != NULL_TREE)
19662 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19663 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19666 /* If the first reference to this type was as the return type of an
19667 inline function, then it may not have a parent. Fix this now. */
19668 if (type_die->die_parent == NULL)
19669 add_child_die (scope_die_for (type, context_die), type_die);
19671 for (link = TYPE_VALUES (type);
19672 link != NULL; link = TREE_CHAIN (link))
19674 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19675 tree value = TREE_VALUE (link);
19677 add_name_attribute (enum_die,
19678 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19680 if (TREE_CODE (value) == CONST_DECL)
19681 value = DECL_INITIAL (value);
19683 if (simple_type_size_in_bits (TREE_TYPE (value))
19684 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19686 /* For constant forms created by add_AT_unsigned DWARF
19687 consumers (GDB, elfutils, etc.) always zero extend
19688 the value. Only when the actual value is negative
19689 do we need to use add_AT_int to generate a constant
19690 form that can represent negative values. */
19691 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19692 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19693 add_AT_unsigned (enum_die, DW_AT_const_value,
19694 (unsigned HOST_WIDE_INT) val);
19695 else
19696 add_AT_int (enum_die, DW_AT_const_value, val);
19698 else
19699 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
19700 that here. TODO: This should be re-worked to use correct
19701 signed/unsigned double tags for all cases. */
19702 add_AT_wide (enum_die, DW_AT_const_value, value);
19705 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19706 if (TYPE_ARTIFICIAL (type))
19707 add_AT_flag (type_die, DW_AT_artificial, 1);
19709 else
19710 add_AT_flag (type_die, DW_AT_declaration, 1);
19712 add_pubtype (type, type_die);
19714 return type_die;
19717 /* Generate a DIE to represent either a real live formal parameter decl or to
19718 represent just the type of some formal parameter position in some function
19719 type.
19721 Note that this routine is a bit unusual because its argument may be a
19722 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19723 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19724 node. If it's the former then this function is being called to output a
19725 DIE to represent a formal parameter object (or some inlining thereof). If
19726 it's the latter, then this function is only being called to output a
19727 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19728 argument type of some subprogram type.
19729 If EMIT_NAME_P is true, name and source coordinate attributes
19730 are emitted. */
19732 static dw_die_ref
19733 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19734 dw_die_ref context_die)
19736 tree node_or_origin = node ? node : origin;
19737 tree ultimate_origin;
19738 dw_die_ref parm_die = NULL;
19740 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19742 parm_die = lookup_decl_die (node);
19744 /* If the contexts differ, we may not be talking about the same
19745 thing. */
19746 if (parm_die && parm_die->die_parent != context_die)
19748 if (!DECL_ABSTRACT_P (node))
19750 /* This can happen when creating an inlined instance, in
19751 which case we need to create a new DIE that will get
19752 annotated with DW_AT_abstract_origin. */
19753 parm_die = NULL;
19755 else
19757 /* FIXME: Reuse DIE even with a differing context.
19759 This can happen when calling
19760 dwarf2out_abstract_function to build debug info for
19761 the abstract instance of a function for which we have
19762 already generated a DIE in
19763 dwarf2out_early_global_decl.
19765 Once we remove dwarf2out_abstract_function, we should
19766 have a call to gcc_unreachable here. */
19770 if (parm_die && parm_die->die_parent == NULL)
19772 /* Check that parm_die already has the right attributes that
19773 we would have added below. If any attributes are
19774 missing, fall through to add them. */
19775 if (! DECL_ABSTRACT_P (node_or_origin)
19776 && !get_AT (parm_die, DW_AT_location)
19777 && !get_AT (parm_die, DW_AT_const_value))
19778 /* We are missing location info, and are about to add it. */
19780 else
19782 add_child_die (context_die, parm_die);
19783 return parm_die;
19788 /* If we have a previously generated DIE, use it, unless this is an
19789 concrete instance (origin != NULL), in which case we need a new
19790 DIE with a corresponding DW_AT_abstract_origin. */
19791 bool reusing_die;
19792 if (parm_die && origin == NULL)
19793 reusing_die = true;
19794 else
19796 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19797 reusing_die = false;
19800 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19802 case tcc_declaration:
19803 ultimate_origin = decl_ultimate_origin (node_or_origin);
19804 if (node || ultimate_origin)
19805 origin = ultimate_origin;
19807 if (reusing_die)
19808 goto add_location;
19810 if (origin != NULL)
19811 add_abstract_origin_attribute (parm_die, origin);
19812 else if (emit_name_p)
19813 add_name_and_src_coords_attributes (parm_die, node);
19814 if (origin == NULL
19815 || (! DECL_ABSTRACT_P (node_or_origin)
19816 && variably_modified_type_p (TREE_TYPE (node_or_origin),
19817 decl_function_context
19818 (node_or_origin))))
19820 tree type = TREE_TYPE (node_or_origin);
19821 if (decl_by_reference_p (node_or_origin))
19822 add_type_attribute (parm_die, TREE_TYPE (type),
19823 TYPE_UNQUALIFIED,
19824 false, context_die);
19825 else
19826 add_type_attribute (parm_die, type,
19827 decl_quals (node_or_origin),
19828 false, context_die);
19830 if (origin == NULL && DECL_ARTIFICIAL (node))
19831 add_AT_flag (parm_die, DW_AT_artificial, 1);
19832 add_location:
19833 if (node && node != origin)
19834 equate_decl_number_to_die (node, parm_die);
19835 if (! DECL_ABSTRACT_P (node_or_origin))
19836 add_location_or_const_value_attribute (parm_die, node_or_origin,
19837 node == NULL);
19839 break;
19841 case tcc_type:
19842 /* We were called with some kind of a ..._TYPE node. */
19843 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19844 context_die);
19845 break;
19847 default:
19848 gcc_unreachable ();
19851 return parm_die;
19854 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19855 children DW_TAG_formal_parameter DIEs representing the arguments of the
19856 parameter pack.
19858 PARM_PACK must be a function parameter pack.
19859 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19860 must point to the subsequent arguments of the function PACK_ARG belongs to.
19861 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19862 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19863 following the last one for which a DIE was generated. */
19865 static dw_die_ref
19866 gen_formal_parameter_pack_die (tree parm_pack,
19867 tree pack_arg,
19868 dw_die_ref subr_die,
19869 tree *next_arg)
19871 tree arg;
19872 dw_die_ref parm_pack_die;
19874 gcc_assert (parm_pack
19875 && lang_hooks.function_parameter_pack_p (parm_pack)
19876 && subr_die);
19878 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19879 add_src_coords_attributes (parm_pack_die, parm_pack);
19881 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19883 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19884 parm_pack))
19885 break;
19886 gen_formal_parameter_die (arg, NULL,
19887 false /* Don't emit name attribute. */,
19888 parm_pack_die);
19890 if (next_arg)
19891 *next_arg = arg;
19892 return parm_pack_die;
19895 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19896 at the end of an (ANSI prototyped) formal parameters list. */
19898 static void
19899 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19901 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19904 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19905 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19906 parameters as specified in some function type specification (except for
19907 those which appear as part of a function *definition*). */
19909 static void
19910 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19912 tree link;
19913 tree formal_type = NULL;
19914 tree first_parm_type;
19915 tree arg;
19917 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19919 arg = DECL_ARGUMENTS (function_or_method_type);
19920 function_or_method_type = TREE_TYPE (function_or_method_type);
19922 else
19923 arg = NULL_TREE;
19925 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19927 /* Make our first pass over the list of formal parameter types and output a
19928 DW_TAG_formal_parameter DIE for each one. */
19929 for (link = first_parm_type; link; )
19931 dw_die_ref parm_die;
19933 formal_type = TREE_VALUE (link);
19934 if (formal_type == void_type_node)
19935 break;
19937 /* Output a (nameless) DIE to represent the formal parameter itself. */
19938 if (!POINTER_BOUNDS_TYPE_P (formal_type))
19940 parm_die = gen_formal_parameter_die (formal_type, NULL,
19941 true /* Emit name attribute. */,
19942 context_die);
19943 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19944 && link == first_parm_type)
19946 add_AT_flag (parm_die, DW_AT_artificial, 1);
19947 if (dwarf_version >= 3 || !dwarf_strict)
19948 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19950 else if (arg && DECL_ARTIFICIAL (arg))
19951 add_AT_flag (parm_die, DW_AT_artificial, 1);
19954 link = TREE_CHAIN (link);
19955 if (arg)
19956 arg = DECL_CHAIN (arg);
19959 /* If this function type has an ellipsis, add a
19960 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
19961 if (formal_type != void_type_node)
19962 gen_unspecified_parameters_die (function_or_method_type, context_die);
19964 /* Make our second (and final) pass over the list of formal parameter types
19965 and output DIEs to represent those types (as necessary). */
19966 for (link = TYPE_ARG_TYPES (function_or_method_type);
19967 link && TREE_VALUE (link);
19968 link = TREE_CHAIN (link))
19969 gen_type_die (TREE_VALUE (link), context_die);
19972 /* We want to generate the DIE for TYPE so that we can generate the
19973 die for MEMBER, which has been defined; we will need to refer back
19974 to the member declaration nested within TYPE. If we're trying to
19975 generate minimal debug info for TYPE, processing TYPE won't do the
19976 trick; we need to attach the member declaration by hand. */
19978 static void
19979 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19981 gen_type_die (type, context_die);
19983 /* If we're trying to avoid duplicate debug info, we may not have
19984 emitted the member decl for this function. Emit it now. */
19985 if (TYPE_STUB_DECL (type)
19986 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19987 && ! lookup_decl_die (member))
19989 dw_die_ref type_die;
19990 gcc_assert (!decl_ultimate_origin (member));
19992 push_decl_scope (type);
19993 type_die = lookup_type_die_strip_naming_typedef (type);
19994 if (TREE_CODE (member) == FUNCTION_DECL)
19995 gen_subprogram_die (member, type_die);
19996 else if (TREE_CODE (member) == FIELD_DECL)
19998 /* Ignore the nameless fields that are used to skip bits but handle
19999 C++ anonymous unions and structs. */
20000 if (DECL_NAME (member) != NULL_TREE
20001 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
20002 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
20004 struct vlr_context vlr_ctx = {
20005 DECL_CONTEXT (member), /* struct_type */
20006 NULL_TREE /* variant_part_offset */
20008 gen_type_die (member_declared_type (member), type_die);
20009 gen_field_die (member, &vlr_ctx, type_die);
20012 else
20013 gen_variable_die (member, NULL_TREE, type_die);
20015 pop_decl_scope ();
20019 /* Forward declare these functions, because they are mutually recursive
20020 with their set_block_* pairing functions. */
20021 static void set_decl_origin_self (tree);
20022 static void set_decl_abstract_flags (tree, vec<tree> &);
20024 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
20025 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
20026 that it points to the node itself, thus indicating that the node is its
20027 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
20028 the given node is NULL, recursively descend the decl/block tree which
20029 it is the root of, and for each other ..._DECL or BLOCK node contained
20030 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
20031 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
20032 values to point to themselves. */
20034 static void
20035 set_block_origin_self (tree stmt)
20037 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
20039 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
20042 tree local_decl;
20044 for (local_decl = BLOCK_VARS (stmt);
20045 local_decl != NULL_TREE;
20046 local_decl = DECL_CHAIN (local_decl))
20047 /* Do not recurse on nested functions since the inlining status
20048 of parent and child can be different as per the DWARF spec. */
20049 if (TREE_CODE (local_decl) != FUNCTION_DECL
20050 && !DECL_EXTERNAL (local_decl))
20051 set_decl_origin_self (local_decl);
20055 tree subblock;
20057 for (subblock = BLOCK_SUBBLOCKS (stmt);
20058 subblock != NULL_TREE;
20059 subblock = BLOCK_CHAIN (subblock))
20060 set_block_origin_self (subblock); /* Recurse. */
20065 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
20066 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
20067 node to so that it points to the node itself, thus indicating that the
20068 node represents its own (abstract) origin. Additionally, if the
20069 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
20070 the decl/block tree of which the given node is the root of, and for
20071 each other ..._DECL or BLOCK node contained therein whose
20072 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
20073 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
20074 point to themselves. */
20076 static void
20077 set_decl_origin_self (tree decl)
20079 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
20081 DECL_ABSTRACT_ORIGIN (decl) = decl;
20082 if (TREE_CODE (decl) == FUNCTION_DECL)
20084 tree arg;
20086 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20087 DECL_ABSTRACT_ORIGIN (arg) = arg;
20088 if (DECL_INITIAL (decl) != NULL_TREE
20089 && DECL_INITIAL (decl) != error_mark_node)
20090 set_block_origin_self (DECL_INITIAL (decl));
20095 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
20096 and if it wasn't 1 before, push it to abstract_vec vector.
20097 For all local decls and all local sub-blocks (recursively) do it
20098 too. */
20100 static void
20101 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
20103 tree local_decl;
20104 tree subblock;
20105 unsigned int i;
20107 if (!BLOCK_ABSTRACT (stmt))
20109 abstract_vec.safe_push (stmt);
20110 BLOCK_ABSTRACT (stmt) = 1;
20113 for (local_decl = BLOCK_VARS (stmt);
20114 local_decl != NULL_TREE;
20115 local_decl = DECL_CHAIN (local_decl))
20116 if (! DECL_EXTERNAL (local_decl))
20117 set_decl_abstract_flags (local_decl, abstract_vec);
20119 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20121 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
20122 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
20123 || TREE_CODE (local_decl) == PARM_DECL)
20124 set_decl_abstract_flags (local_decl, abstract_vec);
20127 for (subblock = BLOCK_SUBBLOCKS (stmt);
20128 subblock != NULL_TREE;
20129 subblock = BLOCK_CHAIN (subblock))
20130 set_block_abstract_flags (subblock, abstract_vec);
20133 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
20134 to 1 and if it wasn't 1 before, push to abstract_vec vector.
20135 In the case where the decl is a FUNCTION_DECL also set the abstract
20136 flags for all of the parameters, local vars, local
20137 blocks and sub-blocks (recursively). */
20139 static void
20140 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
20142 if (!DECL_ABSTRACT_P (decl))
20144 abstract_vec.safe_push (decl);
20145 DECL_ABSTRACT_P (decl) = 1;
20148 if (TREE_CODE (decl) == FUNCTION_DECL)
20150 tree arg;
20152 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20153 if (!DECL_ABSTRACT_P (arg))
20155 abstract_vec.safe_push (arg);
20156 DECL_ABSTRACT_P (arg) = 1;
20158 if (DECL_INITIAL (decl) != NULL_TREE
20159 && DECL_INITIAL (decl) != error_mark_node)
20160 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
20164 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20165 may later generate inlined and/or out-of-line instances of.
20167 FIXME: In the early-dwarf world, this function, and most of the
20168 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
20169 the abstract instance. All we would need to do is annotate
20170 the early DIE with the appropriate DW_AT_inline in late
20171 dwarf (perhaps in gen_inlined_subroutine_die).
20173 However, we can't do this yet, because LTO streaming of DIEs
20174 has not been implemented yet. */
20176 static void
20177 dwarf2out_abstract_function (tree decl)
20179 dw_die_ref old_die;
20180 tree save_fn;
20181 tree context;
20182 hash_table<decl_loc_hasher> *old_decl_loc_table;
20183 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20184 int old_call_site_count, old_tail_call_site_count;
20185 struct call_arg_loc_node *old_call_arg_locations;
20187 /* Make sure we have the actual abstract inline, not a clone. */
20188 decl = DECL_ORIGIN (decl);
20190 old_die = lookup_decl_die (decl);
20191 if (old_die && get_AT (old_die, DW_AT_inline))
20192 /* We've already generated the abstract instance. */
20193 return;
20195 /* We can be called while recursively when seeing block defining inlined subroutine
20196 DIE. Be sure to not clobber the outer location table nor use it or we would
20197 get locations in abstract instantces. */
20198 old_decl_loc_table = decl_loc_table;
20199 decl_loc_table = NULL;
20200 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20201 cached_dw_loc_list_table = NULL;
20202 old_call_arg_locations = call_arg_locations;
20203 call_arg_locations = NULL;
20204 old_call_site_count = call_site_count;
20205 call_site_count = -1;
20206 old_tail_call_site_count = tail_call_site_count;
20207 tail_call_site_count = -1;
20209 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20210 we don't get confused by DECL_ABSTRACT_P. */
20211 if (debug_info_level > DINFO_LEVEL_TERSE)
20213 context = decl_class_context (decl);
20214 if (context)
20215 gen_type_die_for_member
20216 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20219 /* Pretend we've just finished compiling this function. */
20220 save_fn = current_function_decl;
20221 current_function_decl = decl;
20223 auto_vec<tree, 64> abstract_vec;
20224 set_decl_abstract_flags (decl, abstract_vec);
20225 dwarf2out_decl (decl);
20226 unsigned int i;
20227 tree t;
20228 FOR_EACH_VEC_ELT (abstract_vec, i, t)
20229 if (TREE_CODE (t) == BLOCK)
20230 BLOCK_ABSTRACT (t) = 0;
20231 else
20232 DECL_ABSTRACT_P (t) = 0;
20234 current_function_decl = save_fn;
20235 decl_loc_table = old_decl_loc_table;
20236 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20237 call_arg_locations = old_call_arg_locations;
20238 call_site_count = old_call_site_count;
20239 tail_call_site_count = old_tail_call_site_count;
20242 /* Helper function of premark_used_types() which gets called through
20243 htab_traverse.
20245 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20246 marked as unused by prune_unused_types. */
20248 bool
20249 premark_used_types_helper (tree const &type, void *)
20251 dw_die_ref die;
20253 die = lookup_type_die (type);
20254 if (die != NULL)
20255 die->die_perennial_p = 1;
20256 return true;
20259 /* Helper function of premark_types_used_by_global_vars which gets called
20260 through htab_traverse.
20262 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20263 marked as unused by prune_unused_types. The DIE of the type is marked
20264 only if the global variable using the type will actually be emitted. */
20267 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20268 void *)
20270 struct types_used_by_vars_entry *entry;
20271 dw_die_ref die;
20273 entry = (struct types_used_by_vars_entry *) *slot;
20274 gcc_assert (entry->type != NULL
20275 && entry->var_decl != NULL);
20276 die = lookup_type_die (entry->type);
20277 if (die)
20279 /* Ask cgraph if the global variable really is to be emitted.
20280 If yes, then we'll keep the DIE of ENTRY->TYPE. */
20281 varpool_node *node = varpool_node::get (entry->var_decl);
20282 if (node && node->definition)
20284 die->die_perennial_p = 1;
20285 /* Keep the parent DIEs as well. */
20286 while ((die = die->die_parent) && die->die_perennial_p == 0)
20287 die->die_perennial_p = 1;
20290 return 1;
20293 /* Mark all members of used_types_hash as perennial. */
20295 static void
20296 premark_used_types (struct function *fun)
20298 if (fun && fun->used_types_hash)
20299 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20302 /* Mark all members of types_used_by_vars_entry as perennial. */
20304 static void
20305 premark_types_used_by_global_vars (void)
20307 if (types_used_by_vars_hash)
20308 types_used_by_vars_hash
20309 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20312 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20313 for CA_LOC call arg loc node. */
20315 static dw_die_ref
20316 gen_call_site_die (tree decl, dw_die_ref subr_die,
20317 struct call_arg_loc_node *ca_loc)
20319 dw_die_ref stmt_die = NULL, die;
20320 tree block = ca_loc->block;
20322 while (block
20323 && block != DECL_INITIAL (decl)
20324 && TREE_CODE (block) == BLOCK)
20326 stmt_die = BLOCK_DIE (block);
20327 if (stmt_die)
20328 break;
20329 block = BLOCK_SUPERCONTEXT (block);
20331 if (stmt_die == NULL)
20332 stmt_die = subr_die;
20333 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20334 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20335 if (ca_loc->tail_call_p)
20336 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20337 if (ca_loc->symbol_ref)
20339 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20340 if (tdie)
20341 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20342 else
20343 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20345 return die;
20348 /* Generate a DIE to represent a declared function (either file-scope or
20349 block-local). */
20351 static void
20352 gen_subprogram_die (tree decl, dw_die_ref context_die)
20354 tree origin = decl_ultimate_origin (decl);
20355 dw_die_ref subr_die;
20356 dw_die_ref old_die = lookup_decl_die (decl);
20358 /* This function gets called multiple times for different stages of
20359 the debug process. For example, for func() in this code:
20361 namespace S
20363 void func() { ... }
20366 ...we get called 4 times. Twice in early debug and twice in
20367 late debug:
20369 Early debug
20370 -----------
20372 1. Once while generating func() within the namespace. This is
20373 the declaration. The declaration bit below is set, as the
20374 context is the namespace.
20376 A new DIE will be generated with DW_AT_declaration set.
20378 2. Once for func() itself. This is the specification. The
20379 declaration bit below is clear as the context is the CU.
20381 We will use the cached DIE from (1) to create a new DIE with
20382 DW_AT_specification pointing to the declaration in (1).
20384 Late debug via rest_of_handle_final()
20385 -------------------------------------
20387 3. Once generating func() within the namespace. This is also the
20388 declaration, as in (1), but this time we will early exit below
20389 as we have a cached DIE and a declaration needs no additional
20390 annotations (no locations), as the source declaration line
20391 info is enough.
20393 4. Once for func() itself. As in (2), this is the specification,
20394 but this time we will re-use the cached DIE, and just annotate
20395 it with the location information that should now be available.
20397 For something without namespaces, but with abstract instances, we
20398 are also called a multiple times:
20400 class Base
20402 public:
20403 Base (); // constructor declaration (1)
20406 Base::Base () { } // constructor specification (2)
20408 Early debug
20409 -----------
20411 1. Once for the Base() constructor by virtue of it being a
20412 member of the Base class. This is done via
20413 rest_of_type_compilation.
20415 This is a declaration, so a new DIE will be created with
20416 DW_AT_declaration.
20418 2. Once for the Base() constructor definition, but this time
20419 while generating the abstract instance of the base
20420 constructor (__base_ctor) which is being generated via early
20421 debug of reachable functions.
20423 Even though we have a cached version of the declaration (1),
20424 we will create a DW_AT_specification of the declaration DIE
20425 in (1).
20427 3. Once for the __base_ctor itself, but this time, we generate
20428 an DW_AT_abstract_origin version of the DW_AT_specification in
20429 (2).
20431 Late debug via rest_of_handle_final
20432 -----------------------------------
20434 4. One final time for the __base_ctor (which will have a cached
20435 DIE with DW_AT_abstract_origin created in (3). This time,
20436 we will just annotate the location information now
20437 available.
20439 int declaration = (current_function_decl != decl
20440 || class_or_namespace_scope_p (context_die));
20442 premark_used_types (DECL_STRUCT_FUNCTION (decl));
20444 /* Now that the C++ front end lazily declares artificial member fns, we
20445 might need to retrofit the declaration into its class. */
20446 if (!declaration && !origin && !old_die
20447 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20448 && !class_or_namespace_scope_p (context_die)
20449 && debug_info_level > DINFO_LEVEL_TERSE)
20450 old_die = force_decl_die (decl);
20452 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
20453 if (origin != NULL)
20455 gcc_assert (!declaration || local_scope_p (context_die));
20457 /* Fixup die_parent for the abstract instance of a nested
20458 inline function. */
20459 if (old_die && old_die->die_parent == NULL)
20460 add_child_die (context_die, old_die);
20462 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20464 /* If we have a DW_AT_abstract_origin we have a working
20465 cached version. */
20466 subr_die = old_die;
20468 else
20470 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20471 add_abstract_origin_attribute (subr_die, origin);
20472 /* This is where the actual code for a cloned function is.
20473 Let's emit linkage name attribute for it. This helps
20474 debuggers to e.g, set breakpoints into
20475 constructors/destructors when the user asks "break
20476 K::K". */
20477 add_linkage_name (subr_die, decl);
20480 /* A cached copy, possibly from early dwarf generation. Reuse as
20481 much as possible. */
20482 else if (old_die)
20484 /* A declaration that has been previously dumped needs no
20485 additional information. */
20486 if (declaration)
20487 return;
20489 if (!get_AT_flag (old_die, DW_AT_declaration)
20490 /* We can have a normal definition following an inline one in the
20491 case of redefinition of GNU C extern inlines.
20492 It seems reasonable to use AT_specification in this case. */
20493 && !get_AT (old_die, DW_AT_inline))
20495 /* Detect and ignore this case, where we are trying to output
20496 something we have already output. */
20497 if (get_AT (old_die, DW_AT_low_pc)
20498 || get_AT (old_die, DW_AT_ranges))
20499 return;
20501 /* If we have no location information, this must be a
20502 partially generated DIE from early dwarf generation.
20503 Fall through and generate it. */
20506 /* If the definition comes from the same place as the declaration,
20507 maybe use the old DIE. We always want the DIE for this function
20508 that has the *_pc attributes to be under comp_unit_die so the
20509 debugger can find it. We also need to do this for abstract
20510 instances of inlines, since the spec requires the out-of-line copy
20511 to have the same parent. For local class methods, this doesn't
20512 apply; we just use the old DIE. */
20513 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20514 struct dwarf_file_data * file_index = lookup_filename (s.file);
20515 if ((is_cu_die (old_die->die_parent)
20516 /* This condition fixes the inconsistency/ICE with the
20517 following Fortran test (or some derivative thereof) while
20518 building libgfortran:
20520 module some_m
20521 contains
20522 logical function funky (FLAG)
20523 funky = .true.
20524 end function
20525 end module
20527 || (old_die->die_parent
20528 && old_die->die_parent->die_tag == DW_TAG_module)
20529 || context_die == NULL)
20530 && (DECL_ARTIFICIAL (decl)
20531 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20532 && (get_AT_unsigned (old_die, DW_AT_decl_line)
20533 == (unsigned) s.line))))
20535 subr_die = old_die;
20537 /* Clear out the declaration attribute, but leave the
20538 parameters so they can be augmented with location
20539 information later. Unless this was a declaration, in
20540 which case, wipe out the nameless parameters and recreate
20541 them further down. */
20542 if (remove_AT (subr_die, DW_AT_declaration))
20545 remove_AT (subr_die, DW_AT_object_pointer);
20546 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20549 /* Make a specification pointing to the previously built
20550 declaration. */
20551 else
20553 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20554 add_AT_specification (subr_die, old_die);
20555 add_pubname (decl, subr_die);
20556 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20557 add_AT_file (subr_die, DW_AT_decl_file, file_index);
20558 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20559 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20561 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20562 emit the real type on the definition die. */
20563 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20565 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20566 if (die == auto_die || die == decltype_auto_die)
20567 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20568 TYPE_UNQUALIFIED, false, context_die);
20571 /* When we process the method declaration, we haven't seen
20572 the out-of-class defaulted definition yet, so we have to
20573 recheck now. */
20574 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20575 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict)
20576 && !get_AT (subr_die, DW_AT_defaulted))
20577 switch (defaulted)
20579 case 2:
20580 add_AT_unsigned (subr_die, DW_AT_defaulted,
20581 DW_DEFAULTED_out_of_class);
20582 break;
20584 case 1: /* This must have been handled before. */
20585 default:
20586 gcc_unreachable ();
20590 /* Create a fresh DIE for anything else. */
20591 else
20593 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20595 if (TREE_PUBLIC (decl))
20596 add_AT_flag (subr_die, DW_AT_external, 1);
20598 add_name_and_src_coords_attributes (subr_die, decl);
20599 add_pubname (decl, subr_die);
20600 if (debug_info_level > DINFO_LEVEL_TERSE)
20602 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20603 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20604 TYPE_UNQUALIFIED, false, context_die);
20607 add_pure_or_virtual_attribute (subr_die, decl);
20608 if (DECL_ARTIFICIAL (decl))
20609 add_AT_flag (subr_die, DW_AT_artificial, 1);
20611 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20612 add_AT_flag (subr_die, DW_AT_noreturn, 1);
20614 add_accessibility_attribute (subr_die, decl);
20617 /* Unless we have an existing non-declaration DIE, equate the new
20618 DIE. */
20619 if (!old_die || is_declaration_die (old_die))
20620 equate_decl_number_to_die (decl, subr_die);
20622 if (declaration)
20624 if (!old_die || !get_AT (old_die, DW_AT_inline))
20626 add_AT_flag (subr_die, DW_AT_declaration, 1);
20628 /* If this is an explicit function declaration then generate
20629 a DW_AT_explicit attribute. */
20630 if (lang_hooks.decls.function_decl_explicit_p (decl)
20631 && (dwarf_version >= 3 || !dwarf_strict))
20632 add_AT_flag (subr_die, DW_AT_explicit, 1);
20634 /* If this is a C++11 deleted special function member then generate
20635 a DW_AT_deleted attribute. */
20636 if (lang_hooks.decls.function_decl_deleted_p (decl)
20637 && (dwarf_version >= 5 || ! dwarf_strict))
20638 add_AT_flag (subr_die, DW_AT_deleted, 1);
20640 /* If this is a C++11 defaulted special function member then
20641 generate a DW_AT_GNU_defaulted attribute. */
20642 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20643 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict))
20644 switch (defaulted)
20646 case 1:
20647 add_AT_unsigned (subr_die, DW_AT_defaulted,
20648 DW_DEFAULTED_in_class);
20649 break;
20651 /* It is likely that this will never hit, since we
20652 don't have the out-of-class definition yet when we
20653 process the class definition and the method
20654 declaration. We recheck elsewhere, but leave it
20655 here just in case. */
20656 case 2:
20657 add_AT_unsigned (subr_die, DW_AT_defaulted,
20658 DW_DEFAULTED_out_of_class);
20659 break;
20661 default:
20662 gcc_unreachable ();
20666 /* Tag abstract instances with DW_AT_inline. */
20667 else if (DECL_ABSTRACT_P (decl))
20669 if (DECL_DECLARED_INLINE_P (decl))
20671 if (cgraph_function_possibly_inlined_p (decl))
20672 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20673 else
20674 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20676 else
20678 if (cgraph_function_possibly_inlined_p (decl))
20679 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20680 else
20681 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20684 if (DECL_DECLARED_INLINE_P (decl)
20685 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20686 add_AT_flag (subr_die, DW_AT_artificial, 1);
20688 /* For non DECL_EXTERNALs, if range information is available, fill
20689 the DIE with it. */
20690 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20692 HOST_WIDE_INT cfa_fb_offset;
20694 struct function *fun = DECL_STRUCT_FUNCTION (decl);
20696 if (!flag_reorder_blocks_and_partition)
20698 dw_fde_ref fde = fun->fde;
20699 if (fde->dw_fde_begin)
20701 /* We have already generated the labels. */
20702 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20703 fde->dw_fde_end, false);
20705 else
20707 /* Create start/end labels and add the range. */
20708 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20709 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20710 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20711 current_function_funcdef_no);
20712 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20713 current_function_funcdef_no);
20714 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20715 false);
20718 #if VMS_DEBUGGING_INFO
20719 /* HP OpenVMS Industry Standard 64: DWARF Extensions
20720 Section 2.3 Prologue and Epilogue Attributes:
20721 When a breakpoint is set on entry to a function, it is generally
20722 desirable for execution to be suspended, not on the very first
20723 instruction of the function, but rather at a point after the
20724 function's frame has been set up, after any language defined local
20725 declaration processing has been completed, and before execution of
20726 the first statement of the function begins. Debuggers generally
20727 cannot properly determine where this point is. Similarly for a
20728 breakpoint set on exit from a function. The prologue and epilogue
20729 attributes allow a compiler to communicate the location(s) to use. */
20732 if (fde->dw_fde_vms_end_prologue)
20733 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20734 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20736 if (fde->dw_fde_vms_begin_epilogue)
20737 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20738 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20740 #endif
20743 else
20745 /* Generate pubnames entries for the split function code ranges. */
20746 dw_fde_ref fde = fun->fde;
20748 if (fde->dw_fde_second_begin)
20750 if (dwarf_version >= 3 || !dwarf_strict)
20752 /* We should use ranges for non-contiguous code section
20753 addresses. Use the actual code range for the initial
20754 section, since the HOT/COLD labels might precede an
20755 alignment offset. */
20756 bool range_list_added = false;
20757 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20758 fde->dw_fde_end, &range_list_added,
20759 false);
20760 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20761 fde->dw_fde_second_end,
20762 &range_list_added, false);
20763 if (range_list_added)
20764 add_ranges (NULL);
20766 else
20768 /* There is no real support in DW2 for this .. so we make
20769 a work-around. First, emit the pub name for the segment
20770 containing the function label. Then make and emit a
20771 simplified subprogram DIE for the second segment with the
20772 name pre-fixed by __hot/cold_sect_of_. We use the same
20773 linkage name for the second die so that gdb will find both
20774 sections when given "b foo". */
20775 const char *name = NULL;
20776 tree decl_name = DECL_NAME (decl);
20777 dw_die_ref seg_die;
20779 /* Do the 'primary' section. */
20780 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20781 fde->dw_fde_end, false);
20783 /* Build a minimal DIE for the secondary section. */
20784 seg_die = new_die (DW_TAG_subprogram,
20785 subr_die->die_parent, decl);
20787 if (TREE_PUBLIC (decl))
20788 add_AT_flag (seg_die, DW_AT_external, 1);
20790 if (decl_name != NULL
20791 && IDENTIFIER_POINTER (decl_name) != NULL)
20793 name = dwarf2_name (decl, 1);
20794 if (! DECL_ARTIFICIAL (decl))
20795 add_src_coords_attributes (seg_die, decl);
20797 add_linkage_name (seg_die, decl);
20799 gcc_assert (name != NULL);
20800 add_pure_or_virtual_attribute (seg_die, decl);
20801 if (DECL_ARTIFICIAL (decl))
20802 add_AT_flag (seg_die, DW_AT_artificial, 1);
20804 name = concat ("__second_sect_of_", name, NULL);
20805 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20806 fde->dw_fde_second_end, false);
20807 add_name_attribute (seg_die, name);
20808 if (want_pubnames ())
20809 add_pubname_string (name, seg_die);
20812 else
20813 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20814 false);
20817 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20819 /* We define the "frame base" as the function's CFA. This is more
20820 convenient for several reasons: (1) It's stable across the prologue
20821 and epilogue, which makes it better than just a frame pointer,
20822 (2) With dwarf3, there exists a one-byte encoding that allows us
20823 to reference the .debug_frame data by proxy, but failing that,
20824 (3) We can at least reuse the code inspection and interpretation
20825 code that determines the CFA position at various points in the
20826 function. */
20827 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20829 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20830 add_AT_loc (subr_die, DW_AT_frame_base, op);
20832 else
20834 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20835 if (list->dw_loc_next)
20836 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20837 else
20838 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20841 /* Compute a displacement from the "steady-state frame pointer" to
20842 the CFA. The former is what all stack slots and argument slots
20843 will reference in the rtl; the latter is what we've told the
20844 debugger about. We'll need to adjust all frame_base references
20845 by this displacement. */
20846 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20848 if (fun->static_chain_decl)
20850 /* DWARF requires here a location expression that computes the
20851 address of the enclosing subprogram's frame base. The machinery
20852 in tree-nested.c is supposed to store this specific address in the
20853 last field of the FRAME record. */
20854 const tree frame_type
20855 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20856 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20858 tree fb_expr
20859 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20860 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20861 fb_expr, fb_decl, NULL_TREE);
20863 add_AT_location_description (subr_die, DW_AT_static_link,
20864 loc_list_from_tree (fb_expr, 0, NULL));
20868 /* Generate child dies for template paramaters. */
20869 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20870 gen_generic_params_dies (decl);
20872 /* Now output descriptions of the arguments for this function. This gets
20873 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20874 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20875 `...' at the end of the formal parameter list. In order to find out if
20876 there was a trailing ellipsis or not, we must instead look at the type
20877 associated with the FUNCTION_DECL. This will be a node of type
20878 FUNCTION_TYPE. If the chain of type nodes hanging off of this
20879 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20880 an ellipsis at the end. */
20882 /* In the case where we are describing a mere function declaration, all we
20883 need to do here (and all we *can* do here) is to describe the *types* of
20884 its formal parameters. */
20885 if (debug_info_level <= DINFO_LEVEL_TERSE)
20887 else if (declaration)
20888 gen_formal_types_die (decl, subr_die);
20889 else
20891 /* Generate DIEs to represent all known formal parameters. */
20892 tree parm = DECL_ARGUMENTS (decl);
20893 tree generic_decl = early_dwarf
20894 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20895 tree generic_decl_parm = generic_decl
20896 ? DECL_ARGUMENTS (generic_decl)
20897 : NULL;
20898 auto_vec<dw_die_ref> string_types_vec;
20899 if (string_types == NULL)
20900 string_types = &string_types_vec;
20902 /* Now we want to walk the list of parameters of the function and
20903 emit their relevant DIEs.
20905 We consider the case of DECL being an instance of a generic function
20906 as well as it being a normal function.
20908 If DECL is an instance of a generic function we walk the
20909 parameters of the generic function declaration _and_ the parameters of
20910 DECL itself. This is useful because we want to emit specific DIEs for
20911 function parameter packs and those are declared as part of the
20912 generic function declaration. In that particular case,
20913 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20914 That DIE has children DIEs representing the set of arguments
20915 of the pack. Note that the set of pack arguments can be empty.
20916 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20917 children DIE.
20919 Otherwise, we just consider the parameters of DECL. */
20920 while (generic_decl_parm || parm)
20922 if (generic_decl_parm
20923 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20924 gen_formal_parameter_pack_die (generic_decl_parm,
20925 parm, subr_die,
20926 &parm);
20927 else if (parm && !POINTER_BOUNDS_P (parm))
20929 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20931 if (parm == DECL_ARGUMENTS (decl)
20932 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20933 && parm_die
20934 && (dwarf_version >= 3 || !dwarf_strict))
20935 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20937 parm = DECL_CHAIN (parm);
20939 else if (parm)
20940 parm = DECL_CHAIN (parm);
20942 if (generic_decl_parm)
20943 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20946 /* Decide whether we need an unspecified_parameters DIE at the end.
20947 There are 2 more cases to do this for: 1) the ansi ... declaration -
20948 this is detectable when the end of the arg list is not a
20949 void_type_node 2) an unprototyped function declaration (not a
20950 definition). This just means that we have no info about the
20951 parameters at all. */
20952 if (early_dwarf)
20954 if (prototype_p (TREE_TYPE (decl)))
20956 /* This is the prototyped case, check for.... */
20957 if (stdarg_p (TREE_TYPE (decl)))
20958 gen_unspecified_parameters_die (decl, subr_die);
20960 else if (DECL_INITIAL (decl) == NULL_TREE)
20961 gen_unspecified_parameters_die (decl, subr_die);
20964 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
20965 have DIEs. */
20966 if (string_types == &string_types_vec)
20968 adjust_string_types ();
20969 string_types = NULL;
20973 if (subr_die != old_die)
20974 /* Add the calling convention attribute if requested. */
20975 add_calling_convention_attribute (subr_die, decl);
20977 /* Output Dwarf info for all of the stuff within the body of the function
20978 (if it has one - it may be just a declaration).
20980 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20981 a function. This BLOCK actually represents the outermost binding contour
20982 for the function, i.e. the contour in which the function's formal
20983 parameters and labels get declared. Curiously, it appears that the front
20984 end doesn't actually put the PARM_DECL nodes for the current function onto
20985 the BLOCK_VARS list for this outer scope, but are strung off of the
20986 DECL_ARGUMENTS list for the function instead.
20988 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20989 the LABEL_DECL nodes for the function however, and we output DWARF info
20990 for those in decls_for_scope. Just within the `outer_scope' there will be
20991 a BLOCK node representing the function's outermost pair of curly braces,
20992 and any blocks used for the base and member initializers of a C++
20993 constructor function. */
20994 tree outer_scope = DECL_INITIAL (decl);
20995 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
20997 int call_site_note_count = 0;
20998 int tail_call_site_note_count = 0;
21000 /* Emit a DW_TAG_variable DIE for a named return value. */
21001 if (DECL_NAME (DECL_RESULT (decl)))
21002 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
21004 /* The first time through decls_for_scope we will generate the
21005 DIEs for the locals. The second time, we fill in the
21006 location info. */
21007 decls_for_scope (outer_scope, subr_die);
21009 if (call_arg_locations && !dwarf_strict)
21011 struct call_arg_loc_node *ca_loc;
21012 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
21014 dw_die_ref die = NULL;
21015 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
21016 rtx arg, next_arg;
21018 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
21019 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
21020 : NULL_RTX);
21021 arg; arg = next_arg)
21023 dw_loc_descr_ref reg, val;
21024 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
21025 dw_die_ref cdie, tdie = NULL;
21027 next_arg = XEXP (arg, 1);
21028 if (REG_P (XEXP (XEXP (arg, 0), 0))
21029 && next_arg
21030 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
21031 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
21032 && REGNO (XEXP (XEXP (arg, 0), 0))
21033 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
21034 next_arg = XEXP (next_arg, 1);
21035 if (mode == VOIDmode)
21037 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
21038 if (mode == VOIDmode)
21039 mode = GET_MODE (XEXP (arg, 0));
21041 if (mode == VOIDmode || mode == BLKmode)
21042 continue;
21043 /* Get dynamic information about call target only if we
21044 have no static information: we cannot generate both
21045 DW_AT_abstract_origin and DW_AT_GNU_call_site_target
21046 attributes. */
21047 if (ca_loc->symbol_ref == NULL_RTX)
21049 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
21051 tloc = XEXP (XEXP (arg, 0), 1);
21052 continue;
21054 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
21055 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
21057 tlocc = XEXP (XEXP (arg, 0), 1);
21058 continue;
21061 reg = NULL;
21062 if (REG_P (XEXP (XEXP (arg, 0), 0)))
21063 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
21064 VAR_INIT_STATUS_INITIALIZED);
21065 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
21067 rtx mem = XEXP (XEXP (arg, 0), 0);
21068 reg = mem_loc_descriptor (XEXP (mem, 0),
21069 get_address_mode (mem),
21070 GET_MODE (mem),
21071 VAR_INIT_STATUS_INITIALIZED);
21073 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
21074 == DEBUG_PARAMETER_REF)
21076 tree tdecl
21077 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
21078 tdie = lookup_decl_die (tdecl);
21079 if (tdie == NULL)
21080 continue;
21082 else
21083 continue;
21084 if (reg == NULL
21085 && GET_CODE (XEXP (XEXP (arg, 0), 0))
21086 != DEBUG_PARAMETER_REF)
21087 continue;
21088 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
21089 VOIDmode,
21090 VAR_INIT_STATUS_INITIALIZED);
21091 if (val == NULL)
21092 continue;
21093 if (die == NULL)
21094 die = gen_call_site_die (decl, subr_die, ca_loc);
21095 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
21096 NULL_TREE);
21097 if (reg != NULL)
21098 add_AT_loc (cdie, DW_AT_location, reg);
21099 else if (tdie != NULL)
21100 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
21101 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
21102 if (next_arg != XEXP (arg, 1))
21104 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
21105 if (mode == VOIDmode)
21106 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
21107 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
21108 0), 1),
21109 mode, VOIDmode,
21110 VAR_INIT_STATUS_INITIALIZED);
21111 if (val != NULL)
21112 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
21115 if (die == NULL
21116 && (ca_loc->symbol_ref || tloc))
21117 die = gen_call_site_die (decl, subr_die, ca_loc);
21118 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
21120 dw_loc_descr_ref tval = NULL;
21122 if (tloc != NULL_RTX)
21123 tval = mem_loc_descriptor (tloc,
21124 GET_MODE (tloc) == VOIDmode
21125 ? Pmode : GET_MODE (tloc),
21126 VOIDmode,
21127 VAR_INIT_STATUS_INITIALIZED);
21128 if (tval)
21129 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
21130 else if (tlocc != NULL_RTX)
21132 tval = mem_loc_descriptor (tlocc,
21133 GET_MODE (tlocc) == VOIDmode
21134 ? Pmode : GET_MODE (tlocc),
21135 VOIDmode,
21136 VAR_INIT_STATUS_INITIALIZED);
21137 if (tval)
21138 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
21139 tval);
21142 if (die != NULL)
21144 call_site_note_count++;
21145 if (ca_loc->tail_call_p)
21146 tail_call_site_note_count++;
21150 call_arg_locations = NULL;
21151 call_arg_loc_last = NULL;
21152 if (tail_call_site_count >= 0
21153 && tail_call_site_count == tail_call_site_note_count
21154 && !dwarf_strict)
21156 if (call_site_count >= 0
21157 && call_site_count == call_site_note_count)
21158 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
21159 else
21160 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
21162 call_site_count = -1;
21163 tail_call_site_count = -1;
21167 /* Returns a hash value for X (which really is a die_struct). */
21169 hashval_t
21170 block_die_hasher::hash (die_struct *d)
21172 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
21175 /* Return nonzero if decl_id and die_parent of die_struct X is the same
21176 as decl_id and die_parent of die_struct Y. */
21178 bool
21179 block_die_hasher::equal (die_struct *x, die_struct *y)
21181 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
21184 /* Return TRUE if DECL, which may have been previously generated as
21185 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
21186 true if decl (or its origin) is either an extern declaration or a
21187 class/namespace scoped declaration.
21189 The declare_in_namespace support causes us to get two DIEs for one
21190 variable, both of which are declarations. We want to avoid
21191 considering one to be a specification, so we must test for
21192 DECLARATION and DW_AT_declaration. */
21193 static inline bool
21194 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
21196 return (old_die && TREE_STATIC (decl) && !declaration
21197 && get_AT_flag (old_die, DW_AT_declaration) == 1);
21200 /* Return true if DECL is a local static. */
21202 static inline bool
21203 local_function_static (tree decl)
21205 gcc_assert (TREE_CODE (decl) == VAR_DECL);
21206 return TREE_STATIC (decl)
21207 && DECL_CONTEXT (decl)
21208 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
21211 /* Generate a DIE to represent a declared data object.
21212 Either DECL or ORIGIN must be non-null. */
21214 static void
21215 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
21217 HOST_WIDE_INT off = 0;
21218 tree com_decl;
21219 tree decl_or_origin = decl ? decl : origin;
21220 tree ultimate_origin;
21221 dw_die_ref var_die;
21222 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
21223 dw_die_ref origin_die = NULL;
21224 bool declaration = (DECL_EXTERNAL (decl_or_origin)
21225 || class_or_namespace_scope_p (context_die));
21226 bool specialization_p = false;
21228 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21229 if (decl || ultimate_origin)
21230 origin = ultimate_origin;
21231 com_decl = fortran_common (decl_or_origin, &off);
21233 /* Symbol in common gets emitted as a child of the common block, in the form
21234 of a data member. */
21235 if (com_decl)
21237 dw_die_ref com_die;
21238 dw_loc_list_ref loc;
21239 die_node com_die_arg;
21241 var_die = lookup_decl_die (decl_or_origin);
21242 if (var_die)
21244 if (get_AT (var_die, DW_AT_location) == NULL)
21246 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21247 if (loc)
21249 if (off)
21251 /* Optimize the common case. */
21252 if (single_element_loc_list_p (loc)
21253 && loc->expr->dw_loc_opc == DW_OP_addr
21254 && loc->expr->dw_loc_next == NULL
21255 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21256 == SYMBOL_REF)
21258 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21259 loc->expr->dw_loc_oprnd1.v.val_addr
21260 = plus_constant (GET_MODE (x), x , off);
21262 else
21263 loc_list_plus_const (loc, off);
21265 add_AT_location_description (var_die, DW_AT_location, loc);
21266 remove_AT (var_die, DW_AT_declaration);
21269 return;
21272 if (common_block_die_table == NULL)
21273 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21275 com_die_arg.decl_id = DECL_UID (com_decl);
21276 com_die_arg.die_parent = context_die;
21277 com_die = common_block_die_table->find (&com_die_arg);
21278 loc = loc_list_from_tree (com_decl, 2, NULL);
21279 if (com_die == NULL)
21281 const char *cnam
21282 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21283 die_node **slot;
21285 com_die = new_die (DW_TAG_common_block, context_die, decl);
21286 add_name_and_src_coords_attributes (com_die, com_decl);
21287 if (loc)
21289 add_AT_location_description (com_die, DW_AT_location, loc);
21290 /* Avoid sharing the same loc descriptor between
21291 DW_TAG_common_block and DW_TAG_variable. */
21292 loc = loc_list_from_tree (com_decl, 2, NULL);
21294 else if (DECL_EXTERNAL (decl_or_origin))
21295 add_AT_flag (com_die, DW_AT_declaration, 1);
21296 if (want_pubnames ())
21297 add_pubname_string (cnam, com_die); /* ??? needed? */
21298 com_die->decl_id = DECL_UID (com_decl);
21299 slot = common_block_die_table->find_slot (com_die, INSERT);
21300 *slot = com_die;
21302 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21304 add_AT_location_description (com_die, DW_AT_location, loc);
21305 loc = loc_list_from_tree (com_decl, 2, NULL);
21306 remove_AT (com_die, DW_AT_declaration);
21308 var_die = new_die (DW_TAG_variable, com_die, decl);
21309 add_name_and_src_coords_attributes (var_die, decl_or_origin);
21310 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21311 decl_quals (decl_or_origin), false,
21312 context_die);
21313 add_AT_flag (var_die, DW_AT_external, 1);
21314 if (loc)
21316 if (off)
21318 /* Optimize the common case. */
21319 if (single_element_loc_list_p (loc)
21320 && loc->expr->dw_loc_opc == DW_OP_addr
21321 && loc->expr->dw_loc_next == NULL
21322 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21324 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21325 loc->expr->dw_loc_oprnd1.v.val_addr
21326 = plus_constant (GET_MODE (x), x, off);
21328 else
21329 loc_list_plus_const (loc, off);
21331 add_AT_location_description (var_die, DW_AT_location, loc);
21333 else if (DECL_EXTERNAL (decl_or_origin))
21334 add_AT_flag (var_die, DW_AT_declaration, 1);
21335 if (decl)
21336 equate_decl_number_to_die (decl, var_die);
21337 return;
21340 if (old_die)
21342 if (declaration)
21344 /* A declaration that has been previously dumped, needs no
21345 further annotations, since it doesn't need location on
21346 the second pass. */
21347 return;
21349 else if (decl_will_get_specification_p (old_die, decl, declaration)
21350 && !get_AT (old_die, DW_AT_specification))
21352 /* Fall-thru so we can make a new variable die along with a
21353 DW_AT_specification. */
21355 else if (origin && old_die->die_parent != context_die)
21357 /* If we will be creating an inlined instance, we need a
21358 new DIE that will get annotated with
21359 DW_AT_abstract_origin. Clear things so we can get a
21360 new DIE. */
21361 gcc_assert (!DECL_ABSTRACT_P (decl));
21362 old_die = NULL;
21364 else
21366 /* If a DIE was dumped early, it still needs location info.
21367 Skip to where we fill the location bits. */
21368 var_die = old_die;
21369 goto gen_variable_die_location;
21373 /* For static data members, the declaration in the class is supposed
21374 to have DW_TAG_member tag; the specification should still be
21375 DW_TAG_variable referencing the DW_TAG_member DIE. */
21376 if (declaration && class_scope_p (context_die))
21377 var_die = new_die (DW_TAG_member, context_die, decl);
21378 else
21379 var_die = new_die (DW_TAG_variable, context_die, decl);
21381 if (origin != NULL)
21382 origin_die = add_abstract_origin_attribute (var_die, origin);
21384 /* Loop unrolling can create multiple blocks that refer to the same
21385 static variable, so we must test for the DW_AT_declaration flag.
21387 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21388 copy decls and set the DECL_ABSTRACT_P flag on them instead of
21389 sharing them.
21391 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
21392 else if (decl_will_get_specification_p (old_die, decl, declaration))
21394 /* This is a definition of a C++ class level static. */
21395 add_AT_specification (var_die, old_die);
21396 specialization_p = true;
21397 if (DECL_NAME (decl))
21399 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21400 struct dwarf_file_data * file_index = lookup_filename (s.file);
21402 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21403 add_AT_file (var_die, DW_AT_decl_file, file_index);
21405 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21406 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21408 if (old_die->die_tag == DW_TAG_member)
21409 add_linkage_name (var_die, decl);
21412 else
21413 add_name_and_src_coords_attributes (var_die, decl);
21415 if ((origin == NULL && !specialization_p)
21416 || (origin != NULL
21417 && !DECL_ABSTRACT_P (decl_or_origin)
21418 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21419 decl_function_context
21420 (decl_or_origin))))
21422 tree type = TREE_TYPE (decl_or_origin);
21424 if (decl_by_reference_p (decl_or_origin))
21425 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21426 context_die);
21427 else
21428 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21429 context_die);
21432 if (origin == NULL && !specialization_p)
21434 if (TREE_PUBLIC (decl))
21435 add_AT_flag (var_die, DW_AT_external, 1);
21437 if (DECL_ARTIFICIAL (decl))
21438 add_AT_flag (var_die, DW_AT_artificial, 1);
21440 add_accessibility_attribute (var_die, decl);
21443 if (declaration)
21444 add_AT_flag (var_die, DW_AT_declaration, 1);
21446 if (decl && (DECL_ABSTRACT_P (decl)
21447 || !old_die || is_declaration_die (old_die)))
21448 equate_decl_number_to_die (decl, var_die);
21450 gen_variable_die_location:
21451 if (! declaration
21452 && (! DECL_ABSTRACT_P (decl_or_origin)
21453 /* Local static vars are shared between all clones/inlines,
21454 so emit DW_AT_location on the abstract DIE if DECL_RTL is
21455 already set. */
21456 || (TREE_CODE (decl_or_origin) == VAR_DECL
21457 && TREE_STATIC (decl_or_origin)
21458 && DECL_RTL_SET_P (decl_or_origin)))
21459 /* When abstract origin already has DW_AT_location attribute, no need
21460 to add it again. */
21461 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21463 if (early_dwarf)
21464 add_pubname (decl_or_origin, var_die);
21465 else
21466 add_location_or_const_value_attribute (var_die, decl_or_origin,
21467 decl == NULL);
21469 else
21470 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21473 /* Generate a DIE to represent a named constant. */
21475 static void
21476 gen_const_die (tree decl, dw_die_ref context_die)
21478 dw_die_ref const_die;
21479 tree type = TREE_TYPE (decl);
21481 const_die = lookup_decl_die (decl);
21482 if (const_die)
21483 return;
21485 const_die = new_die (DW_TAG_constant, context_die, decl);
21486 equate_decl_number_to_die (decl, const_die);
21487 add_name_and_src_coords_attributes (const_die, decl);
21488 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21489 if (TREE_PUBLIC (decl))
21490 add_AT_flag (const_die, DW_AT_external, 1);
21491 if (DECL_ARTIFICIAL (decl))
21492 add_AT_flag (const_die, DW_AT_artificial, 1);
21493 tree_add_const_value_attribute_for_decl (const_die, decl);
21496 /* Generate a DIE to represent a label identifier. */
21498 static void
21499 gen_label_die (tree decl, dw_die_ref context_die)
21501 tree origin = decl_ultimate_origin (decl);
21502 dw_die_ref lbl_die = lookup_decl_die (decl);
21503 rtx insn;
21504 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21506 if (!lbl_die)
21508 lbl_die = new_die (DW_TAG_label, context_die, decl);
21509 equate_decl_number_to_die (decl, lbl_die);
21511 if (origin != NULL)
21512 add_abstract_origin_attribute (lbl_die, origin);
21513 else
21514 add_name_and_src_coords_attributes (lbl_die, decl);
21517 if (DECL_ABSTRACT_P (decl))
21518 equate_decl_number_to_die (decl, lbl_die);
21519 else
21521 insn = DECL_RTL_IF_SET (decl);
21523 /* Deleted labels are programmer specified labels which have been
21524 eliminated because of various optimizations. We still emit them
21525 here so that it is possible to put breakpoints on them. */
21526 if (insn
21527 && (LABEL_P (insn)
21528 || ((NOTE_P (insn)
21529 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21531 /* When optimization is enabled (via -O) some parts of the compiler
21532 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21533 represent source-level labels which were explicitly declared by
21534 the user. This really shouldn't be happening though, so catch
21535 it if it ever does happen. */
21536 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21538 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21539 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21541 else if (insn
21542 && NOTE_P (insn)
21543 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21544 && CODE_LABEL_NUMBER (insn) != -1)
21546 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21547 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21552 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
21553 attributes to the DIE for a block STMT, to describe where the inlined
21554 function was called from. This is similar to add_src_coords_attributes. */
21556 static inline void
21557 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21559 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21561 if (dwarf_version >= 3 || !dwarf_strict)
21563 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21564 add_AT_unsigned (die, DW_AT_call_line, s.line);
21569 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21570 Add low_pc and high_pc attributes to the DIE for a block STMT. */
21572 static inline void
21573 add_high_low_attributes (tree stmt, dw_die_ref die)
21575 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21577 if (BLOCK_FRAGMENT_CHAIN (stmt)
21578 && (dwarf_version >= 3 || !dwarf_strict))
21580 tree chain, superblock = NULL_TREE;
21581 dw_die_ref pdie;
21582 dw_attr_node *attr = NULL;
21584 if (inlined_function_outer_scope_p (stmt))
21586 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21587 BLOCK_NUMBER (stmt));
21588 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21591 /* Optimize duplicate .debug_ranges lists or even tails of
21592 lists. If this BLOCK has same ranges as its supercontext,
21593 lookup DW_AT_ranges attribute in the supercontext (and
21594 recursively so), verify that the ranges_table contains the
21595 right values and use it instead of adding a new .debug_range. */
21596 for (chain = stmt, pdie = die;
21597 BLOCK_SAME_RANGE (chain);
21598 chain = BLOCK_SUPERCONTEXT (chain))
21600 dw_attr_node *new_attr;
21602 pdie = pdie->die_parent;
21603 if (pdie == NULL)
21604 break;
21605 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21606 break;
21607 new_attr = get_AT (pdie, DW_AT_ranges);
21608 if (new_attr == NULL
21609 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21610 break;
21611 attr = new_attr;
21612 superblock = BLOCK_SUPERCONTEXT (chain);
21614 if (attr != NULL
21615 && (ranges_table[attr->dw_attr_val.v.val_offset
21616 / 2 / DWARF2_ADDR_SIZE].num
21617 == BLOCK_NUMBER (superblock))
21618 && BLOCK_FRAGMENT_CHAIN (superblock))
21620 unsigned long off = attr->dw_attr_val.v.val_offset
21621 / 2 / DWARF2_ADDR_SIZE;
21622 unsigned long supercnt = 0, thiscnt = 0;
21623 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21624 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21626 ++supercnt;
21627 gcc_checking_assert (ranges_table[off + supercnt].num
21628 == BLOCK_NUMBER (chain));
21630 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21631 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21632 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21633 ++thiscnt;
21634 gcc_assert (supercnt >= thiscnt);
21635 add_AT_range_list (die, DW_AT_ranges,
21636 ((off + supercnt - thiscnt)
21637 * 2 * DWARF2_ADDR_SIZE),
21638 false);
21639 return;
21642 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21644 chain = BLOCK_FRAGMENT_CHAIN (stmt);
21647 add_ranges (chain);
21648 chain = BLOCK_FRAGMENT_CHAIN (chain);
21650 while (chain);
21651 add_ranges (NULL);
21653 else
21655 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21656 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21657 BLOCK_NUMBER (stmt));
21658 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21659 BLOCK_NUMBER (stmt));
21660 add_AT_low_high_pc (die, label, label_high, false);
21664 /* Generate a DIE for a lexical block. */
21666 static void
21667 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21669 dw_die_ref old_die = BLOCK_DIE (stmt);
21670 dw_die_ref stmt_die = NULL;
21671 if (!old_die)
21673 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21674 BLOCK_DIE (stmt) = stmt_die;
21677 if (BLOCK_ABSTRACT (stmt))
21679 if (old_die)
21681 /* This must have been generated early and it won't even
21682 need location information since it's a DW_AT_inline
21683 function. */
21684 if (flag_checking)
21685 for (dw_die_ref c = context_die; c; c = c->die_parent)
21686 if (c->die_tag == DW_TAG_inlined_subroutine
21687 || c->die_tag == DW_TAG_subprogram)
21689 gcc_assert (get_AT (c, DW_AT_inline));
21690 break;
21692 return;
21695 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21697 /* If this is an inlined instance, create a new lexical die for
21698 anything below to attach DW_AT_abstract_origin to. */
21699 if (old_die)
21701 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21702 BLOCK_DIE (stmt) = stmt_die;
21703 old_die = NULL;
21706 tree origin = block_ultimate_origin (stmt);
21707 if (origin != NULL_TREE && origin != stmt)
21708 add_abstract_origin_attribute (stmt_die, origin);
21711 if (old_die)
21712 stmt_die = old_die;
21714 /* A non abstract block whose blocks have already been reordered
21715 should have the instruction range for this block. If so, set the
21716 high/low attributes. */
21717 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21719 gcc_assert (stmt_die);
21720 add_high_low_attributes (stmt, stmt_die);
21723 decls_for_scope (stmt, stmt_die);
21726 /* Generate a DIE for an inlined subprogram. */
21728 static void
21729 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21731 tree decl;
21733 /* The instance of function that is effectively being inlined shall not
21734 be abstract. */
21735 gcc_assert (! BLOCK_ABSTRACT (stmt));
21737 decl = block_ultimate_origin (stmt);
21739 /* Make sure any inlined functions are known to be inlineable. */
21740 gcc_checking_assert (DECL_ABSTRACT_P (decl)
21741 || cgraph_function_possibly_inlined_p (decl));
21743 /* Emit info for the abstract instance first, if we haven't yet. We
21744 must emit this even if the block is abstract, otherwise when we
21745 emit the block below (or elsewhere), we may end up trying to emit
21746 a die whose origin die hasn't been emitted, and crashing. */
21747 dwarf2out_abstract_function (decl);
21749 if (! BLOCK_ABSTRACT (stmt))
21751 dw_die_ref subr_die
21752 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21754 if (call_arg_locations)
21755 BLOCK_DIE (stmt) = subr_die;
21756 add_abstract_origin_attribute (subr_die, decl);
21757 if (TREE_ASM_WRITTEN (stmt))
21758 add_high_low_attributes (stmt, subr_die);
21759 add_call_src_coords_attributes (stmt, subr_die);
21761 decls_for_scope (stmt, subr_die);
21765 /* Generate a DIE for a field in a record, or structure. CTX is required: see
21766 the comment for VLR_CONTEXT. */
21768 static void
21769 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21771 dw_die_ref decl_die;
21773 if (TREE_TYPE (decl) == error_mark_node)
21774 return;
21776 decl_die = new_die (DW_TAG_member, context_die, decl);
21777 add_name_and_src_coords_attributes (decl_die, decl);
21778 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21779 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21780 context_die);
21782 if (DECL_BIT_FIELD_TYPE (decl))
21784 add_byte_size_attribute (decl_die, decl);
21785 add_bit_size_attribute (decl_die, decl);
21786 add_bit_offset_attribute (decl_die, decl, ctx);
21789 /* If we have a variant part offset, then we are supposed to process a member
21790 of a QUAL_UNION_TYPE, which is how we represent variant parts in
21791 trees. */
21792 gcc_assert (ctx->variant_part_offset == NULL_TREE
21793 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21794 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21795 add_data_member_location_attribute (decl_die, decl, ctx);
21797 if (DECL_ARTIFICIAL (decl))
21798 add_AT_flag (decl_die, DW_AT_artificial, 1);
21800 add_accessibility_attribute (decl_die, decl);
21802 /* Equate decl number to die, so that we can look up this decl later on. */
21803 equate_decl_number_to_die (decl, decl_die);
21806 #if 0
21807 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21808 Use modified_type_die instead.
21809 We keep this code here just in case these types of DIEs may be needed to
21810 represent certain things in other languages (e.g. Pascal) someday. */
21812 static void
21813 gen_pointer_type_die (tree type, dw_die_ref context_die)
21815 dw_die_ref ptr_die
21816 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21818 equate_type_number_to_die (type, ptr_die);
21819 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21820 context_die);
21821 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21824 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21825 Use modified_type_die instead.
21826 We keep this code here just in case these types of DIEs may be needed to
21827 represent certain things in other languages (e.g. Pascal) someday. */
21829 static void
21830 gen_reference_type_die (tree type, dw_die_ref context_die)
21832 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21834 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21835 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21836 else
21837 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21839 equate_type_number_to_die (type, ref_die);
21840 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21841 context_die);
21842 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21844 #endif
21846 /* Generate a DIE for a pointer to a member type. */
21848 static void
21849 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21851 dw_die_ref ptr_die
21852 = new_die (DW_TAG_ptr_to_member_type,
21853 scope_die_for (type, context_die), type);
21855 equate_type_number_to_die (type, ptr_die);
21856 add_AT_die_ref (ptr_die, DW_AT_containing_type,
21857 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21858 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21859 context_die);
21862 static char *producer_string;
21864 /* Return a heap allocated producer string including command line options
21865 if -grecord-gcc-switches. */
21867 static char *
21868 gen_producer_string (void)
21870 size_t j;
21871 auto_vec<const char *> switches;
21872 const char *language_string = lang_hooks.name;
21873 char *producer, *tail;
21874 const char *p;
21875 size_t len = dwarf_record_gcc_switches ? 0 : 3;
21876 size_t plen = strlen (language_string) + 1 + strlen (version_string);
21878 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21879 switch (save_decoded_options[j].opt_index)
21881 case OPT_o:
21882 case OPT_d:
21883 case OPT_dumpbase:
21884 case OPT_dumpdir:
21885 case OPT_auxbase:
21886 case OPT_auxbase_strip:
21887 case OPT_quiet:
21888 case OPT_version:
21889 case OPT_v:
21890 case OPT_w:
21891 case OPT_L:
21892 case OPT_D:
21893 case OPT_I:
21894 case OPT_U:
21895 case OPT_SPECIAL_unknown:
21896 case OPT_SPECIAL_ignore:
21897 case OPT_SPECIAL_program_name:
21898 case OPT_SPECIAL_input_file:
21899 case OPT_grecord_gcc_switches:
21900 case OPT_gno_record_gcc_switches:
21901 case OPT__output_pch_:
21902 case OPT_fdiagnostics_show_location_:
21903 case OPT_fdiagnostics_show_option:
21904 case OPT_fdiagnostics_show_caret:
21905 case OPT_fdiagnostics_color_:
21906 case OPT_fverbose_asm:
21907 case OPT____:
21908 case OPT__sysroot_:
21909 case OPT_nostdinc:
21910 case OPT_nostdinc__:
21911 case OPT_fpreprocessed:
21912 case OPT_fltrans_output_list_:
21913 case OPT_fresolution_:
21914 case OPT_fdebug_prefix_map_:
21915 /* Ignore these. */
21916 continue;
21917 default:
21918 if (cl_options[save_decoded_options[j].opt_index].flags
21919 & CL_NO_DWARF_RECORD)
21920 continue;
21921 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21922 == '-');
21923 switch (save_decoded_options[j].canonical_option[0][1])
21925 case 'M':
21926 case 'i':
21927 case 'W':
21928 continue;
21929 case 'f':
21930 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21931 "dump", 4) == 0)
21932 continue;
21933 break;
21934 default:
21935 break;
21937 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21938 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21939 break;
21942 producer = XNEWVEC (char, plen + 1 + len + 1);
21943 tail = producer;
21944 sprintf (tail, "%s %s", language_string, version_string);
21945 tail += plen;
21947 FOR_EACH_VEC_ELT (switches, j, p)
21949 len = strlen (p);
21950 *tail = ' ';
21951 memcpy (tail + 1, p, len);
21952 tail += len + 1;
21955 *tail = '\0';
21956 return producer;
21959 /* Given a C and/or C++ language/version string return the "highest".
21960 C++ is assumed to be "higher" than C in this case. Used for merging
21961 LTO translation unit languages. */
21962 static const char *
21963 highest_c_language (const char *lang1, const char *lang2)
21965 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21966 return "GNU C++14";
21967 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21968 return "GNU C++11";
21969 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21970 return "GNU C++98";
21972 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21973 return "GNU C11";
21974 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
21975 return "GNU C99";
21976 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
21977 return "GNU C89";
21979 gcc_unreachable ();
21983 /* Generate the DIE for the compilation unit. */
21985 static dw_die_ref
21986 gen_compile_unit_die (const char *filename)
21988 dw_die_ref die;
21989 const char *language_string = lang_hooks.name;
21990 int language;
21992 die = new_die (DW_TAG_compile_unit, NULL, NULL);
21994 if (filename)
21996 add_name_attribute (die, filename);
21997 /* Don't add cwd for <built-in>. */
21998 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21999 add_comp_dir_attribute (die);
22002 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
22004 /* If our producer is LTO try to figure out a common language to use
22005 from the global list of translation units. */
22006 if (strcmp (language_string, "GNU GIMPLE") == 0)
22008 unsigned i;
22009 tree t;
22010 const char *common_lang = NULL;
22012 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
22014 if (!TRANSLATION_UNIT_LANGUAGE (t))
22015 continue;
22016 if (!common_lang)
22017 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
22018 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
22020 else if (strncmp (common_lang, "GNU C", 5) == 0
22021 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
22022 /* Mixing C and C++ is ok, use C++ in that case. */
22023 common_lang = highest_c_language (common_lang,
22024 TRANSLATION_UNIT_LANGUAGE (t));
22025 else
22027 /* Fall back to C. */
22028 common_lang = NULL;
22029 break;
22033 if (common_lang)
22034 language_string = common_lang;
22037 language = DW_LANG_C;
22038 if (strncmp (language_string, "GNU C", 5) == 0
22039 && ISDIGIT (language_string[5]))
22041 language = DW_LANG_C89;
22042 if (dwarf_version >= 3 || !dwarf_strict)
22044 if (strcmp (language_string, "GNU C89") != 0)
22045 language = DW_LANG_C99;
22047 if (dwarf_version >= 5 /* || !dwarf_strict */)
22048 if (strcmp (language_string, "GNU C11") == 0)
22049 language = DW_LANG_C11;
22052 else if (strncmp (language_string, "GNU C++", 7) == 0)
22054 language = DW_LANG_C_plus_plus;
22055 if (dwarf_version >= 5 /* || !dwarf_strict */)
22057 if (strcmp (language_string, "GNU C++11") == 0)
22058 language = DW_LANG_C_plus_plus_11;
22059 else if (strcmp (language_string, "GNU C++14") == 0)
22060 language = DW_LANG_C_plus_plus_14;
22063 else if (strcmp (language_string, "GNU F77") == 0)
22064 language = DW_LANG_Fortran77;
22065 else if (strcmp (language_string, "GNU Pascal") == 0)
22066 language = DW_LANG_Pascal83;
22067 else if (dwarf_version >= 3 || !dwarf_strict)
22069 if (strcmp (language_string, "GNU Ada") == 0)
22070 language = DW_LANG_Ada95;
22071 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22073 language = DW_LANG_Fortran95;
22074 if (dwarf_version >= 5 /* || !dwarf_strict */)
22076 if (strcmp (language_string, "GNU Fortran2003") == 0)
22077 language = DW_LANG_Fortran03;
22078 else if (strcmp (language_string, "GNU Fortran2008") == 0)
22079 language = DW_LANG_Fortran08;
22082 else if (strcmp (language_string, "GNU Java") == 0)
22083 language = DW_LANG_Java;
22084 else if (strcmp (language_string, "GNU Objective-C") == 0)
22085 language = DW_LANG_ObjC;
22086 else if (strcmp (language_string, "GNU Objective-C++") == 0)
22087 language = DW_LANG_ObjC_plus_plus;
22088 else if (dwarf_version >= 5 || !dwarf_strict)
22090 if (strcmp (language_string, "GNU Go") == 0)
22091 language = DW_LANG_Go;
22094 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
22095 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22096 language = DW_LANG_Fortran90;
22098 add_AT_unsigned (die, DW_AT_language, language);
22100 switch (language)
22102 case DW_LANG_Fortran77:
22103 case DW_LANG_Fortran90:
22104 case DW_LANG_Fortran95:
22105 case DW_LANG_Fortran03:
22106 case DW_LANG_Fortran08:
22107 /* Fortran has case insensitive identifiers and the front-end
22108 lowercases everything. */
22109 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
22110 break;
22111 default:
22112 /* The default DW_ID_case_sensitive doesn't need to be specified. */
22113 break;
22115 return die;
22118 /* Generate the DIE for a base class. */
22120 static void
22121 gen_inheritance_die (tree binfo, tree access, tree type,
22122 dw_die_ref context_die)
22124 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
22125 struct vlr_context ctx = { type, NULL };
22127 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
22128 context_die);
22129 add_data_member_location_attribute (die, binfo, &ctx);
22131 if (BINFO_VIRTUAL_P (binfo))
22132 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22134 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
22135 children, otherwise the default is DW_ACCESS_public. In DWARF2
22136 the default has always been DW_ACCESS_private. */
22137 if (access == access_public_node)
22139 if (dwarf_version == 2
22140 || context_die->die_tag == DW_TAG_class_type)
22141 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
22143 else if (access == access_protected_node)
22144 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
22145 else if (dwarf_version > 2
22146 && context_die->die_tag != DW_TAG_class_type)
22147 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
22150 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
22151 structure. */
22152 static bool
22153 is_variant_part (tree decl)
22155 return (TREE_CODE (decl) == FIELD_DECL
22156 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
22159 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
22160 return the FIELD_DECL. Return NULL_TREE otherwise. */
22162 static tree
22163 analyze_discr_in_predicate (tree operand, tree struct_type)
22165 bool continue_stripping = true;
22166 while (continue_stripping)
22167 switch (TREE_CODE (operand))
22169 CASE_CONVERT:
22170 operand = TREE_OPERAND (operand, 0);
22171 break;
22172 default:
22173 continue_stripping = false;
22174 break;
22177 /* Match field access to members of struct_type only. */
22178 if (TREE_CODE (operand) == COMPONENT_REF
22179 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
22180 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
22181 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
22182 return TREE_OPERAND (operand, 1);
22183 else
22184 return NULL_TREE;
22187 /* Check that SRC is a constant integer that can be represented as a native
22188 integer constant (either signed or unsigned). If so, store it into DEST and
22189 return true. Return false otherwise. */
22191 static bool
22192 get_discr_value (tree src, dw_discr_value *dest)
22194 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
22196 if (TREE_CODE (src) != INTEGER_CST
22197 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
22198 return false;
22200 dest->pos = is_unsigned;
22201 if (is_unsigned)
22202 dest->v.uval = tree_to_uhwi (src);
22203 else
22204 dest->v.sval = tree_to_shwi (src);
22206 return true;
22209 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
22210 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
22211 store NULL_TREE in DISCR_DECL. Otherwise:
22213 - store the discriminant field in STRUCT_TYPE that controls the variant
22214 part to *DISCR_DECL
22216 - put in *DISCR_LISTS_P an array where for each variant, the item
22217 represents the corresponding matching list of discriminant values.
22219 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
22220 the above array.
22222 Note that when the array is allocated (i.e. when the analysis is
22223 successful), it is up to the caller to free the array. */
22225 static void
22226 analyze_variants_discr (tree variant_part_decl,
22227 tree struct_type,
22228 tree *discr_decl,
22229 dw_discr_list_ref **discr_lists_p,
22230 unsigned *discr_lists_length)
22232 tree variant_part_type = TREE_TYPE (variant_part_decl);
22233 tree variant;
22234 dw_discr_list_ref *discr_lists;
22235 unsigned i;
22237 /* Compute how many variants there are in this variant part. */
22238 *discr_lists_length = 0;
22239 for (variant = TYPE_FIELDS (variant_part_type);
22240 variant != NULL_TREE;
22241 variant = DECL_CHAIN (variant))
22242 ++*discr_lists_length;
22244 *discr_decl = NULL_TREE;
22245 *discr_lists_p
22246 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22247 sizeof (**discr_lists_p));
22248 discr_lists = *discr_lists_p;
22250 /* And then analyze all variants to extract discriminant information for all
22251 of them. This analysis is conservative: as soon as we detect something we
22252 do not support, abort everything and pretend we found nothing. */
22253 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22254 variant != NULL_TREE;
22255 variant = DECL_CHAIN (variant), ++i)
22257 tree match_expr = DECL_QUALIFIER (variant);
22259 /* Now, try to analyze the predicate and deduce a discriminant for
22260 it. */
22261 if (match_expr == boolean_true_node)
22262 /* Typically happens for the default variant: it matches all cases that
22263 previous variants rejected. Don't output any matching value for
22264 this one. */
22265 continue;
22267 /* The following loop tries to iterate over each discriminant
22268 possibility: single values or ranges. */
22269 while (match_expr != NULL_TREE)
22271 tree next_round_match_expr;
22272 tree candidate_discr = NULL_TREE;
22273 dw_discr_list_ref new_node = NULL;
22275 /* Possibilities are matched one after the other by nested
22276 TRUTH_ORIF_EXPR expressions. Process the current possibility and
22277 continue with the rest at next iteration. */
22278 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22280 next_round_match_expr = TREE_OPERAND (match_expr, 0);
22281 match_expr = TREE_OPERAND (match_expr, 1);
22283 else
22284 next_round_match_expr = NULL_TREE;
22286 if (match_expr == boolean_false_node)
22287 /* This sub-expression matches nothing: just wait for the next
22288 one. */
22291 else if (TREE_CODE (match_expr) == EQ_EXPR)
22293 /* We are matching: <discr_field> == <integer_cst>
22294 This sub-expression matches a single value. */
22295 tree integer_cst = TREE_OPERAND (match_expr, 1);
22297 candidate_discr
22298 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22299 struct_type);
22301 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22302 if (!get_discr_value (integer_cst,
22303 &new_node->dw_discr_lower_bound))
22304 goto abort;
22305 new_node->dw_discr_range = false;
22308 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22310 /* We are matching:
22311 <discr_field> > <integer_cst>
22312 && <discr_field> < <integer_cst>.
22313 This sub-expression matches the range of values between the
22314 two matched integer constants. Note that comparisons can be
22315 inclusive or exclusive. */
22316 tree candidate_discr_1, candidate_discr_2;
22317 tree lower_cst, upper_cst;
22318 bool lower_cst_included, upper_cst_included;
22319 tree lower_op = TREE_OPERAND (match_expr, 0);
22320 tree upper_op = TREE_OPERAND (match_expr, 1);
22322 /* When the comparison is exclusive, the integer constant is not
22323 the discriminant range bound we are looking for: we will have
22324 to increment or decrement it. */
22325 if (TREE_CODE (lower_op) == GE_EXPR)
22326 lower_cst_included = true;
22327 else if (TREE_CODE (lower_op) == GT_EXPR)
22328 lower_cst_included = false;
22329 else
22330 goto abort;
22332 if (TREE_CODE (upper_op) == LE_EXPR)
22333 upper_cst_included = true;
22334 else if (TREE_CODE (upper_op) == LT_EXPR)
22335 upper_cst_included = false;
22336 else
22337 goto abort;
22339 /* Extract the discriminant from the first operand and check it
22340 is consistant with the same analysis in the second
22341 operand. */
22342 candidate_discr_1
22343 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22344 struct_type);
22345 candidate_discr_2
22346 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22347 struct_type);
22348 if (candidate_discr_1 == candidate_discr_2)
22349 candidate_discr = candidate_discr_1;
22350 else
22351 goto abort;
22353 /* Extract bounds from both. */
22354 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22355 lower_cst = TREE_OPERAND (lower_op, 1);
22356 upper_cst = TREE_OPERAND (upper_op, 1);
22358 if (!lower_cst_included)
22359 lower_cst
22360 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22361 lower_cst,
22362 build_int_cst (TREE_TYPE (lower_cst), 1)));
22363 if (!upper_cst_included)
22364 upper_cst
22365 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22366 upper_cst,
22367 build_int_cst (TREE_TYPE (upper_cst), 1)));
22369 if (!get_discr_value (lower_cst,
22370 &new_node->dw_discr_lower_bound)
22371 || !get_discr_value (upper_cst,
22372 &new_node->dw_discr_upper_bound))
22373 goto abort;
22375 new_node->dw_discr_range = true;
22378 else
22379 /* Unsupported sub-expression: we cannot determine the set of
22380 matching discriminant values. Abort everything. */
22381 goto abort;
22383 /* If the discriminant info is not consistant with what we saw so
22384 far, consider the analysis failed and abort everything. */
22385 if (candidate_discr == NULL_TREE
22386 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22387 goto abort;
22388 else
22389 *discr_decl = candidate_discr;
22391 if (new_node != NULL)
22393 new_node->dw_discr_next = discr_lists[i];
22394 discr_lists[i] = new_node;
22396 match_expr = next_round_match_expr;
22400 /* If we reach this point, we could match everything we were interested
22401 in. */
22402 return;
22404 abort:
22405 /* Clean all data structure and return no result. */
22406 free (*discr_lists_p);
22407 *discr_lists_p = NULL;
22408 *discr_decl = NULL_TREE;
22411 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22412 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
22413 under CONTEXT_DIE.
22415 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22416 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
22417 this type, which are record types, represent the available variants and each
22418 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
22419 values are inferred from these attributes.
22421 In trees, the offsets for the fields inside these sub-records are relative
22422 to the variant part itself, whereas the corresponding DIEs should have
22423 offset attributes that are relative to the embedding record base address.
22424 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22425 must be an expression that computes the offset of the variant part to
22426 describe in DWARF. */
22428 static void
22429 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22430 dw_die_ref context_die)
22432 const tree variant_part_type = TREE_TYPE (variant_part_decl);
22433 tree variant_part_offset = vlr_ctx->variant_part_offset;
22434 struct loc_descr_context ctx = {
22435 vlr_ctx->struct_type, /* context_type */
22436 NULL_TREE, /* base_decl */
22437 NULL /* dpi */
22440 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22441 NULL_TREE if there is no such field. */
22442 tree discr_decl = NULL_TREE;
22443 dw_discr_list_ref *discr_lists;
22444 unsigned discr_lists_length = 0;
22445 unsigned i;
22447 dw_die_ref dwarf_proc_die = NULL;
22448 dw_die_ref variant_part_die
22449 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22451 equate_decl_number_to_die (variant_part_decl, variant_part_die);
22453 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22454 &discr_decl, &discr_lists, &discr_lists_length);
22456 if (discr_decl != NULL_TREE)
22458 dw_die_ref discr_die = lookup_decl_die (discr_decl);
22460 if (discr_die)
22461 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22462 else
22463 /* We have no DIE for the discriminant, so just discard all
22464 discrimimant information in the output. */
22465 discr_decl = NULL_TREE;
22468 /* If the offset for this variant part is more complex than a constant,
22469 create a DWARF procedure for it so that we will not have to generate DWARF
22470 expressions for it for each member. */
22471 if (TREE_CODE (variant_part_offset) != INTEGER_CST
22472 && (dwarf_version >= 3 || !dwarf_strict))
22474 const tree dwarf_proc_fndecl
22475 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22476 build_function_type (TREE_TYPE (variant_part_offset),
22477 NULL_TREE));
22478 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22479 const dw_loc_descr_ref dwarf_proc_body
22480 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22482 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22483 dwarf_proc_fndecl, context_die);
22484 if (dwarf_proc_die != NULL)
22485 variant_part_offset = dwarf_proc_call;
22488 /* Output DIEs for all variants. */
22489 i = 0;
22490 for (tree variant = TYPE_FIELDS (variant_part_type);
22491 variant != NULL_TREE;
22492 variant = DECL_CHAIN (variant), ++i)
22494 tree variant_type = TREE_TYPE (variant);
22495 dw_die_ref variant_die;
22497 /* All variants (i.e. members of a variant part) are supposed to be
22498 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
22499 under these records. */
22500 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22502 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22503 equate_decl_number_to_die (variant, variant_die);
22505 /* Output discriminant values this variant matches, if any. */
22506 if (discr_decl == NULL || discr_lists[i] == NULL)
22507 /* In the case we have discriminant information at all, this is
22508 probably the default variant: as the standard says, don't
22509 output any discriminant value/list attribute. */
22511 else if (discr_lists[i]->dw_discr_next == NULL
22512 && !discr_lists[i]->dw_discr_range)
22513 /* If there is only one accepted value, don't bother outputting a
22514 list. */
22515 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22516 else
22517 add_discr_list (variant_die, discr_lists[i]);
22519 for (tree member = TYPE_FIELDS (variant_type);
22520 member != NULL_TREE;
22521 member = DECL_CHAIN (member))
22523 struct vlr_context vlr_sub_ctx = {
22524 vlr_ctx->struct_type, /* struct_type */
22525 NULL /* variant_part_offset */
22527 if (is_variant_part (member))
22529 /* All offsets for fields inside variant parts are relative to
22530 the top-level embedding RECORD_TYPE's base address. On the
22531 other hand, offsets in GCC's types are relative to the
22532 nested-most variant part. So we have to sum offsets each time
22533 we recurse. */
22535 vlr_sub_ctx.variant_part_offset
22536 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22537 variant_part_offset, byte_position (member)));
22538 gen_variant_part (member, &vlr_sub_ctx, variant_die);
22540 else
22542 vlr_sub_ctx.variant_part_offset = variant_part_offset;
22543 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22548 free (discr_lists);
22551 /* Generate a DIE for a class member. */
22553 static void
22554 gen_member_die (tree type, dw_die_ref context_die)
22556 tree member;
22557 tree binfo = TYPE_BINFO (type);
22558 dw_die_ref child;
22560 /* If this is not an incomplete type, output descriptions of each of its
22561 members. Note that as we output the DIEs necessary to represent the
22562 members of this record or union type, we will also be trying to output
22563 DIEs to represent the *types* of those members. However the `type'
22564 function (above) will specifically avoid generating type DIEs for member
22565 types *within* the list of member DIEs for this (containing) type except
22566 for those types (of members) which are explicitly marked as also being
22567 members of this (containing) type themselves. The g++ front- end can
22568 force any given type to be treated as a member of some other (containing)
22569 type by setting the TYPE_CONTEXT of the given (member) type to point to
22570 the TREE node representing the appropriate (containing) type. */
22572 /* First output info about the base classes. */
22573 if (binfo)
22575 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22576 int i;
22577 tree base;
22579 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22580 gen_inheritance_die (base,
22581 (accesses ? (*accesses)[i] : access_public_node),
22582 type,
22583 context_die);
22586 /* Now output info about the data members and type members. */
22587 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22589 struct vlr_context vlr_ctx = { type, NULL_TREE };
22591 /* If we thought we were generating minimal debug info for TYPE
22592 and then changed our minds, some of the member declarations
22593 may have already been defined. Don't define them again, but
22594 do put them in the right order. */
22596 child = lookup_decl_die (member);
22597 if (child)
22598 splice_child_die (context_die, child);
22600 /* Do not generate standard DWARF for variant parts if we are generating
22601 the corresponding GNAT encodings: DIEs generated for both would
22602 conflict in our mappings. */
22603 else if (is_variant_part (member)
22604 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22606 vlr_ctx.variant_part_offset = byte_position (member);
22607 gen_variant_part (member, &vlr_ctx, context_die);
22609 else
22611 vlr_ctx.variant_part_offset = NULL_TREE;
22612 gen_decl_die (member, NULL, &vlr_ctx, context_die);
22616 /* We do not keep type methods in type variants. */
22617 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22618 /* Now output info about the function members (if any). */
22619 if (TYPE_METHODS (type) != error_mark_node)
22620 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22622 /* Don't include clones in the member list. */
22623 if (DECL_ABSTRACT_ORIGIN (member))
22624 continue;
22625 /* Nor constructors for anonymous classes. */
22626 if (DECL_ARTIFICIAL (member)
22627 && dwarf2_name (member, 0) == NULL)
22628 continue;
22630 child = lookup_decl_die (member);
22631 if (child)
22632 splice_child_die (context_die, child);
22633 else
22634 gen_decl_die (member, NULL, NULL, context_die);
22638 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
22639 is set, we pretend that the type was never defined, so we only get the
22640 member DIEs needed by later specification DIEs. */
22642 static void
22643 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22644 enum debug_info_usage usage)
22646 if (TREE_ASM_WRITTEN (type))
22648 /* Fill in the bound of variable-length fields in late dwarf if
22649 still incomplete. */
22650 if (!early_dwarf && variably_modified_type_p (type, NULL))
22651 for (tree member = TYPE_FIELDS (type);
22652 member;
22653 member = DECL_CHAIN (member))
22654 fill_variable_array_bounds (TREE_TYPE (member));
22655 return;
22658 dw_die_ref type_die = lookup_type_die (type);
22659 dw_die_ref scope_die = 0;
22660 int nested = 0;
22661 int complete = (TYPE_SIZE (type)
22662 && (! TYPE_STUB_DECL (type)
22663 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22664 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22665 complete = complete && should_emit_struct_debug (type, usage);
22667 if (type_die && ! complete)
22668 return;
22670 if (TYPE_CONTEXT (type) != NULL_TREE
22671 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22672 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22673 nested = 1;
22675 scope_die = scope_die_for (type, context_die);
22677 /* Generate child dies for template paramaters. */
22678 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22679 schedule_generic_params_dies_gen (type);
22681 if (! type_die || (nested && is_cu_die (scope_die)))
22682 /* First occurrence of type or toplevel definition of nested class. */
22684 dw_die_ref old_die = type_die;
22686 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22687 ? record_type_tag (type) : DW_TAG_union_type,
22688 scope_die, type);
22689 equate_type_number_to_die (type, type_die);
22690 if (old_die)
22691 add_AT_specification (type_die, old_die);
22692 else
22693 add_name_attribute (type_die, type_tag (type));
22695 else
22696 remove_AT (type_die, DW_AT_declaration);
22698 /* If this type has been completed, then give it a byte_size attribute and
22699 then give a list of members. */
22700 if (complete && !ns_decl)
22702 /* Prevent infinite recursion in cases where the type of some member of
22703 this type is expressed in terms of this type itself. */
22704 TREE_ASM_WRITTEN (type) = 1;
22705 add_byte_size_attribute (type_die, type);
22706 if (TYPE_STUB_DECL (type) != NULL_TREE)
22708 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22709 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22712 /* If the first reference to this type was as the return type of an
22713 inline function, then it may not have a parent. Fix this now. */
22714 if (type_die->die_parent == NULL)
22715 add_child_die (scope_die, type_die);
22717 push_decl_scope (type);
22718 gen_member_die (type, type_die);
22719 pop_decl_scope ();
22721 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22722 if (TYPE_ARTIFICIAL (type))
22723 add_AT_flag (type_die, DW_AT_artificial, 1);
22725 /* GNU extension: Record what type our vtable lives in. */
22726 if (TYPE_VFIELD (type))
22728 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22730 gen_type_die (vtype, context_die);
22731 add_AT_die_ref (type_die, DW_AT_containing_type,
22732 lookup_type_die (vtype));
22735 else
22737 add_AT_flag (type_die, DW_AT_declaration, 1);
22739 /* We don't need to do this for function-local types. */
22740 if (TYPE_STUB_DECL (type)
22741 && ! decl_function_context (TYPE_STUB_DECL (type)))
22742 vec_safe_push (incomplete_types, type);
22745 if (get_AT (type_die, DW_AT_name))
22746 add_pubtype (type, type_die);
22749 /* Generate a DIE for a subroutine _type_. */
22751 static void
22752 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22754 tree return_type = TREE_TYPE (type);
22755 dw_die_ref subr_die
22756 = new_die (DW_TAG_subroutine_type,
22757 scope_die_for (type, context_die), type);
22759 equate_type_number_to_die (type, subr_die);
22760 add_prototyped_attribute (subr_die, type);
22761 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22762 context_die);
22763 gen_formal_types_die (type, subr_die);
22765 if (get_AT (subr_die, DW_AT_name))
22766 add_pubtype (type, subr_die);
22769 /* Generate a DIE for a type definition. */
22771 static void
22772 gen_typedef_die (tree decl, dw_die_ref context_die)
22774 dw_die_ref type_die;
22775 tree origin;
22777 if (TREE_ASM_WRITTEN (decl))
22779 if (DECL_ORIGINAL_TYPE (decl))
22780 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22781 return;
22784 TREE_ASM_WRITTEN (decl) = 1;
22785 type_die = new_die (DW_TAG_typedef, context_die, decl);
22786 origin = decl_ultimate_origin (decl);
22787 if (origin != NULL)
22788 add_abstract_origin_attribute (type_die, origin);
22789 else
22791 tree type;
22793 add_name_and_src_coords_attributes (type_die, decl);
22794 if (DECL_ORIGINAL_TYPE (decl))
22796 type = DECL_ORIGINAL_TYPE (decl);
22798 if (type == error_mark_node)
22799 return;
22801 gcc_assert (type != TREE_TYPE (decl));
22802 equate_type_number_to_die (TREE_TYPE (decl), type_die);
22804 else
22806 type = TREE_TYPE (decl);
22808 if (type == error_mark_node)
22809 return;
22811 if (is_naming_typedef_decl (TYPE_NAME (type)))
22813 /* Here, we are in the case of decl being a typedef naming
22814 an anonymous type, e.g:
22815 typedef struct {...} foo;
22816 In that case TREE_TYPE (decl) is not a typedef variant
22817 type and TYPE_NAME of the anonymous type is set to the
22818 TYPE_DECL of the typedef. This construct is emitted by
22819 the C++ FE.
22821 TYPE is the anonymous struct named by the typedef
22822 DECL. As we need the DW_AT_type attribute of the
22823 DW_TAG_typedef to point to the DIE of TYPE, let's
22824 generate that DIE right away. add_type_attribute
22825 called below will then pick (via lookup_type_die) that
22826 anonymous struct DIE. */
22827 if (!TREE_ASM_WRITTEN (type))
22828 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22830 /* This is a GNU Extension. We are adding a
22831 DW_AT_linkage_name attribute to the DIE of the
22832 anonymous struct TYPE. The value of that attribute
22833 is the name of the typedef decl naming the anonymous
22834 struct. This greatly eases the work of consumers of
22835 this debug info. */
22836 add_linkage_name_raw (lookup_type_die (type), decl);
22840 add_type_attribute (type_die, type, decl_quals (decl), false,
22841 context_die);
22843 if (is_naming_typedef_decl (decl))
22844 /* We want that all subsequent calls to lookup_type_die with
22845 TYPE in argument yield the DW_TAG_typedef we have just
22846 created. */
22847 equate_type_number_to_die (type, type_die);
22849 add_accessibility_attribute (type_die, decl);
22852 if (DECL_ABSTRACT_P (decl))
22853 equate_decl_number_to_die (decl, type_die);
22855 if (get_AT (type_die, DW_AT_name))
22856 add_pubtype (decl, type_die);
22859 /* Generate a DIE for a struct, class, enum or union type. */
22861 static void
22862 gen_tagged_type_die (tree type,
22863 dw_die_ref context_die,
22864 enum debug_info_usage usage)
22866 int need_pop;
22868 if (type == NULL_TREE
22869 || !is_tagged_type (type))
22870 return;
22872 if (TREE_ASM_WRITTEN (type))
22873 need_pop = 0;
22874 /* If this is a nested type whose containing class hasn't been written
22875 out yet, writing it out will cover this one, too. This does not apply
22876 to instantiations of member class templates; they need to be added to
22877 the containing class as they are generated. FIXME: This hurts the
22878 idea of combining type decls from multiple TUs, since we can't predict
22879 what set of template instantiations we'll get. */
22880 else if (TYPE_CONTEXT (type)
22881 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22882 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22884 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22886 if (TREE_ASM_WRITTEN (type))
22887 return;
22889 /* If that failed, attach ourselves to the stub. */
22890 push_decl_scope (TYPE_CONTEXT (type));
22891 context_die = lookup_type_die (TYPE_CONTEXT (type));
22892 need_pop = 1;
22894 else if (TYPE_CONTEXT (type) != NULL_TREE
22895 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22897 /* If this type is local to a function that hasn't been written
22898 out yet, use a NULL context for now; it will be fixed up in
22899 decls_for_scope. */
22900 context_die = lookup_decl_die (TYPE_CONTEXT (type));
22901 /* A declaration DIE doesn't count; nested types need to go in the
22902 specification. */
22903 if (context_die && is_declaration_die (context_die))
22904 context_die = NULL;
22905 need_pop = 0;
22907 else
22909 context_die = declare_in_namespace (type, context_die);
22910 need_pop = 0;
22913 if (TREE_CODE (type) == ENUMERAL_TYPE)
22915 /* This might have been written out by the call to
22916 declare_in_namespace. */
22917 if (!TREE_ASM_WRITTEN (type))
22918 gen_enumeration_type_die (type, context_die);
22920 else
22921 gen_struct_or_union_type_die (type, context_die, usage);
22923 if (need_pop)
22924 pop_decl_scope ();
22926 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22927 it up if it is ever completed. gen_*_type_die will set it for us
22928 when appropriate. */
22931 /* Generate a type description DIE. */
22933 static void
22934 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22935 enum debug_info_usage usage)
22937 struct array_descr_info info;
22939 if (type == NULL_TREE || type == error_mark_node)
22940 return;
22942 if (flag_checking && type)
22943 verify_type (type);
22945 if (TYPE_NAME (type) != NULL_TREE
22946 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22947 && is_redundant_typedef (TYPE_NAME (type))
22948 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22949 /* The DECL of this type is a typedef we don't want to emit debug
22950 info for but we want debug info for its underlying typedef.
22951 This can happen for e.g, the injected-class-name of a C++
22952 type. */
22953 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22955 /* If TYPE is a typedef type variant, let's generate debug info
22956 for the parent typedef which TYPE is a type of. */
22957 if (typedef_variant_p (type))
22959 if (TREE_ASM_WRITTEN (type))
22960 return;
22962 /* Prevent broken recursion; we can't hand off to the same type. */
22963 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22965 /* Give typedefs the right scope. */
22966 context_die = scope_die_for (type, context_die);
22968 TREE_ASM_WRITTEN (type) = 1;
22970 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22971 return;
22974 /* If type is an anonymous tagged type named by a typedef, let's
22975 generate debug info for the typedef. */
22976 if (is_naming_typedef_decl (TYPE_NAME (type)))
22978 /* Use the DIE of the containing namespace as the parent DIE of
22979 the type description DIE we want to generate. */
22980 if (DECL_CONTEXT (TYPE_NAME (type))
22981 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
22982 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
22984 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22985 return;
22988 /* We are going to output a DIE to represent the unqualified version
22989 of this type (i.e. without any const or volatile qualifiers) so
22990 get the main variant (i.e. the unqualified version) of this type
22991 now. (Vectors and arrays are special because the debugging info is in the
22992 cloned type itself). */
22993 if (TREE_CODE (type) != VECTOR_TYPE
22994 && TREE_CODE (type) != ARRAY_TYPE)
22995 type = type_main_variant (type);
22997 /* If this is an array type with hidden descriptor, handle it first. */
22998 if (!TREE_ASM_WRITTEN (type)
22999 && lang_hooks.types.get_array_descr_info)
23001 memset (&info, 0, sizeof (info));
23002 if (lang_hooks.types.get_array_descr_info (type, &info))
23004 /* Fortran sometimes emits array types with no dimension. */
23005 gcc_assert (info.ndimensions >= 0
23006 && (info.ndimensions
23007 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
23008 gen_descr_array_type_die (type, &info, context_die);
23009 TREE_ASM_WRITTEN (type) = 1;
23010 return;
23014 if (TREE_ASM_WRITTEN (type))
23016 /* Variable-length types may be incomplete even if
23017 TREE_ASM_WRITTEN. For such types, fall through to
23018 gen_array_type_die() and possibly fill in
23019 DW_AT_{upper,lower}_bound attributes. */
23020 if ((TREE_CODE (type) != ARRAY_TYPE
23021 && TREE_CODE (type) != RECORD_TYPE
23022 && TREE_CODE (type) != UNION_TYPE
23023 && TREE_CODE (type) != QUAL_UNION_TYPE)
23024 || !variably_modified_type_p (type, NULL))
23025 return;
23028 switch (TREE_CODE (type))
23030 case ERROR_MARK:
23031 break;
23033 case POINTER_TYPE:
23034 case REFERENCE_TYPE:
23035 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
23036 ensures that the gen_type_die recursion will terminate even if the
23037 type is recursive. Recursive types are possible in Ada. */
23038 /* ??? We could perhaps do this for all types before the switch
23039 statement. */
23040 TREE_ASM_WRITTEN (type) = 1;
23042 /* For these types, all that is required is that we output a DIE (or a
23043 set of DIEs) to represent the "basis" type. */
23044 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23045 DINFO_USAGE_IND_USE);
23046 break;
23048 case OFFSET_TYPE:
23049 /* This code is used for C++ pointer-to-data-member types.
23050 Output a description of the relevant class type. */
23051 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
23052 DINFO_USAGE_IND_USE);
23054 /* Output a description of the type of the object pointed to. */
23055 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23056 DINFO_USAGE_IND_USE);
23058 /* Now output a DIE to represent this pointer-to-data-member type
23059 itself. */
23060 gen_ptr_to_mbr_type_die (type, context_die);
23061 break;
23063 case FUNCTION_TYPE:
23064 /* Force out return type (in case it wasn't forced out already). */
23065 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23066 DINFO_USAGE_DIR_USE);
23067 gen_subroutine_type_die (type, context_die);
23068 break;
23070 case METHOD_TYPE:
23071 /* Force out return type (in case it wasn't forced out already). */
23072 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23073 DINFO_USAGE_DIR_USE);
23074 gen_subroutine_type_die (type, context_die);
23075 break;
23077 case ARRAY_TYPE:
23078 case VECTOR_TYPE:
23079 gen_array_type_die (type, context_die);
23080 break;
23082 case ENUMERAL_TYPE:
23083 case RECORD_TYPE:
23084 case UNION_TYPE:
23085 case QUAL_UNION_TYPE:
23086 gen_tagged_type_die (type, context_die, usage);
23087 return;
23089 case VOID_TYPE:
23090 case INTEGER_TYPE:
23091 case REAL_TYPE:
23092 case FIXED_POINT_TYPE:
23093 case COMPLEX_TYPE:
23094 case BOOLEAN_TYPE:
23095 case POINTER_BOUNDS_TYPE:
23096 /* No DIEs needed for fundamental types. */
23097 break;
23099 case NULLPTR_TYPE:
23100 case LANG_TYPE:
23101 /* Just use DW_TAG_unspecified_type. */
23103 dw_die_ref type_die = lookup_type_die (type);
23104 if (type_die == NULL)
23106 tree name = TYPE_IDENTIFIER (type);
23107 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
23108 type);
23109 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
23110 equate_type_number_to_die (type, type_die);
23113 break;
23115 default:
23116 if (is_cxx_auto (type))
23118 tree name = TYPE_IDENTIFIER (type);
23119 dw_die_ref *die = (name == get_identifier ("auto")
23120 ? &auto_die : &decltype_auto_die);
23121 if (!*die)
23123 *die = new_die (DW_TAG_unspecified_type,
23124 comp_unit_die (), NULL_TREE);
23125 add_name_attribute (*die, IDENTIFIER_POINTER (name));
23127 equate_type_number_to_die (type, *die);
23128 break;
23130 gcc_unreachable ();
23133 TREE_ASM_WRITTEN (type) = 1;
23136 static void
23137 gen_type_die (tree type, dw_die_ref context_die)
23139 if (type != error_mark_node)
23141 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
23142 if (flag_checking)
23144 dw_die_ref die = lookup_type_die (type);
23145 if (die)
23146 check_die (die);
23151 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
23152 things which are local to the given block. */
23154 static void
23155 gen_block_die (tree stmt, dw_die_ref context_die)
23157 int must_output_die = 0;
23158 bool inlined_func;
23160 /* Ignore blocks that are NULL. */
23161 if (stmt == NULL_TREE)
23162 return;
23164 inlined_func = inlined_function_outer_scope_p (stmt);
23166 /* If the block is one fragment of a non-contiguous block, do not
23167 process the variables, since they will have been done by the
23168 origin block. Do process subblocks. */
23169 if (BLOCK_FRAGMENT_ORIGIN (stmt))
23171 tree sub;
23173 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
23174 gen_block_die (sub, context_die);
23176 return;
23179 /* Determine if we need to output any Dwarf DIEs at all to represent this
23180 block. */
23181 if (inlined_func)
23182 /* The outer scopes for inlinings *must* always be represented. We
23183 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
23184 must_output_die = 1;
23185 else
23187 /* Determine if this block directly contains any "significant"
23188 local declarations which we will need to output DIEs for. */
23189 if (debug_info_level > DINFO_LEVEL_TERSE)
23190 /* We are not in terse mode so *any* local declaration counts
23191 as being a "significant" one. */
23192 must_output_die = ((BLOCK_VARS (stmt) != NULL
23193 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
23194 && (TREE_USED (stmt)
23195 || TREE_ASM_WRITTEN (stmt)
23196 || BLOCK_ABSTRACT (stmt)));
23197 else if ((TREE_USED (stmt)
23198 || TREE_ASM_WRITTEN (stmt)
23199 || BLOCK_ABSTRACT (stmt))
23200 && !dwarf2out_ignore_block (stmt))
23201 must_output_die = 1;
23204 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
23205 DIE for any block which contains no significant local declarations at
23206 all. Rather, in such cases we just call `decls_for_scope' so that any
23207 needed Dwarf info for any sub-blocks will get properly generated. Note
23208 that in terse mode, our definition of what constitutes a "significant"
23209 local declaration gets restricted to include only inlined function
23210 instances and local (nested) function definitions. */
23211 if (must_output_die)
23213 if (inlined_func)
23215 /* If STMT block is abstract, that means we have been called
23216 indirectly from dwarf2out_abstract_function.
23217 That function rightfully marks the descendent blocks (of
23218 the abstract function it is dealing with) as being abstract,
23219 precisely to prevent us from emitting any
23220 DW_TAG_inlined_subroutine DIE as a descendent
23221 of an abstract function instance. So in that case, we should
23222 not call gen_inlined_subroutine_die.
23224 Later though, when cgraph asks dwarf2out to emit info
23225 for the concrete instance of the function decl into which
23226 the concrete instance of STMT got inlined, the later will lead
23227 to the generation of a DW_TAG_inlined_subroutine DIE. */
23228 if (! BLOCK_ABSTRACT (stmt))
23229 gen_inlined_subroutine_die (stmt, context_die);
23231 else
23232 gen_lexical_block_die (stmt, context_die);
23234 else
23235 decls_for_scope (stmt, context_die);
23238 /* Process variable DECL (or variable with origin ORIGIN) within
23239 block STMT and add it to CONTEXT_DIE. */
23240 static void
23241 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23243 dw_die_ref die;
23244 tree decl_or_origin = decl ? decl : origin;
23246 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23247 die = lookup_decl_die (decl_or_origin);
23248 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
23249 && TYPE_DECL_IS_STUB (decl_or_origin))
23250 die = lookup_type_die (TREE_TYPE (decl_or_origin));
23251 else
23252 die = NULL;
23254 if (die != NULL && die->die_parent == NULL)
23255 add_child_die (context_die, die);
23256 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23258 if (early_dwarf)
23259 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23260 stmt, context_die);
23262 else
23263 gen_decl_die (decl, origin, NULL, context_die);
23266 /* Generate all of the decls declared within a given scope and (recursively)
23267 all of its sub-blocks. */
23269 static void
23270 decls_for_scope (tree stmt, dw_die_ref context_die)
23272 tree decl;
23273 unsigned int i;
23274 tree subblocks;
23276 /* Ignore NULL blocks. */
23277 if (stmt == NULL_TREE)
23278 return;
23280 /* Output the DIEs to represent all of the data objects and typedefs
23281 declared directly within this block but not within any nested
23282 sub-blocks. Also, nested function and tag DIEs have been
23283 generated with a parent of NULL; fix that up now. We don't
23284 have to do this if we're at -g1. */
23285 if (debug_info_level > DINFO_LEVEL_TERSE)
23287 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23288 process_scope_var (stmt, decl, NULL_TREE, context_die);
23289 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23290 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23291 context_die);
23294 /* Even if we're at -g1, we need to process the subblocks in order to get
23295 inlined call information. */
23297 /* Output the DIEs to represent all sub-blocks (and the items declared
23298 therein) of this block. */
23299 for (subblocks = BLOCK_SUBBLOCKS (stmt);
23300 subblocks != NULL;
23301 subblocks = BLOCK_CHAIN (subblocks))
23302 gen_block_die (subblocks, context_die);
23305 /* Is this a typedef we can avoid emitting? */
23307 bool
23308 is_redundant_typedef (const_tree decl)
23310 if (TYPE_DECL_IS_STUB (decl))
23311 return true;
23313 if (DECL_ARTIFICIAL (decl)
23314 && DECL_CONTEXT (decl)
23315 && is_tagged_type (DECL_CONTEXT (decl))
23316 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23317 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23318 /* Also ignore the artificial member typedef for the class name. */
23319 return true;
23321 return false;
23324 /* Return TRUE if TYPE is a typedef that names a type for linkage
23325 purposes. This kind of typedefs is produced by the C++ FE for
23326 constructs like:
23328 typedef struct {...} foo;
23330 In that case, there is no typedef variant type produced for foo.
23331 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23332 struct type. */
23334 static bool
23335 is_naming_typedef_decl (const_tree decl)
23337 if (decl == NULL_TREE
23338 || TREE_CODE (decl) != TYPE_DECL
23339 || DECL_NAMELESS (decl)
23340 || !is_tagged_type (TREE_TYPE (decl))
23341 || DECL_IS_BUILTIN (decl)
23342 || is_redundant_typedef (decl)
23343 /* It looks like Ada produces TYPE_DECLs that are very similar
23344 to C++ naming typedefs but that have different
23345 semantics. Let's be specific to c++ for now. */
23346 || !is_cxx ())
23347 return FALSE;
23349 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23350 && TYPE_NAME (TREE_TYPE (decl)) == decl
23351 && (TYPE_STUB_DECL (TREE_TYPE (decl))
23352 != TYPE_NAME (TREE_TYPE (decl))));
23355 /* Looks up the DIE for a context. */
23357 static inline dw_die_ref
23358 lookup_context_die (tree context)
23360 if (context)
23362 /* Find die that represents this context. */
23363 if (TYPE_P (context))
23365 context = TYPE_MAIN_VARIANT (context);
23366 dw_die_ref ctx = lookup_type_die (context);
23367 if (!ctx)
23368 return NULL;
23369 return strip_naming_typedef (context, ctx);
23371 else
23372 return lookup_decl_die (context);
23374 return comp_unit_die ();
23377 /* Returns the DIE for a context. */
23379 static inline dw_die_ref
23380 get_context_die (tree context)
23382 if (context)
23384 /* Find die that represents this context. */
23385 if (TYPE_P (context))
23387 context = TYPE_MAIN_VARIANT (context);
23388 return strip_naming_typedef (context, force_type_die (context));
23390 else
23391 return force_decl_die (context);
23393 return comp_unit_die ();
23396 /* Returns the DIE for decl. A DIE will always be returned. */
23398 static dw_die_ref
23399 force_decl_die (tree decl)
23401 dw_die_ref decl_die;
23402 unsigned saved_external_flag;
23403 tree save_fn = NULL_TREE;
23404 decl_die = lookup_decl_die (decl);
23405 if (!decl_die)
23407 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23409 decl_die = lookup_decl_die (decl);
23410 if (decl_die)
23411 return decl_die;
23413 switch (TREE_CODE (decl))
23415 case FUNCTION_DECL:
23416 /* Clear current_function_decl, so that gen_subprogram_die thinks
23417 that this is a declaration. At this point, we just want to force
23418 declaration die. */
23419 save_fn = current_function_decl;
23420 current_function_decl = NULL_TREE;
23421 gen_subprogram_die (decl, context_die);
23422 current_function_decl = save_fn;
23423 break;
23425 case VAR_DECL:
23426 /* Set external flag to force declaration die. Restore it after
23427 gen_decl_die() call. */
23428 saved_external_flag = DECL_EXTERNAL (decl);
23429 DECL_EXTERNAL (decl) = 1;
23430 gen_decl_die (decl, NULL, NULL, context_die);
23431 DECL_EXTERNAL (decl) = saved_external_flag;
23432 break;
23434 case NAMESPACE_DECL:
23435 if (dwarf_version >= 3 || !dwarf_strict)
23436 dwarf2out_decl (decl);
23437 else
23438 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
23439 decl_die = comp_unit_die ();
23440 break;
23442 case TRANSLATION_UNIT_DECL:
23443 decl_die = comp_unit_die ();
23444 break;
23446 default:
23447 gcc_unreachable ();
23450 /* We should be able to find the DIE now. */
23451 if (!decl_die)
23452 decl_die = lookup_decl_die (decl);
23453 gcc_assert (decl_die);
23456 return decl_die;
23459 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
23460 always returned. */
23462 static dw_die_ref
23463 force_type_die (tree type)
23465 dw_die_ref type_die;
23467 type_die = lookup_type_die (type);
23468 if (!type_die)
23470 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23472 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23473 false, context_die);
23474 gcc_assert (type_die);
23476 return type_die;
23479 /* Force out any required namespaces to be able to output DECL,
23480 and return the new context_die for it, if it's changed. */
23482 static dw_die_ref
23483 setup_namespace_context (tree thing, dw_die_ref context_die)
23485 tree context = (DECL_P (thing)
23486 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23487 if (context && TREE_CODE (context) == NAMESPACE_DECL)
23488 /* Force out the namespace. */
23489 context_die = force_decl_die (context);
23491 return context_die;
23494 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23495 type) within its namespace, if appropriate.
23497 For compatibility with older debuggers, namespace DIEs only contain
23498 declarations; all definitions are emitted at CU scope, with
23499 DW_AT_specification pointing to the declaration (like with class
23500 members). */
23502 static dw_die_ref
23503 declare_in_namespace (tree thing, dw_die_ref context_die)
23505 dw_die_ref ns_context;
23507 if (debug_info_level <= DINFO_LEVEL_TERSE)
23508 return context_die;
23510 /* External declarations in the local scope only need to be emitted
23511 once, not once in the namespace and once in the scope.
23513 This avoids declaring the `extern' below in the
23514 namespace DIE as well as in the innermost scope:
23516 namespace S
23518 int i=5;
23519 int foo()
23521 int i=8;
23522 extern int i;
23523 return i;
23527 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23528 return context_die;
23530 /* If this decl is from an inlined function, then don't try to emit it in its
23531 namespace, as we will get confused. It would have already been emitted
23532 when the abstract instance of the inline function was emitted anyways. */
23533 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23534 return context_die;
23536 ns_context = setup_namespace_context (thing, context_die);
23538 if (ns_context != context_die)
23540 if (is_fortran ())
23541 return ns_context;
23542 if (DECL_P (thing))
23543 gen_decl_die (thing, NULL, NULL, ns_context);
23544 else
23545 gen_type_die (thing, ns_context);
23547 return context_die;
23550 /* Generate a DIE for a namespace or namespace alias. */
23552 static void
23553 gen_namespace_die (tree decl, dw_die_ref context_die)
23555 dw_die_ref namespace_die;
23557 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23558 they are an alias of. */
23559 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23561 /* Output a real namespace or module. */
23562 context_die = setup_namespace_context (decl, comp_unit_die ());
23563 namespace_die = new_die (is_fortran ()
23564 ? DW_TAG_module : DW_TAG_namespace,
23565 context_die, decl);
23566 /* For Fortran modules defined in different CU don't add src coords. */
23567 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23569 const char *name = dwarf2_name (decl, 0);
23570 if (name)
23571 add_name_attribute (namespace_die, name);
23573 else
23574 add_name_and_src_coords_attributes (namespace_die, decl);
23575 if (DECL_EXTERNAL (decl))
23576 add_AT_flag (namespace_die, DW_AT_declaration, 1);
23577 equate_decl_number_to_die (decl, namespace_die);
23579 else
23581 /* Output a namespace alias. */
23583 /* Force out the namespace we are an alias of, if necessary. */
23584 dw_die_ref origin_die
23585 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23587 if (DECL_FILE_SCOPE_P (decl)
23588 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23589 context_die = setup_namespace_context (decl, comp_unit_die ());
23590 /* Now create the namespace alias DIE. */
23591 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23592 add_name_and_src_coords_attributes (namespace_die, decl);
23593 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23594 equate_decl_number_to_die (decl, namespace_die);
23596 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
23597 if (want_pubnames ())
23598 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23601 /* Generate Dwarf debug information for a decl described by DECL.
23602 The return value is currently only meaningful for PARM_DECLs,
23603 for all other decls it returns NULL.
23605 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23606 It can be NULL otherwise. */
23608 static dw_die_ref
23609 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23610 dw_die_ref context_die)
23612 tree decl_or_origin = decl ? decl : origin;
23613 tree class_origin = NULL, ultimate_origin;
23615 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23616 return NULL;
23618 /* Ignore pointer bounds decls. */
23619 if (DECL_P (decl_or_origin)
23620 && TREE_TYPE (decl_or_origin)
23621 && POINTER_BOUNDS_P (decl_or_origin))
23622 return NULL;
23624 switch (TREE_CODE (decl_or_origin))
23626 case ERROR_MARK:
23627 break;
23629 case CONST_DECL:
23630 if (!is_fortran () && !is_ada ())
23632 /* The individual enumerators of an enum type get output when we output
23633 the Dwarf representation of the relevant enum type itself. */
23634 break;
23637 /* Emit its type. */
23638 gen_type_die (TREE_TYPE (decl), context_die);
23640 /* And its containing namespace. */
23641 context_die = declare_in_namespace (decl, context_die);
23643 gen_const_die (decl, context_die);
23644 break;
23646 case FUNCTION_DECL:
23647 /* Don't output any DIEs to represent mere function declarations,
23648 unless they are class members or explicit block externs. */
23649 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23650 && DECL_FILE_SCOPE_P (decl_or_origin)
23651 && (current_function_decl == NULL_TREE
23652 || DECL_ARTIFICIAL (decl_or_origin)))
23653 break;
23655 #if 0
23656 /* FIXME */
23657 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23658 on local redeclarations of global functions. That seems broken. */
23659 if (current_function_decl != decl)
23660 /* This is only a declaration. */;
23661 #endif
23663 /* If we're emitting a clone, emit info for the abstract instance. */
23664 if (origin || DECL_ORIGIN (decl) != decl)
23665 dwarf2out_abstract_function (origin
23666 ? DECL_ORIGIN (origin)
23667 : DECL_ABSTRACT_ORIGIN (decl));
23669 /* If we're emitting an out-of-line copy of an inline function,
23670 emit info for the abstract instance and set up to refer to it. */
23671 else if (cgraph_function_possibly_inlined_p (decl)
23672 && ! DECL_ABSTRACT_P (decl)
23673 && ! class_or_namespace_scope_p (context_die)
23674 /* dwarf2out_abstract_function won't emit a die if this is just
23675 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
23676 that case, because that works only if we have a die. */
23677 && DECL_INITIAL (decl) != NULL_TREE)
23679 dwarf2out_abstract_function (decl);
23680 set_decl_origin_self (decl);
23683 /* Otherwise we're emitting the primary DIE for this decl. */
23684 else if (debug_info_level > DINFO_LEVEL_TERSE)
23686 /* Before we describe the FUNCTION_DECL itself, make sure that we
23687 have its containing type. */
23688 if (!origin)
23689 origin = decl_class_context (decl);
23690 if (origin != NULL_TREE)
23691 gen_type_die (origin, context_die);
23693 /* And its return type. */
23694 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23696 /* And its virtual context. */
23697 if (DECL_VINDEX (decl) != NULL_TREE)
23698 gen_type_die (DECL_CONTEXT (decl), context_die);
23700 /* Make sure we have a member DIE for decl. */
23701 if (origin != NULL_TREE)
23702 gen_type_die_for_member (origin, decl, context_die);
23704 /* And its containing namespace. */
23705 context_die = declare_in_namespace (decl, context_die);
23708 /* Now output a DIE to represent the function itself. */
23709 if (decl)
23710 gen_subprogram_die (decl, context_die);
23711 break;
23713 case TYPE_DECL:
23714 /* If we are in terse mode, don't generate any DIEs to represent any
23715 actual typedefs. */
23716 if (debug_info_level <= DINFO_LEVEL_TERSE)
23717 break;
23719 /* In the special case of a TYPE_DECL node representing the declaration
23720 of some type tag, if the given TYPE_DECL is marked as having been
23721 instantiated from some other (original) TYPE_DECL node (e.g. one which
23722 was generated within the original definition of an inline function) we
23723 used to generate a special (abbreviated) DW_TAG_structure_type,
23724 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
23725 should be actually referencing those DIEs, as variable DIEs with that
23726 type would be emitted already in the abstract origin, so it was always
23727 removed during unused type prunning. Don't add anything in this
23728 case. */
23729 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23730 break;
23732 if (is_redundant_typedef (decl))
23733 gen_type_die (TREE_TYPE (decl), context_die);
23734 else
23735 /* Output a DIE to represent the typedef itself. */
23736 gen_typedef_die (decl, context_die);
23737 break;
23739 case LABEL_DECL:
23740 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23741 gen_label_die (decl, context_die);
23742 break;
23744 case VAR_DECL:
23745 case RESULT_DECL:
23746 /* If we are in terse mode, don't generate any DIEs to represent any
23747 variable declarations or definitions. */
23748 if (debug_info_level <= DINFO_LEVEL_TERSE)
23749 break;
23751 /* Output any DIEs that are needed to specify the type of this data
23752 object. */
23753 if (decl_by_reference_p (decl_or_origin))
23754 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23755 else
23756 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23758 /* And its containing type. */
23759 class_origin = decl_class_context (decl_or_origin);
23760 if (class_origin != NULL_TREE)
23761 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23763 /* And its containing namespace. */
23764 context_die = declare_in_namespace (decl_or_origin, context_die);
23766 /* Now output the DIE to represent the data object itself. This gets
23767 complicated because of the possibility that the VAR_DECL really
23768 represents an inlined instance of a formal parameter for an inline
23769 function. */
23770 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23771 if (ultimate_origin != NULL_TREE
23772 && TREE_CODE (ultimate_origin) == PARM_DECL)
23773 gen_formal_parameter_die (decl, origin,
23774 true /* Emit name attribute. */,
23775 context_die);
23776 else
23777 gen_variable_die (decl, origin, context_die);
23778 break;
23780 case FIELD_DECL:
23781 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23782 /* Ignore the nameless fields that are used to skip bits but handle C++
23783 anonymous unions and structs. */
23784 if (DECL_NAME (decl) != NULL_TREE
23785 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23786 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23788 gen_type_die (member_declared_type (decl), context_die);
23789 gen_field_die (decl, ctx, context_die);
23791 break;
23793 case PARM_DECL:
23794 if (DECL_BY_REFERENCE (decl_or_origin))
23795 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23796 else
23797 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23798 return gen_formal_parameter_die (decl, origin,
23799 true /* Emit name attribute. */,
23800 context_die);
23802 case NAMESPACE_DECL:
23803 if (dwarf_version >= 3 || !dwarf_strict)
23804 gen_namespace_die (decl, context_die);
23805 break;
23807 case IMPORTED_DECL:
23808 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23809 DECL_CONTEXT (decl), context_die);
23810 break;
23812 case NAMELIST_DECL:
23813 gen_namelist_decl (DECL_NAME (decl), context_die,
23814 NAMELIST_DECL_ASSOCIATED_DECL (decl));
23815 break;
23817 default:
23818 /* Probably some frontend-internal decl. Assume we don't care. */
23819 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23820 break;
23823 return NULL;
23826 /* Output initial debug information for global DECL. Called at the
23827 end of the parsing process.
23829 This is the initial debug generation process. As such, the DIEs
23830 generated may be incomplete. A later debug generation pass
23831 (dwarf2out_late_global_decl) will augment the information generated
23832 in this pass (e.g., with complete location info). */
23834 static void
23835 dwarf2out_early_global_decl (tree decl)
23837 set_early_dwarf s;
23839 /* gen_decl_die() will set DECL_ABSTRACT because
23840 cgraph_function_possibly_inlined_p() returns true. This is in
23841 turn will cause DW_AT_inline attributes to be set.
23843 This happens because at early dwarf generation, there is no
23844 cgraph information, causing cgraph_function_possibly_inlined_p()
23845 to return true. Trick cgraph_function_possibly_inlined_p()
23846 while we generate dwarf early. */
23847 bool save = symtab->global_info_ready;
23848 symtab->global_info_ready = true;
23850 /* We don't handle TYPE_DECLs. If required, they'll be reached via
23851 other DECLs and they can point to template types or other things
23852 that dwarf2out can't handle when done via dwarf2out_decl. */
23853 if (TREE_CODE (decl) != TYPE_DECL
23854 && TREE_CODE (decl) != PARM_DECL)
23856 tree save_fndecl = current_function_decl;
23857 if (TREE_CODE (decl) == FUNCTION_DECL)
23859 /* No cfun means the symbol has no body, so there's nothing
23860 to emit. */
23861 if (!DECL_STRUCT_FUNCTION (decl))
23862 goto early_decl_exit;
23864 current_function_decl = decl;
23866 dwarf2out_decl (decl);
23867 if (TREE_CODE (decl) == FUNCTION_DECL)
23868 current_function_decl = save_fndecl;
23870 early_decl_exit:
23871 symtab->global_info_ready = save;
23874 /* Output debug information for global decl DECL. Called from
23875 toplev.c after compilation proper has finished. */
23877 static void
23878 dwarf2out_late_global_decl (tree decl)
23880 /* We have to generate early debug late for LTO. */
23881 if (in_lto_p)
23882 dwarf2out_early_global_decl (decl);
23884 /* Fill-in any location information we were unable to determine
23885 on the first pass. */
23886 if (TREE_CODE (decl) == VAR_DECL
23887 && !POINTER_BOUNDS_P (decl))
23889 dw_die_ref die = lookup_decl_die (decl);
23890 if (die)
23891 add_location_or_const_value_attribute (die, decl, false);
23895 /* Output debug information for type decl DECL. Called from toplev.c
23896 and from language front ends (to record built-in types). */
23897 static void
23898 dwarf2out_type_decl (tree decl, int local)
23900 if (!local)
23902 set_early_dwarf s;
23903 dwarf2out_decl (decl);
23907 /* Output debug information for imported module or decl DECL.
23908 NAME is non-NULL name in the lexical block if the decl has been renamed.
23909 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23910 that DECL belongs to.
23911 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
23912 static void
23913 dwarf2out_imported_module_or_decl_1 (tree decl,
23914 tree name,
23915 tree lexical_block,
23916 dw_die_ref lexical_block_die)
23918 expanded_location xloc;
23919 dw_die_ref imported_die = NULL;
23920 dw_die_ref at_import_die;
23922 if (TREE_CODE (decl) == IMPORTED_DECL)
23924 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23925 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23926 gcc_assert (decl);
23928 else
23929 xloc = expand_location (input_location);
23931 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23933 at_import_die = force_type_die (TREE_TYPE (decl));
23934 /* For namespace N { typedef void T; } using N::T; base_type_die
23935 returns NULL, but DW_TAG_imported_declaration requires
23936 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
23937 if (!at_import_die)
23939 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23940 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23941 at_import_die = lookup_type_die (TREE_TYPE (decl));
23942 gcc_assert (at_import_die);
23945 else
23947 at_import_die = lookup_decl_die (decl);
23948 if (!at_import_die)
23950 /* If we're trying to avoid duplicate debug info, we may not have
23951 emitted the member decl for this field. Emit it now. */
23952 if (TREE_CODE (decl) == FIELD_DECL)
23954 tree type = DECL_CONTEXT (decl);
23956 if (TYPE_CONTEXT (type)
23957 && TYPE_P (TYPE_CONTEXT (type))
23958 && !should_emit_struct_debug (TYPE_CONTEXT (type),
23959 DINFO_USAGE_DIR_USE))
23960 return;
23961 gen_type_die_for_member (type, decl,
23962 get_context_die (TYPE_CONTEXT (type)));
23964 if (TREE_CODE (decl) == NAMELIST_DECL)
23965 at_import_die = gen_namelist_decl (DECL_NAME (decl),
23966 get_context_die (DECL_CONTEXT (decl)),
23967 NULL_TREE);
23968 else
23969 at_import_die = force_decl_die (decl);
23973 if (TREE_CODE (decl) == NAMESPACE_DECL)
23975 if (dwarf_version >= 3 || !dwarf_strict)
23976 imported_die = new_die (DW_TAG_imported_module,
23977 lexical_block_die,
23978 lexical_block);
23979 else
23980 return;
23982 else
23983 imported_die = new_die (DW_TAG_imported_declaration,
23984 lexical_block_die,
23985 lexical_block);
23987 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
23988 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
23989 if (name)
23990 add_AT_string (imported_die, DW_AT_name,
23991 IDENTIFIER_POINTER (name));
23992 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
23995 /* Output debug information for imported module or decl DECL.
23996 NAME is non-NULL name in context if the decl has been renamed.
23997 CHILD is true if decl is one of the renamed decls as part of
23998 importing whole module. */
24000 static void
24001 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
24002 bool child)
24004 /* dw_die_ref at_import_die; */
24005 dw_die_ref scope_die;
24007 if (debug_info_level <= DINFO_LEVEL_TERSE)
24008 return;
24010 gcc_assert (decl);
24012 set_early_dwarf s;
24014 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
24015 We need decl DIE for reference and scope die. First, get DIE for the decl
24016 itself. */
24018 /* Get the scope die for decl context. Use comp_unit_die for global module
24019 or decl. If die is not found for non globals, force new die. */
24020 if (context
24021 && TYPE_P (context)
24022 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
24023 return;
24025 if (!(dwarf_version >= 3 || !dwarf_strict))
24026 return;
24028 scope_die = get_context_die (context);
24030 if (child)
24032 gcc_assert (scope_die->die_child);
24033 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
24034 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
24035 scope_die = scope_die->die_child;
24038 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
24039 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
24042 /* Output debug information for namelists. */
24044 static dw_die_ref
24045 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
24047 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
24048 tree value;
24049 unsigned i;
24051 if (debug_info_level <= DINFO_LEVEL_TERSE)
24052 return NULL;
24054 gcc_assert (scope_die != NULL);
24055 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
24056 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
24058 /* If there are no item_decls, we have a nondefining namelist, e.g.
24059 with USE association; hence, set DW_AT_declaration. */
24060 if (item_decls == NULL_TREE)
24062 add_AT_flag (nml_die, DW_AT_declaration, 1);
24063 return nml_die;
24066 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
24068 nml_item_ref_die = lookup_decl_die (value);
24069 if (!nml_item_ref_die)
24070 nml_item_ref_die = force_decl_die (value);
24072 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
24073 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
24075 return nml_die;
24079 /* Write the debugging output for DECL and return the DIE. */
24081 static void
24082 dwarf2out_decl (tree decl)
24084 dw_die_ref context_die = comp_unit_die ();
24086 switch (TREE_CODE (decl))
24088 case ERROR_MARK:
24089 return;
24091 case FUNCTION_DECL:
24092 /* What we would really like to do here is to filter out all mere
24093 file-scope declarations of file-scope functions which are never
24094 referenced later within this translation unit (and keep all of ones
24095 that *are* referenced later on) but we aren't clairvoyant, so we have
24096 no idea which functions will be referenced in the future (i.e. later
24097 on within the current translation unit). So here we just ignore all
24098 file-scope function declarations which are not also definitions. If
24099 and when the debugger needs to know something about these functions,
24100 it will have to hunt around and find the DWARF information associated
24101 with the definition of the function.
24103 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
24104 nodes represent definitions and which ones represent mere
24105 declarations. We have to check DECL_INITIAL instead. That's because
24106 the C front-end supports some weird semantics for "extern inline"
24107 function definitions. These can get inlined within the current
24108 translation unit (and thus, we need to generate Dwarf info for their
24109 abstract instances so that the Dwarf info for the concrete inlined
24110 instances can have something to refer to) but the compiler never
24111 generates any out-of-lines instances of such things (despite the fact
24112 that they *are* definitions).
24114 The important point is that the C front-end marks these "extern
24115 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
24116 them anyway. Note that the C++ front-end also plays some similar games
24117 for inline function definitions appearing within include files which
24118 also contain `#pragma interface' pragmas.
24120 If we are called from dwarf2out_abstract_function output a DIE
24121 anyway. We can end up here this way with early inlining and LTO
24122 where the inlined function is output in a different LTRANS unit
24123 or not at all. */
24124 if (DECL_INITIAL (decl) == NULL_TREE
24125 && ! DECL_ABSTRACT_P (decl))
24126 return;
24128 /* If we're a nested function, initially use a parent of NULL; if we're
24129 a plain function, this will be fixed up in decls_for_scope. If
24130 we're a method, it will be ignored, since we already have a DIE. */
24131 if (decl_function_context (decl)
24132 /* But if we're in terse mode, we don't care about scope. */
24133 && debug_info_level > DINFO_LEVEL_TERSE)
24134 context_die = NULL;
24135 break;
24137 case VAR_DECL:
24138 /* For local statics lookup proper context die. */
24139 if (local_function_static (decl))
24140 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24142 /* If we are in terse mode, don't generate any DIEs to represent any
24143 variable declarations or definitions. */
24144 if (debug_info_level <= DINFO_LEVEL_TERSE)
24145 return;
24146 break;
24148 case CONST_DECL:
24149 if (debug_info_level <= DINFO_LEVEL_TERSE)
24150 return;
24151 if (!is_fortran () && !is_ada ())
24152 return;
24153 if (TREE_STATIC (decl) && decl_function_context (decl))
24154 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24155 break;
24157 case NAMESPACE_DECL:
24158 case IMPORTED_DECL:
24159 if (debug_info_level <= DINFO_LEVEL_TERSE)
24160 return;
24161 if (lookup_decl_die (decl) != NULL)
24162 return;
24163 break;
24165 case TYPE_DECL:
24166 /* Don't emit stubs for types unless they are needed by other DIEs. */
24167 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
24168 return;
24170 /* Don't bother trying to generate any DIEs to represent any of the
24171 normal built-in types for the language we are compiling. */
24172 if (DECL_IS_BUILTIN (decl))
24173 return;
24175 /* If we are in terse mode, don't generate any DIEs for types. */
24176 if (debug_info_level <= DINFO_LEVEL_TERSE)
24177 return;
24179 /* If we're a function-scope tag, initially use a parent of NULL;
24180 this will be fixed up in decls_for_scope. */
24181 if (decl_function_context (decl))
24182 context_die = NULL;
24184 break;
24186 case NAMELIST_DECL:
24187 break;
24189 default:
24190 return;
24193 gen_decl_die (decl, NULL, NULL, context_die);
24195 if (flag_checking)
24197 dw_die_ref die = lookup_decl_die (decl);
24198 if (die)
24199 check_die (die);
24203 /* Write the debugging output for DECL. */
24205 static void
24206 dwarf2out_function_decl (tree decl)
24208 dwarf2out_decl (decl);
24209 call_arg_locations = NULL;
24210 call_arg_loc_last = NULL;
24211 call_site_count = -1;
24212 tail_call_site_count = -1;
24213 decl_loc_table->empty ();
24214 cached_dw_loc_list_table->empty ();
24217 /* Output a marker (i.e. a label) for the beginning of the generated code for
24218 a lexical block. */
24220 static void
24221 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
24222 unsigned int blocknum)
24224 switch_to_section (current_function_section ());
24225 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
24228 /* Output a marker (i.e. a label) for the end of the generated code for a
24229 lexical block. */
24231 static void
24232 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
24234 switch_to_section (current_function_section ());
24235 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
24238 /* Returns nonzero if it is appropriate not to emit any debugging
24239 information for BLOCK, because it doesn't contain any instructions.
24241 Don't allow this for blocks with nested functions or local classes
24242 as we would end up with orphans, and in the presence of scheduling
24243 we may end up calling them anyway. */
24245 static bool
24246 dwarf2out_ignore_block (const_tree block)
24248 tree decl;
24249 unsigned int i;
24251 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24252 if (TREE_CODE (decl) == FUNCTION_DECL
24253 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24254 return 0;
24255 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24257 decl = BLOCK_NONLOCALIZED_VAR (block, i);
24258 if (TREE_CODE (decl) == FUNCTION_DECL
24259 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24260 return 0;
24263 return 1;
24266 /* Hash table routines for file_hash. */
24268 bool
24269 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24271 return filename_cmp (p1->filename, p2) == 0;
24274 hashval_t
24275 dwarf_file_hasher::hash (dwarf_file_data *p)
24277 return htab_hash_string (p->filename);
24280 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24281 dwarf2out.c) and return its "index". The index of each (known) filename is
24282 just a unique number which is associated with only that one filename. We
24283 need such numbers for the sake of generating labels (in the .debug_sfnames
24284 section) and references to those files numbers (in the .debug_srcinfo
24285 and .debug_macinfo sections). If the filename given as an argument is not
24286 found in our current list, add it to the list and assign it the next
24287 available unique index number. */
24289 static struct dwarf_file_data *
24290 lookup_filename (const char *file_name)
24292 struct dwarf_file_data * created;
24294 if (!file_name)
24295 return NULL;
24297 dwarf_file_data **slot
24298 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24299 INSERT);
24300 if (*slot)
24301 return *slot;
24303 created = ggc_alloc<dwarf_file_data> ();
24304 created->filename = file_name;
24305 created->emitted_number = 0;
24306 *slot = created;
24307 return created;
24310 /* If the assembler will construct the file table, then translate the compiler
24311 internal file table number into the assembler file table number, and emit
24312 a .file directive if we haven't already emitted one yet. The file table
24313 numbers are different because we prune debug info for unused variables and
24314 types, which may include filenames. */
24316 static int
24317 maybe_emit_file (struct dwarf_file_data * fd)
24319 if (! fd->emitted_number)
24321 if (last_emitted_file)
24322 fd->emitted_number = last_emitted_file->emitted_number + 1;
24323 else
24324 fd->emitted_number = 1;
24325 last_emitted_file = fd;
24327 if (DWARF2_ASM_LINE_DEBUG_INFO)
24329 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24330 output_quoted_string (asm_out_file,
24331 remap_debug_filename (fd->filename));
24332 fputc ('\n', asm_out_file);
24336 return fd->emitted_number;
24339 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24340 That generation should happen after function debug info has been
24341 generated. The value of the attribute is the constant value of ARG. */
24343 static void
24344 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24346 die_arg_entry entry;
24348 if (!die || !arg)
24349 return;
24351 gcc_assert (early_dwarf);
24353 if (!tmpl_value_parm_die_table)
24354 vec_alloc (tmpl_value_parm_die_table, 32);
24356 entry.die = die;
24357 entry.arg = arg;
24358 vec_safe_push (tmpl_value_parm_die_table, entry);
24361 /* Return TRUE if T is an instance of generic type, FALSE
24362 otherwise. */
24364 static bool
24365 generic_type_p (tree t)
24367 if (t == NULL_TREE || !TYPE_P (t))
24368 return false;
24369 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24372 /* Schedule the generation of the generic parameter dies for the
24373 instance of generic type T. The proper generation itself is later
24374 done by gen_scheduled_generic_parms_dies. */
24376 static void
24377 schedule_generic_params_dies_gen (tree t)
24379 if (!generic_type_p (t))
24380 return;
24382 gcc_assert (early_dwarf);
24384 if (!generic_type_instances)
24385 vec_alloc (generic_type_instances, 256);
24387 vec_safe_push (generic_type_instances, t);
24390 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24391 by append_entry_to_tmpl_value_parm_die_table. This function must
24392 be called after function DIEs have been generated. */
24394 static void
24395 gen_remaining_tmpl_value_param_die_attribute (void)
24397 if (tmpl_value_parm_die_table)
24399 unsigned i, j;
24400 die_arg_entry *e;
24402 /* We do this in two phases - first get the cases we can
24403 handle during early-finish, preserving those we cannot
24404 (containing symbolic constants where we don't yet know
24405 whether we are going to output the referenced symbols).
24406 For those we try again at late-finish. */
24407 j = 0;
24408 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24410 if (!tree_add_const_value_attribute (e->die, e->arg))
24412 dw_loc_descr_ref loc = NULL;
24413 if (dwarf_version >= 5 || !dwarf_strict)
24414 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
24415 if (loc)
24416 add_AT_loc (e->die, DW_AT_location, loc);
24417 else
24418 (*tmpl_value_parm_die_table)[j++] = *e;
24421 tmpl_value_parm_die_table->truncate (j);
24425 /* Generate generic parameters DIEs for instances of generic types
24426 that have been previously scheduled by
24427 schedule_generic_params_dies_gen. This function must be called
24428 after all the types of the CU have been laid out. */
24430 static void
24431 gen_scheduled_generic_parms_dies (void)
24433 unsigned i;
24434 tree t;
24436 if (!generic_type_instances)
24437 return;
24439 /* We end up "recursing" into schedule_generic_params_dies_gen, so
24440 pretend this generation is part of "early dwarf" as well. */
24441 set_early_dwarf s;
24443 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24444 if (COMPLETE_TYPE_P (t))
24445 gen_generic_params_dies (t);
24447 generic_type_instances = NULL;
24451 /* Replace DW_AT_name for the decl with name. */
24453 static void
24454 dwarf2out_set_name (tree decl, tree name)
24456 dw_die_ref die;
24457 dw_attr_node *attr;
24458 const char *dname;
24460 die = TYPE_SYMTAB_DIE (decl);
24461 if (!die)
24462 return;
24464 dname = dwarf2_name (name, 0);
24465 if (!dname)
24466 return;
24468 attr = get_AT (die, DW_AT_name);
24469 if (attr)
24471 struct indirect_string_node *node;
24473 node = find_AT_string (dname);
24474 /* replace the string. */
24475 attr->dw_attr_val.v.val_str = node;
24478 else
24479 add_name_attribute (die, dname);
24482 /* True if before or during processing of the first function being emitted. */
24483 static bool in_first_function_p = true;
24484 /* True if loc_note during dwarf2out_var_location call might still be
24485 before first real instruction at address equal to .Ltext0. */
24486 static bool maybe_at_text_label_p = true;
24487 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
24488 static unsigned int first_loclabel_num_not_at_text_label;
24490 /* Called by the final INSN scan whenever we see a var location. We
24491 use it to drop labels in the right places, and throw the location in
24492 our lookup table. */
24494 static void
24495 dwarf2out_var_location (rtx_insn *loc_note)
24497 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24498 struct var_loc_node *newloc;
24499 rtx_insn *next_real, *next_note;
24500 rtx_insn *call_insn = NULL;
24501 static const char *last_label;
24502 static const char *last_postcall_label;
24503 static bool last_in_cold_section_p;
24504 static rtx_insn *expected_next_loc_note;
24505 tree decl;
24506 bool var_loc_p;
24508 if (!NOTE_P (loc_note))
24510 if (CALL_P (loc_note))
24512 call_site_count++;
24513 if (SIBLING_CALL_P (loc_note))
24514 tail_call_site_count++;
24515 if (optimize == 0 && !flag_var_tracking)
24517 /* When the var-tracking pass is not running, there is no note
24518 for indirect calls whose target is compile-time known. In this
24519 case, process such calls specifically so that we generate call
24520 sites for them anyway. */
24521 rtx x = PATTERN (loc_note);
24522 if (GET_CODE (x) == PARALLEL)
24523 x = XVECEXP (x, 0, 0);
24524 if (GET_CODE (x) == SET)
24525 x = SET_SRC (x);
24526 if (GET_CODE (x) == CALL)
24527 x = XEXP (x, 0);
24528 if (!MEM_P (x)
24529 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24530 || !SYMBOL_REF_DECL (XEXP (x, 0))
24531 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24532 != FUNCTION_DECL))
24534 call_insn = loc_note;
24535 loc_note = NULL;
24536 var_loc_p = false;
24538 next_real = next_real_insn (call_insn);
24539 next_note = NULL;
24540 cached_next_real_insn = NULL;
24541 goto create_label;
24545 return;
24548 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24549 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24550 return;
24552 /* Optimize processing a large consecutive sequence of location
24553 notes so we don't spend too much time in next_real_insn. If the
24554 next insn is another location note, remember the next_real_insn
24555 calculation for next time. */
24556 next_real = cached_next_real_insn;
24557 if (next_real)
24559 if (expected_next_loc_note != loc_note)
24560 next_real = NULL;
24563 next_note = NEXT_INSN (loc_note);
24564 if (! next_note
24565 || next_note->deleted ()
24566 || ! NOTE_P (next_note)
24567 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24568 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24569 next_note = NULL;
24571 if (! next_real)
24572 next_real = next_real_insn (loc_note);
24574 if (next_note)
24576 expected_next_loc_note = next_note;
24577 cached_next_real_insn = next_real;
24579 else
24580 cached_next_real_insn = NULL;
24582 /* If there are no instructions which would be affected by this note,
24583 don't do anything. */
24584 if (var_loc_p
24585 && next_real == NULL_RTX
24586 && !NOTE_DURING_CALL_P (loc_note))
24587 return;
24589 create_label:
24591 if (next_real == NULL_RTX)
24592 next_real = get_last_insn ();
24594 /* If there were any real insns between note we processed last time
24595 and this note (or if it is the first note), clear
24596 last_{,postcall_}label so that they are not reused this time. */
24597 if (last_var_location_insn == NULL_RTX
24598 || last_var_location_insn != next_real
24599 || last_in_cold_section_p != in_cold_section_p)
24601 last_label = NULL;
24602 last_postcall_label = NULL;
24605 if (var_loc_p)
24607 decl = NOTE_VAR_LOCATION_DECL (loc_note);
24608 newloc = add_var_loc_to_decl (decl, loc_note,
24609 NOTE_DURING_CALL_P (loc_note)
24610 ? last_postcall_label : last_label);
24611 if (newloc == NULL)
24612 return;
24614 else
24616 decl = NULL_TREE;
24617 newloc = NULL;
24620 /* If there were no real insns between note we processed last time
24621 and this note, use the label we emitted last time. Otherwise
24622 create a new label and emit it. */
24623 if (last_label == NULL)
24625 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24626 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24627 loclabel_num++;
24628 last_label = ggc_strdup (loclabel);
24629 /* See if loclabel might be equal to .Ltext0. If yes,
24630 bump first_loclabel_num_not_at_text_label. */
24631 if (!have_multiple_function_sections
24632 && in_first_function_p
24633 && maybe_at_text_label_p)
24635 static rtx_insn *last_start;
24636 rtx_insn *insn;
24637 for (insn = loc_note; insn; insn = previous_insn (insn))
24638 if (insn == last_start)
24639 break;
24640 else if (!NONDEBUG_INSN_P (insn))
24641 continue;
24642 else
24644 rtx body = PATTERN (insn);
24645 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24646 continue;
24647 /* Inline asm could occupy zero bytes. */
24648 else if (GET_CODE (body) == ASM_INPUT
24649 || asm_noperands (body) >= 0)
24650 continue;
24651 #ifdef HAVE_attr_length
24652 else if (get_attr_min_length (insn) == 0)
24653 continue;
24654 #endif
24655 else
24657 /* Assume insn has non-zero length. */
24658 maybe_at_text_label_p = false;
24659 break;
24662 if (maybe_at_text_label_p)
24664 last_start = loc_note;
24665 first_loclabel_num_not_at_text_label = loclabel_num;
24670 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24671 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24673 if (!var_loc_p)
24675 struct call_arg_loc_node *ca_loc
24676 = ggc_cleared_alloc<call_arg_loc_node> ();
24677 rtx_insn *prev
24678 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24680 ca_loc->call_arg_loc_note = loc_note;
24681 ca_loc->next = NULL;
24682 ca_loc->label = last_label;
24683 gcc_assert (prev
24684 && (CALL_P (prev)
24685 || (NONJUMP_INSN_P (prev)
24686 && GET_CODE (PATTERN (prev)) == SEQUENCE
24687 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24688 if (!CALL_P (prev))
24689 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24690 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24692 /* Look for a SYMBOL_REF in the "prev" instruction. */
24693 rtx x = get_call_rtx_from (PATTERN (prev));
24694 if (x)
24696 /* Try to get the call symbol, if any. */
24697 if (MEM_P (XEXP (x, 0)))
24698 x = XEXP (x, 0);
24699 /* First, look for a memory access to a symbol_ref. */
24700 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24701 && SYMBOL_REF_DECL (XEXP (x, 0))
24702 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24703 ca_loc->symbol_ref = XEXP (x, 0);
24704 /* Otherwise, look at a compile-time known user-level function
24705 declaration. */
24706 else if (MEM_P (x)
24707 && MEM_EXPR (x)
24708 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24709 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24712 ca_loc->block = insn_scope (prev);
24713 if (call_arg_locations)
24714 call_arg_loc_last->next = ca_loc;
24715 else
24716 call_arg_locations = ca_loc;
24717 call_arg_loc_last = ca_loc;
24719 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24720 newloc->label = last_label;
24721 else
24723 if (!last_postcall_label)
24725 sprintf (loclabel, "%s-1", last_label);
24726 last_postcall_label = ggc_strdup (loclabel);
24728 newloc->label = last_postcall_label;
24731 last_var_location_insn = next_real;
24732 last_in_cold_section_p = in_cold_section_p;
24735 /* Called from finalize_size_functions for size functions so that their body
24736 can be encoded in the debug info to describe the layout of variable-length
24737 structures. */
24739 static void
24740 dwarf2out_size_function (tree decl)
24742 function_to_dwarf_procedure (decl);
24745 /* Note in one location list that text section has changed. */
24748 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24750 var_loc_list *list = *slot;
24751 if (list->first)
24752 list->last_before_switch
24753 = list->last->next ? list->last->next : list->last;
24754 return 1;
24757 /* Note in all location lists that text section has changed. */
24759 static void
24760 var_location_switch_text_section (void)
24762 if (decl_loc_table == NULL)
24763 return;
24765 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24768 /* Create a new line number table. */
24770 static dw_line_info_table *
24771 new_line_info_table (void)
24773 dw_line_info_table *table;
24775 table = ggc_cleared_alloc<dw_line_info_table> ();
24776 table->file_num = 1;
24777 table->line_num = 1;
24778 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24780 return table;
24783 /* Lookup the "current" table into which we emit line info, so
24784 that we don't have to do it for every source line. */
24786 static void
24787 set_cur_line_info_table (section *sec)
24789 dw_line_info_table *table;
24791 if (sec == text_section)
24792 table = text_section_line_info;
24793 else if (sec == cold_text_section)
24795 table = cold_text_section_line_info;
24796 if (!table)
24798 cold_text_section_line_info = table = new_line_info_table ();
24799 table->end_label = cold_end_label;
24802 else
24804 const char *end_label;
24806 if (flag_reorder_blocks_and_partition)
24808 if (in_cold_section_p)
24809 end_label = crtl->subsections.cold_section_end_label;
24810 else
24811 end_label = crtl->subsections.hot_section_end_label;
24813 else
24815 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24816 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24817 current_function_funcdef_no);
24818 end_label = ggc_strdup (label);
24821 table = new_line_info_table ();
24822 table->end_label = end_label;
24824 vec_safe_push (separate_line_info, table);
24827 if (DWARF2_ASM_LINE_DEBUG_INFO)
24828 table->is_stmt = (cur_line_info_table
24829 ? cur_line_info_table->is_stmt
24830 : DWARF_LINE_DEFAULT_IS_STMT_START);
24831 cur_line_info_table = table;
24835 /* We need to reset the locations at the beginning of each
24836 function. We can't do this in the end_function hook, because the
24837 declarations that use the locations won't have been output when
24838 that hook is called. Also compute have_multiple_function_sections here. */
24840 static void
24841 dwarf2out_begin_function (tree fun)
24843 section *sec = function_section (fun);
24845 if (sec != text_section)
24846 have_multiple_function_sections = true;
24848 if (flag_reorder_blocks_and_partition && !cold_text_section)
24850 gcc_assert (current_function_decl == fun);
24851 cold_text_section = unlikely_text_section ();
24852 switch_to_section (cold_text_section);
24853 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24854 switch_to_section (sec);
24857 dwarf2out_note_section_used ();
24858 call_site_count = 0;
24859 tail_call_site_count = 0;
24861 set_cur_line_info_table (sec);
24864 /* Helper function of dwarf2out_end_function, called only after emitting
24865 the very first function into assembly. Check if some .debug_loc range
24866 might end with a .LVL* label that could be equal to .Ltext0.
24867 In that case we must force using absolute addresses in .debug_loc ranges,
24868 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24869 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24870 list terminator.
24871 Set have_multiple_function_sections to true in that case and
24872 terminate htab traversal. */
24875 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24877 var_loc_list *entry = *slot;
24878 struct var_loc_node *node;
24880 node = entry->first;
24881 if (node && node->next && node->next->label)
24883 unsigned int i;
24884 const char *label = node->next->label;
24885 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24887 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24889 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24890 if (strcmp (label, loclabel) == 0)
24892 have_multiple_function_sections = true;
24893 return 0;
24897 return 1;
24900 /* Hook called after emitting a function into assembly.
24901 This does something only for the very first function emitted. */
24903 static void
24904 dwarf2out_end_function (unsigned int)
24906 if (in_first_function_p
24907 && !have_multiple_function_sections
24908 && first_loclabel_num_not_at_text_label
24909 && decl_loc_table)
24910 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24911 in_first_function_p = false;
24912 maybe_at_text_label_p = false;
24915 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
24916 front-ends register a translation unit even before dwarf2out_init is
24917 called. */
24918 static tree main_translation_unit = NULL_TREE;
24920 /* Hook called by front-ends after they built their main translation unit.
24921 Associate comp_unit_die to UNIT. */
24923 static void
24924 dwarf2out_register_main_translation_unit (tree unit)
24926 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24927 && main_translation_unit == NULL_TREE);
24928 main_translation_unit = unit;
24929 /* If dwarf2out_init has not been called yet, it will perform the association
24930 itself looking at main_translation_unit. */
24931 if (decl_die_table != NULL)
24932 equate_decl_number_to_die (unit, comp_unit_die ());
24935 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
24937 static void
24938 push_dw_line_info_entry (dw_line_info_table *table,
24939 enum dw_line_info_opcode opcode, unsigned int val)
24941 dw_line_info_entry e;
24942 e.opcode = opcode;
24943 e.val = val;
24944 vec_safe_push (table->entries, e);
24947 /* Output a label to mark the beginning of a source code line entry
24948 and record information relating to this source line, in
24949 'line_info_table' for later output of the .debug_line section. */
24950 /* ??? The discriminator parameter ought to be unsigned. */
24952 static void
24953 dwarf2out_source_line (unsigned int line, const char *filename,
24954 int discriminator, bool is_stmt)
24956 unsigned int file_num;
24957 dw_line_info_table *table;
24959 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
24960 return;
24962 /* The discriminator column was added in dwarf4. Simplify the below
24963 by simply removing it if we're not supposed to output it. */
24964 if (dwarf_version < 4 && dwarf_strict)
24965 discriminator = 0;
24967 table = cur_line_info_table;
24968 file_num = maybe_emit_file (lookup_filename (filename));
24970 /* ??? TODO: Elide duplicate line number entries. Traditionally,
24971 the debugger has used the second (possibly duplicate) line number
24972 at the beginning of the function to mark the end of the prologue.
24973 We could eliminate any other duplicates within the function. For
24974 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
24975 that second line number entry. */
24976 /* Recall that this end-of-prologue indication is *not* the same thing
24977 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
24978 to which the hook corresponds, follows the last insn that was
24979 emitted by gen_prologue. What we need is to precede the first insn
24980 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
24981 insn that corresponds to something the user wrote. These may be
24982 very different locations once scheduling is enabled. */
24984 if (0 && file_num == table->file_num
24985 && line == table->line_num
24986 && discriminator == table->discrim_num
24987 && is_stmt == table->is_stmt)
24988 return;
24990 switch_to_section (current_function_section ());
24992 /* If requested, emit something human-readable. */
24993 if (flag_debug_asm)
24994 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
24996 if (DWARF2_ASM_LINE_DEBUG_INFO)
24998 /* Emit the .loc directive understood by GNU as. */
24999 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
25000 file_num, line, is_stmt, discriminator */
25001 fputs ("\t.loc ", asm_out_file);
25002 fprint_ul (asm_out_file, file_num);
25003 putc (' ', asm_out_file);
25004 fprint_ul (asm_out_file, line);
25005 putc (' ', asm_out_file);
25006 putc ('0', asm_out_file);
25008 if (is_stmt != table->is_stmt)
25010 fputs (" is_stmt ", asm_out_file);
25011 putc (is_stmt ? '1' : '0', asm_out_file);
25013 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
25015 gcc_assert (discriminator > 0);
25016 fputs (" discriminator ", asm_out_file);
25017 fprint_ul (asm_out_file, (unsigned long) discriminator);
25019 putc ('\n', asm_out_file);
25021 else
25023 unsigned int label_num = ++line_info_label_num;
25025 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
25027 push_dw_line_info_entry (table, LI_set_address, label_num);
25028 if (file_num != table->file_num)
25029 push_dw_line_info_entry (table, LI_set_file, file_num);
25030 if (discriminator != table->discrim_num)
25031 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
25032 if (is_stmt != table->is_stmt)
25033 push_dw_line_info_entry (table, LI_negate_stmt, 0);
25034 push_dw_line_info_entry (table, LI_set_line, line);
25037 table->file_num = file_num;
25038 table->line_num = line;
25039 table->discrim_num = discriminator;
25040 table->is_stmt = is_stmt;
25041 table->in_use = true;
25044 /* Record the beginning of a new source file. */
25046 static void
25047 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
25049 if (flag_eliminate_dwarf2_dups)
25051 /* Record the beginning of the file for break_out_includes. */
25052 dw_die_ref bincl_die;
25054 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
25055 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
25058 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25060 macinfo_entry e;
25061 e.code = DW_MACINFO_start_file;
25062 e.lineno = lineno;
25063 e.info = ggc_strdup (filename);
25064 vec_safe_push (macinfo_table, e);
25068 /* Record the end of a source file. */
25070 static void
25071 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
25073 if (flag_eliminate_dwarf2_dups)
25074 /* Record the end of the file for break_out_includes. */
25075 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
25077 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25079 macinfo_entry e;
25080 e.code = DW_MACINFO_end_file;
25081 e.lineno = lineno;
25082 e.info = NULL;
25083 vec_safe_push (macinfo_table, e);
25087 /* Called from debug_define in toplev.c. The `buffer' parameter contains
25088 the tail part of the directive line, i.e. the part which is past the
25089 initial whitespace, #, whitespace, directive-name, whitespace part. */
25091 static void
25092 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
25093 const char *buffer ATTRIBUTE_UNUSED)
25095 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25097 macinfo_entry e;
25098 /* Insert a dummy first entry to be able to optimize the whole
25099 predefined macro block using DW_MACRO_GNU_transparent_include. */
25100 if (macinfo_table->is_empty () && lineno <= 1)
25102 e.code = 0;
25103 e.lineno = 0;
25104 e.info = NULL;
25105 vec_safe_push (macinfo_table, e);
25107 e.code = DW_MACINFO_define;
25108 e.lineno = lineno;
25109 e.info = ggc_strdup (buffer);
25110 vec_safe_push (macinfo_table, e);
25114 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
25115 the tail part of the directive line, i.e. the part which is past the
25116 initial whitespace, #, whitespace, directive-name, whitespace part. */
25118 static void
25119 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
25120 const char *buffer ATTRIBUTE_UNUSED)
25122 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25124 macinfo_entry e;
25125 /* Insert a dummy first entry to be able to optimize the whole
25126 predefined macro block using DW_MACRO_GNU_transparent_include. */
25127 if (macinfo_table->is_empty () && lineno <= 1)
25129 e.code = 0;
25130 e.lineno = 0;
25131 e.info = NULL;
25132 vec_safe_push (macinfo_table, e);
25134 e.code = DW_MACINFO_undef;
25135 e.lineno = lineno;
25136 e.info = ggc_strdup (buffer);
25137 vec_safe_push (macinfo_table, e);
25141 /* Helpers to manipulate hash table of CUs. */
25143 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
25145 static inline hashval_t hash (const macinfo_entry *);
25146 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
25149 inline hashval_t
25150 macinfo_entry_hasher::hash (const macinfo_entry *entry)
25152 return htab_hash_string (entry->info);
25155 inline bool
25156 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
25157 const macinfo_entry *entry2)
25159 return !strcmp (entry1->info, entry2->info);
25162 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
25164 /* Output a single .debug_macinfo entry. */
25166 static void
25167 output_macinfo_op (macinfo_entry *ref)
25169 int file_num;
25170 size_t len;
25171 struct indirect_string_node *node;
25172 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25173 struct dwarf_file_data *fd;
25175 switch (ref->code)
25177 case DW_MACINFO_start_file:
25178 fd = lookup_filename (ref->info);
25179 file_num = maybe_emit_file (fd);
25180 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
25181 dw2_asm_output_data_uleb128 (ref->lineno,
25182 "Included from line number %lu",
25183 (unsigned long) ref->lineno);
25184 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
25185 break;
25186 case DW_MACINFO_end_file:
25187 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
25188 break;
25189 case DW_MACINFO_define:
25190 case DW_MACINFO_undef:
25191 len = strlen (ref->info) + 1;
25192 if (!dwarf_strict
25193 && len > DWARF_OFFSET_SIZE
25194 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25195 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25197 ref->code = ref->code == DW_MACINFO_define
25198 ? DW_MACRO_GNU_define_indirect
25199 : DW_MACRO_GNU_undef_indirect;
25200 output_macinfo_op (ref);
25201 return;
25203 dw2_asm_output_data (1, ref->code,
25204 ref->code == DW_MACINFO_define
25205 ? "Define macro" : "Undefine macro");
25206 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25207 (unsigned long) ref->lineno);
25208 dw2_asm_output_nstring (ref->info, -1, "The macro");
25209 break;
25210 case DW_MACRO_GNU_define_indirect:
25211 case DW_MACRO_GNU_undef_indirect:
25212 node = find_AT_string (ref->info);
25213 gcc_assert (node
25214 && ((node->form == DW_FORM_strp)
25215 || (node->form == DW_FORM_GNU_str_index)));
25216 dw2_asm_output_data (1, ref->code,
25217 ref->code == DW_MACRO_GNU_define_indirect
25218 ? "Define macro indirect"
25219 : "Undefine macro indirect");
25220 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25221 (unsigned long) ref->lineno);
25222 if (node->form == DW_FORM_strp)
25223 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
25224 debug_str_section, "The macro: \"%s\"",
25225 ref->info);
25226 else
25227 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
25228 ref->info);
25229 break;
25230 case DW_MACRO_GNU_transparent_include:
25231 dw2_asm_output_data (1, ref->code, "Transparent include");
25232 ASM_GENERATE_INTERNAL_LABEL (label,
25233 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
25234 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
25235 break;
25236 default:
25237 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
25238 ASM_COMMENT_START, (unsigned long) ref->code);
25239 break;
25243 /* Attempt to make a sequence of define/undef macinfo ops shareable with
25244 other compilation unit .debug_macinfo sections. IDX is the first
25245 index of a define/undef, return the number of ops that should be
25246 emitted in a comdat .debug_macinfo section and emit
25247 a DW_MACRO_GNU_transparent_include entry referencing it.
25248 If the define/undef entry should be emitted normally, return 0. */
25250 static unsigned
25251 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25252 macinfo_hash_type **macinfo_htab)
25254 macinfo_entry *first, *second, *cur, *inc;
25255 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25256 unsigned char checksum[16];
25257 struct md5_ctx ctx;
25258 char *grp_name, *tail;
25259 const char *base;
25260 unsigned int i, count, encoded_filename_len, linebuf_len;
25261 macinfo_entry **slot;
25263 first = &(*macinfo_table)[idx];
25264 second = &(*macinfo_table)[idx + 1];
25266 /* Optimize only if there are at least two consecutive define/undef ops,
25267 and either all of them are before first DW_MACINFO_start_file
25268 with lineno {0,1} (i.e. predefined macro block), or all of them are
25269 in some included header file. */
25270 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25271 return 0;
25272 if (vec_safe_is_empty (files))
25274 if (first->lineno > 1 || second->lineno > 1)
25275 return 0;
25277 else if (first->lineno == 0)
25278 return 0;
25280 /* Find the last define/undef entry that can be grouped together
25281 with first and at the same time compute md5 checksum of their
25282 codes, linenumbers and strings. */
25283 md5_init_ctx (&ctx);
25284 for (i = idx; macinfo_table->iterate (i, &cur); i++)
25285 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25286 break;
25287 else if (vec_safe_is_empty (files) && cur->lineno > 1)
25288 break;
25289 else
25291 unsigned char code = cur->code;
25292 md5_process_bytes (&code, 1, &ctx);
25293 checksum_uleb128 (cur->lineno, &ctx);
25294 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25296 md5_finish_ctx (&ctx, checksum);
25297 count = i - idx;
25299 /* From the containing include filename (if any) pick up just
25300 usable characters from its basename. */
25301 if (vec_safe_is_empty (files))
25302 base = "";
25303 else
25304 base = lbasename (files->last ().info);
25305 for (encoded_filename_len = 0, i = 0; base[i]; i++)
25306 if (ISIDNUM (base[i]) || base[i] == '.')
25307 encoded_filename_len++;
25308 /* Count . at the end. */
25309 if (encoded_filename_len)
25310 encoded_filename_len++;
25312 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25313 linebuf_len = strlen (linebuf);
25315 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
25316 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25317 + 16 * 2 + 1);
25318 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25319 tail = grp_name + 4;
25320 if (encoded_filename_len)
25322 for (i = 0; base[i]; i++)
25323 if (ISIDNUM (base[i]) || base[i] == '.')
25324 *tail++ = base[i];
25325 *tail++ = '.';
25327 memcpy (tail, linebuf, linebuf_len);
25328 tail += linebuf_len;
25329 *tail++ = '.';
25330 for (i = 0; i < 16; i++)
25331 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25333 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25334 in the empty vector entry before the first define/undef. */
25335 inc = &(*macinfo_table)[idx - 1];
25336 inc->code = DW_MACRO_GNU_transparent_include;
25337 inc->lineno = 0;
25338 inc->info = ggc_strdup (grp_name);
25339 if (!*macinfo_htab)
25340 *macinfo_htab = new macinfo_hash_type (10);
25341 /* Avoid emitting duplicates. */
25342 slot = (*macinfo_htab)->find_slot (inc, INSERT);
25343 if (*slot != NULL)
25345 inc->code = 0;
25346 inc->info = NULL;
25347 /* If such an entry has been used before, just emit
25348 a DW_MACRO_GNU_transparent_include op. */
25349 inc = *slot;
25350 output_macinfo_op (inc);
25351 /* And clear all macinfo_entry in the range to avoid emitting them
25352 in the second pass. */
25353 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25355 cur->code = 0;
25356 cur->info = NULL;
25359 else
25361 *slot = inc;
25362 inc->lineno = (*macinfo_htab)->elements ();
25363 output_macinfo_op (inc);
25365 return count;
25368 /* Save any strings needed by the macinfo table in the debug str
25369 table. All strings must be collected into the table by the time
25370 index_string is called. */
25372 static void
25373 save_macinfo_strings (void)
25375 unsigned len;
25376 unsigned i;
25377 macinfo_entry *ref;
25379 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25381 switch (ref->code)
25383 /* Match the logic in output_macinfo_op to decide on
25384 indirect strings. */
25385 case DW_MACINFO_define:
25386 case DW_MACINFO_undef:
25387 len = strlen (ref->info) + 1;
25388 if (!dwarf_strict
25389 && len > DWARF_OFFSET_SIZE
25390 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25391 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25392 set_indirect_string (find_AT_string (ref->info));
25393 break;
25394 case DW_MACRO_GNU_define_indirect:
25395 case DW_MACRO_GNU_undef_indirect:
25396 set_indirect_string (find_AT_string (ref->info));
25397 break;
25398 default:
25399 break;
25404 /* Output macinfo section(s). */
25406 static void
25407 output_macinfo (void)
25409 unsigned i;
25410 unsigned long length = vec_safe_length (macinfo_table);
25411 macinfo_entry *ref;
25412 vec<macinfo_entry, va_gc> *files = NULL;
25413 macinfo_hash_type *macinfo_htab = NULL;
25415 if (! length)
25416 return;
25418 /* output_macinfo* uses these interchangeably. */
25419 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25420 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25421 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25422 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25424 /* For .debug_macro emit the section header. */
25425 if (!dwarf_strict)
25427 dw2_asm_output_data (2, 4, "DWARF macro version number");
25428 if (DWARF_OFFSET_SIZE == 8)
25429 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25430 else
25431 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25432 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25433 (!dwarf_split_debug_info ? debug_line_section_label
25434 : debug_skeleton_line_section_label),
25435 debug_line_section, NULL);
25438 /* In the first loop, it emits the primary .debug_macinfo section
25439 and after each emitted op the macinfo_entry is cleared.
25440 If a longer range of define/undef ops can be optimized using
25441 DW_MACRO_GNU_transparent_include, the
25442 DW_MACRO_GNU_transparent_include op is emitted and kept in
25443 the vector before the first define/undef in the range and the
25444 whole range of define/undef ops is not emitted and kept. */
25445 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25447 switch (ref->code)
25449 case DW_MACINFO_start_file:
25450 vec_safe_push (files, *ref);
25451 break;
25452 case DW_MACINFO_end_file:
25453 if (!vec_safe_is_empty (files))
25454 files->pop ();
25455 break;
25456 case DW_MACINFO_define:
25457 case DW_MACINFO_undef:
25458 if (!dwarf_strict
25459 && HAVE_COMDAT_GROUP
25460 && vec_safe_length (files) != 1
25461 && i > 0
25462 && i + 1 < length
25463 && (*macinfo_table)[i - 1].code == 0)
25465 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25466 if (count)
25468 i += count - 1;
25469 continue;
25472 break;
25473 case 0:
25474 /* A dummy entry may be inserted at the beginning to be able
25475 to optimize the whole block of predefined macros. */
25476 if (i == 0)
25477 continue;
25478 default:
25479 break;
25481 output_macinfo_op (ref);
25482 ref->info = NULL;
25483 ref->code = 0;
25486 if (!macinfo_htab)
25487 return;
25489 delete macinfo_htab;
25490 macinfo_htab = NULL;
25492 /* If any DW_MACRO_GNU_transparent_include were used, on those
25493 DW_MACRO_GNU_transparent_include entries terminate the
25494 current chain and switch to a new comdat .debug_macinfo
25495 section and emit the define/undef entries within it. */
25496 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25497 switch (ref->code)
25499 case 0:
25500 continue;
25501 case DW_MACRO_GNU_transparent_include:
25503 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25504 tree comdat_key = get_identifier (ref->info);
25505 /* Terminate the previous .debug_macinfo section. */
25506 dw2_asm_output_data (1, 0, "End compilation unit");
25507 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
25508 SECTION_DEBUG
25509 | SECTION_LINKONCE,
25510 comdat_key);
25511 ASM_GENERATE_INTERNAL_LABEL (label,
25512 DEBUG_MACRO_SECTION_LABEL,
25513 ref->lineno);
25514 ASM_OUTPUT_LABEL (asm_out_file, label);
25515 ref->code = 0;
25516 ref->info = NULL;
25517 dw2_asm_output_data (2, 4, "DWARF macro version number");
25518 if (DWARF_OFFSET_SIZE == 8)
25519 dw2_asm_output_data (1, 1, "Flags: 64-bit");
25520 else
25521 dw2_asm_output_data (1, 0, "Flags: 32-bit");
25523 break;
25524 case DW_MACINFO_define:
25525 case DW_MACINFO_undef:
25526 output_macinfo_op (ref);
25527 ref->code = 0;
25528 ref->info = NULL;
25529 break;
25530 default:
25531 gcc_unreachable ();
25535 /* Set up for Dwarf output at the start of compilation. */
25537 static void
25538 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25540 /* This option is currently broken, see (PR53118 and PR46102). */
25541 if (flag_eliminate_dwarf2_dups
25542 && strstr (lang_hooks.name, "C++"))
25544 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25545 flag_eliminate_dwarf2_dups = 0;
25548 /* Allocate the file_table. */
25549 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25551 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25552 /* Allocate the decl_die_table. */
25553 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25555 /* Allocate the decl_loc_table. */
25556 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25558 /* Allocate the cached_dw_loc_list_table. */
25559 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25561 /* Allocate the initial hunk of the decl_scope_table. */
25562 vec_alloc (decl_scope_table, 256);
25564 /* Allocate the initial hunk of the abbrev_die_table. */
25565 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25566 (ABBREV_DIE_TABLE_INCREMENT);
25567 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25568 /* Zero-th entry is allocated, but unused. */
25569 abbrev_die_table_in_use = 1;
25571 /* Allocate the dwarf_proc_stack_usage_map. */
25572 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25574 /* Allocate the pubtypes and pubnames vectors. */
25575 vec_alloc (pubname_table, 32);
25576 vec_alloc (pubtype_table, 32);
25578 vec_alloc (incomplete_types, 64);
25580 vec_alloc (used_rtx_array, 32);
25582 if (!dwarf_split_debug_info)
25584 debug_info_section = get_section (DEBUG_INFO_SECTION,
25585 SECTION_DEBUG, NULL);
25586 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25587 SECTION_DEBUG, NULL);
25588 debug_loc_section = get_section (DEBUG_LOC_SECTION,
25589 SECTION_DEBUG, NULL);
25591 else
25593 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25594 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25595 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25596 SECTION_DEBUG | SECTION_EXCLUDE,
25597 NULL);
25598 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25599 SECTION_DEBUG, NULL);
25600 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25601 SECTION_DEBUG, NULL);
25602 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25603 SECTION_DEBUG, NULL);
25604 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25605 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25607 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25608 the main .o, but the skeleton_line goes into the split off dwo. */
25609 debug_skeleton_line_section
25610 = get_section (DEBUG_DWO_LINE_SECTION,
25611 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25612 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25613 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25614 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
25615 SECTION_DEBUG | SECTION_EXCLUDE,
25616 NULL);
25617 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25618 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25619 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25620 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25621 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25622 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25624 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25625 SECTION_DEBUG, NULL);
25626 debug_macinfo_section = get_section (dwarf_strict
25627 ? DEBUG_MACINFO_SECTION
25628 : DEBUG_MACRO_SECTION,
25629 DEBUG_MACRO_SECTION_FLAGS, NULL);
25630 debug_line_section = get_section (DEBUG_LINE_SECTION,
25631 SECTION_DEBUG, NULL);
25632 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25633 SECTION_DEBUG, NULL);
25634 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25635 SECTION_DEBUG, NULL);
25636 debug_str_section = get_section (DEBUG_STR_SECTION,
25637 DEBUG_STR_SECTION_FLAGS, NULL);
25638 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25639 SECTION_DEBUG, NULL);
25640 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25641 SECTION_DEBUG, NULL);
25643 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25644 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25645 DEBUG_ABBREV_SECTION_LABEL, 0);
25646 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25647 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25648 COLD_TEXT_SECTION_LABEL, 0);
25649 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25651 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25652 DEBUG_INFO_SECTION_LABEL, 0);
25653 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25654 DEBUG_LINE_SECTION_LABEL, 0);
25655 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25656 DEBUG_RANGES_SECTION_LABEL, 0);
25657 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25658 DEBUG_ADDR_SECTION_LABEL, 0);
25659 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25660 dwarf_strict
25661 ? DEBUG_MACINFO_SECTION_LABEL
25662 : DEBUG_MACRO_SECTION_LABEL, 0);
25663 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25665 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25666 vec_alloc (macinfo_table, 64);
25668 switch_to_section (text_section);
25669 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25670 #endif
25672 /* Make sure the line number table for .text always exists. */
25673 text_section_line_info = new_line_info_table ();
25674 text_section_line_info->end_label = text_end_label;
25676 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25677 cur_line_info_table = text_section_line_info;
25678 #endif
25680 /* If front-ends already registered a main translation unit but we were not
25681 ready to perform the association, do this now. */
25682 if (main_translation_unit != NULL_TREE)
25683 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25686 /* Called before compile () starts outputtting functions, variables
25687 and toplevel asms into assembly. */
25689 static void
25690 dwarf2out_assembly_start (void)
25692 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25693 && dwarf2out_do_cfi_asm ()
25694 && (!(flag_unwind_tables || flag_exceptions)
25695 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25696 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25699 /* A helper function for dwarf2out_finish called through
25700 htab_traverse. Assign a string its index. All strings must be
25701 collected into the table by the time index_string is called,
25702 because the indexing code relies on htab_traverse to traverse nodes
25703 in the same order for each run. */
25706 index_string (indirect_string_node **h, unsigned int *index)
25708 indirect_string_node *node = *h;
25710 find_string_form (node);
25711 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25713 gcc_assert (node->index == NO_INDEX_ASSIGNED);
25714 node->index = *index;
25715 *index += 1;
25717 return 1;
25720 /* A helper function for output_indirect_strings called through
25721 htab_traverse. Output the offset to a string and update the
25722 current offset. */
25725 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25727 indirect_string_node *node = *h;
25729 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25731 /* Assert that this node has been assigned an index. */
25732 gcc_assert (node->index != NO_INDEX_ASSIGNED
25733 && node->index != NOT_INDEXED);
25734 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25735 "indexed string 0x%x: %s", node->index, node->str);
25736 *offset += strlen (node->str) + 1;
25738 return 1;
25741 /* A helper function for dwarf2out_finish called through
25742 htab_traverse. Output the indexed string. */
25745 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25747 struct indirect_string_node *node = *h;
25749 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25751 /* Assert that the strings are output in the same order as their
25752 indexes were assigned. */
25753 gcc_assert (*cur_idx == node->index);
25754 assemble_string (node->str, strlen (node->str) + 1);
25755 *cur_idx += 1;
25757 return 1;
25760 /* A helper function for dwarf2out_finish called through
25761 htab_traverse. Emit one queued .debug_str string. */
25764 output_indirect_string (indirect_string_node **h, void *)
25766 struct indirect_string_node *node = *h;
25768 node->form = find_string_form (node);
25769 if (node->form == DW_FORM_strp && node->refcount > 0)
25771 ASM_OUTPUT_LABEL (asm_out_file, node->label);
25772 assemble_string (node->str, strlen (node->str) + 1);
25775 return 1;
25778 /* Output the indexed string table. */
25780 static void
25781 output_indirect_strings (void)
25783 switch_to_section (debug_str_section);
25784 if (!dwarf_split_debug_info)
25785 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25786 else
25788 unsigned int offset = 0;
25789 unsigned int cur_idx = 0;
25791 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25793 switch_to_section (debug_str_offsets_section);
25794 debug_str_hash->traverse_noresize
25795 <unsigned int *, output_index_string_offset> (&offset);
25796 switch_to_section (debug_str_dwo_section);
25797 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25798 (&cur_idx);
25802 /* Callback for htab_traverse to assign an index to an entry in the
25803 table, and to write that entry to the .debug_addr section. */
25806 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25808 addr_table_entry *entry = *slot;
25810 if (entry->refcount == 0)
25812 gcc_assert (entry->index == NO_INDEX_ASSIGNED
25813 || entry->index == NOT_INDEXED);
25814 return 1;
25817 gcc_assert (entry->index == *cur_index);
25818 (*cur_index)++;
25820 switch (entry->kind)
25822 case ate_kind_rtx:
25823 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25824 "0x%x", entry->index);
25825 break;
25826 case ate_kind_rtx_dtprel:
25827 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25828 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25829 DWARF2_ADDR_SIZE,
25830 entry->addr.rtl);
25831 fputc ('\n', asm_out_file);
25832 break;
25833 case ate_kind_label:
25834 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25835 "0x%x", entry->index);
25836 break;
25837 default:
25838 gcc_unreachable ();
25840 return 1;
25843 /* Produce the .debug_addr section. */
25845 static void
25846 output_addr_table (void)
25848 unsigned int index = 0;
25849 if (addr_index_table == NULL || addr_index_table->size () == 0)
25850 return;
25852 switch_to_section (debug_addr_section);
25853 addr_index_table
25854 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25857 #if ENABLE_ASSERT_CHECKING
25858 /* Verify that all marks are clear. */
25860 static void
25861 verify_marks_clear (dw_die_ref die)
25863 dw_die_ref c;
25865 gcc_assert (! die->die_mark);
25866 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25868 #endif /* ENABLE_ASSERT_CHECKING */
25870 /* Clear the marks for a die and its children.
25871 Be cool if the mark isn't set. */
25873 static void
25874 prune_unmark_dies (dw_die_ref die)
25876 dw_die_ref c;
25878 if (die->die_mark)
25879 die->die_mark = 0;
25880 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25883 /* Given LOC that is referenced by a DIE we're marking as used, find all
25884 referenced DWARF procedures it references and mark them as used. */
25886 static void
25887 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25889 for (; loc != NULL; loc = loc->dw_loc_next)
25890 switch (loc->dw_loc_opc)
25892 case DW_OP_GNU_implicit_pointer:
25893 case DW_OP_GNU_convert:
25894 case DW_OP_GNU_reinterpret:
25895 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25896 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25897 break;
25898 case DW_OP_call2:
25899 case DW_OP_call4:
25900 case DW_OP_call_ref:
25901 case DW_OP_GNU_const_type:
25902 case DW_OP_GNU_parameter_ref:
25903 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
25904 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25905 break;
25906 case DW_OP_GNU_regval_type:
25907 case DW_OP_GNU_deref_type:
25908 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
25909 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
25910 break;
25911 case DW_OP_GNU_entry_value:
25912 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
25913 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
25914 break;
25915 default:
25916 break;
25920 /* Given DIE that we're marking as used, find any other dies
25921 it references as attributes and mark them as used. */
25923 static void
25924 prune_unused_types_walk_attribs (dw_die_ref die)
25926 dw_attr_node *a;
25927 unsigned ix;
25929 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25931 switch (AT_class (a))
25933 /* Make sure DWARF procedures referenced by location descriptions will
25934 get emitted. */
25935 case dw_val_class_loc:
25936 prune_unused_types_walk_loc_descr (AT_loc (a));
25937 break;
25938 case dw_val_class_loc_list:
25939 for (dw_loc_list_ref list = AT_loc_list (a);
25940 list != NULL;
25941 list = list->dw_loc_next)
25942 prune_unused_types_walk_loc_descr (list->expr);
25943 break;
25945 case dw_val_class_die_ref:
25946 /* A reference to another DIE.
25947 Make sure that it will get emitted.
25948 If it was broken out into a comdat group, don't follow it. */
25949 if (! AT_ref (a)->comdat_type_p
25950 || a->dw_attr == DW_AT_specification)
25951 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
25952 break;
25954 case dw_val_class_str:
25955 /* Set the string's refcount to 0 so that prune_unused_types_mark
25956 accounts properly for it. */
25957 a->dw_attr_val.v.val_str->refcount = 0;
25958 break;
25960 default:
25961 break;
25966 /* Mark the generic parameters and arguments children DIEs of DIE. */
25968 static void
25969 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
25971 dw_die_ref c;
25973 if (die == NULL || die->die_child == NULL)
25974 return;
25975 c = die->die_child;
25978 if (is_template_parameter (c))
25979 prune_unused_types_mark (c, 1);
25980 c = c->die_sib;
25981 } while (c && c != die->die_child);
25984 /* Mark DIE as being used. If DOKIDS is true, then walk down
25985 to DIE's children. */
25987 static void
25988 prune_unused_types_mark (dw_die_ref die, int dokids)
25990 dw_die_ref c;
25992 if (die->die_mark == 0)
25994 /* We haven't done this node yet. Mark it as used. */
25995 die->die_mark = 1;
25996 /* If this is the DIE of a generic type instantiation,
25997 mark the children DIEs that describe its generic parms and
25998 args. */
25999 prune_unused_types_mark_generic_parms_dies (die);
26001 /* We also have to mark its parents as used.
26002 (But we don't want to mark our parent's kids due to this,
26003 unless it is a class.) */
26004 if (die->die_parent)
26005 prune_unused_types_mark (die->die_parent,
26006 class_scope_p (die->die_parent));
26008 /* Mark any referenced nodes. */
26009 prune_unused_types_walk_attribs (die);
26011 /* If this node is a specification,
26012 also mark the definition, if it exists. */
26013 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
26014 prune_unused_types_mark (die->die_definition, 1);
26017 if (dokids && die->die_mark != 2)
26019 /* We need to walk the children, but haven't done so yet.
26020 Remember that we've walked the kids. */
26021 die->die_mark = 2;
26023 /* If this is an array type, we need to make sure our
26024 kids get marked, even if they're types. If we're
26025 breaking out types into comdat sections, do this
26026 for all type definitions. */
26027 if (die->die_tag == DW_TAG_array_type
26028 || (use_debug_types
26029 && is_type_die (die) && ! is_declaration_die (die)))
26030 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
26031 else
26032 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26036 /* For local classes, look if any static member functions were emitted
26037 and if so, mark them. */
26039 static void
26040 prune_unused_types_walk_local_classes (dw_die_ref die)
26042 dw_die_ref c;
26044 if (die->die_mark == 2)
26045 return;
26047 switch (die->die_tag)
26049 case DW_TAG_structure_type:
26050 case DW_TAG_union_type:
26051 case DW_TAG_class_type:
26052 break;
26054 case DW_TAG_subprogram:
26055 if (!get_AT_flag (die, DW_AT_declaration)
26056 || die->die_definition != NULL)
26057 prune_unused_types_mark (die, 1);
26058 return;
26060 default:
26061 return;
26064 /* Mark children. */
26065 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
26068 /* Walk the tree DIE and mark types that we actually use. */
26070 static void
26071 prune_unused_types_walk (dw_die_ref die)
26073 dw_die_ref c;
26075 /* Don't do anything if this node is already marked and
26076 children have been marked as well. */
26077 if (die->die_mark == 2)
26078 return;
26080 switch (die->die_tag)
26082 case DW_TAG_structure_type:
26083 case DW_TAG_union_type:
26084 case DW_TAG_class_type:
26085 if (die->die_perennial_p)
26086 break;
26088 for (c = die->die_parent; c; c = c->die_parent)
26089 if (c->die_tag == DW_TAG_subprogram)
26090 break;
26092 /* Finding used static member functions inside of classes
26093 is needed just for local classes, because for other classes
26094 static member function DIEs with DW_AT_specification
26095 are emitted outside of the DW_TAG_*_type. If we ever change
26096 it, we'd need to call this even for non-local classes. */
26097 if (c)
26098 prune_unused_types_walk_local_classes (die);
26100 /* It's a type node --- don't mark it. */
26101 return;
26103 case DW_TAG_const_type:
26104 case DW_TAG_packed_type:
26105 case DW_TAG_pointer_type:
26106 case DW_TAG_reference_type:
26107 case DW_TAG_rvalue_reference_type:
26108 case DW_TAG_volatile_type:
26109 case DW_TAG_typedef:
26110 case DW_TAG_array_type:
26111 case DW_TAG_interface_type:
26112 case DW_TAG_friend:
26113 case DW_TAG_enumeration_type:
26114 case DW_TAG_subroutine_type:
26115 case DW_TAG_string_type:
26116 case DW_TAG_set_type:
26117 case DW_TAG_subrange_type:
26118 case DW_TAG_ptr_to_member_type:
26119 case DW_TAG_file_type:
26120 /* Type nodes are useful only when other DIEs reference them --- don't
26121 mark them. */
26122 /* FALLTHROUGH */
26124 case DW_TAG_dwarf_procedure:
26125 /* Likewise for DWARF procedures. */
26127 if (die->die_perennial_p)
26128 break;
26130 return;
26132 default:
26133 /* Mark everything else. */
26134 break;
26137 if (die->die_mark == 0)
26139 die->die_mark = 1;
26141 /* Now, mark any dies referenced from here. */
26142 prune_unused_types_walk_attribs (die);
26145 die->die_mark = 2;
26147 /* Mark children. */
26148 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26151 /* Increment the string counts on strings referred to from DIE's
26152 attributes. */
26154 static void
26155 prune_unused_types_update_strings (dw_die_ref die)
26157 dw_attr_node *a;
26158 unsigned ix;
26160 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26161 if (AT_class (a) == dw_val_class_str)
26163 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
26164 s->refcount++;
26165 /* Avoid unnecessarily putting strings that are used less than
26166 twice in the hash table. */
26167 if (s->refcount
26168 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
26170 indirect_string_node **slot
26171 = debug_str_hash->find_slot_with_hash (s->str,
26172 htab_hash_string (s->str),
26173 INSERT);
26174 gcc_assert (*slot == NULL);
26175 *slot = s;
26180 /* Remove from the tree DIE any dies that aren't marked. */
26182 static void
26183 prune_unused_types_prune (dw_die_ref die)
26185 dw_die_ref c;
26187 gcc_assert (die->die_mark);
26188 prune_unused_types_update_strings (die);
26190 if (! die->die_child)
26191 return;
26193 c = die->die_child;
26194 do {
26195 dw_die_ref prev = c;
26196 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
26197 if (c == die->die_child)
26199 /* No marked children between 'prev' and the end of the list. */
26200 if (prev == c)
26201 /* No marked children at all. */
26202 die->die_child = NULL;
26203 else
26205 prev->die_sib = c->die_sib;
26206 die->die_child = prev;
26208 return;
26211 if (c != prev->die_sib)
26212 prev->die_sib = c;
26213 prune_unused_types_prune (c);
26214 } while (c != die->die_child);
26217 /* Remove dies representing declarations that we never use. */
26219 static void
26220 prune_unused_types (void)
26222 unsigned int i;
26223 limbo_die_node *node;
26224 comdat_type_node *ctnode;
26225 pubname_entry *pub;
26226 dw_die_ref base_type;
26228 #if ENABLE_ASSERT_CHECKING
26229 /* All the marks should already be clear. */
26230 verify_marks_clear (comp_unit_die ());
26231 for (node = limbo_die_list; node; node = node->next)
26232 verify_marks_clear (node->die);
26233 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26234 verify_marks_clear (ctnode->root_die);
26235 #endif /* ENABLE_ASSERT_CHECKING */
26237 /* Mark types that are used in global variables. */
26238 premark_types_used_by_global_vars ();
26240 /* Set the mark on nodes that are actually used. */
26241 prune_unused_types_walk (comp_unit_die ());
26242 for (node = limbo_die_list; node; node = node->next)
26243 prune_unused_types_walk (node->die);
26244 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26246 prune_unused_types_walk (ctnode->root_die);
26247 prune_unused_types_mark (ctnode->type_die, 1);
26250 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
26251 are unusual in that they are pubnames that are the children of pubtypes.
26252 They should only be marked via their parent DW_TAG_enumeration_type die,
26253 not as roots in themselves. */
26254 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26255 if (pub->die->die_tag != DW_TAG_enumerator)
26256 prune_unused_types_mark (pub->die, 1);
26257 for (i = 0; base_types.iterate (i, &base_type); i++)
26258 prune_unused_types_mark (base_type, 1);
26260 if (debug_str_hash)
26261 debug_str_hash->empty ();
26262 if (skeleton_debug_str_hash)
26263 skeleton_debug_str_hash->empty ();
26264 prune_unused_types_prune (comp_unit_die ());
26265 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26267 node = *pnode;
26268 if (!node->die->die_mark)
26269 *pnode = node->next;
26270 else
26272 prune_unused_types_prune (node->die);
26273 pnode = &node->next;
26276 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26277 prune_unused_types_prune (ctnode->root_die);
26279 /* Leave the marks clear. */
26280 prune_unmark_dies (comp_unit_die ());
26281 for (node = limbo_die_list; node; node = node->next)
26282 prune_unmark_dies (node->die);
26283 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26284 prune_unmark_dies (ctnode->root_die);
26287 /* Set the parameter to true if there are any relative pathnames in
26288 the file table. */
26290 file_table_relative_p (dwarf_file_data **slot, bool *p)
26292 struct dwarf_file_data *d = *slot;
26293 if (!IS_ABSOLUTE_PATH (d->filename))
26295 *p = true;
26296 return 0;
26298 return 1;
26301 /* Helpers to manipulate hash table of comdat type units. */
26303 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26305 static inline hashval_t hash (const comdat_type_node *);
26306 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26309 inline hashval_t
26310 comdat_type_hasher::hash (const comdat_type_node *type_node)
26312 hashval_t h;
26313 memcpy (&h, type_node->signature, sizeof (h));
26314 return h;
26317 inline bool
26318 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26319 const comdat_type_node *type_node_2)
26321 return (! memcmp (type_node_1->signature, type_node_2->signature,
26322 DWARF_TYPE_SIGNATURE_SIZE));
26325 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26326 to the location it would have been added, should we know its
26327 DECL_ASSEMBLER_NAME when we added other attributes. This will
26328 probably improve compactness of debug info, removing equivalent
26329 abbrevs, and hide any differences caused by deferring the
26330 computation of the assembler name, triggered by e.g. PCH. */
26332 static inline void
26333 move_linkage_attr (dw_die_ref die)
26335 unsigned ix = vec_safe_length (die->die_attr);
26336 dw_attr_node linkage = (*die->die_attr)[ix - 1];
26338 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26339 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26341 while (--ix > 0)
26343 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26345 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26346 break;
26349 if (ix != vec_safe_length (die->die_attr) - 1)
26351 die->die_attr->pop ();
26352 die->die_attr->quick_insert (ix, linkage);
26356 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26357 referenced from typed stack ops and count how often they are used. */
26359 static void
26360 mark_base_types (dw_loc_descr_ref loc)
26362 dw_die_ref base_type = NULL;
26364 for (; loc; loc = loc->dw_loc_next)
26366 switch (loc->dw_loc_opc)
26368 case DW_OP_GNU_regval_type:
26369 case DW_OP_GNU_deref_type:
26370 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26371 break;
26372 case DW_OP_GNU_convert:
26373 case DW_OP_GNU_reinterpret:
26374 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26375 continue;
26376 /* FALLTHRU */
26377 case DW_OP_GNU_const_type:
26378 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26379 break;
26380 case DW_OP_GNU_entry_value:
26381 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26382 continue;
26383 default:
26384 continue;
26386 gcc_assert (base_type->die_parent == comp_unit_die ());
26387 if (base_type->die_mark)
26388 base_type->die_mark++;
26389 else
26391 base_types.safe_push (base_type);
26392 base_type->die_mark = 1;
26397 /* Comparison function for sorting marked base types. */
26399 static int
26400 base_type_cmp (const void *x, const void *y)
26402 dw_die_ref dx = *(const dw_die_ref *) x;
26403 dw_die_ref dy = *(const dw_die_ref *) y;
26404 unsigned int byte_size1, byte_size2;
26405 unsigned int encoding1, encoding2;
26406 if (dx->die_mark > dy->die_mark)
26407 return -1;
26408 if (dx->die_mark < dy->die_mark)
26409 return 1;
26410 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26411 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26412 if (byte_size1 < byte_size2)
26413 return 1;
26414 if (byte_size1 > byte_size2)
26415 return -1;
26416 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26417 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26418 if (encoding1 < encoding2)
26419 return 1;
26420 if (encoding1 > encoding2)
26421 return -1;
26422 return 0;
26425 /* Move base types marked by mark_base_types as early as possible
26426 in the CU, sorted by decreasing usage count both to make the
26427 uleb128 references as small as possible and to make sure they
26428 will have die_offset already computed by calc_die_sizes when
26429 sizes of typed stack loc ops is computed. */
26431 static void
26432 move_marked_base_types (void)
26434 unsigned int i;
26435 dw_die_ref base_type, die, c;
26437 if (base_types.is_empty ())
26438 return;
26440 /* Sort by decreasing usage count, they will be added again in that
26441 order later on. */
26442 base_types.qsort (base_type_cmp);
26443 die = comp_unit_die ();
26444 c = die->die_child;
26447 dw_die_ref prev = c;
26448 c = c->die_sib;
26449 while (c->die_mark)
26451 remove_child_with_prev (c, prev);
26452 /* As base types got marked, there must be at least
26453 one node other than DW_TAG_base_type. */
26454 gcc_assert (c != c->die_sib);
26455 c = c->die_sib;
26458 while (c != die->die_child);
26459 gcc_assert (die->die_child);
26460 c = die->die_child;
26461 for (i = 0; base_types.iterate (i, &base_type); i++)
26463 base_type->die_mark = 0;
26464 base_type->die_sib = c->die_sib;
26465 c->die_sib = base_type;
26466 c = base_type;
26470 /* Helper function for resolve_addr, attempt to resolve
26471 one CONST_STRING, return true if successful. Similarly verify that
26472 SYMBOL_REFs refer to variables emitted in the current CU. */
26474 static bool
26475 resolve_one_addr (rtx *addr)
26477 rtx rtl = *addr;
26479 if (GET_CODE (rtl) == CONST_STRING)
26481 size_t len = strlen (XSTR (rtl, 0)) + 1;
26482 tree t = build_string (len, XSTR (rtl, 0));
26483 tree tlen = size_int (len - 1);
26484 TREE_TYPE (t)
26485 = build_array_type (char_type_node, build_index_type (tlen));
26486 rtl = lookup_constant_def (t);
26487 if (!rtl || !MEM_P (rtl))
26488 return false;
26489 rtl = XEXP (rtl, 0);
26490 if (GET_CODE (rtl) == SYMBOL_REF
26491 && SYMBOL_REF_DECL (rtl)
26492 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26493 return false;
26494 vec_safe_push (used_rtx_array, rtl);
26495 *addr = rtl;
26496 return true;
26499 if (GET_CODE (rtl) == SYMBOL_REF
26500 && SYMBOL_REF_DECL (rtl))
26502 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26504 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26505 return false;
26507 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26508 return false;
26511 if (GET_CODE (rtl) == CONST)
26513 subrtx_ptr_iterator::array_type array;
26514 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26515 if (!resolve_one_addr (*iter))
26516 return false;
26519 return true;
26522 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26523 if possible, and create DW_TAG_dwarf_procedure that can be referenced
26524 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
26526 static rtx
26527 string_cst_pool_decl (tree t)
26529 rtx rtl = output_constant_def (t, 1);
26530 unsigned char *array;
26531 dw_loc_descr_ref l;
26532 tree decl;
26533 size_t len;
26534 dw_die_ref ref;
26536 if (!rtl || !MEM_P (rtl))
26537 return NULL_RTX;
26538 rtl = XEXP (rtl, 0);
26539 if (GET_CODE (rtl) != SYMBOL_REF
26540 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26541 return NULL_RTX;
26543 decl = SYMBOL_REF_DECL (rtl);
26544 if (!lookup_decl_die (decl))
26546 len = TREE_STRING_LENGTH (t);
26547 vec_safe_push (used_rtx_array, rtl);
26548 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26549 array = ggc_vec_alloc<unsigned char> (len);
26550 memcpy (array, TREE_STRING_POINTER (t), len);
26551 l = new_loc_descr (DW_OP_implicit_value, len, 0);
26552 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26553 l->dw_loc_oprnd2.v.val_vec.length = len;
26554 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26555 l->dw_loc_oprnd2.v.val_vec.array = array;
26556 add_AT_loc (ref, DW_AT_location, l);
26557 equate_decl_number_to_die (decl, ref);
26559 return rtl;
26562 /* Helper function of resolve_addr_in_expr. LOC is
26563 a DW_OP_addr followed by DW_OP_stack_value, either at the start
26564 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26565 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
26566 with DW_OP_GNU_implicit_pointer if possible
26567 and return true, if unsuccessful, return false. */
26569 static bool
26570 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26572 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26573 HOST_WIDE_INT offset = 0;
26574 dw_die_ref ref = NULL;
26575 tree decl;
26577 if (GET_CODE (rtl) == CONST
26578 && GET_CODE (XEXP (rtl, 0)) == PLUS
26579 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26581 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26582 rtl = XEXP (XEXP (rtl, 0), 0);
26584 if (GET_CODE (rtl) == CONST_STRING)
26586 size_t len = strlen (XSTR (rtl, 0)) + 1;
26587 tree t = build_string (len, XSTR (rtl, 0));
26588 tree tlen = size_int (len - 1);
26590 TREE_TYPE (t)
26591 = build_array_type (char_type_node, build_index_type (tlen));
26592 rtl = string_cst_pool_decl (t);
26593 if (!rtl)
26594 return false;
26596 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26598 decl = SYMBOL_REF_DECL (rtl);
26599 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
26601 ref = lookup_decl_die (decl);
26602 if (ref && (get_AT (ref, DW_AT_location)
26603 || get_AT (ref, DW_AT_const_value)))
26605 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26606 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26607 loc->dw_loc_oprnd1.val_entry = NULL;
26608 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26609 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26610 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26611 loc->dw_loc_oprnd2.v.val_int = offset;
26612 return true;
26616 return false;
26619 /* Helper function for resolve_addr, handle one location
26620 expression, return false if at least one CONST_STRING or SYMBOL_REF in
26621 the location list couldn't be resolved. */
26623 static bool
26624 resolve_addr_in_expr (dw_loc_descr_ref loc)
26626 dw_loc_descr_ref keep = NULL;
26627 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26628 switch (loc->dw_loc_opc)
26630 case DW_OP_addr:
26631 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26633 if ((prev == NULL
26634 || prev->dw_loc_opc == DW_OP_piece
26635 || prev->dw_loc_opc == DW_OP_bit_piece)
26636 && loc->dw_loc_next
26637 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26638 && !dwarf_strict
26639 && optimize_one_addr_into_implicit_ptr (loc))
26640 break;
26641 return false;
26643 break;
26644 case DW_OP_GNU_addr_index:
26645 case DW_OP_GNU_const_index:
26646 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26647 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26649 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26650 if (!resolve_one_addr (&rtl))
26651 return false;
26652 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26653 loc->dw_loc_oprnd1.val_entry =
26654 add_addr_table_entry (rtl, ate_kind_rtx);
26656 break;
26657 case DW_OP_const4u:
26658 case DW_OP_const8u:
26659 if (loc->dtprel
26660 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26661 return false;
26662 break;
26663 case DW_OP_plus_uconst:
26664 if (size_of_loc_descr (loc)
26665 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26667 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26669 dw_loc_descr_ref repl
26670 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26671 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26672 add_loc_descr (&repl, loc->dw_loc_next);
26673 *loc = *repl;
26675 break;
26676 case DW_OP_implicit_value:
26677 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26678 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26679 return false;
26680 break;
26681 case DW_OP_GNU_implicit_pointer:
26682 case DW_OP_GNU_parameter_ref:
26683 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26685 dw_die_ref ref
26686 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26687 if (ref == NULL)
26688 return false;
26689 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26690 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26691 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26693 break;
26694 case DW_OP_GNU_const_type:
26695 case DW_OP_GNU_regval_type:
26696 case DW_OP_GNU_deref_type:
26697 case DW_OP_GNU_convert:
26698 case DW_OP_GNU_reinterpret:
26699 while (loc->dw_loc_next
26700 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26702 dw_die_ref base1, base2;
26703 unsigned enc1, enc2, size1, size2;
26704 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26705 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26706 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26707 else if (loc->dw_loc_oprnd1.val_class
26708 == dw_val_class_unsigned_const)
26709 break;
26710 else
26711 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26712 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26713 == dw_val_class_unsigned_const)
26714 break;
26715 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26716 gcc_assert (base1->die_tag == DW_TAG_base_type
26717 && base2->die_tag == DW_TAG_base_type);
26718 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26719 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26720 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26721 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26722 if (size1 == size2
26723 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26724 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26725 && loc != keep)
26726 || enc1 == enc2))
26728 /* Optimize away next DW_OP_GNU_convert after
26729 adjusting LOC's base type die reference. */
26730 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26731 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26732 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26733 else
26734 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26735 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26736 continue;
26738 /* Don't change integer DW_OP_GNU_convert after e.g. floating
26739 point typed stack entry. */
26740 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26741 keep = loc->dw_loc_next;
26742 break;
26744 break;
26745 default:
26746 break;
26748 return true;
26751 /* Helper function of resolve_addr. DIE had DW_AT_location of
26752 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26753 and DW_OP_addr couldn't be resolved. resolve_addr has already
26754 removed the DW_AT_location attribute. This function attempts to
26755 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26756 to it or DW_AT_const_value attribute, if possible. */
26758 static void
26759 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26761 if (TREE_CODE (decl) != VAR_DECL
26762 || lookup_decl_die (decl) != die
26763 || DECL_EXTERNAL (decl)
26764 || !TREE_STATIC (decl)
26765 || DECL_INITIAL (decl) == NULL_TREE
26766 || DECL_P (DECL_INITIAL (decl))
26767 || get_AT (die, DW_AT_const_value))
26768 return;
26770 tree init = DECL_INITIAL (decl);
26771 HOST_WIDE_INT offset = 0;
26772 /* For variables that have been optimized away and thus
26773 don't have a memory location, see if we can emit
26774 DW_AT_const_value instead. */
26775 if (tree_add_const_value_attribute (die, init))
26776 return;
26777 if (dwarf_strict)
26778 return;
26779 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26780 and ADDR_EXPR refers to a decl that has DW_AT_location or
26781 DW_AT_const_value (but isn't addressable, otherwise
26782 resolving the original DW_OP_addr wouldn't fail), see if
26783 we can add DW_OP_GNU_implicit_pointer. */
26784 STRIP_NOPS (init);
26785 if (TREE_CODE (init) == POINTER_PLUS_EXPR
26786 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26788 offset = tree_to_shwi (TREE_OPERAND (init, 1));
26789 init = TREE_OPERAND (init, 0);
26790 STRIP_NOPS (init);
26792 if (TREE_CODE (init) != ADDR_EXPR)
26793 return;
26794 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26795 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26796 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26797 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26798 && TREE_OPERAND (init, 0) != decl))
26800 dw_die_ref ref;
26801 dw_loc_descr_ref l;
26803 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26805 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26806 if (!rtl)
26807 return;
26808 decl = SYMBOL_REF_DECL (rtl);
26810 else
26811 decl = TREE_OPERAND (init, 0);
26812 ref = lookup_decl_die (decl);
26813 if (ref == NULL
26814 || (!get_AT (ref, DW_AT_location)
26815 && !get_AT (ref, DW_AT_const_value)))
26816 return;
26817 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26818 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26819 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26820 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26821 add_AT_loc (die, DW_AT_location, l);
26825 /* Return NULL if l is a DWARF expression, or first op that is not
26826 valid DWARF expression. */
26828 static dw_loc_descr_ref
26829 non_dwarf_expression (dw_loc_descr_ref l)
26831 while (l)
26833 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26834 return l;
26835 switch (l->dw_loc_opc)
26837 case DW_OP_regx:
26838 case DW_OP_implicit_value:
26839 case DW_OP_stack_value:
26840 case DW_OP_GNU_implicit_pointer:
26841 case DW_OP_GNU_parameter_ref:
26842 case DW_OP_piece:
26843 case DW_OP_bit_piece:
26844 return l;
26845 default:
26846 break;
26848 l = l->dw_loc_next;
26850 return NULL;
26853 /* Return adjusted copy of EXPR:
26854 If it is empty DWARF expression, return it.
26855 If it is valid non-empty DWARF expression,
26856 return copy of EXPR with copy of DEREF appended to it.
26857 If it is DWARF expression followed by DW_OP_reg{N,x}, return
26858 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
26859 and no DEREF.
26860 If it is DWARF expression followed by DW_OP_stack_value, return
26861 copy of the DWARF expression without anything appended.
26862 Otherwise, return NULL. */
26864 static dw_loc_descr_ref
26865 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
26868 if (expr == NULL)
26869 return NULL;
26871 dw_loc_descr_ref l = non_dwarf_expression (expr);
26872 if (l && l->dw_loc_next)
26873 return NULL;
26875 if (l)
26877 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26878 deref = new_loc_descr ((enum dwarf_location_atom)
26879 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
26880 0, 0);
26881 else
26882 switch (l->dw_loc_opc)
26884 case DW_OP_regx:
26885 deref = new_loc_descr (DW_OP_bregx,
26886 l->dw_loc_oprnd1.v.val_unsigned, 0);
26887 break;
26888 case DW_OP_stack_value:
26889 deref = NULL;
26890 break;
26891 default:
26892 return NULL;
26895 else
26896 deref = new_loc_descr (deref->dw_loc_opc,
26897 deref->dw_loc_oprnd1.v.val_int, 0);
26899 dw_loc_descr_ref ret = NULL, *p = &ret;
26900 while (expr != l)
26902 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
26903 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
26904 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
26905 p = &(*p)->dw_loc_next;
26906 expr = expr->dw_loc_next;
26908 *p = deref;
26909 return ret;
26912 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
26913 or argument, adjust it if needed and return:
26914 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
26915 attribute if present should be removed
26916 0 keep the attribute as is if the referenced var or argument has
26917 only DWARF expression that covers all ranges
26918 1 if the attribute has been successfully adjusted. */
26920 static int
26921 optimize_string_length (dw_attr_node *a)
26923 dw_loc_descr_ref l = AT_loc (a), lv;
26924 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
26925 dw_attr_node *av = get_AT (die, DW_AT_location);
26926 dw_loc_list_ref d;
26927 bool non_dwarf_expr = false;
26929 if (av == NULL)
26930 return -1;
26931 switch (AT_class (av))
26933 case dw_val_class_loc_list:
26934 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26935 if (d->expr && non_dwarf_expression (d->expr))
26936 non_dwarf_expr = true;
26937 break;
26938 case dw_val_class_loc:
26939 lv = AT_loc (av);
26940 if (lv == NULL)
26941 return -1;
26942 if (non_dwarf_expression (lv))
26943 non_dwarf_expr = true;
26944 break;
26945 default:
26946 return -1;
26949 /* If it is safe to keep DW_OP_call4 in, keep it. */
26950 if (!non_dwarf_expr
26951 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
26952 return 0;
26954 /* If not dereferencing the DW_OP_call4 afterwards, we can just
26955 copy over the DW_AT_location attribute from die to a. */
26956 if (l->dw_loc_next == NULL)
26958 a->dw_attr_val = av->dw_attr_val;
26959 return 1;
26962 dw_loc_list_ref list, *p;
26963 switch (AT_class (av))
26965 case dw_val_class_loc_list:
26966 p = &list;
26967 list = NULL;
26968 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26970 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
26971 if (lv)
26973 *p = new_loc_list (lv, d->begin, d->end, d->section);
26974 p = &(*p)->dw_loc_next;
26977 if (list == NULL)
26978 return -1;
26979 a->dw_attr_val.val_class = dw_val_class_loc_list;
26980 gen_llsym (list);
26981 *AT_loc_list_ptr (a) = list;
26982 return 1;
26983 case dw_val_class_loc:
26984 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
26985 if (lv == NULL)
26986 return -1;
26987 a->dw_attr_val.v.val_loc = lv;
26988 return 1;
26989 default:
26990 gcc_unreachable ();
26994 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
26995 an address in .rodata section if the string literal is emitted there,
26996 or remove the containing location list or replace DW_AT_const_value
26997 with DW_AT_location and empty location expression, if it isn't found
26998 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
26999 to something that has been emitted in the current CU. */
27001 static void
27002 resolve_addr (dw_die_ref die)
27004 dw_die_ref c;
27005 dw_attr_node *a;
27006 dw_loc_list_ref *curr, *start, loc;
27007 unsigned ix;
27008 bool remove_AT_byte_size = false;
27010 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27011 switch (AT_class (a))
27013 case dw_val_class_loc_list:
27014 start = curr = AT_loc_list_ptr (a);
27015 loc = *curr;
27016 gcc_assert (loc);
27017 /* The same list can be referenced more than once. See if we have
27018 already recorded the result from a previous pass. */
27019 if (loc->replaced)
27020 *curr = loc->dw_loc_next;
27021 else if (!loc->resolved_addr)
27023 /* As things stand, we do not expect or allow one die to
27024 reference a suffix of another die's location list chain.
27025 References must be identical or completely separate.
27026 There is therefore no need to cache the result of this
27027 pass on any list other than the first; doing so
27028 would lead to unnecessary writes. */
27029 while (*curr)
27031 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
27032 if (!resolve_addr_in_expr ((*curr)->expr))
27034 dw_loc_list_ref next = (*curr)->dw_loc_next;
27035 dw_loc_descr_ref l = (*curr)->expr;
27037 if (next && (*curr)->ll_symbol)
27039 gcc_assert (!next->ll_symbol);
27040 next->ll_symbol = (*curr)->ll_symbol;
27042 if (dwarf_split_debug_info)
27043 remove_loc_list_addr_table_entries (l);
27044 *curr = next;
27046 else
27048 mark_base_types ((*curr)->expr);
27049 curr = &(*curr)->dw_loc_next;
27052 if (loc == *start)
27053 loc->resolved_addr = 1;
27054 else
27056 loc->replaced = 1;
27057 loc->dw_loc_next = *start;
27060 if (!*start)
27062 remove_AT (die, a->dw_attr);
27063 ix--;
27065 break;
27066 case dw_val_class_loc:
27068 dw_loc_descr_ref l = AT_loc (a);
27069 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
27070 DW_AT_string_length is only a rough approximation; unfortunately
27071 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
27072 needs a DWARF expression, while DW_AT_location of the referenced
27073 variable or argument might be any location description. */
27074 if (a->dw_attr == DW_AT_string_length
27075 && l
27076 && l->dw_loc_opc == DW_OP_call4
27077 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
27078 && (l->dw_loc_next == NULL
27079 || (l->dw_loc_next->dw_loc_next == NULL
27080 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
27081 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
27083 switch (optimize_string_length (a))
27085 case -1:
27086 remove_AT (die, a->dw_attr);
27087 ix--;
27088 /* If we drop DW_AT_string_length, we need to drop also
27089 DW_AT_{string_length_,}byte_size. */
27090 remove_AT_byte_size = true;
27091 continue;
27092 default:
27093 break;
27094 case 1:
27095 /* Even if we keep the optimized DW_AT_string_length,
27096 it might have changed AT_class, so process it again. */
27097 ix--;
27098 continue;
27101 /* For -gdwarf-2 don't attempt to optimize
27102 DW_AT_data_member_location containing
27103 DW_OP_plus_uconst - older consumers might
27104 rely on it being that op instead of a more complex,
27105 but shorter, location description. */
27106 if ((dwarf_version > 2
27107 || a->dw_attr != DW_AT_data_member_location
27108 || l == NULL
27109 || l->dw_loc_opc != DW_OP_plus_uconst
27110 || l->dw_loc_next != NULL)
27111 && !resolve_addr_in_expr (l))
27113 if (dwarf_split_debug_info)
27114 remove_loc_list_addr_table_entries (l);
27115 if (l != NULL
27116 && l->dw_loc_next == NULL
27117 && l->dw_loc_opc == DW_OP_addr
27118 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
27119 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
27120 && a->dw_attr == DW_AT_location)
27122 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
27123 remove_AT (die, a->dw_attr);
27124 ix--;
27125 optimize_location_into_implicit_ptr (die, decl);
27126 break;
27128 remove_AT (die, a->dw_attr);
27129 ix--;
27131 else
27132 mark_base_types (l);
27134 break;
27135 case dw_val_class_addr:
27136 if (a->dw_attr == DW_AT_const_value
27137 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
27139 if (AT_index (a) != NOT_INDEXED)
27140 remove_addr_table_entry (a->dw_attr_val.val_entry);
27141 remove_AT (die, a->dw_attr);
27142 ix--;
27144 if (die->die_tag == DW_TAG_GNU_call_site
27145 && a->dw_attr == DW_AT_abstract_origin)
27147 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
27148 dw_die_ref tdie = lookup_decl_die (tdecl);
27149 dw_die_ref cdie;
27150 if (tdie == NULL
27151 && DECL_EXTERNAL (tdecl)
27152 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
27153 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
27155 /* Creating a full DIE for tdecl is overly expensive and
27156 at this point even wrong when in the LTO phase
27157 as it can end up generating new type DIEs we didn't
27158 output and thus optimize_external_refs will crash. */
27159 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
27160 add_AT_flag (tdie, DW_AT_external, 1);
27161 add_AT_flag (tdie, DW_AT_declaration, 1);
27162 add_linkage_attr (tdie, tdecl);
27163 add_name_and_src_coords_attributes (tdie, tdecl);
27164 equate_decl_number_to_die (tdecl, tdie);
27166 if (tdie)
27168 a->dw_attr_val.val_class = dw_val_class_die_ref;
27169 a->dw_attr_val.v.val_die_ref.die = tdie;
27170 a->dw_attr_val.v.val_die_ref.external = 0;
27172 else
27174 if (AT_index (a) != NOT_INDEXED)
27175 remove_addr_table_entry (a->dw_attr_val.val_entry);
27176 remove_AT (die, a->dw_attr);
27177 ix--;
27180 break;
27181 default:
27182 break;
27185 if (remove_AT_byte_size)
27186 remove_AT (die, dwarf_version >= 5
27187 ? DW_AT_string_length_byte_size
27188 : DW_AT_byte_size);
27190 FOR_EACH_CHILD (die, c, resolve_addr (c));
27193 /* Helper routines for optimize_location_lists.
27194 This pass tries to share identical local lists in .debug_loc
27195 section. */
27197 /* Iteratively hash operands of LOC opcode into HSTATE. */
27199 static void
27200 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
27202 dw_val_ref val1 = &loc->dw_loc_oprnd1;
27203 dw_val_ref val2 = &loc->dw_loc_oprnd2;
27205 switch (loc->dw_loc_opc)
27207 case DW_OP_const4u:
27208 case DW_OP_const8u:
27209 if (loc->dtprel)
27210 goto hash_addr;
27211 /* FALLTHRU */
27212 case DW_OP_const1u:
27213 case DW_OP_const1s:
27214 case DW_OP_const2u:
27215 case DW_OP_const2s:
27216 case DW_OP_const4s:
27217 case DW_OP_const8s:
27218 case DW_OP_constu:
27219 case DW_OP_consts:
27220 case DW_OP_pick:
27221 case DW_OP_plus_uconst:
27222 case DW_OP_breg0:
27223 case DW_OP_breg1:
27224 case DW_OP_breg2:
27225 case DW_OP_breg3:
27226 case DW_OP_breg4:
27227 case DW_OP_breg5:
27228 case DW_OP_breg6:
27229 case DW_OP_breg7:
27230 case DW_OP_breg8:
27231 case DW_OP_breg9:
27232 case DW_OP_breg10:
27233 case DW_OP_breg11:
27234 case DW_OP_breg12:
27235 case DW_OP_breg13:
27236 case DW_OP_breg14:
27237 case DW_OP_breg15:
27238 case DW_OP_breg16:
27239 case DW_OP_breg17:
27240 case DW_OP_breg18:
27241 case DW_OP_breg19:
27242 case DW_OP_breg20:
27243 case DW_OP_breg21:
27244 case DW_OP_breg22:
27245 case DW_OP_breg23:
27246 case DW_OP_breg24:
27247 case DW_OP_breg25:
27248 case DW_OP_breg26:
27249 case DW_OP_breg27:
27250 case DW_OP_breg28:
27251 case DW_OP_breg29:
27252 case DW_OP_breg30:
27253 case DW_OP_breg31:
27254 case DW_OP_regx:
27255 case DW_OP_fbreg:
27256 case DW_OP_piece:
27257 case DW_OP_deref_size:
27258 case DW_OP_xderef_size:
27259 hstate.add_object (val1->v.val_int);
27260 break;
27261 case DW_OP_skip:
27262 case DW_OP_bra:
27264 int offset;
27266 gcc_assert (val1->val_class == dw_val_class_loc);
27267 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
27268 hstate.add_object (offset);
27270 break;
27271 case DW_OP_implicit_value:
27272 hstate.add_object (val1->v.val_unsigned);
27273 switch (val2->val_class)
27275 case dw_val_class_const:
27276 hstate.add_object (val2->v.val_int);
27277 break;
27278 case dw_val_class_vec:
27280 unsigned int elt_size = val2->v.val_vec.elt_size;
27281 unsigned int len = val2->v.val_vec.length;
27283 hstate.add_int (elt_size);
27284 hstate.add_int (len);
27285 hstate.add (val2->v.val_vec.array, len * elt_size);
27287 break;
27288 case dw_val_class_const_double:
27289 hstate.add_object (val2->v.val_double.low);
27290 hstate.add_object (val2->v.val_double.high);
27291 break;
27292 case dw_val_class_wide_int:
27293 hstate.add (val2->v.val_wide->get_val (),
27294 get_full_len (*val2->v.val_wide)
27295 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27296 break;
27297 case dw_val_class_addr:
27298 inchash::add_rtx (val2->v.val_addr, hstate);
27299 break;
27300 default:
27301 gcc_unreachable ();
27303 break;
27304 case DW_OP_bregx:
27305 case DW_OP_bit_piece:
27306 hstate.add_object (val1->v.val_int);
27307 hstate.add_object (val2->v.val_int);
27308 break;
27309 case DW_OP_addr:
27310 hash_addr:
27311 if (loc->dtprel)
27313 unsigned char dtprel = 0xd1;
27314 hstate.add_object (dtprel);
27316 inchash::add_rtx (val1->v.val_addr, hstate);
27317 break;
27318 case DW_OP_GNU_addr_index:
27319 case DW_OP_GNU_const_index:
27321 if (loc->dtprel)
27323 unsigned char dtprel = 0xd1;
27324 hstate.add_object (dtprel);
27326 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
27328 break;
27329 case DW_OP_GNU_implicit_pointer:
27330 hstate.add_int (val2->v.val_int);
27331 break;
27332 case DW_OP_GNU_entry_value:
27333 hstate.add_object (val1->v.val_loc);
27334 break;
27335 case DW_OP_GNU_regval_type:
27336 case DW_OP_GNU_deref_type:
27338 unsigned int byte_size
27339 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
27340 unsigned int encoding
27341 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
27342 hstate.add_object (val1->v.val_int);
27343 hstate.add_object (byte_size);
27344 hstate.add_object (encoding);
27346 break;
27347 case DW_OP_GNU_convert:
27348 case DW_OP_GNU_reinterpret:
27349 if (val1->val_class == dw_val_class_unsigned_const)
27351 hstate.add_object (val1->v.val_unsigned);
27352 break;
27354 /* FALLTHRU */
27355 case DW_OP_GNU_const_type:
27357 unsigned int byte_size
27358 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
27359 unsigned int encoding
27360 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
27361 hstate.add_object (byte_size);
27362 hstate.add_object (encoding);
27363 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
27364 break;
27365 hstate.add_object (val2->val_class);
27366 switch (val2->val_class)
27368 case dw_val_class_const:
27369 hstate.add_object (val2->v.val_int);
27370 break;
27371 case dw_val_class_vec:
27373 unsigned int elt_size = val2->v.val_vec.elt_size;
27374 unsigned int len = val2->v.val_vec.length;
27376 hstate.add_object (elt_size);
27377 hstate.add_object (len);
27378 hstate.add (val2->v.val_vec.array, len * elt_size);
27380 break;
27381 case dw_val_class_const_double:
27382 hstate.add_object (val2->v.val_double.low);
27383 hstate.add_object (val2->v.val_double.high);
27384 break;
27385 case dw_val_class_wide_int:
27386 hstate.add (val2->v.val_wide->get_val (),
27387 get_full_len (*val2->v.val_wide)
27388 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27389 break;
27390 default:
27391 gcc_unreachable ();
27394 break;
27396 default:
27397 /* Other codes have no operands. */
27398 break;
27402 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
27404 static inline void
27405 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
27407 dw_loc_descr_ref l;
27408 bool sizes_computed = false;
27409 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
27410 size_of_locs (loc);
27412 for (l = loc; l != NULL; l = l->dw_loc_next)
27414 enum dwarf_location_atom opc = l->dw_loc_opc;
27415 hstate.add_object (opc);
27416 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
27418 size_of_locs (loc);
27419 sizes_computed = true;
27421 hash_loc_operands (l, hstate);
27425 /* Compute hash of the whole location list LIST_HEAD. */
27427 static inline void
27428 hash_loc_list (dw_loc_list_ref list_head)
27430 dw_loc_list_ref curr = list_head;
27431 inchash::hash hstate;
27433 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
27435 hstate.add (curr->begin, strlen (curr->begin) + 1);
27436 hstate.add (curr->end, strlen (curr->end) + 1);
27437 if (curr->section)
27438 hstate.add (curr->section, strlen (curr->section) + 1);
27439 hash_locs (curr->expr, hstate);
27441 list_head->hash = hstate.end ();
27444 /* Return true if X and Y opcodes have the same operands. */
27446 static inline bool
27447 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
27449 dw_val_ref valx1 = &x->dw_loc_oprnd1;
27450 dw_val_ref valx2 = &x->dw_loc_oprnd2;
27451 dw_val_ref valy1 = &y->dw_loc_oprnd1;
27452 dw_val_ref valy2 = &y->dw_loc_oprnd2;
27454 switch (x->dw_loc_opc)
27456 case DW_OP_const4u:
27457 case DW_OP_const8u:
27458 if (x->dtprel)
27459 goto hash_addr;
27460 /* FALLTHRU */
27461 case DW_OP_const1u:
27462 case DW_OP_const1s:
27463 case DW_OP_const2u:
27464 case DW_OP_const2s:
27465 case DW_OP_const4s:
27466 case DW_OP_const8s:
27467 case DW_OP_constu:
27468 case DW_OP_consts:
27469 case DW_OP_pick:
27470 case DW_OP_plus_uconst:
27471 case DW_OP_breg0:
27472 case DW_OP_breg1:
27473 case DW_OP_breg2:
27474 case DW_OP_breg3:
27475 case DW_OP_breg4:
27476 case DW_OP_breg5:
27477 case DW_OP_breg6:
27478 case DW_OP_breg7:
27479 case DW_OP_breg8:
27480 case DW_OP_breg9:
27481 case DW_OP_breg10:
27482 case DW_OP_breg11:
27483 case DW_OP_breg12:
27484 case DW_OP_breg13:
27485 case DW_OP_breg14:
27486 case DW_OP_breg15:
27487 case DW_OP_breg16:
27488 case DW_OP_breg17:
27489 case DW_OP_breg18:
27490 case DW_OP_breg19:
27491 case DW_OP_breg20:
27492 case DW_OP_breg21:
27493 case DW_OP_breg22:
27494 case DW_OP_breg23:
27495 case DW_OP_breg24:
27496 case DW_OP_breg25:
27497 case DW_OP_breg26:
27498 case DW_OP_breg27:
27499 case DW_OP_breg28:
27500 case DW_OP_breg29:
27501 case DW_OP_breg30:
27502 case DW_OP_breg31:
27503 case DW_OP_regx:
27504 case DW_OP_fbreg:
27505 case DW_OP_piece:
27506 case DW_OP_deref_size:
27507 case DW_OP_xderef_size:
27508 return valx1->v.val_int == valy1->v.val_int;
27509 case DW_OP_skip:
27510 case DW_OP_bra:
27511 /* If splitting debug info, the use of DW_OP_GNU_addr_index
27512 can cause irrelevant differences in dw_loc_addr. */
27513 gcc_assert (valx1->val_class == dw_val_class_loc
27514 && valy1->val_class == dw_val_class_loc
27515 && (dwarf_split_debug_info
27516 || x->dw_loc_addr == y->dw_loc_addr));
27517 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27518 case DW_OP_implicit_value:
27519 if (valx1->v.val_unsigned != valy1->v.val_unsigned
27520 || valx2->val_class != valy2->val_class)
27521 return false;
27522 switch (valx2->val_class)
27524 case dw_val_class_const:
27525 return valx2->v.val_int == valy2->v.val_int;
27526 case dw_val_class_vec:
27527 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27528 && valx2->v.val_vec.length == valy2->v.val_vec.length
27529 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27530 valx2->v.val_vec.elt_size
27531 * valx2->v.val_vec.length) == 0;
27532 case dw_val_class_const_double:
27533 return valx2->v.val_double.low == valy2->v.val_double.low
27534 && valx2->v.val_double.high == valy2->v.val_double.high;
27535 case dw_val_class_wide_int:
27536 return *valx2->v.val_wide == *valy2->v.val_wide;
27537 case dw_val_class_addr:
27538 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27539 default:
27540 gcc_unreachable ();
27542 case DW_OP_bregx:
27543 case DW_OP_bit_piece:
27544 return valx1->v.val_int == valy1->v.val_int
27545 && valx2->v.val_int == valy2->v.val_int;
27546 case DW_OP_addr:
27547 hash_addr:
27548 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27549 case DW_OP_GNU_addr_index:
27550 case DW_OP_GNU_const_index:
27552 rtx ax1 = valx1->val_entry->addr.rtl;
27553 rtx ay1 = valy1->val_entry->addr.rtl;
27554 return rtx_equal_p (ax1, ay1);
27556 case DW_OP_GNU_implicit_pointer:
27557 return valx1->val_class == dw_val_class_die_ref
27558 && valx1->val_class == valy1->val_class
27559 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27560 && valx2->v.val_int == valy2->v.val_int;
27561 case DW_OP_GNU_entry_value:
27562 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27563 case DW_OP_GNU_const_type:
27564 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27565 || valx2->val_class != valy2->val_class)
27566 return false;
27567 switch (valx2->val_class)
27569 case dw_val_class_const:
27570 return valx2->v.val_int == valy2->v.val_int;
27571 case dw_val_class_vec:
27572 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27573 && valx2->v.val_vec.length == valy2->v.val_vec.length
27574 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27575 valx2->v.val_vec.elt_size
27576 * valx2->v.val_vec.length) == 0;
27577 case dw_val_class_const_double:
27578 return valx2->v.val_double.low == valy2->v.val_double.low
27579 && valx2->v.val_double.high == valy2->v.val_double.high;
27580 case dw_val_class_wide_int:
27581 return *valx2->v.val_wide == *valy2->v.val_wide;
27582 default:
27583 gcc_unreachable ();
27585 case DW_OP_GNU_regval_type:
27586 case DW_OP_GNU_deref_type:
27587 return valx1->v.val_int == valy1->v.val_int
27588 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27589 case DW_OP_GNU_convert:
27590 case DW_OP_GNU_reinterpret:
27591 if (valx1->val_class != valy1->val_class)
27592 return false;
27593 if (valx1->val_class == dw_val_class_unsigned_const)
27594 return valx1->v.val_unsigned == valy1->v.val_unsigned;
27595 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27596 case DW_OP_GNU_parameter_ref:
27597 return valx1->val_class == dw_val_class_die_ref
27598 && valx1->val_class == valy1->val_class
27599 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27600 default:
27601 /* Other codes have no operands. */
27602 return true;
27606 /* Return true if DWARF location expressions X and Y are the same. */
27608 static inline bool
27609 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27611 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27612 if (x->dw_loc_opc != y->dw_loc_opc
27613 || x->dtprel != y->dtprel
27614 || !compare_loc_operands (x, y))
27615 break;
27616 return x == NULL && y == NULL;
27619 /* Hashtable helpers. */
27621 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27623 static inline hashval_t hash (const dw_loc_list_struct *);
27624 static inline bool equal (const dw_loc_list_struct *,
27625 const dw_loc_list_struct *);
27628 /* Return precomputed hash of location list X. */
27630 inline hashval_t
27631 loc_list_hasher::hash (const dw_loc_list_struct *x)
27633 return x->hash;
27636 /* Return true if location lists A and B are the same. */
27638 inline bool
27639 loc_list_hasher::equal (const dw_loc_list_struct *a,
27640 const dw_loc_list_struct *b)
27642 if (a == b)
27643 return 1;
27644 if (a->hash != b->hash)
27645 return 0;
27646 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27647 if (strcmp (a->begin, b->begin) != 0
27648 || strcmp (a->end, b->end) != 0
27649 || (a->section == NULL) != (b->section == NULL)
27650 || (a->section && strcmp (a->section, b->section) != 0)
27651 || !compare_locs (a->expr, b->expr))
27652 break;
27653 return a == NULL && b == NULL;
27656 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27659 /* Recursively optimize location lists referenced from DIE
27660 children and share them whenever possible. */
27662 static void
27663 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27665 dw_die_ref c;
27666 dw_attr_node *a;
27667 unsigned ix;
27668 dw_loc_list_struct **slot;
27670 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27671 if (AT_class (a) == dw_val_class_loc_list)
27673 dw_loc_list_ref list = AT_loc_list (a);
27674 /* TODO: perform some optimizations here, before hashing
27675 it and storing into the hash table. */
27676 hash_loc_list (list);
27677 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27678 if (*slot == NULL)
27679 *slot = list;
27680 else
27681 a->dw_attr_val.v.val_loc_list = *slot;
27684 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27688 /* Recursively assign each location list a unique index into the debug_addr
27689 section. */
27691 static void
27692 index_location_lists (dw_die_ref die)
27694 dw_die_ref c;
27695 dw_attr_node *a;
27696 unsigned ix;
27698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27699 if (AT_class (a) == dw_val_class_loc_list)
27701 dw_loc_list_ref list = AT_loc_list (a);
27702 dw_loc_list_ref curr;
27703 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27705 /* Don't index an entry that has already been indexed
27706 or won't be output. */
27707 if (curr->begin_entry != NULL
27708 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27709 continue;
27711 curr->begin_entry
27712 = add_addr_table_entry (xstrdup (curr->begin),
27713 ate_kind_label);
27717 FOR_EACH_CHILD (die, c, index_location_lists (c));
27720 /* Optimize location lists referenced from DIE
27721 children and share them whenever possible. */
27723 static void
27724 optimize_location_lists (dw_die_ref die)
27726 loc_list_hash_type htab (500);
27727 optimize_location_lists_1 (die, &htab);
27730 /* Traverse the limbo die list, and add parent/child links. The only
27731 dies without parents that should be here are concrete instances of
27732 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
27733 For concrete instances, we can get the parent die from the abstract
27734 instance. */
27736 static void
27737 flush_limbo_die_list (void)
27739 limbo_die_node *node;
27741 /* get_context_die calls force_decl_die, which can put new DIEs on the
27742 limbo list in LTO mode when nested functions are put in a different
27743 partition than that of their parent function. */
27744 while ((node = limbo_die_list))
27746 dw_die_ref die = node->die;
27747 limbo_die_list = node->next;
27749 if (die->die_parent == NULL)
27751 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27753 if (origin && origin->die_parent)
27754 add_child_die (origin->die_parent, die);
27755 else if (is_cu_die (die))
27757 else if (seen_error ())
27758 /* It's OK to be confused by errors in the input. */
27759 add_child_die (comp_unit_die (), die);
27760 else
27762 /* In certain situations, the lexical block containing a
27763 nested function can be optimized away, which results
27764 in the nested function die being orphaned. Likewise
27765 with the return type of that nested function. Force
27766 this to be a child of the containing function.
27768 It may happen that even the containing function got fully
27769 inlined and optimized out. In that case we are lost and
27770 assign the empty child. This should not be big issue as
27771 the function is likely unreachable too. */
27772 gcc_assert (node->created_for);
27774 if (DECL_P (node->created_for))
27775 origin = get_context_die (DECL_CONTEXT (node->created_for));
27776 else if (TYPE_P (node->created_for))
27777 origin = scope_die_for (node->created_for, comp_unit_die ());
27778 else
27779 origin = comp_unit_die ();
27781 add_child_die (origin, die);
27787 /* Output stuff that dwarf requires at the end of every file,
27788 and generate the DWARF-2 debugging info. */
27790 static void
27791 dwarf2out_finish (const char *filename)
27793 comdat_type_node *ctnode;
27794 dw_die_ref main_comp_unit_die;
27796 /* Flush out any latecomers to the limbo party. */
27797 flush_limbo_die_list ();
27799 /* We shouldn't have any symbols with delayed asm names for
27800 DIEs generated after early finish. */
27801 gcc_assert (deferred_asm_name == NULL);
27803 /* PCH might result in DW_AT_producer string being restored from the
27804 header compilation, so always fill it with empty string initially
27805 and overwrite only here. */
27806 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
27807 producer_string = gen_producer_string ();
27808 producer->dw_attr_val.v.val_str->refcount--;
27809 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
27811 gen_remaining_tmpl_value_param_die_attribute ();
27813 /* Add the name for the main input file now. We delayed this from
27814 dwarf2out_init to avoid complications with PCH.
27815 For LTO produced units use a fixed artificial name to avoid
27816 leaking tempfile names into the dwarf. */
27817 if (!in_lto_p)
27818 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
27819 else
27820 add_name_attribute (comp_unit_die (), "<artificial>");
27821 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
27822 add_comp_dir_attribute (comp_unit_die ());
27823 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
27825 bool p = false;
27826 file_table->traverse<bool *, file_table_relative_p> (&p);
27827 if (p)
27828 add_comp_dir_attribute (comp_unit_die ());
27831 #if ENABLE_ASSERT_CHECKING
27833 dw_die_ref die = comp_unit_die (), c;
27834 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27836 #endif
27837 resolve_addr (comp_unit_die ());
27838 move_marked_base_types ();
27840 if (flag_eliminate_unused_debug_types)
27841 prune_unused_types ();
27843 /* Generate separate COMDAT sections for type DIEs. */
27844 if (use_debug_types)
27846 break_out_comdat_types (comp_unit_die ());
27848 /* Each new type_unit DIE was added to the limbo die list when created.
27849 Since these have all been added to comdat_type_list, clear the
27850 limbo die list. */
27851 limbo_die_list = NULL;
27853 /* For each new comdat type unit, copy declarations for incomplete
27854 types to make the new unit self-contained (i.e., no direct
27855 references to the main compile unit). */
27856 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27857 copy_decls_for_unworthy_types (ctnode->root_die);
27858 copy_decls_for_unworthy_types (comp_unit_die ());
27860 /* In the process of copying declarations from one unit to another,
27861 we may have left some declarations behind that are no longer
27862 referenced. Prune them. */
27863 prune_unused_types ();
27866 /* Generate separate CUs for each of the include files we've seen.
27867 They will go into limbo_die_list. */
27868 if (flag_eliminate_dwarf2_dups)
27869 break_out_includes (comp_unit_die ());
27871 /* Traverse the DIE's and add sibling attributes to those DIE's that
27872 have children. */
27873 add_sibling_attributes (comp_unit_die ());
27874 limbo_die_node *node;
27875 for (node = limbo_die_list; node; node = node->next)
27876 add_sibling_attributes (node->die);
27877 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27878 add_sibling_attributes (ctnode->root_die);
27880 /* When splitting DWARF info, we put some attributes in the
27881 skeleton compile_unit DIE that remains in the .o, while
27882 most attributes go in the DWO compile_unit_die. */
27883 if (dwarf_split_debug_info)
27884 main_comp_unit_die = gen_compile_unit_die (NULL);
27885 else
27886 main_comp_unit_die = comp_unit_die ();
27888 /* Output a terminator label for the .text section. */
27889 switch_to_section (text_section);
27890 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27891 if (cold_text_section)
27893 switch_to_section (cold_text_section);
27894 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27897 /* We can only use the low/high_pc attributes if all of the code was
27898 in .text. */
27899 if (!have_multiple_function_sections
27900 || (dwarf_version < 3 && dwarf_strict))
27902 /* Don't add if the CU has no associated code. */
27903 if (text_section_used)
27904 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27905 text_end_label, true);
27907 else
27909 unsigned fde_idx;
27910 dw_fde_ref fde;
27911 bool range_list_added = false;
27913 if (text_section_used)
27914 add_ranges_by_labels (main_comp_unit_die, text_section_label,
27915 text_end_label, &range_list_added, true);
27916 if (cold_text_section_used)
27917 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27918 cold_end_label, &range_list_added, true);
27920 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27922 if (DECL_IGNORED_P (fde->decl))
27923 continue;
27924 if (!fde->in_std_section)
27925 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27926 fde->dw_fde_end, &range_list_added,
27927 true);
27928 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27929 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27930 fde->dw_fde_second_end, &range_list_added,
27931 true);
27934 if (range_list_added)
27936 /* We need to give .debug_loc and .debug_ranges an appropriate
27937 "base address". Use zero so that these addresses become
27938 absolute. Historically, we've emitted the unexpected
27939 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27940 Emit both to give time for other tools to adapt. */
27941 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27942 if (! dwarf_strict && dwarf_version < 4)
27943 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27945 add_ranges (NULL);
27949 if (debug_info_level >= DINFO_LEVEL_TERSE)
27950 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27951 debug_line_section_label);
27953 if (have_macinfo)
27954 add_AT_macptr (comp_unit_die (),
27955 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
27956 macinfo_section_label);
27958 if (dwarf_split_debug_info)
27960 /* optimize_location_lists calculates the size of the lists,
27961 so index them first, and assign indices to the entries.
27962 Although optimize_location_lists will remove entries from
27963 the table, it only does so for duplicates, and therefore
27964 only reduces ref_counts to 1. */
27965 index_location_lists (comp_unit_die ());
27967 if (addr_index_table != NULL)
27969 unsigned int index = 0;
27970 addr_index_table
27971 ->traverse_noresize<unsigned int *, index_addr_table_entry>
27972 (&index);
27976 if (have_location_lists)
27977 optimize_location_lists (comp_unit_die ());
27979 save_macinfo_strings ();
27981 if (dwarf_split_debug_info)
27983 unsigned int index = 0;
27985 /* Add attributes common to skeleton compile_units and
27986 type_units. Because these attributes include strings, it
27987 must be done before freezing the string table. Top-level
27988 skeleton die attrs are added when the skeleton type unit is
27989 created, so ensure it is created by this point. */
27990 add_top_level_skeleton_die_attrs (main_comp_unit_die);
27991 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
27994 /* Output all of the compilation units. We put the main one last so that
27995 the offsets are available to output_pubnames. */
27996 for (node = limbo_die_list; node; node = node->next)
27997 output_comp_unit (node->die, 0);
27999 hash_table<comdat_type_hasher> comdat_type_table (100);
28000 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
28002 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
28004 /* Don't output duplicate types. */
28005 if (*slot != HTAB_EMPTY_ENTRY)
28006 continue;
28008 /* Add a pointer to the line table for the main compilation unit
28009 so that the debugger can make sense of DW_AT_decl_file
28010 attributes. */
28011 if (debug_info_level >= DINFO_LEVEL_TERSE)
28012 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
28013 (!dwarf_split_debug_info
28014 ? debug_line_section_label
28015 : debug_skeleton_line_section_label));
28017 output_comdat_type_unit (ctnode);
28018 *slot = ctnode;
28021 /* The AT_pubnames attribute needs to go in all skeleton dies, including
28022 both the main_cu and all skeleton TUs. Making this call unconditional
28023 would end up either adding a second copy of the AT_pubnames attribute, or
28024 requiring a special case in add_top_level_skeleton_die_attrs. */
28025 if (!dwarf_split_debug_info)
28026 add_AT_pubnames (comp_unit_die ());
28028 if (dwarf_split_debug_info)
28030 int mark;
28031 unsigned char checksum[16];
28032 struct md5_ctx ctx;
28034 /* Compute a checksum of the comp_unit to use as the dwo_id. */
28035 md5_init_ctx (&ctx);
28036 mark = 0;
28037 die_checksum (comp_unit_die (), &ctx, &mark);
28038 unmark_all_dies (comp_unit_die ());
28039 md5_finish_ctx (&ctx, checksum);
28041 /* Use the first 8 bytes of the checksum as the dwo_id,
28042 and add it to both comp-unit DIEs. */
28043 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
28044 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
28046 /* Add the base offset of the ranges table to the skeleton
28047 comp-unit DIE. */
28048 if (ranges_table_in_use)
28049 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
28050 ranges_section_label);
28052 switch_to_section (debug_addr_section);
28053 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
28054 output_addr_table ();
28057 /* Output the main compilation unit if non-empty or if .debug_macinfo
28058 or .debug_macro will be emitted. */
28059 output_comp_unit (comp_unit_die (), have_macinfo);
28061 if (dwarf_split_debug_info && info_section_emitted)
28062 output_skeleton_debug_sections (main_comp_unit_die);
28064 /* Output the abbreviation table. */
28065 if (abbrev_die_table_in_use != 1)
28067 switch_to_section (debug_abbrev_section);
28068 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
28069 output_abbrev_section ();
28072 /* Output location list section if necessary. */
28073 if (have_location_lists)
28075 /* Output the location lists info. */
28076 switch_to_section (debug_loc_section);
28077 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
28078 output_location_lists (comp_unit_die ());
28081 output_pubtables ();
28083 /* Output the address range information if a CU (.debug_info section)
28084 was emitted. We output an empty table even if we had no functions
28085 to put in it. This because the consumer has no way to tell the
28086 difference between an empty table that we omitted and failure to
28087 generate a table that would have contained data. */
28088 if (info_section_emitted)
28090 switch_to_section (debug_aranges_section);
28091 output_aranges ();
28094 /* Output ranges section if necessary. */
28095 if (ranges_table_in_use)
28097 switch_to_section (debug_ranges_section);
28098 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
28099 output_ranges ();
28102 /* Have to end the macro section. */
28103 if (have_macinfo)
28105 switch_to_section (debug_macinfo_section);
28106 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
28107 output_macinfo ();
28108 dw2_asm_output_data (1, 0, "End compilation unit");
28111 /* Output the source line correspondence table. We must do this
28112 even if there is no line information. Otherwise, on an empty
28113 translation unit, we will generate a present, but empty,
28114 .debug_info section. IRIX 6.5 `nm' will then complain when
28115 examining the file. This is done late so that any filenames
28116 used by the debug_info section are marked as 'used'. */
28117 switch_to_section (debug_line_section);
28118 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
28119 if (! DWARF2_ASM_LINE_DEBUG_INFO)
28120 output_line_info (false);
28122 if (dwarf_split_debug_info && info_section_emitted)
28124 switch_to_section (debug_skeleton_line_section);
28125 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
28126 output_line_info (true);
28129 /* If we emitted any indirect strings, output the string table too. */
28130 if (debug_str_hash || skeleton_debug_str_hash)
28131 output_indirect_strings ();
28134 /* Perform any cleanups needed after the early debug generation pass
28135 has run. */
28137 static void
28138 dwarf2out_early_finish (void)
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"