S/390: Add static OSC breaker if necessary.
[official-gcc.git] / gcc / dwarf2out.c
blob78a29799564cd6a202c175a54d7cb4fda4d2217f
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 "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx_insn *last_var_location_insn;
98 static rtx_insn *cached_next_real_insn;
99 static void dwarf2out_decl (tree);
101 #ifndef XCOFF_DEBUGGING_INFO
102 #define XCOFF_DEBUGGING_INFO 0
103 #endif
105 #ifndef HAVE_XCOFF_DWARF_EXTRAS
106 #define HAVE_XCOFF_DWARF_EXTRAS 0
107 #endif
109 #ifdef VMS_DEBUGGING_INFO
110 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
112 /* Define this macro to be a nonzero value if the directory specifications
113 which are output in the debug info should end with a separator. */
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
115 /* Define this macro to evaluate to a nonzero value if GCC should refrain
116 from generating indirect strings in DWARF2 debug information, for instance
117 if your target is stuck with an old version of GDB that is unable to
118 process them properly or uses VMS Debug. */
119 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
120 #else
121 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
123 #endif
125 /* ??? Poison these here until it can be done generically. They've been
126 totally replaced in this file; make sure it stays that way. */
127 #undef DWARF2_UNWIND_INFO
128 #undef DWARF2_FRAME_INFO
129 #if (GCC_VERSION >= 3000)
130 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
131 #endif
133 /* The size of the target's pointer type. */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
138 /* Array of RTXes referenced by the debugging information, which therefore
139 must be kept around forever. */
140 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
142 /* A pointer to the base of a list of incomplete types which might be
143 completed at some later time. incomplete_types_list needs to be a
144 vec<tree, va_gc> *because we want to tell the garbage collector about
145 it. */
146 static GTY(()) vec<tree, va_gc> *incomplete_types;
148 /* A pointer to the base of a table of references to declaration
149 scopes. This table is a display which tracks the nesting
150 of declaration scopes at the current scope and containing
151 scopes. This table is used to find the proper place to
152 define type declaration DIE's. */
153 static GTY(()) vec<tree, va_gc> *decl_scope_table;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_skeleton_info_section;
158 static GTY(()) section *debug_abbrev_section;
159 static GTY(()) section *debug_skeleton_abbrev_section;
160 static GTY(()) section *debug_aranges_section;
161 static GTY(()) section *debug_addr_section;
162 static GTY(()) section *debug_macinfo_section;
163 static const char *debug_macinfo_section_name;
164 static GTY(()) section *debug_line_section;
165 static GTY(()) section *debug_skeleton_line_section;
166 static GTY(()) section *debug_loc_section;
167 static GTY(()) section *debug_pubnames_section;
168 static GTY(()) section *debug_pubtypes_section;
169 static GTY(()) section *debug_str_section;
170 static GTY(()) section *debug_str_dwo_section;
171 static GTY(()) section *debug_str_offsets_section;
172 static GTY(()) section *debug_ranges_section;
173 static GTY(()) section *debug_frame_section;
175 /* Maximum size (in bytes) of an artificially generated label. */
176 #define MAX_ARTIFICIAL_LABEL_BYTES 30
178 /* According to the (draft) DWARF 3 specification, the initial length
179 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
180 bytes are 0xffffffff, followed by the length stored in the next 8
181 bytes.
183 However, the SGI/MIPS ABI uses an initial length which is equal to
184 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
186 #ifndef DWARF_INITIAL_LENGTH_SIZE
187 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
188 #endif
190 /* Round SIZE up to the nearest BOUNDARY. */
191 #define DWARF_ROUND(SIZE,BOUNDARY) \
192 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
194 /* CIE identifier. */
195 #if HOST_BITS_PER_WIDE_INT >= 64
196 #define DWARF_CIE_ID \
197 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
198 #else
199 #define DWARF_CIE_ID DW_CIE_ID
200 #endif
203 /* A vector for a table that contains frame description
204 information for each routine. */
205 #define NOT_INDEXED (-1U)
206 #define NO_INDEX_ASSIGNED (-2U)
208 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
210 struct GTY((for_user)) indirect_string_node {
211 const char *str;
212 unsigned int refcount;
213 enum dwarf_form form;
214 char *label;
215 unsigned int index;
218 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
220 typedef const char *compare_type;
222 static hashval_t hash (indirect_string_node *);
223 static bool equal (indirect_string_node *, const char *);
226 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
228 /* With split_debug_info, both the comp_dir and dwo_name go in the
229 main object file, rather than the dwo, similar to the force_direct
230 parameter elsewhere but with additional complications:
232 1) The string is needed in both the main object file and the dwo.
233 That is, the comp_dir and dwo_name will appear in both places.
235 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
236 DW_FORM_GNU_str_index.
238 3) GCC chooses the form to use late, depending on the size and
239 reference count.
241 Rather than forcing the all debug string handling functions and
242 callers to deal with these complications, simply use a separate,
243 special-cased string table for any attribute that should go in the
244 main object file. This limits the complexity to just the places
245 that need it. */
247 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
249 static GTY(()) int dw2_string_counter;
251 /* True if the compilation unit places functions in more than one section. */
252 static GTY(()) bool have_multiple_function_sections = false;
254 /* Whether the default text and cold text sections have been used at all. */
256 static GTY(()) bool text_section_used = false;
257 static GTY(()) bool cold_text_section_used = false;
259 /* The default cold text section. */
260 static GTY(()) section *cold_text_section;
262 /* The DIE for C++14 'auto' in a function return type. */
263 static GTY(()) dw_die_ref auto_die;
265 /* The DIE for C++14 'decltype(auto)' in a function return type. */
266 static GTY(()) dw_die_ref decltype_auto_die;
268 /* Forward declarations for functions defined in this file. */
270 static void output_call_frame_info (int);
271 static void dwarf2out_note_section_used (void);
273 /* Personality decl of current unit. Used only when assembler does not support
274 personality CFI. */
275 static GTY(()) rtx current_unit_personality;
277 /* Data and reference forms for relocatable data. */
278 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
279 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
281 #ifndef DEBUG_FRAME_SECTION
282 #define DEBUG_FRAME_SECTION ".debug_frame"
283 #endif
285 #ifndef FUNC_BEGIN_LABEL
286 #define FUNC_BEGIN_LABEL "LFB"
287 #endif
289 #ifndef FUNC_END_LABEL
290 #define FUNC_END_LABEL "LFE"
291 #endif
293 #ifndef PROLOGUE_END_LABEL
294 #define PROLOGUE_END_LABEL "LPE"
295 #endif
297 #ifndef EPILOGUE_BEGIN_LABEL
298 #define EPILOGUE_BEGIN_LABEL "LEB"
299 #endif
301 #ifndef FRAME_BEGIN_LABEL
302 #define FRAME_BEGIN_LABEL "Lframe"
303 #endif
304 #define CIE_AFTER_SIZE_LABEL "LSCIE"
305 #define CIE_END_LABEL "LECIE"
306 #define FDE_LABEL "LSFDE"
307 #define FDE_AFTER_SIZE_LABEL "LASFDE"
308 #define FDE_END_LABEL "LEFDE"
309 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
310 #define LINE_NUMBER_END_LABEL "LELT"
311 #define LN_PROLOG_AS_LABEL "LASLTP"
312 #define LN_PROLOG_END_LABEL "LELTP"
313 #define DIE_LABEL_PREFIX "DW"
315 /* Match the base name of a file to the base name of a compilation unit. */
317 static int
318 matches_main_base (const char *path)
320 /* Cache the last query. */
321 static const char *last_path = NULL;
322 static int last_match = 0;
323 if (path != last_path)
325 const char *base;
326 int length = base_of_path (path, &base);
327 last_path = path;
328 last_match = (length == main_input_baselength
329 && memcmp (base, main_input_basename, length) == 0);
331 return last_match;
334 #ifdef DEBUG_DEBUG_STRUCT
336 static int
337 dump_struct_debug (tree type, enum debug_info_usage usage,
338 enum debug_struct_file criterion, int generic,
339 int matches, int result)
341 /* Find the type name. */
342 tree type_decl = TYPE_STUB_DECL (type);
343 tree t = type_decl;
344 const char *name = 0;
345 if (TREE_CODE (t) == TYPE_DECL)
346 t = DECL_NAME (t);
347 if (t)
348 name = IDENTIFIER_POINTER (t);
350 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
351 criterion,
352 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
353 matches ? "bas" : "hdr",
354 generic ? "gen" : "ord",
355 usage == DINFO_USAGE_DFN ? ";" :
356 usage == DINFO_USAGE_DIR_USE ? "." : "*",
357 result,
358 (void*) type_decl, name);
359 return result;
361 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
362 dump_struct_debug (type, usage, criterion, generic, matches, result)
364 #else
366 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
367 (result)
369 #endif
371 /* Get the number of HOST_WIDE_INTs needed to represent the precision
372 of the number. Some constants have a large uniform precision, so
373 we get the precision needed for the actual value of the number. */
375 static unsigned int
376 get_full_len (const wide_int &op)
378 int prec = wi::min_precision (op, UNSIGNED);
379 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
380 / HOST_BITS_PER_WIDE_INT);
383 static bool
384 should_emit_struct_debug (tree type, enum debug_info_usage usage)
386 enum debug_struct_file criterion;
387 tree type_decl;
388 bool generic = lang_hooks.types.generic_p (type);
390 if (generic)
391 criterion = debug_struct_generic[usage];
392 else
393 criterion = debug_struct_ordinary[usage];
395 if (criterion == DINFO_STRUCT_FILE_NONE)
396 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
397 if (criterion == DINFO_STRUCT_FILE_ANY)
398 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
400 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
402 if (type_decl != NULL)
404 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
405 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
407 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
408 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
414 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
415 switch to the data section instead, and write out a synthetic start label
416 for collect2 the first time around. */
418 static void
419 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
421 if (eh_frame_section == 0)
423 int flags;
425 if (EH_TABLES_CAN_BE_READ_ONLY)
427 int fde_encoding;
428 int per_encoding;
429 int lsda_encoding;
431 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
432 /*global=*/0);
433 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
434 /*global=*/1);
435 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
436 /*global=*/0);
437 flags = ((! flag_pic
438 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
439 && (fde_encoding & 0x70) != DW_EH_PE_aligned
440 && (per_encoding & 0x70) != DW_EH_PE_absptr
441 && (per_encoding & 0x70) != DW_EH_PE_aligned
442 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
443 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
444 ? 0 : SECTION_WRITE);
446 else
447 flags = SECTION_WRITE;
449 #ifdef EH_FRAME_SECTION_NAME
450 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
451 #else
452 eh_frame_section = ((flags == SECTION_WRITE)
453 ? data_section : readonly_data_section);
454 #endif /* EH_FRAME_SECTION_NAME */
457 switch_to_section (eh_frame_section);
459 #ifdef EH_FRAME_THROUGH_COLLECT2
460 /* We have no special eh_frame section. Emit special labels to guide
461 collect2. */
462 if (!back)
464 tree label = get_file_function_name ("F");
465 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
466 targetm.asm_out.globalize_label (asm_out_file,
467 IDENTIFIER_POINTER (label));
468 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
470 #endif
473 /* Switch [BACK] to the eh or debug frame table section, depending on
474 FOR_EH. */
476 static void
477 switch_to_frame_table_section (int for_eh, bool back)
479 if (for_eh)
480 switch_to_eh_frame_section (back);
481 else
483 if (!debug_frame_section)
484 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
485 SECTION_DEBUG, NULL);
486 switch_to_section (debug_frame_section);
490 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
492 enum dw_cfi_oprnd_type
493 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
495 switch (cfi)
497 case DW_CFA_nop:
498 case DW_CFA_GNU_window_save:
499 case DW_CFA_remember_state:
500 case DW_CFA_restore_state:
501 return dw_cfi_oprnd_unused;
503 case DW_CFA_set_loc:
504 case DW_CFA_advance_loc1:
505 case DW_CFA_advance_loc2:
506 case DW_CFA_advance_loc4:
507 case DW_CFA_MIPS_advance_loc8:
508 return dw_cfi_oprnd_addr;
510 case DW_CFA_offset:
511 case DW_CFA_offset_extended:
512 case DW_CFA_def_cfa:
513 case DW_CFA_offset_extended_sf:
514 case DW_CFA_def_cfa_sf:
515 case DW_CFA_restore:
516 case DW_CFA_restore_extended:
517 case DW_CFA_undefined:
518 case DW_CFA_same_value:
519 case DW_CFA_def_cfa_register:
520 case DW_CFA_register:
521 case DW_CFA_expression:
522 return dw_cfi_oprnd_reg_num;
524 case DW_CFA_def_cfa_offset:
525 case DW_CFA_GNU_args_size:
526 case DW_CFA_def_cfa_offset_sf:
527 return dw_cfi_oprnd_offset;
529 case DW_CFA_def_cfa_expression:
530 return dw_cfi_oprnd_loc;
532 default:
533 gcc_unreachable ();
537 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
539 enum dw_cfi_oprnd_type
540 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
542 switch (cfi)
544 case DW_CFA_def_cfa:
545 case DW_CFA_def_cfa_sf:
546 case DW_CFA_offset:
547 case DW_CFA_offset_extended_sf:
548 case DW_CFA_offset_extended:
549 return dw_cfi_oprnd_offset;
551 case DW_CFA_register:
552 return dw_cfi_oprnd_reg_num;
554 case DW_CFA_expression:
555 return dw_cfi_oprnd_loc;
557 default:
558 return dw_cfi_oprnd_unused;
562 /* Output one FDE. */
564 static void
565 output_fde (dw_fde_ref fde, bool for_eh, bool second,
566 char *section_start_label, int fde_encoding, char *augmentation,
567 bool any_lsda_needed, int lsda_encoding)
569 const char *begin, *end;
570 static unsigned int j;
571 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
573 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
574 /* empty */ 0);
575 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
576 for_eh + j);
577 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
578 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
579 if (!XCOFF_DEBUGGING_INFO || for_eh)
581 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
582 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
583 " indicating 64-bit DWARF extension");
584 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
585 "FDE Length");
587 ASM_OUTPUT_LABEL (asm_out_file, l1);
589 if (for_eh)
590 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
591 else
592 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
593 debug_frame_section, "FDE CIE offset");
595 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
596 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
598 if (for_eh)
600 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
601 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
602 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
603 "FDE initial location");
604 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
605 end, begin, "FDE address range");
607 else
609 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
610 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
613 if (augmentation[0])
615 if (any_lsda_needed)
617 int size = size_of_encoded_value (lsda_encoding);
619 if (lsda_encoding == DW_EH_PE_aligned)
621 int offset = ( 4 /* Length */
622 + 4 /* CIE offset */
623 + 2 * size_of_encoded_value (fde_encoding)
624 + 1 /* Augmentation size */ );
625 int pad = -offset & (PTR_SIZE - 1);
627 size += pad;
628 gcc_assert (size_of_uleb128 (size) == 1);
631 dw2_asm_output_data_uleb128 (size, "Augmentation size");
633 if (fde->uses_eh_lsda)
635 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
636 fde->funcdef_number);
637 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
638 gen_rtx_SYMBOL_REF (Pmode, l1),
639 false,
640 "Language Specific Data Area");
642 else
644 if (lsda_encoding == DW_EH_PE_aligned)
645 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
646 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
647 "Language Specific Data Area (none)");
650 else
651 dw2_asm_output_data_uleb128 (0, "Augmentation size");
654 /* Loop through the Call Frame Instructions associated with this FDE. */
655 fde->dw_fde_current_label = begin;
657 size_t from, until, i;
659 from = 0;
660 until = vec_safe_length (fde->dw_fde_cfi);
662 if (fde->dw_fde_second_begin == NULL)
664 else if (!second)
665 until = fde->dw_fde_switch_cfi_index;
666 else
667 from = fde->dw_fde_switch_cfi_index;
669 for (i = from; i < until; i++)
670 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
673 /* If we are to emit a ref/link from function bodies to their frame tables,
674 do it now. This is typically performed to make sure that tables
675 associated with functions are dragged with them and not discarded in
676 garbage collecting links. We need to do this on a per function basis to
677 cope with -ffunction-sections. */
679 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
680 /* Switch to the function section, emit the ref to the tables, and
681 switch *back* into the table section. */
682 switch_to_section (function_section (fde->decl));
683 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
684 switch_to_frame_table_section (for_eh, true);
685 #endif
687 /* Pad the FDE out to an address sized boundary. */
688 ASM_OUTPUT_ALIGN (asm_out_file,
689 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
690 ASM_OUTPUT_LABEL (asm_out_file, l2);
692 j += 2;
695 /* Return true if frame description entry FDE is needed for EH. */
697 static bool
698 fde_needed_for_eh_p (dw_fde_ref fde)
700 if (flag_asynchronous_unwind_tables)
701 return true;
703 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
704 return true;
706 if (fde->uses_eh_lsda)
707 return true;
709 /* If exceptions are enabled, we have collected nothrow info. */
710 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
711 return false;
713 return true;
716 /* Output the call frame information used to record information
717 that relates to calculating the frame pointer, and records the
718 location of saved registers. */
720 static void
721 output_call_frame_info (int for_eh)
723 unsigned int i;
724 dw_fde_ref fde;
725 dw_cfi_ref cfi;
726 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
727 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
728 bool any_lsda_needed = false;
729 char augmentation[6];
730 int augmentation_size;
731 int fde_encoding = DW_EH_PE_absptr;
732 int per_encoding = DW_EH_PE_absptr;
733 int lsda_encoding = DW_EH_PE_absptr;
734 int return_reg;
735 rtx personality = NULL;
736 int dw_cie_version;
738 /* Don't emit a CIE if there won't be any FDEs. */
739 if (!fde_vec)
740 return;
742 /* Nothing to do if the assembler's doing it all. */
743 if (dwarf2out_do_cfi_asm ())
744 return;
746 /* If we don't have any functions we'll want to unwind out of, don't emit
747 any EH unwind information. If we make FDEs linkonce, we may have to
748 emit an empty label for an FDE that wouldn't otherwise be emitted. We
749 want to avoid having an FDE kept around when the function it refers to
750 is discarded. Example where this matters: a primary function template
751 in C++ requires EH information, an explicit specialization doesn't. */
752 if (for_eh)
754 bool any_eh_needed = false;
756 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
758 if (fde->uses_eh_lsda)
759 any_eh_needed = any_lsda_needed = true;
760 else if (fde_needed_for_eh_p (fde))
761 any_eh_needed = true;
762 else if (TARGET_USES_WEAK_UNWIND_INFO)
763 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
766 if (!any_eh_needed)
767 return;
770 /* We're going to be generating comments, so turn on app. */
771 if (flag_debug_asm)
772 app_enable ();
774 /* Switch to the proper frame section, first time. */
775 switch_to_frame_table_section (for_eh, false);
777 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
778 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
780 /* Output the CIE. */
781 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
782 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
783 if (!XCOFF_DEBUGGING_INFO || for_eh)
785 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
786 dw2_asm_output_data (4, 0xffffffff,
787 "Initial length escape value indicating 64-bit DWARF extension");
788 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
789 "Length of Common Information Entry");
791 ASM_OUTPUT_LABEL (asm_out_file, l1);
793 /* Now that the CIE pointer is PC-relative for EH,
794 use 0 to identify the CIE. */
795 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
796 (for_eh ? 0 : DWARF_CIE_ID),
797 "CIE Identifier Tag");
799 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
800 use CIE version 1, unless that would produce incorrect results
801 due to overflowing the return register column. */
802 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
803 dw_cie_version = 1;
804 if (return_reg >= 256 || dwarf_version > 2)
805 dw_cie_version = 3;
806 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
808 augmentation[0] = 0;
809 augmentation_size = 0;
811 personality = current_unit_personality;
812 if (for_eh)
814 char *p;
816 /* Augmentation:
817 z Indicates that a uleb128 is present to size the
818 augmentation section.
819 L Indicates the encoding (and thus presence) of
820 an LSDA pointer in the FDE augmentation.
821 R Indicates a non-default pointer encoding for
822 FDE code pointers.
823 P Indicates the presence of an encoding + language
824 personality routine in the CIE augmentation. */
826 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
827 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
828 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
830 p = augmentation + 1;
831 if (personality)
833 *p++ = 'P';
834 augmentation_size += 1 + size_of_encoded_value (per_encoding);
835 assemble_external_libcall (personality);
837 if (any_lsda_needed)
839 *p++ = 'L';
840 augmentation_size += 1;
842 if (fde_encoding != DW_EH_PE_absptr)
844 *p++ = 'R';
845 augmentation_size += 1;
847 if (p > augmentation + 1)
849 augmentation[0] = 'z';
850 *p = '\0';
853 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
854 if (personality && per_encoding == DW_EH_PE_aligned)
856 int offset = ( 4 /* Length */
857 + 4 /* CIE Id */
858 + 1 /* CIE version */
859 + strlen (augmentation) + 1 /* Augmentation */
860 + size_of_uleb128 (1) /* Code alignment */
861 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
862 + 1 /* RA column */
863 + 1 /* Augmentation size */
864 + 1 /* Personality encoding */ );
865 int pad = -offset & (PTR_SIZE - 1);
867 augmentation_size += pad;
869 /* Augmentations should be small, so there's scarce need to
870 iterate for a solution. Die if we exceed one uleb128 byte. */
871 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
875 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
876 if (dw_cie_version >= 4)
878 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
879 dw2_asm_output_data (1, 0, "CIE Segment Size");
881 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
882 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
883 "CIE Data Alignment Factor");
885 if (dw_cie_version == 1)
886 dw2_asm_output_data (1, return_reg, "CIE RA Column");
887 else
888 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
890 if (augmentation[0])
892 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
893 if (personality)
895 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
896 eh_data_format_name (per_encoding));
897 dw2_asm_output_encoded_addr_rtx (per_encoding,
898 personality,
899 true, NULL);
902 if (any_lsda_needed)
903 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
904 eh_data_format_name (lsda_encoding));
906 if (fde_encoding != DW_EH_PE_absptr)
907 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
908 eh_data_format_name (fde_encoding));
911 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
912 output_cfi (cfi, NULL, for_eh);
914 /* Pad the CIE out to an address sized boundary. */
915 ASM_OUTPUT_ALIGN (asm_out_file,
916 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
917 ASM_OUTPUT_LABEL (asm_out_file, l2);
919 /* Loop through all of the FDE's. */
920 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
922 unsigned int k;
924 /* Don't emit EH unwind info for leaf functions that don't need it. */
925 if (for_eh && !fde_needed_for_eh_p (fde))
926 continue;
928 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
929 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
930 augmentation, any_lsda_needed, lsda_encoding);
933 if (for_eh && targetm.terminate_dw2_eh_frame_info)
934 dw2_asm_output_data (4, 0, "End of Table");
936 /* Turn off app to make assembly quicker. */
937 if (flag_debug_asm)
938 app_disable ();
941 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
943 static void
944 dwarf2out_do_cfi_startproc (bool second)
946 int enc;
947 rtx ref;
948 rtx personality = get_personality_function (current_function_decl);
950 fprintf (asm_out_file, "\t.cfi_startproc\n");
952 if (personality)
954 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
955 ref = personality;
957 /* ??? The GAS support isn't entirely consistent. We have to
958 handle indirect support ourselves, but PC-relative is done
959 in the assembler. Further, the assembler can't handle any
960 of the weirder relocation types. */
961 if (enc & DW_EH_PE_indirect)
962 ref = dw2_force_const_mem (ref, true);
964 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
965 output_addr_const (asm_out_file, ref);
966 fputc ('\n', asm_out_file);
969 if (crtl->uses_eh_lsda)
971 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
973 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
974 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
975 current_function_funcdef_no);
976 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
977 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
979 if (enc & DW_EH_PE_indirect)
980 ref = dw2_force_const_mem (ref, true);
982 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
983 output_addr_const (asm_out_file, ref);
984 fputc ('\n', asm_out_file);
988 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
989 this allocation may be done before pass_final. */
991 dw_fde_ref
992 dwarf2out_alloc_current_fde (void)
994 dw_fde_ref fde;
996 fde = ggc_cleared_alloc<dw_fde_node> ();
997 fde->decl = current_function_decl;
998 fde->funcdef_number = current_function_funcdef_no;
999 fde->fde_index = vec_safe_length (fde_vec);
1000 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1001 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1002 fde->nothrow = crtl->nothrow;
1003 fde->drap_reg = INVALID_REGNUM;
1004 fde->vdrap_reg = INVALID_REGNUM;
1006 /* Record the FDE associated with this function. */
1007 cfun->fde = fde;
1008 vec_safe_push (fde_vec, fde);
1010 return fde;
1013 /* Output a marker (i.e. a label) for the beginning of a function, before
1014 the prologue. */
1016 void
1017 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1018 const char *file ATTRIBUTE_UNUSED)
1020 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1021 char * dup_label;
1022 dw_fde_ref fde;
1023 section *fnsec;
1024 bool do_frame;
1026 current_function_func_begin_label = NULL;
1028 do_frame = dwarf2out_do_frame ();
1030 /* ??? current_function_func_begin_label is also used by except.c for
1031 call-site information. We must emit this label if it might be used. */
1032 if (!do_frame
1033 && (!flag_exceptions
1034 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1035 return;
1037 fnsec = function_section (current_function_decl);
1038 switch_to_section (fnsec);
1039 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1040 current_function_funcdef_no);
1041 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1042 current_function_funcdef_no);
1043 dup_label = xstrdup (label);
1044 current_function_func_begin_label = dup_label;
1046 /* We can elide the fde allocation if we're not emitting debug info. */
1047 if (!do_frame)
1048 return;
1050 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1051 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1052 would include pass_dwarf2_frame. If we've not created the FDE yet,
1053 do so now. */
1054 fde = cfun->fde;
1055 if (fde == NULL)
1056 fde = dwarf2out_alloc_current_fde ();
1058 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1059 fde->dw_fde_begin = dup_label;
1060 fde->dw_fde_current_label = dup_label;
1061 fde->in_std_section = (fnsec == text_section
1062 || (cold_text_section && fnsec == cold_text_section));
1064 /* We only want to output line number information for the genuine dwarf2
1065 prologue case, not the eh frame case. */
1066 #ifdef DWARF2_DEBUGGING_INFO
1067 if (file)
1068 dwarf2out_source_line (line, file, 0, true);
1069 #endif
1071 if (dwarf2out_do_cfi_asm ())
1072 dwarf2out_do_cfi_startproc (false);
1073 else
1075 rtx personality = get_personality_function (current_function_decl);
1076 if (!current_unit_personality)
1077 current_unit_personality = personality;
1079 /* We cannot keep a current personality per function as without CFI
1080 asm, at the point where we emit the CFI data, there is no current
1081 function anymore. */
1082 if (personality && current_unit_personality != personality)
1083 sorry ("multiple EH personalities are supported only with assemblers "
1084 "supporting .cfi_personality directive");
1088 /* Output a marker (i.e. a label) for the end of the generated code
1089 for a function prologue. This gets called *after* the prologue code has
1090 been generated. */
1092 void
1093 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1094 const char *file ATTRIBUTE_UNUSED)
1096 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1098 /* Output a label to mark the endpoint of the code generated for this
1099 function. */
1100 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1101 current_function_funcdef_no);
1102 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1103 current_function_funcdef_no);
1104 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1107 /* Output a marker (i.e. a label) for the beginning of the generated code
1108 for a function epilogue. This gets called *before* the prologue code has
1109 been generated. */
1111 void
1112 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1113 const char *file ATTRIBUTE_UNUSED)
1115 dw_fde_ref fde = cfun->fde;
1116 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1118 if (fde->dw_fde_vms_begin_epilogue)
1119 return;
1121 /* Output a label to mark the endpoint of the code generated for this
1122 function. */
1123 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1124 current_function_funcdef_no);
1125 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1126 current_function_funcdef_no);
1127 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1130 /* Output a marker (i.e. a label) for the absolute end of the generated code
1131 for a function definition. This gets called *after* the epilogue code has
1132 been generated. */
1134 void
1135 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1136 const char *file ATTRIBUTE_UNUSED)
1138 dw_fde_ref fde;
1139 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1141 last_var_location_insn = NULL;
1142 cached_next_real_insn = NULL;
1144 if (dwarf2out_do_cfi_asm ())
1145 fprintf (asm_out_file, "\t.cfi_endproc\n");
1147 /* Output a label to mark the endpoint of the code generated for this
1148 function. */
1149 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1150 current_function_funcdef_no);
1151 ASM_OUTPUT_LABEL (asm_out_file, label);
1152 fde = cfun->fde;
1153 gcc_assert (fde != NULL);
1154 if (fde->dw_fde_second_begin == NULL)
1155 fde->dw_fde_end = xstrdup (label);
1158 void
1159 dwarf2out_frame_finish (void)
1161 /* Output call frame information. */
1162 if (targetm.debug_unwind_info () == UI_DWARF2)
1163 output_call_frame_info (0);
1165 /* Output another copy for the unwinder. */
1166 if ((flag_unwind_tables || flag_exceptions)
1167 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1168 output_call_frame_info (1);
1171 /* Note that the current function section is being used for code. */
1173 static void
1174 dwarf2out_note_section_used (void)
1176 section *sec = current_function_section ();
1177 if (sec == text_section)
1178 text_section_used = true;
1179 else if (sec == cold_text_section)
1180 cold_text_section_used = true;
1183 static void var_location_switch_text_section (void);
1184 static void set_cur_line_info_table (section *);
1186 void
1187 dwarf2out_switch_text_section (void)
1189 section *sect;
1190 dw_fde_ref fde = cfun->fde;
1192 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1194 if (!in_cold_section_p)
1196 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1197 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1198 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1200 else
1202 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1203 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1204 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1206 have_multiple_function_sections = true;
1208 /* There is no need to mark used sections when not debugging. */
1209 if (cold_text_section != NULL)
1210 dwarf2out_note_section_used ();
1212 if (dwarf2out_do_cfi_asm ())
1213 fprintf (asm_out_file, "\t.cfi_endproc\n");
1215 /* Now do the real section switch. */
1216 sect = current_function_section ();
1217 switch_to_section (sect);
1219 fde->second_in_std_section
1220 = (sect == text_section
1221 || (cold_text_section && sect == cold_text_section));
1223 if (dwarf2out_do_cfi_asm ())
1224 dwarf2out_do_cfi_startproc (true);
1226 var_location_switch_text_section ();
1228 if (cold_text_section != NULL)
1229 set_cur_line_info_table (sect);
1232 /* And now, the subset of the debugging information support code necessary
1233 for emitting location expressions. */
1235 /* Data about a single source file. */
1236 struct GTY((for_user)) dwarf_file_data {
1237 const char * filename;
1238 int emitted_number;
1241 /* Describe an entry into the .debug_addr section. */
1243 enum ate_kind {
1244 ate_kind_rtx,
1245 ate_kind_rtx_dtprel,
1246 ate_kind_label
1249 struct GTY((for_user)) addr_table_entry {
1250 enum ate_kind kind;
1251 unsigned int refcount;
1252 unsigned int index;
1253 union addr_table_entry_struct_union
1255 rtx GTY ((tag ("0"))) rtl;
1256 char * GTY ((tag ("1"))) label;
1258 GTY ((desc ("%1.kind"))) addr;
1261 /* Location lists are ranges + location descriptions for that range,
1262 so you can track variables that are in different places over
1263 their entire life. */
1264 typedef struct GTY(()) dw_loc_list_struct {
1265 dw_loc_list_ref dw_loc_next;
1266 const char *begin; /* Label and addr_entry for start of range */
1267 addr_table_entry *begin_entry;
1268 const char *end; /* Label for end of range */
1269 char *ll_symbol; /* Label for beginning of location list.
1270 Only on head of list */
1271 const char *section; /* Section this loclist is relative to */
1272 dw_loc_descr_ref expr;
1273 hashval_t hash;
1274 /* True if all addresses in this and subsequent lists are known to be
1275 resolved. */
1276 bool resolved_addr;
1277 /* True if this list has been replaced by dw_loc_next. */
1278 bool replaced;
1279 bool emitted;
1280 /* True if the range should be emitted even if begin and end
1281 are the same. */
1282 bool force;
1283 } dw_loc_list_node;
1285 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1286 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1288 /* Convert a DWARF stack opcode into its string name. */
1290 static const char *
1291 dwarf_stack_op_name (unsigned int op)
1293 const char *name = get_DW_OP_name (op);
1295 if (name != NULL)
1296 return name;
1298 return "OP_<unknown>";
1301 /* Return a pointer to a newly allocated location description. Location
1302 descriptions are simple expression terms that can be strung
1303 together to form more complicated location (address) descriptions. */
1305 static inline dw_loc_descr_ref
1306 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1307 unsigned HOST_WIDE_INT oprnd2)
1309 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1311 descr->dw_loc_opc = op;
1312 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1313 descr->dw_loc_oprnd1.val_entry = NULL;
1314 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1315 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1316 descr->dw_loc_oprnd2.val_entry = NULL;
1317 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1319 return descr;
1322 /* Return a pointer to a newly allocated location description for
1323 REG and OFFSET. */
1325 static inline dw_loc_descr_ref
1326 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1328 if (reg <= 31)
1329 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1330 offset, 0);
1331 else
1332 return new_loc_descr (DW_OP_bregx, reg, offset);
1335 /* Add a location description term to a location description expression. */
1337 static inline void
1338 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1340 dw_loc_descr_ref *d;
1342 /* Find the end of the chain. */
1343 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1346 *d = descr;
1349 /* Compare two location operands for exact equality. */
1351 static bool
1352 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1354 if (a->val_class != b->val_class)
1355 return false;
1356 switch (a->val_class)
1358 case dw_val_class_none:
1359 return true;
1360 case dw_val_class_addr:
1361 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1363 case dw_val_class_offset:
1364 case dw_val_class_unsigned_const:
1365 case dw_val_class_const:
1366 case dw_val_class_range_list:
1367 case dw_val_class_lineptr:
1368 case dw_val_class_macptr:
1369 /* These are all HOST_WIDE_INT, signed or unsigned. */
1370 return a->v.val_unsigned == b->v.val_unsigned;
1372 case dw_val_class_loc:
1373 return a->v.val_loc == b->v.val_loc;
1374 case dw_val_class_loc_list:
1375 return a->v.val_loc_list == b->v.val_loc_list;
1376 case dw_val_class_die_ref:
1377 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1378 case dw_val_class_fde_ref:
1379 return a->v.val_fde_index == b->v.val_fde_index;
1380 case dw_val_class_lbl_id:
1381 case dw_val_class_high_pc:
1382 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1383 case dw_val_class_str:
1384 return a->v.val_str == b->v.val_str;
1385 case dw_val_class_flag:
1386 return a->v.val_flag == b->v.val_flag;
1387 case dw_val_class_file:
1388 return a->v.val_file == b->v.val_file;
1389 case dw_val_class_decl_ref:
1390 return a->v.val_decl_ref == b->v.val_decl_ref;
1392 case dw_val_class_const_double:
1393 return (a->v.val_double.high == b->v.val_double.high
1394 && a->v.val_double.low == b->v.val_double.low);
1396 case dw_val_class_wide_int:
1397 return *a->v.val_wide == *b->v.val_wide;
1399 case dw_val_class_vec:
1401 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1402 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1404 return (a_len == b_len
1405 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1408 case dw_val_class_data8:
1409 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1411 case dw_val_class_vms_delta:
1412 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1413 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1415 case dw_val_class_discr_value:
1416 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1417 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1418 case dw_val_class_discr_list:
1419 /* It makes no sense comparing two discriminant value lists. */
1420 return false;
1422 gcc_unreachable ();
1425 /* Compare two location atoms for exact equality. */
1427 static bool
1428 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1430 if (a->dw_loc_opc != b->dw_loc_opc)
1431 return false;
1433 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1434 address size, but since we always allocate cleared storage it
1435 should be zero for other types of locations. */
1436 if (a->dtprel != b->dtprel)
1437 return false;
1439 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1440 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1443 /* Compare two complete location expressions for exact equality. */
1445 bool
1446 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1448 while (1)
1450 if (a == b)
1451 return true;
1452 if (a == NULL || b == NULL)
1453 return false;
1454 if (!loc_descr_equal_p_1 (a, b))
1455 return false;
1457 a = a->dw_loc_next;
1458 b = b->dw_loc_next;
1463 /* Add a constant OFFSET to a location expression. */
1465 static void
1466 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1468 dw_loc_descr_ref loc;
1469 HOST_WIDE_INT *p;
1471 gcc_assert (*list_head != NULL);
1473 if (!offset)
1474 return;
1476 /* Find the end of the chain. */
1477 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1480 p = NULL;
1481 if (loc->dw_loc_opc == DW_OP_fbreg
1482 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1483 p = &loc->dw_loc_oprnd1.v.val_int;
1484 else if (loc->dw_loc_opc == DW_OP_bregx)
1485 p = &loc->dw_loc_oprnd2.v.val_int;
1487 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1488 offset. Don't optimize if an signed integer overflow would happen. */
1489 if (p != NULL
1490 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1491 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1492 *p += offset;
1494 else if (offset > 0)
1495 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1497 else
1499 loc->dw_loc_next = int_loc_descriptor (-offset);
1500 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1504 /* Add a constant OFFSET to a location list. */
1506 static void
1507 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1509 dw_loc_list_ref d;
1510 for (d = list_head; d != NULL; d = d->dw_loc_next)
1511 loc_descr_plus_const (&d->expr, offset);
1514 #define DWARF_REF_SIZE \
1515 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1517 static unsigned long int get_base_type_offset (dw_die_ref);
1519 /* Return the size of a location descriptor. */
1521 static unsigned long
1522 size_of_loc_descr (dw_loc_descr_ref loc)
1524 unsigned long size = 1;
1526 switch (loc->dw_loc_opc)
1528 case DW_OP_addr:
1529 size += DWARF2_ADDR_SIZE;
1530 break;
1531 case DW_OP_GNU_addr_index:
1532 case DW_OP_GNU_const_index:
1533 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1534 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1535 break;
1536 case DW_OP_const1u:
1537 case DW_OP_const1s:
1538 size += 1;
1539 break;
1540 case DW_OP_const2u:
1541 case DW_OP_const2s:
1542 size += 2;
1543 break;
1544 case DW_OP_const4u:
1545 case DW_OP_const4s:
1546 size += 4;
1547 break;
1548 case DW_OP_const8u:
1549 case DW_OP_const8s:
1550 size += 8;
1551 break;
1552 case DW_OP_constu:
1553 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1554 break;
1555 case DW_OP_consts:
1556 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1557 break;
1558 case DW_OP_pick:
1559 size += 1;
1560 break;
1561 case DW_OP_plus_uconst:
1562 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1563 break;
1564 case DW_OP_skip:
1565 case DW_OP_bra:
1566 size += 2;
1567 break;
1568 case DW_OP_breg0:
1569 case DW_OP_breg1:
1570 case DW_OP_breg2:
1571 case DW_OP_breg3:
1572 case DW_OP_breg4:
1573 case DW_OP_breg5:
1574 case DW_OP_breg6:
1575 case DW_OP_breg7:
1576 case DW_OP_breg8:
1577 case DW_OP_breg9:
1578 case DW_OP_breg10:
1579 case DW_OP_breg11:
1580 case DW_OP_breg12:
1581 case DW_OP_breg13:
1582 case DW_OP_breg14:
1583 case DW_OP_breg15:
1584 case DW_OP_breg16:
1585 case DW_OP_breg17:
1586 case DW_OP_breg18:
1587 case DW_OP_breg19:
1588 case DW_OP_breg20:
1589 case DW_OP_breg21:
1590 case DW_OP_breg22:
1591 case DW_OP_breg23:
1592 case DW_OP_breg24:
1593 case DW_OP_breg25:
1594 case DW_OP_breg26:
1595 case DW_OP_breg27:
1596 case DW_OP_breg28:
1597 case DW_OP_breg29:
1598 case DW_OP_breg30:
1599 case DW_OP_breg31:
1600 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1601 break;
1602 case DW_OP_regx:
1603 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1604 break;
1605 case DW_OP_fbreg:
1606 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1607 break;
1608 case DW_OP_bregx:
1609 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1610 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1611 break;
1612 case DW_OP_piece:
1613 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1614 break;
1615 case DW_OP_bit_piece:
1616 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1617 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1618 break;
1619 case DW_OP_deref_size:
1620 case DW_OP_xderef_size:
1621 size += 1;
1622 break;
1623 case DW_OP_call2:
1624 size += 2;
1625 break;
1626 case DW_OP_call4:
1627 size += 4;
1628 break;
1629 case DW_OP_call_ref:
1630 size += DWARF_REF_SIZE;
1631 break;
1632 case DW_OP_implicit_value:
1633 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1634 + loc->dw_loc_oprnd1.v.val_unsigned;
1635 break;
1636 case DW_OP_GNU_implicit_pointer:
1637 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1638 break;
1639 case DW_OP_GNU_entry_value:
1641 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1642 size += size_of_uleb128 (op_size) + op_size;
1643 break;
1645 case DW_OP_GNU_const_type:
1647 unsigned long o
1648 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1649 size += size_of_uleb128 (o) + 1;
1650 switch (loc->dw_loc_oprnd2.val_class)
1652 case dw_val_class_vec:
1653 size += loc->dw_loc_oprnd2.v.val_vec.length
1654 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1655 break;
1656 case dw_val_class_const:
1657 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1658 break;
1659 case dw_val_class_const_double:
1660 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1661 break;
1662 case dw_val_class_wide_int:
1663 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1664 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1665 break;
1666 default:
1667 gcc_unreachable ();
1669 break;
1671 case DW_OP_GNU_regval_type:
1673 unsigned long o
1674 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1675 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1676 + size_of_uleb128 (o);
1678 break;
1679 case DW_OP_GNU_deref_type:
1681 unsigned long o
1682 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1683 size += 1 + size_of_uleb128 (o);
1685 break;
1686 case DW_OP_GNU_convert:
1687 case DW_OP_GNU_reinterpret:
1688 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1689 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1690 else
1692 unsigned long o
1693 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1694 size += size_of_uleb128 (o);
1696 break;
1697 case DW_OP_GNU_parameter_ref:
1698 size += 4;
1699 break;
1700 default:
1701 break;
1704 return size;
1707 /* Return the size of a series of location descriptors. */
1709 unsigned long
1710 size_of_locs (dw_loc_descr_ref loc)
1712 dw_loc_descr_ref l;
1713 unsigned long size;
1715 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1716 field, to avoid writing to a PCH file. */
1717 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1719 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1720 break;
1721 size += size_of_loc_descr (l);
1723 if (! l)
1724 return size;
1726 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1728 l->dw_loc_addr = size;
1729 size += size_of_loc_descr (l);
1732 return size;
1735 /* Return the size of the value in a DW_AT_discr_value attribute. */
1737 static int
1738 size_of_discr_value (dw_discr_value *discr_value)
1740 if (discr_value->pos)
1741 return size_of_uleb128 (discr_value->v.uval);
1742 else
1743 return size_of_sleb128 (discr_value->v.sval);
1746 /* Return the size of the value in a DW_discr_list attribute. */
1748 static int
1749 size_of_discr_list (dw_discr_list_ref discr_list)
1751 int size = 0;
1753 for (dw_discr_list_ref list = discr_list;
1754 list != NULL;
1755 list = list->dw_discr_next)
1757 /* One byte for the discriminant value descriptor, and then one or two
1758 LEB128 numbers, depending on whether it's a single case label or a
1759 range label. */
1760 size += 1;
1761 size += size_of_discr_value (&list->dw_discr_lower_bound);
1762 if (list->dw_discr_range != 0)
1763 size += size_of_discr_value (&list->dw_discr_upper_bound);
1765 return size;
1768 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1769 static void get_ref_die_offset_label (char *, dw_die_ref);
1770 static unsigned long int get_ref_die_offset (dw_die_ref);
1772 /* Output location description stack opcode's operands (if any).
1773 The for_eh_or_skip parameter controls whether register numbers are
1774 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1775 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1776 info). This should be suppressed for the cases that have not been converted
1777 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1779 static void
1780 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1782 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1783 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1785 switch (loc->dw_loc_opc)
1787 #ifdef DWARF2_DEBUGGING_INFO
1788 case DW_OP_const2u:
1789 case DW_OP_const2s:
1790 dw2_asm_output_data (2, val1->v.val_int, NULL);
1791 break;
1792 case DW_OP_const4u:
1793 if (loc->dtprel)
1795 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1796 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1797 val1->v.val_addr);
1798 fputc ('\n', asm_out_file);
1799 break;
1801 /* FALLTHRU */
1802 case DW_OP_const4s:
1803 dw2_asm_output_data (4, val1->v.val_int, NULL);
1804 break;
1805 case DW_OP_const8u:
1806 if (loc->dtprel)
1808 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1809 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1810 val1->v.val_addr);
1811 fputc ('\n', asm_out_file);
1812 break;
1814 /* FALLTHRU */
1815 case DW_OP_const8s:
1816 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1817 dw2_asm_output_data (8, val1->v.val_int, NULL);
1818 break;
1819 case DW_OP_skip:
1820 case DW_OP_bra:
1822 int offset;
1824 gcc_assert (val1->val_class == dw_val_class_loc);
1825 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1827 dw2_asm_output_data (2, offset, NULL);
1829 break;
1830 case DW_OP_implicit_value:
1831 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1832 switch (val2->val_class)
1834 case dw_val_class_const:
1835 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1836 break;
1837 case dw_val_class_vec:
1839 unsigned int elt_size = val2->v.val_vec.elt_size;
1840 unsigned int len = val2->v.val_vec.length;
1841 unsigned int i;
1842 unsigned char *p;
1844 if (elt_size > sizeof (HOST_WIDE_INT))
1846 elt_size /= 2;
1847 len *= 2;
1849 for (i = 0, p = val2->v.val_vec.array;
1850 i < len;
1851 i++, p += elt_size)
1852 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1853 "fp or vector constant word %u", i);
1855 break;
1856 case dw_val_class_const_double:
1858 unsigned HOST_WIDE_INT first, second;
1860 if (WORDS_BIG_ENDIAN)
1862 first = val2->v.val_double.high;
1863 second = val2->v.val_double.low;
1865 else
1867 first = val2->v.val_double.low;
1868 second = val2->v.val_double.high;
1870 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1871 first, NULL);
1872 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873 second, NULL);
1875 break;
1876 case dw_val_class_wide_int:
1878 int i;
1879 int len = get_full_len (*val2->v.val_wide);
1880 if (WORDS_BIG_ENDIAN)
1881 for (i = len - 1; i >= 0; --i)
1882 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1883 val2->v.val_wide->elt (i), NULL);
1884 else
1885 for (i = 0; i < len; ++i)
1886 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1887 val2->v.val_wide->elt (i), NULL);
1889 break;
1890 case dw_val_class_addr:
1891 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1892 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1893 break;
1894 default:
1895 gcc_unreachable ();
1897 break;
1898 #else
1899 case DW_OP_const2u:
1900 case DW_OP_const2s:
1901 case DW_OP_const4u:
1902 case DW_OP_const4s:
1903 case DW_OP_const8u:
1904 case DW_OP_const8s:
1905 case DW_OP_skip:
1906 case DW_OP_bra:
1907 case DW_OP_implicit_value:
1908 /* We currently don't make any attempt to make sure these are
1909 aligned properly like we do for the main unwind info, so
1910 don't support emitting things larger than a byte if we're
1911 only doing unwinding. */
1912 gcc_unreachable ();
1913 #endif
1914 case DW_OP_const1u:
1915 case DW_OP_const1s:
1916 dw2_asm_output_data (1, val1->v.val_int, NULL);
1917 break;
1918 case DW_OP_constu:
1919 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1920 break;
1921 case DW_OP_consts:
1922 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1923 break;
1924 case DW_OP_pick:
1925 dw2_asm_output_data (1, val1->v.val_int, NULL);
1926 break;
1927 case DW_OP_plus_uconst:
1928 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1929 break;
1930 case DW_OP_breg0:
1931 case DW_OP_breg1:
1932 case DW_OP_breg2:
1933 case DW_OP_breg3:
1934 case DW_OP_breg4:
1935 case DW_OP_breg5:
1936 case DW_OP_breg6:
1937 case DW_OP_breg7:
1938 case DW_OP_breg8:
1939 case DW_OP_breg9:
1940 case DW_OP_breg10:
1941 case DW_OP_breg11:
1942 case DW_OP_breg12:
1943 case DW_OP_breg13:
1944 case DW_OP_breg14:
1945 case DW_OP_breg15:
1946 case DW_OP_breg16:
1947 case DW_OP_breg17:
1948 case DW_OP_breg18:
1949 case DW_OP_breg19:
1950 case DW_OP_breg20:
1951 case DW_OP_breg21:
1952 case DW_OP_breg22:
1953 case DW_OP_breg23:
1954 case DW_OP_breg24:
1955 case DW_OP_breg25:
1956 case DW_OP_breg26:
1957 case DW_OP_breg27:
1958 case DW_OP_breg28:
1959 case DW_OP_breg29:
1960 case DW_OP_breg30:
1961 case DW_OP_breg31:
1962 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1963 break;
1964 case DW_OP_regx:
1966 unsigned r = val1->v.val_unsigned;
1967 if (for_eh_or_skip >= 0)
1968 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1969 gcc_assert (size_of_uleb128 (r)
1970 == size_of_uleb128 (val1->v.val_unsigned));
1971 dw2_asm_output_data_uleb128 (r, NULL);
1973 break;
1974 case DW_OP_fbreg:
1975 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1976 break;
1977 case DW_OP_bregx:
1979 unsigned r = val1->v.val_unsigned;
1980 if (for_eh_or_skip >= 0)
1981 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1982 gcc_assert (size_of_uleb128 (r)
1983 == size_of_uleb128 (val1->v.val_unsigned));
1984 dw2_asm_output_data_uleb128 (r, NULL);
1985 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1987 break;
1988 case DW_OP_piece:
1989 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1990 break;
1991 case DW_OP_bit_piece:
1992 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1993 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1994 break;
1995 case DW_OP_deref_size:
1996 case DW_OP_xderef_size:
1997 dw2_asm_output_data (1, val1->v.val_int, NULL);
1998 break;
2000 case DW_OP_addr:
2001 if (loc->dtprel)
2003 if (targetm.asm_out.output_dwarf_dtprel)
2005 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2006 DWARF2_ADDR_SIZE,
2007 val1->v.val_addr);
2008 fputc ('\n', asm_out_file);
2010 else
2011 gcc_unreachable ();
2013 else
2015 #ifdef DWARF2_DEBUGGING_INFO
2016 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2017 #else
2018 gcc_unreachable ();
2019 #endif
2021 break;
2023 case DW_OP_GNU_addr_index:
2024 case DW_OP_GNU_const_index:
2025 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2026 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2027 "(index into .debug_addr)");
2028 break;
2030 case DW_OP_call2:
2031 case DW_OP_call4:
2033 unsigned long die_offset
2034 = get_ref_die_offset (val1->v.val_die_ref.die);
2035 /* Make sure the offset has been computed and that we can encode it as
2036 an operand. */
2037 gcc_assert (die_offset > 0
2038 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2039 ? 0xffff
2040 : 0xffffffff));
2041 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2042 die_offset, NULL);
2044 break;
2046 case DW_OP_GNU_implicit_pointer:
2048 char label[MAX_ARTIFICIAL_LABEL_BYTES
2049 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2050 gcc_assert (val1->val_class == dw_val_class_die_ref);
2051 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2052 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2053 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2055 break;
2057 case DW_OP_GNU_entry_value:
2058 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2059 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2060 break;
2062 case DW_OP_GNU_const_type:
2064 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2065 gcc_assert (o);
2066 dw2_asm_output_data_uleb128 (o, NULL);
2067 switch (val2->val_class)
2069 case dw_val_class_const:
2070 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2071 dw2_asm_output_data (1, l, NULL);
2072 dw2_asm_output_data (l, val2->v.val_int, NULL);
2073 break;
2074 case dw_val_class_vec:
2076 unsigned int elt_size = val2->v.val_vec.elt_size;
2077 unsigned int len = val2->v.val_vec.length;
2078 unsigned int i;
2079 unsigned char *p;
2081 l = len * elt_size;
2082 dw2_asm_output_data (1, l, NULL);
2083 if (elt_size > sizeof (HOST_WIDE_INT))
2085 elt_size /= 2;
2086 len *= 2;
2088 for (i = 0, p = val2->v.val_vec.array;
2089 i < len;
2090 i++, p += elt_size)
2091 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2092 "fp or vector constant word %u", i);
2094 break;
2095 case dw_val_class_const_double:
2097 unsigned HOST_WIDE_INT first, second;
2098 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2100 dw2_asm_output_data (1, 2 * l, NULL);
2101 if (WORDS_BIG_ENDIAN)
2103 first = val2->v.val_double.high;
2104 second = val2->v.val_double.low;
2106 else
2108 first = val2->v.val_double.low;
2109 second = val2->v.val_double.high;
2111 dw2_asm_output_data (l, first, NULL);
2112 dw2_asm_output_data (l, second, NULL);
2114 break;
2115 case dw_val_class_wide_int:
2117 int i;
2118 int len = get_full_len (*val2->v.val_wide);
2119 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2121 dw2_asm_output_data (1, len * l, NULL);
2122 if (WORDS_BIG_ENDIAN)
2123 for (i = len - 1; i >= 0; --i)
2124 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2125 else
2126 for (i = 0; i < len; ++i)
2127 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2129 break;
2130 default:
2131 gcc_unreachable ();
2134 break;
2135 case DW_OP_GNU_regval_type:
2137 unsigned r = val1->v.val_unsigned;
2138 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2139 gcc_assert (o);
2140 if (for_eh_or_skip >= 0)
2142 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2143 gcc_assert (size_of_uleb128 (r)
2144 == size_of_uleb128 (val1->v.val_unsigned));
2146 dw2_asm_output_data_uleb128 (r, NULL);
2147 dw2_asm_output_data_uleb128 (o, NULL);
2149 break;
2150 case DW_OP_GNU_deref_type:
2152 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2153 gcc_assert (o);
2154 dw2_asm_output_data (1, val1->v.val_int, NULL);
2155 dw2_asm_output_data_uleb128 (o, NULL);
2157 break;
2158 case DW_OP_GNU_convert:
2159 case DW_OP_GNU_reinterpret:
2160 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2161 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2162 else
2164 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2165 gcc_assert (o);
2166 dw2_asm_output_data_uleb128 (o, NULL);
2168 break;
2170 case DW_OP_GNU_parameter_ref:
2172 unsigned long o;
2173 gcc_assert (val1->val_class == dw_val_class_die_ref);
2174 o = get_ref_die_offset (val1->v.val_die_ref.die);
2175 dw2_asm_output_data (4, o, NULL);
2177 break;
2179 default:
2180 /* Other codes have no operands. */
2181 break;
2185 /* Output a sequence of location operations.
2186 The for_eh_or_skip parameter controls whether register numbers are
2187 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2188 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2189 info). This should be suppressed for the cases that have not been converted
2190 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2192 void
2193 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2195 for (; loc != NULL; loc = loc->dw_loc_next)
2197 enum dwarf_location_atom opc = loc->dw_loc_opc;
2198 /* Output the opcode. */
2199 if (for_eh_or_skip >= 0
2200 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2202 unsigned r = (opc - DW_OP_breg0);
2203 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2204 gcc_assert (r <= 31);
2205 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2207 else if (for_eh_or_skip >= 0
2208 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2210 unsigned r = (opc - DW_OP_reg0);
2211 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2212 gcc_assert (r <= 31);
2213 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2216 dw2_asm_output_data (1, opc,
2217 "%s", dwarf_stack_op_name (opc));
2219 /* Output the operand(s) (if any). */
2220 output_loc_operands (loc, for_eh_or_skip);
2224 /* Output location description stack opcode's operands (if any).
2225 The output is single bytes on a line, suitable for .cfi_escape. */
2227 static void
2228 output_loc_operands_raw (dw_loc_descr_ref loc)
2230 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2231 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2233 switch (loc->dw_loc_opc)
2235 case DW_OP_addr:
2236 case DW_OP_GNU_addr_index:
2237 case DW_OP_GNU_const_index:
2238 case DW_OP_implicit_value:
2239 /* We cannot output addresses in .cfi_escape, only bytes. */
2240 gcc_unreachable ();
2242 case DW_OP_const1u:
2243 case DW_OP_const1s:
2244 case DW_OP_pick:
2245 case DW_OP_deref_size:
2246 case DW_OP_xderef_size:
2247 fputc (',', asm_out_file);
2248 dw2_asm_output_data_raw (1, val1->v.val_int);
2249 break;
2251 case DW_OP_const2u:
2252 case DW_OP_const2s:
2253 fputc (',', asm_out_file);
2254 dw2_asm_output_data_raw (2, val1->v.val_int);
2255 break;
2257 case DW_OP_const4u:
2258 case DW_OP_const4s:
2259 fputc (',', asm_out_file);
2260 dw2_asm_output_data_raw (4, val1->v.val_int);
2261 break;
2263 case DW_OP_const8u:
2264 case DW_OP_const8s:
2265 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2266 fputc (',', asm_out_file);
2267 dw2_asm_output_data_raw (8, val1->v.val_int);
2268 break;
2270 case DW_OP_skip:
2271 case DW_OP_bra:
2273 int offset;
2275 gcc_assert (val1->val_class == dw_val_class_loc);
2276 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2278 fputc (',', asm_out_file);
2279 dw2_asm_output_data_raw (2, offset);
2281 break;
2283 case DW_OP_regx:
2285 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2286 gcc_assert (size_of_uleb128 (r)
2287 == size_of_uleb128 (val1->v.val_unsigned));
2288 fputc (',', asm_out_file);
2289 dw2_asm_output_data_uleb128_raw (r);
2291 break;
2293 case DW_OP_constu:
2294 case DW_OP_plus_uconst:
2295 case DW_OP_piece:
2296 fputc (',', asm_out_file);
2297 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2298 break;
2300 case DW_OP_bit_piece:
2301 fputc (',', asm_out_file);
2302 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2303 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2304 break;
2306 case DW_OP_consts:
2307 case DW_OP_breg0:
2308 case DW_OP_breg1:
2309 case DW_OP_breg2:
2310 case DW_OP_breg3:
2311 case DW_OP_breg4:
2312 case DW_OP_breg5:
2313 case DW_OP_breg6:
2314 case DW_OP_breg7:
2315 case DW_OP_breg8:
2316 case DW_OP_breg9:
2317 case DW_OP_breg10:
2318 case DW_OP_breg11:
2319 case DW_OP_breg12:
2320 case DW_OP_breg13:
2321 case DW_OP_breg14:
2322 case DW_OP_breg15:
2323 case DW_OP_breg16:
2324 case DW_OP_breg17:
2325 case DW_OP_breg18:
2326 case DW_OP_breg19:
2327 case DW_OP_breg20:
2328 case DW_OP_breg21:
2329 case DW_OP_breg22:
2330 case DW_OP_breg23:
2331 case DW_OP_breg24:
2332 case DW_OP_breg25:
2333 case DW_OP_breg26:
2334 case DW_OP_breg27:
2335 case DW_OP_breg28:
2336 case DW_OP_breg29:
2337 case DW_OP_breg30:
2338 case DW_OP_breg31:
2339 case DW_OP_fbreg:
2340 fputc (',', asm_out_file);
2341 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2342 break;
2344 case DW_OP_bregx:
2346 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2347 gcc_assert (size_of_uleb128 (r)
2348 == size_of_uleb128 (val1->v.val_unsigned));
2349 fputc (',', asm_out_file);
2350 dw2_asm_output_data_uleb128_raw (r);
2351 fputc (',', asm_out_file);
2352 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2354 break;
2356 case DW_OP_GNU_implicit_pointer:
2357 case DW_OP_GNU_entry_value:
2358 case DW_OP_GNU_const_type:
2359 case DW_OP_GNU_regval_type:
2360 case DW_OP_GNU_deref_type:
2361 case DW_OP_GNU_convert:
2362 case DW_OP_GNU_reinterpret:
2363 case DW_OP_GNU_parameter_ref:
2364 gcc_unreachable ();
2365 break;
2367 default:
2368 /* Other codes have no operands. */
2369 break;
2373 void
2374 output_loc_sequence_raw (dw_loc_descr_ref loc)
2376 while (1)
2378 enum dwarf_location_atom opc = loc->dw_loc_opc;
2379 /* Output the opcode. */
2380 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2382 unsigned r = (opc - DW_OP_breg0);
2383 r = DWARF2_FRAME_REG_OUT (r, 1);
2384 gcc_assert (r <= 31);
2385 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2387 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2389 unsigned r = (opc - DW_OP_reg0);
2390 r = DWARF2_FRAME_REG_OUT (r, 1);
2391 gcc_assert (r <= 31);
2392 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2394 /* Output the opcode. */
2395 fprintf (asm_out_file, "%#x", opc);
2396 output_loc_operands_raw (loc);
2398 if (!loc->dw_loc_next)
2399 break;
2400 loc = loc->dw_loc_next;
2402 fputc (',', asm_out_file);
2406 /* This function builds a dwarf location descriptor sequence from a
2407 dw_cfa_location, adding the given OFFSET to the result of the
2408 expression. */
2410 struct dw_loc_descr_node *
2411 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2413 struct dw_loc_descr_node *head, *tmp;
2415 offset += cfa->offset;
2417 if (cfa->indirect)
2419 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2420 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2421 head->dw_loc_oprnd1.val_entry = NULL;
2422 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2423 add_loc_descr (&head, tmp);
2424 if (offset != 0)
2426 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2427 add_loc_descr (&head, tmp);
2430 else
2431 head = new_reg_loc_descr (cfa->reg, offset);
2433 return head;
2436 /* This function builds a dwarf location descriptor sequence for
2437 the address at OFFSET from the CFA when stack is aligned to
2438 ALIGNMENT byte. */
2440 struct dw_loc_descr_node *
2441 build_cfa_aligned_loc (dw_cfa_location *cfa,
2442 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2444 struct dw_loc_descr_node *head;
2445 unsigned int dwarf_fp
2446 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2448 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2449 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2451 head = new_reg_loc_descr (dwarf_fp, 0);
2452 add_loc_descr (&head, int_loc_descriptor (alignment));
2453 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2454 loc_descr_plus_const (&head, offset);
2456 else
2457 head = new_reg_loc_descr (dwarf_fp, offset);
2458 return head;
2461 /* And now, the support for symbolic debugging information. */
2463 /* .debug_str support. */
2465 static void dwarf2out_init (const char *);
2466 static void dwarf2out_finish (const char *);
2467 static void dwarf2out_early_finish (const char *);
2468 static void dwarf2out_assembly_start (void);
2469 static void dwarf2out_define (unsigned int, const char *);
2470 static void dwarf2out_undef (unsigned int, const char *);
2471 static void dwarf2out_start_source_file (unsigned, const char *);
2472 static void dwarf2out_end_source_file (unsigned);
2473 static void dwarf2out_function_decl (tree);
2474 static void dwarf2out_begin_block (unsigned, unsigned);
2475 static void dwarf2out_end_block (unsigned, unsigned);
2476 static bool dwarf2out_ignore_block (const_tree);
2477 static void dwarf2out_early_global_decl (tree);
2478 static void dwarf2out_late_global_decl (tree);
2479 static void dwarf2out_type_decl (tree, int);
2480 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2481 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2482 dw_die_ref);
2483 static void dwarf2out_abstract_function (tree);
2484 static void dwarf2out_var_location (rtx_insn *);
2485 static void dwarf2out_size_function (tree);
2486 static void dwarf2out_begin_function (tree);
2487 static void dwarf2out_end_function (unsigned int);
2488 static void dwarf2out_register_main_translation_unit (tree unit);
2489 static void dwarf2out_set_name (tree, tree);
2491 /* The debug hooks structure. */
2493 const struct gcc_debug_hooks dwarf2_debug_hooks =
2495 dwarf2out_init,
2496 dwarf2out_finish,
2497 dwarf2out_early_finish,
2498 dwarf2out_assembly_start,
2499 dwarf2out_define,
2500 dwarf2out_undef,
2501 dwarf2out_start_source_file,
2502 dwarf2out_end_source_file,
2503 dwarf2out_begin_block,
2504 dwarf2out_end_block,
2505 dwarf2out_ignore_block,
2506 dwarf2out_source_line,
2507 dwarf2out_begin_prologue,
2508 #if VMS_DEBUGGING_INFO
2509 dwarf2out_vms_end_prologue,
2510 dwarf2out_vms_begin_epilogue,
2511 #else
2512 debug_nothing_int_charstar,
2513 debug_nothing_int_charstar,
2514 #endif
2515 dwarf2out_end_epilogue,
2516 dwarf2out_begin_function,
2517 dwarf2out_end_function, /* end_function */
2518 dwarf2out_register_main_translation_unit,
2519 dwarf2out_function_decl, /* function_decl */
2520 dwarf2out_early_global_decl,
2521 dwarf2out_late_global_decl,
2522 dwarf2out_type_decl, /* type_decl */
2523 dwarf2out_imported_module_or_decl,
2524 debug_nothing_tree, /* deferred_inline_function */
2525 /* The DWARF 2 backend tries to reduce debugging bloat by not
2526 emitting the abstract description of inline functions until
2527 something tries to reference them. */
2528 dwarf2out_abstract_function, /* outlining_inline_function */
2529 debug_nothing_rtx_code_label, /* label */
2530 debug_nothing_int, /* handle_pch */
2531 dwarf2out_var_location,
2532 dwarf2out_size_function, /* size_function */
2533 dwarf2out_switch_text_section,
2534 dwarf2out_set_name,
2535 1, /* start_end_main_source_file */
2536 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2539 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2541 dwarf2out_init,
2542 debug_nothing_charstar,
2543 debug_nothing_charstar,
2544 dwarf2out_assembly_start,
2545 debug_nothing_int_charstar,
2546 debug_nothing_int_charstar,
2547 debug_nothing_int_charstar,
2548 debug_nothing_int,
2549 debug_nothing_int_int, /* begin_block */
2550 debug_nothing_int_int, /* end_block */
2551 debug_true_const_tree, /* ignore_block */
2552 dwarf2out_source_line, /* source_line */
2553 debug_nothing_int_charstar, /* begin_prologue */
2554 debug_nothing_int_charstar, /* end_prologue */
2555 debug_nothing_int_charstar, /* begin_epilogue */
2556 debug_nothing_int_charstar, /* end_epilogue */
2557 debug_nothing_tree, /* begin_function */
2558 debug_nothing_int, /* end_function */
2559 debug_nothing_tree, /* register_main_translation_unit */
2560 debug_nothing_tree, /* function_decl */
2561 debug_nothing_tree, /* early_global_decl */
2562 debug_nothing_tree, /* late_global_decl */
2563 debug_nothing_tree_int, /* type_decl */
2564 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2565 debug_nothing_tree, /* deferred_inline_function */
2566 debug_nothing_tree, /* outlining_inline_function */
2567 debug_nothing_rtx_code_label, /* label */
2568 debug_nothing_int, /* handle_pch */
2569 debug_nothing_rtx_insn, /* var_location */
2570 debug_nothing_tree, /* size_function */
2571 debug_nothing_void, /* switch_text_section */
2572 debug_nothing_tree_tree, /* set_name */
2573 0, /* start_end_main_source_file */
2574 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2577 /* NOTE: In the comments in this file, many references are made to
2578 "Debugging Information Entries". This term is abbreviated as `DIE'
2579 throughout the remainder of this file. */
2581 /* An internal representation of the DWARF output is built, and then
2582 walked to generate the DWARF debugging info. The walk of the internal
2583 representation is done after the entire program has been compiled.
2584 The types below are used to describe the internal representation. */
2586 /* Whether to put type DIEs into their own section .debug_types instead
2587 of making them part of the .debug_info section. Only supported for
2588 Dwarf V4 or higher and the user didn't disable them through
2589 -fno-debug-types-section. It is more efficient to put them in a
2590 separate comdat sections since the linker will then be able to
2591 remove duplicates. But not all tools support .debug_types sections
2592 yet. */
2594 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2596 /* Various DIE's use offsets relative to the beginning of the
2597 .debug_info section to refer to each other. */
2599 typedef long int dw_offset;
2601 struct comdat_type_node;
2603 /* The entries in the line_info table more-or-less mirror the opcodes
2604 that are used in the real dwarf line table. Arrays of these entries
2605 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2606 supported. */
2608 enum dw_line_info_opcode {
2609 /* Emit DW_LNE_set_address; the operand is the label index. */
2610 LI_set_address,
2612 /* Emit a row to the matrix with the given line. This may be done
2613 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2614 special opcodes. */
2615 LI_set_line,
2617 /* Emit a DW_LNS_set_file. */
2618 LI_set_file,
2620 /* Emit a DW_LNS_set_column. */
2621 LI_set_column,
2623 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2624 LI_negate_stmt,
2626 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2627 LI_set_prologue_end,
2628 LI_set_epilogue_begin,
2630 /* Emit a DW_LNE_set_discriminator. */
2631 LI_set_discriminator
2634 typedef struct GTY(()) dw_line_info_struct {
2635 enum dw_line_info_opcode opcode;
2636 unsigned int val;
2637 } dw_line_info_entry;
2640 struct GTY(()) dw_line_info_table {
2641 /* The label that marks the end of this section. */
2642 const char *end_label;
2644 /* The values for the last row of the matrix, as collected in the table.
2645 These are used to minimize the changes to the next row. */
2646 unsigned int file_num;
2647 unsigned int line_num;
2648 unsigned int column_num;
2649 int discrim_num;
2650 bool is_stmt;
2651 bool in_use;
2653 vec<dw_line_info_entry, va_gc> *entries;
2657 /* Each DIE attribute has a field specifying the attribute kind,
2658 a link to the next attribute in the chain, and an attribute value.
2659 Attributes are typically linked below the DIE they modify. */
2661 typedef struct GTY(()) dw_attr_struct {
2662 enum dwarf_attribute dw_attr;
2663 dw_val_node dw_attr_val;
2665 dw_attr_node;
2668 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2669 The children of each node form a circular list linked by
2670 die_sib. die_child points to the node *before* the "first" child node. */
2672 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2673 union die_symbol_or_type_node
2675 const char * GTY ((tag ("0"))) die_symbol;
2676 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2678 GTY ((desc ("%0.comdat_type_p"))) die_id;
2679 vec<dw_attr_node, va_gc> *die_attr;
2680 dw_die_ref die_parent;
2681 dw_die_ref die_child;
2682 dw_die_ref die_sib;
2683 dw_die_ref die_definition; /* ref from a specification to its definition */
2684 dw_offset die_offset;
2685 unsigned long die_abbrev;
2686 int die_mark;
2687 unsigned int decl_id;
2688 enum dwarf_tag die_tag;
2689 /* Die is used and must not be pruned as unused. */
2690 BOOL_BITFIELD die_perennial_p : 1;
2691 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2692 /* Whether this DIE was removed from the DIE tree, for example via
2693 prune_unused_types. We don't consider those present from the
2694 DIE lookup routines. */
2695 BOOL_BITFIELD removed : 1;
2696 /* Lots of spare bits. */
2698 die_node;
2700 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2701 static bool early_dwarf;
2702 static bool early_dwarf_finished;
2703 struct set_early_dwarf {
2704 bool saved;
2705 set_early_dwarf () : saved(early_dwarf)
2707 gcc_assert (! early_dwarf_finished);
2708 early_dwarf = true;
2710 ~set_early_dwarf () { early_dwarf = saved; }
2713 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2714 #define FOR_EACH_CHILD(die, c, expr) do { \
2715 c = die->die_child; \
2716 if (c) do { \
2717 c = c->die_sib; \
2718 expr; \
2719 } while (c != die->die_child); \
2720 } while (0)
2722 /* The pubname structure */
2724 typedef struct GTY(()) pubname_struct {
2725 dw_die_ref die;
2726 const char *name;
2728 pubname_entry;
2731 struct GTY(()) dw_ranges {
2732 /* If this is positive, it's a block number, otherwise it's a
2733 bitwise-negated index into dw_ranges_by_label. */
2734 int num;
2737 /* A structure to hold a macinfo entry. */
2739 typedef struct GTY(()) macinfo_struct {
2740 unsigned char code;
2741 unsigned HOST_WIDE_INT lineno;
2742 const char *info;
2744 macinfo_entry;
2747 struct GTY(()) dw_ranges_by_label {
2748 const char *begin;
2749 const char *end;
2752 /* The comdat type node structure. */
2753 struct GTY(()) comdat_type_node
2755 dw_die_ref root_die;
2756 dw_die_ref type_die;
2757 dw_die_ref skeleton_die;
2758 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2759 comdat_type_node *next;
2762 /* A list of DIEs for which we can't determine ancestry (parent_die
2763 field) just yet. Later in dwarf2out_finish we will fill in the
2764 missing bits. */
2765 typedef struct GTY(()) limbo_die_struct {
2766 dw_die_ref die;
2767 /* The tree for which this DIE was created. We use this to
2768 determine ancestry later. */
2769 tree created_for;
2770 struct limbo_die_struct *next;
2772 limbo_die_node;
2774 typedef struct skeleton_chain_struct
2776 dw_die_ref old_die;
2777 dw_die_ref new_die;
2778 struct skeleton_chain_struct *parent;
2780 skeleton_chain_node;
2782 /* Define a macro which returns nonzero for a TYPE_DECL which was
2783 implicitly generated for a type.
2785 Note that, unlike the C front-end (which generates a NULL named
2786 TYPE_DECL node for each complete tagged type, each array type,
2787 and each function type node created) the C++ front-end generates
2788 a _named_ TYPE_DECL node for each tagged type node created.
2789 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2790 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2791 front-end, but for each type, tagged or not. */
2793 #define TYPE_DECL_IS_STUB(decl) \
2794 (DECL_NAME (decl) == NULL_TREE \
2795 || (DECL_ARTIFICIAL (decl) \
2796 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2797 /* This is necessary for stub decls that \
2798 appear in nested inline functions. */ \
2799 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2800 && (decl_ultimate_origin (decl) \
2801 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2803 /* Information concerning the compilation unit's programming
2804 language, and compiler version. */
2806 /* Fixed size portion of the DWARF compilation unit header. */
2807 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2808 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2810 /* Fixed size portion of the DWARF comdat type unit header. */
2811 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2812 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2813 + DWARF_OFFSET_SIZE)
2815 /* Fixed size portion of public names info. */
2816 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2818 /* Fixed size portion of the address range info. */
2819 #define DWARF_ARANGES_HEADER_SIZE \
2820 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2821 DWARF2_ADDR_SIZE * 2) \
2822 - DWARF_INITIAL_LENGTH_SIZE)
2824 /* Size of padding portion in the address range info. It must be
2825 aligned to twice the pointer size. */
2826 #define DWARF_ARANGES_PAD_SIZE \
2827 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2828 DWARF2_ADDR_SIZE * 2) \
2829 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2831 /* Use assembler line directives if available. */
2832 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2833 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2834 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2835 #else
2836 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2837 #endif
2838 #endif
2840 /* Minimum line offset in a special line info. opcode.
2841 This value was chosen to give a reasonable range of values. */
2842 #define DWARF_LINE_BASE -10
2844 /* First special line opcode - leave room for the standard opcodes. */
2845 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2847 /* Range of line offsets in a special line info. opcode. */
2848 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2850 /* Flag that indicates the initial value of the is_stmt_start flag.
2851 In the present implementation, we do not mark any lines as
2852 the beginning of a source statement, because that information
2853 is not made available by the GCC front-end. */
2854 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2856 /* Maximum number of operations per instruction bundle. */
2857 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2858 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2859 #endif
2861 /* This location is used by calc_die_sizes() to keep track
2862 the offset of each DIE within the .debug_info section. */
2863 static unsigned long next_die_offset;
2865 /* Record the root of the DIE's built for the current compilation unit. */
2866 static GTY(()) dw_die_ref single_comp_unit_die;
2868 /* A list of type DIEs that have been separated into comdat sections. */
2869 static GTY(()) comdat_type_node *comdat_type_list;
2871 /* A list of CU DIEs that have been separated. */
2872 static GTY(()) limbo_die_node *cu_die_list;
2874 /* A list of DIEs with a NULL parent waiting to be relocated. */
2875 static GTY(()) limbo_die_node *limbo_die_list;
2877 /* A list of DIEs for which we may have to generate
2878 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2879 static GTY(()) limbo_die_node *deferred_asm_name;
2881 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2883 typedef const char *compare_type;
2885 static hashval_t hash (dwarf_file_data *);
2886 static bool equal (dwarf_file_data *, const char *);
2889 /* Filenames referenced by this compilation unit. */
2890 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2892 struct decl_die_hasher : ggc_ptr_hash<die_node>
2894 typedef tree compare_type;
2896 static hashval_t hash (die_node *);
2897 static bool equal (die_node *, tree);
2899 /* A hash table of references to DIE's that describe declarations.
2900 The key is a DECL_UID() which is a unique number identifying each decl. */
2901 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2903 struct block_die_hasher : ggc_ptr_hash<die_struct>
2905 static hashval_t hash (die_struct *);
2906 static bool equal (die_struct *, die_struct *);
2909 /* A hash table of references to DIE's that describe COMMON blocks.
2910 The key is DECL_UID() ^ die_parent. */
2911 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2913 typedef struct GTY(()) die_arg_entry_struct {
2914 dw_die_ref die;
2915 tree arg;
2916 } die_arg_entry;
2919 /* Node of the variable location list. */
2920 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2921 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2922 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2923 in mode of the EXPR_LIST node and first EXPR_LIST operand
2924 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2925 location or NULL for padding. For larger bitsizes,
2926 mode is 0 and first operand is a CONCAT with bitsize
2927 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2928 NULL as second operand. */
2929 rtx GTY (()) loc;
2930 const char * GTY (()) label;
2931 struct var_loc_node * GTY (()) next;
2934 /* Variable location list. */
2935 struct GTY ((for_user)) var_loc_list_def {
2936 struct var_loc_node * GTY (()) first;
2938 /* Pointer to the last but one or last element of the
2939 chained list. If the list is empty, both first and
2940 last are NULL, if the list contains just one node
2941 or the last node certainly is not redundant, it points
2942 to the last node, otherwise points to the last but one.
2943 Do not mark it for GC because it is marked through the chain. */
2944 struct var_loc_node * GTY ((skip ("%h"))) last;
2946 /* Pointer to the last element before section switch,
2947 if NULL, either sections weren't switched or first
2948 is after section switch. */
2949 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2951 /* DECL_UID of the variable decl. */
2952 unsigned int decl_id;
2954 typedef struct var_loc_list_def var_loc_list;
2956 /* Call argument location list. */
2957 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2958 rtx GTY (()) call_arg_loc_note;
2959 const char * GTY (()) label;
2960 tree GTY (()) block;
2961 bool tail_call_p;
2962 rtx GTY (()) symbol_ref;
2963 struct call_arg_loc_node * GTY (()) next;
2967 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2969 typedef const_tree compare_type;
2971 static hashval_t hash (var_loc_list *);
2972 static bool equal (var_loc_list *, const_tree);
2975 /* Table of decl location linked lists. */
2976 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2978 /* Head and tail of call_arg_loc chain. */
2979 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2980 static struct call_arg_loc_node *call_arg_loc_last;
2982 /* Number of call sites in the current function. */
2983 static int call_site_count = -1;
2984 /* Number of tail call sites in the current function. */
2985 static int tail_call_site_count = -1;
2987 /* A cached location list. */
2988 struct GTY ((for_user)) cached_dw_loc_list_def {
2989 /* The DECL_UID of the decl that this entry describes. */
2990 unsigned int decl_id;
2992 /* The cached location list. */
2993 dw_loc_list_ref loc_list;
2995 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2997 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3000 typedef const_tree compare_type;
3002 static hashval_t hash (cached_dw_loc_list *);
3003 static bool equal (cached_dw_loc_list *, const_tree);
3006 /* Table of cached location lists. */
3007 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3009 /* A pointer to the base of a list of references to DIE's that
3010 are uniquely identified by their tag, presence/absence of
3011 children DIE's, and list of attribute/value pairs. */
3012 static GTY((length ("abbrev_die_table_allocated")))
3013 dw_die_ref *abbrev_die_table;
3015 /* Number of elements currently allocated for abbrev_die_table. */
3016 static GTY(()) unsigned abbrev_die_table_allocated;
3018 /* Number of elements in abbrev_die_table currently in use. */
3019 static GTY(()) unsigned abbrev_die_table_in_use;
3021 /* A hash map to remember the stack usage for DWARF procedures. The value
3022 stored is the stack size difference between before the DWARF procedure
3023 invokation and after it returned. In other words, for a DWARF procedure
3024 that consumes N stack slots and that pushes M ones, this stores M - N. */
3025 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3027 /* Size (in elements) of increments by which we may expand the
3028 abbrev_die_table. */
3029 #define ABBREV_DIE_TABLE_INCREMENT 256
3031 /* A global counter for generating labels for line number data. */
3032 static unsigned int line_info_label_num;
3034 /* The current table to which we should emit line number information
3035 for the current function. This will be set up at the beginning of
3036 assembly for the function. */
3037 static GTY(()) dw_line_info_table *cur_line_info_table;
3039 /* The two default tables of line number info. */
3040 static GTY(()) dw_line_info_table *text_section_line_info;
3041 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3043 /* The set of all non-default tables of line number info. */
3044 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3046 /* A flag to tell pubnames/types export if there is an info section to
3047 refer to. */
3048 static bool info_section_emitted;
3050 /* A pointer to the base of a table that contains a list of publicly
3051 accessible names. */
3052 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3054 /* A pointer to the base of a table that contains a list of publicly
3055 accessible types. */
3056 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3058 /* A pointer to the base of a table that contains a list of macro
3059 defines/undefines (and file start/end markers). */
3060 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3062 /* True if .debug_macinfo or .debug_macros section is going to be
3063 emitted. */
3064 #define have_macinfo \
3065 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3066 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3067 && !macinfo_table->is_empty ())
3069 /* Vector of dies for which we should generate .debug_ranges info. */
3070 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3072 /* Vector of pairs of labels referenced in ranges_table. */
3073 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3075 /* Whether we have location lists that need outputting */
3076 static GTY(()) bool have_location_lists;
3078 /* Unique label counter. */
3079 static GTY(()) unsigned int loclabel_num;
3081 /* Unique label counter for point-of-call tables. */
3082 static GTY(()) unsigned int poc_label_num;
3084 /* The last file entry emitted by maybe_emit_file(). */
3085 static GTY(()) struct dwarf_file_data * last_emitted_file;
3087 /* Number of internal labels generated by gen_internal_sym(). */
3088 static GTY(()) int label_num;
3090 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3092 /* Instances of generic types for which we need to generate debug
3093 info that describe their generic parameters and arguments. That
3094 generation needs to happen once all types are properly laid out so
3095 we do it at the end of compilation. */
3096 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3098 /* Offset from the "steady-state frame pointer" to the frame base,
3099 within the current function. */
3100 static HOST_WIDE_INT frame_pointer_fb_offset;
3101 static bool frame_pointer_fb_offset_valid;
3103 static vec<dw_die_ref> base_types;
3105 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3106 once all arguments are parsed. */
3107 static vec<dw_die_ref> *string_types;
3109 /* Flags to represent a set of attribute classes for attributes that represent
3110 a scalar value (bounds, pointers, ...). */
3111 enum dw_scalar_form
3113 dw_scalar_form_constant = 0x01,
3114 dw_scalar_form_exprloc = 0x02,
3115 dw_scalar_form_reference = 0x04
3118 /* Forward declarations for functions defined in this file. */
3120 static int is_pseudo_reg (const_rtx);
3121 static tree type_main_variant (tree);
3122 static int is_tagged_type (const_tree);
3123 static const char *dwarf_tag_name (unsigned);
3124 static const char *dwarf_attr_name (unsigned);
3125 static const char *dwarf_form_name (unsigned);
3126 static tree decl_ultimate_origin (const_tree);
3127 static tree decl_class_context (tree);
3128 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3129 static inline enum dw_val_class AT_class (dw_attr_node *);
3130 static inline unsigned int AT_index (dw_attr_node *);
3131 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3132 static inline unsigned AT_flag (dw_attr_node *);
3133 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3134 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3135 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3136 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3137 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3138 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3139 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3140 unsigned int, unsigned char *);
3141 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3142 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3143 static inline const char *AT_string (dw_attr_node *);
3144 static enum dwarf_form AT_string_form (dw_attr_node *);
3145 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3146 static void add_AT_specification (dw_die_ref, dw_die_ref);
3147 static inline dw_die_ref AT_ref (dw_attr_node *);
3148 static inline int AT_ref_external (dw_attr_node *);
3149 static inline void set_AT_ref_external (dw_attr_node *, int);
3150 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3151 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3152 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3153 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3154 dw_loc_list_ref);
3155 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3156 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3157 static void remove_addr_table_entry (addr_table_entry *);
3158 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3159 static inline rtx AT_addr (dw_attr_node *);
3160 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3161 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3162 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3163 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3164 unsigned HOST_WIDE_INT);
3165 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3166 unsigned long, bool);
3167 static inline const char *AT_lbl (dw_attr_node *);
3168 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3169 static const char *get_AT_low_pc (dw_die_ref);
3170 static const char *get_AT_hi_pc (dw_die_ref);
3171 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3172 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3173 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3174 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3175 static bool is_cxx (void);
3176 static bool is_fortran (void);
3177 static bool is_ada (void);
3178 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3179 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3180 static void add_child_die (dw_die_ref, dw_die_ref);
3181 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3182 static dw_die_ref lookup_type_die (tree);
3183 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3184 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3185 static void equate_type_number_to_die (tree, dw_die_ref);
3186 static dw_die_ref lookup_decl_die (tree);
3187 static var_loc_list *lookup_decl_loc (const_tree);
3188 static void equate_decl_number_to_die (tree, dw_die_ref);
3189 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3190 static void print_spaces (FILE *);
3191 static void print_die (dw_die_ref, FILE *);
3192 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3193 static dw_die_ref pop_compile_unit (dw_die_ref);
3194 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3195 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3196 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3197 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3198 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3199 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3200 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3201 struct md5_ctx *, int *);
3202 struct checksum_attributes;
3203 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3204 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3205 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3206 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3207 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3208 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3209 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3210 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3211 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3212 static void compute_section_prefix (dw_die_ref);
3213 static int is_type_die (dw_die_ref);
3214 static int is_comdat_die (dw_die_ref);
3215 static int is_symbol_die (dw_die_ref);
3216 static inline bool is_template_instantiation (dw_die_ref);
3217 static void assign_symbol_names (dw_die_ref);
3218 static void break_out_includes (dw_die_ref);
3219 static int is_declaration_die (dw_die_ref);
3220 static int should_move_die_to_comdat (dw_die_ref);
3221 static dw_die_ref clone_as_declaration (dw_die_ref);
3222 static dw_die_ref clone_die (dw_die_ref);
3223 static dw_die_ref clone_tree (dw_die_ref);
3224 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3225 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3226 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3227 static dw_die_ref generate_skeleton (dw_die_ref);
3228 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3229 dw_die_ref,
3230 dw_die_ref);
3231 static void break_out_comdat_types (dw_die_ref);
3232 static void copy_decls_for_unworthy_types (dw_die_ref);
3234 static void add_sibling_attributes (dw_die_ref);
3235 static void output_location_lists (dw_die_ref);
3236 static int constant_size (unsigned HOST_WIDE_INT);
3237 static unsigned long size_of_die (dw_die_ref);
3238 static void calc_die_sizes (dw_die_ref);
3239 static void calc_base_type_die_sizes (void);
3240 static void mark_dies (dw_die_ref);
3241 static void unmark_dies (dw_die_ref);
3242 static void unmark_all_dies (dw_die_ref);
3243 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3244 static unsigned long size_of_aranges (void);
3245 static enum dwarf_form value_format (dw_attr_node *);
3246 static void output_value_format (dw_attr_node *);
3247 static void output_abbrev_section (void);
3248 static void output_die_abbrevs (unsigned long, dw_die_ref);
3249 static void output_die_symbol (dw_die_ref);
3250 static void output_die (dw_die_ref);
3251 static void output_compilation_unit_header (void);
3252 static void output_comp_unit (dw_die_ref, int);
3253 static void output_comdat_type_unit (comdat_type_node *);
3254 static const char *dwarf2_name (tree, int);
3255 static void add_pubname (tree, dw_die_ref);
3256 static void add_enumerator_pubname (const char *, dw_die_ref);
3257 static void add_pubname_string (const char *, dw_die_ref);
3258 static void add_pubtype (tree, dw_die_ref);
3259 static void output_pubnames (vec<pubname_entry, va_gc> *);
3260 static void output_aranges (void);
3261 static unsigned int add_ranges_num (int);
3262 static unsigned int add_ranges (const_tree);
3263 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3264 bool *, bool);
3265 static void output_ranges (void);
3266 static dw_line_info_table *new_line_info_table (void);
3267 static void output_line_info (bool);
3268 static void output_file_names (void);
3269 static dw_die_ref base_type_die (tree, bool);
3270 static int is_base_type (tree);
3271 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3272 static int decl_quals (const_tree);
3273 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3274 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3275 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3276 static int type_is_enum (const_tree);
3277 static unsigned int dbx_reg_number (const_rtx);
3278 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3279 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3280 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3281 enum var_init_status);
3282 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3283 enum var_init_status);
3284 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3285 enum var_init_status);
3286 static int is_based_loc (const_rtx);
3287 static bool resolve_one_addr (rtx *);
3288 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3289 enum var_init_status);
3290 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3291 enum var_init_status);
3292 struct loc_descr_context;
3293 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3294 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3295 static dw_loc_list_ref loc_list_from_tree (tree, int,
3296 const struct loc_descr_context *);
3297 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3298 const struct loc_descr_context *);
3299 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3300 static tree field_type (const_tree);
3301 static unsigned int simple_type_align_in_bits (const_tree);
3302 static unsigned int simple_decl_align_in_bits (const_tree);
3303 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3304 struct vlr_context;
3305 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3306 HOST_WIDE_INT *);
3307 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3308 dw_loc_list_ref);
3309 static void add_data_member_location_attribute (dw_die_ref, tree,
3310 struct vlr_context *);
3311 static bool add_const_value_attribute (dw_die_ref, rtx);
3312 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3313 static void insert_wide_int (const wide_int &, unsigned char *, int);
3314 static void insert_float (const_rtx, unsigned char *);
3315 static rtx rtl_for_decl_location (tree);
3316 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3317 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3318 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3319 static void add_name_attribute (dw_die_ref, const char *);
3320 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3321 static void add_comp_dir_attribute (dw_die_ref);
3322 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3323 const struct loc_descr_context *);
3324 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3325 const struct loc_descr_context *);
3326 static void add_subscript_info (dw_die_ref, tree, bool);
3327 static void add_byte_size_attribute (dw_die_ref, tree);
3328 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3329 struct vlr_context *);
3330 static void add_bit_size_attribute (dw_die_ref, tree);
3331 static void add_prototyped_attribute (dw_die_ref, tree);
3332 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3333 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3334 static void add_src_coords_attributes (dw_die_ref, tree);
3335 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3336 static void add_discr_value (dw_die_ref, dw_discr_value *);
3337 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3338 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3339 static void push_decl_scope (tree);
3340 static void pop_decl_scope (void);
3341 static dw_die_ref scope_die_for (tree, dw_die_ref);
3342 static inline int local_scope_p (dw_die_ref);
3343 static inline int class_scope_p (dw_die_ref);
3344 static inline int class_or_namespace_scope_p (dw_die_ref);
3345 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3346 static void add_calling_convention_attribute (dw_die_ref, tree);
3347 static const char *type_tag (const_tree);
3348 static tree member_declared_type (const_tree);
3349 #if 0
3350 static const char *decl_start_label (tree);
3351 #endif
3352 static void gen_array_type_die (tree, dw_die_ref);
3353 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3354 #if 0
3355 static void gen_entry_point_die (tree, dw_die_ref);
3356 #endif
3357 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3358 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3359 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3360 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3361 static void gen_formal_types_die (tree, dw_die_ref);
3362 static void gen_subprogram_die (tree, dw_die_ref);
3363 static void gen_variable_die (tree, tree, dw_die_ref);
3364 static void gen_const_die (tree, dw_die_ref);
3365 static void gen_label_die (tree, dw_die_ref);
3366 static void gen_lexical_block_die (tree, dw_die_ref);
3367 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3368 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3369 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3370 static dw_die_ref gen_compile_unit_die (const char *);
3371 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3372 static void gen_member_die (tree, dw_die_ref);
3373 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3374 enum debug_info_usage);
3375 static void gen_subroutine_type_die (tree, dw_die_ref);
3376 static void gen_typedef_die (tree, dw_die_ref);
3377 static void gen_type_die (tree, dw_die_ref);
3378 static void gen_block_die (tree, dw_die_ref);
3379 static void decls_for_scope (tree, dw_die_ref);
3380 static bool is_naming_typedef_decl (const_tree);
3381 static inline dw_die_ref get_context_die (tree);
3382 static void gen_namespace_die (tree, dw_die_ref);
3383 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3384 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3385 static dw_die_ref force_decl_die (tree);
3386 static dw_die_ref force_type_die (tree);
3387 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3388 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3389 static struct dwarf_file_data * lookup_filename (const char *);
3390 static void retry_incomplete_types (void);
3391 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3392 static void gen_generic_params_dies (tree);
3393 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3394 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3395 static void splice_child_die (dw_die_ref, dw_die_ref);
3396 static int file_info_cmp (const void *, const void *);
3397 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3398 const char *, const char *);
3399 static void output_loc_list (dw_loc_list_ref);
3400 static char *gen_internal_sym (const char *);
3401 static bool want_pubnames (void);
3403 static void prune_unmark_dies (dw_die_ref);
3404 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3405 static void prune_unused_types_mark (dw_die_ref, int);
3406 static void prune_unused_types_walk (dw_die_ref);
3407 static void prune_unused_types_walk_attribs (dw_die_ref);
3408 static void prune_unused_types_prune (dw_die_ref);
3409 static void prune_unused_types (void);
3410 static int maybe_emit_file (struct dwarf_file_data *fd);
3411 static inline const char *AT_vms_delta1 (dw_attr_node *);
3412 static inline const char *AT_vms_delta2 (dw_attr_node *);
3413 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3414 const char *, const char *);
3415 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3416 static void gen_remaining_tmpl_value_param_die_attribute (void);
3417 static bool generic_type_p (tree);
3418 static void schedule_generic_params_dies_gen (tree t);
3419 static void gen_scheduled_generic_parms_dies (void);
3421 static const char *comp_dir_string (void);
3423 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3425 /* enum for tracking thread-local variables whose address is really an offset
3426 relative to the TLS pointer, which will need link-time relocation, but will
3427 not need relocation by the DWARF consumer. */
3429 enum dtprel_bool
3431 dtprel_false = 0,
3432 dtprel_true = 1
3435 /* Return the operator to use for an address of a variable. For dtprel_true, we
3436 use DW_OP_const*. For regular variables, which need both link-time
3437 relocation and consumer-level relocation (e.g., to account for shared objects
3438 loaded at a random address), we use DW_OP_addr*. */
3440 static inline enum dwarf_location_atom
3441 dw_addr_op (enum dtprel_bool dtprel)
3443 if (dtprel == dtprel_true)
3444 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3445 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3446 else
3447 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3450 /* Return a pointer to a newly allocated address location description. If
3451 dwarf_split_debug_info is true, then record the address with the appropriate
3452 relocation. */
3453 static inline dw_loc_descr_ref
3454 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3456 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3458 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3459 ref->dw_loc_oprnd1.v.val_addr = addr;
3460 ref->dtprel = dtprel;
3461 if (dwarf_split_debug_info)
3462 ref->dw_loc_oprnd1.val_entry
3463 = add_addr_table_entry (addr,
3464 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3465 else
3466 ref->dw_loc_oprnd1.val_entry = NULL;
3468 return ref;
3471 /* Section names used to hold DWARF debugging information. */
3473 #ifndef DEBUG_INFO_SECTION
3474 #define DEBUG_INFO_SECTION ".debug_info"
3475 #endif
3476 #ifndef DEBUG_DWO_INFO_SECTION
3477 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3478 #endif
3479 #ifndef DEBUG_ABBREV_SECTION
3480 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3481 #endif
3482 #ifndef DEBUG_DWO_ABBREV_SECTION
3483 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3484 #endif
3485 #ifndef DEBUG_ARANGES_SECTION
3486 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3487 #endif
3488 #ifndef DEBUG_ADDR_SECTION
3489 #define DEBUG_ADDR_SECTION ".debug_addr"
3490 #endif
3491 #ifndef DEBUG_MACINFO_SECTION
3492 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3493 #endif
3494 #ifndef DEBUG_DWO_MACINFO_SECTION
3495 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3496 #endif
3497 #ifndef DEBUG_DWO_MACRO_SECTION
3498 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3499 #endif
3500 #ifndef DEBUG_MACRO_SECTION
3501 #define DEBUG_MACRO_SECTION ".debug_macro"
3502 #endif
3503 #ifndef DEBUG_LINE_SECTION
3504 #define DEBUG_LINE_SECTION ".debug_line"
3505 #endif
3506 #ifndef DEBUG_DWO_LINE_SECTION
3507 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3508 #endif
3509 #ifndef DEBUG_LOC_SECTION
3510 #define DEBUG_LOC_SECTION ".debug_loc"
3511 #endif
3512 #ifndef DEBUG_DWO_LOC_SECTION
3513 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3514 #endif
3515 #ifndef DEBUG_PUBNAMES_SECTION
3516 #define DEBUG_PUBNAMES_SECTION \
3517 ((debug_generate_pub_sections == 2) \
3518 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3519 #endif
3520 #ifndef DEBUG_PUBTYPES_SECTION
3521 #define DEBUG_PUBTYPES_SECTION \
3522 ((debug_generate_pub_sections == 2) \
3523 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3524 #endif
3525 #ifndef DEBUG_STR_OFFSETS_SECTION
3526 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3527 #endif
3528 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3529 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3530 #endif
3531 #ifndef DEBUG_STR_DWO_SECTION
3532 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3533 #endif
3534 #ifndef DEBUG_STR_SECTION
3535 #define DEBUG_STR_SECTION ".debug_str"
3536 #endif
3537 #ifndef DEBUG_RANGES_SECTION
3538 #define DEBUG_RANGES_SECTION ".debug_ranges"
3539 #endif
3541 /* Standard ELF section names for compiled code and data. */
3542 #ifndef TEXT_SECTION_NAME
3543 #define TEXT_SECTION_NAME ".text"
3544 #endif
3546 /* Section flags for .debug_str section. */
3547 #define DEBUG_STR_SECTION_FLAGS \
3548 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3549 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3550 : SECTION_DEBUG)
3552 /* Section flags for .debug_str.dwo section. */
3553 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3555 /* Labels we insert at beginning sections we can reference instead of
3556 the section names themselves. */
3558 #ifndef TEXT_SECTION_LABEL
3559 #define TEXT_SECTION_LABEL "Ltext"
3560 #endif
3561 #ifndef COLD_TEXT_SECTION_LABEL
3562 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3563 #endif
3564 #ifndef DEBUG_LINE_SECTION_LABEL
3565 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3566 #endif
3567 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3568 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3569 #endif
3570 #ifndef DEBUG_INFO_SECTION_LABEL
3571 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3572 #endif
3573 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3574 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3575 #endif
3576 #ifndef DEBUG_ABBREV_SECTION_LABEL
3577 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3578 #endif
3579 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3580 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3581 #endif
3582 #ifndef DEBUG_ADDR_SECTION_LABEL
3583 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3584 #endif
3585 #ifndef DEBUG_LOC_SECTION_LABEL
3586 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3587 #endif
3588 #ifndef DEBUG_RANGES_SECTION_LABEL
3589 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3590 #endif
3591 #ifndef DEBUG_MACINFO_SECTION_LABEL
3592 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3593 #endif
3594 #ifndef DEBUG_MACRO_SECTION_LABEL
3595 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3596 #endif
3597 #define SKELETON_COMP_DIE_ABBREV 1
3598 #define SKELETON_TYPE_DIE_ABBREV 2
3600 /* Definitions of defaults for formats and names of various special
3601 (artificial) labels which may be generated within this file (when the -g
3602 options is used and DWARF2_DEBUGGING_INFO is in effect.
3603 If necessary, these may be overridden from within the tm.h file, but
3604 typically, overriding these defaults is unnecessary. */
3606 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3607 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3608 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3609 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3610 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3611 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3612 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3613 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3614 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3615 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3616 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3617 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3618 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3619 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3621 #ifndef TEXT_END_LABEL
3622 #define TEXT_END_LABEL "Letext"
3623 #endif
3624 #ifndef COLD_END_LABEL
3625 #define COLD_END_LABEL "Letext_cold"
3626 #endif
3627 #ifndef BLOCK_BEGIN_LABEL
3628 #define BLOCK_BEGIN_LABEL "LBB"
3629 #endif
3630 #ifndef BLOCK_END_LABEL
3631 #define BLOCK_END_LABEL "LBE"
3632 #endif
3633 #ifndef LINE_CODE_LABEL
3634 #define LINE_CODE_LABEL "LM"
3635 #endif
3638 /* Return the root of the DIE's built for the current compilation unit. */
3639 static dw_die_ref
3640 comp_unit_die (void)
3642 if (!single_comp_unit_die)
3643 single_comp_unit_die = gen_compile_unit_die (NULL);
3644 return single_comp_unit_die;
3647 /* We allow a language front-end to designate a function that is to be
3648 called to "demangle" any name before it is put into a DIE. */
3650 static const char *(*demangle_name_func) (const char *);
3652 void
3653 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3655 demangle_name_func = func;
3658 /* Test if rtl node points to a pseudo register. */
3660 static inline int
3661 is_pseudo_reg (const_rtx rtl)
3663 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3664 || (GET_CODE (rtl) == SUBREG
3665 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3668 /* Return a reference to a type, with its const and volatile qualifiers
3669 removed. */
3671 static inline tree
3672 type_main_variant (tree type)
3674 type = TYPE_MAIN_VARIANT (type);
3676 /* ??? There really should be only one main variant among any group of
3677 variants of a given type (and all of the MAIN_VARIANT values for all
3678 members of the group should point to that one type) but sometimes the C
3679 front-end messes this up for array types, so we work around that bug
3680 here. */
3681 if (TREE_CODE (type) == ARRAY_TYPE)
3682 while (type != TYPE_MAIN_VARIANT (type))
3683 type = TYPE_MAIN_VARIANT (type);
3685 return type;
3688 /* Return nonzero if the given type node represents a tagged type. */
3690 static inline int
3691 is_tagged_type (const_tree type)
3693 enum tree_code code = TREE_CODE (type);
3695 return (code == RECORD_TYPE || code == UNION_TYPE
3696 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3699 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3701 static void
3702 get_ref_die_offset_label (char *label, dw_die_ref ref)
3704 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3707 /* Return die_offset of a DIE reference to a base type. */
3709 static unsigned long int
3710 get_base_type_offset (dw_die_ref ref)
3712 if (ref->die_offset)
3713 return ref->die_offset;
3714 if (comp_unit_die ()->die_abbrev)
3716 calc_base_type_die_sizes ();
3717 gcc_assert (ref->die_offset);
3719 return ref->die_offset;
3722 /* Return die_offset of a DIE reference other than base type. */
3724 static unsigned long int
3725 get_ref_die_offset (dw_die_ref ref)
3727 gcc_assert (ref->die_offset);
3728 return ref->die_offset;
3731 /* Convert a DIE tag into its string name. */
3733 static const char *
3734 dwarf_tag_name (unsigned int tag)
3736 const char *name = get_DW_TAG_name (tag);
3738 if (name != NULL)
3739 return name;
3741 return "DW_TAG_<unknown>";
3744 /* Convert a DWARF attribute code into its string name. */
3746 static const char *
3747 dwarf_attr_name (unsigned int attr)
3749 const char *name;
3751 switch (attr)
3753 #if VMS_DEBUGGING_INFO
3754 case DW_AT_HP_prologue:
3755 return "DW_AT_HP_prologue";
3756 #else
3757 case DW_AT_MIPS_loop_unroll_factor:
3758 return "DW_AT_MIPS_loop_unroll_factor";
3759 #endif
3761 #if VMS_DEBUGGING_INFO
3762 case DW_AT_HP_epilogue:
3763 return "DW_AT_HP_epilogue";
3764 #else
3765 case DW_AT_MIPS_stride:
3766 return "DW_AT_MIPS_stride";
3767 #endif
3770 name = get_DW_AT_name (attr);
3772 if (name != NULL)
3773 return name;
3775 return "DW_AT_<unknown>";
3778 /* Convert a DWARF value form code into its string name. */
3780 static const char *
3781 dwarf_form_name (unsigned int form)
3783 const char *name = get_DW_FORM_name (form);
3785 if (name != NULL)
3786 return name;
3788 return "DW_FORM_<unknown>";
3791 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3792 instance of an inlined instance of a decl which is local to an inline
3793 function, so we have to trace all of the way back through the origin chain
3794 to find out what sort of node actually served as the original seed for the
3795 given block. */
3797 static tree
3798 decl_ultimate_origin (const_tree decl)
3800 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3801 return NULL_TREE;
3803 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3804 we're trying to output the abstract instance of this function. */
3805 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3806 return NULL_TREE;
3808 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3809 most distant ancestor, this should never happen. */
3810 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3812 return DECL_ABSTRACT_ORIGIN (decl);
3815 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3816 of a virtual function may refer to a base class, so we check the 'this'
3817 parameter. */
3819 static tree
3820 decl_class_context (tree decl)
3822 tree context = NULL_TREE;
3824 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3825 context = DECL_CONTEXT (decl);
3826 else
3827 context = TYPE_MAIN_VARIANT
3828 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3830 if (context && !TYPE_P (context))
3831 context = NULL_TREE;
3833 return context;
3836 /* Add an attribute/value pair to a DIE. */
3838 static inline void
3839 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3841 /* Maybe this should be an assert? */
3842 if (die == NULL)
3843 return;
3845 vec_safe_reserve (die->die_attr, 1);
3846 vec_safe_push (die->die_attr, *attr);
3849 static inline enum dw_val_class
3850 AT_class (dw_attr_node *a)
3852 return a->dw_attr_val.val_class;
3855 /* Return the index for any attribute that will be referenced with a
3856 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3857 are stored in dw_attr_val.v.val_str for reference counting
3858 pruning. */
3860 static inline unsigned int
3861 AT_index (dw_attr_node *a)
3863 if (AT_class (a) == dw_val_class_str)
3864 return a->dw_attr_val.v.val_str->index;
3865 else if (a->dw_attr_val.val_entry != NULL)
3866 return a->dw_attr_val.val_entry->index;
3867 return NOT_INDEXED;
3870 /* Add a flag value attribute to a DIE. */
3872 static inline void
3873 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3875 dw_attr_node attr;
3877 attr.dw_attr = attr_kind;
3878 attr.dw_attr_val.val_class = dw_val_class_flag;
3879 attr.dw_attr_val.val_entry = NULL;
3880 attr.dw_attr_val.v.val_flag = flag;
3881 add_dwarf_attr (die, &attr);
3884 static inline unsigned
3885 AT_flag (dw_attr_node *a)
3887 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3888 return a->dw_attr_val.v.val_flag;
3891 /* Add a signed integer attribute value to a DIE. */
3893 static inline void
3894 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3896 dw_attr_node attr;
3898 attr.dw_attr = attr_kind;
3899 attr.dw_attr_val.val_class = dw_val_class_const;
3900 attr.dw_attr_val.val_entry = NULL;
3901 attr.dw_attr_val.v.val_int = int_val;
3902 add_dwarf_attr (die, &attr);
3905 static inline HOST_WIDE_INT
3906 AT_int (dw_attr_node *a)
3908 gcc_assert (a && AT_class (a) == dw_val_class_const);
3909 return a->dw_attr_val.v.val_int;
3912 /* Add an unsigned integer attribute value to a DIE. */
3914 static inline void
3915 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3916 unsigned HOST_WIDE_INT unsigned_val)
3918 dw_attr_node attr;
3920 attr.dw_attr = attr_kind;
3921 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3922 attr.dw_attr_val.val_entry = NULL;
3923 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3924 add_dwarf_attr (die, &attr);
3927 static inline unsigned HOST_WIDE_INT
3928 AT_unsigned (dw_attr_node *a)
3930 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3931 return a->dw_attr_val.v.val_unsigned;
3934 /* Add an unsigned wide integer attribute value to a DIE. */
3936 static inline void
3937 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3938 const wide_int& w)
3940 dw_attr_node attr;
3942 attr.dw_attr = attr_kind;
3943 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3944 attr.dw_attr_val.val_entry = NULL;
3945 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3946 *attr.dw_attr_val.v.val_wide = w;
3947 add_dwarf_attr (die, &attr);
3950 /* Add an unsigned double integer attribute value to a DIE. */
3952 static inline void
3953 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3954 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3956 dw_attr_node attr;
3958 attr.dw_attr = attr_kind;
3959 attr.dw_attr_val.val_class = dw_val_class_const_double;
3960 attr.dw_attr_val.val_entry = NULL;
3961 attr.dw_attr_val.v.val_double.high = high;
3962 attr.dw_attr_val.v.val_double.low = low;
3963 add_dwarf_attr (die, &attr);
3966 /* Add a floating point attribute value to a DIE and return it. */
3968 static inline void
3969 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3970 unsigned int length, unsigned int elt_size, unsigned char *array)
3972 dw_attr_node attr;
3974 attr.dw_attr = attr_kind;
3975 attr.dw_attr_val.val_class = dw_val_class_vec;
3976 attr.dw_attr_val.val_entry = NULL;
3977 attr.dw_attr_val.v.val_vec.length = length;
3978 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3979 attr.dw_attr_val.v.val_vec.array = array;
3980 add_dwarf_attr (die, &attr);
3983 /* Add an 8-byte data attribute value to a DIE. */
3985 static inline void
3986 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3987 unsigned char data8[8])
3989 dw_attr_node attr;
3991 attr.dw_attr = attr_kind;
3992 attr.dw_attr_val.val_class = dw_val_class_data8;
3993 attr.dw_attr_val.val_entry = NULL;
3994 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3995 add_dwarf_attr (die, &attr);
3998 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3999 dwarf_split_debug_info, address attributes in dies destined for the
4000 final executable have force_direct set to avoid using indexed
4001 references. */
4003 static inline void
4004 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4005 bool force_direct)
4007 dw_attr_node attr;
4008 char * lbl_id;
4010 lbl_id = xstrdup (lbl_low);
4011 attr.dw_attr = DW_AT_low_pc;
4012 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4013 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4014 if (dwarf_split_debug_info && !force_direct)
4015 attr.dw_attr_val.val_entry
4016 = add_addr_table_entry (lbl_id, ate_kind_label);
4017 else
4018 attr.dw_attr_val.val_entry = NULL;
4019 add_dwarf_attr (die, &attr);
4021 attr.dw_attr = DW_AT_high_pc;
4022 if (dwarf_version < 4)
4023 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4024 else
4025 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4026 lbl_id = xstrdup (lbl_high);
4027 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4028 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4029 && dwarf_split_debug_info && !force_direct)
4030 attr.dw_attr_val.val_entry
4031 = add_addr_table_entry (lbl_id, ate_kind_label);
4032 else
4033 attr.dw_attr_val.val_entry = NULL;
4034 add_dwarf_attr (die, &attr);
4037 /* Hash and equality functions for debug_str_hash. */
4039 hashval_t
4040 indirect_string_hasher::hash (indirect_string_node *x)
4042 return htab_hash_string (x->str);
4045 bool
4046 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4048 return strcmp (x1->str, x2) == 0;
4051 /* Add STR to the given string hash table. */
4053 static struct indirect_string_node *
4054 find_AT_string_in_table (const char *str,
4055 hash_table<indirect_string_hasher> *table)
4057 struct indirect_string_node *node;
4059 indirect_string_node **slot
4060 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4061 if (*slot == NULL)
4063 node = ggc_cleared_alloc<indirect_string_node> ();
4064 node->str = ggc_strdup (str);
4065 *slot = node;
4067 else
4068 node = *slot;
4070 node->refcount++;
4071 return node;
4074 /* Add STR to the indirect string hash table. */
4076 static struct indirect_string_node *
4077 find_AT_string (const char *str)
4079 if (! debug_str_hash)
4080 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4082 return find_AT_string_in_table (str, debug_str_hash);
4085 /* Add a string attribute value to a DIE. */
4087 static inline void
4088 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4090 dw_attr_node attr;
4091 struct indirect_string_node *node;
4093 node = find_AT_string (str);
4095 attr.dw_attr = attr_kind;
4096 attr.dw_attr_val.val_class = dw_val_class_str;
4097 attr.dw_attr_val.val_entry = NULL;
4098 attr.dw_attr_val.v.val_str = node;
4099 add_dwarf_attr (die, &attr);
4102 static inline const char *
4103 AT_string (dw_attr_node *a)
4105 gcc_assert (a && AT_class (a) == dw_val_class_str);
4106 return a->dw_attr_val.v.val_str->str;
4109 /* Call this function directly to bypass AT_string_form's logic to put
4110 the string inline in the die. */
4112 static void
4113 set_indirect_string (struct indirect_string_node *node)
4115 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4116 /* Already indirect is a no op. */
4117 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4119 gcc_assert (node->label);
4120 return;
4122 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4123 ++dw2_string_counter;
4124 node->label = xstrdup (label);
4126 if (!dwarf_split_debug_info)
4128 node->form = DW_FORM_strp;
4129 node->index = NOT_INDEXED;
4131 else
4133 node->form = DW_FORM_GNU_str_index;
4134 node->index = NO_INDEX_ASSIGNED;
4138 /* Find out whether a string should be output inline in DIE
4139 or out-of-line in .debug_str section. */
4141 static enum dwarf_form
4142 find_string_form (struct indirect_string_node *node)
4144 unsigned int len;
4146 if (node->form)
4147 return node->form;
4149 len = strlen (node->str) + 1;
4151 /* If the string is shorter or equal to the size of the reference, it is
4152 always better to put it inline. */
4153 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4154 return node->form = DW_FORM_string;
4156 /* If we cannot expect the linker to merge strings in .debug_str
4157 section, only put it into .debug_str if it is worth even in this
4158 single module. */
4159 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4160 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4161 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4162 return node->form = DW_FORM_string;
4164 set_indirect_string (node);
4166 return node->form;
4169 /* Find out whether the string referenced from the attribute should be
4170 output inline in DIE or out-of-line in .debug_str section. */
4172 static enum dwarf_form
4173 AT_string_form (dw_attr_node *a)
4175 gcc_assert (a && AT_class (a) == dw_val_class_str);
4176 return find_string_form (a->dw_attr_val.v.val_str);
4179 /* Add a DIE reference attribute value to a DIE. */
4181 static inline void
4182 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4184 dw_attr_node attr;
4185 gcc_checking_assert (targ_die != NULL);
4187 /* With LTO we can end up trying to reference something we didn't create
4188 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4189 if (targ_die == NULL)
4190 return;
4192 attr.dw_attr = attr_kind;
4193 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4194 attr.dw_attr_val.val_entry = NULL;
4195 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4196 attr.dw_attr_val.v.val_die_ref.external = 0;
4197 add_dwarf_attr (die, &attr);
4200 /* Change DIE reference REF to point to NEW_DIE instead. */
4202 static inline void
4203 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4205 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4206 ref->dw_attr_val.v.val_die_ref.die = new_die;
4207 ref->dw_attr_val.v.val_die_ref.external = 0;
4210 /* Add an AT_specification attribute to a DIE, and also make the back
4211 pointer from the specification to the definition. */
4213 static inline void
4214 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4216 add_AT_die_ref (die, DW_AT_specification, targ_die);
4217 gcc_assert (!targ_die->die_definition);
4218 targ_die->die_definition = die;
4221 static inline dw_die_ref
4222 AT_ref (dw_attr_node *a)
4224 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4225 return a->dw_attr_val.v.val_die_ref.die;
4228 static inline int
4229 AT_ref_external (dw_attr_node *a)
4231 if (a && AT_class (a) == dw_val_class_die_ref)
4232 return a->dw_attr_val.v.val_die_ref.external;
4234 return 0;
4237 static inline void
4238 set_AT_ref_external (dw_attr_node *a, int i)
4240 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4241 a->dw_attr_val.v.val_die_ref.external = i;
4244 /* Add an FDE reference attribute value to a DIE. */
4246 static inline void
4247 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4249 dw_attr_node attr;
4251 attr.dw_attr = attr_kind;
4252 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4253 attr.dw_attr_val.val_entry = NULL;
4254 attr.dw_attr_val.v.val_fde_index = targ_fde;
4255 add_dwarf_attr (die, &attr);
4258 /* Add a location description attribute value to a DIE. */
4260 static inline void
4261 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4263 dw_attr_node attr;
4265 attr.dw_attr = attr_kind;
4266 attr.dw_attr_val.val_class = dw_val_class_loc;
4267 attr.dw_attr_val.val_entry = NULL;
4268 attr.dw_attr_val.v.val_loc = loc;
4269 add_dwarf_attr (die, &attr);
4272 static inline dw_loc_descr_ref
4273 AT_loc (dw_attr_node *a)
4275 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4276 return a->dw_attr_val.v.val_loc;
4279 static inline void
4280 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4282 dw_attr_node attr;
4284 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4285 return;
4287 attr.dw_attr = attr_kind;
4288 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4289 attr.dw_attr_val.val_entry = NULL;
4290 attr.dw_attr_val.v.val_loc_list = loc_list;
4291 add_dwarf_attr (die, &attr);
4292 have_location_lists = true;
4295 static inline dw_loc_list_ref
4296 AT_loc_list (dw_attr_node *a)
4298 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4299 return a->dw_attr_val.v.val_loc_list;
4302 static inline dw_loc_list_ref *
4303 AT_loc_list_ptr (dw_attr_node *a)
4305 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4306 return &a->dw_attr_val.v.val_loc_list;
4309 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4311 static hashval_t hash (addr_table_entry *);
4312 static bool equal (addr_table_entry *, addr_table_entry *);
4315 /* Table of entries into the .debug_addr section. */
4317 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4319 /* Hash an address_table_entry. */
4321 hashval_t
4322 addr_hasher::hash (addr_table_entry *a)
4324 inchash::hash hstate;
4325 switch (a->kind)
4327 case ate_kind_rtx:
4328 hstate.add_int (0);
4329 break;
4330 case ate_kind_rtx_dtprel:
4331 hstate.add_int (1);
4332 break;
4333 case ate_kind_label:
4334 return htab_hash_string (a->addr.label);
4335 default:
4336 gcc_unreachable ();
4338 inchash::add_rtx (a->addr.rtl, hstate);
4339 return hstate.end ();
4342 /* Determine equality for two address_table_entries. */
4344 bool
4345 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4347 if (a1->kind != a2->kind)
4348 return 0;
4349 switch (a1->kind)
4351 case ate_kind_rtx:
4352 case ate_kind_rtx_dtprel:
4353 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4354 case ate_kind_label:
4355 return strcmp (a1->addr.label, a2->addr.label) == 0;
4356 default:
4357 gcc_unreachable ();
4361 /* Initialize an addr_table_entry. */
4363 void
4364 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4366 e->kind = kind;
4367 switch (kind)
4369 case ate_kind_rtx:
4370 case ate_kind_rtx_dtprel:
4371 e->addr.rtl = (rtx) addr;
4372 break;
4373 case ate_kind_label:
4374 e->addr.label = (char *) addr;
4375 break;
4377 e->refcount = 0;
4378 e->index = NO_INDEX_ASSIGNED;
4381 /* Add attr to the address table entry to the table. Defer setting an
4382 index until output time. */
4384 static addr_table_entry *
4385 add_addr_table_entry (void *addr, enum ate_kind kind)
4387 addr_table_entry *node;
4388 addr_table_entry finder;
4390 gcc_assert (dwarf_split_debug_info);
4391 if (! addr_index_table)
4392 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4393 init_addr_table_entry (&finder, kind, addr);
4394 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4396 if (*slot == HTAB_EMPTY_ENTRY)
4398 node = ggc_cleared_alloc<addr_table_entry> ();
4399 init_addr_table_entry (node, kind, addr);
4400 *slot = node;
4402 else
4403 node = *slot;
4405 node->refcount++;
4406 return node;
4409 /* Remove an entry from the addr table by decrementing its refcount.
4410 Strictly, decrementing the refcount would be enough, but the
4411 assertion that the entry is actually in the table has found
4412 bugs. */
4414 static void
4415 remove_addr_table_entry (addr_table_entry *entry)
4417 gcc_assert (dwarf_split_debug_info && addr_index_table);
4418 /* After an index is assigned, the table is frozen. */
4419 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4420 entry->refcount--;
4423 /* Given a location list, remove all addresses it refers to from the
4424 address_table. */
4426 static void
4427 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4429 for (; descr; descr = descr->dw_loc_next)
4430 if (descr->dw_loc_oprnd1.val_entry != NULL)
4432 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4433 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4437 /* A helper function for dwarf2out_finish called through
4438 htab_traverse. Assign an addr_table_entry its index. All entries
4439 must be collected into the table when this function is called,
4440 because the indexing code relies on htab_traverse to traverse nodes
4441 in the same order for each run. */
4444 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4446 addr_table_entry *node = *h;
4448 /* Don't index unreferenced nodes. */
4449 if (node->refcount == 0)
4450 return 1;
4452 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4453 node->index = *index;
4454 *index += 1;
4456 return 1;
4459 /* Add an address constant attribute value to a DIE. When using
4460 dwarf_split_debug_info, address attributes in dies destined for the
4461 final executable should be direct references--setting the parameter
4462 force_direct ensures this behavior. */
4464 static inline void
4465 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4466 bool force_direct)
4468 dw_attr_node attr;
4470 attr.dw_attr = attr_kind;
4471 attr.dw_attr_val.val_class = dw_val_class_addr;
4472 attr.dw_attr_val.v.val_addr = addr;
4473 if (dwarf_split_debug_info && !force_direct)
4474 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4475 else
4476 attr.dw_attr_val.val_entry = NULL;
4477 add_dwarf_attr (die, &attr);
4480 /* Get the RTX from to an address DIE attribute. */
4482 static inline rtx
4483 AT_addr (dw_attr_node *a)
4485 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4486 return a->dw_attr_val.v.val_addr;
4489 /* Add a file attribute value to a DIE. */
4491 static inline void
4492 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4493 struct dwarf_file_data *fd)
4495 dw_attr_node attr;
4497 attr.dw_attr = attr_kind;
4498 attr.dw_attr_val.val_class = dw_val_class_file;
4499 attr.dw_attr_val.val_entry = NULL;
4500 attr.dw_attr_val.v.val_file = fd;
4501 add_dwarf_attr (die, &attr);
4504 /* Get the dwarf_file_data from a file DIE attribute. */
4506 static inline struct dwarf_file_data *
4507 AT_file (dw_attr_node *a)
4509 gcc_assert (a && AT_class (a) == dw_val_class_file);
4510 return a->dw_attr_val.v.val_file;
4513 /* Add a vms delta attribute value to a DIE. */
4515 static inline void
4516 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4517 const char *lbl1, const char *lbl2)
4519 dw_attr_node attr;
4521 attr.dw_attr = attr_kind;
4522 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4523 attr.dw_attr_val.val_entry = NULL;
4524 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4525 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4526 add_dwarf_attr (die, &attr);
4529 /* Add a label identifier attribute value to a DIE. */
4531 static inline void
4532 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4533 const char *lbl_id)
4535 dw_attr_node attr;
4537 attr.dw_attr = attr_kind;
4538 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4539 attr.dw_attr_val.val_entry = NULL;
4540 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4541 if (dwarf_split_debug_info)
4542 attr.dw_attr_val.val_entry
4543 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4544 ate_kind_label);
4545 add_dwarf_attr (die, &attr);
4548 /* Add a section offset attribute value to a DIE, an offset into the
4549 debug_line section. */
4551 static inline void
4552 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4553 const char *label)
4555 dw_attr_node attr;
4557 attr.dw_attr = attr_kind;
4558 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4559 attr.dw_attr_val.val_entry = NULL;
4560 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4561 add_dwarf_attr (die, &attr);
4564 /* Add a section offset attribute value to a DIE, an offset into the
4565 debug_macinfo section. */
4567 static inline void
4568 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4569 const char *label)
4571 dw_attr_node attr;
4573 attr.dw_attr = attr_kind;
4574 attr.dw_attr_val.val_class = dw_val_class_macptr;
4575 attr.dw_attr_val.val_entry = NULL;
4576 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4577 add_dwarf_attr (die, &attr);
4580 /* Add an offset attribute value to a DIE. */
4582 static inline void
4583 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4584 unsigned HOST_WIDE_INT offset)
4586 dw_attr_node attr;
4588 attr.dw_attr = attr_kind;
4589 attr.dw_attr_val.val_class = dw_val_class_offset;
4590 attr.dw_attr_val.val_entry = NULL;
4591 attr.dw_attr_val.v.val_offset = offset;
4592 add_dwarf_attr (die, &attr);
4595 /* Add a range_list attribute value to a DIE. When using
4596 dwarf_split_debug_info, address attributes in dies destined for the
4597 final executable should be direct references--setting the parameter
4598 force_direct ensures this behavior. */
4600 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4601 #define RELOCATED_OFFSET (NULL)
4603 static void
4604 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4605 long unsigned int offset, bool force_direct)
4607 dw_attr_node attr;
4609 attr.dw_attr = attr_kind;
4610 attr.dw_attr_val.val_class = dw_val_class_range_list;
4611 /* For the range_list attribute, use val_entry to store whether the
4612 offset should follow split-debug-info or normal semantics. This
4613 value is read in output_range_list_offset. */
4614 if (dwarf_split_debug_info && !force_direct)
4615 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4616 else
4617 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4618 attr.dw_attr_val.v.val_offset = offset;
4619 add_dwarf_attr (die, &attr);
4622 /* Return the start label of a delta attribute. */
4624 static inline const char *
4625 AT_vms_delta1 (dw_attr_node *a)
4627 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4628 return a->dw_attr_val.v.val_vms_delta.lbl1;
4631 /* Return the end label of a delta attribute. */
4633 static inline const char *
4634 AT_vms_delta2 (dw_attr_node *a)
4636 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4637 return a->dw_attr_val.v.val_vms_delta.lbl2;
4640 static inline const char *
4641 AT_lbl (dw_attr_node *a)
4643 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4644 || AT_class (a) == dw_val_class_lineptr
4645 || AT_class (a) == dw_val_class_macptr
4646 || AT_class (a) == dw_val_class_high_pc));
4647 return a->dw_attr_val.v.val_lbl_id;
4650 /* Get the attribute of type attr_kind. */
4652 static dw_attr_node *
4653 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4655 dw_attr_node *a;
4656 unsigned ix;
4657 dw_die_ref spec = NULL;
4659 if (! die)
4660 return NULL;
4662 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4663 if (a->dw_attr == attr_kind)
4664 return a;
4665 else if (a->dw_attr == DW_AT_specification
4666 || a->dw_attr == DW_AT_abstract_origin)
4667 spec = AT_ref (a);
4669 if (spec)
4670 return get_AT (spec, attr_kind);
4672 return NULL;
4675 /* Returns the parent of the declaration of DIE. */
4677 static dw_die_ref
4678 get_die_parent (dw_die_ref die)
4680 dw_die_ref t;
4682 if (!die)
4683 return NULL;
4685 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4686 || (t = get_AT_ref (die, DW_AT_specification)))
4687 die = t;
4689 return die->die_parent;
4692 /* Return the "low pc" attribute value, typically associated with a subprogram
4693 DIE. Return null if the "low pc" attribute is either not present, or if it
4694 cannot be represented as an assembler label identifier. */
4696 static inline const char *
4697 get_AT_low_pc (dw_die_ref die)
4699 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4701 return a ? AT_lbl (a) : NULL;
4704 /* Return the "high pc" attribute value, typically associated with a subprogram
4705 DIE. Return null if the "high pc" attribute is either not present, or if it
4706 cannot be represented as an assembler label identifier. */
4708 static inline const char *
4709 get_AT_hi_pc (dw_die_ref die)
4711 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4713 return a ? AT_lbl (a) : NULL;
4716 /* Return the value of the string attribute designated by ATTR_KIND, or
4717 NULL if it is not present. */
4719 static inline const char *
4720 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4722 dw_attr_node *a = get_AT (die, attr_kind);
4724 return a ? AT_string (a) : NULL;
4727 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4728 if it is not present. */
4730 static inline int
4731 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4733 dw_attr_node *a = get_AT (die, attr_kind);
4735 return a ? AT_flag (a) : 0;
4738 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4739 if it is not present. */
4741 static inline unsigned
4742 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4744 dw_attr_node *a = get_AT (die, attr_kind);
4746 return a ? AT_unsigned (a) : 0;
4749 static inline dw_die_ref
4750 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4752 dw_attr_node *a = get_AT (die, attr_kind);
4754 return a ? AT_ref (a) : NULL;
4757 static inline struct dwarf_file_data *
4758 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4760 dw_attr_node *a = get_AT (die, attr_kind);
4762 return a ? AT_file (a) : NULL;
4765 /* Return TRUE if the language is C++. */
4767 static inline bool
4768 is_cxx (void)
4770 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4772 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4773 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4776 /* Return TRUE if the language is Java. */
4778 static inline bool
4779 is_java (void)
4781 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4783 return lang == DW_LANG_Java;
4786 /* Return TRUE if the language is Fortran. */
4788 static inline bool
4789 is_fortran (void)
4791 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4793 return (lang == DW_LANG_Fortran77
4794 || lang == DW_LANG_Fortran90
4795 || lang == DW_LANG_Fortran95
4796 || lang == DW_LANG_Fortran03
4797 || lang == DW_LANG_Fortran08);
4800 /* Return TRUE if the language is Ada. */
4802 static inline bool
4803 is_ada (void)
4805 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4807 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4810 /* Remove the specified attribute if present. Return TRUE if removal
4811 was successful. */
4813 static bool
4814 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4816 dw_attr_node *a;
4817 unsigned ix;
4819 if (! die)
4820 return false;
4822 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4823 if (a->dw_attr == attr_kind)
4825 if (AT_class (a) == dw_val_class_str)
4826 if (a->dw_attr_val.v.val_str->refcount)
4827 a->dw_attr_val.v.val_str->refcount--;
4829 /* vec::ordered_remove should help reduce the number of abbrevs
4830 that are needed. */
4831 die->die_attr->ordered_remove (ix);
4832 return true;
4834 return false;
4837 /* Remove CHILD from its parent. PREV must have the property that
4838 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4840 static void
4841 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4843 gcc_assert (child->die_parent == prev->die_parent);
4844 gcc_assert (prev->die_sib == child);
4845 if (prev == child)
4847 gcc_assert (child->die_parent->die_child == child);
4848 prev = NULL;
4850 else
4851 prev->die_sib = child->die_sib;
4852 if (child->die_parent->die_child == child)
4853 child->die_parent->die_child = prev;
4854 child->die_sib = NULL;
4857 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4858 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4860 static void
4861 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4863 dw_die_ref parent = old_child->die_parent;
4865 gcc_assert (parent == prev->die_parent);
4866 gcc_assert (prev->die_sib == old_child);
4868 new_child->die_parent = parent;
4869 if (prev == old_child)
4871 gcc_assert (parent->die_child == old_child);
4872 new_child->die_sib = new_child;
4874 else
4876 prev->die_sib = new_child;
4877 new_child->die_sib = old_child->die_sib;
4879 if (old_child->die_parent->die_child == old_child)
4880 old_child->die_parent->die_child = new_child;
4881 old_child->die_sib = NULL;
4884 /* Move all children from OLD_PARENT to NEW_PARENT. */
4886 static void
4887 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4889 dw_die_ref c;
4890 new_parent->die_child = old_parent->die_child;
4891 old_parent->die_child = NULL;
4892 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4895 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4896 matches TAG. */
4898 static void
4899 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4901 dw_die_ref c;
4903 c = die->die_child;
4904 if (c) do {
4905 dw_die_ref prev = c;
4906 c = c->die_sib;
4907 while (c->die_tag == tag)
4909 remove_child_with_prev (c, prev);
4910 c->die_parent = NULL;
4911 /* Might have removed every child. */
4912 if (die->die_child == NULL)
4913 return;
4914 c = prev->die_sib;
4916 } while (c != die->die_child);
4919 /* Add a CHILD_DIE as the last child of DIE. */
4921 static void
4922 add_child_die (dw_die_ref die, dw_die_ref child_die)
4924 /* FIXME this should probably be an assert. */
4925 if (! die || ! child_die)
4926 return;
4927 gcc_assert (die != child_die);
4929 child_die->die_parent = die;
4930 if (die->die_child)
4932 child_die->die_sib = die->die_child->die_sib;
4933 die->die_child->die_sib = child_die;
4935 else
4936 child_die->die_sib = child_die;
4937 die->die_child = child_die;
4940 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
4942 static void
4943 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4944 dw_die_ref after_die)
4946 gcc_assert (die
4947 && child_die
4948 && after_die
4949 && die->die_child
4950 && die != child_die);
4952 child_die->die_parent = die;
4953 child_die->die_sib = after_die->die_sib;
4954 after_die->die_sib = child_die;
4955 if (die->die_child == after_die)
4956 die->die_child = child_die;
4959 /* Unassociate CHILD from its parent, and make its parent be
4960 NEW_PARENT. */
4962 static void
4963 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4965 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4966 if (p->die_sib == child)
4968 remove_child_with_prev (child, p);
4969 break;
4971 add_child_die (new_parent, child);
4974 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4975 is the specification, to the end of PARENT's list of children.
4976 This is done by removing and re-adding it. */
4978 static void
4979 splice_child_die (dw_die_ref parent, dw_die_ref child)
4981 /* We want the declaration DIE from inside the class, not the
4982 specification DIE at toplevel. */
4983 if (child->die_parent != parent)
4985 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4987 if (tmp)
4988 child = tmp;
4991 gcc_assert (child->die_parent == parent
4992 || (child->die_parent
4993 == get_AT_ref (parent, DW_AT_specification)));
4995 reparent_child (child, parent);
4998 /* Create and return a new die with a parent of PARENT_DIE. If
4999 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5000 associated tree T must be supplied to determine parenthood
5001 later. */
5003 static inline dw_die_ref
5004 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5006 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5008 die->die_tag = tag_value;
5010 if (parent_die != NULL)
5011 add_child_die (parent_die, die);
5012 else
5014 limbo_die_node *limbo_node;
5016 /* No DIEs created after early dwarf should end up in limbo,
5017 because the limbo list should not persist past LTO
5018 streaming. */
5019 if (tag_value != DW_TAG_compile_unit
5020 /* These are allowed because they're generated while
5021 breaking out COMDAT units late. */
5022 && tag_value != DW_TAG_type_unit
5023 && !early_dwarf
5024 /* Allow nested functions to live in limbo because they will
5025 only temporarily live there, as decls_for_scope will fix
5026 them up. */
5027 && (TREE_CODE (t) != FUNCTION_DECL
5028 || !decl_function_context (t))
5029 /* Same as nested functions above but for types. Types that
5030 are local to a function will be fixed in
5031 decls_for_scope. */
5032 && (!RECORD_OR_UNION_TYPE_P (t)
5033 || !TYPE_CONTEXT (t)
5034 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5035 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5036 especially in the ltrans stage, but once we implement LTO
5037 dwarf streaming, we should remove this exception. */
5038 && !in_lto_p)
5040 fprintf (stderr, "symbol ended up in limbo too late:");
5041 debug_generic_stmt (t);
5042 gcc_unreachable ();
5045 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5046 limbo_node->die = die;
5047 limbo_node->created_for = t;
5048 limbo_node->next = limbo_die_list;
5049 limbo_die_list = limbo_node;
5052 return die;
5055 /* Return the DIE associated with the given type specifier. */
5057 static inline dw_die_ref
5058 lookup_type_die (tree type)
5060 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5061 if (die && die->removed)
5063 TYPE_SYMTAB_DIE (type) = NULL;
5064 return NULL;
5066 return die;
5069 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5070 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5071 anonymous type instead the one of the naming typedef. */
5073 static inline dw_die_ref
5074 strip_naming_typedef (tree type, dw_die_ref type_die)
5076 if (type
5077 && TREE_CODE (type) == RECORD_TYPE
5078 && type_die
5079 && type_die->die_tag == DW_TAG_typedef
5080 && is_naming_typedef_decl (TYPE_NAME (type)))
5081 type_die = get_AT_ref (type_die, DW_AT_type);
5082 return type_die;
5085 /* Like lookup_type_die, but if type is an anonymous type named by a
5086 typedef[1], return the DIE of the anonymous type instead the one of
5087 the naming typedef. This is because in gen_typedef_die, we did
5088 equate the anonymous struct named by the typedef with the DIE of
5089 the naming typedef. So by default, lookup_type_die on an anonymous
5090 struct yields the DIE of the naming typedef.
5092 [1]: Read the comment of is_naming_typedef_decl to learn about what
5093 a naming typedef is. */
5095 static inline dw_die_ref
5096 lookup_type_die_strip_naming_typedef (tree type)
5098 dw_die_ref die = lookup_type_die (type);
5099 return strip_naming_typedef (type, die);
5102 /* Equate a DIE to a given type specifier. */
5104 static inline void
5105 equate_type_number_to_die (tree type, dw_die_ref type_die)
5107 TYPE_SYMTAB_DIE (type) = type_die;
5110 /* Returns a hash value for X (which really is a die_struct). */
5112 inline hashval_t
5113 decl_die_hasher::hash (die_node *x)
5115 return (hashval_t) x->decl_id;
5118 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5120 inline bool
5121 decl_die_hasher::equal (die_node *x, tree y)
5123 return (x->decl_id == DECL_UID (y));
5126 /* Return the DIE associated with a given declaration. */
5128 static inline dw_die_ref
5129 lookup_decl_die (tree decl)
5131 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5132 NO_INSERT);
5133 if (!die)
5134 return NULL;
5135 if ((*die)->removed)
5137 decl_die_table->clear_slot (die);
5138 return NULL;
5140 return *die;
5143 /* Returns a hash value for X (which really is a var_loc_list). */
5145 inline hashval_t
5146 decl_loc_hasher::hash (var_loc_list *x)
5148 return (hashval_t) x->decl_id;
5151 /* Return nonzero if decl_id of var_loc_list X is the same as
5152 UID of decl *Y. */
5154 inline bool
5155 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5157 return (x->decl_id == DECL_UID (y));
5160 /* Return the var_loc list associated with a given declaration. */
5162 static inline var_loc_list *
5163 lookup_decl_loc (const_tree decl)
5165 if (!decl_loc_table)
5166 return NULL;
5167 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5170 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5172 inline hashval_t
5173 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5175 return (hashval_t) x->decl_id;
5178 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5179 UID of decl *Y. */
5181 inline bool
5182 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5184 return (x->decl_id == DECL_UID (y));
5187 /* Equate a DIE to a particular declaration. */
5189 static void
5190 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5192 unsigned int decl_id = DECL_UID (decl);
5194 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5195 decl_die->decl_id = decl_id;
5198 /* Return how many bits covers PIECE EXPR_LIST. */
5200 static HOST_WIDE_INT
5201 decl_piece_bitsize (rtx piece)
5203 int ret = (int) GET_MODE (piece);
5204 if (ret)
5205 return ret;
5206 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5207 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5208 return INTVAL (XEXP (XEXP (piece, 0), 0));
5211 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5213 static rtx *
5214 decl_piece_varloc_ptr (rtx piece)
5216 if ((int) GET_MODE (piece))
5217 return &XEXP (piece, 0);
5218 else
5219 return &XEXP (XEXP (piece, 0), 1);
5222 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5223 Next is the chain of following piece nodes. */
5225 static rtx_expr_list *
5226 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5228 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5229 return alloc_EXPR_LIST (bitsize, loc_note, next);
5230 else
5231 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5232 GEN_INT (bitsize),
5233 loc_note), next);
5236 /* Return rtx that should be stored into loc field for
5237 LOC_NOTE and BITPOS/BITSIZE. */
5239 static rtx
5240 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5241 HOST_WIDE_INT bitsize)
5243 if (bitsize != -1)
5245 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5246 if (bitpos != 0)
5247 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5249 return loc_note;
5252 /* This function either modifies location piece list *DEST in
5253 place (if SRC and INNER is NULL), or copies location piece list
5254 *SRC to *DEST while modifying it. Location BITPOS is modified
5255 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5256 not copied and if needed some padding around it is added.
5257 When modifying in place, DEST should point to EXPR_LIST where
5258 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5259 to the start of the whole list and INNER points to the EXPR_LIST
5260 where earlier pieces cover PIECE_BITPOS bits. */
5262 static void
5263 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5264 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5265 HOST_WIDE_INT bitsize, rtx loc_note)
5267 HOST_WIDE_INT diff;
5268 bool copy = inner != NULL;
5270 if (copy)
5272 /* First copy all nodes preceding the current bitpos. */
5273 while (src != inner)
5275 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5276 decl_piece_bitsize (*src), NULL_RTX);
5277 dest = &XEXP (*dest, 1);
5278 src = &XEXP (*src, 1);
5281 /* Add padding if needed. */
5282 if (bitpos != piece_bitpos)
5284 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5285 copy ? NULL_RTX : *dest);
5286 dest = &XEXP (*dest, 1);
5288 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5290 gcc_assert (!copy);
5291 /* A piece with correct bitpos and bitsize already exist,
5292 just update the location for it and return. */
5293 *decl_piece_varloc_ptr (*dest) = loc_note;
5294 return;
5296 /* Add the piece that changed. */
5297 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5298 dest = &XEXP (*dest, 1);
5299 /* Skip over pieces that overlap it. */
5300 diff = bitpos - piece_bitpos + bitsize;
5301 if (!copy)
5302 src = dest;
5303 while (diff > 0 && *src)
5305 rtx piece = *src;
5306 diff -= decl_piece_bitsize (piece);
5307 if (copy)
5308 src = &XEXP (piece, 1);
5309 else
5311 *src = XEXP (piece, 1);
5312 free_EXPR_LIST_node (piece);
5315 /* Add padding if needed. */
5316 if (diff < 0 && *src)
5318 if (!copy)
5319 dest = src;
5320 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5321 dest = &XEXP (*dest, 1);
5323 if (!copy)
5324 return;
5325 /* Finally copy all nodes following it. */
5326 while (*src)
5328 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5329 decl_piece_bitsize (*src), NULL_RTX);
5330 dest = &XEXP (*dest, 1);
5331 src = &XEXP (*src, 1);
5335 /* Add a variable location node to the linked list for DECL. */
5337 static struct var_loc_node *
5338 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5340 unsigned int decl_id;
5341 var_loc_list *temp;
5342 struct var_loc_node *loc = NULL;
5343 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5345 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5347 tree realdecl = DECL_DEBUG_EXPR (decl);
5348 if (handled_component_p (realdecl)
5349 || (TREE_CODE (realdecl) == MEM_REF
5350 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5352 HOST_WIDE_INT maxsize;
5353 bool reverse;
5354 tree innerdecl
5355 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5356 &reverse);
5357 if (!DECL_P (innerdecl)
5358 || DECL_IGNORED_P (innerdecl)
5359 || TREE_STATIC (innerdecl)
5360 || bitsize <= 0
5361 || bitpos + bitsize > 256
5362 || bitsize != maxsize)
5363 return NULL;
5364 decl = innerdecl;
5368 decl_id = DECL_UID (decl);
5369 var_loc_list **slot
5370 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5371 if (*slot == NULL)
5373 temp = ggc_cleared_alloc<var_loc_list> ();
5374 temp->decl_id = decl_id;
5375 *slot = temp;
5377 else
5378 temp = *slot;
5380 /* For PARM_DECLs try to keep around the original incoming value,
5381 even if that means we'll emit a zero-range .debug_loc entry. */
5382 if (temp->last
5383 && temp->first == temp->last
5384 && TREE_CODE (decl) == PARM_DECL
5385 && NOTE_P (temp->first->loc)
5386 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5387 && DECL_INCOMING_RTL (decl)
5388 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5389 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5390 == GET_CODE (DECL_INCOMING_RTL (decl))
5391 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5392 && (bitsize != -1
5393 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5394 NOTE_VAR_LOCATION_LOC (loc_note))
5395 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5396 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5398 loc = ggc_cleared_alloc<var_loc_node> ();
5399 temp->first->next = loc;
5400 temp->last = loc;
5401 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5403 else if (temp->last)
5405 struct var_loc_node *last = temp->last, *unused = NULL;
5406 rtx *piece_loc = NULL, last_loc_note;
5407 HOST_WIDE_INT piece_bitpos = 0;
5408 if (last->next)
5410 last = last->next;
5411 gcc_assert (last->next == NULL);
5413 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5415 piece_loc = &last->loc;
5418 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5419 if (piece_bitpos + cur_bitsize > bitpos)
5420 break;
5421 piece_bitpos += cur_bitsize;
5422 piece_loc = &XEXP (*piece_loc, 1);
5424 while (*piece_loc);
5426 /* TEMP->LAST here is either pointer to the last but one or
5427 last element in the chained list, LAST is pointer to the
5428 last element. */
5429 if (label && strcmp (last->label, label) == 0)
5431 /* For SRA optimized variables if there weren't any real
5432 insns since last note, just modify the last node. */
5433 if (piece_loc != NULL)
5435 adjust_piece_list (piece_loc, NULL, NULL,
5436 bitpos, piece_bitpos, bitsize, loc_note);
5437 return NULL;
5439 /* If the last note doesn't cover any instructions, remove it. */
5440 if (temp->last != last)
5442 temp->last->next = NULL;
5443 unused = last;
5444 last = temp->last;
5445 gcc_assert (strcmp (last->label, label) != 0);
5447 else
5449 gcc_assert (temp->first == temp->last
5450 || (temp->first->next == temp->last
5451 && TREE_CODE (decl) == PARM_DECL));
5452 memset (temp->last, '\0', sizeof (*temp->last));
5453 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5454 return temp->last;
5457 if (bitsize == -1 && NOTE_P (last->loc))
5458 last_loc_note = last->loc;
5459 else if (piece_loc != NULL
5460 && *piece_loc != NULL_RTX
5461 && piece_bitpos == bitpos
5462 && decl_piece_bitsize (*piece_loc) == bitsize)
5463 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5464 else
5465 last_loc_note = NULL_RTX;
5466 /* If the current location is the same as the end of the list,
5467 and either both or neither of the locations is uninitialized,
5468 we have nothing to do. */
5469 if (last_loc_note == NULL_RTX
5470 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5471 NOTE_VAR_LOCATION_LOC (loc_note)))
5472 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5473 != NOTE_VAR_LOCATION_STATUS (loc_note))
5474 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5475 == VAR_INIT_STATUS_UNINITIALIZED)
5476 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5477 == VAR_INIT_STATUS_UNINITIALIZED))))
5479 /* Add LOC to the end of list and update LAST. If the last
5480 element of the list has been removed above, reuse its
5481 memory for the new node, otherwise allocate a new one. */
5482 if (unused)
5484 loc = unused;
5485 memset (loc, '\0', sizeof (*loc));
5487 else
5488 loc = ggc_cleared_alloc<var_loc_node> ();
5489 if (bitsize == -1 || piece_loc == NULL)
5490 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5491 else
5492 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5493 bitpos, piece_bitpos, bitsize, loc_note);
5494 last->next = loc;
5495 /* Ensure TEMP->LAST will point either to the new last but one
5496 element of the chain, or to the last element in it. */
5497 if (last != temp->last)
5498 temp->last = last;
5500 else if (unused)
5501 ggc_free (unused);
5503 else
5505 loc = ggc_cleared_alloc<var_loc_node> ();
5506 temp->first = loc;
5507 temp->last = loc;
5508 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5510 return loc;
5513 /* Keep track of the number of spaces used to indent the
5514 output of the debugging routines that print the structure of
5515 the DIE internal representation. */
5516 static int print_indent;
5518 /* Indent the line the number of spaces given by print_indent. */
5520 static inline void
5521 print_spaces (FILE *outfile)
5523 fprintf (outfile, "%*s", print_indent, "");
5526 /* Print a type signature in hex. */
5528 static inline void
5529 print_signature (FILE *outfile, char *sig)
5531 int i;
5533 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5534 fprintf (outfile, "%02x", sig[i] & 0xff);
5537 static inline void
5538 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5540 if (discr_value->pos)
5541 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5542 else
5543 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5546 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5548 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5549 RECURSE, output location descriptor operations. */
5551 static void
5552 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5554 switch (val->val_class)
5556 case dw_val_class_addr:
5557 fprintf (outfile, "address");
5558 break;
5559 case dw_val_class_offset:
5560 fprintf (outfile, "offset");
5561 break;
5562 case dw_val_class_loc:
5563 fprintf (outfile, "location descriptor");
5564 if (val->v.val_loc == NULL)
5565 fprintf (outfile, " -> <null>\n");
5566 else if (recurse)
5568 fprintf (outfile, ":\n");
5569 print_indent += 4;
5570 print_loc_descr (val->v.val_loc, outfile);
5571 print_indent -= 4;
5573 else
5574 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5575 break;
5576 case dw_val_class_loc_list:
5577 fprintf (outfile, "location list -> label:%s",
5578 val->v.val_loc_list->ll_symbol);
5579 break;
5580 case dw_val_class_range_list:
5581 fprintf (outfile, "range list");
5582 break;
5583 case dw_val_class_const:
5584 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5585 break;
5586 case dw_val_class_unsigned_const:
5587 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5588 break;
5589 case dw_val_class_const_double:
5590 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5591 HOST_WIDE_INT_PRINT_UNSIGNED")",
5592 val->v.val_double.high,
5593 val->v.val_double.low);
5594 break;
5595 case dw_val_class_wide_int:
5597 int i = val->v.val_wide->get_len ();
5598 fprintf (outfile, "constant (");
5599 gcc_assert (i > 0);
5600 if (val->v.val_wide->elt (i - 1) == 0)
5601 fprintf (outfile, "0x");
5602 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5603 val->v.val_wide->elt (--i));
5604 while (--i >= 0)
5605 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5606 val->v.val_wide->elt (i));
5607 fprintf (outfile, ")");
5608 break;
5610 case dw_val_class_vec:
5611 fprintf (outfile, "floating-point or vector constant");
5612 break;
5613 case dw_val_class_flag:
5614 fprintf (outfile, "%u", val->v.val_flag);
5615 break;
5616 case dw_val_class_die_ref:
5617 if (val->v.val_die_ref.die != NULL)
5619 dw_die_ref die = val->v.val_die_ref.die;
5621 if (die->comdat_type_p)
5623 fprintf (outfile, "die -> signature: ");
5624 print_signature (outfile,
5625 die->die_id.die_type_node->signature);
5627 else if (die->die_id.die_symbol)
5628 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5629 else
5630 fprintf (outfile, "die -> %ld", die->die_offset);
5631 fprintf (outfile, " (%p)", (void *) die);
5633 else
5634 fprintf (outfile, "die -> <null>");
5635 break;
5636 case dw_val_class_vms_delta:
5637 fprintf (outfile, "delta: @slotcount(%s-%s)",
5638 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5639 break;
5640 case dw_val_class_lbl_id:
5641 case dw_val_class_lineptr:
5642 case dw_val_class_macptr:
5643 case dw_val_class_high_pc:
5644 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5645 break;
5646 case dw_val_class_str:
5647 if (val->v.val_str->str != NULL)
5648 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5649 else
5650 fprintf (outfile, "<null>");
5651 break;
5652 case dw_val_class_file:
5653 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5654 val->v.val_file->emitted_number);
5655 break;
5656 case dw_val_class_data8:
5658 int i;
5660 for (i = 0; i < 8; i++)
5661 fprintf (outfile, "%02x", val->v.val_data8[i]);
5662 break;
5664 case dw_val_class_discr_value:
5665 print_discr_value (outfile, &val->v.val_discr_value);
5666 break;
5667 case dw_val_class_discr_list:
5668 for (dw_discr_list_ref node = val->v.val_discr_list;
5669 node != NULL;
5670 node = node->dw_discr_next)
5672 if (node->dw_discr_range)
5674 fprintf (outfile, " .. ");
5675 print_discr_value (outfile, &node->dw_discr_lower_bound);
5676 print_discr_value (outfile, &node->dw_discr_upper_bound);
5678 else
5679 print_discr_value (outfile, &node->dw_discr_lower_bound);
5681 if (node->dw_discr_next != NULL)
5682 fprintf (outfile, " | ");
5684 default:
5685 break;
5689 /* Likewise, for a DIE attribute. */
5691 static void
5692 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5694 print_dw_val (&a->dw_attr_val, recurse, outfile);
5698 /* Print the list of operands in the LOC location description to OUTFILE. This
5699 routine is a debugging aid only. */
5701 static void
5702 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5704 dw_loc_descr_ref l = loc;
5706 if (loc == NULL)
5708 print_spaces (outfile);
5709 fprintf (outfile, "<null>\n");
5710 return;
5713 for (l = loc; l != NULL; l = l->dw_loc_next)
5715 print_spaces (outfile);
5716 fprintf (outfile, "(%p) %s",
5717 (void *) l,
5718 dwarf_stack_op_name (l->dw_loc_opc));
5719 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5721 fprintf (outfile, " ");
5722 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5724 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5726 fprintf (outfile, ", ");
5727 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5729 fprintf (outfile, "\n");
5733 /* Print the information associated with a given DIE, and its children.
5734 This routine is a debugging aid only. */
5736 static void
5737 print_die (dw_die_ref die, FILE *outfile)
5739 dw_attr_node *a;
5740 dw_die_ref c;
5741 unsigned ix;
5743 print_spaces (outfile);
5744 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5745 die->die_offset, dwarf_tag_name (die->die_tag),
5746 (void*) die);
5747 print_spaces (outfile);
5748 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5749 fprintf (outfile, " offset: %ld", die->die_offset);
5750 fprintf (outfile, " mark: %d\n", die->die_mark);
5752 if (die->comdat_type_p)
5754 print_spaces (outfile);
5755 fprintf (outfile, " signature: ");
5756 print_signature (outfile, die->die_id.die_type_node->signature);
5757 fprintf (outfile, "\n");
5760 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5762 print_spaces (outfile);
5763 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5765 print_attribute (a, true, outfile);
5766 fprintf (outfile, "\n");
5769 if (die->die_child != NULL)
5771 print_indent += 4;
5772 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5773 print_indent -= 4;
5775 if (print_indent == 0)
5776 fprintf (outfile, "\n");
5779 /* Print the list of operations in the LOC location description. */
5781 DEBUG_FUNCTION void
5782 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5784 print_loc_descr (loc, stderr);
5787 /* Print the information collected for a given DIE. */
5789 DEBUG_FUNCTION void
5790 debug_dwarf_die (dw_die_ref die)
5792 print_die (die, stderr);
5795 DEBUG_FUNCTION void
5796 debug (die_struct &ref)
5798 print_die (&ref, stderr);
5801 DEBUG_FUNCTION void
5802 debug (die_struct *ptr)
5804 if (ptr)
5805 debug (*ptr);
5806 else
5807 fprintf (stderr, "<nil>\n");
5811 /* Print all DWARF information collected for the compilation unit.
5812 This routine is a debugging aid only. */
5814 DEBUG_FUNCTION void
5815 debug_dwarf (void)
5817 print_indent = 0;
5818 print_die (comp_unit_die (), stderr);
5821 /* Verify the DIE tree structure. */
5823 DEBUG_FUNCTION void
5824 verify_die (dw_die_ref die)
5826 gcc_assert (!die->die_mark);
5827 if (die->die_parent == NULL
5828 && die->die_sib == NULL)
5829 return;
5830 /* Verify the die_sib list is cyclic. */
5831 dw_die_ref x = die;
5834 x->die_mark = 1;
5835 x = x->die_sib;
5837 while (x && !x->die_mark);
5838 gcc_assert (x == die);
5839 x = die;
5842 /* Verify all dies have the same parent. */
5843 gcc_assert (x->die_parent == die->die_parent);
5844 if (x->die_child)
5846 /* Verify the child has the proper parent and recurse. */
5847 gcc_assert (x->die_child->die_parent == x);
5848 verify_die (x->die_child);
5850 x->die_mark = 0;
5851 x = x->die_sib;
5853 while (x && x->die_mark);
5856 /* Sanity checks on DIEs. */
5858 static void
5859 check_die (dw_die_ref die)
5861 unsigned ix;
5862 dw_attr_node *a;
5863 bool inline_found = false;
5864 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5865 int n_decl_line = 0, n_decl_file = 0;
5866 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5868 switch (a->dw_attr)
5870 case DW_AT_inline:
5871 if (a->dw_attr_val.v.val_unsigned)
5872 inline_found = true;
5873 break;
5874 case DW_AT_location:
5875 ++n_location;
5876 break;
5877 case DW_AT_low_pc:
5878 ++n_low_pc;
5879 break;
5880 case DW_AT_high_pc:
5881 ++n_high_pc;
5882 break;
5883 case DW_AT_artificial:
5884 ++n_artificial;
5885 break;
5886 case DW_AT_decl_line:
5887 ++n_decl_line;
5888 break;
5889 case DW_AT_decl_file:
5890 ++n_decl_file;
5891 break;
5892 default:
5893 break;
5896 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5897 || n_decl_line > 1 || n_decl_file > 1)
5899 fprintf (stderr, "Duplicate attributes in DIE:\n");
5900 debug_dwarf_die (die);
5901 gcc_unreachable ();
5903 if (inline_found)
5905 /* A debugging information entry that is a member of an abstract
5906 instance tree [that has DW_AT_inline] should not contain any
5907 attributes which describe aspects of the subroutine which vary
5908 between distinct inlined expansions or distinct out-of-line
5909 expansions. */
5910 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5911 gcc_assert (a->dw_attr != DW_AT_low_pc
5912 && a->dw_attr != DW_AT_high_pc
5913 && a->dw_attr != DW_AT_location
5914 && a->dw_attr != DW_AT_frame_base
5915 && a->dw_attr != DW_AT_GNU_all_call_sites);
5919 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5920 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5921 DIE that marks the start of the DIEs for this include file. */
5923 static dw_die_ref
5924 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5926 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5927 dw_die_ref new_unit = gen_compile_unit_die (filename);
5929 new_unit->die_sib = old_unit;
5930 return new_unit;
5933 /* Close an include-file CU and reopen the enclosing one. */
5935 static dw_die_ref
5936 pop_compile_unit (dw_die_ref old_unit)
5938 dw_die_ref new_unit = old_unit->die_sib;
5940 old_unit->die_sib = NULL;
5941 return new_unit;
5944 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5945 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5946 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5948 /* Calculate the checksum of a location expression. */
5950 static inline void
5951 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5953 int tem;
5954 inchash::hash hstate;
5955 hashval_t hash;
5957 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5958 CHECKSUM (tem);
5959 hash_loc_operands (loc, hstate);
5960 hash = hstate.end();
5961 CHECKSUM (hash);
5964 /* Calculate the checksum of an attribute. */
5966 static void
5967 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5969 dw_loc_descr_ref loc;
5970 rtx r;
5972 CHECKSUM (at->dw_attr);
5974 /* We don't care that this was compiled with a different compiler
5975 snapshot; if the output is the same, that's what matters. */
5976 if (at->dw_attr == DW_AT_producer)
5977 return;
5979 switch (AT_class (at))
5981 case dw_val_class_const:
5982 CHECKSUM (at->dw_attr_val.v.val_int);
5983 break;
5984 case dw_val_class_unsigned_const:
5985 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5986 break;
5987 case dw_val_class_const_double:
5988 CHECKSUM (at->dw_attr_val.v.val_double);
5989 break;
5990 case dw_val_class_wide_int:
5991 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5992 get_full_len (*at->dw_attr_val.v.val_wide)
5993 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5994 break;
5995 case dw_val_class_vec:
5996 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5997 (at->dw_attr_val.v.val_vec.length
5998 * at->dw_attr_val.v.val_vec.elt_size));
5999 break;
6000 case dw_val_class_flag:
6001 CHECKSUM (at->dw_attr_val.v.val_flag);
6002 break;
6003 case dw_val_class_str:
6004 CHECKSUM_STRING (AT_string (at));
6005 break;
6007 case dw_val_class_addr:
6008 r = AT_addr (at);
6009 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6010 CHECKSUM_STRING (XSTR (r, 0));
6011 break;
6013 case dw_val_class_offset:
6014 CHECKSUM (at->dw_attr_val.v.val_offset);
6015 break;
6017 case dw_val_class_loc:
6018 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6019 loc_checksum (loc, ctx);
6020 break;
6022 case dw_val_class_die_ref:
6023 die_checksum (AT_ref (at), ctx, mark);
6024 break;
6026 case dw_val_class_fde_ref:
6027 case dw_val_class_vms_delta:
6028 case dw_val_class_lbl_id:
6029 case dw_val_class_lineptr:
6030 case dw_val_class_macptr:
6031 case dw_val_class_high_pc:
6032 break;
6034 case dw_val_class_file:
6035 CHECKSUM_STRING (AT_file (at)->filename);
6036 break;
6038 case dw_val_class_data8:
6039 CHECKSUM (at->dw_attr_val.v.val_data8);
6040 break;
6042 default:
6043 break;
6047 /* Calculate the checksum of a DIE. */
6049 static void
6050 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6052 dw_die_ref c;
6053 dw_attr_node *a;
6054 unsigned ix;
6056 /* To avoid infinite recursion. */
6057 if (die->die_mark)
6059 CHECKSUM (die->die_mark);
6060 return;
6062 die->die_mark = ++(*mark);
6064 CHECKSUM (die->die_tag);
6066 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6067 attr_checksum (a, ctx, mark);
6069 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6072 #undef CHECKSUM
6073 #undef CHECKSUM_BLOCK
6074 #undef CHECKSUM_STRING
6076 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6077 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6078 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6079 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6080 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6081 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6082 #define CHECKSUM_ATTR(FOO) \
6083 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6085 /* Calculate the checksum of a number in signed LEB128 format. */
6087 static void
6088 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6090 unsigned char byte;
6091 bool more;
6093 while (1)
6095 byte = (value & 0x7f);
6096 value >>= 7;
6097 more = !((value == 0 && (byte & 0x40) == 0)
6098 || (value == -1 && (byte & 0x40) != 0));
6099 if (more)
6100 byte |= 0x80;
6101 CHECKSUM (byte);
6102 if (!more)
6103 break;
6107 /* Calculate the checksum of a number in unsigned LEB128 format. */
6109 static void
6110 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6112 while (1)
6114 unsigned char byte = (value & 0x7f);
6115 value >>= 7;
6116 if (value != 0)
6117 /* More bytes to follow. */
6118 byte |= 0x80;
6119 CHECKSUM (byte);
6120 if (value == 0)
6121 break;
6125 /* Checksum the context of the DIE. This adds the names of any
6126 surrounding namespaces or structures to the checksum. */
6128 static void
6129 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6131 const char *name;
6132 dw_die_ref spec;
6133 int tag = die->die_tag;
6135 if (tag != DW_TAG_namespace
6136 && tag != DW_TAG_structure_type
6137 && tag != DW_TAG_class_type)
6138 return;
6140 name = get_AT_string (die, DW_AT_name);
6142 spec = get_AT_ref (die, DW_AT_specification);
6143 if (spec != NULL)
6144 die = spec;
6146 if (die->die_parent != NULL)
6147 checksum_die_context (die->die_parent, ctx);
6149 CHECKSUM_ULEB128 ('C');
6150 CHECKSUM_ULEB128 (tag);
6151 if (name != NULL)
6152 CHECKSUM_STRING (name);
6155 /* Calculate the checksum of a location expression. */
6157 static inline void
6158 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6160 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6161 were emitted as a DW_FORM_sdata instead of a location expression. */
6162 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6164 CHECKSUM_ULEB128 (DW_FORM_sdata);
6165 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6166 return;
6169 /* Otherwise, just checksum the raw location expression. */
6170 while (loc != NULL)
6172 inchash::hash hstate;
6173 hashval_t hash;
6175 CHECKSUM_ULEB128 (loc->dtprel);
6176 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6177 hash_loc_operands (loc, hstate);
6178 hash = hstate.end ();
6179 CHECKSUM (hash);
6180 loc = loc->dw_loc_next;
6184 /* Calculate the checksum of an attribute. */
6186 static void
6187 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6188 struct md5_ctx *ctx, int *mark)
6190 dw_loc_descr_ref loc;
6191 rtx r;
6193 if (AT_class (at) == dw_val_class_die_ref)
6195 dw_die_ref target_die = AT_ref (at);
6197 /* For pointer and reference types, we checksum only the (qualified)
6198 name of the target type (if there is a name). For friend entries,
6199 we checksum only the (qualified) name of the target type or function.
6200 This allows the checksum to remain the same whether the target type
6201 is complete or not. */
6202 if ((at->dw_attr == DW_AT_type
6203 && (tag == DW_TAG_pointer_type
6204 || tag == DW_TAG_reference_type
6205 || tag == DW_TAG_rvalue_reference_type
6206 || tag == DW_TAG_ptr_to_member_type))
6207 || (at->dw_attr == DW_AT_friend
6208 && tag == DW_TAG_friend))
6210 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6212 if (name_attr != NULL)
6214 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6216 if (decl == NULL)
6217 decl = target_die;
6218 CHECKSUM_ULEB128 ('N');
6219 CHECKSUM_ULEB128 (at->dw_attr);
6220 if (decl->die_parent != NULL)
6221 checksum_die_context (decl->die_parent, ctx);
6222 CHECKSUM_ULEB128 ('E');
6223 CHECKSUM_STRING (AT_string (name_attr));
6224 return;
6228 /* For all other references to another DIE, we check to see if the
6229 target DIE has already been visited. If it has, we emit a
6230 backward reference; if not, we descend recursively. */
6231 if (target_die->die_mark > 0)
6233 CHECKSUM_ULEB128 ('R');
6234 CHECKSUM_ULEB128 (at->dw_attr);
6235 CHECKSUM_ULEB128 (target_die->die_mark);
6237 else
6239 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6241 if (decl == NULL)
6242 decl = target_die;
6243 target_die->die_mark = ++(*mark);
6244 CHECKSUM_ULEB128 ('T');
6245 CHECKSUM_ULEB128 (at->dw_attr);
6246 if (decl->die_parent != NULL)
6247 checksum_die_context (decl->die_parent, ctx);
6248 die_checksum_ordered (target_die, ctx, mark);
6250 return;
6253 CHECKSUM_ULEB128 ('A');
6254 CHECKSUM_ULEB128 (at->dw_attr);
6256 switch (AT_class (at))
6258 case dw_val_class_const:
6259 CHECKSUM_ULEB128 (DW_FORM_sdata);
6260 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6261 break;
6263 case dw_val_class_unsigned_const:
6264 CHECKSUM_ULEB128 (DW_FORM_sdata);
6265 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6266 break;
6268 case dw_val_class_const_double:
6269 CHECKSUM_ULEB128 (DW_FORM_block);
6270 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6271 CHECKSUM (at->dw_attr_val.v.val_double);
6272 break;
6274 case dw_val_class_wide_int:
6275 CHECKSUM_ULEB128 (DW_FORM_block);
6276 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6277 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6278 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6279 get_full_len (*at->dw_attr_val.v.val_wide)
6280 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6281 break;
6283 case dw_val_class_vec:
6284 CHECKSUM_ULEB128 (DW_FORM_block);
6285 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6286 * at->dw_attr_val.v.val_vec.elt_size);
6287 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6288 (at->dw_attr_val.v.val_vec.length
6289 * at->dw_attr_val.v.val_vec.elt_size));
6290 break;
6292 case dw_val_class_flag:
6293 CHECKSUM_ULEB128 (DW_FORM_flag);
6294 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6295 break;
6297 case dw_val_class_str:
6298 CHECKSUM_ULEB128 (DW_FORM_string);
6299 CHECKSUM_STRING (AT_string (at));
6300 break;
6302 case dw_val_class_addr:
6303 r = AT_addr (at);
6304 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6305 CHECKSUM_ULEB128 (DW_FORM_string);
6306 CHECKSUM_STRING (XSTR (r, 0));
6307 break;
6309 case dw_val_class_offset:
6310 CHECKSUM_ULEB128 (DW_FORM_sdata);
6311 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6312 break;
6314 case dw_val_class_loc:
6315 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6316 loc_checksum_ordered (loc, ctx);
6317 break;
6319 case dw_val_class_fde_ref:
6320 case dw_val_class_lbl_id:
6321 case dw_val_class_lineptr:
6322 case dw_val_class_macptr:
6323 case dw_val_class_high_pc:
6324 break;
6326 case dw_val_class_file:
6327 CHECKSUM_ULEB128 (DW_FORM_string);
6328 CHECKSUM_STRING (AT_file (at)->filename);
6329 break;
6331 case dw_val_class_data8:
6332 CHECKSUM (at->dw_attr_val.v.val_data8);
6333 break;
6335 default:
6336 break;
6340 struct checksum_attributes
6342 dw_attr_node *at_name;
6343 dw_attr_node *at_type;
6344 dw_attr_node *at_friend;
6345 dw_attr_node *at_accessibility;
6346 dw_attr_node *at_address_class;
6347 dw_attr_node *at_allocated;
6348 dw_attr_node *at_artificial;
6349 dw_attr_node *at_associated;
6350 dw_attr_node *at_binary_scale;
6351 dw_attr_node *at_bit_offset;
6352 dw_attr_node *at_bit_size;
6353 dw_attr_node *at_bit_stride;
6354 dw_attr_node *at_byte_size;
6355 dw_attr_node *at_byte_stride;
6356 dw_attr_node *at_const_value;
6357 dw_attr_node *at_containing_type;
6358 dw_attr_node *at_count;
6359 dw_attr_node *at_data_location;
6360 dw_attr_node *at_data_member_location;
6361 dw_attr_node *at_decimal_scale;
6362 dw_attr_node *at_decimal_sign;
6363 dw_attr_node *at_default_value;
6364 dw_attr_node *at_digit_count;
6365 dw_attr_node *at_discr;
6366 dw_attr_node *at_discr_list;
6367 dw_attr_node *at_discr_value;
6368 dw_attr_node *at_encoding;
6369 dw_attr_node *at_endianity;
6370 dw_attr_node *at_explicit;
6371 dw_attr_node *at_is_optional;
6372 dw_attr_node *at_location;
6373 dw_attr_node *at_lower_bound;
6374 dw_attr_node *at_mutable;
6375 dw_attr_node *at_ordering;
6376 dw_attr_node *at_picture_string;
6377 dw_attr_node *at_prototyped;
6378 dw_attr_node *at_small;
6379 dw_attr_node *at_segment;
6380 dw_attr_node *at_string_length;
6381 dw_attr_node *at_string_length_bit_size;
6382 dw_attr_node *at_string_length_byte_size;
6383 dw_attr_node *at_threads_scaled;
6384 dw_attr_node *at_upper_bound;
6385 dw_attr_node *at_use_location;
6386 dw_attr_node *at_use_UTF8;
6387 dw_attr_node *at_variable_parameter;
6388 dw_attr_node *at_virtuality;
6389 dw_attr_node *at_visibility;
6390 dw_attr_node *at_vtable_elem_location;
6393 /* Collect the attributes that we will want to use for the checksum. */
6395 static void
6396 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6398 dw_attr_node *a;
6399 unsigned ix;
6401 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6403 switch (a->dw_attr)
6405 case DW_AT_name:
6406 attrs->at_name = a;
6407 break;
6408 case DW_AT_type:
6409 attrs->at_type = a;
6410 break;
6411 case DW_AT_friend:
6412 attrs->at_friend = a;
6413 break;
6414 case DW_AT_accessibility:
6415 attrs->at_accessibility = a;
6416 break;
6417 case DW_AT_address_class:
6418 attrs->at_address_class = a;
6419 break;
6420 case DW_AT_allocated:
6421 attrs->at_allocated = a;
6422 break;
6423 case DW_AT_artificial:
6424 attrs->at_artificial = a;
6425 break;
6426 case DW_AT_associated:
6427 attrs->at_associated = a;
6428 break;
6429 case DW_AT_binary_scale:
6430 attrs->at_binary_scale = a;
6431 break;
6432 case DW_AT_bit_offset:
6433 attrs->at_bit_offset = a;
6434 break;
6435 case DW_AT_bit_size:
6436 attrs->at_bit_size = a;
6437 break;
6438 case DW_AT_bit_stride:
6439 attrs->at_bit_stride = a;
6440 break;
6441 case DW_AT_byte_size:
6442 attrs->at_byte_size = a;
6443 break;
6444 case DW_AT_byte_stride:
6445 attrs->at_byte_stride = a;
6446 break;
6447 case DW_AT_const_value:
6448 attrs->at_const_value = a;
6449 break;
6450 case DW_AT_containing_type:
6451 attrs->at_containing_type = a;
6452 break;
6453 case DW_AT_count:
6454 attrs->at_count = a;
6455 break;
6456 case DW_AT_data_location:
6457 attrs->at_data_location = a;
6458 break;
6459 case DW_AT_data_member_location:
6460 attrs->at_data_member_location = a;
6461 break;
6462 case DW_AT_decimal_scale:
6463 attrs->at_decimal_scale = a;
6464 break;
6465 case DW_AT_decimal_sign:
6466 attrs->at_decimal_sign = a;
6467 break;
6468 case DW_AT_default_value:
6469 attrs->at_default_value = a;
6470 break;
6471 case DW_AT_digit_count:
6472 attrs->at_digit_count = a;
6473 break;
6474 case DW_AT_discr:
6475 attrs->at_discr = a;
6476 break;
6477 case DW_AT_discr_list:
6478 attrs->at_discr_list = a;
6479 break;
6480 case DW_AT_discr_value:
6481 attrs->at_discr_value = a;
6482 break;
6483 case DW_AT_encoding:
6484 attrs->at_encoding = a;
6485 break;
6486 case DW_AT_endianity:
6487 attrs->at_endianity = a;
6488 break;
6489 case DW_AT_explicit:
6490 attrs->at_explicit = a;
6491 break;
6492 case DW_AT_is_optional:
6493 attrs->at_is_optional = a;
6494 break;
6495 case DW_AT_location:
6496 attrs->at_location = a;
6497 break;
6498 case DW_AT_lower_bound:
6499 attrs->at_lower_bound = a;
6500 break;
6501 case DW_AT_mutable:
6502 attrs->at_mutable = a;
6503 break;
6504 case DW_AT_ordering:
6505 attrs->at_ordering = a;
6506 break;
6507 case DW_AT_picture_string:
6508 attrs->at_picture_string = a;
6509 break;
6510 case DW_AT_prototyped:
6511 attrs->at_prototyped = a;
6512 break;
6513 case DW_AT_small:
6514 attrs->at_small = a;
6515 break;
6516 case DW_AT_segment:
6517 attrs->at_segment = a;
6518 break;
6519 case DW_AT_string_length:
6520 attrs->at_string_length = a;
6521 break;
6522 case DW_AT_string_length_bit_size:
6523 attrs->at_string_length_bit_size = a;
6524 break;
6525 case DW_AT_string_length_byte_size:
6526 attrs->at_string_length_byte_size = a;
6527 break;
6528 case DW_AT_threads_scaled:
6529 attrs->at_threads_scaled = a;
6530 break;
6531 case DW_AT_upper_bound:
6532 attrs->at_upper_bound = a;
6533 break;
6534 case DW_AT_use_location:
6535 attrs->at_use_location = a;
6536 break;
6537 case DW_AT_use_UTF8:
6538 attrs->at_use_UTF8 = a;
6539 break;
6540 case DW_AT_variable_parameter:
6541 attrs->at_variable_parameter = a;
6542 break;
6543 case DW_AT_virtuality:
6544 attrs->at_virtuality = a;
6545 break;
6546 case DW_AT_visibility:
6547 attrs->at_visibility = a;
6548 break;
6549 case DW_AT_vtable_elem_location:
6550 attrs->at_vtable_elem_location = a;
6551 break;
6552 default:
6553 break;
6558 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6560 static void
6561 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6563 dw_die_ref c;
6564 dw_die_ref decl;
6565 struct checksum_attributes attrs;
6567 CHECKSUM_ULEB128 ('D');
6568 CHECKSUM_ULEB128 (die->die_tag);
6570 memset (&attrs, 0, sizeof (attrs));
6572 decl = get_AT_ref (die, DW_AT_specification);
6573 if (decl != NULL)
6574 collect_checksum_attributes (&attrs, decl);
6575 collect_checksum_attributes (&attrs, die);
6577 CHECKSUM_ATTR (attrs.at_name);
6578 CHECKSUM_ATTR (attrs.at_accessibility);
6579 CHECKSUM_ATTR (attrs.at_address_class);
6580 CHECKSUM_ATTR (attrs.at_allocated);
6581 CHECKSUM_ATTR (attrs.at_artificial);
6582 CHECKSUM_ATTR (attrs.at_associated);
6583 CHECKSUM_ATTR (attrs.at_binary_scale);
6584 CHECKSUM_ATTR (attrs.at_bit_offset);
6585 CHECKSUM_ATTR (attrs.at_bit_size);
6586 CHECKSUM_ATTR (attrs.at_bit_stride);
6587 CHECKSUM_ATTR (attrs.at_byte_size);
6588 CHECKSUM_ATTR (attrs.at_byte_stride);
6589 CHECKSUM_ATTR (attrs.at_const_value);
6590 CHECKSUM_ATTR (attrs.at_containing_type);
6591 CHECKSUM_ATTR (attrs.at_count);
6592 CHECKSUM_ATTR (attrs.at_data_location);
6593 CHECKSUM_ATTR (attrs.at_data_member_location);
6594 CHECKSUM_ATTR (attrs.at_decimal_scale);
6595 CHECKSUM_ATTR (attrs.at_decimal_sign);
6596 CHECKSUM_ATTR (attrs.at_default_value);
6597 CHECKSUM_ATTR (attrs.at_digit_count);
6598 CHECKSUM_ATTR (attrs.at_discr);
6599 CHECKSUM_ATTR (attrs.at_discr_list);
6600 CHECKSUM_ATTR (attrs.at_discr_value);
6601 CHECKSUM_ATTR (attrs.at_encoding);
6602 CHECKSUM_ATTR (attrs.at_endianity);
6603 CHECKSUM_ATTR (attrs.at_explicit);
6604 CHECKSUM_ATTR (attrs.at_is_optional);
6605 CHECKSUM_ATTR (attrs.at_location);
6606 CHECKSUM_ATTR (attrs.at_lower_bound);
6607 CHECKSUM_ATTR (attrs.at_mutable);
6608 CHECKSUM_ATTR (attrs.at_ordering);
6609 CHECKSUM_ATTR (attrs.at_picture_string);
6610 CHECKSUM_ATTR (attrs.at_prototyped);
6611 CHECKSUM_ATTR (attrs.at_small);
6612 CHECKSUM_ATTR (attrs.at_segment);
6613 CHECKSUM_ATTR (attrs.at_string_length);
6614 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6615 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6616 CHECKSUM_ATTR (attrs.at_threads_scaled);
6617 CHECKSUM_ATTR (attrs.at_upper_bound);
6618 CHECKSUM_ATTR (attrs.at_use_location);
6619 CHECKSUM_ATTR (attrs.at_use_UTF8);
6620 CHECKSUM_ATTR (attrs.at_variable_parameter);
6621 CHECKSUM_ATTR (attrs.at_virtuality);
6622 CHECKSUM_ATTR (attrs.at_visibility);
6623 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6624 CHECKSUM_ATTR (attrs.at_type);
6625 CHECKSUM_ATTR (attrs.at_friend);
6627 /* Checksum the child DIEs. */
6628 c = die->die_child;
6629 if (c) do {
6630 dw_attr_node *name_attr;
6632 c = c->die_sib;
6633 name_attr = get_AT (c, DW_AT_name);
6634 if (is_template_instantiation (c))
6636 /* Ignore instantiations of member type and function templates. */
6638 else if (name_attr != NULL
6639 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6641 /* Use a shallow checksum for named nested types and member
6642 functions. */
6643 CHECKSUM_ULEB128 ('S');
6644 CHECKSUM_ULEB128 (c->die_tag);
6645 CHECKSUM_STRING (AT_string (name_attr));
6647 else
6649 /* Use a deep checksum for other children. */
6650 /* Mark this DIE so it gets processed when unmarking. */
6651 if (c->die_mark == 0)
6652 c->die_mark = -1;
6653 die_checksum_ordered (c, ctx, mark);
6655 } while (c != die->die_child);
6657 CHECKSUM_ULEB128 (0);
6660 /* Add a type name and tag to a hash. */
6661 static void
6662 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6664 CHECKSUM_ULEB128 (tag);
6665 CHECKSUM_STRING (name);
6668 #undef CHECKSUM
6669 #undef CHECKSUM_STRING
6670 #undef CHECKSUM_ATTR
6671 #undef CHECKSUM_LEB128
6672 #undef CHECKSUM_ULEB128
6674 /* Generate the type signature for DIE. This is computed by generating an
6675 MD5 checksum over the DIE's tag, its relevant attributes, and its
6676 children. Attributes that are references to other DIEs are processed
6677 by recursion, using the MARK field to prevent infinite recursion.
6678 If the DIE is nested inside a namespace or another type, we also
6679 need to include that context in the signature. The lower 64 bits
6680 of the resulting MD5 checksum comprise the signature. */
6682 static void
6683 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6685 int mark;
6686 const char *name;
6687 unsigned char checksum[16];
6688 struct md5_ctx ctx;
6689 dw_die_ref decl;
6690 dw_die_ref parent;
6692 name = get_AT_string (die, DW_AT_name);
6693 decl = get_AT_ref (die, DW_AT_specification);
6694 parent = get_die_parent (die);
6696 /* First, compute a signature for just the type name (and its surrounding
6697 context, if any. This is stored in the type unit DIE for link-time
6698 ODR (one-definition rule) checking. */
6700 if (is_cxx () && name != NULL)
6702 md5_init_ctx (&ctx);
6704 /* Checksum the names of surrounding namespaces and structures. */
6705 if (parent != NULL)
6706 checksum_die_context (parent, &ctx);
6708 /* Checksum the current DIE. */
6709 die_odr_checksum (die->die_tag, name, &ctx);
6710 md5_finish_ctx (&ctx, checksum);
6712 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6715 /* Next, compute the complete type signature. */
6717 md5_init_ctx (&ctx);
6718 mark = 1;
6719 die->die_mark = mark;
6721 /* Checksum the names of surrounding namespaces and structures. */
6722 if (parent != NULL)
6723 checksum_die_context (parent, &ctx);
6725 /* Checksum the DIE and its children. */
6726 die_checksum_ordered (die, &ctx, &mark);
6727 unmark_all_dies (die);
6728 md5_finish_ctx (&ctx, checksum);
6730 /* Store the signature in the type node and link the type DIE and the
6731 type node together. */
6732 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6733 DWARF_TYPE_SIGNATURE_SIZE);
6734 die->comdat_type_p = true;
6735 die->die_id.die_type_node = type_node;
6736 type_node->type_die = die;
6738 /* If the DIE is a specification, link its declaration to the type node
6739 as well. */
6740 if (decl != NULL)
6742 decl->comdat_type_p = true;
6743 decl->die_id.die_type_node = type_node;
6747 /* Do the location expressions look same? */
6748 static inline int
6749 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6751 return loc1->dw_loc_opc == loc2->dw_loc_opc
6752 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6753 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6756 /* Do the values look the same? */
6757 static int
6758 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6760 dw_loc_descr_ref loc1, loc2;
6761 rtx r1, r2;
6763 if (v1->val_class != v2->val_class)
6764 return 0;
6766 switch (v1->val_class)
6768 case dw_val_class_const:
6769 return v1->v.val_int == v2->v.val_int;
6770 case dw_val_class_unsigned_const:
6771 return v1->v.val_unsigned == v2->v.val_unsigned;
6772 case dw_val_class_const_double:
6773 return v1->v.val_double.high == v2->v.val_double.high
6774 && v1->v.val_double.low == v2->v.val_double.low;
6775 case dw_val_class_wide_int:
6776 return *v1->v.val_wide == *v2->v.val_wide;
6777 case dw_val_class_vec:
6778 if (v1->v.val_vec.length != v2->v.val_vec.length
6779 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6780 return 0;
6781 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6782 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6783 return 0;
6784 return 1;
6785 case dw_val_class_flag:
6786 return v1->v.val_flag == v2->v.val_flag;
6787 case dw_val_class_str:
6788 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6790 case dw_val_class_addr:
6791 r1 = v1->v.val_addr;
6792 r2 = v2->v.val_addr;
6793 if (GET_CODE (r1) != GET_CODE (r2))
6794 return 0;
6795 return !rtx_equal_p (r1, r2);
6797 case dw_val_class_offset:
6798 return v1->v.val_offset == v2->v.val_offset;
6800 case dw_val_class_loc:
6801 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6802 loc1 && loc2;
6803 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6804 if (!same_loc_p (loc1, loc2, mark))
6805 return 0;
6806 return !loc1 && !loc2;
6808 case dw_val_class_die_ref:
6809 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6811 case dw_val_class_fde_ref:
6812 case dw_val_class_vms_delta:
6813 case dw_val_class_lbl_id:
6814 case dw_val_class_lineptr:
6815 case dw_val_class_macptr:
6816 case dw_val_class_high_pc:
6817 return 1;
6819 case dw_val_class_file:
6820 return v1->v.val_file == v2->v.val_file;
6822 case dw_val_class_data8:
6823 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6825 default:
6826 return 1;
6830 /* Do the attributes look the same? */
6832 static int
6833 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6835 if (at1->dw_attr != at2->dw_attr)
6836 return 0;
6838 /* We don't care that this was compiled with a different compiler
6839 snapshot; if the output is the same, that's what matters. */
6840 if (at1->dw_attr == DW_AT_producer)
6841 return 1;
6843 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6846 /* Do the dies look the same? */
6848 static int
6849 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6851 dw_die_ref c1, c2;
6852 dw_attr_node *a1;
6853 unsigned ix;
6855 /* To avoid infinite recursion. */
6856 if (die1->die_mark)
6857 return die1->die_mark == die2->die_mark;
6858 die1->die_mark = die2->die_mark = ++(*mark);
6860 if (die1->die_tag != die2->die_tag)
6861 return 0;
6863 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6864 return 0;
6866 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6867 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6868 return 0;
6870 c1 = die1->die_child;
6871 c2 = die2->die_child;
6872 if (! c1)
6874 if (c2)
6875 return 0;
6877 else
6878 for (;;)
6880 if (!same_die_p (c1, c2, mark))
6881 return 0;
6882 c1 = c1->die_sib;
6883 c2 = c2->die_sib;
6884 if (c1 == die1->die_child)
6886 if (c2 == die2->die_child)
6887 break;
6888 else
6889 return 0;
6893 return 1;
6896 /* Do the dies look the same? Wrapper around same_die_p. */
6898 static int
6899 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6901 int mark = 0;
6902 int ret = same_die_p (die1, die2, &mark);
6904 unmark_all_dies (die1);
6905 unmark_all_dies (die2);
6907 return ret;
6910 /* The prefix to attach to symbols on DIEs in the current comdat debug
6911 info section. */
6912 static const char *comdat_symbol_id;
6914 /* The index of the current symbol within the current comdat CU. */
6915 static unsigned int comdat_symbol_number;
6917 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6918 children, and set comdat_symbol_id accordingly. */
6920 static void
6921 compute_section_prefix (dw_die_ref unit_die)
6923 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6924 const char *base = die_name ? lbasename (die_name) : "anonymous";
6925 char *name = XALLOCAVEC (char, strlen (base) + 64);
6926 char *p;
6927 int i, mark;
6928 unsigned char checksum[16];
6929 struct md5_ctx ctx;
6931 /* Compute the checksum of the DIE, then append part of it as hex digits to
6932 the name filename of the unit. */
6934 md5_init_ctx (&ctx);
6935 mark = 0;
6936 die_checksum (unit_die, &ctx, &mark);
6937 unmark_all_dies (unit_die);
6938 md5_finish_ctx (&ctx, checksum);
6940 sprintf (name, "%s.", base);
6941 clean_symbol_name (name);
6943 p = name + strlen (name);
6944 for (i = 0; i < 4; i++)
6946 sprintf (p, "%.2x", checksum[i]);
6947 p += 2;
6950 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6951 comdat_symbol_number = 0;
6954 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6956 static int
6957 is_type_die (dw_die_ref die)
6959 switch (die->die_tag)
6961 case DW_TAG_array_type:
6962 case DW_TAG_class_type:
6963 case DW_TAG_interface_type:
6964 case DW_TAG_enumeration_type:
6965 case DW_TAG_pointer_type:
6966 case DW_TAG_reference_type:
6967 case DW_TAG_rvalue_reference_type:
6968 case DW_TAG_string_type:
6969 case DW_TAG_structure_type:
6970 case DW_TAG_subroutine_type:
6971 case DW_TAG_union_type:
6972 case DW_TAG_ptr_to_member_type:
6973 case DW_TAG_set_type:
6974 case DW_TAG_subrange_type:
6975 case DW_TAG_base_type:
6976 case DW_TAG_const_type:
6977 case DW_TAG_file_type:
6978 case DW_TAG_packed_type:
6979 case DW_TAG_volatile_type:
6980 case DW_TAG_typedef:
6981 return 1;
6982 default:
6983 return 0;
6987 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6988 Basically, we want to choose the bits that are likely to be shared between
6989 compilations (types) and leave out the bits that are specific to individual
6990 compilations (functions). */
6992 static int
6993 is_comdat_die (dw_die_ref c)
6995 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6996 we do for stabs. The advantage is a greater likelihood of sharing between
6997 objects that don't include headers in the same order (and therefore would
6998 put the base types in a different comdat). jason 8/28/00 */
7000 if (c->die_tag == DW_TAG_base_type)
7001 return 0;
7003 if (c->die_tag == DW_TAG_pointer_type
7004 || c->die_tag == DW_TAG_reference_type
7005 || c->die_tag == DW_TAG_rvalue_reference_type
7006 || c->die_tag == DW_TAG_const_type
7007 || c->die_tag == DW_TAG_volatile_type)
7009 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7011 return t ? is_comdat_die (t) : 0;
7014 return is_type_die (c);
7017 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7018 compilation unit. */
7020 static int
7021 is_symbol_die (dw_die_ref c)
7023 return (is_type_die (c)
7024 || is_declaration_die (c)
7025 || c->die_tag == DW_TAG_namespace
7026 || c->die_tag == DW_TAG_module);
7029 /* Returns true iff C is a compile-unit DIE. */
7031 static inline bool
7032 is_cu_die (dw_die_ref c)
7034 return c && c->die_tag == DW_TAG_compile_unit;
7037 /* Returns true iff C is a unit DIE of some sort. */
7039 static inline bool
7040 is_unit_die (dw_die_ref c)
7042 return c && (c->die_tag == DW_TAG_compile_unit
7043 || c->die_tag == DW_TAG_partial_unit
7044 || c->die_tag == DW_TAG_type_unit);
7047 /* Returns true iff C is a namespace DIE. */
7049 static inline bool
7050 is_namespace_die (dw_die_ref c)
7052 return c && c->die_tag == DW_TAG_namespace;
7055 /* Returns true iff C is a class or structure DIE. */
7057 static inline bool
7058 is_class_die (dw_die_ref c)
7060 return c && (c->die_tag == DW_TAG_class_type
7061 || c->die_tag == DW_TAG_structure_type);
7064 /* Return non-zero if this DIE is a template parameter. */
7066 static inline bool
7067 is_template_parameter (dw_die_ref die)
7069 switch (die->die_tag)
7071 case DW_TAG_template_type_param:
7072 case DW_TAG_template_value_param:
7073 case DW_TAG_GNU_template_template_param:
7074 case DW_TAG_GNU_template_parameter_pack:
7075 return true;
7076 default:
7077 return false;
7081 /* Return non-zero if this DIE represents a template instantiation. */
7083 static inline bool
7084 is_template_instantiation (dw_die_ref die)
7086 dw_die_ref c;
7088 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7089 return false;
7090 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7091 return false;
7094 static char *
7095 gen_internal_sym (const char *prefix)
7097 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7099 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7100 return xstrdup (buf);
7103 /* Assign symbols to all worthy DIEs under DIE. */
7105 static void
7106 assign_symbol_names (dw_die_ref die)
7108 dw_die_ref c;
7110 if (is_symbol_die (die) && !die->comdat_type_p)
7112 if (comdat_symbol_id)
7114 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7116 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7117 comdat_symbol_id, comdat_symbol_number++);
7118 die->die_id.die_symbol = xstrdup (p);
7120 else
7121 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7124 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7127 struct cu_hash_table_entry
7129 dw_die_ref cu;
7130 unsigned min_comdat_num, max_comdat_num;
7131 struct cu_hash_table_entry *next;
7134 /* Helpers to manipulate hash table of CUs. */
7136 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7138 typedef die_struct *compare_type;
7139 static inline hashval_t hash (const cu_hash_table_entry *);
7140 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7141 static inline void remove (cu_hash_table_entry *);
7144 inline hashval_t
7145 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7147 return htab_hash_string (entry->cu->die_id.die_symbol);
7150 inline bool
7151 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7152 const die_struct *entry2)
7154 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7157 inline void
7158 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7160 struct cu_hash_table_entry *next;
7162 while (entry)
7164 next = entry->next;
7165 free (entry);
7166 entry = next;
7170 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7172 /* Check whether we have already seen this CU and set up SYM_NUM
7173 accordingly. */
7174 static int
7175 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7177 struct cu_hash_table_entry dummy;
7178 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7180 dummy.max_comdat_num = 0;
7182 slot = htable->find_slot_with_hash (cu,
7183 htab_hash_string (cu->die_id.die_symbol),
7184 INSERT);
7185 entry = *slot;
7187 for (; entry; last = entry, entry = entry->next)
7189 if (same_die_p_wrap (cu, entry->cu))
7190 break;
7193 if (entry)
7195 *sym_num = entry->min_comdat_num;
7196 return 1;
7199 entry = XCNEW (struct cu_hash_table_entry);
7200 entry->cu = cu;
7201 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7202 entry->next = *slot;
7203 *slot = entry;
7205 return 0;
7208 /* Record SYM_NUM to record of CU in HTABLE. */
7209 static void
7210 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7211 unsigned int sym_num)
7213 struct cu_hash_table_entry **slot, *entry;
7215 slot = htable->find_slot_with_hash (cu,
7216 htab_hash_string (cu->die_id.die_symbol),
7217 NO_INSERT);
7218 entry = *slot;
7220 entry->max_comdat_num = sym_num;
7223 /* Traverse the DIE (which is always comp_unit_die), and set up
7224 additional compilation units for each of the include files we see
7225 bracketed by BINCL/EINCL. */
7227 static void
7228 break_out_includes (dw_die_ref die)
7230 dw_die_ref c;
7231 dw_die_ref unit = NULL;
7232 limbo_die_node *node, **pnode;
7234 c = die->die_child;
7235 if (c) do {
7236 dw_die_ref prev = c;
7237 c = c->die_sib;
7238 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7239 || (unit && is_comdat_die (c)))
7241 dw_die_ref next = c->die_sib;
7243 /* This DIE is for a secondary CU; remove it from the main one. */
7244 remove_child_with_prev (c, prev);
7246 if (c->die_tag == DW_TAG_GNU_BINCL)
7247 unit = push_new_compile_unit (unit, c);
7248 else if (c->die_tag == DW_TAG_GNU_EINCL)
7249 unit = pop_compile_unit (unit);
7250 else
7251 add_child_die (unit, c);
7252 c = next;
7253 if (c == die->die_child)
7254 break;
7256 } while (c != die->die_child);
7258 #if 0
7259 /* We can only use this in debugging, since the frontend doesn't check
7260 to make sure that we leave every include file we enter. */
7261 gcc_assert (!unit);
7262 #endif
7264 assign_symbol_names (die);
7265 cu_hash_type cu_hash_table (10);
7266 for (node = limbo_die_list, pnode = &limbo_die_list;
7267 node;
7268 node = node->next)
7270 int is_dupl;
7272 compute_section_prefix (node->die);
7273 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7274 &comdat_symbol_number);
7275 assign_symbol_names (node->die);
7276 if (is_dupl)
7277 *pnode = node->next;
7278 else
7280 pnode = &node->next;
7281 record_comdat_symbol_number (node->die, &cu_hash_table,
7282 comdat_symbol_number);
7287 /* Return non-zero if this DIE is a declaration. */
7289 static int
7290 is_declaration_die (dw_die_ref die)
7292 dw_attr_node *a;
7293 unsigned ix;
7295 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7296 if (a->dw_attr == DW_AT_declaration)
7297 return 1;
7299 return 0;
7302 /* Return non-zero if this DIE is nested inside a subprogram. */
7304 static int
7305 is_nested_in_subprogram (dw_die_ref die)
7307 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7309 if (decl == NULL)
7310 decl = die;
7311 return local_scope_p (decl);
7314 /* Return non-zero if this DIE contains a defining declaration of a
7315 subprogram. */
7317 static int
7318 contains_subprogram_definition (dw_die_ref die)
7320 dw_die_ref c;
7322 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7323 return 1;
7324 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7325 return 0;
7328 /* Return non-zero if this is a type DIE that should be moved to a
7329 COMDAT .debug_types section. */
7331 static int
7332 should_move_die_to_comdat (dw_die_ref die)
7334 switch (die->die_tag)
7336 case DW_TAG_class_type:
7337 case DW_TAG_structure_type:
7338 case DW_TAG_enumeration_type:
7339 case DW_TAG_union_type:
7340 /* Don't move declarations, inlined instances, types nested in a
7341 subprogram, or types that contain subprogram definitions. */
7342 if (is_declaration_die (die)
7343 || get_AT (die, DW_AT_abstract_origin)
7344 || is_nested_in_subprogram (die)
7345 || contains_subprogram_definition (die))
7346 return 0;
7347 return 1;
7348 case DW_TAG_array_type:
7349 case DW_TAG_interface_type:
7350 case DW_TAG_pointer_type:
7351 case DW_TAG_reference_type:
7352 case DW_TAG_rvalue_reference_type:
7353 case DW_TAG_string_type:
7354 case DW_TAG_subroutine_type:
7355 case DW_TAG_ptr_to_member_type:
7356 case DW_TAG_set_type:
7357 case DW_TAG_subrange_type:
7358 case DW_TAG_base_type:
7359 case DW_TAG_const_type:
7360 case DW_TAG_file_type:
7361 case DW_TAG_packed_type:
7362 case DW_TAG_volatile_type:
7363 case DW_TAG_typedef:
7364 default:
7365 return 0;
7369 /* Make a clone of DIE. */
7371 static dw_die_ref
7372 clone_die (dw_die_ref die)
7374 dw_die_ref clone;
7375 dw_attr_node *a;
7376 unsigned ix;
7378 clone = ggc_cleared_alloc<die_node> ();
7379 clone->die_tag = die->die_tag;
7381 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7382 add_dwarf_attr (clone, a);
7384 return clone;
7387 /* Make a clone of the tree rooted at DIE. */
7389 static dw_die_ref
7390 clone_tree (dw_die_ref die)
7392 dw_die_ref c;
7393 dw_die_ref clone = clone_die (die);
7395 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7397 return clone;
7400 /* Make a clone of DIE as a declaration. */
7402 static dw_die_ref
7403 clone_as_declaration (dw_die_ref die)
7405 dw_die_ref clone;
7406 dw_die_ref decl;
7407 dw_attr_node *a;
7408 unsigned ix;
7410 /* If the DIE is already a declaration, just clone it. */
7411 if (is_declaration_die (die))
7412 return clone_die (die);
7414 /* If the DIE is a specification, just clone its declaration DIE. */
7415 decl = get_AT_ref (die, DW_AT_specification);
7416 if (decl != NULL)
7418 clone = clone_die (decl);
7419 if (die->comdat_type_p)
7420 add_AT_die_ref (clone, DW_AT_signature, die);
7421 return clone;
7424 clone = ggc_cleared_alloc<die_node> ();
7425 clone->die_tag = die->die_tag;
7427 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7429 /* We don't want to copy over all attributes.
7430 For example we don't want DW_AT_byte_size because otherwise we will no
7431 longer have a declaration and GDB will treat it as a definition. */
7433 switch (a->dw_attr)
7435 case DW_AT_abstract_origin:
7436 case DW_AT_artificial:
7437 case DW_AT_containing_type:
7438 case DW_AT_external:
7439 case DW_AT_name:
7440 case DW_AT_type:
7441 case DW_AT_virtuality:
7442 case DW_AT_linkage_name:
7443 case DW_AT_MIPS_linkage_name:
7444 add_dwarf_attr (clone, a);
7445 break;
7446 case DW_AT_byte_size:
7447 default:
7448 break;
7452 if (die->comdat_type_p)
7453 add_AT_die_ref (clone, DW_AT_signature, die);
7455 add_AT_flag (clone, DW_AT_declaration, 1);
7456 return clone;
7460 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7462 struct decl_table_entry
7464 dw_die_ref orig;
7465 dw_die_ref copy;
7468 /* Helpers to manipulate hash table of copied declarations. */
7470 /* Hashtable helpers. */
7472 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7474 typedef die_struct *compare_type;
7475 static inline hashval_t hash (const decl_table_entry *);
7476 static inline bool equal (const decl_table_entry *, const die_struct *);
7479 inline hashval_t
7480 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7482 return htab_hash_pointer (entry->orig);
7485 inline bool
7486 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7487 const die_struct *entry2)
7489 return entry1->orig == entry2;
7492 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7494 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7495 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7496 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7497 to check if the ancestor has already been copied into UNIT. */
7499 static dw_die_ref
7500 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7501 decl_hash_type *decl_table)
7503 dw_die_ref parent = die->die_parent;
7504 dw_die_ref new_parent = unit;
7505 dw_die_ref copy;
7506 decl_table_entry **slot = NULL;
7507 struct decl_table_entry *entry = NULL;
7509 if (decl_table)
7511 /* Check if the entry has already been copied to UNIT. */
7512 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7513 INSERT);
7514 if (*slot != HTAB_EMPTY_ENTRY)
7516 entry = *slot;
7517 return entry->copy;
7520 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7521 entry = XCNEW (struct decl_table_entry);
7522 entry->orig = die;
7523 entry->copy = NULL;
7524 *slot = entry;
7527 if (parent != NULL)
7529 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7530 if (spec != NULL)
7531 parent = spec;
7532 if (!is_unit_die (parent))
7533 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7536 copy = clone_as_declaration (die);
7537 add_child_die (new_parent, copy);
7539 if (decl_table)
7541 /* Record the pointer to the copy. */
7542 entry->copy = copy;
7545 return copy;
7547 /* Copy the declaration context to the new type unit DIE. This includes
7548 any surrounding namespace or type declarations. If the DIE has an
7549 AT_specification attribute, it also includes attributes and children
7550 attached to the specification, and returns a pointer to the original
7551 parent of the declaration DIE. Returns NULL otherwise. */
7553 static dw_die_ref
7554 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7556 dw_die_ref decl;
7557 dw_die_ref new_decl;
7558 dw_die_ref orig_parent = NULL;
7560 decl = get_AT_ref (die, DW_AT_specification);
7561 if (decl == NULL)
7562 decl = die;
7563 else
7565 unsigned ix;
7566 dw_die_ref c;
7567 dw_attr_node *a;
7569 /* The original DIE will be changed to a declaration, and must
7570 be moved to be a child of the original declaration DIE. */
7571 orig_parent = decl->die_parent;
7573 /* Copy the type node pointer from the new DIE to the original
7574 declaration DIE so we can forward references later. */
7575 decl->comdat_type_p = true;
7576 decl->die_id.die_type_node = die->die_id.die_type_node;
7578 remove_AT (die, DW_AT_specification);
7580 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7582 if (a->dw_attr != DW_AT_name
7583 && a->dw_attr != DW_AT_declaration
7584 && a->dw_attr != DW_AT_external)
7585 add_dwarf_attr (die, a);
7588 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7591 if (decl->die_parent != NULL
7592 && !is_unit_die (decl->die_parent))
7594 new_decl = copy_ancestor_tree (unit, decl, NULL);
7595 if (new_decl != NULL)
7597 remove_AT (new_decl, DW_AT_signature);
7598 add_AT_specification (die, new_decl);
7602 return orig_parent;
7605 /* Generate the skeleton ancestor tree for the given NODE, then clone
7606 the DIE and add the clone into the tree. */
7608 static void
7609 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7611 if (node->new_die != NULL)
7612 return;
7614 node->new_die = clone_as_declaration (node->old_die);
7616 if (node->parent != NULL)
7618 generate_skeleton_ancestor_tree (node->parent);
7619 add_child_die (node->parent->new_die, node->new_die);
7623 /* Generate a skeleton tree of DIEs containing any declarations that are
7624 found in the original tree. We traverse the tree looking for declaration
7625 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7627 static void
7628 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7630 skeleton_chain_node node;
7631 dw_die_ref c;
7632 dw_die_ref first;
7633 dw_die_ref prev = NULL;
7634 dw_die_ref next = NULL;
7636 node.parent = parent;
7638 first = c = parent->old_die->die_child;
7639 if (c)
7640 next = c->die_sib;
7641 if (c) do {
7642 if (prev == NULL || prev->die_sib == c)
7643 prev = c;
7644 c = next;
7645 next = (c == first ? NULL : c->die_sib);
7646 node.old_die = c;
7647 node.new_die = NULL;
7648 if (is_declaration_die (c))
7650 if (is_template_instantiation (c))
7652 /* Instantiated templates do not need to be cloned into the
7653 type unit. Just move the DIE and its children back to
7654 the skeleton tree (in the main CU). */
7655 remove_child_with_prev (c, prev);
7656 add_child_die (parent->new_die, c);
7657 c = prev;
7659 else
7661 /* Clone the existing DIE, move the original to the skeleton
7662 tree (which is in the main CU), and put the clone, with
7663 all the original's children, where the original came from
7664 (which is about to be moved to the type unit). */
7665 dw_die_ref clone = clone_die (c);
7666 move_all_children (c, clone);
7668 /* If the original has a DW_AT_object_pointer attribute,
7669 it would now point to a child DIE just moved to the
7670 cloned tree, so we need to remove that attribute from
7671 the original. */
7672 remove_AT (c, DW_AT_object_pointer);
7674 replace_child (c, clone, prev);
7675 generate_skeleton_ancestor_tree (parent);
7676 add_child_die (parent->new_die, c);
7677 node.new_die = c;
7678 c = clone;
7681 generate_skeleton_bottom_up (&node);
7682 } while (next != NULL);
7685 /* Wrapper function for generate_skeleton_bottom_up. */
7687 static dw_die_ref
7688 generate_skeleton (dw_die_ref die)
7690 skeleton_chain_node node;
7692 node.old_die = die;
7693 node.new_die = NULL;
7694 node.parent = NULL;
7696 /* If this type definition is nested inside another type,
7697 and is not an instantiation of a template, always leave
7698 at least a declaration in its place. */
7699 if (die->die_parent != NULL
7700 && is_type_die (die->die_parent)
7701 && !is_template_instantiation (die))
7702 node.new_die = clone_as_declaration (die);
7704 generate_skeleton_bottom_up (&node);
7705 return node.new_die;
7708 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7709 declaration. The original DIE is moved to a new compile unit so that
7710 existing references to it follow it to the new location. If any of the
7711 original DIE's descendants is a declaration, we need to replace the
7712 original DIE with a skeleton tree and move the declarations back into the
7713 skeleton tree. */
7715 static dw_die_ref
7716 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7717 dw_die_ref prev)
7719 dw_die_ref skeleton, orig_parent;
7721 /* Copy the declaration context to the type unit DIE. If the returned
7722 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7723 that DIE. */
7724 orig_parent = copy_declaration_context (unit, child);
7726 skeleton = generate_skeleton (child);
7727 if (skeleton == NULL)
7728 remove_child_with_prev (child, prev);
7729 else
7731 skeleton->comdat_type_p = true;
7732 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7734 /* If the original DIE was a specification, we need to put
7735 the skeleton under the parent DIE of the declaration.
7736 This leaves the original declaration in the tree, but
7737 it will be pruned later since there are no longer any
7738 references to it. */
7739 if (orig_parent != NULL)
7741 remove_child_with_prev (child, prev);
7742 add_child_die (orig_parent, skeleton);
7744 else
7745 replace_child (child, skeleton, prev);
7748 return skeleton;
7751 static void
7752 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7753 comdat_type_node *type_node,
7754 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7756 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7757 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7758 DWARF procedure references in the DW_AT_location attribute. */
7760 static dw_die_ref
7761 copy_dwarf_procedure (dw_die_ref die,
7762 comdat_type_node *type_node,
7763 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7765 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7767 /* DWARF procedures are not supposed to have children... */
7768 gcc_assert (die->die_child == NULL);
7770 /* ... and they are supposed to have only one attribute: DW_AT_location. */
7771 gcc_assert (vec_safe_length (die->die_attr) == 1
7772 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7774 /* Do not copy more than once DWARF procedures. */
7775 bool existed;
7776 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7777 if (existed)
7778 return die_copy;
7780 die_copy = clone_die (die);
7781 add_child_die (type_node->root_die, die_copy);
7782 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7783 return die_copy;
7786 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
7787 procedures in DIE's attributes. */
7789 static void
7790 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7791 comdat_type_node *type_node,
7792 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7794 dw_attr_node *a;
7795 unsigned i;
7797 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7799 dw_loc_descr_ref loc;
7801 if (a->dw_attr_val.val_class != dw_val_class_loc)
7802 continue;
7804 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7806 switch (loc->dw_loc_opc)
7808 case DW_OP_call2:
7809 case DW_OP_call4:
7810 case DW_OP_call_ref:
7811 gcc_assert (loc->dw_loc_oprnd1.val_class
7812 == dw_val_class_die_ref);
7813 loc->dw_loc_oprnd1.v.val_die_ref.die
7814 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7815 type_node,
7816 copied_dwarf_procs);
7818 default:
7819 break;
7825 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7826 rewrite references to point to the copies.
7828 References are looked for in DIE's attributes and recursively in all its
7829 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7830 mapping from old DWARF procedures to their copy. It is used not to copy
7831 twice the same DWARF procedure under TYPE_NODE. */
7833 static void
7834 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7835 comdat_type_node *type_node,
7836 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7838 dw_die_ref c;
7840 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7841 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7842 type_node,
7843 copied_dwarf_procs));
7846 /* Traverse the DIE and set up additional .debug_types sections for each
7847 type worthy of being placed in a COMDAT section. */
7849 static void
7850 break_out_comdat_types (dw_die_ref die)
7852 dw_die_ref c;
7853 dw_die_ref first;
7854 dw_die_ref prev = NULL;
7855 dw_die_ref next = NULL;
7856 dw_die_ref unit = NULL;
7858 first = c = die->die_child;
7859 if (c)
7860 next = c->die_sib;
7861 if (c) do {
7862 if (prev == NULL || prev->die_sib == c)
7863 prev = c;
7864 c = next;
7865 next = (c == first ? NULL : c->die_sib);
7866 if (should_move_die_to_comdat (c))
7868 dw_die_ref replacement;
7869 comdat_type_node *type_node;
7871 /* Break out nested types into their own type units. */
7872 break_out_comdat_types (c);
7874 /* Create a new type unit DIE as the root for the new tree, and
7875 add it to the list of comdat types. */
7876 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7877 add_AT_unsigned (unit, DW_AT_language,
7878 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7879 type_node = ggc_cleared_alloc<comdat_type_node> ();
7880 type_node->root_die = unit;
7881 type_node->next = comdat_type_list;
7882 comdat_type_list = type_node;
7884 /* Generate the type signature. */
7885 generate_type_signature (c, type_node);
7887 /* Copy the declaration context, attributes, and children of the
7888 declaration into the new type unit DIE, then remove this DIE
7889 from the main CU (or replace it with a skeleton if necessary). */
7890 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7891 type_node->skeleton_die = replacement;
7893 /* Add the DIE to the new compunit. */
7894 add_child_die (unit, c);
7896 /* Types can reference DWARF procedures for type size or data location
7897 expressions. Calls in DWARF expressions cannot target procedures
7898 that are not in the same section. So we must copy DWARF procedures
7899 along with this type and then rewrite references to them. */
7900 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7901 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7903 if (replacement != NULL)
7904 c = replacement;
7906 else if (c->die_tag == DW_TAG_namespace
7907 || c->die_tag == DW_TAG_class_type
7908 || c->die_tag == DW_TAG_structure_type
7909 || c->die_tag == DW_TAG_union_type)
7911 /* Look for nested types that can be broken out. */
7912 break_out_comdat_types (c);
7914 } while (next != NULL);
7917 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7918 Enter all the cloned children into the hash table decl_table. */
7920 static dw_die_ref
7921 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7923 dw_die_ref c;
7924 dw_die_ref clone;
7925 struct decl_table_entry *entry;
7926 decl_table_entry **slot;
7928 if (die->die_tag == DW_TAG_subprogram)
7929 clone = clone_as_declaration (die);
7930 else
7931 clone = clone_die (die);
7933 slot = decl_table->find_slot_with_hash (die,
7934 htab_hash_pointer (die), INSERT);
7936 /* Assert that DIE isn't in the hash table yet. If it would be there
7937 before, the ancestors would be necessarily there as well, therefore
7938 clone_tree_partial wouldn't be called. */
7939 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7941 entry = XCNEW (struct decl_table_entry);
7942 entry->orig = die;
7943 entry->copy = clone;
7944 *slot = entry;
7946 if (die->die_tag != DW_TAG_subprogram)
7947 FOR_EACH_CHILD (die, c,
7948 add_child_die (clone, clone_tree_partial (c, decl_table)));
7950 return clone;
7953 /* Walk the DIE and its children, looking for references to incomplete
7954 or trivial types that are unmarked (i.e., that are not in the current
7955 type_unit). */
7957 static void
7958 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7960 dw_die_ref c;
7961 dw_attr_node *a;
7962 unsigned ix;
7964 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7966 if (AT_class (a) == dw_val_class_die_ref)
7968 dw_die_ref targ = AT_ref (a);
7969 decl_table_entry **slot;
7970 struct decl_table_entry *entry;
7972 if (targ->die_mark != 0 || targ->comdat_type_p)
7973 continue;
7975 slot = decl_table->find_slot_with_hash (targ,
7976 htab_hash_pointer (targ),
7977 INSERT);
7979 if (*slot != HTAB_EMPTY_ENTRY)
7981 /* TARG has already been copied, so we just need to
7982 modify the reference to point to the copy. */
7983 entry = *slot;
7984 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7986 else
7988 dw_die_ref parent = unit;
7989 dw_die_ref copy = clone_die (targ);
7991 /* Record in DECL_TABLE that TARG has been copied.
7992 Need to do this now, before the recursive call,
7993 because DECL_TABLE may be expanded and SLOT
7994 would no longer be a valid pointer. */
7995 entry = XCNEW (struct decl_table_entry);
7996 entry->orig = targ;
7997 entry->copy = copy;
7998 *slot = entry;
8000 /* If TARG is not a declaration DIE, we need to copy its
8001 children. */
8002 if (!is_declaration_die (targ))
8004 FOR_EACH_CHILD (
8005 targ, c,
8006 add_child_die (copy,
8007 clone_tree_partial (c, decl_table)));
8010 /* Make sure the cloned tree is marked as part of the
8011 type unit. */
8012 mark_dies (copy);
8014 /* If TARG has surrounding context, copy its ancestor tree
8015 into the new type unit. */
8016 if (targ->die_parent != NULL
8017 && !is_unit_die (targ->die_parent))
8018 parent = copy_ancestor_tree (unit, targ->die_parent,
8019 decl_table);
8021 add_child_die (parent, copy);
8022 a->dw_attr_val.v.val_die_ref.die = copy;
8024 /* Make sure the newly-copied DIE is walked. If it was
8025 installed in a previously-added context, it won't
8026 get visited otherwise. */
8027 if (parent != unit)
8029 /* Find the highest point of the newly-added tree,
8030 mark each node along the way, and walk from there. */
8031 parent->die_mark = 1;
8032 while (parent->die_parent
8033 && parent->die_parent->die_mark == 0)
8035 parent = parent->die_parent;
8036 parent->die_mark = 1;
8038 copy_decls_walk (unit, parent, decl_table);
8044 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8047 /* Copy declarations for "unworthy" types into the new comdat section.
8048 Incomplete types, modified types, and certain other types aren't broken
8049 out into comdat sections of their own, so they don't have a signature,
8050 and we need to copy the declaration into the same section so that we
8051 don't have an external reference. */
8053 static void
8054 copy_decls_for_unworthy_types (dw_die_ref unit)
8056 mark_dies (unit);
8057 decl_hash_type decl_table (10);
8058 copy_decls_walk (unit, unit, &decl_table);
8059 unmark_dies (unit);
8062 /* Traverse the DIE and add a sibling attribute if it may have the
8063 effect of speeding up access to siblings. To save some space,
8064 avoid generating sibling attributes for DIE's without children. */
8066 static void
8067 add_sibling_attributes (dw_die_ref die)
8069 dw_die_ref c;
8071 if (! die->die_child)
8072 return;
8074 if (die->die_parent && die != die->die_parent->die_child)
8075 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8077 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8080 /* Output all location lists for the DIE and its children. */
8082 static void
8083 output_location_lists (dw_die_ref die)
8085 dw_die_ref c;
8086 dw_attr_node *a;
8087 unsigned ix;
8089 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8090 if (AT_class (a) == dw_val_class_loc_list)
8091 output_loc_list (AT_loc_list (a));
8093 FOR_EACH_CHILD (die, c, output_location_lists (c));
8096 /* We want to limit the number of external references, because they are
8097 larger than local references: a relocation takes multiple words, and
8098 even a sig8 reference is always eight bytes, whereas a local reference
8099 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8100 So if we encounter multiple external references to the same type DIE, we
8101 make a local typedef stub for it and redirect all references there.
8103 This is the element of the hash table for keeping track of these
8104 references. */
8106 struct external_ref
8108 dw_die_ref type;
8109 dw_die_ref stub;
8110 unsigned n_refs;
8113 /* Hashtable helpers. */
8115 struct external_ref_hasher : free_ptr_hash <external_ref>
8117 static inline hashval_t hash (const external_ref *);
8118 static inline bool equal (const external_ref *, const external_ref *);
8121 inline hashval_t
8122 external_ref_hasher::hash (const external_ref *r)
8124 dw_die_ref die = r->type;
8125 hashval_t h = 0;
8127 /* We can't use the address of the DIE for hashing, because
8128 that will make the order of the stub DIEs non-deterministic. */
8129 if (! die->comdat_type_p)
8130 /* We have a symbol; use it to compute a hash. */
8131 h = htab_hash_string (die->die_id.die_symbol);
8132 else
8134 /* We have a type signature; use a subset of the bits as the hash.
8135 The 8-byte signature is at least as large as hashval_t. */
8136 comdat_type_node *type_node = die->die_id.die_type_node;
8137 memcpy (&h, type_node->signature, sizeof (h));
8139 return h;
8142 inline bool
8143 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8145 return r1->type == r2->type;
8148 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8150 /* Return a pointer to the external_ref for references to DIE. */
8152 static struct external_ref *
8153 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8155 struct external_ref ref, *ref_p;
8156 external_ref **slot;
8158 ref.type = die;
8159 slot = map->find_slot (&ref, INSERT);
8160 if (*slot != HTAB_EMPTY_ENTRY)
8161 return *slot;
8163 ref_p = XCNEW (struct external_ref);
8164 ref_p->type = die;
8165 *slot = ref_p;
8166 return ref_p;
8169 /* Subroutine of optimize_external_refs, below.
8171 If we see a type skeleton, record it as our stub. If we see external
8172 references, remember how many we've seen. */
8174 static void
8175 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8177 dw_die_ref c;
8178 dw_attr_node *a;
8179 unsigned ix;
8180 struct external_ref *ref_p;
8182 if (is_type_die (die)
8183 && (c = get_AT_ref (die, DW_AT_signature)))
8185 /* This is a local skeleton; use it for local references. */
8186 ref_p = lookup_external_ref (map, c);
8187 ref_p->stub = die;
8190 /* Scan the DIE references, and remember any that refer to DIEs from
8191 other CUs (i.e. those which are not marked). */
8192 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8193 if (AT_class (a) == dw_val_class_die_ref
8194 && (c = AT_ref (a))->die_mark == 0
8195 && is_type_die (c))
8197 ref_p = lookup_external_ref (map, c);
8198 ref_p->n_refs++;
8201 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8204 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8205 points to an external_ref, DATA is the CU we're processing. If we don't
8206 already have a local stub, and we have multiple refs, build a stub. */
8209 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8211 struct external_ref *ref_p = *slot;
8213 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8215 /* We have multiple references to this type, so build a small stub.
8216 Both of these forms are a bit dodgy from the perspective of the
8217 DWARF standard, since technically they should have names. */
8218 dw_die_ref cu = data;
8219 dw_die_ref type = ref_p->type;
8220 dw_die_ref stub = NULL;
8222 if (type->comdat_type_p)
8224 /* If we refer to this type via sig8, use AT_signature. */
8225 stub = new_die (type->die_tag, cu, NULL_TREE);
8226 add_AT_die_ref (stub, DW_AT_signature, type);
8228 else
8230 /* Otherwise, use a typedef with no name. */
8231 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8232 add_AT_die_ref (stub, DW_AT_type, type);
8235 stub->die_mark++;
8236 ref_p->stub = stub;
8238 return 1;
8241 /* DIE is a unit; look through all the DIE references to see if there are
8242 any external references to types, and if so, create local stubs for
8243 them which will be applied in build_abbrev_table. This is useful because
8244 references to local DIEs are smaller. */
8246 static external_ref_hash_type *
8247 optimize_external_refs (dw_die_ref die)
8249 external_ref_hash_type *map = new external_ref_hash_type (10);
8250 optimize_external_refs_1 (die, map);
8251 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8252 return map;
8255 /* The format of each DIE (and its attribute value pairs) is encoded in an
8256 abbreviation table. This routine builds the abbreviation table and assigns
8257 a unique abbreviation id for each abbreviation entry. The children of each
8258 die are visited recursively. */
8260 static void
8261 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8263 unsigned long abbrev_id;
8264 unsigned int n_alloc;
8265 dw_die_ref c;
8266 dw_attr_node *a;
8267 unsigned ix;
8269 /* Scan the DIE references, and replace any that refer to
8270 DIEs from other CUs (i.e. those which are not marked) with
8271 the local stubs we built in optimize_external_refs. */
8272 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8273 if (AT_class (a) == dw_val_class_die_ref
8274 && (c = AT_ref (a))->die_mark == 0)
8276 struct external_ref *ref_p;
8277 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8279 ref_p = lookup_external_ref (extern_map, c);
8280 if (ref_p->stub && ref_p->stub != die)
8281 change_AT_die_ref (a, ref_p->stub);
8282 else
8283 /* We aren't changing this reference, so mark it external. */
8284 set_AT_ref_external (a, 1);
8287 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8289 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8290 dw_attr_node *die_a, *abbrev_a;
8291 unsigned ix;
8292 bool ok = true;
8294 if (abbrev->die_tag != die->die_tag)
8295 continue;
8296 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8297 continue;
8299 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8300 continue;
8302 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8304 abbrev_a = &(*abbrev->die_attr)[ix];
8305 if ((abbrev_a->dw_attr != die_a->dw_attr)
8306 || (value_format (abbrev_a) != value_format (die_a)))
8308 ok = false;
8309 break;
8312 if (ok)
8313 break;
8316 if (abbrev_id >= abbrev_die_table_in_use)
8318 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8320 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8321 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8322 n_alloc);
8324 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8325 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8326 abbrev_die_table_allocated = n_alloc;
8329 ++abbrev_die_table_in_use;
8330 abbrev_die_table[abbrev_id] = die;
8333 die->die_abbrev = abbrev_id;
8334 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8337 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8339 static int
8340 constant_size (unsigned HOST_WIDE_INT value)
8342 int log;
8344 if (value == 0)
8345 log = 0;
8346 else
8347 log = floor_log2 (value);
8349 log = log / 8;
8350 log = 1 << (floor_log2 (log) + 1);
8352 return log;
8355 /* Return the size of a DIE as it is represented in the
8356 .debug_info section. */
8358 static unsigned long
8359 size_of_die (dw_die_ref die)
8361 unsigned long size = 0;
8362 dw_attr_node *a;
8363 unsigned ix;
8364 enum dwarf_form form;
8366 size += size_of_uleb128 (die->die_abbrev);
8367 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8369 switch (AT_class (a))
8371 case dw_val_class_addr:
8372 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8374 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8375 size += size_of_uleb128 (AT_index (a));
8377 else
8378 size += DWARF2_ADDR_SIZE;
8379 break;
8380 case dw_val_class_offset:
8381 size += DWARF_OFFSET_SIZE;
8382 break;
8383 case dw_val_class_loc:
8385 unsigned long lsize = size_of_locs (AT_loc (a));
8387 /* Block length. */
8388 if (dwarf_version >= 4)
8389 size += size_of_uleb128 (lsize);
8390 else
8391 size += constant_size (lsize);
8392 size += lsize;
8394 break;
8395 case dw_val_class_loc_list:
8396 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8398 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8399 size += size_of_uleb128 (AT_index (a));
8401 else
8402 size += DWARF_OFFSET_SIZE;
8403 break;
8404 case dw_val_class_range_list:
8405 size += DWARF_OFFSET_SIZE;
8406 break;
8407 case dw_val_class_const:
8408 size += size_of_sleb128 (AT_int (a));
8409 break;
8410 case dw_val_class_unsigned_const:
8412 int csize = constant_size (AT_unsigned (a));
8413 if (dwarf_version == 3
8414 && a->dw_attr == DW_AT_data_member_location
8415 && csize >= 4)
8416 size += size_of_uleb128 (AT_unsigned (a));
8417 else
8418 size += csize;
8420 break;
8421 case dw_val_class_const_double:
8422 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8423 if (HOST_BITS_PER_WIDE_INT >= 64)
8424 size++; /* block */
8425 break;
8426 case dw_val_class_wide_int:
8427 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8428 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8429 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8430 > 64)
8431 size++; /* block */
8432 break;
8433 case dw_val_class_vec:
8434 size += constant_size (a->dw_attr_val.v.val_vec.length
8435 * a->dw_attr_val.v.val_vec.elt_size)
8436 + a->dw_attr_val.v.val_vec.length
8437 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8438 break;
8439 case dw_val_class_flag:
8440 if (dwarf_version >= 4)
8441 /* Currently all add_AT_flag calls pass in 1 as last argument,
8442 so DW_FORM_flag_present can be used. If that ever changes,
8443 we'll need to use DW_FORM_flag and have some optimization
8444 in build_abbrev_table that will change those to
8445 DW_FORM_flag_present if it is set to 1 in all DIEs using
8446 the same abbrev entry. */
8447 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8448 else
8449 size += 1;
8450 break;
8451 case dw_val_class_die_ref:
8452 if (AT_ref_external (a))
8454 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8455 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8456 is sized by target address length, whereas in DWARF3
8457 it's always sized as an offset. */
8458 if (use_debug_types)
8459 size += DWARF_TYPE_SIGNATURE_SIZE;
8460 else if (dwarf_version == 2)
8461 size += DWARF2_ADDR_SIZE;
8462 else
8463 size += DWARF_OFFSET_SIZE;
8465 else
8466 size += DWARF_OFFSET_SIZE;
8467 break;
8468 case dw_val_class_fde_ref:
8469 size += DWARF_OFFSET_SIZE;
8470 break;
8471 case dw_val_class_lbl_id:
8472 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8474 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8475 size += size_of_uleb128 (AT_index (a));
8477 else
8478 size += DWARF2_ADDR_SIZE;
8479 break;
8480 case dw_val_class_lineptr:
8481 case dw_val_class_macptr:
8482 size += DWARF_OFFSET_SIZE;
8483 break;
8484 case dw_val_class_str:
8485 form = AT_string_form (a);
8486 if (form == DW_FORM_strp)
8487 size += DWARF_OFFSET_SIZE;
8488 else if (form == DW_FORM_GNU_str_index)
8489 size += size_of_uleb128 (AT_index (a));
8490 else
8491 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8492 break;
8493 case dw_val_class_file:
8494 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8495 break;
8496 case dw_val_class_data8:
8497 size += 8;
8498 break;
8499 case dw_val_class_vms_delta:
8500 size += DWARF_OFFSET_SIZE;
8501 break;
8502 case dw_val_class_high_pc:
8503 size += DWARF2_ADDR_SIZE;
8504 break;
8505 case dw_val_class_discr_value:
8506 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8507 break;
8508 case dw_val_class_discr_list:
8510 unsigned block_size = size_of_discr_list (AT_discr_list (a));
8512 /* This is a block, so we have the block length and then its
8513 data. */
8514 size += constant_size (block_size) + block_size;
8516 break;
8517 default:
8518 gcc_unreachable ();
8522 return size;
8525 /* Size the debugging information associated with a given DIE. Visits the
8526 DIE's children recursively. Updates the global variable next_die_offset, on
8527 each time through. Uses the current value of next_die_offset to update the
8528 die_offset field in each DIE. */
8530 static void
8531 calc_die_sizes (dw_die_ref die)
8533 dw_die_ref c;
8535 gcc_assert (die->die_offset == 0
8536 || (unsigned long int) die->die_offset == next_die_offset);
8537 die->die_offset = next_die_offset;
8538 next_die_offset += size_of_die (die);
8540 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8542 if (die->die_child != NULL)
8543 /* Count the null byte used to terminate sibling lists. */
8544 next_die_offset += 1;
8547 /* Size just the base type children at the start of the CU.
8548 This is needed because build_abbrev needs to size locs
8549 and sizing of type based stack ops needs to know die_offset
8550 values for the base types. */
8552 static void
8553 calc_base_type_die_sizes (void)
8555 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8556 unsigned int i;
8557 dw_die_ref base_type;
8558 #if ENABLE_ASSERT_CHECKING
8559 dw_die_ref prev = comp_unit_die ()->die_child;
8560 #endif
8562 die_offset += size_of_die (comp_unit_die ());
8563 for (i = 0; base_types.iterate (i, &base_type); i++)
8565 #if ENABLE_ASSERT_CHECKING
8566 gcc_assert (base_type->die_offset == 0
8567 && prev->die_sib == base_type
8568 && base_type->die_child == NULL
8569 && base_type->die_abbrev);
8570 prev = base_type;
8571 #endif
8572 base_type->die_offset = die_offset;
8573 die_offset += size_of_die (base_type);
8577 /* Set the marks for a die and its children. We do this so
8578 that we know whether or not a reference needs to use FORM_ref_addr; only
8579 DIEs in the same CU will be marked. We used to clear out the offset
8580 and use that as the flag, but ran into ordering problems. */
8582 static void
8583 mark_dies (dw_die_ref die)
8585 dw_die_ref c;
8587 gcc_assert (!die->die_mark);
8589 die->die_mark = 1;
8590 FOR_EACH_CHILD (die, c, mark_dies (c));
8593 /* Clear the marks for a die and its children. */
8595 static void
8596 unmark_dies (dw_die_ref die)
8598 dw_die_ref c;
8600 if (! use_debug_types)
8601 gcc_assert (die->die_mark);
8603 die->die_mark = 0;
8604 FOR_EACH_CHILD (die, c, unmark_dies (c));
8607 /* Clear the marks for a die, its children and referred dies. */
8609 static void
8610 unmark_all_dies (dw_die_ref die)
8612 dw_die_ref c;
8613 dw_attr_node *a;
8614 unsigned ix;
8616 if (!die->die_mark)
8617 return;
8618 die->die_mark = 0;
8620 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8622 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8623 if (AT_class (a) == dw_val_class_die_ref)
8624 unmark_all_dies (AT_ref (a));
8627 /* Calculate if the entry should appear in the final output file. It may be
8628 from a pruned a type. */
8630 static bool
8631 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8633 /* By limiting gnu pubnames to definitions only, gold can generate a
8634 gdb index without entries for declarations, which don't include
8635 enough information to be useful. */
8636 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8637 return false;
8639 if (table == pubname_table)
8641 /* Enumerator names are part of the pubname table, but the
8642 parent DW_TAG_enumeration_type die may have been pruned.
8643 Don't output them if that is the case. */
8644 if (p->die->die_tag == DW_TAG_enumerator &&
8645 (p->die->die_parent == NULL
8646 || !p->die->die_parent->die_perennial_p))
8647 return false;
8649 /* Everything else in the pubname table is included. */
8650 return true;
8653 /* The pubtypes table shouldn't include types that have been
8654 pruned. */
8655 return (p->die->die_offset != 0
8656 || !flag_eliminate_unused_debug_types);
8659 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8660 generated for the compilation unit. */
8662 static unsigned long
8663 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8665 unsigned long size;
8666 unsigned i;
8667 pubname_entry *p;
8668 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8670 size = DWARF_PUBNAMES_HEADER_SIZE;
8671 FOR_EACH_VEC_ELT (*names, i, p)
8672 if (include_pubname_in_output (names, p))
8673 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8675 size += DWARF_OFFSET_SIZE;
8676 return size;
8679 /* Return the size of the information in the .debug_aranges section. */
8681 static unsigned long
8682 size_of_aranges (void)
8684 unsigned long size;
8686 size = DWARF_ARANGES_HEADER_SIZE;
8688 /* Count the address/length pair for this compilation unit. */
8689 if (text_section_used)
8690 size += 2 * DWARF2_ADDR_SIZE;
8691 if (cold_text_section_used)
8692 size += 2 * DWARF2_ADDR_SIZE;
8693 if (have_multiple_function_sections)
8695 unsigned fde_idx;
8696 dw_fde_ref fde;
8698 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8700 if (DECL_IGNORED_P (fde->decl))
8701 continue;
8702 if (!fde->in_std_section)
8703 size += 2 * DWARF2_ADDR_SIZE;
8704 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8705 size += 2 * DWARF2_ADDR_SIZE;
8709 /* Count the two zero words used to terminated the address range table. */
8710 size += 2 * DWARF2_ADDR_SIZE;
8711 return size;
8714 /* Select the encoding of an attribute value. */
8716 static enum dwarf_form
8717 value_format (dw_attr_node *a)
8719 switch (AT_class (a))
8721 case dw_val_class_addr:
8722 /* Only very few attributes allow DW_FORM_addr. */
8723 switch (a->dw_attr)
8725 case DW_AT_low_pc:
8726 case DW_AT_high_pc:
8727 case DW_AT_entry_pc:
8728 case DW_AT_trampoline:
8729 return (AT_index (a) == NOT_INDEXED
8730 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8731 default:
8732 break;
8734 switch (DWARF2_ADDR_SIZE)
8736 case 1:
8737 return DW_FORM_data1;
8738 case 2:
8739 return DW_FORM_data2;
8740 case 4:
8741 return DW_FORM_data4;
8742 case 8:
8743 return DW_FORM_data8;
8744 default:
8745 gcc_unreachable ();
8747 case dw_val_class_range_list:
8748 case dw_val_class_loc_list:
8749 if (dwarf_version >= 4)
8750 return DW_FORM_sec_offset;
8751 /* FALLTHRU */
8752 case dw_val_class_vms_delta:
8753 case dw_val_class_offset:
8754 switch (DWARF_OFFSET_SIZE)
8756 case 4:
8757 return DW_FORM_data4;
8758 case 8:
8759 return DW_FORM_data8;
8760 default:
8761 gcc_unreachable ();
8763 case dw_val_class_loc:
8764 if (dwarf_version >= 4)
8765 return DW_FORM_exprloc;
8766 switch (constant_size (size_of_locs (AT_loc (a))))
8768 case 1:
8769 return DW_FORM_block1;
8770 case 2:
8771 return DW_FORM_block2;
8772 case 4:
8773 return DW_FORM_block4;
8774 default:
8775 gcc_unreachable ();
8777 case dw_val_class_const:
8778 return DW_FORM_sdata;
8779 case dw_val_class_unsigned_const:
8780 switch (constant_size (AT_unsigned (a)))
8782 case 1:
8783 return DW_FORM_data1;
8784 case 2:
8785 return DW_FORM_data2;
8786 case 4:
8787 /* In DWARF3 DW_AT_data_member_location with
8788 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8789 constant, so we need to use DW_FORM_udata if we need
8790 a large constant. */
8791 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8792 return DW_FORM_udata;
8793 return DW_FORM_data4;
8794 case 8:
8795 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8796 return DW_FORM_udata;
8797 return DW_FORM_data8;
8798 default:
8799 gcc_unreachable ();
8801 case dw_val_class_const_double:
8802 switch (HOST_BITS_PER_WIDE_INT)
8804 case 8:
8805 return DW_FORM_data2;
8806 case 16:
8807 return DW_FORM_data4;
8808 case 32:
8809 return DW_FORM_data8;
8810 case 64:
8811 default:
8812 return DW_FORM_block1;
8814 case dw_val_class_wide_int:
8815 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8817 case 8:
8818 return DW_FORM_data1;
8819 case 16:
8820 return DW_FORM_data2;
8821 case 32:
8822 return DW_FORM_data4;
8823 case 64:
8824 return DW_FORM_data8;
8825 default:
8826 return DW_FORM_block1;
8828 case dw_val_class_vec:
8829 switch (constant_size (a->dw_attr_val.v.val_vec.length
8830 * a->dw_attr_val.v.val_vec.elt_size))
8832 case 1:
8833 return DW_FORM_block1;
8834 case 2:
8835 return DW_FORM_block2;
8836 case 4:
8837 return DW_FORM_block4;
8838 default:
8839 gcc_unreachable ();
8841 case dw_val_class_flag:
8842 if (dwarf_version >= 4)
8844 /* Currently all add_AT_flag calls pass in 1 as last argument,
8845 so DW_FORM_flag_present can be used. If that ever changes,
8846 we'll need to use DW_FORM_flag and have some optimization
8847 in build_abbrev_table that will change those to
8848 DW_FORM_flag_present if it is set to 1 in all DIEs using
8849 the same abbrev entry. */
8850 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8851 return DW_FORM_flag_present;
8853 return DW_FORM_flag;
8854 case dw_val_class_die_ref:
8855 if (AT_ref_external (a))
8856 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8857 else
8858 return DW_FORM_ref;
8859 case dw_val_class_fde_ref:
8860 return DW_FORM_data;
8861 case dw_val_class_lbl_id:
8862 return (AT_index (a) == NOT_INDEXED
8863 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8864 case dw_val_class_lineptr:
8865 case dw_val_class_macptr:
8866 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8867 case dw_val_class_str:
8868 return AT_string_form (a);
8869 case dw_val_class_file:
8870 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8872 case 1:
8873 return DW_FORM_data1;
8874 case 2:
8875 return DW_FORM_data2;
8876 case 4:
8877 return DW_FORM_data4;
8878 default:
8879 gcc_unreachable ();
8882 case dw_val_class_data8:
8883 return DW_FORM_data8;
8885 case dw_val_class_high_pc:
8886 switch (DWARF2_ADDR_SIZE)
8888 case 1:
8889 return DW_FORM_data1;
8890 case 2:
8891 return DW_FORM_data2;
8892 case 4:
8893 return DW_FORM_data4;
8894 case 8:
8895 return DW_FORM_data8;
8896 default:
8897 gcc_unreachable ();
8900 case dw_val_class_discr_value:
8901 return (a->dw_attr_val.v.val_discr_value.pos
8902 ? DW_FORM_udata
8903 : DW_FORM_sdata);
8904 case dw_val_class_discr_list:
8905 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8907 case 1:
8908 return DW_FORM_block1;
8909 case 2:
8910 return DW_FORM_block2;
8911 case 4:
8912 return DW_FORM_block4;
8913 default:
8914 gcc_unreachable ();
8917 default:
8918 gcc_unreachable ();
8922 /* Output the encoding of an attribute value. */
8924 static void
8925 output_value_format (dw_attr_node *a)
8927 enum dwarf_form form = value_format (a);
8929 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8932 /* Given a die and id, produce the appropriate abbreviations. */
8934 static void
8935 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8937 unsigned ix;
8938 dw_attr_node *a_attr;
8940 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8941 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8942 dwarf_tag_name (abbrev->die_tag));
8944 if (abbrev->die_child != NULL)
8945 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8946 else
8947 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8949 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8951 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8952 dwarf_attr_name (a_attr->dw_attr));
8953 output_value_format (a_attr);
8956 dw2_asm_output_data (1, 0, NULL);
8957 dw2_asm_output_data (1, 0, NULL);
8961 /* Output the .debug_abbrev section which defines the DIE abbreviation
8962 table. */
8964 static void
8965 output_abbrev_section (void)
8967 unsigned long abbrev_id;
8969 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8970 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8972 /* Terminate the table. */
8973 dw2_asm_output_data (1, 0, NULL);
8976 /* Output a symbol we can use to refer to this DIE from another CU. */
8978 static inline void
8979 output_die_symbol (dw_die_ref die)
8981 const char *sym = die->die_id.die_symbol;
8983 gcc_assert (!die->comdat_type_p);
8985 if (sym == 0)
8986 return;
8988 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8989 /* We make these global, not weak; if the target doesn't support
8990 .linkonce, it doesn't support combining the sections, so debugging
8991 will break. */
8992 targetm.asm_out.globalize_label (asm_out_file, sym);
8994 ASM_OUTPUT_LABEL (asm_out_file, sym);
8997 /* Return a new location list, given the begin and end range, and the
8998 expression. */
9000 static inline dw_loc_list_ref
9001 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9002 const char *section)
9004 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9006 retlist->begin = begin;
9007 retlist->begin_entry = NULL;
9008 retlist->end = end;
9009 retlist->expr = expr;
9010 retlist->section = section;
9012 return retlist;
9015 /* Generate a new internal symbol for this location list node, if it
9016 hasn't got one yet. */
9018 static inline void
9019 gen_llsym (dw_loc_list_ref list)
9021 gcc_assert (!list->ll_symbol);
9022 list->ll_symbol = gen_internal_sym ("LLST");
9025 /* Output the location list given to us. */
9027 static void
9028 output_loc_list (dw_loc_list_ref list_head)
9030 dw_loc_list_ref curr = list_head;
9032 if (list_head->emitted)
9033 return;
9034 list_head->emitted = true;
9036 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9038 /* Walk the location list, and output each range + expression. */
9039 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9041 unsigned long size;
9042 /* Don't output an entry that starts and ends at the same address. */
9043 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9044 continue;
9045 size = size_of_locs (curr->expr);
9046 /* If the expression is too large, drop it on the floor. We could
9047 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9048 in the expression, but >= 64KB expressions for a single value
9049 in a single range are unlikely very useful. */
9050 if (size > 0xffff)
9051 continue;
9052 if (dwarf_split_debug_info)
9054 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9055 "Location list start/length entry (%s)",
9056 list_head->ll_symbol);
9057 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9058 "Location list range start index (%s)",
9059 curr->begin);
9060 /* The length field is 4 bytes. If we ever need to support
9061 an 8-byte length, we can add a new DW_LLE code or fall back
9062 to DW_LLE_GNU_start_end_entry. */
9063 dw2_asm_output_delta (4, curr->end, curr->begin,
9064 "Location list range length (%s)",
9065 list_head->ll_symbol);
9067 else if (!have_multiple_function_sections)
9069 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9070 "Location list begin address (%s)",
9071 list_head->ll_symbol);
9072 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9073 "Location list end address (%s)",
9074 list_head->ll_symbol);
9076 else
9078 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9079 "Location list begin address (%s)",
9080 list_head->ll_symbol);
9081 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9082 "Location list end address (%s)",
9083 list_head->ll_symbol);
9086 /* Output the block length for this list of location operations. */
9087 gcc_assert (size <= 0xffff);
9088 dw2_asm_output_data (2, size, "%s", "Location expression size");
9090 output_loc_sequence (curr->expr, -1);
9093 if (dwarf_split_debug_info)
9094 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9095 "Location list terminator (%s)",
9096 list_head->ll_symbol);
9097 else
9099 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9100 "Location list terminator begin (%s)",
9101 list_head->ll_symbol);
9102 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9103 "Location list terminator end (%s)",
9104 list_head->ll_symbol);
9108 /* Output a range_list offset into the debug_range section. Emit a
9109 relocated reference if val_entry is NULL, otherwise, emit an
9110 indirect reference. */
9112 static void
9113 output_range_list_offset (dw_attr_node *a)
9115 const char *name = dwarf_attr_name (a->dw_attr);
9117 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9119 char *p = strchr (ranges_section_label, '\0');
9120 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9121 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9122 debug_ranges_section, "%s", name);
9123 *p = '\0';
9125 else
9126 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9127 "%s (offset from %s)", name, ranges_section_label);
9130 /* Output the offset into the debug_loc section. */
9132 static void
9133 output_loc_list_offset (dw_attr_node *a)
9135 char *sym = AT_loc_list (a)->ll_symbol;
9137 gcc_assert (sym);
9138 if (dwarf_split_debug_info)
9139 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9140 "%s", dwarf_attr_name (a->dw_attr));
9141 else
9142 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9143 "%s", dwarf_attr_name (a->dw_attr));
9146 /* Output an attribute's index or value appropriately. */
9148 static void
9149 output_attr_index_or_value (dw_attr_node *a)
9151 const char *name = dwarf_attr_name (a->dw_attr);
9153 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9155 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9156 return;
9158 switch (AT_class (a))
9160 case dw_val_class_addr:
9161 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9162 break;
9163 case dw_val_class_high_pc:
9164 case dw_val_class_lbl_id:
9165 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9166 break;
9167 case dw_val_class_loc_list:
9168 output_loc_list_offset (a);
9169 break;
9170 default:
9171 gcc_unreachable ();
9175 /* Output a type signature. */
9177 static inline void
9178 output_signature (const char *sig, const char *name)
9180 int i;
9182 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9183 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9186 /* Output a discriminant value. */
9188 static inline void
9189 output_discr_value (dw_discr_value *discr_value, const char *name)
9191 if (discr_value->pos)
9192 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9193 else
9194 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9197 /* Output the DIE and its attributes. Called recursively to generate
9198 the definitions of each child DIE. */
9200 static void
9201 output_die (dw_die_ref die)
9203 dw_attr_node *a;
9204 dw_die_ref c;
9205 unsigned long size;
9206 unsigned ix;
9208 /* If someone in another CU might refer to us, set up a symbol for
9209 them to point to. */
9210 if (! die->comdat_type_p && die->die_id.die_symbol)
9211 output_die_symbol (die);
9213 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9214 (unsigned long)die->die_offset,
9215 dwarf_tag_name (die->die_tag));
9217 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9219 const char *name = dwarf_attr_name (a->dw_attr);
9221 switch (AT_class (a))
9223 case dw_val_class_addr:
9224 output_attr_index_or_value (a);
9225 break;
9227 case dw_val_class_offset:
9228 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9229 "%s", name);
9230 break;
9232 case dw_val_class_range_list:
9233 output_range_list_offset (a);
9234 break;
9236 case dw_val_class_loc:
9237 size = size_of_locs (AT_loc (a));
9239 /* Output the block length for this list of location operations. */
9240 if (dwarf_version >= 4)
9241 dw2_asm_output_data_uleb128 (size, "%s", name);
9242 else
9243 dw2_asm_output_data (constant_size (size), size, "%s", name);
9245 output_loc_sequence (AT_loc (a), -1);
9246 break;
9248 case dw_val_class_const:
9249 /* ??? It would be slightly more efficient to use a scheme like is
9250 used for unsigned constants below, but gdb 4.x does not sign
9251 extend. Gdb 5.x does sign extend. */
9252 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9253 break;
9255 case dw_val_class_unsigned_const:
9257 int csize = constant_size (AT_unsigned (a));
9258 if (dwarf_version == 3
9259 && a->dw_attr == DW_AT_data_member_location
9260 && csize >= 4)
9261 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9262 else
9263 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9265 break;
9267 case dw_val_class_const_double:
9269 unsigned HOST_WIDE_INT first, second;
9271 if (HOST_BITS_PER_WIDE_INT >= 64)
9272 dw2_asm_output_data (1,
9273 HOST_BITS_PER_DOUBLE_INT
9274 / HOST_BITS_PER_CHAR,
9275 NULL);
9277 if (WORDS_BIG_ENDIAN)
9279 first = a->dw_attr_val.v.val_double.high;
9280 second = a->dw_attr_val.v.val_double.low;
9282 else
9284 first = a->dw_attr_val.v.val_double.low;
9285 second = a->dw_attr_val.v.val_double.high;
9288 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9289 first, "%s", name);
9290 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9291 second, NULL);
9293 break;
9295 case dw_val_class_wide_int:
9297 int i;
9298 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9299 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9300 if (len * HOST_BITS_PER_WIDE_INT > 64)
9301 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9302 NULL);
9304 if (WORDS_BIG_ENDIAN)
9305 for (i = len - 1; i >= 0; --i)
9307 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9308 "%s", name);
9309 name = "";
9311 else
9312 for (i = 0; i < len; ++i)
9314 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9315 "%s", name);
9316 name = "";
9319 break;
9321 case dw_val_class_vec:
9323 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9324 unsigned int len = a->dw_attr_val.v.val_vec.length;
9325 unsigned int i;
9326 unsigned char *p;
9328 dw2_asm_output_data (constant_size (len * elt_size),
9329 len * elt_size, "%s", name);
9330 if (elt_size > sizeof (HOST_WIDE_INT))
9332 elt_size /= 2;
9333 len *= 2;
9335 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9336 i < len;
9337 i++, p += elt_size)
9338 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9339 "fp or vector constant word %u", i);
9340 break;
9343 case dw_val_class_flag:
9344 if (dwarf_version >= 4)
9346 /* Currently all add_AT_flag calls pass in 1 as last argument,
9347 so DW_FORM_flag_present can be used. If that ever changes,
9348 we'll need to use DW_FORM_flag and have some optimization
9349 in build_abbrev_table that will change those to
9350 DW_FORM_flag_present if it is set to 1 in all DIEs using
9351 the same abbrev entry. */
9352 gcc_assert (AT_flag (a) == 1);
9353 if (flag_debug_asm)
9354 fprintf (asm_out_file, "\t\t\t%s %s\n",
9355 ASM_COMMENT_START, name);
9356 break;
9358 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9359 break;
9361 case dw_val_class_loc_list:
9362 output_attr_index_or_value (a);
9363 break;
9365 case dw_val_class_die_ref:
9366 if (AT_ref_external (a))
9368 if (AT_ref (a)->comdat_type_p)
9370 comdat_type_node *type_node =
9371 AT_ref (a)->die_id.die_type_node;
9373 gcc_assert (type_node);
9374 output_signature (type_node->signature, name);
9376 else
9378 const char *sym = AT_ref (a)->die_id.die_symbol;
9379 int size;
9381 gcc_assert (sym);
9382 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9383 length, whereas in DWARF3 it's always sized as an
9384 offset. */
9385 if (dwarf_version == 2)
9386 size = DWARF2_ADDR_SIZE;
9387 else
9388 size = DWARF_OFFSET_SIZE;
9389 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9390 name);
9393 else
9395 gcc_assert (AT_ref (a)->die_offset);
9396 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9397 "%s", name);
9399 break;
9401 case dw_val_class_fde_ref:
9403 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
9405 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9406 a->dw_attr_val.v.val_fde_index * 2);
9407 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9408 "%s", name);
9410 break;
9412 case dw_val_class_vms_delta:
9413 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9414 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9415 AT_vms_delta2 (a), AT_vms_delta1 (a),
9416 "%s", name);
9417 #else
9418 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9419 AT_vms_delta2 (a), AT_vms_delta1 (a),
9420 "%s", name);
9421 #endif
9422 break;
9424 case dw_val_class_lbl_id:
9425 output_attr_index_or_value (a);
9426 break;
9428 case dw_val_class_lineptr:
9429 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9430 debug_line_section, "%s", name);
9431 break;
9433 case dw_val_class_macptr:
9434 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9435 debug_macinfo_section, "%s", name);
9436 break;
9438 case dw_val_class_str:
9439 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9440 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9441 a->dw_attr_val.v.val_str->label,
9442 debug_str_section,
9443 "%s: \"%s\"", name, AT_string (a));
9444 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9445 dw2_asm_output_data_uleb128 (AT_index (a),
9446 "%s: \"%s\"", name, AT_string (a));
9447 else
9448 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9449 break;
9451 case dw_val_class_file:
9453 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9455 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9456 a->dw_attr_val.v.val_file->filename);
9457 break;
9460 case dw_val_class_data8:
9462 int i;
9464 for (i = 0; i < 8; i++)
9465 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9466 i == 0 ? "%s" : NULL, name);
9467 break;
9470 case dw_val_class_high_pc:
9471 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9472 get_AT_low_pc (die), "DW_AT_high_pc");
9473 break;
9475 case dw_val_class_discr_value:
9476 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9477 break;
9479 case dw_val_class_discr_list:
9481 dw_discr_list_ref list = AT_discr_list (a);
9482 const int size = size_of_discr_list (list);
9484 /* This is a block, so output its length first. */
9485 dw2_asm_output_data (constant_size (size), size,
9486 "%s: block size", name);
9488 for (; list != NULL; list = list->dw_discr_next)
9490 /* One byte for the discriminant value descriptor, and then as
9491 many LEB128 numbers as required. */
9492 if (list->dw_discr_range)
9493 dw2_asm_output_data (1, DW_DSC_range,
9494 "%s: DW_DSC_range", name);
9495 else
9496 dw2_asm_output_data (1, DW_DSC_label,
9497 "%s: DW_DSC_label", name);
9499 output_discr_value (&list->dw_discr_lower_bound, name);
9500 if (list->dw_discr_range)
9501 output_discr_value (&list->dw_discr_upper_bound, name);
9503 break;
9506 default:
9507 gcc_unreachable ();
9511 FOR_EACH_CHILD (die, c, output_die (c));
9513 /* Add null byte to terminate sibling list. */
9514 if (die->die_child != NULL)
9515 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9516 (unsigned long) die->die_offset);
9519 /* Output the compilation unit that appears at the beginning of the
9520 .debug_info section, and precedes the DIE descriptions. */
9522 static void
9523 output_compilation_unit_header (void)
9525 /* We don't support actual DWARFv5 units yet, we just use some
9526 DWARFv5 draft DIE tags in DWARFv4 format. */
9527 int ver = dwarf_version < 5 ? dwarf_version : 4;
9529 if (!XCOFF_DEBUGGING_INFO)
9531 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9532 dw2_asm_output_data (4, 0xffffffff,
9533 "Initial length escape value indicating 64-bit DWARF extension");
9534 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9535 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9536 "Length of Compilation Unit Info");
9539 dw2_asm_output_data (2, ver, "DWARF version number");
9540 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9541 debug_abbrev_section,
9542 "Offset Into Abbrev. Section");
9543 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9546 /* Output the compilation unit DIE and its children. */
9548 static void
9549 output_comp_unit (dw_die_ref die, int output_if_empty)
9551 const char *secname, *oldsym;
9552 char *tmp;
9554 /* Unless we are outputting main CU, we may throw away empty ones. */
9555 if (!output_if_empty && die->die_child == NULL)
9556 return;
9558 /* Even if there are no children of this DIE, we must output the information
9559 about the compilation unit. Otherwise, on an empty translation unit, we
9560 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9561 will then complain when examining the file. First mark all the DIEs in
9562 this CU so we know which get local refs. */
9563 mark_dies (die);
9565 external_ref_hash_type *extern_map = optimize_external_refs (die);
9567 build_abbrev_table (die, extern_map);
9569 delete extern_map;
9571 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9572 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9573 calc_die_sizes (die);
9575 oldsym = die->die_id.die_symbol;
9576 if (oldsym)
9578 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9580 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9581 secname = tmp;
9582 die->die_id.die_symbol = NULL;
9583 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9585 else
9587 switch_to_section (debug_info_section);
9588 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9589 info_section_emitted = true;
9592 /* Output debugging information. */
9593 output_compilation_unit_header ();
9594 output_die (die);
9596 /* Leave the marks on the main CU, so we can check them in
9597 output_pubnames. */
9598 if (oldsym)
9600 unmark_dies (die);
9601 die->die_id.die_symbol = oldsym;
9605 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9606 and .debug_pubtypes. This is configured per-target, but can be
9607 overridden by the -gpubnames or -gno-pubnames options. */
9609 static inline bool
9610 want_pubnames (void)
9612 if (debug_info_level <= DINFO_LEVEL_TERSE)
9613 return false;
9614 if (debug_generate_pub_sections != -1)
9615 return debug_generate_pub_sections;
9616 return targetm.want_debug_pub_sections;
9619 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9621 static void
9622 add_AT_pubnames (dw_die_ref die)
9624 if (want_pubnames ())
9625 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9628 /* Add a string attribute value to a skeleton DIE. */
9630 static inline void
9631 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9632 const char *str)
9634 dw_attr_node attr;
9635 struct indirect_string_node *node;
9637 if (! skeleton_debug_str_hash)
9638 skeleton_debug_str_hash
9639 = hash_table<indirect_string_hasher>::create_ggc (10);
9641 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9642 find_string_form (node);
9643 if (node->form == DW_FORM_GNU_str_index)
9644 node->form = DW_FORM_strp;
9646 attr.dw_attr = attr_kind;
9647 attr.dw_attr_val.val_class = dw_val_class_str;
9648 attr.dw_attr_val.val_entry = NULL;
9649 attr.dw_attr_val.v.val_str = node;
9650 add_dwarf_attr (die, &attr);
9653 /* Helper function to generate top-level dies for skeleton debug_info and
9654 debug_types. */
9656 static void
9657 add_top_level_skeleton_die_attrs (dw_die_ref die)
9659 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9660 const char *comp_dir = comp_dir_string ();
9662 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9663 if (comp_dir != NULL)
9664 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9665 add_AT_pubnames (die);
9666 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9669 /* Output skeleton debug sections that point to the dwo file. */
9671 static void
9672 output_skeleton_debug_sections (dw_die_ref comp_unit)
9674 /* We don't support actual DWARFv5 units yet, we just use some
9675 DWARFv5 draft DIE tags in DWARFv4 format. */
9676 int ver = dwarf_version < 5 ? dwarf_version : 4;
9678 /* These attributes will be found in the full debug_info section. */
9679 remove_AT (comp_unit, DW_AT_producer);
9680 remove_AT (comp_unit, DW_AT_language);
9682 switch_to_section (debug_skeleton_info_section);
9683 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9685 /* Produce the skeleton compilation-unit header. This one differs enough from
9686 a normal CU header that it's better not to call output_compilation_unit
9687 header. */
9688 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9689 dw2_asm_output_data (4, 0xffffffff,
9690 "Initial length escape value indicating 64-bit DWARF extension");
9692 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9693 DWARF_COMPILE_UNIT_HEADER_SIZE
9694 - DWARF_INITIAL_LENGTH_SIZE
9695 + size_of_die (comp_unit),
9696 "Length of Compilation Unit Info");
9697 dw2_asm_output_data (2, ver, "DWARF version number");
9698 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9699 debug_abbrev_section,
9700 "Offset Into Abbrev. Section");
9701 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9703 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9704 output_die (comp_unit);
9706 /* Build the skeleton debug_abbrev section. */
9707 switch_to_section (debug_skeleton_abbrev_section);
9708 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9710 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9712 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9715 /* Output a comdat type unit DIE and its children. */
9717 static void
9718 output_comdat_type_unit (comdat_type_node *node)
9720 const char *secname;
9721 char *tmp;
9722 int i;
9723 #if defined (OBJECT_FORMAT_ELF)
9724 tree comdat_key;
9725 #endif
9727 /* First mark all the DIEs in this CU so we know which get local refs. */
9728 mark_dies (node->root_die);
9730 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9732 build_abbrev_table (node->root_die, extern_map);
9734 delete extern_map;
9735 extern_map = NULL;
9737 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9738 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9739 calc_die_sizes (node->root_die);
9741 #if defined (OBJECT_FORMAT_ELF)
9742 if (!dwarf_split_debug_info)
9743 secname = ".debug_types";
9744 else
9745 secname = ".debug_types.dwo";
9747 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9748 sprintf (tmp, "wt.");
9749 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9750 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9751 comdat_key = get_identifier (tmp);
9752 targetm.asm_out.named_section (secname,
9753 SECTION_DEBUG | SECTION_LINKONCE,
9754 comdat_key);
9755 #else
9756 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9757 sprintf (tmp, ".gnu.linkonce.wt.");
9758 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9759 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9760 secname = tmp;
9761 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9762 #endif
9764 /* Output debugging information. */
9765 output_compilation_unit_header ();
9766 output_signature (node->signature, "Type Signature");
9767 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9768 "Offset to Type DIE");
9769 output_die (node->root_die);
9771 unmark_dies (node->root_die);
9774 /* Return the DWARF2/3 pubname associated with a decl. */
9776 static const char *
9777 dwarf2_name (tree decl, int scope)
9779 if (DECL_NAMELESS (decl))
9780 return NULL;
9781 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9784 /* Add a new entry to .debug_pubnames if appropriate. */
9786 static void
9787 add_pubname_string (const char *str, dw_die_ref die)
9789 pubname_entry e;
9791 e.die = die;
9792 e.name = xstrdup (str);
9793 vec_safe_push (pubname_table, e);
9796 static void
9797 add_pubname (tree decl, dw_die_ref die)
9799 if (!want_pubnames ())
9800 return;
9802 /* Don't add items to the table when we expect that the consumer will have
9803 just read the enclosing die. For example, if the consumer is looking at a
9804 class_member, it will either be inside the class already, or will have just
9805 looked up the class to find the member. Either way, searching the class is
9806 faster than searching the index. */
9807 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9808 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9810 const char *name = dwarf2_name (decl, 1);
9812 if (name)
9813 add_pubname_string (name, die);
9817 /* Add an enumerator to the pubnames section. */
9819 static void
9820 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9822 pubname_entry e;
9824 gcc_assert (scope_name);
9825 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9826 e.die = die;
9827 vec_safe_push (pubname_table, e);
9830 /* Add a new entry to .debug_pubtypes if appropriate. */
9832 static void
9833 add_pubtype (tree decl, dw_die_ref die)
9835 pubname_entry e;
9837 if (!want_pubnames ())
9838 return;
9840 if ((TREE_PUBLIC (decl)
9841 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9842 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9844 tree scope = NULL;
9845 const char *scope_name = "";
9846 const char *sep = is_cxx () ? "::" : ".";
9847 const char *name;
9849 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9850 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9852 scope_name = lang_hooks.dwarf_name (scope, 1);
9853 if (scope_name != NULL && scope_name[0] != '\0')
9854 scope_name = concat (scope_name, sep, NULL);
9855 else
9856 scope_name = "";
9859 if (TYPE_P (decl))
9860 name = type_tag (decl);
9861 else
9862 name = lang_hooks.dwarf_name (decl, 1);
9864 /* If we don't have a name for the type, there's no point in adding
9865 it to the table. */
9866 if (name != NULL && name[0] != '\0')
9868 e.die = die;
9869 e.name = concat (scope_name, name, NULL);
9870 vec_safe_push (pubtype_table, e);
9873 /* Although it might be more consistent to add the pubinfo for the
9874 enumerators as their dies are created, they should only be added if the
9875 enum type meets the criteria above. So rather than re-check the parent
9876 enum type whenever an enumerator die is created, just output them all
9877 here. This isn't protected by the name conditional because anonymous
9878 enums don't have names. */
9879 if (die->die_tag == DW_TAG_enumeration_type)
9881 dw_die_ref c;
9883 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9888 /* Output a single entry in the pubnames table. */
9890 static void
9891 output_pubname (dw_offset die_offset, pubname_entry *entry)
9893 dw_die_ref die = entry->die;
9894 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9896 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9898 if (debug_generate_pub_sections == 2)
9900 /* This logic follows gdb's method for determining the value of the flag
9901 byte. */
9902 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9903 switch (die->die_tag)
9905 case DW_TAG_typedef:
9906 case DW_TAG_base_type:
9907 case DW_TAG_subrange_type:
9908 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9909 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9910 break;
9911 case DW_TAG_enumerator:
9912 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9913 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9914 if (!is_cxx () && !is_java ())
9915 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9916 break;
9917 case DW_TAG_subprogram:
9918 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9919 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9920 if (!is_ada ())
9921 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9922 break;
9923 case DW_TAG_constant:
9924 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9925 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9926 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9927 break;
9928 case DW_TAG_variable:
9929 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9930 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9931 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9932 break;
9933 case DW_TAG_namespace:
9934 case DW_TAG_imported_declaration:
9935 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9936 break;
9937 case DW_TAG_class_type:
9938 case DW_TAG_interface_type:
9939 case DW_TAG_structure_type:
9940 case DW_TAG_union_type:
9941 case DW_TAG_enumeration_type:
9942 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9943 if (!is_cxx () && !is_java ())
9944 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9945 break;
9946 default:
9947 /* An unusual tag. Leave the flag-byte empty. */
9948 break;
9950 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9951 "GDB-index flags");
9954 dw2_asm_output_nstring (entry->name, -1, "external name");
9958 /* Output the public names table used to speed up access to externally
9959 visible names; or the public types table used to find type definitions. */
9961 static void
9962 output_pubnames (vec<pubname_entry, va_gc> *names)
9964 unsigned i;
9965 unsigned long pubnames_length = size_of_pubnames (names);
9966 pubname_entry *pub;
9968 if (!XCOFF_DEBUGGING_INFO)
9970 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9971 dw2_asm_output_data (4, 0xffffffff,
9972 "Initial length escape value indicating 64-bit DWARF extension");
9973 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9974 "Pub Info Length");
9977 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9978 dw2_asm_output_data (2, 2, "DWARF Version");
9980 if (dwarf_split_debug_info)
9981 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9982 debug_skeleton_info_section,
9983 "Offset of Compilation Unit Info");
9984 else
9985 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9986 debug_info_section,
9987 "Offset of Compilation Unit Info");
9988 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9989 "Compilation Unit Length");
9991 FOR_EACH_VEC_ELT (*names, i, pub)
9993 if (include_pubname_in_output (names, pub))
9995 dw_offset die_offset = pub->die->die_offset;
9997 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9998 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9999 gcc_assert (pub->die->die_mark);
10001 /* If we're putting types in their own .debug_types sections,
10002 the .debug_pubtypes table will still point to the compile
10003 unit (not the type unit), so we want to use the offset of
10004 the skeleton DIE (if there is one). */
10005 if (pub->die->comdat_type_p && names == pubtype_table)
10007 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10009 if (type_node != NULL)
10010 die_offset = (type_node->skeleton_die != NULL
10011 ? type_node->skeleton_die->die_offset
10012 : comp_unit_die ()->die_offset);
10015 output_pubname (die_offset, pub);
10019 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10022 /* Output public names and types tables if necessary. */
10024 static void
10025 output_pubtables (void)
10027 if (!want_pubnames () || !info_section_emitted)
10028 return;
10030 switch_to_section (debug_pubnames_section);
10031 output_pubnames (pubname_table);
10032 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10033 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10034 simply won't look for the section. */
10035 switch_to_section (debug_pubtypes_section);
10036 output_pubnames (pubtype_table);
10040 /* Output the information that goes into the .debug_aranges table.
10041 Namely, define the beginning and ending address range of the
10042 text section generated for this compilation unit. */
10044 static void
10045 output_aranges (void)
10047 unsigned i;
10048 unsigned long aranges_length = size_of_aranges ();
10050 if (!XCOFF_DEBUGGING_INFO)
10052 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10053 dw2_asm_output_data (4, 0xffffffff,
10054 "Initial length escape value indicating 64-bit DWARF extension");
10055 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10056 "Length of Address Ranges Info");
10059 /* Version number for aranges is still 2, even up to DWARF5. */
10060 dw2_asm_output_data (2, 2, "DWARF Version");
10061 if (dwarf_split_debug_info)
10062 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10063 debug_skeleton_info_section,
10064 "Offset of Compilation Unit Info");
10065 else
10066 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10067 debug_info_section,
10068 "Offset of Compilation Unit Info");
10069 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10070 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10072 /* We need to align to twice the pointer size here. */
10073 if (DWARF_ARANGES_PAD_SIZE)
10075 /* Pad using a 2 byte words so that padding is correct for any
10076 pointer size. */
10077 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10078 2 * DWARF2_ADDR_SIZE);
10079 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10080 dw2_asm_output_data (2, 0, NULL);
10083 /* It is necessary not to output these entries if the sections were
10084 not used; if the sections were not used, the length will be 0 and
10085 the address may end up as 0 if the section is discarded by ld
10086 --gc-sections, leaving an invalid (0, 0) entry that can be
10087 confused with the terminator. */
10088 if (text_section_used)
10090 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10091 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10092 text_section_label, "Length");
10094 if (cold_text_section_used)
10096 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10097 "Address");
10098 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10099 cold_text_section_label, "Length");
10102 if (have_multiple_function_sections)
10104 unsigned fde_idx;
10105 dw_fde_ref fde;
10107 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10109 if (DECL_IGNORED_P (fde->decl))
10110 continue;
10111 if (!fde->in_std_section)
10113 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10114 "Address");
10115 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10116 fde->dw_fde_begin, "Length");
10118 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10120 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10121 "Address");
10122 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10123 fde->dw_fde_second_begin, "Length");
10128 /* Output the terminator words. */
10129 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10130 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10133 /* Add a new entry to .debug_ranges. Return the offset at which it
10134 was placed. */
10136 static unsigned int
10137 add_ranges_num (int num)
10139 dw_ranges r = { num };
10140 vec_safe_push (ranges_table, r);
10141 return (vec_safe_length (ranges_table) - 1) * 2 * DWARF2_ADDR_SIZE;
10144 /* Add a new entry to .debug_ranges corresponding to a block, or a
10145 range terminator if BLOCK is NULL. */
10147 static unsigned int
10148 add_ranges (const_tree block)
10150 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10153 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10154 When using dwarf_split_debug_info, address attributes in dies destined
10155 for the final executable should be direct references--setting the
10156 parameter force_direct ensures this behavior. */
10158 static void
10159 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10160 bool *added, bool force_direct)
10162 unsigned int in_use = vec_safe_length (ranges_by_label);
10163 unsigned int offset;
10164 dw_ranges_by_label rbl = { begin, end };
10165 vec_safe_push (ranges_by_label, rbl);
10166 offset = add_ranges_num (-(int)in_use - 1);
10167 if (!*added)
10169 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10170 *added = true;
10174 static void
10175 output_ranges (void)
10177 unsigned i;
10178 static const char *const start_fmt = "Offset %#x";
10179 const char *fmt = start_fmt;
10180 dw_ranges *r;
10182 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
10184 int block_num = r->num;
10186 if (block_num > 0)
10188 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10189 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10191 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10192 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10194 /* If all code is in the text section, then the compilation
10195 unit base address defaults to DW_AT_low_pc, which is the
10196 base of the text section. */
10197 if (!have_multiple_function_sections)
10199 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10200 text_section_label,
10201 fmt, i * 2 * DWARF2_ADDR_SIZE);
10202 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10203 text_section_label, NULL);
10206 /* Otherwise, the compilation unit base address is zero,
10207 which allows us to use absolute addresses, and not worry
10208 about whether the target supports cross-section
10209 arithmetic. */
10210 else
10212 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10213 fmt, i * 2 * DWARF2_ADDR_SIZE);
10214 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10217 fmt = NULL;
10220 /* Negative block_num stands for an index into ranges_by_label. */
10221 else if (block_num < 0)
10223 int lab_idx = - block_num - 1;
10225 if (!have_multiple_function_sections)
10227 gcc_unreachable ();
10228 #if 0
10229 /* If we ever use add_ranges_by_labels () for a single
10230 function section, all we have to do is to take out
10231 the #if 0 above. */
10232 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10233 (*ranges_by_label)[lab_idx].begin,
10234 text_section_label,
10235 fmt, i * 2 * DWARF2_ADDR_SIZE);
10236 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10237 (*ranges_by_label)[lab_idx].end,
10238 text_section_label, NULL);
10239 #endif
10241 else
10243 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10244 (*ranges_by_label)[lab_idx].begin,
10245 fmt, i * 2 * DWARF2_ADDR_SIZE);
10246 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10247 (*ranges_by_label)[lab_idx].end,
10248 NULL);
10251 else
10253 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10254 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10255 fmt = start_fmt;
10260 /* Data structure containing information about input files. */
10261 struct file_info
10263 const char *path; /* Complete file name. */
10264 const char *fname; /* File name part. */
10265 int length; /* Length of entire string. */
10266 struct dwarf_file_data * file_idx; /* Index in input file table. */
10267 int dir_idx; /* Index in directory table. */
10270 /* Data structure containing information about directories with source
10271 files. */
10272 struct dir_info
10274 const char *path; /* Path including directory name. */
10275 int length; /* Path length. */
10276 int prefix; /* Index of directory entry which is a prefix. */
10277 int count; /* Number of files in this directory. */
10278 int dir_idx; /* Index of directory used as base. */
10281 /* Callback function for file_info comparison. We sort by looking at
10282 the directories in the path. */
10284 static int
10285 file_info_cmp (const void *p1, const void *p2)
10287 const struct file_info *const s1 = (const struct file_info *) p1;
10288 const struct file_info *const s2 = (const struct file_info *) p2;
10289 const unsigned char *cp1;
10290 const unsigned char *cp2;
10292 /* Take care of file names without directories. We need to make sure that
10293 we return consistent values to qsort since some will get confused if
10294 we return the same value when identical operands are passed in opposite
10295 orders. So if neither has a directory, return 0 and otherwise return
10296 1 or -1 depending on which one has the directory. */
10297 if ((s1->path == s1->fname || s2->path == s2->fname))
10298 return (s2->path == s2->fname) - (s1->path == s1->fname);
10300 cp1 = (const unsigned char *) s1->path;
10301 cp2 = (const unsigned char *) s2->path;
10303 while (1)
10305 ++cp1;
10306 ++cp2;
10307 /* Reached the end of the first path? If so, handle like above. */
10308 if ((cp1 == (const unsigned char *) s1->fname)
10309 || (cp2 == (const unsigned char *) s2->fname))
10310 return ((cp2 == (const unsigned char *) s2->fname)
10311 - (cp1 == (const unsigned char *) s1->fname));
10313 /* Character of current path component the same? */
10314 else if (*cp1 != *cp2)
10315 return *cp1 - *cp2;
10319 struct file_name_acquire_data
10321 struct file_info *files;
10322 int used_files;
10323 int max_files;
10326 /* Traversal function for the hash table. */
10329 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10331 struct dwarf_file_data *d = *slot;
10332 struct file_info *fi;
10333 const char *f;
10335 gcc_assert (fnad->max_files >= d->emitted_number);
10337 if (! d->emitted_number)
10338 return 1;
10340 gcc_assert (fnad->max_files != fnad->used_files);
10342 fi = fnad->files + fnad->used_files++;
10344 /* Skip all leading "./". */
10345 f = d->filename;
10346 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10347 f += 2;
10349 /* Create a new array entry. */
10350 fi->path = f;
10351 fi->length = strlen (f);
10352 fi->file_idx = d;
10354 /* Search for the file name part. */
10355 f = strrchr (f, DIR_SEPARATOR);
10356 #if defined (DIR_SEPARATOR_2)
10358 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10360 if (g != NULL)
10362 if (f == NULL || f < g)
10363 f = g;
10366 #endif
10368 fi->fname = f == NULL ? fi->path : f + 1;
10369 return 1;
10372 /* Output the directory table and the file name table. We try to minimize
10373 the total amount of memory needed. A heuristic is used to avoid large
10374 slowdowns with many input files. */
10376 static void
10377 output_file_names (void)
10379 struct file_name_acquire_data fnad;
10380 int numfiles;
10381 struct file_info *files;
10382 struct dir_info *dirs;
10383 int *saved;
10384 int *savehere;
10385 int *backmap;
10386 int ndirs;
10387 int idx_offset;
10388 int i;
10390 if (!last_emitted_file)
10392 dw2_asm_output_data (1, 0, "End directory table");
10393 dw2_asm_output_data (1, 0, "End file name table");
10394 return;
10397 numfiles = last_emitted_file->emitted_number;
10399 /* Allocate the various arrays we need. */
10400 files = XALLOCAVEC (struct file_info, numfiles);
10401 dirs = XALLOCAVEC (struct dir_info, numfiles);
10403 fnad.files = files;
10404 fnad.used_files = 0;
10405 fnad.max_files = numfiles;
10406 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10407 gcc_assert (fnad.used_files == fnad.max_files);
10409 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10411 /* Find all the different directories used. */
10412 dirs[0].path = files[0].path;
10413 dirs[0].length = files[0].fname - files[0].path;
10414 dirs[0].prefix = -1;
10415 dirs[0].count = 1;
10416 dirs[0].dir_idx = 0;
10417 files[0].dir_idx = 0;
10418 ndirs = 1;
10420 for (i = 1; i < numfiles; i++)
10421 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10422 && memcmp (dirs[ndirs - 1].path, files[i].path,
10423 dirs[ndirs - 1].length) == 0)
10425 /* Same directory as last entry. */
10426 files[i].dir_idx = ndirs - 1;
10427 ++dirs[ndirs - 1].count;
10429 else
10431 int j;
10433 /* This is a new directory. */
10434 dirs[ndirs].path = files[i].path;
10435 dirs[ndirs].length = files[i].fname - files[i].path;
10436 dirs[ndirs].count = 1;
10437 dirs[ndirs].dir_idx = ndirs;
10438 files[i].dir_idx = ndirs;
10440 /* Search for a prefix. */
10441 dirs[ndirs].prefix = -1;
10442 for (j = 0; j < ndirs; j++)
10443 if (dirs[j].length < dirs[ndirs].length
10444 && dirs[j].length > 1
10445 && (dirs[ndirs].prefix == -1
10446 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10447 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10448 dirs[ndirs].prefix = j;
10450 ++ndirs;
10453 /* Now to the actual work. We have to find a subset of the directories which
10454 allow expressing the file name using references to the directory table
10455 with the least amount of characters. We do not do an exhaustive search
10456 where we would have to check out every combination of every single
10457 possible prefix. Instead we use a heuristic which provides nearly optimal
10458 results in most cases and never is much off. */
10459 saved = XALLOCAVEC (int, ndirs);
10460 savehere = XALLOCAVEC (int, ndirs);
10462 memset (saved, '\0', ndirs * sizeof (saved[0]));
10463 for (i = 0; i < ndirs; i++)
10465 int j;
10466 int total;
10468 /* We can always save some space for the current directory. But this
10469 does not mean it will be enough to justify adding the directory. */
10470 savehere[i] = dirs[i].length;
10471 total = (savehere[i] - saved[i]) * dirs[i].count;
10473 for (j = i + 1; j < ndirs; j++)
10475 savehere[j] = 0;
10476 if (saved[j] < dirs[i].length)
10478 /* Determine whether the dirs[i] path is a prefix of the
10479 dirs[j] path. */
10480 int k;
10482 k = dirs[j].prefix;
10483 while (k != -1 && k != (int) i)
10484 k = dirs[k].prefix;
10486 if (k == (int) i)
10488 /* Yes it is. We can possibly save some memory by
10489 writing the filenames in dirs[j] relative to
10490 dirs[i]. */
10491 savehere[j] = dirs[i].length;
10492 total += (savehere[j] - saved[j]) * dirs[j].count;
10497 /* Check whether we can save enough to justify adding the dirs[i]
10498 directory. */
10499 if (total > dirs[i].length + 1)
10501 /* It's worthwhile adding. */
10502 for (j = i; j < ndirs; j++)
10503 if (savehere[j] > 0)
10505 /* Remember how much we saved for this directory so far. */
10506 saved[j] = savehere[j];
10508 /* Remember the prefix directory. */
10509 dirs[j].dir_idx = i;
10514 /* Emit the directory name table. */
10515 idx_offset = dirs[0].length > 0 ? 1 : 0;
10516 for (i = 1 - idx_offset; i < ndirs; i++)
10517 dw2_asm_output_nstring (dirs[i].path,
10518 dirs[i].length
10519 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10520 "Directory Entry: %#x", i + idx_offset);
10522 dw2_asm_output_data (1, 0, "End directory table");
10524 /* We have to emit them in the order of emitted_number since that's
10525 used in the debug info generation. To do this efficiently we
10526 generate a back-mapping of the indices first. */
10527 backmap = XALLOCAVEC (int, numfiles);
10528 for (i = 0; i < numfiles; i++)
10529 backmap[files[i].file_idx->emitted_number - 1] = i;
10531 /* Now write all the file names. */
10532 for (i = 0; i < numfiles; i++)
10534 int file_idx = backmap[i];
10535 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10537 #ifdef VMS_DEBUGGING_INFO
10538 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10540 /* Setting these fields can lead to debugger miscomparisons,
10541 but VMS Debug requires them to be set correctly. */
10543 int ver;
10544 long long cdt;
10545 long siz;
10546 int maxfilelen = strlen (files[file_idx].path)
10547 + dirs[dir_idx].length
10548 + MAX_VMS_VERSION_LEN + 1;
10549 char *filebuf = XALLOCAVEC (char, maxfilelen);
10551 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10552 snprintf (filebuf, maxfilelen, "%s;%d",
10553 files[file_idx].path + dirs[dir_idx].length, ver);
10555 dw2_asm_output_nstring
10556 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10558 /* Include directory index. */
10559 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10561 /* Modification time. */
10562 dw2_asm_output_data_uleb128
10563 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10564 ? cdt : 0,
10565 NULL);
10567 /* File length in bytes. */
10568 dw2_asm_output_data_uleb128
10569 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10570 ? siz : 0,
10571 NULL);
10572 #else
10573 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10574 "File Entry: %#x", (unsigned) i + 1);
10576 /* Include directory index. */
10577 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10579 /* Modification time. */
10580 dw2_asm_output_data_uleb128 (0, NULL);
10582 /* File length in bytes. */
10583 dw2_asm_output_data_uleb128 (0, NULL);
10584 #endif /* VMS_DEBUGGING_INFO */
10587 dw2_asm_output_data (1, 0, "End file name table");
10591 /* Output one line number table into the .debug_line section. */
10593 static void
10594 output_one_line_info_table (dw_line_info_table *table)
10596 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10597 unsigned int current_line = 1;
10598 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10599 dw_line_info_entry *ent;
10600 size_t i;
10602 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10604 switch (ent->opcode)
10606 case LI_set_address:
10607 /* ??? Unfortunately, we have little choice here currently, and
10608 must always use the most general form. GCC does not know the
10609 address delta itself, so we can't use DW_LNS_advance_pc. Many
10610 ports do have length attributes which will give an upper bound
10611 on the address range. We could perhaps use length attributes
10612 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10613 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10615 /* This can handle any delta. This takes
10616 4+DWARF2_ADDR_SIZE bytes. */
10617 dw2_asm_output_data (1, 0, "set address %s", line_label);
10618 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10619 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10620 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10621 break;
10623 case LI_set_line:
10624 if (ent->val == current_line)
10626 /* We still need to start a new row, so output a copy insn. */
10627 dw2_asm_output_data (1, DW_LNS_copy,
10628 "copy line %u", current_line);
10630 else
10632 int line_offset = ent->val - current_line;
10633 int line_delta = line_offset - DWARF_LINE_BASE;
10635 current_line = ent->val;
10636 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10638 /* This can handle deltas from -10 to 234, using the current
10639 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10640 This takes 1 byte. */
10641 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10642 "line %u", current_line);
10644 else
10646 /* This can handle any delta. This takes at least 4 bytes,
10647 depending on the value being encoded. */
10648 dw2_asm_output_data (1, DW_LNS_advance_line,
10649 "advance to line %u", current_line);
10650 dw2_asm_output_data_sleb128 (line_offset, NULL);
10651 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10654 break;
10656 case LI_set_file:
10657 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10658 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10659 break;
10661 case LI_set_column:
10662 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10663 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10664 break;
10666 case LI_negate_stmt:
10667 current_is_stmt = !current_is_stmt;
10668 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10669 "is_stmt %d", current_is_stmt);
10670 break;
10672 case LI_set_prologue_end:
10673 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10674 "set prologue end");
10675 break;
10677 case LI_set_epilogue_begin:
10678 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10679 "set epilogue begin");
10680 break;
10682 case LI_set_discriminator:
10683 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10684 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10685 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10686 dw2_asm_output_data_uleb128 (ent->val, NULL);
10687 break;
10691 /* Emit debug info for the address of the end of the table. */
10692 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10693 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10694 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10695 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10697 dw2_asm_output_data (1, 0, "end sequence");
10698 dw2_asm_output_data_uleb128 (1, NULL);
10699 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10702 /* Output the source line number correspondence information. This
10703 information goes into the .debug_line section. */
10705 static void
10706 output_line_info (bool prologue_only)
10708 static unsigned int generation;
10709 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
10710 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
10711 /* We don't support DWARFv5 line tables yet. */
10712 int ver = dwarf_version < 5 ? dwarf_version : 4;
10713 bool saw_one = false;
10714 int opc;
10716 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
10717 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
10718 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
10719 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
10721 if (!XCOFF_DEBUGGING_INFO)
10723 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10724 dw2_asm_output_data (4, 0xffffffff,
10725 "Initial length escape value indicating 64-bit DWARF extension");
10726 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10727 "Length of Source Line Info");
10730 ASM_OUTPUT_LABEL (asm_out_file, l1);
10732 dw2_asm_output_data (2, ver, "DWARF Version");
10733 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10734 ASM_OUTPUT_LABEL (asm_out_file, p1);
10736 /* Define the architecture-dependent minimum instruction length (in bytes).
10737 In this implementation of DWARF, this field is used for information
10738 purposes only. Since GCC generates assembly language, we have no
10739 a priori knowledge of how many instruction bytes are generated for each
10740 source line, and therefore can use only the DW_LNE_set_address and
10741 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10742 this as '1', which is "correct enough" for all architectures,
10743 and don't let the target override. */
10744 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10746 if (ver >= 4)
10747 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10748 "Maximum Operations Per Instruction");
10749 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10750 "Default is_stmt_start flag");
10751 dw2_asm_output_data (1, DWARF_LINE_BASE,
10752 "Line Base Value (Special Opcodes)");
10753 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10754 "Line Range Value (Special Opcodes)");
10755 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10756 "Special Opcode Base");
10758 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10760 int n_op_args;
10761 switch (opc)
10763 case DW_LNS_advance_pc:
10764 case DW_LNS_advance_line:
10765 case DW_LNS_set_file:
10766 case DW_LNS_set_column:
10767 case DW_LNS_fixed_advance_pc:
10768 case DW_LNS_set_isa:
10769 n_op_args = 1;
10770 break;
10771 default:
10772 n_op_args = 0;
10773 break;
10776 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10777 opc, n_op_args);
10780 /* Write out the information about the files we use. */
10781 output_file_names ();
10782 ASM_OUTPUT_LABEL (asm_out_file, p2);
10783 if (prologue_only)
10785 /* Output the marker for the end of the line number info. */
10786 ASM_OUTPUT_LABEL (asm_out_file, l2);
10787 return;
10790 if (separate_line_info)
10792 dw_line_info_table *table;
10793 size_t i;
10795 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10796 if (table->in_use)
10798 output_one_line_info_table (table);
10799 saw_one = true;
10802 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10804 output_one_line_info_table (cold_text_section_line_info);
10805 saw_one = true;
10808 /* ??? Some Darwin linkers crash on a .debug_line section with no
10809 sequences. Further, merely a DW_LNE_end_sequence entry is not
10810 sufficient -- the address column must also be initialized.
10811 Make sure to output at least one set_address/end_sequence pair,
10812 choosing .text since that section is always present. */
10813 if (text_section_line_info->in_use || !saw_one)
10814 output_one_line_info_table (text_section_line_info);
10816 /* Output the marker for the end of the line number info. */
10817 ASM_OUTPUT_LABEL (asm_out_file, l2);
10820 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
10822 static inline bool
10823 need_endianity_attribute_p (bool reverse)
10825 return reverse && (dwarf_version >= 3 || !dwarf_strict);
10828 /* Given a pointer to a tree node for some base type, return a pointer to
10829 a DIE that describes the given type. REVERSE is true if the type is
10830 to be interpreted in the reverse storage order wrt the target order.
10832 This routine must only be called for GCC type nodes that correspond to
10833 Dwarf base (fundamental) types. */
10835 static dw_die_ref
10836 base_type_die (tree type, bool reverse)
10838 dw_die_ref base_type_result;
10839 enum dwarf_type encoding;
10840 bool fpt_used = false;
10841 struct fixed_point_type_info fpt_info;
10842 tree type_bias = NULL_TREE;
10844 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10845 return 0;
10847 /* If this is a subtype that should not be emitted as a subrange type,
10848 use the base type. See subrange_type_for_debug_p. */
10849 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10850 type = TREE_TYPE (type);
10852 switch (TREE_CODE (type))
10854 case INTEGER_TYPE:
10855 if ((dwarf_version >= 4 || !dwarf_strict)
10856 && TYPE_NAME (type)
10857 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10858 && DECL_IS_BUILTIN (TYPE_NAME (type))
10859 && DECL_NAME (TYPE_NAME (type)))
10861 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10862 if (strcmp (name, "char16_t") == 0
10863 || strcmp (name, "char32_t") == 0)
10865 encoding = DW_ATE_UTF;
10866 break;
10869 if ((dwarf_version >= 3 || !dwarf_strict)
10870 && lang_hooks.types.get_fixed_point_type_info)
10872 memset (&fpt_info, 0, sizeof (fpt_info));
10873 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10875 fpt_used = true;
10876 encoding = ((TYPE_UNSIGNED (type))
10877 ? DW_ATE_unsigned_fixed
10878 : DW_ATE_signed_fixed);
10879 break;
10882 if (TYPE_STRING_FLAG (type))
10884 if (TYPE_UNSIGNED (type))
10885 encoding = DW_ATE_unsigned_char;
10886 else
10887 encoding = DW_ATE_signed_char;
10889 else if (TYPE_UNSIGNED (type))
10890 encoding = DW_ATE_unsigned;
10891 else
10892 encoding = DW_ATE_signed;
10894 if (!dwarf_strict
10895 && lang_hooks.types.get_type_bias)
10896 type_bias = lang_hooks.types.get_type_bias (type);
10897 break;
10899 case REAL_TYPE:
10900 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10902 if (dwarf_version >= 3 || !dwarf_strict)
10903 encoding = DW_ATE_decimal_float;
10904 else
10905 encoding = DW_ATE_lo_user;
10907 else
10908 encoding = DW_ATE_float;
10909 break;
10911 case FIXED_POINT_TYPE:
10912 if (!(dwarf_version >= 3 || !dwarf_strict))
10913 encoding = DW_ATE_lo_user;
10914 else if (TYPE_UNSIGNED (type))
10915 encoding = DW_ATE_unsigned_fixed;
10916 else
10917 encoding = DW_ATE_signed_fixed;
10918 break;
10920 /* Dwarf2 doesn't know anything about complex ints, so use
10921 a user defined type for it. */
10922 case COMPLEX_TYPE:
10923 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10924 encoding = DW_ATE_complex_float;
10925 else
10926 encoding = DW_ATE_lo_user;
10927 break;
10929 case BOOLEAN_TYPE:
10930 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10931 encoding = DW_ATE_boolean;
10932 break;
10934 default:
10935 /* No other TREE_CODEs are Dwarf fundamental types. */
10936 gcc_unreachable ();
10939 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10941 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10942 int_size_in_bytes (type));
10943 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10945 if (need_endianity_attribute_p (reverse))
10946 add_AT_unsigned (base_type_result, DW_AT_endianity,
10947 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10949 if (fpt_used)
10951 switch (fpt_info.scale_factor_kind)
10953 case fixed_point_scale_factor_binary:
10954 add_AT_int (base_type_result, DW_AT_binary_scale,
10955 fpt_info.scale_factor.binary);
10956 break;
10958 case fixed_point_scale_factor_decimal:
10959 add_AT_int (base_type_result, DW_AT_decimal_scale,
10960 fpt_info.scale_factor.decimal);
10961 break;
10963 case fixed_point_scale_factor_arbitrary:
10964 /* Arbitrary scale factors cannot be described in standard DWARF,
10965 yet. */
10966 if (!dwarf_strict)
10968 /* Describe the scale factor as a rational constant. */
10969 const dw_die_ref scale_factor
10970 = new_die (DW_TAG_constant, comp_unit_die (), type);
10972 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
10973 fpt_info.scale_factor.arbitrary.numerator);
10974 add_AT_int (scale_factor, DW_AT_GNU_denominator,
10975 fpt_info.scale_factor.arbitrary.denominator);
10977 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
10979 break;
10981 default:
10982 gcc_unreachable ();
10986 if (type_bias)
10987 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
10988 dw_scalar_form_constant
10989 | dw_scalar_form_exprloc
10990 | dw_scalar_form_reference,
10991 NULL);
10993 add_pubtype (type, base_type_result);
10995 return base_type_result;
10998 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10999 named 'auto' in its type: return true for it, false otherwise. */
11001 static inline bool
11002 is_cxx_auto (tree type)
11004 if (is_cxx ())
11006 tree name = TYPE_IDENTIFIER (type);
11007 if (name == get_identifier ("auto")
11008 || name == get_identifier ("decltype(auto)"))
11009 return true;
11011 return false;
11014 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11015 given input type is a Dwarf "fundamental" type. Otherwise return null. */
11017 static inline int
11018 is_base_type (tree type)
11020 switch (TREE_CODE (type))
11022 case ERROR_MARK:
11023 case VOID_TYPE:
11024 case INTEGER_TYPE:
11025 case REAL_TYPE:
11026 case FIXED_POINT_TYPE:
11027 case COMPLEX_TYPE:
11028 case BOOLEAN_TYPE:
11029 case POINTER_BOUNDS_TYPE:
11030 return 1;
11032 case ARRAY_TYPE:
11033 case RECORD_TYPE:
11034 case UNION_TYPE:
11035 case QUAL_UNION_TYPE:
11036 case ENUMERAL_TYPE:
11037 case FUNCTION_TYPE:
11038 case METHOD_TYPE:
11039 case POINTER_TYPE:
11040 case REFERENCE_TYPE:
11041 case NULLPTR_TYPE:
11042 case OFFSET_TYPE:
11043 case LANG_TYPE:
11044 case VECTOR_TYPE:
11045 return 0;
11047 default:
11048 if (is_cxx_auto (type))
11049 return 0;
11050 gcc_unreachable ();
11053 return 0;
11056 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11057 node, return the size in bits for the type if it is a constant, or else
11058 return the alignment for the type if the type's size is not constant, or
11059 else return BITS_PER_WORD if the type actually turns out to be an
11060 ERROR_MARK node. */
11062 static inline unsigned HOST_WIDE_INT
11063 simple_type_size_in_bits (const_tree type)
11065 if (TREE_CODE (type) == ERROR_MARK)
11066 return BITS_PER_WORD;
11067 else if (TYPE_SIZE (type) == NULL_TREE)
11068 return 0;
11069 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11070 return tree_to_uhwi (TYPE_SIZE (type));
11071 else
11072 return TYPE_ALIGN (type);
11075 /* Similarly, but return an offset_int instead of UHWI. */
11077 static inline offset_int
11078 offset_int_type_size_in_bits (const_tree type)
11080 if (TREE_CODE (type) == ERROR_MARK)
11081 return BITS_PER_WORD;
11082 else if (TYPE_SIZE (type) == NULL_TREE)
11083 return 0;
11084 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11085 return wi::to_offset (TYPE_SIZE (type));
11086 else
11087 return TYPE_ALIGN (type);
11090 /* Given a pointer to a tree node for a subrange type, return a pointer
11091 to a DIE that describes the given type. */
11093 static dw_die_ref
11094 subrange_type_die (tree type, tree low, tree high, tree bias,
11095 dw_die_ref context_die)
11097 dw_die_ref subrange_die;
11098 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11100 if (context_die == NULL)
11101 context_die = comp_unit_die ();
11103 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11105 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11107 /* The size of the subrange type and its base type do not match,
11108 so we need to generate a size attribute for the subrange type. */
11109 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11112 if (low)
11113 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11114 if (high)
11115 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11116 if (bias && !dwarf_strict)
11117 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11118 dw_scalar_form_constant
11119 | dw_scalar_form_exprloc
11120 | dw_scalar_form_reference,
11121 NULL);
11123 return subrange_die;
11126 /* Returns the (const and/or volatile) cv_qualifiers associated with
11127 the decl node. This will normally be augmented with the
11128 cv_qualifiers of the underlying type in add_type_attribute. */
11130 static int
11131 decl_quals (const_tree decl)
11133 return ((TREE_READONLY (decl)
11134 /* The C++ front-end correctly marks reference-typed
11135 variables as readonly, but from a language (and debug
11136 info) standpoint they are not const-qualified. */
11137 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
11138 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11139 | (TREE_THIS_VOLATILE (decl)
11140 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11143 /* Determine the TYPE whose qualifiers match the largest strict subset
11144 of the given TYPE_QUALS, and return its qualifiers. Ignore all
11145 qualifiers outside QUAL_MASK. */
11147 static int
11148 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11150 tree t;
11151 int best_rank = 0, best_qual = 0, max_rank;
11153 type_quals &= qual_mask;
11154 max_rank = popcount_hwi (type_quals) - 1;
11156 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11157 t = TYPE_NEXT_VARIANT (t))
11159 int q = TYPE_QUALS (t) & qual_mask;
11161 if ((q & type_quals) == q && q != type_quals
11162 && check_base_type (t, type))
11164 int rank = popcount_hwi (q);
11166 if (rank > best_rank)
11168 best_rank = rank;
11169 best_qual = q;
11174 return best_qual;
11177 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11178 static const dwarf_qual_info_t dwarf_qual_info[] =
11180 { TYPE_QUAL_CONST, DW_TAG_const_type },
11181 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11182 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11183 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11185 static const unsigned int dwarf_qual_info_size
11186 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11188 /* If DIE is a qualified DIE of some base DIE with the same parent,
11189 return the base DIE, otherwise return NULL. Set MASK to the
11190 qualifiers added compared to the returned DIE. */
11192 static dw_die_ref
11193 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11195 unsigned int i;
11196 for (i = 0; i < dwarf_qual_info_size; i++)
11197 if (die->die_tag == dwarf_qual_info[i].t)
11198 break;
11199 if (i == dwarf_qual_info_size)
11200 return NULL;
11201 if (vec_safe_length (die->die_attr) != 1)
11202 return NULL;
11203 dw_die_ref type = get_AT_ref (die, DW_AT_type);
11204 if (type == NULL || type->die_parent != die->die_parent)
11205 return NULL;
11206 *mask |= dwarf_qual_info[i].q;
11207 if (depth)
11209 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11210 if (ret)
11211 return ret;
11213 return type;
11216 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11217 entry that chains the modifiers specified by CV_QUALS in front of the
11218 given type. REVERSE is true if the type is to be interpreted in the
11219 reverse storage order wrt the target order. */
11221 static dw_die_ref
11222 modified_type_die (tree type, int cv_quals, bool reverse,
11223 dw_die_ref context_die)
11225 enum tree_code code = TREE_CODE (type);
11226 dw_die_ref mod_type_die;
11227 dw_die_ref sub_die = NULL;
11228 tree item_type = NULL;
11229 tree qualified_type;
11230 tree name, low, high;
11231 dw_die_ref mod_scope;
11232 /* Only these cv-qualifiers are currently handled. */
11233 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11234 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11236 if (code == ERROR_MARK)
11237 return NULL;
11239 if (lang_hooks.types.get_debug_type)
11241 tree debug_type = lang_hooks.types.get_debug_type (type);
11243 if (debug_type != NULL_TREE && debug_type != type)
11244 return modified_type_die (debug_type, cv_quals, reverse, context_die);
11247 cv_quals &= cv_qual_mask;
11249 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11250 tag modifier (and not an attribute) old consumers won't be able
11251 to handle it. */
11252 if (dwarf_version < 3)
11253 cv_quals &= ~TYPE_QUAL_RESTRICT;
11255 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
11256 if (dwarf_version < 5)
11257 cv_quals &= ~TYPE_QUAL_ATOMIC;
11259 /* See if we already have the appropriately qualified variant of
11260 this type. */
11261 qualified_type = get_qualified_type (type, cv_quals);
11263 if (qualified_type == sizetype
11264 && TYPE_NAME (qualified_type)
11265 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11267 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11269 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11270 && TYPE_PRECISION (t)
11271 == TYPE_PRECISION (qualified_type)
11272 && TYPE_UNSIGNED (t)
11273 == TYPE_UNSIGNED (qualified_type));
11274 qualified_type = t;
11277 /* If we do, then we can just use its DIE, if it exists. */
11278 if (qualified_type)
11280 mod_type_die = lookup_type_die (qualified_type);
11282 /* DW_AT_endianity doesn't come from a qualifier on the type. */
11283 if (mod_type_die
11284 && (!need_endianity_attribute_p (reverse)
11285 || !is_base_type (type)
11286 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11287 return mod_type_die;
11290 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11292 /* Handle C typedef types. */
11293 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11294 && !DECL_ARTIFICIAL (name))
11296 tree dtype = TREE_TYPE (name);
11298 if (qualified_type == dtype)
11300 /* For a named type, use the typedef. */
11301 gen_type_die (qualified_type, context_die);
11302 return lookup_type_die (qualified_type);
11304 else
11306 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11307 dquals &= cv_qual_mask;
11308 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11309 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11310 /* cv-unqualified version of named type. Just use
11311 the unnamed type to which it refers. */
11312 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11313 reverse, context_die);
11314 /* Else cv-qualified version of named type; fall through. */
11318 mod_scope = scope_die_for (type, context_die);
11320 if (cv_quals)
11322 int sub_quals = 0, first_quals = 0;
11323 unsigned i;
11324 dw_die_ref first = NULL, last = NULL;
11326 /* Determine a lesser qualified type that most closely matches
11327 this one. Then generate DW_TAG_* entries for the remaining
11328 qualifiers. */
11329 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11330 cv_qual_mask);
11331 if (sub_quals && use_debug_types)
11333 bool needed = false;
11334 /* If emitting type units, make sure the order of qualifiers
11335 is canonical. Thus, start from unqualified type if
11336 an earlier qualifier is missing in sub_quals, but some later
11337 one is present there. */
11338 for (i = 0; i < dwarf_qual_info_size; i++)
11339 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11340 needed = true;
11341 else if (needed && (dwarf_qual_info[i].q & cv_quals))
11343 sub_quals = 0;
11344 break;
11347 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11348 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11350 /* As not all intermediate qualified DIEs have corresponding
11351 tree types, ensure that qualified DIEs in the same scope
11352 as their DW_AT_type are emitted after their DW_AT_type,
11353 only with other qualified DIEs for the same type possibly
11354 in between them. Determine the range of such qualified
11355 DIEs now (first being the base type, last being corresponding
11356 last qualified DIE for it). */
11357 unsigned int count = 0;
11358 first = qualified_die_p (mod_type_die, &first_quals,
11359 dwarf_qual_info_size);
11360 if (first == NULL)
11361 first = mod_type_die;
11362 gcc_assert ((first_quals & ~sub_quals) == 0);
11363 for (count = 0, last = first;
11364 count < (1U << dwarf_qual_info_size);
11365 count++, last = last->die_sib)
11367 int quals = 0;
11368 if (last == mod_scope->die_child)
11369 break;
11370 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11371 != first)
11372 break;
11376 for (i = 0; i < dwarf_qual_info_size; i++)
11377 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11379 dw_die_ref d;
11380 if (first && first != last)
11382 for (d = first->die_sib; ; d = d->die_sib)
11384 int quals = 0;
11385 qualified_die_p (d, &quals, dwarf_qual_info_size);
11386 if (quals == (first_quals | dwarf_qual_info[i].q))
11387 break;
11388 if (d == last)
11390 d = NULL;
11391 break;
11394 if (d)
11396 mod_type_die = d;
11397 continue;
11400 if (first)
11402 d = ggc_cleared_alloc<die_node> ();
11403 d->die_tag = dwarf_qual_info[i].t;
11404 add_child_die_after (mod_scope, d, last);
11405 last = d;
11407 else
11408 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11409 if (mod_type_die)
11410 add_AT_die_ref (d, DW_AT_type, mod_type_die);
11411 mod_type_die = d;
11412 first_quals |= dwarf_qual_info[i].q;
11415 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11417 dwarf_tag tag = DW_TAG_pointer_type;
11418 if (code == REFERENCE_TYPE)
11420 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11421 tag = DW_TAG_rvalue_reference_type;
11422 else
11423 tag = DW_TAG_reference_type;
11425 mod_type_die = new_die (tag, mod_scope, type);
11427 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11428 simple_type_size_in_bits (type) / BITS_PER_UNIT);
11429 item_type = TREE_TYPE (type);
11431 addr_space_t as = TYPE_ADDR_SPACE (item_type);
11432 if (!ADDR_SPACE_GENERIC_P (as))
11434 int action = targetm.addr_space.debug (as);
11435 if (action >= 0)
11437 /* Positive values indicate an address_class. */
11438 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11440 else
11442 /* Negative values indicate an (inverted) segment base reg. */
11443 dw_loc_descr_ref d
11444 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11445 add_AT_loc (mod_type_die, DW_AT_segment, d);
11449 else if (code == INTEGER_TYPE
11450 && TREE_TYPE (type) != NULL_TREE
11451 && subrange_type_for_debug_p (type, &low, &high))
11453 tree bias = NULL_TREE;
11454 if (lang_hooks.types.get_type_bias)
11455 bias = lang_hooks.types.get_type_bias (type);
11456 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11457 item_type = TREE_TYPE (type);
11459 else if (is_base_type (type))
11460 mod_type_die = base_type_die (type, reverse);
11461 else
11463 gen_type_die (type, context_die);
11465 /* We have to get the type_main_variant here (and pass that to the
11466 `lookup_type_die' routine) because the ..._TYPE node we have
11467 might simply be a *copy* of some original type node (where the
11468 copy was created to help us keep track of typedef names) and
11469 that copy might have a different TYPE_UID from the original
11470 ..._TYPE node. */
11471 if (TREE_CODE (type) != VECTOR_TYPE
11472 && TREE_CODE (type) != ARRAY_TYPE)
11473 return lookup_type_die (type_main_variant (type));
11474 else
11475 /* Vectors have the debugging information in the type,
11476 not the main variant. */
11477 return lookup_type_die (type);
11480 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
11481 don't output a DW_TAG_typedef, since there isn't one in the
11482 user's program; just attach a DW_AT_name to the type.
11483 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11484 if the base type already has the same name. */
11485 if (name
11486 && ((TREE_CODE (name) != TYPE_DECL
11487 && (qualified_type == TYPE_MAIN_VARIANT (type)
11488 || (cv_quals == TYPE_UNQUALIFIED)))
11489 || (TREE_CODE (name) == TYPE_DECL
11490 && TREE_TYPE (name) == qualified_type
11491 && DECL_NAME (name))))
11493 if (TREE_CODE (name) == TYPE_DECL)
11494 /* Could just call add_name_and_src_coords_attributes here,
11495 but since this is a builtin type it doesn't have any
11496 useful source coordinates anyway. */
11497 name = DECL_NAME (name);
11498 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11500 /* This probably indicates a bug. */
11501 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11503 name = TYPE_IDENTIFIER (type);
11504 add_name_attribute (mod_type_die,
11505 name ? IDENTIFIER_POINTER (name) : "__unknown__");
11508 if (qualified_type)
11509 equate_type_number_to_die (qualified_type, mod_type_die);
11511 if (item_type)
11512 /* We must do this after the equate_type_number_to_die call, in case
11513 this is a recursive type. This ensures that the modified_type_die
11514 recursion will terminate even if the type is recursive. Recursive
11515 types are possible in Ada. */
11516 sub_die = modified_type_die (item_type,
11517 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11518 reverse,
11519 context_die);
11521 if (sub_die != NULL)
11522 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11524 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11525 if (TYPE_ARTIFICIAL (type))
11526 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11528 return mod_type_die;
11531 /* Generate DIEs for the generic parameters of T.
11532 T must be either a generic type or a generic function.
11533 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11535 static void
11536 gen_generic_params_dies (tree t)
11538 tree parms, args;
11539 int parms_num, i;
11540 dw_die_ref die = NULL;
11541 int non_default;
11543 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11544 return;
11546 if (TYPE_P (t))
11547 die = lookup_type_die (t);
11548 else if (DECL_P (t))
11549 die = lookup_decl_die (t);
11551 gcc_assert (die);
11553 parms = lang_hooks.get_innermost_generic_parms (t);
11554 if (!parms)
11555 /* T has no generic parameter. It means T is neither a generic type
11556 or function. End of story. */
11557 return;
11559 parms_num = TREE_VEC_LENGTH (parms);
11560 args = lang_hooks.get_innermost_generic_args (t);
11561 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11562 non_default = int_cst_value (TREE_CHAIN (args));
11563 else
11564 non_default = TREE_VEC_LENGTH (args);
11565 for (i = 0; i < parms_num; i++)
11567 tree parm, arg, arg_pack_elems;
11568 dw_die_ref parm_die;
11570 parm = TREE_VEC_ELT (parms, i);
11571 arg = TREE_VEC_ELT (args, i);
11572 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11573 gcc_assert (parm && TREE_VALUE (parm) && arg);
11575 if (parm && TREE_VALUE (parm) && arg)
11577 /* If PARM represents a template parameter pack,
11578 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11579 by DW_TAG_template_*_parameter DIEs for the argument
11580 pack elements of ARG. Note that ARG would then be
11581 an argument pack. */
11582 if (arg_pack_elems)
11583 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11584 arg_pack_elems,
11585 die);
11586 else
11587 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11588 true /* emit name */, die);
11589 if (i >= non_default)
11590 add_AT_flag (parm_die, DW_AT_default_value, 1);
11595 /* Create and return a DIE for PARM which should be
11596 the representation of a generic type parameter.
11597 For instance, in the C++ front end, PARM would be a template parameter.
11598 ARG is the argument to PARM.
11599 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11600 name of the PARM.
11601 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11602 as a child node. */
11604 static dw_die_ref
11605 generic_parameter_die (tree parm, tree arg,
11606 bool emit_name_p,
11607 dw_die_ref parent_die)
11609 dw_die_ref tmpl_die = NULL;
11610 const char *name = NULL;
11612 if (!parm || !DECL_NAME (parm) || !arg)
11613 return NULL;
11615 /* We support non-type generic parameters and arguments,
11616 type generic parameters and arguments, as well as
11617 generic generic parameters (a.k.a. template template parameters in C++)
11618 and arguments. */
11619 if (TREE_CODE (parm) == PARM_DECL)
11620 /* PARM is a nontype generic parameter */
11621 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11622 else if (TREE_CODE (parm) == TYPE_DECL)
11623 /* PARM is a type generic parameter. */
11624 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11625 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11626 /* PARM is a generic generic parameter.
11627 Its DIE is a GNU extension. It shall have a
11628 DW_AT_name attribute to represent the name of the template template
11629 parameter, and a DW_AT_GNU_template_name attribute to represent the
11630 name of the template template argument. */
11631 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11632 parent_die, parm);
11633 else
11634 gcc_unreachable ();
11636 if (tmpl_die)
11638 tree tmpl_type;
11640 /* If PARM is a generic parameter pack, it means we are
11641 emitting debug info for a template argument pack element.
11642 In other terms, ARG is a template argument pack element.
11643 In that case, we don't emit any DW_AT_name attribute for
11644 the die. */
11645 if (emit_name_p)
11647 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11648 gcc_assert (name);
11649 add_AT_string (tmpl_die, DW_AT_name, name);
11652 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11654 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11655 TMPL_DIE should have a child DW_AT_type attribute that is set
11656 to the type of the argument to PARM, which is ARG.
11657 If PARM is a type generic parameter, TMPL_DIE should have a
11658 child DW_AT_type that is set to ARG. */
11659 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11660 add_type_attribute (tmpl_die, tmpl_type,
11661 (TREE_THIS_VOLATILE (tmpl_type)
11662 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11663 false, parent_die);
11665 else
11667 /* So TMPL_DIE is a DIE representing a
11668 a generic generic template parameter, a.k.a template template
11669 parameter in C++ and arg is a template. */
11671 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11672 to the name of the argument. */
11673 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11674 if (name)
11675 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11678 if (TREE_CODE (parm) == PARM_DECL)
11679 /* So PARM is a non-type generic parameter.
11680 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11681 attribute of TMPL_DIE which value represents the value
11682 of ARG.
11683 We must be careful here:
11684 The value of ARG might reference some function decls.
11685 We might currently be emitting debug info for a generic
11686 type and types are emitted before function decls, we don't
11687 know if the function decls referenced by ARG will actually be
11688 emitted after cgraph computations.
11689 So must defer the generation of the DW_AT_const_value to
11690 after cgraph is ready. */
11691 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11694 return tmpl_die;
11697 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11698 PARM_PACK must be a template parameter pack. The returned DIE
11699 will be child DIE of PARENT_DIE. */
11701 static dw_die_ref
11702 template_parameter_pack_die (tree parm_pack,
11703 tree parm_pack_args,
11704 dw_die_ref parent_die)
11706 dw_die_ref die;
11707 int j;
11709 gcc_assert (parent_die && parm_pack);
11711 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11712 add_name_and_src_coords_attributes (die, parm_pack);
11713 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11714 generic_parameter_die (parm_pack,
11715 TREE_VEC_ELT (parm_pack_args, j),
11716 false /* Don't emit DW_AT_name */,
11717 die);
11718 return die;
11721 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11722 an enumerated type. */
11724 static inline int
11725 type_is_enum (const_tree type)
11727 return TREE_CODE (type) == ENUMERAL_TYPE;
11730 /* Return the DBX register number described by a given RTL node. */
11732 static unsigned int
11733 dbx_reg_number (const_rtx rtl)
11735 unsigned regno = REGNO (rtl);
11737 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11739 #ifdef LEAF_REG_REMAP
11740 if (crtl->uses_only_leaf_regs)
11742 int leaf_reg = LEAF_REG_REMAP (regno);
11743 if (leaf_reg != -1)
11744 regno = (unsigned) leaf_reg;
11746 #endif
11748 regno = DBX_REGISTER_NUMBER (regno);
11749 gcc_assert (regno != INVALID_REGNUM);
11750 return regno;
11753 /* Optionally add a DW_OP_piece term to a location description expression.
11754 DW_OP_piece is only added if the location description expression already
11755 doesn't end with DW_OP_piece. */
11757 static void
11758 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11760 dw_loc_descr_ref loc;
11762 if (*list_head != NULL)
11764 /* Find the end of the chain. */
11765 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11768 if (loc->dw_loc_opc != DW_OP_piece)
11769 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11773 /* Return a location descriptor that designates a machine register or
11774 zero if there is none. */
11776 static dw_loc_descr_ref
11777 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11779 rtx regs;
11781 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11782 return 0;
11784 /* We only use "frame base" when we're sure we're talking about the
11785 post-prologue local stack frame. We do this by *not* running
11786 register elimination until this point, and recognizing the special
11787 argument pointer and soft frame pointer rtx's.
11788 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11789 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11790 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11792 dw_loc_descr_ref result = NULL;
11794 if (dwarf_version >= 4 || !dwarf_strict)
11796 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11797 initialized);
11798 if (result)
11799 add_loc_descr (&result,
11800 new_loc_descr (DW_OP_stack_value, 0, 0));
11802 return result;
11805 regs = targetm.dwarf_register_span (rtl);
11807 if (REG_NREGS (rtl) > 1 || regs)
11808 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11809 else
11811 unsigned int dbx_regnum = dbx_reg_number (rtl);
11812 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11813 return 0;
11814 return one_reg_loc_descriptor (dbx_regnum, initialized);
11818 /* Return a location descriptor that designates a machine register for
11819 a given hard register number. */
11821 static dw_loc_descr_ref
11822 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11824 dw_loc_descr_ref reg_loc_descr;
11826 if (regno <= 31)
11827 reg_loc_descr
11828 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11829 else
11830 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11832 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11833 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11835 return reg_loc_descr;
11838 /* Given an RTL of a register, return a location descriptor that
11839 designates a value that spans more than one register. */
11841 static dw_loc_descr_ref
11842 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11843 enum var_init_status initialized)
11845 int size, i;
11846 dw_loc_descr_ref loc_result = NULL;
11848 /* Simple, contiguous registers. */
11849 if (regs == NULL_RTX)
11851 unsigned reg = REGNO (rtl);
11852 int nregs;
11854 #ifdef LEAF_REG_REMAP
11855 if (crtl->uses_only_leaf_regs)
11857 int leaf_reg = LEAF_REG_REMAP (reg);
11858 if (leaf_reg != -1)
11859 reg = (unsigned) leaf_reg;
11861 #endif
11863 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11864 nregs = REG_NREGS (rtl);
11866 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11868 loc_result = NULL;
11869 while (nregs--)
11871 dw_loc_descr_ref t;
11873 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11874 VAR_INIT_STATUS_INITIALIZED);
11875 add_loc_descr (&loc_result, t);
11876 add_loc_descr_op_piece (&loc_result, size);
11877 ++reg;
11879 return loc_result;
11882 /* Now onto stupid register sets in non contiguous locations. */
11884 gcc_assert (GET_CODE (regs) == PARALLEL);
11886 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11887 loc_result = NULL;
11889 for (i = 0; i < XVECLEN (regs, 0); ++i)
11891 dw_loc_descr_ref t;
11893 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11894 VAR_INIT_STATUS_INITIALIZED);
11895 add_loc_descr (&loc_result, t);
11896 add_loc_descr_op_piece (&loc_result, size);
11899 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11900 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11901 return loc_result;
11904 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11906 /* Return a location descriptor that designates a constant i,
11907 as a compound operation from constant (i >> shift), constant shift
11908 and DW_OP_shl. */
11910 static dw_loc_descr_ref
11911 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11913 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11914 add_loc_descr (&ret, int_loc_descriptor (shift));
11915 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11916 return ret;
11919 /* Return a location descriptor that designates a constant. */
11921 static dw_loc_descr_ref
11922 int_loc_descriptor (HOST_WIDE_INT i)
11924 enum dwarf_location_atom op;
11926 /* Pick the smallest representation of a constant, rather than just
11927 defaulting to the LEB encoding. */
11928 if (i >= 0)
11930 int clz = clz_hwi (i);
11931 int ctz = ctz_hwi (i);
11932 if (i <= 31)
11933 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11934 else if (i <= 0xff)
11935 op = DW_OP_const1u;
11936 else if (i <= 0xffff)
11937 op = DW_OP_const2u;
11938 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11939 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11940 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11941 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11942 while DW_OP_const4u is 5 bytes. */
11943 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11944 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11945 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11946 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11947 while DW_OP_const4u is 5 bytes. */
11948 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11950 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
11951 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
11952 <= 4)
11954 /* As i >= 2**31, the double cast above will yield a negative number.
11955 Since wrapping is defined in DWARF expressions we can output big
11956 positive integers as small negative ones, regardless of the size
11957 of host wide ints.
11959 Here, since the evaluator will handle 32-bit values and since i >=
11960 2**31, we know it's going to be interpreted as a negative literal:
11961 store it this way if we can do better than 5 bytes this way. */
11962 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
11964 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11965 op = DW_OP_const4u;
11967 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
11968 least 6 bytes: see if we can do better before falling back to it. */
11969 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11970 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11971 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
11972 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11973 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11974 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11975 >= HOST_BITS_PER_WIDE_INT)
11976 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11977 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
11978 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11979 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11980 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11981 && size_of_uleb128 (i) > 6)
11982 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11983 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11984 else
11985 op = DW_OP_constu;
11987 else
11989 if (i >= -0x80)
11990 op = DW_OP_const1s;
11991 else if (i >= -0x8000)
11992 op = DW_OP_const2s;
11993 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11995 if (size_of_int_loc_descriptor (i) < 5)
11997 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11998 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11999 return ret;
12001 op = DW_OP_const4s;
12003 else
12005 if (size_of_int_loc_descriptor (i)
12006 < (unsigned long) 1 + size_of_sleb128 (i))
12008 dw_loc_descr_ref ret = int_loc_descriptor (-i);
12009 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12010 return ret;
12012 op = DW_OP_consts;
12016 return new_loc_descr (op, i, 0);
12019 /* Likewise, for unsigned constants. */
12021 static dw_loc_descr_ref
12022 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12024 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12025 const unsigned HOST_WIDE_INT max_uint
12026 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12028 /* If possible, use the clever signed constants handling. */
12029 if (i <= max_int)
12030 return int_loc_descriptor ((HOST_WIDE_INT) i);
12032 /* Here, we are left with positive numbers that cannot be represented as
12033 HOST_WIDE_INT, i.e.:
12034 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12036 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12037 whereas may be better to output a negative integer: thanks to integer
12038 wrapping, we know that:
12039 x = x - 2 ** DWARF2_ADDR_SIZE
12040 = x - 2 * (max (HOST_WIDE_INT) + 1)
12041 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12042 small negative integers. Let's try that in cases it will clearly improve
12043 the encoding: there is no gain turning DW_OP_const4u into
12044 DW_OP_const4s. */
12045 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12046 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12047 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12049 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12051 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
12052 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
12053 const HOST_WIDE_INT second_shift
12054 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12056 /* So we finally have:
12057 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12058 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
12059 return int_loc_descriptor (second_shift);
12062 /* Last chance: fallback to a simple constant operation. */
12063 return new_loc_descr
12064 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12065 ? DW_OP_const4u
12066 : DW_OP_const8u,
12067 i, 0);
12070 /* Generate and return a location description that computes the unsigned
12071 comparison of the two stack top entries (a OP b where b is the top-most
12072 entry and a is the second one). The KIND of comparison can be LT_EXPR,
12073 LE_EXPR, GT_EXPR or GE_EXPR. */
12075 static dw_loc_descr_ref
12076 uint_comparison_loc_list (enum tree_code kind)
12078 enum dwarf_location_atom op, flip_op;
12079 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12081 switch (kind)
12083 case LT_EXPR:
12084 op = DW_OP_lt;
12085 break;
12086 case LE_EXPR:
12087 op = DW_OP_le;
12088 break;
12089 case GT_EXPR:
12090 op = DW_OP_gt;
12091 break;
12092 case GE_EXPR:
12093 op = DW_OP_ge;
12094 break;
12095 default:
12096 gcc_unreachable ();
12099 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12100 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12102 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
12103 possible to perform unsigned comparisons: we just have to distinguish
12104 three cases:
12106 1. when a and b have the same sign (as signed integers); then we should
12107 return: a OP(signed) b;
12109 2. when a is a negative signed integer while b is a positive one, then a
12110 is a greater unsigned integer than b; likewise when a and b's roles
12111 are flipped.
12113 So first, compare the sign of the two operands. */
12114 ret = new_loc_descr (DW_OP_over, 0, 0);
12115 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12116 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12117 /* If they have different signs (i.e. they have different sign bits), then
12118 the stack top value has now the sign bit set and thus it's smaller than
12119 zero. */
12120 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12121 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12122 add_loc_descr (&ret, bra_node);
12124 /* We are in case 1. At this point, we know both operands have the same
12125 sign, to it's safe to use the built-in signed comparison. */
12126 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12127 add_loc_descr (&ret, jmp_node);
12129 /* We are in case 2. Here, we know both operands do not have the same sign,
12130 so we have to flip the signed comparison. */
12131 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12132 tmp = new_loc_descr (flip_op, 0, 0);
12133 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12134 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12135 add_loc_descr (&ret, tmp);
12137 /* This dummy operation is necessary to make the two branches join. */
12138 tmp = new_loc_descr (DW_OP_nop, 0, 0);
12139 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12140 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12141 add_loc_descr (&ret, tmp);
12143 return ret;
12146 /* Likewise, but takes the location description lists (might be destructive on
12147 them). Return NULL if either is NULL or if concatenation fails. */
12149 static dw_loc_list_ref
12150 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12151 enum tree_code kind)
12153 if (left == NULL || right == NULL)
12154 return NULL;
12156 add_loc_list (&left, right);
12157 if (left == NULL)
12158 return NULL;
12160 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12161 return left;
12164 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12165 without actually allocating it. */
12167 static unsigned long
12168 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12170 return size_of_int_loc_descriptor (i >> shift)
12171 + size_of_int_loc_descriptor (shift)
12172 + 1;
12175 /* Return size_of_locs (int_loc_descriptor (i)) without
12176 actually allocating it. */
12178 static unsigned long
12179 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12181 unsigned long s;
12183 if (i >= 0)
12185 int clz, ctz;
12186 if (i <= 31)
12187 return 1;
12188 else if (i <= 0xff)
12189 return 2;
12190 else if (i <= 0xffff)
12191 return 3;
12192 clz = clz_hwi (i);
12193 ctz = ctz_hwi (i);
12194 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12195 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12196 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12197 - clz - 5);
12198 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12199 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12200 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12201 - clz - 8);
12202 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
12203 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
12204 <= 4)
12205 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
12206 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12207 return 5;
12208 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12209 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12210 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12211 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12212 - clz - 8);
12213 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12214 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12215 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12216 - clz - 16);
12217 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12218 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12219 && s > 6)
12220 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12221 - clz - 32);
12222 else
12223 return 1 + s;
12225 else
12227 if (i >= -0x80)
12228 return 2;
12229 else if (i >= -0x8000)
12230 return 3;
12231 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12233 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12235 s = size_of_int_loc_descriptor (-i) + 1;
12236 if (s < 5)
12237 return s;
12239 return 5;
12241 else
12243 unsigned long r = 1 + size_of_sleb128 (i);
12244 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12246 s = size_of_int_loc_descriptor (-i) + 1;
12247 if (s < r)
12248 return s;
12250 return r;
12255 /* Return loc description representing "address" of integer value.
12256 This can appear only as toplevel expression. */
12258 static dw_loc_descr_ref
12259 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12261 int litsize;
12262 dw_loc_descr_ref loc_result = NULL;
12264 if (!(dwarf_version >= 4 || !dwarf_strict))
12265 return NULL;
12267 litsize = size_of_int_loc_descriptor (i);
12268 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12269 is more compact. For DW_OP_stack_value we need:
12270 litsize + 1 (DW_OP_stack_value)
12271 and for DW_OP_implicit_value:
12272 1 (DW_OP_implicit_value) + 1 (length) + size. */
12273 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12275 loc_result = int_loc_descriptor (i);
12276 add_loc_descr (&loc_result,
12277 new_loc_descr (DW_OP_stack_value, 0, 0));
12278 return loc_result;
12281 loc_result = new_loc_descr (DW_OP_implicit_value,
12282 size, 0);
12283 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12284 loc_result->dw_loc_oprnd2.v.val_int = i;
12285 return loc_result;
12288 /* Return a location descriptor that designates a base+offset location. */
12290 static dw_loc_descr_ref
12291 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12292 enum var_init_status initialized)
12294 unsigned int regno;
12295 dw_loc_descr_ref result;
12296 dw_fde_ref fde = cfun->fde;
12298 /* We only use "frame base" when we're sure we're talking about the
12299 post-prologue local stack frame. We do this by *not* running
12300 register elimination until this point, and recognizing the special
12301 argument pointer and soft frame pointer rtx's. */
12302 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12304 rtx elim = (ira_use_lra_p
12305 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12306 : eliminate_regs (reg, VOIDmode, NULL_RTX));
12308 if (elim != reg)
12310 if (GET_CODE (elim) == PLUS)
12312 offset += INTVAL (XEXP (elim, 1));
12313 elim = XEXP (elim, 0);
12315 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12316 && (elim == hard_frame_pointer_rtx
12317 || elim == stack_pointer_rtx))
12318 || elim == (frame_pointer_needed
12319 ? hard_frame_pointer_rtx
12320 : stack_pointer_rtx));
12322 /* If drap register is used to align stack, use frame
12323 pointer + offset to access stack variables. If stack
12324 is aligned without drap, use stack pointer + offset to
12325 access stack variables. */
12326 if (crtl->stack_realign_tried
12327 && reg == frame_pointer_rtx)
12329 int base_reg
12330 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12331 ? HARD_FRAME_POINTER_REGNUM
12332 : REGNO (elim));
12333 return new_reg_loc_descr (base_reg, offset);
12336 gcc_assert (frame_pointer_fb_offset_valid);
12337 offset += frame_pointer_fb_offset;
12338 return new_loc_descr (DW_OP_fbreg, offset, 0);
12342 regno = REGNO (reg);
12343 #ifdef LEAF_REG_REMAP
12344 if (crtl->uses_only_leaf_regs)
12346 int leaf_reg = LEAF_REG_REMAP (regno);
12347 if (leaf_reg != -1)
12348 regno = (unsigned) leaf_reg;
12350 #endif
12351 regno = DWARF_FRAME_REGNUM (regno);
12353 if (!optimize && fde
12354 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12356 /* Use cfa+offset to represent the location of arguments passed
12357 on the stack when drap is used to align stack.
12358 Only do this when not optimizing, for optimized code var-tracking
12359 is supposed to track where the arguments live and the register
12360 used as vdrap or drap in some spot might be used for something
12361 else in other part of the routine. */
12362 return new_loc_descr (DW_OP_fbreg, offset, 0);
12365 if (regno <= 31)
12366 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12367 offset, 0);
12368 else
12369 result = new_loc_descr (DW_OP_bregx, regno, offset);
12371 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12372 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12374 return result;
12377 /* Return true if this RTL expression describes a base+offset calculation. */
12379 static inline int
12380 is_based_loc (const_rtx rtl)
12382 return (GET_CODE (rtl) == PLUS
12383 && ((REG_P (XEXP (rtl, 0))
12384 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12385 && CONST_INT_P (XEXP (rtl, 1)))));
12388 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12389 failed. */
12391 static dw_loc_descr_ref
12392 tls_mem_loc_descriptor (rtx mem)
12394 tree base;
12395 dw_loc_descr_ref loc_result;
12397 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12398 return NULL;
12400 base = get_base_address (MEM_EXPR (mem));
12401 if (base == NULL
12402 || !VAR_P (base)
12403 || !DECL_THREAD_LOCAL_P (base))
12404 return NULL;
12406 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12407 if (loc_result == NULL)
12408 return NULL;
12410 if (MEM_OFFSET (mem))
12411 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12413 return loc_result;
12416 /* Output debug info about reason why we failed to expand expression as dwarf
12417 expression. */
12419 static void
12420 expansion_failed (tree expr, rtx rtl, char const *reason)
12422 if (dump_file && (dump_flags & TDF_DETAILS))
12424 fprintf (dump_file, "Failed to expand as dwarf: ");
12425 if (expr)
12426 print_generic_expr (dump_file, expr, dump_flags);
12427 if (rtl)
12429 fprintf (dump_file, "\n");
12430 print_rtl (dump_file, rtl);
12432 fprintf (dump_file, "\nReason: %s\n", reason);
12436 /* Helper function for const_ok_for_output. */
12438 static bool
12439 const_ok_for_output_1 (rtx rtl)
12441 if (GET_CODE (rtl) == UNSPEC)
12443 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12444 we can't express it in the debug info. */
12445 /* Don't complain about TLS UNSPECs, those are just too hard to
12446 delegitimize. Note this could be a non-decl SYMBOL_REF such as
12447 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12448 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
12449 if (flag_checking
12450 && (XVECLEN (rtl, 0) == 0
12451 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12452 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12453 inform (current_function_decl
12454 ? DECL_SOURCE_LOCATION (current_function_decl)
12455 : UNKNOWN_LOCATION,
12456 #if NUM_UNSPEC_VALUES > 0
12457 "non-delegitimized UNSPEC %s (%d) found in variable location",
12458 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12459 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12460 XINT (rtl, 1));
12461 #else
12462 "non-delegitimized UNSPEC %d found in variable location",
12463 XINT (rtl, 1));
12464 #endif
12465 expansion_failed (NULL_TREE, rtl,
12466 "UNSPEC hasn't been delegitimized.\n");
12467 return false;
12470 if (targetm.const_not_ok_for_debug_p (rtl))
12472 expansion_failed (NULL_TREE, rtl,
12473 "Expression rejected for debug by the backend.\n");
12474 return false;
12477 /* FIXME: Refer to PR60655. It is possible for simplification
12478 of rtl expressions in var tracking to produce such expressions.
12479 We should really identify / validate expressions
12480 enclosed in CONST that can be handled by assemblers on various
12481 targets and only handle legitimate cases here. */
12482 if (GET_CODE (rtl) != SYMBOL_REF)
12484 if (GET_CODE (rtl) == NOT)
12485 return false;
12486 return true;
12489 if (CONSTANT_POOL_ADDRESS_P (rtl))
12491 bool marked;
12492 get_pool_constant_mark (rtl, &marked);
12493 /* If all references to this pool constant were optimized away,
12494 it was not output and thus we can't represent it. */
12495 if (!marked)
12497 expansion_failed (NULL_TREE, rtl,
12498 "Constant was removed from constant pool.\n");
12499 return false;
12503 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12504 return false;
12506 /* Avoid references to external symbols in debug info, on several targets
12507 the linker might even refuse to link when linking a shared library,
12508 and in many other cases the relocations for .debug_info/.debug_loc are
12509 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
12510 to be defined within the same shared library or executable are fine. */
12511 if (SYMBOL_REF_EXTERNAL_P (rtl))
12513 tree decl = SYMBOL_REF_DECL (rtl);
12515 if (decl == NULL || !targetm.binds_local_p (decl))
12517 expansion_failed (NULL_TREE, rtl,
12518 "Symbol not defined in current TU.\n");
12519 return false;
12523 return true;
12526 /* Return true if constant RTL can be emitted in DW_OP_addr or
12527 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
12528 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
12530 static bool
12531 const_ok_for_output (rtx rtl)
12533 if (GET_CODE (rtl) == SYMBOL_REF)
12534 return const_ok_for_output_1 (rtl);
12536 if (GET_CODE (rtl) == CONST)
12538 subrtx_var_iterator::array_type array;
12539 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12540 if (!const_ok_for_output_1 (*iter))
12541 return false;
12542 return true;
12545 return true;
12548 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12549 if possible, NULL otherwise. */
12551 static dw_die_ref
12552 base_type_for_mode (machine_mode mode, bool unsignedp)
12554 dw_die_ref type_die;
12555 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12557 if (type == NULL)
12558 return NULL;
12559 switch (TREE_CODE (type))
12561 case INTEGER_TYPE:
12562 case REAL_TYPE:
12563 break;
12564 default:
12565 return NULL;
12567 type_die = lookup_type_die (type);
12568 if (!type_die)
12569 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12570 comp_unit_die ());
12571 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12572 return NULL;
12573 return type_die;
12576 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12577 type matching MODE, or, if MODE is narrower than or as wide as
12578 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
12579 possible. */
12581 static dw_loc_descr_ref
12582 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12584 machine_mode outer_mode = mode;
12585 dw_die_ref type_die;
12586 dw_loc_descr_ref cvt;
12588 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12590 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12591 return op;
12593 type_die = base_type_for_mode (outer_mode, 1);
12594 if (type_die == NULL)
12595 return NULL;
12596 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12597 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12598 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12599 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12600 add_loc_descr (&op, cvt);
12601 return op;
12604 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
12606 static dw_loc_descr_ref
12607 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12608 dw_loc_descr_ref op1)
12610 dw_loc_descr_ref ret = op0;
12611 add_loc_descr (&ret, op1);
12612 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12613 if (STORE_FLAG_VALUE != 1)
12615 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12616 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12618 return ret;
12621 /* Return location descriptor for signed comparison OP RTL. */
12623 static dw_loc_descr_ref
12624 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12625 machine_mode mem_mode)
12627 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12628 dw_loc_descr_ref op0, op1;
12629 int shift;
12631 if (op_mode == VOIDmode)
12632 op_mode = GET_MODE (XEXP (rtl, 1));
12633 if (op_mode == VOIDmode)
12634 return NULL;
12636 if (dwarf_strict
12637 && (!SCALAR_INT_MODE_P (op_mode)
12638 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12639 return NULL;
12641 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12642 VAR_INIT_STATUS_INITIALIZED);
12643 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12644 VAR_INIT_STATUS_INITIALIZED);
12646 if (op0 == NULL || op1 == NULL)
12647 return NULL;
12649 if (!SCALAR_INT_MODE_P (op_mode)
12650 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12651 return compare_loc_descriptor (op, op0, op1);
12653 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12655 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12656 dw_loc_descr_ref cvt;
12658 if (type_die == NULL)
12659 return NULL;
12660 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12661 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12662 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12663 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12664 add_loc_descr (&op0, cvt);
12665 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12666 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12667 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12668 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12669 add_loc_descr (&op1, cvt);
12670 return compare_loc_descriptor (op, op0, op1);
12673 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12674 /* For eq/ne, if the operands are known to be zero-extended,
12675 there is no need to do the fancy shifting up. */
12676 if (op == DW_OP_eq || op == DW_OP_ne)
12678 dw_loc_descr_ref last0, last1;
12679 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12681 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12683 /* deref_size zero extends, and for constants we can check
12684 whether they are zero extended or not. */
12685 if (((last0->dw_loc_opc == DW_OP_deref_size
12686 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12687 || (CONST_INT_P (XEXP (rtl, 0))
12688 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12689 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12690 && ((last1->dw_loc_opc == DW_OP_deref_size
12691 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12692 || (CONST_INT_P (XEXP (rtl, 1))
12693 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12694 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12695 return compare_loc_descriptor (op, op0, op1);
12697 /* EQ/NE comparison against constant in narrower type than
12698 DWARF2_ADDR_SIZE can be performed either as
12699 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12700 DW_OP_{eq,ne}
12702 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12703 DW_OP_{eq,ne}. Pick whatever is shorter. */
12704 if (CONST_INT_P (XEXP (rtl, 1))
12705 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12706 && (size_of_int_loc_descriptor (shift) + 1
12707 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12708 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12709 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12710 & GET_MODE_MASK (op_mode))))
12712 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12713 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12714 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12715 & GET_MODE_MASK (op_mode));
12716 return compare_loc_descriptor (op, op0, op1);
12719 add_loc_descr (&op0, int_loc_descriptor (shift));
12720 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12721 if (CONST_INT_P (XEXP (rtl, 1)))
12722 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12723 else
12725 add_loc_descr (&op1, int_loc_descriptor (shift));
12726 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12728 return compare_loc_descriptor (op, op0, op1);
12731 /* Return location descriptor for unsigned comparison OP RTL. */
12733 static dw_loc_descr_ref
12734 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12735 machine_mode mem_mode)
12737 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12738 dw_loc_descr_ref op0, op1;
12740 if (op_mode == VOIDmode)
12741 op_mode = GET_MODE (XEXP (rtl, 1));
12742 if (op_mode == VOIDmode)
12743 return NULL;
12744 if (!SCALAR_INT_MODE_P (op_mode))
12745 return NULL;
12747 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12748 return NULL;
12750 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12751 VAR_INIT_STATUS_INITIALIZED);
12752 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12753 VAR_INIT_STATUS_INITIALIZED);
12755 if (op0 == NULL || op1 == NULL)
12756 return NULL;
12758 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12760 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12761 dw_loc_descr_ref last0, last1;
12762 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12764 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12766 if (CONST_INT_P (XEXP (rtl, 0)))
12767 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12768 /* deref_size zero extends, so no need to mask it again. */
12769 else if (last0->dw_loc_opc != DW_OP_deref_size
12770 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12772 add_loc_descr (&op0, int_loc_descriptor (mask));
12773 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12775 if (CONST_INT_P (XEXP (rtl, 1)))
12776 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12777 /* deref_size zero extends, so no need to mask it again. */
12778 else if (last1->dw_loc_opc != DW_OP_deref_size
12779 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12781 add_loc_descr (&op1, int_loc_descriptor (mask));
12782 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12785 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12787 HOST_WIDE_INT bias = 1;
12788 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12789 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12790 if (CONST_INT_P (XEXP (rtl, 1)))
12791 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12792 + INTVAL (XEXP (rtl, 1)));
12793 else
12794 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12795 bias, 0));
12797 return compare_loc_descriptor (op, op0, op1);
12800 /* Return location descriptor for {U,S}{MIN,MAX}. */
12802 static dw_loc_descr_ref
12803 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12804 machine_mode mem_mode)
12806 enum dwarf_location_atom op;
12807 dw_loc_descr_ref op0, op1, ret;
12808 dw_loc_descr_ref bra_node, drop_node;
12810 if (dwarf_strict
12811 && (!SCALAR_INT_MODE_P (mode)
12812 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12813 return NULL;
12815 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12816 VAR_INIT_STATUS_INITIALIZED);
12817 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12818 VAR_INIT_STATUS_INITIALIZED);
12820 if (op0 == NULL || op1 == NULL)
12821 return NULL;
12823 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12824 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12825 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12826 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12828 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12830 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12831 add_loc_descr (&op0, int_loc_descriptor (mask));
12832 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12833 add_loc_descr (&op1, int_loc_descriptor (mask));
12834 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12836 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12838 HOST_WIDE_INT bias = 1;
12839 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12840 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12841 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12844 else if (!SCALAR_INT_MODE_P (mode)
12845 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12847 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12848 add_loc_descr (&op0, int_loc_descriptor (shift));
12849 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12850 add_loc_descr (&op1, int_loc_descriptor (shift));
12851 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12853 else if (SCALAR_INT_MODE_P (mode)
12854 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12856 dw_die_ref type_die = base_type_for_mode (mode, 0);
12857 dw_loc_descr_ref cvt;
12858 if (type_die == NULL)
12859 return NULL;
12860 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12861 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12862 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12863 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12864 add_loc_descr (&op0, cvt);
12865 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12866 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12867 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12868 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12869 add_loc_descr (&op1, cvt);
12872 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12873 op = DW_OP_lt;
12874 else
12875 op = DW_OP_gt;
12876 ret = op0;
12877 add_loc_descr (&ret, op1);
12878 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12879 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12880 add_loc_descr (&ret, bra_node);
12881 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12882 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12883 add_loc_descr (&ret, drop_node);
12884 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12885 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12886 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12887 && SCALAR_INT_MODE_P (mode)
12888 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12889 ret = convert_descriptor_to_mode (mode, ret);
12890 return ret;
12893 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12894 but after converting arguments to type_die, afterwards
12895 convert back to unsigned. */
12897 static dw_loc_descr_ref
12898 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12899 machine_mode mode, machine_mode mem_mode)
12901 dw_loc_descr_ref cvt, op0, op1;
12903 if (type_die == NULL)
12904 return NULL;
12905 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12906 VAR_INIT_STATUS_INITIALIZED);
12907 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12908 VAR_INIT_STATUS_INITIALIZED);
12909 if (op0 == NULL || op1 == NULL)
12910 return NULL;
12911 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12912 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12913 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12914 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12915 add_loc_descr (&op0, cvt);
12916 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12917 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12918 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12919 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12920 add_loc_descr (&op1, cvt);
12921 add_loc_descr (&op0, op1);
12922 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12923 return convert_descriptor_to_mode (mode, op0);
12926 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12927 const0 is DW_OP_lit0 or corresponding typed constant,
12928 const1 is DW_OP_lit1 or corresponding typed constant
12929 and constMSB is constant with just the MSB bit set
12930 for the mode):
12931 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12932 L1: const0 DW_OP_swap
12933 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12934 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12935 L3: DW_OP_drop
12936 L4: DW_OP_nop
12938 CTZ is similar:
12939 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12940 L1: const0 DW_OP_swap
12941 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12942 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12943 L3: DW_OP_drop
12944 L4: DW_OP_nop
12946 FFS is similar:
12947 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12948 L1: const1 DW_OP_swap
12949 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12950 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12951 L3: DW_OP_drop
12952 L4: DW_OP_nop */
12954 static dw_loc_descr_ref
12955 clz_loc_descriptor (rtx rtl, machine_mode mode,
12956 machine_mode mem_mode)
12958 dw_loc_descr_ref op0, ret, tmp;
12959 HOST_WIDE_INT valv;
12960 dw_loc_descr_ref l1jump, l1label;
12961 dw_loc_descr_ref l2jump, l2label;
12962 dw_loc_descr_ref l3jump, l3label;
12963 dw_loc_descr_ref l4jump, l4label;
12964 rtx msb;
12966 if (!SCALAR_INT_MODE_P (mode)
12967 || GET_MODE (XEXP (rtl, 0)) != mode)
12968 return NULL;
12970 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12971 VAR_INIT_STATUS_INITIALIZED);
12972 if (op0 == NULL)
12973 return NULL;
12974 ret = op0;
12975 if (GET_CODE (rtl) == CLZ)
12977 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12978 valv = GET_MODE_BITSIZE (mode);
12980 else if (GET_CODE (rtl) == FFS)
12981 valv = 0;
12982 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12983 valv = GET_MODE_BITSIZE (mode);
12984 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12985 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12986 add_loc_descr (&ret, l1jump);
12987 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12988 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12989 VAR_INIT_STATUS_INITIALIZED);
12990 if (tmp == NULL)
12991 return NULL;
12992 add_loc_descr (&ret, tmp);
12993 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12994 add_loc_descr (&ret, l4jump);
12995 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12996 ? const1_rtx : const0_rtx,
12997 mode, mem_mode,
12998 VAR_INIT_STATUS_INITIALIZED);
12999 if (l1label == NULL)
13000 return NULL;
13001 add_loc_descr (&ret, l1label);
13002 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13003 l2label = new_loc_descr (DW_OP_dup, 0, 0);
13004 add_loc_descr (&ret, l2label);
13005 if (GET_CODE (rtl) != CLZ)
13006 msb = const1_rtx;
13007 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
13008 msb = GEN_INT (HOST_WIDE_INT_1U
13009 << (GET_MODE_BITSIZE (mode) - 1));
13010 else
13011 msb = immed_wide_int_const
13012 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
13013 GET_MODE_PRECISION (mode)), mode);
13014 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
13015 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13016 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
13017 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
13018 else
13019 tmp = mem_loc_descriptor (msb, mode, mem_mode,
13020 VAR_INIT_STATUS_INITIALIZED);
13021 if (tmp == NULL)
13022 return NULL;
13023 add_loc_descr (&ret, tmp);
13024 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13025 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13026 add_loc_descr (&ret, l3jump);
13027 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13028 VAR_INIT_STATUS_INITIALIZED);
13029 if (tmp == NULL)
13030 return NULL;
13031 add_loc_descr (&ret, tmp);
13032 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13033 ? DW_OP_shl : DW_OP_shr, 0, 0));
13034 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13035 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13036 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13037 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13038 add_loc_descr (&ret, l2jump);
13039 l3label = new_loc_descr (DW_OP_drop, 0, 0);
13040 add_loc_descr (&ret, l3label);
13041 l4label = new_loc_descr (DW_OP_nop, 0, 0);
13042 add_loc_descr (&ret, l4label);
13043 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13044 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13045 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13046 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13047 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13048 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13049 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13050 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13051 return ret;
13054 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13055 const1 is DW_OP_lit1 or corresponding typed constant):
13056 const0 DW_OP_swap
13057 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13058 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13059 L2: DW_OP_drop
13061 PARITY is similar:
13062 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13063 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13064 L2: DW_OP_drop */
13066 static dw_loc_descr_ref
13067 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13068 machine_mode mem_mode)
13070 dw_loc_descr_ref op0, ret, tmp;
13071 dw_loc_descr_ref l1jump, l1label;
13072 dw_loc_descr_ref l2jump, l2label;
13074 if (!SCALAR_INT_MODE_P (mode)
13075 || GET_MODE (XEXP (rtl, 0)) != mode)
13076 return NULL;
13078 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13079 VAR_INIT_STATUS_INITIALIZED);
13080 if (op0 == NULL)
13081 return NULL;
13082 ret = op0;
13083 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13084 VAR_INIT_STATUS_INITIALIZED);
13085 if (tmp == NULL)
13086 return NULL;
13087 add_loc_descr (&ret, tmp);
13088 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13089 l1label = new_loc_descr (DW_OP_dup, 0, 0);
13090 add_loc_descr (&ret, l1label);
13091 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13092 add_loc_descr (&ret, l2jump);
13093 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13094 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13095 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13096 VAR_INIT_STATUS_INITIALIZED);
13097 if (tmp == NULL)
13098 return NULL;
13099 add_loc_descr (&ret, tmp);
13100 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13101 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13102 ? DW_OP_plus : DW_OP_xor, 0, 0));
13103 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13104 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13105 VAR_INIT_STATUS_INITIALIZED);
13106 add_loc_descr (&ret, tmp);
13107 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13108 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13109 add_loc_descr (&ret, l1jump);
13110 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13111 add_loc_descr (&ret, l2label);
13112 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13113 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13114 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13115 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13116 return ret;
13119 /* BSWAP (constS is initial shift count, either 56 or 24):
13120 constS const0
13121 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13122 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13123 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13124 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13125 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
13127 static dw_loc_descr_ref
13128 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13129 machine_mode mem_mode)
13131 dw_loc_descr_ref op0, ret, tmp;
13132 dw_loc_descr_ref l1jump, l1label;
13133 dw_loc_descr_ref l2jump, l2label;
13135 if (!SCALAR_INT_MODE_P (mode)
13136 || BITS_PER_UNIT != 8
13137 || (GET_MODE_BITSIZE (mode) != 32
13138 && GET_MODE_BITSIZE (mode) != 64))
13139 return NULL;
13141 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13142 VAR_INIT_STATUS_INITIALIZED);
13143 if (op0 == NULL)
13144 return NULL;
13146 ret = op0;
13147 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13148 mode, mem_mode,
13149 VAR_INIT_STATUS_INITIALIZED);
13150 if (tmp == NULL)
13151 return NULL;
13152 add_loc_descr (&ret, tmp);
13153 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13154 VAR_INIT_STATUS_INITIALIZED);
13155 if (tmp == NULL)
13156 return NULL;
13157 add_loc_descr (&ret, tmp);
13158 l1label = new_loc_descr (DW_OP_pick, 2, 0);
13159 add_loc_descr (&ret, l1label);
13160 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13161 mode, mem_mode,
13162 VAR_INIT_STATUS_INITIALIZED);
13163 add_loc_descr (&ret, tmp);
13164 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13165 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13166 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13167 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13168 VAR_INIT_STATUS_INITIALIZED);
13169 if (tmp == NULL)
13170 return NULL;
13171 add_loc_descr (&ret, tmp);
13172 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13173 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13174 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13175 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13176 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13177 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13178 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13179 VAR_INIT_STATUS_INITIALIZED);
13180 add_loc_descr (&ret, tmp);
13181 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13182 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13183 add_loc_descr (&ret, l2jump);
13184 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13185 VAR_INIT_STATUS_INITIALIZED);
13186 add_loc_descr (&ret, tmp);
13187 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13188 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13189 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13190 add_loc_descr (&ret, l1jump);
13191 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13192 add_loc_descr (&ret, l2label);
13193 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13194 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13195 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13196 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13197 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13198 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13199 return ret;
13202 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13203 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13204 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13205 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13207 ROTATERT is similar:
13208 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13209 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13210 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
13212 static dw_loc_descr_ref
13213 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13214 machine_mode mem_mode)
13216 rtx rtlop1 = XEXP (rtl, 1);
13217 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13218 int i;
13220 if (!SCALAR_INT_MODE_P (mode))
13221 return NULL;
13223 if (GET_MODE (rtlop1) != VOIDmode
13224 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13225 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13226 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13227 VAR_INIT_STATUS_INITIALIZED);
13228 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13229 VAR_INIT_STATUS_INITIALIZED);
13230 if (op0 == NULL || op1 == NULL)
13231 return NULL;
13232 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13233 for (i = 0; i < 2; i++)
13235 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13236 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13237 mode, mem_mode,
13238 VAR_INIT_STATUS_INITIALIZED);
13239 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13240 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13241 ? DW_OP_const4u
13242 : HOST_BITS_PER_WIDE_INT == 64
13243 ? DW_OP_const8u : DW_OP_constu,
13244 GET_MODE_MASK (mode), 0);
13245 else
13246 mask[i] = NULL;
13247 if (mask[i] == NULL)
13248 return NULL;
13249 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13251 ret = op0;
13252 add_loc_descr (&ret, op1);
13253 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13254 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13255 if (GET_CODE (rtl) == ROTATERT)
13257 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13258 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13259 GET_MODE_BITSIZE (mode), 0));
13261 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13262 if (mask[0] != NULL)
13263 add_loc_descr (&ret, mask[0]);
13264 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13265 if (mask[1] != NULL)
13267 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13268 add_loc_descr (&ret, mask[1]);
13269 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13271 if (GET_CODE (rtl) == ROTATE)
13273 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13274 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13275 GET_MODE_BITSIZE (mode), 0));
13277 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13278 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13279 return ret;
13282 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
13283 for DEBUG_PARAMETER_REF RTL. */
13285 static dw_loc_descr_ref
13286 parameter_ref_descriptor (rtx rtl)
13288 dw_loc_descr_ref ret;
13289 dw_die_ref ref;
13291 if (dwarf_strict)
13292 return NULL;
13293 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13294 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13295 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13296 if (ref)
13298 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13299 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13300 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13302 else
13304 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13305 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13307 return ret;
13310 /* The following routine converts the RTL for a variable or parameter
13311 (resident in memory) into an equivalent Dwarf representation of a
13312 mechanism for getting the address of that same variable onto the top of a
13313 hypothetical "address evaluation" stack.
13315 When creating memory location descriptors, we are effectively transforming
13316 the RTL for a memory-resident object into its Dwarf postfix expression
13317 equivalent. This routine recursively descends an RTL tree, turning
13318 it into Dwarf postfix code as it goes.
13320 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13322 MEM_MODE is the mode of the memory reference, needed to handle some
13323 autoincrement addressing modes.
13325 Return 0 if we can't represent the location. */
13327 dw_loc_descr_ref
13328 mem_loc_descriptor (rtx rtl, machine_mode mode,
13329 machine_mode mem_mode,
13330 enum var_init_status initialized)
13332 dw_loc_descr_ref mem_loc_result = NULL;
13333 enum dwarf_location_atom op;
13334 dw_loc_descr_ref op0, op1;
13335 rtx inner = NULL_RTX;
13337 if (mode == VOIDmode)
13338 mode = GET_MODE (rtl);
13340 /* Note that for a dynamically sized array, the location we will generate a
13341 description of here will be the lowest numbered location which is
13342 actually within the array. That's *not* necessarily the same as the
13343 zeroth element of the array. */
13345 rtl = targetm.delegitimize_address (rtl);
13347 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13348 return NULL;
13350 switch (GET_CODE (rtl))
13352 case POST_INC:
13353 case POST_DEC:
13354 case POST_MODIFY:
13355 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13357 case SUBREG:
13358 /* The case of a subreg may arise when we have a local (register)
13359 variable or a formal (register) parameter which doesn't quite fill
13360 up an entire register. For now, just assume that it is
13361 legitimate to make the Dwarf info refer to the whole register which
13362 contains the given subreg. */
13363 if (!subreg_lowpart_p (rtl))
13364 break;
13365 inner = SUBREG_REG (rtl);
13366 /* FALLTHRU */
13367 case TRUNCATE:
13368 if (inner == NULL_RTX)
13369 inner = XEXP (rtl, 0);
13370 if (SCALAR_INT_MODE_P (mode)
13371 && SCALAR_INT_MODE_P (GET_MODE (inner))
13372 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13373 #ifdef POINTERS_EXTEND_UNSIGNED
13374 || (mode == Pmode && mem_mode != VOIDmode)
13375 #endif
13377 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13379 mem_loc_result = mem_loc_descriptor (inner,
13380 GET_MODE (inner),
13381 mem_mode, initialized);
13382 break;
13384 if (dwarf_strict)
13385 break;
13386 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13387 break;
13388 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13389 && (!SCALAR_INT_MODE_P (mode)
13390 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
13391 break;
13392 else
13394 dw_die_ref type_die;
13395 dw_loc_descr_ref cvt;
13397 mem_loc_result = mem_loc_descriptor (inner,
13398 GET_MODE (inner),
13399 mem_mode, initialized);
13400 if (mem_loc_result == NULL)
13401 break;
13402 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13403 if (type_die == NULL)
13405 mem_loc_result = NULL;
13406 break;
13408 if (GET_MODE_SIZE (mode)
13409 != GET_MODE_SIZE (GET_MODE (inner)))
13410 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13411 else
13412 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13413 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13414 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13415 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13416 add_loc_descr (&mem_loc_result, cvt);
13417 if (SCALAR_INT_MODE_P (mode)
13418 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13420 /* Convert it to untyped afterwards. */
13421 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13422 add_loc_descr (&mem_loc_result, cvt);
13425 break;
13427 case REG:
13428 if (! SCALAR_INT_MODE_P (mode)
13429 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13430 && rtl != arg_pointer_rtx
13431 && rtl != frame_pointer_rtx
13432 #ifdef POINTERS_EXTEND_UNSIGNED
13433 && (mode != Pmode || mem_mode == VOIDmode)
13434 #endif
13437 dw_die_ref type_die;
13438 unsigned int dbx_regnum;
13440 if (dwarf_strict)
13441 break;
13442 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13443 break;
13444 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13445 if (type_die == NULL)
13446 break;
13448 dbx_regnum = dbx_reg_number (rtl);
13449 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13450 break;
13451 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13452 dbx_regnum, 0);
13453 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13454 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13455 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13456 break;
13458 /* Whenever a register number forms a part of the description of the
13459 method for calculating the (dynamic) address of a memory resident
13460 object, DWARF rules require the register number be referred to as
13461 a "base register". This distinction is not based in any way upon
13462 what category of register the hardware believes the given register
13463 belongs to. This is strictly DWARF terminology we're dealing with
13464 here. Note that in cases where the location of a memory-resident
13465 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13466 OP_CONST (0)) the actual DWARF location descriptor that we generate
13467 may just be OP_BASEREG (basereg). This may look deceptively like
13468 the object in question was allocated to a register (rather than in
13469 memory) so DWARF consumers need to be aware of the subtle
13470 distinction between OP_REG and OP_BASEREG. */
13471 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13472 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13473 else if (stack_realign_drap
13474 && crtl->drap_reg
13475 && crtl->args.internal_arg_pointer == rtl
13476 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13478 /* If RTL is internal_arg_pointer, which has been optimized
13479 out, use DRAP instead. */
13480 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13481 VAR_INIT_STATUS_INITIALIZED);
13483 break;
13485 case SIGN_EXTEND:
13486 case ZERO_EXTEND:
13487 if (!SCALAR_INT_MODE_P (mode))
13488 break;
13489 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13490 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13491 if (op0 == 0)
13492 break;
13493 else if (GET_CODE (rtl) == ZERO_EXTEND
13494 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13495 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13496 < HOST_BITS_PER_WIDE_INT
13497 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13498 to expand zero extend as two shifts instead of
13499 masking. */
13500 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13502 machine_mode imode = GET_MODE (XEXP (rtl, 0));
13503 mem_loc_result = op0;
13504 add_loc_descr (&mem_loc_result,
13505 int_loc_descriptor (GET_MODE_MASK (imode)));
13506 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13508 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13510 int shift = DWARF2_ADDR_SIZE
13511 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13512 shift *= BITS_PER_UNIT;
13513 if (GET_CODE (rtl) == SIGN_EXTEND)
13514 op = DW_OP_shra;
13515 else
13516 op = DW_OP_shr;
13517 mem_loc_result = op0;
13518 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13519 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13520 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13521 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13523 else if (!dwarf_strict)
13525 dw_die_ref type_die1, type_die2;
13526 dw_loc_descr_ref cvt;
13528 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13529 GET_CODE (rtl) == ZERO_EXTEND);
13530 if (type_die1 == NULL)
13531 break;
13532 type_die2 = base_type_for_mode (mode, 1);
13533 if (type_die2 == NULL)
13534 break;
13535 mem_loc_result = op0;
13536 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13537 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13538 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13539 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13540 add_loc_descr (&mem_loc_result, cvt);
13541 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13542 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13543 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13544 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13545 add_loc_descr (&mem_loc_result, cvt);
13547 break;
13549 case MEM:
13551 rtx new_rtl = avoid_constant_pool_reference (rtl);
13552 if (new_rtl != rtl)
13554 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13555 initialized);
13556 if (mem_loc_result != NULL)
13557 return mem_loc_result;
13560 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13561 get_address_mode (rtl), mode,
13562 VAR_INIT_STATUS_INITIALIZED);
13563 if (mem_loc_result == NULL)
13564 mem_loc_result = tls_mem_loc_descriptor (rtl);
13565 if (mem_loc_result != NULL)
13567 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13568 || !SCALAR_INT_MODE_P(mode))
13570 dw_die_ref type_die;
13571 dw_loc_descr_ref deref;
13573 if (dwarf_strict)
13574 return NULL;
13575 type_die
13576 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13577 if (type_die == NULL)
13578 return NULL;
13579 deref = new_loc_descr (DW_OP_GNU_deref_type,
13580 GET_MODE_SIZE (mode), 0);
13581 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13582 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13583 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13584 add_loc_descr (&mem_loc_result, deref);
13586 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13587 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13588 else
13589 add_loc_descr (&mem_loc_result,
13590 new_loc_descr (DW_OP_deref_size,
13591 GET_MODE_SIZE (mode), 0));
13593 break;
13595 case LO_SUM:
13596 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13598 case LABEL_REF:
13599 /* Some ports can transform a symbol ref into a label ref, because
13600 the symbol ref is too far away and has to be dumped into a constant
13601 pool. */
13602 case CONST:
13603 case SYMBOL_REF:
13604 if (!SCALAR_INT_MODE_P (mode)
13605 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13606 #ifdef POINTERS_EXTEND_UNSIGNED
13607 && (mode != Pmode || mem_mode == VOIDmode)
13608 #endif
13610 break;
13611 if (GET_CODE (rtl) == SYMBOL_REF
13612 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13614 dw_loc_descr_ref temp;
13616 /* If this is not defined, we have no way to emit the data. */
13617 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13618 break;
13620 temp = new_addr_loc_descr (rtl, dtprel_true);
13622 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13623 add_loc_descr (&mem_loc_result, temp);
13625 break;
13628 if (!const_ok_for_output (rtl))
13630 if (GET_CODE (rtl) == CONST)
13631 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13632 initialized);
13633 break;
13636 symref:
13637 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13638 vec_safe_push (used_rtx_array, rtl);
13639 break;
13641 case CONCAT:
13642 case CONCATN:
13643 case VAR_LOCATION:
13644 case DEBUG_IMPLICIT_PTR:
13645 expansion_failed (NULL_TREE, rtl,
13646 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13647 return 0;
13649 case ENTRY_VALUE:
13650 if (dwarf_strict)
13651 return NULL;
13652 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13654 if (!SCALAR_INT_MODE_P (mode)
13655 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13656 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13657 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13658 else
13660 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13661 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13662 return NULL;
13663 op0 = one_reg_loc_descriptor (dbx_regnum,
13664 VAR_INIT_STATUS_INITIALIZED);
13667 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13668 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13670 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13671 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13672 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13673 return NULL;
13675 else
13676 gcc_unreachable ();
13677 if (op0 == NULL)
13678 return NULL;
13679 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13680 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13681 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13682 break;
13684 case DEBUG_PARAMETER_REF:
13685 mem_loc_result = parameter_ref_descriptor (rtl);
13686 break;
13688 case PRE_MODIFY:
13689 /* Extract the PLUS expression nested inside and fall into
13690 PLUS code below. */
13691 rtl = XEXP (rtl, 1);
13692 goto plus;
13694 case PRE_INC:
13695 case PRE_DEC:
13696 /* Turn these into a PLUS expression and fall into the PLUS code
13697 below. */
13698 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13699 gen_int_mode (GET_CODE (rtl) == PRE_INC
13700 ? GET_MODE_UNIT_SIZE (mem_mode)
13701 : -GET_MODE_UNIT_SIZE (mem_mode),
13702 mode));
13704 /* fall through */
13706 case PLUS:
13707 plus:
13708 if (is_based_loc (rtl)
13709 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13710 || XEXP (rtl, 0) == arg_pointer_rtx
13711 || XEXP (rtl, 0) == frame_pointer_rtx)
13712 && SCALAR_INT_MODE_P (mode))
13713 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13714 INTVAL (XEXP (rtl, 1)),
13715 VAR_INIT_STATUS_INITIALIZED);
13716 else
13718 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13719 VAR_INIT_STATUS_INITIALIZED);
13720 if (mem_loc_result == 0)
13721 break;
13723 if (CONST_INT_P (XEXP (rtl, 1))
13724 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13725 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13726 else
13728 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13729 VAR_INIT_STATUS_INITIALIZED);
13730 if (op1 == 0)
13731 return NULL;
13732 add_loc_descr (&mem_loc_result, op1);
13733 add_loc_descr (&mem_loc_result,
13734 new_loc_descr (DW_OP_plus, 0, 0));
13737 break;
13739 /* If a pseudo-reg is optimized away, it is possible for it to
13740 be replaced with a MEM containing a multiply or shift. */
13741 case MINUS:
13742 op = DW_OP_minus;
13743 goto do_binop;
13745 case MULT:
13746 op = DW_OP_mul;
13747 goto do_binop;
13749 case DIV:
13750 if (!dwarf_strict
13751 && SCALAR_INT_MODE_P (mode)
13752 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13754 mem_loc_result = typed_binop (DW_OP_div, rtl,
13755 base_type_for_mode (mode, 0),
13756 mode, mem_mode);
13757 break;
13759 op = DW_OP_div;
13760 goto do_binop;
13762 case UMOD:
13763 op = DW_OP_mod;
13764 goto do_binop;
13766 case ASHIFT:
13767 op = DW_OP_shl;
13768 goto do_shift;
13770 case ASHIFTRT:
13771 op = DW_OP_shra;
13772 goto do_shift;
13774 case LSHIFTRT:
13775 op = DW_OP_shr;
13776 goto do_shift;
13778 do_shift:
13779 if (!SCALAR_INT_MODE_P (mode))
13780 break;
13781 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13782 VAR_INIT_STATUS_INITIALIZED);
13784 rtx rtlop1 = XEXP (rtl, 1);
13785 if (GET_MODE (rtlop1) != VOIDmode
13786 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13787 < GET_MODE_BITSIZE (mode))
13788 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13789 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13790 VAR_INIT_STATUS_INITIALIZED);
13793 if (op0 == 0 || op1 == 0)
13794 break;
13796 mem_loc_result = op0;
13797 add_loc_descr (&mem_loc_result, op1);
13798 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13799 break;
13801 case AND:
13802 op = DW_OP_and;
13803 goto do_binop;
13805 case IOR:
13806 op = DW_OP_or;
13807 goto do_binop;
13809 case XOR:
13810 op = DW_OP_xor;
13811 goto do_binop;
13813 do_binop:
13814 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13815 VAR_INIT_STATUS_INITIALIZED);
13816 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13817 VAR_INIT_STATUS_INITIALIZED);
13819 if (op0 == 0 || op1 == 0)
13820 break;
13822 mem_loc_result = op0;
13823 add_loc_descr (&mem_loc_result, op1);
13824 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13825 break;
13827 case MOD:
13828 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13830 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13831 base_type_for_mode (mode, 0),
13832 mode, mem_mode);
13833 break;
13836 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13837 VAR_INIT_STATUS_INITIALIZED);
13838 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13839 VAR_INIT_STATUS_INITIALIZED);
13841 if (op0 == 0 || op1 == 0)
13842 break;
13844 mem_loc_result = op0;
13845 add_loc_descr (&mem_loc_result, op1);
13846 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13847 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13848 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13849 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13850 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13851 break;
13853 case UDIV:
13854 if (!dwarf_strict && SCALAR_INT_MODE_P (mode))
13856 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13858 op = DW_OP_div;
13859 goto do_binop;
13861 mem_loc_result = typed_binop (DW_OP_div, rtl,
13862 base_type_for_mode (mode, 1),
13863 mode, mem_mode);
13865 break;
13867 case NOT:
13868 op = DW_OP_not;
13869 goto do_unop;
13871 case ABS:
13872 op = DW_OP_abs;
13873 goto do_unop;
13875 case NEG:
13876 op = DW_OP_neg;
13877 goto do_unop;
13879 do_unop:
13880 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13881 VAR_INIT_STATUS_INITIALIZED);
13883 if (op0 == 0)
13884 break;
13886 mem_loc_result = op0;
13887 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13888 break;
13890 case CONST_INT:
13891 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13892 #ifdef POINTERS_EXTEND_UNSIGNED
13893 || (mode == Pmode
13894 && mem_mode != VOIDmode
13895 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13896 #endif
13899 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13900 break;
13902 if (!dwarf_strict
13903 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13904 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13906 dw_die_ref type_die = base_type_for_mode (mode, 1);
13907 machine_mode amode;
13908 if (type_die == NULL)
13909 return NULL;
13910 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13911 MODE_INT, 0);
13912 if (INTVAL (rtl) >= 0
13913 && amode != BLKmode
13914 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13915 /* const DW_OP_GNU_convert <XXX> vs.
13916 DW_OP_GNU_const_type <XXX, 1, const>. */
13917 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13918 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13920 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13921 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13922 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13923 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13924 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13925 add_loc_descr (&mem_loc_result, op0);
13926 return mem_loc_result;
13928 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13929 INTVAL (rtl));
13930 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13931 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13932 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13933 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13934 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13935 else
13937 mem_loc_result->dw_loc_oprnd2.val_class
13938 = dw_val_class_const_double;
13939 mem_loc_result->dw_loc_oprnd2.v.val_double
13940 = double_int::from_shwi (INTVAL (rtl));
13943 break;
13945 case CONST_DOUBLE:
13946 if (!dwarf_strict)
13948 dw_die_ref type_die;
13950 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13951 CONST_DOUBLE rtx could represent either a large integer
13952 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13953 the value is always a floating point constant.
13955 When it is an integer, a CONST_DOUBLE is used whenever
13956 the constant requires 2 HWIs to be adequately represented.
13957 We output CONST_DOUBLEs as blocks. */
13958 if (mode == VOIDmode
13959 || (GET_MODE (rtl) == VOIDmode
13960 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13961 break;
13962 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13963 if (type_die == NULL)
13964 return NULL;
13965 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13966 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13967 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13968 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13969 #if TARGET_SUPPORTS_WIDE_INT == 0
13970 if (!SCALAR_FLOAT_MODE_P (mode))
13972 mem_loc_result->dw_loc_oprnd2.val_class
13973 = dw_val_class_const_double;
13974 mem_loc_result->dw_loc_oprnd2.v.val_double
13975 = rtx_to_double_int (rtl);
13977 else
13978 #endif
13980 unsigned int length = GET_MODE_SIZE (mode);
13981 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13983 insert_float (rtl, array);
13984 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13985 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13986 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13987 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13990 break;
13992 case CONST_WIDE_INT:
13993 if (!dwarf_strict)
13995 dw_die_ref type_die;
13997 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13998 if (type_die == NULL)
13999 return NULL;
14000 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
14001 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14002 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14003 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14004 mem_loc_result->dw_loc_oprnd2.val_class
14005 = dw_val_class_wide_int;
14006 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14007 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14009 break;
14011 case EQ:
14012 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
14013 break;
14015 case GE:
14016 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14017 break;
14019 case GT:
14020 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14021 break;
14023 case LE:
14024 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14025 break;
14027 case LT:
14028 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14029 break;
14031 case NE:
14032 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14033 break;
14035 case GEU:
14036 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14037 break;
14039 case GTU:
14040 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14041 break;
14043 case LEU:
14044 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14045 break;
14047 case LTU:
14048 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14049 break;
14051 case UMIN:
14052 case UMAX:
14053 if (!SCALAR_INT_MODE_P (mode))
14054 break;
14055 /* FALLTHRU */
14056 case SMIN:
14057 case SMAX:
14058 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14059 break;
14061 case ZERO_EXTRACT:
14062 case SIGN_EXTRACT:
14063 if (CONST_INT_P (XEXP (rtl, 1))
14064 && CONST_INT_P (XEXP (rtl, 2))
14065 && ((unsigned) INTVAL (XEXP (rtl, 1))
14066 + (unsigned) INTVAL (XEXP (rtl, 2))
14067 <= GET_MODE_BITSIZE (mode))
14068 && SCALAR_INT_MODE_P (mode)
14069 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14070 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14072 int shift, size;
14073 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14074 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14075 if (op0 == 0)
14076 break;
14077 if (GET_CODE (rtl) == SIGN_EXTRACT)
14078 op = DW_OP_shra;
14079 else
14080 op = DW_OP_shr;
14081 mem_loc_result = op0;
14082 size = INTVAL (XEXP (rtl, 1));
14083 shift = INTVAL (XEXP (rtl, 2));
14084 if (BITS_BIG_ENDIAN)
14085 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14086 - shift - size;
14087 if (shift + size != (int) DWARF2_ADDR_SIZE)
14089 add_loc_descr (&mem_loc_result,
14090 int_loc_descriptor (DWARF2_ADDR_SIZE
14091 - shift - size));
14092 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14094 if (size != (int) DWARF2_ADDR_SIZE)
14096 add_loc_descr (&mem_loc_result,
14097 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14098 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14101 break;
14103 case IF_THEN_ELSE:
14105 dw_loc_descr_ref op2, bra_node, drop_node;
14106 op0 = mem_loc_descriptor (XEXP (rtl, 0),
14107 GET_MODE (XEXP (rtl, 0)) == VOIDmode
14108 ? word_mode : GET_MODE (XEXP (rtl, 0)),
14109 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14110 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14111 VAR_INIT_STATUS_INITIALIZED);
14112 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14113 VAR_INIT_STATUS_INITIALIZED);
14114 if (op0 == NULL || op1 == NULL || op2 == NULL)
14115 break;
14117 mem_loc_result = op1;
14118 add_loc_descr (&mem_loc_result, op2);
14119 add_loc_descr (&mem_loc_result, op0);
14120 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14121 add_loc_descr (&mem_loc_result, bra_node);
14122 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14123 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14124 add_loc_descr (&mem_loc_result, drop_node);
14125 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14126 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14128 break;
14130 case FLOAT_EXTEND:
14131 case FLOAT_TRUNCATE:
14132 case FLOAT:
14133 case UNSIGNED_FLOAT:
14134 case FIX:
14135 case UNSIGNED_FIX:
14136 if (!dwarf_strict)
14138 dw_die_ref type_die;
14139 dw_loc_descr_ref cvt;
14141 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14142 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14143 if (op0 == NULL)
14144 break;
14145 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
14146 && (GET_CODE (rtl) == FLOAT
14147 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14148 <= DWARF2_ADDR_SIZE))
14150 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14151 GET_CODE (rtl) == UNSIGNED_FLOAT);
14152 if (type_die == NULL)
14153 break;
14154 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14155 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14156 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14157 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14158 add_loc_descr (&op0, cvt);
14160 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14161 if (type_die == NULL)
14162 break;
14163 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14164 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14165 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14166 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14167 add_loc_descr (&op0, cvt);
14168 if (SCALAR_INT_MODE_P (mode)
14169 && (GET_CODE (rtl) == FIX
14170 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14172 op0 = convert_descriptor_to_mode (mode, op0);
14173 if (op0 == NULL)
14174 break;
14176 mem_loc_result = op0;
14178 break;
14180 case CLZ:
14181 case CTZ:
14182 case FFS:
14183 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14184 break;
14186 case POPCOUNT:
14187 case PARITY:
14188 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14189 break;
14191 case BSWAP:
14192 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14193 break;
14195 case ROTATE:
14196 case ROTATERT:
14197 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14198 break;
14200 case COMPARE:
14201 /* In theory, we could implement the above. */
14202 /* DWARF cannot represent the unsigned compare operations
14203 natively. */
14204 case SS_MULT:
14205 case US_MULT:
14206 case SS_DIV:
14207 case US_DIV:
14208 case SS_PLUS:
14209 case US_PLUS:
14210 case SS_MINUS:
14211 case US_MINUS:
14212 case SS_NEG:
14213 case US_NEG:
14214 case SS_ABS:
14215 case SS_ASHIFT:
14216 case US_ASHIFT:
14217 case SS_TRUNCATE:
14218 case US_TRUNCATE:
14219 case UNORDERED:
14220 case ORDERED:
14221 case UNEQ:
14222 case UNGE:
14223 case UNGT:
14224 case UNLE:
14225 case UNLT:
14226 case LTGT:
14227 case FRACT_CONVERT:
14228 case UNSIGNED_FRACT_CONVERT:
14229 case SAT_FRACT:
14230 case UNSIGNED_SAT_FRACT:
14231 case SQRT:
14232 case ASM_OPERANDS:
14233 case VEC_MERGE:
14234 case VEC_SELECT:
14235 case VEC_CONCAT:
14236 case VEC_DUPLICATE:
14237 case UNSPEC:
14238 case HIGH:
14239 case FMA:
14240 case STRICT_LOW_PART:
14241 case CONST_VECTOR:
14242 case CONST_FIXED:
14243 case CLRSB:
14244 case CLOBBER:
14245 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14246 can't express it in the debug info. This can happen e.g. with some
14247 TLS UNSPECs. */
14248 break;
14250 case CONST_STRING:
14251 resolve_one_addr (&rtl);
14252 goto symref;
14254 default:
14255 if (flag_checking)
14257 print_rtl (stderr, rtl);
14258 gcc_unreachable ();
14260 break;
14263 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14264 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14266 return mem_loc_result;
14269 /* Return a descriptor that describes the concatenation of two locations.
14270 This is typically a complex variable. */
14272 static dw_loc_descr_ref
14273 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14275 dw_loc_descr_ref cc_loc_result = NULL;
14276 dw_loc_descr_ref x0_ref
14277 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14278 dw_loc_descr_ref x1_ref
14279 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14281 if (x0_ref == 0 || x1_ref == 0)
14282 return 0;
14284 cc_loc_result = x0_ref;
14285 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14287 add_loc_descr (&cc_loc_result, x1_ref);
14288 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14290 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14291 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14293 return cc_loc_result;
14296 /* Return a descriptor that describes the concatenation of N
14297 locations. */
14299 static dw_loc_descr_ref
14300 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14302 unsigned int i;
14303 dw_loc_descr_ref cc_loc_result = NULL;
14304 unsigned int n = XVECLEN (concatn, 0);
14306 for (i = 0; i < n; ++i)
14308 dw_loc_descr_ref ref;
14309 rtx x = XVECEXP (concatn, 0, i);
14311 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14312 if (ref == NULL)
14313 return NULL;
14315 add_loc_descr (&cc_loc_result, ref);
14316 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14319 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14320 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14322 return cc_loc_result;
14325 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14326 for DEBUG_IMPLICIT_PTR RTL. */
14328 static dw_loc_descr_ref
14329 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14331 dw_loc_descr_ref ret;
14332 dw_die_ref ref;
14334 if (dwarf_strict)
14335 return NULL;
14336 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14337 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14338 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14339 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14340 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14341 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14342 if (ref)
14344 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14345 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14346 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14348 else
14350 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14351 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14353 return ret;
14356 /* Output a proper Dwarf location descriptor for a variable or parameter
14357 which is either allocated in a register or in a memory location. For a
14358 register, we just generate an OP_REG and the register number. For a
14359 memory location we provide a Dwarf postfix expression describing how to
14360 generate the (dynamic) address of the object onto the address stack.
14362 MODE is mode of the decl if this loc_descriptor is going to be used in
14363 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14364 allowed, VOIDmode otherwise.
14366 If we don't know how to describe it, return 0. */
14368 static dw_loc_descr_ref
14369 loc_descriptor (rtx rtl, machine_mode mode,
14370 enum var_init_status initialized)
14372 dw_loc_descr_ref loc_result = NULL;
14374 switch (GET_CODE (rtl))
14376 case SUBREG:
14377 /* The case of a subreg may arise when we have a local (register)
14378 variable or a formal (register) parameter which doesn't quite fill
14379 up an entire register. For now, just assume that it is
14380 legitimate to make the Dwarf info refer to the whole register which
14381 contains the given subreg. */
14382 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14383 loc_result = loc_descriptor (SUBREG_REG (rtl),
14384 GET_MODE (SUBREG_REG (rtl)), initialized);
14385 else
14386 goto do_default;
14387 break;
14389 case REG:
14390 loc_result = reg_loc_descriptor (rtl, initialized);
14391 break;
14393 case MEM:
14394 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14395 GET_MODE (rtl), initialized);
14396 if (loc_result == NULL)
14397 loc_result = tls_mem_loc_descriptor (rtl);
14398 if (loc_result == NULL)
14400 rtx new_rtl = avoid_constant_pool_reference (rtl);
14401 if (new_rtl != rtl)
14402 loc_result = loc_descriptor (new_rtl, mode, initialized);
14404 break;
14406 case CONCAT:
14407 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14408 initialized);
14409 break;
14411 case CONCATN:
14412 loc_result = concatn_loc_descriptor (rtl, initialized);
14413 break;
14415 case VAR_LOCATION:
14416 /* Single part. */
14417 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14419 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14420 if (GET_CODE (loc) == EXPR_LIST)
14421 loc = XEXP (loc, 0);
14422 loc_result = loc_descriptor (loc, mode, initialized);
14423 break;
14426 rtl = XEXP (rtl, 1);
14427 /* FALLTHRU */
14429 case PARALLEL:
14431 rtvec par_elems = XVEC (rtl, 0);
14432 int num_elem = GET_NUM_ELEM (par_elems);
14433 machine_mode mode;
14434 int i;
14436 /* Create the first one, so we have something to add to. */
14437 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14438 VOIDmode, initialized);
14439 if (loc_result == NULL)
14440 return NULL;
14441 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14442 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14443 for (i = 1; i < num_elem; i++)
14445 dw_loc_descr_ref temp;
14447 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14448 VOIDmode, initialized);
14449 if (temp == NULL)
14450 return NULL;
14451 add_loc_descr (&loc_result, temp);
14452 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14453 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14456 break;
14458 case CONST_INT:
14459 if (mode != VOIDmode && mode != BLKmode)
14460 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14461 INTVAL (rtl));
14462 break;
14464 case CONST_DOUBLE:
14465 if (mode == VOIDmode)
14466 mode = GET_MODE (rtl);
14468 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14470 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14472 /* Note that a CONST_DOUBLE rtx could represent either an integer
14473 or a floating-point constant. A CONST_DOUBLE is used whenever
14474 the constant requires more than one word in order to be
14475 adequately represented. We output CONST_DOUBLEs as blocks. */
14476 loc_result = new_loc_descr (DW_OP_implicit_value,
14477 GET_MODE_SIZE (mode), 0);
14478 #if TARGET_SUPPORTS_WIDE_INT == 0
14479 if (!SCALAR_FLOAT_MODE_P (mode))
14481 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14482 loc_result->dw_loc_oprnd2.v.val_double
14483 = rtx_to_double_int (rtl);
14485 else
14486 #endif
14488 unsigned int length = GET_MODE_SIZE (mode);
14489 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14491 insert_float (rtl, array);
14492 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14493 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14494 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14495 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14498 break;
14500 case CONST_WIDE_INT:
14501 if (mode == VOIDmode)
14502 mode = GET_MODE (rtl);
14504 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14506 loc_result = new_loc_descr (DW_OP_implicit_value,
14507 GET_MODE_SIZE (mode), 0);
14508 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14509 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14510 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14512 break;
14514 case CONST_VECTOR:
14515 if (mode == VOIDmode)
14516 mode = GET_MODE (rtl);
14518 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14520 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14521 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14522 unsigned char *array
14523 = ggc_vec_alloc<unsigned char> (length * elt_size);
14524 unsigned int i;
14525 unsigned char *p;
14526 machine_mode imode = GET_MODE_INNER (mode);
14528 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14529 switch (GET_MODE_CLASS (mode))
14531 case MODE_VECTOR_INT:
14532 for (i = 0, p = array; i < length; i++, p += elt_size)
14534 rtx elt = CONST_VECTOR_ELT (rtl, i);
14535 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14537 break;
14539 case MODE_VECTOR_FLOAT:
14540 for (i = 0, p = array; i < length; i++, p += elt_size)
14542 rtx elt = CONST_VECTOR_ELT (rtl, i);
14543 insert_float (elt, p);
14545 break;
14547 default:
14548 gcc_unreachable ();
14551 loc_result = new_loc_descr (DW_OP_implicit_value,
14552 length * elt_size, 0);
14553 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14554 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14555 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14556 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14558 break;
14560 case CONST:
14561 if (mode == VOIDmode
14562 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14563 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14564 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14566 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14567 break;
14569 /* FALLTHROUGH */
14570 case SYMBOL_REF:
14571 if (!const_ok_for_output (rtl))
14572 break;
14573 /* FALLTHROUGH */
14574 case LABEL_REF:
14575 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14576 && (dwarf_version >= 4 || !dwarf_strict))
14578 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14579 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14580 vec_safe_push (used_rtx_array, rtl);
14582 break;
14584 case DEBUG_IMPLICIT_PTR:
14585 loc_result = implicit_ptr_descriptor (rtl, 0);
14586 break;
14588 case PLUS:
14589 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14590 && CONST_INT_P (XEXP (rtl, 1)))
14592 loc_result
14593 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14594 break;
14596 /* FALLTHRU */
14597 do_default:
14598 default:
14599 if ((SCALAR_INT_MODE_P (mode)
14600 && GET_MODE (rtl) == mode
14601 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14602 && dwarf_version >= 4)
14603 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14605 /* Value expression. */
14606 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14607 if (loc_result)
14608 add_loc_descr (&loc_result,
14609 new_loc_descr (DW_OP_stack_value, 0, 0));
14611 break;
14614 return loc_result;
14617 /* We need to figure out what section we should use as the base for the
14618 address ranges where a given location is valid.
14619 1. If this particular DECL has a section associated with it, use that.
14620 2. If this function has a section associated with it, use that.
14621 3. Otherwise, use the text section.
14622 XXX: If you split a variable across multiple sections, we won't notice. */
14624 static const char *
14625 secname_for_decl (const_tree decl)
14627 const char *secname;
14629 if (VAR_OR_FUNCTION_DECL_P (decl)
14630 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14631 && DECL_SECTION_NAME (decl))
14632 secname = DECL_SECTION_NAME (decl);
14633 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14634 secname = DECL_SECTION_NAME (current_function_decl);
14635 else if (cfun && in_cold_section_p)
14636 secname = crtl->subsections.cold_section_label;
14637 else
14638 secname = text_section_label;
14640 return secname;
14643 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14645 static bool
14646 decl_by_reference_p (tree decl)
14648 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14649 || VAR_P (decl))
14650 && DECL_BY_REFERENCE (decl));
14653 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14654 for VARLOC. */
14656 static dw_loc_descr_ref
14657 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14658 enum var_init_status initialized)
14660 int have_address = 0;
14661 dw_loc_descr_ref descr;
14662 machine_mode mode;
14664 if (want_address != 2)
14666 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14667 /* Single part. */
14668 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14670 varloc = PAT_VAR_LOCATION_LOC (varloc);
14671 if (GET_CODE (varloc) == EXPR_LIST)
14672 varloc = XEXP (varloc, 0);
14673 mode = GET_MODE (varloc);
14674 if (MEM_P (varloc))
14676 rtx addr = XEXP (varloc, 0);
14677 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14678 mode, initialized);
14679 if (descr)
14680 have_address = 1;
14681 else
14683 rtx x = avoid_constant_pool_reference (varloc);
14684 if (x != varloc)
14685 descr = mem_loc_descriptor (x, mode, VOIDmode,
14686 initialized);
14689 else
14690 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14692 else
14693 return 0;
14695 else
14697 if (GET_CODE (varloc) == VAR_LOCATION)
14698 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14699 else
14700 mode = DECL_MODE (loc);
14701 descr = loc_descriptor (varloc, mode, initialized);
14702 have_address = 1;
14705 if (!descr)
14706 return 0;
14708 if (want_address == 2 && !have_address
14709 && (dwarf_version >= 4 || !dwarf_strict))
14711 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14713 expansion_failed (loc, NULL_RTX,
14714 "DWARF address size mismatch");
14715 return 0;
14717 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14718 have_address = 1;
14720 /* Show if we can't fill the request for an address. */
14721 if (want_address && !have_address)
14723 expansion_failed (loc, NULL_RTX,
14724 "Want address and only have value");
14725 return 0;
14728 /* If we've got an address and don't want one, dereference. */
14729 if (!want_address && have_address)
14731 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14732 enum dwarf_location_atom op;
14734 if (size > DWARF2_ADDR_SIZE || size == -1)
14736 expansion_failed (loc, NULL_RTX,
14737 "DWARF address size mismatch");
14738 return 0;
14740 else if (size == DWARF2_ADDR_SIZE)
14741 op = DW_OP_deref;
14742 else
14743 op = DW_OP_deref_size;
14745 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14748 return descr;
14751 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14752 if it is not possible. */
14754 static dw_loc_descr_ref
14755 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14757 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14758 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14759 else if (dwarf_version >= 3 || !dwarf_strict)
14760 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14761 else
14762 return NULL;
14765 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14766 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14768 static dw_loc_descr_ref
14769 dw_sra_loc_expr (tree decl, rtx loc)
14771 rtx p;
14772 unsigned HOST_WIDE_INT padsize = 0;
14773 dw_loc_descr_ref descr, *descr_tail;
14774 unsigned HOST_WIDE_INT decl_size;
14775 rtx varloc;
14776 enum var_init_status initialized;
14778 if (DECL_SIZE (decl) == NULL
14779 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14780 return NULL;
14782 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14783 descr = NULL;
14784 descr_tail = &descr;
14786 for (p = loc; p; p = XEXP (p, 1))
14788 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14789 rtx loc_note = *decl_piece_varloc_ptr (p);
14790 dw_loc_descr_ref cur_descr;
14791 dw_loc_descr_ref *tail, last = NULL;
14792 unsigned HOST_WIDE_INT opsize = 0;
14794 if (loc_note == NULL_RTX
14795 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14797 padsize += bitsize;
14798 continue;
14800 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14801 varloc = NOTE_VAR_LOCATION (loc_note);
14802 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14803 if (cur_descr == NULL)
14805 padsize += bitsize;
14806 continue;
14809 /* Check that cur_descr either doesn't use
14810 DW_OP_*piece operations, or their sum is equal
14811 to bitsize. Otherwise we can't embed it. */
14812 for (tail = &cur_descr; *tail != NULL;
14813 tail = &(*tail)->dw_loc_next)
14814 if ((*tail)->dw_loc_opc == DW_OP_piece)
14816 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14817 * BITS_PER_UNIT;
14818 last = *tail;
14820 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14822 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14823 last = *tail;
14826 if (last != NULL && opsize != bitsize)
14828 padsize += bitsize;
14829 /* Discard the current piece of the descriptor and release any
14830 addr_table entries it uses. */
14831 remove_loc_list_addr_table_entries (cur_descr);
14832 continue;
14835 /* If there is a hole, add DW_OP_*piece after empty DWARF
14836 expression, which means that those bits are optimized out. */
14837 if (padsize)
14839 if (padsize > decl_size)
14841 remove_loc_list_addr_table_entries (cur_descr);
14842 goto discard_descr;
14844 decl_size -= padsize;
14845 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14846 if (*descr_tail == NULL)
14848 remove_loc_list_addr_table_entries (cur_descr);
14849 goto discard_descr;
14851 descr_tail = &(*descr_tail)->dw_loc_next;
14852 padsize = 0;
14854 *descr_tail = cur_descr;
14855 descr_tail = tail;
14856 if (bitsize > decl_size)
14857 goto discard_descr;
14858 decl_size -= bitsize;
14859 if (last == NULL)
14861 HOST_WIDE_INT offset = 0;
14862 if (GET_CODE (varloc) == VAR_LOCATION
14863 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14865 varloc = PAT_VAR_LOCATION_LOC (varloc);
14866 if (GET_CODE (varloc) == EXPR_LIST)
14867 varloc = XEXP (varloc, 0);
14871 if (GET_CODE (varloc) == CONST
14872 || GET_CODE (varloc) == SIGN_EXTEND
14873 || GET_CODE (varloc) == ZERO_EXTEND)
14874 varloc = XEXP (varloc, 0);
14875 else if (GET_CODE (varloc) == SUBREG)
14876 varloc = SUBREG_REG (varloc);
14877 else
14878 break;
14880 while (1);
14881 /* DW_OP_bit_size offset should be zero for register
14882 or implicit location descriptions and empty location
14883 descriptions, but for memory addresses needs big endian
14884 adjustment. */
14885 if (MEM_P (varloc))
14887 unsigned HOST_WIDE_INT memsize
14888 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14889 if (memsize != bitsize)
14891 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14892 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14893 goto discard_descr;
14894 if (memsize < bitsize)
14895 goto discard_descr;
14896 if (BITS_BIG_ENDIAN)
14897 offset = memsize - bitsize;
14901 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14902 if (*descr_tail == NULL)
14903 goto discard_descr;
14904 descr_tail = &(*descr_tail)->dw_loc_next;
14908 /* If there were any non-empty expressions, add padding till the end of
14909 the decl. */
14910 if (descr != NULL && decl_size != 0)
14912 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14913 if (*descr_tail == NULL)
14914 goto discard_descr;
14916 return descr;
14918 discard_descr:
14919 /* Discard the descriptor and release any addr_table entries it uses. */
14920 remove_loc_list_addr_table_entries (descr);
14921 return NULL;
14924 /* Return the dwarf representation of the location list LOC_LIST of
14925 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14926 function. */
14928 static dw_loc_list_ref
14929 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14931 const char *endname, *secname;
14932 rtx varloc;
14933 enum var_init_status initialized;
14934 struct var_loc_node *node;
14935 dw_loc_descr_ref descr;
14936 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14937 dw_loc_list_ref list = NULL;
14938 dw_loc_list_ref *listp = &list;
14940 /* Now that we know what section we are using for a base,
14941 actually construct the list of locations.
14942 The first location information is what is passed to the
14943 function that creates the location list, and the remaining
14944 locations just get added on to that list.
14945 Note that we only know the start address for a location
14946 (IE location changes), so to build the range, we use
14947 the range [current location start, next location start].
14948 This means we have to special case the last node, and generate
14949 a range of [last location start, end of function label]. */
14951 secname = secname_for_decl (decl);
14953 for (node = loc_list->first; node; node = node->next)
14954 if (GET_CODE (node->loc) == EXPR_LIST
14955 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14957 if (GET_CODE (node->loc) == EXPR_LIST)
14959 /* This requires DW_OP_{,bit_}piece, which is not usable
14960 inside DWARF expressions. */
14961 if (want_address != 2)
14962 continue;
14963 descr = dw_sra_loc_expr (decl, node->loc);
14964 if (descr == NULL)
14965 continue;
14967 else
14969 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14970 varloc = NOTE_VAR_LOCATION (node->loc);
14971 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14973 if (descr)
14975 bool range_across_switch = false;
14976 /* If section switch happens in between node->label
14977 and node->next->label (or end of function) and
14978 we can't emit it as a single entry list,
14979 emit two ranges, first one ending at the end
14980 of first partition and second one starting at the
14981 beginning of second partition. */
14982 if (node == loc_list->last_before_switch
14983 && (node != loc_list->first || loc_list->first->next)
14984 && current_function_decl)
14986 endname = cfun->fde->dw_fde_end;
14987 range_across_switch = true;
14989 /* The variable has a location between NODE->LABEL and
14990 NODE->NEXT->LABEL. */
14991 else if (node->next)
14992 endname = node->next->label;
14993 /* If the variable has a location at the last label
14994 it keeps its location until the end of function. */
14995 else if (!current_function_decl)
14996 endname = text_end_label;
14997 else
14999 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15000 current_function_funcdef_no);
15001 endname = ggc_strdup (label_id);
15004 *listp = new_loc_list (descr, node->label, endname, secname);
15005 if (TREE_CODE (decl) == PARM_DECL
15006 && node == loc_list->first
15007 && NOTE_P (node->loc)
15008 && strcmp (node->label, endname) == 0)
15009 (*listp)->force = true;
15010 listp = &(*listp)->dw_loc_next;
15012 if (range_across_switch)
15014 if (GET_CODE (node->loc) == EXPR_LIST)
15015 descr = dw_sra_loc_expr (decl, node->loc);
15016 else
15018 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15019 varloc = NOTE_VAR_LOCATION (node->loc);
15020 descr = dw_loc_list_1 (decl, varloc, want_address,
15021 initialized);
15023 gcc_assert (descr);
15024 /* The variable has a location between NODE->LABEL and
15025 NODE->NEXT->LABEL. */
15026 if (node->next)
15027 endname = node->next->label;
15028 else
15029 endname = cfun->fde->dw_fde_second_end;
15030 *listp = new_loc_list (descr,
15031 cfun->fde->dw_fde_second_begin,
15032 endname, secname);
15033 listp = &(*listp)->dw_loc_next;
15038 /* Try to avoid the overhead of a location list emitting a location
15039 expression instead, but only if we didn't have more than one
15040 location entry in the first place. If some entries were not
15041 representable, we don't want to pretend a single entry that was
15042 applies to the entire scope in which the variable is
15043 available. */
15044 if (list && loc_list->first->next)
15045 gen_llsym (list);
15047 return list;
15050 /* Return if the loc_list has only single element and thus can be represented
15051 as location description. */
15053 static bool
15054 single_element_loc_list_p (dw_loc_list_ref list)
15056 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15057 return !list->ll_symbol;
15060 /* To each location in list LIST add loc descr REF. */
15062 static void
15063 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15065 dw_loc_descr_ref copy;
15066 add_loc_descr (&list->expr, ref);
15067 list = list->dw_loc_next;
15068 while (list)
15070 copy = ggc_alloc<dw_loc_descr_node> ();
15071 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15072 add_loc_descr (&list->expr, copy);
15073 while (copy->dw_loc_next)
15075 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15076 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15077 copy->dw_loc_next = new_copy;
15078 copy = new_copy;
15080 list = list->dw_loc_next;
15084 /* Given two lists RET and LIST
15085 produce location list that is result of adding expression in LIST
15086 to expression in RET on each position in program.
15087 Might be destructive on both RET and LIST.
15089 TODO: We handle only simple cases of RET or LIST having at most one
15090 element. General case would inolve sorting the lists in program order
15091 and merging them that will need some additional work.
15092 Adding that will improve quality of debug info especially for SRA-ed
15093 structures. */
15095 static void
15096 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15098 if (!list)
15099 return;
15100 if (!*ret)
15102 *ret = list;
15103 return;
15105 if (!list->dw_loc_next)
15107 add_loc_descr_to_each (*ret, list->expr);
15108 return;
15110 if (!(*ret)->dw_loc_next)
15112 add_loc_descr_to_each (list, (*ret)->expr);
15113 *ret = list;
15114 return;
15116 expansion_failed (NULL_TREE, NULL_RTX,
15117 "Don't know how to merge two non-trivial"
15118 " location lists.\n");
15119 *ret = NULL;
15120 return;
15123 /* LOC is constant expression. Try a luck, look it up in constant
15124 pool and return its loc_descr of its address. */
15126 static dw_loc_descr_ref
15127 cst_pool_loc_descr (tree loc)
15129 /* Get an RTL for this, if something has been emitted. */
15130 rtx rtl = lookup_constant_def (loc);
15132 if (!rtl || !MEM_P (rtl))
15134 gcc_assert (!rtl);
15135 return 0;
15137 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15139 /* TODO: We might get more coverage if we was actually delaying expansion
15140 of all expressions till end of compilation when constant pools are fully
15141 populated. */
15142 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15144 expansion_failed (loc, NULL_RTX,
15145 "CST value in contant pool but not marked.");
15146 return 0;
15148 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15149 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15152 /* Return dw_loc_list representing address of addr_expr LOC
15153 by looking for inner INDIRECT_REF expression and turning
15154 it into simple arithmetics.
15156 See loc_list_from_tree for the meaning of CONTEXT. */
15158 static dw_loc_list_ref
15159 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15160 const loc_descr_context *context)
15162 tree obj, offset;
15163 HOST_WIDE_INT bitsize, bitpos, bytepos;
15164 machine_mode mode;
15165 int unsignedp, reversep, volatilep = 0;
15166 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15168 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15169 &bitsize, &bitpos, &offset, &mode,
15170 &unsignedp, &reversep, &volatilep);
15171 STRIP_NOPS (obj);
15172 if (bitpos % BITS_PER_UNIT)
15174 expansion_failed (loc, NULL_RTX, "bitfield access");
15175 return 0;
15177 if (!INDIRECT_REF_P (obj))
15179 expansion_failed (obj,
15180 NULL_RTX, "no indirect ref in inner refrence");
15181 return 0;
15183 if (!offset && !bitpos)
15184 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15185 context);
15186 else if (toplev
15187 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15188 && (dwarf_version >= 4 || !dwarf_strict))
15190 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15191 if (!list_ret)
15192 return 0;
15193 if (offset)
15195 /* Variable offset. */
15196 list_ret1 = loc_list_from_tree (offset, 0, context);
15197 if (list_ret1 == 0)
15198 return 0;
15199 add_loc_list (&list_ret, list_ret1);
15200 if (!list_ret)
15201 return 0;
15202 add_loc_descr_to_each (list_ret,
15203 new_loc_descr (DW_OP_plus, 0, 0));
15205 bytepos = bitpos / BITS_PER_UNIT;
15206 if (bytepos > 0)
15207 add_loc_descr_to_each (list_ret,
15208 new_loc_descr (DW_OP_plus_uconst,
15209 bytepos, 0));
15210 else if (bytepos < 0)
15211 loc_list_plus_const (list_ret, bytepos);
15212 add_loc_descr_to_each (list_ret,
15213 new_loc_descr (DW_OP_stack_value, 0, 0));
15215 return list_ret;
15218 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15219 all operations from LOC are nops, move to the last one. Insert in NOPS all
15220 operations that are skipped. */
15222 static void
15223 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15224 hash_set<dw_loc_descr_ref> &nops)
15226 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15228 nops.add (loc);
15229 loc = loc->dw_loc_next;
15233 /* Helper for loc_descr_without_nops: free the location description operation
15234 P. */
15236 bool
15237 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15239 ggc_free (loc);
15240 return true;
15243 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15244 finishes LOC. */
15246 static void
15247 loc_descr_without_nops (dw_loc_descr_ref &loc)
15249 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15250 return;
15252 /* Set of all DW_OP_nop operations we remove. */
15253 hash_set<dw_loc_descr_ref> nops;
15255 /* First, strip all prefix NOP operations in order to keep the head of the
15256 operations list. */
15257 loc_descr_to_next_no_nop (loc, nops);
15259 for (dw_loc_descr_ref cur = loc; cur != NULL;)
15261 /* For control flow operations: strip "prefix" nops in destination
15262 labels. */
15263 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15264 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15265 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15266 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15268 /* Do the same for the operations that follow, then move to the next
15269 iteration. */
15270 if (cur->dw_loc_next != NULL)
15271 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15272 cur = cur->dw_loc_next;
15275 nops.traverse<void *, free_loc_descr> (NULL);
15279 struct dwarf_procedure_info;
15281 /* Helper structure for location descriptions generation. */
15282 struct loc_descr_context
15284 /* The type that is implicitly referenced by DW_OP_push_object_address, or
15285 NULL_TREE if DW_OP_push_object_address in invalid for this location
15286 description. This is used when processing PLACEHOLDER_EXPR nodes. */
15287 tree context_type;
15288 /* The ..._DECL node that should be translated as a
15289 DW_OP_push_object_address operation. */
15290 tree base_decl;
15291 /* Information about the DWARF procedure we are currently generating. NULL if
15292 we are not generating a DWARF procedure. */
15293 struct dwarf_procedure_info *dpi;
15296 /* DWARF procedures generation
15298 DWARF expressions (aka. location descriptions) are used to encode variable
15299 things such as sizes or offsets. Such computations can have redundant parts
15300 that can be factorized in order to reduce the size of the output debug
15301 information. This is the whole point of DWARF procedures.
15303 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15304 already factorized into functions ("size functions") in order to handle very
15305 big and complex types. Such functions are quite simple: they have integral
15306 arguments, they return an integral result and their body contains only a
15307 return statement with arithmetic expressions. This is the only kind of
15308 function we are interested in translating into DWARF procedures, here.
15310 DWARF expressions and DWARF procedure are executed using a stack, so we have
15311 to define some calling convention for them to interact. Let's say that:
15313 - Before calling a DWARF procedure, DWARF expressions must push on the stack
15314 all arguments in reverse order (right-to-left) so that when the DWARF
15315 procedure execution starts, the first argument is the top of the stack.
15317 - Then, when returning, the DWARF procedure must have consumed all arguments
15318 on the stack, must have pushed the result and touched nothing else.
15320 - Each integral argument and the result are integral types can be hold in a
15321 single stack slot.
15323 - We call "frame offset" the number of stack slots that are "under DWARF
15324 procedure control": it includes the arguments slots, the temporaries and
15325 the result slot. Thus, it is equal to the number of arguments when the
15326 procedure execution starts and must be equal to one (the result) when it
15327 returns. */
15329 /* Helper structure used when generating operations for a DWARF procedure. */
15330 struct dwarf_procedure_info
15332 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15333 currently translated. */
15334 tree fndecl;
15335 /* The number of arguments FNDECL takes. */
15336 unsigned args_count;
15339 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
15340 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
15341 equate it to this DIE. */
15343 static dw_die_ref
15344 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15345 dw_die_ref parent_die)
15347 dw_die_ref dwarf_proc_die;
15349 if ((dwarf_version < 3 && dwarf_strict)
15350 || location == NULL)
15351 return NULL;
15353 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
15354 if (fndecl)
15355 equate_decl_number_to_die (fndecl, dwarf_proc_die);
15356 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15357 return dwarf_proc_die;
15360 /* Return whether TYPE is a supported type as a DWARF procedure argument
15361 type or return type (we handle only scalar types and pointer types that
15362 aren't wider than the DWARF expression evaluation stack. */
15364 static bool
15365 is_handled_procedure_type (tree type)
15367 return ((INTEGRAL_TYPE_P (type)
15368 || TREE_CODE (type) == OFFSET_TYPE
15369 || TREE_CODE (type) == POINTER_TYPE)
15370 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15373 /* Helper for resolve_args_picking: do the same but stop when coming across
15374 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
15375 offset *before* evaluating the corresponding operation. */
15377 static bool
15378 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15379 struct dwarf_procedure_info *dpi,
15380 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
15382 /* The "frame_offset" identifier is already used to name a macro... */
15383 unsigned frame_offset_ = initial_frame_offset;
15384 dw_loc_descr_ref l;
15386 for (l = loc; l != NULL;)
15388 bool existed;
15389 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
15391 /* If we already met this node, there is nothing to compute anymore. */
15392 if (existed)
15394 /* Make sure that the stack size is consistent wherever the execution
15395 flow comes from. */
15396 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
15397 break;
15399 l_frame_offset = frame_offset_;
15401 /* If needed, relocate the picking offset with respect to the frame
15402 offset. */
15403 if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15405 /* frame_offset_ is the size of the current stack frame, including
15406 incoming arguments. Besides, the arguments are pushed
15407 right-to-left. Thus, in order to access the Nth argument from
15408 this operation node, the picking has to skip temporaries *plus*
15409 one stack slot per argument (0 for the first one, 1 for the second
15410 one, etc.).
15412 The targetted argument number (N) is already set as the operand,
15413 and the number of temporaries can be computed with:
15414 frame_offsets_ - dpi->args_count */
15415 l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15417 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
15418 if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15419 return false;
15422 /* Update frame_offset according to the effect the current operation has
15423 on the stack. */
15424 switch (l->dw_loc_opc)
15426 case DW_OP_deref:
15427 case DW_OP_swap:
15428 case DW_OP_rot:
15429 case DW_OP_abs:
15430 case DW_OP_neg:
15431 case DW_OP_not:
15432 case DW_OP_plus_uconst:
15433 case DW_OP_skip:
15434 case DW_OP_reg0:
15435 case DW_OP_reg1:
15436 case DW_OP_reg2:
15437 case DW_OP_reg3:
15438 case DW_OP_reg4:
15439 case DW_OP_reg5:
15440 case DW_OP_reg6:
15441 case DW_OP_reg7:
15442 case DW_OP_reg8:
15443 case DW_OP_reg9:
15444 case DW_OP_reg10:
15445 case DW_OP_reg11:
15446 case DW_OP_reg12:
15447 case DW_OP_reg13:
15448 case DW_OP_reg14:
15449 case DW_OP_reg15:
15450 case DW_OP_reg16:
15451 case DW_OP_reg17:
15452 case DW_OP_reg18:
15453 case DW_OP_reg19:
15454 case DW_OP_reg20:
15455 case DW_OP_reg21:
15456 case DW_OP_reg22:
15457 case DW_OP_reg23:
15458 case DW_OP_reg24:
15459 case DW_OP_reg25:
15460 case DW_OP_reg26:
15461 case DW_OP_reg27:
15462 case DW_OP_reg28:
15463 case DW_OP_reg29:
15464 case DW_OP_reg30:
15465 case DW_OP_reg31:
15466 case DW_OP_bregx:
15467 case DW_OP_piece:
15468 case DW_OP_deref_size:
15469 case DW_OP_nop:
15470 case DW_OP_form_tls_address:
15471 case DW_OP_bit_piece:
15472 case DW_OP_implicit_value:
15473 case DW_OP_stack_value:
15474 break;
15476 case DW_OP_addr:
15477 case DW_OP_const1u:
15478 case DW_OP_const1s:
15479 case DW_OP_const2u:
15480 case DW_OP_const2s:
15481 case DW_OP_const4u:
15482 case DW_OP_const4s:
15483 case DW_OP_const8u:
15484 case DW_OP_const8s:
15485 case DW_OP_constu:
15486 case DW_OP_consts:
15487 case DW_OP_dup:
15488 case DW_OP_over:
15489 case DW_OP_pick:
15490 case DW_OP_lit0:
15491 case DW_OP_lit1:
15492 case DW_OP_lit2:
15493 case DW_OP_lit3:
15494 case DW_OP_lit4:
15495 case DW_OP_lit5:
15496 case DW_OP_lit6:
15497 case DW_OP_lit7:
15498 case DW_OP_lit8:
15499 case DW_OP_lit9:
15500 case DW_OP_lit10:
15501 case DW_OP_lit11:
15502 case DW_OP_lit12:
15503 case DW_OP_lit13:
15504 case DW_OP_lit14:
15505 case DW_OP_lit15:
15506 case DW_OP_lit16:
15507 case DW_OP_lit17:
15508 case DW_OP_lit18:
15509 case DW_OP_lit19:
15510 case DW_OP_lit20:
15511 case DW_OP_lit21:
15512 case DW_OP_lit22:
15513 case DW_OP_lit23:
15514 case DW_OP_lit24:
15515 case DW_OP_lit25:
15516 case DW_OP_lit26:
15517 case DW_OP_lit27:
15518 case DW_OP_lit28:
15519 case DW_OP_lit29:
15520 case DW_OP_lit30:
15521 case DW_OP_lit31:
15522 case DW_OP_breg0:
15523 case DW_OP_breg1:
15524 case DW_OP_breg2:
15525 case DW_OP_breg3:
15526 case DW_OP_breg4:
15527 case DW_OP_breg5:
15528 case DW_OP_breg6:
15529 case DW_OP_breg7:
15530 case DW_OP_breg8:
15531 case DW_OP_breg9:
15532 case DW_OP_breg10:
15533 case DW_OP_breg11:
15534 case DW_OP_breg12:
15535 case DW_OP_breg13:
15536 case DW_OP_breg14:
15537 case DW_OP_breg15:
15538 case DW_OP_breg16:
15539 case DW_OP_breg17:
15540 case DW_OP_breg18:
15541 case DW_OP_breg19:
15542 case DW_OP_breg20:
15543 case DW_OP_breg21:
15544 case DW_OP_breg22:
15545 case DW_OP_breg23:
15546 case DW_OP_breg24:
15547 case DW_OP_breg25:
15548 case DW_OP_breg26:
15549 case DW_OP_breg27:
15550 case DW_OP_breg28:
15551 case DW_OP_breg29:
15552 case DW_OP_breg30:
15553 case DW_OP_breg31:
15554 case DW_OP_fbreg:
15555 case DW_OP_push_object_address:
15556 case DW_OP_call_frame_cfa:
15557 ++frame_offset_;
15558 break;
15560 case DW_OP_drop:
15561 case DW_OP_xderef:
15562 case DW_OP_and:
15563 case DW_OP_div:
15564 case DW_OP_minus:
15565 case DW_OP_mod:
15566 case DW_OP_mul:
15567 case DW_OP_or:
15568 case DW_OP_plus:
15569 case DW_OP_shl:
15570 case DW_OP_shr:
15571 case DW_OP_shra:
15572 case DW_OP_xor:
15573 case DW_OP_bra:
15574 case DW_OP_eq:
15575 case DW_OP_ge:
15576 case DW_OP_gt:
15577 case DW_OP_le:
15578 case DW_OP_lt:
15579 case DW_OP_ne:
15580 case DW_OP_regx:
15581 case DW_OP_xderef_size:
15582 --frame_offset_;
15583 break;
15585 case DW_OP_call2:
15586 case DW_OP_call4:
15587 case DW_OP_call_ref:
15589 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15590 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15592 if (stack_usage == NULL)
15593 return false;
15594 frame_offset_ += *stack_usage;
15595 break;
15598 case DW_OP_GNU_push_tls_address:
15599 case DW_OP_GNU_uninit:
15600 case DW_OP_GNU_encoded_addr:
15601 case DW_OP_GNU_implicit_pointer:
15602 case DW_OP_GNU_entry_value:
15603 case DW_OP_GNU_const_type:
15604 case DW_OP_GNU_regval_type:
15605 case DW_OP_GNU_deref_type:
15606 case DW_OP_GNU_convert:
15607 case DW_OP_GNU_reinterpret:
15608 case DW_OP_GNU_parameter_ref:
15609 /* loc_list_from_tree will probably not output these operations for
15610 size functions, so assume they will not appear here. */
15611 /* Fall through... */
15613 default:
15614 gcc_unreachable ();
15617 /* Now, follow the control flow (except subroutine calls). */
15618 switch (l->dw_loc_opc)
15620 case DW_OP_bra:
15621 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15622 frame_offsets))
15623 return false;
15624 /* Fall through. */
15626 case DW_OP_skip:
15627 l = l->dw_loc_oprnd1.v.val_loc;
15628 break;
15630 case DW_OP_stack_value:
15631 return true;
15633 default:
15634 l = l->dw_loc_next;
15635 break;
15639 return true;
15642 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15643 operations) in order to resolve the operand of DW_OP_pick operations that
15644 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
15645 offset *before* LOC is executed. Return if all relocations were
15646 successful. */
15648 static bool
15649 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15650 struct dwarf_procedure_info *dpi)
15652 /* Associate to all visited operations the frame offset *before* evaluating
15653 this operation. */
15654 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
15656 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
15657 frame_offsets);
15660 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15661 Return NULL if it is not possible. */
15663 static dw_die_ref
15664 function_to_dwarf_procedure (tree fndecl)
15666 struct loc_descr_context ctx;
15667 struct dwarf_procedure_info dpi;
15668 dw_die_ref dwarf_proc_die;
15669 tree tree_body = DECL_SAVED_TREE (fndecl);
15670 dw_loc_descr_ref loc_body, epilogue;
15672 tree cursor;
15673 unsigned i;
15675 /* Do not generate multiple DWARF procedures for the same function
15676 declaration. */
15677 dwarf_proc_die = lookup_decl_die (fndecl);
15678 if (dwarf_proc_die != NULL)
15679 return dwarf_proc_die;
15681 /* DWARF procedures are available starting with the DWARFv3 standard. */
15682 if (dwarf_version < 3 && dwarf_strict)
15683 return NULL;
15685 /* We handle only functions for which we still have a body, that return a
15686 supported type and that takes arguments with supported types. Note that
15687 there is no point translating functions that return nothing. */
15688 if (tree_body == NULL_TREE
15689 || DECL_RESULT (fndecl) == NULL_TREE
15690 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15691 return NULL;
15693 for (cursor = DECL_ARGUMENTS (fndecl);
15694 cursor != NULL_TREE;
15695 cursor = TREE_CHAIN (cursor))
15696 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15697 return NULL;
15699 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
15700 if (TREE_CODE (tree_body) != RETURN_EXPR)
15701 return NULL;
15702 tree_body = TREE_OPERAND (tree_body, 0);
15703 if (TREE_CODE (tree_body) != MODIFY_EXPR
15704 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15705 return NULL;
15706 tree_body = TREE_OPERAND (tree_body, 1);
15708 /* Try to translate the body expression itself. Note that this will probably
15709 cause an infinite recursion if its call graph has a cycle. This is very
15710 unlikely for size functions, however, so don't bother with such things at
15711 the moment. */
15712 ctx.context_type = NULL_TREE;
15713 ctx.base_decl = NULL_TREE;
15714 ctx.dpi = &dpi;
15715 dpi.fndecl = fndecl;
15716 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15717 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15718 if (!loc_body)
15719 return NULL;
15721 /* After evaluating all operands in "loc_body", we should still have on the
15722 stack all arguments plus the desired function result (top of the stack).
15723 Generate code in order to keep only the result in our stack frame. */
15724 epilogue = NULL;
15725 for (i = 0; i < dpi.args_count; ++i)
15727 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15728 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15729 op_couple->dw_loc_next->dw_loc_next = epilogue;
15730 epilogue = op_couple;
15732 add_loc_descr (&loc_body, epilogue);
15733 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15734 return NULL;
15736 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15737 because they are considered useful. Now there is an epilogue, they are
15738 not anymore, so give it another try. */
15739 loc_descr_without_nops (loc_body);
15741 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15742 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
15743 though, given that size functions do not come from source, so they should
15744 not have a dedicated DW_TAG_subprogram DIE. */
15745 dwarf_proc_die
15746 = new_dwarf_proc_die (loc_body, fndecl,
15747 get_context_die (DECL_CONTEXT (fndecl)));
15749 /* The called DWARF procedure consumes one stack slot per argument and
15750 returns one stack slot. */
15751 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15753 return dwarf_proc_die;
15757 /* Generate Dwarf location list representing LOC.
15758 If WANT_ADDRESS is false, expression computing LOC will be computed
15759 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15760 if WANT_ADDRESS is 2, expression computing address useable in location
15761 will be returned (i.e. DW_OP_reg can be used
15762 to refer to register values).
15764 CONTEXT provides information to customize the location descriptions
15765 generation. Its context_type field specifies what type is implicitly
15766 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
15767 will not be generated.
15769 Its DPI field determines whether we are generating a DWARF expression for a
15770 DWARF procedure, so PARM_DECL references are processed specifically.
15772 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15773 and dpi fields were null. */
15775 static dw_loc_list_ref
15776 loc_list_from_tree_1 (tree loc, int want_address,
15777 const struct loc_descr_context *context)
15779 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15780 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15781 int have_address = 0;
15782 enum dwarf_location_atom op;
15784 /* ??? Most of the time we do not take proper care for sign/zero
15785 extending the values properly. Hopefully this won't be a real
15786 problem... */
15788 if (context != NULL
15789 && context->base_decl == loc
15790 && want_address == 0)
15792 if (dwarf_version >= 3 || !dwarf_strict)
15793 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15794 NULL, NULL, NULL);
15795 else
15796 return NULL;
15799 switch (TREE_CODE (loc))
15801 case ERROR_MARK:
15802 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15803 return 0;
15805 case PLACEHOLDER_EXPR:
15806 /* This case involves extracting fields from an object to determine the
15807 position of other fields. It is supposed to appear only as the first
15808 operand of COMPONENT_REF nodes and to reference precisely the type
15809 that the context allows. */
15810 if (context != NULL
15811 && TREE_TYPE (loc) == context->context_type
15812 && want_address >= 1)
15814 if (dwarf_version >= 3 || !dwarf_strict)
15816 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15817 have_address = 1;
15818 break;
15820 else
15821 return NULL;
15823 else
15824 expansion_failed (loc, NULL_RTX,
15825 "PLACEHOLDER_EXPR for an unexpected type");
15826 break;
15828 case CALL_EXPR:
15830 const int nargs = call_expr_nargs (loc);
15831 tree callee = get_callee_fndecl (loc);
15832 int i;
15833 dw_die_ref dwarf_proc;
15835 if (callee == NULL_TREE)
15836 goto call_expansion_failed;
15838 /* We handle only functions that return an integer. */
15839 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15840 goto call_expansion_failed;
15842 dwarf_proc = function_to_dwarf_procedure (callee);
15843 if (dwarf_proc == NULL)
15844 goto call_expansion_failed;
15846 /* Evaluate arguments right-to-left so that the first argument will
15847 be the top-most one on the stack. */
15848 for (i = nargs - 1; i >= 0; --i)
15850 dw_loc_descr_ref loc_descr
15851 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15852 context);
15854 if (loc_descr == NULL)
15855 goto call_expansion_failed;
15857 add_loc_descr (&ret, loc_descr);
15860 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15861 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15862 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15863 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15864 add_loc_descr (&ret, ret1);
15865 break;
15867 call_expansion_failed:
15868 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15869 /* There are no opcodes for these operations. */
15870 return 0;
15873 case PREINCREMENT_EXPR:
15874 case PREDECREMENT_EXPR:
15875 case POSTINCREMENT_EXPR:
15876 case POSTDECREMENT_EXPR:
15877 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15878 /* There are no opcodes for these operations. */
15879 return 0;
15881 case ADDR_EXPR:
15882 /* If we already want an address, see if there is INDIRECT_REF inside
15883 e.g. for &this->field. */
15884 if (want_address)
15886 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15887 (loc, want_address == 2, context);
15888 if (list_ret)
15889 have_address = 1;
15890 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15891 && (ret = cst_pool_loc_descr (loc)))
15892 have_address = 1;
15894 /* Otherwise, process the argument and look for the address. */
15895 if (!list_ret && !ret)
15896 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15897 else
15899 if (want_address)
15900 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15901 return NULL;
15903 break;
15905 case VAR_DECL:
15906 if (DECL_THREAD_LOCAL_P (loc))
15908 rtx rtl;
15909 enum dwarf_location_atom tls_op;
15910 enum dtprel_bool dtprel = dtprel_false;
15912 if (targetm.have_tls)
15914 /* If this is not defined, we have no way to emit the
15915 data. */
15916 if (!targetm.asm_out.output_dwarf_dtprel)
15917 return 0;
15919 /* The way DW_OP_GNU_push_tls_address is specified, we
15920 can only look up addresses of objects in the current
15921 module. We used DW_OP_addr as first op, but that's
15922 wrong, because DW_OP_addr is relocated by the debug
15923 info consumer, while DW_OP_GNU_push_tls_address
15924 operand shouldn't be. */
15925 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15926 return 0;
15927 dtprel = dtprel_true;
15928 tls_op = DW_OP_GNU_push_tls_address;
15930 else
15932 if (!targetm.emutls.debug_form_tls_address
15933 || !(dwarf_version >= 3 || !dwarf_strict))
15934 return 0;
15935 /* We stuffed the control variable into the DECL_VALUE_EXPR
15936 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15937 no longer appear in gimple code. We used the control
15938 variable in specific so that we could pick it up here. */
15939 loc = DECL_VALUE_EXPR (loc);
15940 tls_op = DW_OP_form_tls_address;
15943 rtl = rtl_for_decl_location (loc);
15944 if (rtl == NULL_RTX)
15945 return 0;
15947 if (!MEM_P (rtl))
15948 return 0;
15949 rtl = XEXP (rtl, 0);
15950 if (! CONSTANT_P (rtl))
15951 return 0;
15953 ret = new_addr_loc_descr (rtl, dtprel);
15954 ret1 = new_loc_descr (tls_op, 0, 0);
15955 add_loc_descr (&ret, ret1);
15957 have_address = 1;
15958 break;
15960 /* FALLTHRU */
15962 case PARM_DECL:
15963 if (context != NULL && context->dpi != NULL
15964 && DECL_CONTEXT (loc) == context->dpi->fndecl)
15966 /* We are generating code for a DWARF procedure and we want to access
15967 one of its arguments: find the appropriate argument offset and let
15968 the resolve_args_picking pass compute the offset that complies
15969 with the stack frame size. */
15970 unsigned i = 0;
15971 tree cursor;
15973 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15974 cursor != NULL_TREE && cursor != loc;
15975 cursor = TREE_CHAIN (cursor), ++i)
15977 /* If we are translating a DWARF procedure, all referenced parameters
15978 must belong to the current function. */
15979 gcc_assert (cursor != NULL_TREE);
15981 ret = new_loc_descr (DW_OP_pick, i, 0);
15982 ret->frame_offset_rel = 1;
15983 break;
15985 /* FALLTHRU */
15987 case RESULT_DECL:
15988 if (DECL_HAS_VALUE_EXPR_P (loc))
15989 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15990 want_address, context);
15991 /* FALLTHRU */
15993 case FUNCTION_DECL:
15995 rtx rtl;
15996 var_loc_list *loc_list = lookup_decl_loc (loc);
15998 if (loc_list && loc_list->first)
16000 list_ret = dw_loc_list (loc_list, loc, want_address);
16001 have_address = want_address != 0;
16002 break;
16004 rtl = rtl_for_decl_location (loc);
16005 if (rtl == NULL_RTX)
16007 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
16008 return 0;
16010 else if (CONST_INT_P (rtl))
16012 HOST_WIDE_INT val = INTVAL (rtl);
16013 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16014 val &= GET_MODE_MASK (DECL_MODE (loc));
16015 ret = int_loc_descriptor (val);
16017 else if (GET_CODE (rtl) == CONST_STRING)
16019 expansion_failed (loc, NULL_RTX, "CONST_STRING");
16020 return 0;
16022 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16023 ret = new_addr_loc_descr (rtl, dtprel_false);
16024 else
16026 machine_mode mode, mem_mode;
16028 /* Certain constructs can only be represented at top-level. */
16029 if (want_address == 2)
16031 ret = loc_descriptor (rtl, VOIDmode,
16032 VAR_INIT_STATUS_INITIALIZED);
16033 have_address = 1;
16035 else
16037 mode = GET_MODE (rtl);
16038 mem_mode = VOIDmode;
16039 if (MEM_P (rtl))
16041 mem_mode = mode;
16042 mode = get_address_mode (rtl);
16043 rtl = XEXP (rtl, 0);
16044 have_address = 1;
16046 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16047 VAR_INIT_STATUS_INITIALIZED);
16049 if (!ret)
16050 expansion_failed (loc, rtl,
16051 "failed to produce loc descriptor for rtl");
16054 break;
16056 case MEM_REF:
16057 if (!integer_zerop (TREE_OPERAND (loc, 1)))
16059 have_address = 1;
16060 goto do_plus;
16062 /* Fallthru. */
16063 case INDIRECT_REF:
16064 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16065 have_address = 1;
16066 break;
16068 case TARGET_MEM_REF:
16069 case SSA_NAME:
16070 case DEBUG_EXPR_DECL:
16071 return NULL;
16073 case COMPOUND_EXPR:
16074 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16075 context);
16077 CASE_CONVERT:
16078 case VIEW_CONVERT_EXPR:
16079 case SAVE_EXPR:
16080 case MODIFY_EXPR:
16081 case NON_LVALUE_EXPR:
16082 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16083 context);
16085 case COMPONENT_REF:
16086 case BIT_FIELD_REF:
16087 case ARRAY_REF:
16088 case ARRAY_RANGE_REF:
16089 case REALPART_EXPR:
16090 case IMAGPART_EXPR:
16092 tree obj, offset;
16093 HOST_WIDE_INT bitsize, bitpos, bytepos;
16094 machine_mode mode;
16095 int unsignedp, reversep, volatilep = 0;
16097 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16098 &unsignedp, &reversep, &volatilep);
16100 gcc_assert (obj != loc);
16102 list_ret = loc_list_from_tree_1 (obj,
16103 want_address == 2
16104 && !bitpos && !offset ? 2 : 1,
16105 context);
16106 /* TODO: We can extract value of the small expression via shifting even
16107 for nonzero bitpos. */
16108 if (list_ret == 0)
16109 return 0;
16110 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16112 expansion_failed (loc, NULL_RTX,
16113 "bitfield access");
16114 return 0;
16117 if (offset != NULL_TREE)
16119 /* Variable offset. */
16120 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16121 if (list_ret1 == 0)
16122 return 0;
16123 add_loc_list (&list_ret, list_ret1);
16124 if (!list_ret)
16125 return 0;
16126 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16129 bytepos = bitpos / BITS_PER_UNIT;
16130 if (bytepos > 0)
16131 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16132 else if (bytepos < 0)
16133 loc_list_plus_const (list_ret, bytepos);
16135 have_address = 1;
16136 break;
16139 case INTEGER_CST:
16140 if ((want_address || !tree_fits_shwi_p (loc))
16141 && (ret = cst_pool_loc_descr (loc)))
16142 have_address = 1;
16143 else if (want_address == 2
16144 && tree_fits_shwi_p (loc)
16145 && (ret = address_of_int_loc_descriptor
16146 (int_size_in_bytes (TREE_TYPE (loc)),
16147 tree_to_shwi (loc))))
16148 have_address = 1;
16149 else if (tree_fits_shwi_p (loc))
16150 ret = int_loc_descriptor (tree_to_shwi (loc));
16151 else if (tree_fits_uhwi_p (loc))
16152 ret = uint_loc_descriptor (tree_to_uhwi (loc));
16153 else
16155 expansion_failed (loc, NULL_RTX,
16156 "Integer operand is not host integer");
16157 return 0;
16159 break;
16161 case CONSTRUCTOR:
16162 case REAL_CST:
16163 case STRING_CST:
16164 case COMPLEX_CST:
16165 if ((ret = cst_pool_loc_descr (loc)))
16166 have_address = 1;
16167 else if (TREE_CODE (loc) == CONSTRUCTOR)
16169 tree type = TREE_TYPE (loc);
16170 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
16171 unsigned HOST_WIDE_INT offset = 0;
16172 unsigned HOST_WIDE_INT cnt;
16173 constructor_elt *ce;
16175 if (TREE_CODE (type) == RECORD_TYPE)
16177 /* This is very limited, but it's enough to output
16178 pointers to member functions, as long as the
16179 referenced function is defined in the current
16180 translation unit. */
16181 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
16183 tree val = ce->value;
16185 tree field = ce->index;
16187 if (val)
16188 STRIP_NOPS (val);
16190 if (!field || DECL_BIT_FIELD (field))
16192 expansion_failed (loc, NULL_RTX,
16193 "bitfield in record type constructor");
16194 size = offset = (unsigned HOST_WIDE_INT)-1;
16195 ret = NULL;
16196 break;
16199 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16200 unsigned HOST_WIDE_INT pos = int_byte_position (field);
16201 gcc_assert (pos + fieldsize <= size);
16202 if (pos < offset)
16204 expansion_failed (loc, NULL_RTX,
16205 "out-of-order fields in record constructor");
16206 size = offset = (unsigned HOST_WIDE_INT)-1;
16207 ret = NULL;
16208 break;
16210 if (pos > offset)
16212 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
16213 add_loc_descr (&ret, ret1);
16214 offset = pos;
16216 if (val && fieldsize != 0)
16218 ret1 = loc_descriptor_from_tree (val, want_address, context);
16219 if (!ret1)
16221 expansion_failed (loc, NULL_RTX,
16222 "unsupported expression in field");
16223 size = offset = (unsigned HOST_WIDE_INT)-1;
16224 ret = NULL;
16225 break;
16227 add_loc_descr (&ret, ret1);
16229 if (fieldsize)
16231 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
16232 add_loc_descr (&ret, ret1);
16233 offset = pos + fieldsize;
16237 if (offset != size)
16239 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
16240 add_loc_descr (&ret, ret1);
16241 offset = size;
16244 have_address = !!want_address;
16246 else
16247 expansion_failed (loc, NULL_RTX,
16248 "constructor of non-record type");
16250 else
16251 /* We can construct small constants here using int_loc_descriptor. */
16252 expansion_failed (loc, NULL_RTX,
16253 "constructor or constant not in constant pool");
16254 break;
16256 case TRUTH_AND_EXPR:
16257 case TRUTH_ANDIF_EXPR:
16258 case BIT_AND_EXPR:
16259 op = DW_OP_and;
16260 goto do_binop;
16262 case TRUTH_XOR_EXPR:
16263 case BIT_XOR_EXPR:
16264 op = DW_OP_xor;
16265 goto do_binop;
16267 case TRUTH_OR_EXPR:
16268 case TRUTH_ORIF_EXPR:
16269 case BIT_IOR_EXPR:
16270 op = DW_OP_or;
16271 goto do_binop;
16273 case FLOOR_DIV_EXPR:
16274 case CEIL_DIV_EXPR:
16275 case ROUND_DIV_EXPR:
16276 case TRUNC_DIV_EXPR:
16277 case EXACT_DIV_EXPR:
16278 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16279 return 0;
16280 op = DW_OP_div;
16281 goto do_binop;
16283 case MINUS_EXPR:
16284 op = DW_OP_minus;
16285 goto do_binop;
16287 case FLOOR_MOD_EXPR:
16288 case CEIL_MOD_EXPR:
16289 case ROUND_MOD_EXPR:
16290 case TRUNC_MOD_EXPR:
16291 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16293 op = DW_OP_mod;
16294 goto do_binop;
16296 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16297 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16298 if (list_ret == 0 || list_ret1 == 0)
16299 return 0;
16301 add_loc_list (&list_ret, list_ret1);
16302 if (list_ret == 0)
16303 return 0;
16304 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16305 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16306 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16307 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16308 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16309 break;
16311 case MULT_EXPR:
16312 op = DW_OP_mul;
16313 goto do_binop;
16315 case LSHIFT_EXPR:
16316 op = DW_OP_shl;
16317 goto do_binop;
16319 case RSHIFT_EXPR:
16320 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16321 goto do_binop;
16323 case POINTER_PLUS_EXPR:
16324 case PLUS_EXPR:
16325 do_plus:
16326 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16328 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16329 smarter to encode their opposite. The DW_OP_plus_uconst operation
16330 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
16331 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16332 bytes, Y being the size of the operation that pushes the opposite
16333 of the addend. So let's choose the smallest representation. */
16334 const tree tree_addend = TREE_OPERAND (loc, 1);
16335 offset_int wi_addend;
16336 HOST_WIDE_INT shwi_addend;
16337 dw_loc_descr_ref loc_naddend;
16339 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16340 if (list_ret == 0)
16341 return 0;
16343 /* Try to get the literal to push. It is the opposite of the addend,
16344 so as we rely on wrapping during DWARF evaluation, first decode
16345 the literal as a "DWARF-sized" signed number. */
16346 wi_addend = wi::to_offset (tree_addend);
16347 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16348 shwi_addend = wi_addend.to_shwi ();
16349 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16350 ? int_loc_descriptor (-shwi_addend)
16351 : NULL;
16353 if (loc_naddend != NULL
16354 && ((unsigned) size_of_uleb128 (shwi_addend)
16355 > size_of_loc_descr (loc_naddend)))
16357 add_loc_descr_to_each (list_ret, loc_naddend);
16358 add_loc_descr_to_each (list_ret,
16359 new_loc_descr (DW_OP_minus, 0, 0));
16361 else
16363 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16365 loc_naddend = loc_cur;
16366 loc_cur = loc_cur->dw_loc_next;
16367 ggc_free (loc_naddend);
16369 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16371 break;
16374 op = DW_OP_plus;
16375 goto do_binop;
16377 case LE_EXPR:
16378 op = DW_OP_le;
16379 goto do_comp_binop;
16381 case GE_EXPR:
16382 op = DW_OP_ge;
16383 goto do_comp_binop;
16385 case LT_EXPR:
16386 op = DW_OP_lt;
16387 goto do_comp_binop;
16389 case GT_EXPR:
16390 op = DW_OP_gt;
16391 goto do_comp_binop;
16393 do_comp_binop:
16394 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16396 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16397 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16398 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16399 TREE_CODE (loc));
16400 break;
16402 else
16403 goto do_binop;
16405 case EQ_EXPR:
16406 op = DW_OP_eq;
16407 goto do_binop;
16409 case NE_EXPR:
16410 op = DW_OP_ne;
16411 goto do_binop;
16413 do_binop:
16414 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16415 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16416 if (list_ret == 0 || list_ret1 == 0)
16417 return 0;
16419 add_loc_list (&list_ret, list_ret1);
16420 if (list_ret == 0)
16421 return 0;
16422 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16423 break;
16425 case TRUTH_NOT_EXPR:
16426 case BIT_NOT_EXPR:
16427 op = DW_OP_not;
16428 goto do_unop;
16430 case ABS_EXPR:
16431 op = DW_OP_abs;
16432 goto do_unop;
16434 case NEGATE_EXPR:
16435 op = DW_OP_neg;
16436 goto do_unop;
16438 do_unop:
16439 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16440 if (list_ret == 0)
16441 return 0;
16443 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16444 break;
16446 case MIN_EXPR:
16447 case MAX_EXPR:
16449 const enum tree_code code =
16450 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16452 loc = build3 (COND_EXPR, TREE_TYPE (loc),
16453 build2 (code, integer_type_node,
16454 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16455 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16458 /* fall through */
16460 case COND_EXPR:
16462 dw_loc_descr_ref lhs
16463 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16464 dw_loc_list_ref rhs
16465 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16466 dw_loc_descr_ref bra_node, jump_node, tmp;
16468 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16469 if (list_ret == 0 || lhs == 0 || rhs == 0)
16470 return 0;
16472 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16473 add_loc_descr_to_each (list_ret, bra_node);
16475 add_loc_list (&list_ret, rhs);
16476 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16477 add_loc_descr_to_each (list_ret, jump_node);
16479 add_loc_descr_to_each (list_ret, lhs);
16480 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16481 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16483 /* ??? Need a node to point the skip at. Use a nop. */
16484 tmp = new_loc_descr (DW_OP_nop, 0, 0);
16485 add_loc_descr_to_each (list_ret, tmp);
16486 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16487 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16489 break;
16491 case FIX_TRUNC_EXPR:
16492 return 0;
16494 default:
16495 /* Leave front-end specific codes as simply unknown. This comes
16496 up, for instance, with the C STMT_EXPR. */
16497 if ((unsigned int) TREE_CODE (loc)
16498 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16500 expansion_failed (loc, NULL_RTX,
16501 "language specific tree node");
16502 return 0;
16505 /* Otherwise this is a generic code; we should just lists all of
16506 these explicitly. We forgot one. */
16507 if (flag_checking)
16508 gcc_unreachable ();
16510 /* In a release build, we want to degrade gracefully: better to
16511 generate incomplete debugging information than to crash. */
16512 return NULL;
16515 if (!ret && !list_ret)
16516 return 0;
16518 if (want_address == 2 && !have_address
16519 && (dwarf_version >= 4 || !dwarf_strict))
16521 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16523 expansion_failed (loc, NULL_RTX,
16524 "DWARF address size mismatch");
16525 return 0;
16527 if (ret)
16528 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16529 else
16530 add_loc_descr_to_each (list_ret,
16531 new_loc_descr (DW_OP_stack_value, 0, 0));
16532 have_address = 1;
16534 /* Show if we can't fill the request for an address. */
16535 if (want_address && !have_address)
16537 expansion_failed (loc, NULL_RTX,
16538 "Want address and only have value");
16539 return 0;
16542 gcc_assert (!ret || !list_ret);
16544 /* If we've got an address and don't want one, dereference. */
16545 if (!want_address && have_address)
16547 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16549 if (size > DWARF2_ADDR_SIZE || size == -1)
16551 expansion_failed (loc, NULL_RTX,
16552 "DWARF address size mismatch");
16553 return 0;
16555 else if (size == DWARF2_ADDR_SIZE)
16556 op = DW_OP_deref;
16557 else
16558 op = DW_OP_deref_size;
16560 if (ret)
16561 add_loc_descr (&ret, new_loc_descr (op, size, 0));
16562 else
16563 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16565 if (ret)
16566 list_ret = new_loc_list (ret, NULL, NULL, NULL);
16568 return list_ret;
16571 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16572 expressions. */
16574 static dw_loc_list_ref
16575 loc_list_from_tree (tree loc, int want_address,
16576 const struct loc_descr_context *context)
16578 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16580 for (dw_loc_list_ref loc_cur = result;
16581 loc_cur != NULL; loc_cur =
16582 loc_cur->dw_loc_next)
16583 loc_descr_without_nops (loc_cur->expr);
16584 return result;
16587 /* Same as above but return only single location expression. */
16588 static dw_loc_descr_ref
16589 loc_descriptor_from_tree (tree loc, int want_address,
16590 const struct loc_descr_context *context)
16592 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16593 if (!ret)
16594 return NULL;
16595 if (ret->dw_loc_next)
16597 expansion_failed (loc, NULL_RTX,
16598 "Location list where only loc descriptor needed");
16599 return NULL;
16601 return ret->expr;
16604 /* Given a value, round it up to the lowest multiple of `boundary'
16605 which is not less than the value itself. */
16607 static inline HOST_WIDE_INT
16608 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16610 return (((value + boundary - 1) / boundary) * boundary);
16613 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16614 pointer to the declared type for the relevant field variable, or return
16615 `integer_type_node' if the given node turns out to be an
16616 ERROR_MARK node. */
16618 static inline tree
16619 field_type (const_tree decl)
16621 tree type;
16623 if (TREE_CODE (decl) == ERROR_MARK)
16624 return integer_type_node;
16626 type = DECL_BIT_FIELD_TYPE (decl);
16627 if (type == NULL_TREE)
16628 type = TREE_TYPE (decl);
16630 return type;
16633 /* Given a pointer to a tree node, return the alignment in bits for
16634 it, or else return BITS_PER_WORD if the node actually turns out to
16635 be an ERROR_MARK node. */
16637 static inline unsigned
16638 simple_type_align_in_bits (const_tree type)
16640 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16643 static inline unsigned
16644 simple_decl_align_in_bits (const_tree decl)
16646 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16649 /* Return the result of rounding T up to ALIGN. */
16651 static inline offset_int
16652 round_up_to_align (const offset_int &t, unsigned int align)
16654 return wi::udiv_trunc (t + align - 1, align) * align;
16657 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
16658 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
16659 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
16660 if we fail to return the size in one of these two forms. */
16662 static dw_loc_descr_ref
16663 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16665 tree tree_size;
16666 struct loc_descr_context ctx;
16668 /* Return a constant integer in priority, if possible. */
16669 *cst_size = int_size_in_bytes (type);
16670 if (*cst_size != -1)
16671 return NULL;
16673 ctx.context_type = const_cast<tree> (type);
16674 ctx.base_decl = NULL_TREE;
16675 ctx.dpi = NULL;
16677 type = TYPE_MAIN_VARIANT (type);
16678 tree_size = TYPE_SIZE_UNIT (type);
16679 return ((tree_size != NULL_TREE)
16680 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16681 : NULL);
16684 /* Helper structure for RECORD_TYPE processing. */
16685 struct vlr_context
16687 /* Root RECORD_TYPE. It is needed to generate data member location
16688 descriptions in variable-length records (VLR), but also to cope with
16689 variants, which are composed of nested structures multiplexed with
16690 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
16691 function processing a FIELD_DECL, it is required to be non null. */
16692 tree struct_type;
16693 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16694 QUAL_UNION_TYPE), this holds an expression that computes the offset for
16695 this variant part as part of the root record (in storage units). For
16696 regular records, it must be NULL_TREE. */
16697 tree variant_part_offset;
16700 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16701 addressed byte of the "containing object" for the given FIELD_DECL. If
16702 possible, return a native constant through CST_OFFSET (in which case NULL is
16703 returned); otherwise return a DWARF expression that computes the offset.
16705 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16706 that offset is, either because the argument turns out to be a pointer to an
16707 ERROR_MARK node, or because the offset expression is too complex for us.
16709 CTX is required: see the comment for VLR_CONTEXT. */
16711 static dw_loc_descr_ref
16712 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16713 HOST_WIDE_INT *cst_offset)
16715 offset_int object_offset_in_bits;
16716 offset_int object_offset_in_bytes;
16717 offset_int bitpos_int;
16718 bool is_byte_offset_cst, is_bit_offset_cst;
16719 tree tree_result;
16720 dw_loc_list_ref loc_result;
16722 *cst_offset = 0;
16724 if (TREE_CODE (decl) == ERROR_MARK)
16725 return NULL;
16726 else
16727 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16729 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16730 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16732 /* We cannot handle variable bit offsets at the moment, so abort if it's the
16733 case. */
16734 if (is_bit_offset_cst)
16735 return NULL;
16737 #ifdef PCC_BITFIELD_TYPE_MATTERS
16738 /* We used to handle only constant offsets in all cases. Now, we handle
16739 properly dynamic byte offsets only when PCC bitfield type doesn't
16740 matter. */
16741 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16743 tree type;
16744 tree field_size_tree;
16745 offset_int deepest_bitpos;
16746 offset_int field_size_in_bits;
16747 unsigned int type_align_in_bits;
16748 unsigned int decl_align_in_bits;
16749 offset_int type_size_in_bits;
16751 bitpos_int = wi::to_offset (bit_position (decl));
16752 type = field_type (decl);
16753 type_size_in_bits = offset_int_type_size_in_bits (type);
16754 type_align_in_bits = simple_type_align_in_bits (type);
16756 field_size_tree = DECL_SIZE (decl);
16758 /* The size could be unspecified if there was an error, or for
16759 a flexible array member. */
16760 if (!field_size_tree)
16761 field_size_tree = bitsize_zero_node;
16763 /* If the size of the field is not constant, use the type size. */
16764 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16765 field_size_in_bits = wi::to_offset (field_size_tree);
16766 else
16767 field_size_in_bits = type_size_in_bits;
16769 decl_align_in_bits = simple_decl_align_in_bits (decl);
16771 /* The GCC front-end doesn't make any attempt to keep track of the
16772 starting bit offset (relative to the start of the containing
16773 structure type) of the hypothetical "containing object" for a
16774 bit-field. Thus, when computing the byte offset value for the
16775 start of the "containing object" of a bit-field, we must deduce
16776 this information on our own. This can be rather tricky to do in
16777 some cases. For example, handling the following structure type
16778 definition when compiling for an i386/i486 target (which only
16779 aligns long long's to 32-bit boundaries) can be very tricky:
16781 struct S { int field1; long long field2:31; };
16783 Fortunately, there is a simple rule-of-thumb which can be used
16784 in such cases. When compiling for an i386/i486, GCC will
16785 allocate 8 bytes for the structure shown above. It decides to
16786 do this based upon one simple rule for bit-field allocation.
16787 GCC allocates each "containing object" for each bit-field at
16788 the first (i.e. lowest addressed) legitimate alignment boundary
16789 (based upon the required minimum alignment for the declared
16790 type of the field) which it can possibly use, subject to the
16791 condition that there is still enough available space remaining
16792 in the containing object (when allocated at the selected point)
16793 to fully accommodate all of the bits of the bit-field itself.
16795 This simple rule makes it obvious why GCC allocates 8 bytes for
16796 each object of the structure type shown above. When looking
16797 for a place to allocate the "containing object" for `field2',
16798 the compiler simply tries to allocate a 64-bit "containing
16799 object" at each successive 32-bit boundary (starting at zero)
16800 until it finds a place to allocate that 64- bit field such that
16801 at least 31 contiguous (and previously unallocated) bits remain
16802 within that selected 64 bit field. (As it turns out, for the
16803 example above, the compiler finds it is OK to allocate the
16804 "containing object" 64-bit field at bit-offset zero within the
16805 structure type.)
16807 Here we attempt to work backwards from the limited set of facts
16808 we're given, and we try to deduce from those facts, where GCC
16809 must have believed that the containing object started (within
16810 the structure type). The value we deduce is then used (by the
16811 callers of this routine) to generate DW_AT_location and
16812 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16813 the case of DW_AT_location, regular fields as well). */
16815 /* Figure out the bit-distance from the start of the structure to
16816 the "deepest" bit of the bit-field. */
16817 deepest_bitpos = bitpos_int + field_size_in_bits;
16819 /* This is the tricky part. Use some fancy footwork to deduce
16820 where the lowest addressed bit of the containing object must
16821 be. */
16822 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16824 /* Round up to type_align by default. This works best for
16825 bitfields. */
16826 object_offset_in_bits
16827 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16829 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16831 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16833 /* Round up to decl_align instead. */
16834 object_offset_in_bits
16835 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16838 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16840 tree_result = byte_position (decl);
16841 if (ctx->variant_part_offset != NULL_TREE)
16842 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16843 ctx->variant_part_offset, tree_result));
16845 /* If the byte offset is a constant, it's simplier to handle a native
16846 constant rather than a DWARF expression. */
16847 if (TREE_CODE (tree_result) == INTEGER_CST)
16849 *cst_offset = wi::to_offset (tree_result).to_shwi ();
16850 return NULL;
16852 struct loc_descr_context loc_ctx = {
16853 ctx->struct_type, /* context_type */
16854 NULL_TREE, /* base_decl */
16855 NULL /* dpi */
16857 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16859 /* We want a DWARF expression: abort if we only have a location list with
16860 multiple elements. */
16861 if (!loc_result || !single_element_loc_list_p (loc_result))
16862 return NULL;
16863 else
16864 return loc_result->expr;
16867 /* The following routines define various Dwarf attributes and any data
16868 associated with them. */
16870 /* Add a location description attribute value to a DIE.
16872 This emits location attributes suitable for whole variables and
16873 whole parameters. Note that the location attributes for struct fields are
16874 generated by the routine `data_member_location_attribute' below. */
16876 static inline void
16877 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16878 dw_loc_list_ref descr)
16880 if (descr == 0)
16881 return;
16882 if (single_element_loc_list_p (descr))
16883 add_AT_loc (die, attr_kind, descr->expr);
16884 else
16885 add_AT_loc_list (die, attr_kind, descr);
16888 /* Add DW_AT_accessibility attribute to DIE if needed. */
16890 static void
16891 add_accessibility_attribute (dw_die_ref die, tree decl)
16893 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16894 children, otherwise the default is DW_ACCESS_public. In DWARF2
16895 the default has always been DW_ACCESS_public. */
16896 if (TREE_PROTECTED (decl))
16897 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16898 else if (TREE_PRIVATE (decl))
16900 if (dwarf_version == 2
16901 || die->die_parent == NULL
16902 || die->die_parent->die_tag != DW_TAG_class_type)
16903 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16905 else if (dwarf_version > 2
16906 && die->die_parent
16907 && die->die_parent->die_tag == DW_TAG_class_type)
16908 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16911 /* Attach the specialized form of location attribute used for data members of
16912 struct and union types. In the special case of a FIELD_DECL node which
16913 represents a bit-field, the "offset" part of this special location
16914 descriptor must indicate the distance in bytes from the lowest-addressed
16915 byte of the containing struct or union type to the lowest-addressed byte of
16916 the "containing object" for the bit-field. (See the `field_byte_offset'
16917 function above).
16919 For any given bit-field, the "containing object" is a hypothetical object
16920 (of some integral or enum type) within which the given bit-field lives. The
16921 type of this hypothetical "containing object" is always the same as the
16922 declared type of the individual bit-field itself (for GCC anyway... the
16923 DWARF spec doesn't actually mandate this). Note that it is the size (in
16924 bytes) of the hypothetical "containing object" which will be given in the
16925 DW_AT_byte_size attribute for this bit-field. (See the
16926 `byte_size_attribute' function below.) It is also used when calculating the
16927 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16928 function below.)
16930 CTX is required: see the comment for VLR_CONTEXT. */
16932 static void
16933 add_data_member_location_attribute (dw_die_ref die,
16934 tree decl,
16935 struct vlr_context *ctx)
16937 HOST_WIDE_INT offset;
16938 dw_loc_descr_ref loc_descr = 0;
16940 if (TREE_CODE (decl) == TREE_BINFO)
16942 /* We're working on the TAG_inheritance for a base class. */
16943 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16945 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16946 aren't at a fixed offset from all (sub)objects of the same
16947 type. We need to extract the appropriate offset from our
16948 vtable. The following dwarf expression means
16950 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16952 This is specific to the V3 ABI, of course. */
16954 dw_loc_descr_ref tmp;
16956 /* Make a copy of the object address. */
16957 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16958 add_loc_descr (&loc_descr, tmp);
16960 /* Extract the vtable address. */
16961 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16962 add_loc_descr (&loc_descr, tmp);
16964 /* Calculate the address of the offset. */
16965 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16966 gcc_assert (offset < 0);
16968 tmp = int_loc_descriptor (-offset);
16969 add_loc_descr (&loc_descr, tmp);
16970 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16971 add_loc_descr (&loc_descr, tmp);
16973 /* Extract the offset. */
16974 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16975 add_loc_descr (&loc_descr, tmp);
16977 /* Add it to the object address. */
16978 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16979 add_loc_descr (&loc_descr, tmp);
16981 else
16982 offset = tree_to_shwi (BINFO_OFFSET (decl));
16984 else
16986 loc_descr = field_byte_offset (decl, ctx, &offset);
16988 /* If loc_descr is available then we know the field offset is dynamic.
16989 However, GDB does not handle dynamic field offsets very well at the
16990 moment. */
16991 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16993 loc_descr = NULL;
16994 offset = 0;
16997 /* Data member location evalutation starts with the base address on the
16998 stack. Compute the field offset and add it to this base address. */
16999 else if (loc_descr != NULL)
17000 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
17003 if (! loc_descr)
17005 if (dwarf_version > 2)
17007 /* Don't need to output a location expression, just the constant. */
17008 if (offset < 0)
17009 add_AT_int (die, DW_AT_data_member_location, offset);
17010 else
17011 add_AT_unsigned (die, DW_AT_data_member_location, offset);
17012 return;
17014 else
17016 enum dwarf_location_atom op;
17018 /* The DWARF2 standard says that we should assume that the structure
17019 address is already on the stack, so we can specify a structure
17020 field address by using DW_OP_plus_uconst. */
17021 op = DW_OP_plus_uconst;
17022 loc_descr = new_loc_descr (op, offset, 0);
17026 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17029 /* Writes integer values to dw_vec_const array. */
17031 static void
17032 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17034 while (size != 0)
17036 *dest++ = val & 0xff;
17037 val >>= 8;
17038 --size;
17042 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
17044 static HOST_WIDE_INT
17045 extract_int (const unsigned char *src, unsigned int size)
17047 HOST_WIDE_INT val = 0;
17049 src += size;
17050 while (size != 0)
17052 val <<= 8;
17053 val |= *--src & 0xff;
17054 --size;
17056 return val;
17059 /* Writes wide_int values to dw_vec_const array. */
17061 static void
17062 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
17064 int i;
17066 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
17068 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
17069 return;
17072 /* We'd have to extend this code to support odd sizes. */
17073 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
17075 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
17077 if (WORDS_BIG_ENDIAN)
17078 for (i = n - 1; i >= 0; i--)
17080 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17081 dest += sizeof (HOST_WIDE_INT);
17083 else
17084 for (i = 0; i < n; i++)
17086 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17087 dest += sizeof (HOST_WIDE_INT);
17091 /* Writes floating point values to dw_vec_const array. */
17093 static void
17094 insert_float (const_rtx rtl, unsigned char *array)
17096 long val[4];
17097 int i;
17099 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
17101 /* real_to_target puts 32-bit pieces in each long. Pack them. */
17102 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17104 insert_int (val[i], 4, array);
17105 array += 4;
17109 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17110 does not have a "location" either in memory or in a register. These
17111 things can arise in GNU C when a constant is passed as an actual parameter
17112 to an inlined function. They can also arise in C++ where declared
17113 constants do not necessarily get memory "homes". */
17115 static bool
17116 add_const_value_attribute (dw_die_ref die, rtx rtl)
17118 switch (GET_CODE (rtl))
17120 case CONST_INT:
17122 HOST_WIDE_INT val = INTVAL (rtl);
17124 if (val < 0)
17125 add_AT_int (die, DW_AT_const_value, val);
17126 else
17127 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17129 return true;
17131 case CONST_WIDE_INT:
17133 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17134 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17135 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17136 wide_int w = wi::zext (w1, prec);
17137 add_AT_wide (die, DW_AT_const_value, w);
17139 return true;
17141 case CONST_DOUBLE:
17142 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17143 floating-point constant. A CONST_DOUBLE is used whenever the
17144 constant requires more than one word in order to be adequately
17145 represented. */
17147 machine_mode mode = GET_MODE (rtl);
17149 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17150 add_AT_double (die, DW_AT_const_value,
17151 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17152 else
17154 unsigned int length = GET_MODE_SIZE (mode);
17155 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17157 insert_float (rtl, array);
17158 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17161 return true;
17163 case CONST_VECTOR:
17165 machine_mode mode = GET_MODE (rtl);
17166 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17167 unsigned int length = CONST_VECTOR_NUNITS (rtl);
17168 unsigned char *array
17169 = ggc_vec_alloc<unsigned char> (length * elt_size);
17170 unsigned int i;
17171 unsigned char *p;
17172 machine_mode imode = GET_MODE_INNER (mode);
17174 switch (GET_MODE_CLASS (mode))
17176 case MODE_VECTOR_INT:
17177 for (i = 0, p = array; i < length; i++, p += elt_size)
17179 rtx elt = CONST_VECTOR_ELT (rtl, i);
17180 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17182 break;
17184 case MODE_VECTOR_FLOAT:
17185 for (i = 0, p = array; i < length; i++, p += elt_size)
17187 rtx elt = CONST_VECTOR_ELT (rtl, i);
17188 insert_float (elt, p);
17190 break;
17192 default:
17193 gcc_unreachable ();
17196 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17198 return true;
17200 case CONST_STRING:
17201 if (dwarf_version >= 4 || !dwarf_strict)
17203 dw_loc_descr_ref loc_result;
17204 resolve_one_addr (&rtl);
17205 rtl_addr:
17206 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17207 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17208 add_AT_loc (die, DW_AT_location, loc_result);
17209 vec_safe_push (used_rtx_array, rtl);
17210 return true;
17212 return false;
17214 case CONST:
17215 if (CONSTANT_P (XEXP (rtl, 0)))
17216 return add_const_value_attribute (die, XEXP (rtl, 0));
17217 /* FALLTHROUGH */
17218 case SYMBOL_REF:
17219 if (!const_ok_for_output (rtl))
17220 return false;
17221 /* FALLTHROUGH */
17222 case LABEL_REF:
17223 if (dwarf_version >= 4 || !dwarf_strict)
17224 goto rtl_addr;
17225 return false;
17227 case PLUS:
17228 /* In cases where an inlined instance of an inline function is passed
17229 the address of an `auto' variable (which is local to the caller) we
17230 can get a situation where the DECL_RTL of the artificial local
17231 variable (for the inlining) which acts as a stand-in for the
17232 corresponding formal parameter (of the inline function) will look
17233 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
17234 exactly a compile-time constant expression, but it isn't the address
17235 of the (artificial) local variable either. Rather, it represents the
17236 *value* which the artificial local variable always has during its
17237 lifetime. We currently have no way to represent such quasi-constant
17238 values in Dwarf, so for now we just punt and generate nothing. */
17239 return false;
17241 case HIGH:
17242 case CONST_FIXED:
17243 return false;
17245 case MEM:
17246 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17247 && MEM_READONLY_P (rtl)
17248 && GET_MODE (rtl) == BLKmode)
17250 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17251 return true;
17253 return false;
17255 default:
17256 /* No other kinds of rtx should be possible here. */
17257 gcc_unreachable ();
17259 return false;
17262 /* Determine whether the evaluation of EXPR references any variables
17263 or functions which aren't otherwise used (and therefore may not be
17264 output). */
17265 static tree
17266 reference_to_unused (tree * tp, int * walk_subtrees,
17267 void * data ATTRIBUTE_UNUSED)
17269 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17270 *walk_subtrees = 0;
17272 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17273 && ! TREE_ASM_WRITTEN (*tp))
17274 return *tp;
17275 /* ??? The C++ FE emits debug information for using decls, so
17276 putting gcc_unreachable here falls over. See PR31899. For now
17277 be conservative. */
17278 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
17279 return *tp;
17280 else if (VAR_P (*tp))
17282 varpool_node *node = varpool_node::get (*tp);
17283 if (!node || !node->definition)
17284 return *tp;
17286 else if (TREE_CODE (*tp) == FUNCTION_DECL
17287 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17289 /* The call graph machinery must have finished analyzing,
17290 optimizing and gimplifying the CU by now.
17291 So if *TP has no call graph node associated
17292 to it, it means *TP will not be emitted. */
17293 if (!cgraph_node::get (*tp))
17294 return *tp;
17296 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17297 return *tp;
17299 return NULL_TREE;
17302 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17303 for use in a later add_const_value_attribute call. */
17305 static rtx
17306 rtl_for_decl_init (tree init, tree type)
17308 rtx rtl = NULL_RTX;
17310 STRIP_NOPS (init);
17312 /* If a variable is initialized with a string constant without embedded
17313 zeros, build CONST_STRING. */
17314 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17316 tree enttype = TREE_TYPE (type);
17317 tree domain = TYPE_DOMAIN (type);
17318 machine_mode mode = TYPE_MODE (enttype);
17320 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17321 && domain
17322 && integer_zerop (TYPE_MIN_VALUE (domain))
17323 && compare_tree_int (TYPE_MAX_VALUE (domain),
17324 TREE_STRING_LENGTH (init) - 1) == 0
17325 && ((size_t) TREE_STRING_LENGTH (init)
17326 == strlen (TREE_STRING_POINTER (init)) + 1))
17328 rtl = gen_rtx_CONST_STRING (VOIDmode,
17329 ggc_strdup (TREE_STRING_POINTER (init)));
17330 rtl = gen_rtx_MEM (BLKmode, rtl);
17331 MEM_READONLY_P (rtl) = 1;
17334 /* Other aggregates, and complex values, could be represented using
17335 CONCAT: FIXME! */
17336 else if (AGGREGATE_TYPE_P (type)
17337 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17338 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17339 || TREE_CODE (type) == COMPLEX_TYPE)
17341 /* Vectors only work if their mode is supported by the target.
17342 FIXME: generic vectors ought to work too. */
17343 else if (TREE_CODE (type) == VECTOR_TYPE
17344 && !VECTOR_MODE_P (TYPE_MODE (type)))
17346 /* If the initializer is something that we know will expand into an
17347 immediate RTL constant, expand it now. We must be careful not to
17348 reference variables which won't be output. */
17349 else if (initializer_constant_valid_p (init, type)
17350 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17352 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17353 possible. */
17354 if (TREE_CODE (type) == VECTOR_TYPE)
17355 switch (TREE_CODE (init))
17357 case VECTOR_CST:
17358 break;
17359 case CONSTRUCTOR:
17360 if (TREE_CONSTANT (init))
17362 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17363 bool constant_p = true;
17364 tree value;
17365 unsigned HOST_WIDE_INT ix;
17367 /* Even when ctor is constant, it might contain non-*_CST
17368 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17369 belong into VECTOR_CST nodes. */
17370 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17371 if (!CONSTANT_CLASS_P (value))
17373 constant_p = false;
17374 break;
17377 if (constant_p)
17379 init = build_vector_from_ctor (type, elts);
17380 break;
17383 /* FALLTHRU */
17385 default:
17386 return NULL;
17389 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17391 /* If expand_expr returns a MEM, it wasn't immediate. */
17392 gcc_assert (!rtl || !MEM_P (rtl));
17395 return rtl;
17398 /* Generate RTL for the variable DECL to represent its location. */
17400 static rtx
17401 rtl_for_decl_location (tree decl)
17403 rtx rtl;
17405 /* Here we have to decide where we are going to say the parameter "lives"
17406 (as far as the debugger is concerned). We only have a couple of
17407 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17409 DECL_RTL normally indicates where the parameter lives during most of the
17410 activation of the function. If optimization is enabled however, this
17411 could be either NULL or else a pseudo-reg. Both of those cases indicate
17412 that the parameter doesn't really live anywhere (as far as the code
17413 generation parts of GCC are concerned) during most of the function's
17414 activation. That will happen (for example) if the parameter is never
17415 referenced within the function.
17417 We could just generate a location descriptor here for all non-NULL
17418 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17419 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17420 where DECL_RTL is NULL or is a pseudo-reg.
17422 Note however that we can only get away with using DECL_INCOMING_RTL as
17423 a backup substitute for DECL_RTL in certain limited cases. In cases
17424 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17425 we can be sure that the parameter was passed using the same type as it is
17426 declared to have within the function, and that its DECL_INCOMING_RTL
17427 points us to a place where a value of that type is passed.
17429 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17430 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17431 because in these cases DECL_INCOMING_RTL points us to a value of some
17432 type which is *different* from the type of the parameter itself. Thus,
17433 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17434 such cases, the debugger would end up (for example) trying to fetch a
17435 `float' from a place which actually contains the first part of a
17436 `double'. That would lead to really incorrect and confusing
17437 output at debug-time.
17439 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17440 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
17441 are a couple of exceptions however. On little-endian machines we can
17442 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17443 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17444 an integral type that is smaller than TREE_TYPE (decl). These cases arise
17445 when (on a little-endian machine) a non-prototyped function has a
17446 parameter declared to be of type `short' or `char'. In such cases,
17447 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17448 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17449 passed `int' value. If the debugger then uses that address to fetch
17450 a `short' or a `char' (on a little-endian machine) the result will be
17451 the correct data, so we allow for such exceptional cases below.
17453 Note that our goal here is to describe the place where the given formal
17454 parameter lives during most of the function's activation (i.e. between the
17455 end of the prologue and the start of the epilogue). We'll do that as best
17456 as we can. Note however that if the given formal parameter is modified
17457 sometime during the execution of the function, then a stack backtrace (at
17458 debug-time) will show the function as having been called with the *new*
17459 value rather than the value which was originally passed in. This happens
17460 rarely enough that it is not a major problem, but it *is* a problem, and
17461 I'd like to fix it.
17463 A future version of dwarf2out.c may generate two additional attributes for
17464 any given DW_TAG_formal_parameter DIE which will describe the "passed
17465 type" and the "passed location" for the given formal parameter in addition
17466 to the attributes we now generate to indicate the "declared type" and the
17467 "active location" for each parameter. This additional set of attributes
17468 could be used by debuggers for stack backtraces. Separately, note that
17469 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17470 This happens (for example) for inlined-instances of inline function formal
17471 parameters which are never referenced. This really shouldn't be
17472 happening. All PARM_DECL nodes should get valid non-NULL
17473 DECL_INCOMING_RTL values. FIXME. */
17475 /* Use DECL_RTL as the "location" unless we find something better. */
17476 rtl = DECL_RTL_IF_SET (decl);
17478 /* When generating abstract instances, ignore everything except
17479 constants, symbols living in memory, and symbols living in
17480 fixed registers. */
17481 if (! reload_completed)
17483 if (rtl
17484 && (CONSTANT_P (rtl)
17485 || (MEM_P (rtl)
17486 && CONSTANT_P (XEXP (rtl, 0)))
17487 || (REG_P (rtl)
17488 && VAR_P (decl)
17489 && TREE_STATIC (decl))))
17491 rtl = targetm.delegitimize_address (rtl);
17492 return rtl;
17494 rtl = NULL_RTX;
17496 else if (TREE_CODE (decl) == PARM_DECL)
17498 if (rtl == NULL_RTX
17499 || is_pseudo_reg (rtl)
17500 || (MEM_P (rtl)
17501 && is_pseudo_reg (XEXP (rtl, 0))
17502 && DECL_INCOMING_RTL (decl)
17503 && MEM_P (DECL_INCOMING_RTL (decl))
17504 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17506 tree declared_type = TREE_TYPE (decl);
17507 tree passed_type = DECL_ARG_TYPE (decl);
17508 machine_mode dmode = TYPE_MODE (declared_type);
17509 machine_mode pmode = TYPE_MODE (passed_type);
17511 /* This decl represents a formal parameter which was optimized out.
17512 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17513 all cases where (rtl == NULL_RTX) just below. */
17514 if (dmode == pmode)
17515 rtl = DECL_INCOMING_RTL (decl);
17516 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17517 && SCALAR_INT_MODE_P (dmode)
17518 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17519 && DECL_INCOMING_RTL (decl))
17521 rtx inc = DECL_INCOMING_RTL (decl);
17522 if (REG_P (inc))
17523 rtl = inc;
17524 else if (MEM_P (inc))
17526 if (BYTES_BIG_ENDIAN)
17527 rtl = adjust_address_nv (inc, dmode,
17528 GET_MODE_SIZE (pmode)
17529 - GET_MODE_SIZE (dmode));
17530 else
17531 rtl = inc;
17536 /* If the parm was passed in registers, but lives on the stack, then
17537 make a big endian correction if the mode of the type of the
17538 parameter is not the same as the mode of the rtl. */
17539 /* ??? This is the same series of checks that are made in dbxout.c before
17540 we reach the big endian correction code there. It isn't clear if all
17541 of these checks are necessary here, but keeping them all is the safe
17542 thing to do. */
17543 else if (MEM_P (rtl)
17544 && XEXP (rtl, 0) != const0_rtx
17545 && ! CONSTANT_P (XEXP (rtl, 0))
17546 /* Not passed in memory. */
17547 && !MEM_P (DECL_INCOMING_RTL (decl))
17548 /* Not passed by invisible reference. */
17549 && (!REG_P (XEXP (rtl, 0))
17550 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17551 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17552 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17553 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17554 #endif
17556 /* Big endian correction check. */
17557 && BYTES_BIG_ENDIAN
17558 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17559 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17560 < UNITS_PER_WORD))
17562 machine_mode addr_mode = get_address_mode (rtl);
17563 int offset = (UNITS_PER_WORD
17564 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17566 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17567 plus_constant (addr_mode, XEXP (rtl, 0), offset));
17570 else if (VAR_P (decl)
17571 && rtl
17572 && MEM_P (rtl)
17573 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17574 && BYTES_BIG_ENDIAN)
17576 machine_mode addr_mode = get_address_mode (rtl);
17577 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17578 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17580 /* If a variable is declared "register" yet is smaller than
17581 a register, then if we store the variable to memory, it
17582 looks like we're storing a register-sized value, when in
17583 fact we are not. We need to adjust the offset of the
17584 storage location to reflect the actual value's bytes,
17585 else gdb will not be able to display it. */
17586 if (rsize > dsize)
17587 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17588 plus_constant (addr_mode, XEXP (rtl, 0),
17589 rsize - dsize));
17592 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17593 and will have been substituted directly into all expressions that use it.
17594 C does not have such a concept, but C++ and other languages do. */
17595 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
17596 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17598 if (rtl)
17599 rtl = targetm.delegitimize_address (rtl);
17601 /* If we don't look past the constant pool, we risk emitting a
17602 reference to a constant pool entry that isn't referenced from
17603 code, and thus is not emitted. */
17604 if (rtl)
17605 rtl = avoid_constant_pool_reference (rtl);
17607 /* Try harder to get a rtl. If this symbol ends up not being emitted
17608 in the current CU, resolve_addr will remove the expression referencing
17609 it. */
17610 if (rtl == NULL_RTX
17611 && VAR_P (decl)
17612 && !DECL_EXTERNAL (decl)
17613 && TREE_STATIC (decl)
17614 && DECL_NAME (decl)
17615 && !DECL_HARD_REGISTER (decl)
17616 && DECL_MODE (decl) != VOIDmode)
17618 rtl = make_decl_rtl_for_debug (decl);
17619 if (!MEM_P (rtl)
17620 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17621 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17622 rtl = NULL_RTX;
17625 return rtl;
17628 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
17629 returned. If so, the decl for the COMMON block is returned, and the
17630 value is the offset into the common block for the symbol. */
17632 static tree
17633 fortran_common (tree decl, HOST_WIDE_INT *value)
17635 tree val_expr, cvar;
17636 machine_mode mode;
17637 HOST_WIDE_INT bitsize, bitpos;
17638 tree offset;
17639 int unsignedp, reversep, volatilep = 0;
17641 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17642 it does not have a value (the offset into the common area), or if it
17643 is thread local (as opposed to global) then it isn't common, and shouldn't
17644 be handled as such. */
17645 if (!VAR_P (decl)
17646 || !TREE_STATIC (decl)
17647 || !DECL_HAS_VALUE_EXPR_P (decl)
17648 || !is_fortran ())
17649 return NULL_TREE;
17651 val_expr = DECL_VALUE_EXPR (decl);
17652 if (TREE_CODE (val_expr) != COMPONENT_REF)
17653 return NULL_TREE;
17655 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17656 &unsignedp, &reversep, &volatilep);
17658 if (cvar == NULL_TREE
17659 || !VAR_P (cvar)
17660 || DECL_ARTIFICIAL (cvar)
17661 || !TREE_PUBLIC (cvar))
17662 return NULL_TREE;
17664 *value = 0;
17665 if (offset != NULL)
17667 if (!tree_fits_shwi_p (offset))
17668 return NULL_TREE;
17669 *value = tree_to_shwi (offset);
17671 if (bitpos != 0)
17672 *value += bitpos / BITS_PER_UNIT;
17674 return cvar;
17677 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17678 data attribute for a variable or a parameter. We generate the
17679 DW_AT_const_value attribute only in those cases where the given variable
17680 or parameter does not have a true "location" either in memory or in a
17681 register. This can happen (for example) when a constant is passed as an
17682 actual argument in a call to an inline function. (It's possible that
17683 these things can crop up in other ways also.) Note that one type of
17684 constant value which can be passed into an inlined function is a constant
17685 pointer. This can happen for example if an actual argument in an inlined
17686 function call evaluates to a compile-time constant address.
17688 CACHE_P is true if it is worth caching the location list for DECL,
17689 so that future calls can reuse it rather than regenerate it from scratch.
17690 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17691 since we will need to refer to them each time the function is inlined. */
17693 static bool
17694 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17696 rtx rtl;
17697 dw_loc_list_ref list;
17698 var_loc_list *loc_list;
17699 cached_dw_loc_list *cache;
17701 if (early_dwarf)
17702 return false;
17704 if (TREE_CODE (decl) == ERROR_MARK)
17705 return false;
17707 if (get_AT (die, DW_AT_location)
17708 || get_AT (die, DW_AT_const_value))
17709 return true;
17711 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
17712 || TREE_CODE (decl) == RESULT_DECL);
17714 /* Try to get some constant RTL for this decl, and use that as the value of
17715 the location. */
17717 rtl = rtl_for_decl_location (decl);
17718 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17719 && add_const_value_attribute (die, rtl))
17720 return true;
17722 /* See if we have single element location list that is equivalent to
17723 a constant value. That way we are better to use add_const_value_attribute
17724 rather than expanding constant value equivalent. */
17725 loc_list = lookup_decl_loc (decl);
17726 if (loc_list
17727 && loc_list->first
17728 && loc_list->first->next == NULL
17729 && NOTE_P (loc_list->first->loc)
17730 && NOTE_VAR_LOCATION (loc_list->first->loc)
17731 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17733 struct var_loc_node *node;
17735 node = loc_list->first;
17736 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17737 if (GET_CODE (rtl) == EXPR_LIST)
17738 rtl = XEXP (rtl, 0);
17739 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17740 && add_const_value_attribute (die, rtl))
17741 return true;
17743 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17744 list several times. See if we've already cached the contents. */
17745 list = NULL;
17746 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17747 cache_p = false;
17748 if (cache_p)
17750 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17751 if (cache)
17752 list = cache->loc_list;
17754 if (list == NULL)
17756 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17757 NULL);
17758 /* It is usually worth caching this result if the decl is from
17759 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17760 if (cache_p && list && list->dw_loc_next)
17762 cached_dw_loc_list **slot
17763 = cached_dw_loc_list_table->find_slot_with_hash (decl,
17764 DECL_UID (decl),
17765 INSERT);
17766 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17767 cache->decl_id = DECL_UID (decl);
17768 cache->loc_list = list;
17769 *slot = cache;
17772 if (list)
17774 add_AT_location_description (die, DW_AT_location, list);
17775 return true;
17777 /* None of that worked, so it must not really have a location;
17778 try adding a constant value attribute from the DECL_INITIAL. */
17779 return tree_add_const_value_attribute_for_decl (die, decl);
17782 /* Helper function for tree_add_const_value_attribute. Natively encode
17783 initializer INIT into an array. Return true if successful. */
17785 static bool
17786 native_encode_initializer (tree init, unsigned char *array, int size)
17788 tree type;
17790 if (init == NULL_TREE)
17791 return false;
17793 STRIP_NOPS (init);
17794 switch (TREE_CODE (init))
17796 case STRING_CST:
17797 type = TREE_TYPE (init);
17798 if (TREE_CODE (type) == ARRAY_TYPE)
17800 tree enttype = TREE_TYPE (type);
17801 machine_mode mode = TYPE_MODE (enttype);
17803 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17804 return false;
17805 if (int_size_in_bytes (type) != size)
17806 return false;
17807 if (size > TREE_STRING_LENGTH (init))
17809 memcpy (array, TREE_STRING_POINTER (init),
17810 TREE_STRING_LENGTH (init));
17811 memset (array + TREE_STRING_LENGTH (init),
17812 '\0', size - TREE_STRING_LENGTH (init));
17814 else
17815 memcpy (array, TREE_STRING_POINTER (init), size);
17816 return true;
17818 return false;
17819 case CONSTRUCTOR:
17820 type = TREE_TYPE (init);
17821 if (int_size_in_bytes (type) != size)
17822 return false;
17823 if (TREE_CODE (type) == ARRAY_TYPE)
17825 HOST_WIDE_INT min_index;
17826 unsigned HOST_WIDE_INT cnt;
17827 int curpos = 0, fieldsize;
17828 constructor_elt *ce;
17830 if (TYPE_DOMAIN (type) == NULL_TREE
17831 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17832 return false;
17834 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17835 if (fieldsize <= 0)
17836 return false;
17838 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17839 memset (array, '\0', size);
17840 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17842 tree val = ce->value;
17843 tree index = ce->index;
17844 int pos = curpos;
17845 if (index && TREE_CODE (index) == RANGE_EXPR)
17846 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17847 * fieldsize;
17848 else if (index)
17849 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17851 if (val)
17853 STRIP_NOPS (val);
17854 if (!native_encode_initializer (val, array + pos, fieldsize))
17855 return false;
17857 curpos = pos + fieldsize;
17858 if (index && TREE_CODE (index) == RANGE_EXPR)
17860 int count = tree_to_shwi (TREE_OPERAND (index, 1))
17861 - tree_to_shwi (TREE_OPERAND (index, 0));
17862 while (count-- > 0)
17864 if (val)
17865 memcpy (array + curpos, array + pos, fieldsize);
17866 curpos += fieldsize;
17869 gcc_assert (curpos <= size);
17871 return true;
17873 else if (TREE_CODE (type) == RECORD_TYPE
17874 || TREE_CODE (type) == UNION_TYPE)
17876 tree field = NULL_TREE;
17877 unsigned HOST_WIDE_INT cnt;
17878 constructor_elt *ce;
17880 if (int_size_in_bytes (type) != size)
17881 return false;
17883 if (TREE_CODE (type) == RECORD_TYPE)
17884 field = TYPE_FIELDS (type);
17886 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17888 tree val = ce->value;
17889 int pos, fieldsize;
17891 if (ce->index != 0)
17892 field = ce->index;
17894 if (val)
17895 STRIP_NOPS (val);
17897 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17898 return false;
17900 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17901 && TYPE_DOMAIN (TREE_TYPE (field))
17902 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17903 return false;
17904 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17905 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17906 return false;
17907 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17908 pos = int_byte_position (field);
17909 gcc_assert (pos + fieldsize <= size);
17910 if (val && fieldsize != 0
17911 && !native_encode_initializer (val, array + pos, fieldsize))
17912 return false;
17914 return true;
17916 return false;
17917 case VIEW_CONVERT_EXPR:
17918 case NON_LVALUE_EXPR:
17919 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17920 default:
17921 return native_encode_expr (init, array, size) == size;
17925 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17926 attribute is the const value T. */
17928 static bool
17929 tree_add_const_value_attribute (dw_die_ref die, tree t)
17931 tree init;
17932 tree type = TREE_TYPE (t);
17933 rtx rtl;
17935 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17936 return false;
17938 init = t;
17939 gcc_assert (!DECL_P (init));
17941 if (! early_dwarf)
17943 rtl = rtl_for_decl_init (init, type);
17944 if (rtl)
17945 return add_const_value_attribute (die, rtl);
17947 /* If the host and target are sane, try harder. */
17948 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17949 && initializer_constant_valid_p (init, type))
17951 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17952 if (size > 0 && (int) size == size)
17954 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17956 if (native_encode_initializer (init, array, size))
17958 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17959 return true;
17961 ggc_free (array);
17964 return false;
17967 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17968 attribute is the const value of T, where T is an integral constant
17969 variable with static storage duration
17970 (so it can't be a PARM_DECL or a RESULT_DECL). */
17972 static bool
17973 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17976 if (!decl
17977 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
17978 || (VAR_P (decl) && !TREE_STATIC (decl)))
17979 return false;
17981 if (TREE_READONLY (decl)
17982 && ! TREE_THIS_VOLATILE (decl)
17983 && DECL_INITIAL (decl))
17984 /* OK */;
17985 else
17986 return false;
17988 /* Don't add DW_AT_const_value if abstract origin already has one. */
17989 if (get_AT (var_die, DW_AT_const_value))
17990 return false;
17992 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17995 /* Convert the CFI instructions for the current function into a
17996 location list. This is used for DW_AT_frame_base when we targeting
17997 a dwarf2 consumer that does not support the dwarf3
17998 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17999 expressions. */
18001 static dw_loc_list_ref
18002 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
18004 int ix;
18005 dw_fde_ref fde;
18006 dw_loc_list_ref list, *list_tail;
18007 dw_cfi_ref cfi;
18008 dw_cfa_location last_cfa, next_cfa;
18009 const char *start_label, *last_label, *section;
18010 dw_cfa_location remember;
18012 fde = cfun->fde;
18013 gcc_assert (fde != NULL);
18015 section = secname_for_decl (current_function_decl);
18016 list_tail = &list;
18017 list = NULL;
18019 memset (&next_cfa, 0, sizeof (next_cfa));
18020 next_cfa.reg = INVALID_REGNUM;
18021 remember = next_cfa;
18023 start_label = fde->dw_fde_begin;
18025 /* ??? Bald assumption that the CIE opcode list does not contain
18026 advance opcodes. */
18027 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
18028 lookup_cfa_1 (cfi, &next_cfa, &remember);
18030 last_cfa = next_cfa;
18031 last_label = start_label;
18033 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18035 /* If the first partition contained no CFI adjustments, the
18036 CIE opcodes apply to the whole first partition. */
18037 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18038 fde->dw_fde_begin, fde->dw_fde_end, section);
18039 list_tail =&(*list_tail)->dw_loc_next;
18040 start_label = last_label = fde->dw_fde_second_begin;
18043 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
18045 switch (cfi->dw_cfi_opc)
18047 case DW_CFA_set_loc:
18048 case DW_CFA_advance_loc1:
18049 case DW_CFA_advance_loc2:
18050 case DW_CFA_advance_loc4:
18051 if (!cfa_equal_p (&last_cfa, &next_cfa))
18053 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18054 start_label, last_label, section);
18056 list_tail = &(*list_tail)->dw_loc_next;
18057 last_cfa = next_cfa;
18058 start_label = last_label;
18060 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18061 break;
18063 case DW_CFA_advance_loc:
18064 /* The encoding is complex enough that we should never emit this. */
18065 gcc_unreachable ();
18067 default:
18068 lookup_cfa_1 (cfi, &next_cfa, &remember);
18069 break;
18071 if (ix + 1 == fde->dw_fde_switch_cfi_index)
18073 if (!cfa_equal_p (&last_cfa, &next_cfa))
18075 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18076 start_label, last_label, section);
18078 list_tail = &(*list_tail)->dw_loc_next;
18079 last_cfa = next_cfa;
18080 start_label = last_label;
18082 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18083 start_label, fde->dw_fde_end, section);
18084 list_tail = &(*list_tail)->dw_loc_next;
18085 start_label = last_label = fde->dw_fde_second_begin;
18089 if (!cfa_equal_p (&last_cfa, &next_cfa))
18091 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18092 start_label, last_label, section);
18093 list_tail = &(*list_tail)->dw_loc_next;
18094 start_label = last_label;
18097 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18098 start_label,
18099 fde->dw_fde_second_begin
18100 ? fde->dw_fde_second_end : fde->dw_fde_end,
18101 section);
18103 if (list && list->dw_loc_next)
18104 gen_llsym (list);
18106 return list;
18109 /* Compute a displacement from the "steady-state frame pointer" to the
18110 frame base (often the same as the CFA), and store it in
18111 frame_pointer_fb_offset. OFFSET is added to the displacement
18112 before the latter is negated. */
18114 static void
18115 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18117 rtx reg, elim;
18119 #ifdef FRAME_POINTER_CFA_OFFSET
18120 reg = frame_pointer_rtx;
18121 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18122 #else
18123 reg = arg_pointer_rtx;
18124 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18125 #endif
18127 elim = (ira_use_lra_p
18128 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18129 : eliminate_regs (reg, VOIDmode, NULL_RTX));
18130 if (GET_CODE (elim) == PLUS)
18132 offset += INTVAL (XEXP (elim, 1));
18133 elim = XEXP (elim, 0);
18136 frame_pointer_fb_offset = -offset;
18138 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18139 in which to eliminate. This is because it's stack pointer isn't
18140 directly accessible as a register within the ISA. To work around
18141 this, assume that while we cannot provide a proper value for
18142 frame_pointer_fb_offset, we won't need one either. */
18143 frame_pointer_fb_offset_valid
18144 = ((SUPPORTS_STACK_ALIGNMENT
18145 && (elim == hard_frame_pointer_rtx
18146 || elim == stack_pointer_rtx))
18147 || elim == (frame_pointer_needed
18148 ? hard_frame_pointer_rtx
18149 : stack_pointer_rtx));
18152 /* Generate a DW_AT_name attribute given some string value to be included as
18153 the value of the attribute. */
18155 static void
18156 add_name_attribute (dw_die_ref die, const char *name_string)
18158 if (name_string != NULL && *name_string != 0)
18160 if (demangle_name_func)
18161 name_string = (*demangle_name_func) (name_string);
18163 add_AT_string (die, DW_AT_name, name_string);
18167 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18168 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18169 of TYPE accordingly.
18171 ??? This is a temporary measure until after we're able to generate
18172 regular DWARF for the complex Ada type system. */
18174 static void
18175 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18176 dw_die_ref context_die)
18178 tree dtype;
18179 dw_die_ref dtype_die;
18181 if (!lang_hooks.types.descriptive_type)
18182 return;
18184 dtype = lang_hooks.types.descriptive_type (type);
18185 if (!dtype)
18186 return;
18188 dtype_die = lookup_type_die (dtype);
18189 if (!dtype_die)
18191 gen_type_die (dtype, context_die);
18192 dtype_die = lookup_type_die (dtype);
18193 gcc_assert (dtype_die);
18196 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18199 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
18201 static const char *
18202 comp_dir_string (void)
18204 const char *wd;
18205 char *wd1;
18206 static const char *cached_wd = NULL;
18208 if (cached_wd != NULL)
18209 return cached_wd;
18211 wd = get_src_pwd ();
18212 if (wd == NULL)
18213 return NULL;
18215 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18217 int wdlen;
18219 wdlen = strlen (wd);
18220 wd1 = ggc_vec_alloc<char> (wdlen + 2);
18221 strcpy (wd1, wd);
18222 wd1 [wdlen] = DIR_SEPARATOR;
18223 wd1 [wdlen + 1] = 0;
18224 wd = wd1;
18227 cached_wd = remap_debug_filename (wd);
18228 return cached_wd;
18231 /* Generate a DW_AT_comp_dir attribute for DIE. */
18233 static void
18234 add_comp_dir_attribute (dw_die_ref die)
18236 const char * wd = comp_dir_string ();
18237 if (wd != NULL)
18238 add_AT_string (die, DW_AT_comp_dir, wd);
18241 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18242 pointer computation, ...), output a representation for that bound according
18243 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
18244 loc_list_from_tree for the meaning of CONTEXT. */
18246 static void
18247 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18248 int forms, const struct loc_descr_context *context)
18250 dw_die_ref context_die, decl_die;
18251 dw_loc_list_ref list;
18253 bool strip_conversions = true;
18255 while (strip_conversions)
18256 switch (TREE_CODE (value))
18258 case ERROR_MARK:
18259 case SAVE_EXPR:
18260 return;
18262 CASE_CONVERT:
18263 case VIEW_CONVERT_EXPR:
18264 value = TREE_OPERAND (value, 0);
18265 break;
18267 default:
18268 strip_conversions = false;
18269 break;
18272 /* If possible and permitted, output the attribute as a constant. */
18273 if ((forms & dw_scalar_form_constant) != 0
18274 && TREE_CODE (value) == INTEGER_CST)
18276 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18278 /* If HOST_WIDE_INT is big enough then represent the bound as
18279 a constant value. We need to choose a form based on
18280 whether the type is signed or unsigned. We cannot just
18281 call add_AT_unsigned if the value itself is positive
18282 (add_AT_unsigned might add the unsigned value encoded as
18283 DW_FORM_data[1248]). Some DWARF consumers will lookup the
18284 bounds type and then sign extend any unsigned values found
18285 for signed types. This is needed only for
18286 DW_AT_{lower,upper}_bound, since for most other attributes,
18287 consumers will treat DW_FORM_data[1248] as unsigned values,
18288 regardless of the underlying type. */
18289 if (prec <= HOST_BITS_PER_WIDE_INT
18290 || tree_fits_uhwi_p (value))
18292 if (TYPE_UNSIGNED (TREE_TYPE (value)))
18293 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18294 else
18295 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18297 else
18298 /* Otherwise represent the bound as an unsigned value with
18299 the precision of its type. The precision and signedness
18300 of the type will be necessary to re-interpret it
18301 unambiguously. */
18302 add_AT_wide (die, attr, value);
18303 return;
18306 /* Otherwise, if it's possible and permitted too, output a reference to
18307 another DIE. */
18308 if ((forms & dw_scalar_form_reference) != 0)
18310 tree decl = NULL_TREE;
18312 /* Some type attributes reference an outer type. For instance, the upper
18313 bound of an array may reference an embedding record (this happens in
18314 Ada). */
18315 if (TREE_CODE (value) == COMPONENT_REF
18316 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18317 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18318 decl = TREE_OPERAND (value, 1);
18320 else if (VAR_P (value)
18321 || TREE_CODE (value) == PARM_DECL
18322 || TREE_CODE (value) == RESULT_DECL)
18323 decl = value;
18325 if (decl != NULL_TREE)
18327 dw_die_ref decl_die = lookup_decl_die (decl);
18329 /* ??? Can this happen, or should the variable have been bound
18330 first? Probably it can, since I imagine that we try to create
18331 the types of parameters in the order in which they exist in
18332 the list, and won't have created a forward reference to a
18333 later parameter. */
18334 if (decl_die != NULL)
18336 add_AT_die_ref (die, attr, decl_die);
18337 return;
18342 /* Last chance: try to create a stack operation procedure to evaluate the
18343 value. Do nothing if even that is not possible or permitted. */
18344 if ((forms & dw_scalar_form_exprloc) == 0)
18345 return;
18347 list = loc_list_from_tree (value, 2, context);
18348 if (list == NULL || single_element_loc_list_p (list))
18350 /* If this attribute is not a reference nor constant, it is
18351 a DWARF expression rather than location description. For that
18352 loc_list_from_tree (value, 0, &context) is needed. */
18353 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18354 if (list2 && single_element_loc_list_p (list2))
18356 add_AT_loc (die, attr, list2->expr);
18357 return;
18361 /* If that failed to give a single element location list, fall back to
18362 outputting this as a reference... still if permitted. */
18363 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18364 return;
18366 if (current_function_decl == 0)
18367 context_die = comp_unit_die ();
18368 else
18369 context_die = lookup_decl_die (current_function_decl);
18371 decl_die = new_die (DW_TAG_variable, context_die, value);
18372 add_AT_flag (decl_die, DW_AT_artificial, 1);
18373 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18374 context_die);
18375 add_AT_location_description (decl_die, DW_AT_location, list);
18376 add_AT_die_ref (die, attr, decl_die);
18379 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18380 default. */
18382 static int
18383 lower_bound_default (void)
18385 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18387 case DW_LANG_C:
18388 case DW_LANG_C89:
18389 case DW_LANG_C99:
18390 case DW_LANG_C11:
18391 case DW_LANG_C_plus_plus:
18392 case DW_LANG_C_plus_plus_11:
18393 case DW_LANG_C_plus_plus_14:
18394 case DW_LANG_ObjC:
18395 case DW_LANG_ObjC_plus_plus:
18396 case DW_LANG_Java:
18397 return 0;
18398 case DW_LANG_Fortran77:
18399 case DW_LANG_Fortran90:
18400 case DW_LANG_Fortran95:
18401 case DW_LANG_Fortran03:
18402 case DW_LANG_Fortran08:
18403 return 1;
18404 case DW_LANG_UPC:
18405 case DW_LANG_D:
18406 case DW_LANG_Python:
18407 return dwarf_version >= 4 ? 0 : -1;
18408 case DW_LANG_Ada95:
18409 case DW_LANG_Ada83:
18410 case DW_LANG_Cobol74:
18411 case DW_LANG_Cobol85:
18412 case DW_LANG_Pascal83:
18413 case DW_LANG_Modula2:
18414 case DW_LANG_PLI:
18415 return dwarf_version >= 4 ? 1 : -1;
18416 default:
18417 return -1;
18421 /* Given a tree node describing an array bound (either lower or upper) output
18422 a representation for that bound. */
18424 static void
18425 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18426 tree bound, const struct loc_descr_context *context)
18428 int dflt;
18430 while (1)
18431 switch (TREE_CODE (bound))
18433 /* Strip all conversions. */
18434 CASE_CONVERT:
18435 case VIEW_CONVERT_EXPR:
18436 bound = TREE_OPERAND (bound, 0);
18437 break;
18439 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
18440 are even omitted when they are the default. */
18441 case INTEGER_CST:
18442 /* If the value for this bound is the default one, we can even omit the
18443 attribute. */
18444 if (bound_attr == DW_AT_lower_bound
18445 && tree_fits_shwi_p (bound)
18446 && (dflt = lower_bound_default ()) != -1
18447 && tree_to_shwi (bound) == dflt)
18448 return;
18450 /* FALLTHRU */
18452 default:
18453 /* Because of the complex interaction there can be with other GNAT
18454 encodings, GDB isn't ready yet to handle proper DWARF description
18455 for self-referencial subrange bounds: let GNAT encodings do the
18456 magic in such a case. */
18457 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18458 && contains_placeholder_p (bound))
18459 return;
18461 add_scalar_info (subrange_die, bound_attr, bound,
18462 dw_scalar_form_constant
18463 | dw_scalar_form_exprloc
18464 | dw_scalar_form_reference,
18465 context);
18466 return;
18470 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18471 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18472 Note that the block of subscript information for an array type also
18473 includes information about the element type of the given array type.
18475 This function reuses previously set type and bound information if
18476 available. */
18478 static void
18479 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18481 unsigned dimension_number;
18482 tree lower, upper;
18483 dw_die_ref child = type_die->die_child;
18485 for (dimension_number = 0;
18486 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18487 type = TREE_TYPE (type), dimension_number++)
18489 tree domain = TYPE_DOMAIN (type);
18491 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18492 break;
18494 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18495 and (in GNU C only) variable bounds. Handle all three forms
18496 here. */
18498 /* Find and reuse a previously generated DW_TAG_subrange_type if
18499 available.
18501 For multi-dimensional arrays, as we iterate through the
18502 various dimensions in the enclosing for loop above, we also
18503 iterate through the DIE children and pick at each
18504 DW_TAG_subrange_type previously generated (if available).
18505 Each child DW_TAG_subrange_type DIE describes the range of
18506 the current dimension. At this point we should have as many
18507 DW_TAG_subrange_type's as we have dimensions in the
18508 array. */
18509 dw_die_ref subrange_die = NULL;
18510 if (child)
18511 while (1)
18513 child = child->die_sib;
18514 if (child->die_tag == DW_TAG_subrange_type)
18515 subrange_die = child;
18516 if (child == type_die->die_child)
18518 /* If we wrapped around, stop looking next time. */
18519 child = NULL;
18520 break;
18522 if (child->die_tag == DW_TAG_subrange_type)
18523 break;
18525 if (!subrange_die)
18526 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18528 if (domain)
18530 /* We have an array type with specified bounds. */
18531 lower = TYPE_MIN_VALUE (domain);
18532 upper = TYPE_MAX_VALUE (domain);
18534 /* Define the index type. */
18535 if (TREE_TYPE (domain)
18536 && !get_AT (subrange_die, DW_AT_type))
18538 /* ??? This is probably an Ada unnamed subrange type. Ignore the
18539 TREE_TYPE field. We can't emit debug info for this
18540 because it is an unnamed integral type. */
18541 if (TREE_CODE (domain) == INTEGER_TYPE
18542 && TYPE_NAME (domain) == NULL_TREE
18543 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18544 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18546 else
18547 add_type_attribute (subrange_die, TREE_TYPE (domain),
18548 TYPE_UNQUALIFIED, false, type_die);
18551 /* ??? If upper is NULL, the array has unspecified length,
18552 but it does have a lower bound. This happens with Fortran
18553 dimension arr(N:*)
18554 Since the debugger is definitely going to need to know N
18555 to produce useful results, go ahead and output the lower
18556 bound solo, and hope the debugger can cope. */
18558 if (!get_AT (subrange_die, DW_AT_lower_bound))
18559 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18560 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18561 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18564 /* Otherwise we have an array type with an unspecified length. The
18565 DWARF-2 spec does not say how to handle this; let's just leave out the
18566 bounds. */
18570 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
18572 static void
18573 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18575 dw_die_ref decl_die;
18576 HOST_WIDE_INT size;
18577 dw_loc_descr_ref size_expr = NULL;
18579 switch (TREE_CODE (tree_node))
18581 case ERROR_MARK:
18582 size = 0;
18583 break;
18584 case ENUMERAL_TYPE:
18585 case RECORD_TYPE:
18586 case UNION_TYPE:
18587 case QUAL_UNION_TYPE:
18588 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18589 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18591 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18592 return;
18594 size_expr = type_byte_size (tree_node, &size);
18595 break;
18596 case FIELD_DECL:
18597 /* For a data member of a struct or union, the DW_AT_byte_size is
18598 generally given as the number of bytes normally allocated for an
18599 object of the *declared* type of the member itself. This is true
18600 even for bit-fields. */
18601 size = int_size_in_bytes (field_type (tree_node));
18602 break;
18603 default:
18604 gcc_unreachable ();
18607 /* Support for dynamically-sized objects was introduced by DWARFv3.
18608 At the moment, GDB does not handle variable byte sizes very well,
18609 though. */
18610 if ((dwarf_version >= 3 || !dwarf_strict)
18611 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18612 && size_expr != NULL)
18613 add_AT_loc (die, DW_AT_byte_size, size_expr);
18615 /* Note that `size' might be -1 when we get to this point. If it is, that
18616 indicates that the byte size of the entity in question is variable and
18617 that we could not generate a DWARF expression that computes it. */
18618 if (size >= 0)
18619 add_AT_unsigned (die, DW_AT_byte_size, size);
18622 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18623 which specifies the distance in bits from the highest order bit of the
18624 "containing object" for the bit-field to the highest order bit of the
18625 bit-field itself.
18627 For any given bit-field, the "containing object" is a hypothetical object
18628 (of some integral or enum type) within which the given bit-field lives. The
18629 type of this hypothetical "containing object" is always the same as the
18630 declared type of the individual bit-field itself. The determination of the
18631 exact location of the "containing object" for a bit-field is rather
18632 complicated. It's handled by the `field_byte_offset' function (above).
18634 CTX is required: see the comment for VLR_CONTEXT.
18636 Note that it is the size (in bytes) of the hypothetical "containing object"
18637 which will be given in the DW_AT_byte_size attribute for this bit-field.
18638 (See `byte_size_attribute' above). */
18640 static inline void
18641 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18643 HOST_WIDE_INT object_offset_in_bytes;
18644 tree original_type = DECL_BIT_FIELD_TYPE (decl);
18645 HOST_WIDE_INT bitpos_int;
18646 HOST_WIDE_INT highest_order_object_bit_offset;
18647 HOST_WIDE_INT highest_order_field_bit_offset;
18648 HOST_WIDE_INT bit_offset;
18650 field_byte_offset (decl, ctx, &object_offset_in_bytes);
18652 /* Must be a field and a bit field. */
18653 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18655 /* We can't yet handle bit-fields whose offsets are variable, so if we
18656 encounter such things, just return without generating any attribute
18657 whatsoever. Likewise for variable or too large size. */
18658 if (! tree_fits_shwi_p (bit_position (decl))
18659 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18660 return;
18662 bitpos_int = int_bit_position (decl);
18664 /* Note that the bit offset is always the distance (in bits) from the
18665 highest-order bit of the "containing object" to the highest-order bit of
18666 the bit-field itself. Since the "high-order end" of any object or field
18667 is different on big-endian and little-endian machines, the computation
18668 below must take account of these differences. */
18669 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18670 highest_order_field_bit_offset = bitpos_int;
18672 if (! BYTES_BIG_ENDIAN)
18674 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18675 highest_order_object_bit_offset +=
18676 simple_type_size_in_bits (original_type);
18679 bit_offset
18680 = (! BYTES_BIG_ENDIAN
18681 ? highest_order_object_bit_offset - highest_order_field_bit_offset
18682 : highest_order_field_bit_offset - highest_order_object_bit_offset);
18684 if (bit_offset < 0)
18685 add_AT_int (die, DW_AT_bit_offset, bit_offset);
18686 else
18687 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18690 /* For a FIELD_DECL node which represents a bit field, output an attribute
18691 which specifies the length in bits of the given field. */
18693 static inline void
18694 add_bit_size_attribute (dw_die_ref die, tree decl)
18696 /* Must be a field and a bit field. */
18697 gcc_assert (TREE_CODE (decl) == FIELD_DECL
18698 && DECL_BIT_FIELD_TYPE (decl));
18700 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18701 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18704 /* If the compiled language is ANSI C, then add a 'prototyped'
18705 attribute, if arg types are given for the parameters of a function. */
18707 static inline void
18708 add_prototyped_attribute (dw_die_ref die, tree func_type)
18710 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18712 case DW_LANG_C:
18713 case DW_LANG_C89:
18714 case DW_LANG_C99:
18715 case DW_LANG_C11:
18716 case DW_LANG_ObjC:
18717 if (prototype_p (func_type))
18718 add_AT_flag (die, DW_AT_prototyped, 1);
18719 break;
18720 default:
18721 break;
18725 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
18726 by looking in the type declaration, the object declaration equate table or
18727 the block mapping. */
18729 static inline dw_die_ref
18730 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18732 dw_die_ref origin_die = NULL;
18734 if (TREE_CODE (origin) != FUNCTION_DECL
18735 && TREE_CODE (origin) != BLOCK)
18737 /* We may have gotten separated from the block for the inlined
18738 function, if we're in an exception handler or some such; make
18739 sure that the abstract function has been written out.
18741 Doing this for nested functions is wrong, however; functions are
18742 distinct units, and our context might not even be inline. */
18743 tree fn = origin;
18745 if (TYPE_P (fn))
18746 fn = TYPE_STUB_DECL (fn);
18748 fn = decl_function_context (fn);
18749 if (fn)
18750 dwarf2out_abstract_function (fn);
18753 if (DECL_P (origin))
18754 origin_die = lookup_decl_die (origin);
18755 else if (TYPE_P (origin))
18756 origin_die = lookup_type_die (origin);
18757 else if (TREE_CODE (origin) == BLOCK)
18758 origin_die = BLOCK_DIE (origin);
18760 /* XXX: Functions that are never lowered don't always have correct block
18761 trees (in the case of java, they simply have no block tree, in some other
18762 languages). For these functions, there is nothing we can really do to
18763 output correct debug info for inlined functions in all cases. Rather
18764 than die, we'll just produce deficient debug info now, in that we will
18765 have variables without a proper abstract origin. In the future, when all
18766 functions are lowered, we should re-add a gcc_assert (origin_die)
18767 here. */
18769 if (origin_die)
18770 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18771 return origin_die;
18774 /* We do not currently support the pure_virtual attribute. */
18776 static inline void
18777 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18779 if (DECL_VINDEX (func_decl))
18781 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18783 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18784 add_AT_loc (die, DW_AT_vtable_elem_location,
18785 new_loc_descr (DW_OP_constu,
18786 tree_to_shwi (DECL_VINDEX (func_decl)),
18787 0));
18789 /* GNU extension: Record what type this method came from originally. */
18790 if (debug_info_level > DINFO_LEVEL_TERSE
18791 && DECL_CONTEXT (func_decl))
18792 add_AT_die_ref (die, DW_AT_containing_type,
18793 lookup_type_die (DECL_CONTEXT (func_decl)));
18797 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18798 given decl. This used to be a vendor extension until after DWARF 4
18799 standardized it. */
18801 static void
18802 add_linkage_attr (dw_die_ref die, tree decl)
18804 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18806 /* Mimic what assemble_name_raw does with a leading '*'. */
18807 if (name[0] == '*')
18808 name = &name[1];
18810 if (dwarf_version >= 4)
18811 add_AT_string (die, DW_AT_linkage_name, name);
18812 else
18813 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18816 /* Add source coordinate attributes for the given decl. */
18818 static void
18819 add_src_coords_attributes (dw_die_ref die, tree decl)
18821 expanded_location s;
18823 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18824 return;
18825 s = expand_location (DECL_SOURCE_LOCATION (decl));
18826 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18827 add_AT_unsigned (die, DW_AT_decl_line, s.line);
18830 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
18832 static void
18833 add_linkage_name_raw (dw_die_ref die, tree decl)
18835 /* Defer until we have an assembler name set. */
18836 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18838 limbo_die_node *asm_name;
18840 asm_name = ggc_cleared_alloc<limbo_die_node> ();
18841 asm_name->die = die;
18842 asm_name->created_for = decl;
18843 asm_name->next = deferred_asm_name;
18844 deferred_asm_name = asm_name;
18846 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18847 add_linkage_attr (die, decl);
18850 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
18852 static void
18853 add_linkage_name (dw_die_ref die, tree decl)
18855 if (debug_info_level > DINFO_LEVEL_NONE
18856 && VAR_OR_FUNCTION_DECL_P (decl)
18857 && TREE_PUBLIC (decl)
18858 && !(VAR_P (decl) && DECL_REGISTER (decl))
18859 && die->die_tag != DW_TAG_member)
18860 add_linkage_name_raw (die, decl);
18863 /* Add a DW_AT_name attribute and source coordinate attribute for the
18864 given decl, but only if it actually has a name. */
18866 static void
18867 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18869 tree decl_name;
18871 decl_name = DECL_NAME (decl);
18872 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18874 const char *name = dwarf2_name (decl, 0);
18875 if (name)
18876 add_name_attribute (die, name);
18877 if (! DECL_ARTIFICIAL (decl))
18878 add_src_coords_attributes (die, decl);
18880 add_linkage_name (die, decl);
18883 #ifdef VMS_DEBUGGING_INFO
18884 /* Get the function's name, as described by its RTL. This may be different
18885 from the DECL_NAME name used in the source file. */
18886 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18888 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18889 XEXP (DECL_RTL (decl), 0), false);
18890 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18892 #endif /* VMS_DEBUGGING_INFO */
18895 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
18897 static void
18898 add_discr_value (dw_die_ref die, dw_discr_value *value)
18900 dw_attr_node attr;
18902 attr.dw_attr = DW_AT_discr_value;
18903 attr.dw_attr_val.val_class = dw_val_class_discr_value;
18904 attr.dw_attr_val.val_entry = NULL;
18905 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18906 if (value->pos)
18907 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18908 else
18909 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18910 add_dwarf_attr (die, &attr);
18913 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
18915 static void
18916 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18918 dw_attr_node attr;
18920 attr.dw_attr = DW_AT_discr_list;
18921 attr.dw_attr_val.val_class = dw_val_class_discr_list;
18922 attr.dw_attr_val.val_entry = NULL;
18923 attr.dw_attr_val.v.val_discr_list = discr_list;
18924 add_dwarf_attr (die, &attr);
18927 static inline dw_discr_list_ref
18928 AT_discr_list (dw_attr_node *attr)
18930 return attr->dw_attr_val.v.val_discr_list;
18933 #ifdef VMS_DEBUGGING_INFO
18934 /* Output the debug main pointer die for VMS */
18936 void
18937 dwarf2out_vms_debug_main_pointer (void)
18939 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18940 dw_die_ref die;
18942 /* Allocate the VMS debug main subprogram die. */
18943 die = ggc_cleared_alloc<die_node> ();
18944 die->die_tag = DW_TAG_subprogram;
18945 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18946 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18947 current_function_funcdef_no);
18948 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18950 /* Make it the first child of comp_unit_die (). */
18951 die->die_parent = comp_unit_die ();
18952 if (comp_unit_die ()->die_child)
18954 die->die_sib = comp_unit_die ()->die_child->die_sib;
18955 comp_unit_die ()->die_child->die_sib = die;
18957 else
18959 die->die_sib = die;
18960 comp_unit_die ()->die_child = die;
18963 #endif /* VMS_DEBUGGING_INFO */
18965 /* Push a new declaration scope. */
18967 static void
18968 push_decl_scope (tree scope)
18970 vec_safe_push (decl_scope_table, scope);
18973 /* Pop a declaration scope. */
18975 static inline void
18976 pop_decl_scope (void)
18978 decl_scope_table->pop ();
18981 /* walk_tree helper function for uses_local_type, below. */
18983 static tree
18984 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18986 if (!TYPE_P (*tp))
18987 *walk_subtrees = 0;
18988 else
18990 tree name = TYPE_NAME (*tp);
18991 if (name && DECL_P (name) && decl_function_context (name))
18992 return *tp;
18994 return NULL_TREE;
18997 /* If TYPE involves a function-local type (including a local typedef to a
18998 non-local type), returns that type; otherwise returns NULL_TREE. */
19000 static tree
19001 uses_local_type (tree type)
19003 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
19004 return used;
19007 /* Return the DIE for the scope that immediately contains this type.
19008 Non-named types that do not involve a function-local type get global
19009 scope. Named types nested in namespaces or other types get their
19010 containing scope. All other types (i.e. function-local named types) get
19011 the current active scope. */
19013 static dw_die_ref
19014 scope_die_for (tree t, dw_die_ref context_die)
19016 dw_die_ref scope_die = NULL;
19017 tree containing_scope;
19019 /* Non-types always go in the current scope. */
19020 gcc_assert (TYPE_P (t));
19022 /* Use the scope of the typedef, rather than the scope of the type
19023 it refers to. */
19024 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
19025 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
19026 else
19027 containing_scope = TYPE_CONTEXT (t);
19029 /* Use the containing namespace if there is one. */
19030 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
19032 if (context_die == lookup_decl_die (containing_scope))
19033 /* OK */;
19034 else if (debug_info_level > DINFO_LEVEL_TERSE)
19035 context_die = get_context_die (containing_scope);
19036 else
19037 containing_scope = NULL_TREE;
19040 /* Ignore function type "scopes" from the C frontend. They mean that
19041 a tagged type is local to a parmlist of a function declarator, but
19042 that isn't useful to DWARF. */
19043 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19044 containing_scope = NULL_TREE;
19046 if (SCOPE_FILE_SCOPE_P (containing_scope))
19048 /* If T uses a local type keep it local as well, to avoid references
19049 to function-local DIEs from outside the function. */
19050 if (current_function_decl && uses_local_type (t))
19051 scope_die = context_die;
19052 else
19053 scope_die = comp_unit_die ();
19055 else if (TYPE_P (containing_scope))
19057 /* For types, we can just look up the appropriate DIE. */
19058 if (debug_info_level > DINFO_LEVEL_TERSE)
19059 scope_die = get_context_die (containing_scope);
19060 else
19062 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19063 if (scope_die == NULL)
19064 scope_die = comp_unit_die ();
19067 else
19068 scope_die = context_die;
19070 return scope_die;
19073 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
19075 static inline int
19076 local_scope_p (dw_die_ref context_die)
19078 for (; context_die; context_die = context_die->die_parent)
19079 if (context_die->die_tag == DW_TAG_inlined_subroutine
19080 || context_die->die_tag == DW_TAG_subprogram)
19081 return 1;
19083 return 0;
19086 /* Returns nonzero if CONTEXT_DIE is a class. */
19088 static inline int
19089 class_scope_p (dw_die_ref context_die)
19091 return (context_die
19092 && (context_die->die_tag == DW_TAG_structure_type
19093 || context_die->die_tag == DW_TAG_class_type
19094 || context_die->die_tag == DW_TAG_interface_type
19095 || context_die->die_tag == DW_TAG_union_type));
19098 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19099 whether or not to treat a DIE in this context as a declaration. */
19101 static inline int
19102 class_or_namespace_scope_p (dw_die_ref context_die)
19104 return (class_scope_p (context_die)
19105 || (context_die && context_die->die_tag == DW_TAG_namespace));
19108 /* Many forms of DIEs require a "type description" attribute. This
19109 routine locates the proper "type descriptor" die for the type given
19110 by 'type' plus any additional qualifiers given by 'cv_quals', and
19111 adds a DW_AT_type attribute below the given die. */
19113 static void
19114 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19115 bool reverse, dw_die_ref context_die)
19117 enum tree_code code = TREE_CODE (type);
19118 dw_die_ref type_die = NULL;
19120 /* ??? If this type is an unnamed subrange type of an integral, floating-point
19121 or fixed-point type, use the inner type. This is because we have no
19122 support for unnamed types in base_type_die. This can happen if this is
19123 an Ada subrange type. Correct solution is emit a subrange type die. */
19124 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19125 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19126 type = TREE_TYPE (type), code = TREE_CODE (type);
19128 if (code == ERROR_MARK
19129 /* Handle a special case. For functions whose return type is void, we
19130 generate *no* type attribute. (Note that no object may have type
19131 `void', so this only applies to function return types). */
19132 || code == VOID_TYPE)
19133 return;
19135 type_die = modified_type_die (type,
19136 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19137 reverse,
19138 context_die);
19140 if (type_die != NULL)
19141 add_AT_die_ref (object_die, DW_AT_type, type_die);
19144 /* Given an object die, add the calling convention attribute for the
19145 function call type. */
19146 static void
19147 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19149 enum dwarf_calling_convention value = DW_CC_normal;
19151 value = ((enum dwarf_calling_convention)
19152 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19154 if (is_fortran ()
19155 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19157 /* DWARF 2 doesn't provide a way to identify a program's source-level
19158 entry point. DW_AT_calling_convention attributes are only meant
19159 to describe functions' calling conventions. However, lacking a
19160 better way to signal the Fortran main program, we used this for
19161 a long time, following existing custom. Now, DWARF 4 has
19162 DW_AT_main_subprogram, which we add below, but some tools still
19163 rely on the old way, which we thus keep. */
19164 value = DW_CC_program;
19166 if (dwarf_version >= 4 || !dwarf_strict)
19167 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19170 /* Only add the attribute if the backend requests it, and
19171 is not DW_CC_normal. */
19172 if (value && (value != DW_CC_normal))
19173 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19176 /* Given a tree pointer to a struct, class, union, or enum type node, return
19177 a pointer to the (string) tag name for the given type, or zero if the type
19178 was declared without a tag. */
19180 static const char *
19181 type_tag (const_tree type)
19183 const char *name = 0;
19185 if (TYPE_NAME (type) != 0)
19187 tree t = 0;
19189 /* Find the IDENTIFIER_NODE for the type name. */
19190 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19191 && !TYPE_NAMELESS (type))
19192 t = TYPE_NAME (type);
19194 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19195 a TYPE_DECL node, regardless of whether or not a `typedef' was
19196 involved. */
19197 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19198 && ! DECL_IGNORED_P (TYPE_NAME (type)))
19200 /* We want to be extra verbose. Don't call dwarf_name if
19201 DECL_NAME isn't set. The default hook for decl_printable_name
19202 doesn't like that, and in this context it's correct to return
19203 0, instead of "<anonymous>" or the like. */
19204 if (DECL_NAME (TYPE_NAME (type))
19205 && !DECL_NAMELESS (TYPE_NAME (type)))
19206 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19209 /* Now get the name as a string, or invent one. */
19210 if (!name && t != 0)
19211 name = IDENTIFIER_POINTER (t);
19214 return (name == 0 || *name == '\0') ? 0 : name;
19217 /* Return the type associated with a data member, make a special check
19218 for bit field types. */
19220 static inline tree
19221 member_declared_type (const_tree member)
19223 return (DECL_BIT_FIELD_TYPE (member)
19224 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19227 /* Get the decl's label, as described by its RTL. This may be different
19228 from the DECL_NAME name used in the source file. */
19230 #if 0
19231 static const char *
19232 decl_start_label (tree decl)
19234 rtx x;
19235 const char *fnname;
19237 x = DECL_RTL (decl);
19238 gcc_assert (MEM_P (x));
19240 x = XEXP (x, 0);
19241 gcc_assert (GET_CODE (x) == SYMBOL_REF);
19243 fnname = XSTR (x, 0);
19244 return fnname;
19246 #endif
19248 /* For variable-length arrays that have been previously generated, but
19249 may be incomplete due to missing subscript info, fill the subscript
19250 info. Return TRUE if this is one of those cases. */
19251 static bool
19252 fill_variable_array_bounds (tree type)
19254 if (TREE_ASM_WRITTEN (type)
19255 && TREE_CODE (type) == ARRAY_TYPE
19256 && variably_modified_type_p (type, NULL))
19258 dw_die_ref array_die = lookup_type_die (type);
19259 if (!array_die)
19260 return false;
19261 add_subscript_info (array_die, type, !is_ada ());
19262 return true;
19264 return false;
19267 /* These routines generate the internal representation of the DIE's for
19268 the compilation unit. Debugging information is collected by walking
19269 the declaration trees passed in from dwarf2out_decl(). */
19271 static void
19272 gen_array_type_die (tree type, dw_die_ref context_die)
19274 dw_die_ref array_die;
19276 /* GNU compilers represent multidimensional array types as sequences of one
19277 dimensional array types whose element types are themselves array types.
19278 We sometimes squish that down to a single array_type DIE with multiple
19279 subscripts in the Dwarf debugging info. The draft Dwarf specification
19280 say that we are allowed to do this kind of compression in C, because
19281 there is no difference between an array of arrays and a multidimensional
19282 array. We don't do this for Ada to remain as close as possible to the
19283 actual representation, which is especially important against the language
19284 flexibilty wrt arrays of variable size. */
19286 bool collapse_nested_arrays = !is_ada ();
19288 if (fill_variable_array_bounds (type))
19289 return;
19291 dw_die_ref scope_die = scope_die_for (type, context_die);
19292 tree element_type;
19294 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19295 DW_TAG_string_type doesn't have DW_AT_type attribute). */
19296 if (TYPE_STRING_FLAG (type)
19297 && TREE_CODE (type) == ARRAY_TYPE
19298 && is_fortran ()
19299 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19301 HOST_WIDE_INT size;
19303 array_die = new_die (DW_TAG_string_type, scope_die, type);
19304 add_name_attribute (array_die, type_tag (type));
19305 equate_type_number_to_die (type, array_die);
19306 size = int_size_in_bytes (type);
19307 if (size >= 0)
19308 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19309 else if (TYPE_DOMAIN (type) != NULL_TREE
19310 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
19312 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19313 tree rszdecl = szdecl;
19314 HOST_WIDE_INT rsize = 0;
19316 size = int_size_in_bytes (TREE_TYPE (szdecl));
19317 if (!DECL_P (szdecl))
19319 if (TREE_CODE (szdecl) == INDIRECT_REF
19320 && DECL_P (TREE_OPERAND (szdecl, 0)))
19322 rszdecl = TREE_OPERAND (szdecl, 0);
19323 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
19324 if (rsize <= 0)
19325 size = 0;
19327 else
19328 size = 0;
19330 if (size > 0)
19332 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19333 if (loc == NULL
19334 && early_dwarf
19335 && current_function_decl
19336 && DECL_CONTEXT (rszdecl) == current_function_decl)
19338 dw_die_ref ref = lookup_decl_die (rszdecl);
19339 dw_loc_descr_ref l = NULL;
19340 if (ref)
19342 l = new_loc_descr (DW_OP_call4, 0, 0);
19343 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19344 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
19345 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
19347 else if (TREE_CODE (rszdecl) == PARM_DECL
19348 && string_types)
19350 l = new_loc_descr (DW_OP_call4, 0, 0);
19351 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19352 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
19353 string_types->safe_push (array_die);
19355 if (l && rszdecl != szdecl)
19357 if (rsize == DWARF2_ADDR_SIZE)
19358 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
19359 0, 0));
19360 else
19361 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
19362 rsize, 0));
19364 if (l)
19365 loc = new_loc_list (l, NULL, NULL, NULL);
19367 if (loc)
19369 add_AT_location_description (array_die, DW_AT_string_length,
19370 loc);
19371 if (size != DWARF2_ADDR_SIZE)
19372 add_AT_unsigned (array_die, dwarf_version >= 5
19373 ? DW_AT_string_length_byte_size
19374 : DW_AT_byte_size, size);
19378 return;
19381 array_die = new_die (DW_TAG_array_type, scope_die, type);
19382 add_name_attribute (array_die, type_tag (type));
19383 equate_type_number_to_die (type, array_die);
19385 if (TREE_CODE (type) == VECTOR_TYPE)
19386 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19388 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
19389 if (is_fortran ()
19390 && TREE_CODE (type) == ARRAY_TYPE
19391 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19392 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19393 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19395 #if 0
19396 /* We default the array ordering. SDB will probably do
19397 the right things even if DW_AT_ordering is not present. It's not even
19398 an issue until we start to get into multidimensional arrays anyway. If
19399 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19400 then we'll have to put the DW_AT_ordering attribute back in. (But if
19401 and when we find out that we need to put these in, we will only do so
19402 for multidimensional arrays. */
19403 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19404 #endif
19406 if (TREE_CODE (type) == VECTOR_TYPE)
19408 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
19409 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19410 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19411 add_bound_info (subrange_die, DW_AT_upper_bound,
19412 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19414 else
19415 add_subscript_info (array_die, type, collapse_nested_arrays);
19417 /* Add representation of the type of the elements of this array type and
19418 emit the corresponding DIE if we haven't done it already. */
19419 element_type = TREE_TYPE (type);
19420 if (collapse_nested_arrays)
19421 while (TREE_CODE (element_type) == ARRAY_TYPE)
19423 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19424 break;
19425 element_type = TREE_TYPE (element_type);
19428 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19429 TREE_CODE (type) == ARRAY_TYPE
19430 && TYPE_REVERSE_STORAGE_ORDER (type),
19431 context_die);
19433 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19434 if (TYPE_ARTIFICIAL (type))
19435 add_AT_flag (array_die, DW_AT_artificial, 1);
19437 if (get_AT (array_die, DW_AT_name))
19438 add_pubtype (type, array_die);
19441 /* After all arguments are created, adjust any DW_TAG_string_type
19442 DIEs DW_AT_string_length attributes. */
19444 static void
19445 adjust_string_types (void)
19447 dw_die_ref array_die;
19448 unsigned int i;
19449 FOR_EACH_VEC_ELT (*string_types, i, array_die)
19451 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
19452 if (a == NULL)
19453 continue;
19454 dw_loc_descr_ref loc = AT_loc (a);
19455 gcc_assert (loc->dw_loc_opc == DW_OP_call4
19456 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
19457 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
19458 if (ref)
19460 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19461 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
19462 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
19464 else
19466 remove_AT (array_die, DW_AT_string_length);
19467 remove_AT (array_die, dwarf_version >= 5
19468 ? DW_AT_string_length_byte_size
19469 : DW_AT_byte_size);
19474 /* This routine generates DIE for array with hidden descriptor, details
19475 are filled into *info by a langhook. */
19477 static void
19478 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19479 dw_die_ref context_die)
19481 const dw_die_ref scope_die = scope_die_for (type, context_die);
19482 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19483 const struct loc_descr_context context = { type, info->base_decl, NULL };
19484 int dim;
19486 add_name_attribute (array_die, type_tag (type));
19487 equate_type_number_to_die (type, array_die);
19489 if (info->ndimensions > 1)
19490 switch (info->ordering)
19492 case array_descr_ordering_row_major:
19493 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19494 break;
19495 case array_descr_ordering_column_major:
19496 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19497 break;
19498 default:
19499 break;
19502 if (dwarf_version >= 3 || !dwarf_strict)
19504 if (info->data_location)
19505 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19506 dw_scalar_form_exprloc, &context);
19507 if (info->associated)
19508 add_scalar_info (array_die, DW_AT_associated, info->associated,
19509 dw_scalar_form_constant
19510 | dw_scalar_form_exprloc
19511 | dw_scalar_form_reference, &context);
19512 if (info->allocated)
19513 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19514 dw_scalar_form_constant
19515 | dw_scalar_form_exprloc
19516 | dw_scalar_form_reference, &context);
19517 if (info->stride)
19519 const enum dwarf_attribute attr
19520 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19521 const int forms
19522 = (info->stride_in_bits)
19523 ? dw_scalar_form_constant
19524 : (dw_scalar_form_constant
19525 | dw_scalar_form_exprloc
19526 | dw_scalar_form_reference);
19528 add_scalar_info (array_die, attr, info->stride, forms, &context);
19532 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19534 for (dim = 0; dim < info->ndimensions; dim++)
19536 dw_die_ref subrange_die
19537 = new_die (DW_TAG_subrange_type, array_die, NULL);
19539 if (info->dimen[dim].bounds_type)
19540 add_type_attribute (subrange_die,
19541 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19542 false, context_die);
19543 if (info->dimen[dim].lower_bound)
19544 add_bound_info (subrange_die, DW_AT_lower_bound,
19545 info->dimen[dim].lower_bound, &context);
19546 if (info->dimen[dim].upper_bound)
19547 add_bound_info (subrange_die, DW_AT_upper_bound,
19548 info->dimen[dim].upper_bound, &context);
19549 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19550 add_scalar_info (subrange_die, DW_AT_byte_stride,
19551 info->dimen[dim].stride,
19552 dw_scalar_form_constant
19553 | dw_scalar_form_exprloc
19554 | dw_scalar_form_reference,
19555 &context);
19558 gen_type_die (info->element_type, context_die);
19559 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19560 TREE_CODE (type) == ARRAY_TYPE
19561 && TYPE_REVERSE_STORAGE_ORDER (type),
19562 context_die);
19564 if (get_AT (array_die, DW_AT_name))
19565 add_pubtype (type, array_die);
19568 #if 0
19569 static void
19570 gen_entry_point_die (tree decl, dw_die_ref context_die)
19572 tree origin = decl_ultimate_origin (decl);
19573 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19575 if (origin != NULL)
19576 add_abstract_origin_attribute (decl_die, origin);
19577 else
19579 add_name_and_src_coords_attributes (decl_die, decl);
19580 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19581 TYPE_UNQUALIFIED, false, context_die);
19584 if (DECL_ABSTRACT_P (decl))
19585 equate_decl_number_to_die (decl, decl_die);
19586 else
19587 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19589 #endif
19591 /* Walk through the list of incomplete types again, trying once more to
19592 emit full debugging info for them. */
19594 static void
19595 retry_incomplete_types (void)
19597 set_early_dwarf s;
19598 int i;
19600 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19601 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19602 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19603 vec_safe_truncate (incomplete_types, 0);
19606 /* Determine what tag to use for a record type. */
19608 static enum dwarf_tag
19609 record_type_tag (tree type)
19611 if (! lang_hooks.types.classify_record)
19612 return DW_TAG_structure_type;
19614 switch (lang_hooks.types.classify_record (type))
19616 case RECORD_IS_STRUCT:
19617 return DW_TAG_structure_type;
19619 case RECORD_IS_CLASS:
19620 return DW_TAG_class_type;
19622 case RECORD_IS_INTERFACE:
19623 if (dwarf_version >= 3 || !dwarf_strict)
19624 return DW_TAG_interface_type;
19625 return DW_TAG_structure_type;
19627 default:
19628 gcc_unreachable ();
19632 /* Generate a DIE to represent an enumeration type. Note that these DIEs
19633 include all of the information about the enumeration values also. Each
19634 enumerated type name/value is listed as a child of the enumerated type
19635 DIE. */
19637 static dw_die_ref
19638 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19640 dw_die_ref type_die = lookup_type_die (type);
19642 if (type_die == NULL)
19644 type_die = new_die (DW_TAG_enumeration_type,
19645 scope_die_for (type, context_die), type);
19646 equate_type_number_to_die (type, type_die);
19647 add_name_attribute (type_die, type_tag (type));
19648 if (dwarf_version >= 4 || !dwarf_strict)
19650 if (ENUM_IS_SCOPED (type))
19651 add_AT_flag (type_die, DW_AT_enum_class, 1);
19652 if (ENUM_IS_OPAQUE (type))
19653 add_AT_flag (type_die, DW_AT_declaration, 1);
19656 else if (! TYPE_SIZE (type))
19657 return type_die;
19658 else
19659 remove_AT (type_die, DW_AT_declaration);
19661 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
19662 given enum type is incomplete, do not generate the DW_AT_byte_size
19663 attribute or the DW_AT_element_list attribute. */
19664 if (TYPE_SIZE (type))
19666 tree link;
19668 TREE_ASM_WRITTEN (type) = 1;
19669 add_byte_size_attribute (type_die, type);
19670 if (dwarf_version >= 3 || !dwarf_strict)
19672 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19673 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19674 context_die);
19676 if (TYPE_STUB_DECL (type) != NULL_TREE)
19678 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19679 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19682 /* If the first reference to this type was as the return type of an
19683 inline function, then it may not have a parent. Fix this now. */
19684 if (type_die->die_parent == NULL)
19685 add_child_die (scope_die_for (type, context_die), type_die);
19687 for (link = TYPE_VALUES (type);
19688 link != NULL; link = TREE_CHAIN (link))
19690 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19691 tree value = TREE_VALUE (link);
19693 add_name_attribute (enum_die,
19694 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19696 if (TREE_CODE (value) == CONST_DECL)
19697 value = DECL_INITIAL (value);
19699 if (simple_type_size_in_bits (TREE_TYPE (value))
19700 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19702 /* For constant forms created by add_AT_unsigned DWARF
19703 consumers (GDB, elfutils, etc.) always zero extend
19704 the value. Only when the actual value is negative
19705 do we need to use add_AT_int to generate a constant
19706 form that can represent negative values. */
19707 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19708 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19709 add_AT_unsigned (enum_die, DW_AT_const_value,
19710 (unsigned HOST_WIDE_INT) val);
19711 else
19712 add_AT_int (enum_die, DW_AT_const_value, val);
19714 else
19715 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
19716 that here. TODO: This should be re-worked to use correct
19717 signed/unsigned double tags for all cases. */
19718 add_AT_wide (enum_die, DW_AT_const_value, value);
19721 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19722 if (TYPE_ARTIFICIAL (type))
19723 add_AT_flag (type_die, DW_AT_artificial, 1);
19725 else
19726 add_AT_flag (type_die, DW_AT_declaration, 1);
19728 add_pubtype (type, type_die);
19730 return type_die;
19733 /* Generate a DIE to represent either a real live formal parameter decl or to
19734 represent just the type of some formal parameter position in some function
19735 type.
19737 Note that this routine is a bit unusual because its argument may be a
19738 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19739 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19740 node. If it's the former then this function is being called to output a
19741 DIE to represent a formal parameter object (or some inlining thereof). If
19742 it's the latter, then this function is only being called to output a
19743 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19744 argument type of some subprogram type.
19745 If EMIT_NAME_P is true, name and source coordinate attributes
19746 are emitted. */
19748 static dw_die_ref
19749 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19750 dw_die_ref context_die)
19752 tree node_or_origin = node ? node : origin;
19753 tree ultimate_origin;
19754 dw_die_ref parm_die = NULL;
19756 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19758 parm_die = lookup_decl_die (node);
19760 /* If the contexts differ, we may not be talking about the same
19761 thing. */
19762 if (parm_die && parm_die->die_parent != context_die)
19764 if (!DECL_ABSTRACT_P (node))
19766 /* This can happen when creating an inlined instance, in
19767 which case we need to create a new DIE that will get
19768 annotated with DW_AT_abstract_origin. */
19769 parm_die = NULL;
19771 else
19773 /* FIXME: Reuse DIE even with a differing context.
19775 This can happen when calling
19776 dwarf2out_abstract_function to build debug info for
19777 the abstract instance of a function for which we have
19778 already generated a DIE in
19779 dwarf2out_early_global_decl.
19781 Once we remove dwarf2out_abstract_function, we should
19782 have a call to gcc_unreachable here. */
19786 if (parm_die && parm_die->die_parent == NULL)
19788 /* Check that parm_die already has the right attributes that
19789 we would have added below. If any attributes are
19790 missing, fall through to add them. */
19791 if (! DECL_ABSTRACT_P (node_or_origin)
19792 && !get_AT (parm_die, DW_AT_location)
19793 && !get_AT (parm_die, DW_AT_const_value))
19794 /* We are missing location info, and are about to add it. */
19796 else
19798 add_child_die (context_die, parm_die);
19799 return parm_die;
19804 /* If we have a previously generated DIE, use it, unless this is an
19805 concrete instance (origin != NULL), in which case we need a new
19806 DIE with a corresponding DW_AT_abstract_origin. */
19807 bool reusing_die;
19808 if (parm_die && origin == NULL)
19809 reusing_die = true;
19810 else
19812 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19813 reusing_die = false;
19816 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19818 case tcc_declaration:
19819 ultimate_origin = decl_ultimate_origin (node_or_origin);
19820 if (node || ultimate_origin)
19821 origin = ultimate_origin;
19823 if (reusing_die)
19824 goto add_location;
19826 if (origin != NULL)
19827 add_abstract_origin_attribute (parm_die, origin);
19828 else if (emit_name_p)
19829 add_name_and_src_coords_attributes (parm_die, node);
19830 if (origin == NULL
19831 || (! DECL_ABSTRACT_P (node_or_origin)
19832 && variably_modified_type_p (TREE_TYPE (node_or_origin),
19833 decl_function_context
19834 (node_or_origin))))
19836 tree type = TREE_TYPE (node_or_origin);
19837 if (decl_by_reference_p (node_or_origin))
19838 add_type_attribute (parm_die, TREE_TYPE (type),
19839 TYPE_UNQUALIFIED,
19840 false, context_die);
19841 else
19842 add_type_attribute (parm_die, type,
19843 decl_quals (node_or_origin),
19844 false, context_die);
19846 if (origin == NULL && DECL_ARTIFICIAL (node))
19847 add_AT_flag (parm_die, DW_AT_artificial, 1);
19848 add_location:
19849 if (node && node != origin)
19850 equate_decl_number_to_die (node, parm_die);
19851 if (! DECL_ABSTRACT_P (node_or_origin))
19852 add_location_or_const_value_attribute (parm_die, node_or_origin,
19853 node == NULL);
19855 break;
19857 case tcc_type:
19858 /* We were called with some kind of a ..._TYPE node. */
19859 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19860 context_die);
19861 break;
19863 default:
19864 gcc_unreachable ();
19867 return parm_die;
19870 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19871 children DW_TAG_formal_parameter DIEs representing the arguments of the
19872 parameter pack.
19874 PARM_PACK must be a function parameter pack.
19875 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19876 must point to the subsequent arguments of the function PACK_ARG belongs to.
19877 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19878 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19879 following the last one for which a DIE was generated. */
19881 static dw_die_ref
19882 gen_formal_parameter_pack_die (tree parm_pack,
19883 tree pack_arg,
19884 dw_die_ref subr_die,
19885 tree *next_arg)
19887 tree arg;
19888 dw_die_ref parm_pack_die;
19890 gcc_assert (parm_pack
19891 && lang_hooks.function_parameter_pack_p (parm_pack)
19892 && subr_die);
19894 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19895 add_src_coords_attributes (parm_pack_die, parm_pack);
19897 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19899 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19900 parm_pack))
19901 break;
19902 gen_formal_parameter_die (arg, NULL,
19903 false /* Don't emit name attribute. */,
19904 parm_pack_die);
19906 if (next_arg)
19907 *next_arg = arg;
19908 return parm_pack_die;
19911 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19912 at the end of an (ANSI prototyped) formal parameters list. */
19914 static void
19915 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19917 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19920 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19921 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19922 parameters as specified in some function type specification (except for
19923 those which appear as part of a function *definition*). */
19925 static void
19926 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19928 tree link;
19929 tree formal_type = NULL;
19930 tree first_parm_type;
19931 tree arg;
19933 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19935 arg = DECL_ARGUMENTS (function_or_method_type);
19936 function_or_method_type = TREE_TYPE (function_or_method_type);
19938 else
19939 arg = NULL_TREE;
19941 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19943 /* Make our first pass over the list of formal parameter types and output a
19944 DW_TAG_formal_parameter DIE for each one. */
19945 for (link = first_parm_type; link; )
19947 dw_die_ref parm_die;
19949 formal_type = TREE_VALUE (link);
19950 if (formal_type == void_type_node)
19951 break;
19953 /* Output a (nameless) DIE to represent the formal parameter itself. */
19954 if (!POINTER_BOUNDS_TYPE_P (formal_type))
19956 parm_die = gen_formal_parameter_die (formal_type, NULL,
19957 true /* Emit name attribute. */,
19958 context_die);
19959 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19960 && link == first_parm_type)
19962 add_AT_flag (parm_die, DW_AT_artificial, 1);
19963 if (dwarf_version >= 3 || !dwarf_strict)
19964 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19966 else if (arg && DECL_ARTIFICIAL (arg))
19967 add_AT_flag (parm_die, DW_AT_artificial, 1);
19970 link = TREE_CHAIN (link);
19971 if (arg)
19972 arg = DECL_CHAIN (arg);
19975 /* If this function type has an ellipsis, add a
19976 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
19977 if (formal_type != void_type_node)
19978 gen_unspecified_parameters_die (function_or_method_type, context_die);
19980 /* Make our second (and final) pass over the list of formal parameter types
19981 and output DIEs to represent those types (as necessary). */
19982 for (link = TYPE_ARG_TYPES (function_or_method_type);
19983 link && TREE_VALUE (link);
19984 link = TREE_CHAIN (link))
19985 gen_type_die (TREE_VALUE (link), context_die);
19988 /* We want to generate the DIE for TYPE so that we can generate the
19989 die for MEMBER, which has been defined; we will need to refer back
19990 to the member declaration nested within TYPE. If we're trying to
19991 generate minimal debug info for TYPE, processing TYPE won't do the
19992 trick; we need to attach the member declaration by hand. */
19994 static void
19995 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19997 gen_type_die (type, context_die);
19999 /* If we're trying to avoid duplicate debug info, we may not have
20000 emitted the member decl for this function. Emit it now. */
20001 if (TYPE_STUB_DECL (type)
20002 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
20003 && ! lookup_decl_die (member))
20005 dw_die_ref type_die;
20006 gcc_assert (!decl_ultimate_origin (member));
20008 push_decl_scope (type);
20009 type_die = lookup_type_die_strip_naming_typedef (type);
20010 if (TREE_CODE (member) == FUNCTION_DECL)
20011 gen_subprogram_die (member, type_die);
20012 else if (TREE_CODE (member) == FIELD_DECL)
20014 /* Ignore the nameless fields that are used to skip bits but handle
20015 C++ anonymous unions and structs. */
20016 if (DECL_NAME (member) != NULL_TREE
20017 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
20018 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
20020 struct vlr_context vlr_ctx = {
20021 DECL_CONTEXT (member), /* struct_type */
20022 NULL_TREE /* variant_part_offset */
20024 gen_type_die (member_declared_type (member), type_die);
20025 gen_field_die (member, &vlr_ctx, type_die);
20028 else
20029 gen_variable_die (member, NULL_TREE, type_die);
20031 pop_decl_scope ();
20035 /* Forward declare these functions, because they are mutually recursive
20036 with their set_block_* pairing functions. */
20037 static void set_decl_origin_self (tree);
20038 static void set_decl_abstract_flags (tree, vec<tree> &);
20040 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
20041 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
20042 that it points to the node itself, thus indicating that the node is its
20043 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
20044 the given node is NULL, recursively descend the decl/block tree which
20045 it is the root of, and for each other ..._DECL or BLOCK node contained
20046 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
20047 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
20048 values to point to themselves. */
20050 static void
20051 set_block_origin_self (tree stmt)
20053 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
20055 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
20058 tree local_decl;
20060 for (local_decl = BLOCK_VARS (stmt);
20061 local_decl != NULL_TREE;
20062 local_decl = DECL_CHAIN (local_decl))
20063 /* Do not recurse on nested functions since the inlining status
20064 of parent and child can be different as per the DWARF spec. */
20065 if (TREE_CODE (local_decl) != FUNCTION_DECL
20066 && !DECL_EXTERNAL (local_decl))
20067 set_decl_origin_self (local_decl);
20071 tree subblock;
20073 for (subblock = BLOCK_SUBBLOCKS (stmt);
20074 subblock != NULL_TREE;
20075 subblock = BLOCK_CHAIN (subblock))
20076 set_block_origin_self (subblock); /* Recurse. */
20081 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
20082 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
20083 node to so that it points to the node itself, thus indicating that the
20084 node represents its own (abstract) origin. Additionally, if the
20085 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
20086 the decl/block tree of which the given node is the root of, and for
20087 each other ..._DECL or BLOCK node contained therein whose
20088 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
20089 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
20090 point to themselves. */
20092 static void
20093 set_decl_origin_self (tree decl)
20095 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
20097 DECL_ABSTRACT_ORIGIN (decl) = decl;
20098 if (TREE_CODE (decl) == FUNCTION_DECL)
20100 tree arg;
20102 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20103 DECL_ABSTRACT_ORIGIN (arg) = arg;
20104 if (DECL_INITIAL (decl) != NULL_TREE
20105 && DECL_INITIAL (decl) != error_mark_node)
20106 set_block_origin_self (DECL_INITIAL (decl));
20111 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
20112 and if it wasn't 1 before, push it to abstract_vec vector.
20113 For all local decls and all local sub-blocks (recursively) do it
20114 too. */
20116 static void
20117 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
20119 tree local_decl;
20120 tree subblock;
20121 unsigned int i;
20123 if (!BLOCK_ABSTRACT (stmt))
20125 abstract_vec.safe_push (stmt);
20126 BLOCK_ABSTRACT (stmt) = 1;
20129 for (local_decl = BLOCK_VARS (stmt);
20130 local_decl != NULL_TREE;
20131 local_decl = DECL_CHAIN (local_decl))
20132 if (! DECL_EXTERNAL (local_decl))
20133 set_decl_abstract_flags (local_decl, abstract_vec);
20135 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20137 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
20138 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
20139 || TREE_CODE (local_decl) == PARM_DECL)
20140 set_decl_abstract_flags (local_decl, abstract_vec);
20143 for (subblock = BLOCK_SUBBLOCKS (stmt);
20144 subblock != NULL_TREE;
20145 subblock = BLOCK_CHAIN (subblock))
20146 set_block_abstract_flags (subblock, abstract_vec);
20149 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
20150 to 1 and if it wasn't 1 before, push to abstract_vec vector.
20151 In the case where the decl is a FUNCTION_DECL also set the abstract
20152 flags for all of the parameters, local vars, local
20153 blocks and sub-blocks (recursively). */
20155 static void
20156 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
20158 if (!DECL_ABSTRACT_P (decl))
20160 abstract_vec.safe_push (decl);
20161 DECL_ABSTRACT_P (decl) = 1;
20164 if (TREE_CODE (decl) == FUNCTION_DECL)
20166 tree arg;
20168 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20169 if (!DECL_ABSTRACT_P (arg))
20171 abstract_vec.safe_push (arg);
20172 DECL_ABSTRACT_P (arg) = 1;
20174 if (DECL_INITIAL (decl) != NULL_TREE
20175 && DECL_INITIAL (decl) != error_mark_node)
20176 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
20180 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20181 may later generate inlined and/or out-of-line instances of.
20183 FIXME: In the early-dwarf world, this function, and most of the
20184 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
20185 the abstract instance. All we would need to do is annotate
20186 the early DIE with the appropriate DW_AT_inline in late
20187 dwarf (perhaps in gen_inlined_subroutine_die).
20189 However, we can't do this yet, because LTO streaming of DIEs
20190 has not been implemented yet. */
20192 static void
20193 dwarf2out_abstract_function (tree decl)
20195 dw_die_ref old_die;
20196 tree save_fn;
20197 tree context;
20198 hash_table<decl_loc_hasher> *old_decl_loc_table;
20199 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20200 int old_call_site_count, old_tail_call_site_count;
20201 struct call_arg_loc_node *old_call_arg_locations;
20203 /* Make sure we have the actual abstract inline, not a clone. */
20204 decl = DECL_ORIGIN (decl);
20206 old_die = lookup_decl_die (decl);
20207 if (old_die && get_AT (old_die, DW_AT_inline))
20208 /* We've already generated the abstract instance. */
20209 return;
20211 /* We can be called while recursively when seeing block defining inlined subroutine
20212 DIE. Be sure to not clobber the outer location table nor use it or we would
20213 get locations in abstract instantces. */
20214 old_decl_loc_table = decl_loc_table;
20215 decl_loc_table = NULL;
20216 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20217 cached_dw_loc_list_table = NULL;
20218 old_call_arg_locations = call_arg_locations;
20219 call_arg_locations = NULL;
20220 old_call_site_count = call_site_count;
20221 call_site_count = -1;
20222 old_tail_call_site_count = tail_call_site_count;
20223 tail_call_site_count = -1;
20225 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20226 we don't get confused by DECL_ABSTRACT_P. */
20227 if (debug_info_level > DINFO_LEVEL_TERSE)
20229 context = decl_class_context (decl);
20230 if (context)
20231 gen_type_die_for_member
20232 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20235 /* Pretend we've just finished compiling this function. */
20236 save_fn = current_function_decl;
20237 current_function_decl = decl;
20239 auto_vec<tree, 64> abstract_vec;
20240 set_decl_abstract_flags (decl, abstract_vec);
20241 dwarf2out_decl (decl);
20242 unsigned int i;
20243 tree t;
20244 FOR_EACH_VEC_ELT (abstract_vec, i, t)
20245 if (TREE_CODE (t) == BLOCK)
20246 BLOCK_ABSTRACT (t) = 0;
20247 else
20248 DECL_ABSTRACT_P (t) = 0;
20250 current_function_decl = save_fn;
20251 decl_loc_table = old_decl_loc_table;
20252 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20253 call_arg_locations = old_call_arg_locations;
20254 call_site_count = old_call_site_count;
20255 tail_call_site_count = old_tail_call_site_count;
20258 /* Helper function of premark_used_types() which gets called through
20259 htab_traverse.
20261 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20262 marked as unused by prune_unused_types. */
20264 bool
20265 premark_used_types_helper (tree const &type, void *)
20267 dw_die_ref die;
20269 die = lookup_type_die (type);
20270 if (die != NULL)
20271 die->die_perennial_p = 1;
20272 return true;
20275 /* Helper function of premark_types_used_by_global_vars which gets called
20276 through htab_traverse.
20278 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20279 marked as unused by prune_unused_types. The DIE of the type is marked
20280 only if the global variable using the type will actually be emitted. */
20283 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20284 void *)
20286 struct types_used_by_vars_entry *entry;
20287 dw_die_ref die;
20289 entry = (struct types_used_by_vars_entry *) *slot;
20290 gcc_assert (entry->type != NULL
20291 && entry->var_decl != NULL);
20292 die = lookup_type_die (entry->type);
20293 if (die)
20295 /* Ask cgraph if the global variable really is to be emitted.
20296 If yes, then we'll keep the DIE of ENTRY->TYPE. */
20297 varpool_node *node = varpool_node::get (entry->var_decl);
20298 if (node && node->definition)
20300 die->die_perennial_p = 1;
20301 /* Keep the parent DIEs as well. */
20302 while ((die = die->die_parent) && die->die_perennial_p == 0)
20303 die->die_perennial_p = 1;
20306 return 1;
20309 /* Mark all members of used_types_hash as perennial. */
20311 static void
20312 premark_used_types (struct function *fun)
20314 if (fun && fun->used_types_hash)
20315 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20318 /* Mark all members of types_used_by_vars_entry as perennial. */
20320 static void
20321 premark_types_used_by_global_vars (void)
20323 if (types_used_by_vars_hash)
20324 types_used_by_vars_hash
20325 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20328 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20329 for CA_LOC call arg loc node. */
20331 static dw_die_ref
20332 gen_call_site_die (tree decl, dw_die_ref subr_die,
20333 struct call_arg_loc_node *ca_loc)
20335 dw_die_ref stmt_die = NULL, die;
20336 tree block = ca_loc->block;
20338 while (block
20339 && block != DECL_INITIAL (decl)
20340 && TREE_CODE (block) == BLOCK)
20342 stmt_die = BLOCK_DIE (block);
20343 if (stmt_die)
20344 break;
20345 block = BLOCK_SUPERCONTEXT (block);
20347 if (stmt_die == NULL)
20348 stmt_die = subr_die;
20349 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20350 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20351 if (ca_loc->tail_call_p)
20352 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20353 if (ca_loc->symbol_ref)
20355 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20356 if (tdie)
20357 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20358 else
20359 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20361 return die;
20364 /* Generate a DIE to represent a declared function (either file-scope or
20365 block-local). */
20367 static void
20368 gen_subprogram_die (tree decl, dw_die_ref context_die)
20370 tree origin = decl_ultimate_origin (decl);
20371 dw_die_ref subr_die;
20372 dw_die_ref old_die = lookup_decl_die (decl);
20374 /* This function gets called multiple times for different stages of
20375 the debug process. For example, for func() in this code:
20377 namespace S
20379 void func() { ... }
20382 ...we get called 4 times. Twice in early debug and twice in
20383 late debug:
20385 Early debug
20386 -----------
20388 1. Once while generating func() within the namespace. This is
20389 the declaration. The declaration bit below is set, as the
20390 context is the namespace.
20392 A new DIE will be generated with DW_AT_declaration set.
20394 2. Once for func() itself. This is the specification. The
20395 declaration bit below is clear as the context is the CU.
20397 We will use the cached DIE from (1) to create a new DIE with
20398 DW_AT_specification pointing to the declaration in (1).
20400 Late debug via rest_of_handle_final()
20401 -------------------------------------
20403 3. Once generating func() within the namespace. This is also the
20404 declaration, as in (1), but this time we will early exit below
20405 as we have a cached DIE and a declaration needs no additional
20406 annotations (no locations), as the source declaration line
20407 info is enough.
20409 4. Once for func() itself. As in (2), this is the specification,
20410 but this time we will re-use the cached DIE, and just annotate
20411 it with the location information that should now be available.
20413 For something without namespaces, but with abstract instances, we
20414 are also called a multiple times:
20416 class Base
20418 public:
20419 Base (); // constructor declaration (1)
20422 Base::Base () { } // constructor specification (2)
20424 Early debug
20425 -----------
20427 1. Once for the Base() constructor by virtue of it being a
20428 member of the Base class. This is done via
20429 rest_of_type_compilation.
20431 This is a declaration, so a new DIE will be created with
20432 DW_AT_declaration.
20434 2. Once for the Base() constructor definition, but this time
20435 while generating the abstract instance of the base
20436 constructor (__base_ctor) which is being generated via early
20437 debug of reachable functions.
20439 Even though we have a cached version of the declaration (1),
20440 we will create a DW_AT_specification of the declaration DIE
20441 in (1).
20443 3. Once for the __base_ctor itself, but this time, we generate
20444 an DW_AT_abstract_origin version of the DW_AT_specification in
20445 (2).
20447 Late debug via rest_of_handle_final
20448 -----------------------------------
20450 4. One final time for the __base_ctor (which will have a cached
20451 DIE with DW_AT_abstract_origin created in (3). This time,
20452 we will just annotate the location information now
20453 available.
20455 int declaration = (current_function_decl != decl
20456 || class_or_namespace_scope_p (context_die));
20458 /* Now that the C++ front end lazily declares artificial member fns, we
20459 might need to retrofit the declaration into its class. */
20460 if (!declaration && !origin && !old_die
20461 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20462 && !class_or_namespace_scope_p (context_die)
20463 && debug_info_level > DINFO_LEVEL_TERSE)
20464 old_die = force_decl_die (decl);
20466 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
20467 if (origin != NULL)
20469 gcc_assert (!declaration || local_scope_p (context_die));
20471 /* Fixup die_parent for the abstract instance of a nested
20472 inline function. */
20473 if (old_die && old_die->die_parent == NULL)
20474 add_child_die (context_die, old_die);
20476 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20478 /* If we have a DW_AT_abstract_origin we have a working
20479 cached version. */
20480 subr_die = old_die;
20482 else
20484 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20485 add_abstract_origin_attribute (subr_die, origin);
20486 /* This is where the actual code for a cloned function is.
20487 Let's emit linkage name attribute for it. This helps
20488 debuggers to e.g, set breakpoints into
20489 constructors/destructors when the user asks "break
20490 K::K". */
20491 add_linkage_name (subr_die, decl);
20494 /* A cached copy, possibly from early dwarf generation. Reuse as
20495 much as possible. */
20496 else if (old_die)
20498 /* A declaration that has been previously dumped needs no
20499 additional information. */
20500 if (declaration)
20501 return;
20503 if (!get_AT_flag (old_die, DW_AT_declaration)
20504 /* We can have a normal definition following an inline one in the
20505 case of redefinition of GNU C extern inlines.
20506 It seems reasonable to use AT_specification in this case. */
20507 && !get_AT (old_die, DW_AT_inline))
20509 /* Detect and ignore this case, where we are trying to output
20510 something we have already output. */
20511 if (get_AT (old_die, DW_AT_low_pc)
20512 || get_AT (old_die, DW_AT_ranges))
20513 return;
20515 /* If we have no location information, this must be a
20516 partially generated DIE from early dwarf generation.
20517 Fall through and generate it. */
20520 /* If the definition comes from the same place as the declaration,
20521 maybe use the old DIE. We always want the DIE for this function
20522 that has the *_pc attributes to be under comp_unit_die so the
20523 debugger can find it. We also need to do this for abstract
20524 instances of inlines, since the spec requires the out-of-line copy
20525 to have the same parent. For local class methods, this doesn't
20526 apply; we just use the old DIE. */
20527 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20528 struct dwarf_file_data * file_index = lookup_filename (s.file);
20529 if ((is_cu_die (old_die->die_parent)
20530 /* This condition fixes the inconsistency/ICE with the
20531 following Fortran test (or some derivative thereof) while
20532 building libgfortran:
20534 module some_m
20535 contains
20536 logical function funky (FLAG)
20537 funky = .true.
20538 end function
20539 end module
20541 || (old_die->die_parent
20542 && old_die->die_parent->die_tag == DW_TAG_module)
20543 || context_die == NULL)
20544 && (DECL_ARTIFICIAL (decl)
20545 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20546 && (get_AT_unsigned (old_die, DW_AT_decl_line)
20547 == (unsigned) s.line))))
20549 subr_die = old_die;
20551 /* Clear out the declaration attribute, but leave the
20552 parameters so they can be augmented with location
20553 information later. Unless this was a declaration, in
20554 which case, wipe out the nameless parameters and recreate
20555 them further down. */
20556 if (remove_AT (subr_die, DW_AT_declaration))
20559 remove_AT (subr_die, DW_AT_object_pointer);
20560 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20563 /* Make a specification pointing to the previously built
20564 declaration. */
20565 else
20567 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20568 add_AT_specification (subr_die, old_die);
20569 add_pubname (decl, subr_die);
20570 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20571 add_AT_file (subr_die, DW_AT_decl_file, file_index);
20572 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20573 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20575 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20576 emit the real type on the definition die. */
20577 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20579 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20580 if (die == auto_die || die == decltype_auto_die)
20581 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20582 TYPE_UNQUALIFIED, false, context_die);
20585 /* When we process the method declaration, we haven't seen
20586 the out-of-class defaulted definition yet, so we have to
20587 recheck now. */
20588 if ((dwarf_version >= 5 || ! dwarf_strict)
20589 && !get_AT (subr_die, DW_AT_defaulted))
20591 int defaulted
20592 = lang_hooks.decls.decl_dwarf_attribute (decl,
20593 DW_AT_defaulted);
20594 if (defaulted != -1)
20596 /* Other values must have been handled before. */
20597 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
20598 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
20603 /* Create a fresh DIE for anything else. */
20604 else
20606 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20608 if (TREE_PUBLIC (decl))
20609 add_AT_flag (subr_die, DW_AT_external, 1);
20611 add_name_and_src_coords_attributes (subr_die, decl);
20612 add_pubname (decl, subr_die);
20613 if (debug_info_level > DINFO_LEVEL_TERSE)
20615 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20616 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20617 TYPE_UNQUALIFIED, false, context_die);
20620 add_pure_or_virtual_attribute (subr_die, decl);
20621 if (DECL_ARTIFICIAL (decl))
20622 add_AT_flag (subr_die, DW_AT_artificial, 1);
20624 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20625 add_AT_flag (subr_die, DW_AT_noreturn, 1);
20627 add_accessibility_attribute (subr_die, decl);
20630 /* Unless we have an existing non-declaration DIE, equate the new
20631 DIE. */
20632 if (!old_die || is_declaration_die (old_die))
20633 equate_decl_number_to_die (decl, subr_die);
20635 if (declaration)
20637 if (!old_die || !get_AT (old_die, DW_AT_inline))
20639 add_AT_flag (subr_die, DW_AT_declaration, 1);
20641 /* If this is an explicit function declaration then generate
20642 a DW_AT_explicit attribute. */
20643 if ((dwarf_version >= 3 || !dwarf_strict)
20644 && lang_hooks.decls.decl_dwarf_attribute (decl,
20645 DW_AT_explicit) == 1)
20646 add_AT_flag (subr_die, DW_AT_explicit, 1);
20648 /* If this is a C++11 deleted special function member then generate
20649 a DW_AT_deleted attribute. */
20650 if ((dwarf_version >= 5 || !dwarf_strict)
20651 && lang_hooks.decls.decl_dwarf_attribute (decl,
20652 DW_AT_deleted) == 1)
20653 add_AT_flag (subr_die, DW_AT_deleted, 1);
20655 /* If this is a C++11 defaulted special function member then
20656 generate a DW_AT_GNU_defaulted attribute. */
20657 if (dwarf_version >= 5 || !dwarf_strict)
20659 int defaulted
20660 = lang_hooks.decls.decl_dwarf_attribute (decl,
20661 DW_AT_defaulted);
20662 if (defaulted != -1)
20663 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
20666 /* If this is a C++11 non-static member function with & ref-qualifier
20667 then generate a DW_AT_reference attribute. */
20668 if ((dwarf_version >= 5 || !dwarf_strict)
20669 && lang_hooks.decls.decl_dwarf_attribute (decl,
20670 DW_AT_reference) == 1)
20671 add_AT_flag (subr_die, DW_AT_reference, 1);
20673 /* If this is a C++11 non-static member function with &&
20674 ref-qualifier then generate a DW_AT_reference attribute. */
20675 if ((dwarf_version >= 5 || !dwarf_strict)
20676 && lang_hooks.decls.decl_dwarf_attribute (decl,
20677 DW_AT_rvalue_reference)
20678 == 1)
20679 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
20682 /* Tag abstract instances with DW_AT_inline. */
20683 else if (DECL_ABSTRACT_P (decl))
20685 if (DECL_DECLARED_INLINE_P (decl))
20687 if (cgraph_function_possibly_inlined_p (decl))
20688 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20689 else
20690 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20692 else
20694 if (cgraph_function_possibly_inlined_p (decl))
20695 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20696 else
20697 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20700 if (DECL_DECLARED_INLINE_P (decl)
20701 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20702 add_AT_flag (subr_die, DW_AT_artificial, 1);
20704 /* For non DECL_EXTERNALs, if range information is available, fill
20705 the DIE with it. */
20706 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20708 HOST_WIDE_INT cfa_fb_offset;
20710 struct function *fun = DECL_STRUCT_FUNCTION (decl);
20712 if (!flag_reorder_blocks_and_partition)
20714 dw_fde_ref fde = fun->fde;
20715 if (fde->dw_fde_begin)
20717 /* We have already generated the labels. */
20718 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20719 fde->dw_fde_end, false);
20721 else
20723 /* Create start/end labels and add the range. */
20724 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20725 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20726 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20727 current_function_funcdef_no);
20728 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20729 current_function_funcdef_no);
20730 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20731 false);
20734 #if VMS_DEBUGGING_INFO
20735 /* HP OpenVMS Industry Standard 64: DWARF Extensions
20736 Section 2.3 Prologue and Epilogue Attributes:
20737 When a breakpoint is set on entry to a function, it is generally
20738 desirable for execution to be suspended, not on the very first
20739 instruction of the function, but rather at a point after the
20740 function's frame has been set up, after any language defined local
20741 declaration processing has been completed, and before execution of
20742 the first statement of the function begins. Debuggers generally
20743 cannot properly determine where this point is. Similarly for a
20744 breakpoint set on exit from a function. The prologue and epilogue
20745 attributes allow a compiler to communicate the location(s) to use. */
20748 if (fde->dw_fde_vms_end_prologue)
20749 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20750 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20752 if (fde->dw_fde_vms_begin_epilogue)
20753 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20754 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20756 #endif
20759 else
20761 /* Generate pubnames entries for the split function code ranges. */
20762 dw_fde_ref fde = fun->fde;
20764 if (fde->dw_fde_second_begin)
20766 if (dwarf_version >= 3 || !dwarf_strict)
20768 /* We should use ranges for non-contiguous code section
20769 addresses. Use the actual code range for the initial
20770 section, since the HOT/COLD labels might precede an
20771 alignment offset. */
20772 bool range_list_added = false;
20773 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20774 fde->dw_fde_end, &range_list_added,
20775 false);
20776 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20777 fde->dw_fde_second_end,
20778 &range_list_added, false);
20779 if (range_list_added)
20780 add_ranges (NULL);
20782 else
20784 /* There is no real support in DW2 for this .. so we make
20785 a work-around. First, emit the pub name for the segment
20786 containing the function label. Then make and emit a
20787 simplified subprogram DIE for the second segment with the
20788 name pre-fixed by __hot/cold_sect_of_. We use the same
20789 linkage name for the second die so that gdb will find both
20790 sections when given "b foo". */
20791 const char *name = NULL;
20792 tree decl_name = DECL_NAME (decl);
20793 dw_die_ref seg_die;
20795 /* Do the 'primary' section. */
20796 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20797 fde->dw_fde_end, false);
20799 /* Build a minimal DIE for the secondary section. */
20800 seg_die = new_die (DW_TAG_subprogram,
20801 subr_die->die_parent, decl);
20803 if (TREE_PUBLIC (decl))
20804 add_AT_flag (seg_die, DW_AT_external, 1);
20806 if (decl_name != NULL
20807 && IDENTIFIER_POINTER (decl_name) != NULL)
20809 name = dwarf2_name (decl, 1);
20810 if (! DECL_ARTIFICIAL (decl))
20811 add_src_coords_attributes (seg_die, decl);
20813 add_linkage_name (seg_die, decl);
20815 gcc_assert (name != NULL);
20816 add_pure_or_virtual_attribute (seg_die, decl);
20817 if (DECL_ARTIFICIAL (decl))
20818 add_AT_flag (seg_die, DW_AT_artificial, 1);
20820 name = concat ("__second_sect_of_", name, NULL);
20821 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20822 fde->dw_fde_second_end, false);
20823 add_name_attribute (seg_die, name);
20824 if (want_pubnames ())
20825 add_pubname_string (name, seg_die);
20828 else
20829 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20830 false);
20833 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20835 /* We define the "frame base" as the function's CFA. This is more
20836 convenient for several reasons: (1) It's stable across the prologue
20837 and epilogue, which makes it better than just a frame pointer,
20838 (2) With dwarf3, there exists a one-byte encoding that allows us
20839 to reference the .debug_frame data by proxy, but failing that,
20840 (3) We can at least reuse the code inspection and interpretation
20841 code that determines the CFA position at various points in the
20842 function. */
20843 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20845 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20846 add_AT_loc (subr_die, DW_AT_frame_base, op);
20848 else
20850 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20851 if (list->dw_loc_next)
20852 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20853 else
20854 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20857 /* Compute a displacement from the "steady-state frame pointer" to
20858 the CFA. The former is what all stack slots and argument slots
20859 will reference in the rtl; the latter is what we've told the
20860 debugger about. We'll need to adjust all frame_base references
20861 by this displacement. */
20862 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20864 if (fun->static_chain_decl)
20866 /* DWARF requires here a location expression that computes the
20867 address of the enclosing subprogram's frame base. The machinery
20868 in tree-nested.c is supposed to store this specific address in the
20869 last field of the FRAME record. */
20870 const tree frame_type
20871 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20872 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20874 tree fb_expr
20875 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20876 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20877 fb_expr, fb_decl, NULL_TREE);
20879 add_AT_location_description (subr_die, DW_AT_static_link,
20880 loc_list_from_tree (fb_expr, 0, NULL));
20884 /* Generate child dies for template paramaters. */
20885 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20886 gen_generic_params_dies (decl);
20888 /* Now output descriptions of the arguments for this function. This gets
20889 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20890 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20891 `...' at the end of the formal parameter list. In order to find out if
20892 there was a trailing ellipsis or not, we must instead look at the type
20893 associated with the FUNCTION_DECL. This will be a node of type
20894 FUNCTION_TYPE. If the chain of type nodes hanging off of this
20895 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20896 an ellipsis at the end. */
20898 /* In the case where we are describing a mere function declaration, all we
20899 need to do here (and all we *can* do here) is to describe the *types* of
20900 its formal parameters. */
20901 if (debug_info_level <= DINFO_LEVEL_TERSE)
20903 else if (declaration)
20904 gen_formal_types_die (decl, subr_die);
20905 else
20907 /* Generate DIEs to represent all known formal parameters. */
20908 tree parm = DECL_ARGUMENTS (decl);
20909 tree generic_decl = early_dwarf
20910 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20911 tree generic_decl_parm = generic_decl
20912 ? DECL_ARGUMENTS (generic_decl)
20913 : NULL;
20914 auto_vec<dw_die_ref> string_types_vec;
20915 if (string_types == NULL)
20916 string_types = &string_types_vec;
20918 /* Now we want to walk the list of parameters of the function and
20919 emit their relevant DIEs.
20921 We consider the case of DECL being an instance of a generic function
20922 as well as it being a normal function.
20924 If DECL is an instance of a generic function we walk the
20925 parameters of the generic function declaration _and_ the parameters of
20926 DECL itself. This is useful because we want to emit specific DIEs for
20927 function parameter packs and those are declared as part of the
20928 generic function declaration. In that particular case,
20929 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20930 That DIE has children DIEs representing the set of arguments
20931 of the pack. Note that the set of pack arguments can be empty.
20932 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20933 children DIE.
20935 Otherwise, we just consider the parameters of DECL. */
20936 while (generic_decl_parm || parm)
20938 if (generic_decl_parm
20939 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20940 gen_formal_parameter_pack_die (generic_decl_parm,
20941 parm, subr_die,
20942 &parm);
20943 else if (parm && !POINTER_BOUNDS_P (parm))
20945 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20947 if (parm == DECL_ARGUMENTS (decl)
20948 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20949 && parm_die
20950 && (dwarf_version >= 3 || !dwarf_strict))
20951 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20953 parm = DECL_CHAIN (parm);
20955 else if (parm)
20956 parm = DECL_CHAIN (parm);
20958 if (generic_decl_parm)
20959 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20962 /* Decide whether we need an unspecified_parameters DIE at the end.
20963 There are 2 more cases to do this for: 1) the ansi ... declaration -
20964 this is detectable when the end of the arg list is not a
20965 void_type_node 2) an unprototyped function declaration (not a
20966 definition). This just means that we have no info about the
20967 parameters at all. */
20968 if (early_dwarf)
20970 if (prototype_p (TREE_TYPE (decl)))
20972 /* This is the prototyped case, check for.... */
20973 if (stdarg_p (TREE_TYPE (decl)))
20974 gen_unspecified_parameters_die (decl, subr_die);
20976 else if (DECL_INITIAL (decl) == NULL_TREE)
20977 gen_unspecified_parameters_die (decl, subr_die);
20980 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
20981 have DIEs. */
20982 if (string_types == &string_types_vec)
20984 adjust_string_types ();
20985 string_types = NULL;
20989 if (subr_die != old_die)
20990 /* Add the calling convention attribute if requested. */
20991 add_calling_convention_attribute (subr_die, decl);
20993 /* Output Dwarf info for all of the stuff within the body of the function
20994 (if it has one - it may be just a declaration).
20996 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20997 a function. This BLOCK actually represents the outermost binding contour
20998 for the function, i.e. the contour in which the function's formal
20999 parameters and labels get declared. Curiously, it appears that the front
21000 end doesn't actually put the PARM_DECL nodes for the current function onto
21001 the BLOCK_VARS list for this outer scope, but are strung off of the
21002 DECL_ARGUMENTS list for the function instead.
21004 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
21005 the LABEL_DECL nodes for the function however, and we output DWARF info
21006 for those in decls_for_scope. Just within the `outer_scope' there will be
21007 a BLOCK node representing the function's outermost pair of curly braces,
21008 and any blocks used for the base and member initializers of a C++
21009 constructor function. */
21010 tree outer_scope = DECL_INITIAL (decl);
21011 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
21013 int call_site_note_count = 0;
21014 int tail_call_site_note_count = 0;
21016 /* Emit a DW_TAG_variable DIE for a named return value. */
21017 if (DECL_NAME (DECL_RESULT (decl)))
21018 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
21020 /* The first time through decls_for_scope we will generate the
21021 DIEs for the locals. The second time, we fill in the
21022 location info. */
21023 decls_for_scope (outer_scope, subr_die);
21025 if (call_arg_locations && !dwarf_strict)
21027 struct call_arg_loc_node *ca_loc;
21028 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
21030 dw_die_ref die = NULL;
21031 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
21032 rtx arg, next_arg;
21034 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
21035 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
21036 : NULL_RTX);
21037 arg; arg = next_arg)
21039 dw_loc_descr_ref reg, val;
21040 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
21041 dw_die_ref cdie, tdie = NULL;
21043 next_arg = XEXP (arg, 1);
21044 if (REG_P (XEXP (XEXP (arg, 0), 0))
21045 && next_arg
21046 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
21047 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
21048 && REGNO (XEXP (XEXP (arg, 0), 0))
21049 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
21050 next_arg = XEXP (next_arg, 1);
21051 if (mode == VOIDmode)
21053 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
21054 if (mode == VOIDmode)
21055 mode = GET_MODE (XEXP (arg, 0));
21057 if (mode == VOIDmode || mode == BLKmode)
21058 continue;
21059 /* Get dynamic information about call target only if we
21060 have no static information: we cannot generate both
21061 DW_AT_abstract_origin and DW_AT_GNU_call_site_target
21062 attributes. */
21063 if (ca_loc->symbol_ref == NULL_RTX)
21065 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
21067 tloc = XEXP (XEXP (arg, 0), 1);
21068 continue;
21070 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
21071 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
21073 tlocc = XEXP (XEXP (arg, 0), 1);
21074 continue;
21077 reg = NULL;
21078 if (REG_P (XEXP (XEXP (arg, 0), 0)))
21079 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
21080 VAR_INIT_STATUS_INITIALIZED);
21081 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
21083 rtx mem = XEXP (XEXP (arg, 0), 0);
21084 reg = mem_loc_descriptor (XEXP (mem, 0),
21085 get_address_mode (mem),
21086 GET_MODE (mem),
21087 VAR_INIT_STATUS_INITIALIZED);
21089 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
21090 == DEBUG_PARAMETER_REF)
21092 tree tdecl
21093 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
21094 tdie = lookup_decl_die (tdecl);
21095 if (tdie == NULL)
21096 continue;
21098 else
21099 continue;
21100 if (reg == NULL
21101 && GET_CODE (XEXP (XEXP (arg, 0), 0))
21102 != DEBUG_PARAMETER_REF)
21103 continue;
21104 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
21105 VOIDmode,
21106 VAR_INIT_STATUS_INITIALIZED);
21107 if (val == NULL)
21108 continue;
21109 if (die == NULL)
21110 die = gen_call_site_die (decl, subr_die, ca_loc);
21111 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
21112 NULL_TREE);
21113 if (reg != NULL)
21114 add_AT_loc (cdie, DW_AT_location, reg);
21115 else if (tdie != NULL)
21116 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
21117 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
21118 if (next_arg != XEXP (arg, 1))
21120 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
21121 if (mode == VOIDmode)
21122 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
21123 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
21124 0), 1),
21125 mode, VOIDmode,
21126 VAR_INIT_STATUS_INITIALIZED);
21127 if (val != NULL)
21128 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
21131 if (die == NULL
21132 && (ca_loc->symbol_ref || tloc))
21133 die = gen_call_site_die (decl, subr_die, ca_loc);
21134 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
21136 dw_loc_descr_ref tval = NULL;
21138 if (tloc != NULL_RTX)
21139 tval = mem_loc_descriptor (tloc,
21140 GET_MODE (tloc) == VOIDmode
21141 ? Pmode : GET_MODE (tloc),
21142 VOIDmode,
21143 VAR_INIT_STATUS_INITIALIZED);
21144 if (tval)
21145 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
21146 else if (tlocc != NULL_RTX)
21148 tval = mem_loc_descriptor (tlocc,
21149 GET_MODE (tlocc) == VOIDmode
21150 ? Pmode : GET_MODE (tlocc),
21151 VOIDmode,
21152 VAR_INIT_STATUS_INITIALIZED);
21153 if (tval)
21154 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
21155 tval);
21158 if (die != NULL)
21160 call_site_note_count++;
21161 if (ca_loc->tail_call_p)
21162 tail_call_site_note_count++;
21166 call_arg_locations = NULL;
21167 call_arg_loc_last = NULL;
21168 if (tail_call_site_count >= 0
21169 && tail_call_site_count == tail_call_site_note_count
21170 && !dwarf_strict)
21172 if (call_site_count >= 0
21173 && call_site_count == call_site_note_count)
21174 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
21175 else
21176 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
21178 call_site_count = -1;
21179 tail_call_site_count = -1;
21182 /* Mark used types after we have created DIEs for the functions scopes. */
21183 premark_used_types (DECL_STRUCT_FUNCTION (decl));
21186 /* Returns a hash value for X (which really is a die_struct). */
21188 hashval_t
21189 block_die_hasher::hash (die_struct *d)
21191 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
21194 /* Return nonzero if decl_id and die_parent of die_struct X is the same
21195 as decl_id and die_parent of die_struct Y. */
21197 bool
21198 block_die_hasher::equal (die_struct *x, die_struct *y)
21200 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
21203 /* Return TRUE if DECL, which may have been previously generated as
21204 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
21205 true if decl (or its origin) is either an extern declaration or a
21206 class/namespace scoped declaration.
21208 The declare_in_namespace support causes us to get two DIEs for one
21209 variable, both of which are declarations. We want to avoid
21210 considering one to be a specification, so we must test for
21211 DECLARATION and DW_AT_declaration. */
21212 static inline bool
21213 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
21215 return (old_die && TREE_STATIC (decl) && !declaration
21216 && get_AT_flag (old_die, DW_AT_declaration) == 1);
21219 /* Return true if DECL is a local static. */
21221 static inline bool
21222 local_function_static (tree decl)
21224 gcc_assert (VAR_P (decl));
21225 return TREE_STATIC (decl)
21226 && DECL_CONTEXT (decl)
21227 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
21230 /* Generate a DIE to represent a declared data object.
21231 Either DECL or ORIGIN must be non-null. */
21233 static void
21234 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
21236 HOST_WIDE_INT off = 0;
21237 tree com_decl;
21238 tree decl_or_origin = decl ? decl : origin;
21239 tree ultimate_origin;
21240 dw_die_ref var_die;
21241 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
21242 dw_die_ref origin_die = NULL;
21243 bool declaration = (DECL_EXTERNAL (decl_or_origin)
21244 || class_or_namespace_scope_p (context_die));
21245 bool specialization_p = false;
21247 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21248 if (decl || ultimate_origin)
21249 origin = ultimate_origin;
21250 com_decl = fortran_common (decl_or_origin, &off);
21252 /* Symbol in common gets emitted as a child of the common block, in the form
21253 of a data member. */
21254 if (com_decl)
21256 dw_die_ref com_die;
21257 dw_loc_list_ref loc = NULL;
21258 die_node com_die_arg;
21260 var_die = lookup_decl_die (decl_or_origin);
21261 if (var_die)
21263 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
21265 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21266 if (loc)
21268 if (off)
21270 /* Optimize the common case. */
21271 if (single_element_loc_list_p (loc)
21272 && loc->expr->dw_loc_opc == DW_OP_addr
21273 && loc->expr->dw_loc_next == NULL
21274 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21275 == SYMBOL_REF)
21277 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21278 loc->expr->dw_loc_oprnd1.v.val_addr
21279 = plus_constant (GET_MODE (x), x , off);
21281 else
21282 loc_list_plus_const (loc, off);
21284 add_AT_location_description (var_die, DW_AT_location, loc);
21285 remove_AT (var_die, DW_AT_declaration);
21288 return;
21291 if (common_block_die_table == NULL)
21292 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21294 com_die_arg.decl_id = DECL_UID (com_decl);
21295 com_die_arg.die_parent = context_die;
21296 com_die = common_block_die_table->find (&com_die_arg);
21297 if (! early_dwarf)
21298 loc = loc_list_from_tree (com_decl, 2, NULL);
21299 if (com_die == NULL)
21301 const char *cnam
21302 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21303 die_node **slot;
21305 com_die = new_die (DW_TAG_common_block, context_die, decl);
21306 add_name_and_src_coords_attributes (com_die, com_decl);
21307 if (loc)
21309 add_AT_location_description (com_die, DW_AT_location, loc);
21310 /* Avoid sharing the same loc descriptor between
21311 DW_TAG_common_block and DW_TAG_variable. */
21312 loc = loc_list_from_tree (com_decl, 2, NULL);
21314 else if (DECL_EXTERNAL (decl_or_origin))
21315 add_AT_flag (com_die, DW_AT_declaration, 1);
21316 if (want_pubnames ())
21317 add_pubname_string (cnam, com_die); /* ??? needed? */
21318 com_die->decl_id = DECL_UID (com_decl);
21319 slot = common_block_die_table->find_slot (com_die, INSERT);
21320 *slot = com_die;
21322 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21324 add_AT_location_description (com_die, DW_AT_location, loc);
21325 loc = loc_list_from_tree (com_decl, 2, NULL);
21326 remove_AT (com_die, DW_AT_declaration);
21328 var_die = new_die (DW_TAG_variable, com_die, decl);
21329 add_name_and_src_coords_attributes (var_die, decl_or_origin);
21330 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21331 decl_quals (decl_or_origin), false,
21332 context_die);
21333 add_AT_flag (var_die, DW_AT_external, 1);
21334 if (loc)
21336 if (off)
21338 /* Optimize the common case. */
21339 if (single_element_loc_list_p (loc)
21340 && loc->expr->dw_loc_opc == DW_OP_addr
21341 && loc->expr->dw_loc_next == NULL
21342 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21344 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21345 loc->expr->dw_loc_oprnd1.v.val_addr
21346 = plus_constant (GET_MODE (x), x, off);
21348 else
21349 loc_list_plus_const (loc, off);
21351 add_AT_location_description (var_die, DW_AT_location, loc);
21353 else if (DECL_EXTERNAL (decl_or_origin))
21354 add_AT_flag (var_die, DW_AT_declaration, 1);
21355 if (decl)
21356 equate_decl_number_to_die (decl, var_die);
21357 return;
21360 if (old_die)
21362 if (declaration)
21364 /* A declaration that has been previously dumped, needs no
21365 further annotations, since it doesn't need location on
21366 the second pass. */
21367 return;
21369 else if (decl_will_get_specification_p (old_die, decl, declaration)
21370 && !get_AT (old_die, DW_AT_specification))
21372 /* Fall-thru so we can make a new variable die along with a
21373 DW_AT_specification. */
21375 else if (origin && old_die->die_parent != context_die)
21377 /* If we will be creating an inlined instance, we need a
21378 new DIE that will get annotated with
21379 DW_AT_abstract_origin. Clear things so we can get a
21380 new DIE. */
21381 gcc_assert (!DECL_ABSTRACT_P (decl));
21382 old_die = NULL;
21384 else
21386 /* If a DIE was dumped early, it still needs location info.
21387 Skip to where we fill the location bits. */
21388 var_die = old_die;
21389 goto gen_variable_die_location;
21393 /* For static data members, the declaration in the class is supposed
21394 to have DW_TAG_member tag; the specification should still be
21395 DW_TAG_variable referencing the DW_TAG_member DIE. */
21396 if (declaration && class_scope_p (context_die))
21397 var_die = new_die (DW_TAG_member, context_die, decl);
21398 else
21399 var_die = new_die (DW_TAG_variable, context_die, decl);
21401 if (origin != NULL)
21402 origin_die = add_abstract_origin_attribute (var_die, origin);
21404 /* Loop unrolling can create multiple blocks that refer to the same
21405 static variable, so we must test for the DW_AT_declaration flag.
21407 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21408 copy decls and set the DECL_ABSTRACT_P flag on them instead of
21409 sharing them.
21411 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
21412 else if (decl_will_get_specification_p (old_die, decl, declaration))
21414 /* This is a definition of a C++ class level static. */
21415 add_AT_specification (var_die, old_die);
21416 specialization_p = true;
21417 if (DECL_NAME (decl))
21419 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21420 struct dwarf_file_data * file_index = lookup_filename (s.file);
21422 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21423 add_AT_file (var_die, DW_AT_decl_file, file_index);
21425 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21426 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21428 if (old_die->die_tag == DW_TAG_member)
21429 add_linkage_name (var_die, decl);
21432 else
21433 add_name_and_src_coords_attributes (var_die, decl);
21435 if ((origin == NULL && !specialization_p)
21436 || (origin != NULL
21437 && !DECL_ABSTRACT_P (decl_or_origin)
21438 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21439 decl_function_context
21440 (decl_or_origin))))
21442 tree type = TREE_TYPE (decl_or_origin);
21444 if (decl_by_reference_p (decl_or_origin))
21445 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21446 context_die);
21447 else
21448 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21449 context_die);
21452 if (origin == NULL && !specialization_p)
21454 if (TREE_PUBLIC (decl))
21455 add_AT_flag (var_die, DW_AT_external, 1);
21457 if (DECL_ARTIFICIAL (decl))
21458 add_AT_flag (var_die, DW_AT_artificial, 1);
21460 add_accessibility_attribute (var_die, decl);
21463 if (declaration)
21464 add_AT_flag (var_die, DW_AT_declaration, 1);
21466 if (decl && (DECL_ABSTRACT_P (decl)
21467 || !old_die || is_declaration_die (old_die)))
21468 equate_decl_number_to_die (decl, var_die);
21470 gen_variable_die_location:
21471 if (! declaration
21472 && (! DECL_ABSTRACT_P (decl_or_origin)
21473 /* Local static vars are shared between all clones/inlines,
21474 so emit DW_AT_location on the abstract DIE if DECL_RTL is
21475 already set. */
21476 || (VAR_P (decl_or_origin)
21477 && TREE_STATIC (decl_or_origin)
21478 && DECL_RTL_SET_P (decl_or_origin))))
21480 if (early_dwarf)
21481 add_pubname (decl_or_origin, var_die);
21482 else
21483 add_location_or_const_value_attribute (var_die, decl_or_origin,
21484 decl == NULL);
21486 else
21487 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21489 if ((dwarf_version >= 4 || !dwarf_strict)
21490 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
21491 DW_AT_const_expr) == 1
21492 && !get_AT (var_die, DW_AT_const_expr)
21493 && (origin_die == NULL || get_AT (origin_die, DW_AT_const_expr) == NULL)
21494 && !specialization_p)
21495 add_AT_flag (var_die, DW_AT_const_expr, 1);
21498 /* Generate a DIE to represent a named constant. */
21500 static void
21501 gen_const_die (tree decl, dw_die_ref context_die)
21503 dw_die_ref const_die;
21504 tree type = TREE_TYPE (decl);
21506 const_die = lookup_decl_die (decl);
21507 if (const_die)
21508 return;
21510 const_die = new_die (DW_TAG_constant, context_die, decl);
21511 equate_decl_number_to_die (decl, const_die);
21512 add_name_and_src_coords_attributes (const_die, decl);
21513 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21514 if (TREE_PUBLIC (decl))
21515 add_AT_flag (const_die, DW_AT_external, 1);
21516 if (DECL_ARTIFICIAL (decl))
21517 add_AT_flag (const_die, DW_AT_artificial, 1);
21518 tree_add_const_value_attribute_for_decl (const_die, decl);
21521 /* Generate a DIE to represent a label identifier. */
21523 static void
21524 gen_label_die (tree decl, dw_die_ref context_die)
21526 tree origin = decl_ultimate_origin (decl);
21527 dw_die_ref lbl_die = lookup_decl_die (decl);
21528 rtx insn;
21529 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21531 if (!lbl_die)
21533 lbl_die = new_die (DW_TAG_label, context_die, decl);
21534 equate_decl_number_to_die (decl, lbl_die);
21536 if (origin != NULL)
21537 add_abstract_origin_attribute (lbl_die, origin);
21538 else
21539 add_name_and_src_coords_attributes (lbl_die, decl);
21542 if (DECL_ABSTRACT_P (decl))
21543 equate_decl_number_to_die (decl, lbl_die);
21544 else if (! early_dwarf)
21546 insn = DECL_RTL_IF_SET (decl);
21548 /* Deleted labels are programmer specified labels which have been
21549 eliminated because of various optimizations. We still emit them
21550 here so that it is possible to put breakpoints on them. */
21551 if (insn
21552 && (LABEL_P (insn)
21553 || ((NOTE_P (insn)
21554 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21556 /* When optimization is enabled (via -O) some parts of the compiler
21557 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21558 represent source-level labels which were explicitly declared by
21559 the user. This really shouldn't be happening though, so catch
21560 it if it ever does happen. */
21561 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21563 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21564 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21566 else if (insn
21567 && NOTE_P (insn)
21568 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21569 && CODE_LABEL_NUMBER (insn) != -1)
21571 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21572 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21577 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
21578 attributes to the DIE for a block STMT, to describe where the inlined
21579 function was called from. This is similar to add_src_coords_attributes. */
21581 static inline void
21582 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21584 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21586 if (dwarf_version >= 3 || !dwarf_strict)
21588 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21589 add_AT_unsigned (die, DW_AT_call_line, s.line);
21594 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21595 Add low_pc and high_pc attributes to the DIE for a block STMT. */
21597 static inline void
21598 add_high_low_attributes (tree stmt, dw_die_ref die)
21600 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21602 if (BLOCK_FRAGMENT_CHAIN (stmt)
21603 && (dwarf_version >= 3 || !dwarf_strict))
21605 tree chain, superblock = NULL_TREE;
21606 dw_die_ref pdie;
21607 dw_attr_node *attr = NULL;
21609 if (inlined_function_outer_scope_p (stmt))
21611 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21612 BLOCK_NUMBER (stmt));
21613 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21616 /* Optimize duplicate .debug_ranges lists or even tails of
21617 lists. If this BLOCK has same ranges as its supercontext,
21618 lookup DW_AT_ranges attribute in the supercontext (and
21619 recursively so), verify that the ranges_table contains the
21620 right values and use it instead of adding a new .debug_range. */
21621 for (chain = stmt, pdie = die;
21622 BLOCK_SAME_RANGE (chain);
21623 chain = BLOCK_SUPERCONTEXT (chain))
21625 dw_attr_node *new_attr;
21627 pdie = pdie->die_parent;
21628 if (pdie == NULL)
21629 break;
21630 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21631 break;
21632 new_attr = get_AT (pdie, DW_AT_ranges);
21633 if (new_attr == NULL
21634 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21635 break;
21636 attr = new_attr;
21637 superblock = BLOCK_SUPERCONTEXT (chain);
21639 if (attr != NULL
21640 && ((*ranges_table)[attr->dw_attr_val.v.val_offset
21641 / 2 / DWARF2_ADDR_SIZE].num
21642 == BLOCK_NUMBER (superblock))
21643 && BLOCK_FRAGMENT_CHAIN (superblock))
21645 unsigned long off = attr->dw_attr_val.v.val_offset
21646 / 2 / DWARF2_ADDR_SIZE;
21647 unsigned long supercnt = 0, thiscnt = 0;
21648 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21649 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21651 ++supercnt;
21652 gcc_checking_assert ((*ranges_table)[off + supercnt].num
21653 == BLOCK_NUMBER (chain));
21655 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
21656 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21657 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21658 ++thiscnt;
21659 gcc_assert (supercnt >= thiscnt);
21660 add_AT_range_list (die, DW_AT_ranges,
21661 ((off + supercnt - thiscnt)
21662 * 2 * DWARF2_ADDR_SIZE),
21663 false);
21664 return;
21667 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21669 chain = BLOCK_FRAGMENT_CHAIN (stmt);
21672 add_ranges (chain);
21673 chain = BLOCK_FRAGMENT_CHAIN (chain);
21675 while (chain);
21676 add_ranges (NULL);
21678 else
21680 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21681 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21682 BLOCK_NUMBER (stmt));
21683 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21684 BLOCK_NUMBER (stmt));
21685 add_AT_low_high_pc (die, label, label_high, false);
21689 /* Generate a DIE for a lexical block. */
21691 static void
21692 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21694 dw_die_ref old_die = BLOCK_DIE (stmt);
21695 dw_die_ref stmt_die = NULL;
21696 if (!old_die)
21698 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21699 BLOCK_DIE (stmt) = stmt_die;
21702 if (BLOCK_ABSTRACT (stmt))
21704 if (old_die)
21706 /* This must have been generated early and it won't even
21707 need location information since it's a DW_AT_inline
21708 function. */
21709 if (flag_checking)
21710 for (dw_die_ref c = context_die; c; c = c->die_parent)
21711 if (c->die_tag == DW_TAG_inlined_subroutine
21712 || c->die_tag == DW_TAG_subprogram)
21714 gcc_assert (get_AT (c, DW_AT_inline));
21715 break;
21717 return;
21720 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21722 /* If this is an inlined instance, create a new lexical die for
21723 anything below to attach DW_AT_abstract_origin to. */
21724 if (old_die)
21726 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21727 BLOCK_DIE (stmt) = stmt_die;
21728 old_die = NULL;
21731 tree origin = block_ultimate_origin (stmt);
21732 if (origin != NULL_TREE && origin != stmt)
21733 add_abstract_origin_attribute (stmt_die, origin);
21736 if (old_die)
21737 stmt_die = old_die;
21739 /* A non abstract block whose blocks have already been reordered
21740 should have the instruction range for this block. If so, set the
21741 high/low attributes. */
21742 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21744 gcc_assert (stmt_die);
21745 add_high_low_attributes (stmt, stmt_die);
21748 decls_for_scope (stmt, stmt_die);
21751 /* Generate a DIE for an inlined subprogram. */
21753 static void
21754 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21756 tree decl;
21758 /* The instance of function that is effectively being inlined shall not
21759 be abstract. */
21760 gcc_assert (! BLOCK_ABSTRACT (stmt));
21762 decl = block_ultimate_origin (stmt);
21764 /* Make sure any inlined functions are known to be inlineable. */
21765 gcc_checking_assert (DECL_ABSTRACT_P (decl)
21766 || cgraph_function_possibly_inlined_p (decl));
21768 /* Emit info for the abstract instance first, if we haven't yet. We
21769 must emit this even if the block is abstract, otherwise when we
21770 emit the block below (or elsewhere), we may end up trying to emit
21771 a die whose origin die hasn't been emitted, and crashing. */
21772 dwarf2out_abstract_function (decl);
21774 if (! BLOCK_ABSTRACT (stmt))
21776 dw_die_ref subr_die
21777 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21779 if (call_arg_locations)
21780 BLOCK_DIE (stmt) = subr_die;
21781 add_abstract_origin_attribute (subr_die, decl);
21782 if (TREE_ASM_WRITTEN (stmt))
21783 add_high_low_attributes (stmt, subr_die);
21784 add_call_src_coords_attributes (stmt, subr_die);
21786 decls_for_scope (stmt, subr_die);
21790 /* Generate a DIE for a field in a record, or structure. CTX is required: see
21791 the comment for VLR_CONTEXT. */
21793 static void
21794 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21796 dw_die_ref decl_die;
21798 if (TREE_TYPE (decl) == error_mark_node)
21799 return;
21801 decl_die = new_die (DW_TAG_member, context_die, decl);
21802 add_name_and_src_coords_attributes (decl_die, decl);
21803 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21804 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21805 context_die);
21807 if (DECL_BIT_FIELD_TYPE (decl))
21809 add_byte_size_attribute (decl_die, decl);
21810 add_bit_size_attribute (decl_die, decl);
21811 add_bit_offset_attribute (decl_die, decl, ctx);
21814 /* If we have a variant part offset, then we are supposed to process a member
21815 of a QUAL_UNION_TYPE, which is how we represent variant parts in
21816 trees. */
21817 gcc_assert (ctx->variant_part_offset == NULL_TREE
21818 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21819 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21820 add_data_member_location_attribute (decl_die, decl, ctx);
21822 if (DECL_ARTIFICIAL (decl))
21823 add_AT_flag (decl_die, DW_AT_artificial, 1);
21825 add_accessibility_attribute (decl_die, decl);
21827 /* Equate decl number to die, so that we can look up this decl later on. */
21828 equate_decl_number_to_die (decl, decl_die);
21831 #if 0
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_pointer_type_die (tree type, dw_die_ref context_die)
21840 dw_die_ref ptr_die
21841 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21843 equate_type_number_to_die (type, ptr_die);
21844 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21845 context_die);
21846 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21849 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21850 Use modified_type_die instead.
21851 We keep this code here just in case these types of DIEs may be needed to
21852 represent certain things in other languages (e.g. Pascal) someday. */
21854 static void
21855 gen_reference_type_die (tree type, dw_die_ref context_die)
21857 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21859 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21860 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21861 else
21862 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21864 equate_type_number_to_die (type, ref_die);
21865 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21866 context_die);
21867 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21869 #endif
21871 /* Generate a DIE for a pointer to a member type. */
21873 static void
21874 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21876 dw_die_ref ptr_die
21877 = new_die (DW_TAG_ptr_to_member_type,
21878 scope_die_for (type, context_die), type);
21880 equate_type_number_to_die (type, ptr_die);
21881 add_AT_die_ref (ptr_die, DW_AT_containing_type,
21882 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21883 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21884 context_die);
21887 static char *producer_string;
21889 /* Return a heap allocated producer string including command line options
21890 if -grecord-gcc-switches. */
21892 static char *
21893 gen_producer_string (void)
21895 size_t j;
21896 auto_vec<const char *> switches;
21897 const char *language_string = lang_hooks.name;
21898 char *producer, *tail;
21899 const char *p;
21900 size_t len = dwarf_record_gcc_switches ? 0 : 3;
21901 size_t plen = strlen (language_string) + 1 + strlen (version_string);
21903 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21904 switch (save_decoded_options[j].opt_index)
21906 case OPT_o:
21907 case OPT_d:
21908 case OPT_dumpbase:
21909 case OPT_dumpdir:
21910 case OPT_auxbase:
21911 case OPT_auxbase_strip:
21912 case OPT_quiet:
21913 case OPT_version:
21914 case OPT_v:
21915 case OPT_w:
21916 case OPT_L:
21917 case OPT_D:
21918 case OPT_I:
21919 case OPT_U:
21920 case OPT_SPECIAL_unknown:
21921 case OPT_SPECIAL_ignore:
21922 case OPT_SPECIAL_program_name:
21923 case OPT_SPECIAL_input_file:
21924 case OPT_grecord_gcc_switches:
21925 case OPT_gno_record_gcc_switches:
21926 case OPT__output_pch_:
21927 case OPT_fdiagnostics_show_location_:
21928 case OPT_fdiagnostics_show_option:
21929 case OPT_fdiagnostics_show_caret:
21930 case OPT_fdiagnostics_color_:
21931 case OPT_fverbose_asm:
21932 case OPT____:
21933 case OPT__sysroot_:
21934 case OPT_nostdinc:
21935 case OPT_nostdinc__:
21936 case OPT_fpreprocessed:
21937 case OPT_fltrans_output_list_:
21938 case OPT_fresolution_:
21939 case OPT_fdebug_prefix_map_:
21940 /* Ignore these. */
21941 continue;
21942 default:
21943 if (cl_options[save_decoded_options[j].opt_index].flags
21944 & CL_NO_DWARF_RECORD)
21945 continue;
21946 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21947 == '-');
21948 switch (save_decoded_options[j].canonical_option[0][1])
21950 case 'M':
21951 case 'i':
21952 case 'W':
21953 continue;
21954 case 'f':
21955 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21956 "dump", 4) == 0)
21957 continue;
21958 break;
21959 default:
21960 break;
21962 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21963 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21964 break;
21967 producer = XNEWVEC (char, plen + 1 + len + 1);
21968 tail = producer;
21969 sprintf (tail, "%s %s", language_string, version_string);
21970 tail += plen;
21972 FOR_EACH_VEC_ELT (switches, j, p)
21974 len = strlen (p);
21975 *tail = ' ';
21976 memcpy (tail + 1, p, len);
21977 tail += len + 1;
21980 *tail = '\0';
21981 return producer;
21984 /* Given a C and/or C++ language/version string return the "highest".
21985 C++ is assumed to be "higher" than C in this case. Used for merging
21986 LTO translation unit languages. */
21987 static const char *
21988 highest_c_language (const char *lang1, const char *lang2)
21990 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21991 return "GNU C++14";
21992 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21993 return "GNU C++11";
21994 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21995 return "GNU C++98";
21997 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21998 return "GNU C11";
21999 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
22000 return "GNU C99";
22001 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
22002 return "GNU C89";
22004 gcc_unreachable ();
22008 /* Generate the DIE for the compilation unit. */
22010 static dw_die_ref
22011 gen_compile_unit_die (const char *filename)
22013 dw_die_ref die;
22014 const char *language_string = lang_hooks.name;
22015 int language;
22017 die = new_die (DW_TAG_compile_unit, NULL, NULL);
22019 if (filename)
22021 add_name_attribute (die, filename);
22022 /* Don't add cwd for <built-in>. */
22023 if (filename[0] != '<')
22024 add_comp_dir_attribute (die);
22027 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
22029 /* If our producer is LTO try to figure out a common language to use
22030 from the global list of translation units. */
22031 if (strcmp (language_string, "GNU GIMPLE") == 0)
22033 unsigned i;
22034 tree t;
22035 const char *common_lang = NULL;
22037 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
22039 if (!TRANSLATION_UNIT_LANGUAGE (t))
22040 continue;
22041 if (!common_lang)
22042 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
22043 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
22045 else if (strncmp (common_lang, "GNU C", 5) == 0
22046 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
22047 /* Mixing C and C++ is ok, use C++ in that case. */
22048 common_lang = highest_c_language (common_lang,
22049 TRANSLATION_UNIT_LANGUAGE (t));
22050 else
22052 /* Fall back to C. */
22053 common_lang = NULL;
22054 break;
22058 if (common_lang)
22059 language_string = common_lang;
22062 language = DW_LANG_C;
22063 if (strncmp (language_string, "GNU C", 5) == 0
22064 && ISDIGIT (language_string[5]))
22066 language = DW_LANG_C89;
22067 if (dwarf_version >= 3 || !dwarf_strict)
22069 if (strcmp (language_string, "GNU C89") != 0)
22070 language = DW_LANG_C99;
22072 if (dwarf_version >= 5 /* || !dwarf_strict */)
22073 if (strcmp (language_string, "GNU C11") == 0)
22074 language = DW_LANG_C11;
22077 else if (strncmp (language_string, "GNU C++", 7) == 0)
22079 language = DW_LANG_C_plus_plus;
22080 if (dwarf_version >= 5 /* || !dwarf_strict */)
22082 if (strcmp (language_string, "GNU C++11") == 0)
22083 language = DW_LANG_C_plus_plus_11;
22084 else if (strcmp (language_string, "GNU C++14") == 0)
22085 language = DW_LANG_C_plus_plus_14;
22088 else if (strcmp (language_string, "GNU F77") == 0)
22089 language = DW_LANG_Fortran77;
22090 else if (strcmp (language_string, "GNU Pascal") == 0)
22091 language = DW_LANG_Pascal83;
22092 else if (dwarf_version >= 3 || !dwarf_strict)
22094 if (strcmp (language_string, "GNU Ada") == 0)
22095 language = DW_LANG_Ada95;
22096 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22098 language = DW_LANG_Fortran95;
22099 if (dwarf_version >= 5 /* || !dwarf_strict */)
22101 if (strcmp (language_string, "GNU Fortran2003") == 0)
22102 language = DW_LANG_Fortran03;
22103 else if (strcmp (language_string, "GNU Fortran2008") == 0)
22104 language = DW_LANG_Fortran08;
22107 else if (strcmp (language_string, "GNU Java") == 0)
22108 language = DW_LANG_Java;
22109 else if (strcmp (language_string, "GNU Objective-C") == 0)
22110 language = DW_LANG_ObjC;
22111 else if (strcmp (language_string, "GNU Objective-C++") == 0)
22112 language = DW_LANG_ObjC_plus_plus;
22113 else if (dwarf_version >= 5 || !dwarf_strict)
22115 if (strcmp (language_string, "GNU Go") == 0)
22116 language = DW_LANG_Go;
22119 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
22120 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22121 language = DW_LANG_Fortran90;
22123 add_AT_unsigned (die, DW_AT_language, language);
22125 switch (language)
22127 case DW_LANG_Fortran77:
22128 case DW_LANG_Fortran90:
22129 case DW_LANG_Fortran95:
22130 case DW_LANG_Fortran03:
22131 case DW_LANG_Fortran08:
22132 /* Fortran has case insensitive identifiers and the front-end
22133 lowercases everything. */
22134 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
22135 break;
22136 default:
22137 /* The default DW_ID_case_sensitive doesn't need to be specified. */
22138 break;
22140 return die;
22143 /* Generate the DIE for a base class. */
22145 static void
22146 gen_inheritance_die (tree binfo, tree access, tree type,
22147 dw_die_ref context_die)
22149 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
22150 struct vlr_context ctx = { type, NULL };
22152 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
22153 context_die);
22154 add_data_member_location_attribute (die, binfo, &ctx);
22156 if (BINFO_VIRTUAL_P (binfo))
22157 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22159 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
22160 children, otherwise the default is DW_ACCESS_public. In DWARF2
22161 the default has always been DW_ACCESS_private. */
22162 if (access == access_public_node)
22164 if (dwarf_version == 2
22165 || context_die->die_tag == DW_TAG_class_type)
22166 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
22168 else if (access == access_protected_node)
22169 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
22170 else if (dwarf_version > 2
22171 && context_die->die_tag != DW_TAG_class_type)
22172 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
22175 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
22176 structure. */
22177 static bool
22178 is_variant_part (tree decl)
22180 return (TREE_CODE (decl) == FIELD_DECL
22181 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
22184 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
22185 return the FIELD_DECL. Return NULL_TREE otherwise. */
22187 static tree
22188 analyze_discr_in_predicate (tree operand, tree struct_type)
22190 bool continue_stripping = true;
22191 while (continue_stripping)
22192 switch (TREE_CODE (operand))
22194 CASE_CONVERT:
22195 operand = TREE_OPERAND (operand, 0);
22196 break;
22197 default:
22198 continue_stripping = false;
22199 break;
22202 /* Match field access to members of struct_type only. */
22203 if (TREE_CODE (operand) == COMPONENT_REF
22204 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
22205 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
22206 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
22207 return TREE_OPERAND (operand, 1);
22208 else
22209 return NULL_TREE;
22212 /* Check that SRC is a constant integer that can be represented as a native
22213 integer constant (either signed or unsigned). If so, store it into DEST and
22214 return true. Return false otherwise. */
22216 static bool
22217 get_discr_value (tree src, dw_discr_value *dest)
22219 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
22221 if (TREE_CODE (src) != INTEGER_CST
22222 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
22223 return false;
22225 dest->pos = is_unsigned;
22226 if (is_unsigned)
22227 dest->v.uval = tree_to_uhwi (src);
22228 else
22229 dest->v.sval = tree_to_shwi (src);
22231 return true;
22234 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
22235 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
22236 store NULL_TREE in DISCR_DECL. Otherwise:
22238 - store the discriminant field in STRUCT_TYPE that controls the variant
22239 part to *DISCR_DECL
22241 - put in *DISCR_LISTS_P an array where for each variant, the item
22242 represents the corresponding matching list of discriminant values.
22244 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
22245 the above array.
22247 Note that when the array is allocated (i.e. when the analysis is
22248 successful), it is up to the caller to free the array. */
22250 static void
22251 analyze_variants_discr (tree variant_part_decl,
22252 tree struct_type,
22253 tree *discr_decl,
22254 dw_discr_list_ref **discr_lists_p,
22255 unsigned *discr_lists_length)
22257 tree variant_part_type = TREE_TYPE (variant_part_decl);
22258 tree variant;
22259 dw_discr_list_ref *discr_lists;
22260 unsigned i;
22262 /* Compute how many variants there are in this variant part. */
22263 *discr_lists_length = 0;
22264 for (variant = TYPE_FIELDS (variant_part_type);
22265 variant != NULL_TREE;
22266 variant = DECL_CHAIN (variant))
22267 ++*discr_lists_length;
22269 *discr_decl = NULL_TREE;
22270 *discr_lists_p
22271 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22272 sizeof (**discr_lists_p));
22273 discr_lists = *discr_lists_p;
22275 /* And then analyze all variants to extract discriminant information for all
22276 of them. This analysis is conservative: as soon as we detect something we
22277 do not support, abort everything and pretend we found nothing. */
22278 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22279 variant != NULL_TREE;
22280 variant = DECL_CHAIN (variant), ++i)
22282 tree match_expr = DECL_QUALIFIER (variant);
22284 /* Now, try to analyze the predicate and deduce a discriminant for
22285 it. */
22286 if (match_expr == boolean_true_node)
22287 /* Typically happens for the default variant: it matches all cases that
22288 previous variants rejected. Don't output any matching value for
22289 this one. */
22290 continue;
22292 /* The following loop tries to iterate over each discriminant
22293 possibility: single values or ranges. */
22294 while (match_expr != NULL_TREE)
22296 tree next_round_match_expr;
22297 tree candidate_discr = NULL_TREE;
22298 dw_discr_list_ref new_node = NULL;
22300 /* Possibilities are matched one after the other by nested
22301 TRUTH_ORIF_EXPR expressions. Process the current possibility and
22302 continue with the rest at next iteration. */
22303 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22305 next_round_match_expr = TREE_OPERAND (match_expr, 0);
22306 match_expr = TREE_OPERAND (match_expr, 1);
22308 else
22309 next_round_match_expr = NULL_TREE;
22311 if (match_expr == boolean_false_node)
22312 /* This sub-expression matches nothing: just wait for the next
22313 one. */
22316 else if (TREE_CODE (match_expr) == EQ_EXPR)
22318 /* We are matching: <discr_field> == <integer_cst>
22319 This sub-expression matches a single value. */
22320 tree integer_cst = TREE_OPERAND (match_expr, 1);
22322 candidate_discr
22323 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22324 struct_type);
22326 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22327 if (!get_discr_value (integer_cst,
22328 &new_node->dw_discr_lower_bound))
22329 goto abort;
22330 new_node->dw_discr_range = false;
22333 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22335 /* We are matching:
22336 <discr_field> > <integer_cst>
22337 && <discr_field> < <integer_cst>.
22338 This sub-expression matches the range of values between the
22339 two matched integer constants. Note that comparisons can be
22340 inclusive or exclusive. */
22341 tree candidate_discr_1, candidate_discr_2;
22342 tree lower_cst, upper_cst;
22343 bool lower_cst_included, upper_cst_included;
22344 tree lower_op = TREE_OPERAND (match_expr, 0);
22345 tree upper_op = TREE_OPERAND (match_expr, 1);
22347 /* When the comparison is exclusive, the integer constant is not
22348 the discriminant range bound we are looking for: we will have
22349 to increment or decrement it. */
22350 if (TREE_CODE (lower_op) == GE_EXPR)
22351 lower_cst_included = true;
22352 else if (TREE_CODE (lower_op) == GT_EXPR)
22353 lower_cst_included = false;
22354 else
22355 goto abort;
22357 if (TREE_CODE (upper_op) == LE_EXPR)
22358 upper_cst_included = true;
22359 else if (TREE_CODE (upper_op) == LT_EXPR)
22360 upper_cst_included = false;
22361 else
22362 goto abort;
22364 /* Extract the discriminant from the first operand and check it
22365 is consistant with the same analysis in the second
22366 operand. */
22367 candidate_discr_1
22368 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22369 struct_type);
22370 candidate_discr_2
22371 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22372 struct_type);
22373 if (candidate_discr_1 == candidate_discr_2)
22374 candidate_discr = candidate_discr_1;
22375 else
22376 goto abort;
22378 /* Extract bounds from both. */
22379 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22380 lower_cst = TREE_OPERAND (lower_op, 1);
22381 upper_cst = TREE_OPERAND (upper_op, 1);
22383 if (!lower_cst_included)
22384 lower_cst
22385 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22386 lower_cst,
22387 build_int_cst (TREE_TYPE (lower_cst), 1)));
22388 if (!upper_cst_included)
22389 upper_cst
22390 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22391 upper_cst,
22392 build_int_cst (TREE_TYPE (upper_cst), 1)));
22394 if (!get_discr_value (lower_cst,
22395 &new_node->dw_discr_lower_bound)
22396 || !get_discr_value (upper_cst,
22397 &new_node->dw_discr_upper_bound))
22398 goto abort;
22400 new_node->dw_discr_range = true;
22403 else
22404 /* Unsupported sub-expression: we cannot determine the set of
22405 matching discriminant values. Abort everything. */
22406 goto abort;
22408 /* If the discriminant info is not consistant with what we saw so
22409 far, consider the analysis failed and abort everything. */
22410 if (candidate_discr == NULL_TREE
22411 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22412 goto abort;
22413 else
22414 *discr_decl = candidate_discr;
22416 if (new_node != NULL)
22418 new_node->dw_discr_next = discr_lists[i];
22419 discr_lists[i] = new_node;
22421 match_expr = next_round_match_expr;
22425 /* If we reach this point, we could match everything we were interested
22426 in. */
22427 return;
22429 abort:
22430 /* Clean all data structure and return no result. */
22431 free (*discr_lists_p);
22432 *discr_lists_p = NULL;
22433 *discr_decl = NULL_TREE;
22436 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22437 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
22438 under CONTEXT_DIE.
22440 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22441 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
22442 this type, which are record types, represent the available variants and each
22443 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
22444 values are inferred from these attributes.
22446 In trees, the offsets for the fields inside these sub-records are relative
22447 to the variant part itself, whereas the corresponding DIEs should have
22448 offset attributes that are relative to the embedding record base address.
22449 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22450 must be an expression that computes the offset of the variant part to
22451 describe in DWARF. */
22453 static void
22454 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22455 dw_die_ref context_die)
22457 const tree variant_part_type = TREE_TYPE (variant_part_decl);
22458 tree variant_part_offset = vlr_ctx->variant_part_offset;
22459 struct loc_descr_context ctx = {
22460 vlr_ctx->struct_type, /* context_type */
22461 NULL_TREE, /* base_decl */
22462 NULL /* dpi */
22465 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22466 NULL_TREE if there is no such field. */
22467 tree discr_decl = NULL_TREE;
22468 dw_discr_list_ref *discr_lists;
22469 unsigned discr_lists_length = 0;
22470 unsigned i;
22472 dw_die_ref dwarf_proc_die = NULL;
22473 dw_die_ref variant_part_die
22474 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22476 equate_decl_number_to_die (variant_part_decl, variant_part_die);
22478 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22479 &discr_decl, &discr_lists, &discr_lists_length);
22481 if (discr_decl != NULL_TREE)
22483 dw_die_ref discr_die = lookup_decl_die (discr_decl);
22485 if (discr_die)
22486 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22487 else
22488 /* We have no DIE for the discriminant, so just discard all
22489 discrimimant information in the output. */
22490 discr_decl = NULL_TREE;
22493 /* If the offset for this variant part is more complex than a constant,
22494 create a DWARF procedure for it so that we will not have to generate DWARF
22495 expressions for it for each member. */
22496 if (TREE_CODE (variant_part_offset) != INTEGER_CST
22497 && (dwarf_version >= 3 || !dwarf_strict))
22499 const tree dwarf_proc_fndecl
22500 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22501 build_function_type (TREE_TYPE (variant_part_offset),
22502 NULL_TREE));
22503 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22504 const dw_loc_descr_ref dwarf_proc_body
22505 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22507 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22508 dwarf_proc_fndecl, context_die);
22509 if (dwarf_proc_die != NULL)
22510 variant_part_offset = dwarf_proc_call;
22513 /* Output DIEs for all variants. */
22514 i = 0;
22515 for (tree variant = TYPE_FIELDS (variant_part_type);
22516 variant != NULL_TREE;
22517 variant = DECL_CHAIN (variant), ++i)
22519 tree variant_type = TREE_TYPE (variant);
22520 dw_die_ref variant_die;
22522 /* All variants (i.e. members of a variant part) are supposed to be
22523 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
22524 under these records. */
22525 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22527 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22528 equate_decl_number_to_die (variant, variant_die);
22530 /* Output discriminant values this variant matches, if any. */
22531 if (discr_decl == NULL || discr_lists[i] == NULL)
22532 /* In the case we have discriminant information at all, this is
22533 probably the default variant: as the standard says, don't
22534 output any discriminant value/list attribute. */
22536 else if (discr_lists[i]->dw_discr_next == NULL
22537 && !discr_lists[i]->dw_discr_range)
22538 /* If there is only one accepted value, don't bother outputting a
22539 list. */
22540 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22541 else
22542 add_discr_list (variant_die, discr_lists[i]);
22544 for (tree member = TYPE_FIELDS (variant_type);
22545 member != NULL_TREE;
22546 member = DECL_CHAIN (member))
22548 struct vlr_context vlr_sub_ctx = {
22549 vlr_ctx->struct_type, /* struct_type */
22550 NULL /* variant_part_offset */
22552 if (is_variant_part (member))
22554 /* All offsets for fields inside variant parts are relative to
22555 the top-level embedding RECORD_TYPE's base address. On the
22556 other hand, offsets in GCC's types are relative to the
22557 nested-most variant part. So we have to sum offsets each time
22558 we recurse. */
22560 vlr_sub_ctx.variant_part_offset
22561 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22562 variant_part_offset, byte_position (member)));
22563 gen_variant_part (member, &vlr_sub_ctx, variant_die);
22565 else
22567 vlr_sub_ctx.variant_part_offset = variant_part_offset;
22568 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22573 free (discr_lists);
22576 /* Generate a DIE for a class member. */
22578 static void
22579 gen_member_die (tree type, dw_die_ref context_die)
22581 tree member;
22582 tree binfo = TYPE_BINFO (type);
22583 dw_die_ref child;
22585 /* If this is not an incomplete type, output descriptions of each of its
22586 members. Note that as we output the DIEs necessary to represent the
22587 members of this record or union type, we will also be trying to output
22588 DIEs to represent the *types* of those members. However the `type'
22589 function (above) will specifically avoid generating type DIEs for member
22590 types *within* the list of member DIEs for this (containing) type except
22591 for those types (of members) which are explicitly marked as also being
22592 members of this (containing) type themselves. The g++ front- end can
22593 force any given type to be treated as a member of some other (containing)
22594 type by setting the TYPE_CONTEXT of the given (member) type to point to
22595 the TREE node representing the appropriate (containing) type. */
22597 /* First output info about the base classes. */
22598 if (binfo)
22600 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22601 int i;
22602 tree base;
22604 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22605 gen_inheritance_die (base,
22606 (accesses ? (*accesses)[i] : access_public_node),
22607 type,
22608 context_die);
22611 /* Now output info about the data members and type members. */
22612 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22614 struct vlr_context vlr_ctx = { type, NULL_TREE };
22616 /* If we thought we were generating minimal debug info for TYPE
22617 and then changed our minds, some of the member declarations
22618 may have already been defined. Don't define them again, but
22619 do put them in the right order. */
22621 child = lookup_decl_die (member);
22622 if (child)
22624 /* Handle inline static data members, which only have in-class
22625 declarations. */
22626 if (child->die_tag == DW_TAG_variable
22627 && child->die_parent == comp_unit_die ()
22628 && get_AT (child, DW_AT_specification) == NULL)
22630 reparent_child (child, context_die);
22631 child->die_tag = DW_TAG_member;
22633 else
22634 splice_child_die (context_die, child);
22637 /* Do not generate standard DWARF for variant parts if we are generating
22638 the corresponding GNAT encodings: DIEs generated for both would
22639 conflict in our mappings. */
22640 else if (is_variant_part (member)
22641 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22643 vlr_ctx.variant_part_offset = byte_position (member);
22644 gen_variant_part (member, &vlr_ctx, context_die);
22646 else
22648 vlr_ctx.variant_part_offset = NULL_TREE;
22649 gen_decl_die (member, NULL, &vlr_ctx, context_die);
22653 /* We do not keep type methods in type variants. */
22654 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22655 /* Now output info about the function members (if any). */
22656 if (TYPE_METHODS (type) != error_mark_node)
22657 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22659 /* Don't include clones in the member list. */
22660 if (DECL_ABSTRACT_ORIGIN (member))
22661 continue;
22662 /* Nor constructors for anonymous classes. */
22663 if (DECL_ARTIFICIAL (member)
22664 && dwarf2_name (member, 0) == NULL)
22665 continue;
22667 child = lookup_decl_die (member);
22668 if (child)
22669 splice_child_die (context_die, child);
22670 else
22671 gen_decl_die (member, NULL, NULL, context_die);
22675 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
22676 is set, we pretend that the type was never defined, so we only get the
22677 member DIEs needed by later specification DIEs. */
22679 static void
22680 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22681 enum debug_info_usage usage)
22683 if (TREE_ASM_WRITTEN (type))
22685 /* Fill in the bound of variable-length fields in late dwarf if
22686 still incomplete. */
22687 if (!early_dwarf && variably_modified_type_p (type, NULL))
22688 for (tree member = TYPE_FIELDS (type);
22689 member;
22690 member = DECL_CHAIN (member))
22691 fill_variable_array_bounds (TREE_TYPE (member));
22692 return;
22695 dw_die_ref type_die = lookup_type_die (type);
22696 dw_die_ref scope_die = 0;
22697 int nested = 0;
22698 int complete = (TYPE_SIZE (type)
22699 && (! TYPE_STUB_DECL (type)
22700 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22701 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22702 complete = complete && should_emit_struct_debug (type, usage);
22704 if (type_die && ! complete)
22705 return;
22707 if (TYPE_CONTEXT (type) != NULL_TREE
22708 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22709 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22710 nested = 1;
22712 scope_die = scope_die_for (type, context_die);
22714 /* Generate child dies for template paramaters. */
22715 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22716 schedule_generic_params_dies_gen (type);
22718 if (! type_die || (nested && is_cu_die (scope_die)))
22719 /* First occurrence of type or toplevel definition of nested class. */
22721 dw_die_ref old_die = type_die;
22723 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22724 ? record_type_tag (type) : DW_TAG_union_type,
22725 scope_die, type);
22726 equate_type_number_to_die (type, type_die);
22727 if (old_die)
22728 add_AT_specification (type_die, old_die);
22729 else
22730 add_name_attribute (type_die, type_tag (type));
22732 else
22733 remove_AT (type_die, DW_AT_declaration);
22735 /* If this type has been completed, then give it a byte_size attribute and
22736 then give a list of members. */
22737 if (complete && !ns_decl)
22739 /* Prevent infinite recursion in cases where the type of some member of
22740 this type is expressed in terms of this type itself. */
22741 TREE_ASM_WRITTEN (type) = 1;
22742 add_byte_size_attribute (type_die, type);
22743 if (TYPE_STUB_DECL (type) != NULL_TREE)
22745 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22746 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22749 /* If the first reference to this type was as the return type of an
22750 inline function, then it may not have a parent. Fix this now. */
22751 if (type_die->die_parent == NULL)
22752 add_child_die (scope_die, type_die);
22754 push_decl_scope (type);
22755 gen_member_die (type, type_die);
22756 pop_decl_scope ();
22758 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22759 if (TYPE_ARTIFICIAL (type))
22760 add_AT_flag (type_die, DW_AT_artificial, 1);
22762 /* GNU extension: Record what type our vtable lives in. */
22763 if (TYPE_VFIELD (type))
22765 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22767 gen_type_die (vtype, context_die);
22768 add_AT_die_ref (type_die, DW_AT_containing_type,
22769 lookup_type_die (vtype));
22772 else
22774 add_AT_flag (type_die, DW_AT_declaration, 1);
22776 /* We don't need to do this for function-local types. */
22777 if (TYPE_STUB_DECL (type)
22778 && ! decl_function_context (TYPE_STUB_DECL (type)))
22779 vec_safe_push (incomplete_types, type);
22782 if (get_AT (type_die, DW_AT_name))
22783 add_pubtype (type, type_die);
22786 /* Generate a DIE for a subroutine _type_. */
22788 static void
22789 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22791 tree return_type = TREE_TYPE (type);
22792 dw_die_ref subr_die
22793 = new_die (DW_TAG_subroutine_type,
22794 scope_die_for (type, context_die), type);
22796 equate_type_number_to_die (type, subr_die);
22797 add_prototyped_attribute (subr_die, type);
22798 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22799 context_die);
22800 gen_formal_types_die (type, subr_die);
22802 if (get_AT (subr_die, DW_AT_name))
22803 add_pubtype (type, subr_die);
22806 /* Generate a DIE for a type definition. */
22808 static void
22809 gen_typedef_die (tree decl, dw_die_ref context_die)
22811 dw_die_ref type_die;
22812 tree origin;
22814 if (TREE_ASM_WRITTEN (decl))
22816 if (DECL_ORIGINAL_TYPE (decl))
22817 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22818 return;
22821 TREE_ASM_WRITTEN (decl) = 1;
22822 type_die = new_die (DW_TAG_typedef, context_die, decl);
22823 origin = decl_ultimate_origin (decl);
22824 if (origin != NULL)
22825 add_abstract_origin_attribute (type_die, origin);
22826 else
22828 tree type;
22830 add_name_and_src_coords_attributes (type_die, decl);
22831 if (DECL_ORIGINAL_TYPE (decl))
22833 type = DECL_ORIGINAL_TYPE (decl);
22835 if (type == error_mark_node)
22836 return;
22838 gcc_assert (type != TREE_TYPE (decl));
22839 equate_type_number_to_die (TREE_TYPE (decl), type_die);
22841 else
22843 type = TREE_TYPE (decl);
22845 if (type == error_mark_node)
22846 return;
22848 if (is_naming_typedef_decl (TYPE_NAME (type)))
22850 /* Here, we are in the case of decl being a typedef naming
22851 an anonymous type, e.g:
22852 typedef struct {...} foo;
22853 In that case TREE_TYPE (decl) is not a typedef variant
22854 type and TYPE_NAME of the anonymous type is set to the
22855 TYPE_DECL of the typedef. This construct is emitted by
22856 the C++ FE.
22858 TYPE is the anonymous struct named by the typedef
22859 DECL. As we need the DW_AT_type attribute of the
22860 DW_TAG_typedef to point to the DIE of TYPE, let's
22861 generate that DIE right away. add_type_attribute
22862 called below will then pick (via lookup_type_die) that
22863 anonymous struct DIE. */
22864 if (!TREE_ASM_WRITTEN (type))
22865 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22867 /* This is a GNU Extension. We are adding a
22868 DW_AT_linkage_name attribute to the DIE of the
22869 anonymous struct TYPE. The value of that attribute
22870 is the name of the typedef decl naming the anonymous
22871 struct. This greatly eases the work of consumers of
22872 this debug info. */
22873 add_linkage_name_raw (lookup_type_die (type), decl);
22877 add_type_attribute (type_die, type, decl_quals (decl), false,
22878 context_die);
22880 if (is_naming_typedef_decl (decl))
22881 /* We want that all subsequent calls to lookup_type_die with
22882 TYPE in argument yield the DW_TAG_typedef we have just
22883 created. */
22884 equate_type_number_to_die (type, type_die);
22886 add_accessibility_attribute (type_die, decl);
22889 if (DECL_ABSTRACT_P (decl))
22890 equate_decl_number_to_die (decl, type_die);
22892 if (get_AT (type_die, DW_AT_name))
22893 add_pubtype (decl, type_die);
22896 /* Generate a DIE for a struct, class, enum or union type. */
22898 static void
22899 gen_tagged_type_die (tree type,
22900 dw_die_ref context_die,
22901 enum debug_info_usage usage)
22903 int need_pop;
22905 if (type == NULL_TREE
22906 || !is_tagged_type (type))
22907 return;
22909 if (TREE_ASM_WRITTEN (type))
22910 need_pop = 0;
22911 /* If this is a nested type whose containing class hasn't been written
22912 out yet, writing it out will cover this one, too. This does not apply
22913 to instantiations of member class templates; they need to be added to
22914 the containing class as they are generated. FIXME: This hurts the
22915 idea of combining type decls from multiple TUs, since we can't predict
22916 what set of template instantiations we'll get. */
22917 else if (TYPE_CONTEXT (type)
22918 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22919 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22921 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22923 if (TREE_ASM_WRITTEN (type))
22924 return;
22926 /* If that failed, attach ourselves to the stub. */
22927 push_decl_scope (TYPE_CONTEXT (type));
22928 context_die = lookup_type_die (TYPE_CONTEXT (type));
22929 need_pop = 1;
22931 else if (TYPE_CONTEXT (type) != NULL_TREE
22932 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22934 /* If this type is local to a function that hasn't been written
22935 out yet, use a NULL context for now; it will be fixed up in
22936 decls_for_scope. */
22937 context_die = lookup_decl_die (TYPE_CONTEXT (type));
22938 /* A declaration DIE doesn't count; nested types need to go in the
22939 specification. */
22940 if (context_die && is_declaration_die (context_die))
22941 context_die = NULL;
22942 need_pop = 0;
22944 else
22946 context_die = declare_in_namespace (type, context_die);
22947 need_pop = 0;
22950 if (TREE_CODE (type) == ENUMERAL_TYPE)
22952 /* This might have been written out by the call to
22953 declare_in_namespace. */
22954 if (!TREE_ASM_WRITTEN (type))
22955 gen_enumeration_type_die (type, context_die);
22957 else
22958 gen_struct_or_union_type_die (type, context_die, usage);
22960 if (need_pop)
22961 pop_decl_scope ();
22963 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22964 it up if it is ever completed. gen_*_type_die will set it for us
22965 when appropriate. */
22968 /* Generate a type description DIE. */
22970 static void
22971 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22972 enum debug_info_usage usage)
22974 struct array_descr_info info;
22976 if (type == NULL_TREE || type == error_mark_node)
22977 return;
22979 if (flag_checking && type)
22980 verify_type (type);
22982 if (TYPE_NAME (type) != NULL_TREE
22983 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22984 && is_redundant_typedef (TYPE_NAME (type))
22985 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22986 /* The DECL of this type is a typedef we don't want to emit debug
22987 info for but we want debug info for its underlying typedef.
22988 This can happen for e.g, the injected-class-name of a C++
22989 type. */
22990 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22992 /* If TYPE is a typedef type variant, let's generate debug info
22993 for the parent typedef which TYPE is a type of. */
22994 if (typedef_variant_p (type))
22996 if (TREE_ASM_WRITTEN (type))
22997 return;
22999 /* Prevent broken recursion; we can't hand off to the same type. */
23000 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
23002 /* Give typedefs the right scope. */
23003 context_die = scope_die_for (type, context_die);
23005 TREE_ASM_WRITTEN (type) = 1;
23007 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
23008 return;
23011 /* If type is an anonymous tagged type named by a typedef, let's
23012 generate debug info for the typedef. */
23013 if (is_naming_typedef_decl (TYPE_NAME (type)))
23015 /* Use the DIE of the containing namespace as the parent DIE of
23016 the type description DIE we want to generate. */
23017 if (DECL_CONTEXT (TYPE_NAME (type))
23018 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
23019 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
23021 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
23022 return;
23025 /* We are going to output a DIE to represent the unqualified version
23026 of this type (i.e. without any const or volatile qualifiers) so
23027 get the main variant (i.e. the unqualified version) of this type
23028 now. (Vectors and arrays are special because the debugging info is in the
23029 cloned type itself). */
23030 if (TREE_CODE (type) != VECTOR_TYPE
23031 && TREE_CODE (type) != ARRAY_TYPE)
23032 type = type_main_variant (type);
23034 /* If this is an array type with hidden descriptor, handle it first. */
23035 if (!TREE_ASM_WRITTEN (type)
23036 && lang_hooks.types.get_array_descr_info)
23038 memset (&info, 0, sizeof (info));
23039 if (lang_hooks.types.get_array_descr_info (type, &info))
23041 /* Fortran sometimes emits array types with no dimension. */
23042 gcc_assert (info.ndimensions >= 0
23043 && (info.ndimensions
23044 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
23045 gen_descr_array_type_die (type, &info, context_die);
23046 TREE_ASM_WRITTEN (type) = 1;
23047 return;
23051 if (TREE_ASM_WRITTEN (type))
23053 /* Variable-length types may be incomplete even if
23054 TREE_ASM_WRITTEN. For such types, fall through to
23055 gen_array_type_die() and possibly fill in
23056 DW_AT_{upper,lower}_bound attributes. */
23057 if ((TREE_CODE (type) != ARRAY_TYPE
23058 && TREE_CODE (type) != RECORD_TYPE
23059 && TREE_CODE (type) != UNION_TYPE
23060 && TREE_CODE (type) != QUAL_UNION_TYPE)
23061 || !variably_modified_type_p (type, NULL))
23062 return;
23065 switch (TREE_CODE (type))
23067 case ERROR_MARK:
23068 break;
23070 case POINTER_TYPE:
23071 case REFERENCE_TYPE:
23072 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
23073 ensures that the gen_type_die recursion will terminate even if the
23074 type is recursive. Recursive types are possible in Ada. */
23075 /* ??? We could perhaps do this for all types before the switch
23076 statement. */
23077 TREE_ASM_WRITTEN (type) = 1;
23079 /* For these types, all that is required is that we output a DIE (or a
23080 set of DIEs) to represent the "basis" type. */
23081 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23082 DINFO_USAGE_IND_USE);
23083 break;
23085 case OFFSET_TYPE:
23086 /* This code is used for C++ pointer-to-data-member types.
23087 Output a description of the relevant class type. */
23088 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
23089 DINFO_USAGE_IND_USE);
23091 /* Output a description of the type of the object pointed to. */
23092 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23093 DINFO_USAGE_IND_USE);
23095 /* Now output a DIE to represent this pointer-to-data-member type
23096 itself. */
23097 gen_ptr_to_mbr_type_die (type, context_die);
23098 break;
23100 case FUNCTION_TYPE:
23101 /* Force out return type (in case it wasn't forced out already). */
23102 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23103 DINFO_USAGE_DIR_USE);
23104 gen_subroutine_type_die (type, context_die);
23105 break;
23107 case METHOD_TYPE:
23108 /* Force out return type (in case it wasn't forced out already). */
23109 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23110 DINFO_USAGE_DIR_USE);
23111 gen_subroutine_type_die (type, context_die);
23112 break;
23114 case ARRAY_TYPE:
23115 case VECTOR_TYPE:
23116 gen_array_type_die (type, context_die);
23117 break;
23119 case ENUMERAL_TYPE:
23120 case RECORD_TYPE:
23121 case UNION_TYPE:
23122 case QUAL_UNION_TYPE:
23123 gen_tagged_type_die (type, context_die, usage);
23124 return;
23126 case VOID_TYPE:
23127 case INTEGER_TYPE:
23128 case REAL_TYPE:
23129 case FIXED_POINT_TYPE:
23130 case COMPLEX_TYPE:
23131 case BOOLEAN_TYPE:
23132 case POINTER_BOUNDS_TYPE:
23133 /* No DIEs needed for fundamental types. */
23134 break;
23136 case NULLPTR_TYPE:
23137 case LANG_TYPE:
23138 /* Just use DW_TAG_unspecified_type. */
23140 dw_die_ref type_die = lookup_type_die (type);
23141 if (type_die == NULL)
23143 tree name = TYPE_IDENTIFIER (type);
23144 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
23145 type);
23146 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
23147 equate_type_number_to_die (type, type_die);
23150 break;
23152 default:
23153 if (is_cxx_auto (type))
23155 tree name = TYPE_IDENTIFIER (type);
23156 dw_die_ref *die = (name == get_identifier ("auto")
23157 ? &auto_die : &decltype_auto_die);
23158 if (!*die)
23160 *die = new_die (DW_TAG_unspecified_type,
23161 comp_unit_die (), NULL_TREE);
23162 add_name_attribute (*die, IDENTIFIER_POINTER (name));
23164 equate_type_number_to_die (type, *die);
23165 break;
23167 gcc_unreachable ();
23170 TREE_ASM_WRITTEN (type) = 1;
23173 static void
23174 gen_type_die (tree type, dw_die_ref context_die)
23176 if (type != error_mark_node)
23178 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
23179 if (flag_checking)
23181 dw_die_ref die = lookup_type_die (type);
23182 if (die)
23183 check_die (die);
23188 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
23189 things which are local to the given block. */
23191 static void
23192 gen_block_die (tree stmt, dw_die_ref context_die)
23194 int must_output_die = 0;
23195 bool inlined_func;
23197 /* Ignore blocks that are NULL. */
23198 if (stmt == NULL_TREE)
23199 return;
23201 inlined_func = inlined_function_outer_scope_p (stmt);
23203 /* If the block is one fragment of a non-contiguous block, do not
23204 process the variables, since they will have been done by the
23205 origin block. Do process subblocks. */
23206 if (BLOCK_FRAGMENT_ORIGIN (stmt))
23208 tree sub;
23210 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
23211 gen_block_die (sub, context_die);
23213 return;
23216 /* Determine if we need to output any Dwarf DIEs at all to represent this
23217 block. */
23218 if (inlined_func)
23219 /* The outer scopes for inlinings *must* always be represented. We
23220 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
23221 must_output_die = 1;
23222 else
23224 /* Determine if this block directly contains any "significant"
23225 local declarations which we will need to output DIEs for. */
23226 if (debug_info_level > DINFO_LEVEL_TERSE)
23227 /* We are not in terse mode so *any* local declaration counts
23228 as being a "significant" one. */
23229 must_output_die = ((BLOCK_VARS (stmt) != NULL
23230 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
23231 && (TREE_USED (stmt)
23232 || TREE_ASM_WRITTEN (stmt)
23233 || BLOCK_ABSTRACT (stmt)));
23234 else if ((TREE_USED (stmt)
23235 || TREE_ASM_WRITTEN (stmt)
23236 || BLOCK_ABSTRACT (stmt))
23237 && !dwarf2out_ignore_block (stmt))
23238 must_output_die = 1;
23241 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
23242 DIE for any block which contains no significant local declarations at
23243 all. Rather, in such cases we just call `decls_for_scope' so that any
23244 needed Dwarf info for any sub-blocks will get properly generated. Note
23245 that in terse mode, our definition of what constitutes a "significant"
23246 local declaration gets restricted to include only inlined function
23247 instances and local (nested) function definitions. */
23248 if (must_output_die)
23250 if (inlined_func)
23252 /* If STMT block is abstract, that means we have been called
23253 indirectly from dwarf2out_abstract_function.
23254 That function rightfully marks the descendent blocks (of
23255 the abstract function it is dealing with) as being abstract,
23256 precisely to prevent us from emitting any
23257 DW_TAG_inlined_subroutine DIE as a descendent
23258 of an abstract function instance. So in that case, we should
23259 not call gen_inlined_subroutine_die.
23261 Later though, when cgraph asks dwarf2out to emit info
23262 for the concrete instance of the function decl into which
23263 the concrete instance of STMT got inlined, the later will lead
23264 to the generation of a DW_TAG_inlined_subroutine DIE. */
23265 if (! BLOCK_ABSTRACT (stmt))
23266 gen_inlined_subroutine_die (stmt, context_die);
23268 else
23269 gen_lexical_block_die (stmt, context_die);
23271 else
23272 decls_for_scope (stmt, context_die);
23275 /* Process variable DECL (or variable with origin ORIGIN) within
23276 block STMT and add it to CONTEXT_DIE. */
23277 static void
23278 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23280 dw_die_ref die;
23281 tree decl_or_origin = decl ? decl : origin;
23283 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23284 die = lookup_decl_die (decl_or_origin);
23285 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
23287 if (TYPE_DECL_IS_STUB (decl_or_origin))
23288 die = lookup_type_die (TREE_TYPE (decl_or_origin));
23289 else
23290 die = lookup_decl_die (decl_or_origin);
23291 /* Avoid re-creating the DIE late if it was optimized as unused early. */
23292 if (! die && ! early_dwarf)
23293 return;
23295 else
23296 die = NULL;
23298 if (die != NULL && die->die_parent == NULL)
23299 add_child_die (context_die, die);
23300 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23302 if (early_dwarf)
23303 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23304 stmt, context_die);
23306 else
23307 gen_decl_die (decl, origin, NULL, context_die);
23310 /* Generate all of the decls declared within a given scope and (recursively)
23311 all of its sub-blocks. */
23313 static void
23314 decls_for_scope (tree stmt, dw_die_ref context_die)
23316 tree decl;
23317 unsigned int i;
23318 tree subblocks;
23320 /* Ignore NULL blocks. */
23321 if (stmt == NULL_TREE)
23322 return;
23324 /* Output the DIEs to represent all of the data objects and typedefs
23325 declared directly within this block but not within any nested
23326 sub-blocks. Also, nested function and tag DIEs have been
23327 generated with a parent of NULL; fix that up now. We don't
23328 have to do this if we're at -g1. */
23329 if (debug_info_level > DINFO_LEVEL_TERSE)
23331 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23332 process_scope_var (stmt, decl, NULL_TREE, context_die);
23333 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
23334 origin - avoid doing this twice as we have no good way to see
23335 if we've done it once already. */
23336 if (! early_dwarf)
23337 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23338 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23339 context_die);
23342 /* Even if we're at -g1, we need to process the subblocks in order to get
23343 inlined call information. */
23345 /* Output the DIEs to represent all sub-blocks (and the items declared
23346 therein) of this block. */
23347 for (subblocks = BLOCK_SUBBLOCKS (stmt);
23348 subblocks != NULL;
23349 subblocks = BLOCK_CHAIN (subblocks))
23350 gen_block_die (subblocks, context_die);
23353 /* Is this a typedef we can avoid emitting? */
23355 bool
23356 is_redundant_typedef (const_tree decl)
23358 if (TYPE_DECL_IS_STUB (decl))
23359 return true;
23361 if (DECL_ARTIFICIAL (decl)
23362 && DECL_CONTEXT (decl)
23363 && is_tagged_type (DECL_CONTEXT (decl))
23364 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23365 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23366 /* Also ignore the artificial member typedef for the class name. */
23367 return true;
23369 return false;
23372 /* Return TRUE if TYPE is a typedef that names a type for linkage
23373 purposes. This kind of typedefs is produced by the C++ FE for
23374 constructs like:
23376 typedef struct {...} foo;
23378 In that case, there is no typedef variant type produced for foo.
23379 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23380 struct type. */
23382 static bool
23383 is_naming_typedef_decl (const_tree decl)
23385 if (decl == NULL_TREE
23386 || TREE_CODE (decl) != TYPE_DECL
23387 || DECL_NAMELESS (decl)
23388 || !is_tagged_type (TREE_TYPE (decl))
23389 || DECL_IS_BUILTIN (decl)
23390 || is_redundant_typedef (decl)
23391 /* It looks like Ada produces TYPE_DECLs that are very similar
23392 to C++ naming typedefs but that have different
23393 semantics. Let's be specific to c++ for now. */
23394 || !is_cxx ())
23395 return FALSE;
23397 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23398 && TYPE_NAME (TREE_TYPE (decl)) == decl
23399 && (TYPE_STUB_DECL (TREE_TYPE (decl))
23400 != TYPE_NAME (TREE_TYPE (decl))));
23403 /* Looks up the DIE for a context. */
23405 static inline dw_die_ref
23406 lookup_context_die (tree context)
23408 if (context)
23410 /* Find die that represents this context. */
23411 if (TYPE_P (context))
23413 context = TYPE_MAIN_VARIANT (context);
23414 dw_die_ref ctx = lookup_type_die (context);
23415 if (!ctx)
23416 return NULL;
23417 return strip_naming_typedef (context, ctx);
23419 else
23420 return lookup_decl_die (context);
23422 return comp_unit_die ();
23425 /* Returns the DIE for a context. */
23427 static inline dw_die_ref
23428 get_context_die (tree context)
23430 if (context)
23432 /* Find die that represents this context. */
23433 if (TYPE_P (context))
23435 context = TYPE_MAIN_VARIANT (context);
23436 return strip_naming_typedef (context, force_type_die (context));
23438 else
23439 return force_decl_die (context);
23441 return comp_unit_die ();
23444 /* Returns the DIE for decl. A DIE will always be returned. */
23446 static dw_die_ref
23447 force_decl_die (tree decl)
23449 dw_die_ref decl_die;
23450 unsigned saved_external_flag;
23451 tree save_fn = NULL_TREE;
23452 decl_die = lookup_decl_die (decl);
23453 if (!decl_die)
23455 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23457 decl_die = lookup_decl_die (decl);
23458 if (decl_die)
23459 return decl_die;
23461 switch (TREE_CODE (decl))
23463 case FUNCTION_DECL:
23464 /* Clear current_function_decl, so that gen_subprogram_die thinks
23465 that this is a declaration. At this point, we just want to force
23466 declaration die. */
23467 save_fn = current_function_decl;
23468 current_function_decl = NULL_TREE;
23469 gen_subprogram_die (decl, context_die);
23470 current_function_decl = save_fn;
23471 break;
23473 case VAR_DECL:
23474 /* Set external flag to force declaration die. Restore it after
23475 gen_decl_die() call. */
23476 saved_external_flag = DECL_EXTERNAL (decl);
23477 DECL_EXTERNAL (decl) = 1;
23478 gen_decl_die (decl, NULL, NULL, context_die);
23479 DECL_EXTERNAL (decl) = saved_external_flag;
23480 break;
23482 case NAMESPACE_DECL:
23483 if (dwarf_version >= 3 || !dwarf_strict)
23484 dwarf2out_decl (decl);
23485 else
23486 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
23487 decl_die = comp_unit_die ();
23488 break;
23490 case TRANSLATION_UNIT_DECL:
23491 decl_die = comp_unit_die ();
23492 break;
23494 default:
23495 gcc_unreachable ();
23498 /* We should be able to find the DIE now. */
23499 if (!decl_die)
23500 decl_die = lookup_decl_die (decl);
23501 gcc_assert (decl_die);
23504 return decl_die;
23507 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
23508 always returned. */
23510 static dw_die_ref
23511 force_type_die (tree type)
23513 dw_die_ref type_die;
23515 type_die = lookup_type_die (type);
23516 if (!type_die)
23518 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23520 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23521 false, context_die);
23522 gcc_assert (type_die);
23524 return type_die;
23527 /* Force out any required namespaces to be able to output DECL,
23528 and return the new context_die for it, if it's changed. */
23530 static dw_die_ref
23531 setup_namespace_context (tree thing, dw_die_ref context_die)
23533 tree context = (DECL_P (thing)
23534 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23535 if (context && TREE_CODE (context) == NAMESPACE_DECL)
23536 /* Force out the namespace. */
23537 context_die = force_decl_die (context);
23539 return context_die;
23542 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23543 type) within its namespace, if appropriate.
23545 For compatibility with older debuggers, namespace DIEs only contain
23546 declarations; all definitions are emitted at CU scope, with
23547 DW_AT_specification pointing to the declaration (like with class
23548 members). */
23550 static dw_die_ref
23551 declare_in_namespace (tree thing, dw_die_ref context_die)
23553 dw_die_ref ns_context;
23555 if (debug_info_level <= DINFO_LEVEL_TERSE)
23556 return context_die;
23558 /* External declarations in the local scope only need to be emitted
23559 once, not once in the namespace and once in the scope.
23561 This avoids declaring the `extern' below in the
23562 namespace DIE as well as in the innermost scope:
23564 namespace S
23566 int i=5;
23567 int foo()
23569 int i=8;
23570 extern int i;
23571 return i;
23575 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23576 return context_die;
23578 /* If this decl is from an inlined function, then don't try to emit it in its
23579 namespace, as we will get confused. It would have already been emitted
23580 when the abstract instance of the inline function was emitted anyways. */
23581 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23582 return context_die;
23584 ns_context = setup_namespace_context (thing, context_die);
23586 if (ns_context != context_die)
23588 if (is_fortran ())
23589 return ns_context;
23590 if (DECL_P (thing))
23591 gen_decl_die (thing, NULL, NULL, ns_context);
23592 else
23593 gen_type_die (thing, ns_context);
23595 return context_die;
23598 /* Generate a DIE for a namespace or namespace alias. */
23600 static void
23601 gen_namespace_die (tree decl, dw_die_ref context_die)
23603 dw_die_ref namespace_die;
23605 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23606 they are an alias of. */
23607 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23609 /* Output a real namespace or module. */
23610 context_die = setup_namespace_context (decl, comp_unit_die ());
23611 namespace_die = new_die (is_fortran ()
23612 ? DW_TAG_module : DW_TAG_namespace,
23613 context_die, decl);
23614 /* For Fortran modules defined in different CU don't add src coords. */
23615 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23617 const char *name = dwarf2_name (decl, 0);
23618 if (name)
23619 add_name_attribute (namespace_die, name);
23621 else
23622 add_name_and_src_coords_attributes (namespace_die, decl);
23623 if (DECL_EXTERNAL (decl))
23624 add_AT_flag (namespace_die, DW_AT_declaration, 1);
23625 equate_decl_number_to_die (decl, namespace_die);
23627 else
23629 /* Output a namespace alias. */
23631 /* Force out the namespace we are an alias of, if necessary. */
23632 dw_die_ref origin_die
23633 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23635 if (DECL_FILE_SCOPE_P (decl)
23636 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23637 context_die = setup_namespace_context (decl, comp_unit_die ());
23638 /* Now create the namespace alias DIE. */
23639 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23640 add_name_and_src_coords_attributes (namespace_die, decl);
23641 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23642 equate_decl_number_to_die (decl, namespace_die);
23644 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
23645 if (want_pubnames ())
23646 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23649 /* Generate Dwarf debug information for a decl described by DECL.
23650 The return value is currently only meaningful for PARM_DECLs,
23651 for all other decls it returns NULL.
23653 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23654 It can be NULL otherwise. */
23656 static dw_die_ref
23657 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23658 dw_die_ref context_die)
23660 tree decl_or_origin = decl ? decl : origin;
23661 tree class_origin = NULL, ultimate_origin;
23663 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23664 return NULL;
23666 /* Ignore pointer bounds decls. */
23667 if (DECL_P (decl_or_origin)
23668 && TREE_TYPE (decl_or_origin)
23669 && POINTER_BOUNDS_P (decl_or_origin))
23670 return NULL;
23672 switch (TREE_CODE (decl_or_origin))
23674 case ERROR_MARK:
23675 break;
23677 case CONST_DECL:
23678 if (!is_fortran () && !is_ada ())
23680 /* The individual enumerators of an enum type get output when we output
23681 the Dwarf representation of the relevant enum type itself. */
23682 break;
23685 /* Emit its type. */
23686 gen_type_die (TREE_TYPE (decl), context_die);
23688 /* And its containing namespace. */
23689 context_die = declare_in_namespace (decl, context_die);
23691 gen_const_die (decl, context_die);
23692 break;
23694 case FUNCTION_DECL:
23695 /* Don't output any DIEs to represent mere function declarations,
23696 unless they are class members or explicit block externs. */
23697 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23698 && DECL_FILE_SCOPE_P (decl_or_origin)
23699 && (current_function_decl == NULL_TREE
23700 || DECL_ARTIFICIAL (decl_or_origin)))
23701 break;
23703 #if 0
23704 /* FIXME */
23705 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23706 on local redeclarations of global functions. That seems broken. */
23707 if (current_function_decl != decl)
23708 /* This is only a declaration. */;
23709 #endif
23711 /* If we're emitting a clone, emit info for the abstract instance. */
23712 if (origin || DECL_ORIGIN (decl) != decl)
23713 dwarf2out_abstract_function (origin
23714 ? DECL_ORIGIN (origin)
23715 : DECL_ABSTRACT_ORIGIN (decl));
23717 /* If we're emitting an out-of-line copy of an inline function,
23718 emit info for the abstract instance and set up to refer to it. */
23719 else if (cgraph_function_possibly_inlined_p (decl)
23720 && ! DECL_ABSTRACT_P (decl)
23721 && ! class_or_namespace_scope_p (context_die)
23722 /* dwarf2out_abstract_function won't emit a die if this is just
23723 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
23724 that case, because that works only if we have a die. */
23725 && DECL_INITIAL (decl) != NULL_TREE)
23727 dwarf2out_abstract_function (decl);
23728 set_decl_origin_self (decl);
23731 /* Otherwise we're emitting the primary DIE for this decl. */
23732 else if (debug_info_level > DINFO_LEVEL_TERSE)
23734 /* Before we describe the FUNCTION_DECL itself, make sure that we
23735 have its containing type. */
23736 if (!origin)
23737 origin = decl_class_context (decl);
23738 if (origin != NULL_TREE)
23739 gen_type_die (origin, context_die);
23741 /* And its return type. */
23742 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23744 /* And its virtual context. */
23745 if (DECL_VINDEX (decl) != NULL_TREE)
23746 gen_type_die (DECL_CONTEXT (decl), context_die);
23748 /* Make sure we have a member DIE for decl. */
23749 if (origin != NULL_TREE)
23750 gen_type_die_for_member (origin, decl, context_die);
23752 /* And its containing namespace. */
23753 context_die = declare_in_namespace (decl, context_die);
23756 /* Now output a DIE to represent the function itself. */
23757 if (decl)
23758 gen_subprogram_die (decl, context_die);
23759 break;
23761 case TYPE_DECL:
23762 /* If we are in terse mode, don't generate any DIEs to represent any
23763 actual typedefs. */
23764 if (debug_info_level <= DINFO_LEVEL_TERSE)
23765 break;
23767 /* In the special case of a TYPE_DECL node representing the declaration
23768 of some type tag, if the given TYPE_DECL is marked as having been
23769 instantiated from some other (original) TYPE_DECL node (e.g. one which
23770 was generated within the original definition of an inline function) we
23771 used to generate a special (abbreviated) DW_TAG_structure_type,
23772 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
23773 should be actually referencing those DIEs, as variable DIEs with that
23774 type would be emitted already in the abstract origin, so it was always
23775 removed during unused type prunning. Don't add anything in this
23776 case. */
23777 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23778 break;
23780 if (is_redundant_typedef (decl))
23781 gen_type_die (TREE_TYPE (decl), context_die);
23782 else
23783 /* Output a DIE to represent the typedef itself. */
23784 gen_typedef_die (decl, context_die);
23785 break;
23787 case LABEL_DECL:
23788 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23789 gen_label_die (decl, context_die);
23790 break;
23792 case VAR_DECL:
23793 case RESULT_DECL:
23794 /* If we are in terse mode, don't generate any DIEs to represent any
23795 variable declarations or definitions. */
23796 if (debug_info_level <= DINFO_LEVEL_TERSE)
23797 break;
23799 /* Output any DIEs that are needed to specify the type of this data
23800 object. */
23801 if (decl_by_reference_p (decl_or_origin))
23802 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23803 else
23804 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23806 /* And its containing type. */
23807 class_origin = decl_class_context (decl_or_origin);
23808 if (class_origin != NULL_TREE)
23809 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23811 /* And its containing namespace. */
23812 context_die = declare_in_namespace (decl_or_origin, context_die);
23814 /* Now output the DIE to represent the data object itself. This gets
23815 complicated because of the possibility that the VAR_DECL really
23816 represents an inlined instance of a formal parameter for an inline
23817 function. */
23818 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23819 if (ultimate_origin != NULL_TREE
23820 && TREE_CODE (ultimate_origin) == PARM_DECL)
23821 gen_formal_parameter_die (decl, origin,
23822 true /* Emit name attribute. */,
23823 context_die);
23824 else
23825 gen_variable_die (decl, origin, context_die);
23826 break;
23828 case FIELD_DECL:
23829 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23830 /* Ignore the nameless fields that are used to skip bits but handle C++
23831 anonymous unions and structs. */
23832 if (DECL_NAME (decl) != NULL_TREE
23833 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23834 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23836 gen_type_die (member_declared_type (decl), context_die);
23837 gen_field_die (decl, ctx, context_die);
23839 break;
23841 case PARM_DECL:
23842 if (DECL_BY_REFERENCE (decl_or_origin))
23843 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23844 else
23845 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23846 return gen_formal_parameter_die (decl, origin,
23847 true /* Emit name attribute. */,
23848 context_die);
23850 case NAMESPACE_DECL:
23851 if (dwarf_version >= 3 || !dwarf_strict)
23852 gen_namespace_die (decl, context_die);
23853 break;
23855 case IMPORTED_DECL:
23856 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23857 DECL_CONTEXT (decl), context_die);
23858 break;
23860 case NAMELIST_DECL:
23861 gen_namelist_decl (DECL_NAME (decl), context_die,
23862 NAMELIST_DECL_ASSOCIATED_DECL (decl));
23863 break;
23865 default:
23866 /* Probably some frontend-internal decl. Assume we don't care. */
23867 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23868 break;
23871 return NULL;
23874 /* Output initial debug information for global DECL. Called at the
23875 end of the parsing process.
23877 This is the initial debug generation process. As such, the DIEs
23878 generated may be incomplete. A later debug generation pass
23879 (dwarf2out_late_global_decl) will augment the information generated
23880 in this pass (e.g., with complete location info). */
23882 static void
23883 dwarf2out_early_global_decl (tree decl)
23885 set_early_dwarf s;
23887 /* gen_decl_die() will set DECL_ABSTRACT because
23888 cgraph_function_possibly_inlined_p() returns true. This is in
23889 turn will cause DW_AT_inline attributes to be set.
23891 This happens because at early dwarf generation, there is no
23892 cgraph information, causing cgraph_function_possibly_inlined_p()
23893 to return true. Trick cgraph_function_possibly_inlined_p()
23894 while we generate dwarf early. */
23895 bool save = symtab->global_info_ready;
23896 symtab->global_info_ready = true;
23898 /* We don't handle TYPE_DECLs. If required, they'll be reached via
23899 other DECLs and they can point to template types or other things
23900 that dwarf2out can't handle when done via dwarf2out_decl. */
23901 if (TREE_CODE (decl) != TYPE_DECL
23902 && TREE_CODE (decl) != PARM_DECL)
23904 tree save_fndecl = current_function_decl;
23905 if (TREE_CODE (decl) == FUNCTION_DECL)
23907 /* No cfun means the symbol has no body, so there's nothing
23908 to emit. */
23909 if (!DECL_STRUCT_FUNCTION (decl))
23910 goto early_decl_exit;
23912 /* For nested functions, emit DIEs for the parents first so that all
23913 nested DIEs are generated at the proper scope in the first
23914 shot. */
23915 tree context = decl_function_context (decl);
23916 if (context != NULL)
23918 current_function_decl = context;
23919 dwarf2out_decl (context);
23922 current_function_decl = decl;
23924 dwarf2out_decl (decl);
23925 if (TREE_CODE (decl) == FUNCTION_DECL)
23926 current_function_decl = save_fndecl;
23928 early_decl_exit:
23929 symtab->global_info_ready = save;
23932 /* Output debug information for global decl DECL. Called from
23933 toplev.c after compilation proper has finished. */
23935 static void
23936 dwarf2out_late_global_decl (tree decl)
23938 /* Fill-in any location information we were unable to determine
23939 on the first pass. */
23940 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
23942 dw_die_ref die = lookup_decl_die (decl);
23944 /* We have to generate early debug late for LTO. */
23945 if (! die && in_lto_p)
23947 dwarf2out_decl (decl);
23948 die = lookup_decl_die (decl);
23951 if (die)
23953 /* We get called via the symtab code invoking late_global_decl
23954 for symbols that are optimized out. Do not add locations
23955 for those. */
23956 varpool_node *node = varpool_node::get (decl);
23957 if (! node || ! node->definition)
23958 tree_add_const_value_attribute_for_decl (die, decl);
23959 else
23960 add_location_or_const_value_attribute (die, decl, false);
23965 /* Output debug information for type decl DECL. Called from toplev.c
23966 and from language front ends (to record built-in types). */
23967 static void
23968 dwarf2out_type_decl (tree decl, int local)
23970 if (!local)
23972 set_early_dwarf s;
23973 dwarf2out_decl (decl);
23977 /* Output debug information for imported module or decl DECL.
23978 NAME is non-NULL name in the lexical block if the decl has been renamed.
23979 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23980 that DECL belongs to.
23981 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
23982 static void
23983 dwarf2out_imported_module_or_decl_1 (tree decl,
23984 tree name,
23985 tree lexical_block,
23986 dw_die_ref lexical_block_die)
23988 expanded_location xloc;
23989 dw_die_ref imported_die = NULL;
23990 dw_die_ref at_import_die;
23992 if (TREE_CODE (decl) == IMPORTED_DECL)
23994 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23995 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23996 gcc_assert (decl);
23998 else
23999 xloc = expand_location (input_location);
24001 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
24003 at_import_die = force_type_die (TREE_TYPE (decl));
24004 /* For namespace N { typedef void T; } using N::T; base_type_die
24005 returns NULL, but DW_TAG_imported_declaration requires
24006 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
24007 if (!at_import_die)
24009 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
24010 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
24011 at_import_die = lookup_type_die (TREE_TYPE (decl));
24012 gcc_assert (at_import_die);
24015 else
24017 at_import_die = lookup_decl_die (decl);
24018 if (!at_import_die)
24020 /* If we're trying to avoid duplicate debug info, we may not have
24021 emitted the member decl for this field. Emit it now. */
24022 if (TREE_CODE (decl) == FIELD_DECL)
24024 tree type = DECL_CONTEXT (decl);
24026 if (TYPE_CONTEXT (type)
24027 && TYPE_P (TYPE_CONTEXT (type))
24028 && !should_emit_struct_debug (TYPE_CONTEXT (type),
24029 DINFO_USAGE_DIR_USE))
24030 return;
24031 gen_type_die_for_member (type, decl,
24032 get_context_die (TYPE_CONTEXT (type)));
24034 if (TREE_CODE (decl) == NAMELIST_DECL)
24035 at_import_die = gen_namelist_decl (DECL_NAME (decl),
24036 get_context_die (DECL_CONTEXT (decl)),
24037 NULL_TREE);
24038 else
24039 at_import_die = force_decl_die (decl);
24043 if (TREE_CODE (decl) == NAMESPACE_DECL)
24045 if (dwarf_version >= 3 || !dwarf_strict)
24046 imported_die = new_die (DW_TAG_imported_module,
24047 lexical_block_die,
24048 lexical_block);
24049 else
24050 return;
24052 else
24053 imported_die = new_die (DW_TAG_imported_declaration,
24054 lexical_block_die,
24055 lexical_block);
24057 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
24058 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
24059 if (name)
24060 add_AT_string (imported_die, DW_AT_name,
24061 IDENTIFIER_POINTER (name));
24062 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
24065 /* Output debug information for imported module or decl DECL.
24066 NAME is non-NULL name in context if the decl has been renamed.
24067 CHILD is true if decl is one of the renamed decls as part of
24068 importing whole module. */
24070 static void
24071 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
24072 bool child)
24074 /* dw_die_ref at_import_die; */
24075 dw_die_ref scope_die;
24077 if (debug_info_level <= DINFO_LEVEL_TERSE)
24078 return;
24080 gcc_assert (decl);
24082 set_early_dwarf s;
24084 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
24085 We need decl DIE for reference and scope die. First, get DIE for the decl
24086 itself. */
24088 /* Get the scope die for decl context. Use comp_unit_die for global module
24089 or decl. If die is not found for non globals, force new die. */
24090 if (context
24091 && TYPE_P (context)
24092 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
24093 return;
24095 scope_die = get_context_die (context);
24097 if (child)
24099 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
24100 there is nothing we can do, here. */
24101 if (dwarf_version < 3 && dwarf_strict)
24102 return;
24104 gcc_assert (scope_die->die_child);
24105 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
24106 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
24107 scope_die = scope_die->die_child;
24110 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
24111 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
24114 /* Output debug information for namelists. */
24116 static dw_die_ref
24117 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
24119 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
24120 tree value;
24121 unsigned i;
24123 if (debug_info_level <= DINFO_LEVEL_TERSE)
24124 return NULL;
24126 gcc_assert (scope_die != NULL);
24127 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
24128 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
24130 /* If there are no item_decls, we have a nondefining namelist, e.g.
24131 with USE association; hence, set DW_AT_declaration. */
24132 if (item_decls == NULL_TREE)
24134 add_AT_flag (nml_die, DW_AT_declaration, 1);
24135 return nml_die;
24138 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
24140 nml_item_ref_die = lookup_decl_die (value);
24141 if (!nml_item_ref_die)
24142 nml_item_ref_die = force_decl_die (value);
24144 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
24145 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
24147 return nml_die;
24151 /* Write the debugging output for DECL and return the DIE. */
24153 static void
24154 dwarf2out_decl (tree decl)
24156 dw_die_ref context_die = comp_unit_die ();
24158 switch (TREE_CODE (decl))
24160 case ERROR_MARK:
24161 return;
24163 case FUNCTION_DECL:
24164 /* What we would really like to do here is to filter out all mere
24165 file-scope declarations of file-scope functions which are never
24166 referenced later within this translation unit (and keep all of ones
24167 that *are* referenced later on) but we aren't clairvoyant, so we have
24168 no idea which functions will be referenced in the future (i.e. later
24169 on within the current translation unit). So here we just ignore all
24170 file-scope function declarations which are not also definitions. If
24171 and when the debugger needs to know something about these functions,
24172 it will have to hunt around and find the DWARF information associated
24173 with the definition of the function.
24175 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
24176 nodes represent definitions and which ones represent mere
24177 declarations. We have to check DECL_INITIAL instead. That's because
24178 the C front-end supports some weird semantics for "extern inline"
24179 function definitions. These can get inlined within the current
24180 translation unit (and thus, we need to generate Dwarf info for their
24181 abstract instances so that the Dwarf info for the concrete inlined
24182 instances can have something to refer to) but the compiler never
24183 generates any out-of-lines instances of such things (despite the fact
24184 that they *are* definitions).
24186 The important point is that the C front-end marks these "extern
24187 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
24188 them anyway. Note that the C++ front-end also plays some similar games
24189 for inline function definitions appearing within include files which
24190 also contain `#pragma interface' pragmas.
24192 If we are called from dwarf2out_abstract_function output a DIE
24193 anyway. We can end up here this way with early inlining and LTO
24194 where the inlined function is output in a different LTRANS unit
24195 or not at all. */
24196 if (DECL_INITIAL (decl) == NULL_TREE
24197 && ! DECL_ABSTRACT_P (decl))
24198 return;
24200 /* If we're a nested function, initially use a parent of NULL; if we're
24201 a plain function, this will be fixed up in decls_for_scope. If
24202 we're a method, it will be ignored, since we already have a DIE. */
24203 if (decl_function_context (decl)
24204 /* But if we're in terse mode, we don't care about scope. */
24205 && debug_info_level > DINFO_LEVEL_TERSE)
24206 context_die = NULL;
24207 break;
24209 case VAR_DECL:
24210 /* For local statics lookup proper context die. */
24211 if (local_function_static (decl))
24212 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24214 /* If we are in terse mode, don't generate any DIEs to represent any
24215 variable declarations or definitions. */
24216 if (debug_info_level <= DINFO_LEVEL_TERSE)
24217 return;
24218 break;
24220 case CONST_DECL:
24221 if (debug_info_level <= DINFO_LEVEL_TERSE)
24222 return;
24223 if (!is_fortran () && !is_ada ())
24224 return;
24225 if (TREE_STATIC (decl) && decl_function_context (decl))
24226 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24227 break;
24229 case NAMESPACE_DECL:
24230 case IMPORTED_DECL:
24231 if (debug_info_level <= DINFO_LEVEL_TERSE)
24232 return;
24233 if (lookup_decl_die (decl) != NULL)
24234 return;
24235 break;
24237 case TYPE_DECL:
24238 /* Don't emit stubs for types unless they are needed by other DIEs. */
24239 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
24240 return;
24242 /* Don't bother trying to generate any DIEs to represent any of the
24243 normal built-in types for the language we are compiling. */
24244 if (DECL_IS_BUILTIN (decl))
24245 return;
24247 /* If we are in terse mode, don't generate any DIEs for types. */
24248 if (debug_info_level <= DINFO_LEVEL_TERSE)
24249 return;
24251 /* If we're a function-scope tag, initially use a parent of NULL;
24252 this will be fixed up in decls_for_scope. */
24253 if (decl_function_context (decl))
24254 context_die = NULL;
24256 break;
24258 case NAMELIST_DECL:
24259 break;
24261 default:
24262 return;
24265 gen_decl_die (decl, NULL, NULL, context_die);
24267 if (flag_checking)
24269 dw_die_ref die = lookup_decl_die (decl);
24270 if (die)
24271 check_die (die);
24275 /* Write the debugging output for DECL. */
24277 static void
24278 dwarf2out_function_decl (tree decl)
24280 dwarf2out_decl (decl);
24281 call_arg_locations = NULL;
24282 call_arg_loc_last = NULL;
24283 call_site_count = -1;
24284 tail_call_site_count = -1;
24285 decl_loc_table->empty ();
24286 cached_dw_loc_list_table->empty ();
24289 /* Output a marker (i.e. a label) for the beginning of the generated code for
24290 a lexical block. */
24292 static void
24293 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
24294 unsigned int blocknum)
24296 switch_to_section (current_function_section ());
24297 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
24300 /* Output a marker (i.e. a label) for the end of the generated code for a
24301 lexical block. */
24303 static void
24304 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
24306 switch_to_section (current_function_section ());
24307 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
24310 /* Returns nonzero if it is appropriate not to emit any debugging
24311 information for BLOCK, because it doesn't contain any instructions.
24313 Don't allow this for blocks with nested functions or local classes
24314 as we would end up with orphans, and in the presence of scheduling
24315 we may end up calling them anyway. */
24317 static bool
24318 dwarf2out_ignore_block (const_tree block)
24320 tree decl;
24321 unsigned int i;
24323 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24324 if (TREE_CODE (decl) == FUNCTION_DECL
24325 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24326 return 0;
24327 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24329 decl = BLOCK_NONLOCALIZED_VAR (block, i);
24330 if (TREE_CODE (decl) == FUNCTION_DECL
24331 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24332 return 0;
24335 return 1;
24338 /* Hash table routines for file_hash. */
24340 bool
24341 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24343 return filename_cmp (p1->filename, p2) == 0;
24346 hashval_t
24347 dwarf_file_hasher::hash (dwarf_file_data *p)
24349 return htab_hash_string (p->filename);
24352 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24353 dwarf2out.c) and return its "index". The index of each (known) filename is
24354 just a unique number which is associated with only that one filename. We
24355 need such numbers for the sake of generating labels (in the .debug_sfnames
24356 section) and references to those files numbers (in the .debug_srcinfo
24357 and .debug_macinfo sections). If the filename given as an argument is not
24358 found in our current list, add it to the list and assign it the next
24359 available unique index number. */
24361 static struct dwarf_file_data *
24362 lookup_filename (const char *file_name)
24364 struct dwarf_file_data * created;
24366 if (!file_name)
24367 return NULL;
24369 dwarf_file_data **slot
24370 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24371 INSERT);
24372 if (*slot)
24373 return *slot;
24375 created = ggc_alloc<dwarf_file_data> ();
24376 created->filename = file_name;
24377 created->emitted_number = 0;
24378 *slot = created;
24379 return created;
24382 /* If the assembler will construct the file table, then translate the compiler
24383 internal file table number into the assembler file table number, and emit
24384 a .file directive if we haven't already emitted one yet. The file table
24385 numbers are different because we prune debug info for unused variables and
24386 types, which may include filenames. */
24388 static int
24389 maybe_emit_file (struct dwarf_file_data * fd)
24391 if (! fd->emitted_number)
24393 if (last_emitted_file)
24394 fd->emitted_number = last_emitted_file->emitted_number + 1;
24395 else
24396 fd->emitted_number = 1;
24397 last_emitted_file = fd;
24399 if (DWARF2_ASM_LINE_DEBUG_INFO)
24401 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24402 output_quoted_string (asm_out_file,
24403 remap_debug_filename (fd->filename));
24404 fputc ('\n', asm_out_file);
24408 return fd->emitted_number;
24411 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24412 That generation should happen after function debug info has been
24413 generated. The value of the attribute is the constant value of ARG. */
24415 static void
24416 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24418 die_arg_entry entry;
24420 if (!die || !arg)
24421 return;
24423 gcc_assert (early_dwarf);
24425 if (!tmpl_value_parm_die_table)
24426 vec_alloc (tmpl_value_parm_die_table, 32);
24428 entry.die = die;
24429 entry.arg = arg;
24430 vec_safe_push (tmpl_value_parm_die_table, entry);
24433 /* Return TRUE if T is an instance of generic type, FALSE
24434 otherwise. */
24436 static bool
24437 generic_type_p (tree t)
24439 if (t == NULL_TREE || !TYPE_P (t))
24440 return false;
24441 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24444 /* Schedule the generation of the generic parameter dies for the
24445 instance of generic type T. The proper generation itself is later
24446 done by gen_scheduled_generic_parms_dies. */
24448 static void
24449 schedule_generic_params_dies_gen (tree t)
24451 if (!generic_type_p (t))
24452 return;
24454 gcc_assert (early_dwarf);
24456 if (!generic_type_instances)
24457 vec_alloc (generic_type_instances, 256);
24459 vec_safe_push (generic_type_instances, t);
24462 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24463 by append_entry_to_tmpl_value_parm_die_table. This function must
24464 be called after function DIEs have been generated. */
24466 static void
24467 gen_remaining_tmpl_value_param_die_attribute (void)
24469 if (tmpl_value_parm_die_table)
24471 unsigned i, j;
24472 die_arg_entry *e;
24474 /* We do this in two phases - first get the cases we can
24475 handle during early-finish, preserving those we cannot
24476 (containing symbolic constants where we don't yet know
24477 whether we are going to output the referenced symbols).
24478 For those we try again at late-finish. */
24479 j = 0;
24480 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24482 if (!tree_add_const_value_attribute (e->die, e->arg))
24484 dw_loc_descr_ref loc = NULL;
24485 if (! early_dwarf
24486 && (dwarf_version >= 5 || !dwarf_strict))
24487 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
24488 if (loc)
24489 add_AT_loc (e->die, DW_AT_location, loc);
24490 else
24491 (*tmpl_value_parm_die_table)[j++] = *e;
24494 tmpl_value_parm_die_table->truncate (j);
24498 /* Generate generic parameters DIEs for instances of generic types
24499 that have been previously scheduled by
24500 schedule_generic_params_dies_gen. This function must be called
24501 after all the types of the CU have been laid out. */
24503 static void
24504 gen_scheduled_generic_parms_dies (void)
24506 unsigned i;
24507 tree t;
24509 if (!generic_type_instances)
24510 return;
24512 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24513 if (COMPLETE_TYPE_P (t))
24514 gen_generic_params_dies (t);
24516 generic_type_instances = NULL;
24520 /* Replace DW_AT_name for the decl with name. */
24522 static void
24523 dwarf2out_set_name (tree decl, tree name)
24525 dw_die_ref die;
24526 dw_attr_node *attr;
24527 const char *dname;
24529 die = TYPE_SYMTAB_DIE (decl);
24530 if (!die)
24531 return;
24533 dname = dwarf2_name (name, 0);
24534 if (!dname)
24535 return;
24537 attr = get_AT (die, DW_AT_name);
24538 if (attr)
24540 struct indirect_string_node *node;
24542 node = find_AT_string (dname);
24543 /* replace the string. */
24544 attr->dw_attr_val.v.val_str = node;
24547 else
24548 add_name_attribute (die, dname);
24551 /* True if before or during processing of the first function being emitted. */
24552 static bool in_first_function_p = true;
24553 /* True if loc_note during dwarf2out_var_location call might still be
24554 before first real instruction at address equal to .Ltext0. */
24555 static bool maybe_at_text_label_p = true;
24556 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
24557 static unsigned int first_loclabel_num_not_at_text_label;
24559 /* Called by the final INSN scan whenever we see a var location. We
24560 use it to drop labels in the right places, and throw the location in
24561 our lookup table. */
24563 static void
24564 dwarf2out_var_location (rtx_insn *loc_note)
24566 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24567 struct var_loc_node *newloc;
24568 rtx_insn *next_real, *next_note;
24569 rtx_insn *call_insn = NULL;
24570 static const char *last_label;
24571 static const char *last_postcall_label;
24572 static bool last_in_cold_section_p;
24573 static rtx_insn *expected_next_loc_note;
24574 tree decl;
24575 bool var_loc_p;
24577 if (!NOTE_P (loc_note))
24579 if (CALL_P (loc_note))
24581 call_site_count++;
24582 if (SIBLING_CALL_P (loc_note))
24583 tail_call_site_count++;
24584 if (optimize == 0 && !flag_var_tracking)
24586 /* When the var-tracking pass is not running, there is no note
24587 for indirect calls whose target is compile-time known. In this
24588 case, process such calls specifically so that we generate call
24589 sites for them anyway. */
24590 rtx x = PATTERN (loc_note);
24591 if (GET_CODE (x) == PARALLEL)
24592 x = XVECEXP (x, 0, 0);
24593 if (GET_CODE (x) == SET)
24594 x = SET_SRC (x);
24595 if (GET_CODE (x) == CALL)
24596 x = XEXP (x, 0);
24597 if (!MEM_P (x)
24598 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24599 || !SYMBOL_REF_DECL (XEXP (x, 0))
24600 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24601 != FUNCTION_DECL))
24603 call_insn = loc_note;
24604 loc_note = NULL;
24605 var_loc_p = false;
24607 next_real = next_real_insn (call_insn);
24608 next_note = NULL;
24609 cached_next_real_insn = NULL;
24610 goto create_label;
24614 return;
24617 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24618 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24619 return;
24621 /* Optimize processing a large consecutive sequence of location
24622 notes so we don't spend too much time in next_real_insn. If the
24623 next insn is another location note, remember the next_real_insn
24624 calculation for next time. */
24625 next_real = cached_next_real_insn;
24626 if (next_real)
24628 if (expected_next_loc_note != loc_note)
24629 next_real = NULL;
24632 next_note = NEXT_INSN (loc_note);
24633 if (! next_note
24634 || next_note->deleted ()
24635 || ! NOTE_P (next_note)
24636 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24637 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24638 next_note = NULL;
24640 if (! next_real)
24641 next_real = next_real_insn (loc_note);
24643 if (next_note)
24645 expected_next_loc_note = next_note;
24646 cached_next_real_insn = next_real;
24648 else
24649 cached_next_real_insn = NULL;
24651 /* If there are no instructions which would be affected by this note,
24652 don't do anything. */
24653 if (var_loc_p
24654 && next_real == NULL_RTX
24655 && !NOTE_DURING_CALL_P (loc_note))
24656 return;
24658 create_label:
24660 if (next_real == NULL_RTX)
24661 next_real = get_last_insn ();
24663 /* If there were any real insns between note we processed last time
24664 and this note (or if it is the first note), clear
24665 last_{,postcall_}label so that they are not reused this time. */
24666 if (last_var_location_insn == NULL_RTX
24667 || last_var_location_insn != next_real
24668 || last_in_cold_section_p != in_cold_section_p)
24670 last_label = NULL;
24671 last_postcall_label = NULL;
24674 if (var_loc_p)
24676 decl = NOTE_VAR_LOCATION_DECL (loc_note);
24677 newloc = add_var_loc_to_decl (decl, loc_note,
24678 NOTE_DURING_CALL_P (loc_note)
24679 ? last_postcall_label : last_label);
24680 if (newloc == NULL)
24681 return;
24683 else
24685 decl = NULL_TREE;
24686 newloc = NULL;
24689 /* If there were no real insns between note we processed last time
24690 and this note, use the label we emitted last time. Otherwise
24691 create a new label and emit it. */
24692 if (last_label == NULL)
24694 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24695 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24696 loclabel_num++;
24697 last_label = ggc_strdup (loclabel);
24698 /* See if loclabel might be equal to .Ltext0. If yes,
24699 bump first_loclabel_num_not_at_text_label. */
24700 if (!have_multiple_function_sections
24701 && in_first_function_p
24702 && maybe_at_text_label_p)
24704 static rtx_insn *last_start;
24705 rtx_insn *insn;
24706 for (insn = loc_note; insn; insn = previous_insn (insn))
24707 if (insn == last_start)
24708 break;
24709 else if (!NONDEBUG_INSN_P (insn))
24710 continue;
24711 else
24713 rtx body = PATTERN (insn);
24714 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24715 continue;
24716 /* Inline asm could occupy zero bytes. */
24717 else if (GET_CODE (body) == ASM_INPUT
24718 || asm_noperands (body) >= 0)
24719 continue;
24720 #ifdef HAVE_attr_length
24721 else if (get_attr_min_length (insn) == 0)
24722 continue;
24723 #endif
24724 else
24726 /* Assume insn has non-zero length. */
24727 maybe_at_text_label_p = false;
24728 break;
24731 if (maybe_at_text_label_p)
24733 last_start = loc_note;
24734 first_loclabel_num_not_at_text_label = loclabel_num;
24739 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24740 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24742 if (!var_loc_p)
24744 struct call_arg_loc_node *ca_loc
24745 = ggc_cleared_alloc<call_arg_loc_node> ();
24746 rtx_insn *prev
24747 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24749 ca_loc->call_arg_loc_note = loc_note;
24750 ca_loc->next = NULL;
24751 ca_loc->label = last_label;
24752 gcc_assert (prev
24753 && (CALL_P (prev)
24754 || (NONJUMP_INSN_P (prev)
24755 && GET_CODE (PATTERN (prev)) == SEQUENCE
24756 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24757 if (!CALL_P (prev))
24758 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24759 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24761 /* Look for a SYMBOL_REF in the "prev" instruction. */
24762 rtx x = get_call_rtx_from (PATTERN (prev));
24763 if (x)
24765 /* Try to get the call symbol, if any. */
24766 if (MEM_P (XEXP (x, 0)))
24767 x = XEXP (x, 0);
24768 /* First, look for a memory access to a symbol_ref. */
24769 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24770 && SYMBOL_REF_DECL (XEXP (x, 0))
24771 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24772 ca_loc->symbol_ref = XEXP (x, 0);
24773 /* Otherwise, look at a compile-time known user-level function
24774 declaration. */
24775 else if (MEM_P (x)
24776 && MEM_EXPR (x)
24777 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24778 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24781 ca_loc->block = insn_scope (prev);
24782 if (call_arg_locations)
24783 call_arg_loc_last->next = ca_loc;
24784 else
24785 call_arg_locations = ca_loc;
24786 call_arg_loc_last = ca_loc;
24788 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24789 newloc->label = last_label;
24790 else
24792 if (!last_postcall_label)
24794 sprintf (loclabel, "%s-1", last_label);
24795 last_postcall_label = ggc_strdup (loclabel);
24797 newloc->label = last_postcall_label;
24800 last_var_location_insn = next_real;
24801 last_in_cold_section_p = in_cold_section_p;
24804 /* Called from finalize_size_functions for size functions so that their body
24805 can be encoded in the debug info to describe the layout of variable-length
24806 structures. */
24808 static void
24809 dwarf2out_size_function (tree decl)
24811 function_to_dwarf_procedure (decl);
24814 /* Note in one location list that text section has changed. */
24817 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24819 var_loc_list *list = *slot;
24820 if (list->first)
24821 list->last_before_switch
24822 = list->last->next ? list->last->next : list->last;
24823 return 1;
24826 /* Note in all location lists that text section has changed. */
24828 static void
24829 var_location_switch_text_section (void)
24831 if (decl_loc_table == NULL)
24832 return;
24834 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24837 /* Create a new line number table. */
24839 static dw_line_info_table *
24840 new_line_info_table (void)
24842 dw_line_info_table *table;
24844 table = ggc_cleared_alloc<dw_line_info_table> ();
24845 table->file_num = 1;
24846 table->line_num = 1;
24847 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24849 return table;
24852 /* Lookup the "current" table into which we emit line info, so
24853 that we don't have to do it for every source line. */
24855 static void
24856 set_cur_line_info_table (section *sec)
24858 dw_line_info_table *table;
24860 if (sec == text_section)
24861 table = text_section_line_info;
24862 else if (sec == cold_text_section)
24864 table = cold_text_section_line_info;
24865 if (!table)
24867 cold_text_section_line_info = table = new_line_info_table ();
24868 table->end_label = cold_end_label;
24871 else
24873 const char *end_label;
24875 if (flag_reorder_blocks_and_partition)
24877 if (in_cold_section_p)
24878 end_label = crtl->subsections.cold_section_end_label;
24879 else
24880 end_label = crtl->subsections.hot_section_end_label;
24882 else
24884 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24885 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24886 current_function_funcdef_no);
24887 end_label = ggc_strdup (label);
24890 table = new_line_info_table ();
24891 table->end_label = end_label;
24893 vec_safe_push (separate_line_info, table);
24896 if (DWARF2_ASM_LINE_DEBUG_INFO)
24897 table->is_stmt = (cur_line_info_table
24898 ? cur_line_info_table->is_stmt
24899 : DWARF_LINE_DEFAULT_IS_STMT_START);
24900 cur_line_info_table = table;
24904 /* We need to reset the locations at the beginning of each
24905 function. We can't do this in the end_function hook, because the
24906 declarations that use the locations won't have been output when
24907 that hook is called. Also compute have_multiple_function_sections here. */
24909 static void
24910 dwarf2out_begin_function (tree fun)
24912 section *sec = function_section (fun);
24914 if (sec != text_section)
24915 have_multiple_function_sections = true;
24917 if (flag_reorder_blocks_and_partition && !cold_text_section)
24919 gcc_assert (current_function_decl == fun);
24920 cold_text_section = unlikely_text_section ();
24921 switch_to_section (cold_text_section);
24922 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24923 switch_to_section (sec);
24926 dwarf2out_note_section_used ();
24927 call_site_count = 0;
24928 tail_call_site_count = 0;
24930 set_cur_line_info_table (sec);
24933 /* Helper function of dwarf2out_end_function, called only after emitting
24934 the very first function into assembly. Check if some .debug_loc range
24935 might end with a .LVL* label that could be equal to .Ltext0.
24936 In that case we must force using absolute addresses in .debug_loc ranges,
24937 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24938 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24939 list terminator.
24940 Set have_multiple_function_sections to true in that case and
24941 terminate htab traversal. */
24944 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24946 var_loc_list *entry = *slot;
24947 struct var_loc_node *node;
24949 node = entry->first;
24950 if (node && node->next && node->next->label)
24952 unsigned int i;
24953 const char *label = node->next->label;
24954 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24956 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24958 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24959 if (strcmp (label, loclabel) == 0)
24961 have_multiple_function_sections = true;
24962 return 0;
24966 return 1;
24969 /* Hook called after emitting a function into assembly.
24970 This does something only for the very first function emitted. */
24972 static void
24973 dwarf2out_end_function (unsigned int)
24975 if (in_first_function_p
24976 && !have_multiple_function_sections
24977 && first_loclabel_num_not_at_text_label
24978 && decl_loc_table)
24979 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24980 in_first_function_p = false;
24981 maybe_at_text_label_p = false;
24984 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
24985 front-ends register a translation unit even before dwarf2out_init is
24986 called. */
24987 static tree main_translation_unit = NULL_TREE;
24989 /* Hook called by front-ends after they built their main translation unit.
24990 Associate comp_unit_die to UNIT. */
24992 static void
24993 dwarf2out_register_main_translation_unit (tree unit)
24995 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24996 && main_translation_unit == NULL_TREE);
24997 main_translation_unit = unit;
24998 /* If dwarf2out_init has not been called yet, it will perform the association
24999 itself looking at main_translation_unit. */
25000 if (decl_die_table != NULL)
25001 equate_decl_number_to_die (unit, comp_unit_die ());
25004 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
25006 static void
25007 push_dw_line_info_entry (dw_line_info_table *table,
25008 enum dw_line_info_opcode opcode, unsigned int val)
25010 dw_line_info_entry e;
25011 e.opcode = opcode;
25012 e.val = val;
25013 vec_safe_push (table->entries, e);
25016 /* Output a label to mark the beginning of a source code line entry
25017 and record information relating to this source line, in
25018 'line_info_table' for later output of the .debug_line section. */
25019 /* ??? The discriminator parameter ought to be unsigned. */
25021 static void
25022 dwarf2out_source_line (unsigned int line, const char *filename,
25023 int discriminator, bool is_stmt)
25025 unsigned int file_num;
25026 dw_line_info_table *table;
25028 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
25029 return;
25031 /* The discriminator column was added in dwarf4. Simplify the below
25032 by simply removing it if we're not supposed to output it. */
25033 if (dwarf_version < 4 && dwarf_strict)
25034 discriminator = 0;
25036 table = cur_line_info_table;
25037 file_num = maybe_emit_file (lookup_filename (filename));
25039 /* ??? TODO: Elide duplicate line number entries. Traditionally,
25040 the debugger has used the second (possibly duplicate) line number
25041 at the beginning of the function to mark the end of the prologue.
25042 We could eliminate any other duplicates within the function. For
25043 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
25044 that second line number entry. */
25045 /* Recall that this end-of-prologue indication is *not* the same thing
25046 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
25047 to which the hook corresponds, follows the last insn that was
25048 emitted by gen_prologue. What we need is to precede the first insn
25049 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
25050 insn that corresponds to something the user wrote. These may be
25051 very different locations once scheduling is enabled. */
25053 if (0 && file_num == table->file_num
25054 && line == table->line_num
25055 && discriminator == table->discrim_num
25056 && is_stmt == table->is_stmt)
25057 return;
25059 switch_to_section (current_function_section ());
25061 /* If requested, emit something human-readable. */
25062 if (flag_debug_asm)
25063 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
25065 if (DWARF2_ASM_LINE_DEBUG_INFO)
25067 /* Emit the .loc directive understood by GNU as. */
25068 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
25069 file_num, line, is_stmt, discriminator */
25070 fputs ("\t.loc ", asm_out_file);
25071 fprint_ul (asm_out_file, file_num);
25072 putc (' ', asm_out_file);
25073 fprint_ul (asm_out_file, line);
25074 putc (' ', asm_out_file);
25075 putc ('0', asm_out_file);
25077 if (is_stmt != table->is_stmt)
25079 fputs (" is_stmt ", asm_out_file);
25080 putc (is_stmt ? '1' : '0', asm_out_file);
25082 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
25084 gcc_assert (discriminator > 0);
25085 fputs (" discriminator ", asm_out_file);
25086 fprint_ul (asm_out_file, (unsigned long) discriminator);
25088 putc ('\n', asm_out_file);
25090 else
25092 unsigned int label_num = ++line_info_label_num;
25094 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
25096 push_dw_line_info_entry (table, LI_set_address, label_num);
25097 if (file_num != table->file_num)
25098 push_dw_line_info_entry (table, LI_set_file, file_num);
25099 if (discriminator != table->discrim_num)
25100 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
25101 if (is_stmt != table->is_stmt)
25102 push_dw_line_info_entry (table, LI_negate_stmt, 0);
25103 push_dw_line_info_entry (table, LI_set_line, line);
25106 table->file_num = file_num;
25107 table->line_num = line;
25108 table->discrim_num = discriminator;
25109 table->is_stmt = is_stmt;
25110 table->in_use = true;
25113 /* Record the beginning of a new source file. */
25115 static void
25116 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
25118 if (flag_eliminate_dwarf2_dups)
25120 /* Record the beginning of the file for break_out_includes. */
25121 dw_die_ref bincl_die;
25123 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
25124 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
25127 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25129 macinfo_entry e;
25130 e.code = DW_MACINFO_start_file;
25131 e.lineno = lineno;
25132 e.info = ggc_strdup (filename);
25133 vec_safe_push (macinfo_table, e);
25137 /* Record the end of a source file. */
25139 static void
25140 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
25142 if (flag_eliminate_dwarf2_dups)
25143 /* Record the end of the file for break_out_includes. */
25144 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
25146 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25148 macinfo_entry e;
25149 e.code = DW_MACINFO_end_file;
25150 e.lineno = lineno;
25151 e.info = NULL;
25152 vec_safe_push (macinfo_table, e);
25156 /* Called from debug_define in toplev.c. The `buffer' parameter contains
25157 the tail part of the directive line, i.e. the part which is past the
25158 initial whitespace, #, whitespace, directive-name, whitespace part. */
25160 static void
25161 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
25162 const char *buffer ATTRIBUTE_UNUSED)
25164 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25166 macinfo_entry e;
25167 /* Insert a dummy first entry to be able to optimize the whole
25168 predefined macro block using DW_MACRO_GNU_transparent_include. */
25169 if (macinfo_table->is_empty () && lineno <= 1)
25171 e.code = 0;
25172 e.lineno = 0;
25173 e.info = NULL;
25174 vec_safe_push (macinfo_table, e);
25176 e.code = DW_MACINFO_define;
25177 e.lineno = lineno;
25178 e.info = ggc_strdup (buffer);
25179 vec_safe_push (macinfo_table, e);
25183 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
25184 the tail part of the directive line, i.e. the part which is past the
25185 initial whitespace, #, whitespace, directive-name, whitespace part. */
25187 static void
25188 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
25189 const char *buffer ATTRIBUTE_UNUSED)
25191 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25193 macinfo_entry e;
25194 /* Insert a dummy first entry to be able to optimize the whole
25195 predefined macro block using DW_MACRO_GNU_transparent_include. */
25196 if (macinfo_table->is_empty () && lineno <= 1)
25198 e.code = 0;
25199 e.lineno = 0;
25200 e.info = NULL;
25201 vec_safe_push (macinfo_table, e);
25203 e.code = DW_MACINFO_undef;
25204 e.lineno = lineno;
25205 e.info = ggc_strdup (buffer);
25206 vec_safe_push (macinfo_table, e);
25210 /* Helpers to manipulate hash table of CUs. */
25212 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
25214 static inline hashval_t hash (const macinfo_entry *);
25215 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
25218 inline hashval_t
25219 macinfo_entry_hasher::hash (const macinfo_entry *entry)
25221 return htab_hash_string (entry->info);
25224 inline bool
25225 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
25226 const macinfo_entry *entry2)
25228 return !strcmp (entry1->info, entry2->info);
25231 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
25233 /* Output a single .debug_macinfo entry. */
25235 static void
25236 output_macinfo_op (macinfo_entry *ref)
25238 int file_num;
25239 size_t len;
25240 struct indirect_string_node *node;
25241 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25242 struct dwarf_file_data *fd;
25244 switch (ref->code)
25246 case DW_MACINFO_start_file:
25247 fd = lookup_filename (ref->info);
25248 file_num = maybe_emit_file (fd);
25249 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
25250 dw2_asm_output_data_uleb128 (ref->lineno,
25251 "Included from line number %lu",
25252 (unsigned long) ref->lineno);
25253 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
25254 break;
25255 case DW_MACINFO_end_file:
25256 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
25257 break;
25258 case DW_MACINFO_define:
25259 case DW_MACINFO_undef:
25260 len = strlen (ref->info) + 1;
25261 if (!dwarf_strict
25262 && len > DWARF_OFFSET_SIZE
25263 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25264 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25266 ref->code = ref->code == DW_MACINFO_define
25267 ? DW_MACRO_GNU_define_indirect
25268 : DW_MACRO_GNU_undef_indirect;
25269 output_macinfo_op (ref);
25270 return;
25272 dw2_asm_output_data (1, ref->code,
25273 ref->code == DW_MACINFO_define
25274 ? "Define macro" : "Undefine macro");
25275 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25276 (unsigned long) ref->lineno);
25277 dw2_asm_output_nstring (ref->info, -1, "The macro");
25278 break;
25279 case DW_MACRO_GNU_define_indirect:
25280 case DW_MACRO_GNU_undef_indirect:
25281 node = find_AT_string (ref->info);
25282 gcc_assert (node
25283 && ((node->form == DW_FORM_strp)
25284 || (node->form == DW_FORM_GNU_str_index)));
25285 dw2_asm_output_data (1, ref->code,
25286 ref->code == DW_MACRO_GNU_define_indirect
25287 ? "Define macro indirect"
25288 : "Undefine macro indirect");
25289 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25290 (unsigned long) ref->lineno);
25291 if (node->form == DW_FORM_strp)
25292 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
25293 debug_str_section, "The macro: \"%s\"",
25294 ref->info);
25295 else
25296 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
25297 ref->info);
25298 break;
25299 case DW_MACRO_GNU_transparent_include:
25300 dw2_asm_output_data (1, ref->code, "Transparent include");
25301 ASM_GENERATE_INTERNAL_LABEL (label,
25302 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
25303 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
25304 break;
25305 default:
25306 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
25307 ASM_COMMENT_START, (unsigned long) ref->code);
25308 break;
25312 /* Attempt to make a sequence of define/undef macinfo ops shareable with
25313 other compilation unit .debug_macinfo sections. IDX is the first
25314 index of a define/undef, return the number of ops that should be
25315 emitted in a comdat .debug_macinfo section and emit
25316 a DW_MACRO_GNU_transparent_include entry referencing it.
25317 If the define/undef entry should be emitted normally, return 0. */
25319 static unsigned
25320 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25321 macinfo_hash_type **macinfo_htab)
25323 macinfo_entry *first, *second, *cur, *inc;
25324 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25325 unsigned char checksum[16];
25326 struct md5_ctx ctx;
25327 char *grp_name, *tail;
25328 const char *base;
25329 unsigned int i, count, encoded_filename_len, linebuf_len;
25330 macinfo_entry **slot;
25332 first = &(*macinfo_table)[idx];
25333 second = &(*macinfo_table)[idx + 1];
25335 /* Optimize only if there are at least two consecutive define/undef ops,
25336 and either all of them are before first DW_MACINFO_start_file
25337 with lineno {0,1} (i.e. predefined macro block), or all of them are
25338 in some included header file. */
25339 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25340 return 0;
25341 if (vec_safe_is_empty (files))
25343 if (first->lineno > 1 || second->lineno > 1)
25344 return 0;
25346 else if (first->lineno == 0)
25347 return 0;
25349 /* Find the last define/undef entry that can be grouped together
25350 with first and at the same time compute md5 checksum of their
25351 codes, linenumbers and strings. */
25352 md5_init_ctx (&ctx);
25353 for (i = idx; macinfo_table->iterate (i, &cur); i++)
25354 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25355 break;
25356 else if (vec_safe_is_empty (files) && cur->lineno > 1)
25357 break;
25358 else
25360 unsigned char code = cur->code;
25361 md5_process_bytes (&code, 1, &ctx);
25362 checksum_uleb128 (cur->lineno, &ctx);
25363 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25365 md5_finish_ctx (&ctx, checksum);
25366 count = i - idx;
25368 /* From the containing include filename (if any) pick up just
25369 usable characters from its basename. */
25370 if (vec_safe_is_empty (files))
25371 base = "";
25372 else
25373 base = lbasename (files->last ().info);
25374 for (encoded_filename_len = 0, i = 0; base[i]; i++)
25375 if (ISIDNUM (base[i]) || base[i] == '.')
25376 encoded_filename_len++;
25377 /* Count . at the end. */
25378 if (encoded_filename_len)
25379 encoded_filename_len++;
25381 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25382 linebuf_len = strlen (linebuf);
25384 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
25385 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25386 + 16 * 2 + 1);
25387 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25388 tail = grp_name + 4;
25389 if (encoded_filename_len)
25391 for (i = 0; base[i]; i++)
25392 if (ISIDNUM (base[i]) || base[i] == '.')
25393 *tail++ = base[i];
25394 *tail++ = '.';
25396 memcpy (tail, linebuf, linebuf_len);
25397 tail += linebuf_len;
25398 *tail++ = '.';
25399 for (i = 0; i < 16; i++)
25400 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25402 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25403 in the empty vector entry before the first define/undef. */
25404 inc = &(*macinfo_table)[idx - 1];
25405 inc->code = DW_MACRO_GNU_transparent_include;
25406 inc->lineno = 0;
25407 inc->info = ggc_strdup (grp_name);
25408 if (!*macinfo_htab)
25409 *macinfo_htab = new macinfo_hash_type (10);
25410 /* Avoid emitting duplicates. */
25411 slot = (*macinfo_htab)->find_slot (inc, INSERT);
25412 if (*slot != NULL)
25414 inc->code = 0;
25415 inc->info = NULL;
25416 /* If such an entry has been used before, just emit
25417 a DW_MACRO_GNU_transparent_include op. */
25418 inc = *slot;
25419 output_macinfo_op (inc);
25420 /* And clear all macinfo_entry in the range to avoid emitting them
25421 in the second pass. */
25422 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25424 cur->code = 0;
25425 cur->info = NULL;
25428 else
25430 *slot = inc;
25431 inc->lineno = (*macinfo_htab)->elements ();
25432 output_macinfo_op (inc);
25434 return count;
25437 /* Save any strings needed by the macinfo table in the debug str
25438 table. All strings must be collected into the table by the time
25439 index_string is called. */
25441 static void
25442 save_macinfo_strings (void)
25444 unsigned len;
25445 unsigned i;
25446 macinfo_entry *ref;
25448 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25450 switch (ref->code)
25452 /* Match the logic in output_macinfo_op to decide on
25453 indirect strings. */
25454 case DW_MACINFO_define:
25455 case DW_MACINFO_undef:
25456 len = strlen (ref->info) + 1;
25457 if (!dwarf_strict
25458 && len > DWARF_OFFSET_SIZE
25459 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25460 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25461 set_indirect_string (find_AT_string (ref->info));
25462 break;
25463 case DW_MACRO_GNU_define_indirect:
25464 case DW_MACRO_GNU_undef_indirect:
25465 set_indirect_string (find_AT_string (ref->info));
25466 break;
25467 default:
25468 break;
25473 /* Output macinfo section(s). */
25475 static void
25476 output_macinfo (void)
25478 unsigned i;
25479 unsigned long length = vec_safe_length (macinfo_table);
25480 macinfo_entry *ref;
25481 vec<macinfo_entry, va_gc> *files = NULL;
25482 macinfo_hash_type *macinfo_htab = NULL;
25484 if (! length)
25485 return;
25487 /* output_macinfo* uses these interchangeably. */
25488 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25489 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25490 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25491 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25493 /* For .debug_macro emit the section header. */
25494 if (!dwarf_strict)
25496 dw2_asm_output_data (2, 4, "DWARF macro version number");
25497 if (DWARF_OFFSET_SIZE == 8)
25498 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25499 else
25500 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25501 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25502 (!dwarf_split_debug_info ? debug_line_section_label
25503 : debug_skeleton_line_section_label),
25504 debug_line_section, NULL);
25507 /* In the first loop, it emits the primary .debug_macinfo section
25508 and after each emitted op the macinfo_entry is cleared.
25509 If a longer range of define/undef ops can be optimized using
25510 DW_MACRO_GNU_transparent_include, the
25511 DW_MACRO_GNU_transparent_include op is emitted and kept in
25512 the vector before the first define/undef in the range and the
25513 whole range of define/undef ops is not emitted and kept. */
25514 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25516 switch (ref->code)
25518 case DW_MACINFO_start_file:
25519 vec_safe_push (files, *ref);
25520 break;
25521 case DW_MACINFO_end_file:
25522 if (!vec_safe_is_empty (files))
25523 files->pop ();
25524 break;
25525 case DW_MACINFO_define:
25526 case DW_MACINFO_undef:
25527 if (!dwarf_strict
25528 && HAVE_COMDAT_GROUP
25529 && vec_safe_length (files) != 1
25530 && i > 0
25531 && i + 1 < length
25532 && (*macinfo_table)[i - 1].code == 0)
25534 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25535 if (count)
25537 i += count - 1;
25538 continue;
25541 break;
25542 case 0:
25543 /* A dummy entry may be inserted at the beginning to be able
25544 to optimize the whole block of predefined macros. */
25545 if (i == 0)
25546 continue;
25547 default:
25548 break;
25550 output_macinfo_op (ref);
25551 ref->info = NULL;
25552 ref->code = 0;
25555 if (!macinfo_htab)
25556 return;
25558 delete macinfo_htab;
25559 macinfo_htab = NULL;
25561 /* If any DW_MACRO_GNU_transparent_include were used, on those
25562 DW_MACRO_GNU_transparent_include entries terminate the
25563 current chain and switch to a new comdat .debug_macinfo
25564 section and emit the define/undef entries within it. */
25565 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25566 switch (ref->code)
25568 case 0:
25569 continue;
25570 case DW_MACRO_GNU_transparent_include:
25572 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25573 tree comdat_key = get_identifier (ref->info);
25574 /* Terminate the previous .debug_macinfo section. */
25575 dw2_asm_output_data (1, 0, "End compilation unit");
25576 targetm.asm_out.named_section (debug_macinfo_section_name,
25577 SECTION_DEBUG
25578 | SECTION_LINKONCE,
25579 comdat_key);
25580 ASM_GENERATE_INTERNAL_LABEL (label,
25581 DEBUG_MACRO_SECTION_LABEL,
25582 ref->lineno);
25583 ASM_OUTPUT_LABEL (asm_out_file, label);
25584 ref->code = 0;
25585 ref->info = NULL;
25586 dw2_asm_output_data (2, 4, "DWARF macro version number");
25587 if (DWARF_OFFSET_SIZE == 8)
25588 dw2_asm_output_data (1, 1, "Flags: 64-bit");
25589 else
25590 dw2_asm_output_data (1, 0, "Flags: 32-bit");
25592 break;
25593 case DW_MACINFO_define:
25594 case DW_MACINFO_undef:
25595 output_macinfo_op (ref);
25596 ref->code = 0;
25597 ref->info = NULL;
25598 break;
25599 default:
25600 gcc_unreachable ();
25604 /* Initialize the various sections and labels for dwarf output. */
25606 static void
25607 init_sections_and_labels (void)
25609 if (!dwarf_split_debug_info)
25611 debug_info_section = get_section (DEBUG_INFO_SECTION,
25612 SECTION_DEBUG, NULL);
25613 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25614 SECTION_DEBUG, NULL);
25615 debug_loc_section = get_section (DEBUG_LOC_SECTION,
25616 SECTION_DEBUG, NULL);
25617 debug_macinfo_section_name
25618 = dwarf_strict ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
25619 debug_macinfo_section = get_section (debug_macinfo_section_name,
25620 SECTION_DEBUG, NULL);
25622 else
25624 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25625 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25626 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25627 SECTION_DEBUG | SECTION_EXCLUDE,
25628 NULL);
25629 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25630 SECTION_DEBUG, NULL);
25631 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25632 SECTION_DEBUG, NULL);
25633 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25634 SECTION_DEBUG, NULL);
25635 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25636 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25638 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25639 the main .o, but the skeleton_line goes into the split off dwo. */
25640 debug_skeleton_line_section
25641 = get_section (DEBUG_DWO_LINE_SECTION,
25642 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25643 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25644 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25645 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
25646 SECTION_DEBUG | SECTION_EXCLUDE,
25647 NULL);
25648 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25649 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25650 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25651 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25652 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25653 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25654 debug_macinfo_section_name
25655 = dwarf_strict ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
25656 debug_macinfo_section = get_section (debug_macinfo_section_name,
25657 SECTION_DEBUG | SECTION_EXCLUDE,
25658 NULL);
25660 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25661 SECTION_DEBUG, NULL);
25662 debug_line_section = get_section (DEBUG_LINE_SECTION,
25663 SECTION_DEBUG, NULL);
25664 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25665 SECTION_DEBUG, NULL);
25666 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25667 SECTION_DEBUG, NULL);
25668 debug_str_section = get_section (DEBUG_STR_SECTION,
25669 DEBUG_STR_SECTION_FLAGS, NULL);
25670 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25671 SECTION_DEBUG, NULL);
25672 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25673 SECTION_DEBUG, NULL);
25675 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25676 DEBUG_ABBREV_SECTION_LABEL, 0);
25677 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25678 DEBUG_INFO_SECTION_LABEL, 0);
25679 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25680 DEBUG_LINE_SECTION_LABEL, 0);
25681 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25682 DEBUG_RANGES_SECTION_LABEL, 0);
25683 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25684 DEBUG_ADDR_SECTION_LABEL, 0);
25685 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25686 dwarf_strict
25687 ? DEBUG_MACINFO_SECTION_LABEL
25688 : DEBUG_MACRO_SECTION_LABEL, 0);
25689 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25692 /* Set up for Dwarf output at the start of compilation. */
25694 static void
25695 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25697 /* This option is currently broken, see (PR53118 and PR46102). */
25698 if (flag_eliminate_dwarf2_dups
25699 && strstr (lang_hooks.name, "C++"))
25701 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25702 flag_eliminate_dwarf2_dups = 0;
25705 /* Allocate the file_table. */
25706 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25708 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25709 /* Allocate the decl_die_table. */
25710 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25712 /* Allocate the decl_loc_table. */
25713 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25715 /* Allocate the cached_dw_loc_list_table. */
25716 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25718 /* Allocate the initial hunk of the decl_scope_table. */
25719 vec_alloc (decl_scope_table, 256);
25721 /* Allocate the initial hunk of the abbrev_die_table. */
25722 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25723 (ABBREV_DIE_TABLE_INCREMENT);
25724 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25725 /* Zero-th entry is allocated, but unused. */
25726 abbrev_die_table_in_use = 1;
25728 /* Allocate the dwarf_proc_stack_usage_map. */
25729 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25731 /* Allocate the pubtypes and pubnames vectors. */
25732 vec_alloc (pubname_table, 32);
25733 vec_alloc (pubtype_table, 32);
25735 vec_alloc (incomplete_types, 64);
25737 vec_alloc (used_rtx_array, 32);
25739 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25740 vec_alloc (macinfo_table, 64);
25741 #endif
25743 /* If front-ends already registered a main translation unit but we were not
25744 ready to perform the association, do this now. */
25745 if (main_translation_unit != NULL_TREE)
25746 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25749 /* Called before compile () starts outputtting functions, variables
25750 and toplevel asms into assembly. */
25752 static void
25753 dwarf2out_assembly_start (void)
25755 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25756 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25757 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25758 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25759 COLD_TEXT_SECTION_LABEL, 0);
25760 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25762 switch_to_section (text_section);
25763 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25764 #endif
25766 /* Make sure the line number table for .text always exists. */
25767 text_section_line_info = new_line_info_table ();
25768 text_section_line_info->end_label = text_end_label;
25770 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25771 cur_line_info_table = text_section_line_info;
25772 #endif
25774 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25775 && dwarf2out_do_cfi_asm ()
25776 && (!(flag_unwind_tables || flag_exceptions)
25777 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25778 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25781 /* A helper function for dwarf2out_finish called through
25782 htab_traverse. Assign a string its index. All strings must be
25783 collected into the table by the time index_string is called,
25784 because the indexing code relies on htab_traverse to traverse nodes
25785 in the same order for each run. */
25788 index_string (indirect_string_node **h, unsigned int *index)
25790 indirect_string_node *node = *h;
25792 find_string_form (node);
25793 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25795 gcc_assert (node->index == NO_INDEX_ASSIGNED);
25796 node->index = *index;
25797 *index += 1;
25799 return 1;
25802 /* A helper function for output_indirect_strings called through
25803 htab_traverse. Output the offset to a string and update the
25804 current offset. */
25807 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25809 indirect_string_node *node = *h;
25811 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25813 /* Assert that this node has been assigned an index. */
25814 gcc_assert (node->index != NO_INDEX_ASSIGNED
25815 && node->index != NOT_INDEXED);
25816 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25817 "indexed string 0x%x: %s", node->index, node->str);
25818 *offset += strlen (node->str) + 1;
25820 return 1;
25823 /* A helper function for dwarf2out_finish called through
25824 htab_traverse. Output the indexed string. */
25827 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25829 struct indirect_string_node *node = *h;
25831 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25833 /* Assert that the strings are output in the same order as their
25834 indexes were assigned. */
25835 gcc_assert (*cur_idx == node->index);
25836 assemble_string (node->str, strlen (node->str) + 1);
25837 *cur_idx += 1;
25839 return 1;
25842 /* A helper function for dwarf2out_finish called through
25843 htab_traverse. Emit one queued .debug_str string. */
25846 output_indirect_string (indirect_string_node **h, void *)
25848 struct indirect_string_node *node = *h;
25850 node->form = find_string_form (node);
25851 if (node->form == DW_FORM_strp && node->refcount > 0)
25853 ASM_OUTPUT_LABEL (asm_out_file, node->label);
25854 assemble_string (node->str, strlen (node->str) + 1);
25857 return 1;
25860 /* Output the indexed string table. */
25862 static void
25863 output_indirect_strings (void)
25865 switch_to_section (debug_str_section);
25866 if (!dwarf_split_debug_info)
25867 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25868 else
25870 unsigned int offset = 0;
25871 unsigned int cur_idx = 0;
25873 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25875 switch_to_section (debug_str_offsets_section);
25876 debug_str_hash->traverse_noresize
25877 <unsigned int *, output_index_string_offset> (&offset);
25878 switch_to_section (debug_str_dwo_section);
25879 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25880 (&cur_idx);
25884 /* Callback for htab_traverse to assign an index to an entry in the
25885 table, and to write that entry to the .debug_addr section. */
25888 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25890 addr_table_entry *entry = *slot;
25892 if (entry->refcount == 0)
25894 gcc_assert (entry->index == NO_INDEX_ASSIGNED
25895 || entry->index == NOT_INDEXED);
25896 return 1;
25899 gcc_assert (entry->index == *cur_index);
25900 (*cur_index)++;
25902 switch (entry->kind)
25904 case ate_kind_rtx:
25905 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25906 "0x%x", entry->index);
25907 break;
25908 case ate_kind_rtx_dtprel:
25909 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25910 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25911 DWARF2_ADDR_SIZE,
25912 entry->addr.rtl);
25913 fputc ('\n', asm_out_file);
25914 break;
25915 case ate_kind_label:
25916 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25917 "0x%x", entry->index);
25918 break;
25919 default:
25920 gcc_unreachable ();
25922 return 1;
25925 /* Produce the .debug_addr section. */
25927 static void
25928 output_addr_table (void)
25930 unsigned int index = 0;
25931 if (addr_index_table == NULL || addr_index_table->size () == 0)
25932 return;
25934 switch_to_section (debug_addr_section);
25935 addr_index_table
25936 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25939 #if ENABLE_ASSERT_CHECKING
25940 /* Verify that all marks are clear. */
25942 static void
25943 verify_marks_clear (dw_die_ref die)
25945 dw_die_ref c;
25947 gcc_assert (! die->die_mark);
25948 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25950 #endif /* ENABLE_ASSERT_CHECKING */
25952 /* Clear the marks for a die and its children.
25953 Be cool if the mark isn't set. */
25955 static void
25956 prune_unmark_dies (dw_die_ref die)
25958 dw_die_ref c;
25960 if (die->die_mark)
25961 die->die_mark = 0;
25962 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25965 /* Given LOC that is referenced by a DIE we're marking as used, find all
25966 referenced DWARF procedures it references and mark them as used. */
25968 static void
25969 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25971 for (; loc != NULL; loc = loc->dw_loc_next)
25972 switch (loc->dw_loc_opc)
25974 case DW_OP_GNU_implicit_pointer:
25975 case DW_OP_GNU_convert:
25976 case DW_OP_GNU_reinterpret:
25977 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25978 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25979 break;
25980 case DW_OP_call2:
25981 case DW_OP_call4:
25982 case DW_OP_call_ref:
25983 case DW_OP_GNU_const_type:
25984 case DW_OP_GNU_parameter_ref:
25985 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
25986 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25987 break;
25988 case DW_OP_GNU_regval_type:
25989 case DW_OP_GNU_deref_type:
25990 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
25991 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
25992 break;
25993 case DW_OP_GNU_entry_value:
25994 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
25995 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
25996 break;
25997 default:
25998 break;
26002 /* Given DIE that we're marking as used, find any other dies
26003 it references as attributes and mark them as used. */
26005 static void
26006 prune_unused_types_walk_attribs (dw_die_ref die)
26008 dw_attr_node *a;
26009 unsigned ix;
26011 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26013 switch (AT_class (a))
26015 /* Make sure DWARF procedures referenced by location descriptions will
26016 get emitted. */
26017 case dw_val_class_loc:
26018 prune_unused_types_walk_loc_descr (AT_loc (a));
26019 break;
26020 case dw_val_class_loc_list:
26021 for (dw_loc_list_ref list = AT_loc_list (a);
26022 list != NULL;
26023 list = list->dw_loc_next)
26024 prune_unused_types_walk_loc_descr (list->expr);
26025 break;
26027 case dw_val_class_die_ref:
26028 /* A reference to another DIE.
26029 Make sure that it will get emitted.
26030 If it was broken out into a comdat group, don't follow it. */
26031 if (! AT_ref (a)->comdat_type_p
26032 || a->dw_attr == DW_AT_specification)
26033 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
26034 break;
26036 case dw_val_class_str:
26037 /* Set the string's refcount to 0 so that prune_unused_types_mark
26038 accounts properly for it. */
26039 a->dw_attr_val.v.val_str->refcount = 0;
26040 break;
26042 default:
26043 break;
26048 /* Mark the generic parameters and arguments children DIEs of DIE. */
26050 static void
26051 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
26053 dw_die_ref c;
26055 if (die == NULL || die->die_child == NULL)
26056 return;
26057 c = die->die_child;
26060 if (is_template_parameter (c))
26061 prune_unused_types_mark (c, 1);
26062 c = c->die_sib;
26063 } while (c && c != die->die_child);
26066 /* Mark DIE as being used. If DOKIDS is true, then walk down
26067 to DIE's children. */
26069 static void
26070 prune_unused_types_mark (dw_die_ref die, int dokids)
26072 dw_die_ref c;
26074 if (die->die_mark == 0)
26076 /* We haven't done this node yet. Mark it as used. */
26077 die->die_mark = 1;
26078 /* If this is the DIE of a generic type instantiation,
26079 mark the children DIEs that describe its generic parms and
26080 args. */
26081 prune_unused_types_mark_generic_parms_dies (die);
26083 /* We also have to mark its parents as used.
26084 (But we don't want to mark our parent's kids due to this,
26085 unless it is a class.) */
26086 if (die->die_parent)
26087 prune_unused_types_mark (die->die_parent,
26088 class_scope_p (die->die_parent));
26090 /* Mark any referenced nodes. */
26091 prune_unused_types_walk_attribs (die);
26093 /* If this node is a specification,
26094 also mark the definition, if it exists. */
26095 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
26096 prune_unused_types_mark (die->die_definition, 1);
26099 if (dokids && die->die_mark != 2)
26101 /* We need to walk the children, but haven't done so yet.
26102 Remember that we've walked the kids. */
26103 die->die_mark = 2;
26105 /* If this is an array type, we need to make sure our
26106 kids get marked, even if they're types. If we're
26107 breaking out types into comdat sections, do this
26108 for all type definitions. */
26109 if (die->die_tag == DW_TAG_array_type
26110 || (use_debug_types
26111 && is_type_die (die) && ! is_declaration_die (die)))
26112 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
26113 else
26114 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26118 /* For local classes, look if any static member functions were emitted
26119 and if so, mark them. */
26121 static void
26122 prune_unused_types_walk_local_classes (dw_die_ref die)
26124 dw_die_ref c;
26126 if (die->die_mark == 2)
26127 return;
26129 switch (die->die_tag)
26131 case DW_TAG_structure_type:
26132 case DW_TAG_union_type:
26133 case DW_TAG_class_type:
26134 break;
26136 case DW_TAG_subprogram:
26137 if (!get_AT_flag (die, DW_AT_declaration)
26138 || die->die_definition != NULL)
26139 prune_unused_types_mark (die, 1);
26140 return;
26142 default:
26143 return;
26146 /* Mark children. */
26147 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
26150 /* Walk the tree DIE and mark types that we actually use. */
26152 static void
26153 prune_unused_types_walk (dw_die_ref die)
26155 dw_die_ref c;
26157 /* Don't do anything if this node is already marked and
26158 children have been marked as well. */
26159 if (die->die_mark == 2)
26160 return;
26162 switch (die->die_tag)
26164 case DW_TAG_structure_type:
26165 case DW_TAG_union_type:
26166 case DW_TAG_class_type:
26167 if (die->die_perennial_p)
26168 break;
26170 for (c = die->die_parent; c; c = c->die_parent)
26171 if (c->die_tag == DW_TAG_subprogram)
26172 break;
26174 /* Finding used static member functions inside of classes
26175 is needed just for local classes, because for other classes
26176 static member function DIEs with DW_AT_specification
26177 are emitted outside of the DW_TAG_*_type. If we ever change
26178 it, we'd need to call this even for non-local classes. */
26179 if (c)
26180 prune_unused_types_walk_local_classes (die);
26182 /* It's a type node --- don't mark it. */
26183 return;
26185 case DW_TAG_const_type:
26186 case DW_TAG_packed_type:
26187 case DW_TAG_pointer_type:
26188 case DW_TAG_reference_type:
26189 case DW_TAG_rvalue_reference_type:
26190 case DW_TAG_volatile_type:
26191 case DW_TAG_typedef:
26192 case DW_TAG_array_type:
26193 case DW_TAG_interface_type:
26194 case DW_TAG_friend:
26195 case DW_TAG_enumeration_type:
26196 case DW_TAG_subroutine_type:
26197 case DW_TAG_string_type:
26198 case DW_TAG_set_type:
26199 case DW_TAG_subrange_type:
26200 case DW_TAG_ptr_to_member_type:
26201 case DW_TAG_file_type:
26202 /* Type nodes are useful only when other DIEs reference them --- don't
26203 mark them. */
26204 /* FALLTHROUGH */
26206 case DW_TAG_dwarf_procedure:
26207 /* Likewise for DWARF procedures. */
26209 if (die->die_perennial_p)
26210 break;
26212 return;
26214 default:
26215 /* Mark everything else. */
26216 break;
26219 if (die->die_mark == 0)
26221 die->die_mark = 1;
26223 /* Now, mark any dies referenced from here. */
26224 prune_unused_types_walk_attribs (die);
26227 die->die_mark = 2;
26229 /* Mark children. */
26230 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26233 /* Increment the string counts on strings referred to from DIE's
26234 attributes. */
26236 static void
26237 prune_unused_types_update_strings (dw_die_ref die)
26239 dw_attr_node *a;
26240 unsigned ix;
26242 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26243 if (AT_class (a) == dw_val_class_str)
26245 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
26246 s->refcount++;
26247 /* Avoid unnecessarily putting strings that are used less than
26248 twice in the hash table. */
26249 if (s->refcount
26250 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
26252 indirect_string_node **slot
26253 = debug_str_hash->find_slot_with_hash (s->str,
26254 htab_hash_string (s->str),
26255 INSERT);
26256 gcc_assert (*slot == NULL);
26257 *slot = s;
26262 /* Mark DIE and its children as removed. */
26264 static void
26265 mark_removed (dw_die_ref die)
26267 dw_die_ref c;
26268 die->removed = true;
26269 FOR_EACH_CHILD (die, c, mark_removed (c));
26272 /* Remove from the tree DIE any dies that aren't marked. */
26274 static void
26275 prune_unused_types_prune (dw_die_ref die)
26277 dw_die_ref c;
26279 gcc_assert (die->die_mark);
26280 prune_unused_types_update_strings (die);
26282 if (! die->die_child)
26283 return;
26285 c = die->die_child;
26286 do {
26287 dw_die_ref prev = c, next;
26288 for (c = c->die_sib; ! c->die_mark; c = next)
26289 if (c == die->die_child)
26291 /* No marked children between 'prev' and the end of the list. */
26292 if (prev == c)
26293 /* No marked children at all. */
26294 die->die_child = NULL;
26295 else
26297 prev->die_sib = c->die_sib;
26298 die->die_child = prev;
26300 c->die_sib = NULL;
26301 mark_removed (c);
26302 return;
26304 else
26306 next = c->die_sib;
26307 c->die_sib = NULL;
26308 mark_removed (c);
26311 if (c != prev->die_sib)
26312 prev->die_sib = c;
26313 prune_unused_types_prune (c);
26314 } while (c != die->die_child);
26317 /* Remove dies representing declarations that we never use. */
26319 static void
26320 prune_unused_types (void)
26322 unsigned int i;
26323 limbo_die_node *node;
26324 comdat_type_node *ctnode;
26325 pubname_entry *pub;
26326 dw_die_ref base_type;
26328 #if ENABLE_ASSERT_CHECKING
26329 /* All the marks should already be clear. */
26330 verify_marks_clear (comp_unit_die ());
26331 for (node = limbo_die_list; node; node = node->next)
26332 verify_marks_clear (node->die);
26333 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26334 verify_marks_clear (ctnode->root_die);
26335 #endif /* ENABLE_ASSERT_CHECKING */
26337 /* Mark types that are used in global variables. */
26338 premark_types_used_by_global_vars ();
26340 /* Set the mark on nodes that are actually used. */
26341 prune_unused_types_walk (comp_unit_die ());
26342 for (node = limbo_die_list; node; node = node->next)
26343 prune_unused_types_walk (node->die);
26344 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26346 prune_unused_types_walk (ctnode->root_die);
26347 prune_unused_types_mark (ctnode->type_die, 1);
26350 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
26351 are unusual in that they are pubnames that are the children of pubtypes.
26352 They should only be marked via their parent DW_TAG_enumeration_type die,
26353 not as roots in themselves. */
26354 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26355 if (pub->die->die_tag != DW_TAG_enumerator)
26356 prune_unused_types_mark (pub->die, 1);
26357 for (i = 0; base_types.iterate (i, &base_type); i++)
26358 prune_unused_types_mark (base_type, 1);
26360 if (debug_str_hash)
26361 debug_str_hash->empty ();
26362 if (skeleton_debug_str_hash)
26363 skeleton_debug_str_hash->empty ();
26364 prune_unused_types_prune (comp_unit_die ());
26365 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26367 node = *pnode;
26368 if (!node->die->die_mark)
26369 *pnode = node->next;
26370 else
26372 prune_unused_types_prune (node->die);
26373 pnode = &node->next;
26376 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26377 prune_unused_types_prune (ctnode->root_die);
26379 /* Leave the marks clear. */
26380 prune_unmark_dies (comp_unit_die ());
26381 for (node = limbo_die_list; node; node = node->next)
26382 prune_unmark_dies (node->die);
26383 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26384 prune_unmark_dies (ctnode->root_die);
26387 /* Helpers to manipulate hash table of comdat type units. */
26389 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26391 static inline hashval_t hash (const comdat_type_node *);
26392 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26395 inline hashval_t
26396 comdat_type_hasher::hash (const comdat_type_node *type_node)
26398 hashval_t h;
26399 memcpy (&h, type_node->signature, sizeof (h));
26400 return h;
26403 inline bool
26404 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26405 const comdat_type_node *type_node_2)
26407 return (! memcmp (type_node_1->signature, type_node_2->signature,
26408 DWARF_TYPE_SIGNATURE_SIZE));
26411 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26412 to the location it would have been added, should we know its
26413 DECL_ASSEMBLER_NAME when we added other attributes. This will
26414 probably improve compactness of debug info, removing equivalent
26415 abbrevs, and hide any differences caused by deferring the
26416 computation of the assembler name, triggered by e.g. PCH. */
26418 static inline void
26419 move_linkage_attr (dw_die_ref die)
26421 unsigned ix = vec_safe_length (die->die_attr);
26422 dw_attr_node linkage = (*die->die_attr)[ix - 1];
26424 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26425 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26427 while (--ix > 0)
26429 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26431 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26432 break;
26435 if (ix != vec_safe_length (die->die_attr) - 1)
26437 die->die_attr->pop ();
26438 die->die_attr->quick_insert (ix, linkage);
26442 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26443 referenced from typed stack ops and count how often they are used. */
26445 static void
26446 mark_base_types (dw_loc_descr_ref loc)
26448 dw_die_ref base_type = NULL;
26450 for (; loc; loc = loc->dw_loc_next)
26452 switch (loc->dw_loc_opc)
26454 case DW_OP_GNU_regval_type:
26455 case DW_OP_GNU_deref_type:
26456 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26457 break;
26458 case DW_OP_GNU_convert:
26459 case DW_OP_GNU_reinterpret:
26460 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26461 continue;
26462 /* FALLTHRU */
26463 case DW_OP_GNU_const_type:
26464 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26465 break;
26466 case DW_OP_GNU_entry_value:
26467 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26468 continue;
26469 default:
26470 continue;
26472 gcc_assert (base_type->die_parent == comp_unit_die ());
26473 if (base_type->die_mark)
26474 base_type->die_mark++;
26475 else
26477 base_types.safe_push (base_type);
26478 base_type->die_mark = 1;
26483 /* Comparison function for sorting marked base types. */
26485 static int
26486 base_type_cmp (const void *x, const void *y)
26488 dw_die_ref dx = *(const dw_die_ref *) x;
26489 dw_die_ref dy = *(const dw_die_ref *) y;
26490 unsigned int byte_size1, byte_size2;
26491 unsigned int encoding1, encoding2;
26492 if (dx->die_mark > dy->die_mark)
26493 return -1;
26494 if (dx->die_mark < dy->die_mark)
26495 return 1;
26496 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26497 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26498 if (byte_size1 < byte_size2)
26499 return 1;
26500 if (byte_size1 > byte_size2)
26501 return -1;
26502 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26503 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26504 if (encoding1 < encoding2)
26505 return 1;
26506 if (encoding1 > encoding2)
26507 return -1;
26508 return 0;
26511 /* Move base types marked by mark_base_types as early as possible
26512 in the CU, sorted by decreasing usage count both to make the
26513 uleb128 references as small as possible and to make sure they
26514 will have die_offset already computed by calc_die_sizes when
26515 sizes of typed stack loc ops is computed. */
26517 static void
26518 move_marked_base_types (void)
26520 unsigned int i;
26521 dw_die_ref base_type, die, c;
26523 if (base_types.is_empty ())
26524 return;
26526 /* Sort by decreasing usage count, they will be added again in that
26527 order later on. */
26528 base_types.qsort (base_type_cmp);
26529 die = comp_unit_die ();
26530 c = die->die_child;
26533 dw_die_ref prev = c;
26534 c = c->die_sib;
26535 while (c->die_mark)
26537 remove_child_with_prev (c, prev);
26538 /* As base types got marked, there must be at least
26539 one node other than DW_TAG_base_type. */
26540 gcc_assert (die->die_child != NULL);
26541 c = prev->die_sib;
26544 while (c != die->die_child);
26545 gcc_assert (die->die_child);
26546 c = die->die_child;
26547 for (i = 0; base_types.iterate (i, &base_type); i++)
26549 base_type->die_mark = 0;
26550 base_type->die_sib = c->die_sib;
26551 c->die_sib = base_type;
26552 c = base_type;
26556 /* Helper function for resolve_addr, attempt to resolve
26557 one CONST_STRING, return true if successful. Similarly verify that
26558 SYMBOL_REFs refer to variables emitted in the current CU. */
26560 static bool
26561 resolve_one_addr (rtx *addr)
26563 rtx rtl = *addr;
26565 if (GET_CODE (rtl) == CONST_STRING)
26567 size_t len = strlen (XSTR (rtl, 0)) + 1;
26568 tree t = build_string (len, XSTR (rtl, 0));
26569 tree tlen = size_int (len - 1);
26570 TREE_TYPE (t)
26571 = build_array_type (char_type_node, build_index_type (tlen));
26572 rtl = lookup_constant_def (t);
26573 if (!rtl || !MEM_P (rtl))
26574 return false;
26575 rtl = XEXP (rtl, 0);
26576 if (GET_CODE (rtl) == SYMBOL_REF
26577 && SYMBOL_REF_DECL (rtl)
26578 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26579 return false;
26580 vec_safe_push (used_rtx_array, rtl);
26581 *addr = rtl;
26582 return true;
26585 if (GET_CODE (rtl) == SYMBOL_REF
26586 && SYMBOL_REF_DECL (rtl))
26588 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26590 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26591 return false;
26593 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26594 return false;
26597 if (GET_CODE (rtl) == CONST)
26599 subrtx_ptr_iterator::array_type array;
26600 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26601 if (!resolve_one_addr (*iter))
26602 return false;
26605 return true;
26608 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26609 if possible, and create DW_TAG_dwarf_procedure that can be referenced
26610 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
26612 static rtx
26613 string_cst_pool_decl (tree t)
26615 rtx rtl = output_constant_def (t, 1);
26616 unsigned char *array;
26617 dw_loc_descr_ref l;
26618 tree decl;
26619 size_t len;
26620 dw_die_ref ref;
26622 if (!rtl || !MEM_P (rtl))
26623 return NULL_RTX;
26624 rtl = XEXP (rtl, 0);
26625 if (GET_CODE (rtl) != SYMBOL_REF
26626 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26627 return NULL_RTX;
26629 decl = SYMBOL_REF_DECL (rtl);
26630 if (!lookup_decl_die (decl))
26632 len = TREE_STRING_LENGTH (t);
26633 vec_safe_push (used_rtx_array, rtl);
26634 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26635 array = ggc_vec_alloc<unsigned char> (len);
26636 memcpy (array, TREE_STRING_POINTER (t), len);
26637 l = new_loc_descr (DW_OP_implicit_value, len, 0);
26638 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26639 l->dw_loc_oprnd2.v.val_vec.length = len;
26640 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26641 l->dw_loc_oprnd2.v.val_vec.array = array;
26642 add_AT_loc (ref, DW_AT_location, l);
26643 equate_decl_number_to_die (decl, ref);
26645 return rtl;
26648 /* Helper function of resolve_addr_in_expr. LOC is
26649 a DW_OP_addr followed by DW_OP_stack_value, either at the start
26650 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26651 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
26652 with DW_OP_GNU_implicit_pointer if possible
26653 and return true, if unsuccessful, return false. */
26655 static bool
26656 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26658 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26659 HOST_WIDE_INT offset = 0;
26660 dw_die_ref ref = NULL;
26661 tree decl;
26663 if (GET_CODE (rtl) == CONST
26664 && GET_CODE (XEXP (rtl, 0)) == PLUS
26665 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26667 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26668 rtl = XEXP (XEXP (rtl, 0), 0);
26670 if (GET_CODE (rtl) == CONST_STRING)
26672 size_t len = strlen (XSTR (rtl, 0)) + 1;
26673 tree t = build_string (len, XSTR (rtl, 0));
26674 tree tlen = size_int (len - 1);
26676 TREE_TYPE (t)
26677 = build_array_type (char_type_node, build_index_type (tlen));
26678 rtl = string_cst_pool_decl (t);
26679 if (!rtl)
26680 return false;
26682 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26684 decl = SYMBOL_REF_DECL (rtl);
26685 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
26687 ref = lookup_decl_die (decl);
26688 if (ref && (get_AT (ref, DW_AT_location)
26689 || get_AT (ref, DW_AT_const_value)))
26691 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26692 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26693 loc->dw_loc_oprnd1.val_entry = NULL;
26694 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26695 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26696 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26697 loc->dw_loc_oprnd2.v.val_int = offset;
26698 return true;
26702 return false;
26705 /* Helper function for resolve_addr, handle one location
26706 expression, return false if at least one CONST_STRING or SYMBOL_REF in
26707 the location list couldn't be resolved. */
26709 static bool
26710 resolve_addr_in_expr (dw_loc_descr_ref loc)
26712 dw_loc_descr_ref keep = NULL;
26713 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26714 switch (loc->dw_loc_opc)
26716 case DW_OP_addr:
26717 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26719 if ((prev == NULL
26720 || prev->dw_loc_opc == DW_OP_piece
26721 || prev->dw_loc_opc == DW_OP_bit_piece)
26722 && loc->dw_loc_next
26723 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26724 && !dwarf_strict
26725 && optimize_one_addr_into_implicit_ptr (loc))
26726 break;
26727 return false;
26729 break;
26730 case DW_OP_GNU_addr_index:
26731 case DW_OP_GNU_const_index:
26732 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26733 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26735 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26736 if (!resolve_one_addr (&rtl))
26737 return false;
26738 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26739 loc->dw_loc_oprnd1.val_entry =
26740 add_addr_table_entry (rtl, ate_kind_rtx);
26742 break;
26743 case DW_OP_const4u:
26744 case DW_OP_const8u:
26745 if (loc->dtprel
26746 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26747 return false;
26748 break;
26749 case DW_OP_plus_uconst:
26750 if (size_of_loc_descr (loc)
26751 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26753 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26755 dw_loc_descr_ref repl
26756 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26757 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26758 add_loc_descr (&repl, loc->dw_loc_next);
26759 *loc = *repl;
26761 break;
26762 case DW_OP_implicit_value:
26763 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26764 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26765 return false;
26766 break;
26767 case DW_OP_GNU_implicit_pointer:
26768 case DW_OP_GNU_parameter_ref:
26769 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26771 dw_die_ref ref
26772 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26773 if (ref == NULL)
26774 return false;
26775 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26776 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26777 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26779 break;
26780 case DW_OP_GNU_const_type:
26781 case DW_OP_GNU_regval_type:
26782 case DW_OP_GNU_deref_type:
26783 case DW_OP_GNU_convert:
26784 case DW_OP_GNU_reinterpret:
26785 while (loc->dw_loc_next
26786 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26788 dw_die_ref base1, base2;
26789 unsigned enc1, enc2, size1, size2;
26790 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26791 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26792 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26793 else if (loc->dw_loc_oprnd1.val_class
26794 == dw_val_class_unsigned_const)
26795 break;
26796 else
26797 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26798 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26799 == dw_val_class_unsigned_const)
26800 break;
26801 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26802 gcc_assert (base1->die_tag == DW_TAG_base_type
26803 && base2->die_tag == DW_TAG_base_type);
26804 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26805 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26806 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26807 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26808 if (size1 == size2
26809 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26810 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26811 && loc != keep)
26812 || enc1 == enc2))
26814 /* Optimize away next DW_OP_GNU_convert after
26815 adjusting LOC's base type die reference. */
26816 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26817 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26818 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26819 else
26820 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26821 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26822 continue;
26824 /* Don't change integer DW_OP_GNU_convert after e.g. floating
26825 point typed stack entry. */
26826 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26827 keep = loc->dw_loc_next;
26828 break;
26830 break;
26831 default:
26832 break;
26834 return true;
26837 /* Helper function of resolve_addr. DIE had DW_AT_location of
26838 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26839 and DW_OP_addr couldn't be resolved. resolve_addr has already
26840 removed the DW_AT_location attribute. This function attempts to
26841 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26842 to it or DW_AT_const_value attribute, if possible. */
26844 static void
26845 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26847 if (!VAR_P (decl)
26848 || lookup_decl_die (decl) != die
26849 || DECL_EXTERNAL (decl)
26850 || !TREE_STATIC (decl)
26851 || DECL_INITIAL (decl) == NULL_TREE
26852 || DECL_P (DECL_INITIAL (decl))
26853 || get_AT (die, DW_AT_const_value))
26854 return;
26856 tree init = DECL_INITIAL (decl);
26857 HOST_WIDE_INT offset = 0;
26858 /* For variables that have been optimized away and thus
26859 don't have a memory location, see if we can emit
26860 DW_AT_const_value instead. */
26861 if (tree_add_const_value_attribute (die, init))
26862 return;
26863 if (dwarf_strict)
26864 return;
26865 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26866 and ADDR_EXPR refers to a decl that has DW_AT_location or
26867 DW_AT_const_value (but isn't addressable, otherwise
26868 resolving the original DW_OP_addr wouldn't fail), see if
26869 we can add DW_OP_GNU_implicit_pointer. */
26870 STRIP_NOPS (init);
26871 if (TREE_CODE (init) == POINTER_PLUS_EXPR
26872 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26874 offset = tree_to_shwi (TREE_OPERAND (init, 1));
26875 init = TREE_OPERAND (init, 0);
26876 STRIP_NOPS (init);
26878 if (TREE_CODE (init) != ADDR_EXPR)
26879 return;
26880 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26881 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26882 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26883 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26884 && TREE_OPERAND (init, 0) != decl))
26886 dw_die_ref ref;
26887 dw_loc_descr_ref l;
26889 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26891 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26892 if (!rtl)
26893 return;
26894 decl = SYMBOL_REF_DECL (rtl);
26896 else
26897 decl = TREE_OPERAND (init, 0);
26898 ref = lookup_decl_die (decl);
26899 if (ref == NULL
26900 || (!get_AT (ref, DW_AT_location)
26901 && !get_AT (ref, DW_AT_const_value)))
26902 return;
26903 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26904 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26905 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26906 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26907 add_AT_loc (die, DW_AT_location, l);
26911 /* Return NULL if l is a DWARF expression, or first op that is not
26912 valid DWARF expression. */
26914 static dw_loc_descr_ref
26915 non_dwarf_expression (dw_loc_descr_ref l)
26917 while (l)
26919 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26920 return l;
26921 switch (l->dw_loc_opc)
26923 case DW_OP_regx:
26924 case DW_OP_implicit_value:
26925 case DW_OP_stack_value:
26926 case DW_OP_GNU_implicit_pointer:
26927 case DW_OP_GNU_parameter_ref:
26928 case DW_OP_piece:
26929 case DW_OP_bit_piece:
26930 return l;
26931 default:
26932 break;
26934 l = l->dw_loc_next;
26936 return NULL;
26939 /* Return adjusted copy of EXPR:
26940 If it is empty DWARF expression, return it.
26941 If it is valid non-empty DWARF expression,
26942 return copy of EXPR with copy of DEREF appended to it.
26943 If it is DWARF expression followed by DW_OP_reg{N,x}, return
26944 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
26945 and no DEREF.
26946 If it is DWARF expression followed by DW_OP_stack_value, return
26947 copy of the DWARF expression without anything appended.
26948 Otherwise, return NULL. */
26950 static dw_loc_descr_ref
26951 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
26954 if (expr == NULL)
26955 return NULL;
26957 dw_loc_descr_ref l = non_dwarf_expression (expr);
26958 if (l && l->dw_loc_next)
26959 return NULL;
26961 if (l)
26963 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26964 deref = new_loc_descr ((enum dwarf_location_atom)
26965 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
26966 0, 0);
26967 else
26968 switch (l->dw_loc_opc)
26970 case DW_OP_regx:
26971 deref = new_loc_descr (DW_OP_bregx,
26972 l->dw_loc_oprnd1.v.val_unsigned, 0);
26973 break;
26974 case DW_OP_stack_value:
26975 deref = NULL;
26976 break;
26977 default:
26978 return NULL;
26981 else
26982 deref = new_loc_descr (deref->dw_loc_opc,
26983 deref->dw_loc_oprnd1.v.val_int, 0);
26985 dw_loc_descr_ref ret = NULL, *p = &ret;
26986 while (expr != l)
26988 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
26989 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
26990 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
26991 p = &(*p)->dw_loc_next;
26992 expr = expr->dw_loc_next;
26994 *p = deref;
26995 return ret;
26998 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
26999 or argument, adjust it if needed and return:
27000 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
27001 attribute if present should be removed
27002 0 keep the attribute as is if the referenced var or argument has
27003 only DWARF expression that covers all ranges
27004 1 if the attribute has been successfully adjusted. */
27006 static int
27007 optimize_string_length (dw_attr_node *a)
27009 dw_loc_descr_ref l = AT_loc (a), lv;
27010 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
27011 dw_attr_node *av = get_AT (die, DW_AT_location);
27012 dw_loc_list_ref d;
27013 bool non_dwarf_expr = false;
27015 if (av == NULL)
27016 return -1;
27017 switch (AT_class (av))
27019 case dw_val_class_loc_list:
27020 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
27021 if (d->expr && non_dwarf_expression (d->expr))
27022 non_dwarf_expr = true;
27023 break;
27024 case dw_val_class_loc:
27025 lv = AT_loc (av);
27026 if (lv == NULL)
27027 return -1;
27028 if (non_dwarf_expression (lv))
27029 non_dwarf_expr = true;
27030 break;
27031 default:
27032 return -1;
27035 /* If it is safe to keep DW_OP_call4 in, keep it. */
27036 if (!non_dwarf_expr
27037 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
27038 return 0;
27040 /* If not dereferencing the DW_OP_call4 afterwards, we can just
27041 copy over the DW_AT_location attribute from die to a. */
27042 if (l->dw_loc_next == NULL)
27044 a->dw_attr_val = av->dw_attr_val;
27045 return 1;
27048 dw_loc_list_ref list, *p;
27049 switch (AT_class (av))
27051 case dw_val_class_loc_list:
27052 p = &list;
27053 list = NULL;
27054 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
27056 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
27057 if (lv)
27059 *p = new_loc_list (lv, d->begin, d->end, d->section);
27060 p = &(*p)->dw_loc_next;
27063 if (list == NULL)
27064 return -1;
27065 a->dw_attr_val.val_class = dw_val_class_loc_list;
27066 gen_llsym (list);
27067 *AT_loc_list_ptr (a) = list;
27068 return 1;
27069 case dw_val_class_loc:
27070 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
27071 if (lv == NULL)
27072 return -1;
27073 a->dw_attr_val.v.val_loc = lv;
27074 return 1;
27075 default:
27076 gcc_unreachable ();
27080 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
27081 an address in .rodata section if the string literal is emitted there,
27082 or remove the containing location list or replace DW_AT_const_value
27083 with DW_AT_location and empty location expression, if it isn't found
27084 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
27085 to something that has been emitted in the current CU. */
27087 static void
27088 resolve_addr (dw_die_ref die)
27090 dw_die_ref c;
27091 dw_attr_node *a;
27092 dw_loc_list_ref *curr, *start, loc;
27093 unsigned ix;
27094 bool remove_AT_byte_size = false;
27096 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27097 switch (AT_class (a))
27099 case dw_val_class_loc_list:
27100 start = curr = AT_loc_list_ptr (a);
27101 loc = *curr;
27102 gcc_assert (loc);
27103 /* The same list can be referenced more than once. See if we have
27104 already recorded the result from a previous pass. */
27105 if (loc->replaced)
27106 *curr = loc->dw_loc_next;
27107 else if (!loc->resolved_addr)
27109 /* As things stand, we do not expect or allow one die to
27110 reference a suffix of another die's location list chain.
27111 References must be identical or completely separate.
27112 There is therefore no need to cache the result of this
27113 pass on any list other than the first; doing so
27114 would lead to unnecessary writes. */
27115 while (*curr)
27117 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
27118 if (!resolve_addr_in_expr ((*curr)->expr))
27120 dw_loc_list_ref next = (*curr)->dw_loc_next;
27121 dw_loc_descr_ref l = (*curr)->expr;
27123 if (next && (*curr)->ll_symbol)
27125 gcc_assert (!next->ll_symbol);
27126 next->ll_symbol = (*curr)->ll_symbol;
27128 if (dwarf_split_debug_info)
27129 remove_loc_list_addr_table_entries (l);
27130 *curr = next;
27132 else
27134 mark_base_types ((*curr)->expr);
27135 curr = &(*curr)->dw_loc_next;
27138 if (loc == *start)
27139 loc->resolved_addr = 1;
27140 else
27142 loc->replaced = 1;
27143 loc->dw_loc_next = *start;
27146 if (!*start)
27148 remove_AT (die, a->dw_attr);
27149 ix--;
27151 break;
27152 case dw_val_class_loc:
27154 dw_loc_descr_ref l = AT_loc (a);
27155 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
27156 DW_AT_string_length is only a rough approximation; unfortunately
27157 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
27158 needs a DWARF expression, while DW_AT_location of the referenced
27159 variable or argument might be any location description. */
27160 if (a->dw_attr == DW_AT_string_length
27161 && l
27162 && l->dw_loc_opc == DW_OP_call4
27163 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
27164 && (l->dw_loc_next == NULL
27165 || (l->dw_loc_next->dw_loc_next == NULL
27166 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
27167 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
27169 switch (optimize_string_length (a))
27171 case -1:
27172 remove_AT (die, a->dw_attr);
27173 ix--;
27174 /* If we drop DW_AT_string_length, we need to drop also
27175 DW_AT_{string_length_,}byte_size. */
27176 remove_AT_byte_size = true;
27177 continue;
27178 default:
27179 break;
27180 case 1:
27181 /* Even if we keep the optimized DW_AT_string_length,
27182 it might have changed AT_class, so process it again. */
27183 ix--;
27184 continue;
27187 /* For -gdwarf-2 don't attempt to optimize
27188 DW_AT_data_member_location containing
27189 DW_OP_plus_uconst - older consumers might
27190 rely on it being that op instead of a more complex,
27191 but shorter, location description. */
27192 if ((dwarf_version > 2
27193 || a->dw_attr != DW_AT_data_member_location
27194 || l == NULL
27195 || l->dw_loc_opc != DW_OP_plus_uconst
27196 || l->dw_loc_next != NULL)
27197 && !resolve_addr_in_expr (l))
27199 if (dwarf_split_debug_info)
27200 remove_loc_list_addr_table_entries (l);
27201 if (l != NULL
27202 && l->dw_loc_next == NULL
27203 && l->dw_loc_opc == DW_OP_addr
27204 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
27205 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
27206 && a->dw_attr == DW_AT_location)
27208 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
27209 remove_AT (die, a->dw_attr);
27210 ix--;
27211 optimize_location_into_implicit_ptr (die, decl);
27212 break;
27214 remove_AT (die, a->dw_attr);
27215 ix--;
27217 else
27218 mark_base_types (l);
27220 break;
27221 case dw_val_class_addr:
27222 if (a->dw_attr == DW_AT_const_value
27223 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
27225 if (AT_index (a) != NOT_INDEXED)
27226 remove_addr_table_entry (a->dw_attr_val.val_entry);
27227 remove_AT (die, a->dw_attr);
27228 ix--;
27230 if (die->die_tag == DW_TAG_GNU_call_site
27231 && a->dw_attr == DW_AT_abstract_origin)
27233 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
27234 dw_die_ref tdie = lookup_decl_die (tdecl);
27235 dw_die_ref cdie;
27236 if (tdie == NULL
27237 && DECL_EXTERNAL (tdecl)
27238 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
27239 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
27241 /* Creating a full DIE for tdecl is overly expensive and
27242 at this point even wrong when in the LTO phase
27243 as it can end up generating new type DIEs we didn't
27244 output and thus optimize_external_refs will crash. */
27245 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
27246 add_AT_flag (tdie, DW_AT_external, 1);
27247 add_AT_flag (tdie, DW_AT_declaration, 1);
27248 add_linkage_attr (tdie, tdecl);
27249 add_name_and_src_coords_attributes (tdie, tdecl);
27250 equate_decl_number_to_die (tdecl, tdie);
27252 if (tdie)
27254 a->dw_attr_val.val_class = dw_val_class_die_ref;
27255 a->dw_attr_val.v.val_die_ref.die = tdie;
27256 a->dw_attr_val.v.val_die_ref.external = 0;
27258 else
27260 if (AT_index (a) != NOT_INDEXED)
27261 remove_addr_table_entry (a->dw_attr_val.val_entry);
27262 remove_AT (die, a->dw_attr);
27263 ix--;
27266 break;
27267 default:
27268 break;
27271 if (remove_AT_byte_size)
27272 remove_AT (die, dwarf_version >= 5
27273 ? DW_AT_string_length_byte_size
27274 : DW_AT_byte_size);
27276 FOR_EACH_CHILD (die, c, resolve_addr (c));
27279 /* Helper routines for optimize_location_lists.
27280 This pass tries to share identical local lists in .debug_loc
27281 section. */
27283 /* Iteratively hash operands of LOC opcode into HSTATE. */
27285 static void
27286 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
27288 dw_val_ref val1 = &loc->dw_loc_oprnd1;
27289 dw_val_ref val2 = &loc->dw_loc_oprnd2;
27291 switch (loc->dw_loc_opc)
27293 case DW_OP_const4u:
27294 case DW_OP_const8u:
27295 if (loc->dtprel)
27296 goto hash_addr;
27297 /* FALLTHRU */
27298 case DW_OP_const1u:
27299 case DW_OP_const1s:
27300 case DW_OP_const2u:
27301 case DW_OP_const2s:
27302 case DW_OP_const4s:
27303 case DW_OP_const8s:
27304 case DW_OP_constu:
27305 case DW_OP_consts:
27306 case DW_OP_pick:
27307 case DW_OP_plus_uconst:
27308 case DW_OP_breg0:
27309 case DW_OP_breg1:
27310 case DW_OP_breg2:
27311 case DW_OP_breg3:
27312 case DW_OP_breg4:
27313 case DW_OP_breg5:
27314 case DW_OP_breg6:
27315 case DW_OP_breg7:
27316 case DW_OP_breg8:
27317 case DW_OP_breg9:
27318 case DW_OP_breg10:
27319 case DW_OP_breg11:
27320 case DW_OP_breg12:
27321 case DW_OP_breg13:
27322 case DW_OP_breg14:
27323 case DW_OP_breg15:
27324 case DW_OP_breg16:
27325 case DW_OP_breg17:
27326 case DW_OP_breg18:
27327 case DW_OP_breg19:
27328 case DW_OP_breg20:
27329 case DW_OP_breg21:
27330 case DW_OP_breg22:
27331 case DW_OP_breg23:
27332 case DW_OP_breg24:
27333 case DW_OP_breg25:
27334 case DW_OP_breg26:
27335 case DW_OP_breg27:
27336 case DW_OP_breg28:
27337 case DW_OP_breg29:
27338 case DW_OP_breg30:
27339 case DW_OP_breg31:
27340 case DW_OP_regx:
27341 case DW_OP_fbreg:
27342 case DW_OP_piece:
27343 case DW_OP_deref_size:
27344 case DW_OP_xderef_size:
27345 hstate.add_object (val1->v.val_int);
27346 break;
27347 case DW_OP_skip:
27348 case DW_OP_bra:
27350 int offset;
27352 gcc_assert (val1->val_class == dw_val_class_loc);
27353 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
27354 hstate.add_object (offset);
27356 break;
27357 case DW_OP_implicit_value:
27358 hstate.add_object (val1->v.val_unsigned);
27359 switch (val2->val_class)
27361 case dw_val_class_const:
27362 hstate.add_object (val2->v.val_int);
27363 break;
27364 case dw_val_class_vec:
27366 unsigned int elt_size = val2->v.val_vec.elt_size;
27367 unsigned int len = val2->v.val_vec.length;
27369 hstate.add_int (elt_size);
27370 hstate.add_int (len);
27371 hstate.add (val2->v.val_vec.array, len * elt_size);
27373 break;
27374 case dw_val_class_const_double:
27375 hstate.add_object (val2->v.val_double.low);
27376 hstate.add_object (val2->v.val_double.high);
27377 break;
27378 case dw_val_class_wide_int:
27379 hstate.add (val2->v.val_wide->get_val (),
27380 get_full_len (*val2->v.val_wide)
27381 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27382 break;
27383 case dw_val_class_addr:
27384 inchash::add_rtx (val2->v.val_addr, hstate);
27385 break;
27386 default:
27387 gcc_unreachable ();
27389 break;
27390 case DW_OP_bregx:
27391 case DW_OP_bit_piece:
27392 hstate.add_object (val1->v.val_int);
27393 hstate.add_object (val2->v.val_int);
27394 break;
27395 case DW_OP_addr:
27396 hash_addr:
27397 if (loc->dtprel)
27399 unsigned char dtprel = 0xd1;
27400 hstate.add_object (dtprel);
27402 inchash::add_rtx (val1->v.val_addr, hstate);
27403 break;
27404 case DW_OP_GNU_addr_index:
27405 case DW_OP_GNU_const_index:
27407 if (loc->dtprel)
27409 unsigned char dtprel = 0xd1;
27410 hstate.add_object (dtprel);
27412 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
27414 break;
27415 case DW_OP_GNU_implicit_pointer:
27416 hstate.add_int (val2->v.val_int);
27417 break;
27418 case DW_OP_GNU_entry_value:
27419 hstate.add_object (val1->v.val_loc);
27420 break;
27421 case DW_OP_GNU_regval_type:
27422 case DW_OP_GNU_deref_type:
27424 unsigned int byte_size
27425 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
27426 unsigned int encoding
27427 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
27428 hstate.add_object (val1->v.val_int);
27429 hstate.add_object (byte_size);
27430 hstate.add_object (encoding);
27432 break;
27433 case DW_OP_GNU_convert:
27434 case DW_OP_GNU_reinterpret:
27435 if (val1->val_class == dw_val_class_unsigned_const)
27437 hstate.add_object (val1->v.val_unsigned);
27438 break;
27440 /* FALLTHRU */
27441 case DW_OP_GNU_const_type:
27443 unsigned int byte_size
27444 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
27445 unsigned int encoding
27446 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
27447 hstate.add_object (byte_size);
27448 hstate.add_object (encoding);
27449 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
27450 break;
27451 hstate.add_object (val2->val_class);
27452 switch (val2->val_class)
27454 case dw_val_class_const:
27455 hstate.add_object (val2->v.val_int);
27456 break;
27457 case dw_val_class_vec:
27459 unsigned int elt_size = val2->v.val_vec.elt_size;
27460 unsigned int len = val2->v.val_vec.length;
27462 hstate.add_object (elt_size);
27463 hstate.add_object (len);
27464 hstate.add (val2->v.val_vec.array, len * elt_size);
27466 break;
27467 case dw_val_class_const_double:
27468 hstate.add_object (val2->v.val_double.low);
27469 hstate.add_object (val2->v.val_double.high);
27470 break;
27471 case dw_val_class_wide_int:
27472 hstate.add (val2->v.val_wide->get_val (),
27473 get_full_len (*val2->v.val_wide)
27474 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27475 break;
27476 default:
27477 gcc_unreachable ();
27480 break;
27482 default:
27483 /* Other codes have no operands. */
27484 break;
27488 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
27490 static inline void
27491 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
27493 dw_loc_descr_ref l;
27494 bool sizes_computed = false;
27495 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
27496 size_of_locs (loc);
27498 for (l = loc; l != NULL; l = l->dw_loc_next)
27500 enum dwarf_location_atom opc = l->dw_loc_opc;
27501 hstate.add_object (opc);
27502 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
27504 size_of_locs (loc);
27505 sizes_computed = true;
27507 hash_loc_operands (l, hstate);
27511 /* Compute hash of the whole location list LIST_HEAD. */
27513 static inline void
27514 hash_loc_list (dw_loc_list_ref list_head)
27516 dw_loc_list_ref curr = list_head;
27517 inchash::hash hstate;
27519 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
27521 hstate.add (curr->begin, strlen (curr->begin) + 1);
27522 hstate.add (curr->end, strlen (curr->end) + 1);
27523 if (curr->section)
27524 hstate.add (curr->section, strlen (curr->section) + 1);
27525 hash_locs (curr->expr, hstate);
27527 list_head->hash = hstate.end ();
27530 /* Return true if X and Y opcodes have the same operands. */
27532 static inline bool
27533 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
27535 dw_val_ref valx1 = &x->dw_loc_oprnd1;
27536 dw_val_ref valx2 = &x->dw_loc_oprnd2;
27537 dw_val_ref valy1 = &y->dw_loc_oprnd1;
27538 dw_val_ref valy2 = &y->dw_loc_oprnd2;
27540 switch (x->dw_loc_opc)
27542 case DW_OP_const4u:
27543 case DW_OP_const8u:
27544 if (x->dtprel)
27545 goto hash_addr;
27546 /* FALLTHRU */
27547 case DW_OP_const1u:
27548 case DW_OP_const1s:
27549 case DW_OP_const2u:
27550 case DW_OP_const2s:
27551 case DW_OP_const4s:
27552 case DW_OP_const8s:
27553 case DW_OP_constu:
27554 case DW_OP_consts:
27555 case DW_OP_pick:
27556 case DW_OP_plus_uconst:
27557 case DW_OP_breg0:
27558 case DW_OP_breg1:
27559 case DW_OP_breg2:
27560 case DW_OP_breg3:
27561 case DW_OP_breg4:
27562 case DW_OP_breg5:
27563 case DW_OP_breg6:
27564 case DW_OP_breg7:
27565 case DW_OP_breg8:
27566 case DW_OP_breg9:
27567 case DW_OP_breg10:
27568 case DW_OP_breg11:
27569 case DW_OP_breg12:
27570 case DW_OP_breg13:
27571 case DW_OP_breg14:
27572 case DW_OP_breg15:
27573 case DW_OP_breg16:
27574 case DW_OP_breg17:
27575 case DW_OP_breg18:
27576 case DW_OP_breg19:
27577 case DW_OP_breg20:
27578 case DW_OP_breg21:
27579 case DW_OP_breg22:
27580 case DW_OP_breg23:
27581 case DW_OP_breg24:
27582 case DW_OP_breg25:
27583 case DW_OP_breg26:
27584 case DW_OP_breg27:
27585 case DW_OP_breg28:
27586 case DW_OP_breg29:
27587 case DW_OP_breg30:
27588 case DW_OP_breg31:
27589 case DW_OP_regx:
27590 case DW_OP_fbreg:
27591 case DW_OP_piece:
27592 case DW_OP_deref_size:
27593 case DW_OP_xderef_size:
27594 return valx1->v.val_int == valy1->v.val_int;
27595 case DW_OP_skip:
27596 case DW_OP_bra:
27597 /* If splitting debug info, the use of DW_OP_GNU_addr_index
27598 can cause irrelevant differences in dw_loc_addr. */
27599 gcc_assert (valx1->val_class == dw_val_class_loc
27600 && valy1->val_class == dw_val_class_loc
27601 && (dwarf_split_debug_info
27602 || x->dw_loc_addr == y->dw_loc_addr));
27603 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27604 case DW_OP_implicit_value:
27605 if (valx1->v.val_unsigned != valy1->v.val_unsigned
27606 || valx2->val_class != valy2->val_class)
27607 return false;
27608 switch (valx2->val_class)
27610 case dw_val_class_const:
27611 return valx2->v.val_int == valy2->v.val_int;
27612 case dw_val_class_vec:
27613 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27614 && valx2->v.val_vec.length == valy2->v.val_vec.length
27615 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27616 valx2->v.val_vec.elt_size
27617 * valx2->v.val_vec.length) == 0;
27618 case dw_val_class_const_double:
27619 return valx2->v.val_double.low == valy2->v.val_double.low
27620 && valx2->v.val_double.high == valy2->v.val_double.high;
27621 case dw_val_class_wide_int:
27622 return *valx2->v.val_wide == *valy2->v.val_wide;
27623 case dw_val_class_addr:
27624 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27625 default:
27626 gcc_unreachable ();
27628 case DW_OP_bregx:
27629 case DW_OP_bit_piece:
27630 return valx1->v.val_int == valy1->v.val_int
27631 && valx2->v.val_int == valy2->v.val_int;
27632 case DW_OP_addr:
27633 hash_addr:
27634 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27635 case DW_OP_GNU_addr_index:
27636 case DW_OP_GNU_const_index:
27638 rtx ax1 = valx1->val_entry->addr.rtl;
27639 rtx ay1 = valy1->val_entry->addr.rtl;
27640 return rtx_equal_p (ax1, ay1);
27642 case DW_OP_GNU_implicit_pointer:
27643 return valx1->val_class == dw_val_class_die_ref
27644 && valx1->val_class == valy1->val_class
27645 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27646 && valx2->v.val_int == valy2->v.val_int;
27647 case DW_OP_GNU_entry_value:
27648 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27649 case DW_OP_GNU_const_type:
27650 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27651 || valx2->val_class != valy2->val_class)
27652 return false;
27653 switch (valx2->val_class)
27655 case dw_val_class_const:
27656 return valx2->v.val_int == valy2->v.val_int;
27657 case dw_val_class_vec:
27658 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27659 && valx2->v.val_vec.length == valy2->v.val_vec.length
27660 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27661 valx2->v.val_vec.elt_size
27662 * valx2->v.val_vec.length) == 0;
27663 case dw_val_class_const_double:
27664 return valx2->v.val_double.low == valy2->v.val_double.low
27665 && valx2->v.val_double.high == valy2->v.val_double.high;
27666 case dw_val_class_wide_int:
27667 return *valx2->v.val_wide == *valy2->v.val_wide;
27668 default:
27669 gcc_unreachable ();
27671 case DW_OP_GNU_regval_type:
27672 case DW_OP_GNU_deref_type:
27673 return valx1->v.val_int == valy1->v.val_int
27674 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27675 case DW_OP_GNU_convert:
27676 case DW_OP_GNU_reinterpret:
27677 if (valx1->val_class != valy1->val_class)
27678 return false;
27679 if (valx1->val_class == dw_val_class_unsigned_const)
27680 return valx1->v.val_unsigned == valy1->v.val_unsigned;
27681 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27682 case DW_OP_GNU_parameter_ref:
27683 return valx1->val_class == dw_val_class_die_ref
27684 && valx1->val_class == valy1->val_class
27685 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27686 default:
27687 /* Other codes have no operands. */
27688 return true;
27692 /* Return true if DWARF location expressions X and Y are the same. */
27694 static inline bool
27695 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27697 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27698 if (x->dw_loc_opc != y->dw_loc_opc
27699 || x->dtprel != y->dtprel
27700 || !compare_loc_operands (x, y))
27701 break;
27702 return x == NULL && y == NULL;
27705 /* Hashtable helpers. */
27707 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27709 static inline hashval_t hash (const dw_loc_list_struct *);
27710 static inline bool equal (const dw_loc_list_struct *,
27711 const dw_loc_list_struct *);
27714 /* Return precomputed hash of location list X. */
27716 inline hashval_t
27717 loc_list_hasher::hash (const dw_loc_list_struct *x)
27719 return x->hash;
27722 /* Return true if location lists A and B are the same. */
27724 inline bool
27725 loc_list_hasher::equal (const dw_loc_list_struct *a,
27726 const dw_loc_list_struct *b)
27728 if (a == b)
27729 return 1;
27730 if (a->hash != b->hash)
27731 return 0;
27732 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27733 if (strcmp (a->begin, b->begin) != 0
27734 || strcmp (a->end, b->end) != 0
27735 || (a->section == NULL) != (b->section == NULL)
27736 || (a->section && strcmp (a->section, b->section) != 0)
27737 || !compare_locs (a->expr, b->expr))
27738 break;
27739 return a == NULL && b == NULL;
27742 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27745 /* Recursively optimize location lists referenced from DIE
27746 children and share them whenever possible. */
27748 static void
27749 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27751 dw_die_ref c;
27752 dw_attr_node *a;
27753 unsigned ix;
27754 dw_loc_list_struct **slot;
27756 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27757 if (AT_class (a) == dw_val_class_loc_list)
27759 dw_loc_list_ref list = AT_loc_list (a);
27760 /* TODO: perform some optimizations here, before hashing
27761 it and storing into the hash table. */
27762 hash_loc_list (list);
27763 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27764 if (*slot == NULL)
27765 *slot = list;
27766 else
27767 a->dw_attr_val.v.val_loc_list = *slot;
27770 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27774 /* Recursively assign each location list a unique index into the debug_addr
27775 section. */
27777 static void
27778 index_location_lists (dw_die_ref die)
27780 dw_die_ref c;
27781 dw_attr_node *a;
27782 unsigned ix;
27784 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27785 if (AT_class (a) == dw_val_class_loc_list)
27787 dw_loc_list_ref list = AT_loc_list (a);
27788 dw_loc_list_ref curr;
27789 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27791 /* Don't index an entry that has already been indexed
27792 or won't be output. */
27793 if (curr->begin_entry != NULL
27794 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27795 continue;
27797 curr->begin_entry
27798 = add_addr_table_entry (xstrdup (curr->begin),
27799 ate_kind_label);
27803 FOR_EACH_CHILD (die, c, index_location_lists (c));
27806 /* Optimize location lists referenced from DIE
27807 children and share them whenever possible. */
27809 static void
27810 optimize_location_lists (dw_die_ref die)
27812 loc_list_hash_type htab (500);
27813 optimize_location_lists_1 (die, &htab);
27816 /* Traverse the limbo die list, and add parent/child links. The only
27817 dies without parents that should be here are concrete instances of
27818 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
27819 For concrete instances, we can get the parent die from the abstract
27820 instance. */
27822 static void
27823 flush_limbo_die_list (void)
27825 limbo_die_node *node;
27827 /* get_context_die calls force_decl_die, which can put new DIEs on the
27828 limbo list in LTO mode when nested functions are put in a different
27829 partition than that of their parent function. */
27830 while ((node = limbo_die_list))
27832 dw_die_ref die = node->die;
27833 limbo_die_list = node->next;
27835 if (die->die_parent == NULL)
27837 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27839 if (origin && origin->die_parent)
27840 add_child_die (origin->die_parent, die);
27841 else if (is_cu_die (die))
27843 else if (seen_error ())
27844 /* It's OK to be confused by errors in the input. */
27845 add_child_die (comp_unit_die (), die);
27846 else
27848 /* In certain situations, the lexical block containing a
27849 nested function can be optimized away, which results
27850 in the nested function die being orphaned. Likewise
27851 with the return type of that nested function. Force
27852 this to be a child of the containing function.
27854 It may happen that even the containing function got fully
27855 inlined and optimized out. In that case we are lost and
27856 assign the empty child. This should not be big issue as
27857 the function is likely unreachable too. */
27858 gcc_assert (node->created_for);
27860 if (DECL_P (node->created_for))
27861 origin = get_context_die (DECL_CONTEXT (node->created_for));
27862 else if (TYPE_P (node->created_for))
27863 origin = scope_die_for (node->created_for, comp_unit_die ());
27864 else
27865 origin = comp_unit_die ();
27867 add_child_die (origin, die);
27873 /* Output stuff that dwarf requires at the end of every file,
27874 and generate the DWARF-2 debugging info. */
27876 static void
27877 dwarf2out_finish (const char *)
27879 comdat_type_node *ctnode;
27880 dw_die_ref main_comp_unit_die;
27882 /* Flush out any latecomers to the limbo party. */
27883 flush_limbo_die_list ();
27885 if (flag_checking)
27887 verify_die (comp_unit_die ());
27888 for (limbo_die_node *node = cu_die_list; node; node = node->next)
27889 verify_die (node->die);
27892 /* We shouldn't have any symbols with delayed asm names for
27893 DIEs generated after early finish. */
27894 gcc_assert (deferred_asm_name == NULL);
27896 gen_remaining_tmpl_value_param_die_attribute ();
27898 #if ENABLE_ASSERT_CHECKING
27900 dw_die_ref die = comp_unit_die (), c;
27901 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27903 #endif
27904 resolve_addr (comp_unit_die ());
27905 move_marked_base_types ();
27907 /* Initialize sections and labels used for actual assembler output. */
27908 init_sections_and_labels ();
27910 /* Traverse the DIE's and add sibling attributes to those DIE's that
27911 have children. */
27912 add_sibling_attributes (comp_unit_die ());
27913 limbo_die_node *node;
27914 for (node = cu_die_list; node; node = node->next)
27915 add_sibling_attributes (node->die);
27916 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27917 add_sibling_attributes (ctnode->root_die);
27919 /* When splitting DWARF info, we put some attributes in the
27920 skeleton compile_unit DIE that remains in the .o, while
27921 most attributes go in the DWO compile_unit_die. */
27922 if (dwarf_split_debug_info)
27924 limbo_die_node *cu;
27925 main_comp_unit_die = gen_compile_unit_die (NULL);
27926 cu = limbo_die_list;
27927 gcc_assert (cu->die == main_comp_unit_die);
27928 limbo_die_list = limbo_die_list->next;
27929 cu->next = cu_die_list;
27930 cu_die_list = cu;
27932 else
27933 main_comp_unit_die = comp_unit_die ();
27935 /* Output a terminator label for the .text section. */
27936 switch_to_section (text_section);
27937 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27938 if (cold_text_section)
27940 switch_to_section (cold_text_section);
27941 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27944 /* We can only use the low/high_pc attributes if all of the code was
27945 in .text. */
27946 if (!have_multiple_function_sections
27947 || (dwarf_version < 3 && dwarf_strict))
27949 /* Don't add if the CU has no associated code. */
27950 if (text_section_used)
27951 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27952 text_end_label, true);
27954 else
27956 unsigned fde_idx;
27957 dw_fde_ref fde;
27958 bool range_list_added = false;
27960 if (text_section_used)
27961 add_ranges_by_labels (main_comp_unit_die, text_section_label,
27962 text_end_label, &range_list_added, true);
27963 if (cold_text_section_used)
27964 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27965 cold_end_label, &range_list_added, true);
27967 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27969 if (DECL_IGNORED_P (fde->decl))
27970 continue;
27971 if (!fde->in_std_section)
27972 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27973 fde->dw_fde_end, &range_list_added,
27974 true);
27975 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27976 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27977 fde->dw_fde_second_end, &range_list_added,
27978 true);
27981 if (range_list_added)
27983 /* We need to give .debug_loc and .debug_ranges an appropriate
27984 "base address". Use zero so that these addresses become
27985 absolute. Historically, we've emitted the unexpected
27986 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27987 Emit both to give time for other tools to adapt. */
27988 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27989 if (! dwarf_strict && dwarf_version < 4)
27990 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27992 add_ranges (NULL);
27996 if (debug_info_level >= DINFO_LEVEL_TERSE)
27997 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27998 debug_line_section_label);
28000 if (have_macinfo)
28001 add_AT_macptr (comp_unit_die (),
28002 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
28003 macinfo_section_label);
28005 if (dwarf_split_debug_info)
28007 /* optimize_location_lists calculates the size of the lists,
28008 so index them first, and assign indices to the entries.
28009 Although optimize_location_lists will remove entries from
28010 the table, it only does so for duplicates, and therefore
28011 only reduces ref_counts to 1. */
28012 index_location_lists (comp_unit_die ());
28014 if (addr_index_table != NULL)
28016 unsigned int index = 0;
28017 addr_index_table
28018 ->traverse_noresize<unsigned int *, index_addr_table_entry>
28019 (&index);
28023 if (have_location_lists)
28024 optimize_location_lists (comp_unit_die ());
28026 save_macinfo_strings ();
28028 if (dwarf_split_debug_info)
28030 unsigned int index = 0;
28032 /* Add attributes common to skeleton compile_units and
28033 type_units. Because these attributes include strings, it
28034 must be done before freezing the string table. Top-level
28035 skeleton die attrs are added when the skeleton type unit is
28036 created, so ensure it is created by this point. */
28037 add_top_level_skeleton_die_attrs (main_comp_unit_die);
28038 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
28041 /* Output all of the compilation units. We put the main one last so that
28042 the offsets are available to output_pubnames. */
28043 for (node = cu_die_list; node; node = node->next)
28044 output_comp_unit (node->die, 0);
28046 hash_table<comdat_type_hasher> comdat_type_table (100);
28047 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
28049 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
28051 /* Don't output duplicate types. */
28052 if (*slot != HTAB_EMPTY_ENTRY)
28053 continue;
28055 /* Add a pointer to the line table for the main compilation unit
28056 so that the debugger can make sense of DW_AT_decl_file
28057 attributes. */
28058 if (debug_info_level >= DINFO_LEVEL_TERSE)
28059 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
28060 (!dwarf_split_debug_info
28061 ? debug_line_section_label
28062 : debug_skeleton_line_section_label));
28064 output_comdat_type_unit (ctnode);
28065 *slot = ctnode;
28068 /* The AT_pubnames attribute needs to go in all skeleton dies, including
28069 both the main_cu and all skeleton TUs. Making this call unconditional
28070 would end up either adding a second copy of the AT_pubnames attribute, or
28071 requiring a special case in add_top_level_skeleton_die_attrs. */
28072 if (!dwarf_split_debug_info)
28073 add_AT_pubnames (comp_unit_die ());
28075 if (dwarf_split_debug_info)
28077 int mark;
28078 unsigned char checksum[16];
28079 struct md5_ctx ctx;
28081 /* Compute a checksum of the comp_unit to use as the dwo_id. */
28082 md5_init_ctx (&ctx);
28083 mark = 0;
28084 die_checksum (comp_unit_die (), &ctx, &mark);
28085 unmark_all_dies (comp_unit_die ());
28086 md5_finish_ctx (&ctx, checksum);
28088 /* Use the first 8 bytes of the checksum as the dwo_id,
28089 and add it to both comp-unit DIEs. */
28090 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
28091 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
28093 /* Add the base offset of the ranges table to the skeleton
28094 comp-unit DIE. */
28095 if (!vec_safe_is_empty (ranges_table))
28096 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
28097 ranges_section_label);
28099 switch_to_section (debug_addr_section);
28100 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
28101 output_addr_table ();
28104 /* Output the main compilation unit if non-empty or if .debug_macinfo
28105 or .debug_macro will be emitted. */
28106 output_comp_unit (comp_unit_die (), have_macinfo);
28108 if (dwarf_split_debug_info && info_section_emitted)
28109 output_skeleton_debug_sections (main_comp_unit_die);
28111 /* Output the abbreviation table. */
28112 if (abbrev_die_table_in_use != 1)
28114 switch_to_section (debug_abbrev_section);
28115 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
28116 output_abbrev_section ();
28119 /* Output location list section if necessary. */
28120 if (have_location_lists)
28122 /* Output the location lists info. */
28123 switch_to_section (debug_loc_section);
28124 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
28125 output_location_lists (comp_unit_die ());
28128 output_pubtables ();
28130 /* Output the address range information if a CU (.debug_info section)
28131 was emitted. We output an empty table even if we had no functions
28132 to put in it. This because the consumer has no way to tell the
28133 difference between an empty table that we omitted and failure to
28134 generate a table that would have contained data. */
28135 if (info_section_emitted)
28137 switch_to_section (debug_aranges_section);
28138 output_aranges ();
28141 /* Output ranges section if necessary. */
28142 if (!vec_safe_is_empty (ranges_table))
28144 switch_to_section (debug_ranges_section);
28145 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
28146 output_ranges ();
28149 /* Have to end the macro section. */
28150 if (have_macinfo)
28152 switch_to_section (debug_macinfo_section);
28153 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
28154 output_macinfo ();
28155 dw2_asm_output_data (1, 0, "End compilation unit");
28158 /* Output the source line correspondence table. We must do this
28159 even if there is no line information. Otherwise, on an empty
28160 translation unit, we will generate a present, but empty,
28161 .debug_info section. IRIX 6.5 `nm' will then complain when
28162 examining the file. This is done late so that any filenames
28163 used by the debug_info section are marked as 'used'. */
28164 switch_to_section (debug_line_section);
28165 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
28166 if (! DWARF2_ASM_LINE_DEBUG_INFO)
28167 output_line_info (false);
28169 if (dwarf_split_debug_info && info_section_emitted)
28171 switch_to_section (debug_skeleton_line_section);
28172 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
28173 output_line_info (true);
28176 /* If we emitted any indirect strings, output the string table too. */
28177 if (debug_str_hash || skeleton_debug_str_hash)
28178 output_indirect_strings ();
28181 /* Perform any cleanups needed after the early debug generation pass
28182 has run. */
28184 static void
28185 dwarf2out_early_finish (const char *filename)
28187 set_early_dwarf s;
28189 /* PCH might result in DW_AT_producer string being restored from the
28190 header compilation, so always fill it with empty string initially
28191 and overwrite only here. */
28192 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
28193 producer_string = gen_producer_string ();
28194 producer->dw_attr_val.v.val_str->refcount--;
28195 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
28197 /* Add the name for the main input file now. We delayed this from
28198 dwarf2out_init to avoid complications with PCH. */
28199 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
28200 add_comp_dir_attribute (comp_unit_die ());
28202 /* With LTO early dwarf was really finished at compile-time, so make
28203 sure to adjust the phase after annotating the LTRANS CU DIE. */
28204 if (in_lto_p)
28206 early_dwarf_finished = true;
28207 return;
28210 /* Walk through the list of incomplete types again, trying once more to
28211 emit full debugging info for them. */
28212 retry_incomplete_types ();
28214 /* The point here is to flush out the limbo list so that it is empty
28215 and we don't need to stream it for LTO. */
28216 flush_limbo_die_list ();
28218 gen_scheduled_generic_parms_dies ();
28219 gen_remaining_tmpl_value_param_die_attribute ();
28221 /* Add DW_AT_linkage_name for all deferred DIEs. */
28222 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
28224 tree decl = node->created_for;
28225 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
28226 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
28227 ended up in deferred_asm_name before we knew it was
28228 constant and never written to disk. */
28229 && DECL_ASSEMBLER_NAME (decl))
28231 add_linkage_attr (node->die, decl);
28232 move_linkage_attr (node->die);
28235 deferred_asm_name = NULL;
28237 if (flag_eliminate_unused_debug_types)
28238 prune_unused_types ();
28240 /* Generate separate COMDAT sections for type DIEs. */
28241 if (use_debug_types)
28243 break_out_comdat_types (comp_unit_die ());
28245 /* Each new type_unit DIE was added to the limbo die list when created.
28246 Since these have all been added to comdat_type_list, clear the
28247 limbo die list. */
28248 limbo_die_list = NULL;
28250 /* For each new comdat type unit, copy declarations for incomplete
28251 types to make the new unit self-contained (i.e., no direct
28252 references to the main compile unit). */
28253 for (comdat_type_node *ctnode = comdat_type_list;
28254 ctnode != NULL; ctnode = ctnode->next)
28255 copy_decls_for_unworthy_types (ctnode->root_die);
28256 copy_decls_for_unworthy_types (comp_unit_die ());
28258 /* In the process of copying declarations from one unit to another,
28259 we may have left some declarations behind that are no longer
28260 referenced. Prune them. */
28261 prune_unused_types ();
28264 /* Generate separate CUs for each of the include files we've seen.
28265 They will go into limbo_die_list and from there to cu_die_list. */
28266 if (flag_eliminate_dwarf2_dups)
28268 gcc_assert (limbo_die_list == NULL);
28269 break_out_includes (comp_unit_die ());
28270 limbo_die_node *cu;
28271 while ((cu = limbo_die_list))
28273 limbo_die_list = cu->next;
28274 cu->next = cu_die_list;
28275 cu_die_list = cu;
28279 /* The early debug phase is now finished. */
28280 early_dwarf_finished = true;
28283 /* Reset all state within dwarf2out.c so that we can rerun the compiler
28284 within the same process. For use by toplev::finalize. */
28286 void
28287 dwarf2out_c_finalize (void)
28289 last_var_location_insn = NULL;
28290 cached_next_real_insn = NULL;
28291 used_rtx_array = NULL;
28292 incomplete_types = NULL;
28293 decl_scope_table = NULL;
28294 debug_info_section = NULL;
28295 debug_skeleton_info_section = NULL;
28296 debug_abbrev_section = NULL;
28297 debug_skeleton_abbrev_section = NULL;
28298 debug_aranges_section = NULL;
28299 debug_addr_section = NULL;
28300 debug_macinfo_section = NULL;
28301 debug_line_section = NULL;
28302 debug_skeleton_line_section = NULL;
28303 debug_loc_section = NULL;
28304 debug_pubnames_section = NULL;
28305 debug_pubtypes_section = NULL;
28306 debug_str_section = NULL;
28307 debug_str_dwo_section = NULL;
28308 debug_str_offsets_section = NULL;
28309 debug_ranges_section = NULL;
28310 debug_frame_section = NULL;
28311 fde_vec = NULL;
28312 debug_str_hash = NULL;
28313 skeleton_debug_str_hash = NULL;
28314 dw2_string_counter = 0;
28315 have_multiple_function_sections = false;
28316 text_section_used = false;
28317 cold_text_section_used = false;
28318 cold_text_section = NULL;
28319 current_unit_personality = NULL;
28321 next_die_offset = 0;
28322 single_comp_unit_die = NULL;
28323 comdat_type_list = NULL;
28324 limbo_die_list = NULL;
28325 file_table = NULL;
28326 decl_die_table = NULL;
28327 common_block_die_table = NULL;
28328 decl_loc_table = NULL;
28329 call_arg_locations = NULL;
28330 call_arg_loc_last = NULL;
28331 call_site_count = -1;
28332 tail_call_site_count = -1;
28333 cached_dw_loc_list_table = NULL;
28334 abbrev_die_table = NULL;
28335 abbrev_die_table_allocated = 0;
28336 abbrev_die_table_in_use = 0;
28337 delete dwarf_proc_stack_usage_map;
28338 dwarf_proc_stack_usage_map = NULL;
28339 line_info_label_num = 0;
28340 cur_line_info_table = NULL;
28341 text_section_line_info = NULL;
28342 cold_text_section_line_info = NULL;
28343 separate_line_info = NULL;
28344 info_section_emitted = false;
28345 pubname_table = NULL;
28346 pubtype_table = NULL;
28347 macinfo_table = NULL;
28348 ranges_table = NULL;
28349 ranges_by_label = NULL;
28350 have_location_lists = false;
28351 loclabel_num = 0;
28352 poc_label_num = 0;
28353 last_emitted_file = NULL;
28354 label_num = 0;
28355 tmpl_value_parm_die_table = NULL;
28356 generic_type_instances = NULL;
28357 frame_pointer_fb_offset = 0;
28358 frame_pointer_fb_offset_valid = false;
28359 base_types.release ();
28360 XDELETEVEC (producer_string);
28361 producer_string = NULL;
28364 #include "gt-dwarf2out.h"