* lib/gcc-dg.exp (process-message): Support relative line number
[official-gcc.git] / gcc / dwarf2out.c
blobe36473a5c52f16e86b6546456aada56b5b7070c9
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 (const char *);
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_charstar,
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 static bool early_dwarf_finished;
2715 struct set_early_dwarf {
2716 bool saved;
2717 set_early_dwarf () : saved(early_dwarf)
2719 gcc_assert (! early_dwarf_finished);
2720 early_dwarf = true;
2722 ~set_early_dwarf () { early_dwarf = saved; }
2725 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2726 #define FOR_EACH_CHILD(die, c, expr) do { \
2727 c = die->die_child; \
2728 if (c) do { \
2729 c = c->die_sib; \
2730 expr; \
2731 } while (c != die->die_child); \
2732 } while (0)
2734 /* The pubname structure */
2736 typedef struct GTY(()) pubname_struct {
2737 dw_die_ref die;
2738 const char *name;
2740 pubname_entry;
2743 struct GTY(()) dw_ranges {
2744 /* If this is positive, it's a block number, otherwise it's a
2745 bitwise-negated index into dw_ranges_by_label. */
2746 int num;
2749 /* A structure to hold a macinfo entry. */
2751 typedef struct GTY(()) macinfo_struct {
2752 unsigned char code;
2753 unsigned HOST_WIDE_INT lineno;
2754 const char *info;
2756 macinfo_entry;
2759 struct GTY(()) dw_ranges_by_label {
2760 const char *begin;
2761 const char *end;
2764 /* The comdat type node structure. */
2765 struct GTY(()) comdat_type_node
2767 dw_die_ref root_die;
2768 dw_die_ref type_die;
2769 dw_die_ref skeleton_die;
2770 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2771 comdat_type_node *next;
2774 /* A list of DIEs for which we can't determine ancestry (parent_die
2775 field) just yet. Later in dwarf2out_finish we will fill in the
2776 missing bits. */
2777 typedef struct GTY(()) limbo_die_struct {
2778 dw_die_ref die;
2779 /* The tree for which this DIE was created. We use this to
2780 determine ancestry later. */
2781 tree created_for;
2782 struct limbo_die_struct *next;
2784 limbo_die_node;
2786 typedef struct skeleton_chain_struct
2788 dw_die_ref old_die;
2789 dw_die_ref new_die;
2790 struct skeleton_chain_struct *parent;
2792 skeleton_chain_node;
2794 /* Define a macro which returns nonzero for a TYPE_DECL which was
2795 implicitly generated for a type.
2797 Note that, unlike the C front-end (which generates a NULL named
2798 TYPE_DECL node for each complete tagged type, each array type,
2799 and each function type node created) the C++ front-end generates
2800 a _named_ TYPE_DECL node for each tagged type node created.
2801 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2802 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2803 front-end, but for each type, tagged or not. */
2805 #define TYPE_DECL_IS_STUB(decl) \
2806 (DECL_NAME (decl) == NULL_TREE \
2807 || (DECL_ARTIFICIAL (decl) \
2808 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2809 /* This is necessary for stub decls that \
2810 appear in nested inline functions. */ \
2811 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2812 && (decl_ultimate_origin (decl) \
2813 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2815 /* Information concerning the compilation unit's programming
2816 language, and compiler version. */
2818 /* Fixed size portion of the DWARF compilation unit header. */
2819 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2820 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2822 /* Fixed size portion of the DWARF comdat type unit header. */
2823 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2824 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2825 + DWARF_OFFSET_SIZE)
2827 /* Fixed size portion of public names info. */
2828 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2830 /* Fixed size portion of the address range info. */
2831 #define DWARF_ARANGES_HEADER_SIZE \
2832 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2833 DWARF2_ADDR_SIZE * 2) \
2834 - DWARF_INITIAL_LENGTH_SIZE)
2836 /* Size of padding portion in the address range info. It must be
2837 aligned to twice the pointer size. */
2838 #define DWARF_ARANGES_PAD_SIZE \
2839 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2840 DWARF2_ADDR_SIZE * 2) \
2841 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2843 /* Use assembler line directives if available. */
2844 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2845 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2846 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2847 #else
2848 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2849 #endif
2850 #endif
2852 /* Minimum line offset in a special line info. opcode.
2853 This value was chosen to give a reasonable range of values. */
2854 #define DWARF_LINE_BASE -10
2856 /* First special line opcode - leave room for the standard opcodes. */
2857 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2859 /* Range of line offsets in a special line info. opcode. */
2860 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2862 /* Flag that indicates the initial value of the is_stmt_start flag.
2863 In the present implementation, we do not mark any lines as
2864 the beginning of a source statement, because that information
2865 is not made available by the GCC front-end. */
2866 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2868 /* Maximum number of operations per instruction bundle. */
2869 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2870 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2871 #endif
2873 /* This location is used by calc_die_sizes() to keep track
2874 the offset of each DIE within the .debug_info section. */
2875 static unsigned long next_die_offset;
2877 /* Record the root of the DIE's built for the current compilation unit. */
2878 static GTY(()) dw_die_ref single_comp_unit_die;
2880 /* A list of type DIEs that have been separated into comdat sections. */
2881 static GTY(()) comdat_type_node *comdat_type_list;
2883 /* A list of DIEs with a NULL parent waiting to be relocated. */
2884 static GTY(()) limbo_die_node *limbo_die_list;
2886 /* A list of DIEs for which we may have to generate
2887 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2888 static GTY(()) limbo_die_node *deferred_asm_name;
2890 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2892 typedef const char *compare_type;
2894 static hashval_t hash (dwarf_file_data *);
2895 static bool equal (dwarf_file_data *, const char *);
2898 /* Filenames referenced by this compilation unit. */
2899 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2901 struct decl_die_hasher : ggc_ptr_hash<die_node>
2903 typedef tree compare_type;
2905 static hashval_t hash (die_node *);
2906 static bool equal (die_node *, tree);
2908 /* A hash table of references to DIE's that describe declarations.
2909 The key is a DECL_UID() which is a unique number identifying each decl. */
2910 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2912 struct block_die_hasher : ggc_ptr_hash<die_struct>
2914 static hashval_t hash (die_struct *);
2915 static bool equal (die_struct *, die_struct *);
2918 /* A hash table of references to DIE's that describe COMMON blocks.
2919 The key is DECL_UID() ^ die_parent. */
2920 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2922 typedef struct GTY(()) die_arg_entry_struct {
2923 dw_die_ref die;
2924 tree arg;
2925 } die_arg_entry;
2928 /* Node of the variable location list. */
2929 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2930 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2931 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2932 in mode of the EXPR_LIST node and first EXPR_LIST operand
2933 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2934 location or NULL for padding. For larger bitsizes,
2935 mode is 0 and first operand is a CONCAT with bitsize
2936 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2937 NULL as second operand. */
2938 rtx GTY (()) loc;
2939 const char * GTY (()) label;
2940 struct var_loc_node * GTY (()) next;
2943 /* Variable location list. */
2944 struct GTY ((for_user)) var_loc_list_def {
2945 struct var_loc_node * GTY (()) first;
2947 /* Pointer to the last but one or last element of the
2948 chained list. If the list is empty, both first and
2949 last are NULL, if the list contains just one node
2950 or the last node certainly is not redundant, it points
2951 to the last node, otherwise points to the last but one.
2952 Do not mark it for GC because it is marked through the chain. */
2953 struct var_loc_node * GTY ((skip ("%h"))) last;
2955 /* Pointer to the last element before section switch,
2956 if NULL, either sections weren't switched or first
2957 is after section switch. */
2958 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2960 /* DECL_UID of the variable decl. */
2961 unsigned int decl_id;
2963 typedef struct var_loc_list_def var_loc_list;
2965 /* Call argument location list. */
2966 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2967 rtx GTY (()) call_arg_loc_note;
2968 const char * GTY (()) label;
2969 tree GTY (()) block;
2970 bool tail_call_p;
2971 rtx GTY (()) symbol_ref;
2972 struct call_arg_loc_node * GTY (()) next;
2976 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2978 typedef const_tree compare_type;
2980 static hashval_t hash (var_loc_list *);
2981 static bool equal (var_loc_list *, const_tree);
2984 /* Table of decl location linked lists. */
2985 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2987 /* Head and tail of call_arg_loc chain. */
2988 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2989 static struct call_arg_loc_node *call_arg_loc_last;
2991 /* Number of call sites in the current function. */
2992 static int call_site_count = -1;
2993 /* Number of tail call sites in the current function. */
2994 static int tail_call_site_count = -1;
2996 /* A cached location list. */
2997 struct GTY ((for_user)) cached_dw_loc_list_def {
2998 /* The DECL_UID of the decl that this entry describes. */
2999 unsigned int decl_id;
3001 /* The cached location list. */
3002 dw_loc_list_ref loc_list;
3004 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3006 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3009 typedef const_tree compare_type;
3011 static hashval_t hash (cached_dw_loc_list *);
3012 static bool equal (cached_dw_loc_list *, const_tree);
3015 /* Table of cached location lists. */
3016 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3018 /* A pointer to the base of a list of references to DIE's that
3019 are uniquely identified by their tag, presence/absence of
3020 children DIE's, and list of attribute/value pairs. */
3021 static GTY((length ("abbrev_die_table_allocated")))
3022 dw_die_ref *abbrev_die_table;
3024 /* Number of elements currently allocated for abbrev_die_table. */
3025 static GTY(()) unsigned abbrev_die_table_allocated;
3027 /* Number of elements in abbrev_die_table currently in use. */
3028 static GTY(()) unsigned abbrev_die_table_in_use;
3030 /* A hash map to remember the stack usage for DWARF procedures. The value
3031 stored is the stack size difference between before the DWARF procedure
3032 invokation and after it returned. In other words, for a DWARF procedure
3033 that consumes N stack slots and that pushes M ones, this stores M - N. */
3034 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3036 /* Size (in elements) of increments by which we may expand the
3037 abbrev_die_table. */
3038 #define ABBREV_DIE_TABLE_INCREMENT 256
3040 /* A global counter for generating labels for line number data. */
3041 static unsigned int line_info_label_num;
3043 /* The current table to which we should emit line number information
3044 for the current function. This will be set up at the beginning of
3045 assembly for the function. */
3046 static GTY(()) dw_line_info_table *cur_line_info_table;
3048 /* The two default tables of line number info. */
3049 static GTY(()) dw_line_info_table *text_section_line_info;
3050 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3052 /* The set of all non-default tables of line number info. */
3053 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3055 /* A flag to tell pubnames/types export if there is an info section to
3056 refer to. */
3057 static bool info_section_emitted;
3059 /* A pointer to the base of a table that contains a list of publicly
3060 accessible names. */
3061 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3063 /* A pointer to the base of a table that contains a list of publicly
3064 accessible types. */
3065 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3067 /* A pointer to the base of a table that contains a list of macro
3068 defines/undefines (and file start/end markers). */
3069 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3071 /* True if .debug_macinfo or .debug_macros section is going to be
3072 emitted. */
3073 #define have_macinfo \
3074 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3075 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3076 && !macinfo_table->is_empty ())
3078 /* Array of dies for which we should generate .debug_ranges info. */
3079 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3081 /* Number of elements currently allocated for ranges_table. */
3082 static GTY(()) unsigned ranges_table_allocated;
3084 /* Number of elements in ranges_table currently in use. */
3085 static GTY(()) unsigned ranges_table_in_use;
3087 /* Array of pairs of labels referenced in ranges_table. */
3088 static GTY ((length ("ranges_by_label_allocated")))
3089 dw_ranges_by_label *ranges_by_label;
3091 /* Number of elements currently allocated for ranges_by_label. */
3092 static GTY(()) unsigned ranges_by_label_allocated;
3094 /* Number of elements in ranges_by_label currently in use. */
3095 static GTY(()) unsigned ranges_by_label_in_use;
3097 /* Size (in elements) of increments by which we may expand the
3098 ranges_table. */
3099 #define RANGES_TABLE_INCREMENT 64
3101 /* Whether we have location lists that need outputting */
3102 static GTY(()) bool have_location_lists;
3104 /* Unique label counter. */
3105 static GTY(()) unsigned int loclabel_num;
3107 /* Unique label counter for point-of-call tables. */
3108 static GTY(()) unsigned int poc_label_num;
3110 /* The last file entry emitted by maybe_emit_file(). */
3111 static GTY(()) struct dwarf_file_data * last_emitted_file;
3113 /* Number of internal labels generated by gen_internal_sym(). */
3114 static GTY(()) int label_num;
3116 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3118 /* Instances of generic types for which we need to generate debug
3119 info that describe their generic parameters and arguments. That
3120 generation needs to happen once all types are properly laid out so
3121 we do it at the end of compilation. */
3122 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3124 /* Offset from the "steady-state frame pointer" to the frame base,
3125 within the current function. */
3126 static HOST_WIDE_INT frame_pointer_fb_offset;
3127 static bool frame_pointer_fb_offset_valid;
3129 static vec<dw_die_ref> base_types;
3131 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3132 once all arguments are parsed. */
3133 static vec<dw_die_ref> *string_types;
3135 /* Flags to represent a set of attribute classes for attributes that represent
3136 a scalar value (bounds, pointers, ...). */
3137 enum dw_scalar_form
3139 dw_scalar_form_constant = 0x01,
3140 dw_scalar_form_exprloc = 0x02,
3141 dw_scalar_form_reference = 0x04
3144 /* Forward declarations for functions defined in this file. */
3146 static int is_pseudo_reg (const_rtx);
3147 static tree type_main_variant (tree);
3148 static int is_tagged_type (const_tree);
3149 static const char *dwarf_tag_name (unsigned);
3150 static const char *dwarf_attr_name (unsigned);
3151 static const char *dwarf_form_name (unsigned);
3152 static tree decl_ultimate_origin (const_tree);
3153 static tree decl_class_context (tree);
3154 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3155 static inline enum dw_val_class AT_class (dw_attr_node *);
3156 static inline unsigned int AT_index (dw_attr_node *);
3157 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3158 static inline unsigned AT_flag (dw_attr_node *);
3159 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3160 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3161 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3162 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3163 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3164 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3165 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3166 unsigned int, unsigned char *);
3167 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3168 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3169 static inline const char *AT_string (dw_attr_node *);
3170 static enum dwarf_form AT_string_form (dw_attr_node *);
3171 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3172 static void add_AT_specification (dw_die_ref, dw_die_ref);
3173 static inline dw_die_ref AT_ref (dw_attr_node *);
3174 static inline int AT_ref_external (dw_attr_node *);
3175 static inline void set_AT_ref_external (dw_attr_node *, int);
3176 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3177 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3178 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3179 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3180 dw_loc_list_ref);
3181 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3182 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3183 static void remove_addr_table_entry (addr_table_entry *);
3184 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3185 static inline rtx AT_addr (dw_attr_node *);
3186 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3187 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3188 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3189 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3190 unsigned HOST_WIDE_INT);
3191 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3192 unsigned long, bool);
3193 static inline const char *AT_lbl (dw_attr_node *);
3194 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3195 static const char *get_AT_low_pc (dw_die_ref);
3196 static const char *get_AT_hi_pc (dw_die_ref);
3197 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3198 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3199 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3200 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3201 static bool is_cxx (void);
3202 static bool is_fortran (void);
3203 static bool is_ada (void);
3204 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3205 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3206 static void add_child_die (dw_die_ref, dw_die_ref);
3207 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3208 static dw_die_ref lookup_type_die (tree);
3209 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3210 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3211 static void equate_type_number_to_die (tree, dw_die_ref);
3212 static dw_die_ref lookup_decl_die (tree);
3213 static var_loc_list *lookup_decl_loc (const_tree);
3214 static void equate_decl_number_to_die (tree, dw_die_ref);
3215 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3216 static void print_spaces (FILE *);
3217 static void print_die (dw_die_ref, FILE *);
3218 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3219 static dw_die_ref pop_compile_unit (dw_die_ref);
3220 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3221 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3222 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3223 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3224 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3225 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3226 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3227 struct md5_ctx *, int *);
3228 struct checksum_attributes;
3229 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3230 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3231 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3232 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3233 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3234 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3235 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3236 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3237 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3238 static void compute_section_prefix (dw_die_ref);
3239 static int is_type_die (dw_die_ref);
3240 static int is_comdat_die (dw_die_ref);
3241 static int is_symbol_die (dw_die_ref);
3242 static inline bool is_template_instantiation (dw_die_ref);
3243 static void assign_symbol_names (dw_die_ref);
3244 static void break_out_includes (dw_die_ref);
3245 static int is_declaration_die (dw_die_ref);
3246 static int should_move_die_to_comdat (dw_die_ref);
3247 static dw_die_ref clone_as_declaration (dw_die_ref);
3248 static dw_die_ref clone_die (dw_die_ref);
3249 static dw_die_ref clone_tree (dw_die_ref);
3250 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3251 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3252 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3253 static dw_die_ref generate_skeleton (dw_die_ref);
3254 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3255 dw_die_ref,
3256 dw_die_ref);
3257 static void break_out_comdat_types (dw_die_ref);
3258 static void copy_decls_for_unworthy_types (dw_die_ref);
3260 static void add_sibling_attributes (dw_die_ref);
3261 static void output_location_lists (dw_die_ref);
3262 static int constant_size (unsigned HOST_WIDE_INT);
3263 static unsigned long size_of_die (dw_die_ref);
3264 static void calc_die_sizes (dw_die_ref);
3265 static void calc_base_type_die_sizes (void);
3266 static void mark_dies (dw_die_ref);
3267 static void unmark_dies (dw_die_ref);
3268 static void unmark_all_dies (dw_die_ref);
3269 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3270 static unsigned long size_of_aranges (void);
3271 static enum dwarf_form value_format (dw_attr_node *);
3272 static void output_value_format (dw_attr_node *);
3273 static void output_abbrev_section (void);
3274 static void output_die_abbrevs (unsigned long, dw_die_ref);
3275 static void output_die_symbol (dw_die_ref);
3276 static void output_die (dw_die_ref);
3277 static void output_compilation_unit_header (void);
3278 static void output_comp_unit (dw_die_ref, int);
3279 static void output_comdat_type_unit (comdat_type_node *);
3280 static const char *dwarf2_name (tree, int);
3281 static void add_pubname (tree, dw_die_ref);
3282 static void add_enumerator_pubname (const char *, dw_die_ref);
3283 static void add_pubname_string (const char *, dw_die_ref);
3284 static void add_pubtype (tree, dw_die_ref);
3285 static void output_pubnames (vec<pubname_entry, va_gc> *);
3286 static void output_aranges (void);
3287 static unsigned int add_ranges_num (int);
3288 static unsigned int add_ranges (const_tree);
3289 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3290 bool *, bool);
3291 static void output_ranges (void);
3292 static dw_line_info_table *new_line_info_table (void);
3293 static void output_line_info (bool);
3294 static void output_file_names (void);
3295 static dw_die_ref base_type_die (tree, bool);
3296 static int is_base_type (tree);
3297 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3298 static int decl_quals (const_tree);
3299 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3300 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3301 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3302 static int type_is_enum (const_tree);
3303 static unsigned int dbx_reg_number (const_rtx);
3304 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3305 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3306 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3307 enum var_init_status);
3308 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3309 enum var_init_status);
3310 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3311 enum var_init_status);
3312 static int is_based_loc (const_rtx);
3313 static bool resolve_one_addr (rtx *);
3314 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3315 enum var_init_status);
3316 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3317 enum var_init_status);
3318 struct loc_descr_context;
3319 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3320 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3321 static dw_loc_list_ref loc_list_from_tree (tree, int,
3322 const struct loc_descr_context *);
3323 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3324 const struct loc_descr_context *);
3325 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3326 static tree field_type (const_tree);
3327 static unsigned int simple_type_align_in_bits (const_tree);
3328 static unsigned int simple_decl_align_in_bits (const_tree);
3329 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3330 struct vlr_context;
3331 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3332 HOST_WIDE_INT *);
3333 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3334 dw_loc_list_ref);
3335 static void add_data_member_location_attribute (dw_die_ref, tree,
3336 struct vlr_context *);
3337 static bool add_const_value_attribute (dw_die_ref, rtx);
3338 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3339 static void insert_wide_int (const wide_int &, unsigned char *, int);
3340 static void insert_float (const_rtx, unsigned char *);
3341 static rtx rtl_for_decl_location (tree);
3342 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3343 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3344 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3345 static void add_name_attribute (dw_die_ref, const char *);
3346 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3347 static void add_comp_dir_attribute (dw_die_ref);
3348 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3349 const struct loc_descr_context *);
3350 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3351 const struct loc_descr_context *);
3352 static void add_subscript_info (dw_die_ref, tree, bool);
3353 static void add_byte_size_attribute (dw_die_ref, tree);
3354 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3355 struct vlr_context *);
3356 static void add_bit_size_attribute (dw_die_ref, tree);
3357 static void add_prototyped_attribute (dw_die_ref, tree);
3358 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3359 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3360 static void add_src_coords_attributes (dw_die_ref, tree);
3361 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3362 static void add_discr_value (dw_die_ref, dw_discr_value *);
3363 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3364 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3365 static void push_decl_scope (tree);
3366 static void pop_decl_scope (void);
3367 static dw_die_ref scope_die_for (tree, dw_die_ref);
3368 static inline int local_scope_p (dw_die_ref);
3369 static inline int class_scope_p (dw_die_ref);
3370 static inline int class_or_namespace_scope_p (dw_die_ref);
3371 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3372 static void add_calling_convention_attribute (dw_die_ref, tree);
3373 static const char *type_tag (const_tree);
3374 static tree member_declared_type (const_tree);
3375 #if 0
3376 static const char *decl_start_label (tree);
3377 #endif
3378 static void gen_array_type_die (tree, dw_die_ref);
3379 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3380 #if 0
3381 static void gen_entry_point_die (tree, dw_die_ref);
3382 #endif
3383 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3384 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3385 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3386 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3387 static void gen_formal_types_die (tree, dw_die_ref);
3388 static void gen_subprogram_die (tree, dw_die_ref);
3389 static void gen_variable_die (tree, tree, dw_die_ref);
3390 static void gen_const_die (tree, dw_die_ref);
3391 static void gen_label_die (tree, dw_die_ref);
3392 static void gen_lexical_block_die (tree, dw_die_ref);
3393 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3394 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3395 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3396 static dw_die_ref gen_compile_unit_die (const char *);
3397 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3398 static void gen_member_die (tree, dw_die_ref);
3399 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3400 enum debug_info_usage);
3401 static void gen_subroutine_type_die (tree, dw_die_ref);
3402 static void gen_typedef_die (tree, dw_die_ref);
3403 static void gen_type_die (tree, dw_die_ref);
3404 static void gen_block_die (tree, dw_die_ref);
3405 static void decls_for_scope (tree, dw_die_ref);
3406 static bool is_naming_typedef_decl (const_tree);
3407 static inline dw_die_ref get_context_die (tree);
3408 static void gen_namespace_die (tree, dw_die_ref);
3409 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3410 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3411 static dw_die_ref force_decl_die (tree);
3412 static dw_die_ref force_type_die (tree);
3413 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3414 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3415 static struct dwarf_file_data * lookup_filename (const char *);
3416 static void retry_incomplete_types (void);
3417 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3418 static void gen_generic_params_dies (tree);
3419 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3420 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3421 static void splice_child_die (dw_die_ref, dw_die_ref);
3422 static int file_info_cmp (const void *, const void *);
3423 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3424 const char *, const char *);
3425 static void output_loc_list (dw_loc_list_ref);
3426 static char *gen_internal_sym (const char *);
3427 static bool want_pubnames (void);
3429 static void prune_unmark_dies (dw_die_ref);
3430 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3431 static void prune_unused_types_mark (dw_die_ref, int);
3432 static void prune_unused_types_walk (dw_die_ref);
3433 static void prune_unused_types_walk_attribs (dw_die_ref);
3434 static void prune_unused_types_prune (dw_die_ref);
3435 static void prune_unused_types (void);
3436 static int maybe_emit_file (struct dwarf_file_data *fd);
3437 static inline const char *AT_vms_delta1 (dw_attr_node *);
3438 static inline const char *AT_vms_delta2 (dw_attr_node *);
3439 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3440 const char *, const char *);
3441 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3442 static void gen_remaining_tmpl_value_param_die_attribute (void);
3443 static bool generic_type_p (tree);
3444 static void schedule_generic_params_dies_gen (tree t);
3445 static void gen_scheduled_generic_parms_dies (void);
3447 static const char *comp_dir_string (void);
3449 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3451 /* enum for tracking thread-local variables whose address is really an offset
3452 relative to the TLS pointer, which will need link-time relocation, but will
3453 not need relocation by the DWARF consumer. */
3455 enum dtprel_bool
3457 dtprel_false = 0,
3458 dtprel_true = 1
3461 /* Return the operator to use for an address of a variable. For dtprel_true, we
3462 use DW_OP_const*. For regular variables, which need both link-time
3463 relocation and consumer-level relocation (e.g., to account for shared objects
3464 loaded at a random address), we use DW_OP_addr*. */
3466 static inline enum dwarf_location_atom
3467 dw_addr_op (enum dtprel_bool dtprel)
3469 if (dtprel == dtprel_true)
3470 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3471 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3472 else
3473 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3476 /* Return a pointer to a newly allocated address location description. If
3477 dwarf_split_debug_info is true, then record the address with the appropriate
3478 relocation. */
3479 static inline dw_loc_descr_ref
3480 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3482 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3484 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3485 ref->dw_loc_oprnd1.v.val_addr = addr;
3486 ref->dtprel = dtprel;
3487 if (dwarf_split_debug_info)
3488 ref->dw_loc_oprnd1.val_entry
3489 = add_addr_table_entry (addr,
3490 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3491 else
3492 ref->dw_loc_oprnd1.val_entry = NULL;
3494 return ref;
3497 /* Section names used to hold DWARF debugging information. */
3499 #ifndef DEBUG_INFO_SECTION
3500 #define DEBUG_INFO_SECTION ".debug_info"
3501 #endif
3502 #ifndef DEBUG_DWO_INFO_SECTION
3503 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3504 #endif
3505 #ifndef DEBUG_ABBREV_SECTION
3506 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3507 #endif
3508 #ifndef DEBUG_DWO_ABBREV_SECTION
3509 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3510 #endif
3511 #ifndef DEBUG_ARANGES_SECTION
3512 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3513 #endif
3514 #ifndef DEBUG_ADDR_SECTION
3515 #define DEBUG_ADDR_SECTION ".debug_addr"
3516 #endif
3517 #ifndef DEBUG_NORM_MACINFO_SECTION
3518 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3519 #endif
3520 #ifndef DEBUG_DWO_MACINFO_SECTION
3521 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3522 #endif
3523 #ifndef DEBUG_MACINFO_SECTION
3524 #define DEBUG_MACINFO_SECTION \
3525 (!dwarf_split_debug_info \
3526 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3527 #endif
3528 #ifndef DEBUG_NORM_MACRO_SECTION
3529 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3530 #endif
3531 #ifndef DEBUG_DWO_MACRO_SECTION
3532 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3533 #endif
3534 #ifndef DEBUG_MACRO_SECTION
3535 #define DEBUG_MACRO_SECTION \
3536 (!dwarf_split_debug_info \
3537 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3538 #endif
3539 #ifndef DEBUG_LINE_SECTION
3540 #define DEBUG_LINE_SECTION ".debug_line"
3541 #endif
3542 #ifndef DEBUG_DWO_LINE_SECTION
3543 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3544 #endif
3545 #ifndef DEBUG_LOC_SECTION
3546 #define DEBUG_LOC_SECTION ".debug_loc"
3547 #endif
3548 #ifndef DEBUG_DWO_LOC_SECTION
3549 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3550 #endif
3551 #ifndef DEBUG_PUBNAMES_SECTION
3552 #define DEBUG_PUBNAMES_SECTION \
3553 ((debug_generate_pub_sections == 2) \
3554 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3555 #endif
3556 #ifndef DEBUG_PUBTYPES_SECTION
3557 #define DEBUG_PUBTYPES_SECTION \
3558 ((debug_generate_pub_sections == 2) \
3559 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3560 #endif
3561 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3562 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3563 #ifndef DEBUG_STR_OFFSETS_SECTION
3564 #define DEBUG_STR_OFFSETS_SECTION \
3565 (!dwarf_split_debug_info \
3566 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3567 #endif
3568 #ifndef DEBUG_STR_DWO_SECTION
3569 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3570 #endif
3571 #ifndef DEBUG_STR_SECTION
3572 #define DEBUG_STR_SECTION ".debug_str"
3573 #endif
3574 #ifndef DEBUG_RANGES_SECTION
3575 #define DEBUG_RANGES_SECTION ".debug_ranges"
3576 #endif
3578 /* Standard ELF section names for compiled code and data. */
3579 #ifndef TEXT_SECTION_NAME
3580 #define TEXT_SECTION_NAME ".text"
3581 #endif
3583 /* Section flags for .debug_macinfo/.debug_macro section. */
3584 #define DEBUG_MACRO_SECTION_FLAGS \
3585 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3587 /* Section flags for .debug_str section. */
3588 #define DEBUG_STR_SECTION_FLAGS \
3589 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3590 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3591 : SECTION_DEBUG)
3593 /* Section flags for .debug_str.dwo section. */
3594 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3596 /* Labels we insert at beginning sections we can reference instead of
3597 the section names themselves. */
3599 #ifndef TEXT_SECTION_LABEL
3600 #define TEXT_SECTION_LABEL "Ltext"
3601 #endif
3602 #ifndef COLD_TEXT_SECTION_LABEL
3603 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3604 #endif
3605 #ifndef DEBUG_LINE_SECTION_LABEL
3606 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3607 #endif
3608 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3609 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3610 #endif
3611 #ifndef DEBUG_INFO_SECTION_LABEL
3612 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3613 #endif
3614 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3615 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3616 #endif
3617 #ifndef DEBUG_ABBREV_SECTION_LABEL
3618 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3619 #endif
3620 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3621 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3622 #endif
3623 #ifndef DEBUG_ADDR_SECTION_LABEL
3624 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3625 #endif
3626 #ifndef DEBUG_LOC_SECTION_LABEL
3627 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3628 #endif
3629 #ifndef DEBUG_RANGES_SECTION_LABEL
3630 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3631 #endif
3632 #ifndef DEBUG_MACINFO_SECTION_LABEL
3633 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3634 #endif
3635 #ifndef DEBUG_MACRO_SECTION_LABEL
3636 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3637 #endif
3638 #define SKELETON_COMP_DIE_ABBREV 1
3639 #define SKELETON_TYPE_DIE_ABBREV 2
3641 /* Definitions of defaults for formats and names of various special
3642 (artificial) labels which may be generated within this file (when the -g
3643 options is used and DWARF2_DEBUGGING_INFO is in effect.
3644 If necessary, these may be overridden from within the tm.h file, but
3645 typically, overriding these defaults is unnecessary. */
3647 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3648 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3649 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3650 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3651 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3652 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3653 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3654 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3655 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3656 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3657 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3658 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3659 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3660 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3662 #ifndef TEXT_END_LABEL
3663 #define TEXT_END_LABEL "Letext"
3664 #endif
3665 #ifndef COLD_END_LABEL
3666 #define COLD_END_LABEL "Letext_cold"
3667 #endif
3668 #ifndef BLOCK_BEGIN_LABEL
3669 #define BLOCK_BEGIN_LABEL "LBB"
3670 #endif
3671 #ifndef BLOCK_END_LABEL
3672 #define BLOCK_END_LABEL "LBE"
3673 #endif
3674 #ifndef LINE_CODE_LABEL
3675 #define LINE_CODE_LABEL "LM"
3676 #endif
3679 /* Return the root of the DIE's built for the current compilation unit. */
3680 static dw_die_ref
3681 comp_unit_die (void)
3683 if (!single_comp_unit_die)
3684 single_comp_unit_die = gen_compile_unit_die (NULL);
3685 return single_comp_unit_die;
3688 /* We allow a language front-end to designate a function that is to be
3689 called to "demangle" any name before it is put into a DIE. */
3691 static const char *(*demangle_name_func) (const char *);
3693 void
3694 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3696 demangle_name_func = func;
3699 /* Test if rtl node points to a pseudo register. */
3701 static inline int
3702 is_pseudo_reg (const_rtx rtl)
3704 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3705 || (GET_CODE (rtl) == SUBREG
3706 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3709 /* Return a reference to a type, with its const and volatile qualifiers
3710 removed. */
3712 static inline tree
3713 type_main_variant (tree type)
3715 type = TYPE_MAIN_VARIANT (type);
3717 /* ??? There really should be only one main variant among any group of
3718 variants of a given type (and all of the MAIN_VARIANT values for all
3719 members of the group should point to that one type) but sometimes the C
3720 front-end messes this up for array types, so we work around that bug
3721 here. */
3722 if (TREE_CODE (type) == ARRAY_TYPE)
3723 while (type != TYPE_MAIN_VARIANT (type))
3724 type = TYPE_MAIN_VARIANT (type);
3726 return type;
3729 /* Return nonzero if the given type node represents a tagged type. */
3731 static inline int
3732 is_tagged_type (const_tree type)
3734 enum tree_code code = TREE_CODE (type);
3736 return (code == RECORD_TYPE || code == UNION_TYPE
3737 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3740 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3742 static void
3743 get_ref_die_offset_label (char *label, dw_die_ref ref)
3745 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3748 /* Return die_offset of a DIE reference to a base type. */
3750 static unsigned long int
3751 get_base_type_offset (dw_die_ref ref)
3753 if (ref->die_offset)
3754 return ref->die_offset;
3755 if (comp_unit_die ()->die_abbrev)
3757 calc_base_type_die_sizes ();
3758 gcc_assert (ref->die_offset);
3760 return ref->die_offset;
3763 /* Return die_offset of a DIE reference other than base type. */
3765 static unsigned long int
3766 get_ref_die_offset (dw_die_ref ref)
3768 gcc_assert (ref->die_offset);
3769 return ref->die_offset;
3772 /* Convert a DIE tag into its string name. */
3774 static const char *
3775 dwarf_tag_name (unsigned int tag)
3777 const char *name = get_DW_TAG_name (tag);
3779 if (name != NULL)
3780 return name;
3782 return "DW_TAG_<unknown>";
3785 /* Convert a DWARF attribute code into its string name. */
3787 static const char *
3788 dwarf_attr_name (unsigned int attr)
3790 const char *name;
3792 switch (attr)
3794 #if VMS_DEBUGGING_INFO
3795 case DW_AT_HP_prologue:
3796 return "DW_AT_HP_prologue";
3797 #else
3798 case DW_AT_MIPS_loop_unroll_factor:
3799 return "DW_AT_MIPS_loop_unroll_factor";
3800 #endif
3802 #if VMS_DEBUGGING_INFO
3803 case DW_AT_HP_epilogue:
3804 return "DW_AT_HP_epilogue";
3805 #else
3806 case DW_AT_MIPS_stride:
3807 return "DW_AT_MIPS_stride";
3808 #endif
3811 name = get_DW_AT_name (attr);
3813 if (name != NULL)
3814 return name;
3816 return "DW_AT_<unknown>";
3819 /* Convert a DWARF value form code into its string name. */
3821 static const char *
3822 dwarf_form_name (unsigned int form)
3824 const char *name = get_DW_FORM_name (form);
3826 if (name != NULL)
3827 return name;
3829 return "DW_FORM_<unknown>";
3832 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3833 instance of an inlined instance of a decl which is local to an inline
3834 function, so we have to trace all of the way back through the origin chain
3835 to find out what sort of node actually served as the original seed for the
3836 given block. */
3838 static tree
3839 decl_ultimate_origin (const_tree decl)
3841 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3842 return NULL_TREE;
3844 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3845 we're trying to output the abstract instance of this function. */
3846 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3847 return NULL_TREE;
3849 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3850 most distant ancestor, this should never happen. */
3851 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3853 return DECL_ABSTRACT_ORIGIN (decl);
3856 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3857 of a virtual function may refer to a base class, so we check the 'this'
3858 parameter. */
3860 static tree
3861 decl_class_context (tree decl)
3863 tree context = NULL_TREE;
3865 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3866 context = DECL_CONTEXT (decl);
3867 else
3868 context = TYPE_MAIN_VARIANT
3869 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3871 if (context && !TYPE_P (context))
3872 context = NULL_TREE;
3874 return context;
3877 /* Add an attribute/value pair to a DIE. */
3879 static inline void
3880 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3882 /* Maybe this should be an assert? */
3883 if (die == NULL)
3884 return;
3886 vec_safe_reserve (die->die_attr, 1);
3887 vec_safe_push (die->die_attr, *attr);
3890 static inline enum dw_val_class
3891 AT_class (dw_attr_node *a)
3893 return a->dw_attr_val.val_class;
3896 /* Return the index for any attribute that will be referenced with a
3897 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3898 are stored in dw_attr_val.v.val_str for reference counting
3899 pruning. */
3901 static inline unsigned int
3902 AT_index (dw_attr_node *a)
3904 if (AT_class (a) == dw_val_class_str)
3905 return a->dw_attr_val.v.val_str->index;
3906 else if (a->dw_attr_val.val_entry != NULL)
3907 return a->dw_attr_val.val_entry->index;
3908 return NOT_INDEXED;
3911 /* Add a flag value attribute to a DIE. */
3913 static inline void
3914 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3916 dw_attr_node attr;
3918 attr.dw_attr = attr_kind;
3919 attr.dw_attr_val.val_class = dw_val_class_flag;
3920 attr.dw_attr_val.val_entry = NULL;
3921 attr.dw_attr_val.v.val_flag = flag;
3922 add_dwarf_attr (die, &attr);
3925 static inline unsigned
3926 AT_flag (dw_attr_node *a)
3928 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3929 return a->dw_attr_val.v.val_flag;
3932 /* Add a signed integer attribute value to a DIE. */
3934 static inline void
3935 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3937 dw_attr_node attr;
3939 attr.dw_attr = attr_kind;
3940 attr.dw_attr_val.val_class = dw_val_class_const;
3941 attr.dw_attr_val.val_entry = NULL;
3942 attr.dw_attr_val.v.val_int = int_val;
3943 add_dwarf_attr (die, &attr);
3946 static inline HOST_WIDE_INT
3947 AT_int (dw_attr_node *a)
3949 gcc_assert (a && AT_class (a) == dw_val_class_const);
3950 return a->dw_attr_val.v.val_int;
3953 /* Add an unsigned integer attribute value to a DIE. */
3955 static inline void
3956 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3957 unsigned HOST_WIDE_INT unsigned_val)
3959 dw_attr_node attr;
3961 attr.dw_attr = attr_kind;
3962 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3963 attr.dw_attr_val.val_entry = NULL;
3964 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3965 add_dwarf_attr (die, &attr);
3968 static inline unsigned HOST_WIDE_INT
3969 AT_unsigned (dw_attr_node *a)
3971 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3972 return a->dw_attr_val.v.val_unsigned;
3975 /* Add an unsigned wide integer attribute value to a DIE. */
3977 static inline void
3978 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3979 const wide_int& w)
3981 dw_attr_node attr;
3983 attr.dw_attr = attr_kind;
3984 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3985 attr.dw_attr_val.val_entry = NULL;
3986 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3987 *attr.dw_attr_val.v.val_wide = w;
3988 add_dwarf_attr (die, &attr);
3991 /* Add an unsigned double integer attribute value to a DIE. */
3993 static inline void
3994 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3995 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3997 dw_attr_node attr;
3999 attr.dw_attr = attr_kind;
4000 attr.dw_attr_val.val_class = dw_val_class_const_double;
4001 attr.dw_attr_val.val_entry = NULL;
4002 attr.dw_attr_val.v.val_double.high = high;
4003 attr.dw_attr_val.v.val_double.low = low;
4004 add_dwarf_attr (die, &attr);
4007 /* Add a floating point attribute value to a DIE and return it. */
4009 static inline void
4010 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4011 unsigned int length, unsigned int elt_size, unsigned char *array)
4013 dw_attr_node attr;
4015 attr.dw_attr = attr_kind;
4016 attr.dw_attr_val.val_class = dw_val_class_vec;
4017 attr.dw_attr_val.val_entry = NULL;
4018 attr.dw_attr_val.v.val_vec.length = length;
4019 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4020 attr.dw_attr_val.v.val_vec.array = array;
4021 add_dwarf_attr (die, &attr);
4024 /* Add an 8-byte data attribute value to a DIE. */
4026 static inline void
4027 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4028 unsigned char data8[8])
4030 dw_attr_node attr;
4032 attr.dw_attr = attr_kind;
4033 attr.dw_attr_val.val_class = dw_val_class_data8;
4034 attr.dw_attr_val.val_entry = NULL;
4035 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4036 add_dwarf_attr (die, &attr);
4039 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4040 dwarf_split_debug_info, address attributes in dies destined for the
4041 final executable have force_direct set to avoid using indexed
4042 references. */
4044 static inline void
4045 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4046 bool force_direct)
4048 dw_attr_node attr;
4049 char * lbl_id;
4051 lbl_id = xstrdup (lbl_low);
4052 attr.dw_attr = DW_AT_low_pc;
4053 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4054 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4055 if (dwarf_split_debug_info && !force_direct)
4056 attr.dw_attr_val.val_entry
4057 = add_addr_table_entry (lbl_id, ate_kind_label);
4058 else
4059 attr.dw_attr_val.val_entry = NULL;
4060 add_dwarf_attr (die, &attr);
4062 attr.dw_attr = DW_AT_high_pc;
4063 if (dwarf_version < 4)
4064 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4065 else
4066 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4067 lbl_id = xstrdup (lbl_high);
4068 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4069 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4070 && dwarf_split_debug_info && !force_direct)
4071 attr.dw_attr_val.val_entry
4072 = add_addr_table_entry (lbl_id, ate_kind_label);
4073 else
4074 attr.dw_attr_val.val_entry = NULL;
4075 add_dwarf_attr (die, &attr);
4078 /* Hash and equality functions for debug_str_hash. */
4080 hashval_t
4081 indirect_string_hasher::hash (indirect_string_node *x)
4083 return htab_hash_string (x->str);
4086 bool
4087 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4089 return strcmp (x1->str, x2) == 0;
4092 /* Add STR to the given string hash table. */
4094 static struct indirect_string_node *
4095 find_AT_string_in_table (const char *str,
4096 hash_table<indirect_string_hasher> *table)
4098 struct indirect_string_node *node;
4100 indirect_string_node **slot
4101 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4102 if (*slot == NULL)
4104 node = ggc_cleared_alloc<indirect_string_node> ();
4105 node->str = ggc_strdup (str);
4106 *slot = node;
4108 else
4109 node = *slot;
4111 node->refcount++;
4112 return node;
4115 /* Add STR to the indirect string hash table. */
4117 static struct indirect_string_node *
4118 find_AT_string (const char *str)
4120 if (! debug_str_hash)
4121 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4123 return find_AT_string_in_table (str, debug_str_hash);
4126 /* Add a string attribute value to a DIE. */
4128 static inline void
4129 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4131 dw_attr_node attr;
4132 struct indirect_string_node *node;
4134 node = find_AT_string (str);
4136 attr.dw_attr = attr_kind;
4137 attr.dw_attr_val.val_class = dw_val_class_str;
4138 attr.dw_attr_val.val_entry = NULL;
4139 attr.dw_attr_val.v.val_str = node;
4140 add_dwarf_attr (die, &attr);
4143 static inline const char *
4144 AT_string (dw_attr_node *a)
4146 gcc_assert (a && AT_class (a) == dw_val_class_str);
4147 return a->dw_attr_val.v.val_str->str;
4150 /* Call this function directly to bypass AT_string_form's logic to put
4151 the string inline in the die. */
4153 static void
4154 set_indirect_string (struct indirect_string_node *node)
4156 char label[32];
4157 /* Already indirect is a no op. */
4158 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4160 gcc_assert (node->label);
4161 return;
4163 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4164 ++dw2_string_counter;
4165 node->label = xstrdup (label);
4167 if (!dwarf_split_debug_info)
4169 node->form = DW_FORM_strp;
4170 node->index = NOT_INDEXED;
4172 else
4174 node->form = DW_FORM_GNU_str_index;
4175 node->index = NO_INDEX_ASSIGNED;
4179 /* Find out whether a string should be output inline in DIE
4180 or out-of-line in .debug_str section. */
4182 static enum dwarf_form
4183 find_string_form (struct indirect_string_node *node)
4185 unsigned int len;
4187 if (node->form)
4188 return node->form;
4190 len = strlen (node->str) + 1;
4192 /* If the string is shorter or equal to the size of the reference, it is
4193 always better to put it inline. */
4194 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4195 return node->form = DW_FORM_string;
4197 /* If we cannot expect the linker to merge strings in .debug_str
4198 section, only put it into .debug_str if it is worth even in this
4199 single module. */
4200 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4201 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4202 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4203 return node->form = DW_FORM_string;
4205 set_indirect_string (node);
4207 return node->form;
4210 /* Find out whether the string referenced from the attribute should be
4211 output inline in DIE or out-of-line in .debug_str section. */
4213 static enum dwarf_form
4214 AT_string_form (dw_attr_node *a)
4216 gcc_assert (a && AT_class (a) == dw_val_class_str);
4217 return find_string_form (a->dw_attr_val.v.val_str);
4220 /* Add a DIE reference attribute value to a DIE. */
4222 static inline void
4223 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4225 dw_attr_node attr;
4226 gcc_checking_assert (targ_die != NULL);
4228 /* With LTO we can end up trying to reference something we didn't create
4229 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4230 if (targ_die == NULL)
4231 return;
4233 attr.dw_attr = attr_kind;
4234 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4235 attr.dw_attr_val.val_entry = NULL;
4236 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4237 attr.dw_attr_val.v.val_die_ref.external = 0;
4238 add_dwarf_attr (die, &attr);
4241 /* Change DIE reference REF to point to NEW_DIE instead. */
4243 static inline void
4244 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4246 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4247 ref->dw_attr_val.v.val_die_ref.die = new_die;
4248 ref->dw_attr_val.v.val_die_ref.external = 0;
4251 /* Add an AT_specification attribute to a DIE, and also make the back
4252 pointer from the specification to the definition. */
4254 static inline void
4255 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4257 add_AT_die_ref (die, DW_AT_specification, targ_die);
4258 gcc_assert (!targ_die->die_definition);
4259 targ_die->die_definition = die;
4262 static inline dw_die_ref
4263 AT_ref (dw_attr_node *a)
4265 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4266 return a->dw_attr_val.v.val_die_ref.die;
4269 static inline int
4270 AT_ref_external (dw_attr_node *a)
4272 if (a && AT_class (a) == dw_val_class_die_ref)
4273 return a->dw_attr_val.v.val_die_ref.external;
4275 return 0;
4278 static inline void
4279 set_AT_ref_external (dw_attr_node *a, int i)
4281 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4282 a->dw_attr_val.v.val_die_ref.external = i;
4285 /* Add an FDE reference attribute value to a DIE. */
4287 static inline void
4288 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4290 dw_attr_node attr;
4292 attr.dw_attr = attr_kind;
4293 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4294 attr.dw_attr_val.val_entry = NULL;
4295 attr.dw_attr_val.v.val_fde_index = targ_fde;
4296 add_dwarf_attr (die, &attr);
4299 /* Add a location description attribute value to a DIE. */
4301 static inline void
4302 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4304 dw_attr_node attr;
4306 attr.dw_attr = attr_kind;
4307 attr.dw_attr_val.val_class = dw_val_class_loc;
4308 attr.dw_attr_val.val_entry = NULL;
4309 attr.dw_attr_val.v.val_loc = loc;
4310 add_dwarf_attr (die, &attr);
4313 static inline dw_loc_descr_ref
4314 AT_loc (dw_attr_node *a)
4316 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4317 return a->dw_attr_val.v.val_loc;
4320 static inline void
4321 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4323 dw_attr_node attr;
4325 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4326 return;
4328 attr.dw_attr = attr_kind;
4329 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4330 attr.dw_attr_val.val_entry = NULL;
4331 attr.dw_attr_val.v.val_loc_list = loc_list;
4332 add_dwarf_attr (die, &attr);
4333 have_location_lists = true;
4336 static inline dw_loc_list_ref
4337 AT_loc_list (dw_attr_node *a)
4339 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4340 return a->dw_attr_val.v.val_loc_list;
4343 static inline dw_loc_list_ref *
4344 AT_loc_list_ptr (dw_attr_node *a)
4346 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4347 return &a->dw_attr_val.v.val_loc_list;
4350 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4352 static hashval_t hash (addr_table_entry *);
4353 static bool equal (addr_table_entry *, addr_table_entry *);
4356 /* Table of entries into the .debug_addr section. */
4358 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4360 /* Hash an address_table_entry. */
4362 hashval_t
4363 addr_hasher::hash (addr_table_entry *a)
4365 inchash::hash hstate;
4366 switch (a->kind)
4368 case ate_kind_rtx:
4369 hstate.add_int (0);
4370 break;
4371 case ate_kind_rtx_dtprel:
4372 hstate.add_int (1);
4373 break;
4374 case ate_kind_label:
4375 return htab_hash_string (a->addr.label);
4376 default:
4377 gcc_unreachable ();
4379 inchash::add_rtx (a->addr.rtl, hstate);
4380 return hstate.end ();
4383 /* Determine equality for two address_table_entries. */
4385 bool
4386 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4388 if (a1->kind != a2->kind)
4389 return 0;
4390 switch (a1->kind)
4392 case ate_kind_rtx:
4393 case ate_kind_rtx_dtprel:
4394 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4395 case ate_kind_label:
4396 return strcmp (a1->addr.label, a2->addr.label) == 0;
4397 default:
4398 gcc_unreachable ();
4402 /* Initialize an addr_table_entry. */
4404 void
4405 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4407 e->kind = kind;
4408 switch (kind)
4410 case ate_kind_rtx:
4411 case ate_kind_rtx_dtprel:
4412 e->addr.rtl = (rtx) addr;
4413 break;
4414 case ate_kind_label:
4415 e->addr.label = (char *) addr;
4416 break;
4418 e->refcount = 0;
4419 e->index = NO_INDEX_ASSIGNED;
4422 /* Add attr to the address table entry to the table. Defer setting an
4423 index until output time. */
4425 static addr_table_entry *
4426 add_addr_table_entry (void *addr, enum ate_kind kind)
4428 addr_table_entry *node;
4429 addr_table_entry finder;
4431 gcc_assert (dwarf_split_debug_info);
4432 if (! addr_index_table)
4433 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4434 init_addr_table_entry (&finder, kind, addr);
4435 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4437 if (*slot == HTAB_EMPTY_ENTRY)
4439 node = ggc_cleared_alloc<addr_table_entry> ();
4440 init_addr_table_entry (node, kind, addr);
4441 *slot = node;
4443 else
4444 node = *slot;
4446 node->refcount++;
4447 return node;
4450 /* Remove an entry from the addr table by decrementing its refcount.
4451 Strictly, decrementing the refcount would be enough, but the
4452 assertion that the entry is actually in the table has found
4453 bugs. */
4455 static void
4456 remove_addr_table_entry (addr_table_entry *entry)
4458 gcc_assert (dwarf_split_debug_info && addr_index_table);
4459 /* After an index is assigned, the table is frozen. */
4460 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4461 entry->refcount--;
4464 /* Given a location list, remove all addresses it refers to from the
4465 address_table. */
4467 static void
4468 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4470 for (; descr; descr = descr->dw_loc_next)
4471 if (descr->dw_loc_oprnd1.val_entry != NULL)
4473 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4474 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4478 /* A helper function for dwarf2out_finish called through
4479 htab_traverse. Assign an addr_table_entry its index. All entries
4480 must be collected into the table when this function is called,
4481 because the indexing code relies on htab_traverse to traverse nodes
4482 in the same order for each run. */
4485 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4487 addr_table_entry *node = *h;
4489 /* Don't index unreferenced nodes. */
4490 if (node->refcount == 0)
4491 return 1;
4493 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4494 node->index = *index;
4495 *index += 1;
4497 return 1;
4500 /* Add an address constant attribute value to a DIE. When using
4501 dwarf_split_debug_info, address attributes in dies destined for the
4502 final executable should be direct references--setting the parameter
4503 force_direct ensures this behavior. */
4505 static inline void
4506 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4507 bool force_direct)
4509 dw_attr_node attr;
4511 attr.dw_attr = attr_kind;
4512 attr.dw_attr_val.val_class = dw_val_class_addr;
4513 attr.dw_attr_val.v.val_addr = addr;
4514 if (dwarf_split_debug_info && !force_direct)
4515 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4516 else
4517 attr.dw_attr_val.val_entry = NULL;
4518 add_dwarf_attr (die, &attr);
4521 /* Get the RTX from to an address DIE attribute. */
4523 static inline rtx
4524 AT_addr (dw_attr_node *a)
4526 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4527 return a->dw_attr_val.v.val_addr;
4530 /* Add a file attribute value to a DIE. */
4532 static inline void
4533 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4534 struct dwarf_file_data *fd)
4536 dw_attr_node attr;
4538 attr.dw_attr = attr_kind;
4539 attr.dw_attr_val.val_class = dw_val_class_file;
4540 attr.dw_attr_val.val_entry = NULL;
4541 attr.dw_attr_val.v.val_file = fd;
4542 add_dwarf_attr (die, &attr);
4545 /* Get the dwarf_file_data from a file DIE attribute. */
4547 static inline struct dwarf_file_data *
4548 AT_file (dw_attr_node *a)
4550 gcc_assert (a && AT_class (a) == dw_val_class_file);
4551 return a->dw_attr_val.v.val_file;
4554 /* Add a vms delta attribute value to a DIE. */
4556 static inline void
4557 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4558 const char *lbl1, const char *lbl2)
4560 dw_attr_node attr;
4562 attr.dw_attr = attr_kind;
4563 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4564 attr.dw_attr_val.val_entry = NULL;
4565 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4566 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4567 add_dwarf_attr (die, &attr);
4570 /* Add a label identifier attribute value to a DIE. */
4572 static inline void
4573 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4574 const char *lbl_id)
4576 dw_attr_node attr;
4578 attr.dw_attr = attr_kind;
4579 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4580 attr.dw_attr_val.val_entry = NULL;
4581 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4582 if (dwarf_split_debug_info)
4583 attr.dw_attr_val.val_entry
4584 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4585 ate_kind_label);
4586 add_dwarf_attr (die, &attr);
4589 /* Add a section offset attribute value to a DIE, an offset into the
4590 debug_line section. */
4592 static inline void
4593 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4594 const char *label)
4596 dw_attr_node attr;
4598 attr.dw_attr = attr_kind;
4599 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4600 attr.dw_attr_val.val_entry = NULL;
4601 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4602 add_dwarf_attr (die, &attr);
4605 /* Add a section offset attribute value to a DIE, an offset into the
4606 debug_macinfo section. */
4608 static inline void
4609 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4610 const char *label)
4612 dw_attr_node attr;
4614 attr.dw_attr = attr_kind;
4615 attr.dw_attr_val.val_class = dw_val_class_macptr;
4616 attr.dw_attr_val.val_entry = NULL;
4617 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4618 add_dwarf_attr (die, &attr);
4621 /* Add an offset attribute value to a DIE. */
4623 static inline void
4624 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4625 unsigned HOST_WIDE_INT offset)
4627 dw_attr_node attr;
4629 attr.dw_attr = attr_kind;
4630 attr.dw_attr_val.val_class = dw_val_class_offset;
4631 attr.dw_attr_val.val_entry = NULL;
4632 attr.dw_attr_val.v.val_offset = offset;
4633 add_dwarf_attr (die, &attr);
4636 /* Add a range_list attribute value to a DIE. When using
4637 dwarf_split_debug_info, address attributes in dies destined for the
4638 final executable should be direct references--setting the parameter
4639 force_direct ensures this behavior. */
4641 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4642 #define RELOCATED_OFFSET (NULL)
4644 static void
4645 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4646 long unsigned int offset, bool force_direct)
4648 dw_attr_node attr;
4650 attr.dw_attr = attr_kind;
4651 attr.dw_attr_val.val_class = dw_val_class_range_list;
4652 /* For the range_list attribute, use val_entry to store whether the
4653 offset should follow split-debug-info or normal semantics. This
4654 value is read in output_range_list_offset. */
4655 if (dwarf_split_debug_info && !force_direct)
4656 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4657 else
4658 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4659 attr.dw_attr_val.v.val_offset = offset;
4660 add_dwarf_attr (die, &attr);
4663 /* Return the start label of a delta attribute. */
4665 static inline const char *
4666 AT_vms_delta1 (dw_attr_node *a)
4668 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4669 return a->dw_attr_val.v.val_vms_delta.lbl1;
4672 /* Return the end label of a delta attribute. */
4674 static inline const char *
4675 AT_vms_delta2 (dw_attr_node *a)
4677 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4678 return a->dw_attr_val.v.val_vms_delta.lbl2;
4681 static inline const char *
4682 AT_lbl (dw_attr_node *a)
4684 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4685 || AT_class (a) == dw_val_class_lineptr
4686 || AT_class (a) == dw_val_class_macptr
4687 || AT_class (a) == dw_val_class_high_pc));
4688 return a->dw_attr_val.v.val_lbl_id;
4691 /* Get the attribute of type attr_kind. */
4693 static dw_attr_node *
4694 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4696 dw_attr_node *a;
4697 unsigned ix;
4698 dw_die_ref spec = NULL;
4700 if (! die)
4701 return NULL;
4703 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4704 if (a->dw_attr == attr_kind)
4705 return a;
4706 else if (a->dw_attr == DW_AT_specification
4707 || a->dw_attr == DW_AT_abstract_origin)
4708 spec = AT_ref (a);
4710 if (spec)
4711 return get_AT (spec, attr_kind);
4713 return NULL;
4716 /* Returns the parent of the declaration of DIE. */
4718 static dw_die_ref
4719 get_die_parent (dw_die_ref die)
4721 dw_die_ref t;
4723 if (!die)
4724 return NULL;
4726 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4727 || (t = get_AT_ref (die, DW_AT_specification)))
4728 die = t;
4730 return die->die_parent;
4733 /* Return the "low pc" attribute value, typically associated with a subprogram
4734 DIE. Return null if the "low pc" attribute is either not present, or if it
4735 cannot be represented as an assembler label identifier. */
4737 static inline const char *
4738 get_AT_low_pc (dw_die_ref die)
4740 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4742 return a ? AT_lbl (a) : NULL;
4745 /* Return the "high pc" attribute value, typically associated with a subprogram
4746 DIE. Return null if the "high pc" attribute is either not present, or if it
4747 cannot be represented as an assembler label identifier. */
4749 static inline const char *
4750 get_AT_hi_pc (dw_die_ref die)
4752 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4754 return a ? AT_lbl (a) : NULL;
4757 /* Return the value of the string attribute designated by ATTR_KIND, or
4758 NULL if it is not present. */
4760 static inline const char *
4761 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4763 dw_attr_node *a = get_AT (die, attr_kind);
4765 return a ? AT_string (a) : NULL;
4768 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4769 if it is not present. */
4771 static inline int
4772 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4774 dw_attr_node *a = get_AT (die, attr_kind);
4776 return a ? AT_flag (a) : 0;
4779 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4780 if it is not present. */
4782 static inline unsigned
4783 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4785 dw_attr_node *a = get_AT (die, attr_kind);
4787 return a ? AT_unsigned (a) : 0;
4790 static inline dw_die_ref
4791 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4793 dw_attr_node *a = get_AT (die, attr_kind);
4795 return a ? AT_ref (a) : NULL;
4798 static inline struct dwarf_file_data *
4799 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4801 dw_attr_node *a = get_AT (die, attr_kind);
4803 return a ? AT_file (a) : NULL;
4806 /* Return TRUE if the language is C++. */
4808 static inline bool
4809 is_cxx (void)
4811 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4813 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4814 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4817 /* Return TRUE if the language is Java. */
4819 static inline bool
4820 is_java (void)
4822 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4824 return lang == DW_LANG_Java;
4827 /* Return TRUE if the language is Fortran. */
4829 static inline bool
4830 is_fortran (void)
4832 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4834 return (lang == DW_LANG_Fortran77
4835 || lang == DW_LANG_Fortran90
4836 || lang == DW_LANG_Fortran95
4837 || lang == DW_LANG_Fortran03
4838 || lang == DW_LANG_Fortran08);
4841 /* Return TRUE if the language is Ada. */
4843 static inline bool
4844 is_ada (void)
4846 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4848 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4851 /* Remove the specified attribute if present. Return TRUE if removal
4852 was successful. */
4854 static bool
4855 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4857 dw_attr_node *a;
4858 unsigned ix;
4860 if (! die)
4861 return false;
4863 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4864 if (a->dw_attr == attr_kind)
4866 if (AT_class (a) == dw_val_class_str)
4867 if (a->dw_attr_val.v.val_str->refcount)
4868 a->dw_attr_val.v.val_str->refcount--;
4870 /* vec::ordered_remove should help reduce the number of abbrevs
4871 that are needed. */
4872 die->die_attr->ordered_remove (ix);
4873 return true;
4875 return false;
4878 /* Remove CHILD from its parent. PREV must have the property that
4879 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4881 static void
4882 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4884 gcc_assert (child->die_parent == prev->die_parent);
4885 gcc_assert (prev->die_sib == child);
4886 if (prev == child)
4888 gcc_assert (child->die_parent->die_child == child);
4889 prev = NULL;
4891 else
4892 prev->die_sib = child->die_sib;
4893 if (child->die_parent->die_child == child)
4894 child->die_parent->die_child = prev;
4895 child->die_sib = NULL;
4898 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4899 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4901 static void
4902 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4904 dw_die_ref parent = old_child->die_parent;
4906 gcc_assert (parent == prev->die_parent);
4907 gcc_assert (prev->die_sib == old_child);
4909 new_child->die_parent = parent;
4910 if (prev == old_child)
4912 gcc_assert (parent->die_child == old_child);
4913 new_child->die_sib = new_child;
4915 else
4917 prev->die_sib = new_child;
4918 new_child->die_sib = old_child->die_sib;
4920 if (old_child->die_parent->die_child == old_child)
4921 old_child->die_parent->die_child = new_child;
4922 old_child->die_sib = NULL;
4925 /* Move all children from OLD_PARENT to NEW_PARENT. */
4927 static void
4928 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4930 dw_die_ref c;
4931 new_parent->die_child = old_parent->die_child;
4932 old_parent->die_child = NULL;
4933 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4936 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4937 matches TAG. */
4939 static void
4940 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4942 dw_die_ref c;
4944 c = die->die_child;
4945 if (c) do {
4946 dw_die_ref prev = c;
4947 c = c->die_sib;
4948 while (c->die_tag == tag)
4950 remove_child_with_prev (c, prev);
4951 c->die_parent = NULL;
4952 /* Might have removed every child. */
4953 if (die->die_child == NULL)
4954 return;
4955 c = prev->die_sib;
4957 } while (c != die->die_child);
4960 /* Add a CHILD_DIE as the last child of DIE. */
4962 static void
4963 add_child_die (dw_die_ref die, dw_die_ref child_die)
4965 /* FIXME this should probably be an assert. */
4966 if (! die || ! child_die)
4967 return;
4968 gcc_assert (die != child_die);
4970 child_die->die_parent = die;
4971 if (die->die_child)
4973 child_die->die_sib = die->die_child->die_sib;
4974 die->die_child->die_sib = child_die;
4976 else
4977 child_die->die_sib = child_die;
4978 die->die_child = child_die;
4981 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
4983 static void
4984 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4985 dw_die_ref after_die)
4987 gcc_assert (die
4988 && child_die
4989 && after_die
4990 && die->die_child
4991 && die != child_die);
4993 child_die->die_parent = die;
4994 child_die->die_sib = after_die->die_sib;
4995 after_die->die_sib = child_die;
4996 if (die->die_child == after_die)
4997 die->die_child = child_die;
5000 /* Unassociate CHILD from its parent, and make its parent be
5001 NEW_PARENT. */
5003 static void
5004 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5006 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5007 if (p->die_sib == child)
5009 remove_child_with_prev (child, p);
5010 break;
5012 add_child_die (new_parent, child);
5015 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5016 is the specification, to the end of PARENT's list of children.
5017 This is done by removing and re-adding it. */
5019 static void
5020 splice_child_die (dw_die_ref parent, dw_die_ref child)
5022 /* We want the declaration DIE from inside the class, not the
5023 specification DIE at toplevel. */
5024 if (child->die_parent != parent)
5026 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5028 if (tmp)
5029 child = tmp;
5032 gcc_assert (child->die_parent == parent
5033 || (child->die_parent
5034 == get_AT_ref (parent, DW_AT_specification)));
5036 reparent_child (child, parent);
5039 /* Create and return a new die with a parent of PARENT_DIE. If
5040 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5041 associated tree T must be supplied to determine parenthood
5042 later. */
5044 static inline dw_die_ref
5045 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5047 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5049 die->die_tag = tag_value;
5051 if (parent_die != NULL)
5052 add_child_die (parent_die, die);
5053 else
5055 limbo_die_node *limbo_node;
5057 /* No DIEs created after early dwarf should end up in limbo,
5058 because the limbo list should not persist past LTO
5059 streaming. */
5060 if (tag_value != DW_TAG_compile_unit
5061 /* These are allowed because they're generated while
5062 breaking out COMDAT units late. */
5063 && tag_value != DW_TAG_type_unit
5064 && !early_dwarf
5065 /* Allow nested functions to live in limbo because they will
5066 only temporarily live there, as decls_for_scope will fix
5067 them up. */
5068 && (TREE_CODE (t) != FUNCTION_DECL
5069 || !decl_function_context (t))
5070 /* Same as nested functions above but for types. Types that
5071 are local to a function will be fixed in
5072 decls_for_scope. */
5073 && (!RECORD_OR_UNION_TYPE_P (t)
5074 || !TYPE_CONTEXT (t)
5075 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5076 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5077 especially in the ltrans stage, but once we implement LTO
5078 dwarf streaming, we should remove this exception. */
5079 && !in_lto_p)
5081 fprintf (stderr, "symbol ended up in limbo too late:");
5082 debug_generic_stmt (t);
5083 gcc_unreachable ();
5086 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5087 limbo_node->die = die;
5088 limbo_node->created_for = t;
5089 limbo_node->next = limbo_die_list;
5090 limbo_die_list = limbo_node;
5093 return die;
5096 /* Return the DIE associated with the given type specifier. */
5098 static inline dw_die_ref
5099 lookup_type_die (tree type)
5101 return TYPE_SYMTAB_DIE (type);
5104 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5105 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5106 anonymous type instead the one of the naming typedef. */
5108 static inline dw_die_ref
5109 strip_naming_typedef (tree type, dw_die_ref type_die)
5111 if (type
5112 && TREE_CODE (type) == RECORD_TYPE
5113 && type_die
5114 && type_die->die_tag == DW_TAG_typedef
5115 && is_naming_typedef_decl (TYPE_NAME (type)))
5116 type_die = get_AT_ref (type_die, DW_AT_type);
5117 return type_die;
5120 /* Like lookup_type_die, but if type is an anonymous type named by a
5121 typedef[1], return the DIE of the anonymous type instead the one of
5122 the naming typedef. This is because in gen_typedef_die, we did
5123 equate the anonymous struct named by the typedef with the DIE of
5124 the naming typedef. So by default, lookup_type_die on an anonymous
5125 struct yields the DIE of the naming typedef.
5127 [1]: Read the comment of is_naming_typedef_decl to learn about what
5128 a naming typedef is. */
5130 static inline dw_die_ref
5131 lookup_type_die_strip_naming_typedef (tree type)
5133 dw_die_ref die = lookup_type_die (type);
5134 return strip_naming_typedef (type, die);
5137 /* Equate a DIE to a given type specifier. */
5139 static inline void
5140 equate_type_number_to_die (tree type, dw_die_ref type_die)
5142 TYPE_SYMTAB_DIE (type) = type_die;
5145 /* Returns a hash value for X (which really is a die_struct). */
5147 inline hashval_t
5148 decl_die_hasher::hash (die_node *x)
5150 return (hashval_t) x->decl_id;
5153 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5155 inline bool
5156 decl_die_hasher::equal (die_node *x, tree y)
5158 return (x->decl_id == DECL_UID (y));
5161 /* Return the DIE associated with a given declaration. */
5163 static inline dw_die_ref
5164 lookup_decl_die (tree decl)
5166 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5169 /* Returns a hash value for X (which really is a var_loc_list). */
5171 inline hashval_t
5172 decl_loc_hasher::hash (var_loc_list *x)
5174 return (hashval_t) x->decl_id;
5177 /* Return nonzero if decl_id of var_loc_list X is the same as
5178 UID of decl *Y. */
5180 inline bool
5181 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5183 return (x->decl_id == DECL_UID (y));
5186 /* Return the var_loc list associated with a given declaration. */
5188 static inline var_loc_list *
5189 lookup_decl_loc (const_tree decl)
5191 if (!decl_loc_table)
5192 return NULL;
5193 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5196 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5198 inline hashval_t
5199 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5201 return (hashval_t) x->decl_id;
5204 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5205 UID of decl *Y. */
5207 inline bool
5208 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5210 return (x->decl_id == DECL_UID (y));
5213 /* Equate a DIE to a particular declaration. */
5215 static void
5216 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5218 unsigned int decl_id = DECL_UID (decl);
5220 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5221 decl_die->decl_id = decl_id;
5224 /* Return how many bits covers PIECE EXPR_LIST. */
5226 static HOST_WIDE_INT
5227 decl_piece_bitsize (rtx piece)
5229 int ret = (int) GET_MODE (piece);
5230 if (ret)
5231 return ret;
5232 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5233 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5234 return INTVAL (XEXP (XEXP (piece, 0), 0));
5237 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5239 static rtx *
5240 decl_piece_varloc_ptr (rtx piece)
5242 if ((int) GET_MODE (piece))
5243 return &XEXP (piece, 0);
5244 else
5245 return &XEXP (XEXP (piece, 0), 1);
5248 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5249 Next is the chain of following piece nodes. */
5251 static rtx_expr_list *
5252 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5254 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5255 return alloc_EXPR_LIST (bitsize, loc_note, next);
5256 else
5257 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5258 GEN_INT (bitsize),
5259 loc_note), next);
5262 /* Return rtx that should be stored into loc field for
5263 LOC_NOTE and BITPOS/BITSIZE. */
5265 static rtx
5266 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5267 HOST_WIDE_INT bitsize)
5269 if (bitsize != -1)
5271 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5272 if (bitpos != 0)
5273 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5275 return loc_note;
5278 /* This function either modifies location piece list *DEST in
5279 place (if SRC and INNER is NULL), or copies location piece list
5280 *SRC to *DEST while modifying it. Location BITPOS is modified
5281 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5282 not copied and if needed some padding around it is added.
5283 When modifying in place, DEST should point to EXPR_LIST where
5284 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5285 to the start of the whole list and INNER points to the EXPR_LIST
5286 where earlier pieces cover PIECE_BITPOS bits. */
5288 static void
5289 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5290 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5291 HOST_WIDE_INT bitsize, rtx loc_note)
5293 HOST_WIDE_INT diff;
5294 bool copy = inner != NULL;
5296 if (copy)
5298 /* First copy all nodes preceding the current bitpos. */
5299 while (src != inner)
5301 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5302 decl_piece_bitsize (*src), NULL_RTX);
5303 dest = &XEXP (*dest, 1);
5304 src = &XEXP (*src, 1);
5307 /* Add padding if needed. */
5308 if (bitpos != piece_bitpos)
5310 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5311 copy ? NULL_RTX : *dest);
5312 dest = &XEXP (*dest, 1);
5314 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5316 gcc_assert (!copy);
5317 /* A piece with correct bitpos and bitsize already exist,
5318 just update the location for it and return. */
5319 *decl_piece_varloc_ptr (*dest) = loc_note;
5320 return;
5322 /* Add the piece that changed. */
5323 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5324 dest = &XEXP (*dest, 1);
5325 /* Skip over pieces that overlap it. */
5326 diff = bitpos - piece_bitpos + bitsize;
5327 if (!copy)
5328 src = dest;
5329 while (diff > 0 && *src)
5331 rtx piece = *src;
5332 diff -= decl_piece_bitsize (piece);
5333 if (copy)
5334 src = &XEXP (piece, 1);
5335 else
5337 *src = XEXP (piece, 1);
5338 free_EXPR_LIST_node (piece);
5341 /* Add padding if needed. */
5342 if (diff < 0 && *src)
5344 if (!copy)
5345 dest = src;
5346 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5347 dest = &XEXP (*dest, 1);
5349 if (!copy)
5350 return;
5351 /* Finally copy all nodes following it. */
5352 while (*src)
5354 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5355 decl_piece_bitsize (*src), NULL_RTX);
5356 dest = &XEXP (*dest, 1);
5357 src = &XEXP (*src, 1);
5361 /* Add a variable location node to the linked list for DECL. */
5363 static struct var_loc_node *
5364 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5366 unsigned int decl_id;
5367 var_loc_list *temp;
5368 struct var_loc_node *loc = NULL;
5369 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5371 if (TREE_CODE (decl) == VAR_DECL
5372 && DECL_HAS_DEBUG_EXPR_P (decl))
5374 tree realdecl = DECL_DEBUG_EXPR (decl);
5375 if (handled_component_p (realdecl)
5376 || (TREE_CODE (realdecl) == MEM_REF
5377 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5379 HOST_WIDE_INT maxsize;
5380 bool reverse;
5381 tree innerdecl
5382 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5383 &reverse);
5384 if (!DECL_P (innerdecl)
5385 || DECL_IGNORED_P (innerdecl)
5386 || TREE_STATIC (innerdecl)
5387 || bitsize <= 0
5388 || bitpos + bitsize > 256
5389 || bitsize != maxsize)
5390 return NULL;
5391 decl = innerdecl;
5395 decl_id = DECL_UID (decl);
5396 var_loc_list **slot
5397 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5398 if (*slot == NULL)
5400 temp = ggc_cleared_alloc<var_loc_list> ();
5401 temp->decl_id = decl_id;
5402 *slot = temp;
5404 else
5405 temp = *slot;
5407 /* For PARM_DECLs try to keep around the original incoming value,
5408 even if that means we'll emit a zero-range .debug_loc entry. */
5409 if (temp->last
5410 && temp->first == temp->last
5411 && TREE_CODE (decl) == PARM_DECL
5412 && NOTE_P (temp->first->loc)
5413 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5414 && DECL_INCOMING_RTL (decl)
5415 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5416 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5417 == GET_CODE (DECL_INCOMING_RTL (decl))
5418 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5419 && (bitsize != -1
5420 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5421 NOTE_VAR_LOCATION_LOC (loc_note))
5422 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5423 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5425 loc = ggc_cleared_alloc<var_loc_node> ();
5426 temp->first->next = loc;
5427 temp->last = loc;
5428 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5430 else if (temp->last)
5432 struct var_loc_node *last = temp->last, *unused = NULL;
5433 rtx *piece_loc = NULL, last_loc_note;
5434 HOST_WIDE_INT piece_bitpos = 0;
5435 if (last->next)
5437 last = last->next;
5438 gcc_assert (last->next == NULL);
5440 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5442 piece_loc = &last->loc;
5445 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5446 if (piece_bitpos + cur_bitsize > bitpos)
5447 break;
5448 piece_bitpos += cur_bitsize;
5449 piece_loc = &XEXP (*piece_loc, 1);
5451 while (*piece_loc);
5453 /* TEMP->LAST here is either pointer to the last but one or
5454 last element in the chained list, LAST is pointer to the
5455 last element. */
5456 if (label && strcmp (last->label, label) == 0)
5458 /* For SRA optimized variables if there weren't any real
5459 insns since last note, just modify the last node. */
5460 if (piece_loc != NULL)
5462 adjust_piece_list (piece_loc, NULL, NULL,
5463 bitpos, piece_bitpos, bitsize, loc_note);
5464 return NULL;
5466 /* If the last note doesn't cover any instructions, remove it. */
5467 if (temp->last != last)
5469 temp->last->next = NULL;
5470 unused = last;
5471 last = temp->last;
5472 gcc_assert (strcmp (last->label, label) != 0);
5474 else
5476 gcc_assert (temp->first == temp->last
5477 || (temp->first->next == temp->last
5478 && TREE_CODE (decl) == PARM_DECL));
5479 memset (temp->last, '\0', sizeof (*temp->last));
5480 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5481 return temp->last;
5484 if (bitsize == -1 && NOTE_P (last->loc))
5485 last_loc_note = last->loc;
5486 else if (piece_loc != NULL
5487 && *piece_loc != NULL_RTX
5488 && piece_bitpos == bitpos
5489 && decl_piece_bitsize (*piece_loc) == bitsize)
5490 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5491 else
5492 last_loc_note = NULL_RTX;
5493 /* If the current location is the same as the end of the list,
5494 and either both or neither of the locations is uninitialized,
5495 we have nothing to do. */
5496 if (last_loc_note == NULL_RTX
5497 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5498 NOTE_VAR_LOCATION_LOC (loc_note)))
5499 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5500 != NOTE_VAR_LOCATION_STATUS (loc_note))
5501 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5502 == VAR_INIT_STATUS_UNINITIALIZED)
5503 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5504 == VAR_INIT_STATUS_UNINITIALIZED))))
5506 /* Add LOC to the end of list and update LAST. If the last
5507 element of the list has been removed above, reuse its
5508 memory for the new node, otherwise allocate a new one. */
5509 if (unused)
5511 loc = unused;
5512 memset (loc, '\0', sizeof (*loc));
5514 else
5515 loc = ggc_cleared_alloc<var_loc_node> ();
5516 if (bitsize == -1 || piece_loc == NULL)
5517 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5518 else
5519 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5520 bitpos, piece_bitpos, bitsize, loc_note);
5521 last->next = loc;
5522 /* Ensure TEMP->LAST will point either to the new last but one
5523 element of the chain, or to the last element in it. */
5524 if (last != temp->last)
5525 temp->last = last;
5527 else if (unused)
5528 ggc_free (unused);
5530 else
5532 loc = ggc_cleared_alloc<var_loc_node> ();
5533 temp->first = loc;
5534 temp->last = loc;
5535 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5537 return loc;
5540 /* Keep track of the number of spaces used to indent the
5541 output of the debugging routines that print the structure of
5542 the DIE internal representation. */
5543 static int print_indent;
5545 /* Indent the line the number of spaces given by print_indent. */
5547 static inline void
5548 print_spaces (FILE *outfile)
5550 fprintf (outfile, "%*s", print_indent, "");
5553 /* Print a type signature in hex. */
5555 static inline void
5556 print_signature (FILE *outfile, char *sig)
5558 int i;
5560 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5561 fprintf (outfile, "%02x", sig[i] & 0xff);
5564 static inline void
5565 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5567 if (discr_value->pos)
5568 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5569 else
5570 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5573 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5575 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5576 RECURSE, output location descriptor operations. */
5578 static void
5579 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5581 switch (val->val_class)
5583 case dw_val_class_addr:
5584 fprintf (outfile, "address");
5585 break;
5586 case dw_val_class_offset:
5587 fprintf (outfile, "offset");
5588 break;
5589 case dw_val_class_loc:
5590 fprintf (outfile, "location descriptor");
5591 if (val->v.val_loc == NULL)
5592 fprintf (outfile, " -> <null>\n");
5593 else if (recurse)
5595 fprintf (outfile, ":\n");
5596 print_indent += 4;
5597 print_loc_descr (val->v.val_loc, outfile);
5598 print_indent -= 4;
5600 else
5601 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5602 break;
5603 case dw_val_class_loc_list:
5604 fprintf (outfile, "location list -> label:%s",
5605 val->v.val_loc_list->ll_symbol);
5606 break;
5607 case dw_val_class_range_list:
5608 fprintf (outfile, "range list");
5609 break;
5610 case dw_val_class_const:
5611 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5612 break;
5613 case dw_val_class_unsigned_const:
5614 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5615 break;
5616 case dw_val_class_const_double:
5617 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5618 HOST_WIDE_INT_PRINT_UNSIGNED")",
5619 val->v.val_double.high,
5620 val->v.val_double.low);
5621 break;
5622 case dw_val_class_wide_int:
5624 int i = val->v.val_wide->get_len ();
5625 fprintf (outfile, "constant (");
5626 gcc_assert (i > 0);
5627 if (val->v.val_wide->elt (i - 1) == 0)
5628 fprintf (outfile, "0x");
5629 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5630 val->v.val_wide->elt (--i));
5631 while (--i >= 0)
5632 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5633 val->v.val_wide->elt (i));
5634 fprintf (outfile, ")");
5635 break;
5637 case dw_val_class_vec:
5638 fprintf (outfile, "floating-point or vector constant");
5639 break;
5640 case dw_val_class_flag:
5641 fprintf (outfile, "%u", val->v.val_flag);
5642 break;
5643 case dw_val_class_die_ref:
5644 if (val->v.val_die_ref.die != NULL)
5646 dw_die_ref die = val->v.val_die_ref.die;
5648 if (die->comdat_type_p)
5650 fprintf (outfile, "die -> signature: ");
5651 print_signature (outfile,
5652 die->die_id.die_type_node->signature);
5654 else if (die->die_id.die_symbol)
5655 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5656 else
5657 fprintf (outfile, "die -> %ld", die->die_offset);
5658 fprintf (outfile, " (%p)", (void *) die);
5660 else
5661 fprintf (outfile, "die -> <null>");
5662 break;
5663 case dw_val_class_vms_delta:
5664 fprintf (outfile, "delta: @slotcount(%s-%s)",
5665 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5666 break;
5667 case dw_val_class_lbl_id:
5668 case dw_val_class_lineptr:
5669 case dw_val_class_macptr:
5670 case dw_val_class_high_pc:
5671 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5672 break;
5673 case dw_val_class_str:
5674 if (val->v.val_str->str != NULL)
5675 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5676 else
5677 fprintf (outfile, "<null>");
5678 break;
5679 case dw_val_class_file:
5680 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5681 val->v.val_file->emitted_number);
5682 break;
5683 case dw_val_class_data8:
5685 int i;
5687 for (i = 0; i < 8; i++)
5688 fprintf (outfile, "%02x", val->v.val_data8[i]);
5689 break;
5691 case dw_val_class_discr_value:
5692 print_discr_value (outfile, &val->v.val_discr_value);
5693 break;
5694 case dw_val_class_discr_list:
5695 for (dw_discr_list_ref node = val->v.val_discr_list;
5696 node != NULL;
5697 node = node->dw_discr_next)
5699 if (node->dw_discr_range)
5701 fprintf (outfile, " .. ");
5702 print_discr_value (outfile, &node->dw_discr_lower_bound);
5703 print_discr_value (outfile, &node->dw_discr_upper_bound);
5705 else
5706 print_discr_value (outfile, &node->dw_discr_lower_bound);
5708 if (node->dw_discr_next != NULL)
5709 fprintf (outfile, " | ");
5711 default:
5712 break;
5716 /* Likewise, for a DIE attribute. */
5718 static void
5719 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5721 print_dw_val (&a->dw_attr_val, recurse, outfile);
5725 /* Print the list of operands in the LOC location description to OUTFILE. This
5726 routine is a debugging aid only. */
5728 static void
5729 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5731 dw_loc_descr_ref l = loc;
5733 if (loc == NULL)
5735 print_spaces (outfile);
5736 fprintf (outfile, "<null>\n");
5737 return;
5740 for (l = loc; l != NULL; l = l->dw_loc_next)
5742 print_spaces (outfile);
5743 fprintf (outfile, "(%p) %s",
5744 (void *) l,
5745 dwarf_stack_op_name (l->dw_loc_opc));
5746 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5748 fprintf (outfile, " ");
5749 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5751 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5753 fprintf (outfile, ", ");
5754 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5756 fprintf (outfile, "\n");
5760 /* Print the information associated with a given DIE, and its children.
5761 This routine is a debugging aid only. */
5763 static void
5764 print_die (dw_die_ref die, FILE *outfile)
5766 dw_attr_node *a;
5767 dw_die_ref c;
5768 unsigned ix;
5770 print_spaces (outfile);
5771 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5772 die->die_offset, dwarf_tag_name (die->die_tag),
5773 (void*) die);
5774 print_spaces (outfile);
5775 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5776 fprintf (outfile, " offset: %ld", die->die_offset);
5777 fprintf (outfile, " mark: %d\n", die->die_mark);
5779 if (die->comdat_type_p)
5781 print_spaces (outfile);
5782 fprintf (outfile, " signature: ");
5783 print_signature (outfile, die->die_id.die_type_node->signature);
5784 fprintf (outfile, "\n");
5787 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5789 print_spaces (outfile);
5790 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5792 print_attribute (a, true, outfile);
5793 fprintf (outfile, "\n");
5796 if (die->die_child != NULL)
5798 print_indent += 4;
5799 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5800 print_indent -= 4;
5802 if (print_indent == 0)
5803 fprintf (outfile, "\n");
5806 /* Print the list of operations in the LOC location description. */
5808 DEBUG_FUNCTION void
5809 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5811 print_loc_descr (loc, stderr);
5814 /* Print the information collected for a given DIE. */
5816 DEBUG_FUNCTION void
5817 debug_dwarf_die (dw_die_ref die)
5819 print_die (die, stderr);
5822 DEBUG_FUNCTION void
5823 debug (die_struct &ref)
5825 print_die (&ref, stderr);
5828 DEBUG_FUNCTION void
5829 debug (die_struct *ptr)
5831 if (ptr)
5832 debug (*ptr);
5833 else
5834 fprintf (stderr, "<nil>\n");
5838 /* Print all DWARF information collected for the compilation unit.
5839 This routine is a debugging aid only. */
5841 DEBUG_FUNCTION void
5842 debug_dwarf (void)
5844 print_indent = 0;
5845 print_die (comp_unit_die (), stderr);
5848 /* Sanity checks on DIEs. */
5850 static void
5851 check_die (dw_die_ref die)
5853 unsigned ix;
5854 dw_attr_node *a;
5855 bool inline_found = false;
5856 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5857 int n_decl_line = 0, n_decl_file = 0;
5858 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5860 switch (a->dw_attr)
5862 case DW_AT_inline:
5863 if (a->dw_attr_val.v.val_unsigned)
5864 inline_found = true;
5865 break;
5866 case DW_AT_location:
5867 ++n_location;
5868 break;
5869 case DW_AT_low_pc:
5870 ++n_low_pc;
5871 break;
5872 case DW_AT_high_pc:
5873 ++n_high_pc;
5874 break;
5875 case DW_AT_artificial:
5876 ++n_artificial;
5877 break;
5878 case DW_AT_decl_line:
5879 ++n_decl_line;
5880 break;
5881 case DW_AT_decl_file:
5882 ++n_decl_file;
5883 break;
5884 default:
5885 break;
5888 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5889 || n_decl_line > 1 || n_decl_file > 1)
5891 fprintf (stderr, "Duplicate attributes in DIE:\n");
5892 debug_dwarf_die (die);
5893 gcc_unreachable ();
5895 if (inline_found)
5897 /* A debugging information entry that is a member of an abstract
5898 instance tree [that has DW_AT_inline] should not contain any
5899 attributes which describe aspects of the subroutine which vary
5900 between distinct inlined expansions or distinct out-of-line
5901 expansions. */
5902 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5903 gcc_assert (a->dw_attr != DW_AT_low_pc
5904 && a->dw_attr != DW_AT_high_pc
5905 && a->dw_attr != DW_AT_location
5906 && a->dw_attr != DW_AT_frame_base
5907 && a->dw_attr != DW_AT_GNU_all_call_sites);
5911 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5912 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5913 DIE that marks the start of the DIEs for this include file. */
5915 static dw_die_ref
5916 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5918 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5919 dw_die_ref new_unit = gen_compile_unit_die (filename);
5921 new_unit->die_sib = old_unit;
5922 return new_unit;
5925 /* Close an include-file CU and reopen the enclosing one. */
5927 static dw_die_ref
5928 pop_compile_unit (dw_die_ref old_unit)
5930 dw_die_ref new_unit = old_unit->die_sib;
5932 old_unit->die_sib = NULL;
5933 return new_unit;
5936 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5937 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5938 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5940 /* Calculate the checksum of a location expression. */
5942 static inline void
5943 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5945 int tem;
5946 inchash::hash hstate;
5947 hashval_t hash;
5949 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5950 CHECKSUM (tem);
5951 hash_loc_operands (loc, hstate);
5952 hash = hstate.end();
5953 CHECKSUM (hash);
5956 /* Calculate the checksum of an attribute. */
5958 static void
5959 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5961 dw_loc_descr_ref loc;
5962 rtx r;
5964 CHECKSUM (at->dw_attr);
5966 /* We don't care that this was compiled with a different compiler
5967 snapshot; if the output is the same, that's what matters. */
5968 if (at->dw_attr == DW_AT_producer)
5969 return;
5971 switch (AT_class (at))
5973 case dw_val_class_const:
5974 CHECKSUM (at->dw_attr_val.v.val_int);
5975 break;
5976 case dw_val_class_unsigned_const:
5977 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5978 break;
5979 case dw_val_class_const_double:
5980 CHECKSUM (at->dw_attr_val.v.val_double);
5981 break;
5982 case dw_val_class_wide_int:
5983 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5984 get_full_len (*at->dw_attr_val.v.val_wide)
5985 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5986 break;
5987 case dw_val_class_vec:
5988 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5989 (at->dw_attr_val.v.val_vec.length
5990 * at->dw_attr_val.v.val_vec.elt_size));
5991 break;
5992 case dw_val_class_flag:
5993 CHECKSUM (at->dw_attr_val.v.val_flag);
5994 break;
5995 case dw_val_class_str:
5996 CHECKSUM_STRING (AT_string (at));
5997 break;
5999 case dw_val_class_addr:
6000 r = AT_addr (at);
6001 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6002 CHECKSUM_STRING (XSTR (r, 0));
6003 break;
6005 case dw_val_class_offset:
6006 CHECKSUM (at->dw_attr_val.v.val_offset);
6007 break;
6009 case dw_val_class_loc:
6010 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6011 loc_checksum (loc, ctx);
6012 break;
6014 case dw_val_class_die_ref:
6015 die_checksum (AT_ref (at), ctx, mark);
6016 break;
6018 case dw_val_class_fde_ref:
6019 case dw_val_class_vms_delta:
6020 case dw_val_class_lbl_id:
6021 case dw_val_class_lineptr:
6022 case dw_val_class_macptr:
6023 case dw_val_class_high_pc:
6024 break;
6026 case dw_val_class_file:
6027 CHECKSUM_STRING (AT_file (at)->filename);
6028 break;
6030 case dw_val_class_data8:
6031 CHECKSUM (at->dw_attr_val.v.val_data8);
6032 break;
6034 default:
6035 break;
6039 /* Calculate the checksum of a DIE. */
6041 static void
6042 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6044 dw_die_ref c;
6045 dw_attr_node *a;
6046 unsigned ix;
6048 /* To avoid infinite recursion. */
6049 if (die->die_mark)
6051 CHECKSUM (die->die_mark);
6052 return;
6054 die->die_mark = ++(*mark);
6056 CHECKSUM (die->die_tag);
6058 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6059 attr_checksum (a, ctx, mark);
6061 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6064 #undef CHECKSUM
6065 #undef CHECKSUM_BLOCK
6066 #undef CHECKSUM_STRING
6068 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6069 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6070 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6071 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6072 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6073 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6074 #define CHECKSUM_ATTR(FOO) \
6075 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6077 /* Calculate the checksum of a number in signed LEB128 format. */
6079 static void
6080 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6082 unsigned char byte;
6083 bool more;
6085 while (1)
6087 byte = (value & 0x7f);
6088 value >>= 7;
6089 more = !((value == 0 && (byte & 0x40) == 0)
6090 || (value == -1 && (byte & 0x40) != 0));
6091 if (more)
6092 byte |= 0x80;
6093 CHECKSUM (byte);
6094 if (!more)
6095 break;
6099 /* Calculate the checksum of a number in unsigned LEB128 format. */
6101 static void
6102 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6104 while (1)
6106 unsigned char byte = (value & 0x7f);
6107 value >>= 7;
6108 if (value != 0)
6109 /* More bytes to follow. */
6110 byte |= 0x80;
6111 CHECKSUM (byte);
6112 if (value == 0)
6113 break;
6117 /* Checksum the context of the DIE. This adds the names of any
6118 surrounding namespaces or structures to the checksum. */
6120 static void
6121 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6123 const char *name;
6124 dw_die_ref spec;
6125 int tag = die->die_tag;
6127 if (tag != DW_TAG_namespace
6128 && tag != DW_TAG_structure_type
6129 && tag != DW_TAG_class_type)
6130 return;
6132 name = get_AT_string (die, DW_AT_name);
6134 spec = get_AT_ref (die, DW_AT_specification);
6135 if (spec != NULL)
6136 die = spec;
6138 if (die->die_parent != NULL)
6139 checksum_die_context (die->die_parent, ctx);
6141 CHECKSUM_ULEB128 ('C');
6142 CHECKSUM_ULEB128 (tag);
6143 if (name != NULL)
6144 CHECKSUM_STRING (name);
6147 /* Calculate the checksum of a location expression. */
6149 static inline void
6150 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6152 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6153 were emitted as a DW_FORM_sdata instead of a location expression. */
6154 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6156 CHECKSUM_ULEB128 (DW_FORM_sdata);
6157 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6158 return;
6161 /* Otherwise, just checksum the raw location expression. */
6162 while (loc != NULL)
6164 inchash::hash hstate;
6165 hashval_t hash;
6167 CHECKSUM_ULEB128 (loc->dtprel);
6168 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6169 hash_loc_operands (loc, hstate);
6170 hash = hstate.end ();
6171 CHECKSUM (hash);
6172 loc = loc->dw_loc_next;
6176 /* Calculate the checksum of an attribute. */
6178 static void
6179 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6180 struct md5_ctx *ctx, int *mark)
6182 dw_loc_descr_ref loc;
6183 rtx r;
6185 if (AT_class (at) == dw_val_class_die_ref)
6187 dw_die_ref target_die = AT_ref (at);
6189 /* For pointer and reference types, we checksum only the (qualified)
6190 name of the target type (if there is a name). For friend entries,
6191 we checksum only the (qualified) name of the target type or function.
6192 This allows the checksum to remain the same whether the target type
6193 is complete or not. */
6194 if ((at->dw_attr == DW_AT_type
6195 && (tag == DW_TAG_pointer_type
6196 || tag == DW_TAG_reference_type
6197 || tag == DW_TAG_rvalue_reference_type
6198 || tag == DW_TAG_ptr_to_member_type))
6199 || (at->dw_attr == DW_AT_friend
6200 && tag == DW_TAG_friend))
6202 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6204 if (name_attr != NULL)
6206 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6208 if (decl == NULL)
6209 decl = target_die;
6210 CHECKSUM_ULEB128 ('N');
6211 CHECKSUM_ULEB128 (at->dw_attr);
6212 if (decl->die_parent != NULL)
6213 checksum_die_context (decl->die_parent, ctx);
6214 CHECKSUM_ULEB128 ('E');
6215 CHECKSUM_STRING (AT_string (name_attr));
6216 return;
6220 /* For all other references to another DIE, we check to see if the
6221 target DIE has already been visited. If it has, we emit a
6222 backward reference; if not, we descend recursively. */
6223 if (target_die->die_mark > 0)
6225 CHECKSUM_ULEB128 ('R');
6226 CHECKSUM_ULEB128 (at->dw_attr);
6227 CHECKSUM_ULEB128 (target_die->die_mark);
6229 else
6231 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6233 if (decl == NULL)
6234 decl = target_die;
6235 target_die->die_mark = ++(*mark);
6236 CHECKSUM_ULEB128 ('T');
6237 CHECKSUM_ULEB128 (at->dw_attr);
6238 if (decl->die_parent != NULL)
6239 checksum_die_context (decl->die_parent, ctx);
6240 die_checksum_ordered (target_die, ctx, mark);
6242 return;
6245 CHECKSUM_ULEB128 ('A');
6246 CHECKSUM_ULEB128 (at->dw_attr);
6248 switch (AT_class (at))
6250 case dw_val_class_const:
6251 CHECKSUM_ULEB128 (DW_FORM_sdata);
6252 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6253 break;
6255 case dw_val_class_unsigned_const:
6256 CHECKSUM_ULEB128 (DW_FORM_sdata);
6257 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6258 break;
6260 case dw_val_class_const_double:
6261 CHECKSUM_ULEB128 (DW_FORM_block);
6262 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6263 CHECKSUM (at->dw_attr_val.v.val_double);
6264 break;
6266 case dw_val_class_wide_int:
6267 CHECKSUM_ULEB128 (DW_FORM_block);
6268 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6269 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6270 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6271 get_full_len (*at->dw_attr_val.v.val_wide)
6272 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6273 break;
6275 case dw_val_class_vec:
6276 CHECKSUM_ULEB128 (DW_FORM_block);
6277 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6278 * at->dw_attr_val.v.val_vec.elt_size);
6279 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6280 (at->dw_attr_val.v.val_vec.length
6281 * at->dw_attr_val.v.val_vec.elt_size));
6282 break;
6284 case dw_val_class_flag:
6285 CHECKSUM_ULEB128 (DW_FORM_flag);
6286 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6287 break;
6289 case dw_val_class_str:
6290 CHECKSUM_ULEB128 (DW_FORM_string);
6291 CHECKSUM_STRING (AT_string (at));
6292 break;
6294 case dw_val_class_addr:
6295 r = AT_addr (at);
6296 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6297 CHECKSUM_ULEB128 (DW_FORM_string);
6298 CHECKSUM_STRING (XSTR (r, 0));
6299 break;
6301 case dw_val_class_offset:
6302 CHECKSUM_ULEB128 (DW_FORM_sdata);
6303 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6304 break;
6306 case dw_val_class_loc:
6307 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6308 loc_checksum_ordered (loc, ctx);
6309 break;
6311 case dw_val_class_fde_ref:
6312 case dw_val_class_lbl_id:
6313 case dw_val_class_lineptr:
6314 case dw_val_class_macptr:
6315 case dw_val_class_high_pc:
6316 break;
6318 case dw_val_class_file:
6319 CHECKSUM_ULEB128 (DW_FORM_string);
6320 CHECKSUM_STRING (AT_file (at)->filename);
6321 break;
6323 case dw_val_class_data8:
6324 CHECKSUM (at->dw_attr_val.v.val_data8);
6325 break;
6327 default:
6328 break;
6332 struct checksum_attributes
6334 dw_attr_node *at_name;
6335 dw_attr_node *at_type;
6336 dw_attr_node *at_friend;
6337 dw_attr_node *at_accessibility;
6338 dw_attr_node *at_address_class;
6339 dw_attr_node *at_allocated;
6340 dw_attr_node *at_artificial;
6341 dw_attr_node *at_associated;
6342 dw_attr_node *at_binary_scale;
6343 dw_attr_node *at_bit_offset;
6344 dw_attr_node *at_bit_size;
6345 dw_attr_node *at_bit_stride;
6346 dw_attr_node *at_byte_size;
6347 dw_attr_node *at_byte_stride;
6348 dw_attr_node *at_const_value;
6349 dw_attr_node *at_containing_type;
6350 dw_attr_node *at_count;
6351 dw_attr_node *at_data_location;
6352 dw_attr_node *at_data_member_location;
6353 dw_attr_node *at_decimal_scale;
6354 dw_attr_node *at_decimal_sign;
6355 dw_attr_node *at_default_value;
6356 dw_attr_node *at_digit_count;
6357 dw_attr_node *at_discr;
6358 dw_attr_node *at_discr_list;
6359 dw_attr_node *at_discr_value;
6360 dw_attr_node *at_encoding;
6361 dw_attr_node *at_endianity;
6362 dw_attr_node *at_explicit;
6363 dw_attr_node *at_is_optional;
6364 dw_attr_node *at_location;
6365 dw_attr_node *at_lower_bound;
6366 dw_attr_node *at_mutable;
6367 dw_attr_node *at_ordering;
6368 dw_attr_node *at_picture_string;
6369 dw_attr_node *at_prototyped;
6370 dw_attr_node *at_small;
6371 dw_attr_node *at_segment;
6372 dw_attr_node *at_string_length;
6373 dw_attr_node *at_string_length_bit_size;
6374 dw_attr_node *at_string_length_byte_size;
6375 dw_attr_node *at_threads_scaled;
6376 dw_attr_node *at_upper_bound;
6377 dw_attr_node *at_use_location;
6378 dw_attr_node *at_use_UTF8;
6379 dw_attr_node *at_variable_parameter;
6380 dw_attr_node *at_virtuality;
6381 dw_attr_node *at_visibility;
6382 dw_attr_node *at_vtable_elem_location;
6385 /* Collect the attributes that we will want to use for the checksum. */
6387 static void
6388 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6390 dw_attr_node *a;
6391 unsigned ix;
6393 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6395 switch (a->dw_attr)
6397 case DW_AT_name:
6398 attrs->at_name = a;
6399 break;
6400 case DW_AT_type:
6401 attrs->at_type = a;
6402 break;
6403 case DW_AT_friend:
6404 attrs->at_friend = a;
6405 break;
6406 case DW_AT_accessibility:
6407 attrs->at_accessibility = a;
6408 break;
6409 case DW_AT_address_class:
6410 attrs->at_address_class = a;
6411 break;
6412 case DW_AT_allocated:
6413 attrs->at_allocated = a;
6414 break;
6415 case DW_AT_artificial:
6416 attrs->at_artificial = a;
6417 break;
6418 case DW_AT_associated:
6419 attrs->at_associated = a;
6420 break;
6421 case DW_AT_binary_scale:
6422 attrs->at_binary_scale = a;
6423 break;
6424 case DW_AT_bit_offset:
6425 attrs->at_bit_offset = a;
6426 break;
6427 case DW_AT_bit_size:
6428 attrs->at_bit_size = a;
6429 break;
6430 case DW_AT_bit_stride:
6431 attrs->at_bit_stride = a;
6432 break;
6433 case DW_AT_byte_size:
6434 attrs->at_byte_size = a;
6435 break;
6436 case DW_AT_byte_stride:
6437 attrs->at_byte_stride = a;
6438 break;
6439 case DW_AT_const_value:
6440 attrs->at_const_value = a;
6441 break;
6442 case DW_AT_containing_type:
6443 attrs->at_containing_type = a;
6444 break;
6445 case DW_AT_count:
6446 attrs->at_count = a;
6447 break;
6448 case DW_AT_data_location:
6449 attrs->at_data_location = a;
6450 break;
6451 case DW_AT_data_member_location:
6452 attrs->at_data_member_location = a;
6453 break;
6454 case DW_AT_decimal_scale:
6455 attrs->at_decimal_scale = a;
6456 break;
6457 case DW_AT_decimal_sign:
6458 attrs->at_decimal_sign = a;
6459 break;
6460 case DW_AT_default_value:
6461 attrs->at_default_value = a;
6462 break;
6463 case DW_AT_digit_count:
6464 attrs->at_digit_count = a;
6465 break;
6466 case DW_AT_discr:
6467 attrs->at_discr = a;
6468 break;
6469 case DW_AT_discr_list:
6470 attrs->at_discr_list = a;
6471 break;
6472 case DW_AT_discr_value:
6473 attrs->at_discr_value = a;
6474 break;
6475 case DW_AT_encoding:
6476 attrs->at_encoding = a;
6477 break;
6478 case DW_AT_endianity:
6479 attrs->at_endianity = a;
6480 break;
6481 case DW_AT_explicit:
6482 attrs->at_explicit = a;
6483 break;
6484 case DW_AT_is_optional:
6485 attrs->at_is_optional = a;
6486 break;
6487 case DW_AT_location:
6488 attrs->at_location = a;
6489 break;
6490 case DW_AT_lower_bound:
6491 attrs->at_lower_bound = a;
6492 break;
6493 case DW_AT_mutable:
6494 attrs->at_mutable = a;
6495 break;
6496 case DW_AT_ordering:
6497 attrs->at_ordering = a;
6498 break;
6499 case DW_AT_picture_string:
6500 attrs->at_picture_string = a;
6501 break;
6502 case DW_AT_prototyped:
6503 attrs->at_prototyped = a;
6504 break;
6505 case DW_AT_small:
6506 attrs->at_small = a;
6507 break;
6508 case DW_AT_segment:
6509 attrs->at_segment = a;
6510 break;
6511 case DW_AT_string_length:
6512 attrs->at_string_length = a;
6513 break;
6514 case DW_AT_string_length_bit_size:
6515 attrs->at_string_length_bit_size = a;
6516 break;
6517 case DW_AT_string_length_byte_size:
6518 attrs->at_string_length_byte_size = a;
6519 break;
6520 case DW_AT_threads_scaled:
6521 attrs->at_threads_scaled = a;
6522 break;
6523 case DW_AT_upper_bound:
6524 attrs->at_upper_bound = a;
6525 break;
6526 case DW_AT_use_location:
6527 attrs->at_use_location = a;
6528 break;
6529 case DW_AT_use_UTF8:
6530 attrs->at_use_UTF8 = a;
6531 break;
6532 case DW_AT_variable_parameter:
6533 attrs->at_variable_parameter = a;
6534 break;
6535 case DW_AT_virtuality:
6536 attrs->at_virtuality = a;
6537 break;
6538 case DW_AT_visibility:
6539 attrs->at_visibility = a;
6540 break;
6541 case DW_AT_vtable_elem_location:
6542 attrs->at_vtable_elem_location = a;
6543 break;
6544 default:
6545 break;
6550 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6552 static void
6553 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6555 dw_die_ref c;
6556 dw_die_ref decl;
6557 struct checksum_attributes attrs;
6559 CHECKSUM_ULEB128 ('D');
6560 CHECKSUM_ULEB128 (die->die_tag);
6562 memset (&attrs, 0, sizeof (attrs));
6564 decl = get_AT_ref (die, DW_AT_specification);
6565 if (decl != NULL)
6566 collect_checksum_attributes (&attrs, decl);
6567 collect_checksum_attributes (&attrs, die);
6569 CHECKSUM_ATTR (attrs.at_name);
6570 CHECKSUM_ATTR (attrs.at_accessibility);
6571 CHECKSUM_ATTR (attrs.at_address_class);
6572 CHECKSUM_ATTR (attrs.at_allocated);
6573 CHECKSUM_ATTR (attrs.at_artificial);
6574 CHECKSUM_ATTR (attrs.at_associated);
6575 CHECKSUM_ATTR (attrs.at_binary_scale);
6576 CHECKSUM_ATTR (attrs.at_bit_offset);
6577 CHECKSUM_ATTR (attrs.at_bit_size);
6578 CHECKSUM_ATTR (attrs.at_bit_stride);
6579 CHECKSUM_ATTR (attrs.at_byte_size);
6580 CHECKSUM_ATTR (attrs.at_byte_stride);
6581 CHECKSUM_ATTR (attrs.at_const_value);
6582 CHECKSUM_ATTR (attrs.at_containing_type);
6583 CHECKSUM_ATTR (attrs.at_count);
6584 CHECKSUM_ATTR (attrs.at_data_location);
6585 CHECKSUM_ATTR (attrs.at_data_member_location);
6586 CHECKSUM_ATTR (attrs.at_decimal_scale);
6587 CHECKSUM_ATTR (attrs.at_decimal_sign);
6588 CHECKSUM_ATTR (attrs.at_default_value);
6589 CHECKSUM_ATTR (attrs.at_digit_count);
6590 CHECKSUM_ATTR (attrs.at_discr);
6591 CHECKSUM_ATTR (attrs.at_discr_list);
6592 CHECKSUM_ATTR (attrs.at_discr_value);
6593 CHECKSUM_ATTR (attrs.at_encoding);
6594 CHECKSUM_ATTR (attrs.at_endianity);
6595 CHECKSUM_ATTR (attrs.at_explicit);
6596 CHECKSUM_ATTR (attrs.at_is_optional);
6597 CHECKSUM_ATTR (attrs.at_location);
6598 CHECKSUM_ATTR (attrs.at_lower_bound);
6599 CHECKSUM_ATTR (attrs.at_mutable);
6600 CHECKSUM_ATTR (attrs.at_ordering);
6601 CHECKSUM_ATTR (attrs.at_picture_string);
6602 CHECKSUM_ATTR (attrs.at_prototyped);
6603 CHECKSUM_ATTR (attrs.at_small);
6604 CHECKSUM_ATTR (attrs.at_segment);
6605 CHECKSUM_ATTR (attrs.at_string_length);
6606 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6607 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6608 CHECKSUM_ATTR (attrs.at_threads_scaled);
6609 CHECKSUM_ATTR (attrs.at_upper_bound);
6610 CHECKSUM_ATTR (attrs.at_use_location);
6611 CHECKSUM_ATTR (attrs.at_use_UTF8);
6612 CHECKSUM_ATTR (attrs.at_variable_parameter);
6613 CHECKSUM_ATTR (attrs.at_virtuality);
6614 CHECKSUM_ATTR (attrs.at_visibility);
6615 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6616 CHECKSUM_ATTR (attrs.at_type);
6617 CHECKSUM_ATTR (attrs.at_friend);
6619 /* Checksum the child DIEs. */
6620 c = die->die_child;
6621 if (c) do {
6622 dw_attr_node *name_attr;
6624 c = c->die_sib;
6625 name_attr = get_AT (c, DW_AT_name);
6626 if (is_template_instantiation (c))
6628 /* Ignore instantiations of member type and function templates. */
6630 else if (name_attr != NULL
6631 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6633 /* Use a shallow checksum for named nested types and member
6634 functions. */
6635 CHECKSUM_ULEB128 ('S');
6636 CHECKSUM_ULEB128 (c->die_tag);
6637 CHECKSUM_STRING (AT_string (name_attr));
6639 else
6641 /* Use a deep checksum for other children. */
6642 /* Mark this DIE so it gets processed when unmarking. */
6643 if (c->die_mark == 0)
6644 c->die_mark = -1;
6645 die_checksum_ordered (c, ctx, mark);
6647 } while (c != die->die_child);
6649 CHECKSUM_ULEB128 (0);
6652 /* Add a type name and tag to a hash. */
6653 static void
6654 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6656 CHECKSUM_ULEB128 (tag);
6657 CHECKSUM_STRING (name);
6660 #undef CHECKSUM
6661 #undef CHECKSUM_STRING
6662 #undef CHECKSUM_ATTR
6663 #undef CHECKSUM_LEB128
6664 #undef CHECKSUM_ULEB128
6666 /* Generate the type signature for DIE. This is computed by generating an
6667 MD5 checksum over the DIE's tag, its relevant attributes, and its
6668 children. Attributes that are references to other DIEs are processed
6669 by recursion, using the MARK field to prevent infinite recursion.
6670 If the DIE is nested inside a namespace or another type, we also
6671 need to include that context in the signature. The lower 64 bits
6672 of the resulting MD5 checksum comprise the signature. */
6674 static void
6675 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6677 int mark;
6678 const char *name;
6679 unsigned char checksum[16];
6680 struct md5_ctx ctx;
6681 dw_die_ref decl;
6682 dw_die_ref parent;
6684 name = get_AT_string (die, DW_AT_name);
6685 decl = get_AT_ref (die, DW_AT_specification);
6686 parent = get_die_parent (die);
6688 /* First, compute a signature for just the type name (and its surrounding
6689 context, if any. This is stored in the type unit DIE for link-time
6690 ODR (one-definition rule) checking. */
6692 if (is_cxx () && name != NULL)
6694 md5_init_ctx (&ctx);
6696 /* Checksum the names of surrounding namespaces and structures. */
6697 if (parent != NULL)
6698 checksum_die_context (parent, &ctx);
6700 /* Checksum the current DIE. */
6701 die_odr_checksum (die->die_tag, name, &ctx);
6702 md5_finish_ctx (&ctx, checksum);
6704 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6707 /* Next, compute the complete type signature. */
6709 md5_init_ctx (&ctx);
6710 mark = 1;
6711 die->die_mark = mark;
6713 /* Checksum the names of surrounding namespaces and structures. */
6714 if (parent != NULL)
6715 checksum_die_context (parent, &ctx);
6717 /* Checksum the DIE and its children. */
6718 die_checksum_ordered (die, &ctx, &mark);
6719 unmark_all_dies (die);
6720 md5_finish_ctx (&ctx, checksum);
6722 /* Store the signature in the type node and link the type DIE and the
6723 type node together. */
6724 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6725 DWARF_TYPE_SIGNATURE_SIZE);
6726 die->comdat_type_p = true;
6727 die->die_id.die_type_node = type_node;
6728 type_node->type_die = die;
6730 /* If the DIE is a specification, link its declaration to the type node
6731 as well. */
6732 if (decl != NULL)
6734 decl->comdat_type_p = true;
6735 decl->die_id.die_type_node = type_node;
6739 /* Do the location expressions look same? */
6740 static inline int
6741 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6743 return loc1->dw_loc_opc == loc2->dw_loc_opc
6744 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6745 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6748 /* Do the values look the same? */
6749 static int
6750 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6752 dw_loc_descr_ref loc1, loc2;
6753 rtx r1, r2;
6755 if (v1->val_class != v2->val_class)
6756 return 0;
6758 switch (v1->val_class)
6760 case dw_val_class_const:
6761 return v1->v.val_int == v2->v.val_int;
6762 case dw_val_class_unsigned_const:
6763 return v1->v.val_unsigned == v2->v.val_unsigned;
6764 case dw_val_class_const_double:
6765 return v1->v.val_double.high == v2->v.val_double.high
6766 && v1->v.val_double.low == v2->v.val_double.low;
6767 case dw_val_class_wide_int:
6768 return *v1->v.val_wide == *v2->v.val_wide;
6769 case dw_val_class_vec:
6770 if (v1->v.val_vec.length != v2->v.val_vec.length
6771 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6772 return 0;
6773 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6774 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6775 return 0;
6776 return 1;
6777 case dw_val_class_flag:
6778 return v1->v.val_flag == v2->v.val_flag;
6779 case dw_val_class_str:
6780 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6782 case dw_val_class_addr:
6783 r1 = v1->v.val_addr;
6784 r2 = v2->v.val_addr;
6785 if (GET_CODE (r1) != GET_CODE (r2))
6786 return 0;
6787 return !rtx_equal_p (r1, r2);
6789 case dw_val_class_offset:
6790 return v1->v.val_offset == v2->v.val_offset;
6792 case dw_val_class_loc:
6793 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6794 loc1 && loc2;
6795 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6796 if (!same_loc_p (loc1, loc2, mark))
6797 return 0;
6798 return !loc1 && !loc2;
6800 case dw_val_class_die_ref:
6801 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6803 case dw_val_class_fde_ref:
6804 case dw_val_class_vms_delta:
6805 case dw_val_class_lbl_id:
6806 case dw_val_class_lineptr:
6807 case dw_val_class_macptr:
6808 case dw_val_class_high_pc:
6809 return 1;
6811 case dw_val_class_file:
6812 return v1->v.val_file == v2->v.val_file;
6814 case dw_val_class_data8:
6815 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6817 default:
6818 return 1;
6822 /* Do the attributes look the same? */
6824 static int
6825 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6827 if (at1->dw_attr != at2->dw_attr)
6828 return 0;
6830 /* We don't care that this was compiled with a different compiler
6831 snapshot; if the output is the same, that's what matters. */
6832 if (at1->dw_attr == DW_AT_producer)
6833 return 1;
6835 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6838 /* Do the dies look the same? */
6840 static int
6841 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6843 dw_die_ref c1, c2;
6844 dw_attr_node *a1;
6845 unsigned ix;
6847 /* To avoid infinite recursion. */
6848 if (die1->die_mark)
6849 return die1->die_mark == die2->die_mark;
6850 die1->die_mark = die2->die_mark = ++(*mark);
6852 if (die1->die_tag != die2->die_tag)
6853 return 0;
6855 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6856 return 0;
6858 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6859 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6860 return 0;
6862 c1 = die1->die_child;
6863 c2 = die2->die_child;
6864 if (! c1)
6866 if (c2)
6867 return 0;
6869 else
6870 for (;;)
6872 if (!same_die_p (c1, c2, mark))
6873 return 0;
6874 c1 = c1->die_sib;
6875 c2 = c2->die_sib;
6876 if (c1 == die1->die_child)
6878 if (c2 == die2->die_child)
6879 break;
6880 else
6881 return 0;
6885 return 1;
6888 /* Do the dies look the same? Wrapper around same_die_p. */
6890 static int
6891 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6893 int mark = 0;
6894 int ret = same_die_p (die1, die2, &mark);
6896 unmark_all_dies (die1);
6897 unmark_all_dies (die2);
6899 return ret;
6902 /* The prefix to attach to symbols on DIEs in the current comdat debug
6903 info section. */
6904 static const char *comdat_symbol_id;
6906 /* The index of the current symbol within the current comdat CU. */
6907 static unsigned int comdat_symbol_number;
6909 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6910 children, and set comdat_symbol_id accordingly. */
6912 static void
6913 compute_section_prefix (dw_die_ref unit_die)
6915 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6916 const char *base = die_name ? lbasename (die_name) : "anonymous";
6917 char *name = XALLOCAVEC (char, strlen (base) + 64);
6918 char *p;
6919 int i, mark;
6920 unsigned char checksum[16];
6921 struct md5_ctx ctx;
6923 /* Compute the checksum of the DIE, then append part of it as hex digits to
6924 the name filename of the unit. */
6926 md5_init_ctx (&ctx);
6927 mark = 0;
6928 die_checksum (unit_die, &ctx, &mark);
6929 unmark_all_dies (unit_die);
6930 md5_finish_ctx (&ctx, checksum);
6932 sprintf (name, "%s.", base);
6933 clean_symbol_name (name);
6935 p = name + strlen (name);
6936 for (i = 0; i < 4; i++)
6938 sprintf (p, "%.2x", checksum[i]);
6939 p += 2;
6942 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6943 comdat_symbol_number = 0;
6946 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6948 static int
6949 is_type_die (dw_die_ref die)
6951 switch (die->die_tag)
6953 case DW_TAG_array_type:
6954 case DW_TAG_class_type:
6955 case DW_TAG_interface_type:
6956 case DW_TAG_enumeration_type:
6957 case DW_TAG_pointer_type:
6958 case DW_TAG_reference_type:
6959 case DW_TAG_rvalue_reference_type:
6960 case DW_TAG_string_type:
6961 case DW_TAG_structure_type:
6962 case DW_TAG_subroutine_type:
6963 case DW_TAG_union_type:
6964 case DW_TAG_ptr_to_member_type:
6965 case DW_TAG_set_type:
6966 case DW_TAG_subrange_type:
6967 case DW_TAG_base_type:
6968 case DW_TAG_const_type:
6969 case DW_TAG_file_type:
6970 case DW_TAG_packed_type:
6971 case DW_TAG_volatile_type:
6972 case DW_TAG_typedef:
6973 return 1;
6974 default:
6975 return 0;
6979 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6980 Basically, we want to choose the bits that are likely to be shared between
6981 compilations (types) and leave out the bits that are specific to individual
6982 compilations (functions). */
6984 static int
6985 is_comdat_die (dw_die_ref c)
6987 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6988 we do for stabs. The advantage is a greater likelihood of sharing between
6989 objects that don't include headers in the same order (and therefore would
6990 put the base types in a different comdat). jason 8/28/00 */
6992 if (c->die_tag == DW_TAG_base_type)
6993 return 0;
6995 if (c->die_tag == DW_TAG_pointer_type
6996 || c->die_tag == DW_TAG_reference_type
6997 || c->die_tag == DW_TAG_rvalue_reference_type
6998 || c->die_tag == DW_TAG_const_type
6999 || c->die_tag == DW_TAG_volatile_type)
7001 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7003 return t ? is_comdat_die (t) : 0;
7006 return is_type_die (c);
7009 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7010 compilation unit. */
7012 static int
7013 is_symbol_die (dw_die_ref c)
7015 return (is_type_die (c)
7016 || is_declaration_die (c)
7017 || c->die_tag == DW_TAG_namespace
7018 || c->die_tag == DW_TAG_module);
7021 /* Returns true iff C is a compile-unit DIE. */
7023 static inline bool
7024 is_cu_die (dw_die_ref c)
7026 return c && c->die_tag == DW_TAG_compile_unit;
7029 /* Returns true iff C is a unit DIE of some sort. */
7031 static inline bool
7032 is_unit_die (dw_die_ref c)
7034 return c && (c->die_tag == DW_TAG_compile_unit
7035 || c->die_tag == DW_TAG_partial_unit
7036 || c->die_tag == DW_TAG_type_unit);
7039 /* Returns true iff C is a namespace DIE. */
7041 static inline bool
7042 is_namespace_die (dw_die_ref c)
7044 return c && c->die_tag == DW_TAG_namespace;
7047 /* Returns true iff C is a class or structure DIE. */
7049 static inline bool
7050 is_class_die (dw_die_ref c)
7052 return c && (c->die_tag == DW_TAG_class_type
7053 || c->die_tag == DW_TAG_structure_type);
7056 /* Return non-zero if this DIE is a template parameter. */
7058 static inline bool
7059 is_template_parameter (dw_die_ref die)
7061 switch (die->die_tag)
7063 case DW_TAG_template_type_param:
7064 case DW_TAG_template_value_param:
7065 case DW_TAG_GNU_template_template_param:
7066 case DW_TAG_GNU_template_parameter_pack:
7067 return true;
7068 default:
7069 return false;
7073 /* Return non-zero if this DIE represents a template instantiation. */
7075 static inline bool
7076 is_template_instantiation (dw_die_ref die)
7078 dw_die_ref c;
7080 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7081 return false;
7082 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7083 return false;
7086 static char *
7087 gen_internal_sym (const char *prefix)
7089 char buf[256];
7091 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7092 return xstrdup (buf);
7095 /* Assign symbols to all worthy DIEs under DIE. */
7097 static void
7098 assign_symbol_names (dw_die_ref die)
7100 dw_die_ref c;
7102 if (is_symbol_die (die) && !die->comdat_type_p)
7104 if (comdat_symbol_id)
7106 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7108 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7109 comdat_symbol_id, comdat_symbol_number++);
7110 die->die_id.die_symbol = xstrdup (p);
7112 else
7113 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7116 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7119 struct cu_hash_table_entry
7121 dw_die_ref cu;
7122 unsigned min_comdat_num, max_comdat_num;
7123 struct cu_hash_table_entry *next;
7126 /* Helpers to manipulate hash table of CUs. */
7128 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7130 typedef die_struct *compare_type;
7131 static inline hashval_t hash (const cu_hash_table_entry *);
7132 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7133 static inline void remove (cu_hash_table_entry *);
7136 inline hashval_t
7137 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7139 return htab_hash_string (entry->cu->die_id.die_symbol);
7142 inline bool
7143 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7144 const die_struct *entry2)
7146 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7149 inline void
7150 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7152 struct cu_hash_table_entry *next;
7154 while (entry)
7156 next = entry->next;
7157 free (entry);
7158 entry = next;
7162 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7164 /* Check whether we have already seen this CU and set up SYM_NUM
7165 accordingly. */
7166 static int
7167 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7169 struct cu_hash_table_entry dummy;
7170 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7172 dummy.max_comdat_num = 0;
7174 slot = htable->find_slot_with_hash (cu,
7175 htab_hash_string (cu->die_id.die_symbol),
7176 INSERT);
7177 entry = *slot;
7179 for (; entry; last = entry, entry = entry->next)
7181 if (same_die_p_wrap (cu, entry->cu))
7182 break;
7185 if (entry)
7187 *sym_num = entry->min_comdat_num;
7188 return 1;
7191 entry = XCNEW (struct cu_hash_table_entry);
7192 entry->cu = cu;
7193 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7194 entry->next = *slot;
7195 *slot = entry;
7197 return 0;
7200 /* Record SYM_NUM to record of CU in HTABLE. */
7201 static void
7202 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7203 unsigned int sym_num)
7205 struct cu_hash_table_entry **slot, *entry;
7207 slot = htable->find_slot_with_hash (cu,
7208 htab_hash_string (cu->die_id.die_symbol),
7209 NO_INSERT);
7210 entry = *slot;
7212 entry->max_comdat_num = sym_num;
7215 /* Traverse the DIE (which is always comp_unit_die), and set up
7216 additional compilation units for each of the include files we see
7217 bracketed by BINCL/EINCL. */
7219 static void
7220 break_out_includes (dw_die_ref die)
7222 dw_die_ref c;
7223 dw_die_ref unit = NULL;
7224 limbo_die_node *node, **pnode;
7226 c = die->die_child;
7227 if (c) do {
7228 dw_die_ref prev = c;
7229 c = c->die_sib;
7230 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7231 || (unit && is_comdat_die (c)))
7233 dw_die_ref next = c->die_sib;
7235 /* This DIE is for a secondary CU; remove it from the main one. */
7236 remove_child_with_prev (c, prev);
7238 if (c->die_tag == DW_TAG_GNU_BINCL)
7239 unit = push_new_compile_unit (unit, c);
7240 else if (c->die_tag == DW_TAG_GNU_EINCL)
7241 unit = pop_compile_unit (unit);
7242 else
7243 add_child_die (unit, c);
7244 c = next;
7245 if (c == die->die_child)
7246 break;
7248 } while (c != die->die_child);
7250 #if 0
7251 /* We can only use this in debugging, since the frontend doesn't check
7252 to make sure that we leave every include file we enter. */
7253 gcc_assert (!unit);
7254 #endif
7256 assign_symbol_names (die);
7257 cu_hash_type cu_hash_table (10);
7258 for (node = limbo_die_list, pnode = &limbo_die_list;
7259 node;
7260 node = node->next)
7262 int is_dupl;
7264 compute_section_prefix (node->die);
7265 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7266 &comdat_symbol_number);
7267 assign_symbol_names (node->die);
7268 if (is_dupl)
7269 *pnode = node->next;
7270 else
7272 pnode = &node->next;
7273 record_comdat_symbol_number (node->die, &cu_hash_table,
7274 comdat_symbol_number);
7279 /* Return non-zero if this DIE is a declaration. */
7281 static int
7282 is_declaration_die (dw_die_ref die)
7284 dw_attr_node *a;
7285 unsigned ix;
7287 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7288 if (a->dw_attr == DW_AT_declaration)
7289 return 1;
7291 return 0;
7294 /* Return non-zero if this DIE is nested inside a subprogram. */
7296 static int
7297 is_nested_in_subprogram (dw_die_ref die)
7299 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7301 if (decl == NULL)
7302 decl = die;
7303 return local_scope_p (decl);
7306 /* Return non-zero if this DIE contains a defining declaration of a
7307 subprogram. */
7309 static int
7310 contains_subprogram_definition (dw_die_ref die)
7312 dw_die_ref c;
7314 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7315 return 1;
7316 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7317 return 0;
7320 /* Return non-zero if this is a type DIE that should be moved to a
7321 COMDAT .debug_types section. */
7323 static int
7324 should_move_die_to_comdat (dw_die_ref die)
7326 switch (die->die_tag)
7328 case DW_TAG_class_type:
7329 case DW_TAG_structure_type:
7330 case DW_TAG_enumeration_type:
7331 case DW_TAG_union_type:
7332 /* Don't move declarations, inlined instances, types nested in a
7333 subprogram, or types that contain subprogram definitions. */
7334 if (is_declaration_die (die)
7335 || get_AT (die, DW_AT_abstract_origin)
7336 || is_nested_in_subprogram (die)
7337 || contains_subprogram_definition (die))
7338 return 0;
7339 return 1;
7340 case DW_TAG_array_type:
7341 case DW_TAG_interface_type:
7342 case DW_TAG_pointer_type:
7343 case DW_TAG_reference_type:
7344 case DW_TAG_rvalue_reference_type:
7345 case DW_TAG_string_type:
7346 case DW_TAG_subroutine_type:
7347 case DW_TAG_ptr_to_member_type:
7348 case DW_TAG_set_type:
7349 case DW_TAG_subrange_type:
7350 case DW_TAG_base_type:
7351 case DW_TAG_const_type:
7352 case DW_TAG_file_type:
7353 case DW_TAG_packed_type:
7354 case DW_TAG_volatile_type:
7355 case DW_TAG_typedef:
7356 default:
7357 return 0;
7361 /* Make a clone of DIE. */
7363 static dw_die_ref
7364 clone_die (dw_die_ref die)
7366 dw_die_ref clone;
7367 dw_attr_node *a;
7368 unsigned ix;
7370 clone = ggc_cleared_alloc<die_node> ();
7371 clone->die_tag = die->die_tag;
7373 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7374 add_dwarf_attr (clone, a);
7376 return clone;
7379 /* Make a clone of the tree rooted at DIE. */
7381 static dw_die_ref
7382 clone_tree (dw_die_ref die)
7384 dw_die_ref c;
7385 dw_die_ref clone = clone_die (die);
7387 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7389 return clone;
7392 /* Make a clone of DIE as a declaration. */
7394 static dw_die_ref
7395 clone_as_declaration (dw_die_ref die)
7397 dw_die_ref clone;
7398 dw_die_ref decl;
7399 dw_attr_node *a;
7400 unsigned ix;
7402 /* If the DIE is already a declaration, just clone it. */
7403 if (is_declaration_die (die))
7404 return clone_die (die);
7406 /* If the DIE is a specification, just clone its declaration DIE. */
7407 decl = get_AT_ref (die, DW_AT_specification);
7408 if (decl != NULL)
7410 clone = clone_die (decl);
7411 if (die->comdat_type_p)
7412 add_AT_die_ref (clone, DW_AT_signature, die);
7413 return clone;
7416 clone = ggc_cleared_alloc<die_node> ();
7417 clone->die_tag = die->die_tag;
7419 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7421 /* We don't want to copy over all attributes.
7422 For example we don't want DW_AT_byte_size because otherwise we will no
7423 longer have a declaration and GDB will treat it as a definition. */
7425 switch (a->dw_attr)
7427 case DW_AT_abstract_origin:
7428 case DW_AT_artificial:
7429 case DW_AT_containing_type:
7430 case DW_AT_external:
7431 case DW_AT_name:
7432 case DW_AT_type:
7433 case DW_AT_virtuality:
7434 case DW_AT_linkage_name:
7435 case DW_AT_MIPS_linkage_name:
7436 add_dwarf_attr (clone, a);
7437 break;
7438 case DW_AT_byte_size:
7439 default:
7440 break;
7444 if (die->comdat_type_p)
7445 add_AT_die_ref (clone, DW_AT_signature, die);
7447 add_AT_flag (clone, DW_AT_declaration, 1);
7448 return clone;
7452 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7454 struct decl_table_entry
7456 dw_die_ref orig;
7457 dw_die_ref copy;
7460 /* Helpers to manipulate hash table of copied declarations. */
7462 /* Hashtable helpers. */
7464 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7466 typedef die_struct *compare_type;
7467 static inline hashval_t hash (const decl_table_entry *);
7468 static inline bool equal (const decl_table_entry *, const die_struct *);
7471 inline hashval_t
7472 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7474 return htab_hash_pointer (entry->orig);
7477 inline bool
7478 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7479 const die_struct *entry2)
7481 return entry1->orig == entry2;
7484 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7486 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7487 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7488 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7489 to check if the ancestor has already been copied into UNIT. */
7491 static dw_die_ref
7492 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7493 decl_hash_type *decl_table)
7495 dw_die_ref parent = die->die_parent;
7496 dw_die_ref new_parent = unit;
7497 dw_die_ref copy;
7498 decl_table_entry **slot = NULL;
7499 struct decl_table_entry *entry = NULL;
7501 if (decl_table)
7503 /* Check if the entry has already been copied to UNIT. */
7504 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7505 INSERT);
7506 if (*slot != HTAB_EMPTY_ENTRY)
7508 entry = *slot;
7509 return entry->copy;
7512 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7513 entry = XCNEW (struct decl_table_entry);
7514 entry->orig = die;
7515 entry->copy = NULL;
7516 *slot = entry;
7519 if (parent != NULL)
7521 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7522 if (spec != NULL)
7523 parent = spec;
7524 if (!is_unit_die (parent))
7525 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7528 copy = clone_as_declaration (die);
7529 add_child_die (new_parent, copy);
7531 if (decl_table)
7533 /* Record the pointer to the copy. */
7534 entry->copy = copy;
7537 return copy;
7539 /* Copy the declaration context to the new type unit DIE. This includes
7540 any surrounding namespace or type declarations. If the DIE has an
7541 AT_specification attribute, it also includes attributes and children
7542 attached to the specification, and returns a pointer to the original
7543 parent of the declaration DIE. Returns NULL otherwise. */
7545 static dw_die_ref
7546 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7548 dw_die_ref decl;
7549 dw_die_ref new_decl;
7550 dw_die_ref orig_parent = NULL;
7552 decl = get_AT_ref (die, DW_AT_specification);
7553 if (decl == NULL)
7554 decl = die;
7555 else
7557 unsigned ix;
7558 dw_die_ref c;
7559 dw_attr_node *a;
7561 /* The original DIE will be changed to a declaration, and must
7562 be moved to be a child of the original declaration DIE. */
7563 orig_parent = decl->die_parent;
7565 /* Copy the type node pointer from the new DIE to the original
7566 declaration DIE so we can forward references later. */
7567 decl->comdat_type_p = true;
7568 decl->die_id.die_type_node = die->die_id.die_type_node;
7570 remove_AT (die, DW_AT_specification);
7572 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7574 if (a->dw_attr != DW_AT_name
7575 && a->dw_attr != DW_AT_declaration
7576 && a->dw_attr != DW_AT_external)
7577 add_dwarf_attr (die, a);
7580 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7583 if (decl->die_parent != NULL
7584 && !is_unit_die (decl->die_parent))
7586 new_decl = copy_ancestor_tree (unit, decl, NULL);
7587 if (new_decl != NULL)
7589 remove_AT (new_decl, DW_AT_signature);
7590 add_AT_specification (die, new_decl);
7594 return orig_parent;
7597 /* Generate the skeleton ancestor tree for the given NODE, then clone
7598 the DIE and add the clone into the tree. */
7600 static void
7601 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7603 if (node->new_die != NULL)
7604 return;
7606 node->new_die = clone_as_declaration (node->old_die);
7608 if (node->parent != NULL)
7610 generate_skeleton_ancestor_tree (node->parent);
7611 add_child_die (node->parent->new_die, node->new_die);
7615 /* Generate a skeleton tree of DIEs containing any declarations that are
7616 found in the original tree. We traverse the tree looking for declaration
7617 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7619 static void
7620 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7622 skeleton_chain_node node;
7623 dw_die_ref c;
7624 dw_die_ref first;
7625 dw_die_ref prev = NULL;
7626 dw_die_ref next = NULL;
7628 node.parent = parent;
7630 first = c = parent->old_die->die_child;
7631 if (c)
7632 next = c->die_sib;
7633 if (c) do {
7634 if (prev == NULL || prev->die_sib == c)
7635 prev = c;
7636 c = next;
7637 next = (c == first ? NULL : c->die_sib);
7638 node.old_die = c;
7639 node.new_die = NULL;
7640 if (is_declaration_die (c))
7642 if (is_template_instantiation (c))
7644 /* Instantiated templates do not need to be cloned into the
7645 type unit. Just move the DIE and its children back to
7646 the skeleton tree (in the main CU). */
7647 remove_child_with_prev (c, prev);
7648 add_child_die (parent->new_die, c);
7649 c = prev;
7651 else
7653 /* Clone the existing DIE, move the original to the skeleton
7654 tree (which is in the main CU), and put the clone, with
7655 all the original's children, where the original came from
7656 (which is about to be moved to the type unit). */
7657 dw_die_ref clone = clone_die (c);
7658 move_all_children (c, clone);
7660 /* If the original has a DW_AT_object_pointer attribute,
7661 it would now point to a child DIE just moved to the
7662 cloned tree, so we need to remove that attribute from
7663 the original. */
7664 remove_AT (c, DW_AT_object_pointer);
7666 replace_child (c, clone, prev);
7667 generate_skeleton_ancestor_tree (parent);
7668 add_child_die (parent->new_die, c);
7669 node.new_die = c;
7670 c = clone;
7673 generate_skeleton_bottom_up (&node);
7674 } while (next != NULL);
7677 /* Wrapper function for generate_skeleton_bottom_up. */
7679 static dw_die_ref
7680 generate_skeleton (dw_die_ref die)
7682 skeleton_chain_node node;
7684 node.old_die = die;
7685 node.new_die = NULL;
7686 node.parent = NULL;
7688 /* If this type definition is nested inside another type,
7689 and is not an instantiation of a template, always leave
7690 at least a declaration in its place. */
7691 if (die->die_parent != NULL
7692 && is_type_die (die->die_parent)
7693 && !is_template_instantiation (die))
7694 node.new_die = clone_as_declaration (die);
7696 generate_skeleton_bottom_up (&node);
7697 return node.new_die;
7700 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7701 declaration. The original DIE is moved to a new compile unit so that
7702 existing references to it follow it to the new location. If any of the
7703 original DIE's descendants is a declaration, we need to replace the
7704 original DIE with a skeleton tree and move the declarations back into the
7705 skeleton tree. */
7707 static dw_die_ref
7708 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7709 dw_die_ref prev)
7711 dw_die_ref skeleton, orig_parent;
7713 /* Copy the declaration context to the type unit DIE. If the returned
7714 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7715 that DIE. */
7716 orig_parent = copy_declaration_context (unit, child);
7718 skeleton = generate_skeleton (child);
7719 if (skeleton == NULL)
7720 remove_child_with_prev (child, prev);
7721 else
7723 skeleton->comdat_type_p = true;
7724 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7726 /* If the original DIE was a specification, we need to put
7727 the skeleton under the parent DIE of the declaration.
7728 This leaves the original declaration in the tree, but
7729 it will be pruned later since there are no longer any
7730 references to it. */
7731 if (orig_parent != NULL)
7733 remove_child_with_prev (child, prev);
7734 add_child_die (orig_parent, skeleton);
7736 else
7737 replace_child (child, skeleton, prev);
7740 return skeleton;
7743 static void
7744 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7745 comdat_type_node *type_node,
7746 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7748 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7749 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7750 DWARF procedure references in the DW_AT_location attribute. */
7752 static dw_die_ref
7753 copy_dwarf_procedure (dw_die_ref die,
7754 comdat_type_node *type_node,
7755 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7757 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7759 /* DWARF procedures are not supposed to have children... */
7760 gcc_assert (die->die_child == NULL);
7762 /* ... and they are supposed to have only one attribute: DW_AT_location. */
7763 gcc_assert (vec_safe_length (die->die_attr) == 1
7764 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7766 /* Do not copy more than once DWARF procedures. */
7767 bool existed;
7768 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7769 if (existed)
7770 return die_copy;
7772 die_copy = clone_die (die);
7773 add_child_die (type_node->root_die, die_copy);
7774 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7775 return die_copy;
7778 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
7779 procedures in DIE's attributes. */
7781 static void
7782 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7783 comdat_type_node *type_node,
7784 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7786 dw_attr_node *a;
7787 unsigned i;
7789 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7791 dw_loc_descr_ref loc;
7793 if (a->dw_attr_val.val_class != dw_val_class_loc)
7794 continue;
7796 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7798 switch (loc->dw_loc_opc)
7800 case DW_OP_call2:
7801 case DW_OP_call4:
7802 case DW_OP_call_ref:
7803 gcc_assert (loc->dw_loc_oprnd1.val_class
7804 == dw_val_class_die_ref);
7805 loc->dw_loc_oprnd1.v.val_die_ref.die
7806 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7807 type_node,
7808 copied_dwarf_procs);
7810 default:
7811 break;
7817 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7818 rewrite references to point to the copies.
7820 References are looked for in DIE's attributes and recursively in all its
7821 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7822 mapping from old DWARF procedures to their copy. It is used not to copy
7823 twice the same DWARF procedure under TYPE_NODE. */
7825 static void
7826 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7827 comdat_type_node *type_node,
7828 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7830 dw_die_ref c;
7832 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7833 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7834 type_node,
7835 copied_dwarf_procs));
7838 /* Traverse the DIE and set up additional .debug_types sections for each
7839 type worthy of being placed in a COMDAT section. */
7841 static void
7842 break_out_comdat_types (dw_die_ref die)
7844 dw_die_ref c;
7845 dw_die_ref first;
7846 dw_die_ref prev = NULL;
7847 dw_die_ref next = NULL;
7848 dw_die_ref unit = NULL;
7850 first = c = die->die_child;
7851 if (c)
7852 next = c->die_sib;
7853 if (c) do {
7854 if (prev == NULL || prev->die_sib == c)
7855 prev = c;
7856 c = next;
7857 next = (c == first ? NULL : c->die_sib);
7858 if (should_move_die_to_comdat (c))
7860 dw_die_ref replacement;
7861 comdat_type_node *type_node;
7863 /* Break out nested types into their own type units. */
7864 break_out_comdat_types (c);
7866 /* Create a new type unit DIE as the root for the new tree, and
7867 add it to the list of comdat types. */
7868 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7869 add_AT_unsigned (unit, DW_AT_language,
7870 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7871 type_node = ggc_cleared_alloc<comdat_type_node> ();
7872 type_node->root_die = unit;
7873 type_node->next = comdat_type_list;
7874 comdat_type_list = type_node;
7876 /* Generate the type signature. */
7877 generate_type_signature (c, type_node);
7879 /* Copy the declaration context, attributes, and children of the
7880 declaration into the new type unit DIE, then remove this DIE
7881 from the main CU (or replace it with a skeleton if necessary). */
7882 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7883 type_node->skeleton_die = replacement;
7885 /* Add the DIE to the new compunit. */
7886 add_child_die (unit, c);
7888 /* Types can reference DWARF procedures for type size or data location
7889 expressions. Calls in DWARF expressions cannot target procedures
7890 that are not in the same section. So we must copy DWARF procedures
7891 along with this type and then rewrite references to them. */
7892 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7893 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7895 if (replacement != NULL)
7896 c = replacement;
7898 else if (c->die_tag == DW_TAG_namespace
7899 || c->die_tag == DW_TAG_class_type
7900 || c->die_tag == DW_TAG_structure_type
7901 || c->die_tag == DW_TAG_union_type)
7903 /* Look for nested types that can be broken out. */
7904 break_out_comdat_types (c);
7906 } while (next != NULL);
7909 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7910 Enter all the cloned children into the hash table decl_table. */
7912 static dw_die_ref
7913 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7915 dw_die_ref c;
7916 dw_die_ref clone;
7917 struct decl_table_entry *entry;
7918 decl_table_entry **slot;
7920 if (die->die_tag == DW_TAG_subprogram)
7921 clone = clone_as_declaration (die);
7922 else
7923 clone = clone_die (die);
7925 slot = decl_table->find_slot_with_hash (die,
7926 htab_hash_pointer (die), INSERT);
7928 /* Assert that DIE isn't in the hash table yet. If it would be there
7929 before, the ancestors would be necessarily there as well, therefore
7930 clone_tree_partial wouldn't be called. */
7931 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7933 entry = XCNEW (struct decl_table_entry);
7934 entry->orig = die;
7935 entry->copy = clone;
7936 *slot = entry;
7938 if (die->die_tag != DW_TAG_subprogram)
7939 FOR_EACH_CHILD (die, c,
7940 add_child_die (clone, clone_tree_partial (c, decl_table)));
7942 return clone;
7945 /* Walk the DIE and its children, looking for references to incomplete
7946 or trivial types that are unmarked (i.e., that are not in the current
7947 type_unit). */
7949 static void
7950 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7952 dw_die_ref c;
7953 dw_attr_node *a;
7954 unsigned ix;
7956 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7958 if (AT_class (a) == dw_val_class_die_ref)
7960 dw_die_ref targ = AT_ref (a);
7961 decl_table_entry **slot;
7962 struct decl_table_entry *entry;
7964 if (targ->die_mark != 0 || targ->comdat_type_p)
7965 continue;
7967 slot = decl_table->find_slot_with_hash (targ,
7968 htab_hash_pointer (targ),
7969 INSERT);
7971 if (*slot != HTAB_EMPTY_ENTRY)
7973 /* TARG has already been copied, so we just need to
7974 modify the reference to point to the copy. */
7975 entry = *slot;
7976 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7978 else
7980 dw_die_ref parent = unit;
7981 dw_die_ref copy = clone_die (targ);
7983 /* Record in DECL_TABLE that TARG has been copied.
7984 Need to do this now, before the recursive call,
7985 because DECL_TABLE may be expanded and SLOT
7986 would no longer be a valid pointer. */
7987 entry = XCNEW (struct decl_table_entry);
7988 entry->orig = targ;
7989 entry->copy = copy;
7990 *slot = entry;
7992 /* If TARG is not a declaration DIE, we need to copy its
7993 children. */
7994 if (!is_declaration_die (targ))
7996 FOR_EACH_CHILD (
7997 targ, c,
7998 add_child_die (copy,
7999 clone_tree_partial (c, decl_table)));
8002 /* Make sure the cloned tree is marked as part of the
8003 type unit. */
8004 mark_dies (copy);
8006 /* If TARG has surrounding context, copy its ancestor tree
8007 into the new type unit. */
8008 if (targ->die_parent != NULL
8009 && !is_unit_die (targ->die_parent))
8010 parent = copy_ancestor_tree (unit, targ->die_parent,
8011 decl_table);
8013 add_child_die (parent, copy);
8014 a->dw_attr_val.v.val_die_ref.die = copy;
8016 /* Make sure the newly-copied DIE is walked. If it was
8017 installed in a previously-added context, it won't
8018 get visited otherwise. */
8019 if (parent != unit)
8021 /* Find the highest point of the newly-added tree,
8022 mark each node along the way, and walk from there. */
8023 parent->die_mark = 1;
8024 while (parent->die_parent
8025 && parent->die_parent->die_mark == 0)
8027 parent = parent->die_parent;
8028 parent->die_mark = 1;
8030 copy_decls_walk (unit, parent, decl_table);
8036 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8039 /* Copy declarations for "unworthy" types into the new comdat section.
8040 Incomplete types, modified types, and certain other types aren't broken
8041 out into comdat sections of their own, so they don't have a signature,
8042 and we need to copy the declaration into the same section so that we
8043 don't have an external reference. */
8045 static void
8046 copy_decls_for_unworthy_types (dw_die_ref unit)
8048 mark_dies (unit);
8049 decl_hash_type decl_table (10);
8050 copy_decls_walk (unit, unit, &decl_table);
8051 unmark_dies (unit);
8054 /* Traverse the DIE and add a sibling attribute if it may have the
8055 effect of speeding up access to siblings. To save some space,
8056 avoid generating sibling attributes for DIE's without children. */
8058 static void
8059 add_sibling_attributes (dw_die_ref die)
8061 dw_die_ref c;
8063 if (! die->die_child)
8064 return;
8066 if (die->die_parent && die != die->die_parent->die_child)
8067 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8069 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8072 /* Output all location lists for the DIE and its children. */
8074 static void
8075 output_location_lists (dw_die_ref die)
8077 dw_die_ref c;
8078 dw_attr_node *a;
8079 unsigned ix;
8081 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8082 if (AT_class (a) == dw_val_class_loc_list)
8083 output_loc_list (AT_loc_list (a));
8085 FOR_EACH_CHILD (die, c, output_location_lists (c));
8088 /* We want to limit the number of external references, because they are
8089 larger than local references: a relocation takes multiple words, and
8090 even a sig8 reference is always eight bytes, whereas a local reference
8091 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8092 So if we encounter multiple external references to the same type DIE, we
8093 make a local typedef stub for it and redirect all references there.
8095 This is the element of the hash table for keeping track of these
8096 references. */
8098 struct external_ref
8100 dw_die_ref type;
8101 dw_die_ref stub;
8102 unsigned n_refs;
8105 /* Hashtable helpers. */
8107 struct external_ref_hasher : free_ptr_hash <external_ref>
8109 static inline hashval_t hash (const external_ref *);
8110 static inline bool equal (const external_ref *, const external_ref *);
8113 inline hashval_t
8114 external_ref_hasher::hash (const external_ref *r)
8116 dw_die_ref die = r->type;
8117 hashval_t h = 0;
8119 /* We can't use the address of the DIE for hashing, because
8120 that will make the order of the stub DIEs non-deterministic. */
8121 if (! die->comdat_type_p)
8122 /* We have a symbol; use it to compute a hash. */
8123 h = htab_hash_string (die->die_id.die_symbol);
8124 else
8126 /* We have a type signature; use a subset of the bits as the hash.
8127 The 8-byte signature is at least as large as hashval_t. */
8128 comdat_type_node *type_node = die->die_id.die_type_node;
8129 memcpy (&h, type_node->signature, sizeof (h));
8131 return h;
8134 inline bool
8135 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8137 return r1->type == r2->type;
8140 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8142 /* Return a pointer to the external_ref for references to DIE. */
8144 static struct external_ref *
8145 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8147 struct external_ref ref, *ref_p;
8148 external_ref **slot;
8150 ref.type = die;
8151 slot = map->find_slot (&ref, INSERT);
8152 if (*slot != HTAB_EMPTY_ENTRY)
8153 return *slot;
8155 ref_p = XCNEW (struct external_ref);
8156 ref_p->type = die;
8157 *slot = ref_p;
8158 return ref_p;
8161 /* Subroutine of optimize_external_refs, below.
8163 If we see a type skeleton, record it as our stub. If we see external
8164 references, remember how many we've seen. */
8166 static void
8167 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8169 dw_die_ref c;
8170 dw_attr_node *a;
8171 unsigned ix;
8172 struct external_ref *ref_p;
8174 if (is_type_die (die)
8175 && (c = get_AT_ref (die, DW_AT_signature)))
8177 /* This is a local skeleton; use it for local references. */
8178 ref_p = lookup_external_ref (map, c);
8179 ref_p->stub = die;
8182 /* Scan the DIE references, and remember any that refer to DIEs from
8183 other CUs (i.e. those which are not marked). */
8184 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8185 if (AT_class (a) == dw_val_class_die_ref
8186 && (c = AT_ref (a))->die_mark == 0
8187 && is_type_die (c))
8189 ref_p = lookup_external_ref (map, c);
8190 ref_p->n_refs++;
8193 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8196 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8197 points to an external_ref, DATA is the CU we're processing. If we don't
8198 already have a local stub, and we have multiple refs, build a stub. */
8201 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8203 struct external_ref *ref_p = *slot;
8205 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8207 /* We have multiple references to this type, so build a small stub.
8208 Both of these forms are a bit dodgy from the perspective of the
8209 DWARF standard, since technically they should have names. */
8210 dw_die_ref cu = data;
8211 dw_die_ref type = ref_p->type;
8212 dw_die_ref stub = NULL;
8214 if (type->comdat_type_p)
8216 /* If we refer to this type via sig8, use AT_signature. */
8217 stub = new_die (type->die_tag, cu, NULL_TREE);
8218 add_AT_die_ref (stub, DW_AT_signature, type);
8220 else
8222 /* Otherwise, use a typedef with no name. */
8223 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8224 add_AT_die_ref (stub, DW_AT_type, type);
8227 stub->die_mark++;
8228 ref_p->stub = stub;
8230 return 1;
8233 /* DIE is a unit; look through all the DIE references to see if there are
8234 any external references to types, and if so, create local stubs for
8235 them which will be applied in build_abbrev_table. This is useful because
8236 references to local DIEs are smaller. */
8238 static external_ref_hash_type *
8239 optimize_external_refs (dw_die_ref die)
8241 external_ref_hash_type *map = new external_ref_hash_type (10);
8242 optimize_external_refs_1 (die, map);
8243 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8244 return map;
8247 /* The format of each DIE (and its attribute value pairs) is encoded in an
8248 abbreviation table. This routine builds the abbreviation table and assigns
8249 a unique abbreviation id for each abbreviation entry. The children of each
8250 die are visited recursively. */
8252 static void
8253 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8255 unsigned long abbrev_id;
8256 unsigned int n_alloc;
8257 dw_die_ref c;
8258 dw_attr_node *a;
8259 unsigned ix;
8261 /* Scan the DIE references, and replace any that refer to
8262 DIEs from other CUs (i.e. those which are not marked) with
8263 the local stubs we built in optimize_external_refs. */
8264 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8265 if (AT_class (a) == dw_val_class_die_ref
8266 && (c = AT_ref (a))->die_mark == 0)
8268 struct external_ref *ref_p;
8269 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8271 ref_p = lookup_external_ref (extern_map, c);
8272 if (ref_p->stub && ref_p->stub != die)
8273 change_AT_die_ref (a, ref_p->stub);
8274 else
8275 /* We aren't changing this reference, so mark it external. */
8276 set_AT_ref_external (a, 1);
8279 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8281 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8282 dw_attr_node *die_a, *abbrev_a;
8283 unsigned ix;
8284 bool ok = true;
8286 if (abbrev->die_tag != die->die_tag)
8287 continue;
8288 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8289 continue;
8291 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8292 continue;
8294 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8296 abbrev_a = &(*abbrev->die_attr)[ix];
8297 if ((abbrev_a->dw_attr != die_a->dw_attr)
8298 || (value_format (abbrev_a) != value_format (die_a)))
8300 ok = false;
8301 break;
8304 if (ok)
8305 break;
8308 if (abbrev_id >= abbrev_die_table_in_use)
8310 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8312 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8313 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8314 n_alloc);
8316 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8317 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8318 abbrev_die_table_allocated = n_alloc;
8321 ++abbrev_die_table_in_use;
8322 abbrev_die_table[abbrev_id] = die;
8325 die->die_abbrev = abbrev_id;
8326 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8329 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8331 static int
8332 constant_size (unsigned HOST_WIDE_INT value)
8334 int log;
8336 if (value == 0)
8337 log = 0;
8338 else
8339 log = floor_log2 (value);
8341 log = log / 8;
8342 log = 1 << (floor_log2 (log) + 1);
8344 return log;
8347 /* Return the size of a DIE as it is represented in the
8348 .debug_info section. */
8350 static unsigned long
8351 size_of_die (dw_die_ref die)
8353 unsigned long size = 0;
8354 dw_attr_node *a;
8355 unsigned ix;
8356 enum dwarf_form form;
8358 size += size_of_uleb128 (die->die_abbrev);
8359 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8361 switch (AT_class (a))
8363 case dw_val_class_addr:
8364 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8366 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8367 size += size_of_uleb128 (AT_index (a));
8369 else
8370 size += DWARF2_ADDR_SIZE;
8371 break;
8372 case dw_val_class_offset:
8373 size += DWARF_OFFSET_SIZE;
8374 break;
8375 case dw_val_class_loc:
8377 unsigned long lsize = size_of_locs (AT_loc (a));
8379 /* Block length. */
8380 if (dwarf_version >= 4)
8381 size += size_of_uleb128 (lsize);
8382 else
8383 size += constant_size (lsize);
8384 size += lsize;
8386 break;
8387 case dw_val_class_loc_list:
8388 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8390 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8391 size += size_of_uleb128 (AT_index (a));
8393 else
8394 size += DWARF_OFFSET_SIZE;
8395 break;
8396 case dw_val_class_range_list:
8397 size += DWARF_OFFSET_SIZE;
8398 break;
8399 case dw_val_class_const:
8400 size += size_of_sleb128 (AT_int (a));
8401 break;
8402 case dw_val_class_unsigned_const:
8404 int csize = constant_size (AT_unsigned (a));
8405 if (dwarf_version == 3
8406 && a->dw_attr == DW_AT_data_member_location
8407 && csize >= 4)
8408 size += size_of_uleb128 (AT_unsigned (a));
8409 else
8410 size += csize;
8412 break;
8413 case dw_val_class_const_double:
8414 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8415 if (HOST_BITS_PER_WIDE_INT >= 64)
8416 size++; /* block */
8417 break;
8418 case dw_val_class_wide_int:
8419 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8420 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8421 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8422 > 64)
8423 size++; /* block */
8424 break;
8425 case dw_val_class_vec:
8426 size += constant_size (a->dw_attr_val.v.val_vec.length
8427 * a->dw_attr_val.v.val_vec.elt_size)
8428 + a->dw_attr_val.v.val_vec.length
8429 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8430 break;
8431 case dw_val_class_flag:
8432 if (dwarf_version >= 4)
8433 /* Currently all add_AT_flag calls pass in 1 as last argument,
8434 so DW_FORM_flag_present can be used. If that ever changes,
8435 we'll need to use DW_FORM_flag and have some optimization
8436 in build_abbrev_table that will change those to
8437 DW_FORM_flag_present if it is set to 1 in all DIEs using
8438 the same abbrev entry. */
8439 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8440 else
8441 size += 1;
8442 break;
8443 case dw_val_class_die_ref:
8444 if (AT_ref_external (a))
8446 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8447 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8448 is sized by target address length, whereas in DWARF3
8449 it's always sized as an offset. */
8450 if (use_debug_types)
8451 size += DWARF_TYPE_SIGNATURE_SIZE;
8452 else if (dwarf_version == 2)
8453 size += DWARF2_ADDR_SIZE;
8454 else
8455 size += DWARF_OFFSET_SIZE;
8457 else
8458 size += DWARF_OFFSET_SIZE;
8459 break;
8460 case dw_val_class_fde_ref:
8461 size += DWARF_OFFSET_SIZE;
8462 break;
8463 case dw_val_class_lbl_id:
8464 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8466 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8467 size += size_of_uleb128 (AT_index (a));
8469 else
8470 size += DWARF2_ADDR_SIZE;
8471 break;
8472 case dw_val_class_lineptr:
8473 case dw_val_class_macptr:
8474 size += DWARF_OFFSET_SIZE;
8475 break;
8476 case dw_val_class_str:
8477 form = AT_string_form (a);
8478 if (form == DW_FORM_strp)
8479 size += DWARF_OFFSET_SIZE;
8480 else if (form == DW_FORM_GNU_str_index)
8481 size += size_of_uleb128 (AT_index (a));
8482 else
8483 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8484 break;
8485 case dw_val_class_file:
8486 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8487 break;
8488 case dw_val_class_data8:
8489 size += 8;
8490 break;
8491 case dw_val_class_vms_delta:
8492 size += DWARF_OFFSET_SIZE;
8493 break;
8494 case dw_val_class_high_pc:
8495 size += DWARF2_ADDR_SIZE;
8496 break;
8497 case dw_val_class_discr_value:
8498 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8499 break;
8500 case dw_val_class_discr_list:
8502 unsigned block_size = size_of_discr_list (AT_discr_list (a));
8504 /* This is a block, so we have the block length and then its
8505 data. */
8506 size += constant_size (block_size) + block_size;
8508 break;
8509 default:
8510 gcc_unreachable ();
8514 return size;
8517 /* Size the debugging information associated with a given DIE. Visits the
8518 DIE's children recursively. Updates the global variable next_die_offset, on
8519 each time through. Uses the current value of next_die_offset to update the
8520 die_offset field in each DIE. */
8522 static void
8523 calc_die_sizes (dw_die_ref die)
8525 dw_die_ref c;
8527 gcc_assert (die->die_offset == 0
8528 || (unsigned long int) die->die_offset == next_die_offset);
8529 die->die_offset = next_die_offset;
8530 next_die_offset += size_of_die (die);
8532 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8534 if (die->die_child != NULL)
8535 /* Count the null byte used to terminate sibling lists. */
8536 next_die_offset += 1;
8539 /* Size just the base type children at the start of the CU.
8540 This is needed because build_abbrev needs to size locs
8541 and sizing of type based stack ops needs to know die_offset
8542 values for the base types. */
8544 static void
8545 calc_base_type_die_sizes (void)
8547 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8548 unsigned int i;
8549 dw_die_ref base_type;
8550 #if ENABLE_ASSERT_CHECKING
8551 dw_die_ref prev = comp_unit_die ()->die_child;
8552 #endif
8554 die_offset += size_of_die (comp_unit_die ());
8555 for (i = 0; base_types.iterate (i, &base_type); i++)
8557 #if ENABLE_ASSERT_CHECKING
8558 gcc_assert (base_type->die_offset == 0
8559 && prev->die_sib == base_type
8560 && base_type->die_child == NULL
8561 && base_type->die_abbrev);
8562 prev = base_type;
8563 #endif
8564 base_type->die_offset = die_offset;
8565 die_offset += size_of_die (base_type);
8569 /* Set the marks for a die and its children. We do this so
8570 that we know whether or not a reference needs to use FORM_ref_addr; only
8571 DIEs in the same CU will be marked. We used to clear out the offset
8572 and use that as the flag, but ran into ordering problems. */
8574 static void
8575 mark_dies (dw_die_ref die)
8577 dw_die_ref c;
8579 gcc_assert (!die->die_mark);
8581 die->die_mark = 1;
8582 FOR_EACH_CHILD (die, c, mark_dies (c));
8585 /* Clear the marks for a die and its children. */
8587 static void
8588 unmark_dies (dw_die_ref die)
8590 dw_die_ref c;
8592 if (! use_debug_types)
8593 gcc_assert (die->die_mark);
8595 die->die_mark = 0;
8596 FOR_EACH_CHILD (die, c, unmark_dies (c));
8599 /* Clear the marks for a die, its children and referred dies. */
8601 static void
8602 unmark_all_dies (dw_die_ref die)
8604 dw_die_ref c;
8605 dw_attr_node *a;
8606 unsigned ix;
8608 if (!die->die_mark)
8609 return;
8610 die->die_mark = 0;
8612 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8614 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8615 if (AT_class (a) == dw_val_class_die_ref)
8616 unmark_all_dies (AT_ref (a));
8619 /* Calculate if the entry should appear in the final output file. It may be
8620 from a pruned a type. */
8622 static bool
8623 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8625 /* By limiting gnu pubnames to definitions only, gold can generate a
8626 gdb index without entries for declarations, which don't include
8627 enough information to be useful. */
8628 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8629 return false;
8631 if (table == pubname_table)
8633 /* Enumerator names are part of the pubname table, but the
8634 parent DW_TAG_enumeration_type die may have been pruned.
8635 Don't output them if that is the case. */
8636 if (p->die->die_tag == DW_TAG_enumerator &&
8637 (p->die->die_parent == NULL
8638 || !p->die->die_parent->die_perennial_p))
8639 return false;
8641 /* Everything else in the pubname table is included. */
8642 return true;
8645 /* The pubtypes table shouldn't include types that have been
8646 pruned. */
8647 return (p->die->die_offset != 0
8648 || !flag_eliminate_unused_debug_types);
8651 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8652 generated for the compilation unit. */
8654 static unsigned long
8655 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8657 unsigned long size;
8658 unsigned i;
8659 pubname_entry *p;
8660 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8662 size = DWARF_PUBNAMES_HEADER_SIZE;
8663 FOR_EACH_VEC_ELT (*names, i, p)
8664 if (include_pubname_in_output (names, p))
8665 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8667 size += DWARF_OFFSET_SIZE;
8668 return size;
8671 /* Return the size of the information in the .debug_aranges section. */
8673 static unsigned long
8674 size_of_aranges (void)
8676 unsigned long size;
8678 size = DWARF_ARANGES_HEADER_SIZE;
8680 /* Count the address/length pair for this compilation unit. */
8681 if (text_section_used)
8682 size += 2 * DWARF2_ADDR_SIZE;
8683 if (cold_text_section_used)
8684 size += 2 * DWARF2_ADDR_SIZE;
8685 if (have_multiple_function_sections)
8687 unsigned fde_idx;
8688 dw_fde_ref fde;
8690 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8692 if (DECL_IGNORED_P (fde->decl))
8693 continue;
8694 if (!fde->in_std_section)
8695 size += 2 * DWARF2_ADDR_SIZE;
8696 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8697 size += 2 * DWARF2_ADDR_SIZE;
8701 /* Count the two zero words used to terminated the address range table. */
8702 size += 2 * DWARF2_ADDR_SIZE;
8703 return size;
8706 /* Select the encoding of an attribute value. */
8708 static enum dwarf_form
8709 value_format (dw_attr_node *a)
8711 switch (AT_class (a))
8713 case dw_val_class_addr:
8714 /* Only very few attributes allow DW_FORM_addr. */
8715 switch (a->dw_attr)
8717 case DW_AT_low_pc:
8718 case DW_AT_high_pc:
8719 case DW_AT_entry_pc:
8720 case DW_AT_trampoline:
8721 return (AT_index (a) == NOT_INDEXED
8722 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8723 default:
8724 break;
8726 switch (DWARF2_ADDR_SIZE)
8728 case 1:
8729 return DW_FORM_data1;
8730 case 2:
8731 return DW_FORM_data2;
8732 case 4:
8733 return DW_FORM_data4;
8734 case 8:
8735 return DW_FORM_data8;
8736 default:
8737 gcc_unreachable ();
8739 case dw_val_class_range_list:
8740 case dw_val_class_loc_list:
8741 if (dwarf_version >= 4)
8742 return DW_FORM_sec_offset;
8743 /* FALLTHRU */
8744 case dw_val_class_vms_delta:
8745 case dw_val_class_offset:
8746 switch (DWARF_OFFSET_SIZE)
8748 case 4:
8749 return DW_FORM_data4;
8750 case 8:
8751 return DW_FORM_data8;
8752 default:
8753 gcc_unreachable ();
8755 case dw_val_class_loc:
8756 if (dwarf_version >= 4)
8757 return DW_FORM_exprloc;
8758 switch (constant_size (size_of_locs (AT_loc (a))))
8760 case 1:
8761 return DW_FORM_block1;
8762 case 2:
8763 return DW_FORM_block2;
8764 case 4:
8765 return DW_FORM_block4;
8766 default:
8767 gcc_unreachable ();
8769 case dw_val_class_const:
8770 return DW_FORM_sdata;
8771 case dw_val_class_unsigned_const:
8772 switch (constant_size (AT_unsigned (a)))
8774 case 1:
8775 return DW_FORM_data1;
8776 case 2:
8777 return DW_FORM_data2;
8778 case 4:
8779 /* In DWARF3 DW_AT_data_member_location with
8780 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8781 constant, so we need to use DW_FORM_udata if we need
8782 a large constant. */
8783 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8784 return DW_FORM_udata;
8785 return DW_FORM_data4;
8786 case 8:
8787 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8788 return DW_FORM_udata;
8789 return DW_FORM_data8;
8790 default:
8791 gcc_unreachable ();
8793 case dw_val_class_const_double:
8794 switch (HOST_BITS_PER_WIDE_INT)
8796 case 8:
8797 return DW_FORM_data2;
8798 case 16:
8799 return DW_FORM_data4;
8800 case 32:
8801 return DW_FORM_data8;
8802 case 64:
8803 default:
8804 return DW_FORM_block1;
8806 case dw_val_class_wide_int:
8807 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8809 case 8:
8810 return DW_FORM_data1;
8811 case 16:
8812 return DW_FORM_data2;
8813 case 32:
8814 return DW_FORM_data4;
8815 case 64:
8816 return DW_FORM_data8;
8817 default:
8818 return DW_FORM_block1;
8820 case dw_val_class_vec:
8821 switch (constant_size (a->dw_attr_val.v.val_vec.length
8822 * a->dw_attr_val.v.val_vec.elt_size))
8824 case 1:
8825 return DW_FORM_block1;
8826 case 2:
8827 return DW_FORM_block2;
8828 case 4:
8829 return DW_FORM_block4;
8830 default:
8831 gcc_unreachable ();
8833 case dw_val_class_flag:
8834 if (dwarf_version >= 4)
8836 /* Currently all add_AT_flag calls pass in 1 as last argument,
8837 so DW_FORM_flag_present can be used. If that ever changes,
8838 we'll need to use DW_FORM_flag and have some optimization
8839 in build_abbrev_table that will change those to
8840 DW_FORM_flag_present if it is set to 1 in all DIEs using
8841 the same abbrev entry. */
8842 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8843 return DW_FORM_flag_present;
8845 return DW_FORM_flag;
8846 case dw_val_class_die_ref:
8847 if (AT_ref_external (a))
8848 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8849 else
8850 return DW_FORM_ref;
8851 case dw_val_class_fde_ref:
8852 return DW_FORM_data;
8853 case dw_val_class_lbl_id:
8854 return (AT_index (a) == NOT_INDEXED
8855 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8856 case dw_val_class_lineptr:
8857 case dw_val_class_macptr:
8858 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8859 case dw_val_class_str:
8860 return AT_string_form (a);
8861 case dw_val_class_file:
8862 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8864 case 1:
8865 return DW_FORM_data1;
8866 case 2:
8867 return DW_FORM_data2;
8868 case 4:
8869 return DW_FORM_data4;
8870 default:
8871 gcc_unreachable ();
8874 case dw_val_class_data8:
8875 return DW_FORM_data8;
8877 case dw_val_class_high_pc:
8878 switch (DWARF2_ADDR_SIZE)
8880 case 1:
8881 return DW_FORM_data1;
8882 case 2:
8883 return DW_FORM_data2;
8884 case 4:
8885 return DW_FORM_data4;
8886 case 8:
8887 return DW_FORM_data8;
8888 default:
8889 gcc_unreachable ();
8892 case dw_val_class_discr_value:
8893 return (a->dw_attr_val.v.val_discr_value.pos
8894 ? DW_FORM_udata
8895 : DW_FORM_sdata);
8896 case dw_val_class_discr_list:
8897 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8899 case 1:
8900 return DW_FORM_block1;
8901 case 2:
8902 return DW_FORM_block2;
8903 case 4:
8904 return DW_FORM_block4;
8905 default:
8906 gcc_unreachable ();
8909 default:
8910 gcc_unreachable ();
8914 /* Output the encoding of an attribute value. */
8916 static void
8917 output_value_format (dw_attr_node *a)
8919 enum dwarf_form form = value_format (a);
8921 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8924 /* Given a die and id, produce the appropriate abbreviations. */
8926 static void
8927 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8929 unsigned ix;
8930 dw_attr_node *a_attr;
8932 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8933 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8934 dwarf_tag_name (abbrev->die_tag));
8936 if (abbrev->die_child != NULL)
8937 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8938 else
8939 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8941 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8943 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8944 dwarf_attr_name (a_attr->dw_attr));
8945 output_value_format (a_attr);
8948 dw2_asm_output_data (1, 0, NULL);
8949 dw2_asm_output_data (1, 0, NULL);
8953 /* Output the .debug_abbrev section which defines the DIE abbreviation
8954 table. */
8956 static void
8957 output_abbrev_section (void)
8959 unsigned long abbrev_id;
8961 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8962 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8964 /* Terminate the table. */
8965 dw2_asm_output_data (1, 0, NULL);
8968 /* Output a symbol we can use to refer to this DIE from another CU. */
8970 static inline void
8971 output_die_symbol (dw_die_ref die)
8973 const char *sym = die->die_id.die_symbol;
8975 gcc_assert (!die->comdat_type_p);
8977 if (sym == 0)
8978 return;
8980 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8981 /* We make these global, not weak; if the target doesn't support
8982 .linkonce, it doesn't support combining the sections, so debugging
8983 will break. */
8984 targetm.asm_out.globalize_label (asm_out_file, sym);
8986 ASM_OUTPUT_LABEL (asm_out_file, sym);
8989 /* Return a new location list, given the begin and end range, and the
8990 expression. */
8992 static inline dw_loc_list_ref
8993 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8994 const char *section)
8996 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8998 retlist->begin = begin;
8999 retlist->begin_entry = NULL;
9000 retlist->end = end;
9001 retlist->expr = expr;
9002 retlist->section = section;
9004 return retlist;
9007 /* Generate a new internal symbol for this location list node, if it
9008 hasn't got one yet. */
9010 static inline void
9011 gen_llsym (dw_loc_list_ref list)
9013 gcc_assert (!list->ll_symbol);
9014 list->ll_symbol = gen_internal_sym ("LLST");
9017 /* Output the location list given to us. */
9019 static void
9020 output_loc_list (dw_loc_list_ref list_head)
9022 dw_loc_list_ref curr = list_head;
9024 if (list_head->emitted)
9025 return;
9026 list_head->emitted = true;
9028 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9030 /* Walk the location list, and output each range + expression. */
9031 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9033 unsigned long size;
9034 /* Don't output an entry that starts and ends at the same address. */
9035 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9036 continue;
9037 size = size_of_locs (curr->expr);
9038 /* If the expression is too large, drop it on the floor. We could
9039 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9040 in the expression, but >= 64KB expressions for a single value
9041 in a single range are unlikely very useful. */
9042 if (size > 0xffff)
9043 continue;
9044 if (dwarf_split_debug_info)
9046 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9047 "Location list start/length entry (%s)",
9048 list_head->ll_symbol);
9049 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9050 "Location list range start index (%s)",
9051 curr->begin);
9052 /* The length field is 4 bytes. If we ever need to support
9053 an 8-byte length, we can add a new DW_LLE code or fall back
9054 to DW_LLE_GNU_start_end_entry. */
9055 dw2_asm_output_delta (4, curr->end, curr->begin,
9056 "Location list range length (%s)",
9057 list_head->ll_symbol);
9059 else if (!have_multiple_function_sections)
9061 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9062 "Location list begin address (%s)",
9063 list_head->ll_symbol);
9064 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9065 "Location list end address (%s)",
9066 list_head->ll_symbol);
9068 else
9070 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9071 "Location list begin address (%s)",
9072 list_head->ll_symbol);
9073 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9074 "Location list end address (%s)",
9075 list_head->ll_symbol);
9078 /* Output the block length for this list of location operations. */
9079 gcc_assert (size <= 0xffff);
9080 dw2_asm_output_data (2, size, "%s", "Location expression size");
9082 output_loc_sequence (curr->expr, -1);
9085 if (dwarf_split_debug_info)
9086 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9087 "Location list terminator (%s)",
9088 list_head->ll_symbol);
9089 else
9091 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9092 "Location list terminator begin (%s)",
9093 list_head->ll_symbol);
9094 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9095 "Location list terminator end (%s)",
9096 list_head->ll_symbol);
9100 /* Output a range_list offset into the debug_range section. Emit a
9101 relocated reference if val_entry is NULL, otherwise, emit an
9102 indirect reference. */
9104 static void
9105 output_range_list_offset (dw_attr_node *a)
9107 const char *name = dwarf_attr_name (a->dw_attr);
9109 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9111 char *p = strchr (ranges_section_label, '\0');
9112 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9113 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9114 debug_ranges_section, "%s", name);
9115 *p = '\0';
9117 else
9118 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9119 "%s (offset from %s)", name, ranges_section_label);
9122 /* Output the offset into the debug_loc section. */
9124 static void
9125 output_loc_list_offset (dw_attr_node *a)
9127 char *sym = AT_loc_list (a)->ll_symbol;
9129 gcc_assert (sym);
9130 if (dwarf_split_debug_info)
9131 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9132 "%s", dwarf_attr_name (a->dw_attr));
9133 else
9134 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9135 "%s", dwarf_attr_name (a->dw_attr));
9138 /* Output an attribute's index or value appropriately. */
9140 static void
9141 output_attr_index_or_value (dw_attr_node *a)
9143 const char *name = dwarf_attr_name (a->dw_attr);
9145 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9147 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9148 return;
9150 switch (AT_class (a))
9152 case dw_val_class_addr:
9153 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9154 break;
9155 case dw_val_class_high_pc:
9156 case dw_val_class_lbl_id:
9157 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9158 break;
9159 case dw_val_class_loc_list:
9160 output_loc_list_offset (a);
9161 break;
9162 default:
9163 gcc_unreachable ();
9167 /* Output a type signature. */
9169 static inline void
9170 output_signature (const char *sig, const char *name)
9172 int i;
9174 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9175 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9178 /* Output a discriminant value. */
9180 static inline void
9181 output_discr_value (dw_discr_value *discr_value, const char *name)
9183 if (discr_value->pos)
9184 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9185 else
9186 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9189 /* Output the DIE and its attributes. Called recursively to generate
9190 the definitions of each child DIE. */
9192 static void
9193 output_die (dw_die_ref die)
9195 dw_attr_node *a;
9196 dw_die_ref c;
9197 unsigned long size;
9198 unsigned ix;
9200 /* If someone in another CU might refer to us, set up a symbol for
9201 them to point to. */
9202 if (! die->comdat_type_p && die->die_id.die_symbol)
9203 output_die_symbol (die);
9205 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9206 (unsigned long)die->die_offset,
9207 dwarf_tag_name (die->die_tag));
9209 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9211 const char *name = dwarf_attr_name (a->dw_attr);
9213 switch (AT_class (a))
9215 case dw_val_class_addr:
9216 output_attr_index_or_value (a);
9217 break;
9219 case dw_val_class_offset:
9220 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9221 "%s", name);
9222 break;
9224 case dw_val_class_range_list:
9225 output_range_list_offset (a);
9226 break;
9228 case dw_val_class_loc:
9229 size = size_of_locs (AT_loc (a));
9231 /* Output the block length for this list of location operations. */
9232 if (dwarf_version >= 4)
9233 dw2_asm_output_data_uleb128 (size, "%s", name);
9234 else
9235 dw2_asm_output_data (constant_size (size), size, "%s", name);
9237 output_loc_sequence (AT_loc (a), -1);
9238 break;
9240 case dw_val_class_const:
9241 /* ??? It would be slightly more efficient to use a scheme like is
9242 used for unsigned constants below, but gdb 4.x does not sign
9243 extend. Gdb 5.x does sign extend. */
9244 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9245 break;
9247 case dw_val_class_unsigned_const:
9249 int csize = constant_size (AT_unsigned (a));
9250 if (dwarf_version == 3
9251 && a->dw_attr == DW_AT_data_member_location
9252 && csize >= 4)
9253 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9254 else
9255 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9257 break;
9259 case dw_val_class_const_double:
9261 unsigned HOST_WIDE_INT first, second;
9263 if (HOST_BITS_PER_WIDE_INT >= 64)
9264 dw2_asm_output_data (1,
9265 HOST_BITS_PER_DOUBLE_INT
9266 / HOST_BITS_PER_CHAR,
9267 NULL);
9269 if (WORDS_BIG_ENDIAN)
9271 first = a->dw_attr_val.v.val_double.high;
9272 second = a->dw_attr_val.v.val_double.low;
9274 else
9276 first = a->dw_attr_val.v.val_double.low;
9277 second = a->dw_attr_val.v.val_double.high;
9280 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9281 first, "%s", name);
9282 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9283 second, NULL);
9285 break;
9287 case dw_val_class_wide_int:
9289 int i;
9290 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9291 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9292 if (len * HOST_BITS_PER_WIDE_INT > 64)
9293 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9294 NULL);
9296 if (WORDS_BIG_ENDIAN)
9297 for (i = len - 1; i >= 0; --i)
9299 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9300 "%s", name);
9301 name = "";
9303 else
9304 for (i = 0; i < len; ++i)
9306 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9307 "%s", name);
9308 name = "";
9311 break;
9313 case dw_val_class_vec:
9315 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9316 unsigned int len = a->dw_attr_val.v.val_vec.length;
9317 unsigned int i;
9318 unsigned char *p;
9320 dw2_asm_output_data (constant_size (len * elt_size),
9321 len * elt_size, "%s", name);
9322 if (elt_size > sizeof (HOST_WIDE_INT))
9324 elt_size /= 2;
9325 len *= 2;
9327 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9328 i < len;
9329 i++, p += elt_size)
9330 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9331 "fp or vector constant word %u", i);
9332 break;
9335 case dw_val_class_flag:
9336 if (dwarf_version >= 4)
9338 /* Currently all add_AT_flag calls pass in 1 as last argument,
9339 so DW_FORM_flag_present can be used. If that ever changes,
9340 we'll need to use DW_FORM_flag and have some optimization
9341 in build_abbrev_table that will change those to
9342 DW_FORM_flag_present if it is set to 1 in all DIEs using
9343 the same abbrev entry. */
9344 gcc_assert (AT_flag (a) == 1);
9345 if (flag_debug_asm)
9346 fprintf (asm_out_file, "\t\t\t%s %s\n",
9347 ASM_COMMENT_START, name);
9348 break;
9350 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9351 break;
9353 case dw_val_class_loc_list:
9354 output_attr_index_or_value (a);
9355 break;
9357 case dw_val_class_die_ref:
9358 if (AT_ref_external (a))
9360 if (AT_ref (a)->comdat_type_p)
9362 comdat_type_node *type_node =
9363 AT_ref (a)->die_id.die_type_node;
9365 gcc_assert (type_node);
9366 output_signature (type_node->signature, name);
9368 else
9370 const char *sym = AT_ref (a)->die_id.die_symbol;
9371 int size;
9373 gcc_assert (sym);
9374 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9375 length, whereas in DWARF3 it's always sized as an
9376 offset. */
9377 if (dwarf_version == 2)
9378 size = DWARF2_ADDR_SIZE;
9379 else
9380 size = DWARF_OFFSET_SIZE;
9381 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9382 name);
9385 else
9387 gcc_assert (AT_ref (a)->die_offset);
9388 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9389 "%s", name);
9391 break;
9393 case dw_val_class_fde_ref:
9395 char l1[20];
9397 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9398 a->dw_attr_val.v.val_fde_index * 2);
9399 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9400 "%s", name);
9402 break;
9404 case dw_val_class_vms_delta:
9405 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9406 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9407 AT_vms_delta2 (a), AT_vms_delta1 (a),
9408 "%s", name);
9409 #else
9410 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9411 AT_vms_delta2 (a), AT_vms_delta1 (a),
9412 "%s", name);
9413 #endif
9414 break;
9416 case dw_val_class_lbl_id:
9417 output_attr_index_or_value (a);
9418 break;
9420 case dw_val_class_lineptr:
9421 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9422 debug_line_section, "%s", name);
9423 break;
9425 case dw_val_class_macptr:
9426 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9427 debug_macinfo_section, "%s", name);
9428 break;
9430 case dw_val_class_str:
9431 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9432 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9433 a->dw_attr_val.v.val_str->label,
9434 debug_str_section,
9435 "%s: \"%s\"", name, AT_string (a));
9436 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9437 dw2_asm_output_data_uleb128 (AT_index (a),
9438 "%s: \"%s\"", name, AT_string (a));
9439 else
9440 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9441 break;
9443 case dw_val_class_file:
9445 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9447 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9448 a->dw_attr_val.v.val_file->filename);
9449 break;
9452 case dw_val_class_data8:
9454 int i;
9456 for (i = 0; i < 8; i++)
9457 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9458 i == 0 ? "%s" : NULL, name);
9459 break;
9462 case dw_val_class_high_pc:
9463 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9464 get_AT_low_pc (die), "DW_AT_high_pc");
9465 break;
9467 case dw_val_class_discr_value:
9468 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9469 break;
9471 case dw_val_class_discr_list:
9473 dw_discr_list_ref list = AT_discr_list (a);
9474 const int size = size_of_discr_list (list);
9476 /* This is a block, so output its length first. */
9477 dw2_asm_output_data (constant_size (size), size,
9478 "%s: block size", name);
9480 for (; list != NULL; list = list->dw_discr_next)
9482 /* One byte for the discriminant value descriptor, and then as
9483 many LEB128 numbers as required. */
9484 if (list->dw_discr_range)
9485 dw2_asm_output_data (1, DW_DSC_range,
9486 "%s: DW_DSC_range", name);
9487 else
9488 dw2_asm_output_data (1, DW_DSC_label,
9489 "%s: DW_DSC_label", name);
9491 output_discr_value (&list->dw_discr_lower_bound, name);
9492 if (list->dw_discr_range)
9493 output_discr_value (&list->dw_discr_upper_bound, name);
9495 break;
9498 default:
9499 gcc_unreachable ();
9503 FOR_EACH_CHILD (die, c, output_die (c));
9505 /* Add null byte to terminate sibling list. */
9506 if (die->die_child != NULL)
9507 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9508 (unsigned long) die->die_offset);
9511 /* Output the compilation unit that appears at the beginning of the
9512 .debug_info section, and precedes the DIE descriptions. */
9514 static void
9515 output_compilation_unit_header (void)
9517 /* We don't support actual DWARFv5 units yet, we just use some
9518 DWARFv5 draft DIE tags in DWARFv4 format. */
9519 int ver = dwarf_version < 5 ? dwarf_version : 4;
9521 if (!XCOFF_DEBUGGING_INFO)
9523 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9524 dw2_asm_output_data (4, 0xffffffff,
9525 "Initial length escape value indicating 64-bit DWARF extension");
9526 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9527 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9528 "Length of Compilation Unit Info");
9531 dw2_asm_output_data (2, ver, "DWARF version number");
9532 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9533 debug_abbrev_section,
9534 "Offset Into Abbrev. Section");
9535 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9538 /* Output the compilation unit DIE and its children. */
9540 static void
9541 output_comp_unit (dw_die_ref die, int output_if_empty)
9543 const char *secname, *oldsym;
9544 char *tmp;
9546 /* Unless we are outputting main CU, we may throw away empty ones. */
9547 if (!output_if_empty && die->die_child == NULL)
9548 return;
9550 /* Even if there are no children of this DIE, we must output the information
9551 about the compilation unit. Otherwise, on an empty translation unit, we
9552 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9553 will then complain when examining the file. First mark all the DIEs in
9554 this CU so we know which get local refs. */
9555 mark_dies (die);
9557 external_ref_hash_type *extern_map = optimize_external_refs (die);
9559 build_abbrev_table (die, extern_map);
9561 delete extern_map;
9563 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9564 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9565 calc_die_sizes (die);
9567 oldsym = die->die_id.die_symbol;
9568 if (oldsym)
9570 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9572 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9573 secname = tmp;
9574 die->die_id.die_symbol = NULL;
9575 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9577 else
9579 switch_to_section (debug_info_section);
9580 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9581 info_section_emitted = true;
9584 /* Output debugging information. */
9585 output_compilation_unit_header ();
9586 output_die (die);
9588 /* Leave the marks on the main CU, so we can check them in
9589 output_pubnames. */
9590 if (oldsym)
9592 unmark_dies (die);
9593 die->die_id.die_symbol = oldsym;
9597 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9598 and .debug_pubtypes. This is configured per-target, but can be
9599 overridden by the -gpubnames or -gno-pubnames options. */
9601 static inline bool
9602 want_pubnames (void)
9604 if (debug_info_level <= DINFO_LEVEL_TERSE)
9605 return false;
9606 if (debug_generate_pub_sections != -1)
9607 return debug_generate_pub_sections;
9608 return targetm.want_debug_pub_sections;
9611 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9613 static void
9614 add_AT_pubnames (dw_die_ref die)
9616 if (want_pubnames ())
9617 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9620 /* Add a string attribute value to a skeleton DIE. */
9622 static inline void
9623 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9624 const char *str)
9626 dw_attr_node attr;
9627 struct indirect_string_node *node;
9629 if (! skeleton_debug_str_hash)
9630 skeleton_debug_str_hash
9631 = hash_table<indirect_string_hasher>::create_ggc (10);
9633 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9634 find_string_form (node);
9635 if (node->form == DW_FORM_GNU_str_index)
9636 node->form = DW_FORM_strp;
9638 attr.dw_attr = attr_kind;
9639 attr.dw_attr_val.val_class = dw_val_class_str;
9640 attr.dw_attr_val.val_entry = NULL;
9641 attr.dw_attr_val.v.val_str = node;
9642 add_dwarf_attr (die, &attr);
9645 /* Helper function to generate top-level dies for skeleton debug_info and
9646 debug_types. */
9648 static void
9649 add_top_level_skeleton_die_attrs (dw_die_ref die)
9651 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9652 const char *comp_dir = comp_dir_string ();
9654 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9655 if (comp_dir != NULL)
9656 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9657 add_AT_pubnames (die);
9658 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9661 /* Output skeleton debug sections that point to the dwo file. */
9663 static void
9664 output_skeleton_debug_sections (dw_die_ref comp_unit)
9666 /* We don't support actual DWARFv5 units yet, we just use some
9667 DWARFv5 draft DIE tags in DWARFv4 format. */
9668 int ver = dwarf_version < 5 ? dwarf_version : 4;
9670 /* These attributes will be found in the full debug_info section. */
9671 remove_AT (comp_unit, DW_AT_producer);
9672 remove_AT (comp_unit, DW_AT_language);
9674 switch_to_section (debug_skeleton_info_section);
9675 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9677 /* Produce the skeleton compilation-unit header. This one differs enough from
9678 a normal CU header that it's better not to call output_compilation_unit
9679 header. */
9680 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9681 dw2_asm_output_data (4, 0xffffffff,
9682 "Initial length escape value indicating 64-bit DWARF extension");
9684 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9685 DWARF_COMPILE_UNIT_HEADER_SIZE
9686 - DWARF_INITIAL_LENGTH_SIZE
9687 + size_of_die (comp_unit),
9688 "Length of Compilation Unit Info");
9689 dw2_asm_output_data (2, ver, "DWARF version number");
9690 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9691 debug_abbrev_section,
9692 "Offset Into Abbrev. Section");
9693 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9695 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9696 output_die (comp_unit);
9698 /* Build the skeleton debug_abbrev section. */
9699 switch_to_section (debug_skeleton_abbrev_section);
9700 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9702 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9704 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9707 /* Output a comdat type unit DIE and its children. */
9709 static void
9710 output_comdat_type_unit (comdat_type_node *node)
9712 const char *secname;
9713 char *tmp;
9714 int i;
9715 #if defined (OBJECT_FORMAT_ELF)
9716 tree comdat_key;
9717 #endif
9719 /* First mark all the DIEs in this CU so we know which get local refs. */
9720 mark_dies (node->root_die);
9722 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9724 build_abbrev_table (node->root_die, extern_map);
9726 delete extern_map;
9727 extern_map = NULL;
9729 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9730 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9731 calc_die_sizes (node->root_die);
9733 #if defined (OBJECT_FORMAT_ELF)
9734 if (!dwarf_split_debug_info)
9735 secname = ".debug_types";
9736 else
9737 secname = ".debug_types.dwo";
9739 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9740 sprintf (tmp, "wt.");
9741 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9742 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9743 comdat_key = get_identifier (tmp);
9744 targetm.asm_out.named_section (secname,
9745 SECTION_DEBUG | SECTION_LINKONCE,
9746 comdat_key);
9747 #else
9748 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9749 sprintf (tmp, ".gnu.linkonce.wt.");
9750 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9751 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9752 secname = tmp;
9753 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9754 #endif
9756 /* Output debugging information. */
9757 output_compilation_unit_header ();
9758 output_signature (node->signature, "Type Signature");
9759 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9760 "Offset to Type DIE");
9761 output_die (node->root_die);
9763 unmark_dies (node->root_die);
9766 /* Return the DWARF2/3 pubname associated with a decl. */
9768 static const char *
9769 dwarf2_name (tree decl, int scope)
9771 if (DECL_NAMELESS (decl))
9772 return NULL;
9773 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9776 /* Add a new entry to .debug_pubnames if appropriate. */
9778 static void
9779 add_pubname_string (const char *str, dw_die_ref die)
9781 pubname_entry e;
9783 e.die = die;
9784 e.name = xstrdup (str);
9785 vec_safe_push (pubname_table, e);
9788 static void
9789 add_pubname (tree decl, dw_die_ref die)
9791 if (!want_pubnames ())
9792 return;
9794 /* Don't add items to the table when we expect that the consumer will have
9795 just read the enclosing die. For example, if the consumer is looking at a
9796 class_member, it will either be inside the class already, or will have just
9797 looked up the class to find the member. Either way, searching the class is
9798 faster than searching the index. */
9799 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9800 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9802 const char *name = dwarf2_name (decl, 1);
9804 if (name)
9805 add_pubname_string (name, die);
9809 /* Add an enumerator to the pubnames section. */
9811 static void
9812 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9814 pubname_entry e;
9816 gcc_assert (scope_name);
9817 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9818 e.die = die;
9819 vec_safe_push (pubname_table, e);
9822 /* Add a new entry to .debug_pubtypes if appropriate. */
9824 static void
9825 add_pubtype (tree decl, dw_die_ref die)
9827 pubname_entry e;
9829 if (!want_pubnames ())
9830 return;
9832 if ((TREE_PUBLIC (decl)
9833 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9834 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9836 tree scope = NULL;
9837 const char *scope_name = "";
9838 const char *sep = is_cxx () ? "::" : ".";
9839 const char *name;
9841 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9842 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9844 scope_name = lang_hooks.dwarf_name (scope, 1);
9845 if (scope_name != NULL && scope_name[0] != '\0')
9846 scope_name = concat (scope_name, sep, NULL);
9847 else
9848 scope_name = "";
9851 if (TYPE_P (decl))
9852 name = type_tag (decl);
9853 else
9854 name = lang_hooks.dwarf_name (decl, 1);
9856 /* If we don't have a name for the type, there's no point in adding
9857 it to the table. */
9858 if (name != NULL && name[0] != '\0')
9860 e.die = die;
9861 e.name = concat (scope_name, name, NULL);
9862 vec_safe_push (pubtype_table, e);
9865 /* Although it might be more consistent to add the pubinfo for the
9866 enumerators as their dies are created, they should only be added if the
9867 enum type meets the criteria above. So rather than re-check the parent
9868 enum type whenever an enumerator die is created, just output them all
9869 here. This isn't protected by the name conditional because anonymous
9870 enums don't have names. */
9871 if (die->die_tag == DW_TAG_enumeration_type)
9873 dw_die_ref c;
9875 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9880 /* Output a single entry in the pubnames table. */
9882 static void
9883 output_pubname (dw_offset die_offset, pubname_entry *entry)
9885 dw_die_ref die = entry->die;
9886 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9888 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9890 if (debug_generate_pub_sections == 2)
9892 /* This logic follows gdb's method for determining the value of the flag
9893 byte. */
9894 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9895 switch (die->die_tag)
9897 case DW_TAG_typedef:
9898 case DW_TAG_base_type:
9899 case DW_TAG_subrange_type:
9900 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9901 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9902 break;
9903 case DW_TAG_enumerator:
9904 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9905 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9906 if (!is_cxx () && !is_java ())
9907 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9908 break;
9909 case DW_TAG_subprogram:
9910 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9911 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9912 if (!is_ada ())
9913 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9914 break;
9915 case DW_TAG_constant:
9916 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9917 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9918 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9919 break;
9920 case DW_TAG_variable:
9921 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9922 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9923 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9924 break;
9925 case DW_TAG_namespace:
9926 case DW_TAG_imported_declaration:
9927 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9928 break;
9929 case DW_TAG_class_type:
9930 case DW_TAG_interface_type:
9931 case DW_TAG_structure_type:
9932 case DW_TAG_union_type:
9933 case DW_TAG_enumeration_type:
9934 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9935 if (!is_cxx () && !is_java ())
9936 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9937 break;
9938 default:
9939 /* An unusual tag. Leave the flag-byte empty. */
9940 break;
9942 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9943 "GDB-index flags");
9946 dw2_asm_output_nstring (entry->name, -1, "external name");
9950 /* Output the public names table used to speed up access to externally
9951 visible names; or the public types table used to find type definitions. */
9953 static void
9954 output_pubnames (vec<pubname_entry, va_gc> *names)
9956 unsigned i;
9957 unsigned long pubnames_length = size_of_pubnames (names);
9958 pubname_entry *pub;
9960 if (!XCOFF_DEBUGGING_INFO)
9962 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9963 dw2_asm_output_data (4, 0xffffffff,
9964 "Initial length escape value indicating 64-bit DWARF extension");
9965 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9966 "Pub Info Length");
9969 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9970 dw2_asm_output_data (2, 2, "DWARF Version");
9972 if (dwarf_split_debug_info)
9973 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9974 debug_skeleton_info_section,
9975 "Offset of Compilation Unit Info");
9976 else
9977 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9978 debug_info_section,
9979 "Offset of Compilation Unit Info");
9980 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9981 "Compilation Unit Length");
9983 FOR_EACH_VEC_ELT (*names, i, pub)
9985 if (include_pubname_in_output (names, pub))
9987 dw_offset die_offset = pub->die->die_offset;
9989 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9990 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9991 gcc_assert (pub->die->die_mark);
9993 /* If we're putting types in their own .debug_types sections,
9994 the .debug_pubtypes table will still point to the compile
9995 unit (not the type unit), so we want to use the offset of
9996 the skeleton DIE (if there is one). */
9997 if (pub->die->comdat_type_p && names == pubtype_table)
9999 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10001 if (type_node != NULL)
10002 die_offset = (type_node->skeleton_die != NULL
10003 ? type_node->skeleton_die->die_offset
10004 : comp_unit_die ()->die_offset);
10007 output_pubname (die_offset, pub);
10011 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10014 /* Output public names and types tables if necessary. */
10016 static void
10017 output_pubtables (void)
10019 if (!want_pubnames () || !info_section_emitted)
10020 return;
10022 switch_to_section (debug_pubnames_section);
10023 output_pubnames (pubname_table);
10024 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10025 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10026 simply won't look for the section. */
10027 switch_to_section (debug_pubtypes_section);
10028 output_pubnames (pubtype_table);
10032 /* Output the information that goes into the .debug_aranges table.
10033 Namely, define the beginning and ending address range of the
10034 text section generated for this compilation unit. */
10036 static void
10037 output_aranges (void)
10039 unsigned i;
10040 unsigned long aranges_length = size_of_aranges ();
10042 if (!XCOFF_DEBUGGING_INFO)
10044 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10045 dw2_asm_output_data (4, 0xffffffff,
10046 "Initial length escape value indicating 64-bit DWARF extension");
10047 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10048 "Length of Address Ranges Info");
10051 /* Version number for aranges is still 2, even up to DWARF5. */
10052 dw2_asm_output_data (2, 2, "DWARF Version");
10053 if (dwarf_split_debug_info)
10054 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10055 debug_skeleton_info_section,
10056 "Offset of Compilation Unit Info");
10057 else
10058 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10059 debug_info_section,
10060 "Offset of Compilation Unit Info");
10061 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10062 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10064 /* We need to align to twice the pointer size here. */
10065 if (DWARF_ARANGES_PAD_SIZE)
10067 /* Pad using a 2 byte words so that padding is correct for any
10068 pointer size. */
10069 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10070 2 * DWARF2_ADDR_SIZE);
10071 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10072 dw2_asm_output_data (2, 0, NULL);
10075 /* It is necessary not to output these entries if the sections were
10076 not used; if the sections were not used, the length will be 0 and
10077 the address may end up as 0 if the section is discarded by ld
10078 --gc-sections, leaving an invalid (0, 0) entry that can be
10079 confused with the terminator. */
10080 if (text_section_used)
10082 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10083 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10084 text_section_label, "Length");
10086 if (cold_text_section_used)
10088 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10089 "Address");
10090 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10091 cold_text_section_label, "Length");
10094 if (have_multiple_function_sections)
10096 unsigned fde_idx;
10097 dw_fde_ref fde;
10099 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10101 if (DECL_IGNORED_P (fde->decl))
10102 continue;
10103 if (!fde->in_std_section)
10105 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10106 "Address");
10107 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10108 fde->dw_fde_begin, "Length");
10110 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10112 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10113 "Address");
10114 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10115 fde->dw_fde_second_begin, "Length");
10120 /* Output the terminator words. */
10121 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10122 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10125 /* Add a new entry to .debug_ranges. Return the offset at which it
10126 was placed. */
10128 static unsigned int
10129 add_ranges_num (int num)
10131 unsigned int in_use = ranges_table_in_use;
10133 if (in_use == ranges_table_allocated)
10135 ranges_table_allocated += RANGES_TABLE_INCREMENT;
10136 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10137 ranges_table_allocated);
10138 memset (ranges_table + ranges_table_in_use, 0,
10139 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10142 ranges_table[in_use].num = num;
10143 ranges_table_in_use = in_use + 1;
10145 return in_use * 2 * DWARF2_ADDR_SIZE;
10148 /* Add a new entry to .debug_ranges corresponding to a block, or a
10149 range terminator if BLOCK is NULL. */
10151 static unsigned int
10152 add_ranges (const_tree block)
10154 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10157 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10158 When using dwarf_split_debug_info, address attributes in dies destined
10159 for the final executable should be direct references--setting the
10160 parameter force_direct ensures this behavior. */
10162 static void
10163 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10164 bool *added, bool force_direct)
10166 unsigned int in_use = ranges_by_label_in_use;
10167 unsigned int offset;
10169 if (in_use == ranges_by_label_allocated)
10171 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10172 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10173 ranges_by_label_allocated);
10174 memset (ranges_by_label + ranges_by_label_in_use, 0,
10175 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10178 ranges_by_label[in_use].begin = begin;
10179 ranges_by_label[in_use].end = end;
10180 ranges_by_label_in_use = in_use + 1;
10182 offset = add_ranges_num (-(int)in_use - 1);
10183 if (!*added)
10185 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10186 *added = true;
10190 static void
10191 output_ranges (void)
10193 unsigned i;
10194 static const char *const start_fmt = "Offset %#x";
10195 const char *fmt = start_fmt;
10197 for (i = 0; i < ranges_table_in_use; i++)
10199 int block_num = ranges_table[i].num;
10201 if (block_num > 0)
10203 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10204 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10206 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10207 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10209 /* If all code is in the text section, then the compilation
10210 unit base address defaults to DW_AT_low_pc, which is the
10211 base of the text section. */
10212 if (!have_multiple_function_sections)
10214 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10215 text_section_label,
10216 fmt, i * 2 * DWARF2_ADDR_SIZE);
10217 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10218 text_section_label, NULL);
10221 /* Otherwise, the compilation unit base address is zero,
10222 which allows us to use absolute addresses, and not worry
10223 about whether the target supports cross-section
10224 arithmetic. */
10225 else
10227 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10228 fmt, i * 2 * DWARF2_ADDR_SIZE);
10229 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10232 fmt = NULL;
10235 /* Negative block_num stands for an index into ranges_by_label. */
10236 else if (block_num < 0)
10238 int lab_idx = - block_num - 1;
10240 if (!have_multiple_function_sections)
10242 gcc_unreachable ();
10243 #if 0
10244 /* If we ever use add_ranges_by_labels () for a single
10245 function section, all we have to do is to take out
10246 the #if 0 above. */
10247 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10248 ranges_by_label[lab_idx].begin,
10249 text_section_label,
10250 fmt, i * 2 * DWARF2_ADDR_SIZE);
10251 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10252 ranges_by_label[lab_idx].end,
10253 text_section_label, NULL);
10254 #endif
10256 else
10258 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10259 ranges_by_label[lab_idx].begin,
10260 fmt, i * 2 * DWARF2_ADDR_SIZE);
10261 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10262 ranges_by_label[lab_idx].end,
10263 NULL);
10266 else
10268 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10269 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10270 fmt = start_fmt;
10275 /* Data structure containing information about input files. */
10276 struct file_info
10278 const char *path; /* Complete file name. */
10279 const char *fname; /* File name part. */
10280 int length; /* Length of entire string. */
10281 struct dwarf_file_data * file_idx; /* Index in input file table. */
10282 int dir_idx; /* Index in directory table. */
10285 /* Data structure containing information about directories with source
10286 files. */
10287 struct dir_info
10289 const char *path; /* Path including directory name. */
10290 int length; /* Path length. */
10291 int prefix; /* Index of directory entry which is a prefix. */
10292 int count; /* Number of files in this directory. */
10293 int dir_idx; /* Index of directory used as base. */
10296 /* Callback function for file_info comparison. We sort by looking at
10297 the directories in the path. */
10299 static int
10300 file_info_cmp (const void *p1, const void *p2)
10302 const struct file_info *const s1 = (const struct file_info *) p1;
10303 const struct file_info *const s2 = (const struct file_info *) p2;
10304 const unsigned char *cp1;
10305 const unsigned char *cp2;
10307 /* Take care of file names without directories. We need to make sure that
10308 we return consistent values to qsort since some will get confused if
10309 we return the same value when identical operands are passed in opposite
10310 orders. So if neither has a directory, return 0 and otherwise return
10311 1 or -1 depending on which one has the directory. */
10312 if ((s1->path == s1->fname || s2->path == s2->fname))
10313 return (s2->path == s2->fname) - (s1->path == s1->fname);
10315 cp1 = (const unsigned char *) s1->path;
10316 cp2 = (const unsigned char *) s2->path;
10318 while (1)
10320 ++cp1;
10321 ++cp2;
10322 /* Reached the end of the first path? If so, handle like above. */
10323 if ((cp1 == (const unsigned char *) s1->fname)
10324 || (cp2 == (const unsigned char *) s2->fname))
10325 return ((cp2 == (const unsigned char *) s2->fname)
10326 - (cp1 == (const unsigned char *) s1->fname));
10328 /* Character of current path component the same? */
10329 else if (*cp1 != *cp2)
10330 return *cp1 - *cp2;
10334 struct file_name_acquire_data
10336 struct file_info *files;
10337 int used_files;
10338 int max_files;
10341 /* Traversal function for the hash table. */
10344 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10346 struct dwarf_file_data *d = *slot;
10347 struct file_info *fi;
10348 const char *f;
10350 gcc_assert (fnad->max_files >= d->emitted_number);
10352 if (! d->emitted_number)
10353 return 1;
10355 gcc_assert (fnad->max_files != fnad->used_files);
10357 fi = fnad->files + fnad->used_files++;
10359 /* Skip all leading "./". */
10360 f = d->filename;
10361 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10362 f += 2;
10364 /* Create a new array entry. */
10365 fi->path = f;
10366 fi->length = strlen (f);
10367 fi->file_idx = d;
10369 /* Search for the file name part. */
10370 f = strrchr (f, DIR_SEPARATOR);
10371 #if defined (DIR_SEPARATOR_2)
10373 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10375 if (g != NULL)
10377 if (f == NULL || f < g)
10378 f = g;
10381 #endif
10383 fi->fname = f == NULL ? fi->path : f + 1;
10384 return 1;
10387 /* Output the directory table and the file name table. We try to minimize
10388 the total amount of memory needed. A heuristic is used to avoid large
10389 slowdowns with many input files. */
10391 static void
10392 output_file_names (void)
10394 struct file_name_acquire_data fnad;
10395 int numfiles;
10396 struct file_info *files;
10397 struct dir_info *dirs;
10398 int *saved;
10399 int *savehere;
10400 int *backmap;
10401 int ndirs;
10402 int idx_offset;
10403 int i;
10405 if (!last_emitted_file)
10407 dw2_asm_output_data (1, 0, "End directory table");
10408 dw2_asm_output_data (1, 0, "End file name table");
10409 return;
10412 numfiles = last_emitted_file->emitted_number;
10414 /* Allocate the various arrays we need. */
10415 files = XALLOCAVEC (struct file_info, numfiles);
10416 dirs = XALLOCAVEC (struct dir_info, numfiles);
10418 fnad.files = files;
10419 fnad.used_files = 0;
10420 fnad.max_files = numfiles;
10421 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10422 gcc_assert (fnad.used_files == fnad.max_files);
10424 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10426 /* Find all the different directories used. */
10427 dirs[0].path = files[0].path;
10428 dirs[0].length = files[0].fname - files[0].path;
10429 dirs[0].prefix = -1;
10430 dirs[0].count = 1;
10431 dirs[0].dir_idx = 0;
10432 files[0].dir_idx = 0;
10433 ndirs = 1;
10435 for (i = 1; i < numfiles; i++)
10436 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10437 && memcmp (dirs[ndirs - 1].path, files[i].path,
10438 dirs[ndirs - 1].length) == 0)
10440 /* Same directory as last entry. */
10441 files[i].dir_idx = ndirs - 1;
10442 ++dirs[ndirs - 1].count;
10444 else
10446 int j;
10448 /* This is a new directory. */
10449 dirs[ndirs].path = files[i].path;
10450 dirs[ndirs].length = files[i].fname - files[i].path;
10451 dirs[ndirs].count = 1;
10452 dirs[ndirs].dir_idx = ndirs;
10453 files[i].dir_idx = ndirs;
10455 /* Search for a prefix. */
10456 dirs[ndirs].prefix = -1;
10457 for (j = 0; j < ndirs; j++)
10458 if (dirs[j].length < dirs[ndirs].length
10459 && dirs[j].length > 1
10460 && (dirs[ndirs].prefix == -1
10461 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10462 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10463 dirs[ndirs].prefix = j;
10465 ++ndirs;
10468 /* Now to the actual work. We have to find a subset of the directories which
10469 allow expressing the file name using references to the directory table
10470 with the least amount of characters. We do not do an exhaustive search
10471 where we would have to check out every combination of every single
10472 possible prefix. Instead we use a heuristic which provides nearly optimal
10473 results in most cases and never is much off. */
10474 saved = XALLOCAVEC (int, ndirs);
10475 savehere = XALLOCAVEC (int, ndirs);
10477 memset (saved, '\0', ndirs * sizeof (saved[0]));
10478 for (i = 0; i < ndirs; i++)
10480 int j;
10481 int total;
10483 /* We can always save some space for the current directory. But this
10484 does not mean it will be enough to justify adding the directory. */
10485 savehere[i] = dirs[i].length;
10486 total = (savehere[i] - saved[i]) * dirs[i].count;
10488 for (j = i + 1; j < ndirs; j++)
10490 savehere[j] = 0;
10491 if (saved[j] < dirs[i].length)
10493 /* Determine whether the dirs[i] path is a prefix of the
10494 dirs[j] path. */
10495 int k;
10497 k = dirs[j].prefix;
10498 while (k != -1 && k != (int) i)
10499 k = dirs[k].prefix;
10501 if (k == (int) i)
10503 /* Yes it is. We can possibly save some memory by
10504 writing the filenames in dirs[j] relative to
10505 dirs[i]. */
10506 savehere[j] = dirs[i].length;
10507 total += (savehere[j] - saved[j]) * dirs[j].count;
10512 /* Check whether we can save enough to justify adding the dirs[i]
10513 directory. */
10514 if (total > dirs[i].length + 1)
10516 /* It's worthwhile adding. */
10517 for (j = i; j < ndirs; j++)
10518 if (savehere[j] > 0)
10520 /* Remember how much we saved for this directory so far. */
10521 saved[j] = savehere[j];
10523 /* Remember the prefix directory. */
10524 dirs[j].dir_idx = i;
10529 /* Emit the directory name table. */
10530 idx_offset = dirs[0].length > 0 ? 1 : 0;
10531 for (i = 1 - idx_offset; i < ndirs; i++)
10532 dw2_asm_output_nstring (dirs[i].path,
10533 dirs[i].length
10534 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10535 "Directory Entry: %#x", i + idx_offset);
10537 dw2_asm_output_data (1, 0, "End directory table");
10539 /* We have to emit them in the order of emitted_number since that's
10540 used in the debug info generation. To do this efficiently we
10541 generate a back-mapping of the indices first. */
10542 backmap = XALLOCAVEC (int, numfiles);
10543 for (i = 0; i < numfiles; i++)
10544 backmap[files[i].file_idx->emitted_number - 1] = i;
10546 /* Now write all the file names. */
10547 for (i = 0; i < numfiles; i++)
10549 int file_idx = backmap[i];
10550 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10552 #ifdef VMS_DEBUGGING_INFO
10553 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10555 /* Setting these fields can lead to debugger miscomparisons,
10556 but VMS Debug requires them to be set correctly. */
10558 int ver;
10559 long long cdt;
10560 long siz;
10561 int maxfilelen = strlen (files[file_idx].path)
10562 + dirs[dir_idx].length
10563 + MAX_VMS_VERSION_LEN + 1;
10564 char *filebuf = XALLOCAVEC (char, maxfilelen);
10566 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10567 snprintf (filebuf, maxfilelen, "%s;%d",
10568 files[file_idx].path + dirs[dir_idx].length, ver);
10570 dw2_asm_output_nstring
10571 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10573 /* Include directory index. */
10574 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10576 /* Modification time. */
10577 dw2_asm_output_data_uleb128
10578 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10579 ? cdt : 0,
10580 NULL);
10582 /* File length in bytes. */
10583 dw2_asm_output_data_uleb128
10584 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10585 ? siz : 0,
10586 NULL);
10587 #else
10588 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10589 "File Entry: %#x", (unsigned) i + 1);
10591 /* Include directory index. */
10592 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10594 /* Modification time. */
10595 dw2_asm_output_data_uleb128 (0, NULL);
10597 /* File length in bytes. */
10598 dw2_asm_output_data_uleb128 (0, NULL);
10599 #endif /* VMS_DEBUGGING_INFO */
10602 dw2_asm_output_data (1, 0, "End file name table");
10606 /* Output one line number table into the .debug_line section. */
10608 static void
10609 output_one_line_info_table (dw_line_info_table *table)
10611 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10612 unsigned int current_line = 1;
10613 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10614 dw_line_info_entry *ent;
10615 size_t i;
10617 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10619 switch (ent->opcode)
10621 case LI_set_address:
10622 /* ??? Unfortunately, we have little choice here currently, and
10623 must always use the most general form. GCC does not know the
10624 address delta itself, so we can't use DW_LNS_advance_pc. Many
10625 ports do have length attributes which will give an upper bound
10626 on the address range. We could perhaps use length attributes
10627 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10628 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10630 /* This can handle any delta. This takes
10631 4+DWARF2_ADDR_SIZE bytes. */
10632 dw2_asm_output_data (1, 0, "set address %s", line_label);
10633 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10634 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10635 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10636 break;
10638 case LI_set_line:
10639 if (ent->val == current_line)
10641 /* We still need to start a new row, so output a copy insn. */
10642 dw2_asm_output_data (1, DW_LNS_copy,
10643 "copy line %u", current_line);
10645 else
10647 int line_offset = ent->val - current_line;
10648 int line_delta = line_offset - DWARF_LINE_BASE;
10650 current_line = ent->val;
10651 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10653 /* This can handle deltas from -10 to 234, using the current
10654 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10655 This takes 1 byte. */
10656 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10657 "line %u", current_line);
10659 else
10661 /* This can handle any delta. This takes at least 4 bytes,
10662 depending on the value being encoded. */
10663 dw2_asm_output_data (1, DW_LNS_advance_line,
10664 "advance to line %u", current_line);
10665 dw2_asm_output_data_sleb128 (line_offset, NULL);
10666 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10669 break;
10671 case LI_set_file:
10672 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10673 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10674 break;
10676 case LI_set_column:
10677 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10678 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10679 break;
10681 case LI_negate_stmt:
10682 current_is_stmt = !current_is_stmt;
10683 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10684 "is_stmt %d", current_is_stmt);
10685 break;
10687 case LI_set_prologue_end:
10688 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10689 "set prologue end");
10690 break;
10692 case LI_set_epilogue_begin:
10693 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10694 "set epilogue begin");
10695 break;
10697 case LI_set_discriminator:
10698 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10699 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10700 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10701 dw2_asm_output_data_uleb128 (ent->val, NULL);
10702 break;
10706 /* Emit debug info for the address of the end of the table. */
10707 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10708 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10709 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10710 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10712 dw2_asm_output_data (1, 0, "end sequence");
10713 dw2_asm_output_data_uleb128 (1, NULL);
10714 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10717 /* Output the source line number correspondence information. This
10718 information goes into the .debug_line section. */
10720 static void
10721 output_line_info (bool prologue_only)
10723 char l1[20], l2[20], p1[20], p2[20];
10724 /* We don't support DWARFv5 line tables yet. */
10725 int ver = dwarf_version < 5 ? dwarf_version : 4;
10726 bool saw_one = false;
10727 int opc;
10729 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10730 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10731 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10732 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10734 if (!XCOFF_DEBUGGING_INFO)
10736 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10737 dw2_asm_output_data (4, 0xffffffff,
10738 "Initial length escape value indicating 64-bit DWARF extension");
10739 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10740 "Length of Source Line Info");
10743 ASM_OUTPUT_LABEL (asm_out_file, l1);
10745 dw2_asm_output_data (2, ver, "DWARF Version");
10746 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10747 ASM_OUTPUT_LABEL (asm_out_file, p1);
10749 /* Define the architecture-dependent minimum instruction length (in bytes).
10750 In this implementation of DWARF, this field is used for information
10751 purposes only. Since GCC generates assembly language, we have no
10752 a priori knowledge of how many instruction bytes are generated for each
10753 source line, and therefore can use only the DW_LNE_set_address and
10754 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10755 this as '1', which is "correct enough" for all architectures,
10756 and don't let the target override. */
10757 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10759 if (ver >= 4)
10760 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10761 "Maximum Operations Per Instruction");
10762 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10763 "Default is_stmt_start flag");
10764 dw2_asm_output_data (1, DWARF_LINE_BASE,
10765 "Line Base Value (Special Opcodes)");
10766 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10767 "Line Range Value (Special Opcodes)");
10768 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10769 "Special Opcode Base");
10771 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10773 int n_op_args;
10774 switch (opc)
10776 case DW_LNS_advance_pc:
10777 case DW_LNS_advance_line:
10778 case DW_LNS_set_file:
10779 case DW_LNS_set_column:
10780 case DW_LNS_fixed_advance_pc:
10781 case DW_LNS_set_isa:
10782 n_op_args = 1;
10783 break;
10784 default:
10785 n_op_args = 0;
10786 break;
10789 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10790 opc, n_op_args);
10793 /* Write out the information about the files we use. */
10794 output_file_names ();
10795 ASM_OUTPUT_LABEL (asm_out_file, p2);
10796 if (prologue_only)
10798 /* Output the marker for the end of the line number info. */
10799 ASM_OUTPUT_LABEL (asm_out_file, l2);
10800 return;
10803 if (separate_line_info)
10805 dw_line_info_table *table;
10806 size_t i;
10808 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10809 if (table->in_use)
10811 output_one_line_info_table (table);
10812 saw_one = true;
10815 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10817 output_one_line_info_table (cold_text_section_line_info);
10818 saw_one = true;
10821 /* ??? Some Darwin linkers crash on a .debug_line section with no
10822 sequences. Further, merely a DW_LNE_end_sequence entry is not
10823 sufficient -- the address column must also be initialized.
10824 Make sure to output at least one set_address/end_sequence pair,
10825 choosing .text since that section is always present. */
10826 if (text_section_line_info->in_use || !saw_one)
10827 output_one_line_info_table (text_section_line_info);
10829 /* Output the marker for the end of the line number info. */
10830 ASM_OUTPUT_LABEL (asm_out_file, l2);
10833 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
10835 static inline bool
10836 need_endianity_attribute_p (bool reverse)
10838 return reverse && (dwarf_version >= 3 || !dwarf_strict);
10841 /* Given a pointer to a tree node for some base type, return a pointer to
10842 a DIE that describes the given type. REVERSE is true if the type is
10843 to be interpreted in the reverse storage order wrt the target order.
10845 This routine must only be called for GCC type nodes that correspond to
10846 Dwarf base (fundamental) types. */
10848 static dw_die_ref
10849 base_type_die (tree type, bool reverse)
10851 dw_die_ref base_type_result;
10852 enum dwarf_type encoding;
10853 bool fpt_used = false;
10854 struct fixed_point_type_info fpt_info;
10855 tree type_bias = NULL_TREE;
10857 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10858 return 0;
10860 /* If this is a subtype that should not be emitted as a subrange type,
10861 use the base type. See subrange_type_for_debug_p. */
10862 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10863 type = TREE_TYPE (type);
10865 switch (TREE_CODE (type))
10867 case INTEGER_TYPE:
10868 if ((dwarf_version >= 4 || !dwarf_strict)
10869 && TYPE_NAME (type)
10870 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10871 && DECL_IS_BUILTIN (TYPE_NAME (type))
10872 && DECL_NAME (TYPE_NAME (type)))
10874 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10875 if (strcmp (name, "char16_t") == 0
10876 || strcmp (name, "char32_t") == 0)
10878 encoding = DW_ATE_UTF;
10879 break;
10882 if ((dwarf_version >= 3 || !dwarf_strict)
10883 && lang_hooks.types.get_fixed_point_type_info)
10885 memset (&fpt_info, 0, sizeof (fpt_info));
10886 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10888 fpt_used = true;
10889 encoding = ((TYPE_UNSIGNED (type))
10890 ? DW_ATE_unsigned_fixed
10891 : DW_ATE_signed_fixed);
10892 break;
10895 if (TYPE_STRING_FLAG (type))
10897 if (TYPE_UNSIGNED (type))
10898 encoding = DW_ATE_unsigned_char;
10899 else
10900 encoding = DW_ATE_signed_char;
10902 else if (TYPE_UNSIGNED (type))
10903 encoding = DW_ATE_unsigned;
10904 else
10905 encoding = DW_ATE_signed;
10907 if (!dwarf_strict
10908 && lang_hooks.types.get_type_bias)
10909 type_bias = lang_hooks.types.get_type_bias (type);
10910 break;
10912 case REAL_TYPE:
10913 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10915 if (dwarf_version >= 3 || !dwarf_strict)
10916 encoding = DW_ATE_decimal_float;
10917 else
10918 encoding = DW_ATE_lo_user;
10920 else
10921 encoding = DW_ATE_float;
10922 break;
10924 case FIXED_POINT_TYPE:
10925 if (!(dwarf_version >= 3 || !dwarf_strict))
10926 encoding = DW_ATE_lo_user;
10927 else if (TYPE_UNSIGNED (type))
10928 encoding = DW_ATE_unsigned_fixed;
10929 else
10930 encoding = DW_ATE_signed_fixed;
10931 break;
10933 /* Dwarf2 doesn't know anything about complex ints, so use
10934 a user defined type for it. */
10935 case COMPLEX_TYPE:
10936 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10937 encoding = DW_ATE_complex_float;
10938 else
10939 encoding = DW_ATE_lo_user;
10940 break;
10942 case BOOLEAN_TYPE:
10943 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10944 encoding = DW_ATE_boolean;
10945 break;
10947 default:
10948 /* No other TREE_CODEs are Dwarf fundamental types. */
10949 gcc_unreachable ();
10952 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10954 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10955 int_size_in_bytes (type));
10956 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10958 if (need_endianity_attribute_p (reverse))
10959 add_AT_unsigned (base_type_result, DW_AT_endianity,
10960 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10962 if (fpt_used)
10964 switch (fpt_info.scale_factor_kind)
10966 case fixed_point_scale_factor_binary:
10967 add_AT_int (base_type_result, DW_AT_binary_scale,
10968 fpt_info.scale_factor.binary);
10969 break;
10971 case fixed_point_scale_factor_decimal:
10972 add_AT_int (base_type_result, DW_AT_decimal_scale,
10973 fpt_info.scale_factor.decimal);
10974 break;
10976 case fixed_point_scale_factor_arbitrary:
10977 /* Arbitrary scale factors cannot be described in standard DWARF,
10978 yet. */
10979 if (!dwarf_strict)
10981 /* Describe the scale factor as a rational constant. */
10982 const dw_die_ref scale_factor
10983 = new_die (DW_TAG_constant, comp_unit_die (), type);
10985 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
10986 fpt_info.scale_factor.arbitrary.numerator);
10987 add_AT_int (scale_factor, DW_AT_GNU_denominator,
10988 fpt_info.scale_factor.arbitrary.denominator);
10990 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
10992 break;
10994 default:
10995 gcc_unreachable ();
10999 if (type_bias)
11000 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
11001 dw_scalar_form_constant
11002 | dw_scalar_form_exprloc
11003 | dw_scalar_form_reference,
11004 NULL);
11006 add_pubtype (type, base_type_result);
11008 return base_type_result;
11011 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
11012 named 'auto' in its type: return true for it, false otherwise. */
11014 static inline bool
11015 is_cxx_auto (tree type)
11017 if (is_cxx ())
11019 tree name = TYPE_IDENTIFIER (type);
11020 if (name == get_identifier ("auto")
11021 || name == get_identifier ("decltype(auto)"))
11022 return true;
11024 return false;
11027 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11028 given input type is a Dwarf "fundamental" type. Otherwise return null. */
11030 static inline int
11031 is_base_type (tree type)
11033 switch (TREE_CODE (type))
11035 case ERROR_MARK:
11036 case VOID_TYPE:
11037 case INTEGER_TYPE:
11038 case REAL_TYPE:
11039 case FIXED_POINT_TYPE:
11040 case COMPLEX_TYPE:
11041 case BOOLEAN_TYPE:
11042 case POINTER_BOUNDS_TYPE:
11043 return 1;
11045 case ARRAY_TYPE:
11046 case RECORD_TYPE:
11047 case UNION_TYPE:
11048 case QUAL_UNION_TYPE:
11049 case ENUMERAL_TYPE:
11050 case FUNCTION_TYPE:
11051 case METHOD_TYPE:
11052 case POINTER_TYPE:
11053 case REFERENCE_TYPE:
11054 case NULLPTR_TYPE:
11055 case OFFSET_TYPE:
11056 case LANG_TYPE:
11057 case VECTOR_TYPE:
11058 return 0;
11060 default:
11061 if (is_cxx_auto (type))
11062 return 0;
11063 gcc_unreachable ();
11066 return 0;
11069 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11070 node, return the size in bits for the type if it is a constant, or else
11071 return the alignment for the type if the type's size is not constant, or
11072 else return BITS_PER_WORD if the type actually turns out to be an
11073 ERROR_MARK node. */
11075 static inline unsigned HOST_WIDE_INT
11076 simple_type_size_in_bits (const_tree type)
11078 if (TREE_CODE (type) == ERROR_MARK)
11079 return BITS_PER_WORD;
11080 else if (TYPE_SIZE (type) == NULL_TREE)
11081 return 0;
11082 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11083 return tree_to_uhwi (TYPE_SIZE (type));
11084 else
11085 return TYPE_ALIGN (type);
11088 /* Similarly, but return an offset_int instead of UHWI. */
11090 static inline offset_int
11091 offset_int_type_size_in_bits (const_tree type)
11093 if (TREE_CODE (type) == ERROR_MARK)
11094 return BITS_PER_WORD;
11095 else if (TYPE_SIZE (type) == NULL_TREE)
11096 return 0;
11097 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11098 return wi::to_offset (TYPE_SIZE (type));
11099 else
11100 return TYPE_ALIGN (type);
11103 /* Given a pointer to a tree node for a subrange type, return a pointer
11104 to a DIE that describes the given type. */
11106 static dw_die_ref
11107 subrange_type_die (tree type, tree low, tree high, tree bias,
11108 dw_die_ref context_die)
11110 dw_die_ref subrange_die;
11111 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11113 if (context_die == NULL)
11114 context_die = comp_unit_die ();
11116 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11118 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11120 /* The size of the subrange type and its base type do not match,
11121 so we need to generate a size attribute for the subrange type. */
11122 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11125 if (low)
11126 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11127 if (high)
11128 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11129 if (bias && !dwarf_strict)
11130 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11131 dw_scalar_form_constant
11132 | dw_scalar_form_exprloc
11133 | dw_scalar_form_reference,
11134 NULL);
11136 return subrange_die;
11139 /* Returns the (const and/or volatile) cv_qualifiers associated with
11140 the decl node. This will normally be augmented with the
11141 cv_qualifiers of the underlying type in add_type_attribute. */
11143 static int
11144 decl_quals (const_tree decl)
11146 return ((TREE_READONLY (decl)
11147 /* The C++ front-end correctly marks reference-typed
11148 variables as readonly, but from a language (and debug
11149 info) standpoint they are not const-qualified. */
11150 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
11151 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11152 | (TREE_THIS_VOLATILE (decl)
11153 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11156 /* Determine the TYPE whose qualifiers match the largest strict subset
11157 of the given TYPE_QUALS, and return its qualifiers. Ignore all
11158 qualifiers outside QUAL_MASK. */
11160 static int
11161 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11163 tree t;
11164 int best_rank = 0, best_qual = 0, max_rank;
11166 type_quals &= qual_mask;
11167 max_rank = popcount_hwi (type_quals) - 1;
11169 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11170 t = TYPE_NEXT_VARIANT (t))
11172 int q = TYPE_QUALS (t) & qual_mask;
11174 if ((q & type_quals) == q && q != type_quals
11175 && check_base_type (t, type))
11177 int rank = popcount_hwi (q);
11179 if (rank > best_rank)
11181 best_rank = rank;
11182 best_qual = q;
11187 return best_qual;
11190 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11191 static const dwarf_qual_info_t dwarf_qual_info[] =
11193 { TYPE_QUAL_CONST, DW_TAG_const_type },
11194 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11195 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11196 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11198 static const unsigned int dwarf_qual_info_size
11199 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11201 /* If DIE is a qualified DIE of some base DIE with the same parent,
11202 return the base DIE, otherwise return NULL. Set MASK to the
11203 qualifiers added compared to the returned DIE. */
11205 static dw_die_ref
11206 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11208 unsigned int i;
11209 for (i = 0; i < dwarf_qual_info_size; i++)
11210 if (die->die_tag == dwarf_qual_info[i].t)
11211 break;
11212 if (i == dwarf_qual_info_size)
11213 return NULL;
11214 if (vec_safe_length (die->die_attr) != 1)
11215 return NULL;
11216 dw_die_ref type = get_AT_ref (die, DW_AT_type);
11217 if (type == NULL || type->die_parent != die->die_parent)
11218 return NULL;
11219 *mask |= dwarf_qual_info[i].q;
11220 if (depth)
11222 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11223 if (ret)
11224 return ret;
11226 return type;
11229 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11230 entry that chains the modifiers specified by CV_QUALS in front of the
11231 given type. REVERSE is true if the type is to be interpreted in the
11232 reverse storage order wrt the target order. */
11234 static dw_die_ref
11235 modified_type_die (tree type, int cv_quals, bool reverse,
11236 dw_die_ref context_die)
11238 enum tree_code code = TREE_CODE (type);
11239 dw_die_ref mod_type_die;
11240 dw_die_ref sub_die = NULL;
11241 tree item_type = NULL;
11242 tree qualified_type;
11243 tree name, low, high;
11244 dw_die_ref mod_scope;
11245 /* Only these cv-qualifiers are currently handled. */
11246 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11247 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11249 if (code == ERROR_MARK)
11250 return NULL;
11252 if (lang_hooks.types.get_debug_type)
11254 tree debug_type = lang_hooks.types.get_debug_type (type);
11256 if (debug_type != NULL_TREE && debug_type != type)
11257 return modified_type_die (debug_type, cv_quals, reverse, context_die);
11260 cv_quals &= cv_qual_mask;
11262 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11263 tag modifier (and not an attribute) old consumers won't be able
11264 to handle it. */
11265 if (dwarf_version < 3)
11266 cv_quals &= ~TYPE_QUAL_RESTRICT;
11268 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
11269 if (dwarf_version < 5)
11270 cv_quals &= ~TYPE_QUAL_ATOMIC;
11272 /* See if we already have the appropriately qualified variant of
11273 this type. */
11274 qualified_type = get_qualified_type (type, cv_quals);
11276 if (qualified_type == sizetype
11277 && TYPE_NAME (qualified_type)
11278 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11280 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11282 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11283 && TYPE_PRECISION (t)
11284 == TYPE_PRECISION (qualified_type)
11285 && TYPE_UNSIGNED (t)
11286 == TYPE_UNSIGNED (qualified_type));
11287 qualified_type = t;
11290 /* If we do, then we can just use its DIE, if it exists. */
11291 if (qualified_type)
11293 mod_type_die = lookup_type_die (qualified_type);
11295 /* DW_AT_endianity doesn't come from a qualifier on the type. */
11296 if (mod_type_die
11297 && (!need_endianity_attribute_p (reverse)
11298 || !is_base_type (type)
11299 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11300 return mod_type_die;
11303 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11305 /* Handle C typedef types. */
11306 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11307 && !DECL_ARTIFICIAL (name))
11309 tree dtype = TREE_TYPE (name);
11311 if (qualified_type == dtype)
11313 /* For a named type, use the typedef. */
11314 gen_type_die (qualified_type, context_die);
11315 return lookup_type_die (qualified_type);
11317 else
11319 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11320 dquals &= cv_qual_mask;
11321 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11322 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11323 /* cv-unqualified version of named type. Just use
11324 the unnamed type to which it refers. */
11325 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11326 reverse, context_die);
11327 /* Else cv-qualified version of named type; fall through. */
11331 mod_scope = scope_die_for (type, context_die);
11333 if (cv_quals)
11335 int sub_quals = 0, first_quals = 0;
11336 unsigned i;
11337 dw_die_ref first = NULL, last = NULL;
11339 /* Determine a lesser qualified type that most closely matches
11340 this one. Then generate DW_TAG_* entries for the remaining
11341 qualifiers. */
11342 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11343 cv_qual_mask);
11344 if (sub_quals && use_debug_types)
11346 bool needed = false;
11347 /* If emitting type units, make sure the order of qualifiers
11348 is canonical. Thus, start from unqualified type if
11349 an earlier qualifier is missing in sub_quals, but some later
11350 one is present there. */
11351 for (i = 0; i < dwarf_qual_info_size; i++)
11352 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11353 needed = true;
11354 else if (needed && (dwarf_qual_info[i].q & cv_quals))
11356 sub_quals = 0;
11357 break;
11360 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11361 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11363 /* As not all intermediate qualified DIEs have corresponding
11364 tree types, ensure that qualified DIEs in the same scope
11365 as their DW_AT_type are emitted after their DW_AT_type,
11366 only with other qualified DIEs for the same type possibly
11367 in between them. Determine the range of such qualified
11368 DIEs now (first being the base type, last being corresponding
11369 last qualified DIE for it). */
11370 unsigned int count = 0;
11371 first = qualified_die_p (mod_type_die, &first_quals,
11372 dwarf_qual_info_size);
11373 if (first == NULL)
11374 first = mod_type_die;
11375 gcc_assert ((first_quals & ~sub_quals) == 0);
11376 for (count = 0, last = first;
11377 count < (1U << dwarf_qual_info_size);
11378 count++, last = last->die_sib)
11380 int quals = 0;
11381 if (last == mod_scope->die_child)
11382 break;
11383 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11384 != first)
11385 break;
11389 for (i = 0; i < dwarf_qual_info_size; i++)
11390 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11392 dw_die_ref d;
11393 if (first && first != last)
11395 for (d = first->die_sib; ; d = d->die_sib)
11397 int quals = 0;
11398 qualified_die_p (d, &quals, dwarf_qual_info_size);
11399 if (quals == (first_quals | dwarf_qual_info[i].q))
11400 break;
11401 if (d == last)
11403 d = NULL;
11404 break;
11407 if (d)
11409 mod_type_die = d;
11410 continue;
11413 if (first)
11415 d = ggc_cleared_alloc<die_node> ();
11416 d->die_tag = dwarf_qual_info[i].t;
11417 add_child_die_after (mod_scope, d, last);
11418 last = d;
11420 else
11421 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11422 if (mod_type_die)
11423 add_AT_die_ref (d, DW_AT_type, mod_type_die);
11424 mod_type_die = d;
11425 first_quals |= dwarf_qual_info[i].q;
11428 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11430 dwarf_tag tag = DW_TAG_pointer_type;
11431 if (code == REFERENCE_TYPE)
11433 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11434 tag = DW_TAG_rvalue_reference_type;
11435 else
11436 tag = DW_TAG_reference_type;
11438 mod_type_die = new_die (tag, mod_scope, type);
11440 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11441 simple_type_size_in_bits (type) / BITS_PER_UNIT);
11442 item_type = TREE_TYPE (type);
11444 addr_space_t as = TYPE_ADDR_SPACE (item_type);
11445 if (!ADDR_SPACE_GENERIC_P (as))
11447 int action = targetm.addr_space.debug (as);
11448 if (action >= 0)
11450 /* Positive values indicate an address_class. */
11451 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11453 else
11455 /* Negative values indicate an (inverted) segment base reg. */
11456 dw_loc_descr_ref d
11457 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11458 add_AT_loc (mod_type_die, DW_AT_segment, d);
11462 else if (code == INTEGER_TYPE
11463 && TREE_TYPE (type) != NULL_TREE
11464 && subrange_type_for_debug_p (type, &low, &high))
11466 tree bias = NULL_TREE;
11467 if (lang_hooks.types.get_type_bias)
11468 bias = lang_hooks.types.get_type_bias (type);
11469 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11470 item_type = TREE_TYPE (type);
11472 else if (is_base_type (type))
11473 mod_type_die = base_type_die (type, reverse);
11474 else
11476 gen_type_die (type, context_die);
11478 /* We have to get the type_main_variant here (and pass that to the
11479 `lookup_type_die' routine) because the ..._TYPE node we have
11480 might simply be a *copy* of some original type node (where the
11481 copy was created to help us keep track of typedef names) and
11482 that copy might have a different TYPE_UID from the original
11483 ..._TYPE node. */
11484 if (TREE_CODE (type) != VECTOR_TYPE
11485 && TREE_CODE (type) != ARRAY_TYPE)
11486 return lookup_type_die (type_main_variant (type));
11487 else
11488 /* Vectors have the debugging information in the type,
11489 not the main variant. */
11490 return lookup_type_die (type);
11493 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
11494 don't output a DW_TAG_typedef, since there isn't one in the
11495 user's program; just attach a DW_AT_name to the type.
11496 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11497 if the base type already has the same name. */
11498 if (name
11499 && ((TREE_CODE (name) != TYPE_DECL
11500 && (qualified_type == TYPE_MAIN_VARIANT (type)
11501 || (cv_quals == TYPE_UNQUALIFIED)))
11502 || (TREE_CODE (name) == TYPE_DECL
11503 && TREE_TYPE (name) == qualified_type
11504 && DECL_NAME (name))))
11506 if (TREE_CODE (name) == TYPE_DECL)
11507 /* Could just call add_name_and_src_coords_attributes here,
11508 but since this is a builtin type it doesn't have any
11509 useful source coordinates anyway. */
11510 name = DECL_NAME (name);
11511 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11513 /* This probably indicates a bug. */
11514 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11516 name = TYPE_IDENTIFIER (type);
11517 add_name_attribute (mod_type_die,
11518 name ? IDENTIFIER_POINTER (name) : "__unknown__");
11521 if (qualified_type)
11522 equate_type_number_to_die (qualified_type, mod_type_die);
11524 if (item_type)
11525 /* We must do this after the equate_type_number_to_die call, in case
11526 this is a recursive type. This ensures that the modified_type_die
11527 recursion will terminate even if the type is recursive. Recursive
11528 types are possible in Ada. */
11529 sub_die = modified_type_die (item_type,
11530 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11531 reverse,
11532 context_die);
11534 if (sub_die != NULL)
11535 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11537 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11538 if (TYPE_ARTIFICIAL (type))
11539 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11541 return mod_type_die;
11544 /* Generate DIEs for the generic parameters of T.
11545 T must be either a generic type or a generic function.
11546 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11548 static void
11549 gen_generic_params_dies (tree t)
11551 tree parms, args;
11552 int parms_num, i;
11553 dw_die_ref die = NULL;
11554 int non_default;
11556 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11557 return;
11559 if (TYPE_P (t))
11560 die = lookup_type_die (t);
11561 else if (DECL_P (t))
11562 die = lookup_decl_die (t);
11564 gcc_assert (die);
11566 parms = lang_hooks.get_innermost_generic_parms (t);
11567 if (!parms)
11568 /* T has no generic parameter. It means T is neither a generic type
11569 or function. End of story. */
11570 return;
11572 parms_num = TREE_VEC_LENGTH (parms);
11573 args = lang_hooks.get_innermost_generic_args (t);
11574 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11575 non_default = int_cst_value (TREE_CHAIN (args));
11576 else
11577 non_default = TREE_VEC_LENGTH (args);
11578 for (i = 0; i < parms_num; i++)
11580 tree parm, arg, arg_pack_elems;
11581 dw_die_ref parm_die;
11583 parm = TREE_VEC_ELT (parms, i);
11584 arg = TREE_VEC_ELT (args, i);
11585 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11586 gcc_assert (parm && TREE_VALUE (parm) && arg);
11588 if (parm && TREE_VALUE (parm) && arg)
11590 /* If PARM represents a template parameter pack,
11591 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11592 by DW_TAG_template_*_parameter DIEs for the argument
11593 pack elements of ARG. Note that ARG would then be
11594 an argument pack. */
11595 if (arg_pack_elems)
11596 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11597 arg_pack_elems,
11598 die);
11599 else
11600 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11601 true /* emit name */, die);
11602 if (i >= non_default)
11603 add_AT_flag (parm_die, DW_AT_default_value, 1);
11608 /* Create and return a DIE for PARM which should be
11609 the representation of a generic type parameter.
11610 For instance, in the C++ front end, PARM would be a template parameter.
11611 ARG is the argument to PARM.
11612 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11613 name of the PARM.
11614 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11615 as a child node. */
11617 static dw_die_ref
11618 generic_parameter_die (tree parm, tree arg,
11619 bool emit_name_p,
11620 dw_die_ref parent_die)
11622 dw_die_ref tmpl_die = NULL;
11623 const char *name = NULL;
11625 if (!parm || !DECL_NAME (parm) || !arg)
11626 return NULL;
11628 /* We support non-type generic parameters and arguments,
11629 type generic parameters and arguments, as well as
11630 generic generic parameters (a.k.a. template template parameters in C++)
11631 and arguments. */
11632 if (TREE_CODE (parm) == PARM_DECL)
11633 /* PARM is a nontype generic parameter */
11634 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11635 else if (TREE_CODE (parm) == TYPE_DECL)
11636 /* PARM is a type generic parameter. */
11637 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11638 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11639 /* PARM is a generic generic parameter.
11640 Its DIE is a GNU extension. It shall have a
11641 DW_AT_name attribute to represent the name of the template template
11642 parameter, and a DW_AT_GNU_template_name attribute to represent the
11643 name of the template template argument. */
11644 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11645 parent_die, parm);
11646 else
11647 gcc_unreachable ();
11649 if (tmpl_die)
11651 tree tmpl_type;
11653 /* If PARM is a generic parameter pack, it means we are
11654 emitting debug info for a template argument pack element.
11655 In other terms, ARG is a template argument pack element.
11656 In that case, we don't emit any DW_AT_name attribute for
11657 the die. */
11658 if (emit_name_p)
11660 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11661 gcc_assert (name);
11662 add_AT_string (tmpl_die, DW_AT_name, name);
11665 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11667 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11668 TMPL_DIE should have a child DW_AT_type attribute that is set
11669 to the type of the argument to PARM, which is ARG.
11670 If PARM is a type generic parameter, TMPL_DIE should have a
11671 child DW_AT_type that is set to ARG. */
11672 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11673 add_type_attribute (tmpl_die, tmpl_type,
11674 (TREE_THIS_VOLATILE (tmpl_type)
11675 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11676 false, parent_die);
11678 else
11680 /* So TMPL_DIE is a DIE representing a
11681 a generic generic template parameter, a.k.a template template
11682 parameter in C++ and arg is a template. */
11684 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11685 to the name of the argument. */
11686 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11687 if (name)
11688 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11691 if (TREE_CODE (parm) == PARM_DECL)
11692 /* So PARM is a non-type generic parameter.
11693 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11694 attribute of TMPL_DIE which value represents the value
11695 of ARG.
11696 We must be careful here:
11697 The value of ARG might reference some function decls.
11698 We might currently be emitting debug info for a generic
11699 type and types are emitted before function decls, we don't
11700 know if the function decls referenced by ARG will actually be
11701 emitted after cgraph computations.
11702 So must defer the generation of the DW_AT_const_value to
11703 after cgraph is ready. */
11704 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11707 return tmpl_die;
11710 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11711 PARM_PACK must be a template parameter pack. The returned DIE
11712 will be child DIE of PARENT_DIE. */
11714 static dw_die_ref
11715 template_parameter_pack_die (tree parm_pack,
11716 tree parm_pack_args,
11717 dw_die_ref parent_die)
11719 dw_die_ref die;
11720 int j;
11722 gcc_assert (parent_die && parm_pack);
11724 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11725 add_name_and_src_coords_attributes (die, parm_pack);
11726 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11727 generic_parameter_die (parm_pack,
11728 TREE_VEC_ELT (parm_pack_args, j),
11729 false /* Don't emit DW_AT_name */,
11730 die);
11731 return die;
11734 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11735 an enumerated type. */
11737 static inline int
11738 type_is_enum (const_tree type)
11740 return TREE_CODE (type) == ENUMERAL_TYPE;
11743 /* Return the DBX register number described by a given RTL node. */
11745 static unsigned int
11746 dbx_reg_number (const_rtx rtl)
11748 unsigned regno = REGNO (rtl);
11750 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11752 #ifdef LEAF_REG_REMAP
11753 if (crtl->uses_only_leaf_regs)
11755 int leaf_reg = LEAF_REG_REMAP (regno);
11756 if (leaf_reg != -1)
11757 regno = (unsigned) leaf_reg;
11759 #endif
11761 regno = DBX_REGISTER_NUMBER (regno);
11762 gcc_assert (regno != INVALID_REGNUM);
11763 return regno;
11766 /* Optionally add a DW_OP_piece term to a location description expression.
11767 DW_OP_piece is only added if the location description expression already
11768 doesn't end with DW_OP_piece. */
11770 static void
11771 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11773 dw_loc_descr_ref loc;
11775 if (*list_head != NULL)
11777 /* Find the end of the chain. */
11778 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11781 if (loc->dw_loc_opc != DW_OP_piece)
11782 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11786 /* Return a location descriptor that designates a machine register or
11787 zero if there is none. */
11789 static dw_loc_descr_ref
11790 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11792 rtx regs;
11794 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11795 return 0;
11797 /* We only use "frame base" when we're sure we're talking about the
11798 post-prologue local stack frame. We do this by *not* running
11799 register elimination until this point, and recognizing the special
11800 argument pointer and soft frame pointer rtx's.
11801 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11802 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11803 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11805 dw_loc_descr_ref result = NULL;
11807 if (dwarf_version >= 4 || !dwarf_strict)
11809 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11810 initialized);
11811 if (result)
11812 add_loc_descr (&result,
11813 new_loc_descr (DW_OP_stack_value, 0, 0));
11815 return result;
11818 regs = targetm.dwarf_register_span (rtl);
11820 if (REG_NREGS (rtl) > 1 || regs)
11821 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11822 else
11824 unsigned int dbx_regnum = dbx_reg_number (rtl);
11825 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11826 return 0;
11827 return one_reg_loc_descriptor (dbx_regnum, initialized);
11831 /* Return a location descriptor that designates a machine register for
11832 a given hard register number. */
11834 static dw_loc_descr_ref
11835 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11837 dw_loc_descr_ref reg_loc_descr;
11839 if (regno <= 31)
11840 reg_loc_descr
11841 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11842 else
11843 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11845 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11846 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11848 return reg_loc_descr;
11851 /* Given an RTL of a register, return a location descriptor that
11852 designates a value that spans more than one register. */
11854 static dw_loc_descr_ref
11855 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11856 enum var_init_status initialized)
11858 int size, i;
11859 dw_loc_descr_ref loc_result = NULL;
11861 /* Simple, contiguous registers. */
11862 if (regs == NULL_RTX)
11864 unsigned reg = REGNO (rtl);
11865 int nregs;
11867 #ifdef LEAF_REG_REMAP
11868 if (crtl->uses_only_leaf_regs)
11870 int leaf_reg = LEAF_REG_REMAP (reg);
11871 if (leaf_reg != -1)
11872 reg = (unsigned) leaf_reg;
11874 #endif
11876 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11877 nregs = REG_NREGS (rtl);
11879 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11881 loc_result = NULL;
11882 while (nregs--)
11884 dw_loc_descr_ref t;
11886 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11887 VAR_INIT_STATUS_INITIALIZED);
11888 add_loc_descr (&loc_result, t);
11889 add_loc_descr_op_piece (&loc_result, size);
11890 ++reg;
11892 return loc_result;
11895 /* Now onto stupid register sets in non contiguous locations. */
11897 gcc_assert (GET_CODE (regs) == PARALLEL);
11899 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11900 loc_result = NULL;
11902 for (i = 0; i < XVECLEN (regs, 0); ++i)
11904 dw_loc_descr_ref t;
11906 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11907 VAR_INIT_STATUS_INITIALIZED);
11908 add_loc_descr (&loc_result, t);
11909 add_loc_descr_op_piece (&loc_result, size);
11912 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11913 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11914 return loc_result;
11917 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11919 /* Return a location descriptor that designates a constant i,
11920 as a compound operation from constant (i >> shift), constant shift
11921 and DW_OP_shl. */
11923 static dw_loc_descr_ref
11924 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11926 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11927 add_loc_descr (&ret, int_loc_descriptor (shift));
11928 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11929 return ret;
11932 /* Return a location descriptor that designates a constant. */
11934 static dw_loc_descr_ref
11935 int_loc_descriptor (HOST_WIDE_INT i)
11937 enum dwarf_location_atom op;
11939 /* Pick the smallest representation of a constant, rather than just
11940 defaulting to the LEB encoding. */
11941 if (i >= 0)
11943 int clz = clz_hwi (i);
11944 int ctz = ctz_hwi (i);
11945 if (i <= 31)
11946 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11947 else if (i <= 0xff)
11948 op = DW_OP_const1u;
11949 else if (i <= 0xffff)
11950 op = DW_OP_const2u;
11951 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11952 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11953 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11954 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11955 while DW_OP_const4u is 5 bytes. */
11956 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11957 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11958 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11959 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11960 while DW_OP_const4u is 5 bytes. */
11961 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11962 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11963 op = DW_OP_const4u;
11964 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11965 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11966 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11967 while DW_OP_constu of constant >= 0x100000000 takes at least
11968 6 bytes. */
11969 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11970 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11971 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11972 >= HOST_BITS_PER_WIDE_INT)
11973 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11974 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11975 while DW_OP_constu takes in this case at least 6 bytes. */
11976 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11977 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11978 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11979 && size_of_uleb128 (i) > 6)
11980 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11981 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11982 else
11983 op = DW_OP_constu;
11985 else
11987 if (i >= -0x80)
11988 op = DW_OP_const1s;
11989 else if (i >= -0x8000)
11990 op = DW_OP_const2s;
11991 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11993 if (size_of_int_loc_descriptor (i) < 5)
11995 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11996 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11997 return ret;
11999 op = DW_OP_const4s;
12001 else
12003 if (size_of_int_loc_descriptor (i)
12004 < (unsigned long) 1 + size_of_sleb128 (i))
12006 dw_loc_descr_ref ret = int_loc_descriptor (-i);
12007 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12008 return ret;
12010 op = DW_OP_consts;
12014 return new_loc_descr (op, i, 0);
12017 /* Likewise, for unsigned constants. */
12019 static dw_loc_descr_ref
12020 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12022 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12023 const unsigned HOST_WIDE_INT max_uint
12024 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12026 /* If possible, use the clever signed constants handling. */
12027 if (i <= max_int)
12028 return int_loc_descriptor ((HOST_WIDE_INT) i);
12030 /* Here, we are left with positive numbers that cannot be represented as
12031 HOST_WIDE_INT, i.e.:
12032 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12034 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12035 whereas may be better to output a negative integer: thanks to integer
12036 wrapping, we know that:
12037 x = x - 2 ** DWARF2_ADDR_SIZE
12038 = x - 2 * (max (HOST_WIDE_INT) + 1)
12039 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12040 small negative integers. Let's try that in cases it will clearly improve
12041 the encoding: there is no gain turning DW_OP_const4u into
12042 DW_OP_const4s. */
12043 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12044 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12045 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12047 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12049 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
12050 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
12051 const HOST_WIDE_INT second_shift
12052 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12054 /* So we finally have:
12055 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12056 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
12057 return int_loc_descriptor (second_shift);
12060 /* Last chance: fallback to a simple constant operation. */
12061 return new_loc_descr
12062 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12063 ? DW_OP_const4u
12064 : DW_OP_const8u,
12065 i, 0);
12068 /* Generate and return a location description that computes the unsigned
12069 comparison of the two stack top entries (a OP b where b is the top-most
12070 entry and a is the second one). The KIND of comparison can be LT_EXPR,
12071 LE_EXPR, GT_EXPR or GE_EXPR. */
12073 static dw_loc_descr_ref
12074 uint_comparison_loc_list (enum tree_code kind)
12076 enum dwarf_location_atom op, flip_op;
12077 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12079 switch (kind)
12081 case LT_EXPR:
12082 op = DW_OP_lt;
12083 break;
12084 case LE_EXPR:
12085 op = DW_OP_le;
12086 break;
12087 case GT_EXPR:
12088 op = DW_OP_gt;
12089 break;
12090 case GE_EXPR:
12091 op = DW_OP_ge;
12092 break;
12093 default:
12094 gcc_unreachable ();
12097 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12098 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12100 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
12101 possible to perform unsigned comparisons: we just have to distinguish
12102 three cases:
12104 1. when a and b have the same sign (as signed integers); then we should
12105 return: a OP(signed) b;
12107 2. when a is a negative signed integer while b is a positive one, then a
12108 is a greater unsigned integer than b; likewise when a and b's roles
12109 are flipped.
12111 So first, compare the sign of the two operands. */
12112 ret = new_loc_descr (DW_OP_over, 0, 0);
12113 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12114 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12115 /* If they have different signs (i.e. they have different sign bits), then
12116 the stack top value has now the sign bit set and thus it's smaller than
12117 zero. */
12118 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12119 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12120 add_loc_descr (&ret, bra_node);
12122 /* We are in case 1. At this point, we know both operands have the same
12123 sign, to it's safe to use the built-in signed comparison. */
12124 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12125 add_loc_descr (&ret, jmp_node);
12127 /* We are in case 2. Here, we know both operands do not have the same sign,
12128 so we have to flip the signed comparison. */
12129 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12130 tmp = new_loc_descr (flip_op, 0, 0);
12131 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12132 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12133 add_loc_descr (&ret, tmp);
12135 /* This dummy operation is necessary to make the two branches join. */
12136 tmp = new_loc_descr (DW_OP_nop, 0, 0);
12137 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12138 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12139 add_loc_descr (&ret, tmp);
12141 return ret;
12144 /* Likewise, but takes the location description lists (might be destructive on
12145 them). Return NULL if either is NULL or if concatenation fails. */
12147 static dw_loc_list_ref
12148 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12149 enum tree_code kind)
12151 if (left == NULL || right == NULL)
12152 return NULL;
12154 add_loc_list (&left, right);
12155 if (left == NULL)
12156 return NULL;
12158 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12159 return left;
12162 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12163 without actually allocating it. */
12165 static unsigned long
12166 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12168 return size_of_int_loc_descriptor (i >> shift)
12169 + size_of_int_loc_descriptor (shift)
12170 + 1;
12173 /* Return size_of_locs (int_loc_descriptor (i)) without
12174 actually allocating it. */
12176 static unsigned long
12177 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12179 unsigned long s;
12181 if (i >= 0)
12183 int clz, ctz;
12184 if (i <= 31)
12185 return 1;
12186 else if (i <= 0xff)
12187 return 2;
12188 else if (i <= 0xffff)
12189 return 3;
12190 clz = clz_hwi (i);
12191 ctz = ctz_hwi (i);
12192 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12193 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12194 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12195 - clz - 5);
12196 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12197 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12198 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12199 - clz - 8);
12200 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12201 return 5;
12202 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12203 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12204 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12205 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12206 - clz - 8);
12207 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12208 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12209 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12210 - clz - 16);
12211 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12212 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12213 && s > 6)
12214 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12215 - clz - 32);
12216 else
12217 return 1 + s;
12219 else
12221 if (i >= -0x80)
12222 return 2;
12223 else if (i >= -0x8000)
12224 return 3;
12225 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12227 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12229 s = size_of_int_loc_descriptor (-i) + 1;
12230 if (s < 5)
12231 return s;
12233 return 5;
12235 else
12237 unsigned long r = 1 + size_of_sleb128 (i);
12238 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12240 s = size_of_int_loc_descriptor (-i) + 1;
12241 if (s < r)
12242 return s;
12244 return r;
12249 /* Return loc description representing "address" of integer value.
12250 This can appear only as toplevel expression. */
12252 static dw_loc_descr_ref
12253 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12255 int litsize;
12256 dw_loc_descr_ref loc_result = NULL;
12258 if (!(dwarf_version >= 4 || !dwarf_strict))
12259 return NULL;
12261 litsize = size_of_int_loc_descriptor (i);
12262 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12263 is more compact. For DW_OP_stack_value we need:
12264 litsize + 1 (DW_OP_stack_value)
12265 and for DW_OP_implicit_value:
12266 1 (DW_OP_implicit_value) + 1 (length) + size. */
12267 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12269 loc_result = int_loc_descriptor (i);
12270 add_loc_descr (&loc_result,
12271 new_loc_descr (DW_OP_stack_value, 0, 0));
12272 return loc_result;
12275 loc_result = new_loc_descr (DW_OP_implicit_value,
12276 size, 0);
12277 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12278 loc_result->dw_loc_oprnd2.v.val_int = i;
12279 return loc_result;
12282 /* Return a location descriptor that designates a base+offset location. */
12284 static dw_loc_descr_ref
12285 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12286 enum var_init_status initialized)
12288 unsigned int regno;
12289 dw_loc_descr_ref result;
12290 dw_fde_ref fde = cfun->fde;
12292 /* We only use "frame base" when we're sure we're talking about the
12293 post-prologue local stack frame. We do this by *not* running
12294 register elimination until this point, and recognizing the special
12295 argument pointer and soft frame pointer rtx's. */
12296 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12298 rtx elim = (ira_use_lra_p
12299 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12300 : eliminate_regs (reg, VOIDmode, NULL_RTX));
12302 if (elim != reg)
12304 if (GET_CODE (elim) == PLUS)
12306 offset += INTVAL (XEXP (elim, 1));
12307 elim = XEXP (elim, 0);
12309 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12310 && (elim == hard_frame_pointer_rtx
12311 || elim == stack_pointer_rtx))
12312 || elim == (frame_pointer_needed
12313 ? hard_frame_pointer_rtx
12314 : stack_pointer_rtx));
12316 /* If drap register is used to align stack, use frame
12317 pointer + offset to access stack variables. If stack
12318 is aligned without drap, use stack pointer + offset to
12319 access stack variables. */
12320 if (crtl->stack_realign_tried
12321 && reg == frame_pointer_rtx)
12323 int base_reg
12324 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12325 ? HARD_FRAME_POINTER_REGNUM
12326 : REGNO (elim));
12327 return new_reg_loc_descr (base_reg, offset);
12330 gcc_assert (frame_pointer_fb_offset_valid);
12331 offset += frame_pointer_fb_offset;
12332 return new_loc_descr (DW_OP_fbreg, offset, 0);
12336 regno = REGNO (reg);
12337 #ifdef LEAF_REG_REMAP
12338 if (crtl->uses_only_leaf_regs)
12340 int leaf_reg = LEAF_REG_REMAP (regno);
12341 if (leaf_reg != -1)
12342 regno = (unsigned) leaf_reg;
12344 #endif
12345 regno = DWARF_FRAME_REGNUM (regno);
12347 if (!optimize && fde
12348 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12350 /* Use cfa+offset to represent the location of arguments passed
12351 on the stack when drap is used to align stack.
12352 Only do this when not optimizing, for optimized code var-tracking
12353 is supposed to track where the arguments live and the register
12354 used as vdrap or drap in some spot might be used for something
12355 else in other part of the routine. */
12356 return new_loc_descr (DW_OP_fbreg, offset, 0);
12359 if (regno <= 31)
12360 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12361 offset, 0);
12362 else
12363 result = new_loc_descr (DW_OP_bregx, regno, offset);
12365 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12366 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12368 return result;
12371 /* Return true if this RTL expression describes a base+offset calculation. */
12373 static inline int
12374 is_based_loc (const_rtx rtl)
12376 return (GET_CODE (rtl) == PLUS
12377 && ((REG_P (XEXP (rtl, 0))
12378 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12379 && CONST_INT_P (XEXP (rtl, 1)))));
12382 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12383 failed. */
12385 static dw_loc_descr_ref
12386 tls_mem_loc_descriptor (rtx mem)
12388 tree base;
12389 dw_loc_descr_ref loc_result;
12391 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12392 return NULL;
12394 base = get_base_address (MEM_EXPR (mem));
12395 if (base == NULL
12396 || TREE_CODE (base) != VAR_DECL
12397 || !DECL_THREAD_LOCAL_P (base))
12398 return NULL;
12400 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12401 if (loc_result == NULL)
12402 return NULL;
12404 if (MEM_OFFSET (mem))
12405 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12407 return loc_result;
12410 /* Output debug info about reason why we failed to expand expression as dwarf
12411 expression. */
12413 static void
12414 expansion_failed (tree expr, rtx rtl, char const *reason)
12416 if (dump_file && (dump_flags & TDF_DETAILS))
12418 fprintf (dump_file, "Failed to expand as dwarf: ");
12419 if (expr)
12420 print_generic_expr (dump_file, expr, dump_flags);
12421 if (rtl)
12423 fprintf (dump_file, "\n");
12424 print_rtl (dump_file, rtl);
12426 fprintf (dump_file, "\nReason: %s\n", reason);
12430 /* Helper function for const_ok_for_output. */
12432 static bool
12433 const_ok_for_output_1 (rtx rtl)
12435 if (GET_CODE (rtl) == UNSPEC)
12437 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12438 we can't express it in the debug info. */
12439 /* Don't complain about TLS UNSPECs, those are just too hard to
12440 delegitimize. Note this could be a non-decl SYMBOL_REF such as
12441 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12442 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
12443 if (flag_checking
12444 && (XVECLEN (rtl, 0) == 0
12445 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12446 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12447 inform (current_function_decl
12448 ? DECL_SOURCE_LOCATION (current_function_decl)
12449 : UNKNOWN_LOCATION,
12450 #if NUM_UNSPEC_VALUES > 0
12451 "non-delegitimized UNSPEC %s (%d) found in variable location",
12452 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12453 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12454 XINT (rtl, 1));
12455 #else
12456 "non-delegitimized UNSPEC %d found in variable location",
12457 XINT (rtl, 1));
12458 #endif
12459 expansion_failed (NULL_TREE, rtl,
12460 "UNSPEC hasn't been delegitimized.\n");
12461 return false;
12464 if (targetm.const_not_ok_for_debug_p (rtl))
12466 expansion_failed (NULL_TREE, rtl,
12467 "Expression rejected for debug by the backend.\n");
12468 return false;
12471 /* FIXME: Refer to PR60655. It is possible for simplification
12472 of rtl expressions in var tracking to produce such expressions.
12473 We should really identify / validate expressions
12474 enclosed in CONST that can be handled by assemblers on various
12475 targets and only handle legitimate cases here. */
12476 if (GET_CODE (rtl) != SYMBOL_REF)
12478 if (GET_CODE (rtl) == NOT)
12479 return false;
12480 return true;
12483 if (CONSTANT_POOL_ADDRESS_P (rtl))
12485 bool marked;
12486 get_pool_constant_mark (rtl, &marked);
12487 /* If all references to this pool constant were optimized away,
12488 it was not output and thus we can't represent it. */
12489 if (!marked)
12491 expansion_failed (NULL_TREE, rtl,
12492 "Constant was removed from constant pool.\n");
12493 return false;
12497 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12498 return false;
12500 /* Avoid references to external symbols in debug info, on several targets
12501 the linker might even refuse to link when linking a shared library,
12502 and in many other cases the relocations for .debug_info/.debug_loc are
12503 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
12504 to be defined within the same shared library or executable are fine. */
12505 if (SYMBOL_REF_EXTERNAL_P (rtl))
12507 tree decl = SYMBOL_REF_DECL (rtl);
12509 if (decl == NULL || !targetm.binds_local_p (decl))
12511 expansion_failed (NULL_TREE, rtl,
12512 "Symbol not defined in current TU.\n");
12513 return false;
12517 return true;
12520 /* Return true if constant RTL can be emitted in DW_OP_addr or
12521 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
12522 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
12524 static bool
12525 const_ok_for_output (rtx rtl)
12527 if (GET_CODE (rtl) == SYMBOL_REF)
12528 return const_ok_for_output_1 (rtl);
12530 if (GET_CODE (rtl) == CONST)
12532 subrtx_var_iterator::array_type array;
12533 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12534 if (!const_ok_for_output_1 (*iter))
12535 return false;
12536 return true;
12539 return true;
12542 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12543 if possible, NULL otherwise. */
12545 static dw_die_ref
12546 base_type_for_mode (machine_mode mode, bool unsignedp)
12548 dw_die_ref type_die;
12549 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12551 if (type == NULL)
12552 return NULL;
12553 switch (TREE_CODE (type))
12555 case INTEGER_TYPE:
12556 case REAL_TYPE:
12557 break;
12558 default:
12559 return NULL;
12561 type_die = lookup_type_die (type);
12562 if (!type_die)
12563 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12564 comp_unit_die ());
12565 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12566 return NULL;
12567 return type_die;
12570 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12571 type matching MODE, or, if MODE is narrower than or as wide as
12572 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
12573 possible. */
12575 static dw_loc_descr_ref
12576 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12578 machine_mode outer_mode = mode;
12579 dw_die_ref type_die;
12580 dw_loc_descr_ref cvt;
12582 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12584 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12585 return op;
12587 type_die = base_type_for_mode (outer_mode, 1);
12588 if (type_die == NULL)
12589 return NULL;
12590 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12591 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12592 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12593 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12594 add_loc_descr (&op, cvt);
12595 return op;
12598 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
12600 static dw_loc_descr_ref
12601 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12602 dw_loc_descr_ref op1)
12604 dw_loc_descr_ref ret = op0;
12605 add_loc_descr (&ret, op1);
12606 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12607 if (STORE_FLAG_VALUE != 1)
12609 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12610 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12612 return ret;
12615 /* Return location descriptor for signed comparison OP RTL. */
12617 static dw_loc_descr_ref
12618 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12619 machine_mode mem_mode)
12621 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12622 dw_loc_descr_ref op0, op1;
12623 int shift;
12625 if (op_mode == VOIDmode)
12626 op_mode = GET_MODE (XEXP (rtl, 1));
12627 if (op_mode == VOIDmode)
12628 return NULL;
12630 if (dwarf_strict
12631 && (!SCALAR_INT_MODE_P (op_mode)
12632 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12633 return NULL;
12635 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12636 VAR_INIT_STATUS_INITIALIZED);
12637 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12638 VAR_INIT_STATUS_INITIALIZED);
12640 if (op0 == NULL || op1 == NULL)
12641 return NULL;
12643 if (!SCALAR_INT_MODE_P (op_mode)
12644 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12645 return compare_loc_descriptor (op, op0, op1);
12647 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12649 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12650 dw_loc_descr_ref cvt;
12652 if (type_die == NULL)
12653 return NULL;
12654 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12655 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12656 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12657 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12658 add_loc_descr (&op0, cvt);
12659 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12660 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12661 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12662 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12663 add_loc_descr (&op1, cvt);
12664 return compare_loc_descriptor (op, op0, op1);
12667 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12668 /* For eq/ne, if the operands are known to be zero-extended,
12669 there is no need to do the fancy shifting up. */
12670 if (op == DW_OP_eq || op == DW_OP_ne)
12672 dw_loc_descr_ref last0, last1;
12673 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12675 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12677 /* deref_size zero extends, and for constants we can check
12678 whether they are zero extended or not. */
12679 if (((last0->dw_loc_opc == DW_OP_deref_size
12680 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12681 || (CONST_INT_P (XEXP (rtl, 0))
12682 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12683 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12684 && ((last1->dw_loc_opc == DW_OP_deref_size
12685 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12686 || (CONST_INT_P (XEXP (rtl, 1))
12687 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12688 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12689 return compare_loc_descriptor (op, op0, op1);
12691 /* EQ/NE comparison against constant in narrower type than
12692 DWARF2_ADDR_SIZE can be performed either as
12693 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12694 DW_OP_{eq,ne}
12696 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12697 DW_OP_{eq,ne}. Pick whatever is shorter. */
12698 if (CONST_INT_P (XEXP (rtl, 1))
12699 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12700 && (size_of_int_loc_descriptor (shift) + 1
12701 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12702 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12703 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12704 & GET_MODE_MASK (op_mode))))
12706 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12707 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12708 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12709 & GET_MODE_MASK (op_mode));
12710 return compare_loc_descriptor (op, op0, op1);
12713 add_loc_descr (&op0, int_loc_descriptor (shift));
12714 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12715 if (CONST_INT_P (XEXP (rtl, 1)))
12716 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12717 else
12719 add_loc_descr (&op1, int_loc_descriptor (shift));
12720 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12722 return compare_loc_descriptor (op, op0, op1);
12725 /* Return location descriptor for unsigned comparison OP RTL. */
12727 static dw_loc_descr_ref
12728 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12729 machine_mode mem_mode)
12731 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12732 dw_loc_descr_ref op0, op1;
12734 if (op_mode == VOIDmode)
12735 op_mode = GET_MODE (XEXP (rtl, 1));
12736 if (op_mode == VOIDmode)
12737 return NULL;
12738 if (!SCALAR_INT_MODE_P (op_mode))
12739 return NULL;
12741 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12742 return NULL;
12744 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12745 VAR_INIT_STATUS_INITIALIZED);
12746 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12747 VAR_INIT_STATUS_INITIALIZED);
12749 if (op0 == NULL || op1 == NULL)
12750 return NULL;
12752 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12754 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12755 dw_loc_descr_ref last0, last1;
12756 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12758 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12760 if (CONST_INT_P (XEXP (rtl, 0)))
12761 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12762 /* deref_size zero extends, so no need to mask it again. */
12763 else if (last0->dw_loc_opc != DW_OP_deref_size
12764 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12766 add_loc_descr (&op0, int_loc_descriptor (mask));
12767 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12769 if (CONST_INT_P (XEXP (rtl, 1)))
12770 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12771 /* deref_size zero extends, so no need to mask it again. */
12772 else if (last1->dw_loc_opc != DW_OP_deref_size
12773 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12775 add_loc_descr (&op1, int_loc_descriptor (mask));
12776 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12779 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12781 HOST_WIDE_INT bias = 1;
12782 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12783 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12784 if (CONST_INT_P (XEXP (rtl, 1)))
12785 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12786 + INTVAL (XEXP (rtl, 1)));
12787 else
12788 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12789 bias, 0));
12791 return compare_loc_descriptor (op, op0, op1);
12794 /* Return location descriptor for {U,S}{MIN,MAX}. */
12796 static dw_loc_descr_ref
12797 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12798 machine_mode mem_mode)
12800 enum dwarf_location_atom op;
12801 dw_loc_descr_ref op0, op1, ret;
12802 dw_loc_descr_ref bra_node, drop_node;
12804 if (dwarf_strict
12805 && (!SCALAR_INT_MODE_P (mode)
12806 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12807 return NULL;
12809 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12810 VAR_INIT_STATUS_INITIALIZED);
12811 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12812 VAR_INIT_STATUS_INITIALIZED);
12814 if (op0 == NULL || op1 == NULL)
12815 return NULL;
12817 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12818 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12819 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12820 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12822 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12824 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12825 add_loc_descr (&op0, int_loc_descriptor (mask));
12826 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12827 add_loc_descr (&op1, int_loc_descriptor (mask));
12828 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12830 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12832 HOST_WIDE_INT bias = 1;
12833 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12834 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12835 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12838 else if (!SCALAR_INT_MODE_P (mode)
12839 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12841 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12842 add_loc_descr (&op0, int_loc_descriptor (shift));
12843 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12844 add_loc_descr (&op1, int_loc_descriptor (shift));
12845 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12847 else if (SCALAR_INT_MODE_P (mode)
12848 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12850 dw_die_ref type_die = base_type_for_mode (mode, 0);
12851 dw_loc_descr_ref cvt;
12852 if (type_die == NULL)
12853 return NULL;
12854 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12855 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12856 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12857 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12858 add_loc_descr (&op0, cvt);
12859 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12860 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12861 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12862 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12863 add_loc_descr (&op1, cvt);
12866 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12867 op = DW_OP_lt;
12868 else
12869 op = DW_OP_gt;
12870 ret = op0;
12871 add_loc_descr (&ret, op1);
12872 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12873 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12874 add_loc_descr (&ret, bra_node);
12875 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12876 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12877 add_loc_descr (&ret, drop_node);
12878 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12879 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12880 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12881 && SCALAR_INT_MODE_P (mode)
12882 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12883 ret = convert_descriptor_to_mode (mode, ret);
12884 return ret;
12887 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12888 but after converting arguments to type_die, afterwards
12889 convert back to unsigned. */
12891 static dw_loc_descr_ref
12892 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12893 machine_mode mode, machine_mode mem_mode)
12895 dw_loc_descr_ref cvt, op0, op1;
12897 if (type_die == NULL)
12898 return NULL;
12899 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12900 VAR_INIT_STATUS_INITIALIZED);
12901 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12902 VAR_INIT_STATUS_INITIALIZED);
12903 if (op0 == NULL || op1 == NULL)
12904 return NULL;
12905 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12906 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12907 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12908 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12909 add_loc_descr (&op0, cvt);
12910 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12911 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12912 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12913 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12914 add_loc_descr (&op1, cvt);
12915 add_loc_descr (&op0, op1);
12916 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12917 return convert_descriptor_to_mode (mode, op0);
12920 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12921 const0 is DW_OP_lit0 or corresponding typed constant,
12922 const1 is DW_OP_lit1 or corresponding typed constant
12923 and constMSB is constant with just the MSB bit set
12924 for the mode):
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 constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
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 CTZ is similar:
12933 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12934 L1: const0 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 FFS is similar:
12941 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12942 L1: const1 DW_OP_swap
12943 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12944 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12945 L3: DW_OP_drop
12946 L4: DW_OP_nop */
12948 static dw_loc_descr_ref
12949 clz_loc_descriptor (rtx rtl, machine_mode mode,
12950 machine_mode mem_mode)
12952 dw_loc_descr_ref op0, ret, tmp;
12953 HOST_WIDE_INT valv;
12954 dw_loc_descr_ref l1jump, l1label;
12955 dw_loc_descr_ref l2jump, l2label;
12956 dw_loc_descr_ref l3jump, l3label;
12957 dw_loc_descr_ref l4jump, l4label;
12958 rtx msb;
12960 if (!SCALAR_INT_MODE_P (mode)
12961 || GET_MODE (XEXP (rtl, 0)) != mode)
12962 return NULL;
12964 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12965 VAR_INIT_STATUS_INITIALIZED);
12966 if (op0 == NULL)
12967 return NULL;
12968 ret = op0;
12969 if (GET_CODE (rtl) == CLZ)
12971 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12972 valv = GET_MODE_BITSIZE (mode);
12974 else if (GET_CODE (rtl) == FFS)
12975 valv = 0;
12976 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12977 valv = GET_MODE_BITSIZE (mode);
12978 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12979 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12980 add_loc_descr (&ret, l1jump);
12981 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12982 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12983 VAR_INIT_STATUS_INITIALIZED);
12984 if (tmp == NULL)
12985 return NULL;
12986 add_loc_descr (&ret, tmp);
12987 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12988 add_loc_descr (&ret, l4jump);
12989 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12990 ? const1_rtx : const0_rtx,
12991 mode, mem_mode,
12992 VAR_INIT_STATUS_INITIALIZED);
12993 if (l1label == NULL)
12994 return NULL;
12995 add_loc_descr (&ret, l1label);
12996 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12997 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12998 add_loc_descr (&ret, l2label);
12999 if (GET_CODE (rtl) != CLZ)
13000 msb = const1_rtx;
13001 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
13002 msb = GEN_INT (HOST_WIDE_INT_1U
13003 << (GET_MODE_BITSIZE (mode) - 1));
13004 else
13005 msb = immed_wide_int_const
13006 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
13007 GET_MODE_PRECISION (mode)), mode);
13008 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
13009 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13010 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
13011 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
13012 else
13013 tmp = mem_loc_descriptor (msb, 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 (DW_OP_and, 0, 0));
13019 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13020 add_loc_descr (&ret, l3jump);
13021 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13022 VAR_INIT_STATUS_INITIALIZED);
13023 if (tmp == NULL)
13024 return NULL;
13025 add_loc_descr (&ret, tmp);
13026 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13027 ? DW_OP_shl : DW_OP_shr, 0, 0));
13028 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13029 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13030 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13031 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13032 add_loc_descr (&ret, l2jump);
13033 l3label = new_loc_descr (DW_OP_drop, 0, 0);
13034 add_loc_descr (&ret, l3label);
13035 l4label = new_loc_descr (DW_OP_nop, 0, 0);
13036 add_loc_descr (&ret, l4label);
13037 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13038 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13039 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13040 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13041 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13042 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13043 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13044 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13045 return ret;
13048 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13049 const1 is DW_OP_lit1 or corresponding typed constant):
13050 const0 DW_OP_swap
13051 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13052 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13053 L2: DW_OP_drop
13055 PARITY is similar:
13056 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13057 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13058 L2: DW_OP_drop */
13060 static dw_loc_descr_ref
13061 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13062 machine_mode mem_mode)
13064 dw_loc_descr_ref op0, ret, tmp;
13065 dw_loc_descr_ref l1jump, l1label;
13066 dw_loc_descr_ref l2jump, l2label;
13068 if (!SCALAR_INT_MODE_P (mode)
13069 || GET_MODE (XEXP (rtl, 0)) != mode)
13070 return NULL;
13072 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13073 VAR_INIT_STATUS_INITIALIZED);
13074 if (op0 == NULL)
13075 return NULL;
13076 ret = op0;
13077 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13078 VAR_INIT_STATUS_INITIALIZED);
13079 if (tmp == NULL)
13080 return NULL;
13081 add_loc_descr (&ret, tmp);
13082 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13083 l1label = new_loc_descr (DW_OP_dup, 0, 0);
13084 add_loc_descr (&ret, l1label);
13085 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13086 add_loc_descr (&ret, l2jump);
13087 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13088 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13089 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13090 VAR_INIT_STATUS_INITIALIZED);
13091 if (tmp == NULL)
13092 return NULL;
13093 add_loc_descr (&ret, tmp);
13094 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13095 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13096 ? DW_OP_plus : DW_OP_xor, 0, 0));
13097 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13098 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13099 VAR_INIT_STATUS_INITIALIZED);
13100 add_loc_descr (&ret, tmp);
13101 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13102 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13103 add_loc_descr (&ret, l1jump);
13104 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13105 add_loc_descr (&ret, l2label);
13106 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13107 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13108 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13109 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13110 return ret;
13113 /* BSWAP (constS is initial shift count, either 56 or 24):
13114 constS const0
13115 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13116 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13117 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13118 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13119 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
13121 static dw_loc_descr_ref
13122 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13123 machine_mode mem_mode)
13125 dw_loc_descr_ref op0, ret, tmp;
13126 dw_loc_descr_ref l1jump, l1label;
13127 dw_loc_descr_ref l2jump, l2label;
13129 if (!SCALAR_INT_MODE_P (mode)
13130 || BITS_PER_UNIT != 8
13131 || (GET_MODE_BITSIZE (mode) != 32
13132 && GET_MODE_BITSIZE (mode) != 64))
13133 return NULL;
13135 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13136 VAR_INIT_STATUS_INITIALIZED);
13137 if (op0 == NULL)
13138 return NULL;
13140 ret = op0;
13141 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13142 mode, mem_mode,
13143 VAR_INIT_STATUS_INITIALIZED);
13144 if (tmp == NULL)
13145 return NULL;
13146 add_loc_descr (&ret, tmp);
13147 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13148 VAR_INIT_STATUS_INITIALIZED);
13149 if (tmp == NULL)
13150 return NULL;
13151 add_loc_descr (&ret, tmp);
13152 l1label = new_loc_descr (DW_OP_pick, 2, 0);
13153 add_loc_descr (&ret, l1label);
13154 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13155 mode, mem_mode,
13156 VAR_INIT_STATUS_INITIALIZED);
13157 add_loc_descr (&ret, tmp);
13158 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13159 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13160 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13161 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13162 VAR_INIT_STATUS_INITIALIZED);
13163 if (tmp == NULL)
13164 return NULL;
13165 add_loc_descr (&ret, tmp);
13166 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13167 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13168 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13169 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13170 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13171 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13172 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13173 VAR_INIT_STATUS_INITIALIZED);
13174 add_loc_descr (&ret, tmp);
13175 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13176 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13177 add_loc_descr (&ret, l2jump);
13178 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13179 VAR_INIT_STATUS_INITIALIZED);
13180 add_loc_descr (&ret, tmp);
13181 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13182 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13183 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13184 add_loc_descr (&ret, l1jump);
13185 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13186 add_loc_descr (&ret, l2label);
13187 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13188 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13189 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13190 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13191 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13192 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13193 return ret;
13196 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13197 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13198 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13199 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13201 ROTATERT is similar:
13202 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13203 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13204 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
13206 static dw_loc_descr_ref
13207 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13208 machine_mode mem_mode)
13210 rtx rtlop1 = XEXP (rtl, 1);
13211 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13212 int i;
13214 if (!SCALAR_INT_MODE_P (mode))
13215 return NULL;
13217 if (GET_MODE (rtlop1) != VOIDmode
13218 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13219 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13220 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13221 VAR_INIT_STATUS_INITIALIZED);
13222 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13223 VAR_INIT_STATUS_INITIALIZED);
13224 if (op0 == NULL || op1 == NULL)
13225 return NULL;
13226 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13227 for (i = 0; i < 2; i++)
13229 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13230 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13231 mode, mem_mode,
13232 VAR_INIT_STATUS_INITIALIZED);
13233 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13234 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13235 ? DW_OP_const4u
13236 : HOST_BITS_PER_WIDE_INT == 64
13237 ? DW_OP_const8u : DW_OP_constu,
13238 GET_MODE_MASK (mode), 0);
13239 else
13240 mask[i] = NULL;
13241 if (mask[i] == NULL)
13242 return NULL;
13243 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13245 ret = op0;
13246 add_loc_descr (&ret, op1);
13247 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13248 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13249 if (GET_CODE (rtl) == ROTATERT)
13251 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13252 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13253 GET_MODE_BITSIZE (mode), 0));
13255 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13256 if (mask[0] != NULL)
13257 add_loc_descr (&ret, mask[0]);
13258 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13259 if (mask[1] != NULL)
13261 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13262 add_loc_descr (&ret, mask[1]);
13263 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13265 if (GET_CODE (rtl) == ROTATE)
13267 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13268 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13269 GET_MODE_BITSIZE (mode), 0));
13271 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13272 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13273 return ret;
13276 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
13277 for DEBUG_PARAMETER_REF RTL. */
13279 static dw_loc_descr_ref
13280 parameter_ref_descriptor (rtx rtl)
13282 dw_loc_descr_ref ret;
13283 dw_die_ref ref;
13285 if (dwarf_strict)
13286 return NULL;
13287 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13288 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13289 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13290 if (ref)
13292 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13293 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13294 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13296 else
13298 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13299 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13301 return ret;
13304 /* The following routine converts the RTL for a variable or parameter
13305 (resident in memory) into an equivalent Dwarf representation of a
13306 mechanism for getting the address of that same variable onto the top of a
13307 hypothetical "address evaluation" stack.
13309 When creating memory location descriptors, we are effectively transforming
13310 the RTL for a memory-resident object into its Dwarf postfix expression
13311 equivalent. This routine recursively descends an RTL tree, turning
13312 it into Dwarf postfix code as it goes.
13314 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13316 MEM_MODE is the mode of the memory reference, needed to handle some
13317 autoincrement addressing modes.
13319 Return 0 if we can't represent the location. */
13321 dw_loc_descr_ref
13322 mem_loc_descriptor (rtx rtl, machine_mode mode,
13323 machine_mode mem_mode,
13324 enum var_init_status initialized)
13326 dw_loc_descr_ref mem_loc_result = NULL;
13327 enum dwarf_location_atom op;
13328 dw_loc_descr_ref op0, op1;
13329 rtx inner = NULL_RTX;
13331 if (mode == VOIDmode)
13332 mode = GET_MODE (rtl);
13334 /* Note that for a dynamically sized array, the location we will generate a
13335 description of here will be the lowest numbered location which is
13336 actually within the array. That's *not* necessarily the same as the
13337 zeroth element of the array. */
13339 rtl = targetm.delegitimize_address (rtl);
13341 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13342 return NULL;
13344 switch (GET_CODE (rtl))
13346 case POST_INC:
13347 case POST_DEC:
13348 case POST_MODIFY:
13349 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13351 case SUBREG:
13352 /* The case of a subreg may arise when we have a local (register)
13353 variable or a formal (register) parameter which doesn't quite fill
13354 up an entire register. For now, just assume that it is
13355 legitimate to make the Dwarf info refer to the whole register which
13356 contains the given subreg. */
13357 if (!subreg_lowpart_p (rtl))
13358 break;
13359 inner = SUBREG_REG (rtl);
13360 /* FALLTHRU */
13361 case TRUNCATE:
13362 if (inner == NULL_RTX)
13363 inner = XEXP (rtl, 0);
13364 if (SCALAR_INT_MODE_P (mode)
13365 && SCALAR_INT_MODE_P (GET_MODE (inner))
13366 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13367 #ifdef POINTERS_EXTEND_UNSIGNED
13368 || (mode == Pmode && mem_mode != VOIDmode)
13369 #endif
13371 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13373 mem_loc_result = mem_loc_descriptor (inner,
13374 GET_MODE (inner),
13375 mem_mode, initialized);
13376 break;
13378 if (dwarf_strict)
13379 break;
13380 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13381 break;
13382 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13383 && (!SCALAR_INT_MODE_P (mode)
13384 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
13385 break;
13386 else
13388 dw_die_ref type_die;
13389 dw_loc_descr_ref cvt;
13391 mem_loc_result = mem_loc_descriptor (inner,
13392 GET_MODE (inner),
13393 mem_mode, initialized);
13394 if (mem_loc_result == NULL)
13395 break;
13396 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13397 if (type_die == NULL)
13399 mem_loc_result = NULL;
13400 break;
13402 if (GET_MODE_SIZE (mode)
13403 != GET_MODE_SIZE (GET_MODE (inner)))
13404 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13405 else
13406 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13407 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13408 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13409 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13410 add_loc_descr (&mem_loc_result, cvt);
13411 if (SCALAR_INT_MODE_P (mode)
13412 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13414 /* Convert it to untyped afterwards. */
13415 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13416 add_loc_descr (&mem_loc_result, cvt);
13419 break;
13421 case REG:
13422 if (! SCALAR_INT_MODE_P (mode)
13423 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13424 && rtl != arg_pointer_rtx
13425 && rtl != frame_pointer_rtx
13426 #ifdef POINTERS_EXTEND_UNSIGNED
13427 && (mode != Pmode || mem_mode == VOIDmode)
13428 #endif
13431 dw_die_ref type_die;
13432 unsigned int dbx_regnum;
13434 if (dwarf_strict)
13435 break;
13436 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13437 break;
13438 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13439 if (type_die == NULL)
13440 break;
13442 dbx_regnum = dbx_reg_number (rtl);
13443 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13444 break;
13445 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13446 dbx_regnum, 0);
13447 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13448 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13449 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13450 break;
13452 /* Whenever a register number forms a part of the description of the
13453 method for calculating the (dynamic) address of a memory resident
13454 object, DWARF rules require the register number be referred to as
13455 a "base register". This distinction is not based in any way upon
13456 what category of register the hardware believes the given register
13457 belongs to. This is strictly DWARF terminology we're dealing with
13458 here. Note that in cases where the location of a memory-resident
13459 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13460 OP_CONST (0)) the actual DWARF location descriptor that we generate
13461 may just be OP_BASEREG (basereg). This may look deceptively like
13462 the object in question was allocated to a register (rather than in
13463 memory) so DWARF consumers need to be aware of the subtle
13464 distinction between OP_REG and OP_BASEREG. */
13465 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13466 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13467 else if (stack_realign_drap
13468 && crtl->drap_reg
13469 && crtl->args.internal_arg_pointer == rtl
13470 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13472 /* If RTL is internal_arg_pointer, which has been optimized
13473 out, use DRAP instead. */
13474 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13475 VAR_INIT_STATUS_INITIALIZED);
13477 break;
13479 case SIGN_EXTEND:
13480 case ZERO_EXTEND:
13481 if (!SCALAR_INT_MODE_P (mode))
13482 break;
13483 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13484 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13485 if (op0 == 0)
13486 break;
13487 else if (GET_CODE (rtl) == ZERO_EXTEND
13488 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13489 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13490 < HOST_BITS_PER_WIDE_INT
13491 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13492 to expand zero extend as two shifts instead of
13493 masking. */
13494 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13496 machine_mode imode = GET_MODE (XEXP (rtl, 0));
13497 mem_loc_result = op0;
13498 add_loc_descr (&mem_loc_result,
13499 int_loc_descriptor (GET_MODE_MASK (imode)));
13500 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13502 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13504 int shift = DWARF2_ADDR_SIZE
13505 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13506 shift *= BITS_PER_UNIT;
13507 if (GET_CODE (rtl) == SIGN_EXTEND)
13508 op = DW_OP_shra;
13509 else
13510 op = DW_OP_shr;
13511 mem_loc_result = op0;
13512 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13513 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13514 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13515 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13517 else if (!dwarf_strict)
13519 dw_die_ref type_die1, type_die2;
13520 dw_loc_descr_ref cvt;
13522 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13523 GET_CODE (rtl) == ZERO_EXTEND);
13524 if (type_die1 == NULL)
13525 break;
13526 type_die2 = base_type_for_mode (mode, 1);
13527 if (type_die2 == NULL)
13528 break;
13529 mem_loc_result = op0;
13530 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13531 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13532 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13533 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13534 add_loc_descr (&mem_loc_result, cvt);
13535 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13536 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13537 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13538 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13539 add_loc_descr (&mem_loc_result, cvt);
13541 break;
13543 case MEM:
13545 rtx new_rtl = avoid_constant_pool_reference (rtl);
13546 if (new_rtl != rtl)
13548 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13549 initialized);
13550 if (mem_loc_result != NULL)
13551 return mem_loc_result;
13554 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13555 get_address_mode (rtl), mode,
13556 VAR_INIT_STATUS_INITIALIZED);
13557 if (mem_loc_result == NULL)
13558 mem_loc_result = tls_mem_loc_descriptor (rtl);
13559 if (mem_loc_result != NULL)
13561 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13562 || !SCALAR_INT_MODE_P(mode))
13564 dw_die_ref type_die;
13565 dw_loc_descr_ref deref;
13567 if (dwarf_strict)
13568 return NULL;
13569 type_die
13570 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13571 if (type_die == NULL)
13572 return NULL;
13573 deref = new_loc_descr (DW_OP_GNU_deref_type,
13574 GET_MODE_SIZE (mode), 0);
13575 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13576 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13577 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13578 add_loc_descr (&mem_loc_result, deref);
13580 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13581 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13582 else
13583 add_loc_descr (&mem_loc_result,
13584 new_loc_descr (DW_OP_deref_size,
13585 GET_MODE_SIZE (mode), 0));
13587 break;
13589 case LO_SUM:
13590 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13592 case LABEL_REF:
13593 /* Some ports can transform a symbol ref into a label ref, because
13594 the symbol ref is too far away and has to be dumped into a constant
13595 pool. */
13596 case CONST:
13597 case SYMBOL_REF:
13598 if (!SCALAR_INT_MODE_P (mode)
13599 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13600 #ifdef POINTERS_EXTEND_UNSIGNED
13601 && (mode != Pmode || mem_mode == VOIDmode)
13602 #endif
13604 break;
13605 if (GET_CODE (rtl) == SYMBOL_REF
13606 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13608 dw_loc_descr_ref temp;
13610 /* If this is not defined, we have no way to emit the data. */
13611 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13612 break;
13614 temp = new_addr_loc_descr (rtl, dtprel_true);
13616 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13617 add_loc_descr (&mem_loc_result, temp);
13619 break;
13622 if (!const_ok_for_output (rtl))
13624 if (GET_CODE (rtl) == CONST)
13625 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13626 initialized);
13627 break;
13630 symref:
13631 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13632 vec_safe_push (used_rtx_array, rtl);
13633 break;
13635 case CONCAT:
13636 case CONCATN:
13637 case VAR_LOCATION:
13638 case DEBUG_IMPLICIT_PTR:
13639 expansion_failed (NULL_TREE, rtl,
13640 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13641 return 0;
13643 case ENTRY_VALUE:
13644 if (dwarf_strict)
13645 return NULL;
13646 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13648 if (!SCALAR_INT_MODE_P (mode)
13649 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13650 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13651 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13652 else
13654 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13655 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13656 return NULL;
13657 op0 = one_reg_loc_descriptor (dbx_regnum,
13658 VAR_INIT_STATUS_INITIALIZED);
13661 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13662 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13664 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13665 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13666 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13667 return NULL;
13669 else
13670 gcc_unreachable ();
13671 if (op0 == NULL)
13672 return NULL;
13673 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13674 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13675 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13676 break;
13678 case DEBUG_PARAMETER_REF:
13679 mem_loc_result = parameter_ref_descriptor (rtl);
13680 break;
13682 case PRE_MODIFY:
13683 /* Extract the PLUS expression nested inside and fall into
13684 PLUS code below. */
13685 rtl = XEXP (rtl, 1);
13686 goto plus;
13688 case PRE_INC:
13689 case PRE_DEC:
13690 /* Turn these into a PLUS expression and fall into the PLUS code
13691 below. */
13692 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13693 gen_int_mode (GET_CODE (rtl) == PRE_INC
13694 ? GET_MODE_UNIT_SIZE (mem_mode)
13695 : -GET_MODE_UNIT_SIZE (mem_mode),
13696 mode));
13698 /* fall through */
13700 case PLUS:
13701 plus:
13702 if (is_based_loc (rtl)
13703 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13704 || XEXP (rtl, 0) == arg_pointer_rtx
13705 || XEXP (rtl, 0) == frame_pointer_rtx)
13706 && SCALAR_INT_MODE_P (mode))
13707 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13708 INTVAL (XEXP (rtl, 1)),
13709 VAR_INIT_STATUS_INITIALIZED);
13710 else
13712 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13713 VAR_INIT_STATUS_INITIALIZED);
13714 if (mem_loc_result == 0)
13715 break;
13717 if (CONST_INT_P (XEXP (rtl, 1))
13718 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13719 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13720 else
13722 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13723 VAR_INIT_STATUS_INITIALIZED);
13724 if (op1 == 0)
13725 return NULL;
13726 add_loc_descr (&mem_loc_result, op1);
13727 add_loc_descr (&mem_loc_result,
13728 new_loc_descr (DW_OP_plus, 0, 0));
13731 break;
13733 /* If a pseudo-reg is optimized away, it is possible for it to
13734 be replaced with a MEM containing a multiply or shift. */
13735 case MINUS:
13736 op = DW_OP_minus;
13737 goto do_binop;
13739 case MULT:
13740 op = DW_OP_mul;
13741 goto do_binop;
13743 case DIV:
13744 if (!dwarf_strict
13745 && SCALAR_INT_MODE_P (mode)
13746 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13748 mem_loc_result = typed_binop (DW_OP_div, rtl,
13749 base_type_for_mode (mode, 0),
13750 mode, mem_mode);
13751 break;
13753 op = DW_OP_div;
13754 goto do_binop;
13756 case UMOD:
13757 op = DW_OP_mod;
13758 goto do_binop;
13760 case ASHIFT:
13761 op = DW_OP_shl;
13762 goto do_shift;
13764 case ASHIFTRT:
13765 op = DW_OP_shra;
13766 goto do_shift;
13768 case LSHIFTRT:
13769 op = DW_OP_shr;
13770 goto do_shift;
13772 do_shift:
13773 if (!SCALAR_INT_MODE_P (mode))
13774 break;
13775 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13776 VAR_INIT_STATUS_INITIALIZED);
13778 rtx rtlop1 = XEXP (rtl, 1);
13779 if (GET_MODE (rtlop1) != VOIDmode
13780 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13781 < GET_MODE_BITSIZE (mode))
13782 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13783 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13784 VAR_INIT_STATUS_INITIALIZED);
13787 if (op0 == 0 || op1 == 0)
13788 break;
13790 mem_loc_result = op0;
13791 add_loc_descr (&mem_loc_result, op1);
13792 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13793 break;
13795 case AND:
13796 op = DW_OP_and;
13797 goto do_binop;
13799 case IOR:
13800 op = DW_OP_or;
13801 goto do_binop;
13803 case XOR:
13804 op = DW_OP_xor;
13805 goto do_binop;
13807 do_binop:
13808 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13809 VAR_INIT_STATUS_INITIALIZED);
13810 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13811 VAR_INIT_STATUS_INITIALIZED);
13813 if (op0 == 0 || op1 == 0)
13814 break;
13816 mem_loc_result = op0;
13817 add_loc_descr (&mem_loc_result, op1);
13818 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13819 break;
13821 case MOD:
13822 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13824 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13825 base_type_for_mode (mode, 0),
13826 mode, mem_mode);
13827 break;
13830 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13831 VAR_INIT_STATUS_INITIALIZED);
13832 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13833 VAR_INIT_STATUS_INITIALIZED);
13835 if (op0 == 0 || op1 == 0)
13836 break;
13838 mem_loc_result = op0;
13839 add_loc_descr (&mem_loc_result, op1);
13840 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13841 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13842 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13843 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13844 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13845 break;
13847 case UDIV:
13848 if (!dwarf_strict && SCALAR_INT_MODE_P (mode))
13850 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13852 op = DW_OP_div;
13853 goto do_binop;
13855 mem_loc_result = typed_binop (DW_OP_div, rtl,
13856 base_type_for_mode (mode, 1),
13857 mode, mem_mode);
13859 break;
13861 case NOT:
13862 op = DW_OP_not;
13863 goto do_unop;
13865 case ABS:
13866 op = DW_OP_abs;
13867 goto do_unop;
13869 case NEG:
13870 op = DW_OP_neg;
13871 goto do_unop;
13873 do_unop:
13874 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13875 VAR_INIT_STATUS_INITIALIZED);
13877 if (op0 == 0)
13878 break;
13880 mem_loc_result = op0;
13881 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13882 break;
13884 case CONST_INT:
13885 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13886 #ifdef POINTERS_EXTEND_UNSIGNED
13887 || (mode == Pmode
13888 && mem_mode != VOIDmode
13889 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13890 #endif
13893 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13894 break;
13896 if (!dwarf_strict
13897 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13898 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13900 dw_die_ref type_die = base_type_for_mode (mode, 1);
13901 machine_mode amode;
13902 if (type_die == NULL)
13903 return NULL;
13904 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13905 MODE_INT, 0);
13906 if (INTVAL (rtl) >= 0
13907 && amode != BLKmode
13908 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13909 /* const DW_OP_GNU_convert <XXX> vs.
13910 DW_OP_GNU_const_type <XXX, 1, const>. */
13911 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13912 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13914 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13915 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13916 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13917 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13918 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13919 add_loc_descr (&mem_loc_result, op0);
13920 return mem_loc_result;
13922 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13923 INTVAL (rtl));
13924 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13925 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13926 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13927 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13928 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13929 else
13931 mem_loc_result->dw_loc_oprnd2.val_class
13932 = dw_val_class_const_double;
13933 mem_loc_result->dw_loc_oprnd2.v.val_double
13934 = double_int::from_shwi (INTVAL (rtl));
13937 break;
13939 case CONST_DOUBLE:
13940 if (!dwarf_strict)
13942 dw_die_ref type_die;
13944 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13945 CONST_DOUBLE rtx could represent either a large integer
13946 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13947 the value is always a floating point constant.
13949 When it is an integer, a CONST_DOUBLE is used whenever
13950 the constant requires 2 HWIs to be adequately represented.
13951 We output CONST_DOUBLEs as blocks. */
13952 if (mode == VOIDmode
13953 || (GET_MODE (rtl) == VOIDmode
13954 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13955 break;
13956 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13957 if (type_die == NULL)
13958 return NULL;
13959 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13960 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13961 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13962 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13963 #if TARGET_SUPPORTS_WIDE_INT == 0
13964 if (!SCALAR_FLOAT_MODE_P (mode))
13966 mem_loc_result->dw_loc_oprnd2.val_class
13967 = dw_val_class_const_double;
13968 mem_loc_result->dw_loc_oprnd2.v.val_double
13969 = rtx_to_double_int (rtl);
13971 else
13972 #endif
13974 unsigned int length = GET_MODE_SIZE (mode);
13975 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13977 insert_float (rtl, array);
13978 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13979 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13980 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13981 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13984 break;
13986 case CONST_WIDE_INT:
13987 if (!dwarf_strict)
13989 dw_die_ref type_die;
13991 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13992 if (type_die == NULL)
13993 return NULL;
13994 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13995 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13996 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13997 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13998 mem_loc_result->dw_loc_oprnd2.val_class
13999 = dw_val_class_wide_int;
14000 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14001 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14003 break;
14005 case EQ:
14006 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
14007 break;
14009 case GE:
14010 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14011 break;
14013 case GT:
14014 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14015 break;
14017 case LE:
14018 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14019 break;
14021 case LT:
14022 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14023 break;
14025 case NE:
14026 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14027 break;
14029 case GEU:
14030 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14031 break;
14033 case GTU:
14034 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14035 break;
14037 case LEU:
14038 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14039 break;
14041 case LTU:
14042 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14043 break;
14045 case UMIN:
14046 case UMAX:
14047 if (!SCALAR_INT_MODE_P (mode))
14048 break;
14049 /* FALLTHRU */
14050 case SMIN:
14051 case SMAX:
14052 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14053 break;
14055 case ZERO_EXTRACT:
14056 case SIGN_EXTRACT:
14057 if (CONST_INT_P (XEXP (rtl, 1))
14058 && CONST_INT_P (XEXP (rtl, 2))
14059 && ((unsigned) INTVAL (XEXP (rtl, 1))
14060 + (unsigned) INTVAL (XEXP (rtl, 2))
14061 <= GET_MODE_BITSIZE (mode))
14062 && SCALAR_INT_MODE_P (mode)
14063 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14064 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14066 int shift, size;
14067 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14068 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14069 if (op0 == 0)
14070 break;
14071 if (GET_CODE (rtl) == SIGN_EXTRACT)
14072 op = DW_OP_shra;
14073 else
14074 op = DW_OP_shr;
14075 mem_loc_result = op0;
14076 size = INTVAL (XEXP (rtl, 1));
14077 shift = INTVAL (XEXP (rtl, 2));
14078 if (BITS_BIG_ENDIAN)
14079 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14080 - shift - size;
14081 if (shift + size != (int) DWARF2_ADDR_SIZE)
14083 add_loc_descr (&mem_loc_result,
14084 int_loc_descriptor (DWARF2_ADDR_SIZE
14085 - shift - size));
14086 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14088 if (size != (int) DWARF2_ADDR_SIZE)
14090 add_loc_descr (&mem_loc_result,
14091 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14092 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14095 break;
14097 case IF_THEN_ELSE:
14099 dw_loc_descr_ref op2, bra_node, drop_node;
14100 op0 = mem_loc_descriptor (XEXP (rtl, 0),
14101 GET_MODE (XEXP (rtl, 0)) == VOIDmode
14102 ? word_mode : GET_MODE (XEXP (rtl, 0)),
14103 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14104 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14105 VAR_INIT_STATUS_INITIALIZED);
14106 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14107 VAR_INIT_STATUS_INITIALIZED);
14108 if (op0 == NULL || op1 == NULL || op2 == NULL)
14109 break;
14111 mem_loc_result = op1;
14112 add_loc_descr (&mem_loc_result, op2);
14113 add_loc_descr (&mem_loc_result, op0);
14114 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14115 add_loc_descr (&mem_loc_result, bra_node);
14116 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14117 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14118 add_loc_descr (&mem_loc_result, drop_node);
14119 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14120 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14122 break;
14124 case FLOAT_EXTEND:
14125 case FLOAT_TRUNCATE:
14126 case FLOAT:
14127 case UNSIGNED_FLOAT:
14128 case FIX:
14129 case UNSIGNED_FIX:
14130 if (!dwarf_strict)
14132 dw_die_ref type_die;
14133 dw_loc_descr_ref cvt;
14135 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14136 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14137 if (op0 == NULL)
14138 break;
14139 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
14140 && (GET_CODE (rtl) == FLOAT
14141 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14142 <= DWARF2_ADDR_SIZE))
14144 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14145 GET_CODE (rtl) == UNSIGNED_FLOAT);
14146 if (type_die == NULL)
14147 break;
14148 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14149 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14150 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14151 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14152 add_loc_descr (&op0, cvt);
14154 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14155 if (type_die == NULL)
14156 break;
14157 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14158 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14159 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14160 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14161 add_loc_descr (&op0, cvt);
14162 if (SCALAR_INT_MODE_P (mode)
14163 && (GET_CODE (rtl) == FIX
14164 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14166 op0 = convert_descriptor_to_mode (mode, op0);
14167 if (op0 == NULL)
14168 break;
14170 mem_loc_result = op0;
14172 break;
14174 case CLZ:
14175 case CTZ:
14176 case FFS:
14177 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14178 break;
14180 case POPCOUNT:
14181 case PARITY:
14182 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14183 break;
14185 case BSWAP:
14186 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14187 break;
14189 case ROTATE:
14190 case ROTATERT:
14191 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14192 break;
14194 case COMPARE:
14195 /* In theory, we could implement the above. */
14196 /* DWARF cannot represent the unsigned compare operations
14197 natively. */
14198 case SS_MULT:
14199 case US_MULT:
14200 case SS_DIV:
14201 case US_DIV:
14202 case SS_PLUS:
14203 case US_PLUS:
14204 case SS_MINUS:
14205 case US_MINUS:
14206 case SS_NEG:
14207 case US_NEG:
14208 case SS_ABS:
14209 case SS_ASHIFT:
14210 case US_ASHIFT:
14211 case SS_TRUNCATE:
14212 case US_TRUNCATE:
14213 case UNORDERED:
14214 case ORDERED:
14215 case UNEQ:
14216 case UNGE:
14217 case UNGT:
14218 case UNLE:
14219 case UNLT:
14220 case LTGT:
14221 case FRACT_CONVERT:
14222 case UNSIGNED_FRACT_CONVERT:
14223 case SAT_FRACT:
14224 case UNSIGNED_SAT_FRACT:
14225 case SQRT:
14226 case ASM_OPERANDS:
14227 case VEC_MERGE:
14228 case VEC_SELECT:
14229 case VEC_CONCAT:
14230 case VEC_DUPLICATE:
14231 case UNSPEC:
14232 case HIGH:
14233 case FMA:
14234 case STRICT_LOW_PART:
14235 case CONST_VECTOR:
14236 case CONST_FIXED:
14237 case CLRSB:
14238 case CLOBBER:
14239 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14240 can't express it in the debug info. This can happen e.g. with some
14241 TLS UNSPECs. */
14242 break;
14244 case CONST_STRING:
14245 resolve_one_addr (&rtl);
14246 goto symref;
14248 default:
14249 if (flag_checking)
14251 print_rtl (stderr, rtl);
14252 gcc_unreachable ();
14254 break;
14257 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14258 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14260 return mem_loc_result;
14263 /* Return a descriptor that describes the concatenation of two locations.
14264 This is typically a complex variable. */
14266 static dw_loc_descr_ref
14267 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14269 dw_loc_descr_ref cc_loc_result = NULL;
14270 dw_loc_descr_ref x0_ref
14271 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14272 dw_loc_descr_ref x1_ref
14273 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14275 if (x0_ref == 0 || x1_ref == 0)
14276 return 0;
14278 cc_loc_result = x0_ref;
14279 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14281 add_loc_descr (&cc_loc_result, x1_ref);
14282 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14284 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14285 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14287 return cc_loc_result;
14290 /* Return a descriptor that describes the concatenation of N
14291 locations. */
14293 static dw_loc_descr_ref
14294 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14296 unsigned int i;
14297 dw_loc_descr_ref cc_loc_result = NULL;
14298 unsigned int n = XVECLEN (concatn, 0);
14300 for (i = 0; i < n; ++i)
14302 dw_loc_descr_ref ref;
14303 rtx x = XVECEXP (concatn, 0, i);
14305 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14306 if (ref == NULL)
14307 return NULL;
14309 add_loc_descr (&cc_loc_result, ref);
14310 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14313 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14314 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14316 return cc_loc_result;
14319 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14320 for DEBUG_IMPLICIT_PTR RTL. */
14322 static dw_loc_descr_ref
14323 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14325 dw_loc_descr_ref ret;
14326 dw_die_ref ref;
14328 if (dwarf_strict)
14329 return NULL;
14330 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14331 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14332 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14333 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14334 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14335 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14336 if (ref)
14338 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14339 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14340 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14342 else
14344 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14345 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14347 return ret;
14350 /* Output a proper Dwarf location descriptor for a variable or parameter
14351 which is either allocated in a register or in a memory location. For a
14352 register, we just generate an OP_REG and the register number. For a
14353 memory location we provide a Dwarf postfix expression describing how to
14354 generate the (dynamic) address of the object onto the address stack.
14356 MODE is mode of the decl if this loc_descriptor is going to be used in
14357 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14358 allowed, VOIDmode otherwise.
14360 If we don't know how to describe it, return 0. */
14362 static dw_loc_descr_ref
14363 loc_descriptor (rtx rtl, machine_mode mode,
14364 enum var_init_status initialized)
14366 dw_loc_descr_ref loc_result = NULL;
14368 switch (GET_CODE (rtl))
14370 case SUBREG:
14371 /* The case of a subreg may arise when we have a local (register)
14372 variable or a formal (register) parameter which doesn't quite fill
14373 up an entire register. For now, just assume that it is
14374 legitimate to make the Dwarf info refer to the whole register which
14375 contains the given subreg. */
14376 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14377 loc_result = loc_descriptor (SUBREG_REG (rtl),
14378 GET_MODE (SUBREG_REG (rtl)), initialized);
14379 else
14380 goto do_default;
14381 break;
14383 case REG:
14384 loc_result = reg_loc_descriptor (rtl, initialized);
14385 break;
14387 case MEM:
14388 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14389 GET_MODE (rtl), initialized);
14390 if (loc_result == NULL)
14391 loc_result = tls_mem_loc_descriptor (rtl);
14392 if (loc_result == NULL)
14394 rtx new_rtl = avoid_constant_pool_reference (rtl);
14395 if (new_rtl != rtl)
14396 loc_result = loc_descriptor (new_rtl, mode, initialized);
14398 break;
14400 case CONCAT:
14401 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14402 initialized);
14403 break;
14405 case CONCATN:
14406 loc_result = concatn_loc_descriptor (rtl, initialized);
14407 break;
14409 case VAR_LOCATION:
14410 /* Single part. */
14411 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14413 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14414 if (GET_CODE (loc) == EXPR_LIST)
14415 loc = XEXP (loc, 0);
14416 loc_result = loc_descriptor (loc, mode, initialized);
14417 break;
14420 rtl = XEXP (rtl, 1);
14421 /* FALLTHRU */
14423 case PARALLEL:
14425 rtvec par_elems = XVEC (rtl, 0);
14426 int num_elem = GET_NUM_ELEM (par_elems);
14427 machine_mode mode;
14428 int i;
14430 /* Create the first one, so we have something to add to. */
14431 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14432 VOIDmode, initialized);
14433 if (loc_result == NULL)
14434 return NULL;
14435 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14436 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14437 for (i = 1; i < num_elem; i++)
14439 dw_loc_descr_ref temp;
14441 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14442 VOIDmode, initialized);
14443 if (temp == NULL)
14444 return NULL;
14445 add_loc_descr (&loc_result, temp);
14446 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14447 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14450 break;
14452 case CONST_INT:
14453 if (mode != VOIDmode && mode != BLKmode)
14454 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14455 INTVAL (rtl));
14456 break;
14458 case CONST_DOUBLE:
14459 if (mode == VOIDmode)
14460 mode = GET_MODE (rtl);
14462 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14464 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14466 /* Note that a CONST_DOUBLE rtx could represent either an integer
14467 or a floating-point constant. A CONST_DOUBLE is used whenever
14468 the constant requires more than one word in order to be
14469 adequately represented. We output CONST_DOUBLEs as blocks. */
14470 loc_result = new_loc_descr (DW_OP_implicit_value,
14471 GET_MODE_SIZE (mode), 0);
14472 #if TARGET_SUPPORTS_WIDE_INT == 0
14473 if (!SCALAR_FLOAT_MODE_P (mode))
14475 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14476 loc_result->dw_loc_oprnd2.v.val_double
14477 = rtx_to_double_int (rtl);
14479 else
14480 #endif
14482 unsigned int length = GET_MODE_SIZE (mode);
14483 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14485 insert_float (rtl, array);
14486 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14487 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14488 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14489 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14492 break;
14494 case CONST_WIDE_INT:
14495 if (mode == VOIDmode)
14496 mode = GET_MODE (rtl);
14498 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14500 loc_result = new_loc_descr (DW_OP_implicit_value,
14501 GET_MODE_SIZE (mode), 0);
14502 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14503 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14504 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14506 break;
14508 case CONST_VECTOR:
14509 if (mode == VOIDmode)
14510 mode = GET_MODE (rtl);
14512 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14514 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14515 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14516 unsigned char *array
14517 = ggc_vec_alloc<unsigned char> (length * elt_size);
14518 unsigned int i;
14519 unsigned char *p;
14520 machine_mode imode = GET_MODE_INNER (mode);
14522 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14523 switch (GET_MODE_CLASS (mode))
14525 case MODE_VECTOR_INT:
14526 for (i = 0, p = array; i < length; i++, p += elt_size)
14528 rtx elt = CONST_VECTOR_ELT (rtl, i);
14529 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14531 break;
14533 case MODE_VECTOR_FLOAT:
14534 for (i = 0, p = array; i < length; i++, p += elt_size)
14536 rtx elt = CONST_VECTOR_ELT (rtl, i);
14537 insert_float (elt, p);
14539 break;
14541 default:
14542 gcc_unreachable ();
14545 loc_result = new_loc_descr (DW_OP_implicit_value,
14546 length * elt_size, 0);
14547 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14548 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14549 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14550 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14552 break;
14554 case CONST:
14555 if (mode == VOIDmode
14556 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14557 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14558 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14560 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14561 break;
14563 /* FALLTHROUGH */
14564 case SYMBOL_REF:
14565 if (!const_ok_for_output (rtl))
14566 break;
14567 case LABEL_REF:
14568 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14569 && (dwarf_version >= 4 || !dwarf_strict))
14571 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14572 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14573 vec_safe_push (used_rtx_array, rtl);
14575 break;
14577 case DEBUG_IMPLICIT_PTR:
14578 loc_result = implicit_ptr_descriptor (rtl, 0);
14579 break;
14581 case PLUS:
14582 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14583 && CONST_INT_P (XEXP (rtl, 1)))
14585 loc_result
14586 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14587 break;
14589 /* FALLTHRU */
14590 do_default:
14591 default:
14592 if ((SCALAR_INT_MODE_P (mode)
14593 && GET_MODE (rtl) == mode
14594 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14595 && dwarf_version >= 4)
14596 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14598 /* Value expression. */
14599 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14600 if (loc_result)
14601 add_loc_descr (&loc_result,
14602 new_loc_descr (DW_OP_stack_value, 0, 0));
14604 break;
14607 return loc_result;
14610 /* We need to figure out what section we should use as the base for the
14611 address ranges where a given location is valid.
14612 1. If this particular DECL has a section associated with it, use that.
14613 2. If this function has a section associated with it, use that.
14614 3. Otherwise, use the text section.
14615 XXX: If you split a variable across multiple sections, we won't notice. */
14617 static const char *
14618 secname_for_decl (const_tree decl)
14620 const char *secname;
14622 if (VAR_OR_FUNCTION_DECL_P (decl)
14623 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14624 && DECL_SECTION_NAME (decl))
14625 secname = DECL_SECTION_NAME (decl);
14626 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14627 secname = DECL_SECTION_NAME (current_function_decl);
14628 else if (cfun && in_cold_section_p)
14629 secname = crtl->subsections.cold_section_label;
14630 else
14631 secname = text_section_label;
14633 return secname;
14636 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14638 static bool
14639 decl_by_reference_p (tree decl)
14641 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14642 || TREE_CODE (decl) == VAR_DECL)
14643 && DECL_BY_REFERENCE (decl));
14646 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14647 for VARLOC. */
14649 static dw_loc_descr_ref
14650 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14651 enum var_init_status initialized)
14653 int have_address = 0;
14654 dw_loc_descr_ref descr;
14655 machine_mode mode;
14657 if (want_address != 2)
14659 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14660 /* Single part. */
14661 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14663 varloc = PAT_VAR_LOCATION_LOC (varloc);
14664 if (GET_CODE (varloc) == EXPR_LIST)
14665 varloc = XEXP (varloc, 0);
14666 mode = GET_MODE (varloc);
14667 if (MEM_P (varloc))
14669 rtx addr = XEXP (varloc, 0);
14670 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14671 mode, initialized);
14672 if (descr)
14673 have_address = 1;
14674 else
14676 rtx x = avoid_constant_pool_reference (varloc);
14677 if (x != varloc)
14678 descr = mem_loc_descriptor (x, mode, VOIDmode,
14679 initialized);
14682 else
14683 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14685 else
14686 return 0;
14688 else
14690 if (GET_CODE (varloc) == VAR_LOCATION)
14691 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14692 else
14693 mode = DECL_MODE (loc);
14694 descr = loc_descriptor (varloc, mode, initialized);
14695 have_address = 1;
14698 if (!descr)
14699 return 0;
14701 if (want_address == 2 && !have_address
14702 && (dwarf_version >= 4 || !dwarf_strict))
14704 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14706 expansion_failed (loc, NULL_RTX,
14707 "DWARF address size mismatch");
14708 return 0;
14710 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14711 have_address = 1;
14713 /* Show if we can't fill the request for an address. */
14714 if (want_address && !have_address)
14716 expansion_failed (loc, NULL_RTX,
14717 "Want address and only have value");
14718 return 0;
14721 /* If we've got an address and don't want one, dereference. */
14722 if (!want_address && have_address)
14724 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14725 enum dwarf_location_atom op;
14727 if (size > DWARF2_ADDR_SIZE || size == -1)
14729 expansion_failed (loc, NULL_RTX,
14730 "DWARF address size mismatch");
14731 return 0;
14733 else if (size == DWARF2_ADDR_SIZE)
14734 op = DW_OP_deref;
14735 else
14736 op = DW_OP_deref_size;
14738 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14741 return descr;
14744 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14745 if it is not possible. */
14747 static dw_loc_descr_ref
14748 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14750 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14751 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14752 else if (dwarf_version >= 3 || !dwarf_strict)
14753 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14754 else
14755 return NULL;
14758 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14759 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14761 static dw_loc_descr_ref
14762 dw_sra_loc_expr (tree decl, rtx loc)
14764 rtx p;
14765 unsigned HOST_WIDE_INT padsize = 0;
14766 dw_loc_descr_ref descr, *descr_tail;
14767 unsigned HOST_WIDE_INT decl_size;
14768 rtx varloc;
14769 enum var_init_status initialized;
14771 if (DECL_SIZE (decl) == NULL
14772 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14773 return NULL;
14775 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14776 descr = NULL;
14777 descr_tail = &descr;
14779 for (p = loc; p; p = XEXP (p, 1))
14781 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14782 rtx loc_note = *decl_piece_varloc_ptr (p);
14783 dw_loc_descr_ref cur_descr;
14784 dw_loc_descr_ref *tail, last = NULL;
14785 unsigned HOST_WIDE_INT opsize = 0;
14787 if (loc_note == NULL_RTX
14788 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14790 padsize += bitsize;
14791 continue;
14793 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14794 varloc = NOTE_VAR_LOCATION (loc_note);
14795 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14796 if (cur_descr == NULL)
14798 padsize += bitsize;
14799 continue;
14802 /* Check that cur_descr either doesn't use
14803 DW_OP_*piece operations, or their sum is equal
14804 to bitsize. Otherwise we can't embed it. */
14805 for (tail = &cur_descr; *tail != NULL;
14806 tail = &(*tail)->dw_loc_next)
14807 if ((*tail)->dw_loc_opc == DW_OP_piece)
14809 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14810 * BITS_PER_UNIT;
14811 last = *tail;
14813 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14815 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14816 last = *tail;
14819 if (last != NULL && opsize != bitsize)
14821 padsize += bitsize;
14822 /* Discard the current piece of the descriptor and release any
14823 addr_table entries it uses. */
14824 remove_loc_list_addr_table_entries (cur_descr);
14825 continue;
14828 /* If there is a hole, add DW_OP_*piece after empty DWARF
14829 expression, which means that those bits are optimized out. */
14830 if (padsize)
14832 if (padsize > decl_size)
14834 remove_loc_list_addr_table_entries (cur_descr);
14835 goto discard_descr;
14837 decl_size -= padsize;
14838 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14839 if (*descr_tail == NULL)
14841 remove_loc_list_addr_table_entries (cur_descr);
14842 goto discard_descr;
14844 descr_tail = &(*descr_tail)->dw_loc_next;
14845 padsize = 0;
14847 *descr_tail = cur_descr;
14848 descr_tail = tail;
14849 if (bitsize > decl_size)
14850 goto discard_descr;
14851 decl_size -= bitsize;
14852 if (last == NULL)
14854 HOST_WIDE_INT offset = 0;
14855 if (GET_CODE (varloc) == VAR_LOCATION
14856 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14858 varloc = PAT_VAR_LOCATION_LOC (varloc);
14859 if (GET_CODE (varloc) == EXPR_LIST)
14860 varloc = XEXP (varloc, 0);
14864 if (GET_CODE (varloc) == CONST
14865 || GET_CODE (varloc) == SIGN_EXTEND
14866 || GET_CODE (varloc) == ZERO_EXTEND)
14867 varloc = XEXP (varloc, 0);
14868 else if (GET_CODE (varloc) == SUBREG)
14869 varloc = SUBREG_REG (varloc);
14870 else
14871 break;
14873 while (1);
14874 /* DW_OP_bit_size offset should be zero for register
14875 or implicit location descriptions and empty location
14876 descriptions, but for memory addresses needs big endian
14877 adjustment. */
14878 if (MEM_P (varloc))
14880 unsigned HOST_WIDE_INT memsize
14881 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14882 if (memsize != bitsize)
14884 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14885 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14886 goto discard_descr;
14887 if (memsize < bitsize)
14888 goto discard_descr;
14889 if (BITS_BIG_ENDIAN)
14890 offset = memsize - bitsize;
14894 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14895 if (*descr_tail == NULL)
14896 goto discard_descr;
14897 descr_tail = &(*descr_tail)->dw_loc_next;
14901 /* If there were any non-empty expressions, add padding till the end of
14902 the decl. */
14903 if (descr != NULL && decl_size != 0)
14905 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14906 if (*descr_tail == NULL)
14907 goto discard_descr;
14909 return descr;
14911 discard_descr:
14912 /* Discard the descriptor and release any addr_table entries it uses. */
14913 remove_loc_list_addr_table_entries (descr);
14914 return NULL;
14917 /* Return the dwarf representation of the location list LOC_LIST of
14918 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14919 function. */
14921 static dw_loc_list_ref
14922 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14924 const char *endname, *secname;
14925 rtx varloc;
14926 enum var_init_status initialized;
14927 struct var_loc_node *node;
14928 dw_loc_descr_ref descr;
14929 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14930 dw_loc_list_ref list = NULL;
14931 dw_loc_list_ref *listp = &list;
14933 /* Now that we know what section we are using for a base,
14934 actually construct the list of locations.
14935 The first location information is what is passed to the
14936 function that creates the location list, and the remaining
14937 locations just get added on to that list.
14938 Note that we only know the start address for a location
14939 (IE location changes), so to build the range, we use
14940 the range [current location start, next location start].
14941 This means we have to special case the last node, and generate
14942 a range of [last location start, end of function label]. */
14944 secname = secname_for_decl (decl);
14946 for (node = loc_list->first; node; node = node->next)
14947 if (GET_CODE (node->loc) == EXPR_LIST
14948 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14950 if (GET_CODE (node->loc) == EXPR_LIST)
14952 /* This requires DW_OP_{,bit_}piece, which is not usable
14953 inside DWARF expressions. */
14954 if (want_address != 2)
14955 continue;
14956 descr = dw_sra_loc_expr (decl, node->loc);
14957 if (descr == NULL)
14958 continue;
14960 else
14962 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14963 varloc = NOTE_VAR_LOCATION (node->loc);
14964 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14966 if (descr)
14968 bool range_across_switch = false;
14969 /* If section switch happens in between node->label
14970 and node->next->label (or end of function) and
14971 we can't emit it as a single entry list,
14972 emit two ranges, first one ending at the end
14973 of first partition and second one starting at the
14974 beginning of second partition. */
14975 if (node == loc_list->last_before_switch
14976 && (node != loc_list->first || loc_list->first->next)
14977 && current_function_decl)
14979 endname = cfun->fde->dw_fde_end;
14980 range_across_switch = true;
14982 /* The variable has a location between NODE->LABEL and
14983 NODE->NEXT->LABEL. */
14984 else if (node->next)
14985 endname = node->next->label;
14986 /* If the variable has a location at the last label
14987 it keeps its location until the end of function. */
14988 else if (!current_function_decl)
14989 endname = text_end_label;
14990 else
14992 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14993 current_function_funcdef_no);
14994 endname = ggc_strdup (label_id);
14997 *listp = new_loc_list (descr, node->label, endname, secname);
14998 if (TREE_CODE (decl) == PARM_DECL
14999 && node == loc_list->first
15000 && NOTE_P (node->loc)
15001 && strcmp (node->label, endname) == 0)
15002 (*listp)->force = true;
15003 listp = &(*listp)->dw_loc_next;
15005 if (range_across_switch)
15007 if (GET_CODE (node->loc) == EXPR_LIST)
15008 descr = dw_sra_loc_expr (decl, node->loc);
15009 else
15011 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15012 varloc = NOTE_VAR_LOCATION (node->loc);
15013 descr = dw_loc_list_1 (decl, varloc, want_address,
15014 initialized);
15016 gcc_assert (descr);
15017 /* The variable has a location between NODE->LABEL and
15018 NODE->NEXT->LABEL. */
15019 if (node->next)
15020 endname = node->next->label;
15021 else
15022 endname = cfun->fde->dw_fde_second_end;
15023 *listp = new_loc_list (descr,
15024 cfun->fde->dw_fde_second_begin,
15025 endname, secname);
15026 listp = &(*listp)->dw_loc_next;
15031 /* Try to avoid the overhead of a location list emitting a location
15032 expression instead, but only if we didn't have more than one
15033 location entry in the first place. If some entries were not
15034 representable, we don't want to pretend a single entry that was
15035 applies to the entire scope in which the variable is
15036 available. */
15037 if (list && loc_list->first->next)
15038 gen_llsym (list);
15040 return list;
15043 /* Return if the loc_list has only single element and thus can be represented
15044 as location description. */
15046 static bool
15047 single_element_loc_list_p (dw_loc_list_ref list)
15049 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15050 return !list->ll_symbol;
15053 /* To each location in list LIST add loc descr REF. */
15055 static void
15056 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15058 dw_loc_descr_ref copy;
15059 add_loc_descr (&list->expr, ref);
15060 list = list->dw_loc_next;
15061 while (list)
15063 copy = ggc_alloc<dw_loc_descr_node> ();
15064 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15065 add_loc_descr (&list->expr, copy);
15066 while (copy->dw_loc_next)
15068 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15069 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15070 copy->dw_loc_next = new_copy;
15071 copy = new_copy;
15073 list = list->dw_loc_next;
15077 /* Given two lists RET and LIST
15078 produce location list that is result of adding expression in LIST
15079 to expression in RET on each position in program.
15080 Might be destructive on both RET and LIST.
15082 TODO: We handle only simple cases of RET or LIST having at most one
15083 element. General case would inolve sorting the lists in program order
15084 and merging them that will need some additional work.
15085 Adding that will improve quality of debug info especially for SRA-ed
15086 structures. */
15088 static void
15089 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15091 if (!list)
15092 return;
15093 if (!*ret)
15095 *ret = list;
15096 return;
15098 if (!list->dw_loc_next)
15100 add_loc_descr_to_each (*ret, list->expr);
15101 return;
15103 if (!(*ret)->dw_loc_next)
15105 add_loc_descr_to_each (list, (*ret)->expr);
15106 *ret = list;
15107 return;
15109 expansion_failed (NULL_TREE, NULL_RTX,
15110 "Don't know how to merge two non-trivial"
15111 " location lists.\n");
15112 *ret = NULL;
15113 return;
15116 /* LOC is constant expression. Try a luck, look it up in constant
15117 pool and return its loc_descr of its address. */
15119 static dw_loc_descr_ref
15120 cst_pool_loc_descr (tree loc)
15122 /* Get an RTL for this, if something has been emitted. */
15123 rtx rtl = lookup_constant_def (loc);
15125 if (!rtl || !MEM_P (rtl))
15127 gcc_assert (!rtl);
15128 return 0;
15130 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15132 /* TODO: We might get more coverage if we was actually delaying expansion
15133 of all expressions till end of compilation when constant pools are fully
15134 populated. */
15135 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15137 expansion_failed (loc, NULL_RTX,
15138 "CST value in contant pool but not marked.");
15139 return 0;
15141 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15142 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15145 /* Return dw_loc_list representing address of addr_expr LOC
15146 by looking for inner INDIRECT_REF expression and turning
15147 it into simple arithmetics.
15149 See loc_list_from_tree for the meaning of CONTEXT. */
15151 static dw_loc_list_ref
15152 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15153 const loc_descr_context *context)
15155 tree obj, offset;
15156 HOST_WIDE_INT bitsize, bitpos, bytepos;
15157 machine_mode mode;
15158 int unsignedp, reversep, volatilep = 0;
15159 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15161 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15162 &bitsize, &bitpos, &offset, &mode,
15163 &unsignedp, &reversep, &volatilep);
15164 STRIP_NOPS (obj);
15165 if (bitpos % BITS_PER_UNIT)
15167 expansion_failed (loc, NULL_RTX, "bitfield access");
15168 return 0;
15170 if (!INDIRECT_REF_P (obj))
15172 expansion_failed (obj,
15173 NULL_RTX, "no indirect ref in inner refrence");
15174 return 0;
15176 if (!offset && !bitpos)
15177 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15178 context);
15179 else if (toplev
15180 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15181 && (dwarf_version >= 4 || !dwarf_strict))
15183 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15184 if (!list_ret)
15185 return 0;
15186 if (offset)
15188 /* Variable offset. */
15189 list_ret1 = loc_list_from_tree (offset, 0, context);
15190 if (list_ret1 == 0)
15191 return 0;
15192 add_loc_list (&list_ret, list_ret1);
15193 if (!list_ret)
15194 return 0;
15195 add_loc_descr_to_each (list_ret,
15196 new_loc_descr (DW_OP_plus, 0, 0));
15198 bytepos = bitpos / BITS_PER_UNIT;
15199 if (bytepos > 0)
15200 add_loc_descr_to_each (list_ret,
15201 new_loc_descr (DW_OP_plus_uconst,
15202 bytepos, 0));
15203 else if (bytepos < 0)
15204 loc_list_plus_const (list_ret, bytepos);
15205 add_loc_descr_to_each (list_ret,
15206 new_loc_descr (DW_OP_stack_value, 0, 0));
15208 return list_ret;
15211 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15212 all operations from LOC are nops, move to the last one. Insert in NOPS all
15213 operations that are skipped. */
15215 static void
15216 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15217 hash_set<dw_loc_descr_ref> &nops)
15219 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15221 nops.add (loc);
15222 loc = loc->dw_loc_next;
15226 /* Helper for loc_descr_without_nops: free the location description operation
15227 P. */
15229 bool
15230 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15232 ggc_free (loc);
15233 return true;
15236 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15237 finishes LOC. */
15239 static void
15240 loc_descr_without_nops (dw_loc_descr_ref &loc)
15242 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15243 return;
15245 /* Set of all DW_OP_nop operations we remove. */
15246 hash_set<dw_loc_descr_ref> nops;
15248 /* First, strip all prefix NOP operations in order to keep the head of the
15249 operations list. */
15250 loc_descr_to_next_no_nop (loc, nops);
15252 for (dw_loc_descr_ref cur = loc; cur != NULL;)
15254 /* For control flow operations: strip "prefix" nops in destination
15255 labels. */
15256 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15257 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15258 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15259 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15261 /* Do the same for the operations that follow, then move to the next
15262 iteration. */
15263 if (cur->dw_loc_next != NULL)
15264 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15265 cur = cur->dw_loc_next;
15268 nops.traverse<void *, free_loc_descr> (NULL);
15272 struct dwarf_procedure_info;
15274 /* Helper structure for location descriptions generation. */
15275 struct loc_descr_context
15277 /* The type that is implicitly referenced by DW_OP_push_object_address, or
15278 NULL_TREE if DW_OP_push_object_address in invalid for this location
15279 description. This is used when processing PLACEHOLDER_EXPR nodes. */
15280 tree context_type;
15281 /* The ..._DECL node that should be translated as a
15282 DW_OP_push_object_address operation. */
15283 tree base_decl;
15284 /* Information about the DWARF procedure we are currently generating. NULL if
15285 we are not generating a DWARF procedure. */
15286 struct dwarf_procedure_info *dpi;
15289 /* DWARF procedures generation
15291 DWARF expressions (aka. location descriptions) are used to encode variable
15292 things such as sizes or offsets. Such computations can have redundant parts
15293 that can be factorized in order to reduce the size of the output debug
15294 information. This is the whole point of DWARF procedures.
15296 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15297 already factorized into functions ("size functions") in order to handle very
15298 big and complex types. Such functions are quite simple: they have integral
15299 arguments, they return an integral result and their body contains only a
15300 return statement with arithmetic expressions. This is the only kind of
15301 function we are interested in translating into DWARF procedures, here.
15303 DWARF expressions and DWARF procedure are executed using a stack, so we have
15304 to define some calling convention for them to interact. Let's say that:
15306 - Before calling a DWARF procedure, DWARF expressions must push on the stack
15307 all arguments in reverse order (right-to-left) so that when the DWARF
15308 procedure execution starts, the first argument is the top of the stack.
15310 - Then, when returning, the DWARF procedure must have consumed all arguments
15311 on the stack, must have pushed the result and touched nothing else.
15313 - Each integral argument and the result are integral types can be hold in a
15314 single stack slot.
15316 - We call "frame offset" the number of stack slots that are "under DWARF
15317 procedure control": it includes the arguments slots, the temporaries and
15318 the result slot. Thus, it is equal to the number of arguments when the
15319 procedure execution starts and must be equal to one (the result) when it
15320 returns. */
15322 /* Helper structure used when generating operations for a DWARF procedure. */
15323 struct dwarf_procedure_info
15325 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15326 currently translated. */
15327 tree fndecl;
15328 /* The number of arguments FNDECL takes. */
15329 unsigned args_count;
15332 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
15333 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
15334 equate it to this DIE. */
15336 static dw_die_ref
15337 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15338 dw_die_ref parent_die)
15340 dw_die_ref dwarf_proc_die;
15342 if ((dwarf_version < 3 && dwarf_strict)
15343 || location == NULL)
15344 return NULL;
15346 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
15347 if (fndecl)
15348 equate_decl_number_to_die (fndecl, dwarf_proc_die);
15349 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15350 return dwarf_proc_die;
15353 /* Return whether TYPE is a supported type as a DWARF procedure argument
15354 type or return type (we handle only scalar types and pointer types that
15355 aren't wider than the DWARF expression evaluation stack. */
15357 static bool
15358 is_handled_procedure_type (tree type)
15360 return ((INTEGRAL_TYPE_P (type)
15361 || TREE_CODE (type) == OFFSET_TYPE
15362 || TREE_CODE (type) == POINTER_TYPE)
15363 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15366 /* Helper for resolve_args_picking: do the same but stop when coming across
15367 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
15368 offset *before* evaluating the corresponding operation. */
15370 static bool
15371 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15372 struct dwarf_procedure_info *dpi,
15373 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
15375 /* The "frame_offset" identifier is already used to name a macro... */
15376 unsigned frame_offset_ = initial_frame_offset;
15377 dw_loc_descr_ref l;
15379 for (l = loc; l != NULL;)
15381 bool existed;
15382 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
15384 /* If we already met this node, there is nothing to compute anymore. */
15385 if (existed)
15387 /* Make sure that the stack size is consistent wherever the execution
15388 flow comes from. */
15389 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
15390 break;
15392 l_frame_offset = frame_offset_;
15394 /* If needed, relocate the picking offset with respect to the frame
15395 offset. */
15396 if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15398 /* frame_offset_ is the size of the current stack frame, including
15399 incoming arguments. Besides, the arguments are pushed
15400 right-to-left. Thus, in order to access the Nth argument from
15401 this operation node, the picking has to skip temporaries *plus*
15402 one stack slot per argument (0 for the first one, 1 for the second
15403 one, etc.).
15405 The targetted argument number (N) is already set as the operand,
15406 and the number of temporaries can be computed with:
15407 frame_offsets_ - dpi->args_count */
15408 l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15410 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
15411 if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15412 return false;
15415 /* Update frame_offset according to the effect the current operation has
15416 on the stack. */
15417 switch (l->dw_loc_opc)
15419 case DW_OP_deref:
15420 case DW_OP_swap:
15421 case DW_OP_rot:
15422 case DW_OP_abs:
15423 case DW_OP_neg:
15424 case DW_OP_not:
15425 case DW_OP_plus_uconst:
15426 case DW_OP_skip:
15427 case DW_OP_reg0:
15428 case DW_OP_reg1:
15429 case DW_OP_reg2:
15430 case DW_OP_reg3:
15431 case DW_OP_reg4:
15432 case DW_OP_reg5:
15433 case DW_OP_reg6:
15434 case DW_OP_reg7:
15435 case DW_OP_reg8:
15436 case DW_OP_reg9:
15437 case DW_OP_reg10:
15438 case DW_OP_reg11:
15439 case DW_OP_reg12:
15440 case DW_OP_reg13:
15441 case DW_OP_reg14:
15442 case DW_OP_reg15:
15443 case DW_OP_reg16:
15444 case DW_OP_reg17:
15445 case DW_OP_reg18:
15446 case DW_OP_reg19:
15447 case DW_OP_reg20:
15448 case DW_OP_reg21:
15449 case DW_OP_reg22:
15450 case DW_OP_reg23:
15451 case DW_OP_reg24:
15452 case DW_OP_reg25:
15453 case DW_OP_reg26:
15454 case DW_OP_reg27:
15455 case DW_OP_reg28:
15456 case DW_OP_reg29:
15457 case DW_OP_reg30:
15458 case DW_OP_reg31:
15459 case DW_OP_bregx:
15460 case DW_OP_piece:
15461 case DW_OP_deref_size:
15462 case DW_OP_nop:
15463 case DW_OP_form_tls_address:
15464 case DW_OP_bit_piece:
15465 case DW_OP_implicit_value:
15466 case DW_OP_stack_value:
15467 break;
15469 case DW_OP_addr:
15470 case DW_OP_const1u:
15471 case DW_OP_const1s:
15472 case DW_OP_const2u:
15473 case DW_OP_const2s:
15474 case DW_OP_const4u:
15475 case DW_OP_const4s:
15476 case DW_OP_const8u:
15477 case DW_OP_const8s:
15478 case DW_OP_constu:
15479 case DW_OP_consts:
15480 case DW_OP_dup:
15481 case DW_OP_over:
15482 case DW_OP_pick:
15483 case DW_OP_lit0:
15484 case DW_OP_lit1:
15485 case DW_OP_lit2:
15486 case DW_OP_lit3:
15487 case DW_OP_lit4:
15488 case DW_OP_lit5:
15489 case DW_OP_lit6:
15490 case DW_OP_lit7:
15491 case DW_OP_lit8:
15492 case DW_OP_lit9:
15493 case DW_OP_lit10:
15494 case DW_OP_lit11:
15495 case DW_OP_lit12:
15496 case DW_OP_lit13:
15497 case DW_OP_lit14:
15498 case DW_OP_lit15:
15499 case DW_OP_lit16:
15500 case DW_OP_lit17:
15501 case DW_OP_lit18:
15502 case DW_OP_lit19:
15503 case DW_OP_lit20:
15504 case DW_OP_lit21:
15505 case DW_OP_lit22:
15506 case DW_OP_lit23:
15507 case DW_OP_lit24:
15508 case DW_OP_lit25:
15509 case DW_OP_lit26:
15510 case DW_OP_lit27:
15511 case DW_OP_lit28:
15512 case DW_OP_lit29:
15513 case DW_OP_lit30:
15514 case DW_OP_lit31:
15515 case DW_OP_breg0:
15516 case DW_OP_breg1:
15517 case DW_OP_breg2:
15518 case DW_OP_breg3:
15519 case DW_OP_breg4:
15520 case DW_OP_breg5:
15521 case DW_OP_breg6:
15522 case DW_OP_breg7:
15523 case DW_OP_breg8:
15524 case DW_OP_breg9:
15525 case DW_OP_breg10:
15526 case DW_OP_breg11:
15527 case DW_OP_breg12:
15528 case DW_OP_breg13:
15529 case DW_OP_breg14:
15530 case DW_OP_breg15:
15531 case DW_OP_breg16:
15532 case DW_OP_breg17:
15533 case DW_OP_breg18:
15534 case DW_OP_breg19:
15535 case DW_OP_breg20:
15536 case DW_OP_breg21:
15537 case DW_OP_breg22:
15538 case DW_OP_breg23:
15539 case DW_OP_breg24:
15540 case DW_OP_breg25:
15541 case DW_OP_breg26:
15542 case DW_OP_breg27:
15543 case DW_OP_breg28:
15544 case DW_OP_breg29:
15545 case DW_OP_breg30:
15546 case DW_OP_breg31:
15547 case DW_OP_fbreg:
15548 case DW_OP_push_object_address:
15549 case DW_OP_call_frame_cfa:
15550 ++frame_offset_;
15551 break;
15553 case DW_OP_drop:
15554 case DW_OP_xderef:
15555 case DW_OP_and:
15556 case DW_OP_div:
15557 case DW_OP_minus:
15558 case DW_OP_mod:
15559 case DW_OP_mul:
15560 case DW_OP_or:
15561 case DW_OP_plus:
15562 case DW_OP_shl:
15563 case DW_OP_shr:
15564 case DW_OP_shra:
15565 case DW_OP_xor:
15566 case DW_OP_bra:
15567 case DW_OP_eq:
15568 case DW_OP_ge:
15569 case DW_OP_gt:
15570 case DW_OP_le:
15571 case DW_OP_lt:
15572 case DW_OP_ne:
15573 case DW_OP_regx:
15574 case DW_OP_xderef_size:
15575 --frame_offset_;
15576 break;
15578 case DW_OP_call2:
15579 case DW_OP_call4:
15580 case DW_OP_call_ref:
15582 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15583 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15585 if (stack_usage == NULL)
15586 return false;
15587 frame_offset_ += *stack_usage;
15588 break;
15591 case DW_OP_GNU_push_tls_address:
15592 case DW_OP_GNU_uninit:
15593 case DW_OP_GNU_encoded_addr:
15594 case DW_OP_GNU_implicit_pointer:
15595 case DW_OP_GNU_entry_value:
15596 case DW_OP_GNU_const_type:
15597 case DW_OP_GNU_regval_type:
15598 case DW_OP_GNU_deref_type:
15599 case DW_OP_GNU_convert:
15600 case DW_OP_GNU_reinterpret:
15601 case DW_OP_GNU_parameter_ref:
15602 /* loc_list_from_tree will probably not output these operations for
15603 size functions, so assume they will not appear here. */
15604 /* Fall through... */
15606 default:
15607 gcc_unreachable ();
15610 /* Now, follow the control flow (except subroutine calls). */
15611 switch (l->dw_loc_opc)
15613 case DW_OP_bra:
15614 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15615 frame_offsets))
15616 return false;
15617 /* Fall through. */
15619 case DW_OP_skip:
15620 l = l->dw_loc_oprnd1.v.val_loc;
15621 break;
15623 case DW_OP_stack_value:
15624 return true;
15626 default:
15627 l = l->dw_loc_next;
15628 break;
15632 return true;
15635 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15636 operations) in order to resolve the operand of DW_OP_pick operations that
15637 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
15638 offset *before* LOC is executed. Return if all relocations were
15639 successful. */
15641 static bool
15642 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15643 struct dwarf_procedure_info *dpi)
15645 /* Associate to all visited operations the frame offset *before* evaluating
15646 this operation. */
15647 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
15649 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
15650 frame_offsets);
15653 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15654 Return NULL if it is not possible. */
15656 static dw_die_ref
15657 function_to_dwarf_procedure (tree fndecl)
15659 struct loc_descr_context ctx;
15660 struct dwarf_procedure_info dpi;
15661 dw_die_ref dwarf_proc_die;
15662 tree tree_body = DECL_SAVED_TREE (fndecl);
15663 dw_loc_descr_ref loc_body, epilogue;
15665 tree cursor;
15666 unsigned i;
15668 /* Do not generate multiple DWARF procedures for the same function
15669 declaration. */
15670 dwarf_proc_die = lookup_decl_die (fndecl);
15671 if (dwarf_proc_die != NULL)
15672 return dwarf_proc_die;
15674 /* DWARF procedures are available starting with the DWARFv3 standard. */
15675 if (dwarf_version < 3 && dwarf_strict)
15676 return NULL;
15678 /* We handle only functions for which we still have a body, that return a
15679 supported type and that takes arguments with supported types. Note that
15680 there is no point translating functions that return nothing. */
15681 if (tree_body == NULL_TREE
15682 || DECL_RESULT (fndecl) == NULL_TREE
15683 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15684 return NULL;
15686 for (cursor = DECL_ARGUMENTS (fndecl);
15687 cursor != NULL_TREE;
15688 cursor = TREE_CHAIN (cursor))
15689 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15690 return NULL;
15692 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
15693 if (TREE_CODE (tree_body) != RETURN_EXPR)
15694 return NULL;
15695 tree_body = TREE_OPERAND (tree_body, 0);
15696 if (TREE_CODE (tree_body) != MODIFY_EXPR
15697 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15698 return NULL;
15699 tree_body = TREE_OPERAND (tree_body, 1);
15701 /* Try to translate the body expression itself. Note that this will probably
15702 cause an infinite recursion if its call graph has a cycle. This is very
15703 unlikely for size functions, however, so don't bother with such things at
15704 the moment. */
15705 ctx.context_type = NULL_TREE;
15706 ctx.base_decl = NULL_TREE;
15707 ctx.dpi = &dpi;
15708 dpi.fndecl = fndecl;
15709 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15710 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15711 if (!loc_body)
15712 return NULL;
15714 /* After evaluating all operands in "loc_body", we should still have on the
15715 stack all arguments plus the desired function result (top of the stack).
15716 Generate code in order to keep only the result in our stack frame. */
15717 epilogue = NULL;
15718 for (i = 0; i < dpi.args_count; ++i)
15720 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15721 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15722 op_couple->dw_loc_next->dw_loc_next = epilogue;
15723 epilogue = op_couple;
15725 add_loc_descr (&loc_body, epilogue);
15726 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15727 return NULL;
15729 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15730 because they are considered useful. Now there is an epilogue, they are
15731 not anymore, so give it another try. */
15732 loc_descr_without_nops (loc_body);
15734 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15735 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
15736 though, given that size functions do not come from source, so they should
15737 not have a dedicated DW_TAG_subprogram DIE. */
15738 dwarf_proc_die
15739 = new_dwarf_proc_die (loc_body, fndecl,
15740 get_context_die (DECL_CONTEXT (fndecl)));
15742 /* The called DWARF procedure consumes one stack slot per argument and
15743 returns one stack slot. */
15744 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15746 return dwarf_proc_die;
15750 /* Generate Dwarf location list representing LOC.
15751 If WANT_ADDRESS is false, expression computing LOC will be computed
15752 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15753 if WANT_ADDRESS is 2, expression computing address useable in location
15754 will be returned (i.e. DW_OP_reg can be used
15755 to refer to register values).
15757 CONTEXT provides information to customize the location descriptions
15758 generation. Its context_type field specifies what type is implicitly
15759 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
15760 will not be generated.
15762 Its DPI field determines whether we are generating a DWARF expression for a
15763 DWARF procedure, so PARM_DECL references are processed specifically.
15765 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15766 and dpi fields were null. */
15768 static dw_loc_list_ref
15769 loc_list_from_tree_1 (tree loc, int want_address,
15770 const struct loc_descr_context *context)
15772 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15773 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15774 int have_address = 0;
15775 enum dwarf_location_atom op;
15777 /* ??? Most of the time we do not take proper care for sign/zero
15778 extending the values properly. Hopefully this won't be a real
15779 problem... */
15781 if (context != NULL
15782 && context->base_decl == loc
15783 && want_address == 0)
15785 if (dwarf_version >= 3 || !dwarf_strict)
15786 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15787 NULL, NULL, NULL);
15788 else
15789 return NULL;
15792 switch (TREE_CODE (loc))
15794 case ERROR_MARK:
15795 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15796 return 0;
15798 case PLACEHOLDER_EXPR:
15799 /* This case involves extracting fields from an object to determine the
15800 position of other fields. It is supposed to appear only as the first
15801 operand of COMPONENT_REF nodes and to reference precisely the type
15802 that the context allows. */
15803 if (context != NULL
15804 && TREE_TYPE (loc) == context->context_type
15805 && want_address >= 1)
15807 if (dwarf_version >= 3 || !dwarf_strict)
15809 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15810 have_address = 1;
15811 break;
15813 else
15814 return NULL;
15816 else
15817 expansion_failed (loc, NULL_RTX,
15818 "PLACEHOLDER_EXPR for an unexpected type");
15819 break;
15821 case CALL_EXPR:
15823 const int nargs = call_expr_nargs (loc);
15824 tree callee = get_callee_fndecl (loc);
15825 int i;
15826 dw_die_ref dwarf_proc;
15828 if (callee == NULL_TREE)
15829 goto call_expansion_failed;
15831 /* We handle only functions that return an integer. */
15832 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15833 goto call_expansion_failed;
15835 dwarf_proc = function_to_dwarf_procedure (callee);
15836 if (dwarf_proc == NULL)
15837 goto call_expansion_failed;
15839 /* Evaluate arguments right-to-left so that the first argument will
15840 be the top-most one on the stack. */
15841 for (i = nargs - 1; i >= 0; --i)
15843 dw_loc_descr_ref loc_descr
15844 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15845 context);
15847 if (loc_descr == NULL)
15848 goto call_expansion_failed;
15850 add_loc_descr (&ret, loc_descr);
15853 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15854 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15855 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15856 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15857 add_loc_descr (&ret, ret1);
15858 break;
15860 call_expansion_failed:
15861 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15862 /* There are no opcodes for these operations. */
15863 return 0;
15866 case PREINCREMENT_EXPR:
15867 case PREDECREMENT_EXPR:
15868 case POSTINCREMENT_EXPR:
15869 case POSTDECREMENT_EXPR:
15870 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15871 /* There are no opcodes for these operations. */
15872 return 0;
15874 case ADDR_EXPR:
15875 /* If we already want an address, see if there is INDIRECT_REF inside
15876 e.g. for &this->field. */
15877 if (want_address)
15879 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15880 (loc, want_address == 2, context);
15881 if (list_ret)
15882 have_address = 1;
15883 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15884 && (ret = cst_pool_loc_descr (loc)))
15885 have_address = 1;
15887 /* Otherwise, process the argument and look for the address. */
15888 if (!list_ret && !ret)
15889 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15890 else
15892 if (want_address)
15893 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15894 return NULL;
15896 break;
15898 case VAR_DECL:
15899 if (DECL_THREAD_LOCAL_P (loc))
15901 rtx rtl;
15902 enum dwarf_location_atom tls_op;
15903 enum dtprel_bool dtprel = dtprel_false;
15905 if (targetm.have_tls)
15907 /* If this is not defined, we have no way to emit the
15908 data. */
15909 if (!targetm.asm_out.output_dwarf_dtprel)
15910 return 0;
15912 /* The way DW_OP_GNU_push_tls_address is specified, we
15913 can only look up addresses of objects in the current
15914 module. We used DW_OP_addr as first op, but that's
15915 wrong, because DW_OP_addr is relocated by the debug
15916 info consumer, while DW_OP_GNU_push_tls_address
15917 operand shouldn't be. */
15918 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15919 return 0;
15920 dtprel = dtprel_true;
15921 tls_op = DW_OP_GNU_push_tls_address;
15923 else
15925 if (!targetm.emutls.debug_form_tls_address
15926 || !(dwarf_version >= 3 || !dwarf_strict))
15927 return 0;
15928 /* We stuffed the control variable into the DECL_VALUE_EXPR
15929 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15930 no longer appear in gimple code. We used the control
15931 variable in specific so that we could pick it up here. */
15932 loc = DECL_VALUE_EXPR (loc);
15933 tls_op = DW_OP_form_tls_address;
15936 rtl = rtl_for_decl_location (loc);
15937 if (rtl == NULL_RTX)
15938 return 0;
15940 if (!MEM_P (rtl))
15941 return 0;
15942 rtl = XEXP (rtl, 0);
15943 if (! CONSTANT_P (rtl))
15944 return 0;
15946 ret = new_addr_loc_descr (rtl, dtprel);
15947 ret1 = new_loc_descr (tls_op, 0, 0);
15948 add_loc_descr (&ret, ret1);
15950 have_address = 1;
15951 break;
15953 /* FALLTHRU */
15955 case PARM_DECL:
15956 if (context != NULL && context->dpi != NULL
15957 && DECL_CONTEXT (loc) == context->dpi->fndecl)
15959 /* We are generating code for a DWARF procedure and we want to access
15960 one of its arguments: find the appropriate argument offset and let
15961 the resolve_args_picking pass compute the offset that complies
15962 with the stack frame size. */
15963 unsigned i = 0;
15964 tree cursor;
15966 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15967 cursor != NULL_TREE && cursor != loc;
15968 cursor = TREE_CHAIN (cursor), ++i)
15970 /* If we are translating a DWARF procedure, all referenced parameters
15971 must belong to the current function. */
15972 gcc_assert (cursor != NULL_TREE);
15974 ret = new_loc_descr (DW_OP_pick, i, 0);
15975 ret->frame_offset_rel = 1;
15976 break;
15978 /* FALLTHRU */
15980 case RESULT_DECL:
15981 if (DECL_HAS_VALUE_EXPR_P (loc))
15982 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15983 want_address, context);
15984 /* FALLTHRU */
15986 case FUNCTION_DECL:
15988 rtx rtl;
15989 var_loc_list *loc_list = lookup_decl_loc (loc);
15991 if (loc_list && loc_list->first)
15993 list_ret = dw_loc_list (loc_list, loc, want_address);
15994 have_address = want_address != 0;
15995 break;
15997 rtl = rtl_for_decl_location (loc);
15998 if (rtl == NULL_RTX)
16000 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
16001 return 0;
16003 else if (CONST_INT_P (rtl))
16005 HOST_WIDE_INT val = INTVAL (rtl);
16006 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16007 val &= GET_MODE_MASK (DECL_MODE (loc));
16008 ret = int_loc_descriptor (val);
16010 else if (GET_CODE (rtl) == CONST_STRING)
16012 expansion_failed (loc, NULL_RTX, "CONST_STRING");
16013 return 0;
16015 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16016 ret = new_addr_loc_descr (rtl, dtprel_false);
16017 else
16019 machine_mode mode, mem_mode;
16021 /* Certain constructs can only be represented at top-level. */
16022 if (want_address == 2)
16024 ret = loc_descriptor (rtl, VOIDmode,
16025 VAR_INIT_STATUS_INITIALIZED);
16026 have_address = 1;
16028 else
16030 mode = GET_MODE (rtl);
16031 mem_mode = VOIDmode;
16032 if (MEM_P (rtl))
16034 mem_mode = mode;
16035 mode = get_address_mode (rtl);
16036 rtl = XEXP (rtl, 0);
16037 have_address = 1;
16039 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16040 VAR_INIT_STATUS_INITIALIZED);
16042 if (!ret)
16043 expansion_failed (loc, rtl,
16044 "failed to produce loc descriptor for rtl");
16047 break;
16049 case MEM_REF:
16050 if (!integer_zerop (TREE_OPERAND (loc, 1)))
16052 have_address = 1;
16053 goto do_plus;
16055 /* Fallthru. */
16056 case INDIRECT_REF:
16057 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16058 have_address = 1;
16059 break;
16061 case TARGET_MEM_REF:
16062 case SSA_NAME:
16063 case DEBUG_EXPR_DECL:
16064 return NULL;
16066 case COMPOUND_EXPR:
16067 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16068 context);
16070 CASE_CONVERT:
16071 case VIEW_CONVERT_EXPR:
16072 case SAVE_EXPR:
16073 case MODIFY_EXPR:
16074 case NON_LVALUE_EXPR:
16075 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16076 context);
16078 case COMPONENT_REF:
16079 case BIT_FIELD_REF:
16080 case ARRAY_REF:
16081 case ARRAY_RANGE_REF:
16082 case REALPART_EXPR:
16083 case IMAGPART_EXPR:
16085 tree obj, offset;
16086 HOST_WIDE_INT bitsize, bitpos, bytepos;
16087 machine_mode mode;
16088 int unsignedp, reversep, volatilep = 0;
16090 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16091 &unsignedp, &reversep, &volatilep);
16093 gcc_assert (obj != loc);
16095 list_ret = loc_list_from_tree_1 (obj,
16096 want_address == 2
16097 && !bitpos && !offset ? 2 : 1,
16098 context);
16099 /* TODO: We can extract value of the small expression via shifting even
16100 for nonzero bitpos. */
16101 if (list_ret == 0)
16102 return 0;
16103 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16105 expansion_failed (loc, NULL_RTX,
16106 "bitfield access");
16107 return 0;
16110 if (offset != NULL_TREE)
16112 /* Variable offset. */
16113 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16114 if (list_ret1 == 0)
16115 return 0;
16116 add_loc_list (&list_ret, list_ret1);
16117 if (!list_ret)
16118 return 0;
16119 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16122 bytepos = bitpos / BITS_PER_UNIT;
16123 if (bytepos > 0)
16124 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16125 else if (bytepos < 0)
16126 loc_list_plus_const (list_ret, bytepos);
16128 have_address = 1;
16129 break;
16132 case INTEGER_CST:
16133 if ((want_address || !tree_fits_shwi_p (loc))
16134 && (ret = cst_pool_loc_descr (loc)))
16135 have_address = 1;
16136 else if (want_address == 2
16137 && tree_fits_shwi_p (loc)
16138 && (ret = address_of_int_loc_descriptor
16139 (int_size_in_bytes (TREE_TYPE (loc)),
16140 tree_to_shwi (loc))))
16141 have_address = 1;
16142 else if (tree_fits_shwi_p (loc))
16143 ret = int_loc_descriptor (tree_to_shwi (loc));
16144 else if (tree_fits_uhwi_p (loc))
16145 ret = uint_loc_descriptor (tree_to_uhwi (loc));
16146 else
16148 expansion_failed (loc, NULL_RTX,
16149 "Integer operand is not host integer");
16150 return 0;
16152 break;
16154 case CONSTRUCTOR:
16155 case REAL_CST:
16156 case STRING_CST:
16157 case COMPLEX_CST:
16158 if ((ret = cst_pool_loc_descr (loc)))
16159 have_address = 1;
16160 else if (TREE_CODE (loc) == CONSTRUCTOR)
16162 tree type = TREE_TYPE (loc);
16163 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
16164 unsigned HOST_WIDE_INT offset = 0;
16165 unsigned HOST_WIDE_INT cnt;
16166 constructor_elt *ce;
16168 if (TREE_CODE (type) == RECORD_TYPE)
16170 /* This is very limited, but it's enough to output
16171 pointers to member functions, as long as the
16172 referenced function is defined in the current
16173 translation unit. */
16174 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
16176 tree val = ce->value;
16178 tree field = ce->index;
16180 if (val)
16181 STRIP_NOPS (val);
16183 if (!field || DECL_BIT_FIELD (field))
16185 expansion_failed (loc, NULL_RTX,
16186 "bitfield in record type constructor");
16187 size = offset = (unsigned HOST_WIDE_INT)-1;
16188 ret = NULL;
16189 break;
16192 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16193 unsigned HOST_WIDE_INT pos = int_byte_position (field);
16194 gcc_assert (pos + fieldsize <= size);
16195 if (pos < offset)
16197 expansion_failed (loc, NULL_RTX,
16198 "out-of-order fields in record constructor");
16199 size = offset = (unsigned HOST_WIDE_INT)-1;
16200 ret = NULL;
16201 break;
16203 if (pos > offset)
16205 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
16206 add_loc_descr (&ret, ret1);
16207 offset = pos;
16209 if (val && fieldsize != 0)
16211 ret1 = loc_descriptor_from_tree (val, want_address, context);
16212 if (!ret1)
16214 expansion_failed (loc, NULL_RTX,
16215 "unsupported expression in field");
16216 size = offset = (unsigned HOST_WIDE_INT)-1;
16217 ret = NULL;
16218 break;
16220 add_loc_descr (&ret, ret1);
16222 if (fieldsize)
16224 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
16225 add_loc_descr (&ret, ret1);
16226 offset = pos + fieldsize;
16230 if (offset != size)
16232 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
16233 add_loc_descr (&ret, ret1);
16234 offset = size;
16237 have_address = !!want_address;
16239 else
16240 expansion_failed (loc, NULL_RTX,
16241 "constructor of non-record type");
16243 else
16244 /* We can construct small constants here using int_loc_descriptor. */
16245 expansion_failed (loc, NULL_RTX,
16246 "constructor or constant not in constant pool");
16247 break;
16249 case TRUTH_AND_EXPR:
16250 case TRUTH_ANDIF_EXPR:
16251 case BIT_AND_EXPR:
16252 op = DW_OP_and;
16253 goto do_binop;
16255 case TRUTH_XOR_EXPR:
16256 case BIT_XOR_EXPR:
16257 op = DW_OP_xor;
16258 goto do_binop;
16260 case TRUTH_OR_EXPR:
16261 case TRUTH_ORIF_EXPR:
16262 case BIT_IOR_EXPR:
16263 op = DW_OP_or;
16264 goto do_binop;
16266 case FLOOR_DIV_EXPR:
16267 case CEIL_DIV_EXPR:
16268 case ROUND_DIV_EXPR:
16269 case TRUNC_DIV_EXPR:
16270 case EXACT_DIV_EXPR:
16271 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16272 return 0;
16273 op = DW_OP_div;
16274 goto do_binop;
16276 case MINUS_EXPR:
16277 op = DW_OP_minus;
16278 goto do_binop;
16280 case FLOOR_MOD_EXPR:
16281 case CEIL_MOD_EXPR:
16282 case ROUND_MOD_EXPR:
16283 case TRUNC_MOD_EXPR:
16284 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16286 op = DW_OP_mod;
16287 goto do_binop;
16289 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16290 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16291 if (list_ret == 0 || list_ret1 == 0)
16292 return 0;
16294 add_loc_list (&list_ret, list_ret1);
16295 if (list_ret == 0)
16296 return 0;
16297 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16298 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16299 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16300 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16301 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16302 break;
16304 case MULT_EXPR:
16305 op = DW_OP_mul;
16306 goto do_binop;
16308 case LSHIFT_EXPR:
16309 op = DW_OP_shl;
16310 goto do_binop;
16312 case RSHIFT_EXPR:
16313 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16314 goto do_binop;
16316 case POINTER_PLUS_EXPR:
16317 case PLUS_EXPR:
16318 do_plus:
16319 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16321 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16322 smarter to encode their opposite. The DW_OP_plus_uconst operation
16323 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
16324 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16325 bytes, Y being the size of the operation that pushes the opposite
16326 of the addend. So let's choose the smallest representation. */
16327 const tree tree_addend = TREE_OPERAND (loc, 1);
16328 offset_int wi_addend;
16329 HOST_WIDE_INT shwi_addend;
16330 dw_loc_descr_ref loc_naddend;
16332 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16333 if (list_ret == 0)
16334 return 0;
16336 /* Try to get the literal to push. It is the opposite of the addend,
16337 so as we rely on wrapping during DWARF evaluation, first decode
16338 the literal as a "DWARF-sized" signed number. */
16339 wi_addend = wi::to_offset (tree_addend);
16340 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16341 shwi_addend = wi_addend.to_shwi ();
16342 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16343 ? int_loc_descriptor (-shwi_addend)
16344 : NULL;
16346 if (loc_naddend != NULL
16347 && ((unsigned) size_of_uleb128 (shwi_addend)
16348 > size_of_loc_descr (loc_naddend)))
16350 add_loc_descr_to_each (list_ret, loc_naddend);
16351 add_loc_descr_to_each (list_ret,
16352 new_loc_descr (DW_OP_minus, 0, 0));
16354 else
16356 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16358 loc_naddend = loc_cur;
16359 loc_cur = loc_cur->dw_loc_next;
16360 ggc_free (loc_naddend);
16362 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16364 break;
16367 op = DW_OP_plus;
16368 goto do_binop;
16370 case LE_EXPR:
16371 op = DW_OP_le;
16372 goto do_comp_binop;
16374 case GE_EXPR:
16375 op = DW_OP_ge;
16376 goto do_comp_binop;
16378 case LT_EXPR:
16379 op = DW_OP_lt;
16380 goto do_comp_binop;
16382 case GT_EXPR:
16383 op = DW_OP_gt;
16384 goto do_comp_binop;
16386 do_comp_binop:
16387 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16389 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16390 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16391 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16392 TREE_CODE (loc));
16393 break;
16395 else
16396 goto do_binop;
16398 case EQ_EXPR:
16399 op = DW_OP_eq;
16400 goto do_binop;
16402 case NE_EXPR:
16403 op = DW_OP_ne;
16404 goto do_binop;
16406 do_binop:
16407 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16408 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16409 if (list_ret == 0 || list_ret1 == 0)
16410 return 0;
16412 add_loc_list (&list_ret, list_ret1);
16413 if (list_ret == 0)
16414 return 0;
16415 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16416 break;
16418 case TRUTH_NOT_EXPR:
16419 case BIT_NOT_EXPR:
16420 op = DW_OP_not;
16421 goto do_unop;
16423 case ABS_EXPR:
16424 op = DW_OP_abs;
16425 goto do_unop;
16427 case NEGATE_EXPR:
16428 op = DW_OP_neg;
16429 goto do_unop;
16431 do_unop:
16432 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16433 if (list_ret == 0)
16434 return 0;
16436 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16437 break;
16439 case MIN_EXPR:
16440 case MAX_EXPR:
16442 const enum tree_code code =
16443 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16445 loc = build3 (COND_EXPR, TREE_TYPE (loc),
16446 build2 (code, integer_type_node,
16447 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16448 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16451 /* fall through */
16453 case COND_EXPR:
16455 dw_loc_descr_ref lhs
16456 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16457 dw_loc_list_ref rhs
16458 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16459 dw_loc_descr_ref bra_node, jump_node, tmp;
16461 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16462 if (list_ret == 0 || lhs == 0 || rhs == 0)
16463 return 0;
16465 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16466 add_loc_descr_to_each (list_ret, bra_node);
16468 add_loc_list (&list_ret, rhs);
16469 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16470 add_loc_descr_to_each (list_ret, jump_node);
16472 add_loc_descr_to_each (list_ret, lhs);
16473 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16474 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16476 /* ??? Need a node to point the skip at. Use a nop. */
16477 tmp = new_loc_descr (DW_OP_nop, 0, 0);
16478 add_loc_descr_to_each (list_ret, tmp);
16479 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16480 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16482 break;
16484 case FIX_TRUNC_EXPR:
16485 return 0;
16487 default:
16488 /* Leave front-end specific codes as simply unknown. This comes
16489 up, for instance, with the C STMT_EXPR. */
16490 if ((unsigned int) TREE_CODE (loc)
16491 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16493 expansion_failed (loc, NULL_RTX,
16494 "language specific tree node");
16495 return 0;
16498 /* Otherwise this is a generic code; we should just lists all of
16499 these explicitly. We forgot one. */
16500 if (flag_checking)
16501 gcc_unreachable ();
16503 /* In a release build, we want to degrade gracefully: better to
16504 generate incomplete debugging information than to crash. */
16505 return NULL;
16508 if (!ret && !list_ret)
16509 return 0;
16511 if (want_address == 2 && !have_address
16512 && (dwarf_version >= 4 || !dwarf_strict))
16514 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16516 expansion_failed (loc, NULL_RTX,
16517 "DWARF address size mismatch");
16518 return 0;
16520 if (ret)
16521 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16522 else
16523 add_loc_descr_to_each (list_ret,
16524 new_loc_descr (DW_OP_stack_value, 0, 0));
16525 have_address = 1;
16527 /* Show if we can't fill the request for an address. */
16528 if (want_address && !have_address)
16530 expansion_failed (loc, NULL_RTX,
16531 "Want address and only have value");
16532 return 0;
16535 gcc_assert (!ret || !list_ret);
16537 /* If we've got an address and don't want one, dereference. */
16538 if (!want_address && have_address)
16540 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16542 if (size > DWARF2_ADDR_SIZE || size == -1)
16544 expansion_failed (loc, NULL_RTX,
16545 "DWARF address size mismatch");
16546 return 0;
16548 else if (size == DWARF2_ADDR_SIZE)
16549 op = DW_OP_deref;
16550 else
16551 op = DW_OP_deref_size;
16553 if (ret)
16554 add_loc_descr (&ret, new_loc_descr (op, size, 0));
16555 else
16556 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16558 if (ret)
16559 list_ret = new_loc_list (ret, NULL, NULL, NULL);
16561 return list_ret;
16564 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16565 expressions. */
16567 static dw_loc_list_ref
16568 loc_list_from_tree (tree loc, int want_address,
16569 const struct loc_descr_context *context)
16571 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16573 for (dw_loc_list_ref loc_cur = result;
16574 loc_cur != NULL; loc_cur =
16575 loc_cur->dw_loc_next)
16576 loc_descr_without_nops (loc_cur->expr);
16577 return result;
16580 /* Same as above but return only single location expression. */
16581 static dw_loc_descr_ref
16582 loc_descriptor_from_tree (tree loc, int want_address,
16583 const struct loc_descr_context *context)
16585 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16586 if (!ret)
16587 return NULL;
16588 if (ret->dw_loc_next)
16590 expansion_failed (loc, NULL_RTX,
16591 "Location list where only loc descriptor needed");
16592 return NULL;
16594 return ret->expr;
16597 /* Given a value, round it up to the lowest multiple of `boundary'
16598 which is not less than the value itself. */
16600 static inline HOST_WIDE_INT
16601 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16603 return (((value + boundary - 1) / boundary) * boundary);
16606 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16607 pointer to the declared type for the relevant field variable, or return
16608 `integer_type_node' if the given node turns out to be an
16609 ERROR_MARK node. */
16611 static inline tree
16612 field_type (const_tree decl)
16614 tree type;
16616 if (TREE_CODE (decl) == ERROR_MARK)
16617 return integer_type_node;
16619 type = DECL_BIT_FIELD_TYPE (decl);
16620 if (type == NULL_TREE)
16621 type = TREE_TYPE (decl);
16623 return type;
16626 /* Given a pointer to a tree node, return the alignment in bits for
16627 it, or else return BITS_PER_WORD if the node actually turns out to
16628 be an ERROR_MARK node. */
16630 static inline unsigned
16631 simple_type_align_in_bits (const_tree type)
16633 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16636 static inline unsigned
16637 simple_decl_align_in_bits (const_tree decl)
16639 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16642 /* Return the result of rounding T up to ALIGN. */
16644 static inline offset_int
16645 round_up_to_align (const offset_int &t, unsigned int align)
16647 return wi::udiv_trunc (t + align - 1, align) * align;
16650 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
16651 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
16652 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
16653 if we fail to return the size in one of these two forms. */
16655 static dw_loc_descr_ref
16656 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16658 tree tree_size;
16659 struct loc_descr_context ctx;
16661 /* Return a constant integer in priority, if possible. */
16662 *cst_size = int_size_in_bytes (type);
16663 if (*cst_size != -1)
16664 return NULL;
16666 ctx.context_type = const_cast<tree> (type);
16667 ctx.base_decl = NULL_TREE;
16668 ctx.dpi = NULL;
16670 type = TYPE_MAIN_VARIANT (type);
16671 tree_size = TYPE_SIZE_UNIT (type);
16672 return ((tree_size != NULL_TREE)
16673 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16674 : NULL);
16677 /* Helper structure for RECORD_TYPE processing. */
16678 struct vlr_context
16680 /* Root RECORD_TYPE. It is needed to generate data member location
16681 descriptions in variable-length records (VLR), but also to cope with
16682 variants, which are composed of nested structures multiplexed with
16683 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
16684 function processing a FIELD_DECL, it is required to be non null. */
16685 tree struct_type;
16686 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16687 QUAL_UNION_TYPE), this holds an expression that computes the offset for
16688 this variant part as part of the root record (in storage units). For
16689 regular records, it must be NULL_TREE. */
16690 tree variant_part_offset;
16693 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16694 addressed byte of the "containing object" for the given FIELD_DECL. If
16695 possible, return a native constant through CST_OFFSET (in which case NULL is
16696 returned); otherwise return a DWARF expression that computes the offset.
16698 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16699 that offset is, either because the argument turns out to be a pointer to an
16700 ERROR_MARK node, or because the offset expression is too complex for us.
16702 CTX is required: see the comment for VLR_CONTEXT. */
16704 static dw_loc_descr_ref
16705 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16706 HOST_WIDE_INT *cst_offset)
16708 offset_int object_offset_in_bits;
16709 offset_int object_offset_in_bytes;
16710 offset_int bitpos_int;
16711 bool is_byte_offset_cst, is_bit_offset_cst;
16712 tree tree_result;
16713 dw_loc_list_ref loc_result;
16715 *cst_offset = 0;
16717 if (TREE_CODE (decl) == ERROR_MARK)
16718 return NULL;
16719 else
16720 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16722 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16723 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16725 /* We cannot handle variable bit offsets at the moment, so abort if it's the
16726 case. */
16727 if (is_bit_offset_cst)
16728 return NULL;
16730 #ifdef PCC_BITFIELD_TYPE_MATTERS
16731 /* We used to handle only constant offsets in all cases. Now, we handle
16732 properly dynamic byte offsets only when PCC bitfield type doesn't
16733 matter. */
16734 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16736 tree type;
16737 tree field_size_tree;
16738 offset_int deepest_bitpos;
16739 offset_int field_size_in_bits;
16740 unsigned int type_align_in_bits;
16741 unsigned int decl_align_in_bits;
16742 offset_int type_size_in_bits;
16744 bitpos_int = wi::to_offset (bit_position (decl));
16745 type = field_type (decl);
16746 type_size_in_bits = offset_int_type_size_in_bits (type);
16747 type_align_in_bits = simple_type_align_in_bits (type);
16749 field_size_tree = DECL_SIZE (decl);
16751 /* The size could be unspecified if there was an error, or for
16752 a flexible array member. */
16753 if (!field_size_tree)
16754 field_size_tree = bitsize_zero_node;
16756 /* If the size of the field is not constant, use the type size. */
16757 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16758 field_size_in_bits = wi::to_offset (field_size_tree);
16759 else
16760 field_size_in_bits = type_size_in_bits;
16762 decl_align_in_bits = simple_decl_align_in_bits (decl);
16764 /* The GCC front-end doesn't make any attempt to keep track of the
16765 starting bit offset (relative to the start of the containing
16766 structure type) of the hypothetical "containing object" for a
16767 bit-field. Thus, when computing the byte offset value for the
16768 start of the "containing object" of a bit-field, we must deduce
16769 this information on our own. This can be rather tricky to do in
16770 some cases. For example, handling the following structure type
16771 definition when compiling for an i386/i486 target (which only
16772 aligns long long's to 32-bit boundaries) can be very tricky:
16774 struct S { int field1; long long field2:31; };
16776 Fortunately, there is a simple rule-of-thumb which can be used
16777 in such cases. When compiling for an i386/i486, GCC will
16778 allocate 8 bytes for the structure shown above. It decides to
16779 do this based upon one simple rule for bit-field allocation.
16780 GCC allocates each "containing object" for each bit-field at
16781 the first (i.e. lowest addressed) legitimate alignment boundary
16782 (based upon the required minimum alignment for the declared
16783 type of the field) which it can possibly use, subject to the
16784 condition that there is still enough available space remaining
16785 in the containing object (when allocated at the selected point)
16786 to fully accommodate all of the bits of the bit-field itself.
16788 This simple rule makes it obvious why GCC allocates 8 bytes for
16789 each object of the structure type shown above. When looking
16790 for a place to allocate the "containing object" for `field2',
16791 the compiler simply tries to allocate a 64-bit "containing
16792 object" at each successive 32-bit boundary (starting at zero)
16793 until it finds a place to allocate that 64- bit field such that
16794 at least 31 contiguous (and previously unallocated) bits remain
16795 within that selected 64 bit field. (As it turns out, for the
16796 example above, the compiler finds it is OK to allocate the
16797 "containing object" 64-bit field at bit-offset zero within the
16798 structure type.)
16800 Here we attempt to work backwards from the limited set of facts
16801 we're given, and we try to deduce from those facts, where GCC
16802 must have believed that the containing object started (within
16803 the structure type). The value we deduce is then used (by the
16804 callers of this routine) to generate DW_AT_location and
16805 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16806 the case of DW_AT_location, regular fields as well). */
16808 /* Figure out the bit-distance from the start of the structure to
16809 the "deepest" bit of the bit-field. */
16810 deepest_bitpos = bitpos_int + field_size_in_bits;
16812 /* This is the tricky part. Use some fancy footwork to deduce
16813 where the lowest addressed bit of the containing object must
16814 be. */
16815 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16817 /* Round up to type_align by default. This works best for
16818 bitfields. */
16819 object_offset_in_bits
16820 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16822 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16824 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16826 /* Round up to decl_align instead. */
16827 object_offset_in_bits
16828 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16831 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16833 tree_result = byte_position (decl);
16834 if (ctx->variant_part_offset != NULL_TREE)
16835 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16836 ctx->variant_part_offset, tree_result));
16838 /* If the byte offset is a constant, it's simplier to handle a native
16839 constant rather than a DWARF expression. */
16840 if (TREE_CODE (tree_result) == INTEGER_CST)
16842 *cst_offset = wi::to_offset (tree_result).to_shwi ();
16843 return NULL;
16845 struct loc_descr_context loc_ctx = {
16846 ctx->struct_type, /* context_type */
16847 NULL_TREE, /* base_decl */
16848 NULL /* dpi */
16850 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16852 /* We want a DWARF expression: abort if we only have a location list with
16853 multiple elements. */
16854 if (!loc_result || !single_element_loc_list_p (loc_result))
16855 return NULL;
16856 else
16857 return loc_result->expr;
16860 /* The following routines define various Dwarf attributes and any data
16861 associated with them. */
16863 /* Add a location description attribute value to a DIE.
16865 This emits location attributes suitable for whole variables and
16866 whole parameters. Note that the location attributes for struct fields are
16867 generated by the routine `data_member_location_attribute' below. */
16869 static inline void
16870 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16871 dw_loc_list_ref descr)
16873 if (descr == 0)
16874 return;
16875 if (single_element_loc_list_p (descr))
16876 add_AT_loc (die, attr_kind, descr->expr);
16877 else
16878 add_AT_loc_list (die, attr_kind, descr);
16881 /* Add DW_AT_accessibility attribute to DIE if needed. */
16883 static void
16884 add_accessibility_attribute (dw_die_ref die, tree decl)
16886 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16887 children, otherwise the default is DW_ACCESS_public. In DWARF2
16888 the default has always been DW_ACCESS_public. */
16889 if (TREE_PROTECTED (decl))
16890 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16891 else if (TREE_PRIVATE (decl))
16893 if (dwarf_version == 2
16894 || die->die_parent == NULL
16895 || die->die_parent->die_tag != DW_TAG_class_type)
16896 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16898 else if (dwarf_version > 2
16899 && die->die_parent
16900 && die->die_parent->die_tag == DW_TAG_class_type)
16901 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16904 /* Attach the specialized form of location attribute used for data members of
16905 struct and union types. In the special case of a FIELD_DECL node which
16906 represents a bit-field, the "offset" part of this special location
16907 descriptor must indicate the distance in bytes from the lowest-addressed
16908 byte of the containing struct or union type to the lowest-addressed byte of
16909 the "containing object" for the bit-field. (See the `field_byte_offset'
16910 function above).
16912 For any given bit-field, the "containing object" is a hypothetical object
16913 (of some integral or enum type) within which the given bit-field lives. The
16914 type of this hypothetical "containing object" is always the same as the
16915 declared type of the individual bit-field itself (for GCC anyway... the
16916 DWARF spec doesn't actually mandate this). Note that it is the size (in
16917 bytes) of the hypothetical "containing object" which will be given in the
16918 DW_AT_byte_size attribute for this bit-field. (See the
16919 `byte_size_attribute' function below.) It is also used when calculating the
16920 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16921 function below.)
16923 CTX is required: see the comment for VLR_CONTEXT. */
16925 static void
16926 add_data_member_location_attribute (dw_die_ref die,
16927 tree decl,
16928 struct vlr_context *ctx)
16930 HOST_WIDE_INT offset;
16931 dw_loc_descr_ref loc_descr = 0;
16933 if (TREE_CODE (decl) == TREE_BINFO)
16935 /* We're working on the TAG_inheritance for a base class. */
16936 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16938 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16939 aren't at a fixed offset from all (sub)objects of the same
16940 type. We need to extract the appropriate offset from our
16941 vtable. The following dwarf expression means
16943 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16945 This is specific to the V3 ABI, of course. */
16947 dw_loc_descr_ref tmp;
16949 /* Make a copy of the object address. */
16950 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16951 add_loc_descr (&loc_descr, tmp);
16953 /* Extract the vtable address. */
16954 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16955 add_loc_descr (&loc_descr, tmp);
16957 /* Calculate the address of the offset. */
16958 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16959 gcc_assert (offset < 0);
16961 tmp = int_loc_descriptor (-offset);
16962 add_loc_descr (&loc_descr, tmp);
16963 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16964 add_loc_descr (&loc_descr, tmp);
16966 /* Extract the offset. */
16967 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16968 add_loc_descr (&loc_descr, tmp);
16970 /* Add it to the object address. */
16971 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16972 add_loc_descr (&loc_descr, tmp);
16974 else
16975 offset = tree_to_shwi (BINFO_OFFSET (decl));
16977 else
16979 loc_descr = field_byte_offset (decl, ctx, &offset);
16981 /* If loc_descr is available then we know the field offset is dynamic.
16982 However, GDB does not handle dynamic field offsets very well at the
16983 moment. */
16984 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16986 loc_descr = NULL;
16987 offset = 0;
16990 /* Data member location evalutation starts with the base address on the
16991 stack. Compute the field offset and add it to this base address. */
16992 else if (loc_descr != NULL)
16993 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
16996 if (! loc_descr)
16998 if (dwarf_version > 2)
17000 /* Don't need to output a location expression, just the constant. */
17001 if (offset < 0)
17002 add_AT_int (die, DW_AT_data_member_location, offset);
17003 else
17004 add_AT_unsigned (die, DW_AT_data_member_location, offset);
17005 return;
17007 else
17009 enum dwarf_location_atom op;
17011 /* The DWARF2 standard says that we should assume that the structure
17012 address is already on the stack, so we can specify a structure
17013 field address by using DW_OP_plus_uconst. */
17014 op = DW_OP_plus_uconst;
17015 loc_descr = new_loc_descr (op, offset, 0);
17019 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17022 /* Writes integer values to dw_vec_const array. */
17024 static void
17025 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17027 while (size != 0)
17029 *dest++ = val & 0xff;
17030 val >>= 8;
17031 --size;
17035 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
17037 static HOST_WIDE_INT
17038 extract_int (const unsigned char *src, unsigned int size)
17040 HOST_WIDE_INT val = 0;
17042 src += size;
17043 while (size != 0)
17045 val <<= 8;
17046 val |= *--src & 0xff;
17047 --size;
17049 return val;
17052 /* Writes wide_int values to dw_vec_const array. */
17054 static void
17055 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
17057 int i;
17059 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
17061 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
17062 return;
17065 /* We'd have to extend this code to support odd sizes. */
17066 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
17068 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
17070 if (WORDS_BIG_ENDIAN)
17071 for (i = n - 1; i >= 0; i--)
17073 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17074 dest += sizeof (HOST_WIDE_INT);
17076 else
17077 for (i = 0; i < n; i++)
17079 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17080 dest += sizeof (HOST_WIDE_INT);
17084 /* Writes floating point values to dw_vec_const array. */
17086 static void
17087 insert_float (const_rtx rtl, unsigned char *array)
17089 long val[4];
17090 int i;
17092 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
17094 /* real_to_target puts 32-bit pieces in each long. Pack them. */
17095 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17097 insert_int (val[i], 4, array);
17098 array += 4;
17102 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17103 does not have a "location" either in memory or in a register. These
17104 things can arise in GNU C when a constant is passed as an actual parameter
17105 to an inlined function. They can also arise in C++ where declared
17106 constants do not necessarily get memory "homes". */
17108 static bool
17109 add_const_value_attribute (dw_die_ref die, rtx rtl)
17111 switch (GET_CODE (rtl))
17113 case CONST_INT:
17115 HOST_WIDE_INT val = INTVAL (rtl);
17117 if (val < 0)
17118 add_AT_int (die, DW_AT_const_value, val);
17119 else
17120 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17122 return true;
17124 case CONST_WIDE_INT:
17126 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17127 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17128 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17129 wide_int w = wi::zext (w1, prec);
17130 add_AT_wide (die, DW_AT_const_value, w);
17132 return true;
17134 case CONST_DOUBLE:
17135 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17136 floating-point constant. A CONST_DOUBLE is used whenever the
17137 constant requires more than one word in order to be adequately
17138 represented. */
17140 machine_mode mode = GET_MODE (rtl);
17142 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17143 add_AT_double (die, DW_AT_const_value,
17144 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17145 else
17147 unsigned int length = GET_MODE_SIZE (mode);
17148 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17150 insert_float (rtl, array);
17151 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17154 return true;
17156 case CONST_VECTOR:
17158 machine_mode mode = GET_MODE (rtl);
17159 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17160 unsigned int length = CONST_VECTOR_NUNITS (rtl);
17161 unsigned char *array
17162 = ggc_vec_alloc<unsigned char> (length * elt_size);
17163 unsigned int i;
17164 unsigned char *p;
17165 machine_mode imode = GET_MODE_INNER (mode);
17167 switch (GET_MODE_CLASS (mode))
17169 case MODE_VECTOR_INT:
17170 for (i = 0, p = array; i < length; i++, p += elt_size)
17172 rtx elt = CONST_VECTOR_ELT (rtl, i);
17173 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17175 break;
17177 case MODE_VECTOR_FLOAT:
17178 for (i = 0, p = array; i < length; i++, p += elt_size)
17180 rtx elt = CONST_VECTOR_ELT (rtl, i);
17181 insert_float (elt, p);
17183 break;
17185 default:
17186 gcc_unreachable ();
17189 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17191 return true;
17193 case CONST_STRING:
17194 if (dwarf_version >= 4 || !dwarf_strict)
17196 dw_loc_descr_ref loc_result;
17197 resolve_one_addr (&rtl);
17198 rtl_addr:
17199 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17200 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17201 add_AT_loc (die, DW_AT_location, loc_result);
17202 vec_safe_push (used_rtx_array, rtl);
17203 return true;
17205 return false;
17207 case CONST:
17208 if (CONSTANT_P (XEXP (rtl, 0)))
17209 return add_const_value_attribute (die, XEXP (rtl, 0));
17210 /* FALLTHROUGH */
17211 case SYMBOL_REF:
17212 if (!const_ok_for_output (rtl))
17213 return false;
17214 case LABEL_REF:
17215 if (dwarf_version >= 4 || !dwarf_strict)
17216 goto rtl_addr;
17217 return false;
17219 case PLUS:
17220 /* In cases where an inlined instance of an inline function is passed
17221 the address of an `auto' variable (which is local to the caller) we
17222 can get a situation where the DECL_RTL of the artificial local
17223 variable (for the inlining) which acts as a stand-in for the
17224 corresponding formal parameter (of the inline function) will look
17225 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
17226 exactly a compile-time constant expression, but it isn't the address
17227 of the (artificial) local variable either. Rather, it represents the
17228 *value* which the artificial local variable always has during its
17229 lifetime. We currently have no way to represent such quasi-constant
17230 values in Dwarf, so for now we just punt and generate nothing. */
17231 return false;
17233 case HIGH:
17234 case CONST_FIXED:
17235 return false;
17237 case MEM:
17238 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17239 && MEM_READONLY_P (rtl)
17240 && GET_MODE (rtl) == BLKmode)
17242 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17243 return true;
17245 return false;
17247 default:
17248 /* No other kinds of rtx should be possible here. */
17249 gcc_unreachable ();
17251 return false;
17254 /* Determine whether the evaluation of EXPR references any variables
17255 or functions which aren't otherwise used (and therefore may not be
17256 output). */
17257 static tree
17258 reference_to_unused (tree * tp, int * walk_subtrees,
17259 void * data ATTRIBUTE_UNUSED)
17261 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17262 *walk_subtrees = 0;
17264 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17265 && ! TREE_ASM_WRITTEN (*tp))
17266 return *tp;
17267 /* ??? The C++ FE emits debug information for using decls, so
17268 putting gcc_unreachable here falls over. See PR31899. For now
17269 be conservative. */
17270 else if (!symtab->global_info_ready
17271 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17272 return *tp;
17273 else if (TREE_CODE (*tp) == VAR_DECL)
17275 varpool_node *node = varpool_node::get (*tp);
17276 if (!node || !node->definition)
17277 return *tp;
17279 else if (TREE_CODE (*tp) == FUNCTION_DECL
17280 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17282 /* The call graph machinery must have finished analyzing,
17283 optimizing and gimplifying the CU by now.
17284 So if *TP has no call graph node associated
17285 to it, it means *TP will not be emitted. */
17286 if (!cgraph_node::get (*tp))
17287 return *tp;
17289 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17290 return *tp;
17292 return NULL_TREE;
17295 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17296 for use in a later add_const_value_attribute call. */
17298 static rtx
17299 rtl_for_decl_init (tree init, tree type)
17301 rtx rtl = NULL_RTX;
17303 STRIP_NOPS (init);
17305 /* If a variable is initialized with a string constant without embedded
17306 zeros, build CONST_STRING. */
17307 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17309 tree enttype = TREE_TYPE (type);
17310 tree domain = TYPE_DOMAIN (type);
17311 machine_mode mode = TYPE_MODE (enttype);
17313 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17314 && domain
17315 && integer_zerop (TYPE_MIN_VALUE (domain))
17316 && compare_tree_int (TYPE_MAX_VALUE (domain),
17317 TREE_STRING_LENGTH (init) - 1) == 0
17318 && ((size_t) TREE_STRING_LENGTH (init)
17319 == strlen (TREE_STRING_POINTER (init)) + 1))
17321 rtl = gen_rtx_CONST_STRING (VOIDmode,
17322 ggc_strdup (TREE_STRING_POINTER (init)));
17323 rtl = gen_rtx_MEM (BLKmode, rtl);
17324 MEM_READONLY_P (rtl) = 1;
17327 /* Other aggregates, and complex values, could be represented using
17328 CONCAT: FIXME! */
17329 else if (AGGREGATE_TYPE_P (type)
17330 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17331 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17332 || TREE_CODE (type) == COMPLEX_TYPE)
17334 /* Vectors only work if their mode is supported by the target.
17335 FIXME: generic vectors ought to work too. */
17336 else if (TREE_CODE (type) == VECTOR_TYPE
17337 && !VECTOR_MODE_P (TYPE_MODE (type)))
17339 /* If the initializer is something that we know will expand into an
17340 immediate RTL constant, expand it now. We must be careful not to
17341 reference variables which won't be output. */
17342 else if (initializer_constant_valid_p (init, type)
17343 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17345 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17346 possible. */
17347 if (TREE_CODE (type) == VECTOR_TYPE)
17348 switch (TREE_CODE (init))
17350 case VECTOR_CST:
17351 break;
17352 case CONSTRUCTOR:
17353 if (TREE_CONSTANT (init))
17355 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17356 bool constant_p = true;
17357 tree value;
17358 unsigned HOST_WIDE_INT ix;
17360 /* Even when ctor is constant, it might contain non-*_CST
17361 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17362 belong into VECTOR_CST nodes. */
17363 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17364 if (!CONSTANT_CLASS_P (value))
17366 constant_p = false;
17367 break;
17370 if (constant_p)
17372 init = build_vector_from_ctor (type, elts);
17373 break;
17376 /* FALLTHRU */
17378 default:
17379 return NULL;
17382 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17384 /* If expand_expr returns a MEM, it wasn't immediate. */
17385 gcc_assert (!rtl || !MEM_P (rtl));
17388 return rtl;
17391 /* Generate RTL for the variable DECL to represent its location. */
17393 static rtx
17394 rtl_for_decl_location (tree decl)
17396 rtx rtl;
17398 /* Here we have to decide where we are going to say the parameter "lives"
17399 (as far as the debugger is concerned). We only have a couple of
17400 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17402 DECL_RTL normally indicates where the parameter lives during most of the
17403 activation of the function. If optimization is enabled however, this
17404 could be either NULL or else a pseudo-reg. Both of those cases indicate
17405 that the parameter doesn't really live anywhere (as far as the code
17406 generation parts of GCC are concerned) during most of the function's
17407 activation. That will happen (for example) if the parameter is never
17408 referenced within the function.
17410 We could just generate a location descriptor here for all non-NULL
17411 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17412 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17413 where DECL_RTL is NULL or is a pseudo-reg.
17415 Note however that we can only get away with using DECL_INCOMING_RTL as
17416 a backup substitute for DECL_RTL in certain limited cases. In cases
17417 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17418 we can be sure that the parameter was passed using the same type as it is
17419 declared to have within the function, and that its DECL_INCOMING_RTL
17420 points us to a place where a value of that type is passed.
17422 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17423 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17424 because in these cases DECL_INCOMING_RTL points us to a value of some
17425 type which is *different* from the type of the parameter itself. Thus,
17426 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17427 such cases, the debugger would end up (for example) trying to fetch a
17428 `float' from a place which actually contains the first part of a
17429 `double'. That would lead to really incorrect and confusing
17430 output at debug-time.
17432 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17433 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
17434 are a couple of exceptions however. On little-endian machines we can
17435 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17436 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17437 an integral type that is smaller than TREE_TYPE (decl). These cases arise
17438 when (on a little-endian machine) a non-prototyped function has a
17439 parameter declared to be of type `short' or `char'. In such cases,
17440 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17441 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17442 passed `int' value. If the debugger then uses that address to fetch
17443 a `short' or a `char' (on a little-endian machine) the result will be
17444 the correct data, so we allow for such exceptional cases below.
17446 Note that our goal here is to describe the place where the given formal
17447 parameter lives during most of the function's activation (i.e. between the
17448 end of the prologue and the start of the epilogue). We'll do that as best
17449 as we can. Note however that if the given formal parameter is modified
17450 sometime during the execution of the function, then a stack backtrace (at
17451 debug-time) will show the function as having been called with the *new*
17452 value rather than the value which was originally passed in. This happens
17453 rarely enough that it is not a major problem, but it *is* a problem, and
17454 I'd like to fix it.
17456 A future version of dwarf2out.c may generate two additional attributes for
17457 any given DW_TAG_formal_parameter DIE which will describe the "passed
17458 type" and the "passed location" for the given formal parameter in addition
17459 to the attributes we now generate to indicate the "declared type" and the
17460 "active location" for each parameter. This additional set of attributes
17461 could be used by debuggers for stack backtraces. Separately, note that
17462 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17463 This happens (for example) for inlined-instances of inline function formal
17464 parameters which are never referenced. This really shouldn't be
17465 happening. All PARM_DECL nodes should get valid non-NULL
17466 DECL_INCOMING_RTL values. FIXME. */
17468 /* Use DECL_RTL as the "location" unless we find something better. */
17469 rtl = DECL_RTL_IF_SET (decl);
17471 /* When generating abstract instances, ignore everything except
17472 constants, symbols living in memory, and symbols living in
17473 fixed registers. */
17474 if (! reload_completed)
17476 if (rtl
17477 && (CONSTANT_P (rtl)
17478 || (MEM_P (rtl)
17479 && CONSTANT_P (XEXP (rtl, 0)))
17480 || (REG_P (rtl)
17481 && TREE_CODE (decl) == VAR_DECL
17482 && TREE_STATIC (decl))))
17484 rtl = targetm.delegitimize_address (rtl);
17485 return rtl;
17487 rtl = NULL_RTX;
17489 else if (TREE_CODE (decl) == PARM_DECL)
17491 if (rtl == NULL_RTX
17492 || is_pseudo_reg (rtl)
17493 || (MEM_P (rtl)
17494 && is_pseudo_reg (XEXP (rtl, 0))
17495 && DECL_INCOMING_RTL (decl)
17496 && MEM_P (DECL_INCOMING_RTL (decl))
17497 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17499 tree declared_type = TREE_TYPE (decl);
17500 tree passed_type = DECL_ARG_TYPE (decl);
17501 machine_mode dmode = TYPE_MODE (declared_type);
17502 machine_mode pmode = TYPE_MODE (passed_type);
17504 /* This decl represents a formal parameter which was optimized out.
17505 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17506 all cases where (rtl == NULL_RTX) just below. */
17507 if (dmode == pmode)
17508 rtl = DECL_INCOMING_RTL (decl);
17509 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17510 && SCALAR_INT_MODE_P (dmode)
17511 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17512 && DECL_INCOMING_RTL (decl))
17514 rtx inc = DECL_INCOMING_RTL (decl);
17515 if (REG_P (inc))
17516 rtl = inc;
17517 else if (MEM_P (inc))
17519 if (BYTES_BIG_ENDIAN)
17520 rtl = adjust_address_nv (inc, dmode,
17521 GET_MODE_SIZE (pmode)
17522 - GET_MODE_SIZE (dmode));
17523 else
17524 rtl = inc;
17529 /* If the parm was passed in registers, but lives on the stack, then
17530 make a big endian correction if the mode of the type of the
17531 parameter is not the same as the mode of the rtl. */
17532 /* ??? This is the same series of checks that are made in dbxout.c before
17533 we reach the big endian correction code there. It isn't clear if all
17534 of these checks are necessary here, but keeping them all is the safe
17535 thing to do. */
17536 else if (MEM_P (rtl)
17537 && XEXP (rtl, 0) != const0_rtx
17538 && ! CONSTANT_P (XEXP (rtl, 0))
17539 /* Not passed in memory. */
17540 && !MEM_P (DECL_INCOMING_RTL (decl))
17541 /* Not passed by invisible reference. */
17542 && (!REG_P (XEXP (rtl, 0))
17543 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17544 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17545 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17546 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17547 #endif
17549 /* Big endian correction check. */
17550 && BYTES_BIG_ENDIAN
17551 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17552 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17553 < UNITS_PER_WORD))
17555 machine_mode addr_mode = get_address_mode (rtl);
17556 int offset = (UNITS_PER_WORD
17557 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17559 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17560 plus_constant (addr_mode, XEXP (rtl, 0), offset));
17563 else if (TREE_CODE (decl) == VAR_DECL
17564 && rtl
17565 && MEM_P (rtl)
17566 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17567 && BYTES_BIG_ENDIAN)
17569 machine_mode addr_mode = get_address_mode (rtl);
17570 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17571 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17573 /* If a variable is declared "register" yet is smaller than
17574 a register, then if we store the variable to memory, it
17575 looks like we're storing a register-sized value, when in
17576 fact we are not. We need to adjust the offset of the
17577 storage location to reflect the actual value's bytes,
17578 else gdb will not be able to display it. */
17579 if (rsize > dsize)
17580 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17581 plus_constant (addr_mode, XEXP (rtl, 0),
17582 rsize - dsize));
17585 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17586 and will have been substituted directly into all expressions that use it.
17587 C does not have such a concept, but C++ and other languages do. */
17588 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17589 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17591 if (rtl)
17592 rtl = targetm.delegitimize_address (rtl);
17594 /* If we don't look past the constant pool, we risk emitting a
17595 reference to a constant pool entry that isn't referenced from
17596 code, and thus is not emitted. */
17597 if (rtl)
17598 rtl = avoid_constant_pool_reference (rtl);
17600 /* Try harder to get a rtl. If this symbol ends up not being emitted
17601 in the current CU, resolve_addr will remove the expression referencing
17602 it. */
17603 if (rtl == NULL_RTX
17604 && TREE_CODE (decl) == VAR_DECL
17605 && !DECL_EXTERNAL (decl)
17606 && TREE_STATIC (decl)
17607 && DECL_NAME (decl)
17608 && !DECL_HARD_REGISTER (decl)
17609 && DECL_MODE (decl) != VOIDmode)
17611 rtl = make_decl_rtl_for_debug (decl);
17612 if (!MEM_P (rtl)
17613 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17614 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17615 rtl = NULL_RTX;
17618 return rtl;
17621 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
17622 returned. If so, the decl for the COMMON block is returned, and the
17623 value is the offset into the common block for the symbol. */
17625 static tree
17626 fortran_common (tree decl, HOST_WIDE_INT *value)
17628 tree val_expr, cvar;
17629 machine_mode mode;
17630 HOST_WIDE_INT bitsize, bitpos;
17631 tree offset;
17632 int unsignedp, reversep, volatilep = 0;
17634 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17635 it does not have a value (the offset into the common area), or if it
17636 is thread local (as opposed to global) then it isn't common, and shouldn't
17637 be handled as such. */
17638 if (TREE_CODE (decl) != VAR_DECL
17639 || !TREE_STATIC (decl)
17640 || !DECL_HAS_VALUE_EXPR_P (decl)
17641 || !is_fortran ())
17642 return NULL_TREE;
17644 val_expr = DECL_VALUE_EXPR (decl);
17645 if (TREE_CODE (val_expr) != COMPONENT_REF)
17646 return NULL_TREE;
17648 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17649 &unsignedp, &reversep, &volatilep);
17651 if (cvar == NULL_TREE
17652 || TREE_CODE (cvar) != VAR_DECL
17653 || DECL_ARTIFICIAL (cvar)
17654 || !TREE_PUBLIC (cvar))
17655 return NULL_TREE;
17657 *value = 0;
17658 if (offset != NULL)
17660 if (!tree_fits_shwi_p (offset))
17661 return NULL_TREE;
17662 *value = tree_to_shwi (offset);
17664 if (bitpos != 0)
17665 *value += bitpos / BITS_PER_UNIT;
17667 return cvar;
17670 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17671 data attribute for a variable or a parameter. We generate the
17672 DW_AT_const_value attribute only in those cases where the given variable
17673 or parameter does not have a true "location" either in memory or in a
17674 register. This can happen (for example) when a constant is passed as an
17675 actual argument in a call to an inline function. (It's possible that
17676 these things can crop up in other ways also.) Note that one type of
17677 constant value which can be passed into an inlined function is a constant
17678 pointer. This can happen for example if an actual argument in an inlined
17679 function call evaluates to a compile-time constant address.
17681 CACHE_P is true if it is worth caching the location list for DECL,
17682 so that future calls can reuse it rather than regenerate it from scratch.
17683 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17684 since we will need to refer to them each time the function is inlined. */
17686 static bool
17687 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17689 rtx rtl;
17690 dw_loc_list_ref list;
17691 var_loc_list *loc_list;
17692 cached_dw_loc_list *cache;
17694 if (early_dwarf)
17695 return false;
17697 if (TREE_CODE (decl) == ERROR_MARK)
17698 return false;
17700 if (get_AT (die, DW_AT_location)
17701 || get_AT (die, DW_AT_const_value))
17702 return true;
17704 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17705 || TREE_CODE (decl) == RESULT_DECL);
17707 /* Try to get some constant RTL for this decl, and use that as the value of
17708 the location. */
17710 rtl = rtl_for_decl_location (decl);
17711 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17712 && add_const_value_attribute (die, rtl))
17713 return true;
17715 /* See if we have single element location list that is equivalent to
17716 a constant value. That way we are better to use add_const_value_attribute
17717 rather than expanding constant value equivalent. */
17718 loc_list = lookup_decl_loc (decl);
17719 if (loc_list
17720 && loc_list->first
17721 && loc_list->first->next == NULL
17722 && NOTE_P (loc_list->first->loc)
17723 && NOTE_VAR_LOCATION (loc_list->first->loc)
17724 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17726 struct var_loc_node *node;
17728 node = loc_list->first;
17729 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17730 if (GET_CODE (rtl) == EXPR_LIST)
17731 rtl = XEXP (rtl, 0);
17732 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17733 && add_const_value_attribute (die, rtl))
17734 return true;
17736 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17737 list several times. See if we've already cached the contents. */
17738 list = NULL;
17739 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17740 cache_p = false;
17741 if (cache_p)
17743 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17744 if (cache)
17745 list = cache->loc_list;
17747 if (list == NULL)
17749 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17750 NULL);
17751 /* It is usually worth caching this result if the decl is from
17752 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17753 if (cache_p && list && list->dw_loc_next)
17755 cached_dw_loc_list **slot
17756 = cached_dw_loc_list_table->find_slot_with_hash (decl,
17757 DECL_UID (decl),
17758 INSERT);
17759 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17760 cache->decl_id = DECL_UID (decl);
17761 cache->loc_list = list;
17762 *slot = cache;
17765 if (list)
17767 add_AT_location_description (die, DW_AT_location, list);
17768 return true;
17770 /* None of that worked, so it must not really have a location;
17771 try adding a constant value attribute from the DECL_INITIAL. */
17772 return tree_add_const_value_attribute_for_decl (die, decl);
17775 /* Helper function for tree_add_const_value_attribute. Natively encode
17776 initializer INIT into an array. Return true if successful. */
17778 static bool
17779 native_encode_initializer (tree init, unsigned char *array, int size)
17781 tree type;
17783 if (init == NULL_TREE)
17784 return false;
17786 STRIP_NOPS (init);
17787 switch (TREE_CODE (init))
17789 case STRING_CST:
17790 type = TREE_TYPE (init);
17791 if (TREE_CODE (type) == ARRAY_TYPE)
17793 tree enttype = TREE_TYPE (type);
17794 machine_mode mode = TYPE_MODE (enttype);
17796 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17797 return false;
17798 if (int_size_in_bytes (type) != size)
17799 return false;
17800 if (size > TREE_STRING_LENGTH (init))
17802 memcpy (array, TREE_STRING_POINTER (init),
17803 TREE_STRING_LENGTH (init));
17804 memset (array + TREE_STRING_LENGTH (init),
17805 '\0', size - TREE_STRING_LENGTH (init));
17807 else
17808 memcpy (array, TREE_STRING_POINTER (init), size);
17809 return true;
17811 return false;
17812 case CONSTRUCTOR:
17813 type = TREE_TYPE (init);
17814 if (int_size_in_bytes (type) != size)
17815 return false;
17816 if (TREE_CODE (type) == ARRAY_TYPE)
17818 HOST_WIDE_INT min_index;
17819 unsigned HOST_WIDE_INT cnt;
17820 int curpos = 0, fieldsize;
17821 constructor_elt *ce;
17823 if (TYPE_DOMAIN (type) == NULL_TREE
17824 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17825 return false;
17827 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17828 if (fieldsize <= 0)
17829 return false;
17831 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17832 memset (array, '\0', size);
17833 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17835 tree val = ce->value;
17836 tree index = ce->index;
17837 int pos = curpos;
17838 if (index && TREE_CODE (index) == RANGE_EXPR)
17839 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17840 * fieldsize;
17841 else if (index)
17842 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17844 if (val)
17846 STRIP_NOPS (val);
17847 if (!native_encode_initializer (val, array + pos, fieldsize))
17848 return false;
17850 curpos = pos + fieldsize;
17851 if (index && TREE_CODE (index) == RANGE_EXPR)
17853 int count = tree_to_shwi (TREE_OPERAND (index, 1))
17854 - tree_to_shwi (TREE_OPERAND (index, 0));
17855 while (count-- > 0)
17857 if (val)
17858 memcpy (array + curpos, array + pos, fieldsize);
17859 curpos += fieldsize;
17862 gcc_assert (curpos <= size);
17864 return true;
17866 else if (TREE_CODE (type) == RECORD_TYPE
17867 || TREE_CODE (type) == UNION_TYPE)
17869 tree field = NULL_TREE;
17870 unsigned HOST_WIDE_INT cnt;
17871 constructor_elt *ce;
17873 if (int_size_in_bytes (type) != size)
17874 return false;
17876 if (TREE_CODE (type) == RECORD_TYPE)
17877 field = TYPE_FIELDS (type);
17879 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17881 tree val = ce->value;
17882 int pos, fieldsize;
17884 if (ce->index != 0)
17885 field = ce->index;
17887 if (val)
17888 STRIP_NOPS (val);
17890 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17891 return false;
17893 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17894 && TYPE_DOMAIN (TREE_TYPE (field))
17895 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17896 return false;
17897 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17898 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17899 return false;
17900 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17901 pos = int_byte_position (field);
17902 gcc_assert (pos + fieldsize <= size);
17903 if (val && fieldsize != 0
17904 && !native_encode_initializer (val, array + pos, fieldsize))
17905 return false;
17907 return true;
17909 return false;
17910 case VIEW_CONVERT_EXPR:
17911 case NON_LVALUE_EXPR:
17912 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17913 default:
17914 return native_encode_expr (init, array, size) == size;
17918 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17919 attribute is the const value T. */
17921 static bool
17922 tree_add_const_value_attribute (dw_die_ref die, tree t)
17924 tree init;
17925 tree type = TREE_TYPE (t);
17926 rtx rtl;
17928 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17929 return false;
17931 init = t;
17932 gcc_assert (!DECL_P (init));
17934 rtl = rtl_for_decl_init (init, type);
17935 if (rtl)
17936 return add_const_value_attribute (die, rtl);
17937 /* If the host and target are sane, try harder. */
17938 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17939 && initializer_constant_valid_p (init, type))
17941 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17942 if (size > 0 && (int) size == size)
17944 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17946 if (native_encode_initializer (init, array, size))
17948 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17949 return true;
17951 ggc_free (array);
17954 return false;
17957 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17958 attribute is the const value of T, where T is an integral constant
17959 variable with static storage duration
17960 (so it can't be a PARM_DECL or a RESULT_DECL). */
17962 static bool
17963 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17966 if (!decl
17967 || (TREE_CODE (decl) != VAR_DECL
17968 && TREE_CODE (decl) != CONST_DECL)
17969 || (TREE_CODE (decl) == VAR_DECL
17970 && !TREE_STATIC (decl)))
17971 return false;
17973 if (TREE_READONLY (decl)
17974 && ! TREE_THIS_VOLATILE (decl)
17975 && DECL_INITIAL (decl))
17976 /* OK */;
17977 else
17978 return false;
17980 /* Don't add DW_AT_const_value if abstract origin already has one. */
17981 if (get_AT (var_die, DW_AT_const_value))
17982 return false;
17984 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17987 /* Convert the CFI instructions for the current function into a
17988 location list. This is used for DW_AT_frame_base when we targeting
17989 a dwarf2 consumer that does not support the dwarf3
17990 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17991 expressions. */
17993 static dw_loc_list_ref
17994 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17996 int ix;
17997 dw_fde_ref fde;
17998 dw_loc_list_ref list, *list_tail;
17999 dw_cfi_ref cfi;
18000 dw_cfa_location last_cfa, next_cfa;
18001 const char *start_label, *last_label, *section;
18002 dw_cfa_location remember;
18004 fde = cfun->fde;
18005 gcc_assert (fde != NULL);
18007 section = secname_for_decl (current_function_decl);
18008 list_tail = &list;
18009 list = NULL;
18011 memset (&next_cfa, 0, sizeof (next_cfa));
18012 next_cfa.reg = INVALID_REGNUM;
18013 remember = next_cfa;
18015 start_label = fde->dw_fde_begin;
18017 /* ??? Bald assumption that the CIE opcode list does not contain
18018 advance opcodes. */
18019 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
18020 lookup_cfa_1 (cfi, &next_cfa, &remember);
18022 last_cfa = next_cfa;
18023 last_label = start_label;
18025 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18027 /* If the first partition contained no CFI adjustments, the
18028 CIE opcodes apply to the whole first partition. */
18029 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18030 fde->dw_fde_begin, fde->dw_fde_end, section);
18031 list_tail =&(*list_tail)->dw_loc_next;
18032 start_label = last_label = fde->dw_fde_second_begin;
18035 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
18037 switch (cfi->dw_cfi_opc)
18039 case DW_CFA_set_loc:
18040 case DW_CFA_advance_loc1:
18041 case DW_CFA_advance_loc2:
18042 case DW_CFA_advance_loc4:
18043 if (!cfa_equal_p (&last_cfa, &next_cfa))
18045 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18046 start_label, last_label, section);
18048 list_tail = &(*list_tail)->dw_loc_next;
18049 last_cfa = next_cfa;
18050 start_label = last_label;
18052 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18053 break;
18055 case DW_CFA_advance_loc:
18056 /* The encoding is complex enough that we should never emit this. */
18057 gcc_unreachable ();
18059 default:
18060 lookup_cfa_1 (cfi, &next_cfa, &remember);
18061 break;
18063 if (ix + 1 == fde->dw_fde_switch_cfi_index)
18065 if (!cfa_equal_p (&last_cfa, &next_cfa))
18067 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18068 start_label, last_label, section);
18070 list_tail = &(*list_tail)->dw_loc_next;
18071 last_cfa = next_cfa;
18072 start_label = last_label;
18074 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18075 start_label, fde->dw_fde_end, section);
18076 list_tail = &(*list_tail)->dw_loc_next;
18077 start_label = last_label = fde->dw_fde_second_begin;
18081 if (!cfa_equal_p (&last_cfa, &next_cfa))
18083 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18084 start_label, last_label, section);
18085 list_tail = &(*list_tail)->dw_loc_next;
18086 start_label = last_label;
18089 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18090 start_label,
18091 fde->dw_fde_second_begin
18092 ? fde->dw_fde_second_end : fde->dw_fde_end,
18093 section);
18095 if (list && list->dw_loc_next)
18096 gen_llsym (list);
18098 return list;
18101 /* Compute a displacement from the "steady-state frame pointer" to the
18102 frame base (often the same as the CFA), and store it in
18103 frame_pointer_fb_offset. OFFSET is added to the displacement
18104 before the latter is negated. */
18106 static void
18107 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18109 rtx reg, elim;
18111 #ifdef FRAME_POINTER_CFA_OFFSET
18112 reg = frame_pointer_rtx;
18113 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18114 #else
18115 reg = arg_pointer_rtx;
18116 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18117 #endif
18119 elim = (ira_use_lra_p
18120 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18121 : eliminate_regs (reg, VOIDmode, NULL_RTX));
18122 if (GET_CODE (elim) == PLUS)
18124 offset += INTVAL (XEXP (elim, 1));
18125 elim = XEXP (elim, 0);
18128 frame_pointer_fb_offset = -offset;
18130 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18131 in which to eliminate. This is because it's stack pointer isn't
18132 directly accessible as a register within the ISA. To work around
18133 this, assume that while we cannot provide a proper value for
18134 frame_pointer_fb_offset, we won't need one either. */
18135 frame_pointer_fb_offset_valid
18136 = ((SUPPORTS_STACK_ALIGNMENT
18137 && (elim == hard_frame_pointer_rtx
18138 || elim == stack_pointer_rtx))
18139 || elim == (frame_pointer_needed
18140 ? hard_frame_pointer_rtx
18141 : stack_pointer_rtx));
18144 /* Generate a DW_AT_name attribute given some string value to be included as
18145 the value of the attribute. */
18147 static void
18148 add_name_attribute (dw_die_ref die, const char *name_string)
18150 if (name_string != NULL && *name_string != 0)
18152 if (demangle_name_func)
18153 name_string = (*demangle_name_func) (name_string);
18155 add_AT_string (die, DW_AT_name, name_string);
18159 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18160 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18161 of TYPE accordingly.
18163 ??? This is a temporary measure until after we're able to generate
18164 regular DWARF for the complex Ada type system. */
18166 static void
18167 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18168 dw_die_ref context_die)
18170 tree dtype;
18171 dw_die_ref dtype_die;
18173 if (!lang_hooks.types.descriptive_type)
18174 return;
18176 dtype = lang_hooks.types.descriptive_type (type);
18177 if (!dtype)
18178 return;
18180 dtype_die = lookup_type_die (dtype);
18181 if (!dtype_die)
18183 gen_type_die (dtype, context_die);
18184 dtype_die = lookup_type_die (dtype);
18185 gcc_assert (dtype_die);
18188 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18191 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
18193 static const char *
18194 comp_dir_string (void)
18196 const char *wd;
18197 char *wd1;
18198 static const char *cached_wd = NULL;
18200 if (cached_wd != NULL)
18201 return cached_wd;
18203 wd = get_src_pwd ();
18204 if (wd == NULL)
18205 return NULL;
18207 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18209 int wdlen;
18211 wdlen = strlen (wd);
18212 wd1 = ggc_vec_alloc<char> (wdlen + 2);
18213 strcpy (wd1, wd);
18214 wd1 [wdlen] = DIR_SEPARATOR;
18215 wd1 [wdlen + 1] = 0;
18216 wd = wd1;
18219 cached_wd = remap_debug_filename (wd);
18220 return cached_wd;
18223 /* Generate a DW_AT_comp_dir attribute for DIE. */
18225 static void
18226 add_comp_dir_attribute (dw_die_ref die)
18228 const char * wd = comp_dir_string ();
18229 if (wd != NULL)
18230 add_AT_string (die, DW_AT_comp_dir, wd);
18233 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18234 pointer computation, ...), output a representation for that bound according
18235 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
18236 loc_list_from_tree for the meaning of CONTEXT. */
18238 static void
18239 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18240 int forms, const struct loc_descr_context *context)
18242 dw_die_ref context_die, decl_die;
18243 dw_loc_list_ref list;
18245 bool strip_conversions = true;
18247 while (strip_conversions)
18248 switch (TREE_CODE (value))
18250 case ERROR_MARK:
18251 case SAVE_EXPR:
18252 return;
18254 CASE_CONVERT:
18255 case VIEW_CONVERT_EXPR:
18256 value = TREE_OPERAND (value, 0);
18257 break;
18259 default:
18260 strip_conversions = false;
18261 break;
18264 /* If possible and permitted, output the attribute as a constant. */
18265 if ((forms & dw_scalar_form_constant) != 0
18266 && TREE_CODE (value) == INTEGER_CST)
18268 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18270 /* If HOST_WIDE_INT is big enough then represent the bound as
18271 a constant value. We need to choose a form based on
18272 whether the type is signed or unsigned. We cannot just
18273 call add_AT_unsigned if the value itself is positive
18274 (add_AT_unsigned might add the unsigned value encoded as
18275 DW_FORM_data[1248]). Some DWARF consumers will lookup the
18276 bounds type and then sign extend any unsigned values found
18277 for signed types. This is needed only for
18278 DW_AT_{lower,upper}_bound, since for most other attributes,
18279 consumers will treat DW_FORM_data[1248] as unsigned values,
18280 regardless of the underlying type. */
18281 if (prec <= HOST_BITS_PER_WIDE_INT
18282 || tree_fits_uhwi_p (value))
18284 if (TYPE_UNSIGNED (TREE_TYPE (value)))
18285 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18286 else
18287 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18289 else
18290 /* Otherwise represent the bound as an unsigned value with
18291 the precision of its type. The precision and signedness
18292 of the type will be necessary to re-interpret it
18293 unambiguously. */
18294 add_AT_wide (die, attr, value);
18295 return;
18298 /* Otherwise, if it's possible and permitted too, output a reference to
18299 another DIE. */
18300 if ((forms & dw_scalar_form_reference) != 0)
18302 tree decl = NULL_TREE;
18304 /* Some type attributes reference an outer type. For instance, the upper
18305 bound of an array may reference an embedding record (this happens in
18306 Ada). */
18307 if (TREE_CODE (value) == COMPONENT_REF
18308 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18309 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18310 decl = TREE_OPERAND (value, 1);
18312 else if (TREE_CODE (value) == VAR_DECL
18313 || TREE_CODE (value) == PARM_DECL
18314 || TREE_CODE (value) == RESULT_DECL)
18315 decl = value;
18317 if (decl != NULL_TREE)
18319 dw_die_ref decl_die = lookup_decl_die (decl);
18321 /* ??? Can this happen, or should the variable have been bound
18322 first? Probably it can, since I imagine that we try to create
18323 the types of parameters in the order in which they exist in
18324 the list, and won't have created a forward reference to a
18325 later parameter. */
18326 if (decl_die != NULL)
18328 add_AT_die_ref (die, attr, decl_die);
18329 return;
18334 /* Last chance: try to create a stack operation procedure to evaluate the
18335 value. Do nothing if even that is not possible or permitted. */
18336 if ((forms & dw_scalar_form_exprloc) == 0)
18337 return;
18339 list = loc_list_from_tree (value, 2, context);
18340 if (list == NULL || single_element_loc_list_p (list))
18342 /* If this attribute is not a reference nor constant, it is
18343 a DWARF expression rather than location description. For that
18344 loc_list_from_tree (value, 0, &context) is needed. */
18345 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18346 if (list2 && single_element_loc_list_p (list2))
18348 add_AT_loc (die, attr, list2->expr);
18349 return;
18353 /* If that failed to give a single element location list, fall back to
18354 outputting this as a reference... still if permitted. */
18355 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18356 return;
18358 if (current_function_decl == 0)
18359 context_die = comp_unit_die ();
18360 else
18361 context_die = lookup_decl_die (current_function_decl);
18363 decl_die = new_die (DW_TAG_variable, context_die, value);
18364 add_AT_flag (decl_die, DW_AT_artificial, 1);
18365 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18366 context_die);
18367 add_AT_location_description (decl_die, DW_AT_location, list);
18368 add_AT_die_ref (die, attr, decl_die);
18371 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18372 default. */
18374 static int
18375 lower_bound_default (void)
18377 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18379 case DW_LANG_C:
18380 case DW_LANG_C89:
18381 case DW_LANG_C99:
18382 case DW_LANG_C11:
18383 case DW_LANG_C_plus_plus:
18384 case DW_LANG_C_plus_plus_11:
18385 case DW_LANG_C_plus_plus_14:
18386 case DW_LANG_ObjC:
18387 case DW_LANG_ObjC_plus_plus:
18388 case DW_LANG_Java:
18389 return 0;
18390 case DW_LANG_Fortran77:
18391 case DW_LANG_Fortran90:
18392 case DW_LANG_Fortran95:
18393 case DW_LANG_Fortran03:
18394 case DW_LANG_Fortran08:
18395 return 1;
18396 case DW_LANG_UPC:
18397 case DW_LANG_D:
18398 case DW_LANG_Python:
18399 return dwarf_version >= 4 ? 0 : -1;
18400 case DW_LANG_Ada95:
18401 case DW_LANG_Ada83:
18402 case DW_LANG_Cobol74:
18403 case DW_LANG_Cobol85:
18404 case DW_LANG_Pascal83:
18405 case DW_LANG_Modula2:
18406 case DW_LANG_PLI:
18407 return dwarf_version >= 4 ? 1 : -1;
18408 default:
18409 return -1;
18413 /* Given a tree node describing an array bound (either lower or upper) output
18414 a representation for that bound. */
18416 static void
18417 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18418 tree bound, const struct loc_descr_context *context)
18420 int dflt;
18422 while (1)
18423 switch (TREE_CODE (bound))
18425 /* Strip all conversions. */
18426 CASE_CONVERT:
18427 case VIEW_CONVERT_EXPR:
18428 bound = TREE_OPERAND (bound, 0);
18429 break;
18431 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
18432 are even omitted when they are the default. */
18433 case INTEGER_CST:
18434 /* If the value for this bound is the default one, we can even omit the
18435 attribute. */
18436 if (bound_attr == DW_AT_lower_bound
18437 && tree_fits_shwi_p (bound)
18438 && (dflt = lower_bound_default ()) != -1
18439 && tree_to_shwi (bound) == dflt)
18440 return;
18442 /* FALLTHRU */
18444 default:
18445 /* Because of the complex interaction there can be with other GNAT
18446 encodings, GDB isn't ready yet to handle proper DWARF description
18447 for self-referencial subrange bounds: let GNAT encodings do the
18448 magic in such a case. */
18449 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18450 && contains_placeholder_p (bound))
18451 return;
18453 add_scalar_info (subrange_die, bound_attr, bound,
18454 dw_scalar_form_constant
18455 | dw_scalar_form_exprloc
18456 | dw_scalar_form_reference,
18457 context);
18458 return;
18462 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18463 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18464 Note that the block of subscript information for an array type also
18465 includes information about the element type of the given array type.
18467 This function reuses previously set type and bound information if
18468 available. */
18470 static void
18471 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18473 unsigned dimension_number;
18474 tree lower, upper;
18475 dw_die_ref child = type_die->die_child;
18477 for (dimension_number = 0;
18478 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18479 type = TREE_TYPE (type), dimension_number++)
18481 tree domain = TYPE_DOMAIN (type);
18483 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18484 break;
18486 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18487 and (in GNU C only) variable bounds. Handle all three forms
18488 here. */
18490 /* Find and reuse a previously generated DW_TAG_subrange_type if
18491 available.
18493 For multi-dimensional arrays, as we iterate through the
18494 various dimensions in the enclosing for loop above, we also
18495 iterate through the DIE children and pick at each
18496 DW_TAG_subrange_type previously generated (if available).
18497 Each child DW_TAG_subrange_type DIE describes the range of
18498 the current dimension. At this point we should have as many
18499 DW_TAG_subrange_type's as we have dimensions in the
18500 array. */
18501 dw_die_ref subrange_die = NULL;
18502 if (child)
18503 while (1)
18505 child = child->die_sib;
18506 if (child->die_tag == DW_TAG_subrange_type)
18507 subrange_die = child;
18508 if (child == type_die->die_child)
18510 /* If we wrapped around, stop looking next time. */
18511 child = NULL;
18512 break;
18514 if (child->die_tag == DW_TAG_subrange_type)
18515 break;
18517 if (!subrange_die)
18518 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18520 if (domain)
18522 /* We have an array type with specified bounds. */
18523 lower = TYPE_MIN_VALUE (domain);
18524 upper = TYPE_MAX_VALUE (domain);
18526 /* Define the index type. */
18527 if (TREE_TYPE (domain)
18528 && !get_AT (subrange_die, DW_AT_type))
18530 /* ??? This is probably an Ada unnamed subrange type. Ignore the
18531 TREE_TYPE field. We can't emit debug info for this
18532 because it is an unnamed integral type. */
18533 if (TREE_CODE (domain) == INTEGER_TYPE
18534 && TYPE_NAME (domain) == NULL_TREE
18535 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18536 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18538 else
18539 add_type_attribute (subrange_die, TREE_TYPE (domain),
18540 TYPE_UNQUALIFIED, false, type_die);
18543 /* ??? If upper is NULL, the array has unspecified length,
18544 but it does have a lower bound. This happens with Fortran
18545 dimension arr(N:*)
18546 Since the debugger is definitely going to need to know N
18547 to produce useful results, go ahead and output the lower
18548 bound solo, and hope the debugger can cope. */
18550 if (!get_AT (subrange_die, DW_AT_lower_bound))
18551 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18552 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18553 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18556 /* Otherwise we have an array type with an unspecified length. The
18557 DWARF-2 spec does not say how to handle this; let's just leave out the
18558 bounds. */
18562 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
18564 static void
18565 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18567 dw_die_ref decl_die;
18568 HOST_WIDE_INT size;
18569 dw_loc_descr_ref size_expr = NULL;
18571 switch (TREE_CODE (tree_node))
18573 case ERROR_MARK:
18574 size = 0;
18575 break;
18576 case ENUMERAL_TYPE:
18577 case RECORD_TYPE:
18578 case UNION_TYPE:
18579 case QUAL_UNION_TYPE:
18580 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18581 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18583 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18584 return;
18586 size_expr = type_byte_size (tree_node, &size);
18587 break;
18588 case FIELD_DECL:
18589 /* For a data member of a struct or union, the DW_AT_byte_size is
18590 generally given as the number of bytes normally allocated for an
18591 object of the *declared* type of the member itself. This is true
18592 even for bit-fields. */
18593 size = int_size_in_bytes (field_type (tree_node));
18594 break;
18595 default:
18596 gcc_unreachable ();
18599 /* Support for dynamically-sized objects was introduced by DWARFv3.
18600 At the moment, GDB does not handle variable byte sizes very well,
18601 though. */
18602 if ((dwarf_version >= 3 || !dwarf_strict)
18603 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18604 && size_expr != NULL)
18605 add_AT_loc (die, DW_AT_byte_size, size_expr);
18607 /* Note that `size' might be -1 when we get to this point. If it is, that
18608 indicates that the byte size of the entity in question is variable and
18609 that we could not generate a DWARF expression that computes it. */
18610 if (size >= 0)
18611 add_AT_unsigned (die, DW_AT_byte_size, size);
18614 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18615 which specifies the distance in bits from the highest order bit of the
18616 "containing object" for the bit-field to the highest order bit of the
18617 bit-field itself.
18619 For any given bit-field, the "containing object" is a hypothetical object
18620 (of some integral or enum type) within which the given bit-field lives. The
18621 type of this hypothetical "containing object" is always the same as the
18622 declared type of the individual bit-field itself. The determination of the
18623 exact location of the "containing object" for a bit-field is rather
18624 complicated. It's handled by the `field_byte_offset' function (above).
18626 CTX is required: see the comment for VLR_CONTEXT.
18628 Note that it is the size (in bytes) of the hypothetical "containing object"
18629 which will be given in the DW_AT_byte_size attribute for this bit-field.
18630 (See `byte_size_attribute' above). */
18632 static inline void
18633 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18635 HOST_WIDE_INT object_offset_in_bytes;
18636 tree original_type = DECL_BIT_FIELD_TYPE (decl);
18637 HOST_WIDE_INT bitpos_int;
18638 HOST_WIDE_INT highest_order_object_bit_offset;
18639 HOST_WIDE_INT highest_order_field_bit_offset;
18640 HOST_WIDE_INT bit_offset;
18642 field_byte_offset (decl, ctx, &object_offset_in_bytes);
18644 /* Must be a field and a bit field. */
18645 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18647 /* We can't yet handle bit-fields whose offsets are variable, so if we
18648 encounter such things, just return without generating any attribute
18649 whatsoever. Likewise for variable or too large size. */
18650 if (! tree_fits_shwi_p (bit_position (decl))
18651 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18652 return;
18654 bitpos_int = int_bit_position (decl);
18656 /* Note that the bit offset is always the distance (in bits) from the
18657 highest-order bit of the "containing object" to the highest-order bit of
18658 the bit-field itself. Since the "high-order end" of any object or field
18659 is different on big-endian and little-endian machines, the computation
18660 below must take account of these differences. */
18661 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18662 highest_order_field_bit_offset = bitpos_int;
18664 if (! BYTES_BIG_ENDIAN)
18666 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18667 highest_order_object_bit_offset +=
18668 simple_type_size_in_bits (original_type);
18671 bit_offset
18672 = (! BYTES_BIG_ENDIAN
18673 ? highest_order_object_bit_offset - highest_order_field_bit_offset
18674 : highest_order_field_bit_offset - highest_order_object_bit_offset);
18676 if (bit_offset < 0)
18677 add_AT_int (die, DW_AT_bit_offset, bit_offset);
18678 else
18679 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18682 /* For a FIELD_DECL node which represents a bit field, output an attribute
18683 which specifies the length in bits of the given field. */
18685 static inline void
18686 add_bit_size_attribute (dw_die_ref die, tree decl)
18688 /* Must be a field and a bit field. */
18689 gcc_assert (TREE_CODE (decl) == FIELD_DECL
18690 && DECL_BIT_FIELD_TYPE (decl));
18692 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18693 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18696 /* If the compiled language is ANSI C, then add a 'prototyped'
18697 attribute, if arg types are given for the parameters of a function. */
18699 static inline void
18700 add_prototyped_attribute (dw_die_ref die, tree func_type)
18702 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18704 case DW_LANG_C:
18705 case DW_LANG_C89:
18706 case DW_LANG_C99:
18707 case DW_LANG_C11:
18708 case DW_LANG_ObjC:
18709 if (prototype_p (func_type))
18710 add_AT_flag (die, DW_AT_prototyped, 1);
18711 break;
18712 default:
18713 break;
18717 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
18718 by looking in the type declaration, the object declaration equate table or
18719 the block mapping. */
18721 static inline dw_die_ref
18722 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18724 dw_die_ref origin_die = NULL;
18726 if (TREE_CODE (origin) != FUNCTION_DECL
18727 && TREE_CODE (origin) != BLOCK)
18729 /* We may have gotten separated from the block for the inlined
18730 function, if we're in an exception handler or some such; make
18731 sure that the abstract function has been written out.
18733 Doing this for nested functions is wrong, however; functions are
18734 distinct units, and our context might not even be inline. */
18735 tree fn = origin;
18737 if (TYPE_P (fn))
18738 fn = TYPE_STUB_DECL (fn);
18740 fn = decl_function_context (fn);
18741 if (fn)
18742 dwarf2out_abstract_function (fn);
18745 if (DECL_P (origin))
18746 origin_die = lookup_decl_die (origin);
18747 else if (TYPE_P (origin))
18748 origin_die = lookup_type_die (origin);
18749 else if (TREE_CODE (origin) == BLOCK)
18750 origin_die = BLOCK_DIE (origin);
18752 /* XXX: Functions that are never lowered don't always have correct block
18753 trees (in the case of java, they simply have no block tree, in some other
18754 languages). For these functions, there is nothing we can really do to
18755 output correct debug info for inlined functions in all cases. Rather
18756 than die, we'll just produce deficient debug info now, in that we will
18757 have variables without a proper abstract origin. In the future, when all
18758 functions are lowered, we should re-add a gcc_assert (origin_die)
18759 here. */
18761 if (origin_die)
18762 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18763 return origin_die;
18766 /* We do not currently support the pure_virtual attribute. */
18768 static inline void
18769 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18771 if (DECL_VINDEX (func_decl))
18773 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18775 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18776 add_AT_loc (die, DW_AT_vtable_elem_location,
18777 new_loc_descr (DW_OP_constu,
18778 tree_to_shwi (DECL_VINDEX (func_decl)),
18779 0));
18781 /* GNU extension: Record what type this method came from originally. */
18782 if (debug_info_level > DINFO_LEVEL_TERSE
18783 && DECL_CONTEXT (func_decl))
18784 add_AT_die_ref (die, DW_AT_containing_type,
18785 lookup_type_die (DECL_CONTEXT (func_decl)));
18789 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18790 given decl. This used to be a vendor extension until after DWARF 4
18791 standardized it. */
18793 static void
18794 add_linkage_attr (dw_die_ref die, tree decl)
18796 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18798 /* Mimic what assemble_name_raw does with a leading '*'. */
18799 if (name[0] == '*')
18800 name = &name[1];
18802 if (dwarf_version >= 4)
18803 add_AT_string (die, DW_AT_linkage_name, name);
18804 else
18805 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18808 /* Add source coordinate attributes for the given decl. */
18810 static void
18811 add_src_coords_attributes (dw_die_ref die, tree decl)
18813 expanded_location s;
18815 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18816 return;
18817 s = expand_location (DECL_SOURCE_LOCATION (decl));
18818 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18819 add_AT_unsigned (die, DW_AT_decl_line, s.line);
18822 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
18824 static void
18825 add_linkage_name_raw (dw_die_ref die, tree decl)
18827 /* Defer until we have an assembler name set. */
18828 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18830 limbo_die_node *asm_name;
18832 asm_name = ggc_cleared_alloc<limbo_die_node> ();
18833 asm_name->die = die;
18834 asm_name->created_for = decl;
18835 asm_name->next = deferred_asm_name;
18836 deferred_asm_name = asm_name;
18838 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18839 add_linkage_attr (die, decl);
18842 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
18844 static void
18845 add_linkage_name (dw_die_ref die, tree decl)
18847 if (debug_info_level > DINFO_LEVEL_NONE
18848 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18849 && TREE_PUBLIC (decl)
18850 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18851 && die->die_tag != DW_TAG_member)
18852 add_linkage_name_raw (die, decl);
18855 /* Add a DW_AT_name attribute and source coordinate attribute for the
18856 given decl, but only if it actually has a name. */
18858 static void
18859 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18861 tree decl_name;
18863 decl_name = DECL_NAME (decl);
18864 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18866 const char *name = dwarf2_name (decl, 0);
18867 if (name)
18868 add_name_attribute (die, name);
18869 if (! DECL_ARTIFICIAL (decl))
18870 add_src_coords_attributes (die, decl);
18872 add_linkage_name (die, decl);
18875 #ifdef VMS_DEBUGGING_INFO
18876 /* Get the function's name, as described by its RTL. This may be different
18877 from the DECL_NAME name used in the source file. */
18878 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18880 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18881 XEXP (DECL_RTL (decl), 0), false);
18882 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18884 #endif /* VMS_DEBUGGING_INFO */
18887 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
18889 static void
18890 add_discr_value (dw_die_ref die, dw_discr_value *value)
18892 dw_attr_node attr;
18894 attr.dw_attr = DW_AT_discr_value;
18895 attr.dw_attr_val.val_class = dw_val_class_discr_value;
18896 attr.dw_attr_val.val_entry = NULL;
18897 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18898 if (value->pos)
18899 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18900 else
18901 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18902 add_dwarf_attr (die, &attr);
18905 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
18907 static void
18908 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18910 dw_attr_node attr;
18912 attr.dw_attr = DW_AT_discr_list;
18913 attr.dw_attr_val.val_class = dw_val_class_discr_list;
18914 attr.dw_attr_val.val_entry = NULL;
18915 attr.dw_attr_val.v.val_discr_list = discr_list;
18916 add_dwarf_attr (die, &attr);
18919 static inline dw_discr_list_ref
18920 AT_discr_list (dw_attr_node *attr)
18922 return attr->dw_attr_val.v.val_discr_list;
18925 #ifdef VMS_DEBUGGING_INFO
18926 /* Output the debug main pointer die for VMS */
18928 void
18929 dwarf2out_vms_debug_main_pointer (void)
18931 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18932 dw_die_ref die;
18934 /* Allocate the VMS debug main subprogram die. */
18935 die = ggc_cleared_alloc<die_node> ();
18936 die->die_tag = DW_TAG_subprogram;
18937 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18938 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18939 current_function_funcdef_no);
18940 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18942 /* Make it the first child of comp_unit_die (). */
18943 die->die_parent = comp_unit_die ();
18944 if (comp_unit_die ()->die_child)
18946 die->die_sib = comp_unit_die ()->die_child->die_sib;
18947 comp_unit_die ()->die_child->die_sib = die;
18949 else
18951 die->die_sib = die;
18952 comp_unit_die ()->die_child = die;
18955 #endif /* VMS_DEBUGGING_INFO */
18957 /* Push a new declaration scope. */
18959 static void
18960 push_decl_scope (tree scope)
18962 vec_safe_push (decl_scope_table, scope);
18965 /* Pop a declaration scope. */
18967 static inline void
18968 pop_decl_scope (void)
18970 decl_scope_table->pop ();
18973 /* walk_tree helper function for uses_local_type, below. */
18975 static tree
18976 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18978 if (!TYPE_P (*tp))
18979 *walk_subtrees = 0;
18980 else
18982 tree name = TYPE_NAME (*tp);
18983 if (name && DECL_P (name) && decl_function_context (name))
18984 return *tp;
18986 return NULL_TREE;
18989 /* If TYPE involves a function-local type (including a local typedef to a
18990 non-local type), returns that type; otherwise returns NULL_TREE. */
18992 static tree
18993 uses_local_type (tree type)
18995 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
18996 return used;
18999 /* Return the DIE for the scope that immediately contains this type.
19000 Non-named types that do not involve a function-local type get global
19001 scope. Named types nested in namespaces or other types get their
19002 containing scope. All other types (i.e. function-local named types) get
19003 the current active scope. */
19005 static dw_die_ref
19006 scope_die_for (tree t, dw_die_ref context_die)
19008 dw_die_ref scope_die = NULL;
19009 tree containing_scope;
19011 /* Non-types always go in the current scope. */
19012 gcc_assert (TYPE_P (t));
19014 /* Use the scope of the typedef, rather than the scope of the type
19015 it refers to. */
19016 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
19017 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
19018 else
19019 containing_scope = TYPE_CONTEXT (t);
19021 /* Use the containing namespace if there is one. */
19022 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
19024 if (context_die == lookup_decl_die (containing_scope))
19025 /* OK */;
19026 else if (debug_info_level > DINFO_LEVEL_TERSE)
19027 context_die = get_context_die (containing_scope);
19028 else
19029 containing_scope = NULL_TREE;
19032 /* Ignore function type "scopes" from the C frontend. They mean that
19033 a tagged type is local to a parmlist of a function declarator, but
19034 that isn't useful to DWARF. */
19035 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19036 containing_scope = NULL_TREE;
19038 if (SCOPE_FILE_SCOPE_P (containing_scope))
19040 /* If T uses a local type keep it local as well, to avoid references
19041 to function-local DIEs from outside the function. */
19042 if (current_function_decl && uses_local_type (t))
19043 scope_die = context_die;
19044 else
19045 scope_die = comp_unit_die ();
19047 else if (TYPE_P (containing_scope))
19049 /* For types, we can just look up the appropriate DIE. */
19050 if (debug_info_level > DINFO_LEVEL_TERSE)
19051 scope_die = get_context_die (containing_scope);
19052 else
19054 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19055 if (scope_die == NULL)
19056 scope_die = comp_unit_die ();
19059 else
19060 scope_die = context_die;
19062 return scope_die;
19065 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
19067 static inline int
19068 local_scope_p (dw_die_ref context_die)
19070 for (; context_die; context_die = context_die->die_parent)
19071 if (context_die->die_tag == DW_TAG_inlined_subroutine
19072 || context_die->die_tag == DW_TAG_subprogram)
19073 return 1;
19075 return 0;
19078 /* Returns nonzero if CONTEXT_DIE is a class. */
19080 static inline int
19081 class_scope_p (dw_die_ref context_die)
19083 return (context_die
19084 && (context_die->die_tag == DW_TAG_structure_type
19085 || context_die->die_tag == DW_TAG_class_type
19086 || context_die->die_tag == DW_TAG_interface_type
19087 || context_die->die_tag == DW_TAG_union_type));
19090 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19091 whether or not to treat a DIE in this context as a declaration. */
19093 static inline int
19094 class_or_namespace_scope_p (dw_die_ref context_die)
19096 return (class_scope_p (context_die)
19097 || (context_die && context_die->die_tag == DW_TAG_namespace));
19100 /* Many forms of DIEs require a "type description" attribute. This
19101 routine locates the proper "type descriptor" die for the type given
19102 by 'type' plus any additional qualifiers given by 'cv_quals', and
19103 adds a DW_AT_type attribute below the given die. */
19105 static void
19106 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19107 bool reverse, dw_die_ref context_die)
19109 enum tree_code code = TREE_CODE (type);
19110 dw_die_ref type_die = NULL;
19112 /* ??? If this type is an unnamed subrange type of an integral, floating-point
19113 or fixed-point type, use the inner type. This is because we have no
19114 support for unnamed types in base_type_die. This can happen if this is
19115 an Ada subrange type. Correct solution is emit a subrange type die. */
19116 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19117 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19118 type = TREE_TYPE (type), code = TREE_CODE (type);
19120 if (code == ERROR_MARK
19121 /* Handle a special case. For functions whose return type is void, we
19122 generate *no* type attribute. (Note that no object may have type
19123 `void', so this only applies to function return types). */
19124 || code == VOID_TYPE)
19125 return;
19127 type_die = modified_type_die (type,
19128 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19129 reverse,
19130 context_die);
19132 if (type_die != NULL)
19133 add_AT_die_ref (object_die, DW_AT_type, type_die);
19136 /* Given an object die, add the calling convention attribute for the
19137 function call type. */
19138 static void
19139 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19141 enum dwarf_calling_convention value = DW_CC_normal;
19143 value = ((enum dwarf_calling_convention)
19144 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19146 if (is_fortran ()
19147 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19149 /* DWARF 2 doesn't provide a way to identify a program's source-level
19150 entry point. DW_AT_calling_convention attributes are only meant
19151 to describe functions' calling conventions. However, lacking a
19152 better way to signal the Fortran main program, we used this for
19153 a long time, following existing custom. Now, DWARF 4 has
19154 DW_AT_main_subprogram, which we add below, but some tools still
19155 rely on the old way, which we thus keep. */
19156 value = DW_CC_program;
19158 if (dwarf_version >= 4 || !dwarf_strict)
19159 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19162 /* Only add the attribute if the backend requests it, and
19163 is not DW_CC_normal. */
19164 if (value && (value != DW_CC_normal))
19165 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19168 /* Given a tree pointer to a struct, class, union, or enum type node, return
19169 a pointer to the (string) tag name for the given type, or zero if the type
19170 was declared without a tag. */
19172 static const char *
19173 type_tag (const_tree type)
19175 const char *name = 0;
19177 if (TYPE_NAME (type) != 0)
19179 tree t = 0;
19181 /* Find the IDENTIFIER_NODE for the type name. */
19182 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19183 && !TYPE_NAMELESS (type))
19184 t = TYPE_NAME (type);
19186 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19187 a TYPE_DECL node, regardless of whether or not a `typedef' was
19188 involved. */
19189 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19190 && ! DECL_IGNORED_P (TYPE_NAME (type)))
19192 /* We want to be extra verbose. Don't call dwarf_name if
19193 DECL_NAME isn't set. The default hook for decl_printable_name
19194 doesn't like that, and in this context it's correct to return
19195 0, instead of "<anonymous>" or the like. */
19196 if (DECL_NAME (TYPE_NAME (type))
19197 && !DECL_NAMELESS (TYPE_NAME (type)))
19198 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19201 /* Now get the name as a string, or invent one. */
19202 if (!name && t != 0)
19203 name = IDENTIFIER_POINTER (t);
19206 return (name == 0 || *name == '\0') ? 0 : name;
19209 /* Return the type associated with a data member, make a special check
19210 for bit field types. */
19212 static inline tree
19213 member_declared_type (const_tree member)
19215 return (DECL_BIT_FIELD_TYPE (member)
19216 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19219 /* Get the decl's label, as described by its RTL. This may be different
19220 from the DECL_NAME name used in the source file. */
19222 #if 0
19223 static const char *
19224 decl_start_label (tree decl)
19226 rtx x;
19227 const char *fnname;
19229 x = DECL_RTL (decl);
19230 gcc_assert (MEM_P (x));
19232 x = XEXP (x, 0);
19233 gcc_assert (GET_CODE (x) == SYMBOL_REF);
19235 fnname = XSTR (x, 0);
19236 return fnname;
19238 #endif
19240 /* For variable-length arrays that have been previously generated, but
19241 may be incomplete due to missing subscript info, fill the subscript
19242 info. Return TRUE if this is one of those cases. */
19243 static bool
19244 fill_variable_array_bounds (tree type)
19246 if (TREE_ASM_WRITTEN (type)
19247 && TREE_CODE (type) == ARRAY_TYPE
19248 && variably_modified_type_p (type, NULL))
19250 dw_die_ref array_die = lookup_type_die (type);
19251 if (!array_die)
19252 return false;
19253 add_subscript_info (array_die, type, !is_ada ());
19254 return true;
19256 return false;
19259 /* These routines generate the internal representation of the DIE's for
19260 the compilation unit. Debugging information is collected by walking
19261 the declaration trees passed in from dwarf2out_decl(). */
19263 static void
19264 gen_array_type_die (tree type, dw_die_ref context_die)
19266 dw_die_ref array_die;
19268 /* GNU compilers represent multidimensional array types as sequences of one
19269 dimensional array types whose element types are themselves array types.
19270 We sometimes squish that down to a single array_type DIE with multiple
19271 subscripts in the Dwarf debugging info. The draft Dwarf specification
19272 say that we are allowed to do this kind of compression in C, because
19273 there is no difference between an array of arrays and a multidimensional
19274 array. We don't do this for Ada to remain as close as possible to the
19275 actual representation, which is especially important against the language
19276 flexibilty wrt arrays of variable size. */
19278 bool collapse_nested_arrays = !is_ada ();
19280 if (fill_variable_array_bounds (type))
19281 return;
19283 dw_die_ref scope_die = scope_die_for (type, context_die);
19284 tree element_type;
19286 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19287 DW_TAG_string_type doesn't have DW_AT_type attribute). */
19288 if (TYPE_STRING_FLAG (type)
19289 && TREE_CODE (type) == ARRAY_TYPE
19290 && is_fortran ()
19291 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19293 HOST_WIDE_INT size;
19295 array_die = new_die (DW_TAG_string_type, scope_die, type);
19296 add_name_attribute (array_die, type_tag (type));
19297 equate_type_number_to_die (type, array_die);
19298 size = int_size_in_bytes (type);
19299 if (size >= 0)
19300 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19301 else if (TYPE_DOMAIN (type) != NULL_TREE
19302 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
19304 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19305 tree rszdecl = szdecl;
19306 HOST_WIDE_INT rsize = 0;
19308 size = int_size_in_bytes (TREE_TYPE (szdecl));
19309 if (!DECL_P (szdecl))
19311 if (TREE_CODE (szdecl) == INDIRECT_REF
19312 && DECL_P (TREE_OPERAND (szdecl, 0)))
19314 rszdecl = TREE_OPERAND (szdecl, 0);
19315 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
19316 if (rsize <= 0)
19317 size = 0;
19319 else
19320 size = 0;
19322 if (size > 0)
19324 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19325 if (loc == NULL
19326 && early_dwarf
19327 && current_function_decl
19328 && DECL_CONTEXT (rszdecl) == current_function_decl)
19330 dw_die_ref ref = lookup_decl_die (rszdecl);
19331 dw_loc_descr_ref l = NULL;
19332 if (ref)
19334 l = new_loc_descr (DW_OP_call4, 0, 0);
19335 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19336 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
19337 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
19339 else if (TREE_CODE (rszdecl) == PARM_DECL
19340 && string_types)
19342 l = new_loc_descr (DW_OP_call4, 0, 0);
19343 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19344 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
19345 string_types->safe_push (array_die);
19347 if (l && rszdecl != szdecl)
19349 if (rsize == DWARF2_ADDR_SIZE)
19350 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
19351 0, 0));
19352 else
19353 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
19354 rsize, 0));
19356 if (l)
19357 loc = new_loc_list (l, NULL, NULL, NULL);
19359 if (loc)
19361 add_AT_location_description (array_die, DW_AT_string_length,
19362 loc);
19363 if (size != DWARF2_ADDR_SIZE)
19364 add_AT_unsigned (array_die, dwarf_version >= 5
19365 ? DW_AT_string_length_byte_size
19366 : DW_AT_byte_size, size);
19370 return;
19373 array_die = new_die (DW_TAG_array_type, scope_die, type);
19374 add_name_attribute (array_die, type_tag (type));
19375 equate_type_number_to_die (type, array_die);
19377 if (TREE_CODE (type) == VECTOR_TYPE)
19378 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19380 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
19381 if (is_fortran ()
19382 && TREE_CODE (type) == ARRAY_TYPE
19383 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19384 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19385 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19387 #if 0
19388 /* We default the array ordering. SDB will probably do
19389 the right things even if DW_AT_ordering is not present. It's not even
19390 an issue until we start to get into multidimensional arrays anyway. If
19391 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19392 then we'll have to put the DW_AT_ordering attribute back in. (But if
19393 and when we find out that we need to put these in, we will only do so
19394 for multidimensional arrays. */
19395 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19396 #endif
19398 if (TREE_CODE (type) == VECTOR_TYPE)
19400 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
19401 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19402 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19403 add_bound_info (subrange_die, DW_AT_upper_bound,
19404 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19406 else
19407 add_subscript_info (array_die, type, collapse_nested_arrays);
19409 /* Add representation of the type of the elements of this array type and
19410 emit the corresponding DIE if we haven't done it already. */
19411 element_type = TREE_TYPE (type);
19412 if (collapse_nested_arrays)
19413 while (TREE_CODE (element_type) == ARRAY_TYPE)
19415 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19416 break;
19417 element_type = TREE_TYPE (element_type);
19420 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19421 TREE_CODE (type) == ARRAY_TYPE
19422 && TYPE_REVERSE_STORAGE_ORDER (type),
19423 context_die);
19425 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19426 if (TYPE_ARTIFICIAL (type))
19427 add_AT_flag (array_die, DW_AT_artificial, 1);
19429 if (get_AT (array_die, DW_AT_name))
19430 add_pubtype (type, array_die);
19433 /* After all arguments are created, adjust any DW_TAG_string_type
19434 DIEs DW_AT_string_length attributes. */
19436 static void
19437 adjust_string_types (void)
19439 dw_die_ref array_die;
19440 unsigned int i;
19441 FOR_EACH_VEC_ELT (*string_types, i, array_die)
19443 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
19444 if (a == NULL)
19445 continue;
19446 dw_loc_descr_ref loc = AT_loc (a);
19447 gcc_assert (loc->dw_loc_opc == DW_OP_call4
19448 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
19449 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
19450 if (ref)
19452 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19453 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
19454 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
19456 else
19458 remove_AT (array_die, DW_AT_string_length);
19459 remove_AT (array_die, dwarf_version >= 5
19460 ? DW_AT_string_length_byte_size
19461 : DW_AT_byte_size);
19466 /* This routine generates DIE for array with hidden descriptor, details
19467 are filled into *info by a langhook. */
19469 static void
19470 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19471 dw_die_ref context_die)
19473 const dw_die_ref scope_die = scope_die_for (type, context_die);
19474 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19475 const struct loc_descr_context context = { type, info->base_decl, NULL };
19476 int dim;
19478 add_name_attribute (array_die, type_tag (type));
19479 equate_type_number_to_die (type, array_die);
19481 if (info->ndimensions > 1)
19482 switch (info->ordering)
19484 case array_descr_ordering_row_major:
19485 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19486 break;
19487 case array_descr_ordering_column_major:
19488 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19489 break;
19490 default:
19491 break;
19494 if (dwarf_version >= 3 || !dwarf_strict)
19496 if (info->data_location)
19497 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19498 dw_scalar_form_exprloc, &context);
19499 if (info->associated)
19500 add_scalar_info (array_die, DW_AT_associated, info->associated,
19501 dw_scalar_form_constant
19502 | dw_scalar_form_exprloc
19503 | dw_scalar_form_reference, &context);
19504 if (info->allocated)
19505 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19506 dw_scalar_form_constant
19507 | dw_scalar_form_exprloc
19508 | dw_scalar_form_reference, &context);
19509 if (info->stride)
19511 const enum dwarf_attribute attr
19512 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19513 const int forms
19514 = (info->stride_in_bits)
19515 ? dw_scalar_form_constant
19516 : (dw_scalar_form_constant
19517 | dw_scalar_form_exprloc
19518 | dw_scalar_form_reference);
19520 add_scalar_info (array_die, attr, info->stride, forms, &context);
19524 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19526 for (dim = 0; dim < info->ndimensions; dim++)
19528 dw_die_ref subrange_die
19529 = new_die (DW_TAG_subrange_type, array_die, NULL);
19531 if (info->dimen[dim].bounds_type)
19532 add_type_attribute (subrange_die,
19533 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19534 false, context_die);
19535 if (info->dimen[dim].lower_bound)
19536 add_bound_info (subrange_die, DW_AT_lower_bound,
19537 info->dimen[dim].lower_bound, &context);
19538 if (info->dimen[dim].upper_bound)
19539 add_bound_info (subrange_die, DW_AT_upper_bound,
19540 info->dimen[dim].upper_bound, &context);
19541 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19542 add_scalar_info (subrange_die, DW_AT_byte_stride,
19543 info->dimen[dim].stride,
19544 dw_scalar_form_constant
19545 | dw_scalar_form_exprloc
19546 | dw_scalar_form_reference,
19547 &context);
19550 gen_type_die (info->element_type, context_die);
19551 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19552 TREE_CODE (type) == ARRAY_TYPE
19553 && TYPE_REVERSE_STORAGE_ORDER (type),
19554 context_die);
19556 if (get_AT (array_die, DW_AT_name))
19557 add_pubtype (type, array_die);
19560 #if 0
19561 static void
19562 gen_entry_point_die (tree decl, dw_die_ref context_die)
19564 tree origin = decl_ultimate_origin (decl);
19565 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19567 if (origin != NULL)
19568 add_abstract_origin_attribute (decl_die, origin);
19569 else
19571 add_name_and_src_coords_attributes (decl_die, decl);
19572 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19573 TYPE_UNQUALIFIED, false, context_die);
19576 if (DECL_ABSTRACT_P (decl))
19577 equate_decl_number_to_die (decl, decl_die);
19578 else
19579 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19581 #endif
19583 /* Walk through the list of incomplete types again, trying once more to
19584 emit full debugging info for them. */
19586 static void
19587 retry_incomplete_types (void)
19589 set_early_dwarf s;
19590 int i;
19592 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19593 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19594 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19595 vec_safe_truncate (incomplete_types, 0);
19598 /* Determine what tag to use for a record type. */
19600 static enum dwarf_tag
19601 record_type_tag (tree type)
19603 if (! lang_hooks.types.classify_record)
19604 return DW_TAG_structure_type;
19606 switch (lang_hooks.types.classify_record (type))
19608 case RECORD_IS_STRUCT:
19609 return DW_TAG_structure_type;
19611 case RECORD_IS_CLASS:
19612 return DW_TAG_class_type;
19614 case RECORD_IS_INTERFACE:
19615 if (dwarf_version >= 3 || !dwarf_strict)
19616 return DW_TAG_interface_type;
19617 return DW_TAG_structure_type;
19619 default:
19620 gcc_unreachable ();
19624 /* Generate a DIE to represent an enumeration type. Note that these DIEs
19625 include all of the information about the enumeration values also. Each
19626 enumerated type name/value is listed as a child of the enumerated type
19627 DIE. */
19629 static dw_die_ref
19630 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19632 dw_die_ref type_die = lookup_type_die (type);
19634 if (type_die == NULL)
19636 type_die = new_die (DW_TAG_enumeration_type,
19637 scope_die_for (type, context_die), type);
19638 equate_type_number_to_die (type, type_die);
19639 add_name_attribute (type_die, type_tag (type));
19640 if (dwarf_version >= 4 || !dwarf_strict)
19642 if (ENUM_IS_SCOPED (type))
19643 add_AT_flag (type_die, DW_AT_enum_class, 1);
19644 if (ENUM_IS_OPAQUE (type))
19645 add_AT_flag (type_die, DW_AT_declaration, 1);
19648 else if (! TYPE_SIZE (type))
19649 return type_die;
19650 else
19651 remove_AT (type_die, DW_AT_declaration);
19653 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
19654 given enum type is incomplete, do not generate the DW_AT_byte_size
19655 attribute or the DW_AT_element_list attribute. */
19656 if (TYPE_SIZE (type))
19658 tree link;
19660 TREE_ASM_WRITTEN (type) = 1;
19661 add_byte_size_attribute (type_die, type);
19662 if (dwarf_version >= 3 || !dwarf_strict)
19664 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19665 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19666 context_die);
19668 if (TYPE_STUB_DECL (type) != NULL_TREE)
19670 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19671 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19674 /* If the first reference to this type was as the return type of an
19675 inline function, then it may not have a parent. Fix this now. */
19676 if (type_die->die_parent == NULL)
19677 add_child_die (scope_die_for (type, context_die), type_die);
19679 for (link = TYPE_VALUES (type);
19680 link != NULL; link = TREE_CHAIN (link))
19682 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19683 tree value = TREE_VALUE (link);
19685 add_name_attribute (enum_die,
19686 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19688 if (TREE_CODE (value) == CONST_DECL)
19689 value = DECL_INITIAL (value);
19691 if (simple_type_size_in_bits (TREE_TYPE (value))
19692 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19694 /* For constant forms created by add_AT_unsigned DWARF
19695 consumers (GDB, elfutils, etc.) always zero extend
19696 the value. Only when the actual value is negative
19697 do we need to use add_AT_int to generate a constant
19698 form that can represent negative values. */
19699 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19700 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19701 add_AT_unsigned (enum_die, DW_AT_const_value,
19702 (unsigned HOST_WIDE_INT) val);
19703 else
19704 add_AT_int (enum_die, DW_AT_const_value, val);
19706 else
19707 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
19708 that here. TODO: This should be re-worked to use correct
19709 signed/unsigned double tags for all cases. */
19710 add_AT_wide (enum_die, DW_AT_const_value, value);
19713 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19714 if (TYPE_ARTIFICIAL (type))
19715 add_AT_flag (type_die, DW_AT_artificial, 1);
19717 else
19718 add_AT_flag (type_die, DW_AT_declaration, 1);
19720 add_pubtype (type, type_die);
19722 return type_die;
19725 /* Generate a DIE to represent either a real live formal parameter decl or to
19726 represent just the type of some formal parameter position in some function
19727 type.
19729 Note that this routine is a bit unusual because its argument may be a
19730 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19731 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19732 node. If it's the former then this function is being called to output a
19733 DIE to represent a formal parameter object (or some inlining thereof). If
19734 it's the latter, then this function is only being called to output a
19735 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19736 argument type of some subprogram type.
19737 If EMIT_NAME_P is true, name and source coordinate attributes
19738 are emitted. */
19740 static dw_die_ref
19741 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19742 dw_die_ref context_die)
19744 tree node_or_origin = node ? node : origin;
19745 tree ultimate_origin;
19746 dw_die_ref parm_die = NULL;
19748 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19750 parm_die = lookup_decl_die (node);
19752 /* If the contexts differ, we may not be talking about the same
19753 thing. */
19754 if (parm_die && parm_die->die_parent != context_die)
19756 if (!DECL_ABSTRACT_P (node))
19758 /* This can happen when creating an inlined instance, in
19759 which case we need to create a new DIE that will get
19760 annotated with DW_AT_abstract_origin. */
19761 parm_die = NULL;
19763 else
19765 /* FIXME: Reuse DIE even with a differing context.
19767 This can happen when calling
19768 dwarf2out_abstract_function to build debug info for
19769 the abstract instance of a function for which we have
19770 already generated a DIE in
19771 dwarf2out_early_global_decl.
19773 Once we remove dwarf2out_abstract_function, we should
19774 have a call to gcc_unreachable here. */
19778 if (parm_die && parm_die->die_parent == NULL)
19780 /* Check that parm_die already has the right attributes that
19781 we would have added below. If any attributes are
19782 missing, fall through to add them. */
19783 if (! DECL_ABSTRACT_P (node_or_origin)
19784 && !get_AT (parm_die, DW_AT_location)
19785 && !get_AT (parm_die, DW_AT_const_value))
19786 /* We are missing location info, and are about to add it. */
19788 else
19790 add_child_die (context_die, parm_die);
19791 return parm_die;
19796 /* If we have a previously generated DIE, use it, unless this is an
19797 concrete instance (origin != NULL), in which case we need a new
19798 DIE with a corresponding DW_AT_abstract_origin. */
19799 bool reusing_die;
19800 if (parm_die && origin == NULL)
19801 reusing_die = true;
19802 else
19804 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19805 reusing_die = false;
19808 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19810 case tcc_declaration:
19811 ultimate_origin = decl_ultimate_origin (node_or_origin);
19812 if (node || ultimate_origin)
19813 origin = ultimate_origin;
19815 if (reusing_die)
19816 goto add_location;
19818 if (origin != NULL)
19819 add_abstract_origin_attribute (parm_die, origin);
19820 else if (emit_name_p)
19821 add_name_and_src_coords_attributes (parm_die, node);
19822 if (origin == NULL
19823 || (! DECL_ABSTRACT_P (node_or_origin)
19824 && variably_modified_type_p (TREE_TYPE (node_or_origin),
19825 decl_function_context
19826 (node_or_origin))))
19828 tree type = TREE_TYPE (node_or_origin);
19829 if (decl_by_reference_p (node_or_origin))
19830 add_type_attribute (parm_die, TREE_TYPE (type),
19831 TYPE_UNQUALIFIED,
19832 false, context_die);
19833 else
19834 add_type_attribute (parm_die, type,
19835 decl_quals (node_or_origin),
19836 false, context_die);
19838 if (origin == NULL && DECL_ARTIFICIAL (node))
19839 add_AT_flag (parm_die, DW_AT_artificial, 1);
19840 add_location:
19841 if (node && node != origin)
19842 equate_decl_number_to_die (node, parm_die);
19843 if (! DECL_ABSTRACT_P (node_or_origin))
19844 add_location_or_const_value_attribute (parm_die, node_or_origin,
19845 node == NULL);
19847 break;
19849 case tcc_type:
19850 /* We were called with some kind of a ..._TYPE node. */
19851 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19852 context_die);
19853 break;
19855 default:
19856 gcc_unreachable ();
19859 return parm_die;
19862 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19863 children DW_TAG_formal_parameter DIEs representing the arguments of the
19864 parameter pack.
19866 PARM_PACK must be a function parameter pack.
19867 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19868 must point to the subsequent arguments of the function PACK_ARG belongs to.
19869 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19870 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19871 following the last one for which a DIE was generated. */
19873 static dw_die_ref
19874 gen_formal_parameter_pack_die (tree parm_pack,
19875 tree pack_arg,
19876 dw_die_ref subr_die,
19877 tree *next_arg)
19879 tree arg;
19880 dw_die_ref parm_pack_die;
19882 gcc_assert (parm_pack
19883 && lang_hooks.function_parameter_pack_p (parm_pack)
19884 && subr_die);
19886 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19887 add_src_coords_attributes (parm_pack_die, parm_pack);
19889 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19891 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19892 parm_pack))
19893 break;
19894 gen_formal_parameter_die (arg, NULL,
19895 false /* Don't emit name attribute. */,
19896 parm_pack_die);
19898 if (next_arg)
19899 *next_arg = arg;
19900 return parm_pack_die;
19903 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19904 at the end of an (ANSI prototyped) formal parameters list. */
19906 static void
19907 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19909 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19912 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19913 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19914 parameters as specified in some function type specification (except for
19915 those which appear as part of a function *definition*). */
19917 static void
19918 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19920 tree link;
19921 tree formal_type = NULL;
19922 tree first_parm_type;
19923 tree arg;
19925 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19927 arg = DECL_ARGUMENTS (function_or_method_type);
19928 function_or_method_type = TREE_TYPE (function_or_method_type);
19930 else
19931 arg = NULL_TREE;
19933 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19935 /* Make our first pass over the list of formal parameter types and output a
19936 DW_TAG_formal_parameter DIE for each one. */
19937 for (link = first_parm_type; link; )
19939 dw_die_ref parm_die;
19941 formal_type = TREE_VALUE (link);
19942 if (formal_type == void_type_node)
19943 break;
19945 /* Output a (nameless) DIE to represent the formal parameter itself. */
19946 if (!POINTER_BOUNDS_TYPE_P (formal_type))
19948 parm_die = gen_formal_parameter_die (formal_type, NULL,
19949 true /* Emit name attribute. */,
19950 context_die);
19951 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19952 && link == first_parm_type)
19954 add_AT_flag (parm_die, DW_AT_artificial, 1);
19955 if (dwarf_version >= 3 || !dwarf_strict)
19956 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19958 else if (arg && DECL_ARTIFICIAL (arg))
19959 add_AT_flag (parm_die, DW_AT_artificial, 1);
19962 link = TREE_CHAIN (link);
19963 if (arg)
19964 arg = DECL_CHAIN (arg);
19967 /* If this function type has an ellipsis, add a
19968 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
19969 if (formal_type != void_type_node)
19970 gen_unspecified_parameters_die (function_or_method_type, context_die);
19972 /* Make our second (and final) pass over the list of formal parameter types
19973 and output DIEs to represent those types (as necessary). */
19974 for (link = TYPE_ARG_TYPES (function_or_method_type);
19975 link && TREE_VALUE (link);
19976 link = TREE_CHAIN (link))
19977 gen_type_die (TREE_VALUE (link), context_die);
19980 /* We want to generate the DIE for TYPE so that we can generate the
19981 die for MEMBER, which has been defined; we will need to refer back
19982 to the member declaration nested within TYPE. If we're trying to
19983 generate minimal debug info for TYPE, processing TYPE won't do the
19984 trick; we need to attach the member declaration by hand. */
19986 static void
19987 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19989 gen_type_die (type, context_die);
19991 /* If we're trying to avoid duplicate debug info, we may not have
19992 emitted the member decl for this function. Emit it now. */
19993 if (TYPE_STUB_DECL (type)
19994 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19995 && ! lookup_decl_die (member))
19997 dw_die_ref type_die;
19998 gcc_assert (!decl_ultimate_origin (member));
20000 push_decl_scope (type);
20001 type_die = lookup_type_die_strip_naming_typedef (type);
20002 if (TREE_CODE (member) == FUNCTION_DECL)
20003 gen_subprogram_die (member, type_die);
20004 else if (TREE_CODE (member) == FIELD_DECL)
20006 /* Ignore the nameless fields that are used to skip bits but handle
20007 C++ anonymous unions and structs. */
20008 if (DECL_NAME (member) != NULL_TREE
20009 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
20010 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
20012 struct vlr_context vlr_ctx = {
20013 DECL_CONTEXT (member), /* struct_type */
20014 NULL_TREE /* variant_part_offset */
20016 gen_type_die (member_declared_type (member), type_die);
20017 gen_field_die (member, &vlr_ctx, type_die);
20020 else
20021 gen_variable_die (member, NULL_TREE, type_die);
20023 pop_decl_scope ();
20027 /* Forward declare these functions, because they are mutually recursive
20028 with their set_block_* pairing functions. */
20029 static void set_decl_origin_self (tree);
20030 static void set_decl_abstract_flags (tree, vec<tree> &);
20032 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
20033 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
20034 that it points to the node itself, thus indicating that the node is its
20035 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
20036 the given node is NULL, recursively descend the decl/block tree which
20037 it is the root of, and for each other ..._DECL or BLOCK node contained
20038 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
20039 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
20040 values to point to themselves. */
20042 static void
20043 set_block_origin_self (tree stmt)
20045 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
20047 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
20050 tree local_decl;
20052 for (local_decl = BLOCK_VARS (stmt);
20053 local_decl != NULL_TREE;
20054 local_decl = DECL_CHAIN (local_decl))
20055 /* Do not recurse on nested functions since the inlining status
20056 of parent and child can be different as per the DWARF spec. */
20057 if (TREE_CODE (local_decl) != FUNCTION_DECL
20058 && !DECL_EXTERNAL (local_decl))
20059 set_decl_origin_self (local_decl);
20063 tree subblock;
20065 for (subblock = BLOCK_SUBBLOCKS (stmt);
20066 subblock != NULL_TREE;
20067 subblock = BLOCK_CHAIN (subblock))
20068 set_block_origin_self (subblock); /* Recurse. */
20073 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
20074 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
20075 node to so that it points to the node itself, thus indicating that the
20076 node represents its own (abstract) origin. Additionally, if the
20077 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
20078 the decl/block tree of which the given node is the root of, and for
20079 each other ..._DECL or BLOCK node contained therein whose
20080 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
20081 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
20082 point to themselves. */
20084 static void
20085 set_decl_origin_self (tree decl)
20087 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
20089 DECL_ABSTRACT_ORIGIN (decl) = decl;
20090 if (TREE_CODE (decl) == FUNCTION_DECL)
20092 tree arg;
20094 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20095 DECL_ABSTRACT_ORIGIN (arg) = arg;
20096 if (DECL_INITIAL (decl) != NULL_TREE
20097 && DECL_INITIAL (decl) != error_mark_node)
20098 set_block_origin_self (DECL_INITIAL (decl));
20103 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
20104 and if it wasn't 1 before, push it to abstract_vec vector.
20105 For all local decls and all local sub-blocks (recursively) do it
20106 too. */
20108 static void
20109 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
20111 tree local_decl;
20112 tree subblock;
20113 unsigned int i;
20115 if (!BLOCK_ABSTRACT (stmt))
20117 abstract_vec.safe_push (stmt);
20118 BLOCK_ABSTRACT (stmt) = 1;
20121 for (local_decl = BLOCK_VARS (stmt);
20122 local_decl != NULL_TREE;
20123 local_decl = DECL_CHAIN (local_decl))
20124 if (! DECL_EXTERNAL (local_decl))
20125 set_decl_abstract_flags (local_decl, abstract_vec);
20127 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20129 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
20130 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
20131 || TREE_CODE (local_decl) == PARM_DECL)
20132 set_decl_abstract_flags (local_decl, abstract_vec);
20135 for (subblock = BLOCK_SUBBLOCKS (stmt);
20136 subblock != NULL_TREE;
20137 subblock = BLOCK_CHAIN (subblock))
20138 set_block_abstract_flags (subblock, abstract_vec);
20141 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
20142 to 1 and if it wasn't 1 before, push to abstract_vec vector.
20143 In the case where the decl is a FUNCTION_DECL also set the abstract
20144 flags for all of the parameters, local vars, local
20145 blocks and sub-blocks (recursively). */
20147 static void
20148 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
20150 if (!DECL_ABSTRACT_P (decl))
20152 abstract_vec.safe_push (decl);
20153 DECL_ABSTRACT_P (decl) = 1;
20156 if (TREE_CODE (decl) == FUNCTION_DECL)
20158 tree arg;
20160 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20161 if (!DECL_ABSTRACT_P (arg))
20163 abstract_vec.safe_push (arg);
20164 DECL_ABSTRACT_P (arg) = 1;
20166 if (DECL_INITIAL (decl) != NULL_TREE
20167 && DECL_INITIAL (decl) != error_mark_node)
20168 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
20172 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20173 may later generate inlined and/or out-of-line instances of.
20175 FIXME: In the early-dwarf world, this function, and most of the
20176 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
20177 the abstract instance. All we would need to do is annotate
20178 the early DIE with the appropriate DW_AT_inline in late
20179 dwarf (perhaps in gen_inlined_subroutine_die).
20181 However, we can't do this yet, because LTO streaming of DIEs
20182 has not been implemented yet. */
20184 static void
20185 dwarf2out_abstract_function (tree decl)
20187 dw_die_ref old_die;
20188 tree save_fn;
20189 tree context;
20190 hash_table<decl_loc_hasher> *old_decl_loc_table;
20191 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20192 int old_call_site_count, old_tail_call_site_count;
20193 struct call_arg_loc_node *old_call_arg_locations;
20195 /* Make sure we have the actual abstract inline, not a clone. */
20196 decl = DECL_ORIGIN (decl);
20198 old_die = lookup_decl_die (decl);
20199 if (old_die && get_AT (old_die, DW_AT_inline))
20200 /* We've already generated the abstract instance. */
20201 return;
20203 /* We can be called while recursively when seeing block defining inlined subroutine
20204 DIE. Be sure to not clobber the outer location table nor use it or we would
20205 get locations in abstract instantces. */
20206 old_decl_loc_table = decl_loc_table;
20207 decl_loc_table = NULL;
20208 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20209 cached_dw_loc_list_table = NULL;
20210 old_call_arg_locations = call_arg_locations;
20211 call_arg_locations = NULL;
20212 old_call_site_count = call_site_count;
20213 call_site_count = -1;
20214 old_tail_call_site_count = tail_call_site_count;
20215 tail_call_site_count = -1;
20217 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20218 we don't get confused by DECL_ABSTRACT_P. */
20219 if (debug_info_level > DINFO_LEVEL_TERSE)
20221 context = decl_class_context (decl);
20222 if (context)
20223 gen_type_die_for_member
20224 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20227 /* Pretend we've just finished compiling this function. */
20228 save_fn = current_function_decl;
20229 current_function_decl = decl;
20231 auto_vec<tree, 64> abstract_vec;
20232 set_decl_abstract_flags (decl, abstract_vec);
20233 dwarf2out_decl (decl);
20234 unsigned int i;
20235 tree t;
20236 FOR_EACH_VEC_ELT (abstract_vec, i, t)
20237 if (TREE_CODE (t) == BLOCK)
20238 BLOCK_ABSTRACT (t) = 0;
20239 else
20240 DECL_ABSTRACT_P (t) = 0;
20242 current_function_decl = save_fn;
20243 decl_loc_table = old_decl_loc_table;
20244 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20245 call_arg_locations = old_call_arg_locations;
20246 call_site_count = old_call_site_count;
20247 tail_call_site_count = old_tail_call_site_count;
20250 /* Helper function of premark_used_types() which gets called through
20251 htab_traverse.
20253 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20254 marked as unused by prune_unused_types. */
20256 bool
20257 premark_used_types_helper (tree const &type, void *)
20259 dw_die_ref die;
20261 die = lookup_type_die (type);
20262 if (die != NULL)
20263 die->die_perennial_p = 1;
20264 return true;
20267 /* Helper function of premark_types_used_by_global_vars which gets called
20268 through htab_traverse.
20270 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20271 marked as unused by prune_unused_types. The DIE of the type is marked
20272 only if the global variable using the type will actually be emitted. */
20275 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20276 void *)
20278 struct types_used_by_vars_entry *entry;
20279 dw_die_ref die;
20281 entry = (struct types_used_by_vars_entry *) *slot;
20282 gcc_assert (entry->type != NULL
20283 && entry->var_decl != NULL);
20284 die = lookup_type_die (entry->type);
20285 if (die)
20287 /* Ask cgraph if the global variable really is to be emitted.
20288 If yes, then we'll keep the DIE of ENTRY->TYPE. */
20289 varpool_node *node = varpool_node::get (entry->var_decl);
20290 if (node && node->definition)
20292 die->die_perennial_p = 1;
20293 /* Keep the parent DIEs as well. */
20294 while ((die = die->die_parent) && die->die_perennial_p == 0)
20295 die->die_perennial_p = 1;
20298 return 1;
20301 /* Mark all members of used_types_hash as perennial. */
20303 static void
20304 premark_used_types (struct function *fun)
20306 if (fun && fun->used_types_hash)
20307 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20310 /* Mark all members of types_used_by_vars_entry as perennial. */
20312 static void
20313 premark_types_used_by_global_vars (void)
20315 if (types_used_by_vars_hash)
20316 types_used_by_vars_hash
20317 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20320 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20321 for CA_LOC call arg loc node. */
20323 static dw_die_ref
20324 gen_call_site_die (tree decl, dw_die_ref subr_die,
20325 struct call_arg_loc_node *ca_loc)
20327 dw_die_ref stmt_die = NULL, die;
20328 tree block = ca_loc->block;
20330 while (block
20331 && block != DECL_INITIAL (decl)
20332 && TREE_CODE (block) == BLOCK)
20334 stmt_die = BLOCK_DIE (block);
20335 if (stmt_die)
20336 break;
20337 block = BLOCK_SUPERCONTEXT (block);
20339 if (stmt_die == NULL)
20340 stmt_die = subr_die;
20341 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20342 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20343 if (ca_loc->tail_call_p)
20344 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20345 if (ca_loc->symbol_ref)
20347 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20348 if (tdie)
20349 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20350 else
20351 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20353 return die;
20356 /* Generate a DIE to represent a declared function (either file-scope or
20357 block-local). */
20359 static void
20360 gen_subprogram_die (tree decl, dw_die_ref context_die)
20362 tree origin = decl_ultimate_origin (decl);
20363 dw_die_ref subr_die;
20364 dw_die_ref old_die = lookup_decl_die (decl);
20366 /* This function gets called multiple times for different stages of
20367 the debug process. For example, for func() in this code:
20369 namespace S
20371 void func() { ... }
20374 ...we get called 4 times. Twice in early debug and twice in
20375 late debug:
20377 Early debug
20378 -----------
20380 1. Once while generating func() within the namespace. This is
20381 the declaration. The declaration bit below is set, as the
20382 context is the namespace.
20384 A new DIE will be generated with DW_AT_declaration set.
20386 2. Once for func() itself. This is the specification. The
20387 declaration bit below is clear as the context is the CU.
20389 We will use the cached DIE from (1) to create a new DIE with
20390 DW_AT_specification pointing to the declaration in (1).
20392 Late debug via rest_of_handle_final()
20393 -------------------------------------
20395 3. Once generating func() within the namespace. This is also the
20396 declaration, as in (1), but this time we will early exit below
20397 as we have a cached DIE and a declaration needs no additional
20398 annotations (no locations), as the source declaration line
20399 info is enough.
20401 4. Once for func() itself. As in (2), this is the specification,
20402 but this time we will re-use the cached DIE, and just annotate
20403 it with the location information that should now be available.
20405 For something without namespaces, but with abstract instances, we
20406 are also called a multiple times:
20408 class Base
20410 public:
20411 Base (); // constructor declaration (1)
20414 Base::Base () { } // constructor specification (2)
20416 Early debug
20417 -----------
20419 1. Once for the Base() constructor by virtue of it being a
20420 member of the Base class. This is done via
20421 rest_of_type_compilation.
20423 This is a declaration, so a new DIE will be created with
20424 DW_AT_declaration.
20426 2. Once for the Base() constructor definition, but this time
20427 while generating the abstract instance of the base
20428 constructor (__base_ctor) which is being generated via early
20429 debug of reachable functions.
20431 Even though we have a cached version of the declaration (1),
20432 we will create a DW_AT_specification of the declaration DIE
20433 in (1).
20435 3. Once for the __base_ctor itself, but this time, we generate
20436 an DW_AT_abstract_origin version of the DW_AT_specification in
20437 (2).
20439 Late debug via rest_of_handle_final
20440 -----------------------------------
20442 4. One final time for the __base_ctor (which will have a cached
20443 DIE with DW_AT_abstract_origin created in (3). This time,
20444 we will just annotate the location information now
20445 available.
20447 int declaration = (current_function_decl != decl
20448 || class_or_namespace_scope_p (context_die));
20450 premark_used_types (DECL_STRUCT_FUNCTION (decl));
20452 /* Now that the C++ front end lazily declares artificial member fns, we
20453 might need to retrofit the declaration into its class. */
20454 if (!declaration && !origin && !old_die
20455 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20456 && !class_or_namespace_scope_p (context_die)
20457 && debug_info_level > DINFO_LEVEL_TERSE)
20458 old_die = force_decl_die (decl);
20460 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
20461 if (origin != NULL)
20463 gcc_assert (!declaration || local_scope_p (context_die));
20465 /* Fixup die_parent for the abstract instance of a nested
20466 inline function. */
20467 if (old_die && old_die->die_parent == NULL)
20468 add_child_die (context_die, old_die);
20470 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20472 /* If we have a DW_AT_abstract_origin we have a working
20473 cached version. */
20474 subr_die = old_die;
20476 else
20478 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20479 add_abstract_origin_attribute (subr_die, origin);
20480 /* This is where the actual code for a cloned function is.
20481 Let's emit linkage name attribute for it. This helps
20482 debuggers to e.g, set breakpoints into
20483 constructors/destructors when the user asks "break
20484 K::K". */
20485 add_linkage_name (subr_die, decl);
20488 /* A cached copy, possibly from early dwarf generation. Reuse as
20489 much as possible. */
20490 else if (old_die)
20492 /* A declaration that has been previously dumped needs no
20493 additional information. */
20494 if (declaration)
20495 return;
20497 if (!get_AT_flag (old_die, DW_AT_declaration)
20498 /* We can have a normal definition following an inline one in the
20499 case of redefinition of GNU C extern inlines.
20500 It seems reasonable to use AT_specification in this case. */
20501 && !get_AT (old_die, DW_AT_inline))
20503 /* Detect and ignore this case, where we are trying to output
20504 something we have already output. */
20505 if (get_AT (old_die, DW_AT_low_pc)
20506 || get_AT (old_die, DW_AT_ranges))
20507 return;
20509 /* If we have no location information, this must be a
20510 partially generated DIE from early dwarf generation.
20511 Fall through and generate it. */
20514 /* If the definition comes from the same place as the declaration,
20515 maybe use the old DIE. We always want the DIE for this function
20516 that has the *_pc attributes to be under comp_unit_die so the
20517 debugger can find it. We also need to do this for abstract
20518 instances of inlines, since the spec requires the out-of-line copy
20519 to have the same parent. For local class methods, this doesn't
20520 apply; we just use the old DIE. */
20521 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20522 struct dwarf_file_data * file_index = lookup_filename (s.file);
20523 if ((is_cu_die (old_die->die_parent)
20524 /* This condition fixes the inconsistency/ICE with the
20525 following Fortran test (or some derivative thereof) while
20526 building libgfortran:
20528 module some_m
20529 contains
20530 logical function funky (FLAG)
20531 funky = .true.
20532 end function
20533 end module
20535 || (old_die->die_parent
20536 && old_die->die_parent->die_tag == DW_TAG_module)
20537 || context_die == NULL)
20538 && (DECL_ARTIFICIAL (decl)
20539 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20540 && (get_AT_unsigned (old_die, DW_AT_decl_line)
20541 == (unsigned) s.line))))
20543 subr_die = old_die;
20545 /* Clear out the declaration attribute, but leave the
20546 parameters so they can be augmented with location
20547 information later. Unless this was a declaration, in
20548 which case, wipe out the nameless parameters and recreate
20549 them further down. */
20550 if (remove_AT (subr_die, DW_AT_declaration))
20553 remove_AT (subr_die, DW_AT_object_pointer);
20554 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20557 /* Make a specification pointing to the previously built
20558 declaration. */
20559 else
20561 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20562 add_AT_specification (subr_die, old_die);
20563 add_pubname (decl, subr_die);
20564 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20565 add_AT_file (subr_die, DW_AT_decl_file, file_index);
20566 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20567 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20569 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20570 emit the real type on the definition die. */
20571 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20573 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20574 if (die == auto_die || die == decltype_auto_die)
20575 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20576 TYPE_UNQUALIFIED, false, context_die);
20579 /* When we process the method declaration, we haven't seen
20580 the out-of-class defaulted definition yet, so we have to
20581 recheck now. */
20582 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20583 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict)
20584 && !get_AT (subr_die, DW_AT_defaulted))
20585 switch (defaulted)
20587 case 2:
20588 add_AT_unsigned (subr_die, DW_AT_defaulted,
20589 DW_DEFAULTED_out_of_class);
20590 break;
20592 case 1: /* This must have been handled before. */
20593 default:
20594 gcc_unreachable ();
20598 /* Create a fresh DIE for anything else. */
20599 else
20601 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20603 if (TREE_PUBLIC (decl))
20604 add_AT_flag (subr_die, DW_AT_external, 1);
20606 add_name_and_src_coords_attributes (subr_die, decl);
20607 add_pubname (decl, subr_die);
20608 if (debug_info_level > DINFO_LEVEL_TERSE)
20610 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20611 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20612 TYPE_UNQUALIFIED, false, context_die);
20615 add_pure_or_virtual_attribute (subr_die, decl);
20616 if (DECL_ARTIFICIAL (decl))
20617 add_AT_flag (subr_die, DW_AT_artificial, 1);
20619 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20620 add_AT_flag (subr_die, DW_AT_noreturn, 1);
20622 add_accessibility_attribute (subr_die, decl);
20625 /* Unless we have an existing non-declaration DIE, equate the new
20626 DIE. */
20627 if (!old_die || is_declaration_die (old_die))
20628 equate_decl_number_to_die (decl, subr_die);
20630 if (declaration)
20632 if (!old_die || !get_AT (old_die, DW_AT_inline))
20634 add_AT_flag (subr_die, DW_AT_declaration, 1);
20636 /* If this is an explicit function declaration then generate
20637 a DW_AT_explicit attribute. */
20638 if (lang_hooks.decls.function_decl_explicit_p (decl)
20639 && (dwarf_version >= 3 || !dwarf_strict))
20640 add_AT_flag (subr_die, DW_AT_explicit, 1);
20642 /* If this is a C++11 deleted special function member then generate
20643 a DW_AT_deleted attribute. */
20644 if (lang_hooks.decls.function_decl_deleted_p (decl)
20645 && (dwarf_version >= 5 || ! dwarf_strict))
20646 add_AT_flag (subr_die, DW_AT_deleted, 1);
20648 /* If this is a C++11 defaulted special function member then
20649 generate a DW_AT_GNU_defaulted attribute. */
20650 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20651 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict))
20652 switch (defaulted)
20654 case 1:
20655 add_AT_unsigned (subr_die, DW_AT_defaulted,
20656 DW_DEFAULTED_in_class);
20657 break;
20659 /* It is likely that this will never hit, since we
20660 don't have the out-of-class definition yet when we
20661 process the class definition and the method
20662 declaration. We recheck elsewhere, but leave it
20663 here just in case. */
20664 case 2:
20665 add_AT_unsigned (subr_die, DW_AT_defaulted,
20666 DW_DEFAULTED_out_of_class);
20667 break;
20669 default:
20670 gcc_unreachable ();
20674 /* Tag abstract instances with DW_AT_inline. */
20675 else if (DECL_ABSTRACT_P (decl))
20677 if (DECL_DECLARED_INLINE_P (decl))
20679 if (cgraph_function_possibly_inlined_p (decl))
20680 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20681 else
20682 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20684 else
20686 if (cgraph_function_possibly_inlined_p (decl))
20687 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20688 else
20689 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20692 if (DECL_DECLARED_INLINE_P (decl)
20693 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20694 add_AT_flag (subr_die, DW_AT_artificial, 1);
20696 /* For non DECL_EXTERNALs, if range information is available, fill
20697 the DIE with it. */
20698 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20700 HOST_WIDE_INT cfa_fb_offset;
20702 struct function *fun = DECL_STRUCT_FUNCTION (decl);
20704 if (!flag_reorder_blocks_and_partition)
20706 dw_fde_ref fde = fun->fde;
20707 if (fde->dw_fde_begin)
20709 /* We have already generated the labels. */
20710 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20711 fde->dw_fde_end, false);
20713 else
20715 /* Create start/end labels and add the range. */
20716 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20717 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20718 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20719 current_function_funcdef_no);
20720 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20721 current_function_funcdef_no);
20722 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20723 false);
20726 #if VMS_DEBUGGING_INFO
20727 /* HP OpenVMS Industry Standard 64: DWARF Extensions
20728 Section 2.3 Prologue and Epilogue Attributes:
20729 When a breakpoint is set on entry to a function, it is generally
20730 desirable for execution to be suspended, not on the very first
20731 instruction of the function, but rather at a point after the
20732 function's frame has been set up, after any language defined local
20733 declaration processing has been completed, and before execution of
20734 the first statement of the function begins. Debuggers generally
20735 cannot properly determine where this point is. Similarly for a
20736 breakpoint set on exit from a function. The prologue and epilogue
20737 attributes allow a compiler to communicate the location(s) to use. */
20740 if (fde->dw_fde_vms_end_prologue)
20741 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20742 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20744 if (fde->dw_fde_vms_begin_epilogue)
20745 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20746 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20748 #endif
20751 else
20753 /* Generate pubnames entries for the split function code ranges. */
20754 dw_fde_ref fde = fun->fde;
20756 if (fde->dw_fde_second_begin)
20758 if (dwarf_version >= 3 || !dwarf_strict)
20760 /* We should use ranges for non-contiguous code section
20761 addresses. Use the actual code range for the initial
20762 section, since the HOT/COLD labels might precede an
20763 alignment offset. */
20764 bool range_list_added = false;
20765 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20766 fde->dw_fde_end, &range_list_added,
20767 false);
20768 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20769 fde->dw_fde_second_end,
20770 &range_list_added, false);
20771 if (range_list_added)
20772 add_ranges (NULL);
20774 else
20776 /* There is no real support in DW2 for this .. so we make
20777 a work-around. First, emit the pub name for the segment
20778 containing the function label. Then make and emit a
20779 simplified subprogram DIE for the second segment with the
20780 name pre-fixed by __hot/cold_sect_of_. We use the same
20781 linkage name for the second die so that gdb will find both
20782 sections when given "b foo". */
20783 const char *name = NULL;
20784 tree decl_name = DECL_NAME (decl);
20785 dw_die_ref seg_die;
20787 /* Do the 'primary' section. */
20788 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20789 fde->dw_fde_end, false);
20791 /* Build a minimal DIE for the secondary section. */
20792 seg_die = new_die (DW_TAG_subprogram,
20793 subr_die->die_parent, decl);
20795 if (TREE_PUBLIC (decl))
20796 add_AT_flag (seg_die, DW_AT_external, 1);
20798 if (decl_name != NULL
20799 && IDENTIFIER_POINTER (decl_name) != NULL)
20801 name = dwarf2_name (decl, 1);
20802 if (! DECL_ARTIFICIAL (decl))
20803 add_src_coords_attributes (seg_die, decl);
20805 add_linkage_name (seg_die, decl);
20807 gcc_assert (name != NULL);
20808 add_pure_or_virtual_attribute (seg_die, decl);
20809 if (DECL_ARTIFICIAL (decl))
20810 add_AT_flag (seg_die, DW_AT_artificial, 1);
20812 name = concat ("__second_sect_of_", name, NULL);
20813 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20814 fde->dw_fde_second_end, false);
20815 add_name_attribute (seg_die, name);
20816 if (want_pubnames ())
20817 add_pubname_string (name, seg_die);
20820 else
20821 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20822 false);
20825 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20827 /* We define the "frame base" as the function's CFA. This is more
20828 convenient for several reasons: (1) It's stable across the prologue
20829 and epilogue, which makes it better than just a frame pointer,
20830 (2) With dwarf3, there exists a one-byte encoding that allows us
20831 to reference the .debug_frame data by proxy, but failing that,
20832 (3) We can at least reuse the code inspection and interpretation
20833 code that determines the CFA position at various points in the
20834 function. */
20835 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20837 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20838 add_AT_loc (subr_die, DW_AT_frame_base, op);
20840 else
20842 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20843 if (list->dw_loc_next)
20844 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20845 else
20846 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20849 /* Compute a displacement from the "steady-state frame pointer" to
20850 the CFA. The former is what all stack slots and argument slots
20851 will reference in the rtl; the latter is what we've told the
20852 debugger about. We'll need to adjust all frame_base references
20853 by this displacement. */
20854 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20856 if (fun->static_chain_decl)
20858 /* DWARF requires here a location expression that computes the
20859 address of the enclosing subprogram's frame base. The machinery
20860 in tree-nested.c is supposed to store this specific address in the
20861 last field of the FRAME record. */
20862 const tree frame_type
20863 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20864 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20866 tree fb_expr
20867 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20868 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20869 fb_expr, fb_decl, NULL_TREE);
20871 add_AT_location_description (subr_die, DW_AT_static_link,
20872 loc_list_from_tree (fb_expr, 0, NULL));
20876 /* Generate child dies for template paramaters. */
20877 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20878 gen_generic_params_dies (decl);
20880 /* Now output descriptions of the arguments for this function. This gets
20881 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20882 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20883 `...' at the end of the formal parameter list. In order to find out if
20884 there was a trailing ellipsis or not, we must instead look at the type
20885 associated with the FUNCTION_DECL. This will be a node of type
20886 FUNCTION_TYPE. If the chain of type nodes hanging off of this
20887 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20888 an ellipsis at the end. */
20890 /* In the case where we are describing a mere function declaration, all we
20891 need to do here (and all we *can* do here) is to describe the *types* of
20892 its formal parameters. */
20893 if (debug_info_level <= DINFO_LEVEL_TERSE)
20895 else if (declaration)
20896 gen_formal_types_die (decl, subr_die);
20897 else
20899 /* Generate DIEs to represent all known formal parameters. */
20900 tree parm = DECL_ARGUMENTS (decl);
20901 tree generic_decl = early_dwarf
20902 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20903 tree generic_decl_parm = generic_decl
20904 ? DECL_ARGUMENTS (generic_decl)
20905 : NULL;
20906 auto_vec<dw_die_ref> string_types_vec;
20907 if (string_types == NULL)
20908 string_types = &string_types_vec;
20910 /* Now we want to walk the list of parameters of the function and
20911 emit their relevant DIEs.
20913 We consider the case of DECL being an instance of a generic function
20914 as well as it being a normal function.
20916 If DECL is an instance of a generic function we walk the
20917 parameters of the generic function declaration _and_ the parameters of
20918 DECL itself. This is useful because we want to emit specific DIEs for
20919 function parameter packs and those are declared as part of the
20920 generic function declaration. In that particular case,
20921 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20922 That DIE has children DIEs representing the set of arguments
20923 of the pack. Note that the set of pack arguments can be empty.
20924 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20925 children DIE.
20927 Otherwise, we just consider the parameters of DECL. */
20928 while (generic_decl_parm || parm)
20930 if (generic_decl_parm
20931 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20932 gen_formal_parameter_pack_die (generic_decl_parm,
20933 parm, subr_die,
20934 &parm);
20935 else if (parm && !POINTER_BOUNDS_P (parm))
20937 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20939 if (parm == DECL_ARGUMENTS (decl)
20940 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20941 && parm_die
20942 && (dwarf_version >= 3 || !dwarf_strict))
20943 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20945 parm = DECL_CHAIN (parm);
20947 else if (parm)
20948 parm = DECL_CHAIN (parm);
20950 if (generic_decl_parm)
20951 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20954 /* Decide whether we need an unspecified_parameters DIE at the end.
20955 There are 2 more cases to do this for: 1) the ansi ... declaration -
20956 this is detectable when the end of the arg list is not a
20957 void_type_node 2) an unprototyped function declaration (not a
20958 definition). This just means that we have no info about the
20959 parameters at all. */
20960 if (early_dwarf)
20962 if (prototype_p (TREE_TYPE (decl)))
20964 /* This is the prototyped case, check for.... */
20965 if (stdarg_p (TREE_TYPE (decl)))
20966 gen_unspecified_parameters_die (decl, subr_die);
20968 else if (DECL_INITIAL (decl) == NULL_TREE)
20969 gen_unspecified_parameters_die (decl, subr_die);
20972 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
20973 have DIEs. */
20974 if (string_types == &string_types_vec)
20976 adjust_string_types ();
20977 string_types = NULL;
20981 if (subr_die != old_die)
20982 /* Add the calling convention attribute if requested. */
20983 add_calling_convention_attribute (subr_die, decl);
20985 /* Output Dwarf info for all of the stuff within the body of the function
20986 (if it has one - it may be just a declaration).
20988 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20989 a function. This BLOCK actually represents the outermost binding contour
20990 for the function, i.e. the contour in which the function's formal
20991 parameters and labels get declared. Curiously, it appears that the front
20992 end doesn't actually put the PARM_DECL nodes for the current function onto
20993 the BLOCK_VARS list for this outer scope, but are strung off of the
20994 DECL_ARGUMENTS list for the function instead.
20996 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20997 the LABEL_DECL nodes for the function however, and we output DWARF info
20998 for those in decls_for_scope. Just within the `outer_scope' there will be
20999 a BLOCK node representing the function's outermost pair of curly braces,
21000 and any blocks used for the base and member initializers of a C++
21001 constructor function. */
21002 tree outer_scope = DECL_INITIAL (decl);
21003 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
21005 int call_site_note_count = 0;
21006 int tail_call_site_note_count = 0;
21008 /* Emit a DW_TAG_variable DIE for a named return value. */
21009 if (DECL_NAME (DECL_RESULT (decl)))
21010 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
21012 /* The first time through decls_for_scope we will generate the
21013 DIEs for the locals. The second time, we fill in the
21014 location info. */
21015 decls_for_scope (outer_scope, subr_die);
21017 if (call_arg_locations && !dwarf_strict)
21019 struct call_arg_loc_node *ca_loc;
21020 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
21022 dw_die_ref die = NULL;
21023 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
21024 rtx arg, next_arg;
21026 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
21027 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
21028 : NULL_RTX);
21029 arg; arg = next_arg)
21031 dw_loc_descr_ref reg, val;
21032 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
21033 dw_die_ref cdie, tdie = NULL;
21035 next_arg = XEXP (arg, 1);
21036 if (REG_P (XEXP (XEXP (arg, 0), 0))
21037 && next_arg
21038 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
21039 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
21040 && REGNO (XEXP (XEXP (arg, 0), 0))
21041 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
21042 next_arg = XEXP (next_arg, 1);
21043 if (mode == VOIDmode)
21045 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
21046 if (mode == VOIDmode)
21047 mode = GET_MODE (XEXP (arg, 0));
21049 if (mode == VOIDmode || mode == BLKmode)
21050 continue;
21051 /* Get dynamic information about call target only if we
21052 have no static information: we cannot generate both
21053 DW_AT_abstract_origin and DW_AT_GNU_call_site_target
21054 attributes. */
21055 if (ca_loc->symbol_ref == NULL_RTX)
21057 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
21059 tloc = XEXP (XEXP (arg, 0), 1);
21060 continue;
21062 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
21063 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
21065 tlocc = XEXP (XEXP (arg, 0), 1);
21066 continue;
21069 reg = NULL;
21070 if (REG_P (XEXP (XEXP (arg, 0), 0)))
21071 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
21072 VAR_INIT_STATUS_INITIALIZED);
21073 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
21075 rtx mem = XEXP (XEXP (arg, 0), 0);
21076 reg = mem_loc_descriptor (XEXP (mem, 0),
21077 get_address_mode (mem),
21078 GET_MODE (mem),
21079 VAR_INIT_STATUS_INITIALIZED);
21081 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
21082 == DEBUG_PARAMETER_REF)
21084 tree tdecl
21085 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
21086 tdie = lookup_decl_die (tdecl);
21087 if (tdie == NULL)
21088 continue;
21090 else
21091 continue;
21092 if (reg == NULL
21093 && GET_CODE (XEXP (XEXP (arg, 0), 0))
21094 != DEBUG_PARAMETER_REF)
21095 continue;
21096 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
21097 VOIDmode,
21098 VAR_INIT_STATUS_INITIALIZED);
21099 if (val == NULL)
21100 continue;
21101 if (die == NULL)
21102 die = gen_call_site_die (decl, subr_die, ca_loc);
21103 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
21104 NULL_TREE);
21105 if (reg != NULL)
21106 add_AT_loc (cdie, DW_AT_location, reg);
21107 else if (tdie != NULL)
21108 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
21109 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
21110 if (next_arg != XEXP (arg, 1))
21112 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
21113 if (mode == VOIDmode)
21114 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
21115 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
21116 0), 1),
21117 mode, VOIDmode,
21118 VAR_INIT_STATUS_INITIALIZED);
21119 if (val != NULL)
21120 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
21123 if (die == NULL
21124 && (ca_loc->symbol_ref || tloc))
21125 die = gen_call_site_die (decl, subr_die, ca_loc);
21126 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
21128 dw_loc_descr_ref tval = NULL;
21130 if (tloc != NULL_RTX)
21131 tval = mem_loc_descriptor (tloc,
21132 GET_MODE (tloc) == VOIDmode
21133 ? Pmode : GET_MODE (tloc),
21134 VOIDmode,
21135 VAR_INIT_STATUS_INITIALIZED);
21136 if (tval)
21137 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
21138 else if (tlocc != NULL_RTX)
21140 tval = mem_loc_descriptor (tlocc,
21141 GET_MODE (tlocc) == VOIDmode
21142 ? Pmode : GET_MODE (tlocc),
21143 VOIDmode,
21144 VAR_INIT_STATUS_INITIALIZED);
21145 if (tval)
21146 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
21147 tval);
21150 if (die != NULL)
21152 call_site_note_count++;
21153 if (ca_loc->tail_call_p)
21154 tail_call_site_note_count++;
21158 call_arg_locations = NULL;
21159 call_arg_loc_last = NULL;
21160 if (tail_call_site_count >= 0
21161 && tail_call_site_count == tail_call_site_note_count
21162 && !dwarf_strict)
21164 if (call_site_count >= 0
21165 && call_site_count == call_site_note_count)
21166 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
21167 else
21168 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
21170 call_site_count = -1;
21171 tail_call_site_count = -1;
21175 /* Returns a hash value for X (which really is a die_struct). */
21177 hashval_t
21178 block_die_hasher::hash (die_struct *d)
21180 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
21183 /* Return nonzero if decl_id and die_parent of die_struct X is the same
21184 as decl_id and die_parent of die_struct Y. */
21186 bool
21187 block_die_hasher::equal (die_struct *x, die_struct *y)
21189 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
21192 /* Return TRUE if DECL, which may have been previously generated as
21193 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
21194 true if decl (or its origin) is either an extern declaration or a
21195 class/namespace scoped declaration.
21197 The declare_in_namespace support causes us to get two DIEs for one
21198 variable, both of which are declarations. We want to avoid
21199 considering one to be a specification, so we must test for
21200 DECLARATION and DW_AT_declaration. */
21201 static inline bool
21202 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
21204 return (old_die && TREE_STATIC (decl) && !declaration
21205 && get_AT_flag (old_die, DW_AT_declaration) == 1);
21208 /* Return true if DECL is a local static. */
21210 static inline bool
21211 local_function_static (tree decl)
21213 gcc_assert (TREE_CODE (decl) == VAR_DECL);
21214 return TREE_STATIC (decl)
21215 && DECL_CONTEXT (decl)
21216 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
21219 /* Generate a DIE to represent a declared data object.
21220 Either DECL or ORIGIN must be non-null. */
21222 static void
21223 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
21225 HOST_WIDE_INT off = 0;
21226 tree com_decl;
21227 tree decl_or_origin = decl ? decl : origin;
21228 tree ultimate_origin;
21229 dw_die_ref var_die;
21230 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
21231 dw_die_ref origin_die = NULL;
21232 bool declaration = (DECL_EXTERNAL (decl_or_origin)
21233 || class_or_namespace_scope_p (context_die));
21234 bool specialization_p = false;
21236 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21237 if (decl || ultimate_origin)
21238 origin = ultimate_origin;
21239 com_decl = fortran_common (decl_or_origin, &off);
21241 /* Symbol in common gets emitted as a child of the common block, in the form
21242 of a data member. */
21243 if (com_decl)
21245 dw_die_ref com_die;
21246 dw_loc_list_ref loc;
21247 die_node com_die_arg;
21249 var_die = lookup_decl_die (decl_or_origin);
21250 if (var_die)
21252 if (get_AT (var_die, DW_AT_location) == NULL)
21254 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21255 if (loc)
21257 if (off)
21259 /* Optimize the common case. */
21260 if (single_element_loc_list_p (loc)
21261 && loc->expr->dw_loc_opc == DW_OP_addr
21262 && loc->expr->dw_loc_next == NULL
21263 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21264 == SYMBOL_REF)
21266 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21267 loc->expr->dw_loc_oprnd1.v.val_addr
21268 = plus_constant (GET_MODE (x), x , off);
21270 else
21271 loc_list_plus_const (loc, off);
21273 add_AT_location_description (var_die, DW_AT_location, loc);
21274 remove_AT (var_die, DW_AT_declaration);
21277 return;
21280 if (common_block_die_table == NULL)
21281 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21283 com_die_arg.decl_id = DECL_UID (com_decl);
21284 com_die_arg.die_parent = context_die;
21285 com_die = common_block_die_table->find (&com_die_arg);
21286 loc = loc_list_from_tree (com_decl, 2, NULL);
21287 if (com_die == NULL)
21289 const char *cnam
21290 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21291 die_node **slot;
21293 com_die = new_die (DW_TAG_common_block, context_die, decl);
21294 add_name_and_src_coords_attributes (com_die, com_decl);
21295 if (loc)
21297 add_AT_location_description (com_die, DW_AT_location, loc);
21298 /* Avoid sharing the same loc descriptor between
21299 DW_TAG_common_block and DW_TAG_variable. */
21300 loc = loc_list_from_tree (com_decl, 2, NULL);
21302 else if (DECL_EXTERNAL (decl_or_origin))
21303 add_AT_flag (com_die, DW_AT_declaration, 1);
21304 if (want_pubnames ())
21305 add_pubname_string (cnam, com_die); /* ??? needed? */
21306 com_die->decl_id = DECL_UID (com_decl);
21307 slot = common_block_die_table->find_slot (com_die, INSERT);
21308 *slot = com_die;
21310 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21312 add_AT_location_description (com_die, DW_AT_location, loc);
21313 loc = loc_list_from_tree (com_decl, 2, NULL);
21314 remove_AT (com_die, DW_AT_declaration);
21316 var_die = new_die (DW_TAG_variable, com_die, decl);
21317 add_name_and_src_coords_attributes (var_die, decl_or_origin);
21318 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21319 decl_quals (decl_or_origin), false,
21320 context_die);
21321 add_AT_flag (var_die, DW_AT_external, 1);
21322 if (loc)
21324 if (off)
21326 /* Optimize the common case. */
21327 if (single_element_loc_list_p (loc)
21328 && loc->expr->dw_loc_opc == DW_OP_addr
21329 && loc->expr->dw_loc_next == NULL
21330 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21332 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21333 loc->expr->dw_loc_oprnd1.v.val_addr
21334 = plus_constant (GET_MODE (x), x, off);
21336 else
21337 loc_list_plus_const (loc, off);
21339 add_AT_location_description (var_die, DW_AT_location, loc);
21341 else if (DECL_EXTERNAL (decl_or_origin))
21342 add_AT_flag (var_die, DW_AT_declaration, 1);
21343 if (decl)
21344 equate_decl_number_to_die (decl, var_die);
21345 return;
21348 if (old_die)
21350 if (declaration)
21352 /* A declaration that has been previously dumped, needs no
21353 further annotations, since it doesn't need location on
21354 the second pass. */
21355 return;
21357 else if (decl_will_get_specification_p (old_die, decl, declaration)
21358 && !get_AT (old_die, DW_AT_specification))
21360 /* Fall-thru so we can make a new variable die along with a
21361 DW_AT_specification. */
21363 else if (origin && old_die->die_parent != context_die)
21365 /* If we will be creating an inlined instance, we need a
21366 new DIE that will get annotated with
21367 DW_AT_abstract_origin. Clear things so we can get a
21368 new DIE. */
21369 gcc_assert (!DECL_ABSTRACT_P (decl));
21370 old_die = NULL;
21372 else
21374 /* If a DIE was dumped early, it still needs location info.
21375 Skip to where we fill the location bits. */
21376 var_die = old_die;
21377 goto gen_variable_die_location;
21381 /* For static data members, the declaration in the class is supposed
21382 to have DW_TAG_member tag; the specification should still be
21383 DW_TAG_variable referencing the DW_TAG_member DIE. */
21384 if (declaration && class_scope_p (context_die))
21385 var_die = new_die (DW_TAG_member, context_die, decl);
21386 else
21387 var_die = new_die (DW_TAG_variable, context_die, decl);
21389 if (origin != NULL)
21390 origin_die = add_abstract_origin_attribute (var_die, origin);
21392 /* Loop unrolling can create multiple blocks that refer to the same
21393 static variable, so we must test for the DW_AT_declaration flag.
21395 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21396 copy decls and set the DECL_ABSTRACT_P flag on them instead of
21397 sharing them.
21399 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
21400 else if (decl_will_get_specification_p (old_die, decl, declaration))
21402 /* This is a definition of a C++ class level static. */
21403 add_AT_specification (var_die, old_die);
21404 specialization_p = true;
21405 if (DECL_NAME (decl))
21407 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21408 struct dwarf_file_data * file_index = lookup_filename (s.file);
21410 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21411 add_AT_file (var_die, DW_AT_decl_file, file_index);
21413 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21414 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21416 if (old_die->die_tag == DW_TAG_member)
21417 add_linkage_name (var_die, decl);
21420 else
21421 add_name_and_src_coords_attributes (var_die, decl);
21423 if ((origin == NULL && !specialization_p)
21424 || (origin != NULL
21425 && !DECL_ABSTRACT_P (decl_or_origin)
21426 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21427 decl_function_context
21428 (decl_or_origin))))
21430 tree type = TREE_TYPE (decl_or_origin);
21432 if (decl_by_reference_p (decl_or_origin))
21433 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21434 context_die);
21435 else
21436 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21437 context_die);
21440 if (origin == NULL && !specialization_p)
21442 if (TREE_PUBLIC (decl))
21443 add_AT_flag (var_die, DW_AT_external, 1);
21445 if (DECL_ARTIFICIAL (decl))
21446 add_AT_flag (var_die, DW_AT_artificial, 1);
21448 add_accessibility_attribute (var_die, decl);
21451 if (declaration)
21452 add_AT_flag (var_die, DW_AT_declaration, 1);
21454 if (decl && (DECL_ABSTRACT_P (decl)
21455 || !old_die || is_declaration_die (old_die)))
21456 equate_decl_number_to_die (decl, var_die);
21458 gen_variable_die_location:
21459 if (! declaration
21460 && (! DECL_ABSTRACT_P (decl_or_origin)
21461 /* Local static vars are shared between all clones/inlines,
21462 so emit DW_AT_location on the abstract DIE if DECL_RTL is
21463 already set. */
21464 || (TREE_CODE (decl_or_origin) == VAR_DECL
21465 && TREE_STATIC (decl_or_origin)
21466 && DECL_RTL_SET_P (decl_or_origin)))
21467 /* When abstract origin already has DW_AT_location attribute, no need
21468 to add it again. */
21469 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21471 if (early_dwarf)
21472 add_pubname (decl_or_origin, var_die);
21473 else
21474 add_location_or_const_value_attribute (var_die, decl_or_origin,
21475 decl == NULL);
21477 else
21478 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21481 /* Generate a DIE to represent a named constant. */
21483 static void
21484 gen_const_die (tree decl, dw_die_ref context_die)
21486 dw_die_ref const_die;
21487 tree type = TREE_TYPE (decl);
21489 const_die = lookup_decl_die (decl);
21490 if (const_die)
21491 return;
21493 const_die = new_die (DW_TAG_constant, context_die, decl);
21494 equate_decl_number_to_die (decl, const_die);
21495 add_name_and_src_coords_attributes (const_die, decl);
21496 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21497 if (TREE_PUBLIC (decl))
21498 add_AT_flag (const_die, DW_AT_external, 1);
21499 if (DECL_ARTIFICIAL (decl))
21500 add_AT_flag (const_die, DW_AT_artificial, 1);
21501 tree_add_const_value_attribute_for_decl (const_die, decl);
21504 /* Generate a DIE to represent a label identifier. */
21506 static void
21507 gen_label_die (tree decl, dw_die_ref context_die)
21509 tree origin = decl_ultimate_origin (decl);
21510 dw_die_ref lbl_die = lookup_decl_die (decl);
21511 rtx insn;
21512 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21514 if (!lbl_die)
21516 lbl_die = new_die (DW_TAG_label, context_die, decl);
21517 equate_decl_number_to_die (decl, lbl_die);
21519 if (origin != NULL)
21520 add_abstract_origin_attribute (lbl_die, origin);
21521 else
21522 add_name_and_src_coords_attributes (lbl_die, decl);
21525 if (DECL_ABSTRACT_P (decl))
21526 equate_decl_number_to_die (decl, lbl_die);
21527 else
21529 insn = DECL_RTL_IF_SET (decl);
21531 /* Deleted labels are programmer specified labels which have been
21532 eliminated because of various optimizations. We still emit them
21533 here so that it is possible to put breakpoints on them. */
21534 if (insn
21535 && (LABEL_P (insn)
21536 || ((NOTE_P (insn)
21537 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21539 /* When optimization is enabled (via -O) some parts of the compiler
21540 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21541 represent source-level labels which were explicitly declared by
21542 the user. This really shouldn't be happening though, so catch
21543 it if it ever does happen. */
21544 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21546 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21547 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21549 else if (insn
21550 && NOTE_P (insn)
21551 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21552 && CODE_LABEL_NUMBER (insn) != -1)
21554 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21555 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21560 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
21561 attributes to the DIE for a block STMT, to describe where the inlined
21562 function was called from. This is similar to add_src_coords_attributes. */
21564 static inline void
21565 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21567 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21569 if (dwarf_version >= 3 || !dwarf_strict)
21571 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21572 add_AT_unsigned (die, DW_AT_call_line, s.line);
21577 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21578 Add low_pc and high_pc attributes to the DIE for a block STMT. */
21580 static inline void
21581 add_high_low_attributes (tree stmt, dw_die_ref die)
21583 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21585 if (BLOCK_FRAGMENT_CHAIN (stmt)
21586 && (dwarf_version >= 3 || !dwarf_strict))
21588 tree chain, superblock = NULL_TREE;
21589 dw_die_ref pdie;
21590 dw_attr_node *attr = NULL;
21592 if (inlined_function_outer_scope_p (stmt))
21594 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21595 BLOCK_NUMBER (stmt));
21596 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21599 /* Optimize duplicate .debug_ranges lists or even tails of
21600 lists. If this BLOCK has same ranges as its supercontext,
21601 lookup DW_AT_ranges attribute in the supercontext (and
21602 recursively so), verify that the ranges_table contains the
21603 right values and use it instead of adding a new .debug_range. */
21604 for (chain = stmt, pdie = die;
21605 BLOCK_SAME_RANGE (chain);
21606 chain = BLOCK_SUPERCONTEXT (chain))
21608 dw_attr_node *new_attr;
21610 pdie = pdie->die_parent;
21611 if (pdie == NULL)
21612 break;
21613 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21614 break;
21615 new_attr = get_AT (pdie, DW_AT_ranges);
21616 if (new_attr == NULL
21617 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21618 break;
21619 attr = new_attr;
21620 superblock = BLOCK_SUPERCONTEXT (chain);
21622 if (attr != NULL
21623 && (ranges_table[attr->dw_attr_val.v.val_offset
21624 / 2 / DWARF2_ADDR_SIZE].num
21625 == BLOCK_NUMBER (superblock))
21626 && BLOCK_FRAGMENT_CHAIN (superblock))
21628 unsigned long off = attr->dw_attr_val.v.val_offset
21629 / 2 / DWARF2_ADDR_SIZE;
21630 unsigned long supercnt = 0, thiscnt = 0;
21631 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21632 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21634 ++supercnt;
21635 gcc_checking_assert (ranges_table[off + supercnt].num
21636 == BLOCK_NUMBER (chain));
21638 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21639 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21640 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21641 ++thiscnt;
21642 gcc_assert (supercnt >= thiscnt);
21643 add_AT_range_list (die, DW_AT_ranges,
21644 ((off + supercnt - thiscnt)
21645 * 2 * DWARF2_ADDR_SIZE),
21646 false);
21647 return;
21650 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21652 chain = BLOCK_FRAGMENT_CHAIN (stmt);
21655 add_ranges (chain);
21656 chain = BLOCK_FRAGMENT_CHAIN (chain);
21658 while (chain);
21659 add_ranges (NULL);
21661 else
21663 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21664 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21665 BLOCK_NUMBER (stmt));
21666 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21667 BLOCK_NUMBER (stmt));
21668 add_AT_low_high_pc (die, label, label_high, false);
21672 /* Generate a DIE for a lexical block. */
21674 static void
21675 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21677 dw_die_ref old_die = BLOCK_DIE (stmt);
21678 dw_die_ref stmt_die = NULL;
21679 if (!old_die)
21681 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21682 BLOCK_DIE (stmt) = stmt_die;
21685 if (BLOCK_ABSTRACT (stmt))
21687 if (old_die)
21689 /* This must have been generated early and it won't even
21690 need location information since it's a DW_AT_inline
21691 function. */
21692 if (flag_checking)
21693 for (dw_die_ref c = context_die; c; c = c->die_parent)
21694 if (c->die_tag == DW_TAG_inlined_subroutine
21695 || c->die_tag == DW_TAG_subprogram)
21697 gcc_assert (get_AT (c, DW_AT_inline));
21698 break;
21700 return;
21703 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21705 /* If this is an inlined instance, create a new lexical die for
21706 anything below to attach DW_AT_abstract_origin to. */
21707 if (old_die)
21709 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21710 BLOCK_DIE (stmt) = stmt_die;
21711 old_die = NULL;
21714 tree origin = block_ultimate_origin (stmt);
21715 if (origin != NULL_TREE && origin != stmt)
21716 add_abstract_origin_attribute (stmt_die, origin);
21719 if (old_die)
21720 stmt_die = old_die;
21722 /* A non abstract block whose blocks have already been reordered
21723 should have the instruction range for this block. If so, set the
21724 high/low attributes. */
21725 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21727 gcc_assert (stmt_die);
21728 add_high_low_attributes (stmt, stmt_die);
21731 decls_for_scope (stmt, stmt_die);
21734 /* Generate a DIE for an inlined subprogram. */
21736 static void
21737 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21739 tree decl;
21741 /* The instance of function that is effectively being inlined shall not
21742 be abstract. */
21743 gcc_assert (! BLOCK_ABSTRACT (stmt));
21745 decl = block_ultimate_origin (stmt);
21747 /* Make sure any inlined functions are known to be inlineable. */
21748 gcc_checking_assert (DECL_ABSTRACT_P (decl)
21749 || cgraph_function_possibly_inlined_p (decl));
21751 /* Emit info for the abstract instance first, if we haven't yet. We
21752 must emit this even if the block is abstract, otherwise when we
21753 emit the block below (or elsewhere), we may end up trying to emit
21754 a die whose origin die hasn't been emitted, and crashing. */
21755 dwarf2out_abstract_function (decl);
21757 if (! BLOCK_ABSTRACT (stmt))
21759 dw_die_ref subr_die
21760 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21762 if (call_arg_locations)
21763 BLOCK_DIE (stmt) = subr_die;
21764 add_abstract_origin_attribute (subr_die, decl);
21765 if (TREE_ASM_WRITTEN (stmt))
21766 add_high_low_attributes (stmt, subr_die);
21767 add_call_src_coords_attributes (stmt, subr_die);
21769 decls_for_scope (stmt, subr_die);
21773 /* Generate a DIE for a field in a record, or structure. CTX is required: see
21774 the comment for VLR_CONTEXT. */
21776 static void
21777 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21779 dw_die_ref decl_die;
21781 if (TREE_TYPE (decl) == error_mark_node)
21782 return;
21784 decl_die = new_die (DW_TAG_member, context_die, decl);
21785 add_name_and_src_coords_attributes (decl_die, decl);
21786 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21787 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21788 context_die);
21790 if (DECL_BIT_FIELD_TYPE (decl))
21792 add_byte_size_attribute (decl_die, decl);
21793 add_bit_size_attribute (decl_die, decl);
21794 add_bit_offset_attribute (decl_die, decl, ctx);
21797 /* If we have a variant part offset, then we are supposed to process a member
21798 of a QUAL_UNION_TYPE, which is how we represent variant parts in
21799 trees. */
21800 gcc_assert (ctx->variant_part_offset == NULL_TREE
21801 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21802 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21803 add_data_member_location_attribute (decl_die, decl, ctx);
21805 if (DECL_ARTIFICIAL (decl))
21806 add_AT_flag (decl_die, DW_AT_artificial, 1);
21808 add_accessibility_attribute (decl_die, decl);
21810 /* Equate decl number to die, so that we can look up this decl later on. */
21811 equate_decl_number_to_die (decl, decl_die);
21814 #if 0
21815 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21816 Use modified_type_die instead.
21817 We keep this code here just in case these types of DIEs may be needed to
21818 represent certain things in other languages (e.g. Pascal) someday. */
21820 static void
21821 gen_pointer_type_die (tree type, dw_die_ref context_die)
21823 dw_die_ref ptr_die
21824 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21826 equate_type_number_to_die (type, ptr_die);
21827 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21828 context_die);
21829 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21832 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21833 Use modified_type_die instead.
21834 We keep this code here just in case these types of DIEs may be needed to
21835 represent certain things in other languages (e.g. Pascal) someday. */
21837 static void
21838 gen_reference_type_die (tree type, dw_die_ref context_die)
21840 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21842 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21843 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21844 else
21845 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21847 equate_type_number_to_die (type, ref_die);
21848 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21849 context_die);
21850 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21852 #endif
21854 /* Generate a DIE for a pointer to a member type. */
21856 static void
21857 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21859 dw_die_ref ptr_die
21860 = new_die (DW_TAG_ptr_to_member_type,
21861 scope_die_for (type, context_die), type);
21863 equate_type_number_to_die (type, ptr_die);
21864 add_AT_die_ref (ptr_die, DW_AT_containing_type,
21865 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21866 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21867 context_die);
21870 static char *producer_string;
21872 /* Return a heap allocated producer string including command line options
21873 if -grecord-gcc-switches. */
21875 static char *
21876 gen_producer_string (void)
21878 size_t j;
21879 auto_vec<const char *> switches;
21880 const char *language_string = lang_hooks.name;
21881 char *producer, *tail;
21882 const char *p;
21883 size_t len = dwarf_record_gcc_switches ? 0 : 3;
21884 size_t plen = strlen (language_string) + 1 + strlen (version_string);
21886 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21887 switch (save_decoded_options[j].opt_index)
21889 case OPT_o:
21890 case OPT_d:
21891 case OPT_dumpbase:
21892 case OPT_dumpdir:
21893 case OPT_auxbase:
21894 case OPT_auxbase_strip:
21895 case OPT_quiet:
21896 case OPT_version:
21897 case OPT_v:
21898 case OPT_w:
21899 case OPT_L:
21900 case OPT_D:
21901 case OPT_I:
21902 case OPT_U:
21903 case OPT_SPECIAL_unknown:
21904 case OPT_SPECIAL_ignore:
21905 case OPT_SPECIAL_program_name:
21906 case OPT_SPECIAL_input_file:
21907 case OPT_grecord_gcc_switches:
21908 case OPT_gno_record_gcc_switches:
21909 case OPT__output_pch_:
21910 case OPT_fdiagnostics_show_location_:
21911 case OPT_fdiagnostics_show_option:
21912 case OPT_fdiagnostics_show_caret:
21913 case OPT_fdiagnostics_color_:
21914 case OPT_fverbose_asm:
21915 case OPT____:
21916 case OPT__sysroot_:
21917 case OPT_nostdinc:
21918 case OPT_nostdinc__:
21919 case OPT_fpreprocessed:
21920 case OPT_fltrans_output_list_:
21921 case OPT_fresolution_:
21922 case OPT_fdebug_prefix_map_:
21923 /* Ignore these. */
21924 continue;
21925 default:
21926 if (cl_options[save_decoded_options[j].opt_index].flags
21927 & CL_NO_DWARF_RECORD)
21928 continue;
21929 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21930 == '-');
21931 switch (save_decoded_options[j].canonical_option[0][1])
21933 case 'M':
21934 case 'i':
21935 case 'W':
21936 continue;
21937 case 'f':
21938 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21939 "dump", 4) == 0)
21940 continue;
21941 break;
21942 default:
21943 break;
21945 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21946 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21947 break;
21950 producer = XNEWVEC (char, plen + 1 + len + 1);
21951 tail = producer;
21952 sprintf (tail, "%s %s", language_string, version_string);
21953 tail += plen;
21955 FOR_EACH_VEC_ELT (switches, j, p)
21957 len = strlen (p);
21958 *tail = ' ';
21959 memcpy (tail + 1, p, len);
21960 tail += len + 1;
21963 *tail = '\0';
21964 return producer;
21967 /* Given a C and/or C++ language/version string return the "highest".
21968 C++ is assumed to be "higher" than C in this case. Used for merging
21969 LTO translation unit languages. */
21970 static const char *
21971 highest_c_language (const char *lang1, const char *lang2)
21973 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21974 return "GNU C++14";
21975 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21976 return "GNU C++11";
21977 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21978 return "GNU C++98";
21980 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21981 return "GNU C11";
21982 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
21983 return "GNU C99";
21984 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
21985 return "GNU C89";
21987 gcc_unreachable ();
21991 /* Generate the DIE for the compilation unit. */
21993 static dw_die_ref
21994 gen_compile_unit_die (const char *filename)
21996 dw_die_ref die;
21997 const char *language_string = lang_hooks.name;
21998 int language;
22000 die = new_die (DW_TAG_compile_unit, NULL, NULL);
22002 if (filename)
22004 add_name_attribute (die, filename);
22005 /* Don't add cwd for <built-in>. */
22006 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
22007 add_comp_dir_attribute (die);
22010 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
22012 /* If our producer is LTO try to figure out a common language to use
22013 from the global list of translation units. */
22014 if (strcmp (language_string, "GNU GIMPLE") == 0)
22016 unsigned i;
22017 tree t;
22018 const char *common_lang = NULL;
22020 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
22022 if (!TRANSLATION_UNIT_LANGUAGE (t))
22023 continue;
22024 if (!common_lang)
22025 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
22026 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
22028 else if (strncmp (common_lang, "GNU C", 5) == 0
22029 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
22030 /* Mixing C and C++ is ok, use C++ in that case. */
22031 common_lang = highest_c_language (common_lang,
22032 TRANSLATION_UNIT_LANGUAGE (t));
22033 else
22035 /* Fall back to C. */
22036 common_lang = NULL;
22037 break;
22041 if (common_lang)
22042 language_string = common_lang;
22045 language = DW_LANG_C;
22046 if (strncmp (language_string, "GNU C", 5) == 0
22047 && ISDIGIT (language_string[5]))
22049 language = DW_LANG_C89;
22050 if (dwarf_version >= 3 || !dwarf_strict)
22052 if (strcmp (language_string, "GNU C89") != 0)
22053 language = DW_LANG_C99;
22055 if (dwarf_version >= 5 /* || !dwarf_strict */)
22056 if (strcmp (language_string, "GNU C11") == 0)
22057 language = DW_LANG_C11;
22060 else if (strncmp (language_string, "GNU C++", 7) == 0)
22062 language = DW_LANG_C_plus_plus;
22063 if (dwarf_version >= 5 /* || !dwarf_strict */)
22065 if (strcmp (language_string, "GNU C++11") == 0)
22066 language = DW_LANG_C_plus_plus_11;
22067 else if (strcmp (language_string, "GNU C++14") == 0)
22068 language = DW_LANG_C_plus_plus_14;
22071 else if (strcmp (language_string, "GNU F77") == 0)
22072 language = DW_LANG_Fortran77;
22073 else if (strcmp (language_string, "GNU Pascal") == 0)
22074 language = DW_LANG_Pascal83;
22075 else if (dwarf_version >= 3 || !dwarf_strict)
22077 if (strcmp (language_string, "GNU Ada") == 0)
22078 language = DW_LANG_Ada95;
22079 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22081 language = DW_LANG_Fortran95;
22082 if (dwarf_version >= 5 /* || !dwarf_strict */)
22084 if (strcmp (language_string, "GNU Fortran2003") == 0)
22085 language = DW_LANG_Fortran03;
22086 else if (strcmp (language_string, "GNU Fortran2008") == 0)
22087 language = DW_LANG_Fortran08;
22090 else if (strcmp (language_string, "GNU Java") == 0)
22091 language = DW_LANG_Java;
22092 else if (strcmp (language_string, "GNU Objective-C") == 0)
22093 language = DW_LANG_ObjC;
22094 else if (strcmp (language_string, "GNU Objective-C++") == 0)
22095 language = DW_LANG_ObjC_plus_plus;
22096 else if (dwarf_version >= 5 || !dwarf_strict)
22098 if (strcmp (language_string, "GNU Go") == 0)
22099 language = DW_LANG_Go;
22102 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
22103 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22104 language = DW_LANG_Fortran90;
22106 add_AT_unsigned (die, DW_AT_language, language);
22108 switch (language)
22110 case DW_LANG_Fortran77:
22111 case DW_LANG_Fortran90:
22112 case DW_LANG_Fortran95:
22113 case DW_LANG_Fortran03:
22114 case DW_LANG_Fortran08:
22115 /* Fortran has case insensitive identifiers and the front-end
22116 lowercases everything. */
22117 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
22118 break;
22119 default:
22120 /* The default DW_ID_case_sensitive doesn't need to be specified. */
22121 break;
22123 return die;
22126 /* Generate the DIE for a base class. */
22128 static void
22129 gen_inheritance_die (tree binfo, tree access, tree type,
22130 dw_die_ref context_die)
22132 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
22133 struct vlr_context ctx = { type, NULL };
22135 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
22136 context_die);
22137 add_data_member_location_attribute (die, binfo, &ctx);
22139 if (BINFO_VIRTUAL_P (binfo))
22140 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22142 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
22143 children, otherwise the default is DW_ACCESS_public. In DWARF2
22144 the default has always been DW_ACCESS_private. */
22145 if (access == access_public_node)
22147 if (dwarf_version == 2
22148 || context_die->die_tag == DW_TAG_class_type)
22149 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
22151 else if (access == access_protected_node)
22152 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
22153 else if (dwarf_version > 2
22154 && context_die->die_tag != DW_TAG_class_type)
22155 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
22158 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
22159 structure. */
22160 static bool
22161 is_variant_part (tree decl)
22163 return (TREE_CODE (decl) == FIELD_DECL
22164 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
22167 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
22168 return the FIELD_DECL. Return NULL_TREE otherwise. */
22170 static tree
22171 analyze_discr_in_predicate (tree operand, tree struct_type)
22173 bool continue_stripping = true;
22174 while (continue_stripping)
22175 switch (TREE_CODE (operand))
22177 CASE_CONVERT:
22178 operand = TREE_OPERAND (operand, 0);
22179 break;
22180 default:
22181 continue_stripping = false;
22182 break;
22185 /* Match field access to members of struct_type only. */
22186 if (TREE_CODE (operand) == COMPONENT_REF
22187 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
22188 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
22189 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
22190 return TREE_OPERAND (operand, 1);
22191 else
22192 return NULL_TREE;
22195 /* Check that SRC is a constant integer that can be represented as a native
22196 integer constant (either signed or unsigned). If so, store it into DEST and
22197 return true. Return false otherwise. */
22199 static bool
22200 get_discr_value (tree src, dw_discr_value *dest)
22202 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
22204 if (TREE_CODE (src) != INTEGER_CST
22205 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
22206 return false;
22208 dest->pos = is_unsigned;
22209 if (is_unsigned)
22210 dest->v.uval = tree_to_uhwi (src);
22211 else
22212 dest->v.sval = tree_to_shwi (src);
22214 return true;
22217 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
22218 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
22219 store NULL_TREE in DISCR_DECL. Otherwise:
22221 - store the discriminant field in STRUCT_TYPE that controls the variant
22222 part to *DISCR_DECL
22224 - put in *DISCR_LISTS_P an array where for each variant, the item
22225 represents the corresponding matching list of discriminant values.
22227 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
22228 the above array.
22230 Note that when the array is allocated (i.e. when the analysis is
22231 successful), it is up to the caller to free the array. */
22233 static void
22234 analyze_variants_discr (tree variant_part_decl,
22235 tree struct_type,
22236 tree *discr_decl,
22237 dw_discr_list_ref **discr_lists_p,
22238 unsigned *discr_lists_length)
22240 tree variant_part_type = TREE_TYPE (variant_part_decl);
22241 tree variant;
22242 dw_discr_list_ref *discr_lists;
22243 unsigned i;
22245 /* Compute how many variants there are in this variant part. */
22246 *discr_lists_length = 0;
22247 for (variant = TYPE_FIELDS (variant_part_type);
22248 variant != NULL_TREE;
22249 variant = DECL_CHAIN (variant))
22250 ++*discr_lists_length;
22252 *discr_decl = NULL_TREE;
22253 *discr_lists_p
22254 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22255 sizeof (**discr_lists_p));
22256 discr_lists = *discr_lists_p;
22258 /* And then analyze all variants to extract discriminant information for all
22259 of them. This analysis is conservative: as soon as we detect something we
22260 do not support, abort everything and pretend we found nothing. */
22261 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22262 variant != NULL_TREE;
22263 variant = DECL_CHAIN (variant), ++i)
22265 tree match_expr = DECL_QUALIFIER (variant);
22267 /* Now, try to analyze the predicate and deduce a discriminant for
22268 it. */
22269 if (match_expr == boolean_true_node)
22270 /* Typically happens for the default variant: it matches all cases that
22271 previous variants rejected. Don't output any matching value for
22272 this one. */
22273 continue;
22275 /* The following loop tries to iterate over each discriminant
22276 possibility: single values or ranges. */
22277 while (match_expr != NULL_TREE)
22279 tree next_round_match_expr;
22280 tree candidate_discr = NULL_TREE;
22281 dw_discr_list_ref new_node = NULL;
22283 /* Possibilities are matched one after the other by nested
22284 TRUTH_ORIF_EXPR expressions. Process the current possibility and
22285 continue with the rest at next iteration. */
22286 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22288 next_round_match_expr = TREE_OPERAND (match_expr, 0);
22289 match_expr = TREE_OPERAND (match_expr, 1);
22291 else
22292 next_round_match_expr = NULL_TREE;
22294 if (match_expr == boolean_false_node)
22295 /* This sub-expression matches nothing: just wait for the next
22296 one. */
22299 else if (TREE_CODE (match_expr) == EQ_EXPR)
22301 /* We are matching: <discr_field> == <integer_cst>
22302 This sub-expression matches a single value. */
22303 tree integer_cst = TREE_OPERAND (match_expr, 1);
22305 candidate_discr
22306 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22307 struct_type);
22309 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22310 if (!get_discr_value (integer_cst,
22311 &new_node->dw_discr_lower_bound))
22312 goto abort;
22313 new_node->dw_discr_range = false;
22316 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22318 /* We are matching:
22319 <discr_field> > <integer_cst>
22320 && <discr_field> < <integer_cst>.
22321 This sub-expression matches the range of values between the
22322 two matched integer constants. Note that comparisons can be
22323 inclusive or exclusive. */
22324 tree candidate_discr_1, candidate_discr_2;
22325 tree lower_cst, upper_cst;
22326 bool lower_cst_included, upper_cst_included;
22327 tree lower_op = TREE_OPERAND (match_expr, 0);
22328 tree upper_op = TREE_OPERAND (match_expr, 1);
22330 /* When the comparison is exclusive, the integer constant is not
22331 the discriminant range bound we are looking for: we will have
22332 to increment or decrement it. */
22333 if (TREE_CODE (lower_op) == GE_EXPR)
22334 lower_cst_included = true;
22335 else if (TREE_CODE (lower_op) == GT_EXPR)
22336 lower_cst_included = false;
22337 else
22338 goto abort;
22340 if (TREE_CODE (upper_op) == LE_EXPR)
22341 upper_cst_included = true;
22342 else if (TREE_CODE (upper_op) == LT_EXPR)
22343 upper_cst_included = false;
22344 else
22345 goto abort;
22347 /* Extract the discriminant from the first operand and check it
22348 is consistant with the same analysis in the second
22349 operand. */
22350 candidate_discr_1
22351 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22352 struct_type);
22353 candidate_discr_2
22354 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22355 struct_type);
22356 if (candidate_discr_1 == candidate_discr_2)
22357 candidate_discr = candidate_discr_1;
22358 else
22359 goto abort;
22361 /* Extract bounds from both. */
22362 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22363 lower_cst = TREE_OPERAND (lower_op, 1);
22364 upper_cst = TREE_OPERAND (upper_op, 1);
22366 if (!lower_cst_included)
22367 lower_cst
22368 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22369 lower_cst,
22370 build_int_cst (TREE_TYPE (lower_cst), 1)));
22371 if (!upper_cst_included)
22372 upper_cst
22373 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22374 upper_cst,
22375 build_int_cst (TREE_TYPE (upper_cst), 1)));
22377 if (!get_discr_value (lower_cst,
22378 &new_node->dw_discr_lower_bound)
22379 || !get_discr_value (upper_cst,
22380 &new_node->dw_discr_upper_bound))
22381 goto abort;
22383 new_node->dw_discr_range = true;
22386 else
22387 /* Unsupported sub-expression: we cannot determine the set of
22388 matching discriminant values. Abort everything. */
22389 goto abort;
22391 /* If the discriminant info is not consistant with what we saw so
22392 far, consider the analysis failed and abort everything. */
22393 if (candidate_discr == NULL_TREE
22394 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22395 goto abort;
22396 else
22397 *discr_decl = candidate_discr;
22399 if (new_node != NULL)
22401 new_node->dw_discr_next = discr_lists[i];
22402 discr_lists[i] = new_node;
22404 match_expr = next_round_match_expr;
22408 /* If we reach this point, we could match everything we were interested
22409 in. */
22410 return;
22412 abort:
22413 /* Clean all data structure and return no result. */
22414 free (*discr_lists_p);
22415 *discr_lists_p = NULL;
22416 *discr_decl = NULL_TREE;
22419 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22420 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
22421 under CONTEXT_DIE.
22423 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22424 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
22425 this type, which are record types, represent the available variants and each
22426 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
22427 values are inferred from these attributes.
22429 In trees, the offsets for the fields inside these sub-records are relative
22430 to the variant part itself, whereas the corresponding DIEs should have
22431 offset attributes that are relative to the embedding record base address.
22432 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22433 must be an expression that computes the offset of the variant part to
22434 describe in DWARF. */
22436 static void
22437 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22438 dw_die_ref context_die)
22440 const tree variant_part_type = TREE_TYPE (variant_part_decl);
22441 tree variant_part_offset = vlr_ctx->variant_part_offset;
22442 struct loc_descr_context ctx = {
22443 vlr_ctx->struct_type, /* context_type */
22444 NULL_TREE, /* base_decl */
22445 NULL /* dpi */
22448 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22449 NULL_TREE if there is no such field. */
22450 tree discr_decl = NULL_TREE;
22451 dw_discr_list_ref *discr_lists;
22452 unsigned discr_lists_length = 0;
22453 unsigned i;
22455 dw_die_ref dwarf_proc_die = NULL;
22456 dw_die_ref variant_part_die
22457 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22459 equate_decl_number_to_die (variant_part_decl, variant_part_die);
22461 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22462 &discr_decl, &discr_lists, &discr_lists_length);
22464 if (discr_decl != NULL_TREE)
22466 dw_die_ref discr_die = lookup_decl_die (discr_decl);
22468 if (discr_die)
22469 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22470 else
22471 /* We have no DIE for the discriminant, so just discard all
22472 discrimimant information in the output. */
22473 discr_decl = NULL_TREE;
22476 /* If the offset for this variant part is more complex than a constant,
22477 create a DWARF procedure for it so that we will not have to generate DWARF
22478 expressions for it for each member. */
22479 if (TREE_CODE (variant_part_offset) != INTEGER_CST
22480 && (dwarf_version >= 3 || !dwarf_strict))
22482 const tree dwarf_proc_fndecl
22483 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22484 build_function_type (TREE_TYPE (variant_part_offset),
22485 NULL_TREE));
22486 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22487 const dw_loc_descr_ref dwarf_proc_body
22488 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22490 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22491 dwarf_proc_fndecl, context_die);
22492 if (dwarf_proc_die != NULL)
22493 variant_part_offset = dwarf_proc_call;
22496 /* Output DIEs for all variants. */
22497 i = 0;
22498 for (tree variant = TYPE_FIELDS (variant_part_type);
22499 variant != NULL_TREE;
22500 variant = DECL_CHAIN (variant), ++i)
22502 tree variant_type = TREE_TYPE (variant);
22503 dw_die_ref variant_die;
22505 /* All variants (i.e. members of a variant part) are supposed to be
22506 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
22507 under these records. */
22508 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22510 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22511 equate_decl_number_to_die (variant, variant_die);
22513 /* Output discriminant values this variant matches, if any. */
22514 if (discr_decl == NULL || discr_lists[i] == NULL)
22515 /* In the case we have discriminant information at all, this is
22516 probably the default variant: as the standard says, don't
22517 output any discriminant value/list attribute. */
22519 else if (discr_lists[i]->dw_discr_next == NULL
22520 && !discr_lists[i]->dw_discr_range)
22521 /* If there is only one accepted value, don't bother outputting a
22522 list. */
22523 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22524 else
22525 add_discr_list (variant_die, discr_lists[i]);
22527 for (tree member = TYPE_FIELDS (variant_type);
22528 member != NULL_TREE;
22529 member = DECL_CHAIN (member))
22531 struct vlr_context vlr_sub_ctx = {
22532 vlr_ctx->struct_type, /* struct_type */
22533 NULL /* variant_part_offset */
22535 if (is_variant_part (member))
22537 /* All offsets for fields inside variant parts are relative to
22538 the top-level embedding RECORD_TYPE's base address. On the
22539 other hand, offsets in GCC's types are relative to the
22540 nested-most variant part. So we have to sum offsets each time
22541 we recurse. */
22543 vlr_sub_ctx.variant_part_offset
22544 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22545 variant_part_offset, byte_position (member)));
22546 gen_variant_part (member, &vlr_sub_ctx, variant_die);
22548 else
22550 vlr_sub_ctx.variant_part_offset = variant_part_offset;
22551 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22556 free (discr_lists);
22559 /* Generate a DIE for a class member. */
22561 static void
22562 gen_member_die (tree type, dw_die_ref context_die)
22564 tree member;
22565 tree binfo = TYPE_BINFO (type);
22566 dw_die_ref child;
22568 /* If this is not an incomplete type, output descriptions of each of its
22569 members. Note that as we output the DIEs necessary to represent the
22570 members of this record or union type, we will also be trying to output
22571 DIEs to represent the *types* of those members. However the `type'
22572 function (above) will specifically avoid generating type DIEs for member
22573 types *within* the list of member DIEs for this (containing) type except
22574 for those types (of members) which are explicitly marked as also being
22575 members of this (containing) type themselves. The g++ front- end can
22576 force any given type to be treated as a member of some other (containing)
22577 type by setting the TYPE_CONTEXT of the given (member) type to point to
22578 the TREE node representing the appropriate (containing) type. */
22580 /* First output info about the base classes. */
22581 if (binfo)
22583 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22584 int i;
22585 tree base;
22587 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22588 gen_inheritance_die (base,
22589 (accesses ? (*accesses)[i] : access_public_node),
22590 type,
22591 context_die);
22594 /* Now output info about the data members and type members. */
22595 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22597 struct vlr_context vlr_ctx = { type, NULL_TREE };
22599 /* If we thought we were generating minimal debug info for TYPE
22600 and then changed our minds, some of the member declarations
22601 may have already been defined. Don't define them again, but
22602 do put them in the right order. */
22604 child = lookup_decl_die (member);
22605 if (child)
22606 splice_child_die (context_die, child);
22608 /* Do not generate standard DWARF for variant parts if we are generating
22609 the corresponding GNAT encodings: DIEs generated for both would
22610 conflict in our mappings. */
22611 else if (is_variant_part (member)
22612 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22614 vlr_ctx.variant_part_offset = byte_position (member);
22615 gen_variant_part (member, &vlr_ctx, context_die);
22617 else
22619 vlr_ctx.variant_part_offset = NULL_TREE;
22620 gen_decl_die (member, NULL, &vlr_ctx, context_die);
22624 /* We do not keep type methods in type variants. */
22625 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22626 /* Now output info about the function members (if any). */
22627 if (TYPE_METHODS (type) != error_mark_node)
22628 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22630 /* Don't include clones in the member list. */
22631 if (DECL_ABSTRACT_ORIGIN (member))
22632 continue;
22633 /* Nor constructors for anonymous classes. */
22634 if (DECL_ARTIFICIAL (member)
22635 && dwarf2_name (member, 0) == NULL)
22636 continue;
22638 child = lookup_decl_die (member);
22639 if (child)
22640 splice_child_die (context_die, child);
22641 else
22642 gen_decl_die (member, NULL, NULL, context_die);
22646 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
22647 is set, we pretend that the type was never defined, so we only get the
22648 member DIEs needed by later specification DIEs. */
22650 static void
22651 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22652 enum debug_info_usage usage)
22654 if (TREE_ASM_WRITTEN (type))
22656 /* Fill in the bound of variable-length fields in late dwarf if
22657 still incomplete. */
22658 if (!early_dwarf && variably_modified_type_p (type, NULL))
22659 for (tree member = TYPE_FIELDS (type);
22660 member;
22661 member = DECL_CHAIN (member))
22662 fill_variable_array_bounds (TREE_TYPE (member));
22663 return;
22666 dw_die_ref type_die = lookup_type_die (type);
22667 dw_die_ref scope_die = 0;
22668 int nested = 0;
22669 int complete = (TYPE_SIZE (type)
22670 && (! TYPE_STUB_DECL (type)
22671 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22672 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22673 complete = complete && should_emit_struct_debug (type, usage);
22675 if (type_die && ! complete)
22676 return;
22678 if (TYPE_CONTEXT (type) != NULL_TREE
22679 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22680 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22681 nested = 1;
22683 scope_die = scope_die_for (type, context_die);
22685 /* Generate child dies for template paramaters. */
22686 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22687 schedule_generic_params_dies_gen (type);
22689 if (! type_die || (nested && is_cu_die (scope_die)))
22690 /* First occurrence of type or toplevel definition of nested class. */
22692 dw_die_ref old_die = type_die;
22694 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22695 ? record_type_tag (type) : DW_TAG_union_type,
22696 scope_die, type);
22697 equate_type_number_to_die (type, type_die);
22698 if (old_die)
22699 add_AT_specification (type_die, old_die);
22700 else
22701 add_name_attribute (type_die, type_tag (type));
22703 else
22704 remove_AT (type_die, DW_AT_declaration);
22706 /* If this type has been completed, then give it a byte_size attribute and
22707 then give a list of members. */
22708 if (complete && !ns_decl)
22710 /* Prevent infinite recursion in cases where the type of some member of
22711 this type is expressed in terms of this type itself. */
22712 TREE_ASM_WRITTEN (type) = 1;
22713 add_byte_size_attribute (type_die, type);
22714 if (TYPE_STUB_DECL (type) != NULL_TREE)
22716 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22717 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22720 /* If the first reference to this type was as the return type of an
22721 inline function, then it may not have a parent. Fix this now. */
22722 if (type_die->die_parent == NULL)
22723 add_child_die (scope_die, type_die);
22725 push_decl_scope (type);
22726 gen_member_die (type, type_die);
22727 pop_decl_scope ();
22729 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22730 if (TYPE_ARTIFICIAL (type))
22731 add_AT_flag (type_die, DW_AT_artificial, 1);
22733 /* GNU extension: Record what type our vtable lives in. */
22734 if (TYPE_VFIELD (type))
22736 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22738 gen_type_die (vtype, context_die);
22739 add_AT_die_ref (type_die, DW_AT_containing_type,
22740 lookup_type_die (vtype));
22743 else
22745 add_AT_flag (type_die, DW_AT_declaration, 1);
22747 /* We don't need to do this for function-local types. */
22748 if (TYPE_STUB_DECL (type)
22749 && ! decl_function_context (TYPE_STUB_DECL (type)))
22750 vec_safe_push (incomplete_types, type);
22753 if (get_AT (type_die, DW_AT_name))
22754 add_pubtype (type, type_die);
22757 /* Generate a DIE for a subroutine _type_. */
22759 static void
22760 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22762 tree return_type = TREE_TYPE (type);
22763 dw_die_ref subr_die
22764 = new_die (DW_TAG_subroutine_type,
22765 scope_die_for (type, context_die), type);
22767 equate_type_number_to_die (type, subr_die);
22768 add_prototyped_attribute (subr_die, type);
22769 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22770 context_die);
22771 gen_formal_types_die (type, subr_die);
22773 if (get_AT (subr_die, DW_AT_name))
22774 add_pubtype (type, subr_die);
22777 /* Generate a DIE for a type definition. */
22779 static void
22780 gen_typedef_die (tree decl, dw_die_ref context_die)
22782 dw_die_ref type_die;
22783 tree origin;
22785 if (TREE_ASM_WRITTEN (decl))
22787 if (DECL_ORIGINAL_TYPE (decl))
22788 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22789 return;
22792 TREE_ASM_WRITTEN (decl) = 1;
22793 type_die = new_die (DW_TAG_typedef, context_die, decl);
22794 origin = decl_ultimate_origin (decl);
22795 if (origin != NULL)
22796 add_abstract_origin_attribute (type_die, origin);
22797 else
22799 tree type;
22801 add_name_and_src_coords_attributes (type_die, decl);
22802 if (DECL_ORIGINAL_TYPE (decl))
22804 type = DECL_ORIGINAL_TYPE (decl);
22806 if (type == error_mark_node)
22807 return;
22809 gcc_assert (type != TREE_TYPE (decl));
22810 equate_type_number_to_die (TREE_TYPE (decl), type_die);
22812 else
22814 type = TREE_TYPE (decl);
22816 if (type == error_mark_node)
22817 return;
22819 if (is_naming_typedef_decl (TYPE_NAME (type)))
22821 /* Here, we are in the case of decl being a typedef naming
22822 an anonymous type, e.g:
22823 typedef struct {...} foo;
22824 In that case TREE_TYPE (decl) is not a typedef variant
22825 type and TYPE_NAME of the anonymous type is set to the
22826 TYPE_DECL of the typedef. This construct is emitted by
22827 the C++ FE.
22829 TYPE is the anonymous struct named by the typedef
22830 DECL. As we need the DW_AT_type attribute of the
22831 DW_TAG_typedef to point to the DIE of TYPE, let's
22832 generate that DIE right away. add_type_attribute
22833 called below will then pick (via lookup_type_die) that
22834 anonymous struct DIE. */
22835 if (!TREE_ASM_WRITTEN (type))
22836 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22838 /* This is a GNU Extension. We are adding a
22839 DW_AT_linkage_name attribute to the DIE of the
22840 anonymous struct TYPE. The value of that attribute
22841 is the name of the typedef decl naming the anonymous
22842 struct. This greatly eases the work of consumers of
22843 this debug info. */
22844 add_linkage_name_raw (lookup_type_die (type), decl);
22848 add_type_attribute (type_die, type, decl_quals (decl), false,
22849 context_die);
22851 if (is_naming_typedef_decl (decl))
22852 /* We want that all subsequent calls to lookup_type_die with
22853 TYPE in argument yield the DW_TAG_typedef we have just
22854 created. */
22855 equate_type_number_to_die (type, type_die);
22857 add_accessibility_attribute (type_die, decl);
22860 if (DECL_ABSTRACT_P (decl))
22861 equate_decl_number_to_die (decl, type_die);
22863 if (get_AT (type_die, DW_AT_name))
22864 add_pubtype (decl, type_die);
22867 /* Generate a DIE for a struct, class, enum or union type. */
22869 static void
22870 gen_tagged_type_die (tree type,
22871 dw_die_ref context_die,
22872 enum debug_info_usage usage)
22874 int need_pop;
22876 if (type == NULL_TREE
22877 || !is_tagged_type (type))
22878 return;
22880 if (TREE_ASM_WRITTEN (type))
22881 need_pop = 0;
22882 /* If this is a nested type whose containing class hasn't been written
22883 out yet, writing it out will cover this one, too. This does not apply
22884 to instantiations of member class templates; they need to be added to
22885 the containing class as they are generated. FIXME: This hurts the
22886 idea of combining type decls from multiple TUs, since we can't predict
22887 what set of template instantiations we'll get. */
22888 else if (TYPE_CONTEXT (type)
22889 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22890 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22892 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22894 if (TREE_ASM_WRITTEN (type))
22895 return;
22897 /* If that failed, attach ourselves to the stub. */
22898 push_decl_scope (TYPE_CONTEXT (type));
22899 context_die = lookup_type_die (TYPE_CONTEXT (type));
22900 need_pop = 1;
22902 else if (TYPE_CONTEXT (type) != NULL_TREE
22903 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22905 /* If this type is local to a function that hasn't been written
22906 out yet, use a NULL context for now; it will be fixed up in
22907 decls_for_scope. */
22908 context_die = lookup_decl_die (TYPE_CONTEXT (type));
22909 /* A declaration DIE doesn't count; nested types need to go in the
22910 specification. */
22911 if (context_die && is_declaration_die (context_die))
22912 context_die = NULL;
22913 need_pop = 0;
22915 else
22917 context_die = declare_in_namespace (type, context_die);
22918 need_pop = 0;
22921 if (TREE_CODE (type) == ENUMERAL_TYPE)
22923 /* This might have been written out by the call to
22924 declare_in_namespace. */
22925 if (!TREE_ASM_WRITTEN (type))
22926 gen_enumeration_type_die (type, context_die);
22928 else
22929 gen_struct_or_union_type_die (type, context_die, usage);
22931 if (need_pop)
22932 pop_decl_scope ();
22934 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22935 it up if it is ever completed. gen_*_type_die will set it for us
22936 when appropriate. */
22939 /* Generate a type description DIE. */
22941 static void
22942 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22943 enum debug_info_usage usage)
22945 struct array_descr_info info;
22947 if (type == NULL_TREE || type == error_mark_node)
22948 return;
22950 if (flag_checking && type)
22951 verify_type (type);
22953 if (TYPE_NAME (type) != NULL_TREE
22954 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22955 && is_redundant_typedef (TYPE_NAME (type))
22956 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22957 /* The DECL of this type is a typedef we don't want to emit debug
22958 info for but we want debug info for its underlying typedef.
22959 This can happen for e.g, the injected-class-name of a C++
22960 type. */
22961 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22963 /* If TYPE is a typedef type variant, let's generate debug info
22964 for the parent typedef which TYPE is a type of. */
22965 if (typedef_variant_p (type))
22967 if (TREE_ASM_WRITTEN (type))
22968 return;
22970 /* Prevent broken recursion; we can't hand off to the same type. */
22971 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22973 /* Give typedefs the right scope. */
22974 context_die = scope_die_for (type, context_die);
22976 TREE_ASM_WRITTEN (type) = 1;
22978 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22979 return;
22982 /* If type is an anonymous tagged type named by a typedef, let's
22983 generate debug info for the typedef. */
22984 if (is_naming_typedef_decl (TYPE_NAME (type)))
22986 /* Use the DIE of the containing namespace as the parent DIE of
22987 the type description DIE we want to generate. */
22988 if (DECL_CONTEXT (TYPE_NAME (type))
22989 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
22990 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
22992 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22993 return;
22996 /* We are going to output a DIE to represent the unqualified version
22997 of this type (i.e. without any const or volatile qualifiers) so
22998 get the main variant (i.e. the unqualified version) of this type
22999 now. (Vectors and arrays are special because the debugging info is in the
23000 cloned type itself). */
23001 if (TREE_CODE (type) != VECTOR_TYPE
23002 && TREE_CODE (type) != ARRAY_TYPE)
23003 type = type_main_variant (type);
23005 /* If this is an array type with hidden descriptor, handle it first. */
23006 if (!TREE_ASM_WRITTEN (type)
23007 && lang_hooks.types.get_array_descr_info)
23009 memset (&info, 0, sizeof (info));
23010 if (lang_hooks.types.get_array_descr_info (type, &info))
23012 /* Fortran sometimes emits array types with no dimension. */
23013 gcc_assert (info.ndimensions >= 0
23014 && (info.ndimensions
23015 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
23016 gen_descr_array_type_die (type, &info, context_die);
23017 TREE_ASM_WRITTEN (type) = 1;
23018 return;
23022 if (TREE_ASM_WRITTEN (type))
23024 /* Variable-length types may be incomplete even if
23025 TREE_ASM_WRITTEN. For such types, fall through to
23026 gen_array_type_die() and possibly fill in
23027 DW_AT_{upper,lower}_bound attributes. */
23028 if ((TREE_CODE (type) != ARRAY_TYPE
23029 && TREE_CODE (type) != RECORD_TYPE
23030 && TREE_CODE (type) != UNION_TYPE
23031 && TREE_CODE (type) != QUAL_UNION_TYPE)
23032 || !variably_modified_type_p (type, NULL))
23033 return;
23036 switch (TREE_CODE (type))
23038 case ERROR_MARK:
23039 break;
23041 case POINTER_TYPE:
23042 case REFERENCE_TYPE:
23043 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
23044 ensures that the gen_type_die recursion will terminate even if the
23045 type is recursive. Recursive types are possible in Ada. */
23046 /* ??? We could perhaps do this for all types before the switch
23047 statement. */
23048 TREE_ASM_WRITTEN (type) = 1;
23050 /* For these types, all that is required is that we output a DIE (or a
23051 set of DIEs) to represent the "basis" type. */
23052 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23053 DINFO_USAGE_IND_USE);
23054 break;
23056 case OFFSET_TYPE:
23057 /* This code is used for C++ pointer-to-data-member types.
23058 Output a description of the relevant class type. */
23059 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
23060 DINFO_USAGE_IND_USE);
23062 /* Output a description of the type of the object pointed to. */
23063 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23064 DINFO_USAGE_IND_USE);
23066 /* Now output a DIE to represent this pointer-to-data-member type
23067 itself. */
23068 gen_ptr_to_mbr_type_die (type, context_die);
23069 break;
23071 case FUNCTION_TYPE:
23072 /* Force out return type (in case it wasn't forced out already). */
23073 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23074 DINFO_USAGE_DIR_USE);
23075 gen_subroutine_type_die (type, context_die);
23076 break;
23078 case METHOD_TYPE:
23079 /* Force out return type (in case it wasn't forced out already). */
23080 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23081 DINFO_USAGE_DIR_USE);
23082 gen_subroutine_type_die (type, context_die);
23083 break;
23085 case ARRAY_TYPE:
23086 case VECTOR_TYPE:
23087 gen_array_type_die (type, context_die);
23088 break;
23090 case ENUMERAL_TYPE:
23091 case RECORD_TYPE:
23092 case UNION_TYPE:
23093 case QUAL_UNION_TYPE:
23094 gen_tagged_type_die (type, context_die, usage);
23095 return;
23097 case VOID_TYPE:
23098 case INTEGER_TYPE:
23099 case REAL_TYPE:
23100 case FIXED_POINT_TYPE:
23101 case COMPLEX_TYPE:
23102 case BOOLEAN_TYPE:
23103 case POINTER_BOUNDS_TYPE:
23104 /* No DIEs needed for fundamental types. */
23105 break;
23107 case NULLPTR_TYPE:
23108 case LANG_TYPE:
23109 /* Just use DW_TAG_unspecified_type. */
23111 dw_die_ref type_die = lookup_type_die (type);
23112 if (type_die == NULL)
23114 tree name = TYPE_IDENTIFIER (type);
23115 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
23116 type);
23117 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
23118 equate_type_number_to_die (type, type_die);
23121 break;
23123 default:
23124 if (is_cxx_auto (type))
23126 tree name = TYPE_IDENTIFIER (type);
23127 dw_die_ref *die = (name == get_identifier ("auto")
23128 ? &auto_die : &decltype_auto_die);
23129 if (!*die)
23131 *die = new_die (DW_TAG_unspecified_type,
23132 comp_unit_die (), NULL_TREE);
23133 add_name_attribute (*die, IDENTIFIER_POINTER (name));
23135 equate_type_number_to_die (type, *die);
23136 break;
23138 gcc_unreachable ();
23141 TREE_ASM_WRITTEN (type) = 1;
23144 static void
23145 gen_type_die (tree type, dw_die_ref context_die)
23147 if (type != error_mark_node)
23149 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
23150 if (flag_checking)
23152 dw_die_ref die = lookup_type_die (type);
23153 if (die)
23154 check_die (die);
23159 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
23160 things which are local to the given block. */
23162 static void
23163 gen_block_die (tree stmt, dw_die_ref context_die)
23165 int must_output_die = 0;
23166 bool inlined_func;
23168 /* Ignore blocks that are NULL. */
23169 if (stmt == NULL_TREE)
23170 return;
23172 inlined_func = inlined_function_outer_scope_p (stmt);
23174 /* If the block is one fragment of a non-contiguous block, do not
23175 process the variables, since they will have been done by the
23176 origin block. Do process subblocks. */
23177 if (BLOCK_FRAGMENT_ORIGIN (stmt))
23179 tree sub;
23181 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
23182 gen_block_die (sub, context_die);
23184 return;
23187 /* Determine if we need to output any Dwarf DIEs at all to represent this
23188 block. */
23189 if (inlined_func)
23190 /* The outer scopes for inlinings *must* always be represented. We
23191 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
23192 must_output_die = 1;
23193 else
23195 /* Determine if this block directly contains any "significant"
23196 local declarations which we will need to output DIEs for. */
23197 if (debug_info_level > DINFO_LEVEL_TERSE)
23198 /* We are not in terse mode so *any* local declaration counts
23199 as being a "significant" one. */
23200 must_output_die = ((BLOCK_VARS (stmt) != NULL
23201 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
23202 && (TREE_USED (stmt)
23203 || TREE_ASM_WRITTEN (stmt)
23204 || BLOCK_ABSTRACT (stmt)));
23205 else if ((TREE_USED (stmt)
23206 || TREE_ASM_WRITTEN (stmt)
23207 || BLOCK_ABSTRACT (stmt))
23208 && !dwarf2out_ignore_block (stmt))
23209 must_output_die = 1;
23212 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
23213 DIE for any block which contains no significant local declarations at
23214 all. Rather, in such cases we just call `decls_for_scope' so that any
23215 needed Dwarf info for any sub-blocks will get properly generated. Note
23216 that in terse mode, our definition of what constitutes a "significant"
23217 local declaration gets restricted to include only inlined function
23218 instances and local (nested) function definitions. */
23219 if (must_output_die)
23221 if (inlined_func)
23223 /* If STMT block is abstract, that means we have been called
23224 indirectly from dwarf2out_abstract_function.
23225 That function rightfully marks the descendent blocks (of
23226 the abstract function it is dealing with) as being abstract,
23227 precisely to prevent us from emitting any
23228 DW_TAG_inlined_subroutine DIE as a descendent
23229 of an abstract function instance. So in that case, we should
23230 not call gen_inlined_subroutine_die.
23232 Later though, when cgraph asks dwarf2out to emit info
23233 for the concrete instance of the function decl into which
23234 the concrete instance of STMT got inlined, the later will lead
23235 to the generation of a DW_TAG_inlined_subroutine DIE. */
23236 if (! BLOCK_ABSTRACT (stmt))
23237 gen_inlined_subroutine_die (stmt, context_die);
23239 else
23240 gen_lexical_block_die (stmt, context_die);
23242 else
23243 decls_for_scope (stmt, context_die);
23246 /* Process variable DECL (or variable with origin ORIGIN) within
23247 block STMT and add it to CONTEXT_DIE. */
23248 static void
23249 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23251 dw_die_ref die;
23252 tree decl_or_origin = decl ? decl : origin;
23254 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23255 die = lookup_decl_die (decl_or_origin);
23256 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
23257 && TYPE_DECL_IS_STUB (decl_or_origin))
23258 die = lookup_type_die (TREE_TYPE (decl_or_origin));
23259 else
23260 die = NULL;
23262 if (die != NULL && die->die_parent == NULL)
23263 add_child_die (context_die, die);
23264 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23266 if (early_dwarf)
23267 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23268 stmt, context_die);
23270 else
23271 gen_decl_die (decl, origin, NULL, context_die);
23274 /* Generate all of the decls declared within a given scope and (recursively)
23275 all of its sub-blocks. */
23277 static void
23278 decls_for_scope (tree stmt, dw_die_ref context_die)
23280 tree decl;
23281 unsigned int i;
23282 tree subblocks;
23284 /* Ignore NULL blocks. */
23285 if (stmt == NULL_TREE)
23286 return;
23288 /* Output the DIEs to represent all of the data objects and typedefs
23289 declared directly within this block but not within any nested
23290 sub-blocks. Also, nested function and tag DIEs have been
23291 generated with a parent of NULL; fix that up now. We don't
23292 have to do this if we're at -g1. */
23293 if (debug_info_level > DINFO_LEVEL_TERSE)
23295 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23296 process_scope_var (stmt, decl, NULL_TREE, context_die);
23297 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23298 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23299 context_die);
23302 /* Even if we're at -g1, we need to process the subblocks in order to get
23303 inlined call information. */
23305 /* Output the DIEs to represent all sub-blocks (and the items declared
23306 therein) of this block. */
23307 for (subblocks = BLOCK_SUBBLOCKS (stmt);
23308 subblocks != NULL;
23309 subblocks = BLOCK_CHAIN (subblocks))
23310 gen_block_die (subblocks, context_die);
23313 /* Is this a typedef we can avoid emitting? */
23315 bool
23316 is_redundant_typedef (const_tree decl)
23318 if (TYPE_DECL_IS_STUB (decl))
23319 return true;
23321 if (DECL_ARTIFICIAL (decl)
23322 && DECL_CONTEXT (decl)
23323 && is_tagged_type (DECL_CONTEXT (decl))
23324 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23325 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23326 /* Also ignore the artificial member typedef for the class name. */
23327 return true;
23329 return false;
23332 /* Return TRUE if TYPE is a typedef that names a type for linkage
23333 purposes. This kind of typedefs is produced by the C++ FE for
23334 constructs like:
23336 typedef struct {...} foo;
23338 In that case, there is no typedef variant type produced for foo.
23339 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23340 struct type. */
23342 static bool
23343 is_naming_typedef_decl (const_tree decl)
23345 if (decl == NULL_TREE
23346 || TREE_CODE (decl) != TYPE_DECL
23347 || DECL_NAMELESS (decl)
23348 || !is_tagged_type (TREE_TYPE (decl))
23349 || DECL_IS_BUILTIN (decl)
23350 || is_redundant_typedef (decl)
23351 /* It looks like Ada produces TYPE_DECLs that are very similar
23352 to C++ naming typedefs but that have different
23353 semantics. Let's be specific to c++ for now. */
23354 || !is_cxx ())
23355 return FALSE;
23357 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23358 && TYPE_NAME (TREE_TYPE (decl)) == decl
23359 && (TYPE_STUB_DECL (TREE_TYPE (decl))
23360 != TYPE_NAME (TREE_TYPE (decl))));
23363 /* Looks up the DIE for a context. */
23365 static inline dw_die_ref
23366 lookup_context_die (tree context)
23368 if (context)
23370 /* Find die that represents this context. */
23371 if (TYPE_P (context))
23373 context = TYPE_MAIN_VARIANT (context);
23374 dw_die_ref ctx = lookup_type_die (context);
23375 if (!ctx)
23376 return NULL;
23377 return strip_naming_typedef (context, ctx);
23379 else
23380 return lookup_decl_die (context);
23382 return comp_unit_die ();
23385 /* Returns the DIE for a context. */
23387 static inline dw_die_ref
23388 get_context_die (tree context)
23390 if (context)
23392 /* Find die that represents this context. */
23393 if (TYPE_P (context))
23395 context = TYPE_MAIN_VARIANT (context);
23396 return strip_naming_typedef (context, force_type_die (context));
23398 else
23399 return force_decl_die (context);
23401 return comp_unit_die ();
23404 /* Returns the DIE for decl. A DIE will always be returned. */
23406 static dw_die_ref
23407 force_decl_die (tree decl)
23409 dw_die_ref decl_die;
23410 unsigned saved_external_flag;
23411 tree save_fn = NULL_TREE;
23412 decl_die = lookup_decl_die (decl);
23413 if (!decl_die)
23415 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23417 decl_die = lookup_decl_die (decl);
23418 if (decl_die)
23419 return decl_die;
23421 switch (TREE_CODE (decl))
23423 case FUNCTION_DECL:
23424 /* Clear current_function_decl, so that gen_subprogram_die thinks
23425 that this is a declaration. At this point, we just want to force
23426 declaration die. */
23427 save_fn = current_function_decl;
23428 current_function_decl = NULL_TREE;
23429 gen_subprogram_die (decl, context_die);
23430 current_function_decl = save_fn;
23431 break;
23433 case VAR_DECL:
23434 /* Set external flag to force declaration die. Restore it after
23435 gen_decl_die() call. */
23436 saved_external_flag = DECL_EXTERNAL (decl);
23437 DECL_EXTERNAL (decl) = 1;
23438 gen_decl_die (decl, NULL, NULL, context_die);
23439 DECL_EXTERNAL (decl) = saved_external_flag;
23440 break;
23442 case NAMESPACE_DECL:
23443 if (dwarf_version >= 3 || !dwarf_strict)
23444 dwarf2out_decl (decl);
23445 else
23446 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
23447 decl_die = comp_unit_die ();
23448 break;
23450 case TRANSLATION_UNIT_DECL:
23451 decl_die = comp_unit_die ();
23452 break;
23454 default:
23455 gcc_unreachable ();
23458 /* We should be able to find the DIE now. */
23459 if (!decl_die)
23460 decl_die = lookup_decl_die (decl);
23461 gcc_assert (decl_die);
23464 return decl_die;
23467 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
23468 always returned. */
23470 static dw_die_ref
23471 force_type_die (tree type)
23473 dw_die_ref type_die;
23475 type_die = lookup_type_die (type);
23476 if (!type_die)
23478 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23480 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23481 false, context_die);
23482 gcc_assert (type_die);
23484 return type_die;
23487 /* Force out any required namespaces to be able to output DECL,
23488 and return the new context_die for it, if it's changed. */
23490 static dw_die_ref
23491 setup_namespace_context (tree thing, dw_die_ref context_die)
23493 tree context = (DECL_P (thing)
23494 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23495 if (context && TREE_CODE (context) == NAMESPACE_DECL)
23496 /* Force out the namespace. */
23497 context_die = force_decl_die (context);
23499 return context_die;
23502 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23503 type) within its namespace, if appropriate.
23505 For compatibility with older debuggers, namespace DIEs only contain
23506 declarations; all definitions are emitted at CU scope, with
23507 DW_AT_specification pointing to the declaration (like with class
23508 members). */
23510 static dw_die_ref
23511 declare_in_namespace (tree thing, dw_die_ref context_die)
23513 dw_die_ref ns_context;
23515 if (debug_info_level <= DINFO_LEVEL_TERSE)
23516 return context_die;
23518 /* External declarations in the local scope only need to be emitted
23519 once, not once in the namespace and once in the scope.
23521 This avoids declaring the `extern' below in the
23522 namespace DIE as well as in the innermost scope:
23524 namespace S
23526 int i=5;
23527 int foo()
23529 int i=8;
23530 extern int i;
23531 return i;
23535 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23536 return context_die;
23538 /* If this decl is from an inlined function, then don't try to emit it in its
23539 namespace, as we will get confused. It would have already been emitted
23540 when the abstract instance of the inline function was emitted anyways. */
23541 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23542 return context_die;
23544 ns_context = setup_namespace_context (thing, context_die);
23546 if (ns_context != context_die)
23548 if (is_fortran ())
23549 return ns_context;
23550 if (DECL_P (thing))
23551 gen_decl_die (thing, NULL, NULL, ns_context);
23552 else
23553 gen_type_die (thing, ns_context);
23555 return context_die;
23558 /* Generate a DIE for a namespace or namespace alias. */
23560 static void
23561 gen_namespace_die (tree decl, dw_die_ref context_die)
23563 dw_die_ref namespace_die;
23565 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23566 they are an alias of. */
23567 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23569 /* Output a real namespace or module. */
23570 context_die = setup_namespace_context (decl, comp_unit_die ());
23571 namespace_die = new_die (is_fortran ()
23572 ? DW_TAG_module : DW_TAG_namespace,
23573 context_die, decl);
23574 /* For Fortran modules defined in different CU don't add src coords. */
23575 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23577 const char *name = dwarf2_name (decl, 0);
23578 if (name)
23579 add_name_attribute (namespace_die, name);
23581 else
23582 add_name_and_src_coords_attributes (namespace_die, decl);
23583 if (DECL_EXTERNAL (decl))
23584 add_AT_flag (namespace_die, DW_AT_declaration, 1);
23585 equate_decl_number_to_die (decl, namespace_die);
23587 else
23589 /* Output a namespace alias. */
23591 /* Force out the namespace we are an alias of, if necessary. */
23592 dw_die_ref origin_die
23593 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23595 if (DECL_FILE_SCOPE_P (decl)
23596 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23597 context_die = setup_namespace_context (decl, comp_unit_die ());
23598 /* Now create the namespace alias DIE. */
23599 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23600 add_name_and_src_coords_attributes (namespace_die, decl);
23601 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23602 equate_decl_number_to_die (decl, namespace_die);
23604 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
23605 if (want_pubnames ())
23606 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23609 /* Generate Dwarf debug information for a decl described by DECL.
23610 The return value is currently only meaningful for PARM_DECLs,
23611 for all other decls it returns NULL.
23613 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23614 It can be NULL otherwise. */
23616 static dw_die_ref
23617 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23618 dw_die_ref context_die)
23620 tree decl_or_origin = decl ? decl : origin;
23621 tree class_origin = NULL, ultimate_origin;
23623 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23624 return NULL;
23626 /* Ignore pointer bounds decls. */
23627 if (DECL_P (decl_or_origin)
23628 && TREE_TYPE (decl_or_origin)
23629 && POINTER_BOUNDS_P (decl_or_origin))
23630 return NULL;
23632 switch (TREE_CODE (decl_or_origin))
23634 case ERROR_MARK:
23635 break;
23637 case CONST_DECL:
23638 if (!is_fortran () && !is_ada ())
23640 /* The individual enumerators of an enum type get output when we output
23641 the Dwarf representation of the relevant enum type itself. */
23642 break;
23645 /* Emit its type. */
23646 gen_type_die (TREE_TYPE (decl), context_die);
23648 /* And its containing namespace. */
23649 context_die = declare_in_namespace (decl, context_die);
23651 gen_const_die (decl, context_die);
23652 break;
23654 case FUNCTION_DECL:
23655 /* Don't output any DIEs to represent mere function declarations,
23656 unless they are class members or explicit block externs. */
23657 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23658 && DECL_FILE_SCOPE_P (decl_or_origin)
23659 && (current_function_decl == NULL_TREE
23660 || DECL_ARTIFICIAL (decl_or_origin)))
23661 break;
23663 #if 0
23664 /* FIXME */
23665 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23666 on local redeclarations of global functions. That seems broken. */
23667 if (current_function_decl != decl)
23668 /* This is only a declaration. */;
23669 #endif
23671 /* If we're emitting a clone, emit info for the abstract instance. */
23672 if (origin || DECL_ORIGIN (decl) != decl)
23673 dwarf2out_abstract_function (origin
23674 ? DECL_ORIGIN (origin)
23675 : DECL_ABSTRACT_ORIGIN (decl));
23677 /* If we're emitting an out-of-line copy of an inline function,
23678 emit info for the abstract instance and set up to refer to it. */
23679 else if (cgraph_function_possibly_inlined_p (decl)
23680 && ! DECL_ABSTRACT_P (decl)
23681 && ! class_or_namespace_scope_p (context_die)
23682 /* dwarf2out_abstract_function won't emit a die if this is just
23683 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
23684 that case, because that works only if we have a die. */
23685 && DECL_INITIAL (decl) != NULL_TREE)
23687 dwarf2out_abstract_function (decl);
23688 set_decl_origin_self (decl);
23691 /* Otherwise we're emitting the primary DIE for this decl. */
23692 else if (debug_info_level > DINFO_LEVEL_TERSE)
23694 /* Before we describe the FUNCTION_DECL itself, make sure that we
23695 have its containing type. */
23696 if (!origin)
23697 origin = decl_class_context (decl);
23698 if (origin != NULL_TREE)
23699 gen_type_die (origin, context_die);
23701 /* And its return type. */
23702 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23704 /* And its virtual context. */
23705 if (DECL_VINDEX (decl) != NULL_TREE)
23706 gen_type_die (DECL_CONTEXT (decl), context_die);
23708 /* Make sure we have a member DIE for decl. */
23709 if (origin != NULL_TREE)
23710 gen_type_die_for_member (origin, decl, context_die);
23712 /* And its containing namespace. */
23713 context_die = declare_in_namespace (decl, context_die);
23716 /* Now output a DIE to represent the function itself. */
23717 if (decl)
23718 gen_subprogram_die (decl, context_die);
23719 break;
23721 case TYPE_DECL:
23722 /* If we are in terse mode, don't generate any DIEs to represent any
23723 actual typedefs. */
23724 if (debug_info_level <= DINFO_LEVEL_TERSE)
23725 break;
23727 /* In the special case of a TYPE_DECL node representing the declaration
23728 of some type tag, if the given TYPE_DECL is marked as having been
23729 instantiated from some other (original) TYPE_DECL node (e.g. one which
23730 was generated within the original definition of an inline function) we
23731 used to generate a special (abbreviated) DW_TAG_structure_type,
23732 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
23733 should be actually referencing those DIEs, as variable DIEs with that
23734 type would be emitted already in the abstract origin, so it was always
23735 removed during unused type prunning. Don't add anything in this
23736 case. */
23737 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23738 break;
23740 if (is_redundant_typedef (decl))
23741 gen_type_die (TREE_TYPE (decl), context_die);
23742 else
23743 /* Output a DIE to represent the typedef itself. */
23744 gen_typedef_die (decl, context_die);
23745 break;
23747 case LABEL_DECL:
23748 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23749 gen_label_die (decl, context_die);
23750 break;
23752 case VAR_DECL:
23753 case RESULT_DECL:
23754 /* If we are in terse mode, don't generate any DIEs to represent any
23755 variable declarations or definitions. */
23756 if (debug_info_level <= DINFO_LEVEL_TERSE)
23757 break;
23759 /* Output any DIEs that are needed to specify the type of this data
23760 object. */
23761 if (decl_by_reference_p (decl_or_origin))
23762 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23763 else
23764 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23766 /* And its containing type. */
23767 class_origin = decl_class_context (decl_or_origin);
23768 if (class_origin != NULL_TREE)
23769 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23771 /* And its containing namespace. */
23772 context_die = declare_in_namespace (decl_or_origin, context_die);
23774 /* Now output the DIE to represent the data object itself. This gets
23775 complicated because of the possibility that the VAR_DECL really
23776 represents an inlined instance of a formal parameter for an inline
23777 function. */
23778 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23779 if (ultimate_origin != NULL_TREE
23780 && TREE_CODE (ultimate_origin) == PARM_DECL)
23781 gen_formal_parameter_die (decl, origin,
23782 true /* Emit name attribute. */,
23783 context_die);
23784 else
23785 gen_variable_die (decl, origin, context_die);
23786 break;
23788 case FIELD_DECL:
23789 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23790 /* Ignore the nameless fields that are used to skip bits but handle C++
23791 anonymous unions and structs. */
23792 if (DECL_NAME (decl) != NULL_TREE
23793 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23794 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23796 gen_type_die (member_declared_type (decl), context_die);
23797 gen_field_die (decl, ctx, context_die);
23799 break;
23801 case PARM_DECL:
23802 if (DECL_BY_REFERENCE (decl_or_origin))
23803 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23804 else
23805 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23806 return gen_formal_parameter_die (decl, origin,
23807 true /* Emit name attribute. */,
23808 context_die);
23810 case NAMESPACE_DECL:
23811 if (dwarf_version >= 3 || !dwarf_strict)
23812 gen_namespace_die (decl, context_die);
23813 break;
23815 case IMPORTED_DECL:
23816 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23817 DECL_CONTEXT (decl), context_die);
23818 break;
23820 case NAMELIST_DECL:
23821 gen_namelist_decl (DECL_NAME (decl), context_die,
23822 NAMELIST_DECL_ASSOCIATED_DECL (decl));
23823 break;
23825 default:
23826 /* Probably some frontend-internal decl. Assume we don't care. */
23827 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23828 break;
23831 return NULL;
23834 /* Output initial debug information for global DECL. Called at the
23835 end of the parsing process.
23837 This is the initial debug generation process. As such, the DIEs
23838 generated may be incomplete. A later debug generation pass
23839 (dwarf2out_late_global_decl) will augment the information generated
23840 in this pass (e.g., with complete location info). */
23842 static void
23843 dwarf2out_early_global_decl (tree decl)
23845 set_early_dwarf s;
23847 /* gen_decl_die() will set DECL_ABSTRACT because
23848 cgraph_function_possibly_inlined_p() returns true. This is in
23849 turn will cause DW_AT_inline attributes to be set.
23851 This happens because at early dwarf generation, there is no
23852 cgraph information, causing cgraph_function_possibly_inlined_p()
23853 to return true. Trick cgraph_function_possibly_inlined_p()
23854 while we generate dwarf early. */
23855 bool save = symtab->global_info_ready;
23856 symtab->global_info_ready = true;
23858 /* We don't handle TYPE_DECLs. If required, they'll be reached via
23859 other DECLs and they can point to template types or other things
23860 that dwarf2out can't handle when done via dwarf2out_decl. */
23861 if (TREE_CODE (decl) != TYPE_DECL
23862 && TREE_CODE (decl) != PARM_DECL)
23864 tree save_fndecl = current_function_decl;
23865 if (TREE_CODE (decl) == FUNCTION_DECL)
23867 /* No cfun means the symbol has no body, so there's nothing
23868 to emit. */
23869 if (!DECL_STRUCT_FUNCTION (decl))
23870 goto early_decl_exit;
23872 current_function_decl = decl;
23874 dwarf2out_decl (decl);
23875 if (TREE_CODE (decl) == FUNCTION_DECL)
23876 current_function_decl = save_fndecl;
23878 early_decl_exit:
23879 symtab->global_info_ready = save;
23882 /* Output debug information for global decl DECL. Called from
23883 toplev.c after compilation proper has finished. */
23885 static void
23886 dwarf2out_late_global_decl (tree decl)
23888 /* Fill-in any location information we were unable to determine
23889 on the first pass. */
23890 if (TREE_CODE (decl) == VAR_DECL
23891 && !POINTER_BOUNDS_P (decl))
23893 dw_die_ref die = lookup_decl_die (decl);
23895 /* We have to generate early debug late for LTO. */
23896 if (! die && in_lto_p)
23898 dwarf2out_decl (decl);
23899 die = lookup_decl_die (decl);
23902 if (die)
23904 /* We get called during the early debug phase via the symtab
23905 code invoking late_global_decl for symbols that are optimized
23906 out. When the early phase is not finished, do not add
23907 locations. */
23908 if (! early_dwarf_finished)
23909 tree_add_const_value_attribute_for_decl (die, decl);
23910 else
23911 add_location_or_const_value_attribute (die, decl, false);
23916 /* Output debug information for type decl DECL. Called from toplev.c
23917 and from language front ends (to record built-in types). */
23918 static void
23919 dwarf2out_type_decl (tree decl, int local)
23921 if (!local)
23923 set_early_dwarf s;
23924 dwarf2out_decl (decl);
23928 /* Output debug information for imported module or decl DECL.
23929 NAME is non-NULL name in the lexical block if the decl has been renamed.
23930 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23931 that DECL belongs to.
23932 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
23933 static void
23934 dwarf2out_imported_module_or_decl_1 (tree decl,
23935 tree name,
23936 tree lexical_block,
23937 dw_die_ref lexical_block_die)
23939 expanded_location xloc;
23940 dw_die_ref imported_die = NULL;
23941 dw_die_ref at_import_die;
23943 if (TREE_CODE (decl) == IMPORTED_DECL)
23945 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23946 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23947 gcc_assert (decl);
23949 else
23950 xloc = expand_location (input_location);
23952 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23954 at_import_die = force_type_die (TREE_TYPE (decl));
23955 /* For namespace N { typedef void T; } using N::T; base_type_die
23956 returns NULL, but DW_TAG_imported_declaration requires
23957 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
23958 if (!at_import_die)
23960 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23961 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23962 at_import_die = lookup_type_die (TREE_TYPE (decl));
23963 gcc_assert (at_import_die);
23966 else
23968 at_import_die = lookup_decl_die (decl);
23969 if (!at_import_die)
23971 /* If we're trying to avoid duplicate debug info, we may not have
23972 emitted the member decl for this field. Emit it now. */
23973 if (TREE_CODE (decl) == FIELD_DECL)
23975 tree type = DECL_CONTEXT (decl);
23977 if (TYPE_CONTEXT (type)
23978 && TYPE_P (TYPE_CONTEXT (type))
23979 && !should_emit_struct_debug (TYPE_CONTEXT (type),
23980 DINFO_USAGE_DIR_USE))
23981 return;
23982 gen_type_die_for_member (type, decl,
23983 get_context_die (TYPE_CONTEXT (type)));
23985 if (TREE_CODE (decl) == NAMELIST_DECL)
23986 at_import_die = gen_namelist_decl (DECL_NAME (decl),
23987 get_context_die (DECL_CONTEXT (decl)),
23988 NULL_TREE);
23989 else
23990 at_import_die = force_decl_die (decl);
23994 if (TREE_CODE (decl) == NAMESPACE_DECL)
23996 if (dwarf_version >= 3 || !dwarf_strict)
23997 imported_die = new_die (DW_TAG_imported_module,
23998 lexical_block_die,
23999 lexical_block);
24000 else
24001 return;
24003 else
24004 imported_die = new_die (DW_TAG_imported_declaration,
24005 lexical_block_die,
24006 lexical_block);
24008 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
24009 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
24010 if (name)
24011 add_AT_string (imported_die, DW_AT_name,
24012 IDENTIFIER_POINTER (name));
24013 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
24016 /* Output debug information for imported module or decl DECL.
24017 NAME is non-NULL name in context if the decl has been renamed.
24018 CHILD is true if decl is one of the renamed decls as part of
24019 importing whole module. */
24021 static void
24022 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
24023 bool child)
24025 /* dw_die_ref at_import_die; */
24026 dw_die_ref scope_die;
24028 if (debug_info_level <= DINFO_LEVEL_TERSE)
24029 return;
24031 gcc_assert (decl);
24033 set_early_dwarf s;
24035 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
24036 We need decl DIE for reference and scope die. First, get DIE for the decl
24037 itself. */
24039 /* Get the scope die for decl context. Use comp_unit_die for global module
24040 or decl. If die is not found for non globals, force new die. */
24041 if (context
24042 && TYPE_P (context)
24043 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
24044 return;
24046 if (!(dwarf_version >= 3 || !dwarf_strict))
24047 return;
24049 scope_die = get_context_die (context);
24051 if (child)
24053 gcc_assert (scope_die->die_child);
24054 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
24055 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
24056 scope_die = scope_die->die_child;
24059 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
24060 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
24063 /* Output debug information for namelists. */
24065 static dw_die_ref
24066 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
24068 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
24069 tree value;
24070 unsigned i;
24072 if (debug_info_level <= DINFO_LEVEL_TERSE)
24073 return NULL;
24075 gcc_assert (scope_die != NULL);
24076 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
24077 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
24079 /* If there are no item_decls, we have a nondefining namelist, e.g.
24080 with USE association; hence, set DW_AT_declaration. */
24081 if (item_decls == NULL_TREE)
24083 add_AT_flag (nml_die, DW_AT_declaration, 1);
24084 return nml_die;
24087 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
24089 nml_item_ref_die = lookup_decl_die (value);
24090 if (!nml_item_ref_die)
24091 nml_item_ref_die = force_decl_die (value);
24093 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
24094 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
24096 return nml_die;
24100 /* Write the debugging output for DECL and return the DIE. */
24102 static void
24103 dwarf2out_decl (tree decl)
24105 dw_die_ref context_die = comp_unit_die ();
24107 switch (TREE_CODE (decl))
24109 case ERROR_MARK:
24110 return;
24112 case FUNCTION_DECL:
24113 /* What we would really like to do here is to filter out all mere
24114 file-scope declarations of file-scope functions which are never
24115 referenced later within this translation unit (and keep all of ones
24116 that *are* referenced later on) but we aren't clairvoyant, so we have
24117 no idea which functions will be referenced in the future (i.e. later
24118 on within the current translation unit). So here we just ignore all
24119 file-scope function declarations which are not also definitions. If
24120 and when the debugger needs to know something about these functions,
24121 it will have to hunt around and find the DWARF information associated
24122 with the definition of the function.
24124 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
24125 nodes represent definitions and which ones represent mere
24126 declarations. We have to check DECL_INITIAL instead. That's because
24127 the C front-end supports some weird semantics for "extern inline"
24128 function definitions. These can get inlined within the current
24129 translation unit (and thus, we need to generate Dwarf info for their
24130 abstract instances so that the Dwarf info for the concrete inlined
24131 instances can have something to refer to) but the compiler never
24132 generates any out-of-lines instances of such things (despite the fact
24133 that they *are* definitions).
24135 The important point is that the C front-end marks these "extern
24136 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
24137 them anyway. Note that the C++ front-end also plays some similar games
24138 for inline function definitions appearing within include files which
24139 also contain `#pragma interface' pragmas.
24141 If we are called from dwarf2out_abstract_function output a DIE
24142 anyway. We can end up here this way with early inlining and LTO
24143 where the inlined function is output in a different LTRANS unit
24144 or not at all. */
24145 if (DECL_INITIAL (decl) == NULL_TREE
24146 && ! DECL_ABSTRACT_P (decl))
24147 return;
24149 /* If we're a nested function, initially use a parent of NULL; if we're
24150 a plain function, this will be fixed up in decls_for_scope. If
24151 we're a method, it will be ignored, since we already have a DIE. */
24152 if (decl_function_context (decl)
24153 /* But if we're in terse mode, we don't care about scope. */
24154 && debug_info_level > DINFO_LEVEL_TERSE)
24155 context_die = NULL;
24156 break;
24158 case VAR_DECL:
24159 /* For local statics lookup proper context die. */
24160 if (local_function_static (decl))
24161 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24163 /* If we are in terse mode, don't generate any DIEs to represent any
24164 variable declarations or definitions. */
24165 if (debug_info_level <= DINFO_LEVEL_TERSE)
24166 return;
24167 break;
24169 case CONST_DECL:
24170 if (debug_info_level <= DINFO_LEVEL_TERSE)
24171 return;
24172 if (!is_fortran () && !is_ada ())
24173 return;
24174 if (TREE_STATIC (decl) && decl_function_context (decl))
24175 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24176 break;
24178 case NAMESPACE_DECL:
24179 case IMPORTED_DECL:
24180 if (debug_info_level <= DINFO_LEVEL_TERSE)
24181 return;
24182 if (lookup_decl_die (decl) != NULL)
24183 return;
24184 break;
24186 case TYPE_DECL:
24187 /* Don't emit stubs for types unless they are needed by other DIEs. */
24188 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
24189 return;
24191 /* Don't bother trying to generate any DIEs to represent any of the
24192 normal built-in types for the language we are compiling. */
24193 if (DECL_IS_BUILTIN (decl))
24194 return;
24196 /* If we are in terse mode, don't generate any DIEs for types. */
24197 if (debug_info_level <= DINFO_LEVEL_TERSE)
24198 return;
24200 /* If we're a function-scope tag, initially use a parent of NULL;
24201 this will be fixed up in decls_for_scope. */
24202 if (decl_function_context (decl))
24203 context_die = NULL;
24205 break;
24207 case NAMELIST_DECL:
24208 break;
24210 default:
24211 return;
24214 gen_decl_die (decl, NULL, NULL, context_die);
24216 if (flag_checking)
24218 dw_die_ref die = lookup_decl_die (decl);
24219 if (die)
24220 check_die (die);
24224 /* Write the debugging output for DECL. */
24226 static void
24227 dwarf2out_function_decl (tree decl)
24229 dwarf2out_decl (decl);
24230 call_arg_locations = NULL;
24231 call_arg_loc_last = NULL;
24232 call_site_count = -1;
24233 tail_call_site_count = -1;
24234 decl_loc_table->empty ();
24235 cached_dw_loc_list_table->empty ();
24238 /* Output a marker (i.e. a label) for the beginning of the generated code for
24239 a lexical block. */
24241 static void
24242 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
24243 unsigned int blocknum)
24245 switch_to_section (current_function_section ());
24246 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
24249 /* Output a marker (i.e. a label) for the end of the generated code for a
24250 lexical block. */
24252 static void
24253 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
24255 switch_to_section (current_function_section ());
24256 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
24259 /* Returns nonzero if it is appropriate not to emit any debugging
24260 information for BLOCK, because it doesn't contain any instructions.
24262 Don't allow this for blocks with nested functions or local classes
24263 as we would end up with orphans, and in the presence of scheduling
24264 we may end up calling them anyway. */
24266 static bool
24267 dwarf2out_ignore_block (const_tree block)
24269 tree decl;
24270 unsigned int i;
24272 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24273 if (TREE_CODE (decl) == FUNCTION_DECL
24274 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24275 return 0;
24276 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24278 decl = BLOCK_NONLOCALIZED_VAR (block, i);
24279 if (TREE_CODE (decl) == FUNCTION_DECL
24280 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24281 return 0;
24284 return 1;
24287 /* Hash table routines for file_hash. */
24289 bool
24290 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24292 return filename_cmp (p1->filename, p2) == 0;
24295 hashval_t
24296 dwarf_file_hasher::hash (dwarf_file_data *p)
24298 return htab_hash_string (p->filename);
24301 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24302 dwarf2out.c) and return its "index". The index of each (known) filename is
24303 just a unique number which is associated with only that one filename. We
24304 need such numbers for the sake of generating labels (in the .debug_sfnames
24305 section) and references to those files numbers (in the .debug_srcinfo
24306 and .debug_macinfo sections). If the filename given as an argument is not
24307 found in our current list, add it to the list and assign it the next
24308 available unique index number. */
24310 static struct dwarf_file_data *
24311 lookup_filename (const char *file_name)
24313 struct dwarf_file_data * created;
24315 if (!file_name)
24316 return NULL;
24318 dwarf_file_data **slot
24319 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24320 INSERT);
24321 if (*slot)
24322 return *slot;
24324 created = ggc_alloc<dwarf_file_data> ();
24325 created->filename = file_name;
24326 created->emitted_number = 0;
24327 *slot = created;
24328 return created;
24331 /* If the assembler will construct the file table, then translate the compiler
24332 internal file table number into the assembler file table number, and emit
24333 a .file directive if we haven't already emitted one yet. The file table
24334 numbers are different because we prune debug info for unused variables and
24335 types, which may include filenames. */
24337 static int
24338 maybe_emit_file (struct dwarf_file_data * fd)
24340 if (! fd->emitted_number)
24342 if (last_emitted_file)
24343 fd->emitted_number = last_emitted_file->emitted_number + 1;
24344 else
24345 fd->emitted_number = 1;
24346 last_emitted_file = fd;
24348 if (DWARF2_ASM_LINE_DEBUG_INFO)
24350 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24351 output_quoted_string (asm_out_file,
24352 remap_debug_filename (fd->filename));
24353 fputc ('\n', asm_out_file);
24357 return fd->emitted_number;
24360 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24361 That generation should happen after function debug info has been
24362 generated. The value of the attribute is the constant value of ARG. */
24364 static void
24365 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24367 die_arg_entry entry;
24369 if (!die || !arg)
24370 return;
24372 gcc_assert (early_dwarf);
24374 if (!tmpl_value_parm_die_table)
24375 vec_alloc (tmpl_value_parm_die_table, 32);
24377 entry.die = die;
24378 entry.arg = arg;
24379 vec_safe_push (tmpl_value_parm_die_table, entry);
24382 /* Return TRUE if T is an instance of generic type, FALSE
24383 otherwise. */
24385 static bool
24386 generic_type_p (tree t)
24388 if (t == NULL_TREE || !TYPE_P (t))
24389 return false;
24390 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24393 /* Schedule the generation of the generic parameter dies for the
24394 instance of generic type T. The proper generation itself is later
24395 done by gen_scheduled_generic_parms_dies. */
24397 static void
24398 schedule_generic_params_dies_gen (tree t)
24400 if (!generic_type_p (t))
24401 return;
24403 gcc_assert (early_dwarf);
24405 if (!generic_type_instances)
24406 vec_alloc (generic_type_instances, 256);
24408 vec_safe_push (generic_type_instances, t);
24411 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24412 by append_entry_to_tmpl_value_parm_die_table. This function must
24413 be called after function DIEs have been generated. */
24415 static void
24416 gen_remaining_tmpl_value_param_die_attribute (void)
24418 if (tmpl_value_parm_die_table)
24420 unsigned i, j;
24421 die_arg_entry *e;
24423 /* We do this in two phases - first get the cases we can
24424 handle during early-finish, preserving those we cannot
24425 (containing symbolic constants where we don't yet know
24426 whether we are going to output the referenced symbols).
24427 For those we try again at late-finish. */
24428 j = 0;
24429 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24431 if (!tree_add_const_value_attribute (e->die, e->arg))
24433 dw_loc_descr_ref loc = NULL;
24434 if (! early_dwarf
24435 && (dwarf_version >= 5 || !dwarf_strict))
24436 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
24437 if (loc)
24438 add_AT_loc (e->die, DW_AT_location, loc);
24439 else
24440 (*tmpl_value_parm_die_table)[j++] = *e;
24443 tmpl_value_parm_die_table->truncate (j);
24447 /* Generate generic parameters DIEs for instances of generic types
24448 that have been previously scheduled by
24449 schedule_generic_params_dies_gen. This function must be called
24450 after all the types of the CU have been laid out. */
24452 static void
24453 gen_scheduled_generic_parms_dies (void)
24455 unsigned i;
24456 tree t;
24458 if (!generic_type_instances)
24459 return;
24461 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24462 if (COMPLETE_TYPE_P (t))
24463 gen_generic_params_dies (t);
24465 generic_type_instances = NULL;
24469 /* Replace DW_AT_name for the decl with name. */
24471 static void
24472 dwarf2out_set_name (tree decl, tree name)
24474 dw_die_ref die;
24475 dw_attr_node *attr;
24476 const char *dname;
24478 die = TYPE_SYMTAB_DIE (decl);
24479 if (!die)
24480 return;
24482 dname = dwarf2_name (name, 0);
24483 if (!dname)
24484 return;
24486 attr = get_AT (die, DW_AT_name);
24487 if (attr)
24489 struct indirect_string_node *node;
24491 node = find_AT_string (dname);
24492 /* replace the string. */
24493 attr->dw_attr_val.v.val_str = node;
24496 else
24497 add_name_attribute (die, dname);
24500 /* True if before or during processing of the first function being emitted. */
24501 static bool in_first_function_p = true;
24502 /* True if loc_note during dwarf2out_var_location call might still be
24503 before first real instruction at address equal to .Ltext0. */
24504 static bool maybe_at_text_label_p = true;
24505 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
24506 static unsigned int first_loclabel_num_not_at_text_label;
24508 /* Called by the final INSN scan whenever we see a var location. We
24509 use it to drop labels in the right places, and throw the location in
24510 our lookup table. */
24512 static void
24513 dwarf2out_var_location (rtx_insn *loc_note)
24515 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24516 struct var_loc_node *newloc;
24517 rtx_insn *next_real, *next_note;
24518 rtx_insn *call_insn = NULL;
24519 static const char *last_label;
24520 static const char *last_postcall_label;
24521 static bool last_in_cold_section_p;
24522 static rtx_insn *expected_next_loc_note;
24523 tree decl;
24524 bool var_loc_p;
24526 if (!NOTE_P (loc_note))
24528 if (CALL_P (loc_note))
24530 call_site_count++;
24531 if (SIBLING_CALL_P (loc_note))
24532 tail_call_site_count++;
24533 if (optimize == 0 && !flag_var_tracking)
24535 /* When the var-tracking pass is not running, there is no note
24536 for indirect calls whose target is compile-time known. In this
24537 case, process such calls specifically so that we generate call
24538 sites for them anyway. */
24539 rtx x = PATTERN (loc_note);
24540 if (GET_CODE (x) == PARALLEL)
24541 x = XVECEXP (x, 0, 0);
24542 if (GET_CODE (x) == SET)
24543 x = SET_SRC (x);
24544 if (GET_CODE (x) == CALL)
24545 x = XEXP (x, 0);
24546 if (!MEM_P (x)
24547 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24548 || !SYMBOL_REF_DECL (XEXP (x, 0))
24549 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24550 != FUNCTION_DECL))
24552 call_insn = loc_note;
24553 loc_note = NULL;
24554 var_loc_p = false;
24556 next_real = next_real_insn (call_insn);
24557 next_note = NULL;
24558 cached_next_real_insn = NULL;
24559 goto create_label;
24563 return;
24566 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24567 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24568 return;
24570 /* Optimize processing a large consecutive sequence of location
24571 notes so we don't spend too much time in next_real_insn. If the
24572 next insn is another location note, remember the next_real_insn
24573 calculation for next time. */
24574 next_real = cached_next_real_insn;
24575 if (next_real)
24577 if (expected_next_loc_note != loc_note)
24578 next_real = NULL;
24581 next_note = NEXT_INSN (loc_note);
24582 if (! next_note
24583 || next_note->deleted ()
24584 || ! NOTE_P (next_note)
24585 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24586 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24587 next_note = NULL;
24589 if (! next_real)
24590 next_real = next_real_insn (loc_note);
24592 if (next_note)
24594 expected_next_loc_note = next_note;
24595 cached_next_real_insn = next_real;
24597 else
24598 cached_next_real_insn = NULL;
24600 /* If there are no instructions which would be affected by this note,
24601 don't do anything. */
24602 if (var_loc_p
24603 && next_real == NULL_RTX
24604 && !NOTE_DURING_CALL_P (loc_note))
24605 return;
24607 create_label:
24609 if (next_real == NULL_RTX)
24610 next_real = get_last_insn ();
24612 /* If there were any real insns between note we processed last time
24613 and this note (or if it is the first note), clear
24614 last_{,postcall_}label so that they are not reused this time. */
24615 if (last_var_location_insn == NULL_RTX
24616 || last_var_location_insn != next_real
24617 || last_in_cold_section_p != in_cold_section_p)
24619 last_label = NULL;
24620 last_postcall_label = NULL;
24623 if (var_loc_p)
24625 decl = NOTE_VAR_LOCATION_DECL (loc_note);
24626 newloc = add_var_loc_to_decl (decl, loc_note,
24627 NOTE_DURING_CALL_P (loc_note)
24628 ? last_postcall_label : last_label);
24629 if (newloc == NULL)
24630 return;
24632 else
24634 decl = NULL_TREE;
24635 newloc = NULL;
24638 /* If there were no real insns between note we processed last time
24639 and this note, use the label we emitted last time. Otherwise
24640 create a new label and emit it. */
24641 if (last_label == NULL)
24643 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24644 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24645 loclabel_num++;
24646 last_label = ggc_strdup (loclabel);
24647 /* See if loclabel might be equal to .Ltext0. If yes,
24648 bump first_loclabel_num_not_at_text_label. */
24649 if (!have_multiple_function_sections
24650 && in_first_function_p
24651 && maybe_at_text_label_p)
24653 static rtx_insn *last_start;
24654 rtx_insn *insn;
24655 for (insn = loc_note; insn; insn = previous_insn (insn))
24656 if (insn == last_start)
24657 break;
24658 else if (!NONDEBUG_INSN_P (insn))
24659 continue;
24660 else
24662 rtx body = PATTERN (insn);
24663 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24664 continue;
24665 /* Inline asm could occupy zero bytes. */
24666 else if (GET_CODE (body) == ASM_INPUT
24667 || asm_noperands (body) >= 0)
24668 continue;
24669 #ifdef HAVE_attr_length
24670 else if (get_attr_min_length (insn) == 0)
24671 continue;
24672 #endif
24673 else
24675 /* Assume insn has non-zero length. */
24676 maybe_at_text_label_p = false;
24677 break;
24680 if (maybe_at_text_label_p)
24682 last_start = loc_note;
24683 first_loclabel_num_not_at_text_label = loclabel_num;
24688 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24689 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24691 if (!var_loc_p)
24693 struct call_arg_loc_node *ca_loc
24694 = ggc_cleared_alloc<call_arg_loc_node> ();
24695 rtx_insn *prev
24696 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24698 ca_loc->call_arg_loc_note = loc_note;
24699 ca_loc->next = NULL;
24700 ca_loc->label = last_label;
24701 gcc_assert (prev
24702 && (CALL_P (prev)
24703 || (NONJUMP_INSN_P (prev)
24704 && GET_CODE (PATTERN (prev)) == SEQUENCE
24705 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24706 if (!CALL_P (prev))
24707 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24708 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24710 /* Look for a SYMBOL_REF in the "prev" instruction. */
24711 rtx x = get_call_rtx_from (PATTERN (prev));
24712 if (x)
24714 /* Try to get the call symbol, if any. */
24715 if (MEM_P (XEXP (x, 0)))
24716 x = XEXP (x, 0);
24717 /* First, look for a memory access to a symbol_ref. */
24718 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24719 && SYMBOL_REF_DECL (XEXP (x, 0))
24720 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24721 ca_loc->symbol_ref = XEXP (x, 0);
24722 /* Otherwise, look at a compile-time known user-level function
24723 declaration. */
24724 else if (MEM_P (x)
24725 && MEM_EXPR (x)
24726 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24727 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24730 ca_loc->block = insn_scope (prev);
24731 if (call_arg_locations)
24732 call_arg_loc_last->next = ca_loc;
24733 else
24734 call_arg_locations = ca_loc;
24735 call_arg_loc_last = ca_loc;
24737 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24738 newloc->label = last_label;
24739 else
24741 if (!last_postcall_label)
24743 sprintf (loclabel, "%s-1", last_label);
24744 last_postcall_label = ggc_strdup (loclabel);
24746 newloc->label = last_postcall_label;
24749 last_var_location_insn = next_real;
24750 last_in_cold_section_p = in_cold_section_p;
24753 /* Called from finalize_size_functions for size functions so that their body
24754 can be encoded in the debug info to describe the layout of variable-length
24755 structures. */
24757 static void
24758 dwarf2out_size_function (tree decl)
24760 function_to_dwarf_procedure (decl);
24763 /* Note in one location list that text section has changed. */
24766 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24768 var_loc_list *list = *slot;
24769 if (list->first)
24770 list->last_before_switch
24771 = list->last->next ? list->last->next : list->last;
24772 return 1;
24775 /* Note in all location lists that text section has changed. */
24777 static void
24778 var_location_switch_text_section (void)
24780 if (decl_loc_table == NULL)
24781 return;
24783 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24786 /* Create a new line number table. */
24788 static dw_line_info_table *
24789 new_line_info_table (void)
24791 dw_line_info_table *table;
24793 table = ggc_cleared_alloc<dw_line_info_table> ();
24794 table->file_num = 1;
24795 table->line_num = 1;
24796 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24798 return table;
24801 /* Lookup the "current" table into which we emit line info, so
24802 that we don't have to do it for every source line. */
24804 static void
24805 set_cur_line_info_table (section *sec)
24807 dw_line_info_table *table;
24809 if (sec == text_section)
24810 table = text_section_line_info;
24811 else if (sec == cold_text_section)
24813 table = cold_text_section_line_info;
24814 if (!table)
24816 cold_text_section_line_info = table = new_line_info_table ();
24817 table->end_label = cold_end_label;
24820 else
24822 const char *end_label;
24824 if (flag_reorder_blocks_and_partition)
24826 if (in_cold_section_p)
24827 end_label = crtl->subsections.cold_section_end_label;
24828 else
24829 end_label = crtl->subsections.hot_section_end_label;
24831 else
24833 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24834 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24835 current_function_funcdef_no);
24836 end_label = ggc_strdup (label);
24839 table = new_line_info_table ();
24840 table->end_label = end_label;
24842 vec_safe_push (separate_line_info, table);
24845 if (DWARF2_ASM_LINE_DEBUG_INFO)
24846 table->is_stmt = (cur_line_info_table
24847 ? cur_line_info_table->is_stmt
24848 : DWARF_LINE_DEFAULT_IS_STMT_START);
24849 cur_line_info_table = table;
24853 /* We need to reset the locations at the beginning of each
24854 function. We can't do this in the end_function hook, because the
24855 declarations that use the locations won't have been output when
24856 that hook is called. Also compute have_multiple_function_sections here. */
24858 static void
24859 dwarf2out_begin_function (tree fun)
24861 section *sec = function_section (fun);
24863 if (sec != text_section)
24864 have_multiple_function_sections = true;
24866 if (flag_reorder_blocks_and_partition && !cold_text_section)
24868 gcc_assert (current_function_decl == fun);
24869 cold_text_section = unlikely_text_section ();
24870 switch_to_section (cold_text_section);
24871 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24872 switch_to_section (sec);
24875 dwarf2out_note_section_used ();
24876 call_site_count = 0;
24877 tail_call_site_count = 0;
24879 set_cur_line_info_table (sec);
24882 /* Helper function of dwarf2out_end_function, called only after emitting
24883 the very first function into assembly. Check if some .debug_loc range
24884 might end with a .LVL* label that could be equal to .Ltext0.
24885 In that case we must force using absolute addresses in .debug_loc ranges,
24886 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24887 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24888 list terminator.
24889 Set have_multiple_function_sections to true in that case and
24890 terminate htab traversal. */
24893 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24895 var_loc_list *entry = *slot;
24896 struct var_loc_node *node;
24898 node = entry->first;
24899 if (node && node->next && node->next->label)
24901 unsigned int i;
24902 const char *label = node->next->label;
24903 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24905 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24907 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24908 if (strcmp (label, loclabel) == 0)
24910 have_multiple_function_sections = true;
24911 return 0;
24915 return 1;
24918 /* Hook called after emitting a function into assembly.
24919 This does something only for the very first function emitted. */
24921 static void
24922 dwarf2out_end_function (unsigned int)
24924 if (in_first_function_p
24925 && !have_multiple_function_sections
24926 && first_loclabel_num_not_at_text_label
24927 && decl_loc_table)
24928 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24929 in_first_function_p = false;
24930 maybe_at_text_label_p = false;
24933 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
24934 front-ends register a translation unit even before dwarf2out_init is
24935 called. */
24936 static tree main_translation_unit = NULL_TREE;
24938 /* Hook called by front-ends after they built their main translation unit.
24939 Associate comp_unit_die to UNIT. */
24941 static void
24942 dwarf2out_register_main_translation_unit (tree unit)
24944 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24945 && main_translation_unit == NULL_TREE);
24946 main_translation_unit = unit;
24947 /* If dwarf2out_init has not been called yet, it will perform the association
24948 itself looking at main_translation_unit. */
24949 if (decl_die_table != NULL)
24950 equate_decl_number_to_die (unit, comp_unit_die ());
24953 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
24955 static void
24956 push_dw_line_info_entry (dw_line_info_table *table,
24957 enum dw_line_info_opcode opcode, unsigned int val)
24959 dw_line_info_entry e;
24960 e.opcode = opcode;
24961 e.val = val;
24962 vec_safe_push (table->entries, e);
24965 /* Output a label to mark the beginning of a source code line entry
24966 and record information relating to this source line, in
24967 'line_info_table' for later output of the .debug_line section. */
24968 /* ??? The discriminator parameter ought to be unsigned. */
24970 static void
24971 dwarf2out_source_line (unsigned int line, const char *filename,
24972 int discriminator, bool is_stmt)
24974 unsigned int file_num;
24975 dw_line_info_table *table;
24977 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
24978 return;
24980 /* The discriminator column was added in dwarf4. Simplify the below
24981 by simply removing it if we're not supposed to output it. */
24982 if (dwarf_version < 4 && dwarf_strict)
24983 discriminator = 0;
24985 table = cur_line_info_table;
24986 file_num = maybe_emit_file (lookup_filename (filename));
24988 /* ??? TODO: Elide duplicate line number entries. Traditionally,
24989 the debugger has used the second (possibly duplicate) line number
24990 at the beginning of the function to mark the end of the prologue.
24991 We could eliminate any other duplicates within the function. For
24992 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
24993 that second line number entry. */
24994 /* Recall that this end-of-prologue indication is *not* the same thing
24995 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
24996 to which the hook corresponds, follows the last insn that was
24997 emitted by gen_prologue. What we need is to precede the first insn
24998 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
24999 insn that corresponds to something the user wrote. These may be
25000 very different locations once scheduling is enabled. */
25002 if (0 && file_num == table->file_num
25003 && line == table->line_num
25004 && discriminator == table->discrim_num
25005 && is_stmt == table->is_stmt)
25006 return;
25008 switch_to_section (current_function_section ());
25010 /* If requested, emit something human-readable. */
25011 if (flag_debug_asm)
25012 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
25014 if (DWARF2_ASM_LINE_DEBUG_INFO)
25016 /* Emit the .loc directive understood by GNU as. */
25017 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
25018 file_num, line, is_stmt, discriminator */
25019 fputs ("\t.loc ", asm_out_file);
25020 fprint_ul (asm_out_file, file_num);
25021 putc (' ', asm_out_file);
25022 fprint_ul (asm_out_file, line);
25023 putc (' ', asm_out_file);
25024 putc ('0', asm_out_file);
25026 if (is_stmt != table->is_stmt)
25028 fputs (" is_stmt ", asm_out_file);
25029 putc (is_stmt ? '1' : '0', asm_out_file);
25031 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
25033 gcc_assert (discriminator > 0);
25034 fputs (" discriminator ", asm_out_file);
25035 fprint_ul (asm_out_file, (unsigned long) discriminator);
25037 putc ('\n', asm_out_file);
25039 else
25041 unsigned int label_num = ++line_info_label_num;
25043 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
25045 push_dw_line_info_entry (table, LI_set_address, label_num);
25046 if (file_num != table->file_num)
25047 push_dw_line_info_entry (table, LI_set_file, file_num);
25048 if (discriminator != table->discrim_num)
25049 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
25050 if (is_stmt != table->is_stmt)
25051 push_dw_line_info_entry (table, LI_negate_stmt, 0);
25052 push_dw_line_info_entry (table, LI_set_line, line);
25055 table->file_num = file_num;
25056 table->line_num = line;
25057 table->discrim_num = discriminator;
25058 table->is_stmt = is_stmt;
25059 table->in_use = true;
25062 /* Record the beginning of a new source file. */
25064 static void
25065 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
25067 if (flag_eliminate_dwarf2_dups)
25069 /* Record the beginning of the file for break_out_includes. */
25070 dw_die_ref bincl_die;
25072 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
25073 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
25076 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25078 macinfo_entry e;
25079 e.code = DW_MACINFO_start_file;
25080 e.lineno = lineno;
25081 e.info = ggc_strdup (filename);
25082 vec_safe_push (macinfo_table, e);
25086 /* Record the end of a source file. */
25088 static void
25089 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
25091 if (flag_eliminate_dwarf2_dups)
25092 /* Record the end of the file for break_out_includes. */
25093 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
25095 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25097 macinfo_entry e;
25098 e.code = DW_MACINFO_end_file;
25099 e.lineno = lineno;
25100 e.info = NULL;
25101 vec_safe_push (macinfo_table, e);
25105 /* Called from debug_define in toplev.c. The `buffer' parameter contains
25106 the tail part of the directive line, i.e. the part which is past the
25107 initial whitespace, #, whitespace, directive-name, whitespace part. */
25109 static void
25110 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
25111 const char *buffer ATTRIBUTE_UNUSED)
25113 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25115 macinfo_entry e;
25116 /* Insert a dummy first entry to be able to optimize the whole
25117 predefined macro block using DW_MACRO_GNU_transparent_include. */
25118 if (macinfo_table->is_empty () && lineno <= 1)
25120 e.code = 0;
25121 e.lineno = 0;
25122 e.info = NULL;
25123 vec_safe_push (macinfo_table, e);
25125 e.code = DW_MACINFO_define;
25126 e.lineno = lineno;
25127 e.info = ggc_strdup (buffer);
25128 vec_safe_push (macinfo_table, e);
25132 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
25133 the tail part of the directive line, i.e. the part which is past the
25134 initial whitespace, #, whitespace, directive-name, whitespace part. */
25136 static void
25137 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
25138 const char *buffer ATTRIBUTE_UNUSED)
25140 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25142 macinfo_entry e;
25143 /* Insert a dummy first entry to be able to optimize the whole
25144 predefined macro block using DW_MACRO_GNU_transparent_include. */
25145 if (macinfo_table->is_empty () && lineno <= 1)
25147 e.code = 0;
25148 e.lineno = 0;
25149 e.info = NULL;
25150 vec_safe_push (macinfo_table, e);
25152 e.code = DW_MACINFO_undef;
25153 e.lineno = lineno;
25154 e.info = ggc_strdup (buffer);
25155 vec_safe_push (macinfo_table, e);
25159 /* Helpers to manipulate hash table of CUs. */
25161 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
25163 static inline hashval_t hash (const macinfo_entry *);
25164 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
25167 inline hashval_t
25168 macinfo_entry_hasher::hash (const macinfo_entry *entry)
25170 return htab_hash_string (entry->info);
25173 inline bool
25174 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
25175 const macinfo_entry *entry2)
25177 return !strcmp (entry1->info, entry2->info);
25180 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
25182 /* Output a single .debug_macinfo entry. */
25184 static void
25185 output_macinfo_op (macinfo_entry *ref)
25187 int file_num;
25188 size_t len;
25189 struct indirect_string_node *node;
25190 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25191 struct dwarf_file_data *fd;
25193 switch (ref->code)
25195 case DW_MACINFO_start_file:
25196 fd = lookup_filename (ref->info);
25197 file_num = maybe_emit_file (fd);
25198 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
25199 dw2_asm_output_data_uleb128 (ref->lineno,
25200 "Included from line number %lu",
25201 (unsigned long) ref->lineno);
25202 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
25203 break;
25204 case DW_MACINFO_end_file:
25205 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
25206 break;
25207 case DW_MACINFO_define:
25208 case DW_MACINFO_undef:
25209 len = strlen (ref->info) + 1;
25210 if (!dwarf_strict
25211 && len > DWARF_OFFSET_SIZE
25212 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25213 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25215 ref->code = ref->code == DW_MACINFO_define
25216 ? DW_MACRO_GNU_define_indirect
25217 : DW_MACRO_GNU_undef_indirect;
25218 output_macinfo_op (ref);
25219 return;
25221 dw2_asm_output_data (1, ref->code,
25222 ref->code == DW_MACINFO_define
25223 ? "Define macro" : "Undefine macro");
25224 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25225 (unsigned long) ref->lineno);
25226 dw2_asm_output_nstring (ref->info, -1, "The macro");
25227 break;
25228 case DW_MACRO_GNU_define_indirect:
25229 case DW_MACRO_GNU_undef_indirect:
25230 node = find_AT_string (ref->info);
25231 gcc_assert (node
25232 && ((node->form == DW_FORM_strp)
25233 || (node->form == DW_FORM_GNU_str_index)));
25234 dw2_asm_output_data (1, ref->code,
25235 ref->code == DW_MACRO_GNU_define_indirect
25236 ? "Define macro indirect"
25237 : "Undefine macro indirect");
25238 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25239 (unsigned long) ref->lineno);
25240 if (node->form == DW_FORM_strp)
25241 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
25242 debug_str_section, "The macro: \"%s\"",
25243 ref->info);
25244 else
25245 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
25246 ref->info);
25247 break;
25248 case DW_MACRO_GNU_transparent_include:
25249 dw2_asm_output_data (1, ref->code, "Transparent include");
25250 ASM_GENERATE_INTERNAL_LABEL (label,
25251 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
25252 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
25253 break;
25254 default:
25255 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
25256 ASM_COMMENT_START, (unsigned long) ref->code);
25257 break;
25261 /* Attempt to make a sequence of define/undef macinfo ops shareable with
25262 other compilation unit .debug_macinfo sections. IDX is the first
25263 index of a define/undef, return the number of ops that should be
25264 emitted in a comdat .debug_macinfo section and emit
25265 a DW_MACRO_GNU_transparent_include entry referencing it.
25266 If the define/undef entry should be emitted normally, return 0. */
25268 static unsigned
25269 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25270 macinfo_hash_type **macinfo_htab)
25272 macinfo_entry *first, *second, *cur, *inc;
25273 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25274 unsigned char checksum[16];
25275 struct md5_ctx ctx;
25276 char *grp_name, *tail;
25277 const char *base;
25278 unsigned int i, count, encoded_filename_len, linebuf_len;
25279 macinfo_entry **slot;
25281 first = &(*macinfo_table)[idx];
25282 second = &(*macinfo_table)[idx + 1];
25284 /* Optimize only if there are at least two consecutive define/undef ops,
25285 and either all of them are before first DW_MACINFO_start_file
25286 with lineno {0,1} (i.e. predefined macro block), or all of them are
25287 in some included header file. */
25288 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25289 return 0;
25290 if (vec_safe_is_empty (files))
25292 if (first->lineno > 1 || second->lineno > 1)
25293 return 0;
25295 else if (first->lineno == 0)
25296 return 0;
25298 /* Find the last define/undef entry that can be grouped together
25299 with first and at the same time compute md5 checksum of their
25300 codes, linenumbers and strings. */
25301 md5_init_ctx (&ctx);
25302 for (i = idx; macinfo_table->iterate (i, &cur); i++)
25303 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25304 break;
25305 else if (vec_safe_is_empty (files) && cur->lineno > 1)
25306 break;
25307 else
25309 unsigned char code = cur->code;
25310 md5_process_bytes (&code, 1, &ctx);
25311 checksum_uleb128 (cur->lineno, &ctx);
25312 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25314 md5_finish_ctx (&ctx, checksum);
25315 count = i - idx;
25317 /* From the containing include filename (if any) pick up just
25318 usable characters from its basename. */
25319 if (vec_safe_is_empty (files))
25320 base = "";
25321 else
25322 base = lbasename (files->last ().info);
25323 for (encoded_filename_len = 0, i = 0; base[i]; i++)
25324 if (ISIDNUM (base[i]) || base[i] == '.')
25325 encoded_filename_len++;
25326 /* Count . at the end. */
25327 if (encoded_filename_len)
25328 encoded_filename_len++;
25330 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25331 linebuf_len = strlen (linebuf);
25333 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
25334 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25335 + 16 * 2 + 1);
25336 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25337 tail = grp_name + 4;
25338 if (encoded_filename_len)
25340 for (i = 0; base[i]; i++)
25341 if (ISIDNUM (base[i]) || base[i] == '.')
25342 *tail++ = base[i];
25343 *tail++ = '.';
25345 memcpy (tail, linebuf, linebuf_len);
25346 tail += linebuf_len;
25347 *tail++ = '.';
25348 for (i = 0; i < 16; i++)
25349 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25351 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25352 in the empty vector entry before the first define/undef. */
25353 inc = &(*macinfo_table)[idx - 1];
25354 inc->code = DW_MACRO_GNU_transparent_include;
25355 inc->lineno = 0;
25356 inc->info = ggc_strdup (grp_name);
25357 if (!*macinfo_htab)
25358 *macinfo_htab = new macinfo_hash_type (10);
25359 /* Avoid emitting duplicates. */
25360 slot = (*macinfo_htab)->find_slot (inc, INSERT);
25361 if (*slot != NULL)
25363 inc->code = 0;
25364 inc->info = NULL;
25365 /* If such an entry has been used before, just emit
25366 a DW_MACRO_GNU_transparent_include op. */
25367 inc = *slot;
25368 output_macinfo_op (inc);
25369 /* And clear all macinfo_entry in the range to avoid emitting them
25370 in the second pass. */
25371 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25373 cur->code = 0;
25374 cur->info = NULL;
25377 else
25379 *slot = inc;
25380 inc->lineno = (*macinfo_htab)->elements ();
25381 output_macinfo_op (inc);
25383 return count;
25386 /* Save any strings needed by the macinfo table in the debug str
25387 table. All strings must be collected into the table by the time
25388 index_string is called. */
25390 static void
25391 save_macinfo_strings (void)
25393 unsigned len;
25394 unsigned i;
25395 macinfo_entry *ref;
25397 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25399 switch (ref->code)
25401 /* Match the logic in output_macinfo_op to decide on
25402 indirect strings. */
25403 case DW_MACINFO_define:
25404 case DW_MACINFO_undef:
25405 len = strlen (ref->info) + 1;
25406 if (!dwarf_strict
25407 && len > DWARF_OFFSET_SIZE
25408 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25409 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25410 set_indirect_string (find_AT_string (ref->info));
25411 break;
25412 case DW_MACRO_GNU_define_indirect:
25413 case DW_MACRO_GNU_undef_indirect:
25414 set_indirect_string (find_AT_string (ref->info));
25415 break;
25416 default:
25417 break;
25422 /* Output macinfo section(s). */
25424 static void
25425 output_macinfo (void)
25427 unsigned i;
25428 unsigned long length = vec_safe_length (macinfo_table);
25429 macinfo_entry *ref;
25430 vec<macinfo_entry, va_gc> *files = NULL;
25431 macinfo_hash_type *macinfo_htab = NULL;
25433 if (! length)
25434 return;
25436 /* output_macinfo* uses these interchangeably. */
25437 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25438 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25439 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25440 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25442 /* For .debug_macro emit the section header. */
25443 if (!dwarf_strict)
25445 dw2_asm_output_data (2, 4, "DWARF macro version number");
25446 if (DWARF_OFFSET_SIZE == 8)
25447 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25448 else
25449 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25450 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25451 (!dwarf_split_debug_info ? debug_line_section_label
25452 : debug_skeleton_line_section_label),
25453 debug_line_section, NULL);
25456 /* In the first loop, it emits the primary .debug_macinfo section
25457 and after each emitted op the macinfo_entry is cleared.
25458 If a longer range of define/undef ops can be optimized using
25459 DW_MACRO_GNU_transparent_include, the
25460 DW_MACRO_GNU_transparent_include op is emitted and kept in
25461 the vector before the first define/undef in the range and the
25462 whole range of define/undef ops is not emitted and kept. */
25463 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25465 switch (ref->code)
25467 case DW_MACINFO_start_file:
25468 vec_safe_push (files, *ref);
25469 break;
25470 case DW_MACINFO_end_file:
25471 if (!vec_safe_is_empty (files))
25472 files->pop ();
25473 break;
25474 case DW_MACINFO_define:
25475 case DW_MACINFO_undef:
25476 if (!dwarf_strict
25477 && HAVE_COMDAT_GROUP
25478 && vec_safe_length (files) != 1
25479 && i > 0
25480 && i + 1 < length
25481 && (*macinfo_table)[i - 1].code == 0)
25483 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25484 if (count)
25486 i += count - 1;
25487 continue;
25490 break;
25491 case 0:
25492 /* A dummy entry may be inserted at the beginning to be able
25493 to optimize the whole block of predefined macros. */
25494 if (i == 0)
25495 continue;
25496 default:
25497 break;
25499 output_macinfo_op (ref);
25500 ref->info = NULL;
25501 ref->code = 0;
25504 if (!macinfo_htab)
25505 return;
25507 delete macinfo_htab;
25508 macinfo_htab = NULL;
25510 /* If any DW_MACRO_GNU_transparent_include were used, on those
25511 DW_MACRO_GNU_transparent_include entries terminate the
25512 current chain and switch to a new comdat .debug_macinfo
25513 section and emit the define/undef entries within it. */
25514 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25515 switch (ref->code)
25517 case 0:
25518 continue;
25519 case DW_MACRO_GNU_transparent_include:
25521 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25522 tree comdat_key = get_identifier (ref->info);
25523 /* Terminate the previous .debug_macinfo section. */
25524 dw2_asm_output_data (1, 0, "End compilation unit");
25525 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
25526 SECTION_DEBUG
25527 | SECTION_LINKONCE,
25528 comdat_key);
25529 ASM_GENERATE_INTERNAL_LABEL (label,
25530 DEBUG_MACRO_SECTION_LABEL,
25531 ref->lineno);
25532 ASM_OUTPUT_LABEL (asm_out_file, label);
25533 ref->code = 0;
25534 ref->info = NULL;
25535 dw2_asm_output_data (2, 4, "DWARF macro version number");
25536 if (DWARF_OFFSET_SIZE == 8)
25537 dw2_asm_output_data (1, 1, "Flags: 64-bit");
25538 else
25539 dw2_asm_output_data (1, 0, "Flags: 32-bit");
25541 break;
25542 case DW_MACINFO_define:
25543 case DW_MACINFO_undef:
25544 output_macinfo_op (ref);
25545 ref->code = 0;
25546 ref->info = NULL;
25547 break;
25548 default:
25549 gcc_unreachable ();
25553 /* Set up for Dwarf output at the start of compilation. */
25555 static void
25556 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25558 /* This option is currently broken, see (PR53118 and PR46102). */
25559 if (flag_eliminate_dwarf2_dups
25560 && strstr (lang_hooks.name, "C++"))
25562 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25563 flag_eliminate_dwarf2_dups = 0;
25566 /* Allocate the file_table. */
25567 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25569 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25570 /* Allocate the decl_die_table. */
25571 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25573 /* Allocate the decl_loc_table. */
25574 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25576 /* Allocate the cached_dw_loc_list_table. */
25577 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25579 /* Allocate the initial hunk of the decl_scope_table. */
25580 vec_alloc (decl_scope_table, 256);
25582 /* Allocate the initial hunk of the abbrev_die_table. */
25583 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25584 (ABBREV_DIE_TABLE_INCREMENT);
25585 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25586 /* Zero-th entry is allocated, but unused. */
25587 abbrev_die_table_in_use = 1;
25589 /* Allocate the dwarf_proc_stack_usage_map. */
25590 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25592 /* Allocate the pubtypes and pubnames vectors. */
25593 vec_alloc (pubname_table, 32);
25594 vec_alloc (pubtype_table, 32);
25596 vec_alloc (incomplete_types, 64);
25598 vec_alloc (used_rtx_array, 32);
25600 if (!dwarf_split_debug_info)
25602 debug_info_section = get_section (DEBUG_INFO_SECTION,
25603 SECTION_DEBUG, NULL);
25604 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25605 SECTION_DEBUG, NULL);
25606 debug_loc_section = get_section (DEBUG_LOC_SECTION,
25607 SECTION_DEBUG, NULL);
25609 else
25611 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25612 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25613 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25614 SECTION_DEBUG | SECTION_EXCLUDE,
25615 NULL);
25616 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25617 SECTION_DEBUG, NULL);
25618 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25619 SECTION_DEBUG, NULL);
25620 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25621 SECTION_DEBUG, NULL);
25622 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25623 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25625 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25626 the main .o, but the skeleton_line goes into the split off dwo. */
25627 debug_skeleton_line_section
25628 = get_section (DEBUG_DWO_LINE_SECTION,
25629 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25630 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25631 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25632 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
25633 SECTION_DEBUG | SECTION_EXCLUDE,
25634 NULL);
25635 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25636 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25637 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25638 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25639 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25640 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25642 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25643 SECTION_DEBUG, NULL);
25644 debug_macinfo_section = get_section (dwarf_strict
25645 ? DEBUG_MACINFO_SECTION
25646 : DEBUG_MACRO_SECTION,
25647 DEBUG_MACRO_SECTION_FLAGS, NULL);
25648 debug_line_section = get_section (DEBUG_LINE_SECTION,
25649 SECTION_DEBUG, NULL);
25650 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25651 SECTION_DEBUG, NULL);
25652 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25653 SECTION_DEBUG, NULL);
25654 debug_str_section = get_section (DEBUG_STR_SECTION,
25655 DEBUG_STR_SECTION_FLAGS, NULL);
25656 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25657 SECTION_DEBUG, NULL);
25658 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25659 SECTION_DEBUG, NULL);
25661 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25662 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25663 DEBUG_ABBREV_SECTION_LABEL, 0);
25664 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25665 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25666 COLD_TEXT_SECTION_LABEL, 0);
25667 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25669 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25670 DEBUG_INFO_SECTION_LABEL, 0);
25671 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25672 DEBUG_LINE_SECTION_LABEL, 0);
25673 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25674 DEBUG_RANGES_SECTION_LABEL, 0);
25675 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25676 DEBUG_ADDR_SECTION_LABEL, 0);
25677 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25678 dwarf_strict
25679 ? DEBUG_MACINFO_SECTION_LABEL
25680 : DEBUG_MACRO_SECTION_LABEL, 0);
25681 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25683 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25684 vec_alloc (macinfo_table, 64);
25686 switch_to_section (text_section);
25687 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25688 #endif
25690 /* Make sure the line number table for .text always exists. */
25691 text_section_line_info = new_line_info_table ();
25692 text_section_line_info->end_label = text_end_label;
25694 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25695 cur_line_info_table = text_section_line_info;
25696 #endif
25698 /* If front-ends already registered a main translation unit but we were not
25699 ready to perform the association, do this now. */
25700 if (main_translation_unit != NULL_TREE)
25701 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25704 /* Called before compile () starts outputtting functions, variables
25705 and toplevel asms into assembly. */
25707 static void
25708 dwarf2out_assembly_start (void)
25710 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25711 && dwarf2out_do_cfi_asm ()
25712 && (!(flag_unwind_tables || flag_exceptions)
25713 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25714 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25717 /* A helper function for dwarf2out_finish called through
25718 htab_traverse. Assign a string its index. All strings must be
25719 collected into the table by the time index_string is called,
25720 because the indexing code relies on htab_traverse to traverse nodes
25721 in the same order for each run. */
25724 index_string (indirect_string_node **h, unsigned int *index)
25726 indirect_string_node *node = *h;
25728 find_string_form (node);
25729 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25731 gcc_assert (node->index == NO_INDEX_ASSIGNED);
25732 node->index = *index;
25733 *index += 1;
25735 return 1;
25738 /* A helper function for output_indirect_strings called through
25739 htab_traverse. Output the offset to a string and update the
25740 current offset. */
25743 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25745 indirect_string_node *node = *h;
25747 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25749 /* Assert that this node has been assigned an index. */
25750 gcc_assert (node->index != NO_INDEX_ASSIGNED
25751 && node->index != NOT_INDEXED);
25752 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25753 "indexed string 0x%x: %s", node->index, node->str);
25754 *offset += strlen (node->str) + 1;
25756 return 1;
25759 /* A helper function for dwarf2out_finish called through
25760 htab_traverse. Output the indexed string. */
25763 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25765 struct indirect_string_node *node = *h;
25767 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25769 /* Assert that the strings are output in the same order as their
25770 indexes were assigned. */
25771 gcc_assert (*cur_idx == node->index);
25772 assemble_string (node->str, strlen (node->str) + 1);
25773 *cur_idx += 1;
25775 return 1;
25778 /* A helper function for dwarf2out_finish called through
25779 htab_traverse. Emit one queued .debug_str string. */
25782 output_indirect_string (indirect_string_node **h, void *)
25784 struct indirect_string_node *node = *h;
25786 node->form = find_string_form (node);
25787 if (node->form == DW_FORM_strp && node->refcount > 0)
25789 ASM_OUTPUT_LABEL (asm_out_file, node->label);
25790 assemble_string (node->str, strlen (node->str) + 1);
25793 return 1;
25796 /* Output the indexed string table. */
25798 static void
25799 output_indirect_strings (void)
25801 switch_to_section (debug_str_section);
25802 if (!dwarf_split_debug_info)
25803 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25804 else
25806 unsigned int offset = 0;
25807 unsigned int cur_idx = 0;
25809 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25811 switch_to_section (debug_str_offsets_section);
25812 debug_str_hash->traverse_noresize
25813 <unsigned int *, output_index_string_offset> (&offset);
25814 switch_to_section (debug_str_dwo_section);
25815 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25816 (&cur_idx);
25820 /* Callback for htab_traverse to assign an index to an entry in the
25821 table, and to write that entry to the .debug_addr section. */
25824 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25826 addr_table_entry *entry = *slot;
25828 if (entry->refcount == 0)
25830 gcc_assert (entry->index == NO_INDEX_ASSIGNED
25831 || entry->index == NOT_INDEXED);
25832 return 1;
25835 gcc_assert (entry->index == *cur_index);
25836 (*cur_index)++;
25838 switch (entry->kind)
25840 case ate_kind_rtx:
25841 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25842 "0x%x", entry->index);
25843 break;
25844 case ate_kind_rtx_dtprel:
25845 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25846 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25847 DWARF2_ADDR_SIZE,
25848 entry->addr.rtl);
25849 fputc ('\n', asm_out_file);
25850 break;
25851 case ate_kind_label:
25852 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25853 "0x%x", entry->index);
25854 break;
25855 default:
25856 gcc_unreachable ();
25858 return 1;
25861 /* Produce the .debug_addr section. */
25863 static void
25864 output_addr_table (void)
25866 unsigned int index = 0;
25867 if (addr_index_table == NULL || addr_index_table->size () == 0)
25868 return;
25870 switch_to_section (debug_addr_section);
25871 addr_index_table
25872 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25875 #if ENABLE_ASSERT_CHECKING
25876 /* Verify that all marks are clear. */
25878 static void
25879 verify_marks_clear (dw_die_ref die)
25881 dw_die_ref c;
25883 gcc_assert (! die->die_mark);
25884 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25886 #endif /* ENABLE_ASSERT_CHECKING */
25888 /* Clear the marks for a die and its children.
25889 Be cool if the mark isn't set. */
25891 static void
25892 prune_unmark_dies (dw_die_ref die)
25894 dw_die_ref c;
25896 if (die->die_mark)
25897 die->die_mark = 0;
25898 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25901 /* Given LOC that is referenced by a DIE we're marking as used, find all
25902 referenced DWARF procedures it references and mark them as used. */
25904 static void
25905 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25907 for (; loc != NULL; loc = loc->dw_loc_next)
25908 switch (loc->dw_loc_opc)
25910 case DW_OP_GNU_implicit_pointer:
25911 case DW_OP_GNU_convert:
25912 case DW_OP_GNU_reinterpret:
25913 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25914 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25915 break;
25916 case DW_OP_call2:
25917 case DW_OP_call4:
25918 case DW_OP_call_ref:
25919 case DW_OP_GNU_const_type:
25920 case DW_OP_GNU_parameter_ref:
25921 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
25922 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25923 break;
25924 case DW_OP_GNU_regval_type:
25925 case DW_OP_GNU_deref_type:
25926 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
25927 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
25928 break;
25929 case DW_OP_GNU_entry_value:
25930 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
25931 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
25932 break;
25933 default:
25934 break;
25938 /* Given DIE that we're marking as used, find any other dies
25939 it references as attributes and mark them as used. */
25941 static void
25942 prune_unused_types_walk_attribs (dw_die_ref die)
25944 dw_attr_node *a;
25945 unsigned ix;
25947 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25949 switch (AT_class (a))
25951 /* Make sure DWARF procedures referenced by location descriptions will
25952 get emitted. */
25953 case dw_val_class_loc:
25954 prune_unused_types_walk_loc_descr (AT_loc (a));
25955 break;
25956 case dw_val_class_loc_list:
25957 for (dw_loc_list_ref list = AT_loc_list (a);
25958 list != NULL;
25959 list = list->dw_loc_next)
25960 prune_unused_types_walk_loc_descr (list->expr);
25961 break;
25963 case dw_val_class_die_ref:
25964 /* A reference to another DIE.
25965 Make sure that it will get emitted.
25966 If it was broken out into a comdat group, don't follow it. */
25967 if (! AT_ref (a)->comdat_type_p
25968 || a->dw_attr == DW_AT_specification)
25969 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
25970 break;
25972 case dw_val_class_str:
25973 /* Set the string's refcount to 0 so that prune_unused_types_mark
25974 accounts properly for it. */
25975 a->dw_attr_val.v.val_str->refcount = 0;
25976 break;
25978 default:
25979 break;
25984 /* Mark the generic parameters and arguments children DIEs of DIE. */
25986 static void
25987 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
25989 dw_die_ref c;
25991 if (die == NULL || die->die_child == NULL)
25992 return;
25993 c = die->die_child;
25996 if (is_template_parameter (c))
25997 prune_unused_types_mark (c, 1);
25998 c = c->die_sib;
25999 } while (c && c != die->die_child);
26002 /* Mark DIE as being used. If DOKIDS is true, then walk down
26003 to DIE's children. */
26005 static void
26006 prune_unused_types_mark (dw_die_ref die, int dokids)
26008 dw_die_ref c;
26010 if (die->die_mark == 0)
26012 /* We haven't done this node yet. Mark it as used. */
26013 die->die_mark = 1;
26014 /* If this is the DIE of a generic type instantiation,
26015 mark the children DIEs that describe its generic parms and
26016 args. */
26017 prune_unused_types_mark_generic_parms_dies (die);
26019 /* We also have to mark its parents as used.
26020 (But we don't want to mark our parent's kids due to this,
26021 unless it is a class.) */
26022 if (die->die_parent)
26023 prune_unused_types_mark (die->die_parent,
26024 class_scope_p (die->die_parent));
26026 /* Mark any referenced nodes. */
26027 prune_unused_types_walk_attribs (die);
26029 /* If this node is a specification,
26030 also mark the definition, if it exists. */
26031 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
26032 prune_unused_types_mark (die->die_definition, 1);
26035 if (dokids && die->die_mark != 2)
26037 /* We need to walk the children, but haven't done so yet.
26038 Remember that we've walked the kids. */
26039 die->die_mark = 2;
26041 /* If this is an array type, we need to make sure our
26042 kids get marked, even if they're types. If we're
26043 breaking out types into comdat sections, do this
26044 for all type definitions. */
26045 if (die->die_tag == DW_TAG_array_type
26046 || (use_debug_types
26047 && is_type_die (die) && ! is_declaration_die (die)))
26048 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
26049 else
26050 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26054 /* For local classes, look if any static member functions were emitted
26055 and if so, mark them. */
26057 static void
26058 prune_unused_types_walk_local_classes (dw_die_ref die)
26060 dw_die_ref c;
26062 if (die->die_mark == 2)
26063 return;
26065 switch (die->die_tag)
26067 case DW_TAG_structure_type:
26068 case DW_TAG_union_type:
26069 case DW_TAG_class_type:
26070 break;
26072 case DW_TAG_subprogram:
26073 if (!get_AT_flag (die, DW_AT_declaration)
26074 || die->die_definition != NULL)
26075 prune_unused_types_mark (die, 1);
26076 return;
26078 default:
26079 return;
26082 /* Mark children. */
26083 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
26086 /* Walk the tree DIE and mark types that we actually use. */
26088 static void
26089 prune_unused_types_walk (dw_die_ref die)
26091 dw_die_ref c;
26093 /* Don't do anything if this node is already marked and
26094 children have been marked as well. */
26095 if (die->die_mark == 2)
26096 return;
26098 switch (die->die_tag)
26100 case DW_TAG_structure_type:
26101 case DW_TAG_union_type:
26102 case DW_TAG_class_type:
26103 if (die->die_perennial_p)
26104 break;
26106 for (c = die->die_parent; c; c = c->die_parent)
26107 if (c->die_tag == DW_TAG_subprogram)
26108 break;
26110 /* Finding used static member functions inside of classes
26111 is needed just for local classes, because for other classes
26112 static member function DIEs with DW_AT_specification
26113 are emitted outside of the DW_TAG_*_type. If we ever change
26114 it, we'd need to call this even for non-local classes. */
26115 if (c)
26116 prune_unused_types_walk_local_classes (die);
26118 /* It's a type node --- don't mark it. */
26119 return;
26121 case DW_TAG_const_type:
26122 case DW_TAG_packed_type:
26123 case DW_TAG_pointer_type:
26124 case DW_TAG_reference_type:
26125 case DW_TAG_rvalue_reference_type:
26126 case DW_TAG_volatile_type:
26127 case DW_TAG_typedef:
26128 case DW_TAG_array_type:
26129 case DW_TAG_interface_type:
26130 case DW_TAG_friend:
26131 case DW_TAG_enumeration_type:
26132 case DW_TAG_subroutine_type:
26133 case DW_TAG_string_type:
26134 case DW_TAG_set_type:
26135 case DW_TAG_subrange_type:
26136 case DW_TAG_ptr_to_member_type:
26137 case DW_TAG_file_type:
26138 /* Type nodes are useful only when other DIEs reference them --- don't
26139 mark them. */
26140 /* FALLTHROUGH */
26142 case DW_TAG_dwarf_procedure:
26143 /* Likewise for DWARF procedures. */
26145 if (die->die_perennial_p)
26146 break;
26148 return;
26150 default:
26151 /* Mark everything else. */
26152 break;
26155 if (die->die_mark == 0)
26157 die->die_mark = 1;
26159 /* Now, mark any dies referenced from here. */
26160 prune_unused_types_walk_attribs (die);
26163 die->die_mark = 2;
26165 /* Mark children. */
26166 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26169 /* Increment the string counts on strings referred to from DIE's
26170 attributes. */
26172 static void
26173 prune_unused_types_update_strings (dw_die_ref die)
26175 dw_attr_node *a;
26176 unsigned ix;
26178 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26179 if (AT_class (a) == dw_val_class_str)
26181 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
26182 s->refcount++;
26183 /* Avoid unnecessarily putting strings that are used less than
26184 twice in the hash table. */
26185 if (s->refcount
26186 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
26188 indirect_string_node **slot
26189 = debug_str_hash->find_slot_with_hash (s->str,
26190 htab_hash_string (s->str),
26191 INSERT);
26192 gcc_assert (*slot == NULL);
26193 *slot = s;
26198 /* Remove from the tree DIE any dies that aren't marked. */
26200 static void
26201 prune_unused_types_prune (dw_die_ref die)
26203 dw_die_ref c;
26205 gcc_assert (die->die_mark);
26206 prune_unused_types_update_strings (die);
26208 if (! die->die_child)
26209 return;
26211 c = die->die_child;
26212 do {
26213 dw_die_ref prev = c, next;
26214 for (c = c->die_sib; ! c->die_mark; c = next)
26215 if (c == die->die_child)
26217 /* No marked children between 'prev' and the end of the list. */
26218 if (prev == c)
26219 /* No marked children at all. */
26220 die->die_child = NULL;
26221 else
26223 prev->die_sib = c->die_sib;
26224 die->die_child = prev;
26226 c->die_sib = NULL;
26227 return;
26229 else
26231 next = c->die_sib;
26232 c->die_sib = NULL;
26235 if (c != prev->die_sib)
26236 prev->die_sib = c;
26237 prune_unused_types_prune (c);
26238 } while (c != die->die_child);
26241 /* Remove dies representing declarations that we never use. */
26243 static void
26244 prune_unused_types (void)
26246 unsigned int i;
26247 limbo_die_node *node;
26248 comdat_type_node *ctnode;
26249 pubname_entry *pub;
26250 dw_die_ref base_type;
26252 #if ENABLE_ASSERT_CHECKING
26253 /* All the marks should already be clear. */
26254 verify_marks_clear (comp_unit_die ());
26255 for (node = limbo_die_list; node; node = node->next)
26256 verify_marks_clear (node->die);
26257 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26258 verify_marks_clear (ctnode->root_die);
26259 #endif /* ENABLE_ASSERT_CHECKING */
26261 /* Mark types that are used in global variables. */
26262 premark_types_used_by_global_vars ();
26264 /* Set the mark on nodes that are actually used. */
26265 prune_unused_types_walk (comp_unit_die ());
26266 for (node = limbo_die_list; node; node = node->next)
26267 prune_unused_types_walk (node->die);
26268 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26270 prune_unused_types_walk (ctnode->root_die);
26271 prune_unused_types_mark (ctnode->type_die, 1);
26274 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
26275 are unusual in that they are pubnames that are the children of pubtypes.
26276 They should only be marked via their parent DW_TAG_enumeration_type die,
26277 not as roots in themselves. */
26278 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26279 if (pub->die->die_tag != DW_TAG_enumerator)
26280 prune_unused_types_mark (pub->die, 1);
26281 for (i = 0; base_types.iterate (i, &base_type); i++)
26282 prune_unused_types_mark (base_type, 1);
26284 if (debug_str_hash)
26285 debug_str_hash->empty ();
26286 if (skeleton_debug_str_hash)
26287 skeleton_debug_str_hash->empty ();
26288 prune_unused_types_prune (comp_unit_die ());
26289 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26291 node = *pnode;
26292 if (!node->die->die_mark)
26293 *pnode = node->next;
26294 else
26296 prune_unused_types_prune (node->die);
26297 pnode = &node->next;
26300 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26301 prune_unused_types_prune (ctnode->root_die);
26303 /* Leave the marks clear. */
26304 prune_unmark_dies (comp_unit_die ());
26305 for (node = limbo_die_list; node; node = node->next)
26306 prune_unmark_dies (node->die);
26307 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26308 prune_unmark_dies (ctnode->root_die);
26311 /* Set the parameter to true if there are any relative pathnames in
26312 the file table. */
26314 file_table_relative_p (dwarf_file_data **slot, bool *p)
26316 struct dwarf_file_data *d = *slot;
26317 if (!IS_ABSOLUTE_PATH (d->filename))
26319 *p = true;
26320 return 0;
26322 return 1;
26325 /* Helpers to manipulate hash table of comdat type units. */
26327 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26329 static inline hashval_t hash (const comdat_type_node *);
26330 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26333 inline hashval_t
26334 comdat_type_hasher::hash (const comdat_type_node *type_node)
26336 hashval_t h;
26337 memcpy (&h, type_node->signature, sizeof (h));
26338 return h;
26341 inline bool
26342 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26343 const comdat_type_node *type_node_2)
26345 return (! memcmp (type_node_1->signature, type_node_2->signature,
26346 DWARF_TYPE_SIGNATURE_SIZE));
26349 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26350 to the location it would have been added, should we know its
26351 DECL_ASSEMBLER_NAME when we added other attributes. This will
26352 probably improve compactness of debug info, removing equivalent
26353 abbrevs, and hide any differences caused by deferring the
26354 computation of the assembler name, triggered by e.g. PCH. */
26356 static inline void
26357 move_linkage_attr (dw_die_ref die)
26359 unsigned ix = vec_safe_length (die->die_attr);
26360 dw_attr_node linkage = (*die->die_attr)[ix - 1];
26362 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26363 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26365 while (--ix > 0)
26367 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26369 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26370 break;
26373 if (ix != vec_safe_length (die->die_attr) - 1)
26375 die->die_attr->pop ();
26376 die->die_attr->quick_insert (ix, linkage);
26380 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26381 referenced from typed stack ops and count how often they are used. */
26383 static void
26384 mark_base_types (dw_loc_descr_ref loc)
26386 dw_die_ref base_type = NULL;
26388 for (; loc; loc = loc->dw_loc_next)
26390 switch (loc->dw_loc_opc)
26392 case DW_OP_GNU_regval_type:
26393 case DW_OP_GNU_deref_type:
26394 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26395 break;
26396 case DW_OP_GNU_convert:
26397 case DW_OP_GNU_reinterpret:
26398 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26399 continue;
26400 /* FALLTHRU */
26401 case DW_OP_GNU_const_type:
26402 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26403 break;
26404 case DW_OP_GNU_entry_value:
26405 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26406 continue;
26407 default:
26408 continue;
26410 gcc_assert (base_type->die_parent == comp_unit_die ());
26411 if (base_type->die_mark)
26412 base_type->die_mark++;
26413 else
26415 base_types.safe_push (base_type);
26416 base_type->die_mark = 1;
26421 /* Comparison function for sorting marked base types. */
26423 static int
26424 base_type_cmp (const void *x, const void *y)
26426 dw_die_ref dx = *(const dw_die_ref *) x;
26427 dw_die_ref dy = *(const dw_die_ref *) y;
26428 unsigned int byte_size1, byte_size2;
26429 unsigned int encoding1, encoding2;
26430 if (dx->die_mark > dy->die_mark)
26431 return -1;
26432 if (dx->die_mark < dy->die_mark)
26433 return 1;
26434 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26435 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26436 if (byte_size1 < byte_size2)
26437 return 1;
26438 if (byte_size1 > byte_size2)
26439 return -1;
26440 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26441 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26442 if (encoding1 < encoding2)
26443 return 1;
26444 if (encoding1 > encoding2)
26445 return -1;
26446 return 0;
26449 /* Move base types marked by mark_base_types as early as possible
26450 in the CU, sorted by decreasing usage count both to make the
26451 uleb128 references as small as possible and to make sure they
26452 will have die_offset already computed by calc_die_sizes when
26453 sizes of typed stack loc ops is computed. */
26455 static void
26456 move_marked_base_types (void)
26458 unsigned int i;
26459 dw_die_ref base_type, die, c;
26461 if (base_types.is_empty ())
26462 return;
26464 /* Sort by decreasing usage count, they will be added again in that
26465 order later on. */
26466 base_types.qsort (base_type_cmp);
26467 die = comp_unit_die ();
26468 c = die->die_child;
26471 dw_die_ref prev = c;
26472 c = c->die_sib;
26473 while (c->die_mark)
26475 remove_child_with_prev (c, prev);
26476 /* As base types got marked, there must be at least
26477 one node other than DW_TAG_base_type. */
26478 gcc_assert (die->die_child != NULL);
26479 c = prev->die_sib;
26482 while (c != die->die_child);
26483 gcc_assert (die->die_child);
26484 c = die->die_child;
26485 for (i = 0; base_types.iterate (i, &base_type); i++)
26487 base_type->die_mark = 0;
26488 base_type->die_sib = c->die_sib;
26489 c->die_sib = base_type;
26490 c = base_type;
26494 /* Helper function for resolve_addr, attempt to resolve
26495 one CONST_STRING, return true if successful. Similarly verify that
26496 SYMBOL_REFs refer to variables emitted in the current CU. */
26498 static bool
26499 resolve_one_addr (rtx *addr)
26501 rtx rtl = *addr;
26503 if (GET_CODE (rtl) == CONST_STRING)
26505 size_t len = strlen (XSTR (rtl, 0)) + 1;
26506 tree t = build_string (len, XSTR (rtl, 0));
26507 tree tlen = size_int (len - 1);
26508 TREE_TYPE (t)
26509 = build_array_type (char_type_node, build_index_type (tlen));
26510 rtl = lookup_constant_def (t);
26511 if (!rtl || !MEM_P (rtl))
26512 return false;
26513 rtl = XEXP (rtl, 0);
26514 if (GET_CODE (rtl) == SYMBOL_REF
26515 && SYMBOL_REF_DECL (rtl)
26516 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26517 return false;
26518 vec_safe_push (used_rtx_array, rtl);
26519 *addr = rtl;
26520 return true;
26523 if (GET_CODE (rtl) == SYMBOL_REF
26524 && SYMBOL_REF_DECL (rtl))
26526 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26528 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26529 return false;
26531 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26532 return false;
26535 if (GET_CODE (rtl) == CONST)
26537 subrtx_ptr_iterator::array_type array;
26538 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26539 if (!resolve_one_addr (*iter))
26540 return false;
26543 return true;
26546 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26547 if possible, and create DW_TAG_dwarf_procedure that can be referenced
26548 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
26550 static rtx
26551 string_cst_pool_decl (tree t)
26553 rtx rtl = output_constant_def (t, 1);
26554 unsigned char *array;
26555 dw_loc_descr_ref l;
26556 tree decl;
26557 size_t len;
26558 dw_die_ref ref;
26560 if (!rtl || !MEM_P (rtl))
26561 return NULL_RTX;
26562 rtl = XEXP (rtl, 0);
26563 if (GET_CODE (rtl) != SYMBOL_REF
26564 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26565 return NULL_RTX;
26567 decl = SYMBOL_REF_DECL (rtl);
26568 if (!lookup_decl_die (decl))
26570 len = TREE_STRING_LENGTH (t);
26571 vec_safe_push (used_rtx_array, rtl);
26572 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26573 array = ggc_vec_alloc<unsigned char> (len);
26574 memcpy (array, TREE_STRING_POINTER (t), len);
26575 l = new_loc_descr (DW_OP_implicit_value, len, 0);
26576 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26577 l->dw_loc_oprnd2.v.val_vec.length = len;
26578 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26579 l->dw_loc_oprnd2.v.val_vec.array = array;
26580 add_AT_loc (ref, DW_AT_location, l);
26581 equate_decl_number_to_die (decl, ref);
26583 return rtl;
26586 /* Helper function of resolve_addr_in_expr. LOC is
26587 a DW_OP_addr followed by DW_OP_stack_value, either at the start
26588 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26589 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
26590 with DW_OP_GNU_implicit_pointer if possible
26591 and return true, if unsuccessful, return false. */
26593 static bool
26594 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26596 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26597 HOST_WIDE_INT offset = 0;
26598 dw_die_ref ref = NULL;
26599 tree decl;
26601 if (GET_CODE (rtl) == CONST
26602 && GET_CODE (XEXP (rtl, 0)) == PLUS
26603 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26605 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26606 rtl = XEXP (XEXP (rtl, 0), 0);
26608 if (GET_CODE (rtl) == CONST_STRING)
26610 size_t len = strlen (XSTR (rtl, 0)) + 1;
26611 tree t = build_string (len, XSTR (rtl, 0));
26612 tree tlen = size_int (len - 1);
26614 TREE_TYPE (t)
26615 = build_array_type (char_type_node, build_index_type (tlen));
26616 rtl = string_cst_pool_decl (t);
26617 if (!rtl)
26618 return false;
26620 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26622 decl = SYMBOL_REF_DECL (rtl);
26623 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
26625 ref = lookup_decl_die (decl);
26626 if (ref && (get_AT (ref, DW_AT_location)
26627 || get_AT (ref, DW_AT_const_value)))
26629 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26630 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26631 loc->dw_loc_oprnd1.val_entry = NULL;
26632 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26633 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26634 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26635 loc->dw_loc_oprnd2.v.val_int = offset;
26636 return true;
26640 return false;
26643 /* Helper function for resolve_addr, handle one location
26644 expression, return false if at least one CONST_STRING or SYMBOL_REF in
26645 the location list couldn't be resolved. */
26647 static bool
26648 resolve_addr_in_expr (dw_loc_descr_ref loc)
26650 dw_loc_descr_ref keep = NULL;
26651 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26652 switch (loc->dw_loc_opc)
26654 case DW_OP_addr:
26655 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26657 if ((prev == NULL
26658 || prev->dw_loc_opc == DW_OP_piece
26659 || prev->dw_loc_opc == DW_OP_bit_piece)
26660 && loc->dw_loc_next
26661 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26662 && !dwarf_strict
26663 && optimize_one_addr_into_implicit_ptr (loc))
26664 break;
26665 return false;
26667 break;
26668 case DW_OP_GNU_addr_index:
26669 case DW_OP_GNU_const_index:
26670 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26671 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26673 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26674 if (!resolve_one_addr (&rtl))
26675 return false;
26676 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26677 loc->dw_loc_oprnd1.val_entry =
26678 add_addr_table_entry (rtl, ate_kind_rtx);
26680 break;
26681 case DW_OP_const4u:
26682 case DW_OP_const8u:
26683 if (loc->dtprel
26684 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26685 return false;
26686 break;
26687 case DW_OP_plus_uconst:
26688 if (size_of_loc_descr (loc)
26689 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26691 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26693 dw_loc_descr_ref repl
26694 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26695 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26696 add_loc_descr (&repl, loc->dw_loc_next);
26697 *loc = *repl;
26699 break;
26700 case DW_OP_implicit_value:
26701 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26702 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26703 return false;
26704 break;
26705 case DW_OP_GNU_implicit_pointer:
26706 case DW_OP_GNU_parameter_ref:
26707 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26709 dw_die_ref ref
26710 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26711 if (ref == NULL)
26712 return false;
26713 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26714 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26715 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26717 break;
26718 case DW_OP_GNU_const_type:
26719 case DW_OP_GNU_regval_type:
26720 case DW_OP_GNU_deref_type:
26721 case DW_OP_GNU_convert:
26722 case DW_OP_GNU_reinterpret:
26723 while (loc->dw_loc_next
26724 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26726 dw_die_ref base1, base2;
26727 unsigned enc1, enc2, size1, size2;
26728 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26729 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26730 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26731 else if (loc->dw_loc_oprnd1.val_class
26732 == dw_val_class_unsigned_const)
26733 break;
26734 else
26735 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26736 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26737 == dw_val_class_unsigned_const)
26738 break;
26739 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26740 gcc_assert (base1->die_tag == DW_TAG_base_type
26741 && base2->die_tag == DW_TAG_base_type);
26742 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26743 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26744 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26745 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26746 if (size1 == size2
26747 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26748 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26749 && loc != keep)
26750 || enc1 == enc2))
26752 /* Optimize away next DW_OP_GNU_convert after
26753 adjusting LOC's base type die reference. */
26754 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26755 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26756 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26757 else
26758 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26759 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26760 continue;
26762 /* Don't change integer DW_OP_GNU_convert after e.g. floating
26763 point typed stack entry. */
26764 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26765 keep = loc->dw_loc_next;
26766 break;
26768 break;
26769 default:
26770 break;
26772 return true;
26775 /* Helper function of resolve_addr. DIE had DW_AT_location of
26776 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26777 and DW_OP_addr couldn't be resolved. resolve_addr has already
26778 removed the DW_AT_location attribute. This function attempts to
26779 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26780 to it or DW_AT_const_value attribute, if possible. */
26782 static void
26783 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26785 if (TREE_CODE (decl) != VAR_DECL
26786 || lookup_decl_die (decl) != die
26787 || DECL_EXTERNAL (decl)
26788 || !TREE_STATIC (decl)
26789 || DECL_INITIAL (decl) == NULL_TREE
26790 || DECL_P (DECL_INITIAL (decl))
26791 || get_AT (die, DW_AT_const_value))
26792 return;
26794 tree init = DECL_INITIAL (decl);
26795 HOST_WIDE_INT offset = 0;
26796 /* For variables that have been optimized away and thus
26797 don't have a memory location, see if we can emit
26798 DW_AT_const_value instead. */
26799 if (tree_add_const_value_attribute (die, init))
26800 return;
26801 if (dwarf_strict)
26802 return;
26803 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26804 and ADDR_EXPR refers to a decl that has DW_AT_location or
26805 DW_AT_const_value (but isn't addressable, otherwise
26806 resolving the original DW_OP_addr wouldn't fail), see if
26807 we can add DW_OP_GNU_implicit_pointer. */
26808 STRIP_NOPS (init);
26809 if (TREE_CODE (init) == POINTER_PLUS_EXPR
26810 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26812 offset = tree_to_shwi (TREE_OPERAND (init, 1));
26813 init = TREE_OPERAND (init, 0);
26814 STRIP_NOPS (init);
26816 if (TREE_CODE (init) != ADDR_EXPR)
26817 return;
26818 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26819 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26820 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26821 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26822 && TREE_OPERAND (init, 0) != decl))
26824 dw_die_ref ref;
26825 dw_loc_descr_ref l;
26827 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26829 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26830 if (!rtl)
26831 return;
26832 decl = SYMBOL_REF_DECL (rtl);
26834 else
26835 decl = TREE_OPERAND (init, 0);
26836 ref = lookup_decl_die (decl);
26837 if (ref == NULL
26838 || (!get_AT (ref, DW_AT_location)
26839 && !get_AT (ref, DW_AT_const_value)))
26840 return;
26841 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26842 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26843 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26844 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26845 add_AT_loc (die, DW_AT_location, l);
26849 /* Return NULL if l is a DWARF expression, or first op that is not
26850 valid DWARF expression. */
26852 static dw_loc_descr_ref
26853 non_dwarf_expression (dw_loc_descr_ref l)
26855 while (l)
26857 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26858 return l;
26859 switch (l->dw_loc_opc)
26861 case DW_OP_regx:
26862 case DW_OP_implicit_value:
26863 case DW_OP_stack_value:
26864 case DW_OP_GNU_implicit_pointer:
26865 case DW_OP_GNU_parameter_ref:
26866 case DW_OP_piece:
26867 case DW_OP_bit_piece:
26868 return l;
26869 default:
26870 break;
26872 l = l->dw_loc_next;
26874 return NULL;
26877 /* Return adjusted copy of EXPR:
26878 If it is empty DWARF expression, return it.
26879 If it is valid non-empty DWARF expression,
26880 return copy of EXPR with copy of DEREF appended to it.
26881 If it is DWARF expression followed by DW_OP_reg{N,x}, return
26882 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
26883 and no DEREF.
26884 If it is DWARF expression followed by DW_OP_stack_value, return
26885 copy of the DWARF expression without anything appended.
26886 Otherwise, return NULL. */
26888 static dw_loc_descr_ref
26889 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
26892 if (expr == NULL)
26893 return NULL;
26895 dw_loc_descr_ref l = non_dwarf_expression (expr);
26896 if (l && l->dw_loc_next)
26897 return NULL;
26899 if (l)
26901 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26902 deref = new_loc_descr ((enum dwarf_location_atom)
26903 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
26904 0, 0);
26905 else
26906 switch (l->dw_loc_opc)
26908 case DW_OP_regx:
26909 deref = new_loc_descr (DW_OP_bregx,
26910 l->dw_loc_oprnd1.v.val_unsigned, 0);
26911 break;
26912 case DW_OP_stack_value:
26913 deref = NULL;
26914 break;
26915 default:
26916 return NULL;
26919 else
26920 deref = new_loc_descr (deref->dw_loc_opc,
26921 deref->dw_loc_oprnd1.v.val_int, 0);
26923 dw_loc_descr_ref ret = NULL, *p = &ret;
26924 while (expr != l)
26926 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
26927 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
26928 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
26929 p = &(*p)->dw_loc_next;
26930 expr = expr->dw_loc_next;
26932 *p = deref;
26933 return ret;
26936 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
26937 or argument, adjust it if needed and return:
26938 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
26939 attribute if present should be removed
26940 0 keep the attribute as is if the referenced var or argument has
26941 only DWARF expression that covers all ranges
26942 1 if the attribute has been successfully adjusted. */
26944 static int
26945 optimize_string_length (dw_attr_node *a)
26947 dw_loc_descr_ref l = AT_loc (a), lv;
26948 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
26949 dw_attr_node *av = get_AT (die, DW_AT_location);
26950 dw_loc_list_ref d;
26951 bool non_dwarf_expr = false;
26953 if (av == NULL)
26954 return -1;
26955 switch (AT_class (av))
26957 case dw_val_class_loc_list:
26958 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26959 if (d->expr && non_dwarf_expression (d->expr))
26960 non_dwarf_expr = true;
26961 break;
26962 case dw_val_class_loc:
26963 lv = AT_loc (av);
26964 if (lv == NULL)
26965 return -1;
26966 if (non_dwarf_expression (lv))
26967 non_dwarf_expr = true;
26968 break;
26969 default:
26970 return -1;
26973 /* If it is safe to keep DW_OP_call4 in, keep it. */
26974 if (!non_dwarf_expr
26975 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
26976 return 0;
26978 /* If not dereferencing the DW_OP_call4 afterwards, we can just
26979 copy over the DW_AT_location attribute from die to a. */
26980 if (l->dw_loc_next == NULL)
26982 a->dw_attr_val = av->dw_attr_val;
26983 return 1;
26986 dw_loc_list_ref list, *p;
26987 switch (AT_class (av))
26989 case dw_val_class_loc_list:
26990 p = &list;
26991 list = NULL;
26992 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26994 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
26995 if (lv)
26997 *p = new_loc_list (lv, d->begin, d->end, d->section);
26998 p = &(*p)->dw_loc_next;
27001 if (list == NULL)
27002 return -1;
27003 a->dw_attr_val.val_class = dw_val_class_loc_list;
27004 gen_llsym (list);
27005 *AT_loc_list_ptr (a) = list;
27006 return 1;
27007 case dw_val_class_loc:
27008 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
27009 if (lv == NULL)
27010 return -1;
27011 a->dw_attr_val.v.val_loc = lv;
27012 return 1;
27013 default:
27014 gcc_unreachable ();
27018 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
27019 an address in .rodata section if the string literal is emitted there,
27020 or remove the containing location list or replace DW_AT_const_value
27021 with DW_AT_location and empty location expression, if it isn't found
27022 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
27023 to something that has been emitted in the current CU. */
27025 static void
27026 resolve_addr (dw_die_ref die)
27028 dw_die_ref c;
27029 dw_attr_node *a;
27030 dw_loc_list_ref *curr, *start, loc;
27031 unsigned ix;
27032 bool remove_AT_byte_size = false;
27034 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27035 switch (AT_class (a))
27037 case dw_val_class_loc_list:
27038 start = curr = AT_loc_list_ptr (a);
27039 loc = *curr;
27040 gcc_assert (loc);
27041 /* The same list can be referenced more than once. See if we have
27042 already recorded the result from a previous pass. */
27043 if (loc->replaced)
27044 *curr = loc->dw_loc_next;
27045 else if (!loc->resolved_addr)
27047 /* As things stand, we do not expect or allow one die to
27048 reference a suffix of another die's location list chain.
27049 References must be identical or completely separate.
27050 There is therefore no need to cache the result of this
27051 pass on any list other than the first; doing so
27052 would lead to unnecessary writes. */
27053 while (*curr)
27055 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
27056 if (!resolve_addr_in_expr ((*curr)->expr))
27058 dw_loc_list_ref next = (*curr)->dw_loc_next;
27059 dw_loc_descr_ref l = (*curr)->expr;
27061 if (next && (*curr)->ll_symbol)
27063 gcc_assert (!next->ll_symbol);
27064 next->ll_symbol = (*curr)->ll_symbol;
27066 if (dwarf_split_debug_info)
27067 remove_loc_list_addr_table_entries (l);
27068 *curr = next;
27070 else
27072 mark_base_types ((*curr)->expr);
27073 curr = &(*curr)->dw_loc_next;
27076 if (loc == *start)
27077 loc->resolved_addr = 1;
27078 else
27080 loc->replaced = 1;
27081 loc->dw_loc_next = *start;
27084 if (!*start)
27086 remove_AT (die, a->dw_attr);
27087 ix--;
27089 break;
27090 case dw_val_class_loc:
27092 dw_loc_descr_ref l = AT_loc (a);
27093 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
27094 DW_AT_string_length is only a rough approximation; unfortunately
27095 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
27096 needs a DWARF expression, while DW_AT_location of the referenced
27097 variable or argument might be any location description. */
27098 if (a->dw_attr == DW_AT_string_length
27099 && l
27100 && l->dw_loc_opc == DW_OP_call4
27101 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
27102 && (l->dw_loc_next == NULL
27103 || (l->dw_loc_next->dw_loc_next == NULL
27104 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
27105 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
27107 switch (optimize_string_length (a))
27109 case -1:
27110 remove_AT (die, a->dw_attr);
27111 ix--;
27112 /* If we drop DW_AT_string_length, we need to drop also
27113 DW_AT_{string_length_,}byte_size. */
27114 remove_AT_byte_size = true;
27115 continue;
27116 default:
27117 break;
27118 case 1:
27119 /* Even if we keep the optimized DW_AT_string_length,
27120 it might have changed AT_class, so process it again. */
27121 ix--;
27122 continue;
27125 /* For -gdwarf-2 don't attempt to optimize
27126 DW_AT_data_member_location containing
27127 DW_OP_plus_uconst - older consumers might
27128 rely on it being that op instead of a more complex,
27129 but shorter, location description. */
27130 if ((dwarf_version > 2
27131 || a->dw_attr != DW_AT_data_member_location
27132 || l == NULL
27133 || l->dw_loc_opc != DW_OP_plus_uconst
27134 || l->dw_loc_next != NULL)
27135 && !resolve_addr_in_expr (l))
27137 if (dwarf_split_debug_info)
27138 remove_loc_list_addr_table_entries (l);
27139 if (l != NULL
27140 && l->dw_loc_next == NULL
27141 && l->dw_loc_opc == DW_OP_addr
27142 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
27143 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
27144 && a->dw_attr == DW_AT_location)
27146 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
27147 remove_AT (die, a->dw_attr);
27148 ix--;
27149 optimize_location_into_implicit_ptr (die, decl);
27150 break;
27152 remove_AT (die, a->dw_attr);
27153 ix--;
27155 else
27156 mark_base_types (l);
27158 break;
27159 case dw_val_class_addr:
27160 if (a->dw_attr == DW_AT_const_value
27161 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
27163 if (AT_index (a) != NOT_INDEXED)
27164 remove_addr_table_entry (a->dw_attr_val.val_entry);
27165 remove_AT (die, a->dw_attr);
27166 ix--;
27168 if (die->die_tag == DW_TAG_GNU_call_site
27169 && a->dw_attr == DW_AT_abstract_origin)
27171 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
27172 dw_die_ref tdie = lookup_decl_die (tdecl);
27173 dw_die_ref cdie;
27174 if (tdie == NULL
27175 && DECL_EXTERNAL (tdecl)
27176 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
27177 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
27179 /* Creating a full DIE for tdecl is overly expensive and
27180 at this point even wrong when in the LTO phase
27181 as it can end up generating new type DIEs we didn't
27182 output and thus optimize_external_refs will crash. */
27183 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
27184 add_AT_flag (tdie, DW_AT_external, 1);
27185 add_AT_flag (tdie, DW_AT_declaration, 1);
27186 add_linkage_attr (tdie, tdecl);
27187 add_name_and_src_coords_attributes (tdie, tdecl);
27188 equate_decl_number_to_die (tdecl, tdie);
27190 if (tdie)
27192 a->dw_attr_val.val_class = dw_val_class_die_ref;
27193 a->dw_attr_val.v.val_die_ref.die = tdie;
27194 a->dw_attr_val.v.val_die_ref.external = 0;
27196 else
27198 if (AT_index (a) != NOT_INDEXED)
27199 remove_addr_table_entry (a->dw_attr_val.val_entry);
27200 remove_AT (die, a->dw_attr);
27201 ix--;
27204 break;
27205 default:
27206 break;
27209 if (remove_AT_byte_size)
27210 remove_AT (die, dwarf_version >= 5
27211 ? DW_AT_string_length_byte_size
27212 : DW_AT_byte_size);
27214 FOR_EACH_CHILD (die, c, resolve_addr (c));
27217 /* Helper routines for optimize_location_lists.
27218 This pass tries to share identical local lists in .debug_loc
27219 section. */
27221 /* Iteratively hash operands of LOC opcode into HSTATE. */
27223 static void
27224 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
27226 dw_val_ref val1 = &loc->dw_loc_oprnd1;
27227 dw_val_ref val2 = &loc->dw_loc_oprnd2;
27229 switch (loc->dw_loc_opc)
27231 case DW_OP_const4u:
27232 case DW_OP_const8u:
27233 if (loc->dtprel)
27234 goto hash_addr;
27235 /* FALLTHRU */
27236 case DW_OP_const1u:
27237 case DW_OP_const1s:
27238 case DW_OP_const2u:
27239 case DW_OP_const2s:
27240 case DW_OP_const4s:
27241 case DW_OP_const8s:
27242 case DW_OP_constu:
27243 case DW_OP_consts:
27244 case DW_OP_pick:
27245 case DW_OP_plus_uconst:
27246 case DW_OP_breg0:
27247 case DW_OP_breg1:
27248 case DW_OP_breg2:
27249 case DW_OP_breg3:
27250 case DW_OP_breg4:
27251 case DW_OP_breg5:
27252 case DW_OP_breg6:
27253 case DW_OP_breg7:
27254 case DW_OP_breg8:
27255 case DW_OP_breg9:
27256 case DW_OP_breg10:
27257 case DW_OP_breg11:
27258 case DW_OP_breg12:
27259 case DW_OP_breg13:
27260 case DW_OP_breg14:
27261 case DW_OP_breg15:
27262 case DW_OP_breg16:
27263 case DW_OP_breg17:
27264 case DW_OP_breg18:
27265 case DW_OP_breg19:
27266 case DW_OP_breg20:
27267 case DW_OP_breg21:
27268 case DW_OP_breg22:
27269 case DW_OP_breg23:
27270 case DW_OP_breg24:
27271 case DW_OP_breg25:
27272 case DW_OP_breg26:
27273 case DW_OP_breg27:
27274 case DW_OP_breg28:
27275 case DW_OP_breg29:
27276 case DW_OP_breg30:
27277 case DW_OP_breg31:
27278 case DW_OP_regx:
27279 case DW_OP_fbreg:
27280 case DW_OP_piece:
27281 case DW_OP_deref_size:
27282 case DW_OP_xderef_size:
27283 hstate.add_object (val1->v.val_int);
27284 break;
27285 case DW_OP_skip:
27286 case DW_OP_bra:
27288 int offset;
27290 gcc_assert (val1->val_class == dw_val_class_loc);
27291 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
27292 hstate.add_object (offset);
27294 break;
27295 case DW_OP_implicit_value:
27296 hstate.add_object (val1->v.val_unsigned);
27297 switch (val2->val_class)
27299 case dw_val_class_const:
27300 hstate.add_object (val2->v.val_int);
27301 break;
27302 case dw_val_class_vec:
27304 unsigned int elt_size = val2->v.val_vec.elt_size;
27305 unsigned int len = val2->v.val_vec.length;
27307 hstate.add_int (elt_size);
27308 hstate.add_int (len);
27309 hstate.add (val2->v.val_vec.array, len * elt_size);
27311 break;
27312 case dw_val_class_const_double:
27313 hstate.add_object (val2->v.val_double.low);
27314 hstate.add_object (val2->v.val_double.high);
27315 break;
27316 case dw_val_class_wide_int:
27317 hstate.add (val2->v.val_wide->get_val (),
27318 get_full_len (*val2->v.val_wide)
27319 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27320 break;
27321 case dw_val_class_addr:
27322 inchash::add_rtx (val2->v.val_addr, hstate);
27323 break;
27324 default:
27325 gcc_unreachable ();
27327 break;
27328 case DW_OP_bregx:
27329 case DW_OP_bit_piece:
27330 hstate.add_object (val1->v.val_int);
27331 hstate.add_object (val2->v.val_int);
27332 break;
27333 case DW_OP_addr:
27334 hash_addr:
27335 if (loc->dtprel)
27337 unsigned char dtprel = 0xd1;
27338 hstate.add_object (dtprel);
27340 inchash::add_rtx (val1->v.val_addr, hstate);
27341 break;
27342 case DW_OP_GNU_addr_index:
27343 case DW_OP_GNU_const_index:
27345 if (loc->dtprel)
27347 unsigned char dtprel = 0xd1;
27348 hstate.add_object (dtprel);
27350 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
27352 break;
27353 case DW_OP_GNU_implicit_pointer:
27354 hstate.add_int (val2->v.val_int);
27355 break;
27356 case DW_OP_GNU_entry_value:
27357 hstate.add_object (val1->v.val_loc);
27358 break;
27359 case DW_OP_GNU_regval_type:
27360 case DW_OP_GNU_deref_type:
27362 unsigned int byte_size
27363 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
27364 unsigned int encoding
27365 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
27366 hstate.add_object (val1->v.val_int);
27367 hstate.add_object (byte_size);
27368 hstate.add_object (encoding);
27370 break;
27371 case DW_OP_GNU_convert:
27372 case DW_OP_GNU_reinterpret:
27373 if (val1->val_class == dw_val_class_unsigned_const)
27375 hstate.add_object (val1->v.val_unsigned);
27376 break;
27378 /* FALLTHRU */
27379 case DW_OP_GNU_const_type:
27381 unsigned int byte_size
27382 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
27383 unsigned int encoding
27384 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
27385 hstate.add_object (byte_size);
27386 hstate.add_object (encoding);
27387 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
27388 break;
27389 hstate.add_object (val2->val_class);
27390 switch (val2->val_class)
27392 case dw_val_class_const:
27393 hstate.add_object (val2->v.val_int);
27394 break;
27395 case dw_val_class_vec:
27397 unsigned int elt_size = val2->v.val_vec.elt_size;
27398 unsigned int len = val2->v.val_vec.length;
27400 hstate.add_object (elt_size);
27401 hstate.add_object (len);
27402 hstate.add (val2->v.val_vec.array, len * elt_size);
27404 break;
27405 case dw_val_class_const_double:
27406 hstate.add_object (val2->v.val_double.low);
27407 hstate.add_object (val2->v.val_double.high);
27408 break;
27409 case dw_val_class_wide_int:
27410 hstate.add (val2->v.val_wide->get_val (),
27411 get_full_len (*val2->v.val_wide)
27412 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27413 break;
27414 default:
27415 gcc_unreachable ();
27418 break;
27420 default:
27421 /* Other codes have no operands. */
27422 break;
27426 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
27428 static inline void
27429 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
27431 dw_loc_descr_ref l;
27432 bool sizes_computed = false;
27433 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
27434 size_of_locs (loc);
27436 for (l = loc; l != NULL; l = l->dw_loc_next)
27438 enum dwarf_location_atom opc = l->dw_loc_opc;
27439 hstate.add_object (opc);
27440 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
27442 size_of_locs (loc);
27443 sizes_computed = true;
27445 hash_loc_operands (l, hstate);
27449 /* Compute hash of the whole location list LIST_HEAD. */
27451 static inline void
27452 hash_loc_list (dw_loc_list_ref list_head)
27454 dw_loc_list_ref curr = list_head;
27455 inchash::hash hstate;
27457 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
27459 hstate.add (curr->begin, strlen (curr->begin) + 1);
27460 hstate.add (curr->end, strlen (curr->end) + 1);
27461 if (curr->section)
27462 hstate.add (curr->section, strlen (curr->section) + 1);
27463 hash_locs (curr->expr, hstate);
27465 list_head->hash = hstate.end ();
27468 /* Return true if X and Y opcodes have the same operands. */
27470 static inline bool
27471 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
27473 dw_val_ref valx1 = &x->dw_loc_oprnd1;
27474 dw_val_ref valx2 = &x->dw_loc_oprnd2;
27475 dw_val_ref valy1 = &y->dw_loc_oprnd1;
27476 dw_val_ref valy2 = &y->dw_loc_oprnd2;
27478 switch (x->dw_loc_opc)
27480 case DW_OP_const4u:
27481 case DW_OP_const8u:
27482 if (x->dtprel)
27483 goto hash_addr;
27484 /* FALLTHRU */
27485 case DW_OP_const1u:
27486 case DW_OP_const1s:
27487 case DW_OP_const2u:
27488 case DW_OP_const2s:
27489 case DW_OP_const4s:
27490 case DW_OP_const8s:
27491 case DW_OP_constu:
27492 case DW_OP_consts:
27493 case DW_OP_pick:
27494 case DW_OP_plus_uconst:
27495 case DW_OP_breg0:
27496 case DW_OP_breg1:
27497 case DW_OP_breg2:
27498 case DW_OP_breg3:
27499 case DW_OP_breg4:
27500 case DW_OP_breg5:
27501 case DW_OP_breg6:
27502 case DW_OP_breg7:
27503 case DW_OP_breg8:
27504 case DW_OP_breg9:
27505 case DW_OP_breg10:
27506 case DW_OP_breg11:
27507 case DW_OP_breg12:
27508 case DW_OP_breg13:
27509 case DW_OP_breg14:
27510 case DW_OP_breg15:
27511 case DW_OP_breg16:
27512 case DW_OP_breg17:
27513 case DW_OP_breg18:
27514 case DW_OP_breg19:
27515 case DW_OP_breg20:
27516 case DW_OP_breg21:
27517 case DW_OP_breg22:
27518 case DW_OP_breg23:
27519 case DW_OP_breg24:
27520 case DW_OP_breg25:
27521 case DW_OP_breg26:
27522 case DW_OP_breg27:
27523 case DW_OP_breg28:
27524 case DW_OP_breg29:
27525 case DW_OP_breg30:
27526 case DW_OP_breg31:
27527 case DW_OP_regx:
27528 case DW_OP_fbreg:
27529 case DW_OP_piece:
27530 case DW_OP_deref_size:
27531 case DW_OP_xderef_size:
27532 return valx1->v.val_int == valy1->v.val_int;
27533 case DW_OP_skip:
27534 case DW_OP_bra:
27535 /* If splitting debug info, the use of DW_OP_GNU_addr_index
27536 can cause irrelevant differences in dw_loc_addr. */
27537 gcc_assert (valx1->val_class == dw_val_class_loc
27538 && valy1->val_class == dw_val_class_loc
27539 && (dwarf_split_debug_info
27540 || x->dw_loc_addr == y->dw_loc_addr));
27541 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27542 case DW_OP_implicit_value:
27543 if (valx1->v.val_unsigned != valy1->v.val_unsigned
27544 || valx2->val_class != valy2->val_class)
27545 return false;
27546 switch (valx2->val_class)
27548 case dw_val_class_const:
27549 return valx2->v.val_int == valy2->v.val_int;
27550 case dw_val_class_vec:
27551 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27552 && valx2->v.val_vec.length == valy2->v.val_vec.length
27553 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27554 valx2->v.val_vec.elt_size
27555 * valx2->v.val_vec.length) == 0;
27556 case dw_val_class_const_double:
27557 return valx2->v.val_double.low == valy2->v.val_double.low
27558 && valx2->v.val_double.high == valy2->v.val_double.high;
27559 case dw_val_class_wide_int:
27560 return *valx2->v.val_wide == *valy2->v.val_wide;
27561 case dw_val_class_addr:
27562 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27563 default:
27564 gcc_unreachable ();
27566 case DW_OP_bregx:
27567 case DW_OP_bit_piece:
27568 return valx1->v.val_int == valy1->v.val_int
27569 && valx2->v.val_int == valy2->v.val_int;
27570 case DW_OP_addr:
27571 hash_addr:
27572 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27573 case DW_OP_GNU_addr_index:
27574 case DW_OP_GNU_const_index:
27576 rtx ax1 = valx1->val_entry->addr.rtl;
27577 rtx ay1 = valy1->val_entry->addr.rtl;
27578 return rtx_equal_p (ax1, ay1);
27580 case DW_OP_GNU_implicit_pointer:
27581 return valx1->val_class == dw_val_class_die_ref
27582 && valx1->val_class == valy1->val_class
27583 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27584 && valx2->v.val_int == valy2->v.val_int;
27585 case DW_OP_GNU_entry_value:
27586 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27587 case DW_OP_GNU_const_type:
27588 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27589 || valx2->val_class != valy2->val_class)
27590 return false;
27591 switch (valx2->val_class)
27593 case dw_val_class_const:
27594 return valx2->v.val_int == valy2->v.val_int;
27595 case dw_val_class_vec:
27596 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27597 && valx2->v.val_vec.length == valy2->v.val_vec.length
27598 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27599 valx2->v.val_vec.elt_size
27600 * valx2->v.val_vec.length) == 0;
27601 case dw_val_class_const_double:
27602 return valx2->v.val_double.low == valy2->v.val_double.low
27603 && valx2->v.val_double.high == valy2->v.val_double.high;
27604 case dw_val_class_wide_int:
27605 return *valx2->v.val_wide == *valy2->v.val_wide;
27606 default:
27607 gcc_unreachable ();
27609 case DW_OP_GNU_regval_type:
27610 case DW_OP_GNU_deref_type:
27611 return valx1->v.val_int == valy1->v.val_int
27612 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27613 case DW_OP_GNU_convert:
27614 case DW_OP_GNU_reinterpret:
27615 if (valx1->val_class != valy1->val_class)
27616 return false;
27617 if (valx1->val_class == dw_val_class_unsigned_const)
27618 return valx1->v.val_unsigned == valy1->v.val_unsigned;
27619 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27620 case DW_OP_GNU_parameter_ref:
27621 return valx1->val_class == dw_val_class_die_ref
27622 && valx1->val_class == valy1->val_class
27623 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27624 default:
27625 /* Other codes have no operands. */
27626 return true;
27630 /* Return true if DWARF location expressions X and Y are the same. */
27632 static inline bool
27633 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27635 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27636 if (x->dw_loc_opc != y->dw_loc_opc
27637 || x->dtprel != y->dtprel
27638 || !compare_loc_operands (x, y))
27639 break;
27640 return x == NULL && y == NULL;
27643 /* Hashtable helpers. */
27645 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27647 static inline hashval_t hash (const dw_loc_list_struct *);
27648 static inline bool equal (const dw_loc_list_struct *,
27649 const dw_loc_list_struct *);
27652 /* Return precomputed hash of location list X. */
27654 inline hashval_t
27655 loc_list_hasher::hash (const dw_loc_list_struct *x)
27657 return x->hash;
27660 /* Return true if location lists A and B are the same. */
27662 inline bool
27663 loc_list_hasher::equal (const dw_loc_list_struct *a,
27664 const dw_loc_list_struct *b)
27666 if (a == b)
27667 return 1;
27668 if (a->hash != b->hash)
27669 return 0;
27670 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27671 if (strcmp (a->begin, b->begin) != 0
27672 || strcmp (a->end, b->end) != 0
27673 || (a->section == NULL) != (b->section == NULL)
27674 || (a->section && strcmp (a->section, b->section) != 0)
27675 || !compare_locs (a->expr, b->expr))
27676 break;
27677 return a == NULL && b == NULL;
27680 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27683 /* Recursively optimize location lists referenced from DIE
27684 children and share them whenever possible. */
27686 static void
27687 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27689 dw_die_ref c;
27690 dw_attr_node *a;
27691 unsigned ix;
27692 dw_loc_list_struct **slot;
27694 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27695 if (AT_class (a) == dw_val_class_loc_list)
27697 dw_loc_list_ref list = AT_loc_list (a);
27698 /* TODO: perform some optimizations here, before hashing
27699 it and storing into the hash table. */
27700 hash_loc_list (list);
27701 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27702 if (*slot == NULL)
27703 *slot = list;
27704 else
27705 a->dw_attr_val.v.val_loc_list = *slot;
27708 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27712 /* Recursively assign each location list a unique index into the debug_addr
27713 section. */
27715 static void
27716 index_location_lists (dw_die_ref die)
27718 dw_die_ref c;
27719 dw_attr_node *a;
27720 unsigned ix;
27722 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27723 if (AT_class (a) == dw_val_class_loc_list)
27725 dw_loc_list_ref list = AT_loc_list (a);
27726 dw_loc_list_ref curr;
27727 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27729 /* Don't index an entry that has already been indexed
27730 or won't be output. */
27731 if (curr->begin_entry != NULL
27732 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27733 continue;
27735 curr->begin_entry
27736 = add_addr_table_entry (xstrdup (curr->begin),
27737 ate_kind_label);
27741 FOR_EACH_CHILD (die, c, index_location_lists (c));
27744 /* Optimize location lists referenced from DIE
27745 children and share them whenever possible. */
27747 static void
27748 optimize_location_lists (dw_die_ref die)
27750 loc_list_hash_type htab (500);
27751 optimize_location_lists_1 (die, &htab);
27754 /* Traverse the limbo die list, and add parent/child links. The only
27755 dies without parents that should be here are concrete instances of
27756 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
27757 For concrete instances, we can get the parent die from the abstract
27758 instance. */
27760 static void
27761 flush_limbo_die_list (void)
27763 limbo_die_node *node;
27765 /* get_context_die calls force_decl_die, which can put new DIEs on the
27766 limbo list in LTO mode when nested functions are put in a different
27767 partition than that of their parent function. */
27768 while ((node = limbo_die_list))
27770 dw_die_ref die = node->die;
27771 limbo_die_list = node->next;
27773 if (die->die_parent == NULL)
27775 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27777 if (origin && origin->die_parent)
27778 add_child_die (origin->die_parent, die);
27779 else if (is_cu_die (die))
27781 else if (seen_error ())
27782 /* It's OK to be confused by errors in the input. */
27783 add_child_die (comp_unit_die (), die);
27784 else
27786 /* In certain situations, the lexical block containing a
27787 nested function can be optimized away, which results
27788 in the nested function die being orphaned. Likewise
27789 with the return type of that nested function. Force
27790 this to be a child of the containing function.
27792 It may happen that even the containing function got fully
27793 inlined and optimized out. In that case we are lost and
27794 assign the empty child. This should not be big issue as
27795 the function is likely unreachable too. */
27796 gcc_assert (node->created_for);
27798 if (DECL_P (node->created_for))
27799 origin = get_context_die (DECL_CONTEXT (node->created_for));
27800 else if (TYPE_P (node->created_for))
27801 origin = scope_die_for (node->created_for, comp_unit_die ());
27802 else
27803 origin = comp_unit_die ();
27805 add_child_die (origin, die);
27811 /* Output stuff that dwarf requires at the end of every file,
27812 and generate the DWARF-2 debugging info. */
27814 static void
27815 dwarf2out_finish (const char *)
27817 comdat_type_node *ctnode;
27818 dw_die_ref main_comp_unit_die;
27820 /* Flush out any latecomers to the limbo party. */
27821 flush_limbo_die_list ();
27823 /* We shouldn't have any symbols with delayed asm names for
27824 DIEs generated after early finish. */
27825 gcc_assert (deferred_asm_name == NULL);
27827 gen_remaining_tmpl_value_param_die_attribute ();
27829 #if ENABLE_ASSERT_CHECKING
27831 dw_die_ref die = comp_unit_die (), c;
27832 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27834 #endif
27835 resolve_addr (comp_unit_die ());
27836 move_marked_base_types ();
27838 if (flag_eliminate_unused_debug_types)
27839 prune_unused_types ();
27841 /* Generate separate COMDAT sections for type DIEs. */
27842 if (use_debug_types)
27844 break_out_comdat_types (comp_unit_die ());
27846 /* Each new type_unit DIE was added to the limbo die list when created.
27847 Since these have all been added to comdat_type_list, clear the
27848 limbo die list. */
27849 limbo_die_list = NULL;
27851 /* For each new comdat type unit, copy declarations for incomplete
27852 types to make the new unit self-contained (i.e., no direct
27853 references to the main compile unit). */
27854 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27855 copy_decls_for_unworthy_types (ctnode->root_die);
27856 copy_decls_for_unworthy_types (comp_unit_die ());
27858 /* In the process of copying declarations from one unit to another,
27859 we may have left some declarations behind that are no longer
27860 referenced. Prune them. */
27861 prune_unused_types ();
27864 /* Generate separate CUs for each of the include files we've seen.
27865 They will go into limbo_die_list. */
27866 if (flag_eliminate_dwarf2_dups)
27867 break_out_includes (comp_unit_die ());
27869 /* Traverse the DIE's and add sibling attributes to those DIE's that
27870 have children. */
27871 add_sibling_attributes (comp_unit_die ());
27872 limbo_die_node *node;
27873 for (node = limbo_die_list; node; node = node->next)
27874 add_sibling_attributes (node->die);
27875 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27876 add_sibling_attributes (ctnode->root_die);
27878 /* When splitting DWARF info, we put some attributes in the
27879 skeleton compile_unit DIE that remains in the .o, while
27880 most attributes go in the DWO compile_unit_die. */
27881 if (dwarf_split_debug_info)
27882 main_comp_unit_die = gen_compile_unit_die (NULL);
27883 else
27884 main_comp_unit_die = comp_unit_die ();
27886 /* Output a terminator label for the .text section. */
27887 switch_to_section (text_section);
27888 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27889 if (cold_text_section)
27891 switch_to_section (cold_text_section);
27892 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27895 /* We can only use the low/high_pc attributes if all of the code was
27896 in .text. */
27897 if (!have_multiple_function_sections
27898 || (dwarf_version < 3 && dwarf_strict))
27900 /* Don't add if the CU has no associated code. */
27901 if (text_section_used)
27902 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27903 text_end_label, true);
27905 else
27907 unsigned fde_idx;
27908 dw_fde_ref fde;
27909 bool range_list_added = false;
27911 if (text_section_used)
27912 add_ranges_by_labels (main_comp_unit_die, text_section_label,
27913 text_end_label, &range_list_added, true);
27914 if (cold_text_section_used)
27915 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27916 cold_end_label, &range_list_added, true);
27918 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27920 if (DECL_IGNORED_P (fde->decl))
27921 continue;
27922 if (!fde->in_std_section)
27923 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27924 fde->dw_fde_end, &range_list_added,
27925 true);
27926 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27927 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27928 fde->dw_fde_second_end, &range_list_added,
27929 true);
27932 if (range_list_added)
27934 /* We need to give .debug_loc and .debug_ranges an appropriate
27935 "base address". Use zero so that these addresses become
27936 absolute. Historically, we've emitted the unexpected
27937 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27938 Emit both to give time for other tools to adapt. */
27939 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27940 if (! dwarf_strict && dwarf_version < 4)
27941 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27943 add_ranges (NULL);
27947 if (debug_info_level >= DINFO_LEVEL_TERSE)
27948 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27949 debug_line_section_label);
27951 if (have_macinfo)
27952 add_AT_macptr (comp_unit_die (),
27953 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
27954 macinfo_section_label);
27956 if (dwarf_split_debug_info)
27958 /* optimize_location_lists calculates the size of the lists,
27959 so index them first, and assign indices to the entries.
27960 Although optimize_location_lists will remove entries from
27961 the table, it only does so for duplicates, and therefore
27962 only reduces ref_counts to 1. */
27963 index_location_lists (comp_unit_die ());
27965 if (addr_index_table != NULL)
27967 unsigned int index = 0;
27968 addr_index_table
27969 ->traverse_noresize<unsigned int *, index_addr_table_entry>
27970 (&index);
27974 if (have_location_lists)
27975 optimize_location_lists (comp_unit_die ());
27977 save_macinfo_strings ();
27979 if (dwarf_split_debug_info)
27981 unsigned int index = 0;
27983 /* Add attributes common to skeleton compile_units and
27984 type_units. Because these attributes include strings, it
27985 must be done before freezing the string table. Top-level
27986 skeleton die attrs are added when the skeleton type unit is
27987 created, so ensure it is created by this point. */
27988 add_top_level_skeleton_die_attrs (main_comp_unit_die);
27989 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
27992 /* Output all of the compilation units. We put the main one last so that
27993 the offsets are available to output_pubnames. */
27994 for (node = limbo_die_list; node; node = node->next)
27995 output_comp_unit (node->die, 0);
27997 hash_table<comdat_type_hasher> comdat_type_table (100);
27998 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
28000 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
28002 /* Don't output duplicate types. */
28003 if (*slot != HTAB_EMPTY_ENTRY)
28004 continue;
28006 /* Add a pointer to the line table for the main compilation unit
28007 so that the debugger can make sense of DW_AT_decl_file
28008 attributes. */
28009 if (debug_info_level >= DINFO_LEVEL_TERSE)
28010 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
28011 (!dwarf_split_debug_info
28012 ? debug_line_section_label
28013 : debug_skeleton_line_section_label));
28015 output_comdat_type_unit (ctnode);
28016 *slot = ctnode;
28019 /* The AT_pubnames attribute needs to go in all skeleton dies, including
28020 both the main_cu and all skeleton TUs. Making this call unconditional
28021 would end up either adding a second copy of the AT_pubnames attribute, or
28022 requiring a special case in add_top_level_skeleton_die_attrs. */
28023 if (!dwarf_split_debug_info)
28024 add_AT_pubnames (comp_unit_die ());
28026 if (dwarf_split_debug_info)
28028 int mark;
28029 unsigned char checksum[16];
28030 struct md5_ctx ctx;
28032 /* Compute a checksum of the comp_unit to use as the dwo_id. */
28033 md5_init_ctx (&ctx);
28034 mark = 0;
28035 die_checksum (comp_unit_die (), &ctx, &mark);
28036 unmark_all_dies (comp_unit_die ());
28037 md5_finish_ctx (&ctx, checksum);
28039 /* Use the first 8 bytes of the checksum as the dwo_id,
28040 and add it to both comp-unit DIEs. */
28041 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
28042 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
28044 /* Add the base offset of the ranges table to the skeleton
28045 comp-unit DIE. */
28046 if (ranges_table_in_use)
28047 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
28048 ranges_section_label);
28050 switch_to_section (debug_addr_section);
28051 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
28052 output_addr_table ();
28055 /* Output the main compilation unit if non-empty or if .debug_macinfo
28056 or .debug_macro will be emitted. */
28057 output_comp_unit (comp_unit_die (), have_macinfo);
28059 if (dwarf_split_debug_info && info_section_emitted)
28060 output_skeleton_debug_sections (main_comp_unit_die);
28062 /* Output the abbreviation table. */
28063 if (abbrev_die_table_in_use != 1)
28065 switch_to_section (debug_abbrev_section);
28066 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
28067 output_abbrev_section ();
28070 /* Output location list section if necessary. */
28071 if (have_location_lists)
28073 /* Output the location lists info. */
28074 switch_to_section (debug_loc_section);
28075 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
28076 output_location_lists (comp_unit_die ());
28079 output_pubtables ();
28081 /* Output the address range information if a CU (.debug_info section)
28082 was emitted. We output an empty table even if we had no functions
28083 to put in it. This because the consumer has no way to tell the
28084 difference between an empty table that we omitted and failure to
28085 generate a table that would have contained data. */
28086 if (info_section_emitted)
28088 switch_to_section (debug_aranges_section);
28089 output_aranges ();
28092 /* Output ranges section if necessary. */
28093 if (ranges_table_in_use)
28095 switch_to_section (debug_ranges_section);
28096 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
28097 output_ranges ();
28100 /* Have to end the macro section. */
28101 if (have_macinfo)
28103 switch_to_section (debug_macinfo_section);
28104 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
28105 output_macinfo ();
28106 dw2_asm_output_data (1, 0, "End compilation unit");
28109 /* Output the source line correspondence table. We must do this
28110 even if there is no line information. Otherwise, on an empty
28111 translation unit, we will generate a present, but empty,
28112 .debug_info section. IRIX 6.5 `nm' will then complain when
28113 examining the file. This is done late so that any filenames
28114 used by the debug_info section are marked as 'used'. */
28115 switch_to_section (debug_line_section);
28116 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
28117 if (! DWARF2_ASM_LINE_DEBUG_INFO)
28118 output_line_info (false);
28120 if (dwarf_split_debug_info && info_section_emitted)
28122 switch_to_section (debug_skeleton_line_section);
28123 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
28124 output_line_info (true);
28127 /* If we emitted any indirect strings, output the string table too. */
28128 if (debug_str_hash || skeleton_debug_str_hash)
28129 output_indirect_strings ();
28132 /* Perform any cleanups needed after the early debug generation pass
28133 has run. */
28135 static void
28136 dwarf2out_early_finish (const char *filename)
28138 set_early_dwarf s;
28140 /* PCH might result in DW_AT_producer string being restored from the
28141 header compilation, so always fill it with empty string initially
28142 and overwrite only here. */
28143 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
28144 producer_string = gen_producer_string ();
28145 producer->dw_attr_val.v.val_str->refcount--;
28146 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
28148 /* Add the name for the main input file now. We delayed this from
28149 dwarf2out_init to avoid complications with PCH. */
28150 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
28151 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
28152 add_comp_dir_attribute (comp_unit_die ());
28153 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
28155 bool p = false;
28156 file_table->traverse<bool *, file_table_relative_p> (&p);
28157 if (p)
28158 add_comp_dir_attribute (comp_unit_die ());
28161 /* With LTO early dwarf was really finished at compile-time, so make
28162 sure to adjust the phase after annotating the LTRANS CU DIE. */
28163 if (in_lto_p)
28165 early_dwarf_finished = true;
28166 return;
28169 /* Walk through the list of incomplete types again, trying once more to
28170 emit full debugging info for them. */
28171 retry_incomplete_types ();
28173 /* The point here is to flush out the limbo list so that it is empty
28174 and we don't need to stream it for LTO. */
28175 flush_limbo_die_list ();
28177 gen_scheduled_generic_parms_dies ();
28178 gen_remaining_tmpl_value_param_die_attribute ();
28180 /* Add DW_AT_linkage_name for all deferred DIEs. */
28181 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
28183 tree decl = node->created_for;
28184 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
28185 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
28186 ended up in deferred_asm_name before we knew it was
28187 constant and never written to disk. */
28188 && DECL_ASSEMBLER_NAME (decl))
28190 add_linkage_attr (node->die, decl);
28191 move_linkage_attr (node->die);
28194 deferred_asm_name = NULL;
28196 /* The early debug phase is now finished. */
28197 early_dwarf_finished = true;
28200 /* Reset all state within dwarf2out.c so that we can rerun the compiler
28201 within the same process. For use by toplev::finalize. */
28203 void
28204 dwarf2out_c_finalize (void)
28206 last_var_location_insn = NULL;
28207 cached_next_real_insn = NULL;
28208 used_rtx_array = NULL;
28209 incomplete_types = NULL;
28210 decl_scope_table = NULL;
28211 debug_info_section = NULL;
28212 debug_skeleton_info_section = NULL;
28213 debug_abbrev_section = NULL;
28214 debug_skeleton_abbrev_section = NULL;
28215 debug_aranges_section = NULL;
28216 debug_addr_section = NULL;
28217 debug_macinfo_section = NULL;
28218 debug_line_section = NULL;
28219 debug_skeleton_line_section = NULL;
28220 debug_loc_section = NULL;
28221 debug_pubnames_section = NULL;
28222 debug_pubtypes_section = NULL;
28223 debug_str_section = NULL;
28224 debug_str_dwo_section = NULL;
28225 debug_str_offsets_section = NULL;
28226 debug_ranges_section = NULL;
28227 debug_frame_section = NULL;
28228 fde_vec = NULL;
28229 debug_str_hash = NULL;
28230 skeleton_debug_str_hash = NULL;
28231 dw2_string_counter = 0;
28232 have_multiple_function_sections = false;
28233 text_section_used = false;
28234 cold_text_section_used = false;
28235 cold_text_section = NULL;
28236 current_unit_personality = NULL;
28238 next_die_offset = 0;
28239 single_comp_unit_die = NULL;
28240 comdat_type_list = NULL;
28241 limbo_die_list = NULL;
28242 file_table = NULL;
28243 decl_die_table = NULL;
28244 common_block_die_table = NULL;
28245 decl_loc_table = NULL;
28246 call_arg_locations = NULL;
28247 call_arg_loc_last = NULL;
28248 call_site_count = -1;
28249 tail_call_site_count = -1;
28250 cached_dw_loc_list_table = NULL;
28251 abbrev_die_table = NULL;
28252 abbrev_die_table_allocated = 0;
28253 abbrev_die_table_in_use = 0;
28254 delete dwarf_proc_stack_usage_map;
28255 dwarf_proc_stack_usage_map = NULL;
28256 line_info_label_num = 0;
28257 cur_line_info_table = NULL;
28258 text_section_line_info = NULL;
28259 cold_text_section_line_info = NULL;
28260 separate_line_info = NULL;
28261 info_section_emitted = false;
28262 pubname_table = NULL;
28263 pubtype_table = NULL;
28264 macinfo_table = NULL;
28265 ranges_table = NULL;
28266 ranges_table_allocated = 0;
28267 ranges_table_in_use = 0;
28268 ranges_by_label = 0;
28269 ranges_by_label_allocated = 0;
28270 ranges_by_label_in_use = 0;
28271 have_location_lists = false;
28272 loclabel_num = 0;
28273 poc_label_num = 0;
28274 last_emitted_file = NULL;
28275 label_num = 0;
28276 tmpl_value_parm_die_table = NULL;
28277 generic_type_instances = NULL;
28278 frame_pointer_fb_offset = 0;
28279 frame_pointer_fb_offset_valid = false;
28280 base_types.release ();
28281 XDELETEVEC (producer_string);
28282 producer_string = NULL;
28285 #include "gt-dwarf2out.h"